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:
2026-09-22 08:45:31 +01:00
parent 3320f222bb
commit ce2e3e4cd3
21 changed files with 854 additions and 0 deletions
@@ -0,0 +1,14 @@
package p.studio.lsp.messages;
public record BaselineFoldingRange(
int startLine,
int startCharacter,
int endLine,
int endCharacter) {
public BaselineFoldingRange {
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("folding range coordinates must not be negative");
}
}
}
@@ -0,0 +1,13 @@
package p.studio.lsp.messages;
import java.util.List;
public record BaselineFoldingRanges(List<BaselineFoldingRange> ranges) {
public BaselineFoldingRanges {
ranges = ranges == null ? List.of() : List.copyOf(ranges);
}
public static BaselineFoldingRanges empty() {
return new BaselineFoldingRanges(List.of());
}
}
@@ -0,0 +1,15 @@
package p.studio.lsp.messages;
public record BaselineSelectionRange(
int startLine,
int startCharacter,
int endLine,
int endCharacter,
BaselineSelectionRange parent) {
public BaselineSelectionRange {
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("selection range coordinates must not be negative");
}
}
}
@@ -0,0 +1,13 @@
package p.studio.lsp.messages;
import java.util.List;
public record BaselineSelectionRanges(List<BaselineSelectionRange> ranges) {
public BaselineSelectionRanges {
ranges = ranges == null ? List.of() : List.copyOf(ranges);
}
public static BaselineSelectionRanges empty() {
return new BaselineSelectionRanges(List.of());
}
}
@@ -17,6 +17,8 @@ public record BaselineServerDescription(
boolean renameSupported,
boolean codeActionsSupported,
boolean documentLinksSupported,
boolean foldingRangesSupported,
boolean selectionRangesSupported,
List<String> semanticTokenTypes,
List<BaselineSemanticHostProjection> semanticHostProjections,
List<BaselineVisualTheme> visualThemes,
@@ -10,6 +10,9 @@ import p.studio.lsp.messages.BaselineWorkspaceSymbols;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselineDocumentLinks;
import p.studio.lsp.messages.BaselineFoldingRanges;
import p.studio.lsp.messages.BaselineSelectionRanges;
import p.studio.lsp.messages.DocumentPosition;
import p.studio.lsp.messages.BaselinePrepareRename;
import p.studio.lsp.messages.BaselineRename;
import p.studio.lsp.messages.BaselineSemanticTokens;
@@ -65,6 +68,18 @@ public interface LanguageServiceBridge {
return BaselineDocumentLinks.empty();
}
default BaselineFoldingRanges foldingRanges(LspProjectContext context, String documentUri, String text) {
return BaselineFoldingRanges.empty();
}
default BaselineSelectionRanges selectionRanges(
LspProjectContext context,
String documentUri,
String text,
List<DocumentPosition> positions) {
return BaselineSelectionRanges.empty();
}
BaselineCodeActions codeActions(
LspProjectContext context,
String documentUri,
@@ -7,6 +7,8 @@ import p.studio.compiler.models.*;
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.FrontendLanguageService;
import p.studio.compiler.services.FrontendDefinitionLocation;
import p.studio.compiler.services.FrontendDocumentRequest;
@@ -61,6 +63,12 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
final boolean documentLinksSupported = languageService
.map(FrontendLanguageService::documentLinksSupported)
.orElse(false);
final boolean foldingRangesSupported = languageService
.map(FrontendLanguageService::foldingRangesSupported)
.orElse(false);
final boolean selectionRangesSupported = languageService
.map(FrontendLanguageService::selectionRangesSupported)
.orElse(false);
return new BaselineServerDescription(
"Prometeu Studio LSP",
"0.1.0",
@@ -75,6 +83,8 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
languageServicePresent,
codeActionsSupported,
documentLinksSupported,
foldingRangesSupported,
selectionRangesSupported,
presentation.semanticKeys(),
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
presentation.themes().stream().map(this::mapVisualTheme).toList(),
@@ -393,6 +403,120 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
.orElseGet(BaselineDocumentLinks::empty);
}
@Override
public BaselineFoldingRanges foldingRanges(
final LspProjectContext context,
final String documentUri,
final String text) {
return structureRanges(context, documentUri, text, true, List.of()).folding();
}
@Override
public BaselineSelectionRanges selectionRanges(
final LspProjectContext context,
final String documentUri,
final String text,
final List<DocumentPosition> positions) {
return structureRanges(context, documentUri, text, false, positions == null ? List.of() : positions).selection();
}
private StructureRanges structureRanges(
final LspProjectContext context,
final String documentUri,
final String text,
final boolean folding,
final List<DocumentPosition> positions) {
Objects.requireNonNull(context, "context");
final var languageService = frontendProvider(context).languageService();
if (languageService.isEmpty()) {
return StructureRanges.empty();
}
final boolean supported = folding
? languageService.orElseThrow().foldingRangesSupported()
: languageService.orElseThrow().selectionRangesSupported();
if (!supported) {
return StructureRanges.empty();
}
final String effectiveText = text == null ? "" : text;
final FrontendDocumentRequest request = new FrontendDocumentRequest(
context.projectRoot(),
normalizeDocumentPath(documentUri),
effectiveText);
final DocumentPositionMapper mapper = new DocumentPositionMapper(effectiveText);
if (folding) {
return new StructureRanges(new BaselineFoldingRanges(mapFoldingRanges(
languageService.orElseThrow().foldingRanges(request, null),
mapper)), BaselineSelectionRanges.empty());
}
final ArrayList<Integer> offsets = new ArrayList<>();
for (final DocumentPosition position : positions) {
if (position == null) {
offsets.add(0);
continue;
}
offsets.add(mapper.byteOffsetOf(position.line(), position.character()));
}
return new StructureRanges(
BaselineFoldingRanges.empty(),
new BaselineSelectionRanges(mapSelectionRanges(
languageService.orElseThrow().selectionRanges(request, null, offsets),
mapper)));
}
private List<BaselineFoldingRange> mapFoldingRanges(
final List<FrontendFoldingRange> ranges,
final DocumentPositionMapper mapper) {
if (ranges == null || ranges.isEmpty()) {
return List.of();
}
final ArrayList<BaselineFoldingRange> mapped = new ArrayList<>();
for (final FrontendFoldingRange range : ranges) {
final DocumentPosition start = mapper.positionOf(range.startOffset());
final DocumentPosition end = mapper.positionOf(range.endOffset());
mapped.add(new BaselineFoldingRange(
start.line(),
start.character(),
end.line(),
end.character()));
}
return List.copyOf(mapped);
}
private List<BaselineSelectionRange> mapSelectionRanges(
final List<FrontendSelectionRange> ranges,
final DocumentPositionMapper mapper) {
if (ranges == null || ranges.isEmpty()) {
return List.of();
}
final ArrayList<BaselineSelectionRange> mapped = new ArrayList<>();
for (final FrontendSelectionRange range : ranges) {
mapped.add(mapSelectionRange(range, mapper));
}
return List.copyOf(mapped);
}
private BaselineSelectionRange mapSelectionRange(
final FrontendSelectionRange range,
final DocumentPositionMapper mapper) {
if (range == null) {
return null;
}
final DocumentPosition start = mapper.positionOf(range.startOffset());
final DocumentPosition end = mapper.positionOf(range.endOffset());
return new BaselineSelectionRange(
start.line(),
start.character(),
end.line(),
end.character(),
mapSelectionRange(range.parent(), mapper));
}
private record StructureRanges(BaselineFoldingRanges folding, BaselineSelectionRanges selection) {
private static StructureRanges empty() {
return new StructureRanges(BaselineFoldingRanges.empty(), BaselineSelectionRanges.empty());
}
}
@Override
public BaselineCodeActions codeActions(
final LspProjectContext context,
@@ -9,6 +9,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;
import org.eclipse.lsp4j.services.LanguageClient;
import org.eclipse.lsp4j.services.TextDocumentService;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.DocumentPosition;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.lsp.services.LanguageServiceBridge;
import p.studio.lsp.services.protocol.mapping.ProtocolMessageMapper;
@@ -168,6 +169,31 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
return CompletableFuture.completedFuture(protocolMessageMapper.mapRename(result));
}
@Override
public CompletableFuture<List<FoldingRange>> foldingRange(final FoldingRangeRequestParams params) {
final String uri = params.getTextDocument().getUri();
final String text = documentTextByUri.get(uri);
return CompletableFuture.completedFuture(protocolMessageMapper.mapFoldingRanges(
languageServiceBridge.foldingRanges(project, uri, text)));
}
@Override
public CompletableFuture<List<SelectionRange>> selectionRange(final SelectionRangeParams params) {
final String uri = params.getTextDocument().getUri();
final String text = documentTextByUri.get(uri);
final ArrayList<DocumentPosition> positions = new ArrayList<>();
if (params.getPositions() != null) {
for (final Position position : params.getPositions()) {
if (position == null) {
continue;
}
positions.add(new DocumentPosition(position.getLine(), position.getCharacter()));
}
}
return CompletableFuture.completedFuture(protocolMessageMapper.mapSelectionRanges(
languageServiceBridge.selectionRanges(project, uri, text, positions)));
}
@Override
public CompletableFuture<List<DocumentLink>> documentLink(final DocumentLinkParams params) {
final String uri = params.getTextDocument().getUri();
@@ -58,6 +58,12 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
documentLinkOptions.setResolveProvider(false);
capabilities.setDocumentLinkProvider(documentLinkOptions);
}
if (description.foldingRangesSupported()) {
capabilities.setFoldingRangeProvider(true);
}
if (description.selectionRangesSupported()) {
capabilities.setSelectionRangeProvider(true);
}
final SemanticTokensWithRegistrationOptions semanticTokens = new SemanticTokensWithRegistrationOptions();
semanticTokens.setLegend(new SemanticTokensLegend(description.semanticTokenTypes(), List.of()));
semanticTokens.setFull(true);
@@ -181,6 +187,47 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
return List.copyOf(mapped);
}
@Override
public List<FoldingRange> mapFoldingRanges(final BaselineFoldingRanges ranges) {
if (ranges == null || ranges.ranges().isEmpty()) {
return List.of();
}
final ArrayList<FoldingRange> mapped = new ArrayList<>();
for (final BaselineFoldingRange range : ranges.ranges()) {
final FoldingRange foldingRange = new FoldingRange();
foldingRange.setStartLine(range.startLine());
foldingRange.setStartCharacter(range.startCharacter());
foldingRange.setEndLine(range.endLine());
foldingRange.setEndCharacter(range.endCharacter());
mapped.add(foldingRange);
}
return List.copyOf(mapped);
}
@Override
public List<SelectionRange> mapSelectionRanges(final BaselineSelectionRanges ranges) {
if (ranges == null || ranges.ranges().isEmpty()) {
return List.of();
}
final ArrayList<SelectionRange> mapped = new ArrayList<>();
for (final BaselineSelectionRange range : ranges.ranges()) {
mapped.add(mapSelectionRange(range));
}
return List.copyOf(mapped);
}
private SelectionRange mapSelectionRange(final BaselineSelectionRange range) {
if (range == null) {
return null;
}
final SelectionRange selectionRange = new SelectionRange();
selectionRange.setRange(new Range(
new Position(range.startLine(), range.startCharacter()),
new Position(range.endLine(), range.endCharacter())));
selectionRange.setParent(mapSelectionRange(range.parent()));
return selectionRange;
}
@Override
public List<Either<Command, CodeAction>> mapCodeActions(
final BaselineCodeActions actions,
@@ -11,6 +11,8 @@ import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.FoldingRange;
import org.eclipse.lsp4j.SelectionRange;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
import org.eclipse.lsp4j.SymbolInformation;
@@ -21,6 +23,8 @@ import org.eclipse.lsp4j.PrepareRenameDefaultBehavior;
import org.eclipse.lsp4j.PrepareRenameResult;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineDocumentLinks;
import p.studio.lsp.messages.BaselineFoldingRanges;
import p.studio.lsp.messages.BaselineSelectionRanges;
import p.studio.lsp.messages.BaselineDocumentSymbols;
import p.studio.lsp.messages.BaselineWorkspaceSymbols;
import p.studio.lsp.messages.BaselineCodeActions;
@@ -65,6 +69,14 @@ public interface ProtocolMessageMapper {
return List.of();
}
default List<FoldingRange> mapFoldingRanges(BaselineFoldingRanges ranges) {
return List.of();
}
default List<SelectionRange> mapSelectionRanges(BaselineSelectionRanges ranges) {
return List.of();
}
CompletionList mapCompletion(BaselineCompletion completion);
SignatureHelp mapSignatureHelp(BaselineSignatureHelp signatureHelp);
@@ -11,6 +11,7 @@ import p.studio.compiler.services.FrontendProvider;
import p.studio.compiler.source.diagnostics.DiagnosticSink;
import p.studio.lsp.messages.BaselineCodeActionDiagnostic;
import p.studio.lsp.messages.BaselineIssueSeverity;
import p.studio.lsp.messages.DocumentPosition;
import p.studio.lsp.messages.LspProjectContext;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.ReadOnlySet;
@@ -77,6 +78,9 @@ class CompilerLanguageServiceBridgeTest {
new BaselineCodeActionDiagnostic("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE", 0, 0, 0, 1)))
.actions().isEmpty());
assertTrue(bridge.documentLinks(context, documentUri, "fn main() {}").links().isEmpty());
assertTrue(bridge.foldingRanges(context, documentUri, "fn main() {}").ranges().isEmpty());
assertTrue(bridge.selectionRanges(context, documentUri, "fn main() {}", List.of(new DocumentPosition(0, 0)))
.ranges().isEmpty());
}
@Test
@@ -772,6 +776,8 @@ class CompilerLanguageServiceBridgeTest {
assertTrue(description.renameSupported());
assertTrue(description.codeActionsSupported());
assertTrue(description.documentLinksSupported());
assertTrue(description.foldingRangesSupported());
assertTrue(description.selectionRangesSupported());
assertEquals(1, description.semanticHostProjections().size());
assertEquals("vscode", description.semanticHostProjections().getFirst().hostId());
assertTrue(description.semanticHostProjections().getFirst().tokenProjections().stream()
@@ -106,6 +106,8 @@ class PrometeuLanguageServerTest {
true,
false,
false,
false,
false,
List.of(),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -58,6 +58,8 @@ final class Lsp4jProtocolMessageMapperTest {
true,
true,
true,
true,
true,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -86,6 +88,8 @@ final class Lsp4jProtocolMessageMapperTest {
assertEquals(List.of(CodeActionKind.QuickFix), result.getCapabilities().getCodeActionProvider().getRight().getCodeActionKinds());
assertEquals(Boolean.FALSE, result.getCapabilities().getCodeActionProvider().getRight().getResolveProvider());
assertEquals(Boolean.FALSE, result.getCapabilities().getDocumentLinkProvider().getResolveProvider());
assertEquals(Boolean.TRUE, result.getCapabilities().getFoldingRangeProvider().getLeft());
assertEquals(Boolean.TRUE, result.getCapabilities().getSelectionRangeProvider().getLeft());
final var experimental = assertInstanceOf(Map.class, result.getCapabilities().getExperimental());
final var semanticPayload = assertInstanceOf(Map.class, experimental.get("prometeuSemanticHostProjections"));
@@ -308,6 +312,8 @@ final class Lsp4jProtocolMessageMapperTest {
final var mapper = new Lsp4jProtocolMessageMapper();
final var unsupported = mapper.mapInitializeResult(serverDescription(false));
assertNull(unsupported.getCapabilities().getDocumentLinkProvider());
assertNull(unsupported.getCapabilities().getFoldingRangeProvider());
assertNull(unsupported.getCapabilities().getSelectionRangeProvider());
final var mapped = mapper.mapDocumentLinks(new BaselineDocumentLinks(List.of(
new BaselineDocumentLink("file:///tmp/a/source.pbs", 0, 19, 0, 25))));
@@ -335,6 +341,8 @@ final class Lsp4jProtocolMessageMapperTest {
true,
true,
documentLinksSupported,
documentLinksSupported,
documentLinksSupported,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",