implements PLN-0072
This commit is contained in:
+2
-1
@@ -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();
|
||||
}
|
||||
|
||||
+97
@@ -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));
|
||||
}
|
||||
}
|
||||
+17
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
package p.studio.compiler.models;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record FrontendHostProjectionEntrySpec(
|
||||
String semanticKey,
|
||||
String hostTokenType,
|
||||
List<String> hostTokenModifiers,
|
||||
String fallbackTokenType,
|
||||
List<String> fallbackTokenModifiers) {
|
||||
|
||||
public FrontendHostProjectionEntrySpec {
|
||||
semanticKey = requireText(semanticKey, "semanticKey");
|
||||
hostTokenType = requireText(hostTokenType, "hostTokenType");
|
||||
hostTokenModifiers = normalizeList(hostTokenModifiers, "hostTokenModifiers");
|
||||
fallbackTokenType = requireText(fallbackTokenType, "fallbackTokenType");
|
||||
fallbackTokenModifiers = normalizeList(fallbackTokenModifiers, "fallbackTokenModifiers");
|
||||
}
|
||||
|
||||
private static List<String> normalizeList(
|
||||
final List<String> values,
|
||||
final String field) {
|
||||
return List.copyOf(Objects.requireNonNull(values, field).stream()
|
||||
.map(value -> requireText(value, field + " entry"))
|
||||
.toList());
|
||||
}
|
||||
|
||||
private static String requireText(
|
||||
final String value,
|
||||
final String field) {
|
||||
final String candidate = Objects.requireNonNull(value, field).trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException(field + " must not be blank");
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
package p.studio.compiler.models;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public record FrontendHostProjectionSpec(
|
||||
String hostId,
|
||||
List<FrontendHostProjectionEntrySpec> tokenProjections) {
|
||||
|
||||
public FrontendHostProjectionSpec {
|
||||
hostId = requireText(hostId, "hostId");
|
||||
tokenProjections = List.copyOf(Objects.requireNonNull(tokenProjections, "tokenProjections"));
|
||||
if (tokenProjections.isEmpty()) {
|
||||
throw new IllegalArgumentException("tokenProjections must not be empty");
|
||||
}
|
||||
final Map<String, FrontendHostProjectionEntrySpec> bySemanticKey = new LinkedHashMap<>();
|
||||
for (final FrontendHostProjectionEntrySpec tokenProjection : tokenProjections) {
|
||||
final FrontendHostProjectionEntrySpec previous = bySemanticKey.put(tokenProjection.semanticKey(), tokenProjection);
|
||||
if (previous != null) {
|
||||
throw new IllegalArgumentException("duplicate host projection for semantic key: " + tokenProjection.semanticKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<FrontendHostProjectionEntrySpec> projectionFor(final String semanticKey) {
|
||||
return tokenProjections.stream()
|
||||
.filter(tokenProjection -> tokenProjection.semanticKey().equals(semanticKey))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public FrontendHostProjectionEntrySpec requireProjection(final String semanticKey) {
|
||||
return projectionFor(semanticKey)
|
||||
.orElseThrow(() -> new IllegalArgumentException("missing host projection for semantic key: " + semanticKey));
|
||||
}
|
||||
|
||||
private static String requireText(
|
||||
final String value,
|
||||
final String field) {
|
||||
final String candidate = Objects.requireNonNull(value, field).trim();
|
||||
if (candidate.isEmpty()) {
|
||||
throw new IllegalArgumentException(field + " must not be blank");
|
||||
}
|
||||
return candidate;
|
||||
}
|
||||
}
|
||||
+40
-3
@@ -6,12 +6,21 @@ import java.util.Objects;
|
||||
public record FrontendSemanticPresentationSpec(
|
||||
List<String> semanticKeys,
|
||||
List<FrontendVisualThemeSpec> themes,
|
||||
String defaultThemeId) {
|
||||
String defaultThemeId,
|
||||
List<FrontendHostProjectionSpec> hostProjections) {
|
||||
|
||||
public FrontendSemanticPresentationSpec(
|
||||
final List<String> semanticKeys,
|
||||
final List<FrontendVisualThemeSpec> themes,
|
||||
final String defaultThemeId) {
|
||||
this(semanticKeys, themes, defaultThemeId, List.of());
|
||||
}
|
||||
|
||||
public FrontendSemanticPresentationSpec {
|
||||
semanticKeys = List.copyOf(Objects.requireNonNull(semanticKeys, "semanticKeys"));
|
||||
themes = List.copyOf(Objects.requireNonNull(themes, "themes"));
|
||||
defaultThemeId = Objects.requireNonNull(defaultThemeId, "defaultThemeId").trim();
|
||||
hostProjections = List.copyOf(Objects.requireNonNull(hostProjections, "hostProjections"));
|
||||
if (semanticKeys.isEmpty() && themes.isEmpty()) {
|
||||
if (!defaultThemeId.isEmpty()) {
|
||||
throw new IllegalArgumentException("defaultThemeId must be blank when no themes are declared");
|
||||
@@ -25,10 +34,27 @@ public record FrontendSemanticPresentationSpec(
|
||||
throw new IllegalArgumentException("defaultThemeId must reference one of the declared themes");
|
||||
}
|
||||
}
|
||||
final List<String> normalizedSemanticKeys = semanticKeys.stream()
|
||||
.map(key -> requireText(key, "semanticKeys entry"))
|
||||
.toList();
|
||||
final var hostIds = new java.util.LinkedHashSet<String>();
|
||||
for (final FrontendHostProjectionSpec hostProjection : hostProjections) {
|
||||
if (!hostIds.add(hostProjection.hostId())) {
|
||||
throw new IllegalArgumentException("duplicate host projection for hostId: " + hostProjection.hostId());
|
||||
}
|
||||
for (final FrontendHostProjectionEntrySpec tokenProjection : hostProjection.tokenProjections()) {
|
||||
if (!normalizedSemanticKeys.contains(tokenProjection.semanticKey())) {
|
||||
throw new IllegalArgumentException(
|
||||
"host projection for hostId '" + hostProjection.hostId() + "' references unknown semantic key: "
|
||||
+ tokenProjection.semanticKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
semanticKeys = normalizedSemanticKeys;
|
||||
}
|
||||
|
||||
public static FrontendSemanticPresentationSpec empty() {
|
||||
return new FrontendSemanticPresentationSpec(List.of(), List.of(), "");
|
||||
return new FrontendSemanticPresentationSpec(List.of(), List.of(), "", List.of());
|
||||
}
|
||||
|
||||
public FrontendVisualThemeSpec defaultTheme() {
|
||||
@@ -42,7 +68,18 @@ public record FrontendSemanticPresentationSpec(
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return semanticKeys.isEmpty() && themes.isEmpty() && defaultThemeId.isEmpty();
|
||||
return semanticKeys.isEmpty() && themes.isEmpty() && defaultThemeId.isEmpty() && hostProjections.isEmpty();
|
||||
}
|
||||
|
||||
public java.util.Optional<FrontendHostProjectionSpec> hostProjection(final String hostId) {
|
||||
return hostProjections.stream()
|
||||
.filter(hostProjection -> hostProjection.hostId().equals(requireText(hostId, "hostId")))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public FrontendHostProjectionSpec requireHostProjection(final String hostId) {
|
||||
return hostProjection(hostId)
|
||||
.orElseThrow(() -> new IllegalArgumentException("missing host projection for hostId: " + hostId));
|
||||
}
|
||||
|
||||
private static String requireText(
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package p.studio.compiler.models;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
final class FrontendSemanticPresentationSpecTest {
|
||||
|
||||
@Test
|
||||
void shouldExposeHostProjectionByHostId() {
|
||||
final var presentation = new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
new FrontendEditorPaletteSpec("#111111", "#eeeeee", "#333333", "#777777", "#111111", "#222222", "#eeeeee"),
|
||||
List.of(new FrontendTokenStyleSpec("demo-key", "#eeeeee", false, false, false)))),
|
||||
"demo-theme",
|
||||
List.of(new FrontendHostProjectionSpec(
|
||||
"vscode",
|
||||
List.of(new FrontendHostProjectionEntrySpec(
|
||||
"demo-key",
|
||||
"keyword",
|
||||
List.of("declaration"),
|
||||
"variable",
|
||||
List.of())))));
|
||||
|
||||
final var vscodeProjection = presentation.requireHostProjection("vscode").requireProjection("demo-key");
|
||||
assertEquals("keyword", vscodeProjection.hostTokenType());
|
||||
assertEquals(List.of("declaration"), vscodeProjection.hostTokenModifiers());
|
||||
assertEquals("variable", vscodeProjection.fallbackTokenType());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectDuplicateHostIds() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
new FrontendEditorPaletteSpec("#111111", "#eeeeee", "#333333", "#777777", "#111111", "#222222", "#eeeeee"),
|
||||
List.of(new FrontendTokenStyleSpec("demo-key", "#eeeeee", false, false, false)))),
|
||||
"demo-theme",
|
||||
List.of(
|
||||
new FrontendHostProjectionSpec(
|
||||
"vscode",
|
||||
List.of(new FrontendHostProjectionEntrySpec(
|
||||
"demo-key",
|
||||
"keyword",
|
||||
List.of(),
|
||||
"variable",
|
||||
List.of()))),
|
||||
new FrontendHostProjectionSpec(
|
||||
"vscode",
|
||||
List.of(new FrontendHostProjectionEntrySpec(
|
||||
"demo-key",
|
||||
"type",
|
||||
List.of(),
|
||||
"variable",
|
||||
List.of()))))));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRejectProjectionForUnknownSemanticKey() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new FrontendSemanticPresentationSpec(
|
||||
List.of("demo-key"),
|
||||
List.of(new FrontendVisualThemeSpec(
|
||||
"demo-theme",
|
||||
"Demo Theme",
|
||||
new FrontendEditorPaletteSpec("#111111", "#eeeeee", "#333333", "#777777", "#111111", "#222222", "#eeeeee"),
|
||||
List.of(new FrontendTokenStyleSpec("demo-key", "#eeeeee", false, false, false)))),
|
||||
"demo-theme",
|
||||
List.of(new FrontendHostProjectionSpec(
|
||||
"vscode",
|
||||
List.of(new FrontendHostProjectionEntrySpec(
|
||||
"unknown-key",
|
||||
"keyword",
|
||||
List.of(),
|
||||
"variable",
|
||||
List.of()))))));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user