aboutsummaryrefslogtreecommitdiffstats
path: root/sd-plugin/src/main/java/org/intellij/sdk/language/SdSyntaxHighlighter.java
blob: ea2e6331c382ba795c367dc4422a9ff3b0d5bf42 (plain) (blame)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package org.intellij.sdk.language;

import static com.intellij.openapi.editor.colors.TextAttributesKey.createTextAttributesKey;

import com.intellij.lexer.Lexer;
import com.intellij.openapi.editor.DefaultLanguageHighlighterColors;
import com.intellij.openapi.editor.HighlighterColors;
import com.intellij.openapi.editor.colors.TextAttributesKey;
import com.intellij.openapi.fileTypes.SyntaxHighlighterBase;
import com.intellij.psi.TokenType;
import com.intellij.psi.tree.IElementType;
import org.intellij.sdk.language.psi.SdTypes;
import org.jetbrains.annotations.NotNull;

import java.util.HashSet;

public class SdSyntaxHighlighter extends SyntaxHighlighterBase {
    
    private static final HashSet<IElementType> keyWordsSet = initKeyWordsSet();
    private static final HashSet<IElementType> constantsSet = initConstantsSet();
//    private static final HashSet<IElementType> symbols = initSymbolsSet();
    
    
    public static final TextAttributesKey IDENTIFIER =
        createTextAttributesKey("SD_IDENTIFIER", DefaultLanguageHighlighterColors.INSTANCE_FIELD);
    public static final TextAttributesKey CONSTANT =
        createTextAttributesKey("SD_CONSTANT", DefaultLanguageHighlighterColors.CONSTANT);
    public static final TextAttributesKey KEY =
        createTextAttributesKey("SD_KEY", DefaultLanguageHighlighterColors.KEYWORD);
    public static final TextAttributesKey SYMBOL =
        createTextAttributesKey("SD_SYMBOL", DefaultLanguageHighlighterColors.BRACKETS);
    public static final TextAttributesKey STRING =
        createTextAttributesKey("SD_STRING", DefaultLanguageHighlighterColors.STRING);
    public static final TextAttributesKey COMMENT =
        createTextAttributesKey("SD_COMMENT", DefaultLanguageHighlighterColors.LINE_COMMENT);
    public static final TextAttributesKey BAD_CHARACTER =
        createTextAttributesKey("Sd_BAD_CHARACTER", HighlighterColors.BAD_CHARACTER);
    
    
    private static final TextAttributesKey[] BAD_CHAR_KEYS = new TextAttributesKey[]{BAD_CHARACTER};
    private static final TextAttributesKey[] IDENTIFIER_KEYS = new TextAttributesKey[]{IDENTIFIER};
    private static final TextAttributesKey[] CONSTANT_KEYS = new TextAttributesKey[]{CONSTANT};
    private static final TextAttributesKey[] KEY_KEYS = new TextAttributesKey[]{KEY};
    private static final TextAttributesKey[] SYMBOL_KEYS = new TextAttributesKey[]{SYMBOL};
    private static final TextAttributesKey[] STRING_KEYS = new TextAttributesKey[]{STRING};
    private static final TextAttributesKey[] COMMENT_KEYS = new TextAttributesKey[]{COMMENT};
    private static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0];
    
    @NotNull
    @Override
    public Lexer getHighlightingLexer() {
        return new SdLexerAdapter();
    }
    
    @NotNull
    @Override
    public TextAttributesKey[] getTokenHighlights(IElementType tokenType) {
        if (tokenType.equals(SdTypes.IDENTIFIER_VAL)) {
            return IDENTIFIER_KEYS;
//        } else if (tokenType.equals(SdTypes.KEY)) {
//            return KEY_KEYS;
//        } else if (tokenType.equals(SdTypes.VALUE)) {
//            return VALUE_KEYS;
        } else if (keyWordsSet.contains(tokenType)) {
            return KEY_KEYS;
        } else if (tokenType.equals(SdTypes.SYMBOL)) {
            return SYMBOL_KEYS;
        } else if (tokenType.equals(SdTypes.STRING)) {
            return STRING_KEYS;
        } else if (tokenType.equals(SdTypes.COMMENT)) {
            return COMMENT_KEYS;
        } else if (constantsSet.contains(tokenType)) {
            return CONSTANT_KEYS;
        } else if (tokenType.equals(TokenType.BAD_CHARACTER)) {
            return BAD_CHAR_KEYS;
        } else {
            return EMPTY_KEYS;
        }
    }
    
//    private static HashSet<IElementType> initSymbolsSet() {
//        HashSet<IElementType> symbols = new HashSet<>();
//        symbols.add('{');
//        return symbols;
//    }
    
    private static HashSet<IElementType> initKeyWordsSet() {
        HashSet<IElementType> keyWords = new HashSet<>();
        keyWords.add(SdTypes.MACRO);
        keyWords.add(SdTypes.FIELD);
        keyWords.add(SdTypes.TYPE);
        keyWords.add(SdTypes.SEARCH);
        keyWords.add(SdTypes.DOCUMENT);
        keyWords.add(SdTypes.INHERITS);
        keyWords.add(SdTypes.STRUCT);
        keyWords.add(SdTypes.STRUCT_FIELD);
        keyWords.add(SdTypes.MATCH);
        keyWords.add(SdTypes.INDEXING);
        keyWords.add(SdTypes.RANK);
        keyWords.add(SdTypes.INDEXING_REWRITE);
        keyWords.add(SdTypes.QUERY_COMMAND);
        return keyWords;
    }
    
    private static HashSet<IElementType> initConstantsSet() {
        HashSet<IElementType> constants = new HashSet<>();
        constants.add(SdTypes.SUMMARY);
        constants.add(SdTypes.ATTRIBUTE);
        constants.add(SdTypes.TEXT);
        constants.add(SdTypes.EXACT);
        constants.add(SdTypes.EXACT_TERMINATOR);
        constants.add(SdTypes.WORD);
        constants.add(SdTypes.PREFIX);
        constants.add(SdTypes.CASED);
        constants.add(SdTypes.UNCASED);
        constants.add(SdTypes.SUBSTRING);
        constants.add(SdTypes.SUFFIX);
        constants.add(SdTypes.MAX_LENGTH);
        constants.add(SdTypes.GRAM);
        constants.add(SdTypes.GRAM_SIZE);
        constants.add(SdTypes.INDEX);
        constants.add(SdTypes.FAST_SEARCH);
        constants.add(SdTypes.FAST_ACCESS);
        constants.add(SdTypes.ALIAS);
        constants.add(SdTypes.SORTING);
        constants.add(SdTypes.DISTANCE_METRIC);
        constants.add(SdTypes.FILTER);
        constants.add(SdTypes.NORMAL);
        constants.add(SdTypes.NONE);
        constants.add(SdTypes.FULL);
        constants.add(SdTypes.DYNAMIC);
        constants.add(SdTypes.MATCHED_ELEMENTS_ONLY);
        
        return constants;
    }
}