Skip to content

Commit

Permalink
make live template contexts more specific
Browse files Browse the repository at this point in the history
  • Loading branch information
mkurnikov committed Nov 13, 2024
1 parent af7f17b commit f027c5f
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/kotlin/org/move/ide/liveTemplates/MvContextType.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ import com.intellij.psi.util.PsiTreeUtil
import com.intellij.psi.util.PsiUtilCore
import org.move.ide.MvHighlighter
import org.move.lang.MoveLanguage
import org.move.lang.core.psi.MvBlockFields
import org.move.lang.core.psi.MvCodeBlock
import org.move.lang.core.psi.MvEnumBody
import org.move.lang.core.psi.MvModule
import org.move.lang.core.psi.MvType

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

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

class Block: MvContextType("Block") {
class Block: MvContextType("Code block") {
override fun isInContext(element: PsiElement): Boolean = owner(element) is MvCodeBlock
}

class Type: MvContextType("Type") {
override fun isInContext(element: PsiElement): Boolean = owner(element) is MvType
}

companion object {
private fun owner(element: PsiElement): PsiElement? = PsiTreeUtil.findFirstParent(element) {
it is MvCodeBlock || it is MvModule || it is PsiFile
it is MvCodeBlock
|| it is MvModule
|| it is PsiFile
|| it is MvType
// filter out enum/struct body
|| it is MvEnumBody || it is MvBlockFields
}
}
}
4 changes: 4 additions & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@
implementation="org.move.ide.liveTemplates.MvContextType$Block"
contextId="MOVE_BLOCK"
baseContextId="MOVE_FILE" />
<liveTemplateContext
implementation="org.move.ide.liveTemplates.MvContextType$Type"
contextId="MOVE_TYPE"
baseContextId="MOVE_FILE" />

<renamePsiElementProcessor implementation="org.move.ide.refactoring.MvRenameProcessor"
order="first"
Expand Down
64 changes: 64 additions & 0 deletions src/test/kotlin/org/move/ide/liveTemplates/MvLiveTemplatesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.move.ide.liveTemplates

import com.intellij.openapi.actionSystem.IdeActions
import org.intellij.lang.annotations.Language
import org.move.utils.tests.MvTestBase

class MvLiveTemplatesTest: MvTestBase() {

fun `test no test function in type`() = noSnippet("""
module 0x1::m {
struct S { val: t/*caret*/}
}
""")

fun `test no test function in struct fields block`() = noSnippet("""
module 0x1::m {
struct S { t/*caret*/ }
}
""")

fun `test no test function in enum variants block`() = noSnippet("""
module 0x1::m {
enum S { t/*caret*/ }
}
""")

fun `test no test function in tuple struct type`() = noSnippet("""
module 0x1::m {
struct S(u8, t/*caret*/)
}
""")

fun `test no test function in expr`() = noSnippet("""
module 0x1::m {
fun main() {
t/*caret*/
}
}
""")

fun `test no test function outside module`() = noSnippet("""
t/*caret*/
module 0x1::m {
}
""")

fun `test test function in module body`() = expandSnippet("""
module 0x1::m {
t/*caret*/
}
""", """
module 0x1::m {
#[test]
fun /*caret*/() {
}
}
""")

private fun expandSnippet(@Language("Move") before: String, @Language("Move") after: String) =
checkEditorAction(before, after, IdeActions.ACTION_EXPAND_LIVE_TEMPLATE_BY_TAB)

private fun noSnippet(@Language("Move") code: String) = expandSnippet(code, code)
}

0 comments on commit f027c5f

Please sign in to comment.