Skip to content

Commit 364e821

Browse files
committed
Impl. Syntax Highlighter for Expression Language
1 parent 6439cfb commit 364e821

File tree

3 files changed

+146
-0
lines changed

3 files changed

+146
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package fr.adrienbrault.idea.symfony2plugin.expressionLanguage;
2+
3+
import com.intellij.openapi.editor.colors.TextAttributesKey;
4+
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
5+
import com.intellij.openapi.options.colors.AttributesDescriptor;
6+
import com.intellij.openapi.options.colors.ColorDescriptor;
7+
import com.intellij.openapi.options.colors.ColorSettingsPage;
8+
import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
9+
import org.jetbrains.annotations.NotNull;
10+
import org.jetbrains.annotations.Nullable;
11+
12+
import javax.swing.*;
13+
import java.util.Map;
14+
15+
public class ExpressionLanguageColorSettingsPage implements ColorSettingsPage {
16+
17+
private static final AttributesDescriptor[] ATTRIBUTE_DESCRIPTORS = new AttributesDescriptor[]{
18+
new AttributesDescriptor("Number", ExpressionLanguageSyntaxHighlighter.NUMBER),
19+
new AttributesDescriptor("String", ExpressionLanguageSyntaxHighlighter.STRING),
20+
new AttributesDescriptor("Identifier", ExpressionLanguageSyntaxHighlighter.IDENTIFIER),
21+
new AttributesDescriptor("Keyword", ExpressionLanguageSyntaxHighlighter.KEYWORD),
22+
};
23+
24+
@Nullable
25+
@Override
26+
public Icon getIcon() {
27+
return Symfony2Icons.SYMFONY;
28+
}
29+
30+
@NotNull
31+
@Override
32+
public SyntaxHighlighter getHighlighter() {
33+
return new ExpressionLanguageSyntaxHighlighter();
34+
}
35+
36+
@NotNull
37+
@Override
38+
public String getDemoText() {
39+
return "article.getCommentCount(true) > 100 and article.category not in [\"misc\", null, true] === false";
40+
}
41+
42+
@NotNull
43+
@Override
44+
public String getDisplayName() {
45+
return "Symfony Expression Language";
46+
}
47+
48+
@NotNull
49+
@Override
50+
public AttributesDescriptor[] getAttributeDescriptors() {
51+
return ATTRIBUTE_DESCRIPTORS;
52+
}
53+
54+
@NotNull
55+
@Override
56+
public ColorDescriptor[] getColorDescriptors() {
57+
return ColorDescriptor.EMPTY_ARRAY;
58+
}
59+
60+
@Override
61+
public @Nullable
62+
Map<String, TextAttributesKey> getAdditionalHighlightingTagToDescriptorMap() {
63+
return null;
64+
}
65+
}
66+
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fr.adrienbrault.idea.symfony2plugin.expressionLanguage;
2+
3+
import com.intellij.lexer.Lexer;
4+
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
5+
import com.intellij.openapi.editor.colors.TextAttributesKey;
6+
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
7+
import com.intellij.psi.tree.IElementType;
8+
import fr.adrienbrault.idea.symfony2plugin.expressionLanguage.psi.ExpressionLanguageTypes;
9+
import org.jetbrains.annotations.NotNull;
10+
11+
import static com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributesKey;
12+
13+
public class ExpressionLanguageSyntaxHighlighter extends SyntaxHighlighterBase {
14+
15+
public static final TextAttributesKey NUMBER = createTextAttributesKey("NUMBER", DefaultLanguageHighlighterColors.NUMBER);
16+
public static final TextAttributesKey STRING = createTextAttributesKey("STRING", DefaultLanguageHighlighterColors.STRING);
17+
public static final TextAttributesKey IDENTIFIER = createTextAttributesKey("IDENTIFIER", DefaultLanguageHighlighterColors.IDENTIFIER);
18+
public static final TextAttributesKey KEYWORD = createTextAttributesKey("KEYWORD", DefaultLanguageHighlighterColors.KEYWORD);
19+
20+
private static final TextAttributesKey[] NUMBER_KEYS = new TextAttributesKey[]{NUMBER};
21+
private static final TextAttributesKey[] STRING_KEYS = new TextAttributesKey[]{STRING};
22+
private static final TextAttributesKey[] IDENTIFIER_KEYS = new TextAttributesKey[]{IDENTIFIER};
23+
private static final TextAttributesKey[] KEYWORD_KEYS = new TextAttributesKey[]{KEYWORD};
24+
private static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0];
25+
26+
@NotNull
27+
@Override
28+
public Lexer getHighlightingLexer() {
29+
return new ExpressionLanguageLexerAdapter();
30+
}
31+
32+
@NotNull
33+
@Override
34+
public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
35+
if (tokenType.equals(ExpressionLanguageTypes.NUMBER)) {
36+
return NUMBER_KEYS;
37+
} else if (tokenType.equals(ExpressionLanguageTypes.STRING)) {
38+
return STRING_KEYS;
39+
} else if (tokenType.equals(ExpressionLanguageTypes.ID)) {
40+
return IDENTIFIER_KEYS;
41+
} else if (tokenType.equals(ExpressionLanguageTypes.TRUE)) {
42+
return KEYWORD_KEYS;
43+
} else if (tokenType.equals(ExpressionLanguageTypes.FALSE)) {
44+
return KEYWORD_KEYS;
45+
} else if (tokenType.equals(ExpressionLanguageTypes.NULL)) {
46+
return KEYWORD_KEYS;
47+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_IN)) {
48+
return KEYWORD_KEYS;
49+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_NOT_IN)) {
50+
return KEYWORD_KEYS;
51+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_MATCHES)) {
52+
return KEYWORD_KEYS;
53+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_AND_KW)) {
54+
return KEYWORD_KEYS;
55+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_OR_KW)) {
56+
return KEYWORD_KEYS;
57+
} else if (tokenType.equals(ExpressionLanguageTypes.OP_NOT_KW)) {
58+
return KEYWORD_KEYS;
59+
}
60+
61+
return EMPTY_KEYS;
62+
}
63+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package fr.adrienbrault.idea.symfony2plugin.expressionLanguage;
2+
3+
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
4+
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
5+
import com.intellij.openapi.project.Project;
6+
import com.intellij.openapi.vfs.VirtualFile;
7+
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
9+
10+
public class ExpressionLanguageSyntaxHighlighterFactory extends SyntaxHighlighterFactory {
11+
12+
@NotNull
13+
@Override
14+
public SyntaxHighlighter getSyntaxHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile) {
15+
return new ExpressionLanguageSyntaxHighlighter();
16+
}
17+
}

0 commit comments

Comments
 (0)