refactor, rename and reducing complexity

This commit is contained in:
2026-04-02 11:55:09 +01:00
parent b64be2a9a1
commit ba55b8e65f
120 changed files with 1134 additions and 1025 deletions
@@ -0,0 +1,24 @@
package p.studio.lsp;
import p.studio.lsp.dtos.LspSessionStateDTO;
import p.studio.lsp.messages.LspAnalyzeDocumentRequest;
import p.studio.lsp.messages.LspAnalyzeDocumentResult;
import p.studio.lsp.messages.SemanticSession;
import java.util.List;
import static p.studio.lsp.LspSemanticUtilities.normalize;
class LspSemanticAnalyseService {
LspAnalyzeDocumentResult analyze(
final SemanticSession session,
final LspAnalyzeDocumentRequest request) {
final var normalizedRequestedDocument = normalize(request.documentPath());
return new LspAnalyzeDocumentResult(
new LspSessionStateDTO(true, List.of("diagnostics", "symbols", "definition", "highlight")),
session.diagnosticsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.semanticHighlightsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.documentSymbolsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.workspaceSymbols());
}
}
@@ -0,0 +1,46 @@
package p.studio.lsp;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.pbs.lexer.PbsTokenKind;
import p.studio.lsp.dtos.LspDefinitionTargetDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import p.studio.lsp.messages.LspDefinitionRequest;
import p.studio.lsp.messages.LspDefinitionResult;
import p.studio.lsp.messages.SemanticSession;
import java.nio.file.Path;
import java.util.List;
import static p.studio.lsp.LspSemanticUtilities.normalize;
import static p.studio.lsp.LspSemanticUtilities.tokenAt;
class LspSemanticDefinitionService {
LspDefinitionResult definition(
final SemanticSession session,
final LspDefinitionRequest request) {
final List<LspDefinitionTargetDTO> targets = resolveDefinitionTargets(session, request);
return new LspDefinitionResult(request.documentPath(), request.offset(), targets);
}
private List<LspDefinitionTargetDTO> resolveDefinitionTargets(
final SemanticSession session,
final LspDefinitionRequest request) {
final Path documentPath = normalize(request.documentPath());
final List<PbsToken> tokens = session.tokensByDocument().getOrDefault(documentPath, List.of());
final PbsToken activeToken = tokenAt(tokens, request.offset());
if (activeToken == null || activeToken.kind() != PbsTokenKind.IDENTIFIER) {
return List.of();
}
final String lexeme = activeToken.lexeme();
final List<LspSymbolDTO> sameDocumentMatches = session.symbolsByName().getOrDefault(lexeme, List.of()).stream()
.filter(symbol -> symbol.documentPath().equals(documentPath))
.toList();
final List<LspSymbolDTO> effectiveMatches = sameDocumentMatches.isEmpty()
? session.symbolsByName().getOrDefault(lexeme, List.of())
: sameDocumentMatches;
return effectiveMatches.stream()
.map(symbol -> new LspDefinitionTargetDTO(symbol.name(), symbol.documentPath(), symbol.range()))
.toList();
}
}
@@ -0,0 +1,248 @@
package p.studio.lsp;
import p.studio.compiler.FrontendRegistryService;
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.SourceHandle;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsLexer;
import p.studio.compiler.pbs.parser.PbsParser;
import p.studio.compiler.source.diagnostics.Diagnostic;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.compiler.source.identifiers.FileId;
import p.studio.compiler.workspaces.AssetSurfaceContextLoader;
import p.studio.compiler.workspaces.PipelineStage;
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.messages.*;
import p.studio.lsp.models.AnalysisRuntimeSnapshot;
import p.studio.lsp.models.SemanticIndex;
import p.studio.utilities.logs.LogAggregator;
import p.studio.vfs.VfsProjectDocument;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static p.studio.lsp.LspSemanticUtilities.*;
final class LspSemanticReadPhase {
private final LspSemanticAnalyseService analyseService;
private final LspSemanticDefinitionService definitionService;
LspSemanticReadPhase() {
this.analyseService = new LspSemanticAnalyseService();
this.definitionService = new LspSemanticDefinitionService();
}
public LspAnalyzeDocumentResult analyze(
final LspProjectContext projectContext,
final VfsProjectDocument vfsProjectDocument,
final LspAnalyzeDocumentRequest request) {
final var session = buildSession(projectContext, vfsProjectDocument, request.documentPath());
return analyseService.analyze(session, request);
}
public LspDefinitionResult definition(
final LspProjectContext projectContext,
final VfsProjectDocument vfsProjectDocument,
final LspDefinitionRequest request) {
final var session = buildSession(projectContext, vfsProjectDocument, request.documentPath());
return definitionService.definition(session, request);
}
public static SemanticSession buildSession(
final LspProjectContext projectContext,
final VfsProjectDocument vfsProjectDocument,
final Path requestedDocumentPath) {
final BuilderPipelineConfig config = new BuilderPipelineConfig(
false,
projectContext.rootPath().toString(),
"core-v1",
new OverlaySourceProviderFactoryImpl(vfsProjectDocument, requestedDocumentPath));
final BuilderPipelineContext context = BuilderPipelineContext.fromConfig(config);
final AnalysisRuntimeSnapshot snapshot = runAnalysisStages(context);
return index(snapshot, requestedDocumentPath);
}
private static AnalysisRuntimeSnapshot runAnalysisStages(final BuilderPipelineContext context) {
final LogAggregator logs = LogAggregator.empty();
final List<BuildingIssue> diagnostics = new ArrayList<>();
final List<PipelineStage> stages = List.of(
new ResolveDepsPipelineStage(),
new LoadSourcesPipelineStage());
for (final PipelineStage stage : stages) {
final BuildingIssueSink stageIssues = stage.run(context, logs);
diagnostics.addAll(stageIssues.asCollection());
if (stageIssues.hasErrors()) {
break;
}
}
final DiagnosticSink frontendDiagnostics = DiagnosticSink.empty();
FrontendSpec frontendSpec = FrontendRegistryService.getDefaultFrontendSpec();
if (context.resolvedWorkspace != null && context.fileTable != null) {
final BuildingIssueSink frontendIssues = runFrontendPhase(context, logs, frontendDiagnostics);
diagnostics.addAll(frontendIssues.asCollection());
frontendSpec = context.resolvedWorkspace.frontendSpec();
}
return new AnalysisRuntimeSnapshot(
new AnalysisSnapshot(
frontendSpec,
diagnostics,
context.resolvedWorkspace,
context.fileTable,
context.irBackend),
List.copyOf(frontendDiagnostics.asCollection()));
}
private static BuildingIssueSink runFrontendPhase(
final BuilderPipelineContext context,
final LogAggregator logs,
final DiagnosticSink frontendDiagnostics) {
final var frontendSpec = context.resolvedWorkspace.frontendSpec();
final var service = FrontendRegistryService.getFrontendPhaseService(frontendSpec.getLanguageId());
if (service.isEmpty()) {
return BuildingIssueSink.empty().report(builder -> builder
.error(true)
.message("[BUILD]: unable to find a service for frontend phase: " + frontendSpec.getLanguageId()));
}
final FESurfaceContext feSurfaceContext = new AssetSurfaceContextLoader().load(context.resolvedWorkspace.mainProject().getRootPath());
final FrontendPhaseContext frontendPhaseContext = new FrontendPhaseContext(
context.resolvedWorkspace.graph().projectTable(),
context.fileTable,
context.resolvedWorkspace.stack(),
context.resolvedWorkspace.stdlib(),
HostAdmissionContext.permissiveDefault(),
feSurfaceContext);
final BuildingIssueSink issues = BuildingIssueSink.empty();
context.irBackend = service.get().compile(frontendPhaseContext, frontendDiagnostics, logs, issues);
return issues;
}
private static SemanticSession index(
final AnalysisRuntimeSnapshot runtimeSnapshot,
final Path requestedDocumentPath) {
final AnalysisSnapshot snapshot = runtimeSnapshot.analysisSnapshot();
final Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument = diagnosticsByDocument(
snapshot.diagnostics(),
snapshot,
runtimeSnapshot.frontendDiagnostics());
final SemanticIndex semanticIndex = new SemanticIndex();
if (snapshot.fileTable() == null) {
return new SemanticSession(
normalize(requestedDocumentPath),
diagnosticsByDocument,
Map.of(),
Map.of(),
List.of(),
Map.of(),
Map.of());
}
for (final FileId fileId : snapshot.fileTable()) {
final SourceHandle sourceHandle = snapshot.fileTable().get(fileId);
if (!isSourceRelated(snapshot.frontendSpec(), sourceHandle)) {
continue;
}
final String source = sourceHandle.readUtf8().orElse("");
final DiagnosticSink diagnostics = DiagnosticSink.empty();
final var tokens = PbsLexer.lex(source, fileId, diagnostics);
final PbsAst.File ast = PbsParser.parse(tokens, fileId, diagnostics, PbsParser.ParseMode.ORDINARY);
semanticIndex.index(sourceHandle.getCanonPath(), ast, tokens.asList());
}
return new SemanticSession(
normalize(requestedDocumentPath),
diagnosticsByDocument,
semanticIndex.semanticHighlightsByDocument(),
semanticIndex.documentSymbolsByDocument(),
semanticIndex.workspaceSymbols(),
semanticIndex.symbolsByName(),
semanticIndex.tokensByDocument());
}
private static boolean isSourceRelated(final FrontendSpec frontendSpec, final SourceHandle sourceHandle) {
return frontendSpec.getAllowedExtensions().contains(sourceHandle.getExtension());
}
private static Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument(
final List<BuildingIssue> issues,
final AnalysisSnapshot snapshot,
final List<Diagnostic> frontendDiagnostics) {
final Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument = new LinkedHashMap<>();
diagnosticIssues(issues, snapshot, diagnosticsByDocument);
diagnosticFrontend(frontendDiagnostics, snapshot, diagnosticsByDocument);
return freezeMapOfLists(diagnosticsByDocument);
}
private static boolean isValidIssueDiagnostic(
final AnalysisSnapshot snapshot,
final BuildingIssue issue) {
if (snapshot.fileTable() == null || issue.getFileId() == null || issue.getFileId() < 0) {
return false;
}
return issue.getFileId() < snapshot.fileTable().size();
}
private static void diagnosticIssues(
final List<BuildingIssue> issues,
final AnalysisSnapshot snapshot,
final Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument) {
for (final var issue : issues) {
if (!isValidIssueDiagnostic(snapshot, issue)) {
continue;
}
final SourceHandle sourceHandle = snapshot.fileTable().get(new FileId(issue.getFileId()));
final Path documentPath = sourceHandle.getCanonPath().toAbsolutePath().normalize();
diagnosticsByDocument
.computeIfAbsent(documentPath, ignored -> new ArrayList<>())
.add(new LspDiagnosticDTO(
documentPath,
new LspRangeDTO(
safeOffset(issue.getStart()),
safeEnd(issue.getStart(), issue.getEnd())),
issue.isError()
? LspDiagnosticSeverity.ERROR
: LspDiagnosticSeverity.WARNING,
issue.getPhase(),
issue.getCode(),
issue.getMessage()));
}
}
private static boolean isValidFEDiagnostic(
final AnalysisSnapshot snapshot,
final Diagnostic diagnostic) {
return snapshot.fileTable() != null && !diagnostic.getSpan().getFileId().isNone();
}
private static void diagnosticFrontend(
final List<Diagnostic> feDiagnostics,
final AnalysisSnapshot snapshot,
final Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument) {
for (final var feDiagnostic : feDiagnostics) {
if (!isValidFEDiagnostic(snapshot, feDiagnostic)) {
continue;
}
final SourceHandle sourceHandle = snapshot.fileTable().get(feDiagnostic.getSpan().getFileId());
final Path documentPath = sourceHandle.getCanonPath().toAbsolutePath().normalize();
diagnosticsByDocument
.computeIfAbsent(documentPath, ignored -> new ArrayList<>())
.add(new LspDiagnosticDTO(
documentPath,
new LspRangeDTO(
(int) feDiagnostic.getSpan().getStart(),
(int) feDiagnostic.getSpan().getEnd()),
feDiagnostic.getSeverity().isError()
? LspDiagnosticSeverity.ERROR
: LspDiagnosticSeverity.WARNING,
feDiagnostic.getPhase().name(),
feDiagnostic.getCode(),
feDiagnostic.getMessage()));
}
}
}
@@ -0,0 +1,52 @@
package p.studio.lsp;
import lombok.experimental.UtilityClass;
import p.studio.compiler.pbs.lexer.PbsToken;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@UtilityClass
public class LspSemanticUtilities {
public static PbsToken tokenAt(final List<PbsToken> tokens, final int offset) {
for (final PbsToken token : tokens) {
if (token.start() <= offset && offset < token.end()) {
return token;
}
}
return null;
}
public static Path normalize(final Path path) {
final Path normalized = Objects.requireNonNull(path, "path").toAbsolutePath().normalize();
try {
return Files.exists(normalized) ? normalized.toRealPath() : normalized;
} catch (IOException ignored) {
return normalized;
}
}
public static <T> Map<Path, List<T>> freezeMapOfLists(final Map<Path, List<T>> mutable) {
final Map<Path, List<T>> frozen = new LinkedHashMap<>();
for (final var entry : mutable.entrySet()) {
frozen.put(entry.getKey(), List.copyOf(entry.getValue()));
}
return Map.copyOf(frozen);
}
public static int safeOffset(final Integer value) {
return value == null ? 0 : Math.max(0, value);
}
public static int safeEnd(final Integer start, final Integer end) {
final int safeStart = safeOffset(start);
final int safeEnd = safeOffset(end);
return Math.max(safeStart, safeEnd);
}
}
@@ -0,0 +1,17 @@
package p.studio.lsp;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.vfs.VfsProjectDocument;
import java.util.Objects;
public final class LspServiceFactoryImpl implements LspServiceFactory {
@Override
public LspService open(
final LspProjectContext projectContext,
final VfsProjectDocument vfsProjectDocument) {
return new LspServiceImpl(
Objects.requireNonNull(projectContext, "projectContext"),
Objects.requireNonNull(vfsProjectDocument, "vfsProjectDocument"));
}
}
@@ -0,0 +1,50 @@
package p.studio.lsp;
import p.studio.lsp.dtos.LspSessionStateDTO;
import p.studio.lsp.messages.*;
import p.studio.vfs.VfsProjectDocument;
import java.util.List;
import java.util.Objects;
public final class LspServiceImpl implements LspService {
private final LspProjectContext projectContext;
private final VfsProjectDocument vfsProjectDocument;
private final LspSemanticReadPhase semanticReadPhase;
public LspServiceImpl(
final LspProjectContext projectContext,
final VfsProjectDocument vfsProjectDocument) {
this.projectContext = Objects.requireNonNull(projectContext, "projectContext");
this.vfsProjectDocument = Objects.requireNonNull(vfsProjectDocument, "vfsProjectDocument");
this.semanticReadPhase = new LspSemanticReadPhase();
}
@Override
public LspProjectContext projectContext() {
return projectContext;
}
@Override
public VfsProjectDocument projectDocumentVfs() {
return vfsProjectDocument;
}
@Override
public LspSessionStateDTO snapshot() {
return new LspSessionStateDTO(
true,
List.of("diagnostics", "symbols", "definition", "highlight"));
}
@Override
public LspAnalyzeDocumentResult analyzeDocument(final LspAnalyzeDocumentRequest request) {
return semanticReadPhase.analyze(projectContext, vfsProjectDocument, request);
}
@Override
public LspDefinitionResult definition(final LspDefinitionRequest request) {
return semanticReadPhase.definition(projectContext, vfsProjectDocument, request);
}
}
@@ -0,0 +1,51 @@
package p.studio.lsp;
import p.studio.compiler.utilities.SourceProvider;
import p.studio.compiler.utilities.SourceProviderFactory;
import p.studio.vfs.VfsDocumentOpenResult;
import p.studio.vfs.VfsProjectDocument;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;
final class OverlaySourceProviderFactoryImpl implements SourceProviderFactory {
private final VfsProjectDocument vfsProjectDocument;
private final Path openedDocumentPath;
OverlaySourceProviderFactoryImpl(
final VfsProjectDocument vfsProjectDocument,
final Path openedDocumentPath) {
this.vfsProjectDocument = Objects.requireNonNull(vfsProjectDocument, "vfsProjectDocument");
this.openedDocumentPath = openedDocumentPath == null
? null
: normalize(openedDocumentPath);
}
@Override
public SourceProvider create(final Path path) {
final Path normalizedPath = normalize(path);
return () -> read(normalizedPath);
}
private byte[] read(final Path path) throws IOException {
if (openedDocumentPath != null && openedDocumentPath.equals(path)) {
final VfsDocumentOpenResult result = vfsProjectDocument.openDocument(path);
if (result instanceof VfsDocumentOpenResult.VfsTextDocument textDocument) {
return textDocument.content().getBytes(StandardCharsets.UTF_8);
}
}
return Files.readAllBytes(path);
}
private Path normalize(final Path path) {
final Path normalized = path.toAbsolutePath().normalize();
try {
return Files.exists(normalized) ? normalized.toRealPath() : normalized;
} catch (IOException ignored) {
return normalized;
}
}
}
@@ -0,0 +1,20 @@
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.LspSymbolDTO;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
public record SemanticSession(
Path requestedDocumentPath,
Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument,
Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument,
Map<Path, List<LspSymbolDTO>> documentSymbolsByDocument,
List<LspSymbolDTO> workspaceSymbols,
Map<String, List<LspSymbolDTO>> symbolsByName,
Map<Path, List<PbsToken>> tokensByDocument) {
}
@@ -0,0 +1,11 @@
package p.studio.lsp.models;
import p.studio.compiler.models.AnalysisSnapshot;
import p.studio.compiler.source.diagnostics.Diagnostic;
import java.util.List;
public record AnalysisRuntimeSnapshot(
AnalysisSnapshot analysisSnapshot,
List<Diagnostic> frontendDiagnostics) {
}
@@ -0,0 +1,225 @@
package p.studio.lsp.models;
import p.studio.compiler.pbs.ast.PbsAst;
import p.studio.compiler.pbs.lexer.PbsToken;
import p.studio.compiler.pbs.lexer.PbsTokenKind;
import p.studio.compiler.source.Span;
import p.studio.lsp.dtos.LspHighlightSpanDTO;
import p.studio.lsp.dtos.LspRangeDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import p.studio.lsp.messages.LspSymbolKind;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import static p.studio.lsp.LspSemanticUtilities.normalize;
public final class SemanticIndex {
private final Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument = new LinkedHashMap<>();
private final Map<Path, List<LspSymbolDTO>> documentSymbolsByDocument = new LinkedHashMap<>();
private final List<LspSymbolDTO> workspaceSymbols = new ArrayList<>();
private final Map<String, List<LspSymbolDTO>> symbolsByName = new LinkedHashMap<>();
private final Map<Path, List<PbsToken>> tokensByDocument = new LinkedHashMap<>();
public void index(
final Path documentPath,
final PbsAst.File ast,
final List<PbsToken> tokens) {
final Path normalizedDocumentPath = normalize(documentPath);
tokensByDocument.put(normalizedDocumentPath, List.copyOf(tokens));
final List<LspSymbolDTO> documentSymbols = new ArrayList<>();
for (final PbsAst.TopDecl topDecl : ast.topDecls()) {
final LspSymbolDTO symbol = symbolForTopDecl(normalizedDocumentPath, topDecl);
if (symbol == null) {
continue;
}
documentSymbols.add(symbol);
workspaceSymbols.add(symbol);
symbolsByName.computeIfAbsent(symbol.name(), ignored -> new ArrayList<>()).add(symbol);
for (final LspSymbolDTO child : symbol.children()) {
symbolsByName.computeIfAbsent(child.name(), ignored -> new ArrayList<>()).add(child);
}
}
semanticHighlightsByDocument.put(
normalizedDocumentPath,
buildSemanticHighlights(tokens, symbolsByName));
documentSymbolsByDocument.put(normalizedDocumentPath, List.copyOf(documentSymbols));
}
private List<LspHighlightSpanDTO> buildSemanticHighlights(
final List<PbsToken> tokens,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final List<LspHighlightSpanDTO> highlights = new ArrayList<>();
for (final PbsToken token : tokens) {
if (token.kind() == PbsTokenKind.EOF) {
continue;
}
final String semanticKey = semanticKey(token, indexedSymbolsByName);
if (semanticKey == null || semanticKey.isBlank()) {
continue;
}
highlights.add(new LspHighlightSpanDTO(
new LspRangeDTO(token.start(), token.end()),
semanticKey));
}
return List.copyOf(highlights);
}
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;
};
}
private String semanticKeyForIdentifier(
final String lexeme,
final Map<String, List<LspSymbolDTO>> indexedSymbolsByName) {
final List<LspSymbolDTO> symbols = indexedSymbolsByName.getOrDefault(lexeme, List.of());
if (symbols.isEmpty()) {
return "fe-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";
};
}
private LspSymbolDTO symbolForTopDecl(
final Path documentPath,
final PbsAst.TopDecl topDecl) {
if (topDecl instanceof PbsAst.FunctionDecl functionDecl) {
return symbol(documentPath, functionDecl.name(), LspSymbolKind.FUNCTION, functionDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.StructDecl structDecl) {
return symbol(documentPath, structDecl.name(), LspSymbolKind.STRUCT, structDecl.span(), structChildren(documentPath, structDecl));
}
if (topDecl instanceof PbsAst.ContractDecl(
String name, p.studio.utilities.structures.ReadOnlyList<PbsAst.FunctionSignature> signatures, Span span
)) {
return symbol(documentPath, name, LspSymbolKind.CONTRACT, span, signatureChildren(documentPath, signatures.asList()));
}
if (topDecl instanceof PbsAst.HostDecl(
String name, p.studio.utilities.structures.ReadOnlyList<PbsAst.FunctionSignature> signatures, Span span
)) {
return symbol(documentPath, name, LspSymbolKind.HOST, span, signatureChildren(documentPath, signatures.asList()));
}
if (topDecl instanceof PbsAst.BuiltinTypeDecl builtinTypeDecl) {
return symbol(documentPath, builtinTypeDecl.name(), LspSymbolKind.BUILTIN_TYPE, builtinTypeDecl.span(), signatureChildren(documentPath, builtinTypeDecl.signatures().asList()));
}
if (topDecl instanceof PbsAst.ServiceDecl(
String name, p.studio.utilities.structures.ReadOnlyList<PbsAst.FunctionDecl> methods, Span span
)) {
return symbol(documentPath, name, LspSymbolKind.SERVICE, span, functionChildren(documentPath, methods.asList()));
}
if (topDecl instanceof PbsAst.ErrorDecl errorDecl) {
return symbol(documentPath, errorDecl.name(), LspSymbolKind.ERROR, errorDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.EnumDecl enumDecl) {
return symbol(documentPath, enumDecl.name(), LspSymbolKind.ENUM, enumDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.CallbackDecl callbackDecl) {
return symbol(documentPath, callbackDecl.name(), LspSymbolKind.CALLBACK, callbackDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.GlobalDecl globalDecl) {
return symbol(documentPath, globalDecl.name(), LspSymbolKind.GLOBAL, globalDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.ConstDecl constDecl) {
return symbol(documentPath, constDecl.name(), LspSymbolKind.CONST, constDecl.span(), List.of());
}
if (topDecl instanceof PbsAst.ImplementsDecl implementsDecl) {
return symbol(documentPath, implementsDecl.binderName(), LspSymbolKind.IMPLEMENTS, implementsDecl.span(), functionChildren(documentPath, implementsDecl.methods().asList()));
}
return null;
}
private List<LspSymbolDTO> structChildren(
final Path documentPath,
final PbsAst.StructDecl structDecl) {
final List<LspSymbolDTO> children = new ArrayList<>(functionChildren(documentPath, structDecl.methods().asList()));
for (final PbsAst.CtorDecl ctorDecl : structDecl.ctors()) {
children.add(symbol(documentPath, ctorDecl.name(), LspSymbolKind.CONSTRUCTOR, ctorDecl.span(), List.of()));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> functionChildren(
final Path documentPath,
final List<PbsAst.FunctionDecl> functions) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.FunctionDecl functionDecl : functions) {
children.add(symbol(documentPath, functionDecl.name(), LspSymbolKind.METHOD, functionDecl.span(), List.of()));
}
return List.copyOf(children);
}
private List<LspSymbolDTO> signatureChildren(
final Path documentPath,
final List<PbsAst.FunctionSignature> signatures) {
final List<LspSymbolDTO> children = new ArrayList<>();
for (final PbsAst.FunctionSignature signature : signatures) {
children.add(symbol(documentPath, signature.name(), LspSymbolKind.METHOD, signature.span(), List.of()));
}
return List.copyOf(children);
}
private LspSymbolDTO symbol(
final Path documentPath,
final String name,
final LspSymbolKind kind,
final Span span,
final List<LspSymbolDTO> children) {
return new LspSymbolDTO(
name,
kind,
documentPath,
new LspRangeDTO((int) span.getStart(), (int) span.getEnd()),
children);
}
public Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument() {
return Map.copyOf(semanticHighlightsByDocument);
}
public Map<Path, List<LspSymbolDTO>> documentSymbolsByDocument() {
return Map.copyOf(documentSymbolsByDocument);
}
public List<LspSymbolDTO> workspaceSymbols() {
return List.copyOf(workspaceSymbols);
}
public Map<String, List<LspSymbolDTO>> symbolsByName() {
final Map<String, List<LspSymbolDTO>> frozen = new LinkedHashMap<>();
for (final var entry : symbolsByName.entrySet()) {
frozen.put(entry.getKey(), List.copyOf(entry.getValue()));
}
return Map.copyOf(frozen);
}
public Map<Path, List<PbsToken>> tokensByDocument() {
return Map.copyOf(tokensByDocument);
}
}