implements PLN-0073

This commit is contained in:
2026-05-06 15:26:24 +01:00
parent 9d75f2f0e2
commit e5ab4cd021
10 changed files with 193 additions and 7 deletions
@@ -0,0 +1,27 @@
package p.studio.lsp.messages;
import java.util.List;
import java.util.Objects;
public record BaselineSemanticHostProjection(
String hostId,
List<BaselineSemanticHostProjectionEntry> tokenProjections) {
public BaselineSemanticHostProjection {
hostId = requireText(hostId, "hostId");
tokenProjections = List.copyOf(Objects.requireNonNull(tokenProjections, "tokenProjections"));
if (tokenProjections.isEmpty()) {
throw new IllegalArgumentException("tokenProjections must not be empty");
}
}
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,38 @@
package p.studio.lsp.messages;
import java.util.List;
import java.util.Objects;
public record BaselineSemanticHostProjectionEntry(
String semanticKey,
String hostTokenType,
List<String> hostTokenModifiers,
String fallbackTokenType,
List<String> fallbackTokenModifiers) {
public BaselineSemanticHostProjectionEntry {
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;
}
}
@@ -9,6 +9,7 @@ public record BaselineServerDescription(
String frontendLanguageId,
boolean hoverSupported,
List<String> semanticTokenTypes,
List<BaselineSemanticHostProjection> semanticHostProjections,
List<BaselineVisualTheme> visualThemes,
String activeVisualThemeId) {
@@ -17,8 +18,15 @@ public record BaselineServerDescription(
version = requireText(version, "version");
frontendLanguageId = requireText(frontendLanguageId, "frontendLanguageId");
semanticTokenTypes = List.copyOf(Objects.requireNonNull(semanticTokenTypes, "semanticTokenTypes"));
semanticHostProjections = List.copyOf(Objects.requireNonNull(semanticHostProjections, "semanticHostProjections"));
visualThemes = List.copyOf(Objects.requireNonNull(visualThemes, "visualThemes"));
activeVisualThemeId = requireText(activeVisualThemeId, "activeVisualThemeId");
final var hostIds = new java.util.LinkedHashSet<String>();
for (final BaselineSemanticHostProjection hostProjection : semanticHostProjections) {
if (!hostIds.add(hostProjection.hostId())) {
throw new IllegalArgumentException("duplicate semantic host projection for hostId: " + hostProjection.hostId());
}
}
if (visualThemes.stream().map(BaselineVisualTheme::themeId).noneMatch(activeVisualThemeId::equals)) {
throw new IllegalArgumentException("activeVisualThemeId must reference one of the declared visual themes");
}
@@ -6,6 +6,8 @@ import p.studio.compiler.messages.BuildingIssue;
import p.studio.compiler.models.AnalysisSnapshot;
import p.studio.compiler.models.BuilderPipelineContext;
import p.studio.compiler.models.FrontendEditorPaletteSpec;
import p.studio.compiler.models.FrontendHostProjectionEntrySpec;
import p.studio.compiler.models.FrontendHostProjectionSpec;
import p.studio.compiler.PBSDefinitions;
import p.studio.compiler.models.FrontendTokenStyleSpec;
import p.studio.compiler.models.FrontendVisualThemeSpec;
@@ -33,14 +35,16 @@ public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
@Override
public BaselineServerDescription describeServer(final LspProjectContext context) {
Objects.requireNonNull(context, "context");
final var presentation = PBSDefinitions.PBS.getSemanticPresentation();
return new BaselineServerDescription(
"Prometeu Studio LSP",
"0.1.0",
context.languageId(),
true,
PBSDefinitions.PBS.getSemanticPresentation().semanticKeys(),
PBSDefinitions.PBS.getSemanticPresentation().themes().stream().map(this::mapVisualTheme).toList(),
PBSDefinitions.PBS.getSemanticPresentation().defaultThemeId());
presentation.semanticKeys(),
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
presentation.themes().stream().map(this::mapVisualTheme).toList(),
presentation.defaultThemeId());
}
@Override
@@ -205,6 +209,21 @@ public record CompilerLanguageServiceBridge() implements LanguageServiceBridge {
theme.tokenStyles().stream().map(this::mapTokenStyle).toList());
}
private BaselineSemanticHostProjection mapSemanticHostProjection(final FrontendHostProjectionSpec hostProjection) {
return new BaselineSemanticHostProjection(
hostProjection.hostId(),
hostProjection.tokenProjections().stream().map(this::mapSemanticHostProjectionEntry).toList());
}
private BaselineSemanticHostProjectionEntry mapSemanticHostProjectionEntry(final FrontendHostProjectionEntrySpec tokenProjection) {
return new BaselineSemanticHostProjectionEntry(
tokenProjection.semanticKey(),
tokenProjection.hostTokenType(),
tokenProjection.hostTokenModifiers(),
tokenProjection.fallbackTokenType(),
tokenProjection.fallbackTokenModifiers());
}
private BaselineEditorPalette mapEditorPalette(final FrontendEditorPaletteSpec palette) {
return new BaselineEditorPalette(
palette.baseForeground(),
@@ -33,7 +33,12 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
Map.of(
"frontendLanguageId", description.frontendLanguageId(),
"activeVisualThemeId", description.activeVisualThemeId(),
"visualThemes", description.visualThemes().stream().map(this::mapVisualThemePayload).toList())));
"visualThemes", description.visualThemes().stream().map(this::mapVisualThemePayload).toList()),
"prometeuSemanticHostProjections",
Map.of(
"frontendLanguageId", description.frontendLanguageId(),
"semanticKeys", description.semanticTokenTypes(),
"hostProjections", description.semanticHostProjections().stream().map(this::mapSemanticHostProjectionPayload).toList())));
final InitializeResult result = new InitializeResult(capabilities);
final ServerInfo serverInfo = new ServerInfo();
@@ -151,4 +156,16 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
"underline", tokenStyle.underline())).toList());
return payload;
}
private Map<String, Object> mapSemanticHostProjectionPayload(final BaselineSemanticHostProjection hostProjection) {
final Map<String, Object> payload = new LinkedHashMap<>();
payload.put("hostId", hostProjection.hostId());
payload.put("tokenProjections", hostProjection.tokenProjections().stream().map(tokenProjection -> Map.of(
"semanticKey", tokenProjection.semanticKey(),
"hostTokenType", tokenProjection.hostTokenType(),
"hostTokenModifiers", tokenProjection.hostTokenModifiers(),
"fallbackTokenType", tokenProjection.fallbackTokenType(),
"fallbackTokenModifiers", tokenProjection.fallbackTokenModifiers())).toList());
return payload;
}
}
@@ -64,6 +64,11 @@ class CompilerLanguageServiceBridgeTest {
assertEquals("pbs", description.frontendLanguageId());
assertEquals("pbs-default", description.activeVisualThemeId());
assertEquals(1, description.semanticHostProjections().size());
assertEquals("vscode", description.semanticHostProjections().getFirst().hostId());
assertTrue(description.semanticHostProjections().getFirst().tokenProjections().stream()
.anyMatch(tokenProjection -> tokenProjection.semanticKey().equals("pbs-keyword")
&& tokenProjection.hostTokenType().equals("keyword")));
assertEquals(1, description.visualThemes().size());
assertEquals("PBS Default", description.visualThemes().getFirst().displayName());
assertEquals("#d9e2ec", description.visualThemes().getFirst().editorPalette().baseForeground());
@@ -14,6 +14,8 @@ import org.junit.jupiter.api.Test;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineEditorPalette;
import p.studio.lsp.messages.BaselineHover;
import p.studio.lsp.messages.BaselineSemanticHostProjection;
import p.studio.lsp.messages.BaselineSemanticHostProjectionEntry;
import p.studio.lsp.messages.BaselineSemanticTokens;
import p.studio.lsp.messages.BaselineServerDescription;
import p.studio.lsp.messages.BaselineTokenStyle;
@@ -89,6 +91,14 @@ class PrometeuLanguageServerTest {
"pbs",
true,
List.of(),
List.of(new BaselineSemanticHostProjection(
"vscode",
List.of(new BaselineSemanticHostProjectionEntry(
"demo-keyword",
"keyword",
List.of(),
"variable",
List.of())))),
List.of(new BaselineVisualTheme(
"demo-default",
"Demo Default",
@@ -0,0 +1,62 @@
package p.studio.lsp.services.protocol.mapping;
import org.eclipse.lsp4j.InitializeResult;
import org.junit.jupiter.api.Test;
import p.studio.lsp.messages.BaselineEditorPalette;
import p.studio.lsp.messages.BaselineSemanticHostProjection;
import p.studio.lsp.messages.BaselineSemanticHostProjectionEntry;
import p.studio.lsp.messages.BaselineServerDescription;
import p.studio.lsp.messages.BaselineTokenStyle;
import p.studio.lsp.messages.BaselineVisualTheme;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
final class Lsp4jProtocolMessageMapperTest {
@Test
void initializeResultPublishesCanonicalKeysAndHostProjectionPayload() {
final var mapper = new Lsp4jProtocolMessageMapper();
final var description = new BaselineServerDescription(
"demo",
"1",
"pbs",
true,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",
List.of(new BaselineSemanticHostProjectionEntry(
"demo-keyword",
"keyword",
List.of("declaration"),
"variable",
List.of())))),
List.of(new BaselineVisualTheme(
"demo-default",
"Demo Default",
new BaselineEditorPalette("#111111", "#222222", "#333333", "#444444", "#555555", "#666666", "#777777"),
List.of(new BaselineTokenStyle("demo-keyword", "#888888", false, false, false)))),
"demo-default");
final InitializeResult result = mapper.mapInitializeResult(description);
final var experimental = assertInstanceOf(Map.class, result.getCapabilities().getExperimental());
final var semanticPayload = assertInstanceOf(Map.class, experimental.get("prometeuSemanticHostProjections"));
assertEquals("pbs", semanticPayload.get("frontendLanguageId"));
assertEquals(List.of("demo-keyword"), semanticPayload.get("semanticKeys"));
final var hostProjectionPayloads = assertInstanceOf(List.class, semanticPayload.get("hostProjections"));
final var firstHostProjection = assertInstanceOf(Map.class, hostProjectionPayloads.getFirst());
assertEquals("vscode", firstHostProjection.get("hostId"));
final var tokenProjectionPayloads = assertInstanceOf(List.class, firstHostProjection.get("tokenProjections"));
final var firstTokenProjection = assertInstanceOf(Map.class, tokenProjectionPayloads.getFirst());
assertEquals("demo-keyword", firstTokenProjection.get("semanticKey"));
assertEquals("keyword", firstTokenProjection.get("hostTokenType"));
assertEquals(List.of("declaration"), firstTokenProjection.get("hostTokenModifiers"));
assertEquals("variable", firstTokenProjection.get("fallbackTokenType"));
}
}