feat(zon): Simple syntax highlighting

This commit is contained in:
FalsePattern 2023-08-08 23:00:20 +02:00 committed by FalsePattern
parent 3f4bc3d550
commit 6385be94dc
Signed by: falsepattern
GPG key ID: FDF7126A9E124447
4 changed files with 88 additions and 0 deletions

View file

@ -22,6 +22,7 @@ Changelog structure reference:
#### Zon
- Basic parser and PSI tree
- Basic syntax highlighting
## [0.4.0]

View file

@ -0,0 +1,69 @@
package com.falsepattern.zigbrains.zon.highlight;
import com.falsepattern.zigbrains.zon.lexer.ZonLexerAdapter;
import com.falsepattern.zigbrains.zon.psi.ZonTypes;
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.jetbrains.annotations.NotNull;
import java.util.HashMap;
import java.util.Map;
public class ZonSyntaxHighlighter extends SyntaxHighlighterBase {
// @formatter: off
public static final TextAttributesKey
EQ = createKey("EQ" , DefaultLanguageHighlighterColors.OPERATION_SIGN),
ID = createKey("ID" , DefaultLanguageHighlighterColors.INSTANCE_FIELD),
COMMENT = createKey("COMMENT" , DefaultLanguageHighlighterColors.LINE_COMMENT ),
BAD_CHAR = createKey("BAD_CHARACTER", HighlighterColors.BAD_CHARACTER ),
STRING = createKey("STRING" , DefaultLanguageHighlighterColors.STRING ),
COMMA = createKey("COMMA" , DefaultLanguageHighlighterColors.COMMA ),
DOT = createKey("DOT" , DefaultLanguageHighlighterColors.DOT ),
BRACE = createKey("BRACE" , DefaultLanguageHighlighterColors.BRACES );
// @formatter: on
private static final TextAttributesKey[] EMPTY_KEYS = new TextAttributesKey[0];
private static final Map<IElementType, TextAttributesKey[]> KEYMAP = new HashMap<>();
static {
// @formatter: off
addMapping(DOT , ZonTypes.DOT);
addMapping(COMMA , ZonTypes.COMMA);
addMapping(BRACE , ZonTypes.LBRACE);
addMapping(BRACE , ZonTypes.RBRACE);
addMapping(STRING , ZonTypes.LINE_STRING, ZonTypes.STRING_LITERAL_SINGLE);
addMapping(BAD_CHAR, TokenType.BAD_CHARACTER);
addMapping(COMMENT , ZonTypes.COMMENT);
addMapping(ID , ZonTypes.ID);
addMapping(EQ , ZonTypes.EQ);
// @formatter: on
}
private static void addMapping(TextAttributesKey key, IElementType... types) {
for (var type: types) {
KEYMAP.put(type, new TextAttributesKey[]{key});
}
}
private static TextAttributesKey createKey(String name, TextAttributesKey fallback) {
return TextAttributesKey.createTextAttributesKey("ZON_" + name, fallback);
}
@Override
public @NotNull Lexer getHighlightingLexer() {
return new ZonLexerAdapter();
}
@Override
public TextAttributesKey @NotNull [] getTokenHighlights(IElementType tokenType) {
if (KEYMAP.containsKey(tokenType)) {
return KEYMAP.get(tokenType);
}
return EMPTY_KEYS;
}
}

View file

@ -0,0 +1,15 @@
package com.falsepattern.zigbrains.zon.highlight;
import com.intellij.openapi.fileTypes.SyntaxHighlighter;
import com.intellij.openapi.fileTypes.SyntaxHighlighterFactory;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class ZonSyntaxHighlighterFactory extends SyntaxHighlighterFactory {
@Override
public @NotNull SyntaxHighlighter getSyntaxHighlighter(@Nullable Project project, @Nullable VirtualFile virtualFile) {
return new ZonSyntaxHighlighter();
}
}

View file

@ -88,6 +88,9 @@
<lang.parserDefinition language="Zon"
implementationClass="com.falsepattern.zigbrains.zon.parser.ZonParserDefinition"/>
<lang.syntaxHighlighterFactory language="Zon"
implementationClass="com.falsepattern.zigbrains.zon.highlight.ZonSyntaxHighlighterFactory"/>
<!-- endregion Zon -->
</extensions>