Frontend Visual Theme Spec and Retirement of Host-Consumed Semantic CSS
This commit is contained in:
+95
-1
@@ -14,6 +14,7 @@ import p.studio.compiler.models.FrontendVisualThemeSpec;
|
||||
import p.studio.compiler.pbs.PbsSemanticKind;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.lexer.PbsToken;
|
||||
import p.studio.compiler.pbs.lexer.PbsTokenKind;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.utilities.SourceProviderFactory;
|
||||
@@ -29,8 +30,11 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
|
||||
public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
|
||||
private static final Set<String> BUILTIN_TYPE_NAMES = Set.of("int", "float", "bool", "string", "i32", "i64", "f32", "f64");
|
||||
|
||||
|
||||
@Override
|
||||
public BaselineServerDescription describeServer(final LspProjectContext context) {
|
||||
@@ -182,7 +186,97 @@ public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
|
||||
if (isLifecycleToken(tokens, index, token)) {
|
||||
return PbsSemanticKind.LIFECYCLE;
|
||||
}
|
||||
return PbsSemanticKind.IDENTIFIER;
|
||||
return classifyIdentifierToken(tokens, index, token);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private PbsSemanticKind classifyIdentifierToken(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final int index,
|
||||
final PbsToken token) {
|
||||
final PbsTokenKind previousKind = previousSignificantKind(tokens, index);
|
||||
final PbsTokenKind nextKind = nextSignificantKind(tokens, index);
|
||||
|
||||
if (previousKind == PbsTokenKind.STRUCT) {
|
||||
return PbsSemanticKind.STRUCT;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.CONTRACT) {
|
||||
return PbsSemanticKind.CONTRACT;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.HOST) {
|
||||
return PbsSemanticKind.HOST;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.SERVICE) {
|
||||
return PbsSemanticKind.SERVICE;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.ENUM || previousKind == PbsTokenKind.ERROR) {
|
||||
return PbsSemanticKind.ENUM;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.CALLBACK) {
|
||||
return PbsSemanticKind.CALLBACK;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.GLOBAL) {
|
||||
return PbsSemanticKind.GLOBAL;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.CONST) {
|
||||
return PbsSemanticKind.CONST;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.CTOR || previousKind == PbsTokenKind.NEW) {
|
||||
return PbsSemanticKind.CONSTRUCTOR;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.BUILTIN) {
|
||||
return PbsSemanticKind.BUILTIN_TYPE;
|
||||
}
|
||||
if (previousKind == PbsTokenKind.DOT && nextKind == PbsTokenKind.LEFT_PAREN) {
|
||||
return PbsSemanticKind.METHOD;
|
||||
}
|
||||
if (nextKind == PbsTokenKind.LEFT_PAREN) {
|
||||
return PbsSemanticKind.FUNCTION;
|
||||
}
|
||||
if (isTypeReferenceContext(previousKind, token.lexeme())) {
|
||||
return PbsSemanticKind.BUILTIN_TYPE;
|
||||
}
|
||||
return PbsSemanticKind.IDENTIFIER;
|
||||
}
|
||||
|
||||
private boolean isTypeReferenceContext(
|
||||
final PbsTokenKind previousKind,
|
||||
final String lexeme) {
|
||||
if (previousKind == null) {
|
||||
return false;
|
||||
}
|
||||
if (previousKind != PbsTokenKind.COLON
|
||||
&& previousKind != PbsTokenKind.ARROW
|
||||
&& previousKind != PbsTokenKind.AS
|
||||
&& previousKind != PbsTokenKind.IMPLEMENTS) {
|
||||
return false;
|
||||
}
|
||||
return BUILTIN_TYPE_NAMES.contains(lexeme);
|
||||
}
|
||||
|
||||
private PbsTokenKind previousSignificantKind(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final int index) {
|
||||
for (int cursor = index - 1; cursor >= 0; cursor -= 1) {
|
||||
final PbsTokenKind kind = tokens.get(cursor).kind();
|
||||
if (kind == PbsTokenKind.COMMENT || kind == PbsTokenKind.EOF) {
|
||||
continue;
|
||||
}
|
||||
return kind;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private PbsTokenKind nextSignificantKind(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final int index) {
|
||||
for (int cursor = index + 1; cursor < tokens.size(); cursor += 1) {
|
||||
final PbsTokenKind kind = tokens.get(cursor).kind();
|
||||
if (kind == PbsTokenKind.COMMENT || kind == PbsTokenKind.EOF) {
|
||||
continue;
|
||||
}
|
||||
return kind;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
+16
-2
@@ -46,14 +46,28 @@ class CompilerLanguageServiceBridgeTest {
|
||||
new LspProjectContext("main", "pbs", Path.of(".")),
|
||||
Path.of("demo.pbs").toUri().toString(),
|
||||
"""
|
||||
struct Vec2 {
|
||||
fn length() -> int {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> void {
|
||||
const answer = 42;
|
||||
const answer: int = 42;
|
||||
let value = new Vec2();
|
||||
value.length();
|
||||
}
|
||||
""");
|
||||
|
||||
assertTrue(semanticTokens.legend().contains("pbs-keyword"));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-keyword")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-number")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-struct")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-function")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-method")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-const")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-builtin-type")));
|
||||
assertTrue(semanticTokens.tokens().stream().anyMatch(token -> token.semanticKey().equals("pbs-constructor")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -74,7 +88,7 @@ class CompilerLanguageServiceBridgeTest {
|
||||
assertEquals("#d9e2ec", description.visualThemes().getFirst().editorPalette().baseForeground());
|
||||
assertTrue(description.visualThemes().getFirst().tokenStyles().stream()
|
||||
.anyMatch(tokenStyle -> tokenStyle.semanticKey().equals("pbs-keyword")
|
||||
&& tokenStyle.foreground().equals("#569cd6")));
|
||||
&& tokenStyle.foreground().equals("#d2a8ff")));
|
||||
}
|
||||
|
||||
private Path findRepoRoot(final Path start) {
|
||||
|
||||
Reference in New Issue
Block a user