chore: Typo protection for semantic token processing

This commit is contained in:
FalsePattern 2024-07-18 14:01:55 +02:00
parent ad31397479
commit ee98d04b04
Signed by: falsepattern
GPG key ID: E930CDEC50C50E23
3 changed files with 120 additions and 33 deletions

View file

@ -0,0 +1,18 @@
package com.falsepattern.zigbrains.zig.lsp;
import org.eclipse.lsp4j.SemanticTokenModifiers;
public class ZLSSemanticTokenModifiers {
public static final String Declaration = SemanticTokenModifiers.Declaration;
public static final String Definition = SemanticTokenModifiers.Definition;
public static final String Readonly = SemanticTokenModifiers.Readonly;
public static final String Static = SemanticTokenModifiers.Static;
public static final String Deprecated = SemanticTokenModifiers.Deprecated;
public static final String Abstract = SemanticTokenModifiers.Abstract;
public static final String Async = SemanticTokenModifiers.Async;
public static final String Modification = SemanticTokenModifiers.Modification;
public static final String Documentation = SemanticTokenModifiers.Documentation;
public static final String DefaultLibrary = SemanticTokenModifiers.DefaultLibrary;
/** non standard token modifier */
public static final String Generic = "generic";
}

View file

@ -0,0 +1,41 @@
package com.falsepattern.zigbrains.zig.lsp;
import org.eclipse.lsp4j.SemanticTokenTypes;
public final class ZLSSemanticTokenTypes {
public static final String Namespace = SemanticTokenTypes.Namespace;
public static final String Type = SemanticTokenTypes.Type;
public static final String Class = SemanticTokenTypes.Class;
public static final String Enum = SemanticTokenTypes.Enum;
public static final String Interface = SemanticTokenTypes.Interface;
public static final String Struct = SemanticTokenTypes.Struct;
public static final String TypeParameter = SemanticTokenTypes.TypeParameter;
public static final String Parameter = SemanticTokenTypes.Parameter;
public static final String Variable = SemanticTokenTypes.Variable;
public static final String Property = SemanticTokenTypes.Property;
public static final String EnumMember = SemanticTokenTypes.EnumMember;
public static final String Event = SemanticTokenTypes.Event;
public static final String Function = SemanticTokenTypes.Function;
public static final String Method = SemanticTokenTypes.Method;
public static final String Macro = SemanticTokenTypes.Macro;
public static final String Keyword = SemanticTokenTypes.Keyword;
public static final String Modifier = SemanticTokenTypes.Modifier;
public static final String Comment = SemanticTokenTypes.Comment;
public static final String String = SemanticTokenTypes.String;
public static final String Number = SemanticTokenTypes.Number;
public static final String Regexp = SemanticTokenTypes.Regexp;
public static final String Operator = SemanticTokenTypes.Operator;
public static final String Decorator = SemanticTokenTypes.Decorator;
/** non standard token type */
public static final String ErrorTag = "errorTag";
/** non standard token type */
public static final String Builtin = "builtin";
/** non standard token type */
public static final String Label = "label";
/** non standard token type */
public static final String KeywordLiteral = "keywordLiteral";
private ZLSSemanticTokenTypes() {
}
}

View file

