feat(pbs): resolve editorial chains and expose read-only external sources
This commit is contained in:
+63
@@ -0,0 +1,63 @@
|
||||
package p.studio.lsp.services;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import p.studio.lsp.messages.*;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
import java.nio.file.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Opt-in actual VS Code test against the development classpath (never installDist). */
|
||||
@EnabledIfEnvironmentVariable(named = "PROMETEU_VSCODE_DEV_TEST", matches = "1")
|
||||
class ExternalSourceDevelopmentHostTest {
|
||||
@TempDir Path temporary;
|
||||
|
||||
@Test void realEditorReadsBundledSourceWithoutMaterializingIt() throws Exception {
|
||||
Path repository = Path.of("").toAbsolutePath();
|
||||
while (!Files.isDirectory(repository.resolve("tools/vscode-extension"))) repository = repository.getParent();
|
||||
final Path root = Files.createDirectories(temporary.resolve("project")).toRealPath();
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.createDirectories(root.resolve(".vscode"));
|
||||
Files.writeString(root.resolve("prometeu.json"), """
|
||||
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[{"path":".workspace/repos/lib"}]}
|
||||
""");
|
||||
Files.writeString(root.resolve("src/main.pbs"), "import { Gfx } from @sdk:gfx;\nimport { answer } from @lib:shared;\nfn main() -> void { Gfx.clear(0); answer(); }\n");
|
||||
Files.writeString(root.resolve("src/mod.barrel"), "pub fn main() -> void;\n");
|
||||
final Path library = Files.createDirectories(root.resolve(".workspace/repos/lib/src")).getParent();
|
||||
Files.createDirectories(library.resolve("src/shared"));
|
||||
Files.writeString(library.resolve("prometeu.json"), """
|
||||
{"name":"lib","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
|
||||
""");
|
||||
Files.writeString(root.resolve(".workspace/repos/registry.json"), "{}");
|
||||
final String libraryText = "// á 😀\r\nfn answer() -> int { return helper(); }\r\nfn helper() -> int { return 42; }\r\n";
|
||||
Files.writeString(library.resolve("src/shared/main.pbs"), libraryText);
|
||||
Files.writeString(library.resolve("src/shared/mod.barrel"), "pub fn answer() -> int;\n");
|
||||
final var host = new TcpLspServerHost(LspServerConfiguration.builder()
|
||||
.endpoint(new LspServerEndpoint("127.0.0.1", 0)).build(), new LspProjectContext("dev", "pbs", root),
|
||||
new CompilerLanguageServiceBridge());
|
||||
host.start();
|
||||
try {
|
||||
Files.writeString(root.resolve(".vscode/settings.json"), "{\"prometeu.studio.port\":" + host.endpoint().orElseThrow().port() + "}");
|
||||
final String executable = System.getenv().getOrDefault("PROMETEU_VSCODE_EXECUTABLE", "/Applications/Visual Studio Code.app/Contents/MacOS/Code");
|
||||
final Path extension = repository.resolve("tools/vscode-extension");
|
||||
final var builder = new ProcessBuilder(executable, "--no-sandbox", "--disable-extensions", "--disable-workspace-trust",
|
||||
"--skip-welcome", "--skip-release-notes", "--user-data-dir=" + temporary.resolve("vscode-user"),
|
||||
"--extensions-dir=" + temporary.resolve("vscode-extensions"), "--extensionDevelopmentPath=" + extension,
|
||||
"--extensionTestsPath=" + extension.resolve("out/test/externalSources.js"), root.toString());
|
||||
builder.environment().remove("ELECTRON_RUN_AS_NODE");
|
||||
final Path log = Path.of("/tmp/pln141-vscode-development.log");
|
||||
final var process = builder.redirectErrorStream(true).redirectOutput(log.toFile()).start();
|
||||
try {
|
||||
assertTrue(process.waitFor(90, TimeUnit.SECONDS), "VS Code test timed out: " + log);
|
||||
assertEquals(0, process.exitValue(), () -> read(log));
|
||||
} finally { process.destroyForcibly(); }
|
||||
assertEquals(libraryText, Files.readString(library.resolve("src/shared/main.pbs")));
|
||||
try (var directories = Files.list(root.resolve(".workspace"))) {
|
||||
assertEquals(java.util.List.of("repos"), directories.map(p -> p.getFileName().toString()).toList(), "No editorial SDK cache");
|
||||
}
|
||||
} finally { host.close(); }
|
||||
}
|
||||
private static String read(Path path) { try { return Files.readString(path); } catch (Exception e) { return e.toString(); } }
|
||||
}
|
||||
+76
@@ -28,6 +28,82 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class CompilerLanguageServiceBridgeTest {
|
||||
|
||||
@Test
|
||||
void retainedDependencyUsesOriginalSemanticSnapshotAfterDiskChanges(@TempDir Path temporary) throws Exception {
|
||||
final Path root = temporary.toRealPath();
|
||||
final Path library = Files.createDirectories(root.resolve(".workspace/repos/lib/src")).getParent();
|
||||
Files.createDirectories(root.resolve("src"));
|
||||
Files.writeString(root.resolve("prometeu.json"), """
|
||||
{"name":"app","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game",
|
||||
"dependencies":[{"path":".workspace/repos/lib"}]}
|
||||
""");
|
||||
Files.createDirectories(library.resolve("src/shared"));
|
||||
Files.writeString(library.resolve("prometeu.json"), """
|
||||
{"name":"lib","version":"1.0.0","language":"pbs","stdlib":"1","target":"Game","dependencies":[]}
|
||||
""");
|
||||
Files.writeString(root.resolve(".workspace/repos/registry.json"), "{}");
|
||||
final String projectText = "import { answer } from @lib:shared;\nfn main() -> int { return answer(); }\n";
|
||||
final Path projectFile = Files.writeString(root.resolve("src/main.pbs"), projectText);
|
||||
final String old = "// á 😀\r\nfn answer() -> int { return original(); }\r\nfn original() -> int { return 1; }\r\n";
|
||||
final Path libraryFile = Files.writeString(library.resolve("src/shared/main.pbs"), old);
|
||||
Files.writeString(library.resolve("src/shared/mod.barrel"), "pub fn answer() -> int;\n");
|
||||
final var bridge = new CompilerLanguageServiceBridge();
|
||||
final var context = new LspProjectContext("snapshot", "pbs", root);
|
||||
try (var registry = new ExternalSourceRegistry()) {
|
||||
final String consumer = registry.connect(null, false);
|
||||
final String uri = bridge.navigation(context, projectFile.toUri().toString(), projectText, 1, 26, registry, consumer)
|
||||
.locations().getFirst().uri();
|
||||
final var retained = registry.source(consumer, uri);
|
||||
assertEquals(old, registry.read(consumer, uri).text());
|
||||
Files.writeString(libraryFile, "// different revision\n" + old.replace("original", "replacement"));
|
||||
final var target = bridge.navigation(context, uri, null, 1, 28, registry, consumer).locations().getFirst();
|
||||
assertEquals(2, target.startLine());
|
||||
assertEquals(old, registry.read(consumer, target.uri()).text());
|
||||
assertTrue(bridge.externalHover(context, uri, 1, 28, registry, consumer).markdown().contains("original"));
|
||||
assertTrue(retained.context() == registry.source(consumer, uri).context());
|
||||
registry.disconnect(consumer);
|
||||
registry.connect(consumer, false);
|
||||
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class,
|
||||
() -> registry.revalidate(consumer, uri, path -> bridge.currentSource(context, path)));
|
||||
org.junit.jupiter.api.Assertions.assertThrows(IllegalStateException.class, () -> registry.source(consumer, uri));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void externalNavigationRetainsBundledTextAndContinuesIntoHostDeclarations() {
|
||||
final Path root = findRepoRoot(Path.of("").toAbsolutePath()).resolve("test-projects/main");
|
||||
final var context = new LspProjectContext("main", "pbs", root);
|
||||
final var bridge = new CompilerLanguageServiceBridge();
|
||||
final String source = "import { Gfx } from @sdk:gfx;\nfn frame() -> void { Gfx.clear(0); }\n";
|
||||
final var position = new DocumentPositionMapper(source).positionOf(source.indexOf("clear"));
|
||||
try (var registry = new ExternalSourceRegistry()) {
|
||||
final String consumer = registry.connect(null, false);
|
||||
final var locations = bridge.navigation(context, root.resolve("src/main.pbs").toUri().toString(), source,
|
||||
position.line(), position.character(), registry, consumer).locations();
|
||||
assertEquals(1, locations.size());
|
||||
final String uri = locations.getFirst().uri();
|
||||
assertTrue(uri.startsWith("prometeu-source:"));
|
||||
final var content = registry.read(consumer, uri);
|
||||
assertEquals("bundled", content.descriptor().origin());
|
||||
assertTrue(content.descriptor().readOnly());
|
||||
assertTrue(content.text().contains("LowGfx.clear"));
|
||||
final int call = content.text().indexOf("LowGfx.clear") + "LowGfx.".length();
|
||||
final var hostUse = new DocumentPositionMapper(content.text()).positionOf(call);
|
||||
assertTrue(bridge.externalHover(context, uri, hostUse.line(), hostUse.character(), registry, consumer)
|
||||
.markdown().contains("clear"));
|
||||
final var host = bridge.navigation(context, uri, null, hostUse.line(), hostUse.character(), registry, consumer)
|
||||
.locations().getFirst();
|
||||
final var hostContent = registry.read(consumer, host.uri());
|
||||
final String[] lines = hostContent.text().split("\n");
|
||||
assertTrue(lines[host.startLine()].contains("clear"), lines[host.startLine()]);
|
||||
assertFalse(bridge.externalSemanticTokens(context, uri, registry, consumer).tokens().isEmpty());
|
||||
final var colorUse = new DocumentPositionMapper(content.text()).positionOf(content.text().indexOf("color: Color") + 7);
|
||||
final String coreUri = bridge.navigation(context, uri, null, colorUse.line(), colorUse.character(), registry, consumer)
|
||||
.locations().getFirst().uri();
|
||||
assertTrue(registry.read(consumer, coreUri).descriptor().name().replace('\\', '/').contains("core/color"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void editorialEndpointsReturnNeutralResponsesWhenLanguageServiceIsAbsent() {
|
||||
final var provider = new CompileOnlyFrontendProvider();
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
package p.studio.lsp.services.compiler;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.compiler.services.FrontendEditorialContext;
|
||||
import p.studio.compiler.services.FrontendSourceDocument;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Optional;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ExternalSourceRegistryTest {
|
||||
private FrontendSourceDocument source(String text) {
|
||||
return new FrontendSourceDocument(Path.of("/virtual/stdlib/sdk/main.pbs"), text, "bundled", "sdk/main.pbs", "pbs", true,
|
||||
new FrontendEditorialContext(new Object(), null, new Object()));
|
||||
}
|
||||
|
||||
@Test void readersShareRevisionWithoutSharingReleaseAndUnknownIdentitiesAreRejected() {
|
||||
try (var registry = new ExternalSourceRegistry(); var otherProject = new ExternalSourceRegistry()) {
|
||||
final var source = source("// á 😀\r\nfn f() {}\r\n");
|
||||
final String one = registry.connect(null, false), two = registry.connect(null, false);
|
||||
final String uri = registry.register(one, source);
|
||||
assertEquals(uri, registry.register(two, source));
|
||||
registry.retain(one, uri);
|
||||
registry.release(one, uri);
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(one, uri));
|
||||
assertSame(source.context(), registry.source(two, uri).context());
|
||||
assertEquals(source.text(), registry.read(two, uri).text());
|
||||
assertThrows(IllegalStateException.class, () -> otherProject.read(otherProject.connect(null, false), uri));
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(two, "file:///etc/passwd"));
|
||||
registry.release(two, uri);
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(two, uri));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void disconnectedDocumentsRequireRevalidationAndChangedRevisionCannotReplaceText() {
|
||||
try (var registry = new ExternalSourceRegistry()) {
|
||||
final String consumer = registry.connect(null, false);
|
||||
final var old = source("old");
|
||||
final String uri = registry.register(consumer, old);
|
||||
registry.retain(consumer, uri);
|
||||
registry.disconnect(consumer);
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(consumer, uri));
|
||||
registry.connect(consumer, false);
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(consumer, uri));
|
||||
assertThrows(IllegalStateException.class, () -> registry.revalidate(consumer, uri, p -> Optional.of(source("new"))));
|
||||
assertEquals("old", registry.revalidate(consumer, uri, p -> Optional.of(source("old"))).text());
|
||||
}
|
||||
}
|
||||
|
||||
@Test void taskConsumerOutlivesTransportButNotTaskOrProject() {
|
||||
final var registry = new ExternalSourceRegistry();
|
||||
final String consumer = registry.connect(null, true);
|
||||
final var snapshot = source("source");
|
||||
final String uri = registry.register(consumer, snapshot);
|
||||
registry.retain(consumer, uri);
|
||||
registry.disconnect(consumer);
|
||||
registry.connect(consumer, true);
|
||||
assertThrows(IllegalStateException.class, () -> registry.source(consumer, uri));
|
||||
registry.revalidate(consumer, uri, p -> Optional.of(source("source")));
|
||||
assertSame(snapshot, registry.source(consumer, uri));
|
||||
registry.release(consumer, null);
|
||||
assertThrows(IllegalStateException.class, () -> registry.connect(consumer, true));
|
||||
final String next = registry.connect(null, true);
|
||||
registry.register(next, snapshot);
|
||||
registry.close();
|
||||
assertThrows(IllegalStateException.class, () -> registry.read(next, uri));
|
||||
}
|
||||
|
||||
@Test void cancelledOrUnopenedNavigationDoesNotRetainASnapshot() {
|
||||
try (var registry = new ExternalSourceRegistry()) {
|
||||
final String consumer = registry.connect(null, true);
|
||||
final String uri = registry.register(consumer, source("unused"));
|
||||
registry.disconnect(consumer);
|
||||
registry.connect(consumer, true);
|
||||
assertThrows(IllegalStateException.class, () -> registry.revalidate(consumer, uri, p -> Optional.of(source("unused"))));
|
||||
}
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
package p.studio.lsp.services.protocol;
|
||||
|
||||
import org.eclipse.lsp4j.*;
|
||||
import org.eclipse.lsp4j.jsonrpc.ResponseErrorException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import p.studio.lsp.messages.LspProjectContext;
|
||||
import p.studio.lsp.services.compiler.CompilerLanguageServiceBridge;
|
||||
import p.studio.lsp.services.protocol.mapping.Lsp4jProtocolMessageMapper;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ExternalWriteBoundaryTest {
|
||||
@Test void externalEditsAreRefusedEvenWithoutARegisteredConsumer() {
|
||||
final var service = new PrometeuTextDocumentService(new LspProjectContext("test", "pbs", Path.of(".")),
|
||||
new CompilerLanguageServiceBridge(), new Lsp4jProtocolMessageMapper());
|
||||
final String uri = "prometeu-source://session/revision/main.pbs";
|
||||
final var id = new TextDocumentIdentifier(uri);
|
||||
final var versioned = new VersionedTextDocumentIdentifier(uri, 2);
|
||||
final var position = new Position(0, 0);
|
||||
final List<org.junit.jupiter.api.function.Executable> attempts = List.of(
|
||||
() -> service.didChange(new DidChangeTextDocumentParams(versioned, List.of(new TextDocumentContentChangeEvent("changed")))),
|
||||
() -> service.didSave(new DidSaveTextDocumentParams(id)),
|
||||
() -> service.formatting(new DocumentFormattingParams(id, new FormattingOptions(4, true))),
|
||||
() -> service.rename(new RenameParams(id, position, "changed")),
|
||||
() -> service.prepareRename(new PrepareRenameParams(id, position)),
|
||||
() -> service.codeAction(new CodeActionParams(id, new Range(position, position), new CodeActionContext(List.of()))));
|
||||
for (var attempt : attempts) assertEquals(-32010, assertThrows(ResponseErrorException.class, attempt).getResponseError().getCode());
|
||||
assertTrue(service.references(new ReferenceParams(id, position, new ReferenceContext(true))).join().isEmpty());
|
||||
assertTrue(service.documentLink(new DocumentLinkParams(id)).join().isEmpty());
|
||||
assertTrue(service.prepareCallHierarchy(new CallHierarchyPrepareParams(id, position)).join().isEmpty());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user