Skip to content

Commit

Permalink
named tuple field completion
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 11, 2024
1 parent 32dd71e commit 554aa4f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.move.lang.core.resolve.createProcessor
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.processFieldLookupResolveVariants
import org.move.lang.core.resolve2.processLabelResolveVariants
import org.move.lang.core.resolve2.processMethodResolveVariants
import org.move.lang.core.resolve2.processPatBindingResolveVariants
Expand Down Expand Up @@ -87,7 +88,7 @@ object CommonCompletionProvider: MvCompletionProvider() {
val tyAdt = receiverTy.derefIfNeeded() as? TyAdt
if (tyAdt != null) {
collectCompletionVariants(result, ctx, subst = tyAdt.substitution) {
processNamedFieldVariants(element, tyAdt, msl, it)
processFieldLookupResolveVariants(element, tyAdt, msl, it)
}
}

Expand Down
35 changes: 0 additions & 35 deletions src/main/kotlin/org/move/lang/core/psi/ext/MvFieldLookup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,11 @@ package org.move.lang.core.psi.ext
import com.intellij.lang.ASTNode
import com.intellij.psi.PsiElement
import org.move.lang.core.psi.*
import org.move.lang.core.resolve.*
import org.move.lang.core.resolve.ref.MvPolyVariantReference
import org.move.lang.core.resolve.ref.MvPolyVariantReferenceBase
import org.move.lang.core.resolve.ref.NONE
import org.move.lang.core.resolve2.ref.FieldResolveVariant
import org.move.lang.core.types.infer.inference
import org.move.lang.core.types.ty.TyAdt
import org.move.stdext.wrapWithList

fun processNamedFieldVariants(
element: MvMethodOrField,
receiverTy: TyAdt,
msl: Boolean,
processor: RsResolveProcessor
): Boolean {
val receiverItem = receiverTy.item
if (!isFieldsAccessible(element, receiverItem, msl)) return false

return when (receiverItem) {
is MvStruct -> processor.processAll(NONE, receiverItem.namedFields)
is MvEnum -> {
val visitedFields = mutableSetOf<String>()
for (variant in receiverItem.variants) {
val visitedVariantFields = mutableSetOf<String>()
for (namedField in variant.namedFields) {
val fieldName = namedField.name
if (fieldName in visitedFields) continue
if (processor.process(NONE, namedField)) return true
// collect all names for the variant
visitedVariantFields.add(fieldName)
}
// add variant fields to the global fields list to skip them in the next variants
visitedFields.addAll(visitedVariantFields)
}
false
}
else -> error("unreachable")
}
}

// todo: change into VisibilityFilter
fun isFieldsAccessible(
element: MvElement,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@ import org.move.lang.core.types.ty.TyAdt
import org.move.lang.index.MvModuleIndex

fun processFieldLookupResolveVariants(
fieldLookup: MvFieldLookup,
fieldLookup: MvMethodOrField,
receiverTy: TyAdt,
msl: Boolean,
originalProcessor: RsResolveProcessorBase<FieldResolveVariant>
): Boolean {
val receiverItem = receiverTy.item
if (!isFieldsAccessible(fieldLookup, receiverItem, msl)) return false

val processor = originalProcessor.wrapWithMapper { it: ScopeEntry ->
FieldResolveVariant(it.name, it.element)
}
val processor = originalProcessor
.wrapWithMapper { it: ScopeEntry -> FieldResolveVariant(it.name, it.element) }

return when (receiverItem) {
is MvStruct -> processFieldDeclarations(receiverItem, processor)
// is MvStruct -> processor.processAll(NONE, receiverItem.fields)
is MvEnum -> {
val visitedFields = mutableSetOf<String>()
for (variant in receiverItem.variants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.intellij.psi.NavigatablePsiElement
import org.intellij.lang.annotations.Language
import org.move.lang.core.completion.MvCompletionContext
import org.move.lang.core.completion.createLookupElement
import org.move.lang.core.completion.providers.MethodOrFieldCompletionProvider
import org.move.lang.core.completion.providers.CommonCompletionProvider
import org.move.lang.core.psi.MvElement
import org.move.lang.core.psi.MvNamedElement
import org.move.lang.core.psi.ext.MvMethodOrField
Expand Down Expand Up @@ -237,7 +237,7 @@ class LookupElementTest: MvTestBase() {
}

if (element is MvMethodOrField) {
MethodOrFieldCompletionProvider.addMethodOrFieldVariants(element, result)
CommonCompletionProvider.addMethodOrFieldVariants(element, result)
}

val lookup = lookups.single {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,16 @@ module 0x1::M {
}
"""
)

fun `test named tuple field completion`() = checkContainsCompletionExact(
listOf("0", "1"),
"""
module 0x1::m {
struct S(u8, u8);
fun main(s: S) {
s./*caret*/
}
}
"""
)
}

0 comments on commit 554aa4f

Please sign in to comment.