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
@@ -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;
}
}
@@ -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;
}
}
@@ -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(
@@ -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()))))));
}
}