Skip to content

Commit

Permalink
proper call for getOriginalOrSelf
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Jan 15, 2025
1 parent 2e1b1fb commit c5b7196
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ object StructFieldsCompletionProvider: MvCompletionProvider() {
// from cache anymore. In this case we need to get the old path.
// OLD: "safe" here means that if tree changes too much (=any of the ancestors of path are changed),
// then it's a no-op and we continue working with current path.
val struct = patStruct.path.getOriginalOrSelf().maybeFieldsOwner ?: return
val struct = patStruct.path.maybeFieldsOwner ?: return
addFieldsToCompletion(
struct,
patStruct.fieldNames,
Expand All @@ -57,7 +57,7 @@ object StructFieldsCompletionProvider: MvCompletionProvider() {
is MvStructLitField -> {
val structLit = element.parentStructLitExpr
// see MvPatField's comment above
val struct = structLit.path.getOriginalOrSelf().maybeFieldsOwner ?: return
val struct = structLit.path.maybeFieldsOwner ?: return
addFieldsToCompletion(
struct,
structLit.providedFieldNames,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ fun processSchemaLitFieldResolveVariants(
processor: RsResolveProcessor
): Boolean {
val schemaLit = literalField.schemaLit ?: return false
// getOriginalOrSelf() to prevent cache misses for the path cache in completion
val schema = schemaLit.path.getOriginalOrSelf().maybeSchema ?: return false
val schema = schemaLit.path.maybeSchema ?: return false
return schema.fieldsAsBindings
.any { field ->
processor.process(SimpleScopeEntry(field.name, field, setOf(Namespace.NAME)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fun processStructPatFieldResolveVariants(
processor: RsResolveProcessor
): Boolean {
// used in completion
val fieldsOwner = patFieldFull.patStruct.path.getOriginalOrSelf().maybeFieldsOwner ?: return false
val fieldsOwner = patFieldFull.patStruct.path.maybeFieldsOwner ?: return false
return processNamedFieldDeclarations(fieldsOwner, processor)
}

Expand All @@ -80,7 +80,7 @@ fun processPatBindingResolveVariants(
// field pattern shorthand
if (binding.parent is MvPatField) {
val parentPat = binding.parent.parent as MvPatStruct
val fieldsOwner = parentPat.path.getOriginalOrSelf().maybeFieldsOwner
val fieldsOwner = parentPat.path.maybeFieldsOwner
// can be null if unresolved
if (fieldsOwner != null) {
if (processNamedFieldDeclarations(fieldsOwner, originalProcessor)) return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.move.lang.core.resolve2.ref

import com.intellij.psi.ResolveResult
import org.move.cli.MoveProject
import org.move.lang.core.completion.getOriginalOrSelf
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
import org.move.lang.core.resolve.*
Expand Down Expand Up @@ -64,7 +65,10 @@ class MvPath2ReferenceImpl(element: MvPath): MvPolyVariantReferenceBase<MvPath>(
}

private fun getResolvedPathFromInference(path: MvPath, msl: Boolean): List<RsPathResolveResult<MvElement>>? {
return path.inference(msl)?.getResolvedPath(path)
// Path resolution is cached, but sometimes path changes so much that it can't be retrieved
// from cache anymore. In this case we need to get the old path.
val originalPath = path.getOriginalOrSelf()
return originalPath.inference(msl)?.getResolvedPath(originalPath)
?.map {
RsPathResolveResult(it.element, it.isVisible)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.move.lang.core.types.infer

import com.intellij.psi.PsiElement
import org.move.lang.core.completion.getOriginalOrSelf
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.elementType
import org.move.lang.core.types.ty.Ty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import com.intellij.psi.util.CachedValue
import org.jetbrains.annotations.TestOnly
import org.move.cli.settings.isDebugModeEnabled
import org.move.ide.formatter.impl.location
import org.move.lang.core.completion.getOriginalOrSelf
import org.move.lang.core.completion.safeGetOriginalOrSelf
import org.move.lang.core.psi.*
import org.move.lang.core.psi.ext.*
import org.move.lang.core.resolve.ScopeEntry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ class StructsCompletionTest: CompletionTestCase() {
}
""")

fun `test struct fields completion in struct pattern shorthand`() = doSingleCompletion("""
fun `test struct fields completion in struct pattern shorthand 1`() = doSingleCompletion("""
module 0x1::M {
struct T { my_field: u8 }
fun main() {
Expand All @@ -210,6 +210,22 @@ class StructsCompletionTest: CompletionTestCase() {
}
""")

fun `test struct fields completion in struct pattern shorthand 2`() = doSingleCompletion("""
module 0x1::modules {
struct Ss has key { def_val: u8, rut_def_val: u16 }
fun main(s: Ss) {
let Ss { def/*caret*/, rut_def_val } = s;
}
}
""", """
module 0x1::modules {
struct Ss has key { def_val: u8, rut_def_val: u16 }
fun main(s: Ss) {
let Ss { def_val/*caret*/, rut_def_val } = s;
}
}
""")

fun `test no completion in struct pattern if field already specified`() = checkNoCompletion("""
module 0x1::M {
struct T { offered: u8, collateral: u8 }
Expand Down

0 comments on commit c5b7196

Please sign in to comment.