From e9dc2601df1c4ebb8b1873be29ca768cbc80d374 Mon Sep 17 00:00:00 2001 From: Maksim Kurnikov Date: Fri, 8 Nov 2024 21:16:59 +0100 Subject: [PATCH] remove SpecItemCompletionProvider, move code to ReferenceProvider --- .../completion/CommonCompletionContributor.kt | 2 +- .../lang/core/completion/LookupElements.kt | 12 +++++- .../providers/ReferenceCompletionProvider.kt | 9 ++++- .../providers/SpecItemCompletionProvider.kt | 38 ------------------- .../org/move/lang/core/psi/ext/MvItemSpec.kt | 4 -- .../org/move/lang/core/psi/ext/MvModule.kt | 14 +------ .../resolve/ref/MvItemSpecRefReferenceImpl.kt | 34 +++++++++++++---- 7 files changed, 47 insertions(+), 66 deletions(-) delete mode 100644 src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt diff --git a/src/main/kotlin/org/move/lang/core/completion/CommonCompletionContributor.kt b/src/main/kotlin/org/move/lang/core/completion/CommonCompletionContributor.kt index aac24bebd..6810f75a5 100644 --- a/src/main/kotlin/org/move/lang/core/completion/CommonCompletionContributor.kt +++ b/src/main/kotlin/org/move/lang/core/completion/CommonCompletionContributor.kt @@ -14,7 +14,7 @@ import org.move.lang.core.psi.ext.prevNonWsSibling class CommonCompletionContributor: CompletionContributor() { init { extend(CompletionType.BASIC, PrimitiveTypesCompletionProvider) - extend(CompletionType.BASIC, SpecItemCompletionProvider) +// extend(CompletionType.BASIC, SpecItemCompletionProvider) // addresses extend(CompletionType.BASIC, NamedAddressInUseStmtCompletionProvider) 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 dd62f211e..6ac350ac5 100644 --- a/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt +++ b/src/main/kotlin/org/move/lang/core/completion/LookupElements.kt @@ -10,9 +10,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.util.PsiTreeUtil 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 const val KEYWORD_PRIORITY = 80.0 @@ -114,6 +112,16 @@ open class DefaultInsertHandler(val completionCtx: MvCompletionContext? = null): item: LookupElement ) { val document = context.document + + val itemSpecRef = context.getElementOfType() + if (itemSpecRef != null) { + // inserting item in `spec /*caret*/ {}`, no need for the signature, just insert with leading space + if (!context.alreadyHasSpace) { + context.addSuffix(" ") + } + return + } + when (element) { is MvFunctionLike -> { // no suffix for imports diff --git a/src/main/kotlin/org/move/lang/core/completion/providers/ReferenceCompletionProvider.kt b/src/main/kotlin/org/move/lang/core/completion/providers/ReferenceCompletionProvider.kt index fc1420c03..b7a217e85 100644 --- a/src/main/kotlin/org/move/lang/core/completion/providers/ReferenceCompletionProvider.kt +++ b/src/main/kotlin/org/move/lang/core/completion/providers/ReferenceCompletionProvider.kt @@ -7,6 +7,7 @@ import com.intellij.patterns.PlatformPatterns import com.intellij.psi.PsiElement import com.intellij.util.ProcessingContext import org.move.lang.core.completion.MvCompletionContext +import org.move.lang.core.psi.MvItemSpecRef import org.move.lang.core.psi.MvLabel import org.move.lang.core.psi.MvPatBinding import org.move.lang.core.psi.MvPatField @@ -17,6 +18,7 @@ import org.move.lang.core.psiElement import org.move.lang.core.resolve.RsResolveProcessor import org.move.lang.core.resolve.collectCompletionVariants import org.move.lang.core.resolve.ref.MvReferenceElement +import org.move.lang.core.resolve.ref.processItemSpecRefResolveVariants import org.move.lang.core.resolve.wrapWithFilter import org.move.lang.core.resolve2.processLabelResolveVariants import org.move.lang.core.resolve2.processPatBindingResolveVariants @@ -46,9 +48,9 @@ object ReferenceCompletionProvider: MvCompletionProvider() { fun addCompletionVariants( element: MvReferenceElement, result: CompletionResultSet, - context: MvCompletionContext, + completionCtx: MvCompletionContext, ) { - collectCompletionVariants(result, context) { + collectCompletionVariants(result, completionCtx) { val processor0 = filterCompletionVariantsByVisibility(element, it) // todo: filter test functions when (element) { @@ -59,7 +61,10 @@ object ReferenceCompletionProvider: MvCompletionProvider() { val processor = skipAlreadyProvidedFields(element, processor0) processPatBindingResolveVariants(element, true, processor) } + // loop labels is MvLabel -> processLabelResolveVariants(element, it) + // `spec ITEM {}` module items, where ITEM is a reference to the function/struct/enum + is MvItemSpecRef -> processItemSpecRefResolveVariants(element, it) } } } 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 deleted file mode 100644 index 8c7833d12..000000000 --- a/src/main/kotlin/org/move/lang/core/completion/providers/SpecItemCompletionProvider.kt +++ /dev/null @@ -1,38 +0,0 @@ -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 -import org.move.lang.core.completion.alreadyHasSpace -import org.move.lang.core.psi.MvItemSpecRef -import org.move.lang.core.psi.ext.itemSpec -import org.move.lang.core.psi.ext.module -import org.move.lang.core.psi.ext.mslSpecifiableItems - -object SpecItemCompletionProvider: MvCompletionProvider() { - override val elementPattern get() = MvPsiPattern.itemSpecRef() - - override fun addCompletions( - parameters: CompletionParameters, - context: ProcessingContext, - result: CompletionResultSet - ) { - val itemSpecRef = parameters.position.parent as? MvItemSpecRef ?: return - val module = itemSpecRef.itemSpec.module ?: return - module.mslSpecifiableItems - .forEach { - val name = it.name ?: return@forEach - val lookup = LookupElementBuilder.createWithSmartPointer(name, it) - .withIcon(it.getIcon(0)) - .withInsertHandler { ctx, _ -> - if (!ctx.alreadyHasSpace) ctx.addSuffix(" ") - } - result.addElement(lookup) - } - } - - -} diff --git a/src/main/kotlin/org/move/lang/core/psi/ext/MvItemSpec.kt b/src/main/kotlin/org/move/lang/core/psi/ext/MvItemSpec.kt index 494824dc9..572dfb0c5 100644 --- a/src/main/kotlin/org/move/lang/core/psi/ext/MvItemSpec.kt +++ b/src/main/kotlin/org/move/lang/core/psi/ext/MvItemSpec.kt @@ -11,10 +11,6 @@ val MvItemSpec.funcItem get() = this.item as? MvFunction val MvModuleItemSpec.itemSpecBlock: MvSpecCodeBlock? get() = this.childOfType() -val MvModule.mslSpecifiableItems: List - get() = - this.allFunctions().chain(this.structs()).toList() - val MvItemSpec.module: MvModule? get() { val parent = this.parent diff --git a/src/main/kotlin/org/move/lang/core/psi/ext/MvModule.kt b/src/main/kotlin/org/move/lang/core/psi/ext/MvModule.kt index c3354b5b2..1d6e32603 100644 --- a/src/main/kotlin/org/move/lang/core/psi/ext/MvModule.kt +++ b/src/main/kotlin/org/move/lang/core/psi/ext/MvModule.kt @@ -1,8 +1,6 @@ package org.move.lang.core.psi.ext -import com.intellij.ide.projectView.PresentationData import com.intellij.lang.ASTNode -import com.intellij.navigation.ItemPresentation import com.intellij.openapi.project.Project import com.intellij.openapi.util.Key import com.intellij.psi.stubs.IStubElementType @@ -51,16 +49,8 @@ fun MvModule.allFunctions(): List { return stub?.childrenStubsOfType()?.map { it.psi } ?: functionList } -fun MvModule.allNonTestFunctions(): List = -// allFunctions().filter { f -> !f.isTest } - this.allFunctions().filter { f -> !f.hasTestAttr } -// getProjectPsiDependentCache(this) { -// } - -fun MvModule.testFunctions(): List = - getProjectPsiDependentCache(this) { - it.allFunctions().filter { f -> f.hasTestAttr } - } +fun MvModule.allNonTestFunctions(): List = this.allFunctions().filter { f -> !f.hasTestAttr } +fun MvModule.testFunctions(): List = this.allFunctions().filter { f -> f.hasTestAttr } val MvModule.isBuiltins: Boolean get() = this.name == "builtins" && (this.address(null)?.is0x0 ?: false) val MvModule.isSpecBuiltins: Boolean diff --git a/src/main/kotlin/org/move/lang/core/resolve/ref/MvItemSpecRefReferenceImpl.kt b/src/main/kotlin/org/move/lang/core/resolve/ref/MvItemSpecRefReferenceImpl.kt index a2bccbb18..9507f8741 100644 --- a/src/main/kotlin/org/move/lang/core/resolve/ref/MvItemSpecRefReferenceImpl.kt +++ b/src/main/kotlin/org/move/lang/core/resolve/ref/MvItemSpecRefReferenceImpl.kt @@ -2,17 +2,37 @@ package org.move.lang.core.resolve.ref import org.move.lang.core.psi.MvItemSpecRef import org.move.lang.core.psi.MvNamedElement +import org.move.lang.core.psi.ext.allNonTestFunctions import org.move.lang.core.psi.ext.itemSpec import org.move.lang.core.psi.ext.module -import org.move.lang.core.psi.ext.mslSpecifiableItems +import org.move.lang.core.psi.ext.structs +import org.move.lang.core.resolve.RsResolveProcessor +import org.move.lang.core.resolve.SimpleScopeEntry +import org.move.lang.core.resolve.collectResolveVariants +import org.move.lang.core.resolve.processAll class MvItemSpecRefReferenceImpl(element: MvItemSpecRef): MvPolyVariantReferenceCached(element) { - override fun multiResolveInner(): List { - val module = element.itemSpec.module ?: return emptyList() - val referenceName = element.referenceName - return module.mslSpecifiableItems - .filter { it.name == referenceName } - } + override fun multiResolveInner(): List = + collectResolveVariants(element.referenceName) { + processItemSpecRefResolveVariants(element, it) + } +} +fun processItemSpecRefResolveVariants( + itemSpecRef: MvItemSpecRef, + processor: RsResolveProcessor +): Boolean { + val module = itemSpecRef.itemSpec.module ?: return false + val mslEnabledItems = + listOf( + module.allNonTestFunctions(), + module.structs(), + module.enumList, + ).flatten() + return processor.processAll( + mslEnabledItems.mapNotNull { + it.name?.let { name -> SimpleScopeEntry(name, it, ALL_NAMESPACES) } + } + ) }