11package dev.robotcode.robotcode4ij.highlighting
22
33import com.intellij.lexer.Lexer
4+ import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
5+ import com.intellij.openapi.editor.HighlighterColors
46import com.intellij.openapi.editor.colors.TextAttributesKey
5- import com.intellij.openapi.fileTypes.PlainSyntaxHighlighter
67import com.intellij.openapi.fileTypes.SyntaxHighlighterBase
78import com.intellij.psi.tree.IElementType
8- import com.intellij.util.containers.ContainerUtil
99import dev.robotcode.robotcode4ij.psi.ARGUMENT
1010import dev.robotcode.robotcode4ij.psi.COMMENT_BLOCK
1111import dev.robotcode.robotcode4ij.psi.COMMENT_LINE
@@ -23,11 +23,6 @@ import dev.robotcode.robotcode4ij.psi.TESTCASE_NAME
2323import dev.robotcode.robotcode4ij.psi.VARIABLE
2424import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
2525import dev.robotcode.robotcode4ij.psi.VARIABLE_END
26- import org.jetbrains.plugins.textmate.TextMateService
27- import org.jetbrains.plugins.textmate.language.TextMateScopeComparator
28- import org.jetbrains.plugins.textmate.language.syntax.highlighting.TextMateTheme
29- import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateScope
30- import java.util.function.Function
3126
3227
3328class RobotCodeSyntaxHighlighter : SyntaxHighlighterBase () {
@@ -51,59 +46,85 @@ class RobotCodeSyntaxHighlighter : SyntaxHighlighterBase() {
5146 CONTINUATION to arrayOf(Colors .CONTINUATION ),
5247 )
5348
54- val PLAIN_SYNTAX_HIGHLIGHTER : PlainSyntaxHighlighter = PlainSyntaxHighlighter ()
49+ val textMateElementMap = mapOf (
50+ " comment" to arrayOf(DefaultLanguageHighlighterColors .LINE_COMMENT ),
51+ " constant" to arrayOf(DefaultLanguageHighlighterColors .CONSTANT ),
52+ " constant.character.escape" to arrayOf(DefaultLanguageHighlighterColors .VALID_STRING_ESCAPE ),
53+ " constant.language" to arrayOf(DefaultLanguageHighlighterColors .KEYWORD ),
54+ " constant.numeric" to arrayOf(DefaultLanguageHighlighterColors .NUMBER ),
55+ " declaration.section" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
56+ " entity.name.section" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
57+ " declaration.tag" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
58+ " entity.name.function" to arrayOf(DefaultLanguageHighlighterColors .FUNCTION_DECLARATION ),
59+ " entity.name.tag" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
60+ " entity.name.type" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
61+ " entity.other.attribute-name" to arrayOf(DefaultLanguageHighlighterColors .INSTANCE_FIELD ),
62+ " entity.other.inherited-class" to arrayOf(DefaultLanguageHighlighterColors .CLASS_REFERENCE ),
63+ " invalid" to arrayOf(DefaultLanguageHighlighterColors .INVALID_STRING_ESCAPE ),
64+ " invalid.deprecated.trailing-whitespace" to arrayOf(DefaultLanguageHighlighterColors .INVALID_STRING_ESCAPE ),
65+ " keyword" to arrayOf(DefaultLanguageHighlighterColors .KEYWORD ),
66+ " keyword.control.import" to arrayOf(DefaultLanguageHighlighterColors .KEYWORD ),
67+ " keyword.operator" to arrayOf(DefaultLanguageHighlighterColors .OPERATION_SIGN ),
68+ " markup.heading" to arrayOf(DefaultLanguageHighlighterColors .MARKUP_TAG ),
69+ " markup.list" to arrayOf(DefaultLanguageHighlighterColors .MARKUP_TAG ),
70+ " markup.quote" to arrayOf(DefaultLanguageHighlighterColors .MARKUP_TAG ),
71+ " meta.embedded" to arrayOf(HighlighterColors .TEXT ),
72+ " meta.preprocessor" to arrayOf(DefaultLanguageHighlighterColors .METADATA ),
73+ " meta.section" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
74+ " entity.name.section" to arrayOf(DefaultLanguageHighlighterColors .CLASS_NAME ),
75+ " meta.tag" to arrayOf(DefaultLanguageHighlighterColors .METADATA ),
76+ " storage" to arrayOf(DefaultLanguageHighlighterColors .KEYWORD ),
77+ " storage.type.method" to arrayOf(DefaultLanguageHighlighterColors .KEYWORD ),
78+ " string" to arrayOf(DefaultLanguageHighlighterColors .STRING ),
79+ " string.source" to arrayOf(DefaultLanguageHighlighterColors .STRING ),
80+ " string.unquoted" to arrayOf(DefaultLanguageHighlighterColors .STRING ),
81+ " support.class" to arrayOf(DefaultLanguageHighlighterColors .CLASS_REFERENCE ),
82+ " support.constant" to arrayOf(DefaultLanguageHighlighterColors .CONSTANT ),
83+ " support.function" to arrayOf(DefaultLanguageHighlighterColors .FUNCTION_CALL ),
84+ " support.type" to arrayOf(DefaultLanguageHighlighterColors .CLASS_REFERENCE ),
85+ " support.variable" to arrayOf(DefaultLanguageHighlighterColors .GLOBAL_VARIABLE ),
86+ " text" to arrayOf(DefaultLanguageHighlighterColors .STRING ),
87+ " variable" to arrayOf(DefaultLanguageHighlighterColors .GLOBAL_VARIABLE ),
88+ " variable.language" to arrayOf(DefaultLanguageHighlighterColors .GLOBAL_VARIABLE ),
89+ " variable.other" to arrayOf(DefaultLanguageHighlighterColors .GLOBAL_VARIABLE ),
90+ " variable.parameter" to arrayOf(DefaultLanguageHighlighterColors .PARAMETER ),
91+ " punctuation.definition.string" to arrayOf(DefaultLanguageHighlighterColors .STRING ),
92+ )
5593 }
5694
95+
5796 private val myLexer = RobotCodeLexer ()
5897
5998 override fun getHighlightingLexer (): Lexer {
6099 return myLexer
61100 }
62101
102+ fun createSubstringSequence (input : String ): Sequence <String > = sequence {
103+ var current = input
104+ while (current.isNotEmpty()) {
105+ yield (current)
106+ current = current.substringBeforeLast(' .' , " " )
107+ }
108+ }
109+
63110 override fun getTokenHighlights (tokenType : IElementType ? ): Array <TextAttributesKey > {
64111 val result = elementTypeMap[tokenType]
65112 if (result != null ) return result
66113
67- if (tokenType !is RobotTextMateElementType ) return PLAIN_SYNTAX_HIGHLIGHTER .getTokenHighlights(tokenType)
68-
69- val service = TextMateService .getInstance()
70- val customHighlightingColors = service.customHighlightingColors
114+ if (tokenType !is RobotTextMateElementType ) return arrayOf(HighlighterColors .TEXT )
71115
72- val highlightingRules = ContainerUtil .union(customHighlightingColors.keys, TextMateTheme . INSTANCE .rules )
116+ val result1 = mutableListOf< TextAttributesKey >( )
73117
74- val textMateScope = trimEmbeddedScope(tokenType)
75- val selectors: List <CharSequence > = ContainerUtil .reverse(
76- TextMateScopeComparator (textMateScope, Function .identity())
77- .sortAndFilter(highlightingRules)
78- )
79- val result1 = ContainerUtil .map2Array(
80- selectors,
81- TextAttributesKey ::class .java
82- ) { rule: CharSequence ->
83- val customTextAttributes = customHighlightingColors[rule]
84- customTextAttributes?.getTextAttributesKey(TextMateTheme .INSTANCE )
85- ? : TextMateTheme .INSTANCE .getTextAttributesKey(rule)
86- }
87-
88- return result1
89- }
90-
91- private fun trimEmbeddedScope (tokenType : RobotTextMateElementType ): TextMateScope {
92- var current: TextMateScope ? = tokenType.scope
93- val trail: MutableList <CharSequence ?> = ArrayList ()
94- while (current != null ) {
95- val scopeName = current.scopeName
96- if (scopeName != null && scopeName.contains(" .embedded." )) {
97- var result = TextMateScope .EMPTY
98- for (i in trail.indices.reversed()) {
99- result = result.add(trail[i])
100- }
101- return result
118+ for (scope1 in (tokenType.scope.scopeName?.toString() ? : " " ).split(" ." )) {
119+ for (scope2 in createSubstringSequence(scope1)) {
120+ val result2 = textMateElementMap[scope2]
121+ if (result2 != null ) result1.addAll(result2)
102122 }
103- trail.add(scopeName)
104- current = current.parent
105123 }
106- return tokenType.scope
124+
125+ if (result1.isNotEmpty()) return result1.toTypedArray()
126+
127+ return arrayOf(HighlighterColors .TEXT )
107128 }
108129}
109130
0 commit comments