implements PLN-0072

This commit is contained in:
2026-05-06 15:24:21 +01:00
parent cab0e92e60
commit 9d75f2f0e2
9 changed files with 330 additions and 7 deletions
@@ -17,6 +17,7 @@ public class PBSDefinitions {
.semanticPresentation(new FrontendSemanticPresentationSpec(
PbsSemanticKind.semanticKeys(),
List.of(PBSFrontendThemes.DEFAULT_PBS),
"pbs-default"))
"pbs-default",
List.of(PBSFrontendHostProjections.VSCODE)))
.build();
}
@@ -0,0 +1,97 @@
package p.studio.compiler;
import p.studio.compiler.models.FrontendHostProjectionEntrySpec;
import p.studio.compiler.models.FrontendHostProjectionSpec;
import java.util.Arrays;
import static p.studio.compiler.pbs.PbsSemanticKind.BUILTIN_TYPE;
import static p.studio.compiler.pbs.PbsSemanticKind.CALLBACK;
import static p.studio.compiler.pbs.PbsSemanticKind.COMMENT;
import static p.studio.compiler.pbs.PbsSemanticKind.CONST;
import static p.studio.compiler.pbs.PbsSemanticKind.CONSTRUCTOR;
import static p.studio.compiler.pbs.PbsSemanticKind.CONTRACT;
import static p.studio.compiler.pbs.PbsSemanticKind.ENUM;
import static p.studio.compiler.pbs.PbsSemanticKind.ERROR;
import static p.studio.compiler.pbs.PbsSemanticKind.FUNCTION;
import static p.studio.compiler.pbs.PbsSemanticKind.GLOBAL;
import static p.studio.compiler.pbs.PbsSemanticKind.HOST;
import static p.studio.compiler.pbs.PbsSemanticKind.IDENTIFIER;
import static p.studio.compiler.pbs.PbsSemanticKind.IMPLEMENTS;
import static p.studio.compiler.pbs.PbsSemanticKind.KEYWORD;
import static p.studio.compiler.pbs.PbsSemanticKind.LIFECYCLE;
import static p.studio.compiler.pbs.PbsSemanticKind.LITERAL;
import static p.studio.compiler.pbs.PbsSemanticKind.METHOD;
import static p.studio.compiler.pbs.PbsSemanticKind.NUMBER;
import static p.studio.compiler.pbs.PbsSemanticKind.OPERATOR;
import static p.studio.compiler.pbs.PbsSemanticKind.PUNCTUATION;
import static p.studio.compiler.pbs.PbsSemanticKind.SERVICE;
import static p.studio.compiler.pbs.PbsSemanticKind.STRING;
import static p.studio.compiler.pbs.PbsSemanticKind.STRUCT;
public final class PBSFrontendHostProjections {
public static final String VSCODE_HOST_ID = "vscode";
public static final FrontendHostProjectionSpec VSCODE = new FrontendHostProjectionSpec(
VSCODE_HOST_ID,
Arrays.stream(p.studio.compiler.pbs.PbsSemanticKind.values())
.map(PBSFrontendHostProjections::mapVscodeProjection)
.toList());
private PBSFrontendHostProjections() {
}
private static FrontendHostProjectionEntrySpec mapVscodeProjection(final p.studio.compiler.pbs.PbsSemanticKind kind) {
return switch (kind) {
case COMMENT -> projection(COMMENT.semanticKey(), "comment");
case STRING -> projection(STRING.semanticKey(), "string");
case NUMBER -> projection(NUMBER.semanticKey(), "number");
case LITERAL -> projection(LITERAL.semanticKey(), "enumMember", "variable");
case LIFECYCLE -> projection(LIFECYCLE.semanticKey(), "function", "variable", "async");
case KEYWORD -> projection(KEYWORD.semanticKey(), "keyword");
case OPERATOR -> projection(OPERATOR.semanticKey(), "operator", "keyword");
case PUNCTUATION -> projection(PUNCTUATION.semanticKey(), "operator", "keyword");
case FUNCTION -> projection(FUNCTION.semanticKey(), "function");
case METHOD -> projection(METHOD.semanticKey(), "method", "function");
case CONSTRUCTOR -> projection(CONSTRUCTOR.semanticKey(), "class", "type", "declaration");
case STRUCT -> projection(STRUCT.semanticKey(), "struct", "type");
case CONTRACT -> projection(CONTRACT.semanticKey(), "interface", "type");
case HOST -> projection(HOST.semanticKey(), "class", "type", "defaultLibrary");
case BUILTIN_TYPE -> projection(BUILTIN_TYPE.semanticKey(), "type", "variable", "defaultLibrary");
case SERVICE -> projection(SERVICE.semanticKey(), "class", "type", "defaultLibrary");
case ERROR -> projection(ERROR.semanticKey(), "enum", "type");
case ENUM -> projection(ENUM.semanticKey(), "enum", "type");
case CALLBACK -> projection(CALLBACK.semanticKey(), "interface", "type");
case GLOBAL -> projection(GLOBAL.semanticKey(), "variable", "variable", "readonly");
case CONST -> projection(CONST.semanticKey(), "variable", "variable", "readonly");
case IMPLEMENTS -> projection(IMPLEMENTS.semanticKey(), "keyword");
case IDENTIFIER -> projection(IDENTIFIER.semanticKey(), "variable");
};
}
private static FrontendHostProjectionEntrySpec projection(
final String semanticKey,
final String hostTokenType) {
return new FrontendHostProjectionEntrySpec(semanticKey, hostTokenType, java.util.List.of(), hostTokenType, java.util.List.of());
}
private static FrontendHostProjectionEntrySpec projection(
final String semanticKey,
final String hostTokenType,
final String fallbackTokenType) {
return new FrontendHostProjectionEntrySpec(semanticKey, hostTokenType, java.util.List.of(), fallbackTokenType, java.util.List.of());
}
private static FrontendHostProjectionEntrySpec projection(
final String semanticKey,
final String hostTokenType,
final String fallbackTokenType,
final String modifier) {
return new FrontendHostProjectionEntrySpec(
semanticKey,
hostTokenType,
java.util.List.of(modifier),
fallbackTokenType,
java.util.List.of(modifier));
}
}
@@ -1,6 +1,7 @@
package p.studio.compiler.pbs;
import org.junit.jupiter.api.Test;
import p.studio.compiler.PBSFrontendHostProjections;
import p.studio.compiler.PBSDefinitions;
import static org.junit.jupiter.api.Assertions.assertEquals;
@@ -21,6 +22,7 @@ final class PbsSemanticPresentationContractTest {
assertEquals("#d9e2ec", presentation.defaultTheme().editorPalette().baseForeground());
assertEquals("#569cd6", presentation.defaultTheme().requireTokenStyle("pbs-keyword").foreground());
assertTrue(presentation.defaultTheme().requireTokenStyle("pbs-comment").italic());
assertEquals(PBSFrontendHostProjections.VSCODE_HOST_ID, presentation.requireHostProjection("vscode").hostId());
}
@Test
@@ -32,4 +34,19 @@ final class PbsSemanticPresentationContractTest {
PbsSemanticKind.semanticKeys(),
defaultTheme.tokenStyles().stream().map(tokenStyle -> tokenStyle.semanticKey()).toList());
}
@Test
void shouldCoverTheWholePbsSemanticVocabularyInTheVscodeProjection() {
final var presentation = PBSDefinitions.PBS.getSemanticPresentation();
final var vscodeProjection = presentation.requireHostProjection(PBSFrontendHostProjections.VSCODE_HOST_ID);
assertEquals(
PbsSemanticKind.semanticKeys(),
vscodeProjection.tokenProjections().stream().map(tokenProjection -> tokenProjection.semanticKey()).toList());
assertEquals("keyword", vscodeProjection.requireProjection(PbsSemanticKind.KEYWORD.semanticKey()).hostTokenType());
assertEquals("type", vscodeProjection.requireProjection(PbsSemanticKind.STRUCT.semanticKey()).fallbackTokenType());
assertEquals(
java.util.List.of("readonly"),
vscodeProjection.requireProjection(PbsSemanticKind.GLOBAL.semanticKey()).hostTokenModifiers());
}
}