implement PLN-0032 structural anchor payloads and tests
This commit is contained in:
+102
-17
@@ -1,6 +1,7 @@
|
||||
package p.studio.workspaces.editor;
|
||||
|
||||
import p.studio.lsp.dtos.LspRangeDTO;
|
||||
import p.studio.lsp.dtos.LspStructuralAnchorDTO;
|
||||
import p.studio.lsp.dtos.LspSymbolDTO;
|
||||
import p.studio.lsp.messages.LspSymbolKind;
|
||||
|
||||
@@ -34,11 +35,70 @@ final class EditorDocumentScopeGuideModel {
|
||||
static EditorDocumentScopeGuideModel from(
|
||||
final String content,
|
||||
final List<LspSymbolDTO> symbols) {
|
||||
return fromRanges(
|
||||
content,
|
||||
symbols,
|
||||
0,
|
||||
null,
|
||||
(symbol, depth, parentId, nextId, lineStarts, contentLength) -> {
|
||||
if (symbol.kind() == LspSymbolKind.UNKNOWN) {
|
||||
return null;
|
||||
}
|
||||
final LspRangeDTO range = symbol.range();
|
||||
return toGuideRange(
|
||||
lineStarts,
|
||||
contentLength,
|
||||
range,
|
||||
new LspRangeDTO(range.startOffset(), Math.min(range.endOffset(), range.startOffset() + 1)),
|
||||
new LspRangeDTO(Math.max(range.startOffset(), range.endOffset() - 1), range.endOffset()),
|
||||
depth,
|
||||
parentId,
|
||||
nextId[0]);
|
||||
},
|
||||
LspSymbolDTO::children);
|
||||
}
|
||||
|
||||
static EditorDocumentScopeGuideModel fromStructuralAnchors(
|
||||
final String content,
|
||||
final List<LspStructuralAnchorDTO> structuralAnchors) {
|
||||
return fromRanges(
|
||||
content,
|
||||
structuralAnchors,
|
||||
0,
|
||||
null,
|
||||
(anchor, depth, parentId, nextId, lineStarts, contentLength) -> toGuideRange(
|
||||
lineStarts,
|
||||
contentLength,
|
||||
anchor.range(),
|
||||
anchor.startAnchor(),
|
||||
anchor.endAnchor(),
|
||||
depth,
|
||||
parentId,
|
||||
nextId[0]),
|
||||
LspStructuralAnchorDTO::children);
|
||||
}
|
||||
|
||||
private static <T> EditorDocumentScopeGuideModel fromRanges(
|
||||
final String content,
|
||||
final List<T> roots,
|
||||
final int initialDepth,
|
||||
final Integer initialParentId,
|
||||
final RangeFactory<T> rangeFactory,
|
||||
final ChildAccessor<T> childAccessor) {
|
||||
final List<Integer> lineStarts = lineStarts(content);
|
||||
final List<GuideRange> ranges = new ArrayList<>();
|
||||
final int[] nextId = new int[] {1};
|
||||
for (final LspSymbolDTO symbol : symbols) {
|
||||
appendSymbol(ranges, lineStarts, content.length(), symbol, 0, null, nextId);
|
||||
for (final T root : roots) {
|
||||
appendRangeNode(
|
||||
ranges,
|
||||
lineStarts,
|
||||
content.length(),
|
||||
root,
|
||||
initialDepth,
|
||||
initialParentId,
|
||||
nextId,
|
||||
rangeFactory,
|
||||
childAccessor);
|
||||
}
|
||||
ranges.sort(Comparator
|
||||
.comparingInt(GuideRange::startOffset)
|
||||
@@ -88,25 +148,25 @@ final class EditorDocumentScopeGuideModel {
|
||||
return lineStarts.size();
|
||||
}
|
||||
|
||||
private static void appendSymbol(
|
||||
private static <T> void appendRangeNode(
|
||||
final List<GuideRange> ranges,
|
||||
final List<Integer> lineStarts,
|
||||
final int contentLength,
|
||||
final LspSymbolDTO symbol,
|
||||
final T node,
|
||||
final int depth,
|
||||
final Integer parentId,
|
||||
final int[] nextId) {
|
||||
final int[] nextId,
|
||||
final RangeFactory<T> rangeFactory,
|
||||
final ChildAccessor<T> childAccessor) {
|
||||
Integer currentParentId = parentId;
|
||||
if (symbol.kind() != LspSymbolKind.UNKNOWN) {
|
||||
final GuideRange range = toGuideRange(lineStarts, contentLength, symbol.range(), depth, parentId, nextId[0]);
|
||||
if (range != null) {
|
||||
ranges.add(range);
|
||||
currentParentId = range.id();
|
||||
nextId[0]++;
|
||||
}
|
||||
final GuideRange range = rangeFactory.create(node, depth, parentId, nextId, lineStarts, contentLength);
|
||||
if (range != null) {
|
||||
ranges.add(range);
|
||||
currentParentId = range.id();
|
||||
nextId[0]++;
|
||||
}
|
||||
for (final LspSymbolDTO child : symbol.children()) {
|
||||
appendSymbol(ranges, lineStarts, contentLength, child, depth + 1, currentParentId, nextId);
|
||||
for (final T child : childAccessor.children(node)) {
|
||||
appendRangeNode(ranges, lineStarts, contentLength, child, depth + 1, currentParentId, nextId, rangeFactory, childAccessor);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,18 +174,25 @@ final class EditorDocumentScopeGuideModel {
|
||||
final List<Integer> lineStarts,
|
||||
final int contentLength,
|
||||
final LspRangeDTO range,
|
||||
final LspRangeDTO startAnchor,
|
||||
final LspRangeDTO endAnchor,
|
||||
final int depth,
|
||||
final Integer parentId,
|
||||
final int id) {
|
||||
final int startOffset = clamp(range.startOffset(), 0, contentLength);
|
||||
final int endOffset = Math.max(startOffset, clamp(range.endOffset(), 0, contentLength));
|
||||
final int startLine = lineForOffset(lineStarts, startOffset);
|
||||
final int startAnchorOffset = clamp(startAnchor.startOffset(), startOffset, endOffset);
|
||||
final int endAnchorOffset = clamp(Math.max(startAnchorOffset, endAnchor.startOffset()), startOffset, endOffset);
|
||||
final int startLine = lineForOffset(lineStarts, startAnchorOffset);
|
||||
final int inclusiveEndOffset = Math.max(startOffset, Math.max(startOffset, endOffset) - 1);
|
||||
final int endLine = lineForOffset(lineStarts, clamp(inclusiveEndOffset, 0, contentLength));
|
||||
final int inclusiveEndAnchorOffset = Math.max(
|
||||
startAnchorOffset,
|
||||
Math.min(Math.max(startOffset, endOffset) - 1, Math.max(endAnchorOffset, startAnchorOffset)));
|
||||
final int endLine = lineForOffset(lineStarts, clamp(inclusiveEndAnchorOffset, 0, contentLength));
|
||||
if (endLine <= startLine) {
|
||||
return null;
|
||||
}
|
||||
return new GuideRange(id, depth, startOffset, endOffset, startLine, endLine, parentId);
|
||||
return new GuideRange(id, depth, startOffset, endOffset, startAnchorOffset, endAnchorOffset, startLine, endLine, parentId);
|
||||
}
|
||||
|
||||
private GuideRange findById(final int id) {
|
||||
@@ -225,6 +292,8 @@ final class EditorDocumentScopeGuideModel {
|
||||
int depth,
|
||||
int startOffset,
|
||||
int endOffset,
|
||||
int startAnchorOffset,
|
||||
int endAnchorOffset,
|
||||
int startLine,
|
||||
int endLine,
|
||||
Integer parentId) {
|
||||
@@ -252,4 +321,20 @@ final class EditorDocumentScopeGuideModel {
|
||||
Objects.requireNonNull(kind, "kind");
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface ChildAccessor<T> {
|
||||
List<T> children(T node);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
private interface RangeFactory<T> {
|
||||
GuideRange create(
|
||||
T node,
|
||||
int depth,
|
||||
Integer parentId,
|
||||
int[] nextId,
|
||||
List<Integer> lineStarts,
|
||||
int contentLength);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -381,6 +381,11 @@ public final class EditorWorkspace extends Workspace {
|
||||
if (!fileBuffer.frontendDocument() || analysis == null) {
|
||||
return EditorDocumentScopeGuideModel.empty();
|
||||
}
|
||||
if (!analysis.structuralAnchors().isEmpty()) {
|
||||
return EditorDocumentScopeGuideModel.fromStructuralAnchors(
|
||||
fileBuffer.content(),
|
||||
analysis.structuralAnchors());
|
||||
}
|
||||
return EditorDocumentScopeGuideModel.from(fileBuffer.content(), analysis.documentSymbols());
|
||||
}
|
||||
|
||||
|
||||
+4
@@ -37,6 +37,7 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
List.of(),
|
||||
List.of(new LspHighlightSpanDTO(new LspRangeDTO(0, 2), "pbs-keyword")),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
|
||||
@@ -67,6 +68,7 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
List.of(),
|
||||
List.of(new LspHighlightSpanDTO(new LspRangeDTO(0, 1), "fe-punctuation")),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
|
||||
@@ -98,6 +100,7 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
List.of(),
|
||||
List.of(new LspHighlightSpanDTO(new LspRangeDTO(0, 2), "pbs-keyword")),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
|
||||
@@ -130,6 +133,7 @@ final class EditorDocumentHighlightingRouterTest {
|
||||
List.of(),
|
||||
List.of(new LspHighlightSpanDTO(new LspRangeDTO(16, 20), "pbs-service")),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentHighlightingResult result = EditorDocumentHighlightingRouter.route(
|
||||
|
||||
+28
@@ -2,6 +2,7 @@ package p.studio.workspaces.editor;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.dtos.LspRangeDTO;
|
||||
import p.studio.lsp.dtos.LspStructuralAnchorDTO;
|
||||
import p.studio.lsp.dtos.LspSymbolDTO;
|
||||
import p.studio.lsp.messages.LspSymbolKind;
|
||||
|
||||
@@ -108,6 +109,33 @@ final class EditorDocumentScopeGuideModelTest {
|
||||
assertEquals(EditorDocumentScopeGuideModel.ActiveGuides.empty(), model.resolveActiveGuides(content.indexOf("value")));
|
||||
}
|
||||
|
||||
@Test
|
||||
void structuralAnchorsDriveGuideStartAndEndLines() {
|
||||
final String content = """
|
||||
fn main(
|
||||
value: int
|
||||
) -> void
|
||||
{
|
||||
helper();
|
||||
}
|
||||
""";
|
||||
final LspStructuralAnchorDTO anchor = new LspStructuralAnchorDTO(
|
||||
new LspRangeDTO(content.indexOf("fn main"), content.length()),
|
||||
new LspRangeDTO(content.indexOf("{"), content.indexOf("{") + 1),
|
||||
new LspRangeDTO(content.indexOf("}"), content.indexOf("}") + 1),
|
||||
List.of());
|
||||
|
||||
final EditorDocumentScopeGuideModel model = EditorDocumentScopeGuideModel.fromStructuralAnchors(content, List.of(anchor));
|
||||
final EditorDocumentScopeGuideModel.ActiveGuides guides = model.resolveActiveGuides(content.indexOf("helper"));
|
||||
|
||||
assertSegments(model, 0, guides);
|
||||
assertSegments(model, 1, guides);
|
||||
assertSegments(model, 2, guides);
|
||||
assertSegments(model, 3, guides, "ACTIVE_SCOPE:START");
|
||||
assertSegments(model, 4, guides, "ACTIVE_SCOPE:CONTINUE");
|
||||
assertSegments(model, 5, guides, "ACTIVE_SCOPE:END");
|
||||
}
|
||||
|
||||
private static LspSymbolDTO symbol(
|
||||
final String content,
|
||||
final String startMarker,
|
||||
|
||||
Reference in New Issue
Block a user