implements PLN-0137 (2/7) document-links

Advertise document links only when the frontend supports them.
A link covers the ModuleRef span when module assembly already
bound that module to a regular file. Virtual stdlib and unresolved
modules produce no link.
This commit is contained in:
2026-09-22 08:34:13 +01:00
parent 099857b636
commit 3320f222bb
18 changed files with 489 additions and 2 deletions
@@ -0,0 +1,28 @@
package p.studio.lsp.messages;
import java.util.Objects;
public record BaselineDocumentLink(
String targetUri,
int startLine,
int startCharacter,
int endLine,
int endCharacter) {
public BaselineDocumentLink {
targetUri = requireText(targetUri, "targetUri");
if (startLine < 0 || startCharacter < 0 || endLine < 0 || endCharacter < 0) {
throw new IllegalArgumentException("document link range coordinates must not be negative");
}
}
private static String requireText(
final String value,
final String field) {
final String candidate = Objects.requireNonNull(value, field).trim();
if (candidate.isEmpty()) {
throw new IllegalArgumentException(field + " must not be blank");
}
return candidate;
}
}
@@ -0,0 +1,13 @@
package p.studio.lsp.messages;
import java.util.List;
public record BaselineDocumentLinks(List<BaselineDocumentLink> links) {
public BaselineDocumentLinks {
links = links == null ? List.of() : List.copyOf(links);
}
public static BaselineDocumentLinks empty() {
return new BaselineDocumentLinks(List.of());
}
}
@@ -16,6 +16,7 @@ public record BaselineServerDescription(
boolean workspaceSymbolsSupported,
boolean renameSupported,
boolean codeActionsSupported,
boolean documentLinksSupported,
List<String> semanticTokenTypes,
List<BaselineSemanticHostProjection> semanticHostProjections,
List<BaselineVisualTheme> visualThemes,
@@ -9,6 +9,7 @@ import p.studio.lsp.messages.BaselineReferences;
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.BaselinePrepareRename;
import p.studio.lsp.messages.BaselineRename;
import p.studio.lsp.messages.BaselineSemanticTokens;
@@ -60,6 +61,10 @@ public interface LanguageServiceBridge {
BaselineSemanticTokens semanticTokens(LspProjectContext context, String documentUri, String text);
default BaselineDocumentLinks documentLinks(LspProjectContext context, String documentUri, String text) {
return BaselineDocumentLinks.empty();
}
BaselineCodeActions codeActions(
LspProjectContext context,
String documentUri,
@@ -6,6 +6,7 @@ import p.studio.compiler.messages.*;
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.FrontendLanguageService;
import p.studio.compiler.services.FrontendDefinitionLocation;
import p.studio.compiler.services.FrontendDocumentRequest;
@@ -57,6 +58,9 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
final boolean codeActionsSupported = languageService
.map(FrontendLanguageService::codeActionsSupported)
.orElse(false);
final boolean documentLinksSupported = languageService
.map(FrontendLanguageService::documentLinksSupported)
.orElse(false);
return new BaselineServerDescription(
"Prometeu Studio LSP",
"0.1.0",
@@ -70,6 +74,7 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
languageServicePresent,
languageServicePresent,
codeActionsSupported,
documentLinksSupported,
presentation.semanticKeys(),
presentation.hostProjections().stream().map(this::mapSemanticHostProjection).toList(),
presentation.themes().stream().map(this::mapVisualTheme).toList(),
@@ -365,6 +370,29 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
.orElseGet(() -> BaselineRename.refused("not renameable"));
}
@Override
public BaselineDocumentLinks documentLinks(
final LspProjectContext context,
final String documentUri,
final String text) {
Objects.requireNonNull(context, "context");
final var languageService = frontendProvider(context).languageService();
if (languageService.isEmpty() || !languageService.orElseThrow().documentLinksSupported()) {
return BaselineDocumentLinks.empty();
}
return editorialDocument(context, documentUri, text)
.map(document -> {
final FrontendDocumentRequest request = new FrontendDocumentRequest(
context.projectRoot(),
normalizeDocumentPath(documentUri),
document.text());
return new BaselineDocumentLinks(mapDocumentLinks(
languageService.orElseThrow().documentLinks(request, document.editorialContext()),
document.text()));
})
.orElseGet(BaselineDocumentLinks::empty);
}
@Override
public BaselineCodeActions codeActions(
final LspProjectContext context,
@@ -830,6 +858,28 @@ public final class CompilerLanguageServiceBridge implements LanguageServiceBridg
return List.copyOf(mapped);
}
private List<BaselineDocumentLink> mapDocumentLinks(
final List<FrontendDocumentLink> links,
final String currentDocumentText) {
if (links == null || links.isEmpty()) {
return List.of();
}
final DocumentPositionMapper mapper = new DocumentPositionMapper(
currentDocumentText == null ? "" : currentDocumentText);
final ArrayList<BaselineDocumentLink> mapped = new ArrayList<>();
for (final FrontendDocumentLink link : links) {
final DocumentPosition start = mapper.positionOf(link.startOffset());
final DocumentPosition end = mapper.positionOf(link.endOffset());
mapped.add(new BaselineDocumentLink(
link.path().toUri().toString(),
start.line(),
start.character(),
end.line(),
end.character()));
}
return List.copyOf(mapped);
}
private List<BaselineDefinitionLocation> mapDefinitionLocations(
final List<FrontendDefinitionLocation> locations,
final Path currentDocumentPath,
@@ -168,6 +168,14 @@ public final class PrometeuTextDocumentService implements TextDocumentService {
return CompletableFuture.completedFuture(protocolMessageMapper.mapRename(result));
}
@Override
public CompletableFuture<List<DocumentLink>> documentLink(final DocumentLinkParams params) {
final String uri = params.getTextDocument().getUri();
final String text = documentTextByUri.get(uri);
return CompletableFuture.completedFuture(protocolMessageMapper.mapDocumentLinks(
languageServiceBridge.documentLinks(project, uri, text)));
}
@Override
public CompletableFuture<List<Either<Command, CodeAction>>> codeAction(final CodeActionParams params) {
if (!quickFixRequested(params.getContext())) {
@@ -53,6 +53,11 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
codeActionOptions.setCodeActionKinds(List.of(CodeActionKind.QuickFix));
capabilities.setCodeActionProvider(codeActionOptions);
}
if (description.documentLinksSupported()) {
final DocumentLinkOptions documentLinkOptions = new DocumentLinkOptions();
documentLinkOptions.setResolveProvider(false);
capabilities.setDocumentLinkProvider(documentLinkOptions);
}
final SemanticTokensWithRegistrationOptions semanticTokens = new SemanticTokensWithRegistrationOptions();
semanticTokens.setLegend(new SemanticTokensLegend(description.semanticTokenTypes(), List.of()));
semanticTokens.setFull(true);
@@ -159,6 +164,23 @@ public final class Lsp4jProtocolMessageMapper implements ProtocolMessageMapper {
return edit;
}
@Override
public List<DocumentLink> mapDocumentLinks(final BaselineDocumentLinks links) {
if (links == null || links.links().isEmpty()) {
return List.of();
}
final ArrayList<DocumentLink> mapped = new ArrayList<>();
for (final BaselineDocumentLink link : links.links()) {
final DocumentLink documentLink = new DocumentLink();
documentLink.setRange(new Range(
new Position(link.startLine(), link.startCharacter()),
new Position(link.endLine(), link.endCharacter())));
documentLink.setTarget(link.targetUri());
mapped.add(documentLink);
}
return List.copyOf(mapped);
}
@Override
public List<Either<Command, CodeAction>> mapCodeActions(
final BaselineCodeActions actions,
@@ -9,6 +9,7 @@ import org.eclipse.lsp4j.PublishDiagnosticsParams;
import org.eclipse.lsp4j.SignatureHelp;
import org.eclipse.lsp4j.SemanticTokens;
import org.eclipse.lsp4j.Diagnostic;
import org.eclipse.lsp4j.DocumentLink;
import org.eclipse.lsp4j.DocumentSymbol;
import org.eclipse.lsp4j.Location;
import org.eclipse.lsp4j.Range;
@@ -19,6 +20,7 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either3;
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.BaselineDocumentSymbols;
import p.studio.lsp.messages.BaselineWorkspaceSymbols;
import p.studio.lsp.messages.BaselineCodeActions;
@@ -59,6 +61,10 @@ public interface ProtocolMessageMapper {
List<Either<Command, CodeAction>> mapCodeActions(BaselineCodeActions actions, List<Diagnostic> contextDiagnostics);
default List<DocumentLink> mapDocumentLinks(BaselineDocumentLinks links) {
return List.of();
}
CompletionList mapCompletion(BaselineCompletion completion);
SignatureHelp mapSignatureHelp(BaselineSignatureHelp signatureHelp);