Skip to content

Commit

Permalink
remove old lookup builder functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 8, 2024
1 parent 102402e commit a2e3429
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 33 deletions.
24 changes: 1 addition & 23 deletions src/main/kotlin/org/move/lang/core/completion/LookupElements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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,
Expand All @@ -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<LookupElement> = 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(" ")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -43,8 +45,9 @@ class BuiltInFunctionLookupTest: MvTestBase() {
val moduleElement = myFixture.findElementInEditor<MvModule>()
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -196,8 +198,11 @@ class LookupElementTest: MvTestBase() {
val element = myFixture.findElementInEditor<T>() 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,
Expand Down

0 comments on commit a2e3429

Please sign in to comment.