1
1
package dev.robotcode.robotcode4ij.highlighting
2
2
3
3
import com.intellij.lexer.Lexer
4
+ import com.intellij.openapi.editor.DefaultLanguageHighlighterColors
5
+ import com.intellij.openapi.editor.HighlighterColors
4
6
import com.intellij.openapi.editor.colors.TextAttributesKey
5
- import com.intellij.openapi.fileTypes.PlainSyntaxHighlighter
6
7
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase
7
8
import com.intellij.psi.tree.IElementType
8
- import com.intellij.util.containers.ContainerUtil
9
9
import dev.robotcode.robotcode4ij.psi.ARGUMENT
10
10
import dev.robotcode.robotcode4ij.psi.COMMENT_BLOCK
11
11
import dev.robotcode.robotcode4ij.psi.COMMENT_LINE
@@ -23,11 +23,6 @@ import dev.robotcode.robotcode4ij.psi.TESTCASE_NAME
23
23
import dev.robotcode.robotcode4ij.psi.VARIABLE
24
24
import dev.robotcode.robotcode4ij.psi.VARIABLE_BEGIN
25
25
import 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
31
26
32
27
33
28
class RobotCodeSyntaxHighlighter : SyntaxHighlighterBase () {
@@ -51,59 +46,85 @@ class RobotCodeSyntaxHighlighter : SyntaxHighlighterBase() {
51
46
CONTINUATION to arrayOf(Colors .CONTINUATION ),
52
47
)
53
48
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
+ )
55
93
}
56
94
95
+
57
96
private val myLexer = RobotCodeLexer ()
58
97
59
98
override fun getHighlightingLexer (): Lexer {
60
99
return myLexer
61
100
}
62
101
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
+
63
110
override fun getTokenHighlights (tokenType : IElementType ? ): Array <TextAttributesKey > {
64
111
val result = elementTypeMap[tokenType]
65
112
if (result != null ) return result
66
113
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 )
71
115
72
- val highlightingRules = ContainerUtil .union(customHighlightingColors.keys, TextMateTheme . INSTANCE .rules )
116
+ val result1 = mutableListOf< TextAttributesKey >( )
73
117
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)
102
122
}
103
- trail.add(scopeName)
104
- current = current.parent
105
123
}
106
- return tokenType.scope
124
+
125
+ if (result1.isNotEmpty()) return result1.toTypedArray()
126
+
127
+ return arrayOf(HighlighterColors .TEXT )
107
128
}
108
129
}
109
130
0 commit comments