implements PLN-0137 (3/7) folding-and-selection-ranges
Advertise folding and selection only when the frontend supports them. Folding uses recovered spans for the listed declarations, their bodies, parameter lists, and Doc text blocks. A brace, parenthesis, or text block the tree does not cover still folds, including an unmatched opener through the end of the text. Selection grows outward from the identifier to the argument or parameter list, the block, and the declaration.
This commit is contained in:
+32
@@ -16,12 +16,15 @@ import p.studio.compiler.pbs.semantics.PbsEditorialSupportService.EditorialDocum
|
||||
import p.studio.compiler.models.SourceKind;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialRename;
|
||||
import p.studio.compiler.pbs.semantics.PbsEditorialWorkspaceSymbol;
|
||||
import p.studio.compiler.pbs.PbsStructureRanges;
|
||||
import p.studio.compiler.pbs.semantics.PbsQuickFix;
|
||||
import p.studio.compiler.pbs.semantics.PbsQuickFixCollector;
|
||||
import p.studio.compiler.services.PBSFrontendPhaseService.PbsSemanticReadSurface;
|
||||
import p.studio.compiler.services.FrontendCodeAction;
|
||||
import p.studio.compiler.services.FrontendCompletionCandidate;
|
||||
import p.studio.compiler.services.FrontendDocumentLink;
|
||||
import p.studio.compiler.services.FrontendFoldingRange;
|
||||
import p.studio.compiler.services.FrontendSelectionRange;
|
||||
import p.studio.compiler.services.FrontendDefinitionLocation;
|
||||
import p.studio.compiler.services.FrontendDocumentRequest;
|
||||
import p.studio.compiler.services.FrontendDocumentSymbol;
|
||||
@@ -443,6 +446,35 @@ public final class PBSFrontendLanguageService implements FrontendLanguageService
|
||||
return List.copyOf(links);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean foldingRangesSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendFoldingRange> foldingRanges(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext) {
|
||||
return PbsStructureRanges.foldingRanges(request == null || request.documentText() == null
|
||||
? ""
|
||||
: request.documentText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean selectionRangesSupported() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<FrontendSelectionRange> selectionRanges(
|
||||
final FrontendDocumentRequest request,
|
||||
final FrontendEditorialContext editorialContext,
|
||||
final List<Integer> offsets) {
|
||||
return PbsStructureRanges.selectionRanges(
|
||||
request == null || request.documentText() == null ? "" : request.documentText(),
|
||||
offsets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean codeActionsSupported() {
|
||||
return true;
|
||||
|
||||
+380
@@ -0,0 +1,380 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import p.studio.compiler.pbs.ast.PbsAst;
|
||||
import p.studio.compiler.pbs.lexer.PbsLexer;
|
||||
import p.studio.compiler.pbs.lexer.PbsToken;
|
||||
import p.studio.compiler.pbs.lexer.PbsTokenKind;
|
||||
import p.studio.compiler.pbs.parser.PbsParser;
|
||||
import p.studio.compiler.services.FrontendFoldingRange;
|
||||
import p.studio.compiler.services.FrontendSelectionRange;
|
||||
import p.studio.compiler.source.Span;
|
||||
import p.studio.compiler.source.diagnostics.DiagnosticSink;
|
||||
import p.studio.compiler.source.identifiers.FileId;
|
||||
import p.studio.utilities.structures.ReadOnlyList;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
|
||||
public final class PbsStructureRanges {
|
||||
private PbsStructureRanges() {
|
||||
}
|
||||
|
||||
public static List<FrontendFoldingRange> foldingRanges(final String documentText) {
|
||||
final String text = documentText == null ? "" : documentText;
|
||||
final ReadOnlyList<PbsToken> tokens = PbsLexer.lex(text, FileId.none(), DiagnosticSink.empty());
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, FileId.none(), DiagnosticSink.empty());
|
||||
return foldingRanges(text, ast, tokens);
|
||||
}
|
||||
|
||||
public static List<FrontendSelectionRange> selectionRanges(
|
||||
final String documentText,
|
||||
final List<Integer> offsets) {
|
||||
final String text = documentText == null ? "" : documentText;
|
||||
final ReadOnlyList<PbsToken> tokens = PbsLexer.lex(text, FileId.none(), DiagnosticSink.empty());
|
||||
final PbsAst.File ast = PbsParser.parse(tokens, FileId.none(), DiagnosticSink.empty());
|
||||
if (offsets == null || offsets.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
final Collected collected = collect(ast);
|
||||
final List<SpanPair> delimiters = delimiters(tokens, textEnd(text));
|
||||
final ArrayList<FrontendSelectionRange> ranges = new ArrayList<>();
|
||||
for (final Integer offset : offsets) {
|
||||
if (offset == null || offset < 0) {
|
||||
continue;
|
||||
}
|
||||
final FrontendSelectionRange range = selectionAt(offset, tokens, collected, delimiters);
|
||||
if (range != null) {
|
||||
ranges.add(range);
|
||||
}
|
||||
}
|
||||
return List.copyOf(ranges);
|
||||
}
|
||||
|
||||
private static List<FrontendFoldingRange> foldingRanges(
|
||||
final String text,
|
||||
final PbsAst.File ast,
|
||||
final ReadOnlyList<PbsToken> tokens) {
|
||||
final Collected collected = collect(ast);
|
||||
final LinkedHashSet<String> emitted = new LinkedHashSet<>();
|
||||
final ArrayList<SpanPair> ranges = new ArrayList<>();
|
||||
for (final SpanPair span : collected.folding()) {
|
||||
addRange(ranges, emitted, span);
|
||||
}
|
||||
for (final SpanPair delimiter : delimiters(tokens, textEnd(text))) {
|
||||
if (collected.coveredKeys().contains(delimiter.key())) {
|
||||
continue;
|
||||
}
|
||||
addRange(ranges, emitted, delimiter);
|
||||
}
|
||||
ranges.sort(Comparator.comparingInt(SpanPair::start).thenComparingInt(SpanPair::end));
|
||||
return ranges.stream()
|
||||
.map(span -> new FrontendFoldingRange(span.start(), span.end()))
|
||||
.toList();
|
||||
}
|
||||
|
||||
private static FrontendSelectionRange selectionAt(
|
||||
final int offset,
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final Collected collected,
|
||||
final List<SpanPair> delimiters) {
|
||||
final SpanPair identifier = smallest(identifierSpans(tokens), offset);
|
||||
final SpanPair parameters = smallest(delimiters, offset);
|
||||
final SpanPair block = smallest(collected.blocks(), offset);
|
||||
final SpanPair declaration = smallest(collected.declarations(), offset);
|
||||
FrontendSelectionRange node = null;
|
||||
SpanPair previous = null;
|
||||
for (final SpanPair layer : new SpanPair[] {declaration, block, parameters, identifier}) {
|
||||
if (layer == null) {
|
||||
continue;
|
||||
}
|
||||
if (previous != null && previous.start() == layer.start() && previous.end() == layer.end()) {
|
||||
continue;
|
||||
}
|
||||
node = new FrontendSelectionRange(layer.start(), layer.end(), node);
|
||||
previous = layer;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
private static Collected collect(final PbsAst.File ast) {
|
||||
final Collected collected = new Collected();
|
||||
if (ast == null || ast.topDecls() == null) {
|
||||
return collected;
|
||||
}
|
||||
for (final PbsAst.TopDecl topDecl : ast.topDecls()) {
|
||||
visitTop(topDecl, true, collected);
|
||||
}
|
||||
return collected;
|
||||
}
|
||||
|
||||
private static void visitTop(
|
||||
final PbsAst.TopDecl topDecl,
|
||||
final boolean topLevel,
|
||||
final Collected collected) {
|
||||
if (topDecl == null) {
|
||||
return;
|
||||
}
|
||||
if (topLevel && isListedDeclaration(topDecl)) {
|
||||
addSpan(collected.folding(), topDecl.span());
|
||||
}
|
||||
if (isListedDeclaration(topDecl) || topDecl instanceof PbsAst.FunctionDecl) {
|
||||
addSpan(collected.declarations(), topDecl.span());
|
||||
}
|
||||
switch (topDecl) {
|
||||
case PbsAst.FunctionDecl functionDecl -> visitFunction(functionDecl, collected);
|
||||
case PbsAst.StructDecl structDecl -> {
|
||||
visitAttributes(structDecl.attributes(), collected);
|
||||
if (structDecl.methods() != null) {
|
||||
for (final PbsAst.FunctionDecl method : structDecl.methods()) {
|
||||
visitTop(method, false, collected);
|
||||
}
|
||||
}
|
||||
if (structDecl.ctors() != null) {
|
||||
for (final PbsAst.CtorDecl ctor : structDecl.ctors()) {
|
||||
visitCtor(ctor, collected);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PbsAst.ServiceDecl serviceDecl -> {
|
||||
visitAttributes(serviceDecl.attributes(), collected);
|
||||
if (serviceDecl.methods() != null) {
|
||||
for (final PbsAst.FunctionDecl method : serviceDecl.methods()) {
|
||||
visitTop(method, false, collected);
|
||||
}
|
||||
}
|
||||
}
|
||||
case PbsAst.ContractDecl contractDecl -> visitAttributes(contractDecl.attributes(), collected);
|
||||
case PbsAst.HostDecl hostDecl -> visitAttributes(hostDecl.attributes(), collected);
|
||||
case PbsAst.EnumDecl enumDecl -> visitAttributes(enumDecl.attributes(), collected);
|
||||
case PbsAst.ErrorDecl errorDecl -> visitAttributes(errorDecl.attributes(), collected);
|
||||
case PbsAst.CallbackDecl callbackDecl -> visitAttributes(callbackDecl.attributes(), collected);
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void visitFunction(
|
||||
final PbsAst.FunctionDecl functionDecl,
|
||||
final Collected collected) {
|
||||
visitAttributes(functionDecl.attributes(), collected);
|
||||
coverBlock(functionDecl.body(), true, collected);
|
||||
}
|
||||
|
||||
private static void visitCtor(
|
||||
final PbsAst.CtorDecl ctor,
|
||||
final Collected collected) {
|
||||
if (ctor == null) {
|
||||
return;
|
||||
}
|
||||
addSpan(collected.declarations(), ctor.span());
|
||||
coverBlock(ctor.body(), true, collected);
|
||||
}
|
||||
|
||||
private static void visitAttributes(
|
||||
final ReadOnlyList<PbsAst.Attribute> attributes,
|
||||
final Collected collected) {
|
||||
if (attributes == null) {
|
||||
return;
|
||||
}
|
||||
for (final PbsAst.Attribute attribute : attributes) {
|
||||
if (attribute == null || attribute.arguments() == null) {
|
||||
continue;
|
||||
}
|
||||
for (final PbsAst.AttributeArgument argument : attribute.arguments()) {
|
||||
if (argument != null && argument.value() instanceof PbsAst.AttributeDocTextBlockValue doc) {
|
||||
addSpan(collected.folding(), doc.span());
|
||||
addSpan(collected.blocks(), doc.span());
|
||||
collected.cover(doc.span());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void coverBlock(
|
||||
final PbsAst.Block block,
|
||||
final boolean emit,
|
||||
final Collected collected) {
|
||||
if (block == null) {
|
||||
return;
|
||||
}
|
||||
if (emit) {
|
||||
addSpan(collected.folding(), block.span());
|
||||
}
|
||||
addSpan(collected.blocks(), block.span());
|
||||
collected.cover(block.span());
|
||||
if (block.statements() == null) {
|
||||
return;
|
||||
}
|
||||
for (final PbsAst.Statement statement : block.statements()) {
|
||||
coverStatement(statement, collected);
|
||||
}
|
||||
}
|
||||
|
||||
private static void coverStatement(
|
||||
final PbsAst.Statement statement,
|
||||
final Collected collected) {
|
||||
switch (statement) {
|
||||
case null -> {
|
||||
}
|
||||
case PbsAst.IfStatement ifStatement -> {
|
||||
coverBlock(ifStatement.thenBlock(), false, collected);
|
||||
coverBlock(ifStatement.elseBlock(), false, collected);
|
||||
coverStatement(ifStatement.elseIf(), collected);
|
||||
}
|
||||
case PbsAst.ForStatement forStatement -> coverBlock(forStatement.body(), false, collected);
|
||||
case PbsAst.WhileStatement whileStatement -> coverBlock(whileStatement.body(), false, collected);
|
||||
default -> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isListedDeclaration(final PbsAst.TopDecl topDecl) {
|
||||
return topDecl instanceof PbsAst.FunctionDecl
|
||||
|| topDecl instanceof PbsAst.StructDecl
|
||||
|| topDecl instanceof PbsAst.ServiceDecl
|
||||
|| topDecl instanceof PbsAst.ContractDecl
|
||||
|| topDecl instanceof PbsAst.HostDecl
|
||||
|| topDecl instanceof PbsAst.EnumDecl
|
||||
|| topDecl instanceof PbsAst.ErrorDecl
|
||||
|| topDecl instanceof PbsAst.CallbackDecl;
|
||||
}
|
||||
|
||||
private static List<SpanPair> delimiters(
|
||||
final ReadOnlyList<PbsToken> tokens,
|
||||
final int textEnd) {
|
||||
final ArrayList<PbsToken> stack = new ArrayList<>();
|
||||
final ArrayList<SpanPair> pairs = new ArrayList<>();
|
||||
if (tokens == null) {
|
||||
return pairs;
|
||||
}
|
||||
for (final PbsToken token : tokens) {
|
||||
if (token == null) {
|
||||
continue;
|
||||
}
|
||||
if (token.kind() == PbsTokenKind.LEFT_BRACE || token.kind() == PbsTokenKind.LEFT_PAREN) {
|
||||
stack.add(token);
|
||||
continue;
|
||||
}
|
||||
if (token.kind() == PbsTokenKind.DOC_TEXT_BLOCK) {
|
||||
addPair(pairs, token.start(), token.end());
|
||||
continue;
|
||||
}
|
||||
if (token.kind() == PbsTokenKind.RIGHT_BRACE || token.kind() == PbsTokenKind.RIGHT_PAREN) {
|
||||
final PbsTokenKind openKind = token.kind() == PbsTokenKind.RIGHT_BRACE
|
||||
? PbsTokenKind.LEFT_BRACE
|
||||
: PbsTokenKind.LEFT_PAREN;
|
||||
if (!stack.isEmpty() && stack.getLast().kind() == openKind) {
|
||||
final PbsToken open = stack.removeLast();
|
||||
addPair(pairs, open.start(), token.end());
|
||||
}
|
||||
}
|
||||
}
|
||||
for (final PbsToken open : stack) {
|
||||
addPair(pairs, open.start(), textEnd);
|
||||
}
|
||||
return pairs;
|
||||
}
|
||||
|
||||
private static List<SpanPair> identifierSpans(final ReadOnlyList<PbsToken> tokens) {
|
||||
final ArrayList<SpanPair> spans = new ArrayList<>();
|
||||
if (tokens == null) {
|
||||
return spans;
|
||||
}
|
||||
for (final PbsToken token : tokens) {
|
||||
if (token != null && token.kind() == PbsTokenKind.IDENTIFIER) {
|
||||
addPair(spans, token.start(), token.end());
|
||||
}
|
||||
}
|
||||
return spans;
|
||||
}
|
||||
|
||||
private static SpanPair smallest(final List<SpanPair> spans, final int offset) {
|
||||
SpanPair best = null;
|
||||
for (final SpanPair span : spans) {
|
||||
if (span.start() > offset || offset >= span.end()) {
|
||||
continue;
|
||||
}
|
||||
if (best == null
|
||||
|| span.end() - span.start() < best.end() - best.start()
|
||||
|| (span.end() - span.start() == best.end() - best.start() && span.start() > best.start())) {
|
||||
best = span;
|
||||
}
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
private static void addSpan(final List<SpanPair> spans, final Span span) {
|
||||
if (span == null) {
|
||||
return;
|
||||
}
|
||||
addPair(spans, toOffset(span.getStart()), toOffset(span.getEnd()));
|
||||
}
|
||||
|
||||
private static void addPair(final List<SpanPair> spans, final int start, final int end) {
|
||||
if (start < 0 || end <= start) {
|
||||
return;
|
||||
}
|
||||
spans.add(new SpanPair(start, end));
|
||||
}
|
||||
|
||||
private static void addRange(
|
||||
final List<SpanPair> ranges,
|
||||
final LinkedHashSet<String> emitted,
|
||||
final SpanPair span) {
|
||||
if (span == null || !emitted.add(span.key())) {
|
||||
return;
|
||||
}
|
||||
ranges.add(span);
|
||||
}
|
||||
|
||||
private static int textEnd(final String text) {
|
||||
return text.getBytes(StandardCharsets.UTF_8).length;
|
||||
}
|
||||
|
||||
private static int toOffset(final long value) {
|
||||
if (value <= 0L) {
|
||||
return 0;
|
||||
}
|
||||
return (int) Math.min(Integer.MAX_VALUE, value);
|
||||
}
|
||||
|
||||
private record SpanPair(int start, int end) {
|
||||
private String key() {
|
||||
return start + ":" + end;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class Collected {
|
||||
private final ArrayList<SpanPair> folding = new ArrayList<>();
|
||||
private final ArrayList<SpanPair> covered = new ArrayList<>();
|
||||
private final ArrayList<SpanPair> blocks = new ArrayList<>();
|
||||
private final ArrayList<SpanPair> declarations = new ArrayList<>();
|
||||
|
||||
private List<SpanPair> folding() {
|
||||
return folding;
|
||||
}
|
||||
|
||||
private void cover(final Span span) {
|
||||
addSpan(covered, span);
|
||||
}
|
||||
|
||||
private LinkedHashSet<String> coveredKeys() {
|
||||
final LinkedHashSet<String> keys = new LinkedHashSet<>();
|
||||
for (final SpanPair span : covered) {
|
||||
keys.add(span.key());
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
private List<SpanPair> blocks() {
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private List<SpanPair> declarations() {
|
||||
return declarations;
|
||||
}
|
||||
}
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package p.studio.compiler.pbs;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.services.FrontendFoldingRange;
|
||||
import p.studio.compiler.services.FrontendSelectionRange;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class PbsStructureRangesTest {
|
||||
private static final String SOURCE = """
|
||||
[Doc(markdown = \"\"\"
|
||||
hello
|
||||
\"\"\")]
|
||||
fn length(value: int) -> int {
|
||||
return value;
|
||||
}
|
||||
""";
|
||||
|
||||
@Test
|
||||
void foldingUsesDeclarationBodyParameterListAndDocText() {
|
||||
final var ranges = PbsStructureRanges.foldingRanges(SOURCE);
|
||||
final var slices = ranges.stream().map(range -> slice(SOURCE, range)).toList();
|
||||
|
||||
assertTrue(slices.stream().anyMatch(slice -> slice.contains("hello")), slices.toString());
|
||||
assertTrue(slices.stream().anyMatch(slice -> slice.equals("(value: int)")), slices.toString());
|
||||
assertTrue(slices.stream().anyMatch(slice -> slice.startsWith("{")), slices.toString());
|
||||
assertTrue(slices.stream().anyMatch(slice -> slice.contains("fn length")), slices.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectionGrowsFromIdentifierToBlockToDeclaration() {
|
||||
final int marker = SOURCE.indexOf("return value");
|
||||
assertTrue(marker >= 0, SOURCE);
|
||||
final int offset = marker + "return ".length();
|
||||
final var selections = PbsStructureRanges.selectionRanges(SOURCE, List.of(offset));
|
||||
assertEquals(1, selections.size(), "offset " + offset);
|
||||
final FrontendSelectionRange selection = selections.getFirst();
|
||||
|
||||
assertEquals("value", slice(SOURCE, selection.startOffset(), selection.endOffset()));
|
||||
assertNotNull(selection.parent());
|
||||
assertTrue(slice(SOURCE, selection.parent().startOffset(), selection.parent().endOffset()).startsWith("{"));
|
||||
assertNotNull(selection.parent().parent());
|
||||
assertTrue(slice(SOURCE, selection.parent().parent().startOffset(), selection.parent().parent().endOffset())
|
||||
.contains("fn length"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unmatchedBraceFoldsThroughTheEndOfTheText() {
|
||||
final String broken = """
|
||||
fn broken() -> void {
|
||||
let x: int = 1;
|
||||
""";
|
||||
final int textEnd = broken.getBytes(StandardCharsets.UTF_8).length;
|
||||
final var ranges = PbsStructureRanges.foldingRanges(broken);
|
||||
|
||||
assertTrue(ranges.stream().anyMatch(range ->
|
||||
range.endOffset() == textEnd && slice(broken, range).startsWith("{")));
|
||||
}
|
||||
|
||||
private static String slice(final String text, final FrontendFoldingRange range) {
|
||||
return slice(text, range.startOffset(), range.endOffset());
|
||||
}
|
||||
|
||||
private static String slice(final String text, final int start, final int end) {
|
||||
final byte[] bytes = text.getBytes(StandardCharsets.UTF_8);
|
||||
return new String(bytes, start, end - start, StandardCharsets.UTF_8);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user