implement PLN-0032 structural anchor payloads and tests

This commit is contained in:
2026-04-03 10:02:48 +01:00
parent 20851c0958
commit d93fe98bcd
13 changed files with 264 additions and 25 deletions
@@ -0,0 +1,18 @@
package p.studio.lsp.dtos;
import java.util.List;
import java.util.Objects;
public record LspStructuralAnchorDTO(
LspRangeDTO range,
LspRangeDTO startAnchor,
LspRangeDTO endAnchor,
List<LspStructuralAnchorDTO> children) {
public LspStructuralAnchorDTO {
range = Objects.requireNonNull(range, "range");
startAnchor = Objects.requireNonNull(startAnchor, "startAnchor");
endAnchor = Objects.requireNonNull(endAnchor, "endAnchor");
children = List.copyOf(Objects.requireNonNull(children, "children"));
}
}
@@ -4,6 +4,7 @@ import p.studio.lsp.dtos.LspDiagnosticDTO;
import p.studio.lsp.dtos.LspHighlightSpanDTO;
import p.studio.lsp.dtos.LspSemanticPresentationDTO;
import p.studio.lsp.dtos.LspSessionStateDTO;
import p.studio.lsp.dtos.LspStructuralAnchorDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import java.util.List;
@@ -15,6 +16,7 @@ public record LspAnalyzeDocumentResult(
List<LspDiagnosticDTO> diagnostics,
List<LspHighlightSpanDTO> semanticHighlights,
List<LspSymbolDTO> documentSymbols,
List<LspStructuralAnchorDTO> structuralAnchors,
List<LspSymbolDTO> workspaceSymbols) {
public LspAnalyzeDocumentResult {
@@ -23,6 +25,7 @@ public record LspAnalyzeDocumentResult(
diagnostics = List.copyOf(Objects.requireNonNull(diagnostics, "diagnostics"));
semanticHighlights = List.copyOf(Objects.requireNonNull(semanticHighlights, "semanticHighlights"));
documentSymbols = List.copyOf(Objects.requireNonNull(documentSymbols, "documentSymbols"));
structuralAnchors = List.copyOf(Objects.requireNonNull(structuralAnchors, "structuralAnchors"));
workspaceSymbols = List.copyOf(Objects.requireNonNull(workspaceSymbols, "workspaceSymbols"));
}
}
@@ -15,11 +15,12 @@ class LspSemanticAnalyseService {
final LspAnalyzeDocumentRequest request) {
final var normalizedRequestedDocument = normalize(request.documentPath());
return new LspAnalyzeDocumentResult(
new LspSessionStateDTO(true, List.of("diagnostics", "symbols", "definition", "highlight")),
new LspSessionStateDTO(true, List.of("diagnostics", "symbols", "definition", "highlight", "structural-anchors")),
session.semanticPresentation(),
session.diagnosticsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.semanticHighlightsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.documentSymbolsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.structuralAnchorsByDocument().getOrDefault(normalizedRequestedDocument, List.of()),
session.workspaceSymbols());
}
}
@@ -145,6 +145,7 @@ final class LspSemanticReadPhase {
diagnosticsByDocument,
Map.of(),
Map.of(),
Map.of(),
List.of(),
Map.of(),
Map.of());
@@ -180,6 +181,7 @@ final class LspSemanticReadPhase {
diagnosticsByDocument,
semanticIndex.semanticHighlightsByDocument(),
semanticIndex.documentSymbolsByDocument(),
semanticIndex.structuralAnchorsByDocument(),
semanticIndex.workspaceSymbols(),
semanticIndex.symbolsByName(),
semanticIndex.tokensByDocument());
@@ -4,6 +4,7 @@ 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.LspStructuralAnchorDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import java.nio.file.Path;
@@ -16,6 +17,7 @@ public record SemanticSession(
Map<Path, List<LspDiagnosticDTO>> diagnosticsByDocument,
Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument,
Map<Path, List<LspSymbolDTO>> documentSymbolsByDocument,
Map<Path, List<LspStructuralAnchorDTO>> structuralAnchorsByDocument,
List<LspSymbolDTO> workspaceSymbols,
Map<String, List<LspSymbolDTO>> symbolsByName,
Map<Path, List<PbsToken>> tokensByDocument) {
@@ -7,6 +7,7 @@ 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.LspStructuralAnchorDTO;
import p.studio.lsp.dtos.LspSymbolDTO;
import p.studio.lsp.messages.LspSymbolKind;
@@ -22,6 +23,7 @@ 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 Map<Path, List<LspStructuralAnchorDTO>> structuralAnchorsByDocument = 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<>();
@@ -62,6 +64,9 @@ public final class SemanticIndex {
}
}
documentSymbolsByDocument.put(normalizedDocumentPath, List.copyOf(documentSymbols));
structuralAnchorsByDocument.put(
normalizedDocumentPath,
structuralAnchors(documentSymbols, tokens));
}
public void buildHighlights(
@@ -498,6 +503,66 @@ public final class SemanticIndex {
children);
}
private List<LspStructuralAnchorDTO> structuralAnchors(
final List<LspSymbolDTO> documentSymbols,
final List<PbsToken> tokens) {
final List<LspStructuralAnchorDTO> anchors = new ArrayList<>();
for (final LspSymbolDTO symbol : documentSymbols) {
final LspStructuralAnchorDTO anchor = structuralAnchor(symbol, tokens);
if (anchor != null) {
anchors.add(anchor);
}
}
return List.copyOf(anchors);
}
private LspStructuralAnchorDTO structuralAnchor(
final LspSymbolDTO symbol,
final List<PbsToken> tokens) {
final List<LspStructuralAnchorDTO> children = new ArrayList<>();
for (final LspSymbolDTO child : symbol.children()) {
final LspStructuralAnchorDTO anchorChild = structuralAnchor(child, tokens);
if (anchorChild != null) {
children.add(anchorChild);
}
}
final AnchorPair anchorPair = anchorPair(symbol.range(), tokens);
if (anchorPair == null) {
if (children.isEmpty()) {
return null;
}
return new LspStructuralAnchorDTO(symbol.range(), symbol.range(), symbol.range(), children);
}
return new LspStructuralAnchorDTO(symbol.range(), anchorPair.start(), anchorPair.end(), children);
}
private AnchorPair anchorPair(
final LspRangeDTO range,
final List<PbsToken> tokens) {
PbsToken firstLeftBrace = null;
PbsToken lastRightBrace = null;
for (final PbsToken token : tokens) {
if (token.kind() == PbsTokenKind.EOF) {
continue;
}
if (token.start() < range.startOffset() || token.end() > range.endOffset()) {
continue;
}
if (token.kind() == PbsTokenKind.LEFT_BRACE && firstLeftBrace == null) {
firstLeftBrace = token;
}
if (token.kind() == PbsTokenKind.RIGHT_BRACE) {
lastRightBrace = token;
}
}
if (firstLeftBrace == null || lastRightBrace == null || lastRightBrace.start() < firstLeftBrace.start()) {
return null;
}
return new AnchorPair(
new LspRangeDTO(firstLeftBrace.start(), firstLeftBrace.end()),
new LspRangeDTO(lastRightBrace.start(), lastRightBrace.end()));
}
public Map<Path, List<LspHighlightSpanDTO>> semanticHighlightsByDocument() {
return Map.copyOf(semanticHighlightsByDocument);
}
@@ -506,6 +571,10 @@ public final class SemanticIndex {
return Map.copyOf(documentSymbolsByDocument);
}
public Map<Path, List<LspStructuralAnchorDTO>> structuralAnchorsByDocument() {
return Map.copyOf(structuralAnchorsByDocument);
}
public List<LspSymbolDTO> workspaceSymbols() {
return List.copyOf(workspaceSymbols);
}
@@ -521,4 +590,9 @@ public final class SemanticIndex {
public Map<Path, List<PbsToken>> tokensByDocument() {
return Map.copyOf(tokensByDocument);
}
private record AnchorPair(
LspRangeDTO start,
LspRangeDTO end) {
}
}
@@ -2,6 +2,7 @@ package p.studio.lsp;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.lsp.dtos.LspStructuralAnchorDTO;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.lsp.dtos.LspDefinitionTargetDTO;
import p.studio.lsp.messages.LspAnalyzeDocumentRequest;
@@ -104,6 +105,12 @@ final class LspServiceImplTest {
assertTrue(
flatten(analysis.documentSymbols()).stream().anyMatch(symbol -> symbol.kind() == p.studio.lsp.messages.LspSymbolKind.HANDLE),
analysis.toString());
assertFalse(analysis.structuralAnchors().isEmpty(), analysis.toString());
assertEquals(OVERLAY_SOURCE.indexOf("{"), analysis.structuralAnchors().getFirst().startAnchor().startOffset());
assertTrue(
flattenAnchors(analysis.structuralAnchors()).stream().anyMatch(anchor ->
anchor.startAnchor().startOffset() == OVERLAY_SOURCE.indexOf("if true {") + "if true ".length()),
analysis.toString());
assertTrue(
analysis.workspaceSymbols().stream().anyMatch(symbol ->
symbol.name().equals("helper") && symbol.documentPath().equals(normalize(helperFile))),
@@ -236,6 +243,16 @@ final class LspServiceImplTest {
return flattened;
}
private static List<LspStructuralAnchorDTO> flattenAnchors(
final List<LspStructuralAnchorDTO> anchors) {
final List<LspStructuralAnchorDTO> flattened = new java.util.ArrayList<>();
for (final var anchor : anchors) {
flattened.add(anchor);
flattened.addAll(flattenAnchors(anchor.children()));
}
return flattened;
}
private VfsProjectContext projectContext(final Path projectRoot) {
return new VfsProjectContext("Example", "pbs", projectRoot);
}