From a2e342965f4bf392046124beb5635e5b7abc0319 Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Fri, 8 Nov 2024 14:02:11 +0100 Subject: [PATCH] remove old lookup builder functions --- .../lang/core/completion/LookupElements.kt | 24 +------------------ .../providers/MvPathCompletionProvider2.kt | 5 ++-- .../providers/SpecItemCompletionProvider.kt | 5 +++- .../StructFieldsCompletionProvider.kt | 7 ++++-- .../lookups/BuiltInFunctionLookupTest.kt | 9 ++++--- .../completion/lookups/LookupElementTest.kt | 9 +++++-- 6 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt b/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt index d92be9279..dd62f211e 100644 --- a/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt +++ b/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt @@ -8,13 +8,12 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.openapi.editor.EditorModificationUtil import com.intellij.psi.PsiElement import com.intellij.psi.util.PsiTreeUtil -import org.move.ide.presentation.text import org.move.lang.core.psi.* import org.move.lang.core.psi.ext.* +import org.move.lang.core.resolve.ScopeEntry import org.move.lang.core.resolve2.ref.ResolutionContext import org.move.lang.core.types.infer.* import org.move.lang.core.types.ty.Ty -import org.move.lang.core.types.ty.TyUnknown const val KEYWORD_PRIORITY = 80.0 @@ -48,12 +47,6 @@ const val VECTOR_LITERAL_PRIORITY = 30.0 //const val MACRO_PRIORITY = -0.1 //const val DEPRECATED_PRIORITY = -1.0 -fun MvNamedElement.createLookupElementWithIcon(): LookupElementBuilder { - return LookupElementBuilder - .createWithIcon(this) - .withLookupString(this.name ?: "") -} - data class MvCompletionContext( val contextElement: MvElement, val msl: Boolean, @@ -62,21 +55,6 @@ data class MvCompletionContext( val structAsType: Boolean = false ) -fun MvNamedElement.createLookupFromNamedElement( - completionCtx: MvCompletionContext, - subst: Substitution = emptySubstitution, - priority: Double = DEFAULT_PRIORITY, - insertHandler: InsertHandler = DefaultInsertHandler(completionCtx), -): LookupElement { - val scopeName = this.name ?: "" - val builder = - this.getLookupElementBuilder2(completionCtx, scopeName, subst) - .withInsertHandler(insertHandler) - .withPriority(priority) - val props = getLookupElementProperties(this, subst, completionCtx) - return builder.toMvLookupElement(properties = props) -} - fun InsertionContext.addSuffix(suffix: String) { document.insertString(selectionEndOffset, suffix) EditorModificationUtil.moveCaretRelatively(editor, suffix.length) diff --git a/src/main/kotlin/org/move/lang/core/completion/providers/MvPathCompletionProvider2.kt b/src/main/kotlin/org/move/lang/core/completion/providers/MvPathCompletionProvider2.kt index 855bf0c99..0d6f1bf03 100644 --- a/src/main/kotlin/org/move/lang/core/completion/providers/MvPathCompletionProvider2.kt +++ b/src/main/kotlin/org/move/lang/core/completion/providers/MvPathCompletionProvider2.kt @@ -12,7 +12,7 @@ import org.move.ide.utils.imports.ImportCandidateCollector import org.move.lang.core.MvPsiPattern.path import org.move.lang.core.completion.MvCompletionContext import org.move.lang.core.completion.UNIMPORTED_ITEM_PRIORITY -import org.move.lang.core.completion.createLookupFromNamedElement +import org.move.lang.core.completion.createLookupElement import org.move.lang.core.completion.getOriginalOrSelf import org.move.lang.core.psi.* import org.move.lang.core.psi.ext.* @@ -132,7 +132,8 @@ object MvPathCompletionProvider2: MvCompletionProvider() { var candidatesCollector = createProcessor { e -> e as CandidateScopeEntry - val lookupElement = e.element.createLookupFromNamedElement( + val lookupElement = createLookupElement( + e, completionContext, priority = UNIMPORTED_ITEM_PRIORITY, insertHandler = ImportInsertHandler(parameters, e.candidate) diff --git a/src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt b/src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt index 5db74e8fd..81f9b05a2 100644 --- a/src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt +++ b/src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt @@ -2,6 +2,7 @@ package org.move.lang.core.completion.providers import com.intellij.codeInsight.completion.CompletionParameters import com.intellij.codeInsight.completion.CompletionResultSet +import com.intellij.codeInsight.lookup.LookupElementBuilder import com.intellij.util.ProcessingContext import org.move.lang.core.MvPsiPattern import org.move.lang.core.completion.addSuffix @@ -24,7 +25,9 @@ object SpecItemCompletionProvider: MvCompletionProvider() { val module = itemSpecRef.itemSpec.module ?: return module.mslSpecifiableItems .forEach { - val lookup = it.createLookupElementWithIcon() + val name = it.name ?: return@forEach + val lookup = LookupElementBuilder.createWithSmartPointer(name, it) + .withIcon(it.getIcon(0)) .withInsertHandler { ctx, _ -> if (!ctx.alreadyHasSpace) ctx.addSuffix(" ") } diff --git a/src/main/kotlin/org/move/lang/core/completion/providers/StructFieldsCompletionProvider.kt b/src/main/kotlin/org/move/lang/core/completion/providers/StructFieldsCompletionProvider.kt index 32509f4cd..ad4714fba 100644 --- a/src/main/kotlin/org/move/lang/core/completion/providers/StructFieldsCompletionProvider.kt +++ b/src/main/kotlin/org/move/lang/core/completion/providers/StructFieldsCompletionProvider.kt @@ -8,9 +8,10 @@ import com.intellij.patterns.StandardPatterns import com.intellij.psi.PsiElement import com.intellij.util.ProcessingContext import org.move.lang.core.completion.MvCompletionContext -import org.move.lang.core.completion.createLookupFromNamedElement +import org.move.lang.core.completion.createLookupElement import org.move.lang.core.psi.* import org.move.lang.core.psi.ext.* +import org.move.lang.core.resolve2.ref.FieldResolveVariant import org.move.lang.core.withParent object StructFieldsCompletionProvider: MvCompletionProvider() { @@ -67,8 +68,10 @@ object StructFieldsCompletionProvider: MvCompletionProvider() { completionContext: MvCompletionContext, ) { for (field in referredStruct.namedFields.filter { it.name !in providedFieldNames }) { + val scopeEntry = FieldResolveVariant(field.name, field) + createLookupElement(scopeEntry, completionContext) result.addElement( - field.createLookupFromNamedElement(completionContext) + createLookupElement(scopeEntry, completionContext) ) } } diff --git a/src/test/kotlin/org/move/lang/completion/lookups/BuiltInFunctionLookupTest.kt b/src/test/kotlin/org/move/lang/completion/lookups/BuiltInFunctionLookupTest.kt index 755bac729..80aa4da56 100644 --- a/src/test/kotlin/org/move/lang/completion/lookups/BuiltInFunctionLookupTest.kt +++ b/src/test/kotlin/org/move/lang/completion/lookups/BuiltInFunctionLookupTest.kt @@ -3,9 +3,11 @@ package org.move.lang.completion.lookups import com.intellij.codeInsight.lookup.LookupElement import com.intellij.codeInsight.lookup.LookupElementPresentation import org.move.lang.core.completion.MvCompletionContext -import org.move.lang.core.completion.createLookupFromNamedElement +import org.move.lang.core.completion.createLookupElement import org.move.lang.core.psi.MvModule import org.move.lang.core.psi.ext.builtinFunctions +import org.move.lang.core.resolve.SimpleScopeEntry +import org.move.lang.core.resolve.ref.NAMES import org.move.utils.tests.MvTestBase import org.move.utils.tests.base.findElementInEditor @@ -43,8 +45,9 @@ class BuiltInFunctionLookupTest: MvTestBase() { val moduleElement = myFixture.findElementInEditor() val lookup = moduleElement.builtinFunctions().single { it.name == name }.let { - it.createLookupFromNamedElement(MvCompletionContext(it, false)) -// it.createLookupElement(CompletionContext(it, ContextScopeInfo.default())) + val scopeEntry = SimpleScopeEntry(name, it, NAMES) + val completionCtx = MvCompletionContext(it, false) + createLookupElement(scopeEntry, completionCtx) } checkLookupPresentation( lookup, diff --git a/src/test/kotlin/org/move/lang/completion/lookups/LookupElementTest.kt b/src/test/kotlin/org/move/lang/completion/lookups/LookupElementTest.kt index 802be2e4c..1535092b8 100644 --- a/src/test/kotlin/org/move/lang/completion/lookups/LookupElementTest.kt +++ b/src/test/kotlin/org/move/lang/completion/lookups/LookupElementTest.kt @@ -9,12 +9,14 @@ import com.intellij.patterns.ElementPattern import com.intellij.psi.NavigatablePsiElement import org.intellij.lang.annotations.Language import org.move.lang.core.completion.MvCompletionContext -import org.move.lang.core.completion.createLookupFromNamedElement +import org.move.lang.core.completion.createLookupElement import org.move.lang.core.completion.providers.MethodOrFieldCompletionProvider import org.move.lang.core.psi.MvElement import org.move.lang.core.psi.MvNamedElement import org.move.lang.core.psi.ext.MvMethodOrField +import org.move.lang.core.resolve.SimpleScopeEntry import org.move.lang.core.resolve.ref.MvReferenceElement +import org.move.lang.core.resolve.ref.NAMES import org.move.utils.tests.MoveV2 import org.move.utils.tests.MvTestBase import org.move.utils.tests.base.findElementInEditor @@ -196,8 +198,11 @@ class LookupElementTest: MvTestBase() { val element = myFixture.findElementInEditor() as? MvNamedElement ?: error("Marker `^` should point to the MvNamedElement") + val name = element.name ?: error("name == null") + val scopeEntry = SimpleScopeEntry(name, element, NAMES) val completionCtx = MvCompletionContext(element, false) - val lookup = element.createLookupFromNamedElement(completionCtx) + + val lookup = createLookupElement(scopeEntry, completionCtx) checkLookupPresentation( lookup, tailText = tailText,