implement PLN-0032 structural anchor payloads and tests
This commit is contained in:
+2
-1
@@ -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) {
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user