Skip to content

Commit 9ee1f22

Browse files
committed
feat(intellij): implemented brace matcher for variables
1 parent ef67d2c commit 9ee1f22

File tree

6 files changed

+91
-7
lines changed

6 files changed

+91
-7
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package dev.robotcode.robotcode4ij.editor
2+
3+
import com.intellij.lang.BracePair
4+
import com.intellij.lang.PairedBraceMatcher
5+
import com.intellij.psi.PsiFile
6+
import com.intellij.psi.tree.IElementType
7+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
8+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
9+
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
10+
import dev.robotcode.robotcode4ij.psi.VARIABLE_END
11+
12+
private val PAIRS = arrayOf(
13+
BracePair(VARIABLE_BEGIN, VARIABLE_END, false),
14+
BracePair(ENVIRONMENT_VARIABLE_BEGIN, ENVIRONMENT_VARIABLE_END, false)
15+
)
16+
17+
class RobotCodeBraceMatcher : PairedBraceMatcher {
18+
19+
override fun getPairs(): Array<BracePair> {
20+
return PAIRS
21+
}
22+
23+
override fun isPairedBracesAllowedBeforeType(lbraceType: IElementType, contextType: IElementType?): Boolean {
24+
return true
25+
}
26+
27+
override fun getCodeConstructStart(file: PsiFile?, openingBraceOffset: Int): Int {
28+
return openingBraceOffset
29+
}
30+
}

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/highlighting/RobotCodeHighlighterProvider.kt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory
1212
import com.intellij.openapi.project.Project
1313
import com.intellij.openapi.vfs.VirtualFile
1414
import com.intellij.psi.tree.IElementType
15+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
16+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
1517
import dev.robotcode.robotcode4ij.psi.RobotTextMateElementType
18+
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
19+
import dev.robotcode.robotcode4ij.psi.VARIABLE_END
1620
import org.jetbrains.plugins.textmate.language.syntax.highlighting.TextMateHighlighter
1721
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateLexerDataStorage
1822

@@ -57,11 +61,23 @@ class RobotCodeHighlighter : TextMateHighlighter(RobotTextMateHighlightingLexer(
5761
"string.unquoted.argument.robotframework" to arrayOf(RobotColors.ARGUMENT),
5862
"keyword.operator.continue.robotframework" to arrayOf(RobotColors.CONTINUATION),
5963
)
64+
val elementTypeMap = mapOf(
65+
VARIABLE_BEGIN to arrayOf(RobotColors.VARIABLE_BEGIN),
66+
VARIABLE_END to arrayOf(RobotColors.VARIABLE_END),
67+
ENVIRONMENT_VARIABLE_BEGIN to arrayOf(RobotColors.VARIABLE_BEGIN),
68+
ENVIRONMENT_VARIABLE_END to arrayOf(RobotColors.VARIABLE_END),
69+
)
6070
}
6171

6272
override fun getTokenHighlights(tokenType: IElementType?): Array<TextAttributesKey> {
63-
val result = elementMap[(tokenType as? RobotTextMateElementType)?.element?.scope?.scopeName]
64-
if (result != null) return result
73+
if (tokenType is RobotTextMateElementType) {
74+
val result = elementMap[tokenType.element.scope.scopeName]
75+
if (result != null) return result
76+
}
77+
if (tokenType in elementTypeMap) {
78+
val result = elementTypeMap[tokenType]
79+
if (result != null) return result
80+
}
6581
return super.getTokenHighlights(tokenType)
6682
}
6783
}

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/highlighting/RobotTextMateHighlightingLexer.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,39 @@ package dev.robotcode.robotcode4ij.highlighting
33
import com.intellij.openapi.util.registry.Registry
44
import com.intellij.psi.tree.IElementType
55
import dev.robotcode.robotcode4ij.TextMateBundleHolder
6+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
7+
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
68
import dev.robotcode.robotcode4ij.psi.RobotTextMateElementType
9+
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
10+
import dev.robotcode.robotcode4ij.psi.VARIABLE_END
711
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateElementType
812
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateHighlightingLexer
913