@ -49,62 +49,90 @@ import static com.falsepattern.zigbrains.zig.highlighter.ZigSyntaxHighlighter.VA
import static com.falsepattern.zigbrains.zig.highlighter.ZigSyntaxHighlighter.VARIABLE_DECL_DEPR;
import static com.falsepattern.zigbrains.zig.highlighter.ZigSyntaxHighlighter.VARIABLE_REF;
import static com.falsepattern.zigbrains.zig.highlighter.ZigSyntaxHighlighter.VARIABLE_REF_DEPR;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenModifiers.Declaration;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenModifiers.Definition;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenModifiers.Deprecated;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenModifiers.Documentation;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenModifiers.Generic;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Builtin;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Comment;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Enum;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.EnumMember;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.ErrorTag;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Function;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Keyword;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.KeywordLiteral;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Label;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Method;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Namespace;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Number;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Operator;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Parameter;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Property;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.String;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Struct;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Type;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.TypeParameter;
import static com.falsepattern.zigbrains.zig.lsp.ZLSSemanticTokenTypes.Variable;
public class ZLSSemanticTokensColorsProvider extends DefaultSemanticTokensColorsProvider {
private record TokenHelper(List<String> tokenModifiers) {
public boolean hasAny(String... keys) {
if (tokenModifiers.isEmpty())
if (tokenModifiers.isEmpty()) {
return false;
}
for (val key : keys) {
if (tokenModifiers.contains(key))
if (tokenModifiers.contains(key)) {
return true;
}
}
return false;
}
public boolean has(String... keys) {
if (tokenModifiers.isEmpty())
if (tokenModifiers.isEmpty()) {
return false;
}
for (val key : keys) {
if (!tokenModifiers.contains(key))
if (!tokenModifiers.contains(key)) {
return false;
}
}
return true;
}
public boolean isDecl() {
return hasAny("declaration", "definition");
return hasAny(Declaration, Definition);
}
}
@Override
public @Nullable TextAttributesKey getTextAttributesKey(@NotNull String tokenType,
@NotNull List<String> tokenModifiers,
@NotNull PsiFile file) {
public @Nullable TextAttributesKey getTextAttributesKey(@NotNull String tokenType, @NotNull List<String> tokenModifiers, @NotNull PsiFile file) {
val tok = new TokenHelper(tokenModifiers);
val res = switch (tokenType) {
case "builtin" -> BUILTIN;
case "comment" -> tok.has("documentation") ? COMMENT_DOC : COMMENT;
case "enum" -> tok.isDecl() ? ENUM_DECL : ENUM_REF;
case "enumMember" -> tok.isDecl() ? ENUM_MEMBER_DECL : ENUM_MEMBER_REF;
case "errorTag" -> tok.isDecl() ? ERROR_TAG_DECL : ERROR_TAG_REF;
case "property" -> tok.isDecl() ? PROPERTY_DECL : PROPERTY_REF;
case "function" -> tok.isDecl() ? (tok.has("generic") ? FUNCTION_DECL_GEN : FUNCTION_DECL)
: (tok.has("generic") ? FUNCTION_REF_GEN : FUNCTION_REF);
case "keyword", "keywordLiteral" -> KEYWORD;
case "label" -> tok.isDecl() ? LABEL_DECL : LABEL_REF;
case "method" -> tok.isDecl() ? (tok.has("generic") ? METHOD_DECL_GEN : METHOD_DECL)
: (tok.has("generic") ? METHOD_REF_GEN : METHOD_REF);
case "namespace" -> tok.isDecl() ? NAMESPACE_DECL : NAMESPACE_REF;
case "number" -> NUMBER;
case "operator" -> OPERATOR;
case "parameter" -> PARAMETER;
case "string" -> STRING;
case "struct" -> tok.isDecl() ? STRUCT_DECL : STRUCT_REF;
case "type" -> tok.isDecl() ? (tok.has("generic") ? TYPE_DECL_GEN : TYPE_DECL)
: (tok.has("generic") ? TYPE_REF_GEN : TYPE_REF);
case "typeParameter" -> tok.isDecl() ? TYPE_PARAM_DECL : TYPE_PARAM;
case "variable" -> tok.isDecl() ? (tok.has("deprecated") ? VARIABLE_DECL_DEPR : VARIABLE_DECL)
: (tok.has("deprecated") ? VARIABLE_REF_DEPR : VARIABLE_REF);
case Builtin -> BUILTIN;
case Comment -> tok.has(Documentation) ? COMMENT_DOC : COMMENT;
case Enum -> tok.isDecl() ? ENUM_DECL : ENUM_REF;
case EnumMember -> tok.isDecl() ? ENUM_MEMBER_DECL : ENUM_MEMBER_REF;
case ErrorTag -> tok.isDecl() ? ERROR_TAG_DECL : ERROR_TAG_REF;
case Property -> tok.isDecl() ? PROPERTY_DECL : PROPERTY_REF;
case Function -> tok.isDecl() ? (tok.has(Generic) ? FUNCTION_DECL_GEN : FUNCTION_DECL)
: (tok.has(Generic) ? FUNCTION_REF_GEN : FUNCTION_REF);
case Keyword, KeywordLiteral -> KEYWORD;
case Label -> tok.isDecl() ? LABEL_DECL : LABEL_REF;
case Method -> tok.isDecl() ? (tok.has(Generic) ? METHOD_DECL_GEN : METHOD_DECL)
: (tok.has(Generic) ? METHOD_REF_GEN : METHOD_REF);
case Namespace -> tok.isDecl() ? NAMESPACE_DECL : NAMESPACE_REF;
case Number -> NUMBER;
case Operator -> OPERATOR;
case Parameter -> PARAMETER;
case String -> STRING;
case Struct -> tok.isDecl() ? STRUCT_DECL : STRUCT_REF;
case Type -> tok.isDecl() ? (tok.has(Generic) ? TYPE_DECL_GEN : TYPE_DECL)
: (tok.has(Generic) ? TYPE_REF_GEN : TYPE_REF);
case TypeParameter -> tok.isDecl() ? TYPE_PARAM_DECL : TYPE_PARAM;
case Variable -> tok.isDecl() ? (tok.has(Deprecated) ? VARIABLE_DECL_DEPR : VARIABLE_DECL)
: (tok.has(Deprecated) ? VARIABLE_REF_DEPR : VARIABLE_REF);
default -> null;
};
return res != null ? res : super.getTextAttributesKey(tokenType, tokenModifiers, file);