Skip to content

Commit

Permalink
remove vector[] and assert! completions from invalid places, improve …
Browse files Browse the repository at this point in the history
…tuple struct lookup
  • Loading branch information
mkurnikov committed Nov 13, 2024
1 parent 78b0fe1 commit be1b55c
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 44 deletions.
13 changes: 7 additions & 6 deletions src/main/kotlin/org/move/lang/core/MvPsiPattern.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ object MvPsiPattern {
typeParameter()
.afterLeafSkipping(
whitespaceAndErrors(),
PlatformPatterns.psiElement(COLON),
psiElement(COLON),
)

fun ability(): PsiElementPattern.Capture<PsiElement> = psiElementWithParent<MvAbility>()

fun path(): PsiElementPattern.Capture<PsiElement> = psiElementWithParent<MvPath>()

fun refExpr(): PsiElementPattern.Capture<PsiElement> =
fun pathExpr(): PsiElementPattern.Capture<PsiElement> =
path()
.withSuperParent(2, MvPathExpr::class.java)

Expand Down Expand Up @@ -109,8 +109,9 @@ object MvPsiPattern {

inline fun <reified I: PsiElement> psiElementWithParent() =
psiElement()
.withParent(or(psiElement<I>(), psiElement<PsiErrorElement>().withParent(psiElement<I>()))
)
.withParent(
or(psiElement<I>(), psiElement<PsiErrorElement>().withParent(psiElement<I>()))
)

inline fun <reified I: PsiElement> psiElementAfterSiblingSkipping(
skip: ElementPattern<*>,
Expand All @@ -132,7 +133,7 @@ object MvPsiPattern {
val simplePathPattern: PsiElementPattern.Capture<PsiElement>
get() {
val simplePath = psiElement<MvPath>()
.with(object : PatternCondition<MvPath>("SimplePath") {
.with(object: PatternCondition<MvPath>("SimplePath") {
override fun accepts(path: MvPath, context: ProcessingContext?): Boolean =
path.pathAddress == null &&
path.path == null &&
Expand Down Expand Up @@ -224,7 +225,7 @@ private val PsiElement.prevVisibleOrNewLine: PsiElement?
}

inline fun <reified I: PsiElement> psiElement(): PsiElementPattern.Capture<I> {
return PlatformPatterns.psiElement(I::class.java)
return psiElement(I::class.java)
}

inline fun <reified I: PsiElement> PsiElementPattern.Capture<PsiElement>.withParent(): PsiElementPattern.Capture<PsiElement> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ fun MvNamedElement.getLookupElementBuilder2(
.withTypeText(this.containingFile?.name)

is MvStruct -> {
val tailText = if (completionCtx.structAsType) "" else " { ... }"
base
.withTailText(tailText)
val tailText = this.tupleFields?.let {
it.tupleFieldDeclList
.joinToString(", ", "(", ")") { it.type.text }
}
base.withTailText(tailText)
.withTypeText(this.containingFile?.name)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,19 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.editor.EditorModificationUtil
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.PlatformPatterns
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.PsiElement
import com.intellij.util.ProcessingContext
import org.move.lang.MvElementTypes
import org.move.lang.MvElementTypes.COLON_COLON
import org.move.lang.core.MvPsiPattern
import org.move.lang.core.completion.MACRO_PRIORITY
import org.move.lang.core.psi.MvPath

object AssertMacroCompletionProvider : MvCompletionProvider() {
object AssertMacroCompletionProvider: MvCompletionProvider() {
override val elementPattern: ElementPattern<out PsiElement>
get() = MvPsiPattern.path()
.andNot(MvPsiPattern.pathType())
.andNot(MvPsiPattern.schemaLit())
get() = MvPsiPattern.pathExpr()
.andNot(
PlatformPatterns.psiElement()
.afterLeaf(PlatformPatterns.psiElement(MvElementTypes.COLON_COLON))
psiElement().afterLeaf(psiElement(COLON_COLON))
)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,22 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.openapi.editor.EditorModificationUtil
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.PlatformPatterns
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.psi.PsiElement
import com.intellij.util.ProcessingContext
import org.move.lang.MvElementTypes
import org.move.lang.MvElementTypes.COLON_COLON
import org.move.lang.core.MvPsiPattern
import org.move.lang.core.completion.VECTOR_LITERAL_PRIORITY
import org.move.lang.core.completion.withPriority
import org.move.lang.core.psi.MvPath

object VectorLiteralCompletionProvider : MvCompletionProvider() {

override val elementPattern: ElementPattern<out PsiElement>
get() = MvPsiPattern.path()
.andNot(MvPsiPattern.pathType())
.andNot(MvPsiPattern.schemaLit())
get() = MvPsiPattern.pathExpr()
.andNot(
PlatformPatterns.psiElement()
.afterLeaf(PlatformPatterns.psiElement(MvElementTypes.COLON_COLON))
psiElement().afterLeaf(psiElement(COLON_COLON))
)


Expand All @@ -41,7 +42,8 @@ object VectorLiteralCompletionProvider : MvCompletionProvider() {
.withInsertHandler { ctx, _ ->
EditorModificationUtil.moveCaretRelatively(ctx.editor, -1)
}
result.addElement(PrioritizedLookupElement.withPriority(lookupElement, VECTOR_LITERAL_PRIORITY))
.withPriority(VECTOR_LITERAL_PRIORITY)
result.addElement(lookupElement)
}

}
3 changes: 2 additions & 1 deletion src/main/kotlin/org/move/utils/SignatureUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ object SignatureUtils {
}
append(")")
}
}
}

51 changes: 51 additions & 0 deletions src/test/kotlin/org/move/lang/completion/BuiltInsCompletionTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,57 @@ class BuiltInsCompletionTest : CompletionTestCase() {
}
""")

fun `test no vector completion in use`() = checkNoCompletion("""
module 0x1::m {
use vec/*caret*/
}
""")

fun `test no vector completion in fq path`() = checkNoCompletion("""
module 0x1::m {
fun main() {
aptos::vec/*caret*/
}
}
""")

fun `test no vector lit in path qualifier`() = checkNoCompletion("""
module 0x1::m {
fun main() {
vec/*caret*/::call();
}
}
""")

fun `test no assert! in use`() = checkNoCompletion("""
module 0x1::m {
use ass/*caret*/
}
""")

fun `test no assert! in fq path`() = checkNoCompletion("""
module 0x1::m {
fun main() {
aptos_framework::aptos::ass/*caret*/
}
}
""")

fun `test no assert! in path qualifier`() = checkNoCompletion("""
module 0x1::m {
fun main() {
ass/*caret*/::call();
}
}
""")

fun `test no assert! in type`() = checkNoCompletion("""
module 0x1::m {
fun main(s: ass/*caret*/) {
}
}
""")

private fun checkContainsBuiltins(@Language("Move") text: String) {
val functionNames = BUILTIN_FUNCTIONS
for (name in functionNames) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class LookupElementTest: MvTestBase() {
struct MyStruct { val: u8 }
//^
}
""", tailText = " { ... }", typeText = "main.move"
""", typeText = "main.move"
)

fun `test module`() = checkNamedItem(
Expand Down Expand Up @@ -169,26 +169,13 @@ class LookupElementTest: MvTestBase() {
}
""", typeText = "u8"
)
//
// fun `test import module lookup`() = checkNamedItem("""
// module 0x1::m {
// public fun identity(a: u8): u8 { a }
// }
// module 0x1::main {
// use 0x1::m;
// //^
// }
// """, tailText = " 0x1", typeText = "main.move")

// fun `test import function lookup`() = checkNamedItem("""
// module 0x1::m {
// public fun identity(a: u8): u8 { a }
// }
// module 0x1::main {
// use 0x1::m::identity;
// //^
// }
// """, tailText = "(a: u8): u8", typeText = "main.move")
fun `test lookup for tuple struct`() = checkNamedItem("""
module 0x1::m {
struct SS(u8, u16);
//^
}
""", tailText = "(u8, u16)", typeText = "main.move")

private fun checkNamedItem(
@Language("Move") code: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package org.move.utils.tests.completion

import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.psi.impl.source.tree.LeafPsiElement
import com.intellij.psi.util.elementType
import com.intellij.psi.util.prevLeaf
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
import com.intellij.testFramework.fixtures.impl.BaseFixture
import org.intellij.lang.annotations.Language
import org.move.lang.MvElementTypes
import org.move.utils.tests.InlineFile
import org.move.utils.tests.hasCaretMarker
import org.move.utils.tests.replaceCaretMarker
Expand Down Expand Up @@ -55,7 +59,16 @@ class MvCompletionTestFixture(
val lookups = myFixture.completeBasic()
checkNotNull(lookups) {
val element = myFixture.file.findElementAt(myFixture.caretOffset - 1)
"Expected zero completions, but one completion was auto inserted: `${element?.text}`."
// handle assert!()
var elementText = element?.text
if (element != null) {
if (element.prevSibling.elementType == MvElementTypes.EXCL) {
// IDENTIFIER + ! + (
elementText =
element.prevSibling.prevSibling.text + element.prevSibling.text + elementText
}
}
"Expected zero completions, but one completion was auto inserted: `$elementText`."
}
check(lookups.isEmpty()) {
"Expected zero completions, got ${lookups.map { it.lookupString }}."
Expand Down

0 comments on commit be1b55c

Please sign in to comment.