Skip to content

Commit a1c77ef

Browse files
authored
Merge pull request #247 from pontem-network/fix-live-templates-contexts
Improve locality in live templates contexts
2 parents af7f17b + 3f3935d commit a1c77ef

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/main/kotlin/org/move/ide/liveTemplates/MvContextType.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ import com.intellij.psi.util.PsiTreeUtil
1010
import com.intellij.psi.util.PsiUtilCore
1111
import org.move.ide.MvHighlighter
1212
import org.move.lang.MoveLanguage
13+
import org.move.lang.core.psi.MvBlockFields
1314
import org.move.lang.core.psi.MvCodeBlock
15+
import org.move.lang.core.psi.MvEnumBody
1416
import org.move.lang.core.psi.MvModule
17+
import org.move.lang.core.psi.MvType
1518

1619
sealed class MvContextType(presentableName: String): TemplateContextType(presentableName) {
1720

@@ -40,13 +43,22 @@ sealed class MvContextType(presentableName: String): TemplateContextType(present
4043
override fun isInContext(element: PsiElement): Boolean = owner(element) is MvModule
4144
}
4245

43-
class Block: MvContextType("Block") {
46+
class Block: MvContextType("Code block") {
4447
override fun isInContext(element: PsiElement): Boolean = owner(element) is MvCodeBlock
4548
}
4649

50+
class Type: MvContextType("Type") {
51+
override fun isInContext(element: PsiElement): Boolean = owner(element) is MvType
52+
}
53+
4754
companion object {
4855
private fun owner(element: PsiElement): PsiElement? = PsiTreeUtil.findFirstParent(element) {
49-
it is MvCodeBlock || it is MvModule || it is PsiFile
56+
it is MvCodeBlock
57+
|| it is MvModule
58+
|| it is PsiFile
59+
|| it is MvType
60+
// filter out enum/struct body
61+
|| it is MvEnumBody || it is MvBlockFields
5062
}
5163
}
5264
}

src/main/resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@
192192
implementation="org.move.ide.liveTemplates.MvContextType$Block"
193193
contextId="MOVE_BLOCK"
194194
baseContextId="MOVE_FILE" />
195+
<liveTemplateContext
196+
implementation="org.move.ide.liveTemplates.MvContextType$Type"
197+
contextId="MOVE_TYPE"
198+
baseContextId="MOVE_FILE" />
195199

196200
<renamePsiElementProcessor implementation="org.move.ide.refactoring.MvRenameProcessor"
197201
order="first"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.move.ide.liveTemplates
2+
3+
import com.intellij.openapi.actionSystem.IdeActions
4+
import org.intellij.lang.annotations.Language
5+
import org.move.utils.tests.MvTestBase
6+
7+
class MvLiveTemplatesTest: MvTestBase() {
8+
9+
fun `test no test function in type`() = noSnippet("""
10+
module 0x1::m {
11+
struct S { val: t/*caret*/}
12+
}
13+
""")
14+
15+
fun `test no test function in struct fields block`() = noSnippet("""
16+
module 0x1::m {
17+
struct S { t/*caret*/ }
18+
}
19+
""")
20+
21+
fun `test no test function in enum variants block`() = noSnippet("""
22+
module 0x1::m {
23+
enum S { t/*caret*/ }
24+
}
25+
""")
26+
27+
fun `test no test function in tuple struct type`() = noSnippet("""
28+
module 0x1::m {
29+
struct S(u8, t/*caret*/)
30+
}
31+
""")
32+
33+
fun `test no test function in expr`() = noSnippet("""
34+
module 0x1::m {
35+
fun main() {
36+
t/*caret*/
37+
}
38+
}
39+
""")
40+
41+
fun `test no test function outside module`() = noSnippet("""
42+
t/*caret*/
43+
module 0x1::m {
44+
}
45+
""")
46+
47+
fun `test test function in module body`() = expandSnippet("""
48+
module 0x1::m {
49+
t/*caret*/
50+
}
51+
""", """
52+
module 0x1::m {
53+
#[test]
54+
fun /*caret*/() {
55+
56+
}
57+
}
58+
""")
59+
60+
private fun expandSnippet(@Language("Move") before: String, @Language("Move") after: String) =
61+
checkEditorAction(before, after, IdeActions.ACTION_EXPAND_LIVE_TEMPLATE_BY_TAB)
62+
63+
private fun noSnippet(@Language("Move") code: String) = expandSnippet(code, code)
64+
}

0 commit comments

Comments
 (0)