Skip to content

Commit af7f17b

Browse files
authored
remove vector[] and assert! completions from invalid places, improve tuple struct lookup (#246)
1 parent 4686540 commit af7f17b

File tree

8 files changed

+99
-44
lines changed

8 files changed

+99
-44
lines changed

src/main/kotlin/org/move/lang/core/MvPsiPattern.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ object MvPsiPattern {
7474
typeParameter()
7575
.afterLeafSkipping(
7676
whitespaceAndErrors(),
77-
PlatformPatterns.psiElement(COLON),
77+
psiElement(COLON),
7878
)
7979

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

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

84-
fun refExpr(): PsiElementPattern.Capture<PsiElement> =
84+
fun pathExpr(): PsiElementPattern.Capture<PsiElement> =
8585
path()
8686
.withSuperParent(2, MvPathExpr::class.java)
8787

@@ -109,8 +109,9 @@ object MvPsiPattern {
109109

110110
inline fun <reified I: PsiElement> psiElementWithParent() =
111111
psiElement()
112-
.withParent(or(psiElement<I>(), psiElement<PsiErrorElement>().withParent(psiElement<I>()))
113-
)
112+
.withParent(
113+
or(psiElement<I>(), psiElement<PsiErrorElement>().withParent(psiElement<I>()))
114+
)
114115

115116
inline fun <reified I: PsiElement> psiElementAfterSiblingSkipping(
116117
skip: ElementPattern<*>,
@@ -132,7 +133,7 @@ object MvPsiPattern {
132133
val simplePathPattern: PsiElementPattern.Capture<PsiElement>
133134
get() {
134135
val simplePath = psiElement<MvPath>()
135-
.with(object : PatternCondition<MvPath>("SimplePath") {
136+
.with(object: PatternCondition<MvPath>("SimplePath") {
136137
override fun accepts(path: MvPath, context: ProcessingContext?): Boolean =
137138
path.pathAddress == null &&
138139
path.path == null &&
@@ -224,7 +225,7 @@ private val PsiElement.prevVisibleOrNewLine: PsiElement?
224225
}
225226

226227
inline fun <reified I: PsiElement> psiElement(): PsiElementPattern.Capture<I> {
227-
return PlatformPatterns.psiElement(I::class.java)
228+
return psiElement(I::class.java)
228229
}
229230

230231
inline fun <reified I: PsiElement> PsiElementPattern.Capture<PsiElement>.withParent(): PsiElementPattern.Capture<PsiElement> {

src/main/kotlin/org/move/lang/core/completion/LookupElements2.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,11 @@ fun MvNamedElement.getLookupElementBuilder2(
5757
.withTypeText(this.containingFile?.name)
5858

5959
is MvStruct -> {
60-
val tailText = if (completionCtx.structAsType) "" else " { ... }"
61-
base
62-
.withTailText(tailText)
60+
val tailText = this.tupleFields?.let {
61+
it.tupleFieldDeclList
62+
.joinToString(", ", "(", ")") { it.type.text }
63+
}
64+
base.withTailText(tailText)
6365
.withTypeText(this.containingFile?.name)
6466
}
6567

src/main/kotlin/org/move/lang/core/completion/providers/AssertMacroCompletionProvider.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder
77
import com.intellij.openapi.editor.EditorModificationUtil
88
import com.intellij.patterns.ElementPattern
99
import com.intellij.patterns.PlatformPatterns
10+
import com.intellij.patterns.PlatformPatterns.psiElement
1011
import com.intellij.psi.PsiElement
1112
import com.intellij.util.ProcessingContext
12-
import org.move.lang.MvElementTypes
13+
import org.move.lang.MvElementTypes.COLON_COLON
1314
import org.move.lang.core.MvPsiPattern
1415
import org.move.lang.core.completion.MACRO_PRIORITY
1516
import org.move.lang.core.psi.MvPath
1617

17-
object AssertMacroCompletionProvider : MvCompletionProvider() {
18+
object AssertMacroCompletionProvider: MvCompletionProvider() {
1819
override val elementPattern: ElementPattern<out PsiElement>
19-
get() = MvPsiPattern.path()
20-
.andNot(MvPsiPattern.pathType())
21-
.andNot(MvPsiPattern.schemaLit())
20+
get() = MvPsiPattern.pathExpr()
2221
.andNot(
23-
PlatformPatterns.psiElement()
24-
.afterLeaf(PlatformPatterns.psiElement(MvElementTypes.COLON_COLON))
22+
psiElement().afterLeaf(psiElement(COLON_COLON))
2523
)
2624

2725

src/main/kotlin/org/move/lang/core/completion/providers/VectorLiteralCompletionProvider.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ import com.intellij.codeInsight.lookup.LookupElementBuilder
77
import com.intellij.openapi.editor.EditorModificationUtil
88
import com.intellij.patterns.ElementPattern
99
import com.intellij.patterns.PlatformPatterns
10+
import com.intellij.patterns.PlatformPatterns.psiElement
1011
import com.intellij.psi.PsiElement
1112
import com.intellij.util.ProcessingContext
1213
import org.move.lang.MvElementTypes
14+
import org.move.lang.MvElementTypes.COLON_COLON
1315
import org.move.lang.core.MvPsiPattern
1416
import org.move.lang.core.completion.VECTOR_LITERAL_PRIORITY
17+
import org.move.lang.core.completion.withPriority
1518
import org.move.lang.core.psi.MvPath
1619

1720
object VectorLiteralCompletionProvider : MvCompletionProvider() {
21+
1822
override val elementPattern: ElementPattern<out PsiElement>
19-
get() = MvPsiPattern.path()
20-
.andNot(MvPsiPattern.pathType())
21-
.andNot(MvPsiPattern.schemaLit())
23+
get() = MvPsiPattern.pathExpr()
2224
.andNot(
23-
PlatformPatterns.psiElement()
24-
.afterLeaf(PlatformPatterns.psiElement(MvElementTypes.COLON_COLON))
25+
psiElement().afterLeaf(psiElement(COLON_COLON))
2526
)
2627

2728

@@ -41,7 +42,8 @@ object VectorLiteralCompletionProvider : MvCompletionProvider() {
4142
.withInsertHandler { ctx, _ ->
4243
EditorModificationUtil.moveCaretRelatively(ctx.editor, -1)
4344
}
44-
result.addElement(PrioritizedLookupElement.withPriority(lookupElement, VECTOR_LITERAL_PRIORITY))
45+
.withPriority(VECTOR_LITERAL_PRIORITY)
46+
result.addElement(lookupElement)
4547
}
4648

4749
}

src/main/kotlin/org/move/utils/SignatureUtils.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,5 @@ object SignatureUtils {
1616
}
1717
append(")")
1818
}
19-
}
19+
}
20+

src/test/kotlin/org/move/lang/completion/BuiltInsCompletionTest.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,57 @@ class BuiltInsCompletionTest : CompletionTestCase() {
128128
}
129129
""")
130130

131+
fun `test no vector completion in use`() = checkNoCompletion("""
132+
module 0x1::m {
133+
use vec/*caret*/
134+
}
135+
""")
136+
137+
fun `test no vector completion in fq path`() = checkNoCompletion("""
138+
module 0x1::m {
139+
fun main() {
140+
aptos::vec/*caret*/
141+
}
142+
}
143+
""")
144+
145+
fun `test no vector lit in path qualifier`() = checkNoCompletion("""
146+
module 0x1::m {
147+
fun main() {
148+
vec/*caret*/::call();
149+
}
150+
}
151+
""")
152+
153+
fun `test no assert! in use`() = checkNoCompletion("""
154+
module 0x1::m {
155+
use ass/*caret*/
156+
}
157+
""")
158+
159+
fun `test no assert! in fq path`() = checkNoCompletion("""
160+
module 0x1::m {
161+
fun main() {
162+
aptos_framework::aptos::ass/*caret*/
163+
}
164+
}
165+
""")
166+
167+
fun `test no assert! in path qualifier`() = checkNoCompletion("""
168+
module 0x1::m {
169+
fun main() {
170+
ass/*caret*/::call();
171+
}
172+
}
173+
""")
174+
175+
fun `test no assert! in type`() = checkNoCompletion("""
176+
module 0x1::m {
177+
fun main(s: ass/*caret*/) {
178+
}
179+
}
180+
""")
181+
131182
private fun checkContainsBuiltins(@Language("Move") text: String) {
132183
val functionNames = BUILTIN_FUNCTIONS
133184
for (name in functionNames) {

src/test/kotlin/org/move/lang/completion/lookups/LookupElementTest.kt

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class LookupElementTest: MvTestBase() {
6666
struct MyStruct { val: u8 }
6767
//^
6868
}
69-
""", tailText = " { ... }", typeText = "main.move"
69+
""", typeText = "main.move"
7070
)
7171

7272
fun `test module`() = checkNamedItem(
@@ -169,26 +169,13 @@ class LookupElementTest: MvTestBase() {
169169
}
170170
""", typeText = "u8"
171171
)
172-
//
173-
// fun `test import module lookup`() = checkNamedItem("""
174-
// module 0x1::m {
175-
// public fun identity(a: u8): u8 { a }
176-
// }
177-
// module 0x1::main {
178-
// use 0x1::m;
179-
// //^
180-
// }
181-
// """, tailText = " 0x1", typeText = "main.move")
182172

183-
// fun `test import function lookup`() = checkNamedItem("""
184-
// module 0x1::m {
185-
// public fun identity(a: u8): u8 { a }
186-
// }
187-
// module 0x1::main {
188-
// use 0x1::m::identity;
189-
// //^
190-
// }
191-
// """, tailText = "(a: u8): u8", typeText = "main.move")
173+
fun `test lookup for tuple struct`() = checkNamedItem("""
174+
module 0x1::m {
175+
struct SS(u8, u16);
176+
//^
177+
}
178+
""", tailText = "(u8, u16)", typeText = "main.move")
192179

193180
private fun checkNamedItem(
194181
@Language("Move") code: String,

src/test/kotlin/org/move/utils/tests/completion/MoveCompletionTestFixture.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.move.utils.tests.completion
22

33
import com.intellij.codeInsight.lookup.LookupElement
4+
import com.intellij.psi.impl.source.tree.LeafPsiElement
5+
import com.intellij.psi.util.elementType
6+
import com.intellij.psi.util.prevLeaf
47
import com.intellij.testFramework.fixtures.CodeInsightTestFixture
58
import com.intellij.testFramework.fixtures.impl.BaseFixture
69
import org.intellij.lang.annotations.Language
10+
import org.move.lang.MvElementTypes
711
import org.move.utils.tests.InlineFile
812
import org.move.utils.tests.hasCaretMarker
913
import org.move.utils.tests.replaceCaretMarker
@@ -55,7 +59,16 @@ class MvCompletionTestFixture(
5559
val lookups = myFixture.completeBasic()
5660
checkNotNull(lookups) {
5761
val element = myFixture.file.findElementAt(myFixture.caretOffset - 1)
58-
"Expected zero completions, but one completion was auto inserted: `${element?.text}`."
62+
// handle assert!()
63+
var elementText = element?.text
64+
if (element != null) {
65+
if (element.prevSibling.elementType == MvElementTypes.EXCL) {
66+
// IDENTIFIER + ! + (
67+
elementText =
68+
element.prevSibling.prevSibling.text + element.prevSibling.text + elementText
69+
}
70+
}
71+
"Expected zero completions, but one completion was auto inserted: `$elementText`."
5972
}
6073
check(lookups.isEmpty()) {
6174
"Expected zero completions, got ${lookups.map { it.lookupString }}."

0 commit comments

Comments
 (0)