Skip to content

Commit

Permalink
feat(intellij): implemented brace matcher for variables
Browse files Browse the repository at this point in the history
  • Loading branch information
d-biehl committed Jan 3, 2025
1 parent ef67d2c commit 9ee1f22
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.robotcode.robotcode4ij.editor

import com.intellij.lang.BracePair
import com.intellij.lang.PairedBraceMatcher
import com.intellij.psi.PsiFile
import com.intellij.psi.tree.IElementType
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.VARIABLE_END

private val PAIRS = arrayOf(
BracePair(VARIABLE_BEGIN, VARIABLE_END, false),
BracePair(ENVIRONMENT_VARIABLE_BEGIN, ENVIRONMENT_VARIABLE_END, false)
)

class RobotCodeBraceMatcher : PairedBraceMatcher {

override fun getPairs(): Array<BracePair> {
return PAIRS
}

override fun isPairedBracesAllowedBeforeType(lbraceType: IElementType, contextType: IElementType?): Boolean {
return true
}

override fun getCodeConstructStart(file: PsiFile?, openingBraceOffset: Int): Int {
return openingBraceOffset
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory
import com.intellij.openapi.project.Project
import com.intellij.openapi.vfs.VirtualFile
import com.intellij.psi.tree.IElementType
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
import dev.robotcode.robotcode4ij.psi.RobotTextMateElementType
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.VARIABLE_END
import org.jetbrains.plugins.textmate.language.syntax.highlighting.TextMateHighlighter
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateLexerDataStorage

Expand Down Expand Up @@ -57,11 +61,23 @@ class RobotCodeHighlighter : TextMateHighlighter(RobotTextMateHighlightingLexer(
"string.unquoted.argument.robotframework" to arrayOf(RobotColors.ARGUMENT),
"keyword.operator.continue.robotframework" to arrayOf(RobotColors.CONTINUATION),
)
val elementTypeMap = mapOf(
VARIABLE_BEGIN to arrayOf(RobotColors.VARIABLE_BEGIN),
VARIABLE_END to arrayOf(RobotColors.VARIABLE_END),
ENVIRONMENT_VARIABLE_BEGIN to arrayOf(RobotColors.VARIABLE_BEGIN),
ENVIRONMENT_VARIABLE_END to arrayOf(RobotColors.VARIABLE_END),
)
}

override fun getTokenHighlights(tokenType: IElementType?): Array<TextAttributesKey> {
val result = elementMap[(tokenType as? RobotTextMateElementType)?.element?.scope?.scopeName]
if (result != null) return result
if (tokenType is RobotTextMateElementType) {
val result = elementMap[tokenType.element.scope.scopeName]
if (result != null) return result
}
if (tokenType in elementTypeMap) {
val result = elementTypeMap[tokenType]
if (result != null) return result
}
return super.getTokenHighlights(tokenType)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,39 @@ package dev.robotcode.robotcode4ij.highlighting
import com.intellij.openapi.util.registry.Registry
import com.intellij.psi.tree.IElementType
import dev.robotcode.robotcode4ij.TextMateBundleHolder
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.ENVIRONMENT_VARIABLE_END
import dev.robotcode.robotcode4ij.psi.RobotTextMateElementType
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
import dev.robotcode.robotcode4ij.psi.VARIABLE_END
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateElementType
import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateHighlightingLexer

class RobotTextMateHighlightingLexer : TextMateHighlightingLexer(
TextMateBundleHolder.descriptor,
Registry.get("textmate.line.highlighting.limit").asInteger()
) {
companion object {
val BRACES_START = setOf(
"punctuation.definition.variable.begin.robotframework",
"punctuation.definition.envvar.begin.robotframework"
)
val BRACES_END = setOf(
"punctuation.definition.variable.end.robotframework",
"punctuation.definition.envvar.end.robotframework"
)
}

override fun getTokenType(): IElementType? {
val result = super.getTokenType() ?: return null
if (result is TextMateElementType) {
return RobotTextMateElementType(result)
return when (result.scope.scopeName) {
"punctuation.definition.variable.begin.robotframework" -> VARIABLE_BEGIN
"punctuation.definition.variable.end.robotframework" -> VARIABLE_END
"punctuation.definition.envvar.begin.robotframework" -> ENVIRONMENT_VARIABLE_BEGIN
"punctuation.definition.envvar.end.robotframework" -> ENVIRONMENT_VARIABLE_END
else -> RobotTextMateElementType(result)
}
}
return result
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@ val COMMENT_LINE = IElementType("COMMENT_LINE", RobotFrameworkLanguage)
val COMMENT_BLOCK = IElementType("COMMENT_BLOCK", RobotFrameworkLanguage)
val ARGUMENT = IElementType("ARGUMENT", RobotFrameworkLanguage)

val VARIABLE_BEGIN = IElementType("VARIABLE_BEGIN", RobotFrameworkLanguage)
val VARIABLE_END = IElementType("VARIABLE_END", RobotFrameworkLanguage)
val ENVIRONMENT_VARIABLE_BEGIN = IElementType("VARIABLE_BEGIN", RobotFrameworkLanguage)
val ENVIRONMENT_VARIABLE_END = IElementType("VARIABLE_END", RobotFrameworkLanguage)

val COMMENT_TOKENS = TokenSet.create(COMMENT_LINE, COMMENT_BLOCK)
val STRING_TOKENS = TokenSet.create(ARGUMENT)

class RobotTextMateElementType(val element: TextMateElementType) : IElementType(
"ROBOT_TEXTMATE_ELEMENT_TYPE",
RobotFrameworkLanguage
)

class RobotTextMateElementType(
val element: TextMateElementType, debugName: String = "ROBOT_TEXTMATE_ELEMENT_TYPE",
register: Boolean = false
) :
IElementType(
debugName,
RobotFrameworkLanguage,
register
)

Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class RobotCodeParserDefinition : ParserDefinition {
// ARGUMENT -> ArgumentPsiElement(node)
TESTCASE_NAME -> TestCasePsiElement(node)
is RobotTextMateElementType -> SimpleASTWrapperPsiElement(node)
VARIABLE_BEGIN, VARIABLE_END -> SimpleASTWrapperPsiElement(node)
ENVIRONMENT_VARIABLE_BEGIN, ENVIRONMENT_VARIABLE_END -> SimpleASTWrapperPsiElement(node)

else -> throw IllegalArgumentException("Unknown element type: ${node.elementType}")
}
}
Expand Down
2 changes: 2 additions & 0 deletions intellij-client/src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
language="robotframework"
implementationClass="dev.robotcode.robotcode4ij.highlighting.RobotCodeSyntaxHighlighterFactory"/>

<lang.braceMatcher language="robotframework"
implementationClass="dev.robotcode.robotcode4ij.editor.RobotCodeBraceMatcher"/>
<lang.commenter language="robotframework"
implementationClass="dev.robotcode.robotcode4ij.editor.RobotCodeCommenter"/>

Expand Down

0 comments on commit 9ee1f22

Please sign in to comment.