implements PLN-0024

This commit is contained in:
2026-03-31 17:14:57 +01:00
parent 8dd3b58f39
commit bcc89dcfbd
25 changed files with 1049 additions and 13 deletions
@@ -0,0 +1,183 @@
package p.lsp.v1.internal;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import p.lsp.PrometeuLspProjectContext;
import p.lsp.PrometeuLspService;
import p.lsp.dtos.PrometeuLspDefinitionTargetDTO;
import p.lsp.messages.PrometeuLspAnalyzeDocumentRequest;
import p.lsp.messages.PrometeuLspDefinitionRequest;
import p.studio.vfs.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
final class PrometeuLspV1ServiceTest {
@TempDir
Path tempDir;
@Test
void analyzeDocumentUsesVfsOverlayForRequestedDocumentAndFilesystemFallbackForClosedFiles() throws Exception {
final Path projectRoot = createProject();
final Path mainFile = projectRoot.resolve("src/main.pbs");
final Path helperFile = projectRoot.resolve("src/helper.pbs");
Files.writeString(mainFile, "fn broken( -> void {}\n");
Files.writeString(helperFile, "fn helper() -> void {}\n");
final ProjectDocumentVfs delegate = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
final String overlaySource = """
fn helper_call() -> void
{
helper();
}
""";
final PrometeuLspService service = new PrometeuLspV1Service(
new PrometeuLspProjectContext("Example", "pbs", projectRoot),
new OverlayProjectDocumentVfs(delegate, mainFile, overlaySource));
final var analysis = service.analyzeDocument(new PrometeuLspAnalyzeDocumentRequest(mainFile));
assertTrue(
analysis.documentSymbols().stream().anyMatch(symbol -> symbol.name().equals("helper_call")),
analysis.toString());
assertTrue(
analysis.workspaceSymbols().stream().anyMatch(symbol ->
symbol.name().equals("helper") && symbol.documentPath().equals(normalize(helperFile))),
analysis.toString());
final int offset = overlaySource.indexOf("helper();");
final var definition = service.definition(new PrometeuLspDefinitionRequest(mainFile, offset));
final List<PrometeuLspDefinitionTargetDTO> targets = definition.targets();
assertEquals(1, targets.size());
assertEquals(normalize(helperFile), targets.get(0).documentPath());
assertEquals("helper", targets.get(0).name());
}
@Test
void analyzeDocumentSurfacesDiagnosticsWithoutAbortingSemanticRead() throws Exception {
final Path projectRoot = createProject();
final Path mainFile = projectRoot.resolve("src/main.pbs");
Files.writeString(mainFile, """
fn main( -> void
{
helper();
}
""");
final ProjectDocumentVfs vfs = new FilesystemProjectDocumentVfsFactory().open(projectContext(projectRoot));
final PrometeuLspService service = new PrometeuLspV1Service(
new PrometeuLspProjectContext("Example", "pbs", projectRoot),
vfs);
final var analysis = service.analyzeDocument(new PrometeuLspAnalyzeDocumentRequest(mainFile));
assertFalse(analysis.diagnostics().isEmpty(), analysis.toString());
assertTrue(analysis.diagnostics().stream().allMatch(diagnostic ->
diagnostic.documentPath().equals(normalize(mainFile))));
}
private Path createProject() throws Exception {
final Path src = Files.createDirectories(tempDir.resolve("src"));
Files.writeString(tempDir.resolve("prometeu.json"), """
{
"name": "Example",
"version": "1.0.0",
"language": "pbs",
"stdlib": "1",
"dependencies": []
}
""");
Files.writeString(src.resolve("mod.barrel"), "pub fn helper() -> void;\n");
return tempDir;
}
private VfsProjectContext projectContext(final Path projectRoot) {
return new VfsProjectContext("Example", "pbs", projectRoot);
}
private static Path normalize(final Path path) {
try {
return path.toAbsolutePath().normalize().toRealPath();
} catch (Exception exception) {
throw new IllegalStateException(exception);
}
}
private static final class OverlayProjectDocumentVfs implements ProjectDocumentVfs {
private final ProjectDocumentVfs delegate;
private final Path overlayPath;
private final String overlayContent;
private OverlayProjectDocumentVfs(
final ProjectDocumentVfs delegate,
final Path overlayPath,
final String overlayContent) {
this.delegate = delegate;
this.overlayPath = normalize(overlayPath);
this.overlayContent = overlayContent;
}
@Override
public VfsProjectContext projectContext() {
return delegate.projectContext();
}
@Override
public VfsProjectSnapshot snapshot() {
return delegate.snapshot();
}
@Override
public VfsProjectSnapshot refresh() {
return delegate.refresh();
}
@Override
public VfsProjectSnapshot refresh(final VfsRefreshRequest request) {
return delegate.refresh(request);
}
@Override
public VfsDocumentOpenResult openDocument(final Path path) {
final Path normalizedPath = normalize(path);
if (!overlayPath.equals(normalizedPath)) {
return delegate.openDocument(normalizedPath);
}
final VfsDocumentAccessContext accessContext = new VfsDocumentAccessContext(
normalizedPath,
"pbs",
true,
VfsDocumentAccessMode.READ_ONLY,
Map.of());
return new VfsTextDocument(
normalizedPath,
normalizedPath.getFileName().toString(),
"pbs",
overlayContent,
"LF",
false,
accessContext);
}
@Override
public void close() {
delegate.close();
}
private static Path normalize(final Path path) {
final Path normalized = path.toAbsolutePath().normalize();
try {
return Files.exists(normalized) ? normalized.toRealPath() : normalized;
} catch (Exception ignored) {
return normalized;
}
}
}
}