1014
class RobotTextMateHighlightingLexer : TextMateHighlightingLexer(
1115
TextMateBundleHolder.descriptor,
1216
Registry.get("textmate.line.highlighting.limit").asInteger()
1317
) {
18+
companion object {
19+
val BRACES_START = setOf(
20+
"punctuation.definition.variable.begin.robotframework",
21+
"punctuation.definition.envvar.begin.robotframework"
22+
)
23+
val BRACES_END = setOf(
24+
"punctuation.definition.variable.end.robotframework",
25+
"punctuation.definition.envvar.end.robotframework"
26+
)
27+
}
28+
1429
override fun getTokenType(): IElementType? {
1530
val result = super.getTokenType() ?: return null
1631
if (result is TextMateElementType) {
17-
return RobotTextMateElementType(result)
32+
return when (result.scope.scopeName) {
33+
"punctuation.definition.variable.begin.robotframework" -> VARIABLE_BEGIN
34+
"punctuation.definition.variable.end.robotframework" -> VARIABLE_END
35+
"punctuation.definition.envvar.begin.robotframework" -> ENVIRONMENT_VARIABLE_BEGIN
36+
"punctuation.definition.envvar.end.robotframework" -> ENVIRONMENT_VARIABLE_END
37+
else -> RobotTextMateElementType(result)
38+
}
1839
}
1940
return result
2041
}

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/psi/ElementTypes.kt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,22 @@ val COMMENT_LINE = IElementType("COMMENT_LINE", RobotFrameworkLanguage)
1313
val COMMENT_BLOCK = IElementType("COMMENT_BLOCK", RobotFrameworkLanguage)
1414
val ARGUMENT = IElementType("ARGUMENT", RobotFrameworkLanguage)
1515

16+
val VARIABLE_BEGIN = IElementType("VARIABLE_BEGIN", RobotFrameworkLanguage)
17+
val VARIABLE_END = IElementType("VARIABLE_END", RobotFrameworkLanguage)
18+
val ENVIRONMENT_VARIABLE_BEGIN = IElementType("VARIABLE_BEGIN", RobotFrameworkLanguage)
19+
val ENVIRONMENT_VARIABLE_END = IElementType("VARIABLE_END", RobotFrameworkLanguage)
20+
1621
val COMMENT_TOKENS = TokenSet.create(COMMENT_LINE, COMMENT_BLOCK)
1722
val STRING_TOKENS = TokenSet.create(ARGUMENT)
1823

19-
class RobotTextMateElementType(val element: TextMateElementType) : IElementType(
20-
"ROBOT_TEXTMATE_ELEMENT_TYPE",
21-
RobotFrameworkLanguage
22-
)
24+
25+
class RobotTextMateElementType(
26+
val element: TextMateElementType, debugName: String = "ROBOT_TEXTMATE_ELEMENT_TYPE",
27+
register: Boolean = false
28+
) :
29+
IElementType(
30+
debugName,
31+
RobotFrameworkLanguage,
32+
register
33+
)
34+

intellij-client/src/main/kotlin/dev/robotcode/robotcode4ij/psi/RobotCodeParserDefinition.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class RobotCodeParserDefinition : ParserDefinition {
4343
// ARGUMENT -> ArgumentPsiElement(node)
4444
TESTCASE_NAME -> TestCasePsiElement(node)
4545
is RobotTextMateElementType -> SimpleASTWrapperPsiElement(node)
46+
VARIABLE_BEGIN, VARIABLE_END -> SimpleASTWrapperPsiElement(node)
47+
ENVIRONMENT_VARIABLE_BEGIN, ENVIRONMENT_VARIABLE_END -> SimpleASTWrapperPsiElement(node)
48+
4649
else -> throw IllegalArgumentException("Unknown element type: ${node.elementType}")
4750
}
4851
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
language="robotframework"
3333
implementationClass="dev.robotcode.robotcode4ij.highlighting.RobotCodeSyntaxHighlighterFactory"/>
3434

35+
<lang.braceMatcher language="robotframework"
36+
implementationClass="dev.robotcode.robotcode4ij.editor.RobotCodeBraceMatcher"/>
3537
<lang.commenter language="robotframework"
3638
implementationClass="dev.robotcode.robotcode4ij.editor.RobotCodeCommenter"/>
3739

0 commit comments

Comments
 (0)