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
@@ -1,6 +1,7 @@
package p.studio.lsp.services.compiler;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.studio.compiler.messages.BuildingIssueSink;
import p.studio.compiler.messages.FrontendPhaseContext;
import p.studio.compiler.models.FrontendSpec;
@@ -14,6 +15,7 @@ import p.studio.lsp.messages.LspProjectContext;
import p.studio.utilities.logs.LogAggregator;
import p.studio.utilities.structures.ReadOnlySet;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -74,6 +76,7 @@ class CompilerLanguageServiceBridgeTest {
assertTrue(bridge.codeActions(context, documentUri, "fn main() {}", List.of(
new BaselineCodeActionDiagnostic("E_SEM_DUPLICATE_RESERVED_ATTRIBUTE", 0, 0, 0, 1)))
.actions().isEmpty());
assertTrue(bridge.documentLinks(context, documentUri, "fn main() {}").links().isEmpty());
}
@Test
@@ -768,6 +771,7 @@ class CompilerLanguageServiceBridgeTest {
assertTrue(description.workspaceSymbolsSupported());
assertTrue(description.renameSupported());
assertTrue(description.codeActionsSupported());
assertTrue(description.documentLinksSupported());
assertEquals(1, description.semanticHostProjections().size());
assertEquals("vscode", description.semanticHostProjections().getFirst().hostId());
assertTrue(description.semanticHostProjections().getFirst().tokenProjections().stream()
@@ -781,6 +785,77 @@ class CompilerLanguageServiceBridgeTest {
&& tokenStyle.foreground().equals("#7fb8ff")));
}
@Test
void documentLinksSkipVirtualStdlibImports() throws IOException {
final Path projectRoot = findRepoRoot(Path.of("").toAbsolutePath().normalize())
.resolve("test-projects")
.resolve("main")
.toAbsolutePath()
.normalize();
final Path documentPath = projectRoot.resolve("src").resolve("main.pbs");
final String text = Files.readString(documentPath);
final CompilerLanguageServiceBridge bridge = new CompilerLanguageServiceBridge();
final var links = bridge.documentLinks(
new LspProjectContext("main", "pbs", projectRoot),
documentPath.toUri().toString(),
text);
assertTrue(links.links().isEmpty());
}
@Test
void documentLinksPointAtThePhysicalModuleFile(@TempDir final Path tempDir) throws IOException {
final Path projectRoot = tempDir.resolve("app");
final Path sourceA = projectRoot.resolve("src").resolve("a").resolve("source.pbs");
final Path sourceB = projectRoot.resolve("src").resolve("b").resolve("source.pbs");
Files.createDirectories(sourceA.getParent());
Files.createDirectories(sourceB.getParent());
Files.writeString(projectRoot.resolve("prometeu.json"), """
{
"name": "app",
"version": "1.0.0",
"language": "pbs",
"stdlib": "1",
"target": "Game",
"dependencies": []
}
""");
Files.writeString(sourceA, """
fn target() -> int {
return 1;
}
""");
Files.writeString(sourceA.getParent().resolve("mod.barrel"), "pub fn target() -> int;\n");
final String caller = """
import { target } from @app:a;
import { Log } from @sdk:log;
import { Missing } from @app:missing;
fn caller() -> int {
return target();
}
""";
Files.writeString(sourceB, caller);
Files.writeString(sourceB.getParent().resolve("mod.barrel"), "pub fn caller() -> int;\n");
final Path realRoot = projectRoot.toRealPath();
final Path realA = sourceA.toRealPath();
final Path realB = sourceB.toRealPath();
final CompilerLanguageServiceBridge bridge = new CompilerLanguageServiceBridge();
final var links = bridge.documentLinks(
new LspProjectContext("app", "pbs", realRoot),
realB.toUri().toString(),
caller);
assertEquals(1, links.links().size());
final var link = links.links().getFirst();
assertEquals(realA.toUri().toString(), link.targetUri());
final int start = new DocumentPositionMapper(caller).byteOffsetOf(link.startLine(), link.startCharacter());
final int end = new DocumentPositionMapper(caller).byteOffsetOf(link.endLine(), link.endCharacter());
assertEquals("@app:a", new String(caller.getBytes(StandardCharsets.UTF_8), start, end - start, StandardCharsets.UTF_8));
}
private Path findRepoRoot(final Path start) {
var current = start;
while (current != null) {
@@ -105,6 +105,7 @@ class PrometeuLanguageServerTest {
true,
true,
false,
false,
List.of(),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -10,6 +10,8 @@ import p.studio.lsp.messages.BaselineCodeActions;
import p.studio.lsp.messages.BaselineCompletion;
import p.studio.lsp.messages.BaselineDocumentAnalysis;
import p.studio.lsp.messages.BaselineDocumentIssue;
import p.studio.lsp.messages.BaselineDocumentLink;
import p.studio.lsp.messages.BaselineDocumentLinks;
import p.studio.lsp.messages.BaselineIssueSeverity;
import p.studio.lsp.messages.BaselineCompletionItem;
import p.studio.lsp.messages.BaselineCompletionItemKind;
@@ -55,6 +57,7 @@ final class Lsp4jProtocolMessageMapperTest {
true,
true,
true,
true,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",
@@ -82,6 +85,7 @@ final class Lsp4jProtocolMessageMapperTest {
assertEquals(Boolean.TRUE, result.getCapabilities().getRenameProvider().getRight().getPrepareProvider());
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());
final var experimental = assertInstanceOf(Map.class, result.getCapabilities().getExperimental());
final var semanticPayload = assertInstanceOf(Map.class, experimental.get("prometeuSemanticHostProjections"));
@@ -298,4 +302,53 @@ final class Lsp4jProtocolMessageMapperTest {
assertEquals(1, action.getDiagnostics().size());
assertEquals(diagnostic, action.getDiagnostics().getFirst());
}
@Test
void documentLinksMapRangeAndTargetWithoutResolve() {
final var mapper = new Lsp4jProtocolMessageMapper();
final var unsupported = mapper.mapInitializeResult(serverDescription(false));
assertNull(unsupported.getCapabilities().getDocumentLinkProvider());
final var mapped = mapper.mapDocumentLinks(new BaselineDocumentLinks(List.of(
new BaselineDocumentLink("file:///tmp/a/source.pbs", 0, 19, 0, 25))));
assertEquals(1, mapped.size());
assertEquals("file:///tmp/a/source.pbs", mapped.getFirst().getTarget());
assertEquals(0, mapped.getFirst().getRange().getStart().getLine());
assertEquals(19, mapped.getFirst().getRange().getStart().getCharacter());
assertEquals(25, mapped.getFirst().getRange().getEnd().getCharacter());
assertNull(mapped.getFirst().getData());
assertTrue(mapper.mapDocumentLinks(BaselineDocumentLinks.empty()).isEmpty());
}
private static BaselineServerDescription serverDescription(final boolean documentLinksSupported) {
return new BaselineServerDescription(
"demo",
"1",
"pbs",
true,
true,
true,
true,
true,
true,
true,
true,
true,
documentLinksSupported,
List.of("demo-keyword"),
List.of(new BaselineSemanticHostProjection(
"vscode",
List.of(new BaselineSemanticHostProjectionEntry(
"demo-keyword",
"keyword",
List.of(),
"variable",
List.of())))),
List.of(new BaselineVisualTheme(
"demo-default",
"Demo Default",
new BaselineEditorPalette("#111111", "#222222", "#333333", "#444444", "#555555", "#666666", "#777777"),
List.of(new BaselineTokenStyle("demo-keyword", "#888888", false, false, false)))),
"demo-default");
}
}