implements PLN-0027 frontend semantic presentation contract and lsp descriptor

This commit is contained in:
2026-04-02 15:11:40 +01:00
parent 78758c1023
commit de9782c16e
14 changed files with 253 additions and 32 deletions
@@ -16,6 +16,7 @@ class LspSemanticAnalyseService {
final var normalizedRequestedDocument = normalize(request.documentPath());
return new LspAnalyzeDocumentResult(
new LspSessionStateDTO(true, List.of("diagnostics", "symbols", "definition", "highlight")),
session.semanticPresentation(),
session.diagnosticsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.semanticHighlightsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.documentSymbolsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
@@ -5,6 +5,7 @@ import p.studio.compiler.messages.*;
import p.studio.compiler.models.AnalysisSnapshot;
import p.studio.compiler.models.BuilderPipelineContext;
import p.studio.compiler.models.FrontendSpec;
import p.studio.compiler.models.FrontendSemanticPresentationSpec;
import p.studio.compiler.models.SourceHandle;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
@@ -18,6 +19,7 @@ import p.studio.compiler.workspaces.stages.LoadSourcesPipelineStage;
import p.studio.compiler.workspaces.stages.ResolveDepsPipelineStage;
import p.studio.lsp.dtos.LspDiagnosticDTO;
import p.studio.lsp.dtos.LspRangeDTO;
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
import p.studio.lsp.messages.*;
import p.studio.lsp.models.AnalysisRuntimeSnapshot;
import p.studio.lsp.models.SemanticIndex;
@@ -137,6 +139,7 @@ final class LspSemanticReadPhase {
if (snapshot.fileTable() == null) {
return new SemanticSession(
normalize(requestedDocumentPath),
semanticPresentation(snapshot.frontendSpec()),
diagnosticsByDocument,
Map.of(),
Map.of(),
@@ -157,6 +160,7 @@ final class LspSemanticReadPhase {
}
return new SemanticSession(
normalize(requestedDocumentPath),
semanticPresentation(snapshot.frontendSpec()),
diagnosticsByDocument,
semanticIndex.semanticHighlightsByDocument(),
semanticIndex.documentSymbolsByDocument(),
@@ -169,6 +173,11 @@ final class LspSemanticReadPhase {
return frontendSpec.getAllowedExtensions().contains(sourceHandle.getExtension());
}
private static LspSemanticPresentationDTO semanticPresentation(final FrontendSpec frontendSpec) {
final FrontendSemanticPresentationSpec presentation = frontendSpec.getSemanticPresentation();
return new LspSemanticPresentationDTO(presentation.semanticKeys(), presentation.resources());
}
private static Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument(
final List<BuildingIssue> issues,
final AnalysisSnapshot snapshot,
@@ -3,6 +3,7 @@ package p.studio.lsp.messages;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.lsp.dtos.LspDiagnosticDTO;
import p.studio.lsp.dtos.LspHighlightSpanDTO;
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import java.nio.file.Path;
@@ -11,6 +12,7 @@ import java.util.Map;
public record SemanticSession(
Path requestedDocumentPath,
LspSemanticPresentationDTO semanticPresentation,
Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument,
Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument,
Map<Path, List<LspSymbolDTO>> documentSymbolsByDocument,
@@ -1,5 +1,6 @@
package p.studio.lsp.models;
import p.studio.compiler.pbs.PbsSemanticKind;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.pbs.lexer.PbsTokenKind;
@@ -71,39 +72,25 @@ public final class SemanticIndex {
private String semanticKey(
final PbsToken token,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
return switch (token.kind()) {
case COMMENT -> "fe-comment";
case STRING_LITERAL -> "fe-string";
case INT_LITERAL, FLOAT_LITERAL, BOUNDED_LITERAL -> "fe-number";
case TRUE, FALSE, NONE -> "fe-literal";
case IDENTIFIER -> semanticKeyForIdentifier(token.lexeme(), indexedSymbolsByName);
case IMPORT, FROM, AS, SERVICE, HOST, FN, APPLY, BIND, NEW, IMPLEMENTS, USING, CTOR,
DECLARE, LET, CONST, GLOBAL, STRUCT, CONTRACT, ERROR, ENUM, CALLBACK, BUILTIN,
SELF, THIS, PUB, MUT, MOD, TYPE, IF, ELSE, SWITCH, DEFAULT, FOR, UNTIL, STEP,
WHILE, BREAK, CONTINUE, RETURN, VOID, OPTIONAL, RESULT, SOME, OK, ERR, HANDLE,
AND, OR, NOT -> "fe-keyword";
case PLUS, MINUS, STAR, SLASH, PERCENT, BANG, PLUS_EQUAL, MINUS_EQUAL, STAR_EQUAL,
SLASH_EQUAL, PERCENT_EQUAL, BANG_EQUAL, EQUAL, EQUAL_EQUAL, LESS, LESS_EQUAL,
GREATER, GREATER_EQUAL, AND_AND, OR_OR, ARROW, QUESTION -> "fe-operator";
case LEFT_PAREN, RIGHT_PAREN, LEFT_BRACE, RIGHT_BRACE, LEFT_BRACKET, RIGHT_BRACKET,
COMMA, COLON, SEMICOLON, AT, DOT, DOT_DOT -> "fe-punctuation";
default -> null;
};
final PbsSemanticKind semanticKind = token.kind() == p.studio.compiler.pbs.lexer.PbsTokenKind.IDENTIFIER
? semanticKindForIdentifier(token.lexeme(), indexedSymbolsByName)
: PbsSemanticKind.forToken(token);
return semanticKind == null ? null : semanticKind.semanticKey();
}
private String semanticKeyForIdentifier(
private PbsSemanticKind semanticKindForIdentifier(
final String lexeme,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final List<LspSymbolDTO> symbols = indexedSymbolsByName.getOrDefault(lexeme, List.of());
if (symbols.isEmpty()) {
return "fe-identifier";
return PbsSemanticKind.IDENTIFIER;
}
final LspSymbolKind kind = symbols.getFirst().kind();
return switch (kind) {
case FUNCTION, METHOD, CALLBACK -> "fe-callable";
case STRUCT, CONTRACT, HOST, BUILTIN_TYPE, SERVICE, ERROR, ENUM -> "fe-type";
case GLOBAL, CONST -> "fe-binding";
default -> "fe-identifier";
case FUNCTION, METHOD, CALLBACK -> PbsSemanticKind.FUNCTION;
case STRUCT, CONTRACT, HOST, BUILTIN_TYPE, SERVICE, ERROR, ENUM -> PbsSemanticKind.TYPE;
case GLOBAL, CONST -> PbsSemanticKind.BINDING;
default -> PbsSemanticKind.IDENTIFIER;
};
}