Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor completion providers #231

Merged
merged 2 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package org.move.lang.core.completion
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.completion.impl.CompletionSorterImpl
import com.intellij.psi.util.elementType
import org.move.lang.MvElementTypes.IDENTIFIER
import org.move.lang.MvElementTypes.MODULE_KW
import org.move.lang.core.MvPsiPattern
import org.move.lang.core.completion.providers.*
import org.move.lang.core.completion.sort.COMPLETION_WEIGHERS_GROUPED
import org.move.lang.core.psi.MvModule
Expand All @@ -14,7 +12,6 @@ import org.move.lang.core.psi.ext.prevNonWsSibling
class CommonCompletionContributor: CompletionContributor() {
init {
extend(CompletionType.BASIC, PrimitiveTypesCompletionProvider)
extend(CompletionType.BASIC, SpecItemCompletionProvider)

// addresses
extend(CompletionType.BASIC, NamedAddressInUseStmtCompletionProvider)
Expand All @@ -26,11 +23,9 @@ class CommonCompletionContributor: CompletionContributor() {
extend(CompletionType.BASIC, SchemaFieldsCompletionProvider)
extend(CompletionType.BASIC, MvPathCompletionProvider2)

extend(CompletionType.BASIC, MvPsiPattern.ability(), AbilitiesCompletionProvider)
// extend(CompletionType.BASIC, MvPsiPattern.refExpr(), BoolsCompletionProvider)

extend(CompletionType.BASIC, AbilitiesCompletionProvider)
extend(CompletionType.BASIC, BoolsCompletionProvider)
extend(CompletionType.BASIC, MacrosCompletionProvider)
extend(CompletionType.BASIC, AssertMacroCompletionProvider)
extend(CompletionType.BASIC, VectorLiteralCompletionProvider)
extend(CompletionType.BASIC, MethodOrFieldCompletionProvider)

Expand Down
12 changes: 10 additions & 2 deletions src/main/kotlin/org/move/lang/core/completion/LookupElements.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -114,6 +112,16 @@ open class DefaultInsertHandler(val completionCtx: MvCompletionContext? = null):
item: LookupElement
) {
val document = context.document

val itemSpecRef = context.getElementOfType<MvItemSpecRef>()
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import com.intellij.codeInsight.completion.CompletionProvider
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.PrioritizedLookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.patterns.ElementPattern
import com.intellij.psi.PsiElement
import com.intellij.util.ProcessingContext
import org.move.lang.core.MvPsiPattern
import org.move.lang.core.completion.KEYWORD_PRIORITY

object AbilitiesCompletionProvider : CompletionProvider<CompletionParameters>() {
object AbilitiesCompletionProvider : MvCompletionProvider() {
override val elementPattern: ElementPattern<out PsiElement> get() = MvPsiPattern.ability()

override fun addCompletions(
parameters: CompletionParameters,
context: ProcessingContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import org.move.lang.core.MvPsiPattern
import org.move.lang.core.completion.MACRO_PRIORITY
import org.move.lang.core.psi.MvPath

object MacrosCompletionProvider : MvCompletionProvider() {
object AssertMacroCompletionProvider : MvCompletionProvider() {
override val elementPattern: ElementPattern<out PsiElement>
get() = MvPsiPattern.path()
.andNot(MvPsiPattern.pathType())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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)
}
}
}
Expand Down

This file was deleted.

4 changes: 0 additions & 4 deletions src/main/kotlin/org/move/lang/core/psi/ext/MvItemSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ val MvItemSpec.funcItem get() = this.item as? MvFunction

val MvModuleItemSpec.itemSpecBlock: MvSpecCodeBlock? get() = this.childOfType()

val MvModule.mslSpecifiableItems: List<MvNameIdentifierOwner>
get() =
this.allFunctions().chain(this.structs()).toList()

val MvItemSpec.module: MvModule?
get() {
val parent = this.parent
Expand Down
14 changes: 2 additions & 12 deletions src/main/kotlin/org/move/lang/core/psi/ext/MvModule.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -51,16 +49,8 @@ fun MvModule.allFunctions(): List<MvFunction> {
return stub?.childrenStubsOfType<MvFunctionStub>()?.map { it.psi } ?: functionList
}

fun MvModule.allNonTestFunctions(): List<MvFunction> =
// allFunctions().filter { f -> !f.isTest }
this.allFunctions().filter { f -> !f.hasTestAttr }
// getProjectPsiDependentCache(this) {
// }

fun MvModule.testFunctions(): List<MvFunction> =
getProjectPsiDependentCache(this) {
it.allFunctions().filter { f -> f.hasTestAttr }
}
fun MvModule.allNonTestFunctions(): List<MvFunction> = this.allFunctions().filter { f -> !f.hasTestAttr }
fun MvModule.testFunctions(): List<MvFunction> = this.allFunctions().filter { f -> f.hasTestAttr }

val MvModule.isBuiltins: Boolean get() = this.name == "builtins" && (this.address(null)?.is0x0 ?: false)
val MvModule.isSpecBuiltins: Boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<MvItemSpecRef>(element) {

override fun multiResolveInner(): List<MvNamedElement> {
val module = element.itemSpec.module ?: return emptyList()
val referenceName = element.referenceName
return module.mslSpecifiableItems
.filter { it.name == referenceName }
}
override fun multiResolveInner(): List<MvNamedElement> =
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) }
}
)
}
Loading