-
Notifications
You must be signed in to change notification settings - Fork 8
/
STSyntaxHighlighter.java
103 lines (93 loc) · 3.44 KB
/
STSyntaxHighlighter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package org.antlr.jetbrains.st4plugin.highlight;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import org.antlr.jetbrains.st4plugin.parsing.ANTLR3StringStream;
import org.antlr.jetbrains.st4plugin.parsing.ParserErrorListener;
import org.antlr.jetbrains.st4plugin.parsing.ParsingResult;
import org.antlr.jetbrains.st4plugin.parsing.STLexer;
import org.antlr.jetbrains.st4plugin.parsing.STParser;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.ParseTree;
import org.antlr.v4.runtime.tree.TerminalNode;
import org.antlr.v4.runtime.tree.xpath.XPath;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import static com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributesKey;
public class STSyntaxHighlighter extends SyntaxHighlighter {
public static final TextAttributesKey STGroup_TEMPLATE_TEXT =
createTextAttributesKey("STGroup_TEMPLATE_TEXT", DefaultLanguageHighlighterColors.TEMPLATE_LANGUAGE_COLOR);
public static final TextAttributesKey ST_ID = STGroupSyntaxHighlighter.STGroup_TEMPLATE_NAME;
public STGroupSyntaxHighlighter groupHighlighter;
public Token templateToken;
public STSyntaxHighlighter(STGroupSyntaxHighlighter groupHighlighter,
Editor editor,
Token templateToken,
int startIndex)
{
super(editor, startIndex, groupHighlighter.highlightInfos);
this.templateToken = templateToken;
this.groupHighlighter = groupHighlighter;
}
@Override
public Lexer getLexer(String text) {
STLexer lexer = new STLexer(new ANTLR3StringStream(text), templateToken, groupHighlighter.delimiters[0], groupHighlighter.delimiters[1]);
return lexer;
}
@Override
public ParsingResult parse(CommonTokenStream tokens) {
STParser parser = new STParser(tokens);
parser.removeErrorListeners();
ParserErrorListener errorListener = new ParserErrorListener();
parser.addErrorListener(errorListener);
ParserRuleContext tree = parser.template();
return new ParsingResult(parser, tree, errorListener);
}
@Override
public void highlightTree(ParserRuleContext tree, Parser parser) {
final Collection<ParseTree> ids = XPath.findAll(tree, "//primary/ID", parser);
for (ParseTree id : ids) {
TerminalNode tnode = (TerminalNode)id;
highlightToken(tnode.getSymbol(), new TextAttributesKey[]{ST_ID});
}
}
@NotNull
@Override
public TextAttributesKey[] getAttributesKey(Token t) {
int tokenType = t.getType();
TextAttributesKey key;
switch ( tokenType ) {
case STLexer.IF:
case STLexer.ELSE:
case STLexer.REGION_END:
case STLexer.TRUE:
case STLexer.FALSE:
case STLexer.ELSEIF:
case STLexer.ENDIF:
case STLexer.SUPER:
key = DefaultLanguageHighlighterColors.KEYWORD;
break;
case STLexer.ID:
key = ST_ID;
break;
case STLexer.STRING:
case STLexer.TEXT:
key = STGroup_TEMPLATE_TEXT;
break;
case STLexer.COMMENT:
key = DefaultLanguageHighlighterColors.LINE_COMMENT;
break;
case STLexer.ERROR_TYPE :
key = HighlighterColors.BAD_CHARACTER;
break;
default:
return EMPTY;
}
return new TextAttributesKey[]{key};
}
}