From b5232e0124f97cac2c0d0b5e2d6492e67adfe285 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:19 +0100 Subject: [PATCH 001/324] Preserve module repository read failures without blocking usable candidates - Preserve module repository read failures and retry behavior. - Retain repository read failures without blocking usable candidates. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../main/java/org/xvm/asm/DirRepository.java | 39 ++++- .../main/java/org/xvm/asm/FileRepository.java | 67 +++++--- .../java/org/xvm/asm/LinkedRepository.java | 19 +- .../java/org/xvm/asm/ModuleRepository.java | 20 +++ .../org/xvm/asm/DirRepositoryFailureTest.java | 162 ++++++++++++++++++ .../xvm/asm/FileRepositoryFailureTest.java | 104 +++++++++++ 6 files changed, 375 insertions(+), 36 deletions(-) create mode 100644 javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java diff --git a/javatools/src/main/java/org/xvm/asm/DirRepository.java b/javatools/src/main/java/org/xvm/asm/DirRepository.java index 1615f48d8d8..aea8d3b2d59 100644 --- a/javatools/src/main/java/org/xvm/asm/DirRepository.java +++ b/javatools/src/main/java/org/xvm/asm/DirRepository.java @@ -80,6 +80,17 @@ public synchronized Set getModuleNames() { return Collections.unmodifiableSet(modulesByName.keySet()); } + @Override + public synchronized Map getReadFailures() { + Map failures = new HashMap<>(); + for (ModuleInfo info : modulesByFile.values()) { + if (info.readFailure != null) { + failures.put(info.file, info.readFailure); + } + } + return Map.copyOf(failures); + } + @Override public synchronized ModuleStructure loadModule(String sModule) { ensureCache(); @@ -155,6 +166,8 @@ protected void ensureCache() { } private void rebuildCache(File[] files) { + // An unexpected failure must not leave a partially rebuilt cache marked current. + lastScan = 0; Map oldModulesByFile = modulesByFile; boolean fWriteCache = false; if (oldModulesByFile.isEmpty()) { @@ -174,7 +187,8 @@ private void rebuildCache(File[] files) { fWriteCache |= files.length != oldModulesByFile.size(); for (File file : files) { ModuleInfo info = oldModulesByFile.get(file); - if (info == null || info.timestamp != file.lastModified() || info.size != file.length()) { + if (info == null || info.hasReadFailure() + || info.timestamp != file.lastModified() || info.size != file.length()) { // build a new one to cache info = createModuleInfo(file); fWriteCache = true; @@ -341,8 +355,11 @@ private File getCacheFile() { * @return true if the cache is still good, or false if it needs to be rebuilt */ private boolean isCacheValid() { - // only scan once a second (at the most) - if (System.currentTimeMillis() < lastScan + 1000) { + // Failed candidates are retried once per second, or immediately when metadata changes. + // Retaining their cause avoids treating the intervening cached result as unexplained absence. + boolean recent = System.currentTimeMillis() < lastScan + 1000; + boolean failed = modulesByFile.values().stream().anyMatch(ModuleInfo::hasReadFailure); + if (recent && !failed) { return true; } @@ -358,7 +375,7 @@ private boolean isCacheValid() { } } - return true; + return !failed || recent; } // ----- inner class: ModuleInfo --------------------------------------------------------------- @@ -393,12 +410,17 @@ private ModuleInfo(File file, String name, long timestamp, long size, boolean er ModuleStructure tryLoad() { try { FileStructure struct = new FileStructure(file); + readFailure = null; return struct.getModule(); - } catch (Exception e) { - System.out.println("Error loading module from file: " + file + "; " + e.getMessage()); + } catch (IOException e) { + readFailure = e; + return null; } + } - return null; + boolean hasReadFailure() { + // A failed entry read from the disk cache must be retried to recover its cause. + return err || readFailure != null; } ModuleStructure ensureModule() { @@ -425,6 +447,9 @@ ModuleStructure ensureModule() { * reload it as necessary. */ private transient ModuleStructure module; + + /** The original cause, retained separately from available modules. */ + private IOException readFailure; } // ----- constants ----------------------------------------------------------------------------- diff --git a/javatools/src/main/java/org/xvm/asm/FileRepository.java b/javatools/src/main/java/org/xvm/asm/FileRepository.java index 233b95ee58d..88d46d88da2 100644 --- a/javatools/src/main/java/org/xvm/asm/FileRepository.java +++ b/javatools/src/main/java/org/xvm/asm/FileRepository.java @@ -62,6 +62,11 @@ public synchronized Set getModuleNames() { return validateCache() ? cachedVersionsByName.keySet() : Set.of(); } + @Override + public synchronized Map getReadFailures() { + return readFailure == null ? Map.of() : Map.of(file, readFailure); + } + @Override public synchronized VersionTree getAvailableVersions(String sModule) { return validateCache() ? cachedVersionsByName.get(sModule) : null; @@ -228,31 +233,40 @@ private static File moduleFile(File file) { * Make sure that the cache is up to date. */ private boolean validateCache() { - // assume cache is up-to-date if it has been checked recently; assumption is that activity - // comes in bursts; also assume cache is up-to-date if nothing appears to have changed - if (System.currentTimeMillis() < lastScan + 60/*ms*/ - || timestamp == file.lastModified() && size == file.length()) { + // Successful reads keep the usual burst cache. Failed reads retain their cause and are + // retried after the burst, or immediately if the file metadata changes. + long now = System.currentTimeMillis(); + boolean unchanged = timestamp == file.lastModified() && size == file.length(); + boolean recent = now < lastScan + 60/*ms*/; + if (readFailure == null ? recent || unchanged : recent && unchanged) { return cacheOk; } - // cache is not up-to-date; clear whatever was cached before - timestamp = file.lastModified(); - size = file.length(); + boolean retryFailedRead = readFailure != null; cachedVersionsByName = Map.of(); cachedFileStructure = null; cachedModuleStructures = null; cacheOk = false; - lastScan = System.currentTimeMillis(); - // load the cache if possible if (file.exists() && file.isFile() && file.canRead()) { - // read just the module contents of the FileStructure (not the entire FileStructure) - FileInfo info = readFileInfo(); + FileInfo info; + if (retryFailedRead) { + // A successful header alone cannot clear an earlier payload failure. + cachedFileStructure = readFileStructure(); + info = cachedFileStructure == null ? null : cachedFileStructure.buildFileInfo(); + } else { + info = readFileInfo(); + } if (info != null) { cachedVersionsByName = info.modules(); cacheOk = true; } + } else { + readFailure = null; } + timestamp = file.lastModified(); + size = file.length(); + lastScan = now; return cacheOk; } @@ -280,37 +294,36 @@ private boolean ensureFileStructure() { if (validateCache() && cachedFileStructure == null) { if ((cachedFileStructure = readFileStructure()) == null) { cacheOk = false; + lastScan = System.currentTimeMillis(); } } return cacheOk; } /** - * @return the FileInfo freshly read from the file system, or `null` on any failure + * @return the FileInfo freshly read from the file system, or null with a retained read failure */ private FileInfo readFileInfo() { try { - return FileStructure.readFileInfo(file); - } catch (Exception e) { - if (!reportedFileStructureError) { - reportedFileStructureError = true; - System.out.println("Error loading FileInfo from file: " + file + "; " + e.getMessage()); - } + FileInfo info = FileStructure.readFileInfo(file); + readFailure = null; + return info; + } catch (IOException e) { + readFailure = e; return null; } } /** - * @return the FileStructure freshly read from the file system, or `null` on any failure + * @return the FileStructure freshly read from the file system, or null with a retained read failure */ private FileStructure readFileStructure() { try { - return new FileStructure(file); - } catch (Exception e) { - if (!reportedFileInfoError) { - reportedFileInfoError = true; - System.out.println("Error loading FileStructure from file: " + file + "; " + e.getMessage()); - } + FileStructure structure = new FileStructure(file); + readFailure = null; + return structure; + } catch (IOException e) { + readFailure = e; return null; } } @@ -358,8 +371,8 @@ private Map ensureModuleCache() { private long lastScan; private boolean cacheOk; - private boolean reportedFileInfoError; - private boolean reportedFileStructureError; + /** The most recent unsuccessful read; cleared by a successful retry or removal. */ + private IOException readFailure; /** * Cached file contents: module names and versions thereof. diff --git a/javatools/src/main/java/org/xvm/asm/LinkedRepository.java b/javatools/src/main/java/org/xvm/asm/LinkedRepository.java index ee4e4e41bdc..9ab32deedd8 100644 --- a/javatools/src/main/java/org/xvm/asm/LinkedRepository.java +++ b/javatools/src/main/java/org/xvm/asm/LinkedRepository.java @@ -1,9 +1,12 @@ package org.xvm.asm; +import java.io.File; import java.io.IOException; import java.util.Arrays; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeSet; @@ -82,12 +85,24 @@ public Set getModuleNames() { return names; } + @Override + public Map getReadFailures() { + Map failures = new HashMap<>(); + for (ModuleRepository repo : repos) { + failures.putAll(repo.getReadFailures()); + } + return Map.copyOf(failures); + } + @Override public VersionTree getAvailableVersions(String sModule) { VersionTree vers = new VersionTree<>(); for (ModuleRepository repo : repos) { - for (Version ver : repo.getAvailableVersions(sModule)) { - vers.put(ver, true); + VersionTree available = repo.getAvailableVersions(sModule); + if (available != null) { + for (Version ver : available) { + vers.put(ver, true); + } } } return vers; diff --git a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java index 284fa3ef99b..ef42aaa2c93 100644 --- a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java +++ b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java @@ -1,7 +1,9 @@ package org.xvm.asm; +import java.io.File; import java.io.IOException; +import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -51,6 +53,24 @@ default Set getModuleNames(String sDomain) { */ Set getModuleNames(); + /** + * Obtain the file read failures retained by this repository's most recent accesses. + * + *

An unreadable candidate does not stop a search: other files or repositories may still + * supply the requested module. Hosts can inspect these failures when explaining an unsuccessful + * lookup. Their presence alone does not mean that a lookup or compilation failed, and a file's + * module identity may be unknown if its header could not be read. + * + *

This method does not initiate I/O. File repositories replace or clear retained failures + * when subsequent accesses retry the file or observe its removal. Implementations that do not + * retain file read failures return an empty map. + * + * @return an immutable snapshot mapping unreadable files to their original I/O exceptions + */ + default Map getReadFailures() { + return Map.of(); + } + /** * Determine the set of available versions of the specified module. * diff --git a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java new file mode 100644 index 00000000000..30ff7436769 --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java @@ -0,0 +1,162 @@ +package org.xvm.asm; + +import java.io.IOException; + +import java.nio.ByteBuffer; +import java.nio.file.Files; +import java.nio.file.Path; + +import java.time.Duration; + +import java.util.Set; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class DirRepositoryFailureTest { + @TempDir + Path directory; + + @Test + void corruptModuleRetainsItsFailureAndCanBeRepaired() throws IOException { + Path file = directory.resolve("broken.xtc"); + Files.writeString(file, "not a compiled module"); + var repository = new DirRepository(directory.toFile(), true); + + assertTrue(repository.getReadFailures().isEmpty(), "inspection must not initiate a scan"); + assertTrue(repository.getModuleNames().isEmpty()); + assertNull(repository.loadModule("Broken")); + var failures = repository.getReadFailures(); + assertEquals(Set.of(file.toFile()), failures.keySet()); + assertNotNull(failures.get(file.toFile())); + assertThrows(UnsupportedOperationException.class, failures::clear); + + new FileStructure("Broken").writeTo(file.toFile()); + assertNotNull(repository.loadModule("Broken")); + assertTrue(repository.getReadFailures().isEmpty()); + assertEquals(1, failures.size(), "an earlier snapshot must not change"); + } + + @Test + void incompatibleCandidateDoesNotHideAUsableModuleInTheSameDirectory() throws IOException { + Path old = directory.resolve("Old.xtc"); + writeUnsupportedModule(old, "Old"); + new FileStructure("Good").writeTo(directory.resolve("Good.xtc").toFile()); + var repository = new DirRepository(directory.toFile(), true); + + assertNotNull(repository.loadModule("Good")); + assertEquals(Set.of("Good"), repository.getModuleNames()); + assertNull(repository.loadModule("Unknown")); + assertTrue(repository.getReadFailures().get(old.toFile()).getMessage() + .contains("Unsupported .xtc version")); + } + + @ParameterizedTest + @ValueSource(booleans = {false, true}) + void incompatibleCandidateDoesNotBlockALaterRepository(boolean singleFile) throws IOException { + Path first = Files.createDirectory(directory.resolve("first")); + Path second = Files.createDirectory(directory.resolve("second")); + Path old = first.resolve("Good.xtc"); + writeUnsupportedModule(old, "Good"); + var version = new Version("1.0"); + var good = new FileStructure("Good"); + good.getModule().setVersion(version); + good.writeTo(second.resolve("Good.xtc").toFile()); + ModuleRepository candidate = singleFile + ? new FileRepository(old.toFile(), true) + : new DirRepository(first.toFile(), true); + var repository = new LinkedRepository(candidate, new DirRepository(second.toFile(), true)); + + assertNotNull(repository.loadModule("Good")); + assertEquals(Set.of("Good"), repository.getModuleNames()); + assertTrue(repository.getAvailableVersions("Good").contains(version)); + assertNotNull(repository.loadModule("Good", version, true)); + var failures = repository.getReadFailures(); + assertEquals(Set.of(old.toFile()), failures.keySet()); + assertTrue(failures.get(old.toFile()).getMessage().contains("Unsupported .xtc version")); + assertThrows(UnsupportedOperationException.class, failures::clear); + } + + @Test + void unsuccessfulLinkedSearchRetainsFailuresFromAllCandidates() throws IOException { + Path first = Files.createDirectory(directory.resolve("first")); + Path second = Files.createDirectory(directory.resolve("second")); + Path old = first.resolve("Old.xtc"); + Path broken = second.resolve("Broken.xtc"); + writeUnsupportedModule(old, "Old"); + Files.writeString(broken, "not a compiled module"); + var repository = new LinkedRepository(new DirRepository(first.toFile(), true), + new DirRepository(second.toFile(), true)); + + assertNull(repository.loadModule("Missing")); + assertEquals(Set.of(old.toFile(), broken.toFile()), repository.getReadFailures().keySet()); + } + + @Test + void cachedFailureIsRereadByANewRepositoryInstance() throws IOException { + Path file = directory.resolve("Broken.xtc"); + Files.writeString(file, "not a compiled module"); + assertTrue(new DirRepository(directory.toFile(), true).getModuleNames().isEmpty()); + var repository = new DirRepository(directory.toFile(), true); + + assertNull(repository.loadModule("Broken")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + new FileStructure("Broken").writeTo(file.toFile()); + assertNotNull(repository.loadModule("Broken")); + assertTrue(repository.getReadFailures().isEmpty()); + } + + @Test + void failedCandidateIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException { + Path file = directory.resolve("Good.xtc"); + new FileStructure("Good").writeTo(file.toFile()); + byte[] valid = Files.readAllBytes(file); + writeUnsupportedModule(file, "Good"); + var timestamp = Files.getLastModifiedTime(file); + var repository = new DirRepository(directory.toFile(), true); + assertNull(repository.loadModule("Good")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + + Files.write(file, valid); + Files.setLastModifiedTime(file, timestamp); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (repository.loadModule("Good") == null) { + Thread.sleep(10); + } + }); + assertTrue(repository.getReadFailures().isEmpty()); + } + + @Test + void removingAnUnreadableFileClearsItsFailure() throws IOException { + Path file = directory.resolve("Broken.xtc"); + Files.writeString(file, "not a compiled module"); + var repository = new DirRepository(directory.toFile(), true); + assertNull(repository.loadModule("Broken")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + + Files.delete(file); + assertTrue(repository.getModuleNames().isEmpty()); + assertTrue(repository.getReadFailures().isEmpty()); + } + + private static void writeUnsupportedModule(Path file, String name) throws IOException { + new FileStructure(name).writeTo(file.toFile()); + byte[] contents = Files.readAllBytes(file); + // The binary header starts with three ints: magic, major version, minor version. + int minor = FileStructure.getToolMinorVersion() - 1; + assertFalse(FileStructure.isFileVersionSupported(FileStructure.getToolMajorVersion(), minor)); + ByteBuffer.wrap(contents).putInt(2 * Integer.BYTES, minor); + Files.write(file, contents); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java new file mode 100644 index 00000000000..ea721767797 --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java @@ -0,0 +1,104 @@ +package org.xvm.asm; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +import java.nio.file.Files; +import java.nio.file.Path; + +import java.time.Duration; + +import java.util.Arrays; +import java.util.Set; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class FileRepositoryFailureTest { + @TempDir + Path directory; + + @Test + void invalidHeaderRetainsItsCauseAndCanBeRepaired() throws IOException { + Path file = directory.resolve("broken.xtc"); + Files.writeString(file, "not a compiled module"); + var repository = new FileRepository(file.toFile(), true); + + assertTrue(repository.getReadFailures().isEmpty(), "inspection must not initiate a read"); + assertTrue(repository.getModuleNames().isEmpty()); + assertNull(repository.loadModule("Broken")); + var failures = repository.getReadFailures(); + assertEquals(Set.of(file.toFile()), failures.keySet()); + assertNotNull(failures.get(file.toFile())); + assertThrows(UnsupportedOperationException.class, failures::clear); + + new FileStructure("Broken").writeTo(file.toFile()); + assertNotNull(repository.loadModule("Broken")); + assertTrue(repository.getReadFailures().isEmpty()); + assertEquals(1, failures.size(), "an earlier snapshot must not change"); + } + + @Test + void truncatedModuleRetainsItsCauseAfterItsHeaderWasRead() throws IOException { + Path file = directory.resolve("broken.xtc"); + new FileStructure("Broken").writeTo(file.toFile()); + byte[] contents = Files.readAllBytes(file); + var input = new ByteArrayInputStream(contents); + FileStructure.readFileInfo(input); + Files.write(file, Arrays.copyOf(contents, contents.length - input.available())); + var repository = new FileRepository(file.toFile(), true); + + assertTrue(repository.getModuleNames().contains("Broken")); + assertNull(repository.loadModule("Broken")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNull(repository.loadModule("Broken")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + + Files.write(file, contents); + assertNotNull(repository.loadModule("Broken")); + assertTrue(repository.getReadFailures().isEmpty()); + } + + @Test + void failedReadIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException { + Path file = directory.resolve("Good.xtc"); + new FileStructure("Good").writeTo(file.toFile()); + byte[] valid = Files.readAllBytes(file); + byte[] broken = valid.clone(); + broken[0] = 0; // Invalidate the magic value without changing the file size. + Files.write(file, broken); + var timestamp = Files.getLastModifiedTime(file); + var repository = new FileRepository(file.toFile(), true); + assertNull(repository.loadModule("Good")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + + Files.write(file, valid); + Files.setLastModifiedTime(file, timestamp); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (repository.loadModule("Good") == null) { + Thread.sleep(10); + } + }); + assertTrue(repository.getReadFailures().isEmpty()); + } + + @Test + void removingAnUnreadableFileClearsItsFailure() throws IOException { + Path file = directory.resolve("Broken.xtc"); + Files.writeString(file, "not a compiled module"); + var repository = new FileRepository(file.toFile(), true); + assertNull(repository.loadModule("Broken")); + assertNotNull(repository.getReadFailures().get(file.toFile())); + + Files.delete(file); + assertTrue(repository.getModuleNames().isEmpty()); + assertTrue(repository.getReadFailures().isEmpty()); + } +} From 60ee89ce04a5a0bffdc189d4b75d06c5a23b0996 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:19 +0100 Subject: [PATCH 002/324] Separate diagnostic reporting from listener abort policy Introduce source sites, collecting listeners, named budgets and silence reasons while preserving branch and merge behavior. Keep null and ambient ownership policy for later slices; document the planned 0.5.0 source and binary break. Master's own reporting sites keep the old form here; the next commit migrates them. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 41 ++ .../src/main/java/org/xvm/asm/Annotation.java | 3 +- .../src/main/java/org/xvm/asm/ErrorList.java | 50 +- .../main/java/org/xvm/asm/ErrorListener.java | 469 ++++++++++++++++-- .../main/java/org/xvm/asm/XvmStructure.java | 4 +- .../asm/constants/ImmutableTypeConstant.java | 3 +- .../src/main/java/org/xvm/compiler/Lexer.java | 3 +- .../main/java/org/xvm/compiler/Parser.java | 15 +- .../src/main/java/org/xvm/compiler/Token.java | 7 +- .../java/org/xvm/compiler/ast/AstNode.java | 15 +- .../src/main/java/org/xvm/tool/Launcher.java | 4 +- .../main/java/org/xvm/tool/ModuleInfo.java | 4 +- .../org/xvm/asm/ErrorListenerAbortTest.java | 80 +++ .../org/xvm/asm/ErrorListenerBranchTest.java | 144 ++++++ .../xvm/asm/ErrorListenerMigrationTest.java | 91 ++++ .../org/xvm/asm/ErrorListenerSilenceTest.java | 111 +++++ .../org/xvm/asm/ErrorListenerSiteTest.java | 121 +++++ .../xvm/tool/LauncherErrorHandlingTest.java | 3 +- 18 files changed, 1097 insertions(+), 71 deletions(-) create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerBranchTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerSiteTest.java diff --git a/javatools/README.md b/javatools/README.md index 1b0f6ee22dc..6c5d2dbf37c 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -31,6 +31,47 @@ Status: Suitable for use * AST nodes are multi-pass compiled (with optional re-queuing) via `org.xvm.compiler.ast.StageManager` +## Error-listener API migration (breaking change) + +This change requires recompiling Java clients and listener implementations. `ErrorListener.log` +now returns `void`, including its legacy positional overloads. The forwarding `log` helpers on +`XvmStructure`, `AstNode` and `Token` also return `void`. Java cannot retain a compatibility overload +that differs only by return type. These source and binary compatibility breaks are planned for **0.5.0**; Java clients and listener +implementations must be migrated and recompiled when adopting that release. + +Report a diagnostic, then ask the listener whether the work should stop: + +```java +ErrorListener errors = ErrorListener.collecting(hostDiagnostics::add); +errors.error(code, ErrorListener.in(source, start, end), arguments); +if (errors.isAbortDesired()) { + return; +} +``` + +`collecting` remembers severity and reported codes and forwards every report without deduplication. +A bare `ErrorListener` lambda only implements reporting and inherits state queries that always +answer false. Use `ErrorList` when repeated diagnostics should collapse or a count budget is needed: +its default constructor uses `DEFAULT_MAX_ERRORS`; `FIRST_ERROR` and `UNLIMITED` name other policies. +A fatal diagnostic requests an abort even with an unlimited budget. + +`Site.In`, `Site.At` and `Site.None` describe source spans, structures and unpositioned diagnostics. +The severity helpers accept message arguments as trailing varargs. The old positional forms still +work after recompilation, including source re-anchoring through a branch. The legacy `BLACKHOLE` +constant/class remain while callers migrate to `silent(Silence.PROBE/CASCADE/DISCARD)` or a listener's +`silence(...)` wrapper. A derived silence keeps its parent's abort request. + +A custom listener's default branch buffers without an arbitrary one-error budget and observes the +parent's abort state. `ErrorList` branches also retain their configured budget. Merging an unbranched +listener returns itself. `tee` forwards reports to both sinks and honors either sink's abort state. +Callbacks must be serialized by the host; these helpers do not make compiler execution concurrent. + +`RUNTIME` now prints diagnostics and records an abort request for ERROR/FATAL; reporting no longer +throws. Runtime callers that depended on that exception must explicitly check `isAbortDesired()` +and choose their own failure handling. Parser and lexer reporting sites have been migrated to that +sequence. This slice retains the existing null/ambient listener policy; later ownership changes +must document their additional compatibility effects separately. + ## Assembler Status: Suitable for use diff --git a/javatools/src/main/java/org/xvm/asm/Annotation.java b/javatools/src/main/java/org/xvm/asm/Annotation.java index f32d829e8ea..153d3e1dbce 100644 --- a/javatools/src/main/java/org/xvm/asm/Annotation.java +++ b/javatools/src/main/java/org/xvm/asm/Annotation.java @@ -321,8 +321,9 @@ public boolean validate(ErrorListener errs) { // it must be an annotation type if (getAnnotationType().getExplicitClassFormat() != Component.Format.ANNOTATION) { - fHalt |= log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, + log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, getAnnotationClass().getValueString()); + fHalt |= ensureErrorListener(errs).isAbortDesired(); } return fHalt; diff --git a/javatools/src/main/java/org/xvm/asm/ErrorList.java b/javatools/src/main/java/org/xvm/asm/ErrorList.java index 7c6133e7a71..e683f8c7bc1 100644 --- a/javatools/src/main/java/org/xvm/asm/ErrorList.java +++ b/javatools/src/main/java/org/xvm/asm/ErrorList.java @@ -16,10 +16,43 @@ public class ErrorList implements ErrorListener { // ----- constructors -------------------------------------------------------------------------- + /** + * Construct a list that tolerates {@link #DEFAULT_MAX_ERRORS} serious errors, for a caller + * with no reason to choose a number. A caller that has one says so with the other + * constructor, naming {@link #UNLIMITED} or {@link #FIRST_ERROR} where those are what it + * means. + */ + public ErrorList() { + this(DEFAULT_MAX_ERRORS); + } + + /** + * @param cMaxErrors the number of serious errors to tolerate before asking for the process to + * be abandoned, or {@link #UNLIMITED} to tolerate any number + */ public ErrorList(int cMaxErrors) { f_cMaxErrors = cMaxErrors; } + /** + * Tolerate any number of serious errors: only a FATAL asks for the process to be abandoned. + */ + public static final int UNLIMITED = 0; + + /** + * The budget for a caller that wants to stop at the first serious error. + */ + public static final int FIRST_ERROR = 1; + + /** + * How many serious errors to tolerate when the caller has no reason to choose a number. + * + *

Enough that a file with a genuine spread of problems reports them all, and few enough that + * source which has gone badly wrong - a mismatched brace early on, say - stops rather than + * producing a page of consequences. + */ + public static final int DEFAULT_MAX_ERRORS = 100; + // ----- ErrorListener methods ----------------------------------------------------------------- @Override @@ -28,7 +61,7 @@ public ErrorListener branch(AstNode node) { } @Override - public boolean log(ErrorInfo err) { + public void log(ErrorInfo err) { String uid = err.genUID(); if (f_setUID.add(uid)) { // remember the highest severity encountered @@ -46,8 +79,6 @@ public boolean log(ErrorInfo err) { ++m_cErrors; } } - - return isAbortDesired(); } @Override @@ -168,11 +199,14 @@ public ErrorListener branch(AstNode node) { } @Override - public boolean log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) { - return f_node == null - ? super.log(severity, sCode, aoParam, xs) - : log(severity, sCode, aoParam, - f_node.getSource(), f_node.getStartPosition(), f_node.getEndPosition()); + public void log(Severity severity, String sCode, Site site, Object... aoParam) { + // a branch taken for a particular node re-anchors at that node: a diagnostic raised + // against a structure carries no source location of its own, and the node is the + // location the brancher knew about + if (f_node != null && site instanceof Site.At) { + site = ErrorListener.in(f_node.getSource(), f_node.getStartPosition(), f_node.getEndPosition()); + } + super.log(severity, sCode, site, aoParam); } @Override diff --git a/javatools/src/main/java/org/xvm/asm/ErrorListener.java b/javatools/src/main/java/org/xvm/asm/ErrorListener.java index a13fd78e50e..2b7b7628f8b 100644 --- a/javatools/src/main/java/org/xvm/asm/ErrorListener.java +++ b/javatools/src/main/java/org/xvm/asm/ErrorListener.java @@ -6,6 +6,11 @@ import java.util.Arrays; import java.util.ResourceBundle; +import java.util.Set; + +import java.util.concurrent.ConcurrentHashMap; + +import java.util.function.Consumer; import org.xvm.compiler.Source; @@ -13,6 +18,8 @@ import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + import static org.xvm.util.Handy.quotedString; /** @@ -26,12 +33,15 @@ public interface ErrorListener { /** * Handles the logging of an error that originates in Ecstasy source code. * - * @param err the error info + *

Recording a diagnostic says nothing about whether the work should continue; ask + * {@link #isAbortDesired()} for that. The two used to be one boolean, which left a listener + * that only wants to watch with no correct value to return - false suppressed a legitimate + * abort and true invented one - and made whether the compiler kept going a property of who was + * listening. * - * @return true to attempt to abort the process that reported the error, or - * false to attempt to continue the process + * @param err the error info */ - boolean log(ErrorInfo err); + void log(ErrorInfo err); /** * Handles the logging of an error that originates in Ecstasy source code. @@ -44,13 +54,9 @@ public interface ErrorListener { * @param source the source code (optional) * @param lPosStart the position in the source where the error was detected * @param lPosEnd the position in the source at which the error concluded - * - * @return true to attempt to abort the process that reported the error, or - * false to attempt to continue the process */ - default boolean log(Severity severity, String sCode, Object[] aoParam, - Source source, long lPosStart, long lPosEnd) { - return log(new ErrorInfo(severity, sCode, aoParam, source, lPosStart, lPosEnd)); + default void log(Severity severity, String sCode, Object[] aoParam, Source source, long lPosStart, long lPosEnd) { + log(new ErrorInfo(severity, sCode, aoParam, source, lPosStart, lPosEnd)); } /** @@ -63,34 +69,331 @@ default boolean log(Severity severity, String sCode, Object[] aoParam, * @param aoParam the parameters for the error message; may be null * @param xs the XvmStructure that the error is related to; may * be null + */ + default void log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) { + log(severity, sCode, at(xs), aoParam); + } + + // ----- reporting ----------------------------------------------------------------------------- + + /** + * Report a diagnostic. + * + *

The message parameters are the trailing arguments, which is why the location is a + * {@link Site} rather than the two shapes it takes: an {@code Object[]} in the middle of the + * signature is what the older overloads need in order to leave room for the location after it, + * and it is why call sites have to write {@code new Object[]{...}} by hand. * - * @return true to attempt to abort the process that reported the error, or - * false to attempt continue the process + * @param severity the severity of the diagnostic + * @param sCode the error code that identifies the message + * @param site where the diagnostic belongs; see {@link #at} and {@link #in} + * @param aoParam the message parameters */ - default boolean log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) { - return log(new ErrorInfo(severity, sCode, aoParam, xs)); + default void log(Severity severity, String sCode, Site site, Object... aoParam) { + switch (site) { + case Site.In in -> log(new ErrorInfo(severity, sCode, aoParam, in.source(), in.lPosStart(), in.lPosEnd())); + case Site.At at -> log(new ErrorInfo(severity, sCode, aoParam, at.xs())); + case Site.None _ -> log(new ErrorInfo(severity, sCode, aoParam, null, 0, 0)); + } + } + + /** + * Report that the caller's operation failed. + * + * @see #log(Severity, String, Site, Object...) + */ + default void error(String sCode, Site site, Object... aoParam) { + log(Severity.ERROR, sCode, site, aoParam); + } + + /** + * Report something the compiler recovered from. + * + * @see #log(Severity, String, Site, Object...) + */ + default void warn(String sCode, Site site, Object... aoParam) { + log(Severity.WARNING, sCode, site, aoParam); + } + + /** + * Report something that is only being traced. + * + * @see #log(Severity, String, Site, Object...) + */ + default void info(String sCode, Site site, Object... aoParam) { + log(Severity.INFO, sCode, site, aoParam); + } + + /** + * Report that the caller cannot continue. Throw on the next line; a diagnostic is a report, + * not a control-flow mechanism, and nothing should depend on what the listener does with it. + * + * @see #log(Severity, String, Site, Object...) + */ + default void fatal(String sCode, Site site, Object... aoParam) { + log(Severity.FATAL, sCode, site, aoParam); + } + + /** + * A listener that hands each diagnostic to the given consumer, and answers the questions the + * compiler asks about what it has seen. + * + *

This is how a host should build one. {@link ErrorListener} is a functional interface, so a + * bare lambda compiles - but a lambda only supplies {@link #log}, and inherits defaults for + * {@link #hasSeriousErrors()}, {@link #isAbortDesired()} and {@link #hasError} that answer as + * though nothing had been reported. The compiler asks those questions around a hundred times + * during a compilation, to decide whether a stage may proceed, so a host that lambdas the + * interface directly is telling the compiler that its own diagnostics did not happen. + * + *

Every report is delivered; this factory does not deduplicate. Use {@link ErrorList} when + * repeated reports should collapse. Compiler callbacks must be serialized by the host. + * + * @param consumer receives each diagnostic as it is reported + * + * @return a listener that reports to the consumer and remembers what it reported + */ + static ErrorListener collecting(Consumer consumer) { + requireNonNull(consumer, "consumer"); + return new ErrorListener() { + @Override + public void log(ErrorInfo err) { + Severity severity = err.getSeverity(); + if (severity.isWorseThan(m_severity)) { + m_severity = severity; + } + f_setCodes.add(err.getCode()); + consumer.accept(err); + } + + @Override + public boolean isAbortDesired() { + return m_severity == Severity.FATAL; + } + + @Override + public boolean hasSeriousErrors() { + return m_severity.isAtLeast(Severity.ERROR); + } + + @Override + public boolean hasError(String sCode) { + return f_setCodes.contains(sCode); + } + + @Override + public String toString() { + return "Collecting(worst=" + m_severity + ")"; + } + + private final Set f_setCodes = ConcurrentHashMap.newKeySet(); + private volatile Severity m_severity = Severity.NONE; + }; + } + + /** + * Report to both listeners. + * + *

The abort question is answered by either: a caller that wrapped a budgeted listener has to + * keep getting the stop it asked for, whatever else is also listening. + * + * @param first one listener + * @param second the other + * + * @return a listener that reports to both + */ + static ErrorListener tee(ErrorListener first, ErrorListener second) { + requireNonNull(first, "first"); + requireNonNull(second, "second"); + return new ErrorListener() { + @Override + public void log(ErrorInfo err) { + first.log(err); + second.log(err); + } + + @Override + public boolean isAbortDesired() { + return first.isAbortDesired() || second.isAbortDesired(); + } + + @Override + public boolean hasSeriousErrors() { + return first.hasSeriousErrors() || second.hasSeriousErrors(); + } + + @Override + public boolean hasError(String sCode) { + return first.hasError(sCode) || second.hasError(sCode); + } + + @Override + public boolean isSilent() { + return first.isSilent() && second.isSilent(); + } + + @Override + public String toString() { + return "Tee(" + first + ", " + second + ")"; + } + }; + } + + /** + * @param xs the structure a diagnostic is about + * + * @return the site of a diagnostic about an XVM structure + */ + static Site at(XvmStructure xs) { + return new Site.At(xs); + } + + /** + * @param source the source being compiled + * @param lPosStart where the diagnostic starts + * @param lPosEnd where it ends + * + * @return the site of a diagnostic about a span of source + */ + static Site in(Source source, long lPosStart, long lPosEnd) { + return new Site.In(source, lPosStart, lPosEnd); + } + + /** + * Where a diagnostic belongs. + * + *

A listener receives the location as one of a small closed set of shapes, so a host that + * republishes diagnostics - an LSP server turning them into editor squiggles, say - can switch + * over them exhaustively instead of testing which of several nullable fields was populated. + */ + sealed interface Site { + /** + * A diagnostic about a span of source. + */ + record In(Source source, long lPosStart, long lPosEnd) implements Site {} + + /** + * A diagnostic about an XVM structure, whose location is wherever that structure is. + */ + record At(XvmStructure xs) implements Site {} + + /** + * A diagnostic with no location. + */ + record None() implements Site {} } + /** + * The site of a diagnostic that is not about any particular place. + */ + Site NOWHERE = new Site.None(); + /** * Branch this ErrorListener by creating a new one that will collect subsequent errors * in the same manner as this one until it is {@link #merge() merged} or discarded in the * (optional) context of the specified node. * + *

A branch buffers errors that may or may not end up being reported, so deciding to abandon + * the work is the parent's call and not the branch's: it is given {@link ErrorList#UNLIMITED} + * rather than a budget of its own. {@link ErrorList} overrides this to pass on the budget it + * was built with, and the two have to agree, or the compiler would do less work for a host + * that supplied its own listener than for one that used an ErrorList. + * * @param node (optional) the context ast node * * @return the branched-out ErrorListener */ default ErrorListener branch(AstNode node) { - return new ErrorList.BranchedErrorListener(this, 0, node); + return new ErrorList.BranchedErrorListener(this, ErrorList.UNLIMITED, node); } /** * Merge all errors collected by this ErrorListener into the one it was branched out of. * - * @return the ErrorListener this one was {@link #branch branched out} of + *

A listener that was never branched has nothing to merge and is already the sink the errors + * would be merged into, so merging it is a no-op. It is not an error: whether a listener in + * hand is a branch is not something its holder should have to know. + * + * @return the ErrorListener this one was {@link #branch branched out} of, or this one if it + * was not branched out of anything */ default ErrorListener merge() { - throw new UnsupportedOperationException("nothing to merge"); + return this; + } + + /** + * Why a stretch of work is not reporting its diagnostics. + * + *

The three behave identically - nothing may branch on which one a listener holds - but they + * are different intentions, and saying which is meant is the point. Grepping for one of these + * is the list of the places that meant it. + */ + enum Silence { + /** + * Speculative work whose failure is the answer, and whose failure must therefore not be + * audible. The compiler constantly asks "would this expression fit that type?", and the + * return value - not a diagnostic - is what the caller acts on. + * + *

Not for work whose failure the user should hear about if every alternative also fails; + * that is {@link ErrorListener#branch}. + */ + PROBE, + + /** + * The remainder of a computation already known to be incomplete. A result built from + * incomplete information produces diagnostics that describe the incompleteness rather + * than the user's code, and reporting them buries the one diagnostic that matters under + * a cascade of consequences. Unlike a probe, these are real diagnostics - a host may want + * them as related information for the diagnostic that does matter, which is why the + * listener they were taken from stays reachable. + */ + CASCADE, + + /** + * The caller has no sink to attach and wants none. Naming it is what keeps "I do not want + * these" from looking like "I did not think about these". + */ + DISCARD + } + + /** + * Obtain a listener that discards everything, for a caller with none of its own to derive + * from. + * + * @param why why the diagnostics are being discarded + * + * @return a listener that discards what it is given + */ + static ErrorListener silent(Silence why) { + return switch (why) { + case PROBE -> SILENT_PROBE; + case CASCADE -> SILENT_CASCADE; + case DISCARD -> SILENT_DISCARD; + }; + } + + /** + * Obtain a listener that discards the rest of what this one would have been told. + * + *

The result wraps this listener rather than being a shared constant, so the silence is a + * decision about one stretch of work instead of an anonymous gap, and + * {@link SilentErrorListener#suppressed} can still reach what was silenced. + * + *

Applying it twice is the same silence, so it is safe to call per use rather than having to + * hold the result. + * + * @param why why the diagnostics are being discarded + * + * @return a listener that discards what the rest of this work has to say + */ + default ErrorListener silence(Silence why) { + return new SilentErrorListener(this, why); + } + + /** + * @return why this listener is silent, or null if it is not + */ + default Silence silenceReason() { + return null; } /** @@ -115,9 +418,16 @@ default boolean hasError(String sCode) { } /** - * Used for debugging only. + * Whether this listener discards what it is told, for one of the reasons {@link Silence} + * names. {@link #silenceReason} says which. + * + *

Not debugging-only, despite what this said for a long time: work that exists solely to + * produce a diagnostic is worth skipping when nobody is listening, and + * {@code MethodDeclarationStatement} and {@code PropertyDeclarationStatement} both ask before + * doing it. That is the one thing a caller may branch on - never on which silence it + * is, which is why the three behave identically. * - * @return true iff this listener sits on top of the BlackHoleListener + * @return true iff this listener discards what it is told */ default boolean isSilent() { return false; @@ -132,8 +442,8 @@ default boolean isSilent() { class BlackholeErrorListener implements ErrorListener { @Override - public boolean log(ErrorInfo err) { - return false; + public void log(ErrorInfo err) { + // Retained until callers migrate to named silence. } @Override @@ -152,25 +462,105 @@ public String toString() { } } + // ----- inner class: SilentErrorListener ------------------------------------------------------ + + /** + * A listener that discards everything, for one of the reasons {@link Silence} names. + * + *

The three reasons behave identically and deliberately so - nothing may branch on which one + * it holds - but they are not the same intention, and a host that republishes diagnostics may + * want to treat them differently. Where the silence was derived from a real listener, + * {@link #suppressed} still reaches it. + */ + class SilentErrorListener + implements ErrorListener { + /** + * @param errs the listener being silenced, or null where the caller had none + * @param why why the diagnostics are being discarded + */ + public SilentErrorListener(ErrorListener errs, Silence why) { + f_errs = errs; + f_why = requireNonNull(why, "why"); + } + + /** + * @return the listener this silence was derived from, or null if it was not derived from + * one - a caller that never had a listener has nothing to suppress + */ + public ErrorListener suppressed() { + return f_errs; + } + + @Override + public void log(ErrorInfo err) { + // discarded on purpose; f_why says which purpose + } + + @Override + public ErrorListener merge() { + return this; + } + + @Override + public boolean isAbortDesired() { + // Suppressing reports does not make cancelled work useful again. + return f_errs != null && f_errs.isAbortDesired(); + } + + @Override + public boolean isSilent() { + return true; + } + + @Override + public Silence silenceReason() { + return f_why; + } + + @Override + public ErrorListener silence(Silence why) { + // already silent for a stated reason; a second reason would only add a layer + return this; + } + + @Override + public String toString() { + return f_errs == null ? "(" + f_why + ")" : "(" + f_why + " of " + f_errs + ")"; + } + + private final ErrorListener f_errs; + private final Silence f_why; + } + // ----- inner class: Runtime ErrorListener ---------------------------------------------------- /** - * A simple implementation of the ErrorListener that can be used at runtime. Errors will throw, - * and non-errors will go to standard out. + * A runtime sink that prints diagnostics and records an abort request for ERROR or FATAL. + * Reporting itself does not throw; callers query isAbortDesired() to decide whether to stop. */ class RuntimeErrorListener implements ErrorListener { @Override - public boolean log(ErrorInfo err) { - String s = err.toString(); - if (err.getSeverity().ordinal() >= Severity.ERROR.ordinal()) { - throw new IllegalStateException(s); - } else { - System.out.println(err.getSeverity() + ": " + s); - return false; + public void log(ErrorInfo err) { + System.out.println(err.getSeverity() + ": " + err); + if (err.getSeverity().isAtLeast(Severity.ERROR)) { + m_fAbort = true; } } + /** + * This used to throw from inside log(), which made the decision to stop - and the type of + * the exception that stopped it - a property of who was listening rather than of what went + * wrong. A caller that asks gets the same answer; a caller that does not is no longer + * interrupted in the middle of reporting. + */ + @Override + public boolean isAbortDesired() { + return m_fAbort; + } + + private volatile boolean m_fAbort; + @Override public String toString() { return "(Runtime error listener)"; @@ -223,6 +613,19 @@ public ErrorInfo(Severity severity, String sCode, Object[] aoParam, XvmStructure // TODO need to be able to ask the XVM structure for the source & location } + /** + * The location of this diagnostic, as a closed set of shapes rather than as several + * fields of which some are null. A host that republishes diagnostics can switch over the + * result exhaustively. + * + * @return where this diagnostic belongs + */ + public Site site() { + return m_source != null ? new Site.In(m_source, m_lPosStart, m_lPosEnd) + : m_xs != null ? new Site.At(m_xs) + : NOWHERE; + } + /** * @return the Severity of the error */ @@ -423,8 +826,12 @@ public String toString() { ResourceBundle RESOURCES = ResourceBundle.getBundle("errors"); /** - * Stateless ErrorListeners. + * Legacy silent sink and runtime diagnostic sink. */ + ErrorListener SILENT_PROBE = new SilentErrorListener(null, Silence.PROBE); + ErrorListener SILENT_CASCADE = new SilentErrorListener(null, Silence.CASCADE); + ErrorListener SILENT_DISCARD = new SilentErrorListener(null, Silence.DISCARD); + ErrorListener BLACKHOLE = new BlackholeErrorListener(); ErrorListener RUNTIME = new RuntimeErrorListener(); diff --git a/javatools/src/main/java/org/xvm/asm/XvmStructure.java b/javatools/src/main/java/org/xvm/asm/XvmStructure.java index 14000a384ec..a2156e87155 100755 --- a/javatools/src/main/java/org/xvm/asm/XvmStructure.java +++ b/javatools/src/main/java/org/xvm/asm/XvmStructure.java @@ -468,8 +468,8 @@ public boolean validate(ErrorListener errs) { * @param sCode the error code * @param aoParam the parameters of the error */ - public boolean log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) { - return ensureErrorListener(errs).log(sev, sCode, aoParam, this); + public void log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) { + ensureErrorListener(errs).log(sev, sCode, aoParam, this); } /** diff --git a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java index 30310fe6107..86abe8aebaf 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java @@ -238,7 +238,8 @@ public boolean validate(ErrorListener errs) { // type constant TypeConstant type = m_constType; if (type instanceof ImmutableTypeConstant) { - fHalt |= log(errs, Severity.WARNING, VE_IMMUTABLE_REDUNDANT); + log(errs, Severity.WARNING, VE_IMMUTABLE_REDUNDANT); + fHalt |= ensureErrorListener(errs).isAbortDesired(); } // a service type cannot be immutable diff --git a/javatools/src/main/java/org/xvm/compiler/Lexer.java b/javatools/src/main/java/org/xvm/compiler/Lexer.java index 89cbfb0ca36..6b0631df777 100644 --- a/javatools/src/main/java/org/xvm/compiler/Lexer.java +++ b/javatools/src/main/java/org/xvm/compiler/Lexer.java @@ -2521,7 +2521,8 @@ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosS * Log an error. */ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) { - if (m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd)) { + m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd); + if (m_errorListener.isAbortDesired()) { throw new CompilerException("error list is full: " + m_errorListener); } } diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index 20fdd3b0e89..dfd45f89278 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -66,10 +66,10 @@ private Parser(Source source, ErrorListener errs, Lexer lexer) { // we need to route lexical reports through the active lookahead attempt, if any ErrorListener listenerWrapper = err -> { if (m_lookAhead == null) { - return m_errorListener.log(err); + m_errorListener.log(err); + } else { + m_lookAhead.log(err); } - m_lookAhead.log(err); - return false; }; m_lexer = lexer == null ? new Lexer(source, listenerWrapper) : lexer; @@ -5624,9 +5624,12 @@ protected Token takeDoc() { protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd, Object... aoParam) { if (m_lookAhead != null) { m_lookAhead.log(severity, sCode, aoParam, lPosStart, lPosEnd); - } else if (m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd)) { - m_fAvoidRecovery = true; - throw new CompilerException("error list is full: " + m_errorListener); + } else { + m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd); + if (m_errorListener.isAbortDesired()) { + m_fAvoidRecovery = true; + throw new CompilerException("error list is full: " + m_errorListener); + } } } diff --git a/javatools/src/main/java/org/xvm/compiler/Token.java b/javatools/src/main/java/org/xvm/compiler/Token.java index 54faab17062..5c07c20106c 100644 --- a/javatools/src/main/java/org/xvm/compiler/Token.java +++ b/javatools/src/main/java/org/xvm/compiler/Token.java @@ -316,16 +316,13 @@ public String getString(Source source) { * {@link Severity#WARNING}, {@link Severity#ERROR}, or {@link Severity#FATAL} * @param sCode the error code that identifies the error message * @param aoParam the parameters for the error message; may be null - * - * @return true to attempt to abort the process that reported the error, or false to attempt to - * continue the process */ - public boolean log(ErrorListener errs, Source source, Severity severity, String sCode, Object... aoParam) { + public void log(ErrorListener errs, Source source, Severity severity, String sCode, Object... aoParam) { if (aoParam == null || aoParam.length == 0) { aoParam = new Object[] {source == null ? toString() : getString(source)}; } - return errs.log(severity, sCode, aoParam, source, + errs.log(severity, sCode, aoParam, source, source == null ? 0L : getStartPosition(), source == null ? 0L : getEndPosition()); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index d6d613d4ea7..2b22036519c 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -637,17 +637,14 @@ public Access getDefaultAccess() { * {@link Severity#ERROR}, or {@link Severity#FATAL} * @param sCode the error code that identifies the error message * @param aoParam the parameters for the error message; may be null - * - * @return true to attempt to abort the process that reported the error, or - * false to attempt to continue the process */ - public boolean log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) { + public void log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) { Source source = getSource(); - return errs == null - ? severity.ordinal() >= Severity.ERROR.ordinal() - : errs.log(severity, sCode, aoParam, source, - source == null ? 0L : getStartPosition(), - source == null ? 0L : getEndPosition()); + if (errs != null) { + errs.log(severity, sCode, aoParam, source, + source == null ? 0L : getStartPosition(), + source == null ? 0L : getEndPosition()); + } } // ----- compile phases ------------------------------------------------------------------------ diff --git a/javatools/src/main/java/org/xvm/tool/Launcher.java b/javatools/src/main/java/org/xvm/tool/Launcher.java index 6081f227daf..43edd23268f 100644 --- a/javatools/src/main/java/org/xvm/tool/Launcher.java +++ b/javatools/src/main/java/org/xvm/tool/Launcher.java @@ -586,14 +586,12 @@ public String desc() { * Displays via Console and forwards to external ErrorListener if provided. * * @param err the error information - * @return true if compilation should abort */ @Override - public boolean log(ErrorInfo err) { + public void log(ErrorInfo err) { m_sevWorst = worstOf(m_sevWorst, err.getSeverity()); log(err.getSeverity(), err.toString()); m_errors.log(err); - return isAbortDesired(); } /** diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index b38f6676fc5..7aa6ef955bd 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -802,8 +802,8 @@ public void linkParseTrees() { public abstract TypeCompositionStatement type(); @Override - public boolean log(ErrorInfo err) { - return errs().log(err); + public void log(ErrorInfo err) { + errs().log(err); } @Override diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java new file mode 100644 index 00000000000..c83cbb4774c --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java @@ -0,0 +1,80 @@ +package org.xvm.asm; + +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Source; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorListener.in; + +/** + * Recording a diagnostic and deciding to abandon the work are two questions. + */ +public class ErrorListenerAbortTest { + /** + * The budget still stops the work; it is just asked for rather than returned from the act of + * recording. + */ + @Test + public void testTheErrorBudgetStillAborts() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(2); + + errs.error(CODE, in(source, 0, 1), "a"); + assertFalse(errs.isAbortDesired(), "one of a budget of two"); + + errs.error(CODE, in(source, 2, 3), "b"); + assertTrue(errs.isAbortDesired(), "the budget is spent"); + + assertEquals(2, errs.getErrors().size()); + } + + @Test + public void testUnlimitedNeverAbortsOnCount() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(ErrorList.UNLIMITED); + + IntStream.range(0, 50).forEach(i -> errs.error(CODE, in(source, i, i + 1), "a")); + + assertFalse(errs.isAbortDesired()); + assertTrue(errs.hasSeriousErrors()); + } + + /** + * A FATAL abandons the work whatever the budget. + */ + @Test + public void testFatalAbortsRegardlessOfBudget() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(ErrorList.UNLIMITED); + + errs.fatal(CODE, in(source, 0, 1), "a"); + + assertTrue(errs.isAbortDesired()); + } + + /** + * RUNTIME is the listener of last resort, reached when nobody supplied one. It used to throw + * from inside log(), which pre-empted whatever the detecting code was about to throw and made + * the exception type a property of who was listening. It reports and answers the abort + * question instead. + */ + @Test + public void testTheFallbackListenerReportsRatherThanThrowing() { + Source source = new Source(SOURCE); + var runtime = new ErrorListener.RuntimeErrorListener(); + + assertDoesNotThrow(() -> + runtime.error(CODE, in(source, 0, 1), "a", "b")); + assertTrue(runtime.isAbortDesired(), "it still says the work should stop, when asked"); + } + + private static final String SOURCE = "module TestSimple { void run() {} }"; + private static final String CODE = "PARSER-03"; +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerBranchTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerBranchTest.java new file mode 100644 index 00000000000..5fb8777e610 --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerBranchTest.java @@ -0,0 +1,144 @@ +package org.xvm.asm; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Source; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorList.UNLIMITED; +import static org.xvm.asm.ErrorListener.collecting; +import static org.xvm.asm.ErrorListener.in; + +/** + * Tests that branching and merging behave the same for a listener supplied by a host as they do + * for an {@link ErrorList}. + * + *

An embedding host - an LSP server, say - implements {@link ErrorListener} to receive + * diagnostics, and gets the interface's default {@code branch} and {@code merge}. Those defaults + * have to agree with the ones {@link ErrorList} overrides, or the compiler behaves differently + * depending on who is listening. + */ +public class ErrorListenerBranchTest { + /** + * A listener of the shape a host would write: it collects, and overrides nothing else. + */ + private static class HostListener + implements ErrorListener { + @Override + public void log(ErrorInfo err) { + received.add(err.getCode()); + } + + final List received = new ArrayList<>(); + } + + /** + * A branch exists to buffer errors that may or may not be reported; deciding to abandon the + * work is the parent's call, not the branch's. Inheriting a budget of one made a branch ask + * for an abort as soon as it saw a single error, so a host that collected diagnostics got + * less of the compiler's work done than one that used an ErrorList. + */ + @Test + public void testBranchOfAHostListenerDoesNotAbortAfterOneError() { + Source source = new Source(SOURCE); + HostListener host = new HostListener(); + ErrorListener branch = host.branch(null); + + branch.error(CODE, in(source, 0, 1), "a"); + assertFalse(branch.isAbortDesired(), "one error is not a reason to abandon the work"); + + branch.error(CODE, in(source, 2, 3), "b"); + assertFalse(branch.isAbortDesired()); + } + + @Test + public void testBranchOfAHostListenerMergesEverythingItCollected() { + Source source = new Source(SOURCE); + HostListener host = new HostListener(); + ErrorListener branch = host.branch(null); + + branch.error(CODE, in(source, 0, 1), "a"); + branch.error(CODE, in(source, 2, 3), "b"); + assertEquals(List.of(), host.received, "a branch buffers until it is merged"); + + branch.merge(); + assertEquals(List.of(CODE, CODE), host.received); + } + + /** + * An ErrorList branch is the reference behaviour the above has to match. + */ + @Test + public void testBranchOfAnErrorListBehavesTheSame() { + Source source = new Source(SOURCE); + ErrorList parent = new ErrorList(UNLIMITED); + ErrorListener branch = parent.branch(null); + + branch.error(CODE, in(source, 0, 1), "a"); + assertFalse(branch.isAbortDesired()); + + branch.error(CODE, in(source, 2, 3), "b"); + branch.merge(); + assertEquals(2, parent.getErrors().size()); + } + + /** + * Merging a listener that was never branched is a no-op rather than a failure: there is + * nothing to merge, and the listener is already the sink. + */ + @Test + public void testMergingAnUnbranchedListenerIsHarmless() { + HostListener host = new HostListener(); + + assertSame(host, host.merge()); + } + + /** + * The interface is functional, so a host can lambda it - and a lambda supplies only log(), + * inheriting defaults that answer as though nothing had been reported. The compiler asks those + * questions around a hundred times to decide whether a stage may proceed, so a bare lambda + * tells it that the host's own diagnostics did not happen. + */ + @Test + public void testABareLambdaAnswersAsThoughNothingWasReported() { + Source source = new Source(SOURCE); + List seen = new ArrayList<>(); + ErrorListener naive = err -> seen.add(err.getCode()); + + naive.error(CODE, in(source, 0, 1), "a"); + + assertEquals(1, seen.size(), "it did receive the diagnostic"); + assertFalse(naive.hasSeriousErrors(), "but it reports that nothing serious happened"); + assertFalse(naive.hasError(CODE)); + } + + /** + * collecting() is the same one-liner for a host, and answers truthfully. + */ + @Test + public void testACollectingListenerAnswersTruthfully() { + Source source = new Source(SOURCE); + List seen = new ArrayList<>(); + ErrorListener host = collecting(err -> seen.add(err.getCode())); + + host.error(CODE, in(source, 0, 1), "a"); + + assertEquals(1, seen.size()); + assertTrue(host.hasSeriousErrors()); + assertTrue(host.hasError(CODE)); + assertFalse(host.isAbortDesired(), "an error is not by itself a reason to abandon"); + + host.fatal(CODE, in(source, 2, 3), "b"); + assertTrue(host.isAbortDesired(), "a fatal is"); + } + + private static final String SOURCE = "module TestSimple { void run() {} }"; + private static final String CODE = "PARSER-03"; +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java new file mode 100644 index 00000000000..3c947a5ece6 --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java @@ -0,0 +1,91 @@ +package org.xvm.asm; + +import java.util.ArrayList; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorListener.ErrorInfo; +import org.xvm.asm.ErrorListener.Silence; +import org.xvm.asm.ErrorListener.Site; + +import org.xvm.compiler.CompilerException; +import org.xvm.compiler.Lexer; +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorListener.NOWHERE; + +/** Migration examples that compile independently of later listener-ownership changes. */ +class ErrorListenerMigrationTest { + @Test + void collectingDeliversDuplicatesWhileErrorListCollapsesThem() { + var received = new ArrayList(); + var collector = ErrorListener.collecting(received::add); + var errors = new ErrorList(); + var both = ErrorListener.tee(collector, errors); + both.error("PARSER-03", NOWHERE, "a", "b"); + both.error("PARSER-03", NOWHERE, "a", "b"); + assertEquals(2, received.size()); + assertEquals(1, errors.getErrors().size()); + assertTrue(collector.hasSeriousErrors()); + assertFalse(collector.isAbortDesired()); + } + + @Test + void branchesAndDerivedSilenceObserveTheParentBudget() { + var parent = new ErrorList(ErrorList.FIRST_ERROR); + var branch = parent.branch(null).branch(null); + var quiet = parent.silence(Silence.PROBE); + parent.error("PARSER-03", NOWHERE, "a", "b"); + assertTrue(branch.isAbortDesired()); + assertTrue(quiet.isAbortDesired()); + quiet.error("PARSER-03", NOWHERE, "discarded", "report"); + assertEquals(1, parent.getErrors().size()); + } + + @Test + void legacyStructureReportsStillUseTheBranchSourceSite() { + var source = new Source("module Example {}"); + var errors = new ErrorList(); + var node = new Parser(source, errors).parseSource().getStatements().getLast(); + var branch = errors.branch(node); + var structure = new FileStructure("Example").getModule(); + branch.log(Severity.ERROR, "PARSER-03", new Object[]{"legacy", "report"}, structure); + branch.error("PARSER-03", ErrorListener.at(structure), "new", "report"); + branch.merge(); + assertEquals(2, errors.getErrors().size()); + errors.getErrors().forEach(error -> { + var site = assertInstanceOf(Site.In.class, error.site()); + assertSame(source, site.source()); + assertEquals(node.getStartPosition(), site.lPosStart()); + assertEquals(node.getEndPosition(), site.lPosEnd()); + }); + } + + @Test + void parserStillStopsWhenReportingSpendsTheBudget() { + var errors = new ErrorList(ErrorList.FIRST_ERROR); + var parser = new Parser(new Source("module Example { void run() { Int x = ; Int y = ; } }"), errors); + assertThrows(CompilerException.class, parser::parseSource); + assertEquals(1, errors.getSeriousErrorCount()); + assertTrue(errors.isAbortDesired()); + } + + @Test + void lexerStillStopsWhenReportingSpendsTheBudget() { + var errors = new ErrorList(ErrorList.FIRST_ERROR); + var lexer = new Lexer(new Source("\"unterminated"), errors); + assertThrows(CompilerException.class, () -> lexer.forEachRemaining(token -> {})); + assertEquals(1, errors.getSeriousErrorCount()); + assertTrue(errors.isAbortDesired()); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java new file mode 100644 index 00000000000..2082546d27a --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java @@ -0,0 +1,111 @@ +package org.xvm.asm; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Source; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorListener.Silence.CASCADE; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + +/** + * The compiler is silent for three different reasons, and says which it means. + */ +public class ErrorListenerSilenceTest { + /** + * The reasons are separate names for the same behaviour. They have to stay indistinguishable + * at runtime: the moment anything can branch on which one it holds, the listener has become + * the mode flag it exists to avoid. + */ + @Test + public void testTheSilencesAreIndistinguishableInBehaviour() { + Source source = new Source(SOURCE); + + assertNotSame(silent(PROBE), silent(DISCARD), "they are separate constants"); + + List.of(silent(PROBE), silent(CASCADE), silent(DISCARD)).forEach(errs -> { + errs.log(Severity.ERROR, CODE, in(source, 0, 1), "a"); + assertFalse(errs.isAbortDesired()); + assertFalse(errs.hasSeriousErrors()); + assertTrue(errs.isSilent()); + assertSame(errs, errs.merge(), "a silent listener absorbs its own branch"); + }); + } + + /** + * Suppressing a cascade is a decision, not an absence: a result built from incomplete + * information describes the incompleteness rather than the user's code, so the rest of that + * computation is discarded rather than reported. + */ + @Test + public void testSuppressCascadeIsSilentAndDoesNotDisturbTheCaller() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(10); + + ErrorListener suppressed = errs.silence(CASCADE); + suppressed.error(CODE, in(source, 0, 1), "a", "b"); + + assertTrue(suppressed.isSilent()); + assertEquals(0, errs.getErrors().size(), "the caller's listener is untouched"); + } + + /** + * The reason is part of the listener rather than part of its name, so a host republishing + * diagnostics can tell a probe from a cascade - which is what lets it offer the one and not + * the other. + */ + @Test + public void testASilentListenerSaysWhyItIsSilent() { + ErrorList errs = new ErrorList(); + + assertEquals(PROBE, silent(PROBE).silenceReason()); + assertEquals(CASCADE, errs.silence(CASCADE).silenceReason()); + assertEquals(DISCARD, silent(DISCARD).silenceReason()); + + assertNull(errs.silenceReason(), "a listener that is not silent has no reason"); + } + + /** + * A silence derived from a listener keeps it, so a host that wants the consequences after all + * has somewhere to get them. A silence a caller reached for without having one cannot. + */ + @Test + public void testADerivedSilenceKeepsTheListenerItSilenced() { + ErrorList errs = new ErrorList(); + + var derived = assertInstanceOf(ErrorListener.SilentErrorListener.class, errs.silence(CASCADE)); + assertSame(errs, derived.suppressed()); + + var shared = assertInstanceOf(ErrorListener.SilentErrorListener.class, silent(PROBE)); + assertNull(shared.suppressed(), "there was no listener to derive it from"); + } + + /** + * Silencing an already-silent listener is the same silence, so it stays safe to call per use + * rather than having to be held. + */ + @Test + public void testSilencingASilenceIsIdempotent() { + ErrorListener quiet = new ErrorList().silence(CASCADE); + + assertSame(quiet, quiet.silence(CASCADE)); + assertSame(quiet, quiet.silence(PROBE), "already silent; a second reason adds nothing"); + } + + private static final String SOURCE = "module TestSimple { void run() {} }"; + private static final String CODE = "PARSER-03"; +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerSiteTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerSiteTest.java new file mode 100644 index 00000000000..a93ca6c840d --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerSiteTest.java @@ -0,0 +1,121 @@ +package org.xvm.asm; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorListener.ErrorInfo; +import org.xvm.asm.ErrorListener.Site; + +import org.xvm.compiler.Source; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorList.FIRST_ERROR; +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.tee; + +/** + * Tests the reporting API that takes the message parameters as a trailing varargs. + */ +public class ErrorListenerSiteTest { + @Test + public void testParametersArePassedAsATrailingVarargs() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(10); + + errs.error(CODE, in(source, 3, 7), "a", "b"); + + ErrorInfo err = errs.getErrors().getFirst(); + assertEquals(Severity.ERROR, err.getSeverity()); + assertEquals(CODE, err.getCode()); + assertEquals(List.of("a", "b"), List.of(err.getParams())); + } + + @Test + public void testSeverityVerbsAgreeWithTheSeverityTheyName() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(10); + + errs.info (CODE, in(source, 0, 1), "x", "y"); + errs.warn (CODE, in(source, 1, 2), "x", "y"); + errs.error(CODE, in(source, 2, 3), "x", "y"); + errs.fatal(CODE, in(source, 3, 4), "x", "y"); + + var actual = errs.getErrors().stream().map(ErrorInfo::getSeverity).toList(); + assertEquals(List.of(Severity.INFO, Severity.WARNING, Severity.ERROR, Severity.FATAL), actual); + } + + /** + * The point of Site being a closed set: a host republishing diagnostics - an LSP server + * turning them into editor squiggles - switches over it exhaustively, instead of testing + * which of several nullable fields happened to be populated. + */ + @Test + public void testSiteIsExhaustivelySwitchable() { + Source source = new Source(SOURCE); + ErrorList errs = new ErrorList(10); + + errs.error(CODE, in(source, 3, 7), "a", "b"); + errs.error(CODE, NOWHERE, "c", "d"); + + assertInstanceOf(Site.In.class, errs.getErrors().getFirst().site()); + assertInstanceOf(Site.None.class, errs.getErrors().get(1).site()); + + var published = errs.getErrors().stream().map(err -> switch (err.site()) { + case Site.In site -> site.lPosStart() + ".." + site.lPosEnd(); + case Site.At site -> site.xs().getDescription(); + case Site.None _ -> "whole file"; + }).toList(); + assertEquals(List.of("3..7", "whole file"), published); + } + + /** + * A tee reports to both, and answers the abort question for either: a caller that wrapped a + * budgeted listener has to keep getting the stop it asked for. + */ + @Test + public void testATeeReportsToBothAndKeepsEithersAbort() { + Source source = new Source(SOURCE); + ErrorList watcher = new ErrorList(ErrorList.UNLIMITED); + ErrorList budget = new ErrorList(FIRST_ERROR); + + ErrorListener both = tee(budget, watcher); + both.error(CODE, in(source, 0, 1), "a", "b"); + + assertEquals(1, budget.getErrors().size()); + assertEquals(1, watcher.getErrors().size()); + assertTrue(both.isAbortDesired(), "the budgeted side still gets its stop"); + } + + /** + * Recording with a tee and replaying with logTo is what lets a memoized result tell a later + * caller what building it had to say. Deduplication makes the replay idempotent. + */ + @Test + public void testRecordedDiagnosticsReplayOnceToALaterCaller() { + Source source = new Source(SOURCE); + ErrorList first = new ErrorList(ErrorList.UNLIMITED); + ErrorList recorder = new ErrorList(ErrorList.UNLIMITED); + + tee(first, recorder).error(CODE, in(source, 0, 1), "a", "b"); + assertEquals(1, first.getErrors().size()); + + // a later caller, with its own listener, hears the same thing + ErrorList later = new ErrorList(ErrorList.UNLIMITED); + recorder.logTo(later); + assertEquals(1, later.getErrors().size()); + + // and asking twice does not say it twice + recorder.logTo(later); + assertEquals(1, later.getErrors().size()); + } + + private static final String SOURCE = "module TestSimple { void run() {} }"; + private static final String CODE = "PARSER-03"; +} diff --git a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java index 29b0d95ee06..c31e5b1898a 100644 --- a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java +++ b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java @@ -67,13 +67,12 @@ private static class TestErrorListener implements ErrorListener { private Severity worstSeverity = NONE; @Override - public boolean log(final ErrorInfo err) { + public void log(final ErrorInfo err) { severities.add(err.getSeverity()); errors.add(err.getCode() + ": " + err); if (err.getSeverity().compareTo(worstSeverity) > 0) { worstSeverity = err.getSeverity(); } - return false; // Don't abort } @SuppressWarnings("unused") From 2cfef562a94944af951d8557c526c5a0d4d6167d Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:21 +0100 Subject: [PATCH 003/324] Require explicit compiler listeners and reasons for silence - Require explicit compiler listeners and reasons for silence. - Document listener ownership and demonstrate host regressions. - Clarify listener nullability and add standalone silence constructor. - Remove unnecessary wrapping from TypeInfo listener calls. - Use one shared compiler error budget for default listeners. - Reuse silent listeners within related compiler checks. - Describe listener regression baseline without internal slice labels. The migration includes master's embedded run code: InterpreterControl's entry-method reports use errs.error and errs.warn, and EmbeddingSupport keeps master's runMethod, with its entry point and console input, beside run. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 377 ++++++++++++-- .../java/org/xvm/api/EmbeddingSupport.java | 54 +- .../java/org/xvm/api/InterpreterControl.java | 17 +- .../main/java/org/xvm/api/ModuleCompiler.java | 36 +- .../main/java/org/xvm/asm/ClassStructure.java | 5 +- .../src/main/java/org/xvm/asm/Component.java | 15 +- .../java/org/xvm/asm/ComponentResolver.java | 9 +- .../src/main/java/org/xvm/asm/ErrorList.java | 88 +++- .../main/java/org/xvm/asm/ErrorListener.java | 485 +++++++++++------- .../java/org/xvm/asm/MethodStructure.java | 13 +- .../main/java/org/xvm/asm/XvmStructure.java | 4 +- .../org/xvm/asm/constants/TypeCollector.java | 16 +- .../org/xvm/asm/constants/TypeConstant.java | 81 +-- .../org/xvm/asm/constants/TypeInfoReal.java | 5 +- .../asm/constants/UnresolvedTypeConstant.java | 5 +- .../main/java/org/xvm/compiler/Compiler.java | 11 +- .../java/org/xvm/compiler/EvalCompiler.java | 5 +- .../src/main/java/org/xvm/compiler/Lexer.java | 78 ++- .../main/java/org/xvm/compiler/Parser.java | 14 +- .../src/main/java/org/xvm/compiler/Token.java | 8 +- .../compiler/ast/AnnotationExpression.java | 5 +- .../org/xvm/compiler/ast/AnonInnerClass.java | 4 +- .../compiler/ast/ArrayAccessExpression.java | 78 +-- .../org/xvm/compiler/ast/AsExpression.java | 7 +- .../xvm/compiler/ast/AssignmentStatement.java | 11 +- .../java/org/xvm/compiler/ast/AstNode.java | 17 +- .../org/xvm/compiler/ast/CaseManager.java | 15 +- .../xvm/compiler/ast/CmpChainExpression.java | 5 +- .../org/xvm/compiler/ast/CmpExpression.java | 5 +- .../java/org/xvm/compiler/ast/Context.java | 20 +- .../org/xvm/compiler/ast/ElseExpression.java | 5 +- .../org/xvm/compiler/ast/ElvisExpression.java | 15 +- .../java/org/xvm/compiler/ast/Expression.java | 31 +- .../xvm/compiler/ast/ForEachStatement.java | 13 +- .../compiler/ast/InvocationExpression.java | 40 +- .../xvm/compiler/ast/LambdaExpression.java | 28 +- .../org/xvm/compiler/ast/ListExpression.java | 5 +- .../org/xvm/compiler/ast/MapExpression.java | 5 +- .../ast/MethodDeclarationStatement.java | 10 +- .../org/xvm/compiler/ast/NameExpression.java | 23 +- .../org/xvm/compiler/ast/NameResolver.java | 3 +- .../xvm/compiler/ast/NamedTypeExpression.java | 5 +- .../org/xvm/compiler/ast/NewExpression.java | 16 +- .../compiler/ast/NonBindingExpression.java | 5 +- .../xvm/compiler/ast/NotNullExpression.java | 11 +- .../compiler/ast/ParenthesizedExpression.java | 14 +- .../org/xvm/compiler/ast/RelOpExpression.java | 27 +- .../org/xvm/compiler/ast/ReturnStatement.java | 13 +- .../ast/SequentialAssignExpression.java | 3 + .../java/org/xvm/compiler/ast/StageMgr.java | 16 +- .../org/xvm/compiler/ast/StatementBlock.java | 9 +- .../xvm/compiler/ast/StatementExpression.java | 14 +- .../xvm/compiler/ast/TemplateExpression.java | 9 +- .../xvm/compiler/ast/TernaryExpression.java | 23 +- .../org/xvm/compiler/ast/TraceExpression.java | 6 +- .../org/xvm/compiler/ast/TupleExpression.java | 7 +- .../ast/TypeCompositionStatement.java | 31 +- .../org/xvm/compiler/ast/TypeExpression.java | 11 +- .../ast/UnaryComplementExpression.java | 5 +- .../compiler/ast/UnaryMinusExpression.java | 5 +- .../_native/lang/src/xRTCompiler.java | 7 +- .../template/_native/reflect/xRTType.java | 10 +- .../src/main/java/org/xvm/tool/Bundler.java | 10 +- .../src/main/java/org/xvm/tool/Compiler.java | 20 +- .../main/java/org/xvm/tool/Disassembler.java | 13 +- .../main/java/org/xvm/tool/Initializer.java | 10 +- .../src/main/java/org/xvm/tool/Launcher.java | 58 ++- .../main/java/org/xvm/tool/ModuleInfo.java | 17 +- .../src/main/java/org/xvm/tool/Runner.java | 14 +- .../main/java/org/xvm/tool/TestRunner.java | 10 +- .../xvm/api/ErrorListenerBoundaryTest.java | 127 +++++ .../org/xvm/asm/ErrorListenerAbortTest.java | 16 + .../asm/ErrorListenerHostRegressionTest.java | 62 +++ .../xvm/asm/ErrorListenerMigrationTest.java | 36 +- .../org/xvm/asm/ErrorListenerSilenceTest.java | 54 +- .../test/java/org/xvm/tool/BundlerTest.java | 13 +- .../xvm/tool/LauncherErrorHandlingTest.java | 57 +- .../org/xvm/tool/LauncherVersionTest.java | 9 +- 78 files changed, 1724 insertions(+), 720 deletions(-) create mode 100644 javatools/src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java diff --git a/javatools/README.md b/javatools/README.md index 6c5d2dbf37c..b57757488ec 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -31,46 +31,361 @@ Status: Suitable for use * AST nodes are multi-pass compiled (with optional re-queuing) via `org.xvm.compiler.ast.StageManager` -## Error-listener API migration (breaking change) +## Compiler diagnostics and error listeners -This change requires recompiling Java clients and listener implementations. `ErrorListener.log` -now returns `void`, including its legacy positional overloads. The forwarding `log` helpers on -`XvmStructure`, `AstNode` and `Token` also return `void`. Java cannot retain a compatibility overload -that differs only by return type. These source and binary compatibility breaks are planned for **0.5.0**; Java clients and listener -implementations must be migrated and recompiled when adopting that release. +### Why the old reporting model had to change -Report a diagnostic, then ask the listener whether the work should stop: +The old error-listener API did not provide a reliable contract for embedding the compiler. +Accepting a listener at the entry point was not enough: deeper calls could substitute `BLACKHOLE`, +accept null as an implicit request to discard reports, or find a different listener through a +shared structure. The caller could receive only part of an attempt's diagnostics without knowing +anything had been lost. Suppression, ownership and stopping policy were spread across unrelated +call sites, so adding a callback at the outer API could not repair what happened underneath it. + +This caused observable failures. The default branch for a host listener imposed a one-error +budget the host had never requested: the lexer stopped at the first recoverable error and never +reported the next one. A previously created branch could also ignore its parent's later abort +request. Two regression tests reproduce those failures on the old compiler with exactly the same +test source that passes on the repaired implementation. + +Reporting also had inconsistent control-flow effects. `log` returned an abort decision, but the +fallback `RuntimeErrorListener` instead threw an exception for ERROR and FATAL. Selecting a +diagnostic destination could therefore change how the operation failed. An editor must be able to +collect an ordinary source error as data, distinguish it from an internal compiler failure, and +let the compiler stop at an appropriate checkpoint. The destination receiving a report should not +introduce a different failure path merely because that report has a particular severity. + +### Changes and enhancements + +* **Separate reporting from stopping.** `log(ErrorInfo)` delivers a diagnostic; + `isAbortDesired()` answers whether work should stop. The lexer and parser retain explicit stop + checkpoints, and forwarding listeners preserve the owner's policy. +* **Make host callbacks usable by the compiler.** `collecting` forwards structured diagnostics + while remembering severity and error codes. A bare callback receives reports but does not + implement those state queries automatically. +* **Make branching consistent.** A host's default branch buffers without an arbitrary one-error + limit. `ErrorList` branches retain their configured budget, and both observe their parent's + current abort request. +* **Make suppression explicit.** Named reasons distinguish probes, cascade suppression and + intentional discards. A derived silence discards messages while retaining the owner's stop + policy. +* **Require a reporting destination at active compilation boundaries.** An omitted listener is + rejected before compilation starts; intentional silence is supplied as an explicit listener. +* **Keep ownership stable.** Constructor-owned listeners remain attached to their operation. + Temporary suppression derives a local listener instead of overwriting the original destination. +* **Preserve diagnostic structure.** `Site` describes source spans, structures or unpositioned + reports. Severity helpers take message arguments as trailing varargs; `tee` forwards reports to + two destinations while combining their state. + +### Removed behavior and its replacement + +* **The boolean return from `log`** is replaced by a reporting call followed by an explicit policy + query where the caller needs to stop. There is one continuation query for wrappers to preserve. +* **`BLACKHOLE` and `BlackholeErrorListener`** are replaced by named suppression factories. + Removing the old symbols makes unmigrated call sites fail to compile, requiring each caller to + identify its reason for silence and whether a parent's abort policy must survive. +* **Null-to-discard fallback at compilation boundaries** is replaced by a required listener. + Passing `silent(DISCARD)` still supports a caller that deliberately wants no reports; accidentally + omitting the destination no longer looks like a successful choice of silence. +* **The silent default in `ResolutionCollector.getErrorListener()`** is removed. Implementations + must provide their destination, and `SimpleCollector` retains the listener it was given. +* **Reassigning a TypeInfo build's listener to `BLACKHOLE`** is replaced by local cascade + suppression. The original listener continues to identify the operation and its stop policy. +* **Severity-triggered exceptions in the fallback runtime listener** are replaced by recorded + abort state. The compiler or runtime caller decides how to end its operation. This does not + remove the command-line launcher's deliberate fatal-error and exit handling. +* **The listener parameter on `Expression.testFitAsType`** is removed because that operation is + a probe. Its staging and fit checks use explicit probe silence; selected validation can still + retain and merge its diagnostics. + +The legacy positional reporting overloads remain available after recompilation, but are +deprecated in favor of `Site` and trailing arguments. Their source re-anchoring behavior is tested. + +### Why this is a prerequisite for the compiler-backed LSP + +**Reliable diagnostic ownership and abort propagation are required for the compiler-backed LSP.** +The adapter must associate reports with the requested compilation, reject unusable semantic +results, and stop obsolete work at compiler checkpoints. It cannot reconstruct diagnostics that +the compiler discarded or obtain errors from work that an unintended one-error budget prevented. +Leaving those failures in place blocks a reliable compiler-backed adapter, regardless of how much +code is added around the public entry point. These changes establish the reporting foundation; +the companion ownership and delivery changes described below complete the paths built on it. +The exact helper names and the decision to remove a legacy alias are API design choices. The +delivery, state and stopping guarantees they implement are the essential requirements. + +### Compatibility with ordinary compilation + +Normal command-line compilation continues to use the existing compiler pipeline, console and +exit policy. Users do not need new flags or changes to their Ecstasy source. The CLI supplies an +explicit discard listener only for its external delegate; its console still reports diagnostics. +`ErrorList` retains its configured serious-error budget, fatal reports still request an abort, +and the lexer/parser still stop when the applicable policy requires it. The changes repair +diagnostic delivery and listener ownership without introducing an IDE dependency into the compiler. + +There is strong regression evidence for that continuity: the full Java suite for this listener +migration executed 418 tests successfully, and all 24 rebuilt XDK modules matched a freshly built +master baseline after normalizing only module creation timestamps. The focused checks below add +direct coverage of reporting, aborts, boundary validation and launcher behavior. These results +support confidence in ordinary compilation; they are evidence for the tested paths, not an +exhaustive guarantee for every source program or external integration. + +**The Java embedding API deliberately changes at the planned 0.5.0 boundary.** Existing Java +callers and listener implementations must migrate and recompile. Changed return types, removed +symbols, rejected nulls and the runtime listener's new exception behavior prevent a claim of +100% source or binary compatibility, even for a non-LSP Java consumer. The migration steps below +identify those changes explicitly so ordinary consumers can preserve their intended behavior. + +### Code tour and practical usage + +#### 1. Give each compilation its own diagnostic owner + +Start at [ModuleCompiler](src/main/java/org/xvm/api/ModuleCompiler.java) or +[EmbeddingSupport](src/main/java/org/xvm/api/EmbeddingSupport.java). Supply the attempt's listener +at the entry point and keep it available until the result and diagnostics have been consumed. +For example, with a configured core repository and source text: + +```java +final var errors = new ErrorList(100); +var compiler = new ModuleCompiler(coreRepository); +var module = compiler.compile(sourceText, null, errors); +var diagnostics = List.copyOf(errors.getErrors()); + +if (module == null || errors.hasSeriousErrors()) { + // Publish the diagnostics and do not treat this as a successful compilation. + return; +} +// The caller can now use the successfully compiled module. +``` + +The null argument here is the optional dependency repository, not the listener. The required +listener also reaches launcher construction and dispatch, the compiler, lexer/parser, +[StageMgr](src/main/java/org/xvm/compiler/ast/StageMgr.java), anonymous-class construction and +[Component.SimpleCollector](src/main/java/org/xvm/asm/Component.java). +[ResolutionCollector](src/main/java/org/xvm/asm/ComponentResolver.java) implementations must +return their selected destination. AST reporting and validation helpers no longer silently +accept a missing listener. + +Use a fresh collector for an independent attempt. An `ErrorList` is mutable; even `clear()` keeps +its previously seen diagnostic UIDs. A new operation should not inherit that deduplication history. +Hosts must serialize compiler access and listener callbacks. Final references and non-null +parameters do not make compiler objects or their accumulated state thread-safe. + +#### 2. Report data, then make the control-flow decision + +[ErrorListener](src/main/java/org/xvm/asm/ErrorListener.java) remains the central interface. +Its nested `ErrorInfo` carries severity, code, message arguments and location. `Site.In`, `Site.At` +and `Site.None` describe the location independently of the message: ```java -ErrorListener errors = ErrorListener.collecting(hostDiagnostics::add); errors.error(code, ErrorListener.in(source, start, end), arguments); if (errors.isAbortDesired()) { return; } ``` -`collecting` remembers severity and reported codes and forwards every report without deduplication. -A bare `ErrorListener` lambda only implements reporting and inherits state queries that always -answer false. Use `ErrorList` when repeated diagnostics should collapse or a count budget is needed: -its default constructor uses `DEFAULT_MAX_ERRORS`; `FIRST_ERROR` and `UNLIMITED` name other policies. -A fatal diagnostic requests an abort even with an unlimited budget. - -`Site.In`, `Site.At` and `Site.None` describe source spans, structures and unpositioned diagnostics. -The severity helpers accept message arguments as trailing varargs. The old positional forms still -work after recompilation, including source re-anchoring through a branch. The legacy `BLACKHOLE` -constant/class remain while callers migrate to `silent(Silence.PROBE/CASCADE/DISCARD)` or a listener's -`silence(...)` wrapper. A derived silence keeps its parent's abort request. - -A custom listener's default branch buffers without an arbitrary one-error budget and observes the -parent's abort state. `ErrorList` branches also retain their configured budget. Merging an unbranched -listener returns itself. `tee` forwards reports to both sinks and honors either sink's abort state. -Callbacks must be serialized by the host; these helpers do not make compiler execution concurrent. - -`RUNTIME` now prints diagnostics and records an abort request for ERROR/FATAL; reporting no longer -throws. Runtime callers that depended on that exception must explicitly check `isAbortDesired()` -and choose their own failure handling. Parser and lexer reporting sites have been migrated to that -sequence. This slice retains the existing null/ambient listener policy; later ownership changes -must document their additional compatibility effects separately. +Use `in(source, start, end)` for source text, `at(structure)` for an assembled structure, and +`NOWHERE` for a report without either location. Source positions are the encoded values returned +by `Source.getPosition()`, not raw byte offsets or LSP positions. The `error`, `warn`, `info` and +`fatal` helpers select severity and delegate to `log`; they do not transfer control themselves. + +`isAbortDesired()` and `hasSeriousErrors()` answer different questions. An operation can have +errors while remaining within its budget, and a host may request a stop without reporting an +error. Callers use the first query at stop checkpoints and the second when deciding whether a +result is usable. `hasError(code)` checks whether a particular diagnostic was recorded. + +`RuntimeErrorListener` now prints diagnostics and records an abort request for ERROR/FATAL +instead of deliberately throwing from `log`. Its abort flag remains set for that instance's +lifetime; the shared `RUNTIME` sink is not a fresh per-attempt collector. A reporting callback +can still throw its own exception, which forwarding helpers propagate. The redesign removes the +fallback sink's severity-driven exception policy, not Java exception handling in general. + +#### 3. Choose retention, callback delivery or both + +[ErrorList](src/main/java/org/xvm/asm/ErrorList.java) retains the first report for each diagnostic +UID in insertion order. ERROR/FATAL reports spend its count budget; warnings do not. Its +no-argument constructor uses the shared `ErrorListener.DEFAULT_MAX_ERRORS` (512), `FIRST_ERROR` +selects one, and `UNLIMITED` disables the count limit. A retained FATAL requests an abort with +any budget. Pass an explicit limit to `ErrorList(int)` when an operation needs a different policy. + +When a host wants every report delivered to a callback, use: + +```java +ErrorListener errors = ErrorListener.collecting(hostDiagnostics::add); +``` + +`collecting` forwards every report, including duplicates, and tracks severity and codes before +calling the consumer. It has no count budget and requests an abort on FATAL. A bare lambda +implements only `log` and inherits state queries that return false, so it is not a substitute +when the compiler relies on those queries. + +`tee(first, second)` delivers to both destinations in order and observes either destination's +abort, serious-error and code state. This supports reporting to a host while recording diagnostics +for later replay. `ErrorList.logTo(destination)` replays retained reports without clearing them; +deduplication belongs to the destination. If the first destination throws, the second is not called. + +#### 4. Buffer speculative diagnostics until a result is accepted + +`branch(node)` creates an `ErrorList.BranchedErrorListener`. Pass it to speculative work and merge +it once if that work's diagnostics should be retained: + +```java +var trial = errors.branch(node); +// Run the candidate validation with trial as its listener. +if (keepDiagnostics) { + trial.merge(); +} +``` + +Unmerged diagnostics remain local to the branch. A default host branch has an unlimited count +budget, while an `ErrorList` branch copies its parent's configured limit. Both observe the +parent's current abort request, and a local FATAL still stops the branch. A node-associated +branch re-anchors structure reports to that node's source span; explicit source spans remain intact. + +Merging an unbranched listener returns that listener. Merging a branch forwards its reports and +returns the parent without clearing the buffer. Do not repeatedly merge an accepted branch: +an `ErrorList` parent can deduplicate replay, but an arbitrary callback need not. + +#### 5. Suppress reports without losing the operation's owner + +`Silence` and `SilentErrorListener`, nested in `ErrorListener`, make suppression explicit. +`PROBE`, `CASCADE` and `DISCARD` are **reasons, not severity levels**. They have identical discard +behavior; compiler algorithms must not depend on which name is selected. + +| Reason | Use it when | +|---|---| +| `PROBE` | Testing an alternative, such as whether an expression fits a candidate type. Failure is the answer to that query, not necessarily a problem in the selected program. | +| `CASCADE` | Examining already incomplete information whose follow-on errors would obscure the original failure. | +| `DISCARD` | Deliberately ignoring this destination, such as the CLI's external delegate when its console already reports diagnostics. | + +The important behavioral distinction is between a standalone sink and a derived silence: + +```java +// No parent listener or stop policy: an independent, deliberately silent probe. +ErrorListener independent = ErrorListener.silent(ErrorListener.Silence.PROBE); + +// Suppress follow-on reports but continue to honor this operation's abort request. +ErrorListener quiet = errors.silence(ErrorListener.Silence.CASCADE); +``` + +`independent` never requests an abort. `quiet.isAbortDesired()` observes `errors`, even when +no diagnostic caused that stop request. Neither retains discarded messages; use a branch when +diagnostics might later be needed. `suppressed()` exposes a derived silence's parent, not a buffer +of hidden diagnostics. Re-silencing an existing silence returns the same instance and reason. + +For a separately constructed standalone sink, `new SilentErrorListener(reason)` needs no +explicit null parent. The two-argument constructor retains an optional parent for callers that +need its stop policy. Required factory arguments and results use `@NotNull`; intentional absence +uses `@Nullable`, including `suppressed()` on a standalone sink. Public construction boundaries +still reject invalid null arguments immediately; annotations also expose the contract to IDEs +and Kotlin callers. + +The names make intent searchable and reviewable. `BLACKHOLE` did not distinguish those purposes, +and substituting it for an active listener also removed access to that listener's stop policy. +Removing the old constant and class makes missed migrations visible to the Java compiler. A +deprecated alias was possible, but would allow unexplained suppression to survive the migration. +Callers that want exactly the old standalone discard behavior can use `silent(DISCARD)`. + +Stable ownership is equally important. In +[TypeConstant](src/main/java/org/xvm/asm/constants/TypeConstant.java), `cascade(incomplete, errors)` +derives suppression at affected calls instead of assigning a sink to the `errors` parameter. +The original destination remains available throughout the build. Constructor-owned references, +such as those in `StageMgr`, `SimpleCollector` and `Launcher`, are final so they cannot drift to +another destination during the operation. Their listeners' counters can still change. + +Non-null means that an active reporting boundary has an explicit owner or an explicit discard +policy. It does not mean that every listener field everywhere must be permanently non-null or +final: nested parser/resolver work needs temporary destinations and scoped restoration. That +ownership work is described separately below. + +#### 6. Migrate Java callers and implementations + +The planned release boundary is **0.5.0**. Recompile Java clients and listener implementations +after applying these changes; Java cannot retain compatibility overloads that differ only by +the return type of `log`. + +| Previous use | Replacement and consequence | +|---|---| +| `if (errors.log(...))` | Report first, then query `errors.isAbortDesired()` and perform the same return/throw at that checkpoint. Update custom implementations to `void log(ErrorInfo)`. | +| `BLACKHOLE` or `BlackholeErrorListener` | Choose a named standalone silence or derive one from the current owner when its abort policy must survive. | +| Null at a compilation/launcher boundary | Pass the operation's listener or explicit `silent(DISCARD)`. Missing listeners now fail at the boundary. | +| A `ResolutionCollector` relying on the default sink | Implement `getErrorListener()` and retain the intended destination. | +| `testFitAsType(..., errors)` | Remove the listener argument; the operation owns its probe policy. | +| Depending on `RUNTIME.log` to throw on severity | Check `isAbortDesired()` and explicitly choose the operation's failure handling. | +| Legacy positional reporting overloads | They remain callable after recompilation but are deprecated; migrate to `Site` and trailing message arguments. | + +### Regression evidence and how to reproduce it + +The strongest evidence is the pair of +[host regression tests](src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java). +Their source uses API shapes available on both sides of the reporting change. On the previous +implementation at `1d9352b6f`, both compile and fail for the expected behavioral reason. On the +repaired implementation, the same source passes both cases: + +* `hostBranchLetsLexerReportBothErrors` sends two bad string escapes through the real lexer. + Previously it threw after the first error because the default branch had a one-error budget. + It now completes the scan and publishes both reports when the branch is merged. +* `parentAbortReachesTheLexerThroughAnExistingBranch` aborts the parent after creating its branch. + Previously the lexer continued because the branch considered only its local state. It now + honors the parent at its reporting checkpoint. + +[ErrorListenerMigrationTest](src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java) also proves +that derived silence preserves a host stop request without publishing an error. Its parser/lexer +budget tests and source-reanchoring test guard existing behavior. The branch, site, silence and +abort suites cover collection, forwarding, replay and suppression. +[ErrorListenerBoundaryTest](src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java) checks required +listeners and destination retention; these assertions enforce the chosen API contract rather than +claiming that every API change reproduces an old bug. + +The 2026-10-09 focused run on this implementation passed **60 tests with zero failures, errors +or skips**. Earlier full-suite verification of the same production listener migration passed +418 executed Java tests, with 42 skips recorded separately. A forced XDK rebuild produced +24 modules matching the master baseline after normalizing creation timestamps. The separate +integration checkout combining the reporting changes with the embedding foundations also passed +the real compiler-consumer test with no skips. These checks cover different scopes; the 60 focused +tests are not presented as a substitute for compilation and integration validation. + +Run the focused tests from the repository root: + +```bash +./gradlew :javatools:test \ + --tests 'org.xvm.asm.ErrorListener*Test' \ + --tests org.xvm.api.ErrorListenerBoundaryTest \ + --tests org.xvm.tool.LauncherErrorHandlingTest \ + --rerun-tasks --no-build-cache +``` + +These focused cases do not require installed XDK modules. For broader compiler verification, +build the XDK first, then force the Java test run: + +```bash +./gradlew :xdk:installDist +./gradlew :javatools:test --rerun-tasks --no-build-cache +``` + +Check failures and skips in `javatools/build/test-results/test/TEST-*.xml`; a green task alone does +not establish that optional tests executed. The real compiler-consumer integration test additionally +exercises the standard build path with compiled modules supplied by its Gradle dependencies. + +### Review scope and related work + +This listener-ownership migration builds on +[PR #680: separating diagnostic reporting from abort policy](https://github.com/xtclang/xvm/pull/680). +That pull request introduces the listener contract, factories and branch fixes. This change +migrates compiler callers to explicit destinations and named suppression, including the TypeInfo +cascade calls and required-listener boundaries. Most of the broad file coverage follows existing +reporting calls; review their severity, code, arguments and location alongside the substantive +ownership changes. + +The separate **scoped parser and validation reporting** branch handles temporary destinations, +parser attempts and restoration after normal, early and exceptional exits. Further +**explicit structure reporting and TypeInfo diagnostic replay** work removes ambient file/pool +lookup and preserves diagnostics associated with cached semantic results. Those changes belong +to their own review boundaries and are not claimed as completed by this migration. + +In this branch, the file/pool ambient-listener API and parser/resolver ownership are still present. +`XvmStructure.log` still has its existing fallback when passed null; the active compilation +boundaries listed above do not. This migration is a prerequisite for complete host reporting, +not a claim that every compiler-failure or TypeInfo-cache path is already repaired. ## Assembler @@ -98,4 +413,4 @@ The runtime is currently implemented as an interpreter, and the interpreter (which would be naturally slow to begin with) has not been optimized. Its purpose is to be malleable and easy to test, so that we could prove out the design of the compiler -and the Ecstasy IR. \ No newline at end of file +and the Ecstasy IR. diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index 11a36c56a07..ac4e9ee350e 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -12,6 +12,8 @@ import java.util.Objects; import java.util.Set; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.ConstantPool; import org.xvm.asm.DirRepository; import org.xvm.asm.ErrorListener; @@ -28,8 +30,12 @@ import org.xvm.runtime.template._native.io.xExternalConsole; +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.at; + import static org.xvm.util.Handy.quoted; -import static org.xvm.util.Severity.ERROR; /** * A class used to support embedding Ecstasy tools. This implementation uses the Connector API to @@ -210,14 +216,15 @@ public ConstantPool getConstantPool() { * * @param source the source code for an entire module to compile * @param input (optional) the module repository to read any required modules from - * @param errs (optional) the ErrorListener to log any compiler messages to + * @param errs the ErrorListener to log any compiler messages to * * @return the resulting ModuleStructure, or null if a compiler error occurred * * @see ModuleCompiler#compile(String, ModuleRepository, ErrorListener) */ - public ModuleStructure compile(String source, ModuleRepository input, ErrorListener errs) { + public ModuleStructure compile(String source, ModuleRepository input, @NotNull ErrorListener errs) { verifyConfigured(); + requireNonNull(errs, "errs"); return new ModuleCompiler(cfgRepo).compile(source, input, errs); } @@ -227,15 +234,16 @@ public ModuleStructure compile(String source, ModuleRepository input, ErrorListe * @param file the module source file * @param input (optional) the module repository to read any required modules from * @param output (optional) the module repository to write any compiled modules to - * @param errs (optional) the ErrorListener to log any compiler messages to + * @param errs the ErrorListener to log any compiler messages to * * @return true if the compilation succeeded and the result was placed into the output * * @see ModuleCompiler#compile(File, ModuleRepository, ModuleRepository, ErrorListener) */ public boolean compile(File file, ModuleRepository input, ModuleRepository output, - ErrorListener errs) { + @NotNull ErrorListener errs) { verifyConfigured(); + requireNonNull(errs, "errs"); return new ModuleCompiler(cfgRepo).compile(file, input, output, errs); } @@ -358,7 +366,7 @@ public interface Control * task-specific directory under "./.runner" that is deleted when the * returned Control is closed * @param injections (optional) additional "String" and "String[]" injections - * @param errs (optional) a means for the container to report uncaught exceptions and + * @param errs a means for the container to report uncaught exceptions and * other errors * * @return a Control object for the running module @@ -394,7 +402,7 @@ public Control run( * @param customInjector (optional) "module:class" name of a custom injector implementation to * use to provide injectable resources; when used, the "rootDir" value is * ignored - * @param errs (optional) a means for the container to report uncaught exceptions and + * @param errs a means for the container to report uncaught exceptions and * other errors * * @return a Control object for the running module, or null if it could not be started, in @@ -408,7 +416,7 @@ public Control run( File rootDir, Map> injections, String customInjector, - ErrorListener errs) { + @NotNull ErrorListener errs) { if (injections != null && !injections.isEmpty() || customInjector != null) { throw new UnsupportedOperationException("Custom injectors are not implemented yet"); } @@ -434,34 +442,31 @@ public Control run( * as an empty line. Used only with a console * @param rootDir (optional) the root directory for the application's file system, as * described for {@link #run(ModuleStructure, PrintWriter, File, Map, ErrorListener)} - * @param errs (optional) a means for the container to report uncaught exceptions and - * other errors + * @param errs a means for the container to report uncaught exceptions and other errors * * @return a Control object for the running module, or null if it could not be started, in * which case the reason is reported to "errs" */ public Control runMethod( - ModuleRepository input, - String moduleName, - Version version, - EntryPoint entry, - PrintWriter console, - ConsoleInput consoleIn, - File rootDir, - ErrorListener errs) { + ModuleRepository input, + String moduleName, + Version version, + EntryPoint entry, + PrintWriter console, + ConsoleInput consoleIn, + File rootDir, + @NotNull ErrorListener errs) { Objects.requireNonNull(entry, "entry"); Objects.requireNonNull(consoleIn, "consoleIn"); verifyConfigured(); + requireNonNull(errs, "errs"); ModuleRepository repository = new LinkedRepository(input, cfgRepo); ModuleStructure module = version == null ? repository.loadModule(moduleName) : repository.loadModule(moduleName, version, true); if (module == null) { - if (errs != null) { - errs.log(ERROR, version == null ? ERR_NO_APP_MODULE : ERR_NO_APP_MODULE_VER, - new Object[] {moduleName, version}, null); - } + errs.error(version == null ? ERR_NO_APP_MODULE : ERR_NO_APP_MODULE_VER, NOWHERE, moduleName, version); return null; } @@ -480,10 +485,7 @@ public Control runMethod( // assertion in the structure code past this report and out to the host. Errors are // not caught wholesale: a VirtualMachineError says the JVM is in trouble, not that // this module failed to start, and handling one is not something to rely on - if (errs != null) { - errs.log(ERROR, ERR_CREATE_APP_CONTAINER, - new Object[] {e, "Unable to start " + moduleName}, module); - } + errs.error(ERR_CREATE_APP_CONTAINER, at(module), e, "Unable to start " + moduleName); return null; } } diff --git a/javatools/src/main/java/org/xvm/api/InterpreterControl.java b/javatools/src/main/java/org/xvm/api/InterpreterControl.java index bb0f412475f..e0e7e5215e3 100644 --- a/javatools/src/main/java/org/xvm/api/InterpreterControl.java +++ b/javatools/src/main/java/org/xvm/api/InterpreterControl.java @@ -38,13 +38,12 @@ import org.xvm.runtime.template._native.mgmt.xCoreRepository; import org.xvm.runtime.template._native.reflect.xRTModuleTemplate; +import static org.xvm.asm.ErrorListener.at; + import static org.xvm.api.EmbeddingSupport.ERR_ENTRY_METHOD; import static org.xvm.api.EmbeddingSupport.ERR_UNHANDLED_EXCEPTION; import static org.xvm.api.EmbeddingSupport.WARN_ARGUMENTS_IGNORED; -import static org.xvm.util.Severity.ERROR; -import static org.xvm.util.Severity.WARNING; - /** * Interpreter-backed management and monitoring for one runner task. */ @@ -89,14 +88,12 @@ static EmbeddingSupport.Control create(Connector connector, ModuleStructure modu } catch (IllegalArgumentException e) { // the method is why the run does not start, as the command-line runner reports it; // its container was never the problem - if (errs != null) { - errs.log(ERROR, ERR_ENTRY_METHOD, new Object[] {e.getMessage()}, module); - } + errs.error(ERR_ENTRY_METHOD, at(module), e.getMessage()); return null; } - if (errs != null && !entry.args().isEmpty() && method.getParamCount() == 0) { + if (!entry.args().isEmpty() && method.getParamCount() == 0) { // the arguments are dropped, with the command-line runner's warning - errs.log(WARNING, WARN_ARGUMENTS_IGNORED, new Object[] {entry.name()}, module); + errs.warn(WARN_ARGUMENTS_IGNORED, at(module), entry.name()); } if (console != null) { @@ -193,9 +190,7 @@ private void finish(long result, String failure) { this.result = result; } else { this.result = null; - if (errs != null) { - errs.log(ERROR, ERR_UNHANDLED_EXCEPTION, new Object[] {failure}, module); - } + errs.error(ERR_UNHANDLED_EXCEPTION, at(module), failure); } } finally { this.stopped = Instant.now(); diff --git a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java index a70c520b812..b3d24cacdbe 100644 --- a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java +++ b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java @@ -6,6 +6,8 @@ import java.util.List; import java.util.Objects; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; import org.xvm.asm.FileStructure; @@ -31,6 +33,9 @@ import static org.xvm.api.EmbeddingSupport.ERR_INTERNAL; +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.at; + import static org.xvm.util.Handy.readFileChars; import static org.xvm.util.Severity.ERROR; @@ -74,11 +79,12 @@ public ModuleCompiler(ModuleRepository coreRepo) { * * @param source the source code for an entire module to compile * @param input (optional) the module repository to read any required modules from - * @param errs (optional) the ErrorListener to log any compiler messages to + * @param errs the ErrorListener to log any compiler messages to * * @return the resulting ModuleStructure, or null if a compiler error occurred */ - public ModuleStructure compile(String source, ModuleRepository input, ErrorListener errs) { + public ModuleStructure compile(String source, ModuleRepository input, @NotNull ErrorListener errs) { + Objects.requireNonNull(errs, "errs"); try { EmbeddingCompiler compiler = new EmbeddingCompiler(source, input, coreRepo, errs); return compiler.process() == 0 @@ -88,18 +94,15 @@ public ModuleStructure compile(String source, ModuleRepository input, ErrorListe // the compiler stops at the end of a stage that logged errors; those errors are already // in "errs", so this is an ordinary failed compile, as it is for the command-line // compiler. A tool-level failure is logged only to the silent console, so report it - if (errs != null && !errs.hasSeriousErrors()) { - errs.log(ERROR, ERR_INTERNAL, new Object[] {e, "Compilation failed"}, null); + if (!errs.hasSeriousErrors()) { + errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed"); } return null; } catch (RuntimeException | AssertionError e) { // as in EmbeddingSupport.run(): the compiler runs over caller-supplied source, so a // failure in it is reported here rather than thrown at the caller, who was promised a // null instead - if (errs != null) { - errs.log(ERROR, ERR_INTERNAL, - new Object[] {e, "Compilation failed"}, null); - } + errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed"); return null; } } @@ -110,25 +113,23 @@ public ModuleStructure compile(String source, ModuleRepository input, ErrorListe * @param file the module source file * @param input (optional) the module repository to read any required modules from * @param output (optional) the module repository to write any compiled modules to - * @param errs (optional) the ErrorListener to log any compiler messages to + * @param errs the ErrorListener to log any compiler messages to * * @return true if the compilation succeeded and the result was placed into the output */ public boolean compile(File file, ModuleRepository input, ModuleRepository output, - ErrorListener errs) { + @NotNull ErrorListener errs) { + Objects.requireNonNull(errs, "errs"); ModuleStructure module; try { module = compile(new String(readFileChars(file)), input, errs); } catch (IOException e) { - if (errs != null) { - errs.log(ERROR, ERR_INTERNAL, - new Object[] {e, "Unable to read module " + file}, null); - } + errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to read module " + file); return false; } if (module == null) { - assert errs == null || errs.hasSeriousErrors(); + assert errs.hasSeriousErrors(); return false; } @@ -136,10 +137,7 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu try { output.storeModule(module); } catch (IOException e) { - if (errs != null) { - errs.log(ERROR, ERR_INTERNAL, - new Object[] {e, "Unable to store module " + module.getName()}, module); - } + errs.error(ERR_INTERNAL, at(module), e, "Unable to store module " + module.getName()); return false; } } diff --git a/javatools/src/main/java/org/xvm/asm/ClassStructure.java b/javatools/src/main/java/org/xvm/asm/ClassStructure.java index 82eb7fa18f0..d8114bcb444 100644 --- a/javatools/src/main/java/org/xvm/asm/ClassStructure.java +++ b/javatools/src/main/java/org/xvm/asm/ClassStructure.java @@ -37,6 +37,9 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.readIndex; import static org.xvm.util.Handy.readMagnitude; import static org.xvm.util.Handy.writeMagnitude; @@ -987,7 +990,7 @@ public ResolutionResult resolveName(String sName, Access access, ResolutionColle if (result == ResolutionResult.UNKNOWN && getFormat() == Format.SERVICE) { // look into the Service interface itself ClassStructure clzSvc = (ClassStructure) getConstantPool().clzService().getComponent(); - SimpleCollector collectorSvc = new SimpleCollector(ErrorListener.BLACKHOLE); + SimpleCollector collectorSvc = new SimpleCollector(silent(PROBE)); ResolutionResult resultSvc = clzSvc.resolveName(sName, Access.PROTECTED, collectorSvc); if (resultSvc == ResolutionResult.RESOLVED) { // only allow child classes; properties and methods are resolved by the TypeInfo diff --git a/javatools/src/main/java/org/xvm/asm/Component.java b/javatools/src/main/java/org/xvm/asm/Component.java index 53f60fd41bc..92cec3ad290 100644 --- a/javatools/src/main/java/org/xvm/asm/Component.java +++ b/javatools/src/main/java/org/xvm/asm/Component.java @@ -24,6 +24,8 @@ import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.constants.ClassConstant; import org.xvm.asm.constants.ConditionalConstant; import org.xvm.asm.constants.IdentityConstant; @@ -46,7 +48,10 @@ import org.xvm.util.Handy; import org.xvm.util.Hash; import org.xvm.util.ListMap; -import org.xvm.util.Severity; + +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.at; import static org.xvm.util.Handy.readIndex; import static org.xvm.util.Handy.readMagnitude; @@ -2012,8 +2017,8 @@ protected ResolutionResult resolveContributedName( } if (m_FVisited != null && m_FVisited.booleanValue() == fAllowInto) { // recursive contribution - collector.getErrorListener().log(Severity.FATAL, Constants.VE_CYCLICAL_CONTRIBUTION, - new Object[] {getName(), contrib.getComposition().toString().toLowerCase()}, this); + collector.getErrorListener().fatal(Constants.VE_CYCLICAL_CONTRIBUTION, at(this), + getName(), contrib.getComposition().toString().toLowerCase()); return ResolutionResult.ERROR; } @@ -3528,8 +3533,8 @@ public String toString() { */ public static class SimpleCollector implements ResolutionCollector { - public SimpleCollector(ErrorListener errs) { - m_errs = errs; + public SimpleCollector(@NotNull ErrorListener errs) { + m_errs = requireNonNull(errs, "errs"); } @Override diff --git a/javatools/src/main/java/org/xvm/asm/ComponentResolver.java b/javatools/src/main/java/org/xvm/asm/ComponentResolver.java index 930c81bd884..3f69ba5d8a1 100644 --- a/javatools/src/main/java/org/xvm/asm/ComponentResolver.java +++ b/javatools/src/main/java/org/xvm/asm/ComponentResolver.java @@ -63,9 +63,12 @@ default AstNode getNode() { /** * Provide an ErrorListener to report resolution issues to. + * + * Deliberately not defaulted. A default of "discard" would mean a collector that had not + * thought about diagnostics and one that had decided against them looked identical at the + * point of resolution, which is the distinction the listener exists to make. A collector + * that genuinely wants none says so by naming a silent listener. */ - default ErrorListener getErrorListener() { - return ErrorListener.BLACKHOLE; - } + ErrorListener getErrorListener(); } } diff --git a/javatools/src/main/java/org/xvm/asm/ErrorList.java b/javatools/src/main/java/org/xvm/asm/ErrorList.java index e683f8c7bc1..1c69f7e8444 100644 --- a/javatools/src/main/java/org/xvm/asm/ErrorList.java +++ b/javatools/src/main/java/org/xvm/asm/ErrorList.java @@ -9,57 +9,77 @@ import org.xvm.util.Severity; /** - * Represents a list of errors collected from a process such as compilation, assembly, or the - * verifier, with an option to abort the process should a maximum number of errors be exceeded. + * An operation's diagnostic buffer with deduplication, severity state, and an optional count budget. + * + *

The first report for each {@link ErrorInfo#genUID()} is retained in insertion order. Each + * retained ERROR or FATAL counts toward the budget; warnings and informational reports do not. + * {@link #isAbortDesired()} becomes true when a positive budget is reached, or after any FATAL. + * Logging still records subsequent reports until the caller honors that stop request. + * + *

Use this for compilation results that must retain diagnostics, and + * {@link ErrorListener#collecting(java.util.function.Consumer)} when a host callback should receive + * every report without deduplication. Instances are mutable and are not thread-safe. */ public class ErrorList implements ErrorListener { // ----- constructors -------------------------------------------------------------------------- /** - * Construct a list that tolerates {@link #DEFAULT_MAX_ERRORS} serious errors, for a caller - * with no reason to choose a number. A caller that has one says so with the other - * constructor, naming {@link #UNLIMITED} or {@link #FIRST_ERROR} where those are what it - * means. + * Construct a buffer that requests an abort on its {@link ErrorListener#DEFAULT_MAX_ERRORS}-th serious report. + * + *

Use {@link #ErrorList(int)} with {@link #FIRST_ERROR}, {@link #UNLIMITED}, or a caller-specific + * positive budget when that policy is required. */ public ErrorList() { - this(DEFAULT_MAX_ERRORS); + this(ErrorListener.DEFAULT_MAX_ERRORS); } /** - * @param cMaxErrors the number of serious errors to tolerate before asking for the process to - * be abandoned, or {@link #UNLIMITED} to tolerate any number + * Construct a buffer with the requested serious-error budget. + * + * @param cMaxErrors request an abort when this many retained ERROR/FATAL reports have arrived; + * non-positive values disable the count limit, conventionally {@link #UNLIMITED} */ public ErrorList(int cMaxErrors) { f_cMaxErrors = cMaxErrors; } /** - * Tolerate any number of serious errors: only a FATAL asks for the process to be abandoned. + * Disable the count budget. A retained FATAL still requests an abort. */ public static final int UNLIMITED = 0; /** - * The budget for a caller that wants to stop at the first serious error. + * Request an abort after the first retained ERROR or FATAL. */ public static final int FIRST_ERROR = 1; + // ----- ErrorListener methods ----------------------------------------------------------------- + /** - * How many serious errors to tolerate when the caller has no reason to choose a number. + * Create a diagnostic buffer with this list's count budget and parent abort policy. + * + *

The child counts its own retained reports; it does not subtract the parent's count from its + * budget. It requests an abort when either its own policy or the parent's policy says to stop. + * Reports reach this list only when the child is merged. * - *

Enough that a file with a genuine spread of problems reports them all, and few enough that - * source which has gone badly wrong - a mismatched brace early on, say - stops rather than - * producing a page of consequences. + * @param node the optional source context for structure-site diagnostics + * + * @return a new child buffer */ - public static final int DEFAULT_MAX_ERRORS = 100; - - // ----- ErrorListener methods ----------------------------------------------------------------- - @Override public ErrorListener branch(AstNode node) { return new BranchedErrorListener(this, f_cMaxErrors, node); } + /** + * Record the first report for this diagnostic's UID and update severity and budget state. + * + *

Duplicate UIDs do not add entries or spend the count budget. This method does not stop the + * caller or refuse reports after the budget is reached; query {@link #isAbortDesired()} explicitly. + * + * @param err the diagnostic to record + */ @Override public void log(ErrorInfo err) { String uid = err.genUID(); @@ -81,6 +101,11 @@ public void log(ErrorInfo err) { } } + /** + * Test whether a retained FATAL or the configured serious-error count requests an abort. + * + * @return true for FATAL, or when a positive budget has been reached + */ @Override public boolean isAbortDesired() { return m_severity == Severity.FATAL || f_cMaxErrors > 0 && @@ -148,7 +173,10 @@ public List getErrors() { } /** - * Clear the list of errors, resetting the error collection state. + * Clear the retained reports, serious-error count, and worst severity. + * + *

Previously seen diagnostic UIDs remain recorded, so logging the same diagnostic after + * clearing still suppresses it. Use a new ErrorList for a new independent operation. */ public void clear() { f_list.clear(); @@ -157,9 +185,12 @@ public void clear() { } /** - * Log the errors from this ErrorList into another ErrorListener. + * Replay all retained diagnostics to another listener in insertion order. * - * @param errs the ErrorListener to log all the errors from this ErrorList to + *

This neither clears this list nor queries the destination's abort policy. Repeated calls + * replay the same reports; deduplication, if desired, is the destination's responsibility. + * + * @param errs the destination for the retained reports */ public void logTo(ErrorListener errs) { for (ErrorInfo err : getErrors()) { @@ -181,10 +212,21 @@ public String toString() { // ----- inner class: BranchedErrorListener ---------------------------------------------------- /** - * The ErrorListener that can be used to capture errors that may or may not be reported. + * A temporary diagnostic buffer whose accepted reports can be merged into a parent listener. + * + *

It retains its own deduplication and count state while observing the parent's abort request. + * Discarding the branch leaves the parent's diagnostics untouched. Merging forwards reports and + * returns the parent without clearing the branch, so callers should merge an accepted branch once. */ public static class BranchedErrorListener extends ErrorList { + /** + * Construct a speculative buffer associated with a parent and optional source node. + * + * @param listener the parent destination and additional abort policy + * @param cMaxErrors this branch's serious-error budget, or {@link ErrorList#UNLIMITED} + * @param node the optional source context for structure-site reports + */ public BranchedErrorListener(ErrorListener listener, int cMaxErrors, AstNode node) { super(cMaxErrors); diff --git a/javatools/src/main/java/org/xvm/asm/ErrorListener.java b/javatools/src/main/java/org/xvm/asm/ErrorListener.java index 2b7b7628f8b..cd02dc5a9d3 100644 --- a/javatools/src/main/java/org/xvm/asm/ErrorListener.java +++ b/javatools/src/main/java/org/xvm/asm/ErrorListener.java @@ -12,6 +12,9 @@ import java.util.function.Consumer; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + import org.xvm.compiler.Source; import org.xvm.compiler.ast.AstNode; @@ -23,23 +26,60 @@ import static org.xvm.util.Handy.quotedString; /** - * A listener for errors being reported about source code, compilation, assembly, or verification of - * XVM structures. + * Receives diagnostics from compilation, assembly, and verification, and exposes the state that + * callers use to decide whether work may continue. + * + *

Reporting and continuation are separate operations. {@link #log(ErrorInfo)} delivers a + * diagnostic; {@link #isAbortDesired()} answers the current stop policy. A compiler caller that + * previously consumed the boolean result of {@code log} must query the policy explicitly: + *

{@code
+ * errors.error(code, ErrorListener.in(source, start, end), arguments);
+ * if (errors.isAbortDesired()) {
+ *     return;
+ * }
+ * }
+ *

This gives forwarding listeners one place to preserve a parent's error budget or abort + * request. It also lets a host query a stop policy independently of emitting another diagnostic. + * Changing {@code log} to {@code void} is an intentional source and binary API change; existing + * Java implementations and callers must migrate and recompile. + * + *

Use {@link #collecting(Consumer)} to deliver diagnostics to a host callback while maintaining + * severity and code state, {@link ErrorList} to retain and deduplicate reports with an error + * budget, and {@link #tee(ErrorListener, ErrorListener)} to forward to two destinations. A bare + * lambda implements only reporting and inherits state queries that always return {@code false}. + * + *

Use {@link #branch(AstNode)} when speculative diagnostics may need to be kept, or + * {@link #silence(Silence)} when they must be discarded while preserving this listener's abort + * policy. {@link #silent(Silence)} is a standalone discard sink with no parent policy. The named + * reasons replace the ambiguous intent of an unnamed discard sink such as {@code BLACKHOLE}; + * they do not select different compiler modes. + * + *

Keep an operation's original listener reference stable. Pass a derived silence or branch to + * temporary work instead of replacing the owner with a discard sink. This preserves the destination + * and its stop policy for later work; it does not make the listener's accumulated state immutable. + * + *

Listeners belong to an operation. Hosts must serialize callbacks and compiler access; + * these factories do not make compilation or listener state thread-safe. Reporting callbacks + * may throw, and the forwarding helpers do not catch or translate those exceptions. + * + *

Factory and constructor parameters annotated {@link NotNull} are required collaborators, + * not optional reporting destinations. Null arguments are rejected when the listener is created. + * {@link Nullable} marks intentional absence, such as a standalone silence's parent. */ @FunctionalInterface public interface ErrorListener { // ----- API ----------------------------------------------------------------------------------- /** - * Handles the logging of an error that originates in Ecstasy source code. + * Deliver one diagnostic to this listener. * - *

Recording a diagnostic says nothing about whether the work should continue; ask - * {@link #isAbortDesired()} for that. The two used to be one boolean, which left a listener - * that only wants to watch with no correct value to return - false suppressed a legitimate - * abort and true invented one - and made whether the compiler kept going a property of who was - * listening. + *

Implementations decide whether to retain, forward, deduplicate, or discard the report. + * Reporting supplies no continuation result. Call {@link #isAbortDesired()} at the caller's + * stop checkpoints, and use {@link #hasSeriousErrors()} or {@link #hasError(String)} when deciding + * whether a compiler result is usable. A custom reporting callback must maintain those queries + * itself or be wrapped with {@link #collecting(Consumer)}. * - * @param err the error info + * @param err the diagnostic to report */ void log(ErrorInfo err); @@ -54,7 +94,10 @@ public interface ErrorListener { * @param source the source code (optional) * @param lPosStart the position in the source where the error was detected * @param lPosEnd the position in the source at which the error concluded + * + * @deprecated use {@link #log(Severity, String, Site, Object...)} with {@link #in} */ + @Deprecated default void log(Severity severity, String sCode, Object[] aoParam, Source source, long lPosStart, long lPosEnd) { log(new ErrorInfo(severity, sCode, aoParam, source, lPosStart, lPosEnd)); } @@ -69,7 +112,10 @@ default void log(Severity severity, String sCode, Object[] aoParam, Source sourc * @param aoParam the parameters for the error message; may be null * @param xs the XvmStructure that the error is related to; may * be null + * + * @deprecated use {@link #log(Severity, String, Site, Object...)} with {@link #at} */ + @Deprecated default void log(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) { log(severity, sCode, at(xs), aoParam); } @@ -77,17 +123,22 @@ default void log(Severity severity, String sCode, Object[] aoParam, XvmStructure // ----- reporting ----------------------------------------------------------------------------- /** - * Report a diagnostic. + * Construct and report a diagnostic with an explicit location and trailing message arguments. + * + *

Use {@link #in(Source, long, long)} for source text, {@link #at(XvmStructure)} for an + * assembled structure, or {@link #NOWHERE} when no location is available. Packaging the location + * as a {@link Site} leaves the final parameter available for varargs, so callers need not build + * an {@code Object[]} between the message code and location arguments. * - *

The message parameters are the trailing arguments, which is why the location is a - * {@link Site} rather than the two shapes it takes: an {@code Object[]} in the middle of the - * signature is what the older overloads need in order to leave room for the location after it, - * and it is why call sites have to write {@code new Object[]{...}} by hand. + *

The default implementation creates an {@link ErrorInfo} and delegates to {@link #log(ErrorInfo)}. + * It does not test the listener's abort policy or stop the caller. * - * @param severity the severity of the diagnostic - * @param sCode the error code that identifies the message - * @param site where the diagnostic belongs; see {@link #at} and {@link #in} - * @param aoParam the message parameters + * @param severity the diagnostic severity + * @param sCode the message code from the compiler's error resources + * @param site the diagnostic location; must not be null + * @param aoParam the message-format arguments, or an empty array when none are needed + * + * @throws NullPointerException if {@code site} is null */ default void log(Severity severity, String sCode, Site site, Object... aoParam) { switch (site) { @@ -98,7 +149,14 @@ default void log(Severity severity, String sCode, Site site, Object... aoParam) } /** - * Report that the caller's operation failed. + * Report an {@link Severity#ERROR} diagnostic for an operation that failed validation. + * + *

The listener's error budget determines whether this also requests an abort. Reporting an + * error does not itself stop the caller. + * + * @param sCode the message code + * @param site the diagnostic location; must not be null + * @param aoParam the message-format arguments * * @see #log(Severity, String, Site, Object...) */ @@ -107,7 +165,11 @@ default void error(String sCode, Site site, Object... aoParam) { } /** - * Report something the compiler recovered from. + * Report a {@link Severity#WARNING} diagnostic without classifying it as a serious error. + * + * @param sCode the message code + * @param site the diagnostic location; must not be null + * @param aoParam the message-format arguments * * @see #log(Severity, String, Site, Object...) */ @@ -116,7 +178,11 @@ default void warn(String sCode, Site site, Object... aoParam) { } /** - * Report something that is only being traced. + * Report an {@link Severity#INFO} diagnostic, for example an informational compiler message. + * + * @param sCode the message code + * @param site the diagnostic location; must not be null + * @param aoParam the message-format arguments * * @see #log(Severity, String, Site, Object...) */ @@ -125,8 +191,16 @@ default void info(String sCode, Site site, Object... aoParam) { } /** - * Report that the caller cannot continue. Throw on the next line; a diagnostic is a report, - * not a control-flow mechanism, and nothing should depend on what the listener does with it. + * Report a {@link Severity#FATAL} diagnostic for work that cannot continue successfully. + * + *

{@link ErrorList} and {@link #collecting(Consumer)} request an abort after a fatal report, + * regardless of their count policy. This method does not itself throw or transfer control; + * the caller must return, throw, or honor its next stop checkpoint. Code that cannot safely + * continue must stop explicitly even when the chosen listener discards the report. + * + * @param sCode the message code + * @param site the diagnostic location; must not be null + * @param aoParam the message-format arguments * * @see #log(Severity, String, Site, Object...) */ @@ -135,24 +209,24 @@ default void fatal(String sCode, Site site, Object... aoParam) { } /** - * A listener that hands each diagnostic to the given consumer, and answers the questions the - * compiler asks about what it has seen. + * Create a stateful listener that forwards every report to a host callback. * - *

This is how a host should build one. {@link ErrorListener} is a functional interface, so a - * bare lambda compiles - but a lambda only supplies {@link #log}, and inherits defaults for - * {@link #hasSeriousErrors()}, {@link #isAbortDesired()} and {@link #hasError} that answer as - * though nothing had been reported. The compiler asks those questions around a hundred times - * during a compilation, to decide whether a stage may proceed, so a host that lambdas the - * interface directly is telling the compiler that its own diagnostics did not happen. + *

Use this when an embedding host or editor collects diagnostics through a consumer, but the + * compiler still needs accurate state queries. Before invoking the consumer, the listener records + * the highest severity and the reported code. {@link #hasSeriousErrors()} becomes true for ERROR + * or FATAL, {@link #hasError(String)} recognizes codes from any severity, and + * {@link #isAbortDesired()} becomes true only for FATAL. There is no count budget. * - *

Every report is delivered; this factory does not deduplicate. Use {@link ErrorList} when - * repeated reports should collapse. Compiler callbacks must be serialized by the host. + *

Every report is forwarded, including duplicates; use {@link ErrorList} for deduplication or + * a count budget. If the consumer throws, its exception propagates and the recorded state remains. + * Create a new listener for each independent operation; there is no reset operation. * - * @param consumer receives each diagnostic as it is reported + * @param consumer the destination for each diagnostic, invoked synchronously * - * @return a listener that reports to the consumer and remembers what it reported + * @return a new listener that retains severity and code state while forwarding reports */ - static ErrorListener collecting(Consumer consumer) { + @NotNull + static ErrorListener collecting(@NotNull Consumer consumer) { requireNonNull(consumer, "consumer"); return new ErrorListener() { @Override @@ -191,17 +265,25 @@ public String toString() { } /** - * Report to both listeners. + * Create a listener that forwards reports to two destinations in order. * - *

The abort question is answered by either: a caller that wrapped a budgeted listener has to - * keep getting the stop it asked for, whatever else is also listening. + *

For example, combine a host's budgeted listener with a recorder whose reports may later be + * replayed. Each report goes to {@code first}, then {@code second}; an abort request does not + * skip delivery to the second listener. If the first listener throws, the exception propagates + * and the second is not called. * - * @param first one listener - * @param second the other + *

The wrapper keeps no state of its own. Its abort, serious-error, and code queries are true + * when either destination answers true; it is silent only when both are silent. It uses this + * interface's default buffering {@link #branch(AstNode)} behavior rather than independently + * branching each destination. * - * @return a listener that reports to both + * @param first the first destination + * @param second the second destination + * + * @return a listener combining delivery and the two destinations' current state */ - static ErrorListener tee(ErrorListener first, ErrorListener second) { + @NotNull + static ErrorListener tee(@NotNull ErrorListener first, @NotNull ErrorListener second) { requireNonNull(first, "first"); requireNonNull(second, "second"); return new ErrorListener() { @@ -239,131 +321,154 @@ public String toString() { } /** - * @param xs the structure a diagnostic is about + * Describe a diagnostic associated with an assembled structure. + * + *

This does not calculate a source span. A node-associated diagnostic branch can re-anchor + * the structure report to its node; otherwise an editor host must interpret the structure site. + * Prefer {@link #NOWHERE} when the caller has no location. * - * @return the site of a diagnostic about an XVM structure + * @param xs the related structure; null is accepted for legacy unpositioned reports + * + * @return a structure site referring to {@code xs} */ static Site at(XvmStructure xs) { return new Site.At(xs); } /** - * @param source the source being compiled - * @param lPosStart where the diagnostic starts - * @param lPosEnd where it ends + * Describe a diagnostic over a source span. + * + *

Positions use the encoding returned by {@link Source#getPosition()}, not byte offsets or + * LSP positions. This factory stores the supplied values without validating or copying the source. * - * @return the site of a diagnostic about a span of source + * @param source the source being diagnosed; prefer {@link #NOWHERE} if absent + * @param lPosStart the encoded start position, inclusive + * @param lPosEnd the encoded end position, exclusive + * + * @return a site referring to the supplied source span */ static Site in(Source source, long lPosStart, long lPosEnd) { return new Site.In(source, lPosStart, lPosEnd); } /** - * Where a diagnostic belongs. + * The location of a diagnostic, independent of its severity and message arguments. * - *

A listener receives the location as one of a small closed set of shapes, so a host that - * republishes diagnostics - an LSP server turning them into editor squiggles, say - can switch - * over them exhaustively instead of testing which of several nullable fields was populated. + *

Hosts can switch exhaustively over source spans, structure references, and unpositioned + * reports. These values describe locations; they do not resolve a structure to source text or + * detach the referenced compiler objects for concurrent access. */ sealed interface Site { /** - * A diagnostic about a span of source. + * A source span using the encoded positions returned by {@link Source#getPosition()}. + * + * @param source the referenced source; not copied + * @param lPosStart the inclusive encoded start position + * @param lPosEnd the exclusive encoded end position */ record In(Source source, long lPosStart, long lPosEnd) implements Site {} /** - * A diagnostic about an XVM structure, whose location is wherever that structure is. + * A diagnostic associated with a structure rather than an explicit source span. + * + * @param xs the related structure; null represents a legacy unpositioned report */ record At(XvmStructure xs) implements Site {} /** - * A diagnostic with no location. + * A diagnostic with no source span or structure, such as an operation-wide setup failure. */ record None() implements Site {} } /** - * The site of a diagnostic that is not about any particular place. + * The shared location for a diagnostic with no source span or structure. */ Site NOWHERE = new Site.None(); /** - * Branch this ErrorListener by creating a new one that will collect subsequent errors - * in the same manner as this one until it is {@link #merge() merged} or discarded in the - * (optional) context of the specified node. + * Create a temporary buffer for diagnostics from work whose reports may later be retained. + * + *

For speculative validation, pass the branch to the trial operation and call {@link #merge()} + * once if its reports should reach this listener. Otherwise discard the branch. Reports are + * buffered and deduplicated in an {@link ErrorList}; they are not forwarded during the trial. + * Use {@link #silence(Silence)} instead when the reports must never be retained. * - *

A branch buffers errors that may or may not end up being reported, so deciding to abandon - * the work is the parent's call and not the branch's: it is given {@link ErrorList#UNLIMITED} - * rather than a budget of its own. {@link ErrorList} overrides this to pass on the budget it - * was built with, and the two have to agree, or the compiler would do less work for a host - * that supplied its own listener than for one that used an ErrorList. + *

The default branch has an unlimited count budget, still aborts on its own FATAL, and observes + * this parent's abort request. {@link ErrorList#branch(AstNode)} instead copies its configured + * count budget. A supplied node re-anchors structure-site reports to that node's source span; + * explicit source-site reports retain their own location. * - * @param node (optional) the context ast node + * @param node the optional source context for structure-site diagnostics * - * @return the branched-out ErrorListener + * @return a new buffering child listener */ default ErrorListener branch(AstNode node) { return new ErrorList.BranchedErrorListener(this, ErrorList.UNLIMITED, node); } /** - * Merge all errors collected by this ErrorListener into the one it was branched out of. + * Publish a branch's retained diagnostics to its parent and return the parent. * - *

A listener that was never branched has nothing to merge and is already the sink the errors - * would be merged into, so merging it is a no-op. It is not an error: whether a listener in - * hand is a branch is not something its holder should have to know. + *

The default implementation is a no-op returning this listener, for a sink that has no parent. + * A {@link ErrorList.BranchedErrorListener} forwards its retained reports without clearing them; + * merging it again replays them again. A parent {@link ErrorList} can deduplicate that replay, + * but an arbitrary callback need not. Merge each accepted branch once. * - * @return the ErrorListener this one was {@link #branch branched out} of, or this one if it - * was not branched out of anything + * @return the parent for a branch, or this listener when there is nothing to merge */ default ErrorListener merge() { return this; } /** - * Why a stretch of work is not reporting its diagnostics. + * The caller's reason for deliberately discarding diagnostics. + * + *

The legacy {@code BLACKHOLE} name did not distinguish a type-fit probe, follow-on errors + * from incomplete analysis, and a caller that deliberately wanted no reports. Naming the reason + * makes that decision reviewable and searchable. These reasons have the same discard behavior; + * compiler semantics must not depend on which reason was selected. * - *

The three behave identically - nothing may branch on which one a listener holds - but they - * are different intentions, and saying which is meant is the point. Grepping for one of these - * is the list of the places that meant it. + *

Suppression does not buffer diagnostics for later publication. Use {@link #branch(AstNode)} + * when deciding whether reports should eventually be kept. Use a derived {@link #silence(Silence)} + * when the parent listener's stop policy must remain active. */ enum Silence { /** - * Speculative work whose failure is the answer, and whose failure must therefore not be - * audible. The compiler constantly asks "would this expression fit that type?", and the - * return value - not a diagnostic - is what the caller acts on. - * - *

Not for work whose failure the user should hear about if every alternative also fails; - * that is {@link ErrorListener#branch}. + * Speculative work whose return value answers the question, such as whether an expression fits + * a candidate type. Its failure is not a diagnostic for the user's program. Use a branch instead + * if the trial's reports may be needed when all alternatives fail. */ PROBE, /** - * The remainder of a computation already known to be incomplete. A result built from - * incomplete information produces diagnostics that describe the incompleteness rather - * than the user's code, and reporting them buries the one diagnostic that matters under - * a cascade of consequences. Unlike a probe, these are real diagnostics - a host may want - * them as related information for the diagnostic that does matter, which is why the - * listener they were taken from stays reachable. + * Follow-on diagnostics from information already known to be incomplete. Discard them to avoid + * burying the original failure in its consequences. Derive this silence from the operation's + * listener when its abort policy must remain active. Discarded reports cannot later be recovered. */ CASCADE, /** - * The caller has no sink to attach and wants none. Naming it is what keeps "I do not want - * these" from looking like "I did not think about these". + * An explicit decision not to receive diagnostics at this destination, for example a command-line + * launcher's external delegate when its console already reports them. This is the general-purpose + * replacement for an intentional {@code BLACKHOLE}; choose PROBE or CASCADE when those reasons apply. */ DISCARD } /** - * Obtain a listener that discards everything, for a caller with none of its own to derive - * from. + * Obtain a shared discard sink without a parent listener or inherited stop policy. + * + *

It retains no diagnostics, reports no serious errors or codes, never requests an abort, and + * is silent. All reasons behave identically. If the caller already has an operation listener, + * prefer {@link #silence(Silence)} to preserve that listener's abort or cancellation request. * - * @param why why the diagnostics are being discarded + * @param why the reason for discarding reports * - * @return a listener that discards what it is given + * @return the shared stateless sink for the requested reason */ - static ErrorListener silent(Silence why) { + @NotNull + static ErrorListener silent(@NotNull Silence why) { return switch (why) { case PROBE -> SILENT_PROBE; case CASCADE -> SILENT_CASCADE; @@ -372,121 +477,133 @@ static ErrorListener silent(Silence why) { } /** - * Obtain a listener that discards the rest of what this one would have been told. + * Create a discard wrapper for part of this listener's operation while retaining its stop policy. * - *

The result wraps this listener rather than being a shared constant, so the silence is a - * decision about one stretch of work instead of an anonymous gap, and - * {@link SilentErrorListener#suppressed} can still reach what was silenced. + *

The wrapper does not forward, retain, or replay reports, and its serious-error and code + * queries return false. Its abort query delegates to this listener, so suppressing reports cannot + * cancel a parent's stop request. {@link SilentErrorListener#suppressed()} exposes that parent, + * not the discarded diagnostics. The parent itself is not modified. * - *

Applying it twice is the same silence, so it is safe to call per use rather than having to - * hold the result. + *

Silencing an existing {@link SilentErrorListener} returns the same instance and keeps its + * original reason, even when a different reason is requested. * - * @param why why the diagnostics are being discarded + * @param why the reason for discarding reports * - * @return a listener that discards what the rest of this work has to say + * @return a wrapper retaining this listener's abort policy, or the existing silent wrapper */ - default ErrorListener silence(Silence why) { + @NotNull + default ErrorListener silence(@NotNull Silence why) { return new SilentErrorListener(this, why); } /** - * @return why this listener is silent, or null if it is not + * Describe why reports are discarded, when the listener exposes a named suppression policy. + * + *

This is explanatory metadata, not a compiler mode. Wrappers do not necessarily expose a + * reason even when {@link #isSilent()} is true; use that predicate to test silence. + * + * @return the suppression reason, or null when no named reason is exposed */ + @Nullable default Silence silenceReason() { return null; } /** - * @return true if the ErrorListener has decided to abort the process that reported the error + * Ask whether the current operation should stop at the caller's next safe checkpoint. + * + *

This query is independent of reporting and need not imply that an error was emitted. A + * listener may request a stop because of an error budget, a fatal diagnostic, or host policy. + * Conversely, serious errors may be collected without exhausting the budget. The caller owns + * the control flow; the default implementation never requests an abort. + * + * @return true if the caller should stop the operation */ default boolean isAbortDesired() { return false; } /** - * @return true iff an error has been logged with at least the Severity of Error + * Ask whether this listener has recorded an ERROR or FATAL diagnostic. + * + *

Use this to decide whether a compiler result is usable, not as a substitute for the abort + * policy. The default is false because a reporting-only implementation retains no state. + * + * @return true if a serious diagnostic has been recorded */ default boolean hasSeriousErrors() { return false; } /** - * @return true iff an error has been logged with the specified code + * Ask whether this listener has recorded the specified diagnostic code at any severity. + * + *

The default returns false. Stateful implementations such as {@link ErrorList} and + * {@link #collecting(Consumer)} record codes for this query. + * + * @param sCode the diagnostic code to look for + * + * @return true if the code has been recorded */ default boolean hasError(String sCode) { return false; } /** - * Whether this listener discards what it is told, for one of the reasons {@link Silence} - * names. {@link #silenceReason} says which. + * Ask whether reports sent to this listener are ultimately discarded. * - *

Not debugging-only, despite what this said for a long time: work that exists solely to - * produce a diagnostic is worth skipping when nobody is listening, and - * {@code MethodDeclarationStatement} and {@code PropertyDeclarationStatement} both ask before - * doing it. That is the one thing a caller may branch on - never on which silence it - * is, which is why the three behave identically. + *

Callers can skip work done solely to construct diagnostics when the answer is true. They + * must still perform semantic work and honor {@link #isAbortDesired()}; a silent wrapper may + * retain a parent's stop request. The default is false. A buffering branch can be silent when + * its eventual parent discards the reports, even though the branch records its local state. * - * @return true iff this listener discards what it is told + * @return true if the diagnostic destination is silent */ default boolean isSilent() { return false; } - // ----- inner class: BlackholeErrorListener --------------------------------------------------- - - /** - * A simple implementation of the ErrorListener that converts reported errors to ErrorInfo - * objects and routes them to a single sink method. - */ - class BlackholeErrorListener - implements ErrorListener { - @Override - public void log(ErrorInfo err) { - // Retained until callers migrate to named silence. - } - - @Override - public ErrorListener merge() { - return this; - } - - @Override - public boolean isSilent() { - return true; - } - - @Override - public String toString() { - return "(Blackhole)"; - } - } - // ----- inner class: SilentErrorListener ------------------------------------------------------ /** - * A listener that discards everything, for one of the reasons {@link Silence} names. + * A discard listener carrying a reason and, optionally, a parent stop policy. * - *

The three reasons behave identically and deliberately so - nothing may branch on which one - * it holds - but they are not the same intention, and a host that republishes diagnostics may - * want to treat them differently. Where the silence was derived from a real listener, - * {@link #suppressed} still reaches it. + *

Reports are neither forwarded nor retained. The inherited serious-error and code queries + * return false; the abort query delegates to the parent when present. {@link #suppressed()} + * returns that parent only, and cannot recover discarded messages. Prefer the + * {@link ErrorListener#silent(Silence)} and {@link ErrorListener#silence(Silence)} factories. */ class SilentErrorListener implements ErrorListener { /** - * @param errs the listener being silenced, or null where the caller had none - * @param why why the diagnostics are being discarded + * Construct a standalone discard listener with no parent abort policy. + * + * @param why the reason for discarding reports */ - public SilentErrorListener(ErrorListener errs, Silence why) { + public SilentErrorListener(@NotNull Silence why) { + this(null, why); + } + + /** + * Construct a discard listener with an optional parent abort policy. + * + * @param errs the parent whose abort query is retained, or null for no parent policy + * @param why the reason for discarding reports + */ + public SilentErrorListener(@Nullable ErrorListener errs, @NotNull Silence why) { f_errs = errs; f_why = requireNonNull(why, "why"); } /** - * @return the listener this silence was derived from, or null if it was not derived from - * one - a caller that never had a listener has nothing to suppress + * Return the listener whose reports this wrapper suppresses. + * + *

This is a reference to the original destination, not a buffer of discarded diagnostics. + * The wrapper does not mutate or silence that destination for other callers. + * + * @return the parent listener, or null for a standalone discard sink */ + @Nullable public ErrorListener suppressed() { return f_errs; } @@ -513,12 +630,15 @@ public boolean isSilent() { } @Override + @NotNull public Silence silenceReason() { return f_why; } @Override - public ErrorListener silence(Silence why) { + @NotNull + public ErrorListener silence(@NotNull Silence why) { + requireNonNull(why, "why"); // already silent for a stated reason; a second reason would only add a layer return this; } @@ -528,15 +648,23 @@ public String toString() { return f_errs == null ? "(" + f_why + ")" : "(" + f_why + " of " + f_errs + ")"; } + @Nullable private final ErrorListener f_errs; + @NotNull private final Silence f_why; } // ----- inner class: Runtime ErrorListener ---------------------------------------------------- /** - * A runtime sink that prints diagnostics and records an abort request for ERROR or FATAL. - * Reporting itself does not throw; callers query isAbortDesired() to decide whether to stop. + * A console sink that remembers an abort request after an ERROR or FATAL report. + * + *

It prints reports to standard output. Unlike the older implementation, reporting does not + * deliberately throw to stop the operation; callers must query {@link #isAbortDesired()} and + * choose their own failure handling. The abort flag remains set for the life of this instance. + * + *

This sink does not collect diagnostic codes or override the serious-error query. Use + * {@link ErrorList} or {@link ErrorListener#collecting(Consumer)} for a stateful compiler host. */ class RuntimeErrorListener implements ErrorListener { @@ -603,7 +731,7 @@ public ErrorInfo(Severity severity, String sCode, Object[] aoParam, * {@link Severity#ERROR}, or {@link Severity#FATAL} * @param sCode the error code that identifies the error message * @param aoParam the parameters for the error message; may be null - * @param xs + * @param xs the related structure, or null for an unpositioned diagnostic */ public ErrorInfo(Severity severity, String sCode, Object[] aoParam, XvmStructure xs) { m_severity = severity; @@ -614,11 +742,13 @@ public ErrorInfo(Severity severity, String sCode, Object[] aoParam, XvmStructure } /** - * The location of this diagnostic, as a closed set of shapes rather than as several - * fields of which some are null. A host that republishes diagnostics can switch over the - * result exhaustively. + * Describe this diagnostic's location as a source span, structure, or unpositioned report. + * + *

A source takes precedence when present. Otherwise a non-null structure yields a structure + * site; if neither is present, the result is {@link ErrorListener#NOWHERE}. No structure-to-source + * lookup or copying of compiler objects occurs here. * - * @return where this diagnostic belongs + * @return the diagnostic location */ public Site site() { return m_source != null ? new Site.In(m_source, m_lPosStart, m_lPosEnd) @@ -826,19 +956,28 @@ public String toString() { ResourceBundle RESOURCES = ResourceBundle.getBundle("errors"); /** - * Legacy silent sink and runtime diagnostic sink. + * Shared standalone sink for speculative probes; prefer {@link #silent(Silence)} at call sites. + */ + ErrorListener SILENT_PROBE = new SilentErrorListener(Silence.PROBE); + /** + * Shared standalone sink for cascade suppression. Use {@link #silence(Silence)} on an operation + * listener when its abort policy must be preserved. */ - ErrorListener SILENT_PROBE = new SilentErrorListener(null, Silence.PROBE); - ErrorListener SILENT_CASCADE = new SilentErrorListener(null, Silence.CASCADE); - ErrorListener SILENT_DISCARD = new SilentErrorListener(null, Silence.DISCARD); + ErrorListener SILENT_CASCADE = new SilentErrorListener(Silence.CASCADE); + /** + * Shared standalone sink for an explicitly unwanted diagnostic destination. + */ + ErrorListener SILENT_DISCARD = new SilentErrorListener(Silence.DISCARD); - ErrorListener BLACKHOLE = new BlackholeErrorListener(); - ErrorListener RUNTIME = new RuntimeErrorListener(); + /** + * Shared console fallback. Its abort flag is sticky across uses; it is not a fresh per-operation + * collector. Embedding hosts should supply their own listener. + */ + ErrorListener RUNTIME = new RuntimeErrorListener(); /** - * The default maximum number of serious errors an {@link ErrorList} collects before it asks - * the process reporting them to abort; for example, the number of errors the compiler collects - * for one module in one stage. + * Default serious-error budget for compilation, shared by tools and {@link ErrorList#ErrorList()}. + * Pass an explicit budget to {@link ErrorList#ErrorList(int)} when a different policy is needed. */ int DEFAULT_MAX_ERRORS = 512; diff --git a/javatools/src/main/java/org/xvm/asm/MethodStructure.java b/javatools/src/main/java/org/xvm/asm/MethodStructure.java index 41019bf95a4..3336acea3c9 100644 --- a/javatools/src/main/java/org/xvm/asm/MethodStructure.java +++ b/javatools/src/main/java/org/xvm/asm/MethodStructure.java @@ -59,6 +59,10 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.at; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.indentLines; import static org.xvm.util.Handy.parseDelimitedString; import static org.xvm.util.Handy.readIndex; @@ -757,8 +761,10 @@ public List collectUnresolvedTypeParameters(Set setResolved) { private static boolean checkConflict(TypeConstant typeResult, FormalConstant constFormal, boolean fParam, Map mapTypeParams) { if (typeResult != null) { + ErrorListener probe = silent(PROBE); + // downgrade enum value types to their base type (e.g. True -> Boolean) - TypeInfo info = typeResult.ensureTypeInfo(ErrorListener.BLACKHOLE); + TypeInfo info = typeResult.ensureTypeInfo(probe); if (info.getFormat() == Format.ENUMVALUE) { typeResult = info.getExtends(); } @@ -774,7 +780,7 @@ private static boolean checkConflict(TypeConstant typeResult, FormalConstant con // the new parameter type is wider or the old return type is narrower; use it instead } else { // the type are not compatible; use the common type (TODO: consider union?) - typeResult = Op.selectCommonType(typePrev, typeResult, ErrorListener.BLACKHOLE); + typeResult = Op.selectCommonType(typePrev, typeResult, probe); if (typeResult == null) { // different arguments cause the formal type to resolve into // incompatible types @@ -1681,8 +1687,7 @@ public ResolutionResult resolveName(String sName, Access access, ResolutionColle // REVIEW need a better error? AstNode node = collector.getNode(); if (node == null) { - collector.getErrorListener().log(Severity.ERROR, - Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS, null, this); + collector.getErrorListener().error(Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS, at(this)); } else { node.log(collector.getErrorListener(), Severity.ERROR, Compiler.UNSUPPORTED_DYNAMIC_TYPE_PARAMS); diff --git a/javatools/src/main/java/org/xvm/asm/XvmStructure.java b/javatools/src/main/java/org/xvm/asm/XvmStructure.java index a2156e87155..104c219db03 100755 --- a/javatools/src/main/java/org/xvm/asm/XvmStructure.java +++ b/javatools/src/main/java/org/xvm/asm/XvmStructure.java @@ -15,6 +15,8 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.at; + import static org.xvm.util.Handy.stream; /** @@ -469,7 +471,7 @@ public boolean validate(ErrorListener errs) { * @param aoParam the parameters of the error */ public void log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) { - ensureErrorListener(errs).log(sev, sCode, aoParam, this); + ensureErrorListener(errs).log(sev, sCode, at(this), aoParam); } /** diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java b/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java index ba27cbed5ee..99cb8e54fbb 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeCollector.java @@ -10,6 +10,9 @@ import org.xvm.asm.ErrorListener; import org.xvm.asm.Op; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A TypeCollector is used to collect a number of types, such as would occur from return statements * within a lambda, in order to infer a type from that collection of types. @@ -167,19 +170,21 @@ public TypeConstant inferSingle(TypeConstant typeRequired) { return null; } + ErrorListener probe = silent(PROBE); + TypeConstant typeCommon = inferFrom(listTypes.toArray(new TypeConstant[cTypes]), f_pool); if (typeRequired != null && typeRequired.containsFormalType(true)) { typeCommon = typeRequired.resolvePending(f_pool, typeCommon); } - typeCommon = Op.selectCommonType(typeCommon, typeRequired, ErrorListener.BLACKHOLE); + typeCommon = Op.selectCommonType(typeCommon, typeRequired, probe); if (typeRequired != null && (typeCommon == null || !typeCommon.isAssignableTo(typeRequired))) { // approach above didn't quite work; try to match with individual types one-by-one - TypeConstant typeAlt = Op.selectCommonType(typeRequired, listTypes.get(0), ErrorListener.BLACKHOLE); + TypeConstant typeAlt = Op.selectCommonType(typeRequired, listTypes.get(0), probe); for (int i = 1; i < cTypes; i++) { - typeAlt = Op.selectCommonType(typeAlt, listTypes.get(i), ErrorListener.BLACKHOLE); + typeAlt = Op.selectCommonType(typeAlt, listTypes.get(i), probe); } if (typeAlt != null) { @@ -342,8 +347,7 @@ public TypeConstant[] inferMulti(TypeConstant[] atypeRequired) { for (int iCol = 0; iCol < cWidth; ++iCol) { TypeConstant typeRequired = iCol < cReqTypes ? atypeRequired[iCol] : null; if (typeRequired != null) { - aResult[iCol] = Op.selectCommonType(aResult[iCol], typeRequired, - ErrorListener.BLACKHOLE); + aResult[iCol] = Op.selectCommonType(aResult[iCol], typeRequired, silent(PROBE)); } } } @@ -429,7 +433,7 @@ public static TypeConstant inferFrom(TypeConstant[] aTypes, ConstantPool pool) { } } - typeCommon = Op.selectCommonType(type, typeCommon, ErrorListener.BLACKHOLE); + typeCommon = Op.selectCommonType(type, typeCommon, silent(PROBE)); if (typeCommon == null) { // no obvious common type return null; diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java index fa2a018b02f..5431d8f0687 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java @@ -12,10 +12,10 @@ import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; import java.util.List; -import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.Objects; @@ -91,6 +91,10 @@ import static java.lang.constant.ConstantDescs.CD_boolean; import static java.lang.constant.ConstantDescs.CD_int; +import static org.xvm.asm.ErrorListener.Silence.CASCADE; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.javajit.Builder.CD_Class; import static org.xvm.javajit.Builder.CD_Ctx; import static org.xvm.javajit.Builder.CD_nType; @@ -2183,6 +2187,26 @@ public static boolean isComplete(TypeInfo info) { return rankTypeInfo(info) == 3; } + /** + * Choose the listener to report the rest of a TypeInfo build to. + * + * Once a contribution has turned out to be incomplete, what follows is reported against a + * type that is known to be missing pieces, so the diagnostics are consequences of what is + * absent rather than faults in the source. They are suppressed for the remainder of the + * build; the incompleteness itself is what the caller is told, by the return value. + * + * The choice is made at each use rather than by rebinding the listener, so that the errs + * parameter still means what its signature says all the way down the method. + * + * @param fIncomplete whether the build is already known to be incomplete + * @param errs the caller's listener + * + * @return the listener to report to + */ + private static ErrorListener cascade(boolean fIncomplete, ErrorListener errs) { + return fIncomplete ? errs.silence(CASCADE) : errs; + } + /** * Determine if the passed TypeInfo is up-to-date for this type. * @@ -2378,7 +2402,7 @@ private TypeInfo buildBaseTypeInfoImpl( // validate the type parameters against the properties checkTypeParameterProperties(mapTypeParams, mapVirtProps, - fComplete && !errs.hasSeriousErrors() ? errs : ErrorListener.BLACKHOLE); + fComplete && !errs.hasSeriousErrors() ? errs : errs.silence(CASCADE)); Annotation[] aAnnoMixin = fComplete ? collectMixinAnnotations(listProcess) @@ -2759,7 +2783,7 @@ private TypeInfo buildStructInfo(ErrorListener errs) { typeContrib = typeContrib.removeAccess(); typeContrib = pool.ensureAccessTypeConstant(typeContrib, Access.STRUCT); - TypeInfo infoContrib = typeContrib.ensureTypeInfoInternal(errs); + TypeInfo infoContrib = typeContrib.ensureTypeInfoInternal(cascade(fIncomplete, errs)); if (isComplete(infoContrib)) { for (Map.Entry entry : infoContrib.getProperties().entrySet()) { PropertyInfo prop = entry.getValue(); @@ -2771,14 +2795,13 @@ private TypeInfo buildStructInfo(ErrorListener errs) { } } else { fIncomplete = true; - errs = ErrorListener.BLACKHOLE; } break; }} } // add Object.toString() method - MethodInfo infoToString = pool.typeObject().ensureTypeInfo(errs). + MethodInfo infoToString = pool.typeObject().ensureTypeInfo(cascade(fIncomplete, errs)). getMethodBySignature(pool.sigToString()); mapMethods.putIfAbsent(infoToString.getIdentity(), infoToString); @@ -3567,18 +3590,14 @@ private boolean createCallChains( case Into: { // append to the call chain TypeConstant typeContrib = contrib.getTypeConstant(); // already resolved - TypeInfo infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(errs); + TypeInfo infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(cascade(fIncomplete, errs)); if (!isComplete(infoContrib)) { fIncomplete |= computeIncomplete(composition, typeContrib, infoContrib, setDepends); - if (fIncomplete) { - errs = ErrorListener.BLACKHOLE; - } } if (infoContrib != null) { - infoContrib.contributeChains(listmapClassChain, listmapDefaultChain, - listmapRootChain, composition); - layerOnTypeParams(mapTypeParams, typeContrib, infoContrib.getTypeParams(), errs); + infoContrib.contributeChains(listmapClassChain, listmapDefaultChain, listmapRootChain, composition); + layerOnTypeParams(mapTypeParams, typeContrib, infoContrib.getTypeParams(), cascade(fIncomplete, errs)); } break; } @@ -3737,18 +3756,16 @@ private boolean collectMemberInfo( int nBasePropRank = mapProps.size(); int nBaseMethRank = mapMethods.size(); - if (!collectSelfTypeParameters(struct, mapTypeParams, mapContribProps, nBasePropRank, errs)) { + if (!collectSelfTypeParameters(struct, mapTypeParams, mapContribProps, nBasePropRank, cascade(fIncomplete, errs))) { fIncomplete = true; - errs = ErrorListener.BLACKHOLE; } var listExplode = new ArrayList(); boolean fInterface = struct.getFormat() == Component.Format.INTERFACE; if (!collectChildInfo(constId, fInterface, struct, mapTypeParams, mapContribProps, mapContribMethods, mapContribChildren, listExplode, - mapVirtProps, nBasePropRank, nBaseMethRank, errs)) { + mapVirtProps, nBasePropRank, nBaseMethRank, cascade(fIncomplete, errs))) { fIncomplete = true; - errs = ErrorListener.BLACKHOLE; } // the order in which the properties are layered on and exploded is extremely @@ -3770,21 +3787,19 @@ private boolean collectMemberInfo( // layer on the property so its information is all correct before we have to // make any decisions about how to process the property prop = layerOnProp(constId, ContribSource.Self, null, mapProps, mapVirtProps, - typeContrib, idProp, prop, errs); + typeContrib, idProp, prop, cascade(fIncomplete, errs)); // now that the necessary data is in place, explode the property - if (!fNative && !explodeProperty(constId, struct, idProp, prop, - mapProps, mapVirtProps, mapMethods, mapVirtMethods, errs)) { + if (!fNative && !explodeProperty(constId, struct, idProp, prop, mapProps, + mapVirtProps, mapMethods, mapVirtMethods, cascade(fIncomplete, errs))) { fIncomplete = true; - errs = ErrorListener.BLACKHOLE; } } } else { - infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(errs); + infoContrib = typeContrib.adjustAccess(constId).ensureTypeInfoInternal(cascade(fIncomplete, errs)); if (!isComplete(infoContrib)) { if (computeIncomplete(composition, typeContrib, infoContrib, setDepends)) { fIncomplete = true; - errs = ErrorListener.BLACKHOLE; } if (infoContrib == null) { // even if the contribution has an incomplete info we can still proceed @@ -3838,7 +3853,7 @@ private boolean collectMemberInfo( // process properties layerOnProps(constId, contribSource, idDelegate, mapProps, mapVirtProps, - typeContrib, mapContribProps, errs); + typeContrib, mapContribProps, cascade(fIncomplete, errs)); // if there are any remaining declared-but-not-overridden properties originating from // an interface on a class once the "self" layer is applied, then those need to be @@ -3846,7 +3861,7 @@ private boolean collectMemberInfo( if (fSelf && !isInterface(constId, struct) && !struct.isExplicitlyAbstract()) { for (Entry entry : mapProps.entrySet()) { PropertyInfo infoOld = entry.getValue(); - PropertyInfo infoNew = infoOld.finishAdoption(fNative, errs); + PropertyInfo infoNew = infoOld.finishAdoption(fNative, cascade(fIncomplete, errs)); if (infoNew != infoOld) { entry.setValue(infoNew); if (infoNew.isVirtual()) { @@ -3863,7 +3878,7 @@ private boolean collectMemberInfo( if (!mapContribMethods.isEmpty()) { assert contrib.getComposition() != Composition.Annotation; layerOnMethods(constId, contribSource, idDelegate, mapMethods, mapVirtMethods, - typeContrib, mapContribMethods, errs); + typeContrib, mapContribMethods, cascade(fIncomplete, errs)); } // process children @@ -3875,7 +3890,7 @@ private boolean collectMemberInfo( if (infoPrev != null) { ChildInfo infoNew = infoPrev.layerOn(infoChild); if (infoNew == null) { - log(errs, Severity.ERROR, VE_CHILD_COLLISION, + log(cascade(fIncomplete, errs), Severity.ERROR, VE_CHILD_COLLISION, constId, sName, contrib.getTypeConstant(), @@ -3893,7 +3908,7 @@ private boolean collectMemberInfo( // to be processed by "finishAdoption" for (Entry entry : mapMethods.entrySet()) { MethodInfo infoOld = entry.getValue(); - MethodInfo infoNew = infoOld.finishAdoption(fNative, errs); + MethodInfo infoNew = infoOld.finishAdoption(fNative, cascade(fIncomplete, errs)); if (infoNew != infoOld) { entry.setValue(infoNew); if (infoNew.isVirtual()) { @@ -3975,7 +3990,6 @@ protected boolean explodeProperty( infoProp.getHead().markExploded(); } else { fComplete = false; - errs = ErrorListener.BLACKHOLE; } // layer on any annotations, if any @@ -3993,13 +4007,12 @@ protected boolean explodeProperty( } typeAnno = pool.ensureAccessTypeConstant(typeAnno, Access.PROTECTED); - TypeInfo infoAnno = typeAnno.ensureTypeInfoInternal(errs); + TypeInfo infoAnno = typeAnno.ensureTypeInfoInternal(cascade(!fComplete, errs)); if (infoAnno == null) { fComplete = false; - errs = ErrorListener.BLACKHOLE; } else { nestAndLayerOn(constId, idProp, mapProps, mapVirtProps, mapMethods, mapVirtMethods, - typeAnno, infoAnno, ContribSource.Annotation, errs); + typeAnno, infoAnno, ContribSource.Annotation, cascade(!fComplete, errs)); } } @@ -4054,11 +4067,11 @@ protected boolean explodeProperty( idGet.getValueString() + " at " + this.getValueString()); } infoGet = infoGet.layerOn(new MethodInfo(new MethodBody(idGet, - idGet.getSignature(), Implementation.Implicit), nRank), false, errs); + idGet.getSignature(), Implementation.Implicit), nRank), false, cascade(!fComplete, errs)); if (infoSet != null) { infoSet = infoSet.layerOn(new MethodInfo(new MethodBody(idSet, - idSet.getSignature(), Implementation.Implicit), nRank + 1), false, errs); + idSet.getSignature(), Implementation.Implicit), nRank+1), false, cascade(!fComplete, errs)); } } @@ -7661,7 +7674,7 @@ public TypeConstant getJitCCType() { public TypeConstant getJitICType() { // TODO CP: plug in the new logic TypeConstant type = getJitCCType(); - assert type.isNativeRef() || type.ensureTypeInfo().isNewable(false, ErrorListener.BLACKHOLE); + assert type.isNativeRef() || type.ensureTypeInfo().isNewable(false, silent(PROBE)); return type; } diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java b/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java index 7d84ae258be..c506f4069cf 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeInfoReal.java @@ -37,6 +37,9 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The fully realized "flattened" information about a type. */ @@ -803,7 +806,7 @@ public boolean isNewable(boolean fSingleton, ErrorListener errs) { return false; } TypeConstant typeParent = f_type.getParentType(); - if (!typeParent.ensureTypeInfo(errs).isNewable(false, ErrorListener.BLACKHOLE)) { + if (!typeParent.ensureTypeInfo(errs).isNewable(false, silent(PROBE))) { // the parent is abstract, so the virtual child "new-ability" will be checked // by concrete parent's subclasses return true; diff --git a/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java index 1ff5142b810..5cdcf2c5ae0 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java @@ -18,7 +18,8 @@ import org.xvm.compiler.CompilerException; import org.xvm.util.Hash; -import org.xvm.util.Severity; + +import static org.xvm.asm.ErrorListener.at; /** * Represent a type constant that will eventually be replaced with a real type constant. @@ -517,7 +518,7 @@ public boolean validate(ErrorListener errs) { return getResolvedType().validate(errs); } - errs.log(Severity.ERROR, Compiler.NAME_UNRESOLVABLE, new Object[]{getValueString()}, this); + errs.error(Compiler.NAME_UNRESOLVABLE, at(this), getValueString()); return true; } diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index cbeb25b45c0..962ab60481b 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -10,6 +10,11 @@ import org.xvm.compiler.ast.StageMgr; import org.xvm.compiler.ast.TypeCompositionStatement; +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + /** * A module compiler for Ecstasy code. * @@ -33,9 +38,7 @@ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs) { if (stmtModule.getCategory().getId() != Token.Id.MODULE) { throw new IllegalArgumentException("AST node for module is not a module statement"); } - if (errs == null) { - throw new IllegalArgumentException("ErrorListener required"); - } + requireNonNull(errs, "errs"); m_stmtModule = stmtModule; m_errs = errs; @@ -122,7 +125,7 @@ public FileStructure generateInitialFileStructure() { throw new CompilerException("failed to create module"); } m_structFile = m_stmtModule.getComponent().getFileStructure(); - m_structFile.setErrorListener(ErrorListener.BLACKHOLE); + m_structFile.setErrorListener(silent(DISCARD)); setStage(Stage.Registered); } diff --git a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java index dc4afbdf977..a40e8a8004f 100644 --- a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java +++ b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java @@ -39,7 +39,6 @@ import org.xvm.runtime.Utils; import org.xvm.util.ListMap; -import org.xvm.util.Severity; /** * The compiler of the "eval" script used by the debugger. @@ -134,8 +133,8 @@ public MethodStructure createLambda(TypeConstant typeReturn) { return lambda; } catch (Exception e) { if (!errs.hasSeriousErrors()) { - errs.log(Severity.FATAL, Parser.FATAL_ERROR, null, - f_source, f_source.getPosition(), f_source.getPosition()); + errs.fatal(Parser.FATAL_ERROR, ErrorListener.in( + f_source, f_source.getPosition(), f_source.getPosition())); } return discard(lambda); } diff --git a/javatools/src/main/java/org/xvm/compiler/Lexer.java b/javatools/src/main/java/org/xvm/compiler/Lexer.java index 6b0631df777..ab2737d15a2 100644 --- a/javatools/src/main/java/org/xvm/compiler/Lexer.java +++ b/javatools/src/main/java/org/xvm/compiler/Lexer.java @@ -19,6 +19,10 @@ import org.xvm.util.PackedInteger; import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.in; + import static org.xvm.util.Handy.hexitValue; import static org.xvm.util.Handy.isAsciiLetter; import static org.xvm.util.Handy.isDigit; @@ -39,16 +43,14 @@ public class Lexer * * @param source the source to parse */ - public Lexer(Source source, ErrorListener errorListener) { + public Lexer(Source source, ErrorListener errs) { if (source == null) { throw new IllegalArgumentException("Source required"); } - if (errorListener == null) { - throw new IllegalArgumentException("ErrorListener required"); - } + requireNonNull(errs, "errs"); m_source = source; - m_errorListener = errorListener; + m_errorListener = errs; eatWhitespace(); } @@ -407,7 +409,7 @@ protected Token eatBinaryLiteral(long lInitPos, boolean fMultiline) { } else if (ch == '_') { if (fFirst) { // it's an error to start with an underscore - log(Severity.ERROR, ILLEGAL_HEX, new Object[] {String.valueOf(ch)}, lInitPos); + log(Severity.ERROR, ILLEGAL_HEX, span(lInitPos), String.valueOf(ch)); } // ignore the _ (it's used for spacing within the literal) } else if (!fMultiline) { @@ -422,7 +424,7 @@ protected Token eatBinaryLiteral(long lInitPos, boolean fMultiline) { } } else { // error - log(Severity.ERROR, ILLEGAL_HEX, new Object[] {String.valueOf(ch)}, lInitPos); + log(Severity.ERROR, ILLEGAL_HEX, span(lInitPos), String.valueOf(ch)); source.rewind(); break; } @@ -851,7 +853,7 @@ protected Token eatToken() { default: if (!isIdentifierStart(chInit)) { - log(Severity.ERROR, ILLEGAL_CHAR, new Object[]{quotedChar(chInit)}, lInitPos); + log(Severity.ERROR, ILLEGAL_CHAR, span(lInitPos), quotedChar(chInit)); } // fall through case 'A':case 'B':case 'C':case 'D':case 'E':case 'F':case 'G': @@ -1045,9 +1047,8 @@ protected Token eatToken() { case LIT_FLOAT: if (!fFloat) { // we were expecting an integer - log(Severity.ERROR, ILLEGAL_NUMBER, new Object[] { - extractSource(lInitPos, tokNum.getEndPosition())}, - lInitPos); + log(Severity.ERROR, ILLEGAL_NUMBER, span(lInitPos), + extractSource(lInitPos, tokNum.getEndPosition())); return tokNum; } // fall through @@ -1836,8 +1837,8 @@ protected PackedInteger eatDigits(boolean fNeg, int radix, int[] digitCount) { protected Token eatTime(long lInitPos) { Token tokDate = eatDate(lInitPos, true); if (!(match('t') || expect('T'))) { - log(Severity.ERROR, BAD_TIME, new Object[] {tokDate.getValue()}, - tokDate.getStartPosition(), tokDate.getEndPosition()); + log(Severity.ERROR, BAD_TIME, + span(tokDate.getStartPosition(), tokDate.getEndPosition()), tokDate.getValue()); return tokDate; } @@ -1887,7 +1888,7 @@ protected Token eatTimeZone(long lInitPos) { if (nHour >= 0 && nMin >= 0) { if (nHour > 16 || nMin > 59) { - log(Severity.ERROR, BAD_TIMEZONE, new Object[] {sZone}, lStart, lEnd); + log(Severity.ERROR, BAD_TIMEZONE, span(lStart, lEnd), sZone); } } @@ -1907,7 +1908,7 @@ protected int eatDigits(int digitCount) { int n = 0; for (int i = 0; i < digitCount; ++i) { if (!isNextCharDigit(10)) { - log(Severity.ERROR, EXPECTED_DIGITS, new Object[] {digitCount, i}, lPosStart); + log(Severity.ERROR, EXPECTED_DIGITS, span(lPosStart), digitCount, i); return -n; } @@ -2136,7 +2137,7 @@ protected Token eatDate(long lInitPos, boolean fContinued) { String sDate = extractSource(lLitPos, lEndPos); if (nYear >= 0 && nMonth >= 0 && nDay >= 0) { if (nYear < 1582 || nMonth < 1 || nMonth > 12 || nDay < 1 || nDay > 31) { - log(Severity.ERROR, BAD_DATE, new Object[] {sDate}, lLitPos, lEndPos); + log(Severity.ERROR, BAD_DATE, span(lLitPos, lEndPos), sDate); } } @@ -2196,7 +2197,7 @@ protected Token eatTimeOfDay(long lInitPos, boolean fContinued) { String sTime = extractSource(lStart, lEnd); if (nHour >= 0 && nMin >= 0 && nSec >= 0) { if (nHour > 23 || nMin > 59 || nSec > 59 && !(nHour == 23 && nMin == 59 && nSec == 60)) { - log(Severity.ERROR, BAD_TIME_OF_DAY, new Object[] {sTime}, lStart, lEnd); + log(Severity.ERROR, BAD_TIME_OF_DAY, span(lStart, lEnd), sTime); } } @@ -2343,7 +2344,7 @@ protected Token eatDuration(long lInitPos) { String sDuration = extractSource(lStart, lEnd).toUpperCase(); if (fErr) { - log(Severity.ERROR, BAD_DURATION, new Object[] {sDuration}, lStart, lEnd); + log(Severity.ERROR, BAD_DURATION, span(lStart, lEnd), sDuration); } else { peekNotIdentifierOrNumber(); } @@ -2447,9 +2448,8 @@ protected boolean expect(char ch) { } if (chActual != ch) { - log(Severity.ERROR, EXPECTED_CHAR, - new Object[] {String.valueOf(ch), String.valueOf(chActual)}, - m_source.getPosition()); + log(Severity.ERROR, EXPECTED_CHAR, span(m_source.getPosition()), + String.valueOf(ch), String.valueOf(chActual)); return false; } @@ -2477,7 +2477,7 @@ protected void peekNotIdentifierOrNumber() { long lEnd = m_source.getPosition(); m_source.rewind(); if (ch >= '0' && ch <= '9' || isIdentifierPart(ch)) { - log(Severity.ERROR, UNEXPECTED_CHAR, new Object[] {ch}, m_source.getPosition(), lEnd); + log(Severity.ERROR, UNEXPECTED_CHAR, span(m_source.getPosition(), lEnd), ch); } } @@ -2507,7 +2507,7 @@ protected void log(Severity severity, String sCode, long lPosPrev, long lPosStar m_source.next(); } - log(severity, sCode, new Object[]{extractSource(lPosPrev, lPosEnd)}, lPosStart, lPosEnd); + log(severity, sCode, span(lPosStart, lPosEnd), extractSource(lPosPrev, lPosEnd)); } /** @@ -2521,12 +2521,42 @@ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosS * Log an error. */ protected void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) { - m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd); + log(severity, sCode, span(lPosStart, lPosEnd), aoParam); + } + + /** + * Log an error, with the message parameters as a trailing varargs. + * + * The location is a {@link ErrorListener.Site} rather than a pair of positions so that the + * parameters can be the tail; taking them as an array in the middle is what forces a call site + * to build one by hand. + */ + protected void log(Severity severity, String sCode, ErrorListener.Site site, Object... aoParam) { + m_errorListener.log(severity, sCode, site, aoParam); if (m_errorListener.isAbortDesired()) { throw new CompilerException("error list is full: " + m_errorListener); } } + /** + * @param lPosStart where the diagnostic starts + * + * @return the span from there to the current position in the script + */ + protected ErrorListener.Site span(long lPosStart) { + return in(m_source, lPosStart, m_source.getPosition()); + } + + /** + * @param lPosStart where the diagnostic starts + * @param lPosEnd where it ends + * + * @return that span of the script + */ + protected ErrorListener.Site span(long lPosStart, long lPosEnd) { + return in(m_source, lPosStart, lPosEnd); + } + // ----- helper methods ------------------------------------------------------------------------ /** diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index dfd45f89278..2516da74c9c 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -25,6 +25,12 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + /** * A recursive descent parser for Ecstasy source code. */ @@ -56,9 +62,7 @@ private Parser(Source source, ErrorListener errs, Lexer lexer) { throw new IllegalArgumentException("Source required"); } - if (errs == null) { - throw new IllegalArgumentException("ErrorListener required"); - } + requireNonNull(errs, "errs"); m_source = source; m_errorListener = errs; @@ -146,7 +150,7 @@ public Map parseImplicits() { public String parseModuleNameIgnoreEverythingElse() { ErrorListener errsPrev = m_errorListener; try { - m_errorListener = ErrorListener.BLACKHOLE; + m_errorListener = silent(DISCARD); Loop: while (!eof()) { if (match(Id.MODULE) != null) { @@ -5625,7 +5629,7 @@ protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd if (m_lookAhead != null) { m_lookAhead.log(severity, sCode, aoParam, lPosStart, lPosEnd); } else { - m_errorListener.log(severity, sCode, aoParam, m_source, lPosStart, lPosEnd); + m_errorListener.log(severity, sCode, in(m_source, lPosStart, lPosEnd), aoParam); if (m_errorListener.isAbortDesired()) { m_fAvoidRecovery = true; throw new CompilerException("error list is full: " + m_errorListener); diff --git a/javatools/src/main/java/org/xvm/compiler/Token.java b/javatools/src/main/java/org/xvm/compiler/Token.java index 5c07c20106c..8d9c18e152c 100644 --- a/javatools/src/main/java/org/xvm/compiler/Token.java +++ b/javatools/src/main/java/org/xvm/compiler/Token.java @@ -7,6 +7,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.in; + import static org.xvm.util.Handy.appendChar; import static org.xvm.util.Handy.appendString; @@ -322,8 +325,9 @@ public void log(ErrorListener errs, Source source, Severity severity, String sCo aoParam = new Object[] {source == null ? toString() : getString(source)}; } - errs.log(severity, sCode, aoParam, source, - source == null ? 0L : getStartPosition(), source == null ? 0L : getEndPosition()); + errs.log(severity, sCode, source == null + ? NOWHERE + : in(source, getStartPosition(), getEndPosition()), aoParam); } // ----- Object methods ------------------------------------------------------------------------ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java index 43ef4064b15..82fafd16aa9 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java @@ -40,6 +40,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A type annotation is used for type annotations with an optional argument list. */ @@ -300,7 +303,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { if (exprNew instanceof NameExpression exprName && exprName.getMeaning() == NameExpression.Meaning.Method) { MethodConstant idMethod = (MethodConstant) - exprName.resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE); + exprName.resolveRawArgument(ctx, false, silent(PROBE)); method = (MethodStructure) idMethod.getComponent(); } else if (exprNew instanceof LambdaExpression exprLambda) { method = exprLambda.getLambda(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java b/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java index 8fb3762c434..f8eaabcbf6e 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AnonInnerClass.java @@ -21,6 +21,8 @@ import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + /** * The AnonInnerClass represents the suggested shape for an anonymous inner class. */ @@ -33,7 +35,7 @@ public class AnonInnerClass { */ public AnonInnerClass(TypeExpression expr, ErrorListener errs) { assert expr != null; - assert errs != null; + requireNonNull(errs, "errs"); m_exprType = expr; f_errs = errs; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java index 97a9d77a931..7f1e77c4586 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java @@ -19,10 +19,10 @@ import org.xvm.asm.constants.ArrayConstant; import org.xvm.asm.constants.IntConstant; -import org.xvm.asm.constants.RangeConstant; import org.xvm.asm.constants.MapConstant; import org.xvm.asm.constants.MethodConstant; import org.xvm.asm.constants.MethodInfo; +import org.xvm.asm.constants.RangeConstant; import org.xvm.asm.constants.SignatureConstant; import org.xvm.asm.constants.TypeConstant; import org.xvm.asm.constants.TypeInfo; @@ -38,6 +38,9 @@ import org.xvm.util.PackedInteger; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * An array access expression is an expression followed by an array index expression. * @@ -191,6 +194,8 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti } } + ErrorListener probe = silent(PROBE); + // at this point, either the "array" expression has no idea what type it will result in, or // its best guess doesn't have an array operator that results in the required type; "probe" // for some likely types that the array access operators are known to exist on @@ -206,7 +211,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti // (which must be determinable at compile time in order to know which tuple elements // are being accessed) if (typeTarget != null && typeTarget.isTuple() - || expr.testFit(ctx, pool.typeTuple(), fExhaustive, null) == TypeFit.Fit) { + || expr.testFit(ctx, pool.typeTuple(), fExhaustive, probe) == TypeFit.Fit) { TypeConstant typeTest = determineTupleTestType(typeRequired); return typeTest == null // the only thing that we can say for sure at this point is that the return @@ -227,7 +232,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti // this:type) and index could be a Range // array[index] - if (!isSliceOp() && exprIndex.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit()) { + if (!isSliceOp() && exprIndex.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit()) { return exprTarget.testFit(ctx, pool.ensureParameterizedTypeConstant( pool.typeList(), typeRequired), fExhaustive, errs); } @@ -235,7 +240,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti // array[index..index] or array[index..index) // REVIEW what if it is a Range or Range ??? if (exprTarget.testFit(ctx, typeRequired, fExhaustive, errs) == TypeFit.Fit - && exprIndex.testFit(ctx, pool.ensureRangeType(pool.typeInt64()), fExhaustive, null).isFit()) { + && exprIndex.testFit(ctx, pool.ensureRangeType(pool.typeInt64()), fExhaustive, probe).isFit()) { return TypeFit.Fit; } } else { // not a List, but might still be UniformIndexed and/or Sliceable @@ -261,7 +266,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti // Index -> Element - probably does not matter what indexes are, since // Sliceable ops return this:type if (testType(ctx, exprTarget, typeTarget, pool.typeSliceable()) - && exprIndex.testFit(ctx, pool.typeRange(), fExhaustive, null).isFit()) { + && exprIndex.testFit(ctx, pool.typeRange(), fExhaustive, probe).isFit()) { fit = exprTarget.testFit(ctx, typeRequired, fExhaustive, errs); if (fit.isFit()) { return fit; @@ -291,16 +296,17 @@ && testType(ctx, exprTarget, typeTarget, pool.typeMap())) { Expression exprRow = indexes.get(1); // matrix[index,index] - if (exprCol.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit() && - exprRow.testFit(ctx, pool.typeInt64(), fExhaustive, null).isFit()) { + if (exprCol.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit() && + exprRow.testFit(ctx, pool.typeInt64(), fExhaustive, probe).isFit()) { return expr.testFit(ctx, pool.ensureParameterizedTypeConstant( pool.typeMatrix(), typeRequired), fExhaustive, errs); } // matrix[index..index,index..index] TypeConstant typeInterval = pool.ensureRangeType(pool.typeInt64()); - if (typeRequired.isA(pool.typeMatrix()) && exprCol.testFit(ctx, typeInterval, fExhaustive, null).isFit() - && exprRow.testFit(ctx, typeInterval, fExhaustive, null).isFit()) { + if (typeRequired.isA(pool.typeMatrix()) + && exprCol.testFit(ctx, typeInterval, fExhaustive, probe).isFit() + && exprRow.testFit(ctx, typeInterval, fExhaustive, probe).isFit()) { // REVIEW same issue as array above TypeConstant typeElement = typeRequired.resolveGenericType("Element"); return typeElement == null @@ -327,14 +333,16 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe int cIndexes = indexes.size(); Expression[] aexprIndexes = indexes.toArray(new Expression[cIndexes]); + ErrorListener probe = silent(PROBE); + // test if the access is against a tuple expression if (typeArray != null && typeArray.isTuple() - || typeArray == null && exprArray.testFit(ctx, pool.typeTuple(), false, null) == TypeFit.Fit) { + || typeArray == null && exprArray.testFit(ctx, pool.typeTuple(), false, probe) == TypeFit.Fit) { if (typeArray == null) { typeArrayReq = pool.typeTuple(); } else { TypeConstant typeTupleTest = determineTupleTestType(typeRequired); - if (exprArray.testFit(ctx, typeTupleTest, false, null).isFit()) { + if (exprArray.testFit(ctx, typeTupleTest, false, probe).isFit()) { typeArrayReq = typeTupleTest; } } @@ -344,11 +352,11 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe if (typeRequired != null) { // array[index] TypeConstant typeElement = null; - if (!isSliceOp() && aexprIndexes[0].testFit(ctx, pool.typeInt64(), false, null).isFit()) { + if (!isSliceOp() && aexprIndexes[0].testFit(ctx, pool.typeInt64(), false, probe).isFit()) { typeElement = typeRequired; // array[index..index] or array[index..index) } else if (typeRequired.isA(pool.typeList()) && aexprIndexes[0].testFit(ctx, - pool.ensureRangeType(pool.typeInt64()), false, null).isFit()) { + pool.ensureRangeType(pool.typeInt64()), false, probe).isFit()) { // REVIEW keep this in sync with testFit() typeElement = typeRequired.resolveGenericType("Element"); } @@ -356,7 +364,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe if (typeElement != null) { TypeConstant typeArrayTest = pool.ensureParameterizedTypeConstant(typeArrayReq, typeElement); - if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) { + if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) { typeArrayReq = typeArrayTest; } } @@ -369,7 +377,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe TypeConstant typeIndex = typeArray == null ? null : typeArray.resolveGenericType("Index"); - if (typeIndex == null || !aexprIndexes[0].testFit(ctx, typeIndex, false, null).isFit()) { + if (typeIndex == null || !aexprIndexes[0].testFit(ctx, typeIndex, false, probe).isFit()) { typeIndex = aexprIndexes[0].getImplicitType(ctx); typeIndex = determineIndexType(ctx, exprArray, typeArray, aexprIndexes, typeIndex); } @@ -379,7 +387,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe if (typeRequired != null) { TypeConstant typeTest = pool.ensureParameterizedTypeConstant( pool.typeIndexed(), typeIndex, typeRequired); - if (exprArray.testFit(ctx, typeTest, false, null).isFit()) { + if (exprArray.testFit(ctx, typeTest, false, probe).isFit()) { // we figured out what to ask for, including the index and element types typeElement = typeRequired; typeArrayReq = typeTest; @@ -390,7 +398,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // we can only figure out the index type TypeConstant typeArrayTest = pool.ensureParameterizedTypeConstant(typeArrayReq, typeIndex); - if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) { + if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) { typeArrayReq = typeArrayTest; } } @@ -403,15 +411,15 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe Expression exprRow = aexprIndexes[1]; // matrix[index,index] - TypeConstant typeElement = null; - if (exprCol.testFit(ctx, pool.typeInt64(), false, null).isFit() && - exprRow.testFit(ctx, pool.typeInt64(), false, null).isFit()) { + TypeConstant typeElement = null; + if (exprCol.testFit(ctx, pool.typeInt64(), false, probe).isFit() && + exprRow.testFit(ctx, pool.typeInt64(), false, probe).isFit()) { typeElement = typeRequired; // array[index..index] } else if (typeRequired.isA(pool.typeInterval())) { TypeConstant typeIntInterval = pool.ensureRangeType(pool.typeInt64()); - if (exprCol.testFit(ctx, typeIntInterval, false, null).isFit() && - exprRow.testFit(ctx, typeIntInterval, false, null).isFit()) { + if (exprCol.testFit(ctx, typeIntInterval, false, probe).isFit() && + exprRow.testFit(ctx, typeIntInterval, false, probe).isFit()) { // REVIEW keep this in sync with testFit() typeElement = typeRequired.resolveGenericType("Element"); } @@ -420,7 +428,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe if (typeElement != null) { TypeConstant typeArrayTest = pool.ensureParameterizedTypeConstant(typeArrayReq, typeRequired); - if (exprArray.testFit(ctx, typeArrayTest, false, null).isFit()) { + if (exprArray.testFit(ctx, typeArrayTest, false, probe).isFit()) { typeArrayReq = typeArrayTest; } } @@ -493,8 +501,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe System.arraycopy(aexprIndexes, 0, aExprSet, 0, cIndexes); // note: cannot fill in the expression that represents the value being set because it // hasn't yet been validated (for example, if our parent is an AssignmentStatement) - m_idSet = findOpMethod(ctx, typeArray, "setElement", "[]=", aExprSet, null, - ErrorListener.BLACKHOLE); + m_idSet = findOpMethod(ctx, typeArray, "setElement", "[]=", aExprSet, null, probe); } // tuple is different from other container types (like array) in that every field in the @@ -669,6 +676,8 @@ private MethodConstant findArrayAccessor( Set setMatch = new HashSet<>(); Map mapMethods = new HashMap<>(); + ErrorListener probe = silent(PROBE); + Set setAll = findPotentialOps(infoTarget, cArgs); NextOp: for (MethodConstant idMethod : setAll) { SignatureConstant sig = idMethod.getSignature(); @@ -701,7 +710,7 @@ private MethodConstant findArrayAccessor( TypeConstant typeArg = atypeArgs[i]; if (typeArg == null || !isAssignable(ctx, typeArg, typeParam)) { Expression exprArg = aexprArgs[i]; - if (!exprArg.testFit(ctx, typeParam, false, null).isFit()) { + if (!exprArg.testFit(ctx, typeParam, false, probe).isFit()) { continue NextOp; } } @@ -722,7 +731,7 @@ private MethodConstant findArrayAccessor( return switch (setMatch.size()) { case 0 -> null; case 1 -> setMatch.iterator().next(); - default -> chooseBest(setMatch, typeTarget, mapMethods, ErrorListener.BLACKHOLE); + default -> chooseBest(setMatch, typeTarget, mapMethods, probe); }; } @@ -860,7 +869,7 @@ private boolean indexesFit(Context ctx, MethodConstant method) { int cTypes = atypes.length; if (cTypes == indexes.size()) { for (int i = 0; i < cTypes; ++i) { - if (!indexes.get(i).testFit(ctx, atypes[i], false, null).isFit()) { + if (!indexes.get(i).testFit(ctx, atypes[i], false, silent(PROBE)).isFit()) { return false; } } @@ -1111,7 +1120,7 @@ TypeConstant estimateIndexType(Context ctx, TypeConstant typeTarget, String sInd TypeConstant typeIndex = typeTarget == null ? null : typeTarget.resolveGenericType(sIndexParam); - if (typeIndex == null || !exprIndex.testFit(ctx, typeIndex, false, null).isFit()) { + if (typeIndex == null || !exprIndex.testFit(ctx, typeIndex, false, silent(PROBE)).isFit()) { typeIndex = exprIndex.getImplicitType(ctx); } return typeIndex; @@ -1131,11 +1140,12 @@ TypeConstant estimateIndexType(Context ctx, TypeConstant typeTarget, String sInd */ private TypeConstant determineIndexType(Context ctx, Expression exprArray, TypeConstant typeArray, Expression[] aexprIndexes, TypeConstant typeIndex) { + ErrorListener probe = silent(PROBE); + ConstantPool pool = pool(); if (typeIndex == null) { if (typeArray != null) { - MethodConstant id = findOpMethod(ctx, typeArray, "getElement", "[]", aexprIndexes, - null, ErrorListener.BLACKHOLE); + MethodConstant id = findOpMethod(ctx, typeArray, "getElement", "[]", aexprIndexes, null, probe); if (id != null && id.getRawReturns().length >= 1) { typeIndex = id.getRawReturns()[0]; if (!pool.typeObject().isA(typeIndex)) { @@ -1148,7 +1158,7 @@ private TypeConstant determineIndexType(Context ctx, Expression exprArray, } if (exprArray.testFit(ctx, - pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, null).isFit()) { + pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, probe).isFit()) { return typeIndex; } @@ -1156,7 +1166,7 @@ private TypeConstant determineIndexType(Context ctx, Expression exprArray, for (MethodInfo info : setInfos) { typeIndex = info.getSignature().getRawReturns()[0]; if (exprArray.testFit(ctx, - pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, null).isFit()) { + pool.ensureParameterizedTypeConstant(pool.typeIndexed(), typeIndex), false, probe).isFit()) { return typeIndex; } } @@ -1179,7 +1189,7 @@ private boolean testType(Context ctx, Expression expr, TypeConstant typeExpr, Ty return true; } - return expr.testFit(ctx, typeTest, false, null) == TypeFit.Fit; + return expr.testFit(ctx, typeTest, false, silent(PROBE)) == TypeFit.Fit; } /** diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java index 1c0afec533a..50eb62a2c90 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AsExpression.java @@ -13,6 +13,9 @@ import org.xvm.compiler.Token; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Expression for "expression as type". */ @@ -35,7 +38,7 @@ public long getEndPosition() { @Override public TypeConstant getImplicitType(Context ctx) { - return ((TypeExpression) expr2).ensureTypeConstant(ctx, null); + return ((TypeExpression) expr2).ensureTypeConstant(ctx, silent(PROBE)); } @Override @@ -59,7 +62,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe TypeConstant type = exprType.ensureTypeConstant(ctx, errs).resolveAutoNarrowingBase(); - if (!exprType.isDynamic() && expr1.testFit(ctx, type, false, null).isFit()) { + if (!exprType.isDynamic() && expr1.testFit(ctx, type, false, silent(PROBE)).isFit()) { typeRequest = type; m_fCastRequired = false; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java index 8ebc52bc5b8..a87a50065b5 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AssignmentStatement.java @@ -34,6 +34,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * An assignment statement specifies an l-value, an assignment operator, and an r-value. * @@ -386,6 +389,8 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { return null; } + ErrorListener probe = silent(PROBE); + // regardless of whether the LValue is a statement or expression, all L-Values must be able // to provide an expression as a representative form Expression exprLeft = nodeLeft.getLValueExpression(); @@ -407,12 +412,12 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { // contributions Context ctxInfer = ctxRValue.enterInferring(atypeLeft[0]); - TypeFit fit = rvalue.testFitMulti(ctxInfer, atypeTest, false, null); + TypeFit fit = rvalue.testFitMulti(ctxInfer, atypeTest, false, probe); if (!fit.isFit() && cLeft > 1) { Expression exprUnpack = new UnpackExpression(rvalue, null); - fit = exprUnpack.testFitMulti(ctxInfer, atypeTest, false, null); + fit = exprUnpack.testFitMulti(ctxInfer, atypeTest, false, probe); if (fit.isFit()) { rvalue = exprUnpack; } @@ -456,7 +461,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { if (exprLeft instanceof NameExpression exprName && exprName.isDynamicVar()) { // test for a future assignment first TypeConstant typeFuture = pool.ensureFuture(typeLeft); - if (rvalue.testFit(ctxRValue, typeFuture, false, null).isFit()) { + if (rvalue.testFit(ctxRValue, typeFuture, false, probe).isFit()) { typeLeft = typeFuture; } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index 2b22036519c..687750b7e8f 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -55,6 +55,11 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.indentLines; /** @@ -640,11 +645,9 @@ public Access getDefaultAccess() { */ public void log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) { Source source = getSource(); - if (errs != null) { - errs.log(severity, sCode, aoParam, source, - source == null ? 0L : getStartPosition(), - source == null ? 0L : getEndPosition()); - } + errs.log(severity, sCode, source == null + ? NOWHERE + : in(source, getStartPosition(), getEndPosition()), aoParam); } // ----- compile phases ------------------------------------------------------------------------ @@ -1214,7 +1217,7 @@ private void collectMatchingMethods( lit.getLiteral().getValueText()); } else { if (exprArg instanceof NameExpression exprName) { - typeExpr = exprName.getImplicitType(ctx, typeParam, ErrorListener.BLACKHOLE); + typeExpr = exprName.getImplicitType(ctx, typeParam, silent(PROBE)); } log(errsTemp, Severity.ERROR, Compiler.INCOMPATIBLE_PARAMETER_TYPE, @@ -1324,7 +1327,7 @@ protected TypeConstant[] transformTypeArguments(Context ctx, protected TypeConstant transformType(Context ctx, NameExpression exprName) { ConstantPool pool = pool(); TypeConstant type = pool.typeType(); - Argument arg = exprName.resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE); + Argument arg = exprName.resolveRawArgument(ctx, false, silent(PROBE)); if (arg instanceof Register reg) { PropertyConstant idProp = type.ensureTypeInfo().findProperty("DataType").getIdentity(); FormalConstant idFormal = pool.ensureDynamicFormal( diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java b/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java index 451e1cde608..80164cb0a36 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/CaseManager.java @@ -15,8 +15,8 @@ import org.xvm.asm.constants.ArrayConstant; import org.xvm.asm.constants.IntConstant; -import org.xvm.asm.constants.RangeConstant; import org.xvm.asm.constants.MatchAnyConstant; +import org.xvm.asm.constants.RangeConstant; import org.xvm.asm.constants.TypeConstant; import org.xvm.asm.constants.ValueConstant; @@ -39,6 +39,9 @@ import org.xvm.util.PackedInteger; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The CaseManager is shared compilation logic used by both the "switch" statement and expression. */ @@ -416,6 +419,8 @@ public boolean validateCase(Context ctx, CaseStatement stmtCase, ErrorListener e // allow case values to infer based on the expected type from the switch condition ctx = ctx.enterInferring(m_typeCase); + ErrorListener probe = silent(PROBE); + // validate each separate value in the case label ConstantPool pool = pool(); boolean fIfSwitch = usesIfLadder(); @@ -435,9 +440,9 @@ public boolean validateCase(Context ctx, CaseStatement stmtCase, ErrorListener e Expression exprField = listFields.get(i); if (exprField instanceof IgnoredNameExpression) { lIgnore |= 1L << i; - } else if (!exprField.testFit(ctx, m_atypeCond[i], false, null).isFit()) { + } else if (!exprField.testFit(ctx, m_atypeCond[i], false, probe).isFit()) { TypeConstant typeRange = pool.ensureRangeType(m_atypeCond[i]); - if (exprField.testFit(ctx, typeRange, false, null).isFit()) { + if (exprField.testFit(ctx, typeRange, false, probe).isFit()) { lRange |= 1L << i; if (atypeAlt == null) { @@ -450,9 +455,9 @@ public boolean validateCase(Context ctx, CaseStatement stmtCase, ErrorListener e } else if (getConditionCount() == 1) { if (exprCase instanceof IgnoredNameExpression) { lIgnore = 1; - } else if (!exprCase.testFit(ctx, m_typeCase, false, null).isFit()) { + } else if (!exprCase.testFit(ctx, m_typeCase, false, probe).isFit()) { TypeConstant typeRange = pool.ensureRangeType(m_typeCase); - if (exprCase.testFit(ctx, typeRange, false, null).isFit()) { + if (exprCase.testFit(ctx, typeRange, false, probe).isFit()) { lRange = 1; typeMatch = typeRange; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java index 7e66cacb6c1..79818358864 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/CmpChainExpression.java @@ -43,6 +43,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Comparison over a chain of expressions. * @@ -453,7 +456,7 @@ protected boolean testCommonType(Context ctx, TypeConstant type) { ctx = ctx.enterInferring(type); for (Expression expr : expressions) { - if (!expr.testFit(ctx, type, false, null).isFit()) { + if (!expr.testFit(ctx, type, false, silent(PROBE)).isFit()) { return false; } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CmpExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/CmpExpression.java index 706949b7160..0c1115e27b8 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CmpExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/CmpExpression.java @@ -44,6 +44,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Comparison binary expression. * @@ -393,7 +396,7 @@ protected static TypeConstant chooseCommonType(ConstantPool pool, boolean fEqua return pool.typeRef(); } - TypeConstant typeCommon = Op.selectCommonType(type1, type2, ErrorListener.BLACKHOLE); + TypeConstant typeCommon = Op.selectCommonType(type1, type2, silent(PROBE)); if (type1 == null || type2 == null) { return typeCommon; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Context.java b/javatools/src/main/java/org/xvm/compiler/ast/Context.java index 7ec803ec735..140bcee6d79 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Context.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Context.java @@ -11,13 +11,14 @@ import java.util.TreeSet; import org.xvm.asm.Argument; +import org.xvm.asm.Assignment; import org.xvm.asm.ClassStructure; import org.xvm.asm.Component; import org.xvm.asm.Component.SimpleCollector; import org.xvm.asm.ComponentResolver.ResolutionResult; import org.xvm.asm.Constant; -import org.xvm.asm.Constants.Access; import org.xvm.asm.ConstantPool; +import org.xvm.asm.Constants.Access; import org.xvm.asm.ErrorListener; import org.xvm.asm.GenericTypeResolver; import org.xvm.asm.MethodStructure; @@ -25,7 +26,6 @@ import org.xvm.asm.Parameter; import org.xvm.asm.PropertyStructure; import org.xvm.asm.Register; -import org.xvm.asm.Assignment; import org.xvm.asm.constants.FormalConstant; import org.xvm.asm.constants.IdentityConstant; @@ -41,6 +41,10 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + /** * Compiler context for compiling a method body. */ @@ -941,7 +945,7 @@ protected void markVarRead(boolean fNested, String sName, Token tokName, boolean } } } else { - if (tokName != null && errs != null) { + if (tokName != null) { if (isReservedName(sName)) { MethodStructure method = getMethod(); IdentityConstant idCtx = method == null @@ -1028,9 +1032,7 @@ public final void markVarWrite(Token tokName, boolean fCond, ErrorListener errs) public boolean requireThis(long lPos, ErrorListener errs) { Context ctxOuter = getOuterContext(); if (ctxOuter == null) { - if (errs != null) { - errs.log(Severity.ERROR, Compiler.NO_THIS, new Object[0], getSource(), lPos, lPos); - } + errs.error(Compiler.NO_THIS, in(getSource(), lPos, lPos)); return false; } return ctxOuter.requireThis(lPos, errs); @@ -1054,7 +1056,7 @@ protected void markVarWrite(String sName, Token tokName, boolean fCond, ErrorLis if (isVarWritable(sName)) { setVarAssignment(sName, getVarAssignment(sName).applyAssignment()); - } else if (tokName != null && errs != null) { + } else if (tokName != null) { tokName.log(errs, getSource(), Severity.ERROR, Compiler.VAR_ASSIGNMENT_ILLEGAL, sName); } else { throw new IllegalStateException("illegal var write: name=" + sName); @@ -1124,7 +1126,7 @@ protected Map ensureDefiniteAssignments() { * @return the Argument representing the meaning of the name, or null */ public final Argument resolveName(String sName) { - return resolveName(sName, null, ErrorListener.BLACKHOLE); + return resolveName(sName, null, silent(PROBE)); } /** @@ -2490,7 +2492,7 @@ public void useFormalType(TypeConstant type, ErrorListener errs) { case Property: case TypeParameter: { String sName = constFormal.getName(); - Argument arg = resolveName(sName, null, ErrorListener.BLACKHOLE); + Argument arg = resolveName(sName, null, silent(PROBE)); if (arg != null) { ensureFormalMap().putIfAbsent(sName, arg); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ElseExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ElseExpression.java index b600ec83580..7122db9ae91 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ElseExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ElseExpression.java @@ -20,6 +20,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The trailing "else" expression for any short-circuited expressions that precede it: * @@ -97,7 +100,7 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er TypeConstant[] atype2Req = selectCommonTypes(atype1, new TypeConstant[atype1.length]); if (atypeRequired != null && atypeRequired.length > 0 && - (atype2Req == null || !expr2.testFitMulti(ctx, atype2Req, false, null).isFit())) { + (atype2Req == null || !expr2.testFitMulti(ctx, atype2Req, false, silent(PROBE)).isFit())) { atype2Req = atypeRequired; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ElvisExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ElvisExpression.java index 1a0370dad44..fcdf7e119eb 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ElvisExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ElvisExpression.java @@ -22,6 +22,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The "Elvis" expression, which is used to optionally substitute the value of the second expression * iff the value of the first expression is null. @@ -70,7 +73,7 @@ public TypeConstant getImplicitType(Context ctx) { return null; } - TypeConstant typeResult = Op.selectCommonType(type1, type2, ErrorListener.BLACKHOLE); + TypeConstant typeResult = Op.selectCommonType(type1, type2, silent(PROBE)); // hey, wouldn't it be nice if we could just do this? // @@ -84,7 +87,7 @@ public TypeConstant getImplicitType(Context ctx) { @Override public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhaustive, ErrorListener errs) { // first try the less likely (and more complicated) "conditional" use case - ErrorListener errsTemp = errs == null ? ErrorListener.BLACKHOLE : errs.branch(this); + ErrorListener errsTemp = errs.branch(this); TypeConstant[] atypeCond = new TypeConstant[]{pool().typeBoolean(), typeRequired}; TypeFit fit = expr1.testFitMulti(ctx, atypeCond, fExhaustive, errsTemp); if (fit.isFit()) { @@ -127,7 +130,9 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // ctx = ctx.enterIf(); - if (expr1.testFitMulti(ctx, atypeCond, true, ErrorListener.BLACKHOLE).isFit()) { + ErrorListener probe = silent(PROBE); + + if (expr1.testFitMulti(ctx, atypeCond, true, probe).isFit()) { m_fCond = fCond = true; if (typeRequired != null) { @@ -150,7 +155,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe ? null : Op.selectCommonType(type1.removeNullable(), null, errs); if (typeRequired == null) { - if (type2Req != null && !expr2.testFit(ctx, type2Req, true, null).isFit()) { + if (type2Req != null && !expr2.testFit(ctx, type2Req, true, probe).isFit()) { // there are no requirements from outside and the second expression is not going to // validate against the first expression type. Compute the narrowest type that expr2 // has a chance of validating - a union of type1 and the implicit type for expr2 @@ -160,7 +165,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } } } else { - if (type2Req == null || !expr2.testFit(ctx, type2Req, false, null).isFit()) { + if (type2Req == null || !expr2.testFit(ctx, type2Req, false, probe).isFit()) { type2Req = typeRequired; } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Expression.java b/javatools/src/main/java/org/xvm/compiler/ast/Expression.java index 339dfb124d7..3b8cfc150f2 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Expression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Expression.java @@ -33,6 +33,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.checkElementsNonNull; /** @@ -304,8 +307,7 @@ protected TypeFit testFitMultiExhaustive(Context ctx, TypeConstant[] atypeRequir ErrorListener errs) { Expression exprTemp = (Expression) clone(); Context ctxTemp = ctx.enter(); - Expression exprNew = exprTemp.validateMulti(ctxTemp, atypeRequired, - errs == null ? ErrorListener.BLACKHOLE : errs); + Expression exprNew = exprTemp.validateMulti(ctxTemp, atypeRequired, errs); exprTemp.discard(true); ctxTemp.discard(); @@ -461,11 +463,16 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er * * @see #testFit(Context, TypeConstant, boolean, ErrorListener) */ - protected TypeFit testFitAsType(Context ctx, TypeConstant typeRequired, boolean fExhaustive, - ErrorListener errs) { + protected TypeFit testFitAsType(Context ctx, TypeConstant typeRequired, boolean fExhaustive) { + // a fit test reports nothing: its answer is the return value. Staging used to be handed + // the caller's listener, so whether a staging failure inside a probe was reported as a + // real error depended on what that caller happened to pass. validateAsType(), below, + // already had this right + ErrorListener probe = silent(PROBE); + TypeExpression exprType = toTypeExpression(); - return new StageMgr(exprType, Compiler.Stage.Validated, errs).fastForward(20) - ? exprType.testFit(ctx, typeRequired, fExhaustive, ErrorListener.BLACKHOLE) + return new StageMgr(exprType, Compiler.Stage.Validated, probe).fastForward(20) + ? exprType.testFit(ctx, typeRequired, fExhaustive, probe) : TypeFit.NoFit; } @@ -477,7 +484,7 @@ protected TypeFit testFitAsType(Context ctx, TypeConstant typeRequired, boolean protected Expression validateAsType(Context ctx, TypeConstant typeRequired, ErrorListener errs) { TypeExpression exprType = toTypeExpression(); - if (new StageMgr(exprType, Compiler.Stage.Validated, ErrorListener.BLACKHOLE).fastForward(20)) { + if (new StageMgr(exprType, Compiler.Stage.Validated, silent(PROBE)).fastForward(20)) { ErrorListener errsTemp = errs.branch(this); Expression exprNew = exprType.validate(ctx, typeRequired, errsTemp); if (exprNew != null) { @@ -500,8 +507,8 @@ protected Expression validateAsType(Context ctx, TypeConstant typeRequired, Erro * has already been applied; {@link TypeFit#isPacking()} indicates that a * tuple packing has already been applied; {@link TypeFit#isUnpacking()} * indicates that a tuple un-packing has already been applied - * @param constVal a constant value, iff this expression is constant (optional) - * @param errs the error list to log any errors to + * @param constVal a constant value, iff this expression is constant (optional) + * @param errs the error list to log any errors to * * @return an expression to use (which may or may not be "this"), or null to indicate that the * compilation should halt as soon as is practical @@ -1924,7 +1931,7 @@ protected TypeConstant[] selectCommonTypes(TypeConstant[] atypeThen, TypeConstan TypeConstant typeElse = atypeElse[i]; ConstantPool pool = pool(); - TypeConstant typeCommon = Op.selectCommonType(typeThen, typeElse, ErrorListener.BLACKHOLE); + TypeConstant typeCommon = Op.selectCommonType(typeThen, typeElse, silent(PROBE)); atypeCommon[i] = typeCommon == null && typeThen != null && typeElse != null ? typeThen.isOnlyNullable() ? pool.ensureNullableTypeConstant(typeElse) : typeElse.isOnlyNullable() ? pool.ensureNullableTypeConstant(typeThen) @@ -2281,8 +2288,8 @@ public boolean supportsLocalPropMode() { * @param fLocalPropOk if no L-value is provided, then this is used to indicate whether the * resulting argument can be a property constant indicating a local * property value - * @param code the code object to which the assembly is added - * @param errs the error listener to log to + * @param code the code object to which the assembly is added + * @param errs the error listener to log to * * @return an argument, if an Assignable was not provided */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java index 3ac0f4b86fa..a5c6047ad1a 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java @@ -20,11 +20,11 @@ import org.xvm.asm.constants.ClassConstant; import org.xvm.asm.constants.FormalConstant; import org.xvm.asm.constants.IntConstant; +import org.xvm.asm.constants.MethodConstant; import org.xvm.asm.constants.MethodInfo; +import org.xvm.asm.constants.PropertyConstant; import org.xvm.asm.constants.PropertyInfo; import org.xvm.asm.constants.RangeConstant; -import org.xvm.asm.constants.MethodConstant; -import org.xvm.asm.constants.PropertyConstant; import org.xvm.asm.constants.StringConstant; import org.xvm.asm.constants.TypeConstant; import org.xvm.asm.constants.TypeInfo; @@ -42,6 +42,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.indentLines; /** @@ -357,6 +360,8 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { ctx = ctx.enterInferring(typeLVal); } + ErrorListener probe = silent(PROBE); + TypeConstant[] atypeLVals = null; for (int i = Plan.ITERATOR.ordinal(); i <= Plan.ITERABLE.ordinal(); ++i) { plan = Plan.valueOf(i); @@ -368,7 +373,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { case ITERABLE -> pool.typeIterable(); }; - if (exprRVal.testFit(ctx, typeRVal, false, null).isFit()) { + if (exprRVal.testFit(ctx, typeRVal, false, probe).isFit()) { atypeLVals = fValid ? exprLVal.getTypes() : null; break; } @@ -396,7 +401,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { typeRValExact = pool.ensureParameterizedTypeConstant(typeRVal, atypeLVals); } - if (exprRVal.testFit(ctx, typeRValExact, false, null).isFit()) { + if (exprRVal.testFit(ctx, typeRValExact, false, probe).isFit()) { typeRVal = typeRValExact; } else { // the specific container type didn't fit; proceed with the basic type, diff --git a/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java index 470275d5330..35c79a232fb 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java @@ -53,6 +53,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Invocation expression represents calling a method or function. An oversimplification of the * model is as follows: @@ -293,7 +296,7 @@ protected boolean hasMultiValueImpl() { @Override public TypeConstant[] getImplicitTypes(Context ctx) { - return resolveReturnTypes(ctx, null, false, ErrorListener.BLACKHOLE); + return resolveReturnTypes(ctx, null, false, silent(PROBE)); } @Override @@ -303,8 +306,7 @@ public TypeFit testFitMulti(Context ctx, TypeConstant[] atypeRequired, boolean f return TypeFit.Fit; } - TypeConstant[] atype = resolveReturnTypes(ctx, atypeRequired, fExhaustive, - errs == null ? ErrorListener.BLACKHOLE : errs); + TypeConstant[] atype = resolveReturnTypes(ctx, atypeRequired, fExhaustive, errs); return calcFitMulti(ctx, atype, atypeRequired); } @@ -392,8 +394,7 @@ protected TypeConstant[] resolveReturnTypes(Context ctx, TypeConstant[] atypeReq TypeConstant[] atype = m_fCall || cReturns == 0 || atypeReturn == null ? atypeReturn : pool.extractFunctionReturns(atypeReturn[0]); - resolver = makeTypeParameterResolver(ctx, method, false, typeLeft, atype, - ErrorListener.BLACKHOLE); + resolver = makeTypeParameterResolver(ctx, method, false, typeLeft, atype, silent(PROBE)); } if (m_fCall) { @@ -876,7 +877,7 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er if (atypeReturn.length == 0) { atypeResult = atypeReturn; } else if (calculateReturnFit(sigMethod, fCall, atypeReturn, ctx.getThisType(), - ErrorListener.BLACKHOLE).isPacking()) { + silent(PROBE)).isPacking()) { atypeResult = new TypeConstant[]{pool.ensureTupleType(atypeResult)}; m_fPack = true; } @@ -1820,6 +1821,8 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef } } + ErrorListener probe = silent(PROBE); + // if the name does not have a left expression, then walk up the AST parent node chain // looking for a registered name, i.e. a local variable of that name, stopping once the // containing method/function (but not a lambda, since it has a permeable barrier to @@ -1837,7 +1840,7 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef } if (exprLeft == null) { - Argument arg = ctx.resolveName(tokName, ErrorListener.BLACKHOLE); + Argument arg = ctx.resolveName(tokName, probe); if (arg == null) { typeLeft = ctx.getThisType(); @@ -1846,8 +1849,7 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef // try to use the type info TypeInfo infoLeft = getTypeInfo(ctx, typeLeft, errs); - arg = findCallable(ctx, typeLeft, infoLeft, sName, MethodKind.Any, - true, atypeReturn, ErrorListener.BLACKHOLE); + arg = findCallable(ctx, typeLeft, infoLeft, sName, MethodKind.Any, true, atypeReturn, probe); if (arg instanceof MethodConstant idMethod) { MethodStructure method = getMethod(ctx, typeLeft, infoLeft, idMethod); if (method == null) { @@ -1872,12 +1874,11 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef log(errs, Severity.ERROR, Compiler.NO_SUPER); } else { TypeConstant typeTarget = ctx.getThisType(); - TypeInfo infoTarget = getTypeInfo(ctx, null, ErrorListener.BLACKHOLE); + TypeInfo infoTarget = getTypeInfo(ctx, null, probe); // check if the method would be callable from outside the constructor - if (ctx.isConstructor() && - findCallable(ctx, typeTarget, infoTarget, sName, MethodKind.Any, - true, atypeReturn, ErrorListener.BLACKHOLE) != null) { + if (ctx.isConstructor() && findCallable(ctx, typeTarget, infoTarget, sName, MethodKind.Any, + true, atypeReturn, probe) != null) { log(errs, Severity.ERROR, Compiler.INVALID_CALL_FROM_CONSTRUCT, sName); } else { log(errs, Severity.ERROR, Compiler.MISSING_METHOD, sName, @@ -1931,7 +1932,7 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef TypeInfo infoSuper = typeSuper.ensureTypeInfo(errs); MethodConstant idConstruct = (MethodConstant) findCallable(ctx, typeSuper, infoSuper, "construct", MethodKind.Constructor, - false, atypeReturn, ErrorListener.BLACKHOLE); + false, atypeReturn, probe); if (idConstruct == null) { log(errs, Severity.ERROR, Compiler.IMPLICIT_SUPER_CONSTRUCTOR_MISSING, ctx.getThisType().getValueString(), typeSuper.getValueString()); @@ -1991,7 +1992,7 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef // search if (kind == MethodKind.Function && findMethod(ctx, typeTarget, infoTarget, sName, args, MethodKind.Method, - !fNoCall, id.isNested(), atypeReturn, ErrorListener.BLACKHOLE) != null) { + !fNoCall, id.isNested(), atypeReturn, probe) != null) { if (target.getStepsOut() > 0) { exprName.log(errs, Severity.ERROR, Compiler.NO_OUTER_METHOD, target.getTargetType().removeAccess().getValueString(), sName); @@ -2202,9 +2203,8 @@ protected Argument resolveName(Context ctx, boolean fForce, TypeConstant typeLef Argument arg = findCallable(ctx, infoLeft.getType(), infoLeft, sName, kind, false, atypeReturn, errsTemp); - if (arg == null && kind == MethodKind.Function && - findCallable(ctx, infoLeft.getType(), infoLeft, sName, - MethodKind.Any, false, atypeReturn, ErrorListener.BLACKHOLE) != null) { + if (arg == null && kind == MethodKind.Function && findCallable(ctx, infoLeft.getType(), infoLeft, sName, + MethodKind.Any, false, atypeReturn, probe) != null) { exprName.log(errs, Severity.ERROR, Compiler.NO_THIS_METHOD, sName, infoLeft.getType().getValueString()); return null; @@ -2448,7 +2448,7 @@ private MethodStructure getMethod(Context ctx, MethodConstant idMethod) { method = (MethodStructure) idMethod.getComponent(); if (method == null) { TypeConstant type = m_targetInfo.getTargetType(); - TypeInfo info = getTypeInfo(ctx, type, ErrorListener.BLACKHOLE); + TypeInfo info = getTypeInfo(ctx, type, silent(PROBE)); method = getMethod(ctx, type, info, idMethod); } @@ -2481,7 +2481,7 @@ private PropertyStructure getProperty(Context ctx, PropertyConstant idProp) { PropertyStructure prop = (PropertyStructure) idProp.getComponent(); if (prop == null) { TypeConstant type = m_targetInfo.getTargetType(); - TypeInfo info = getTypeInfo(ctx, type, ErrorListener.BLACKHOLE); + TypeInfo info = getTypeInfo(ctx, type, silent(PROBE)); prop = info.findProperty(idProp).getHead().getStructure(); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java index 77c81511ffd..06b2258d3c3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java @@ -13,6 +13,7 @@ import java.util.stream.Collectors; import org.xvm.asm.Argument; +import org.xvm.asm.Assignment; import org.xvm.asm.Component; import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; @@ -21,7 +22,6 @@ import org.xvm.asm.MethodStructure.Code; import org.xvm.asm.MultiMethodStructure; import org.xvm.asm.Register; -import org.xvm.asm.Assignment; import org.xvm.asm.constants.DynamicFormalConstant; import org.xvm.asm.constants.FormalConstant; @@ -55,6 +55,10 @@ import org.xvm.util.Handy; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.indentLines; /** @@ -287,7 +291,9 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { @Override public TypeConstant getImplicitType(Context ctx) { - if (!ensurePrepared(ErrorListener.BLACKHOLE)) { + ErrorListener probe = silent(PROBE); + + if (!ensurePrepared(probe)) { return null; } @@ -311,12 +317,12 @@ public TypeConstant getImplicitType(Context ctx) { String[] asParams = cParams == 0 ? NO_NAMES : new String[cParams]; TypeConstant[] atypeParams = cParams == 0 ? TypeConstant.NO_TYPES : new TypeConstant[cParams]; - if (!collectParamNamesAndTypes(null, atypeParams, asParams, ErrorListener.BLACKHOLE)) { + if (!collectParamNamesAndTypes(null, atypeParams, asParams, probe)) { return null; } TypeConstant[] atypeReturns = - extractReturnTypes(ctx, atypeParams, asParams, null, false, ErrorListener.BLACKHOLE); + extractReturnTypes(ctx, atypeParams, asParams, null, false, probe); return atypeReturns == null ? null : pool().buildFunctionType(buildParamTypes(), atypeReturns); @@ -324,10 +330,6 @@ public TypeConstant getImplicitType(Context ctx) { @Override public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhaustive, ErrorListener errs) { - if (errs == null) { - errs = ErrorListener.BLACKHOLE; - } - if (!ensurePrepared(errs)) { return TypeFit.NoFit; } @@ -466,9 +468,8 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe int cParams = getParamCount(); if (cReqParams != -1 && cParams != cReqParams) { - errs.log(Severity.ERROR, Compiler.ARGUMENT_WRONG_COUNT, - new Object[]{cReqParams, cParams}, - getSource(), getStartPosition(), operator.getStartPosition()); + errs.error(Compiler.ARGUMENT_WRONG_COUNT, in(getSource(), getStartPosition(), operator.getStartPosition()), + cReqParams, cParams); fValid = false; } @@ -588,9 +589,8 @@ protected boolean collectParamNamesAndTypes(TypeConstant[] atypeReqParams, if (hasOnlyParamNames()) { if (atypeReqParams == null) { - errs.log(Severity.ERROR, Compiler.PARAMETER_TYPES_REQUIRED, null, - getSource(), paramNames.get(0).getStartPosition(), - paramNames.get(cParams - 1).getEndPosition()); + errs.error(Compiler.PARAMETER_TYPES_REQUIRED, in(getSource(), paramNames.get(0).getStartPosition(), + paramNames.get(cParams-1).getEndPosition())); fValid = false; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ListExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ListExpression.java index c8b122dca3b..19f27c278b7 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ListExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ListExpression.java @@ -27,6 +27,9 @@ import org.xvm.util.ListSet; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A list expression is an expression containing some number (0 or more) expressions of some common * type. @@ -102,7 +105,7 @@ private TypeConstant getBaseType(Context ctx, TypeConstant typeRequired) { return pool.typeArray(); } - return type.ensureTypeConstant(ctx, null); + return type.ensureTypeConstant(ctx, silent(PROBE)); } private TypeConstant getImplicitElementType(Context ctx) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/MapExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/MapExpression.java index 473ff5054bb..c3948479859 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/MapExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/MapExpression.java @@ -24,6 +24,9 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A map expression is an expression containing some number (0 or more) entries, each of which has * a key and a value. @@ -63,7 +66,7 @@ protected Field[] getChildFields() { @Override public TypeConstant getImplicitType(Context ctx) { - TypeConstant typeExplicit = type.ensureTypeConstant(ctx, null); + TypeConstant typeExplicit = type.ensureTypeConstant(ctx, silent(PROBE)); TypeConstant typeKey = null; TypeConstant typeVal = null; if (typeExplicit != null) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java index a5a424319d9..8fd1c101c6c 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java @@ -41,6 +41,10 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.appendString; import static org.xvm.util.Handy.indentLines; @@ -362,8 +366,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { if (fValidator) { if (modifiers != null && !modifiers.isEmpty()) { Token tok = modifiers.getFirst(); - errs.log(Severity.ERROR, Compiler.ILLEGAL_MODIFIER, null, - getSource(), tok.getStartPosition(), tok.getEndPosition()); + errs.error(Compiler.ILLEGAL_MODIFIER, in( + getSource(), tok.getStartPosition(), tok.getEndPosition())); return; } if (params != null && !params.isEmpty()) { @@ -726,7 +730,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { shuffle(atypeInto, iFound, iNext); shuffle(atypeAnno, iFound, iNext); - if (validateAnnotations(typeBase, aAnno, atypeAnno, atypeInto, ErrorListener.BLACKHOLE)) { + if (validateAnnotations(typeBase, aAnno, atypeAnno, atypeInto, silent(PROBE))) { fReordered = true; break Validate; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java index 490032f627f..a4b930d7827 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java @@ -11,6 +11,7 @@ import java.util.stream.Collectors; import org.xvm.asm.Annotation; +import org.xvm.asm.Argument; import org.xvm.asm.ClassStructure; import org.xvm.asm.Component; import org.xvm.asm.Constant; @@ -21,7 +22,6 @@ import org.xvm.asm.MethodStructure; import org.xvm.asm.MethodStructure.Code; import org.xvm.asm.MultiMethodStructure; -import org.xvm.asm.Argument; import org.xvm.asm.PackageStructure; import org.xvm.asm.PropertyStructure; import org.xvm.asm.Register; @@ -44,6 +44,9 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A name expression specifies a name. This handles a simple name, a qualified name, a dot name * expression, and names with type parameters and/or suppress-de-reference symbols. @@ -491,7 +494,7 @@ public void resetLValueTypes(Context ctx) { public TypeConstant getImplicitType(Context ctx) { return isValidated() ? getType() - : getImplicitType(ctx, null, ErrorListener.BLACKHOLE); + : getImplicitType(ctx, null, silent(PROBE)); } /** @@ -519,10 +522,6 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti return TypeFit.Fit; } - if (errs == null) { - errs = ErrorListener.BLACKHOLE; - } - return calcFit(ctx, getImplicitType(ctx, typeRequired, errs), typeRequired); } @@ -537,7 +536,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // outer type TypeConstant typeDesired = null; if (typeRequired != null && typeRequired.isTypeOfType() && isIdentityMode(ctx, true) && - testFit(ctx, pool.typeInner().getType(), false, null).isFit()) { + testFit(ctx, pool.typeInner().getType(), false, silent(PROBE)).isFit()) { typeDesired = pool.typeOuter().getType(); } @@ -755,7 +754,9 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } // there is a read of the implicit "this" variable else if (getParent() instanceof NameExpression) { - if (!ctx.requireThis(getStartPosition(), null)) { + // a question, not an assertion: the comment below says the outer + // expression reports whatever follows from the answer + if (!ctx.requireThis(getStartPosition(), silent(PROBE))) { // we know that this expression represents a property but there is // no "this"; we can only proceed with the identity mode here; // it becomes the outer expression's job to report any errors that @@ -2015,7 +2016,7 @@ protected Argument resolveRawArgument(Context ctx, boolean fForce, ErrorListener // process the "this.OuterName" construct if (!typeLeft.isTypeOfType() && !fIdMode) { Constant constTarget = new NameResolver(this, sName) - .forceResolve(ErrorListener.BLACKHOLE); + .forceResolve(silent(PROBE)); if (constTarget instanceof IdentityConstant && constTarget.isClass()) { if (constTarget.equals(pool.clzOuter())) { // this.Outer @@ -2752,7 +2753,7 @@ private TypeConstant resolveDynamicType(Context ctx, TypeConstant typeLeft, break CheckDynamic; } - Argument argLeft = exprLeft.resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE); + Argument argLeft = exprLeft.resolveRawArgument(ctx, false, silent(PROBE)); if (!(argLeft instanceof Register regLeft)) { break CheckDynamic; } @@ -3025,7 +3026,7 @@ public void narrowType(Context ctx, Branch branch, TypeConstant typeNarrow) { if (typeNarrow != null) { assert isValidated(); - Argument arg = resolveRawArgument(ctx, false, ErrorListener.BLACKHOLE); + Argument arg = resolveRawArgument(ctx, false, silent(PROBE)); if (left != null) { if (arg instanceof FormalTypeChildConstant constFormal) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java index e2180f473c5..512995b5972 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java @@ -449,8 +449,7 @@ private XvmStructure ensurePartiallyResolvedComponent() { } if (!type.isTypeOfType()) { - m_errs.log(Severity.ERROR, Compiler.NOT_CLASS_TYPE, - new Object[] {id.getValueString()}, component); + m_errs.error(Compiler.NOT_CLASS_TYPE, ErrorListener.at(component), id.getValueString()); m_stage = Stage.ERROR; return null; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java index 18adae194d3..51e6ec73361 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java @@ -37,6 +37,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.compiler.Lexer.isValidQualifiedModule; /** @@ -352,7 +355,7 @@ protected TypeConstant instantiateTypeConstant(Context ctx, ErrorListener errs) // constId has been already "auto-narrowed" by resolveNames() ConstantPool pool = pool(); - TypeConstant type = calculateDefaultType(ctx, constId, ErrorListener.BLACKHOLE); + TypeConstant type = calculateDefaultType(ctx, constId, silent(PROBE)); if (listParams != null) { int cParams = listParams.size(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java index d6fbf060a5d..ad19bf9e511 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java @@ -45,6 +45,10 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.util.Handy.indentLines; /** @@ -171,7 +175,7 @@ public AnonInnerClassContext getCaptureContext() { @Override public TypeConstant getImplicitType(Context ctx) { - return calculateTargetType(ctx, null); + return calculateTargetType(ctx, silent(PROBE)); } private TypeConstant calculateTargetType(Context ctx, ErrorListener errs) { @@ -179,10 +183,6 @@ private TypeConstant calculateTargetType(Context ctx, ErrorListener errs) { return getType(); } - if (errs == null) { - errs = ErrorListener.BLACKHOLE; - } - TypeConstant typeTarget = null; if (body == null) { if (isVirtualNew()) { @@ -684,7 +684,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // structures, such that we can revert it after we collect the information about the // captures; force a temp clone of the inner class to go through its validate() stage so // that we can determine what variables get captured (and if they are effectively final) - ensureInnerClass(ctx, AnonPurpose.CaptureAnalysis, ErrorListener.BLACKHOLE); + ensureInnerClass(ctx, AnonPurpose.CaptureAnalysis, silent(PROBE)); // the capture information gets collected in a specialized Context that was created with // the inner class @@ -1523,9 +1523,7 @@ public ClassStructure getThisClass() { @Override public boolean requireThis(long lPos, ErrorListener errs) { if (getMethod().isStatic()) { - if (errs != null) { - errs.log(Severity.ERROR, Compiler.NO_THIS, null, getSource(), lPos, lPos); - } + errs.error(Compiler.NO_THIS, in(getSource(), lPos, lPos)); return false; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NonBindingExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NonBindingExpression.java index 3f718d0ff10..5b8f518c0d1 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NonBindingExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NonBindingExpression.java @@ -9,6 +9,9 @@ import org.xvm.asm.constants.TypeConstant; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * This is used to specify an argument ("?") for a function that indicates that the corresponding * parameter of the function should remain unbound. @@ -51,7 +54,7 @@ protected Field[] getChildFields() { @Override public TypeConstant getImplicitType(Context ctx) { - return type == null ? null : type.ensureTypeConstant(ctx, null); + return type == null ? null : type.ensureTypeConstant(ctx, silent(PROBE)); } @Override diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NotNullExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NotNullExpression.java index 192c51987ee..5b03126fe6f 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NotNullExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NotNullExpression.java @@ -21,6 +21,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A short-circuiting expression for testing if a sub-expression is null, and yielding the non-null * value if the sub-expression is not null. @@ -99,15 +102,17 @@ public TypeConstant getImplicitType(Context ctx) { @Override public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhaustive, ErrorListener errs) { if (typeRequired != null) { + ErrorListener probe = silent(PROBE); + if (typeRequired.isTypeOfType()) { - TypeFit fit = toTypeExpression().testFit(ctx, typeRequired, fExhaustive, ErrorListener.BLACKHOLE); + TypeFit fit = toTypeExpression().testFit(ctx, typeRequired, fExhaustive, probe); if (fit.isFit()) { return fit; } } TypeFit fit = expr.testFitMulti(ctx, new TypeConstant[]{pool().typeBoolean(), typeRequired}, - fExhaustive, ErrorListener.BLACKHOLE); + fExhaustive, probe); if (fit.isFit()) { return fit; } @@ -124,7 +129,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe boolean fCond = false; TypeConstant[] atypeCond = new TypeConstant[]{pool.typeBoolean(), pool.typeObject()}; Expression exprNew; - if (expr.testFitMulti(ctx, atypeCond, true, ErrorListener.BLACKHOLE).isFit()) { + if (expr.testFitMulti(ctx, atypeCond, true, silent(PROBE)).isFit()) { m_fCond = fCond = true; if (typeRequired != null) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ParenthesizedExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ParenthesizedExpression.java index ad42149f973..9768ec4f427 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ParenthesizedExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ParenthesizedExpression.java @@ -10,6 +10,9 @@ import org.xvm.asm.constants.TypeConstant; import org.xvm.asm.constants.UnionTypeConstant; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Used for parenthesized expressions. */ @@ -27,10 +30,11 @@ public ParenthesizedExpression(Expression expr, long lStartPos, long lEndPos) { // ----- Expression compilation ---------------------------------------------------------------- @Override - public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhaustive, - ErrorListener errs) { - TypeFit fitTuple = testTupleFit(ctx, typeRequired, fExhaustive, null); - TypeFit fitValue = super.testFit(ctx, typeRequired, fExhaustive, null); + public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhaustive, ErrorListener errs) { + ErrorListener probe = silent(PROBE); + + TypeFit fitTuple = testTupleFit(ctx, typeRequired, fExhaustive, probe); + TypeFit fitValue = super.testFit(ctx, typeRequired, fExhaustive, probe); return fitValue.betterOf(fitTuple); } @@ -100,7 +104,7 @@ public TypeFit testFitMulti(Context ctx, TypeConstant[] atypeRequired, boolean f protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListener errs) { if (typeRequired != null) { TypeFit fitTuple = testTupleFit(ctx, typeRequired, true, null); - TypeFit fitValue = super.testFit(ctx, typeRequired, true, null); + TypeFit fitValue = super.testFit(ctx, typeRequired, true, silent(PROBE)); if (fitTuple.betterThan(fitValue)) { // replace this parenthesized expression with an actual tuple expression containing // the one element `(expr)` diff --git a/javatools/src/main/java/org/xvm/compiler/ast/RelOpExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/RelOpExpression.java index 1ef4a299a3d..5f2afe6ecc9 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/RelOpExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/RelOpExpression.java @@ -6,11 +6,11 @@ import java.util.Objects; import java.util.Set; +import org.xvm.asm.Argument; import org.xvm.asm.Constant; import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; import org.xvm.asm.MethodStructure.Code; -import org.xvm.asm.Argument; import org.xvm.asm.constants.ArrayConstant; import org.xvm.asm.constants.ConditionalConstant; @@ -24,10 +24,10 @@ import org.xvm.asm.op.GP_And; import org.xvm.asm.op.GP_Div; import org.xvm.asm.op.GP_DivRem; -import org.xvm.asm.op.GP_IRangeI; +import org.xvm.asm.op.GP_ERangeE; import org.xvm.asm.op.GP_ERangeI; import org.xvm.asm.op.GP_IRangeE; -import org.xvm.asm.op.GP_ERangeE; +import org.xvm.asm.op.GP_IRangeI; import org.xvm.asm.op.GP_Mod; import org.xvm.asm.op.GP_Mul; import org.xvm.asm.op.GP_Or; @@ -44,6 +44,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Relational operator expression (with @Op support) for something that follows the pattern * "expression operator expression". @@ -294,7 +297,7 @@ public TypeFit testFit(Context ctx, TypeConstant typeRequired, boolean fExhausti // this logic must conform to the rules used by validate() if (typeRequired != null && typeRequired.isTypeOfType()) { - TypeFit fit = testFitAsType(ctx, typeRequired, fExhaustive, errs); + TypeFit fit = testFitAsType(ctx, typeRequired, fExhaustive); if (fit.isFit()) { return fit; } @@ -622,12 +625,14 @@ private TypeConstant guessLeftType(Context ctx, TypeConstant typeRequired) { return null; } + ErrorListener probe = silent(PROBE); + String sMethod = getDefaultMethodName(); String sOp = operator.getId().TEXT; - if (expr1.testFit(ctx, typeRequired, false, null).isFit()) { + if (expr1.testFit(ctx, typeRequired, false, probe).isFit()) { Set setOps = typeRequired.ensureTypeInfo().findOpMethods(sMethod, sOp, 1); for (MethodConstant idMethod : setOps) { - if (expr2.testFit(ctx, idMethod.getRawParams()[0], false, null).isFit()) { + if (expr2.testFit(ctx, idMethod.getRawParams()[0], false, probe).isFit()) { TypeConstant typeReturn = idMethod.getRawReturns()[0]; if (typeReturn.containsAutoNarrowing(false)) { typeReturn = typeReturn.resolveAutoNarrowing(pool(), true, typeRequired, null); @@ -643,10 +648,10 @@ private TypeConstant guessLeftType(Context ctx, TypeConstant typeRequired) { if (typeRequired.isParamsSpecified()) { for (TypeConstant typeParam : typeRequired.getParamTypesArray()) { - if (expr1.testFit(ctx, typeParam, false, null).isFit()) { + if (expr1.testFit(ctx, typeParam, false, probe).isFit()) { Set setOps = typeParam.ensureTypeInfo().findOpMethods(sMethod, sOp, 1); for (MethodConstant idMethod : setOps) { - if (expr2.testFit(ctx, idMethod.getRawParams()[0], false, null).isFit()) { + if (expr2.testFit(ctx, idMethod.getRawParams()[0], false, probe).isFit()) { TypeConstant typeReturn = idMethod.getRawReturns()[0]; if (typeReturn.containsAutoNarrowing(false)) { typeReturn = typeReturn.resolveAutoNarrowing(pool(), false, @@ -686,6 +691,8 @@ private TypeConstant selectRightType(Context ctx, TypeConstant typeRequired, Set setOps = typeLeft.ensureTypeInfo().findOpMethods( getDefaultMethodName(), operator.getId().TEXT, 1); if (!setOps.isEmpty()) { + ErrorListener probe = silent(PROBE); + TypeConstant typeBest = null; TypeFit fitBest = TypeFit.NoFit; for (MethodConstant idMethod : setOps) { @@ -700,9 +707,9 @@ private TypeConstant selectRightType(Context ctx, TypeConstant typeRequired, } TypeConstant typeParam = idMethod.getRawParams()[0]; - TypeFit fit = expr2.testFit(ctx, typeParam, /*fExhaustive*/ false, null); + TypeFit fit = expr2.testFit(ctx, typeParam, /*fExhaustive*/ false, probe); if (!fit.isFit()) { - fit = expr2.testFitExhaustive(ctx, typeParam, null); + fit = expr2.testFitExhaustive(ctx, typeParam, probe); } if (fit.betterThan(fitBest)) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ReturnStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ReturnStatement.java index 94ac4d1c257..e0775f2d858 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ReturnStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ReturnStatement.java @@ -33,6 +33,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A return statement specifies a return with optional values. */ @@ -197,16 +200,18 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { ctx = ctx.enterInferring(typeRequired); } + ErrorListener probe = silent(PROBE); + // let's test several possibilities: do { // - most likely the expression matches the return types for the method - if (cRets < 0 || exprOld.testFitMulti(ctx, aRetTypes, false, null).isFit()) { + if (cRets < 0 || exprOld.testFitMulti(ctx, aRetTypes, false, probe).isFit()) { exprNew = exprOld.validateMulti(ctx, aRetTypes, errs); break; } // - it could be a conditional false - if (fConditional && exprOld.testFit(ctx, pool.typeFalse(), false, null).isFit()) { + if (fConditional && exprOld.testFit(ctx, pool.typeFalse(), false, probe).isFit()) { exprNew = exprOld.validate(ctx, pool.typeFalse(), errs); if (exprNew != null && (!exprNew.isConstant() || !exprNew.toConstant().equals(pool.valFalse()))) { // it's not clear how this could happen; it's more like an assertion @@ -219,7 +224,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { // - it could be a Future return if (cRets == 1) { TypeConstant typeFuture = pool.ensureFuture(aRetTypes[0]); - if (exprOld.testFit(ctx, typeFuture, false, null).isFit()) { + if (exprOld.testFit(ctx, typeFuture, false, probe).isFit()) { exprNew = exprOld.validate(ctx, typeFuture, errs); m_fFutureReturn = true; break; @@ -228,7 +233,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { // - it could be a tuple return TypeConstant typeTuple = pool.ensureTupleType(aRetTypes); - if (exprOld.testFit(ctx, typeTuple, false, null).isFit()) { + if (exprOld.testFit(ctx, typeTuple, false, probe).isFit()) { exprNew = exprOld.validate(ctx, typeTuple, errs); m_fTupleReturn = true; break; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/SequentialAssignExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/SequentialAssignExpression.java index c86e27c9a8c..1b4b99e2313 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/SequentialAssignExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/SequentialAssignExpression.java @@ -13,6 +13,9 @@ import org.xvm.compiler.Token.Id; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The "++" or "--" that precedes or follows an assignable expression of type Sequential. */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java index dc8302ae75b..8f0bb22379a 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java @@ -6,6 +6,8 @@ import java.util.function.Predicate; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.Component; import org.xvm.asm.ErrorListener; @@ -19,6 +21,8 @@ import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + /** * A Stage Manager is used to shepherd the AST nodes through their various stages. */ @@ -29,15 +33,15 @@ public class StageMgr { * * @param node the node to process * @param stageTarget the target stage - * @param errs the optional error list to log to + * @param errs the error listener to log to */ - public StageMgr(AstNode node, Stage stageTarget, ErrorListener errs) { + public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs) { assert node != null; assert stageTarget != null && stageTarget.isTargetable(); m_listRevisit = Collections.singletonList(node); m_target = stageTarget; - m_errs = errs == null ? ErrorListener.BLACKHOLE : errs; + m_errs = requireNonNull(errs, "errs"); } /** @@ -46,15 +50,15 @@ public StageMgr(AstNode node, Stage stageTarget, ErrorListener errs) { * * @param list the list of nodes to process * @param stageTarget the target stage - * @param errs the optional error list to log to + * @param errs the error listener to log to */ - public StageMgr(List list, Stage stageTarget, ErrorListener errs) { + public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs) { assert list != null && !list.isEmpty(); assert stageTarget != null && stageTarget.isTargetable(); m_listRevisit = list; m_target = stageTarget; - m_errs = errs == null ? ErrorListener.BLACKHOLE : errs; + m_errs = requireNonNull(errs, "errs"); } /** diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java index 847c443d6f5..321193490e2 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java @@ -58,6 +58,8 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.in; + /** * A block statement specifies a series of statements. * @@ -322,8 +324,7 @@ public boolean compileMethod(RootContext ctx, Code code, ErrorListener errs) { // a void method has an implicit "return;" at the end of it code.add(new Return_0()); } else { - errs.log(Severity.ERROR, Compiler.RETURN_REQUIRED, null, getSource(), - getEndPosition(), getEndPosition()); + errs.error(Compiler.RETURN_REQUIRED, in(getSource(), getEndPosition(), getEndPosition())); } } else { // it is possible that there is a dangling label at the end that is unreachable, @@ -737,8 +738,8 @@ public boolean requireThis(long lPos, ErrorListener errs) { ? exprLambda.isRequiredThis() : !isFunction(); - if (!fHasThis && errs != null) { - errs.log(Severity.ERROR, Compiler.NO_THIS, null, getSource(), lPos, lPos); + if (!fHasThis) { + errs.error(Compiler.NO_THIS, in(getSource(), lPos, lPos)); } return fHasThis; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementExpression.java index 32cec928730..50dc6540dab 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementExpression.java @@ -13,6 +13,10 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + /** * Statement expression is conceptually similar to a lambda, except that it does not require an * actual function, and it behaves as if it is executed at the point in the code where it is @@ -113,7 +117,7 @@ public TypeConstant[] getImplicitTypes(Context ctx) { ctx = enterStatementContext(ctx); // the resulting returned types come back in the type collector - StatementBlock blockTempNew = (StatementBlock) blockTempOld.validate(ctx, ErrorListener.BLACKHOLE); + StatementBlock blockTempNew = (StatementBlock) blockTempOld.validate(ctx, silent(PROBE)); ctx = ctx.exit(); // extract the type information (if everything validated ok) @@ -160,7 +164,7 @@ public TypeFit testFitMulti(Context ctx, TypeConstant[] atypeRequired, boolean f blockTempOld.suppressScope(); ctx = enterStatementContext(ctx); - StatementBlock blockTempNew = (StatementBlock) blockTempOld.validate(ctx, ErrorListener.BLACKHOLE); + StatementBlock blockTempNew = (StatementBlock) blockTempOld.validate(ctx, silent(PROBE)); ctx = ctx.exit(); TypeFit fit = TypeFit.NoFit; @@ -226,10 +230,8 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er @Override public void generateAssignments(Context ctx, Code code, Assignable[] aLVal, ErrorListener errs) { m_aLVal = aLVal; - if (body.completes(ctx, true, code, errs) && - m_atypeRequired != null && m_atypeRequired.length > 0) { - errs.log(Severity.ERROR, Compiler.RETURN_REQUIRED, null, getSource(), - getEndPosition(), getEndPosition()); + if (body.completes(ctx, true, code, errs) && m_atypeRequired != null && m_atypeRequired.length > 0) { + errs.error(Compiler.RETURN_REQUIRED, in(getSource(), getEndPosition(), getEndPosition())); } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TemplateExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TemplateExpression.java index df4bb4db36c..2ca077f5281 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TemplateExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TemplateExpression.java @@ -26,6 +26,9 @@ import org.xvm.util.Handy; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.asm.Assignment.AssignedOnce; /** @@ -95,14 +98,16 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe ctx.registerVar(tok$, m_reg$, errs); ctx.setVarAssignment("$", AssignedOnce); + ErrorListener probe = silent(PROBE); + // validate the expressions that make up the template int cExprs = exprs.size(); for (int i = 0; i < cExprs; ++i) { Expression exprOld = exprs.get(i); TypeConstant[] atypeExpr; - if (exprOld.testFit(ctx, T_STRING, false, null).isFit()) { + if (exprOld.testFit(ctx, T_STRING, false, probe).isFit()) { atypeExpr = A_STRING; - } else if (exprOld.testFit(ctx, T_OBJECT, false, null).isFit()) { + } else if (exprOld.testFit(ctx, T_OBJECT, false, probe).isFit()) { atypeExpr = A_OBJECT; } else { // void expression (e.g. a lambda-style expr explicitly appending to "$"); note that diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TernaryExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TernaryExpression.java index fc887c2f525..68c3759bac1 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TernaryExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TernaryExpression.java @@ -17,6 +17,9 @@ import org.xvm.asm.op.Return_1; import org.xvm.asm.op.Return_N; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A ternary expression is the "a ? b : c" expression. */ @@ -164,13 +167,15 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er int cThen = atypeThen.length; int cElse = atypeElse.length; + ErrorListener probe = silent(PROBE); + // try to figure out which side is more flexible TypeFit fitThen = cElse > 0 - ? exprThen.testFitMulti(ctxThen, atypeElse, false, null) + ? exprThen.testFitMulti(ctxThen, atypeElse, false, probe) : TypeFit.NoFit; TypeFit fitElse = cThen > 0 - ? exprElse.testFitMulti(ctxElse, atypeThen, false, null) + ? exprElse.testFitMulti(ctxElse, atypeThen, false, probe) : TypeFit.NoFit; use = computeUsage(fitThen, fitElse); @@ -182,11 +187,11 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er TypeConstant[] atypeThenR = resolveConstraints(atypeThen); TypeConstant[] atypeElseR = resolveConstraints(atypeElse); if (atypeElseR != null) { - fitThen = exprThen.testFitMulti(ctxThen, atypeElseR, false, null); + fitThen = exprThen.testFitMulti(ctxThen, atypeElseR, false, probe); } if (atypeThenR != null) { - fitElse = exprElse.testFitMulti(ctxElse, atypeThenR, false, null); + fitElse = exprElse.testFitMulti(ctxElse, atypeThenR, false, probe); } use = computeUsage(fitThen, fitElse); @@ -214,8 +219,8 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er if (atypeThenR != null && atypeElseR != null) { TypeConstant[] atypeCommonR = selectCommonTypes(atypeThenR, atypeElseR); - if (exprThen.testFitMulti(ctxThen, atypeCommonR, false, null).isFit() && - exprElse.testFitMulti(ctxElse, atypeCommonR, false, null).isFit() ) { + if (exprThen.testFitMulti(ctxThen, atypeCommonR, false, probe).isFit() && + exprElse.testFitMulti(ctxElse, atypeCommonR, false, probe).isFit() ) { atypeThen = atypeElse = atypeCommonR; break; } @@ -576,15 +581,17 @@ private TypeConstant[] resolveConstraints(TypeConstant[] atype) { private Plan generatePlan(Context ctx, boolean fExhaustive) { if (m_fConditional) { + ErrorListener probe = silent(PROBE); + TypeConstant typeFalse = pool().typeFalse(); // test "? (True, result) : False" first - if (exprElse.testFit(ctx, typeFalse, fExhaustive, null).isFit()) { + if (exprElse.testFit(ctx, typeFalse, fExhaustive, probe).isFit()) { return m_plan = Plan.ElseIsFalse; } // test "? False : (True, result)" next - if (exprThen.testFit(ctx, typeFalse, fExhaustive, null).isFit()) { + if (exprThen.testFit(ctx, typeFalse, fExhaustive, probe).isFit()) { return m_plan = Plan.ThenIsFalse; } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TraceExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TraceExpression.java index b40d6f14a48..afaa4669351 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TraceExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TraceExpression.java @@ -10,6 +10,9 @@ import org.xvm.asm.constants.TypeConstant; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * An expression that holds a copy of the result of another expression in order to provide optional * traceability. @@ -28,8 +31,7 @@ public TraceExpression(Expression expr) { assert expr.isValidated(); - finishValidations(null, null, expr.getTypes(), expr.getTypeFit(), expr.toConstants(), - ErrorListener.BLACKHOLE); + finishValidations(null, null, expr.getTypes(), expr.getTypeFit(), expr.toConstants(), silent(PROBE)); } // ----- accessors ----------------------------------------------------------------------------- diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TupleExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TupleExpression.java index d620edba7c0..bacf245eb37 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TupleExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TupleExpression.java @@ -7,11 +7,11 @@ import java.util.List; import java.util.stream.Collectors; +import org.xvm.asm.Argument; import org.xvm.asm.Constant; import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; import org.xvm.asm.MethodStructure.Code; -import org.xvm.asm.Argument; import org.xvm.asm.Register; import org.xvm.asm.constants.ArrayConstant; @@ -27,6 +27,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A tuple expression is an expression containing some number (0 or more) expressions. */ @@ -143,7 +146,7 @@ protected boolean hasMultiValueImpl() { @Override public TypeConstant getImplicitType(Context ctx) { ConstantPool pool = pool(); - TypeConstant typeTuple = type == null ? pool.typeTuple() : type.ensureTypeConstant(ctx, null); + TypeConstant typeTuple = type == null ? pool.typeTuple() : type.ensureTypeConstant(ctx, silent(PROBE)); if (typeTuple.containsUnresolved() || !typeTuple.isTuple()) { // let someone else log an error later, e.g. during validation, if the specified type diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java index 51168b30b24..e10a8b28fcb 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java @@ -57,10 +57,10 @@ import org.xvm.asm.op.Construct_N; import org.xvm.asm.op.JumpNType; import org.xvm.asm.op.L_Get; -import org.xvm.asm.op.Label; -import org.xvm.asm.op.SynInit; import org.xvm.asm.op.L_Set; +import org.xvm.asm.op.Label; import org.xvm.asm.op.Return_0; +import org.xvm.asm.op.SynInit; import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; @@ -72,8 +72,8 @@ import org.xvm.compiler.ast.CompositionNode.Default; import org.xvm.compiler.ast.CompositionNode.Delegates; import org.xvm.compiler.ast.CompositionNode.Extends; -import org.xvm.compiler.ast.CompositionNode.Incorporates; import org.xvm.compiler.ast.CompositionNode.Import; +import org.xvm.compiler.ast.CompositionNode.Incorporates; import org.xvm.compiler.ast.Context.Branch; import org.xvm.compiler.ast.StatementBlock.RootContext; @@ -82,6 +82,10 @@ import org.xvm.util.ListSet; import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.compiler.Constants.ECSTASY_MODULE; import static org.xvm.compiler.Constants.X_PKG_IMPORT; import static org.xvm.compiler.Lexer.CR; @@ -423,10 +427,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { // validate the module name String sModule = getName(); if (!isValidQualifiedModule(sModule)) { - errs.log(Severity.FATAL, Compiler.MODULE_BAD_NAME, - new String[] {sModule}, source, - qualified.get(0).getStartPosition(), - qualified.get(qualified.size() - 1).getEndPosition()); + errs.fatal(Compiler.MODULE_BAD_NAME, in(source, qualified.get(0).getStartPosition(), + qualified.get(qualified.size()-1).getEndPosition()), sModule); return; } @@ -1407,8 +1409,7 @@ private void reportAmbiguousConstructor(List listParams, TypeConstant lStart = listParams.get(0).getStartPosition(); lEnd = listParams.get(cParams - 1).getEndPosition(); } - errs.log(Severity.ERROR, Compiler.SIGNATURE_AMBIGUOUS, - new String[] {sb.toString()}, getSource(), lStart, lEnd); + errs.error(Compiler.SIGNATURE_AMBIGUOUS, in(getSource(), lStart, lEnd), sb.toString()); } /** @@ -1481,12 +1482,9 @@ public void resolveNames(StageMgr mgr, ErrorListener errs) { lEndPos = compositions.getFirst().getEndPosition(); } - errs.log(Severity.FATAL, Constants.VE_CYCLICAL_CONTRIBUTION, - new Object[] { - contribCyclical.getComponent().getIdentityConstant().getValueString(), - contribCyclical.getTypeConstant().getValueString() - }, - getSource(), lStartPos, lEndPos); + errs.fatal(Constants.VE_CYCLICAL_CONTRIBUTION, in(getSource(), lStartPos, lEndPos), + contribCyclical.getComponent().getIdentityConstant().getValueString(), + contribCyclical.getTypeConstant().getValueString()); return; } @@ -2312,8 +2310,7 @@ private Map findImplicitConstraint(ClassStructure clz, Str if (typeConstraint != null) { if (typeConstraint.equals(pool.typeObject())) { // report errors only at the "top" level - mapConstraints = findImplicitConstraint(clzContrib, sName, mapConstraints, - fAllowInto, ErrorListener.BLACKHOLE); + mapConstraints = findImplicitConstraint(clzContrib, sName, mapConstraints, fAllowInto, silent(PROBE)); } else { if (mapConstraints == null) { mapConstraints = new ListMap<>(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java index 799c3b9fe91..8bd3726dec6 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java @@ -9,6 +9,9 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A type expression is used to specify an abstract data type. In its compiled form, there are many * different possible representations of an abstract data type, depending on how it is declared, and @@ -36,7 +39,7 @@ public TypeExpression toTypeExpression() { * @return a TypeConstant */ public TypeConstant ensureTypeConstant() { - return ensureTypeConstant(null, null); + return ensureTypeConstant(null, silent(PROBE)); } /** @@ -44,7 +47,7 @@ public TypeConstant ensureTypeConstant() { * TypeConstant if necessary. * * @param ctx an optional Context; may be null - * @param errs an optional ErrorListener; may be null + * @param errs the error listener to log to * * @return a TypeConstant */ @@ -63,7 +66,7 @@ public TypeConstant ensureTypeConstant(Context ctx, ErrorListener errs) { // once the expression has validated, we know the type (can be Object for dynamic types) constType = getType().getParamType(0); } else { - constType = instantiateTypeConstant(ctx, errs == null ? ErrorListener.BLACKHOLE : errs); + constType = instantiateTypeConstant(ctx, errs); } m_constType = constType; @@ -181,7 +184,7 @@ public void resolveNames(StageMgr mgr, ErrorListener errs) { @Override public TypeConstant getImplicitType(Context ctx) { - TypeConstant type = ensureTypeConstant(ctx, null); + TypeConstant type = ensureTypeConstant(ctx, silent(PROBE)); if (type == null) { throw new IllegalStateException("type has not yet been determined for this: " + this); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/UnaryComplementExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/UnaryComplementExpression.java index 39f60c08b26..725e6de8a2c 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/UnaryComplementExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/UnaryComplementExpression.java @@ -12,6 +12,9 @@ import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The "~" that precedes a value (or "!" for a Boolean). */ @@ -46,7 +49,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // the "!" operator only applies to a boolean typeRight = pool().typeBoolean(); fBoolean = true; - } else if (typeRequired != null && exprRight.testFit(ctx, typeRequired, false, null).isFit() + } else if (typeRequired != null && exprRight.testFit(ctx, typeRequired, false, silent(PROBE)).isFit() && !typeRequired.ensureTypeInfo(errs).findOpMethods("not", "~", 0).isEmpty()) { typeRight = typeRequired; fBoolean = typeRight.isA(pool().typeBoolean()); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/UnaryMinusExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/UnaryMinusExpression.java index dccbe5bf0e3..03acc65e789 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/UnaryMinusExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/UnaryMinusExpression.java @@ -11,6 +11,9 @@ import org.xvm.compiler.Token; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * The "-" that precedes a number. */ @@ -55,7 +58,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // operation, then do so (just like with binary ops, convert as "deep" in the AST tree as // possible) TypeConstant typeRight = null; - if (typeRequired != null && exprRight.testFit(ctx, typeRequired, false, null).isFit() + if (typeRequired != null && exprRight.testFit(ctx, typeRequired, false, silent(PROBE)).isFit() && !typeRequired.ensureTypeInfo(errs).findOpMethods("neg", "-#", 0).isEmpty()) { typeRight = typeRequired; } diff --git a/javatools/src/main/java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java b/javatools/src/main/java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java index 9723b2f5745..48ad98802bc 100644 --- a/javatools/src/main/java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java +++ b/javatools/src/main/java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java @@ -16,7 +16,6 @@ import org.xvm.asm.ModuleRepository; import org.xvm.asm.ModuleStructure; import org.xvm.asm.Op; -import org.xvm.asm.XvmStructure; import org.xvm.asm.constants.MethodConstant; import org.xvm.asm.constants.ModuleConstant; @@ -226,7 +225,7 @@ private boolean popModuleStructure(Frame frame, CompilerAdapter compiler) { private int completeWithError(Frame frame, CompilerAdapter compiler, String sMissing, int[] aiReturn) { // org.xvm.compiler.Compiler.MODULE_MISSING - compiler.logError(Severity.FATAL, "MODULE_MISSING", new Object[] {sMissing}); + compiler.logError(Severity.FATAL, "MODULE_MISSING", sMissing); return completeCompilation(frame, compiler, null, aiReturn); } @@ -355,8 +354,8 @@ protected ModuleRepository getBuildRepository() { return m_repoResults; } - protected void logError(Severity severity, String sCode, Object[] aoParam) { - m_errorList.log(severity, sCode, aoParam, null); + protected void logError(Severity severity, String sCode, Object... aoParam) { + m_errorList.log(severity, sCode, ErrorListener.NOWHERE, aoParam); } /** diff --git a/javatools/src/main/java/org/xvm/runtime/template/_native/reflect/xRTType.java b/javatools/src/main/java/org/xvm/runtime/template/_native/reflect/xRTType.java index da504cb71c1..6ee9e96aa26 100644 --- a/javatools/src/main/java/org/xvm/runtime/template/_native/reflect/xRTType.java +++ b/javatools/src/main/java/org/xvm/runtime/template/_native/reflect/xRTType.java @@ -9,7 +9,6 @@ import org.xvm.asm.Constant; import org.xvm.asm.ConstantPool; import org.xvm.asm.Constants.Access; -import org.xvm.asm.ErrorListener; import org.xvm.asm.MethodStructure; import org.xvm.asm.Op; import org.xvm.asm.PackageStructure; @@ -34,8 +33,8 @@ import org.xvm.runtime.Container; import org.xvm.runtime.Frame; import org.xvm.runtime.ObjectHandle; -import org.xvm.runtime.ObjectHandle.DeferredCallHandle; import org.xvm.runtime.ObjectHandle.DeferredArrayHandle; +import org.xvm.runtime.ObjectHandle.DeferredCallHandle; import org.xvm.runtime.ObjectHandle.ExceptionHandle; import org.xvm.runtime.ObjectHandle.GenericHandle; import org.xvm.runtime.ProxyComposition; @@ -68,6 +67,9 @@ import org.xvm.runtime.template._native.reflect.xRTMethod.MethodHandle; import org.xvm.runtime.template._native.reflect.xRTProperty.PropertyHandle; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * Native RTType implementation. */ @@ -671,7 +673,7 @@ public int getPropertyConstructors(Frame frame, TypeHandle hType, int iReturn) { } ObjectHandle[] ahFunctions; - if (infoTarget.isNewable(false, ErrorListener.BLACKHOLE)) { + if (infoTarget.isNewable(false, silent(PROBE))) { ConstantPool pool = frame.poolContext(); TypeComposition clzTarget = typeTarget.ensureClass(frame); ArrayList listHandles = new ArrayList<>(); @@ -1486,7 +1488,7 @@ public int invokeStructConstructor(Frame frame, TypeHandle hType, ObjectHandle h assert !typeParent.equals(pool().typeObject()); } - if (!infoTarget.isNewable(false, ErrorListener.BLACKHOLE)) { + if (!infoTarget.isNewable(false, silent(PROBE))) { return frame.assignValue(aiReturn[0], xBoolean.FALSE); } diff --git a/javatools/src/main/java/org/xvm/tool/Bundler.java b/javatools/src/main/java/org/xvm/tool/Bundler.java index dad9e94fe8f..d848f371e9d 100644 --- a/javatools/src/main/java/org/xvm/tool/Bundler.java +++ b/javatools/src/main/java/org/xvm/tool/Bundler.java @@ -48,12 +48,12 @@ public class Bundler extends Launcher { /** * Bundler constructor for programmatic use. * - * @param options pre-configured bundler options - * @param console representation of the terminal within which this command is run, or null - * @param errListener optional ErrorListener to receive errors, or null for no delegation + * @param options pre-configured bundler options + * @param console representation of the terminal within which this command is run, or null + * @param errs the ErrorListener to receive errors */ - public Bundler(BundlerOptions options, Console console, ErrorListener errListener) { - super(options, console, errListener); + public Bundler(BundlerOptions options, Console console, ErrorListener errs) { + super(options, console, errs); } /** diff --git a/javatools/src/main/java/org/xvm/tool/Compiler.java b/javatools/src/main/java/org/xvm/tool/Compiler.java index e58adfd13ca..ede945be168 100644 --- a/javatools/src/main/java/org/xvm/tool/Compiler.java +++ b/javatools/src/main/java/org/xvm/tool/Compiler.java @@ -28,6 +28,8 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.NOWHERE; + import static org.xvm.compiler.Compiler.MODULE_MISSING; import static org.xvm.tool.ModuleInfo.isExplicitCompiledFile; import static org.xvm.util.Handy.parentOf; @@ -113,12 +115,12 @@ public Compiler(CompilerOptions options) { /** * Compiler constructor for programmatic use. * - * @param options pre-configured compiler options - * @param console representation of the terminal within which this command is run, or null - * @param errListener optional ErrorListener to receive errors, or null for no delegation + * @param options pre-configured compiler options + * @param console representation of the terminal within which this command is run, or null + * @param errs the ErrorListener to receive errors */ - public Compiler(CompilerOptions options, Console console, ErrorListener errListener) { - super(options, console, errListener); + public Compiler(CompilerOptions options, Console console, ErrorListener errs) { + super(options, console, errs); } /** @@ -426,8 +428,7 @@ protected void linkModules(List compilers, ModuleRepo for (var compiler : compilers) { ModuleConstant idMissing = compiler.linkModules(repo); if (idMissing != null) { - compiler.getErrorListener().log(FATAL, MODULE_MISSING, - new String[]{idMissing.getName()}, null); + compiler.getErrorListener().fatal(MODULE_MISSING, NOWHERE, idMissing.getName()); return; } } @@ -645,8 +646,9 @@ protected boolean isBadEnoughToAbort(Severity sev) { public boolean isAbortDesired() { // Check BOTH Console (tool errors) AND ErrorListener delegate (compiler errors) // Use Compiler's strictness-aware abort threshold - return isBadEnoughToAbort(m_sevWorst) || - (m_errors != ErrorListener.BLACKHOLE && m_errors.isAbortDesired()); + // no sentinel comparison: a silent listener answers false to isAbortDesired anyway, so + // the guard could never change the answer - and there is now more than one silent listener + return isBadEnoughToAbort(m_sevWorst) || m_errors.isAbortDesired(); } // ----- accessors ----------------------------------------------------------------------------- diff --git a/javatools/src/main/java/org/xvm/tool/Disassembler.java b/javatools/src/main/java/org/xvm/tool/Disassembler.java index f63da72ccfb..cb65effb9b8 100644 --- a/javatools/src/main/java/org/xvm/tool/Disassembler.java +++ b/javatools/src/main/java/org/xvm/tool/Disassembler.java @@ -28,8 +28,9 @@ import org.xvm.util.Handy; -import static java.time.ZoneOffset.UTC; import static java.util.Objects.requireNonNull; + +import static java.time.ZoneOffset.UTC; import static org.xvm.compiler.ast.FileExpression.createdTime; import static org.xvm.compiler.ast.FileExpression.modifiedTime; import static org.xvm.util.Handy.readFileBytes; @@ -58,12 +59,12 @@ public class Disassembler extends Launcher { /** * Disassembler constructor for programmatic use. * - * @param options pre-configured disassembler options - * @param console representation of the terminal within which this command is run, or null - * @param errListener optional ErrorListener to receive errors, or null for no delegation + * @param options pre-configured disassembler options + * @param console representation of the terminal within which this command is run, or null + * @param errs the ErrorListener to receive errors */ - public Disassembler(DisassemblerOptions options, Console console, ErrorListener errListener) { - super(options, console, errListener); + public Disassembler(DisassemblerOptions options, Console console, ErrorListener errs) { + super(options, console, errs); } /** diff --git a/javatools/src/main/java/org/xvm/tool/Initializer.java b/javatools/src/main/java/org/xvm/tool/Initializer.java index e9cdb13fbf8..d91f6ead06d 100644 --- a/javatools/src/main/java/org/xvm/tool/Initializer.java +++ b/javatools/src/main/java/org/xvm/tool/Initializer.java @@ -28,12 +28,12 @@ public class Initializer extends Launcher { /** * Initializer constructor for programmatic use. * - * @param options pre-configured initializer options - * @param console representation of the terminal within which this command is run, or null - * @param errListener optional ErrorListener to receive errors, or null for no delegation + * @param options pre-configured initializer options + * @param console representation of the terminal within which this command is run, or null + * @param errs the ErrorListener to receive errors */ - public Initializer(InitializerOptions options, Console console, ErrorListener errListener) { - super(options, console, errListener); + public Initializer(InitializerOptions options, Console console, ErrorListener errs) { + super(options, console, errs); } /** diff --git a/javatools/src/main/java/org/xvm/tool/Launcher.java b/javatools/src/main/java/org/xvm/tool/Launcher.java index 43edd23268f..3456dfc1fdc 100644 --- a/javatools/src/main/java/org/xvm/tool/Launcher.java +++ b/javatools/src/main/java/org/xvm/tool/Launcher.java @@ -13,6 +13,8 @@ import java.util.stream.Stream; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.BuildInfo; import org.xvm.asm.DirRepository; import org.xvm.asm.ErrorList; @@ -35,6 +37,11 @@ import org.xvm.util.Severity; +import static java.util.Objects.requireNonNull; + +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + import static org.xvm.asm.Constants.ECSTASY_MODULE; import static org.xvm.asm.Constants.TURTLE_MODULE; import static org.xvm.asm.Constants.VERSION_MAJOR_CUR; @@ -67,7 +74,7 @@ public abstract class Launcher */ @FunctionalInterface private interface CommandHandler { - int launch(String[] args, Console console, ErrorListener errListener); + int launch(String[] args, Console console, ErrorListener errs); } /** @@ -125,9 +132,8 @@ private static String commandNames() { protected final Console m_console; /** - * Optional ErrorListener that receives ALL errors (tool-level and compilation). When - * provided (not null), errors are forwarded for external programmatic access. Console - * displays errors, but m_errors provides structured access. + * The supplied listener receives diagnostics for external programmatic access. The command + * line supplies an explicit discard listener because its Console already displays them. */ protected final ErrorListener m_errors; @@ -148,11 +154,11 @@ private static String commandNames() { * @param options the pre-configured Options * @param console representation of the terminal within which this command is run (null = * default) - * @param errors optional ErrorListener to receive all errors (null = BLACKHOLE) + * @param errs the ErrorListener to receive all errors */ - protected Launcher(T options, Console console, ErrorListener errors) { + protected Launcher(T options, Console console, @NotNull ErrorListener errs) { m_console = console == null ? DEFAULT_CONSOLE : console; - m_errors = errors == null ? ErrorListener.BLACKHOLE : errors; + m_errors = requireNonNull(errs, "errs"); m_options = options; moduleCache = new HashMap<>(); } @@ -194,7 +200,9 @@ public static int launch(String[] asArg) { stripDebugPrefix(asArg[0]), Arrays.copyOfRange(asArg, 1, asArg.length), console, - null); + // the command line has no delegate to forward to: a Launcher is itself an + // ErrorListener, and reports through the Console it was given + silent(DISCARD)); } /** @@ -204,14 +212,15 @@ public static int launch(String[] asArg) { *

Supported commands: build, run, test (or --help, --version). * Shell scripts call with the command directly (xcc calls with "build", xec with "run"). * - * @param cmd command name: build, run, or test - * @param args command line arguments (options and files) - * @param console console for output (must not be null) - * @param errListener optional ErrorListener to receive errors, or null + * @param cmd command name: build, run, or test + * @param args command line arguments (options and files) + * @param console console for output (must not be null) + * @param errs the ErrorListener to receive errors * * @return exit code (0 for success, non-zero for error) */ - public static int launch(String cmd, String[] args, Console console, ErrorListener errListener) { + public static int launch(String cmd, String[] args, Console console, @NotNull ErrorListener errs) { + requireNonNull(errs, "errs"); try { // Check for global options first return switch (cmd) { @@ -226,7 +235,7 @@ public static int launch(String cmd, String[] args, Console console, ErrorListen default -> { final var handler = COMMANDS.get(cmd); if (handler != null) { - yield handler.launch(args, console, errListener); + yield handler.launch(args, console, errs); } // If the command looks like an option (e.g., "-L"), no command was provided; // show help without an error message @@ -286,24 +295,25 @@ private static void showVersion(Console console) { * * @param options pre-built options (CompilerOptions, RunnerOptions, or * DisassemblerOptions) - * @param console console for output (must not be null) - * @param errListener optional ErrorListener to receive errors, or null + * @param console console for output (must not be null) + * @param errs the ErrorListener to receive errors * * @return exit code (0 for success, non-zero for error) */ - public static int launch(LauncherOptions options, Console console, ErrorListener errListener) { + public static int launch(LauncherOptions options, Console console, @NotNull ErrorListener errs) { + requireNonNull(errs, "errs"); if (options == null) { console.log(ERROR, "Options must not be null"); return 1; } final var launcher = switch (options) { - case final CompilerOptions opts -> new Compiler(opts, console, errListener); - case final InitializerOptions opts -> new Initializer(opts, console, errListener); - case final TestRunnerOptions opts -> new TestRunner(opts, console, errListener); - case final RunnerOptions opts -> new Runner(opts, console, errListener); - case final DisassemblerOptions opts -> new Disassembler(opts, console, errListener); - case final BundlerOptions opts -> new Bundler(opts, console, errListener); + case final CompilerOptions opts -> new Compiler(opts, console, errs); + case final InitializerOptions opts -> new Initializer(opts, console, errs); + case final TestRunnerOptions opts -> new TestRunner(opts, console, errs); + case final RunnerOptions opts -> new Runner(opts, console, errs); + case final DisassemblerOptions opts -> new Disassembler(opts, console, errs); + case final BundlerOptions opts -> new Bundler(opts, console, errs); default -> { console.log(ERROR, "Unknown options type: {}", options.getClass().getName()); yield null; @@ -583,7 +593,7 @@ public String desc() { /** * Log a compilation error (ErrorInfo from AST nodes). - * Displays via Console and forwards to external ErrorListener if provided. + * Displays via Console and forwards to the supplied ErrorListener. * * @param err the error information */ diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 7aa6ef955bd..11d55530fd2 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -27,7 +27,11 @@ import org.xvm.compiler.ast.TypeCompositionStatement; import org.xvm.util.ListMap; -import org.xvm.util.Severity; + +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.in; +import static org.xvm.asm.ErrorListener.silent; import static org.xvm.asm.Constants.ECSTASY_MODULE; import static org.xvm.asm.Constants.TURTLE_MODULE; @@ -1027,7 +1031,7 @@ public void registerNames() { public void registerName(String name, Node node) { if (name != null) { if (children().containsKey(name)) { - log(Severity.ERROR, DUP_NAME, new Object[] {name, descriptiveName()}, null); + error(DUP_NAME, ErrorListener.NOWHERE, name, descriptiveName()); } else { children().put(name, node); } @@ -1038,7 +1042,7 @@ public void registerName(String name, Node node) { public void linkParseTrees() { Node nodePkg = sourceNode(); if (nodePkg == null) { - log(Severity.ERROR, MISSING_PKG_NODE, new Object[]{descriptiveName()}, null); + error(MISSING_PKG_NODE, ErrorListener.NOWHERE, descriptiveName()); } else { TypeCompositionStatement typePkg = nodePkg.type(); @@ -1266,7 +1270,7 @@ public char[] content() { try { return readFileChars(m_file); } catch (IOException e) { - log(Severity.ERROR, READ_FAILURE, new Object[] {m_file}, null); + error(READ_FAILURE, ErrorListener.NOWHERE, m_file); } return new char[0]; @@ -1348,8 +1352,7 @@ public void parse() { m_stmtAST = new Parser(source, this).parseSource(); } catch (CompilerException e) { if (!hasSeriousErrors()) { - log(Severity.FATAL, Parser.FATAL_ERROR, null, source, - source.getPosition(), source.getPosition()); + fatal(Parser.FATAL_ERROR, in(source, source.getPosition(), source.getPosition())); } } } @@ -1429,7 +1432,7 @@ public static String extractModuleName(File file) { if (isExplicitSourceFile(name)) { try { Source source = new Source(file); - Parser parser = new Parser(source, ErrorListener.BLACKHOLE); + Parser parser = new Parser(source, silent(DISCARD)); return parser.parseModuleNameIgnoreEverythingElse(); } catch (CompilerException | IOException ignore) {} } else if (isExplicitCompiledFile(name)) { diff --git a/javatools/src/main/java/org/xvm/tool/Runner.java b/javatools/src/main/java/org/xvm/tool/Runner.java index 6cc4ba2320a..1ac56fab3e1 100644 --- a/javatools/src/main/java/org/xvm/tool/Runner.java +++ b/javatools/src/main/java/org/xvm/tool/Runner.java @@ -29,6 +29,8 @@ import org.xvm.util.Handy; +import static java.util.Objects.requireNonNull; + import static org.xvm.tool.ModuleInfo.isExplicitCompiledFile; import static org.xvm.tool.ModuleInfo.isExplicitEcstasyFile; import static org.xvm.util.Handy.checkReadable; @@ -39,8 +41,6 @@ import static org.xvm.util.Severity.INFO; import static org.xvm.util.Severity.WARNING; -import static java.util.Objects.requireNonNull; - /** * The "execute" command: * @@ -53,12 +53,12 @@ public class Runner extends Launcher { /** * Runner constructor for programmatic use. * - * @param options pre-configured runner options - * @param console representation of the terminal within which this command is run, or null - * @param errListener optional ErrorListener to receive errors, or null for no delegation + * @param options pre-configured runner options + * @param console representation of the terminal within which this command is run, or null + * @param errs the ErrorListener to receive errors */ - public Runner(RunnerOptions options, Console console, ErrorListener errListener) { - super(options, console, errListener); + public Runner(RunnerOptions options, Console console, ErrorListener errs) { + super(options, console, errs); } /** diff --git a/javatools/src/main/java/org/xvm/tool/TestRunner.java b/javatools/src/main/java/org/xvm/tool/TestRunner.java index ca153389b38..159b7a6d7e0 100644 --- a/javatools/src/main/java/org/xvm/tool/TestRunner.java +++ b/javatools/src/main/java/org/xvm/tool/TestRunner.java @@ -78,12 +78,12 @@ static void main(final String[] args) { /** * TestRunner constructor for programmatic invocation. * - * @param options the runner options (RunnerOptions or TestRunnerOptions) - * @param console representation of the terminal within which this command is run - * @param errListener optional error listener for programmatic error access + * @param options the runner options (RunnerOptions or TestRunnerOptions) + * @param console representation of the terminal within which this command is run + * @param errs optional error listener for programmatic error access */ - public TestRunner(final TestRunnerOptions options, final Console console, final ErrorListener errListener) { - super(options, console, errListener); + public TestRunner(final TestRunnerOptions options, final Console console, final ErrorListener errs) { + super(options, console, errs); } @Override diff --git a/javatools/src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java b/javatools/src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java new file mode 100644 index 00000000000..77ce63126f8 --- /dev/null +++ b/javatools/src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java @@ -0,0 +1,127 @@ +package org.xvm.api; + +import java.nio.file.Path; + +import java.util.List; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import org.xvm.asm.Component.SimpleCollector; +import org.xvm.asm.ComponentResolver.ResolutionCollector; +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.compiler.BuildRepository; +import org.xvm.compiler.Compiler; +import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.Lexer; +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.TypeCompositionStatement; + +import org.xvm.tool.Console; +import org.xvm.tool.Launcher; +import org.xvm.tool.LauncherOptions.CompilerOptions; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import static org.xvm.asm.ErrorListener.NOWHERE; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + +/** A missing listener is rejected at the boundary, before work can silently lose diagnostics. */ +class ErrorListenerBoundaryTest { + @Test + void parserAndLexerRejectMissingListeners() { + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new Lexer(new Source("module Boundary {}"), null)).getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new Parser(new Source("module Boundary {}"), null)).getMessage()); + } + + @Test + void compilerAndStageManagersRejectMissingListeners() { + var module = module(); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new Compiler(module, null)).getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new StageMgr(module, Stage.Registered, null)).getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new StageMgr(List.of(module), Stage.Registered, null)).getMessage()); + } + + @Test + void embeddingRejectsMissingListenersBeforeCompilationOrFileAccess(@TempDir Path directory) { + var support = new EmbeddingSupport().configure(new BuildRepository(), null); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> support.compile("module Boundary {}", null, null)).getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> support.compile(directory.resolve("missing.x").toFile(), null, null, null)) + .getMessage()); + } + + @Test + void moduleCompilerRejectsMissingListenersBeforeCompilationOrFileAccess(@TempDir Path directory) { + var compiler = new ModuleCompiler(new BuildRepository()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> compiler.compile("module Boundary {}", null, null)).getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> compiler.compile(directory.resolve("missing.x").toFile(), null, null, null)) + .getMessage()); + } + + @Test + void launcherRejectsMissingListenersBeforeDispatch() { + var console = new Console() {}; + assertEquals("errs", assertThrows(NullPointerException.class, + () -> Launcher.launch("build", new String[]{"--help"}, console, null)) + .getMessage()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> Launcher.launch(new CompilerOptions.Builder().build(), console, null)) + .getMessage()); + } + + @Test + void resolutionCollectorsMustChooseAndRetainTheirDestination() throws NoSuchMethodException { + assertFalse(ResolutionCollector.class.getMethod("getErrorListener").isDefault()); + assertEquals("errs", assertThrows(NullPointerException.class, + () -> new SimpleCollector(null)).getMessage()); + var errors = new ErrorList(); + var collector = new SimpleCollector(errors); + assertSame(errors, collector.getErrorListener()); + collector.getErrorListener().error(Parser.MISSING_SEMICOLON, NOWHERE); + assertEquals(1, errors.getSeriousErrorCount()); + } + + @Test + void stageManagersRetainTheChosenListenerIncludingExplicitSilence() { + var module = module(); + var errors = new ErrorList(); + ErrorListener discard = silent(DISCARD); + assertSame(errors, new StageMgr(module, Stage.Registered, errors).getErrorListener()); + assertSame(discard, new StageMgr(List.of(module), Stage.Registered, discard).getErrorListener()); + module.log(discard, Severity.ERROR, Parser.MISSING_SEMICOLON); + assertFalse(discard.hasSeriousErrors()); + } + + @Test + void astReportsCannotSilentlyAcceptAMissingListener() { + var module = module(); + assertThrows(NullPointerException.class, + () -> module.log(null, Severity.ERROR, Parser.MISSING_SEMICOLON)); + } + + private static TypeCompositionStatement module() { + return (TypeCompositionStatement) new Parser(new Source("module Boundary {}"), new ErrorList()) + .parseSource().getStatements().getLast(); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java index c83cbb4774c..95d2abed908 100644 --- a/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerAbortTest.java @@ -35,6 +35,22 @@ public void testTheErrorBudgetStillAborts() { assertEquals(2, errs.getErrors().size()); } + /** + * The convenience constructor must use the same default budget as compiler and tool callers. + */ + @Test + public void testDefaultBudgetMatchesCompilerPolicy() { + ErrorList errs = new ErrorList(); + for (int i = 1; i < ErrorListener.DEFAULT_MAX_ERRORS; ++i) { + errs.error(CODE, ErrorListener.NOWHERE, i, "error"); + assertFalse(errs.isAbortDesired(), "default budget exhausted at report " + i); + } + + errs.error(CODE, ErrorListener.NOWHERE, ErrorListener.DEFAULT_MAX_ERRORS, "error"); + assertEquals(ErrorListener.DEFAULT_MAX_ERRORS, errs.getErrors().size()); + assertTrue(errs.isAbortDesired(), "the shared default budget is now spent"); + } + @Test public void testUnlimitedNeverAbortsOnCount() { Source source = new Source(SOURCE); diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java new file mode 100644 index 00000000000..3c8d1c1ffa0 --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java @@ -0,0 +1,62 @@ +package org.xvm.asm; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorListener.ErrorInfo; + +import org.xvm.compiler.CompilerException; +import org.xvm.compiler.Lexer; +import org.xvm.compiler.Source; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Exercises listener ownership through the real lexer, without installed XDK modules. + * + *

These tests use API shapes shared by the old and new listener contracts so the same source can + * demonstrate the failures on the previous compiler. Ignoring the old log return value is deliberate: + * the lexer itself decides when to stop, and the assertions observe its externally visible result. + */ +class ErrorListenerHostRegressionTest { + /** + * A host's buffering branch must not stop the lexer at the first recoverable error merely + * because the host uses a callback instead of ErrorList. Both bad escapes belong to this source. + */ + @Test + void hostBranchLetsLexerReportBothErrors() { + var received = new ArrayList(); + ErrorListener host = received::add; + var branch = host.branch(null); + var source = new Source("\"\\q\" \"\\w\""); + + assertDoesNotThrow(() -> new Lexer(source, branch).forEachRemaining(token -> {})); + assertEquals(0, received.size(), "speculative reports stay buffered until accepted"); + assertSame(host, branch.merge()); + assertEquals(List.of(Lexer.STRING_BAD_ESC, Lexer.STRING_BAD_ESC), + received.stream().map(ErrorInfo::getCode).toList()); + received.forEach(error -> assertSame(source, error.getSource())); + } + + /** + * Once the owner requests an abort, a previously created branch must not keep lexing just + * because its local count is unlimited. Suppression and buffering cannot revoke that request. + */ + @Test + void parentAbortReachesTheLexerThroughAnExistingBranch() { + var parent = new ErrorList(0); + var branch = parent.branch(null); + parent.log(new ErrorInfo(Severity.FATAL, "PARSER-03", new Object[]{"a", "b"}, null)); + + assertThrows(CompilerException.class, + () -> new Lexer(new Source("\"\\q\""), branch).forEachRemaining(token -> {})); + assertEquals(1, parent.getErrors().size(), "aborted trial reports were not merged"); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java index 3c947a5ece6..32b31b3ae9a 100644 --- a/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java @@ -2,6 +2,8 @@ import java.util.ArrayList; +import java.util.concurrent.atomic.AtomicBoolean; + import org.junit.jupiter.api.Test; import org.xvm.asm.ErrorListener.ErrorInfo; @@ -15,6 +17,8 @@ import org.xvm.util.Severity; +import static org.xvm.asm.ErrorListener.NOWHERE; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertInstanceOf; @@ -22,8 +26,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.xvm.asm.ErrorListener.NOWHERE; - /** Migration examples that compile independently of later listener-ownership changes. */ class ErrorListenerMigrationTest { @Test @@ -53,6 +55,7 @@ void branchesAndDerivedSilenceObserveTheParentBudget() { } @Test + @SuppressWarnings("deprecation") void legacyStructureReportsStillUseTheBranchSourceSite() { var source = new Source("module Example {}"); var errors = new ErrorList(); @@ -80,6 +83,35 @@ void parserStillStopsWhenReportingSpendsTheBudget() { assertTrue(errors.isAbortDesired()); } + /** + * A host may cancel without emitting an error. Suppression must preserve that live policy, + * even though the recoverable lexer diagnostic itself is deliberately discarded. + */ + @Test + void derivedSilencePreservesAHostStopWithoutAnError() { + var stopped = new AtomicBoolean(); + var received = new ArrayList(); + ErrorListener host = new ErrorListener() { + @Override + public void log(ErrorInfo error) { + received.add(error); + } + + @Override + public boolean isAbortDesired() { + return stopped.get(); + } + }; + var quiet = host.silence(Silence.CASCADE); + assertFalse(quiet.isAbortDesired()); + stopped.set(true); + + assertThrows(CompilerException.class, + () -> new Lexer(new Source("\"\\q\""), quiet).forEachRemaining(token -> {})); + assertTrue(received.isEmpty(), "suppression did not publish a diagnostic to cause the stop"); + assertTrue(quiet.isAbortDesired(), "the live host policy, not a local error, requests the stop"); + } + @Test void lexerStillStopsWhenReportingSpendsTheBudget() { var errors = new ErrorList(ErrorList.FIRST_ERROR); diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java index 2082546d27a..068553d3de0 100644 --- a/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java @@ -14,6 +14,7 @@ import static org.junit.jupiter.api.Assertions.assertNotSame; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.xvm.asm.ErrorListener.Silence.CASCADE; @@ -64,9 +65,8 @@ public void testSuppressCascadeIsSilentAndDoesNotDisturbTheCaller() { } /** - * The reason is part of the listener rather than part of its name, so a host republishing - * diagnostics can tell a probe from a cascade - which is what lets it offer the one and not - * the other. + * The reason makes a suppression decision inspectable. It does not retain discarded messages + * or select a different compiler algorithm. */ @Test public void testASilentListenerSaysWhyItIsSilent() { @@ -80,8 +80,8 @@ public void testASilentListenerSaysWhyItIsSilent() { } /** - * A silence derived from a listener keeps it, so a host that wants the consequences after all - * has somewhere to get them. A silence a caller reached for without having one cannot. + * A derived silence retains the original destination and its abort policy, not discarded + * messages. A standalone silence has no parent to preserve. */ @Test public void testADerivedSilenceKeepsTheListenerItSilenced() { @@ -106,6 +106,50 @@ public void testSilencingASilenceIsIdempotent() { assertSame(quiet, quiet.silence(PROBE), "already silent; a second reason adds nothing"); } + /** + * The one-argument constructor expresses a standalone sink without a null parent argument. + */ + @Test + public void testStandaloneConstructorDiscardsWithoutAnAbortPolicy() { + var quiet = new ErrorListener.SilentErrorListener(PROBE); + quiet.fatal(CODE, ErrorListener.NOWHERE, "a", "b"); + + assertNull(quiet.suppressed()); + assertEquals(PROBE, quiet.silenceReason()); + assertTrue(quiet.isSilent()); + assertFalse(quiet.hasSeriousErrors()); + assertFalse(quiet.hasError(CODE)); + assertFalse(quiet.isAbortDesired()); + } + + /** + * An explicitly supplied parent keeps its live abort policy, even after wrapper construction. + */ + @Test + public void testParentConstructorPreservesLaterAbortRequests() { + ErrorList parent = new ErrorList(ErrorList.FIRST_ERROR); + var quiet = new ErrorListener.SilentErrorListener(parent, CASCADE); + quiet.error(CODE, ErrorListener.NOWHERE, "discarded", "report"); + assertTrue(parent.getErrors().isEmpty()); + assertFalse(quiet.isAbortDesired()); + + parent.error(CODE, ErrorListener.NOWHERE, "retained", "report"); + assertSame(parent, quiet.suppressed()); + assertTrue(quiet.isAbortDesired()); + assertFalse(quiet.hasSeriousErrors()); + } + + /** + * A missing reason is a caller error for both new and already-silent listeners. + */ + @Test + @SuppressWarnings("DataFlowIssue") // Deliberately exercise Java callers violating @NotNull. + public void testSilenceAlwaysRequiresAReason() { + assertThrows(NullPointerException.class, () -> new ErrorListener.SilentErrorListener(null)); + assertThrows(NullPointerException.class, () -> new ErrorList().silence(null)); + assertThrows(NullPointerException.class, () -> silent(PROBE).silence(null)); + } + private static final String SOURCE = "module TestSimple { void run() {} }"; private static final String CODE = "PARSER-03"; } diff --git a/javatools/src/test/java/org/xvm/tool/BundlerTest.java b/javatools/src/test/java/org/xvm/tool/BundlerTest.java index f5b08b1aac9..d1075130a61 100644 --- a/javatools/src/test/java/org/xvm/tool/BundlerTest.java +++ b/javatools/src/test/java/org/xvm/tool/BundlerTest.java @@ -16,12 +16,15 @@ import org.xvm.asm.FileRepository; import org.xvm.asm.FileStructure; -import org.xvm.asm.Version; import org.xvm.asm.ModuleStructure.ModuleType; +import org.xvm.asm.Version; import org.xvm.asm.VersionTree; import org.xvm.tool.LauncherOptions.BundlerOptions; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -82,8 +85,7 @@ void testBundlerOptionsCommandLineRoundTrip() { @Test void testBundleCommandDispatch() { // -h takes the help path through the real Bundler launcher and returns success - int result = Launcher.launch(Launcher.CMD_BUNDLE, new String[] {"-h"}, - new Console() {}, null); + int result = Launcher.launch(Launcher.CMD_BUNDLE, new String[] {"-h"}, new Console() {}, silent(DISCARD)); assertEquals(0, result); } @@ -206,7 +208,7 @@ void testDuplicateExplicitModuleSelectionsAreRejected(@TempDir Path tempDir) thr int result = Launcher.launch(Launcher.CMD_BUNDLE, new String[] { "-o", tempDir.resolve("out.xtc").toString(), fileFirst.getPath(), - fileSecond.getPath()}, console, null); + fileSecond.getPath()}, console, silent(DISCARD)); assertEquals(1, result); var output = console.getAllOutput(); @@ -265,8 +267,7 @@ private static int launchBundle(Path fileOut, File... inputs) { for (var input : inputs) { args.add(input.getPath()); } - return Launcher.launch(Launcher.CMD_BUNDLE, args.toArray(new String[0]), - new CaptureConsole(), null); + return Launcher.launch(Launcher.CMD_BUNDLE, args.toArray(new String[0]), new CaptureConsole(), silent(DISCARD)); } private static final class CaptureConsole implements Console { diff --git a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java index c31e5b1898a..7566dcd405b 100644 --- a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java +++ b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java @@ -11,6 +11,9 @@ import java.util.ArrayList; import java.util.List; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -151,7 +154,7 @@ void testInfoLogging() { .addInputFile(new File("test.x")) .enableVerbose() // Also enable in options (for consistency) .build(); - TestCompiler compiler = new TestCompiler(opts, console, null); + TestCompiler compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(INFO, "Test info message"); @@ -167,7 +170,7 @@ void testWarningLogging() { CompilerOptions opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(WARNING, "Test warning: {}", "detail"); assertEquals(1, console.getMessages().size()); assertTrue(console.getMessages().getFirst().contains("Test warning: detail")); @@ -181,7 +184,7 @@ void testErrorLogging() { CompilerOptions opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(ERROR, "Test error: {} at line {}", "syntax", 42); assertEquals(1, console.getMessages().size()); assertTrue(console.getMessages().getFirst().contains("Test error: syntax at line 42")); @@ -195,7 +198,7 @@ void testFatalLogging() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); // FATAL error for now throws LauncherException immediately and the exception carries the // actual message to avoid double-dipping @@ -210,7 +213,7 @@ void testSeverityTracking() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); // Log multiple messages with different severities compiler.testLog(INFO, "Info message"); @@ -235,7 +238,7 @@ void testCheckErrorsWithInfo() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(INFO, "Info message"); @@ -249,7 +252,7 @@ void testCheckErrorsWithWarning() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(WARNING, "Warning message"); @@ -264,7 +267,7 @@ void testCheckErrorsWithError() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(ERROR, "Error message"); @@ -278,7 +281,7 @@ void testCheckErrorsWithFatal() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); // FATAL now throws LauncherException immediately - no need to call checkErrors() assertThrows(LauncherException.class, () -> compiler.testLog(FATAL, "Fatal error")); @@ -290,7 +293,7 @@ void testLogWithThrowable() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); final var cause = new IOException("File not found"); compiler.testLogWithThrowable(ERROR, cause, "Failed to read file: {}", "test.x"); @@ -308,7 +311,7 @@ void testLogWithThrowableNoTemplate() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); IOException cause = new IOException("File not found"); compiler.testLogWithThrowable(ERROR, cause, null); @@ -325,7 +328,7 @@ void testLogWithThrowableEmptyTemplate() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); IOException cause = new IOException("File not found"); compiler.testLogWithThrowable(ERROR, cause, ""); @@ -342,29 +345,31 @@ void testShouldAbortBehavior() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - var compiler = new TestCompiler(opts, console, null); + final var discard = silent(DISCARD); + + var compiler = new TestCompiler(opts, console, discard); // Test abort behavior: ERROR and worse should trigger abort (via Launcher.isAbortDesired()) compiler.testLog(Severity.NONE, "None message"); assertFalse(compiler.isAbortDesired()); console.clear(); - compiler = new TestCompiler(opts, console, null); + compiler = new TestCompiler(opts, console, discard); compiler.testLog(INFO, "Info message"); assertFalse(compiler.isAbortDesired()); console.clear(); - compiler = new TestCompiler(opts, console, null); + compiler = new TestCompiler(opts, console, discard); compiler.testLog(WARNING, "Warning message"); assertFalse(compiler.isAbortDesired()); console.clear(); - compiler = new TestCompiler(opts, console, null); + compiler = new TestCompiler(opts, console, discard); compiler.testLog(ERROR, "Error message"); assertTrue(compiler.isAbortDesired()); console.clear(); - final TestCompiler fatalCompiler = new TestCompiler(opts, console, null); + final TestCompiler fatalCompiler = new TestCompiler(opts, console, discard); // FATAL now throws LauncherException immediately assertThrows(LauncherException.class, () -> fatalCompiler.testLog(FATAL, "Fatal message")); } @@ -401,7 +406,7 @@ void testMultipleCheckErrorsCalls() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); // First checkpoint - should not throw, return 0 compiler.testLog(INFO, "Info 1"); @@ -429,7 +434,7 @@ void testNormalStrictnessWarningDoesNotAbort() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(WARNING, "Warning message"); @@ -446,7 +451,7 @@ void testNormalStrictnessErrorAborts() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(ERROR, "Error message"); @@ -463,7 +468,7 @@ void testStrictModeWarningAborts() { .addInputFile(new File("test.x")) .enableStrictMode() .build(); - final var compiler = new TestCompiler(opts, console, null, Compiler.Strictness.Stickler); + final var compiler = new TestCompiler(opts, console, silent(DISCARD), Compiler.Strictness.Stickler); compiler.testLog(WARNING, "Warning message"); @@ -480,7 +485,7 @@ void testStrictModeErrorAborts() { .addInputFile(new File("test.x")) .enableStrictMode() .build(); - final var compiler = new TestCompiler(opts, console, null, Compiler.Strictness.Stickler); + final var compiler = new TestCompiler(opts, console, silent(DISCARD), Compiler.Strictness.Stickler); compiler.testLog(ERROR, "Error message"); @@ -497,7 +502,7 @@ void testNoWarnModeWarningDoesNotAbort() { .addInputFile(new File("test.x")) .disableWarnings() .build(); - final var compiler = new TestCompiler(opts, console, null, Compiler.Strictness.Suppressed); + final var compiler = new TestCompiler(opts, console, silent(DISCARD), Compiler.Strictness.Suppressed); compiler.testLog(WARNING, "Warning message"); @@ -515,7 +520,7 @@ void testNoWarnModeErrorAborts() { .addInputFile(new File("test.x")) .disableWarnings() .build(); - final var compiler = new TestCompiler(opts, console, null, Compiler.Strictness.Suppressed); + final var compiler = new TestCompiler(opts, console, silent(DISCARD), Compiler.Strictness.Suppressed); compiler.testLog(ERROR, "Error message"); @@ -530,7 +535,7 @@ void testCheckErrorsReturnsExitCode() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); // No errors - should return 0 assertEquals(0, compiler.checkErrors()); @@ -558,7 +563,7 @@ void testCheckErrorsWithContextMessage() { final var opts = new CompilerOptions.Builder() .addInputFile(new File("test.x")) .build(); - final var compiler = new TestCompiler(opts, console, null); + final var compiler = new TestCompiler(opts, console, silent(DISCARD)); compiler.testLog(ERROR, "Error message"); diff --git a/javatools/src/test/java/org/xvm/tool/LauncherVersionTest.java b/javatools/src/test/java/org/xvm/tool/LauncherVersionTest.java index 8d1e2e13a2f..2b794b46ecc 100644 --- a/javatools/src/test/java/org/xvm/tool/LauncherVersionTest.java +++ b/javatools/src/test/java/org/xvm/tool/LauncherVersionTest.java @@ -7,6 +7,9 @@ import org.junit.jupiter.api.Test; import org.xvm.asm.BuildInfo; +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -48,7 +51,7 @@ public void testVersionOutputFormat() { final var args = new String[]{"--version"}; // Run build --version command - Launcher.launch(Launcher.CMD_BUILD, args, console, null); + Launcher.launch(Launcher.CMD_BUILD, args, console, silent(DISCARD)); final var output = console.getAllOutput().trim(); assertFalse(output.isEmpty(), "Version output should not be empty"); @@ -93,7 +96,7 @@ public void testRunnerVersionOutput() { final var console = new CaptureConsole(); final var args = new String[]{"--version"}; - Launcher.launch(Launcher.CMD_RUN, args, console, null); + Launcher.launch(Launcher.CMD_RUN, args, console, silent(DISCARD)); final var output = console.getAllOutput().trim(); assertFalse(output.isEmpty(), "Runner version output should not be empty"); @@ -107,7 +110,7 @@ public void testCompilerVersionOutput() { final var console = new CaptureConsole(); final var args = new String[]{"--version"}; - Launcher.launch(Launcher.CMD_BUILD, args, console, null); + Launcher.launch(Launcher.CMD_BUILD, args, console, silent(DISCARD)); final var output = console.getAllOutput().trim(); assertFalse(output.isEmpty(), "Compiler version output should not be empty"); From 5e7b84984261a8cc6fcdcf5a6d70f64fc3d2d46c Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:22 +0100 Subject: [PATCH 004/324] Scope parser diagnostics and restore validation listeners on every exit Buffer speculative parser diagnostics until an attempt is kept, scope parser and resolver destinations, and hold validation context and listener together. Restore reporting and validation state on early returns and exceptions; initialize evaluation and source-node buffers once. Preserve current parser nesting limits and add regression coverage for nested attempts, host callback failures, resolver reuse, statement restoration and diagnostic buffer draining. Document the parser API migration and thread confinement. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 59 ++-- .../src/main/java/org/xvm/asm/Reporting.java | 67 +++++ .../java/org/xvm/compiler/EvalCompiler.java | 8 +- .../main/java/org/xvm/compiler/Parser.java | 254 +++++++++++------- .../xvm/compiler/ast/ForEachStatement.java | 25 +- .../org/xvm/compiler/ast/ForStatement.java | 29 +- .../org/xvm/compiler/ast/NameResolver.java | 41 ++- .../java/org/xvm/compiler/ast/Statement.java | 9 +- .../org/xvm/compiler/ast/TryStatement.java | 27 +- .../org/xvm/compiler/ast/ValidationScope.java | 24 ++ .../org/xvm/compiler/ast/WhileStatement.java | 31 ++- .../main/java/org/xvm/tool/ModuleInfo.java | 25 +- .../test/java/org/xvm/asm/ReportingTest.java | 43 +++ .../org/xvm/compiler/EvalCompilerTest.java | 13 + .../org/xvm/compiler/ParserAttemptTest.java | 181 +++++++++++++ .../java/org/xvm/compiler/ParserTest.java | 4 +- .../ast/NameResolverReportingTest.java | 102 +++++++ .../xvm/compiler/ast/ValidationScopeTest.java | 116 ++++++++ .../java/org/xvm/tool/ModuleInfoTest.java | 26 ++ 19 files changed, 898 insertions(+), 186 deletions(-) create mode 100644 javatools/src/main/java/org/xvm/asm/Reporting.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/ValidationScope.java create mode 100644 javatools/src/test/java/org/xvm/asm/ReportingTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ParserAttemptTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java diff --git a/javatools/README.md b/javatools/README.md index b57757488ec..d473fce01e0 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -368,24 +368,47 @@ exercises the standard build path with compiled modules supplied by its Gradle d ### Review scope and related work -This listener-ownership migration builds on -[PR #680: separating diagnostic reporting from abort policy](https://github.com/xtclang/xvm/pull/680). -That pull request introduces the listener contract, factories and branch fixes. This change -migrates compiler callers to explicit destinations and named suppression, including the TypeInfo -cascade calls and required-listener boundaries. Most of the broad file coverage follows existing -reporting calls; review their severity, code, arguments and location alongside the substantive -ownership changes. - -The separate **scoped parser and validation reporting** branch handles temporary destinations, -parser attempts and restoration after normal, early and exceptional exits. Further -**explicit structure reporting and TypeInfo diagnostic replay** work removes ambient file/pool -lookup and preserves diagnostics associated with cached semantic results. Those changes belong -to their own review boundaries and are not claimed as completed by this migration. - -In this branch, the file/pool ambient-listener API and parser/resolver ownership are still present. -`XvmStructure.log` still has its existing fallback when passed null; the active compilation -boundaries listed above do not. This migration is a prerequisite for complete host reporting, -not a claim that every compiler-failure or TypeInfo-cache path is already repaired. +[PR #680: separating diagnostic reporting from abort policy](https://github.com/xtclang/xvm/pull/680) +introduces the listener contract, factories and branch fixes. +[PR #681: explicit compiler listeners and named suppression](https://github.com/xtclang/xvm/pull/681) +migrates callers and active compilation boundaries. This scoped-reporting change builds on both: +it controls temporary parser/resolver destinations and restores validation callback state on normal, +early and exceptional exits. + +Further **explicit structure reporting and TypeInfo diagnostic replay** work removes ambient +file/pool lookup and preserves diagnostics associated with cached semantic results. That work +remains a separate review boundary. The file/pool ambient-listener API is still present here: +`XvmStructure.log` retains its existing fallback when passed null; the active compilation +boundaries listed above do not. Scoped restoration is another prerequisite for complete host +reporting, not a claim that every compiler-failure or TypeInfo-cache path is already repaired. + +### Scoped parser and validation reporting + +`Parser.attempt()` replaces `Parser.SafeLookAhead`: use `Attempt.keep()` in place of +`keepResults()`. A kept attempt merges its buffered diagnostics, including warnings; a discarded +attempt restores its tokens and recovery state. Nested attempts observe their parent's abort +request. This removes a public nested parser class and requires source migration/recompilation at +the same breaking release boundary described above. + +Parser and resolver destinations use `Reporting` scopes, closed in reverse order with +try-with-resources. `NameResolver.getErrorListener()` is null outside `resolve`; callbacks borrow +the caller's non-null listener only during that call. These scopes restore destinations on every +exit. They do not make a parser, resolver, AST or listener safe for concurrent use. The lexer still +reports to its original listener; module-name-only scans should construct the parser with an +explicit discard listener if lexical diagnostics are unwanted. + +Loop and try/finally label variables retain their context/listener as one immutable +`ValidationScope` value. Each statement restores its previous value on normal, early and +exceptional exits, and `Statement.validate` likewise restores its common validation context. +The temporary references stay on the AST because label-variable and jump callbacks need the +currently validating statement. No shared mutable holder is added to cloned AST nodes. +`EvalCompiler` and `ModuleInfo.Node` own final diagnostic buffers from construction onward. + +The unit regressions cover nested reporting, failed host/resolver callbacks, token rollback, +abort propagation and exceptional statement exits without requiring installed modules. Compile +and run the existing `manualTests/src/main/x/loop.x` and `exceptions.x` exercises with the built +XDK to check lazy label variables and try/finally behavior through code generation and execution. +The separate Gradle compiler-consumer test supplies its compiled modules through dependencies. ## Assembler diff --git a/javatools/src/main/java/org/xvm/asm/Reporting.java b/javatools/src/main/java/org/xvm/asm/Reporting.java new file mode 100644 index 00000000000..59a339abfbf --- /dev/null +++ b/javatools/src/main/java/org/xvm/asm/Reporting.java @@ -0,0 +1,67 @@ +package org.xvm.asm; + +import static java.util.Objects.requireNonNull; + +/** + * A diagnostic destination that can be diverted for a lexical scope. + * + *

The parser and name resolver use this when callbacks cannot take a listener parameter. + * Scopes must be closed in reverse order, normally by try-with-resources. Each scope restores + * its predecessor even when the operation throws. An inactive resolver has a null destination; + * an active scope always has a listener. + * + *

This holder controls lifetime, not ownership or concurrency. Owners remain confined to one + * compiler operation; sharing a holder across threads is not supported. + */ +public final class Reporting { + /** + * @param errs where diagnostics go to begin with; null if the owner has no destination + * until one is given to it + */ + public Reporting(ErrorListener errs) { + m_errs = errs; + } + + /** + * @return where diagnostics go right now, or null if the owner has none + */ + public ErrorListener get() { + return m_errs; + } + + /** + * Send diagnostics to the given listener until the returned scope is closed. + * + * @param errs the listener to report to for the duration of the scope + * + * @return the scope, which restores the previous destination when closed + */ + public Scope to(ErrorListener errs) { + return new Scope(errs); + } + + /** + * The scope opened by {@link #to}. + */ + public final class Scope + implements AutoCloseable { + private Scope(ErrorListener errs) { + f_errsPrev = m_errs; + m_errs = requireNonNull(errs, "errs"); + } + + @Override + public void close() { + m_errs = f_errsPrev; + } + + private final ErrorListener f_errsPrev; + } + + @Override + public String toString() { + return "Reporting(" + m_errs + ")"; + } + + private ErrorListener m_errs; +} diff --git a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java index a40e8a8004f..c482747acdf 100644 --- a/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java +++ b/javatools/src/main/java/org/xvm/compiler/EvalCompiler.java @@ -64,7 +64,7 @@ public EvalCompiler(Frame frame, String sMethod) { */ public MethodStructure createLambda(TypeConstant typeReturn) { ConstantPool pool = f_frame.poolContext(); - ErrorList errs = m_errs = new ErrorList(1); + ErrorList errs = f_errs; MethodStructure method = f_frame.f_function; ClassStructure clz = method.getContainingClass(); @@ -154,7 +154,7 @@ private static MethodStructure discard(MethodStructure lambda) { * @return a list of errors */ public List getErrors() { - return m_errs.getErrors(); + return f_errs.getErrors(); } /** @@ -318,9 +318,9 @@ protected Argument getLocalVar(String sName, Branch branch) { private final Source f_source; /** - * The errors. + * Diagnostics owned by this evaluation, available before lambda compilation starts. */ - private ErrorList m_errs; + private final ErrorList f_errs = new ErrorList(ErrorList.FIRST_ERROR); /** * A synthetic MethodDeclarationStatement that contains the eval body. diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index 2516da74c9c..8551113a5d8 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -11,8 +11,11 @@ import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; + import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; +import org.xvm.asm.Reporting; import org.xvm.asm.Version; import org.xvm.compiler.Token.Id; @@ -54,7 +57,7 @@ public Parser(Source source, ErrorListener listener) { * @param atoken the tokens to parse */ protected Parser(Parser parent, Token[] atoken) { - this(parent.m_source, parent.m_errorListener, parent.m_lexer.createLexer(atoken)); + this(parent.m_source, parent.f_errs.get(), parent.m_lexer.createLexer(atoken)); } private Parser(Source source, ErrorListener errs, Lexer lexer) { @@ -62,20 +65,22 @@ private Parser(Source source, ErrorListener errs, Lexer lexer) { throw new IllegalArgumentException("Source required"); } - requireNonNull(errs, "errs"); - - m_source = source; - m_errorListener = errs; + m_source = source; + f_errs = new Reporting(requireNonNull(errs, "errs")); + // Lexical reports and cancellation follow the active speculative attempt. + ErrorListener lexicalReports = new ErrorListener() { + @Override + public void log(ErrorInfo error) { + f_errs.get().log(error); + } - // we need to route lexical reports through the active lookahead attempt, if any - ErrorListener listenerWrapper = err -> { - if (m_lookAhead == null) { - m_errorListener.log(err); - } else { - m_lookAhead.log(err); + @Override + public boolean isAbortDesired() { + return f_errs.get().isAbortDesired(); } }; - m_lexer = lexer == null ? new Lexer(source, listenerWrapper) : lexer; + m_lexer = lexer == null ? new Lexer(source, lexicalReports) : lexer; + // prime the token stream next(); @@ -143,47 +148,58 @@ public Map parseImplicits() { } /** - * Quick-scan the file for the module name. + * Quick-scan the source for the name of the module it declares, ignoring everything else. + * + *

Parser diagnostics from the scan use a {@link ErrorListener.Silence#DISCARD} silence. + * The module name itself is parsed against a separate listener so a malformed name is not + * accepted. Lexical diagnostics + * still use the lexer's original listener; callers performing only this scan should supply + * a discard listener when constructing the parser. * - * @return the module name + * @return the module's dotted name, or null if the source declares no module - or declares + * one whose name does not parse */ public String parseModuleNameIgnoreEverythingElse() { - ErrorListener errsPrev = m_errorListener; - try { - m_errorListener = silent(DISCARD); - - Loop: while (!eof()) { - if (match(Id.MODULE) != null) { - if (!eof()) { - m_errorListener = new ErrorList(1); - List tokens = parseQualifiedName(); - if (!m_errorListener.hasSeriousErrors()) { - StringBuilder sb = new StringBuilder(); - for (int i = 0, c = tokens.size(); i < c; ++i) { - if (i > 0) { - sb.append('.'); - } - sb.append(tokens.get(i).getValueText()); - } - return sb.toString(); - } + try (Reporting.Scope quiet = reportingTo(silent(DISCARD))) { + while (!eof()) { + if (match(Id.MODULE) == null) { + // not a module declaration; skip it, and give up at the first body we meet, + // because a module declaration cannot follow one + Id id = current().getId(); + if (id == Id.L_CURLY || id == Id.R_CURLY) { + return null; } - } else { - switch (current().getId()) { - case L_CURLY: - case R_CURLY: - break Loop; + } else if (!eof()) { + String sName = parseModuleName(); + if (sName != null) { + return sName; } } } - } catch (RuntimeException ignore) { - } finally { - m_errorListener = errsPrev; + } catch (RuntimeException _) { + // a quick scan answers or gives up without propagating parsing failures } return null; } + /** + * Parse the qualified name of a module that has just been announced by its keyword. + * + * @return the dotted name, or null if it did not parse cleanly + */ + private String parseModuleName() { + ErrorList errs = new ErrorList(ErrorList.FIRST_ERROR); + List tokens; + try (Reporting.Scope reporting = reportingTo(errs)) { + tokens = parseQualifiedName(); + } + + return errs.hasSeriousErrors() + ? null + : tokens.stream().map(Token::getValueText).collect(Collectors.joining(".")); + } + /** * As part of the runtime (NOT compile-time), parse the name of the class that is in the source. * @@ -723,7 +739,7 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet // evaluate annotations if (annotations != null) { for (AnnotationExpression annotation : annotations) { - annotation.log(m_errorListener, Severity.ERROR, Compiler.ANNOTATION_UNEXPECTED); + annotation.log(f_errs.get(), Severity.ERROR, Compiler.ANNOTATION_UNEXPECTED); } } @@ -741,7 +757,8 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet } // fall through default: - modifier.log(m_errorListener, m_source, Severity.ERROR, Compiler.KEYWORD_UNEXPECTED, modifier.getValueText()); + modifier.log(f_errs.get(), m_source, Severity.ERROR, Compiler.KEYWORD_UNEXPECTED, + modifier.getValueText()); break; } } @@ -851,7 +868,7 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet if (expr.isLValueSyntax()) { listLVals.add(expr); } else { - expr.log(m_errorListener, Severity.ERROR, NOT_ASSIGNABLE); + expr.log(f_errs.get(), Severity.ERROR, NOT_ASSIGNABLE); } } else { listLVals.add(new VariableDeclarationStatement( @@ -1710,16 +1727,16 @@ AstNode parseCondition(boolean fNegated) { // test for a negated conditional assignment Token tokNot = null; AssignmentStatement stmtAsn = null; - try (SafeLookAhead attempt = new SafeLookAhead()) { + try (Attempt attempt = attempt()) { tokNot = expect(Id.NOT); if (match(Id.L_PAREN) != null) { AstNode stmtPeek = parseCondition(true); if (attempt.isClean() && stmtPeek instanceof AssignmentStatement) { stmtAsn = (AssignmentStatement) stmtPeek; - attempt.keepResults(); + attempt.keep(); } } - } catch (CompilerException ignore) {} + } catch (CompilerException _) {} if (stmtAsn != null) { stmtAsn.negate(tokNot, expect(Id.R_PAREN)); @@ -2456,7 +2473,7 @@ AssignmentStatement parseVariableInitializer(boolean fAllowExpr) { */ Expression parseLinkerCondition() { Expression expr = parseExpression(); - expr.validateCondition(m_errorListener); + expr.validateCondition(f_errs.get()); return expr; } @@ -3100,15 +3117,15 @@ Expression parsePostfixExpression(boolean fExtended) { Token tokPeekLT = match(Id.COMP_LT); if (tokPeekLT != null) { putBack(tokPeekLT); - try (SafeLookAhead attempt = new SafeLookAhead()) { + try (Attempt attempt = attempt()) { params = parseTypeParameterTypeList(true, true); if (attempt.isClean()) { - attempt.keepResults(); + attempt.keep(); lEndPos = prev().getEndPosition(); } else { params = null; } - } catch (CompilerException ignore) {} + } catch (CompilerException _) {} } if (expr instanceof NamedTypeExpression) { @@ -3448,17 +3465,17 @@ Expression parsePrimaryExpression(boolean fExtended) { Token tokPeekLT = match(Id.COMP_LT); if (tokPeekLT != null) { putBack(tokPeekLT); - try (SafeLookAhead attempt = new SafeLookAhead()) { + try (Attempt attempt = attempt()) { params = parseTypeParameterTypeList(true, true); if (attempt.isClean() // "index>1" is a comparison and a shift, not a type && (peek().getId() != Id.COMP_GT || peek().hasLeadingWhitespace())) { - attempt.keepResults(); + attempt.keep(); lEndPos = prev().getEndPosition(); } else { params = null; } - } catch (CompilerException ignore) {} + } catch (CompilerException _) {} } // test to see if this is a tuple literal of the form "Tuple:(", or some other @@ -5626,14 +5643,10 @@ protected Token takeDoc() { * @param aoParam */ protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd, Object... aoParam) { - if (m_lookAhead != null) { - m_lookAhead.log(severity, sCode, aoParam, lPosStart, lPosEnd); - } else { - m_errorListener.log(severity, sCode, in(m_source, lPosStart, lPosEnd), aoParam); - if (m_errorListener.isAbortDesired()) { - m_fAvoidRecovery = true; - throw new CompilerException("error list is full: " + m_errorListener); - } + f_errs.get().log(severity, sCode, in(m_source, lPosStart, lPosEnd), aoParam); + if (f_errs.get().isAbortDesired()) { + m_fAvoidRecovery = true; + throw new CompilerException("error list is full: " + f_errs.get()); } } @@ -5685,57 +5698,110 @@ public void close() { } } - public class SafeLookAhead - implements AutoCloseable { - public SafeLookAhead() { - m_oldLookAhead = m_lookAhead; - m_lookAhead = this; - m_mark = mark(); - } + /** + * Report to the given listener until the returned scope is closed. + * + * @param errs the listener to report to for the duration of the scope + */ + private Reporting.Scope reportingTo(ErrorListener errs) { + return f_errs.to(errs); + } - public void log(Severity severity, String sCode, Object[] aoParam, long lPosStart, long lPosEnd) { - log(new ErrorList.ErrorInfo(severity, sCode, aoParam, m_source, lPosStart, lPosEnd)); + /** + * Begin a speculative parse: an attempt whose tokens and whose diagnostics both count only if + * it is kept. + * + *

The parser used to hand-roll this, buffering nothing and throwing away every diagnostic + * below ERROR even when the attempt was kept - which lost those diagnostics for good, because + * a kept attempt is the real parse and nothing re-reads those tokens. An attempt is a + * {@link ErrorListener#branch} instead, so keeping it merges what it had to say and dropping + * it drops the lot, which is the behaviour the rest of the compiler already gets. + */ + public Attempt attempt() { + return new Attempt(); + } + + /** + * The scope opened by {@link #attempt()}. + */ + public class Attempt + implements AutoCloseable, ErrorListener { + public Attempt() { + // read the destination before moving it: the branch buffers into whatever this parser + // was reporting to when the attempt began + f_branch = f_errs.get().branch(null); + f_scope = f_errs.to(this); + f_mark = mark(); + ++m_cSpeculating; } - private void log(ErrorListener.ErrorInfo err) { - if (err.getSeverity().ordinal() >= Severity.ERROR.ordinal()) { - m_err = err; - m_fKeepResults = false; - throw new CompilerException("err=" + m_err); + @Override + public void log(ErrorInfo err) { + f_branch.log(err); + + // an attempt that has gone wrong has answered; abandoning it here is what stops the + // parser from trying to recover inside a guess + if (err.getSeverity().isAtLeast(Severity.ERROR)) { + m_fKeep = false; + throw new CompilerException("err=" + err); } } + @Override + public boolean isAbortDesired() { + return f_branch.isAbortDesired(); + } + + @Override + public boolean hasSeriousErrors() { + return f_branch.hasSeriousErrors(); + } + + @Override + public boolean hasError(String sCode) { + return f_branch.hasError(sCode); + } + + /** + * @return true iff nothing serious has been reported against this attempt + */ public boolean isClean() { - return m_err == null; + return !f_branch.hasSeriousErrors(); } - public void keepResults() { - m_fKeepResults = true; + /** + * Keep what this attempt parsed, and with it whatever it had to say. + */ + public void keep() { + m_fKeep = true; } @Override public void close() { - assert m_lookAhead == this; - m_lookAhead = m_oldLookAhead; + assert f_errs.get() == this; + f_scope.close(); + --m_cSpeculating; - if (m_fKeepResults) { - assert m_err == null; + if (m_fKeep) { + assert isClean(); + f_branch.merge(); } else { - restore(m_mark); + // the attempt did not happen: its tokens are put back and its branch is dropped + restore(f_mark); } } - SafeLookAhead m_oldLookAhead; - Mark m_mark; - ErrorList.ErrorInfo m_err; - boolean m_fKeepResults; + private final ErrorListener f_branch; + private final Reporting.Scope f_scope; + private final Mark f_mark; + private boolean m_fKeep; } /** * @return true iff it's ok to try to recover from a parsing error */ protected boolean recoverable() { - return !eof() && !m_fAvoidRecovery && m_lookAhead == null; + return !eof() && !m_fAvoidRecovery && m_cSpeculating == 0; } // ----- Object methods ------------------------------------------------------------------------ @@ -5909,9 +5975,10 @@ public String toString() { private final Nesting f_nesting = new Nesting(); /** - * The ErrorListener to report errors to. + * Where this parser's diagnostics go: the listener it was handed, or - for as long as a + * scope or an {@link Attempt} is open - somewhere else. */ - private ErrorListener m_errorListener; + private final Reporting f_errs; /** * The lexical analyzer. @@ -5955,7 +6022,8 @@ public String toString() { private boolean m_fAvoidRecovery; /** - * Object supporting unpredictable amount of look-ahead. + * How many speculative attempts are open. Recovery is for the real parse: inside an attempt a + * syntax error is an answer, so there is nothing to recover from. */ - private SafeLookAhead m_lookAhead; + private int m_cSpeculating; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java index a5c6047ad1a..daca4ec8c44 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ForEachStatement.java @@ -224,7 +224,7 @@ public Register getLabelVar(Context ctx, String sName) { if (reg == null) { // this occurs only during validate() - assert m_ctxLabelVars != null; + assert m_labelVars != null; String sLabel = ((LabeledStatement) getParent()).getName(); Token tok = new Token(keyword.getStartPosition(), keyword.getEndPosition(), Id.IDENTIFIER, sLabel + '.' + sName); @@ -240,7 +240,7 @@ public Register getLabelVar(Context ctx, String sName) { }; reg = ctx.createRegister(type, getLabelName() + '.' + sName); - m_ctxLabelVars.registerVar(tok, reg, m_errsLabelVars); + m_labelVars.ctx().registerVar(tok, reg, m_labelVars.errs()); switch (sName) { case "first": m_regFirst = reg; break; @@ -261,6 +261,18 @@ public Register getLabelVar(Context ctx, String sName) { @Override protected Statement validateImpl(Context ctx, ErrorListener errs) { + ValidationScope previous = m_labelVars; + try { + return validateScoped(ctx, errs); + } finally { + m_labelVars = previous; + } + } + + /** + * Validate with callback state restored by {@link #validateImpl} on every exit. + */ + private Statement validateScoped(Context ctx, ErrorListener errs) { // each attempt to validate the loop will log errors into a temporary error list; whichever // run is the "keeper" will have its temporary errors moved over (relogged) into the // original error listener @@ -298,8 +310,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { ctx.setReachable(true); // save off the current context and errors, in case we have to lazily create some loop vars - m_ctxLabelVars = ctx; - m_errsLabelVars = errs; + m_labelVars = new ValidationScope(ctx, errs); // ultimately, the condition has to be re-written, because it is inevitably shorthand for // a measure of syntactic sugar; in order of precedence, the condition can be: @@ -561,8 +572,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { ctx = ctx.exit(); // lazily created loop vars are only created inside the validation of this statement - m_ctxLabelVars = null; - m_errsLabelVars = null; + m_labelVars = null; errs.merge(); return fValid ? this : null; @@ -1321,8 +1331,7 @@ public static Plan valueOf(int i) { private transient Expression m_exprLValue; private transient Expression m_exprRValue; private transient Plan m_plan; - private transient Context m_ctxLabelVars; - private transient ErrorListener m_errsLabelVars; + private transient ValidationScope m_labelVars; private transient Register m_regFirst; private transient Register m_regLast; private transient Register m_regCount; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ForStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ForStatement.java index 4b4ecf1d429..a515945cd16 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ForStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ForStatement.java @@ -229,13 +229,13 @@ public Register getLabelVar(Context ctx, String sName) { Register reg = fFirst ? m_regFirst : m_regCount; if (reg == null) { // this occurs only during validate() - assert m_ctxLabelVars != null; + assert m_labelVars != null; String sLabel = ((LabeledStatement) getParent()).getName(); Token tok = new Token(keyword.getStartPosition(), keyword.getEndPosition(), Id.IDENTIFIER, sLabel + '.' + sName); reg = ctx.createRegister(fFirst ? pool().typeBoolean() : pool().typeInt64(), sLabel + '.' + sName); - m_ctxLabelVars.registerVar(tok, reg, m_errsLabelVars); + m_labelVars.ctx().registerVar(tok, reg, m_labelVars.errs()); if (fFirst) { m_regFirst = reg; @@ -251,6 +251,18 @@ public Register getLabelVar(Context ctx, String sName) { @Override protected Statement validateImpl(Context ctx, ErrorListener errs) { + ValidationScope previous = m_labelVars; + try { + return validateScoped(ctx, errs); + } finally { + m_labelVars = previous; + } + } + + /** + * Validate with callback state restored by {@link #validateImpl} on every exit. + */ + private Statement validateScoped(Context ctx, ErrorListener errs) { boolean fValidInit = true; // the for() statement will represent its own scope @@ -332,8 +344,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { ctx.setReachable(true); // save off the current context and errors, in case we have to lazily create some loop vars - m_ctxLabelVars = ctx; - m_errsLabelVars = errs; + m_labelVars = new ValidationScope(ctx, errs); // the test expression plays a role of an "if", since the block cannot be entered if this // expression evaluates to "false" @@ -497,8 +508,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { } // lazily created loop vars are only created inside the validation of this statement - m_ctxLabelVars = null; - m_errsLabelVars = null; + m_labelVars = null; errs.merge(); return fValidInit && fValid ? this : null; @@ -670,10 +680,9 @@ public String toString() { private transient Label m_labelContinue; - private transient Context m_ctxLabelVars; - private transient ErrorListener m_errsLabelVars; - private transient Register m_regFirst; - private transient Register m_regCount; + private transient ValidationScope m_labelVars; + private transient Register m_regFirst; + private transient Register m_regCount; /** * Generally null, unless there is a "continue" that jumps to this statement. diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java index 512995b5972..95a98cfc067 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java @@ -18,6 +18,7 @@ import org.xvm.asm.ModuleStructure; import org.xvm.asm.PackageStructure; import org.xvm.asm.PropertyStructure; +import org.xvm.asm.Reporting; import org.xvm.asm.TypedefStructure; import org.xvm.asm.XvmStructure; @@ -112,10 +113,25 @@ public boolean isFirstTime() { * {@link Result#RESOLVED} to indicate that the name has been successfully resolved */ public Result resolve(ErrorListener errs) { - // store off the error list for use by call backs - // (note: there's no attempt to clean this up later) - m_errs = errs; + // the callbacks this resolution makes - ResolutionCollector.getErrorListener() among them + // - have no listener of their own, so the caller's is held for the duration of the call + // and given back afterwards. It used to be assigned and left, as the comment here admitted, + // which meant the resolver went on holding a listener belonging to a request that had + // finished: a later callback reported to it, and a resolver reused for a second name + // silently replaced the first caller's. + try (Reporting.Scope reporting = f_errs.to(errs)) { + return resolveStage(errs); + } + } + /** + * The stage machine behind {@link #resolve}, which runs with the caller's listener held. + * + * @param errs the listener to report to + * + * @return the result of advancing the resolution as far as it can go + */ + private Result resolveStage(ErrorListener errs) { switch (m_stage) { case CHECK_IMPORTS: // the first name could be an import, in which case that needs to be evaluated right @@ -360,7 +376,7 @@ private XvmStructure ensurePartiallyResolvedComponent() { // for methods (and multi-methods), it is not possible to further resolve the name, // because methods are opaque from the outside, and multi-methods can only be // resolved by analyzing signatures (not names) - m_node.log(m_errs, Severity.ERROR, Compiler.NAME_UNRESOLVABLE, m_sName); + m_node.log(f_errs.get(), Severity.ERROR, Compiler.NAME_UNRESOLVABLE, m_sName); m_stage = Stage.ERROR; return null; } else { @@ -449,7 +465,7 @@ private XvmStructure ensurePartiallyResolvedComponent() { } if (!type.isTypeOfType()) { - m_errs.error(Compiler.NOT_CLASS_TYPE, ErrorListener.at(component), id.getValueString()); + f_errs.get().error(Compiler.NOT_CLASS_TYPE, ErrorListener.at(component), id.getValueString()); m_stage = Stage.ERROR; return null; } @@ -607,7 +623,7 @@ public ResolutionResult resolvedComponent(Component component) { // it is possible that the name "resolved to" an ambiguous component, which is an error IdentityConstant id = component.getIdentityConstant(); if (component instanceof CompositeComponent composite && composite.isAmbiguous()) { - m_node.log(m_errs, Severity.ERROR, Compiler.NAME_AMBIGUOUS, m_sName); + m_node.log(f_errs.get(), Severity.ERROR, Compiler.NAME_AMBIGUOUS, m_sName); m_stage = Stage.ERROR; return ResolutionResult.ERROR; } @@ -619,7 +635,7 @@ public ResolutionResult resolvedComponent(Component component) { // typedef is allowed in type mode, but not in formal type mode if (m_typeMode == TypeMode.FORMAL_TYPE && !component.getParent().getIdentityConstant().equals(getPool().clzType())) { - m_node.log(m_errs, Severity.ERROR, Compiler.TYPEDEF_UNEXPECTED, + m_node.log(f_errs.get(), Severity.ERROR, Compiler.TYPEDEF_UNEXPECTED, m_sName, id.getParentConstant().getValueString()); m_stage = Stage.ERROR; return ResolutionResult.ERROR; @@ -661,7 +677,7 @@ public ResolutionResult resolvedComponent(Component component) { } if (fNameMissing) { - m_node.log(m_errs, Severity.ERROR, Compiler.NAME_MISSING, component.getName(), m_constant); + m_node.log(f_errs.get(), Severity.ERROR, Compiler.NAME_MISSING, component.getName(), m_constant); m_stage = Stage.ERROR; return ResolutionResult.ERROR; } @@ -677,7 +693,7 @@ public ResolutionResult resolvedComponent(Component component) { ? module.getFingerprintOrigin() : module; if (component == null) { - m_node.log(m_errs, Severity.ERROR, Compiler.MODULE_MISSING, module.getName()); + m_node.log(f_errs.get(), Severity.ERROR, Compiler.MODULE_MISSING, module.getName()); m_stage = Stage.ERROR; return ResolutionResult.ERROR; } @@ -731,7 +747,7 @@ public AstNode getNode() { @Override public ErrorListener getErrorListener() { - return m_errs; + return f_errs.get(); } // ----- inner classes ------------------------------------------------------------------------- @@ -823,7 +839,8 @@ private enum TypeMode {TYPE, FORMAL_TYPE} private TypeMode m_typeMode; /** - * The ErrorListener to log errors to. + * Where the callbacks made during a resolution report. Empty outside {@link #resolve}, which + * holds the caller's listener here for the duration of the call. */ - private ErrorListener m_errs; + private final Reporting f_errs = new Reporting(null); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java index 9a8a514f481..3da76d830d4 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java @@ -130,9 +130,14 @@ protected final Statement validate(Context ctx, ErrorListener errs) { // before validating the nested code, associate this statement with the context so that any // "break" or "continue" can find the context to apply assignment data to + Context ctxPrevious = m_ctx; + Statement stmt; m_ctx = ctx; - Statement stmt = validateImpl(ctx, errs); - m_ctx = null; + try { + stmt = validateImpl(ctx, errs); + } finally { + m_ctx = ctxPrevious; + } if (m_listBreaks != null) { for (Iterator iter = m_listBreaks.iterator(); iter.hasNext(); ) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TryStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/TryStatement.java index 69f417c7b97..7345a608980 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TryStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TryStatement.java @@ -87,7 +87,7 @@ protected Field[] getChildFields() { @Override public boolean hasLabelVar(String sName) { return "exception".equals(sName) && - (m_ctxValidatingFinally != null || m_regFinallyException != null); + (m_validatingFinally != null || m_regFinallyException != null); } @Override @@ -102,7 +102,7 @@ public Register getLabelVar(Context ctx, String sName) { Id.IDENTIFIER, sRegName); m_regFinallyException = reg = ctx.createRegister(pool().typeException१(), sRegName); - m_ctxValidatingFinally.registerVar(tok, reg, m_errsValidatingFinally); + m_validatingFinally.ctx().registerVar(tok, reg, m_validatingFinally.errs()); } return reg; @@ -112,6 +112,18 @@ public Register getLabelVar(Context ctx, String sName) { @Override protected Statement validateImpl(Context ctx, ErrorListener errs) { + ValidationScope previous = m_validatingFinally; + try { + return validateScoped(ctx, errs); + } finally { + m_validatingFinally = previous; + } + } + + /** + * Validate with callback state restored by {@link #validateImpl} on every exit. + */ + private Statement validateScoped(Context ctx, ErrorListener errs) { boolean fValid = true; if (resources == null) { @@ -263,11 +275,9 @@ protected void promoteNarrowedGenericType(FormalConstant constFormal, TypeConsta // promote the information gathered by the finally block Context ctxFinally = ctxCatchAll.enter(); - m_ctxValidatingFinally = ctxFinally; - m_errsValidatingFinally = errs; + m_validatingFinally = new ValidationScope(ctxFinally, errs); StatementBlock catchallNew = (StatementBlock) catchall.validate(ctxFinally, errs); - m_ctxValidatingFinally = null; - m_errsValidatingFinally = null; + m_validatingFinally = null; ctxFinally.promoteAssignments(ctxOrig); @@ -524,9 +534,8 @@ protected void promoteNarrowedType(String sName, Argument arg, Branch branch) { protected List catches; protected StatementBlock catchall; - private transient Context m_ctxValidatingFinally; - private transient ErrorListener m_errsValidatingFinally; - private transient Register m_regFinallyException; + private transient ValidationScope m_validatingFinally; + private transient Register m_regFinallyException; private static final Field[] CHILD_FIELDS = fieldsForNames(TryStatement.class, "resources", "block", "catches", "catchall"); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ValidationScope.java b/javatools/src/main/java/org/xvm/compiler/ast/ValidationScope.java new file mode 100644 index 00000000000..5d86972f101 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/ValidationScope.java @@ -0,0 +1,24 @@ +package org.xvm.compiler.ast; + +import org.xvm.asm.ErrorListener; + +import static java.util.Objects.requireNonNull; + +/** + * The context and error listener in force while a statement is being validated, captured so that a + * variable created lazily during that validation can still be registered against them. + * + *

A statement holds one of these only while it is validating, and null otherwise. That null is the + * statement's state - "I am not validating" - and not an absent listener: the two used to be + * separate fields set and cleared together, so "am I validating" and "what do I report to" could + * disagree, and the listener half had to be treated as nullable everywhere it was read. + * + * @param ctx the context to register lazily created variables against + * @param errs the listener to report against while validating + */ +record ValidationScope(Context ctx, ErrorListener errs) { + ValidationScope { + requireNonNull(ctx, "ctx"); + requireNonNull(errs, "errs"); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/WhileStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/WhileStatement.java index d026269d51c..8c7487edde3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/WhileStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/WhileStatement.java @@ -126,14 +126,14 @@ public Register getLabelVar(Context ctx, String sName) { Register reg = fFirst ? m_regFirst : m_regCount; if (reg == null) { // this occurs only during validate() - assert m_ctxLabelVars != null; + assert m_labelVars != null; String sLabel = ((LabeledStatement) getParent()).getName(); String sRegName = sLabel + '.' + sName; Token tok = new Token(keyword.getStartPosition(), keyword.getEndPosition(), Id.IDENTIFIER, sRegName); reg = ctx.createRegister(fFirst ? pool().typeBoolean() : pool().typeInt64(), sRegName); - m_ctxLabelVars.registerVar(tok, reg, m_errsLabelVars); + m_labelVars.ctx().registerVar(tok, reg, m_labelVars.errs()); if (fFirst) { m_regFirst = reg; @@ -149,6 +149,18 @@ public Register getLabelVar(Context ctx, String sName) { @Override protected Statement validateImpl(Context ctx, ErrorListener errs) { + ValidationScope previous = m_labelVars; + try { + return validateScoped(ctx, errs); + } finally { + m_labelVars = previous; + } + } + + /** + * Validate with callback state restored by {@link #validateImpl} on every exit. + */ + private Statement validateScoped(Context ctx, ErrorListener errs) { // there are a set of assumptions coming in: // - the loop is actually going to loop, as in "it is able to execute more than once" // -> while(False) obviously does not result in any execution of the body whatsoever (the @@ -236,8 +248,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { // the current context and error list are required by getLabelVar() if, in the process // of validation, one of the nested AST nodes requires a loop variable - m_ctxLabelVars = ctx; - m_errsLabelVars = errs; + m_labelVars = new ValidationScope(ctx, errs); m_listContinues = null; // either enter normal or loop, depending on the assumption @@ -403,8 +414,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { blockOrig.discard(true); // lazily created loop vars are only created inside the validation of this statement - m_ctxLabelVars = null; - m_errsLabelVars = null; + m_labelVars = null; if (ctxFork != null && !hasBreaks()) { // there are no breaks out of the loop, therefore the only way to get out is for the @@ -701,11 +711,10 @@ public String getDumpDesc() { protected StatementBlock block; protected long lEndPos; - private transient Label m_labelContinue; - private transient Context m_ctxLabelVars; - private transient ErrorListener m_errsLabelVars; - private transient Register m_regFirst; - private transient Register m_regCount; + private transient Label m_labelContinue; + private transient ValidationScope m_labelVars; + private transient Register m_regFirst; + private transient Register m_regCount; /** * Generally null, unless there is a "continue" that jumps to this statement. diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 11d55530fd2..595f54fb667 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -812,41 +812,32 @@ public void log(ErrorInfo err) { @Override public boolean isAbortDesired() { - return m_errs != null && m_errs.isAbortDesired(); + return f_errs.isAbortDesired(); } @Override public boolean hasSeriousErrors() { - return m_errs != null && m_errs.hasSeriousErrors(); + return f_errs.hasSeriousErrors(); } @Override public boolean hasError(String sCode) { - return m_errs != null && m_errs.hasError(sCode); + return f_errs.hasError(sCode); } /** * @return the list containing any errors accumulated on (or under) this node */ public ErrorList errs() { - ErrorList errs = m_errs; - if (errs == null) { - m_errs = errs = new ErrorList(ErrorListener.DEFAULT_MAX_ERRORS); - } - return errs; + return f_errs; } /** * Log any errors accumulated on (or under) this node */ public void logErrors(ErrorListener errs) { - ErrorList deferred = m_errs; - if (deferred != null) { - for (ErrorInfo err : deferred.getErrors()) { - errs.log(err); - } - deferred.clear(); - } + f_errs.getErrors().forEach(errs::log); + f_errs.clear(); } // ----- fields ------------------------------------------------------------------------ @@ -867,9 +858,9 @@ public void logErrors(ErrorListener errs) { protected ResourceDir m_resdir; /** - * The error list which buffers errors for the file node, if any. + * The diagnostic buffer owned by this node from construction onward. */ - private ErrorList m_errs; + private final ErrorList f_errs = new ErrorList(); } /** diff --git a/javatools/src/test/java/org/xvm/asm/ReportingTest.java b/javatools/src/test/java/org/xvm/asm/ReportingTest.java new file mode 100644 index 00000000000..0d41d3f112e --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ReportingTest.java @@ -0,0 +1,43 @@ +package org.xvm.asm; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Reporting destinations follow lexical nesting, including exceptional exits. */ +class ReportingTest { + @Test + void restoresNestedDestinationsAfterException() { + var original = new ErrorList(); + var outer = new ErrorList(); + var inner = new ErrorList(); + var reporting = new Reporting(original); + var failure = new IllegalStateException("operation failed"); + try (var scope = reporting.to(outer)) { + assertSame(outer, reporting.get()); + assertSame(failure, assertThrows(IllegalStateException.class, () -> { + try (var nested = reporting.to(inner)) { + assertSame(inner, reporting.get()); + throw failure; + } + })); + assertSame(outer, reporting.get()); + } + assertSame(original, reporting.get()); + } + + @Test + void inactiveDestinationIsRestoredAndNullScopesAreRejected() { + var reporting = new Reporting(null); + var errors = new ErrorList(); + assertThrows(NullPointerException.class, () -> reporting.to(null)); + assertNull(reporting.get()); + try (var scope = reporting.to(errors)) { + assertThrows(NullPointerException.class, () -> reporting.to(null)); + assertSame(errors, reporting.get()); + } + assertNull(reporting.get()); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java b/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java new file mode 100644 index 00000000000..ed1cbe20c9b --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java @@ -0,0 +1,13 @@ +package org.xvm.compiler; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +class EvalCompilerTest { + @Test + void diagnosticsAreAvailableBeforeCompilation() { + var compiler = new EvalCompiler(null, "return 1;"); + assertTrue(compiler.getErrors().isEmpty()); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ParserAttemptTest.java b/javatools/src/test/java/org/xvm/compiler/ParserAttemptTest.java new file mode 100644 index 00000000000..65853daca0a --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ParserAttemptTest.java @@ -0,0 +1,181 @@ +package org.xvm.compiler; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.util.Severity; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import static org.xvm.asm.ErrorList.UNLIMITED; + +/** + * A speculative parse is a branch: keeping it keeps what it had to say, dropping it drops the lot. + * + *

The parser cannot currently report anything below ERROR, so the kept-warning case here is a + * guard rather than a reproduction. It is worth guarding: the machinery this replaced discarded + * every sub-ERROR diagnostic unconditionally, and a kept attempt is the real parse - its tokens + * are not put back and nothing re-reads them - so the first warning the parser ever learned to + * report would have vanished with no trace. + */ +public class ParserAttemptTest { + /** + * A kept attempt's diagnostics reach the caller. + */ + @Test + public void testAKeptAttemptKeepsWhatItReported() { + ErrorList errs = new ErrorList(UNLIMITED); + Parser parser = new Parser(new Source(SOURCE), errs); + + try (Parser.Attempt attempt = parser.attempt()) { + parser.log(Severity.WARNING, CODE, 0, 1); + assertTrue(attempt.isClean(), "a warning is not a reason to abandon the attempt"); + assertEquals(0, errs.getErrors().size(), "a branch buffers until it is kept"); + attempt.keep(); + } + + assertEquals(1, errs.getErrors().size(), "the attempt was kept, so its report counts"); + assertEquals(Severity.WARNING, errs.getErrors().getFirst().getSeverity()); + } + + /** + * A discarded attempt's diagnostics do not. This is the half that already worked, and it has + * to keep working: speculative parses are the compiler's normal mode, and reporting from the + * roads it did not take would bury the ones it did. + */ + @Test + public void testADiscardedAttemptReportsNothing() { + ErrorList errs = new ErrorList(UNLIMITED); + Parser parser = new Parser(new Source(SOURCE), errs); + + try (Parser.Attempt attempt = parser.attempt()) { + parser.log(Severity.WARNING, CODE, 0, 1); + // never kept + } + + assertEquals(0, errs.getErrors().size(), "the attempt did not happen"); + } + + /** + * An error abandons the attempt immediately rather than letting the parser try to recover + * inside a guess, and still reaches nobody unless the attempt is somehow kept. + */ + @Test + public void testAnErrorAbandonsTheAttemptAndStaysBuffered() { + ErrorList errs = new ErrorList(UNLIMITED); + Parser parser = new Parser(new Source(SOURCE), errs); + + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt attempt = parser.attempt()) { + parser.log(Severity.ERROR, CODE, 0, 1); + } + }); + + assertEquals(0, errs.getErrors().size(), "a failed guess is not the user's problem"); + } + + /** + * The parser does not try to recover inside an attempt: a syntax error there is the answer. + */ + @Test + public void testRecoveryIsOffWhileSpeculating() { + Parser parser = new Parser(new Source(SOURCE), new ErrorList(UNLIMITED)); + + assertTrue(parser.recoverable(), "the real parse recovers"); + try (Parser.Attempt attempt = parser.attempt()) { + assertFalse(parser.recoverable(), "a guess does not"); + } + assertTrue(parser.recoverable(), "and recovery comes back afterwards"); + } + + @Test + public void testNestedKeptAttemptStillBelongsToOuterAttempt() { + var errors = new ErrorList(UNLIMITED); + var parser = new Parser(new Source(SOURCE), errors); + var first = parser.peek(); + try (var outer = parser.attempt()) { + parser.current(); + try (var inner = parser.attempt()) { + parser.log(Severity.WARNING, CODE, 0, 1); + inner.keep(); + } + assertTrue(outer.hasError(CODE)); + assertTrue(errors.getErrors().isEmpty()); + assertFalse(parser.recoverable()); + } + assertEquals(first.getId(), parser.peek().getId()); + assertEquals(first.getStartPosition(), parser.peek().getStartPosition()); + assertTrue(parser.recoverable()); + assertTrue(errors.getErrors().isEmpty()); + parser.log(Severity.WARNING, CODE, 2, 3); + assertEquals(1, errors.getErrors().size(), "the caller's destination is restored"); + } + + @Test + public void testExceptionRewindsTokensAndRestoresRecovery() { + var errors = new ErrorList(UNLIMITED); + var parser = new Parser(new Source(SOURCE), errors); + var first = parser.peek(); + assertThrows(CompilerException.class, () -> { + try (var attempt = parser.attempt()) { + parser.current(); + parser.log(Severity.ERROR, CODE, 0, 1); + } + }); + assertEquals(first.getId(), parser.peek().getId()); + assertEquals(first.getStartPosition(), parser.peek().getStartPosition()); + assertTrue(parser.recoverable()); + parser.log(Severity.WARNING, CODE, 2, 3); + assertEquals(1, errors.getErrors().size()); + } + + @Test + public void testKeptAttemptAdvancesTokensAndRestoresEvenWhenHostThrows() { + var failure = new IllegalStateException("host rejected report"); + var parser = new Parser(new Source(SOURCE), err -> { throw failure; }); + assertThrows(IllegalStateException.class, () -> { + try (var attempt = parser.attempt()) { + parser.current(); + parser.log(Severity.WARNING, CODE, 0, 1); + attempt.keep(); + } + }); + assertEquals(Token.Id.IDENTIFIER, parser.peek().getId()); + assertTrue(parser.recoverable()); + assertThrows(IllegalStateException.class, () -> parser.log(Severity.WARNING, CODE, 2, 3)); + } + + @Test + public void testNestedAttemptsObserveCallerAbort() { + var errors = new ErrorList(ErrorList.FIRST_ERROR); + var parser = new Parser(new Source(SOURCE), errors); + try (var outer = parser.attempt(); var inner = parser.attempt()) { + errors.error(CODE, ErrorListener.NOWHERE); + assertTrue(outer.isAbortDesired()); + assertTrue(inner.isAbortDesired()); + } + } + + @Test + public void testModuleNameScanRestoresDestinationForValidAndMalformedNames() { + List.of("module example.com {}", "module 123 {}", "class Other {}").forEach(source -> { + var errors = new ErrorList(UNLIMITED); + var parser = new Parser(new Source(source), errors); + assertEquals(source.startsWith("module example") ? "example.com" : null, + parser.parseModuleNameIgnoreEverythingElse()); + assertTrue(errors.getErrors().isEmpty()); + parser.log(Severity.WARNING, CODE, 0, 1); + assertEquals(1, errors.getErrors().size()); + }); + } + + private static final String SOURCE = "module TestSimple { void run() {} }"; + private static final String CODE = Parser.MISSING_SEMICOLON; +} diff --git a/javatools/src/test/java/org/xvm/compiler/ParserTest.java b/javatools/src/test/java/org/xvm/compiler/ParserTest.java index f55124d7d6d..2798e5db6c2 100644 --- a/javatools/src/test/java/org/xvm/compiler/ParserTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ParserTest.java @@ -123,7 +123,7 @@ void run() { @Test public void testDeeplyNestedTypeIsReported() { // types recurse through their own chain, not through the expression chain. The code is - // not asserted here: a type is parsed inside a SafeLookAhead, which discards the errors + // not asserted here: a type is parsed inside a Attempt, which discards the errors // of an attempt that fails, so what survives is the token error the caller reports // instead. What matters is that the parse ends in a CompilerException rather than a // StackOverflowError, which assertThrows already pins, since an Error is not a @@ -166,7 +166,7 @@ public void testDiscardedLookAheadDoesNotReportLexicalError() { ErrorList errlist = new ErrorList(5); Parser parser = new Parser(new Source("name \"\\q\""), errlist); - try (Parser.SafeLookAhead attempt = parser.new SafeLookAhead()) { + try (Parser.Attempt attempt = parser.attempt()) { assertThrows(CompilerException.class, parser::next); } diff --git a/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java new file mode 100644 index 00000000000..97da0b03d14 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java @@ -0,0 +1,102 @@ +package org.xvm.compiler.ast; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ComponentResolver; +import org.xvm.asm.ComponentResolver.ResolutionResult; +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** A resolver borrows its caller's listener only while making resolution callbacks. */ +class NameResolverReportingTest { + @Test + void callbackReportsToCallerAndReleasesIt() { + var errors = new ErrorList(); + var resolver = new NameResolver(new ResolvingNode((name, access, collector) -> { + assertSame(errors, collector.getErrorListener()); + collector.getErrorListener().error("TEST", ErrorListener.NOWHERE); + return ResolutionResult.ERROR; + }), "missing"); + assertNull(resolver.getErrorListener()); + assertEquals(NameResolver.Result.ERROR, resolver.resolve(errors)); + assertEquals(1, errors.getErrors().size()); + assertNull(resolver.getErrorListener()); + } + + @Test + void exceptionalCallbackRestoresInactiveStateAndCanBeRetriedWithAnotherListener() { + var first = new ErrorList(); + var second = new ErrorList(); + var failure = new IllegalStateException("callback failed"); + var resolver = new NameResolver(new ResolvingNode((name, access, collector) -> { + var errors = collector.getErrorListener(); + errors.warn("TEST", ErrorListener.NOWHERE); + if (errors == first) { + throw failure; + } + assertSame(second, errors); + return ResolutionResult.ERROR; + }), "missing"); + assertSame(failure, assertThrows(IllegalStateException.class, () -> resolver.resolve(first))); + assertNull(resolver.getErrorListener()); + assertEquals(NameResolver.Result.ERROR, resolver.resolve(second)); + assertNull(resolver.getErrorListener()); + assertEquals(1, first.getErrors().size()); + assertEquals(1, second.getErrors().size()); + } + + @Test + void deferredResolutionAndRejectedNullDoNotRetainAListener() { + var resolver = new NameResolver(new ResolvingNode(null), "later"); + assertEquals(NameResolver.Result.DEFERRED, resolver.resolve(new ErrorList())); + assertNull(resolver.getErrorListener()); + assertThrows(NullPointerException.class, () -> resolver.resolve(null)); + assertNull(resolver.getErrorListener()); + } + + @Test + void nestedResolutionRestoresTheOuterCallbacksListener() { + var outer = new ErrorList(); + var inner = new ErrorList(); + var resolver = new NameResolver(new ResolvingNode((name, access, collector) -> { + if (collector.getErrorListener() == outer) { + assertEquals(NameResolver.Result.ERROR, ((NameResolver) collector).resolve(inner)); + assertSame(outer, collector.getErrorListener()); + } else { + assertSame(inner, collector.getErrorListener()); + } + collector.getErrorListener().error("TEST", ErrorListener.NOWHERE); + return ResolutionResult.ERROR; + }), "missing"); + assertEquals(NameResolver.Result.ERROR, resolver.resolve(outer)); + assertNull(resolver.getErrorListener()); + assertEquals(1, outer.getErrors().size()); + assertEquals(1, inner.getErrors().size()); + } + + private static class ResolvingNode extends AstNode { + ResolvingNode(ComponentResolver resolver) { + f_resolver = resolver; + } + + @Override + public boolean isComponentNode() { return true; } + @Override + protected boolean canResolveNames() { return true; } + @Override + public ComponentResolver getComponentResolver() { return f_resolver; } + @Override + public long getStartPosition() { return 0; } + @Override + public long getEndPosition() { return 1; } + @Override + public String toString() { return "test component"; } + + private final ComponentResolver f_resolver; + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java b/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java new file mode 100644 index 00000000000..7276e462670 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java @@ -0,0 +1,116 @@ +package org.xvm.compiler.ast; + +import java.lang.reflect.Field; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.compiler.Source; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Callback context and listener state must not survive a failed validation. */ +class ValidationScopeTest { + @Test + void loopScopesReleaseTheirListenersAfterExceptionalExit() throws Exception { + var condition = new AssignmentStatement(null, token(Id.COLON), null); + List.of( + new ForStatement(token(Id.FOR), List.of(), List.of(), List.of(), block()), + new ForEachStatement(token(Id.FOR), condition, block()), + new WhileStatement(token(Id.WHILE), List.of(), block())).forEach(statement -> { + var field = scopeField(statement, "m_labelVars"); + var failure = new IllegalStateException("nested validation failed"); + var context = new Context(null, false) { + @Override + public Context enter() { return this; } + @Override + public Context enterIf() { return fail(); } + @Override + public Context enterLoop() { return fail(); } + + private Context fail() { + assertNotNull(readScope(field, statement), "failure must occur with an active scope"); + throw failure; + } + }; + assertSame(failure, assertThrows(IllegalStateException.class, + () -> statement.validate(context, new ErrorList()))); + assertNull(readScope(field, statement), statement.getClass().getSimpleName()); + assertThrows(IllegalStateException.class, statement::ensureValidationContext); + }); + } + + @Test + void finallyScopeAndStatementContextAreRestoredAfterFailure() throws Exception { + var failure = new IllegalStateException("finally failed"); + var body = new StatementBlock(List.of()) { + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { return this; } + }; + var catchall = new StatementBlock(List.of()) { + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + var owner = (TryStatement) getParent(); + assertNotNull(readScope(scopeField(owner, "m_validatingFinally"), owner)); + throw failure; + } + }; + var statement = new TryStatement(token(Id.TRY), null, body, null, catchall); + statement.adopt(body); + statement.adopt(catchall); + assertSame(failure, assertThrows(IllegalStateException.class, + () -> statement.validate(new Context(null, false), new ErrorList()))); + assertNull(readScope(scopeField(statement, "m_validatingFinally"), statement)); + assertThrows(IllegalStateException.class, statement::ensureValidationContext); + assertThrows(IllegalStateException.class, catchall::ensureValidationContext); + } + + @Test + void infiniteForEarlyReturnReleasesValidationState() { + var statement = new ForStatement(token(Id.FOR), List.of(), List.of(), List.of(), block()); + var source = new StatementBlock(List.of(statement), new Source("for (;;) {}"), 0, 1); + source.adopt(statement); + var errors = new ErrorList(); + assertNull(statement.validate(new Context(null, false), errors)); + assertTrue(errors.hasSeriousErrors(), "the empty infinite loop is rejected"); + assertNull(readScope(scopeField(statement, "m_labelVars"), statement)); + assertThrows(IllegalStateException.class, statement::ensureValidationContext); + } + + @Test + void validationScopeCannotContainMissingContextOrListener() { + assertThrows(NullPointerException.class, () -> new ValidationScope(null, new ErrorList())); + assertThrows(NullPointerException.class, () -> new ValidationScope(new Context(null, false), null)); + } + + private static StatementBlock block() { return new StatementBlock(List.of()); } + private static Token token(Id id) { return new Token(0, 1, id); } + + // Inspect ownership without adding public diagnostic-state accessors to AST nodes. + private static Field scopeField(Statement statement, String name) { + try { + var field = statement.getClass().getDeclaredField(name); + field.setAccessible(true); + return field; + } catch (ReflectiveOperationException e) { + throw new AssertionError(e); + } + } + + private static ValidationScope readScope(Field field, Statement statement) { + try { + return (ValidationScope) field.get(statement); + } catch (IllegalAccessException e) { + throw new AssertionError(e); + } + } +} diff --git a/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java b/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java index 26b02620b23..3edbf6d98d2 100644 --- a/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java +++ b/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java @@ -4,6 +4,7 @@ import org.junit.jupiter.api.io.TempDir; import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; import java.io.File; import java.io.IOException; @@ -72,6 +73,31 @@ private Path createModuleSourceTree(final String name) throws IOException { return sourceFile; } + @Test + void testNodeOwnsOneDiagnosticBufferFromConstruction() throws IOException { + var file = createModuleSource("Diagnostics").toFile(); + var info = new ModuleInfo(file, false); + var node = info.new FileNode(null, file); + var buffer = node.errs(); + assertSame(buffer, node.errs()); + assertFalse(node.hasSeriousErrors()); + node.error("TEST", ErrorListener.NOWHERE); + assertTrue(node.hasSeriousErrors()); + assertTrue(node.hasError("TEST")); + var diagnostic = buffer.getErrors().getFirst(); + var caller = new ErrorList(); + node.logErrors(caller); + assertEquals(1, caller.getErrors().size()); + assertSame(diagnostic, caller.getErrors().getFirst()); + assertTrue(buffer.getErrors().isEmpty(), "successful forwarding drains the buffer"); + assertFalse(node.hasSeriousErrors()); + var nextCaller = new ErrorList(); + node.logErrors(nextCaller); + assertTrue(nextCaller.getErrors().isEmpty(), "drained diagnostics are not replayed"); + node.fatal("FATAL", ErrorListener.NOWHERE); + assertTrue(node.isAbortDesired()); + } + // ----- Basic construction tests -------------------------------------------------------------- /** From 4cf65b7a3f49b60486bdbbc86d2e14401624f0d1 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:23 +0100 Subject: [PATCH 005/324] Expose host compilation outcomes, snapshots, cancellation and cached diagnostics Its module-source error is EMB-8, after master's EMB-6 (arguments ignored) and EMB-7 (entry method), which keep their codes and messages. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 118 ++++- .../java/org/xvm/api/EmbeddingSupport.java | 433 +++++++++++++++++- .../main/java/org/xvm/api/ModuleCompiler.java | 31 +- .../src/main/java/org/xvm/asm/Annotation.java | 2 +- .../main/java/org/xvm/asm/ErrorListener.java | 77 ++++ .../main/java/org/xvm/asm/FileStructure.java | 30 -- .../main/java/org/xvm/asm/XvmStructure.java | 34 +- .../asm/constants/ImmutableTypeConstant.java | 2 +- .../org/xvm/asm/constants/PropertyInfo.java | 4 +- .../org/xvm/asm/constants/TypeConstant.java | 61 ++- .../main/java/org/xvm/compiler/Compiler.java | 7 +- .../src/main/java/org/xvm/compiler/Lexer.java | 8 + .../main/java/org/xvm/compiler/Parser.java | 1 - .../org/xvm/compiler/ast/NameResolver.java | 19 +- .../org/xvm/compiler/ast/StatementBlock.java | 2 +- .../src/main/java/org/xvm/tool/Launcher.java | 3 +- .../main/java/org/xvm/tool/ModuleInfo.java | 155 ++++++- .../src/main/resources/errors.properties | 3 +- .../org/xvm/api/EmbeddingFootprintTest.java | 35 ++ .../asm/ErrorListenerCancellationTest.java | 43 ++ .../asm/FileStructureErrorListenerTest.java | 50 +- .../compiler/CompilerCancellationTest.java | 24 + .../ast/NameResolverReportingTest.java | 15 +- .../xvm/compiler/ast/ValidationScopeTest.java | 134 ++++-- .../xvm/tool/LauncherErrorHandlingTest.java | 13 + .../lsp/adapter/EmbeddingDiagnosticsTest.kt | 178 +++++++ .../adapter/EmbeddingSourceSnapshotTest.kt | 149 ++++++ .../lsp/adapter/TypeInfoDiagnosticsTest.kt | 292 ++++++++++++ 28 files changed, 1699 insertions(+), 224 deletions(-) create mode 100644 javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java create mode 100644 javatools/src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/CompilerCancellationTest.java create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingSourceSnapshotTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt diff --git a/javatools/README.md b/javatools/README.md index d473fce01e0..fde2df41867 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -109,7 +109,7 @@ results, and stop obsolete work at compiler checkpoints. It cannot reconstruct d the compiler discarded or obtain errors from work that an unintended one-error budget prevented. Leaving those failures in place blocks a reliable compiler-backed adapter, regardless of how much code is added around the public entry point. These changes establish the reporting foundation; -the companion ownership and delivery changes described below complete the paths built on it. +the result, source-snapshot and cached-diagnostic APIs below complete the host-facing path. The exact helper names and the decision to remove a legacy alias are API design choices. The delivery, state and stopping guarantees they implement are the essential requirements. @@ -366,21 +366,22 @@ Check failures and skips in `javatools/build/test-results/test/TEST-*.xml`; a gr not establish that optional tests executed. The real compiler-consumer integration test additionally exercises the standard build path with compiled modules supplied by its Gradle dependencies. -### Review scope and related work +### Combined embedding review scope -[PR #680: separating diagnostic reporting from abort policy](https://github.com/xtclang/xvm/pull/680) -introduces the listener contract, factories and branch fixes. -[PR #681: explicit compiler listeners and named suppression](https://github.com/xtclang/xvm/pull/681) -migrates callers and active compilation boundaries. This scoped-reporting change builds on both: -it controls temporary parser/resolver destinations and restores validation callback state on normal, -early and exceptional exits. +This combined change includes the listener contract and explicit-listener migration, scoped +parser/validation reporting, compilation outcomes, cached TypeInfo diagnostic replay, cancellation +and host-supplied module source snapshots. The shared behavior can be reviewed with its actual +host consumer tests rather than inferred across separate API migrations. -Further **explicit structure reporting and TypeInfo diagnostic replay** work removes ambient -file/pool lookup and preserves diagnostics associated with cached semantic results. That work -remains a separate review boundary. The file/pool ambient-listener API is still present here: -`XvmStructure.log` retains its existing fallback when passed null; the active compilation -boundaries listed above do not. Scoped restoration is another prerequisite for complete host -reporting, not a claim that every compiler-failure or TypeInfo-cache path is already repaired. +The independent [repository read-failure fix](https://github.com/xtclang/xvm/pull/683) supplies +retained I/O causes; this API forwards them only after a required dependency search fails. +[The parser/branch correction](https://github.com/xtclang/xvm/pull/684) supplies the parent-abort, +default-budget and lexical-rollback behavior preserved here. Empty transient-local retention is +an independent utility fix. None of these APIs depends on an LSP implementation or editor. + +Selected-call facts, declaration-only analysis, incomplete-source recovery, cursor completion, +AST copying and the production language server remain later review boundaries. The result here +exposes ordinary compilation progress, not recovered syntax or executable output from invalid code. ### Scoped parser and validation reporting @@ -393,9 +394,9 @@ the same breaking release boundary described above. Parser and resolver destinations use `Reporting` scopes, closed in reverse order with try-with-resources. `NameResolver.getErrorListener()` is null outside `resolve`; callbacks borrow the caller's non-null listener only during that call. These scopes restore destinations on every -exit. They do not make a parser, resolver, AST or listener safe for concurrent use. The lexer still -reports to its original listener; module-name-only scans should construct the parser with an -explicit discard listener if lexical diagnostics are unwanted. +exit. They do not make a parser, resolver, AST or listener safe for concurrent use. Lexical +reports also follow the active attempt, preserving lexical rollback from the parser correction. +Module-name-only scans can supply an explicit discard listener when reports are unwanted. Loop and try/finally label variables retain their context/listener as one immutable `ValidationScope` value. Each statement restores its previous value on normal, early and @@ -410,6 +411,89 @@ and run the existing `manualTests/src/main/x/loop.x` and `exceptions.x` exercise XDK to check lazy label variables and try/finally behavior through code generation and execution. The separate Gradle compiler-consumer test supplies its compiled modules through dependencies. +### Compile a host snapshot and inspect its outcome + +```java +ErrorList reports = new ErrorList(); +ErrorListener operation = ErrorListener.cancellable(reports, cancelled::get); +EmbeddingSupport.Compilation result = embedding.compileModule( + new Source(text, documentName), dependencies, operation); + +if (result.succeeded()) { + ModuleStructure executable = result.module(); +} +if (result.parsed() != null) { + // Inspect ordinary parsed syntax, even if semantic validation subsequently failed. +} +if (result.pool() != null) { + // Inspect structures built before a semantic failure, on the compiling worker. +} +``` + +`Compilation` separates a successful executable module from the file structure and assembled AST +that an attempt reached. Loading/parsing failures can leave no AST; semantic failures can leave a +file and pool without an executable module. Returning these objects does not make them immutable +or safe to share between workers. The host owns their lifetime and must detach any facts it shares. +The existing String/File convenience methods delegate to this path and retain their module/null +or boolean result shapes. A compilation creates a fresh compiler and build repository; hosts must +serialize attempts sharing mutable repositories. + +`compileModule(ModuleInfo, ...)` runs the same source discovery, package assembly and resource +association used by the CLI. A host can override `readSource` for unsaved text, `sourceEntries` +for added/deleted members, and the protected source-identity constructor for a wholly virtual +module. Membership, text and resource identity must describe one consistent snapshot. Construct a +fresh `ModuleInfo` per attempt: it caches its assembled tree. Ordinary file constructors retain +disk discovery and reading. + +Errors stay structured. A non-module/empty source gets a source diagnostic. Failed dependency +resolution can include the original repository path and I/O cause. Successful fallback past an +unreadable file does not fail a valid compilation. Unexpected exceptions and assertions are reported +as internal failures even when an earlier source error was already collected. FATAL reports reach +the host before the CLI console's deliberate exception can stop execution. + +`footprint(result)` reads the supplied compilation pool and repository counts without starting an +interpreter. `ensureRuntimePool()` explicitly initializes the runtime if needed; the deprecated +`getConstantPool()` retains that behavior. A runtime pool is not the pool of a failed compilation. + +### Report diagnostics from cached type information + +Structures no longer store, inherit or discover a listener through an ambient pool. Pass the +operation's listener to `XvmStructure.log` and `TypeConstant.ensureTypeInfo(errors)`. The no-argument +TypeInfo lookup is an explicit silent metadata query. A successful TypeInfo build retains an +immutable diagnostic list; a later explicit lookup replays it to the new listener, including when +it returns the same cached TypeInfo instance. Receiving `ErrorList` instances deduplicate repeated +reports; a raw callback deliberately receives each replay. Cache invalidation causes a fresh build +and replaces the retained reports. + +This matters when speculative work builds a generic type before the selected source use: the +later caller must still hear an ignored-annotation warning. Tests cover source and serialized +library types, silent-first/cache-hit ordering, distinct reporting owners, final selected uses +and partial structures after semantic failure. + +### Cancel obsolete work without manufacturing errors + +`ErrorListener.cancellable` decorates the operation listener, preserving its reporting, state, +branching and merging while polling the host predicate at abort checkpoints. A valid token stream +also polls; cancellation cannot depend on finding a source error. Module-member loading and phase +boundaries retain the same policy. This is cooperative cancellation, not interruption: a host must +keep its predicate true once an attempt becomes obsolete. Other attempts have separate policies. + +The direct consumer suites `EmbeddingDiagnosticsTest`, `EmbeddingSourceSnapshotTest` and +`TypeInfoDiagnosticsTest` use compiled modules supplied by Gradle, with no production adapter. +Run them from the repository root: + +```bash +./gradlew :lang:lsp-server:test \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true \ + --tests 'org.xvm.lsp.adapter.Embedding*Test' \ + --tests org.xvm.lsp.adapter.TypeInfoDiagnosticsTest \ + --rerun-tasks --no-build-cache +``` + +The earlier numerical receipts above describe the original listener migration. Validation of the +combined branch must additionally include these consumer suites and a fresh XDK build; those +historical receipts alone do not validate the enlarged scope. + ## Assembler Status: Suitable for use diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index ac4e9ee350e..ff3da68df71 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -7,11 +7,14 @@ import java.time.Instant; +import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; +import java.util.function.Function; + import org.jetbrains.annotations.NotNull; import org.xvm.asm.ConstantPool; @@ -26,7 +29,24 @@ import org.xvm.asm.constants.TypeConstant; +import org.xvm.compiler.BuildRepository; +import org.xvm.compiler.Compiler; +import org.xvm.compiler.CompilerException; import org.xvm.compiler.InstantRepository; +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; + +import org.xvm.compiler.Token.Id; + +import org.xvm.compiler.ast.Statement; +import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.TypeCompositionStatement; + +import org.xvm.tool.Console; +import org.xvm.tool.Launcher.LauncherException; +import org.xvm.tool.LauncherOptions.CompilerOptions; +import org.xvm.tool.ModuleInfo; +import org.xvm.tool.ModuleInfo.Node; import org.xvm.runtime.template._native.io.xExternalConsole; @@ -34,6 +54,7 @@ import static org.xvm.asm.ErrorListener.NOWHERE; import static org.xvm.asm.ErrorListener.at; +import static org.xvm.util.Severity.ERROR; import static org.xvm.util.Handy.quoted; @@ -45,7 +66,8 @@ * Ecstasy classes. Without configuration, EmbeddingSupport will attempt to locate the core Ecstasy * classes using the "XDK_HOME" OS property. * - *

The methods on EmbeddingSupport itself can be assumed to be thread-safe and concurrent. + *

Hosts must serialize compilations that share this instance's configured repository. Configuration + * must complete before compilation or execution begins. * *

EmbeddingSupport is a singleton because running a module requires the interpreter, and the * interpreter keeps JVM-wide state. When the Connector creates its NativeContainer, the container @@ -83,11 +105,89 @@ private static class Singleton { private static final Object LOCK = new Object(); + private static final Console SILENT_CONSOLE = new Console() { + @Override + public String out(Object value) { + return String.valueOf(value); + } + + @Override + public String err(Object value) { + return String.valueOf(value); + } + }; + private volatile boolean configured; private ModuleRepository cfgRepo; private String cfgInjector; private Connector connector; + /** + * Build a read-only repository over whichever of the given directories exist. + * + * @param dirs the directories, in search order + * + * @return the repository, or null if none of the directories exist + */ + private static ModuleRepository repoOver(File... dirs) { + List list = Arrays.stream(dirs) + .filter(File::isDirectory) + .map(dir -> new DirRepository(dir, true)) + .toList(); + return switch (list.size()) { + case 0 -> null; + case 1 -> list.getFirst(); + default -> new LinkedRepository(list.toArray(ModuleRepository.NO_REPOS)); + }; + } + + /** + * A snapshot of what the compiler is holding on to. + * + *

An embedding host that keeps a compiler alive across many compilations - a language server + * is the obvious one - needs some way to see whether it is accumulating. These are the cheap + * numbers: taking them costs a field read and a repository listing, so a host can log one per + * compilation without measuring itself instead of the compiler. + * + * @param modules how many modules the configured repository offers + * @param constants how many constants are interned in the current pool + * @param invalidations how many times cached type information has been invalidated + * @param heapBytes used heap, which is the JVM's figure and not the compiler's alone + */ + public record Footprint(int modules, int constants, int invalidations, long heapBytes) { + @Override + public String toString() { + return "modules=" + modules + ", constants=" + constants + + ", invalidations=" + invalidations + + ", heap=" + (heapBytes / (1024 * 1024)) + "MB"; + } + } + + /** + * Take a {@link Footprint} without selecting a compilation's pool. This does not initialize + * the runtime; pool counts are zero. Use {@link #footprint(Compilation)} for compiler counts. + * + * @return the snapshot; the counts are zero where nothing has been configured or built yet + */ + public Footprint footprint() { + return footprint(null); + } + + /** + * Take a passive snapshot of a compilation's pool and the configured repository. + * + * @param compilation the compilation to measure, or null for repository and heap counts only + * + * @return the snapshot; pool counts are zero if no file structure was built + */ + public Footprint footprint(Compilation compilation) { + ConstantPool pool = compilation == null ? null : compilation.pool(); + Runtime runtime = Runtime.getRuntime(); + return new Footprint(cfgRepo == null ? 0 : cfgRepo.getModuleNames().size(), pool == null ? 0 : pool.size(), + pool == null ? 0 : pool.getInvalidationCount(), + runtime.totalMemory() - runtime.freeMemory()); + } + /** * @return true if configured * @throws IllegalStateException if not configured @@ -97,9 +197,15 @@ private boolean verifyConfigured() { // attempt to auto-configure String home = System.getenv("XDK_HOME"); if (home != null) { - File dir = new File(new File(home), "lib"); - if (dir.isDirectory()) { - configure(new DirRepository(dir, true), null); + // an XDK keeps its libraries in lib/ and the two modules the compiler bootstraps + // against - the turtle, mack.xtclang.org, and the native bridge - in javatools/. + // Configuring only lib/ produced a repository that could never compile anything: + // every compile failed in prelinkSystemLibraries with "Unable to load module: + // mack.xtclang.org", reported as an internal error with no location + File dirHome = new File(home); + ModuleRepository repo = repoOver(new File(dirHome, "lib"), new File(dirHome, "javatools")); + if (repo != null) { + configure(repo, null); } } @@ -201,14 +307,33 @@ public String getConfiguredInjector() { } /** - * @return the ConstantPool of the core Ecstasy libraries used by the runtime Connector instance - * that is instantiated by EmbeddingSupport + * Obtain the constant pool of the runtime, starting one if it is not running yet. + * + *

This is not the pool a compilation used - that belongs to the {@link Compilation} it + * produced. Asking for this one boots an interpreter: a connector builds a NativeContainer, + * which loads a native template for every core module, so it needs the whole library and not + * just the part the compiler bootstraps against. The old name said "get" and read like an + * accessor. + * + * @return the runtime's constant pool */ - public ConstantPool getConstantPool() { + public ConstantPool ensureRuntimePool() { verifyConfigured(); return ensureConnector().getConstantPool(); } + /** + * Obtain the runtime's constant pool, starting the runtime if necessary. + * + * @return the runtime's constant pool + * @deprecated use {@link #ensureRuntimePool()} to make runtime initialization explicit; + * use {@link Compilation#pool()} for a compilation's pool + */ + @Deprecated + public ConstantPool getConstantPool() { + return ensureRuntimePool(); + } + // ----- compiler support ---------------------------------------------------------------------- /** @@ -223,15 +348,132 @@ public ConstantPool getConstantPool() { * @see ModuleCompiler#compile(String, ModuleRepository, ErrorListener) */ public ModuleStructure compile(String source, ModuleRepository input, @NotNull ErrorListener errs) { + return compile(new Source(source), input, errs); + } + + /** + * Compile a module held in memory, as a named document. + * + *

A diagnostic's identity includes the name of the source it came from, so a host holding + * several documents that are not on disk - an editor's unsaved buffers - has to be able to + * tell them apart. Without a name, two documents with a problem at the same offset produce + * the same identity and a listener that deduplicates discards the second. The name belongs to + * the document rather than to the act of compiling it, which is why this takes a + * {@link Source}: {@code new Source(text, uri)} says it once, where a second String parameter + * would sit next to the first and be silently swappable with it. + * + * @param source the source to compile, carrying whatever name it was created with + * @param input (optional) the module repository to read any required modules from + * @param errs the ErrorListener to log any compiler messages to + * + * @return the resulting ModuleStructure, or null if a compiler error occurred + */ + public ModuleStructure compile(Source source, ModuleRepository input, @NotNull ErrorListener errs) { + return compileModule(source, input, errs).module(); + } + + /** + * The outcome of one compilation, including progress retained after a source error. + * + *

The compiling worker owns the returned mutable compiler objects. A host must copy the + * facts it needs before sharing them with other threads; this record does not detach an AST. + * No field is a promise that a failed compilation is executable. + * + * @param module the executable module on success, or null on failure or cancellation + * @param file the file structure, or null if registration did not finish + * @param ast the assembled source tree, or null if loading or parsing did not finish + */ + public record Compilation(ModuleStructure module, FileStructure file, StatementBlock ast) { + /** + * Wrap a file structure for passive inspection without implying successful compilation. + * + * @param file the structure to inspect + * @return a partial compilation + */ + public static Compilation forFile(FileStructure file) { + return new Compilation(null, requireNonNull(file, "file"), null); + } + + /** @return true if an executable module was produced */ + public boolean succeeded() { + return module != null; + } + + /** @return this attempt's pool, or null if no file structure was built */ + public ConstantPool pool() { + return file == null ? null : file.getConstantPool(); + } + + /** @return the assembled source root, or null if loading or parsing failed */ + public StatementBlock parsed() { + return ast; + } + } + + /** + * Compile a module held in memory, and answer with everything the attempt produced. + * + * @param source the source to compile, carrying whatever name it was created with + * @param input (optional) the module repository to read any required modules from + * @param errs the ErrorListener to log any compiler messages to + * + * @return the outcome; never null, though its parts may be + */ + public Compilation compileModule(Source source, ModuleRepository input, @NotNull ErrorListener errs) { + requireNonNull(source, "source"); + return compileModule(listener -> { + StatementBlock tree = new Parser(source, listener).parseSource(); + return listener.hasSeriousErrors() ? null : tree; + }, input, errs); + } + + /** + * Compile a module's source tree using the same discovery, resource association and parse-tree + * assembly as the CLI. The returned AST includes the member files with their original Sources. + * Hosts may subclass {@link ModuleInfo} and override its protected {@code readSource(File)} + * and {@code sourceEntries(File)} hooks to supply a consistent snapshot of unsaved text and + * source membership. The default provider discovers and reads files from disk. + * + * @param sources a fresh ModuleInfo for this attempt; do not reuse a previously parsed tree + * @param input the optional repository of compiled dependencies + * @param errs the listener for this compilation + * + * @return the outcome, including the linked AST when source loading succeeded + */ + public Compilation compileModule(ModuleInfo sources, ModuleRepository input, @NotNull ErrorListener errs) { + requireNonNull(sources, "sources"); + return compileModule(listener -> { + Node root = sources.getSourceTree(listener); + return root == null ? null : (StatementBlock) root.ast(); + }, input, errs); + } + + private Compilation compileModule(Function parse, + ModuleRepository input, ErrorListener errs) { verifyConfigured(); requireNonNull(errs, "errs"); - return new ModuleCompiler(cfgRepo).compile(source, input, errs); + EmbeddingCompiler compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs); + try { + if (!errs.isAbortDesired()) { + compiler.process(); + } + } catch (LauncherException e) { + // Expected aborts already have diagnostics or a cancellation request. + if (!errs.hasSeriousErrors() && !errs.isAbortDesired()) { + errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed"); + } + } catch (RuntimeException | AssertionError e) { + // An earlier source error must not hide an unexpected compiler failure. + errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed"); + } + return compiler.result(); } /** - * Compile a module that is in a file, against the configured core repository. + * Compile a module that is in a file or directory, against the configured core repository. * - * @param file the module source file + * @param file the location of the module source code on disk, either the module source file + * or the directory containing a single .x file and nested contents thereof * @param input (optional) the module repository to read any required modules from * @param output (optional) the module repository to write any compiled modules to * @param errs the ErrorListener to log any compiler messages to @@ -240,11 +482,172 @@ public ModuleStructure compile(String source, ModuleRepository input, @NotNull E * * @see ModuleCompiler#compile(File, ModuleRepository, ModuleRepository, ErrorListener) */ - public boolean compile(File file, ModuleRepository input, ModuleRepository output, - @NotNull ErrorListener errs) { + public boolean compile(File file, ModuleRepository input, ModuleRepository output, @NotNull ErrorListener errs) { verifyConfigured(); requireNonNull(errs, "errs"); - return new ModuleCompiler(cfgRepo).compile(file, input, output, errs); + ModuleStructure module; + try { + module = compileModule(new ModuleInfo(file, false), input, errs).module(); + } catch (IllegalArgumentException e) { + errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to locate module " + file); + return false; + } + + if (module == null) { + assert errs.hasSeriousErrors() || errs.isAbortDesired(); + return false; + } + + if (output != null) { + try { + output.storeModule(module); + } catch (IOException e) { + errs.error(ERR_INTERNAL, at(module), e, "Unable to store module " + module.getName()); + return false; + } + } + return true; + } + + /** + * Adapter that supplies the source and repositories to the standard compiler pipeline and + * captures its single compiled module instead of writing it to disk. + */ + private static class EmbeddingCompiler + extends org.xvm.tool.Compiler { + private final Function parse; + + private final ModuleRepository inRepo; + private final ModuleRepository coreRepo; + private ModuleStructure module; + private FileStructure file; + private StatementBlock ast; + + /** + * Everything this attempt produced. + * + *

Each part is present when the attempt got that far and null when it did not, which the + * fields already say: the module is only ever assigned on success, and the file structure + * and the AST only once they exist. So there is nothing for the caller to decide, and no + * success flag to pass in and get wrong. + * + * @return the outcome; never null, though its parts may be + */ + Compilation result() { + return new Compilation(module, file, ast); + } + + @Override + public boolean isAbortDesired() { + // Cancellation must also stop between phases; stopping only an individual compiler + // loop would let the following phase run against an unfinished predecessor. + return super.isAbortDesired() || m_errors.isAbortDesired(); + } + + protected EmbeddingCompiler(Function parse, + ModuleRepository input, ModuleRepository core, ErrorListener errs) { + super(CompilerOptions.builder().build(), SILENT_CONSOLE, errs); + + this.parse = parse; + this.inRepo = input; + this.coreRepo = core; + } + + @Override + protected int process() { + ModuleRepository repoLib = ensureLibraryRepo(); + checkErrors("repository setup"); + + prelinkSystemLibraries(repoLib); + checkErrors("system library linking"); + + StatementBlock block; + try { + // The launcher stops the next compilation stage on any error. Parsing can still + // recover within its own stage, subject to the host's budget and cancellation. + ErrorListener errs = ErrorListener.cancellable( + ErrorListener.collecting(this::log), m_errors::isAbortDesired); + block = this.ast = parse.apply(errs); + } catch (CompilerException e) { + return 1; + } + if (checkErrors("source parsing") != 0) { + return 1; + } + + if (block == null) { + error(ERR_MODULE_SOURCE, NOWHERE); + return 1; + } + + if (block.getStatements().isEmpty()) { + block.log(this, ERROR, ERR_MODULE_SOURCE); + return checkErrors("source parsing"); + } + Statement stmt = block.getStatements().getLast(); + if (!(stmt instanceof TypeCompositionStatement stmtModule) || + stmtModule.getCategory().getId() != Id.MODULE) { + stmt.log(this, ERROR, ERR_MODULE_SOURCE); + return checkErrors("source parsing"); + } + + Compiler compiler = new Compiler(stmtModule, this); + FileStructure struct = compiler.generateInitialFileStructure(); + this.file = struct; + if (struct == null || checkErrors("module creation") != 0) { + return 1; + } + + try { + repoLib.storeModule(struct.getModule()); + } catch (IOException e) { + log(ERROR, e, "I/O exception storing module: {}", struct.getModule().getName()); + return 1; + } + + int result = super.compile(List.of(compiler), repoLib); + if (result == 0 && !isAbortDesired()) { + this.module = struct.getModule(); + } + return result; + } + + @Override + protected int compile(List compilers, ModuleRepository repoLib) { + throw new IllegalStateException("This method must not be called"); + } + + @Override + protected void prelinkSystemLibraries(ModuleRepository repository) { + try { + super.prelinkSystemLibraries(repository); + } catch (LauncherException e) { + ModuleCompiler.reportRepositoryReadFailures(repository, m_errors); + throw e; + } + } + + @Override + public void log(ErrorInfo error) { + if (Compiler.MODULE_MISSING.equals(error.getCode())) { + ModuleCompiler.reportRepositoryReadFailures(ensureLibraryRepo(), m_errors); + } + super.log(error); + } + + @Override + protected ModuleRepository configureLibraryRepo(List ignore) { + BuildRepository build = new BuildRepository(); + return inRepo == null || inRepo == coreRepo + ? new LinkedRepository(true, build, coreRepo) + : new LinkedRepository(true, build, inRepo, coreRepo); + } + + @Override + protected int emitModules(List allNodes, ModuleRepository ignore) { + throw new IllegalStateException("This method must not be called"); + } + } /** @@ -592,4 +995,8 @@ static boolean passesArguments(MethodStructure method, EntryPoint entry) { * "%1" - why the module's entry method cannot be run, as the command-line runner reports it */ public static final String ERR_ENTRY_METHOD = "EMB-7"; + /** + * The source does not contain a module declaration. + */ + public static final String ERR_MODULE_SOURCE = "EMB-8"; } diff --git a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java index b3d24cacdbe..99fa748e2c9 100644 --- a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java +++ b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java @@ -45,7 +45,9 @@ *

Unlike {@link EmbeddingSupport}, a ModuleCompiler is an ordinary object that needs no * configuration: compiling does not start the runtime, so it does not depend on the runtime's * JVM-wide state (see {@link EmbeddingSupport}), and any number of ModuleCompiler instances, each - * with its own core repository, can exist in one JVM. Each call to compile is independent. + * with its own core repository, can exist in one JVM. Each call to compile has its own attempt. + * Hosts must serialize calls that share repositories; separate instances do not isolate mutable + * compiler state held by a repository. * *

The compile methods report the same errors as the command-line compiler: every error found in * a compiler stage, with the compilation stopping at the end of the first stage that found any. @@ -144,6 +146,15 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu return true; } + /** + * Forward retained read issues only after the compiler has established a dependency failure. + * Successful fallback and speculative repository misses must not become source errors. + */ + static void reportRepositoryReadFailures(ModuleRepository repository, ErrorListener errs) { + repository.getReadFailures().forEach((file, cause) -> errs.error(ERR_INTERNAL, NOWHERE, + cause, "Dependency search skipped unreadable file " + file)); + } + /** * Adapter that supplies the source and repositories to the standard compiler pipeline and * captures its single compiled module instead of writing it to disk. @@ -229,6 +240,24 @@ private int flushAndCheckErrors(ErrorList errsModule, String context) { return checkErrors(context); } + @Override + protected void prelinkSystemLibraries(ModuleRepository repository) { + try { + super.prelinkSystemLibraries(repository); + } catch (LauncherException e) { + reportRepositoryReadFailures(repository, m_errors); + throw e; + } + } + + @Override + public void log(ErrorInfo error) { + if (Compiler.MODULE_MISSING.equals(error.getCode())) { + reportRepositoryReadFailures(ensureLibraryRepo(), m_errors); + } + super.log(error); + } + /** * The build repository comes first: it receives the module being compiled and, as the * launcher's does, a copy of each module read from the others. The caller's modules are diff --git a/javatools/src/main/java/org/xvm/asm/Annotation.java b/javatools/src/main/java/org/xvm/asm/Annotation.java index 153d3e1dbce..1666c42782d 100644 --- a/javatools/src/main/java/org/xvm/asm/Annotation.java +++ b/javatools/src/main/java/org/xvm/asm/Annotation.java @@ -323,7 +323,7 @@ public boolean validate(ErrorListener errs) { if (getAnnotationType().getExplicitClassFormat() != Component.Format.ANNOTATION) { log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, getAnnotationClass().getValueString()); - fHalt |= ensureErrorListener(errs).isAbortDesired(); + fHalt |= errs.isAbortDesired(); } return fHalt; diff --git a/javatools/src/main/java/org/xvm/asm/ErrorListener.java b/javatools/src/main/java/org/xvm/asm/ErrorListener.java index cd02dc5a9d3..e2f512e27a2 100644 --- a/javatools/src/main/java/org/xvm/asm/ErrorListener.java +++ b/javatools/src/main/java/org/xvm/asm/ErrorListener.java @@ -10,6 +10,7 @@ import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BooleanSupplier; import java.util.function.Consumer; import org.jetbrains.annotations.NotNull; @@ -264,6 +265,82 @@ public String toString() { }; } + /** + * Wrap an operation listener with an independent, cooperatively polled cancellation policy. + * + *

For example, an editor can mark a completion request stale after a new edit without + * emitting a fake compiler error. Every abort query first polls {@code cancelled}; if it + * is false, the wrapped listener's abort policy is queried. Cancellation is not latched: + * a host that requires a permanent stop must keep its predicate true. + * + *

Reports and the other state queries delegate to the wrapped listener. Branching and + * merging preserve the cancellation predicate around the resulting child or parent. The + * wrapper does not interrupt a thread, discard reports, or stop work until a caller checks + * the abort policy. Predicate and listener exceptions propagate. + * + * @param errs the operation listener + * @param cancelled a predicate, cheap and safe to poll on the compiling thread + * + * @return a listener requesting a stop for cancellation or the wrapped listener's policy + */ + @NotNull + static ErrorListener cancellable(@NotNull ErrorListener errs, @NotNull BooleanSupplier cancelled) { + requireNonNull(errs, "errs"); + requireNonNull(cancelled, "cancelled"); + return new ErrorListener() { + @Override + public void log(ErrorInfo err) { + errs.log(err); + } + + @Override + public boolean isAbortDesired() { + return cancelled.getAsBoolean() || errs.isAbortDesired(); + } + + @Override + public boolean hasSeriousErrors() { + return errs.hasSeriousErrors(); + } + + @Override + public boolean hasError(String sCode) { + return errs.hasError(sCode); + } + + @Override + public boolean isSilent() { + return errs.isSilent(); + } + + @Override + public ErrorListener branch(AstNode node) { + // a branch of a cancellable listener is still cancellable: work inside it is just + // as pointless once the answer is not wanted + return cancellable(errs.branch(node), cancelled); + } + + @Override + public ErrorListener merge() { + // and it has to be mergeable, or branching it would quietly throw the branch away: + // the default merge() answers "this", which for a wrapper means the wrapped + // branch is never merged into what it branched from + return cancellable(errs.merge(), cancelled); + } + + @Override + @Nullable + public Silence silenceReason() { + return errs.silenceReason(); + } + + @Override + public String toString() { + return "Cancellable(" + errs + ")"; + } + }; + } + /** * Create a listener that forwards reports to two destinations in order. * diff --git a/javatools/src/main/java/org/xvm/asm/FileStructure.java b/javatools/src/main/java/org/xvm/asm/FileStructure.java index fd523673249..0f91f99da36 100644 --- a/javatools/src/main/java/org/xvm/asm/FileStructure.java +++ b/javatools/src/main/java/org/xvm/asm/FileStructure.java @@ -205,7 +205,6 @@ public FileStructure(FileStructure that) { m_kind = that.m_kind; m_fLinked = that.m_fLinked; - m_errs = that.m_errs; resetModified(); } @@ -1452,29 +1451,6 @@ public boolean validateModuleConstants() { return true; } - @Override - public ErrorListener getErrorListener() { - ErrorListener errs = m_errs; - if (errs == null) { - // getCurrentPool() is an AMBIENT thread-local: it is null on any thread that has not had - // a pool pushed onto it, which is every thread driving the compiler or runtime from - // ordinary Java code. Dereferencing it unconditionally turned this diagnostic accessor - // into an NPE source. Ownership belongs in a parameter, not a thread-local - see the PR - // discussion - but the null guard is the minimal, behaviour-preserving fix. - ConstantPool poolCurrent = ConstantPool.getCurrentPool(); - if (poolCurrent != null && poolCurrent != m_pool) { - errs = poolCurrent.getErrorListener(); - } - } - return errs == null ? ErrorListener.RUNTIME : errs; - } - - @Override - public void setErrorListener(ErrorListener errs) { - // this is not considered a "mutation" of the FileStructure - m_errs = errs; - } - // ----- Object methods ------------------------------------------------------------------------ @Override @@ -1649,10 +1625,4 @@ public boolean isBundle() { */ private transient AssemblerContext m_ctx; - /** - * Holds an ErrorListener explicitly provided to this FileStructure. An absence of an - * ErrorListener implies that either the ErrorListener from the ConstantPool associated with - * the current thread should be used, or failing that, the runtime ErrorListener should be used. - */ - private transient ErrorListener m_errs; } diff --git a/javatools/src/main/java/org/xvm/asm/XvmStructure.java b/javatools/src/main/java/org/xvm/asm/XvmStructure.java index 104c219db03..138f9663ff9 100755 --- a/javatools/src/main/java/org/xvm/asm/XvmStructure.java +++ b/javatools/src/main/java/org/xvm/asm/XvmStructure.java @@ -15,8 +15,6 @@ import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.at; - import static org.xvm.util.Handy.stream; /** @@ -465,41 +463,13 @@ public boolean validate(ErrorListener errs) { /** * Log an error against this structure. * - * @param errs the error list to log to, or null to use the runtime ErrorListener + * @param errs the error listener to log to * @param sev the severity of the error * @param sCode the error code * @param aoParam the parameters of the error */ public void log(ErrorListener errs, Severity sev, String sCode, Object ... aoParam) { - ensureErrorListener(errs).log(sev, sCode, at(this), aoParam); - } - - /** - * Make sure that an error listener is returned to use. - * - * @param errs an error listener, or null - * - * @return the error listener passed in, if it was not null, otherwise the previously specified - * error listener, otherwise the runtime error listener - */ - public ErrorListener ensureErrorListener(ErrorListener errs) { - return errs == null ? getErrorListener() : errs; - } - - /** - * @return the error listener, if provided, otherwise the runtime error listener - */ - public ErrorListener getErrorListener() { - return m_xsParent.getErrorListener(); - } - - /** - * Specify an error listener. - * - * @param errs the error listener - */ - public void setErrorListener(ErrorListener errs) { - m_xsParent.setErrorListener(errs); + errs.log(sev, sCode, ErrorListener.at(this), aoParam); } // ----- debugging support --------------------------------------------------------------------- diff --git a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java index 86abe8aebaf..6eb5b11e8ac 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java @@ -239,7 +239,7 @@ public boolean validate(ErrorListener errs) { TypeConstant type = m_constType; if (type instanceof ImmutableTypeConstant) { log(errs, Severity.WARNING, VE_IMMUTABLE_REDUNDANT); - fHalt |= ensureErrorListener(errs).isAbortDesired(); + fHalt |= errs.isAbortDesired(); } // a service type cannot be immutable diff --git a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java index 4c0f38fc191..a0f786c1d2a 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java +++ b/javatools/src/main/java/org/xvm/asm/constants/PropertyInfo.java @@ -304,7 +304,7 @@ else if (!(bodyAdd.isRO() && typeResult.isA(typeAdd)) for (Annotation annotation : aAnnoBase) { TypeConstant typeAnnoBase = annotation.getAnnotationType(); if (typeAnnoAdd.equals(typeAnnoBase)) { - idProp.log(errs, Severity.WARNING, VE_DUP_ANNOTATION_IGNORED, + that.getIdentity().log(errs, Severity.WARNING, VE_DUP_ANNOTATION_IGNORED, that.getIdentity().getParentConstant().getValueString(), getName(), annoAdd.getAnnotationClass().getValueString()); @@ -312,7 +312,7 @@ else if (!(bodyAdd.isRO() && typeResult.isA(typeAdd)) } if (typeAnnoAdd.isA(typeAnnoBase)) { - idProp.log(errs, Severity.WARNING, VE_SUP_ANNOTATION_IGNORED, + that.getIdentity().log(errs, Severity.WARNING, VE_SUP_ANNOTATION_IGNORED, that.getIdentity().getParentConstant().getValueString(), getName(), typeAnnoAdd.getValueString(), diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java index 5431d8f0687..8d3bf777886 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java @@ -39,6 +39,7 @@ import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; +import org.xvm.asm.ErrorListener.ErrorInfo; import org.xvm.asm.GenericTypeResolver; import org.xvm.asm.MethodStructure; import org.xvm.asm.ModuleStructure; @@ -94,6 +95,7 @@ import static org.xvm.asm.ErrorListener.Silence.CASCADE; import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; +import static org.xvm.asm.ErrorListener.tee; import static org.xvm.javajit.Builder.CD_Class; import static org.xvm.javajit.Builder.CD_Ctx; @@ -1784,12 +1786,27 @@ public TypeConstant widenEnumValueTypes() { // ----- TypeInfo support ---------------------------------------------------------------------- /** - * Obtain the information about this type, resolved from its recursive composition. + * Obtain the information about this type, resolved from its recursive composition, without + * reporting anything about the attempt. + * + *

A TypeConstant is an interned value shared by everything, so when it is asked to build a + * TypeInfo and given no listener it has no caller to ask. It used to walk up to its file + * structure and report to whatever that file was last told - an ambient lookup which, for the + * two thirds of these call sites that run after compilation is over, ended at a listener that + * prints to stdout. Measured across a full XDK build and test run, that listener never + * received anything, and during a compilation the file was parked on a silence anyway. So the + * silence is said here instead of arranged elsewhere. + * + *

This convenience read does not report diagnostics to a host. It is used by speculative and + * metadata queries, including provisional compositions. Diagnostics recorded by a completed + * build remain available for replay; a caller responsible for reporting a selected source use + * must pass its listener to {@link #ensureTypeInfo(ErrorListener)}. Silence here is not evidence + * that every diagnostic encountered is spurious. * * @return the flattened TypeInfo that represents the resolved type of this TypeConstant */ public TypeInfo ensureTypeInfo() { - return ensureTypeInfo(getErrorListener()); + return ensureTypeInfo(silent(CASCADE)); } /** @@ -1818,10 +1835,38 @@ public TypeInfo ensureTypeInfo(ErrorListener errs) { TypeInfo info = getTypeInfo(); if (isComplete(info) && isUpToDate(info)) { + // the cached answer was built for whoever asked first. If that was a speculative + // probe, the probe produced the diagnostics and this caller - which may be the one + // that actually cared - would otherwise hear nothing. Replay them instead, so what a + // caller is told does not depend on the order callers happen to arrive in + replayDiagnostics(errs); return info; } - return ensureTypeInfo(info, errs); + // record what building it has to say, so a later caller can be told the same + ErrorList recorder = new ErrorList(ErrorList.UNLIMITED); + info = ensureTypeInfo(info, tee(errs, recorder)); + + // freeze it. An ErrorList is a listener, which is what the recording needed to be while + // it was being made and not what it should be once it is made: keeping one per + // TypeConstant for the life of the pool holds an error budget, a severity, a dedup set + // and a mutable list, to say something a List already says. Most builds report nothing - + // measured over an XDK build, all of them - and copyOf gives back the shared empty list + // for those, so saying "nothing" costs no allocation at all + m_diagnostics = List.copyOf(recorder.getErrors()); + return info; + } + + /** + * Report the diagnostics that were produced while this type's TypeInfo was built. + * + *

Deduplication in the receiving listener makes this idempotent: a caller that asks twice, or + * two callers sharing a listener, are told once. + * + * @param errs the listener to replay them to + */ + private void replayDiagnostics(ErrorListener errs) { + m_diagnostics.forEach(errs::log); } private synchronized TypeInfo ensureTypeInfo(TypeInfo info, ErrorListener errs) { @@ -2108,6 +2153,16 @@ protected void setTypeInfo(TypeInfo info) { } } + /** + * The diagnostics produced while this type's TypeInfo was built, replayed to later callers + * that get the memoized result; see {@link #replayDiagnostics}. + * + *

Empty until the TypeInfo has been built, and empty afterwards when building it had nothing + * to say - which is the ordinary case. Never null: "not built yet" and "built quietly" are + * the same answer to everyone who reads this, so there is nothing for a third state to say. + */ + private transient volatile List m_diagnostics = List.of(); + /** * @return the invalidation count that this TypeConstant has already processed */ diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index 962ab60481b..0a88fcc9efc 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -12,9 +12,6 @@ import static java.util.Objects.requireNonNull; -import static org.xvm.asm.ErrorListener.Silence.DISCARD; -import static org.xvm.asm.ErrorListener.silent; - /** * A module compiler for Ecstasy code. * @@ -119,13 +116,12 @@ public FileStructure generateInitialFileStructure() { StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, m_errs); if (!mgr.processComplete()) { - if (m_errs.hasSeriousErrors()) { + if (m_errs.hasSeriousErrors() || m_errs.isAbortDesired()) { return null; } throw new CompilerException("failed to create module"); } m_structFile = m_stmtModule.getComponent().getFileStructure(); - m_structFile.setErrorListener(silent(DISCARD)); setStage(Stage.Registered); } @@ -293,7 +289,6 @@ public boolean generateCode(boolean fLastAttempt) { // "purge" the constant pool and do a final validation on the entire module structure m_structFile.reregisterConstants(true); m_structFile.validate(m_errs); - m_structFile.setErrorListener(null); } } } diff --git a/javatools/src/main/java/org/xvm/compiler/Lexer.java b/javatools/src/main/java/org/xvm/compiler/Lexer.java index ab2737d15a2..fd3d89fb33b 100644 --- a/javatools/src/main/java/org/xvm/compiler/Lexer.java +++ b/javatools/src/main/java/org/xvm/compiler/Lexer.java @@ -133,6 +133,10 @@ public boolean hasNext() { @Override public Token next() { + // Valid input must also observe cancellation; no diagnostic may be emitted. + if (m_errorListener.isAbortDesired()) { + throw new CompilerException("Tokenization aborted"); + } if (hasNext()) { return atoken[iNext++]; } @@ -233,6 +237,10 @@ public static boolean isValidQualifiedModule(String sName) { @Override public Token next() { + // Valid input must also observe cancellation; no diagnostic may be emitted. + if (m_errorListener.isAbortDesired()) { + throw new CompilerException("Tokenization aborted"); + } boolean fWhitespaceBefore = m_fWhitespace; Token token = eatToken(); boolean fWhitespaceAfter = eatWhitespace(); diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index 8551113a5d8..661aa983044 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -81,7 +81,6 @@ public boolean isAbortDesired() { }; m_lexer = lexer == null ? new Lexer(source, lexicalReports) : lexer; - // prime the token stream next(); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java index 95a98cfc067..1e990fb3f9f 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java @@ -113,12 +113,9 @@ public boolean isFirstTime() { * {@link Result#RESOLVED} to indicate that the name has been successfully resolved */ public Result resolve(ErrorListener errs) { - // the callbacks this resolution makes - ResolutionCollector.getErrorListener() among them - // - have no listener of their own, so the caller's is held for the duration of the call - // and given back afterwards. It used to be assigned and left, as the comment here admitted, - // which meant the resolver went on holding a listener belonging to a request that had - // finished: a later callback reported to it, and a resolver reused for a second name - // silently replaced the first caller's. + // Direct resolution uses the explicit listener parameter. ResolutionCollector callbacks + // have no such parameter, so lend them the same listener for this call and restore the + // previous destination on every exit (including nested calls and exceptions). try (Reporting.Scope reporting = f_errs.to(errs)) { return resolveStage(errs); } @@ -285,7 +282,7 @@ private Result resolveStage(ErrorListener errs) { // at this point, we have a component (or other identity) to work from, so the next // name has to be relative to that component while (m_sName != null) { - XvmStructure structure = ensurePartiallyResolvedComponent(); + XvmStructure structure = ensurePartiallyResolvedComponent(errs); if (structure == null) { return getResult(); } @@ -366,17 +363,19 @@ private Result resolveStage(ErrorListener errs) { } /** + * @param errs the listener supplied to the current resolution call + * * @return the component that is responsible for resolving the next name or null if an error * has been reported */ - private XvmStructure ensurePartiallyResolvedComponent() { + private XvmStructure ensurePartiallyResolvedComponent(ErrorListener errs) { Component component = m_component; if (m_typeMode == null) { if (component.getFormat().isDeadEnd()) { // for methods (and multi-methods), it is not possible to further resolve the name, // because methods are opaque from the outside, and multi-methods can only be // resolved by analyzing signatures (not names) - m_node.log(f_errs.get(), Severity.ERROR, Compiler.NAME_UNRESOLVABLE, m_sName); + m_node.log(errs, Severity.ERROR, Compiler.NAME_UNRESOLVABLE, m_sName); m_stage = Stage.ERROR; return null; } else { @@ -465,7 +464,7 @@ private XvmStructure ensurePartiallyResolvedComponent() { } if (!type.isTypeOfType()) { - f_errs.get().error(Compiler.NOT_CLASS_TYPE, ErrorListener.at(component), id.getValueString()); + errs.error(Compiler.NOT_CLASS_TYPE, ErrorListener.at(component), id.getValueString()); m_stage = Stage.ERROR; return null; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java index 321193490e2..4a7433f18b8 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java @@ -1167,7 +1167,7 @@ protected Argument resolveReservedName(String sName, Token name, ErrorListener e MethodConstant idMethod = method.getIdentityConstant(); Access access = idMethod.isTopLevel() ? Access.PROTECTED : Access.PRIVATE; TypeConstant typeCtx = pool.ensureAccessTypeConstant(typeThis, access); - TypeInfo infoType = typeCtx.ensureTypeInfo(); + TypeInfo infoType = typeCtx.ensureTypeInfo(errs); MethodInfo infoMethod = infoType.getMethodById(idMethod); SignatureConstant sigSuper = infoMethod == null ? null : infoMethod.getSuper(infoType); diff --git a/javatools/src/main/java/org/xvm/tool/Launcher.java b/javatools/src/main/java/org/xvm/tool/Launcher.java index 3456dfc1fdc..ff8af67e9a5 100644 --- a/javatools/src/main/java/org/xvm/tool/Launcher.java +++ b/javatools/src/main/java/org/xvm/tool/Launcher.java @@ -600,8 +600,9 @@ public String desc() { @Override public void log(ErrorInfo err) { m_sevWorst = worstOf(m_sevWorst, err.getSeverity()); - log(err.getSeverity(), err.toString()); + // Console reporting may throw for FATAL; deliver the original diagnostic first. m_errors.log(err); + log(err.getSeverity(), err.toString()); } /** diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 595f54fb667..9c7a03a2340 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -27,11 +27,7 @@ import org.xvm.compiler.ast.TypeCompositionStatement; import org.xvm.util.ListMap; - -import static org.xvm.asm.ErrorListener.NOWHERE; -import static org.xvm.asm.ErrorListener.Silence.DISCARD; -import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; +import org.xvm.util.Severity; import static org.xvm.asm.Constants.ECSTASY_MODULE; import static org.xvm.asm.Constants.TURTLE_MODULE; @@ -50,6 +46,11 @@ import static org.xvm.util.Handy.removeExtension; import static org.xvm.util.Handy.resolveFile; +import static org.xvm.asm.ErrorListener.NOWHERE; + +import static org.xvm.asm.ErrorListener.Silence.DISCARD; +import static org.xvm.asm.ErrorListener.silent; + /** * Information gleaned about a module from a single specified file. This is a lazily populated * structure, not a point-in-time snapshot; as a result, in the presence of realtime changes @@ -61,6 +62,34 @@ public class ModuleInfo { // ----- constructors -------------------------------------------------------------------------- + /** + * Construct host-supplied source identity without discovering or requiring physical files. + * This supports immutable overlays and proposed source layouts before a file operation. + * Subclasses supply {@link #readSource}, {@link #sourceEntries}, {@link #isSourceTree}, and + * {@link #getResourceDir} from the same snapshot. Ordinary file constructors retain discovery. + * + * @param sourceFile the logical module root, including its source extension + * @param moduleName the module identity obtained from the host's source snapshot + */ + protected ModuleInfo(File sourceFile, String moduleName) { + this.fileSpec = Objects.requireNonNull(sourceFile, "sourceFile").getAbsoluteFile(); + this.moduleName = Objects.requireNonNull(moduleName, "moduleName"); + if (moduleName.isBlank() || !isExplicitSourceFile(fileSpec.getName())) { + throw new IllegalArgumentException("A module name and .x source root are required"); + } + this.sourceFile = fileSpec; + deduce = false; + fileName = removeExtension(fileSpec.getName()); + sourceDir = fileSpec.getParentFile(); + projectDir = sourceDir; + sourceStatus = Status.Exists; + sourceContent = Content.Module; + resourceDir = NoResources; + binaryDir = sourceDir; + binaryStatus = Status.NotExists; + binaryContent = Content.Invalid; + } + /** * Construct the module information from the specified file. * @@ -631,16 +660,57 @@ private Stream collectFiles(File dir, String ext) { // ----- source tree --------------------------------------------------------------------------- + /** + * A source-tree entry. The directory flag describes the input snapshot, so a host can supply + * files and packages that have not yet been saved to disk. + */ + public record SourceEntry(File file, boolean directory) {} + + /** + * List the immediate source-tree entries of a directory. Hosts overriding this together with + * {@link #readSource} must use one consistent input snapshot for the entire compilation. + * Resource lookup remains based on the original disk locations. + * + * @param directory the directory to enumerate + * @return the entries, including directories needed to assemble nested packages + */ + protected List sourceEntries(File directory) { + return listFiles(directory).stream() + .map(file -> new SourceEntry(file, file.isDirectory())) + .toList(); + } + + /** + * Read the text of a discovered source file. Hosts may override this to supply unsaved text + * while retaining the file's identity and resource directory. Use a fresh ModuleInfo for each + * compilation; source trees and their text are cached for this instance. + * + * @param file the discovered source file, resolved by ModuleInfo; hosts should normalize + * overlay keys to the same canonical paths + * + * @return the source characters, owned by the caller + * + * @throws IOException if the source cannot be read + */ + protected char[] readSource(File file) + throws IOException { + return readFileChars(file); + } + /** * When working with a source code tree and given a "module file", produce a source tree of * the desired processing stage. * - * @param errs an optional error listener + * @param errs the reporting and cancellation listener for this attempt * * @return the root {@link Node} of the tree, or null if the ModuleInfo does not know the source * location, or if serious errors occur loading the source tree */ public Node getSourceTree(ErrorListener errs) { + Objects.requireNonNull(errs, "errs"); + if (errs.isAbortDesired()) { + return null; + } if (sourceNode != null) { return sourceNode; } @@ -654,7 +724,6 @@ public Node getSourceTree(ErrorListener errs) { if (isSourceTree()) { assert fileName != null; File subDir = new File(srcDir, fileName); - assert subDir.exists(); DirNode dirNode = new DirNode(/*parent=*/null, subDir, srcFile); dirNode.buildSourceTree(); sourceNode = dirNode; @@ -666,7 +735,7 @@ public Node getSourceTree(ErrorListener errs) { return null; } - sourceNode.parse(); + sourceNode.parse(errs); sourceNode.logErrors(errs); if (errs.hasSeriousErrors() || errs.isAbortDesired()) { return null; @@ -773,6 +842,16 @@ protected String resourcePathPart() { */ public abstract void parse(); + /** + * Parse while observing the host's abort request. The no-argument entry point is retained + * for CLI consumers that drain the node's diagnostics themselves. + */ + public void parse(ErrorListener errs) { + if (!errs.isAbortDesired()) { + parse(); + } + } + /** * Collect the various top-level type names within the module. */ @@ -836,7 +915,7 @@ public ErrorList errs() { * Log any errors accumulated on (or under) this node */ public void logErrors(ErrorListener errs) { - f_errs.getErrors().forEach(errs::log); + f_errs.logTo(errs); f_errs.clear(); } @@ -858,7 +937,11 @@ public void logErrors(ErrorListener errs) { protected ResourceDir m_resdir; /** - * The diagnostic buffer owned by this node from construction onward. + * The errors accumulated on (or under) this node. A Node is an ErrorListener, so it has + * to be able to answer for what it has seen from the moment it exists: the list used to be + * built on first use, which left the three queries above answering "nothing" for a node + * nobody had logged to yet, and left two threads able to build two lists and keep + * different halves of the errors. */ private final ErrorList f_errs = new ErrorList(); } @@ -877,7 +960,6 @@ public class DirNode */ DirNode(DirNode parent, File dir, File fileSrc) { super(parent, dir); - assert dir.isDirectory(); if (fileSrc != null) { m_fileSrc = fileSrc; @@ -890,12 +972,18 @@ public class DirNode */ void buildSourceTree() { File thisDir = file(); - for (File file : listFiles(thisDir)) { + List entries = sourceEntries(thisDir); + var directories = entries.stream().filter(SourceEntry::directory) + .map(entry -> entry.file().getName()).toList(); + var files = entries.stream().filter(entry -> !entry.directory()) + .map(entry -> entry.file().getName()).toList(); + for (SourceEntry entry : entries) { + File file = entry.file(); String name = file.getName(); - if (file.isDirectory()) { + if (entry.directory()) { // if the directory has no corresponding ".x" file, then it is an implied package; // ignore invalid package names; they wouldn't have compiled anyway - if (!new File(thisDir, name + ".x").exists() && Lexer.isValidIdentifier(name)) { + if (!files.contains(name + ".x") && Lexer.isValidIdentifier(name)) { DirNode child = new DirNode(this, file, null); packageNodes().add(child); child.buildSourceTree(); @@ -904,7 +992,7 @@ void buildSourceTree() { // if there is a directory by the same name (minus the ".x"), then recurse to create // a subtree File subDir = new File(thisDir, removeExtension(name)); - if (subDir.exists() && subDir.isDirectory()) { + if (directories.contains(subDir.getName())) { // create a subtree DirNode child = new DirNode(this, subDir, file); packageNodes().add(child); @@ -975,19 +1063,27 @@ protected String resourcePathPart() { */ @Override public void parse() { + parse(silent(DISCARD)); + } + + @Override + public void parse(ErrorListener errs) { + if (errs.isAbortDesired()) { + return; + } if (m_nodeSrc == null) { // provide a default implementation assert m_parent != null; m_nodeSrc = new FileNode(this, "package " + file().getName() + "{}"); } - m_nodeSrc.parse(); + m_nodeSrc.parse(errs); for (FileNode cmpFile : m_mapClzNodes.values()) { - cmpFile.parse(); + cmpFile.parse(errs); } for (DirNode child : m_listPkgNodes) { - child.parse(); + child.parse(errs); } } @@ -1022,7 +1118,7 @@ public void registerNames() { public void registerName(String name, Node node) { if (name != null) { if (children().containsKey(name)) { - error(DUP_NAME, ErrorListener.NOWHERE, name, descriptiveName()); + error(DUP_NAME, NOWHERE, name, descriptiveName()); } else { children().put(name, node); } @@ -1033,7 +1129,7 @@ public void registerName(String name, Node node) { public void linkParseTrees() { Node nodePkg = sourceNode(); if (nodePkg == null) { - error(MISSING_PKG_NODE, ErrorListener.NOWHERE, descriptiveName()); + error(MISSING_PKG_NODE, NOWHERE, descriptiveName()); } else { TypeCompositionStatement typePkg = nodePkg.type(); @@ -1259,9 +1355,9 @@ public char[] content() { } try { - return readFileChars(m_file); + return readSource(m_file); } catch (IOException e) { - error(READ_FAILURE, ErrorListener.NOWHERE, m_file); + error(READ_FAILURE, NOWHERE, m_file); } return new char[0]; @@ -1338,12 +1434,21 @@ public Object resolveResource(String path) { @Override public void parse() { + parse(silent(DISCARD)); + } + + @Override + public void parse(ErrorListener errs) { + if (errs.isAbortDesired()) { + return; + } Source source = source(); try { - m_stmtAST = new Parser(source, this).parseSource(); + m_stmtAST = new Parser(source, ErrorListener.cancellable(this, errs::isAbortDesired)).parseSource(); } catch (CompilerException e) { - if (!hasSeriousErrors()) { - fatal(Parser.FATAL_ERROR, in(source, source.getPosition(), source.getPosition())); + if (!hasSeriousErrors() && !errs.isAbortDesired()) { + log(Severity.FATAL, Parser.FATAL_ERROR, null, source, + source.getPosition(), source.getPosition()); } } } diff --git a/javatools/src/main/resources/errors.properties b/javatools/src/main/resources/errors.properties index 8f0909720ce..c1f9acb2ce5 100644 --- a/javatools/src/main/resources/errors.properties +++ b/javatools/src/main/resources/errors.properties @@ -362,6 +362,7 @@ EMB-1 = Unable to find application module "{0}". EMB-2 = Unable to find application module "{0}" version "{1}". EMB-3 = Unable to create the application container: {1}. Cause: {0} EMB-4 = Unhandled application exception: {0} -EMB-5 = Internal runtime error: {0} +EMB-5 = Internal error: {1}. Cause: {0} EMB-6 = Method "{0}" does not take any parameters; ignoring the specified arguments. EMB-7 = {0} +EMB-8 = Source does not contain a module declaration. diff --git a/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java b/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java new file mode 100644 index 00000000000..ed7cb87a575 --- /dev/null +++ b/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java @@ -0,0 +1,35 @@ +package org.xvm.api; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.FileStructure; +import org.xvm.compiler.BuildRepository; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class EmbeddingFootprintTest { + @Test + public void snapshotDoesNotBootstrapTheRuntime() { + // An empty repository cannot start an interpreter. Measuring it must still work. + var support = new EmbeddingSupport().configure(new BuildRepository(), null); + var footprint = support.footprint(); + assertEquals(0, footprint.modules()); + assertEquals(0, footprint.constants()); + assertEquals(0, footprint.invalidations()); + assertTrue(footprint.heapBytes() > 0); + } + + @Test + public void snapshotMeasuresTheSuppliedCompilation() { + var support = new EmbeddingSupport(); + var file = new FileStructure("Measured"); + var compilation = EmbeddingSupport.Compilation.forFile(file); + file.getConstantPool().ensureStringConstant("from this compilation"); + + var footprint = support.footprint(compilation); + assertEquals(file.getConstantPool().size(), footprint.constants()); + assertEquals(file.getConstantPool().getInvalidationCount(), footprint.invalidations()); + assertEquals(0, support.footprint().constants()); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java b/javatools/src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java new file mode 100644 index 00000000000..34a8525d7eb --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java @@ -0,0 +1,43 @@ +package org.xvm.asm; + +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** A cancellation policy survives delegation without becoming a diagnostic. */ +class ErrorListenerCancellationTest { + @Test + void cancellationSurvivesBranchingAndMerging() { + var cancelled = new AtomicBoolean(); + var errors = new ErrorList(); + var listener = ErrorListener.cancellable(errors, cancelled::get); + var branch = listener.branch(null); + branch.warn("PARSER-03", ErrorListener.NOWHERE, "identifier"); + assertTrue(errors.getErrors().isEmpty()); + cancelled.set(true); + assertTrue(branch.isAbortDesired()); + var merged = branch.merge(); + assertTrue(merged.isAbortDesired()); + assertEquals(1, errors.getErrors().size()); + assertFalse(errors.hasSeriousErrors()); + assertTrue(merged.hasError("PARSER-03")); + cancelled.set(false); + assertFalse(merged.isAbortDesired()); + } + + @Test + void cancellationDoesNotReplaceTheWrappedBudgetOrAffectAnotherAttempt() { + var errors = new ErrorList(1); + var first = ErrorListener.cancellable(errors, () -> false); + first.error("PARSER-03", ErrorListener.NOWHERE, "identifier"); + assertTrue(first.isAbortDesired()); + assertTrue(first.hasSeriousErrors()); + var second = ErrorListener.cancellable(new ErrorList(), () -> false); + assertFalse(second.isAbortDesired()); + assertFalse(second.hasSeriousErrors()); + } +} diff --git a/javatools/src/test/java/org/xvm/asm/FileStructureErrorListenerTest.java b/javatools/src/test/java/org/xvm/asm/FileStructureErrorListenerTest.java index afdbcf2f737..58b9c87f950 100644 --- a/javatools/src/test/java/org/xvm/asm/FileStructureErrorListenerTest.java +++ b/javatools/src/test/java/org/xvm/asm/FileStructureErrorListenerTest.java @@ -2,42 +2,36 @@ import org.junit.jupiter.api.Test; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.xvm.asm.ErrorListener.Site; + +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; -/** - * {@link FileStructure#getErrorListener()} must not depend on an ambient "current pool" being bound - * to the calling thread. - * - *

It consulted {@code ConstantPool.getCurrentPool()} - a thread-local - and dereferenced the - * result unconditionally. That thread-local is simply {@code null} on any thread that has not had a - * pool pushed onto it, which is every thread that drives the compiler or runtime from ordinary Java - * code (an embedding host, a build tool, a test). The accessor is a DIAGNOSTIC accessor, so the - * failure mode was a {@code NullPointerException} thrown from the very code meant to report - * problems.

- */ +import static org.xvm.util.Severity.WARNING; + +/** Structure diagnostics belong to the calling operation, independently of ambient pool state. */ public class FileStructureErrorListenerTest { @Test - public void getErrorListenerWorksWithNoAmbientPoolBound() { - // a plain FileStructure with no explicit ErrorListener set; this test thread has never had a - // pool bound, so getCurrentPool() returns null + public void reportingUsesTheSuppliedListenerWithoutAnAmbientPool() { var file = new FileStructure("test"); - - ErrorListener errs = file.getErrorListener(); - - assertNotNull(errs, "a diagnostic accessor must never return null"); - assertSame(ErrorListener.RUNTIME, errs, - "with no explicit listener and no ambient pool, the runtime listener is the answer"); + var errors = new ErrorList(); + try (var scope = ConstantPool.withPool(null)) { + file.log(errors, WARNING, "VERIFY-75", "x", "Atomic"); + } + assertEquals(1, errors.getErrors().size()); + assertSame(file, ((Site.At) errors.getErrors().getFirst().site()).xs()); } @Test - public void getErrorListenerPrefersAnExplicitlySetListener() { + public void reusingAStructureDoesNotReuseThePreviousRequestsListener() { var file = new FileStructure("test"); - var mine = new ErrorList(10); - - file.setErrorListener(mine); - - assertSame(mine, file.getErrorListener(), - "an explicitly supplied listener must win over any fallback"); + var first = new ErrorList(); + var second = new ErrorList(); + file.log(first, WARNING, "VERIFY-75", "first", "Atomic"); + file.log(second, WARNING, "VERIFY-75", "second", "Atomic"); + assertEquals(1, first.getErrors().size()); + assertEquals(1, second.getErrors().size()); + assertEquals("first", first.getErrors().getFirst().getParams()[0]); + assertEquals("second", second.getErrors().getFirst().getParams()[0]); } } diff --git a/javatools/src/test/java/org/xvm/compiler/CompilerCancellationTest.java b/javatools/src/test/java/org/xvm/compiler/CompilerCancellationTest.java new file mode 100644 index 00000000000..e803de9e32d --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/CompilerCancellationTest.java @@ -0,0 +1,24 @@ +package org.xvm.compiler; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.compiler.ast.TypeCompositionStatement; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Cancelling registration is an ordinary incomplete outcome, not an internal compiler failure. */ +class CompilerCancellationTest { + @Test + void cancelledRegistrationDoesNotThrowOrInventAnError() { + var errors = new ErrorList(); + var syntax = new Parser(new Source("module Cancelled {}"), errors).parseSource(); + var module = (TypeCompositionStatement) syntax.getStatements().getLast(); + var compiler = new Compiler(module, ErrorListener.cancellable(errors, () -> true)); + assertNull(compiler.generateInitialFileStructure()); + assertTrue(errors.getErrors().isEmpty()); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java index 97da0b03d14..9324df80465 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java @@ -15,16 +15,23 @@ /** A resolver borrows its caller's listener only while making resolution callbacks. */ class NameResolverReportingTest { @Test - void callbackReportsToCallerAndReleasesIt() { + void directResolutionAndCallbacksReportToTheSameCallerAndReleaseIt() { var errors = new ErrorList(); var resolver = new NameResolver(new ResolvingNode((name, access, collector) -> { assertSame(errors, collector.getErrorListener()); - collector.getErrorListener().error("TEST", ErrorListener.NOWHERE); + collector.getErrorListener().error("CALLBACK", ErrorListener.NOWHERE); return ResolutionResult.ERROR; - }), "missing"); + }) { + @Override + protected ImportStatement resolveImportBySingleName(String name, ErrorListener errs) { + assertSame(errors, errs); + errs.error("DIRECT", ErrorListener.NOWHERE); + return null; + } + }, "missing"); assertNull(resolver.getErrorListener()); assertEquals(NameResolver.Result.ERROR, resolver.resolve(errors)); - assertEquals(1, errors.getErrors().size()); + assertEquals(2, errors.getErrors().size()); assertNull(resolver.getErrorListener()); } diff --git a/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java b/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java index 7276e462670..08b7c14881d 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java @@ -1,17 +1,27 @@ package org.xvm.compiler.ast; -import java.lang.reflect.Field; import java.util.List; +import java.util.stream.Stream; + import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; +import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; +import org.xvm.asm.FileStructure; +import org.xvm.asm.Register; + +import org.xvm.asm.constants.TypeConstant; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; @@ -20,37 +30,52 @@ /** Callback context and listener state must not survive a failed validation. */ class ValidationScopeTest { - @Test - void loopScopesReleaseTheirListenersAfterExceptionalExit() throws Exception { - var condition = new AssignmentStatement(null, token(Id.COLON), null); - List.of( - new ForStatement(token(Id.FOR), List.of(), List.of(), List.of(), block()), - new ForEachStatement(token(Id.FOR), condition, block()), - new WhileStatement(token(Id.WHILE), List.of(), block())).forEach(statement -> { - var field = scopeField(statement, "m_labelVars"); - var failure = new IllegalStateException("nested validation failed"); - var context = new Context(null, false) { - @Override - public Context enter() { return this; } - @Override - public Context enterIf() { return fail(); } - @Override - public Context enterLoop() { return fail(); } - - private Context fail() { - assertNotNull(readScope(field, statement), "failure must occur with an active scope"); - throw failure; - } - }; - assertSame(failure, assertThrows(IllegalStateException.class, - () -> statement.validate(context, new ErrorList()))); - assertNull(readScope(field, statement), statement.getClass().getSimpleName()); - assertThrows(IllegalStateException.class, statement::ensureValidationContext); - }); + @ParameterizedTest + @MethodSource("loopStatements") + void loopCallbacksReturnToTheOuterContextAndListenerAfterNestedFailure(Statement statement) { + var pool = new FileStructure("Test").getConstantPool(); + var label = new LabeledStatement(new Token(0, 1, Id.IDENTIFIER, "loop"), statement) { + @Override + protected ConstantPool pool() { return pool; } + }; + label.adopt(statement); + var outerErrors = new ErrorList(); + var innerErrors = new ErrorList(); + var innerFailure = new IllegalStateException("nested validation failed"); + var outerFailure = new IllegalStateException("outer validation failed"); + var innerContext = new LoopContext() { + @Override + void validateBody() { + assertSame(this, statement.ensureValidationContext()); + throw innerFailure; + } + }; + var outerContext = new LoopContext() { + @Override + void validateBody() { + assertSame(this, statement.ensureValidationContext()); + assertSame(innerFailure, assertThrows(IllegalStateException.class, + () -> statement.validate(innerContext, innerErrors))); + assertSame(this, statement.ensureValidationContext()); + + // Lazy label creation must register and report against this outer validation, + // not the failed nested call. Neither callback state nor its field names are + // inspected: the context records the registration and the listener the report. + assertNotNull(label.getLabelVar(this, "count")); + assertEquals(1, registrations); + assertEquals(0, innerContext.registrations); + assertEquals(1, outerErrors.getErrors().size()); + assertTrue(innerErrors.getErrors().isEmpty()); + throw outerFailure; + } + }; + assertSame(outerFailure, assertThrows(IllegalStateException.class, + () -> statement.validate(outerContext, outerErrors))); + assertThrows(IllegalStateException.class, statement::ensureValidationContext); } @Test - void finallyScopeAndStatementContextAreRestoredAfterFailure() throws Exception { + void finallyScopeAndStatementContextAreRestoredAfterFailure() { var failure = new IllegalStateException("finally failed"); var body = new StatementBlock(List.of()) { @Override @@ -60,7 +85,8 @@ void finallyScopeAndStatementContextAreRestoredAfterFailure() throws Exception { @Override protected Statement validateImpl(Context ctx, ErrorListener errs) { var owner = (TryStatement) getParent(); - assertNotNull(readScope(scopeField(owner, "m_validatingFinally"), owner)); + assertTrue(owner.hasLabelVar("exception")); + assertSame(ctx, ensureValidationContext()); throw failure; } }; @@ -69,7 +95,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { statement.adopt(catchall); assertSame(failure, assertThrows(IllegalStateException.class, () -> statement.validate(new Context(null, false), new ErrorList()))); - assertNull(readScope(scopeField(statement, "m_validatingFinally"), statement)); + assertFalse(statement.hasLabelVar("exception")); assertThrows(IllegalStateException.class, statement::ensureValidationContext); assertThrows(IllegalStateException.class, catchall::ensureValidationContext); } @@ -82,7 +108,6 @@ void infiniteForEarlyReturnReleasesValidationState() { var errors = new ErrorList(); assertNull(statement.validate(new Context(null, false), errors)); assertTrue(errors.hasSeriousErrors(), "the empty infinite loop is rejected"); - assertNull(readScope(scopeField(statement, "m_labelVars"), statement)); assertThrows(IllegalStateException.class, statement::ensureValidationContext); } @@ -92,25 +117,40 @@ void validationScopeCannotContainMissingContextOrListener() { assertThrows(NullPointerException.class, () -> new ValidationScope(new Context(null, false), null)); } + private static Stream loopStatements() { + var condition = new AssignmentStatement(null, token(Id.COLON), null); + return Stream.of( + new ForStatement(token(Id.FOR), List.of(), List.of(), List.of(), block()), + new ForEachStatement(token(Id.FOR), condition, block()), + new WhileStatement(token(Id.WHILE), List.of(), block())); + } + private static StatementBlock block() { return new StatementBlock(List.of()); } private static Token token(Id id) { return new Token(0, 1, id); } - // Inspect ownership without adding public diagnostic-state accessors to AST nodes. - private static Field scopeField(Statement statement, String name) { - try { - var field = statement.getClass().getDeclaredField(name); - field.setAccessible(true); - return field; - } catch (ReflectiveOperationException e) { - throw new AssertionError(e); - } - } + /** Inject a failure at a loop callback, and observe lazy-variable registration. */ + private abstract static class LoopContext extends Context { + LoopContext() { super(null, false); } - private static ValidationScope readScope(Field field, Statement statement) { - try { - return (ValidationScope) field.get(statement); - } catch (IllegalAccessException e) { - throw new AssertionError(e); + @Override + public Context enter() { return this; } + @Override + public Context enterIf() { validateBody(); return this; } + @Override + public Context enterLoop() { validateBody(); return this; } + @Override + public Register createRegister(TypeConstant type, String name) { + return new Register(type, name, 0); } + @Override + public void registerVar(Token name, Register register, ErrorListener errs) { + registrations++; + errs.error("TEST", ErrorListener.NOWHERE); + errs.merge(); + } + + abstract void validateBody(); + + int registrations; } } diff --git a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java index 7566dcd405b..da3b9e770c6 100644 --- a/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java +++ b/javatools/src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java @@ -1,6 +1,7 @@ package org.xvm.tool; import org.junit.jupiter.api.Test; +import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; import org.xvm.tool.Launcher.LauncherException; import org.xvm.tool.LauncherOptions.CompilerOptions; @@ -30,6 +31,18 @@ */ class LauncherErrorHandlingTest { + @Test + void fatalDiagnosticReachesHostBeforeConsoleAborts() { + var errors = new ErrorList(); + var compiler = new TestCompiler(CompilerOptions.builder().build(), new TestConsole(), errors); + + assertThrows(LauncherException.class, + () -> compiler.fatal("PARSER-03", ErrorListener.NOWHERE, "identifier")); + assertEquals(1, errors.getErrors().size()); + assertEquals("PARSER-03", errors.getErrors().getFirst().getCode()); + assertEquals(FATAL, errors.getErrors().getFirst().getSeverity()); + } + /** * Custom console that captures log output for testing. * Implements Console with output capturing for verification. diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt new file mode 100644 index 00000000000..8b8538ca226 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt @@ -0,0 +1,178 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.DirRepository +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.FileRepository +import org.xvm.asm.FileStructure +import org.xvm.asm.ModuleRepository +import org.xvm.compiler.Source +import org.xvm.tool.ModuleInfo +import java.nio.ByteBuffer +import java.nio.file.Files +import java.nio.file.Path + +class EmbeddingDiagnosticsTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `a non-module root reports its source diagnostic instead of an internal failure`(fileInput: Boolean) { + CompilerTestSupport.configure() + val text = "class NotModule {}" + val file = directory.resolve("NotModule.x").toFile().canonicalFile + val errors = ErrorList() + val support = EmbeddingSupport.instance() + val result = + if (fileInput) { + file.writeText(text) + support.compileModule(ModuleInfo(file, false), null, errors) + } else { + support.compileModule(Source(text, file.path), null, errors) + } + assertThat(result.succeeded()).isFalse() + assertThat(errors.errors.map { it.code }) + .containsExactly(EmbeddingSupport.ERR_MODULE_SOURCE) + val site = errors.errors.single().site() as ErrorListener.Site.In + assertThat(site.source().fileName).isEqualTo(file.path) + assertThat(site.source().toString(site.lPosStart(), site.lPosEnd())).isEqualTo(text) + } + + @ParameterizedTest + @ValueSource(strings = ["module FileAudit {}", "module FileAudit { void broken( { }"]) + fun `discovery with an unreadable adjacent binary preserves source compilation diagnostics`(text: String) { + CompilerTestSupport.configure() + val file = directory.resolve("FileAudit.x").toFile().canonicalFile + file.writeText(text) + directory.resolve("FileAudit.xtc").toFile().writeText("corrupt") + val sources = ModuleInfo(file, false) + assertThat(sources.qualifiedModuleName).isEqualTo("FileAudit") + assertThat(sources.moduleVersion).isNull() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(sources, null, errors) + val valid = text == "module FileAudit {}" + assertThat(result.succeeded()).isEqualTo(valid) + assertThat(errors.hasSeriousErrors()).isEqualTo(!valid) + assertThat(errors.errors).noneMatch { it.code == "EMB-5" } + errors.errors.forEach { + assertThat(it.code).startsWith("PARSER-") + assertThat((it.site() as ErrorListener.Site.In).source().fileName).isEqualTo(file.path) + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `an incompatible candidate does not poison fallback or ordinary source diagnostics`(singleFile: Boolean) { + CompilerTestSupport.configure() + val file = directory.resolve("Old.xtc").toFile() + FileStructure("Old").writeTo(file) + val bytes = file.readBytes() + ByteBuffer.wrap(bytes).putInt(2 * Int.SIZE_BYTES, FileStructure.getToolMinorVersion() - 1) + Files.write(file.toPath(), bytes) + val repository: ModuleRepository = + if (singleFile) FileRepository(file, true) else DirRepository(directory.toFile(), true) + val support = EmbeddingSupport.instance() + + for (text in listOf("module Test {}", "module Test { void broken( { }")) { + val errors = ErrorList() + val result = support.compileModule(Source(text), repository, errors) + assertThat(result.succeeded()).isEqualTo(text == "module Test {}") + assertThat(repository.readFailures).containsKey(file) + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + + val errors = ErrorList() + val result = + support.compileModule( + Source("module Missing { package lib import Unavailable; }"), + repository, + errors, + ) + assertThat(result.succeeded()).isFalse() + assertThat(errors.errors.map { it.code }).contains("COMPILER-24", "EMB-5") + assertThat(errors.errors.filter { it.code == "EMB-5" }.map { it.message }) + .anyMatch { it.contains(file.path) && it.contains("Unsupported .xtc version") } + } + + @Test + fun `cancellation stops a valid token stream before parsing the rest of the document`() { + CompilerTestSupport.configure() + val errors = ErrorList() + var reads = 0 + val text = + "module Cancelled { " + (1..4000).joinToString(" ") { "Int value$it = $it;" } + " }" + val source = + object : Source(text) { + override fun next(): Char { + reads++ + return super.next() + } + } + val result = + EmbeddingSupport + .instance() + .compileModule(source, null, ErrorListener.cancellable(errors) { reads >= 80 }) + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(reads).isBetween(80, 120) + assertThat(errors.errors).isEmpty() + } + + @Test + fun `unexpected compiler failures are not hidden by an earlier source error`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = + object : Source("module Broken {}", "file:///Broken.x") { + override fun next(): Char { + errors.error( + "PARSER-03", + ErrorListener.`in`(this, 0, 1), + "injected source error", + ) + throw IllegalStateException("injected compiler failure") + } + } + val result = EmbeddingSupport.instance().compileModule(source, null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.errors.map { it.code }).contains("PARSER-03", "EMB-5") + } + + @Test + fun `cancellation before compilation does not read source or invent a diagnostic`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = + object : Source("module Unneeded {}") { + override fun next(): Char = error("cancelled compilation must not read source") + } + val result = + EmbeddingSupport + .instance() + .compileModule(source, null, ErrorListener.cancellable(errors) { true }) + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `an empty editor buffer reports a source problem without an internal failure`() { + CompilerTestSupport.configure() + for (text in listOf("", "// no module yet")) { + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule(Source(text, "file:///Empty.x"), null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingSourceSnapshotTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingSourceSnapshotTest.kt new file mode 100644 index 00000000000..1b1cbf6f35c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingSourceSnapshotTest.kt @@ -0,0 +1,149 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.tool.ModuleInfo +import org.xvm.tool.ResourceDir +import java.io.File +import java.io.IOException +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicBoolean + +/** Direct host tests: no IDE, adapter, URI service or compiler-owned state in the input snapshot. */ +class EmbeddingSourceSnapshotTest { + @TempDir lateinit var directory: Path + + @Test + fun `unsaved roots and members compile without creating files`() { + val root = directory.resolve("Project.x").toFile().canonicalFile + val member = directory.resolve("Project/pkg/Added.x").toFile().canonicalFile + val errors = ErrorList() + val result = compile(snapshot(root, mapOf(root to "module Project {}", member to "class Added {}")), errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(result.module().getChild("pkg").getChild("Added")).isNotNull() + assertThat(result.parsed()).isNotNull() + assertThat(root.exists()).isFalse() + assertThat(member.exists()).isFalse() + } + + @Test + fun `a fresh snapshot controls membership instead of cached or disk contents`() { + val root = directory.resolve("Project.x").toFile().canonicalFile + val member = directory.resolve("Project/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("this disk text must not be parsed") + member.writeText("class Child { void broken( }") + val errors = ErrorList() + val first = compile(snapshot(root, mapOf(root to "module Project {}", member to "class Child {}")), errors) + assertThat(first.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(first.module().getChild("Child")).isNotNull() + val second = compile(snapshot(root, mapOf(root to "module Project {}")), ErrorList()) + assertThat(second.succeeded()).isTrue() + assertThat(second.module().getChild("Child")).isNull() + assertThat(first.module().getChild("Child")).isNotNull() + assertThat(member.readText()).contains("broken") + } + + @Test + fun `member diagnostics retain their source identity and correction is independent`() { + val root = directory.resolve("Project.x").toFile().canonicalFile + val member = directory.resolve("Project/Child.x").toFile().canonicalFile + val errors = ErrorList() + val failed = compile(snapshot(root, mapOf(root to "module Project {}", member to "class Child { void broken( }")), errors) + assertThat(failed.succeeded()).isFalse() + assertThat(errors.errors).isNotEmpty() + assertThat(errors.errors.map { (it.site() as ErrorListener.Site.In).source().fileName }).containsOnly(member.path) + val fixed = compile(snapshot(root, mapOf(root to "module Project {}", member to "class Child {}")), ErrorList()) + assertThat(fixed.succeeded()).isTrue() + } + + @Test + fun `resource-only packages survive source snapshot assembly`() { + val root = directory.resolve("Project.x").toFile().canonicalFile + val resources = directory.resolve("Project/spare").toFile().canonicalFile + resources.mkdirs() + File(resources, "data.txt").writeText("resource content") + val errors = ErrorList() + val sources = snapshot(root, mapOf(root to "module Project {}"), setOf(resources), ResourceDir(root.parentFile)) + val result = compile(sources, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val spare = sources.getSourceTree(ErrorList()) as ModuleInfo.DirNode + assertThat( + spare + .packageNodes() + .single() + .resourceDir() + .getByName("data.txt"), + ).isNotNull() + } + + @Test + fun `cancellation during source loading produces no parser fatal`() { + val root = directory.resolve("Project.x").toFile().canonicalFile + val member = directory.resolve("Project/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Project {}") + member.writeText("class Child {}") + val cancelled = AtomicBoolean() + val sources = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray = + super.readSource(file).also { + if (file == member) cancelled.set(true) + } + } + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(sources, null, ErrorListener.cancellable(errors, cancelled::get)) + assertThat(cancelled.get()).isTrue() + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(errors.errors).isEmpty() + } + + private fun compile( + sources: ModuleInfo, + errors: ErrorList, + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + return EmbeddingSupport.instance().compileModule(sources, null, errors) + } + + private fun snapshot( + root: File, + text: Map, + extraDirectories: Set = emptySet(), + resources: ResourceDir = ResourceDir(emptyList()), + ): ModuleInfo { + val boundary = File(root.parentFile, root.nameWithoutExtension) + val entries = linkedMapOf>() + + fun addParents(file: File) { + var current = file + while (current != boundary) { + entries.getOrPut(current.parentFile) { linkedMapOf() }[current] = ModuleInfo.SourceEntry(current, true) + current = current.parentFile + } + } + text.keys.filter { it != root }.forEach { file -> + entries.getOrPut(file.parentFile) { linkedMapOf() }[file] = ModuleInfo.SourceEntry(file, false) + addParents(file.parentFile) + } + extraDirectories.forEach(::addParents) + val snapshot = text.toMap() + return object : ModuleInfo(root, "Project") { + override fun isSourceTree(): Boolean = true + + override fun getResourceDir(): ResourceDir = resources + + override fun sourceEntries(directory: File): List = entries[directory]?.values?.toList().orEmpty() + + override fun readSource(file: File): CharArray = + snapshot[file]?.toCharArray() ?: throw IOException("Missing snapshot source: $file") + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt new file mode 100644 index 00000000000..63f7d253820 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt @@ -0,0 +1,292 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorList.UNLIMITED +import org.xvm.asm.FileStructure +import org.xvm.asm.ModuleRepository +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.Source +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +/** + * Diagnostics raised while a TypeInfo is being assembled reach the listener the caller supplied, + * and a later caller that gets the memoized TypeInfo is told the same thing. + * + * These live here rather than in javatools because they need a compiled core library, and javatools + * is what compiles it - a test there would either skip on a clean build or pass only on the + * leftovers of a previous one. This module is downstream of the compiler and already requires a + * built XDK, supplied by Gradle as a declared test dependency. + * + * The replay is the part worth pinning. A TypeInfo is built once and memoized, so which caller + * triggers the build is an accident of order: if a speculative one did, a later caller that + * actually cared would hear nothing at all unless the diagnostics were kept and replayed. + */ +class TypeInfoDiagnosticsTest { + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `generic warning survives source and serialized dependency use`(external: Boolean) { + val base = + """ + class Base { @Atomic Int count = 1; Element echo(Element value) = value; } + """.trimIndent() + val repository = if (external) dependency("module Library { $base }") else null + val declarations = if (external) "package lib import Library; import lib.Base;" else base + val errors = ErrorList(UNLIMITED) + val result = + compile( + """ + module GenericUse { + $declarations + class Derived extends Base { @Atomic @Override Int count = 2; } + String run() { + Derived text = new Derived(); + Derived number = new Derived(); + return text.echo("ok") + number.echo(1); + } + } + """.trimIndent(), + errors, + repository, + ) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors.map { it.code }).containsOnly("VERIFY-75").isNotEmpty() + } + + @Test + fun `a silent generic instantiation cannot consume the warning on a dependency override`() { + val repository = + dependency( + """ + module Library { + class Base { @Atomic Int count = 1; } + } + """.trimIndent(), + ) + val errors = ErrorList(UNLIMITED) + val result = + compile( + """ + module Consumer { + package lib import Library; + class Derived extends lib.Base { @Atomic @Override Int count = 2; } + } + """.trimIndent(), + errors, + repository, + ) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val module = requireNotNull(result.module()) + val pool = module.constantPool + val derived = (module.getChild("Derived") as ClassStructure).identityConstant.type + val concrete = pool.ensureParameterizedTypeConstant(derived, pool.typeString()) + val cached = concrete.ensureTypeInfo() + val later = ErrorList(UNLIMITED) + assertThat(concrete.ensureTypeInfo(later)).isSameAs(cached) + assertThat(later.errors.map { it.code }).contains("VERIFY-75") + } + + private fun dependency(source: String): ModuleRepository { + val errors = ErrorList(UNLIMITED) + val result = compile(source, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val bytes = ByteArrayOutputStream().also { result.file().writeTo(it) }.toByteArray() + return BuildRepository().apply { + storeModule(FileStructure(ByteArrayInputStream(bytes)).module) + } + } + + @ParameterizedTest + @ValueSource(strings = ["array", "assignment", "constructor", "property"]) + fun `validated uses retain the type warning without cascades`(operation: String) { + val body = + when (operation) { + "array" -> "Base target = new Derived(); Int value = target[0];" + "assignment" -> "Base target = new Derived(); target += target;" + "constructor" -> "Derived target = new Derived(2);" + "property" -> "Derived target = new Derived(); Ref ref = target.&count;" + else -> error(operation) + } + val errors = ErrorList(UNLIMITED) + val result = + compile( + """ + module TypeInfoUses { + class Base { + construct(Int seed = 1) { count = seed; } + @Atomic Int count = 1; + @Op("[]") Int getElement(Int index) = count; + @Op("+") Base add(Base other) = this; + } + class Derived(Int seed = 1) extends Base(seed) { + @Atomic @Override Int count = 2; + } + void run() { $body } + } + """.trimIndent(), + errors, + ) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors.map { it.code }).containsExactly("VERIFY-75") + } + + @Test + fun `an unmatched Override is reported while the TypeInfo is assembled`() { + val errs = ErrorList(UNLIMITED) + compile(OVERRIDE_WITH_NO_SUPER, errs) + + assertThat(errs.getErrors().map { it.code }) + .`as`("a class whose @Override has nothing to override: %s", errs.getErrors()) + .contains("VERIFY-70") + } + + @Test + fun `a clean type reports nothing`() { + val errs = ErrorList(UNLIMITED) + compile(CLEAN, errs) + + assertThat(errs.getErrors()).isEmpty() + } + + /** + * The memoized result carries what building it said. The first caller here is the compilation + * itself; the second is a later one with a listener of its own, and must still be told. + * + * Serious errors can force a rebuild, so this case checks delivery rather than cache reuse. The + * warning-only test below proves replay from the same cached TypeInfo instance. + */ + @Test + fun `a later caller is told what building the TypeInfo said`() { + val errs = ErrorList(UNLIMITED) + val result = compile(OVERRIDE_WITH_NO_SUPER, errs) + + // the compilation failed, so there is no module - but the structures it raised the + // diagnostic against are still reachable, which is what compileModule exists for + assertThat(result.succeeded()).isFalse() + val pool = requireNotNull(result.pool()) { "a failed compilation still interned its types" } + + val type: TypeConstant = + requireNotNull( + pool.constants + .filterIsInstance() + .firstOrNull { it.name == "Foo" } + ?.type, + ) { + "the class the diagnostic was about should be in the pool" + } + + // a later caller, with a listener of its own, is told the same thing + val later = ErrorList(UNLIMITED) + type.ensureTypeInfo(later) + + assertThat(later.getErrors().map { it.code }) + .`as`("what the first caller was told, heard again: %s", later.getErrors()) + .contains("VERIFY-70") + } + + private fun compile( + source: String, + errs: ErrorList, + repository: ModuleRepository? = null, + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + return EmbeddingSupport + .instance() + .compileModule(Source(source, "file:///Test.x"), repository, errs) + } + + /** + * A warning raised while a TypeInfo is assembled reaches the caller. + * + * This one is worth its own test because master loses it. Compiling this source there reports + * nothing at all - the compiler parks the file on a silence for the duration of a compilation, + * and the warning goes into it - so the annotation is silently ignored and the author is never + * told. + */ + @Test + fun `a duplicated property annotation is reported, which master swallows`() { + val errs = ErrorList(UNLIMITED) + compile(DUPLICATE_ANNOTATION, errs) + + assertThat(errs.getErrors().map { it.code }) + .`as`("the derived property re-declares @Atomic: %s", errs.getErrors()) + .contains("VERIFY-75") + assertThat(errs.hasSeriousErrors()) + .`as`("a warning, so the source still compiles") + .isFalse() + } + + /** + * And a later caller hears it too, from the recording rather than from a second build. + * + * This is the case the memoized diagnostics exist for, and the only one where they can fire: a + * build that reports a warning leaves the TypeInfo cached, because only a serious error makes + * the compiler refuse to cache one. So the next caller takes the cached path - and without the + * recording would be told nothing, purely because somebody else asked first. + */ + @Test + fun `a later caller hears a warning recorded by the build that cached the TypeInfo`() { + val errs = ErrorList(UNLIMITED) + val result = compile(DUPLICATE_ANNOTATION, errs) + + assertThat(result.succeeded()).`as`("a warning does not fail the compilation").isTrue() + val pool = requireNotNull(result.pool()) + + val type = + requireNotNull( + pool.constants + .filterIsInstance() + .firstOrNull { it.name == "Derived" } + ?.type, + ) { + "the class the warning was about should be in the pool" + } + + val later = ErrorList(UNLIMITED) + val cached = type.ensureTypeInfo(later) + val next = ErrorList(UNLIMITED) + assertThat(type.ensureTypeInfo(next)).isSameAs(cached) + assertThat(next.getErrors().map { it.code }) + .containsExactlyElementsOf(later.getErrors().map { it.code }) + + assertThat(later.getErrors().map { it.code }) + .`as`("replayed to a caller that did not trigger the build: %s", later.getErrors()) + .contains("VERIFY-75") + } + + private companion object { + val OVERRIDE_WITH_NO_SUPER = + """ + module TestOverride { + class Foo { + @Override void nope() {} + } + } + """.trimIndent() + + val DUPLICATE_ANNOTATION = + """ + module TestDuplicateAnnotation { + class Base { @Atomic Int x = 1; } + class Derived extends Base { @Atomic @Override Int x = 2; } + } + """.trimIndent() + + val CLEAN = + """ + module TestClean { + class Foo { + void fine() {} + } + } + """.trimIndent() + } +} From 9f7760567b81d56ca15feccfbc1ffaad1ae60fe0 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:23 +0100 Subject: [PATCH 006/324] Expose compilation-owned syntax and resolved semantic facts - Preserve compiler types through fitting and binary AST emission. - Expose compilation-owned syntax and resolved semantic facts. The tests check compiled code without the binary AST, which master removed: their CompilerTestSupport.execute runs the serialized module instead. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../java/org/xvm/api/EmbeddingSupport.java | 173 +++++++++++-- .../org/xvm/asm/constants/MethodBody.java | 6 +- .../org/xvm/asm/constants/MethodInfo.java | 24 ++ .../main/java/org/xvm/compiler/Compiler.java | 45 ++-- .../org/xvm/compiler/InitializerBinding.java | 85 ++++++ .../org/xvm/compiler/InvocationBinding.java | 244 ++++++++++++++++++ .../compiler/ast/AnonymousClassBindings.java | 54 ++++ .../java/org/xvm/compiler/ast/AstNode.java | 126 ++++++++- .../org/xvm/compiler/ast/CompositionNode.java | 9 +- .../java/org/xvm/compiler/ast/Context.java | 14 +- .../java/org/xvm/compiler/ast/Expression.java | 11 +- .../org/xvm/compiler/ast/ImportStatement.java | 22 ++ .../compiler/ast/InvocationExpression.java | 64 ++++- .../org/xvm/compiler/ast/LambdaBindings.java | 76 ++++++ .../xvm/compiler/ast/LambdaExpression.java | 42 ++- .../ast/MethodDeclarationStatement.java | 36 ++- .../org/xvm/compiler/ast/NameExpression.java | 26 ++ .../org/xvm/compiler/ast/NameResolver.java | 14 + .../xvm/compiler/ast/NamedTypeExpression.java | 46 +++- .../org/xvm/compiler/ast/NewExpression.java | 99 +++++-- .../java/org/xvm/compiler/ast/Parameter.java | 25 +- .../ast/PropertyDeclarationStatement.java | 27 +- .../java/org/xvm/compiler/ast/StageMgr.java | 47 +++- .../java/org/xvm/compiler/ast/Statement.java | 5 + .../org/xvm/compiler/ast/StatementBlock.java | 54 +++- .../ast/TypeCompositionStatement.java | 74 ++++-- .../org/xvm/compiler/ast/TypeExpression.java | 5 + .../xvm/compiler/ast/TypedefStatement.java | 7 + .../main/java/org/xvm/tool/ModuleInfo.java | 25 ++ .../xvm/api/BindingIdentitySnapshotTest.java | 144 +++++++++++ .../compiler/ast/AstChildTraversalTest.java | 131 ++++++++++ .../compiler/ast/CompositionSourceTest.java | 36 +++ .../ast/DeclarationProvenanceTest.java | 151 +++++++++++ .../compiler/ast/OrdinaryAstTreeCopyTest.java | 42 +++ .../lsp/adapter/CompilerAtomicEmissionTest.kt | 107 ++++++++ .../lsp/adapter/CompilerBindingFactsTest.kt | 90 +++++++ .../lsp/adapter/CompilerFunctionTypingTest.kt | 149 +++++++++++ .../xvm/lsp/adapter/CompilerTestSupport.kt | 21 ++ 38 files changed, 2199 insertions(+), 157 deletions(-) create mode 100644 javatools/src/main/java/org/xvm/compiler/InitializerBinding.java create mode 100644 javatools/src/main/java/org/xvm/compiler/InvocationBinding.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/LambdaBindings.java create mode 100644 javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/CompositionSourceTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerAtomicEmissionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBindingFactsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerFunctionTypingTest.kt diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index ff3da68df71..3a3dac3e6db 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -1,5 +1,9 @@ package org.xvm.api; +import org.jetbrains.annotations.NotNull; + +import static java.util.Objects.requireNonNull; + import java.io.BufferedReader; import java.io.File; import java.io.IOException; @@ -8,6 +12,8 @@ import java.time.Instant; import java.util.Arrays; +import java.util.Collections; +import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; @@ -15,8 +21,6 @@ import java.util.function.Function; -import org.jetbrains.annotations.NotNull; - import org.xvm.asm.ConstantPool; import org.xvm.asm.DirRepository; import org.xvm.asm.ErrorListener; @@ -33,11 +37,16 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.CompilerException; import org.xvm.compiler.InstantRepository; +import org.xvm.compiler.InitializerBinding; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Parser; import org.xvm.compiler.Source; import org.xvm.compiler.Token.Id; - +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.InvocationExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.PropertyDeclarationStatement; import org.xvm.compiler.ast.Statement; import org.xvm.compiler.ast.StatementBlock; import org.xvm.compiler.ast.TypeCompositionStatement; @@ -50,8 +59,6 @@ import org.xvm.runtime.template._native.io.xExternalConsole; -import static java.util.Objects.requireNonNull; - import static org.xvm.asm.ErrorListener.NOWHERE; import static org.xvm.asm.ErrorListener.at; import static org.xvm.util.Severity.ERROR; @@ -373,38 +380,117 @@ public ModuleStructure compile(Source source, ModuleRepository input, @NotNull E } /** - * The outcome of one compilation, including progress retained after a source error. + * The outcome of compiling a source or module source tree. * - *

The compiling worker owns the returned mutable compiler objects. A host must copy the - * facts it needs before sharing them with other threads; this record does not detach an AST. - * No field is a promise that a failed compilation is executable. + *

A failed compilation used to answer with nothing but null, which threw away everything the + * attempt had built. That is most of what a host wants when it fails: the structures a + * verification error was raised against still exist, and the pool they were interned in is + * the only way to reach them - to ask a type what building its TypeInfo had to say, for + * instance. * - * @param module the executable module on success, or null on failure or cancellation - * @param file the file structure, or null if registration did not finish - * @param ast the assembled source tree, or null if loading or parsing did not finish + *

Recovered source trees have not entered compiler passes. Traverse their children using + * each root's Source; child parent pointers may not yet be installed. Binding maps are + * immutable identity snapshots: keys and values compare by reference, not structural equality. + * + * @param module the compiled module, or null if the compilation did not get that far + * @param file the file structure that was built, or null if it did not get that far + * @param ast the assembled source tree, or null if loading/parsing failed; linked + * member trees retain their original Sources and positions + * @param sourceTrees available per-source syntax, including recovered trees on parse failure; + * these are structural facts, not a promise of semantic validity + * @param callBindings immutable call facts keyed by surviving invocation identity; compiler + * objects remain worker-owned and must be copied before concurrent use + * @param functionBindings validated function signatures with no selected runtime method; + * ownership is the same as for callBindings + * @param constructorBindings validated constructor and argument provenance; the same ownership + * rules apply, with no additional state on the construction AST + * @param initializerBindings successful constant-initializer facts anchored to surviving source + * properties; no speculative clone nodes or contexts are retained */ - public record Compilation(ModuleStructure module, FileStructure file, StatementBlock ast) { + public record Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, + List sourceTrees, + Map callBindings, + Map functionBindings, + Map constructorBindings, + Map initializerBindings) { + public Compilation { + sourceTrees = List.copyOf(sourceTrees); + callBindings = identitySnapshot(callBindings); + functionBindings = identitySnapshot(functionBindings); + constructorBindings = identitySnapshot(constructorBindings); + initializerBindings = identitySnapshot(initializerBindings); + } + + /** Retain hosts that supply constructor bindings without folded initializer facts. */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, + List sourceTrees, + Map callBindings, + Map functionBindings, + Map constructorBindings) { + this(module, file, ast, sourceTrees, callBindings, functionBindings, constructorBindings, Map.of()); + } + + /** Retain hosts that supply method and function bindings. */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, + List sourceTrees, + Map callBindings, + Map functionBindings) { + this(module, file, ast, sourceTrees, callBindings, functionBindings, Map.of()); + } + + /** Retain hosts that supply only statically selected method calls. */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, + List sourceTrees, + Map callBindings) { + this(module, file, ast, sourceTrees, callBindings, Map.of()); + } + + /** Retain the construction API for hosts supplying structural source trees. */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, + List sourceTrees) { + this(module, file, ast, sourceTrees, Map.of()); + } + + /** Retain the original construction API for hosts supplying one assembled tree. */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast) { + this(module, file, ast, ast == null ? List.of() : List.of(ast)); + } + /** - * Wrap a file structure for passive inspection without implying successful compilation. + * Represent file structures without a successful module or retained source tree. + * + * @param file the file structure produced so far * - * @param file the structure to inspect * @return a partial compilation */ public static Compilation forFile(FileStructure file) { return new Compilation(null, requireNonNull(file, "file"), null); } - /** @return true if an executable module was produced */ + /** + * @return true iff a module came out of it + */ public boolean succeeded() { return module != null; } - /** @return this attempt's pool, or null if no file structure was built */ + /** + * @return the pool this compilation interned into, or null if there was no compilation + */ public ConstantPool pool() { return file == null ? null : file.getConstantPool(); } - /** @return the assembled source root, or null if loading or parsing failed */ + /** + * Walk the assembled source after successful loading/parsing. + * + *

A host that wants to say where something is has to come through here: a + * {@link org.xvm.asm.Component} knows its name, its kind and its children, and nothing + * about the text it was written in. Only the AST carries positions. + * + * @return the assembled source root, or null after loading/parsing errors; use + * {@link #sourceTrees()} for available structural syntax in that case + */ public StatementBlock parsed() { return ast; } @@ -423,7 +509,7 @@ public Compilation compileModule(Source source, ModuleRepository input, @NotNull requireNonNull(source, "source"); return compileModule(listener -> { StatementBlock tree = new Parser(source, listener).parseSource(); - return listener.hasSeriousErrors() ? null : tree; + return new ParsedSources(listener.hasSeriousErrors() ? null : tree, List.of(tree)); }, input, errs); } @@ -444,11 +530,22 @@ public Compilation compileModule(ModuleInfo sources, ModuleRepository input, @No requireNonNull(sources, "sources"); return compileModule(listener -> { Node root = sources.getSourceTree(listener); - return root == null ? null : (StatementBlock) root.ast(); + return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), + sources.getParsedSources()); }, input, errs); } - private Compilation compileModule(Function parse, + /** An assembled tree is available only when parsing/loading succeeded. */ + /** Preserve node identity, reject null entries and detach from the collector. */ + private static Map identitySnapshot(Map source) { + Map copy = new IdentityHashMap<>(); + source.forEach((key, value) -> copy.put(requireNonNull(key), requireNonNull(value))); + return Collections.unmodifiableMap(copy); + } + + private record ParsedSources(StatementBlock root, List sources) {} + + private Compilation compileModule(Function parse, ModuleRepository input, ErrorListener errs) { verifyConfigured(); requireNonNull(errs, "errs"); @@ -515,13 +612,15 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu */ private static class EmbeddingCompiler extends org.xvm.tool.Compiler { - private final Function parse; + private final Function parse; + private final InvocationBinding.Collector bindings = new InvocationBinding.Collector(); private final ModuleRepository inRepo; private final ModuleRepository coreRepo; private ModuleStructure module; private FileStructure file; private StatementBlock ast; + private List sourceTrees = List.of(); /** * Everything this attempt produced. @@ -534,7 +633,17 @@ private static class EmbeddingCompiler * @return the outcome; never null, though its parts may be */ Compilation result() { - return new Compilation(module, file, ast); + var facts = bindings.finishFacts(ast == null ? List.of() : List.of(ast)); + return new Compilation(module, file, ast, sourceTrees, facts.methods(), facts.functions(), + facts.constructors(), facts.initializers()); + } + + /** + * @return the file structure this compilation built, which exists whether or not the + * compilation went on to succeed + */ + FileStructure getFileStructure() { + return file; } @Override @@ -544,7 +653,7 @@ public boolean isAbortDesired() { return super.isAbortDesired() || m_errors.isAbortDesired(); } - protected EmbeddingCompiler(Function parse, + protected EmbeddingCompiler(Function parse, ModuleRepository input, ModuleRepository core, ErrorListener errs) { super(CompilerOptions.builder().build(), SILENT_CONSOLE, errs); @@ -567,7 +676,9 @@ protected int process() { // recover within its own stage, subject to the host's budget and cancellation. ErrorListener errs = ErrorListener.cancellable( ErrorListener.collecting(this::log), m_errors::isAbortDesired); - block = this.ast = parse.apply(errs); + ParsedSources parsed = parse.apply(errs); + block = this.ast = parsed.root(); + this.sourceTrees = parsed.sources(); } catch (CompilerException e) { return 1; } @@ -591,7 +702,7 @@ protected int process() { return checkErrors("source parsing"); } - Compiler compiler = new Compiler(stmtModule, this); + Compiler compiler = new Compiler(stmtModule, this, bindings); FileStructure struct = compiler.generateInitialFileStructure(); this.file = struct; if (struct == null || checkErrors("module creation") != 0) { @@ -648,6 +759,12 @@ protected int emitModules(List allNodes, ModuleRepository ignore) { throw new IllegalStateException("This method must not be called"); } + /** + * @return the result of the compilation + */ + protected ModuleStructure getModule() { + return module; + } } /** @@ -758,7 +875,7 @@ public interface Control /** * Create a runtime container and execute the provided module. * - * A limited set of injections are made available to the module, including the console, clock, + *

A limited set of injections are made available to the module, including the console, clock, * and other "safe" injectable types. The FileSystem is provided as detailed by the "rootDir" * parameter. * @@ -787,7 +904,7 @@ public Control run( /** * Create a runtime container and execute the specified module. * - * The "customerInjector" option allows the caller to indicate an Ecstasy Injector class that + *

The "customerInjector" option allows the caller to indicate an Ecstasy Injector class that * will be loaded into its own container, and provided with the full set of injectable resources * that Ecstasy supports, also including any provided String injections; in turn, that * implementation provides the injections that will be available to the specified module within diff --git a/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java b/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java index 3bfc486ed29..b4cf3b8a9e3 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java +++ b/javatools/src/main/java/org/xvm/asm/constants/MethodBody.java @@ -292,9 +292,13 @@ public boolean isInto() { } /** + * Inspect the existing mixin constraint chain without generating a forwarding body. + * The returned compiler metadata belongs to the current compilation; hosts must copy facts + * before publishing them to another thread. Only call this for a FromInto implementation. + * * @return the (potentially incomplete) MethodInfo from which the "into" MethodBody was created */ - MethodInfo getIntoMethodInfo() { + public MethodInfo getIntoMethodInfo() { assert isInto(); return (MethodInfo) m_target; } diff --git a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java index c6436f087b5..4ef470bc56d 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java +++ b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java @@ -6,6 +6,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.function.Predicate; @@ -1348,6 +1349,29 @@ public boolean hasSuper(TypeInfo infoType) { return getSuper(infoType) != null; } + /** + * The written method body selected by super dispatch, when there is one. Field, native, + * delegation and into-constraint bodies have no source method target. Inspection uses the + * same chain selection as getSuper(), without generating or optimizing forwarding code. + */ + public Optional getSuperMethod(TypeInfo infoType) { + MethodStructure method = getTopmostMethodStructure(infoType); + if (method.getParent().getParent() instanceof PropertyStructure property && + (method == property.getGetter() || method == property.getSetter()) && + infoType.findProperty(property.getIdentityConstant()).hasField()) { + return Optional.empty(); + } + MethodBody[] chain = m_aBodyResolved; + MethodBody body = chain == null ? findSuper(infoType, getChain()) + : chain.length > 1 ? chain[1] : null; + return Optional.ofNullable(body) + .filter(candidate -> candidate.getImplementation() == Implementation.Explicit || + candidate.getImplementation() == Implementation.Default) + .map(MethodBody::getMethodStructure) + .filter(target -> !target.isSynthetic()) + .map(MethodStructure::getIdentityConstant); + } + /** * @return the "super" signature */ diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index 0a88fcc9efc..95836b5d97f 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -1,5 +1,7 @@ package org.xvm.compiler; +import static java.util.Objects.requireNonNull; + import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; import org.xvm.asm.FileStructure; @@ -10,8 +12,6 @@ import org.xvm.compiler.ast.StageMgr; import org.xvm.compiler.ast.TypeCompositionStatement; -import static java.util.Objects.requireNonNull; - /** * A module compiler for Ecstasy code. * @@ -29,16 +29,21 @@ public class Compiler { * @param errs the listener to log any errors to during the various phases of compilation */ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs) { + this(stmtModule, errs, InvocationBinding.Collector.NONE); + } + + /** Construct a compiler that records call facts in the supplied attempt-owned collector. */ + public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, + InvocationBinding.Collector bindings) { if (stmtModule == null) { throw new IllegalArgumentException("AST node for module required"); } if (stmtModule.getCategory().getId() != Token.Id.MODULE) { throw new IllegalArgumentException("AST node for module is not a module statement"); } - requireNonNull(errs, "errs"); - m_stmtModule = stmtModule; - m_errs = errs; + f_errs = requireNonNull(errs, "errs"); + f_bindings = requireNonNull(bindings, "bindings"); } // ----- accessors ----------------------------------------------------------------------------- @@ -56,7 +61,7 @@ public TypeCompositionStatement getModuleStatement() { */ public ErrorListener getErrorListener() { validateCompiler(); - return m_errs; + return f_errs; } /** @@ -91,7 +96,7 @@ public boolean alreadyReached(Stage stage) { * @return true if the compiler has decided to abort the process */ public boolean isAbortDesired() { - return m_errs.isAbortDesired(); + return f_errs.isAbortDesired(); } // ----- public API ---------------------------------------------------------------------------- @@ -114,9 +119,9 @@ public FileStructure generateInitialFileStructure() { if (getStage() == Stage.Initial) { setStage(Stage.Registering); - StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, m_errs); + StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, f_errs, f_bindings); if (!mgr.processComplete()) { - if (m_errs.hasSeriousErrors() || m_errs.isAbortDesired()) { + if (f_errs.hasSeriousErrors() || f_errs.isAbortDesired()) { return null; } throw new CompilerException("failed to create module"); @@ -187,7 +192,7 @@ public boolean resolveNames(boolean fLastAttempt) { if (!alreadyReached(Stage.Resolving)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Resolving); - m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, m_errs); + m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, f_errs, f_bindings); } if (fLastAttempt) { @@ -231,7 +236,7 @@ public boolean validateExpressions(boolean fLastAttempt) { if (!alreadyReached(Stage.Validating)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Validating); - m_mgr = new StageMgr(m_stmtModule, Stage.Validated, m_errs); + m_mgr = new StageMgr(m_stmtModule, Stage.Validated, f_errs, f_bindings); } if (fLastAttempt) { @@ -275,7 +280,7 @@ public boolean generateCode(boolean fLastAttempt) { if (!alreadyReached(Stage.Emitting)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Emitting); - m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, m_errs); + m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, f_errs, f_bindings); } if (fLastAttempt) { @@ -285,10 +290,11 @@ public boolean generateCode(boolean fLastAttempt) { if (m_mgr.processComplete()) { setStage(Stage.Emitted); - if (!m_errs.hasSeriousErrors()) { - // "purge" the constant pool and do a final validation on the entire module structure + if (!f_errs.hasSeriousErrors()) { + // "purge" the constant pool and do a final validation on the entire module + // structure m_structFile.reregisterConstants(true); - m_structFile.validate(m_errs); + m_structFile.validate(f_errs); } } } @@ -301,8 +307,8 @@ public boolean generateCode(boolean fLastAttempt) { * method will report any unresolved names as fatal errors. */ public void logRemainingDeferredAsErrors() { - if (!m_errs.hasSeriousErrors()) { - m_mgr.logDeferredAsErrors(m_errs); + if (!f_errs.hasSeriousErrors()) { + m_mgr.logDeferredAsErrors(f_errs); } } @@ -376,7 +382,10 @@ private void setStage(Stage stage) { /** * The ErrorListener to report errors to. */ - private final ErrorListener m_errs; + private final ErrorListener f_errs; + + /** Optional tooling output, confined to this compiler attempt. */ + private final InvocationBinding.Collector f_bindings; /** * The FileStructure that this compiler is putting together in a series of passes. diff --git a/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java b/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java new file mode 100644 index 00000000000..9bcef7267de --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java @@ -0,0 +1,85 @@ +package org.xvm.compiler; + +import java.util.ArrayList; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; + +import org.xvm.asm.Constant; +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NamedTypeExpression; + +/** + * Successful constant-initializer facts copied before its temporary method is discarded. + * Contains source spans and compiler constants, never clone nodes, contexts, registers or tokens. + * Constants remain owned by the compilation worker, like {@link InvocationBinding} constants. + */ +public record InitializerBinding(List references, List expressions, + List calls) { + public InitializerBinding { + references = List.copyOf(references); + expressions = List.copyOf(expressions); + calls = List.copyOf(calls); + } + + public record Span(long startPosition, long endPosition) { + public static Span of(AstNode node) { + return new Span(node.getStartPosition(), node.getEndPosition()); + } + } + + public record Reference(String name, Span span, Constant target, TypeConstant type) {} + + public record TypedExpression(Span span, TypeConstant type) {} + + public record Call(Span span, Span callee, InvocationBinding binding, boolean construction) {} + + /** Capture the written nodes before validation can replace constant subexpressions. */ + public static List writtenNodes(AstNode root) { + List nodes = new ArrayList<>(List.of(root)); + for (int i = 0; i < nodes.size(); i++) { + nodes.get(i).children().forEachRemaining(nodes::add); + } + return List.copyOf(nodes); + } + + /** Called only after successful validation/emission and the decision to discard the method. */ + public static InitializerBinding capture(List written, InvocationBinding.Facts facts) { + List references = new ArrayList<>(); + List expressions = new ArrayList<>(); + List calls = new ArrayList<>(); + Map callees = new IdentityHashMap<>(); + facts.methods().forEach((node, binding) -> { + callees.put(node.getInvokedExpression(), binding.method()); + calls.add(new Call(Span.of(node), Span.of(node.getInvokedExpression()), binding, false)); + }); + facts.constructors().forEach((node, binding) -> + calls.add(new Call(Span.of(node), Span.of(node), binding, true))); + written.forEach(node -> { + TypeConstant type = node instanceof Expression expression && expression.isValidated() && + expression.getTypeFit().isFit() ? expression.getType() : null; + if (type != null) { + expressions.add(new TypedExpression(Span.of(node), type)); + } + if (node instanceof NameExpression name) { + var target = callees.containsKey(name) ? callees.get(name) : name.getResolvedTarget(); + if (target instanceof Constant constant) { + references.add(reference(name.getNameToken(), constant, type)); + } + } else if (node instanceof NamedTypeExpression name) { + name.getNameBindings().stream().filter(binding -> binding.target() != null) + .forEach(binding -> references.add(reference(binding.name(), binding.target(), type))); + } + }); + return new InitializerBinding(references, expressions, calls); + } + + private static Reference reference(Token token, Constant target, TypeConstant type) { + return new Reference(token.getValueText(), + new Span(token.getStartPosition(), token.getEndPosition()), target, type); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java b/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java new file mode 100644 index 00000000000..1e3ddc71ecd --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java @@ -0,0 +1,244 @@ +package org.xvm.compiler; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.xvm.asm.constants.MethodConstant; +import org.xvm.asm.constants.SignatureConstant; +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.InvocationExpression; +import org.xvm.compiler.ast.LabeledExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.PropertyDeclarationStatement; + +import static java.util.Objects.requireNonNull; + +/** + * Immutable source provenance for a selected, instantiated method call. Created during real + * validation, not overload probing. No Context or mutable argument expressions are retained. + */ +public record InvocationBinding(MethodConstant method, SignatureConstant signature, + List arguments) { + public InvocationBinding { + arguments = List.copyOf(arguments); + } + + /** A validated function-valued invocation has a signature, but no statically selected method. */ + public record FunctionCall(TypeConstant type, List arguments) { + public FunctionCall { + arguments = List.copyOf(arguments); + } + } + + /** + * The two kinds of call facts remain separate so a function never claims a method target. + * Maps are immutable identity snapshots: keys and values compare by reference. + */ + public record Facts(Map methods, + Map functions, + Map constructors, + Map initializers) { + public Facts { + methods = identitySnapshot(methods); + functions = identitySnapshot(functions); + constructors = identitySnapshot(constructors); + initializers = identitySnapshot(initializers); + } + + public Facts(Map methods, + Map functions, + Map constructors) { + this(methods, functions, constructors, Map.of()); + } + + public Facts(Map methods, + Map functions) { + this(methods, functions, Map.of()); + } + } + + /** Preserve node identity, reject null entries and detach from the collector. */ + private static Map identitySnapshot(Map source) { + Map copy = new IdentityHashMap<>(); + source.forEach((key, value) -> copy.put(requireNonNull(key), requireNonNull(value))); + return Collections.unmodifiableMap(copy); + } + + /** A written label copied before argument rewriting, without retaining its mutable token. */ + public record Label(String name, long startPosition, long endPosition) {} + + /** A written argument's source range and visible parameter index; positional/legacy facts have no label. */ + public record Argument(long startPosition, long endPosition, int parameterIndex, boolean named, + Label label) { + /** Retain callers that recorded named status without the label's source span. */ + public Argument(long startPosition, long endPosition, int parameterIndex, boolean named) { + this(startPosition, endPosition, parameterIndex, named, null); + } + + /** Retain the original positional-argument construction API. */ + public Argument(long startPosition, long endPosition, int parameterIndex) { + this(startPosition, endPosition, parameterIndex, false); + } + } + + /** + * Match the compiler's parameter-ordered expressions to their written argument spans before + * validation inserts conversions. Synthetic defaults have no matching written expression. + */ + public static Optional> arguments(List written, List ordered) { + List bindings = new ArrayList<>(); + for (Expression expression : written) { + Expression value = expression instanceof LabeledExpression labeled + ? labeled.getUnderlyingExpression() : expression; + int parameter = -1; + for (int i = 0; i < ordered.size(); ++i) { + Expression candidate = ordered.get(i); + if (candidate == expression || candidate == value || + candidate.getStartPosition() == value.getStartPosition() && + candidate.getEndPosition() == value.getEndPosition()) { + parameter = i; + break; + } + } + if (parameter < 0) { + // A compiler rewrite changed the source association. Omit tooling facts rather + // than guessing a parameter or changing whether the program compiles. + return Optional.empty(); + } + Label label = expression instanceof LabeledExpression labeled + ? new Label(labeled.getName(), labeled.getNameToken().getStartPosition(), + labeled.getNameToken().getEndPosition()) + : null; + bindings.add(new Argument(expression.getStartPosition(), expression.getEndPosition(), + parameter, label != null, label)); + } + return Optional.of(List.copyOf(bindings)); + } + + /** + * Compiler-worker scratch space owned by one compilation attempt, never by an AST node. + * Validation may replace or invalidate entries. Publication retains only successfully + * validated invocations still present in the final tree, excluding speculative clones and + * discarded rewrites. Finishing clears scratch entries, including discarded clones, even if + * a compiler context survives with the returned tree. The result is detached from the collector. + */ + public static final class Collector { + public Collector() { + this(true); + } + + private Collector(boolean enabled) { + f_enabled = enabled; + } + + /** @return whether this attempt requests call facts */ + public boolean isEnabled() { + return f_enabled; + } + + /** Invalidate any earlier result before retrying validation of this invocation. */ + public void begin(InvocationExpression invocation) { + if (f_enabled) { + f_bindings.remove(invocation); + f_functions.remove(invocation); + } + } + + /** Record a completed validation; node identity keeps speculative clones separate. */ + public void record(InvocationExpression invocation, InvocationBinding binding) { + if (f_enabled) { + f_bindings.put(invocation, binding); + } + } + + public void record(InvocationExpression invocation, FunctionCall binding) { + if (f_enabled) { + f_functions.put(invocation, binding); + } + } + + /** Constructor arguments undergo the same named/default argument rewriting as methods. */ + public void begin(NewExpression construction) { + if (f_enabled) { + f_constructors.remove(construction); + } + } + + public void record(NewExpression construction, InvocationBinding binding) { + if (f_enabled) { + f_constructors.put(construction, binding); + } + } + + /** A discarded synthetic initializer keeps detached facts anchored to its source property. */ + public void record(PropertyDeclarationStatement property, InitializerBinding binding) { + if (f_enabled) { + f_initializers.put(property, binding); + } + } + + /** Finish the attempt, returning immutable facts for the surviving source trees. */ + public Map finish(List roots) { + return finishFacts(roots).methods(); + } + + /** Finish both kinds together, releasing discarded or speculative nodes in either map. */ + public Facts finishFacts(List roots) { + Map result = new IdentityHashMap<>(); + Map functions = new IdentityHashMap<>(); + Map constructors = new IdentityHashMap<>(); + Map initializers = new IdentityHashMap<>(); + List nodes = new ArrayList<>(roots); + for (int i = 0; i < nodes.size(); ++i) { + AstNode node = nodes.get(i); + if (node instanceof InvocationExpression invocation && + invocation.isValidated() && invocation.getTypeFit().isFit()) { + InvocationBinding binding = f_bindings.get(invocation); + if (binding != null) { + result.put(invocation, binding); + } + FunctionCall function = f_functions.get(invocation); + if (function != null) { + functions.put(invocation, function); + } + } + if (node instanceof NewExpression construction && construction.isValidated() && + construction.getTypeFit().isFit()) { + InvocationBinding binding = f_constructors.get(construction); + if (binding != null) { + constructors.put(construction, binding); + } + } + if (node instanceof PropertyDeclarationStatement property) { + InitializerBinding binding = f_initializers.get(property); + if (binding != null) { + initializers.put(property, binding); + } + } + node.children().forEachRemaining(nodes::add); + } + f_bindings.clear(); + f_functions.clear(); + f_constructors.clear(); + f_initializers.clear(); + return new Facts(result, functions, constructors, initializers); + } + + /** Ordinary compiler clients need not collect tooling facts. This instance never writes. */ + public static final Collector NONE = new Collector(false); + + private final boolean f_enabled; + + private final Map f_bindings = new IdentityHashMap<>(); + private final Map f_functions = new IdentityHashMap<>(); + private final Map f_constructors = new IdentityHashMap<>(); + private final Map f_initializers = new IdentityHashMap<>(); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java b/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java new file mode 100644 index 00000000000..ef2e7005311 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java @@ -0,0 +1,54 @@ +package org.xvm.compiler.ast; + +import java.util.Collections; +import java.util.HashMap; +import java.util.IdentityHashMap; +import java.util.Map; + +import org.xvm.asm.Component; +import org.xvm.asm.PropertyStructure; +import org.xvm.asm.Register; +import org.xvm.asm.constants.PropertyConstant; + +/** Captured variables and the source origins of registers used to read them. */ +public final class AnonymousClassBindings { + AnonymousClassBindings(NewExpression owner, Map registers) { + f_owner = owner; + f_registers = Map.copyOf(registers); + } + + /** + * @return an immutable snapshot, preserving register identity rather than register equality + */ + public Map getCaptureOrigins() { + return Collections.unmodifiableMap(new IdentityHashMap<>(f_origins)); + } + + /** Copy generated capture properties to their enclosing source registers. */ + Map propertyOrigins(Component component) { + var origins = new HashMap(); + f_registers.forEach((name, register) -> { + if (component.getChild(name) instanceof PropertyStructure property && + property.isSynthetic()) { + origins.put(property.getIdentityConstant(), register.getOriginalRegister()); + } + }); + return Map.copyOf(origins); + } + + boolean isFor(NewExpression owner) { + return f_owner == owner; + } + + Map registers() { + return f_registers; + } + + void bind(String name, Register captured) { + f_origins.put(captured.getOriginalRegister(), f_registers.get(name).getOriginalRegister()); + } + + private final NewExpression f_owner; + private final Map f_registers; + private final Map f_origins = new IdentityHashMap<>(); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index 687750b7e8f..e2254e71c38 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -18,6 +18,8 @@ import java.util.NoSuchElementException; import java.util.Set; +import java.util.function.BiConsumer; + import java.util.stream.Collectors; import org.xvm.asm.Argument; @@ -46,6 +48,7 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; @@ -55,12 +58,13 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; +import static org.xvm.util.Handy.indentLines; + import static org.xvm.asm.ErrorListener.NOWHERE; -import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; -import static org.xvm.util.Handy.indentLines; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; /** * Common base class for all statements and expressions. @@ -129,15 +133,29 @@ protected T adopt(T child) { } /** - * Return an Iterable/Iterator that represents all the child nodes of this node. + * Return a one-shot cursor over the child nodes of this node. The cursor supports replacement + * and removal during compiler traversal. Its Iterable view uses the same cursor, not a new + * traversal; use {@link #childNodes()} when an independently iterable view is needed. * - * @return an Iterable of child nodes (from whence an Iterator can be obtained) + * @return a mutable traversal cursor */ public ChildIterator children() { Field[] fields = getChildFields(); return fields.length == 0 ? ChildIterator.EMPTY : new ChildIteratorImpl(fields); } + /** + * Return a view of this node's children that starts an independent traversal for each iterator. + * This is a live view, not a snapshot: subsequent traversals see subsequent AST changes. + * Callers that need a stable list must copy it while they own the tree; iteration does not + * provide synchronization with compiler mutation. + * + * @return an iterable view of the child nodes + */ + public Iterable childNodes() { + return this::children; + } + /** * Replace the specified child of this AstNode with a new child. * @@ -206,8 +224,39 @@ protected Field[] getChildFields() { return NO_FIELDS; } + /** + * Compatibility entry point for {@link #copyTree()}. + * + * @return a tree copy of this node + */ @Override + @SuppressWarnings("MethodDoesntCallSuperMethod") // copyTree owns allocation and copying. public AstNode clone() { + return copyTree(); + } + + /** + * Copy this node and its registered syntax children, preserving the concrete node type, source + * positions and compilation stage. Copied children belong to their copied owners. The root + * retains its original parent until a caller adopts it into another tree; copying does not + * replace or reparent any original node. + * + *

This is not a fresh parse or an isolated copy of all compiler state. The default + * implementation shallow-copies non-child fields, including resolved bindings and transient + * fields. Overrides must handle owned state that cannot be shared, such as a lambda's + * generated method. Recovery nodes construct fresh shells and copy their written syntax + * without carrying inherited validation or emission state. + * + *

The default implementation still uses shallow cloning followed by reflective copying of + * registered children; copied child lists are mutable. Constructor-based overrides must copy + * and adopt their children and preserve root metadata with {@link #copyTreeMetadataTo(AstNode)}. + * Specialize this method rather than {@link #clone()} so both entry points behave alike. + * Declare a covariant return type when specializing, so callers can copy that node type without + * a cast. A caller-selected method type parameter would not enforce that relationship. + * + * @return a tree copy of this node + */ + public AstNode copyTree() { AstNode that; try { that = (AstNode) super.clone(); @@ -227,14 +276,14 @@ public AstNode clone() { if (oVal != null) { if (oVal instanceof AstNode node) { - AstNode nodeNew = node.clone(); + AstNode nodeNew = node.copyTree(); that.adopt(nodeNew); oVal = nodeNew; } else if (oVal instanceof List list) { ArrayList listNew = new ArrayList<>(); for (AstNode node : (List) list) { - listNew.add(node.clone()); + listNew.add(node.copyTree()); } that.adopt(listNew); @@ -255,6 +304,21 @@ public AstNode clone() { return that; } + /** + * Preserve the root metadata of a constructor-based tree copy. The caller is responsible for + * copying and adopting children; this method does not transfer validation or emission state. + * + * @param copy the newly constructed node + * @param the concrete node type + * + * @return the supplied copy with this node's parent and compilation stage + */ + protected final T copyTreeMetadataTo(T copy) { + copy.setParent(getParent()); + copy.setStage(getStage()); + return copy; + } + /** * @return the current compilation stage for this node */ @@ -642,6 +706,7 @@ public Access getDefaultAccess() { * {@link Severity#ERROR}, or {@link Severity#FATAL} * @param sCode the error code that identifies the error message * @param aoParam the parameters for the error message; may be null + * */ public void log(ErrorListener errs, Severity severity, String sCode, Object... aoParam) { Source source = getSource(); @@ -738,6 +803,11 @@ protected void updateLineNumber(Code code) { * @return true if the children got caught up; false if the catch-up aborted */ protected boolean catchUpChildren(ErrorListener errs) { + return catchUpChildren(errs, InvocationBinding.Collector.NONE); + } + + /** Catch up children using the enclosing compilation attempt's collector. */ + protected boolean catchUpChildren(ErrorListener errs, InvocationBinding.Collector bindings) { // determine what stage we're trying to catch the children up to Stage stageTarget = getStage(); if (!stageTarget.isTargetable()) { @@ -777,7 +847,7 @@ protected boolean catchUpChildren(ErrorListener errs) { ErrorListener errsTemp = errs.branch(this); while (stageOldest.compareTo(stageTarget) < 0) { Stage stageNext = stageOldest.nextTarget(); - StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp); + StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp, bindings); for (int cTries = 0; !mgrKids.processComplete(); cTries++) { if (errsTemp.isAbortDesired() || cTries > 20) { mgrKids.logDeferredAsErrors(errsTemp); @@ -1074,6 +1144,29 @@ private void collectMatchingMethods( Set setConvert, Map mapMethods, ErrorListener errs) { + collectMatchingMethods(ctx, typeTarget, infoTarget, setMethods, listExprArgs, fCall, + mapNamedExpr, atypeReturn, setIs, setConvert, mapMethods, errs, (signature, ordered) -> {}); + } + + /** + * Reuse ordinary argument fitting. The package-private observer lets partial-call queries copy + * tentative signatures and argument order without adding cursor-result policy to the fitter. + * The callback runs synchronously after a match is recorded in the supplied collections. + */ + final void collectMatchingMethods( + Context ctx, + TypeConstant typeTarget, + TypeInfo infoTarget, + Set setMethods, + List listExprArgs, + boolean fCall, + Map mapNamedExpr, + TypeConstant[] atypeReturn, + Set setIs, + Set setConvert, + Map mapMethods, + ErrorListener errs, + BiConsumer> matching) { ConstantPool pool = pool(); int cExprs = listExprArgs == null ? 0 : listExprArgs.size(); int cReturns = atypeReturn == null ? 0 : atypeReturn.length; @@ -1289,6 +1382,7 @@ private void collectMatchingMethods( setIs.add(idMethod); } mapMethods.put(idMethod, method); + matching.accept(sigMethod, listArgs); } // if there is any ambiguity, don't report anything; the caller will log a generic @@ -1823,7 +1917,8 @@ protected static ArrayList ensureArrayList(List list) { } /** - * Collect fields by name. + * Collect fields by name. Nodes in another package of this compiler module grant access only + * to these explicitly registered child fields; their representation need not become public. * * @param clz the class on which the fields exist * @param names the field names @@ -1850,6 +1945,14 @@ protected static Field[] fieldsForNames(Class clz, String... names) { + clzField.getSimpleName() + " on field " + clzTry.getSimpleName() + '.' + names[i]); } + if (!clzTry.getPackageName().equals(AstNode.class.getPackageName()) + && (!AstNode.class.isAssignableFrom(clz) + || clz.getModule() != AstNode.class.getModule() + || clzTry.getModule() != AstNode.class.getModule() + || !field.trySetAccessible())) { + throw new IllegalStateException("cannot access registered child field " + + clzTry.getName() + '.' + names[i]); + } fields[i] = field; continue NextField; } catch (NoSuchFieldException e) { @@ -1873,7 +1976,10 @@ protected static Field[] fieldsForNames(Class clz, String... names) { // ----- inner class: ChildIterator ------------------------------------------------------------ /** - * Represents an Iterator that can also replace the most recently iterated element. + * Represents an Iterator that can also replace the most recently iterated element. Iterable + * is retained for compatibility with enhanced-for loops that mutate through this same cursor. + * Calling {@link #iterator()} does not restart traversal; readers can use + * {@link AstNode#childNodes()} for independent iterations. */ public interface ChildIterator extends Iterable, Iterator { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CompositionNode.java b/javatools/src/main/java/org/xvm/compiler/ast/CompositionNode.java index e64531db55a..d0575efc23c 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CompositionNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/CompositionNode.java @@ -71,10 +71,13 @@ public void setContribution(Contribution contribution) { @Override public Source getSource() { Source source = super.getSource(); - if (source == null) { - source = condition == null ? type.getSource() : condition.getSource(); + if (source != null) { + return source; } - return source; + + // Parsed trees may not have parents yet, and a default composition has no type. + AstNode child = condition == null ? type : condition; + return child == null ? null : child.getSource(); } @Override diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Context.java b/javatools/src/main/java/org/xvm/compiler/ast/Context.java index 140bcee6d79..cb8aae10eca 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Context.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Context.java @@ -11,14 +11,13 @@ import java.util.TreeSet; import org.xvm.asm.Argument; -import org.xvm.asm.Assignment; import org.xvm.asm.ClassStructure; import org.xvm.asm.Component; import org.xvm.asm.Component.SimpleCollector; import org.xvm.asm.ComponentResolver.ResolutionResult; import org.xvm.asm.Constant; -import org.xvm.asm.ConstantPool; import org.xvm.asm.Constants.Access; +import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; import org.xvm.asm.GenericTypeResolver; import org.xvm.asm.MethodStructure; @@ -26,6 +25,7 @@ import org.xvm.asm.Parameter; import org.xvm.asm.PropertyStructure; import org.xvm.asm.Register; +import org.xvm.asm.Assignment; import org.xvm.asm.constants.FormalConstant; import org.xvm.asm.constants.IdentityConstant; @@ -36,13 +36,15 @@ import org.xvm.compiler.ast.StatementBlock.TargetInfo; import org.xvm.compiler.Compiler; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.in; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; /** @@ -61,6 +63,12 @@ protected Context(Context ctxOuter, boolean fDemuxOnExit) { m_fReachable = ctxOuter == null || ctxOuter.isReachable(); } + /** @return the enclosing compilation's call-fact collector, if enabled */ + public InvocationBinding.Collector getInvocationBindings() { + Context outer = getOuterContext(); + return outer == null ? InvocationBinding.Collector.NONE : outer.getInvocationBindings(); + } + /** * @return the outer context */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Expression.java b/javatools/src/main/java/org/xvm/compiler/ast/Expression.java index 3b8cfc150f2..b3bf65df01d 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Expression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Expression.java @@ -33,11 +33,11 @@ import org.xvm.util.Severity; +import static org.xvm.util.Handy.checkElementsNonNull; + import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; -import static org.xvm.util.Handy.checkElementsNonNull; - /** * Base class for all Ecstasy expressions. * @@ -96,6 +96,11 @@ public abstract class Expression extends AstNode { // ----- accessors ----------------------------------------------------------------------------- + @Override + public Expression copyTree() { + return (Expression) super.copyTree(); + } + @Override protected boolean usesSuper() { for (AstNode node : children()) { @@ -305,7 +310,7 @@ protected TypeFit testFitExhaustive(Context ctx, TypeConstant typeRequired, Erro */ protected TypeFit testFitMultiExhaustive(Context ctx, TypeConstant[] atypeRequired, ErrorListener errs) { - Expression exprTemp = (Expression) clone(); + Expression exprTemp = copyTree(); Context ctxTemp = ctx.enter(); Expression exprNew = exprTemp.validateMulti(ctxTemp, atypeRequired, errs); exprTemp.discard(true); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ImportStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/ImportStatement.java index 0977ad63296..80d7714f1e1 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ImportStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ImportStatement.java @@ -71,6 +71,21 @@ public String getAliasName() { return alias == null ? null : alias.getValueText(); } + /** + * @return the written alias token, or null for a wildcard import + */ + public Token getAliasToken() { + return alias; + } + + /** + * @return the resolved import identity without initiating or resuming name resolution + */ + public IdentityConstant getImportedIdentity() { + return m_resolver != null && m_resolver.getConstant() instanceof IdentityConstant identity + ? identity : null; + } + /** * @return true iff the import is of the ".*" form */ @@ -106,6 +121,13 @@ public String[] getQualifiedName() { return asName; } + /** + * @return an immutable copy of the written name tokens, excluding the alias and wildcard + */ + public List getQualifiedNameTokens() { + return List.copyOf(qualifiedName); + } + /** * @return the imported name as a dot-delimited name */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java index 35c79a232fb..5d7a22527a6 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/InvocationExpression.java @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.stream.Collectors; @@ -45,6 +46,7 @@ import org.xvm.asm.op.*; import org.xvm.compiler.Compiler; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; @@ -266,6 +268,28 @@ public long getEndPosition() { return lEndPos; } + /** + * @return the expression supplying the method or function to invoke, excluding call arguments + */ + public Expression getInvokedExpression() { + return expr; + } + + /** + * Which method this call turned out to be a call to. + * + *

The name in a call does not resolve to anything on its own - `print` means nothing without + * knowing what it is being called on, and what the arguments are - so the answer is decided + * here, while the invocation is validated, and kept here. This only makes it readable from + * outside the compiler, which is what a host needs to answer "where is this method declared". + * + * @return the method this resolved to, or null if it never resolved to one, which covers a + * call through a function reference as well as source that does not compile + */ + public MethodConstant getResolvedMethod() { + return m_method == null ? null : m_method.getIdentityConstant(); + } + @Override protected void updateLineNumber(Code code) { if (expr instanceof NameExpression exprName) { @@ -588,6 +612,11 @@ private TypeConstant[] applyRedundantTypes(Context ctx, TypeConstant[] atypeRequ @Override protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, ErrorListener errs) { + InvocationBinding.Collector bindings = ctx.getInvocationBindings(); + List writtenArgs = bindings.isEnabled() ? List.copyOf(args) : List.of(); + Optional sourceBinding = Optional.empty(); + Optional functionBinding = Optional.empty(); + bindings.begin(this); // the reason for tracking success (fValid) is that we want to get as many things // validated as possible, but if some expressions didn't validate, we can't predictably find // the desired method or function (e.g. without a left expression providing validated type @@ -792,6 +821,8 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er : m_targetInfo.getTargetType(); } + Optional> sourceArgs = bindings.isEnabled() && fCall && !m_fBjarne + ? InvocationBinding.arguments(writtenArgs, listArgs) : Optional.empty(); TypeConstant[] atypeArgs = validateExpressions(ctx, listArgs, atypeParams, errs); if (atypeArgs == null) { return null; @@ -859,6 +890,10 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er if (!mapTypeParams.isEmpty()) { sigMethod = sigMethod.resolveGenericTypes(pool, mapTypeParams::get); } + if (sourceArgs.isPresent()) { + sourceBinding = Optional.of(new InvocationBinding(method.getIdentityConstant(), + sigMethod, sourceArgs.get())); + } atypeResult = sigMethod.getRawReturns(); if (fCondReturn) { @@ -998,6 +1033,24 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er } atypeResult = validateFunction(ctx, typeFn, cTypeParams, cDefaults, atypeRequired, errs); + if (bindings.isEnabled() && fCall && !(argMethod instanceof Register register && register.isSuper())) { + TypeConstant functionType = typeFn; + functionBinding = InvocationBinding.arguments(writtenArgs, writtenArgs) + .map(arguments -> new InvocationBinding.FunctionCall(functionType, arguments)); + } + if (bindings.isEnabled() && fCall && argMethod instanceof Register register && + register.isSuper()) { + TypeInfo info = ctx.getThisType().ensureAccess(Access.PRIVATE).ensureTypeInfo(errs); + MethodInfo method = info.getMethodById(ctx.getMethod().getIdentityConstant()); + if (method != null) { + TypeConstant functionType = typeFn; + sourceBinding = method.getSuperMethod(info).flatMap(target -> + InvocationBinding.arguments(writtenArgs, writtenArgs).map(arguments -> + new InvocationBinding(target, pool.ensureSignatureConstant( + target.getName(), pool.extractFunctionParams(functionType), + pool.extractFunctionReturns(functionType)), arguments))); + } + } } } else { // the expr is NOT a NameExpression // it has to either be a function or convertible to a function @@ -1020,6 +1073,10 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er } atypeResult = validateFunction(ctx, typeFn, 0, 0, atypeRequired, errs); + if (bindings.isEnabled() && fCall) { + functionBinding = InvocationBinding.arguments(writtenArgs, writtenArgs) + .map(arguments -> new InvocationBinding.FunctionCall(typeFn, arguments)); + } } } @@ -1050,7 +1107,12 @@ protected Expression validateMulti(Context ctx, TypeConstant[] atypeRequired, Er } } } - return finishValidations(ctx, atypeRequired, atypeResult, TypeFit.Fit, null, errs); + Expression result = finishValidations(ctx, atypeRequired, atypeResult, TypeFit.Fit, null, errs); + if (result != null) { + sourceBinding.ifPresent(binding -> bindings.record(this, binding)); + functionBinding.ifPresent(binding -> bindings.record(this, binding)); + } + return result; } @Override diff --git a/javatools/src/main/java/org/xvm/compiler/ast/LambdaBindings.java b/javatools/src/main/java/org/xvm/compiler/ast/LambdaBindings.java new file mode 100644 index 00000000000..2d080ee6947 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/LambdaBindings.java @@ -0,0 +1,76 @@ +package org.xvm.compiler.ast; + +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.xvm.asm.Argument; +import org.xvm.asm.MethodStructure; +import org.xvm.asm.Register; + +import org.xvm.compiler.Token; + +import org.xvm.compiler.ast.LambdaExpression.LambdaContext; + +/** + * Source associations for one generated lambda method, owned by its compilation context. + * A new validation context starts with new bindings; cloning an AST neither copies nor clears + * these maps. Register allocation and runtime register identity are unaffected. + */ +public final class LambdaBindings { + /** A source parameter and its validated binding, excluding synthetic capture parameters. */ + public record ParameterBinding(Token name, Register register) {} + + /** + * @return the source parameter bindings retained so far + */ + public List getParameters() { + return List.copyOf(parameters.values()); + } + + /** + * @return capture registers mapped to enclosing source registers, by object identity + */ + public Map getCaptureOrigins() { + return Collections.unmodifiableMap(new IdentityHashMap<>(captureOrigins)); + } + + boolean isFor(MethodStructure method) { + return method != null && this.method == method; + } + + void bind(LambdaExpression lambda, LambdaContext context, int index, Register register) { + method = lambda.getLambda(); + int captures = method.getParamCount() - lambda.getParamCount(); + if (index < captures) { + String name = method.getParam(index).getName(); + Argument origin = context.ensureRegisterMap().get(name); + if (origin == null) { + origin = context.getFormalMap().get(name); + } + if (origin instanceof Register source) { + captureOrigins.put(register.getOriginalRegister(), source.getOriginalRegister()); + } + } else { + int sourceIndex = index - captures; + Token name; + if (lambda.hasOnlyParamNames()) { + name = lambda.paramNames.get(sourceIndex) instanceof NameExpression expression + ? expression.getNameToken() : null; + } else { + Parameter parameter = lambda.params.get(sourceIndex); + parameter.setResolvedTarget(register); + name = parameter.getNameToken(); + } + if (name != null) { + parameters.put(sourceIndex, new ParameterBinding(name, register)); + } + } + } + + private MethodStructure method; + private final Map captureOrigins = new IdentityHashMap<>(); + private final Map parameters = new LinkedHashMap<>(); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java index 06b2258d3c3..3968d05c307 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java @@ -13,7 +13,6 @@ import java.util.stream.Collectors; import org.xvm.asm.Argument; -import org.xvm.asm.Assignment; import org.xvm.asm.Component; import org.xvm.asm.ConstantPool; import org.xvm.asm.ErrorListener; @@ -22,6 +21,7 @@ import org.xvm.asm.MethodStructure.Code; import org.xvm.asm.MultiMethodStructure; import org.xvm.asm.Register; +import org.xvm.asm.Assignment; import org.xvm.asm.constants.DynamicFormalConstant; import org.xvm.asm.constants.FormalConstant; @@ -55,11 +55,12 @@ import org.xvm.util.Handy; import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.util.Handy.indentLines; + import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; -import static org.xvm.util.Handy.indentLines; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; /** * Lambda expression is an inlined function. This version uses parameters that are assumed to be @@ -113,6 +114,25 @@ public MethodStructure getLambda() { return m_lambda; } + /** The written arrow token, for source-position consumers. */ + public Token getOperator() { + return operator; + } + + /** + * @return the source bindings retained by this lambda's compilation context, or null before + * its generated method has been validated + */ + public LambdaBindings getSourceBindings() { + return m_ctxLambda == null || !m_ctxLambda.f_bindings.isFor(m_lambda) + ? null : m_ctxLambda.f_bindings; + } + + /** Record a generated parameter through the context that owns its source associations. */ + void bindSourceParameter(int index, Register register) { + m_ctxLambda.f_bindings.bind(this, m_ctxLambda, index, register); + } + /** * @return true iff the lambda declaration has parameters */ @@ -280,8 +300,8 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { // passed to the lambda (via FBIND) // - so now, at this point, we have the signature, we have the method structure, and we just // have to emit the code corresponding to the lambda - if (catchUpChildren(errs)) { - if (!body.compileMethod(method.createCode(), errs)) { + if (catchUpChildren(errs, mgr.getInvocationBindings())) { + if (!body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings())) { mgr.deferChildren(); } } @@ -589,8 +609,8 @@ protected boolean collectParamNamesAndTypes(TypeConstant[] atypeReqParams, if (hasOnlyParamNames()) { if (atypeReqParams == null) { - errs.error(Compiler.PARAMETER_TYPES_REQUIRED, in(getSource(), paramNames.get(0).getStartPosition(), - paramNames.get(cParams-1).getEndPosition())); + errs.error(Compiler.PARAMETER_TYPES_REQUIRED, in(getSource(), paramNames.getFirst().getStartPosition(), + paramNames.getLast().getEndPosition())); fValid = false; } @@ -833,9 +853,9 @@ protected void discard(boolean fRecurse) { } @Override - public AstNode clone() { + public LambdaExpression copyTree() { // the reference to the lambda's method structure should not be a part of the cloned state - LambdaExpression that = (LambdaExpression) super.clone(); + LambdaExpression that = (LambdaExpression) super.copyTree(); that.m_lambda = null; return that; } @@ -1401,6 +1421,8 @@ protected void initNameMap(Map mapByName) { } } + private final LambdaBindings f_bindings = new LambdaBindings(); + private final TypeConstant[] f_atypeParams; private final String[] f_asParams; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java index 8fd1c101c6c..fe94f2a62d2 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java @@ -41,13 +41,14 @@ import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.Silence.PROBE; -import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; - import static org.xvm.util.Handy.appendString; import static org.xvm.util.Handy.indentLines; +import static org.xvm.asm.ErrorListener.in; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A method declaration. */ @@ -135,6 +136,11 @@ public MethodDeclarationStatement(MethodStructure struct, StatementBlock body) { // ----- accessors ----------------------------------------------------------------------------- + /** @return the written documentation text, or null when no documentation comment is present */ + public String getDocumentation() { + return TypeCompositionStatement.extractDocumentation(doc); + } + /** * @return true iff this statement represents a constructor */ @@ -212,7 +218,7 @@ private void bindConstructor(MethodDeclarationStatement stmtConstructor) { /** * @return the simple name for this statement */ - private String getName() { + public String getName() { if (name != null) { return name.getValueText(); } @@ -223,6 +229,13 @@ private String getName() { : struct.getName(); } + /** + * @return the declared name token, or null for an implicit method without a source name + */ + public Token getNameToken() { + return name; + } + @Override public Access getDefaultAccess() { // methods are *not* taking the parent's access by default @@ -544,6 +557,13 @@ public void resolveNames(StageMgr mgr, ErrorListener errs) { MethodStructure method = (MethodStructure) getComponent(); if (method != null) { + if (typeParams != null) { + for (int i = 0; i < typeParams.size(); ++i) { + typeParams.get(i).setResolvedTarget( + method.getParam(i).asTypeParameterConstant(method.getIdentityConstant())); + } + } + // methods are opaque, so everything inside the curlies can be deferred until we get to the // validateContent() stage mgr.processChildrenExcept((child) -> child == body); @@ -591,7 +611,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { // method children are all deferred up until this stage, so we have to "catch them up" at // this point, recreating the various compiler stages here MethodStructure method = (MethodStructure) getComponent(); - if (method == null || !catchUpChildren(errs)) { + if (method == null || !catchUpChildren(errs, mgr.getInvocationBindings())) { // we are in an error state; we choose not to proceed with compilation mgr.deferChildren(); return; @@ -896,7 +916,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { if (cDefaults > 0) { StatementBlock block = adopt(new StatementBlock(Collections.emptyList())); - RootContext ctxMethod = new RootContext(block, method); + RootContext ctxMethod = new RootContext(block, method, mgr.getInvocationBindings()); Context ctx = ctxMethod.validatingContext(); int cParamExprs = params.size(); @@ -933,7 +953,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { } protected void compileBody(StageMgr mgr, MethodStructure method, ErrorListener errs) { - if (body != null && !body.compileMethod(method.createCode(), errs)) { + if (body != null && !body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings())) { // the compilation has failed; no further progress is possible mgr.deferChildren(); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java index a4b930d7827..b37b9fcb9bf 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameExpression.java @@ -226,6 +226,11 @@ public NameExpression(Expression left, Token amp, Token name, ListThe compiler decides this while it resolves the name, and the answer stays on the node + * afterwards; this only makes it readable from outside the compiler. It is what a host needs + * in order to answer "where is this declared", which cannot be answered from the text: two + * names spelled the same can refer to different things, and the same thing can be referred to + * by different names. + * + *

The result is a {@link Constant} - usually an + * {@link org.xvm.asm.constants.IdentityConstant} - when the name refers to something the + * module declares, and a {@link org.xvm.asm.Register} when it refers to a local variable, + * which has no identity beyond the method it lives in. + * + * @return what the name resolved to, or null if it was never resolved, which is the ordinary + * case in source that does not compile + */ + public Argument getResolvedTarget() { + return m_arg; + } + /** * @return true iff there are any trailing type expressions */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java index 1e990fb3f9f..8ae0613b4a8 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NameResolver.java @@ -1,5 +1,6 @@ package org.xvm.compiler.ast; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -271,6 +272,7 @@ private Result resolveStage(ErrorListener errs) { } } m_constantFirst = m_constant; + m_resolvedNames.add(m_constant); // first name has been resolved m_stage = Stage.RESOLVE_DOT_NAME; @@ -323,6 +325,7 @@ private Result resolveStage(ErrorListener errs) { case RESOLVED: // the component resolved the name; advance to the next one + m_resolvedNames.add(m_constant); m_sName = m_iter.hasNext() ? m_iter.next() : null; break; @@ -564,6 +567,7 @@ private Result resolveFormalDotName(TypeConstant typeConstraint, ErrorListener e case RESOLVED: // the component resolved the name; advance to the next one + m_resolvedNames.add(m_constant); m_sName = m_iter.hasNext() ? m_iter.next() : null; return Result.RESOLVED; @@ -572,6 +576,13 @@ private Result resolveFormalDotName(TypeConstant typeConstraint, ErrorListener e } } + /** + * @return resolved name segments in source order, without resuming resolution + */ + public List getResolvedNames() { + return List.copyOf(m_resolvedNames); + } + /** * @return the result of the NameResolver thus far; the ERROR and RESOLVED states are the * terminal states @@ -812,6 +823,9 @@ private enum TypeMode {TYPE, FORMAL_TYPE} */ private Constant m_constantFirst; + /** Resolved prefixes in source order, retained even if a later segment cannot resolve. */ + private final List m_resolvedNames = new ArrayList<>(); + /** * The constant representing what the node has thus far resolved to. */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java index 51e6ec73361..b2ab7607406 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.IntStream; import org.xvm.asm.Argument; import org.xvm.asm.ClassStructure; @@ -37,11 +38,11 @@ import org.xvm.util.Severity; +import static org.xvm.compiler.Lexer.isValidQualifiedModule; + import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; -import static org.xvm.compiler.Lexer.isValidQualifiedModule; - /** * A type expression specifies a named type with optional parameters. */ @@ -155,6 +156,39 @@ public String[] getNames() { return as; } + /** + * @return the final name token, excluding qualifiers and type arguments, or null if absent + */ + public Token getNameToken() { + return names == null || names.isEmpty() ? null : names.getLast(); + } + + /** A written segment and its resolved target, or null when resolution did not reach it. */ + public record NameBinding(Token name, Constant target) {} + + /** + * @return each written name's binding, without initiating or resuming name resolution + */ + public List getNameBindings() { + var tokens = new ArrayList(); + collectNameTokens(tokens); + List resolved = m_resolver == null ? List.of() : m_resolver.getResolvedNames(); + return IntStream.range(0, tokens.size()).mapToObj(i -> { + Constant target = i < resolved.size() ? resolved.get(i) + : i == tokens.size() - 1 ? m_constId : null; + return new NameBinding(tokens.get(i), target); + }).toList(); + } + + private void collectNameTokens(List tokens) { + if (left instanceof NamedTypeExpression expression) { + expression.collectNameTokens(tokens); + } + if (names != null) { + tokens.addAll(names); + } + } + public Constant getIdentityConstant() { Constant constId = m_constId; if (constId == null) { @@ -699,7 +733,7 @@ private TypeConstant[] validateParameters(Context ctx, List list * Considering the containing class, calculate a type for the specified target constant in the * absence of explicitly provided type parameters. * - * Note, the returned type should not be parameterized; it will be done later by the caller. + *

Note, the returned type should not be parameterized; it will be done later by the caller. * * @return a resulting type */ @@ -965,11 +999,11 @@ public Argument generateArgument(Context ctx, Code code, boolean fLocalPropOk, E // ----- AstNode methods ----------------------------------------------------------------------- @Override - public AstNode clone() { - NamedTypeExpression that = (NamedTypeExpression) super.clone(); + public NamedTypeExpression copyTree() { + NamedTypeExpression that = (NamedTypeExpression) super.copyTree(); // the "m_exprDynamic" is not a child and has to be handled manually if (m_exprDynamic != null) { - that.m_exprDynamic = (NameExpression) m_exprDynamic.clone(); + that.m_exprDynamic = m_exprDynamic.copyTree(); } return that; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java index ad19bf9e511..a552e6961bb 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java @@ -6,6 +6,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.stream.Collectors; import org.xvm.asm.Annotation; @@ -38,6 +39,7 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; @@ -45,11 +47,12 @@ import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.util.Handy.indentLines; + import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; -import static org.xvm.util.Handy.indentLines; +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; /** * "New object" expression. @@ -111,6 +114,22 @@ public int getDimensionCount() { return Math.max(dims, 0); } + /** + * @return a snapshot of the written constructor arguments, including array dimensions + */ + public List getArguments() { + return List.copyOf(args); + } + + /** + * Written anonymous body before an anonymous class owns it. It is deliberately not a compiler + * child yet: registering/validating it requires the construction's inferred type and context. + * Syntax readers may inspect this borrowed node; they must not adopt or validate it themselves. + */ + public Optional getUnregisteredBody() { + return anon == null ? Optional.ofNullable(body) : Optional.empty(); + } + @Override public boolean isAutoNarrowingAllowed(TypeExpression type) { // auto-narrowing is allowed for type parameters, but not the type itself @@ -135,12 +154,12 @@ protected Field[] getChildFields() { // ----- AstNode methods ----------------------------------------------------------------------- @Override - public AstNode clone() { - NewExpression that = (NewExpression) super.clone(); + public NewExpression copyTree() { + NewExpression that = (NewExpression) super.copyTree(); // the "body" is not a child and has to be handled manually if (body != null) { that.body = anon == null - ? (StatementBlock) body.clone() + ? body.copyTree() : that.anon.body; } return that; @@ -171,6 +190,31 @@ public AnonInnerClassContext getCaptureContext() { return m_ctxCapture; } + /** + * @return source bindings collected by capture analysis, or null before it completes + */ + public AnonymousClassBindings getSourceBindings() { + return m_captureBindings != null && m_captureBindings.isFor(this) ? m_captureBindings : null; + } + + void bindSourceCapture(String name, Register captured) { + m_captureBindings.bind(name, captured); + } + + /** + * Obtain source origins for the generated properties of a validated anonymous class. + * This reads existing capture data; it does not resume compilation or retain another cache. + * + * @return a snapshot of captured properties and their enclosing source registers + */ + public Map getCaptureOrigins() { + if (!isValidated() || anon == null || anon.getComponent() == null || getSourceBindings() == null) { + return Map.of(); + } + + return m_captureBindings.propertyOrigins(anon.getComponent()); + } + // ----- compilation (Expression) -------------------------------------------------------------- @Override @@ -239,6 +283,9 @@ protected TypeFit calcFit(Context ctx, TypeConstant typeIn, TypeConstant typeOut @Override protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListener errs) { + var bindings = ctx.getInvocationBindings(); + var writtenArgs = bindings.isEnabled() ? List.copyOf(args) : List.of(); + bindings.begin(this); ConstantPool pool = pool(); TypeConstant typeSuper = null; // the super class type of the anon inner class TypeConstant typeTarget = null; // the type to look for a constructor at (could be private) @@ -565,8 +612,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } // for a regular or virtual child construction, the target type must be new-able - if ((plan == Plan.Regular || plan == Plan.Child) && - !infoTarget.isNewable(false, errsTemp)) { + if ((plan == Plan.Regular || plan == Plan.Child) && !infoTarget.isNewable(false, errsTemp)) { String sTarget = infoTarget.getType().removeAccess().getValueString(); infoTarget.reportNotNewable(sTarget, null, false, errsTemp); errsTemp.merge(); @@ -657,6 +703,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe args = listArgs; } + var sourceArguments = InvocationBinding.arguments(writtenArgs, listArgs); if (validateExpressions(ctx, listArgs, idConstruct.getRawParams(), errs) == null) { return null; } @@ -705,10 +752,10 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // nested contexts in which the captured variables were declared go through their exit() // logic (as the variables go out of scope in the method body that contains this // NewExpression); for now, store off the data from the capture context - m_mapCapture = ctxAnon.getCaptureMap(); - m_mapRegisters = ctxAnon.ensureRegisterMap(); - m_fInstanceChild = ctxAnon.isInstanceChild(); - m_ctxCapture = null; + m_mapCapture = ctxAnon.getCaptureMap(); + m_captureBindings = new AnonymousClassBindings(this, ctxAnon.ensureRegisterMap()); + m_fInstanceChild = ctxAnon.isInstanceChild(); + m_ctxCapture = null; // make sure the capture names don't collide ClassStructure clzAnon = ctxAnon.getThisClass(); @@ -723,6 +770,11 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe m_plan = plan; Expression exprResult = finishValidation(ctx, typeRequired, typeResult, TypeFit.Fit, null, errs); + if (exprResult == this && bindings.isEnabled()) { + MethodConstant selected = constructor.getIdentityConstant(); + sourceArguments.ifPresent(arguments -> bindings.record(this, + new InvocationBinding(selected, selected.getSignature(), arguments))); + } clearAnonTypeInfos(); return exprResult; } @@ -1120,7 +1172,7 @@ private void ensureInnerClass(Context ctx, AnonPurpose purpose, ErrorListener er m_ctxCapture = new AnonInnerClassContext(ctx); - catchUpChildren(errs); + catchUpChildren(errs, ctx.getInvocationBindings()); if (purpose != AnonPurpose.CaptureAnalysis) { // the context is ONLY retained to provide capture information @@ -1255,11 +1307,11 @@ private Argument[] addCaptures(Code code, Argument[] aOldArgs) { anon.getComponent().setStatic(!m_fInstanceChild); // if nothing else is captured, then we're done - Map mapCapture = m_mapCapture; - Map mapRegisters = m_mapRegisters; + Map mapCapture = m_mapCapture; if (mapCapture == null || mapCapture.isEmpty()) { return aOldArgs; } + Map mapRegisters = m_captureBindings.registers(); // we're going to replace the constructor by creating a new constructor that calls the old // one, but that first stores off all the passed-in binding values @@ -1390,18 +1442,18 @@ protected boolean isCapture(String sCaptureName) { * @return the type of the value (not the Ref or Var, if implicit deref is used) */ protected TypeConstant getCaptureType(String sCaptureName) { - assert m_mapRegisters.containsKey(sCaptureName); + assert m_captureBindings.registers().containsKey(sCaptureName); - return m_mapRegisters.get(sCaptureName).getType(); + return m_captureBindings.registers().get(sCaptureName).getType(); } /** * @return true iff the captured variable has been marked as being effectively final */ protected boolean isCaptureFinal(String sCaptureName) { - assert m_mapRegisters.containsKey(sCaptureName); + assert m_captureBindings.registers().containsKey(sCaptureName); - return m_mapRegisters.get(sCaptureName).isEffectivelyFinal(); + return m_captureBindings.registers().get(sCaptureName).isEffectivelyFinal(); } /** @@ -1410,8 +1462,8 @@ protected boolean isCaptureFinal(String sCaptureName) { protected boolean isImplicitDeref(String sCaptureName) { assert m_mapCapture.containsKey(sCaptureName); - Register reg = m_mapRegisters.get(sCaptureName); - Boolean FVar = m_mapCapture .get(sCaptureName); + Register reg = m_captureBindings.registers().get(sCaptureName); + Boolean FVar = m_mapCapture .get(sCaptureName); return FVar || !reg.isEffectivelyFinal(); } @@ -1571,9 +1623,10 @@ private enum AnonPurpose {None, RoughDraft, CaptureAnalysis, Actual} */ private transient Map m_mapCapture; /** - * A map from variable name to register, built by the anonymous inner class context. + * The capture-analysis result: enclosing registers and source origins for their local reads. + * Replaces the former name-to-register map; created when capture analysis completes. */ - private transient Map m_mapRegisters; + private transient AnonymousClassBindings m_captureBindings; /** * The construction plan: diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Parameter.java b/javatools/src/main/java/org/xvm/compiler/ast/Parameter.java index e4ced54b2bd..2eeb335cc75 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Parameter.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Parameter.java @@ -1,9 +1,11 @@ package org.xvm.compiler.ast; -import org.xvm.compiler.Token; - import java.lang.reflect.Field; +import org.xvm.asm.Argument; + +import org.xvm.compiler.Token; + /** * A parameter type and name, with an optional default value. */ @@ -27,6 +29,11 @@ public Parameter(TypeExpression type, Token name, Expression value) { // ----- accessors ----------------------------------------------------------------------------- + @Override + public Parameter copyTree() { + return (Parameter) super.copyTree(); + } + public TypeExpression getType() { return type; } @@ -39,6 +46,18 @@ public Token getNameToken() { return name; } + /** + * @return the source parameter's register, constructor-property identity or formal type + * constant, or null if that source association has not been established + */ + public Argument getResolvedTarget() { + return m_arg; + } + + void setResolvedTarget(Argument arg) { + m_arg = arg; + } + public Expression getValue() { return value; } @@ -101,5 +120,7 @@ public String getDumpDesc() { protected Token name; protected Expression value; + private transient Argument m_arg; + private static final Field[] CHILD_FIELDS = fieldsForNames(Parameter.class, "type", "value"); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java index 40348137cd0..4ac08f3b43b 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java @@ -39,6 +39,8 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; import org.xvm.compiler.Constants; +import org.xvm.compiler.InitializerBinding; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Token; import org.xvm.util.ListMap; @@ -111,6 +113,13 @@ public String getName() { return name.getValueText(); } + /** + * @return the property name token + */ + public Token getNameToken() { + return name; + } + public TypeExpression getType() { return type; } @@ -420,8 +429,11 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { // if it could be discarded and replaced with a constant // IMPORTANT NOTE: this goes forward BEYOND validation, so the caller's context // must be ready to resolve the corresponding names (e.g. see NewExpression) + var bindings = mgr.getInvocationBindings().isEnabled() + ? new InvocationBinding.Collector() : InvocationBinding.Collector.NONE; + var written = bindings.isEnabled() ? InitializerBinding.writtenNodes(stmtInit) : List.of(); ErrorListener errsTmp = errs.branch(this); - if (!(new StageMgr(stmtInit, Stage.Emitted, errsTmp).fastForward(10)) || + if (!(new StageMgr(stmtInit, Stage.Emitted, errsTmp, bindings).fastForward(10)) || errsTmp.hasSeriousErrors()) { stmtClone.discardInitializer(methodInit); stmtInit.discard(true); @@ -488,6 +500,13 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { prop.setInitialValue(null); } + // Only successful constant folding publishes clone-free facts. Real methods + // validate their source-owned initializer below; failed/revisited probes publish none. + if (!fMethodRequired && bindings.isEnabled() && !errs.isAbortDesired() && !errs.hasSeriousErrors()) { + mgr.getInvocationBindings().record(this, + InitializerBinding.capture(written, bindings.finishFacts(List.of(stmtInit)))); + } + // at this point, we are done with the "test clone" stmtClone.discardInitializer(methodInit); stmtInit.discard(true); @@ -503,7 +522,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { value = null; // "catch up" the newly created initializer to our stage - if (!new StageMgr(initializer, Stage.Validated, errs).fastForward(10)) { + if (!new StageMgr(initializer, Stage.Validated, errs, mgr.getInvocationBindings()).fastForward(10)) { // basically an assertion log(errs, Severity.FATAL, Compiler.FATAL_ERROR, initializer); } @@ -559,8 +578,8 @@ private boolean validateRefAnnotations(Annotation[] aAnno, ClassStructure clz, /** * Validate the specified property annotations. * - * Note: this method is similar to {@link TypeCompositionStatement#validateAnnotations} logic, - * but differs in the way that it could force the node revisit. + *

This method is similar to the annotation validation in {@link TypeCompositionStatement}, + * but differs in the way that it can force the node to be revisited. * * @param aAnno the annotations * @param typeProp the annotated property type diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java index 8f0bb22379a..e5a2692d5f5 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java @@ -1,13 +1,15 @@ package org.xvm.compiler.ast; +import org.jetbrains.annotations.NotNull; + +import static java.util.Objects.requireNonNull; + import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.function.Predicate; -import org.jetbrains.annotations.NotNull; - import org.xvm.asm.Component; import org.xvm.asm.ErrorListener; @@ -16,13 +18,12 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; import org.xvm.compiler.CompilerException; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.ast.AstNode.ChildIterator; import org.xvm.util.Severity; -import static java.util.Objects.requireNonNull; - /** * A Stage Manager is used to shepherd the AST nodes through their various stages. */ @@ -36,12 +37,19 @@ public class StageMgr { * @param errs the error listener to log to */ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs) { + this(node, stageTarget, errs, InvocationBinding.Collector.NONE); + } + + /** Progress a node while preserving the compilation attempt's call-fact collector. */ + public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, + InvocationBinding.Collector bindings) { assert node != null; assert stageTarget != null && stageTarget.isTargetable(); m_listRevisit = Collections.singletonList(node); m_target = stageTarget; - m_errs = requireNonNull(errs, "errs"); + f_errs = requireNonNull(errs, "errs"); + f_bindings = requireNonNull(bindings, "bindings"); } /** @@ -53,12 +61,19 @@ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs) { * @param errs the error listener to log to */ public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs) { + this(list, stageTarget, errs, InvocationBinding.Collector.NONE); + } + + /** Progress child nodes using the same collector as their enclosing compilation. */ + public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, + InvocationBinding.Collector bindings) { assert list != null && !list.isEmpty(); assert stageTarget != null && stageTarget.isTargetable(); m_listRevisit = list; m_target = stageTarget; - m_errs = requireNonNull(errs, "errs"); + f_errs = requireNonNull(errs, "errs"); + f_bindings = requireNonNull(bindings, "bindings"); } /** @@ -77,7 +92,7 @@ public boolean isComplete() { * target stage */ public boolean processComplete() { - ErrorListener errs = m_errs; + ErrorListener errs = f_errs; if (errs.isAbortDesired()) { return false; } @@ -164,7 +179,11 @@ public void markLastAttempt() { * @return this Stage Manager's error list */ public ErrorListener getErrorListener() { - return m_errs; + return f_errs; + } + + public InvocationBinding.Collector getInvocationBindings() { + return f_bindings; } /** @@ -198,7 +217,7 @@ protected boolean processInternal(AstNode node) { node.setStage(stageTarget.getTransitionStage()); switch (stageTarget) { case Registered: - node.registerStructures(this, m_errs); + node.registerStructures(this, f_errs); break; case Loaded: @@ -207,15 +226,15 @@ protected boolean processInternal(AstNode node) { return true; case Resolved: - node.resolveNames(this, m_errs); + node.resolveNames(this, f_errs); break; case Validated: - node.validateContent(this, m_errs); + node.validateContent(this, f_errs); break; case Emitted: - node.generateCode(this, m_errs); + node.generateCode(this, f_errs); break; default: @@ -444,7 +463,9 @@ private List takeRevisitList() { /** * Error list to log processing errors to. */ - private final ErrorListener m_errs; + private final ErrorListener f_errs; + + private final InvocationBinding.Collector f_bindings; /** * The current node being processed if processing is occurring. diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java index 3da76d830d4..b9156d99c5f 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java @@ -20,6 +20,11 @@ public abstract class Statement extends AstNode { // ----- accessors ----------------------------------------------------------------------------- + @Override + public Statement copyTree() { + return (Statement) super.copyTree(); + } + @Override protected boolean usesSuper() { for (AstNode node : children()) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java index 4a7433f18b8..818b3560592 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.Set; import org.xvm.asm.Argument; @@ -48,6 +49,7 @@ import org.xvm.asm.op.Var_IN; import org.xvm.compiler.Compiler; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; @@ -100,6 +102,11 @@ public StatementBlock(List stmts, Source source, long lStartPos, long // ----- accessors ----------------------------------------------------------------------------- + @Override + public StatementBlock copyTree() { + return (StatementBlock) super.copyTree(); + } + public List getStatements() { return stmts; } @@ -293,7 +300,12 @@ public boolean isTerminatedAbnormally() { * @return true if nothing occurred during the compilation that should stop further progress */ public boolean compileMethod(Code code, ErrorListener errs) { - return compileMethod(new RootContext(this, code.getMethodStructure()), code, errs); + return compileMethod(code, errs, InvocationBinding.Collector.NONE); + } + + /** Compile a method with the collector owned by its enclosing compilation attempt. */ + public boolean compileMethod(Code code, ErrorListener errs, InvocationBinding.Collector bindings) { + return compileMethod(new RootContext(this, code.getMethodStructure(), bindings), code, errs); } /** @@ -359,6 +371,7 @@ protected Statement validateImpl(Context ctx, ErrorListener errs) { Assignment asnVar = ctx.getVarAssignment(sName); Register regVal = param.deref(regVar, method); + exprLambda.bindSourceParameter(param.getIndex(), regVal); ctx.ensureNameMap().put(sName, regVal); // shadow using the capture ctx.setVarAssignment(sName, asnVar); // ... and copy its assignment } @@ -543,9 +556,20 @@ public String toString() { public static class RootContext extends Context { public RootContext(StatementBlock stmt, MethodStructure method) { + this(stmt, method, InvocationBinding.Collector.NONE); + } + + public RootContext(StatementBlock stmt, MethodStructure method, + InvocationBinding.Collector bindings) { super(null, false); - f_stmt = stmt; - f_method = method; + f_stmt = stmt; + f_method = method; + f_bindings = Objects.requireNonNull(bindings, "bindings"); + } + + @Override + public InvocationBinding.Collector getInvocationBindings() { + return f_bindings; } @Override @@ -815,6 +839,7 @@ protected Argument resolveRegularName(Context ctxFrom, String sName, Token name, Register reg = createRegister(type, sName); mapByName.put(sName, reg); ensureCaptureVars().put(sName, reg); + exprNew.bindSourceCapture(sName, reg); // TODO REVIEW CP // we need to know the definite assignment of the variable at the point @@ -1228,6 +1253,20 @@ protected void initNameMap(Map mapByName) { } mapByName.put(sName, reg); + // Retain the binding on the source parameter for hosts reading the validated + // tree. Generated methods and captured parameters may have no source parameter. + if (!param.isTypeParameter() && + f_stmt.getParent() instanceof MethodDeclarationStatement declaration && + declaration.getComponent() == method && declaration.params != null) { + int index = i - method.getTypeParamCount(); + if (index < declaration.params.size()) { + declaration.params.get(index).setResolvedTarget(reg); + } + } else if (f_stmt.getParent() instanceof LambdaExpression lambda && + lambda.getLambda() == method) { + lambda.bindSourceParameter(i, reg); + } + // the variable has been definitely assigned, but not multiple times (i.e. it's // still effectively final) mapAssigned.put(sName, Assignment.AssignedOnce); @@ -1398,10 +1437,11 @@ private Map ensureCaptureVars() { return map; } - private final StatementBlock f_stmt; - private final MethodStructure f_method; - private Context m_ctxValidating; - private boolean m_fEmitting; + private final StatementBlock f_stmt; + private final MethodStructure f_method; + private final InvocationBinding.Collector f_bindings; + private Context m_ctxValidating; + private boolean m_fEmitting; /** * A lazily created mapping of captured variables that is collected during the validation diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java index e10a8b28fcb..ea3c55d7d20 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java @@ -57,13 +57,14 @@ import org.xvm.asm.op.Construct_N; import org.xvm.asm.op.JumpNType; import org.xvm.asm.op.L_Get; -import org.xvm.asm.op.L_Set; import org.xvm.asm.op.Label; -import org.xvm.asm.op.Return_0; import org.xvm.asm.op.SynInit; +import org.xvm.asm.op.L_Set; +import org.xvm.asm.op.Return_0; import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; @@ -72,8 +73,8 @@ import org.xvm.compiler.ast.CompositionNode.Default; import org.xvm.compiler.ast.CompositionNode.Delegates; import org.xvm.compiler.ast.CompositionNode.Extends; -import org.xvm.compiler.ast.CompositionNode.Import; import org.xvm.compiler.ast.CompositionNode.Incorporates; +import org.xvm.compiler.ast.CompositionNode.Import; import org.xvm.compiler.ast.Context.Branch; import org.xvm.compiler.ast.StatementBlock.RootContext; @@ -82,10 +83,6 @@ import org.xvm.util.ListSet; import org.xvm.util.Severity; -import static org.xvm.asm.ErrorListener.Silence.PROBE; -import static org.xvm.asm.ErrorListener.in; -import static org.xvm.asm.ErrorListener.silent; - import static org.xvm.compiler.Constants.ECSTASY_MODULE; import static org.xvm.compiler.Constants.X_PKG_IMPORT; import static org.xvm.compiler.Lexer.CR; @@ -97,6 +94,11 @@ import static org.xvm.util.Handy.appendString; import static org.xvm.util.Handy.indentLines; +import static org.xvm.asm.ErrorListener.in; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + /** * A type declaration. */ @@ -104,6 +106,14 @@ public class TypeCompositionStatement extends ComponentStatement { // ----- constructors -------------------------------------------------------------------------- + /** Written syntax for recovery subclasses that deliberately register no component. */ + protected TypeCompositionStatement(Source source, Token category, Token name, long start, long end) { + super(start, end); + this.source = source; + this.category = category; + this.name = name; + } + public TypeCompositionStatement( Source source, long lStartPos, @@ -232,6 +242,13 @@ public Token getCategory() { return category; } + /** + * @return the type's simple name token + */ + public Token getNameToken() { + return name; + } + public String getName() { if (category.getId() == Token.Id.MODULE) { StringBuilder sb = new StringBuilder(); @@ -427,8 +444,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { // validate the module name String sModule = getName(); if (!isValidQualifiedModule(sModule)) { - errs.fatal(Compiler.MODULE_BAD_NAME, in(source, qualified.get(0).getStartPosition(), - qualified.get(qualified.size()-1).getEndPosition()), sModule); + errs.fatal(Compiler.MODULE_BAD_NAME, in(source, qualified.getFirst().getStartPosition(), + qualified.getLast().getEndPosition()), sModule); return; } @@ -473,6 +490,17 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { case CLASS, INTERFACE, SERVICE, CONST, ENUM, ENUM_VAL, ANNOTATION, MIXIN: if (container != null && container.isClassContainer()) { + // The assembler supports conditional siblings, so createClass() can accept two + // unconditional declarations too. Reject that source conflict before name + // resolution can mistake their CompositeComponent for a ClassStructure. + if (constCond == null && container.getCondition() == null && + container.getChild(sName) instanceof ClassStructure sibling && + sibling.getCondition() == null) { + name.log(errs, getSource(), Severity.ERROR, Compiler.DUPLICATE_NAME, sName); + mgr.deferChildren(); + return; + } + Format format = switch (category.getId()) { case CLASS -> Format.CLASS; case INTERFACE -> Format.INTERFACE; @@ -798,7 +826,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { TypeConstant constType = exprType == null ? pool.typeObject() : exprType.ensureTypeConstant(); - component.addTypeParam(sParam, constType); + param.setResolvedTarget(component.addTypeParam(sParam, constType) + .getIdentityConstant()); } else { log(errs, Severity.ERROR, Compiler.DUPLICATE_TYPE_PARAM, sName); } @@ -1317,7 +1346,9 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { } // recursively register structures - mgr.processChildren(); + if (!mgr.isChildrenDeferred()) { + mgr.processChildren(); + } // if there are any constructor parameters, then that implies the existence both of // properties and of a constructor; we will handle the constructor creation later (the @@ -1349,7 +1380,12 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { // "Registered" stage (it will create the property structure) body.addStatement(prop); new StageMgr(prop, Stage.Registered, errs).fastForward(1); - } else if (!(child instanceof PropertyStructure)) { + if (prop.getComponent() != null) { + param.setResolvedTarget(prop.getComponent().getIdentityConstant()); + } + } else if (child instanceof PropertyStructure prop) { + param.setResolvedTarget(prop.getIdentityConstant()); + } else { // the parameter implies a property, but we found something else instead param.log(errs, Severity.ERROR, Compiler.NAME_COLLISION, sParam); } @@ -1821,7 +1857,9 @@ public void resolveNames(StageMgr mgr, ErrorListener errs) { } } - mgr.processChildren(); + if (!mgr.isChildrenDeferred()) { + mgr.processChildren(); + } Map mapChildren = component.getChildByNameMap(); MultiMethodStructure constructors = (MultiMethodStructure) mapChildren.get("construct"); @@ -2310,7 +2348,8 @@ private Map findImplicitConstraint(ClassStructure clz, Str if (typeConstraint != null) { if (typeConstraint.equals(pool.typeObject())) { // report errors only at the "top" level - mapConstraints = findImplicitConstraint(clzContrib, sName, mapConstraints, fAllowInto, silent(PROBE)); + mapConstraints = findImplicitConstraint(clzContrib, sName, mapConstraints, fAllowInto, + silent(PROBE)); } else { if (mapConstraints == null) { mapConstraints = new ListMap<>(); @@ -2362,7 +2401,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { break ValidateShorthand; } - RootContext ctxConstruct = createConstructorContext(constructor); + RootContext ctxConstruct = createConstructorContext(constructor, mgr.getInvocationBindings()); Context ctx = ctxConstruct.validatingContext(); if (constructorParams != null && !constructorParams.isEmpty()) { @@ -2492,12 +2531,13 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { /** * A simple helper to create a new context for shorthand constructor processing. */ - private RootContext createConstructorContext(MethodStructure constructor) { + private RootContext createConstructorContext(MethodStructure constructor, + InvocationBinding.Collector bindings) { StatementBlock blockBody = body; if (body == null) { blockBody = adopt(new StatementBlock(Collections.emptyList())); } - return new RootContext(blockBody, constructor); + return new RootContext(blockBody, constructor, bindings); } /** diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java index 8bd3726dec6..21431724845 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypeExpression.java @@ -27,6 +27,11 @@ public abstract class TypeExpression extends Expression { // ----- type specific functionality ----------------------------------------------------------- + @Override + public TypeExpression copyTree() { + return (TypeExpression) super.copyTree(); + } + @Override public TypeExpression toTypeExpression() { return this; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypedefStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/TypedefStatement.java index e29a0d57cf6..ba48321cf27 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypedefStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypedefStatement.java @@ -34,6 +34,13 @@ public TypedefStatement(Expression cond, Token keyword, TypeExpression type, Tok // ----- accessors ----------------------------------------------------------------------------- + /** + * @return the token that declares this alias + */ + public Token getNameToken() { + return alias; + } + @Override public Access getDefaultAccess() { if (modifier != null) { diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 9c7a03a2340..8e73d5719c2 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -756,6 +756,31 @@ public Node getSourceTree(ErrorListener errs) { return sourceNode; } + /** + * Read the source trees retained by the last loading attempt, including available syntax + * when {@link #getSourceTree(ErrorListener)} could not assemble the module. This does not load, + * parse, register names or link trees. Synthetic package sources are excluded. + * + * @return a snapshot of the available per-file syntax; these trees may be incomplete and + * must not be used for semantic compilation after a loading/parsing error + */ + public List getParsedSources() { + List sources = new ArrayList<>(); + collectParsedSources(sourceNode, sources); + return List.copyOf(sources); + } + + private void collectParsedSources(Node node, List sources) { + if (node instanceof DirNode dir) { + collectParsedSources(dir.sourceNode(), sources); + dir.classNodes().values().forEach(file -> collectParsedSources(file, sources)); + dir.packageNodes().forEach(pkg -> collectParsedSources(pkg, sources)); + } else if (node instanceof FileNode file && file.file() != null && + file.ast() instanceof StatementBlock block) { + sources.add(block); + } + } + /** * Represents either a module/package or a class source node. */ diff --git a/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java new file mode 100644 index 00000000000..30d265bb924 --- /dev/null +++ b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java @@ -0,0 +1,144 @@ +package org.xvm.api; + +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; + +import java.util.function.Supplier; +import java.util.function.UnaryOperator; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ConstantPool; +import org.xvm.asm.FileStructure; + +import org.xvm.compiler.InvocationBinding; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.InvocationExpression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.StatementBlock; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Publication must preserve distinct syntax identities even when an AST subclass adds equality. */ +public class BindingIdentitySnapshotTest { + @Test + public void methodFactsPreserveIdentityAtEveryPublicationBoundary() { + var host = new HostInputs(); + var pool = host.file().getConstantPool(); + var signature = pool.ensureSignatureConstant("call", ConstantPool.NO_TYPES, ConstantPool.NO_TYPES); + var method = pool.ensureMethodConstant(host.file().getModule().getIdentityConstant(), signature); + var binding = new InvocationBinding(method, signature, List.of()); + + assertEquals(new EqualInvocation(), new EqualInvocation()); + List.>>of( + methods -> new InvocationBinding.Facts(methods, Map.of()).methods(), + methods -> host.compilation(methods, Map.of()).callBindings()) + .forEach(copy -> verifySnapshot(copy, EqualInvocation::new, binding)); + } + + @Test + public void functionFactsPreserveIdentityAtEveryPublicationBoundary() { + var host = new HostInputs(); + var type = host.file().getConstantPool().buildFunctionType(ConstantPool.NO_TYPES, ConstantPool.NO_TYPES); + var call = new InvocationBinding.FunctionCall(type, List.of()); + + List.>>of( + functions -> new InvocationBinding.Facts(Map.of(), functions).functions(), + functions -> host.compilation(Map.of(), functions).functionBindings()) + .forEach(copy -> verifySnapshot(copy, EqualInvocation::new, call)); + } + + @Test + public void constructorFactsPreserveIdentityAtEveryPublicationBoundary() { + var host = new HostInputs(); + var pool = host.file().getConstantPool(); + var signature = pool.ensureSignatureConstant("construct", ConstantPool.NO_TYPES, ConstantPool.NO_TYPES); + var method = pool.ensureMethodConstant(host.file().getModule().getIdentityConstant(), signature); + var binding = new InvocationBinding(method, signature, List.of()); + + List.>>of( + constructors -> new InvocationBinding.Facts(Map.of(), Map.of(), constructors).constructors(), + constructors -> new EmbeddingSupport.Compilation(host.file().getModule(), host.file(), + host.tree(), List.of(host.tree()), Map.of(), Map.of(), constructors).constructorBindings()) + .forEach(copy -> verifySnapshot(copy, EqualConstruction::new, binding)); + } + + private static void verifySnapshot(UnaryOperator> copy, Supplier key, V value) { + K first = key.get(); + K second = key.get(); + Map source = new IdentityHashMap<>(); + source.put(first, value); + source.put(second, value); + + Map snapshot = copy.apply(source); + assertEquals(2, snapshot.size()); + assertSame(value, snapshot.get(first)); + assertSame(value, snapshot.get(second)); + assertFalse(snapshot.containsKey(key.get())); + assertThrows(UnsupportedOperationException.class, () -> snapshot.put(key.get(), value)); + assertThrows(UnsupportedOperationException.class, () -> snapshot.remove(first)); + source.clear(); + assertEquals(2, snapshot.size()); + + source.put(null, value); + assertThrows(NullPointerException.class, () -> copy.apply(source)); + source.clear(); + source.put(first, null); + assertThrows(NullPointerException.class, () -> copy.apply(source)); + assertThrows(NullPointerException.class, () -> copy.apply(null)); + } + + private static Token name() { + return new Token(0, 4, Id.IDENTIFIER, "call"); + } + + private static class EqualInvocation extends InvocationExpression { + EqualInvocation() { + super(new NameExpression(name()), false, List.of(), 4); + } + + @Override + public boolean equals(Object other) { + return other instanceof EqualInvocation; + } + + @Override + public int hashCode() { + return 1; + } + } + + private static class EqualConstruction extends NewExpression { + EqualConstruction() { + super(null, new Token(0, 3, Id.NEW, null), null, List.of(), -1, null, 4); + } + + @Override + public boolean equals(Object other) { + return other instanceof EqualConstruction; + } + + @Override + public int hashCode() { + return 1; + } + } + + private record HostInputs(FileStructure file, StatementBlock tree) { + HostInputs() { + this(new FileStructure("IdentityFacts"), new StatementBlock(List.of())); + } + + EmbeddingSupport.Compilation compilation(Map methods, + Map functions) { + return new EmbeddingSupport.Compilation(file.getModule(), file, tree, List.of(tree), methods, functions); + } + + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java b/javatools/src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java new file mode 100644 index 00000000000..58a3565ab5c --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java @@ -0,0 +1,131 @@ +package org.xvm.compiler.ast; + +import java.util.ArrayList; +import java.util.List; +import java.util.NoSuchElementException; + +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Repeatable child views must preserve the compiler's existing one-shot mutation cursor. */ +public class AstChildTraversalTest { + @Test + public void viewSupportsRepeatedAndInterleavedTraversals() { + var first = statement("first"); + var last = statement("last"); + var block = new StatementBlock(new ArrayList<>(List.of(first, last))); + var view = block.childNodes(); + var left = view.iterator(); + var right = view.iterator(); + + assertSame(first, left.next()); + assertSame(first, right.next()); + assertSame(last, right.next()); + assertSame(last, left.next()); + assertFalse(left.hasNext()); + assertFalse(right.hasNext()); + assertEquals(List.of(first, last), snapshot(view)); + assertEquals(List.of(first, last), snapshot(view)); + } + + @Test + public void laterTraversalSeesEditsButAnEarlierSnapshotDoesNot() { + var original = statement("original"); + var replacement = statement("replacement"); + var block = new StatementBlock(new ArrayList<>(List.of(original))); + var view = block.childNodes(); + var before = snapshot(view); + + block.replaceChild(original, replacement); + + assertEquals(List.of(original), before); + assertEquals(List.of(replacement), snapshot(view)); + assertSame(block, replacement.getParent()); + } + + @Test + public void legacyEnhancedForRetainsTheCursorUsedForReplacement() { + var original = statement("original"); + var untouched = statement("untouched"); + var replacement = statement("replacement"); + var block = new StatementBlock(new ArrayList<>(List.of(original, untouched))); + var cursor = block.children(); + + assertSame(cursor, cursor.iterator()); + for (AstNode child : cursor) { + if (child == original) { + cursor.replaceWith(replacement); + } + } + + assertFalse(cursor.iterator().hasNext()); + assertEquals(List.of(replacement, untouched), block.getStatements()); + assertEquals(List.of(replacement, untouched), snapshot(block.childNodes())); + } + + @Test + public void legacyListCursorCanReplaceAndRemoveWithoutSkippingSiblings() { + var first = statement("first"); + var last = statement("last"); + var replacement = statement("replacement"); + var block = new StatementBlock(new ArrayList<>(List.of(first, last))); + var cursor = block.children(); + + assertSame(first, cursor.next()); + cursor.replaceWith(replacement); + assertSame(last, cursor.next()); + cursor.remove(); + + assertFalse(cursor.hasNext()); + assertEquals(List.of(replacement), block.getStatements()); + } + + @Test + public void legacyScalarCursorCanReplaceAndRemoveItsField() { + var statement = statement("original"); + var replacement = name("replacement"); + var cursor = statement.children(); + + assertSame(statement.getExpression(), cursor.next()); + cursor.replaceWith(replacement); + assertSame(replacement, statement.getExpression()); + assertEquals(List.of(replacement), snapshot(statement.childNodes())); + cursor.remove(); + + assertFalse(cursor.hasNext()); + assertEquals(List.of(), snapshot(statement.childNodes())); + } + + @Test + public void emptyViewAndLegacyCursorPreserveExhaustion() { + var leaf = name("leaf"); + var view = leaf.childNodes(); + + assertEquals(List.of(), snapshot(view)); + assertEquals(List.of(), snapshot(view)); + assertThrows(NoSuchElementException.class, () -> view.iterator().next()); + assertThrows(NoSuchElementException.class, () -> leaf.children().next()); + assertThrows(IllegalStateException.class, () -> leaf.children().replaceWith(name("other"))); + } + + private static List snapshot(Iterable children) { + return StreamSupport.stream(children.spliterator(), false).toList(); + } + + private static ExpressionStatement statement(String name) { + return new ExpressionStatement(name(name)); + } + + private static NameExpression name(String name) { + return new NameExpression(new Token(0, name.length(), Id.IDENTIFIER, name)); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/CompositionSourceTest.java b/javatools/src/test/java/org/xvm/compiler/ast/CompositionSourceTest.java new file mode 100644 index 00000000000..f58be53853f --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/CompositionSourceTest.java @@ -0,0 +1,36 @@ +package org.xvm.compiler.ast; + +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +/** Source lookup also applies to parsed trees that failed before child adoption. */ +class CompositionSourceTest { + @Test + void defaultCompositionHasNoSourceUntilAdopted() { + var source = new Source("module Test { enum Kind default(Plain) {Plain, Fancy} }"); + var errors = new ErrorList(); + var tree = new Parser(source, errors).parseSource(); + assertFalse(errors.hasSeriousErrors()); + var defaults = descendants(tree).filter(CompositionNode.Default.class::isInstance).toList(); + assertFalse(defaults.isEmpty()); + defaults.forEach(node -> assertNull(node.getSource())); + + tree.introduceParentage(); + defaults.forEach(node -> assertSame(source, node.getSource())); + } + + private static Stream descendants(AstNode node) { + return Stream.concat(Stream.of(node), StreamSupport.stream(node.childNodes().spliterator(), false) + .flatMap(CompositionSourceTest::descendants)); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java b/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java new file mode 100644 index 00000000000..a12b7f0f219 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java @@ -0,0 +1,151 @@ +package org.xvm.compiler.ast; + +import java.util.List; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.Component; +import org.xvm.asm.ComponentResolver; +import org.xvm.asm.ConstantPool; +import org.xvm.asm.Constants.Access; +import org.xvm.asm.ErrorList; +import org.xvm.asm.FileStructure; +import org.xvm.asm.Register; + +import org.xvm.compiler.Source; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Observation is passive, resolution is resumable, and tree copies have distinct owners. */ +class DeclarationProvenanceTest { + @Test + void deferredResolutionPublishesOnlyCompletedSegmentsAndDoesNotDuplicateThemOnRetry() { + var owner = new ResolvingScope(); + var type = owner.type("Outer", "Inner"); + var resolver = type.getNameResolver(); + var errors = new ErrorList(); + assertEquals(NameResolver.Result.DEFERRED, resolver.resolve(errors)); + assertTrue(resolver.getResolvedNames().isEmpty()); + assertTrue(type.getNameBindings().stream().allMatch(binding -> binding.target() == null)); + + owner.ready = true; + // Observing bindings must not advance a resolver even when its dependency is now ready. + assertTrue(type.getNameBindings().stream().allMatch(binding -> binding.target() == null)); + assertEquals(NameResolver.Result.RESOLVED, resolver.resolve(errors)); + var resolved = resolver.getResolvedNames(); + assertEquals(List.of("Outer", "Inner"), type.getNameBindings().stream() + .map(binding -> binding.name().getValueText()).toList()); + assertEquals(resolved, type.getNameBindings().stream().map(NamedTypeExpression.NameBinding::target).toList()); + assertEquals(2, resolved.size()); + assertEquals(NameResolver.Result.RESOLVED, resolver.resolve(errors)); + assertEquals(resolved, resolver.getResolvedNames()); + assertThrows(UnsupportedOperationException.class, resolved::clear); + assertThrows(UnsupportedOperationException.class, () -> type.getNameBindings().clear()); + assertTrue(errors.getErrors().isEmpty()); + } + + @Test + void failedSuffixRetainsTheProvenPrefixAndTerminalRetriesDoNotInventAnotherBinding() { + var owner = new ResolvingScope(); + owner.ready = true; + var type = owner.type("Outer", "Inner", "Missing"); + var before = type.getNameBindings(); + var resolver = type.getNameResolver(); + var errors = new ErrorList(); + assertEquals(NameResolver.Result.ERROR, resolver.resolve(errors)); + var resolved = resolver.getResolvedNames(); + assertEquals(2, resolved.size()); + assertEquals(resolved, type.getNameBindings().subList(0, 2).stream() + .map(NamedTypeExpression.NameBinding::target).toList()); + assertNull(type.getNameBindings().getLast().target()); + assertTrue(before.stream().allMatch(binding -> binding.target() == null)); + assertEquals(1, errors.getErrors().size()); + assertEquals(NameResolver.Result.ERROR, resolver.resolve(errors)); + assertEquals(resolved, resolver.getResolvedNames()); + assertEquals(1, errors.getErrors().size()); + } + + @Test + void resumingAClonedTypeCreatesAResolverOwnedByTheClone() { + var owner = new ResolvingScope(); + var original = owner.type("Outer", "Inner"); + var resolver = original.getNameResolver(); + var errors = new ErrorList(); + assertEquals(NameResolver.Result.DEFERRED, resolver.resolve(errors)); + var copy = original.copyTree(); + var copiedResolver = copy.getNameResolver(); + assertNotSame(resolver, copiedResolver); + assertSame(copy, copiedResolver.getNode()); + assertSame(original, resolver.getNode()); + owner.ready = true; + assertEquals(NameResolver.Result.RESOLVED, copiedResolver.resolve(errors)); + assertEquals(2, copiedResolver.getResolvedNames().size()); + assertTrue(resolver.getResolvedNames().isEmpty()); + assertTrue(original.getNameBindings().stream().allMatch(binding -> binding.target() == null)); + assertEquals(NameResolver.Result.RESOLVED, resolver.resolve(errors)); + assertEquals(resolver.getResolvedNames(), copiedResolver.getResolvedNames()); + assertTrue(errors.getErrors().isEmpty()); + } + + @Test + void parameterClonesRetainKnownTargetsButRebindingDoesNotRewriteTheOriginalSlot() { + var pool = new FileStructure("Provenance").getConstantPool(); + var type = pool.getFileStructure().getModule().getIdentityConstant().getType(); + var parameter = new Parameter(null, new Token(0, 5, Id.IDENTIFIER, "value")); + assertNull(parameter.getResolvedTarget()); + var original = new Register(type, "value", 0); + parameter.setResolvedTarget(original); + for (Parameter copy : List.of(parameter.copyTree(), (Parameter) parameter.clone())) { + // Tree copies retain known bindings; this is not a fresh parse. + assertSame(original, copy.getResolvedTarget()); + var replacement = new Register(type, "value", 1); + copy.setResolvedTarget(replacement); + assertSame(replacement, copy.getResolvedTarget()); + assertSame(original, parameter.getResolvedTarget()); + } + } + + private static class ResolvingScope extends AstNode { + private final FileStructure file = new FileStructure("Provenance"); + private final Source source = new Source("Outer.Inner.Missing"); + private boolean ready; + + ResolvingScope() { + file.getModule().createClass(Access.PUBLIC, Component.Format.CLASS, "Outer", null) + .createClass(Access.PUBLIC, Component.Format.CLASS, "Inner", null); + } + + NamedTypeExpression type(String... names) { + var tokens = IntStream.range(0, names.length).mapToObj(index -> { + int start = IntStream.range(0, index).map(previous -> names[previous].length() + 1).sum(); + return new Token(start, start + names[index].length(), Id.IDENTIFIER, names[index]); + }).toList(); + return adopt(new NamedTypeExpression(null, tokens, null, null, null, tokens.getLast().getEndPosition())); + } + + @Override + public boolean isComponentNode() { return true; } + @Override + protected boolean canResolveNames() { return ready; } + @Override + public ComponentResolver getComponentResolver() { return file.getModule(); } + @Override + public ConstantPool pool() { return file.getConstantPool(); } + @Override + public Source getSource() { return source; } + @Override + public long getStartPosition() { return 0; } + @Override + public long getEndPosition() { return source.toRawString().length(); } + @Override + public String toString() { return "declaration provenance test scope"; } + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java b/javatools/src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java new file mode 100644 index 00000000000..42bf3f15df5 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java @@ -0,0 +1,42 @@ +package org.xvm.compiler.ast; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; + +/** Exercises tree-copy ownership before any incomplete-source node types are introduced. */ +class OrdinaryAstTreeCopyTest { + @Test + void typedAndLegacyCopiesKeepMetadataAndOwnTheirChildren() { + var name = new NameExpression(new Token(0, 5, Id.IDENTIFIER, "value")); + var statement = new ExpressionStatement(name); + var original = new StatementBlock(List.of(statement)); + original.introduceParentage(); + original.setStage(Stage.Registered); + + for (StatementBlock copy : List.of(original.copyTree(), (StatementBlock) original.clone())) { + assertNotSame(original, copy); + assertEquals(original.getStage(), copy.getStage()); + assertEquals(original.getStartPosition(), copy.getStartPosition()); + assertEquals(original.getEndPosition(), copy.getEndPosition()); + var copiedStatement = assertInstanceOf(ExpressionStatement.class, copy.getStatements().getFirst()); + assertNotSame(statement, copiedStatement); + assertSame(copy, copiedStatement.getParent()); + assertSame(original, statement.getParent()); + var copiedName = copiedStatement.getExpression(); + assertNotSame(name, copiedName); + assertSame(copiedStatement, copiedName.getParent()); + assertSame(statement, name.getParent()); + assertEquals(name.toString(), copiedName.toString()); + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerAtomicEmissionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerAtomicEmissionTest.kt new file mode 100644 index 00000000000..ee161208497 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerAtomicEmissionTest.kt @@ -0,0 +1,107 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Timeout +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ModuleStructure + +/** Verifies atomic results and receiver mutations by executing serialized compiler output. */ +@Timeout(30) +class CompilerAtomicEmissionTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "++counter.count", + "counter.count++", + "--counter.count", + "counter.count--", + "++count", + "count++", + "--count", + "count--", + ], + ) + fun `atomic sequential operations return the correct value and mutate their receiver`(expression: String) { + val changed = if (expression.contains("++")) 2 else 0 + val expected = if (expression.startsWith("++") || expression.startsWith("--")) changed else 1 + val property = expression.replace("++", "").replace("--", "") + val result = + compile( + "module Emission { class Counter { @Atomic Int count = 1; " + + "Int check() { Counter counter = new Counter(); Int value = $expression; " + + "assert value == $expected; assert $property == $changed; return 42; } } " + + "Int run() = new Counter().check(); }", + ) + assertThat(CompilerTestSupport.execute(result)).isEqualTo(42) + } + + @ParameterizedTest + @ValueSource(strings = ["++count", "count++", "--count", "count--"]) + fun `atomic operations can target enclosing instances`(expression: String) { + val changed = if (expression.contains("++")) 2 else 0 + val expected = if (expression.startsWith("++") || expression.startsWith("--")) changed else 1 + val result = + compile( + "module Emission { class Counter { @Atomic Int count = 1; " + + "class Worker { Int check() { Int value = $expression; assert value == $expected; " + + "assert count == $changed; return 42; } } Int check() = new Worker().check(); } " + + "Int run() = new Counter().check(); }", + ) + assertThat(CompilerTestSupport.execute(result)).isEqualTo(42) + } + + @ParameterizedTest + @ValueSource(strings = ["count += 1", "counter.count += 1", "counter.count <<= 1"]) + fun `atomic compound assignments update the intended receiver`(statement: String) { + val property = statement.substringBefore(" ") + val result = + compile( + "module Emission { class Counter { " + + "construct(T seed) { count = seed; } @Atomic T count; } " + + "class Runner { @Atomic Int count = 1; " + + "Int check() { Counter counter = new Counter(1); $statement; " + + "assert $property == 2; return 42; } } Int run() = new Runner().check(); }", + ) + assertThat(CompilerTestSupport.execute(result)).isEqualTo(42) + } + + @Test + fun `generic atomic referents produce correctly typed executable results`() { + val result = + compile( + "module Emission { class Counter { " + + "construct(T seed) { count = seed; } @Atomic T count; } " + + "Int increment(Counter counter) = ++counter.count; " + + "Int run() { Counter counter = new Counter(1); assert increment(counter) == 2; " + + "assert counter.count == 2; return 42; } }", + ) + assertThat(CompilerTestSupport.execute(result)).isEqualTo(42) + } + + @ParameterizedTest + @ValueSource(strings = ["Int run() = ++count;", "void run() { count -= 1; }"]) + fun `invalid atomic operations produce source errors before emission`(method: String) { + CompilerTestSupport.configure() + val errors = ErrorList(100) + val source = "module Emission { @Atomic String count = \"x\"; $method }" + val result = EmbeddingSupport.instance().compile(source, null, errors) + assertThat(result).isNull() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5").anyMatch { + it.startsWith("COMPILER-") + } + } + + private fun compile(source: String): ModuleStructure { + CompilerTestSupport.configure() + val errors = ErrorList(100) + val module = EmbeddingSupport.instance().compile(source, null, errors) + assertThat(errors.errors).isEmpty() + return requireNotNull(module) { "Compilation failed: ${errors.errors}" } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBindingFactsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBindingFactsTest.kt new file mode 100644 index 00000000000..d7f04486099 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBindingFactsTest.kt @@ -0,0 +1,90 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source + +/** Direct consumers of ordinary compiler facts, without an adapter or partial analysis. */ +class CompilerBindingFactsTest { + @Test + fun `selected calls retain instantiated types and written argument order`() { + val result = + compile( + "module Calls { T echo(T value, T backup) = value; " + + "void run() { String text = echo(backup = \"b\", value = \"a\"); } }", + ) + val call = result.callBindings().values.single { it.method().name == "echo" } + // The compiler signature includes the hidden type parameter; argument indexes are visible slots. + assertThat( + call + .signature() + .rawParams + .first() + .isTypeOfType, + ).isTrue() + assertThat(call.signature().rawParams.drop(1)).containsExactly(result.pool().typeString(), result.pool().typeString()) + assertThat(call.signature().rawReturns).containsExactly(result.pool().typeString()) + assertThat(call.arguments().map { it.parameterIndex() }).containsExactly(1, 0) + val node = result.callBindings().keys.single() + assertThat(result.callBindings()[node.copyTree()]).isNull() + assertThatThrownBy { (result.callBindings() as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @Test + fun `function calls have signatures without invented method identities`() { + val result = compile("module Calls { Int apply(function Int(Int) fn) = fn(42); }") + assertThat(result.callBindings()).isEmpty() + val function = result.functionBindings().values.single() + assertThat(result.pool().extractFunctionParams(function.type())).containsExactly(result.pool().typeInt64()) + assertThat(result.pool().extractFunctionReturns(function.type())).containsExactly(result.pool().typeInt64()) + } + + @Test + fun `constructors retain written argument provenance`() { + val result = compile("module Calls { const Box(Int value) {} Box make() = new Box(42); }") + val call = result.constructorBindings().values.single() + assertThat(call.method().name).isEqualTo("construct") + assertThat(call.arguments().map { it.parameterIndex() }).containsExactly(0) + } + + @Test + fun `folded property initializers retain source references`() { + val result = compile("module Calls { static Int base = 1; static Int answer = base + 1; }") + val facts = + result + .initializerBindings() + .entries + .single { it.key.nameToken.valueText == "answer" } + .value + assertThat(facts.references().map { it.name() }).contains("base") + assertThat(facts.expressions()).isNotEmpty() + } + + @Test + fun `invalid calls do not publish successful function bindings`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport.instance().compileModule( + Source("module Calls { Int apply(function Int(Int) fn) = fn(\"bad\"); }", "Calls.x"), + null, + errors, + ) + assertThat(result.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(result.functionBindings()).isEmpty() + } + + private fun compile(source: String): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(source, "Calls.x"), null, errors) + assertThat(errors.errors).isEmpty() + assertThat(result.succeeded()).isTrue() + return result + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerFunctionTypingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerFunctionTypingTest.kt new file mode 100644 index 00000000000..7d4252483b6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerFunctionTypingTest.kt @@ -0,0 +1,149 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Timeout +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.api.InterpreterConnector +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.FileStructure +import org.xvm.asm.LinkedRepository +import org.xvm.asm.ModuleStructure +import org.xvm.asm.MultiMethodStructure +import org.xvm.asm.constants.TypeConstant +import org.xvm.compiler.InstantRepository +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.Token.Id +import org.xvm.compiler.ast.Context +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.LiteralExpression +import org.xvm.compiler.ast.NameExpression +import org.xvm.util.PackedInteger +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +/** Verifies ordinary function typing without the partial-analysis or selected-call APIs. */ +@Timeout(30) +class CompilerFunctionTypingTest { + @ParameterizedTest + @ValueSource(strings = ["id", "&id", "pick", "&pick", "plain", "&plain"]) + fun `bound callable references retain their callable type and execute after serialization`(reference: String) { + val module = + compile( + "module FunctionTypes { static T id(T value) = value; T pick(T value) = value; Int plain(Int value) = value; " + + "function Int(Int) make() = $reference; Int run() = make()(42); }", + ) + val bytes = ByteArrayOutputStream().also { module.fileStructure.writeTo(it) }.toByteArray() + val restored = FileStructure(ByteArrayInputStream(bytes)).module + val method = (restored.getChild("make") as MultiMethodStructure).methods().single() + val functionType = method.identityConstant.rawReturns.single() + assertThat(method.ops).isNotEmpty() + assertThat(method.constantPool.extractFunctionParams(functionType)) + .containsExactly(method.constantPool.typeInt64()) + assertThat(method.constantPool.extractFunctionReturns(functionType)) + .containsExactly(method.constantPool.typeInt64()) + + val repository = LinkedRepository(InstantRepository(restored), EmbeddingSupport.instance().configuredRepository) + val connector = InterpreterConnector(repository) + connector.loadModule(restored.name) + connector.start(null) + ConstantPool.withPool(connector.constantPool).use { + connector.invoke0(connector.findMethods("run").single()) + } + assertThat(connector.join()).isEqualTo(42) + } + + @ParameterizedTest + @ValueSource(strings = ["1, \"x\"", "1", "True, \"x\"", "1, \"x\", 2"]) + fun `function calls accept matching arguments and reject invalid arguments`(arguments: String) { + CompilerTestSupport.configure() + val source = + "module FunctionCalls { function Int(Int, String) make(function Int(Int, String) fn) = fn; " + + "Int apply(function Int(Int, String) fn) = make(fn)($arguments); }" + val errors = ErrorList(100) + val module = EmbeddingSupport.instance().compile(source, null, errors) + if (arguments == "1, \"x\"") { + assertThat(module).isNotNull() + assertThat(errors.errors).isEmpty() + } else { + assertThat(module).isNull() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + } + + @ParameterizedTest + @ValueSource(strings = ["valid", "missing", "excess", "wrong type"]) + fun `matching return types do not erase argument fitting failures`(case: String) { + val module = compile("module FunctionFit {}") + val pool = module.constantPool + val source = Source("fn(\"x\")") + + fun stringArgument() = LiteralExpression(Token(3, 6, Id.LIT_STRING, "x")) + val arguments = + when (case) { + "missing" -> emptyList() + "excess" -> listOf(stringArgument(), stringArgument()) + "wrong type" -> listOf(LiteralExpression(Token(3, 4, Id.LIT_INT, PackedInteger.ONE))) + else -> listOf(stringArgument()) + } + val invocation = FunctionFitInvocation(pool, source, arguments) + val context = + object : Context(null, false) { + override fun pool(): ConstantPool = pool + + override fun getThisType(): TypeConstant = module.formalType + + override fun getSource(): Source = source + } + val errors = ErrorList(100) + ConstantPool.withPool(pool).use { + val function = pool.buildFunctionType(arrayOf(pool.typeString()), pool.typeInt64()) + val fitted = invocation.fit(context, function, pool.typeInt64(), errors) + if (case == "valid") { + assertThat(fitted).isEqualTo(function) + assertThat(errors.errors).isEmpty() + } else { + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(fitted).isNull() + } + } + } + + // Source diagnostics alone reject invalid programs even when the fitting routine mistakenly + // returns a usable function type. Exercise its documented success/absence contract directly. + + /** Exposes function fitting against a supplied compiler pool and source for focused inference assertions. */ + private class FunctionFitInvocation( + private val compilerPool: ConstantPool, + private val inputSource: Source, + arguments: List, + ) : InvocationExpression(NameExpression(Token(0, 2, Id.IDENTIFIER, "fn")), false, arguments, 7) { + init { + introduceParentage() + } + + override fun pool(): ConstantPool = compilerPool + + override fun getSource(): Source = inputSource + + fun fit( + context: Context, + function: TypeConstant, + requiredReturn: TypeConstant, + errors: ErrorList, + ): TypeConstant? = testFunction(context, function, 0, 0, arrayOf(requiredReturn), errors) + } + + private fun compile(source: String): ModuleStructure { + CompilerTestSupport.configure() + val errors = ErrorList(100) + val module = EmbeddingSupport.instance().compile(source, null, errors) + assertThat(errors.errors).isEmpty() + return requireNotNull(module) { "Compilation failed: ${errors.errors}" } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt index c76c4a55d57..0ac57f383b6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt @@ -1,9 +1,16 @@ package org.xvm.lsp.adapter import org.xvm.api.EmbeddingSupport +import org.xvm.api.InterpreterConnector +import org.xvm.asm.ConstantPool import org.xvm.asm.DirRepository +import org.xvm.asm.FileStructure import org.xvm.asm.LinkedRepository +import org.xvm.asm.ModuleStructure import org.xvm.compiler.BuildRepository +import org.xvm.compiler.InstantRepository +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream import java.io.File /** Uses only the compiled-module variants supplied by Gradle, including on a clean checkout. */ @@ -26,4 +33,18 @@ internal object CompilerTestSupport { fun configure() { EmbeddingSupport.instance().configure(repository, null) } + + /** Executes the serialized module's no-argument run method through the interpreter. */ + fun execute(module: ModuleStructure): Int { + val bytes = ByteArrayOutputStream().also { module.fileStructure.writeTo(it) }.toByteArray() + val restored = FileStructure(ByteArrayInputStream(bytes)).module + val repository = LinkedRepository(InstantRepository(restored), EmbeddingSupport.instance().configuredRepository) + val connector = InterpreterConnector(repository) + connector.loadModule(restored.name) + connector.start(null) + ConstantPool.withPool(connector.constantPool).use { + connector.invoke0(connector.findMethods("run").single()) + } + return connector.join() + } } From f9850720e9827114a4635e6bbe540a2cb551313e Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:25 +0100 Subject: [PATCH 007/324] Expose recovery, cursor fitting and declaration analysis to compiler hosts - Expose recovery, cursor fitting and declaration analysis to compiler hosts. - Verify transient-local compatibility through compiler consumers. - Expose declaration register identity to semantic consumers. - Remove deferred queue test from transient-local fix. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../java/org/xvm/api/EmbeddingSupport.java | 328 +++- .../main/java/org/xvm/asm/ConstantPool.java | 5 + .../main/java/org/xvm/compiler/Compiler.java | 15 +- .../java/org/xvm/compiler/CursorBinding.java | 326 ++++ .../main/java/org/xvm/compiler/Parser.java | 1316 ++++++++++++++--- .../java/org/xvm/compiler/ast/AstNode.java | 8 +- .../java/org/xvm/compiler/ast/Context.java | 7 + .../org/xvm/compiler/ast/CursorScope.java | 466 ++++++ .../xvm/compiler/ast/LambdaExpression.java | 4 +- .../ast/MethodDeclarationStatement.java | 6 +- .../org/xvm/compiler/ast/NewExpression.java | 57 +- .../org/xvm/compiler/ast/PartialArgument.java | 49 + .../xvm/compiler/ast/PartialCallResolver.java | 452 ++++++ .../ast/PartialConstructionResolver.java | 139 ++ .../org/xvm/compiler/ast/PartialQueries.java | 77 + .../ast/PropertyDeclarationStatement.java | 12 +- .../java/org/xvm/compiler/ast/StageMgr.java | 19 + .../java/org/xvm/compiler/ast/Statement.java | 8 +- .../org/xvm/compiler/ast/StatementBlock.java | 20 +- .../ast/TypeCompositionStatement.java | 7 +- .../ast/VariableDeclarationStatement.java | 5 +- .../IncompleteDeclarationStatement.java | 136 ++ .../ast/partial/IncompleteExpression.java | 93 ++ .../partial/IncompleteLocalDeclaration.java | 87 ++ .../ast/partial/IncompleteStatement.java | 271 ++++ .../IncompleteTypeCompositionStatement.java | 143 ++ .../compiler/ast/partial/PartialSyntax.java | 94 ++ .../ast/partial/ProposedLiteralToken.java | 23 + .../src/main/java/org/xvm/tool/Compiler.java | 28 +- .../main/java/org/xvm/tool/ModuleInfo.java | 47 +- .../src/main/resources/errors.properties | 1 + .../xvm/api/BindingIdentitySnapshotTest.java | 23 +- .../org/xvm/compiler/CursorBindingTest.java | 133 ++ .../compiler/ParserDeclarationNameTest.java | 172 +++ .../org/xvm/compiler/ParserRecoveryTest.java | 672 +++++++++ .../org/xvm/compiler/ast/AstTreeCopyTest.java | 134 ++ .../xvm/compiler/ast/PartialSyntaxTest.java | 233 +++ .../partial/RegisteredChildFieldsTest.java | 143 ++ .../java/org/xvm/tool/ModuleInfoTest.java | 63 +- .../CompilerDeclarationAnalysisTest.kt | 80 + .../lsp/adapter/CompilerPartialFactsTest.kt | 69 + 41 files changed, 5654 insertions(+), 317 deletions(-) create mode 100644 javatools/src/main/java/org/xvm/compiler/CursorBinding.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java create mode 100644 javatools/src/test/java/org/xvm/compiler/CursorBindingTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ParserRecoveryTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java create mode 100644 javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPartialFactsTest.kt diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index 3a3dac3e6db..db90fe60b8d 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -1,6 +1,7 @@ package org.xvm.api; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import static java.util.Objects.requireNonNull; @@ -13,16 +14,21 @@ import java.util.Arrays; import java.util.Collections; +import java.util.HashSet; import java.util.IdentityHashMap; import java.util.List; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.function.Function; +import java.util.stream.Stream; + import org.xvm.asm.ConstantPool; import org.xvm.asm.DirRepository; +import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; import org.xvm.asm.FileStructure; import org.xvm.asm.LinkedRepository; @@ -36,6 +42,7 @@ import org.xvm.compiler.BuildRepository; import org.xvm.compiler.Compiler; import org.xvm.compiler.CompilerException; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InstantRepository; import org.xvm.compiler.InitializerBinding; import org.xvm.compiler.InvocationBinding; @@ -50,6 +57,8 @@ import org.xvm.compiler.ast.Statement; import org.xvm.compiler.ast.StatementBlock; import org.xvm.compiler.ast.TypeCompositionStatement; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; import org.xvm.tool.Console; import org.xvm.tool.Launcher.LauncherException; @@ -61,6 +70,7 @@ import static org.xvm.asm.ErrorListener.NOWHERE; import static org.xvm.asm.ErrorListener.at; +import static org.xvm.util.Handy.resolveFile; import static org.xvm.util.Severity.ERROR; import static org.xvm.util.Handy.quoted; @@ -535,6 +545,286 @@ public Compilation compileModule(ModuleInfo sources, ModuleRepository input, @No }, input, errs); } + /** + * A successfully resolved declaration graph, before expression validation and code generation. + * It contains no executable artifact. The calling worker exclusively owns these structures and + * may inspect TypeInfo with its listener; ordinary compilation still validates every body. + */ + public record DeclarationAnalysis(FileStructure file, StatementBlock ast, + List sourceTrees) { + public DeclarationAnalysis { + requireNonNull(file, "file"); + requireNonNull(ast, "ast"); + sourceTrees = List.copyOf(sourceTrees); + } + + public ConstantPool pool() { + return file.getConstantPool(); + } + } + + /** + * Resolve one source's declarations in a fresh attempt. Body errors are outside this operation's + * scope; syntax, linkage, declaration errors and cancellation prevent a result. + * + * @param source the source to parse + * @param input optional compiled dependencies + * @param errs the host listener + * @return resolved declarations, never a compiled module + */ + public Optional analyzeDeclarations(Source source, ModuleRepository input, + @NotNull ErrorListener errs) { + requireNonNull(source, "source"); + return analyzeDeclarations(listener -> { + StatementBlock tree = new Parser(source, listener).parseSource(); + return new ParsedSources(listener.hasSeriousErrors() ? null : tree, List.of(tree)); + }, input, errs); + } + + /** + * Resolve a module tree's declarations using the same source/resource overlay contract as + * {@link #compileModule(ModuleInfo, ModuleRepository, ErrorListener)}. Use a fresh ModuleInfo. + * + * @param sources the module's source tree + * @param input optional compiled dependencies + * @param errs the host listener + * @return resolved declarations, empty after any declaration failure or cancellation + */ + public Optional analyzeDeclarations(ModuleInfo sources, ModuleRepository input, + @NotNull ErrorListener errs) { + requireNonNull(sources, "sources"); + return analyzeDeclarations(listener -> { + Node root = sources.getSourceTree(listener); + return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), + sources.getParsedSources()); + }, input, errs); + } + + private Optional analyzeDeclarations(Function parse, + ModuleRepository input, ErrorListener errs) { + CompilerAttempt attempt = runCompiler(parse, input, errs, CursorBinding.Collector.NONE, + CompilationGoal.DECLARATIONS); + EmbeddingCompiler compiler = attempt.compiler(); + return attempt.succeeded() + ? Optional.of(new DeclarationAnalysis(compiler.file, compiler.ast, compiler.sourceTrees)) + : Optional.empty(); + } + + /** + * Facts retained by an explicit incomplete-source analysis, never a compiled module. + * + *

The available source syntax and sites may be unvalidated. A child expression supplies a + * semantic fact only if validation succeeded (isValidated and a fitting TypeFit) and its + * resolved target/type is available. Failed validation can leave placeholder types. An absent + * pool means semantic analysis did not start. Cursor bindings copy visible scope and candidate + * signatures/mappings fitted to written arguments; they never select the incomplete operation + * or invent a missing argument/result. Consumers must copy facts while exclusively + * owning the attempt, as with Compilation; ASTs and pools are not concurrent query objects. + * Binding maps preserve reference identity for both keys and values and reject null entries. + * + * @param sourceTrees available source syntax, including unvalidated recovered trees + * @param sites incomplete source sites retained by this attempt + * @param pool the attempt's constant pool, or null if semantic analysis did not start + * @param callBindings validated method calls keyed by source invocation identity + * @param cursorBindings visible scope and candidate facts keyed by incomplete site identity + * @param functionBindings validated function signatures keyed by source invocation identity + */ + public record PartialAnalysis(List sourceTrees, List sites, + @Nullable ConstantPool pool, + Map callBindings, + Map cursorBindings, + Map functionBindings) { + public PartialAnalysis { + sourceTrees = List.copyOf(sourceTrees); + sites = List.copyOf(sites); + cursorBindings = identitySnapshot(cursorBindings); + callBindings = identitySnapshot(callBindings); + functionBindings = identitySnapshot(functionBindings); + } + + /** Create partial facts with cursor and selected-method bindings. */ + public PartialAnalysis(List sourceTrees, List sites, + @Nullable ConstantPool pool, + Map callBindings, + Map cursorBindings) { + this(sourceTrees, sites, pool, callBindings, cursorBindings, Map.of()); + } + + /** Create partial facts with call and receiver information only. */ + public PartialAnalysis(List sourceTrees, List sites, + @Nullable ConstantPool pool, Map callBindings) { + this(sourceTrees, sites, pool, callBindings, Map.of()); + } + + /** Create partial facts with syntax and receiver information only. */ + public PartialAnalysis(List sourceTrees, List sites, + @Nullable ConstantPool pool) { + this(sourceTrees, sites, pool, Map.of()); + } + } + + /** + * Analyze the intact prefix of one trailing incomplete expression statement in a single + * source module. Supports a member-access dot or unfinished call at EOF, with complete + * preceding declarations/statements and arguments. Other syntax errors prevent semantic + * analysis; returns, assignments, incomplete nested arguments and module member files are + * outside this bounded contract. Complete input has no incomplete site and is not compiled. + * + *

Parsing reports immediately to the host and respects its budget/cancellation. Only the + * recognized EOF boundary may enter compiler passes. The incomplete statement validates its + * intact children in the real method context, then fails validation before method emission. + * Its repeated EOF diagnostic stops the compiler internally without being delivered twice + * to the host. Other diagnostics and unexpected failures remain visible. + * + * @param source a fresh named source for this attempt + * @param input the optional repository of compiled dependencies + * @param errs the host's diagnostic listener + * + * @return partial facts with no success/module artifact contract + */ + public PartialAnalysis analyzeIncomplete(Source source, ModuleRepository input, + @NotNull ErrorListener errs) { + requireNonNull(source, "source"); + return analyzeIncomplete(listener -> { + StatementBlock tree = Parser.forPartialAnalysis(source, listener).parseSource(); + return new ParsedSources(tree, List.of(tree)); + }, Parser.UNEXPECTED_EOF, input, errs); + } + + /** + * Analyze an incomplete member access/call at an explicit source position, including before + * existing closing braces or a semicolon. Source text is unchanged and later declarations + * retain their positions. The position must come from {@link Source#getPosition()} for this text. + * Supports standalone statements, simple assignment/initializer values, single return values, + * and final nested call arguments, including named arguments and existing closing parentheses. + * A cursor at the end of a written member token retains that token as a completion prefix. + * A cursor before a call's closing parenthesis inspects its receiver and written arguments, + * even when the selected syntax is complete. The selected member/call itself is not validated. + * Bare-name prefixes and empty statement boundaries expose visible scope; a final named argument + * awaiting its value retains its label. Candidate signatures can infer types from written arguments. + * Compound/conditional values and arguments following the cursor retain their real contexts. + * Constructors reuse normal receiver/type preparation, including virtual/inner/annotated types, + * required-type and argument-derived class inference, and parenthesized array initializers. + * Missing call/group/tuple parentheses and index/list/map brackets are retained at statement or + * outer-delimiter boundaries. Declaration values retain missing terminators; parameter defaults + * may retain a missing closing parenthesis before a body. An explicit cursor at EOF can also + * retain missing block braces. This does not invent operands, declaration names/types or literal + * contents, or repair unrelated errors. Anonymous construction prepares the retained declaration + * and fits its own or superclass constructors; it does not emit forwarding constructors or run + * capture analysis. Single-dimensional array brackets fit the size parameter of the fixed-size + * constructor, including an empty/final-name slot and a missing closing bracket. A following + * supplier is parsed for recovery but is outside this prefix proof. Multidimensional + * construction remains unsupported. Member/return and method-parameter type prefixes query + * types through the enclosing compiler scope. Flat qualified names resolve their qualifier + * and visible nested types, replacing only the final written identifier. Unqualified empty + * parameter slots are also supported. An unfinished header retains its written declaration + * name and range but registers no method/property component or parameters. Written leaf + * names inside parameterized and compound types also participate, including bounded missing + * angle/group closers. These are visible-type suggestions, not proof of generic constraints. + * Empty type/qualified slots, selected qualifier tokens, parameterized qualifiers, generic + * base names and function/sequence types are supported. Generic-method, multiple-return and + * type-composition headers query their enclosing scope; unregistered formals hide outer names + * without acquiring invented identities. A root module header registers only its written + * namespace/core import; its incomplete body and compositions cannot emit or contribute facts. + * Other syntax errors prevent semantic analysis; cursors outside supported boundaries yield no site. + */ + public PartialAnalysis analyzeIncomplete(Source source, long cursor, ModuleRepository input, + @NotNull ErrorListener errs) { + requireNonNull(source, "source"); + return analyzeIncomplete(listener -> { + StatementBlock tree = Parser.forPartialAnalysis(source, cursor, listener).parseSource(); + return new ParsedSources(tree, List.of(tree)); + }, Parser.INCOMPLETE_EXPRESSION, input, errs); + } + + /** + * Analyze an explicit cursor in one file of a module snapshot. Uses the same source loading, + * overlay and resource contract as {@link #compileModule(ModuleInfo, ModuleRepository, ErrorListener)}. + * The cursor refers to the selected file's snapshot, which may contain unsaved text. Neither + * this ModuleInfo nor its parsed trees may be reused for another attempt or normal compilation. + * Only the recognized cursor diagnostic is deferred during assembly; other source errors abort it. + */ + public PartialAnalysis analyzeIncomplete(ModuleInfo sources, File sourceFile, long cursor, + ModuleRepository input, @NotNull ErrorListener errs) { + requireNonNull(sources, "sources"); + String name = resolveFile(requireNonNull(sourceFile, "sourceFile")).getPath(); + return analyzeIncomplete(listener -> { + ErrorListener assembly = ErrorListener.cancellable( + ErrorListener.collecting(listener::log), listener::isAbortDesired); + Node root = sources.getSourceTree(assembly, (source, nodeErrors) -> { + if (!name.equals(source.getFileName())) { + return new Parser(source, nodeErrors).parseSource(); + } + ErrorListener partial = ErrorListener.cancellable(ErrorListener.collecting(error -> { + if (error.getCode().equals(Parser.INCOMPLETE_EXPRESSION)) { + listener.log(error); + } else { + nodeErrors.log(error); + } + }), listener::isAbortDesired); + return Parser.forPartialAnalysis(source, cursor, partial).parseSource(); + }); + return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), + sources.getParsedSources()); + }, Parser.INCOMPLETE_EXPRESSION, input, errs); + } + + private PartialAnalysis analyzeIncomplete(Function parse, + String boundaryCode, ModuleRepository input, + ErrorListener errs) { + verifyConfigured(); + requireNonNull(errs, "errs"); + Set delivered = new HashSet<>(); + ErrorListener host = ErrorListener.cancellable(ErrorListener.collecting(error -> { + if (delivered.add(error.genUID())) { + errs.log(error); + } + }), errs::isAbortDesired); + ErrorList syntaxErrors = new ErrorList(ErrorList.UNLIMITED); + ParsedSources parsed; + try { + if (host.isAbortDesired()) { + return new PartialAnalysis(List.of(), List.of(), null); + } + parsed = parse.apply(ErrorListener.tee(syntaxErrors, host)); + } catch (CompilerException e) { + return new PartialAnalysis(List.of(), List.of(), null); + } catch (RuntimeException | AssertionError e) { + host.error(ERR_INTERNAL, NOWHERE, e, "Incomplete-source parsing failed"); + return new PartialAnalysis(List.of(), List.of(), null); + } + + if (parsed.root() == null) { + return new PartialAnalysis(parsed.sources(), List.of(), null); + } + var sites = incompleteSites(parsed.root()).distinct().toList(); + if (sites.size() != 1 || syntaxErrors.getErrors().stream().anyMatch(error -> + error.getSeverity().isAtLeast(ERROR) && !error.getCode().equals(boundaryCode))) { + return new PartialAnalysis(parsed.sources(), List.of(), null); + } + + var cursors = new CursorBinding.Collector(); + Compilation attempt = compileModule(listener -> parsed, input, host, cursors); + // Anonymous construction can replace its deferred body with an owned class/validation + // clone. Publish only surviving syntax, never the pre-validation cursor identity. + // Primary-constructor defaults can expose the same surviving node through both the + // written parameter and generated property getter. Publish each identity only once. + var surviving = incompleteSites(parsed.root()).distinct().toList(); + return new PartialAnalysis(parsed.sources(), surviving, attempt.pool(), + attempt.callBindings(), cursors.finish(parsed.sources()), attempt.functionBindings()); + } + + /** Parsed children exist before parent links; expose the innermost unfinished operations. */ + private static Stream incompleteSites(AstNode node) { + var nested = PartialSyntax.children(node) + .flatMap(EmbeddingSupport::incompleteSites); + if (node instanceof IncompleteStatement site) { + var descendants = nested.toList(); + return descendants.isEmpty() ? Stream.of(site) : descendants.stream(); + } + return nested; + } + /** An assembled tree is available only when parsing/loading succeeded. */ /** Preserve node identity, reject null entries and detach from the collector. */ private static Map identitySnapshot(Map source) { @@ -547,13 +837,27 @@ private record ParsedSources(StatementBlock root, List sources) private Compilation compileModule(Function parse, ModuleRepository input, ErrorListener errs) { + return compileModule(parse, input, errs, CursorBinding.Collector.NONE); + } + + private Compilation compileModule(Function parse, + ModuleRepository input, ErrorListener errs, CursorBinding.Collector cursors) { + return runCompiler(parse, input, errs, cursors, CompilationGoal.MODULE).compiler().result(); + } + + private enum CompilationGoal { DECLARATIONS, MODULE } + + private record CompilerAttempt(EmbeddingCompiler compiler, boolean succeeded) {} + + private CompilerAttempt runCompiler(Function parse, + ModuleRepository input, ErrorListener errs, + CursorBinding.Collector cursors, CompilationGoal goal) { verifyConfigured(); requireNonNull(errs, "errs"); - EmbeddingCompiler compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs); + EmbeddingCompiler compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs, cursors, goal); try { - if (!errs.isAbortDesired()) { - compiler.process(); - } + return new CompilerAttempt(compiler, !errs.isAbortDesired() && compiler.process() == 0 + && !errs.hasSeriousErrors() && !errs.isAbortDesired()); } catch (LauncherException e) { // Expected aborts already have diagnostics or a cancellation request. if (!errs.hasSeriousErrors() && !errs.isAbortDesired()) { @@ -563,7 +867,7 @@ private Compilation compileModule(Function parse, // An earlier source error must not hide an unexpected compiler failure. errs.error(ERR_INTERNAL, NOWHERE, e, "Compilation failed"); } - return compiler.result(); + return new CompilerAttempt(compiler, false); } /** @@ -614,6 +918,8 @@ private static class EmbeddingCompiler extends org.xvm.tool.Compiler { private final Function parse; private final InvocationBinding.Collector bindings = new InvocationBinding.Collector(); + private final CursorBinding.Collector cursors; + private final CompilationGoal goal; private final ModuleRepository inRepo; private final ModuleRepository coreRepo; @@ -654,10 +960,13 @@ public boolean isAbortDesired() { } protected EmbeddingCompiler(Function parse, - ModuleRepository input, ModuleRepository core, ErrorListener errs) { + ModuleRepository input, ModuleRepository core, ErrorListener errs, + CursorBinding.Collector cursors, CompilationGoal goal) { super(CompilerOptions.builder().build(), SILENT_CONSOLE, errs); this.parse = parse; + this.cursors = cursors; + this.goal = goal; this.inRepo = input; this.coreRepo = core; } @@ -702,7 +1011,7 @@ protected int process() { return checkErrors("source parsing"); } - Compiler compiler = new Compiler(stmtModule, this, bindings); + Compiler compiler = new Compiler(stmtModule, this, bindings, cursors); FileStructure struct = compiler.generateInitialFileStructure(); this.file = struct; if (struct == null || checkErrors("module creation") != 0) { @@ -716,6 +1025,11 @@ protected int process() { return 1; } + if (goal == CompilationGoal.DECLARATIONS) { + resolveDeclarations(List.of(compiler), repoLib); + return hasSeriousErrors() || isAbortDesired() ? 1 : 0; + } + int result = super.compile(List.of(compiler), repoLib); if (result == 0 && !isAbortDesired()) { this.module = struct.getModule(); diff --git a/javatools/src/main/java/org/xvm/asm/ConstantPool.java b/javatools/src/main/java/org/xvm/asm/ConstantPool.java index bc125aba077..69d3ad8fdc4 100755 --- a/javatools/src/main/java/org/xvm/asm/ConstantPool.java +++ b/javatools/src/main/java/org/xvm/asm/ConstantPool.java @@ -1277,6 +1277,11 @@ public static String getImplicitImportName(String sPath) { return IMPLICITS.byPath().get(sPath); } + /** The language's implicit import names, without resolving or loading their components. */ + public static Set getImplicitImportNames() { + return IMPLICITS.byName().keySet(); + } + /** * Given the specified typedef name and the context (module, package, class, method) within * which it exists, obtain a TypedefConstant that represents it. diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index 95836b5d97f..2daf2df16ee 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -35,6 +35,11 @@ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs) { /** Construct a compiler that records call facts in the supplied attempt-owned collector. */ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, InvocationBinding.Collector bindings) { + this(stmtModule, errs, bindings, CursorBinding.Collector.NONE); + } + + public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, + InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { if (stmtModule == null) { throw new IllegalArgumentException("AST node for module required"); } @@ -44,6 +49,7 @@ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, m_stmtModule = stmtModule; f_errs = requireNonNull(errs, "errs"); f_bindings = requireNonNull(bindings, "bindings"); + f_cursors = requireNonNull(cursors, "cursors"); } // ----- accessors ----------------------------------------------------------------------------- @@ -119,7 +125,7 @@ public FileStructure generateInitialFileStructure() { if (getStage() == Stage.Initial) { setStage(Stage.Registering); - StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, f_errs, f_bindings); + StageMgr mgr = new StageMgr(m_stmtModule, Stage.Registered, f_errs, f_bindings, f_cursors); if (!mgr.processComplete()) { if (f_errs.hasSeriousErrors() || f_errs.isAbortDesired()) { return null; @@ -192,7 +198,7 @@ public boolean resolveNames(boolean fLastAttempt) { if (!alreadyReached(Stage.Resolving)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Resolving); - m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, f_errs, f_bindings); + m_mgr = new StageMgr(m_stmtModule, Stage.Resolved, f_errs, f_bindings, f_cursors); } if (fLastAttempt) { @@ -236,7 +242,7 @@ public boolean validateExpressions(boolean fLastAttempt) { if (!alreadyReached(Stage.Validating)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Validating); - m_mgr = new StageMgr(m_stmtModule, Stage.Validated, f_errs, f_bindings); + m_mgr = new StageMgr(m_stmtModule, Stage.Validated, f_errs, f_bindings, f_cursors); } if (fLastAttempt) { @@ -280,7 +286,7 @@ public boolean generateCode(boolean fLastAttempt) { if (!alreadyReached(Stage.Emitting)) { // first time through: resolve starting from the module, and recurse down setStage(Stage.Emitting); - m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, f_errs, f_bindings); + m_mgr = new StageMgr(m_stmtModule, Stage.Emitted, f_errs, f_bindings, f_cursors); } if (fLastAttempt) { @@ -386,6 +392,7 @@ private void setStage(Stage stage) { /** Optional tooling output, confined to this compiler attempt. */ private final InvocationBinding.Collector f_bindings; + private final CursorBinding.Collector f_cursors; /** * The FileStructure that this compiler is putting together in a series of passes. diff --git a/javatools/src/main/java/org/xvm/compiler/CursorBinding.java b/javatools/src/main/java/org/xvm/compiler/CursorBinding.java new file mode 100644 index 00000000000..611f6ec89a0 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/CursorBinding.java @@ -0,0 +1,326 @@ +package org.xvm.compiler; + +import java.util.Collections; +import java.util.IdentityHashMap; +import java.util.List; +import java.util.Map; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.xvm.asm.Register; +import org.xvm.asm.constants.IdentityConstant; +import org.xvm.asm.constants.MethodConstant; +import org.xvm.asm.constants.PropertyConstant; +import org.xvm.asm.constants.SignatureConstant; +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.partial.IncompleteStatement; +/** Facts captured at an explicit cursor while its real validation context is alive. */ +public record CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties, List formals, + List argumentLiterals, List argumentExpressions, List enclosingExpressions, + List argumentTemplates) { + public CursorBinding { + variables = List.copyOf(variables); + types = List.copyOf(types); + candidates = List.copyOf(candidates); + functions = List.copyOf(functions); + argumentValues = List.copyOf(argumentValues); + argumentProperties = List.copyOf(argumentProperties); + formals = List.copyOf(formals); + argumentLiterals = List.copyOf(argumentLiterals); + argumentExpressions = List.copyOf(argumentExpressions); + enclosingExpressions = List.copyOf(enclosingExpressions); + argumentTemplates = List.copyOf(argumentTemplates); + } + + /** Retain ordinary-expression consumers predating compiler-fitted value templates. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties, List formals, + List argumentLiterals, List argumentExpressions, List enclosingExpressions) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, + argumentProperties, formals, argumentLiterals, argumentExpressions, enclosingExpressions, List.of()); + } + + public CursorBinding withArgumentTemplates(List templates) { + return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, + functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, + enclosingExpressions, templates); + } + + /** Preserve the previous constructor while adding ordinary-expression proposals. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties, List formals, + List argumentLiterals, List argumentExpressions) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, + argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, List.of()); + } + + public CursorBinding withEnclosingExpressions(List expressions) { + return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, + functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, expressions, argumentTemplates); + } + + /** Retain callers predating enclosing-instance insertion proposals. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties, List formals, List argumentLiterals) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, + argumentValues, argumentProperties, formals, argumentLiterals, List.of()); + } + + /** Retain callers predating literal insertion proposals. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties, List formals) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, + argumentValues, argumentProperties, formals, List.of()); + } + + /** Retain the original complete constructor for existing embedding consumers. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues, + List argumentProperties) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, + argumentValues, argumentProperties, List.of()); + } + + /** + * A written formal, not an invented declaration identity. A recursive written constraint has + * no resolved type until its declaration exists; only that case supplies text instead. + */ + public record Formal(Token name, TypeConstant constraint, String writtenConstraint) { + public Formal(Token name, TypeConstant constraint) { + this(name, constraint, null); + } + + public Formal { + if ((constraint == null) == (writtenConstraint == null)) { + throw new IllegalArgumentException("A formal needs either a resolved or a written constraint"); + } + } + } + + public CursorBinding withFormals(List formals) { + return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, + functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, enclosingExpressions, argumentTemplates); + } + + /** Retain variable-completion callers. Record patterns must also include argument properties. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions, List argumentValues) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, List.of()); + } + + /** Retain signature-only callers. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected, + List functions) { + this(variables, thisType, instance, types, candidates, callsInspected, functions, List.of()); + } + + /** Retain callers that only consume method candidates. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, List candidates, boolean callsInspected) { + this(variables, thisType, instance, types, candidates, callsInspected, List.of()); + } + + public CursorBinding(List variables, TypeConstant thisType, boolean instance) { + this(variables, thisType, instance, List.of(), List.of(), false); + } + + /** Group call-specific facts while retaining the existing constructors and record components. */ + public CursorBinding(List variables, TypeConstant thisType, boolean instance, + List types, CallFacts calls) { + this(variables, thisType, instance, types, calls.candidates(), calls.inspected(), + calls.functions(), calls.argumentValues(), calls.argumentProperties(), List.of(), calls.argumentLiterals(), calls.argumentExpressions()); + } + + /** A grouped immutable view; no context, mutable operation or AST child is introduced. */ + public CallFacts callFacts() { + return new CallFacts(candidates, callsInspected, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); + } + + private CursorBinding withCallFacts(CallFacts calls) { + return new CursorBinding(variables, thisType, instance, types, calls).withFormals(formals).withEnclosingExpressions(enclosingExpressions).withArgumentTemplates(argumentTemplates); + } + + public CursorBinding withCandidates(List candidates) { + return withCallFacts(callFacts().withCandidates(candidates)); + } + + public CursorBinding withFunctions(List functions) { + return withCallFacts(callFacts().withFunctions(functions)); + } + + public CursorBinding withTypes(List types) { + return new CursorBinding(variables, thisType, instance, types, callFacts()).withFormals(formals).withEnclosingExpressions(enclosingExpressions).withArgumentTemplates(argumentTemplates); + } + + /** Readable source variables whose proposed insertion fits at least one incomplete-call candidate. */ + public CursorBinding withArgumentValues(List values) { + return withCallFacts(callFacts().withArgumentValues(values)); + } + + /** Implicit property/constant reads whose insertion fits at least one incomplete-call candidate. */ + public CursorBinding withArgumentProperties(List properties) { + return withCallFacts(callFacts().withArgumentProperties(properties)); + } + + /** Literal source spellings whose insertion fits and validates with the other written arguments. */ + public CursorBinding withArgumentLiterals(List literals) { + return withCallFacts(new CallFacts(candidates, callsInspected, functions, argumentValues, argumentProperties, literals, argumentExpressions)); + } + + /** Compiler-validated enclosing-instance source expressions, distinct from literals. */ + public CursorBinding withArgumentExpressions(List expressions) { + return withCallFacts(new CallFacts(candidates, callsInspected, functions, argumentValues, + argumentProperties, argumentLiterals, expressions)); + } + + /** + * Call inspection and insertion facts, separate from visible scope and syntax selection. + * An inspected empty candidate list means rejection, not absence of inspection. Updating + * argument values, properties or literals preserves that distinction. Previous constructors + * remain available; record patterns must include the literal-proposal component. + */ + public record CallFacts(List candidates, boolean inspected, + List functions, List argumentValues, + List argumentProperties, List argumentLiterals, List argumentExpressions) { + public CallFacts { + candidates = List.copyOf(candidates); + functions = List.copyOf(functions); + argumentValues = List.copyOf(argumentValues); + argumentProperties = List.copyOf(argumentProperties); + argumentLiterals = List.copyOf(argumentLiterals); + argumentExpressions = List.copyOf(argumentExpressions); + } + + public CallFacts(List candidates, boolean inspected, + List functions, List argumentValues, + List argumentProperties, List argumentLiterals) { + this(candidates, inspected, functions, argumentValues, argumentProperties, argumentLiterals, List.of()); + } + + public CallFacts(List candidates, boolean inspected, + List functions, List argumentValues, + List argumentProperties) { + this(candidates, inspected, functions, argumentValues, argumentProperties, List.of()); + } + + public CallFacts withCandidates(List candidates) { + return new CallFacts(candidates, true, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); + } + + public CallFacts withFunctions(List functions) { + return new CallFacts(candidates, true, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); + } + + public CallFacts withArgumentValues(List values) { + return new CallFacts(candidates, inspected, functions, values, argumentProperties, argumentLiterals, argumentExpressions); + } + + public CallFacts withArgumentProperties(List properties) { + return new CallFacts(candidates, inspected, functions, argumentValues, properties, argumentLiterals, argumentExpressions); + } + } + + /** A compiler-resolved type name; type preserves parameterized qualifier substitution. */ + public record NamedType(String name, IdentityConstant identity, TypeConstant type) { + public NamedType(String name, IdentityConstant identity) { + this(name, identity, identity.getType()); + } + } + + /** Fits the written arguments; missing arguments cannot establish a selected overload. */ + public record Candidate(MethodConstant method, SignatureConstant signature, + List arguments, boolean converting, boolean receiverArgument) { + public Candidate { + arguments = List.copyOf(arguments); + } + + public Candidate(MethodConstant method, SignatureConstant signature, + List arguments, boolean converting) { + this(method, signature, arguments, converting, false); + } + + public Candidate withReceiverArgument() { + return new Candidate(method, signature, arguments, converting, true); + } + } + + /** A function type whose written arguments fit; there is no selected method or parameter names. */ + public record FunctionCandidate(TypeConstant type, List arguments) { + public FunctionCandidate { + arguments = List.copyOf(arguments); + } + } + + /** A visible variable and its narrowed type; unreadable variables still shadow outer names. */ + public record Variable(String name, Register register, TypeConstant type, boolean readable) {} + + /** A property read, resolved in the cursor's context, and its validated value type. */ + public record Property(String name, PropertyConstant identity, TypeConstant type) {} + + /** Attempt-owned scratch space; no context or collector is stored on a syntax node. */ + public static final class Collector { + public Collector() { + this(true); + } + + private Collector(boolean enabled) { + f_enabled = enabled; + } + + public boolean isEnabled() { + return f_enabled; + } + + /** A validation retry must not retain facts from its earlier attempt. */ + public void begin(IncompleteStatement site) { + if (f_enabled) { + f_bindings.remove(site); + } + } + + public void record(IncompleteStatement site, CursorBinding binding) { + if (f_enabled) { + f_bindings.put(site, binding); + } + } + + /** Discard trial clones and release scratch entries when publishing surviving syntax. */ + public Map finish(List roots) { + Map result = new IdentityHashMap<>(); + roots.stream().flatMap(Collector::nodes) + .filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast) + .filter(f_bindings::containsKey) + .forEach(site -> result.put(site, f_bindings.get(site))); + f_bindings.clear(); + return Collections.unmodifiableMap(result); + } + + private static Stream nodes(AstNode node) { + return Stream.concat(Stream.of(node), StreamSupport.stream(node.children().spliterator(), false) + .flatMap(Collector::nodes)); + } + + public static final Collector NONE = new Collector(false); + + private final boolean f_enabled; + + private final Map f_bindings = new IdentityHashMap<>(); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index 661aa983044..c447a6a70ed 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -1,5 +1,7 @@ package org.xvm.compiler; +import static java.util.Objects.requireNonNull; + import java.io.File; import java.io.IOException; @@ -9,9 +11,12 @@ import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.xvm.asm.ErrorList; import org.xvm.asm.ErrorListener; @@ -21,6 +26,12 @@ import org.xvm.compiler.Token.Id; import org.xvm.compiler.ast.*; +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement; +import org.xvm.compiler.ast.partial.IncompleteExpression; +import org.xvm.compiler.ast.partial.IncompleteLocalDeclaration; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; import org.xvm.tool.ResourceDir; @@ -28,10 +39,9 @@ import org.xvm.util.ListMap; import org.xvm.util.Severity; -import static java.util.Objects.requireNonNull; +import static org.xvm.asm.ErrorListener.in; import static org.xvm.asm.ErrorListener.Silence.DISCARD; -import static org.xvm.asm.ErrorListener.in; import static org.xvm.asm.ErrorListener.silent; /** @@ -47,7 +57,31 @@ public class Parser { * @param listener the error listener */ public Parser(Source source, ErrorListener listener) { - this(source, listener, null); + this(source, listener, null, false, NO_CURSOR); + } + + /** + * Create a parser that can retain a trailing incomplete expression statement for explicit + * partial analysis. Errors are still reported; this does not make the source compilable. + */ + public static Parser forPartialAnalysis(Source source, ErrorListener listener) { + return new Parser(source, listener, null, true, NO_CURSOR); + } + + /** + * Retain a supported incomplete statement at a cursor, without truncating the source. + * The cursor uses a position token obtained from {@link Source#getPosition()} for this text. + * Supports missing member/call suffixes, typed member prefixes and calls before an existing + * closing parenthesis at statement and final call-argument boundaries. Cursor-selected syntax + * need not be malformed; it is inspected as a prefix, without validating the selected operation. + * Missing call/group/index closers around that operation retain their enclosing syntax at a + * statement or outer-delimiter boundary; a cursor at EOF also retains unfinished blocks. + */ + public static Parser forPartialAnalysis(Source source, long cursor, ErrorListener listener) { + if (cursor == NO_CURSOR) { + throw new IllegalArgumentException("A source cursor position is required"); + } + return new Parser(source, listener, null, true, cursor); } /** @@ -57,16 +91,18 @@ public Parser(Source source, ErrorListener listener) { * @param atoken the tokens to parse */ protected Parser(Parser parent, Token[] atoken) { - this(parent.m_source, parent.f_errs.get(), parent.m_lexer.createLexer(atoken)); + this(parent.m_source, parent.f_errs.get(), parent.m_lexer.createLexer(atoken), false, + NO_CURSOR); } - private Parser(Source source, ErrorListener errs, Lexer lexer) { + private Parser(Source source, ErrorListener errs, Lexer lexer, boolean fPartialAnalysis, + long cursor) { if (source == null) { throw new IllegalArgumentException("Source required"); } - m_source = source; - f_errs = new Reporting(requireNonNull(errs, "errs")); + m_source = source; + f_errs = new Reporting(requireNonNull(errs, "errs")); // Lexical reports and cancellation follow the active speculative attempt. ErrorListener lexicalReports = new ErrorListener() { @Override @@ -81,6 +117,9 @@ public boolean isAbortDesired() { }; m_lexer = lexer == null ? new Lexer(source, lexicalReports) : lexer; + f_partialAnalysis = fPartialAnalysis; + f_cursor = cursor; + // prime the token stream next(); } @@ -95,7 +134,10 @@ public boolean isAbortDesired() { * AliasStatements-opt TypeDeclaration * } * - * @return the top level type declaration + *

Recover at statement/declaration boundaries when possible. Recovered syntax is incomplete; + * callers must check their listener for errors before entering semantic compilation. + * + * @return the top level syntax, possibly omitting malformed statements or declarations * * @throws CompilerException if a parsing error occurs while parsing the * source code that forces the parser to abandon its progress before @@ -108,9 +150,13 @@ public StatementBlock parseSource() { // during parsing m_fDone = true; + long lStart = peek().getStartPosition(); List stmts = parseTypeCompositionComponents(null, new ArrayList<>(), true); - m_root = new StatementBlock(stmts, m_source, stmts.getFirst().getStartPosition(), - stmts.getLast().getEndPosition()); + if (stmts.isEmpty() && !f_errs.get().hasSeriousErrors()) { + log(Severity.ERROR, NO_TYPE_FOUND, lStart, m_source.getPosition()); + } + m_root = new StatementBlock(stmts, m_source, lStart, + stmts.isEmpty() ? m_source.getPosition() : stmts.getLast().getEndPosition()); // there shouldn't be more in the file; (note that a zero-length token doesn't count, // since it is probably the synthetic closing '}') @@ -370,14 +416,20 @@ TypeCompositionStatement parseTypeDeclarationStatementAfterModifiers(long lStart name = qualified.get(0); } - // optional type parameters - List typeParams = parseTypeParameterList(false); - - // optional constructor definition - List constructorParams = parseParameterList(false); - - // sequence of compositions - List compositions = parseCompositions(); + Mark header = mark(); + List typeParams = null; + List constructorParams; + List compositions; + try { + typeParams = parseTypeParameterList(false, true); + constructorParams = parseParameterList(false); + compositions = parseCompositions(true); + } catch (IncompleteHeader error) { + return retainTypeDeclaration(header, lStartPos, category, name, qualified, + List.of(error.site), error.withFormals(typeParams)); + } catch (CompilerException error) { + return retainTypeDeclaration(header, lStartPos, category, name, qualified, List.of(), error); + } // TypeCompositionBody StatementBlock body = null; @@ -400,7 +452,7 @@ TypeCompositionStatement parseTypeDeclarationStatementAfterModifiers(long lStart * * @return a list of compositions */ - List parseCompositions() { + List parseCompositions(boolean header) { List compositions = new ArrayList<>(); while (true) { // the keywords below require "match()" to extract them, because they are context @@ -408,18 +460,19 @@ List parseCompositions() { Token keyword; if ((keyword = match(Id.EXTENDS)) != null) { do { - TypeExpression type = parseExtendedTypeExpression(); + TypeExpression type = header ? parseDeclarationType(true) : parseExtendedTypeExpression(); List args = parseArgumentList(false, false, false); compositions.add(new CompositionNode.Extends(null, keyword, type, args, prev().getEndPosition())); } while (match(Id.COMMA) != null); } else if ((keyword = match(Id.IMPLEMENTS)) != null) { do { - compositions.add(new CompositionNode.Implements(null, keyword, parseExtendedTypeExpression())); + compositions.add(new CompositionNode.Implements(null, keyword, + header ? parseDeclarationType(true) : parseExtendedTypeExpression())); } while (match(Id.COMMA) != null); } else if ((keyword = match(Id.DELEGATES)) != null) { do { - TypeExpression type = parseExtendedTypeExpression(); + TypeExpression type = header ? parseDeclarationType(true) : parseExtendedTypeExpression(); expect(Id.L_PAREN); Expression expr = parseExpression(); Token tokEnd = expect(Id.R_PAREN); @@ -435,7 +488,7 @@ List parseCompositions() { TypeExpression type = null; List constraints = null; if (match(Id.CONDITIONAL) == null) { - type = parseTypeExpression(); + type = header ? parseDeclarationType() : parseTypeExpression(); } else { do { // parse the type parameter list e.g. "", @@ -469,7 +522,8 @@ List parseCompositions() { compositions.add(new CompositionNode.Incorporates(null, keyword, type, args, constraints)); } while (match(Id.COMMA) != null); } else if ((keyword = match(Id.INTO)) != null) { - compositions.add(new CompositionNode.Into(null, keyword, parseExtendedTypeExpression())); + compositions.add(new CompositionNode.Into(null, keyword, + header ? parseDeclarationType(true) : parseExtendedTypeExpression())); } else { // not context-sensitive keywords switch (peek().getId()) { case IMPORT: { @@ -601,58 +655,62 @@ StatementBlock parseTypeCompositionBody(Token category) { List parseTypeCompositionComponents(Expression exprCondition, List stmts, boolean fFileLevel) { boolean fFoundType = false; - while (match(Id.R_CURLY) == null) { - Statement stmt; - switch (peek().getId()) { - case IMPORT: - stmt = parseImportStatement(exprCondition); - break; + while (!atBlockEnd(fFileLevel)) { + Mark statementStart = mark(); + try { + Statement stmt; + switch (peek().getId()) { + case IMPORT: + stmt = parseImportStatement(exprCondition); + break; - case TYPEDEF: - stmt = parseTypeDefStatement(exprCondition, null); - break; + case TYPEDEF: + stmt = parseTypeDefStatement(exprCondition, null); + break; - default: { - Token start = peek(); - - stmt = parseTypeCompositionComponent(exprCondition, false); - fFoundType = true; - - if (fFileLevel) { - // module cannot have any statements before it in the source - if (stmt instanceof TypeCompositionStatement stmtType) { - if (stmtType.getCategory().getId() == Id.MODULE && !stmts.isEmpty()) { - if (stmts.stream().allMatch(ImportStatement.class::isInstance)) { - // transfer the imports from outside the module body to inside - // the module body - stmtType.ensureBody().getStatements().addAll(0, stmts); - stmts.clear(); - } else { - log(Severity.ERROR, MODULE_NOT_ROOT, start.getStartPosition(), - start.getEndPosition()); + default: { + Token start = peek(); + + stmt = parseTypeCompositionComponent(exprCondition, false); + fFoundType = true; + + if (fFileLevel) { + // module cannot have any statements before it in the source + if (stmt instanceof TypeCompositionStatement stmtType) { + if (stmtType.getCategory().getId() == Id.MODULE && !stmts.isEmpty()) { + if (stmts.stream().allMatch(ImportStatement.class::isInstance)) { + // transfer the imports from outside the module body to inside + // the module body + stmtType.ensureBody().getStatements().addAll(0, stmts); + stmts.clear(); + } else { + log(Severity.ERROR, MODULE_NOT_ROOT, start.getStartPosition(), + start.getEndPosition()); + } } + } else { + log(Severity.ERROR, NO_TYPE_FOUND, start.getStartPosition(), + start.getEndPosition()); } - } else { - log(Severity.ERROR, NO_TYPE_FOUND, start.getStartPosition(), - start.getEndPosition()); } + break; + } } - break; - } - } - stmts.add(stmt); - if (stmt instanceof MethodDeclarationStatement) { - MethodDeclarationStatement stmtFinally = - ((MethodDeclarationStatement) stmt).getConstructorFinally(); - if (stmtFinally != null) { - stmts.add(stmtFinally); + stmts.add(stmt); + if (stmt instanceof MethodDeclarationStatement method) { + MethodDeclarationStatement stmtFinally = method.getConstructorFinally(); + if (stmtFinally != null) { + stmts.add(stmtFinally); + } } - } - if (fFileLevel && fFoundType) { - // at the file level, there is nothing after the outermost type's conclusion - break; + if (fFileLevel && fFoundType) { + // at the file level, there is nothing after the outermost type's conclusion + break; + } + } catch (CompilerException e) { + recoverStatement(statementStart, e); } } @@ -769,12 +827,19 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet case CONDITIONAL: case VOID: { // it's definitely a method or a function - List typeVars = peek(Id.COMP_LT) ? parseTypeParameterList(true) : null; - Token conditional = match(Id.CONDITIONAL); - List returns = parseReturnList(); - Token name = expect(Id.IDENTIFIER); - return parseMethodDeclarationAfterName(lStartPos, exprCondition, doc, - modifiers, annotations, typeVars, conditional, returns, name); + Mark header = mark(); + List typeVars = null; + try { + typeVars = peek(Id.COMP_LT) ? parseTypeParameterList(true, true) : null; + Token conditional = match(Id.CONDITIONAL); + List returns = parseDeclarationReturns(); + Token name = expect(Id.IDENTIFIER); + return parseMethodDeclarationAfterName(lStartPos, exprCondition, doc, + modifiers, annotations, typeVars, conditional, returns, name); + } catch (IncompleteHeader error) { + return retainDeclaration(header, lStartPos, null, IncompleteDeclarationStatement.Kind.METHOD, + List.of(error.site), error.withFormals(typeVars)); + } } case CONSTRUCT: { @@ -799,6 +864,22 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet } case L_PAREN: { + if (!fInMethod && f_cursor != NO_CURSOR) { + // Outside a method this is declaration syntax, not a tuple value. Probe the + // type grammar without letting a discarded interpretation publish diagnostics. + Mark header = mark(); + var branch = f_errs.get().branch(null); + try (var ignored = reportingTo(branch)) { + parseDeclarationReturns(); + } catch (IncompleteHeader error) { + branch.merge(); + return retainDeclaration(header, lStartPos, null, IncompleteDeclarationStatement.Kind.METHOD, + List.of(error.site), error); + } catch (CompilerException ignored) { + // The ordinary declaration grammar below owns non-cursor failures. + } + restore(header); + } // it's a property or a method, but the property type is parenthesized (odd!) or // the method return types (with optional names) are parenthesized (multi-return), // which means that to know it's a method, we have to find one of: @@ -931,6 +1012,14 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet throw new CompilerException("var or val keyword outside of method"); } + var missingName = declarationNameSlot(new VariableTypeExpression(tokType)); + if (missingName.isPresent() && peek(Id.ASN)) { + current(); + Expression initializer = parseExpression(); + expect(Id.SEMICOLON); + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + return new IncompleteLocalDeclaration(missingName.orElseThrow(), initializer, prev().getEndPosition()); + } Token tokName = matchNameOrAny(); if (tokName == null) { // var and val are not reserved keywords; they are context sensitive types @@ -938,8 +1027,10 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet } else { VariableTypeExpression typeDecl = new VariableTypeExpression(tokType); VariableDeclarationStatement stmtDecl = new VariableDeclarationStatement(typeDecl, tokName, false); - AssignmentStatement stmtAsn = new AssignmentStatement(stmtDecl, match(Id.ASN), parseExpression()); - expect(Id.SEMICOLON); + Token op = match(Id.ASN); + Expression value = parsePartialValue(); + AssignmentStatement stmtAsn = new AssignmentStatement(stmtDecl, op, value); + finishValueStatement(value); return stmtAsn; } } @@ -954,7 +1045,15 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet type = expr.toTypeExpression(); } else { - type = parseTypeExpression(); + Mark header = mark(); + try { + type = parseDeclarationType(); + } catch (IncompleteHeader e) { + Token name = matchNameOrAny(); + var kind = peek(Id.L_PAREN) ? IncompleteDeclarationStatement.Kind.METHOD + : IncompleteDeclarationStatement.Kind.PROPERTY; + return retainDeclaration(header, lStartPos, name, kind, List.of(e.site), e); + } } if (type instanceof BadTypeExpression) { @@ -962,6 +1061,14 @@ Statement parseTypeCompositionComponent(Expression exprCondition, boolean fInMet } // it's a constant, property, or method + if (!fInMethod) { + var missingName = declarationNameSlot(type); + if (missingName.isPresent()) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + return retainDeclaration(mark(), lStartPos, null, IncompleteDeclarationStatement.Kind.PROPERTY, + List.of(missingName.orElseThrow()), new CompilerException("Missing declaration name")); + } + } Token name = expectNameOrAny(); if (peek(Id.COMP_LT) || peek(Id.L_PAREN)) { // '<' indicates redundant return type list @@ -1012,15 +1119,19 @@ Statement parsePossibleExpressionOrAssignmentStatement(Expression expr) { case COND_OR_ASN: case COND_NN_ASN: case COND_ELSE_ASN: { - AssignmentStatement stmt = new AssignmentStatement(expr, current(), parseExpression()); - expect(Id.SEMICOLON); + Token op = current(); + Expression value = op.getId() == Id.ASN ? parsePartialValue() : parseExpression(); + AssignmentStatement stmt = new AssignmentStatement(expr, op, value); + finishValueStatement(value); return stmt; } } if (isMissingStatementSemicolon()) { long lPos = expr.getEndPosition(); - log(Severity.ERROR, MISSING_SEMICOLON, lPos, lPos); + if (!containsCursorHole(expr)) { + log(Severity.ERROR, MISSING_SEMICOLON, lPos, lPos); + } return new ExpressionStatement(expr); } @@ -1074,9 +1185,9 @@ Statement parseVariableDeclarationAfterName(List annotatio Expression value = null; Token op = match(Id.ASN); if (op != null) { - value = parseExpression(); + value = parsePartialValue(); } - expect(Id.SEMICOLON); + finishValueStatement(value); // apply any annotations to the variable type; "@A @B @C T" is "@A of (@B of (@C of T))" if (annotations != null) { @@ -1102,13 +1213,25 @@ Statement parseVariableDeclarationAfterName(List annotatio * StatementBlock * } * - * @return a MethodDeclarationStatement + * @return a method declaration, or retained incomplete header syntax after a parse error */ - MethodDeclarationStatement parseMethodDeclarationAfterName(long lStartPos, Expression exprCondition, + Statement parseMethodDeclarationAfterName(long lStartPos, Expression exprCondition, Token doc, List modifiers, List annotations, List typeVars, Token conditional, List returns, Token name) { - List redundantReturns = parseTypeParameterTypeList(false, true); - List params = parseParameterList(true); + Mark header = mark(); + List redundantReturns; + List params; + try { + redundantReturns = parseTypeParameterTypeList(false, true); + params = parseParameterList(true); + } catch (IncompleteHeader e) { + // This incomplete method has no registered type parameters or own scope yet. + return retainDeclaration(header, lStartPos, name, IncompleteDeclarationStatement.Kind.METHOD, + List.of(e.site), e.withFormals(typeVars)); + } catch (CompilerException e) { + return retainDeclaration(header, lStartPos, name, IncompleteDeclarationStatement.Kind.METHOD, + List.of(), e); + } long lEndPos = prev().getEndPosition(); StatementBlock body; switch (peek().getId()) { @@ -1119,10 +1242,10 @@ MethodDeclarationStatement parseMethodDeclarationAfterName(long lStartPos, Expre case ASN: Token eq = expect(Id.ASN); Expression expr = parseExpression(); - Token semi = expect(Id.SEMICOLON); + finishValueStatement(expr); ReturnStatement stmt = new ReturnStatement(eq, expr); body = new StatementBlock(Arrays.asList(stmt), stmt.getStartPosition(), - semi.getEndPosition()); + prev().getEndPosition()); break; default: body = parseStatementBlock(); @@ -1199,7 +1322,7 @@ PropertyDeclarationStatement parsePropertyDeclarationFinish(long lStartPos, // "=" Expression ";" value = parseExpression(); lEndPos = value.getEndPosition(); - expect(Id.SEMICOLON); + finishValueStatement(value); } else if (body == null || fNeedsSemi) { expect(Id.SEMICOLON); } @@ -1216,13 +1339,47 @@ PropertyDeclarationStatement parsePropertyDeclarationFinish(long lStartPos, StatementBlock parseStatementBlock() { Token tokStart = expect(Id.L_CURLY); List stmts = new ArrayList<>(); - while (match(Id.R_CURLY) == null) { - stmts.add(parseStatement()); + retainEmptyCursor(stmts); + while (!atBlockEnd(false)) { + Mark statementStart = mark(); + long lStart = peek().getStartPosition(); + try { + stmts.add(parseStatement()); + retainEmptyCursor(stmts); + } catch (IncompleteSyntax e) { + // Do not reinterpret a return, assignment, condition or nested call as a + // standalone expression. Only the intact statement prefix is supported. + if (e.statement.getStartPosition() == lStart) { + stmts.add(e.statement); + // The cursor boundary has already consumed the intact prefix. Keep the + // enclosing brace and following declarations instead of scanning the open + // call again as an unmatched parenthesis. + if (f_cursor != NO_CURSOR) { + match(Id.SEMICOLON); + continue; + } + } + recoverStatement(statementStart, e); + } catch (CompilerException e) { + recoverStatement(statementStart, e); + } } return new StatementBlock(stmts, tokStart.getStartPosition(), prev().getEndPosition()); } + /** A zero-width insertion anchor after an intact statement or opening brace. */ + private void retainEmptyCursor(List statements) { + if (f_cursor != NO_CURSOR && (prev().getId() == Id.L_CURLY + || prev().getId() == Id.R_CURLY || prev().getId() == Id.SEMICOLON) + && canRetainIncomplete()) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + statements.add(new IncompleteStatement( + new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION)); + match(Id.SEMICOLON); + } + } + /** * Parse an Ecstasy statement. * @@ -1859,8 +2016,18 @@ ReturnStatement parseReturnStatement() { // note: it is possible that the expression list is parenthesized, in which case it will be // parsed as a single expression (a tuple literal), and the compiler will have to // it out later - List exprs = parseExpressionList(); - expect(Id.SEMICOLON); + long start = peek().getStartPosition(); + List exprs; + try { + exprs = parseExpressionList(); + } catch (IncompleteSyntax e) { + exprs = List.of(retainPartialValue(start, e)); + } + if (exprs.size() == 1) { + finishValueStatement(exprs.getFirst()); + } else { + expect(Id.SEMICOLON); + } return new ReturnStatement(keyword, exprs); } @@ -3059,6 +3226,18 @@ private Expression parsePrefixExpressionBody(boolean fExtended) { * @return an expression */ Expression parsePostfixExpression(boolean fExtended) { + try { + return parsePostfixValue(fExtended); + } catch (IncompleteSyntax e) { + if (f_cursor == NO_CURSOR) { + throw e; + } + return new IncompleteExpression(e.statement); + } + } + + /** Retain a cursor hole as syntax so enclosing operators establish their real contexts. */ + private Expression parsePostfixValue(boolean fExtended) { Expression expr = parsePrimaryExpression(fExtended); while (true) { switch (peek().getId()) { @@ -3080,7 +3259,10 @@ Expression parsePostfixExpression(boolean fExtended) { break; case DOT: { - expect(Id.DOT); + Token dot = expect(Id.DOT); + if (canRetainIncomplete()) { + throw incomplete(expr, dot, List.of(), List.of()); + } switch (peek().getId()) { case NEW: { expr = parseNewExpression(expr); @@ -3110,6 +3292,16 @@ Expression parsePostfixExpression(boolean fExtended) { name = expect(Id.IDENTIFIER); } } + if (noDeRef == null && name.getId() == Id.IDENTIFIER + && name.getStartPosition() < f_cursor && f_cursor <= name.getEndPosition() + && canRetainIncompleteAt(name.getEndPosition(), true)) { + var hole = incomplete(expr, dot, name); + if (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN)) { + expr = new IncompleteExpression(hole.statement); + break; + } + throw hole; + } long lEndPos = name.getEndPosition(); List params = null; @@ -3148,8 +3340,7 @@ Expression parsePostfixExpression(boolean fExtended) { case L_PAREN: case ASYNC_PAREN: // ArgumentList - expr = new InvocationExpression(expr, peek(Id.ASYNC_PAREN), - parseArgumentList(true, true, false), prev().getEndPosition()); + expr = parseInvocation(expr); break; case L_SQUARE: { @@ -3170,7 +3361,8 @@ Expression parsePostfixExpression(boolean fExtended) { } else if (match(Id.COND) == null) { // "someArray[3]" List indexes = parseExpressionList(); - expr = new ArrayAccessExpression(expr, indexes, expect(Id.R_SQUARE)); + expr = new ArrayAccessExpression(expr, indexes, + expectPartialClose(Id.R_SQUARE, indexes)); } else { // "SomeClass[?,?]" int cExplicitDims = 1; @@ -3211,7 +3403,10 @@ NewExpression parseNewExpression(Expression left) { // argument list for a "virtual new"; assume it's a virtual new, and we'll back // up if we were wrong Mark mark = mark(); - args = parseArgumentList(true, false, false); + args = f_partialAnalysis + ? parsePartialArguments(new NewExpression(left, keyword, null, List.of(), + -1, null, keyword.getEndPosition()), false) + : parseArgumentList(true, false, false); lEndPos = prev().getEndPosition(); Token.Id idNext = peek().getId(); @@ -3232,7 +3427,19 @@ NewExpression parseNewExpression(Expression left) { boolean fArray = type instanceof ArrayTypeExpression; if (fArray) { if (peek(Id.L_SQUARE)) { - args = parseArgumentList(true, false, true); + try { + args = f_partialAnalysis + ? parsePartialArguments(new NewExpression(left, keyword, type, List.of(), + ((ArrayTypeExpression) type).getDimensions(), null, lEndPos), false) + : parseArgumentList(true, false, true); + } catch (IncompleteSyntax failure) { + // Finish the construction's suffix so a written supplier does not hide + // the dimension cursor. This prefix query does not validate the supplier. + if (prev().getId() == Id.R_SQUARE) { + parseArgumentList(false, false, false); + } + throw failure; + } dims = args.size(); lEndPos = prev().getEndPosition(); } else { @@ -3240,7 +3447,10 @@ NewExpression parseNewExpression(Expression left) { } // parenthesized arguments after the dims - List argsTrailing = parseArgumentList(false, false, false); + List argsTrailing = f_partialAnalysis && peek(Id.L_PAREN) + ? parsePartialArguments(new NewExpression(left, keyword, type, args, + dims, null, lEndPos), false) + : parseArgumentList(false, false, false); if (argsTrailing != null) { if (!argsTrailing.isEmpty()) { if (args.isEmpty()) { @@ -3252,7 +3462,22 @@ NewExpression parseNewExpression(Expression left) { lEndPos = prev().getEndPosition(); } } else { - args = parseArgumentList(true, false, false); + try { + args = f_partialAnalysis + ? parsePartialArguments(new NewExpression(left, keyword, type, List.of(), + -1, null, type.getEndPosition()), false) + : parseArgumentList(true, false, false); + } catch (IncompleteSyntax failure) { + if (left == null && peek(Id.L_CURLY)) { + // Preserve the anonymous body as construction syntax. A cursor probe + // must not mistake it for ordinary construction followed by a block. + body = parseTypeCompositionBody(keyword); + var site = failure.statement; + site.replaceChild(site.getTarget(), new NewExpression(left, keyword, type, + List.of(), -1, body, prev().getEndPosition())); + } + throw failure; + } lEndPos = prev().getEndPosition(); } @@ -3305,6 +3530,11 @@ NewExpression parseNewExpression(Expression left) { * @return an expression */ Expression parsePrimaryExpression(boolean fExtended) { + if (f_cursor != NO_CURSOR && canRetainIncomplete()) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + return new IncompleteExpression(new IncompleteStatement( + new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION)); + } switch (peek().getId()) { case ANY: { IgnoredNameExpression exprIgnore = new IgnoredNameExpression(current()); @@ -3435,6 +3665,14 @@ Expression parsePrimaryExpression(boolean fExtended) { } left = new NameExpression(left, nameNDR, name, null, lEndPos); + if (fNormal && nameNext.getStartPosition() < f_cursor && f_cursor <= nameNext.getEndPosition() + && canRetainIncompleteAt(nameNext.getEndPosition(), true)) { + var hole = incomplete(left, dot, nameNext); + if (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN)) { + return new IncompleteExpression(hole.statement); + } + throw hole; + } nameNDR = null; // only gets applied once name = nameNext; lEndPos = name.getEndPosition(); @@ -3453,6 +3691,12 @@ Expression parsePrimaryExpression(boolean fExtended) { return expr; } + if (left == null && name.getStartPosition() < f_cursor && f_cursor <= name.getEndPosition() + && canRetainIncompleteAt(name.getEndPosition())) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + throw new IncompleteSyntax(new IncompleteStatement(name, f_cursor, INCOMPLETE_EXPRESSION)); + } + // test for a non-auto-narrowing modifier ("!") Token tokNoNarrow = !peek().hasLeadingWhitespace() ? match(Id.NOT) @@ -3550,7 +3794,10 @@ Expression parsePrimaryExpression(boolean fExtended) { // parsed to see if it's followed by a lambda operator List exprs = new ArrayList<>(); exprs.add(expr); - while (match(Id.R_PAREN, match(Id.COMMA) == null) == null) { + Token tupleClose; + while ((tupleClose = match(Id.COMMA) == null + ? expectPartialClose(Id.R_PAREN, exprs) + : matchPartialClose(Id.R_PAREN, exprs)) == null) { exprs.add(parseExpression()); } @@ -3560,7 +3807,7 @@ Expression parsePrimaryExpression(boolean fExtended) { } // it's a Tuple literal - return new TupleExpression(null, exprs, tokLParen.getStartPosition(), prev().getEndPosition()); + return new TupleExpression(null, exprs, tokLParen.getStartPosition(), tupleClose.getEndPosition()); case R_PAREN: // this is either a parenthesized expression or a single parameter for a @@ -3592,9 +3839,8 @@ Expression parsePrimaryExpression(boolean fExtended) { } default: - expect(Id.R_PAREN); - skipToNextStatement(); - return expr; + return new ParenthesizedExpression(expr, tokLParen.getStartPosition(), + expectPartialClose(Id.R_PAREN, List.of(expr)).getEndPosition()); } } @@ -3698,7 +3944,7 @@ Expression parsePrimaryExpression(boolean fExtended) { byte[] abData = null; try { abData = m_source.includeBinary(sFile); - } catch (IOException ignore) {} + } catch (IOException _) {} if (abData == null) { abData = new byte[0]; fErr = true; @@ -3709,7 +3955,7 @@ Expression parsePrimaryExpression(boolean fExtended) { try { Source source = m_source.includeString(sFile); sData = source == null ? null : source.toRawString(); - } catch (IOException ignore) {} + } catch (IOException _) {} if (sData == null) { sData = ""; fErr = true; @@ -4078,12 +4324,13 @@ Expression parseComplexLiteral(TypeExpression type) { if (peek().getId() == Id.ASN) { List keys = new ArrayList<>(); List values = new ArrayList<>(); - while (match(Id.R_SQUARE) == null) { + Token close; + while ((close = match(Id.R_SQUARE)) == null) { keys.add(keys.isEmpty() ? expr : parseExpression()); expect(Id.ASN); values.add(parseExpression()); if (match(Id.COMMA) == null) { - expect(Id.R_SQUARE); + close = expectPartialClose(Id.R_SQUARE, values); break; } } @@ -4092,12 +4339,12 @@ Expression parseComplexLiteral(TypeExpression type) { Token tokName = new Token(ofMap, ofMap, Id.IDENTIFIER, "Map"); TypeExpression exprType = new NamedTypeExpression(null, Collections.singletonList(tokName), null, null, null, ofMap); - return new MapExpression(exprType, keys, values, prev().getEndPosition()); + return new MapExpression(exprType, keys, values, close.getEndPosition()); } } - expect(Id.R_SQUARE); - break; + return new ListExpression(type, exprs, lStartPos, + expectPartialClose(Id.R_SQUARE, exprs).getEndPosition()); } } return new ListExpression(type, exprs, lStartPos, prev().getEndPosition()); @@ -4126,8 +4373,8 @@ Expression parseComplexLiteral(TypeExpression type) { expect(Id.ASN); values.add(parseExpression()); if (match(Id.COMMA) == null) { - expect(Id.R_SQUARE); - break; + return new MapExpression(type, keys, values, + expectPartialClose(Id.R_SQUARE, values).getEndPosition()); } } return new MapExpression(type, keys, values, prev().getEndPosition()); @@ -4135,17 +4382,16 @@ Expression parseComplexLiteral(TypeExpression type) { case "Tuple": { expect(Id.L_PAREN); - List exprs = null; - while (match(Id.R_PAREN) == null) { - if (exprs == null) { - exprs = new ArrayList<>(); - } else { + List exprs = new ArrayList<>(); + Token close; + while ((close = matchPartialClose(Id.R_PAREN, exprs)) == null) { + if (!exprs.isEmpty()) { expect(Id.COMMA); } exprs.add(parseExpression()); } return new TupleExpression(type, exprs, type.getStartPosition(), - prev().getEndPosition()); + close.getEndPosition()); } case "Path": @@ -4230,11 +4476,16 @@ public TypeExpression parseExtendedTypeExpression() { * @return a type expression */ TypeExpression parseIntersectingTypeExpression(boolean fExtended) { - TypeExpression expr = parseUnionedTypeExpression(fExtended); + return parseIntersectingTypeExpression(fExtended, false); + } + + /** Pass header recovery explicitly through the type grammar; other type contexts stay strict. */ + private TypeExpression parseIntersectingTypeExpression(boolean fExtended, boolean fHeader) { + TypeExpression expr = parseUnionedTypeExpression(fExtended, fHeader); Token tokOp; do { if ((tokOp = match(Id.ADD)) != null || (tokOp = match(Id.SUB)) != null) { - expr = new BiTypeExpression(expr, tokOp, parseUnionedTypeExpression(fExtended)); + expr = new BiTypeExpression(expr, tokOp, parseUnionedTypeExpression(fExtended, fHeader)); } } while (tokOp != null); return expr; @@ -4257,10 +4508,10 @@ TypeExpression parseIntersectingTypeExpression(boolean fExtended) { * * @return a type expression */ - TypeExpression parseUnionedTypeExpression(boolean fExtended) { - TypeExpression expr = parseNonBiTypeExpression(fExtended); + TypeExpression parseUnionedTypeExpression(boolean fExtended, boolean fHeader) { + TypeExpression expr = parseNonBiTypeExpression(fExtended, fHeader); while (peek(Id.BIT_OR)) { - expr = new BiTypeExpression(expr, expect(Id.BIT_OR), parseNonBiTypeExpression(fExtended)); + expr = new BiTypeExpression(expr, expect(Id.BIT_OR), parseNonBiTypeExpression(fExtended, fHeader)); } return expr; } @@ -4320,9 +4571,9 @@ TypeExpression parseUnionedTypeExpression(boolean fExtended) { * * @return a type expression */ - TypeExpression parseNonBiTypeExpression(boolean fExtended) { + TypeExpression parseNonBiTypeExpression(boolean fExtended, boolean fHeader) { try (var ignore = nest()) { - return parseNonBiTypeExpressionBody(fExtended); + return parseNonBiTypeExpressionBody(fExtended, fHeader); } } @@ -4333,22 +4584,23 @@ TypeExpression parseNonBiTypeExpression(boolean fExtended) { * * @return a type expression */ - private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) { + private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended, boolean fHeader) { + retainEmptyTypeArgument(fHeader); TypeExpression type; Token tokAccess = null; switch (peek().getId()) { case L_PAREN: expect(Id.L_PAREN); - type = parseExtendedTypeExpression(); - expect(Id.R_PAREN); + type = parseIntersectingTypeExpression(true, fHeader); + expectHeaderTypeClose(Id.R_PAREN, List.of(type), fHeader); break; case AT: - type = parseAnnotatedTypeExpression(fExtended); + type = parseAnnotatedTypeExpression(fExtended, fHeader); break; case FUNCTION: - type = parseFunctionTypeExpression(); + type = parseFunctionTypeExpression(fHeader); break; case IMMUTABLE: { @@ -4368,7 +4620,7 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) { } } - type = new DecoratedTypeExpression(tokImmut, parseNonBiTypeExpression(fExtended)); + type = new DecoratedTypeExpression(tokImmut, parseNonBiTypeExpression(fExtended, fHeader)); break; } @@ -4397,54 +4649,67 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) { } tokAccess = match(Id.STRUCT); } - type = parseNamedTypeExpression(tokAccess); + type = parseNamedTypeExpression(tokAccess, fHeader); break; } while (true) { switch (peek().getId()) { - case L_SQUARE: - // this could be either: - // -> NonBiTypeExpression ArrayDims - // -> NonBiTypeExpression ArrayIndexes - // in the case of the ArrayIndexes, we do NOT consume that portion of the - // expression; we use it to give us a dimension count, as if it were ArrayDims + case L_SQUARE: { + // Sizes belong to the surrounding construction, not to its type. Look ahead + // with a branch so a cursor hole is reported only when the sizes are reparsed + // with their owning NewExpression. No diagnostic escapes a discarded lookahead. Mark mark = mark(); - - expect(Id.L_SQUARE); - int cDims = 0; - int cIndexes = 0; - while (match(Id.R_SQUARE) == null) { - if (cDims + cIndexes > 0) { - expect(Id.COMMA); - } - - Token dim = peek(); // just for error reporting - if (match(Id.COND) == null) { - parseExpression(); - if (cIndexes == 0 && cDims > 0) { - // just log the first one that deviates - log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition()); + var branch = f_errs.get().branch(null); + try (var ignored = reportingTo(branch)) { + expect(Id.L_SQUARE); + int cDims = 0; + int cIndexes = 0; + while (true) { + if (f_cursor != NO_CURSOR && canRetainIncomplete()) { + int dimensions = cDims + cIndexes + 1 + (peek(Id.COMMA) ? 1 : 0); + restore(mark); + return new ArrayTypeExpression(type, dimensions, f_cursor); } - ++cIndexes; - } else { // we ate the "?" - if (cDims == 0 && cIndexes > 0) { - // just log the first one that deviates - log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition()); + if (match(Id.R_SQUARE) != null) { + break; + } + if (cDims + cIndexes > 0) { + expect(Id.COMMA); } - ++cDims; - } - } - long lEndPos = prev().getEndPosition(); - type = new ArrayTypeExpression(type, cDims + cIndexes, lEndPos); - // if there were only indexes, then we need to leave them in place because the - // type expression does not consume them - if (cDims == 0 && cIndexes > 0) { - restore(mark); - return type; + Token dim = peek(); + if (match(Id.COND) == null) { + Expression index = f_partialAnalysis ? parsePartialValue() : parseExpression(); + ++cIndexes; + if (f_cursor != NO_CURSOR && (containsCursorHole(index) || canRetainIncomplete())) { + int dimensions = cDims + cIndexes + (peek(Id.COMMA) ? 1 : 0); + long end = Math.max(f_cursor, prev().getEndPosition()); + restore(mark); + return new ArrayTypeExpression(type, dimensions, end); + } + if (cIndexes == 1 && cDims > 0) { + log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition()); + } + } else { + if (cDims == 0 && cIndexes > 0) { + log(Severity.ERROR, ALL_OR_NO_DIMS, dim.getStartPosition(), dim.getEndPosition()); + } + ++cDims; + } + } + type = new ArrayTypeExpression(type, cDims + cIndexes, prev().getEndPosition()); + if (cDims == 0 && cIndexes > 0) { + restore(mark); + return type; + } + } catch (CompilerException error) { + branch.merge(); + throw error; } + branch.merge(); break; + } case COND: if (!peek().hasLeadingWhitespace()) { @@ -4475,9 +4740,9 @@ private TypeExpression parseNonBiTypeExpressionBody(boolean fExtended) { * * @return an AnnotatedTypeExpression */ - AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended) { + AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended, boolean fHeader) { AnnotationExpression annotation = parseAnnotation(true); - TypeExpression type = parseNonBiTypeExpression(fExtended); + TypeExpression type = parseNonBiTypeExpression(fExtended, fHeader); return new AnnotatedTypeExpression(annotation, type); } @@ -4497,19 +4762,23 @@ AnnotatedTypeExpression parseAnnotatedTypeExpression(boolean fExtended) { * @return a FunctionTypeExpression */ FunctionTypeExpression parseFunctionTypeExpression() { + return parseFunctionTypeExpression(false); + } + + private FunctionTypeExpression parseFunctionTypeExpression(boolean header) { Token function = expect(Id.FUNCTION); // return values Token conditional = match(Id.CONDITIONAL); - List listReturn = parseReturnList(); + List listReturn = parseReturnList(header); // see if the parameters precede the name - List listParam = parseParameterTypeList(false); + List listParam = parseParameterTypeList(false, header); if (listParam == null) { // name optionally comes before or after the parameters Token name = expect(Id.IDENTIFIER); - listParam = parseParameterTypeList(true); + listParam = parseParameterTypeList(true, header); // pretend the name is the next token (as if we didn't eat it already) putBack(name); @@ -4540,6 +4809,10 @@ FunctionTypeExpression parseFunctionTypeExpression() { * @return a NamedTypeExpression */ NamedTypeExpression parseNamedTypeExpression(Token tokAccess) { + return parseNamedTypeExpression(tokAccess, false); + } + + private NamedTypeExpression parseNamedTypeExpression(Token tokAccess, boolean fHeader) { NamedTypeExpression expr = null; do { if (expr != null) { @@ -4547,7 +4820,22 @@ NamedTypeExpression parseNamedTypeExpression(Token tokAccess) { } // QualifiedName - List names = parseQualifiedName(); + List names = new ArrayList<>(); + do { + boolean hole = fHeader && f_cursor != NO_CURSOR && canRetainTypeSlot(); + Token name = hole ? new Token(f_cursor, f_cursor, Id.IDENTIFIER, "") : expect(Id.IDENTIFIER); + names.add(name); + // A cursor in a qualifier owns this token only; the remaining dotted suffix + // stays in source. A trailing dot owns a zero-width insertion slot. + if (fHeader && (hole || peek(Id.DOT) && name.getStartPosition() < f_cursor + && f_cursor <= name.getEndPosition())) { + var selected = expr == null + ? new NamedTypeExpression(null, names, tokAccess, null, null, name.getEndPosition()) + : new NamedTypeExpression(expr, names, null, name.getEndPosition()); + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + throw new IncompleteHeader(IncompleteStatement.forDeclarationType(selected, f_cursor)); + } + } while (match(Id.DOT) != null); if (tokAccess != null && expr != null) { log(Severity.ERROR, NO_CHILD_ACCESS, tokAccess.getStartPosition(), @@ -4565,7 +4853,7 @@ NamedTypeExpression parseNamedTypeExpression(Token tokAccess) { } // TypeParameterTypeList - List params = parseTypeParameterTypeList(false, true); + List params = parseTypeParameterTypeList(false, true, fHeader); if (expr == null) { expr = new NamedTypeExpression(null, names, tokAccess, tokNarrow, params, @@ -4783,6 +5071,10 @@ AnnotationExpression parseAnnotation(boolean required) { * @return a list of zero or more type parameters, or null if there were no angle brackets */ List parseTypeParameterList(boolean required) { + return parseTypeParameterList(required, false); + } + + private List parseTypeParameterList(boolean required, boolean header) { List typeParams = null; if (match(Id.COMP_LT, required) != null) { typeParams = new ArrayList<>(); @@ -4797,7 +5089,12 @@ List parseTypeParameterList(boolean required) { Token param = expect(Id.IDENTIFIER); TypeExpression type = null; if (match(Id.EXTENDS) != null) { - type = parseExtendedTypeExpression(); + try { + type = header ? parseDeclarationType(true) : parseExtendedTypeExpression(); + } catch (IncompleteHeader error) { + throw error.withFormals(Stream.concat(typeParams.stream(), + Stream.of(new Parameter(new BadTypeExpression((Expression) error.writtenType.clone()), param))).toList()); + } } typeParams.add(new Parameter(type, param)); } @@ -4827,13 +5124,19 @@ List parseTypeParameterList(boolean required) { * @return a list of zero or more types, or null if there were no angle brackets */ List parseTypeParameterTypeList(boolean required, boolean fAllowTypeSequence) { + return parseTypeParameterTypeList(required, fAllowTypeSequence, false); + } + + private List parseTypeParameterTypeList(boolean required, boolean fAllowTypeSequence, + boolean fHeader) { List types = null; if (match(Id.COMP_LT, required) != null) { + retainEmptyTypeArgument(fHeader); if (match(Id.COMP_GT) != null) { types = Collections.emptyList(); } else { - types = parseTypeExpressionList(fAllowTypeSequence); - expect(Id.COMP_GT); + types = parseTypeExpressionList(fAllowTypeSequence, fHeader); + expectHeaderTypeClose(Id.COMP_GT, types, fHeader); } } return types; @@ -4851,23 +5154,48 @@ List parseTypeParameterTypeList(boolean required, boolean fAllow * @return a list of type expressions */ List parseTypeExpressionList(boolean fAllowTypeSequence) { + return parseTypeExpressionList(fAllowTypeSequence, false); + } + + private List parseTypeExpressionList(boolean fAllowTypeSequence, boolean fHeader) { List types = new ArrayList<>(); while (true) { if (!types.isEmpty() && match(Id.COMMA) == null) { return types; } + retainEmptyTypeArgument(fHeader); + if (fAllowTypeSequence && peek(Id.COMP_LT)) { Token tokStart = peek(); - List listSeq = parseTypeParameterTypeList(true, false); - Token tokEnd = prev(); + List listSeq = parseTypeParameterTypeList(true, false, fHeader); + Token tokEnd = prev(); types.add(new TupleTypeExpression(listSeq, tokStart.getStartPosition(), tokEnd.getEndPosition())); } else { - types.add(parseExtendedTypeExpression()); + types.add(parseIntersectingTypeExpression(true, fHeader)); } } } + /** An explicit empty generic slot has no invented type expression or generic owner. */ + private void retainEmptyTypeArgument(boolean header) { + if (header && f_cursor != NO_CURSOR && canRetainTypeSlot()) { + throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, "")); + } + } + + /** Only explicit cursor probes can select an empty type slot at a grammar boundary. */ + private boolean canRetainTypeSlot() { + return m_cSpeculating == 0 && !m_fAvoidRecovery && !f_errs.get().isAbortDesired() + && prev().getEndPosition() <= f_cursor + && f_cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition()) + && (switch (peek().getId()) { + case COMP_GT, COMP_GTEQ, SHR, SHR_ASN, USHR, USHR_ASN, BIT_OR, ADD, SUB -> true; + case IDENTIFIER -> f_cursor < peek().getStartPosition(); + default -> canRetainIncomplete(); + }); + } + /** * Parse a list of package injections, starting with the opening parenthesis. * @@ -4939,21 +5267,226 @@ List parseParameterList(boolean required) { List params = null; if (match(Id.L_PAREN, required) != null) { params = new ArrayList<>(); + if (f_cursor != NO_CURSOR && canRetainIncomplete()) { + throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, "")); + } if (match(Id.R_PAREN) == null) { - do { - TypeExpression type = parseTypeExpression(); + while (true) { + TypeExpression type = parseDeclarationType(); + var missingName = declarationNameSlot(type); + if (missingName.isPresent()) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + throw new IncompleteHeader(missingName.orElseThrow()); + } Token name = expect(Id.IDENTIFIER); Expression value = null; if (match(Id.ASN) != null) { value = parseExpression(); } params.add(new Parameter(type, name, value)); - } while (match(Id.R_PAREN, (match(Id.COMMA) == null)) == null); + Token comma = match(Id.COMMA); + if (comma != null && f_cursor != NO_CURSOR && canRetainIncomplete()) { + throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, "")); + } + if ((comma == null ? expectPartialClose(Id.R_PAREN, params) : match(Id.R_PAREN)) != null) { + break; + } + } } } return params; } + /** A header type prefix is queried in its enclosing declaration, never as a value. */ + private TypeExpression parseDeclarationType() { + return parseDeclarationType(false); + } + + /** + * Only a grammar position that requires a name may retain an empty name slot. In a method + * body, a bare expression followed by whitespace is not evidence of a variable declaration. + * Keep the complete written type as syntax; no parameter or property is registered. + */ + private Optional declarationNameSlot(TypeExpression type) { + return f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery + && !f_errs.get().isAbortDesired() && !(type instanceof BadTypeExpression) + && prev().getEndPosition() < f_cursor + && f_cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition()) + && m_source.toString(prev().getEndPosition(), f_cursor).isBlank() + && (eof() || peek(Id.ASN) || peek(Id.SEMICOLON) || peek(Id.R_CURLY) + || peek(Id.R_PAREN) || peek(Id.COMMA)) + ? Optional.of(IncompleteStatement.forDeclarationName(type, prev(), f_cursor)) + : Optional.empty(); + } + + private TypeExpression parseDeclarationType(boolean extended) { + if (f_cursor != NO_CURSOR && canRetainIncomplete()) { + throw incompleteHeader(new Token(f_cursor, f_cursor, Id.IDENTIFIER, "")); + } + boolean header = f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery + && !f_errs.get().isAbortDesired(); + TypeExpression type = parseIntersectingTypeExpression(extended, header); + if (header) { + var prefix = declarationTypePrefix(type); + if (prefix.isPresent()) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + throw new IncompleteHeader(IncompleteStatement.forDeclarationType(prefix.orElseThrow(), f_cursor), type, List.of()); + } + } + return type; + } + + /** + * Select a written type name, including a generic base whose arguments follow the cursor. The + * caller transfers this original node into the cursor site; ordinary adoption and cloning + * then own it. This is syntax selection, not validation of the enclosing type's constraints. + */ + private Optional declarationTypePrefix(TypeExpression type) { + if (type instanceof NamedTypeExpression named && named.getModule() == null + && named.getNameToken() != null && named.getNameToken().getStartPosition() < f_cursor + && f_cursor <= named.getNameToken().getEndPosition()) { + return Optional.of(named); + } + var types = type instanceof FunctionTypeExpression function + ? Stream.concat(function.getReturnValues().stream().map(Parameter::getType), + function.getParamTypes().stream()) + : StreamSupport.stream(type.children().spliterator(), false) + .filter(TypeExpression.class::isInstance).map(TypeExpression.class::cast); + return types.flatMap(child -> declarationTypePrefix(child).stream()).findFirst(); + } + + /** Missing type closers are tolerated only around a selected header prefix, never in normal parsing. */ + private void expectHeaderTypeClose(Id close, List types, boolean header) { + if (match(close) != null) { + return; + } + if (header && m_cSpeculating == 0 && !m_fAvoidRecovery && !f_errs.get().isAbortDesired() + && (atMissingClose() || peek(Id.IDENTIFIER) || peek(Id.COMP_GT)) + && types.stream().anyMatch(type -> declarationTypePrefix(type).isPresent())) { + return; + } + expect(close); + } + + private IncompleteHeader incompleteHeader(Token prefix) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + return new IncompleteHeader(new IncompleteStatement(prefix, f_cursor, INCOMPLETE_EXPRESSION)); + } + + private static class IncompleteHeader extends CompilerException { + private IncompleteHeader(IncompleteStatement site) { + this(site, site.getTarget(), List.of()); + } + + private IncompleteHeader(IncompleteStatement site, Expression writtenType, List formals) { + super("Incomplete declaration header"); + this.site = site; + this.formals = formals; + this.writtenType = writtenType; + } + + private IncompleteHeader withFormals(List parameters) { + return parameters == null ? this : new IncompleteHeader(site, writtenType, + Stream.concat(formals.stream(), parameters.stream()).distinct().toList()); + } + + private final IncompleteStatement site; + private final List formals; + private final Expression writtenType; + } + + /** Keep a type's real body for structure, but never register an unfinished inheritance header. */ + private TypeCompositionStatement retainTypeDeclaration(Mark header, long start, Token category, + Token name, List qualified, List sites, CompilerException error) { + if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) { + throw error; + } + restore(header); + while (!eof() && !peek(Id.R_CURLY)) { + if (f_errs.get().isAbortDesired()) { + throw error; + } + if (peek(Id.L_CURLY)) { + StatementBlock body = parseTypeCompositionBody(category); + return new IncompleteTypeCompositionStatement(m_source, category, name, qualified, body, + start, body.getEndPosition(), sites, + error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of()); + } + if (match(Id.SEMICOLON) != null) { + break; + } + current(); + } + return new IncompleteTypeCompositionStatement(m_source, category, name, qualified, null, + start, retainedDeclarationEnd(sites), sites, + error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of()); + } + + /** + * Retain only the known declaration name and source extent. A brace or semicolon bounds + * this recovery; the body is skipped as one unit so its names cannot leak into the owner. + * Unfinished parameter syntax never registers a partial method signature. + */ + private IncompleteDeclarationStatement retainDeclaration(Mark header, long start, Token name, + IncompleteDeclarationStatement.Kind kind, List sites, CompilerException error) { + if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) { + throw error; + } + restore(header); + while (!eof() && !peek(Id.R_CURLY)) { + if (f_errs.get().isAbortDesired()) { + throw error; + } + if (peek(Id.L_CURLY)) { + current(); + // Consume the body by brace depth so missing parameter delimiters cannot eat + // its siblings. current() also preserves the actual closing token's range. + int depth = 1; + while (depth > 0 && !eof()) { + if (f_errs.get().isAbortDesired()) { + throw error; + } + switch (current().getId()) { + case L_CURLY -> ++depth; + case R_CURLY -> --depth; + default -> {} + } + } + break; + } + if (match(Id.SEMICOLON) != null) { + break; + } + current(); + } + return new IncompleteDeclarationStatement(kind, name, start, retainedDeclarationEnd(sites), sites, + error instanceof IncompleteHeader incomplete ? incomplete.formals : List.of()); + } + + /** An empty name slot at EOF includes the whitespace after its last written type token. */ + private long retainedDeclarationEnd(List sites) { + return Math.max(prev().getEndPosition(), sites.stream().mapToLong(IncompleteStatement::getEndPosition) + .max().orElse(prev().getEndPosition())); + } + + /** Declaration return slots select types without publishing an unfinished callable. */ + private List parseDeclarationReturns() { + if (f_cursor == NO_CURSOR) { + return parseReturnList(); + } + if (match(Id.VOID) != null) { + return List.of(); + } + if (match(Id.L_PAREN) == null) { + return List.of(new Parameter(parseDeclarationType())); + } + List returns = new ArrayList<>(); + do { + returns.add(new Parameter(parseDeclarationType(), matchNameOrAny())); + } while (match(Id.R_PAREN, match(Id.COMMA) == null) == null); + return returns; + } + /** * Parse a list of parameter types (without parameter names). * @@ -4966,12 +5499,16 @@ List parseParameterList(boolean required) { * @return */ List parseParameterTypeList(boolean required) { + return parseParameterTypeList(required, false); + } + + private List parseParameterTypeList(boolean required, boolean header) { List types = null; if (match(Id.L_PAREN, required) != null) { types = peek(Id.R_PAREN) ? Collections.emptyList() - : parseTypeExpressionList(false); - expect(Id.R_PAREN); + : parseTypeExpressionList(false, header); + expectHeaderTypeClose(Id.R_PAREN, types, header); } return types; } @@ -5042,62 +5579,281 @@ List parseArgumentList(boolean required, boolean allowCurrying, bool List args = new ArrayList<>(); if (match(idClose) == null) { do { - Token label = null; - if (!fArray) { - // special case where the parameter names are being specified with the arguments - if (peek(Id.IDENTIFIER)) { - Token name = expect(Id.IDENTIFIER); - if (match(Id.ASN) == null) { - // oops, it wasn't a "name=value" argument - putBack(name); - } else { - label = name; - } - } - } + args.add(parseArgument(allowCurrying, fArray)); + } while (match(idClose, (match(Id.COMMA) == null)) == null); + } - Expression expr; - if (allowCurrying && !fArray) { - switch (peek().getId()) { - case ANY: { - // one of two cases: an unbound argument, or a lambda whose one parameter is - // ignored - Token tokUnbound = expect(Id.ANY); - if (peek(Id.LAMBDA)) { - putBack(tokUnbound); - expr = parseExpression(); - } else { - expr = new NonBindingExpression(tokUnbound.getStartPosition(), - tokUnbound.getEndPosition(), null); - } - break; - } + return args; + } - case COMP_LT: { - Token tokOpen = expect(Id.COMP_LT); - TypeExpression type = parseTypeExpression(); - Token tokClose = expect(Id.COMP_GT); - Token tokUnbound = expect(Id.ANY); - expr = new NonBindingExpression(tokOpen.getStartPosition(), - tokUnbound.getEndPosition(), type); - break; - } + /** Parse one argument, shared by calls, constructors and annotation argument lists. */ + private Expression parseArgument(boolean allowCurrying, boolean fArray) { + return parseArgument(allowCurrying, fArray, false); + } - default: - expr = parseExpression(); - break; - } + private Expression parseArgument(boolean allowCurrying, boolean fArray, boolean partial) { + Token label = null; + if (!fArray) { + // special case where the parameter names are being specified with the arguments + if (peek(Id.IDENTIFIER)) { + Token name = expect(Id.IDENTIFIER); + if (match(Id.ASN) == null) { + // oops, it wasn't a "name=value" argument + putBack(name); } else { + label = name; + } + } + } + + if (partial && canRetainIncomplete() && peek(Id.COMMA)) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + Expression hole = new IncompleteExpression(new IncompleteStatement( + new Token(f_cursor, f_cursor, Id.IDENTIFIER, ""), f_cursor, INCOMPLETE_EXPRESSION)); + return label == null ? hole : new LabeledExpression(label, hole); + } + if (partial && label != null && canRetainIncomplete()) { + throw new IncompleteArgument(label); + } + Expression expr; + if (allowCurrying && !fArray) { + switch (peek().getId()) { + case ANY: { + // one of two cases: an unbound argument, or a lambda whose one parameter is + // ignored + Token tokUnbound = expect(Id.ANY); + if (peek(Id.LAMBDA)) { + putBack(tokUnbound); expr = parseExpression(); + } else { + expr = new NonBindingExpression(tokUnbound.getStartPosition(), + tokUnbound.getEndPosition(), null); } + break; + } - args.add(label == null ? expr : new LabeledExpression(label, expr)); - } while (match(idClose, (match(Id.COMMA) == null)) == null); + case COMP_LT: { + Token tokOpen = expect(Id.COMP_LT); + TypeExpression type = parseTypeExpression(); + Token tokClose = expect(Id.COMP_GT); + Token tokUnbound = expect(Id.ANY); + expr = new NonBindingExpression(tokOpen.getStartPosition(), + tokUnbound.getEndPosition(), type); + break; + } + + default: + expr = partial ? parsePartialValue() : parseExpression(); + break; + } + } else { + expr = partial ? parsePartialValue() : parseExpression(); + } + + return label == null ? expr : new LabeledExpression(label, expr); + } + + /** Retain the opening token and separators only for the explicit partial-analysis parser. */ + private Expression parseInvocation(Expression callee) { + if (!f_partialAnalysis) { + return new InvocationExpression(callee, peek(Id.ASYNC_PAREN), + parseArgumentList(true, true, false), prev().getEndPosition()); } + boolean async = peek(Id.ASYNC_PAREN); + List args = parsePartialArguments(callee, true); + return new InvocationExpression(callee, async, args, prev().getEndPosition()); + } + + /** Shared cursor syntax for ordinary calls and constructor arguments. */ + private List parsePartialArguments(Expression callee, boolean allowBindings) { + boolean array = callee instanceof NewExpression && peek(Id.L_SQUARE); + Id close = array ? Id.R_SQUARE : Id.R_PAREN; + Token open = array || allowBindings && peek(Id.ASYNC_PAREN) ? current() : expect(Id.L_PAREN); + List args = new ArrayList<>(); + List separators = new ArrayList<>(); + boolean incomplete = containsCursorHole(callee); + if (canRetainIncomplete() && !peek(Id.COMMA)) { + match(close); + throw incomplete(callee, open, args, separators); + } + if (match(close) == null) { + while (true) { + Expression argument; + try { + argument = parseArgument(allowBindings, array, f_cursor != NO_CURSOR); + } catch (IncompleteArgument e) { + match(close); + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + throw new IncompleteSyntax(IncompleteStatement.forNamedArgument( + callee, open, args, separators, f_cursor, e.name)); + } + Expression value = argument instanceof LabeledExpression labeled + ? labeled.getUnderlyingExpression() : argument; + // A direct final argument prefix belongs to this call's fitting context. Keep + // its token rather than validating it as a value or retaining a second site. + // Compound/member expressions and slots before later arguments keep their sites. + if (value instanceof IncompleteExpression partial && partial.getSite().isNameCompletion() + && canRetainIncomplete() && !peek(Id.COMMA)) { + match(close); + throw new IncompleteSyntax(IncompleteStatement.forArgumentPrefix(callee, open, + args, separators, f_cursor, + argument instanceof LabeledExpression labeled ? labeled.getNameToken() : null, + partial.getSite().getMemberName().orElseThrow())); + } + args.add(argument); + if (containsCursorHole(value)) { + incomplete = true; + } + Token comma = match(Id.COMMA); + if (comma != null) { + separators.add(comma); + } + if (canRetainIncomplete()) { + match(close); + throw incomplete(callee, open, args, separators); + } + if (comma == null && incomplete && !peek(close) && atMissingClose()) { + break; + } + if (match(close, comma == null) != null) { + break; + } + } + } + if (incomplete) { + throw incomplete(callee, open, args, separators); + } return args; } + private boolean canRetainIncomplete() { + return canRetainIncompleteAt(f_cursor); + } + + /** A mid-token value cursor uses the same recovery boundary after its complete written token. */ + private boolean canRetainIncompleteAt(long cursor) { + return canRetainIncompleteAt(cursor, false); + } + + /** A selected member name can precede a call whose arguments must still be parsed and owned. */ + private boolean canRetainIncompleteAt(long cursor, boolean allowCall) { + if (!f_partialAnalysis || m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) { + return false; + } + if (cursor == NO_CURSOR) { + return eof(); + } + return prev().getEndPosition() <= cursor + && cursor <= (eof() ? m_source.getPosition() : peek().getStartPosition()) + && (eof() || peek(Id.R_CURLY) || peek(Id.SEMICOLON) || peek(Id.R_PAREN) + || peek(Id.R_SQUARE) || peek(Id.COMMA) || peek(Id.COLON) || peek(Id.L_CURLY) + || prev().getId() == Id.IDENTIFIER && switch (peek().getId()) { + case ADD, SUB, MUL, DIV, MOD, DIVREM, SHL, SHR, USHR, + BIT_AND, BIT_OR, BIT_XOR, COND_AND, COND_OR, COND_XOR, + COMP_EQ, COMP_NEQ, COMP_LT, COMP_LTEQ, COMP_GT, COMP_GTEQ, + COMP_ORD, COND_ELSE -> true; + case COND -> peek().hasLeadingWhitespace(); + default -> false; + } + || allowCall && (peek(Id.L_PAREN) || peek(Id.ASYNC_PAREN))); + } + + /** + * Retain enclosing syntax around a cursor hole at a statement/outer-delimiter boundary. + * The existing incomplete-expression diagnostic covers the hole and its missing suffix; + * this zero-width end marker neither edits the source nor consumes an enclosing delimiter. + * Without that hole, ordinary parsing (including speculation and error budgets) still fails. + */ + private Token expectPartialClose(Id close, List expressions) { + Token token = matchPartialClose(close, expressions); + return token == null ? expect(close) : token; + } + + private Token matchPartialClose(Id close, List expressions) { + Token token = match(close); + if (token != null) { + return token; + } + if (atMissingClose() && expressions.stream().anyMatch(this::containsCursorHole)) { + long end = Math.max(f_cursor, prev().getEndPosition()); + return new Token(end, end, close); + } + return null; + } + + private boolean atMissingClose() { + return f_cursor != NO_CURSOR && m_cSpeculating == 0 && !m_fAvoidRecovery + && !f_errs.get().isAbortDesired() + && (eof() || peek(Id.SEMICOLON) || peek(Id.R_CURLY) + || peek(Id.R_PAREN) || peek(Id.R_SQUARE) || peek(Id.L_CURLY)); + } + + /** Query syntax ownership rather than caching cursor state on ordinary AST nodes. */ + private boolean containsCursorHole(AstNode node) { + return f_cursor != NO_CURSOR && PartialSyntax.containsAt(node, f_cursor); + } + + private IncompleteSyntax incomplete(Expression target, Token operator, + List arguments, List separators) { + long position = f_cursor == NO_CURSOR ? m_source.getPosition() : f_cursor; + String code = f_cursor == NO_CURSOR ? UNEXPECTED_EOF : INCOMPLETE_EXPRESSION; + log(Severity.ERROR, code, position, position); + return new IncompleteSyntax( + new IncompleteStatement(target, operator, arguments, separators, position, code)); + } + + private IncompleteSyntax incomplete(Expression receiver, Token dot, Token memberName) { + log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); + return new IncompleteSyntax(new IncompleteStatement( + receiver, dot, memberName, f_cursor, INCOMPLETE_EXPRESSION)); + } + + /** Preserve the enclosing statement only when its whole value is the incomplete operation. */ + private Expression parsePartialValue() { + long start = peek().getStartPosition(); + try { + return parseExpression(); + } catch (IncompleteSyntax e) { + return retainPartialValue(start, e); + } + } + + private Expression retainPartialValue(long start, IncompleteSyntax error) { + if (f_cursor == NO_CURSOR || error.statement.getStartPosition() != start) { + throw error; + } + return new IncompleteExpression(error.statement); + } + + private void finishValueStatement(Expression value) { + if (value instanceof IncompleteExpression || atMissingClose() && containsCursorHole(value)) { + match(Id.SEMICOLON); + } else { + expect(Id.SEMICOLON); + } + } + + /** Unwind to the enclosing statement without pretending the unfinished expression has a value. */ + private static class IncompleteSyntax extends CompilerException { + private IncompleteSyntax(IncompleteStatement statement) { + super("Incomplete expression"); + this.statement = statement; + } + + private final IncompleteStatement statement; + } + + /** Unwind only to the owning call, retaining a written label without inventing a value. */ + private static class IncompleteArgument extends CompilerException { + private IncompleteArgument(Token name) { + super("Incomplete named argument"); + this.name = name; + } + + private final Token name; + } + /** * Parse a declared list of return types. * @@ -5119,15 +5875,19 @@ List parseArgumentList(boolean required, boolean allowCurrying, bool * } */ List parseReturnList() { + return parseReturnList(false); + } + + private List parseReturnList(boolean header) { List listReturn; if (match(Id.VOID) != null) { listReturn = Collections.emptyList(); } else if (match(Id.L_PAREN) == null) { - listReturn = Collections.singletonList(new Parameter(parseTypeExpression())); + listReturn = List.of(new Parameter(parseIntersectingTypeExpression(false, header))); } else { listReturn = new ArrayList<>(); do { - listReturn.add(new Parameter(parseTypeExpression(), matchNameOrAny())); + listReturn.add(new Parameter(parseIntersectingTypeExpression(false, header), matchNameOrAny())); } while (match(Id.R_PAREN, (match(Id.COMMA) == null)) == null); } return listReturn; @@ -5238,19 +5998,70 @@ LiteralExpression parseVersionLiteral(boolean fRequired) { return tokVer == null ? null : new LiteralExpression(tokVer); } + /** + * Finish a block without consuming the synthetic EOF token. Missing braces are errors, but + * completed declarations and statements remain useful to source tools. A speculative parse + * still fails immediately, and cancellation/error budgets always stop recovery. + */ + private boolean atBlockEnd(boolean fFileLevel) { + if (f_errs.get().isAbortDesired()) { + throw new CompilerException("Parsing aborted"); + } + if (eof()) { + // An explicit EOF cursor already reports its unfinished suffix, including containers. + // Other cursors and ordinary parsing must still diagnose missing block braces. + if (!fFileLevel && !f_errs.get().hasError(UNEXPECTED_EOF) + && !(f_cursor == m_source.getPosition() + && f_errs.get().hasError(INCOMPLETE_EXPRESSION))) { + log(Severity.ERROR, UNEXPECTED_EOF, m_source.getPosition(), m_source.getPosition()); + } + // Retain an end marker for recovered containers without advancing past EOF. + m_tokenPrev = new Token(m_source.getPosition(), m_source.getPosition(), Id.R_CURLY); + return true; + } + return match(Id.R_CURLY) != null; + } + + /** + * Drop one malformed statement and resume at a lexical statement boundary. Rewind to its + * beginning so delimiters consumed by the failed parse cannot change the enclosing scope. + * Always consume its first token before scanning, even if that token is a statement keyword. + */ + private void recoverStatement(Mark start, CompilerException error) { + if (m_cSpeculating != 0 || m_fAvoidRecovery || f_errs.get().isAbortDesired()) { + throw error; + } + if (eof()) { + return; + } + restore(start); + Token token = current(); + switch (token.getId()) { + case L_CURLY, L_PAREN, L_SQUARE: + skipEnclosed(token.getId()); + break; + default: + skipToNextStatement(); + break; + } + } + /** * Attempt to get out of whatever parsing mess we got ourselves into by * figuring out where the current statement ends, and starting anew from * there. */ void skipToNextStatement() { - while (true) { + while (!eof()) { switch (peek().getId()) { case SEMICOLON: next(); return; case L_CURLY: + skipEnclosed(current().getId()); + return; + case L_PAREN: case L_SQUARE: skipEnclosed(current().getId()); @@ -5311,7 +6122,7 @@ void skipToNextStatement() { * @param idOpen the opening parenthesis / curlies / brackets */ void skipEnclosed(Id idOpen) { - while (true) { + while (!eof()) { switch (peek().getId()) { case L_CURLY: case L_PAREN: @@ -5466,7 +6277,6 @@ protected static class Mark { Token putBack; Token lastMatch; Token doc; - boolean noRec; } protected Mark mark() { @@ -5476,17 +6286,17 @@ protected Mark mark() { mark.putBack = m_tokenPutBack == null ? null : m_tokenPutBack.clone(); mark.lastMatch = m_tokenPrev == null ? null : m_tokenPrev .clone(); mark.doc = m_doc; - mark.noRec = m_fAvoidRecovery; return mark; } protected void restore(Mark mark) { + // A nesting-limit or listener abort ends this parse, including after speculative rewind. + // Token backtracking must not re-enable recovery from an exhausted parser budget. m_lexer.restore(mark.pos); m_token = mark.token; m_tokenPutBack = mark.putBack; m_tokenPrev = mark.lastMatch; m_doc = mark.doc; - m_fAvoidRecovery = mark.noRec; } /** @@ -5652,19 +6462,19 @@ protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd /** * Count one level of nested parsing, to be released when the returned handle is closed: * - *

+     * 

      * try (var ignore = nest()) {
      *     ...
      * }
-     * 
+ *
* - * The parser is a recursive descent, and a level of nesting in the source costs a dozen or so + *

The parser is a recursive descent, and a level of nesting in the source costs a dozen or so * Java frames. Without a limit, sufficiently nested source exhausts the thread's stack, and a * StackOverflowError is not something a caller can be asked to handle: it escapes the compiler * and reaches whatever embeds it. Counting the levels turns that into an ordinary error with a * source position. * - * The count is shared by every construct that nests, because they nest through each other - + *

The count is shared by every construct that nests, because they nest through each other - * a type inside an expression inside a statement - so one budget bounds the descent no matter * how the source alternates between them. It counts levels of parsing rather than levels of * source: one construct can pass more than one guard on its way down, so the limit is a bound @@ -5676,6 +6486,7 @@ protected void log(Severity severity, String sCode, long lPosStart, long lPosEnd */ private Nesting nest() { if (m_cNesting >= MAX_NESTING_DEPTH) { + m_fAvoidRecovery = true; Token token = peek(); log(Severity.ERROR, NESTING_TOO_DEEP, token.getStartPosition(), token.getEndPosition(), MAX_NESTING_DEPTH); @@ -5943,6 +6754,10 @@ public String toString() { * Semicolon is missing. */ public static final String MISSING_SEMICOLON = "PARSER-29"; + /** + * An explicit partial-analysis cursor marks an unfinished expression. + */ + public static final String INCOMPLETE_EXPRESSION = "PARSER-31"; /** * Nesting is too deep. @@ -5958,6 +6773,9 @@ public String toString() { // ----- data members -------------------------------------------------------------------------- + /** Reserved value that cannot be produced by Source.getPosition(). */ + private static final long NO_CURSOR = -1; + /** * The Source to parse. */ @@ -5973,6 +6791,9 @@ public String toString() { */ private final Nesting f_nesting = new Nesting(); + /** + * The ErrorListener to report errors to. + */ /** * Where this parser's diagnostics go: the listener it was handed, or - for as long as a * scope or an {@link Attempt} is open - somewhere else. @@ -5984,6 +6805,12 @@ public String toString() { */ private final Lexer m_lexer; + /** Whether trailing incomplete expression statements may be retained for partial analysis. */ + private final boolean f_partialAnalysis; + + /** Explicit cursor, or NO_CURSOR for ordinary parsing and trailing-EOF partial analysis. */ + private final long f_cursor; + /** * The "put back" token. */ @@ -6020,6 +6847,9 @@ public String toString() { */ private boolean m_fAvoidRecovery; + /** + * Object supporting unpredictable amount of look-ahead. + */ /** * How many speculative attempts are open. Recovery is for the real parse: inside an attempt a * syntax error is an answer, so there is nothing to recover from. diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index e2254e71c38..2ef0e32f983 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -48,6 +48,7 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; @@ -808,6 +809,11 @@ protected boolean catchUpChildren(ErrorListener errs) { /** Catch up children using the enclosing compilation attempt's collector. */ protected boolean catchUpChildren(ErrorListener errs, InvocationBinding.Collector bindings) { + return catchUpChildren(errs, bindings, CursorBinding.Collector.NONE); + } + + protected boolean catchUpChildren(ErrorListener errs, InvocationBinding.Collector bindings, + CursorBinding.Collector cursors) { // determine what stage we're trying to catch the children up to Stage stageTarget = getStage(); if (!stageTarget.isTargetable()) { @@ -847,7 +853,7 @@ protected boolean catchUpChildren(ErrorListener errs, InvocationBinding.Collecto ErrorListener errsTemp = errs.branch(this); while (stageOldest.compareTo(stageTarget) < 0) { Stage stageNext = stageOldest.nextTarget(); - StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp, bindings); + StageMgr mgrKids = new StageMgr(listChildren, stageNext, errsTemp, bindings, cursors); for (int cTries = 0; !mgrKids.processComplete(); cTries++) { if (errsTemp.isAbortDesired() || cTries > 20) { mgrKids.logDeferredAsErrors(errsTemp); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Context.java b/javatools/src/main/java/org/xvm/compiler/ast/Context.java index cb8aae10eca..38c94ee320b 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Context.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Context.java @@ -36,6 +36,7 @@ import org.xvm.compiler.ast.StatementBlock.TargetInfo; import org.xvm.compiler.Compiler; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; @@ -69,6 +70,12 @@ public InvocationBinding.Collector getInvocationBindings() { return outer == null ? InvocationBinding.Collector.NONE : outer.getInvocationBindings(); } + /** @return the explicit cursor attempt's collector, or the disabled collector */ + public CursorBinding.Collector getCursorBindings() { + Context outer = getOuterContext(); + return outer == null ? CursorBinding.Collector.NONE : outer.getCursorBindings(); + } + /** * @return the outer context */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java b/javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java new file mode 100644 index 00000000000..64c13bf87db --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java @@ -0,0 +1,466 @@ +package org.xvm.compiler.ast; + +import java.util.Arrays; +import java.util.Comparator; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.xvm.asm.Argument; +import org.xvm.asm.ClassStructure; +import org.xvm.asm.ConstantPool; +import org.xvm.asm.Constants.Access; +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure; +import org.xvm.asm.Register; +import org.xvm.asm.TypedefStructure; +import org.xvm.asm.constants.ChildInfo; +import org.xvm.asm.constants.IdentityConstant; +import org.xvm.asm.constants.PropertyConstant; +import org.xvm.asm.constants.TypeConstant; +import org.xvm.asm.constants.TypeParameterConstant; + +import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.CursorBinding; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.partial.IncompleteStatement; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + +/** Capture cursor scope facts and resolve type-name candidates through normal contextual lookup. */ +final class CursorScope { + /** Copy scope facts now; callers must not retain this validation context. */ + static CursorBinding capture(Context ctx) { + // Parameter registers are initialized lazily, including their assignment state. + Stream.iterate(ctx, Objects::nonNull, Context::getOuterContext).forEach(Context::getNameMap); + Set names = new HashSet<>(); + ctx.collectVariables(names); + var variables = names.stream().sorted() + .filter(name -> !ctx.isReservedName(name)) + .map(name -> ctx.getVar(name) instanceof Register register + ? new CursorBinding.Variable(name, register, register.getType(), ctx.isVarReadable(name)) + : null) + .filter(Objects::nonNull) + .toList(); + return new CursorBinding(variables, ctx.getThisType(), !ctx.isFunction()); + } + + static List types(IncompleteStatement site, Context ctx, ErrorListener errs) { + String prefix = site.getCompletionPrefix(); + var probe = ErrorListener.cancellable(silent(PROBE), errs::isAbortDesired); + return names(site).stream().filter(name -> name.startsWith(prefix)).sorted() + .takeWhile(name -> !errs.isAbortDesired()) + .map(name -> { + var token = new Token(site.getEndPosition(), site.getEndPosition(), Id.IDENTIFIER, name); + var target = ctx.resolveName(token, probe); + return namedType(name, target); + }).filter(Objects::nonNull).toList(); + } + + /** Header lookup uses the real enclosing declaration; no method Context is invented. */ + static List declarationTypes(IncompleteStatement site, AstNode scope, + List writtenFormals, ErrorListener errs) { + if (site.getTarget() instanceof NamedTypeExpression type) { + if (writtenFormals.stream().anyMatch(formal -> formal.getName().equals(firstName(type)))) { + if (type.left != null || type.getNames().length < 2) { + return List.of(); + } + var parameters = writtenFormals.stream() + .collect(Collectors.toMap(Parameter::getName, Function.identity(), (first, second) -> first)); + var qualifier = formalConstraint(parameters.get(firstName(type)), scope, parameters, Set.of(), errs); + if (qualifier == null) { + return List.of(); + } + // NameResolver's FORMAL_TYPE mode permits virtual child classes, not typedefs + // or static nested types. An upper bound must not erase that distinction. + var candidates = parameterizedTypes(new NamedTypeExpression( + qualifier, type.names.subList(1, type.names.size()), type.paramTypes, type.getEndPosition()), + scope, site.getCompletionPrefix(), errs); + if (type.names.size() == 2) { + return candidates.stream().filter(candidate -> + candidate.identity().getComponent() instanceof ClassStructure child && child.isVirtualChild()).toList(); + } + var bound = formalBound(parameters.get(firstName(type)), scope, parameters, Set.of(), errs); + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var first = bound == null ? null : bound.ensureTypeInfo(scope.getComponent().getIdentityConstant(), probe) + .getChildInfosByName().get(type.names.get(1).getValueText()); + return first != null && first.getIdentity().getComponent() instanceof ClassStructure child + && child.isVirtualChild() && !probe.hasSeriousErrors() && !probe.isAbortDesired() ? candidates : List.of(); + } + if (type.left != null) { + return parameterizedTypes(type, scope, site.getCompletionPrefix(), errs); + } + if (type.getNames().length > 1) { + return qualifiedTypes(type, scope, site.getCompletionPrefix(), errs); + } + } + String prefix = site.getCompletionPrefix(); + var probe = ErrorListener.cancellable(silent(PROBE), errs::isAbortDesired); + return names(site, writtenFormals).stream().filter(name -> name.startsWith(prefix)).sorted() + .takeWhile(name -> !errs.isAbortDesired()) + .map(name -> namedType(name, new NameResolver(scope, name).forceResolve(probe))) + .filter(Objects::nonNull).toList(); + } + + private static String firstName(NamedTypeExpression type) { + return type.left instanceof NamedTypeExpression left ? firstName(left) : type.getNames()[0]; + } + + /** Resolve bounds on disposable syntax only; no unfinished class/method or formal is registered. */ + static List declarationFormals(IncompleteStatement site, AstNode scope, + List writtenFormals, ErrorListener errs) { + return site.getTarget() instanceof NamedTypeExpression named + && (named.left != null || named.getNames().length > 1) ? List.of() : resolveFormals(site, scope, writtenFormals, errs); + } + + private static List resolveFormals(IncompleteStatement site, AstNode scope, List parameters, + ErrorListener errs) { + var byName = parameters.stream().collect(Collectors.toMap(Parameter::getName, Function.identity(), + (first, second) -> first)); + return parameters.stream().takeWhile(parameter -> !errs.isAbortDesired()).map(parameter -> { + if (parameter.getType() instanceof BadTypeExpression bad && bad.nonType instanceof TypeExpression written + && site.getTarget() instanceof NamedTypeExpression selected + && parameter.getName().startsWith(site.getCompletionPrefix())) { + var proposal = completeBound(written, selected.getNameToken(), parameter.getNameToken()); + // Only the candidate's own guarded cycle may complete an unfinished bound. Keep + // its written text; this probe creates no formal identity or registered signature. + return writtenBound(proposal, scope, byName, Map.of(parameter.getName(), 0), 0, errs) == BoundSyntax.RECURSIVE + ? new CursorBinding.Formal(parameter.getNameToken(), null, written + " (incomplete)") : null; + } + TypeConstant bound = formalBound(parameter, scope, byName, Set.of(), errs); + if (bound != null) { + return new CursorBinding.Formal(parameter.getNameToken(), bound); + } + return writtenBound(parameter.getType(), scope, byName, Map.of(parameter.getName(), 0), 0, errs) + == BoundSyntax.RECURSIVE + ? new CursorBinding.Formal(parameter.getNameToken(), null, parameter.getType().toString()) : null; + }).filter(Objects::nonNull).toList(); + } + + /** Replace just the selected leaf on disposable syntax, retaining every other bound operand. */ + private static TypeExpression completeBound(TypeExpression written, Token selected, Token name) { + if (written instanceof NamedTypeExpression named && named.left == null && named.paramTypes == null + && named.getNames().length == 1 + && named.getNameToken().getStartPosition() == selected.getStartPosition()) { + return new NamedTypeExpression(null, List.of(name), null, null, null, named.getEndPosition()); + } + var copy = (TypeExpression) written.clone(); + StreamSupport.stream(copy.children().spliterator(), false).filter(TypeExpression.class::isInstance) + .map(TypeExpression.class::cast).toList() + .forEach(child -> copy.replaceChild(child, completeBound(child, selected, name))); + return copy; + } + + /** + * Recognize guarded written recursion without assigning it a type. A cycle must cross a real + * class's type-argument boundary; aliases and direct/sibling cycles cannot establish one. + * Unknown or inaccessible names still reject the constraint. Qualified formal members need a + * resolved bound and deliberately do not use this syntax-only fallback. + */ + private static BoundSyntax writtenBound(TypeExpression type, AstNode scope, + Map parameters, Map resolving, int depth, ErrorListener errs) { + if (errs.isAbortDesired()) { + return BoundSyntax.INVALID; + } + // These operators preserve the written recursion guard; none creates a class boundary. + // Resolve every operand so an unknown name or an unguarded sibling cycle still rejects it. + if (type instanceof NullableTypeExpression || type instanceof DecoratedTypeExpression + || type instanceof BiTypeExpression) { + return StreamSupport.stream(type.children().spliterator(), false) + .filter(TypeExpression.class::isInstance).map(TypeExpression.class::cast) + .map(child -> writtenBound(child, scope, parameters, resolving, depth, errs)) + .reduce(BoundSyntax.COMPLETE, BoundSyntax::combine); + } + if (!(type instanceof NamedTypeExpression named) || named.left != null) { + return BoundSyntax.INVALID; + } + String name = named.getNames()[0]; + if (parameters.containsKey(name)) { + if (named.getNames().length != 1 || named.paramTypes != null) { + return BoundSyntax.INVALID; + } + if (resolving.containsKey(name)) { + return depth > resolving.get(name) ? BoundSyntax.RECURSIVE : BoundSyntax.INVALID; + } + var parameter = parameters.get(name); + if (parameter.getType() == null) { + return BoundSyntax.COMPLETE; + } + var active = Stream.concat(resolving.entrySet().stream(), Stream.of(Map.entry(name, depth))) + .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); + return writtenBound(parameter.getType(), scope, parameters, active, depth, errs); + } + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var reference = new NamedTypeExpression(null, named.names, named.access, null, null, named.getEndPosition()); + reference.setParent(scope); + var target = new NameResolver(reference, Arrays.asList(named.getNames()).iterator()).forceResolve(probe); + if (!(target instanceof IdentityConstant identity) || !isTypeIdentity(identity) + || !visible(identity, scope.getComponent().getIdentityConstant()) + || probe.hasSeriousErrors() || probe.isAbortDesired()) { + return BoundSyntax.INVALID; + } + int nested = depth + (identity.getComponent() instanceof ClassStructure ? 1 : 0); + return named.paramTypes == null ? BoundSyntax.COMPLETE : named.paramTypes.stream() + .map(argument -> writtenBound(argument, scope, parameters, resolving, nested, errs)) + .reduce(BoundSyntax.COMPLETE, BoundSyntax::combine); + } + + private enum BoundSyntax { + COMPLETE, RECURSIVE, INVALID; + + BoundSyntax combine(BoundSyntax other) { + return switch (this) { + case INVALID -> INVALID; + case RECURSIVE -> other == INVALID ? INVALID : RECURSIVE; + case COMPLETE -> other; + }; + } + } + + private static TypeConstant formalBound(Parameter parameter, AstNode scope, Map parameters, + Set resolving, ErrorListener errs) { + var copy = formalConstraint(parameter, scope, parameters, resolving, errs); + if (copy == null) { + return null; + } + copy.setParent(scope); + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + TypeConstant bound = resolveType(copy, probe); + return bound != null && visibleNames(copy, scope.getComponent().getIdentityConstant()) ? bound : null; + } + + /** Keep real bound syntax: NameResolver must never interpret a TypeConstant's display string. */ + private static TypeExpression formalConstraint(Parameter parameter, AstNode scope, + Map parameters, Set resolving, ErrorListener errs) { + if (resolving.contains(parameter.getName()) || errs.isAbortDesired()) { + return null; + } + if (parameter.getType() == null) { + return new NamedTypeExpression(new NameExpression(parameter.getNameToken()), scope.pool().typeObject()); + } + var active = Stream.concat(resolving.stream(), Stream.of(parameter.getName())) + .collect(Collectors.toUnmodifiableSet()); + return substituteFormals((TypeExpression) parameter.getType().clone(), scope, parameters, active, errs); + } + + /** Written references to sibling formals resolve through their bounds, never an outer homonym. */ + private static TypeExpression substituteFormals(TypeExpression type, AstNode scope, + Map parameters, Set resolving, ErrorListener errs) { + if (type instanceof BadTypeExpression) { + return null; + } + if (type instanceof NamedTypeExpression named && named.left == null + && parameters.containsKey(named.getNames()[0])) { + TypeExpression replacement = formalConstraint(parameters.get(named.getNames()[0]), scope, parameters, resolving, errs); + if (replacement == null || named.paramTypes != null) { + return null; + } + return named.getNames().length == 1 ? replacement + : new NamedTypeExpression(replacement, named.names.subList(1, named.names.size()), + null, named.getEndPosition()); + } + var children = StreamSupport.stream(type.children().spliterator(), false).toList(); + for (AstNode child : children) { + if (child instanceof TypeExpression childType) { + TypeExpression replacement = substituteFormals(childType, scope, parameters, resolving, errs); + if (replacement == null) { + return null; + } + type.replaceChild(child, replacement); + } + } + return type; + } + + /** Use contextual TypeInfo to enumerate children, then normal type-name resolution to bind them. */ + private static List qualifiedTypes(NamedTypeExpression type, AstNode scope, + String prefix, ErrorListener errs) { + var names = Arrays.asList(type.getNames()); + var qualifier = names.subList(0, names.size() - 1); + var owner = scope.getComponent().getIdentityConstant(); + var lookup = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var resolver = new NameResolver(type, qualifier.iterator()); + var target = resolver.forceResolve(lookup); + if (!(target instanceof IdentityConstant identity) || !isTypeIdentity(identity) + || lookup.hasSeriousErrors() || lookup.isAbortDesired() || !visible(identity, owner)) { + return List.of(); + } + var info = identity.getType().ensureTypeInfo(owner, lookup); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired()) { + return List.of(); + } + var probe = ErrorListener.cancellable(silent(PROBE), errs::isAbortDesired); + return info.getChildInfosByName().values().stream() + .filter(child -> child.getName().startsWith(prefix)) + .filter(child -> info.getType().getAccess().canSee(child.getAccess()) + || child.getIdentity().getClassIdentity().isNestMateOf(owner)) + .sorted(Comparator.comparing(ChildInfo::getName)) + .takeWhile(child -> !errs.isAbortDesired()) + .map(child -> { + String name = child.getName(); + var resolved = new NameResolver(type, Stream.concat(qualifier.stream(), Stream.of(name)).iterator()) + .forceResolve(probe); + return resolved instanceof IdentityConstant id && visible(id, owner) ? namedType(name, id) : null; + }).filter(Objects::nonNull).toList(); + } + + /** Resolve only a disposable copy of the complete written qualifier, including its arguments. */ + private static List parameterizedTypes(NamedTypeExpression type, AstNode scope, + String prefix, ErrorListener errs) { + var left = (TypeExpression) type.left.clone(); + TypeExpression qualifier = type.names.size() == 1 ? left + : new NamedTypeExpression(left, type.names.subList(0, type.names.size() - 1), null, + type.names.get(type.names.size() - 2).getEndPosition()); + qualifier.setParent(scope); + var lookup = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var owner = scope.getComponent().getIdentityConstant(); + TypeConstant resolved = resolveType(qualifier, lookup); + if (resolved == null || !visibleNames(qualifier, owner)) { + return List.of(); + } + var info = resolved.ensureTypeInfo(owner, lookup); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired()) { + return List.of(); + } + return info.getChildInfosByName().values().stream() + .filter(child -> child.getName().startsWith(prefix)) + .filter(child -> info.getType().getAccess().canSee(child.getAccess()) + || child.getIdentity().getClassIdentity().isNestMateOf(owner)) + .sorted(Comparator.comparing(ChildInfo::getName)) + .takeWhile(child -> !errs.isAbortDesired()) + .map(child -> { + Token written = type.getNameToken(); + var name = new Token(written.getStartPosition(), written.getEndPosition(), Id.IDENTIFIER, child.getName()); + var candidate = new NamedTypeExpression((TypeExpression) qualifier.clone(), List.of(name), null, + written.getEndPosition()); + candidate.setParent(scope); + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + TypeConstant result = resolveType(candidate, probe); + var target = candidate.getNameBindings().getLast().target(); + return result != null && target instanceof IdentityConstant identity + && isTypeIdentity(identity) && visible(identity, owner) + ? new CursorBinding.NamedType(child.getName(), identity, result) : null; + }).filter(Objects::nonNull).toList(); + } + + private static TypeConstant resolveType(TypeExpression type, ErrorListener errs) { + if (!new StageMgr(type, Stage.Resolved, errs).fastForward(20) + || errs.hasSeriousErrors() || errs.isAbortDesired()) { + return null; + } + TypeConstant result = type.ensureTypeConstant(null, errs); + return result.containsUnresolved() || errs.hasSeriousErrors() || errs.isAbortDesired() ? null : result; + } + + private static boolean visibleNames(AstNode node, IdentityConstant owner) { + return (!(node instanceof NamedTypeExpression named) + || named.getNameBindings().stream().allMatch(binding -> + binding.target() instanceof IdentityConstant identity && visible(identity, owner) + || binding.target() instanceof TypeConstant && named.alreadyReached(Stage.Validated))) + && StreamSupport.stream(node.children().spliterator(), false).allMatch(child -> visibleNames(child, owner)); + } + + /** Every containing type must be visible, including ancestors hidden by an imported alias. */ + private static boolean visible(IdentityConstant target, IdentityConstant owner) { + return Stream.iterate(target, Objects::nonNull, IdentityConstant::getParentConstant).allMatch(identity -> { + var component = identity.getComponent(); + var parent = identity.getParentConstant(); + return component != null && (component.getAccess() == Access.PUBLIC || identity.isNestMateOf(owner) + || parent != null && parent.getComponent() instanceof ClassStructure enclosing + && enclosing.getFormalType().adjustAccess(owner).getAccess().canSee(component.getAccess())); + }); + } + + private static CursorBinding.NamedType namedType(String name, Argument target) { + if (target instanceof StatementBlock.TargetInfo info) { + target = info.getId(); + } else if (target instanceof Register register && register.getType().isTypeOfType()) { + TypeConstant formal = register.getType().getParamType(0); + target = !formal.containsUnresolved() && formal.isSingleDefiningConstant() + && formal.getDefiningConstant() instanceof TypeParameterConstant parameter + && parameter.getRegister() == register.getIndex() ? parameter : null; + } + return target instanceof IdentityConstant identity && isTypeIdentity(identity) + ? new CursorBinding.NamedType(name, identity, + identity instanceof PropertyConstant property ? property.getFormalType() : identity.getType()) : null; + } + + private static boolean isTypeIdentity(IdentityConstant identity) { + return identity instanceof PropertyConstant property ? property.isFormalType() + : identity instanceof TypeParameterConstant + || identity.getComponent() instanceof ClassStructure || identity.getComponent() instanceof TypedefStructure; + } + + /** Candidate names only; ordinary read validation still proves accessibility and ownership. */ + static Set valueNames(IncompleteStatement site) { + return names(site); + } + + /** Validate disposable proposals while the real lexical context and required type are alive. */ + static List enclosingValues(IncompleteStatement site, Context ctx, TypeConstant required, + ErrorListener errs) { + if (site.isCall() || site.isTypeCompletion() || site.getDeclarationType().isPresent()) { + return List.of(); + } + return PartialCallResolver.instanceSpellings(site).takeWhile(text -> !errs.isAbortDesired()) + .filter(text -> { + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var trial = ctx.enter(); + try { + var value = PartialCallResolver.proposedInstance(site, text).validate(trial, required, probe); + return value != null && value.getTypeFit().isFit() + && !probe.hasSeriousErrors() && !probe.isAbortDesired(); + } finally { + trial.discard(); + } + }).toList(); + } + + /** Lexical spellings only; normal expression validation proves that an outer instance exists. */ + static List enclosingInstances(IncompleteStatement site) { + return Stream.iterate(site.getParent(), Objects::nonNull, AstNode::getParent) + .filter(TypeCompositionStatement.class::isInstance).map(TypeCompositionStatement.class::cast) + .map(TypeCompositionStatement::getNameToken).filter(Objects::nonNull) + .map(Token::getValueText).distinct().toList(); + } + + private static Set names(IncompleteStatement site) { + return names(site, List.of()); + } + + private static Set names(IncompleteStatement site, List writtenFormals) { + Set names = new HashSet<>(ConstantPool.getImplicitImportNames()); + Stream.iterate(site.getParent(), Objects::nonNull, AstNode::getParent).forEach(node -> { + if (node.isComponentNode() && node.getComponent() != null) { + names.addAll(node.getComponent().getChildByNameMap().keySet()); + if (node.getComponent() instanceof MethodStructure method) { + method.getParams().stream().filter(parameter -> parameter.isTypeParameter()) + .map(parameter -> parameter.getName()).forEach(names::add); + } + } + if (node instanceof StatementBlock block) { + if (block.imports != null) { + names.addAll(block.imports.keySet()); + } + if (block.importsWild != null) { + block.importsWild.forEach(statement -> { + if (statement.getNameResolver().getConstant() instanceof IdentityConstant identity + && identity.getComponent() != null) { + names.addAll(identity.getComponent().getChildByNameMap().keySet()); + } + }); + } + } + }); + writtenFormals.stream().map(Parameter::getName).forEach(names::remove); + return names; + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java index 3968d05c307..7b5a27b77ea 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java @@ -300,8 +300,8 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { // passed to the lambda (via FBIND) // - so now, at this point, we have the signature, we have the method structure, and we just // have to emit the code corresponding to the lambda - if (catchUpChildren(errs, mgr.getInvocationBindings())) { - if (!body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings())) { + if (catchUpChildren(errs, mgr.getInvocationBindings(), mgr.getCursorBindings())) { + if (!body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings(), mgr.getCursorBindings())) { mgr.deferChildren(); } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java index fe94f2a62d2..d903b31d319 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java @@ -611,7 +611,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { // method children are all deferred up until this stage, so we have to "catch them up" at // this point, recreating the various compiler stages here MethodStructure method = (MethodStructure) getComponent(); - if (method == null || !catchUpChildren(errs, mgr.getInvocationBindings())) { + if (method == null || !catchUpChildren(errs, mgr.getInvocationBindings(), mgr.getCursorBindings())) { // we are in an error state; we choose not to proceed with compilation mgr.deferChildren(); return; @@ -916,7 +916,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { if (cDefaults > 0) { StatementBlock block = adopt(new StatementBlock(Collections.emptyList())); - RootContext ctxMethod = new RootContext(block, method, mgr.getInvocationBindings()); + RootContext ctxMethod = new RootContext(block, method, mgr.getInvocationBindings(), mgr.getCursorBindings()); Context ctx = ctxMethod.validatingContext(); int cParamExprs = params.size(); @@ -953,7 +953,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { } protected void compileBody(StageMgr mgr, MethodStructure method, ErrorListener errs) { - if (body != null && !body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings())) { + if (body != null && !body.compileMethod(method.createCode(), errs, mgr.getInvocationBindings(), mgr.getCursorBindings())) { // the compilation has failed; no further progress is possible mgr.deferChildren(); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java index a552e6961bb..6063c1115e4 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java @@ -44,6 +44,7 @@ import org.xvm.compiler.Token.Id; import org.xvm.compiler.ast.Context.CaptureContext; +import org.xvm.compiler.ast.partial.PartialSyntax; import org.xvm.util.Severity; @@ -281,11 +282,9 @@ protected TypeFit calcFit(Context ctx, TypeConstant typeIn, TypeConstant typeOut return super.calcFit(ctx, typeIn, typeOut); } - @Override - protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListener errs) { - var bindings = ctx.getInvocationBindings(); - var writtenArgs = bindings.isEnabled() ? List.copyOf(args) : List.of(); - bindings.begin(this); + /** Resolve the written construction type using the same rules for validation and cursor probes. */ + Construction prepareConstruction(Context ctx, TypeConstant typeRequired, ErrorListener errs, + boolean incomplete) { ConstantPool pool = pool(); TypeConstant typeSuper = null; // the super class type of the anon inner class TypeConstant typeTarget = null; // the type to look for a constructor at (could be private) @@ -559,7 +558,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe // we will emit the second constructor in leu of the first one // using the default value for the element type as the second argument int cArgs = args.size(); - if (cArgs == 1) { + if (cArgs == 1 && !incomplete) { // array[capacity] is a fixed size array and is allowed only for // types with default values TypeConstant typeElement = typeTarget.getParamType(0); @@ -600,6 +599,32 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } } + return new Construction(typeResult, typeTarget, typeSuper, plan); + } + + /** Stack-owned preparation result; no new state is retained on a syntax node. */ + record Construction(TypeConstant result, TypeConstant target, TypeConstant superType, Plan plan) { + boolean requiresNewable() { + return plan == Plan.Regular || plan == Plan.Child; + } + } + + @Override + protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListener errs) { + var bindings = ctx.getInvocationBindings(); + var writtenArgs = bindings.isEnabled() ? List.copyOf(args) : List.of(); + bindings.begin(this); + var construction = prepareConstruction(ctx, typeRequired, errs, false); + if (construction == null) { + return null; + } + ConstantPool pool = pool(); + TypeConstant typeResult = construction.result(); + TypeConstant typeTarget = construction.target(); + TypeConstant typeSuper = construction.superType(); + Plan plan = construction.plan(); + boolean fAnonymous = body != null; + ErrorListener errsTemp = errs.branch(this); TypeInfo infoTarget = fAnonymous ? typeTarget.ensureTypeInfo(errsTemp) @@ -612,7 +637,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } // for a regular or virtual child construction, the target type must be new-able - if ((plan == Plan.Regular || plan == Plan.Child) && !infoTarget.isNewable(false, errsTemp)) { + if (construction.requiresNewable() && !infoTarget.isNewable(false, errsTemp)) { String sTarget = infoTarget.getType().removeAccess().getValueString(); infoTarget.reportNotNewable(sTarget, null, false, errsTemp); errsTemp.merge(); @@ -722,6 +747,22 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe } if (fAnonymous) { + if (ctx.getCursorBindings().isEnabled() && PartialSyntax.contains(body)) { + // A body cursor needs the actual source nodes and their enclosing-instance + // context. Capture analysis below validates disposable clones and then drops + // them; publishing those identities would lose the cursor or leak trial facts. + var capture = new AnonInnerClassContext(ctx); + m_ctxCapture = capture; + try { + new StageMgr(anon, Stage.Emitted, errs, bindings, ctx.getCursorBindings()).fastForward(20); + } finally { + capture.exit(); + m_ctxCapture = null; + } + // The cursor is never a valid value, and must not start capture rewriting or + // produce a constructor binding/emittable construction. + return null; + } // at this point, we need to create a temporary copy of the anonymous inner class for // the purpose of determining which local variables from this context will be "captured" // by the code in the anonymous inner class; to determine the captures, we need to go @@ -1172,7 +1213,7 @@ private void ensureInnerClass(Context ctx, AnonPurpose purpose, ErrorListener er m_ctxCapture = new AnonInnerClassContext(ctx); - catchUpChildren(errs, ctx.getInvocationBindings()); + catchUpChildren(errs, ctx.getInvocationBindings(), ctx.getCursorBindings()); if (purpose != AnonPurpose.CaptureAnalysis) { // the context is ONLY retained to provide capture information diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java b/javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java new file mode 100644 index 00000000000..aa18d5e1d04 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java @@ -0,0 +1,49 @@ +package org.xvm.compiler.ast; + +import java.util.List; +import java.util.stream.IntStream; +import java.util.stream.StreamSupport; + +import org.xvm.compiler.ast.partial.IncompleteExpression; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.partial.PartialSyntax.ArgumentCursor; + +/** Trial argument construction; lexical parenting stays inside the ordinary compiler package. */ +final class PartialArgument { + private PartialArgument() {} + + /** Candidate enumeration uses the compiler's ordinary unbound-parameter marker, never a type. */ + static List unbound(IncompleteStatement call, ArgumentCursor argument) { + var written = call.getArguments().get(argument.index()); + Expression hole = new NonBindingExpression(written.getStartPosition(), written.getEndPosition(), null); + if (written instanceof LabeledExpression label) { + hole = new LabeledExpression(label.getNameToken(), hole); + } + return replace(call, argument.index(), hole); + } + + static List proposed(IncompleteStatement call, ArgumentCursor argument, Expression value) { + return replace(call, argument.index(), substitute(call.getArguments().get(argument.index()), value)); + } + + private static List replace(IncompleteStatement call, int index, Expression value) { + value.setParent(call); + value.introduceParentage(); + var arguments = call.getArguments(); + return IntStream.range(0, arguments.size()) + .mapToObj(i -> i == index ? value : arguments.get(i)).toList(); + } + + private static Expression substitute(Expression written, Expression value) { + if (written instanceof IncompleteExpression) { + return value; + } + var copy = written.copyTree(); + var children = StreamSupport.stream(copy.childNodes().spliterator(), false).toList(); + children.stream().filter(Expression.class::isInstance).map(Expression.class::cast) + .filter(child -> PartialSyntax.valueCursor(child).isPresent()) + .forEach(child -> copy.replaceChild(child, substitute(child, value))); + return copy; + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java new file mode 100644 index 00000000000..117f0edd17a --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java @@ -0,0 +1,452 @@ +package org.xvm.compiler.ast; + +import java.math.BigDecimal; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.Function; +import java.util.function.Predicate; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; + +import org.xvm.asm.ClassStructure; +import org.xvm.asm.Constants.Access; +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure; +import org.xvm.asm.constants.ClassConstant; +import org.xvm.asm.constants.IdentityConstant; +import org.xvm.asm.constants.MethodConstant; +import org.xvm.asm.constants.MultiMethodConstant; +import org.xvm.asm.constants.PropertyConstant; +import org.xvm.asm.constants.SingletonConstant; +import org.xvm.asm.constants.TypeConstant; +import org.xvm.asm.constants.TypeInfo; +import org.xvm.asm.constants.TypeInfo.MethodKind; +import org.xvm.asm.constants.TypedefConstant; + +import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.CursorBinding; +import org.xvm.compiler.InvocationBinding; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.StatementBlock.TargetInfo; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.partial.PartialSyntax.ArgumentCursor; +import org.xvm.compiler.ast.partial.ProposedLiteralToken; + +import org.xvm.util.PackedInteger; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + +/** Explicit incomplete-call inspection on the compiler worker, while the lexical context exists. */ +final class PartialCallResolver { + static CursorBinding inspect(IncompleteStatement site, Context ctx, TypeConstant required, ErrorListener errs) { + if (site.getTarget() instanceof NewExpression creation) { + return PartialConstructionResolver.inspect(site, creation, ctx, required, errs); + } + var scope = CursorScope.capture(ctx).withCandidates(List.of()); + if (errs.isAbortDesired()) { + return scope; + } + var probe = ErrorListener.cancellable(silent(PROBE), errs::isAbortDesired); + Target target = switch (site.getTarget()) { + case NameExpression callee -> target(callee, ctx, probe); + default -> null; + }; + if (target == null || errs.isAbortDesired()) { + return functionScope(site, ctx, scope, errs); + } + var direct = methodScope(site, ctx, scope, target, errs); + if (!direct.candidates().isEmpty() || !direct.functions().isEmpty() || target.kind() != MethodKind.Method) { + return direct; + } + // Match InvocationExpression's fallback order: a Type's functions, then + // receiver-rewritten functions X.f(receiver, args) for ordinary instances. + var alternate = target.type().isTypeOfType() + ? new Target(target.type().getParamType(0).resolveConstraints(), MethodKind.Function, false) + : new Target(target.type(), MethodKind.Function, true); + return methodScope(site, ctx, scope, alternate, errs); + } + + private static CursorBinding methodScope(IncompleteStatement site, Context ctx, CursorBinding scope, + Target target, ErrorListener errs) { + String methodName = ((NameExpression) site.getTarget()).getName(); + var lookup = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var info = target.type().ensureTypeInfo(ctx.getThisClassId(), lookup); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired()) { + return scope; + } + // InvocationExpression gives a property precedence over methods of the same name. + if (!target.receiverArgument() && info.findProperty(methodName) != null) { + return functionScope(site, ctx, scope, errs); + } + var methods = info.findMethods(methodName, -1, target.kind()).stream() + .filter(method -> method.isTopLevel() && !info.getMethodById(method).isCtorOrValidator()) + .filter(method -> info.getType().getAccess() == Access.PRIVATE + || info.getMethodById(method).isVisible(ctx.getThisClassId())) + .toList(); + var probe = ErrorListener.cancellable(silent(PROBE), errs::isAbortDesired); + Function, List> fit = arguments -> { + var supplied = target.receiverArgument() + ? Stream.concat(Stream.of(site.getReceiver().orElseThrow()), arguments.stream()).toList() : arguments; + return methods.stream().takeWhile(method -> !errs.isAbortDesired()) + .flatMap(method -> probeCallCandidate(site, ctx, target.type(), info, method, + supplied, probe).stream()) + .map(candidate -> target.receiverArgument() ? candidate.withReceiverArgument() : candidate) + .toList(); + }; + var candidates = fit.apply(writtenArguments(site)).stream() + .filter(candidate -> acceptsLabel(site, candidate)).toList(); + var result = scope.withCandidates(candidates); + return candidates.isEmpty() ? result : argumentValues(site, ctx, result, errs, + arguments -> fit.apply(arguments).stream().anyMatch(candidate -> + validateArguments(ctx, candidate, target.receiverArgument() + ? Stream.concat(Stream.of(site.getReceiver().orElseThrow()), arguments.stream()).toList() + : arguments, errs))); + } + + /** + * Test one incomplete-call candidate with the compiler's usual named-argument, conversion and + * generic inference rules. Cloned arguments and a child context isolate speculative changes. + * Missing parameters are permitted; no best-overload selection or invocation validation occurs. + */ + static List probeCallCandidate(AstNode site, Context ctx, TypeConstant target, TypeInfo info, + MethodConstant method, List arguments, ErrorListener errs) { + var written = arguments.stream().map(argument -> (Expression) argument.clone()).toList(); + var named = site.collectNamedArgs(written, errs); + if (named == null || errs.isAbortDesired()) { + return List.of(); + } + Set direct = new HashSet<>(); + Set converting = new HashSet<>(); + List result = new ArrayList<>(); + site.collectMatchingMethods(ctx.enter(), target, info, Set.of(method), written, false, named, + null, direct, converting, new HashMap<>(), errs, (signature, ordered) -> { + var resolved = signature.resolveGenericTypes(site.pool(), target); + var original = info.getMethodById(method).getSignature(); + // A pending method formal is not a concrete expected type. Retain its written + // formal instead of leaking PendingTypeConstant or substituting Object. + var params = IntStream.range(0, resolved.getParamCount()) + .mapToObj(i -> resolved.getRawParams()[i].containsUnresolved() + ? original.getRawParams()[i] : resolved.getRawParams()[i]) + .toArray(TypeConstant[]::new); + var returns = IntStream.range(0, resolved.getReturnCount()) + .mapToObj(i -> resolved.getRawReturns()[i].containsUnresolved() + ? original.getRawReturns()[i] : resolved.getRawReturns()[i]) + .toArray(TypeConstant[]::new); + var copied = site.pool().ensureSignatureConstant(resolved.getName(), params, returns); + var declaration = info.getMethodById(method).getTopmostMethodStructure(info).getIdentityConstant(); + InvocationBinding.arguments(written, ordered).ifPresent(mapping -> + result.add(new CursorBinding.Candidate(declaration, copied, mapping, converting.contains(method)))); + }); + return List.copyOf(result); + } + + /** testFit can use an operator's optimistic implicit type; validate each actual insertion too. */ + static boolean validateArguments(Context ctx, CursorBinding.Candidate candidate, + List arguments, ErrorListener errs) { + var trial = ctx.enter(); + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + int formals = ((MethodStructure) candidate.method().getComponent()).getTypeParamCount(); + return arguments.stream().allMatch(written -> { + var mapping = candidate.arguments().stream().filter(argument -> + argument.startPosition() == written.getStartPosition() + && argument.endPosition() == written.getEndPosition()).findFirst(); + if (mapping.isEmpty() || probe.isAbortDesired()) { + return false; + } + var expected = candidate.signature().getRawParams()[formals + mapping.orElseThrow().parameterIndex()]; + Expression value = written instanceof LabeledExpression label ? label.getUnderlyingExpression() : written; + Expression validated = ((Expression) value.clone()).validate(trial, expected, probe); + return validated != null && validated.getTypeFit().isFit() + && !probe.hasSeriousErrors() && !probe.isAbortDesired(); + }); + } + + private static CursorBinding functionScope(IncompleteStatement site, Context ctx, + CursorBinding scope, ErrorListener errs) { + var functions = function(site, ctx, writtenArguments(site), errs); + var result = scope.withFunctions(functions); + return functions.isEmpty() ? result : argumentValues(site, ctx, result, errs, + arguments -> !function(site, ctx, arguments, errs).isEmpty()); + } + + /** + * Probe proposed source names in the ordinary argument fitter. Trials have lexical parentage + * for resolution, but are never installed in the source tree or selected as complete calls. + * This preserves generic inference and conversions without copying type rules into the host. + */ + static CursorBinding argumentValues(IncompleteStatement site, Context ctx, + CursorBinding scope, ErrorListener errs, Predicate> fits) { + var written = site.getArguments(); + var nested = PartialSyntax.argument(site); + var cursor = nested.map(ArgumentCursor::cursor).orElse(site); + boolean qualified = !cursor.isCall() && cursor.getReceiver().isPresent(); + if (nested.isEmpty() && site.getPendingArgumentName().isEmpty() + && (written.size() != site.getSeparators().size() + || written.stream().anyMatch(LabeledExpression.class::isInstance))) { + return scope; + } + String prefix = cursor.getCompletionPrefix(); + Predicate fitsValue = value -> { + if (nested.isPresent()) { + return fits.test(PartialArgument.proposed(site, nested.orElseThrow(), value)); + } + Expression argument = site.getPendingArgumentName() + .map(label -> new LabeledExpression(label, value)).orElse(value); + argument.setParent(site); + argument.introduceParentage(); + var arguments = new ArrayList<>(written); + arguments.add(argument); + return fits.test(arguments); + }; + Predicate fitsName = name -> fitsValue.test(proposedName(cursor, name)); + var variables = scope.variables().stream().filter(variable -> !qualified && variable.readable()) + .filter(variable -> variable.name().startsWith(prefix)) + .takeWhile(variable -> !errs.isAbortDesired()) + .filter(variable -> fitsName.test(variable.name())).toList(); + var literals = qualified ? List.of() + : List.of("False", "True", "Null", "0", "0.0", "\"\"", "' '", "#00", "[]", "Map:[]", "Tuple:()").stream() + .filter(text -> text.startsWith(prefix)) + .takeWhile(text -> !errs.isAbortDesired()) + .filter(text -> { + Expression proposal = proposedLiteral(cursor, text, errs); + return proposal != null && fitsValue.test(proposal); + }).toList(); + var expressions = instanceSpellings(cursor).takeWhile(text -> !errs.isAbortDesired()) + .filter(text -> fitsValue.test(proposedInstance(cursor, text))).toList(); + var templates = qualified || !prefix.isEmpty() ? List.of() + : Stream.concat(scope.candidates().stream().flatMap(candidate -> Arrays.stream(candidate.signature().getRawParams())), + scope.functions().stream().flatMap(function -> Arrays.stream(site.pool().extractFunctionParams(function.type())))) + .map(TypeConstant::resolveTypedefs).map(TypeConstant::removeNullable) + .filter(TypeConstant::isFunction) + .map(site.pool()::extractFunctionParams).filter(Objects::nonNull) + .map(parameters -> parameters.length).distinct().sorted() + .takeWhile(arity -> !errs.isAbortDesired()) + .filter(arity -> fitsValue.test(proposedLambda(cursor, arity))) + .map(arity -> "(" + IntStream.range(0, arity).mapToObj(index -> "arg" + (index + 1)) + .collect(Collectors.joining(", ")) + ") -> TODO()") + .toList(); + var result = scope.withArgumentValues(variables).withArgumentLiterals(literals) + .withArgumentExpressions(expressions).withArgumentTemplates(templates); + if (errs.isAbortDesired()) { + return result; + } + var lookup = ErrorListener.cancellable(ErrorListener.collecting(errs::log), errs::isAbortDesired); + var receiver = qualified ? ((Expression) cursor.getReceiver().orElseThrow().clone()) + .validate(ctx.enter(), null, lookup) : null; + if (qualified && (receiver == null || !receiver.getTypeFit().isFit())) { + return result; + } + var info = (qualified ? receiver.getType() : scope.thisType()).ensureTypeInfo(ctx.getThisClassId(), lookup); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired()) { + return result; + } + var propertyNames = info.ensurePropertiesByName().values().stream() + .filter(property -> property.getName().startsWith(prefix)) + .filter(property -> qualified || scope.instance() || property.isConstant()) + .filter(property -> qualified || scope.variables().stream().noneMatch(variable -> variable.name().equals(property.getName()))) + .filter(property -> info.getType().getAccess() == Access.PRIVATE || property.isVisible(ctx.getThisClassId())) + .map(property -> property.getName()); + var properties = Stream.concat(propertyNames, qualified ? Stream.empty() : CursorScope.valueNames(cursor).stream()) + .filter(name -> name.startsWith(prefix)) + .filter(name -> qualified || scope.variables().stream().noneMatch(variable -> variable.name().equals(name))) + .distinct().sorted().takeWhile(name -> !errs.isAbortDesired()) + .map(name -> propertyValue(cursor, ctx, name, errs)) + .filter(Objects::nonNull) + .filter(property -> fitsName.test(property.name())) + .toList(); + return result.withArgumentProperties(properties); + } + + /** A real inferred-parameter lambda on disposable syntax; the whole-call fitter proves its arity. */ + private static Expression proposedLambda(IncompleteStatement site, int arity) { + long cursor = site.getEndPosition(); + var names = IntStream.range(0, arity).mapToObj(index -> + new NameExpression(new Token(cursor, cursor, Id.IDENTIFIER, "arg" + (index + 1)))) + .collect(Collectors.toCollection(ArrayList::new)); + var body = new ThrowExpression(new Token(cursor, cursor, Id.TODO), null, null, + new Token(cursor, cursor, Id.R_PAREN)); + var returns = new ReturnStatement(new Token(cursor, cursor, Id.RETURN), body); + var lambda = new LambdaExpression(names, new Token(cursor, cursor, Id.LAMBDA), + new StatementBlock(new ArrayList<>(List.of(returns)), cursor, cursor), cursor); + lambda.setParent(site); + lambda.introduceParentage(); + return lambda; + } + + private static Expression proposedLiteral(IncompleteStatement site, String text, ErrorListener errs) { + long cursor = site.getEndPosition(); + return switch (text) { + case "0" -> new LiteralExpression(new ProposedLiteralToken(cursor, Id.LIT_INT, PackedInteger.ZERO, text)); + case "0.0" -> new LiteralExpression(new ProposedLiteralToken(cursor, Id.LIT_DEC, BigDecimal.ZERO, text)); + case "\"\"" -> new LiteralExpression(new ProposedLiteralToken(cursor, Id.LIT_STRING, "", text)); + case "' '" -> new LiteralExpression(new ProposedLiteralToken(cursor, Id.LIT_CHAR, ' ', text)); + case "#00" -> new LiteralExpression(new ProposedLiteralToken(cursor, Id.LIT_BINSTR, new byte[]{0}, text)); + case "[]" -> new ListExpression(null, new ArrayList<>(), cursor, cursor); + case "Map:[]" -> { + var map = new MapExpression(new NamedTypeExpression(null, + List.of(new Token(cursor, cursor, Id.IDENTIFIER, "Map")), null, null, null, cursor), + new ArrayList<>(), new ArrayList<>(), cursor); + map.setParent(site); + map.introduceParentage(); + var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + yield new StageMgr(map, Stage.Resolved, probe).fastForward(20) + && !probe.hasSeriousErrors() && !probe.isAbortDesired() ? map : null; + } + case "Tuple:()" -> new TupleExpression(null, new ArrayList<>(), cursor, cursor); + default -> proposedName(site, text); + }; + } + + /** Lexical candidates only; each caller must prove these through ordinary validation. */ + static Stream instanceSpellings(IncompleteStatement site) { + boolean qualified = !site.isCall() && site.getReceiver().isPresent(); + boolean explicitThis = qualified && site.getReceiver().orElseThrow() instanceof NameExpression receiver + && receiver.getLeftExpression() == null && receiver.getName().equals("this"); + return qualified && !explicitThis ? Stream.empty() + : Stream.concat(qualified ? Stream.empty() : Stream.of("this"), + CursorScope.enclosingInstances(site).stream().map(name -> qualified ? name : "this." + name)) + .filter(text -> text.startsWith(site.getCompletionPrefix())); + } + + static Expression proposedInstance(IncompleteStatement site, String text) { + if (!text.startsWith("this.")) { + return proposedName(site, text); + } + long cursor = site.getEndPosition(); + var expression = new NameExpression(new NameExpression(new Token(cursor, cursor, Id.IDENTIFIER, "this")), + null, new Token(cursor, cursor, Id.IDENTIFIER, text.substring("this.".length())), null, cursor); + expression.setParent(site); + expression.introduceParentage(); + return expression; + } + + /** Normal read validation supplies identity, narrowing and receiver-specific type substitution. */ + private static CursorBinding.Property propertyValue(IncompleteStatement site, Context ctx, + String name, ErrorListener errs) { + var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var expression = proposedName(site, name); + var value = expression.validate(ctx.enter(), null, validation); + return value != null && value.getTypeFit().isFit() + && !validation.hasSeriousErrors() && !validation.isAbortDesired() + && expression.getResolvedTarget() instanceof PropertyConstant identity + ? new CursorBinding.Property(name, identity, value.getType()) : null; + } + + private static NameExpression proposedName(IncompleteStatement site, String name) { + long cursor = site.getEndPosition(); + var token = new Token(cursor, cursor, Id.IDENTIFIER, name); + var expression = !site.isCall() && site.getReceiver().isPresent() + ? new NameExpression((Expression) site.getReceiver().orElseThrow().clone(), null, token, null, cursor) + : new NameExpression(token); + expression.setParent(site); + expression.introduceParentage(); + return expression; + } + + static boolean acceptsLabel(IncompleteStatement site, CursorBinding.Candidate candidate) { + return site.getPendingArgumentName().map(name -> { + var method = (MethodStructure) candidate.method().getComponent(); + var parameter = method.getParam(name.getValueText()); + if (parameter == null || parameter.isTypeParameter()) { + return false; + } + int index = parameter.getIndex() - method.getTypeParamCount(); + return candidate.arguments().stream().noneMatch(argument -> argument.parameterIndex() == index); + }).orElse(true); + } + + /** Validate trial copies of the callee and arguments without fabricating a complete call. */ + private static List function( + IncompleteStatement site, Context ctx, List written, ErrorListener errs) { + if (errs.isAbortDesired() || site.getPendingArgumentName().isPresent() + || written.stream().anyMatch(argument -> argument instanceof LabeledExpression + || argument instanceof NonBindingExpression && PartialSyntax.argument(site).isEmpty())) { + return List.of(); + } + var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var trial = ctx.enter(); + var callee = ((Expression) site.getTarget().clone()).validate(trial, null, validation); + if (callee == null || !callee.getTypeFit().isFit() || validation.hasSeriousErrors() || validation.isAbortDesired()) { + return List.of(); + } + var type = callee.getType().resolveTypedefs(); + if (!type.isFunction()) { + return List.of(); + } + var parameters = site.pool().extractFunctionParams(type); + if (parameters == null || parameters.length < written.size()) { + return List.of(); + } + var arguments = written.stream().map(argument -> (Expression) argument.clone()) + .collect(Collectors.toCollection(ArrayList::new)); + boolean fit = IntStream.range(0, arguments.size()).allMatch(index -> { + Expression argument = arguments.get(index); + if (argument instanceof NonBindingExpression && PartialSyntax.argument(site).isPresent()) { + return true; + } + var value = argument.validate(trial, parameters[index], validation); + return value != null && value.isSingle() && value.getTypeFit().isFit() + && !validation.hasSeriousErrors() && !validation.isAbortDesired(); + }); + if (!fit) { + return List.of(); + } + var mapping = IntStream.range(0, written.size()).mapToObj(index -> { + var argument = written.get(index); + return new InvocationBinding.Argument(argument.getStartPosition(), argument.getEndPosition(), index); + }).toList(); + return List.of(new CursorBinding.FunctionCandidate(type, mapping)); + } + + /** Preserve later arguments while letting an unknown slot remain unbound during enumeration. */ + static List writtenArguments(IncompleteStatement site) { + return PartialSyntax.argument(site).map(argument -> PartialArgument.unbound(site, argument)).orElseGet(site::getArguments); + } + + private static Target target(NameExpression callee, Context ctx, ErrorListener errs) { + Expression receiver = callee.getLeftExpression(); + if (receiver == null) { + var resolved = ctx.resolveName(callee.getNameToken(), errs); + return switch (resolved) { + case TargetInfo target when target.getId() instanceof MultiMethodConstant -> + new Target(target.getTargetType(), target.hasThis() ? MethodKind.Any : MethodKind.Function); + case MultiMethodConstant methods -> new Target(methods.getParentConstant().getType(), MethodKind.Function); + case null, default -> null; + }; + } + if (!receiver.isValidated() || !receiver.getTypeFit().isFit()) { + return null; + } + if (receiver instanceof NameExpression name) { + switch (name.getResolvedTarget()) { + case ClassConstant identity: + return new Target(identity.getType(), MethodKind.Function); + case TypedefConstant alias: + return new Target(alias.getReferredToType(), MethodKind.Function); + case SingletonConstant singleton: + return new Target(singleton.getClassConstant().getType(), MethodKind.Any); + case IdentityConstant identity when identity.getComponent() instanceof ClassStructure structure: + return new Target(identity.getType(), structure.isSingleton() ? MethodKind.Any : MethodKind.Function); + case null, default: + break; + } + } + return new Target(receiver.getType(), MethodKind.Method); + } + + private record Target(TypeConstant type, MethodKind kind, boolean receiverArgument) { + private Target(TypeConstant type, MethodKind kind) { + this(type, kind, false); + } + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java new file mode 100644 index 00000000000..a40a3da035f --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java @@ -0,0 +1,139 @@ +package org.xvm.compiler.ast; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import org.xvm.asm.ClassStructure; +import org.xvm.asm.Component.Format; +import org.xvm.asm.Constants.Access; +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure; +import org.xvm.asm.constants.MethodConstant; +import org.xvm.asm.constants.TypeConstant; +import org.xvm.asm.constants.TypeInfo; +import org.xvm.asm.constants.TypeInfo.MethodKind; + +import org.xvm.compiler.CursorBinding; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.partial.IncompleteStatement; + +import static org.xvm.asm.ErrorListener.Silence.PROBE; +import static org.xvm.asm.ErrorListener.silent; + +/** Constructor cursor queries reuse normal type preparation and argument fitting. */ +final class PartialConstructionResolver { + static CursorBinding inspect(IncompleteStatement site, NewExpression creation, Context ctx, + TypeConstant required, ErrorListener errs) { + var scope = CursorScope.capture(ctx).withCandidates(List.of()); + if (errs.isAbortDesired()) { + return scope; + } + var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + // An anonymous declaration owns its class shell in this analysis attempt. Preparing a + // detached clone would attach orphan components to the enclosing source method instead. + // Stop before forwarding-constructor synthesis, capture validation or emission. + boolean anonymous = creation.body != null; + var trial = anonymous ? creation : (NewExpression) creation.clone(); + var construction = trial.prepareConstruction(ctx.enter(), required, validation, true); + if (construction == null || validation.hasSeriousErrors() || validation.isAbortDesired()) { + return scope; + } + var lookup = ErrorListener.cancellable(ErrorListener.collecting(errs::log), errs::isAbortDesired); + var info = anonymous ? construction.target().ensureTypeInfo(lookup) + : trial.getTypeInfo(ctx, construction.target(), lookup); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired() + || construction.requiresNewable() && !info.isNewable(false, validation)) { + return scope; + } + // Keep both declaration and superclass possibilities while arguments are incomplete. + // Normal validation chooses a constructor and creates a forwarding wrapper only later. + var targets = anonymous ? List.of(info, construction.superType().ensureTypeInfo(lookup)) + : List.of(info); + if (lookup.hasSeriousErrors() || lookup.isAbortDesired()) { + return scope; + } + var methods = targets.stream().flatMap(target -> target.findMethods("construct", -1, + MethodKind.Constructor).stream() + // Brackets select the size slot of the real fixed-size array constructor. + // Other Array overloads (copy, mutability, capacity) are not dimension syntax. + .filter(method -> site.getOperator().getId() != Id.L_SQUARE + || trial.type instanceof ArrayTypeExpression array + && target.getMethodById(method).getTopmostMethodStructure(target) + .getIdentityConstant().equals(array.getSupplyConstructor())) + .filter(method -> anonymous || target.getType().getAccess() == Access.PRIVATE + || target.getMethodById(method).isVisible(ctx.getThisClassId())) + // A class shell's generated default is only a placeholder for superclass + // forwarding. Use real superclass signatures; interfaces instead need that + // default because they declare no superclass constructor to forward to. + .filter(method -> !anonymous || target != info || targets.getLast().getFormat() == Format.INTERFACE + || !target.getMethodById(method).getTopmostMethodStructure(target).isSynthetic()) + .map(method -> new Constructor(target, method))).toList(); + var written = arguments(site, PartialCallResolver.writtenArguments(site)); + var candidates = methods.stream() + .takeWhile(method -> !errs.isAbortDesired()) + .flatMap(method -> PartialCallResolver.probeCallCandidate(site, ctx, method.info().getType(), method.info(), + method.method(), written, validation).stream() + .map(candidate -> anonymous ? candidate : infer(trial, ctx, construction.result(), candidate, written, errs))) + .filter(Objects::nonNull) + .filter(candidate -> site.getLeadingArguments().isEmpty() + || candidate.signature().getParamCount() > site.getLeadingArguments().size()) + .filter(candidate -> !anonymous || site.getArgumentPrefix().isEmpty() + || candidate.signature().getParamCount() > written.size()) + .filter(candidate -> PartialCallResolver.acceptsLabel(site, candidate)) + .toList(); + var result = scope.withCandidates(candidates); + return candidates.isEmpty() ? result : PartialCallResolver.argumentValues(site, ctx, result, errs, + values -> methods.stream().takeWhile(method -> !errs.isAbortDesired()) + .flatMap(method -> PartialCallResolver.probeCallCandidate(site, ctx, method.info().getType(), method.info(), + method.method(), arguments(site, values), validation).stream()) + // Ordinary construction validates against the prepared constructor, then + // infers the result type. That later inference is not a new argument constraint. + .anyMatch(candidate -> PartialCallResolver.validateArguments(ctx, candidate, arguments(site, values), errs))); + } + + private record Constructor(TypeInfo info, MethodConstant method) {} + + private static List arguments(IncompleteStatement site, List values) { + return Stream.concat(site.getLeadingArguments().stream(), values.stream()).toList(); + } + + /** Apply the ordinary constructor's post-argument class inference without selecting an overload. */ + private static CursorBinding.Candidate infer(NewExpression creation, Context ctx, TypeConstant result, + CursorBinding.Candidate candidate, List written, ErrorListener errs) { + var method = (MethodStructure) candidate.method().getComponent(); + var owner = (ClassStructure) method.getParent().getParent(); + if (result.isParamsSpecified() || !owner.isParameterized()) { + return candidate; + } + var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); + var trial = ctx.enter(); + List ordered = written.stream().map(value -> (Expression) value.clone()) + .collect(Collectors.toCollection(ArrayList::new)); + if (creation.containsNamedArgs(ordered)) { + ordered = creation.rearrangeNamedArgs(method, ordered, validation); + if (ordered == null) { + return null; + } + } + for (int index = 0; index < ordered.size() && !validation.isAbortDesired(); ++index) { + var value = ordered.get(index); + if (!(value instanceof NonBindingExpression)) { + var validated = value.validate(trial, candidate.signature().getRawParams()[index], validation); + if (validated == null || !validated.getTypeFit().isFit()) { + return null; + } + ordered.set(index, validated); + } + } + if (validation.hasSeriousErrors() || validation.isAbortDesired()) { + return null; + } + var inferred = creation.inferTypeFromConstructor(trial, owner, method, ordered); + return inferred == null ? candidate : new CursorBinding.Candidate(candidate.method(), + method.getIdentityConstant().getSignature().resolveGenericTypes(creation.pool(), inferred), + candidate.arguments(), candidate.converting()); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java b/javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java new file mode 100644 index 00000000000..dc64ec8be9b --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java @@ -0,0 +1,77 @@ +package org.xvm.compiler.ast; + +import java.util.List; +import java.util.Optional; + +import org.xvm.asm.ClassStructure; +import org.xvm.asm.ErrorListener; +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.CursorBinding; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; + +/** + * Compiler-internal semantic operations for retained incomplete syntax. Public only to connect + * the partial syntax package to ordinary validation and inference; embedding hosts query their + * compilation results instead. Contexts and bindings belong to the current compilation attempt. + * This service retains no state and never turns an incomplete operation into an emittable value. + */ +public final class PartialQueries { + private PartialQueries() {} + + /** Inspect intact children in their real context, without selecting the incomplete operation. */ + public static void inspect(IncompleteStatement site, Context ctx, TypeConstant required, + ErrorListener errs) { + var bindings = ctx.getCursorBindings(); + bindings.begin(site); + if (bindings.isEnabled() && !errs.isAbortDesired()) { + var scope = CursorScope.capture(ctx) + .withEnclosingExpressions(CursorScope.enclosingValues(site, ctx, required, errs)); + bindings.record(site, site.isNameCompletion() + ? scope.withTypes(CursorScope.types(site, ctx, errs)) : scope); + } + site.getReceiver().ifPresent(receiver -> { + Expression validated = receiver.validate(ctx, null, errs); + if (validated != null) { + AstNode owner = site.isCall() ? site.getTarget() : site; + owner.replaceChild(receiver, validated); + } + }); + boolean partialTarget = site.isCall() && PartialSyntax.contains(site.getTarget()); + if (partialTarget && !errs.isAbortDesired()) { + // A member prefix before existing arguments owns the original receiver. Inspect it + // in place; a trial callee clone cannot supply bindings for the retained source site. + site.getTarget().validate(ctx, null, errs); + } + CursorBinding call = bindings.isEnabled() && site.isCall() && !partialTarget && !errs.isAbortDesired() + ? PartialCallResolver.inspect(site, ctx, required, errs) : null; + if (call != null) { + bindings.record(site, call); + } + site.getArguments().stream().takeWhile(argument -> !errs.isAbortDesired()).forEach(argument -> { + // No single expected type is selected. Candidate fitting inspects trial copies above. + Expression value = argument instanceof LabeledExpression labeled + ? labeled.getUnderlyingExpression() : argument; + if (!(value instanceof NonBindingExpression || value instanceof LambdaExpression)) { + Expression validated = argument.validate(ctx, null, errs); + if (validated != null) { + site.replaceChild(argument, validated); + } + } + }); + if (call != null && !errs.isAbortDesired()) { + PartialSyntax.argument(site).ifPresent(argument -> bindings.record(argument.cursor(), call)); + } + } + + /** Resolve an unfinished header in its explicit lexical scope, without registering formals. */ + public static Optional declarationBinding(IncompleteStatement site, AstNode scope, + List writtenFormals, ErrorListener errs) { + return scope.getComponent() instanceof ClassStructure owner + ? Optional.of(new CursorBinding(List.of(), owner.getFormalType(), false) + .withTypes(CursorScope.declarationTypes(site, scope, writtenFormals, errs)) + .withFormals(CursorScope.declarationFormals(site, scope, writtenFormals, errs))) + : Optional.empty(); + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java index 4ac08f3b43b..b18bbc159f4 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java @@ -42,6 +42,7 @@ import org.xvm.compiler.InitializerBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Token; +import org.xvm.compiler.ast.partial.PartialSyntax; import org.xvm.util.ListMap; import org.xvm.util.Severity; @@ -418,6 +419,15 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { // clear the "has initial value" setting prop.setInitialValue(null); } else { + if (mgr.getCursorBindings().isEnabled() && PartialSyntax.contains(value)) { + // A cursor hole cannot become a constant. Validate the source-owned + // initializer so its facts survive; disposable clones are never published. + initializer = createAstNodeFor(createInitializer()); + value = null; + new StageMgr(initializer, Stage.Emitted, errs, + mgr.getInvocationBindings(), mgr.getCursorBindings()).fastForward(10); + return; + } // create a clone of ourselves PropertyDeclarationStatement stmtClone = (PropertyDeclarationStatement) clone(); @@ -522,7 +532,7 @@ public void validateContent(StageMgr mgr, ErrorListener errs) { value = null; // "catch up" the newly created initializer to our stage - if (!new StageMgr(initializer, Stage.Validated, errs, mgr.getInvocationBindings()).fastForward(10)) { + if (!new StageMgr(initializer, Stage.Validated, errs, mgr.getInvocationBindings(), mgr.getCursorBindings()).fastForward(10)) { // basically an assertion log(errs, Severity.FATAL, Compiler.FATAL_ERROR, initializer); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java index e5a2692d5f5..b3508d468c9 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java @@ -18,6 +18,7 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; import org.xvm.compiler.CompilerException; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.ast.AstNode.ChildIterator; @@ -43,6 +44,11 @@ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs) { /** Progress a node while preserving the compilation attempt's call-fact collector. */ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings) { + this(node, stageTarget, errs, bindings, CursorBinding.Collector.NONE); + } + + public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, + InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { assert node != null; assert stageTarget != null && stageTarget.isTargetable(); @@ -50,6 +56,7 @@ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, m_target = stageTarget; f_errs = requireNonNull(errs, "errs"); f_bindings = requireNonNull(bindings, "bindings"); + f_cursors = requireNonNull(cursors, "cursors"); } /** @@ -67,6 +74,11 @@ public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener er /** Progress child nodes using the same collector as their enclosing compilation. */ public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings) { + this(list, stageTarget, errs, bindings, CursorBinding.Collector.NONE); + } + + public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, + InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { assert list != null && !list.isEmpty(); assert stageTarget != null && stageTarget.isTargetable(); @@ -74,6 +86,7 @@ public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener er m_target = stageTarget; f_errs = requireNonNull(errs, "errs"); f_bindings = requireNonNull(bindings, "bindings"); + f_cursors = requireNonNull(cursors, "cursors"); } /** @@ -182,6 +195,11 @@ public ErrorListener getErrorListener() { return f_errs; } + /** @return the collector owned by this compilation attempt */ + public CursorBinding.Collector getCursorBindings() { + return f_cursors; + } + public InvocationBinding.Collector getInvocationBindings() { return f_bindings; } @@ -466,6 +484,7 @@ private List takeRevisitList() { private final ErrorListener f_errs; private final InvocationBinding.Collector f_bindings; + private final CursorBinding.Collector f_cursors; /** * The current node being processed if processing is occurring. diff --git a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java index b9156d99c5f..6675df1efd5 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/Statement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/Statement.java @@ -5,6 +5,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.function.Supplier; import org.xvm.asm.Argument; import org.xvm.asm.Assignment; @@ -129,6 +130,11 @@ protected Label ensureShortCircuitLabel(AstNode nodeOrigin, Context ctxOrigin) { * @return the resulting statement (typically this) or null if the compilation cannot proceed */ protected final Statement validate(Context ctx, ErrorListener errs) { + return validate(ctx, errs, () -> validateImpl(ctx, errs)); + } + + /** Preserve the statement validation scope while supplying an explicit value-context hint. */ + protected final Statement validate(Context ctx, ErrorListener errs, Supplier validation) { if (errs.isAbortDesired()) { return null; } @@ -139,7 +145,7 @@ protected final Statement validate(Context ctx, ErrorListener errs) { Statement stmt; m_ctx = ctx; try { - stmt = validateImpl(ctx, errs); + stmt = validation.get(); } finally { m_ctx = ctxPrevious; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java index 818b3560592..e6e3e5cc1a3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java @@ -49,6 +49,7 @@ import org.xvm.asm.op.Var_IN; import org.xvm.compiler.Compiler; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; @@ -305,7 +306,12 @@ public boolean compileMethod(Code code, ErrorListener errs) { /** Compile a method with the collector owned by its enclosing compilation attempt. */ public boolean compileMethod(Code code, ErrorListener errs, InvocationBinding.Collector bindings) { - return compileMethod(new RootContext(this, code.getMethodStructure(), bindings), code, errs); + return compileMethod(code, errs, bindings, CursorBinding.Collector.NONE); + } + + public boolean compileMethod(Code code, ErrorListener errs, InvocationBinding.Collector bindings, + CursorBinding.Collector cursors) { + return compileMethod(new RootContext(this, code.getMethodStructure(), bindings, cursors), code, errs); } /** @@ -561,10 +567,21 @@ public RootContext(StatementBlock stmt, MethodStructure method) { public RootContext(StatementBlock stmt, MethodStructure method, InvocationBinding.Collector bindings) { + this(stmt, method, bindings, CursorBinding.Collector.NONE); + } + + public RootContext(StatementBlock stmt, MethodStructure method, + InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { super(null, false); f_stmt = stmt; f_method = method; f_bindings = Objects.requireNonNull(bindings, "bindings"); + f_cursors = Objects.requireNonNull(cursors, "cursors"); + } + + @Override + public CursorBinding.Collector getCursorBindings() { + return f_cursors; } @Override @@ -1440,6 +1457,7 @@ private Map ensureCaptureVars() { private final StatementBlock f_stmt; private final MethodStructure f_method; private final InvocationBinding.Collector f_bindings; + private final CursorBinding.Collector f_cursors; private Context m_ctxValidating; private boolean m_fEmitting; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java index ea3c55d7d20..3044b6e9172 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java @@ -64,6 +64,7 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; @@ -2401,7 +2402,7 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { break ValidateShorthand; } - RootContext ctxConstruct = createConstructorContext(constructor, mgr.getInvocationBindings()); + RootContext ctxConstruct = createConstructorContext(constructor, mgr.getInvocationBindings(), mgr.getCursorBindings()); Context ctx = ctxConstruct.validatingContext(); if (constructorParams != null && !constructorParams.isEmpty()) { @@ -2532,12 +2533,12 @@ public void generateCode(StageMgr mgr, ErrorListener errs) { * A simple helper to create a new context for shorthand constructor processing. */ private RootContext createConstructorContext(MethodStructure constructor, - InvocationBinding.Collector bindings) { + InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { StatementBlock blockBody = body; if (body == null) { blockBody = adopt(new StatementBlock(Collections.emptyList())); } - return new RootContext(blockBody, constructor, bindings); + return new RootContext(blockBody, constructor, bindings, cursors); } /** diff --git a/javatools/src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java index e08c7f58962..88b37293623 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java @@ -81,9 +81,10 @@ public boolean hasRefAnnotations() { /** * @return the Register for this VariableDeclarationStatement, if any has been created by this - * point in the compilation process + * point in the compilation process. Narrowed uses can be matched to this declaration + * using {@link Register#getOriginalRegister()} and object identity. */ - Register getRegister() { + public Register getRegister() { return m_reg; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java new file mode 100644 index 00000000000..c9524be23ac --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java @@ -0,0 +1,136 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import java.util.List; +import java.util.Optional; + +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure.Code; + +import org.xvm.compiler.Parser; +import org.xvm.compiler.Token; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Parameter; +import org.xvm.compiler.ast.PartialQueries; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.Statement; + +import static org.xvm.asm.ErrorListener.in; + +/** + * Written declaration syntax whose unfinished header cannot register a compiler component. + * A selected type prefix participates in partial analysis; missing names retain only written + * type syntax. The body and unfinished parameters have no fabricated signatures, registers or + * validation contexts. + */ +public final class IncompleteDeclarationStatement extends Statement { + public enum Kind { METHOD, PROPERTY } + + public IncompleteDeclarationStatement(Kind kind, Token name, long start, long end, List cursors) { + this(kind, name, start, end, cursors, List.of()); + } + + public IncompleteDeclarationStatement(Kind kind, Token name, long start, long end, List cursors, List formals) { + this.kind = kind; + this.name = name; + this.start = start; + this.end = end; + this.cursors = List.copyOf(cursors); + this.formals = List.copyOf(formals); + } + + public Kind getKind() { + return kind; + } + + /** A written name only; an absent declaration name does not acquire a placeholder. */ + public Optional getNameToken() { + return Optional.ofNullable(name); + } + + @Override + public long getStartPosition() { + return start; + } + + @Override + public long getEndPosition() { + return end; + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + @Override + @SuppressWarnings("MethodDoesntCallSuperMethod") // Preserve the covariant compatibility entry point. + public IncompleteDeclarationStatement clone() { + return copyTree(); + } + + @Override + public IncompleteDeclarationStatement copyTree() { + var copy = new IncompleteDeclarationStatement(kind, name, start, end, + cursors.stream().map(IncompleteStatement::copyTree).toList(), + formals.stream().map(Parameter::copyTree).toList()); + copy.adopt(copy.cursors); + copy.adopt(copy.formals); + return copyTreeMetadataTo(copy); + } + + @Override + public void resolveNames(StageMgr mgr, ErrorListener errs) { + // A prefix is a query, not an unresolved name in an otherwise valid declaration. + mgr.deferChildren(); + } + + @Override + public void validateContent(StageMgr mgr, ErrorListener errs) { + mgr.deferChildren(); + var bindings = mgr.getCursorBindings(); + cursors.forEach(site -> { + bindings.begin(site); + if (site.isTypeCompletion() && bindings.isEnabled() && !errs.isAbortDesired()) { + PartialQueries.declarationBinding(site, site, formals, errs) + .ifPresent(binding -> bindings.record(site, binding)); + } + errs.error(Parser.INCOMPLETE_EXPRESSION, in(getSource(), site.getEndPosition(), site.getEndPosition())); + }); + if (cursors.isEmpty()) { + errs.error(Parser.INCOMPLETE_EXPRESSION, in(getSource(), end, end)); + } + } + + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + return null; + } + + @Override + protected boolean emit(Context ctx, boolean reachable, Code code, ErrorListener errs) { + throw new IllegalStateException("An incomplete declaration cannot emit code"); + } + + @Override + public void generateCode(StageMgr mgr, ErrorListener errs) { + throw new IllegalStateException("An incomplete declaration cannot emit code"); + } + + @Override + public String toString() { + return kind + getNameToken().map(token -> " " + token.getValueText()).orElse("") + " "; + } + + // Only real syntax children participate in adoption and tree copying. + private final List cursors; + + private final Kind kind; + private final Token name; + private final long start; + private final long end; + private final List formals; + + private static final Field[] CHILD_FIELDS = fieldsForNames(IncompleteDeclarationStatement.class, "cursors", "formals"); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java new file mode 100644 index 00000000000..d9bcbfc77a0 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java @@ -0,0 +1,93 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import org.xvm.asm.ErrorListener; + +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; + +/** + * An incomplete operation in a value position. Retaining the enclosing assignment or return lets + * its normal validation establish the real inference and name-resolution context. This node has + * no implicit type, never fits a requested type, and always fails validation after inspecting the + * intact prefix. It cannot become a value or reach code emission. + * + *

The existing partial site is an ordinary AST child, so adoption and cloning preserve its + * receiver/argument ownership without a second semantic cache or clone-reset protocol. + */ +public final class IncompleteExpression extends Expression { + public IncompleteExpression(IncompleteStatement site) { + this.site = site; + } + + /** The retained source operation; exposes syntax ownership without a validation result. */ + public IncompleteStatement getSite() { + return site; + } + + @Override + public long getStartPosition() { + return site.getStartPosition(); + } + + @Override + public long getEndPosition() { + return site.getEndPosition(); + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + @Override + public IncompleteExpression copyTree() { + var copy = new IncompleteExpression(site.copyTree()); + copy.adopt(copy.site); + return copyTreeMetadataTo(copy); + } + + @Override + public TypeConstant getImplicitType(Context ctx) { + return null; + } + + @Override + public TypeConstant[] getImplicitTypes(Context ctx) { + return TypeConstant.NO_TYPES; + } + + @Override + public TypeFit testFit(Context ctx, TypeConstant required, boolean exhaustive, ErrorListener errs) { + return TypeFit.NoFit; + } + + @Override + public TypeFit testFitMulti(Context ctx, TypeConstant[] required, boolean exhaustive, ErrorListener errs) { + return TypeFit.NoFit; + } + + @Override + protected Expression validate(Context ctx, TypeConstant required, ErrorListener errs) { + site.validate(ctx, required, errs); + return null; + } + + @Override + protected Expression validateMulti(Context ctx, TypeConstant[] required, ErrorListener errs) { + site.validate(ctx, required != null && required.length == 1 ? required[0] : null, errs); + return null; + } + + @Override + public String toString() { + return site.toString(); + } + + private final IncompleteStatement site; + + private static final Field[] CHILD_FIELDS = fieldsForNames(IncompleteExpression.class, "site"); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java new file mode 100644 index 00000000000..77f4f95e4ce --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java @@ -0,0 +1,87 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure.Code; +import org.xvm.compiler.Parser; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.Statement; + +import static org.xvm.asm.ErrorListener.in; + +/** A missing local name and its written initializer; no register or inferred type is invented. */ +public final class IncompleteLocalDeclaration extends Statement { + public IncompleteLocalDeclaration(IncompleteStatement cursor, Expression initializer, long end) { + this.cursor = cursor; + this.initializer = initializer; + this.end = end; + } + + public Expression getInitializer() { + return initializer; + } + + @Override + public String toString() { + return cursor + " = " + initializer; + } + + @Override + public long getStartPosition() { + return cursor.getStartPosition(); + } + + @Override + public long getEndPosition() { + return end; + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + @Override + @SuppressWarnings("MethodDoesntCallSuperMethod") // Constructor copy preserves final children. + public IncompleteLocalDeclaration clone() { + return copyTree(); + } + + @Override + public IncompleteLocalDeclaration copyTree() { + var copy = new IncompleteLocalDeclaration(cursor.copyTree(), initializer.copyTree(), end); + copy.adopt(copy.cursor); + copy.adopt(copy.initializer); + return copyTreeMetadataTo(copy); + } + + @Override + public void resolveNames(StageMgr mgr, ErrorListener errs) { + mgr.deferChildren(); + } + + @Override + public void validateContent(StageMgr mgr, ErrorListener errs) { + mgr.deferChildren(); + errs.error(Parser.INCOMPLETE_EXPRESSION, in(getSource(), cursor.getEndPosition(), cursor.getEndPosition())); + } + + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + return null; + } + + @Override + protected boolean emit(Context ctx, boolean reachable, Code code, ErrorListener errs) { + throw new IllegalStateException("An incomplete local declaration cannot emit code"); + } + + private final IncompleteStatement cursor; + private final Expression initializer; + private final long end; + + private static final Field[] CHILD_FIELDS = fieldsForNames(IncompleteLocalDeclaration.class, "cursor", "initializer"); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java new file mode 100644 index 00000000000..5a0c7b80d0a --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java @@ -0,0 +1,271 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure.Code; +import org.xvm.asm.constants.TypeConstant; + +import org.xvm.compiler.Parser; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NamedTypeExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.PartialQueries; +import org.xvm.compiler.ast.Statement; +import org.xvm.compiler.ast.TypeExpression; + +import static org.xvm.asm.ErrorListener.in; + +/** + * A name, member access or call whose intact prefix can be validated in its original scope. It can + * stand alone or be owned by an {@link IncompleteExpression} in a value position. The marker has + * no fabricated value or type. Validation always fails after inspecting the receiver and ordinary + * arguments, preventing method emission. Only the explicit partial-analysis parser creates it. + * An explicit cursor can select a written member token or a call before its closing parenthesis; + * the selected operation need not be malformed. The original member token is retained as syntax. + * + *

Child fields participate in normal AST adoption/cloning; no Context, callback, or separate + * semantic cache survives validation. Consumers copy facts only from children whose validation + * succeeded (isValidated and a fitting TypeFit), never a failed child's placeholder type. + */ +public final class IncompleteStatement extends Statement { + public IncompleteStatement(Expression target, Token operator, List arguments, List separators, long endPosition) { + this(target, operator, arguments, separators, endPosition, Parser.UNEXPECTED_EOF); + } + + public IncompleteStatement(Expression target, Token operator, List arguments, List separators, long endPosition, String diagnosticCode) { + this(target, operator, arguments, separators, endPosition, diagnosticCode, null); + } + + /** A written member token selected at its end by an explicit cursor probe. */ + public IncompleteStatement(Expression receiver, Token dot, Token memberName, long cursor, String diagnosticCode) { + this(receiver, dot, List.of(), List.of(), cursor, diagnosticCode, memberName); + } + + /** An unqualified name prefix at an explicit cursor. */ + public IncompleteStatement(Token name, long cursor, String diagnosticCode) { + this(new NameExpression(name), name, List.of(), List.of(), cursor, diagnosticCode, name); + } + + /** A declaration type prefix; retain its qualifiers as syntax and replace only the final token. */ + public static IncompleteStatement forDeclarationType(NamedTypeExpression type, long cursor) { + Token name = type.getNameToken(); + return new IncompleteStatement(type, name, List.of(), List.of(), cursor, Parser.INCOMPLETE_EXPRESSION, name); + } + + /** A missing declaration name after a complete written type; never fabricate a name token. */ + @SuppressWarnings("unused") + public static IncompleteStatement forDeclarationName(NamedTypeExpression type, long cursor) { + return forDeclarationName(type, type.getNameToken(), cursor); + } + + /** Retain a complete wrapped or compound type and its last written token, without a new name. */ + public static IncompleteStatement forDeclarationName(TypeExpression type, Token last, long cursor) { + return new IncompleteStatement(type, last, List.of(), List.of(), cursor, Parser.INCOMPLETE_EXPRESSION, null); + } + + /** Complete written type at an empty declaration-name slot; syntax only, never a binding. */ + public Optional getDeclarationType() { + return target instanceof TypeExpression type && cursorName == null + && !isCall() && operator.getId() != Id.DOT + && operator.getEndPosition() < endPosition && type.getEndPosition() < endPosition + ? Optional.of(type) : Optional.empty(); + } + + /** The written type's final name, for syntax suggestions only, not a resolved type or binding. */ + public Optional getDeclarationNameType() { + return getDeclarationType().filter(NamedTypeExpression.class::isInstance) + .map(NamedTypeExpression.class::cast).map(NamedTypeExpression::getNameToken); + } + + /** A call whose last written name is a named argument awaiting its value. */ + public static IncompleteStatement forNamedArgument(Expression callee, Token open, + List arguments, List separators, long cursor, Token name) { + return new IncompleteStatement(callee, open, arguments, separators, cursor, Parser.INCOMPLETE_EXPRESSION, name); + } + + /** A direct argument name prefix; label is null for a positional argument. */ + public static IncompleteStatement forArgumentPrefix(Expression callee, Token open, + List arguments, List separators, long cursor, Token label, Token prefix) { + return new IncompleteStatement(callee, open, arguments, separators, cursor, + Parser.INCOMPLETE_EXPRESSION, label, prefix); + } + + private IncompleteStatement(Expression target, Token operator, List arguments, + List separators, long endPosition, String diagnosticCode, + Token cursorName) { + this(target, operator, arguments, separators, endPosition, diagnosticCode, cursorName, null); + } + + private IncompleteStatement(Expression target, Token operator, List arguments, + List separators, long endPosition, String diagnosticCode, + Token cursorName, Token argumentPrefix) { + this.target = target; + this.operator = operator; + this.arguments = new ArrayList<>(arguments); + this.separators = List.copyOf(separators); + this.endPosition = endPosition; + this.diagnosticCode = diagnosticCode; + this.cursorName = cursorName; + this.argumentPrefix = argumentPrefix; + } + + /** The written receiver, callee or declaration type; a call is never overload-resolved. */ + public Expression getTarget() { + return target; + } + + /** The dot or opening call/dimension delimiter, at its original source position. */ + public Token getOperator() { + return operator; + } + + /** The original typed member token, if present; its text and range are syntax, not a binding. */ + public Optional getMemberName() { + return isCall() ? Optional.empty() : Optional.ofNullable(cursorName); + } + + /** Typed text before the cursor; the original token still supplies the whole replacement range. */ + public String getCompletionPrefix() { + return getArgumentPrefix().or(this::getMemberName).map(name -> + endPosition < name.getEndPosition() + ? getSource().toString(name.getStartPosition(), endPosition) + : name.getValueText()).orElse(""); + } + + /** The named argument at the cursor, whose value is absent or retained only as a name prefix. */ + public Optional getPendingArgumentName() { + return isCall() ? Optional.ofNullable(cursorName) : Optional.empty(); + } + + /** Original token to replace when completing an argument; it is never validated as a value. */ + public Optional getArgumentPrefix() { + return Optional.ofNullable(argumentPrefix); + } + + /** The real containing call for a direct, labeled or parenthesized argument cursor. */ + public Optional getArgumentCall() { + return PartialSyntax.argumentCall(this); + } + + /** Complete written arguments; excludes the missing value or cursor-selected argument prefix. */ + public List getArguments() { + return List.copyOf(arguments); + } + + /** Written array dimensions preceding this call's opening parenthesis. */ + public List getLeadingArguments() { + return target instanceof NewExpression creation ? creation.getArguments() : List.of(); + } + + /** Top-level commas only; nested calls and strings do not contribute separators. */ + public List getSeparators() { + return separators; + } + + public boolean isCall() { + return operator.getId() == Id.L_PAREN || operator.getId() == Id.ASYNC_PAREN + || operator.getId() == Id.L_SQUARE && target instanceof NewExpression; + } + + public boolean isNameCompletion() { + return operator.getId() == Id.IDENTIFIER && getDeclarationType().isEmpty(); + } + + /** A type query in an unfinished declaration, with no value or parameter-name completion. */ + public boolean isTypeCompletion() { + return getDeclarationType().isEmpty() + && (getParent() instanceof IncompleteDeclarationStatement + || getParent() instanceof IncompleteTypeCompositionStatement); + } + + /** Explicit receiver only; an unqualified call does not invent an implicit receiver. */ + public Optional getReceiver() { + if (isNameCompletion() || getDeclarationType().isPresent()) { + return Optional.empty(); + } + return isCall() + ? target instanceof NameExpression name + ? Optional.ofNullable(name.getLeftExpression()) + : Optional.empty() + : Optional.of(target); + } + + @Override + public long getStartPosition() { + return target.getStartPosition(); + } + + @Override + public long getEndPosition() { + return endPosition; + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + @Override + public IncompleteStatement copyTree() { + var copy = new IncompleteStatement(target.copyTree(), operator, + arguments.stream().map(Expression::copyTree).toList(), + separators, endPosition, diagnosticCode, cursorName, argumentPrefix); + copy.adopt(copy.target); + copy.adopt(copy.arguments); + return copyTreeMetadataTo(copy); + } + + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + return inspect(ctx, null, errs); + } + + /** Forward the enclosing expression's required type without storing it on the syntax node. */ + @SuppressWarnings("UnusedReturnValue") + Statement validate(Context ctx, TypeConstant required, ErrorListener errs) { + return validate(ctx, errs, () -> inspect(ctx, required, errs)); + } + + private Statement inspect(Context ctx, TypeConstant required, ErrorListener errs) { + PartialQueries.inspect(this, ctx, required, errs); + errs.error(diagnosticCode, in(getSource(), endPosition, endPosition)); + return null; + } + + @Override + protected boolean emit(Context ctx, boolean reachable, Code code, ErrorListener errs) { + throw new IllegalStateException("An incomplete statement cannot emit code"); + } + + @Override + public String toString() { + if (getDeclarationType().isPresent()) { + return target + " "; + } + String syntax = isNameCompletion() ? getMemberName().map(Token::getValueText).orElse("") + : target + (isCall() ? (operator.getId() == Id.L_SQUARE ? "[" : "(") + arguments + : "." + getMemberName().map(Token::getValueText).orElse("")); + return syntax + " "; + } + + private final Expression target; + private final List arguments; + + private final Token operator; + private final List separators; + private final long endPosition; + private final String diagnosticCode; + private final Token cursorName; + private final Token argumentPrefix; + + private static final Field[] CHILD_FIELDS = fieldsForNames(IncompleteStatement.class, "target", "arguments"); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java new file mode 100644 index 00000000000..0d8614b543d --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java @@ -0,0 +1,143 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import java.util.List; + +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure.Code; + +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Parameter; +import org.xvm.compiler.ast.PartialQueries; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.Statement; +import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.TypeCompositionStatement; + +import static org.xvm.asm.ErrorListener.in; + +/** + * A written type whose unfinished header cannot register inheritance or body facts. + * Only a root module registers its written namespace/core import, providing the query scope. + * Its real body remains syntax for structural consumers, including member-file assembly. + * Only the selected header type participates in partial analysis, in the enclosing scope. + */ +public final class IncompleteTypeCompositionStatement extends TypeCompositionStatement { + + public IncompleteTypeCompositionStatement(Source source, Token category, Token name, long start, long end, List cursors) { + this(source, category, name, null, null, start, end, cursors, List.of()); + } + + public IncompleteTypeCompositionStatement(Source source, Token category, Token name, List qualified, StatementBlock body, long start, long end, List cursors, List formals) { + super(source, category, name, start, end); + this.cursors = List.copyOf(cursors); + this.formals = List.copyOf(formals); + this.qualified = qualified == null ? null : List.copyOf(qualified); + this.body = body; + } + + public IncompleteTypeCompositionStatement(Source source, Token category, Token name, StatementBlock body, long start, long end, List cursors) { + this(source, category, name, start, end, cursors); + this.body = body; + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + @Override + @SuppressWarnings("MethodDoesntCallSuperMethod") // Preserve the covariant compatibility entry point. + public IncompleteTypeCompositionStatement clone() { + return copyTree(); + } + + /** + * Recovery syntax is never rewritten by compiler validation. Construct a fresh node instead + * of using the general AST clone, which replaces child fields and mutable child lists. + */ + @Override + public IncompleteTypeCompositionStatement copyTree() { + var copy = new IncompleteTypeCompositionStatement(source, category, name, qualified, null, + getStartPosition(), getEndPosition(), + cursors.stream().map(IncompleteStatement::copyTree).toList(), + formals.stream().map(Parameter::copyTree).toList()); + copy.body = body == null ? null : copy.adopt(body.copyTree()); + copy.adopt(copy.cursors); + copy.adopt(copy.formals); + return copyTreeMetadataTo(copy); + } + + @Override + public boolean isComponentNode() { + return category.getId() == Id.MODULE; + } + + @Override + public void registerStructures(StageMgr mgr, ErrorListener errs) { + // Defer before the base class explicitly visits children, not just before returning. + mgr.deferChildren(); + // A root's written module name supplies the actual compilation namespace and core + // import. Its incomplete compositions and body still register no declarations. + if (category.getId() == Id.MODULE) { + super.registerStructures(mgr, errs); + } + } + + @Override + public void resolveNames(StageMgr mgr, ErrorListener errs) { + mgr.deferChildren(); + if (category.getId() == Id.MODULE) { + super.resolveNames(mgr, errs); + } + } + + @Override + public void validateContent(StageMgr mgr, ErrorListener errs) { + mgr.deferChildren(); + var bindings = mgr.getCursorBindings(); + cursors.forEach(site -> { + bindings.begin(site); + if (site.isTypeCompletion() && bindings.isEnabled() && !errs.isAbortDesired()) { + PartialQueries.declarationBinding(site, isComponentNode() ? this : getParent(), formals, errs) + .ifPresent(binding -> bindings.record(site, binding)); + } + errs.error(Parser.INCOMPLETE_EXPRESSION, in(getSource(), site.getEndPosition(), site.getEndPosition())); + }); + if (cursors.isEmpty()) { + errs.error(Parser.INCOMPLETE_EXPRESSION, in(getSource(), getEndPosition(), getEndPosition())); + } + } + + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + return null; + } + + @Override + protected boolean emit(Context ctx, boolean reachable, Code code, ErrorListener errs) { + throw new IllegalStateException("An incomplete type declaration cannot emit code"); + } + + @Override + public void generateCode(StageMgr mgr, ErrorListener errs) { + throw new IllegalStateException("An incomplete type declaration cannot emit code"); + } + + @Override + public String toString() { + return toSignatureString() + " "; + } + + // Immutable syntax children; copyTree constructs and adopts fresh lists. + private final List cursors; + + private final List formals; + + private static final Field[] CHILD_FIELDS = fieldsForNames(IncompleteTypeCompositionStatement.class, "body", "cursors", "formals"); +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java new file mode 100644 index 00000000000..94edc80090b --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java @@ -0,0 +1,94 @@ +package org.xvm.compiler.ast.partial; + +import java.util.Optional; +import java.util.function.Predicate; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.InvocationExpression; +import org.xvm.compiler.ast.LambdaExpression; +import org.xvm.compiler.ast.NewExpression; + +/** + * Read-only queries over retained partial syntax, shared by parsing and compiler validation. + * These operations neither resolve names nor attach trial children. Results refer to the caller's + * current tree; they are not detached semantic snapshots and must not outlive that tree's ownership. + */ +public final class PartialSyntax { + private PartialSyntax() { + // empty + } + + /** Include deferred anonymous source bodies without changing compiler child ownership. */ + public static Stream children(AstNode node) { + var children = StreamSupport.stream(node.childNodes().spliterator(), false); + return node instanceof NewExpression creation ? Stream.concat(children, creation.getUnregisteredBody().stream()) : children; + } + + /** A value cursor and its written argument slot; no inferred type or candidate is retained. */ + public record ArgumentCursor(IncompleteStatement cursor, int index) {} + + /** Whether this node or any descendant is retained incomplete syntax; null has no syntax. */ + public static boolean contains(AstNode node) { + return contains(node, site -> true); + } + + /** Whether a retained site ends at exactly this source cursor, including beneath nested calls. */ + public static boolean containsAt(AstNode node, long cursor) { + return contains(node, site -> site.getEndPosition() == cursor); + } + + private static boolean contains(AstNode node, Predicate matches) { + return node != null && (node instanceof IncompleteStatement site && matches.test(site) + || children(node).anyMatch(child -> contains(child, matches))); + } + + /** Find a value cursor in a written argument, excluding array-dimension slots. */ + public static Optional argument(IncompleteStatement call) { + // Multiple array dimensions are not positional arguments to an ordinary constructor. + if (call.getOperator().getId() == Id.L_SQUARE) { + return Optional.empty(); + } + var arguments = call.getArguments(); + return IntStream.range(0, arguments.size()).mapToObj(index -> + valueCursor(arguments.get(index)) + .map(site -> new ArgumentCursor(site, index))) + .flatMap(Optional::stream).findFirst(); + } + + /** + * Find the single value cursor inside labels, groups or compound expressions. A nested call, + * construction or lambda owns its own arguments/scope and is never searched through here. + */ + public static Optional valueCursor(Expression expression) { + if (expression instanceof IncompleteExpression hole) { + return hole.getSite().isCall() ? Optional.empty() : Optional.of(hole.getSite()); + } + if (isArgumentBoundary(expression)) { + return Optional.empty(); + } + var sites = StreamSupport.stream(expression.childNodes().spliterator(), false) + .filter(Expression.class::isInstance).map(Expression.class::cast) + .flatMap(child -> valueCursor(child).stream()).toList(); + return sites.size() == 1 ? Optional.of(sites.getFirst()) : Optional.empty(); + } + + /** Find the real call containing a direct, labeled, grouped or compound argument cursor. */ + public static Optional argumentCall(IncompleteStatement site) { + AstNode parent = site.getParent(); + while (parent instanceof Expression expression && !isArgumentBoundary(expression)) { + parent = parent.getParent(); + } + return parent instanceof IncompleteStatement call && call.isCall() + && argument(call).map(argument -> argument.cursor() == site).orElse(false) + ? Optional.of(call) : Optional.empty(); + } + + private static boolean isArgumentBoundary(Expression expression) { + return expression instanceof LambdaExpression || expression instanceof InvocationExpression || expression instanceof NewExpression; + } +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java new file mode 100644 index 00000000000..d6eb8c6a227 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java @@ -0,0 +1,23 @@ +package org.xvm.compiler.ast.partial; + +import org.xvm.compiler.Source; +import org.xvm.compiler.Token; + +/** + * A disposable argument proposal has an insertion position but no characters in the original + * source. Literal conversion still needs its exact spelling (not the empty cursor range). + * These tokens are never installed in the source tree or published as source bindings. + */ +public final class ProposedLiteralToken extends Token { + private final String spelling; + + public ProposedLiteralToken(long cursor, Id id, Object value, String spelling) { + super(cursor, cursor, id, value); + this.spelling = spelling; + } + + @Override + public String getString(Source source) { + return spelling; + } +} diff --git a/javatools/src/main/java/org/xvm/tool/Compiler.java b/javatools/src/main/java/org/xvm/tool/Compiler.java index ede945be168..a2999338e10 100644 --- a/javatools/src/main/java/org/xvm/tool/Compiler.java +++ b/javatools/src/main/java/org/xvm/tool/Compiler.java @@ -299,14 +299,7 @@ protected int process() { * @return 0 for success, non-zero for failure */ protected int compile(List compilers, ModuleRepository repoLib) { - linkModules(compilers, repoLib); - flushAndCheckErrors(compilers, "module linking"); - - resolveNames(compilers); - flushAndCheckErrors(compilers, "name resolution"); - - injectNativeTurtle(repoLib); - checkErrors("native turtle injection"); + resolveDeclarations(compilers, repoLib); log(INFO, "Validating expressions"); validateExpressions(compilers); @@ -319,6 +312,25 @@ protected int compile(List compilers, ModuleRepositor return hasSeriousErrors() ? 1 : 0; } + /** + * Run the normal declaration phases without validating bodies or generating code. + * Embedding hosts may stop here, but must not treat the resulting structures as artifacts. + * + * @param compilers the fresh module compilers + * @param repoLib the linked library repository + */ + protected void resolveDeclarations(List compilers, + ModuleRepository repoLib) { + linkModules(compilers, repoLib); + flushAndCheckErrors(compilers, "module linking"); + + resolveNames(compilers); + flushAndCheckErrors(compilers, "name resolution"); + + injectNativeTurtle(repoLib); + checkErrors("native turtle injection"); + } + /** * Flush errors from the module compilers and then check for errors globally. * diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 8e73d5719c2..334ce828e09 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -10,6 +10,8 @@ import java.util.Objects; import java.util.TreeMap; +import java.util.function.BiFunction; + import java.util.stream.Stream; import org.xvm.asm.ErrorList; @@ -585,7 +587,7 @@ private boolean loadBinaryFile() { binaryVersion = struct.getModule().getVersion(); binaryContent = Content.Module; return true; - } catch (Exception ignore) {} + } catch (Exception _) {} } else { binaryStatus = Status.NotExists; } @@ -707,7 +709,19 @@ protected char[] readSource(File file) * location, or if serious errors occur loading the source tree */ public Node getSourceTree(ErrorListener errs) { + return getSourceTree(errs, (source, listener) -> new Parser(source, listener).parseSource()); + } + + /** + * Assemble the source tree using an attempt-owned source parser. This supports explicit + * partial analysis while retaining the same source snapshot, resources and package assembly. + * The parser must report other syntax errors to its supplied listener; incomplete syntax + * must never be passed to ordinary compilation. Use a fresh ModuleInfo for each attempt. + */ + public Node getSourceTree(ErrorListener errs, + BiFunction parse) { Objects.requireNonNull(errs, "errs"); + Objects.requireNonNull(parse, "parse"); if (errs.isAbortDesired()) { return null; } @@ -735,7 +749,7 @@ public Node getSourceTree(ErrorListener errs) { return null; } - sourceNode.parse(errs); + sourceNode.parse(errs, parse); sourceNode.logErrors(errs); if (errs.hasSeriousErrors() || errs.isAbortDesired()) { return null; @@ -757,7 +771,7 @@ public Node getSourceTree(ErrorListener errs) { } /** - * Read the source trees retained by the last loading attempt, including available syntax + * Read the source trees retained by the last loading attempt, including recovered syntax * when {@link #getSourceTree(ErrorListener)} could not assemble the module. This does not load, * parse, register names or link trees. Synthetic package sources are excluded. * @@ -877,6 +891,11 @@ public void parse(ErrorListener errs) { } } + /** Parse with an attempt-owned strategy; existing custom nodes retain their own parser. */ + public void parse(ErrorListener errs, BiFunction parse) { + parse(errs); + } + /** * Collect the various top-level type names within the module. */ @@ -1093,6 +1112,11 @@ public void parse() { @Override public void parse(ErrorListener errs) { + parse(errs, (source, listener) -> new Parser(source, listener).parseSource()); + } + + @Override + public void parse(ErrorListener errs, BiFunction parse) { if (errs.isAbortDesired()) { return; } @@ -1101,14 +1125,14 @@ public void parse(ErrorListener errs) { assert m_parent != null; m_nodeSrc = new FileNode(this, "package " + file().getName() + "{}"); } - m_nodeSrc.parse(errs); + m_nodeSrc.parse(errs, parse); for (FileNode cmpFile : m_mapClzNodes.values()) { - cmpFile.parse(errs); + cmpFile.parse(errs, parse); } for (DirNode child : m_listPkgNodes) { - child.parse(errs); + child.parse(errs, parse); } } @@ -1464,12 +1488,17 @@ public void parse() { @Override public void parse(ErrorListener errs) { + parse(errs, (source, listener) -> new Parser(source, listener).parseSource()); + } + + @Override + public void parse(ErrorListener errs, BiFunction parse) { if (errs.isAbortDesired()) { return; } Source source = source(); try { - m_stmtAST = new Parser(source, ErrorListener.cancellable(this, errs::isAbortDesired)).parseSource(); + m_stmtAST = parse.apply(source, ErrorListener.cancellable(this, errs::isAbortDesired)); } catch (CompilerException e) { if (!hasSeriousErrors() && !errs.isAbortDesired()) { log(Severity.FATAL, Parser.FATAL_ERROR, null, source, @@ -1555,11 +1584,11 @@ public static String extractModuleName(File file) { Source source = new Source(file); Parser parser = new Parser(source, silent(DISCARD)); return parser.parseModuleNameIgnoreEverythingElse(); - } catch (CompilerException | IOException ignore) {} + } catch (CompilerException | IOException _) {} } else if (isExplicitCompiledFile(name)) { try { return new FileStructure(file).getModuleId().getName(); - } catch (IOException ignore) {} + } catch (IOException _) {} } } diff --git a/javatools/src/main/resources/errors.properties b/javatools/src/main/resources/errors.properties index c1f9acb2ce5..189d946dd2d 100644 --- a/javatools/src/main/resources/errors.properties +++ b/javatools/src/main/resources/errors.properties @@ -50,6 +50,7 @@ PARSER-27 = Unexpected keyword "{0}" indicates an extended type; use parenthesis PARSER-28 = Assertion used as an expression must not specify an assertion condition. PARSER-29 = Semicolon is missing. PARSER-30 = Nested too deeply; the limit is {0} levels. +PARSER-31 = Incomplete expression at the analysis cursor. COMPILER-01 = Unknown fatal compiler error. {0} COMPILER-02 = Cannot nest a module. diff --git a/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java index 30d265bb924..e1e4ddcd3ac 100644 --- a/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java +++ b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java @@ -12,6 +12,7 @@ import org.xvm.asm.ConstantPool; import org.xvm.asm.FileStructure; +import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; @@ -19,6 +20,7 @@ import org.xvm.compiler.ast.NameExpression; import org.xvm.compiler.ast.NewExpression; import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.partial.IncompleteStatement; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -38,7 +40,8 @@ public void methodFactsPreserveIdentityAtEveryPublicationBoundary() { assertEquals(new EqualInvocation(), new EqualInvocation()); List.>>of( methods -> new InvocationBinding.Facts(methods, Map.of()).methods(), - methods -> host.compilation(methods, Map.of()).callBindings()) + methods -> host.compilation(methods, Map.of()).callBindings(), + methods -> host.partial(methods, Map.of(), Map.of()).callBindings()) .forEach(copy -> verifySnapshot(copy, EqualInvocation::new, binding)); } @@ -50,7 +53,8 @@ public void functionFactsPreserveIdentityAtEveryPublicationBoundary() { List.>>of( functions -> new InvocationBinding.Facts(Map.of(), functions).functions(), - functions -> host.compilation(Map.of(), functions).functionBindings()) + functions -> host.compilation(Map.of(), functions).functionBindings(), + functions -> host.partial(Map.of(), Map.of(), functions).functionBindings()) .forEach(copy -> verifySnapshot(copy, EqualInvocation::new, call)); } @@ -69,6 +73,15 @@ public void constructorFactsPreserveIdentityAtEveryPublicationBoundary() { .forEach(copy -> verifySnapshot(copy, EqualConstruction::new, binding)); } + @Test + public void cursorFactsAreDetachedImmutableAndRejectNullEntries() { + var host = new HostInputs(); + var binding = new CursorBinding(List.of(), host.file().getModule().getIdentityConstant().getType(), false); + + verifySnapshot(cursors -> host.partial(Map.of(), cursors, Map.of()).cursorBindings(), + () -> new IncompleteStatement(name(), 4, "TEST"), binding); + } + private static void verifySnapshot(UnaryOperator> copy, Supplier key, V value) { K first = key.get(); K second = key.get(); @@ -140,5 +153,11 @@ EmbeddingSupport.Compilation compilation(Map methods, + Map cursors, + Map functions) { + return new EmbeddingSupport.PartialAnalysis(List.of(tree), List.of(), + file.getConstantPool(), methods, cursors, functions); + } } } diff --git a/javatools/src/test/java/org/xvm/compiler/CursorBindingTest.java b/javatools/src/test/java/org/xvm/compiler/CursorBindingTest.java new file mode 100644 index 00000000000..ae8d09b6638 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/CursorBindingTest.java @@ -0,0 +1,133 @@ +package org.xvm.compiler; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ConstantPool; +import org.xvm.asm.FileStructure; +import org.xvm.asm.Register; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Grouped call facts preserve scope, inspection state and the existing flat-record contract. */ +public class CursorBindingTest { + @Test + public void groupedConstructionMatchesTheExistingRecord() { + var original = fixture(); + var grouped = new CursorBinding(original.variables(), original.thisType(), original.instance(), + original.types(), original.callFacts()); + + assertEquals(original, grouped); + assertEquals(original.candidates(), grouped.callFacts().candidates()); + assertEquals(original.functions(), grouped.callFacts().functions()); + assertEquals(original.argumentValues(), grouped.callFacts().argumentValues()); + assertEquals(original.argumentProperties(), grouped.callFacts().argumentProperties()); + } + + @Test + public void independentCallUpdatesRetainEveryOtherFactAndLeaveTheOriginalAlone() { + var expected = fixture(); + var scope = new CursorBinding(expected.variables(), expected.thisType(), expected.instance()) + .withTypes(expected.types()); + var updated = scope.withCandidates(expected.candidates()) + .withFunctions(expected.functions()) + .withArgumentValues(expected.argumentValues()) + .withArgumentProperties(expected.argumentProperties()); + + assertEquals(expected, updated); + assertFalse(scope.callsInspected()); + assertTrue(scope.candidates().isEmpty()); + assertTrue(scope.functions().isEmpty()); + assertTrue(scope.argumentValues().isEmpty()); + assertTrue(scope.argumentProperties().isEmpty()); + assertEquals(updated.callFacts(), updated.withTypes(List.of()).callFacts()); + } + + @Test + public void argumentUpdatesDoNotTurnUninspectedCallsIntoRejectedCalls() { + var expected = fixture(); + var scope = new CursorBinding(expected.variables(), expected.thisType(), expected.instance()); + var values = scope.withArgumentValues(expected.argumentValues()) + .withArgumentProperties(expected.argumentProperties()); + + assertFalse(values.callsInspected()); + assertFalse(values.withTypes(expected.types()).callsInspected()); + assertTrue(values.withCandidates(List.of()).callsInspected()); + assertTrue(values.withFunctions(List.of()).callsInspected()); + assertEquals(values.argumentProperties(), values.withCandidates(List.of()).argumentProperties()); + } + + @Test + public void groupedListsAreDetachedAndImmutable() { + var expected = fixture().callFacts(); + var candidates = new ArrayList<>(expected.candidates()); + var functions = new ArrayList<>(expected.functions()); + var values = new ArrayList<>(expected.argumentValues()); + var properties = new ArrayList<>(expected.argumentProperties()); + var facts = new CursorBinding.CallFacts(candidates, true, functions, values, properties); + + List.of(candidates, functions, values, properties).forEach(List::clear); + assertEquals(expected, facts); + List.of(facts.candidates(), facts.functions(), facts.argumentValues(), facts.argumentProperties()) + .forEach(list -> assertThrows(UnsupportedOperationException.class, list::clear)); + } + + @Test + public void enclosingInstanceProposalsSurviveIndependentUpdatesAndDetachFromTheirCaller() { + var input = new ArrayList<>(List.of("this.Outer")); + var scope = fixture().withArgumentExpressions(input).withArgumentLiterals(List.of("0")); + input.clear(); + assertEquals(List.of("this.Outer"), scope.argumentExpressions()); + List.of(scope.withCandidates(List.of()), scope.withFunctions(List.of()), + scope.withArgumentValues(List.of()), scope.withArgumentProperties(List.of()), + scope.withTypes(List.of()), scope.withFormals(List.of()), scope.withArgumentLiterals(List.of())) + .forEach(updated -> assertEquals(scope.argumentExpressions(), updated.argumentExpressions())); + assertThrows(UnsupportedOperationException.class, () -> scope.argumentExpressions().clear()); + assertThrows(UnsupportedOperationException.class, () -> scope.callFacts().argumentExpressions().clear()); + assertEquals(scope, new CursorBinding(scope.variables(), scope.thisType(), scope.instance(), + scope.types(), scope.callFacts())); + } + + @Test + public void ordinaryEnclosingFactsRemainIndependentOfCallFacts() { + var names = new ArrayList<>(List.of("this.Owner")); + var scope = fixture().withEnclosingExpressions(names); + names.clear(); + List.of(scope.withCandidates(List.of()), scope.withFunctions(List.of()), + scope.withArgumentValues(List.of()), scope.withArgumentProperties(List.of()), + scope.withTypes(List.of()), scope.withFormals(List.of()), scope.withArgumentLiterals(List.of()), + scope.withArgumentExpressions(List.of())).forEach(updated -> + assertEquals(List.of("this.Owner"), updated.enclosingExpressions())); + assertThrows(UnsupportedOperationException.class, () -> scope.enclosingExpressions().clear()); + } + + @Test + public void valueTemplatesSurviveIndependentFactUpdates() { + var scope = fixture().withArgumentTemplates(List.of("(arg1) -> TODO()")); + List.of(scope.withCandidates(List.of()), scope.withFunctions(List.of()), scope.withTypes(List.of()), + scope.withFormals(List.of()), scope.withArgumentExpressions(List.of()), + scope.withEnclosingExpressions(List.of()), scope.withArgumentLiterals(List.of())) + .forEach(updated -> assertEquals(scope.argumentTemplates(), updated.argumentTemplates())); + assertThrows(UnsupportedOperationException.class, () -> scope.argumentTemplates().clear()); + } + + private static CursorBinding fixture() { + var file = new FileStructure("CursorFacts"); + var pool = file.getConstantPool(); + var identity = file.getModule().getIdentityConstant(); + var type = identity.getType(); + var signature = pool.ensureSignatureConstant("call", ConstantPool.NO_TYPES, ConstantPool.NO_TYPES); + var method = pool.ensureMethodConstant(identity, signature); + var variable = new CursorBinding.Variable("value", new Register(type, "value", 0), type, true); + var candidate = new CursorBinding.Candidate(method, signature, List.of(), false); + var function = new CursorBinding.FunctionCandidate(pool.buildFunctionType(ConstantPool.NO_TYPES), List.of()); + var property = new CursorBinding.Property("property", pool.ensurePropertyConstant(identity, "property"), type); + return new CursorBinding(List.of(variable), type, true, List.of(new CursorBinding.NamedType("CursorFacts", identity)), + List.of(candidate), true, List.of(function), List.of(variable), List.of(property)); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java b/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java new file mode 100644 index 00000000000..fc2be4d1507 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java @@ -0,0 +1,172 @@ +package org.xvm.compiler; + +import java.util.List; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement; +import org.xvm.compiler.ast.partial.IncompleteLocalDeclaration; +import org.xvm.compiler.ast.partial.IncompleteStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** A missing name is a syntax slot, never a fabricated declaration or a type-completion query. */ +public class ParserDeclarationNameTest { + @Test + public void explicitNameSlotsRetainWrittenTypesAndFollowingDeclarations() { + List.of("String §;", "List § = [];", "ecstasy.text.StringBuffer §;", + "void run(String §) {}", "void run(Int count, List § = []) {}", + "construct(String §) {}", "class Item(String §) {}") + .forEach(header -> { + String marked = "module Names { " + header + " Int later = 1; }"; + var parsed = parse(marked); + var slot = slots(parsed.tree()).findFirst().orElseThrow(); + assertEquals(header.contains("List") ? "List" : header.contains("StringBuffer") + ? "StringBuffer" : "String", slot.getDeclarationNameType().orElseThrow().getValueText()); + assertTrue(parsed.errors().hasError(Parser.INCOMPLETE_EXPRESSION)); + assertEquals(1, parsed.errors().getSeriousErrorCount(), header); + assertTrue(parsed.tree().toDumpString().contains("later")); + assertTrue(slot.getMemberName().isEmpty()); + assertTrue(slot.getReceiver().isEmpty()); + assertFalse(slot.isTypeCompletion()); + assertFalse(slot.isNameCompletion()); + assertEquals(parsed.cursor(), slot.getEndPosition()); + assertEquals(marked.replace("§", ""), parsed.source().toRawString()); + + var copy = (StatementBlock) parsed.tree().clone(); + var copiedSlot = slots(copy).findFirst().orElseThrow(); + assertNotSame(slot, copiedSlot); + assertNotSame(slot.getTarget(), copiedSlot.getTarget()); + assertSame(copiedSlot, copiedSlot.getTarget().getParent()); + assertEquals(slot.getDeclarationNameType(), copiedSlot.getDeclarationNameType()); + assertFalse(copiedSlot.isTypeCompletion()); + assertTrue(copiedSlot.getEndPosition() <= copiedSlot.getParent().getEndPosition()); + + var ordinary = new ErrorList(); + var tree = new Parser(new Source(marked.replace("§", "")), ordinary).parseSource(); + assertTrue(ordinary.hasSeriousErrors()); + assertEquals(0, slots(tree).count()); + assertFalse(ordinary.hasError(Parser.INCOMPLETE_EXPRESSION)); + }); + } + + @Test + public void eofSlotIncludesTrailingWhitespaceWithoutAcquiringANameOrEmission() { + List.of("module Names { String §", "module Names { void run(String §", + "module Names { class Item(String §").forEach(marked -> { + var parsed = parse(marked); + var slot = slots(parsed.tree()).findFirst().orElseThrow(); + var copy = (StatementBlock) parsed.tree().clone(); + var copiedSlot = slots(copy).findFirst().orElseThrow(); + assertEquals(parsed.cursor(), slot.getEndPosition()); + assertTrue(copiedSlot.getEndPosition() <= copiedSlot.getParent().getEndPosition()); + assertTrue(nodes(copy).filter(IncompleteDeclarationStatement.class::isInstance) + .map(IncompleteDeclarationStatement.class::cast) + .allMatch(declaration -> declaration.getKind() != IncompleteDeclarationStatement.Kind.PROPERTY + || declaration.getNameToken().isEmpty())); + assertThrows(IllegalStateException.class, () -> copiedSlot.getParent().generateCode(null, new ErrorList())); + }); + } + + @Test + public void wrappedTypesRemainIndependentSyntaxWithoutRegisteringDeclarations() { + List.of("String? § = Null;", "String[] § = [];", "immutable String §;", + "void run((String | Int) §) {}", "void run(function String(Int)? §) {}", + "class Item(Element?[] §) {}", "void run(@RO String §) {}") + .forEach(header -> { + var parsed = parse("module Names { " + header + " Int later = 1; }"); + var slot = slots(parsed.tree()).findFirst().orElseThrow(() -> + new AssertionError(header + ": " + parsed.tree().toDumpString() + parsed.errors())); + var copy = slots((StatementBlock) parsed.tree().clone()).findFirst().orElseThrow(); + assertTrue(slot.getDeclarationType().isPresent(), header); + assertNotSame(slot.getTarget(), copy.getTarget()); + assertEquals(slot.getDeclarationType().orElseThrow().toString(), + copy.getDeclarationType().orElseThrow().toString()); + assertTrue(slot.getReceiver().isEmpty()); + assertFalse(slot.isTypeCompletion()); + assertFalse(slot.isCall()); + assertTrue(parsed.tree().toDumpString().contains("later")); + assertEquals(1, parsed.errors().getSeriousErrorCount(), header); + }); + } + + @Test + public void missingInferredLocalsRetainInitializerAndIndependentChildren() { + List.of("val § = \"hello\";", "var § = new StringBuffer();").forEach(declaration -> { + var parsed = parse("module Names { void run() { " + declaration + " Int later = 1; } }"); + var slot = slots(parsed.tree()).findFirst().orElseThrow(); + var local = nodes(parsed.tree()).filter(IncompleteLocalDeclaration.class::isInstance) + .map(IncompleteLocalDeclaration.class::cast).findFirst().orElseThrow(); + var copy = local.clone(); + assertNotSame(local.getInitializer(), copy.getInitializer()); + assertSame(copy, copy.getInitializer().getParent()); + assertTrue(parsed.tree().toDumpString().contains("later")); + assertTrue(slot.getDeclarationType().isPresent()); + assertEquals(1, parsed.errors().getSeriousErrorCount()); + assertTrue(local.getEndPosition() > slot.getEndPosition()); + }); + } + + @Test + public void nonNameContextsDoNotAcquireSlots() { + List.of("String§;", "String §already;", "List<§> value;", "List assertEquals(0, slots(parse("module Names { " + header + " }").tree()).count(), header)); + } + + @Test + public void slotsRespectErrorBudgetsCancellationAndSpeculation() { + var source = new Source("module Names { String ; }"); + "module Names { String ".chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + assertThrows(CompilerException.class, () -> Parser.forPartialAnalysis(source.clone(), cursor, + new ErrorList(ErrorList.FIRST_ERROR)).parseSource()); + assertThrows(CompilerException.class, () -> Parser.forPartialAnalysis(source.clone(), cursor, + ErrorListener.cancellable(new ErrorList(), () -> true)).parseSource()); + var errors = new ErrorList(); + var parser = Parser.forPartialAnalysis(source, cursor, errors); + assertThrows(CompilerException.class, () -> { + try (var ignored = parser.attempt()) { + parser.parseTypeCompositionStatement(); + } + }); + assertFalse(errors.hasSeriousErrors()); + assertEquals(1, slots(parser.parseSource()).count()); + } + + private static Parsed parse(String marked) { + var source = new Source(marked.replace("§", "")); + marked.substring(0, marked.indexOf('§')).chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var errors = new ErrorList(); + return new Parsed(Parser.forPartialAnalysis(source, cursor, errors).parseSource(), source, cursor, errors); + } + + private static Stream nodes(AstNode node) { + return Stream.concat(Stream.of(node), StreamSupport.stream(node.children().spliterator(), false) + .flatMap(ParserDeclarationNameTest::nodes)); + } + + private static Stream slots(AstNode root) { + return nodes(root).filter(IncompleteStatement.class::isInstance).map(IncompleteStatement.class::cast) + .filter(site -> site.getDeclarationType().isPresent()); + } + + private record Parsed(StatementBlock tree, Source source, long cursor, ErrorList errors) {} +} diff --git a/javatools/src/test/java/org/xvm/compiler/ParserRecoveryTest.java b/javatools/src/test/java/org/xvm/compiler/ParserRecoveryTest.java new file mode 100644 index 00000000000..85cfc0e181e --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ParserRecoveryTest.java @@ -0,0 +1,672 @@ +package org.xvm.compiler; + +import java.time.Duration; + +import java.util.ArrayList; +import java.util.List; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorList; +import org.xvm.asm.ErrorListener; + +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.MethodDeclarationStatement; +import org.xvm.compiler.ast.NamedTypeExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.PropertyDeclarationStatement; +import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.TypeCompositionStatement; +import org.xvm.compiler.ast.VariableDeclarationStatement; +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Recovery retains real syntax without validating or inventing a replacement expression. */ +public class ParserRecoveryTest { + @Test + public void selectedMembersBeforeOperatorsRetainBothOperandsAndFollowingStatements() { + List.of(" + 1", " < 1", " == 1", " ? 1 : 0", " ?: 1", " * 2") + .forEach(suffix -> { + String marked = "module Recovery { void run(String text) { val value = text.si§ze" + + suffix + "; Int after = 1; } }"; + var source = new Source(marked.replace("§", "")); + marked.substring(0, marked.indexOf('§')).chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var errors = new ErrorList(); + var tree = Parser.forPartialAnalysis(source, cursor, errors).parseSource(); + var sites = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).toList(); + assertEquals(1, sites.size(), suffix); + assertEquals("size", sites.getFirst().getMemberName().orElseThrow().getValueText()); + assertEquals(cursor, sites.getFirst().getEndPosition()); + assertTrue(tree.toDumpString().contains("after")); + assertTrue(errors.getErrors().stream().allMatch(error -> + error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + assertEquals(marked.replace("§", ""), source.toRawString()); + }); + } + + @Test + public void memberCursorsBeforeWrittenCallsRetainTheirArgumentsAndFollowingStatements() { + List.of("text.tr§()", "text.trim().tr§()", "text.ind§(\"a,b\", 1)") + .forEach(expression -> { + String marked = "module Recovery { void run(String text) { String value = " + + expression + "; Int after = 1; } }"; + var source = new Source(marked.replace("§", "")); + marked.substring(0, marked.indexOf('§')).chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var errors = new ErrorList(); + var tree = Parser.forPartialAnalysis(source, cursor, errors).parseSource(); + assertNotNull(tree); + assertTrue(tree.toDumpString().contains("after")); + var call = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).filter(IncompleteStatement::isCall) + .findFirst().orElseThrow(); + assertEquals(expression.contains("ind") ? 2 : 0, call.getArguments().size()); + assertTrue(errors.getErrors().stream().allMatch(error -> + error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + }); + } + + @Test + public void malformedStatementRetainsItsMethodAndFollowingDeclarations() { + ErrorList errs = new ErrorList(); + StatementBlock tree = parse(""" + module Recovery { + void damaged() { console.; Int kept = 1; } + Int after = 2; + } + """, errs); + assertTrue(errs.hasError(Parser.EXPECTED_TOKEN)); + assertEquals(List.of("Recovery", "damaged", "kept", "after"), names(tree)); + } + + @Test + public void missingBracesAndTrailingDotRetainCompletedHeadersAtActualSourceEnd() { + List.of("", "void run() {", "void run() { console.").forEach(suffix -> { + String text = "module Recovery {\n " + suffix; + ErrorList errs = new ErrorList(); + StatementBlock tree = parse(text, errs); + assertTrue(errs.hasSeriousErrors()); + assertTrue(names(tree).contains("Recovery")); + if (!suffix.isEmpty()) { + assertTrue(names(tree).contains("run")); + } + assertEquals(1, Source.calculateLine(tree.getEndPosition())); + assertEquals(4 + suffix.length(), Source.calculateOffset(tree.getEndPosition())); + assertEquals(1, errs.getErrors().stream() + .filter(err -> err.getCode().equals(Parser.UNEXPECTED_EOF)).count()); + }); + } + + @Test + public void malformedMemberDoesNotDiscardItsSiblings() { + ErrorList errs = new ErrorList(); + StatementBlock tree = parse(""" + module Recovery { + Int before = 1; + Int broken = ; + Int after = 2; + class Nested { Int value = 3; } + } + """, errs); + assertTrue(errs.hasSeriousErrors()); + assertEquals(List.of("Recovery", "before", "after", "Nested", "value"), names(tree)); + } + + @Test + public void recoverySkipsBalancedBodiesAndAlwaysMakesProgress() { + ErrorList errs = new ErrorList(ErrorList.UNLIMITED); + StatementBlock tree = parse(""" + module Recovery { + void run() { + return ; + Int x = ; + if () { Int hidden = 1; } + Int kept = 2; + } + void after() {} + } + """, errs); + assertTrue(errs.hasSeriousErrors()); + assertEquals(List.of("Recovery", "run", "kept", "after"), names(tree)); + } + + @Test + public void aFailedSpeculationDoesNotRecoverOrPublishItsDiagnostics() { + ErrorList errs = new ErrorList(); + Parser parser = new Parser(new Source("module Recovery { void run() { console.; } }"), errs); + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt ignored = parser.attempt()) { + parser.parseTypeCompositionStatement(); + } + }); + assertFalse(errs.hasSeriousErrors()); + assertNotNull(parser.parseSource()); + assertTrue(errs.hasError(Parser.EXPECTED_TOKEN)); + } + + @Test + public void recoveryHonorsTheErrorBudget() { + ErrorList errs = new ErrorList(ErrorList.FIRST_ERROR); + assertThrows(CompilerException.class, () -> parse( + "module Recovery { Int broken = ; Int alsoBroken = ; }", errs)); + assertEquals(1, errs.getSeriousErrorCount()); + } + + @Test + public void validClosingBracesDoNotIncludeTrailingCommentsInTheirRanges() { + ErrorList errs = new ErrorList(); + String text = "module Recovery { void run() {} }\n// trailing café 😀"; + StatementBlock tree = parse(text, errs); + assertFalse(errs.hasSeriousErrors()); + assertEquals(0, Source.calculateLine(tree.getEndPosition())); + assertEquals(text.indexOf('\n'), Source.calculateOffset(tree.getEndPosition())); + } + + @Test + public void unterminatedStringsFinishAfterOneDiagnosticEvenWithoutAnAbortBudget() { + List.of("\"", "\"café 😀", "$\"", "$\"unfinished").forEach(text -> { + ErrorList errs = new ErrorList(ErrorList.UNLIMITED); + AtomicInteger reports = new AtomicInteger(); + Lexer lexer = new Lexer(new Source(text), ErrorListener.collecting(error -> { + // Fail on a repeat instead of leaving an infinite lexer running after a timeout. + assertEquals(1, reports.incrementAndGet()); + errs.log(error); + })); + Token token = lexer.next(); + assertFalse(lexer.hasNext()); + assertEquals(text.length(), Source.calculateOffset(token.getEndPosition())); + assertTrue(errs.hasError(Lexer.STRING_NO_TERM)); + assertEquals(1, reports.get()); + }); + } + + @Test + public void partialParsingIsOptInAndClonesOwnTheirIncompleteChildren() { + String text = "module Recovery { void run() { console.print(1, "; + StatementBlock ordinary = parse(text, new ErrorList()); + assertTrue(nodes(ordinary).stream().noneMatch(IncompleteStatement.class::isInstance)); + + ErrorList errs = new ErrorList(); + StatementBlock partial = Parser.forPartialAnalysis(new Source(text), errs).parseSource(); + assertEquals(List.of(Parser.UNEXPECTED_EOF), errs.getErrors().stream().map(ErrorListener.ErrorInfo::getCode).toList()); + IncompleteStatement site = nodes(partial).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + IncompleteStatement clone = (IncompleteStatement) site.clone(); + assertNotSame(site.getTarget(), clone.getTarget()); + assertNotSame(site.getReceiver().orElseThrow(), clone.getReceiver().orElseThrow()); + assertNotSame(site.getArguments().getFirst(), clone.getArguments().getFirst()); + assertEquals(site.getEndPosition(), clone.getEndPosition()); + assertEquals(site.getSeparators(), clone.getSeparators()); + assertFalse(clone.getReceiver().orElseThrow().isValidated()); + } + + @Test + public void partialParserSpeculationCannotPublishAnIncompleteSite() { + ErrorList errs = new ErrorList(); + Parser parser = Parser.forPartialAnalysis(new Source("module Recovery { void run() { console."), errs); + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt ignored = parser.attempt()) { + parser.parseTypeCompositionStatement(); + } + }); + assertFalse(errs.hasSeriousErrors()); + assertEquals(1, nodes(parser.parseSource()).stream().filter(IncompleteStatement.class::isInstance).count()); + } + + @Test + public void missingCursorDelimitersRetainOriginalRangesAndFollowingDeclarations() { + List.of("((value.si", "work((value.si", "values[value.si", "(1, value.si", + "Tuple:(1, value.si", "[value.si", "[1=value.si", "Map:[1=value.si").forEach(expression -> { + String prefix = "module Recovery { Int run(String value) { return " + expression; + String text = prefix + "; } Int later = 42; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + ErrorList errs = new ErrorList(); + StatementBlock tree = Parser.forPartialAnalysis(source, cursor, errs).parseSource(); + assertTrue(names(tree).containsAll(List.of("Recovery", "run", "later"))); + assertTrue(errs.getErrors().stream() + .allMatch(error -> error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + IncompleteStatement site = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).filter(node -> !node.isCall()) + .findFirst().orElseThrow(); + assertEquals(cursor, site.getEndPosition()); + assertEquals(prefix.length() - 2, Source.calculateOffset( + site.getMemberName().orElseThrow().getStartPosition())); + assertEquals(text, source.toRawString()); + + ErrorList ordinary = new ErrorList(); + assertTrue(nodes(parse(text, ordinary)).stream().noneMatch(IncompleteStatement.class::isInstance)); + assertTrue(ordinary.hasSeriousErrors()); + }); + } + + @Test + public void argumentPrefixesRetainCallSyntaxAndCloneOwnership() { + List.of("work(1, te", "work(first=1, second=te", + "new Box(1, te", "new Box(first=1, second=te", + "outer.new Child(1, te", "box.new(1, te", "new @Tagged Box(1, te").forEach(call -> { + String prefix = "module Recovery { void run() { " + call; + String text = prefix + "); } Int later = 42; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + ErrorList errs = new ErrorList(); + StatementBlock tree = Parser.forPartialAnalysis(source, cursor, errs).parseSource(); + var sites = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).toList(); + assertEquals(1, sites.size(), call); + var site = sites.getFirst(); + assertTrue(site.isCall(), call); + assertEquals(1, site.getArguments().size()); + assertEquals(1, site.getSeparators().size()); + assertEquals("te", site.getArgumentPrefix().orElseThrow().getValueText()); + assertEquals(prefix.length() - 2, Source.calculateOffset( + site.getArgumentPrefix().orElseThrow().getStartPosition())); + assertEquals(call.contains("second=") ? "second" : "", + site.getPendingArgumentName().map(Token::getValueText).orElse("")); + var clone = (IncompleteStatement) site.clone(); + assertNotSame(site.getTarget(), clone.getTarget()); + assertNotSame(site.getArguments().getFirst(), clone.getArguments().getFirst()); + assertEquals(site.getArgumentPrefix(), clone.getArgumentPrefix()); + assertEquals(site.getPendingArgumentName(), clone.getPendingArgumentName()); + assertTrue(names(tree).contains("later")); + assertEquals(text, source.toRawString()); + assertTrue(errs.getErrors().stream().allMatch(error -> error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + ErrorList ordinary = new ErrorList(); + assertTrue(nodes(parse(text, ordinary)).stream().noneMatch(IncompleteStatement.class::isInstance)); + assertFalse(ordinary.hasSeriousErrors()); + }); + } + + @Test + public void arrayDimensionsRemainOwnedSyntaxBeforeTrailingArguments() { + String prefix = "module Recovery { void run() { new String[2](te"; + Source source = new Source(prefix + "); } }"); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var tree = Parser.forPartialAnalysis(source, cursor, new ErrorList()).parseSource(); + var site = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + assertEquals(1, site.getLeadingArguments().size()); + assertTrue(site.getArguments().isEmpty()); + assertTrue(site.getSeparators().isEmpty()); + var clone = (IncompleteStatement) site.clone(); + assertNotSame(site.getLeadingArguments().getFirst(), clone.getLeadingArguments().getFirst()); + assertEquals("2", site.getLeadingArguments().getFirst().toString()); + } + + @Test + public void dimensionCursorRetainsItsOwnerWithoutDuplicatingLookaheadDiagnostics() { + List.of("]", "", "](\"x\")").forEach(suffix -> { + String prefix = "module Recovery { void run(Int number) { new String[nu"; + String text = prefix + suffix + "; } Int later = 1; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reported = new ArrayList(); + var errs = ErrorListener.collecting(error -> reported.add(error.getCode())); + var tree = Parser.forPartialAnalysis(source, cursor, errs).parseSource(); + var sites = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).toList(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reported); + assertEquals(1, sites.size()); + var site = sites.getFirst(); + assertTrue(site.isCall()); + assertEquals(Token.Id.L_SQUARE, site.getOperator().getId()); + assertTrue(site.getTarget() instanceof NewExpression); + assertTrue(site.getLeadingArguments().isEmpty()); + assertEquals("nu", site.getArgumentPrefix().orElseThrow().getValueText()); + assertEquals(cursor, site.getEndPosition()); + var clone = (IncompleteStatement) site.clone(); + assertNotSame(site.getTarget(), clone.getTarget()); + assertEquals(site.getArgumentPrefix(), clone.getArgumentPrefix()); + assertTrue(names(tree).contains("later")); + assertEquals(text, source.toRawString()); + }); + } + + @Test + public void missingDelimiterRecoveryHonorsBudgetsCancellationAndSpeculation() { + String prefix = "module Recovery { Int run(String value) { return ((value."; + String text = prefix + "; } }"; + Source source = new Source(prefix); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + ErrorList budget = new ErrorList(ErrorList.FIRST_ERROR); + assertThrows(CompilerException.class, () -> + Parser.forPartialAnalysis(new Source(text), cursor, budget).parseSource()); + assertEquals(1, budget.getSeriousErrorCount()); + + ErrorListener cancelled = ErrorListener.cancellable(new ErrorList(), () -> true); + assertThrows(CompilerException.class, () -> + Parser.forPartialAnalysis(new Source(text), cursor, cancelled).parseSource()); + + ErrorList errs = new ErrorList(); + Parser parser = Parser.forPartialAnalysis(new Source(text), cursor, errs); + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt ignored = parser.attempt()) { + parser.parseTypeCompositionStatement(); + } + }); + assertFalse(errs.hasSeriousErrors()); + assertEquals(1, nodes(parser.parseSource()).stream().filter(IncompleteStatement.class::isInstance).count()); + } + + @Test + public void malformedHeadersRetainWrittenNamesButDoNotExposeTheirBodiesAsMembers() { + List.of("void damaged(Int)", "void damaged(Int value", "void damaged(").forEach(header -> { + var errs = new ErrorList(); + var tree = parse("module Recovery { " + header + " { Int hidden=1; } Int later=2; }", errs); + assertTrue(errs.hasSeriousErrors()); + var declaration = nodes(tree).stream().filter(IncompleteDeclarationStatement.class::isInstance) + .map(IncompleteDeclarationStatement.class::cast).findFirst().orElseThrow(); + assertEquals("damaged", declaration.getNameToken().orElseThrow().getValueText()); + assertEquals(IncompleteDeclarationStatement.Kind.METHOD, declaration.getKind()); + assertEquals(List.of("Recovery", "later"), names(tree)); + assertFalse(declaration.children().hasNext()); + }); + } + + @Test + public void declarationTypeCursorIsOwnedSyntaxWithOneDiagnosticAndIndependentClones() { + List.of("void damaged(Str| value) {}", "void damaged(|) {}", + "void damaged(Int first, |) {}", "Str| property;", "Str| damaged() {}", "Str|;", + "void damaged(ecstasy.text.Str| value) {}", "ecstasy.text.Str| property;", "ecstasy.text.Str| damaged() {}").forEach(header -> { + String prefix = "module Recovery { " + header.substring(0, header.indexOf('|')); + String text = prefix + header.substring(header.indexOf('|') + 1) + " Int later=1; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reports = new ArrayList(); + var tree = Parser.forPartialAnalysis(source, cursor, + ErrorListener.collecting(error -> reports.add(error.getCode()))).parseSource(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reports, header); + var declaration = nodes(tree).stream().filter(IncompleteDeclarationStatement.class::isInstance) + .map(IncompleteDeclarationStatement.class::cast).findFirst().orElseThrow(); + var site = assertInstanceOf(IncompleteStatement.class, declaration.children().next()); + assertEquals(cursor, site.getEndPosition()); + var clone = (IncompleteDeclarationStatement) declaration.clone(); + var clonedSite = assertInstanceOf(IncompleteStatement.class, clone.children().next()); + assertNotSame(site, clonedSite); + assertNotSame(site.getTarget(), clonedSite.getTarget()); + assertSame(clone, clonedSite.getParent()); + assertSame(clonedSite, clonedSite.getTarget().getParent()); + assertTrue(clonedSite.isTypeCompletion()); + assertEquals(declaration.getNameToken(), clone.getNameToken()); + assertEquals(header.equals("Str|;"), declaration.getNameToken().isEmpty()); + assertTrue(names(tree).contains("later")); + assertEquals(text, source.toRawString()); + }); + } + + @Test + public void genericBaseCursorKeepsWrittenArgumentsWithIndependentCloning() { + List.of("void damaged(Li§st value) {}", "void damaged(Map> value) {}") + .forEach(header -> { + String prefix = "module Recovery { " + header.substring(0, header.indexOf('§')); + String text = prefix + header.substring(header.indexOf('§') + 1) + " Int later=1; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var errors = new ErrorList(); + var tree = Parser.forPartialAnalysis(source, cursor, errors).parseSource(); + tree = (StatementBlock) tree.clone(); + var site = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + var type = assertInstanceOf(NamedTypeExpression.class, site.getTarget()); + assertEquals("List", type.getName()); + assertEquals(1, type.getParamTypes().size()); + assertEquals("Li", site.getCompletionPrefix()); + var copy = (IncompleteStatement) site.clone(); + var copied = assertInstanceOf(NamedTypeExpression.class, copy.getTarget()); + assertNotSame(type.getParamTypes().getFirst(), copied.getParamTypes().getFirst()); + assertSame(copied, copied.getParamTypes().getFirst().getParent()); + assertEquals(text, source.toRawString()); + assertTrue(names(tree).contains("later")); + }); + } + + @Test + public void headerSlotsAndGenericDeclarationsHaveOneOwnedCursor() { + List.of("void damaged(ecstasy.text.§ value) {}", "void damaged(ecstasy.te§xt.String value) {}", + "void damaged(Int | § value) {}", "void damaged(List<(Int | §)> value) {}", + "(Int, Str§) damaged() {}", "(Int count, Str§ text) damaged() {}", + " (T first, Str§ second,) damaged(T value) {}", + " void damaged(Str§ value) {}", " Str§ damaged(T value) {}", + " void damaged(T value) {}").forEach(header -> { + String marked = "module Recovery { " + header + " Int later=1; }"; + String text = marked.replace("§", ""); + Source source = new Source(text); + marked.substring(0, marked.indexOf('§')).chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reports = new ArrayList(); + var tree = Parser.forPartialAnalysis(source, cursor, + ErrorListener.collecting(error -> reports.add(error.getCode()))).parseSource(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reports, header); + var copy = (StatementBlock) tree.clone(); + var original = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + var site = nodes(copy).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + assertNotSame(original.getTarget(), site.getTarget()); + assertSame(site, site.getTarget().getParent()); + assertTrue(site.isTypeCompletion()); + assertEquals(List.of("Recovery", "later"), names(copy)); + assertEquals(text, source.toRawString()); + var ordinary = new ErrorList(); + parse(text, ordinary); + assertFalse(ordinary.getErrors().stream().anyMatch(error -> error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + }); + } + + @Test + public void compoundHeadersRetainOnlyTheSelectedLeafWithIndependentOwnership() { + List.of("void damaged(Map> value) {}", + "void damaged(Map value) {}", "void damaged(List<(Int | Str§)> value) {}", + "void damaged(List value) {}", "List property;", + "List damaged() {}", "void damaged(List, > value) {}", + "void damaged(function void(List) value) {}", "void damaged(function void(Str§ value) {}", + "void damaged(Function< { + String prefix = "module Recovery { " + header.substring(0, header.indexOf('§')); + String text = prefix + header.substring(header.indexOf('§') + 1) + " Int later=1; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reports = new ArrayList(); + var tree = Parser.forPartialAnalysis(source, cursor, + ErrorListener.collecting(error -> reports.add(error.getCode()))).parseSource(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reports, header); + var declaration = nodes(tree).stream().filter(IncompleteDeclarationStatement.class::isInstance) + .map(IncompleteDeclarationStatement.class::cast).findFirst().orElseThrow(); + var site = assertInstanceOf(IncompleteStatement.class, declaration.children().next()); + assertFalse(site.getTarget().children().hasNext()); + assertEquals(cursor, site.getTarget().getEndPosition()); + var clone = (IncompleteDeclarationStatement) declaration.clone(); + var clonedSite = assertInstanceOf(IncompleteStatement.class, clone.children().next()); + assertTrue(clonedSite.isTypeCompletion(), header); + assertSame(clone, clonedSite.getParent()); + assertNotSame(site.getTarget(), clonedSite.getTarget()); + assertSame(clonedSite, clonedSite.getTarget().getParent()); + assertEquals(List.of("Recovery", "later"), names(tree)); + assertEquals(text, source.toRawString()); + var ordinary = new ErrorList(); + parse(text, ordinary); + assertFalse(ordinary.getErrors().stream().anyMatch(error -> error.getCode().equals(Parser.INCOMPLETE_EXPRESSION))); + assertEquals(header.contains("§ value"), ordinary.hasSeriousErrors(), header); + }); + } + + @Test + public void genericHeaderCursorsKeepWholeTokensAndIndependentQualifierChildren() { + List.of("List<§>", "Map<§, Int>", "Map", "Str§ing", + "Owner.Ite§m", "Owner>.Ite§m").forEach(type -> { + String marked = "module Recovery { void damaged(" + type + " value) {} Int later=1; }"; + String text = marked.replace("§", ""); + Source source = new Source(text); + marked.substring(0, marked.indexOf('§')).chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reports = new ArrayList(); + var tree = Parser.forPartialAnalysis(source, cursor, + ErrorListener.collecting(error -> reports.add(error.getCode()))).parseSource(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reports, type); + // Cloning adopts the raw parser children, providing their normal source ancestry. + tree = (StatementBlock) tree.clone(); + var site = nodes(tree).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + var token = site.getMemberName().orElseThrow(); + assertEquals(type.contains("Ite") ? "Ite" : type.contains("Str§") ? "Str" : "", site.getCompletionPrefix()); + assertEquals(type.contains("Ite") ? "Item" : type.contains("Str§") ? "String" : "", token.getValueText()); + var clone = (IncompleteStatement) site.clone(); + assertEquals(site.getCompletionPrefix(), clone.getCompletionPrefix()); + var originalNodes = nodes(site); + var clonedNodes = nodes(clone); + assertEquals(originalNodes.size(), clonedNodes.size()); + IntStream.range(0, originalNodes.size()) + .forEach(i -> assertNotSame(originalNodes.get(i), clonedNodes.get(i))); + assertSame(clone, clone.getTarget().getParent()); + assertEquals(List.of("Recovery", "later"), names(tree)); + assertEquals(text, source.toRawString()); + }); + } + + @Test + public void typeHeadersKeepTheirBodyAndCloneTheirCursorIndependently() { + List.of("class Damaged extends Ba§", "interface Damaged extends Ba§", + "class Damaged implements List", "class Damaged implements List { + String prefix = "module Recovery { " + header.substring(0, header.indexOf('§')); + String text = prefix + header.substring(header.indexOf('§') + 1) + + " { Int inside=1; } Int later=2; }"; + Source source = new Source(text); + prefix.chars().forEach(_ -> source.next()); + long cursor = source.getPosition(); + source.reset(); + var reports = new ArrayList(); + var tree = Parser.forPartialAnalysis(source, cursor, + ErrorListener.collecting(error -> reports.add(error.getCode()))).parseSource(); + assertEquals(List.of(Parser.INCOMPLETE_EXPRESSION), reports, header); + var declaration = nodes(tree).stream().filter(IncompleteTypeCompositionStatement.class::isInstance) + .map(IncompleteTypeCompositionStatement.class::cast).findFirst().orElseThrow(); + var clone = (IncompleteTypeCompositionStatement) declaration.clone(); + var site = nodes(declaration).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + var clonedSite = nodes(clone).stream().filter(IncompleteStatement.class::isInstance) + .map(IncompleteStatement.class::cast).findFirst().orElseThrow(); + assertNotSame(site, clonedSite); + assertSame(clone, clonedSite.getParent()); + assertTrue(clonedSite.isTypeCompletion()); + var cursorChildren = declaration.children(); + var body = assertInstanceOf(StatementBlock.class, cursorChildren.next()); + var clonedBody = assertInstanceOf(StatementBlock.class, clone.children().next()); + assertNotSame(body, clonedBody); + assertNotSame(body.children().next(), clonedBody.children().next()); + assertSame(clone, clonedBody.getParent()); + assertSame(site, cursorChildren.next()); + assertThrows(UnsupportedOperationException.class, cursorChildren::remove); + assertEquals(cursor, clonedSite.getEndPosition()); + assertEquals(List.of("Recovery", "Damaged", "inside", "later"), names(tree)); + assertEquals(List.of("Damaged", "inside"), names(clone)); + assertEquals(text, source.toRawString()); + }); + } + + @Test + public void malformedTypeHeadersRetainStructureAndHonorStopping() { + String text = "module Recovery { class Damaged extends { Int inside=1; } Int later=2; }"; + var errors = new ErrorList(); + assertEquals(List.of("Recovery", "Damaged", "inside", "later"), names(parse(text, errors))); + assertTrue(errors.hasSeriousErrors()); + assertThrows(CompilerException.class, () -> parse(text, new ErrorList(ErrorList.FIRST_ERROR))); + assertThrows(CompilerException.class, () -> new Parser(new Source(text), + ErrorListener.cancellable(new ErrorList(), () -> true)).parseSource()); + var strict = new Parser(new Source("class Damaged extends { Int inside=1; }"), new ErrorList()); + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt ignored = strict.attempt()) { + strict.parseTypeCompositionStatement(); + } + }); + } + + @Test + public void declarationRecoveryHonorsBudgetsCancellationAndSpeculation() { + String text = "module Recovery { void damaged(Int) {} Int later=1; }"; + var budget = new ErrorList(ErrorList.FIRST_ERROR); + assertThrows(CompilerException.class, () -> parse(text, budget)); + assertEquals(1, budget.getSeriousErrorCount()); + assertThrows(CompilerException.class, () -> new Parser(new Source(text), + ErrorListener.cancellable(new ErrorList(), () -> true)).parseSource()); + var errs = new ErrorList(); + var parser = new Parser(new Source(text), errs); + assertThrows(CompilerException.class, () -> { + try (Parser.Attempt ignored = parser.attempt()) { + parser.parseTypeCompositionStatement(); + } + }); + assertFalse(errs.hasSeriousErrors()); + assertEquals(1, nodes(parser.parseSource()).stream() + .filter(IncompleteDeclarationStatement.class::isInstance).count()); + + String largeBody = "module Recovery { void damaged(Int) { " + "Int hidden=1; ".repeat(1000) + "} }"; + Source source = new Source(largeBody); + assertThrows(CompilerException.class, () -> new Parser(source, ErrorListener.cancellable( + new ErrorList(), () -> Source.calculateOffset(source.getPosition()) > 200)).parseSource()); + assertTrue(Source.calculateOffset(source.getPosition()) < 300, "Cancellation must stop inside the skipped body"); + } + + private List nodes(AstNode root) { + List nodes = new ArrayList<>(List.of(root)); + for (int i = 0; i < nodes.size(); ++i) { + nodes.get(i).children().forEachRemaining(nodes::add); + } + return nodes; + } + + private StatementBlock parse(String text, ErrorList errs) { + return assertTimeoutPreemptively(Duration.ofSeconds(3), + () -> new Parser(new Source(text, "Recovery.x"), errs).parseSource()); + } + + private List names(AstNode node) { + List names = new ArrayList<>(); + switch (node) { + case TypeCompositionStatement type -> names.add(type.getName()); + case MethodDeclarationStatement method -> names.add(method.getName()); + case PropertyDeclarationStatement property -> names.add(property.getName()); + case VariableDeclarationStatement variable -> names.add(variable.getNameToken().getValueText()); + default -> {} + } + node.children().forEachRemaining(child -> names.addAll(names(child))); + return names; + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java b/javatools/src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java new file mode 100644 index 00000000000..60f327c0439 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java @@ -0,0 +1,134 @@ +package org.xvm.compiler.ast; + +import java.util.List; +import java.util.stream.Stream; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Compiler.Stage; +import org.xvm.compiler.Parser; +import org.xvm.compiler.Source; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement; +import org.xvm.compiler.ast.partial.IncompleteExpression; +import org.xvm.compiler.ast.partial.IncompleteLocalDeclaration; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** Tree copying preserves syntax ownership through both new and compatibility entry points. */ +class AstTreeCopyTest { + @Test + void recoveryCopiesPreserveMetadataAndOwnChildrenInsideOrdinaryTrees() { + for (var original : recoveryNodes()) { + var statement = original instanceof Statement node ? node + : new ExpressionStatement((Expression) original); + var parent = new StatementBlock(List.of(statement)); + parent.introduceParentage(); + original.setStage(Stage.Registered); + + for (var copy : List.of(original.copyTree(), original.clone())) { + assertTreeCopy(original, copy, original.getParent()); + assertEquals(original.toDumpString(), copy.toDumpString()); + } + // An ordinary node must dispatch to constructor copies for its recovery descendants. + for (StatementBlock copy : List.of(parent.copyTree(), (StatementBlock) parent.clone())) { + assertTreeCopy(parent, copy, null); + } + } + } + + @Test + void callCopiesRetainCursorTokensAndKeepArgumentEditsIndependent() { + var original = IncompleteStatement.forArgumentPrefix(name("call"), token(Id.L_PAREN), + List.of(name("argument")), List.of(token(Id.COMMA)), 20, token("label"), token("prefix")); + ((AstNode) original).introduceParentage(); + for (IncompleteStatement copy : List.of(original.copyTree(), (IncompleteStatement) original.clone())) { + assertSame(original.getOperator(), copy.getOperator()); + assertEquals(original.getSeparators(), copy.getSeparators()); + assertEquals(original.getPendingArgumentName(), copy.getPendingArgumentName()); + assertEquals(original.getArgumentPrefix(), copy.getArgumentPrefix()); + var replacement = name("replacement"); + copy.replaceChild(copy.getArguments().getFirst(), replacement); + assertSame(copy, replacement.getParent()); + assertSame(replacement, copy.getArguments().getFirst()); + assertEquals("argument", original.getArguments().getFirst().toString()); + } + } + + @Test + void copiedRecoveryHeadersKeepTheirChildListsImmutable() { + for (var original : recoveryNodes()) { + if (original instanceof IncompleteDeclarationStatement + || original instanceof IncompleteTypeCompositionStatement) { + for (var copy : List.of(original.copyTree(), original.clone())) { + for (String field : List.of("cursors", "formals")) { + var children = (List) copy.getDumpChildren().get(field); + assertThrows(UnsupportedOperationException.class, children::clear); + } + } + } + } + } + + private static List recoveryNodes() { + return List.of(site(), new IncompleteExpression(site()), + new IncompleteLocalDeclaration(site(), name("initializer"), 30), + new IncompleteDeclarationStatement(IncompleteDeclarationStatement.Kind.METHOD, + token("method"), 0, 30, List.of(site()), List.of(parameter("arg"))), + new IncompleteTypeCompositionStatement(new Source("module Copy {}"), token(Id.MODULE), + token("Copy"), null, new StatementBlock(List.of(site())), 0, 30, + List.of(site()), List.of(parameter("typeArg")))); + } + + private static void assertTreeCopy(AstNode original, AstNode copy, AstNode parent) { + assertNotSame(original, copy); + assertEquals(original.getClass(), copy.getClass()); + assertEquals(original.getStartPosition(), copy.getStartPosition()); + assertEquals(original.getEndPosition(), copy.getEndPosition()); + assertEquals(original.getStage(), copy.getStage()); + assertSame(parent, copy.getParent()); + var originalChildren = childrenWithoutAdoption(original); + var copiedChildren = childrenWithoutAdoption(copy); + assertEquals(originalChildren.size(), copiedChildren.size()); + for (int index = 0; index < originalChildren.size(); ++index) { + assertSame(original, originalChildren.get(index).getParent()); + assertTreeCopy(originalChildren.get(index), copiedChildren.get(index), copy); + } + } + + private static List childrenWithoutAdoption(AstNode node) { + // The normal child iterator adopts nodes; using it would hide broken copy parentage. + return node.getDumpChildren().values().stream().flatMap(value -> switch (value) { + case AstNode child -> Stream.of(child); + case List children -> children.stream().map(AstNode.class::cast); + case null, default -> Stream.empty(); + }).toList(); + } + + private static IncompleteStatement site() { + return new IncompleteStatement(token("prefix"), 20, Parser.INCOMPLETE_EXPRESSION); + } + + private static Parameter parameter(String name) { + var type = new NamedTypeExpression(null, List.of(token("String")), null, null, null, 6); + return new Parameter(type, token(name)); + } + + private static NameExpression name(String name) { + return new NameExpression(token(name)); + } + + private static Token token(String name) { + return new Token(0, name.length(), Id.IDENTIFIER, name); + } + + private static Token token(Id id) { + return new Token(0, 1, id); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java b/javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java new file mode 100644 index 00000000000..a5290627530 --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java @@ -0,0 +1,233 @@ +package org.xvm.compiler.ast; + +import java.util.List; +import java.util.function.UnaryOperator; + +import org.junit.jupiter.api.Test; + +import org.xvm.compiler.Parser; +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.partial.IncompleteExpression; +import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.partial.PartialSyntax; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Syntax searches must preserve scope boundaries and leave both source and trial ownership intact. */ +public class PartialSyntaxTest { + @Test + public void deferredAnonymousBodiesAreVisibleToSyntaxWithoutBecomingCompilerChildren() { + var body = new StatementBlock(List.of(new ExpressionStatement(hole(20)))); + var creation = new NewExpression(null, token(Id.NEW), null, List.of(), -1, body, 40); + var originalParent = body.getParent(); + assertSame(body, creation.getUnregisteredBody().orElseThrow()); + assertEquals(List.of(body), PartialSyntax.children(creation).toList()); + assertFalse(creation.children().hasNext()); + assertTrue(PartialSyntax.containsAt(creation, 20)); + assertSame(originalParent, body.getParent()); + for (NewExpression copy : List.of(creation.copyTree(), (NewExpression) creation.clone())) { + assertNotSame(body, copy.getUnregisteredBody().orElseThrow()); + assertTrue(PartialSyntax.containsAt(copy, 20)); + } + assertTrue(PartialSyntax.valueCursor(creation).isEmpty()); + } + + @Test + public void containmentIncludesTheRootAndNestedSitesAtTheirExactCursor() { + var value = hole(20); + var call = call(value); + + assertFalse(PartialSyntax.contains(null)); + assertFalse(PartialSyntax.containsAt(null, 20)); + assertFalse(PartialSyntax.contains(name("complete"))); + assertTrue(PartialSyntax.contains(value.getSite())); + assertTrue(PartialSyntax.containsAt(value.getSite(), 20)); + assertTrue(PartialSyntax.containsAt(call, 20)); + assertTrue(PartialSyntax.containsAt(call, call.getEndPosition())); + assertFalse(PartialSyntax.containsAt(call, 19)); + assertFalse(PartialSyntax.containsAt(call, 21)); + } + + @Test + public void argumentSelectionPreservesTheSlotThroughLabelsGroupsAndCompoundValues() { + List.>of( + UnaryOperator.identity(), + value -> new LabeledExpression(token("second"), value), + value -> new ParenthesizedExpression(value, 10, 30), + value -> new LabeledExpression(token("second"), new ParenthesizedExpression( + new RelOpExpression(name("left"), token(Id.ADD), value), 10, 30))) + .forEach(wrap -> { + var value = hole(20); + var argument = wrap.apply(value); + var call = call(argument); + var before = call.toDumpString(); + var selected = PartialSyntax.argument(call).orElseThrow(); + + assertEquals(1, selected.index()); + assertSame(value.getSite(), selected.cursor()); + assertSame(selected.cursor(), PartialSyntax.valueCursor(argument).orElseThrow()); + assertSame(call, selected.cursor().getArgumentCall().orElseThrow()); + assertSame(call, argument.getParent()); + assertSame(value, selected.cursor().getParent()); + assertEquals(selected, PartialSyntax.argument(call).orElseThrow()); + assertEquals(before, call.toDumpString()); + assertFalse(value.isValidated()); + }); + } + + @Test + public void containmentCrossesBoundariesThatArgumentSelectionMustNotCross() { + List.>of( + value -> new InvocationExpression(name("nested"), false, List.of(value), 40), + value -> new NewExpression(null, token(Id.NEW), null, List.of(value), -1, null, 40), + value -> new LambdaExpression(List.of(), token(Id.LAMBDA), + new StatementBlock(List.of(new ExpressionStatement(value))), 10)) + .forEach(wrap -> { + var value = hole(20); + var boundary = wrap.apply(value); + var call = call(boundary); + + assertTrue(PartialSyntax.contains(boundary)); + assertTrue(PartialSyntax.containsAt(call, 20)); + assertTrue(PartialSyntax.valueCursor(boundary).isEmpty()); + assertTrue(PartialSyntax.argument(call).isEmpty()); + assertTrue(value.getSite().getArgumentCall().isEmpty()); + }); + } + + @Test + public void aNestedIncompleteCallOwnsItsArgumentCursor() { + var value = hole(20); + var inner = call(value); + var outer = call(new IncompleteExpression(inner)); + + assertTrue(PartialSyntax.containsAt(outer, 20)); + assertTrue(PartialSyntax.argument(outer).isEmpty()); + assertSame(inner, value.getSite().getArgumentCall().orElseThrow()); + assertTrue(inner.getArgumentCall().isEmpty()); + } + + @Test + public void multipleValueCursorsInOneArgumentDoNotInventAnArgumentBinding() { + var left = hole(20); + var right = hole(30); + var ambiguous = new RelOpExpression(left, token(Id.ADD), right); + var call = call(ambiguous); + + assertTrue(PartialSyntax.containsAt(call, 20)); + assertTrue(PartialSyntax.containsAt(call, 30)); + assertTrue(PartialSyntax.valueCursor(ambiguous).isEmpty()); + assertTrue(PartialSyntax.argument(call).isEmpty()); + assertTrue(left.getSite().getArgumentCall().isEmpty()); + assertTrue(right.getSite().getArgumentCall().isEmpty()); + } + + @Test + public void dimensionsAreNotOrdinaryArgumentSlots() { + var value = hole(20); + var creation = new NewExpression(null, token(Id.NEW), null, List.of(), 0, null, 40); + var dimensions = new IncompleteStatement(creation, token(Id.L_SQUARE), + List.of(value), List.of(), 40); + dimensions.introduceParentage(); + + assertTrue(PartialSyntax.containsAt(dimensions, 20)); + assertTrue(PartialSyntax.argument(dimensions).isEmpty()); + assertTrue(value.getSite().getArgumentCall().isEmpty()); + } + + @Test + public void clonedCallsSelectTheirOwnCursorAndKeepTheOriginalBinding() { + var value = hole(20); + var original = call(new ParenthesizedExpression(value, 10, 30)); + var clone = (IncompleteStatement) original.clone(); + var selected = PartialSyntax.argument(clone).orElseThrow(); + + assertNotSame(value.getSite(), selected.cursor()); + assertSame(clone, selected.cursor().getArgumentCall().orElseThrow()); + assertSame(original, value.getSite().getArgumentCall().orElseThrow()); + assertSame(value.getSite(), PartialSyntax.argument(original).orElseThrow().cursor()); + assertTrue(PartialSyntax.containsAt(clone, 20)); + assertEquals(original.toDumpString(), clone.toDumpString()); + } + + @Test + public void trialSubstitutionPreservesOriginalSyntaxAndLaterArguments() { + var value = hole(20); + var compound = new RelOpExpression(name("left"), token(Id.ADD), value); + var label = new LabeledExpression(token("second"), new ParenthesizedExpression(compound, 10, 30)); + var call = call(label); + var selected = PartialSyntax.argument(call).orElseThrow(); + var before = call.toDumpString(); + var replacement = name("proposed"); + var proposed = PartialArgument.proposed(call, selected, replacement); + var trialLabel = assertInstanceOf(LabeledExpression.class, proposed.get(1)); + var trialGroup = assertInstanceOf(ParenthesizedExpression.class, trialLabel.getUnderlyingExpression()); + var trialValue = assertInstanceOf(RelOpExpression.class, trialGroup.getUnderlyingExpression()); + + assertSame(call.getArguments().getFirst(), proposed.getFirst()); + assertSame(call.getArguments().getLast(), proposed.getLast()); + assertNotSame(label, trialLabel); + assertEquals("second", trialLabel.getName()); + assertSame(call, trialLabel.getParent()); + assertSame(trialLabel, trialGroup.getParent()); + assertSame(trialGroup, trialValue.getParent()); + assertSame(trialValue, replacement.getParent()); + assertFalse(PartialSyntax.contains(trialLabel)); + assertSame(compound, value.getParent()); + assertSame(call, selected.cursor().getArgumentCall().orElseThrow()); + assertEquals(before, call.toDumpString()); + } + + @Test + public void unboundTrialsKeepTheWrittenLabelAndDoNotReplaceTheSourceSlot() { + var value = hole(20); + var label = new LabeledExpression(token("second"), value); + var call = call(label); + var selected = PartialSyntax.argument(call).orElseThrow(); + var unbound = PartialArgument.unbound(call, selected); + var trial = assertInstanceOf(LabeledExpression.class, unbound.get(1)); + var marker = assertInstanceOf(NonBindingExpression.class, trial.getUnderlyingExpression()); + + assertEquals(label.getName(), trial.getName()); + assertEquals(label.getStartPosition(), marker.getStartPosition()); + assertEquals(label.getEndPosition(), marker.getEndPosition()); + assertSame(call, trial.getParent()); + assertSame(trial, marker.getParent()); + assertSame(label, call.getArguments().get(1)); + assertSame(label, value.getParent()); + assertSame(call.getArguments().getFirst(), unbound.getFirst()); + assertSame(call.getArguments().getLast(), unbound.getLast()); + assertSame(selected.cursor(), PartialSyntax.argument(call).orElseThrow().cursor()); + } + + private static IncompleteStatement call(Expression argument) { + var call = new IncompleteStatement(name("work"), token(Id.L_PAREN), + List.of(name("first"), argument, name("last")), + List.of(token(Id.COMMA), token(Id.COMMA)), 100); + call.introduceParentage(); + return call; + } + + private static IncompleteExpression hole(long cursor) { + return new IncompleteExpression(new IncompleteStatement( + new Token(cursor - 2, cursor, Id.IDENTIFIER, "pr"), cursor, Parser.INCOMPLETE_EXPRESSION)); + } + + private static NameExpression name(String name) { + return new NameExpression(token(name)); + } + + private static Token token(String name) { + return new Token(0, name.length(), Id.IDENTIFIER, name); + } + + private static Token token(Id id) { + return new Token(0, 1, id, null); + } +} diff --git a/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java b/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java new file mode 100644 index 00000000000..c7a614086bc --- /dev/null +++ b/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java @@ -0,0 +1,143 @@ +package org.xvm.compiler.ast.partial; + +import java.lang.reflect.Field; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.StreamSupport; + +import org.junit.jupiter.api.Test; + +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure.Code; + +import org.xvm.compiler.Token; +import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.Statement; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** Exercise actual AST operations on private registered fields outside the base AST package. */ +public class RegisteredChildFieldsTest { + @Test + public void traversalAndDumpIncludeInheritedAndDeclaredFieldsOnly() { + var node = new Child(name("receiver"), List.of(name("argument"))); + var children = snapshot(node); + + assertEquals(List.of("receiver", "argument"), children.stream().map(Object::toString).toList()); + children.forEach(child -> assertSame(node, child.getParent())); + assertEquals(children, snapshot(node)); + assertEquals(List.of("receiver", "arguments"), List.copyOf(node.getDumpChildren().keySet())); + assertTrue(node.toDumpString().contains("receiver")); + assertTrue(node.toDumpString().contains("arguments")); + assertFalse(node.toDumpString().contains("unregistered")); + } + + @Test + public void cloneOwnsIndependentChildrenAndMutableLists() { + var node = new Child(name("receiver"), List.of(name("argument"))); + var copy = (Child) node.clone(); + var original = snapshot(node); + var cloned = snapshot(copy); + + assertNotSame(original.getFirst(), cloned.getFirst()); + assertNotSame(original.getLast(), cloned.getLast()); + cloned.forEach(child -> assertSame(copy, child.getParent())); + original.forEach(child -> assertSame(node, child.getParent())); + assertEquals(node.toDumpString(), copy.toDumpString()); + + var replacement = name("replacement"); + copy.replaceChild(cloned.getLast(), replacement); + assertSame(copy, replacement.getParent()); + assertEquals(original, snapshot(node)); + assertEquals(List.of(cloned.getFirst(), replacement), snapshot(copy)); + } + + @Test + public void replacementAndRemovalWorkForInheritedScalarAndDeclaredList() { + var node = new Child(name("receiver"), List.of(name("first"), name("last"))); + var original = snapshot(node); + var receiver = name("newReceiver"); + var argument = name("newArgument"); + node.replaceChild(original.getFirst(), receiver); + node.replaceChild(original.get(1), argument); + assertSame(node, receiver.getParent()); + assertSame(node, argument.getParent()); + + var cursor = node.children(); + assertSame(receiver, cursor.next()); + cursor.remove(); + assertSame(argument, cursor.next()); + cursor.remove(); + assertSame(original.getLast(), cursor.next()); + assertFalse(cursor.hasNext()); + assertEquals(List.of(original.getLast()), snapshot(node)); + } + + private static List snapshot(AstNode node) { + return StreamSupport.stream(node.childNodes().spliterator(), false).toList(); + } + + private static NameExpression name(String name) { + return new NameExpression(new Token(0, name.length(), Id.IDENTIFIER, name)); + } + + private abstract static class Parent extends Statement { + Parent(Expression receiver) { + this.receiver = adopt(receiver); + } + + @Override + public long getStartPosition() { + return 0; + } + + @Override + public long getEndPosition() { + return 1; + } + + @Override + protected Statement validateImpl(Context ctx, ErrorListener errs) { + return this; + } + + @Override + protected boolean emit(Context ctx, boolean reachable, Code code, ErrorListener errs) { + throw new UnsupportedOperationException("Traversal fixture cannot emit code"); + } + + @Override + public String toString() { + return "registered children"; + } + + private Expression receiver; + } + + private static final class Child extends Parent { + Child(Expression receiver, List arguments) { + super(receiver); + this.arguments = new ArrayList<>(arguments); + adopt(this.arguments); + } + + @Override + protected Field[] getChildFields() { + return CHILD_FIELDS; + } + + private List arguments; + private final Expression unregistered = name("unregistered"); + + private static final Field[] CHILD_FIELDS = fieldsForNames(Child.class, "receiver", "arguments"); + } +} diff --git a/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java b/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java index 3edbf6d98d2..bdda1611e13 100644 --- a/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java +++ b/javatools/src/test/java/org/xvm/tool/ModuleInfoTest.java @@ -30,6 +30,25 @@ class ModuleInfoTest { @TempDir Path tempDir; + @Test + void testHostSnapshotParsesBeforeItsDirectoryExists() { + var root = tempDir.resolve("not-created/Virtual.x").toFile(); + var info = new ModuleInfo(root, "Virtual.example.org") { + @Override + protected char[] readSource(File file) { + assertEquals(root, file); + return "module Virtual.example.org {}".toCharArray(); + } + }; + assertEquals("Virtual.example.org", info.getQualifiedModuleName()); + assertEquals("Virtual", info.getFileName()); + var errors = new ErrorList(); + assertNotNull(info.getSourceTree(errors)); + assertFalse(errors.hasSeriousErrors(), errors.getErrors().toString()); + assertFalse(root.getParentFile().exists(), "source proof must not create a destination"); + assertThrows(IllegalArgumentException.class, () -> new ModuleInfo(root, false)); + } + // ----- Helper methods ------------------------------------------------------------------------ /** @@ -627,7 +646,7 @@ void testGetSourceTreeRequiresErrorListener() throws IOException { void testGetSourceTreeWithSingleFileModule() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertFalse(errs.hasSeriousErrors()); @@ -644,7 +663,7 @@ void testGetSourceTreeWithSingleFileModule() throws IOException { void testGetSourceTreeWithMultiFileModule() throws IOException { var sourceFile = createModuleSourceTree("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertFalse(errs.hasSeriousErrors()); @@ -660,7 +679,7 @@ void testGetSourceTreeWithMultiFileModule() throws IOException { void testGetSourceTreeCachesResult() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node1 = info.getSourceTree(errs); var node2 = info.getSourceTree(errs); assertSame(node1, node2); @@ -673,7 +692,7 @@ void testGetSourceTreeCachesResult() throws IOException { void testGetSourceTreeNodeMethods() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertEquals(0, node.depth()); @@ -696,7 +715,7 @@ void testGetSourceTreeWithNestedPackages() throws IOException { Files.writeString(utilsDir.resolve("Util.x"), "class Util {}"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertFalse(errs.hasSeriousErrors()); @@ -712,7 +731,7 @@ void testGetSourceTreeWithParseErrors() throws IOException { var sourceFile = tempDir.resolve("MyModule.x"); Files.writeString(sourceFile, "module MyModule {"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNull(node); assertTrue(errs.hasSeriousErrors()); @@ -725,7 +744,7 @@ void testGetSourceTreeWithParseErrors() throws IOException { void testGetSourceTreeModuleNode() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); var moduleNode = node.module(); @@ -740,7 +759,7 @@ void testGetSourceTreeModuleNode() throws IOException { void testGetSourceTreeDescriptiveName() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertTrue(node.descriptiveName().contains("MyModule")); @@ -753,7 +772,7 @@ void testGetSourceTreeDescriptiveName() throws IOException { void testGetSourceTreeResourceDir() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertNotNull(node.resourceDir()); @@ -772,7 +791,7 @@ void testDirNodeWithPackageAndClasses() throws IOException { Files.writeString(moduleDir.resolve("MyClass.x"), "class MyClass {}"); Files.writeString(subPkgDir.resolve("SubClass.x"), "class SubClass {}"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); assertFalse(errs.hasSeriousErrors()); @@ -789,7 +808,7 @@ void testDirNodeWithPackageAndClasses() throws IOException { void testFileNodeDepth() throws IOException { var sourceFile = createModuleSourceTree("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1099,7 +1118,7 @@ void testGetSourceTreeWithExplicitResources() throws IOException { var info = new ModuleInfo(sourceFile.toFile(), false, List.of(resourceDir.toFile()), null); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); @@ -1287,7 +1306,7 @@ void testFileNodeResolveResourceRelativePath() throws IOException { Files.writeString(resDir.resolve("config.txt"), "config content"); var info = new ModuleInfo(sourceFile.toFile(), true); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1311,7 +1330,7 @@ void testFileNodeResolveResourceDirectoryPath() throws IOException { Files.writeString(subDir.resolve("file.txt"), "content"); var info = new ModuleInfo(sourceFile.toFile(), true); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1330,7 +1349,7 @@ void testFileNodeResolveResourceNonExistent() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1350,7 +1369,7 @@ void testFileNodeSourceReturnsSource() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1370,7 +1389,7 @@ void testFileNodeContentReturnsCharArray() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1393,7 +1412,7 @@ void testFileNodeIsPackageSourceForRootModule() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1415,7 +1434,7 @@ void testDirNodeChildrenContainsRegisteredNodes() throws IOException { Files.writeString(moduleDir.resolve("Helper.x"), "class Helper {}"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1437,7 +1456,7 @@ void testFileNodeToString() throws IOException { var sourceFile = createModuleSource("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1454,7 +1473,7 @@ void testDirNodeToString() throws IOException { var sourceFile = createModuleSourceTree("MyModule"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); assertNotNull(node); @@ -1475,7 +1494,7 @@ void testNodeErrorListenerAccumulatesErrors() throws IOException { Files.writeString(sourceFile, "module BadModule { invalid syntax here"); var info = new ModuleInfo(sourceFile.toFile(), false); - var errs = new ErrorList(100); + var errs = new ErrorList(); var node = info.getSourceTree(errs); diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt new file mode 100644 index 00000000000..1ad207b5818 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt @@ -0,0 +1,80 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Source +import org.xvm.tool.ModuleInfo +import java.nio.file.Path + +class CompilerDeclarationAnalysisTest { + @TempDir lateinit var directory: Path + + @Test + fun `fresh declaration analysis does not validate bodies or masquerade as compilation`() { + CompilerTestSupport.configure() + val text = "module Headers { String broken() = 1; }" + val errors = ErrorList() + val headers = + EmbeddingSupport.instance().analyzeDeclarations(Source(text, "Headers.x"), null, errors) + assertThat(headers).isPresent() + assertThat(errors.hasSeriousErrors()).isFalse() + val normal = ErrorList() + assertThat( + EmbeddingSupport + .instance() + .compileModule(Source(text, "Headers.x"), null, normal) + .succeeded(), + ).isFalse() + assertThat(normal.hasSeriousErrors()).isTrue() + } + + @Test + fun `declaration and syntax failures prevent exposing a queryable graph`() { + CompilerTestSupport.configure() + listOf("module Headers { Missing field; }", "module Headers {").forEach { text -> + val errors = ErrorList() + assertThat( + EmbeddingSupport + .instance() + .analyzeDeclarations(Source(text, "Headers.x"), null, errors), + ).isEmpty() + assertThat(errors.hasSeriousErrors()).isTrue() + } + } + + @Test + fun `declaration analysis obeys cancellation before parsing`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val cancelled = ErrorListener.cancellable(errors) { true } + assertThat( + EmbeddingSupport + .instance() + .analyzeDeclarations(Source("broken", "Headers.x"), null, cancelled), + ).isEmpty() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `module tree declaration analysis retains member source identity`() { + CompilerTestSupport.configure() + val root = directory.resolve("Headers.x").toFile().apply { writeText("module Headers {}") } + val member = + directory.resolve("Headers/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box { String broken() = 1; }") + } + val errors = ErrorList() + val headers = + EmbeddingSupport + .instance() + .analyzeDeclarations(ModuleInfo(root, false), null, errors) + .orElseThrow() + assertThat(errors.hasSeriousErrors()).isFalse() + assertThat(headers.sourceTrees().map { it.source.fileName }).contains(member.canonicalPath) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPartialFactsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPartialFactsTest.kt new file mode 100644 index 00000000000..a4085adc273 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPartialFactsTest.kt @@ -0,0 +1,69 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Source + +/** Direct compiler consumers: no production LSP adapter is present in this branch. */ +class CompilerPartialFactsTest { + @Test + fun `cursor queries retain readable local types`() { + val analysis = analyze("module Editing { void run(String text, Int number) { tex|; } }") + val binding = analysis.cursorBindings().getValue(analysis.sites().single()) + assertThat(binding.variables().filter { it.readable() }.map { it.name() }) + .contains("text", "number") + assertThat(binding.variables().single { it.name() == "text" }.type()) + .isEqualTo(requireNotNull(analysis.pool()).typeString()) + } + + @Test + fun `incomplete calls expose candidates without claiming a selected call`() { + val analysis = analyze("module Editing { String echo(String value) = value; void run() { echo(") + val binding = analysis.cursorBindings().getValue(analysis.sites().single()) + assertThat(binding.callsInspected()).isTrue() + assertThat(binding.candidates().map { it.method().name }).contains("echo") + assertThat(analysis.callBindings().values.map { it.method().name }).doesNotContain("echo") + } + + @Test + fun `incomplete construction exposes compiler fitted constructors`() { + val analysis = analyze("module Editing { class Box(String text) {} void run() { new Box(") + val binding = analysis.cursorBindings().getValue(analysis.sites().single()) + assertThat(binding.callsInspected()).isTrue() + assertThat(binding.candidates().map { it.method().name }).contains("construct") + } + + @Test + fun `cancellation produces no partial graph or candidates`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport.instance().analyzeIncomplete( + Source("module Editing { void run() {", "Editing.x"), + null, + ErrorListener.cancellable(errors) { true }, + ) + assertThat(result.sourceTrees()).isEmpty() + assertThat(result.cursorBindings()).isEmpty() + assertThat(errors.errors).isEmpty() + } + + private fun analyze(text: String): EmbeddingSupport.PartialAnalysis { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + if ("|" in text) { + val prefix = Source(text.substringBefore("|")) + while (prefix.hasNext()) prefix.next() + EmbeddingSupport.instance().analyzeIncomplete(Source(text.replace("|", ""), "Editing.x"), prefix.position, null, errors) + } else { + EmbeddingSupport.instance().analyzeIncomplete(Source(text, "Editing.x"), null, errors) + } + assertThat(result.pool()).describedAs(errors.errors.toString()).isNotNull() + assertThat(result.sites()).describedAs(errors.errors.toString()).hasSize(1) + return result + } +} From 9bb1c8712bf1fc410155e4bbfa2bb1ff28e3cd57 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:26 +0100 Subject: [PATCH 008/324] Integrate compiler-backed language support with IntelliJ and VS Code - Integrate compiler-backed language support with IntelliJ and VS Code. - Allow metaspace for complete language-tooling builds. - Prepare product documentation for the complete LSP reference. - Include language-tooling flags in the temporary build example. The integration's compiler tests check compiled code without the binary AST, which master removed. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .github/workflows/commit.yml | 36 +- .../src/main/kotlin/IntellijRunIdeSupport.kt | 107 +- .../src/main/kotlin/PruneTestReportsTask.kt | 86 + .../src/test/kotlin/ServerLogTailsTest.kt | 51 + .../test/kotlin/TestReportRetentionTest.kt | 70 + gradle.properties | 25 +- gradle/libs.versions.toml | 18 +- lang/README.md | 40 +- lang/build.gradle.kts | 103 +- lang/doc/manual-test-plan.md | 2900 +++++- .../org/xtclang/tooling/LanguageModelCli.kt | 8 +- .../kotlin/org/xtclang/tooling/XtcLanguage.kt | 76 +- .../tooling/generators/EmacsGenerator.kt | 24 +- .../tooling/generators/GeneratorUtils.kt | 39 +- .../generators/SublimeSyntaxGenerator.kt | 7 +- .../TextMateBundleManifestGenerator.kt | 3 +- .../tooling/generators/TextMateGenerator.kt | 60 +- .../tooling/generators/TreeSitterGenerator.kt | 43 +- .../tooling/generators/VimGenerator.kt | 28 +- .../xtclang/tooling/model/LanguageModel.kt | 230 +- .../org/xtclang/tooling/scanner/CCodeDsl.kt | 15 +- .../tooling/scanner/ScannerCGeneratorDsl.kt | 142 +- .../xtclang/tooling/scanner/ScannerSpec.kt | 15 +- .../xtclang/tooling/DslPowerShowcaseTest.kt | 5 +- .../org/xtclang/tooling/LanguageModelTest.kt | 41 +- lang/generated-examples/xtc.tmLanguage.json | 2 +- lang/intellij-plugin/README.md | 216 +- lang/intellij-plugin/TESTING.md | 176 +- lang/intellij-plugin/build.gradle.kts | 130 +- .../idea/playbook/CachedIdeInstaller.kt | 55 + .../idea/playbook/ClientDiagnostics.kt | 151 + .../xtclang/idea/playbook/ClientProtocol.kt | 216 + .../org/xtclang/idea/playbook/ClientTrace.kt | 195 + .../xtclang/idea/playbook/CompilerPlaybook.kt | 1989 ++++ .../idea/playbook/CompilerPlaybookTest.kt | 385 + .../idea/playbook/CompilerSettingsPage.kt | 27 + .../idea/playbook/CompletionActions.kt | 219 + .../idea/playbook/DiagnosticProbePlugin.kt | 47 + .../org/xtclang/idea/playbook/EditorFocus.kt | 180 + .../xtclang/idea/playbook/FocusRecovery.kt | 120 + .../org/xtclang/idea/playbook/IdeServices.kt | 148 + .../idea/playbook/LanguageServicePage.kt | 49 + .../xtclang/idea/playbook/LargeFileEditing.kt | 92 + .../idea/playbook/LocalLsp4ijPlugin.kt | 57 + .../idea/playbook/MemberActionScenarios.kt | 83 + .../idea/playbook/NativeDiagnostics.kt | 33 + .../idea/playbook/NativeSemanticViews.kt | 312 + .../idea/playbook/NavigationChooserTest.kt | 69 + .../org/xtclang/idea/playbook/ParityColors.kt | 157 + .../idea/playbook/ParityCompilerImports.kt | 315 + .../idea/playbook/ParityDependencies.kt | 150 + .../idea/playbook/ParityDocumentation.kt | 37 + .../org/xtclang/idea/playbook/ParityGraph.kt | 313 + .../idea/playbook/ParityHighlighting.kt | 147 + .../idea/playbook/ParityInlineCompletion.kt | 96 + .../idea/playbook/ParityLibrarySettings.kt | 131 + .../xtclang/idea/playbook/ParityModules.kt | 271 + .../xtclang/idea/playbook/ParityNavigation.kt | 329 + .../xtclang/idea/playbook/ParityPlatform.kt | 1225 +++ .../xtclang/idea/playbook/ParityProgress.kt | 123 + .../idea/playbook/ParityReferenceLenses.kt | 77 + .../idea/playbook/ParityReliability.kt | 285 + .../org/xtclang/idea/playbook/ParityRename.kt | 215 + .../idea/playbook/ParityRuntimeSettings.kt | 178 + .../xtclang/idea/playbook/ParityScenarios.kt | 56 + .../xtclang/idea/playbook/ParitySemantics.kt | 348 + .../xtclang/idea/playbook/ParityTypeMoves.kt | 205 + .../xtclang/idea/playbook/ParityWorkspace.kt | 514 + .../xtclang/idea/playbook/PlaybookFailure.kt | 15 + .../idea/playbook/PlaybookFailureTest.kt | 34 + .../xtclang/idea/playbook/PlaybookProgress.kt | 22 + .../xtclang/idea/playbook/ProjectLifecycle.kt | 208 + .../idea/playbook/RefactoringActions.kt | 296 + .../xtclang/idea/playbook/ReferenceActions.kt | 139 + .../idea/playbook/RenameFamilyScenarios.kt | 143 + .../idea/playbook/SettingsPersistence.kt | 89 + .../xtclang/idea/playbook/SharedScenarios.kt | 264 + .../xtclang/idea/playbook/SignatureHints.kt | 143 + .../xtclang/idea/playbook/StartupEditing.kt | 144 + .../xtclang/idea/playbook/StructureViews.kt | 175 + .../org/xtclang/idea/playbook/UiWaits.kt | 62 + .../org/xtclang/idea/playbook/UiWaitsTest.kt | 71 + .../idea/playbook/UpstreamReplacementGates.kt | 192 + .../xtclang/idea/playbook/WorkspaceFolders.kt | 50 + .../xtclang/idea/playbook/probe/CodeLensUi.kt | 48 + .../xtclang/idea/playbook/probe/ColorUi.kt | 165 + .../idea/playbook/probe/CompilerImportPage.kt | 103 + .../playbook/probe/CompilerLibrariesPage.kt | 114 + .../idea/playbook/probe/CompilerReportPage.kt | 87 + .../playbook/probe/CompilerSettingsPage.kt | 173 + .../idea/playbook/probe/EditorDiagnostics.kt | 33 + .../xtclang/idea/playbook/probe/EditorUi.kt | 154 + .../idea/playbook/probe/FileTreeOperations.kt | 116 + .../idea/playbook/probe/HighlightingUi.kt | 34 + .../idea/playbook/probe/InlineCompletionUi.kt | 30 + .../playbook/probe/LanguageServicePage.kt | 182 + .../idea/playbook/probe/LargeFileProbe.kt | 112 + .../idea/playbook/probe/NavigationChooser.kt | 36 + .../idea/playbook/probe/PartialResults.kt | 57 + .../idea/playbook/probe/PlaybookProgress.kt | 84 + .../xtclang/idea/playbook/probe/ProgressUi.kt | 68 + .../idea/playbook/probe/ProjectLifecycle.kt | 96 + .../idea/playbook/probe/RefreshRequests.kt | 122 + .../idea/playbook/probe/ServerRuntimePage.kt | 96 + .../playbook/probe/SettingsPersistencePage.kt | 56 + .../idea/playbook/probe/StartupEdits.kt | 86 + .../idea/playbook/probe/WorkspaceEdits.kt | 29 + .../kotlin/org/xtclang/idea/PluginPaths.kt | 46 +- .../kotlin/org/xtclang/idea/XtcCommenter.kt | 22 +- .../kotlin/org/xtclang/idea/XtcFileType.kt | 8 +- .../org/xtclang/idea/XtcIconProvider.kt | 7 +- .../org/xtclang/idea/XtcIntelliJLanguage.kt | 17 +- .../xtclang/idea/XtcTextMateBundleProvider.kt | 24 +- .../idea/dap/XtcDebugAdapterFactory.kt | 42 +- .../idea/editor/XtcEditorStartupActivity.kt | 8 +- .../idea/editor/XtcEnterHandlerDelegate.kt | 22 +- .../idea/lsp/ClosedRefactoringDocuments.kt | 81 + .../xtclang/idea/lsp/CodeActionMessages.kt | 28 + .../xtclang/idea/lsp/CompilerBuildModel.kt | 310 + .../xtclang/idea/lsp/CompilerBuildUpdates.kt | 73 + .../org/xtclang/idea/lsp/CompilerImport.kt | 143 + .../xtclang/idea/lsp/CompilerImportService.kt | 57 + .../idea/lsp/CompilerLibrariesPanel.kt | 108 + .../idea/lsp/CompilerProjectConfigurable.kt | 476 + .../xtclang/idea/lsp/CompilerRootWatches.kt | 199 + .../org/xtclang/idea/lsp/CompilerSettings.kt | 33 + .../idea/lsp/CompilerWorkspaceModels.kt | 77 + .../org/xtclang/idea/lsp/ConfigurationJson.kt | 39 + .../xtclang/idea/lsp/ConnectionLifetime.kt | 10 +- .../xtclang/idea/lsp/DiagnosticQuickFixes.kt | 74 + .../xtclang/idea/lsp/DiagnosticReportJson.kt | 40 + .../idea/lsp/DiagnosticResultMessages.kt | 110 + .../idea/lsp/DirectoryDocumentMoves.kt | 101 + .../idea/lsp/DocumentStartupMessages.kt | 275 + .../org/xtclang/idea/lsp/EditorRefresh.kt | 90 + .../idea/lsp/ExportServerLogsAction.kt | 75 + .../org/xtclang/idea/lsp/FileMoveTargets.kt | 41 + .../org/xtclang/idea/lsp/LanguageAdapter.kt | 23 + .../idea/lsp/LanguageServiceConfigurable.kt | 212 + .../idea/lsp/LanguageServiceConfiguration.kt | 144 + .../idea/lsp/LanguageServicePreferences.kt | 24 + .../idea/lsp/LanguageServiceSettings.kt | 33 + .../xtclang/idea/lsp/LibraryConfiguration.kt | 110 + .../org/xtclang/idea/lsp/OrderedPaths.kt | 103 + .../xtclang/idea/lsp/PreflightedRenames.kt | 69 + .../idea/lsp/SelectLanguageAdapterAction.kt | 32 + .../org/xtclang/idea/lsp/ServerJvmOptions.kt | 54 + .../idea/lsp/ServerRuntimeConfigurable.kt | 107 + .../xtclang/idea/lsp/ServerRuntimeSettings.kt | 62 + .../org/xtclang/idea/lsp/ServerSupportLogs.kt | 159 + .../xtclang/idea/lsp/ServerWorkspaceEdit.kt | 209 + .../idea/lsp/SourceGraphConfiguration.kt | 185 + .../org/xtclang/idea/lsp/SourceGraphEdit.kt | 126 + .../org/xtclang/idea/lsp/XtcClientFeatures.kt | 158 + .../xtclang/idea/lsp/XtcFileMoveHandler.kt | 150 + .../org/xtclang/idea/lsp/XtcFileOperations.kt | 78 + .../xtclang/idea/lsp/XtcFileRenameHandler.kt | 99 + .../xtclang/idea/lsp/XtcFormattingService.kt | 138 + .../org/xtclang/idea/lsp/XtcLanguageClient.kt | 238 +- .../org/xtclang/idea/lsp/XtcLanguageServer.kt | 43 + .../idea/lsp/XtcLspServerSupportProvider.kt | 164 +- .../idea/lsp/XtcParameterInfoHandler.kt | 53 + .../org/xtclang/idea/lsp/XtcRenameEdit.kt | 358 + .../org/xtclang/idea/lsp/XtcRenameHandler.kt | 259 + .../xtclang/idea/lsp/XtcResolveCodeAction.kt | 24 + .../xtclang/idea/lsp/XtcServerLogAction.kt | 34 + .../idea/project/XtcNewProjectWizard.kt | 7 +- .../idea/project/XtcNewProjectWizardStep.kt | 22 +- .../xtclang/idea/run/XtcRunConfiguration.kt | 13 +- .../idea/run/XtcRunConfigurationProducer.kt | 8 +- .../idea/run/XtcRunConfigurationType.kt | 11 +- .../idea/style/XtcCodeStyleSettings.kt | 12 +- .../XtcLanguageCodeStyleSettingsProvider.kt | 8 +- .../src/main/resources/META-INF/plugin.xml | 65 +- .../src/main/resources/liveTemplates/XTC.xml | 2 +- .../idea/lsp/ClientCapabilitiesTest.kt | 96 + .../idea/lsp/CodeActionMessagesTest.kt | 42 + .../idea/lsp/CompilerBuildModelTest.kt | 65 + .../idea/lsp/CompilerBuildUpdatesTest.kt | 18 + .../idea/lsp/CompilerImportServiceTest.kt | 87 + .../xtclang/idea/lsp/CompilerImportTest.kt | 168 + .../idea/lsp/CompilerRootWatchesTest.kt | 92 + .../xtclang/idea/lsp/ConfigurationJsonTest.kt | 53 + .../idea/lsp/ConnectionLifetimeTest.kt | 19 +- .../xtclang/idea/lsp/ConnectionProcessTest.kt | 30 +- .../idea/lsp/DiagnosticQuickFixesTest.kt | 117 + .../idea/lsp/DiagnosticReportJsonTest.kt | 79 + .../idea/lsp/DiagnosticResultMessagesTest.kt | 210 + .../idea/lsp/DirectoryDocumentMovesTest.kt | 66 + .../idea/lsp/DocumentStartupMessagesTest.kt | 272 + .../xtclang/idea/lsp/FileMoveTargetsTest.kt | 61 + .../lsp/LanguageServiceConfigurationTest.kt | 94 + .../idea/lsp/LibraryConfigurationTest.kt | 49 + .../idea/lsp/LspServerJarResolutionTest.kt | 20 +- .../idea/lsp/PreflightedRenamesTest.kt | 152 + .../xtclang/idea/lsp/ServerJvmOptionsTest.kt | 40 + .../idea/lsp/ServerSettingsPersistenceTest.kt | 19 + .../xtclang/idea/lsp/ServerSupportLogsTest.kt | 67 + .../idea/lsp/ServerWorkspaceEditTest.kt | 29 + .../idea/lsp/SourceGraphConfigurationTest.kt | 219 + .../idea/manifest/BundledResourcesTest.kt | 28 +- .../manifest/LiveTemplateRegistrationTest.kt | 23 +- .../idea/manifest/PluginManifestTest.kt | 71 +- lang/lsp-server/README.md | 545 +- lang/lsp-server/build.gradle.kts | 159 +- lang/lsp-server/editor-setup.md | 10 +- .../org/xvm/lsp/adapter/AbstractAdapter.kt | 100 +- .../kotlin/org/xvm/lsp/adapter/Adapter.kt | 500 +- .../org/xvm/lsp/adapter/AdapterCapability.kt | 33 + .../org/xvm/lsp/adapter/AdapterCodeActions.kt | 81 +- .../org/xvm/lsp/adapter/AdapterFormatter.kt | 282 +- .../org/xvm/lsp/adapter/AdapterFutures.kt | 31 + .../org/xvm/lsp/adapter/AdapterModels.kt | 125 +- .../kotlin/org/xvm/lsp/adapter/AdapterTree.kt | 17 +- .../org/xvm/lsp/adapter/DocumentColor.kt | 24 + .../org/xvm/lsp/adapter/FormattingConfig.kt | 25 +- .../org/xvm/lsp/adapter/LanguageConstants.kt | 34 +- .../org/xvm/lsp/adapter/mock/MockAdapter.kt | 128 +- .../adapter/treesitter/TreeSitterAdapter.kt | 616 +- .../adapter/xdk/CompilerAnnotationIdentity.kt | 115 + .../adapter/xdk/CompilerCallableIdentity.kt | 66 + .../org/xvm/lsp/adapter/xdk/CompilerCalls.kt | 53 + .../lsp/adapter/xdk/CompilerDeclarations.kt | 49 + .../xvm/lsp/adapter/xdk/CompilerDispatch.kt | 183 + .../adapter/xdk/CompilerImplementations.kt | 231 + .../lsp/adapter/xdk/CompilerImportAliases.kt | 99 + .../lsp/adapter/xdk/CompilerMemberAction.kt | 174 + .../adapter/xdk/CompilerMemberSignature.kt | 235 + .../adapter/xdk/CompilerMethodRelations.kt | 60 + .../lsp/adapter/xdk/CompilerMissingMethod.kt | 591 ++ .../adapter/xdk/CompilerPrimaryParameters.kt | 19 + .../adapter/xdk/CompilerPropertyRelations.kt | 69 + .../xvm/lsp/adapter/xdk/CompilerQueueTrace.kt | 213 + .../adapter/xdk/CompilerReceiverIdentity.kt | 85 + .../lsp/adapter/xdk/CompilerRenameFacts.kt | 558 ++ .../lsp/adapter/xdk/CompilerResourceValues.kt | 95 + .../lsp/adapter/xdk/CompilerSourceLinks.kt | 46 + .../lsp/adapter/xdk/CompilerSourceTypes.kt | 35 + .../lsp/adapter/xdk/CompilerSourceUsage.kt | 63 + .../xvm/lsp/adapter/xdk/CompilerTypeNames.kt | 108 + .../xvm/lsp/adapter/xdk/LibraryDeclaration.kt | 70 + .../lsp/adapter/xdk/PartialSemanticModel.kt | 176 + .../org/xvm/lsp/adapter/xdk/SemanticModel.kt | 566 ++ .../lsp/adapter/xdk/SemanticModelBuilder.kt | 2089 ++++ .../org/xvm/lsp/adapter/xdk/XdkAdapter.kt | 2517 ++++- .../kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt | 179 + .../xvm/lsp/adapter/xdk/XdkAttachedSources.kt | 111 + .../org/xvm/lsp/adapter/xdk/XdkAutoImports.kt | 145 + .../org/xvm/lsp/adapter/xdk/XdkBuildModel.kt | 190 + .../org/xvm/lsp/adapter/xdk/XdkCalls.kt | 88 + .../xvm/lsp/adapter/xdk/XdkColorPrototype.kt | 179 + .../xvm/lsp/adapter/xdk/XdkCursorQueries.kt | 336 + .../org/xvm/lsp/adapter/xdk/XdkDependency.kt | 170 + .../xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt | 33 + .../org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt | 96 + .../xvm/lsp/adapter/xdk/XdkDocumentation.kt | 56 + .../org/xvm/lsp/adapter/xdk/XdkFileChanges.kt | 76 + .../org/xvm/lsp/adapter/xdk/XdkHierarchy.kt | 86 + .../org/xvm/lsp/adapter/xdk/XdkImports.kt | 108 + .../lsp/adapter/xdk/XdkInlineCompletions.kt | 52 + .../org/xvm/lsp/adapter/xdk/XdkLexical.kt | 331 + .../org/xvm/lsp/adapter/xdk/XdkLibraries.kt | 85 + .../xvm/lsp/adapter/xdk/XdkLibrarySources.kt | 196 + .../lsp/adapter/xdk/XdkLocalDeclarations.kt | 59 + .../xvm/lsp/adapter/xdk/XdkLocalExtraction.kt | 140 + .../org/xvm/lsp/adapter/xdk/XdkLocalInline.kt | 203 + .../xvm/lsp/adapter/xdk/XdkLocalRemoval.kt | 73 + .../xvm/lsp/adapter/xdk/XdkMemberActions.kt | 138 + .../xvm/lsp/adapter/xdk/XdkMemberInline.kt | 144 + .../lsp/adapter/xdk/XdkMethodExtraction.kt | 245 + .../lsp/adapter/xdk/XdkMissingDeclarations.kt | 162 + .../xvm/lsp/adapter/xdk/XdkMissingMethods.kt | 183 + .../org/xvm/lsp/adapter/xdk/XdkMonikers.kt | 132 + .../org/xvm/lsp/adapter/xdk/XdkMoveImports.kt | 37 + .../org/xvm/lsp/adapter/xdk/XdkMoveInputs.kt | 40 + .../xvm/lsp/adapter/xdk/XdkMoveOperations.kt | 30 + .../xvm/lsp/adapter/xdk/XdkNavigationIndex.kt | 53 + .../xvm/lsp/adapter/xdk/XdkPresentation.kt | 252 + .../xvm/lsp/adapter/xdk/XdkProjectQueries.kt | 1506 +++ .../xvm/lsp/adapter/xdk/XdkQualifiedName.kt | 87 + .../lsp/adapter/xdk/XdkRefactoringSyntax.kt | 53 + .../org/xvm/lsp/adapter/xdk/XdkRename.kt | 1229 +++ .../xvm/lsp/adapter/xdk/XdkRenameProposal.kt | 44 + .../xvm/lsp/adapter/xdk/XdkResourceMoves.kt | 80 + .../org/xvm/lsp/adapter/xdk/XdkResources.kt | 104 + .../org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt | 106 + .../xvm/lsp/adapter/xdk/XdkSourceModule.kt | 163 + .../org/xvm/lsp/adapter/xdk/XdkSourceMoves.kt | 104 + .../org/xvm/lsp/adapter/xdk/XdkSources.kt | 223 + .../org/xvm/lsp/adapter/xdk/XdkSymbols.kt | 168 + .../lsp/adapter/xdk/XdkSyntaxCompletions.kt | 506 + .../org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt | 229 + .../lsp/adapter/xdk/XdkWorkspaceDiscovery.kt | 167 + .../lsp/adapter/xdk/XdkWorkspaceNavigation.kt | 267 + .../kotlin/org/xvm/lsp/index/IndexedSymbol.kt | 12 +- .../org/xvm/lsp/index/WorkspaceIndex.kt | 45 +- .../org/xvm/lsp/index/WorkspaceIndexer.kt | 217 +- .../org/xvm/lsp/lexer/TemplateScanner.kt | 25 +- .../org/xvm/lsp/lexer/TemplateScannerToken.kt | 17 +- .../org/xvm/lsp/model/CompilationResult.kt | 14 +- .../kotlin/org/xvm/lsp/model/Diagnostic.kt | 10 +- .../main/kotlin/org/xvm/lsp/model/Location.kt | 4 +- .../org/xvm/lsp/model/Lsp4jConversions.kt | 17 +- .../kotlin/org/xvm/lsp/model/SymbolInfo.kt | 9 +- .../org/xvm/lsp/server/ClientPresentation.kt | 153 + .../org/xvm/lsp/server/ClientRefresh.kt | 88 + .../kotlin/org/xvm/lsp/server/ClientTrace.kt | 60 + .../org/xvm/lsp/server/CodeLensSettings.kt | 14 + .../xvm/lsp/server/CompilerConfiguration.kt | 126 + .../org/xvm/lsp/server/CompilerLibraries.kt | 143 + .../org/xvm/lsp/server/ConnectionProgress.kt | 236 + .../org/xvm/lsp/server/DiagnosticReports.kt | 171 + .../xvm/lsp/server/DocumentSynchronization.kt | 52 + .../kotlin/org/xvm/lsp/server/DocumentText.kt | 73 + .../xvm/lsp/server/EditorFormattingState.kt | 78 + .../org/xvm/lsp/server/FileChangeSnapshots.kt | 40 + .../org/xvm/lsp/server/LspJsonOptions.kt | 89 +- .../org/xvm/lsp/server/PartialResults.kt | 93 + .../org/xvm/lsp/server/ProtocolLifecycle.kt | 129 + .../org/xvm/lsp/server/ProtocolTrace.kt | 160 + .../org/xvm/lsp/server/ReadOnlyDocuments.kt | 109 + .../org/xvm/lsp/server/RenameProposal.kt | 36 + .../org/xvm/lsp/server/ResolveReports.kt | 68 + .../xvm/lsp/server/ResourceFileWatchers.kt | 250 + .../xvm/lsp/server/SemanticTokenReports.kt | 127 + .../org/xvm/lsp/server/SourceRootResolver.kt | 33 +- .../org/xvm/lsp/server/XtcLanguageServer.kt | 1339 ++- .../lsp/server/XtcLanguageServerLauncher.kt | 141 +- .../xvm/lsp/server/XtcTextDocumentService.kt | 1787 +++- .../org/xvm/lsp/server/XtcWorkspaceService.kt | 114 +- .../kotlin/org/xvm/lsp/treesitter/Platform.kt | 16 +- .../lsp/treesitter/SemanticTokenEncoder.kt | 71 +- .../lsp/treesitter/TreeSitterLibraryLookup.kt | 4 +- .../kotlin/org/xvm/lsp/treesitter/XtcNode.kt | 448 +- .../org/xvm/lsp/treesitter/XtcParser.kt | 350 +- .../org/xvm/lsp/treesitter/XtcQueries.kt | 42 +- .../org/xvm/lsp/treesitter/XtcQueryEngine.kt | 162 +- .../kotlin/org/xvm/lsp/treesitter/XtcTree.kt | 136 +- .../kotlin/org/xvm/lsp/util/ExecutionTrace.kt | 121 + .../kotlin/org/xvm/lsp/util/ProgressLabels.kt | 34 + .../kotlin/org/xvm/lsp/util/ServerLogs.kt | 221 + .../kotlin/org/xvm/lsp/util/TraceProcessId.kt | 12 + .../kotlin/org/xvm/lsp/util/WorkInProgress.kt | 6 +- .../lsp-server/src/main/resources/logback.xml | 35 +- .../org/xvm/lsp/adapter/AdapterFuturesTest.kt | 55 + .../org/xvm/lsp/adapter/CodeActionTest.kt | 12 +- .../org/xvm/lsp/adapter/CodeLensTest.kt | 4 +- .../CompilerBoundaryRequirementsTest.kt | 249 + .../xvm/lsp/adapter/CompilerCallSiteTest.kt | 366 + .../lsp/adapter/CompilerCallableProofTest.kt | 221 + .../xvm/lsp/adapter/CompilerConsumerTest.kt | 9 +- .../lsp/adapter/CompilerDispatchRoutesTest.kt | 238 + .../lsp/adapter/CompilerEmissionAuditTest.kt | 155 + .../adapter/CompilerExtractMethodFactsTest.kt | 57 + .../CompilerMethodExtractionProofTest.kt | 106 + .../adapter/CompilerMissingMethodProofTest.kt | 404 + .../xvm/lsp/adapter/CompilerPoolScopeTest.kt | 102 + .../xvm/lsp/adapter/CompilerProjectTest.kt | 383 + .../adapter/CompilerRelocationProofTest.kt | 76 + .../adapter/CompilerRenameRequirementsTest.kt | 103 + .../xvm/lsp/adapter/CompilerTestSupport.kt | 23 +- .../org/xvm/lsp/adapter/CompletionTest.kt | 93 +- .../xvm/lsp/adapter/DocumentFormattingTest.kt | 28 +- .../lsp/adapter/EmbeddingDiagnosticsTest.kt | 17 + .../org/xvm/lsp/adapter/FoldingRangeTest.kt | 26 +- .../org/xvm/lsp/adapter/FormattingTest.kt | 26 +- .../org/xvm/lsp/adapter/InlayHintTest.kt | 64 +- .../xvm/lsp/adapter/LinkedEditingRangeTest.kt | 4 +- .../lsp/adapter/ManualCompositionFixture.kt | 26 + .../org/xvm/lsp/adapter/MockAdapterTest.kt | 31 +- .../org/xvm/lsp/adapter/NavigationTest.kt | 141 +- .../xvm/lsp/adapter/OnTypeFormattingTest.kt | 23 +- .../xvm/lsp/adapter/PrepareLibraryPlaybook.kt | 38 + .../kotlin/org/xvm/lsp/adapter/RenameTest.kt | 30 +- .../org/xvm/lsp/adapter/SelectionRangeTest.kt | 56 +- .../org/xvm/lsp/adapter/SemanticModelTest.kt | 390 + .../org/xvm/lsp/adapter/SemanticTokenTest.kt | 54 +- .../org/xvm/lsp/adapter/SignatureHelpTest.kt | 150 +- .../xvm/lsp/adapter/TreeSitterAdapterTest.kt | 375 +- .../org/xvm/lsp/adapter/TreeSitterTestBase.kt | 19 +- .../lsp/adapter/TypeInfoDiagnosticsTest.kt | 2 +- .../adapter/TypeInfoFinalCompositionTest.kt | 333 + .../lsp/adapter/XdkAdapterLifecycleTest.kt | 159 + .../org/xvm/lsp/adapter/XdkAdapterTest.kt | 486 + .../adapter/XdkAnonymousConstructorTest.kt | 263 + .../lsp/adapter/XdkArgumentCompletionTest.kt | 387 + .../xvm/lsp/adapter/XdkArgumentContextTest.kt | 75 + .../xvm/lsp/adapter/XdkArrayDimensionTest.kt | 183 + .../org/xvm/lsp/adapter/XdkBuildModelTest.kt | 218 + .../xvm/lsp/adapter/XdkCallHierarchyTest.kt | 165 + .../xvm/lsp/adapter/XdkCallableBreadthTest.kt | 45 + .../xvm/lsp/adapter/XdkCandidateProbeTest.kt | 169 + .../org/xvm/lsp/adapter/XdkColorValueTest.kt | 182 + .../lsp/adapter/XdkCompletionSignatureTest.kt | 201 + .../xvm/lsp/adapter/XdkCompoundHeaderTest.kt | 195 + .../lsp/adapter/XdkConditionalRenameTest.kt | 155 + .../XdkConfigurationReplacementTest.kt | 217 + .../XdkCrossModuleMissingMethodsTest.kt | 381 + .../XdkCrossOwnerMissingMethodsTest.kt | 497 + .../lsp/adapter/XdkCursorPresentationTest.kt | 84 + .../xvm/lsp/adapter/XdkCursorRequestTest.kt | 328 + .../xvm/lsp/adapter/XdkCyclicRenameTest.kt | 44 + .../lsp/adapter/XdkDeclarationHeaderTest.kt | 225 + .../adapter/XdkDeclarationProvenanceTest.kt | 132 + .../org/xvm/lsp/adapter/XdkDeclarationTest.kt | 121 + .../xvm/lsp/adapter/XdkDelegatedRenameTest.kt | 62 + .../lsp/adapter/XdkDelimiterRecoveryTest.kt | 246 + .../org/xvm/lsp/adapter/XdkDependencyTest.kt | 407 + .../xvm/lsp/adapter/XdkDiagnosticIndexTest.kt | 150 + .../org/xvm/lsp/adapter/XdkDiagnosticTest.kt | 149 + .../xvm/lsp/adapter/XdkDocumentationTest.kt | 93 + .../org/xvm/lsp/adapter/XdkEditingTest.kt | 129 + .../XdkEnclosingValueCompletionTest.kt | 130 + .../xvm/lsp/adapter/XdkExternalRenameTest.kt | 200 + .../org/xvm/lsp/adapter/XdkFileChangesTest.kt | 130 + .../xvm/lsp/adapter/XdkFileOperationsTest.kt | 214 + .../org/xvm/lsp/adapter/XdkFoldingTest.kt | 40 + .../lsp/adapter/XdkFormattingBreadthTest.kt | 127 + .../xvm/lsp/adapter/XdkGenericHeaderTest.kt | 241 + .../org/xvm/lsp/adapter/XdkGraphMoveTest.kt | 457 + .../org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt | 153 + .../XdkImportCompletionLifecycleTest.kt | 68 + .../lsp/adapter/XdkImportCompletionTest.kt | 103 + .../xvm/lsp/adapter/XdkIncompleteCallTest.kt | 144 + .../adapter/XdkIncompleteConstructorTest.kt | 95 + .../lsp/adapter/XdkIncompleteFunctionTest.kt | 128 + .../xvm/lsp/adapter/XdkIndexLifecycleTest.kt | 132 + .../adapter/XdkInferredPresentationTest.kt | 78 + .../org/xvm/lsp/adapter/XdkInitializerTest.kt | 64 + .../lsp/adapter/XdkInlineCompletionTest.kt | 142 + .../lsp/adapter/XdkL63SharedActionsTest.kt | 95 + .../org/xvm/lsp/adapter/XdkL65ClosureTest.kt | 106 + .../xvm/lsp/adapter/XdkLambdaRenameTest.kt | 129 + .../org/xvm/lsp/adapter/XdkLexicalTest.kt | 41 + .../org/xvm/lsp/adapter/XdkLibrariesTest.kt | 101 + .../lsp/adapter/XdkLibraryDocumentsTest.kt | 62 + .../xvm/lsp/adapter/XdkLinkedEditingTest.kt | 106 + .../lsp/adapter/XdkLiteralCompletionTest.kt | 136 + .../xvm/lsp/adapter/XdkLiteralRecoveryTest.kt | 185 + .../xvm/lsp/adapter/XdkLiveWorkspaceTest.kt | 177 + .../xvm/lsp/adapter/XdkLocalExtractionTest.kt | 143 + .../adapter/XdkLocalInitializerActionsTest.kt | 219 + .../org/xvm/lsp/adapter/XdkLocalInlineTest.kt | 146 + .../xvm/lsp/adapter/XdkLocalRemovalTest.kt | 137 + .../adapter/XdkManualCompositionRenameTest.kt | 132 + .../lsp/adapter/XdkManualCompositionTest.kt | 64 + .../xvm/lsp/adapter/XdkMemberActionsTest.kt | 641 ++ .../xvm/lsp/adapter/XdkMemberInlineTest.kt | 148 + .../lsp/adapter/XdkMethodExtractionTest.kt | 381 + .../adapter/XdkMissingDeclarationNameTest.kt | 134 + .../lsp/adapter/XdkMissingDeclarationsTest.kt | 119 + .../xvm/lsp/adapter/XdkMissingMethodsTest.kt | 688 ++ .../xvm/lsp/adapter/XdkModuleSessionTest.kt | 284 + .../org/xvm/lsp/adapter/XdkMonikerTest.kt | 212 + .../org/xvm/lsp/adapter/XdkMoveInputsTest.kt | 30 + .../xvm/lsp/adapter/XdkNavigationIndexTest.kt | 233 + .../org/xvm/lsp/adapter/XdkNavigationTest.kt | 521 + .../lsp/adapter/XdkOperandCompletionTest.kt | 117 + .../xvm/lsp/adapter/XdkParameterRenameTest.kt | 187 + .../xvm/lsp/adapter/XdkPartialAnalysisTest.kt | 952 ++ .../lsp/adapter/XdkPlatformRegressionTest.kt | 193 + .../lsp/adapter/XdkPresentationScaleTest.kt | 86 + .../xvm/lsp/adapter/XdkPresentationTest.kt | 247 + .../adapter/XdkPrimaryParameterRenameTest.kt | 112 + .../xvm/lsp/adapter/XdkProjectGraphTest.kt | 99 + .../lsp/adapter/XdkProjectProofMemoryTest.kt | 104 + .../adapter/XdkProjectQueryLifecycleTest.kt | 301 + .../xvm/lsp/adapter/XdkProjectQueryTest.kt | 573 ++ .../org/xvm/lsp/adapter/XdkProjectTest.kt | 302 + .../XdkPropertyArgumentCompletionTest.kt | 270 + .../xvm/lsp/adapter/XdkQualifiedHeaderTest.kt | 240 + .../org/xvm/lsp/adapter/XdkRecoveryTest.kt | 145 + .../xvm/lsp/adapter/XdkReferenceLensTest.kt | 117 + .../xvm/lsp/adapter/XdkRenameBoundaryTest.kt | 116 + .../org/xvm/lsp/adapter/XdkRenameTest.kt | 228 + .../xvm/lsp/adapter/XdkResourceInputsTest.kt | 159 + .../xvm/lsp/adapter/XdkResourceRenameTest.kt | 317 + .../org/xvm/lsp/adapter/XdkRetentionTest.kt | 194 + .../org/xvm/lsp/adapter/XdkSafeDeleteTest.kt | 108 + .../xvm/lsp/adapter/XdkScopeCompletionTest.kt | 187 + .../xvm/lsp/adapter/XdkSemanticColorTest.kt | 313 + .../xvm/lsp/adapter/XdkSemanticLookupTest.kt | 758 ++ .../org/xvm/lsp/adapter/XdkSourceLinksTest.kt | 125 + .../adapter/XdkSpecializedConstructorTest.kt | 216 + .../org/xvm/lsp/adapter/XdkStructuralTest.kt | 68 + .../lsp/adapter/XdkSyntaxCompletionTest.kt | 204 + .../lsp/adapter/XdkTeachingWorkspaceTest.kt | 237 + .../org/xvm/lsp/adapter/XdkTypeHeaderTest.kt | 195 + .../org/xvm/lsp/adapter/XdkTypeMoveTest.kt | 650 ++ .../adapter/XdkUnionDelegationRenameTest.kt | 150 + .../org/xvm/lsp/adapter/XdkUnionRenameTest.kt | 323 + .../xvm/lsp/adapter/XdkValueTemplateTest.kt | 54 + .../lsp/adapter/XdkWorkspaceDiscoveryTest.kt | 108 + .../lsp/adapter/XdkWorkspaceNavigationTest.kt | 98 + .../adapter/XdkWorkspaceRefactoringTest.kt | 349 + .../xvm/lsp/adapter/XdkWrittenFormalTest.kt | 212 + .../org/xvm/lsp/index/WorkspaceIndexTest.kt | 22 +- .../org/xvm/lsp/index/WorkspaceIndexerTest.kt | 155 +- .../org/xvm/lsp/lexer/TemplateScannerTest.kt | 12 +- .../org/xvm/lsp/server/AdapterBackendTest.kt | 42 + .../xvm/lsp/server/CapabilityContractTest.kt | 100 + .../lsp/server/CapabilityNegotiationTest.kt | 406 + .../xvm/lsp/server/ClientNotificationsTest.kt | 130 + .../xvm/lsp/server/ClientPresentationTest.kt | 165 + .../xvm/lsp/server/CodeLensSettingsTest.kt | 18 + .../xvm/lsp/server/ColorValueProtocolTest.kt | 138 + .../lsp/server/CompilerConfigurationTest.kt | 450 + .../xvm/lsp/server/CompilerLibrariesTest.kt | 115 + .../xvm/lsp/server/ConnectionProgressTest.kt | 324 + .../lsp/server/DiagnosticPresentationTest.kt | 107 + .../lsp/server/DocumentSynchronizationTest.kt | 287 + .../lsp/server/EditorFormattingStateTest.kt | 50 + .../org/xvm/lsp/server/ExecutionTraceTest.kt | 213 + .../xvm/lsp/server/FileChangeSnapshotsTest.kt | 44 + .../server/FormattingConfigRoundTripTest.kt | 86 +- .../xvm/lsp/server/IndexingProgressTest.kt | 109 + .../server/InlineCompletionProtocolTest.kt | 149 + .../lsp/server/LibraryContentProtocolTest.kt | 166 + .../org/xvm/lsp/server/LspIntegrationTest.kt | 81 +- .../xvm/lsp/server/LspProcessLifecycleTest.kt | 72 +- .../org/xvm/lsp/server/MonikerProtocolTest.kt | 114 + .../org/xvm/lsp/server/PartialResultsTest.kt | 168 + .../org/xvm/lsp/server/ProgressLabelsTest.kt | 21 + .../xvm/lsp/server/ProtocolLifecycleTest.kt | 112 + .../xvm/lsp/server/RequestOwnershipTest.kt | 86 + .../org/xvm/lsp/server/ResolveReportsTest.kt | 29 + .../lsp/server/ResourceFileWatchersTest.kt | 232 + .../lsp/server/SemanticTokenReportsTest.kt | 71 + .../xvm/lsp/server/SourceRootResolverTest.kt | 34 +- .../xvm/lsp/server/StartupRegistrationTest.kt | 50 + .../xvm/lsp/server/XdkCallbackServerTest.kt | 183 + .../org/xvm/lsp/server/XdkCursorServerTest.kt | 373 + .../org/xvm/lsp/server/XdkCursorWatchTest.kt | 103 + .../lsp/server/XdkDeclarationServerTest.kt | 62 + .../server/XdkFeatureResolveProtocolTest.kt | 282 + .../xvm/lsp/server/XdkLanguageServerTest.kt | 430 + .../org/xvm/lsp/server/XdkModuleServerTest.kt | 269 + .../xvm/lsp/server/XdkProjectServerTest.kt | 467 + .../xvm/lsp/server/XdkPullDiagnosticsTest.kt | 444 + .../org/xvm/lsp/server/XdkRenameServerTest.kt | 729 ++ .../xvm/lsp/server/XdkResolveProtocolTest.kt | 220 + .../server/XdkSemanticTokenProtocolTest.kt | 146 + .../kotlin/org/xvm/lsp/server/XdkStdioTest.kt | 2079 ++++ .../server/XdkSyntaxCompletionProtocolTest.kt | 143 + .../xvm/lsp/server/XtcLanguageServerTest.kt | 75 +- .../SemanticTokensVsTextMateTest.kt | 225 +- .../kotlin/org/xvm/lsp/util/ServerLogsTest.kt | 79 + .../test/resources/platform/CircularBuffer.x | 132 + lang/scripts/compiler-workload.py | 504 + .../color-values/ColorPrototype.x | 17 + .../compiler-import/build.gradle.kts | 49 + .../compiler-import/gradle.properties | 4 + .../compiler-import/settings.gradle.kts | 1 + .../compiler-playbook/scenarios.json | 8837 +++++++++++++++++ .../compiler-workload/platform.json | 18 + .../semantic-highlighting/SemanticColors.x | 29 + .../semantic-colors.code-workspace | 16 + lang/tree-sitter/build.gradle.kts | 1831 ++-- lang/tree-sitter/doc/functionality.md | 2 +- lang/vscode-extension/.gitignore | 1 + lang/vscode-extension/README.md | 231 +- lang/vscode-extension/build.gradle.kts | 448 +- lang/vscode-extension/package-lock.json | 108 +- lang/vscode-extension/package.json | 433 +- lang/vscode-extension/scripts/bundle.cjs | 4 +- .../scripts/run-vscode-tests.cjs | 4 +- .../vscode-extension/src/adapter-selection.ts | 23 + lang/vscode-extension/src/build-model.ts | 84 + lang/vscode-extension/src/commands.ts | 16 +- lang/vscode-extension/src/compiler-import.ts | 81 + lang/vscode-extension/src/compiler-paths.ts | 246 + lang/vscode-extension/src/compiler-task.ts | 43 + lang/vscode-extension/src/debug-adapter.ts | 2 +- lang/vscode-extension/src/editor-settings.ts | 30 + lang/vscode-extension/src/extension.ts | 106 +- lang/vscode-extension/src/java.ts | 5 +- .../src/library-configuration.ts | 46 + lang/vscode-extension/src/library-settings.ts | 79 + lang/vscode-extension/src/lsp-client.ts | 230 +- lang/vscode-extension/src/rename-proposal.ts | 154 + lang/vscode-extension/src/runtime-settings.ts | 46 + .../src/service-notifications.ts | 11 + lang/vscode-extension/src/service-settings.ts | 35 + lang/vscode-extension/src/settings-report.ts | 33 + .../src/source-graph-configuration.ts | 46 + lang/vscode-extension/src/status-bar.ts | 21 +- lang/vscode-extension/src/support-logs.ts | 105 + lang/vscode-extension/src/test/archive.ts | 19 + .../src/test/explorer-move-trace.ts | 64 + .../src/test/explorer-move.ts | 43 + .../src/test/explorer-probe/index.ts | 75 + .../src/test/lifecycle/index.ts | 113 + .../vscode-extension/src/test/native-focus.ts | 23 + .../src/test/playbook/advanced.ts | 530 + .../src/test/playbook/colors.ts | 156 + .../src/test/playbook/compilerImports.ts | 238 + .../src/test/playbook/completion.ts | 174 + .../src/test/playbook/dependencies.ts | 173 + .../src/test/playbook/documentation.ts | 32 + .../src/test/playbook/editingClosure.ts | 102 + .../src/test/playbook/graph.ts | 237 + .../src/test/playbook/highlighting.ts | 103 + .../src/test/playbook/index.ts | 88 + .../src/test/playbook/inlineCompletion.ts | 81 + .../src/test/playbook/librarySettings.ts | 119 + .../src/test/playbook/liveWorkspace.ts | 175 + .../src/test/playbook/memberActions.ts | 116 + .../src/test/playbook/modules.ts | 207 + .../src/test/playbook/navigation.ts | 97 + .../src/test/playbook/platform.ts | 534 + .../src/test/playbook/playbook.test.ts | 125 + .../src/test/playbook/progress.ts | 115 + .../src/test/playbook/properties.ts | 65 + .../src/test/playbook/referenceLenses.ts | 52 + .../src/test/playbook/reliability.ts | 160 + .../src/test/playbook/rename.ts | 93 + .../src/test/playbook/renameFamilies.ts | 177 + .../src/test/playbook/runtimeSettings.ts | 118 + .../src/test/playbook/semantics.ts | 221 + .../src/test/playbook/shared.ts | 102 + .../src/test/playbook/support.ts | 274 + .../src/test/playbook/syntaxCompletion.ts | 48 + .../src/test/playbook/typeMoves.ts | 77 + lang/vscode-extension/src/test/progress.ts | 62 + .../vscode-extension/src/test/runLifecycle.ts | 154 + lang/vscode-extension/src/test/runTest.ts | 192 +- .../src/test/settings-persistence/index.ts | 46 + .../src/test/suite/activation.test.ts | 17 + .../src/test/suite/build-model.test.ts | 33 + .../src/test/suite/compiler-import.test.ts | 94 + .../src/test/suite/compiler-paths.test.ts | 41 + .../src/test/suite/compiler-project.test.ts | 125 + .../src/test/suite/compiler-task.test.ts | 108 + .../src/test/suite/extension.test.ts | 7 +- lang/vscode-extension/src/test/suite/index.ts | 7 +- .../test/suite/library-configuration.test.ts | 41 + .../src/test/suite/lsp-startup.test.ts | 109 +- .../src/test/suite/playbook-wait.test.ts | 37 + .../src/test/suite/runtime-settings.test.ts | 16 + .../src/test/suite/service-settings.test.ts | 20 + .../src/test/suite/settings-report.test.ts | 42 + .../suite/source-graph-configuration.test.ts | 68 + .../src/test/suite/support-logs.test.ts | 53 + lang/vscode-extension/src/test/vscodeCache.ts | 25 +- lang/vscode-extension/src/test/wait.ts | 36 + lang/vscode-extension/src/test/workbenchUi.ts | 62 + lang/vscode-extension/tsconfig.json | 5 +- xdk/build.gradle.kts | 23 +- 648 files changed, 109353 insertions(+), 5438 deletions(-) create mode 100644 build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt create mode 100644 build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt create mode 100644 build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CachedIdeInstaller.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybookTest.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/DiagnosticProbePlugin.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/FocusRecovery.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LocalLsp4ijPlugin.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/MemberActionScenarios.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDependencies.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDocumentation.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityGraph.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityModules.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityNavigation.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityScenarios.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RenameFamilyScenarios.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SignatureHints.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaits.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CodeLensUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ColorUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerImportPage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerLibrariesPage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerReportPage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerSettingsPage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorDiagnostics.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/FileTreeOperations.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/HighlightingUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/InlineCompletionUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LanguageServicePage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LargeFileProbe.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/NavigationChooser.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PartialResults.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProgressUi.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProjectLifecycle.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/RefreshRequests.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ServerRuntimePage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/SettingsPersistencePage.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/StartupEdits.kt create mode 100644 lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/WorkspaceEdits.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdates.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerProjectConfigurable.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerSettings.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerWorkspaceModels.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConfigurationJson.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticReportJson.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ExportServerLogsAction.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/FileMoveTargets.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageAdapter.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfiguration.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServicePreferences.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceSettings.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/OrderedPaths.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SelectLanguageAdapterAction.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeConfigurable.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcClientFeatures.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcParameterInfoHandler.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcResolveCodeAction.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcServerLogAction.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCapability.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFutures.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCallableIdentity.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCalls.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDeclarations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPrimaryParameters.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceTypes.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModelBuilder.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCalls.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDocumentation.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkFileChanges.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkHierarchy.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkInlineCompletions.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveInputs.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveOperations.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRefactoringSyntax.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResources.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceMoves.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceNavigation.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientPresentation.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerConfiguration.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerLibraries.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentSynchronization.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentText.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/FileChangeSnapshots.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResourceFileWatchers.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt create mode 100644 lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallableProofTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDispatchRoutesTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerEmissionAuditTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPoolScopeTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerProjectTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRelocationProofTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRenameRequirementsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/ManualCompositionFixture.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/PrepareLibraryPlaybook.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticModelTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoFinalCompositionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallableBreadthTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCandidateProbeTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConditionalRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationProvenanceTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL63SharedActionsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL65ClosureTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPartialAnalysisTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPlatformRegressionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameBoundaryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTeachingWorkspaceTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityContractTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt create mode 100644 lang/lsp-server/src/test/resources/platform/CircularBuffer.x create mode 100644 lang/scripts/compiler-workload.py create mode 100644 lang/test-fixtures/color-values/ColorPrototype.x create mode 100644 lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts create mode 100644 lang/test-fixtures/compiler-playbook/compiler-import/gradle.properties create mode 100644 lang/test-fixtures/compiler-playbook/compiler-import/settings.gradle.kts create mode 100644 lang/test-fixtures/compiler-playbook/scenarios.json create mode 100644 lang/test-fixtures/compiler-workload/platform.json create mode 100644 lang/test-fixtures/semantic-highlighting/SemanticColors.x create mode 100644 lang/test-fixtures/semantic-highlighting/semantic-colors.code-workspace create mode 100644 lang/vscode-extension/src/adapter-selection.ts create mode 100644 lang/vscode-extension/src/build-model.ts create mode 100644 lang/vscode-extension/src/compiler-import.ts create mode 100644 lang/vscode-extension/src/compiler-paths.ts create mode 100644 lang/vscode-extension/src/compiler-task.ts create mode 100644 lang/vscode-extension/src/editor-settings.ts create mode 100644 lang/vscode-extension/src/library-configuration.ts create mode 100644 lang/vscode-extension/src/library-settings.ts create mode 100644 lang/vscode-extension/src/rename-proposal.ts create mode 100644 lang/vscode-extension/src/runtime-settings.ts create mode 100644 lang/vscode-extension/src/service-notifications.ts create mode 100644 lang/vscode-extension/src/service-settings.ts create mode 100644 lang/vscode-extension/src/settings-report.ts create mode 100644 lang/vscode-extension/src/source-graph-configuration.ts create mode 100644 lang/vscode-extension/src/support-logs.ts create mode 100644 lang/vscode-extension/src/test/archive.ts create mode 100644 lang/vscode-extension/src/test/explorer-move-trace.ts create mode 100644 lang/vscode-extension/src/test/explorer-move.ts create mode 100644 lang/vscode-extension/src/test/explorer-probe/index.ts create mode 100644 lang/vscode-extension/src/test/lifecycle/index.ts create mode 100644 lang/vscode-extension/src/test/native-focus.ts create mode 100644 lang/vscode-extension/src/test/playbook/advanced.ts create mode 100644 lang/vscode-extension/src/test/playbook/colors.ts create mode 100644 lang/vscode-extension/src/test/playbook/compilerImports.ts create mode 100644 lang/vscode-extension/src/test/playbook/completion.ts create mode 100644 lang/vscode-extension/src/test/playbook/dependencies.ts create mode 100644 lang/vscode-extension/src/test/playbook/documentation.ts create mode 100644 lang/vscode-extension/src/test/playbook/editingClosure.ts create mode 100644 lang/vscode-extension/src/test/playbook/graph.ts create mode 100644 lang/vscode-extension/src/test/playbook/highlighting.ts create mode 100644 lang/vscode-extension/src/test/playbook/index.ts create mode 100644 lang/vscode-extension/src/test/playbook/inlineCompletion.ts create mode 100644 lang/vscode-extension/src/test/playbook/librarySettings.ts create mode 100644 lang/vscode-extension/src/test/playbook/liveWorkspace.ts create mode 100644 lang/vscode-extension/src/test/playbook/memberActions.ts create mode 100644 lang/vscode-extension/src/test/playbook/modules.ts create mode 100644 lang/vscode-extension/src/test/playbook/navigation.ts create mode 100644 lang/vscode-extension/src/test/playbook/platform.ts create mode 100644 lang/vscode-extension/src/test/playbook/playbook.test.ts create mode 100644 lang/vscode-extension/src/test/playbook/progress.ts create mode 100644 lang/vscode-extension/src/test/playbook/properties.ts create mode 100644 lang/vscode-extension/src/test/playbook/referenceLenses.ts create mode 100644 lang/vscode-extension/src/test/playbook/reliability.ts create mode 100644 lang/vscode-extension/src/test/playbook/rename.ts create mode 100644 lang/vscode-extension/src/test/playbook/renameFamilies.ts create mode 100644 lang/vscode-extension/src/test/playbook/runtimeSettings.ts create mode 100644 lang/vscode-extension/src/test/playbook/semantics.ts create mode 100644 lang/vscode-extension/src/test/playbook/shared.ts create mode 100644 lang/vscode-extension/src/test/playbook/support.ts create mode 100644 lang/vscode-extension/src/test/playbook/syntaxCompletion.ts create mode 100644 lang/vscode-extension/src/test/playbook/typeMoves.ts create mode 100644 lang/vscode-extension/src/test/progress.ts create mode 100644 lang/vscode-extension/src/test/runLifecycle.ts create mode 100644 lang/vscode-extension/src/test/settings-persistence/index.ts create mode 100644 lang/vscode-extension/src/test/suite/build-model.test.ts create mode 100644 lang/vscode-extension/src/test/suite/compiler-import.test.ts create mode 100644 lang/vscode-extension/src/test/suite/compiler-paths.test.ts create mode 100644 lang/vscode-extension/src/test/suite/compiler-project.test.ts create mode 100644 lang/vscode-extension/src/test/suite/compiler-task.test.ts create mode 100644 lang/vscode-extension/src/test/suite/library-configuration.test.ts create mode 100644 lang/vscode-extension/src/test/suite/playbook-wait.test.ts create mode 100644 lang/vscode-extension/src/test/suite/runtime-settings.test.ts create mode 100644 lang/vscode-extension/src/test/suite/service-settings.test.ts create mode 100644 lang/vscode-extension/src/test/suite/settings-report.test.ts create mode 100644 lang/vscode-extension/src/test/suite/source-graph-configuration.test.ts create mode 100644 lang/vscode-extension/src/test/suite/support-logs.test.ts create mode 100644 lang/vscode-extension/src/test/wait.ts create mode 100644 lang/vscode-extension/src/test/workbenchUi.ts diff --git a/.github/workflows/commit.yml b/.github/workflows/commit.yml index 8837faa8049..a502586d2ea 100644 --- a/.github/workflows/commit.yml +++ b/.github/workflows/commit.yml @@ -750,6 +750,18 @@ jobs: echo "🎉 ALL LANG VALIDATION COMPLETE" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" + - name: Verify the packaged compiler LSP over stdio + if: | + success() && matrix.os == 'ubuntu-latest' && + steps.publish-flags.outputs.validate-core == 'true' + shell: bash + env: + GRADLE_OPTS: ${{ steps.versions.outputs.gradle-jvm-opts }} + run: | + ./gradlew ${{ steps.versions.outputs.gradle-options }} \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + :lang:lsp-server:compilerStdioTest + - name: Require compiler-consumer test results if: | success() && matrix.os == 'ubuntu-latest' && @@ -759,10 +771,26 @@ jobs: python3 - <<'PYTEST' from pathlib import Path from xml.etree import ElementTree - result = ElementTree.parse(Path("lang/lsp-server/build/test-results/test/TEST-org.xvm.lsp.adapter.CompilerConsumerTest.xml")).getroot() - counts = {key: int(result.attrib[key]) for key in ("tests", "failures", "errors", "skipped")} - assert counts["tests"] > 0 and all(counts[key] == 0 for key in ("failures", "errors", "skipped")), counts - print("adapter.CompilerConsumerTest", counts) + results = Path("lang/lsp-server/build/test-results/test") + suites = [ + "adapter.CompilerConsumerTest", + "adapter.XdkAdapterTest", "adapter.XdkNavigationTest", "adapter.XdkDiagnosticTest", + "adapter.XdkAdapterLifecycleTest", "adapter.SemanticModelTest", "adapter.CompilerProjectTest", + "adapter.TypeInfoDiagnosticsTest", "adapter.TypeInfoFinalCompositionTest", "adapter.EmbeddingDiagnosticsTest", + "adapter.XdkModuleSessionTest", "adapter.XdkRecoveryTest", "adapter.XdkPartialAnalysisTest", + "adapter.CompilerCallSiteTest", + "server.XdkModuleServerTest", + "server.XdkLanguageServerTest", "server.AdapterBackendTest", + ] + for suite in suites: + result = ElementTree.parse(results / f"TEST-org.xvm.lsp.{suite}.xml").getroot() + counts = {key: int(result.attrib[key]) for key in ("tests", "failures", "errors", "skipped")} + assert counts["tests"] > 0 and all(counts[key] == 0 for key in ("failures", "errors", "skipped")), (suite, counts) + print(suite, counts) + stdio = ElementTree.parse("lang/lsp-server/build/test-results/compilerStdioTest/TEST-org.xvm.lsp.server.XdkStdioTest.xml").getroot() + counts = {key: int(stdio.attrib[key]) for key in ("tests", "failures", "errors", "skipped")} + assert counts["tests"] > 0 and all(counts[key] == 0 for key in ("failures", "errors", "skipped")), ("server.XdkStdioTest", counts) + print("server.XdkStdioTest", counts) PYTEST # The checks that lang validation ran passed; later runs of this pull request on the diff --git a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt index f33e1fe8a37..3887818a2a6 100644 --- a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt +++ b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt @@ -1,18 +1,20 @@ import org.gradle.api.DefaultTask import org.gradle.api.file.DirectoryProperty -import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.ListProperty import org.gradle.api.provider.Property import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputDirectory -import org.gradle.api.tasks.InputFile -import org.gradle.api.tasks.Optional import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.UntrackedTask -import java.io.File -import java.io.RandomAccessFile +import java.nio.ByteBuffer +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.NoSuchFileException +import java.nio.file.Path +import java.nio.file.StandardOpenOption.READ +import java.nio.file.attribute.BasicFileAttributes import kotlin.concurrent.thread @UntrackedTask(because = "Runtime reporting task reads live sandbox state and should never be state-tracked") @@ -43,10 +45,8 @@ abstract class RunIdeEnvironmentReportTask : DefaultTask() { @get:Input abstract val pluginNames: ListProperty - @get:InputFile - @get:Optional - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val lspLogFile: RegularFileProperty + @get:Input + abstract val lspLogDirectory: Property @TaskAction fun report() { @@ -84,18 +84,15 @@ abstract class RunIdeEnvironmentReportTask : DefaultTask() { logger.lifecycle("[runIde] Nuke sandbox (keeps IDE download): ./gradlew :lang:intellij-plugin:clean") logger.lifecycle("[runIde] Nuke cached IDE + metadata: rm -rf lang/.intellijPlatform/localPlatformArtifacts") - lspLogFile.orNull?.asFile?.let { logFile -> - logger.lifecycle("[runIde] LSP log: ${logFile.absolutePath} (tailing to console)") - } + logger.lifecycle("[runIde] LSP logs: ${lspLogDirectory.get()}/server-*/server.log (tailing new output to console)") } } @UntrackedTask(because = "Runtime log tail task manages background thread state and should never be state-tracked") abstract class StartLogTailTask : DefaultTask() { - @get:InputFile - @get:Optional - @get:PathSensitive(PathSensitivity.NONE) - abstract val logFile: RegularFileProperty + // The directory may not exist until the first server starts; its live contents are not inputs. + @get:Input + abstract val logDirectory: Property @get:Input abstract val threadName: Property @@ -105,29 +102,25 @@ abstract class StartLogTailTask : DefaultTask() { @TaskAction fun startTail() { - val file = logFile.orNull?.asFile ?: return + val root = Path.of(logDirectory.get()) val name = threadName.get() - Thread.getAllStackTraces().keys.firstOrNull { it.name == name }?.interrupt() - - val startSize = if (file.exists()) file.length() else 0L val prefix = linePrefix.get() + val initial = ServerLogTails.poll(root, emptyMap(), skipExisting = true).positions thread(isDaemon = true, name = name) { - runCatching { - while (!file.exists() && !Thread.currentThread().isInterrupted) { + // Owned exclusively by this tail thread; no compiler or IDE callbacks access the cursors. + var positions = initial + try { + while (!Thread.currentThread().isInterrupted) { + val batch = ServerLogTails.poll(root, positions) + positions = batch.positions + batch.lines.forEach { logger.lifecycle("$prefix$it") } Thread.sleep(500) } - RandomAccessFile(file, "r").use { raf -> - raf.seek(startSize) - while (!Thread.currentThread().isInterrupted) { - val line = raf.readLine() - if (line != null) { - logger.lifecycle("$prefix$line") - } else { - Thread.sleep(200) - } - } - } + } catch (_: InterruptedException) { + Thread.currentThread().interrupt() + } catch (failure: Exception) { + logger.warn("Ecstasy log tail stopped: ${failure.message}") } } } @@ -143,3 +136,51 @@ abstract class StopLogTailTask : DefaultTask() { Thread.getAllStackTraces().keys.firstOrNull { it.name == threadName.get() }?.interrupt() } } + +/** Detached file cursors follow new sessions and rollover without keeping old file handles open. */ +internal object ServerLogTails { + private val sessionName = Regex("server-[0-9]+-[0-9]+") + + data class Position(val key: Any?, val offset: Long) + + data class Batch(val positions: Map, val lines: List) + + fun poll(root: Path, previous: Map, skipExisting: Boolean = false): Batch { + val paths = if (Files.isDirectory(root, NOFOLLOW_LINKS)) { + Files.list(root).use { sessions -> + sessions + .filter { Files.isDirectory(it, NOFOLLOW_LINKS) && sessionName.matches(it.fileName.toString()) } + .map { it.resolve("server.log") } + .toList() + } + } else emptyList() + val rows = paths.mapNotNull { path -> + try { + val attributes = Files.readAttributes(path, BasicFileAttributes::class.java, NOFOLLOW_LINKS) + if (!attributes.isRegularFile) return@mapNotNull null + val prior = previous[path]?.takeIf { it.key == attributes.fileKey() && it.offset <= attributes.size() } + if (skipExisting) { + return@mapNotNull Triple(path, Position(attributes.fileKey(), attributes.size()), emptyList()) + } + val offset = prior?.offset ?: 0L + Files.newByteChannel(path, setOf(READ, NOFOLLOW_LINKS)).use { channel -> + channel.position(offset) + val bytes = ByteBuffer.allocate(minOf((channel.size() - offset).coerceAtLeast(0), 64 * 1024L).toInt()) + while (bytes.hasRemaining() && channel.read(bytes) > 0) { /* bounded poll */ } + // Hold partial lines for the next poll; split oversized lines at a UTF-8 boundary. + val end = (0 until bytes.position()).lastOrNull { bytes[it] == '\n'.code.toByte() }?.plus(1) + ?: if (bytes.position() < 64 * 1024) { + 0 + } else { + (0 until bytes.position()).lastOrNull { bytes[it].toInt() and 0xC0 != 0x80 } ?: 0 + } + val text = String(bytes.array(), 0, end, Charsets.UTF_8) + Triple(path, Position(attributes.fileKey(), offset + end), text.lineSequence().filter(String::isNotEmpty).toList()) + } + } catch (_: NoSuchFileException) { + null // A retired session or rolled file may disappear between discovery and reading. + } + } + return Batch(rows.associate { it.first to it.second }, rows.flatMap { it.third }) + } +} diff --git a/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt b/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt new file mode 100644 index 00000000000..377ea48e770 --- /dev/null +++ b/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt @@ -0,0 +1,86 @@ +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.Internal +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault +import java.nio.channels.FileChannel +import java.nio.channels.OverlappingFileLockException +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path +import java.nio.file.StandardOpenOption.CREATE +import java.nio.file.StandardOpenOption.WRITE + +/** Prunes disposable payloads only after a runner confirms that its IDE has exited. */ +@DisableCachingByDefault(because = "Retention acts on completed test runs, not reproducible task outputs") +abstract class PruneTestReportsTask : DefaultTask() { + @get:Internal + abstract val reportsDirectory: DirectoryProperty + + @get:Input + abstract val retainedRuns: Property + + init { + retainedRuns.convention(5) + } + + @TaskAction + fun prune() { + val count = TestReportRetention.prune(reportsDirectory.get().asFile.toPath(), retainedRuns.get()) + logger.lifecycle("Test report retention: pruned payloads from {} old runs; summaries retained", count) + } +} + +internal object TestReportRetention { + fun prune(root: Path, keep: Int = 5): Int { + require(keep >= 1) { "Keep at least one complete run" } + if (!Files.isDirectory(root, NOFOLLOW_LINKS)) return 0 + return FileChannel.open(root.resolve(".retention.lock"), CREATE, WRITE, NOFOLLOW_LINKS).use { channel -> + val lock = try { + channel.tryLock() + } catch (_: OverlappingFileLockException) { + null + } + lock?.use { pruneClosed(root, keep) } ?: 0 + } + } + + private fun pruneClosed(root: Path, keep: Int): Int { + val runs = children(root) + .filter { it.fileName.toString().startsWith("run-") && Files.isDirectory(it, NOFOLLOW_LINKS) } + .filter { Files.isRegularFile(it.resolve(".completed"), NOFOLLOW_LINKS) } + .sortedByDescending { Files.getLastModifiedTime(it.resolve(".completed")) } + val old = runs.drop(keep).filterNot { Files.exists(it.resolve(".keep-artifacts"), NOFOLLOW_LINKS) }.toSet() + runs.filterNot { Files.exists(it.resolve(".keep-artifacts"), NOFOLLOW_LINKS) }.forEach { run -> + // Starter stores each sandbox separately from the run's result files. + val sandboxes = children(root.resolve("out/ide-tests/tests")) + .filter { Files.isDirectory(it, NOFOLLOW_LINKS) } + .map { it.resolve("XtcCompilerPlaybook-${run.fileName}") } + .filter { Files.isDirectory(it, NOFOLLOW_LINKS) } + sandboxes.forEach { sandbox -> + val payloads = if (run in old) children(sandbox) + else listOf("system", "plugins", "config", "temp").map(sandbox::resolve) + payloads.forEach(::removePayload) + } + if (run in old) { + children(run).filter { + Files.isDirectory(it, NOFOLLOW_LINKS) || Files.isSymbolicLink(it) || it.fileName.toString().endsWith(".log") + }.forEach(::removePayload) + } + } + return old.size + } + + private fun children(path: Path): List = + if (Files.isDirectory(path, NOFOLLOW_LINKS)) Files.list(path).use { it.toList() } else emptyList() + + private fun removePayload(path: Path) { + if (!Files.exists(path, NOFOLLOW_LINKS)) return + // walk does not follow symlinks; never touch a source worktree accidentally placed here. + val paths = Files.walk(path).use { it.toList() } + if (paths.any { it.fileName.toString() == ".git" }) return + paths.sortedDescending().forEach(Files::deleteIfExists) + } +} diff --git a/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt b/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt new file mode 100644 index 00000000000..5cf21dae235 --- /dev/null +++ b/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt @@ -0,0 +1,51 @@ +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardOpenOption.APPEND + +class ServerLogTailsTest { + @TempDir lateinit var root: Path + + @Test + fun `oversized lines keep moving without splitting UTF-8 characters`() { + val log = Files.createDirectory(root.resolve("server-1-1")).resolve("server.log") + val text = "é".repeat(40_000) + Files.writeString(log, "$text\n") + val first = ServerLogTails.poll(root, emptyMap()) + val second = ServerLogTails.poll(root, first.positions) + assertEquals(text, (first.lines + second.lines).joinToString("")) + } + + @Test + fun `tail skips old output follows new sessions and handles rollover`() { + val first = Files.createDirectory(root.resolve("server-1-1")).resolve("server.log") + Files.writeString(first, "old output\n") + val initial = ServerLogTails.poll(root, emptyMap(), skipExisting = true) + assertEquals(emptyList(), initial.lines) + Files.writeString(first, "héllo\npartial", APPEND) + val appended = ServerLogTails.poll(root, initial.positions) + assertEquals(listOf("héllo"), appended.lines) + assertEquals(emptyList(), ServerLogTails.poll(root, appended.positions).lines) + Files.writeString(first, " line\n", APPEND) + val completed = ServerLogTails.poll(root, appended.positions) + assertEquals(listOf("partial line"), completed.lines) + Files.move(first, first.resolveSibling("server.2026-10-06.0.log")) + Files.writeString(first, "rolled\n") + val next = Files.createDirectory(root.resolve("server-2-2")).resolve("server.log") + Files.writeString(next, "started\n") + assertEquals(setOf("rolled", "started"), ServerLogTails.poll(root, completed.positions).lines.toSet()) + } + + @Test + fun `tail tolerates absent files and excludes foreign directories and symlinks`() { + assertEquals(emptyMap(), ServerLogTails.poll(root.resolve("absent"), emptyMap()).positions) + val foreign = Files.createDirectory(root.resolve("foreign")).resolve("server.log") + Files.writeString(foreign, "unrelated\n") + Files.createDirectory(root.resolve("server-1-1")) + Files.createSymbolicLink(root.resolve("server-2-2"), foreign.parent) + Files.createSymbolicLink(root.resolve("server-1-1/server.log"), foreign) + assertEquals(emptyList(), ServerLogTails.poll(root, emptyMap()).lines) + } +} diff --git a/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt b/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt new file mode 100644 index 00000000000..fe0c69921f7 --- /dev/null +++ b/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt @@ -0,0 +1,70 @@ +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.FileTime + +class TestReportRetentionTest { + @TempDir lateinit var root: Path + + private fun run(name: String, time: Long, completed: Boolean = true): Path { + val run = Files.createDirectories(root.resolve(name)) + Files.writeString(run.resolve("results.json"), "{\"failures\": 1}") + Files.writeString(Files.createDirectory(run.resolve("workspace")).resolve("Sample.x"), "module Sample {}") + if (completed) { + Files.writeString(run.resolve(".completed"), "IDE exited") + Files.setLastModifiedTime(run.resolve(".completed"), FileTime.fromMillis(time)) + } + return run + } + + @Test + fun `retention preserves receipts recent payloads pins and unfinished runs`() { + val old = run("run-old", 1) + val pinned = run("run-pinned", 2) + Files.createFile(pinned.resolve(".keep-artifacts")) + val recent = run("run-recent", 3) + val active = run("run-active", 4, completed = false) + Files.writeString(old.resolve("client-trace.log"), "trace") + TestReportRetention.prune(root, keep = 1) + assertTrue(Files.exists(old.resolve("results.json"))) + assertFalse(Files.exists(old.resolve("workspace"))) + assertFalse(Files.exists(old.resolve("client-trace.log"))) + listOf(pinned, recent, active).forEach { assertTrue(Files.exists(it.resolve("workspace/Sample.x"))) } + } + + @Test + fun `completed IntelliJ sandboxes release caches but retain recent diagnostic logs`() { + val old = run("run-old", 1) + val recent = run("run-recent", 2) + fun sandbox(run: Path): Path = root.resolve("out/ide-tests/tests/IU-1/XtcCompilerPlaybook-${run.fileName}") + listOf(old, recent).forEach { run -> + listOf("system", "config", "plugins", "temp", "log", "reports").forEach { name -> + Files.writeString(Files.createDirectories(sandbox(run).resolve(name)).resolve("data"), "generated") + } + } + TestReportRetention.prune(root, keep = 1) + assertFalse(Files.exists(sandbox(old).resolve("log"))) + assertFalse(Files.exists(sandbox(recent).resolve("system"))) + assertTrue(Files.exists(sandbox(recent).resolve("log/data"))) + assertTrue(Files.exists(sandbox(recent).resolve("reports/data"))) + } + + @Test + fun `cleanup never follows payload symlinks or removes Git worktrees`() { + val old = run("run-old", 1) + run("run-new", 2) + val outside = Files.createDirectories(root.resolve("source")) + Files.writeString(outside.resolve("precious.x"), "module Precious {}") + Files.createSymbolicLink(old.resolve("workspace/external"), outside) + val worktree = Files.createDirectories(old.resolve("unexpected-worktree")) + Files.writeString(worktree.resolve(".git"), "gitdir: preserve") + TestReportRetention.prune(root, keep = 1) + TestReportRetention.prune(root, keep = 1) + assertTrue(Files.exists(outside.resolve("precious.x"))) + assertTrue(Files.exists(worktree.resolve(".git"))) + assertFalse(Files.exists(old.resolve("workspace"))) + } +} diff --git a/gradle.properties b/gradle.properties index 17ed32a6236..16dd3116aa7 100644 --- a/gradle.properties +++ b/gradle.properties @@ -38,7 +38,7 @@ org.gradle.vfs.verbose=false # JVM and Gradle daemon flags. The default memory usage is 700M for a Daemon, which can be slow in extreme environments. # Note: --enable-native-access=ALL-UNNAMED suppresses warnings from Gradle's native platform library -org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=512m -Dfile.encoding=UTF-8 +org.gradle.jvmargs=-Xmx2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8 # Logging and warning levels @@ -101,22 +101,25 @@ includeBuildAttachManualTests=false # 2. includeBuildAttachLang: Whether lang lifecycle tasks are wired to root lifecycle tasks, # so that ./gradlew build also builds lang. Requires includeBuildLang=true. # -# Use ./gradlew lang:lsp-server:build to build LSP server separately. -# CI overrides includeBuildLang to false for push/PR builds (see commit.yml). +# CI overrides includeBuildLang to false for push/PR builds (see commit.yml), because attaching +# lang pulls in the IntelliJ plugin and its platform resolution; that cost is a cold-cache cost, +# not a per-build one. # -# TODO: We want to have lang part of the default build, ideally, provided that it can be shown to bring no significant overhead with warm caches. +# These are on because lang/lsp-server depends on javatools: its XdkAdapter compiles through the +# embedding API, so a change to the compiler can break the language server, and nothing would say +# so if lang were not part of the build. Measured overhead on a warm, fully up-to-date +# './gradlew build': 0.74-0.82s without lang against 0.95s with, which is the condition the +# earlier TODO here set for turning this on. # -includeBuildLang=false -includeBuildAttachLang=false +includeBuildLang=true +includeBuildAttachLang=true # Searchable options takes significant build time to compute the first time and has some complexity: This is typically something we want to do # only for a production release, not in dev, unless we want to manually use the plugin under development. This enabled index building for things # like being able to find Ecstasy plugin features in IntelliJ with Shift+Shift. lsp.buildSearchableOptions=false -# Semantic tokens are tested and should be enabled by default in the LSP -# Treesitter is tested and should be enabled by default in the LSP. The regex based mock adapter is trivially constrained, and the xdk adapter -# is still under development, and will need the SemanticModel to be finished and integrate with the real Ecstasy Lexer/Parser. Most of this work -# is done but incomplete. +# Compiler-backed language support is the default. Explicit alternatives remain available with +# -Plsp.adapter=treesitter (syntax only) or -Plsp.adapter=mock (test fallback). lsp.semanticTokens=true -lsp.adapter=treesitter +lsp.adapter=compiler diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 5bbee0fa4b5..c5948345490 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -81,7 +81,7 @@ lang-node-gradle-plugin = "7.1.0" # ============================================================================= # Lang tooling - IntelliJ plugin # ============================================================================= -lang-intellij-ide = "2026.1" +lang-intellij-ide = "2026.2.3" lang-intellij-platform-gradle-plugin = "2.20.1" # JUnit 4.x is required at RUNTIME by the IntelliJ Platform test harness # (com.intellij.testFramework). IntelliJ's test infrastructure internally depends on JUnit 4 @@ -89,7 +89,11 @@ lang-intellij-platform-gradle-plugin = "2.20.1" # on the testRuntimeOnly classpath, IntelliJ platform tests fail with ClassNotFoundException # for org.junit.Test or org.junit.runners.BlockJUnit4ClassRunner. lang-intellij-junit4-compat = "4.13.2" -lang-intellij-lsp4ij = "0.19.4" +lang-intellij-lsp4ij = "0.21.0" +# Test launcher libraries aligned with the Starter framework for the selected IDE. +lang-intellij-kodein = "7.26.1" +lang-intellij-coroutines = "1.10.2-intellij-2" +lang-intellij-service-messages = "2024.07" # ============================================================================= # Lang tooling - Tree-sitter native library @@ -228,6 +232,9 @@ lang-slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "lang-slf # ============================================================================= # JUnit 4 runtime compat for IntelliJ Platform test harness (see version comment above) lang-intellij-junit4-compat = { module = "junit:junit", version.ref = "lang-intellij-junit4-compat" } +lang-intellij-kodein = { module = "org.kodein.di:kodein-di-jvm", version.ref = "lang-intellij-kodein" } +lang-intellij-coroutines = { module = "org.jetbrains.intellij.deps.kotlinx:kotlinx-coroutines-core-jvm", version.ref = "lang-intellij-coroutines" } +lang-intellij-service-messages = { module = "org.jetbrains.teamcity:serviceMessages", version.ref = "lang-intellij-service-messages" } # ============================================================================= # Lang tooling - Tree-sitter native library @@ -244,4 +251,11 @@ tukaani-xz = { module = "org.tukaani:xz", version.ref = "tukaani-xz" } lang-jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = "lang-jackson" } [bundles] +# One library set for the distribution and embedded compiler hosts. +xdk-libraries = [ + "xdk-aggregate", "xdk-cli", "xdk-collections", "xdk-convert", "xdk-crypto", + "xdk-ecstasy", "xdk-json", "xdk-jsondb", "xdk-metrics", "xdk-net", "xdk-oodb", + "xdk-runner", "xdk-sec", "xdk-web", "xdk-webauth", "xdk-webcli", "xdk-xenia", + "xdk-xml", "xdk-xunit", "xdk-xunit-db", "xdk-xunit-engine", "javatools-bridge", +] unicode = ["jakarta-activation-api", "jakarta-xml-bind-api", "jaxb-runtime"] diff --git a/lang/README.md b/lang/README.md index 28fa07111ce..c8436fdd0e2 100644 --- a/lang/README.md +++ b/lang/README.md @@ -1,5 +1,11 @@ # Ecstasy Language Tooling +The compiler-backed LSP is the default server for the IntelliJ and VS Code integrations. +It provides diagnostics, semantic highlighting, navigation, completion and compiler-checked +refactorings, including analysis of incomplete source. Tree-sitter remains a selectable syntax-only +adapter. See the [manual playbook](doc/manual-test-plan.md) for feature boundaries, test fixtures, +native harness commands and client compatibility limits. + Language tooling for the Ecstasy programming language, including LSP server, IDE plugins, and editor support. @@ -30,7 +36,7 @@ you need to set two properties: ### Option 1: Command Line (Temporary) ```bash -./gradlew build +./gradlew build -PincludeBuildLang=true -PincludeBuildAttachLang=true ``` ### Option 2: Environment Variables (Session/Shell) @@ -94,6 +100,26 @@ The test outputs parse timing sorted by slowest files, helping identify grammar ./gradlew :vscode-extension:build ``` +## Kotlin Formatting + +Spotless runs ktlint's default rules over Kotlin sources, tests, IntelliJ integration tests +and the `lang` Gradle scripts, using the same formatter versions as master. Versions are pinned in +the shared version catalog. Generated build outputs are excluded. From the repository root: + +```bash +# Format all lang Kotlin sources and Gradle scripts +./gradlew :lang:spotlessApply -PincludeBuildLang=true -PincludeBuildAttachLang=true + +# Verify without modifying files +./gradlew :lang:spotlessCheck -PincludeBuildLang=true -PincludeBuildAttachLang=true +``` + +For one module, use a task such as `:lang:lsp-server:spotlessCheck`. Local Kotlin compilation +formats that module's sources before compiling, and local `check` also applies formatting first. +When `CI` is set, compilation and checks only verify formatting. Explicit `spotlessCheck` is always +read-only. Spotless invokes the ktlint engine directly; the separate ktlint Gradle plugin is not +used. Formatting and ktlint's non-formatting rules (such as naming) are both checked. + ## Native Library Build (Tree-sitter) The LSP server uses tree-sitter for fast, incremental parsing. This requires a native shared library @@ -245,7 +271,7 @@ Generated files: │ │ Adapter (interface) │ │ │ │ ┌──────────────┬──────────────┬──────────────┐ │ │ │ │ │ Mock │ TreeSitter │ Xdk │ │ │ -│ │ │ (regex) │ (syntax) │ (future) │ │ │ +│ │ │ (regex) │ (syntax) │ (compiler) │ │ │ │ │ └──────────────┴──────────────┴──────────────┘ │ │ │ └─────────────────────────────────────────────────────────┘ │ └─────────────────────────────────────────────────────────────────┘ @@ -254,16 +280,20 @@ Generated files: ### LSP Adapter Selection ```bash -# Tree-sitter (default) - syntax-aware, ~70% LSP features +# Tree-sitter (explicit syntax-only alternative) - syntax-aware features and workspace index ./gradlew :lang:lsp-server:fatJar -Plsp.adapter=treesitter # Mock - regex-based, for testing without native libraries ./gradlew :lang:lsp-server:fatJar -Plsp.adapter=mock -# XDK adapter stub - placeholder for future compiler / semantic integration -./gradlew :lang:lsp-server:fatJar -Plsp.adapter=xdk +# Compiler (default) - diagnostics and semantic features, with bundled XDK modules +./gradlew :lang:lsp-server:fatJar -Plsp.adapter=compiler ``` +Compiler mode is the default. Its bounded semantic features and dependency host API are described in the +[LSP server README](./lsp-server/README.md); the capability matrix below records supported requests +and remaining project/workspace limits. No external XDK installation is required. + ## Canonical Status Documents To avoid duplicating stale implementation matrices across multiple READMEs, the canonical diff --git a/lang/build.gradle.kts b/lang/build.gradle.kts index 371134a675b..cff0ebaf871 100644 --- a/lang/build.gradle.kts +++ b/lang/build.gradle.kts @@ -2,7 +2,7 @@ import com.diffplug.gradle.spotless.SpotlessCheck import com.diffplug.gradle.spotless.SpotlessExtension import org.jetbrains.kotlin.gradle.tasks.KotlinCompile -/** +/* * Root project for XTC language tooling. * * Subprojects: @@ -22,13 +22,21 @@ plugins { // ============================================================================= // Kotlin formatting // ============================================================================= -// Spotless runs ktlint with its default rules, as the ktlint Gradle plugin did. Each Kotlin -// subproject checks its own sources, tests and build script; generated and synced build outputs -// stay excluded. The lang root, tree-sitter and vscode-extension scripts were never under ktlint -// and would need a one-off reformat to join. -val ktlintVersion = libs.versions.lang.ktlint.get() +// Build scripts are owned by the lang root; each Kotlin subproject owns its sources, +// including tests and integration tests. Generated and synced build outputs stay excluded. +// Match master's Spotless integration; do not restore the separate ktlint Gradle plugin. +val ktlintVersion = + libs.versions.lang.ktlint + .get() val ci = providers.environmentVariable("CI").isPresent +spotless { + kotlinGradle { + target("*.gradle.kts", "*/build.gradle.kts") + ktlint(ktlintVersion) + } +} + allprojects { pluginManager.withPlugin("com.diffplug.spotless") { if (!ci) { @@ -46,9 +54,6 @@ subprojects { target("src/**/*.kt", "src/**/*.kts") ktlint(ktlintVersion) } - kotlinGradle { - ktlint(ktlintVersion) - } } val applyFormatting = tasks.named("spotlessApply") val checkFormatting = tasks.named("spotlessCheck") @@ -78,25 +83,26 @@ subprojects { // after modifying the language model or generators. // ============================================================================= -val updateGeneratedExamples = tasks.register("updateGeneratedExamples") { - group = "generation" - description = "Update the generated-examples directory with freshly generated files" - - dependsOn(project(":dsl").tasks.named("generateEditorSupport")) +val updateGeneratedExamples = + tasks.register("updateGeneratedExamples") { + group = "generation" + description = "Update the generated-examples directory with freshly generated files" + + dependsOn(project(":dsl").tasks.named("generateEditorSupport")) + + from(project(":dsl").layout.buildDirectory.dir("generated")) { + include("xtc.tmLanguage.json") + include("language-configuration.json") + include("xtc.vim") + include("xtc-mode.el") + include("grammar.js") + include("highlights.scm") + include("xtc.sublime-syntax") + } - from(project(":dsl").layout.buildDirectory.dir("generated")) { - include("xtc.tmLanguage.json") - include("language-configuration.json") - include("xtc.vim") - include("xtc-mode.el") - include("grammar.js") - include("highlights.scm") - include("xtc.sublime-syntax") + into(layout.projectDirectory.dir("generated-examples")) } - into(layout.projectDirectory.dir("generated-examples")) -} - // ============================================================================= // Aggregate subproject tasks // ============================================================================= @@ -111,9 +117,17 @@ val updateGeneratedExamples = tasks.register("updateGeneratedExamples") { // Turn one on with -PincludeBuildAttachIntellijPlugin=true or -PincludeBuildAttachVsCodeExtension=true, // or build it directly: ./gradlew :lang:intellij-plugin:buildPlugin val attachIntellijPlugin = - providers.gradleProperty("includeBuildAttachIntellijPlugin").orElse("false").get().toBoolean() + providers + .gradleProperty("includeBuildAttachIntellijPlugin") + .orElse("false") + .get() + .toBoolean() val attachVsCodeExtension = - providers.gradleProperty("includeBuildAttachVsCodeExtension").orElse("false").get().toBoolean() + providers + .gradleProperty("includeBuildAttachVsCodeExtension") + .orElse("false") + .get() + .toBoolean() // Projects to aggregate standard lifecycle tasks from val coreProjects = @@ -131,12 +145,13 @@ val allProjects = listOf(":dsl", ":tree-sitter", ":lsp-server", ":dap-server", ":intellij-plugin", ":vscode-extension") // Map of aggregate task -> subproject task (null means same name) -val taskMappings = mapOf( - "build" to mapOf(":intellij-plugin" to "buildPlugin"), // intellij uses buildPlugin - "assemble" to emptyMap(), - "check" to emptyMap(), - "clean" to emptyMap() -) +val taskMappings = + mapOf( + "build" to mapOf(":intellij-plugin" to "buildPlugin"), // intellij uses buildPlugin + "assemble" to emptyMap(), + "check" to emptyMap(), + "clean" to emptyMap(), + ) taskMappings.forEach { (aggregateTask, overrides) -> val projects = if (aggregateTask == "clean") allProjects else coreProjects @@ -152,14 +167,16 @@ taskMappings.forEach { (aggregateTask, overrides) -> // IDE run tasks - convenience aliases for subproject tasks // ============================================================================= -val runIntellijPlugin = tasks.register("runIntellijPlugin") { - group = "run" - description = "Launch IntelliJ IDEA with the XTC plugin loaded for testing" - dependsOn(project(":intellij-plugin").tasks.named("runIde")) -} +val runIntellijPlugin = + tasks.register("runIntellijPlugin") { + group = "run" + description = "Launch IntelliJ IDEA with the XTC plugin loaded for testing" + dependsOn(project(":intellij-plugin").tasks.named("runIde")) + } -val runVsCodeExtension = tasks.register("runVsCodeExtension") { - group = "run" - description = "Launch VS Code with the XTC extension loaded for testing" - dependsOn(project(":vscode-extension").tasks.named("runCode")) -} +val runVsCodeExtension = + tasks.register("runVsCodeExtension") { + group = "run" + description = "Launch VS Code with the XTC extension loaded for testing" + dependsOn(project(":vscode-extension").tasks.named("runCode")) + } diff --git a/lang/doc/manual-test-plan.md b/lang/doc/manual-test-plan.md index 86f045934f4..4f7e25ce70e 100644 --- a/lang/doc/manual-test-plan.md +++ b/lang/doc/manual-test-plan.md @@ -1,12 +1,173 @@ # Ecstasy Language Server - Manual Test Plan -This document describes how to manually test every feature implemented in the Ecstasy Language Server and IntelliJ plugin. +This playbook describes the compiler-backed language server and its IntelliJ and VS Code integrations. +The shared catalog contains **284 scenarios**: X1–X279, CFG1–CFG3 and 7a.8/7a.9. +The compiler adapter is enabled by default; Tree-sitter remains a selectable syntax-only adapter. +Scenario definitions, source fixtures and native assertions are retained below. -## Feature Implementation Status +## Acceptance reports -> See [plan-ide-integration.md](plans/plan-ide-integration.md) for the canonical feature implementation matrix comparing Mock, Tree-sitter, and Compiler adapter capabilities. +Run both native harnesses from the repository root: ---- +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true +./gradlew :lang:vscode-extension:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true +``` + +Each editor writes per-case results under its `build/reports/compiler-playbook/` directory. +IntelliJ also records IDE failures, screenshots and server traces; VS Code records extension errors +and the isolated workspace. Judge acceptance from those reports, including their catalog hash, +selected IDs, skipped/not-selected cases and errors. Historical selected runs do not establish +acceptance of a later complete build. The development diary and extraction plans are not required +to build or test this product. + +IntelliJ additionally tests startup editing, focus recovery, settings persistence and project +lifecycle. Its large-file probe is opt-in. Run the editors sequentially because native actions +use desktop focus. Their profiles are isolated from personal IDE settings. Timeouts and unexpected +notifications are failures; do not replay a timed-out mutation to manufacture a pass. + +## Client compatibility limits + +- VS Code's native batch-move and overlapping-move Undo paths have recorded host failures + (X130/UP16 and X218/UP23). The cases remain assertions, not silently skipped successes. +- IntelliJ uses the supported read-only file fallback for library content (X254/UP25). + X257/UP26 covers inline-completion constraints partly through the installed connection because + released LSP4IJ does not expose all required native invocation metadata. Both report partial coverage. +- The optional color-value prototype is disabled by default. X273–X275 explicitly enable it; + its native swatch refresh has a recorded VS Code failure. Theme-based source highlighting is + separate and enabled by default. +- Error notifications are intentional in the negative import/settings cases X261/X262, + X268–X270 and failed JVM launch X272. Their retention and recovery assertions must still pass. + +## Server process lifecycle acceptance + +Run this with the shipped Tree-sitter backend as well as compiler mode. Start from a recorded +process baseline so existing old servers and another IDE's workspace are not counted as leaks. + +1. Open an Ecstasy project and several `.x` files. In IntelliJ's Language Servers panel, record + the XTC server PID. Check `ps -axo pid,ppid,command | grep '[x]tc-lsp-server.jar'` on macOS/Linux. +2. Restart that server several times through the panel. Each previous PID must disappear; + the final active server must still provide completion and diagnostics. +3. Close the project during a fresh server startup, then reopen it. Cancelled startup must + not leave an extra server behind. Repeat once after the workspace finishes indexing. +4. Quit the test IDE. Its recorded server PIDs must disappear within ten seconds. Reopening + the IDE must not be needed to reap them. Do not terminate another workspace's server. + +The native project-lifecycle tests exercise ownership and process exit. Check their reports +separately from the feature catalog; old processes from another run are not evidence of a leak +in the current run. + +## IntelliJ startup editing acceptance (L56) + +Run in a fresh compiler-mode project before waiting for the Language Servers panel to report ready. +The unit tests cover notification permutations; these steps exercise the actual client/editor path. + +1. Open a valid `.x` module and immediately type a deliberate unknown name in a method body. + Problems must eventually show the diagnostic for the current buffer. Repair it while startup + is still progressing; stale initial text must not bring the diagnostic back. +2. During a fresh startup, replace a long module containing several foldable methods with a short + valid module. Folds must use the shortened text, with no line/offset exception in `idea.log`. +3. Close and reopen that file during startup, then edit it again. The final buffer must receive + current diagnostics and folds; a delayed close must not retire the reopened document. +4. Repeat with a bulk replacement and with an unsaved edit made before the server initializes. + Record the server PID, final document version, diagnostic state and IDE-error log. + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + --tests '*CompilerPlaybookTest.startupEditing' \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +The report includes `startup-editing.json` (PID/version/text stamps and phase results) and +`server-trace/` (one execution trace per child process). X103's forward/reverse file rename also +passes after removing the transport read lock that caused a real EDT deadlock. This regression +was unrelated to focus loss. X42/X97/X108 contain the shared L57–L59 semantic variants. + +## Current development batch: workspace queries and editing + +For the new workspace queries, add an unopened Consumer subclass overriding Library's method. +From Library, check implementations and subtype hierarchy, then incoming calls from Consumer. +Edit Consumer on disk and verify a previously opened hierarchy does not silently reuse stale edges. +Rename an inline Library class, static function or static property and inspect the versioned edits +in both modules. In a compiling file with an unused ordinary import and a used aliased import, +request import cleanup: only a removal that preserves compilation/bindings should be offered. +Sort adjacent imports and verify comments remain in place. + +For compiler editing features, format a file containing CRLF, string spaces and URL comments; +only indentation/outer whitespace should change. Format a selected range, then type a closing +brace and check its indentation. An unterminated string must yield no formatting edits. Ctrl-click +an HTTP(S) URL in a comment/string. Check the module Run lens uses the normal client run action. +Linked editing on a local should include that declaration's uses while ignoring an equal-spelled +local in another method. Semantic highlighting should preserve lexical comments/literals/keywords +as well as resolved names; inlays retain their existing compiler-derived scope. + +## Current development batch: discovery and complete XDK + +Also compile `module UsesXml { package xml import xml.xtclang.org; void accept(xml.Document doc) {} }` +without external XDK paths. XML must resolve from the production bundle, while rename on `Document` +remains unavailable because it is a binary library declaration. The full XDK, including XML/JSONDB +and the other distribution libraries, is now shared by production and compiler tests. + +## Acceptance checklist for the 2026-09-30 batch + +Run in a disposable compiler-mode workspace using the build/run instructions below. Keep user +projects out of destructive move/rename tests. For each editor record commit, catalog hash, selected +IDs, pass/fail/not-run, IDE errors, language-server errors and logs. A timeout is a maximum wait +for a response/state transition, not a prescribed pause. Do not silently retry a completed edit +or Undo against an already modified fixture. + +| Check | Exact exercise and required result | Existing automation / remaining evidence | +| --- | --- | --- | +| External resource creation | Run X124 with the editor continuously focused. Create missing `generated/assets` and the resource from an external terminal; delete/recreate it, then switch to a missing replacement root. Problems must update without opening those folders or invoking Refresh. | Strengthened X124 passes in both drivers without manual refresh. | +| External source changes | Put `Library.x` outside the workspace, configure Library and a local Consumer explicitly, open only Consumer, then change Library's return type on disk from Int to String and back. Problems must appear/clear and Definition must still find Library. Repeat after deleting/recreating the external source directory. | X134 passes in both drivers for unopened external source edit, delete and recreation. Directory deletion/recreation remains an additional manual check. | +| Watch ownership and idle work | Configure two modules sharing an external root. Remove one, verify the other still updates; replace the last reference, then edit the retired root. Close/reopen the project and restart the server. Verify no obsolete diagnostic publication, duplicate subscription, growing server-process count or repeated idle compiler jobs. | Lease/coalescing/disposal unit tests; manual lifecycle/idle observation still required. A scheduled VFS refresh is not itself a compilation. | +| Batch container move | Run X130: select both module containers, move them into `destination`, keep members/resources closed, verify the consumer, then Undo once and Redo once. All paths and bytes must match the shared fixture in each state. | X130 passes in both drivers; IntelliJ drives native Move with project Undo/Redo and VS Code drives Explorer batch Cut/Paste with its Undo/Redo. Explorer drag/drop is a separate manual check. | +| Single-file and package moves | Run X128's file/package Rename and X118's explicit-graph module rename. Also move one source to an existing sibling directory through native Move; verify references, resources and one Undo/Redo. | X118/X128/X161–X163 shared; type qualification, closed consumers, companion resources and persisted graph paths. | +| Collision and invalid destinations | Repeat Move with an existing file, existing directory, missing destination parent, target inside its own source, overlapping parent/child selection and duplicate target names. Cancel the dialog. Each rejected/canceled operation must leave every source, setting and path unchanged. | `FileMoveTargetsTest` covers filesystem validation; dialog/error presentation and cancellation still need manual checks. | +| Source changes during proof | Start a move/rename on a larger fixture, edit an affected open buffer or create a destination collision while proof is pending, or cancel the operation. A stale result must not overwrite the new text/path. The next fresh operation must work. | Existing snapshot/epoch regressions and X57/X118; move-specific concurrent timing is not claimed by X130. Use tracing or deterministic backend barriers when the race cannot be reproduced manually. | +| Deliberately refused operations | Try moving and renaming a module root simultaneously and a type move into another module; also try a read-only bundled-library declaration. Verify the reason, unchanged files/settings and responsive IDE. | Existing refusal tests/X101 and manual checks. Normal LSP null responses cannot veto arbitrary host file moves; compiler preflight claims apply to registered native actions. | +| Lazy action listing and application | Run X105, X122 and X127. Opening/canceling intentions or resolving an action must not edit the document. Selecting the import/cleanup action must clear the intended diagnostic; one Undo and Redo must restore exact text and Problems. | Shared cases pass in both installed clients, including IntelliJ lazy-action application and Undo/Redo. | +| Other lazy resolvers | Run X131, then inspect the native Run lens, comment URL, inferred type/parameter inlays and workspace symbol navigation. Initial/deferred properties must agree, positions and command arguments survive, and source edits invalidate old handles. | X131 is protocol acceptance. Existing feature UI scenarios cover presentation; it is not evidence that a tooltip/popup was displayed for every resolved property. | +| Resolver lifetime | Resolve handles after a dependency/configuration change, close/reopen or server restart; send a foreign/evicted handle through a protocol test. Require a stale/invalid response, no stale edit and no leaked graph retained by handles. Eager clients must still receive complete payloads. | Resolve-store/service regressions; X127/X131 cover source-edit expiration only. Inspect test coverage before counting the other transitions complete. | +| Normal save and formatting | Run X132, save a dirty file and verify disk/Problems; exercise whole-file, selection and on-type formatting with editor preferences both enabled and disabled. Saving must not unexpectedly reformat or block behind compilation. | X132 checks overlapping/disjoint range responses and negotiated default save hooks; existing formatting scenarios cover normal UI. Native save-on-large-compilation remains a manual responsiveness check. | +| Incremental text transport | Run `DocumentSynchronizationTest`: sequential/mixed full-and-range changes, UTF-16 surrogate pairs, CRLF/bare CR, old versions, invalid batch atomicity and Full-mode refusal. The packaged stdio test also negotiates incremental transport. Change the plugin's Full/Incremental preference with an unsaved file. | Backend/packaged tests and shared X137 cover the wiring. Full remains default. This is not incremental compiler support. | +| Opt-in save edits and range failures | Test `xtcDocumentSync.formatOnSave` independently from incremental transport; verify no extra compilation, unchanged server buffer until client didChange, malformed/reversed/out-of-document ranges and duplicate/conflicting formatting edits. Cancel/stale requests must not apply edits. | Backend tests plus X132's default mode. Opt-in native save UI, conflict/stale/cancel coverage and real transport coverage must have their own receipts; do not infer them from a default no-op. | +| Terminology and compatibility | Inspect server panels, status bar, commands, Move/Rename dialogs, startup/error messages, templates and playbook progress. Labels should say Ecstasy; `.x`, `.xtc`, `xtc.*`, environment names and class/file prefixes remain technical identifiers. | Packaging/manifest checks plus visual inspection. Search production workarounds for `// TODO LSP4IJ:`; removal conditions must name the behavior to revalidate. | +| Configuration UI follow-up | Verify Apply/Cancel/Reset, user/project inheritance, multi-root scope, invalid-value retention, restart-required settings, live refresh and absence of duplicate format-on-save. | X136–X139 cover the implemented local settings subset. Physical dialog navigation, multi-root and fresh-IDE persistence remain manual checks in the settings acceptance section below. | + +Focused automated gate after compilation/unit checks: CFG2, X57, X101, X105, X118, +X122–X134 in each host, plus the existing native lens/link/inlay/workspace-navigation checks +when their display path changed. Run both newly added and modified tests. A full catalog checkpoint +and longer idle/restart/memory runs remain the submission gate under L82; selected cases alone +cannot establish full playbook completion. + +Backend command for the save/sync/range checks (no editor window): + +```bash +./gradlew :lang:lsp-server:test --tests '*DocumentSynchronizationTest' \ + -Plsp.adapter=compiler -PincludeBuildLang=true -PincludeBuildAttachLang=true \ + --rerun-tasks --no-build-cache +``` + +Configuration examples belong in a custom client's initialization payload, **not** in an editor's +settings file until UI5 wires a setting to them: + +```json +{"initializationOptions":{"xtcDocumentSync":{"incremental":true,"formatOnSave":false}}} +``` + +Full transport and no server save edits are the defaults. Native editor format-on-save may already +invoke normal formatting; enabling a second save-formatting path must not apply formatting twice. + +## Feature Implementation Status + +The [server capability matrix](../lsp-server/README.md) describes compiler and fallback behavior. +The scenarios below give concrete inputs, expected edits, refusal boundaries and native coverage. +The compiler adapter consumes compilation results, semantic facts and incomplete-source queries; +its editor features require all of those foundations. Native client limitations remain explicit +in the [compatibility list](#client-compatibility-limits) and per-case reports. ## Pre-Test Setup @@ -15,13 +176,29 @@ This document describes how to manually test every feature implemented in the Ec > **Note:** All `./gradlew :lang:*` commands require `-PincludeBuildLang=true -PincludeBuildAttachLang=true` when run from the project root. ```bash -# Build with tree-sitter adapter (recommended for full functionality) +# Build with the explicit tree-sitter syntax-only adapter ./gradlew :lang:lsp-server:build -Plsp.adapter=treesitter -# Or build with mock adapter (default, no native dependencies) +# Build the default XdkAdapter - compiler diagnostics and semantic IDE features +./gradlew :lang:lsp-server:build -Plsp.adapter=compiler + +# Or with mock adapter (no native dependencies) ./gradlew :lang:lsp-server:build -Plsp.adapter=mock ``` +**The compiler adapter uses the full matching XDK library set bundled with the server.** Gradle builds +them through the composite module dependencies and packages them as resources. No external XDK or +`XDK_HOME` setting is required for compiler analysis. A Kotlin host API can now supply additional +dependency artifacts/source indices and explicit source roots/edges for automatic recompilation. +Workspace folders automatically supply discovered source roots/import edges. VS Code exposes an +explicit override through `xtc.compiler.sourceModules` (`null` restores discovery, `[]` disables it). +The ordinary fixtures below need only bundled libraries; the source-project checks also exercise +the explicit settings shown in the XdkAdapter playbook. + +It is also the slow one, deliberately: the first compilation in a session takes about a second +(class loading, reading the XDK, a JIT still warming up) and then settles to about 60ms. If the +first file you open seems to hang for a moment, that is what it is. + ### 2. Launch in Your Editor **IntelliJ:** @@ -49,15 +226,33 @@ Requires `JAVA_HOME` or `XTC_JAVA_HOME` pointing to Java 25+. ### 3. Verify Which Adapter is Active +The server runs out of process and writes its own log to `~/.xtc/logs/lsp/server-*/server.log`, for both +editors. It announces itself on startup: + +``` +======================================== +Ecstasy Language Server v +backend: Tree-sitter +log file: /Users/you/.xtc/logs/lsp/server--/server.log +======================================== +``` + +The `backend:` line is the answer: +- `backend: Tree-sitter` - tree-sitter is active +- `backend: Ecstasy Compiler` - the real compiler is active +- `backend: Mock` - mock adapter is active +- `backend: Mock` **together with** `tree-sitter was requested but failed to initialize` - + tree-sitter was asked for and fell back. The two lines together are the fallback; the `backend:` + line alone does not say whether it was chosen or settled for + +Clients can also ask over JSON-RPC (`xtc/healthCheck`), which answers with `adapter` set to +`TreeSitter`, `XDK` or `Mock`. + **IntelliJ:** -1. Open IntelliJ → Help → Show Log in Finder/Explorer -2. Search for `"Selected adapter:"` or `"XTC LSP Server started"` -3. You should see one of: - - `"Selected adapter: TreeSitterAdapter"` - Tree-sitter is active - - `"Selected adapter: MockAdapter"` - Mock adapter is active - - `"Selected adapter: MockAdapter (fallback - ...)"` - Tree-sitter failed -4. Also verify: `"semantic tokens ENABLED (23 types, 10 modifiers)"` in the log -5. For IntelliJ plugin runs from this repo, also verify the startup command line in the IDE log: +1. Help → Show Log in Finder/Explorer for the IDE-side log, or read the server log directly +2. Find the banner above +3. For Tree-sitter, also verify: `"semantic tokens ENABLED (23 types, 10 modifiers)"` in the log +4. For IntelliJ plugin runs from this repo using Tree-sitter, also verify the startup command line in the IDE log: - `XTC LSP command configured` - `-Dxtc.lsp.semanticTokens=true` This confirms you are exercising the branch's semantic-token path rather than a stale fallback. @@ -65,9 +260,21 @@ Requires `JAVA_HOME` or `XTC_JAVA_HOME` pointing to Java 25+. **VS Code:** 1. Open Output panel (Ctrl+Shift+U / Cmd+Shift+U) 2. Select "Ecstasy Language Server" from the dropdown -3. Look for `"Backend: TreeSitter"` or `"Backend: Mock"` +3. Find the same banner + +**With the compiler adapter**, every compilation also logs what it cost and what the compiler is +holding on to, which is the quickest way to tell it is really running: -### 3. Create Test File +``` +XdkAdapter - compile: uri=file:///X.x, 106 bytes, 1 diagnostic(s), 3 symbol(s), queue=1, + waited 160us, compiled in 64ms [modules=24, constants=207196, invalidations=10, heap=281MB] +``` + +`queue` is how many documents are waiting: compilations are serialised, because the compiler was +not written for two at once. `waited` growing while you type is the sign that serialising has +started to hurt. + +### 4. Create Test File Create a file named `TestModule.x` with this content: @@ -105,6 +312,48 @@ module TestModule { ## Test Cases by Feature +> **"Both adapters" means mock and tree-sitter**, which is how this document was written when +> there were two. The compiler adapter now answers diagnostics (§7), the outline (§6), hover +> (§2), go-to-definition (§4), find-references (§5), document highlights (§8), selection ranges +> (§9) and folding (§10), plus type hierarchy. Definition queries span the discovered/configured source graph; +> references also span discovered/configured source graphs, including unopened consumers. Workspace-symbol +> search compiles unopened modules on demand. +> +> Compiler completion and signature help are available for the supported cursor contexts, along +> with type-definition, type/method implementation lookup, static call hierarchy, resolved-name +> semantic tokens, read/write highlights, bounded inlay hints and validated local/private-parameter +> rename, plus graph-backed type, static-member, ordinary method/property-family and alias rename +> (versioned-edit clients; see section I). Bounded formatting, proven import cleanup, module run +> lenses, HTTP(S) links and local linked editing are advertised with the limits listed above. Definition/type-definition +> and inherited implementation bodies can also resolve into explicitly host-indexed dependencies; +> the bundled XDK supplies matching read-only source targets automatically, while additional +> host dependency artifacts require explicit configuration. Compiler mode stays Java-only. +> +> Use the [XdkAdapter playbook](#xdkadapter-playbook) for a complete compiler run, including fixtures +> that compile and precise expectations for compiler-only features. §7a adds diagnostic stress checks. + +### Compiler module sessions and hierarchy + +With the compiler backend, create `Project.x` containing `module Project { class Base {} }` and +`Project/Child.x` containing `class Child extends Base {}`. + +| Check | Action | Expected | +|---|---|---| +| Member diagnostics | Open Child.x; change Base to Missing in its extends clause | A compiler diagnostic points into Child.x, with no generic internal error on Project.x. | +| Sibling invalidation | Restore Child.x; rename Base in the open Project.x buffer | Child.x is reanalysed and its diagnostic updates even without an edit there. | +| Unsaved member | Open a new `Project/pkg/Added.x` buffer containing `class Added extends Base {}` | It joins the module without being saved; no temporary source files appear on disk. | +| Cross-file navigation | Navigate from Base in Child.x; find references on the Base declaration | Definition points into Project.x; references include Child.x. Highlights remain document-local. | +| Hierarchy | Prepare hierarchy on Base and expand its subtypes; inspect Child's supertype | Child and Base point to their own files. A generic parent retains its type arguments. | +| Close overlay | Introduce an error in Child.x, then discard and close its buffer while Project.x stays open | The disk version replaces the overlay; obsolete diagnostics clear. | +| Membership | Create an invalid member on disk, then delete it | File notifications refresh the module and clear the deleted file's diagnostics. | +| Broken syntax | Remove a member's closing brace, then restore it | Current outlines/folding remain available, including sibling files. Semantic navigation clears until correction. | +| Incomplete statement | Type `console.` inside a method and remove the closing braces | The method/module outline and enclosing selection ranges survive; compiler diagnostics remain, and no semantic definition is invented for the broken expression. | + +These checks exercise module source files, not workspace dependency builds or conditional-mixin +hierarchy. Dependency source navigation needs an explicit host-supplied artifact/source index; +the [host API checks](#dependency-host-api-checks) below cover that boundary. The XdkAdapter playbook +also covers method-implementation lookup. + ### 1. Syntax Highlighting (TextMate) **Provider:** TextMate grammar (NOT tree-sitter or LSP) @@ -121,7 +370,9 @@ module TestModule { | 1.6 | Editor color scheme sanity | Open a `.x` file in IntelliJ | Editor background matches the active theme (not a solid white fallback) | | 1.7 | TextMate + semantic token layering | Open a `.x` file with types, methods, and annotations | Base TextMate colors remain sane; semantic tokens refine symbols instead of washing out the theme | -**Note:** Semantic tokens Tier 1 (declaration-site classification, type refs, annotations, calls) is implemented. Tier 2+ (distinguishing field vs local vs parameter at usage sites) requires compiler integration. +**Note:** Tree-sitter supplies syntax-based semantic tokens. The default compiler adapter additionally +classifies resolved usage sites as properties, locals or parameters and supplies semantic modifiers; +see X41 in the compiler playbook. TextMate remains the lexical coloring layer. --- @@ -149,7 +400,7 @@ module TestModule { **LSP Method:** `textDocument/completion` **Status:** ⚠️ Partial (Mock not context-aware; tree-sitter scope-aware in BODY context) -**Works with:** Both adapters (tree-sitter better) +**Works with:** All three (the compiler adds the type, which no grammar can) **How to trigger:** - *IntelliJ:* Ctrl+Space (Basic Completion), or type and wait for auto-popup @@ -175,7 +426,7 @@ module TestModule { **LSP Method:** `textDocument/definition` **Status:** ✅ Done (scope-aware same-file + cross-file via workspace index) -**Works with:** Both adapters (cross-file: tree-sitter only) +**Works with:** All three (compiler: resolved identities across the current module, including closed member files) **How to trigger:** - *IntelliJ:* Ctrl+Click on a symbol, or Ctrl+B, or F12 @@ -194,7 +445,7 @@ module TestModule { | 4.9 | Forward reference not resolved | Method body where a usage of `name` precedes a local declaration of `name`. Ctrl+Click on the usage | Resolves to module-level / outer-scope / workspace `name`, NOT the forward-declared local | | 4.10 | Doc-commented target | Ctrl+Click on a class/method/property preceded by a `/** ... */` doc comment | Cursor lands on the declaration line (e.g. `class Foo {`), NOT on the `/**` opener | -**Notes:** +**Tree-sitter notes:** - Resolution order is: enclosing-scope locals/parameters → class/module members → same-file top-levels → cross-file workspace index. - Cross-file definition uses workspace index fallback only when scope-aware resolution finds nothing. - Import-path-based resolution is not yet implemented. @@ -205,7 +456,7 @@ module TestModule { **LSP Method:** `textDocument/references` **Status:** ⚠️ Partial -**Works with:** Tree-sitter (Mock limited) +**Works with:** Tree-sitter, and the compiler (current module, by resolved identity). Mock limited **How to trigger:** - *IntelliJ:* Alt+F7 (Find Usages), or right-click → Find Usages, or Shift+F12 @@ -225,7 +476,7 @@ module TestModule { **LSP Method:** `textDocument/documentSymbol` **Status:** ✅ Done -**Works with:** Both adapters +**Works with:** All three adapters **How to trigger:** - *IntelliJ:* Alt+7 (Structure tool window), Ctrl+F12 (File Structure popup) @@ -236,34 +487,106 @@ module TestModule { | 6.1 | Structure view | IntelliJ: Alt+7; VS Code: Outline panel | Hierarchical outline | | 6.2 | File structure popup | IntelliJ: Ctrl+F12; VS Code: Ctrl+Shift+O | Popup with all symbols | | 6.3 | Breadcrumbs | Look at editor top (VS Code) or bottom (IntelliJ) | `TestModule > Person > getName` | +| 6.4 | Outline on the compiler adapter | Same as 6.1, built with `-Plsp.adapter=compiler` | Same hierarchy. It comes from the parsed AST, not the compiled structures, so it appears for a file that does not compile | +| 6.5 | Outline of a file with errors | Give a non-void method a bare `return;`, then Alt+7 / Outline | `COMPILER-41: Return is supposed to be non-void.`, and the outline still lists every declaration. A structure view that empties out on a typo is the bug this guards | --- ### 7. Diagnostics / Error Detection **LSP Method:** `textDocument/publishDiagnostics` -**Status:** ⚠️ Partial +**Status:** ✅ Done (compiler adapter) / ⚠️ Partial (others) **Works with:** Different behavior per adapter **How to trigger:** Diagnostics appear automatically as you type (push-based). - *IntelliJ:* Red/yellow squiggly underlines; Alt+Enter for quick fixes; F2 to jump to next error - *VS Code:* Red/yellow squiggly underlines; Ctrl+Shift+M (Problems panel); F8 to jump to next error -| # | Test | Steps | Mock | Tree-sitter | -|---|------|-------|:----:|:-----------:| -| 7.1 | Syntax error (missing brace) | Delete a `}` | ❌ | ✅ | -| 7.2 | Unmatched braces | Add `{` without `}` | ⚠️ | ✅ | -| 7.3 | ERROR comment marker | Add `// ERROR: message` | ✅ | N/A | -| 7.4 | WARN comment marker | Add `// WARN: message` | ✅ | N/A | -| 7.5 | Semantic error (undefined var) | Use undefined variable | ❌ | ❌ | -| 7.6 | Module-level property getter parses cleanly | At module scope (outside any class) write `Int val2.get() = 43;`. Same form inside a class body should also parse | N/A | ✅ no diagnostic | -| 7.7 | Package-level property getter parses cleanly | Inside `package util { Int answer.get() = 42; }` | N/A | ✅ no diagnostic | +| # | Test | Steps | Mock | Tree-sitter | Compiler | +|---|------|-------|:----:|:-----------:|:--------:| +| 7.1 | Syntax error (missing brace) | Delete a `}` | ❌ | ✅ | ✅ with the compiler's own code | +| 7.2 | Unmatched braces | Add `{` without `}` | ⚠️ | ✅ | ✅ | +| 7.3 | ERROR comment marker | Add `// ERROR: message` | ✅ | N/A | N/A | +| 7.4 | WARN comment marker | Add `// WARN: message` | ✅ | N/A | N/A | +| 7.5 | Semantic error (undefined var) | `Int y = x + 1;` with no `x` in scope | ❌ | ❌ | ✅ `COMPILER-38: Name "x" is unresolvable.` | +| 7.6 | Module-level property getter parses cleanly | At module scope (outside any class) write `Int val2.get() = 43;`. Same form inside a class body should also parse | N/A | ✅ no diagnostic | ✅ no diagnostic | +| 7.7 | Package-level property getter parses cleanly | Inside `package util { Int answer.get() = 42; }` | N/A | ✅ no diagnostic | ✅ no diagnostic | +| 7.8 | Type error | `String s = 1;` | ❌ | ❌ | ✅ `COMPILER-43: Type mismatch: "String" expected, "IntLiteral" found.` | +| 7.9 | Wrong argument count | Call a one-argument method with two | ❌ | ❌ | ✅ `COMPILER-56: Could not find a matching method or function "f" ...` - the compiler reports no *matching* method rather than a count | +| 7.10 | Codes are the compiler's | Any of 7.1-7.9 | - | - | The code shown is the one `xcc` prints for the same file: `PARSER-*`, `COMPILER-*`, `VERIFY-*` | **Notes:** - Mock: Detects `// ERROR:` and `// WARN:` comment markers (testing convenience) - Tree-sitter: Real syntax error detection via parsing (doesn't use comment markers by design) -- Comment markers: N/A for tree-sitter because it focuses on real parse errors -- Semantic errors: Requires compiler adapter (future) +- Comment markers: N/A for tree-sitter and the compiler, because both report real problems +- Compiler: the same diagnostics `xcc` would print, at the same spans. A row it disagrees with + `xcc` about is a bug worth reporting either way - the two are meant to be the same compiler + +--- + +### 7a. Compiler Adapter Specifics + +**Status:** ✅ Done +**Works with:** Compiler adapter only (`-Plsp.adapter=compiler`) + +These are the behaviours that only exist because the adapter runs the real compiler. Nothing here +is observable under mock or tree-sitter. + +**Prerequisite:** a server built with `-Plsp.adapter=compiler`; its module resources must be present. + +| # | Test | Steps | Expected Result | +|---|------|-------|-----------------| +| 7a.1 | No external XDK | Unset `XDK_HOME`, restart the server, open a complete module | Real compiler diagnostics; correcting the source clears them | +| 7a.2 | Invalid external XDK | Set `XDK_HOME` to a nonexistent directory, restart and edit the file | The bundled libraries still supply compiler analysis | +| 7a.3 | Cold start | Watch the log on the first `.x` file opened in a session | Compare the first and subsequent `EmbeddingSupport.compileModule` durations in the server trace, including queue wait separately. Workspace diagnostics may compile before the first editor request; a first editor compile is not necessarily cold | +| 7a.4 | Steady state | Edit the same file ten times, watching `compiled in` | Settles to tens of milliseconds. A number that keeps climbing means something is accumulating - compare `footprint` across the run | +| 7a.5 | Queue depth and contents | Type quickly across two or three open files | In the execution trace, inspect `queueSize` and FIFO `queuedJobs`, plus `debouncingJobs` and `runningJobs`. Job IDs, operation names and URIs show what is waiting and running; `waitMs` and `runMs` separate delay from execution. | +| 7a.6 | Superseded edit | Type continuously for several seconds without pausing | Log shows `superseded before it started, skipped` or `superseded after ..., abandoned`. No diagnostics are published for text that has already been replaced - a squiggle under an identifier you have finished typing is the failure this prevents | +| 7a.7 | Memory over a session | Leave the server up, edit for a while, watch `heap=` in the `footprint` on each line | Normal allocation/GC produces a sawtooth. Record sustained growth across repeated collections or after closing files; a single increasing sequence is not proof of retained compiler state. | +| 7a.8 | A diagnostic no other adapter can find | See the duplicate-annotation file below | One `WARNING VERIFY-75`, the annotation is ignored | +| 7a.9 | A file that has gone badly wrong | Paste a hundred lines of non-Ecstasy text into a `.x` file | Diagnostics stop at a hundred serious errors rather than filling the panel with consequences of the first one | +| 7a.10 | References follow meaning, not spelling | Two classes each with a property `x`; Shift+F12 on one | Only that class's `x`. A text search cannot do this, and neither can a grammar | + +Execution trace files are `~/.xtc/logs/lsp/server--/lsp-trace--.jsonl`; native IntelliJ runs +save them in `run-*/server-trace/`. They include compiler queue counts and lists, javatools phase +and API timings, process/thread IDs, and server request-to-reply latency. Use the +[trace guide](../lsp-server/README.md#compiler-queue-and-api-timing) for field meanings and controls. +The service-started notification uses the title **Ecstasy Language Server Started**, with separate +**Version**, **Adapter** and **Process ID** lines in the normal themed body style. Its balloon fades +after roughly eight seconds unless being interacted with; the notification and server log remain +available afterward. +| 7a.11 | Definition of a method call | F12 on `p.sum()` | Jumps to `sum`'s declaration. The name in a call resolves to nothing by itself - which method it is depends on the target and the arguments - so this is the compiler's answer, not a name match | +| 7a.12 | Definition of something from the core library | F12 on `Int` or `Console` | Nothing happens. It resolves perfectly well and this document has nowhere to point at; jumping to another mention of `Int` in the same file would be worse than doing nothing | +| 7a.13 | Hover shows a type | Hover over a variable in an expression | The declaration, and the type the compiler decided. On a document that does not compile the type may be absent - an expression only has one once it has been validated | +| 7a.14 | Compiler completion and call hints | Run X6–X19 in the [XdkAdapter playbook](#xdkadapter-playbook) | Accessible members, visible scope and applicable call candidates come from compiler validation; completing the expression clears normal diagnostics. | + +**7a.8 - the duplicate annotation.** This is the case worth keeping, because it is invisible +everywhere else. No grammar can find it: it needs the compiler to lay `Derived` over `Base` and +notice the annotation was already there. + +```xtc +module DupAnno { + class Base { + @Atomic Int x = 1; + } + class Derived extends Base { + @Atomic @Override Int x = 2; // VERIFY-75: duplicates the base property's annotation + } +} +``` + +Expected: `WARNING VERIFY-75`, naming the annotation, the property and the derived class, and +saying the annotation on the derived property is ignored. + +> Exactly one warning, not two. The compiler reports this one twice internally - once when the +> type is laid out, once when a later stage asks for the same type again - and the adapter +> collects through an `ErrorList`, which is what filters the repeat. Two identical warnings in the +> Problems panel means that filtering has been lost. + +> Compiling the same file with `xcc` on **master** prints nothing at all - the warning is +> produced and then discarded before anyone sees it. This is a real bug in master, recorded in +> [acceptance reports](#acceptance-reports); the adapter showing it is the fix working, not a false +> positive. --- @@ -271,7 +594,7 @@ module TestModule { **LSP Method:** `textDocument/documentHighlight` **Status:** ✅ Done -**Works with:** Both adapters +**Works with:** All three (the compiler matches resolved identity within the document) **How to trigger:** - *IntelliJ:* Click on any identifier — other occurrences highlight automatically @@ -287,13 +610,17 @@ module TestModule { | 8.6 | Read highlight | Click on `x` in `return x;` | Usage site shows as **read** highlight | | 8.7 | Assignment write | Click on `age` in `age = newAge;` | Assignment target shows as **write** highlight | +Rows 8.5–8.7 describe Tree-sitter's read/write classification. The compiler also distinguishes reads +and writes by resolved identity, but declarations receive TEXT highlights; an assignment or compound +assignment target receives WRITE. Use X41 for the compiler-specific expectations. + --- ### 9. Selection Ranges (Smart Select) **LSP Method:** `textDocument/selectionRange` -**Status:** ✅ Done (tree-sitter only) -**Works with:** Tree-sitter adapter +**Status:** ✅ Done +**Works with:** Tree-sitter, and the compiler (it expands out through the parsed tree) **How to trigger:** - *IntelliJ:* Ctrl+W (Expand) / Ctrl+Shift+W (Shrink) @@ -311,7 +638,7 @@ module TestModule { **LSP Method:** `textDocument/foldingRange` **Status:** ✅ Done -**Works with:** Both adapters +**Works with:** All three (the compiler folds blocks and declarations that span more than a line) **How to trigger:** - *IntelliJ:* Click the fold/unfold arrows in the editor gutter (left margin); Ctrl+Shift+Minus (fold all) / Ctrl+Shift+Plus (unfold all) @@ -372,8 +699,16 @@ module TestModule { ### 13. Document Formatting **LSP Method:** `textDocument/formatting` + `textDocument/rangeFormatting` -**Status:** ✅ Done -**Works with:** Both adapters +**Status:** Implemented with bounded formatting rules; not a complete style formatter. +**Works with:** Tree-sitter and compiler adapters (Mock only cleans whitespace). + +Compiler mode uses the Java compiler lexer to indent blocks and delimiter/operator continuations, +trim trailing whitespace and optionally insert the final newline. It wraps expressions/lists at safe +token boundaries using the configured margin and shifts standalone block-comment margins together. +Literal/template contents and relative comment layout are preserved. Failed lexing or changed +code/literal tokens refuse the edit. Operator-spacing normalization, declaration alignment, literal +splitting, comment reflow and brace relocation are not implemented. Range and save-time requests use +these rules; on-type formatting only indents and cleans whitespace. See X102/X107/X132/X249/X250. **How to trigger (full document):** - *IntelliJ:* Ctrl+Alt+L (Reformat Code) @@ -386,9 +721,9 @@ module TestModule { | # | Test | Steps | Expected Result | |---|------|-------|-----------------| | 13.1 | Remove trailing whitespace | Add spaces at end of a line, format | Trailing whitespace removed | -| 13.2 | Insert final newline | Remove final newline from file, format | Final newline added | +| 13.2 | Insert final newline | Enable final-newline insertion in the editor, remove the final newline from the file, format | Final newline added | | 13.3 | Range format | Select 2-3 lines with trailing spaces, format selection | Only selected lines cleaned | -| 13.4 | No-op on clean file | Format a file with no trailing whitespace | No changes | +| 13.4 | No-op on clean file | Format a correctly indented file with no trailing whitespace and the configured final newline | No changes | --- @@ -396,11 +731,12 @@ module TestModule { **LSP Method:** `textDocument/onTypeFormatting` **Status:** ✅ Done -**Works with:** Tree-sitter adapter only +**Works with:** Tree-sitter and compiler adapters, with different formatting breadth. -The LSP server uses tree-sitter AST context to auto-indent as you type. Trigger characters -are `Enter`, `}`, and `;`. This is strictly better than regex-based TextMate indentation -because it understands nesting depth, continuation lines, and string literals. +Tree-sitter mode uses AST context for auto-indentation. Compiler mode uses the Java lexer for +current-line indentation/whitespace; it does not use Tree-sitter. Trigger characters are `Enter`, +`}`, and `;`. The cases below describe intended editing behavior; bounded compiler formatting +and native IntelliJ Enter handling must be checked separately (X102/X107). **How it works:** Automatic — indentation is adjusted immediately when you type a trigger character. No manual action needed. @@ -465,25 +801,25 @@ Code Style settings for XTC appear under Settings > Editor > Code Style > Ecstas **Provider:** `XtcLanguageClient` (workspace/configuration) + `XtcLanguageServer` **Status:** ✅ Done -**Works with:** IntelliJ only (VS Code falls back to LSP `FormattingOptions`) +**Works with:** IntelliJ Code Style and VS Code `xtc.formatting.*` preferences; clients that omit workspace formatting configuration fall back to LSP `FormattingOptions`. IntelliJ Code Style settings are forwarded to the LSP server via `workspace/configuration` at startup. Changes are pushed via `workspace/didChangeConfiguration`. The server uses -these settings for on-type formatting when no project-level `xtc-format.toml` is present. +these settings for formatting. Project-level `xtc-format.toml` loading is not implemented. **How to verify the config flow:** -1. Open an IntelliJ instance running the XTC plugin -2. Check the LSP server log for `workspace/configuration: editor formatting config:` — this +1. Open an IntelliJ instance running the Ecstasy plugin +2. Check the LSP server log for `workspace/configuration: parsed config` — this confirms the server received the settings | # | Test | Steps | Expected Result | |---|------|-------|-----------------| -| 13c.1 | Default config flows to server | Open a `.x` file, check LSP log | Log shows `editor formatting config: XtcFormattingConfig(indentSize=4, ...)` | +| 13c.1 | Default config flows to server | Open a `.x` file, check LSP log | Log shows `workspace/configuration: parsed config` with `indentSize=4` | | 13c.2 | Custom indent flows to server | Set Code Style indent to 2, restart LSP server | Log shows `indentSize=2` | | 13c.3 | 2-space indent affects formatting | Set Code Style indent to 2, type `module Foo {` then Enter | New line indented by 2 spaces (not 4) | | 13c.4 | Nested indent with custom config | Set indent to 3, type class inside module, then method, press Enter after `{` | Indent at 9 (3 * 3 levels) | | 13c.5 | No TextMate interference | Open a `.x` file with custom indent | Syntax highlighting works normally (no white background, correct colors) | -| 13c.6 | VS Code fallback | Open same project in VS Code with `editor.tabSize: 2` | On-type formatting uses 2-space indent from LSP FormattingOptions | +| 13c.6 | VS Code configuration | Set `xtc.formatting.indentSize: 2`, then format an unindented nonblank body line | Compiler formatter uses 2-space indentation. `editor.tabSize` alone does not override the workspace formatting configuration. | | 13c.7 | Auto-close brace honors custom indent | Set indent to 2, type `void foo() {` then Enter in IntelliJ | Blank body line indents to 4 spaces and the auto-inserted `}` aligns to 2 spaces | **How to restart the LSP server** (to pick up changed Code Style settings): @@ -573,7 +909,7 @@ Workspace folders take precedence; extra roots are merged in. Non-existent paths ## Adapter Comparison Summary -> See [plan-ide-integration.md](plans/plan-ide-integration.md) for the canonical adapter comparison matrix. +> See [plan-ide-integration.md](plans/plan-ide-integration.md#adapter-capability-matrix) for the canonical adapter comparison matrix. --- @@ -670,7 +1006,8 @@ the process management and health monitoring. ``` **Expected in IDE:** -- Notification: "Ecstasy Language Server Started - Out-of-process server (v..., adapter=treesitter)" +- Notification title: **Ecstasy Language Server Started**. The body has separate `Version: …`, + `Adapter: treesitter` and `Process ID: …` lines (or `Adapter: compiler` for a compiler build). ### Test: Health Check @@ -803,7 +1140,7 @@ prefix and select from the completion popup). **LSP Method:** `textDocument/codeLens` **Status:** ✅ Done -**Works with:** Tree-sitter adapter (both IntelliJ and VS Code) +**Works with:** Tree-sitter and compiler adapters (both IntelliJ and VS Code) Code lenses appear as inline annotations above module declarations. LSP4IJ (IntelliJ) and VS Code render them automatically from the LSP server response — no plugin code needed. @@ -820,34 +1157,1234 @@ and VS Code render them automatically from the LSP server response — no plugin ### 19. Semantic Tokens **LSP Method:** `textDocument/semanticTokens/full` -**Status:** ✅ Done (enabled by default) -**Works with:** Tree-sitter adapter (both IntelliJ and VS Code) +**Status:** Implemented; classification coverage depends on the adapter and theme +**Works with:** Tree-sitter and compiler adapters (both IntelliJ and VS Code) -Semantic tokens layer on top of TextMate highlighting, providing AST-aware coloring -that TextMate's regex patterns cannot achieve. The server logs `semantic tokens ENABLED` -at startup to confirm they're active. +Semantic tokens layer on top of TextMate highlighting. Tree-sitter classifies syntax; the +compiler adapter additionally uses resolved identities. TextMate can recognize many declaration +patterns, but cannot reliably resolve imports, shadowing or writes. The server logs +`semantic tokens ENABLED` at startup to confirm they are active. Categories do not prescribe RGB +colors, and a theme can give multiple categories the same appearance. **How to verify:** -- *IntelliJ:* Open a `.x` file — types, methods, properties, and annotations should - have distinct colors. Check LSP server log for `semantic tokens ENABLED`. -- *VS Code:* Same — semantic tokens are automatically layered on top of TextMate. +- *IntelliJ:* Installed plugins, Gradle `runIde` and the playbook enable semantic tokens by default. + To compare the TextMate baseline, use `XTC_LSP_SEMANTIC_TOKENS=false` in the IDE's environment + or `-Dxtc.lsp.semanticTokens=false` in its JVM options, then restart the IDE. For Gradle + `runIde`, use `-Pxtc.intellij.semanticTokens=false`. Check the server log before judging colors. +- *VS Code:* Semantic highlighting defaults on for Ecstasy; use **Developer: Inspect Editor Tokens + and Scopes** to distinguish actual classifications from coincident theme colors. | # | Test | Steps | Expected Result | |---|------|-------|-----------------| | 19.1 | Types colored distinctly | Open file with `String name;` and `Int count;` | `String` and `Int` have type color (different from `name`/`count`) | | 19.2 | Methods vs properties | Open file with `void foo()` and `String name;` | `foo` has method color, `name` has property color | -| 19.3 | Annotations as decorators | Add `@Override` or `@Inject` | Annotation name has decorator color | -| 19.4 | Deprecated strikethrough | Add `@Deprecated class Old {}` | `Old` shown with strikethrough | +| 19.3 | Annotations as decorators | Add `@Override` | Compiler tokens use resolved annotation identities, including import aliases and qualified names. Same-named ordinary types keep their type category. X276 covers both editors. | +| 19.4 | Deprecated strikethrough | Inspect a syntactic `@Deprecated` example with Tree-sitter | Tree-sitter recognizes the spelling; this is not evidence of an Ecstasy deprecation API. Compiler deprecation classification is not implemented. | | 19.5 | new Foo() as type | Write `new Person()` | `Person` colored as type, not method | | 19.6 | Method call coloring | Write `getName()` | `getName` colored as method call | -| 19.7 | Static modifier | Add `static void helper()` | `helper` may show italic (static modifier) | +| 19.7 | Static functions | Add `static void helper()` and a call to it | Compiler tokens classify both as `function.static`; instance methods and constructors remain `method`. Appearance is theme-dependent. | | 19.8 | Enum members | Write `enum Color { Red, Green, Blue }` | `Red`, `Green`, `Blue` colored as enum members | | 19.9 | Parameter highlighting | Write `void foo(Int count)` | `count` has parameter color | | 19.10 | Namespace coloring | `module myapp` declaration | `myapp` colored as namespace | | 19.11 | Server log confirmation | Check LSP server log at startup | Shows `semantic tokens ENABLED (23 types, 10 modifiers)` | +| 19.12 | Documentation comments | Compare `/** Documentation */`, `/* Ordinary */` and a string containing `/**` | Compiler tokens add `documentation` only to documentation comments, including each line of a multiline comment. | +| 19.13 | Bundled ownership | Compare imported/aliased XDK symbols, same-named local types and third-party binary types | Only symbols owned by bundled XDK modules carry `defaultLibrary`; source attachments do not change ownership. | +| 19.14 | Lexical detail and recovery | Run X277 in separate dark/light/high-contrast startup configurations, then damage and repair its string, template and declaration | Semantic overlays preserve lexical escape/keyword/literal styling. Current-source tokens stay in bounds and repair restores the exact classifications. | + +#### Compiler semantic-color prototype + +This is the requested source-highlighting feature. It uses the IDE theme and requires no new +Ecstasy library or application-color API. The separate L77 picker experiment below is opt-in. + +Open [`semantic-colors.code-workspace`](../test-fixtures/semantic-highlighting/semantic-colors.code-workspace) +in VS Code with the compiler extension, then open `SemanticColors.x`. The workspace only adds +optional font styling: **bold declarations**, underlined writes, italic static functions and italic +documentation comments. Foreground colors remain entirely theme-owned. Disable +`[xtc].editor.semanticHighlighting.enabled` temporarily to compare the TextMate baseline, then +enable it again. The same `.x` fixture works in IntelliJ with semantic tokens enabled; the VS Code +workspace styling is not an IntelliJ setting. + +Inspect lowercase `counter` as `class`, uppercase `Count` as `parameter`, `count` as `property`, +`next` as `variable`, `Reader` and imported `List` as `interface`, `Mode` as `enum`, and both uses +of `Quiet` as `enumMember`. `create` is `function.static` at its declaration and call; `read` is +`method`. The `count += 1` target has `modification`; its reads do not. This is source-code +highlighting, separate from L77 color-value swatches and pickers. + +`XdkSemanticColorTest` compiles this fixture and checks classifications. Shared X41/X154 check +functions, enum values, imported interfaces and writes in both editor drivers. Both cases pass in +both editors; dark/light/high-contrast captures were visually inspected on October 7. See the +[acceptance reports](#acceptance-reports). +The backend suite also checks the real platform `CircularBuffer` fixture, including identical +names used as parameters/properties/locals and incomplete-member edits followed by repair. --- +### L77 color-value prototype + +This opt-in experiment is separate from syntax/semantic highlighting. The ordinary fixture +[`ColorPrototype.x`](../test-fixtures/color-values/ColorPrototype.x) defines a four-byte `Rgba` +const: sRGB channels, straight alpha, opaque by default. It is not an XDK library API. +The server recognizes that resolved constructor identity, including aliases, rather than matching +arbitrary `Color` names or `"#ff8000"` strings. Normal launches advertise no color provider. + +Build and launch an isolated VS Code development host from the repository root: + +```bash +./gradlew :lang:vscode-extension:assemble -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +XTC_LSP_COLOR_PROTOTYPE=true code --new-window \ + --user-data-dir /tmp/ecstasy-color-prototype \ + --extensionDevelopmentPath="$PWD/lang/vscode-extension" \ + "$PWD/lang/test-fixtures/color-values" +``` + +The separate, reusable profile ensures an already running VS Code process does not discard the +prototype environment. Keep **Editor: Color Decorators** enabled. For IntelliJ, start the +development IDE with the same environment, then open `lang/test-fixtures/color-values` as a project: + +```bash +XTC_LSP_COLOR_PROTOTYPE=true ./gradlew :lang:intellij-plugin:runIde \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +Both hosts use standard `textDocument/documentColor` / `textDocument/colorPresentation` requests. +LSP4IJ implements color squares and its native chooser through its +[document color support](https://github.com/redhat-developer/lsp4ij/blob/main/docs/LSPSupport.md#document-color). +The server can alternatively be started directly with `-Dxtc.lsp.colorPrototype=true` before +`-jar`. Startup logs confirm the experimental `ColorPrototype.Rgba` mapping is enabled. + +Use the same steps in either editor, without saving changes to the tracked fixture: + +| Check | Action | Expected result | +|---|---|---| +| Swatches | Open `ColorPrototype.x` and wait for successful analysis | `accent` is opaque orange; `translucent()` has RGB 200/80/40 and alpha 128/255. | +| Apply | Open `accent`'s picker and choose blue with approximately half opacity | Its byte literals change and an `alpha = …` argument appears; the edited source compiles and the swatch agrees within byte rounding. | +| Named arguments | Change the color returned by `translucent()` | Blue/red/alpha/green stay in their written order and retain labels; only numeric values change. | +| Dismiss and Undo | Open and dismiss the picker without selecting a new value; reopen, change opacity, Undo and Redo | Dismissal without selection preserves source. Both native pickers apply selected values immediately; closing afterward retains the edit. Undo/Redo restores the corresponding source and color. | +| Dynamic/ordinary text | Inspect `dynamic(red)` and `ordinaryText` | Neither receives a swatch. No runtime expressions are evaluated. | +| Failure and recovery | Change a channel to 256, then restore it | Compiler diagnostic and no stale swatch/edit while the compilation fails; swatches return after repair. | +| Baseline | Relaunch without the prototype environment/property | No `colorProvider`; normal semantic token coloring continues independently. | + +Backend tests cover binary and typedef aliases, same-named unrelated types, comments, trailing +commas, UTF-16 columns, CRLF/LF/CR preservation, rounding, changed defaults, stale ranges and +document closure. References and compound constant expressions are deliberately outside the +literal-only prototype. The real packaged stdio test verifies opt-in and an edit/recompile round +trip. Shared X273–X275 automate native swatches, picker edits, dismissal, Undo/Redo and error recovery. +The launchers enable the prototype when those cases are selected, including a full catalog run. +Native acceptance passes in VS Code `run-atPCiE` and IntelliJ `run-5420151303010878432` +(zero IDE failures). X41/X154 also pass in both editors, with dark/light/high-contrast screenshots +reviewed. See the [acceptance reports](#acceptance-reports). + +## XdkAdapter Playbook + +Run this section with the **compiler** backend in either editor, now the repository build default. +These checks cover the current compiler feature surface, including semantic +answers that a syntax parser cannot supply. They do not require running the test program. + +### Automated VS Code run + +Run the compiler playbook from the repository root: + +```bash +./gradlew :lang:vscode-extension:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +This builds the extension and its bundled compiler, runs the server and packaged-JAR regression +suites, then launches a real VS Code extension host. It reads the fixtures below directly, creates +a separate workspace/profile, and runs one case for every X1–X265 row plus the configuration and +compiler-diagnostic checks. Missing case IDs, a wrong backend, failures and skipped editor cases +fail the run. The editor cases run on every invocation; Gradle may reuse unchanged host-test results. +The test window's status bar shows completed/selected cases, remaining cases and the current case, +including failure and skip counts. Focused selections use their own total rather than the full +catalog. The ordinary `testVscodeExtension` smoke suite shares this display, labelled **Ecstasy +tests**; the catalog is labelled **Ecstasy playbook**. Both appear at the left of the status bar. +Long test names are abbreviated there and shown in full in the tooltip. Smoke runs also use fresh +workspaces/profiles and retain logs under `build/reports/extension-tests/run-*/`. +To force fresh host results as well, add `:lang:lsp-server:test --rerun` and +`:lang:lsp-server:compilerStdioTest --rerun` to the command. + +Reports and failing source buffers remain under +`lang/vscode-extension/build/reports/compiler-playbook/run-*/`. `latest-run.txt` identifies the +latest directory; `results.txt` is readable and `results.json` includes the commit, dirty paths, +VS Code version, case durations/failures and host-test XML evidence. VS Code's logs are under that +run's `logs/`. The short temporary profile is removed after shutdown. Your normal editor profile +and manually prepared scratch workspace are separate. + +These checks exercise editor providers, real document edits and filesystem watchers. Raw LSP +requests additionally verify hierarchy snapshot identity, cancellation, versioned rename and +named nonexistent-file overlays. After reverting/closing a tab, the runner uses the language client's +synchronization provider to send `didClose` for any retained hidden text model; reopening sends `didOpen`. +They do not drive every menu/key or inspect rendered pixels: +popup/hierarchy layout, theme appearance, physical keyboard interaction and a prolonged interactive +memory/GC soak remain manual. The report identifies those limits and maps 7a.1–7a.14 and the host-only +API checks to their automated coverage. This command covers the **XdkAdapter** playbook, not the +separate debugger, IntelliJ or Tree-sitter playbooks. + +After assembling with the same compiler flag, `npm run test:playbook` from `lang/vscode-extension` +reruns just the editor suite. It reports existing host-test evidence without rebuilding/rerunning it. +See the [extension testing notes](../vscode-extension/README.md#testing) for display/`xvfb` requirements. + +The host suite also includes `CompilerEmissionAuditTest`: it compiles atomic operations through +qualified, singleton, enclosing-instance and generic receivers, deserializes their binary ASTs, +checks source/dependency diagnostic delivery, and inspects dense-switch conversion metadata. These +are compiler-output checks that the editor UI cannot establish. To run them without launching VS Code: + +```bash +./gradlew :lang:lsp-server:test --tests org.xvm.lsp.adapter.CompilerEmissionAuditTest \ + --rerun --no-build-cache \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +### Automated IntelliJ run + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +The Starter/Driver suite launches the packaged plugin in IDEA 2026.2.3 with Ultimate features +disabled. Native completion checks now keep sole candidates visible in the disposable test profile, verify exact +candidate sets and accept the actual edit. The same checks cover constructor and argument-value +completion. X1/X92 inspect native Structure/folding, X4 uses Find/Highlight Usages, and error/warning +cases verify received compiler metadata, Problems rows, Next Problem navigation and clearing. +X45/X46 check unopened dependencies, unchanged consumer text versions and unsaved library bytes. +X91–X96 cover simple, qualified, parameterized/compound and class/interface composition headers, +registered formals, empty generic arguments and whole-token replacement. +X106–X108 cover function/sequence leaves, trailing dots, empty type operands and declaration +headers. Discovery/refactoring cases use the same per-scenario workspace scope as VS Code. +X103 additionally reverses its file rename with the member open and verifies diagnostics clear. +The IntelliJ driver checks the restored member text in its editor, saves that document through +the IDE, then checks the disk. Refactoring an open document does not imply it has already been +autosaved; changing desktop focus must not decide whether this assertion passes. + +X20 now checks that the native popup retains the full candidate label without an invented active +argument. X81/X82 inspect Property-kind metadata from the native completion request as well as +candidates and edits. All 50 formerly missing cases now have test bodies. Protocol-only invariants +are checked through the installed client, while visible actions still use the native UI. +Nonexistent/deleted-file diagnostics are observed in its verbose trace because LSP4IJ drops these +from its VFS diagnostic store. X57 reproduced LSP4IJ's stale-edit application and now passes +through the guarded Ecstasy Rename handler. The new subset has 50/50 individual passing receipts; +X29 now passes with current signature metadata. Problems-row clicking and visual layout remain manual. +X31/X32 also pass in VS Code (`run-KfBbW1`). At empty invalid argument slots, VS Code may offer +the extension's static snippets; X32 checks the compiler response separately and rejects any +non-snippet editor proposals. +X33–X35 and X99–X108 have passing native receipts across the checkpoint and +focused X105 rerun. Execution details and the fixes found during validation are in the +[acceptance reports](#acceptance-reports). +The later full checkpoint `run-6034631232732848040` passes all 113 scenarios plus START, with +zero IDE errors and zero JUnit failures/errors/skips. Gradle completes in 6 minutes 40 seconds. +Corrections found during validation include X41's PSI read action, the session timeout, focus +handling, and X30's document-readiness check after restart. The driver allows 30 minutes overall +while retaining individual bounded waits. Separate startup acceptance also verifies disappearance +of the untouched information balloon. See the +[acceptance reports](#acceptance-reports). + +Parameter Info checks keep every overload's text assertion, but only enabled overload rows +may bold the active argument. Inactive rows are dimmed according to `activeSignature`. +The 7a.9 driver creates its own `Broken.x` empty-module baseline from shared data before +inserting the invalid declarations, then checks native Problems rows and diagnostic clearing. + +Results are under `lang/intellij-plugin/build/reports/compiler-playbook/run-*/results.json`. +`ide-paths.txt` identifies the separate IDE profile/log directory. A graphical desktop is +required. See the [IntelliJ test and configuration instructions](../intellij-plugin/README.md#compiler-playbook-in-intellij). + +During an automated run, leave its isolated editor window to the harness. The driver selects tabs, +invokes active-editor commands and closes buffers; concurrent manual navigation can change those +targets or cancel requests even without typing. The title and status bar show completed/selected +cases, remaining cases and the current case. When focus is lost during popup inspection, the +harness activates the IDE without moving the pointer and reopens the inspection only if the +document's modification stamp is unchanged. Accepted completion edits, quick fixes, renames and +file moves are outside the replay path. Typing in the fixture during recovery fails the check. +Automatic activation can redirect typing into the test IDE; use an isolated desktop for concurrent +work. Ordinary file opening and caret movement request no focus. Other native controls may still +use the mouse. Readiness polling is 100 ms; operation deadlines remain unchanged. +The disposable profile disables autosave and automatic completion/sole-candidate insertion; +personal settings and shipped plugin defaults are unchanged. Cleanup closes the IDE after a +failed check; the recorded failures were not IDE crashes. +System permission dialogs can still block native actions while Java reports the editor as focused. +If a native dialog never appears, inspect the saved screenshot before changing request deadlines +or replaying an action. A fresh selected workspace can retry an unapplied operation; do not replay +an already accepted rename. The X159 October 2 first attempt recorded this macOS prompt condition. +CFG2 deliberately submits a cyclic dependency graph and expects an error notification while the +last valid graph remains usable. That notification is an expected negative test; until its scoped +cleanup is implemented it may remain visible over later cases. + +While compiler feature coverage is growing, run the IntelliJ playbook at occasional checkpoints; +it is not required for every compiler change. Keep shared scenarios and the native consumer code +current, and state explicitly when a new IntelliJ case has not yet had a native run. + +### Focused VS Code reruns + +For a correction limited to one scenario, select exact case IDs. This builds the extension and +runs only the selected editor cases; it does not schedule the LSP unit or packaged stdio suites. +For example, rerun the class/interface header case: + +```bash +./gradlew :lang:vscode-extension:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + -PcompilerPlaybookCases=X95 +``` + +Use `-PcompilerPlaybookCases=X94,X95,X96` for the related type-completion group. Without that property the existing full +playbook plus host checks run. With an assembled extension, the direct equivalent from +`lang/vscode-extension` is `npm run test:playbook -- --cases=X95`. +Unknown, duplicate or empty IDs fail before VS Code opens. Reports state `focused`, list selected +IDs and mark every excluded case `not-selected`; every selected case must pass. Existing host XML +is supporting evidence with timestamps, not a claim that focused runs executed it. Use full runs +for broad changes and checkpoints, and focused runs for bounded fixes after a full regression pass. +IntelliJ has the equivalent `-PintellijPlaybookCases=X34,X99,X100,X101,X102,X103,X104,X105,X107,X108`. +It always runs startup, reports unselected implementations as `not-run`, and requires every selected +case to execute. Native runs remain occasional checkpoints. + +### Shared editor scenarios + +Both drivers read [the shared scenario data](../test-fixtures/compiler-playbook/scenarios.json) +for all 148 scenarios: X1–X143, CFG1–CFG3 and 7a.8–7a.9. The catalog owns titles, source-module +configuration, fixture selectors, edits, cursor/definition anchors, variants, expectations and +manual-check notes. Base programs remain the canonical fixtures below; bounded replacement +programs also live in the shared scenario values. A `§` marks an offset; +`${0}` templates substitute literal values without evaluating code. + +Native TypeScript and Kotlin code still performs editor actions and assertions. VS Code executes +all 148 cases. IntelliJ now has assertions for the same 148, plus a separate startup check. +Current selected native pass receipts are recorded above; implementation is not validation. A missing driver implementation +must be called `not-implemented`, not an unsupported IDE feature. `not-run` means an implemented +case was unselected or prevented from running, such as after an earlier failure. Partial coverage never appears +as a full pass. A failed implemented check fails the Gradle task. +These counts describe implemented assertions. Selected native execution receipts are recorded +separately above; unselected cases remain `not-run`, and the complete native checkpoint is pending. + +Both reports include the shared file, SHA-256 and complete ID list. Both drivers compare catalog +IDs with the manual table; VS Code also checks exact registration order, and IntelliJ checks that +every case declared full/partial executes. To add a scenario, add its data and VS Code consumer, +then implement its native IntelliJ actions or record the exact missing assertions in its coverage +entry. Keep client-specific protocol and UI mechanics in the drivers. + +### Launch and confirm the backend + +From the repository root, choose one command. Keep `-Plsp.adapter=compiler` on the editor launch +task too: a later build without it can restore the default backend. + +```bash +# IntelliJ sandbox +./gradlew :lang:runIntellijPlugin \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler + +# Or VS Code Extension Development Host +./gradlew :lang:vscode-extension:runCode \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +Open a scratch folder in that editor window. Confirm `backend: Ecstasy Compiler` in +`~/.xtc/logs/lsp/server-*/server.log`; the health-check adapter name is `XDK`. The matching XDK libraries +are bundled, so no `XDK_HOME`, extracted distribution or separate compiler installation is needed. +Confirm the backend in the log even when colors look familiar: compiler semantic tokens cover +resolved names, while the editor's TextMate colors and snippets also remain available. + +Use editor action search if a shortcut conflicts with your keymap. The VS Code actions are listed +in the [keybindings reference](#keybindings-reference); IntelliJ provides Quick Documentation, +Go to Declaration, Find Usages, Basic Completion and Parameter Info. For hierarchy, use the +client's LSP type-hierarchy view (VS Code: **Show Type Hierarchy**). If the installed client does +not expose an action, record that case as **not exercised**, not a server pass. + +Create the files below exactly as named. `|` in a test row marks the cursor; **do not type it**. +Undo each temporary edit before the next row and wait for diagnostics to settle. A clean baseline +is important: parse failures can suppress normal semantic answers throughout a module. + +### A. Diagnostics, navigation and structure + +Save this as `Navigation.x`: + +```xtc +module Navigation { + class Holder { + Int value = 1; + Int read() { + Int value = 2; + return value + this.value; + } + } + + String text(Object input) { + Object value = input; + if (value.is(String)) { + return value; + } + return value.toString(); + } +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X1 | Open the saved file; inspect Problems, Outline, folding and Expand Selection inside `return value;`. | No errors. Outline includes the module, class, property and methods; folding follows their blocks; selection grows through enclosing syntax. | +| X2 | In `read`, change `Int value = 2;` to `String value = 2;`, then undo. Separately change `input` to `missing` in the initializer in `text`. | Real compiler diagnostics identify the type mismatch and unresolved name at their source spans, with compiler codes. Each clears after correction without saving. `// ERROR: test` alone produces no diagnostic. | +| X3 | Hover `value` in `text`'s `return value;`, then in `return value.toString();`. | The first use has narrowed type `String`; the second has `Object`. Go to Definition from either reaches the same local declaration. | +| X4 | In `read`, use Go to Definition, Find References and occurrence highlighting on the bare `value` in `return value + this.value;`. Repeat on `this.value`. | The local and property lead to different declarations and separate reference sets despite identical spelling. Highlights stay within the document and distinguish reads from writes; X41 checks an assignment target. | +| X5 | Run Go to Definition on `String`. Then remove the final closing brace and inspect Outline, folding and selection; restore it. | The matching bundled library opens read-only at the String declaration. Recoverable syntax retains surrounding structure with a parser diagnostic; stale semantic targets are not reused. A badly broken header may leave structural gaps or a cursor-only selection. | + +For the error-listener regression, also run **7a.8** with `DupAnno.x`: the duplicate inherited +annotation produces exactly one `VERIFY-75` warning. Rows **7a.1–7a.7** cover bundled-library startup, +timings, queued/superseded edits and memory. Timings depend on hardware and module size; record them +rather than treating the illustrative numbers as pass thresholds. + +**Problems view checks (X2 and 7a.8).** In VS Code, open **View → Problems** (Cmd+Shift+M on macOS, +Ctrl+Shift+M elsewhere). Clear the text filter, enable both Errors and Warnings, and disable +“Active File Only” while comparing files. + +1. Make each X2 edit. Check that the row belongs to Navigation.x, has **Error** severity, the + compiler message/code and a line/column matching the marked source range. Click the row or use + **Go to Next Problem** (F8); the editor must select that source location. +2. Correct it without saving. The row, red squiggle and error count must clear after analysis. +3. Open DupAnno.x from 7a.8. Check exactly one **Warning** row with code `VERIFY-75`. This warning + selects the derived property's `x` declaration. The compiler reports a structure, and the adapter + maps that exact declaration to its source token. Remove only the derived `@Atomic`: the warning + row/count must clear without saving. Undo to restore one warning. Binary-only structure + diagnostics retain a document-level fallback. +4. Leave the warning in DupAnno.x and introduce an X2 error in Navigation.x. Both file groups and + severities must remain visible; correcting one must not remove the other's diagnostic. + +The automated cases check diagnostic severity, code, source range, unsaved clearing and the +editor's next-problem command. They open the Problems view; inspecting its rendered rows, severity +icons, filters, counts and clicking a row remains a visual/manual check. + +### B. Typed and scope-aware completion + +Save this as `Editing.x`: + +```xtc +module Editing { + class Box { + T item; + private T itemMethod() = item; + String choose(String first, String second) = first; + Int choose(Int first, Int second) = first; + T pair(T first, T second) = first; + U generic(U first, U second) = first; + void inspect(T itemLocal) {} + } + + class Tools { + static Int itemFunction() = 1; + static Int itemConstant = 2; + Int itemProperty = 3; + Int itemMethod() = 4; + private static Int itemHidden() = 5; + static class itemType {} + private static class itemPrivate {} + } + + String getValue() = "text"; + void run(Box box, String itemParameter, Object value) {} +} +``` + +Use **Trigger Completion / Basic Completion**, not just the popup triggered by typing. Replace +the body of `run` for X6–X10 and X12; for X11 use `Box.inspect`. Undo after each row. + +These rows deliberately contain unfinished names. For example, `.si` is the partially typed +`.size` in X7/X8/X14. `COMPILER-36: Could not find name "si" within "String"` is expected at that +point: `String` is the receiver's type, and `si` is not one of its members. (`COMPILER-38` is the +related `Name "..." is unresolvable` diagnostic.) The completion probe +offers `size` while normal compilation still reports the unfinished source. The test checks that +the diagnostic clears after accepting the completion; it is not a failure merely to see it while +the runner is typing. + +| # | Temporary body / action | Expected result | +|---|-------------------------|-----------------| +| X6 | `box.|;`, then `box.it|;` | Public members include `item` with substituted type `String`. The prefix filters to matching names; private `itemMethod` is absent. Accepting `item` replaces only `it`, preserving the receiver and semicolon. A bare property access is not a valid statement; change the accepted text to `String selected = box.item;` to confirm diagnostics clear. | +| X7 | `Int size = getValue().si|;` | `size` is offered from the expression receiver's `String` type. Accept it: `Int size = getValue().size;` has no errors. | +| X8 | `if (value.is(String)) { Int size = value.si|; }` | `size` is available because of flow narrowing. Accept it and check diagnostics clear. Undo the guard too when finished. | +| X9 | `Int itemLocal = 1; Int itemUnassigned; item|; Int itemLater = 2;` | Offers `itemLocal` and `itemParameter`; excludes the unreadable unassigned variable and the later declaration. | +| X10 | Put the cursor in the empty `run` body and invoke completion without typing a prefix. | Visible parameters such as `itemParameter` appear. Accepting one inserts at the cursor without deleting a brace or nearby text. Undo the insertion afterward. | +| X11 | In `Box.inspect`: `ite|;`. Then try `{ Int itemClosed = 1; } Int item = 2; ite|;`. | First: implicit `item`, private `itemMethod` and parameter `itemLocal`, with formal type `T`. Second: local `item` has type `Int`, the property is shadowed, and closed-scope `itemClosed` is absent. | +| X12 | In `run`: `Tools.item|;` | Offers static `itemFunction`, `itemConstant` and nested `itemType`; excludes instance `itemProperty`/`itemMethod` and private `itemHidden`/`itemPrivate`. | +| X13 | In `run`: `Strin|;`. Then add module import `import ecstasy.text.StringBuffer as Buffer;` and try `Buffe|;`. Replace the import with `import ecstasy.text.*;` and try `StringBuffe|;`. | Implicit `String`, explicit alias `Buffer`, then wildcard-imported `StringBuffer` appear. Restore the file between import variants. | +| X14 | Repeat X7 with `/* 😀 */` before the statement on the same line and the file saved with CRLF line endings. | The accepted completion still replaces exactly `si`. No shifted edit, damaged emoji or extra character. | + +### C. Selected signatures and incomplete-call candidates + +Keep `Editing.x`. Replace `run`'s body with the call in each row. Invoke **Trigger Parameter Hints / +Parameter Info** at `|`; leave the closing `)` in place, as an editor normally does. + +| # | Call / action | Expected result | +|---|---------------|-----------------| +| X15 | `box.choose("x", |);`, then `box.choose(1, |);` | The first offers the `String` overload, the second the `Int` overload. The second parameter is active. These are applicable **candidates**, not final overload selections. | +| X16 | `box.pair(second = "x", first = |);` | Signature types are `String` from `Box`; `first` is active despite being the second written argument. Insert `"y"`: diagnostics clear and the now-valid call uses the compiler-selected signature. Go to Definition on `pair` reaches its declaration. | +| X17 | `box.generic("x", |);`, then `box.generic(|);` | The first infers `String` for the expected second parameter. Without an argument, the type remains formal `U`, not an invented `Object`. | +| X18 | `box.choose(True, |);`, `box.pair(unknown = |);`, and `box.pair(first = "x", first = |);`, one at a time | No applicable signature for incompatible types, an unknown label or a duplicate label. Earlier hints must not remain visible as the answer to the new request. | +| X19 | Inside `Box.inspect`: `pair(itemLocal, |);`. Then add `static String join(String first, String second) = first;` to `Tools` and try `Tools.join("x", |);` in `run`. | Implicit-instance and static calls both show applicable candidates with the second parameter active. Fill in the missing values and confirm diagnostics clear. | +| X20 | Use `box.pair(second = "x", |);`. Then complete `box.choose("x", "y");` and navigate from `choose`; repeat with `box.choose(1, 2);`. | After a named argument without a new label, no guessed parameter is highlighted. Complete calls select and navigate to the correct distinct overloads. | + +The candidate label/documentation does not mean an unfinished overload has been selected. Ecstasy +allows trailing commas in valid calls: such a call can already have a selected signature. +Empty final positional slots and pending named values also offer compiler-fitted readable locals, +parameters and implicit properties/constants, including direct final typed argument prefixes (X77–X82). Normal diagnostics may +remain while these deliberately incomplete calls +still provide useful hints; accepting a valid value clears them. + +### D. Module files, overlays and type hierarchy + +Create this layout in the scratch folder. Save both files, then close `Child.x` while keeping +`Project.x` open. + +```text +Project.x +Project/ + Child.x +``` + +`Project.x`: + +```xtc +module Project { + interface Named {} + class Base implements Named { + Element echo(Element value) = value; + Int item = 1; + } +} +``` + +`Project/Child.x`: + +```xtc +class Child extends Base { + String answer() = echo("member"); +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X21 | From `Base` in the root, Find References and search workspace symbols for `Child` (Go to Symbol in Workspace). Open Child and navigate from `Base` and `echo`. | References and symbol search include the closed member. Definitions land on the correct names in Project.x. Search covers analysed module sessions, not every unopened module in the workspace. | +| X22 | Show Type Hierarchy on `Child`; expand supertypes. Show it on `Base` and `Named`; expand subtypes. | Direct links are `Child → Base → Named`, with correct file locations. Expanding Base's subtypes finds Child even if its tab is closed. Generic arguments remain visible. | +| X23 | Keep both files open. Change Base's name to `Renamed` in the root without saving, then undo. | Child's diagnostic changes without an edit there, then clears after undo. No generic internal error is added to the root merely because the member has an error. | +| X24 | Add `void inspect() { ite|; }` in Child and complete. Change root property to `String item = "overlay";` without saving; repeat completion in Child. | `item` changes from `Int` to `String` using the unsaved root. Undo the root change: completion returns `Int`. No temporary source files are written. | +| X25 | Create a named, unsaved `Project/pkg/Added.x` buffer with `class Added extends Base {}`. | The new member and implicit package join the current module; diagnostics and Base's hierarchy reflect Added. An anonymous Untitled buffer without this URI is not this test. | +| X26 | With Project.x still open, introduce an error in Child, then discard changes and close Child. Reopen it. | The saved disk version replaces the overlay; its obsolete diagnostic clears. Navigation and hierarchy use the restored source. | +| X27 | With Project.x open, create `Project/Bad.x` on disk containing `class Bad extends Missing {}`; wait, then delete it. | Watched-file notifications refresh membership; the closed file's diagnostic appears and then clears. Run in a workspace containing these files so the client watches them. | +| X28 | Open hierarchy on Child. Temporarily replace the member file with `class Child {}`, wait for analysis, then expand the old item and reopen hierarchy. Undo. | The old item cannot return edges from the previous compilation; a fresh hierarchy no longer claims Base as parent. Restoring the file restores the edge. `Object` is an interface, so `extends Object` is not a valid replacement fixture. | + +### E. Edits, cancellation and capability boundaries + +| # | Action | Expected result | +|---|--------|-----------------| +| X29 | In Editing.x, alternate rapidly between X15's String and Int arguments and request hints/completion. Finish with a valid call. Repeat while editing a module sibling. | The final answer and diagnostics match the latest text. Superseded queries do not resurrect old types, offsets or errors. | +| X30 | Start a completion/hint request, dismiss it and close the document; reopen it. Repeat around a language-server restart. | No response repopulates a closed document, no hanging UI, and the reopened file gives current answers. Dismissing a popup does not guarantee the client sends cancellation; protocol cancellation is also covered by the automated stdio tests. | +| X31 | Inspect compiler-mode capabilities, then format a module with an unindented body. | Declaration lookup, monikers, formatting, range formatting, code actions and code lenses are advertised; formatting produces edits. Inline values are not advertised. Document colors are off by default and advertised only when the experimental color prototype is explicitly enabled; the harness enables it for selections containing X273–X275. Native formatting is exercised further in X102/X107. | +| X32 | Try completion in `box.pair(unknown = \|);`, `box.pair(first = "x", first = \|);`, `box.pair(True, \|);`, `box.pair("x", "y", \|);`, and `missing(\|);`. | Unknown or duplicate labels, incompatible or excess arguments, and unresolved calls offer no argument values. Valid argument-value insertion, including positions before a later written argument, is covered by the positive completion scenarios. | + +### F. Type-definition and implementation lookup + +Save this as `Lookups.x`. Use **Go to Type Definition** (IntelliJ: **Go to Type Declaration**), +and **Go to Implementations / Go to Implementation(s)** from the editor action menu. + +```xtc +module Lookups { + interface Mapper { T map(T value); } + class TextMapper implements Mapper { + @Override String map(String value) = value; + String map(Int value) = value.toString(); + } + class Child extends TextMapper { + @Override String map(String value) = value; + } + class Inherited extends TextMapper {} + class Unrelated { String map(String value) = value; } + TextMapper make() = new TextMapper(); + void run(Mapper mapper, TextMapper|Unrelated value) { + mapper.map("text"); + value.toString(); + if (value.is(TextMapper)) { value.toString(); } + make(); + } +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X33 | Go to Type Definition on `mapper` in `mapper.map("text")`, then on `make` in its call and declaration. | The variable navigates to `Mapper`, not its `String` argument; the method navigates to its `TextMapper` return type. | +| X34 | Go to Type Definition on `value` before the `if`, then inside the narrowed branch. | Before narrowing, two targets: `TextMapper` and `Unrelated`. Inside, only `TextMapper`. A chooser/peek list instead of a direct jump is normal for multiple targets. | +| X35 | Go to Type Definition on `value` in `Mapper`'s method signature, then on the written `String` in TextMapper. | The formal value type leads to the declaration of `T`; the bundled `String` type opens its matching read-only declaration. No same-spelled local substitute is returned. | +| X36 | Find Implementations on `Mapper`. | `TextMapper`, `Child` and `Inherited`, once each. `Unrelated` is excluded despite its matching method shape. These are nominal declaration-level results, not a search for every structurally compatible class. | +| X37 | Find Implementations on the interface's `map`, then on the call `mapper.map("text")`. Repeat on Child's override. | Interface/call: the String bodies in TextMapper and Child. The Int overload and Unrelated's method are excluded; Inherited adds no duplicate body. Child's override resolves to its own body. | +| X38 | Return to the two-file Project fixture in section D. Go to Type Definition on the return-type `Child` after adding `Child make() = new Child();` to the root. Find Implementations on Base's `echo`; repeat after adding two blank lines before Child without saving, then after a broken member edit and correction. | Type-definition reaches the closed/member source at its current position. The inherited method points to the actual Base body once. A parse failure clears semantic targets until correction; old offsets are never reused. | + +Implementation lookup requires a successful current module compilation. Concrete classes can be +their own implementation target. Interface default bodies and mixin bodies from composed hosts +can be source targets; an unused mixin is not an implementation of its `into` constraint. +A user-written override inside an anonymous class is also a method target, although the synthetic +class is not listed as a named type implementation. +Property/accessor implementation checks are in section J. Concrete delegation is covered in +section K; synthetic redirect targets and unknown runtime receivers remain outside lookup. + +### G. Call hierarchy, semantic highlighting and inlay hints + +Save this as `Consumers.x`. Enable semantic highlighting and inlay hints in the editor. For call +hierarchy use **Show Call Hierarchy** (VS Code) or the client's incoming/outgoing call view. +In IntelliJ, record an unavailable LSP action as not exercised. Semantic token colors depend on +the theme; VS Code's **Developer: Inspect Editor Tokens and Scopes** shows the actual token kind. + +```xtc +module Consumers { + static Int leaf(Int input, Int extra = 2) = input + extra; + static String leaf(String text) = text; + Int run(Int seed) { + var number = leaf(1); + val label = leaf("text"); + number += leaf(input = 2, extra = 3); + function Int() fn = () -> leaf(seed); + return number + label.size + fn(); + } +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X39 | Show incoming calls for the Int `leaf`, then outgoing calls for `run`. | Incoming groups two sites under `run` and one under ``. Outgoing `run` lists the Int and String overload separately; the lambda's call is not attributed to `run`. | +| X40 | Expand the lambda's outgoing calls. Inspect the dynamic `fn()` call. | The lambda leads to Int `leaf`; `fn()` does not invent a statically selected edge. Call ranges navigate to the caller's source. | +| X41 | Inspect tokens for `leaf`, `seed`, `number` and the `number +=` target. Select `number` to highlight occurrences. | Function, parameter and variable kinds reflect resolved identities. Static/declaration/modification modifiers are present where applicable. Highlights distinguish the write and subsequent read. Theme colors may coincide. | +| X42 | Inspect inline hints in `run`; compare positional and named calls. Then use the shared `inferred` source with a destructured pair and `(value) -> value`. | `number: Int` and `label: String` inferred-type hints; `input:` and `text:` before positional values. The named call has no redundant hints and omitted default `extra` has none. Explicit declarations get no inferred-type hint. The original fixture also has an inferred `Int` lambda return (three type hints total); the additional shared source has two destructured types plus lambda parameter/return types (four total). | +| X43 | Keep hierarchy items open, insert a blank line before `run`, then reopen hierarchy. Break the module with an unfinished declaration, correct it and close/reopen the file. | Fresh results use current ranges. Old hierarchy items do not resolve against the edited snapshot. A parse failure clears semantic answers; correction restores them. | +| X44 | In the section D two-file fixture, add `static Int target(Int n) = n;` to Project and `Int callTarget() = target(1);` to Child, then close Child and show incoming calls on `target`. | `callTarget` and its call site point to the closed Child source. An unsaved member edit moves the result; stale positions are not reused. | + +Also unavailable: separate Go to Declaration, document links and linked editing. The bounded +rename checks below exercise recompilation plus binding comparison. The interactive fixtures +do not configure dependency artifacts; source navigation through the host API is tested separately +below. Workspace-wide reference/implementation searches and external or conditional-mixin hierarchy +remain open. No Tree-sitter fallback runs in compiler mode. Check the +[capability matrix](plans/plan-ide-integration.md#adapter-capability-matrix) when those limits change. + +Record the commit, editor/version, confirmed backend, case ID, source/unsaved edits, expected and +actual result, and relevant server-log lines for failures. Mark unsupported client actions and +unrun rows explicitly. The packaged compiler regression suite complements the interactive pass: + +```bash +./gradlew :lang:lsp-server:compilerStdioTest --rerun --no-build-cache \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +### Dependency host API checks + +Binary artifact/source-index replacement has no editor setting or JSON-RPC endpoint. Source module +configuration is available separately in the following section. A Kotlin host can +export a successful `Compilation.toDependency()` and call +`XtcLanguageServer.replaceCompilerDependencies(listOf(dependency))`. Direct adapter hosts use +`replaceDependencies(...)` and reschedule the returned scope keys themselves. Binary-only artifacts +load with `XdkDependency.fromBinary(bytes)` and intentionally supply no source targets. + +Run the host regression checks to verify the integration boundary without pretending an ordinary +editor launch configures it: + +```bash +./gradlew :lang:lsp-server:test \ + --tests 'org.xvm.lsp.adapter.XdkDependencyTest' \ + --tests 'org.xvm.lsp.server.XdkLanguageServerTest' \ + --rerun --no-build-cache \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +Expect dependency definition/type-definition and inherited generic method-body locations to point +to the exported sources. Replacing a library invalidates direct and transitive consumers, cancels +queued compilation/cursor work and preserves unrelated successful sessions. Server tests replace +an Int-returning library with a String-returning one: consumer diagnostics appear at the unchanged +document version, then clear when the original artifact is restored. Binary-only replacement removes +source links; compiling the dependency's current source uses that source rather than its old index. +The source-project checks below separately exercise builds from unsaved sources. Neither API +establishes a persistent workspace reference index. + +### Automatic source recompilation host checks + +For VS Code with the compiler build, open the folder containing Library.x and Consumer.x and add +this to workspace settings (`.vscode/settings.json`): + +```json +{ + "xtc.compiler.sourceModules": [ + { "name": "Library", "uri": "Library.x" }, + { "name": "Consumer", "uri": "Consumer.x", "dependencies": ["Library"] } + ] +} +``` + +No restart is needed. Relative URIs require one workspace folder; use absolute file URIs for +multi-root workspaces. Other clients can supply `initializationOptions.xtcCompiler` or the +`xtc.compiler` configuration section. In IntelliJ, use LSP4IJ's Ecstasy Language Server +**Configuration** JSON with nested `xtc.compiler.sourceModules`, as shown in the +[IntelliJ instructions](../intellij-plugin/README.md#compiler-source-module-configuration). +These are IDE-wide server settings; there is no dedicated XTC project graph UI. +The explicit setting overrides automatic workspace discovery. Use `null` to restore discovery +or `[]` to disable it. An embedding host can also register the graph directly: + +```kotlin +server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("Library", "file:///workspace/lsp-project/Library.x"), + XdkSourceModule("Consumer", "file:///workspace/lsp-project/Consumer.x", setOf("Library")), + ), +) +``` + +Create these two files on disk: + +```xtc +// Library.x +module Library { static Int value() = 1; } +``` + +```xtc +// Consumer.x +module Consumer { package lib import Library; Int run() = lib.value(); } +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X45 | Open Consumer.x without opening Library.x. Navigate from `value`. | Both modules compile; Consumer has no errors and definition points into Library.x. No separate Gradle build is needed. | +| X46 | Open Library.x and change its method to `static String value() = "text";` without saving. | Consumer gains a type error without an edit/version change there. Disk still contains the Int version. | +| X47 | Replace Library's method with `MissingType broken;`, then restore the original method. | The original compiler error belongs to Library. Consumer reports `DEPENDENCY-FAILED` and has no stale navigation; correction clears both files. | +| X48 | Make the unsaved String change again, then discard and close Library.x. Reopen it. | Consumer recovers using the Int version on disk; reopening uses current text. Closing an overlay does not retain its unsaved artifact. | +| X49 | While Library is closed, delete its root on disk, then restore it; ensure watched-file notifications reach the server. | Library reports `SOURCE-UNAVAILABLE`; Consumer reports `DEPENDENCY-FAILED`. Restoring the file clears both without editing Consumer. | +| X50 | Open an unsaved Library/Extra.x with `class Extra { MissingType broken; }`, then discard/close it. Repeat with a saved member and disk deletion. | The member owns its compiler diagnostic. Consumer blocks, then recovers when the invalid member disappears; removed diagnostics clear. | +| X51 | Make rapid valid/invalid edits in Library while querying Consumer, then leave a valid Int method. Keep an unrelated module open. | Final diagnostics/navigation use the latest inputs; obsolete requests cannot restore older facts. The unrelated module remains available. | +| X52 | Add Bridge.x with `module Bridge { package lib import Library; static Int value() = lib.value(); }`; register Bridge depending on Library and change Consumer's edge/import to Bridge. Repeat X46–X47. | Changes propagate Library → Bridge → Consumer. A broken Bridge blocks Consumer; correction restores the chain. | + +There is a 100 ms edit debounce; compiler cancellation remains cooperative. Source cycles and +overlapping roots are rejected during configuration. Blocked consumers currently expose no +semantic or structural views until their dependencies recover. Hosts must supply accurate edges; +the server does not infer a project graph from unresolved imports or build-tool files. + +Automated adapter/server coverage of this setup, including uncooperative late compiler results, +cursor cancellation, snapshot timing and binary replacement: + +```bash +./gradlew :lang:lsp-server:test \ + --tests 'org.xvm.lsp.adapter.XdkProjectTest' \ + --tests 'org.xvm.lsp.server.XdkProjectServerTest' \ + --rerun --no-build-cache \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +### Configuration update checks + +After X45–X52, set `xtc.compiler.sourceModules` to `[]`: Consumer should report its unavailable +Library dependency. Restore the list and expect diagnostics to clear without editing Consumer. +Introduce a cycle by adding Consumer as a dependency of Library: expect a configuration error and +the old working graph to remain active. Restore valid settings, repeat the same list, and verify +that existing semantic results remain available. These checks exercise the ordinary editor bridge; +the host-only binary-artifact section above still requires a Kotlin host. + +### H. Compiler-validated rename + +Use the compiler backend and an editor that advertises `workspace.workspaceEdit.documentChanges`. +If it does not, record this section as unsupported by that client; the compiler server deliberately +omits rename. Create `Rename.x`: + +```xtc +module Rename { + Int value = 10; + private Int pick(Int input) = input; + Int run() { + Int local = pick(input = 1); + function Int() captured = () -> local; + return captured() + value; + } +} +``` + +Use F2 (VS Code) or Shift+F6 (IntelliJ). Wait for diagnostics to clear before each case and undo +accepted edits before continuing. These are bounded semantic edits, not general member refactoring. + +| # | Action | Expected result | +|---|--------|-----------------| +| X53 | Rename `local` to `renamed`. | Declaration and captured use change; `value` and unrelated names do not. Recompilation has no errors. | +| X54 | Rename `input` to `number`, first at its declaration, then after undo at `input = 1`. | Declaration, method body and named label all change; `=` and argument value remain intact. | +| X55 | Rename `local` to `value`. | No edits: the untouched property use would silently bind to the local even though compilation would succeed. | +| X56 | Without registering Rename.x in a source graph, try renaming `pick`, module `Rename`, property `value`, or a public method's parameter. | Rename unavailable. Public/lambda/constructor parameters, properties and module names remain unsupported. Ordinary instance methods require the explicit graph and checks in section I. | +| X57 | Start a rename and edit another file in the same module, close/reopen the target, or change its version before applying. | Pending work is canceled or rejected as changed; an edit for an old open-buffer version is not applied. Fast machines may need the controlled server regression below to exercise this race. | +| X58 | Introduce a syntax error, try rename, fix it and retry. Try an invalid identifier or an existing local name. | Broken/unsupported/conflicting requests give no edits or temporary diagnostics. A valid rename works again after correction. | + +#### Additional L62 rename checks + +Shared cases **X109–X117** now carry these sources, expected edits and file destinations for +both editor drivers. Each starts with other consumers closed and checks a single Undo against +every original source and path. Selected native validation passed (receipts below). For manual +exploration, use a scratch workspace with automatic source discovery. Save +`RenameLibrary.x` and `RenameConsumer.x`, then wait for clean diagnostics: + +```xtc +module RenameLibrary { + class Box { + construct(Int input, Int count = 1) { total = input + count; } + Int total; + Int pick(Int input, Int count = 1) = input + count; + } +} +``` + +```xtc +module RenameConsumer { + package lib import RenameLibrary; + lib.Box make() = new lib.Box(count = 2, input = 1); + Int use(lib.Box box) = box.pick(count = 2, input = 1); +} +``` + +- Rename the `pick` parameter `input` to `value`. Its declaration, body and named caller change; + the constructor parameter stays unchanged. Undo, close RenameConsumer.x, and repeat to cover closed callers. +- Rename `input` at the constructor's named call. Its constructor declaration/body change, while + the `pick` parameter stays unchanged. Undo. Rename to the already-used `count`: no edit is offered. +- Change the module declaration/import to `RenameLibrary.example.org`. Rename the module's `RenameLibrary` + token to `Renamed`: the domain and local alias `lib` stay unchanged, and the root file moves. +- In a separate `App.x`, use `module App { void accept(tools.deep.Box value) {} }` and create + `App/tools/deep/Box.x` containing `class Box {}`. Rename `tools` to `helpers`: only the package + directory moves, uses change, and definition reaches the moved Box.x. Undo restores the tree. +- For composition variants, use the small fixtures in `XdkResourceRenameTest`: rename `read` + through an interface delegate, a mixin override (including `super()`), and an `@Lazy` property. + All written contract names/uses change and the resulting module compiles. Binary contracts and + method-value escapes remain refused. + +Explicit source settings now use the native Rename action through `xtc/rename`, advertised as +`experimental.xtcRenameProposal = 1`. The response contains versioned edits plus the exact before +and after source graphs. It does not install either graph. Both clients reject a mismatched graph +or a stale settings snapshot before accepting the edit. Standard `textDocument/rename` continues +to refuse graph changes for clients without this persistence integration. +An explicit graph must contain every consumer you intend to update. An omitted source module +is outside the proof and will not be renamed; its mere presence beside a configured root does +not cause rejection. The new X119–X121 cases cover primary-constructor properties, lambda +parameters and escaped method values. Unsupported composition routes remain refused. +Project `xtc/rename` responses also describe this input boundary in `scope`; absolute registered +roots may be outside workspace folders. A scope receipt does not authorize applying stale edits. + +- **VS Code:** keep the explicit graph in workspace settings (`.vscode/settings.json`, or the + `settings` object of a saved `.code-workspace`). Rename edits only `xtc.compiler.sourceModules` + in the same WorkspaceEdit as source text and moves. Comments and unrelated settings survive. + Open settings documents feed the compiler immediately, including the interval after saving + before VS Code refreshes its configuration cache. Native Rename follows + `files.refactoring.autoSave` (default true); **Save All** also persists the settings and sources. + Undo/Redo restore the graph along with the files. Global/folder-only configurations are refused. + If Rename edits a file it also moves, disabling refactoring auto-save refuses the proposal + before any edits: VS Code cannot reliably restore that unsaved resource history. Text-only + renames and moves of unedited companions remain available. The harness checks both refusal + and the enabled-policy transaction; it uses refactoring metadata like the native action. +- **IntelliJ:** open **Settings > Languages & Frameworks > Ecstasy Compiler**. Select automatic + discovery, or disable it and add module names, root URIs/relative paths and comma-separated + dependencies. **Apply** saves through LSP4IJ's project settings and refreshes the running server. + The resource-root column accepts a JSON array such as `["assets/templates", "shared/resources"]`. + Leave it blank for compiler layout deduction; `[]` explicitly disables resources. Relative paths + use the project root. **Refresh Gradle model** imports evaluated build-defined paths; automatic + Gradle-sync refresh remains a follow-up. In VS Code, use the same `resourceRoots` array within + `xtc.compiler.sourceModules`. Change/delete/recreate a referenced resource on disk and check + dependency diagnostics refresh; a resource file is not an extra XTC source root. + Blank fields and duplicate roots are refused. **Reset** discards pending edits. The legacy LSP4IJ + Configuration JSON remains the fallback until a project override exists. Rename and native + Undo/Redo update that same effective store; a changed graph or changed settings owner refuses + stale history. Project settings do not change other projects' global configuration. + +X118 checks persistence, root/edge replacement, Undo, Redo and second Undo in both drivers. +The VS Code driver also changes the real settings document during reply conversion and expects +a stale-settings refusal without source edits. Run with `-PcompilerPlaybookMultiRoot=true` +to open a saved two-folder `.code-workspace`; X118 additionally attempts a source-graph override +in the second folder and verifies VS Code rejects that unsupported scope, then exercises native Rename/Undo/Redo. +These subchecks pass in multi-root `run-ZOoaXw` (X118/CFG1–CFG3) and single-folder +`run-nFKwOD` (X118). Earlier X118 receipts do not cover them. Independent graph edits during +Undo/Redo remain separate native stress coverage; the new race is during Rename conversion. +All X109–X118 have passing selected receipts: IntelliJ `run-6245646041474423108`, VS Code +`run-3CeLiB` (X109–X117) and `run-7Xbo86` (X118 plus CFG1–CFG3 after the save-cache correction). +See the [acceptance reports](#acceptance-reports). + +### I. Configured-graph references and method rename + +Save these three files in one scratch folder. Register `Contracts` at `Contracts.x`, `Uses` at +`Uses.x` depending on `Contracts`, and `Dormant` at `Dormant.x` depending on `Contracts`, using +`xtc.compiler.sourceModules` as in section G. Start with only Contracts.x open. + +```xtc +module Contracts { + interface Mapper { T map(T value); } + class Base { + Int pick(Int value) = value; + Int choose(Object value) = 0; + } +} +``` + +```xtc +module Uses { + package api import Contracts; + class Mapper implements api.Mapper { + @Override String map(String value) = value; + } + String run(api.Mapper contract, Mapper impl) = contract.map("a") + impl.map("b"); + Int choose(api.Base box) = box.choose(1); + conditional Int library(String text) = text.indexOf('a'); + class Named { @Override String toString() = "name"; } +} +``` + +```xtc +module Dormant { + package api import Contracts; + String run(api.Mapper mapper) = mapper.map("c"); +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X59 | Find References on the interface's `map` in Contracts.x, with Uses.x and Dormant.x unopened. | The declaration and the two calls through `api.Mapper` appear. The concrete override and `impl.map` have their own identity and are excluded from this exact reference query. | +| X60 | Rename that `map` to `convert`; inspect the preview, apply, then undo. | Five edits across all three files: the contract, concrete override, and all three calls. No diagnostics after recompilation. Closed files have null edit versions; open buffers carry their current versions. IntelliJ persists edited closed consumers through the UP21 bridge before opening them; the original open buffer stays unsaved. | +| X61 | Rename Contracts.Base's `pick` to `choose`. | No edit. The unchanged `box.choose(1)` would select a different overload even though the edited graph compiles. | +| X62 | Open Uses.x, invoke signature help inside `indexOf('a')`, then try renaming `indexOf`. Separately try renaming Named's `toString` override. | The bundled XDK supplies the Char overload's signature; binary `String.indexOf` cannot be renamed. The source override also cannot be renamed because its contract belongs to the bundled XDK. These are resolved binary targets, not missing dependencies. | +| X63 | Add a second `mapper.map("d")` call to Dormant's return expression without saving, query references/rename, then temporarily replace its body with `MissingType broken;`. Restore the fixture. | Queries include the unsaved call and rename uses its current buffer version. An incomplete configured graph gives no reference list or rename edit; proof compilations add no diagnostics of their own. Restoring it restores results. | + +Each query captures and compiles the entire explicit graph, including closed members and transitive +consumers. References use exact compiler identities, including source uses of bundled binary members; +method rename follows ordinary instance-method override families and checks all written bindings, +selected calls and dispatch chains before returning edits. Binary ancestors, +mixin/delegating/capped chains and unknown bindings fail closed. Ordinary `super(...)` calls retain +their selected source body through the attempt's call-binding collector. Function-valued calls +expose validated signature types separately, without a selected method target. Instance-property +families/accessors, constructors and public-parameter renames remain outside this proof. The current +batch additionally proves inline-type and static-member renames over the same graph. +Preparing a method rename identifies a candidate; the final graph proof can still reject it. +An explicit configuration limits the graph to its listed modules. Automatic discovery instead scans +workspace folders; neither mode proves closure over external clients of an exported API. Keep every +source consumer in the configured/discovered graph. + +Controlled regressions in `XdkProjectQueryLifecycleTest` and `XdkCursorServerTest` cover changes in +another module, close/reopen, configuration/dependency replacement, canceled/superseded queries and +late disk changes before returning results. These races are hard to trigger reliably by hand. + +For closed-member input checks, version conversion, cancellation and the repeated retention workload: + +```bash +./gradlew :lang:lsp-server:test \ + --tests 'org.xvm.lsp.adapter.XdkRenameTest' \ + --tests 'org.xvm.lsp.adapter.XdkProjectQueryTest' \ + --tests 'org.xvm.lsp.adapter.XdkProjectQueryLifecycleTest' \ + --tests 'org.xvm.lsp.server.XdkRenameServerTest' \ + --tests 'org.xvm.lsp.server.XdkCursorServerTest' \ + --tests 'org.xvm.lsp.adapter.XdkRetentionTest' \ + --rerun --no-build-cache \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +The retention test repeatedly replaces dependencies, rebuilds an explicit source graph, performs +rename/cursor proofs and closes/reopens the consumer. It reports actual latency and checks release +of compiler results, AST roots and pools after close and shutdown. This is a bounded automated +workload; record interactive and prolonged editor tests separately. + +### J. Property and accessor implementations + +Save this as `Properties.x`. Use **Go to Implementations** on the indicated property or accessor +name. A property query lists its effective getter/setter bodies or backing-field declarations; +an accessor query follows only that accessor. The query is declaration-level: a read of a property +has the same implementation set as its declaration, rather than selecting only its getter. + +```xtc +module Properties { + interface Named { T name; } + class Stored implements Named { @Override String name = "stored"; } + class Computed implements Named { @Override String name.get() = "computed"; } + class Inherited extends Stored {} + class Unrelated { String name = "other"; } + String read(Named value) = value.name; + + class Base { + Int value { + Int get() = 1; + void set(Int value) {} + } + } + class Child extends Base { @Override Int value.get() = 2; } + interface Defaulted { @RO String label { @Override String get() = "default"; } } + class DefaultUser implements Defaulted {} + class FieldUser implements Defaulted { @Override String label = "field"; } + + interface Missing { @RO String absent; } + class Forward(Missing target) delegates Missing(target) {} + class Delayed { @Lazy Int later.calc() = 1; } + Int size(String text) = text.size; +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X64 | Find Implementations on `name` in `Named`, then in `value.name`. | Stored's `name` field and Computed's `get` body. Inherited adds no duplicate; Unrelated is excluded. | +| X65 | Query Base's `value`, then its `get` and `set` separately. Query Defaulted's `label`. | Property: Base's getter/setter and Child's getter. Getter: the two getter bodies only. Setter: Base's setter only. Defaulted: its default getter and FieldUser's field. | +| X66 | Query Missing's `absent`, Delayed's `later`, and `size` in `text.size`. | No invented target for an abstract/interface-delegated property, binary-only `@Lazy` getter/native storage or bundled binary source. | +| X67 | Create `Properties/Member.x` containing `class Member implements Named { @Override String name.get() = "member"; }`. Close its tab and query Named's `name`. Open the member, insert two blank lines without saving, break its declaration, then restore it. | The closed member adds its getter. Unsaved positions move by two lines; parse failure clears implementation results; correction restores them. | + +Lookup requires successful module analysis and uses copied source identities. It does not enable +property rename, infer a runtime delegate receiver or search every configured module for additional +implementations. Source Ref/Var annotation accessors are covered by X72 below; binary-only +accessors such as `@Lazy.get` still require an explicit host source index to navigate. + +### K. Delegation, function calls and broader cursor contexts + +Create Advanced.x: + +```xtc +module Advanced { + interface Api { T map(T value); @RO T name; } + class Engine implements Api { + @Override String map(String value) = value; + @Override String name.get() = "engine"; + } + class Forward(Engine target) delegates Api(target) {} + class Outer(Forward target) delegates Api(target) {} + String read(Outer forward) = forward.map(forward.name); + class Base { T pick(T value) = value; } + class Child extends Base { @Override String pick(String value) = super(value); } + Int apply(function Int(Int) fn) = fn(42); + Int pair(Int first, Int second) = first; + Int edit(String word, Boolean flag) { return 1 + word.size; } + annotation Trace into Var { + @Override Referent get() = super(); + @Override void set(Referent value) { super(value); } + } + class Measured { + @Trace Int count = 1; + @Trace Int computed { + @Override Int get() = 3; + @Override void set(Int value) {} + } + } + Int readMeasured(Measured meter) = meter.count + meter.computed; + Int applyPair(function Int(Int, String) fn) = fn(1, "x"); + class Packet { construct(T first, T second) {} } + Packet buildPacket() = new Packet("a", "b"); + annotation Marked into Object {} + class Envelope { class Part { construct(T first, T second) {} } } + void constructions(Envelope outer, Packet packet, String text, Int textNumber) { + outer.new Part("a", text); + packet.new("a", text); + new @Marked Packet("a", text); + Packet inferred = new Packet("a", text); + new Packet("a", text); + new String[2](text); + } + Object literal(String word) = [word.size]; + interface CursorReader { String read(); } + void anonymousConstructions(String text, Int textNumber) { + new Packet("anonymous", text) { String read() = text; }; + new CursorReader("a", text) { + construct(String first, String second) {} + @Override String read() = text; + }; + } + void take(Int first, String second) {} + void values(Int number, String text, Boolean flag, function Int(Int, String) fn) { + Int textNumber = 1; + take(1, text); + fn(1, text); + new Packet("a", text); + } + void choose(Int value) {} + void choose(String value) {} + void alternatives(Int number, String text, Boolean flag) { choose(text); } + void prefixes(Int valueNumber, String valueText, Boolean valueFlag) { choose(valueText); } + class ArgumentValues { + private String valueText = "text"; + static String valueConstant = "constant"; + Int valueNumber = 1; + static void takeValue(String value) {} + void inspectValues() { takeValue(valueText); } + static void inspectConstants() { takeValue(valueConstant); } + } +} +``` + +| # | Action | Expected result | +|---|--------|-----------------| +| X68 | Find Implementations on `forward.map` and `forward.name`. | Engine's written `map` and `get` bodies, reached through both delegation layers. No generated forwarding target. Interface-typed and cyclic delegates remain unresolved. | +| X69 | Go to Definition on `super(value)`. Try Prepare Rename there. | Base's `pick` body; the `super` keyword cannot be renamed. Configured-graph method-family rename preserves this call target without editing the keyword. | +| X70 | Request signature help inside `fn(42)` and Go to Definition on `fn`. | `Int fn(Int)` with no guessed parameter name; definition reaches the function parameter, without claiming its runtime callable target. | +| X71 | In `edit`, replace `1 + word.size` with `1 + word.`, `flag ? word. : 0`, `flag ? 0 : word.`, and `pair(word., 2)` in turn. Complete after the dot, then restore the original. | `size` is offered in the original lexical context, including when another argument follows the cursor. No stale result or fabricated value for the incomplete expression. | +| X72 | Find Implementations on `meter.count` and `meter.computed`. | `count` reaches Trace's written `get` and `set`; `computed` reaches its explicit `get` and `set`, which precede the annotation. Property queries return the combined accessor set, not a read/write-specific target. Native annotation storage has no invented source body. | +| X73 | In `applyPair`, replace the call with `fn()` and then `fn(1, )`; request signature help after `(` and after the comma. Try `fn(True, )`, then restore the original. | `Int fn(Int, String)` with the corresponding parameter highlighted, no invented names/defaults/runtime target. The incompatible Boolean argument produces no candidate. Normal diagnostics clear after restoring the complete call. | +| X74 | In `buildPacket`, replace the construction with `new Packet()`, then `new Packet("a", )`, then `new Packet(second = "b", first = )`; request help at each missing argument, then restore. | `new Packet(String first, String second)`; active parameter is respectively first, second, first. Written arguments fit a candidate, but an unfinished call does not select a constructor. Specialized constructions are exercised separately in X83–X85. | +| X75 | In `edit`, replace `1 + word.size` with `(word.si`, `pair((word.si`, and `word[word.si` in turn, leaving the semicolon and method/module braces. Trigger completion after `si`, then restore. | `size` is offered despite missing `)`/`]`. The edit replaces only `si`; Problems continues showing the normal compiler errors until the source is repaired. No missing operand or value is invented. | +| X76 | In `edit`, replace `1 + word.size` with `pair((pair(1, `, leaving the semicolon and braces. Request signature help after the comma, then restore. | The innermost `Int pair(Int first, Int second)` candidate highlights `second` despite the missing call/group delimiters. Problems clears after restoring the original source. Both drivers also reject overlapping semantic-token ranges before and during recovery, including anonymous construction type names. | +| X77 | In `values`, remove `text` from each of the three calls in turn and invoke completion at the empty slot. Also try `take(first = 1, second = )` and `new Packet(second = "b", first = )`. Accept `text`, then restore before the next edit. | The compiler offers compatible `text`, the bundled module properties `simpleName` and `qualifiedName`, and the empty-string literal `""`, with an insertion at the cursor. `number`, `flag` and `fn` are excluded; VS Code may also show its independent snippets. The completed source compiles and Problems clears. Ordinary methods, function values and explicitly parameterized constructors use compiler argument fitting. | +| X78 | In `alternatives`, remove `text` from `choose(text)` and complete inside `choose()`. Request signature help before accepting `number`. Restore and repeat with `text`. | Both `number` and `text` are offered, along with compatible module properties `simpleName` and `qualifiedName` and literals `0` and `""`; `flag` is excluded. Signature help retains both overload candidates until a value is inserted. Either accepted completion compiles and clears Problems. | +| X79 | In `values`, shorten `text` to `te` in each of the three calls and invoke completion at its end. Also try `take(first = 1, second = te)` and `new Packet(second = "b", first = te)`. Accept `text`, restoring between edits. | Only compatible `text` is offered; same-prefix `Int textNumber` is excluded. The edit replaces exactly `te`, preserving the label, commas and closing delimiter. The completed source compiles and Problems clears. | +| X80 | In `prefixes`, shorten `choose(valueText)` to `choose(va)` and invoke completion after `va`. Request signature help, then accept `valueNumber`; restore and repeat accepting `valueText`. | Both compatible names are offered; `valueFlag` is excluded. Both overload signatures remain until acceptance. Exactly `va` is replaced, the completed call compiles and Problems clears. | +| X81 | In `ArgumentValues.inspectValues`, shorten `takeValue(valueText)` to `takeValue(va)`, then try `takeValue(value = va)`. Complete after `va`, accept `valueText`, and restore between edits. | The compatible property `valueText` and constant `valueConstant` are offered with String types; `valueNumber` is excluded. Exactly `va` is replaced, preserving the label and delimiter. Acceptance clears Problems. | +| X82 | In `ArgumentValues.inspectConstants`, shorten `takeValue(valueConstant)` to `takeValue(va)` and invoke completion. Accept `valueConstant`. | Only the compatible constant is offered; instance properties require an instance receiver. The completed call restores the original source and clears Problems. | +| X83 | In `constructions`, shorten the final `text` to `te` in `outer.new Part`, `packet.new`, and `new @Marked Packet` in turn. Request completion and signature help; accept `text`, then restore. | String constructor parameters and active parameter `second`; only `text` fits the prefix, not `textNumber`. Acceptance replaces exactly `te` and clears Problems. | +| X84 | Shorten `text` in the two constructions with omitted ``. Request completion and signature help, then accept `text`. | The declaration's `Packet` expected type constrains completion to `text`. The standalone construction also permits `textNumber`: its provisional String signature can change as more arguments determine the omitted class type. Neither unfinished call selects an overload. | +| X85 | Replace `new String[2](text)` with `new String[2](te)`, then `new String[2](supply = te)`. Request help and accept `text`. | Parameter `supply` is active (index 1), since `[2]` supplies `size`. Only the compatible String value is offered. Acceptance clears Problems. | +| X86 | In `literal`, replace `[word.size]` with `(1, word.si`, `Tuple:(1, word.si`, `[word.si`, and `["key"=word.si` in turn, leaving the semicolon. Complete `size`, then restore. | Member completion survives missing tuple/list/map closers and replaces only `si`. Accepting the member alone leaves delimiter diagnostics; restoring the complete expression clears Problems. | +| X87 | Before the module's final brace, add `Int declaredSize(String word) = word.si`, `Int declaredSize = "x".si`, or `void defaults(Int size = Int64.Ma {}` in turn. Complete at the prefix, then repair the missing `;` or `)`, and remove the added declaration. | `size`/`MaxValue` is offered in the declaration's real compiler context. The missing terminator remains a normal diagnostic until repaired. Method defaults must still be constants. Shorthand constructor defaults have backend coverage. | +| X88 | In `anonymousConstructions`, shorten `text` to `te` in `new Packet("anonymous", text)`. Request signature help and completion, then accept `text`. | The inherited constructor has String parameters and active parameter `second`. Only `text` fits; acceptance clears Problems. The anonymous method still captures its enclosing `text`. | +| X89 | Shorten `text` in `new CursorReader("a", text)` to `te`. Request help/completion, accept `text`, then repeat after deleting the constructor call's `)` before `{`. Restore the fixture. | The constructor declared inside the anonymous body supplies `new CursorReader(String first, String second)`, without a generated class suffix. Completion replaces only `te`. Missing-`)` diagnostics remain until the delimiter is restored. | +| X90 | In `constructions`, replace `new String[2](text)` with `new String[te](text)`. Request completion and signature help inside the brackets, then accept `textNumber`. Repeat with `new String[te` before the semicolon, then restore `](text)` and the fixture. | Only the Int value `textNumber` fits the size prefix; the String value `text` is excluded. Help shows the fixed-size Array constructor with `Int size` active at index 0. Acceptance replaces only `te`. A missing bracket remains a diagnostic until repaired. IntelliJ verifies the full candidate set, visible size highlight and exact accepted edit; the disposable test profile disables sole-candidate auto-insertion. | +| X91 | In `Editing.x`, temporarily use `module Editing { String StringValue = "x"; void damaged(Str value) {} Int later = 1; }`. Request completion just after `Str`, then accept `String`. Repeat with `void damaged(Int first, Str second) {}`, `Str property;`, and `Str damaged() = "x";` in the same module, then restore the fixture. | Compiler type candidates include `String`, exclude the value `StringValue`, and replace only the three prefix characters. No call signature is shown in the declaration header. Each repaired source clears diagnostics. Both native drivers use these same inputs and assertions. | +| X92 | In `Editing.x`, use a multiline `module Editing` with `void damaged(Int) {` on line 2, `Int hidden = 1;` on line 3, its closing brace on line 4, and `Int later = 1;` afterward. Repeat with the header `void damaged(Int value, Str)`. Repair either header to `void damaged(Int value)` and restore the fixture. | Diagnostics remain while the parameter header is incomplete. Outline includes `Editing`, `damaged` and `later`; `hidden` does not leak from the skipped body. The method folds through its actual closing brace. Repair clears diagnostics. IntelliJ verifies native diagnostics, Problems rows, Structure inclusions/exclusions and the exact fold at zero-based lines 1–3 for both variants. | +| X93 | In the same temporary `Editing.x` module, complete `ecstasy.text.Str` as a parameter type, property type and return type (return `new StringBuffer()`). Accept `StringBuffer`. Then use the X93 source below (also in the shared catalog): `Owner` extends `Base` and declares public/private/protected nested types, a typedef and a value; `Alias` imports `Owner`. Complete `Owner.Ite` and `Alias.Ite`, accepting `ItemPublic` and `ItemAlias`. Restore the fixture. | The bundled XDK qualifier resolves; only `Str` or `Ite` is replaced. Owner/alias candidates include inherited `ItemBase`, public `ItemPublic` and typedef `ItemAlias`, and exclude `ItemPrivate`, `ItemProtected`, `ItemValue` and enclosing `StringValue`. No call signature appears. Every accepted edit compiles and clears diagnostics. Both native drivers use the shared variants and assertions. | +| X94 | In temporary `Editing.x`, complete `Str` in `List`, `Map>`, `Map`, `List<(Int \| Str)>`, `Object + Str` and `Object - Str` parameter headers. Also try `List` property/return types and `List`. Use the shared X94 variants. Finally remove both `>` from the nested `Map` header, accept `String`, then restore `>>`. | Only the selected leaf token changes; generic arguments, compound operators and qualifiers stay intact. Values such as `StringValue` are excluded and no call signature appears. Complete accepted headers clear Problems. Missing `>` still reports an error after acceptance; adding the closers clears it. Type suggestions establish visibility, not generic-constraint compatibility. Both drivers consume the same nine variants. | +| X95 | In temporary `Editing.x`, use the shared X95 replacement program. Complete the marked type in class `extends`, interface `extends`, `implements`, `delegates`, `incorporates` and mixin `into` headers. Also try `Owner.Nes`, `List` and the missing-`>` variant. Accept the selected entry, then restore the repaired declaration. | Completion replaces only the final token and offers visible types rather than values. Complete accepted headers clear Problems; accepting `String` with a missing `>` leaves an error until the closer is restored. No signature appears in a type slot. Both drivers consume the same nine variants; native IntelliJ assertions pass. | +| X96 | In temporary `Editing.x`, use the shared X96 replacement program. Complete registered `Element` in a type prefix and empty generic slot, `String` before a later generic argument, and `Item`/`Alias` after `Owner`. Put the cursor inside `String`, `StringBuffer` and `Item`, then accept the selected entry. | All eight variants preserve surrounding syntax and clear Problems after acceptance. Empty slots insert at the cursor; mid-token edits replace the whole identifier without duplicating its suffix. Parameterized aliases retain their substituted compiler type. No signature appears. Both editor drivers consume the same data; native IntelliJ assertions pass. | +| X97 | In temporary `Editing.x`, run the shared argument-context variants: later/named arguments, groups, qualified properties, function values, construction, operators and missing operands. Accept the specified variable or literal in each row. | Fitting rejects incompatible values, preserves surrounding syntax and exact edits, retains signature help and clears diagnostics after acceptance. Literal rows verify Value kind and sort metadata; method rows verify documentation and active parameters. The 30 shared variants include character, decimal, binary, array, map and tuple insertions; see the latest L64 receipt for validation. | +| X98 | Complete `Li|st` and `Li|`, including a nested `Map` argument. | Only the base identifier is replaced; `` and nested delimiters survive. The accepted source compiles. VS Code passes in `run-KoAP6K`; the IntelliJ consumer compiles and awaits its native checkpoint. | +| X99 | In an isolated discovered workspace, create `LiveLibrary.x` with `module LiveLibrary { static Int value() = 1; }` and `LiveConsumer.x` with `module LiveConsumer {}`. Add `package lib import LiveLibrary; Int run() = lib.value();` to the consumer without saving. Change the library result to `String`, then discard it. | Definition reaches `value`; consumer Problems updates for the incompatible unsaved dependency and clears on discard. Neither buffer is saved by the server. | +| X100 | Create a healthy module with `class Base {}` and `class Child extends Base {}`, plus an independent module. Break the neighbor with `Missing broken;`, then request Base's subtypes. | Child remains navigable. Exact references return no complete answer while the graph is broken. Repairing the neighbor restores full graph queries. | +| X101 | Open `module LibrarySource { package xml import xml.xtclang.org; void accept(xml.Document document, String text) {} }`. Go to Definition on `Document` and `String`, and Type Definition on `text`. | Matching XDK source opens at the declaration token, read-only. Library symbols cannot be renamed; the VS Code formatting provider returns no edits. IntelliJ Reformat opens **Clear Read-Only Status**: cancel it, then verify the buffer/disk bytes and read-only status are unchanged. The native Rename action displays the server’s rejection. Missing/ambiguous source metadata gives no guessed target. | +| X102 | In a source graph, declare Base's `Int value` getter and Child's `@Override Int value` getter; use both properties. Rename `value` to `amount`. | Both declarations and uses change. `get`, `set` and setter parameters keep their names. Binary contracts and annotation/delegation families remain unavailable. | +| X103 | Create `MoveType.x` using `Item` and `Item.Nested`, `MoveType/Item.x` declaring `Item`, and `MoveType/Item/Nested.x` declaring `Nested`. Rename `Item` to `Renamed`, then undo. | Source edits, `Item.x` → `Renamed.x` and the companion directory move apply together. Both directions compile and preserve the nested source. Existing destination files/directories block the move. Simple discovery-managed module roots can also be renamed; standard LSP rename still refuses configured-root changes; the bundled host proposal path handles them in X118. | +| X104 | Import `ecstasy.text.StringBuffer as Buffer`; use `Buffer` in a type and construction. Rename the alias to `Builder`. | Three alias tokens change; `StringBuffer` and nested aliases remain unchanged. | +| X105 | Use unresolved `Document` in a type header; apply the import quick fix for `xml.xtclang.org`. Then try unresolved `Widget` declared in a separate discovered source module. | Only imports whose complete graph compiles are offered; applying clears diagnostics. Source imports also add the discovered dependency edge. Explicit graphs do not silently gain dependencies. | +| X106 | In Editing.x, complete `Str` inside `function Str(Int)`, `function void(Str)`, `function (Int, Str)(Int)` and `Function<, >` parameter types. Repeat with a missing function/sequence closer. | Only the written leaf token is replaced. Complete forms compile; missing delimiters remain diagnostics until repaired. No callable signature is invented for a declaration header. The same eight variants are implemented in both drivers. | +| X107 | Complete a trailing `ecstasy.text.` type, edit `te` inside `ecstasy.text.StringBuffer`, and fill empty union/intersection/difference operands. | Insert only into an empty slot or replace only the selected qualifier token; preserve the remaining suffix and compile the accepted source. | +| X108 | Complete type names in multiple-return lists, generic method/class headers, package compositions and written `Element`/`Other` formals, including `Chain` recursion. | Preserve exact ranges and restore compilation. Recursive constraints are explicitly labelled written syntax with no invented formal identity. The 17 shared variants include compound/nullable/immutable recursion and a cursor inside its own bound; see the latest L64 receipt for validation. | +| X109 | In the shared `X109` fixture, public parameter rename updates a closed caller and preserves another overload. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X110 | In the shared `X110` fixture, override parameter slots join differently named declarations and callers. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X111 | In the shared `X111` fixture, constructor parameter rename starts at a named argument. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X112 | In the shared `X112` fixture, delegation rename joins the contract and concrete receiver. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X113 | In the shared `X113` fixture, mixin method rename preserves contextual super. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X114 | In the shared `X114` fixture, mixin property rename updates declarations and access. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X115 | In the shared `X115` fixture, annotated property rename preserves its Lazy implementation. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X116 | In the shared `X116` fixture, qualified module rename preserves its domain and alias and moves its companion. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | +| X117 | In the shared `X117` fixture, find references on `Box` in `App.x`, then rename implicit package `tools` to `helpers`. Keep the companion file closed. | References include the use in `App.x` and declaration in `App/tools/deep/Box.x`; synthetic implicit packages are not files and must not cause an internal error. Both drivers assert these installed-client references before native Rename/Undo. Compare every expected source/path after rename and restore every original with one Undo. | +| X118 | Repeat X116 with explicit sourceModules settings containing Library.example.org and Consumer → Library.example.org. | Rename changes the root URI, module name and dependency edge in settings; Undo and Redo restore the matching graph, source contents and resource paths together. Save All in VS Code persists the settings document with the sources. IntelliJ uses the Ecstasy Compiler project settings page (Reset/Apply) and verifies the same project graph follows native history. | +| X119 | Rename the primary constructor property input in Library.Box with Consumer closed. | Header, constructor label and property access change together; Other.input stays unchanged. Undo restores every source. | +| X120 | Rename a lambda input captured by a nested lambda. | Declaration and nested capture change; sibling lambda input stays unchanged. Undo restores the source. | +| X121 | Rename a method input while a closed consumer stores its method value and also calls it by name. | Direct label and selected declaration change; positional function calls and Other.pick stay unchanged. Undo restores both files. | +| X122 | At Box in each shared Actions variant, apply Implement or Override, then Undo/Redo/Undo. Cover ordinary signatures, a missing implementation diagnosed at construction, a conditional multiple return with a parameterized type/default, a generic method, an existing named call through a descendant, atomic implementation of all required members, a bundled Iterator contract, a compound return and fresh literal-default repair. Finish with an already implemented contract. | Both editors discover the action, compare the complete generated source and compile it. Native history restores exact source; the construction diagnostic returns after Undo and clears after Redo. No duplicate implementation is offered. | + +| X123 | Replace Navigation.x with the shared broken source, observe Problems, pull diagnostics twice with the returned result ID, repair the source, and pull using the old ID. Restore the fixture. | Both editors display and clear the compiler error. The installed connection returns full, unchanged, then empty full reports with a new result ID. | +| X124 | Configure a nonexistent nested external resource directory for Assets.x; test Reset/Apply, empty roots, create/delete/recreate data.txt, then replace the configured root with another missing directory and create it. Keep the editor focused; do not open or manually refresh the external directory. | Missing-resource diagnostics clear and return through automatic native watchers; settings preserve the selected roots. Old receipts with manual VFS refresh do not count for this version. | +| X125 | Hover a generic echo call; request signature help before an existing positional/named argument; complete direct and chained `.tr()`; navigate the narrowed JsonObject variable's type. | Hover identifies echo, active parameters are 1/0, completion offers trim, and type definition opens bundled Map.x. | +| X126 | Request full tokens, insert a leading newline, request a delta and a range when negotiated, then close/reopen and request using the old ID. | Applying edits reconstructs the full result; empty ranges are empty; retired IDs return full data. Unsupported client operations are refused explicitly. | +| X127 | Select the unused-import action by its shared title (other actions may also be offered), resolve its edit when negotiated, edit the source, then try resolving the old handle. | Initial lazy actions omit edits; resolve returns versioned edits without changing the title; obsolete handles are refused. Eager-only clients retain complete actions. | +| X128 | Rename Box.x to Crate.x and an implicit tools package folder to util using the IntelliJ project-tree Rename action (Shift+F6), or the VS Code file Rename action. | Compiler-proven declaration/reference edits and the move apply together; root and member diagnostics remain clear. IntelliJ invokes the registered handler and actual Rename dialog. Raw VFS moves occur too early for LSP4IJ's before-listener to obtain proof and are not equivalent coverage. | +| X129 | Import an evaluated Gradle model with processed resources; refresh to empty roots and back; try a malformed report; enable an explicit empty-resource override and refresh again; reset to the build model. | Diagnostics follow the imported paths, malformed reports retain the last valid import, refresh preserves explicit settings, and resetting restores the model. Both drivers exercise the installed client configuration path. | + +| X130 | Select the two shared module containers, Move them into the destination directory, then Undo and Redo once. | Both modules, member files and embedded resources follow the move; the unchanged consumer still resolves its imports. IntelliJ uses the Community Move handler/dialog; VS Code uses Explorer Cut/Paste and native Undo/Redo once each. Its host repaint exception still fails the case even when the move assertions succeed. | + +| X131 | Request code lenses, document links, inlay hints and workspace symbols; resolve deferred properties when negotiated; edit the source and retry old handles. | Stable positions/labels, preserved command arguments, complete resolved payloads and explicit stale-handle refusal. Both drivers use their installed client connection; existing UI cases cover presentation/navigation. | +| X132 | Request formatting for overlapping and disjoint line ranges, then negotiated pre-save hooks with default options. | Formatting edits are sorted and deduplicated; default save hooks return no edits. Both clients retain the unchanged buffer. Incremental UTF-16/CRLF patches and opt-in save edits have backend regression coverage. | +| X133 | Use two methods with same-spelled locals. Request linked editing on one local, on a type and on a parameter; introduce a source error, then repair it. | Exactly that local declaration/use pair is linked; unrelated bindings, nonlocal symbols and invalid-source results stay unlinked. Both drivers assert the installed protocol; native linked-typing presentation remains separate. | +| X134 | Configure an unopened Library.x outside the workspace and a local consumer. Change its return type externally, restore it, delete it and recreate it. | Consumer Problems appears/clears from OS file events without opening Library or manually refreshing VFS in either host. | +| X135 | Use the server-log actions to open/hide the docked log twice, then return to the source. | The log view opens and hides without modifying source. Physical shortcut dispatch, remapped shortcuts and alternate docking are manual checks. | +| X136 | Open the language-service settings, change inlays, Reset/Cancel, then Apply; inspect effective configuration. | Live presentation changes preserve source and compiler graph ownership; report includes PID, adapter and queue names/count. IntelliJ drives the actual settings components; VS Code uses native configuration and the contributed report command. | +| X137 | Leave a buffer unsaved; select Incremental text synchronization, then Full. | Each applied transport change restarts the existing connection and restores current unsaved text. This is text transport, not incremental compilation. | +| X138 | Change indentation to 2, format, then supply an invalid indentation value. | Valid change applies without restarting; invalid settings retain the previous formatter. Effective status identifies native save-hook availability. | +| X139 | Enable native format-on-save and save an unindented source. In IntelliJ also leave a second dirty document closed, then Save All. In VS Code also select server save formatting with native formatting disabled. | Open and closed IntelliJ documents are indented without formatting-error notifications or a file-cache conflict. VS Code suppresses the server hook when native formatting is enabled; IntelliJ uses native Actions on Save because LSP4IJ has no server save-edit hook. | +| X140 | Open the shared source with an emoji before an `Int` reference on the same line. Hover and prepare rename on the final `value`. | UTF-16 is explicit; hover resolves `Int` and rename selects exactly `value`, without shifting after the surrogate pair. | +| X141 | Switch runtime server trace between `messages` and `verbose`, then request hover. Restore the editor trace setting. | Both log the method and timing; only verbose adds correlation metadata. No source buffer or request payload appears in server trace notifications. | +| X142 | Open configured `FoldedInitializer.x`; hover and navigate from `Int copy = value`, find references, rename `value` to `number`, then Undo. | The declaration, folded initializer and method body share one semantic identity; all three rename together, compile cleanly and restore on Undo. | +| X143 | Configure `PartialSymbols.x` with 130 classes. Compare ordinary workspace symbols with a request carrying a partial-result token. | Ordered progress batches contain at most 64 symbols each; their combined names match the ordinary result exactly and the final response is empty. | +| X144 | Apply a current versioned text edit through the installed client, Undo it, then send a two-document edit with one stale version. | Current edit and native Undo succeed. A stale target refuses the whole batch and preserves both current buffers. Drivers invoke the installed application handler; packaged tests separately exercise server-to-client transport. | +| X145 | Replace ProgressWork with the shared workload and request references. Verify the progress source/activity text, cancel, then check hover; repeat and restart while pending. | Cancellation terminates only the request; progress disappears. Restart preserves unsaved text, retires the pending reader and exits the old PID. Visible VS Code Cancel-control checks use 20,000 methods; automatic IntelliJ Cancel, the ordinary VS Code callback and restart use 5,000. A request finishing too early fails as unexercised. Historical model-cancellation receipts and current visible-control acceptance are distinguished in the L81 section below. | +| X146 | Open RefreshConsumer with inferred `var value = lib.make()`. Change only RefreshLibrary from returning Int to String, then restore it. In IntelliJ, also keep at least 16 additional documents connected and issue a burst of lens/hint/token refreshes before the dependency changes. | Both clients receive provider refreshes and show the changed inferred-type hint. Consumer text/version stays unchanged. IntelliJ reads the native cached inlay result and must report no read-action overload; its driver restores the temporary tab limit. VS Code observes the registered provider's refresh events and result. | +| X147 | Hold an older settings report while requesting a newer one, changing settings, restarting or closing the settings page. Complete the old reply last. | Older reports cannot overwrite current or disposed UI state. VS Code delays a real server reply; IntelliJ drives the real settings component with controlled asynchronous report data and an EDT completion barrier. Restart retires the old PID and preserves source. | +| X148 | Open the shared Extract.x fixture, select exactly `42` in the return statement, and choose Extract literal to local variable. Undo, Redo, then Undo. Repeat with a string/character literal; try selecting a call or only part of a literal. | The action inserts `val extractedValue = 42;` immediately before the return, which uses that local. Existing identifiers force a fresh suffix. Exact text, indentation and diagnostics survive Undo/Redo. Unsupported selections offer no extraction. Selected X148 acceptance passes in both editors. | +| X149 | Open the shared Naming.x variants. Complete `str` in the middle of `strange` to `string`; repeat in a parameter, then complete `st` with another `string` already present. Undo each acceptance. | The whole declaration token is replaced. The collision variant proposes `string1`. No reference or rename is claimed. Both drivers pass exact source, diagnostics and one Undo (see the per-case acceptance reports). | +| X150 | Open the shared Templates.x variants. Complete `cl` with `class declaration`, then `if`, `for`, `do` and `try` with their block templates, and `interface` with its declaration template. Inspect the selected name/condition, press Tab to reach the body, and Undo. Also try `mo` in an empty file and completion inside a string, comment, member access and argument. | Templates appear only in the corresponding file/member/statement context. Placeholder navigation leaves exact indentation and source unchanged; Undo restores the prefix. IntelliJ may first restore the placeholder selection without changing text, then undo the insertion. Comments, strings and value/type positions receive no syntax templates. Clients without snippet support receive plain text without placeholder syntax. Both editor cases pass; syntax exclusions and minimal-client behavior have backend/protocol coverage. IntelliJ uses the documented UP18 indentation constraint. | +| X151 | Open the shared MissingNames.x variants. Invoke completion after the space following the property or parameter type, before the initializer or closing parenthesis. Accept `string1`, `string`, `list`, `stringArray`, `fn` and `value` across named, nullable, array, function and compound types, then Undo each insertion. Also try an empty name at EOF, an already written name, a comment and a bare local expression. | Completion inserts only the suggested name at the empty cursor range, preserving the written type, whitespace and remaining source. Collision checks include existing document identifiers. Accepted variants compile; one Undo restores the incomplete source. Existing names, comments and ambiguous expressions acquire no empty declaration-name slot. X151 passes in both editors; EOF, UTF-16/CRLF and refusal controls pass in parser/backend/protocol tests (see the per-case acceptance reports). | + +| X152 | Open the shared SemanticValues.x variants. Complete `thi` to `this.Owner`, then `this.Ow` to `this.Owner` inside a nested class call. Complete an empty indexed callback argument with `0`. Undo each acceptance. | Only an enclosing instance that fits the actual call is offered. Qualified insertion replaces only the selected member. Indexed functions retain their function parameter type. Every accepted document compiles and Undo restores the exact incomplete source. Static-boundary and incompatible-type refusals have backend coverage. | + +For a project using the updated Gradle plugin, run `./gradlew exportXtcLspModel` in that project's +root to export `.gradle/xtc/lsp-model.json`. Run `./gradlew prepareXtcLspModel` to process resources +and export the model together. These tasks do not compile application modules; declared source +generators may run to establish source membership. In IntelliJ, open **Languages & Frameworks > +Ecstasy Compiler**; in VS Code, invoke **Ecstasy: Configure Compiler Paths**. Inspect the model origin, +main/test owner, source roots, processed resource roots and missing outputs. Use **Refresh Gradle +model**, **Prepare generated resources**, **Open build file** and reset controls. An explicit +source/resource override must survive refresh. Missing or malformed reports must not crash the +server. X129 covers the model/settings lifecycle; X260–X262 invoke the real Gradle wrapper from +these controls against a shared gated producer. TestKit covers the actual XTC producer tasks. Automatic IntelliJ Gradle +sync and nested/composite build aggregation are still follow-ups. + +**Build import cancellation and ownership (UI5/UI6):** both controls now keep the previous +accepted model while Gradle runs. Use a project whose generated-input task is long enough to +cancel; generated files themselves are not rolled back by cancelling an import. + +1. Import a valid model and note the effective resource roots and current diagnostics. +2. Start **Prepare generated resources**. VS Code shows a cancellable Ecstasy notification and + the owned task terminal; IntelliJ shows a cancellable background task with the project path. + Invoke Refresh again while it runs: the second request must refuse without starting another + task for that folder/project. Other workspace folders retain independent ownership. +3. Cancel through the native progress control. The operation must finish as cancelled, retain + the previous compiler inputs, and remain available for retry. Cancellation is not a compiler + diagnostic or an error notification. Inspect **Show Compiler Paths** in VS Code or reopen the + IntelliJ Compiler page for the last import result/time. +4. Exercise a task that writes a valid new model and then fails. Watcher/configuration callbacks + must not publish that model during the task or after its failure. A successful explicit retry + may accept it. Existing explicit source/resource overrides must still be preserved. +5. Remove the owning VS Code folder or close the IntelliJ project during preparation. Its owned + progress is cancelled; an old callback must not replace another operation's model. Also try + a missing wrapper and malformed/missing output, then repair and retry. + +Automated ownership regressions cover failed/cancelled output and stale callbacks. X129 retains +model/settings acceptance; X260–X262 add real Gradle tasks, visible Cancel and rejected-output/retry +checks through both installed clients. Project/folder closure during import and a representative +user project's generated-input task remain manual acceptance steps. + +X122 exercises eleven library member-generation variants, comparing complete source and +native Undo/Redo/Undo in both editors. +Member generation uses `refactor.rewrite` so the class intention remains available without a +diagnostic at the class name. Library contracts stay read-only while implementations are inserted +in user sources. Unvalidated computed defaults and unsupported type/constant spellings still refuse +an action. The final variant checks that `read` is not offered again; other inherited overrides may +legitimately remain available. + +For X93's nested-type and alias variants, temporarily replace `Editing.x` with this source. +Complete immediately after `Owner.Ite`, accept `ItemPublic`, then replace that type with +`Alias.Ite` and accept `ItemAlias`. Restore the original fixture afterward. + +```text +module Editing { + import Owner as Alias; + class Base { class ItemBase {} } + class Owner extends Base { + class ItemPublic {} + private class ItemPrivate {} + protected class ItemProtected {} + static Int ItemValue = 1; + typedef String as ItemAlias; + } + String StringValue = "x"; + void damaged(Owner.Ite value) {} + Int later = 1; +} +``` + +Argument-value suggestions cover visible readable locals/parameters and implicit properties/constants +in empty final positional slots, pending named values and direct final bare-name prefixes. They use +compiler inference, conversions and receiver-specific types. Locals retain flow narrowing; ordinary +property reads do not gain that narrowing. Lexically visible enclosing/imported property names +are checked by ordinary read validation and argument fitting; arbitrary enclosing-instance enumeration and literal synthesis remain unsupported. +X97 fits qualified/grouped values, compound operators and empty/prefix operand slots against the +complete argument expression and later written arguments. Type-valued and receiver-rewritten +function fallbacks preserve visible signature parameter mapping. Outside a call, missing operand +slots provide lexical completion without an argument-fit claim. X83–X87 add specialized +constructors and bounded declaration/literal recovery. +X88–X89 add anonymous superclass forwarding and constructors declared inside retained bodies, including +interface implementations and captured locals. Cursor analysis prepares declaration signatures but does +not validate capture behavior or emit unfinished bodies. X91–X92 add simple unqualified member/return +and method-parameter type prefixes, including empty parameter type slots in API tests. Incomplete +names/signatures are never invented. X93 adds flat qualified type prefixes, imported qualifiers, +visibility, inherited types and final-token edits. X94 adds written leaf names inside parameterized +and compound headers, nullable/array wrappers (API tests), and bounded missing angle/group closers. +X95 adds class/interface composition type slots, qualified/generic leaves and missing-angle repair. +Candidates are visible types; the full generic constraints are checked by normal compilation. +X96 adds registered formals, empty generic slots, parameterized qualifiers and whole-final-token edits. +The focused X94–X96 run `run-MdzjLq` passes all three cases; 98 other cases are not selected. +X98 adds generic base-name completion while preserving written type arguments. +X106 adds function-parameter/return and sequence leaf types. X107/X108 add trailing dots, empty type +operands, qualifier-middle edits, and generic/multiple-return headers. Unregistered header formals +provide bound-labelled completion/hover after compiler constraint resolution, including sibling +bounds and qualified children. Cyclic/unresolved bounds still shadow outer names without fabricated +type identities; module/package compositions have bounded +compiler recovery. X90 adds empty/final-prefix single-dimensional +size slots, including a missing `]`; fitting uses the real Array constructor's Int parameter. The +prefix query does not validate a following supplier. Types without an element default still require +a supplier when compiled normally. Multidimensional construction and unfinished declaration names +remain unsupported; no literal value or declaration token is invented. + ## VS Code Extension Playbook A self-contained QA runbook for verifying the **VS Code extension** against the same feature surface the IntelliJ plugin is tested against in sections 1–19. Use this whenever you ship a `.vsix` (release, snapshot, or local build) and want end-to-end confidence that nothing regressed for VS Code users. Headless regression coverage of the file-association pipeline is provided by `:lang:vscode-extension:testVscodeExtension` (see the [extension README](../vscode-extension/README.md#testing)); the playbook below covers everything that test can't, which is the interactive LSP / DAP / UI surface. @@ -899,8 +2436,8 @@ code /path/to/xtc-project |---|-------|-----|----------------| | P1 | Extension is loaded | Extensions panel → search "Ecstasy" | "Ecstasy Language Support" listed with version `0.4.4`+ | | P2 | `.x` files map to Ecstasy language | Open any `.x` file | Status bar (bottom right) reads "Ecstasy" | -| P3 | LSP server started | Status bar (bottom right) | Shows `✓ XTC` (green check). `⟳ XTC` = starting, `⚠ XTC` = error, `✗ XTC` = stopped | -| P4 | Adapter selection visible | Output panel (`Cmd/Ctrl+Shift+U`) → "XTC Language Server" channel | `Backend: TreeSitter` (or `Backend: Mock` if `-Plsp.adapter=mock` build) | +| P3 | LSP server started | Status bar (bottom right) | Shows `✓ Ecstasy` (green check). `⟳ Ecstasy` = starting, `⚠ Ecstasy` = error, `✗ Ecstasy` = stopped | +| P4 | Adapter selection visible | Output panel (`Cmd/Ctrl+Shift+U`) → "Ecstasy Language Server" channel | `Backend: TreeSitter` (or `Backend: Mock` if `-Plsp.adapter=mock` build) | | P5 | Semantic tokens enabled | Same output channel | `semantic tokens ENABLED (23 types, 10 modifiers)` | | P6 | Java runtime detected | Same output channel at startup | `Java home: /path/to/java` (Java 25+) | | P7 | Snippets registered | Type `mod` then `Tab` in a `.x` file | Expands to a `module` declaration skeleton | @@ -909,7 +2446,7 @@ If P3 stays `⟳`/`⚠`/`✗`, click the status bar item to restart the server. ### Feature playbook -This table maps every numbered feature from sections 1–19 to the exact VS Code action and verification surface. The numeric IDs (e.g. `3.5`) match the test-case IDs in the per-feature sections above — refer there for fine-grained subtests and expected outputs. +This table maps every numbered feature from sections 1–19 to the exact VS Code action and verification surface. The numeric IDs (e.g. `3.5`) match the test-case IDs in the per-feature sections above — refer there for fine-grained subtests and expected outputs. For compiler mode, use the [XdkAdapter playbook](#xdkadapter-playbook); unsupported compiler features in this table are not expected to pass. | § | Feature | VS Code action | Where to verify | Notes | |----|---------|---------------|-----------------|-------| @@ -918,8 +2455,9 @@ This table maps every numbered feature from sections 1–19 to the exact VS Code | 3 | Completion | Type a prefix → `Ctrl+Space` | Completion popup | Subtests 3.1–3.11 — each row in §3 applies verbatim. | | 4 | Go to Definition | `F12` or `Cmd+Click` on a symbol | Editor jumps to declaration | Subtests 4.1–4.10 — see §4. `Opt+F12` peeks instead of jumping. | | 5 | Find References | `Shift+F12` on a symbol | "References" peek view | §5 covers both same-file and cross-file expectations. | -| 6 | Outline | View → Outline (or Cmd+Shift+O for symbols-in-file) | Outline panel populates | §6 — module / class / method hierarchy. | -| 7 | Diagnostics | Save a `.x` file with a known syntax error | Problems panel (Cmd+Shift+M) + red squigglies | §7. The mock adapter reports fewer diagnostics than tree-sitter. | +| 6 | Outline | View → Outline (or Cmd+Shift+O for symbols-in-file) | Outline panel populates | §6 — module / class / method hierarchy. All three adapters. | +| 7 | Diagnostics | Save a `.x` file with a known syntax error | Problems panel (Cmd+Shift+M) + red squigglies | §7. The mock adapter reports fewer diagnostics than tree-sitter; the compiler adapter reports semantics neither can see. | +| 7a | Compiler adapter specifics | Build with `-Plsp.adapter=compiler`; watch the server output channel | Problems panel + the `compile:` lines in the log | §7a — bundled-library startup without `XDK_HOME`, cold vs steady timing, queue depth and superseded edits. | | 8 | Document highlight | Click on an identifier | Other same-name occurrences in file get a subtle highlight box | §8. | | 9 | Selection ranges | Place cursor in expression → `Shift+Opt+Cmd+→` (macOS) or `Shift+Alt+→` | Selection expands outward through AST nodes | §9. | | 10 | Folding ranges | Click the gutter triangles or `Cmd+Opt+[` | Block / method / class folds | §10 — verify all listed scopes fold correctly. | @@ -945,9 +2483,9 @@ These have no IntelliJ analogue (or are surfaced differently). They round out th |---|---------|--------------|----------------| | V1 | File association on stale profile | Open a workspace where `files.associations` in user `settings.json` maps `"*.x"` to `"plaintext"` or another language | Status bar still shows "Ecstasy" within ~1s; the extension's `ensureXtcLanguageAssociation` hook overrides via `setTextDocumentLanguage`. | | V2 | File association on tab restore | Close VS Code with a `.x` file open in a non-active tab → reopen the workspace → click the tab | Tab loads with `Ecstasy` language, not the default. Verifies the `onDidChangeActiveTextEditor` listener. | -| V3 | Status bar lifecycle | Watch the bottom-right status item during server startup, idle, restart | Transitions `⟳ XTC` → `✓ XTC` on start; `✓` → `⟳` → `✓` on Cmd-Palette "Ecstasy: Restart Language Server"; `✗ XTC` on crash (LSP server JAR removed or JVM killed) | -| V4 | Output channel routing | Open Output panel → dropdown | Two channels: `XTC Language Server` (LSP traffic) and `Log (Extension Host)` (extension's own `console.log`/warn/error). No errors in either under normal use. | -| V5 | Trace setting | Set `xtc.trace.server: "verbose"` in `settings.json` → restart server | `XTC Language Server` output channel now shows every JSON-RPC frame | +| V3 | Status bar lifecycle | Watch the bottom-right status item during server startup, idle, restart | Transitions `⟳ Ecstasy` → `✓ Ecstasy` on start; `✓` → `⟳` → `✓` on Cmd-Palette "Ecstasy: Restart Language Server"; `✗ Ecstasy` on crash (LSP server JAR removed or JVM killed) | +| V4 | Output channel routing | Open Output panel → dropdown | Two channels: `Ecstasy Language Server` (LSP traffic) and `Log (Extension Host)` (extension's own `console.log`/warn/error). No errors in either under normal use. | +| V5 | Trace setting | Set `xtc.trace.server: "verbose"` in `settings.json` → restart server | `Ecstasy Language Server` output channel now shows every JSON-RPC frame | | V6 | Java discovery fallback | Unset `JAVA_HOME`, leave `xtc.java.home` empty → restart VS Code | Extension finds Java via `jdk-utils` (SDKMAN, mise, Homebrew, Gradle cache, etc.) — log line `Java home: …` at startup | | V7 | Java auto-download | On a machine with no Java 25+: same as V6, but `jdk-utils` finds nothing | Progress notification `Downloading Java 25 JRE for Ecstasy Language Support`; subsequent restart uses the cached JRE silently | | V8 | Tasks (Gradle build / test / clean / run) | In a workspace with a `build.gradle.kts`, open Terminal → Run Task… | "Build", "Test", "Clean" tasks listed under "xtc"; selecting runs `./gradlew build/test/clean` | @@ -971,46 +2509,1206 @@ The single automated check that exercises the manifest end-to-end: It downloads a pinned VS Code build into `.vscode-test/`, loads the extension from the build tree, opens `src/test/fixtures/hello.x`, and asserts `editor.document.languageId === 'xtc'`. Runs in <30 s after the first cached download. -This is the only automated gate for V1/V2 (file association); everything else in this playbook is interactive. Wire `testVscodeExtension` into CI when you want a continuous canary for the manifest pipeline (needs `xvfb` on headless Linux runners — the wrapper auto-detects). +This is the dedicated automated gate for V1/V2 (file association). The compiler shared scenarios also automate many provider/host workflows; the visual and physical-input checks in this section remain manual. Wire `testVscodeExtension` into CI when you want a continuous canary for the manifest pipeline (needs `xvfb` on headless Linux runners — the wrapper auto-detects). --- -## Future Enhancements +## Additional feature acceptance and remaining gaps ### 20. Linked Editing Ranges -Linked editing ranges enable rename-on-type: when the cursor is on an identifier, -all same-name occurrences in the file are highlighted and edited simultaneously. +The compiler adapter implements linked ranges for repeated, rename-eligible **local variables** +in one successful source snapshot. X133 now checks identity separation, nonlocal refusals, invalid +source and recovery in both installed transports. Linked editing has no proposed-name proof; +use Rename for binding-preserving refactoring. It does not link arbitrary same-spelled names or +cross-file occurrences. | # | Test | Steps | Expected | |---|------|-------|----------| -| 20.1 | Basic linked editing | Place cursor on a variable name used multiple times in a method → trigger linked editing (Ctrl+Shift+F2 in VS Code, or via LSP) | All occurrences highlighted; typing renames all simultaneously | -| 20.2 | Single occurrence | Place cursor on identifier used only once | No linked editing ranges returned (need 2+ occurrences) | -| 20.3 | Parameter name | Place cursor on a method parameter name used in the body | Parameter declaration and all uses linked | -| 20.4 | Class name | Place cursor on a class name that appears in the file | All same-name occurrences linked (same-file, text-based) | -| 20.5 | Non-identifier | Place cursor on a keyword or literal | No linked editing ranges | +| 20.1 | Local identity | Use X133's two methods with same-spelled locals; request linked ranges on the first local. | Only its declaration and use are returned; second method untouched. | +| 20.2 | Single occurrence | Declare an unused local; request linked ranges. | No linked range set requiring multiple occurrences. Backend coverage exists; add this variant to shared acceptance when extending X133. | +| 20.3 | Unsupported symbols | Request linked ranges for a parameter, type/class, property, keyword or literal. | Compiler returns no linked ranges. Tree-sitter's heuristic scope is different. X133 includes type and parameter; remaining token classes are manual/adapter tests. | +| 20.4 | Broken/repaired source | Introduce an unresolved type, request links, then repair it. | Broken semantic snapshots yield no linked edits; local ranges return after repair. | +| 20.5 | Native presentation | Enable the editor's linked-editing feature if supported; type an innocuous local-name change and undo it. | Check synchronized occurrences, cursor/selection and exact restoration. If this host only exposes the protocol, record UI support as unavailable; do not substitute textual multi-cursor selection. | + +### Existing-feature coverage audit (2026-09-30) + +All adapter feature families have implementation checks, but the shared catalog is not a claim +of every native interaction or every language form. The following checks supplement existing +sections instead of silently counting provider responses as complete UI acceptance: + +| Feature family | Existing shared evidence | Additional manual or targeted acceptance still needed | +| --- | --- | --- | +| Diagnostics and Problems | X1/X2/X23/X27/X46–X52/X123, 7a.8/7a.9 | Inspect error/warning presentation, source code and related-location navigation in Problems; verify closed/deleted URIs, duplicate-free warnings, configuration failures without modal loops, and refresh without changing buffer versions. Long editing/idle/restart runs are distinct from one successful correction. | +| Completion and signature UI | X6–X20/X70–X98/X106–X108/X125 | Keyboard and mouse acceptance, Escape without edits, overload selection/active parameter after retrigger, IME/non-ASCII identifiers and end-of-file/no-final-newline boundaries. Existing UTF-16/CRLF and syntax-recovery variants remain mandatory; untested forms are not implied by a fitting result. | +| Navigation, symbols and hierarchy | X3–X5/X21–X22/X28/X33–X44/X59/X63–X69/X99–X101/X131 | Native workspace-symbol search/navigation for closed roots, multiple declaration targets, back navigation, ambiguous/missing source attachments, inherited generic/compound edges and stale closed-file handles. Do not infer a complete conditional-mixin hierarchy or dynamic call graph. | +| Highlights, folds and selection | X1/X4/X41/X92 and L56 startup test | Exact nested selection expansion/shrink, empty/recovered document fallback, folding boundaries after closing/reopening or deleting text, and theme/read/write visual distinctions. Startup race assertions are a separate harness test, not the normal readiness wait. | +| Inlay hints | X42/X131 | Enable/disable through native controls, inspect tooltip rendering for locals/parameters/lambdas, verify no misleading names for unnamed function calls, named arguments or omitted defaults. A protocol tooltip does not prove the UI renders it. | +| Formatting | X31/X132; backend token-preservation tests; sections 13–13c | Apply full/range/on-type edits through native actions; verify literal and multiline-string/comment bytes, CRLF, tabs, final-newline policy, untouched lines and idempotence. X138 verifies live indent settings; X249/X250 verify continuation/comment margins and bounded width wrapping with native history. Backend regressions cover custom indentation and tabs. Not every variant is a shared-driver scenario. | +| Document links | X131 plus lexical unit tests; section 15 | Click HTTP(S) URLs in comments/strings; verify exact ranges and absence of invented import/file/unsupported-scheme links. Malformed/partial literals must not create arbitrary targets. External browser launching is a manual action, separate from resolving a URI. | +| Code lenses / Run | X131 verifies lens identity and command arguments; section 18 | Verify native lens placement and invocation target, no lens for an ordinary class-only member, and current arguments after moving a file. Successful persistent execution/rerun is **R1–R8 work**, not established by showing a Run lens. | +| Semantic tokens | X41/X126, existing token tests; section 19 | Inspect theme fallback, enable/disable behavior, UTF-16/range clipping, edits/close/restart with old result IDs and refresh. Audit per-token claims (e.g. deprecated tags) against actual modifiers rather than claiming every row for every adapter. | +| Rename, actions and resource edits | X53–X63/X102–X105/X109–X122/X127–X130 | Collision/capture refusals, cancellation, stale documents, graph persistence, closed consumers, one Undo/Redo and source text plus filesystem bytes; see the batch matrix. Explorer drag/drop and arbitrary third-party edit application are separate host paths. | +| Discovery, paths and build settings | CFG1–CFG3/X99/X100/X124/X129/X134 | Multiple roots, Unicode/space-containing paths, symlinks, project close/reopen and external shared watch leases. Gradle-import failure, missing generated resources and no-Gradle projects must retain usable last-valid inputs and show actionable status. | +| Generic editor integration | Separate sections 1, 16, 17 and VS Code V-cases | File association, TextMate fallback, comment/uncomment, templates/snippets, keybindings, plugin reload and Community-only startup. Compiler shared scenarios do not replace these checks or an explicit Tree-sitter smoke run. | +| Reliability and configuration | X29/X30/X51/X57 plus process/retention suites | Cross-platform/remote filesystems, large-workspace latency/heap bounds, prolonged run/stop/crash cycles, cancellation under queue pressure, and UI1–UI7 settings acceptance remain explicit L82 gates. | + +A catalog entry's `coverage: full` means the driver implements that **case's stated assertions**; +it does not mean the entire feature family is exhaustive. Manual checks should record pass/fail/ +unavailable/not-run and a reason. Promote repeatable missing variants into shared data and both +drivers as they are implemented; keep genuine visual/OS/runtime checks explicitly separate. + +### Semantic Tokens: Current Scope and Follow-ups + +Tree-sitter supplies syntax-based tokens when explicitly selected; broader heuristic usage-site classification +remains a possible enhancement. The opt-in compiler adapter now supplies resolved-name tokens and +modifiers independently, with no Tree-sitter fallback or combined adapter. Broader syntax coverage +must preserve the distinction between compiler facts and lexical coloring. + +### Cross-File References (partly done) + +Tree-sitter supplies cross-file go-to-definition, workspace symbols and import links through its +workspace index. The compiler supplies definition and references across the current module by +resolved identity, including closed member files; workspace symbols compile unopened graph members on demand. +Explicit host-indexed dependency sources also supply definition/type-definition and inherited +method-body targets. Reference queries now compile the complete configured source graph; they +include unopened consumers and source uses of binary XDK members without inventing source targets. +Still remaining: + +- Persistent reference indexing and consumer discovery outside workspace roots +- Broader member rename and external-consumer closure + +### Full Compiler Integration (partly done) + +Done - see §6, §7, §7a and [module sessions and hierarchy](#compiler-module-sessions-and-hierarchy): +- Semantic error detection, with the compiler's own codes and spans +- Document outline, from the parsed AST +- Typed hover and identity-based definition/references across a module +- Unsaved member overlays, sibling invalidation and diagnostics at each file's URI/version +- Direct extends/implements hierarchy between source types, including generic parents +- Bounded scope/member completion, generic call-site signatures and incomplete-call candidates +- Type-definition and nominal type/method implementation lookup +- Static call hierarchy, resolved-name tokens, read/write highlights and bounded inlay hints +- Explicit dependency artifacts/source indices, consumer invalidation and server diagnostic refresh +- Workspace discovery and automatic dependency recompilation for discovered/configured roots/edges, including unsaved overlays +- Cross-module implementations and type/call hierarchy over the complete source graph +- Proven inline-type/static-member rename and ordinary-import cleanup +- Java-lexer formatting, URL links and lexical tokens, module run lenses and local linked editing +- Initialization/live source-graph settings, exposed in VS Code as `xtc.compiler.sourceModules` + +Still to come: +- Broader Java parser recovery, incomplete-expression contexts and callable forms +- Persistent indexing and measured incremental work across larger module graphs +- Binary source attachment, conditional-mixin hierarchy and broader implementation targets +- Wider member/workspace rename: unsupported composition routes and automatic external-consumer + enumeration. X119–X121 add primary-constructor properties, lambda parameters and method-value + escapes. The L62 extension also covers public/explicit-constructor parameters, supported + composition families, qualified modules and implicit package directories. Both clients implement graph persistence through native Rename. Shared X109–X118 + pass in selected runs in both editors. VS Code edited-file moves with refactoring auto-save + disabled remain refused. Consumers outside the graph remain unknown +- Diagnostic-driven quick fixes and refactorings -> **Adapter support**: TreeSitter (same-file text matching). Cross-file linked editing requires compiler/SemanticModel. +L61 declaration acceptance: X4 now also requests Go to Declaration for the shadowed local and +property and checks that their distinct declaration targets match definition. VS Code uses its +declaration provider; IntelliJ checks the request through the installed client transport beside +the existing native navigation actions. Backend/protocol cases cover multiple inherited contracts, +property overrides, import aliases, closed consumers, indexed libraries and stale source. +Combined backend/protocol validation and selected X4/X102/X103/X104 runs pass in both clients. +See the [acceptance reports](#acceptance-reports). + +### Rename extension batch acceptance + +X119–X121 are shared by both drivers and add primary-header properties, lambda captures and +escaped method values. At that checkpoint the catalog contained 126 cases; X119–X121 pass in VS Code +`run-c7xgn7` and IntelliJ `run-14634841832018763989`. The latter also passes X57, X118 and +CFG1–CFG3 plus START with zero IDE errors. VS Code's single-folder settings correction passes +X118 and CFG1–CFG3 in `run-Z5w8sz`. No full 126-case receipt is claimed. Source-graph scope receipts on +`xtc/rename` describe configured/discovered inputs, including explicitly registered external +roots. Unknown external consumers are not discovered by rename. Register them in sourceModules +before renaming; an unreadable registered source refuses the proposal rather than reducing scope. + +### Server log shortcut acceptance + +Press **Ctrl+Alt+X, then L** (macOS: **Control+Option+X, then L**) in either editor. +IntelliJ opens **Language Servers** with the Ecstasy **Log** tab selected; repeat the shortcut +to hide that tool window. VS Code selects **Ecstasy Language Server** in Output; repeat to hide +that output view. Remap the actions in native Keymap/Keyboard Shortcuts settings. Showing logs +does not start another server, clear history or change the logging level. + +- Open the log from a source editor and verify recent queue/job, compile and request timing lines. +- Toggle closed/open; manually close the panel and invoke the shortcut again. +- Select a different VS Code output channel, then invoke the shortcut: select Ecstasy output. +- Move/dock the view and verify the shortcut still opens/hides it. In IntelliJ, a visible + Language Servers window is hidden even if its selected tab is Settings or Trace. +- Stop/restart the server and verify retained logs remain accessible without a source edit. +- Verify the chord does not modify the source and a custom keymap binding works. + +Shared X135 passes in both editors for view open/hide and unchanged source. Physical +shortcut dispatch and log-tab selection remain explicit manual checks. + +Known native-host regression to watch: VS Code 1.139.1 can throw an Explorer `Data tree node not +found` while clearing Cut decorations after a successful batch Paste. If it occurs in X130, +inspect actual paths before doing anything else; never replay an already applied move. Verify +Explorer refresh and its Undo/Redo separately and retain the failed receipt. This is tracked in +[acceptance reports](#acceptance-reports). + +### Language-service settings acceptance (UI1–UI7) + +Selected X118/X132/X135–X139 pass in VS Code `run-1aSPTX` and IntelliJ +`run-3866544261762285778` (also START; zero IDE errors). The [acceptance reports](#acceptance-reports) +retains the earlier failures and fixes. These receipts cover seven selected scenarios, not all 144. + +IntelliJ Community: Settings → Languages & Frameworks → **Ecstasy Language Service Defaults** +(application) or **Ecstasy Language Service** (project). The project page inherits defaults until +unchecked. Use Apply, Reset, Cancel and re-open to verify persistence. Transport applies with a +restart; inlays and Code Style apply live. Server save formatting is disabled with an explanation; +use Settings → Tools → Actions on Save → Reformat code, then the native Save action. X139 deliberately +starts with `Int` at column 0 and must indent it four spaces when saved. The effective view is read-only and refreshed on +request. Compiler source/resource paths stay in **Ecstasy Compiler**, now with native path choosers. + +VS Code: run **Ecstasy: Open Language Service Settings**, or filter Settings by +`@ext:xtclang.xtc-language`. Connection options are user/workspace scoped, not folder scoped. +Use **Ecstasy: Show Effective Language Service Configuration** for configured values and origin, +running capabilities, queue and bundled read-only libraries. **Ecstasy: Show Effective Compiler +Paths** remains the detailed source/build-model view. Trace and logs use the existing controls. +Legacy `xtc.formatting.tabSize` is deprecated; use `editor.tabSize` for visual tab width. +`xtc.formatting.maxLineWidth` controls bounded compiler expression/list wrapping (default 120). + +Additional manual acceptance: change an application default while a project inherits it, then add +and remove a project override; reopen the IDE and verify persistence. Edit a compiler graph through +rename/Undo while the service page is open and ensure unrelated fields survive Apply. Verify user +versus workspace VS Code precedence, two workspace folders and folder-specific native save/inlay +preferences. Check invalid JSON retains a running service, native Reset/Cancel never saves a draft, +and restart never saves a dirty source. Remote filesystems, alternate keymaps and restricted +workspace build execution remain explicit boundaries, not assertions covered by these local runs. + +Protocol hardening acceptance (L80/L81): X136/X137/X140/X141 pass in VS Code `run-5eCFZV` and +IntelliJ `run-15914309414363009017` (also START, zero IDE errors). X140 checks a method-body reference +after an emoji; the subsequent L83 implementation is exercised separately by X142 below. +For a large configured project, request workspace diagnostics or references +and observe the work-done progress notification. Cancel the request: its progress must end, while +another reader and document analysis remain usable. Check no stuck progress after restart/close. +Unit tests control the queue and delayed creation acknowledgements deterministically; short editor +fixtures do not guarantee a visible progress popup. Bounded partial-result streaming is covered by X143 below. +Check dependency changes refresh supported inlays/lenses/folding without editing the consumer. +Legacy/minimal-client capability shapes and pre-initialize/shutdown errors are protocol tests, not +features exercised by changing the modern IDE's capability declaration. + +### Protocol and lifecycle coverage map + +This map covers the L80/L81 protocol batch and the accompanying state-ownership fixes. A shared +scenario is automated in both editors only where named below. Controlled backend tests exercise +interleavings that normal editor actions cannot reliably force. Manual rows remain acceptance +work; they are not passing automated receipts. The current catalog has 152 cases. The earlier +protocol checkpoint reran X136/X137/X140/X141 plus IntelliJ START; follow-up receipts are recorded below. + +| Change | Automated regression evidence | Editor/playbook coverage and limits | +|---|---|---| +| UTF-16 hover and rename ranges | `ClientPresentationTest`, `XdkPresentationTest` | X140 passes in both editors after an astral character; folded property initializers have the separate X142 regression. | +| Legacy roots, minimal-client hover/outlines/symbol kinds and optional diagnostic metadata | `ClientPresentationTest`; negotiated clients in `XdkLanguageServerTest`, `XdkModuleServerTest`, `XdkProjectServerTest` and `XdkFeatureResolveProtocolTest` | Modern editors cannot exercise every legacy capability shape. X140 and existing outline/diagnostic cases cover modern clients; protocol tests cover the reduced shapes. | +| Progress creation, token ownership, cancellation, late acknowledgements and one terminal event | `ConnectionProgressTest` controls replies and races completion/cancel/close | Manual P1/P2 below. Short compiler fixtures do not guarantee a visible progress popup. | +| Pending readers canceled independently of shared analysis; immediate retirement on close | `RequestOwnershipTest` uses an analysis future that can ignore cancellation | X137 covers connection restart with an unsaved buffer; P2 covers pending UI work. The selected editor case does not force the backend race. | +| Pre-initialize, duplicate initialize and shutdown request rules | `ProtocolLifecycleTest`, `XdkStdioTest`; `LspProcessLifecycleTest` covers child-process termination | X137 passes repeated transport restarts; invalid wire order is tested over packaged stdio, not sent through a conforming IDE. | +| Negotiated, coalesced refresh outside compiler locks | `ClientNotificationsTest`, `XdkPullDiagnosticsTest`, `XdkSemanticTokenProtocolTest` | Shared X146 passes dependency refresh and inferred hints in an untouched consumer in both editors. P3 retains broader per-provider visual acceptance. | +| Runtime `off` / `messages` / `verbose` trace without source payloads | `ClientNotificationsTest` | X141 passes messages/verbose in both editors; switching off and post-close behavior are controlled unit tests. | +| IntelliJ diagnostic-cache retirement after unlocked snapshot lookup | `DiagnosticResultMessagesTest` forces close/cancel during that lookup | Existing diagnostic/reopen scenarios cover normal editor flow; the precise race is unit-tested. | +| Tree-sitter scan starvation and native parser/disposal ownership | `WorkspaceIndexerTest` concurrent scans/parser requests run without skips | This is the shipping Tree-sitter adapter, not an XdkAdapter feature. Compiler playbook passes do not validate its UI behavior. | +| Retired VS Code connections and stale IntelliJ settings reports | X136/X137 pass in both editors; new X147 controls late report completion | X147 passes publication ownership after newer reports, settings changes, restart and native UI disposal in both editors, including a native restart between reply receipt and EDT publication. Broader multi-window interaction remains manual. | +| Negotiated configuration requests and post-close formatting replies | `ClientPresentationTest`, `EditorFormattingStateTest`, `FormattingConfigRoundTripTest` | X136 covers effective settings; earlier X138/X139 cover live formatting and save ownership. | +| Stalled watcher replies, late registration/removal and disconnect | `ResourceFileWatchersTest` controls acknowledgement deadlines and retry ownership | X124/X134 exercise ordinary external resource/source watching; they do not force client RPC stalls. | +| Compiler path drafts invalidated by settings/model/new-dialog changes | VS Code `compiler-paths.test.ts` | Actual settings writes and disposal are tested inside the extension host; no automated picker-click race is claimed. | +| Open buffers own index entries through scan/watch/close races | `WorkspaceIndexerTest` uses read barriers, including equivalent file URI spellings | Tree-sitter-specific; compiler playbook cases cannot establish this behavior. | +| Final provider inventory and optional link/signature/pull-diagnostic fields | `CapabilityContractTest` covers every adapter flag; `CapabilityNegotiationTest` tests absent/false/true fields; `DiagnosticPresentationTest` tests independent push/pull and related-report shapes; `XdkStdioTest` exercises rich/reduced sessions | X131 retains modern-client link/resolve coverage. Reduced initialization profiles are protocol tests, not a new UI case; no editor capability is changed midway through a session. | +| Action literals, preferred metadata, completion kinds and legacy command edits | `CapabilityNegotiationTest` | X131 covers negotiated modern-client actions. Legacy/minimal clients, stale handles and client refusal are protocol regressions. | +| Partial batches and actual initial scan progress | `PartialResultsTest`, `IndexingProgressTest`, `ConnectionProgressTest`, packaged `XdkStdioTest` | X143 compares every streamed workspace symbol with the ordinary response. Visible long-operation cancellation remains P1. | +| Detached constant-initializer facts | `SemanticModelTest`, `XdkInitializerTest` | X142 covers initializer hover/definition/references/rename/Undo in both drivers. | + +Pending manual acceptance in both editors: + +1. **P1 — Visible progress and cancel.** Open a large configured compiler project, enable verbose + LSP tracing and open the server log using Control+Option+X, then L on macOS (Ctrl+Alt+X, then L + elsewhere). Check that initialization advertises `window.workDoneProgress`. Request references + on a widely used source declaration. For a request lasting long enough, verify progress is + created only after initialization, begins after creation acknowledgement and ends on completion. + Check that its detail identifies the source/workspace, then updates with the active compiler + operation/source and queued count. Concurrent reference and workspace-check notifications are + separate requests and may show the same shared compiler work; they do not imply duplicate compiles. + Repeat and cancel through the editor's progress UI. A subsequent hover/reference request must + still succeed. If all requests finish before progress appears, record this as unexercised. +2. **P2 — Disconnect with pending work.** While P1 or a workspace diagnostic request is running, + restart the service using the same action as X137, then close/reopen the project. Unsaved text + must survive restart, old progress must disappear, requests must resume, and old server PIDs + must exit. Do not infer the race was exercised if the request finished before the action. +3. **P3 — Refresh without editing the consumer.** In the configured Library/Consumer graph, let + Library expose `static Int make() = 1;` and let a Consumer method use + `var value = lib.make();` with native inlay hints enabled. Change Library to + `static String make() = "one";`, leaving Consumer untouched. Its inferred-type hint and semantic + presentation must update. Inspect trace for refresh requests only for negotiated providers; + inlay/lens/folding/diagnostic/token refresh replies must not leave the compiler queue blocked. + Revert Library and verify the consumer returns to its original presentation. +4. **P4 — Late connection/settings callbacks.** Repeat X137's two transport changes while a + request is pending. Check the displayed adapter/PID belongs to the newest connection and there + is one live server afterward. In IntelliJ's Ecstasy Compiler settings, request two effective + path reports around a configuration change, then close/reopen the page. A late old report must + not overwrite the current report or a disposed page. Record actual overlap; a fast normal run + alone is not evidence of out-of-order completion handling. + +The disk-index/open-buffer, long-lived path-picker and stalled watcher-registration fixes and +their deterministic regressions are explicit in the [acceptance reports](#acceptance-reports). +Partial-result streaming and L83 initializer facts are implemented; their acceptance and remaining +limits are recorded below. Generic IntelliJ server-initiated text-edit checking is implemented; +X144 passes both editors. The catalog now contains 152 cases; the receipts below retain their original scope. + +### Constant-folded initializer acceptance (X142) + +Open `X142/FoldedInitializer.x` with its module configured in the compiler source graph. In +`Int copy = value`, hover `value`, go to its declaration, and find references: the declaration, +initializer and method-body reference must all appear. Rename to `number`; all three occurrences +must change and compilation must remain clean. Undo must restore the original source. Both editor +drivers pass these steps, including native IntelliJ rename/Undo. Backend tests +also cover expression types, semantic-token availability, nonconstant initializers and rejected probes. + +### Partial-result acceptance (X143) + +Both editor drivers create a configured module containing 130 classes, request its workspace +symbols normally and with a partial-result token, and compare every name in order. Progress batches +must contain at most 64 items and the final response must be empty. Unit tests additionally cover +cancellation between batches, stale publication, disconnect and backend failure. Acceptance results +pass in both editors. Initial Tree-sitter scanning now reports the actual scan lifetime; +its begin/report/end and cancellation ownership have controlled regressions. + +Follow-up receipt (2026-09-30): VS Code `run-4Vo2G3` passes X124/X131/X134/X142/X143. IntelliJ +`run-6087249141816329018` passes START and X124/X131/X134/X143; corrected X142 passes with START +in `run-1763480386953487411`, with zero IDE failures and Ultimate disabled. Both drivers use catalog +hash `cbb3c633a3006b394cfffd86d3ec790783124b61562f128a8b97ab072deb47a8` (148 cases). This is selected +acceptance, not a full-catalog rerun. The [acceptance reports](#acceptance-reports) +keeps initial failures, fixture/driver corrections and the unreproduced first native text mismatch. +Generic native applyEdit version checking is covered by X144 below. P1/P2 have X145's native +progress-model coverage; physical Cancel-button selection and the broader P3/P4 checks remain manual. + +X139 verifies closed-tab formatting; cleanup discards dirty fixtures only after their scenario +finishes. X143 uses a scoped wire listener so console rollover cannot hide partial-result batches. +X145 distinguishes cancellation through the workbench token from the optional visible-button check. + +VS Code X130 host limitation: 1.140.0 can complete a native batch Paste and then throw +`Data tree node not found` from Explorer's `itemsCopied` while clearing the old Cut highlighting. +The driver verifies Move, Undo, Redo and resource contents without replaying Paste, then reports the +original host error as a failure. A focused pass does not close this intermittent defect; the full +`run-06Z6tq` reproduces it after successful semantic/resource assertions. Track separately from +compiler move correctness and recheck with a future VS Code fix. + +Corrected full VS Code `run-06Z6tq` passes **149/150** with only the X130 host repaint failure +above. All formerly failing completion/signature cases pass. X144/X145 pass with current workload; +X145 takes 16.0 seconds. This is explicitly not a clean full-catalog receipt. + +Final IntelliJ receipt: `run-1843149430446112481` passes **all 150 scenarios plus START** in one +uninterrupted run, with zero IDE errors and successful shutdown (IDE 2026.2.3, Ultimate disabled, +LSP4IJ 0.21.0). It uses the same catalog hash as the full VS Code run above. X139's open and +closed Save All checks pass, and completed-fixture cleanup no longer causes a shutdown conflict. +Earlier aborted runs remain recorded for diagnosis, rather than being relabelled as passes. + +The ordinary VS Code suite passes **23/23** in `extension-tests/run-Ts46BQ`, using a fresh profile +and workspace and restoring settings through the editor API. Its new shared status reporter shows +`Ecstasy tests: completed/total, remaining` at bottom left; the compiler runner uses +`Ecstasy playbook` with the same counter. X144/X145 pass in `run-aBMhCu` after sharing the reporter; +this selected run does not replace the full catalog's X130 failure. Hover the counter for the full +test title. Physical Cancel-button selection and the broader P3/P4 checks remain manual. + +### Selected reliability receipt (2026-10-01) + +The catalog has **152 cases**. VS Code `run-Ki54bo` passes X118/X129/X130/X136/X137/X139/X146/X147. +IntelliJ `run-13013331047107199721` passes all of those except X147, which exposes a stale report +after a service-only settings override. Corrected `run-5612866093768251189` passes X136/X137/X147 +plus startup, with zero IDE errors and successful shutdown. Thus both new scenarios pass both +editors, while the initial failure remains evidence of the bug, not a clean full-catalog run. +X147 also completes an old reply just before restarting on the same EDT turn, so publication must +reject the retired connection even though the settings are unchanged. + +P3's dependency/inlay refresh and P4's controlled stale-report checks now have automation. Broader +provider visual checks, physical Cancel-button selection and multiple-window interactions remain +manual. X130's selected pass and extension-free probe's `not-reproduced` result do not close its +earlier intermittent full-run Explorer failure. + +For a realistic compiler workload, use `lang/scripts/compiler-workload.py` with the recipe in the +[acceptance reports](#acceptance-reports). +It tests unsaved overlays, cancellation, outlines, hover, diagnostics and repeated server lifetimes +against `../platform`, preserving the checkout and retaining queue/API/heap traces. Its 30-cycle +baseline is separate from editor interaction, prolonged soak and release performance targets. + +### X130 controlled Explorer-refresh reproduction (2026-10-01) + +The diagnostic probe now uses the shared X130 nested layout. Compile the extension tests, then run +from `lang/vscode-extension`: -### Semantic Tokens Phase 2+ (TODO) +```bash +node scripts/run-vscode-tests.cjs --explorer-move-probe --refresh-during-move +``` -Phase 1 (Tier 1+) is implemented and enabled by default. Future phases: -- **Tier 2**: Heuristic usage-site tokens (UpperCamelCase type detection, broader property/variable classification) -- **Tier 3** (compiler): Override tree-sitter tokens with compiler-resolved classifications +The development extension is empty: Ecstasy must be absent and no language server is launched. +A public `onWillRenameFiles` participant waits for Refresh Explorer and supplies no edit. Select +both source directories, Cut, select the destination, and Paste. This reproduces the stale Cut-node +repaint exception on VS Code 1.140.0. The script still checks one native Undo, one Redo and all file +contents before rethrowing; a nonzero exit records the bug, not a passing acceptance case. +Omit `--refresh-during-move` for the ordinary control. Each attempt uses a fresh profile/workspace. + +Normal X130 writes `X130-move-trace.json` with native command/window/file events and the real +compiler request/reply. The probe writes `move-trace.json` beside `results.json`. Event listeners +only observe; the explicitly requested probe participant is the sole forced interleaving. +Native test actions restore window focus through VS Code only when needed, without moving the +pointer. Already executed edits, Paste, Undo and Redo are never replayed. + +`run-czIsNj` and `run-GSxFkH` independently reproduce the host exception without Ecstasy. +`run-LjhgZP` passes X118–X129, then fails X130 on that exception after all history/content checks. +X130 alone passes in `run-QLBWYV`; it is not evidence of a host fix. The +[acceptance reports](#acceptance-reports) +records the failed focus attempt, correction, upstream source and remaining repair task. + +### Final L80 protocol checks (2026-10-01) + +The current response/provider capability audit is complete; see the +[acceptance reports](#acceptance-reports). +The shared catalog remains 152 cases. This server-only change is validated through focused backend +and packaged stdio tests; earlier native receipts are preserved, not claimed as fresh runs. + +For protocol acceptance, run `CapabilityContractTest`, `CapabilityNegotiationTest`, +`DiagnosticPresentationTest` and the packaged `XdkStdioTest` optional-signature/link cases. Clients +without tooltip support must receive the target without a tooltip, including after lazy resolve. +Clients without per-signature parameter support must still highlight the selected overload's +parameter through `SignatureHelp.activeParameter`. Pull clients must not inherit related-information +support from push diagnostics or from related-document support. Error text retains external source +identity when related information is suppressed. The tests include supported and reduced clients. + +For the next normal editor acceptance run, retain the existing signature-help cases, X123 diagnostics +and X131 resolve checks in both drivers. These exercise the installed clients' negotiated behavior; +they cannot substitute for the reduced-client protocol tests. L81 manual checks and the X130 host +failure remain open. Exact test receipts are in the +[acceptance reports](#acceptance-reports). + +### L81 detailed progress and upstream acceptance (2026-10-01) + +X145 now checks that reference progress names its source. The editor supplies the Ecstasy +service/source name once; the operation title is simply `Finding references`, without another +prefix. Its ordinary VS Code run exercises the real SDK cancellation callback with 5,000 methods +and leaves the Notifications panel closed; only the explicit visible-control run opens that panel. +To exercise the visible button separately, build the compiler extension, then run with Node available: -### Cross-File References (TODO) +```bash +./gradlew :lang:vscode-extension:assemble \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +cd lang/vscode-extension +node scripts/run-vscode-tests.cjs --playbook --cases=X145 --cancel-ui +``` -Cross-file go-to-definition and workspace symbols are implemented via the workspace index. -Import link navigation is now implemented (resolves import paths to file URIs via workspace index). -Still remaining: -- Cross-file find-references (workspace-wide name search) -- Cross-file rename refactoring +The `--cancel-ui` driver activates **Cancel on “Finding references”** through the isolated window's +Chromium renderer. It uses `playwright-core` against the existing Electron instance: no browser +download, desktop pointer movement, private cancellation fallback or arbitrary window selection. +The shared 20,000-method `uiMethods` workload must still be pending when the actual button is +clicked. A request completing before cancellation fails. Reports record `visible-control` separately +from `native-token` and retain a pre-click screenshot. The Gradle equivalent is +`-PcompilerPlaybookCases=X145 -PcompilerPlaybookCancelUi=true`. +IntelliJ activates its displayed control through its accessibility action, bound to the task and +project, with the normal 5,000-method workload. Both verify continued hover, progress removal, +unsaved restart and old-PID exit. +The IntelliJ popup keeps the IDE's standard width; there is no Ecstasy-specific size override. +The separate 20,000-method IntelliJ diagnostic can expose bulk-replacement UI stalls; it is +not part of the normal 5,000-method acceptance workload. + +X146 now observes all five refresh families in VS Code and all providers actually negotiated by +IntelliJ; dependency changes must update the untouched consumer in both directions. X259 additionally +checks rendered consumer inlays while toggling settings and restarting after a dependency edit. +The consumer must remain unedited, disabled hints must disappear, and the displayed type must +change Int → String → Int. X147 separately controls late report callbacks. Other per-provider visual +appearance remains manual; these cases do not claim exhaustive pixel-level presentation coverage. +The [acceptance reports](#acceptance-reports) +retains failed attempts separately from passing refresh/restart cases. IntelliJ +`run-6551466376631163236` passes visible Cancel and restart in 7.2 seconds with zero IDE errors; +VS Code `run-VeotM0` passes its automated visible-control check in 17.6 seconds, plus X146/X147/X259. + +All upstream compatibility issues are collected in [client compatibility limits](#client-compatibility-limits). +Source `TODO LSP4IJ:` / `TODO VSCODE:` markers carry matching UP IDs and removal conditions. +UP15's malformed-parameter classification and UP16's X130 host repaint remain defects; a passing +reader-recovery or Move/Undo/Redo check is not evidence that those defects are repaired. + +### Bounded L82 regression and memory checkpoint + +Run the combined compiler/LSP/packaged-server/plugin checks before the full editor catalogs. +Confirm XML counts and record existing disabled/opt-in cases separately; a cached task or missing +native-library skip is not execution evidence. Full editor runs omit the scenario-selection +property. Keep known upstream native failures and explicit partial cases visible in their reports. + +For a repeatable real-project memory run, with the compiler server already built: -### Full Compiler Integration (TODO) +```bash +python3 lang/scripts/compiler-workload.py \ + --workspace ../platform \ + --jar lang/lsp-server/build/libs/lsp-server-0.4.4-SNAPSHOT-all.jar \ + --output lang/lsp-server/build/reports/platform-workload/l82-checkpoint \ + --cycles 400 --restarts 2 --sample-rss --gc-every 100 --heap-histograms +``` -Planned features once compiler adapter is implemented: -- Semantic error detection -- Type inference in hover and inlay hints -- Accurate completion filtering (type-aware) -- Cross-file rename refactoring -- Diagnostic-driven quick fixes and refactorings +Choose a fresh output directory; the script refuses to overwrite an earlier receipt. It changes +only LSP overlays and verifies disk-source hashes afterward. The three bounded sessions alternate +normal shutdown and unexpected transport EOF, and require actual process exit. Each live-object +histogram is captured between edit cycles through the owned server's `jcmd GC.class_histogram`; +its diagnostic full-GC pause is excluded from request latency. Compare `heap-100.txt` through +`heap-400.txt` with the post-GC checkpoints and sampled RSS. Class counts locate retained growth; +they do not identify reference owners or replace a heap-dominator investigation when growth remains. +This is a bounded local regression workload, not multi-hour or cross-platform acceptance. + +For the longer local check, use `--cycles 1200` with another fresh output directory and compare +the same classes through `heap-1200.txt`. The October 6 run passes all 3,600 cancellations and +normal/EOF/normal process exits, with stable live thread-local counts. Correctness passing does +not imply timing targets passed: that run misses one 500 ms edit-to-symbol p95 target and one +10-second cold-graph target. Record timing comparisons separately from the script's correctness +status, and preserve misses. See the [acceptance reports](#acceptance-reports). + +For configuration responsiveness with many retained editor buffers, run this separately from other +builds and compiler workloads: + +```bash +python3 lang/scripts/compiler-workload.py \ + --jar lang/lsp-server/build/libs/lsp-server-0.4.4-SNAPSHOT-all.jar \ + --output lang/lsp-server/build/reports/configuration-workload/generated \ + --configuration-documents 56 --cycles 3 +python3 lang/scripts/compiler-workload.py \ + --workspace ../platform \ + --jar lang/lsp-server/build/libs/lsp-server-0.4.4-SNAPSHOT-all.jar \ + --output lang/lsp-server/build/reports/configuration-workload/platform \ + --configuration-documents 56 --cycles 3 +``` + +The generated case changes one module's resource configuration, repeats it, removes the graph and +restores it while all 56 buffers remain open. A single-module change should submit one compilation; +the identical update should submit none. Full removal/restoration may recompile all modules. +The real-project case opens up to 56 source documents and adds/removes an unrelated temporary module +outside the source checkout. Existing successful modules should retain their analyses. Every phase +checks unchanged symbols and diagnostics, records notification-to-status and settled latency, and +requires server exit. Source hashes must remain unchanged. Timing data and compile counts live in +`results.json`; a correctness pass alone is not a response-time guarantee. + +Use X147/X148/X259 for the subsequent selected native check: replace/reset compiler settings, +restart while a report is pending, extract with Undo/Redo, and refresh an untouched consumer after +dependency edits. Keep the usual deadlines. The generated workload supplies the many-buffer scale +evidence; a three-case native selection does not reproduce the full catalog's retained workload. + +### Large-file IntelliJ diagnostic (L82 / UP17) + +The optional `CompilerPlaybookTest.largeFileEditing` probe reproduces the recorded UI freeze. +It is disabled by default, including in ordinary full playbook runs. The +[acceptance reports](#acceptance-reports) +contains its exact Gradle command, failed receipts and plain-document control measurements. +It can bring the disposable IDE forward without moving the pointer to install highlighting. +Do not edit that fixture during the measurement. + +For a manual check, open its generated `parity/LARGE_FILE/LargeFile.x` and replace the original +20,000-method source with the first 5,000 methods, retaining the module's closing brace. Wait for +semantic colors before replacing: the compiler replying does not establish that the IDE applied +its highlights. Check typing, repaint and navigation responsiveness, then Undo and verify the +source and highlighting recover. Record UI and server times separately. A freeze or missing +decoration fails this acceptance; a passing bounded X145 does not override it. UP17 remains open. + +Startup notification presentation: the bold service title appears once. Beneath it, Version, +Adapter and PID share one compact line with dimmed, theme-aware labels and normal value text. +Verify all three values are readable without truncation or an expansion arrow, then leave the +balloon untouched and check that it fades after eight seconds. The values remain available in +Notifications and the server log. This is presentation acceptance, not a compiler capability. + +Large-file semantic response checks can run independently of native decoration: use the +`compiler-workload.py --semantic-methods 5000 20000` command in the +[acceptance reports](#acceptance-reports). +It checks exact reference/hint counts and server process exit after compilation, and records +one-line versus whole-file hints, tokens, hover, queue/API phases and transport timings. Keep cold +project-query compilation distinct from warmed responses. This does not replace the decorated +IntelliJ probe, native tooltip/theme checks or a prolonged editing/retention workload. + +### Native project lifetime checks (L81) + +These optional checks open disposable native windows and exercise real close/reopen behavior: + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook --tests '*CompilerPlaybookTest.projectLifecycle' \ + -Plsp.adapter=compiler -PincludeBuildLang=true -PincludeBuildAttachLang=true --no-build-cache +``` + +After building the VS Code extension, run from `lang/vscode-extension`: + +```bash +node scripts/run-vscode-tests.cjs --project-lifecycle +node scripts/run-vscode-tests.cjs --project-lifecycle --shared-process +``` + +Both reuse X145's shared workload with different types in the two projects. Watch one close while +references are pending, then reopen with a new compiler PID. The other must keep its unsaved text, +original PID, correct hover and reference count. IntelliJ uses two frames in one IDE and saves the +closing project. VS Code's default mode uses two separate normal application instances; the +`--shared-process` mode opens two windows in one Electron process and proves their common parent PID. +Both restore the closing window's dirty buffer through hot exit. The isolated profile configures +`files.hotExit` at application scope, where VS Code reads it; workspace settings cannot set it. +The still-open window retains its original compiler PID and unsaved source throughout. Its disposable +extensions directory contains the local extension and a test controller because development windows +intentionally have no persistent backup path. Neither driver kills a compiler to satisfy the checks. + +IntelliJ trusts only the generated fixture path inside its disposable IDE, avoiding a blocking trust +dialog without changing normal trust defaults. The commands do not move the pointer. Passing receipts +are IntelliJ `run-3852388644684425549` and VS Code `project-lifecycle/run-zI8fT9`; see the +[acceptance reports](#acceptance-reports). +VS Code shared-process lifecycle passes in `project-lifecycle/run-Sh7ro5`. The first attempt, +`run-NXMUZj`, failed hot-exit restoration because the harness placed an application setting in the +workspace. That failure is retained. The older Cancel attempt `run-6JOJx7` failed to select the right +window before work finished; `run-VeotM0` now passes using the isolated renderer. + +Keep longer workload sessions bounded and announce their expected duration. The latest platform run +completed two 1,200-cycle sessions with preserved source hashes and compiler retirement, then was +deliberately stopped before the third session ran. Post-GC heap rose about 15 MiB per completed session; +its ownership remains an investigation, not completed memory acceptance. See the +[acceptance reports](#acceptance-reports). +Combined suites and the full native catalogs are deferred to a separate checkpoint. +The final IntelliJ lifetime rerun `run-11163643850362008619` passes with zero IDE errors and checks +the reopened document before any edit is replayed; startup plus the lifecycle case take 38.3 seconds. + +X105 extension (selected acceptance passes in both editors): replace the fixture with +`module AutoImports { Document value; }`, place the caret after `Doc`, and accept Document. +The whole identifier must be replaced once and +`import xml.Document;` inserted atomically; Problems clears. Repeat with Widget from ImportLibrary. +Both drivers use the same completion variants and exact expected resulting documents; their +earlier X105 passes establish only quick fixes. + +L65 manual-module check (backend regression coverage passes; these manual actions remain available): in +`manualTests/src/main/x/jit/jit_tests/basic/mixinTests.x`, use Go to Implementation on +`t5.Root.self`; verify the written `Base.self` body and no generated redirect location. In +`delegationTests.x`, `ReportableAsString.showText` reaches `ReportableString.showText`; the +interface-valued delegate field does not invent another executable source body. + +L62 manual-module check (backend regression coverage passes): rename `t5.Root.self` to `copySelf` +in mixinTests; Root, Base, Mix and the test5 call change together, while t6 stays unchanged. Undo restores exact +text. In condMixinTests rename the first `MixS.size` to `width`; only its corresponding test1 call +changes, not the independent t2 composition. A rename colliding with another default method must +be refused. Go to Definition on Element in a conditional-incorporation clause reaches the +corresponding mixin formal. + +L64 closure additions (execution pending): X150 now includes a function argument lambda. Accept +`(arg1) -> TODO()`, verify that TODO() is selected, Tab reaches the end of the expression and Undo +restores the empty argument. This is a scaffold; replace TODO() before executing the program. +X151 adds unnamed val/var locals with string, integer and constructor initializers; accept the +syntax-derived name, verify the following source and Undo. X152 adds ordinary return expressions +with `thi` and `this.Ow`; verify the real enclosing instance, exact replacement and Undo. + +L65 closure additions (execution pending): + +| ID | Manual actions | Expected result | +| --- | --- | --- | +| X153 | Open each shared Dispatch.x variant. Use Go to Implementation on `box.value`, then `text.size()`, then the interface-valued delegate call. | The covariant property reaches its written getter; the conditional mixin reaches its written method. A runtime-only delegate has no guessed target. Both drivers check exact source positions; IntelliJ follows the native navigation action. | +| X154 | Open shared Access.x. Inspect `Mode`, `Quiet`, imported `List`, static `run`, and usages of destructured `left`/`right`, incremented `box.value` and `values[index]`. | Types are classified as enum, enumMember and interface; `run` is a function. Destructured variables and the incremented property are writes. Receiver/index expressions remain reads. Both drivers consume the same expectations; IntelliJ also checks native token consumption and highlights. | + +| Case | Manual action | Expected result | +| --- | --- | --- | +| X155 | Open shared Conditional.x. Rename the conditional Textual.size method to width, then Undo once. | The declaration and Box call change together. String.size and Box.value stay unchanged. The source remains free of diagnostics, and Undo restores the exact original source. Both drivers implement the case; the current batch has not run yet. | + +| X156 | Select the complete `input + step()` return expression in shared Extract.x and apply “Extract expression to local variable”. | An explicitly typed local is inserted immediately before the return. The selected expression appears once, the return reads the new local, and diagnostics remain clear. Undo/Redo/Undo restore the exact expected sources. Both drivers pass the case in the October 2 selected receipts at the top of this playbook. | + +| X157 | Place the cursor on `value` in the shared Extract.x local and apply “Inline returned local variable”. | The initializer replaces the sole adjacent return read, its declaration disappears, diagnostics stay clear and Undo/Redo/Undo restore exact source. Both drivers pass this case in the October 2 selected receipts. | + +| X158 | Open the shared ImportLinks.x with LinkLibrary.x. Inspect module/type import links and linked editing at `Crate`. | Link ranges name `LinkLibrary` and `Crate`, both target the actual library source, and the alias links only its three written occurrences. Both drivers resolve links, check ranges and open the target source. Both drivers pass in the October 2 selected receipts; modifier-click appearance remains a manual observation. | +| X159 | Open the scenario's Library.x, leaving Consumer.x closed. Rename the conditional `Textual.measure` method to `width`, inspect both files, then Undo once. | The mixin declaration and closed `Box` consumer call change together even though `Box` has no written method contract. `String.size` and `Box.value` stay unchanged. Undo restores both files exactly. Passes in both editors; see the current selected receipt above. | +| X160 | Open the scenario's Library.x, leaving Consumer.x closed. Rename `Api.value` to `content`, inspect the Engine/Forward/Outer consumer, then Undo once. | The interface property, concrete implementation, internal read and delegated read change together across two generic delegation layers. Method names and delegate receiver properties stay unchanged. Undo restores both files exactly. Passes in both editors; see the current selected receipt above. | +| X161 | Move `App/tools/Box.x` to `App/util/Box.x` using native Move in IntelliJ or a VS Code workspace file move. Keep Consumer.x closed. Inspect the import alias, constructor/static calls, old-package Helper references, companion Part.x and data.txt. Undo once, then Redo once. | One transaction rewrites qualified references, preserves Crate and all selected declarations/calls, and moves the companion tree. Undo restores all text and paths; Redo reapplies them. Passes in both editors; see the selected receipts above. | +| X162 | Configure App at `old/App.x` with resource roots `old/assets`, then `fallback`; keep Consumer closed and its resources disabled (`[]`). Move `old` into `target` through native Move. Inspect compiler settings, reopen both modules, Undo once and Redo once. Then add an unresolved name in App.x without saving and repair it. | Source and resource paths move together; resource precedence, dependency names and `[]` are retained. One Undo/Redo restores/reapplies files and settings, with clean diagnostics. Subsequent typing produces and clears diagnostics at the current path without duplicate document connections. | +| X163 | Configure App at `old/App.x` without resource overrides and keep Consumer closed. Move just `App.x` into `target`. Inspect its companion directory, `old/data.txt` and compiler settings, then Undo/Redo. Add and repair an unresolved name without saving. | The companion follows the source. Default resources left behind become an explicit `old` root; the resource bytes remain available. Undo restores the absent override; Redo restores the pinned root. Continued editing reaches the moved source and diagnostics clear after repair. | + +L67 index replacement check: with an unopened consumer, navigate a member from a host-indexed +binary. Replace its source index with another source location, remove the index while retaining +the binary, then configure editable source for that module. Navigation must follow the replacement, +be absent without an index, and prefer the configured source. Previously prepared hierarchy +handles must expire at each change. `XdkIndexLifecycleTest` now exercises this sequence through +one adapter (alongside graph removal); this is backend coverage, not a new editor receipt. + +X162/X163 use the shared fixture catalog in both drivers. IntelliJ additionally checks an unsaved +edit after Redo to catch a retired document connection; this remains a manual follow-up in VS Code. +VS Code exercises file-operation participation through a native workspace edit and uses the settings +editor as the Undo context. This does not establish Explorer Cut/Paste stability or imply that a +refused participation reply can veto a host move. + +| Case | Manual action | Expected result | +| --- | --- | --- | +| X164 | Open the shared First.x and leave Second.x/Consumer.x closed. Rename `First.Box.read` to `fetch`; inspect both declarations and the union receiver call, then Undo once. | Both otherwise independent written methods and the closed consumer change together. Diagnostics remain clear; Undo restores all three files exactly. | +| X165 | Open the shared Library.x with Api and leave Consumer.x closed. Rename `Api.read` to `fetch`; inspect First/Second's mutual delegation and the call, then Undo once. | The interface declaration and delegated call change together, with receiver properties unchanged and clear diagnostics. Undo restores both files. Go to Implementation must not invent a terminal body for the cycle. | + +| Case | Manual action | Expected result | +| --- | --- | --- | +| X166 | Open the shared Library.x, keep Consumer.x closed and rename `First.read` to `fetch`. Inspect both generic declarations and the `First \| Second` consumer, then Undo. | Both written methods and the call change; concrete type arguments remain unchanged, diagnostics clear and one Undo restores both files exactly. | +| X167 | Open shared Library.x and rename `First.read` to `fetch`, keeping the annotated Consumer.x closed. Inspect its `@lib.Mark("one")` and `@lib.Mark("two")` receiver types, then Undo. | Both methods and the union call change; annotation identities/arguments stay unchanged. Diagnostics clear and Undo restores both files. | +| X168 | Open shared Library.x and rename `Api.read` to `fetch`, keeping Consumer.x closed. Inspect First/Second and the call through Outer/Forward, then Undo. | The interface, both implementations and delegated call change together. Delegate receiver names/types remain unchanged, diagnostics clear and Undo restores both files. | + +X166–X168 have shared fixtures and implementations in both drivers. All three pass alongside +X164/X165 in the latest selected runs recorded at the top of this file. Exact receipts, the shared +catalog hash, proof boundaries and the failed/repaired IntelliJ UP20 evidence are in the +[acceptance reports](#acceptance-reports). +For manual focus coverage, switch away while Rename is opening, return to its dialog and submit +once; inspect the exact edits and Undo once. The native harness now observes focus while the +dialog is open without blocking on its modality or replaying the Rename action. + +| Case | Manual action | Expected result | +| --- | --- | --- | +| X169 | Move `App/tools/Box.x` into `App/util`, setting **New name** to `Parcel.x` in IntelliJ. In VS Code apply the equivalent file move. Keep Consumer closed; inspect all files, Undo once and Redo once. | Declaration, constructors and qualified uses become Parcel; the explicit Crate alias stays unchanged. Companion sources/resources follow the renamed type. Old-package Helper references retain their target. All changes undo/redo together; closed files have the expected contents on disk after each operation, and diagnostics remain clean. | +| X170 | Move `old/App.x` into `target` with **New name** `Renamed.x`. Keep Consumer closed, inspect compiler settings, then Undo/Redo. Add and repair an unsaved error in the moved module. | The module declaration/import and dependency names change together with source paths. Companion files move; default resources left under old stay available through an explicit resource root. Consumer resources remain disabled. Undo/Redo restores exact settings and paths; later diagnostics follow the current file. | +| X171 | Select First.x and Second.x from their separate packages and move both to `App/util` in one action. Keep Consumer closed, then Undo/Redo. | Mutual class references and the closed consumer use the final util namespace. One Undo/Redo restores/reapplies both moves and all text. | +| X172 | Request the shared two-file proposal, with one destination colliding with an inline Taken declaration. Inspect files and compiler settings afterward. | The installed compiler connection returns no proposal; neither file nor graph changes. This is a protocol refusal check in both drivers. It does not claim that VS Code can veto an arbitrary Explorer move. | + +X169–X172 pass in both drivers in the selected runs recorded at the top of this file. IntelliJ's native +Move dialog now permits a new basename for a single selected file. Batch moves retain each selected +basename; the compiler proposal API additionally supports individually renamed batch entries. + +A fresh reverse refactoring is distinct from Undo: a move bringing previously uncaptured files into +a pinned resource root is conservatively refused. Undo restores the stored, proven transaction and +its original resource configuration. Before applying refactorings, save any pre-existing unsaved +closed IntelliJ document; the host refuses buffers that were not available to the compiler proof. + +### L62 empty destinations and qualified-name trivia + +Shared X173–X176 pass in both drivers in the selected runs recorded at the top of this file. +The setup creates the listed empty directories explicitly, without a Marker.x source. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X173 | Create empty `App/util/nested`; move Box.x there as Parcel.x, keeping Consumer closed. Undo and Redo. | Aliases, all comments/whitespace, qualified calls, companions and resources survive; closed sources have exact expected contents. | +| X174 | Move Box.x from tools to the module companion root. Undo and Redo. | Removed prefixes leave both block and line comments, CRLF and Unicode intact; nested Part and constructors still resolve. | +| X175 | Request the shared batch into empty util with an inline Taken collision. | No proposal, edits, file changes or settings changes. This is a protocol refusal; it cannot veto arbitrary VS Code Explorer moves. | +| X176 | Move First.x and Second.x together into empty util. Undo and Redo. | Mutual references and the closed consumer update as one transaction; diagnostics remain clean. | + +### L63 local initializer and unused-local actions + +Shared X177–X180 pass in both drivers in the selected runs recorded at the top of this file. +Use the exact source in `lang/test-fixtures/compiler-playbook/scenarios.json` for each row. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X177 | Select the complete `input /* keep */ + step()` initializer and invoke **Extract expression to local variable**. Undo, Redo, Undo. | A preceding explicitly typed local holds the original expression and comment; the original initializer uses it. Text and diagnostics match the fixture at every step. | +| X178 | Invoke **Inline local variable into initializer** on `value`. Undo, Redo, Undo. | The lambda moves into the adjacent initializer with its original expected function type and capture. No duplicate evaluation. | +| X179 | Invoke **Remove unused local variable** on `unused`. Undo, Redo, Undo. | The constant initializer disappears, both surrounding comments remain, and exact original/changed text and clean diagnostics follow history. | +| X180 | Inspect code actions at `unused` where its initializer calls `step()`. | Removal is absent; the unused initializer may still have effects. The installed-connection refusal leaves source untouched. | + +Inferred locals, changed expected types, partial/deferred expressions, intervening statements, +Ref/Var annotations and runtime initializers remain refusal controls in backend tests. General +extract-method, missing-declaration generation and global safe delete remain separate work. + +### L63 private helper extraction + +X181–X184 pass in both drivers in the selected runs recorded at the top of this file. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X181 | Select the complete returned expression; apply **Extract expression to private method**, then Undo/Redo/Undo. | One private helper receives the stable input; comments and overload bindings survive, with exact edits and clean diagnostics. | +| X182 | Extract the complete typed initializer. | Explicit helper parameters receive the stable local and method parameter in the expected order; the result type stays `Int`. | +| X183 | Extract inside `Box`. | The helper stays inside that generic owner; its `Element` signature and implicit instance call still resolve. | +| X184 | Select the returned expression after `input++` and inspect actions. | Private-method extraction is absent. Query the whole selection, not a zero-length caret; source remains unchanged. | + +Backend controls additionally cover contextual `Int8`, short circuiting, static methods, fresh +names, CRLF/Unicode, reference-backed storage, method formals, lambdas, conditional returns and +broken neighbors. Swapped helper arguments and changed moved-call targets must fail proof even +when both programs compile. Multi-statement extraction and mutable/by-reference captures remain +outside this slice. + +### Missing-method quick fixes (L63, X185–X188) + +Use compiler mode. The call must initially report an error. Place the caret on `missing` and +open Quick Fix / intentions (Alt+Enter in IntelliJ; Quick Fix in VS Code). Generated methods are +private and contain `TODO()`; inspect and implement the body before running the program. + +| Case | Action | Required result | +| --- | --- | --- | +| X185 | Apply **Create private method 'missing'** on a zero-argument statement call. | A `private void missing()` stub is inserted in the same module; the diagnostic clears. | +| X186 | Apply the action on `return missing(value, label)` with declared `Int` and `String` parameters. | Generated signature is `private Int64 missing(Int64 arg1, String arg2)` (the resolved `Int` alias); existing source remains intact. | +| X187 | Apply the action inside a static method with string, character and explicitly typed numeric literals. | The helper is static with compiler-selected `String`, `Char` and `Int8` parameters and the declared return type. | +| X188 | Inspect actions on `missing(1)`. | No method creation action: an untyped numeric literal does not establish a runtime parameter type. Error/source stay unchanged. | + +For X185–X187, compare the exact shared expected source, check cleared diagnostics, then +Undo/Redo/Undo. Undo must restore both the source and the error; Redo must clear the error again. +Both drivers use the same fixtures. These are new scenarios, not evidence from X122 or extraction. +Cross-module/computed receivers, computed/named arguments, generic methods, conditional returns and +nested expression result contexts remain refusals. X189–X192 below add compiler-established local +arguments and explicitly typed initializer results; X193–X196 add proven same-owner instance receivers +and X197–X200 add named enclosing-class static calls and dispatch controls. X201–X204 add explicit +public repairs in other writable classes of the same module. +No diagnostic text or numeric error code is used to invent a signature. + +Run **X181 followed by X185** together when validating IntelliJ: an unchanged error after a +source-graph refresh must still offer a working quick fix. Standalone X185 passing did not catch +the UP07 stale-annotation issue. Final acceptance passes all six selected cases in both editors. + +### Local arguments and typed initializer quick fixes (L63, X189–X192) + +Use the exact fixtures in the shared catalog. Place a caret on `missing`, invoke Quick Fix +(IntelliJ Option/Alt+Enter; VS Code Quick Fix), and select **Create private method 'missing'**. + +| Case | Action | Required result | +| --- | --- | --- | +| X189 | Create a method called with the preceding `Int other` local. | `private Int64 missing(Int64 arg1)`; the argument continues to refer to that local. | +| X190 | Create a static method called with an inferred `var other` and a string, assigned to `String result`. | `private static String missing(Int64 arg1, String arg2)`; the result follows the initializer declaration, not a guessed type. | +| X191 | Create a method assigned to `List result`. | `private List missing(List arg1)` using resolved compiler spelling. | +| X192 | Inspect `var result = missing(value)`. | No creation action: there is no established result type. The source/error remain unchanged. | + +For X189–X191, compare the complete expected source, require diagnostics to clear, Undo to restore +the error/source, Redo to clear them and Undo once more. X192 checks refusal through the installed +connection in both editors. The selected continuation also runs X181 → X185, retaining the UP07 +quick-fix-refresh regression. These cases use the existing shared action drivers; they add no +editor-specific production bridge. + +### Qualified missing-method quick fixes (L63, X193–X196) + +Use the exact shared sources in compiler mode. Place the caret on the leaf `missing` token, then +invoke Quick Fix (IntelliJ Option/Alt+Enter; VS Code Quick Fix). + +| Case | Action | Required result | +| --- | --- | --- | +| X193 | Apply **Create private method 'missing'** on `this.missing(value)`. | `private Int64 missing(Int64 arg1)` is inserted in the enclosing module; the error clears and the receiver stays `this`. | +| X194 | Apply the action on `other.missing(value)`, where `var other = peer` in a static `Box` method. | An **instance** `private Int64 missing(Int64 arg1)` is inserted in `Box`; no `static` modifier and no changed receiver or result declaration. | +| X195 | Inspect a call on an `Other` receiver from `Box`. | No **private** creation action in the caller. X201 separately verifies the public destination action; inspecting actions leaves source/error unchanged. | +| X196 | Inspect `this:public.missing(value)`. | No private-method creation action; source and error remain unchanged. | + +For X193/X194, compare the complete expected shared text and cleared diagnostics, then Undo to +restore source/error, Redo to clear them and Undo again. Refusals use the installed connection +in both editors. The backend additionally rejects a repair that still compiles after changing +`peer` to `this`, and verifies that selecting only the receiver does not offer member creation. + +### Class-qualified missing-method quick fixes (L63, X197–X200) + +Use the exact shared sources in compiler mode, with the caret on the leaf `missing` token. +Open Quick Fix / intentions and select **Create private method 'missing'** where offered. + +| Case | Action | Required result | +| --- | --- | --- | +| X197 | Repair `Box.missing(value)` from an instance method of `Box`. | Insert `private static Int64 missing(Int64 arg1)` in `Box`; the original qualifier stays unchanged. | +| X198 | Repair `Extract.Box.missing(other)` in a `List` initializer, with a validated `val other`. | Insert `private static List missing(List arg1)` in `Box`; the qualifier, local and declared result context are preserved. | +| X199 | Inspect `type.missing(value)` on a runtime `Type` parameter. | No method-creation action; source and error stay unchanged. | +| X200 | Repair `Box.missing(value)` with a parameter declared as `Extract.Box Box`. | Insert an **instance** `private Int64 missing(Int64 arg1)` even though the caller is static: the receiver resolves to a parameter, not a class. | + +For X197/X198/X200, compare the complete expected source, require diagnostics to clear, Undo to +restore source/error, Redo to clear them and Undo again. X199 checks refusal through the installed +connection in both editors. The backend also checks static callers, multiple compatible sites, +other class owners, existing static overloads, explicit generic/singleton qualifiers and runtime +`Class` values. An otherwise compiling repair redirected to another class must fail target proof. +The parameter type in X200 is qualified deliberately: `Box Box` shadows its own type annotation +and reports a separate dynamic-type diagnostic before the missing call can be analyzed. + +### Cross-owner missing-method quick fixes (L63, X201–X204) + +Use the exact shared sources in compiler mode. Open only the caller initially, put the caret on +`missing` and inspect Quick Fix / intentions. The public action explicitly names the destination; +its stub contains `TODO()` and is not an implemented method body. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X201 | Apply **Create public method 'missing' in 'Other'** on an instance parameter receiver. | Insert `public Int64 missing(Int64 arg1)` in the inline `Other` class; preserve the caller and clear its diagnostic. | +| X202 | Keep `Extract/Other.x` closed; repair `Other.missing(value)` from `Extract.x`. | Only the companion changes, adding `public static String missing(String arg1)`; the caller source stays unchanged and its diagnostic clears. Open the destination and Undo/Redo/Undo; verify exact text and caller diagnostics after each action. | +| X203 | Inspect the missing call returning `Object` when the destination declares its own nested `Object`. | No public creation action: the destination would change the requested type identity. Both files and the caller error remain unchanged. | +| X204 | Inspect a missing call on an interface receiver. | No public creation action; no interface mutation and no caller change. | + +For X201, also compare complete shared expected text and run Undo/Redo/Undo, checking restoration +and clearing of diagnostics. X195 remains a distinct private-action refusal; it does not assert +that the new explicitly public action is absent. Backend controls additionally cover read-only and +binary destinations, another configured module, generic/const classes, existing overloads, broken +neighbors, supported signature type shapes, and destination document versions (null when closed, +its own version when open). The next continuation adds bounded local arguments and typed initializers. + +### Cross-owner local types and initializer contexts (L63, X205–X208) + +Use the shared source for each case in compiler mode. The public action states both visibility +and destination; `TODO()` still needs an implementation. These cases reuse the same native +quick-fix and Undo/Redo steps as X201/X202. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X205 | Apply the public method fix to `peer.missing(local)` after `val local = value`. | The inline destination gains `public Int64 missing(Int64 arg1)`; the argument keeps its original local declaration binding. Inspect exact text and diagnostics across Undo/Redo/Undo. | +| X206 | Keep `Extract/Other.x` closed; repair the `Other.missing(local)` initializer of `List result`. | Only the companion gains `public static List missing(List arg1)`. The caller is unchanged. Open the companion and check exact text, caller diagnostics and Undo/Redo/Undo. | +| X207 | Inspect an explicitly typed `Object result` when the destination declares its own nested `Object`. | No public action; even an assignable narrower result may not change the requested type identity. Both sources remain unchanged. | +| X208 | Inspect `var result = Other.missing(value)`. | No public action: the missing call supplies no compiler-established result type. Source and diagnostic remain unchanged. | + +Backend controls additionally cover typed/var/val locals, separately assigned locals, instance and +class qualifiers, zero/mixed arguments, source and compound types, invalid/out-of-scope locals, +and compilable signature substitutions or argument rebinding that the proof must reject. + +### Cross-module missing-method destinations (L63, X209–X212) + +Use compiler mode and the complete shared fixture set for each case. Keep the destination closed +until applying the Quick Fix on `missing`. Successful actions must edit only the named destination; +the caller text stays unchanged. Inspect exact generated text and caller diagnostics through +Undo/Redo/Undo. These repairs use existing source ownership and imports; they never add graph edges. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X209 | Repair `peer.missing(value)` in `Extract.x`, with `Library.x` closed. | The dependency's `Other` gains `public Int64 missing(Int64 arg1)`. The caller diagnostic clears and returns on Undo. | +| X210 | Repair `lib.Other.missing(local)` in a typed `lib.Value` initializer; keep `Library/Other.x` closed. | Only the companion gains `public static Value missing(Value arg1)`. Types use the destination module's names. | +| X211 | Inspect a call passing and returning the caller module's `Value`. | No public-method action: it would require a reverse dependency. Both sources and the caller error stay unchanged. | +| X212 | Repair the local argument and initializer using caller alias `shared` and destination alias `types`. | The generated declaration uses `public types.Value missing(types.Value arg1)` in `Library.x`; no import, graph or caller edits. | + +Backend controls cover explicit dependency graphs, unrelated roots, binary artifacts with and without +source indexes, read-only destinations, unsupported owners, existing members, closed broken consumers, +missing destination imports, exact signature identity and destination-specific document versions. + +### Missing-method destination imports (L63, X213–X215) + +Load every shared fixture and use the explicit `sourceModules` graph recorded for each case. +The source import alone does not establish the destination's dependency permission. Keep the +destination closed until applying Quick Fix on `missing`. Compare complete generated text, +verify that the caller remains unchanged, then Undo/Redo/Undo and check caller diagnostics. +The import and method must behave as one atomic edit; compiler graph settings remain unchanged. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X213 | Repair the call using `shared.Value`; Library has a configured Types dependency but no import. | Library gains `package types import Types;` and a public `types.Value` method together. One Undo removes both. | +| X214 | Repair the static call with a local argument and typed initializer; destination is the closed `Library/Other.x`. | `Library.x` gains `package types2 import Types;`; the companion gains the public static method. Existing `types` and `types1` names, comments and caller text are preserved. Undo/Redo affects both additions together. | +| X215 | Inspect the same call with Types configured only as a caller dependency. | No public-method action. No file or graph setting changes; the diagnostic remains. | + +Backend controls include repeated/compound types, multiple imports with colliding module-name stems, +existing alias reuse, unused dependency exclusion, CRLF preservation, open/closed document versions, +and rejection of compilable signature substitutions after import insertions shift the method. + +### L62 ownership and relocation closure checks + +X216–X220 are implemented in both drivers; selected acceptance and the UP23 exception are below. +Use the shared fixtures for exact source, destination and graph settings. + +| Case | Manual action | Required result | +| --- | --- | --- | +| X216 | Move App/tools/Box.x into Other; Undo and Redo. | Imports, closed consumers, module dependencies and companion resources change atomically and return with history. | +| X217 | Request the cross-module proposal where Box still depends on App. | The new cycle refuses the proposal; sources and settings stay unchanged. This is a protocol refusal check, not an Explorer veto. | +| X218 | Select tools and tools/Box.x together; move to util; Undo and Redo. | The child and its companion leave first. Remaining parent contents relocate once. References and resources follow native history. | +| X219 | Move old into assets; Undo and Redo. | Previously uncaptured resources are retained, fallback contents stay identical and the root graph follows history. | +| X220 | Rename Library.First.read to fetch with Consumer closed, then Undo. | Both union branches and the consumer change together; typed numeric annotation arguments retain their values. | + +X218's VS Code transaction must submit the independently moved child before its parent. The +server accepts either request order, but the VS Code participant cannot reorder the host's own +file operations. Parent-first application can partially fail; child-first application succeeds but native Undo +also fails because VS Code retains the inverse move order. X218 remains a failing VS Code +acceptance case, while IntelliJ passes. See [client compatibility limits](#client-compatibility-limits). + +### L63 generic repairs and broader refactorings (X221–X242) + +These shared scenarios have passing selected-run receipts in both drivers. The +[acceptance reports](#acceptance-reports) +identifies each run and the corrected provider-cancellation/selected-tab harness failures; no +full-catalog rerun is claimed. Select an X number through the existing Gradle case properties. +Successful cases check exact edits, clear diagnostics and Undo/Redo/Undo; +missing declarations additionally restore their diagnostics on Undo. Refusal cases check that no +matching action exists and the source remains unchanged. + +| Case | Action | Required result | +| --- | --- | --- | +| X221 | Create a missing method using the exact generic owner formal. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X222 | Create a constrained generic missing method. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X223 | Create a conditional missing method preserving result arity. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X224 | Create a public method on a compiler validated call receiver. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X225 | Create named parameters from computed argument types. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X226 | Create a missing zero argument class. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X227 | Create a read only instance property with the required result type. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X228 | Refuse inventing an initializer for a missing static constant. | Refuse without editing; retain the original diagnostics. | +| X229 | Extract a nested expression using its compiler selected type. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X230 | Extract contiguous statements preserving order with native history. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X231 | Refuse statement extraction of mutable captures. | Refuse without editing; retain the original diagnostics. | +| X232 | Inline a constant local across an intervening statement. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X233 | Inline one private call while retaining the method declaration. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X234 | Inline a private constant read while retaining its declaration. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X235 | Refuse method inline when a caller parameter would capture a member. | Refuse without editing; retain the original diagnostics. | +| X236 | Safely delete an unused private method with parameter and call bindings. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X237 | Refuse deleting a public API with unknown external consumers. | Refuse without editing; retain the original diagnostics. | +| X238 | Safely delete an unused private compiler constant. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X239 | Extract an imported module type using its real source alias. | Apply the shared expected source; compile cleanly; Undo/Redo/Undo restores exact text. | +| X240 | Refuse inline requiring argument substitution. | Refuse without editing; retain the original diagnostics. | +| X241 | Refuse repeating a static runtime initializer during inline. | No action; no source or initialization changes. | +| X242 | Refuse deleting a static runtime initializer. | No action; no source or initialization changes. | + +X230 selects multiple lines; both drivers use actual document offsets. X239 selects the initializer +rather than its same-named parameter and imports a type from a configured Library source module. +The backend repeats that import/type-spelling check with a compiled binary dependency too. + +Boundaries: runtime Class/Type values without a concrete source owner, invented generic inverse +substitutions, static-property initializers, mutable capture/control-flow extraction, argument +substitution for method inline, instance-property getters and public API deletion are not inferred. +Private member inline keeps the declaration; safe delete is a separate action. UP23 remains the +recorded VS Code overlapping-resource Undo defect; this text-edit batch does not repair it. + +### L66 structural and editing continuation + +X243–X250 have passing selected receipts in both editors, together with X132/X138/X139/X158. +The [acceptance reports](#acceptance-reports) +retains the failed attempts and corrections, including UP24 (IntelliJ formatting Redo). The shared +formatting fixtures preserve their existing final newline; newline insertion remains an independent +editor setting. Four/eight spaces and a 120-column margin are the default settings. Change IntelliJ +Ecstasy Code Style or VS Code `xtc.formatting.*` to verify custom indentation and width; use +`editor.tabSize` for VS Code tab width. This is selected acceptance, not a new full-catalog run. + +| Case | Action | Required result | +| --- | --- | --- | +| X243 | Open the wildcard import link on `lib.tools`. | The resolved container opens in Library; no child or filesystem path is guessed. | +| X244 | Inspect the unsupported conditional import source. | Ordinary parser diagnostics are shown and no semantic import link is guessed. | +| X245 | Request linked ranges for each lambda parameter and its nested capture. | Only that lambda's declaration and uses are included; the sibling parameter stays separate. | +| X246 | Request linked editing for private/public method and primary constructor parameters. | No linked ranges: named callers and property contracts require graph Rename. | +| X247 | Request linked ranges for `import types.Box as Box` in each method. | Include the explicit alias and its uses, excluding the imported target and other lexical scopes. | +| X248 | Inspect outline, folds and selection in the shared damaged header/call/list/tuple variants, then repair. | Alias/current/later remain in the outline; method folds stay within source; selection strictly expands without duplicate spans; repair removes stale structure. | +| X249 | Format continuation expressions and standalone block comments, repeat, then Undo/Redo/Undo. | Shared indentation is exact and stable; literal contents and relative comment layout remain intact. | +| X250 | Format the shared long concatenation, repeat, then Undo/Redo/Undo. | Wrap at token boundaries at the configured 120-column margin; preserve every literal and native history. | + +### L67 graph-index acceptance (2026-10-05) + +| Case | Actions | Expected result | +| --- | --- | --- | +| X251 | Configure the shared 16 independent roots and Consumer; open Node0 and find references to Box while Consumer stays closed. Repeat workspace-symbol search; add the shared field without saving. Break and repair an independent root, then remove and restore it in the configured graph. | References include the closed consumer. The new field appears immediately. Broken neighbors suppress complete references, while healthy symbols remain available. Repair restores references; graph replacement removes/restores the root's symbols. | + +Both drivers implement X251 from the same catalog. IntelliJ assertions use its connected client for +references/workspace symbols and native documents/diagnostics; this does not assert References-panel +layout. Backend compile counters establish reuse, exact dependency-closure invalidation and resource +changes despite unchanged timestamps. Weak-reference checks establish that the retained navigation +index does not own compiler attempts, constant pools or ASTs, including during graph compilation. +Selected acceptance passes in VS Code `run-8dtGVL` and IntelliJ `run-3343499825664248739`: +X45/X59/X63/X143/X251, with zero scenario failures; IntelliJ also passes startup. The catalog now has +256 cases. This does not claim a full-catalog rerun. + +### Portable symbol identities — L74 + +| Case | Action | Expected result | +|------|--------|-----------------| +| X252 | Configure the shared Library/Consumer sources. Query monikers for Consumer's integer call and closed Library's matching declaration; compare the string overload. Open Library, change the integer body without saving, then restore it. | Export/import share scheme and identifier; overloads differ. Opening/recompiling unchanged source preserves identity. The edit changes both ends together; restoration recovers the original identifier. | +| X253 | Open the shared private-property example. Query its private declaration, bundled String and register local. Introduce the shared unresolved name, then repair it. | Private declaration is local; String is imported. Register locals and failed attempts supply no identity. Repair restores the same IDs. | + +These cases use `textDocument/moniker` through each installed language client. Inspect the protocol +trace for `scheme`, `identifier`, `unique` and `kind`; neither IDE currently provides a moniker panel. +X31 now requires the compiler moniker provider. Binary-only/source-indexed parity and checkout +relocation are backend tests, not claims about native editor widgets. + +### Read-only library documents — L75 + +| Case | Action | Expected result | +|------|--------|-----------------| +| X254 | Open the shared LibraryViews fixture and navigate to String and xml.Document. Open each matching source, compare its declaration moniker with the consumer, and request formatting. Attempt an edit in the VS Code virtual editor. | VS Code negotiates revision-owned virtual content and refuses edits; IntelliJ uses protected file views (UP25). Declaration/export and consumer/import share artifact identity. Formatting leaves the source unchanged. | + +Select **X31,X101,X158,X252,X253,X254** to cover existing bundled navigation, portable identities and the +new library-view path. X254 runs the same String/xml.Document source fixture in both editors. + +1. Open `X254/LibraryViews.x` and navigate from `String` and `xml.Document` to their definitions. +2. In VS Code, confirm the target uses `ecstasy-library:` and displays the matching declaration. + Typing must leave the document unchanged and clean. Formatting must return no edits. +3. In IntelliJ, confirm the matching source file opens read-only and formatting leaves it unchanged. + LSP4IJ 0.21.0 lacks virtual content/refresh (UP25); this explicitly tests the supported file fallback. +4. The driver requests `textDocument/moniker` at the library declaration. Its scheme/identifier must + match the consumer import; the declaration kind is `export`. Neither IDE has a moniker browser. +5. For supporting clients, `workspace/textDocumentContent` returns exact matching bundled text. + The protocol tests replace compiler inputs and verify refresh of fetched views, coalescing while + a reply is pending, rejection of unknown/other-connection URIs, and retirement on shutdown. + +URI revisions belong to the matching binary and source archive. Workspace edits never rewrite them; +opening a library view does not add it to the writable source graph. Externally supplied host source +indexes remain file views because they do not carry a verified source-text snapshot. This does not +claim complete semantic editing/navigation inside library bodies. + +### Compiler inline completion (L76) + +| Case | Action | Expected | +|---|---|---| +| X255 | Accept and undo a compiler-backed inline name. Use the shared `X255/InlineValues.x` source. | Native ghost text accepts answer; one Undo restores ans. | +| X256 | Dismiss ghost text and keep suggestions current while typing. Use the shared `X256/InlineValues.x` source. | Dismissal leaves source unchanged; further typing produces a current suggestion. | +| X257 | Respect ambiguity and the selected completion item. Use the shared `X257/InlineValues.x` source. | Automatic ambiguity yields nothing; explicit alternatives and selection constraints match the source. IntelliJ native trigger/selection limits are UP26. | +| X258 | Complete a type-compatible value in an unfinished call. Use the shared `X258/InlineValues.x` source. | Only the Int-compatible answer is suggested; missing delimiters remain untouched. | +| X259 | Keep RefreshConsumer open, including its bundled recursive json.Doc method signature. Disable inlay hints, change RefreshLibrary.make from Int to String, re-enable hints and restart. Restore the dependency while toggling hints again. | Visible consumer hints disappear when disabled, then show String and Int in order. Recursive signatures do not crash workspace analysis. The consumer text/version stays unchanged and the old server exits. X146 separately verifies all five refresh families; X147 covers late report publication. | + +Ghost suggestions are plain compiler names/values. No imports or placeholder bodies are inserted. +VS Code uses its Inline Suggest commands (Tab to accept, Escape to dismiss); IntelliJ uses +Insert Inline Completion and Escape. Explicit ambiguous suggestions are a VS Code native check; +LSP4IJ currently sends Automatic even for a direct invocation (UP26). + +### Compiler build import lifecycle (UI5–UI7) + +These cases share the real gated Gradle producer under `test-fixtures/compiler-playbook/compiler-import`. +Each uses an isolated project and the repository's Gradle wrapper. The fixture writes a model, signals +that publication has happened, then waits for an explicit release or native cancellation. It tests +client ownership rather than repeating the XTC Gradle plugin's model-export TestKit coverage. + +| ID | Action | Expected result | +| --- | --- | --- | +| X260 | Prepare generated inputs; while Gradle waits after writing its report, request Refresh again and click the visible progress Cancel button; retry Refresh successfully. | Only one task starts, pending/cancelled output never replaces accepted inputs, cancellation retires progress, and retry accepts the new resource roots. Both drivers activate the actual Cancel control without moving the desktop pointer. | +| X261 | Export a valid changed model, hold the task pending, then deliberately fail Gradle; retry with identical output and a successful exit. | Pending/failed output retains old paths and clean diagnostics through later configuration reads. The successful retry accepts the changed paths and produces the expected missing-resource diagnostic. | +| X262 | Let Gradle exit successfully with malformed output, then no output; repair with a valid report. | Both invalid imports retain accepted paths, show an actionable last outcome, and allow a successful retry. Source text remains unchanged. | + +IntelliJ displays the shipping Compiler settings component in a test-owned modeless dialog and +activates its real Refresh/Prepare buttons; native background progress supplies Cancel. VS Code +invokes the installed commands and clicks the renderer's notification Cancel button. No task, +progress token, producer exit or acceptance result is mocked. These cases do not cover closing a +project/folder during import, automatic Gradle sync, remote filesystems or arbitrary user builds. + +### Compiler workspace synchronization (X263–X265) + +| Case | Action | Expected result | +| --- | --- | --- | +| X263 | Import a composite root with an included build that itself includes a nested build. Hold the parent export pending, then release it. | No partial model is published. All three source owners and build roots appear together; processed-resource status is visible. | +| X264 | After initial import, run native Gradle synchronization in IntelliJ or a Gradle task in VS Code. Then replace the exported report, first with invalid JSON and then repaired inputs. | Automatic refresh changes diagnostics without resetting explicit settings. Invalid exports retain the accepted configuration; valid exports are observed without an explicit refresh command. | +| X265 | Start an import in a second IntelliJ project or VS Code workspace folder. Close that project/remove the folder while the producer is gated, then permit late completion. | The producer retires; removed inputs cannot return. The original project and compiler process remain usable. | + +The shared Gradle init script produces `.gradle/xtc/lsp-workspace.json` for the complete composite; +legacy single-build reports remain readable. Import does not parse Gradle script text or run the +Ecstasy compiler. Preparing resources remains a separate explicit action. Automatic export is +enabled by an existing imported report; VS Code additionally requires a trusted local workspace. +Tagged IDE exports are accepted only by the successful import that owns them; late daemon writes +after cancellation cannot replace the accepted model. Untagged external exports remain observable. +This guard is session-local. X265 uses a saved VS Code workspace so adding/removing a folder does +not replace the extension-test host; the launcher chooses this automatically when X265 is selected. + +### Ordered libraries and attached sources (X266–X268) + +Use **Settings → Ecstasy Compiler → Libraries and sources** in IntelliJ Community, or +**Ecstasy: Configure Libraries and Sources** in VS Code. VS Code also exposes +`xtc.compiler.libraries` in native Settings. Compiler settings are project/workspace-owned; +use absolute file URIs when multiple workspace folders are open. The bundled XDK remains +read-only and always available. `xtc.sourceRoots` is a separate indexing option, not a binary +source attachment. + +| Case | Actions | Expected result | +| --- | --- | --- | +| X266 | Add two library directories, reorder them, cancel a draft and reset/reopen settings, then apply. | Draft changes do not persist until Apply. The first binary for a module wins. The bundled XDK remains available. IntelliJ exercises the installed page controls; VS Code drives the native picker. | +| X267 | Add a binary and matching source directories for its exact module name. Navigate from a consumer, restart the server, navigate again, then remove and restore the binary. | Attached source opens read-only at the declaration; settings survive reconnect. Removing the binary produces diagnostics; restoration clears them. Sources do not become editable source modules. | +| X268 | Try duplicate and missing library paths after a valid import. Set resource roots in a deliberate order. | Invalid replacements preserve accepted compilation/navigation; path validation explains the refusal. Resource order survives settings application. | + +Null module paths inherit Gradle libraries; an empty list clears external binaries. Source roots +are searched in listed order using artifact source paths and declaration metadata. Available +method debug text must match; declarations without debug text are not a reproducibility check. +Use matching library sources. **Reload applied libraries** rereads the saved paths without restarting. Applying or reloading captures an immutable source snapshot; modifying an +attachment on disk does not edit a running compiler artifact. + +Both drivers share the Ecstasy fixture in the scenario catalog. `prepareLibraryPlaybook` builds +its binaries through the embedding API before either editor run. No binary fixture is checked in. +Selected acceptance (2026-10-06): VS Code `run-uZLiO0` passes X129/X254/X260/X263; +`run-Fpp8xO` passes X267; corrected `run-HTMdex` passes X266/X268. The initial runs exposed +URI hostname normalization and picker/URI-spelling assumptions, all fixed and retained as failed +receipts. IntelliJ `run-5914234873745700022` passes START/X129/X260/X263/X266/X267 and keeps +X254 partial for UP25; `run-5018889098298757849` passes START/X268 after creating its source-row +fixture explicitly. Both IntelliJ runs record zero IDE errors. The backend selection passes 33 +tests, IntelliJ units 114, and VS Code extension tests 41, without failures or skips. + +These cases verify settings reset/reopen and server restart, not reopening the IDE/project itself. +IntelliJ operates the installed settings components; VS Code X266 drives the visible picker. +Attachment setup and resource-order assertions use the installed page/settings APIs. Native file +chooser appearance, the resource-order modal's individual actions and broad remote/multi-root +acceptance remain manual checks; the new cases do not claim complete UI coverage. + +### Machine-local runtime and logs (X269–X272) + +In IntelliJ open **Settings → Ecstasy Server Runtime and Logs**. In VS Code open User Settings +and search `xtc.java.vmOptions` / `xtc.server.logs`. Values belong to this machine, not shared +project configuration. Defaults are 7 days, 10 MB per file, 50 MB archived per stream and five +stopped-server sessions. Running processes are protected; cleanup runs on the next server start. + +| Case | Actions | Expected result | +| --- | --- | --- | +| X269 | Open RuntimeSettings.x from the shared fixture. Save `-Xmx768M` (one array item in VS Code, one line in IntelliJ). Inspect effective status, restart explicitly, then try `-Xms2G` with `-Xmx1G`. In IntelliJ also cancel/reset a draft. | Saving keeps the PID; restart changes it and reports `-Xmx768M`. Invalid settings preserve the running server; fix the saved invalid VS Code setting before restarting. IntelliJ refuses Apply. Source text and diagnostics stay unchanged. | +| X270 | Save retention of 2 days, 1 MB/file, 3 MB archives/stream and two retired sessions. Restart; run Tools → Export Ecstasy Server Logs (IntelliJ) or Ecstasy: Export Server Logs (VS Code), choose a ZIP and inspect it. Try an archive cap below the per-file cap. | Status reports the applied policy and a new process directory. ZIP contains recent server logs and manifest.json with the matching PID/policy; exported tails are bounded and truncation is declared. Invalid retention is refused without losing the connection. Restore previous settings/restart afterward. | +| X271 | Stop the language server, then Export Server Logs. The ZIP identifies offline mode and contains only this project's recorded launcher/server session; restart restores diagnostics. | Shared X271 in both editors. | +| X272 | Save JVM option `-Xmx1K`, restart and observe the intentional JVM startup failure. Export logs while stopped; correct/reset the option and restart. Buffers and diagnostics recover. | Shared X272 in both editors; uses a disposable test profile. | + +### Color-value prototype (X273–X275) + +| ID | Action | Expected result | +|---|---|---| +| X273 | With the color prototype enabled, verify only two swatches in `ColorPrototype.x`; use native opacity/RGB picker controls on positional and reordered named constructors. Inspect dark/light/high-contrast schemes. | Source preserves labels, order and surrounding text; edited bytes compile and match the picker color. Both editor drivers use the shared source and negative controls. | +| X274 | Open and dismiss the native color picker without choosing a value, then change opacity once, Undo once and Redo once. | Dismissal preserves source; Undo and Redo restore the exact corresponding constructors and reported RGBA. | +| X275 | Change a channel from 255 to 256, then restore the source. | Compiler error removes all native color swatches; repair restores exactly the two known colors. Ordinary strings and dynamic expressions never gain swatches. | + +Automation checks installed settings/export operations and exact restart/status boundaries; +it supplies the save destination rather than driving the OS chooser. Manually verify chooser +Cancel and overwrite confirmation. Full editor exit/reopen is covered separately below; inspect +the visible settings layout during the native run. To exercise +retention manually, use an isolated `XTC_LSP_LOG_DIR` (VS Code/server) or `xtc.logs.directory` +server property: create several stopped-server sessions and start another server. Live sessions +and unrelated files must survive. Do not count a short playbook run as rollover/long-duration evidence. +See the [acceptance reports](#acceptance-reports). + +### Settings persistence and layout acceptance (UI1–UI7) + +IntelliJ’s **Settings → Ecstasy Compiler** page has **Source modules**, **Libraries and sources** +and **Build import** pages in a bordered vertical navigation list. Build actions are stacked +vertically at their normal width; expand **Effective paths and import details** for the full report. Confirm that switching pages +retains an unapplied draft and that Cancel leaves saved inputs unchanged. Gradle actions require +project trust and retain the accepted model if refused or cancelled. + +VS Code groups native settings into Compiler, Language Service, Formatting, and Runtime and Logs. +**Ecstasy: Show Language Service Status** distinguishes the saved origin, ignored folder/machine +scope overrides, running values, and changes awaiting restart. Invalid explicit Java homes fail +visibly; they do not silently select a different JDK. Untrusted and virtual workspaces are explicitly +unsupported. Remote extension hosts are not yet accepted by this local desktop test gate. + +These auxiliary tests use a disposable profile twice, fully exiting between write and read. They +verify persisted JVM/log limits, service preferences, source graph, and a new server PID. VS Code +also verifies workspace inlay settings override user defaults; IntelliJ exercises the native trust +refusal before any Gradle task starts. They supplement the X-case selectors rather than rename them. + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook --tests '*CompilerPlaybookTest.settingsPersistence' \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler --no-build-cache +# After building the VS Code extension, from lang/vscode-extension: +node scripts/run-vscode-tests.cjs --settings-persistence +``` + +Both full-exit persistence checks pass: IntelliJ `run-7326867442734256276` and VS Code +`extension-tests/run-fWbld3`. Tree-sitter also passes: IntelliJ `run-6749401871977790942` and +VS Code `extension-tests/run-LXUiIb`. Build with `-Plsp.adapter=treesitter` to repeat that settings-only +check, then restore `-Plsp.adapter=compiler` for compiler feature scenarios. Broader acceptance is +recorded separately in the UI completion receipt. + +X269/X270 intentionally submit invalid heap/log settings and verify that the running server is +retained. X272 intentionally starts a JVM that cannot initialize, verifies one actionable startup +error plus offline export, then restores valid settings and verifies recovery. These expected +notifications are checked and dismissed between cases; they are not successful-start evidence. + +Both harnesses stop the current IDE run on a timeout or failed workspace cleanup. Remaining cases +stay **not run**, rather than inheriting unfinished work and producing misleading results. VS Code's +status bar shows the current read-only wait and elapsed time against its deadline; the tooltip +contains the full condition. Commands that edit/rename/Undo remain single-shot. Expected refusal +tests must assert the refusal itself, not accept a timeout. Resume only remaining IDs in a fresh +test window after diagnosing a failure. + +### Compiler documentation action (X278) + +On the multiline generic `echo` header, select **Generate documentation comment**. The skeleton +must precede the complete header and contain the declared formal/value parameter names and return +entry. Existing documentation suppresses the action. Undo, Redo and Undo must restore the exact +shared source with clean diagnostics. Both editor drivers pass this case (October 7 selected acceptance). + +### Theme-based highlighting follow-up (X276/X277) + +| ID | Actions | Expected result | +|---|---|---| +| X276 | Open shared Highlighting.x with local, imported-aliased and qualified annotations, a shadowed Override class and bundled/local JsonObject types. Inspect semantic tokens and capture the startup theme. | Only annotation uses are decorators; only bundled ownership carries defaultLibrary. Theme colors remain in control. | +| X277 | Compare separate startup configurations with semantic highlighting off/on in three themes. Damage the string, interpolation and declaration, repairing each before the next edit. | Escapes, literals and keywords retain lexical detail. Errors retire stale tokens; repair restores exact classifications. The interpolation expression still receives compiler semantic highlighting. | +| X278 | Generate documentation on the multiline generic echo declaration. Undo, Redo and Undo. Invoke the action again after insertion. | Exact compiler parameter/return skeleton before the header; no duplicate comment; exact native history and clean diagnostics. | +| X279 | Open LensLibrary with LensConsumer closed. Click the two-reference lens and navigate to the consumer. Edit the consumer; disable and re-enable reference lenses. | Native reference navigation includes both files, overloads have independent counts, the changed count is three, and disabling reference counts retains Run. | + +The normal harness does not cycle the visible theme or toggle semantic highlighting. To compare +VS Code startup configurations after building the extension, run from `lang/vscode-extension`: + +```bash +npm run test:playbook -- --cases=X277 --theme="Default Dark Modern" +npm run test:playbook -- --cases=X277 --theme="Default Dark Modern" --lexical-baseline +``` + +Repeat with `Default Light Modern` and `Default High Contrast`. Compare `anchors` and `styles` in +the two runs' `X277-lexical-styles.json`; they must be equal, while `semanticHighlighting` must be +true/false respectively. The fixture also compares expression-bodied string/escape styling and +later keywords with control fragments, catching method-header grammar state leaking into bodies. +IntelliJ captures its current scheme, requires TextMate token types and checks installed lexical +attributes against controls and absence of semantic overlays on lexical fragments. Change its scheme manually for additional +visual comparisons; normal native runs leave it alone. + +All four focused cases pass in both editors; final X277 grammar/recovery checks and the six VS Code +startup comparisons are recorded in the [acceptance reports](#acceptance-reports). + +### Full catalog and UP12 follow-up (2026-10-07) + +VS Code's full attempt and continuations cover the same 284 cases: 281 pass, X130/UP16 and +X218/UP23 retain their host failures, and experimental X273 fails to display native color swatches. +Its first native color request receives `ContentModified`; a later explicit provider query returns +the expected colors, but the 30-second rendered check still fails. A fresh selected launch reproduces +that failure. X274/X275, theme-based highlighting X276/X277, documentation X278 and reference lenses +X279 pass in the continuation. Do not replace this evidence with a retry that edits the source or +toggles rendering merely to obtain a pass. The color prototype remains off by default. + +See the [acceptance reports](#acceptance-reports) +for every receipt, the X31/X127 harness corrections and the outstanding prototype refresh investigation. +This is combined VS Code coverage, not an uninterrupted green run. UP17's opt-in large-file test, +prolonged workloads and cross-platform/remote-host acceptance remain separate. diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/LanguageModelCli.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/LanguageModelCli.kt index fac57e55af1..dfdb5155cf3 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/LanguageModelCli.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/LanguageModelCli.kt @@ -304,8 +304,8 @@ private fun showHelp() { } /** - * Data-driven statistics model for the language. - * All statistics are computed once and stored in a structured format. + * Data-driven statistics model for the language. All statistics are computed once and stored in a + * structured format. */ data class ModelStatistics( val name: String, @@ -370,8 +370,8 @@ data class ModelStatistics( } /** - * Renders statistics in a beautiful box-drawing format. - * All widths are computed dynamically from the data. + * Renders statistics in a beautiful box-drawing format. All widths are computed dynamically from + * the data. */ object StatsRenderer { private const val BOX_WIDTH = 64 diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/XtcLanguage.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/XtcLanguage.kt index 7fdade10c52..12c906d6a38 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/XtcLanguage.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/XtcLanguage.kt @@ -181,8 +181,8 @@ import org.xtclang.tooling.model.language * - LSP semantic token mappings * - Eclipse syntax coloring * - * language() is the DSL entry point function (see LanguageModelBuilder above). - * Returns LanguageModel data class containing all parsed definitions. + * language() is the DSL entry point function (see LanguageModelBuilder above). Returns + * LanguageModel data class containing all parsed definitions. */ val xtcLanguage = language( @@ -200,13 +200,16 @@ val xtcLanguage = // block: ScopeBuilder.() -> Unit - Configuration lambda // // ScopeBuilder properties: - // textMate: String - TextMate scope name (https://macromates.com/manual/en/language_grammars) + // textMate: String - TextMate scope name + // (https://macromates.com/manual/en/language_grammars) // intellij: String - IntelliJ DefaultLanguageHighlighterColors key // eclipse: String - Eclipse IPreferenceStore color key - // semanticToken: String - LSP SemanticTokenTypes (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokenTypes) + // semanticToken: String - LSP SemanticTokenTypes + // (https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#semanticTokenTypes) // vim: String - Vim highlight group (see :help group-name) // emacs: String - Emacs font-lock face (see M-x describe-face) - // treeSitter: String - Tree-sitter capture name (see https://tree-sitter.github.io/tree-sitter/syntax-highlighting) + // treeSitter: String - Tree-sitter capture name (see + // https://tree-sitter.github.io/tree-sitter/syntax-highlighting) scope("keyword") { textMate = "keyword.control.xtc" @@ -641,7 +644,11 @@ val xtcLanguage = token("ANNOTATION", "@[a-zA-Z_][a-zA-Z0-9_]*", "storage.type.annotation.xtc") // Doc comments (/** ... */) - token("DOC_COMMENT", "/\\*\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", "comment.block.documentation.xtc") + token( + "DOC_COMMENT", + "/\\*\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", + "comment.block.documentation.xtc", + ) // Block comments (/* ... */) token("BLOCK_COMMENT", "/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/", "comment.block.xtc") @@ -711,7 +718,8 @@ val xtcLanguage = // RIGHT: a op b op c = a op (b op c) // NONE: Cannot chain // - // OperatorCategory enum: { ASSIGNMENT, ARITHMETIC, COMPARISON, LOGICAL, BITWISE, MEMBER_ACCESS, OTHER } + // OperatorCategory enum: { ASSIGNMENT, ARITHMETIC, COMPARISON, LOGICAL, BITWISE, + // MEMBER_ACCESS, OTHER } // Assignment operators (precedence 1, lowest - binds last) operator("=", 1, Associativity.RIGHT, OperatorCategory.ASSIGNMENT) @@ -1175,7 +1183,11 @@ val xtcLanguage = extends("Statement") child("condition", "Expression") child("message", "Expression", Cardinality.OPTIONAL) - property("kind", "String", default = "assert") // assert, assert:arg, assert:bounds, etc. + property( + "kind", + "String", + default = "assert", + ) // assert, assert:arg, assert:bounds, etc. } // From BreakStatement.java @@ -1687,20 +1699,52 @@ val xtcLanguage = token("LIT_UINTN", "[0-9][0-9_]*[uU][nN]", "constant.numeric.integer.uintn.xtc") // Decimal literal types (suffixed: 3.14d32, 2.718d64, etc.) - token("LIT_DEC32", "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]32", "constant.numeric.decimal.dec32.xtc") - token("LIT_DEC64", "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]64", "constant.numeric.decimal.dec64.xtc") - token("LIT_DEC128", "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]128", "constant.numeric.decimal.dec128.xtc") - token("LIT_DECN", "[0-9][0-9_]*\\.[0-9][0-9_]*[dD][nN]", "constant.numeric.decimal.decn.xtc") + token( + "LIT_DEC32", + "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]32", + "constant.numeric.decimal.dec32.xtc", + ) + token( + "LIT_DEC64", + "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]64", + "constant.numeric.decimal.dec64.xtc", + ) + token( + "LIT_DEC128", + "[0-9][0-9_]*\\.[0-9][0-9_]*[dD]128", + "constant.numeric.decimal.dec128.xtc", + ) + token( + "LIT_DECN", + "[0-9][0-9_]*\\.[0-9][0-9_]*[dD][nN]", + "constant.numeric.decimal.decn.xtc", + ) // Float literal types (suffixed: 3.14f32, 2.718f64, etc.) - token("LIT_FLOAT8E4", "[0-9][0-9_]*\\.?[0-9]*[fF]8[eE]4", "constant.numeric.float.float8e4.xtc") - token("LIT_FLOAT8E5", "[0-9][0-9_]*\\.?[0-9]*[fF]8[eE]5", "constant.numeric.float.float8e5.xtc") + token( + "LIT_FLOAT8E4", + "[0-9][0-9_]*\\.?[0-9]*[fF]8[eE]4", + "constant.numeric.float.float8e4.xtc", + ) + token( + "LIT_FLOAT8E5", + "[0-9][0-9_]*\\.?[0-9]*[fF]8[eE]5", + "constant.numeric.float.float8e5.xtc", + ) token("LIT_FLOAT16", "[0-9][0-9_]*\\.?[0-9]*[fF]16", "constant.numeric.float.float16.xtc") token("LIT_FLOAT32", "[0-9][0-9_]*\\.?[0-9]*[fF]32", "constant.numeric.float.float32.xtc") token("LIT_FLOAT64", "[0-9][0-9_]*\\.?[0-9]*[fF]64", "constant.numeric.float.float64.xtc") - token("LIT_FLOAT128", "[0-9][0-9_]*\\.?[0-9]*[fF]128", "constant.numeric.float.float128.xtc") + token( + "LIT_FLOAT128", + "[0-9][0-9_]*\\.?[0-9]*[fF]128", + "constant.numeric.float.float128.xtc", + ) token("LIT_FLOATN", "[0-9][0-9_]*\\.?[0-9]*[fF][nN]", "constant.numeric.float.floatn.xtc") - token("LIT_BFLOAT16", "[0-9][0-9_]*\\.?[0-9]*[bB][fF]16", "constant.numeric.float.bfloat16.xtc") + token( + "LIT_BFLOAT16", + "[0-9][0-9_]*\\.?[0-9]*[bB][fF]16", + "constant.numeric.float.bfloat16.xtc", + ) // Bit and Nibble literals token("LIT_BIT", "0|1", "constant.numeric.bit.xtc") diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/EmacsGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/EmacsGenerator.kt index b63adf5b143..5b7dc16b04e 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/EmacsGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/EmacsGenerator.kt @@ -25,14 +25,18 @@ class EmacsGenerator( ) { fun generate(): String = buildString { - appendLine(";;; xtc-mode.el --- Major mode for editing ${model.name} files -*- lexical-binding: t; -*-") + appendLine( + ";;; xtc-mode.el --- Major mode for editing ${model.name} files -*- lexical-binding: t; -*-", + ) appendLine() appendLine(";; Generated from Ecstasy language model") appendLine(";; Language: ${model.name}") appendLine(";; File extensions: ${model.fileExtensions.joinToString(", ") { ".$it" }}") appendLine() appendLine(";;; Commentary:") - appendLine(";; This major mode provides syntax highlighting for ${model.name} (XTC) source files.") + appendLine( + ";; This major mode provides syntax highlighting for ${model.name} (XTC) source files.", + ) appendLine(";; It is automatically generated from the Ecstasy language model.") appendLine() appendLine(";;; Code:") @@ -47,9 +51,7 @@ class EmacsGenerator( // Exception keywords from model val exceptionKeywords = - model - .keywordsByCategory(KeywordCategory.EXCEPTION) - .filter { !it.contains(":") } + model.keywordsByCategory(KeywordCategory.EXCEPTION).filter { !it.contains(":") } appendLine("(defconst xtc-exception-keywords") appendLine(" '(${exceptionKeywords.joinToString(" ") { "\"$it\"" }})") appendLine(" \"Exception handling keywords in ${model.name}.\")") @@ -119,7 +121,9 @@ class EmacsGenerator( // Declaration keywords if (declarationKeywords.isNotEmpty()) { appendLine(" ;; Declaration keywords") - appendLine(" `(,(regexp-opt xtc-declaration-keywords 'words) . font-lock-keyword-face)") + appendLine( + " `(,(regexp-opt xtc-declaration-keywords 'words) . font-lock-keyword-face)", + ) } // Modifier keywords @@ -131,7 +135,9 @@ class EmacsGenerator( // Type relation keywords if (typeRelationKeywords.isNotEmpty()) { appendLine(" ;; Type relation keywords") - appendLine(" `(,(regexp-opt xtc-type-relation-keywords 'words) . font-lock-keyword-face)") + appendLine( + " `(,(regexp-opt xtc-type-relation-keywords 'words) . font-lock-keyword-face)", + ) } // Built-in types @@ -150,7 +156,9 @@ class EmacsGenerator( // Function definitions appendLine(" ;; Function definitions") - appendLine(" '(\"\\\\b\\\\([a-z_][A-Za-z0-9_]*\\\\)\\\\s-*(\" 1 font-lock-function-name-face)") + appendLine( + " '(\"\\\\b\\\\([a-z_][A-Za-z0-9_]*\\\\)\\\\s-*(\" 1 font-lock-function-name-face)", + ) // Boolean constants if (booleans.isNotEmpty()) { diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/GeneratorUtils.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/GeneratorUtils.kt index d1b923df1cb..0ec190462dc 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/GeneratorUtils.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/GeneratorUtils.kt @@ -15,12 +15,17 @@ import kotlinx.serialization.json.buildJsonObject /** Characters that need escaping in standard regex patterns (TextMate, Sublime, etc.) */ private const val REGEX_SPECIAL = """.+*?|\\^$()[]{}""" -/** Characters that need escaping in Vim patterns (magic mode). - * Note: In Vim's magic mode, ? and + are LITERAL - escaping them creates quantifiers. */ +/** + * Characters that need escaping in Vim patterns (magic mode). Note: In Vim's magic mode, ? and + + * are LITERAL - escaping them creates quantifiers. + */ private const val VIM_SPECIAL = """\\/|^$.*[]()""" /** Escape special characters by prepending backslash */ -private fun String.escapeChars(special: String) = map { if (it in special) "\\$it" else "$it" }.joinToString("") +private fun String.escapeChars(special: String) = + map { + if (it in special) "\\$it" else "$it" + }.joinToString("") /** Escape for standard regex patterns (TextMate, Sublime, etc.) */ fun escapeRegex(s: String) = s.escapeChars(REGEX_SPECIAL) @@ -35,9 +40,7 @@ fun escapeJsString(s: String) = s.replace("\\", "\\\\").replace("'", "\\'") // JSON Builder Extensions // ============================================================================= -/** - * Add a JSON array with the given key using a builder block. - */ +/** Add a JSON array with the given key using a builder block. */ fun JsonObjectBuilder.putJsonArray( key: String, block: JsonArrayBuilder.() -> Unit, @@ -45,9 +48,7 @@ fun JsonObjectBuilder.putJsonArray( put(key, buildJsonArray(block)) } -/** - * Add a nested JSON object with the given key using a builder block. - */ +/** Add a nested JSON object with the given key using a builder block. */ fun JsonObjectBuilder.putJsonObject( key: String, block: JsonObjectBuilder.() -> Unit, @@ -55,37 +56,27 @@ fun JsonObjectBuilder.putJsonObject( put(key, buildJsonObject(block)) } -/** - * Add a JSON object to an array using a builder block. - */ +/** Add a JSON object to an array using a builder block. */ fun JsonArrayBuilder.addJsonObject(block: JsonObjectBuilder.() -> Unit) { add(buildJsonObject(block)) } -/** - * Add a nested JSON array to an array using a builder block. - */ +/** Add a nested JSON array to an array using a builder block. */ fun JsonArrayBuilder.addJsonArray(block: JsonArrayBuilder.() -> Unit) { add(buildJsonArray(block)) } -/** - * Add a string value to a JSON array (convenience wrapper for JsonPrimitive). - */ +/** Add a string value to a JSON array (convenience wrapper for JsonPrimitive). */ fun JsonArrayBuilder.addString(value: String) { add(JsonPrimitive(value)) } -/** - * Add an include reference to a TextMate patterns array. - */ +/** Add an include reference to a TextMate patterns array. */ fun JsonArrayBuilder.include(ref: String) { addJsonObject { put("include", JsonPrimitive("#$ref")) } } -/** - * Add multiple include references to a TextMate patterns array. - */ +/** Add multiple include references to a TextMate patterns array. */ fun JsonArrayBuilder.includeAll(vararg refs: String) { refs.forEach { include(it) } } diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/SublimeSyntaxGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/SublimeSyntaxGenerator.kt index b2aff33bc36..28fa8741c92 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/SublimeSyntaxGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/SublimeSyntaxGenerator.kt @@ -12,8 +12,8 @@ import org.xtclang.tooling.model.OperatorCategory * - bat (command-line cat clone with syntax highlighting) * - Any tool using the syntect library * - * This format is more powerful than TextMate grammars and supports - * context-based parsing with push/pop/set operations. + * This format is more powerful than TextMate grammars and supports context-based parsing with + * push/pop/set operations. */ class SublimeSyntaxGenerator( private val model: LanguageModel, @@ -251,7 +251,8 @@ class SublimeSyntaxGenerator( OperatorCategory.BITWISE to "keyword.operator.bitwise.xtc", OperatorCategory.ARITHMETIC to "keyword.operator.arithmetic.xtc", OperatorCategory.MEMBER_ACCESS to "keyword.operator.access.xtc", - OperatorCategory.OTHER to "keyword.operator.other.xtc", // includes range, elvis, etc. + OperatorCategory.OTHER to + "keyword.operator.other.xtc", // includes range, elvis, etc. ) for ((category, scope) in categoryToScope) { diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateBundleManifestGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateBundleManifestGenerator.kt index 9dd3332a4bf..e5868815bf9 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateBundleManifestGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateBundleManifestGenerator.kt @@ -30,7 +30,8 @@ class TextMateBundleManifestGenerator( fun generate(): String { // Derive language ID from scopeName (e.g., "source.xtc" -> "xtc") - // This ensures consistency: language ID, grammar filename, and scope all use the same identifier + // This ensures consistency: language ID, grammar filename, and scope all use the same + // identifier val languageId = model.scopeName.substringAfterLast(".") val config = diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateGenerator.kt index 477051edf2b..f6300585f69 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TextMateGenerator.kt @@ -215,7 +215,12 @@ class TextMateGenerator( if (assignmentOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.assignment.xtc") - put("match", assignmentOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + assignmentOps + .sortedByDescending { it.length } + .joinToString("|"), + ) } } // Comparison operators from model @@ -223,7 +228,12 @@ class TextMateGenerator( if (comparisonOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.comparison.xtc") - put("match", comparisonOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + comparisonOps + .sortedByDescending { it.length } + .joinToString("|"), + ) } } // Logical operators from model @@ -231,7 +241,10 @@ class TextMateGenerator( if (logicalOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.logical.xtc") - put("match", logicalOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + logicalOps.sortedByDescending { it.length }.joinToString("|"), + ) } } // Bitwise operators from model @@ -239,7 +252,10 @@ class TextMateGenerator( if (bitwiseOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.bitwise.xtc") - put("match", bitwiseOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + bitwiseOps.sortedByDescending { it.length }.joinToString("|"), + ) } } // Arithmetic operators from model @@ -247,7 +263,12 @@ class TextMateGenerator( if (arithmeticOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.arithmetic.xtc") - put("match", arithmeticOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + arithmeticOps + .sortedByDescending { it.length } + .joinToString("|"), + ) } } // Member access operators from model @@ -255,7 +276,12 @@ class TextMateGenerator( if (memberAccessOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.access.xtc") - put("match", memberAccessOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + memberAccessOps + .sortedByDescending { it.length } + .joinToString("|"), + ) } } // Other operators from model (range, etc.) @@ -263,7 +289,10 @@ class TextMateGenerator( if (otherOps.isNotEmpty()) { addJsonObject { put("name", "keyword.operator.other.xtc") - put("match", otherOps.sortedByDescending { it.length }.joinToString("|")) + put( + "match", + otherOps.sortedByDescending { it.length }.joinToString("|"), + ) } } } @@ -283,7 +312,10 @@ class TextMateGenerator( // Class declarations putJsonObject("class-declaration") { put("name", "meta.class.xtc") - put("begin", "\\b(class|interface|mixin|service|const|enum)\\s+([A-Z][a-zA-Z0-9_]*)") + put( + "begin", + "\\b(class|interface|mixin|service|const|enum)\\s+([A-Z][a-zA-Z0-9_]*)", + ) put("end", "(?=\\{|$)") putJsonObject("beginCaptures") { putJsonObject("1") { @@ -316,7 +348,10 @@ class TextMateGenerator( // Property declaration putJsonObject("property-declaration") { put("name", "meta.property.xtc") - put("match", "\\b([A-Z][a-zA-Z0-9_<>?,\\s]*)\\s+([a-z_][a-zA-Z0-9_]*)\\s*(?=[=;])") + put( + "match", + "\\b([A-Z][a-zA-Z0-9_<>?,\\s]*)\\s+([a-z_][a-zA-Z0-9_]*)\\s*(?=[=;])", + ) putJsonObject("captures") { putJsonObject("1") { put("name", "support.type.property.xtc") @@ -365,7 +400,9 @@ class TextMateGenerator( "([A-Z][a-zA-Z0-9_<>?,\\s]*)\\s+" + "([a-z_][a-zA-Z0-9_]*)\\s*(?=\\()", ) - put("end", "(?=\\{|;)") + // An expression body starts at =; leaving it in the header can strand + // a template closing quote and miscolor following declarations. + put("end", "(?=\\{|;|=)") putJsonObject("beginCaptures") { putJsonObject("1") { put("name", "storage.modifier.xtc") @@ -418,7 +455,8 @@ class TextMateGenerator( private fun controlFlowKeywords(): List { val control = model.keywordsByCategory(KeywordCategory.CONTROL) - val exception = model.keywordsByCategory(KeywordCategory.EXCEPTION).filter { !it.contains(":") } + val exception = + model.keywordsByCategory(KeywordCategory.EXCEPTION).filter { !it.contains(":") } return control + exception } diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TreeSitterGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TreeSitterGenerator.kt index 21aff06480c..7cdad4ef613 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TreeSitterGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/TreeSitterGenerator.kt @@ -44,8 +44,8 @@ class TreeSitterGenerator( /** * Generates the tree-sitter.json configuration file for ABI 15 support. * - * This config file is required by tree-sitter CLI 0.25+ for ABI version 15. - * It defines the grammar metadata including name, file types, and version. + * This config file is required by tree-sitter CLI 0.25+ for ABI version 15. It defines the + * grammar metadata including name, file types, and version. */ fun generateConfig(): String { val config = @@ -74,15 +74,10 @@ class TreeSitterGenerator( } private fun loadTemplate(name: String): String = - javaClass - .getResourceAsStream("/templates/$name") - ?.bufferedReader() - ?.readText() + javaClass.getResourceAsStream("/templates/$name")?.bufferedReader()?.readText() ?: error("Template not found: $name") - /** - * Generates the Tree-sitter grammar.js file - */ + /** Generates the Tree-sitter grammar.js file */ fun generateGrammar(): String { val template = loadTemplate("grammar.js.template") @@ -134,7 +129,8 @@ class TreeSitterGenerator( | // Dual visibility: public/private, protected/private, etc. | seq(choice($mods), '/', choice($mods)), |), - """.trimMargin().prependIndent(i2) + """.trimMargin() + .prependIndent(i2) } else { "${i2}visibility_modifier: $d => choice('public', 'private', 'protected')," } @@ -154,9 +150,7 @@ class TreeSitterGenerator( .replace("{{VISIBILITY_MODIFIER_RULE}}", visibilityRule) } - /** - * Generates the Tree-sitter highlights.scm query file - */ + /** Generates the Tree-sitter highlights.scm query file */ fun generateHighlights(): String { val template = loadTemplate("highlights.scm.template") @@ -172,9 +166,7 @@ class TreeSitterGenerator( } val exceptionKeywords = - model - .keywordsByCategory(KeywordCategory.EXCEPTION) - .filter { !it.contains(":") } + model.keywordsByCategory(KeywordCategory.EXCEPTION).filter { !it.contains(":") } val exceptionSection = if (exceptionKeywords.isNotEmpty()) { buildString { @@ -205,9 +197,11 @@ class TreeSitterGenerator( if (visibilityKeywords.isNotEmpty()) { appendLine("(visibility_modifier) @keyword.modifier") } - modifierKeywords.filter { it !in visibilityKeywords }.forEach { - appendLine("\"$it\" @keyword.modifier") - } + modifierKeywords + .filter { it !in visibilityKeywords } + .forEach { + appendLine("\"$it\" @keyword.modifier") + } } } else { "" @@ -276,14 +270,15 @@ class TreeSitterGenerator( } /** - * Generates binary expression rules from model operators, grouped by precedence. - * Operators at the same precedence level are combined into a choice(). + * Generates binary expression rules from model operators, grouped by precedence. Operators at + * the same precedence level are combined into a choice(). */ private fun generateBinaryExpressionRules(): String = buildString { val binaryOps = model.operators.filter { op -> - op.category !in listOf(OperatorCategory.ASSIGNMENT, OperatorCategory.MEMBER_ACCESS) && + op.category !in + listOf(OperatorCategory.ASSIGNMENT, OperatorCategory.MEMBER_ACCESS) && op.symbol !in listOf("!", "~", "++", "--") } @@ -307,7 +302,9 @@ class TreeSitterGenerator( } val comma = if (index < byPrecedence.size - 1) "," else "" - appendLine("$i3$precFn($precedence, seq($d._expression, $choiceExpr, $d._expression))$comma") + appendLine( + "$i3$precFn($precedence, seq($d._expression, $choiceExpr, $d._expression))$comma", + ) } }.trimEnd() diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/VimGenerator.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/VimGenerator.kt index 2afec07ecd9..2a3236672fb 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/VimGenerator.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/generators/VimGenerator.kt @@ -51,9 +51,9 @@ class VimGenerator( // Exception handling keywords from model val exceptionKeywords = - model - .keywordsByCategory(KeywordCategory.EXCEPTION) - .filter { !it.contains(":") } // Skip assert:* variants for keyword matching + model.keywordsByCategory(KeywordCategory.EXCEPTION).filter { + !it.contains(":") + } // Skip assert:* variants for keyword matching if (exceptionKeywords.isNotEmpty()) { appendLine("\" Keywords - Exception handling") appendLine("syn keyword xtcException ${exceptionKeywords.joinToString(" ")}") @@ -86,9 +86,9 @@ class VimGenerator( // Other keywords from model val otherKeywords = - model - .keywordsByCategory(KeywordCategory.OTHER) - .filter { !it.contains(":") } // Skip this:* variants + model.keywordsByCategory(KeywordCategory.OTHER).filter { + !it.contains(":") + } // Skip this:* variants if (otherKeywords.isNotEmpty()) { appendLine("\" Keywords - Other") appendLine("syn keyword ecstasyKeyword ${otherKeywords.joinToString(" ")}") @@ -129,7 +129,9 @@ class VimGenerator( // Strings appendLine("\" Strings") appendLine("syn region xtcString start='\"' skip='\\\\.' end='\"' contains=xtcEscape") - appendLine("syn region xtcTemplateString start='\\$\"' skip='\\\\.' end='\"' contains=xtcEscape,xtcTemplateExpr") + appendLine( + "syn region xtcTemplateString start='\\$\"' skip='\\\\.' end='\"' contains=xtcEscape,xtcTemplateExpr", + ) appendLine("syn match xtcCharacter \"'[^'\\\\]'\"") appendLine("syn match xtcCharacter \"'\\\\[nrtbf\\\\\\\"']'\"") appendLine("syn match xtcCharacter \"'\\\\u[0-9a-fA-F]\\{4}'\"") @@ -143,7 +145,9 @@ class VimGenerator( // Template expressions appendLine("\" Template expressions") - appendLine("syn region xtcTemplateExpr matchgroup=xtcTemplateBrace start='{' end='}' contained contains=TOP") + appendLine( + "syn region xtcTemplateExpr matchgroup=xtcTemplateBrace start='{' end='}' contained contains=TOP", + ) appendLine() // Comments @@ -175,7 +179,9 @@ class VimGenerator( // Function calls appendLine("\" Function calls") - appendLine("syn match xtcFunctionCall '\\<[a-z_][A-Za-z0-9_]*\\s*(' contains=xtcFunctionName") + appendLine( + "syn match xtcFunctionCall '\\<[a-z_][A-Za-z0-9_]*\\s*(' contains=xtcFunctionName", + ) appendLine("syn match xtcFunctionName '\\<[a-z_][A-Za-z0-9_]*' contained") appendLine() @@ -260,9 +266,7 @@ class VimGenerator( // Range operators from model val rangeOps = - model.operators - .filter { it.symbol.contains("..") } - .map { escapeVimPattern(it.symbol) } + model.operators.filter { it.symbol.contains("..") }.map { escapeVimPattern(it.symbol) } if (rangeOps.isNotEmpty()) { appendLine("syn match xtcOperator '${rangeOps.joinToString("\\|")}'") } diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/model/LanguageModel.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/model/LanguageModel.kt index 4481d011f45..300cfd4652e 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/model/LanguageModel.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/model/LanguageModel.kt @@ -32,9 +32,7 @@ import kotlinx.serialization.Serializable // ENUMS // ============================================================================= -/** - * Operator associativity - how operators of the same precedence bind. - */ +/** Operator associativity - how operators of the same precedence bind. */ enum class Associativity { /** Left-to-right: a op b op c = (a op b) op c */ LEFT, @@ -46,9 +44,7 @@ enum class Associativity { NONE, } -/** - * Operator category for semantic grouping and styling. - */ +/** Operator category for semantic grouping and styling. */ enum class OperatorCategory { /** Assignment operators: =, +=, -=, etc. */ ASSIGNMENT, @@ -72,9 +68,7 @@ enum class OperatorCategory { OTHER, } -/** - * Keyword category for semantic grouping and styling. - */ +/** Keyword category for semantic grouping and styling. */ enum class KeywordCategory { /** Control flow: if, else, for, while, switch, case, break, continue, return, etc. */ CONTROL, @@ -95,9 +89,7 @@ enum class KeywordCategory { OTHER, } -/** - * Cardinality for AST node children. - */ +/** Cardinality for AST node children. */ enum class Cardinality { /** Exactly one child, must be present */ REQUIRED, @@ -113,9 +105,7 @@ enum class Cardinality { // DATA CLASSES - The model types // ============================================================================= -/** - * Keyword definition with category for semantic grouping. - */ +/** Keyword definition with category for semantic grouping. */ @Serializable data class KeywordDefinition( val word: String, @@ -124,9 +114,7 @@ data class KeywordDefinition( val reserved: Boolean, ) -/** - * Complete language model containing all definitions. - */ +/** Complete language model containing all definitions. */ @Serializable data class LanguageModel( val name: String, @@ -145,67 +133,54 @@ data class LanguageModel( val visibilityKeywords: List, val concepts: List, ) { - /** - * Get all keywords (reserved + context-sensitive). - */ + /** Get all keywords (reserved + context-sensitive). */ val allKeywords: List get() = keywords + contextKeywords - /** - * Get keywords by category. - */ + /** Get keywords by category. */ fun keywordsByCategory(category: KeywordCategory): List = categorizedKeywords.filter { it.category == category }.map { it.word } - /** - * Get a concept by name. - */ + /** Get a concept by name. */ fun getConcept(name: String): ConceptDefinition? = concepts.find { it.name == name } - /** - * Get all concrete (non-abstract) concepts. - */ + /** Get all concrete (non-abstract) concepts. */ val concreteConcepts: List get() = concepts.filter { !it.isAbstract } - /** - * Get all abstract concepts. - */ + /** Get all abstract concepts. */ val abstractConcepts: List get() = concepts.filter { it.isAbstract } - /** - * Get operators sorted by precedence (lowest to highest). - */ + /** Get operators sorted by precedence (lowest to highest). */ val operatorsByPrecedence: List get() = operators.sortedBy { it.precedence } - /** - * Get constants by category (e.g., "boolean", "null"). - */ - fun constantsByCategory(category: String): List = constants.filter { it.category == category } + /** Get constants by category (e.g., "boolean", "null"). */ + fun constantsByCategory(category: String): List = + constants.filter { + it.category == category + } - /** - * Get boolean constant literals (e.g., ["True", "False"]). - */ + /** Get boolean constant literals (e.g., ["True", "False"]). */ val booleanLiterals: List get() = constantsByCategory("boolean").map { it.literal } - /** - * Get null constant literal (e.g., "Null"). - */ + /** Get null constant literal (e.g., "Null"). */ val nullLiteral: String? get() = constantsByCategory("null").firstOrNull()?.literal - /** - * Get bracket pairs from punctuation (for editor config). - */ + /** Get bracket pairs from punctuation (for editor config). */ val bracketPairs: List> get() = listOf( - punctuation.find { it.name == "L_PAREN" }?.symbol to punctuation.find { it.name == "R_PAREN" }?.symbol, - punctuation.find { it.name == "L_SQUARE" }?.symbol to punctuation.find { it.name == "R_SQUARE" }?.symbol, - punctuation.find { it.name == "L_CURLY" }?.symbol to punctuation.find { it.name == "R_CURLY" }?.symbol, - punctuation.find { it.name == "L_ANGLE" }?.symbol to punctuation.find { it.name == "R_ANGLE" }?.symbol, + punctuation.find { it.name == "L_PAREN" }?.symbol to + punctuation.find { it.name == "R_PAREN" }?.symbol, + punctuation.find { it.name == "L_SQUARE" }?.symbol to + punctuation.find { it.name == "R_SQUARE" }?.symbol, + punctuation.find { it.name == "L_CURLY" }?.symbol to + punctuation.find { it.name == "R_CURLY" }?.symbol, + punctuation.find { it.name == "L_ANGLE" }?.symbol to + punctuation.find { it.name == "R_ANGLE" }?.symbol, ).filter { it.first != null && it.second != null } .map { it.first!! to it.second!! } } @@ -234,9 +209,7 @@ data class ScopeDefinition( val treeSitter: String?, ) -/** - * Token rule for lexical analysis. - */ +/** Token rule for lexical analysis. */ @Serializable data class TokenRule( val name: String, @@ -246,9 +219,7 @@ data class TokenRule( val textMateScope: String, ) -/** - * Operator definition with precedence and associativity. - */ +/** Operator definition with precedence and associativity. */ @Serializable data class OperatorDefinition( val symbol: String, @@ -258,9 +229,7 @@ data class OperatorDefinition( val category: OperatorCategory, ) -/** - * Punctuation/delimiter definition. - */ +/** Punctuation/delimiter definition. */ @Serializable data class PunctuationDefinition( val symbol: String, @@ -268,9 +237,7 @@ data class PunctuationDefinition( val name: String, ) -/** - * Language constant definition (boolean literals, null, etc.). - */ +/** Language constant definition (boolean literals, null, etc.). */ @Serializable data class ConstantDefinition( val name: String, @@ -280,9 +247,7 @@ data class ConstantDefinition( val category: String, ) -/** - * Comment syntax definition. - */ +/** Comment syntax definition. */ @Serializable data class CommentSyntax( /** Line comment prefix, e.g. "//" */ @@ -297,9 +262,7 @@ data class CommentSyntax( val docCommentEnd: String, ) -/** - * AST concept definition (represents a node type in the abstract syntax tree). - */ +/** AST concept definition (represents a node type in the abstract syntax tree). */ @Serializable data class ConceptDefinition( val name: String, @@ -311,9 +274,7 @@ data class ConceptDefinition( /** Optional regex pattern for grammar generation */ val syntaxPattern: String?, ) { - /** - * Check if this concept extends another (directly or indirectly). - */ + /** Check if this concept extends another (directly or indirectly). */ fun extendsFrom( conceptName: String, model: LanguageModel, @@ -324,9 +285,7 @@ data class ConceptDefinition( } } -/** - * Scalar property of a concept. - */ +/** Scalar property of a concept. */ @Serializable data class PropertyDefinition( val name: String, @@ -335,9 +294,7 @@ data class PropertyDefinition( val optional: Boolean, ) -/** - * Child node of a concept. - */ +/** Child node of a concept. */ @Serializable data class ChildDefinition( val name: String, @@ -345,9 +302,7 @@ data class ChildDefinition( val cardinality: Cardinality, ) -/** - * Reference to another node. - */ +/** Reference to another node. */ @Serializable data class ReferenceDefinition( val name: String, @@ -359,9 +314,7 @@ data class ReferenceDefinition( // BUILDER CLASSES - DSL implementation // ============================================================================= -/** - * Builder for scope definitions. - */ +/** Builder for scope definitions. */ class ScopeBuilder( private val name: String, ) { @@ -399,9 +352,7 @@ class ScopeBuilder( ) } -/** - * Builder for concept definitions. - */ +/** Builder for concept definitions. */ class ConceptBuilder( private val name: String, private val isAbstract: Boolean, @@ -412,16 +363,12 @@ class ConceptBuilder( private val references = mutableListOf() private var syntaxPattern: String? = null - /** - * Inherit from another concept. - */ + /** Inherit from another concept. */ fun extends(parentConcept: String) { this.parentConcept = parentConcept } - /** - * Add a scalar property. - */ + /** Add a scalar property. */ fun property( name: String, type: String, @@ -431,9 +378,7 @@ class ConceptBuilder( properties.add(PropertyDefinition(name, type, default, optional)) } - /** - * Add a single child node. - */ + /** Add a single child node. */ fun child( name: String, type: String, @@ -442,9 +387,7 @@ class ConceptBuilder( children.add(ChildDefinition(name, type, cardinality)) } - /** - * Add a collection of child nodes (zero or more). - */ + /** Add a collection of child nodes (zero or more). */ fun children( name: String, type: String, @@ -452,9 +395,7 @@ class ConceptBuilder( children.add(ChildDefinition(name, type, Cardinality.ZERO_OR_MORE)) } - /** - * Add a reference to another node. - */ + /** Add a reference to another node. */ fun reference( name: String, type: String, @@ -463,9 +404,7 @@ class ConceptBuilder( references.add(ReferenceDefinition(name, type, optional)) } - /** - * Set a regex pattern for grammar generation. - */ + /** Set a regex pattern for grammar generation. */ fun syntax(pattern: String) { syntaxPattern = pattern } @@ -482,9 +421,7 @@ class ConceptBuilder( ) } -/** - * Main builder for language models. - */ +/** Main builder for language models. */ class LanguageModelBuilder( private val name: String, private val fileExtensions: List, @@ -503,9 +440,7 @@ class LanguageModelBuilder( private val visibilityKeywordsList = mutableListOf() private val concepts = mutableListOf() - /** - * Define a scope mapping for editor styling. - */ + /** Define a scope mapping for editor styling. */ fun scope( name: String, block: ScopeBuilder.() -> Unit, @@ -515,16 +450,12 @@ class LanguageModelBuilder( scopes.add(builder.build()) } - /** - * Register reserved keywords (backward compatible, no category). - */ + /** Register reserved keywords (backward compatible, no category). */ fun keywords(vararg words: String) { keywordsList.addAll(words) } - /** - * Register reserved keywords with a category. - */ + /** Register reserved keywords with a category. */ fun keywords( category: KeywordCategory, vararg words: String, @@ -535,16 +466,12 @@ class LanguageModelBuilder( } } - /** - * Register context-sensitive keywords (backward compatible, no category). - */ + /** Register context-sensitive keywords (backward compatible, no category). */ fun contextKeywords(vararg words: String) { contextKeywordsList.addAll(words) } - /** - * Register context-sensitive keywords with a category. - */ + /** Register context-sensitive keywords with a category. */ fun contextKeywords( category: KeywordCategory, vararg words: String, @@ -555,16 +482,12 @@ class LanguageModelBuilder( } } - /** - * Register built-in types. - */ + /** Register built-in types. */ fun builtinTypes(vararg types: String) { builtinTypesList.addAll(types) } - /** - * Define a lexical token rule. - */ + /** Define a lexical token rule. */ fun token( name: String, pattern: String, @@ -573,9 +496,7 @@ class LanguageModelBuilder( tokens.add(TokenRule(name, pattern, textMateScope)) } - /** - * Define an operator with precedence information. - */ + /** Define an operator with precedence information. */ fun operator( symbol: String, precedence: Int, @@ -585,9 +506,7 @@ class LanguageModelBuilder( operators.add(OperatorDefinition(symbol, precedence, associativity, category)) } - /** - * Define a punctuation/delimiter token. - */ + /** Define a punctuation/delimiter token. */ fun punctuation( symbol: String, name: String, @@ -595,9 +514,7 @@ class LanguageModelBuilder( punctuationList.add(PunctuationDefinition(symbol, name)) } - /** - * Define a language constant (boolean, null, etc.). - */ + /** Define a language constant (boolean, null, etc.). */ fun constant( name: String, literal: String, @@ -606,9 +523,7 @@ class LanguageModelBuilder( constantsList.add(ConstantDefinition(name, literal, category)) } - /** - * Define boolean constants. - */ + /** Define boolean constants. */ fun booleanConstants( trueLiteral: String, falseLiteral: String, @@ -617,16 +532,12 @@ class LanguageModelBuilder( constantsList.add(ConstantDefinition("FALSE", falseLiteral, "boolean")) } - /** - * Define the null constant. - */ + /** Define the null constant. */ fun nullConstant(literal: String) { constantsList.add(ConstantDefinition("NULL", literal, "null")) } - /** - * Define comment syntax. - */ + /** Define comment syntax. */ fun comments( lineComment: String = "//", blockCommentStart: String = "/*", @@ -634,20 +545,25 @@ class LanguageModelBuilder( docCommentStart: String = "/**", docCommentEnd: String = "*/", ) { - commentSyntax = CommentSyntax(lineComment, blockCommentStart, blockCommentEnd, docCommentStart, docCommentEnd) + commentSyntax = + CommentSyntax( + lineComment, + blockCommentStart, + blockCommentEnd, + docCommentStart, + docCommentEnd, + ) } /** - * Define visibility keywords (subset of modifiers that control access). - * These are typically "public", "protected", "private", "internal", etc. + * Define visibility keywords (subset of modifiers that control access). These are typically + * "public", "protected", "private", "internal", etc. */ fun visibilityKeywords(vararg keywords: String) { visibilityKeywordsList.addAll(keywords) } - /** - * Define a concrete AST concept. - */ + /** Define a concrete AST concept. */ fun concept( name: String, block: ConceptBuilder.() -> Unit = {}, @@ -657,9 +573,7 @@ class LanguageModelBuilder( concepts.add(builder.build()) } - /** - * Define an abstract AST concept (cannot be instantiated). - */ + /** Define an abstract AST concept (cannot be instantiated). */ fun abstractConcept( name: String, block: ConceptBuilder.() -> Unit = {}, diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/CCodeDsl.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/CCodeDsl.kt index d11efe3ff4a..4c77d2aa662 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/CCodeDsl.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/CCodeDsl.kt @@ -11,11 +11,8 @@ package org.xtclang.tooling.scanner * - Tree-sitter specific patterns (token emission, lexer operations) */ -/** - * Marker annotation for DSL scope control. - */ -@DslMarker -annotation class CCodeDsl +/** Marker annotation for DSL scope control. */ +@DslMarker annotation class CCodeDsl /** * Builder for C code blocks with automatic indentation. @@ -374,9 +371,7 @@ fun cFunction( return builder.build() } -/** - * Builder for C enums. - */ +/** Builder for C enums. */ @Suppress("unused", "MemberVisibilityCanBePrivate") @CCodeDsl class CEnumBuilder( @@ -418,9 +413,7 @@ fun cEnum( return builder.build() } -/** - * Builder for C struct definitions. - */ +/** Builder for C struct definitions. */ @Suppress("unused", "MemberVisibilityCanBePrivate") @CCodeDsl class CStructBuilder( diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerCGeneratorDsl.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerCGeneratorDsl.kt index 384c77f22df..e54f5a71933 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerCGeneratorDsl.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerCGeneratorDsl.kt @@ -5,8 +5,8 @@ import java.io.File /** * Generates scanner.c using the Kotlin C code DSL. * - * This generator uses composable DSL builders for better maintainability. - * The scanner is STATELESS - it uses valid_symbols to determine context. + * This generator uses composable DSL builders for better maintainability. The scanner is + * STATELESS - it uses valid_symbols to determine context. */ object ScannerCGeneratorDsl { fun generate(debug: Boolean = false): String = @@ -65,9 +65,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} // silently swallowed. appendLine( """ - |typedef struct { - | unsigned char ml_interp_depth; - |} ScannerState; + |typedef struct { + | unsigned char ml_interp_depth; + |} ScannerState; """.trimMargin(), ) appendLine() @@ -205,7 +205,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} comment("The 'TODO' keyword is matched by tree-sitter's internal lexer.") comment("This scanner matches \" message text\" (space + message to end of line).") emptyLine() - comment("This mimics Java Lexer.java:877-885 which calls eatSingleLineComment() for TODO") + comment( + "This mimics Java Lexer.java:877-885 which calls eatSingleLineComment() for TODO", + ) comment("when not followed by '('.") }, ) @@ -251,7 +253,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} emptyLine() debugBlock { - line("fprintf(stderr, \"[SCANNER] Starting stmt block scan, depth=%d, multiline=%d\\n\", depth, multiline);") + line( + "fprintf(stderr, \"[SCANNER] Starting stmt block scan, depth=%d, multiline=%d\\n\", depth, multiline);", + ) } emptyLine() @@ -261,7 +265,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} debugBlock { ifBlock("c >= 32 && c < 127") { - line("fprintf(stderr, \"[SCANNER] stmt_block char='%c' depth=%d\\n\", (char)c, depth);") + line( + "fprintf(stderr, \"[SCANNER] stmt_block char='%c' depth=%d\\n\", (char)c, depth);", + ) } } emptyLine() @@ -318,7 +324,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} emptyLine() debugBlock { - line("fprintf(stderr, \"[SCANNER] Finished stmt block scan, depth=%d\\n\", depth);") + line( + "fprintf(stderr, \"[SCANNER] Finished stmt block scan, depth=%d\\n\", depth);", + ) } emptyLine() @@ -327,8 +335,8 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } /** - * Generates the common prefix for TODO freeform scanner functions. - * Both scan_todo_freeform_text and scan_todo_freeform_until_semi share this pattern: + * Generates the common prefix for TODO freeform scanner functions. Both scan_todo_freeform_text + * and scan_todo_freeform_until_semi share this pattern: * - Debug log entry * - Check for leading whitespace * - Skip whitespace @@ -337,7 +345,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} private fun CCodeBuilder.todoFreeformPrefix(funcName: String) { debugBlock { line("""fprintf(stderr, "[SCANNER] $funcName called, peek='%c'(0x%02x)\n",""") - line(""" (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?', peek(lexer));""") + line( + """ (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?', peek(lexer));""", + ) } comment("Check if we're at whitespace (required after TODO keyword)") @@ -357,14 +367,20 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} debugBlock { line("""fprintf(stderr, "[SCANNER] $funcName: after skip ws, peek='%c'(0x%02x)\n",""") - line(""" (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?', peek(lexer));""") + line( + """ (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?', peek(lexer));""", + ) } emptyLine() comment("Check what follows the whitespace") - ifBlock("at_eof(lexer) || peek(lexer) == '(' || peek(lexer) == '\\n' || peek(lexer) == ';'") { + ifBlock( + "at_eof(lexer) || peek(lexer) == '(' || peek(lexer) == '\\n' || peek(lexer) == ';'", + ) { debugBlock { - line("""fprintf(stderr, "[SCANNER] $funcName: not freeform (paren/newline/semi/eof)\n");""") + line( + """fprintf(stderr, "[SCANNER] $funcName: not freeform (paren/newline/semi/eof)\n");""", + ) } returnFalse() } @@ -372,8 +388,8 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } /** - * Generates the expression/statement block start handling common to both - * single-line and multiline templates. + * Generates the expression/statement block start handling common to both single-line and + * multiline templates. * * @param exprStart The escaped character for expression start ('{') * @param prefix Either "SINGLELINE" or "MULTILINE" for token names @@ -400,13 +416,17 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } comment("Not a statement block, but we already consumed {") ifBlock("valid_symbols[${prefix}_EXPR_START]") { - line("${depthBump}lexer->mark_end(lexer); lexer->result_symbol = ${prefix}_EXPR_START; return true;") + line( + "${depthBump}lexer->mark_end(lexer); lexer->result_symbol = ${prefix}_EXPR_START; return true;", + ) } breakStmt() } ifBlock("valid_symbols[${prefix}_EXPR_START]") { advance() - line("${depthBump}lexer->mark_end(lexer); lexer->result_symbol = ${prefix}_EXPR_START; return true;") + line( + "${depthBump}lexer->mark_end(lexer); lexer->result_symbol = ${prefix}_EXPR_START; return true;", + ) } breakStmt() } @@ -414,8 +434,8 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } /** - * Generates the escape sequence handling and default advance common to both - * single-line and multiline templates. + * Generates the escape sequence handling and default advance common to both single-line and + * multiline templates. * * @param backslash The escaped backslash character */ @@ -443,7 +463,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} todoFreeformPrefix("scan_todo_freeform_text") debugBlock { - line("""fprintf(stderr, "[SCANNER] scan_todo_freeform_text: consuming to EOL\n");""") + line( + """fprintf(stderr, "[SCANNER] scan_todo_freeform_text: consuming to EOL\n");""", + ) } comment("It's freeform text - consume to end of line") @@ -471,7 +493,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} emptyLine() debugBlock { - line("""fprintf(stderr, "[SCANNER] scan_todo_freeform_until_semi: found_semi=%d\n", found_semi);""") + line( + """fprintf(stderr, "[SCANNER] scan_todo_freeform_until_semi: found_semi=%d\n", found_semi);""", + ) } comment("Only succeed if we found a ';' (so grammar can match it)") @@ -523,7 +547,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} comment("During error recovery, tree-sitter may set ALL external tokens valid.") comment("If both single-line AND multiline tokens are valid simultaneously,") - comment("we're likely in error recovery - return false to let tree-sitter handle it.") + comment( + "we're likely in error recovery - return false to let tree-sitter handle it.", + ) ifBlock("in_singleline && in_multiline") { returnFalse() } @@ -533,9 +559,13 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} line( """fprintf(stderr, "[SCANNER] char='%c'(0x%02x) single=%d multi=%d expr=%d todo_text=%d todo_until_semi=%d type_gt=%d\n",""", ) - line(""" (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?',""") + line( + """ (peek(lexer) >= 32 && peek(lexer) < 127) ? (char)peek(lexer) : '?',""", + ) line(""" peek(lexer), in_singleline, in_multiline, in_expr,""") - line(""" valid_symbols[TODO_FREEFORM_TEXT], valid_symbols[TODO_FREEFORM_UNTIL_SEMI], valid_symbols[TYPE_GT]);""") + line( + """ valid_symbols[TODO_FREEFORM_TEXT], valid_symbols[TODO_FREEFORM_UNTIL_SEMI], valid_symbols[TYPE_GT]);""", + ) } emptyLine() @@ -556,7 +586,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} emitToken("TYPE_GT") } comment("Outside template interpolation, allow whitespace before `>`.") - ifBlock("!valid_symbols[TEMPLATE_EXPR_END] && !in_singleline && !in_multiline") { + ifBlock( + "!valid_symbols[TEMPLATE_EXPR_END] && !in_singleline && !in_multiline", + ) { whileBlock( "!at_eof(lexer) && (peek(lexer) == ' ' || peek(lexer) == '\\t' || peek(lexer) == '\\n' || peek(lexer) == '\\r')", ) { @@ -665,7 +697,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} comment("String literal: skip past matching `\"`, honoring escapes.") ifBlock("tc == '\"'") { advance() - whileBlock("!at_eof(lexer) && peek(lexer) != '\"' && peek(lexer) != '\\n'") { + whileBlock( + "!at_eof(lexer) && peek(lexer) != '\"' && peek(lexer) != '\\n'", + ) { ifBlock("peek(lexer) == '\\\\'") { advance() ifBlock("!at_eof(lexer)") { @@ -686,7 +720,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} comment("Character literal: skip past matching `'`, honoring escapes.") ifBlock("tc == '\\''") { advance() - whileBlock("!at_eof(lexer) && peek(lexer) != '\\'' && peek(lexer) != '\\n'") { + whileBlock( + "!at_eof(lexer) && peek(lexer) != '\\'' && peek(lexer) != '\\n'", + ) { ifBlock("peek(lexer) == '\\\\'") { advance() ifBlock("!at_eof(lexer)") { @@ -785,7 +821,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} comment("- If there's a ';' on the line, use until_semi (stops at ';')") comment("- Otherwise, use freeform_text (consumes to EOL)") - ifBlock("(valid_symbols[TODO_FREEFORM_UNTIL_SEMI] || valid_symbols[TODO_FREEFORM_TEXT]) && is_hspace(peek(lexer))") { + ifBlock( + "(valid_symbols[TODO_FREEFORM_UNTIL_SEMI] || valid_symbols[TODO_FREEFORM_TEXT]) && is_hspace(peek(lexer))", + ) { comment("Skip whitespace first (common to both)") whileBlock("!at_eof(lexer) && is_hspace(peek(lexer))") { advance() @@ -793,12 +831,16 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} emptyLine() comment("Check what follows the whitespace") - ifBlock("at_eof(lexer) || peek(lexer) == '(' || peek(lexer) == '$newline' || peek(lexer) == ';'") { + ifBlock( + "at_eof(lexer) || peek(lexer) == '(' || peek(lexer) == '$newline' || peek(lexer) == ';'", + ) { returnFalse() } emptyLine() - comment("Now we know there's freeform text. Check if there's a ';' on this line.") + comment( + "Now we know there's freeform text. Check if there's a ';' on this line.", + ) variable("bool", "has_semicolon", "false") emptyLine() @@ -818,28 +860,40 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} markEnd() line("lexer->result_symbol = TODO_FREEFORM_UNTIL_SEMI;") debugBlock { - line("""fprintf(stderr, "[SCANNER] TODO: returning UNTIL_SEMI (found ;)\n");""") + line( + """fprintf(stderr, "[SCANNER] TODO: returning UNTIL_SEMI (found ;)\n");""", + ) } returnTrue() } elseIfBlock("!has_semicolon && valid_symbols[TODO_FREEFORM_TEXT]") { - comment("No ';' and freeform_text is valid - we've already consumed the text") + comment( + "No ';' and freeform_text is valid - we've already consumed the text", + ) markEnd() line("lexer->result_symbol = TODO_FREEFORM_TEXT;") debugBlock { - line("""fprintf(stderr, "[SCANNER] TODO: returning FREEFORM_TEXT (no ;)\n");""") + line( + """fprintf(stderr, "[SCANNER] TODO: returning FREEFORM_TEXT (no ;)\n");""", + ) } returnTrue() } - elseIfBlock("has_semicolon && !valid_symbols[TODO_FREEFORM_UNTIL_SEMI] && valid_symbols[TODO_FREEFORM_TEXT]") { - comment("Has ';' but only freeform_text is valid - consume to EOL including ';'") + elseIfBlock( + "has_semicolon && !valid_symbols[TODO_FREEFORM_UNTIL_SEMI] && valid_symbols[TODO_FREEFORM_TEXT]", + ) { + comment( + "Has ';' but only freeform_text is valid - consume to EOL including ';'", + ) whileBlock("!at_eof(lexer) && peek(lexer) != '$newline'") { advance() } markEnd() line("lexer->result_symbol = TODO_FREEFORM_TEXT;") debugBlock { - line("""fprintf(stderr, "[SCANNER] TODO: returning FREEFORM_TEXT (has ; but until_semi not valid)\n");""") + line( + """fprintf(stderr, "[SCANNER] TODO: returning FREEFORM_TEXT (has ; but until_semi not valid)\n");""", + ) } returnTrue() } @@ -847,7 +901,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } emptyLine() - sectionComment("Inside expression: skip whitespace + `|` continuations, then look for closing brace") + sectionComment( + "Inside expression: skip whitespace + `|` continuations, then look for closing brace", + ) comment("Tree-sitter does NOT auto-skip extras before invoking an external") comment("scanner, so we have to skip whitespace ourselves to handle forms") comment("like `\${ y }` or `\$\"{y } abc\"` where the closing `}` is preceded") @@ -880,7 +936,9 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} } emptyLine() - sectionComment("MULTILINE_CONTINUATION: `\\n` + optional ` |` inside a multiline interpolation") + sectionComment( + "MULTILINE_CONTINUATION: `\\n` + optional ` |` inside a multiline interpolation", + ) comment("Gated on the persistent `ml_interp_depth` counter so the `|` is") comment("only consumed when we're certain we're inside an interpolation") comment("expression that belongs to a `\$|...|` multiline template. The") @@ -890,9 +948,7 @@ ${if (debug) "\n#define SCANNER_DEBUG 1" else ""} "valid_symbols[MULTILINE_CONTINUATION] && peek(lexer) == '\\n' && state->ml_interp_depth > 0 && !in_singleline && !in_multiline", ) { advance() - whileBlock( - "!at_eof(lexer) && (peek(lexer) == ' ' || peek(lexer) == '\\t')", - ) { + whileBlock("!at_eof(lexer) && (peek(lexer) == ' ' || peek(lexer) == '\\t')") { advance() } ifBlock("peek(lexer) == '|'") { diff --git a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerSpec.kt b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerSpec.kt index 3941a3dd004..ff0a28d7622 100644 --- a/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerSpec.kt +++ b/lang/dsl/src/main/kotlin/org/xtclang/tooling/scanner/ScannerSpec.kt @@ -89,23 +89,20 @@ enum class TokenType { TUPLE_ASSIGN_LPAREN, } -/** - * Scanner behavior rules and constants. - */ +/** Scanner behavior rules and constants. */ object TemplateScannerSpec { val tokens = TokenType.entries /** * Scanner logic - determined by which tokens are valid: * - * 1. If SINGLELINE_CONTENT or SINGLELINE_EXPR_START or SINGLELINE_END valid: - * → In single-line template: scan until '{' or `"`, handle escapes + * 1. If SINGLELINE_CONTENT or SINGLELINE_EXPR_START or SINGLELINE_END valid: → In single-line + * template: scan until '{' or `"`, handle escapes * - * 2. If MULTILINE_CONTENT or MULTILINE_EXPR_START or MULTILINE_END valid: - * → In multiline template: scan until '{' or newline-without-continuation + * 2. If MULTILINE_CONTENT or MULTILINE_EXPR_START or MULTILINE_END valid: → In multiline + * template: scan until '{' or newline-without-continuation * - * 3. If TEMPLATE_EXPR_END valid and see }: - * → End of embedded expression + * 3. If TEMPLATE_EXPR_END valid and see }: → End of embedded expression */ object Rules { const val EXPR_START_CHAR = '{' diff --git a/lang/dsl/src/test/kotlin/org/xtclang/tooling/DslPowerShowcaseTest.kt b/lang/dsl/src/test/kotlin/org/xtclang/tooling/DslPowerShowcaseTest.kt index 091c10938ba..20abce38967 100644 --- a/lang/dsl/src/test/kotlin/org/xtclang/tooling/DslPowerShowcaseTest.kt +++ b/lang/dsl/src/test/kotlin/org/xtclang/tooling/DslPowerShowcaseTest.kt @@ -398,7 +398,10 @@ class DslPowerShowcaseTest { assertTrue(textMate.contains(testKeyword), "TextMate has '$testKeyword'") assertTrue(vim.contains(testKeyword), "Vim has '$testKeyword'") assertTrue(emacs.contains("\"$testKeyword\""), "Emacs has '$testKeyword'") - assertTrue(treeSitterHighlights.contains("\"$testKeyword\""), "Tree-sitter has '$testKeyword'") + assertTrue( + treeSitterHighlights.contains("\"$testKeyword\""), + "Tree-sitter has '$testKeyword'", + ) // All contain built-in types val testType = "String" diff --git a/lang/dsl/src/test/kotlin/org/xtclang/tooling/LanguageModelTest.kt b/lang/dsl/src/test/kotlin/org/xtclang/tooling/LanguageModelTest.kt index 2d1643543cc..a04701dddcd 100644 --- a/lang/dsl/src/test/kotlin/org/xtclang/tooling/LanguageModelTest.kt +++ b/lang/dsl/src/test/kotlin/org/xtclang/tooling/LanguageModelTest.kt @@ -69,9 +69,18 @@ class LanguageModelTest { assertNotNull(memberAccess) // Precedence should be: = < + < * < . - assertTrue(assignment.precedence < addition.precedence, "Assignment should have lower precedence than addition") - assertTrue(addition.precedence < multiplication.precedence, "Addition should have lower precedence than multiplication") - assertTrue(multiplication.precedence < memberAccess.precedence, "Multiplication should have lower precedence than member access") + assertTrue( + assignment.precedence < addition.precedence, + "Assignment should have lower precedence than addition", + ) + assertTrue( + addition.precedence < multiplication.precedence, + "Addition should have lower precedence than multiplication", + ) + assertTrue( + multiplication.precedence < memberAccess.precedence, + "Multiplication should have lower precedence than member access", + ) } @Test @@ -290,9 +299,11 @@ class LanguageModelTest { } println("Built-in type occurrences (sample of 50 files):") - typeCounts.entries.sortedByDescending { it.value }.forEach { (type, count) -> - println(" $type: $count") - } + typeCounts.entries + .sortedByDescending { it.value } + .forEach { (type, count) -> + println(" $type: $count") + } assertTrue(typeCounts.containsKey("String"), "Should find String type") assertTrue(typeCounts.containsKey("Int"), "Should find Int type") @@ -329,9 +340,11 @@ class LanguageModelTest { } println("Operator occurrences (sample of 50 files):") - opCounts.entries.sortedByDescending { it.value }.forEach { (op, count) -> - println(" '$op': $count") - } + opCounts.entries + .sortedByDescending { it.value } + .forEach { (op, count) -> + println(" '$op': $count") + } assertTrue(opCounts.containsKey("=="), "Should find == operator") } @@ -408,7 +421,10 @@ class LanguageModelTest { val declarationKeywords = model.keywordsByCategory(KeywordCategory.DECLARATION) assertTrue(declarationKeywords.isNotEmpty(), "Should have declaration keywords") assertTrue("class" in declarationKeywords, "Declaration keywords should include 'class'") - assertTrue("interface" in declarationKeywords, "Declaration keywords should include 'interface'") + assertTrue( + "interface" in declarationKeywords, + "Declaration keywords should include 'interface'", + ) assertTrue("module" in declarationKeywords, "Declaration keywords should include 'module'") } @@ -538,7 +554,10 @@ class LanguageModelTest { assertTrue(kw in controlKeywords, "'$kw' should be a control keyword") assertTrue(vim.contains(kw), "Vim should contain '$kw'") assertTrue(emacs.contains("\"$kw\""), "Emacs should contain '$kw'") - assertTrue(highlights.contains("\"$kw\""), "Tree-sitter highlights should contain '$kw'") + assertTrue( + highlights.contains("\"$kw\""), + "Tree-sitter highlights should contain '$kw'", + ) } } diff --git a/lang/generated-examples/xtc.tmLanguage.json b/lang/generated-examples/xtc.tmLanguage.json index effc2894589..d868cf22fd6 100644 --- a/lang/generated-examples/xtc.tmLanguage.json +++ b/lang/generated-examples/xtc.tmLanguage.json @@ -267,7 +267,7 @@ "method-declaration": { "name": "meta.method.xtc", "begin": "\\b((?:(?:public|private|protected|static|abstract|final|native|lazy|atomic|inject|delegate|override)\\s+)*)(<[^>]+>\\s+)?([A-Z][a-zA-Z0-9_<>?,\\s]*)\\s+([a-z_][a-zA-Z0-9_]*)\\s*(?=\\()", - "end": "(?=\\{|;)", + "end": "(?=\\{|;|=)", "beginCaptures": { "1": { "name": "storage.modifier.xtc" diff --git a/lang/intellij-plugin/README.md b/lang/intellij-plugin/README.md index ca0e3b0cdc5..c0c1740aa9a 100644 --- a/lang/intellij-plugin/README.md +++ b/lang/intellij-plugin/README.md @@ -1,12 +1,20 @@ # Ecstasy IntelliJ Plugin -IntelliJ IDEA plugin for Ecstasy language support. +The bundled language server uses the compiler adapter by default. Tree-sitter remains a selectable +syntax-only alternative. Compiler features include diagnostics, navigation, references, completion, +theme-based semantic highlighting and guarded refactorings over configured source and library inputs. +See the [manual playbook](../doc/manual-test-plan.md) for exact feature boundaries and native tests. ## Features - **New Project Wizard** - Create XTC projects directly from IntelliJ (File → New → Project → XTC) - **Run Configurations** - Run XTC applications via Gradle or `xtc run` -- **Syntax Highlighting** - Full syntax highlighting for `.x` files (via TextMate grammar) +- **Syntax Highlighting** - TextMate grammar for `.x` files, explicitly connected to Ecstasy’s + native file type through TextMate’s syntax factory and editor highlighter. +- **Semantic Highlighting** - Enabled by default; compiler-resolved types, functions, enum members + annotations and bundled-library identities refine the editor's theme colors. String escapes, + interpolation, keywords and numeric literals retain their lexical scopes. See the + [highlighting playbook](../doc/manual-test-plan.md#19-semantic-tokens) for examples and opt-out settings. - **Language Features via LSP** - hover, completion, go-to-definition, find references, outline, auto-indent on type (see [LSP Server README](../lsp-server/README.md) for details) - **Code Style Settings** - Configurable indentation defaults under @@ -56,7 +64,8 @@ Then install manually: ## Prerequisites -- IntelliJ IDEA 2026.1 or later +- IntelliJ IDEA 2026.2 or later (build 262+); development and acceptance tests target 2026.2.3 +- The free feature set is sufficient; no Ultimate subscription is required - XDK installed and `xtc` command available in PATH - Gradle plugin for IntelliJ (bundled with most editions) @@ -110,6 +119,63 @@ The plugin invokes `xtc init` to scaffold the project, then imports it as a Grad | `runIde` | Launches sandbox IDE | Quick testing during development | | `verifyPlugin` | Verification report | Check IDE compatibility | +### Compiler playbook in IntelliJ + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true +``` + +This opt-in task launches the packaged plugin with released LSP4IJ in an isolated IntelliJ profile. +Ultimate features and automatic trials are disabled. The 284-case catalog is shared with VS Code; +see the [manual playbook](../doc/manual-test-plan.md) and [testing reference](TESTING.md). + +The task also runs startup editing, focus recovery, settings persistence and project-lifecycle tests. +Select a method with `--tests '*CompilerPlaybookTest.compilerPlaybook'`, or select feature cases with +`-PintellijPlaybookCases=X145,X146,X147,X259`. Omit the case property for the full catalog. +The large-file probe is separately enabled with `-PintellijLargeFileProbe=true`. + +The status bar displays the current case and completed/remaining counts. The harness may restore +desktop focus; avoid typing into its fixtures. Failed operations stop that run, and timed-out +mutations are not replayed. Unexpected IDE errors fail acceptance. X254/X257 retain explicit partial +coverage for the [client compatibility limits](../doc/manual-test-plan.md#client-compatibility-limits). + +Results, live `progress.jsonl`, screenshots and server traces are written under +`build/reports/compiler-playbook/run-*`. Inspect `results.json` and `ideFailures`, plus JUnit XML +under `build/test-results/testCompilerPlaybook/`. `ide-paths.txt` identifies the disposable IDE logs. +Tests use the signed native IDE distribution and JBR from the Starter cache; test-only probes are +absent from the shipped plugin. Report cleanup retains five completed payloads by default. + +### Compiler source-module configuration + +Compiler mode bundles the full matching XDK library set as read-only dependencies and discovers +source modules/import edges under workspace folders at startup and on watched-file changes. +The configuration below overrides that discovered graph. Set `sourceModules` to `null` to +restore discovery; `[]` disables it. Unsaved import edges, close and dynamic folder changes now refresh discovered graphs. + +With a compiler build, open **Settings → Languages & Frameworks → Language Servers**, select +**XTC Language Server**, and edit its **Configuration** JSON: + +```json +{ + "xtc": { + "compiler": { + "sourceModules": [ + { "name": "Library", "uri": "Library.x" }, + { "name": "Consumer", "uri": "Consumer.x", "dependencies": ["Library"] } + ] + } + } +} +``` + +LSP4IJ stores these server settings at IDE scope. Relative paths resolve against the current +single workspace folder; use absolute file URIs when needed. Applying settings notifies the +running server. Setting `sourceModules` to `[]` clears the graph; omitting it preserves the +current graph. This uses LSP4IJ's existing JSON editor, not a dedicated XTC project-settings UI. +`XtcLanguageClient` delegates section lookup and configuration notifications to LSP4IJ; only +`xtc.formatting` reads Ecstasy Code Style settings. + ### Release-Grade Build And Publish Use these commands from the repository root when preparing an installable alpha ZIP or publishing to JetBrains Marketplace: @@ -163,7 +229,7 @@ What matters is the Gradle task outcome: ### Installing the Built Plugin -After running `buildPlugin`, you can install the ZIP in any IntelliJ IDEA 2025.1+ instance: +After running `buildPlugin`, you can install the ZIP in an IntelliJ IDEA 2026.2+ instance: 1. Locate the ZIP: `lang/intellij-plugin/build/distributions/intellij-plugin-.zip` 2. Open IntelliJ IDEA → **Settings/Preferences → Plugins** @@ -224,10 +290,18 @@ feature: #### Testing LSP Features (Language Server) -The LSP server supports multiple adapters. See [LSP Server README](../lsp-server/README.md) for details. +Use **Tools → Switch Ecstasy Language Adapter**, Find Action, or **Ctrl+Alt+X, then A** +(**Control+Option+X, then A** on macOS) to select **Ecstasy Compiler**, **Tree-sitter** or +**Bundled default**. The same choice is available in **Ecstasy Language Service** settings. +Compiler provides semantic analysis; Tree-sitter provides syntax-based features without compiler +type checking. Switching restarts the server and reopens unsaved buffers. Both adapters ship in +the plugin, so switching does not require rebuilding or changing LSP4IJ. + +The default setting honors the server build's choice, normally Compiler. Development builds can +still change that default. See [LSP Server README](../lsp-server/README.md) for details. ```bash -# Run with default adapter (tree-sitter - AST-based) +# Run with the default compiler adapter and bundled XDK ./gradlew :lang:intellij-plugin:runIde # Run with mock adapter (regex-based, no native dependencies) @@ -250,15 +324,15 @@ stale sandbox state, or missing artifacts: ``` [runIde] ─── Version Matrix (gradle/libs.versions.toml) ─── -[runIde] IntelliJ IDEA: 2026.1 (sinceBuild=261) -[runIde] LSP4IJ: 0.19.2 +[runIde] IntelliJ IDEA: 2026.2.3 (sinceBuild=262) +[runIde] LSP4IJ: 0.21.0 [runIde] XTC plugin: 0.4.4-SNAPSHOT [runIde] ─── Sandbox ─── -[runIde] Path: .../build/idea-sandbox/IC-2026.1 +[runIde] Path: .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3 [runIde] Status: reused (existing sandbox with IDE caches/indices) [runIde] Plugins: [intellij-plugin, lsp4ij] -[runIde] IDE log: .../build/idea-sandbox/IC-2026.1/log/idea.log -[runIde] tail -f .../build/idea-sandbox/IC-2026.1/log/idea.log +[runIde] IDE log: .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log +[runIde] tail -f .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log [runIde] ─── mavenLocal XTC Artifacts ─── [runIde] ~/.m2/repository/org/xtclang [runIde] xdk: 0.4.4-SNAPSHOT @@ -266,7 +340,7 @@ stale sandbox state, or missing artifacts: [runIde] ─── Reset Commands ─── [runIde] Nuke sandbox (keeps IDE download): ./gradlew :lang:intellij-plugin:clean [runIde] Nuke cached IDE + metadata: rm -rf lang/.intellijPlatform/localPlatformArtifacts -[runIde] LSP log: ~/.xtc/logs/lsp-server.log (tailing to console) +[runIde] LSP log: ~/.xtc/logs/lsp/server-*/server.log (tailing to console) ``` Once the IDE is running and you open a `.x` file, LSP server logs are streamed @@ -306,15 +380,15 @@ These appear with a `[lsp-server]` prefix whenever the LSP server is active. noisy (indexing, VFS, GC, etc.). To view them in a separate terminal: ```bash -tail -f lang/intellij-plugin/build/idea-sandbox/IC-2026.1/log/idea.log +tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log # Or filter to XTC-related entries: -tail -f lang/intellij-plugin/build/idea-sandbox/IC-2026.1/log/idea.log | grep -i "xtc\|lsp" +tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log | grep -i "xtc\|lsp" ``` **LSP server file log** (always available, even outside `runIde`): ```bash -tail -f ~/.xtc/logs/lsp-server.log +tail -f ~/.xtc/logs/lsp/server-*/server.log ``` #### Clearing Sandbox State @@ -324,7 +398,7 @@ tail -f ~/.xtc/logs/lsp-server.log ./gradlew :lang:intellij-plugin:clean # Nuke everything including the downloaded IDE (re-downloads ~1.5 GB) -rm -rf ~/.gradle/caches/modules-2/files-2.1/idea/ideaIC/2026.1 +rm -rf lang/.intellijPlatform/ides/IU-2026.2.3 rm -rf lang/.intellijPlatform/localPlatformArtifacts ``` @@ -569,7 +643,7 @@ intellij-plugin/ - Communication is via stdio (stdin/stdout) using JSON-RPC; logging goes to stderr - `XtcLanguageClient` bridges IntelliJ Code Style settings to the LSP server via `workspace/configuration` - LSP4IJ captures stderr and shows it in the Language Servers panel -- The `runIde` task also tails `~/.xtc/logs/lsp-server.log` to the Gradle console +- The `runIde` task also tails `~/.xtc/logs/lsp/server-*/server.log` to the Gradle console ## Troubleshooting @@ -592,3 +666,111 @@ Ensure you have the Gradle plugin enabled in IntelliJ (bundled by default). ## License Apache License 2.0 - See [LICENSE](../../LICENSE) for details. + +The native file-tree Move action now requests compiler proof before moving selected source files +or module containers. Shared X130 passes native batch Move and project Undo/Redo. Labels +use Ecstasy; implementation names retain Xtc. Upstream compatibility bridges carry searchable +`// TODO LSP4IJ:` comments with their removal conditions and stable UP identifiers in the +[client compatibility limits](../doc/manual-test-plan.md#client-compatibility-limits). + +Lazy action selection now uses a registered client command to resolve and apply the edit in a +version-checked undo command; selected native X105/X122/X127 acceptance passes. X131 adds protocol assertions +for code-lens/link/inlay/symbol resolution through the installed client connection. + +L72 adds shared X132 for multiple-range formatting and negotiated save hooks; selected native +acceptance and the backend/packaged transport regressions pass. +The server defaults to full synchronization and no save-time edits; custom LSP hosts can set +`initializationOptions.xtcDocumentSync` with independent `incremental` and `formatOnSave` booleans. +No additional compiler process or compilation is needed for save formatting. + +### Quick server log access + +Press **Ctrl+Alt+X, then L** (**Control+Option+X, then L** on macOS) to show/hide the server log. +The view retains its history while hidden. Change the shortcut in the editor's native keymap. +See the [manual playbook](../doc/manual-test-plan.md#server-log-shortcut-acceptance) for acceptance +steps and the distinction between server logs and protocol tracing. + +Language-service preferences are under **Settings → Languages & Frameworks → Ecstasy Language +Service**; **Ecstasy Language Service Defaults** supplies application defaults. Project overrides +share LSP4IJ storage and preserve compiler graph/Undo ownership. Changing the language adapter or +Full/Incremental text transport restarts the service and restores unsaved buffers. Inlay changes and Code Style settings +apply live. The read-only effective view includes PID, runtime, capabilities, bundled read-only XDK +libraries and compiler queue names/count. Server save edits are unavailable in LSP4IJ 0.21.0; use +native **Actions on Save → Reformat code**. Compiler mode wraps expressions/lists at safe token +boundaries using the Code Style right margin; literal splitting and comment reflow are not supported. + +X142 verifies constant-initializer navigation and rename/Undo. X143 compares partial +workspace-symbol batches with the ordinary response. These checks complement the protocol +and lifecycle cases; see the [manual playbook](../doc/manual-test-plan.md). + +X145–X147 exercise cancellation, refresh and report ownership. X259 checks displayed consumer inlays while applying the real Language Service settings page, +changing a dependency and restarting; the consumer text/version must remain unchanged. +This complements the existing two-project lifetime check. See the +[acceptance reports](../doc/manual-test-plan.md#acceptance-reports). + +### Ordered libraries and attached sources + +**Settings → Ecstasy Compiler → Libraries and sources** edits ordered external binary paths +and per-module source-directory attachments. Use **Inherit binary libraries from Gradle** to keep +build-model ownership, or clear the list to remove external libraries. The bundled XDK is always +present and read-only. Apply persists the draft; Reset/Cancel discards pending changes. **Reload +applied libraries** rereads saved paths without a restart. The source-module tab also offers +**Order resource directories…** with native path selection and automatic-resource inheritance. + +Attachments supply navigation-only snapshots; they do not add compiler source modules. IntelliJ +opens protected fallback files until LSP4IJ supports our virtual library editor (UP25). Use matching +sources; available debug text is checked but is not a complete binary/source equivalence proof. +Project settings survive server restart; invalid replacements retain accepted compiler inputs. +Shared X266–X268 and their [acceptance reports](../doc/manual-test-plan.md#acceptance-reports) +record automated coverage and the remaining acceptance boundaries. + +### Machine-local JVM tuning and log support + +Open **Settings → Ecstasy Server Runtime and Logs**. Enter one supported JVM option per line +(e.g. `-Xmx2G`). Apply saves locally, outside project files and Settings Sync; the explicit Restart +button restarts running Ecstasy servers in this IDE using the saved settings. + +Retention defaults: 7 days, 10 MB/file, 50 MB archived per stream and five stopped-server sessions. +Logs live under `~/.xtc/logs/lsp/server--/`; active files are additional to archive +caps, active processes are protected and stopped sessions are pruned on the next server start. +Legacy shared logs and the IDE's own protocol console are outside this policy. + +**Tools → Export Ecstasy Server Logs** saves a ZIP of recent log tails and current status from +the connected server (at most eight 512 KiB tails, 4 MiB log input). It may contain local paths and +logged diagnostics; it does not collect source files. When stopped, export uses only this project’s +last recorded launch, including up to 64 KiB of launcher output. Its manifest identifies offline +mode and truncation and lists included files; another project’s logs are never substituted. Shared +X269–X272 cover settings, restart, live/offline export and recovery from an intentional failed JVM +launch. Full editor exit/reopen has a separate two-process persistence test. Native OS save-dialog +layout and rollover stress remain manual checks. See the [acceptance report format](../doc/manual-test-plan.md#acceptance-reports) for +how these checks are recorded. + +The **Ecstasy Compiler** page separates **Source modules**, **Libraries and sources** and +**Build import**. Import buttons stay with their help; the detailed paths/report are expandable. +Build import uses native Kotlin UI DSL groups, ordinary-width buttons, smaller help text and +collapsed details, and a bordered vertical page list. Build actions are stacked at their normal +width; the standalone window fits the selected page. Switching pages preserves unapplied edits. +The Language Service page distinguishes saved machine runtime settings from the running server; +stale replies cannot replace a newer report. + +### Experimental color-value picker + +The opt-in L77 prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared +fixture. It supplies native color swatches and picker edits separately from normal semantic +highlighting. It is disabled for ordinary launches and does not introduce a public XDK color API. +Shared X273–X275 exercise picker edits, dismissal, native Undo/Redo and diagnostic recovery. +See the [prototype recipe and acceptance](../doc/manual-test-plan.md#l77-color-value-prototype). + +### Compiler documentation and reference counts + +The compiler adapter offers **Generate documentation comment** on written declaration headers. +It preserves indentation and line endings, derives method parameter/return entries from the +compiler signature, and leaves existing comments intact. The edit participates in normal Undo/Redo. + +Reference CodeLens shows source type, method and property usage counts across the complete configured +source graph, including unopened consumers. Counts exclude declarations and are omitted while the +graph cannot be resolved. Clicking a count opens the editor's native references view. External +consumers outside the configured graph are not counted. Run lenses remain independent. + +Use **Ecstasy Language Service → Show Ecstasy reference counts** to toggle counts live. The +project can inherit the application preference. IntelliJ Code Vision settings still control rendering. diff --git a/lang/intellij-plugin/TESTING.md b/lang/intellij-plugin/TESTING.md index ea245d4e393..7238d37c2a8 100644 --- a/lang/intellij-plugin/TESTING.md +++ b/lang/intellij-plugin/TESTING.md @@ -26,45 +26,36 @@ E2E framework, log harvesting, and how each approach maps to the plugin's extens ## Current State -The plugin has **two test files**, both pure unit tests with no IntelliJ platform dependencies: +Headless unit and manifest tests live under `src/test/kotlin`; the native Starter/Driver harness +lives under `src/integrationTest/kotlin`. They cover project configuration/import ownership, +watchers, source graphs, lifecycle, edit guards, startup, diagnostics and packaged resources. -### `LspServerJarResolutionTest.kt` +The native suite uses the shared **284-scenario catalog**. It launches a disposable IntelliJ +profile with Ultimate disabled and released LSP4IJ. Startup editing, focus recovery, settings +persistence and project lifecycle are separate tests in the same task. The large-file diagnostic +is opt-in. See the [manual playbook](../doc/manual-test-plan.md) for fixtures and +[client compatibility limits](../doc/manual-test-plan.md#client-compatibility-limits). -Tests `XtcLspConnectionProvider.resolveServerJar()` — a static method that locates -`bin/xtc-lsp-server.jar` relative to a plugin directory path. - -- 5 tests: correct layout, missing bin dir, JAR in wrong directory, empty bin, wrong name -- Uses: JUnit 5 + AssertJ + `@TempDir` -- No IntelliJ API usage — tests a pure `Path` → `Path?` function - -### Build Configuration - -```kotlin -// build.gradle.kts (dependencies) -testImplementation(platform(libs.junit.bom)) // JUnit 6.0.2 BOM -testImplementation(libs.junit.jupiter) -testRuntimeOnly(libs.junit.platform.launcher) -testRuntimeOnly(libs.lang.intellij.junit4.compat) // JUnit 4.13.2 (required by IntelliJ test harness) -testImplementation(libs.assertj) // AssertJ 3.27.7 - -// build.gradle.kts (test task) -val test by tasks.existing(Test::class) { - useJUnitPlatform() - jvmArgs("-Xlog:cds=off") // Suppress CDS warning from IntelliJ's PathClassLoader - testLogging { events("passed", "skipped", "failed") } -} +```bash +./gradlew :lang:intellij-plugin:test \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true --no-build-cache +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true --no-build-cache ``` -The IntelliJ Platform Gradle Plugin (2.10.5) automatically configures the test JVM with: -- `-Djava.system.class.loader=com.intellij.util.lang.PathClassLoader` -- `idea.classpath.index.enabled=false` -- `idea.force.use.core.classloader=true` -- Sandbox directories (`config/`, `plugins/`, `system/`, `log/`) via `SandboxArgumentProvider` +Use `--tests '*CompilerPlaybookTest.compilerPlaybook'` to select only feature tests, and +`-PintellijPlaybookCases=X145,X146,X147,X259` for a bounded selection. Omit the case property for +the full implemented catalog. Run native editors sequentially: the driver can restore desktop +focus, and fixture modifications during a test fail explicitly. Completed mutations are not replayed. -This means the IntelliJ classloading infrastructure is already available to tests — the -missing piece is the test framework dependency and test base classes. +JUnit XML is under `build/test-results/`; per-case results, IDE errors, traces and screenshots are +under `build/reports/compiler-playbook/run-*`. Read the actual case statuses and `ideFailures`; +a green task summary is insufficient. Partial and unselected cases do not count as full native +acceptance. Test history from older builds does not substitute for running the current artifact. ---- +The compiler is the default adapter. Native tests reuse the cached signed IntelliJ distribution +and JBR, with isolated configuration, plugins and indices. Report cleanup retains five completed +payloads; `-PplaybookRetainedRuns=N` changes that count, and a `.keep-artifacts` marker preserves a run. ## Testing Approaches Overview @@ -317,7 +308,7 @@ Logs **persist after the IDE exits** — you can always read them post-hoc. The LSP server writes to a separate log file: ``` -~/.xtc/logs/lsp-server.log +~/.xtc/logs/lsp/server-*/server.log ``` This is independent of the IDE sandbox and persists across IDE sessions. @@ -339,7 +330,7 @@ After any Gradle test or IDE run, archive these paths as CI artifacts: | Artifact | Path | When | |---|---|---| | IDE log | `build/idea-sandbox/log/idea.log` | After `runIde` or `testIdeUi` | -| LSP server log | `~/.xtc/logs/lsp-server.log` | After any LSP session | +| LSP server log | `~/.xtc/logs/lsp/server-*/server.log` | After any LSP session | | Test results (XML) | `build/test-results/test/` | After `test` | | Test report (HTML) | `build/reports/tests/test/` | After `test` | | Test sandbox log | `build/idea-sandbox-test/log/idea.log` | After platform tests | @@ -353,10 +344,10 @@ To watch logs while the IDE is running: tail -f lang/intellij-plugin/build/idea-sandbox/log/idea.log # LSP server log -tail -f ~/.xtc/logs/lsp-server.log +tail -f ~/.xtc/logs/lsp/server-*/server.log # Both, interleaved -tail -f lang/intellij-plugin/build/idea-sandbox/log/idea.log ~/.xtc/logs/lsp-server.log +tail -f lang/intellij-plugin/build/idea-sandbox/log/idea.log ~/.xtc/logs/lsp/server-*/server.log ``` --- @@ -729,6 +720,52 @@ The XTC plugin depends on three bundled/third-party plugins: ### LSP4IJ in Tests +Ordinary builds, `runIde` and the native playbook use the Marketplace version pinned in the +catalog (currently 0.21.0). To test an unpushed local repair, first run `prepareSandbox` in its checkout, then +pass the same explicit override to the XVM task: + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + --tests '*CompilerPlaybookTest.compilerPlaybook' \ + -Plsp4ijPlugin="$HOME/src/lsp4ij/build/idea-sandbox/IC-2024.2/plugins/lsp4ij" \ + -PintellijPlaybookCases=X130,X138,X139,X146,X162,X181,X185,X259 \ + -Plsp.adapter=compiler -PincludeBuildLang=true -PincludeBuildAttachLang=true --no-build-cache +``` + +`lsp4ijPlugin` must be an absolute path to the built plugin directory containing `lib/`. Gradle's +`localPlugin` dependency handles the compile/test sandbox; Starter copies that same directory +instead of downloading the Marketplace release. The native report records its path, descriptor +version and SHA-256 of sorted relative filenames and contents; startup verifies the loaded version. +The directory contents are declared task inputs, including when the snapshot version stays the +same. Omit the property to return to the released dependency. Using the built directory avoids +the Gradle plugin's temporary ZIP metadata extraction invalidating configuration-cache reuse. +No local path or snapshot is committed as a default, and no plugin is published by this task. + +The release build retains the Ecstasy compatibility bridges for LSP4IJ 0.21.0. +An experiment against a locally repaired dependency does not justify removing them from the +published plugin. Validate each bridge independently, then repeat native acceptance with the +released dependency. Standalone formatting Undo/Redo and reconnection of closed dirty buffers +are separate requirements. See the [client limits](../doc/manual-test-plan.md#client-compatibility-limits). + +Readiness polling accepts a successful observation once, using a monotonic deadline. Pinned +Driver `waitFor` rechecks success and can falsely report a timeout during connection replacement +(UP29). `UiWaitsTest` covers this race, actual expiry, pending reads, cancellation and null values; +`PlaybookFailureTest` retains fatal timeout/cancellation behavior. These tests run without opening +an IDE through `testCompilerPlaybook --tests '*UiWaitsTest' --tests '*PlaybookFailureTest'`. +The wrapper only retries observations; edit/refactoring actions remain outside polling loops. + +Multi-target navigation chooses the visible Show Usages table through its locally registered +Enter callback, once. Driver's `popup.keyboard` sends a global key rather than addressing the +popup, which left X36's visible chooser unsubmitted in a full run. The helper checks row/action +validity, then native acceptance still requires closure and exact file/caret destinations. +`NavigationChooserTest` covers the binding/selection guards without opening an IDE. This is a +harness input correction, not a production LSP4IJ workaround or a replacement protocol request. + +X147's controlled asynchronous report probe selects **Build import** through the settings +navigation list before reading `xtc.compiler.details`. Unselected pages are detached from the +component tree. Keep its EDT publication barriers and stale-response assertions when changing +settings layouts; selecting the page does not replace the production report implementation. + LSP4IJ is the most complex dependency. For platform tests, you need it on the test classpath so that the `lsp4ij:server` and `lsp4ij:fileNamePatternMapping` extensions in `plugin.xml` resolve correctly. @@ -822,3 +859,68 @@ All testing and development tasks for the IntelliJ plugin, runnable from the pro - [Integration Tests for Plugin Developers (Feb 2025)](https://blog.jetbrains.com/platform/2025/02/integration-tests-for-plugin-developers-intro-dependencies-and-first-integration-test/) - [Integration Tests: API Interaction (Mar 2025)](https://blog.jetbrains.com/platform/2025/03/integration-tests-for-plugin-developers-api-interaction/) - [IntelliJ Platform SDK — IDE Development Instance](https://plugins.jetbrains.com/docs/intellij/ide-development-instance.html) + +### L82 refresh and VFS regression selection + +X146 additionally keeps 16 extra documents connected and schedules 64 lens, hint and semantic-token +refreshes each while a write action delays their read passes. The test temporarily raises the sandbox +IDE tab limit and restores it afterward. The untouched consumer must still show Int, then String, +then Int hints. X259 covers hint settings and connection retirement during restart. Hints stay enabled. + +The refresh and move selection is X103/X118/X130/X146/X161/X162/X163/X169/ +X173/X174/X175/X176/X216/X217/X218/X259. Keep the full-suite IDE-error gate; +do not suppress upstream alerts. `PreflightedRenamesTest` also tests the production launcher +with an unset optional class loader. + +Compiler workspace synchronization now has shared X263–X265 cases for nested composite input +aggregation, automatic Gradle refresh/export watching, and project/folder closure during import. +Both clients preserve explicit settings and the last accepted inputs after failed refresh. +Late daemon publication after +cancellation is guarded by import identity; the configuration cache is reused across identities. +Automatic exports require an initial imported report; VS Code uses public Gradle task events in +trusted local workspaces. + +Ordered library settings add X266–X268. The driver operates the installed Compiler settings +components, checks cancel/reset/apply and ordering, opens the compiler-selected read-only source +snapshot, restarts the server, and checks invalid-input retention and library removal/restoration. +The shared `prepareLibraryPlaybook` task produces two versions of a binary and matching sources; +neither editor bundles test binaries in source control. Use +`-PintellijPlaybookCases=X266,X267,X268` for this selection. The new URI round-trip regression +keeps case-sensitive module names in the virtual URI path because hosts normalize URI authorities. +See the [acceptance reports](../doc/manual-test-plan.md#acceptance-reports) +for native results; a successful fallback-file check does not close UP25's virtual-editor gap. + +### Machine-local JVM tuning and log support + +Open **Settings → Ecstasy Server Runtime and Logs**. Enter one supported JVM option per line +(e.g. `-Xmx2G`). Apply saves locally, outside project files and Settings Sync; the explicit Restart +button restarts running Ecstasy servers in this IDE using the saved settings. + +Retention defaults: 7 days, 10 MB/file, 50 MB archived per stream and five stopped-server sessions. +Logs live under `~/.xtc/logs/lsp/server--/`; active files are additional to archive +caps, active processes are protected and stopped sessions are pruned on the next server start. +Legacy shared logs and the IDE's own protocol console are outside this policy. + +**Tools → Export Ecstasy Server Logs** saves a ZIP of recent log tails and current status from +the connected server (at most eight 512 KiB tails, 4 MiB log input). It may contain local paths and +logged diagnostics; it does not collect source files. When stopped, export uses only this project’s +last recorded launch, including up to 64 KiB of launcher output. Its manifest identifies offline +mode and truncation and lists included files; another project’s logs are never substituted. Shared +X269–X272 cover settings, restart, live/offline export and recovery from an intentional failed JVM +launch. Full editor exit/reopen has a separate two-process persistence test. Native OS save-dialog +layout and rollover stress remain manual checks. See the [acceptance report format](../doc/manual-test-plan.md#acceptance-reports) for +how these checks are recorded. + +The UI completion gate includes `--tests '*CompilerPlaybookTest.settingsPersistence'` on +`testCompilerPlaybook`, with the usual compiler adapter and both lang inclusion flags. It starts +and completely exits two IDE processes using one disposable project/profile. See the +[settings acceptance steps](../doc/manual-test-plan.md#settings-persistence-and-layout-acceptance-ui1ui7) +for exact commands and layout checks. + +X276 checks compiler-resolved annotations, aliases, shadowing and bundled ownership, including +installed decorator markup. X277 checks lexical string/escape/keyword styles against controls, +absence of broad semantic overlays and exact damage/repair recovery. It explicitly requires +TextMate token types, rejecting a plain-text lexical fallback. Select either by its stable +X ID. Normal runs retain the current color scheme. Selected X41/X154/X276/X277 and the final X277 +regression run pass with zero IDE failures; see the +[acceptance reports](../doc/manual-test-plan.md#acceptance-reports). diff --git a/lang/intellij-plugin/build.gradle.kts b/lang/intellij-plugin/build.gradle.kts index 5c61b74fc9f..a52f1d9b831 100644 --- a/lang/intellij-plugin/build.gradle.kts +++ b/lang/intellij-plugin/build.gradle.kts @@ -7,6 +7,7 @@ import org.gradle.api.tasks.Input import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.TaskAction import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType +import org.jetbrains.intellij.platform.gradle.TestFrameworkType import org.jetbrains.intellij.platform.gradle.models.ProductRelease import org.jetbrains.intellij.platform.gradle.tasks.VerifyPluginTask import java.io.File @@ -37,6 +38,14 @@ plugins { val xdkVersion: String = project.version.toString() val releaseChannel: String = xdkProperties.stringValue("xdk.intellij.release.channel", "alpha") +// Explicit development override; Marketplace remains the default for ordinary builds. +val localLsp4ijPlugin = + providers.gradleProperty("lsp4ijPlugin").map { path -> + require(File(path).isAbsolute) { "lsp4ijPlugin must be an absolute path to a built plugin directory" } + require(File(path, "lib").isDirectory) { "lsp4ijPlugin must name a built plugin directory containing lib/: $path" } + File(path) + } + // Publishing is disabled by default. Enable with: ./gradlew publishPlugin -PenablePublish=true val enablePublish = providers.gradleProperty("enablePublish").map { it.toBoolean() }.getOrElse(false) val enablePublishProvider = providers.gradleProperty("enablePublish").map { it.toBoolean() }.orElse(false) @@ -276,9 +285,10 @@ sourceSets.main { } val processResources = - tasks.named("processResources") { + tasks.named("processResources") { dependsOn(syncGradleWrapperResources) dependsOn(copyLspVersionProperties) + from(rootProject.layout.projectDirectory.file("gradle/compiler-model.init.gradle")) } // ============================================================================= @@ -325,6 +335,17 @@ val lspVersionProperties = } } +val integrationTestSourceSet = + sourceSets.create("integrationTest") { + compileClasspath += sourceSets.main.get().output + // IDE-side probes compile against the same platform/plugin APIs as production. + compileClasspath += sourceSets.main.get().compileClasspath + runtimeClasspath += sourceSets.main.get().output + } + +configurations[integrationTestSourceSet.implementationConfigurationName].extendsFrom(configurations.testImplementation.get()) +configurations[integrationTestSourceSet.runtimeOnlyConfigurationName].extendsFrom(configurations.testRuntimeOnly.get()) + dependencies { // Test dependencies testImplementation(platform(libs.junit.bom)) @@ -332,6 +353,16 @@ dependencies { testRuntimeOnly(libs.junit.platform.launcher) testRuntimeOnly(libs.lang.intellij.junit4.compat) testImplementation(libs.assertj) + add(integrationTestSourceSet.implementationConfigurationName, libs.lang.intellij.kodein) + add(integrationTestSourceSet.implementationConfigurationName, libs.lang.intellij.coroutines) + // The standalone Starter JVM has no IDE classloader to supply Kotlin's standard library. + add(integrationTestSourceSet.runtimeOnlyConfigurationName, kotlin("stdlib")) + // Starter 262 reports run metadata through this API even for local, non-TeamCity runs. + add(integrationTestSourceSet.runtimeOnlyConfigurationName, libs.lang.intellij.service.messages) + // Driver-side protocol assertions use named error codes outside the IDE/plugin classloader. + add(integrationTestSourceSet.runtimeOnlyConfigurationName, libs.lang.lsp4j.jsonrpc) + // Protocol enums in the external playbook driver are not loaded through the IDE plugin loader. + add(integrationTestSourceSet.runtimeOnlyConfigurationName, libs.lang.lsp4j) // LSP server fat JAR for out-of-process execution lspServerJar(project(path = ":lsp-server", configuration = "lspServerElements")) @@ -351,12 +382,17 @@ dependencies { bundledPlugin("com.intellij.java") bundledPlugin("com.intellij.gradle") bundledPlugin("org.jetbrains.plugins.textmate") - plugin( - "com.redhat.devtools.lsp4ij", - libs.versions.lang.intellij.lsp4ij - .get(), - ) + if (localLsp4ijPlugin.isPresent) { + localPlugin(localLsp4ijPlugin) + } else { + plugin( + "com.redhat.devtools.lsp4ij", + libs.versions.lang.intellij.lsp4ij + .get(), + ) + } pluginVerifier() + testFramework(TestFrameworkType.Starter, configurationName = integrationTestSourceSet.implementationConfigurationName) } textMateGrammar(project(path = ":dsl", configuration = "textMateElements")) @@ -740,10 +776,16 @@ val parentPublishLocal = val runIdeCapturedIdeVersion = ideVersion val runIdeCapturedSinceBuild = intellijSinceBuild val runIdeCapturedLsp4ijVersion = - libs.versions.lang.intellij.lsp4ij - .get() + localLsp4ijPlugin.map { "local plugin: $it" }.orElse( + libs.versions.lang.intellij.lsp4ij + .get(), + ) val runIdeCapturedPluginVersion = project.version.toString() val runIdeCapturedSemanticTokens = ideLspSemanticTokens +val runIdeLspLogDirectory = + providers + .environmentVariable("XTC_LSP_LOG_DIR") + .orElse(providers.systemProperty("user.home").map { "$it/.xtc/logs/lsp" }) val runIdeInfo = tasks.register("runIdeInfo") { @@ -775,18 +817,19 @@ val runIdeInfo = ?.sorted() ?: emptyList() }, ) - lspLogFile.set(layout.file(providers.systemProperty("user.home").map { File(it, ".xtc/logs/lsp-server.log") })) + lspLogDirectory.set(runIdeLspLogDirectory) } val startLspLogTail = tasks.register("startLspLogTail") { - logFile.set(layout.file(providers.systemProperty("user.home").map { File(it, ".xtc/logs/lsp-server.log") })) + logDirectory.set(runIdeLspLogDirectory) threadName.set("lsp-log-tailer") linePrefix.set("[lsp-server] ") } val stopLspLogTail = tasks.register("stopLspLogTail") { + mustRunAfter(startLspLogTail) threadName.set("lsp-log-tailer") } @@ -851,3 +894,70 @@ val test = events("failed") } } + +val pruneCompilerPlaybookReports = + tasks.register("pruneCompilerPlaybookReports") { + group = "verification" + description = "Retain five completed playbook payloads and all compact results" + reportsDirectory.set(layout.buildDirectory.dir("reports/compiler-playbook")) + retainedRuns.set(providers.gradleProperty("playbookRetainedRuns").map(String::toInt).orElse(5)) + } + +// Launch the packaged plugin in an isolated IDE using JetBrains' Starter/Driver test task. +// This suite is opt-in; ordinary plugin tests do not open an IDE window. +intellijPlatformTesting.testIdeUi.register("testCompilerPlaybook") { + task { + description = "Run the compiler playbook's IntelliJ acceptance cases in an isolated IDE" + dependsOn(":lsp-server:prepareLibraryPlaybook") + finalizedBy(pruneCompilerPlaybookReports) + // Starter uses JNA for native process/window integration in this test JVM. + jvmArgs("--enable-native-access=ALL-UNNAMED") + testClassesDirs = integrationTestSourceSet.output.classesDirs + classpath = integrationTestSourceSet.runtimeClasspath + // IDE-side probes load only inside the sandbox plugin classloader, not Starter's JVM. + include("**/*Test.class") + useJUnitPlatform() + systemProperty("xtc.playbook.ideVersion", ideVersion) + localLsp4ijPlugin.orNull?.let { pluginDirectory -> + inputs.dir(pluginDirectory).withPropertyName("localLsp4ijPlugin") + systemProperty("xtc.playbook.lsp4ijPlugin", pluginDirectory.absolutePath) + } + systemProperty( + "xtc.playbook.lsp4ijVersion", + libs.versions.lang.intellij.lsp4ij + .get(), + ) + systemProperty("xtc.playbook.adapter", xdkProperties.stringValue("lsp.adapter", "compiler")) + systemProperty("xtc.playbook.cases", providers.gradleProperty("intellijPlaybookCases").getOrElse("")) + systemProperty("xtc.playbook.replacementGates", providers.gradleProperty("intellijPlaybookReplacementGates").getOrElse("")) + systemProperty( + "xtc.playbook.largeFileProbe", + providers.gradleProperty("intellijLargeFileProbe").getOrElse("false"), + ) + systemProperty( + "allure.results.directory", + layout.buildDirectory + .dir("reports/compiler-playbook/allure-results") + .get() + .asFile.absolutePath, + ) + val scenarios = rootProject.layout.projectDirectory.file("test-fixtures/compiler-playbook/scenarios.json") + inputs.file(scenarios) + systemProperty("xtc.playbook.scenarios", scenarios.asFile.absolutePath) + inputs.file(rootProject.layout.projectDirectory.file("doc/manual-test-plan.md")) + systemProperty( + "xtc.playbook.manual", + rootProject.layout.projectDirectory + .file("doc/manual-test-plan.md") + .asFile.absolutePath, + ) + systemProperty( + "xtc.playbook.reports", + layout.buildDirectory + .dir("reports/compiler-playbook") + .get() + .asFile.absolutePath, + ) + testLogging.events("passed", "skipped", "failed") + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CachedIdeInstaller.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CachedIdeInstaller.kt new file mode 100644 index 00000000000..0a56a89428b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CachedIdeInstaller.kt @@ -0,0 +1,55 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonParser +import com.intellij.ide.starter.di.di +import com.intellij.ide.starter.ide.IdeDistributionFactory +import com.intellij.ide.starter.ide.IdeInstaller +import com.intellij.ide.starter.ide.InstalledIde +import com.intellij.ide.starter.models.IdeInfo +import com.intellij.ide.starter.path.GlobalPaths +import org.kodein.di.direct +import org.kodein.di.instance +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path + +/** Reuse a matching native installation before asking Starter to download an installer archive. */ +internal class CachedIdeInstaller( + private val fallback: IdeInstaller, +) : IdeInstaller { + override suspend fun install(ideInfo: IdeInfo): Pair { + val builds = GlobalPaths.instance.getLocalCacheDirectoryFor("builds") + val cached = + Files.list(builds).use { paths -> + paths + .filter { Files.isDirectory(it, NOFOLLOW_LINKS) } + .filter { matches(it, ideInfo) } + .sorted() + .findFirst() + } + if (cached.isEmpty) return fallback.install(ideInfo) + val ide = di.direct.instance().installIDE(cached.get(), ideInfo.executableFileName) + println("Reusing native IntelliJ installation: ${ide.installationPath} (${ide.build})") + return ide.build to ide + } + + private fun matches( + directory: Path, + info: IdeInfo, + ): Boolean = + Files + .find(directory, 4, { path, attributes -> + attributes.isRegularFile && path.fileName.toString() == "product-info.json" + }) + .use { files -> + files.anyMatch { file -> + // Missing or invalid product metadata cannot establish a matching installation. + runCatching { + val product = JsonParser.parseString(Files.readString(file)).asJsonObject + product["productCode"].asString == info.productCode && + product["version"].asString == info.version && + directory.fileName.toString() == "${info.productCode}-${product["buildNumber"].asString}" + }.getOrDefault(false) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt new file mode 100644 index 00000000000..578d56b1871 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt @@ -0,0 +1,151 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.sdk.VirtualFile +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent + +/** + * Read what IntelliJ's language client already received; never issue a diagnostic request from the + * test. + */ +fun Driver.receivedDiagnostics(editor: JEditorUiComponent): List { + val path = editor.editor.getVirtualFile().getPath() + return service(singleProject()) + .getStartedServers() + .flatMap { it.getOpenedDocuments() } + .filter { it.getFile().getPath() == path } + .flatMap { it.getDiagnostics() } + .map { + val range = it.getRange() + ReceivedDiagnostic( + it.getCode()?.let { code -> code.getLeft() ?: code.getRight()?.toString() }, + it.getSource(), + it.getSeverity()?.name(), + range.getStart().let { position -> position.getLine() to position.getCharacter() }, + range.getEnd().let { position -> position.getLine() to position.getCharacter() }, + ) + } +} + +data class ReceivedDiagnostic( + val code: String?, + val source: String?, + val severity: String?, + val start: Pair, + val end: Pair, +) + +@Remote("com.redhat.devtools.lsp4ij.LanguageServiceAccessor", plugin = "com.redhat.devtools.lsp4ij") +interface LanguageClients { + // Driver transports collection results as lists even when the IDE method returns a Set. + fun getStartedServers(): Collection +} + +@Remote("com.redhat.devtools.lsp4ij.LanguageServerWrapper", plugin = "com.redhat.devtools.lsp4ij") +interface StartedLanguageServer { + fun getOpenedDocuments(): Collection + + fun getLanguageServer(): RemoteLanguageServer + + fun getServerStatus(): ClientServerStatus + + fun getServerCapabilitiesSync(): ClientValue + + fun stop(): ClientFuture + + fun stopAndDisable() + + fun restart() + + fun getCurrentProcessId(): Long? + + fun getTraces(): List + + fun getServerTrace(): ClientTraceLevel +} + +@Remote("com.redhat.devtools.lsp4ij.ServerStatus", plugin = "com.redhat.devtools.lsp4ij") +interface ClientServerStatus { + fun name(): String +} + +@Remote( + "com.redhat.devtools.lsp4ij.LanguageServerWrapper\$LSPTrace", + plugin = "com.redhat.devtools.lsp4ij", +) +interface ClientTraceEntry { + fun message(): ClientValue +} + +@Remote("com.redhat.devtools.lsp4ij.OpenedDocument", plugin = "com.redhat.devtools.lsp4ij") +interface ClientDocument { + fun getFile(): VirtualFile + + fun getDiagnostics(): Collection + + fun getSynchronizer(): DocumentSynchronizer? +} + +@Remote( + "com.redhat.devtools.lsp4ij.DocumentContentSynchronizer", + plugin = "com.redhat.devtools.lsp4ij", +) +interface DocumentSynchronizer { + fun getDidOpenFuture(): DocumentOpenFuture + + fun flushPendingChanges(): ClientFuture + + fun getDocument(): ParityDocument +} + +@Remote("java.util.concurrent.CompletableFuture") +interface DocumentOpenFuture { + fun isDone(): Boolean + + fun isCompletedExceptionally(): Boolean +} + +@Remote("org.eclipse.lsp4j.Diagnostic", plugin = "com.redhat.devtools.lsp4ij") +interface ClientDiagnostic { + fun getCode(): DiagnosticCode? + + fun getSource(): String? + + fun getSeverity(): DiagnosticSeverity? + + fun getRange(): SourceRange +} + +@Remote("org.eclipse.lsp4j.jsonrpc.messages.Either", plugin = "com.redhat.devtools.lsp4ij") +interface DiagnosticCode { + fun getLeft(): String? + + fun getRight(): Int? +} + +@Remote("org.eclipse.lsp4j.DiagnosticSeverity", plugin = "com.redhat.devtools.lsp4ij") +interface DiagnosticSeverity { + fun name(): String +} + +@Remote("org.eclipse.lsp4j.Range", plugin = "com.redhat.devtools.lsp4ij") +interface SourceRange { + fun getStart(): SourcePosition + + fun getEnd(): SourcePosition +} + +@Remote("org.eclipse.lsp4j.Position", plugin = "com.redhat.devtools.lsp4ij") +interface SourcePosition { + fun getLine(): Int + + fun getCharacter(): Int +} + +@Remote("com.intellij.openapi.editor.Document") +interface DocumentVersion { + fun getModificationStamp(): Long +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt new file mode 100644 index 00000000000..d31c5293ec2 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt @@ -0,0 +1,216 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonArray +import com.google.gson.JsonElement +import com.google.gson.JsonNull +import com.google.gson.JsonParser +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.impl.RefWrapper +import com.intellij.driver.client.service +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import java.util.concurrent.CancellationException +import kotlin.time.Duration.Companion.seconds + +/** Protocol assertions use the installed client's existing connection, never a second server. */ +class ClientProtocol( + private val driver: Driver, + private val project: () -> Project = { driver.singleProject() }, +) { + private val gson = Gson() + + fun server(): StartedLanguageServer = + with(driver) { + awaitUi( + message = "installed language client is started", + timeout = 45.seconds, + getter = { + service(project()).getStartedServers().toList() + }, + checker = { servers -> + servers.singleOrNull()?.getServerStatus()?.name() == "started" + }, + ).single() + } + + fun capabilities(): JsonElement = copy(server().getServerCapabilitiesSync()) + + fun request( + method: String, + params: Any, + ): ClientFuture = + with(driver) { + cast(server().getLanguageServer(), ClientEndpoint::class) + .request(method, remoteJson(params)) + } + + fun notify( + method: String, + params: Any, + ) = with(driver) { + cast(server().getLanguageServer(), ClientEndpoint::class) + .notify(method, remoteJson(params)) + } + + fun query( + method: String, + params: Any, + ): JsonElement = await(method, request(method, params)) + + fun await( + method: String, + future: ClientFuture, + ): JsonElement = + with(driver) { + awaitUi("installed client response: $method", 60.seconds) { future.isDone() } + if (future.isCancelled()) throw CancellationException("$method was cancelled") + if (future.isCompletedExceptionally()) { + // Throw locally: ResponseError is not Java-serializable, so throwing it through + // Driver's JMX transport would hide the actual protocol failure. + val failure = generateSequence(future.exceptionNow()) { it.getCause() }.last() + val code = + if ( + (failure as RefWrapper).getRef().className == + "org.eclipse.lsp4j.jsonrpc.ResponseErrorException" + ) { + cast(failure, ClientResponseFailure::class).getResponseError().getCode() + } else { + null + } + throw ClientRequestFailure(method, code, failure.getMessage()) + } + if (method in listResults) { + cast(future, ClientListFuture::class).get()?.let { values -> + JsonArray().apply { values.forEach { add(copy(it)) } } + } ?: JsonNull.INSTANCE + } else { + copy(cast(future, ClientObjectFuture::class).get()) + } + } + + fun copy(value: ClientValue?): JsonElement = + with(driver) { + if (value == null) { + JsonNull.INSTANCE + } else { + JsonParser.parseString(utility(ClientJsonTools::class).getLsp4jGson().toJson(value)) + } + } + + private fun remoteJson(value: Any): ClientJson = + with(driver) { + utility(ClientJsonParser::class) + .parseString(if (value is JsonElement) value.toString() else gson.toJson(value)) + } + + private companion object { + // Driver transports Java collection returns as reference lists; object/Either returns are + // individual references. Preserve that distinction before copying through LSP4IJ's Gson. + val listResults = + setOf( + "textDocument/codeAction", + "textDocument/codeLens", + "textDocument/documentColor", + "textDocument/colorPresentation", + "textDocument/documentLink", + "textDocument/references", + "textDocument/documentSymbol", + "textDocument/documentHighlight", + "textDocument/foldingRange", + "textDocument/selectionRange", + "textDocument/inlayHint", + "textDocument/moniker", + "textDocument/formatting", + "textDocument/rangesFormatting", + "textDocument/willSaveWaitUntil", + "textDocument/prepareTypeHierarchy", + "typeHierarchy/supertypes", + "typeHierarchy/subtypes", + "textDocument/prepareCallHierarchy", + "callHierarchy/incomingCalls", + "callHierarchy/outgoingCalls", + ) + } +} + +@Remote("org.eclipse.lsp4j.jsonrpc.Endpoint", plugin = "com.redhat.devtools.lsp4ij") +interface ClientEndpoint { + fun request( + method: String, + params: ClientJson, + ): ClientFuture + + fun notify( + method: String, + params: ClientJson, + ) +} + +@Remote("java.util.concurrent.CompletableFuture") +interface ClientFuture { + fun isDone(): Boolean + + fun isCancelled(): Boolean + + fun isCompletedExceptionally(): Boolean + + fun cancel(interrupt: Boolean): Boolean + + fun exceptionNow(): ClientFailure +} + +class ClientRequestFailure( + method: String, + val code: Int?, + message: String?, +) : RuntimeException("$method failed ($code): $message") + +@Remote("java.lang.Throwable") +interface ClientFailure { + fun getMessage(): String? + + fun getCause(): ClientFailure? +} + +@Remote("org.eclipse.lsp4j.jsonrpc.ResponseErrorException", plugin = "com.redhat.devtools.lsp4ij") +interface ClientResponseFailure { + fun getResponseError(): ClientResponseError +} + +@Remote("org.eclipse.lsp4j.jsonrpc.messages.ResponseError", plugin = "com.redhat.devtools.lsp4ij") +interface ClientResponseError { + fun getCode(): Int +} + +@Remote("java.util.concurrent.CompletableFuture") +interface ClientObjectFuture : ClientFuture { + fun get(): ClientValue? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface ClientListFuture : ClientFuture { + fun get(): List? +} + +@Remote("java.lang.Object") +interface ClientValue + +@Remote("com.google.gson.JsonElement", plugin = "com.redhat.devtools.lsp4ij") +interface ClientJson + +@Remote("com.google.gson.JsonParser", plugin = "com.redhat.devtools.lsp4ij") +interface ClientJsonParser { + fun parseString(json: String): ClientJson +} + +@Remote("com.redhat.devtools.lsp4ij.JSONUtils", plugin = "com.redhat.devtools.lsp4ij") +interface ClientJsonTools { + fun getLsp4jGson(): ClientGson +} + +@Remote("com.google.gson.Gson", plugin = "com.redhat.devtools.lsp4ij") +interface ClientGson { + fun toJson(value: ClientValue): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt new file mode 100644 index 00000000000..d95ea3226f6 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt @@ -0,0 +1,195 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.google.gson.JsonSyntaxException +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.singleProject +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** + * Read the installed client's verbose console. In particular, do not confuse an absent VFS + * diagnostic with an empty server publication: LSP4IJ drops publications for nonexistent files. + */ +class ClientTrace( + private val driver: Driver, +) { + /** Preserve native requests and replies before failure cleanup disposes the IDE console. */ + fun capture(path: Path) = + with(driver) { + val printed = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(TraceEditors::class).getInstance().getAllEditors().joinToString("\n") { + it.getDocument().getText() + } + } + val protocol = ClientProtocol(driver) + val queued = + // Failure cleanup may already have closed every document and stopped the server. + // Read available traces immediately; waiting for startup hides the original error. + service(singleProject()) + .getStartedServers() + .flatMap { it.getTraces().toList() } + .joinToString("\n") { protocol.copy(it.message()).toString() } + Files.writeString(path, "$printed\n$queued\n") + } + + fun notifications( + method: String, + received: Boolean = true, + ): List = messages(method, received, request = false) + + fun requests( + method: String, + received: Boolean = false, + ): List = messages(method, received, request = true) + + private fun messages( + method: String, + received: Boolean, + request: Boolean, + ): List = + with(driver) { + val direction = if (received) "Received" else "Sending" + val prefix = if (request) "$direction request '$method - (" else "$direction notification '$method'\n" + val printed = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(TraceEditors::class).getInstance().getAllEditors().flatMap { editor -> + editor.getDocument().getText().split("[Trace - ").mapNotNull { entry -> + if (!entry.contains(prefix)) return@mapNotNull null + val json = entry.substringAfter("\nParams: ").substringBefore("\n\n\n").trim() + try { + JsonParser + .parseString(json) + .takeIf { it.isJsonObject } + ?.asJsonObject + } catch (_: JsonSyntaxException) { + // Console output can be sampled before the final chunk is flushed. + null + } + } + } + } + val protocol = ClientProtocol(driver) + val queued = + protocol + .server() + .getTraces() + .map { protocol.copy(it.message()).asJsonObject } + .filter { it["method"]?.asString == method && (it["id"] != null) == request } + .map { it["params"].asJsonObject } + printed + queued + } + + fun diagnostics( + uri: String, + matches: (List) -> Boolean, + ): List = + with(driver) { + val protocol = ClientProtocol(driver) + val pull = protocol.capabilities().asJsonObject.has("diagnosticProvider") + awaitUi( + message = "diagnostic report for $uri", + timeout = 45.seconds, + getter = { + if (pull) { + // Closed/nonexistent files have no native editor diagnostic cache. Use + // the negotiated report on the installed connection, not a push log. + protocol + .query( + "textDocument/diagnostic", + mapOf("textDocument" to mapOf("uri" to uri)), + ).asJsonObject["items"] + ?.rows() + } else { + notifications("textDocument/publishDiagnostics") + .lastOrNull { sameUri(it.string("uri"), uri) } + ?.get("diagnostics") + ?.rows() + } + }, + checker = { it != null && matches(it) }, + )!! + } + + fun version( + uri: String, + text: String, + ): Int = + with(driver) { + awaitUi( + message = "client document version for $uri", + errorMessage = { + val protocol = ClientProtocol(driver) + val dump = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(TraceEditors::class).getInstance().getAllEditors().joinToString( + "\n--- EDITOR ---\n", + ) { + it.getDocument().getText() + } + } + val path = + Path + .of(System.getProperty("xtc.playbook.reports")) + .resolve("trace-debug.txt") + Files.writeString( + path, + "level=${protocol.server().getServerTrace().name()}\nexpected=$text\n$dump\nqueued=" + + protocol.server().getTraces().map { protocol.copy(it.message()) }, + ) + "Missing current client version; trace capture: $path" + }, + timeout = 15.seconds, + getter = { + ( + notifications("textDocument/didOpen", received = false).filter { + it["textDocument"].asJsonObject.string("text") == text + } + + notifications("textDocument/didChange", received = false).filter { + it["contentChanges"] + .rows() + .lastOrNull() + ?.get("text") + ?.asString == + text + } + ).map { it["textDocument"].asJsonObject } + .filter { sameUri(it.string("uri"), uri) } + .maxOfOrNull { it.int("version") } + }, + checker = { it != null }, + )!! + } + + private fun sameUri( + left: String, + right: String, + ): Boolean = URI(left) == URI(right) +} + +@Remote("com.redhat.devtools.lsp4ij.settings.ServerTrace", plugin = "com.redhat.devtools.lsp4ij") +interface ClientTraceLevel { + fun valueOf(name: String): ClientTraceLevel + + fun name(): String +} + +@Remote("com.intellij.openapi.editor.EditorFactory") +interface TraceEditors { + fun getInstance(): TraceEditors + + fun getAllEditors(): List +} + +@Remote("com.intellij.openapi.editor.Editor") +interface TraceEditor { + fun getDocument(): ParityDocument +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt new file mode 100644 index 00000000000..ac48347409b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt @@ -0,0 +1,1989 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.impl.DriverCallException +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.HIGHLIGHTING_PANEL_ID +import com.intellij.driver.sdk.ProblemsViewToolWindowUtils +import com.intellij.driver.sdk.findFile +import com.intellij.driver.sdk.getPlugin +import com.intellij.driver.sdk.getProblemsViewProblems +import com.intellij.driver.sdk.getToolWindow +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.isPluginLoaded +import com.intellij.driver.sdk.selectProblemsViewTab +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.common.codeEditorForFile +import com.intellij.driver.sdk.ui.components.common.ideFrame +import com.intellij.driver.sdk.waitForIndicators +import com.intellij.driver.sdk.waitForProblemsViewFile +import com.intellij.openapi.progress.ProcessCanceledException +import org.xtclang.idea.playbook.SharedScenarios.Companion.offset +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.minutes +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +enum class PlaybookMode { + FEATURES, + STARTUP, + FOCUS_RECOVERY, + LARGE_FILE, + PROJECT_LIFECYCLE, + SETTINGS_PERSISTENCE, +} + +/** Drive editor actions and inspect the diagnostics/lookup actually delivered to IntelliJ. */ +class CompilerPlaybook( + private val fixtures: Map, + private val shared: SharedScenarios, + private val lsp4ijVersion: String, + private val selectedIds: Set = shared.implementedIds, + private val mode: PlaybookMode = PlaybookMode.FEATURES, + private val onResult: (Result) -> Unit = {}, +) { + data class Result( + val id: String, + val description: String, + val status: String, + val milliseconds: Long, + val error: String? = null, + val limitations: List = emptyList(), + val manual: List = emptyList(), + ) + + private val completed = mutableListOf() + val results: List + get() = + completed.filter { it.id.startsWith("START") } + + shared.scenarios.map { (id, scenario) -> + val executed = completed.singleOrNull { it.id == id } + val status = + when { + executed?.status == "failed" -> "failed" + executed != null && scenario.intellij.coverage == "partial" -> "partial" + executed != null -> "passed" + scenario.intellij.coverage == "not-implemented" -> "not-implemented" + else -> "not-run" + } + Result( + id, + scenario.title, + status, + executed?.milliseconds ?: 0, + executed?.error, + scenario.intellij.limitations, + scenario.manual, + ) + } + + private fun Driver.assertTokenRanges(editor: JEditorUiComponent) { + val uri = Path.of(editor.editor.getVirtualFile().getPath()).toUri().toString() + val response = + ClientProtocol(this) + .query( + "textDocument/semanticTokens/full", + mapOf("textDocument" to mapOf("uri" to uri)), + ) + if (response.isJsonNull) return // A failed parse may produce no semantic report. + val data = response.asJsonObject["data"].asJsonArray.map { it.asInt } + data.chunked(5).runningFold(0 to 0) { (column, previousEnd), token -> + val next = if (token[0] == 0) column + token[1] else token[1] + check(token[0] > 0 || next >= previousEnd) { + "Overlapping semantic tokens at column $next" + } + check(token[2] > 0) + next to next + token[2] + } + } + + fun run( + driver: Driver, + persistencePhase: Int = 0, + ) { + with(driver) { + case(if (persistencePhase == 0) "START" else "START_REOPEN", "Packaged XTC and pinned LSP4IJ load") { + waitForIndicators(2.minutes) + withContext(OnDispatcher.EDT) { + utility(PlaybookProgress::class).install(singleProject()) + } + check(getPlugin("org.xtclang.idea")?.isEnabled() == true) + check(getPlugin("com.intellij.modules.ultimate")?.isEnabled() != true) { + "The playbook must run with Ultimate features disabled" + } + val lsp = requireNotNull(getPlugin("com.redhat.devtools.lsp4ij")) + check(lsp.isEnabled() && lsp.getVersion() == lsp4ijVersion) + } + if (mode == PlaybookMode.STARTUP) { + case( + "STARTUP", + "Edits, bulk replacement and close/reopen before server initialization", + ) { + startupEditing(fixtures, shared) + } + return@with + } + if (mode == PlaybookMode.FOCUS_RECOVERY) { + case( + "START_FOCUS", + "Restore interrupted popups without replaying completed edits", + ) { + focusRecovery(fixtures, shared) + } + return@with + } + if (mode == PlaybookMode.LARGE_FILE) { + case("START_LARGE_FILE", "Measure large-document range-marker updates") { + largeFileEditing(fixtures, shared) + } + return@with + } + if (mode == PlaybookMode.PROJECT_LIFECYCLE) { + case("START_PROJECTS", "Two project windows, pending close and reopen") { + projectLifecycle(shared) + } + return@with + } + if (mode == PlaybookMode.SETTINGS_PERSISTENCE) { + case("START_SETTINGS_$persistencePhase", "Settings survive a complete IDE exit and reopen") { + settingsPersistence(shared, persistencePhase) + } + return@with + } + configure(shared.graph) + withContext(OnDispatcher.EDT) { + getToolWindow("Language Servers").show() + getToolWindow("Language Servers").hide() + } + case( + shared.dependencyNavigation.id, + "Initial compiler configuration and cross-module definition", + ) { + val scenario = shared.dependencyNavigation + configure(shared.graph) + val consumer = open(scenario.file) + check( + service(singleProject()).getAllEditors().none { + it.getFile().getPath().endsWith("/${scenario.location.targetFile}") + }, + ) { + "The dependency must resolve before opening its source file" + } + navigate(consumer, scenario.location) + open(scenario.file).awaitDiagnostics(emptyList()) + } + case(shared.dependencyEdit.id, "Unsaved dependency edit recompiles its consumer") { + val scenario = shared.dependencyEdit + val consumer = open(scenario.consumer) + val version = cast(consumer.document, DocumentVersion::class).getModificationStamp() + val library = open(scenario.file) + val libraryPath = Path.of(library.editor.getVirtualFile().getPath()) + library.text = scenario.edit.apply(fixtures.getValue(scenario.file)) + val changed = open(scenario.consumer) + changed.awaitError() + awaitUi("consumer owns its type mismatch", 45.seconds) { + receivedDiagnostics(changed).let { items -> + items.isNotEmpty() && + items.none { + it.code == + shared.scenarios.getValue(scenario.id).text("diagnosticCode") + } + } + } + check( + cast(changed.document, DocumentVersion::class).getModificationStamp() == version, + ) { + "Dependency recompilation changed the consumer document" + } + check(Files.readString(libraryPath) == fixtures.getValue(scenario.file)) { + "The unsaved dependency edit was saved to disk" + } + open(scenario.file).text = fixtures.getValue(scenario.file) + open(scenario.consumer).awaitDiagnostics(emptyList()) + } + case( + shared.configuration.id, + "Clear and restore source graph through configuration notifications", + ) { + val consumer = open(shared.configuration.consumer) + configure("""{"xtc":{"compiler":{"sourceModules":[]}}}""") + consumer.awaitError() + configure(shared.graph) + consumer.awaitDiagnostics(emptyList()) + navigate(consumer, shared.dependencyNavigation.location) + } + case( + shared.diagnostics.id, + "Compiler error severity, source span and clearing without save", + ) { + val scenario = shared.diagnostics + val editor = open(scenario.file) + scenario.errors.forEach { expected -> + val text = expected.edit.apply(fixtures.getValue(scenario.file)) + editor.text = text + val range = offset(text, expected.rangeStart)..offset(text, expected.rangeEnd) + awaitUi("compiler diagnostic at shared source span", 45.seconds) { + editor.diagnostics().any { + it.severity == expected.severity && + it.start in range && + it.description.contains(expected.messageContains, ignoreCase = true) + } + } + awaitUi("compiler diagnostic codes, source, and nonempty ranges", 45.seconds) { + receivedDiagnostics(editor).let { items -> + items.isNotEmpty() && + items.all { + it.code?.startsWith(expected.codePrefix) == true && + it.source == "xtc" && + it.start.first > 0 && + it.start != it.end + } + } + } + problems(editor) + nextProblem(editor) + editor.text = fixtures.getValue(scenario.file) + editor.awaitDiagnostics(emptyList()) + problems(editor) + } + editor.text = fixtures.getValue(scenario.file) + scenario.cleanAppend + editor.awaitDiagnostics(emptyList()) + } + case( + shared.definitions.id, + "Shadowed local and property navigate to separate declarations", + ) { + val editor = open(shared.definitions.file) + shared.definitions.locations.forEach { navigate(editor, it) } + val uses = shared.definitions.locations.map { offset(editor.text, it.cursor) } + uses.forEach { at -> + val client = ClientProtocol(this) + val params = + mapOf( + "textDocument" to + mapOf( + "uri" to + Path + .of(editor.editor.getVirtualFile().getPath()) + .toUri() + .toString(), + ), + "position" to + mapOf( + "line" to editor.text.take(at).count { it == '\n' }, + "character" to (at - editor.text.lastIndexOf('\n', at - 1) - 1), + ), + ) + check( + client.query("textDocument/declaration", params) == + client.query("textDocument/definition", params), + ) { + "Local/property declarations must match their resolved definition" + } + } + uses.forEach { at -> referencesAndHighlights(editor, at, uses.single { it != at }) } + } + case( + shared.warning.id, + "Exactly one compiler warning for the duplicate inherited annotation", + ) { + val scenario = shared.warning + val editor = open(scenario.file) + awaitUi("one duplicate-annotation warning", 45.seconds) { + editor.diagnostics().let { values -> + values.size == scenario.count && + values.all { + it.severity == scenario.severity && + it.start == offset(editor.text, scenario.declaration) && + it.description.contains( + scenario.messageContains, + ignoreCase = true, + ) + } + } + } + problems(editor) + val data = shared.scenarios.getValue(scenario.id) + awaitUi("compiler warning metadata", 45.seconds) { + receivedDiagnostics(editor).let { items -> + items.size == scenario.count && + items.all { + it.code == scenario.code && + it.source == "xtc" && + it.severity == "Warning" && + it.start.first == it.end.first && + it.end.second - it.start.second == + data.values["warningCount"].asInt + } + } + } + nextProblem(editor) + val error = open(data.text("errorFile")) + error.text = + fixtures + .getValue(data.text("errorFile")) + .replace(data.text("replaceFrom"), data.text("replaceWith")) + error.awaitError() + val warning = open(scenario.file) + check(warning.diagnostics().size == scenario.count) + warning.text = scenario.edit.apply(warning.text) + warning.awaitDiagnostics(emptyList()) + problems(warning) + open(data.text("errorFile")).awaitError() + restore(data.text("errorFile")) + val restored = open(scenario.file) + restored.text = fixtures.getValue(scenario.file) + awaitUi("restored compiler warning", 45.seconds) { + receivedDiagnostics(restored).let { items -> + items.size == scenario.count && items.single().code == scenario.code + } + } + } + recoveryStructure() + case( + shared.completion.id, + "Compiler member completion replaces the typed prefix exactly", + ) { + val scenario = shared.completion + val editor = open(scenario.file) + val incomplete = scenario.edit.apply(fixtures.getValue(scenario.file)) + editor.text = incomplete + editor.awaitError() + accept(editor, offset(incomplete, scenario.cursor), scenario.label) + check(editor.text == scenario.accepted.apply(incomplete)) { + "Shared accepted completion text differs" + } + editor.awaitDiagnostics(emptyList()) + } + scenario("X6") { data -> + data.strings("bodies").forEach { body -> + val (editor, at) = editing(body) + lookup(editor, at) { items -> + hasType( + items, + data.text("label"), + data.values["type"].asJsonObject["source"].asString, + ) && items.none { it.getLookupString() == data.text("excludedLabel") } + } + invokeAction("EditorEscape", component = editor.component) + accept(editor, at, data.text("label")) + check(editor.text.contains(data.text("acceptedExpression"))) + editor.text = + editor.text.replace( + data.text("acceptedExpression"), + data.text("validStatement"), + ) + editor.awaitDiagnostics(emptyList()) + } + restore(shared.common.editing.file) + } + scenario("X8") { data -> + val (editor, at) = editing(data.text("body")) + accept(editor, at, data.text("label")) + check(editor.text.contains(data.text("acceptedSuffix"))) + editor.awaitDiagnostics(emptyList()) + restore(shared.common.editing.file) + } + for (id in listOf("X9", "X12")) { + scenario(id) { data -> + val (editor, at) = editing(data.text("body")) + lookup(editor, at) { items -> + val names = items.map { it.getLookupString() } + names.containsAll(data.strings("include")) && + data.strings("exclude").none { it in names } + } + invokeAction("EditorEscape", component = editor.component) + restore(shared.common.editing.file) + } + } + scenario("X10") { data -> + val (editor, at) = editing(data.text("body")) + accept(editor, at, data.text("label")) + restore(shared.common.editing.file) + } + scenario("X11") { data -> + val (first, firstAt) = editing(data.text("body"), inspect = true) + lookup(first, firstAt) { items -> + val names = items.map { it.getLookupString() } + names.containsAll(data.strings("include")) && + hasType( + items, + data.text("label"), + data.values["genericType"].asJsonObject["source"].asString, + ) + } + invokeAction("EditorEscape", component = first.component) + val (second, secondAt) = editing(data.text("shadowedBody"), inspect = true) + lookup(second, secondAt) { items -> + items.none { it.getLookupString() == data.text("excludedLabel") } && + hasType( + items, + data.text("label"), + data.values["shadowedType"].asJsonObject["source"].asString, + ) + } + invokeAction("EditorEscape", component = second.component) + restore(shared.common.editing.file) + } + scenario("X13") { data -> + data.rows("variants").forEach { variant -> + val body = SharedScenarios.text(data.text("body"), variant["prefix"].asString) + val (editor, at) = + editing(body) { text -> + text.replace( + data.text("moduleStart"), + SharedScenarios.text( + data.text("moduleWithImport"), + variant["addition"].asString, + ), + ) + } + accept(editor, at, variant["name"].asString) + } + restore(shared.common.editing.file) + } + signatureScenarios() + additionalScenarios() + workspaceScenarios() + for (id in listOf("X71", "X75")) { + scenario(id) { data -> + val editor = open(data.text("file")) + data.strings("variants").forEach { expression -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("replaceWith"), expression), + ) + if (id == "X75") editor.awaitError() + val at = + editor.text.indexOf(data.text("anchor")) + data.values["offset"].asInt + accept(editor, at, data.text("label")) + if (id == "X75") editor.awaitError() + } + restore(data.text("file")) + } + } + scenario("X86") { data -> + val editor = open(data.text("file")) + data.strings("variants").forEach { expression -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("replaceFrom"), expression) + editor.awaitError() + accept( + editor, + editor.text.indexOf(expression) + expression.length, + data.text("label"), + ) + editor.awaitError() + restore(data.text("file")) + } + } + scenario("X87") { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val prefix = variant["prefix"].asString + val suffix = variant["suffix"].asString + val selected = variant["selected"].asString + val pattern = Regex(data.values["replaceFrom"].asJsonObject["source"].asString) + val text = + pattern.replace(fixtures.getValue(data.text("file"))) { + SharedScenarios.text(data.text("replaceWith"), prefix, suffix) + } + editor.text = text + editor.awaitError() + accept(editor, text.indexOf(prefix) + prefix.length, selected) + editor.text = + text.replace( + prefix, + prefix.dropLast(data.values["prefixLength"].asInt) + + selected + + variant["terminator"].asString, + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + ParityScenarios(driver, fixtures, shared) { id, action -> + case( + id, + shared.scenarios.getValue(id).title, + continueAfterFailure = true, + action = action, + ) + }.run() + check(completed.filter { it.id != "START" }.map { it.id }.toSet() == selectedIds) { + "Every selected implemented case must execute; omitted cases remain not-run" + } + check(completed.none { it.status == "failed" }) { + "Failed IntelliJ cases: ${completed.filter { it.status == "failed" }.map { it.id }}; see results.json" + } + } + } + + private fun Driver.signatureScenarios() { + scenario("X15") { data -> + data.rows("variants").forEach { variant -> + val (editor, at) = + editing(SharedScenarios.text(data.text("body"), variant["argument"].asString)) + signature(editor, at) { items -> + items.size == data.values["expected"].asInt && + Regex(variant["type"].asString).containsMatchIn(items.single().label) && + items.single().activeParameter == data.values["expected"].asInt + } + } + restore(shared.common.editing.file) + } + scenario("X16") { data -> + val (editor, at) = editing(data.text("body")) + signature(editor, at) { items -> + items.size == data.values["expected"].asInt && + Regex(data.values["type"].asJsonObject["source"].asString) + .containsMatchIn(items.single().label) && + items.single().activeParameter == data.values["activeParameter"].asInt + } + editor.text = editor.text.replace(data.text("replaceFrom"), data.text("replaceWith")) + editor.awaitDiagnostics(emptyList()) + signature( + editor, + editor.text.indexOf(data.text("completedArgument")) + + data.values["argumentOffset"].asInt, + ) { + it.isNotEmpty() + } + definition( + editor, + editor.text.indexOf(data.text("call")) + data.values["callOffset"].asInt, + ) + restore(shared.common.editing.file) + } + scenario("X17") { data -> + data.rows("variants").forEach { variant -> + val (editor, at) = editing(variant["body"].asString) + val type = + Regex(SharedScenarios.text(data.text("typePattern"), variant["type"].asString)) + signature(editor, at) { it.isNotEmpty() && type.containsMatchIn(it.first().label) } + } + restore(shared.common.editing.file) + } + scenario("X18") { data -> + data.strings("bodies").forEach { body -> + val valid = shared.scenarios.getValue("X15") + val argument = valid.rows("variants").first()["argument"].asString + val (previous, previousAt) = + editing(SharedScenarios.text(valid.text("body"), argument)) + signature(previous, previousAt, keepOpen = true) { it.isNotEmpty() } + val (editor, at) = editing(body) + signature(editor, at) { it.size == data.values["signatureCount"].asInt } + } + restore(shared.common.editing.file) + } + scenario("X19") { data -> + data.values["receivers"].asJsonArray.forEach { receiver -> + val instance = receiver.asBoolean + val (editor, at) = + editing( + data.text(if (instance) "instanceBody" else "staticBody"), + inspect = instance, + ) { + it.replace(data.text("replaceFrom"), data.text("replaceWith")) + } + signature(editor, at) { + it.isNotEmpty() && + it.first().activeParameter == data.values["activeParameter"].asInt + } + editor.text = + editor.text.replaceRange( + at, + at, + data.text(if (instance) "instanceArgument" else "staticArgument"), + ) + editor.awaitDiagnostics(emptyList()) + } + restore(shared.common.editing.file) + } + scenario("X20") { data -> + val (editor, at) = editing(data.text("body")) + signature(editor, at) { it.isNotEmpty() && it.first().parameters.isEmpty() } + val targets = + data.strings("bodies").map { body -> + val (selected, _) = editing(body) + selected.awaitDiagnostics(emptyList()) + definition( + selected, + selected.text.indexOf(data.text("anchor")) + data.values["offset"].asInt, + ) + } + check(targets.distinct().size == targets.size) { + "Written argument types must select different overload declarations" + } + restore(shared.common.editing.file) + } + scenario("X74") { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val call = variant["call"].asString + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("replaceWith"), call), + ) + signature(editor, editor.text.indexOf(call) + call.length) { + it.isNotEmpty() && + it.first().label == data.text("signature") && + it.first().activeParameter == variant["active"].asInt + } + } + restore(data.text("file")) + } + for (id in listOf("X83", "X84")) { + scenario(id) { data -> + val editor = open(data.text("file")) + data.strings(if (id == "X84") "constructors" else "variants").forEach { original -> + val prefix = original.replace(data.text("replaceFrom"), data.text("prefix")) + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + original, + SharedScenarios.text(data.text("replaceWith"), prefix), + ) + editor.awaitError() + val at = editor.text.indexOf(prefix) + prefix.length + signature(editor, at) { + it.isNotEmpty() && + Regex(data.values["signature"].asJsonObject["source"].asString) + .containsMatchIn(it.first().label) && + ( + id != "X83" || + it.first().activeParameter == data.values["activeParameter"].asInt + ) + } + val labels = + data.strings( + if (id == "X83") { + "labels" + } else if (original.contains(data.text("requiredContext"))) { + "requiredLabels" + } else { + "provisionalLabels" + }, + ) + acceptCandidates( + editor, + at, + labels, + if (id == "X83") labels.single() else data.text("label"), + ) + check(editor.text == fixtures.getValue(data.text("file"))) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + } + scenario("X85") { data -> + val editor = open(data.text("file")) + data.strings("variants").forEach { prefix -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("replaceWith"), prefix), + ) + editor.awaitError() + val at = editor.text.indexOf(prefix) + prefix.length + signature(editor, at) { + it.isNotEmpty() && + it.first().activeParameter == data.values["activeParameter"].asInt && + Regex(data.values["parameter"].asJsonObject["source"].asString) + .containsMatchIn(it.first().label) + } + acceptCandidates( + editor, + at, + data.strings("labels"), + data.strings("labels").single(), + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + for (id in listOf("X88", "X89")) { + scenario(id) { data -> + val editor = open(data.text("file")) + val prefix = data.text("prefix") + val replacements = + if (id == "X89") { + data.strings("closers").map { prefix + it } + } else { + listOf(SharedScenarios.text(data.text("replaceWith"), prefix)) + } + replacements.forEach { replacement -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("original"), replacement) + editor.awaitError() + val at = editor.text.indexOf(prefix) + prefix.length + signature(editor, at) { + it.isNotEmpty() && + it.first().label == data.text("signature") && + it.first().activeParameter == data.values["activeParameter"].asInt + } + acceptCandidates( + editor, + at, + data.strings("labels"), + data.strings("labels").single(), + ) + if (id == "X89" && replacement == prefix) { + editor.awaitError() + } else { + check(editor.text == fixtures.getValue(data.text("file"))) + editor.awaitDiagnostics(emptyList()) + } + } + restore(data.text("file")) + } + } + scenario("X90") { data -> + val editor = open(data.text("file")) + val prefix = data.text("prefix") + val selected = data.strings("labels").single() + data.rows("variants").forEach { variant -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("original"), prefix + variant["suffix"].asString) + val at = editor.text.indexOf(prefix) + prefix.length + signature(editor, at) { + it.size == 1 && + it.single().label == data.text("signature") && + it.single().activeParameter == data.values["activeParameter"].asInt + } + acceptCandidates(editor, at, data.strings("labels"), selected) + if (variant["validAfterAcceptance"].asBoolean) { + editor.awaitDiagnostics(emptyList()) + } else { + awaitUi("missing array bracket remains a diagnostic", 30.seconds) { + editor.diagnostics().isNotEmpty() + } + } + val accepted = + prefix.dropLast(-data.values["replacementStartDelta"].asInt) + selected + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("original"), + accepted + variant["repairedSuffix"].asString, + ) + editor.awaitDiagnostics(emptyList()) + restore(data.text("file")) + } + } + scenario("X91") { data -> + val editor = open(data.text("file")) + data.strings("variants").forEach { variant -> + val marked = SharedScenarios.text(data.text("source"), variant) + val at = marked.indexOf('|') + editor.text = marked.replace("|", "") + editor.awaitError() + signature(editor, at) { it.isEmpty() } + lookup(editor, at) { items -> + val names = items.map { it.getLookupString() } + names.containsAll(data.strings("include")) && + data.strings("exclude").none { it in names } + } + invokeAction("EditorEscape", component = editor.component) + accept(editor, at, data.text("selected")) + check( + editor.text == + marked.take(at - data.values["prefixLength"].asInt) + + data.text("selected") + + marked.substring(at + 1), + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + scenario("X93") { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val marked = + SharedScenarios.text(data.text("source"), variant["declaration"].asString) + val at = marked.indexOf('|') + editor.text = marked.replace("|", "") + editor.awaitError() + signature(editor, at) { it.isEmpty() } + lookup(editor, at) { items -> + val names = items.map { it.getLookupString() } + names.containsAll(variant["include"].asJsonArray.map { it.asString }) && + data.strings("exclude").none { it in names } + } + invokeAction("EditorEscape", component = editor.component) + accept(editor, at, variant["selected"].asString) + check( + editor.text == + marked.take(at - data.values["prefixLength"].asInt) + + variant["selected"].asString + + marked.substring(at + 1), + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + listOf("X94", "X95", "X96", "X97", "X98", "X106", "X107", "X108").forEach { id -> + scenario(id) { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val marked = + SharedScenarios.text(data.text("source"), variant["declaration"].asString) + val at = marked.indexOf(data.text("marker")) + val before = variant["prefixLength"]?.asInt ?: data.values["prefixLength"].asInt + val after = variant["suffixLength"]?.asInt ?: 0 + editor.text = marked.replace(data.text("marker"), "") + if (variant["initiallyValid"]?.asBoolean == true) { + editor.awaitDiagnostics(emptyList()) + } else { + editor.awaitError() + } + signature( + editor, + at, + inspectDocumentation = variant["signatureDocumentationContains"] != null, + ) { signatures -> + ( + if (data.values["callContext"]?.asBoolean == true) { + signatures.isNotEmpty() + } else { + signatures.isEmpty() + } + ) && + ( + variant["activeParameter"] == null || + signatures.all { + it.activeParameter == variant["activeParameter"].asInt + } + ) && + ( + variant["signatureDocumentationContains"] == null || + signatures.any { + it.documentation?.contains( + variant["signatureDocumentationContains"].asString, + ) == true + } + ) + } + lookup(editor, at) { items -> + val names = items.map { it.getLookupString() } + names.containsAll(variant["include"].asJsonArray.map { it.asString }) && + ( + variant["exclude"]?.asJsonArray?.map { it.asString } + ?: data.strings("exclude") + ).none { it in names } && + ( + variant["metadata"] == null || + hasCompletionMetadata( + items, + variant["selected"].asString, + variant["metadata"].asJsonObject, + ) + ) + } + dismissPopups() + accept( + editor, + at, + variant["selected"].asString, + marked.take(at - before) + + variant["selected"].asString + + marked.substring(at + data.text("marker").length + after), + ) + if (variant["validAfterAcceptance"].asBoolean) { + editor.awaitDiagnostics(emptyList()) + } else { + editor.awaitError() + } + editor.text = + SharedScenarios.text( + data.text("source"), + variant["repairedDeclaration"].asString, + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + } + } + + private fun Driver.recoveryStructure() { + scenario("X92") { data -> + val editor = open(data.text("file")) + data.strings("parameters").forEach { parameters -> + editor.text = SharedScenarios.text(data.text("source"), parameters) + editor.awaitError() + problems(editor) + structure(editor, data.strings("symbols"), listOf(data.text("excludedSymbol"))) + val expected = data.values["foldStart"].asInt..data.values["foldEnd"].asInt + awaitUi( + message = "unfinished declaration body fold $expected", + errorMessage = { "Expected native fold $expected; actual: ${folds(editor)}" }, + timeout = 45.seconds, + ) { + expected in folds(editor) + } + editor.text = + SharedScenarios.text(data.text("source"), data.text("repairedParameters")) + editor.awaitDiagnostics(emptyList()) + problems(editor) + } + restore(data.text("file")) + } + } + + private fun Driver.additionalScenarios() { + scenario("X34") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + declaration( + definition( + editor, + editor.text.lastIndexOf(data.text("narrowedUse")) + + data.values["narrowedOffset"].asInt, + "LSP.GotoTypeDefinition", + ), + data.strings("narrowedTargets").single(), + ) + val reopened = open(data.text("file")) + focusEditor(reopened) + caret( + reopened, + reopened.text.indexOf(data.text("unionUse")) + data.values["unionOffset"].asInt, + ) + invokeAction("LSP.GotoTypeDefinition", component = reopened.component) + val expected = data.strings("unionTargets") + chooseTargets(reopened, expected, expected.last()) { + invokeAction("LSP.GotoTypeDefinition", component = reopened.component) + } + awaitUi("selected union declaration opens", 30.seconds) { + withContext(OnDispatcher.EDT) { + service(singleProject()).getSelectedTextEditor()?.let { selected -> + reopened.text + .substring(selected.getCaretModel().getOffset()) + .startsWith(expected.last()) + } == true + } + } + } + scenario("X33") { data -> + data.rows("variants").forEach { variant -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + val target = + definition( + editor, + editor.text.indexOf(variant["anchor"].asString), + "LSP.GotoTypeDefinition", + ) + check(target.first.endsWith("/${data.text("file")}")) + declaration(target, variant["expected"].asString) + } + } + scenario("X35") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + val formal = + definition( + editor, + editor.text.indexOf(data.text("formalUse")) + data.values["offset"].asInt, + "LSP.GotoTypeDefinition", + ) + check(formal.first.endsWith("/${data.text("file")}")) + declaration(formal, data.strings("formalTargets").single()) + val reopened = open(data.text("file")) + val library = + definition( + reopened, + reopened.text.indexOf(data.text("binaryUse")), + "LSP.GotoTypeDefinition", + ) + libraryDeclaration(library, data.strings("binaryTargets").single()) + } + scenario("X101") { data -> + data.strings("types").forEach { name -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + val target = definition(editor, editor.text.indexOf(name)) + libraryDeclaration(target, name) + val library = + ideFrame().codeEditorForFile(Path.of(target.first).fileName.toString()) + val original = library.text + rejectFormatting(library) + check( + library.text == original && Files.readString(Path.of(target.first)) == original, + ) + check(!Files.isWritable(Path.of(target.first))) + val reopened = open(data.text("file")) + rejectRename(reopened, reopened.text.indexOf(name), data.text("renameRejection")) + } + val editor = open(data.text("file")) + libraryDeclaration( + definition( + editor, + editor.text.indexOf(data.text("variable")), + "LSP.GotoTypeDefinition", + ), + data.text("variableType"), + ) + } + scenario("X1") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + structure(editor, data.strings("symbolNames")) + awaitUi("native fold regions", 45.seconds) { + folds(editor).size >= data.values["minimumFolds"].asInt + } + selectionParents( + editor, + editor.text.indexOf(data.text("anchor")) + data.values["offset"].asInt, + ) + } + scenario("X70") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + val at = editor.text.indexOf(data.text("anchor")) + signature(editor, at + data.values["offset"].asInt) { + it.firstOrNull()?.label == data.text("signature") + } + val (_, target) = definition(editor, at) + check(editor.text.substring(target).startsWith(data.text("targetName"))) + } + scenario("X73") { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val call = variant["call"].asString + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("incompleteExpression"), call), + ) + val anchor = SharedScenarios.text(data.text("callAnchor"), call) + signature(editor, editor.text.indexOf(anchor) + anchor.length) { + it.firstOrNull()?.let { item -> + item.label == data.text("signature") && + item.activeParameter == variant["active"].asInt + } == true + } + } + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("replaceFrom"), data.text("invalidExpression")) + signature( + editor, + editor.text.indexOf(data.text("invalidCall")) + data.values["offset"].asInt, + ) { + it.size == data.values["rejectedSignatureCount"].asInt + } + restore(data.text("file")) + } + scenario("X76") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + if (data.values["tokensNonOverlapping"].asBoolean) assertTokenRanges(editor) + val expression = data.text("expression") + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("replaceWith"), expression), + ) + editor.awaitError() + if (data.values["tokensNonOverlapping"].asBoolean) assertTokenRanges(editor) + signature(editor, editor.text.indexOf(expression) + expression.length) { + it.firstOrNull()?.let { item -> + item.label == data.text("signature") && + item.activeParameter == data.values["activeParameter"].asInt + } == true + } + restore(data.text("file")) + } + for (id in listOf("X77", "X79")) { + scenario(id) { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + val prefix = variant["prefix"].asString + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + variant["original"].asString, + SharedScenarios.text( + data.text(if (id == "X77") "replaceWith" else "replaceFrom"), + prefix, + ), + ) + editor.awaitError() + val at = editor.text.lastIndexOf(prefix) + prefix.length + acceptCandidates( + editor, + at, + data.strings("labels"), + if (id == "X77") { + data.text("label") + } else { + data.strings("labels").single() + }, + ) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + } + for (id in listOf("X78", "X80")) { + scenario(id) { data -> + val editor = open(data.text("file")) + data.strings(if (id == "X78") "selections" else "labels").forEach { selected -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("replaceFrom"), data.text("anchor")) + editor.awaitError() + val at = editor.text.indexOf(data.text("anchor")) + data.values["offset"].asInt + signature(editor, at) { it.size == data.values["signatureCount"].asInt } + acceptCandidates(editor, at, data.strings("labels"), selected) + editor.awaitDiagnostics(emptyList()) + val acceptedAt = + at + + selected.length + + if (id == "X80") data.values["replacementStartDelta"].asInt else 0 + signature(editor, acceptedAt) { it.size == 1 } + } + restore(data.text("file")) + } + } + scenario("X81") { data -> + val editor = open(data.text("file")) + data.strings("variants").forEach { prefix -> + editor.text = + fixtures + .getValue(data.text("file")) + .replace( + data.text("replaceFrom"), + SharedScenarios.text(data.text("replaceWith"), prefix), + ) + editor.awaitError() + val at = editor.text.indexOf(prefix) + prefix.length + lookup(editor, at) { items -> + items.map { it.getLookupString() }.sorted() == + data.strings("labels").sorted() && + hasType( + items, + data.text("label"), + data.values["type"].asJsonObject["source"].asString, + ) && + hasCompletionKinds(items, data.strings("labels"), 10) + } + invokeAction("EditorEscape", component = editor.component) + accept(editor, at, data.text("label")) + editor.awaitDiagnostics(emptyList()) + } + restore(data.text("file")) + } + scenario("X82") { data -> + val editor = open(data.text("file")) + editor.text = + fixtures + .getValue(data.text("file")) + .replace(data.text("replaceFrom"), data.text("anchor")) + editor.awaitError() + val at = editor.text.indexOf(data.text("anchor")) + data.values["offset"].asInt + acceptCandidates( + editor, + at, + data.strings("labels"), + data.strings("labels").single(), + kind = 10, + ) + check(editor.text == fixtures.getValue(data.text("file"))) + editor.awaitDiagnostics(emptyList()) + } + scenario("7a.9") { data -> + val editor = open(data.text("file")) + editor.text = + data.text("moduleStart") + + (0 until data.values["declarationCount"].asInt).joinToString("\n") { + SharedScenarios.text(data.text("declaration"), it.toString(), it.toString()) + } + + data.text("moduleEnd") + editor.awaitError() + awaitUi("bounded source diagnostics without an internal compiler failure", 45.seconds) { + receivedDiagnostics(editor).let { items -> + items.isNotEmpty() && + items.count { it.severity == "Error" } <= + data.values["maximumErrors"].asInt && + items.none { it.code == data.text("diagnosticCode") } + } + } + problems(editor) + editor.text = data.text("moduleStart") + data.text("moduleEnd") + editor.awaitDiagnostics(emptyList()) + } + } + + private fun Driver.nextProblem(editor: JEditorUiComponent) { + val positions = editor.diagnostics().map { it.start } + check(positions.isNotEmpty()) + caret(editor, 0) + invokeAction("GotoNextError", component = editor.component) + awaitUi("Next Problem moves to an actual compiler diagnostic", 15.seconds) { + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().getOffset() in positions } + } + // Next Problem also shows a diagnostic hint; dismiss it before another editor/tool-window + // action. + withContext(OnDispatcher.EDT) { service().hideAllHints() } + } + + private fun Driver.problems(editor: JEditorUiComponent) { + val text = editor.text + val expected = + editor + .diagnostics() + .map { diagnostic -> + check(diagnostic.start in 0..text.length) + val preceding = text.take(diagnostic.start) + preceding.count { it == '\n' } to + (diagnostic.start - preceding.lastIndexOf('\n') - 1) + }.sortedWith(compareBy({ it.first }, { it.second })) + val file = + withContext(OnDispatcher.EDT) { + requireNotNull(service(singleProject()).getSelectedTextEditor()) + .getVirtualFile() + } + val visible = + withContext(OnDispatcher.EDT) { + utility(ProblemsViewToolWindowUtils::class) + .getToolWindow(singleProject()) + ?.isVisible() == true + } + if (!visible) invokeAction("ActivateProblemsViewToolWindow", component = editor.component) + selectProblemsViewTab(HIGHLIGHTING_PANEL_ID) + awaitUi("Problems tool window is visible", 15.seconds) { + withContext(OnDispatcher.EDT) { + utility(ProblemsViewToolWindowUtils::class) + .getToolWindow(singleProject()) + ?.isVisible() == true + } + } + waitForProblemsViewFile(file) + awaitUi("Problems rows match editor diagnostic locations, including clearing", 45.seconds) { + getProblemsViewProblems(file) + .map { it.getLine() to it.getColumn() } + .sortedWith(compareBy({ it.first }, { it.second })) == expected + } + } + + private fun Driver.scenario( + id: String, + action: (SharedScenarios.Scenario) -> Unit, + ) { + val scenario = shared.scenarios.getValue(id) + case(id, scenario.title) { action(scenario) } + } + + private fun Driver.restore(file: String) { + val editor = open(file) + editor.text = fixtures.getValue(file) + editor.awaitDiagnostics(emptyList()) + } + + private fun Driver.editing( + body: String, + inspect: Boolean = false, + transform: (String) -> String = { it }, + ): Pair { + val setup = shared.common.editing + val anchor = if (inspect) setup.inspect else setup.run + val text = + transform(fixtures.getValue(setup.file)) + .replace(anchor, anchor.replace("{}", "{ $body }")) + val at = text.indexOf('§') + check(at >= 0 && text.indexOf('§', at + 1) < 0) { "Expected one cursor marker" } + val editor = open(setup.file) + editor.text = text.replace("§", "") + return editor to at + } + + private fun Driver.case( + id: String, + description: String, + continueAfterFailure: Boolean = false, + action: () -> Unit, + ) { + if (!id.startsWith("START") && id !in selectedIds) return + check(completed.none { it.id == id }) { "Duplicate native case $id" } + val start = TimeSource.Monotonic.markNow() + println("IntelliJ playbook $id: $description") + if (id != "START" && id != "START_REOPEN") progress(id, "running", description) + try { + action() + check(!isPluginLoaded("com.intellij.modules.ultimate")) { + "Ultimate became active during $id; this cannot establish Community support" + } + completed += + Result(id, description, "passed", start.elapsedNow().inWholeMilliseconds) + .also(onResult) + progress(id, "passed", description) + } catch (failure: Throwable) { + val unsafeToContinue = failure.mustStopPlaybook() + if (!unsafeToContinue) { + runCatching { + ClientTrace(this) + .capture( + Path + .of(singleProject().getBasePath()) + .parent + .resolve("client-trace-$id.log"), + ) + }.onFailure(failure::addSuppressed) + } + completed += + Result( + id, + description, + "failed", + start.elapsedNow().inWholeMilliseconds, + failure.stackTraceToString(), + ).also(onResult) + if (!unsafeToContinue) runCatching { progress(id, "failed", description) }.onFailure(failure::addSuppressed) + if ( + !continueAfterFailure || + unsafeToContinue || + (failure !is Exception && failure !is AssertionError) + ) { + throw failure + } + } + } + + private fun Driver.progress( + id: String, + status: String, + description: String, + ) { + val total = + when (mode) { + PlaybookMode.FEATURES -> selectedIds.size + PlaybookMode.SETTINGS_PERSISTENCE -> 2 + else -> 1 + } + val done = completed.count { it.id != "START" && it.id != "START_REOPEN" } + val failed = completed.count { it.status == "failed" } + val text = + "Ecstasy playbook: $done/$total completed, ${total - done} left | $id $status" + + if (failed > 0) " | $failed failed" else "" + val shortDescription = if (description.length > 80) description.take(77).trimEnd() + "…" else description + withContext(OnDispatcher.EDT) { + utility(PlaybookProgress::class).update(singleProject(), "$text — $shortDescription", "$text — $description") + } + } + + private fun Driver.workspaceScenarios() { + ((109..121).map { "X$it" } + listOf("X155", "X159", "X160", "X164", "X165", "X166", "X167", "X168", "X220")).forEach { id -> + scenario(id) { + discovered(id) { data -> + val projectSettings = data.values["projectSettingsRoundTrip"]?.asBoolean == true + try { + if (projectSettings) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class) + .installProjectGraph(singleProject()) + } + } + renameFamily(id, data, { open(it) }, { it.awaitDiagnostics(emptyList()) }) + } finally { + if (projectSettings) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class) + .clearProjectGraph(singleProject()) + } + } + } + } + } + } + scenario("X99") { + discovered("X99") { data -> + val consumer = open(data.text("file")) + consumer.text = data.text("changed") + consumer.awaitDiagnostics(emptyList()) + declaration( + definition(consumer, consumer.text.indexOf(data.text("anchor"))), + data.text("target"), + ) + val library = open(data.text("library")) + library.text = data.text("brokenLibrary") + open(data.text("file")).awaitError() + val discarded = open(data.text("library")) + check( + Files.readString(Path.of(discarded.editor.getVirtualFile().getPath())) == + data.text("libraryText"), + ) + discarded.text = data.text("libraryText") + withContext(OnDispatcher.EDT) { + service(singleProject()) + .closeFile(discarded.editor.getVirtualFile()) + } + val restored = open(data.text("file")) + restored.awaitDiagnostics(emptyList()) + check( + Files.readString(Path.of(restored.editor.getVirtualFile().getPath())) == + data.text("original"), + ) + restored.text = data.text("original") + } + } + scenario("X100") { + discovered("X100") { data -> + val neighbor = open(data.text("neighbor")) + neighbor.text = data.text("brokenNeighbor") + neighbor.awaitError() + val healthy = open(data.text("file")) + partialGraphHierarchy( + healthy, + healthy.text.indexOf(data.text("anchor")), + data.text("child"), + healthy.text.lastIndexOf(data.text("anchor")), + ) + val restored = open(data.text("neighbor")) + restored.text = data.text("neighborText") + restored.awaitDiagnostics(emptyList()) + } + } + listOf("X102", "X104").forEach { id -> + scenario(id) { + discovered(id) { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + rename( + editor, + editor.text.indexOf(data.text("anchor")), + data.text("replacement"), + ) + awaitUi("all source rename edits applied", 45.seconds) { + editor.text.split(data.text("replacement")).size - 1 == + data.values["edits"].asInt + } + editor.awaitDiagnostics(emptyList()) + data.values["preserved"]?.let { check(it.asString in editor.text) } + editor.text = data.text("source") + editor.awaitDiagnostics(emptyList()) + } + } + } + scenario("X103") { + discovered("X103") { data -> + val editor = open(data.text("file")) + editor.awaitDiagnostics(emptyList()) + val root = Path.of(editor.editor.getVirtualFile().getPath()).parent + rename(editor, editor.text.indexOf(data.text("anchor")), data.text("replacement")) + awaitUi("type rename moves the member file", 45.seconds) { + Files.exists(root.resolve(data.text("destination"))) && + !Files.exists(root.resolve(data.text("member"))) && + editor.text.contains(data.text("replacement")) + } + val moved = open("X103/${data.text("destination")}") + check( + moved.text == + data + .text("memberSource") + .replace(data.text("anchor"), data.text("replacement")), + ) + check( + Files.readString(root.resolve(data.text("companionDestination"))) == + data.text("companionSource"), + ) + check(!Files.exists(root.resolve(data.text("companion")))) + val reopened = open(data.text("file")) + reopened.awaitDiagnostics(emptyList()) + // Restore through the same native refactoring path, including the reverse move. + rename( + reopened, + reopened.text.indexOf(data.text("replacement")), + data.text("anchor"), + ) + awaitUi("reverse rename restores source and the member", 45.seconds) { + Files.exists(root.resolve(data.text("member"))) && + !Files.exists(root.resolve(data.text("destination"))) && + reopened.text == data.text("source") + } + reopened.awaitDiagnostics(emptyList()) + val restored = open("X103/${data.text("member")}") + check(restored.text == data.text("memberSource")) { + "Reverse rename must restore the member declaration in the open editor" + } + // Refactoring updates an open document; persistence must not depend on autosave. + withContext(OnDispatcher.EDT, semantics = LockSemantics.WRITE_ACTION) { + service() + .saveDocument(cast(restored.document, ParityDocument::class)) + } + check( + Files.readString(root.resolve(data.text("member"))) == data.text("memberSource"), + ) { "Saving the reverse rename must persist the restored member declaration" } + check( + Files.readString(root.resolve(data.text("companion"))) == + data.text("companionSource"), + ) + check(!Files.exists(root.resolve(data.text("companionDestination")))) + } + } + scenario("X123") { data -> + val editor = open(data.text("file")) + val protocol = ClientProtocol(this) + check(protocol.capabilities().asJsonObject.has("diagnosticProvider")) + val uri = Path.of(editor.editor.getVirtualFile().getPath()).toUri().toString() + val params = mapOf("textDocument" to mapOf("uri" to uri), "identifier" to "xtc") + try { + editor.text = data.text("broken") + editor.awaitError() + val first = protocol.query("textDocument/diagnostic", params).asJsonObject + check(first["kind"].asString == "full" && first["items"].asJsonArray.size() > 0) + val previous = params + ("previousResultId" to first["resultId"].asString) + check( + protocol + .query("textDocument/diagnostic", previous) + .asJsonObject["kind"] + .asString == "unchanged", + ) + editor.text = data.text("repaired") + editor.awaitDiagnostics(emptyList()) + val repaired = protocol.query("textDocument/diagnostic", previous).asJsonObject + check(repaired["kind"].asString == "full" && repaired["items"].asJsonArray.isEmpty) + check(repaired["resultId"] != first["resultId"]) + } finally { + restore(data.text("file")) + } + } + listOf( + "X148", + "X156", + "X157", + "X177", + "X178", + "X179", + "X180", + "X181", + "X182", + "X183", + "X184", + "X185", + "X186", + "X187", + "X188", + "X189", + "X190", + "X191", + "X192", + "X193", + "X194", + "X195", + "X196", + "X197", + "X198", + "X199", + "X200", + "X201", + "X202", + "X203", + "X204", + "X205", + "X206", + "X207", + "X208", + "X209", + "X210", + "X211", + "X212", + "X213", + "X214", + "X215", + "X221", + "X222", + "X223", + "X224", + "X225", + "X226", + "X227", + "X228", + "X229", + "X230", + "X231", + "X232", + "X233", + "X234", + "X235", + "X236", + "X237", + "X238", + "X239", + "X240", + "X241", + "X242", + ).forEach { id -> + scenario(id) { + discovered(id) { data -> + val editor = open(data.text("file")) + val original = data.text("source") + val companion = data.values.has("destinationFile") + check(editor.text == original) { "Fresh scenario source differs from shared fixture: $id" } + + fun callerDiagnostics(broken: Boolean) { + // The locator sees the selected tab. Dependency-file checks also switch + // tabs; selecting the caller never reapplies an edit or changes its text. + val caller = open(data.text("file")) + if (companion) check(caller.text == original) + if (broken) caller.awaitError() else caller.awaitDiagnostics(emptyList()) + } + + fun additionalFiles(applied: Boolean) { + if (!data.values.has("files")) return + data.rows("files").forEach { file -> + val target = open("$id/${file["file"].asString}") + val expected = file[if (applied && file.has("expected")) "expected" else "source"].asString + awaitUi("additional refactoring file ${file["file"].asString}", 45.seconds) { target.text == expected } + } + } + val initiallyBroken = data.values["initiallyValid"]?.asBoolean == false + callerDiagnostics(initiallyBroken) + val at = data.values["selectionOffset"]?.asInt ?: original.indexOf(data.text("selected")) + if (data.values["refused"]?.asBoolean == true) { + fun position(offset: Int): Map { + val prefix = original.take(offset) + return mapOf("line" to prefix.count { it == '\n' }, "character" to (offset - prefix.lastIndexOf('\n') - 1)) + } + val actions = + ClientProtocol(this) + .query( + "textDocument/codeAction", + mapOf( + "textDocument" to + mapOf( + "uri" to Path.of(editor.editor.getVirtualFile().getPath()).toUri().toString(), + ), + "range" to mapOf("start" to position(at), "end" to position(at + data.text("selected").length)), + "context" to mapOf("diagnostics" to emptyList()), + ), + ).asJsonArray + check(actions.none { it.asJsonObject["title"].asString == data.text("title") }) + check(editor.text == original) + return@discovered + } + // Diagnostic quick fixes use a caret at the error; expression refactorings + // use the complete selection specified by the shared scenario. + val selectionEnd = if (data.values["initiallyValid"]?.asBoolean == false) at else at + data.text("selected").length + quickFix(editor, at, data.text("title"), selectionEnd) + val destination = if (companion) open(data.text("destinationFile")) else editor + val destinationOriginal = if (companion) data.text("destinationSource") else original + awaitUi("local refactoring matches shared source", 45.seconds) { destination.text == data.text("expected") } + additionalFiles(true) + callerDiagnostics(false) + val history = + listOf("\$Undo" to destinationOriginal, "\$Redo" to data.text("expected"), "\$Undo" to destinationOriginal) + history.forEach { (action, expected) -> + val target = if (companion) open(data.text("destinationFile")) else editor + focusEditor(target) + invokeAction(action, now = false, component = target.component) + awaitUi("$action local refactoring", 45.seconds) { target.text == expected } + additionalFiles(action == "\$Redo") + callerDiagnostics(expected == destinationOriginal && initiallyBroken) + } + } + } + } + listOf("X149", "X150", "X151", "X152").forEach { id -> + scenario(id) { + discovered(id) { data -> + val editor = open(data.text("file")) + syntaxCompletions(data, editor) { editor.awaitDiagnostics(emptyList()) } + } + } + } + scenario("X122") { + discovered("X122") { data -> + memberActions(data, open(data.text("file"))) { editor, broken -> + if (broken) editor.awaitError() else editor.awaitDiagnostics(emptyList()) + } + } + } + scenario("X105") { + discovered("X105") { data -> + val editor = open(data.text("file")) + data.rows("variants").forEach { variant -> + editor.text = variant["source"].asString + editor.awaitError() + quickFix( + editor, + editor.text.indexOf(variant["anchor"].asString), + variant["title"].asString, + ) + awaitUi("native import quick fix applied", 45.seconds) { + variant["importText"].asString in editor.text + } + editor.awaitDiagnostics(emptyList()) + } + data.rows("completions").forEach { variant -> + val marked = variant["source"].asString + editor.text = marked.replace("§", "") + editor.awaitError() + accept(editor, marked.indexOf('§'), variant["label"].asString, variant["expected"].asString) + awaitUi("completion imports the selected type", 45.seconds) { + variant["importText"].asString in editor.text + } + editor.awaitDiagnostics(emptyList()) + } + editor.text = "module AutoImports {}" + editor.awaitDiagnostics(emptyList()) + } + } + } + + private fun Driver.discovered( + id: String, + action: (SharedScenarios.Scenario) -> Unit, + ) { + val original = shared.scenarios.getValue(id) + val data = + original.copy( + values = + original.values.deepCopy().apply { + listOf("file", "library", "neighbor", "destinationFile").filter(::has).forEach { key -> + addProperty(key, "$id/${get(key).asString}") + } + }, + ) + val discovery = !data.values.has("sourceModules") + if (discovery && service(singleProject()).getStartedServers().isEmpty()) { + // A selected discovery case can be the first to start the server. Bootstrap with + // the common graph's valid consumer, never this case's not-yet-configured source. + open(shared.dependencyNavigation.file) + } + // Independent cases replace the source graph. Close their predecessors' fixture tabs, + // as the workspace scenarios already did, before opening this case's caller. + // TODO LSP4IJ: UP07 — keeping unrelated broken fixtures open during replacement can + // cancel the new caller's lazy intentions after a successful server action reply. + // This isolates fixtures; it does not claim to repair that production delivery race. + withContext(OnDispatcher.EDT) { + val manager = service(singleProject()) + manager + .getAllEditors() + .map { it.getFile() } + .distinctBy { it.getPath() } + .forEach(manager::closeFile) + } + // Match VS Code's per-case discovery workspace so unrelated teaching fixtures cannot + // silently alter this scenario's graph or the scope of its refactoring proof. + val root = Path.of(singleProject().getBasePath()) + // Explicit graphs already isolate the case. Keep their project-relative roots anchored + // to the real project; moving the workspace folder would change their meaning. + if (discovery) { + // The server is already started. Opening the target before selecting its graph + // lets LSP4IJ cache an empty lazy quick-fix result against the previous graph. + changeWorkspaceFolders(root, root.resolve(id)) + configure("""{"xtc":{"compiler":{"sourceModules":null}}}""") + } + try { + if (data.values.has("sourceModules")) { + val modules = data.values["sourceModules"].deepCopy().asJsonArray + modules.forEach { module -> + val entry = module.asJsonObject + entry.addProperty( + "uri", + Path + .of(singleProject().getBasePath()) + .resolve(id) + .resolve(entry["uri"].asString) + .toUri() + .toString(), + ) + } + configure("""{"xtc":{"compiler":{"sourceModules":$modules}}}""") + } + // Open only after selecting this case's graph, avoiding a cold pull against the + // previous fixture graph followed immediately by diagnostic invalidation. + open(data.text("file")) + action(data) + } finally { + configure(shared.graph) + if (discovery) changeWorkspaceFolders(root.resolve(id), root) + } + } + + private fun Driver.configure(content: String) = + withContext(OnDispatcher.EDT) { + val settings = new(LspServerSettings::class) + settings.setConfigurationContent(content) + service().updateSettings("xtcLanguageServer", settings) + val traceSettings = + new(LspServerSettings::class) + .setServerTrace(utility(ClientTraceLevel::class).valueOf("verbose")) + service(singleProject()) + .updateSettings("xtcLanguageServer", traceSettings) + } + + private fun Driver.open(file: String): JEditorUiComponent { + val target = + awaitUi( + message = "File is available: $file", + timeout = 10.seconds, + getter = { findFile(relativePath = file) }, + checker = { it != null }, + ) + withContext(OnDispatcher.EDT) { + // Driver.openFile always requests focus; only popup checks need foreground activation. + service(singleProject()) + .openFile(requireNotNull(target), false, false) + } + // Tool windows such as Find Usages also contain editors; bind actions to this file's tab. + val editor = ideFrame().codeEditorForFile(Path.of(file).fileName.toString()) + // A focused run may edit the very first file. Wait for the client's initial snapshot to be + // sent before changing its document; merely opening the tab does not mean LSP startup + // ended. + awaitUi("language client opened $file", 45.seconds) { + service(singleProject()) + .getStartedServers() + .flatMap { it.getOpenedDocuments() } + .filter { it.getFile().getPath() == editor.editor.getVirtualFile().getPath() } + .any { + // OpenedDocument is registered before its synchronizer is installed. + it.getSynchronizer()?.getDidOpenFuture()?.let { future -> + future.isDone() && !future.isCompletedExceptionally() + } == true + } + } + return editor + } + + private fun Driver.caret( + editor: JEditorUiComponent, + offset: Int, + ) { + withContext(OnDispatcher.EDT) { + editor.editor.getCaretModel().moveToOffset(offset) + } + } + + private fun Driver.navigate( + editor: JEditorUiComponent, + location: SharedScenarios.Location, + ) { + val targetOffset = offset(fixtures.getValue(location.targetFile), location.target) + caret(editor, offset(editor.text, location.cursor)) + invokeAction("GotoDeclaration", component = editor.component) + awaitUi("definition ${location.targetFile}:$targetOffset", 45.seconds) { + withContext(OnDispatcher.EDT) { + service(singleProject()).getSelectedTextEditor()?.let { + it.getVirtualFile().getPath().endsWith("/${location.targetFile}") && + it.getCaretModel().getOffset() == targetOffset + } == true + } + } + } + + private fun Driver.definition( + editor: JEditorUiComponent, + at: Int, + action: String = "GotoDeclaration", + ): Pair { + val origin = editor.editor.getVirtualFile().getPath() to at + focusEditor(editor) + caret(editor, at) + invokeAction(action, component = editor.component) + + fun selected() = + withContext(OnDispatcher.EDT) { + service(singleProject()).getSelectedTextEditor()?.let { + it.getVirtualFile().getPath() to it.getCaretModel().getOffset() + } + } + awaitUi( + message = "$action navigates to a single declaration", + errorMessage = { + "Origin: $origin; selected: ${selected()}; diagnostics: ${editor.diagnostics()}" + }, + timeout = 45.seconds, + ) { + selected()?.let { it != origin } == true + } + return requireNotNull(selected()) + } + + private fun declaration( + target: Pair, + name: String, + ) { + val text = Files.readString(Path.of(target.first)) + check(Regex("${Regex.escape(name)}\\b").matchesAt(text, target.second)) { + "Expected declaration '$name' at $target" + } + } + + private fun libraryDeclaration( + target: Pair, + name: String, + ) { + declaration(target, name) + check(!Files.isWritable(Path.of(target.first))) { + "Bundled declaration must open read-only: ${target.first}" + } + } + + private fun JEditorUiComponent.diagnostics(): List = + awaitUiNotNull("read editor diagnostics", 45.seconds) { readDiagnostics() } + + private fun JEditorUiComponent.readDiagnostics(): List? = + try { + installedDiagnostics() + } catch (e: DriverCallException) { + // A concurrent edit can cancel an IDE read. + // Retry that transient read; never turn cancellation into an empty problem list. + if (generateSequence(e) { it.cause }.none { it is ProcessCanceledException }) { + throw e + } + null + } + + private fun JEditorUiComponent.awaitError() { + awaitUi( + message = "compiler error in editor", + timeout = 45.seconds, + errorMessage = { "Source: $text; editor diagnostics: ${readDiagnostics()}" }, + ) { + readDiagnostics()?.any { it.severity == "ERROR" } == true + } + } + + private fun JEditorUiComponent.awaitDiagnostics(expected: List) { + awaitUi( + message = "diagnostics $expected", + errorMessage = { "Expected $expected; editor diagnostics: ${readDiagnostics()}" }, + timeout = 45.seconds, + ) { + readDiagnostics() == expected + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybookTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybookTest.kt new file mode 100644 index 00000000000..8d4dc3e659d --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybookTest.kt @@ -0,0 +1,385 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.GsonBuilder +import com.intellij.ide.starter.ci.CIServer +import com.intellij.ide.starter.ci.NoCIServer +import com.intellij.ide.starter.di.di +import com.intellij.ide.starter.driver.engine.runIdeWithDriver +import com.intellij.ide.starter.models.IdeInfo +import com.intellij.ide.starter.models.TestCase +import com.intellij.ide.starter.path.GlobalPaths +import com.intellij.ide.starter.plugins.PluginConfigurator +import com.intellij.ide.starter.project.LocalProjectInfo +import com.intellij.ide.starter.runner.Starter +import com.intellij.platform.testFramework.teamCity.TeamCityReporter.SyntheticTestKind +import com.intellij.tools.ide.starter.product.idea.ultimate.IdeaUltimate +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.condition.EnabledIfSystemProperty +import org.kodein.di.DI +import org.kodein.di.bindSingleton +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardOpenOption.APPEND +import java.nio.file.StandardOpenOption.CREATE +import java.util.concurrent.CopyOnWriteArrayList +import kotlin.time.Duration.Companion.minutes + +/** Opt-in acceptance tests against the packaged plugin, a real IDE and the bundled compiler. */ +class CompilerPlaybookTest { + @Test fun compilerPlaybook() = runPlaybook(PlaybookMode.FEATURES) + + @Test fun startupEditing() = runPlaybook(PlaybookMode.STARTUP) + + @Test fun focusRecovery() = runPlaybook(PlaybookMode.FOCUS_RECOVERY) + + @Test fun settingsPersistence() = runPlaybook(PlaybookMode.SETTINGS_PERSISTENCE) + + @Test fun projectLifecycle() = runPlaybook(PlaybookMode.PROJECT_LIFECYCLE) + + @Test + @EnabledIfSystemProperty(named = "xtc.playbook.largeFileProbe", matches = "true") + fun largeFileEditing() = runPlaybook(PlaybookMode.LARGE_FILE) + + private fun runPlaybook(mode: PlaybookMode) { + val adapter = System.getProperty("xtc.playbook.adapter") + require(adapter == "compiler" || (adapter == "treesitter" && mode == PlaybookMode.SETTINGS_PERSISTENCE)) { + "Feature playbooks require -Plsp.adapter=compiler; settings persistence also supports treesitter" + } + val reports = Files.createDirectories(Path.of(System.getProperty("xtc.playbook.reports"))) + val run = Files.createTempDirectory(reports, "run-") + val workspace = Files.createDirectory(run.resolve("workspace")) + val manual = Files.readString(Path.of(System.getProperty("xtc.playbook.manual"))) + val blocks = + Regex("```xtc\\n([\\s\\S]*?)\\n```").findAll(manual).map { it.groupValues[1] }.toList() + val scenarioPath = Path.of(System.getProperty("xtc.playbook.scenarios")) + val shared = SharedScenarios.read(scenarioPath) + val fixtures = + shared.common.fixtures.associate { fixture -> + val matches = + blocks.filter { + Regex(fixture.pattern, RegexOption.MULTILINE).containsMatchIn(it) + } + require(matches.size == 1) { + "Expected one ${fixture.file} fixture in manual-test-plan.md" + } + val text = "${matches.single()}\n" + val file = workspace.resolve(fixture.file) + Files.createDirectories(file.parent) + Files.writeString(file, text) + fixture.file to text + } + val manualIds = + Regex("^\\| (X\\d+) \\|", RegexOption.MULTILINE) + .findAll(manual) + .map { it.groupValues[1] } + .toList() + + // These cases supply complete sources in the shared catalog instead of duplicate code + // blocks. + fun fixture( + file: String, + text: String, + ) { + val target = workspace.resolve(file) + Files.createDirectories(target.parent) + Files.writeString(target, text) + } + listOf("X101", "X102", "X103", "X104").forEach { id -> + val data = shared.scenarios.getValue(id) + fixture( + if (id == "X101") data.text("file") else "$id/${data.text("file")}", + data.text("source"), + ) + } + shared.scenarios.getValue("X99").let { + fixture("X99/${it.text("file")}", it.text("original")) + fixture("X99/${it.text("library")}", it.text("libraryText")) + } + shared.scenarios.getValue("X100").let { + fixture("X100/${it.text("file")}", it.text("source")) + fixture("X100/${it.text("neighbor")}", it.text("neighborText")) + } + shared.scenarios.getValue("X103").let { + fixture("X103/${it.text("member")}", it.text("memberSource")) + } + shared.scenarios.getValue("X103").let { + fixture("X103/${it.text("companion")}", it.text("companionSource")) + } + shared.scenarios.getValue("X105").let { + fixture("X105/${it.text("file")}", "module AutoImports {}") + fixture("X105/${it.text("library")}", it.text("libraryText")) + } + fixture("X122/Actions.x", "module Actions {}") + listOf( + "X148", + "X156", + "X157", + "X177", + "X178", + "X179", + "X180", + "X181", + "X182", + "X183", + "X184", + "X185", + "X186", + "X187", + "X188", + "X189", + "X190", + "X191", + "X192", + "X193", + "X194", + "X195", + "X196", + "X197", + "X198", + "X199", + "X200", + "X201", + "X202", + "X203", + "X204", + "X205", + "X206", + "X207", + "X208", + "X209", + "X210", + "X211", + "X212", + "X213", + "X214", + "X215", + "X221", + "X222", + "X223", + "X224", + "X225", + "X226", + "X227", + "X228", + "X229", + "X230", + "X231", + "X232", + "X233", + "X234", + "X235", + "X236", + "X237", + "X238", + "X239", + "X240", + "X241", + "X242", + ).forEach { id -> + val data = shared.scenarios.getValue(id) + fixture("$id/Extract.x", data.text("source")) + if (data.values.has("destinationFile")) fixture("$id/${data.text("destinationFile")}", data.text("destinationSource")) + if (data.values.has("files")) { + data.rows("files").forEach { file -> + fixture("$id/${file["file"].asString}", file["source"].asString) + } + } + } + listOf("X149", "X150", "X151", "X152").forEach { id -> + val data = shared.scenarios.getValue(id) + fixture("$id/${data.text("file")}", data.rows("variants").first()["source"].asString) + } + ((109..121).map { "X$it" } + listOf("X155", "X159", "X160", "X164", "X165", "X166", "X167", "X168", "X220")).forEach { id -> + shared.scenarios.getValue(id).rows("files").forEach { file -> + fixture("$id/${file["file"].asString}", file["source"].asString) + } + } + shared.scenarios.getValue("7a.9").let { + fixture(it.text("file"), it.text("moduleStart") + it.text("moduleEnd")) + } + require(shared.ids.filter { it.startsWith("X") } == manualIds) { + "Shared catalog and manual playbook rows differ" + } + shared.validate(fixtures) + val ideVersion = System.getProperty("xtc.playbook.ideVersion") + val localLsp4ij = System.getProperty("xtc.playbook.lsp4ijPlugin")?.let { LocalLsp4ijPlugin.read(Path.of(it)) } + val replacementGates = System.getProperty("xtc.playbook.replacementGates", "").split(',').filter(String::isNotEmpty) + require(replacementGates.all { it in setOf("UP25", "UP26") }) { "Unknown replacement gate: $replacementGates" } + require(replacementGates.isEmpty() || localLsp4ij != null) { "Replacement gates require an explicit local LSP4IJ build" } + val lsp4ijVersion = localLsp4ij?.version ?: System.getProperty("xtc.playbook.lsp4ijVersion") + val ideFailures = CopyOnWriteArrayList() + val selection = + System + .getProperty("xtc.playbook.cases", "") + .split(',') + .map(String::trim) + .filter(String::isNotEmpty) + .toSet() + require(selection.all { it in shared.implementedIds }) { + "Unknown or unimplemented native case: $selection" + } + val cases = + CompilerPlaybook( + fixtures, + shared, + lsp4ijVersion, + selection.ifEmpty { shared.implementedIds }, + mode, + ) { result -> + Files.writeString( + run.resolve("progress.jsonl"), + Gson().toJson(result) + "\n", + CREATE, + APPEND, + ) + } + val previousDi = di + di = + DI { + extend(previousDi) + bindSingleton(overrides = true) { object : GlobalPaths(reports) {} } + bindSingleton(overrides = true) { + object : CIServer by NoCIServer { + override fun reportTestFailure( + testName: String, + message: String, + details: String, + linkToLogs: String?, + kind: SyntheticTestKind, + generifyTestName: Boolean, + ) { + ideFailures += "$testName: $message\n$details\n$linkToLogs" + } + } + } + } + try { + val testCase = TestCase(IdeInfo.IdeaUltimate, LocalProjectInfo(workspace)).withVersion(ideVersion) + val context = + Starter.newContext( + "XtcCompilerPlaybook-${run.fileName}", + testCase.copy( + ideInfo = + testCase.ideInfo.copy( + getInstaller = { CachedIdeInstaller(testCase.ideInfo.getInstaller(it)) }, + ), + ), + ) + Files.writeString(run.resolve("ide-paths.txt"), context.paths.toString()) + PluginConfigurator(context).apply { + if (localLsp4ij == null) { + installPluginFromPluginManager("com.redhat.devtools.lsp4ij", lsp4ijVersion) + } else { + installPluginFromDir(Path.of(localLsp4ij.path)) + } + installPluginFromPath(Path.of(System.getProperty("path.to.build.plugin"))) + installPluginFromDir(DiagnosticProbePlugin.create(run)) + disablePlugins("com.intellij.kubernetes", "com.intellij.clouds.kubernetes") + } + // Keep even a sole candidate visible until the driver inspects and accepts it. + // This affects only the disposable test IDE, not the packaged plugin's defaults. + Files.writeString( + Files + .createDirectories(context.paths.configDir.resolve("options")) + .resolve("editor.xml"), + """ + + + + + """.trimIndent(), + ) + // Unsaved-overlay scenarios must not be silently saved by an application focus change. + Files.writeString( + context.paths.configDir.resolve("options/ide.general.xml"), + """ + + + + + """.trimIndent(), + ) + // The full playbook exceeds Starter's ten-minute default; individual waits + // remain bounded so an unresponsive editor still fails promptly. + val configured = + context + .disableUltimateModule() + .applyVMOptionsPatch { + // Prevent trial/license startup from dynamically re-enabling the paid module. + addSystemProperty("request.trial", false) + addSystemProperty("idea.suppressed.plugins.id", "com.intellij.modules.ultimate") + addSystemProperty("xtc.lsp.semanticTokens", true) + addSystemProperty( + "xtc.lsp.colorPrototype", + shared.common.colorPrototype["cases"].asJsonArray.any { + selection.isEmpty() || it.asString in selection + }, + ) + addSystemProperty("xtc.trace.directory", run.resolve("server-trace").toString()) + addSystemProperty("xtc.logs.directory", run.resolve("server-logs").toString()) + addSystemProperty("idea.auto.reload.plugins", false) + } + repeat(if (mode == PlaybookMode.SETTINGS_PERSISTENCE) 2 else 1) { phase -> + configured.runIdeWithDriver(runTimeout = 30.minutes).useDriverAndCloseIde { + cases.run(this, phase) + } + } + Files.writeString(run.resolve(".completed"), "IDE closed\n") + check(ideFailures.isEmpty()) { ideFailures.joinToString("\n\n") } + } finally { + // Restore global Starter state even if serializing or writing the report fails. + di = previousDi + val report = + mapOf( + "ideVersion" to ideVersion, + "lsp4ijVersion" to lsp4ijVersion, + "localLsp4ij" to localLsp4ij, + "replacementGates" to replacementGates, + // The shared catalog retains shipping-version limitations. Report the + // additional opt-in assertions separately from those default statuses. + "replacementGateResults" to + replacementGates.associateWith { gate -> + val id = if (gate == "UP25") "X254" else "X257" + when (val status = cases.results.single { it.id == id }.status) { + "passed", "partial" -> if (ideFailures.isEmpty()) "passed" else "ide-failure" + else -> status + } + }, + "adapter" to adapter, + "startupOnly" to (mode == PlaybookMode.STARTUP), + "mode" to mode.name, + "selectedCases" to + when (mode) { + PlaybookMode.FEATURES -> selection.ifEmpty { shared.implementedIds } + PlaybookMode.STARTUP -> setOf("STARTUP") + PlaybookMode.FOCUS_RECOVERY -> setOf("START_FOCUS") + PlaybookMode.LARGE_FILE -> setOf("START_LARGE_FILE") + PlaybookMode.PROJECT_LIFECYCLE -> setOf("START_PROJECTS") + PlaybookMode.SETTINGS_PERSISTENCE -> setOf("START_SETTINGS_0", "START_SETTINGS_1") + }, + "sharedScenarios" to + mapOf( + "file" to scenarioPath.toString(), + "sha256" to shared.sourceHash, + "ids" to shared.ids, + ), + "cases" to cases.results, + "counts" to cases.results.groupingBy { it.status }.eachCount(), + "scope" to + "Native IntelliJ actions with Ultimate disabled. Partial and unimplemented cases are explicit; manual visual checks remain separate.", + "ideFailures" to ideFailures.toList(), + ) + Files.writeString( + run.resolve("results.json"), + GsonBuilder().setPrettyPrinting().create().toJson(report) + "\n", + ) + println("IntelliJ compiler playbook report: ${run.resolve("results.json")}") + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt new file mode 100644 index 00000000000..f283683033e --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt @@ -0,0 +1,27 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.sdk.Project + +@Remote( + "org.xtclang.idea.playbook.probe.CompilerSettingsPage", + plugin = "org.xtclang.playbook.probe", +) +interface CompilerSettingsPage { + fun useBuildModel(project: Project): String + + fun refreshBuildModel(project: Project) + + fun installProjectGraph(project: Project) + + fun dismissExpectedConfigurationError(project: Project) + + fun resourceRootsRoundTrip( + project: Project, + roots: String, + ) + + fun content(project: Project): String? + + fun clearProjectGraph(project: Project) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt new file mode 100644 index 00000000000..890a610d72f --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt @@ -0,0 +1,219 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.common.LookupElementPresentation +import com.intellij.openapi.actionSystem.IdeActions.ACTION_EDITOR_NEXT_TEMPLATE_VARIABLE +import kotlin.time.Duration.Companion.seconds + +fun Driver.syntaxCompletions( + data: SharedScenarios.Scenario, + editor: JEditorUiComponent, + diagnostics: () -> Unit, +) { + data.rows("variants").forEach { variant -> + val source = variant["source"].asString + val expected = variant["expected"].asString + val anchor = variant["anchor"].asString + editor.text = source + val at = source.indexOf(anchor) + (variant["prefix"]?.asString ?: anchor).length + accept(editor, at, variant["label"].asString, expected) + variant["selected"]?.asString?.let { placeholder -> + withContext(OnDispatcher.EDT) { + val selection = cast(editor.editor.getSelectionModel(), SelectionOffsets::class) + check(editor.text.substring(selection.getSelectionStart(), selection.getSelectionEnd()) == placeholder) { + "Native snippet must select its first placeholder: $placeholder" + } + } + // Tab is routed to this action while a live template is active. EditorTab itself + // bypasses that routing and replaces the selected placeholder with whitespace. + invokeAction(ACTION_EDITOR_NEXT_TEMPLATE_VARIABLE, component = editor.component) + withContext(OnDispatcher.EDT) { + val selection = cast(editor.editor.getSelectionModel(), SelectionOffsets::class) + check(selection.getSelectionStart() == selection.getSelectionEnd()) + check(editor.text == expected) { "Template navigation changed source: ${editor.text}" } + val finalOffset = + variant["finalAnchor"]?.asString?.let { expected.indexOf(it) + it.length } + ?: expected.lines().first { it.isNotEmpty() && it.isBlank() }.let { + expected.indexOf("\n$it\n") + 1 + it.length + } + check(editor.editor.getCaretModel().getOffset() == finalOffset) + } + } + diagnostics() + focusEditor(editor) + invokeAction("\$Undo", now = false, component = editor.component) + if (variant["selected"] != null) { + // IDEA can undo caret navigation separately (editor.undo.transparent.caret.movement). + // Accept only that exact, text-preserving step before undoing the insertion once. + awaitUi("undo insertion or restore its selected placeholder", 15.seconds) { + editor.text == source || + withContext(OnDispatcher.EDT) { + val selection = cast(editor.editor.getSelectionModel(), SelectionOffsets::class) + editor.text == expected && + editor.text.substring(selection.getSelectionStart(), selection.getSelectionEnd()) == + variant["selected"].asString + } + } + if (editor.text != source) invokeAction("\$Undo", now = false, component = editor.component) + } + awaitUi("one undo restores the completion prefix", 15.seconds) { editor.text == source } + } +} + +fun Driver.lookup( + editor: JEditorUiComponent, + at: Int, + matches: (List) -> Boolean, +) { + dismissPopups() + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + editor.scrollToCaretNow() + val inspection = + PopupInspection(this, editor) { + invokeAction("CodeCompletion", component = editor.component) + } + invokeAction("CodeCompletion", component = editor.component) + val manager = utility(EditorLookupManager::class).getInstance(singleProject()) + awaitUi( + message = "shared completion scope", + timeout = 45.seconds, + errorMessage = { + "Current native items: " + + manager.getActiveLookup()?.getItems()?.map { item -> + val presentation = new(LookupElementPresentation::class) + item.renderElement(presentation) + "${item.getLookupString()}: ${presentation.getTypeText()} ${presentation.getTailText()}" + } + }, + ) { + inspection.recover() + manager.getActiveLookup()?.let { !it.isCalculating() && matches(it.getItems()) } == true + } +} + +fun Driver.hasType( + items: List, + label: String, + pattern: String, +): Boolean = + items + .firstOrNull { it.getLookupString() == label } + ?.let { item -> + val presentation = new(LookupElementPresentation::class) + item.renderElement(presentation) + Regex(pattern) + .containsMatchIn( + presentation.getTypeText().orEmpty() + + " " + + presentation.getTailText().orEmpty(), + ) + } == true + +fun Driver.acceptCandidates( + editor: JEditorUiComponent, + at: Int, + expected: List, + selected: String, + kind: Int? = null, +) { + lookup(editor, at) { items -> + items.map { it.getLookupString() }.sorted() == expected.sorted() && + (kind == null || hasCompletionKinds(items, expected, kind)) + } + invokeAction("EditorEscape", component = editor.component) + accept(editor, at, selected) +} + +/** Inspect the actual native proposals, including metadata omitted from their rendered text. */ +fun Driver.hasCompletionKinds( + items: List, + labels: List, + kind: Int, +): Boolean { + val protocol = ClientProtocol(this) + return labels.all { label -> + items + .singleOrNull { it.getLookupString() == label } + ?.let { + val proposal = cast(it, NativeCompletionElement::class).getObject() + protocol.copy(proposal.getItem()).asJsonObject["kind"]?.asInt == kind + } == true + } +} + +/** + * Read metadata from the actual native proposal, without issuing a competing completion request. + */ +fun Driver.hasCompletionMetadata( + items: List, + label: String, + expected: JsonObject, +): Boolean { + val item = items.singleOrNull { it.getLookupString() == label } ?: return false + val proposal = cast(item, NativeCompletionElement::class).getObject() + val value = ClientProtocol(this).copy(proposal.getItem()).asJsonObject + return value["detail"]?.asString?.contains(expected["detailContains"].asString) == true && + (expected["kind"] == null || value["kind"]?.asInt == expected["kind"].asInt) && + (expected["sorted"]?.asBoolean != true || value["sortText"]?.asString?.isNotBlank() == true) +} + +@Remote("com.intellij.codeInsight.lookup.LookupElement") +interface NativeCompletionElement { + fun getObject(): NativeCompletionProposal +} + +@Remote( + "com.redhat.devtools.lsp4ij.client.features.LSPCompletionProposal", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeCompletionProposal { + fun getItem(): ClientValue +} + +fun Driver.accept( + editor: JEditorUiComponent, + at: Int, + label: String, + expectedText: String? = null, +) { + val before = editor.text + check(at in 0..before.length) + val prefix = before.take(at).takeLastWhile { it.isLetterOrDigit() || it == '_' } + val expected = expectedText ?: before.replaceRange(at - prefix.length, at, label) + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + editor.scrollToCaretNow() + val inspection = + PopupInspection(this, editor) { + invokeAction("CodeCompletion", component = editor.component) + } + invokeAction("CodeCompletion", component = editor.component) + val manager = utility(EditorLookupManager::class).getInstance(singleProject()) + awaitUi("$label completion or single-item insertion", 45.seconds) { + // An insertion may already have completed before focus was lost. Never reopen it. + if (editor.text == expected) return@awaitUi true + inspection.recover() + editor.text == expected || + manager.getActiveLookup()?.let { + !it.isCalculating() && it.getItems().any { item -> item.getLookupString() == label } + } == true + } + if (editor.text != expected) { + withContext(OnDispatcher.EDT) { + val lookup = + requireNotNull(manager.getActiveLookup()) { + "Native completion closed before acceptance; switching applications or editors cancels the popup" + } + lookup.setCurrentItem(lookup.getItems().first { it.getLookupString() == label }) + } + invokeAction("EditorChooseLookupItem", component = editor.component) + } + awaitUi("exact replacement of the typed prefix", 15.seconds) { editor.text == expected } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/DiagnosticProbePlugin.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/DiagnosticProbePlugin.kt new file mode 100644 index 00000000000..8fb0f7784df --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/DiagnosticProbePlugin.kt @@ -0,0 +1,47 @@ +package org.xtclang.idea.playbook + +import java.nio.file.Files +import java.nio.file.Path +import java.util.jar.JarEntry +import java.util.jar.JarOutputStream + +/** Package IDE-side test helpers without changing the shipping artifact. */ +object DiagnosticProbePlugin { + fun create(directory: Path): Path { + val plugin = directory.resolve("xtc-playbook-probe") + val archive = Files.createDirectories(plugin.resolve("lib")).resolve("probe.jar") + JarOutputStream(Files.newOutputStream(archive)).use { jar -> + jar.putNextEntry(JarEntry("META-INF/plugin.xml")) + jar.write( + """ + + org.xtclang.playbook.probe + XTC Playbook Diagnostics Probe + 1 + xtclang.org + com.intellij.modules.platform + com.intellij.modules.lang + com.redhat.devtools.lsp4ij + org.xtclang.idea + + """.trimIndent() + .toByteArray(), + ) + jar.closeEntry() + val packagePath = "org/xtclang/idea/playbook/probe" + val classes = Path.of(requireNotNull(javaClass.getResource("/$packagePath")).toURI()) + // Include compiler-generated function-reference classes as well as the reader itself. + Files.list(classes).use { files -> + files + .filter { it.fileName.toString().endsWith(".class") } + .sorted() + .forEach { file -> + jar.putNextEntry(JarEntry("$packagePath/${file.fileName}")) + Files.copy(file, jar) + jar.closeEntry() + } + } + } + return plugin + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt new file mode 100644 index 00000000000..25dbd2a1f12 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt @@ -0,0 +1,180 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.common.ideFrame +import com.intellij.driver.sdk.ui.remote.Window +import kotlin.time.Duration.Companion.seconds + +/** Close transient UI between isolated scenarios, including after an assertion fails. */ +fun Driver.dismissPopups() { + withContext(OnDispatcher.EDT) { + utility(NativeEventQueue::class).getInstance().getPopupManager().closeAllPopups() + service().hideAllHints() + } +} + +@Remote("com.intellij.ide.IdeEventQueue") +interface NativeEventQueue { + fun getInstance(): NativeEventQueue + + fun getPopupManager(): NativePopupManager +} + +@Remote("com.intellij.ide.IdePopupManager") +interface NativePopupManager { + fun closeAllPopups(): Boolean +} + +/** + * Native popups require focus; AppIcon activates the window without Driver's title-bar mouse click. + */ +fun Driver.focusEditor(editor: JEditorUiComponent) { + val window = cast(ideFrame().component, Window::class) + val nativeEditor = editor.editor + withContext(OnDispatcher.EDT) { + if (!window.isFocused()) utility(NativeAppFocus::class).getInstance().activate(window) + } + // AppIcon activation is asynchronous. Requesting component focus in that same EDT event + // can be superseded when the window restores its previous focus owner. + awaitUi("native IDE window focus", 10.seconds) { + withContext(OnDispatcher.EDT) { window.isFocused() } + } + withContext(OnDispatcher.EDT) { + utility(NativeEditorUi::class).focusEditor(nativeEditor) + } + awaitUi( + "native editor focus", + 10.seconds, + errorMessage = { + withContext(OnDispatcher.EDT) { + utility(NativeEditorUi::class).focusState(nativeEditor) + } + }, + ) { + withContext(OnDispatcher.EDT) { + window.isFocused() && utility(NativeEditorUi::class).isEditorActive(nativeEditor) + } + } +} + +/** + * Restore application focus without replaying the action that opened a dialog or applied an edit. + */ +internal fun Driver.restorePopupFocus(editor: Editor): Boolean { + // TODO LSP4IJ: UP20 (IntelliJ Driver) captures modality before posting its EDT call. + // A dialog can open between those steps. Use the probe's bounded Swing dispatch until + // Driver makes observation/focus calls safe across a modal transition. + fun focused() = withContext(OnDispatcher.DEFAULT) { utility(NativeEditorUi::class).hasFocus(editor) } + if (focused()) return false + withContext(OnDispatcher.DEFAULT) { + utility(NativeEditorUi::class).popupFocus(editor, true) + } + try { + awaitUi( + "restored native IDE focus", + 10.seconds, + errorMessage = { + withContext(OnDispatcher.DEFAULT) { utility(NativeEditorUi::class).focusState(editor) } + }, + ) { + focused() + } + } finally { + withContext(OnDispatcher.DEFAULT) { + utility(NativeEditorUi::class).popupFocus(editor, false) + } + } + println("IntelliJ playbook: restored IDE focus without pointer input") + return true +} + +/** Only the unapplied popup inspection can be reopened; callers accept edits after the wait. */ +internal class PopupInspection( + private val driver: Driver, + private val editor: JEditorUiComponent, + private val reopen: () -> Unit, +) { + private val document = driver.cast(editor.document, DocumentVersion::class) + private val stamp = document.getModificationStamp() + private val originalText = editor.text + private val nativeEditor = editor.editor + private val caretOffset = driver.withContext(OnDispatcher.EDT) { nativeEditor.getCaretModel().getOffset() } + + fun recover() { + if (driver.restorePopupFocus(nativeEditor)) { + reopenUnapplied() + println("IntelliJ playbook: reopened an unapplied popup after focus loss") + } + } + + /** A cold intention request may populate its cache without opening a menu. */ + fun reopenUnapplied() { + check(document.getModificationStamp() == stamp) { + "Source changed during popup inspection; refusing to replay an action. " + + "Before: ${originalText.take(300)}; now: ${editor.text.take(300)}" + } + driver.withContext(OnDispatcher.EDT) { + driver.utility(NativeEditorUi::class).closeCompletion(nativeEditor) + } + driver.dismissPopups() + driver.focusEditor(editor) + // Focusing another component can move the caret without changing the source. Reopen + // this unapplied query at its captured position, never at an unrelated click location. + driver.withContext(OnDispatcher.EDT) { nativeEditor.getCaretModel().moveToOffset(caretOffset) } + reopen() + } +} + +internal fun JEditorUiComponent.scrollToCaretNow() { + val nativeEditor = editor + driver.withContext(OnDispatcher.EDT) { + driver.utility(NativeEditorUi::class).scrollToCaret(nativeEditor) + } +} + +@Remote("org.xtclang.idea.playbook.probe.EditorUi", plugin = "org.xtclang.playbook.probe") +internal interface NativeEditorUi { + fun focusEditor(editor: Editor) + + fun isEditorActive(editor: Editor): Boolean + + fun focusState(editor: Editor): String + + fun renameState(editor: Editor): String + + fun renameAvailable(editor: Editor): Boolean + + fun interruptFocus(): Window + + fun closeCompletion(editor: Editor) + + fun hasFocus(editor: Editor): Boolean + + fun popupFocus( + editor: Editor, + restoring: Boolean, + ) + + fun scrollToCaret(editor: Editor) +} + +@Remote("com.intellij.ui.AppIcon") +interface NativeAppFocus { + fun getInstance(): NativeAppFocus + + fun requestFocus(window: Window) + + fun requestFocus() +} + +private fun NativeAppFocus.activate(window: Window) { + requestFocus(window) + // On macOS the window overload uses requestForeground(false). The application overload + // uses true, which also restores the IDE when another application owns desktop focus. + requestFocus() +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/FocusRecovery.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/FocusRecovery.kt new file mode 100644 index 00000000000..c56e8e37dd0 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/FocusRecovery.kt @@ -0,0 +1,120 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import kotlin.time.Duration.Companion.seconds + +/** Exercise actual focus switches, native popup reopening, and the document-stamp replay guard. */ +fun Driver.focusRecovery( + fixtures: Map, + shared: SharedScenarios, +) { + ParityWorkspace(this, "FOCUS", fixtures, shared).use { workspace -> + workspace.write( + "Focus.x", + """ + module Focus { + private Int run(String word) { return word.si; } + void pair(Int number, String text) {} + void inspect() { pair(1, "x"); } + } + """.trimIndent() + "\n", + ) + val document = workspace.open("Focus.x") + val editor = document.editor + val at = document.at("word.si") + "word.si".length + + fun completion() = lookup(editor, at) { items -> items.any { it.getLookupString() == "size" } } + val inspection = PopupInspection(this, editor, ::completion) + completion() + interruptFocus(editor) { inspection.recover() } + check(editor.text.contains("word.si;")) { "Inspecting completion applied an edit" } + accept(editor, at, "size") + workspace.clean(document) + val accepted = editor.text + interruptFocus(editor) { + val failure = runCatching { inspection.recover() }.exceptionOrNull() + check( + failure is IllegalStateException && + failure.message.orEmpty().startsWith("Source changed"), + ) { + "A completed insertion was allowed into the replay path: $failure" + } + } + check(editor.text == accepted) + + // A recovered argument popup must not become a second invocation with untyped words. + editor.text = + accepted.replace( + "pair(1, \"x\");", + "String text = \"x\"; Int textNumber = 1; pair(1, te);", + ) + workspace.errors(document) + val argumentAt = editor.text.indexOf("pair(1, te)") + "pair(1, te".length + + fun argumentCompletion() = + lookup(editor, argumentAt) { items -> + items.map { it.getLookupString() } == listOf("text") + } + val argumentInspection = PopupInspection(this, editor, ::argumentCompletion) + argumentCompletion() + interruptFocus(editor) { argumentInspection.recover() } + check(editor.text.contains("pair(1, te);")) { + "Recovering argument completion applied an edit" + } + dismissPopups() + editor.text = accepted + workspace.clean(document) + + val call = document.at("pair(1") + "pair(".length + + fun parameters() = + signature(editor, call, keepOpen = true) { values -> + values.singleOrNull()?.parameters?.size == 2 + } + val parameterInspection = PopupInspection(this, editor, ::parameters) + parameters() + interruptFocus(editor) { parameterInspection.recover() } + dismissPopups() + + // Capture the same guard before a real rename, then prove it rejects any replay afterward. + val renameInspection = + PopupInspection(this, editor) { error("A completed rename must never be replayed") } + rename(editor, document.at("word"), "text") + val renamed = accepted.replace("word", "text") + awaitUi("rename applied exactly once", 45.seconds) { editor.text == renamed } + interruptFocus(editor) { + val failure = runCatching { renameInspection.recover() }.exceptionOrNull() + check( + failure is IllegalStateException && + failure.message.orEmpty().startsWith("Source changed"), + ) { + "A completed rename was allowed into the replay path: $failure" + } + } + check(editor.text == renamed) + workspace.clean(document) + } +} + +private fun Driver.interruptFocus( + editor: JEditorUiComponent, + action: () -> Unit, +) { + val nativeEditor = editor.editor + val other = withContext(OnDispatcher.EDT) { utility(NativeEditorUi::class).interruptFocus() } + try { + awaitUi("unowned test window interrupts IDE focus", 10.seconds) { + withContext(OnDispatcher.EDT) { + other.isFocused() && !utility(NativeEditorUi::class).hasFocus(nativeEditor) + } + } + action() + check( + withContext(OnDispatcher.EDT) { utility(NativeEditorUi::class).hasFocus(nativeEditor) }, + ) + } finally { + withContext(OnDispatcher.EDT) { other.dispose() } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt new file mode 100644 index 00000000000..44e0e6c6d62 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt @@ -0,0 +1,148 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.PsiFile +import com.intellij.driver.sdk.ui.components.common.LookupElementPresentation + +/** Test-only Driver proxies; no automation hooks are added to the shipped plugin. */ +@Remote( + "com.redhat.devtools.lsp4ij.settings.GlobalLanguageServerSettings", + plugin = "com.redhat.devtools.lsp4ij", +) +interface LspSettings { + fun getLanguageServerSettings(serverId: String): LspServerSettings? + + fun updateSettings( + serverId: String, + settings: LspServerSettings, + ) +} + +@Remote( + "com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings", + plugin = "com.redhat.devtools.lsp4ij", +) +interface ProjectLspSettings { + fun updateSettings( + serverId: String, + settings: LspServerSettings, + ) +} + +@Remote( + "com.redhat.devtools.lsp4ij.settings.LanguageServerSettings\$LanguageServerDefinitionSettings", + plugin = "com.redhat.devtools.lsp4ij", +) +interface LspServerSettings { + fun getConfigurationContent(): String? + + fun setConfigurationContent(content: String) + + fun setServerTrace(trace: ClientTraceLevel): LspServerSettings +} + +@Remote("com.intellij.codeInsight.lookup.LookupManager") +interface EditorLookupManager { + fun getInstance(project: Project): EditorLookupManager + + fun getActiveLookup(): EditorLookup? +} + +@Remote( + value = "com.intellij.codeInsight.lookup.impl.LookupImpl", + serviceInterface = "com.intellij.codeInsight.lookup.Lookup", +) +interface EditorLookup { + fun getItems(): List + + fun isCalculating(): Boolean + + fun getCurrentItem(): CompletionItem? + + fun setCurrentItem(item: CompletionItem) +} + +@Remote("com.intellij.codeInsight.lookup.LookupElement") +interface CompletionItem { + fun getLookupString(): String + + fun renderElement(presentation: LookupElementPresentation) +} + +@Remote("com.redhat.devtools.lsp4ij.LSPFileSupport", plugin = "com.redhat.devtools.lsp4ij") +interface LspFileSupport { + fun getSupport(file: PsiFile): LspFileSupport + + fun getSignatureHelpSupport(): SignatureSupport + + fun getHighlightSupport(): HighlightSupport + + fun getSelectionRangeSupport(): SelectionRangeSupport +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.signatureHelp.LSPSignatureHelpSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface SignatureSupport { + fun getValidLSPFuture(): SignatureFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface SignatureFuture { + fun isDone(): Boolean + + fun isCompletedExceptionally(): Boolean + + fun get(): SignatureHelp? +} + +@Remote("org.eclipse.lsp4j.SignatureHelp", plugin = "com.redhat.devtools.lsp4ij") +interface SignatureHelp { + fun getSignatures(): List + + fun getActiveSignature(): Int? + + fun getActiveParameter(): Int? +} + +@Remote("org.eclipse.lsp4j.SignatureInformation", plugin = "com.redhat.devtools.lsp4ij") +interface SignatureInformation { + fun getLabel(): String + + fun getDocumentation(): ClientValue? + + fun getActiveParameter(): Int? + + fun getParameters(): List +} + +@Remote("org.eclipse.lsp4j.ParameterInformation", plugin = "com.redhat.devtools.lsp4ij") +interface ParameterInformation { + fun getLabel(): ParameterLabel +} + +@Remote("org.eclipse.lsp4j.jsonrpc.messages.Either", plugin = "com.redhat.devtools.lsp4ij") +interface ParameterLabel { + fun getLeft(): String? + + fun getRight(): ParameterRange +} + +@Remote("org.eclipse.lsp4j.jsonrpc.messages.Tuple\$Two", plugin = "com.redhat.devtools.lsp4ij") +interface ParameterRange { + fun getFirst(): Int + + fun getSecond(): Int +} + +@Remote("com.intellij.ui.components.JBHtmlPane") +interface ParameterHintText { + fun getText(): String +} + +@Remote("com.intellij.codeInsight.hint.HintManager") +interface EditorHints { + fun hideAllHints() +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt new file mode 100644 index 00000000000..1a4f7ad8c25 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt @@ -0,0 +1,49 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.sdk.Project + +@Remote( + "org.xtclang.idea.playbook.probe.LanguageServicePage", + plugin = "org.xtclang.playbook.probe", +) +interface LanguageServicePage { + fun exercise(project: Project) + + fun content(project: Project): String? + + fun restore( + project: Project, + content: String?, + ) + + fun inlayHints( + project: Project, + enabled: Boolean, + ) + + fun referenceCodeLens( + project: Project, + enabled: Boolean, + ) + + fun transport( + project: Project, + value: String, + ) + + fun adapter( + project: Project, + value: String, + ) + + fun saveFormatting( + project: Project, + enabled: Boolean, + ): Boolean + + fun indent( + project: Project, + value: Int, + ): Int +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt new file mode 100644 index 00000000000..444b26e502f --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt @@ -0,0 +1,92 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonParser +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.VirtualFile +import com.intellij.driver.sdk.singleProject +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardOpenOption.APPEND +import java.nio.file.StandardOpenOption.CREATE +import kotlin.time.Duration.Companion.seconds + +// TODO LSP4IJ: UP17 — the underlying IntelliJ interval tree scales poorly when many ranges are +// invalidated together. Keep this diagnostic and freeze gate until the platform repair is verified. + +/** Opt-in freeze investigation; receipts survive even if the IDE's freeze detector fails the run. */ +fun Driver.largeFileEditing( + fixtures: Map, + shared: SharedScenarios, +) { + val report = Path.of(singleProject().getBasePath()).parent.resolve("large-file-editing.jsonl") + + fun record(value: String) { + Files.writeString(report, value + "\n", CREATE, APPEND) + println("IntelliJ large-file probe: $value") + } + + listOf(0, 20_000, 40_000, 80_000).forEach { markers -> + record(Gson().toJson(mapOf("phase" to "plain-document-start", "markers" to markers))) + record(withContext(OnDispatcher.EDT) { utility(LargeFileProbe::class).plain(markers, false) }) + } + record(Gson().toJson(mapOf("phase" to "plain-document-start", "markers" to 80_000, "bulk" to true))) + record(withContext(OnDispatcher.EDT) { utility(LargeFileProbe::class).plain(80_000, true) }) + ParityWorkspace(this, "LARGE_FILE", fixtures, shared).use { workspace -> + fun workload(methods: Int) = + "module LargeFile {\n static Int value = 1;\n" + + (0 until methods).joinToString("\n") { " Int read$it() { return value; }" } + + "\n}\n" + + workspace.write("LargeFile.x", workload(20_000)) + workspace.configure( + listOf(SharedScenarios.SourceModule("LargeFile", workspace.uri("LargeFile.x"), emptyList())), + ) + val document = workspace.open("LargeFile.x") + workspace.clean(document) + check(!workspace.query("textDocument/hover", document, document.at("value")).isJsonNull) + val file = document.editor.editor.getVirtualFile() + focusEditor(document.editor) + // A server reply does not mean the IDE has applied its semantic highlighting yet. + // Replacing an undecorated document would produce a misleading fast result. + val before = + awaitUi( + "large-file highlighting is installed before replacement", + 60.seconds, + getter = { withContext(OnDispatcher.EDT) { utility(LargeFileProbe::class).snapshot(file) } }, + checker = { value -> + JsonParser.parseString(value).asJsonObject["markupModels"].asJsonArray.sumOf { + it.asJsonObject["highlighters"].asInt + } >= 20_000 + }, + ) + record(before) + record( + withContext(OnDispatcher.EDT) { + utility(LargeFileProbe::class).replace(singleProject(), file, workload(5_000)) + }, + ) + workspace.settle(document) + workspace.clean(document) + } + println("IntelliJ large-file editing receipt: $report") +} + +@Remote("org.xtclang.idea.playbook.probe.LargeFileProbe", plugin = "org.xtclang.playbook.probe") +interface LargeFileProbe { + fun plain( + markers: Int, + bulk: Boolean, + ): String + + fun snapshot(file: VirtualFile): String + + fun replace( + project: Project, + file: VirtualFile, + text: String, + ): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LocalLsp4ijPlugin.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LocalLsp4ijPlugin.kt new file mode 100644 index 00000000000..567460fe5b6 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LocalLsp4ijPlugin.kt @@ -0,0 +1,57 @@ +package org.xtclang.idea.playbook + +import java.nio.file.Files +import java.nio.file.Path +import java.security.MessageDigest +import java.util.HexFormat +import java.util.jar.JarFile +import javax.xml.XMLConstants +import javax.xml.parsers.DocumentBuilderFactory + +/** Identify the explicit local dependency before copying it into the disposable test IDE. */ +data class LocalLsp4ijPlugin( + val path: String, + val version: String, + val sha256: String, +) { + companion object { + fun read(path: Path): LocalLsp4ijPlugin { + val files = Files.walk(path).use { it.filter(Files::isRegularFile).sorted().toList() } + val descriptor = + files + .asSequence() + .filter { it.toString().endsWith(".jar") } + .mapNotNull { file -> + JarFile(file.toFile()).use { jar -> + jar.getJarEntry("META-INF/plugin.xml")?.let { entry -> + jar.getInputStream(entry).use { xml -> + DocumentBuilderFactory + .newInstance() + .apply { + setFeature("http://apache.org/xml/features/disallow-doctype-decl", true) + setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "") + setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "") + }.newDocumentBuilder() + .parse(xml) + .documentElement + } + } + } + }.single { it.getElementsByTagName("id").item(0)?.textContent == "com.redhat.devtools.lsp4ij" } + val version = + descriptor + .getElementsByTagName("version") + .item(0) + .textContent + .trim() + require(version.isNotEmpty()) { "Missing LSP4IJ version in $path" } + val digest = MessageDigest.getInstance("SHA-256") + files.forEach { file -> + digest.update(path.relativize(file).toString().toByteArray(Charsets.UTF_8)) + digest.update(0.toByte()) + digest.update(Files.readAllBytes(file)) + } + return LocalLsp4ijPlugin(path.toAbsolutePath().toString(), version, HexFormat.of().formatHex(digest.digest())) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/MemberActionScenarios.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/MemberActionScenarios.kt new file mode 100644 index 00000000000..4160770ebba --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/MemberActionScenarios.kt @@ -0,0 +1,83 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Shared generation data, native intention selection and the editor's own Undo/Redo. */ +fun Driver.memberActions( + data: SharedScenarios.Scenario, + editor: JEditorUiComponent, + diagnostics: (JEditorUiComponent, Boolean) -> Unit, +) { + data.rows("variants").forEach { variant -> + val original = variant["source"].asString + val expected = variant["expected"].asString + val title = variant["title"].asString + val broken = variant["initiallyValid"]?.asBoolean == false + editor.text = original + diagnostics(editor, broken) + val at = editor.text.indexOf(data.text("anchor")) + val position = mapOf("line" to 0, "character" to at) + + fun actions() = + ClientProtocol(this) + .query( + "textDocument/codeAction", + mapOf( + "textDocument" to + mapOf( + "uri" to + Path + .of(editor.editor.getVirtualFile().getPath()) + .toUri() + .toString(), + ), + "range" to mapOf("start" to position, "end" to position), + "context" to mapOf("diagnostics" to emptyList()), + ), + ).asJsonArray + if (title.isEmpty()) { + val actions = + awaitUiNotNull("member refusal response", 45.seconds) { + try { + actions() + } catch (failure: ClientRequestFailure) { + if (failure.code !in + setOf(ResponseErrorCode.RequestCancelled.value, ResponseErrorCode.ContentModified.value) + ) { + throw failure + } + null + } + } + check( + actions.none { + val label = it.asJsonObject["title"].asString + (label.startsWith("Implement ") || label.startsWith("Override ")) && + label.contains(" ${data.text("refusalMember")}(") + }, + ) + check(editor.text == original) + } else { + // Let the native intention own its request. A separate protocol probe can supersede + // that request and leave the native client holding its canceled result. + quickFix(editor, at, title) + awaitUi("generated member matches shared source", 45.seconds) { + editor.text == expected + } + diagnostics(editor, false) + listOf("\$Undo" to original, "\$Redo" to expected, "\$Undo" to original).forEach { (action, text) -> + focusEditor(editor) + invokeAction(action, now = false, component = editor.component) + awaitUi("$action restores the shared member source", 45.seconds) { + editor.text == text + } + diagnostics(editor, broken && text == original) + } + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt new file mode 100644 index 00000000000..29d06131485 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt @@ -0,0 +1,33 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.intellij.driver.client.Remote +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent + +data class Diagnostic( + val severity: String, + val description: String, + val start: Int, +) + +/** + * Export immutable values: the driver's object descriptions evaluate cancellable lazy quick fixes. + */ +fun JEditorUiComponent.installedDiagnostics(): List = + driver.withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + val json = driver.utility(EditorDiagnostics::class).read(editor, driver.singleProject()) + Gson().fromJson(json, Array::class.java).toList() + } + +@Remote("org.xtclang.idea.playbook.probe.EditorDiagnostics", plugin = "org.xtclang.playbook.probe") +interface EditorDiagnostics { + fun read( + editor: Editor, + project: Project, + ): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt new file mode 100644 index 00000000000..f7e2ad2ecef --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt @@ -0,0 +1,312 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.PsiManager +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.elements.accessibleTable +import com.intellij.driver.sdk.ui.components.elements.popup +import com.intellij.driver.sdk.ui.components.elements.tree +import com.intellij.driver.sdk.ui.ui +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +internal fun Driver.semanticSupport(editor: JEditorUiComponent): NativeSemanticSupport = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + val file = + requireNotNull( + service(singleProject()).findFile(editor.editor.getVirtualFile()), + ) + cast(utility(LspFileSupport::class).getSupport(file), NativeSemanticSupport::class) + } + +/** + * Follow every native chooser entry and verify its file/caret, even if navigation retires its + * cache. + */ +internal fun Driver.nativeLocations( + document: ParityWorkspace.Document, + at: Int, + kind: String, + expected: List, +) { + val action = + when (kind) { + "definition" -> "GotoDeclaration" + "typeDefinition" -> "LSP.GotoTypeDefinition" + "implementation" -> "LSP.GotoImplementation" + else -> error("Unsupported native location feature $kind") + } + + fun invoke() { + dismissPopups() + val editor = document.editor + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + editor.scrollToCaretNow() + invokeAction(action, component = editor.component) + } + if (expected.isEmpty()) { + val support = semanticSupport(document.editor) + val feature = + when (kind) { + "definition" -> support.getDefinitionSupport() + "typeDefinition" -> support.getTypeDefinitionSupport() + else -> support.getImplementationSupport() + } + invoke() + awaitUi("native $kind receives no targets", 45.seconds) { + val future = feature.getValidLSPFuture() + future != null && + future.isDone() && + !future.isCompletedExceptionally() && + future.get().isEmpty() + } + dismissPopups() + return + } + val expectedPoints = + expected.map { target -> + Triple( + Path.of(URI(target.string("uri"))).toString(), + target.getAsJsonObject("range").getAsJsonObject("start").int("line"), + target.getAsJsonObject("range").getAsJsonObject("start").int("character"), + ) + } + val opened = + expected.indices.map { index -> + val inspection = PopupInspection(this, document.editor, ::invoke) + invoke() + if (expected.size > 1) { + val popup = ui.popup() + val table = popup.accessibleTable() + awaitUi("native $kind chooser has ${expected.size} targets", 45.seconds) { + inspection.recover() + table.present() && table.content().size == expected.size + } + chooseNavigationRow(table, index) + awaitUi("native $kind chooser closes", 15.seconds) { popup.notPresent() } + } + awaitUi( + message = "$kind opens an exact source target", + timeout = 30.seconds, + getter = { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + service(singleProject()).getSelectedTextEditor()?.let { selected -> + val position = + ParityWorkspace.position( + selected.getDocument().getText(), + selected.getCaretModel().getOffset(), + ) + Triple( + selected.getVirtualFile().getPath(), + position.getValue("line"), + position.getValue("character"), + ) + } + } + }, + checker = { it in expectedPoints }, + ) + } + check(opened.toSet() == expectedPoints.toSet()) { + "Expected $expectedPoints; native destinations: $opened" + } +} + +/** + * Exercise Quick Documentation; read the native request's hover instead of issuing another hover. + */ +internal fun Driver.nativeHover( + document: ParityWorkspace.Document, + at: Int, + pattern: Regex, +) { + dismissPopups() + focusEditor(document.editor) + withContext(OnDispatcher.EDT) { + document.editor.editor + .getCaretModel() + .moveToOffset(at) + } + val support = semanticSupport(document.editor).getHoverSupport() + document.editor.scrollToCaretNow() + invokeAction("QuickJavaDoc", component = document.editor.component) + val protocol = ClientProtocol(this) + awaitUi("native documentation contains $pattern", 45.seconds) { + val future = support.getValidLSPFuture() + future != null && + future.isDone() && + !future.isCompletedExceptionally() && + future.get().any { pattern.containsMatchIn(protocol.copy(it.hover()).toString()) } && + ui.x("//div[@class='DocumentationPopupPane']").present() + } + dismissPopups() +} + +internal fun Driver.nativeHierarchy( + document: ParityWorkspace.Document, + at: Int, + kind: String, + names: List, +) { + focusEditor(document.editor) + withContext(OnDispatcher.EDT) { + document.editor.editor + .getCaretModel() + .moveToOffset(at) + } + document.editor.scrollToCaretNow() + invokeAction( + if (kind == "type") "TypeHierarchy" else "CallHierarchy", + component = document.editor.component, + ) + val className = if (kind == "type") "LSPTypeHierarchyBrowser" else "LSPCallHierarchyBrowser" + val browser = ui.x("//div[@class='$className']") + awaitUi("native $kind hierarchy contains $names", 45.seconds) { + browser.present() && + browser.tree().collectExpandedPaths().let { rows -> + names.all { name -> rows.any { path -> path.path.last().contains(name) } } + } + } +} + +internal fun targetNames( + locations: List, + openText: (String) -> String?, +): List = + locations.map { location -> + val uri = location.string("uri") + val text = openText(uri) ?: Files.readString(Path.of(URI(uri))) + val at = + ParityWorkspace.offset(text, location.getAsJsonObject("range").getAsJsonObject("start")) + Regex("[\\p{L}_$][\\p{L}\\p{N}_$]*").find(text, at)?.takeIf { it.range.first == at }?.value + ?: error("Target does not select a declaration: $location") + } + +@Remote("com.redhat.devtools.lsp4ij.LSPFileSupport", plugin = "com.redhat.devtools.lsp4ij") +interface NativeSemanticSupport { + fun getDefinitionSupport(): NativeLocationsSupport + + fun getTypeDefinitionSupport(): NativeLocationsSupport + + fun getImplementationSupport(): NativeLocationsSupport + + fun getHoverSupport(): NativeHoverSupport + + fun getSemanticTokensSupport(): NativeTokensSupport + + fun getInlayHintsSupport(): NativeInlaysSupport +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.AbstractLSPDocumentFeatureSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeLocationsSupport { + fun getValidLSPFuture(): NativeLocationsFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface NativeLocationsFuture : ClientFuture { + fun get(): List +} + +@Remote("com.redhat.devtools.lsp4ij.usages.LocationData", plugin = "com.redhat.devtools.lsp4ij") +interface NativeLocation { + fun location(): ClientValue +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.documentation.LSPHoverSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeHoverSupport { + fun getValidLSPFuture(): NativeHoverFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface NativeHoverFuture : ClientFuture { + fun get(): List +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.documentation.HoverData", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeHover { + fun hover(): ClientValue +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.semanticTokens.LSPSemanticTokensSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeTokensSupport { + fun getValidLSPFuture(): NativeTokensFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface NativeTokensFuture : ClientFuture { + fun get(): NativeTokens? +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.semanticTokens.SemanticTokensData", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeTokens { + fun getSemanticTokens(): ClientValue +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.inlayhint.LSPInlayHintsSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeInlaysSupport { + fun getValidLSPFuture(): NativeInlaysFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface NativeInlaysFuture : ClientFuture { + fun get(): List +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.inlayhint.InlayHintData", + plugin = "com.redhat.devtools.lsp4ij", +) +interface NativeInlayData { + fun inlayHint(): ClientValue +} + +@Remote("com.intellij.openapi.editor.Editor") +interface NativeInlayEditor { + fun getInlayModel(): NativeInlayModel +} + +@Remote("com.intellij.openapi.editor.InlayModel") +interface NativeInlayModel { + fun getInlineElementsInRange( + start: Int, + end: Int, + ): List +} + +@Remote("com.intellij.openapi.editor.Inlay") +interface NativeInlay { + fun getOffset(): Int + + fun getWidthInPixels(): Int + + fun isValid(): Boolean +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt new file mode 100644 index 00000000000..47f896de9b1 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt @@ -0,0 +1,69 @@ +package org.xtclang.idea.playbook + +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.xtclang.idea.playbook.probe.NavigationChooser +import java.awt.event.ActionEvent +import java.awt.event.KeyEvent +import java.util.concurrent.atomic.AtomicInteger +import javax.swing.AbstractAction +import javax.swing.JComponent +import javax.swing.JTable +import javax.swing.KeyStroke +import javax.swing.SwingUtilities + +class NavigationChooserTest { + @Test + fun `the chosen row reaches the popup action once without keyboard focus`() = + SwingUtilities.invokeAndWait { + val calls = AtomicInteger() + val table = + table { + assertEquals(1, (it.source as JTable).selectedRow) + calls.incrementAndGet() + } + NavigationChooser.choose(table, 1) + assertEquals(1, calls.get()) + } + + @Test + fun `a disabled action refuses before selection or navigation`() = + SwingUtilities.invokeAndWait { + val table = table { error("Disabled action must not run") } + table.actionMap.get("choose").isEnabled = false + assertThrows(IllegalStateException::class.java) { NavigationChooser.choose(table, 1) } + assertEquals(-1, table.selectedRow) + } + + @Test + fun `a missing row or popup binding refuses without choosing a different target`() = + SwingUtilities.invokeAndWait { + val table = table { error("Invalid selection must not run") } + assertThrows(IllegalArgumentException::class.java) { NavigationChooser.choose(table, 2) } + table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).clear() + assertThrows(IllegalArgumentException::class.java) { NavigationChooser.choose(table, 0) } + assertEquals(-1, table.selectedRow) + } + + private fun table(chosen: (ActionEvent) -> Unit): JTable = + object : JTable(2, 1) { + override fun isShowing(): Boolean = true + }.apply { + val enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0) + getInputMap(JComponent.WHEN_FOCUSED).put(enter, "wrong-context") + actionMap.put( + "wrong-context", + object : AbstractAction() { + override fun actionPerformed(event: ActionEvent) = error("Used the focused-cell action") + }, + ) + getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(enter, "choose") + actionMap.put( + "choose", + object : AbstractAction() { + override fun actionPerformed(event: ActionEvent) = chosen(event) + }, + ) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt new file mode 100644 index 00000000000..085a4f3fba0 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt @@ -0,0 +1,157 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import com.intellij.driver.sdk.invokeAction +import kotlin.math.abs +import kotlin.math.roundToInt +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.colorCases() { + listOf("X273", "X274", "X275").forEach { id -> + case(id) { data -> + val values = common.getAsJsonObject("colorPrototype") + val source = values.string("source") + val document = open(values.string("file"), source) + clean(document) + check(protocol.capabilities().asJsonObject["colorProvider"].asBoolean) + val expected = values["colors"].rows() + + fun colors() = + query("textDocument/documentColor", document).rows().sortedWith( + compareBy( + { it["range"].asJsonObject["start"].asJsonObject.int("line") }, + { it["range"].asJsonObject["start"].asJsonObject.int("character") }, + ), + ) + + fun rgba() = + colors().map { color -> + listOf("red", "green", "blue", "alpha").map { (color["color"].asJsonObject[it].asDouble * 255).roundToInt() } + } + check(rgba() == expected.map { it["rgba"].asJsonArray.map { channel -> channel.asInt } }) + with(driver) { + val editor = document.editor + val probe = utility(ColorProbe::class) + + fun offsets() = withContext(OnDispatcher.EDT) { probe.offsets(editor.editor) } + + fun visible() = withContext(OnDispatcher.EDT) { probe.visible() } + + fun renderedColors(expected: List>) { + awaitUi( + "rendered swatches match current constructor values", + 15.seconds, + errorMessage = { "Expected rendered RGBA $expected, actual $it" }, + getter = { withContext(OnDispatcher.EDT) { probe.rgba(editor.editor) }.chunked(4) }, + // Java2D alpha compositing can round a channel by one byte. + checker = { actual -> + actual.size == expected.size && + actual.zip(expected).all { (painted, wanted) -> + painted.zip(wanted).all { (channel, value) -> abs(channel - value) <= 1 } + } + }, + ) + } + + fun openPicker(index: Int) { + focusEditor(editor) + withContext(OnDispatcher.EDT) { probe.open(editor.editor, offsets()[index]) } + awaitUi("native color chooser opens", 15.seconds) { visible() } + } + + fun dismiss() { + withContext(OnDispatcher.EDT) { probe.dismiss() } + awaitUi("native color chooser closes", 10.seconds) { !visible() } + } + val expectedOffsets = expected.map { source.indexOf(it.string("anchor")) } + awaitUi("exactly two rendered swatches, none on dynamic values or ordinary strings", 30.seconds) { + offsets() == expectedOffsets + } + renderedColors(rgba()) + withContext(OnDispatcher.EDT) { probe.screenshot(editor.editor, directory.resolve("$id-swatches.png").toString()) } + if (data.string("mode") == "recovery") { + val invalid = values.getAsJsonObject("invalid") + replace(document, source.replace(invalid.string("from"), invalid.string("to"))) + diagnostics(document) { it.isNotEmpty() } + check(colors().isEmpty()) + awaitUi("failed compilation retires native swatches", 30.seconds) { offsets().isEmpty() } + replace(document, source) + clean(document) + awaitUi("source repair restores native swatches", 30.seconds) { offsets() == expectedOffsets } + renderedColors(rgba()) + return@case + } + openPicker(0) + withContext(OnDispatcher.EDT) { probe.screenshot(editor.editor, directory.resolve("$id-picker.png").toString()) } + if (data.string("mode") == "history") { + dismiss() + check(document.text == source) { "Dismissing the picker must not edit source" } + openPicker(0) + } + withContext(OnDispatcher.EDT) { probe.change("alpha") } + val changed = source.replace(expected[0].string("anchor"), "new Rgba(255, 128, 0, alpha = 128)") + awaitUi("native alpha field applies exact source-preserving edit", 15.seconds) { document.text == changed } + dismiss() + clean(document) + check(rgba()[0] == listOf(255, 128, 0, 128)) + renderedColors(rgba()) + if (data.string("mode") == "history") { + focusEditor(editor) + invokeAction("\$Undo", component = editor.component) + awaitUi("one Undo restores the original constructor", 15.seconds) { document.text == source } + settle(document) + check(rgba()[0] == listOf(255, 128, 0, 255)) + renderedColors(rgba()) + invokeAction("\$Redo", component = editor.component) + awaitUi("one Redo restores the picker edit", 15.seconds) { document.text == changed } + clean(document) + renderedColors(rgba()) + } else { + openPicker(1) + withContext(OnDispatcher.EDT) { probe.change("rgb") } + val named = changed.replace(expected[1].string("anchor"), "new Rgba(blue = 192, red = 32, alpha = 128, green = 96)") + awaitUi("native RGB picker preserves named argument order", 15.seconds) { document.text == named } + dismiss() + clean(document) + check(rgba()[1] == listOf(32, 96, 192, 128)) + renderedColors(rgba()) + captureColorScheme(document) + } + } + } + } +} + +/** Capture the installed scheme without cycling the visible editor through other palettes. */ +internal fun ParityWorkspace.captureColorScheme(document: ParityWorkspace.Document) { + with(driver) { + withContext(OnDispatcher.EDT) { + utility(ColorProbe::class).screenshot(document.editor.editor, directory.resolve("$id-current-theme.png").toString()) + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.ColorUi", plugin = "org.xtclang.playbook.probe") +internal interface ColorProbe { + fun offsets(editor: Editor): List + + fun rgba(editor: Editor): List + + fun open( + editor: Editor, + offset: Int, + ) + + fun visible(): Boolean + + fun change(channel: String) + + fun dismiss() + + fun screenshot( + editor: Editor, + file: String, + ) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt new file mode 100644 index 00000000000..5e6a0c94ae2 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt @@ -0,0 +1,315 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonParser +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Shared real Gradle producer; assertions go through installed settings, progress and diagnostics. */ +internal fun ParityScenarios.compilerImportCases() { + listOf("X260", "X261", "X262", "X263", "X264").forEach { id -> + case(id) { data -> + val root = with(driver) { Path.of(singleProject().getBasePath()) } + check( + root.fileName.toString() == "workspace" && + root.parent.fileName + .toString() + .startsWith("run-"), + ) + val shared = Path.of(System.getProperty("xtc.playbook.scenarios")).parent + val repository = shared.parent.parent.parent + val fixture = common["compilerImport"].asJsonObject + val files = + listOf( + "build.gradle.kts", + "settings.gradle.kts", + "gradle.properties", + "gradlew", + "gradlew.bat", + "gradle/wrapper/gradle-wrapper.jar", + "gradle/wrapper/gradle-wrapper.properties", + ) + // Fail before touching a pre-existing build; every test owns an isolated workspace. + files.forEach { check(!Files.exists(root.resolve(it))) { "Fixture would replace $it" } } + val control = Files.createDirectories(root.resolve(".compiler-import-playbook")) + val report = root.resolve(".gradle/xtc/lsp-model.json") + val previousReport = report.takeIf(Files::exists)?.let(Files::readAllBytes) + val aggregate = root.resolve(".gradle/xtc/lsp-workspace.json") + val previousAggregate = aggregate.takeIf(Files::exists)?.let(Files::readAllBytes) + val model = + JsonParser + .parseString( + fixture["model"].toString().replace("\${workspace}", directory.toUri().toString().trimEnd('/')), + ).asJsonObject + val replacement = + model.deepCopy().apply { + getAsJsonArray( + "sourceSets", + )[0].asJsonObject.getAsJsonArray("resourceRoots").asList().clear() + } + val steps = data["steps"].rows() + + fun refresh() = + with(driver) { + withContext(OnDispatcher.EDT) { utility(CompilerSettingsPage::class).refreshBuildModel(singleProject()) } + } + + try { + Files.deleteIfExists(aggregate) + Files.writeString(control.resolve("invocations.txt"), "") + files.forEach { file -> + val source = + if (file.contains("gradlew") || + file.startsWith("gradle/") + ) { + repository.resolve(file) + } else { + shared.resolve("compiler-import/$file") + } + Files.createDirectories(root.resolve(file).parent) + Files.copy(source, root.resolve(file)) + } + if (!System.getProperty("os.name").startsWith("Windows")) check(root.resolve("gradlew").toFile().setExecutable(true)) + data["included"]?.rows()?.forEach { build -> + val target = Files.createDirectories(root.resolve(build.string("folder"))) + val gate = Files.createDirectories(target.resolve(".compiler-import-playbook")) + Files.copy(shared.resolve("compiler-import/build.gradle.kts"), target.resolve("build.gradle.kts")) + Files.writeString( + target.resolve("settings.gradle.kts"), + "rootProject.name = \"${build.string("module")}\"\n" + + if (build.string("folder") == "included") "includeBuild(\"nested\")\n" else "", + ) + val nested = + fixture["model"] + .toString() + .replace("\${workspace}", target.toUri().toString().trimEnd('/')) + .replace(fixture.string("module"), build.string("module")) + Files.writeString( + target.resolve("${build.string("module")}.x"), + fixture.string("source").replace(fixture.string("module"), build.string("module")), + ) + Files.createDirectories(target.resolve("processed")) + Files.writeString(target.resolve(fixture.string("resource")), fixture.string("contents")) + Files.writeString(gate.resolve("model.json"), nested) + Files.writeString(gate.resolve("request.properties"), "operation=included\noutput=valid\noutcome=success\n") + Files.writeString(gate.resolve("included.release"), "release\n") + } + if (data.has( + "included", + ) + ) { + Files.writeString( + root.resolve("settings.gradle.kts"), + "rootProject.name = \"composite\"\nincludeBuild(\"included\")\n", + ) + } + write(fixture.string("file"), fixture.string("source")) + write(fixture.string("resource"), fixture.string("contents")) + Files.createDirectories(report.parent) + Files.writeString(report, model.toString()) + with(driver) { withContext(OnDispatcher.EDT) { utility(CompilerSettingsPage::class).useBuildModel(singleProject()) } } + val document = open(fixture.string("file")) + clean(document) + val page = with(driver) { withContext(OnDispatcher.EDT) { utility(CompilerImportPage::class).open(singleProject()) } } + + fun text() = with(driver) { withContext(OnDispatcher.EDT) { page.text() } } + + fun click(label: String) = with(driver) { withContext(OnDispatcher.EDT) { page.click(label) } } + val baseline = page.accepted() + check(baseline != "null") + try { + steps.forEach { step -> + val operation = "$id-${step.string("operation")}" + val prepare = step["prepare"].asBoolean + val title = if (prepare) "Prepare Ecstasy compiler inputs" else "Refresh Ecstasy compiler paths" + Files.writeString(control.resolve("model.json"), replacement.toString()) + Files.writeString( + control.resolve("request.properties"), + "operation=$operation\noutput=${step.string("output")}\noutcome=${step.string("outcome")}\n", + ) + if (step.has("automatic")) { + with(driver) { withContext(OnDispatcher.EDT) { page.sync() } } + } else { + click(if (prepare) "Prepare generated resources" else "Refresh Gradle model") + } + awaitUi("$id real Gradle producer reached its publication gate", 45.seconds) { + control.resolve("$operation.started").takeIf(Files::exists)?.let(Files::readString) == + if (prepare) "prepareXtcLspModel" else "exportXtcLspModel" + } + refresh() + check(page.accepted() == baseline) { "Pending output replaced the accepted report" } + clean(document) + if (step["duplicate"].asBoolean) { + click("Refresh Gradle model") + awaitUi("Duplicate import is refused in the settings page", 10.seconds) { text().contains("already running") } + } + if (step["cancel"].asBoolean) { + with(driver) { + withContext(OnDispatcher.EDT) { utility(ProgressUi::class).show(singleProject()) } + awaitUi("$id visible import Cancel button", 15.seconds) { + withContext(OnDispatcher.EDT) { utility(ProgressUi::class).cancel(singleProject(), title) } + } + } + } else { + Files.writeString(control.resolve("$operation.release"), "release\n") + } + awaitUi("$id import completion status", 30.seconds) { + val description = page.description() + description.contains("Last import:") && description.contains(step.string("expected"), ignoreCase = true) && + with( + driver, + ) { withContext(OnDispatcher.EDT) { !utility(ProgressUi::class).visible(singleProject(), title) } } + } + Files.writeString(control.resolve("$operation.release"), "cleanup\n") + val producer = Files.readString(control.resolve("$operation.pid")).toLong() + check(producer > 0) + awaitUi("$id Gradle fixture action retired or its cancelled JVM exited", 15.seconds) { + Files.exists(control.resolve("$operation.finished")) || + ( + step["cancel"].asBoolean && + ProcessHandle + .of(producer) + .map { it.isAlive } + .orElse(false) + .not() + ) + } + refresh() + with(driver) { withContext(OnDispatcher.EDT) { page.resetPage() } } + awaitUi("$id settings page displays the last outcome", 10.seconds) { + text().contains("Last import:") && text().contains(step.string("expected"), ignoreCase = true) + } + if (step["accepted"].asBoolean) { + check(page.accepted() != baseline) + data["included"]?.rows()?.let { included -> + val accepted = JsonParser.parseString(page.accepted()).asJsonObject + check(accepted["sourceSets"].asJsonArray.size() == included.size + 1) + check(accepted["buildRoots"].asJsonArray.size() == included.size + 1) + } + errors(document) + } else { + check(page.accepted() == baseline) { "Rejected report was accepted by a later consumer" } + clean(document) + } + check(Files.readAllLines(control.resolve("invocations.txt")).count { it.startsWith("$operation:") } == 1) + check(document.text == fixture.string("source")) + } + if (id == "X264") { + Files.writeString(aggregate, "{") + with(driver) { withContext(OnDispatcher.EDT) { page.refreshReports() } } + Files.writeString(aggregate, model.toString()) + with(driver) { withContext(OnDispatcher.EDT) { page.refreshReports() } } + // Consume the real VFS notification; do not call publish from the driver. + clean(document) + } + } finally { + if (id == "X264") with(driver) { withContext(OnDispatcher.EDT) { page.unlink() } } + with(driver) { withContext(OnDispatcher.EDT) { page.closePage() } } + } + } finally { + steps.forEach { Files.writeString(control.resolve("$id-${it.string("operation")}.release"), "cleanup\n") } + with(driver) { withContext(OnDispatcher.EDT) { utility(ProgressUi::class).hide(singleProject()) } } + files.forEach { Files.deleteIfExists(root.resolve(it)) } + if (previousReport == null) Files.deleteIfExists(report) else Files.write(report, previousReport) + if (previousAggregate == null) Files.deleteIfExists(aggregate) else Files.write(aggregate, previousAggregate) + if (data.has("included")) root.resolve("included").toFile().deleteRecursively() + with(driver) { withContext(OnDispatcher.EDT) { utility(CompilerSettingsPage::class).clearProjectGraph(singleProject()) } } + refresh() + } + } + } + case("X265") { data -> + val primary = with(driver) { singleProject() } + val root = Files.createDirectories(Path.of(primary.getBasePath()).parent.resolve("retired-import")) + val fixture = common["compilerImport"].asJsonObject + val shared = Path.of(System.getProperty("xtc.playbook.scenarios")).parent + val repository = shared.parent.parent.parent + val control = Files.createDirectories(root.resolve(".compiler-import-playbook")) + listOf( + "build.gradle.kts", + "settings.gradle.kts", + "gradle.properties", + "gradlew", + "gradlew.bat", + "gradle/wrapper/gradle-wrapper.jar", + "gradle/wrapper/gradle-wrapper.properties", + ).forEach { file -> + Files.createDirectories(root.resolve(file).parent) + Files.copy( + if (file.contains("gradlew") || file.startsWith("gradle/")) { + repository.resolve(file) + } else { + shared.resolve("compiler-import/$file") + }, + root.resolve(file), + ) + } + if (!System.getProperty("os.name").startsWith("Windows")) check(root.resolve("gradlew").toFile().setExecutable(true)) + val model = fixture["model"].toString().replace("\${workspace}", root.toUri().toString().trimEnd('/')) + Files.writeString(root.resolve(fixture.string("file")), fixture.string("source")) + Files.createDirectories(root.resolve("processed")) + Files.writeString(root.resolve(fixture.string("resource")), fixture.string("contents")) + val report = root.resolve(".gradle/xtc/lsp-model.json") + Files.createDirectories(report.parent) + Files.writeString(report, model) + val operation = data.string("operation") + Files.writeString(control.resolve("model.json"), model) + Files.writeString(control.resolve("request.properties"), "operation=$operation\noutput=valid\noutcome=success\n") + val first = ClientProtocol(driver) { primary } + val firstPid = first.server().getCurrentProcessId() + val probe = with(driver) { utility(ProjectLifecycle::class) } + val opening = probe.open(root.toString()) + awaitUi("Second native project opens", 60.seconds) { opening.isDone() } + val secondary = requireNotNull(opening.get()) + try { + with(driver) { withContext(OnDispatcher.EDT) { utility(CompilerSettingsPage::class).useBuildModel(secondary) } } + val page = with(driver) { withContext(OnDispatcher.EDT) { utility(CompilerImportPage::class).open(secondary) } } + check(page.accepted() != "null") + with(driver) { withContext(OnDispatcher.EDT) { page.click("Refresh Gradle model") } } + awaitUi("Secondary project import is pending", 45.seconds) { Files.exists(control.resolve("$operation.started")) } + with(driver) { withContext(OnDispatcher.EDT) { page.closePage() } } + val closing = probe.close(secondary) + awaitUi("Project closes while its compiler import is pending", 30.seconds) { closing.isDone() } + check(closing.get()) + Files.writeString(control.resolve("$operation.release"), "late completion\n") + val producer = Files.readString(control.resolve("$operation.pid")).toLong() + awaitUi("Closed project producer retires", 30.seconds) { + Files.exists(control.resolve("$operation.finished")) || !ProcessHandle.of(producer).map { it.isAlive }.orElse(false) + } + check(first.server().getCurrentProcessId() == firstPid) + first.query("xtc/healthCheck", emptyMap()) + write("Survivor.x", "module Survivor {}\n") + clean(open("Survivor.x")) + } finally { + Files.writeString(control.resolve("$operation.release"), "cleanup\n") + runCatching { probe.close(secondary) } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.CompilerImportPage", plugin = "org.xtclang.playbook.probe") +internal interface CompilerImportPage { + fun open(project: Project): CompilerImportPage + + fun click(label: String) + + fun text(): String + + fun accepted(): String + + fun description(): String + + fun resetPage() + + fun sync() + + fun unlink() + + fun refreshReports() + + fun closePage() +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDependencies.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDependencies.kt new file mode 100644 index 00000000000..3a8b40d7103 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDependencies.kt @@ -0,0 +1,150 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.singleProject +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.dependencyCases() { + case("X47") { data -> + val consumer = dependencies() + val library = open(data.string("file")) + replace(library, common["dependency"].asJsonObject.string("brokenLibrary")) + diagnostics(library) { items -> + items.any { it.code?.startsWith(data.string("compilerCodePrefix")) == true } + } + blocked() + check( + targets(consumer, "definition", consumer.at(data.string("anchor"), data.int("offset"))) + .isEmpty(), + ) + replace(library, fixture(library.file)) + clean(library) + linked(consumer) + } + case("X48") { data -> + val consumer = dependencies() + val library = open(data.string("file")) + replace(library, common["dependency"].asJsonObject.string("stringLibrary")) + errors(consumer) + discard(library) + linked(consumer) + check(open(library.file).text == fixture(library.file)) + } + case("X49") { data -> + val consumer = dependencies() + val file = data.string("file") + delete(file) + published(file) { items -> + items.any { it.string("code") == data.string("diagnosticCode") } + } + blocked() + write(file) + linked(consumer) + published(file) { it.isEmpty() } + } + case("X50") { data -> + val consumer = dependencies() + val file = data.string("file") + virtual(file, data.string("text"), data.int("version")) { + published(file) { it.isNotEmpty() } + blocked() + } + linked(consumer) + published(file) { it.isEmpty() } + write(file, data.string("text")) + published(file) { it.isNotEmpty() } + blocked() + delete(file) + linked(consumer) + published(file) { it.isEmpty() } + } + case("X51") { data -> + val consumer = dependencies() + val unrelated = open(data.string("unrelatedFile")) + val library = open(data.string("libraryFile")) + val pending = + (0 until 10).map { index -> + replace( + library, + if (index % 2 == 1) { + fixture(library.file) + } else { + common["dependency"].asJsonObject.string("brokenLibrary") + }, + settle = false, + ) + protocol.request( + "textDocument/definition", + consumer.params(consumer.at(data.string("anchor"), data.int("offset"))), + ) + } + pending.forEach { awaitRetired("textDocument/definition", it) } + settle(library) + linked(consumer) + clean(unrelated) + check(query("textDocument/documentSymbol", unrelated).rows().isNotEmpty()) + } + case("X52") { data -> + write(data.string("libraryFile")) + write(data.string("bridgeFile"), data.string("bridgeText")) + write( + data.string("consumerFile"), + fixture(data.string("consumerFile")) + .replace(data.string("replaceFrom"), data.string("bridgeImport")), + ) + configure(data["sourceModules"]) + val consumer = open(data.string("consumerFile")) + clean(consumer) + val library = open(data.string("libraryFile")) + replace(library, common["dependency"].asJsonObject.string("stringLibrary")) + published(data.string("bridgeFile")) { it.isNotEmpty() } + blocked() + replace(library, common["dependency"].asJsonObject.string("brokenLibrary")) + blocked() + replace(library, fixture(library.file)) + clean(consumer) + val bridge = open(data.string("bridgeFile")) + replace(bridge, data.string("brokenBridge")) + blocked() + replace(bridge, data.string("bridgeText")) + clean(consumer) + check( + targets(consumer, "definition", consumer.at(data.string("anchor"), data.int("offset"))) + .single() + .string("uri") == bridge.uri, + ) + } + case("CFG2") { data -> + val consumer = dependencies() + val before = trace.notifications("window/showMessage").size + configure(data["sourceModules"]) + with(driver) { + awaitUi("client receives rejection of cyclic graph", 45.seconds) { + trace.notifications("window/showMessage").drop(before).any { + data.pattern("pattern").containsMatchIn(it.string("message")) + } + } + } + linked(consumer) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class) + .dismissExpectedConfigurationError(singleProject()) + } + } + } + case("CFG3") { + val consumer = dependencies() + val version = version(consumer) + protocol.notify("workspace/didChangeConfiguration", mapOf("settings" to null)) + linked(consumer) + check(version(consumer) == version) + } +} + +private fun ParityWorkspace.blocked() { + val dependency = common["dependency"].asJsonObject + published(dependency.string("consumer")) { items -> + items.any { it.string("code") == dependency.string("blockedCode") } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDocumentation.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDocumentation.kt new file mode 100644 index 00000000000..ef8a5baf300 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityDocumentation.kt @@ -0,0 +1,37 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.invokeAction +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.documentationCases() { + case("X278") { data -> + val source = data.string("source") + val expected = data.string("expected") + val document = open(data.string("file"), source) + configure(listOf(SharedScenarios.SourceModule("Documentation", document.uri, emptyList()))) + clean(document) + with(driver) { quickFix(document.editor, document.at(data.string("anchor")), data.string("title")) } + awaitUi("documentation matches the compiler signature", 30.seconds) { document.text == expected } + clean(document) + val at = ParityWorkspace.position(document.text, document.at(data.string("anchor"))) + val actions = + query( + "textDocument/codeAction", + document, + extra = + mapOf( + "range" to mapOf("start" to at, "end" to at), + "context" to mapOf("diagnostics" to emptyList()), + ), + ).rows() + check(actions.none { it.string("title") == data.string("title") }) + listOf("\$Undo" to source, "\$Redo" to expected, "\$Undo" to source).forEach { (action, text) -> + with(driver) { + focusEditor(document.editor) + invokeAction(action, now = false, component = document.editor.component) + } + awaitUi("$action restores documentation", 30.seconds) { document.text == text } + clean(document) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityGraph.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityGraph.kt new file mode 100644 index 00000000000..08aafc36837 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityGraph.kt @@ -0,0 +1,313 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path + +internal fun ParityScenarios.graphCases() { + case("X59") { data -> + val document = graph() + val result = graphReferences(document) + check(result.size == data.int("referenceCount")) + check( + result.map { it.string("uri") }.sorted() == + data + .strings("modules") + .map { uri(SharedScenarios.text(data.string("moduleFile"), it)) } + .sorted(), + ) + check( + protocol.server().getOpenedDocuments().none { + it.getFile().getPath() == + directory.resolve(data.string("closedConsumer")).toString() + }, + ) + } + case("X60") { data -> + val document = graph() + val proposed = + proposedRename(document, document.at(data.string("anchor")), data.string("replaceWith")) + .asJsonObject + check( + proposed["changes"].let { it == null || it.isJsonNull || it.asJsonObject.size() == 0 }, + ) { + proposed.toString() + } + val changes = proposed["documentChanges"].rows() + check(changes.size == data.int("moduleCount")) + val currentVersion = version(document) + changes.forEach { change -> + val target = change["textDocument"].asJsonObject + if (target.string("uri") == document.uri) { + check(target.int("version") == currentVersion) + } else { + check(target["version"] == null || target["version"].isJsonNull) + } + } + applyRename(document, document.at(data.string("anchor")), data.string("replaceWith")) + data.strings("modules").forEach { name -> + val file = SharedScenarios.text(data.string("file"), name) + val expected = fixture(file).replace(data.pattern("replaceFrom"), data.string("replaceWith")) + // UP21 persists closed consumers for the compiler; the open editor stays unsaved. + val expectedDisk = if (file == document.file) fixture(file) else expected + check(Files.readString(directory.resolve(file)) == expectedDisk) { + "Rename must preserve the open buffer's disk text and persist closed consumers: $file" + } + val changed = open(file) + clean(changed) + check(changed.text == expected) + } + } + case("X61") { data -> + val document = graph() + check( + proposedRename(document, document.at(data.string("anchor")), data.string("newName")) + .isJsonNull, + ) + check(document.text == fixture(data.string("file"))) + clean(document) + } + case("X62") { data -> + graph() + val document = open(data.string("file")) + clean(document) + val search = data.string("searchCall") + with(driver) { + signature(document.editor, document.at(search, search.length)) { signatures -> + signatures.any { + data.pattern("searchName").containsMatchIn(it.label) && + data.pattern("searchArgumentType").containsMatchIn(it.label) + } + } + nativeHover( + document, + document.at(data.string("descriptionCall")), + data.pattern("descriptionType"), + ) + } + check(proposedRename(document, document.at(search), data.string("searchRename")).isJsonNull) + check( + proposedRename( + document, + document.at(data.string("descriptionCall")), + data.string("descriptionRename"), + ).isJsonNull, + ) + } + case("X63") { data -> + val document = graph() + val consumer = open(data.string("file")) + replace( + consumer, + fixture(consumer.file) + .replace(data.string("replaceFrom"), data.string("addedReference")), + ) + check( + graphReferences(document).count { it.string("uri") == consumer.uri } == + data.int("consumerReferenceCount"), + ) + val edit = + proposedRename(document, document.at(data.string("anchor")), data.string("newName")) + .asJsonObject + val change = + edit["documentChanges"].rows().single { + it["textDocument"].asJsonObject.string("uri") == consumer.uri + } + check(change["textDocument"].asJsonObject.int("version") == version(consumer)) + replace(consumer, data.string("replaceWith")) + errors(consumer) + check(graphReferences(document).isEmpty()) + check( + proposedRename(document, document.at(data.string("anchor")), data.string("newName")) + .isJsonNull, + ) + clean(document) + replace(consumer, fixture(consumer.file)) + clean(consumer) + check(graphReferences(document).size == data.int("referenceCount")) + } +} + +internal fun ParityScenarios.indexingCases() { + case("X251") { data -> + val roots = + (0 until data.int("roots")).map { index -> + val file = SharedScenarios.text(data.string("rootFile"), index.toString()) + write(file, SharedScenarios.text(data.string("rootSource"), index.toString())) + SharedScenarios.SourceModule(SharedScenarios.text(data.string("rootName"), index.toString()), uri(file), emptyList()) + } + val consumer = data["consumer"].asJsonObject + write(consumer.string("uri"), consumer.string("source")) + val graph = + roots + SharedScenarios.SourceModule(consumer.string("name"), uri(consumer.string("uri")), consumer.strings("dependencies")) + configure(graph) + val document = open(data.string("file")) + clean(document) + + fun refs() = + query( + "textDocument/references", + document, + document.at(data.string("anchor")), + mapOf("context" to mapOf("includeDeclaration" to true)), + ).rows() + + fun search(value: String) = protocol.query("workspace/symbol", mapOf("query" to value)).rows() + check(refs().size == data.int("referenceCount")) + check( + protocol.server().getOpenedDocuments().none { it.getFile().getPath() == directory.resolve(consumer.string("uri")).toString() }, + ) + check(search(data.string("symbol")).size == data.int("roots")) + check(search(data.string("symbol")).size == data.int("roots")) + replace(document, document.text.replace(data.string("replaceFrom"), data.string("replacement"))) + clean(document) + check(search(data.string("addedSymbol")).size == 1) + check(refs().size == data.int("referenceCount")) + val last = data.int("roots") - 1 + val broken = open(SharedScenarios.text(data.string("rootFile"), last.toString())) + replace(broken, SharedScenarios.text(data.string("brokenSource"), last.toString())) + errors(broken) + check(refs().isEmpty()) + check(search(data.string("addedSymbol")).size == 1) + replace(broken, SharedScenarios.text(data.string("rootSource"), last.toString())) + clean(broken) + check(refs().size == data.int("referenceCount")) + configure(graph.filter { it.name != roots.last().name }) + with(driver) { awaitUi("removed root leaves index") { search(data.string("symbol")).size == data.int("roots") - 1 } } + configure(graph) + with(driver) { awaitUi("restored root returns to index") { search(data.string("symbol")).size == data.int("roots") } } + } +} + +private fun ParityWorkspace.graph(): ParityWorkspace.Document { + val setup = common["graph"].asJsonObject + setup["sourceModules"].rows().forEach { write(it.string("uri")) } + configure(setup["sourceModules"]) + return open(setup.string("root")).also(::clean) +} + +private fun ParityWorkspace.graphReferences(document: ParityWorkspace.Document): List = + query( + "textDocument/references", + document, + document.at(common["graph"].asJsonObject.string("referenceAnchor")), + mapOf("context" to mapOf("includeDeclaration" to true)), + ).rows() + +internal fun ParityScenarios.monikerCases() { + case("X252") { data -> + write(data.string("libraryFile"), data.string("librarySource")) + write(data.string("consumerFile"), data.string("consumerSource")) + configure(data["sourceModules"]) + val consumer = open(data.string("consumerFile")) + clean(consumer) + + fun imports() = query("textDocument/moniker", consumer, consumer.at(data.string("reference"))).rows() + val imported = imports().single() + val text = data.string("librarySource") + val offset = text.indexOf(data.string("declaration")) + check(offset >= 0) + val exported = + protocol + .query( + "textDocument/moniker", + mapOf( + "textDocument" to mapOf("uri" to uri(data.string("libraryFile"))), + "position" to + mapOf( + "line" to text.take(offset).count { it == '\n' }, + "character" to offset - text.lastIndexOf('\n', offset) - 1, + ), + ), + ).rows() + .single() + check(imported.string("scheme") == data.string("scheme")) + check(imported.string("unique") == data.string("unique")) + check(exported.string("kind") == "export") + check(imported == exported.deepCopy().apply { addProperty("kind", "import") }) + val overload = query("textDocument/moniker", consumer, consumer.at(data.string("overload"))).rows().single() + check(overload.string("identifier") != imported.string("identifier")) + val library = open(data.string("libraryFile")) + clean(library) + + fun exports() = query("textDocument/moniker", library, library.at(data.string("declaration"))).rows().single() + check(exports() == exported) + replace(library, text.replace(data.string("replaceFrom"), data.string("replaceWith"))) + clean(consumer) + val changed = exports() + check(changed.string("identifier") != exported.string("identifier")) + check(imports().single() == changed.deepCopy().apply { addProperty("kind", "import") }) + replace(library, text) + clean(consumer) + check(imports().single() == imported) + } + case("X253") { data -> + write(data.string("file"), data.string("source")) + val document = open(data.string("file")) + clean(document) + + fun monikers(key: String) = query("textDocument/moniker", document, document.at(data.string(key))).rows() + val own = monikers("private").single() + val bundled = monikers("library").single() + check(own.string("kind") == "local") + check(bundled.string("kind") == "import") + check(bundled.string("scheme") == data.string("scheme")) + check(bundled.string("unique") == data.string("unique")) + check(monikers("local").isEmpty()) + replace(document, data.string("source").replace(data.string("replaceFrom"), data.string("replaceWith"))) + errors(document) + check(monikers("private").isEmpty()) + replace(document, data.string("source")) + clean(document) + check(monikers("private").single() == own) + check(monikers("library").single() == bundled) + } +} + +internal fun ParityScenarios.libraryContentCases() { + case("X254") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + data.strings("types").forEach { name -> + val at = document.at(name) + val imported = query("textDocument/moniker", document, at).rows().single() + val target = query("textDocument/definition", document, at).rows().single() + val uri = target.string("uri") + if (replacementGate("UP25")) { + check(URI(uri).scheme == data.string("scheme")) { "Local UP25 gate requires virtual content: $uri" } + nativeLibraryContent(document, at, target, imported, data.string("monikerScheme")) + return@forEach + } + // TODO LSP4IJ: UP25 — retain native read-only file acceptance until upstream supports + // workspace/textDocumentContent, virtual URI resolution and refresh. + check(URI(uri).scheme == "file") + val path = Path.of(URI(uri)) + check(!Files.isWritable(path)) + val library = open(path.toString()) + val text = library.text + val start = target["range"].asJsonObject["start"].asJsonObject + check(text.lines()[start.int("line")].substring(start.int("character")).startsWith(name)) + val exported = + protocol + .query( + "textDocument/moniker", + mapOf( + "textDocument" to mapOf("uri" to uri), + "position" to start, + ), + ).rows() + .single() + check(exported == imported.deepCopy().apply { addProperty("kind", "export") }) + check(exported.string("scheme") == data.string("monikerScheme")) + check( + query( + "textDocument/formatting", + library, + extra = mapOf("options" to mapOf("tabSize" to 4, "insertSpaces" to true)), + ).rows().isEmpty(), + ) + check(library.text == text && Files.readString(path) == text) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt new file mode 100644 index 00000000000..79f28286b18 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt @@ -0,0 +1,147 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import kotlin.time.Duration.Companion.seconds + +private data class HighlightToken( + val start: Int, + val end: Int, + val text: String, + val type: String, + val modifiers: Set, +) + +private fun ParityWorkspace.highlightTokens(doc: ParityWorkspace.Document): List { + val expected = + query("textDocument/semanticTokens/full", doc) + .takeUnless { it.isJsonNull } + ?.asJsonObject + ?.get("data") + ?.asJsonArray + ?.map { it.asInt } + .orEmpty() + val support = with(driver) { semanticSupport(doc.editor).getSemanticTokensSupport() } + val values = + awaitUi("native semantic highlighting uses the current document", 30.seconds, getter = { + support.getValidLSPFuture()?.takeIf { it.isDone() && !it.isCompletedExceptionally() }?.let { future -> + future + .get() + ?.let { + protocol + .copy(it.getSemanticTokens()) + .asJsonObject["data"] + .asJsonArray + .map { number -> number.asInt } + }.orEmpty() + } + }, checker = { it == expected })!! + val legend = + protocol + .capabilities() + .asJsonObject["semanticTokensProvider"] + .asJsonObject["legend"] + .asJsonObject + val lines = doc.text.lines() + var line = 0 + var column = 0 + return values.chunked(5).map { tuple -> + line += tuple[0] + column = (if (tuple[0] == 0) column else 0) + tuple[1] + check(line < lines.size && column + tuple[2] <= lines[line].length) { "Stale semantic token $tuple" } + val start = lines.take(line).sumOf { it.length + 1 } + column + HighlightToken( + start, + start + tuple[2], + doc.text.substring(start, start + tuple[2]), + legend["tokenTypes"].asJsonArray[tuple[3]].asString, + legend["tokenModifiers"] + .asJsonArray + .mapIndexedNotNull { + bit, + name, + -> + name.asString.takeIf { tuple[4] and (1 shl bit) != 0 } + }.toSet(), + ) + } +} + +internal fun ParityScenarios.highlightingCases() { + case("X276") { data -> + val doc = open(data.string("file"), data.string("source")) + clean(doc) + val tokens = highlightTokens(doc) + data["tokens"].rows().forEach { expected -> + val start = doc.at(expected.string("anchor"), expected.int("offset")) + val token = tokens.single { it.start == start } + check(token.text == expected.string("text") && token.type == expected.string("type")) { "$expected; $token" } + check(("defaultLibrary" in token.modifiers) == expected["library"].asBoolean) { "$expected; $token" } + } + val probe = with(driver) { utility(HighlightingProbe::class) } + val annotationOffset = doc.at("@Marked", 1) + awaitUi("native annotation decorator markup is installed", 30.seconds) { + with(driver) { + withContext(OnDispatcher.EDT) { + probe.semanticOverlays(doc.editor.editor, annotationOffset).any { "DECORATOR" in it } + } + } + } + } + case("X277") { data -> + val source = data.string("source") + val doc = open(data.string("file"), source) + clean(doc) + val before = highlightTokens(doc) + val offsets = + data["lexical"].asJsonArray.flatMap { item -> + val start = doc.at(item.asString) + (start until start + item.asString.length).toList() + } + check(before.none { token -> offsets.any { it in token.start until token.end } }) + val probe = with(driver) { utility(HighlightingProbe::class) } + with(driver) { + withContext(OnDispatcher.EDT) { + data["matchingStyles"].rows().forEach { pair -> + val left = doc.at(pair.string("left")) + val right = doc.at(pair.string("right")) + (0 until pair.int("length")).forEach { index -> + check(probe.lexical(doc.editor.editor, left + index) == probe.lexical(doc.editor.editor, right + index)) { + "Expression-body lexical styling differs at $pair, character $index" + } + } + } + offsets.forEach { offset -> + check(probe.lexical(doc.editor.editor, offset).isNotBlank()) + check( + probe.semanticOverlays(doc.editor.editor, offset).isEmpty(), + ) { "Semantic markup masks lexical detail at $offset" } + } + } + } + captureColorScheme(doc) + data["recovery"].rows().forEach { edit -> + replace(doc, source.replace(edit.string("from"), edit.string("to"))) + errors(doc) + val current = highlightTokens(doc) + check(current.filter { it.type != "comment" }.all { it.text.matches(Regex("[A-Za-z_][A-Za-z_0-9]*")) }) + replace(doc, source) + clean(doc) + check(highlightTokens(doc) == before) { "Repair did not restore exact classifications" } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.HighlightingUi", plugin = "org.xtclang.playbook.probe") +internal interface HighlightingProbe { + fun lexical( + editor: Editor, + offset: Int, + ): String + + fun semanticOverlays( + editor: Editor, + offset: Int, + ): List +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt new file mode 100644 index 00000000000..2bdc665d557 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt @@ -0,0 +1,96 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import com.intellij.driver.sdk.invokeAction +import org.eclipse.lsp4j.InlineCompletionTriggerKind +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.inlineCompletionCases() { + listOf("X255", "X256", "X257", "X258").forEach { id -> + case(id) { data -> + val marked = data.string("source") + val offset = marked.indexOf('§') + val source = marked.replace("§", "") + val document = open(data.string("file"), source) + val mode = data.string("mode") + + fun queryInline( + kind: InlineCompletionTriggerKind, + selected: Map? = null, + ) = query( + "textDocument/inlineCompletion", + document, + offset, + mapOf( + "context" to + buildMap { + put("triggerKind", kind.value) + selected?.let { put("selectedCompletionInfo", it) } + }, + ), + ).asJsonObject["items"].rows() + check(protocol.capabilities().asJsonObject["inlineCompletionProvider"].asBoolean) + if (mode == "selection") { + if (replacementGate("UP26")) { + nativeInlineAlternatives(document, offset, source) + } + check(queryInline(InlineCompletionTriggerKind.Automatic).isEmpty()) + val alternatives = queryInline(InlineCompletionTriggerKind.Invoked).map { it.string("insertText") } + check(alternatives.sorted() == listOf("another", "answer")) + val range = + mapOf( + "start" to ParityWorkspace.position(source, offset - 2), + "end" to ParityWorkspace.position(source, offset), + ) + val selected = queryInline(InlineCompletionTriggerKind.Invoked, mapOf("range" to range, "text" to "ans")) + check(selected.map { it.string("insertText") } == listOf("answer")) + check(queryInline(InlineCompletionTriggerKind.Invoked, mapOf("range" to range, "text" to "answer")).isEmpty()) + // TODO LSP4IJ: UP26 always sends Automatic, including DirectCall, without selectedCompletionInfo. + // Keep this protocol coverage explicit until native invocation/selection is forwarded. + return@case + } + check(queryInline(InlineCompletionTriggerKind.Automatic).map { it.string("insertText") } == listOf(data.string("expected"))) + with(driver) { + val editor = document.editor + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(offset) } + + fun visible() = withContext(OnDispatcher.EDT) { utility(InlineCompletionProbe::class).visibleText(editor.editor) } + invokeAction("CallInlineCompletionAction", component = editor.component) + awaitUi("compiler ghost text is visible", 30.seconds) { visible() == "wer" } + if (mode == "typing") { + invokeAction("EditorEscape", component = editor.component) + awaitUi("ghost text is dismissed", 10.seconds) { visible().isEmpty() } + check(document.text == source) + // Use the native typing route so the provider sees the real document-change event. + withContext(OnDispatcher.EDT) { utility(InlineCompletionProbe::class).type(editor.editor, "w") } + invokeAction("CallInlineCompletionAction", component = editor.component) + awaitUi("continued typing has current ghost text", 30.seconds) { visible() == "er" } + } + invokeAction("InsertInlineCompletionAction", component = editor.component) + val expected = source.substring(0, offset - 3) + data.string("expected") + source.substring(offset) + awaitUi("native inline acceptance preserves source suffix", 15.seconds) { document.text == expected } + if (mode == "accept") { + clean(document) + invokeAction("\$Undo", component = editor.component) + awaitUi("one Undo restores the prefix", 15.seconds) { document.text == source } + } else if (mode == "incomplete") { + replace(document, expected.replace("consume(answer\n", "consume(answer);\n")) + clean(document) + } + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.InlineCompletionUi", plugin = "org.xtclang.playbook.probe") +internal interface InlineCompletionProbe { + fun type( + editor: Editor, + text: String, + ) + + fun visibleText(editor: Editor): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt new file mode 100644 index 00000000000..c702872d25b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt @@ -0,0 +1,131 @@ +package org.xtclang.idea.playbook + +import com.google.gson.GsonBuilder +import com.google.gson.JsonParser +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.librarySettingsCases() { + listOf("X266", "X267", "X268").forEach { id -> + case(id) { + val data = common["librarySettings"].asJsonObject + val binaries = + Path + .of(System.getProperty("xtc.playbook.scenarios")) + .parent.parent.parent + .resolve("lsp-server/build/generated/compiler-playbook/libraries") + val one = binaries.resolve("one").toUri().toString() + val two = binaries.resolve("two").toUri().toString() + val sources = binaries.resolve("sources").toUri().toString() + + fun options( + paths: List?, + attached: Boolean = false, + ) = GsonBuilder().serializeNulls().create().toJson( + mapOf( + "modulePath" to paths, + "sourceAttachments" to + if (attached) listOf(mapOf("module" to data.string("module"), "roots" to listOf(sources))) else emptyList(), + ), + ) + val document = open(data.string("consumerFile"), data.string("consumer")) + if (id == "X268") { + configure(listOf(SharedScenarios.SourceModule("LibraryConsumer", document.uri, emptyList()))) + } + with(driver) { + val page = utility(CompilerLibrariesPage::class) + val project = singleProject() + val original = withContext(OnDispatcher.EDT) { page.content(project) } + + fun edit( + paths: List?, + action: String, + attached: Boolean = false, + ) = withContext(OnDispatcher.EDT) { page.edit(project, options(paths, attached), action) } + try { + if (id == "X266") { + edit(listOf(one, two), "cancel") + edit(listOf(one, two), "reset") + val result = requireNotNull(edit(listOf(one, two), "order")) + val paths = + JsonParser + .parseString( + result, + ).asJsonObject["xtc"] + .asJsonObject["compiler"] + .asJsonObject["libraries"] + .asJsonObject["modulePath"] + .asJsonArray + check(paths.map { it.asString } == listOf(two, one)) + clean(document) + } else { + edit(listOf(one), "apply", attached = true) + clean(document) + + fun definition() { + val target = query("textDocument/definition", document, document.at(data.string("anchor"))).rows().single() + val path = Path.of(URI(target.string("uri"))) + check(!Files.isWritable(path)) + check(Files.readString(path) == data.string("source")) + check(target["range"].asJsonObject["start"].asJsonObject.int("line") == data.int("definitionLine")) + val library = open(path.toString()) + check(library.text == data.string("source")) + } + definition() + if (id == "X267") { + val previous = protocol.server().getCurrentProcessId() + protocol.server().restart() + awaitUi("library settings survive server restart", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { + it != + previous + } == + true + } + clean(document) + definition() + } else { + edit(listOf(binaries.resolve("missing").toUri().toString()), "invalid") + edit(listOf(one, one), "invalid") + withContext(OnDispatcher.EDT) { + utility( + CompilerSettingsPage::class, + ).resourceRootsRoundTrip(project, GsonBuilder().create().toJson(listOf(two, one))) + } + clean(document) + definition() + } + edit(emptyList(), "apply") + diagnostics(document) { it.isNotEmpty() } + edit(listOf(one), "apply", attached = true) + clean(document) + } + } finally { + withContext(OnDispatcher.EDT) { page.restore(project, original) } + } + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.CompilerLibrariesPage", plugin = "org.xtclang.playbook.probe") +interface CompilerLibrariesPage { + fun content(project: Project): String? + + fun edit( + project: Project, + json: String, + action: String, + ): String? + + fun restore( + project: Project, + content: String?, + ) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityModules.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityModules.kt new file mode 100644 index 00000000000..16b0dc68990 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityModules.kt @@ -0,0 +1,271 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.invokeAction +import java.nio.file.Files +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.moduleCases() { + case("X14") { data -> + val marked = + fixture(data.string("file")) + .replace(data.string("method"), data.string("incompleteMethod")) + val disk = marked.replace("§", "").replace("\n", "\r\n") + val document = open(data.string("file"), disk) + // IntelliJ normalizes the in-memory document to LF. Its saved file must retain CRLF. + val at = marked.indexOf('§') + check(document.text == marked.replace("§", "")) + val expected = document.text.replace(data.string("prefix"), data.string("accepted")) + with(driver) { accept(document.editor, at, data.string("label"), expected) } + clean(document) + save(document) + awaitUi("saved completion preserves emoji and CRLF", 10.seconds) { + Files.readString(directory.resolve(document.file)) == expected.replace("\n", "\r\n") + } + } + case("X23") { data -> + val root = project() + val child = open(data.string("memberFile")) + val version = version(child) + replace( + root, + fixture(data.string("rootFile")) + .replace(data.string("replaceFrom"), data.string("replaceWith")), + ) + check(errors(child).none { it.code == data.string("diagnosticCode") }) + check(version(child) == version) + check( + with(driver) { receivedDiagnostics(root.editor) } + .none { it.code == data.string("diagnosticCode") }, + ) + replace(root, fixture(data.string("rootFile"))) + clean(child) + } + case("X24") { data -> + val root = project() + val (child, at) = + marked( + data.string("memberFile"), + fixture(data.string("memberFile")) + .replace(data.string("method"), data.string("incompleteMethod")), + ) + data.strings("types").forEach { type -> + val value = + data.string( + if (type == data.string("integerType")) "integerValue" else "stringValue", + ) + replace( + root, + fixture(root.file) + .replace( + data.string("replaceFrom"), + SharedScenarios.text(data.string("replaceWith"), type, value), + ), + ) + with(driver) { + lookup(child.editor, at) { hasType(it, data.string("label"), type) } + invokeAction("EditorEscape", component = child.editor.component) + } + } + check(Files.readString(directory.resolve(root.file)) == fixture(root.file)) + } + case("X25") { data -> + val root = project() + val file = data.string("file") + virtual(file, data.string("text"), data.int("version")) { + with(driver) { + awaitUi("virtual member participates in current type hierarchy", 45.seconds) { + edges(hierarchy(root, root.at(data.string("anchor"))), "subtypes").any { + it.string("name") == data.string("name") + } + } + } + check(!Files.exists(directory.resolve(file))) + published(file) { it.isEmpty() } + } + } + case("X26") { data -> + val root = project() + val child = open(data.string("file")) + replace( + child, + fixture(child.file).replace(data.string("anchor"), data.string("replaceWith")), + ) + errors(child) + discard(child) + settle(root) + published(child.file) { it.isEmpty() } + val reopened = open(child.file) + check(reopened.text == fixture(child.file)) + check( + targets(reopened, "definition", reopened.at(data.string("anchor"))).size == + data.int("expected"), + ) + } + case("X27") { data -> + project() + val file = data.string("file") + write(file, data.string("text")) + published(file) { it.isNotEmpty() } + delete(file) + published(file) { it.isEmpty() } + } + case("X28") { data -> + project() + val child = open(data.string("file")) + val old = hierarchy(child, child.at(data.string("anchor"))) + replace(child, data.string("replaceWith")) + clean(child) + check(edges(old, "supertypes").isEmpty()) + check( + edges( + hierarchy(child, child.at(data.string("anchor"))), + "supertypes", + ).none { it.string("name").startsWith(data.string("contains")) }, + ) + replace(child, fixture(child.file)) + check( + edges( + hierarchy(child, child.at(data.string("anchor"))), + "supertypes", + ).any { it.string("name").startsWith(data.string("contains")) }, + ) + } + case("X29") { data -> + val (document, at) = editing(data.string("incompleteBody")) + val initial = document.text + val pending = + (0 until 10).map { index -> + replace( + document, + initial.replace( + data.string("integerCall"), + data.string( + if (index % 2 == 1) { + "integerCall" + } else { + "stringCall" + }, + ), + ), + settle = false, + ) + protocol.request("textDocument/signatureHelp", document.params(at)) + } + pending.forEach { awaitRetired("textDocument/signatureHelp", it) } + val (final, _) = editing(data.string("completedBody")) + clean(final) + with(driver) { + signature(final.editor, final.at(data.string("completedCall"), data.int("offset"))) { + data.pattern("pattern").containsMatchIn(it.firstOrNull()?.label.orEmpty()) + } + } + val root = project() + val child = open(data.string("memberFile")) + (0 until 6).forEach { index -> + replace( + root, + fixture(root.file) + .replace( + data.string("replaceFrom"), + SharedScenarios.text(data.string("replaceWith"), index.toString()), + ), + settle = false, + ) + } + settle(root) + settle(child) + clean(child) + check( + targets(child, "definition", child.at(data.string("anchor"))).size == + data.int("expected"), + ) + } + case("X30") { data -> + val (document, at) = editing(data.string("body")) + val pending = protocol.request("textDocument/completion", document.params(at)) + if (pending.cancel(true)) { + check(pending.isCancelled()) + } else { + protocol.await("textDocument/completion", pending) + } + discard(document) + val reopened = open(data.string("file")) + clean(reopened) + val server = protocol.server() + val oldProcess = server.getCurrentProcessId() + server.restart() + with(driver) { + awaitUi("new language server process", 60.seconds) { + server.getCurrentProcessId()?.let { it != oldProcess } == true + } + } + // A new PID precedes initialization and LSP4IJ's asynchronous document reopening. + // Use the normal didOpen readiness check before touching the new synchronizer. + clean(open(data.string("file"))) + val (current, cursor) = editing(data.string("body")) + with(driver) { + lookup(current.editor, cursor) { + it.any { item -> item.getLookupString() == data.string("label") } + } + invokeAction("EditorEscape", component = current.editor.component) + } + } + case("X31") { data -> + val document = open(data.string("file")) + val capabilities = protocol.capabilities().asJsonObject + val selection = + System + .getProperty("xtc.playbook.cases", "") + .split(',') + .map(String::trim) + .filter(String::isNotBlank) + val prototypeEnabled = + common.getAsJsonObject("colorPrototype").strings("cases").any { + selection.isEmpty() || it in selection + } + val prototypeCapability = data.string("prototypeCapability") + check((capabilities[prototypeCapability]?.takeUnless { it.isJsonNull }?.asBoolean == true) == prototypeEnabled) { + prototypeCapability + } + data.strings("unsupportedCapabilities").forEach { + check( + capabilities[it] == null || + capabilities[it].isJsonNull || + capabilities[it].toString() == "false", + ) { + it + } + } + data.strings("supportedCapabilities").forEach { + check( + capabilities[it] != null && + !capabilities[it].isJsonNull && + capabilities[it].toString() != "false", + ) { + it + } + } + replace(document, data.string("unformatted")) + check( + query( + "textDocument/formatting", + document, + extra = + mapOf( + "options" to + mapOf("tabSize" to data.int("tabSize"), "insertSpaces" to true), + ), + ).rows() + .isNotEmpty(), + ) + } + case("X32") { data -> + data.strings("bodies").forEach { body -> + val (document, at) = editing(body) + val result = query("textDocument/completion", document, at) + val items = + if (result.isJsonObject) result.asJsonObject["items"].rows() else result.rows() + check(items.size == data.int("completionCount")) { "$body: $items" } + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityNavigation.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityNavigation.kt new file mode 100644 index 00000000000..7657e0be1a3 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityNavigation.kt @@ -0,0 +1,329 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import java.nio.file.Files +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.navigationCases() { + case("X3") { data -> + val doc = open(data.string("file")) + clean(doc) + val narrowed = doc.at(data.string("narrowUse"), data.int("offset")) + val wide = doc.at(data.string("wideUse"), data.int("offset")) + with(driver) { + nativeHover(doc, narrowed, data.pattern("narrowedType")) + nativeHover(doc, wide, data.pattern("declaredType")) + } + val first = targets(doc, "definition", narrowed) + val second = targets(doc, "definition", wide) + check(first.size == data.int("targetCount") && first == second) + check( + first.single().line() == + ParityWorkspace.position( + doc.text, + doc.at(data.string("declaration"), data.int("offset")), + )["line"], + ) + } + case("X5") { data -> + val doc = open(data.string("file")) + check( + names(targets(doc, "definition", doc.at(data.string("libraryType")))) == + listOf(data.string("libraryTarget")), + ) + replace(doc, fixture(doc.file).substringBeforeLast(data.string("missingCloser"))) + diagnostics(doc) { errors -> + errors.any { it.code?.startsWith(data.string("parserCodePrefix")) == true } + } + with(driver) { + structure(doc.editor, listOf(data.string("retainedSymbol"))) + awaitUi("recovered folds", 45.seconds) { folds(doc.editor).isNotEmpty() } + selectionParents(doc.editor, doc.at(data.string("anchor"), data.int("offset"))) + } + check( + targets(doc, "definition", doc.at(data.string("anchor"), data.int("offset"))).isEmpty(), + ) + replace(doc, fixture(doc.file)) + clean(doc) + } + case("X21") { data -> + val root = project() + val refs = + query( + "textDocument/references", + root, + root.at(data.string("anchor")), + mapOf("context" to mapOf("includeDeclaration" to true)), + ).rows() + check(refs.any { it.string("uri") == uri(data.string("file")) }) + val symbols = + protocol.query("workspace/symbol", mapOf("query" to data.string("symbolName"))).rows() + check( + symbols.any { it.getAsJsonObject("location").string("uri") == uri(data.string("file")) }, + ) + check( + protocol.server().getOpenedDocuments().none { + it.getFile().getPath() == directory.resolve(data.string("file")).toString() + }, + ) + val child = open(data.string("file")) + data.strings("uses").forEach { word -> + val locations = targets(child, "definition", child.at(word)) + check( + locations.size == data.int("targetCount") && + locations.single().string("uri") == root.uri, + ) + } + } + case("X22") { data -> + val root = project() + val base = hierarchy(root, root.at(data.string("baseDeclaration"))) + val children = edges(base, "subtypes") + check(children.size == data.int("childCount")) + check(children.single().string("name") == data.string("childName")) + check(children.single().string("uri") == uri(data.string("file"))) + val parents = edges(children.single(), "supertypes") + check( + parents.any { + "${it.string("name")} ${it["detail"]}".contains(data.string("typeArgument")) + }, + ) { + parents.toString() + } + check(edges(base, "supertypes").any { it.string("name") == data.string("interfaceName") }) + val named = + hierarchy(root, root.at(data.string("interfaceDeclaration"), data.int("offset"))) + check( + edges(named, "subtypes").any { it.string("name").startsWith(data.string("baseName")) }, + ) + with(driver) { + nativeHierarchy( + root, + root.at(data.string("baseDeclaration")), + "type", + listOf(data.string("childName")), + ) + } + } + case("X36") { data -> + val doc = open(data.string("file")) + check( + names(targets(doc, "implementation", doc.at(data.string("anchor")))).sorted() == + data.strings("implementations").sorted(), + ) + } + case("X37") { data -> + val doc = open(data.string("file")) + val lines = + data + .pattern("overrides") + .findAll(doc.text) + .map { + ParityWorkspace + .position( + doc.text, + it.range.first + data.string("declarationPrefix").length, + ).getValue("line") + }.toList() + check(lines.size == data.int("declarationOffset")) + listOf( + doc.at(data.string("declaration"), data.int("declarationOffset")), + doc.at(data.string("use"), data.int("useOffset")), + ).forEach { at -> + check( + targets(doc, "implementation", at).map { it.line() }.sorted() == lines.sorted(), + ) + } + val offset = + doc.text + .lineSequence() + .take(lines[1]) + .sumOf { it.length + 1 } + + doc.text.lines()[lines[1]].indexOf(data.string("methodName")) + val child = targets(doc, "implementation", offset) + check(child.size == data.int("targetCount") && child.single().line() == lines[1]) + } + case("X153") { data -> + val variants = data["variants"].rows() + write(data.string("file"), variants.first().string("source")) + val doc = open(data.string("file")) + variants.forEach { variant -> + replace(doc, variant.string("source")) + clean(doc) + val actual = targets(doc, variant.string("kind"), doc.at(variant.string("anchor"), variant.int("offset"))) + val expected = + variant + .strings("targets") + .map { anchor -> + ParityWorkspace.position(doc.text, doc.at(anchor)) + }.toSet() + check( + actual.map { it.getAsJsonObject("range").getAsJsonObject("start") }.toSet() == + expected.map { Gson().toJsonTree(it).asJsonObject }.toSet(), + ) { "Unexpected implementation locations: $actual; expected $expected" } + check(actual.all { it.string("uri") == doc.uri }) + } + } + case("X38") { data -> + val root = project() + replace( + root, + fixture(data.string("rootFile")) + .replace(data.string("replaceFrom"), data.string("rootWithFactory")), + ) + + fun lookup() = targets(root, "typeDefinition", root.at(data.string("factory"))) + val original = lookup() + check( + original.size == data.int("targetCount") && + original.single().string("uri") == uri(data.string("memberFile")), + ) + val implementations = targets(root, "implementation", root.at(data.string("method"))) + check( + implementations.size == data.int("targetCount") && + implementations.single().string("uri") == root.uri, + ) + val child = open(data.string("memberFile")) + replace(child, "\n\n" + fixture(child.file)) + check(lookup().single().line() == original.single().line() + data.int("lineShift")) + replace(child, data.string("replaceWith")) + check(lookup().isEmpty()) + replace(child, fixture(child.file)) + check(lookup().single().line() == original.single().line()) + } + case("X64") { data -> + val doc = open(data.string("file")) + val expected = data["locations"].rows().map { expectedRange(doc, it) } + listOf( + doc.at(data.string("anchor2"), data.int("offset2")), + doc.at(data.string("anchor"), data.int("offset")), + ).forEach { at -> + assertRanges(targets(doc, "implementation", at).map { it["range"] }, expected) + } + } + case("X65") { data -> + val doc = open(data.string("file")) + data["variants"].rows().forEach { variant -> + val expected = variant["expected"].rows().map { expectedRange(doc, it) } + assertRanges( + targets( + doc, + "implementation", + doc.at(variant.string("anchor"), variant.int("offset")), + ).map { it["range"] }, + expected, + ) + } + } + case("X66") { data -> + val doc = open(data.string("file")) + data["variants"].rows().forEach { variant -> + check( + targets( + doc, + "implementation", + doc.at(variant.string("anchor"), variant.int("offset")), + ).isEmpty(), + ) + } + } + case("X67") { data -> + write(data.string("memberFile"), data.string("original")) + val root = open(data.string("rootFile")) + + fun lookup() = targets(root, "implementation", root.at(data.string("anchor"), 2)) + val original = lookup() + check(original.size == data.int("targetCount")) + val memberUri = uri(data.string("memberFile")) + check( + original.single { it.string("uri") == memberUri }.line() == data.int("declarationLine"), + ) + val member = open(data.string("memberFile")) + replace(member, "\n\n" + data.string("original")) + check(lookup().single { it.string("uri") == memberUri }.line() == data.int("lineShift")) + replace(member, data.string("replaceWith")) + check(lookup().isEmpty()) + replace(member, data.string("original")) + check(lookup().size == data.int("targetCount")) + check(Files.readString(directory.resolve(member.file)) == data.string("original")) + } + case("X68") { data -> + val doc = open(data.string("file")) + data["variants"].rows().forEach { variant -> + val targets = + targets(doc, "implementation", doc.at(variant.string("use"), variant.int("offset"))) + check(targets.size == data.int("targetCount")) + val start = doc.at(variant.string("declaration"), variant.int("declarationOffset")) + check(targets.single()["range"] == sourceRange(doc, start, variant.int("length"))) + } + } + case("X69") { data -> + val doc = open(data.string("file")) + val targets = targets(doc, "definition", doc.at(data.string("anchor"))) + check(targets.size == data.int("targetCount")) + val start = doc.at(data.string("inheritedDeclaration"), data.int("inheritedOffset")) + check(targets.single()["range"] == sourceRange(doc, start, data.int("targetLength"))) + val caller = + query( + "textDocument/prepareCallHierarchy", + doc, + doc.at(data.string("override"), data.int("overrideOffset")), + ).rows() + .single() + val calls = protocol.query("callHierarchy/outgoingCalls", mapOf("item" to caller)).rows() + check(calls.any { it.getAsJsonObject("to")["selectionRange"] == targets.single()["range"] }) + with(driver) { + rejectRename(doc.editor, doc.at(data.string("anchor")), data.string("renameRejection")) + } + } + case("X72") { data -> + val doc = open(data.string("file")) + data["variants"].rows().forEach { variant -> + val targets = + targets(doc, "implementation", doc.at(variant.string("use"), data.int("offset"))) + val expected = + listOf("getter", "setter").map { name -> + sourceRange( + doc, + doc.at(variant.string(name), variant.int("${name}Offset")), + data.int("targetLength"), + ) + } + check(targets.size == data.int("targetCount")) + assertRanges(targets.map { it["range"] }, expected) + } + } +} + +private fun assertRanges( + actual: List, + expected: List, +) { + // Gson's parsed numbers and integer primitives compare equal but can hash differently. + // Compare ordered lists so exact ranges and duplicate counts do not depend on that hash. + check(actual.sortedBy(JsonElement::toString) == expected.sortedBy(JsonElement::toString)) { + "Expected $expected; received $actual" + } +} + +private fun JsonObject.line(): Int = getAsJsonObject("range").getAsJsonObject("start").int("line") + +private fun expectedRange( + doc: ParityWorkspace.Document, + data: JsonObject, +) = sourceRange(doc, doc.at(data.string("anchor"), data.int("offset")), data.int("length")) + +internal fun sourceRange( + doc: ParityWorkspace.Document, + at: Int, + length: Int, +): JsonObject = + Gson() + .toJsonTree( + mapOf( + "start" to ParityWorkspace.position(doc.text, at), + "end" to ParityWorkspace.position(doc.text, at + length), + ), + ).asJsonObject diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt new file mode 100644 index 00000000000..4a1d8183f48 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt @@ -0,0 +1,1225 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.getToolWindow +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.invokeGlobalBackendAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.elements.button +import com.intellij.driver.sdk.ui.components.elements.dialog +import com.intellij.driver.sdk.ui.ui +import org.junit.jupiter.api.Assertions.assertThrows +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.platformCases() { + listOf("X158", "X243", "X244").forEach { id -> + case(id) { data -> + write(data.string("libraryFile"), data.string("library")) + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule(data.string("libraryModule"), uri(data.string("libraryFile")), emptyList()), + SharedScenarios.SourceModule(data.string("module"), uri(data.string("file")), listOf(data.string("libraryModule"))), + ), + ) + val document = open(data.string("file")) + if (id == "X244") { + diagnostics(document) { items -> items.any { it.severity == "Error" } } + } else { + clean(document) + } + val links = + query("textDocument/documentLink", document).rows().map { + if (it.has("data")) protocol.query("documentLink/resolve", it).asJsonObject else it + } + check(links.size == data.strings("linkNames").size) + check(links.all { Path.of(URI(it.string("target"))) == directory.resolve(data.string("libraryFile")) }) + links.zip(data.strings("linkNames")).forEach { (link, name) -> + val range = link["range"].asJsonObject + val start = range["start"].asJsonObject + val end = range["end"].asJsonObject + check(start["line"] == end["line"]) + check( + document.text + .lines()[start["line"].asInt] + .substring(start["character"].asInt, end["character"].asInt) == name, + ) + } + if (data.string("alias").isNotEmpty()) { + val ranges = + query("textDocument/linkedEditingRange", document, document.at(data.string("alias"))) + .asJsonObject["ranges"] + .asJsonArray + val expected = data.strings("aliasUses").map { document.text.indexOf(it) } + check(ranges.map { ParityWorkspace.offset(document.text, it.asJsonObject["start"].asJsonObject) } == expected) + } + check(open(data.string("libraryFile")).text == data.string("library")) + } + } + listOf("X245", "X246", "X247").forEach { id -> + case(id) { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + data["queries"].asJsonArray.forEach { row -> + val request = row.asJsonObject + val result = query("textDocument/linkedEditingRange", document, document.at(request.string("anchor"))) + val starts = + if (result.isJsonNull) { + emptyList() + } else { + result.asJsonObject["ranges"].asJsonArray.map { + ParityWorkspace.offset(document.text, it.asJsonObject["start"].asJsonObject) + } + } + check(starts == request.strings("occurrences").map { document.text.indexOf(it) }) + check(document.text == data.string("source")) + } + } + } + case("X248") { data -> + val document = + open( + data.string("file"), + data["variants"] + .asJsonArray + .first() + .asJsonObject + .string("source"), + ) + data["variants"].asJsonArray.forEach { row -> + val variant = row.asJsonObject + replace(document, variant.string("source")) + errors(document) + val outline = query("textDocument/documentSymbol", document) + + fun names(rows: List): List = + rows.flatMap { symbol -> + listOf(symbol.string("name")) + symbol["children"]?.let { names(it.rows()) }.orEmpty() + } + check(names(outline.rows()).containsAll(variant.strings("symbols"))) + val folds = query("textDocument/foldingRange", document).rows() + check(folds.any { it["startLine"].asInt == variant["foldLine"].asInt && it["endLine"].asInt > it["startLine"].asInt }) + val at = document.at(variant.string("anchor")) + val selections = + query( + "textDocument/selectionRange", + document, + extra = + mapOf("positions" to listOf(document.params(at).getValue("position"))), + ).rows() + val chain = + generateSequence(selections.single()) { + it["parent"] + ?.takeUnless { parent -> + parent.isJsonNull + }?.asJsonObject + }.toList() + check(chain.size > 1) + val spans = + chain.map { selection -> + val range = selection["range"].asJsonObject + ParityWorkspace.offset(document.text, range["start"].asJsonObject) to + ParityWorkspace.offset(document.text, range["end"].asJsonObject) + } + check(spans.distinct() == spans) + val cursor = document.text.indexOf(variant.string("anchor")) + check(spans.all { cursor in it.first..it.second }) + check(spans.zipWithNext().all { (child, parent) -> parent.first <= child.first && parent.second >= child.second }) + } + replace(document, data.string("repaired")) + clean(document) + val outline = query("textDocument/documentSymbol", document).rows().single() + check(outline["children"].asJsonArray.map { it.asJsonObject.string("name") } == listOf("repaired")) + } + listOf("X249", "X250").forEach { id -> + case(id) { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + with(driver) { + focusEditor(document.editor) + invokeAction("ReformatCode", now = false, component = document.editor.component) + awaitUi("$id native formatting matches shared layout", 30.seconds) { document.text == data.string("expected") } + } + clean(document) + check( + query( + "textDocument/formatting", + document, + extra = + mapOf("options" to mapOf("tabSize" to 4, "insertSpaces" to true, "insertFinalNewline" to true)), + ).rows().isEmpty(), + ) + listOf("\$Undo" to "source", "\$Redo" to "expected", "\$Undo" to "source").forEach { (action, state) -> + with(driver) { + focusEditor(document.editor) + invokeAction(action, now = false, component = document.editor.component) + awaitUi("$id $action restores exact text", 15.seconds) { document.text == data.string(state) } + } + } + } + } + case("X136") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + val before = + protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).exercise(singleProject()) + } + } + val after = + protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + check(before["pid"] == after["pid"]) + val queue = after["compilerQueue"].asJsonObject + check(queue["queueSize"].asInt == queue["queuedJobs"].asJsonArray.size()) + check(document.text == data.string("source")) + } + case("X137") { data -> + val document = open(data.string("file"), data.string("source")) + replace(document, data.string("edited")) + val original = with(driver) { utility(LanguageServicePage::class).content(singleProject()) } + var previous = protocol.server().getCurrentProcessId() + try { + listOf("incremental", "full").forEach { transport -> + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).transport(singleProject(), transport) + } + awaitUi("transport restart into $transport", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previous } == true + } + } + previous = protocol.server().getCurrentProcessId() + val status = + protocol + .query("xtc/languageServiceStatus", emptyMap()) + .asJsonObject + check(status.string("textSynchronization") == transport) + check(document.text == data.string("edited")) + settle(document) + clean(document) + } + val unsaved = data.string("edited").replace("value", "adapterSwitchProof") + replace(document, unsaved) + listOf("Tree-sitter" to "TreeSitter", "Ecstasy Compiler" to "XDK").forEach { (label, expected) -> + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).adapter(singleProject(), label) + } + awaitUi("adapter restart into $label", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previous } == true + } + } + previous = protocol.server().getCurrentProcessId() + val status = protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + check(status.string("adapter") == expected) + check(document.text == unsaved) + + fun names(rows: List): List = + rows.flatMap { symbol -> + listOf(symbol.string("name")) + symbol["children"]?.let { names(it.rows()) }.orEmpty() + } + with(driver) { + awaitUi("unsaved declaration analyzed by $label", 30.seconds) { + "adapterSwitchProof" in names(query("textDocument/documentSymbol", document).rows()) + } + } + } + } finally { + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).restore(singleProject(), original) + } + } + } + } + case("X138") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + val previousPid = protocol.server().getCurrentProcessId() + val original = + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class) + .indent(singleProject(), data["indent"].asInt) + } + } + try { + with(driver) { + awaitUi("live Code Style configuration", 15.seconds) { + protocol + .query("xtc/languageServiceStatus", emptyMap()) + .asJsonObject["formatting"] + ?.takeIf { it.isJsonObject } + ?.asJsonObject + ?.get("indentSize") + ?.asInt == data["indent"].asInt + } + } + + fun formatted() = + query( + "textDocument/formatting", + document, + extra = mapOf("options" to mapOf("tabSize" to 4, "insertSpaces" to true)), + ).rows() + check(formatted().any { it.string("newText") == data.string("expectedIndent") }) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).indent(singleProject(), 0) + } + } + check(formatted().any { it.string("newText") == data.string("expectedIndent") }) + val status = + protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + check( + !status["saveHookSupported"].asBoolean && !status["serverSaveFormatting"].asBoolean, + ) + check(protocol.server().getCurrentProcessId() == previousPid) + check(document.text == data.string("source")) + } finally { + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).indent(singleProject(), original) + } + } + } + } + + case("X139") { data -> + val closed = open(data.string("closedFile"), data.string("closedSource")) + replace(closed, data.string("closedSource") + "\n") + with(driver) { + withContext(OnDispatcher.EDT) { + service(singleProject()) + .closeFile(closed.editor.editor.getVirtualFile()) + } + } + val document = open(data.string("file"), data.string("source")) + clean(document) + val previous = + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).saveFormatting(singleProject(), true) + } + } + try { + replace(document, data.string("source") + "\n") + with(driver) { + // A bare FileDocumentManager.saveDocument bypasses Actions on Save. + invokeAction("SaveAll", component = document.editor.component) + awaitUi("native format on save", 30.seconds) { + document.text.contains(" Int value = 1;") + } + awaitUi("native format on save for a closed dirty tab", 30.seconds) { + closed.text == data.string("closedFormatted") + "\n" + } + } + val status = + protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + check(!status["serverSaveFormatting"].asBoolean) + } finally { + with(driver) { + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).saveFormatting(singleProject(), previous) + } + } + } + } + + case("X144") { data -> + write(data.string("file"), data.string("source")) + write(data.string("otherFile"), data.string("otherSource")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + SharedScenarios.SourceModule( + data.string("otherModule"), + uri(data.string("otherFile")), + emptyList(), + ), + ), + ) + val other = open(data.string("otherFile")) + val document = open(data.string("file")) + + fun change( + target: ParityWorkspace.Document, + version: Int, + text: String, + ) = mapOf( + "textDocument" to mapOf("uri" to target.uri, "version" to version), + "edits" to + listOf( + mapOf( + "range" to + mapOf( + "start" to ParityWorkspace.position(target.text, 0), + "end" to + ParityWorkspace.position(target.text, target.text.length), + ), + "newText" to text, + ), + ), + ) + + fun apply(changes: List>): JsonObject { + val pending = + with(driver) { + utility(WorkspaceEdits::class) + .apply( + singleProject(), + Gson() + .toJson( + mapOf( + "label" to "Ecstasy guarded edit", + "edit" to mapOf("documentChanges" to changes), + ), + ), + ) + } + return protocol.await("workspace/applyEdit", pending).asJsonObject + } + check( + apply(listOf(change(document, version(document), data.string("changed"))))["applied"] + .asBoolean, + ) + check(document.text == data.string("changed")) + with(driver) { + focusEditor(document.editor) + invokeAction("\$Undo", now = false, component = document.editor.component) + awaitUi("server edit Undo restores the source", 15.seconds) { + document.text == data.string("source") + } + } + val oldVersion = version(other) + replace(other, data.string("otherChanged")) + check( + !apply( + listOf( + change(document, version(document), data.string("changed")), + change(other, oldVersion, data.string("otherSource")), + ), + )["applied"] + .asBoolean, + ) + check(document.text == data.string("source")) + check(other.text == data.string("otherChanged")) + clean(document) + clean(other) + } + + case("X143") { data -> + val source = + "module ${data.string("module")} {\n" + + (0 until data["declarations"].asInt).joinToString("\n") { + " class ${data.string("prefix")}$it {}" + } + + "\n}\n" + write(data.string("file"), source) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + query("textDocument/documentSymbol", document) + val normal = + protocol.query("workspace/symbol", mapOf("query" to data.string("prefix"))).rows() + val capture = + with(driver) { + utility(PartialResults::class).listen(singleProject(), data.string("token")) + } + try { + val final = + protocol + .query( + "workspace/symbol", + mapOf( + "query" to data.string("prefix"), + "partialResultToken" to data.string("token"), + ), + ).rows() + val batches = capture.values().map { JsonParser.parseString(it).asJsonArray } + check(final.isEmpty()) + check(normal.size == data["declarations"].asInt) + check(batches.size > 1 && batches.all { it.size() <= data["batchSize"].asInt }) + check( + batches.flatMap { it.map { row -> row.asJsonObject.string("name") } } == + normal.map { it.string("name") }, + ) + } finally { + capture.dispose() + } + } + + case("X142") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + val offset = document.text.indexOf(data.string("anchor")) + check(document.text == data.string("source")) { + "Initializer fixture changed before rename: ${Gson().toJson(document.text)}" + } + check( + query("textDocument/hover", document, offset) + .toString() + .contains(data.string("expected")), + ) + val definitions = query("textDocument/definition", document, offset).rows() + check( + definitions.any { + it["range"].asJsonObject["start"].asJsonObject["character"].asInt == + document.text.indexOf(data.string("name")) + }, + ) + check( + query( + "textDocument/references", + document, + offset, + mapOf("context" to mapOf("includeDeclaration" to true)), + ).rows() + .size == data["references"].asInt, + ) + applyRename(document, offset, data.string("newName")) + check( + document.text == + data.string("source").replace(data.string("name"), data.string("newName")), + ) { + "Initializer rename result: ${Gson().toJson(document.text)}" + } + with(driver) { + invokeAction("\$Undo", now = false, component = document.editor.component) + awaitUi("initializer rename Undo restores every occurrence", 15.seconds) { + document.text == data.string("source") + } + } + settle(document) + clean(document) + } + + case("X140") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + check(protocol.capabilities().asJsonObject.string("positionEncoding") == "utf-16") + val offset = document.text.lastIndexOf(data.string("anchor")) + check( + query("textDocument/hover", document, offset) + .toString() + .contains(data.string("expected")), + ) + val prepared = + query("textDocument/prepareRename", document, offset).asJsonObject["range"].asJsonObject + check(prepared["start"].asJsonObject["character"].asInt == offset) + check( + prepared["end"].asJsonObject["character"].asInt == offset + data.string("anchor").length, + ) + } + case("X141") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + try { + data["levels"] + .asJsonArray + .map { it.asString } + .forEach { level -> + protocol.notify("$/setTrace", mapOf("value" to level)) + val before = trace.notifications("$/logTrace").size + query( + "textDocument/hover", + document, + document.text.indexOf(data.string("anchor")), + ) + with(driver) { + awaitUi("server runtime trace $level", 15.seconds) { + trace.notifications("$/logTrace").drop(before).any { + it.string("message").startsWith("textDocument/hover:") && + (it.has("verbose") == (level == "verbose")) && + !it.toString().contains(data.string("source").trim()) + } + } + } + } + } finally { + protocol.notify("$/setTrace", mapOf("value" to "verbose")) + } + } + + case("X135") { data -> + write(data.string("file"), data.string("source")) + val document = open(data.string("file")) + clean(document) + with(driver) { + withContext(OnDispatcher.EDT) { getToolWindow("Language Servers").hide() } + listOf(true, false, true, false).forEach { visible -> + invokeAction("xtc.toggleServerLog") + awaitUi("Ecstasy server log visibility = $visible", 15.seconds) { + withContext(OnDispatcher.EDT) { + getToolWindow("Language Servers").isVisible() + } == visible + } + } + } + check(document.text == data.string("source")) + } + + case("X124") { data -> + val external = Files.createTempDirectory("xtc-playbook-resources-").toRealPath() + try { + write(data.string("file"), data.string("text")) + val resourceRoot = external.resolve(data.string("resourceDirectory")) + val roots = listOf(resourceRoot.toUri().toString()) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + roots, + ), + ), + ) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class) + .resourceRootsRoundTrip(singleProject(), Gson().toJson(roots)) + } + } + val document = open(data.string("file")) + errors(document) + Files.createDirectories(resourceRoot) + val resource = resourceRoot.resolve(data.string("resource")) + Files.writeString(resource, data.string("contents")) + clean(document) + Files.delete(resource) + errors(document) + Files.writeString(resource, data.string("contents")) + clean(document) + val replacement = external.resolve("replacement") + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + listOf(replacement.toUri().toString()), + ), + ), + ) + errors(document) + Files.createDirectories(replacement) + Files.writeString(replacement.resolve(data.string("resource")), data.string("contents")) + clean(document) + } finally { + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class).clearProjectGraph(singleProject()) + } + } + Files.walk(external).use { paths -> + paths.sorted(Comparator.reverseOrder()).forEach(Files::deleteIfExists) + } + } + } + case("X125") { data -> + val document = open(data.string("file"), data.string("hoverSource")) + clean(document) + with(driver) { + nativeHover( + document, + document.at(data.string("hoverAnchor")), + Regex(Regex.escape(data.string("hoverExpected"))), + ) + } + data["signatures"].rows().forEach { call -> + val marked = call.string("text") + replace(document, marked.replace("§", "")) + val help = + query("textDocument/signatureHelp", document, marked.indexOf('§')).asJsonObject + check( + help["signatures"] + .rows() + .single()["parameters"] + .asJsonArray + .size() == 2, + ) + check(help.int("activeParameter") == call.int("active")) + with(driver) { + signature(document.editor, marked.indexOf('§')) { + it.any { item -> "echo" in item.label } + } + } + } + data.strings("completions").forEach { marked -> + replace(document, marked.replace("§", "")) + with(driver) { + lookup(document.editor, marked.indexOf('§')) { items -> + items.any { it.getLookupString() == data.string("completion") } + } + dismissPopups() + } + } + replace(document, data.string("narrowedSource")) + clean(document) + check( + targets(document, "typeDefinition", document.at(data.string("narrowedAnchor"))).any { + it.string("uri").endsWith(data.string("targetSuffix")) + }, + ) + } + case("X126") { data -> + val document = open(data.string("file"), data.string("source")) + val full = query("textDocument/semanticTokens/full", document).asJsonObject + check(full["data"].asJsonArray.size() > 0 && full.string("resultId").isNotEmpty()) + replace(document, "\n" + data.string("source")) + val caps = protocol.capabilities().asJsonObject["semanticTokensProvider"].asJsonObject + val deltaSupported = + caps["full"].let { it.isJsonObject && it.asJsonObject["delta"]?.asBoolean == true } + if (deltaSupported) { + val delta = + query( + "textDocument/semanticTokens/full/delta", + document, + extra = mapOf("previousResultId" to full.string("resultId")), + ).asJsonObject + val patched = full["data"].asJsonArray.map { it.asInt }.toMutableList() + delta["edits"] + .rows() + .sortedByDescending { it.int("start") } + .forEach { edit -> + val start = edit.int("start") + patched.subList(start, start + edit.int("deleteCount")).clear() + patched.addAll(start, edit["data"]?.asJsonArray?.map { it.asInt }.orEmpty()) + } + check( + patched == + query("textDocument/semanticTokens/full", document) + .asJsonObject["data"] + .asJsonArray + .map { it.asInt }, + ) + } else { + check( + assertThrows(ClientRequestFailure::class.java) { + query( + "textDocument/semanticTokens/full/delta", + document, + extra = mapOf("previousResultId" to full.string("resultId")), + ) + }.message + ?.contains("not negotiated") == true, + ) + } + if (caps["range"]?.let { it.isJsonObject || it.asBoolean } == true) { + check( + query( + "textDocument/semanticTokens/range", + document, + extra = + mapOf( + "range" to + mapOf( + "start" to mapOf("line" to 0, "character" to 0), + "end" to mapOf("line" to 1, "character" to 0), + ), + ), + ).asJsonObject["data"] + .asJsonArray + .isEmpty, + ) + } + discard(document) + val reopened = open(data.string("file")) + if (deltaSupported) { + check( + query( + "textDocument/semanticTokens/full/delta", + reopened, + extra = mapOf("previousResultId" to full.string("resultId")), + ).asJsonObject + .has("data"), + ) + } + } + case("X127") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf(SharedScenarios.SourceModule("Resolve", uri(data.string("file")), emptyList())), + ) + val document = open(data.string("file")) + val action = + query( + "textDocument/codeAction", + document, + extra = + mapOf( + "range" to + mapOf( + "start" to mapOf("line" to 0, "character" to 0), + "end" to + mapOf("line" to 0, "character" to document.text.length), + ), + "context" to mapOf("diagnostics" to emptyList()), + ), + ).rows() + .single { it.string("title") == data.string("actionTitle") } + val capability = protocol.capabilities().asJsonObject["codeActionProvider"] + if ( + capability.isJsonObject && capability.asJsonObject["resolveProvider"]?.asBoolean == true + ) { + check(action.has("data") && !action.has("edit")) + val resolved = protocol.query("codeAction/resolve", action).asJsonObject + check(resolved["edit"].asJsonObject["documentChanges"].asJsonArray.size() == 1) + check(resolved.string("title") == action.string("title")) + replace(document, "\n" + data.string("source")) + check( + assertThrows(ClientRequestFailure::class.java) { protocol.query("codeAction/resolve", action) } + ?.message + ?.contains("expired or changed") == true, + ) + } else { + check(action.has("edit")) + } + } + case("X128") { data -> + data["variants"].rows().forEach { variant -> + write(variant.string("root"), variant.string("source")) + write(variant.string("member"), variant.string("memberSource")) + configure( + listOf( + SharedScenarios.SourceModule("App", uri(variant.string("root")), emptyList()), + ), + ) + val document = open(variant.string("root")) + clean(document) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(FileTreeOperations::class) + .rename( + singleProject(), + directory.resolve(variant.string("from")).toString(), + ) + } + chooseXtcFileRename() + fillRenameDialog(variant.string("newName")) + awaitUi("native file rename updates references", 45.seconds) { + document.text == variant.string("expected") + } + } + check(open(variant.string("target")).text == variant.string("targetSource")) + clean(document) + } + } + + case("X129") { data -> + val root = with(driver) { Path.of(singleProject().getBasePath()) } + val report = root.resolve(".gradle/xtc/lsp-model.json") + val model = + JsonParser + .parseString( + data["model"] + .toString() + .replace("\${workspace}", directory.toUri().toString().trimEnd('/')), + ).asJsonObject + val inputs = model["sourceSets"].asJsonArray[0].asJsonObject + val resources = inputs["resourceRoots"].deepCopy() + + fun refresh() = + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class).refreshBuildModel(singleProject()) + } + } + + fun writeModel() { + Files.createDirectories(report.parent) + Files.writeString(report, model.toString()) + refresh() + } + + fun automatic() = + with(driver) { + withContext(OnDispatcher.EDT) { + check( + utility(CompilerSettingsPage::class) + .useBuildModel(singleProject()) + .contains("Gradle model"), + ) + } + } + try { + write(data.string("file"), data.string("source")) + write(data.string("resource"), data.string("contents")) + writeModel() + automatic() + val document = open(data.string("file")) + clean(document) + inputs.add("resourceRoots", Gson().toJsonTree(emptyList())) + writeModel() + errors(document) + inputs.add("resourceRoots", resources) + writeModel() + clean(document) + Files.writeString(report, "{") + refresh() + clean(document) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class).clearProjectGraph(singleProject()) + } + } + configure( + listOf( + SharedScenarios.SourceModule( + "GradleAssets", + document.uri, + emptyList(), + emptyList(), + ), + ), + ) + errors(document) + writeModel() + errors(document) + automatic() + clean(document) + } finally { + Files.deleteIfExists(report) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(CompilerSettingsPage::class).clearProjectGraph(singleProject()) + } + } + refresh() + } + } + case("X130") { data -> + data["files"].rows().forEach { write(it.string("file"), it.string("text")) } + val target = Files.createDirectories(directory.resolve(data.string("destination"))) + refresh(target) + // A focused run has no earlier source editor to start the installed language client. + val document = open(data.string("consumer")) + protocol.server() + val root = with(driver) { Path.of(singleProject().getBasePath()) } + with(driver) { changeWorkspaceFolders(root, directory) } + configure(null) + try { + clean(document) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(FileTreeOperations::class) + .move( + singleProject(), + data.strings("sources").map { directory.resolve(it).toString() }, + target.toString(), + ) + } + val dialog = ui.dialog(title = "Move Ecstasy Sources") + awaitUi("native Move dialog", 45.seconds) { dialog.present() } + withContext(OnDispatcher.EDT) { + cast(dialog.button("Refactor").component, NativeButton::class).doClick() + } + + fun moved(expected: Boolean) = + data.strings("sources").all { + Files.exists(target.resolve(it)) == expected && + Files.exists(directory.resolve(it)) != expected + } + awaitUi("all selected containers moved", 45.seconds) { moved(true) } + clean(document) + listOf("\$Undo" to false, "\$Redo" to true).forEach { (action, expected) -> + // A file-only Move belongs to project history, not the unchanged Consumer + // editor. Check availability before dispatch instead of timing out on a no-op. + awaitUi("project $action is available") { + withContext(OnDispatcher.EDT) { + utility(FileTreeOperations::class) + .globalHistoryAvailable(singleProject(), expected) + } + } + invokeGlobalBackendAction(action, project = singleProject(), now = false) + awaitUi("one $action restores all container paths", 45.seconds) { + val confirm = ui.dialog(title = if (expected) "Redo" else "Undo") + if (confirm.present()) { + withContext(OnDispatcher.EDT) { + cast( + confirm.button(if (expected) "Redo" else "Undo").component, + NativeButton::class, + ).doClick() + } + } + moved(expected) + } + clean(document) + } + } + data["files"].rows().forEach { file -> + val original = file.string("file") + val moved = + if (data.strings("sources").any { original.startsWith("$it/") }) { + "${data.string("destination")}/$original" + } else { + original + } + check(Files.readString(directory.resolve(moved)) == file.string("text")) + } + } finally { + configure(emptyList()) + with(driver) { changeWorkspaceFolders(directory, root) } + } + } + case("X131") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + val lens = + query("textDocument/codeLens", document).rows().single { + it["range"].asJsonObject["start"].asJsonObject["line"].asInt == + 0 + } + val resolvedLens = + if (lens.has("data")) protocol.query("codeLens/resolve", lens).asJsonObject else lens + check(resolvedLens["range"] == lens["range"]) + check(resolvedLens["command"].asJsonObject["arguments"].asJsonArray.size() == 2) + val link = query("textDocument/documentLink", document).rows().single() + val resolvedLink = + if (link.has("data")) { + protocol.query("documentLink/resolve", link).asJsonObject + } else { + link + } + check(resolvedLink.string("target") == data.string("link")) + val hints = + query( + "textDocument/inlayHint", + document, + extra = + mapOf( + "range" to + mapOf( + "start" to mapOf("line" to 0, "character" to 0), + "end" to mapOf("line" to 6, "character" to 0), + ), + ), + ).rows() + val hint = hints.first { it.has("data") || it.has("tooltip") } + val resolvedHint = + if (hint.has("data")) protocol.query("inlayHint/resolve", hint).asJsonObject else hint + check(resolvedHint["label"] == hint["label"] && resolvedHint.has("tooltip")) + val symbol = + protocol + .query("workspace/symbol", mapOf("query" to data.string("module"))) + .rows() + .first() + val resolvedSymbol = + if (symbol.has("data")) { + protocol.query("workspaceSymbol/resolve", symbol).asJsonObject + } else { + symbol + } + check(resolvedSymbol["location"].asJsonObject.has("range")) + replace(document, "\n" + data.string("source")) + listOf( + "codeLens/resolve" to lens, + "documentLink/resolve" to link, + "inlayHint/resolve" to hint, + "workspaceSymbol/resolve" to symbol, + ).filter { it.second.has("data") } + .forEach { (method, item) -> + check( + assertThrows(ClientRequestFailure::class.java) { protocol.query(method, item) } + ?.message + ?.contains("expired or changed") == true, + ) + } + } + case("X132") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + val edits = + query( + "textDocument/rangesFormatting", + document, + extra = + mapOf( + "options" to mapOf("tabSize" to 4, "insertSpaces" to true), + "ranges" to data["ranges"], + ), + ).rows() + check( + edits.map { it["range"].asJsonObject["start"].asJsonObject["line"].asInt } == + data["lines"].asJsonArray.map { it.asInt }, + ) + check(edits.all { it.string("newText") == data.string("indent") }) + val sync = protocol.capabilities().asJsonObject["textDocumentSync"] + if (sync.isJsonObject) { + val save = + mapOf("textDocument" to mapOf("uri" to uri(data.string("file"))), "reason" to 1) + if (sync.asJsonObject["willSave"]?.asBoolean == true) { + protocol.notify("textDocument/willSave", save) + } + if (sync.asJsonObject["willSaveWaitUntil"]?.asBoolean == true) { + check(protocol.query("textDocument/willSaveWaitUntil", save).rows().isEmpty()) + } + } + check(document.text == data.string("source")) + } + case("X133") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + + fun linked(anchor: String) = + query("textDocument/linkedEditingRange", document, document.text.indexOf(anchor)) + .asJsonObject + + fun ranges(anchor: String) = linked(anchor)["ranges"]?.takeUnless { it.isJsonNull }?.asJsonArray + check( + ranges(data.string("anchor"))!!.map { + it.asJsonObject["start"].asJsonObject["character"].asInt + } == data.strings("uses").map { data.string("source").indexOf(it) }, + ) + data.strings("refused").forEach { check(ranges(it)?.size() in listOf(null, 0)) } + replace(document, data.string("broken")) + errors(document) + check(ranges(data.string("anchor"))?.size() in listOf(null, 0)) + replace(document, data.string("source")) + clean(document) + check(ranges(data.string("anchor"))?.size() == 2) + } + case("X134") { data -> + val external = Files.createTempDirectory("xtc-playbook-source-").toRealPath() + try { + val library = external.resolve(data.string("libraryFile")) + Files.writeString(library, data.string("library")) + write(data.string("file"), data.string("consumer")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("libraryModule"), + library.toUri().toString(), + emptyList(), + ), + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + listOf(data.string("libraryModule")), + ), + ), + ) + val document = open(data.string("file")) + clean(document) + Files.writeString(library, data.string("brokenLibrary")) + errors(document) + Files.writeString(library, data.string("library")) + clean(document) + Files.delete(library) + errors(document) + Files.writeString(library, data.string("library")) + clean(document) + } finally { + configure(emptyList()) + Files.walk(external).use { paths -> + paths.sorted(Comparator.reverseOrder()).forEach(Files::deleteIfExists) + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.PartialResults", plugin = "org.xtclang.playbook.probe") +interface PartialResults { + fun listen( + project: Project, + token: String, + ): PartialResults + + fun values(): List + + fun dispose() +} + +@Remote("org.xtclang.idea.playbook.probe.WorkspaceEdits", plugin = "org.xtclang.playbook.probe") +interface WorkspaceEdits { + fun apply( + project: Project, + json: String, + ): ClientFuture +} + +@Remote("org.xtclang.idea.playbook.probe.FileTreeOperations", plugin = "org.xtclang.playbook.probe") +interface FileTreeOperations { + fun globalHistoryAvailable( + project: Project, + redo: Boolean, + ): Boolean + + fun move( + project: Project, + paths: List, + destination: String, + ) + + fun rename( + project: Project, + path: String, + ) + + fun loadDirectory(path: String) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt new file mode 100644 index 00000000000..e78754d1d44 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt @@ -0,0 +1,123 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.util.concurrent.CancellationException +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.progressCases() { + case("X145") { data -> + write(data.string("file"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("module"), + uri(data.string("file")), + emptyList(), + ), + ), + ) + val document = open(data.string("file")) + val title = "Finding references" + + fun workload(methods: Int) = + "module ${data.string("module")} {\n static Int value = 1;\n" + + (0 until methods).joinToString("\n") { + " Int read$it() { return value; }" + } + + "\n}\n" + + val large = workload(data["methods"].asInt) + + fun visible() = + with(driver) { + withContext(OnDispatcher.EDT) { + utility(ProgressUi::class).visible(singleProject(), title) + } + } + + fun start(text: String): ClientFuture { + replace(document, text, settle = false) + flush(document) + val pending = + protocol.request( + "textDocument/references", + document.params(text.indexOf("value")) + + mapOf("context" to mapOf("includeDeclaration" to true)), + ) + with(driver) { + awaitUi("real compiler reference progress", 20.seconds) { + check(!pending.isDone()) { "Workload completed before progress was exercised" } + visible() + } + withContext(OnDispatcher.EDT) { utility(ProgressUi::class).show(singleProject()) } + } + return pending + } + try { + val canceled = start(large) + with(driver) { + awaitUi("visible progress Cancel control", 20.seconds) { + check(!canceled.isDone()) { "Workload completed before Cancel was exercised" } + // False means the button is not visible yet; a successful click ends polling. + withContext(OnDispatcher.EDT) { + utility(ProgressUi::class).cancel(singleProject(), title) + } + } + } + val outcome = + runCatching { protocol.await("textDocument/references", canceled) } + .onFailure { if (it !is CancellationException && it.mustStopPlaybook()) throw it } + check( + outcome.exceptionOrNull().let { + it is CancellationException || (it is ClientRequestFailure && it.code == ResponseErrorCode.RequestCancelled.value) + }, + ) { + "Cancel must terminate the pending request: ${outcome.exceptionOrNull() ?: "completed before cancellation"}" + } + with(driver) { awaitUi("canceled progress disappears", 15.seconds) { !visible() } } + check(!query("textDocument/hover", document, large.indexOf("value")).isJsonNull) + clean(document) + val wrapper = protocol.server() + val previous = requireNotNull(wrapper.getCurrentProcessId()) + val pending = start(large + "// unsaved restart\n") + wrapper.restart() + with(driver) { + awaitUi("old process exits and pending request retires", 30.seconds) { + pending.isDone() && !utility(ProgressUi::class).alive(previous) + } + } + check(protocol.server().getCurrentProcessId() != previous) + check(document.text == large + "// unsaved restart\n") + replace(document, data.string("source")) + clean(document) + with(driver) { awaitUi("retired progress disappears", 15.seconds) { !visible() } } + } finally { + with(driver) { + withContext(OnDispatcher.EDT) { utility(ProgressUi::class).hide(singleProject()) } + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.ProgressUi", plugin = "org.xtclang.playbook.probe") +internal interface ProgressUi { + fun visible( + project: Project, + title: String, + ): Boolean + + fun show(project: Project) + + fun hide(project: Project) + + fun cancel( + project: Project, + title: String, + ): Boolean + + fun alive(pid: Long): Boolean +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt new file mode 100644 index 00000000000..0f4bd28008b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt @@ -0,0 +1,77 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Editor +import com.intellij.driver.sdk.singleProject +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.referenceLensCases() { + case("X279") { data -> + write(data.string("file"), data.string("source")) + write(data.string("consumerFile"), data.string("consumer")) + configure( + listOf( + SharedScenarios.SourceModule(data.string("module"), uri(data.string("file")), emptyList()), + SharedScenarios.SourceModule( + data.string("consumerModule"), + uri(data.string("consumerFile")), + listOf(data.string("module")), + ), + ), + ) + val project = with(driver) { singleProject() } + val document = open(data.string("file")) + val page = with(driver) { utility(LanguageServicePage::class) } + val probe = with(driver) { utility(CodeLensProbe::class) } + val before = with(driver) { withContext(OnDispatcher.EDT) { page.content(project) } } + + fun setting(enabled: Boolean) = with(driver) { withContext(OnDispatcher.EDT) { page.referenceCodeLens(project, enabled) } } + + fun titles() = with(driver) { withContext(OnDispatcher.EDT) { probe.titles(document.editor.editor) } } + + fun click() = + with(driver) { + focusEditor(document.editor) + withContext(OnDispatcher.EDT) { probe.click(document.editor.editor, data.string("initialTitle")) } + } + try { + setting(true) + clean(document) + awaitUi("native reference Code Vision includes the closed consumer", 45.seconds) { data.string("initialTitle") in titles() } + click() + with(driver) { + chooseTargets( + document.editor, + listOf(data.string("file"), data.string("consumerFile")), + data.string("consumerFile"), + ::click, + ) + } + val consumer = open(data.string("consumerFile")) + replace(consumer, data.string("changedConsumer")) + clean(consumer) + open(data.string("file")) + awaitUi("native reference Code Vision refreshes after a consumer edit", 45.seconds) { data.string("changedTitle") in titles() } + setting(false) + awaitUi("reference counts disappear while Run stays visible", 30.seconds) { + titles().let { values -> values.any { "Run" in it } && values.none { "reference" in it } } + } + setting(true) + awaitUi("reference counts return without restarting", 30.seconds) { data.string("changedTitle") in titles() } + check(document.text == data.string("source")) + } finally { + with(driver) { withContext(OnDispatcher.EDT) { page.restore(project, before) } } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.CodeLensUi", plugin = "org.xtclang.playbook.probe") +interface CodeLensProbe { + fun titles(editor: Editor): List + + fun click( + editor: Editor, + title: String, + ) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt new file mode 100644 index 00000000000..ff60c3d7fda --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt @@ -0,0 +1,285 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.reliabilityCases() { + case("X146") { data -> + write(data.string("library"), data.string("original")) + write(data.string("consumer"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule( + data.string("libraryModule"), + uri(data.string("library")), + emptyList(), + ), + SharedScenarios.SourceModule( + data.string("consumerModule"), + uri(data.string("consumer")), + listOf(data.string("libraryModule")), + ), + ), + ) + val library = open(data.string("library")) + val consumer = open(data.string("consumer")) + val version = version(consumer) + val support = with(driver) { semanticSupport(consumer.editor).getInlayHintsSupport() } + val events = with(driver) { utility(RefreshRequests::class).listen(singleProject()) } + val tabLimit = with(driver) { withContext(OnDispatcher.EDT) { events.tabLimit(32) } } + val negotiated = events.negotiated() + check(negotiated.containsAll(listOf("workspace/inlayHint/refresh", "workspace/semanticTokens/refresh"))) + + fun verify(expected: String) { + // Read the native provider's automatically refreshed future, without manually + // requesting inlays or editing the consumer to trigger another analysis. + with(driver) { + awaitUi("untouched consumer receives $expected inlay", 45.seconds) { + support + .getValidLSPFuture() + ?.takeIf { it.isDone() && !it.isCompletedExceptionally() } + ?.get() + ?.any { item -> + protocol + .copy(item.inlayHint()) + .asJsonObject["label"] + .toString() + .contains(expected) + } == true + } + } + check(consumer.text == data.string("source")) + check(version(consumer) == version) + } + try { + verify(data.string("before")) + // Native-only UP27 stress: many distinct documents and repeated refresh requests + // used to overwhelm IntelliJ's read-action submission tracker. The shared semantic + // assertions below must still pass after this burst, with hints enabled. + repeat(16) { index -> + open("RefreshQueue$index.x", "module RefreshQueue$index {}\n") + } + with(driver) { + val refreshed = withContext(OnDispatcher.EDT) { events.burst() } + awaitUi("many-file refresh burst completes", 15.seconds) { refreshed.isDone() } + check(!refreshed.isCompletedExceptionally()) + } + listOf("changed" to "after", "original" to "before").forEach { (source, expected) -> + events.clear() + replace(library, data.string(source)) + // Display the untouched consumer again; no source mutation or explicit inlay + // request. + consumer.editor + with(driver) { + awaitUi("native dependency refresh requests", 45.seconds) { + events + .values() + .containsAll(negotiated) + } + } + verify(data.string(expected)) + check(events.values().all { it in negotiated }) { "Unnegotiated refresh: ${events.values()}" } + clean(consumer) + } + } finally { + with(driver) { withContext(OnDispatcher.EDT) { events.tabLimit(tabLimit) } } + events.dispose() + } + } + case("X147") { data -> + val document = open(data.string("file"), data.string("source")) + clean(document) + with(driver) { + val settings = utility(LanguageServicePage::class).content(singleProject()) + val page = + withContext(OnDispatcher.EDT) { + utility(CompilerReportPage::class).open(singleProject()) + } + + fun text() = withContext(OnDispatcher.EDT) { page.text() } + + fun complete( + index: Int, + name: String, + ) { + val barrier = withContext(OnDispatcher.EDT) { page.complete(index, name) } + awaitUi("settings callback and EDT publication complete", 15.seconds) { + barrier.isDone() + } + check(!barrier.isCompletedExceptionally()) + } + try { + withContext(OnDispatcher.EDT) { page.reset() } + complete(1, "NewestReport") + check(text().contains("NewestReport")) + complete(0, "RetiredReport") + check(text().contains("NewestReport") && !text().contains("RetiredReport")) + withContext(OnDispatcher.EDT) { page.reset() } + val pendingPublication = text() + val replyPid = protocol.server().getCurrentProcessId() + val publication = + withContext(OnDispatcher.EDT) { + page.completeAndRestart(2, "RetiredBeforePublication") + } + awaitUi("restart between report receipt and publication", 45.seconds) { + publication.isDone() && + protocol.server().getCurrentProcessId()?.let { it != replyPid } == true + } + check(!publication.isCompletedExceptionally()) + check(text() == pendingPublication) + withContext(OnDispatcher.EDT) { page.reset() } + val before = text() + val previousPid = protocol.server().getCurrentProcessId() + withContext(OnDispatcher.EDT) { + utility(LanguageServicePage::class).transport(singleProject(), "incremental") + } + awaitUi("new connection owns settings", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previousPid } == true + } + complete(3, "OldSettings") + check(text() == before) + awaitUi("retired process exits", 45.seconds) { + previousPid == null || + !ProcessHandle.of(previousPid).map { it.isAlive }.orElse(false) + } + withContext(OnDispatcher.EDT) { page.reset() } + val disposed = text() + withContext(OnDispatcher.EDT) { page.dispose() } + complete(4, "DisposedReport") + check(text() == disposed) + check(document.text == data.string("source")) + } finally { + withContext(OnDispatcher.EDT) { + page.dispose() + utility(LanguageServicePage::class).restore(singleProject(), settings) + } + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.RefreshRequests", plugin = "org.xtclang.playbook.probe") +interface RefreshRequests { + fun listen(project: Project): RefreshRequests + + fun values(): List + + fun negotiated(): List + + fun clear() + + fun tabLimit(value: Int): Int + + fun burst(): ClientFuture + + fun dispose() +} + +@Remote("org.xtclang.idea.playbook.probe.CompilerReportPage", plugin = "org.xtclang.playbook.probe") +interface CompilerReportPage { + fun open(project: Project): CompilerReportPage + + fun reset() + + fun text(): String + + fun complete( + index: Int, + name: String, + ): ClientFuture + + fun completeAndRestart( + index: Int, + name: String, + ): ClientFuture + + fun dispose() +} + +internal fun ParityScenarios.refreshOverlapCases() { + case("X259") { data -> + write(data.string("library"), data.string("original")) + write(data.string("consumer"), data.string("source")) + configure( + listOf( + SharedScenarios.SourceModule(data.string("libraryModule"), uri(data.string("library")), emptyList()), + SharedScenarios.SourceModule( + data.string("consumerModule"), + uri(data.string("consumer")), + listOf(data.string("libraryModule")), + ), + ), + ) + val library = open(data.string("library")) + val consumer = open(data.string("consumer")) + val version = version(consumer) + with(driver) { + val page = utility(LanguageServicePage::class) + val settings = page.content(singleProject()) + + fun hints(enabled: Boolean) { + withContext(OnDispatcher.EDT) { page.inlayHints(singleProject(), enabled) } + } + + fun visible(expected: String? = null) { + val editor = consumer.editor + awaitUi("untouched consumer displays ${expected ?: "no"} hints", 45.seconds) { + val installed = + withContext(OnDispatcher.EDT) { + cast(editor.editor, NativeInlayEditor::class) + .getInlayModel() + .getInlineElementsInRange(0, consumer.text.length) + .filter { it.isValid() && it.getWidthInPixels() > 0 } + .map { it.getOffset() } + } + if (expected == null) { + installed.isEmpty() + } else { + // Reacquire after restart, read the automatic provider and prove its hints + // are installed in the real editor. Never query inlays from the harness. + val values = + semanticSupport(editor) + .getInlayHintsSupport() + .getValidLSPFuture() + ?.takeIf { it.isDone() && !it.isCompletedExceptionally() } + ?.get() + ?.map { protocol.copy(it.inlayHint()).asJsonObject } + .orEmpty() + val other = if (expected == data.string("before")) data.string("after") else data.string("before") + values.any { it["label"].toString().contains(expected) } && + values.none { it["label"].toString().contains(other) } && + values.all { ParityWorkspace.offset(consumer.text, it.getAsJsonObject("position")) in installed } + } + } + check(consumer.text == data.string("source")) + check(version(consumer) == version) + } + try { + hints(true) + visible(data.string("before")) + hints(false) + visible() + val previous = requireNotNull(protocol.server().getCurrentProcessId()) + replace(library, data.string("changed"), settle = false) + hints(true) + protocol.server().restart() + awaitUi("retired refresh producer exits", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previous } == true && + !ProcessHandle.of(previous).map { it.isAlive }.orElse(false) + } + visible(data.string("after")) + clean(consumer) + hints(false) + replace(library, data.string("original"), settle = false) + hints(true) + visible(data.string("before")) + clean(consumer) + } finally { + withContext(OnDispatcher.EDT) { page.restore(singleProject(), settings) } + } + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt new file mode 100644 index 00000000000..1ba606300eb --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt @@ -0,0 +1,215 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonElement +import com.intellij.driver.client.Remote +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.VirtualFile +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.util.concurrent.CancellationException +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.renameCases() { + case("X53") { data -> + val document = open(data.string("file")) + applyRename(document, document.at(data.string("anchor")), data.string("replaceWith")) + check( + document.text == + fixture(document.file) + .replace(data.pattern("replaceFrom"), data.string("replaceWith")), + ) + } + case("X54") { data -> + val document = open(data.string("file")) + data.strings("anchors").forEach { anchor -> + replace(document, fixture(document.file)) + val offset = + data.int( + if (anchor == data.string("declaration")) "callOffset" else "declarationOffset", + ) + applyRename(document, document.at(anchor, offset), data.string("replaceWith")) + check( + document.text == + fixture(document.file) + .replace(data.pattern("replaceFrom"), data.string("replaceWith")), + ) + check(data.string("namedArgument") in document.text) + } + } + case("X55") { data -> + val document = open(data.string("file")) + check( + proposedRename(document, document.at(data.string("anchor")), data.string("newName")) + .isJsonNull, + ) + check(document.text == fixture(document.file)) + clean(document) + } + case("X56") { data -> + val document = open(data.string("file")) + data.strings("variants").forEach { anchor -> + check( + proposedRename(document, document.at(anchor), data.string("newName")).isJsonNull, + ) { + anchor + } + } + replace( + document, + fixture(document.file).replace(data.string("replaceFrom"), data.string("replaceWith")), + ) + check( + proposedRename( + document, + document.at(data.string("anchor"), data.int("offset")), + data.string("newName"), + ).isJsonNull, + ) + } + case("X57") { data -> + val document = open(data.string("file")) + val transaction = + renameTransaction(document, document.at(data.string("anchor")), data.string("newName")) + val edit = + protocol.copy(driver.cast(transaction.getEdit(), ClientValue::class)).asJsonObject + // LSP4J's no-argument constructor restores an empty map when the wire field is absent. + check(edit["changes"].let { it == null || it.isJsonNull || it.asJsonObject.size() == 0 }) { + edit.toString() + } + val change = edit["documentChanges"].rows().single() + val oldVersion = version(document) + check(change["textDocument"].asJsonObject.int("version") == oldVersion) + replace(document, "\n" + fixture(document.file)) + check(version(document) != oldVersion) + // Apply through the same production transaction as the native Rename action. + // Comparing versions only in the driver would not prove that IntelliJ rejects the edit. + with(driver) { + withContext(OnDispatcher.EDT) { + check(!transaction.apply()) { + "Native rename accepted an obsolete document snapshot" + } + } + } + check(document.text == "\n" + fixture(document.file)) { + "Native rename applied an edit for an obsolete document version" + } + replace(document, fixture(document.file)) + val retired = + renameTransaction(document, document.at(data.string("anchor")), data.string("newName")) + val closing = + protocol.request( + "textDocument/rename", + document.params(document.at(data.string("anchor"))) + + mapOf("newName" to data.string("newName")), + ) + discard(document) + awaitRetired("textDocument/rename", closing) + val reopened = open(document.file) + check(reopened.text == fixture(document.file)) + with(driver) { + withContext(OnDispatcher.EDT) { + check(!retired.apply()) { + "Native rename accepted a closed/reopened document epoch" + } + } + } + check(reopened.text == fixture(document.file)) + applyRename(reopened, reopened.at(data.string("anchor")), data.string("newName")) + } + case("X58") { data -> + val document = open(data.string("file")) + replace( + document, + fixture(document.file).replace(data.string("replaceFrom"), data.string("replaceWith")), + ) + errors(document) + check( + proposedRename(document, document.at(data.string("anchor")), data.string("newName")) + .isJsonNull, + ) + replace(document, fixture(document.file)) + clean(document) + data.strings("invalidNames").forEach { name -> + check(proposedRename(document, document.at(data.string("anchor")), name).isJsonNull) { + name + } + } + applyRename(document, document.at(data.string("anchor")), data.string("newName")) + } +} + +/** Retain the real production transaction so delayed application exercises its write guard. */ +internal fun ParityWorkspace.renameTransaction( + document: ParityWorkspace.Document, + at: Int, + name: String, +): NativeRenameTransaction { + val server = protocol.server() + return with(driver) { + val pending = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(NativeRenameRequests::class) + .request(server, document.editor.editor.getVirtualFile(), at, name) + } + awaitUi("native rename transaction", 60.seconds) { pending.isDone() } + requireNotNull(cast(pending, NativeRenameFuture::class).get()) + } +} + +internal fun ParityWorkspace.proposedRename( + document: ParityWorkspace.Document, + at: Int, + name: String, +): JsonElement = query("textDocument/rename", document, at, mapOf("newName" to name)) + +internal fun ParityWorkspace.applyRename( + document: ParityWorkspace.Document, + at: Int, + name: String, +) { + val before = document.text + with(driver) { + rename(document.editor, at, name) + awaitUi("native rename changes ${document.file}", 45.seconds) { document.text != before } + } + settle(document) + clean(document) +} + +internal fun ParityWorkspace.awaitRetired( + method: String, + future: ClientFuture, +) { + try { + protocol.await(method, future) + } catch (_: CancellationException) { + // Client cancellation is an allowed outcome for a deliberately retired request. + } catch (error: ClientRequestFailure) { + check(error.code == ResponseErrorCode.RequestCancelled.value || error.code == ResponseErrorCode.ContentModified.value) { error } + } +} + +@Remote("org.xtclang.idea.lsp.XtcRenameEdit", plugin = "org.xtclang.idea") +interface NativeRenameRequests { + fun request( + wrapper: StartedLanguageServer, + file: VirtualFile, + offset: Int, + name: String, + ): ClientFuture +} + +@Remote("org.eclipse.lsp4j.WorkspaceEdit", plugin = "com.redhat.devtools.lsp4ij") +interface NativeWorkspaceEdit + +@Remote("org.xtclang.idea.lsp.XtcRenameEdit", plugin = "org.xtclang.idea") +interface NativeRenameTransaction { + fun getEdit(): NativeWorkspaceEdit + + fun apply(): Boolean +} + +@Remote("java.util.concurrent.CompletableFuture") +interface NativeRenameFuture : ClientFuture { + fun get(): NativeRenameTransaction? +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt new file mode 100644 index 00000000000..6f384cc675f --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt @@ -0,0 +1,178 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import java.nio.file.Files +import java.util.zip.ZipFile +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.runtimeSettingsCases() { + listOf("X269", "X270").forEach { id -> + case(id) { + val data = common["runtimeSettings"].asJsonObject + val document = open(data.string("file"), data.string("source")) + clean(document) + + fun status() = protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + with(driver) { + val page = utility(ServerRuntimePage::class) + val project = singleProject() + val original = withContext(OnDispatcher.EDT) { page.content() } + + fun edit( + values: JsonObject, + action: String, + ) = withContext(OnDispatcher.EDT) { page.edit(values.toString(), action) } + + fun restart(previous: Int) { + edit(JsonObject(), "restart") + awaitUi("runtime settings restart completed", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previous.toLong() } == true + } + clean(document) + } + try { + val before = status() + val values = + JsonObject().apply { + addProperty("vmOptions", data["vmOptions"].asJsonArray.joinToString("\n") { it.asString }) + if (id == "X270") data["logs"].asJsonObject.entrySet().forEach { (key, value) -> add("xtc.logs.$key", value) } + } + edit(values, "cancel") + edit(values, "reset") + edit(values, "apply") + check(status().int("pid") == before.int("pid")) { "Apply must preserve the running server" } + restart(before.int("pid")) + val applied = status() + check(applied["jvmOptions"].asJsonArray.contains(data["vmOptions"].asJsonArray.single())) + if (id == "X269") { + edit( + JsonObject().apply { + addProperty("vmOptions", data["invalidOptions"].asJsonArray.joinToString("\n") { it.asString }) + }, + "invalid", + ) + } else { + check(applied["logs"].asJsonObject["retention"] == data["logs"]) + check(applied["logs"].asJsonObject["directory"] != before["logs"].asJsonObject["directory"]) + val destination = Files.createTempFile("ecstasy-server-logs-", ".zip") + try { + page.export(project, destination.toString()) + check(Files.size(destination) < 6 * 1024 * 1024) + ZipFile(destination.toFile()).use { zip -> + check(zip.entries().asSequence().any { it.name.endsWith("server.log") }) + val manifest = + zip.getInputStream(zip.getEntry("manifest.json")).reader().use { + JsonParser.parseReader(it).asJsonObject + } + check(manifest["service"].asJsonObject.int("pid") == applied.int("pid")) + check(manifest["service"].asJsonObject["logs"].asJsonObject["retention"] == data["logs"]) + } + } finally { + Files.deleteIfExists(destination) + } + edit(JsonObject().apply { addProperty("xtc.logs.totalSizeMb", 0) }, "invalid") + } + check(status().int("pid") == applied.int("pid")) + clean(document) + check(document.text == data.string("source")) + } finally { + val previous = status().int("pid") + edit(JsonParser.parseString(original).asJsonObject, "apply") + restart(previous) + } + } + } + } + listOf("X271", "X272").forEach { id -> + case(id) { + val data = common["runtimeSettings"].asJsonObject + val document = open(data.string("file"), data.string("source")) + clean(document) + val before = protocol.query("xtc/languageServiceStatus", emptyMap()).asJsonObject + val server = protocol.server() + with(driver) { + val page = utility(ServerRuntimePage::class) + val project = singleProject() + val original = withContext(OnDispatcher.EDT) { page.content() } + val destination = Files.createTempFile("ecstasy-offline-", ".zip") + try { + if (id == "X271") { + // Match the Language Servers panel's Stop action. Internal stop() leaves + // the wrapper enabled, so a background editor request can restart it. + withContext(OnDispatcher.EDT) { server.stopAndDisable() } + awaitUi("server stops before offline export", 30.seconds) { server.getServerStatus().name() == "stopped" } + } else { + val failing = + JsonObject().apply { + addProperty("vmOptions", data["startupFailureOptions"].asJsonArray.joinToString("\n") { it.asString }) + } + withContext(OnDispatcher.EDT) { page.edit(failing.toString(), "apply") } + server.restart() + awaitUi("intentional JVM startup failure recorded", 30.seconds) { + server.getServerStatus().name() == "stopped" && + run { + page.export(project, destination.toString()) + ZipFile(destination.toFile()).use { zip -> + zip.getInputStream(zip.getEntry("launcher.log")).reader().use { + it.readText().contains(data["startupFailureOptions"].asJsonArray.single().asString) + } + } + } + } + } + page.export(project, destination.toString()) + ZipFile(destination.toFile()).use { zip -> + val manifest = + zip.getInputStream(zip.getEntry("manifest.json")).reader().use { + JsonParser.parseReader(it).asJsonObject + } + check(manifest["offline"].asBoolean) + if (id == "X271") { + val expectedDirectory = before["logs"].asJsonObject.string("directory") + check(manifest.string("directory") == expectedDirectory) { + "Offline export directory ${manifest.string( + "directory", + )} differs from the stopped server's $expectedDirectory" + } + check(zip.entries().asSequence().any { it.name.endsWith("server.log") }) + check(server.getServerStatus().name() == "stopped") { "Offline export must not start the server" } + check( + server.getCurrentProcessId() == before.int("pid").toLong(), + ) { "Offline export must retain its stopped process" } + } else { + val launcher = zip.getInputStream(zip.getEntry("launcher.log")).reader().use { it.readText() } + check(launcher.contains(data["startupFailureOptions"].asJsonArray.single().asString)) + check(manifest.string("directory") != before["logs"].asJsonObject.string("directory")) + } + } + } finally { + withContext(OnDispatcher.EDT) { page.edit(original, "apply") } + server.restart() + clean(document) + check(document.text == data.string("source")) + Files.deleteIfExists(destination) + } + } + } + } +} + +@Remote("org.xtclang.idea.playbook.probe.ServerRuntimePage", plugin = "org.xtclang.playbook.probe") +interface ServerRuntimePage { + fun content(): String + + fun edit( + json: String, + action: String, + ) + + fun export( + project: Project, + destination: String, + ) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityScenarios.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityScenarios.kt new file mode 100644 index 00000000000..9ac177d69b4 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityScenarios.kt @@ -0,0 +1,56 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.intellij.driver.client.Driver + +/** + * Shared expectations, native editor actions and explicitly identified client protocol assertions. + */ +class ParityScenarios( + private val driver: Driver, + private val fixtures: Map, + private val shared: SharedScenarios, + private val execute: (String, () -> Unit) -> Unit, +) { + fun run() { + navigationCases() + semanticCases() + moduleCases() + dependencyCases() + renameCases() + graphCases() + platformCases() + typeMoveCases() + progressCases() + reliabilityCases() + indexingCases() + monikerCases() + libraryContentCases() + inlineCompletionCases() + refreshOverlapCases() + compilerImportCases() + librarySettingsCases() + runtimeSettingsCases() + colorCases() + highlightingCases() + documentationCases() + referenceLensCases() + } + + internal fun case( + id: String, + body: ParityWorkspace.(JsonObject) -> Unit, + ) { + execute(id) { + val workspace = ParityWorkspace(driver, id, fixtures, shared) + try { + workspace.body(shared.scenarios.getValue(id).values) + } catch (failure: Throwable) { + // Do not race cleanup with a driver command that may still be running. + if (!failure.mustStopPlaybook()) runCatching(workspace::close).onFailure(failure::addSuppressed) + throw failure + } + workspace.close() + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt new file mode 100644 index 00000000000..c4bf5bb9f5b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt @@ -0,0 +1,348 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.PsiManager +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.semanticCases() { + case("X39") { data -> + val doc = open(data.string("file")) + val leaf = calls(doc, doc.at(data.string("callee"), data.int("offset"))).single() + val incoming = callEdges(leaf, "incomingCalls") + check(incoming.size == data.int("edgeCount")) + check( + incoming + .single { + it.getAsJsonObject("from").string("name").contains(data.string("callerName")) + }["fromRanges"] + .asJsonArray + .size() == data.int("edgeCount"), + ) + check( + incoming + .single { + it.getAsJsonObject("from").string("name").contains(data.string("lambdaName")) + }["fromRanges"] + .asJsonArray + .size() == data.int("lambdaSites"), + ) + val run = calls(doc, doc.at(data.string("caller"), data.int("offset"))).single() + val outgoing = callEdges(run, "outgoingCalls") + check(outgoing.size == data.int("edgeCount")) + check( + outgoing + .map { + it + .getAsJsonObject("to")["selectionRange"] + .asJsonObject["start"] + .asJsonObject + .int("line") + }.toSet() + .size == data.int("edgeCount"), + ) + check( + outgoing.map { it["fromRanges"].asJsonArray.size() }.sorted() == + data["sitesPerCallee"].asJsonArray.map { it.asInt }, + ) + with(driver) { + nativeHierarchy( + doc, + doc.at(data.string("callee"), data.int("offset")), + "call", + listOf(data.string("callerName"), data.string("lambdaName")), + ) + } + } + case("X40") { data -> + val doc = open(data.string("file")) + val leaf = calls(doc, doc.at(data.string("callee"), data.int("offset"))).single() + val lambda = + callEdges( + leaf, + "incomingCalls", + ).single { + it.getAsJsonObject("from").string("name").contains(data.string("lambdaName")) + } + val outgoing = callEdges(lambda.getAsJsonObject("from"), "outgoingCalls") + check(outgoing.size == data.int("edgeCount")) + check(outgoing.single().getAsJsonObject("to")["selectionRange"] == leaf["selectionRange"]) + check( + outgoing + .single()["fromRanges"] + .rows() + .single() + .getAsJsonObject("start") + .int("line") == + ParityWorkspace.position(doc.text, doc.at(data.string("lambdaCall")))["line"], + ) + check(calls(doc, doc.at(data.string("dynamicCall"))).isEmpty()) + } + listOf("X41", "X154").forEach { id -> + case(id) { data -> + data["source"]?.asString?.let { write(data.string("file"), it) } + val doc = open(data.string("file")) + clean(doc) + val legend = + protocol + .capabilities() + .asJsonObject["semanticTokensProvider"] + .asJsonObject["legend"] + .asJsonObject + val support = with(driver) { semanticSupport(doc.editor).getSemanticTokensSupport() } + val tokens = + with(driver) { + awaitUi( + "native editor receives semantic token classifications", + 45.seconds, + getter = { + support + .getValidLSPFuture() + ?.takeIf { it.isDone() && !it.isCompletedExceptionally() } + ?.get() + ?.let { + protocol + .copy(it.getSemanticTokens()) + .asJsonObject["data"] + .asJsonArray + .map { value -> value.asInt } + } + }, + checker = { it != null }, + ) + }!! + + data class Token( + val line: Int, + val character: Int, + val text: String, + val type: String, + val modifiers: Set, + ) + val decoded = + tokens.chunked(5).fold(emptyList()) { previous, tuple -> + val line = (previous.lastOrNull()?.line ?: 0) + tuple[0] + val column = + (if (tuple[0] == 0) previous.lastOrNull()?.character ?: 0 else 0) + tuple[1] + previous + + Token( + line, + column, + doc.text.lines()[line].substring(column, column + tuple[2]), + legend["tokenTypes"].asJsonArray[tuple[3]].asString, + legend["tokenModifiers"] + .asJsonArray + .mapIndexedNotNull { bit, name -> + name.asString.takeIf { tuple[4] and (1 shl bit) != 0 } + }.toSet(), + ) + } + data["tokenKinds"].rows().forEach { kind -> + check(decoded.any { it.text == kind.string("name") && it.type == kind.string("type") }) + } + check( + decoded.any { + it.text == data.string("methodName") && + it.modifiers.containsAll( + listOf(data.string("staticModifier"), data.string("declarationModifier")), + ) + }, + ) + data["accesses"]?.rows()?.forEach { access -> + val at = ParityWorkspace.position(doc.text, doc.at(access.string("anchor"), access.int("offset"))) + val token = decoded.single { it.line == at["line"] && it.character == at["character"] } + check(("modification" in token.modifiers) == access["write"].asBoolean) { "Wrong access classification: $access; $token" } + } + val write = doc.at(data.string("anchor2")) + check( + decoded.any { + it.line == ParityWorkspace.position(doc.text, write)["line"] && + it.text == data.string("variableName") && + data.string("writeModifier") in it.modifiers + }, + ) + with(driver) { + focusEditor(doc.editor) + withContext(OnDispatcher.EDT) { + doc.editor.editor + .getCaretModel() + .moveToOffset(write) + } + invokeAction("HighlightUsagesInFile", component = doc.editor.component) + val highlights = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + val psi = + requireNotNull( + service(singleProject()) + .findFile(doc.editor.editor.getVirtualFile()), + ) + utility(LspFileSupport::class).getSupport(psi).getHighlightSupport() + } + awaitUi("native read/write occurrence highlights", 45.seconds) { + val future = highlights.getValidLSPFuture() + future != null && + future.isDone() && + !future.isCompletedExceptionally() && + future.get().let { values -> + fun contains( + at: Int, + kind: String, + ) = values.any { item -> + val p = ParityWorkspace.position(doc.text, at) + item.getRange().getStart().let { + it.getLine() == p["line"] && it.getCharacter() == p["character"] + } && item.getKind()?.name() == kind + } + contains(write, "Write") && + contains(doc.at(data.string("anchor"), data.int("offset")), "Read") + } + } + invokeAction("EditorEscape", component = doc.editor.component) + } + } + } + case("X42") { data -> + val doc = open(data.string("file")) + + fun verify(expected: JsonObject) { + clean(doc) + val support = with(driver) { semanticSupport(doc.editor).getInlayHintsSupport() } + val hints = + with(driver) { + awaitUi( + "native inlay provider has inferred types and parameter labels", + 45.seconds, + getter = { + support + .getValidLSPFuture() + ?.takeIf { + it.isDone() && !it.isCompletedExceptionally() + }?.get() + ?.map { protocol.copy(it.inlayHint()).asJsonObject } + }, + checker = { it != null && it.isNotEmpty() }, + ) + }!! + val labels = + hints.map { + it["label"].let { label -> + if (label.isJsonPrimitive) { + label.asString + } else { + label.rows().joinToString("") { part -> part.string("value") } + } + } + } + expected.strings("hints").forEach { expected -> + check(labels.any { it.contains(expected) }) + } + check( + hints.none { + it.getAsJsonObject("position").int("line") == + ParityWorkspace.position(doc.text, doc.at(expected.string("anchor")))[ + "line", + ] + }, + ) + check(hints.count { it.int("kind") == 1 } == expected.int("typeHintCount")) + check(labels.none { it.contains(expected.string("excludedHint")) }) + val offsets = + hints + .map { ParityWorkspace.offset(doc.text, it.getAsJsonObject("position")) } + .toSet() + with(driver) { + awaitUi("inlay labels are installed in the editor", 45.seconds) { + withContext(OnDispatcher.EDT) { + cast(doc.editor.editor, NativeInlayEditor::class) + .getInlayModel() + .getInlineElementsInRange(0, doc.text.length) + .filter { it.isValid() && it.getWidthInPixels() > 0 } + .map { it.getOffset() } + .containsAll(offsets) + } + } + } + } + verify(data) + val inferred = data.getAsJsonObject("inferred") + replace(doc, inferred.string("source")) + verify(inferred) + } + case("X43") { data -> + val doc = open(data.string("file")) + + fun current() = calls(doc, doc.at(data.string("anchor"), data.int("offset"))).single() + val old = current() + replace( + doc, + fixture(doc.file) + .replace(data.string("replaceFrom"), data.string("shiftedDeclaration")), + ) + check(callEdges(old, "outgoingCalls").isEmpty()) + val fresh = current() + check( + fresh["selectionRange"].asJsonObject["start"].asJsonObject.int("line") == + old["selectionRange"].asJsonObject["start"].asJsonObject.int("line") + + data.int("lineShift"), + ) + check(callEdges(fresh, "outgoingCalls").size == data.int("edgeCount")) + replace(doc, doc.text.replace(data.string("anchor"), data.string("replaceWith"))) + check(callEdges(fresh, "outgoingCalls").isEmpty()) + replace(doc, fixture(doc.file)) + check(callEdges(current(), "outgoingCalls").size == data.int("edgeCount")) + discard(doc) + val reopened = open(doc.file) + check( + callEdges( + calls(reopened, reopened.at(data.string("anchor"), data.int("offset"))) + .single(), + "outgoingCalls", + ).size == data.int("edgeCount"), + ) + } + case("X44") { data -> + val memberText = + fixture(data.string("memberFile")) + .replace(data.string("memberDeclaration"), data.string("memberWithCall")) + write(data.string("memberFile"), memberText) + val root = + open( + data.string("rootFile"), + fixture(data.string("rootFile")) + .replace(data.string("replaceFrom"), data.string("replaceWith")), + ) + clean(root) + + fun current() = calls(root, root.at(data.string("anchor"), data.int("offset"))).single() + val original = current() + val edge = callEdges(original, "incomingCalls").single() + check(edge.getAsJsonObject("from").string("uri") == uri(data.string("memberFile"))) + val child = open(data.string("memberFile")) + replace(child, "\n\n$memberText") + settle(root) + check(callEdges(original, "incomingCalls").isEmpty()) + val moved = callEdges(current(), "incomingCalls").single() + + fun line(item: JsonObject) = + item["fromRanges"] + .rows() + .first() + .getAsJsonObject("start") + .int("line") + check(line(moved) == line(edge) + data.int("lineShift")) + } +} + +private fun ParityWorkspace.calls( + document: ParityWorkspace.Document, + at: Int, +) = query("textDocument/prepareCallHierarchy", document, at).rows() + +private fun ParityWorkspace.callEdges( + item: JsonObject, + direction: String, +) = protocol.query("callHierarchy/$direction", mapOf("item" to item)).rows() diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt new file mode 100644 index 00000000000..26ff3d712d3 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt @@ -0,0 +1,205 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonElement +import com.google.gson.JsonParser +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.invokeGlobalBackendAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.elements.button +import com.intellij.driver.sdk.ui.components.elements.dialog +import com.intellij.driver.sdk.ui.ui +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +internal fun ParityScenarios.typeMoveCases() { + listOf( + "X161", + "X162", + "X163", + "X169", + "X170", + "X171", + "X172", + "X173", + "X174", + "X175", + "X176", + "X216", + "X217", + "X218", + "X219", + ).forEach { id -> + case(id) { data -> + val files = data["files"].rows() + files.forEach { write(it.string("file"), it.string("source")) } + data["directories"]?.asJsonArray?.forEach { Files.createDirectories(directory.resolve(it.asString)) } + configure(data["modules"]) + val document = open(data.string("root")) + clean(document) + if (data.has("consumer")) { + with(driver) { + withContext(OnDispatcher.EDT) { + check( + service(singleProject()).getAllEditors().none { + it.getFile().getPath() == directory.resolve(data.string("consumer")).toString() + }, + ) + } + } + } + val moves = + if (data.has("moves")) { + data["moves"].rows().map { it.string("from") to it.string("to") } + } else { + listOf(data.string("from") to data.string("to")) + } + if (data["refused"]?.asBoolean == true) { + val protocol = ClientProtocol(driver) + val beforeGraph = driver.utility(CompilerSettingsPage::class).content(driver.singleProject()) + val proposal = + protocol.query( + "xtc/renameFiles", + mapOf( + "files" to + moves.map { (from, to) -> + mapOf( + "oldUri" to directory.resolve(from).toUri().toString(), + "newUri" to directory.resolve(to).toUri().toString(), + ) + }, + ), + ) + check(proposal.isJsonNull) { "A colliding member must reject the entire proposal" } + files.forEach { check(Files.readString(directory.resolve(it.string("file"))) == it.string("source")) } + moves.forEach { (_, to) -> check(!Files.exists(directory.resolve(to))) } + check(driver.utility(CompilerSettingsPage::class).content(driver.singleProject()) == beforeGraph) + clean(document) + return@case + } + val target = directory.resolve(data.string("destination")) + refresh(target) + with(driver) { + withContext(OnDispatcher.EDT) { + utility(FileTreeOperations::class).move( + singleProject(), + moves.map { directory.resolve(it.first).toString() }, + target.toString(), + ) + } + val dialog = ui.dialog(title = "Move Ecstasy Sources") + awaitUi("source Move dialog", 45.seconds) { dialog.present() } + if (data.has("newName")) { + val field = cast(dialog.x { byAccessibleName("New name") }.component, MoveNameField::class) + withContext(OnDispatcher.EDT) { field.setText(data.string("newName")) } + } + withContext(OnDispatcher.EDT) { cast(dialog.button("Refactor").component, NativeButton::class).doClick() } + + fun verify(moved: Boolean) { + awaitUi("source Move ${if (moved) "applied" else "undone"}", 45.seconds) { + files.all { file -> + val path = directory.resolve(file.string(if (moved) "destination" else "file")) + val expected = file.string(if (moved) "expected" else "source") + Files.exists(path) && + ( + if (path.toString().endsWith(".x")) { + // IntelliJ normalizes document buffers to LF; closed files + // below still assert exact disk bytes, including CRLF. + Document(path.fileName.toString(), requireNotNull(refresh(path))).text == + expected.replace("\r\n", "\n").replace('\r', '\n') + } else { + Files.readString(path) == expected + } + ) && + ( + file.string("file") == file.string("destination") || + !Files.exists(directory.resolve(file.string(if (moved) "file" else "destination"))) + ) + } + } + val openPaths = + withContext(OnDispatcher.EDT) { + service(singleProject()).getAllEditors().map { it.getFile().getPath() }.toSet() + } + files.forEach { file -> + val path = directory.resolve(file.string(if (moved) "destination" else "file")) + if (path.toString() !in openPaths) { + check(Files.readString(path) == file.string(if (moved) "expected" else "source")) { + "Closed source/resource $path differs on disk after Move/Undo/Redo" + } + } + } + if (data.has("afterModules")) { + val content = requireNotNull(utility(CompilerSettingsPage::class).content(singleProject())) + val modules = + JsonParser + .parseString(content) + .asJsonObject["xtc"] + .asJsonObject["compiler"] + .asJsonObject["sourceModules"] + + fun graph( + value: JsonElement, + relative: Boolean, + ) = value.rows().associate { module -> + fun path(value: String) = + if (relative) directory.resolve(value).normalize() else Path.of(URI(value)).normalize() + module.string("name") to + Triple( + path(module.string("uri")), + module.strings("dependencies").toSet(), + module["resourceRoots"]?.takeUnless { it.isJsonNull }?.asJsonArray?.map { path(it.asString) }, + ) + } + val expected = graph(data[if (moved) "afterModules" else "modules"], true) + check(graph(modules, false) == expected) { + "Persisted graph differs after Move/Undo/Redo" + } + clean(open(data.string(if (moved) "afterRoot" else "root"))) + clean(open(data.string("consumer"))) + } else { + clean(document) + } + } + verify(true) + listOf("\$Undo" to false, "\$Redo" to true).forEach { (action, moved) -> + awaitUi("project $action is available") { + withContext(OnDispatcher.EDT) { + utility(FileTreeOperations::class).globalHistoryAvailable(singleProject(), moved) + } + } + invokeGlobalBackendAction(action, project = singleProject(), now = false) + awaitUi("$action restores the source path", 45.seconds) { + val confirm = ui.dialog(title = if (moved) "Redo" else "Undo") + if (confirm.present()) { + withContext(OnDispatcher.EDT) { + cast(confirm.button(if (moved) "Redo" else "Undo").component, NativeButton::class).doClick() + } + } + Files.exists(directory.resolve(data.string("to"))) == moved + } + verify(moved) + } + if (data.has("afterModules")) { + // Directory Move/Undo/Redo must retire the old synchronizer as well as move + // files/settings. A later unsaved edit must reach the current URI exactly once. + val moved = open(data.string("afterRoot")) + val source = moved.text + replace(moved, source.replaceFirst("{", "{\n Int relocationProbe = missingRelocationValue;")) + errors(moved) + replace(moved, source) + clean(moved) + } + } + } + } +} + +@Remote("javax.swing.JTextField") +private interface MoveNameField { + fun setText(text: String) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt new file mode 100644 index 00000000000..57431ad7870 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt @@ -0,0 +1,514 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.VirtualFile +import com.intellij.driver.sdk.getToolWindow +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.common.codeEditorForFile +import com.intellij.driver.sdk.ui.components.common.ideFrame +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Per-scenario native files/buffers and the same module boundaries as the VS Code playbook. */ +class ParityWorkspace( + val driver: Driver, + val id: String, + private val fixtures: Map, + private val shared: SharedScenarios, +) : AutoCloseable { + val protocol = ClientProtocol(driver) + val trace = ClientTrace(driver) + + internal fun replacementGate(id: String): Boolean = id in System.getProperty("xtc.playbook.replacementGates", "").split(',') + + val common = + JsonParser + .parseString( + Files.readString(Path.of(System.getProperty("xtc.playbook.scenarios"))), + ).asJsonObject["common"] + .asJsonObject + private val projectRoot = with(driver) { Path.of(singleProject().getBasePath()) } + val directory: Path = projectRoot.resolve("parity/$id") + + inner class Document( + val file: String, + private val source: VirtualFile, + ) { + private val buffer = + with(driver) { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + requireNotNull(service().getDocument(source)) + } + } + val text: String + get() = + with(driver) { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + buffer.getText() + } + } + + val uri: String + get() = Path.of(source.getPath()).toUri().toString() + + val editor: JEditorUiComponent + get() = + with(driver) { + withContext(OnDispatcher.EDT) { + val manager = service(singleProject()) + if ( + manager.getSelectedTextEditor()?.getVirtualFile()?.getPath() != + source.getPath() + ) { + manager.openFile(source, false, false) + } + } + ideFrame().codeEditorForFile(Path.of(file).fileName.toString()) + } + + fun at( + anchor: String, + offset: Int = 0, + last: Boolean = false, + ): Int = + (if (last) text.lastIndexOf(anchor) else text.indexOf(anchor)).also { + check(it >= 0) { "Missing '$anchor' in $file" } + } + offset + + fun params(at: Int? = null): Map = + mapOf("textDocument" to mapOf("uri" to uri)) + + if (at == null) emptyMap() else mapOf("position" to position(text, at)) + } + + init { + Files.createDirectories(directory) + // Existing cases use the common workspace. Close its tabs before same-named per-case files + // are opened; otherwise editor lookup by tab name can bind to a different source buffer. + with(driver) { + dismissPopups() + withContext(OnDispatcher.EDT) { + val manager = service(singleProject()) + manager + .getAllEditors() + .map { it.getFile() } + .distinctBy { it.getPath() } + .forEach(manager::closeFile) + // The common-workspace cases also leave closed dirty buffers. Retire them + // before this independent case, just as close() retires our own buffers; + // otherwise a later native Save All formats every preceding fixture first. + val documents = service() + documents + .getUnsavedDocuments() + .filter { + documents.getFile(it)?.getPath()?.startsWith(projectRoot.toString() + "/") == true + }.forEach(documents::reloadFromDisk) + } + } + configure(emptyList()) + with(driver) { + withContext(OnDispatcher.EDT) { + // Instantiate LSP4IJ's trace console before traffic begins. A hidden, never + // created tool window otherwise retains no console document to inspect. + getToolWindow("Language Servers").show() + getToolWindow("Language Servers").hide() + } + } + } + + fun fixture(file: String): String = fixtures.getValue(file) + + fun uri(file: String): String = directory.resolve(file).toUri().toString() + + fun write( + file: String, + text: String = fixture(file), + ) { + val path = directory.resolve(file) + Files.createDirectories(path.parent) + Files.writeString(path, text) + refresh(path) + } + + fun delete(file: String) { + val path = directory.resolve(file) + Files.delete(path) + refresh(path.parent) + } + + fun refresh(path: Path): VirtualFile? = + with(driver) { + utility(ParityFiles::class).getInstance().refreshAndFindFileByPath(path.toString()) + } + + fun open( + file: String, + text: String? = null, + ): Document = + with(driver) { + if (text != null || !Files.exists(directory.resolve(file))) { + write(file, text ?: fixture(file)) + } + val target = requireNotNull(refresh(directory.resolve(file))) + withContext(OnDispatcher.EDT) { + service(singleProject()).openFile(target, false, false) + } + val document = Document(file, target) + awaitUi("LSP4IJ opens $id/$file", 45.seconds) { + clientDocument(document)?.getSynchronizer()?.getDidOpenFuture()?.let { + it.isDone() && !it.isCompletedExceptionally() + } == true + } + settle(document) + document + } + + private fun clientDocument(document: Document): ClientDocument? = + protocol.server().getOpenedDocuments().singleOrNull { + it.getFile().getPath() == Path.of(URI(document.uri)).toString() + } + + fun replace( + document: Document, + text: String, + settle: Boolean = true, + ) { + document.editor.text = text + if (settle) settle(document) + } + + fun settle(document: Document) { + flush(document) + // File notifications can retire the first analysis even after didOpen has been sent. + // Retry only this read-only readiness probe; feature assertions and edits stay single-shot. + awaitUi("current analysis for $id/${document.file}", 45.seconds) { + try { + query("textDocument/documentSymbol", document) + true + } catch (failure: ClientRequestFailure) { + if (failure.code != ResponseErrorCode.ContentModified.value) throw failure + false + } + } + } + + /** Wait for transport delivery without waiting for the compiler query that tests may cancel. */ + fun flush(document: Document) { + val pending = + requireNotNull(clientDocument(document)?.getSynchronizer()) { + "Document synchronizer is not ready for $id/${document.file}" + }.flushPendingChanges() + with(driver) { + awaitUi("client sends current $id/${document.file}", 45.seconds) { pending.isDone() } + } + check(!pending.isCompletedExceptionally()) { "Document synchronization failed" } + } + + fun query( + method: String, + document: Document, + at: Int? = null, + extra: Map = emptyMap(), + ): JsonElement = protocol.query(method, document.params(at) + extra) + + fun version(document: Document): Int { + settle(document) + return trace.version(document.uri, document.text) + } + + fun published( + file: String, + matches: (List) -> Boolean, + ): List = trace.diagnostics(uri(file), matches) + + fun targets( + document: Document, + kind: String, + at: Int, + native: Boolean = true, + ): List = + query("textDocument/$kind", document, at).rows().also { + if (native) with(driver) { nativeLocations(document, at, kind, it) } + } + + fun names(locations: List): List = + targetNames(locations) { uri -> + protocol + .server() + .getOpenedDocuments() + .firstOrNull { it.getFile().getPath() == Path.of(URI(uri)).toString() } + ?.getSynchronizer() + ?.getDocument() + ?.getText() + } + + fun hierarchy( + document: Document, + at: Int, + ): JsonObject = query("textDocument/prepareTypeHierarchy", document, at).rows().single() + + fun edges( + item: JsonObject, + direction: String, + ): List = protocol.query("typeHierarchy/$direction", mapOf("item" to item)).rows() + + fun clean(document: Document) = diagnostics(document) { it.isEmpty() } + + fun errors(document: Document) = diagnostics(document) { it.isNotEmpty() } + + fun diagnostics( + document: Document, + matches: (List) -> Boolean, + ): List = + with(driver) { + awaitUi( + message = "current diagnostics for $id/${document.file}", + timeout = 45.seconds, + getter = { receivedDiagnostics(document.editor) }, + checker = matches, + ) + } + + fun project(): Document { + val setup = common["project"].asJsonObject + write(setup.string("member")) + return open(setup.string("root")).also(::clean) + } + + fun configure(modules: List?) = + with(driver) { + val content = + Gson() + .toJson(mapOf("xtc" to mapOf("compiler" to mapOf("sourceModules" to modules)))) + withContext(OnDispatcher.EDT) { + val settings = new(LspServerSettings::class) + settings.setConfigurationContent(content) + service().updateSettings("xtcLanguageServer", settings) + val traceSettings = + new(LspServerSettings::class) + .setServerTrace(utility(ClientTraceLevel::class).valueOf("verbose")) + service(singleProject()) + .updateSettings("xtcLanguageServer", traceSettings) + } + } + + fun dependencies(): Document { + shared.common.sourceModules.forEach { write(it.uri) } + configure(shared.common.sourceModules.map { it.copy(uri = uri(it.uri)) }) + return open(shared.dependencyNavigation.file).also(::clean) + } + + fun configure(modules: JsonElement) = + configure( + modules.asJsonArray.map { + it.asJsonObject.let { module -> + SharedScenarios.SourceModule( + module.string("name"), + uri(module.string("uri")), + module.strings("dependencies"), + module["resourceRoots"]?.asJsonArray?.map { uri(it.asString) }, + ) + } + }, + ) + + fun linked(consumer: Document) { + settle(consumer) + clean(consumer) + val location = shared.dependencyNavigation.location + val result = + targets(consumer, "definition", SharedScenarios.offset(consumer.text, location.cursor)) + check(result.single().string("uri") == uri(location.targetFile)) { result.toString() } + } + + fun marked( + file: String, + text: String, + ): Pair { + val at = text.indexOf('§') + check(at >= 0 && text.lastIndexOf('§') == at) + return open(file).also { replace(it, text.replace("§", "")) } to at + } + + fun editing(body: String): Pair { + val setup = shared.common.editing + return marked( + setup.file, + fixture(setup.file).replace(setup.run, setup.run.replace("{}", "{ $body }")), + ) + } + + fun virtual( + file: String, + text: String, + version: Int, + action: () -> Unit, + ) { + protocol.notify( + "textDocument/didOpen", + mapOf( + "textDocument" to + mapOf( + "uri" to uri(file), + "languageId" to "xtc", + "version" to version, + "text" to text, + ), + ), + ) + try { + action() + } finally { + protocol.notify( + "textDocument/didClose", + mapOf("textDocument" to mapOf("uri" to uri(file))), + ) + } + } + + fun save(document: Document) = + with(driver) { + withContext(OnDispatcher.EDT, semantics = LockSemantics.WRITE_ACTION) { + service() + .saveDocument(cast(document.editor.document, ParityDocument::class)) + } + } + + fun discard(document: Document) = + with(driver) { + val connection = protocol.server() + // Revert via IntelliJ's file/document manager, preserving the disk line separator. + withContext(OnDispatcher.EDT) { + service() + .reloadFromDisk(cast(document.editor.document, ParityDocument::class)) + service(singleProject()) + .closeFile(document.editor.editor.getVirtualFile()) + } + awaitUi("client closes ${document.file}", 30.seconds) { + connection.getOpenedDocuments().none { + it.getFile().getPath() == Path.of(URI(document.uri)).toString() + } + } + } + + override fun close() { + try { + restoreWorkspace() + } catch (failure: Throwable) { + throw PlaybookCleanupFailure(failure) + } + } + + private fun restoreWorkspace() { + with(driver) { + dismissPopups() + val manager = service(singleProject()) + val files = + manager + .getAllEditors() + .map { + it.getFile() + }.filter { it.getPath().startsWith(directory.toString() + "/") } + .distinctBy { it.getPath() } + withContext(OnDispatcher.EDT) { files.forEach(manager::closeFile) } + withContext(OnDispatcher.EDT) { + // Closing a tab does not discard its dirty document. A later Save All must + // not format or save fixtures belonging to an already completed scenario. + val documents = service() + documents + .getUnsavedDocuments() + .filter { + documents.getFile(it)?.getPath()?.startsWith(directory.toString() + "/") == + true + }.forEach(documents::reloadFromDisk) + } + } + configure( + shared.common.sourceModules.map { + it.copy(uri = projectRoot.resolve(it.uri).toUri().toString()) + }, + ) + } + + companion object { + fun position( + text: String, + offset: Int, + ): Map { + check(offset in 0..text.length) + val prefix = text.take(offset) + return mapOf( + "line" to prefix.count { it == '\n' }, + "character" to offset - prefix.lastIndexOf('\n') - 1, + ) + } + + fun offset( + text: String, + position: JsonObject, + ): Int = + text.splitToSequence('\n').take(position.int("line")).sumOf { it.length + 1 } + + position.int("character") + } +} + +/** Compare local URI spelling without changing objects that must round-trip to the server. */ +internal fun JsonObject.string(name: String): String = + get(name).asString.let { value -> + if (name in setOf("uri", "targetUri", "oldUri", "newUri") && value.startsWith("file:")) { + Path.of(URI(value)).toUri().toString() + } else { + value + } + } + +internal fun JsonObject.int(name: String): Int = get(name).asInt + +internal fun JsonObject.strings(name: String): List = getAsJsonArray(name).map { it.asString } + +internal fun JsonElement.rows(): List = if (isJsonNull) emptyList() else asJsonArray.map { it.asJsonObject } + +internal fun JsonObject.pattern(name: String): Regex = + getAsJsonObject(name).let { + Regex( + it.string("source"), + if (it.string("flags").contains('i')) setOf(RegexOption.IGNORE_CASE) else emptySet(), + ) + } + +@Remote("com.intellij.openapi.vfs.LocalFileSystem") +interface ParityFiles { + fun getInstance(): ParityFiles + + fun refreshAndFindFileByPath(path: String): VirtualFile? +} + +@Remote("com.intellij.openapi.fileEditor.FileDocumentManager") +interface ParityDocuments { + fun getDocument(file: VirtualFile): ParityDocument? + + fun getUnsavedDocuments(): Array + + fun getFile(document: ParityDocument): VirtualFile? + + fun reloadFromDisk(document: ParityDocument) + + fun saveDocument(document: ParityDocument) +} + +@Remote("com.intellij.openapi.editor.Document") +interface ParityDocument { + fun getText(): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt new file mode 100644 index 00000000000..283e476e8ac --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt @@ -0,0 +1,15 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.WaitForException +import java.util.concurrent.CancellationException +import java.util.concurrent.TimeoutException + +internal class PlaybookCleanupFailure( + cause: Throwable, +) : IllegalStateException("Could not restore the playbook workspace", cause) + +/** An unfinished operation or failed cleanup cannot share an IDE with subsequent cases. */ +internal fun Throwable.mustStopPlaybook(): Boolean = + this is WaitForException || this is TimeoutException || this is CancellationException || + this is InterruptedException || this is PlaybookCleanupFailure || + cause?.mustStopPlaybook() == true || suppressed.any(Throwable::mustStopPlaybook) diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt new file mode 100644 index 00000000000..cd51c127b03 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt @@ -0,0 +1,34 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.WaitForException +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import java.util.concurrent.TimeoutException +import kotlin.time.Duration.Companion.seconds + +class PlaybookFailureTest { + @Test fun `timeouts stop the run including wrapped driver failures`() { + assertTrue(WaitForException(1.seconds, "Editor did not change").mustStopPlaybook()) + assertTrue(IllegalStateException("Remote call", TimeoutException()).mustStopPlaybook()) + } + + @Test fun `a suppressed cleanup failure prevents continuation`() { + val failure = AssertionError("Wrong source") + failure.addSuppressed(PlaybookCleanupFailure(IllegalStateException("Cannot reset settings"))) + assertTrue(failure.mustStopPlaybook()) + } + + @Test fun `ordinary completed assertions remain collectable`() { + assertFalse(AssertionError("Wrong source").mustStopPlaybook()) + } + + @Test fun `an expected refusal cannot turn a timeout into a collectable assertion`() { + val failure = + assertThrows(AssertionError::class.java) { + assertThrows(ClientRequestFailure::class.java) { throw TimeoutException("No protocol reply") } + } + assertTrue(failure.mustStopPlaybook()) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt new file mode 100644 index 00000000000..e5d473b6f8c --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt @@ -0,0 +1,22 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Remote +import com.intellij.driver.sdk.Project + +@Remote("org.xtclang.idea.playbook.probe.PlaybookProgress", plugin = "org.xtclang.playbook.probe") +internal interface PlaybookProgress { + fun install(project: Project) + + fun update( + project: Project, + text: String, + detail: String, + ) + + fun focus( + project: Project, + restoring: Boolean, + ) + + fun text(project: Project): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt new file mode 100644 index 00000000000..614cf3c327e --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt @@ -0,0 +1,208 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Two real frames share the IDE while their compiler processes and pending replies stay isolated. */ +internal fun Driver.projectLifecycle(shared: SharedScenarios) { + val primary = singleProject() + val root = Path.of(primary.getBasePath()) + val directory = Files.createDirectories(root.parent.resolve("second-project")) + val data = shared.scenarios.getValue("X145") + val module = data.text("module") + val name = data.text("file") + val firstFile = root.resolve(name) + val secondFile = directory.resolve(name) + val initial = data.text("source") + listOf(firstFile, secondFile).forEach { Files.writeString(it, initial) } + val first = ClientProtocol(this) { primary } + val probe = utility(ProjectLifecycle::class) + + fun configure( + project: Project, + file: Path, + ) = withContext(OnDispatcher.EDT) { + val settings = new(LspServerSettings::class) + settings.setConfigurationContent( + Gson().toJson( + mapOf( + "xtc" to + mapOf( + "compiler" to + mapOf( + "sourceModules" to listOf(mapOf("name" to module, "uri" to file.toUri().toString())), + ), + ), + ), + ), + ) + service(project).updateSettings("xtcLanguageServer", settings) + } + + fun edit( + project: Project, + file: Path, + text: String, + ) = withContext(OnDispatcher.EDT) { + probe.edit(project, file.toString(), text) + } + + fun params(file: Path) = + mapOf( + "textDocument" to mapOf("uri" to file.toUri().toString()), + "position" to + mapOf( + "line" to 1, + "character" to + probe + .text(file.toString()) + .lineSequence() + .elementAt(1) + .indexOf("value"), + ), + ) + + fun hover( + client: ClientProtocol, + file: Path, + type: String, + ) { + awaitUi("$type hover in ${file.parent.fileName}", 45.seconds) { + try { + client.query("textDocument/hover", params(file)).toString().contains("$type value") + } catch (failure: ClientRequestFailure) { + if (failure.code != ResponseErrorCode.ContentModified.value) throw failure + false + } + } + } + + fun openSecond(): Project { + val opened = probe.open(directory.toString()) + awaitUi("second project opens", 60.seconds) { opened.isDone() } + val project = requireNotNull(opened.get()) + awaitUi("both native project frames are visible", 30.seconds) { + withContext(OnDispatcher.EDT) { probe.visible(primary) && probe.visible(project) } + } + return project + } + + fun close( + project: Project, + pid: Long, + ) { + val closed = probe.close(project) + awaitUi("project closes and owns server retirement", 30.seconds) { + closed.isDone() && !utility(ProgressUi::class).alive(pid) + } + check(closed.get()) + } + + fun workload( + type: String, + value: String, + ) = "module $module {\n static $type value = $value;\n" + + (0 until data.values["methods"].asInt).joinToString("\n") { " $type read$it() { return value; }" } + "\n}\n" + + configure(primary, firstFile) + val firstText = "module $module {\n static Int value = 7;\n}\n" + edit(primary, firstFile, firstText) + hover(first, firstFile, "Int") + val firstPid = requireNotNull(first.server().getCurrentProcessId()) + val secondary = openSecond() + configure(secondary, secondFile) + edit(secondary, secondFile, "module $module {\n static String value = \"second\";\n}\n") + val second = ClientProtocol(this) { secondary } + hover(second, secondFile, "String") + val secondPid = requireNotNull(second.server().getCurrentProcessId()) + check(firstPid != secondPid) + val firstPendingText = workload("Int", "7") + "// unsaved primary\n" + val secondPendingText = workload("String", "\"second\"") + "// pending close\n" + edit(primary, firstFile, firstPendingText) + val firstPending = first.request("textDocument/references", params(firstFile) + mapOf("context" to mapOf("includeDeclaration" to true))) + edit(secondary, secondFile, secondPendingText) + val secondPending = + second.request( + "textDocument/references", + params(secondFile) + mapOf("context" to mapOf("includeDeclaration" to true)), + ) + awaitUi("compiler is working when its project closes", 15.seconds) { + check(!secondPending.isDone()) { "Close must overlap a pending native request" } + second + .query("xtc/languageServiceStatus", emptyMap()) + .asJsonObject["compilerQueue"] + .asJsonObject["runningSize"] + .asInt > 0 + } + close(secondary, secondPid) + awaitUi("closed project's pending reply retires", 15.seconds) { secondPending.isDone() } + check(first.await("textDocument/references", firstPending).asJsonArray.size() == data.values["methods"].asInt + 1) + check(first.server().getCurrentProcessId() == firstPid) + check(probe.text(firstFile.toString()) == firstPendingText) + hover(first, firstFile, "Int") + check(Files.readString(secondFile) == secondPendingText) { "Native project close must preserve edited source" } + val reopened = openSecond() + withContext(OnDispatcher.EDT) { probe.show(reopened, secondFile.toString()) } + check(probe.text(secondFile.toString()) == secondPendingText) { "Reopening must restore source without replaying edits" } + val reconnected = ClientProtocol(this) { reopened } + hover(reconnected, secondFile, "String") + val reopenedPid = requireNotNull(reconnected.server().getCurrentProcessId()) + check(reopenedPid != secondPid && reopenedPid != firstPid) + close(reopened, reopenedPid) + hover(first, firstFile, "Int") + Files.writeString( + root.parent.resolve("project-lifecycle.json"), + Gson().toJson( + mapOf( + "primaryPid" to firstPid, + "closedPid" to secondPid, + "reopenedPid" to reopenedPid, + "closedPendingRetired" to secondPending.isDone(), + "primaryTextPreserved" to true, + "reopenedTextPreserved" to true, + "status" to "passed", + ), + ) + "\n", + ) +} + +@Remote("org.xtclang.idea.playbook.probe.ProjectLifecycle", plugin = "org.xtclang.playbook.probe") +internal interface ProjectLifecycle { + fun open(path: String): OpenProjectFuture + + fun close(project: Project): CloseProjectFuture + + fun show( + project: Project, + path: String, + ) + + fun edit( + project: Project, + path: String, + text: String, + ) + + fun text(path: String): String + + fun visible(project: Project): Boolean +} + +@Remote("java.util.concurrent.CompletableFuture") +internal interface OpenProjectFuture : ClientFuture { + fun get(): Project? +} + +@Remote("java.util.concurrent.CompletableFuture") +internal interface CloseProjectFuture : ClientFuture { + fun get(): Boolean +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt new file mode 100644 index 00000000000..4591b701be5 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt @@ -0,0 +1,296 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.WaitForException +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.elements.JTableUiComponent +import com.intellij.driver.sdk.ui.components.elements.accessibleList +import com.intellij.driver.sdk.ui.components.elements.accessibleTable +import com.intellij.driver.sdk.ui.components.elements.button +import com.intellij.driver.sdk.ui.components.elements.dialog +import com.intellij.driver.sdk.ui.components.elements.popup +import com.intellij.driver.sdk.ui.components.elements.tree +import com.intellij.driver.sdk.ui.remote.Component +import com.intellij.driver.sdk.ui.ui +import kotlin.time.Duration.Companion.seconds + +/** Exercise the installed Rename handler and its dialog, including ordered resource operations. */ +fun Driver.rename( + editor: JEditorUiComponent, + at: Int, + replacement: String, +) { + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + + fun available() = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(NativeEditorUi::class).renameAvailable(editor.editor) + } + awaitUi("installed Rename handler is available", 45.seconds) { available() } + invokeAction("RenameElement", now = false, component = editor.component) + val dialog = ui.dialog(title = "Rename") + awaitUi( + "native rename dialog", + 45.seconds, + errorMessage = { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + utility(NativeEditorUi::class).renameState(editor.editor) + } + }, + ) { + if (dialog.present()) return@awaitUi true + restorePopupFocus(editor.editor) + false + } + // Exercise the focus observation with the actual modal dialog already open as well. + restorePopupFocus(editor.editor) + fillRenameDialog(replacement) +} + +/** Submit the installed Rename dialog without synthesizing text edits in the driver. */ +fun Driver.fillRenameDialog(replacement: String) { + val dialog = ui.dialog(title = "Rename") + awaitUi("native rename dialog opens", 45.seconds) { dialog.present() } + val input = + dialog.x(JEditorUiComponent::class.java) { + byType("com.intellij.openapi.editor.impl.EditorComponentImpl") + } + input.text = replacement + val button = cast(dialog.button("Refactor").component, NativeButton::class) + awaitUi("rename accepts the new name", 10.seconds) { button.isEnabled() } + withContext(OnDispatcher.EDT) { button.doClick() } + awaitUi("rename dialog closes", 45.seconds) { dialog.notPresent() } +} + +/** Select the reference-preserving XTC scope when another plugin also offers plain file rename. */ +fun Driver.chooseXtcFileRename() { + val chooser = ui.dialog(title = "Select Refactoring") + val rename = ui.dialog(title = "Rename") + awaitUi("native file rename scope or name dialog", 45.seconds) { + chooser.present() || rename.present() + } + if (chooser.present()) { + val option = + cast( + chooser + .x { + byType("javax.swing.JRadioButton") and + byAccessibleName("Rename ecstasy file and references") + }.component, + NativeButton::class, + ) + val accept = cast(chooser.button("OK").component, NativeButton::class) + withContext(OnDispatcher.EDT) { + option.doClick() + accept.doClick() + } + } +} + +/** Select the intention list, excluding its separate preview popup, without moving the pointer. */ +fun Driver.choosePopup( + editor: JEditorUiComponent, + expected: List, + selected: String, + reopen: () -> Unit, +) { + val inspection = PopupInspection(this, editor, reopen) + // Capture the source/caret before dispatch: an intention can move the caret while it + // discovers actions. Recovery must retain the requested location, not that later position. + reopen() + val popup = ui.popup("//div[@class='HeavyWeightWindow'][.//div[@class='MyList']]") + awaitUi( + "native popup contains $expected", + 45.seconds, + errorMessage = { + "Expected $expected; rendered rows: ${if (popup.present()) popup.accessibleList { byClass("MyList") }.items else "no list"}" + }, + ) { + inspection.recover() + if (!popup.present()) { + try { + awaitUi("intention menu opens", 2.seconds) { popup.present() } + } catch (_: WaitForException) { + // Reopen only an unapplied inspection, never a completed generation or rename. + inspection.reopenUnapplied() + } + return@awaitUi false + } + val rows = popup.accessibleList { byClass("MyList") }.items + expected.all { name -> rows.any { it.contains(name) } } + } + val list = popup.accessibleList { byClass("MyList") } + val index = list.items.indexOfFirst { it.contains(selected) } + check(index >= 0) + withContext(OnDispatcher.EDT) { + cast(list.component, NativeListSelection::class).setSelectedIndex(index) + } + popup.keyboard { enter() } + awaitUi("chosen popup closes", 15.seconds) { popup.notPresent() } +} + +/** LSP4IJ renders multiple navigation targets through IntelliJ's Show Usages table. */ +fun Driver.chooseTargets( + editor: JEditorUiComponent, + expected: List, + selected: String, + reopen: () -> Unit, +) { + val inspection = PopupInspection(this, editor, reopen) + val popup = ui.popup() + val table = popup.accessibleTable() + awaitUi( + message = "native navigation chooser contains exactly $expected", + errorMessage = { + "Expected $expected; rendered rows: ${if (table.present()) table.content() else "no table"}" + }, + timeout = 45.seconds, + ) { + inspection.recover() + if (!popup.present()) return@awaitUi false + val rows = table.content().values.map { it.values.joinToString(" ") } + rows.size == expected.size && expected.all { name -> rows.any { it.contains(name) } } + } + val row = + table + .content() + .entries + .single { (_, cells) -> cells.values.any { it.contains(selected) } } + .key + chooseNavigationRow(table, row) + awaitUi("selected declaration closes the chooser", 15.seconds) { popup.notPresent() } +} + +/** Invoke the chooser's registered Enter action once, independent of desktop keyboard focus. */ +internal fun Driver.chooseNavigationRow( + table: JTableUiComponent, + row: Int, +) { + withContext(OnDispatcher.EDT) { + utility(NativeNavigationChooser::class).choose(table.component, row) + } +} + +fun Driver.quickFix( + editor: JEditorUiComponent, + at: Int, + title: String, + selectionEnd: Int = at, +) { + focusEditor(editor) + withContext(OnDispatcher.EDT) { + editor.editor.getSelectionModel().removeSelection() + editor.editor.getCaretModel().moveToOffset(at) + if (selectionEnd != at) editor.editor.getSelectionModel().setSelection(at, selectionEnd) + } + // TODO LSP4IJ: UP07 — overlapping full pulls can cancel an old lazy fix during discovery. + // Dispatch through the UI queue so the platform handles ProcessCanceledException normally; + // the bounded popup wait may reopen an unapplied inspection, never replay a chosen edit. + choosePopup(editor, listOf(title), title) { + invokeAction("ShowIntentionActions", now = false, component = editor.component) + } +} + +/** Inspect the rendered native hierarchy, then the result of the native references action. */ +fun Driver.partialGraphHierarchy( + editor: JEditorUiComponent, + at: Int, + child: String, + referenceAt: Int, +) { + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + invokeAction("TypeHierarchy", component = editor.component) + val browser = + ui.x { + byType("com.redhat.devtools.lsp4ij.features.typeHierarchy.LSPTypeHierarchyBrowser") + } + awaitUi("native type hierarchy includes $child", 45.seconds) { + if (!browser.present()) return@awaitUi false + // The native browser expands its root. Observe it without expandAll's nested ten-second + // loading deadline cutting short this asynchronous hierarchy request. + browser.tree().collectExpandedPaths().any { path -> path.path.last().contains(child) } + } + val path = editor.editor.getVirtualFile().getPath() + val views = service(singleProject()) + val previous = withContext(OnDispatcher.EDT) { views.getSelectedUsageView() } + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + invokeAction("FindUsages", component = editor.component) + awaitUi("incomplete graph withholds references while retaining local navigation", 45.seconds) { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + val view = views.getSelectedUsageView() + if (view == null || view == previous || view.isSearchInProgress()) { + return@withContext false + } + // LSP4IJ combines definitions, implementations, type definitions and references here. + // The declaration may remain, but the known extends-use must not masquerade as a + // complete workspace reference result beside the broken module. + val ranges = view.rangesIn(path) + ranges.any { at in it } && ranges.none { referenceAt in it } + } + } +} + +@Remote("javax.swing.JButton") +interface NativeButton { + fun isEnabled(): Boolean + + fun doClick() +} + +@Remote("javax.swing.JList") +interface NativeListSelection { + fun setSelectedIndex(index: Int) +} + +@Remote("org.xtclang.idea.playbook.probe.NavigationChooser", plugin = "org.xtclang.playbook.probe") +internal interface NativeNavigationChooser { + fun choose( + component: Component, + row: Int, + ) +} + +/** A library use must be rejected by prepareRename before any refactoring dialog or edit. */ +fun Driver.rejectRename( + editor: JEditorUiComponent, + at: Int, + reason: String, +) { + val original = editor.text + focusEditor(editor) + withContext(OnDispatcher.EDT) { + service().hideAllHints() + editor.editor.getCaretModel().moveToOffset(at) + } + invokeAction("RenameElement", now = false, component = editor.component) + // The guarded XTC handler preserves the server reason and adds its native action prefix. + awaitUi("library rename displays its rejection", 45.seconds) { + ui.x { byVisibleText("Rename failed: $reason") }.present() + } + check(ui.dialog(title = "Rename").notPresent()) + check(editor.text == original) + withContext(OnDispatcher.EDT) { service().hideAllHints() } +} + +/** + * Reformat must encounter the native read-only gate; cancel it without unlocking bundled sources. + */ +fun Driver.rejectFormatting(editor: JEditorUiComponent) { + focusEditor(editor) + invokeAction("ReformatCode", now = false, component = editor.component) + val dialog = ui.dialog(title = "Clear Read-Only Status") + awaitUi("reformat is blocked by read-only source", 45.seconds) { dialog.present() } + withContext(OnDispatcher.EDT) { + cast(dialog.button("Cancel").component, NativeButton::class).doClick() + } + awaitUi("read-only prompt closes", 15.seconds) { dialog.notPresent() } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt new file mode 100644 index 00000000000..d771b6a9304 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt @@ -0,0 +1,139 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.PsiManager +import com.intellij.driver.sdk.VirtualFile +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import kotlin.time.Duration.Companion.seconds + +/** Native Find Usages and Highlight Usages must preserve the identity selected in the editor. */ +fun Driver.referencesAndHighlights( + editor: JEditorUiComponent, + at: Int, + excluded: Int, +) { + val support = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + editor.editor.getCaretModel().moveToOffset(at) + val file = + requireNotNull( + service(singleProject()).findFile(editor.editor.getVirtualFile()), + ) + utility(LspFileSupport::class).getSupport(file) + } + val path = editor.editor.getVirtualFile().getPath() + + fun contains( + range: SourceRange, + offset: Int, + ): Boolean { + val start = + range.getStart().let { + editor.document.getLineStartOffset(it.getLine()) + it.getCharacter() + } + val end = + range.getEnd().let { + editor.document.getLineStartOffset(it.getLine()) + it.getCharacter() + } + return offset in start until end + } + val views = service(singleProject()) + val previous = withContext(OnDispatcher.EDT) { views.getSelectedUsageView() } + invokeAction("FindUsages", component = editor.component) + awaitUi("native references contain this use and exclude its shadowed namesake", 45.seconds) { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + val view = views.getSelectedUsageView() + if (view == null || view == previous || view.isSearchInProgress()) { + return@withContext false + } + val ranges = view.rangesIn(path) + ranges.any { at in it } && ranges.none { excluded in it } + } + } + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + invokeAction("HighlightUsagesInFile", component = editor.component) + awaitUi("native highlights contain this use and exclude its shadowed namesake", 45.seconds) { + val future = support.getHighlightSupport().getValidLSPFuture() + if (future == null || !future.isDone() || future.isCompletedExceptionally()) { + return@awaitUi false + } + val highlights = future.get() + highlights.any { contains(it.getRange(), at) } && + highlights.none { contains(it.getRange(), excluded) } + } + invokeAction("EditorEscape", component = editor.component) +} + +/** Read inside an IDE read action, after the native usage search has completed. */ +fun NativeUsageView.rangesIn(path: String): List = + getUsages() + .flatMap { it.getMergedInfos().toList() } + .filter { it.getVirtualFile()?.getPath() == path } + .mapNotNull { it.getNavigationRange() } + .map { it.getStartOffset() until it.getEndOffset() } + +@Remote("com.intellij.usages.UsageViewManager") +interface NativeUsageViews { + fun getSelectedUsageView(): NativeUsageView? +} + +@Remote("com.intellij.usages.UsageView") +interface NativeUsageView { + fun isSearchInProgress(): Boolean + + fun getUsages(): Collection +} + +@Remote("com.intellij.usages.UsageInfo2UsageAdapter") +interface NativeUsage { + fun getMergedInfos(): Array +} + +@Remote("com.intellij.usageView.UsageInfo") +interface NativeUsageInfo { + fun getVirtualFile(): VirtualFile? + + fun getNavigationRange(): NativeSourceRange? +} + +@Remote("com.intellij.openapi.util.Segment") +interface NativeSourceRange { + fun getStartOffset(): Int + + fun getEndOffset(): Int +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.highlight.LSPHighlightSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface HighlightSupport { + fun getValidLSPFuture(): HighlightFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface HighlightFuture { + fun isDone(): Boolean + + fun isCompletedExceptionally(): Boolean + + fun get(): List +} + +@Remote("org.eclipse.lsp4j.DocumentHighlight", plugin = "com.redhat.devtools.lsp4ij") +interface ClientHighlight { + fun getRange(): SourceRange + + fun getKind(): ClientHighlightKind? +} + +@Remote("org.eclipse.lsp4j.DocumentHighlightKind", plugin = "com.redhat.devtools.lsp4ij") +interface ClientHighlightKind { + fun name(): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RenameFamilyScenarios.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RenameFamilyScenarios.kt new file mode 100644 index 00000000000..6b30be461ff --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RenameFamilyScenarios.kt @@ -0,0 +1,143 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.elements.button +import com.intellij.driver.sdk.ui.components.elements.dialog +import com.intellij.driver.sdk.ui.ui +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Shared rename fixtures, real native Rename/Undo, and consumers kept closed before the edit. */ +fun Driver.renameFamily( + id: String, + data: SharedScenarios.Scenario, + open: (String) -> JEditorUiComponent, + clean: (JEditorUiComponent) -> Unit, +) { + val root = Path.of(singleProject().getBasePath()).resolve(id) + val files = data.rows("files") + + fun graphContains(name: String): Boolean = utility(CompilerSettingsPage::class).content(singleProject())?.contains(name) == true + + fun contents(file: String): String { + val path = root.resolve(file) + val source = + utility(ParityFiles::class).getInstance().refreshAndFindFileByPath(path.toString()) + return source?.let { + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + service().getDocument(it)?.getText() + } + } ?: Files.readString(path) + } + + val editor = open(data.text("file")) + clean(editor) + if (data.values.has("navigationAnchor")) { + val prefix = editor.text.substringBefore(data.text("navigationAnchor")) + val references = + ClientProtocol(this) + .query( + "textDocument/references", + mapOf( + "textDocument" to mapOf("uri" to Path.of(editor.editor.getVirtualFile().getPath()).toUri().toString()), + "position" to mapOf("line" to prefix.count { it == '\n' }, "character" to prefix.substringAfterLast('\n').length), + "context" to mapOf("includeDeclaration" to true), + ), + ).asJsonArray + .map { Path.of(URI.create(it.asJsonObject["uri"].asString)) } + .sorted() + check(references == data.strings("navigationFiles").map(root::resolve).sorted()) { references.toString() } + } + val request = files.single { "$id/${it["file"].asString}" == data.text("file") } + withContext(OnDispatcher.EDT) { + val opened = + service(singleProject()) + .getAllEditors() + .map { it.getFile().getPath() } + .toSet() + files + .filter { it !== request } + .forEach { check(root.resolve(it["file"].asString).toString() !in opened) } + } + rename(editor, editor.text.indexOf(data.text("anchor")), data.text("replacement")) + awaitUi("$id source and resource edits applied", 45.seconds) { + files.all { file -> + val destination = file["destination"].asString + Files.exists(root.resolve(destination)) && + contents(destination) == file["expected"].asString && + ( + destination == file["file"].asString || + !Files.exists(root.resolve(file["file"].asString)) + ) + } + } + val renamed = open("$id/${request["destination"].asString}") + clean(renamed) + if (data.values.has("sourceModules")) { + check(graphContains("Renamed.example.org") && !graphContains("Library.example.org")) + check(graphContains("assets") && graphContains("fallback")) + } + focusEditor(renamed) + invokeAction("\$Undo", now = false, component = renamed.component) + awaitUi("$id one Undo restores every source and path", 45.seconds) { + val confirmation = ui.dialog(title = "Undo") + if (confirmation.present()) { + withContext(OnDispatcher.EDT) { + cast(confirmation.button("Undo").component, NativeButton::class).doClick() + } + } + files.all { file -> + val original = file["file"].asString + Files.exists(root.resolve(original)) && + contents(original) == file["source"].asString && + ( + original == file["destination"].asString || + !Files.exists(root.resolve(file["destination"].asString)) + ) + } + } + clean(open(data.text("file"))) + if (data.values.has("sourceModules")) { + check(graphContains("Library.example.org") && !graphContains("Renamed.example.org")) + check(graphContains("assets") && graphContains("fallback")) + val restored = open(data.text("file")) + focusEditor(restored) + invokeAction("\$Redo", now = false, component = restored.component) + awaitUi("$id Redo restores graph, sources and paths", 45.seconds) { + graphContains("Renamed.example.org") && + !graphContains("Library.example.org") && + files.all { file -> + Files.exists(root.resolve(file["destination"].asString)) && + contents(file["destination"].asString) == file["expected"].asString + } + } + val redone = open("$id/${request["destination"].asString}") + clean(redone) + focusEditor(redone) + invokeAction("\$Undo", now = false, component = redone.component) + awaitUi("$id second Undo restores the original graph and sources", 45.seconds) { + val confirmation = ui.dialog(title = "Undo") + if (confirmation.present()) { + withContext(OnDispatcher.EDT) { + cast(confirmation.button("Undo").component, NativeButton::class).doClick() + } + } + graphContains("Library.example.org") && + !graphContains("Renamed.example.org") && + files.all { file -> + Files.exists(root.resolve(file["file"].asString)) && + contents(file["file"].asString) == file["source"].asString + } + } + clean(open(data.text("file"))) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt new file mode 100644 index 00000000000..8c95f2d1132 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt @@ -0,0 +1,89 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.singleProject +import java.nio.file.Files +import java.nio.file.Path + +/** Two complete IDE processes use the same disposable project and profile. */ +internal fun Driver.settingsPersistence( + shared: SharedScenarios, + phase: Int, +) { + val project = singleProject() + val root = Path.of(project.getBasePath()) + val data = shared.common.runtimeSettings + val file = root.resolve(data["file"].asString) + val runtime = utility(ServerRuntimePage::class) + val service = utility(LanguageServicePage::class) + val inputs = utility(SettingsPersistencePage::class) + val lifecycle = utility(ProjectLifecycle::class) + val expected = root.parent.resolve("persisted-settings.json") + if (phase == 0) { + Files.writeString(file, data["source"].asString) + withContext(OnDispatcher.EDT) { + runtime.edit( + JsonObject() + .apply { + addProperty("vmOptions", data["vmOptions"].asJsonArray.joinToString("\n") { it.asString }) + data["logs"].asJsonObject.entrySet().forEach { (key, value) -> add("xtc.logs.$key", value) } + }.toString(), + "apply", + ) + service.transport(project, "incremental") + service.inlayHints(project, false) + inputs.sources(project, "RuntimeSettings", file.toUri().toString()) + inputs.rejectUntrustedImport(project) + lifecycle.show(project, file.toString()) + Files.writeString( + expected, + JsonObject() + .apply { + addProperty("runtime", runtime.content()) + addProperty("project", service.content(project)) + }.toString(), + ) + } + } else { + val saved = JsonParser.parseString(Files.readString(expected)).asJsonObject + withContext(OnDispatcher.EDT) { + val actualRuntime = runtime.content() + val actualProject = service.content(project) + check(actualRuntime == saved["runtime"].asString) { + "Runtime settings after restart: expected ${saved["runtime"].asString}, got $actualRuntime" + } + check(actualProject == saved["project"].asString) { + "Project settings after restart: expected ${saved["project"].asString}, got $actualProject" + } + lifecycle.show(project, file.toString()) + } + } + val status = ClientProtocol(this).query("xtc/languageServiceStatus", emptyMap()).asJsonObject + val expectedAdapter = if (System.getProperty("xtc.playbook.adapter") == "treesitter") "TreeSitter" else "XDK" + check(status["adapter"].asString == expectedAdapter) { "Settings ran against the wrong packaged backend: $status" } + check(status["textSynchronization"].asString == "incremental") + check(status["jvmOptions"].asJsonArray.contains(data["vmOptions"].asJsonArray.single())) + check(status["logs"].asJsonObject["retention"] == data["logs"]) + val pidFile = root.parent.resolve("persisted-settings-pid.txt") + if (phase == 0) { + Files.writeString(pidFile, status["pid"].asString) + } else { + check(Files.readString(pidFile) != status["pid"].asString) + } +} + +@Remote("org.xtclang.idea.playbook.probe.SettingsPersistencePage", plugin = "org.xtclang.playbook.probe") +internal interface SettingsPersistencePage { + fun sources( + project: Project, + module: String, + uri: String, + ) + + fun rejectUntrustedImport(project: Project) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt new file mode 100644 index 00000000000..4c8c04f34fc --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt @@ -0,0 +1,264 @@ +package org.xtclang.idea.playbook + +import com.google.gson.Gson +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import java.nio.file.Files +import java.nio.file.Path +import java.security.MessageDigest +import java.util.HexFormat + +/** Test data shared with the VS Code driver; editor actions remain native to each client. */ +class SharedScenarios( + val sourceHash: String, + val common: Common, + val scenarios: Map, +) { + data class Common( + val sourceModules: List, + val fixtures: List, + val editing: Editing, + val runtimeSettings: JsonObject, + val colorPrototype: JsonObject, + ) + + data class Fixture( + val file: String, + val pattern: String, + ) + + data class Editing( + val file: String, + val run: String, + val inspect: String, + ) + + data class Coverage( + val coverage: String, + val limitations: List, + ) + + data class Scenario( + val title: String, + val manual: List, + val values: JsonObject, + val intellij: Coverage, + ) { + fun text(key: String): String = requireNotNull(values[key]) { "Missing scenario value $key" }.asString + + fun strings(key: String): List = requireNotNull(values[key]).asJsonArray.map { it.asString } + + fun rows(key: String): List = requireNotNull(values[key]).asJsonArray.map { it.asJsonObject } + } + + private inline fun core(id: String): T { + val json = scenarios.getValue(id).values.deepCopy() + json.addProperty("id", id) + return Gson().fromJson(json, T::class.java) + } + + val diagnostics: Diagnostics = core("X2") + val definitions: Definitions = core("X4") + val completion: Completion = core("X7") + val dependencyNavigation: Navigation = core("X45") + val dependencyEdit: DependencyEdit = core("X46") + val configuration: Configuration = core("CFG1") + val warning: Warning = core("7a.8") + + data class SourceModule( + val name: String, + val uri: String, + val dependencies: List, + val resourceRoots: List? = null, + ) + + data class Edit( + val from: String, + val to: String, + ) { + fun apply(text: String): String { + val start = unique(text, from) + return text.replaceRange(start, start + from.length, to) + } + } + + data class Location( + val cursor: String, + val targetFile: String, + val target: String, + ) + + data class ExpectedError( + val edit: Edit, + val severity: String, + val codePrefix: String, + val messageContains: String, + val rangeStart: String, + val rangeEnd: String, + ) + + data class Diagnostics( + val id: String, + val file: String, + val errors: List, + val cleanAppend: String, + ) + + data class Definitions( + val id: String, + val file: String, + val locations: List, + ) + + data class Completion( + val id: String, + val file: String, + val edit: Edit, + val cursor: String, + val label: String, + val accepted: Edit, + ) + + data class Navigation( + val id: String, + val file: String, + val location: Location, + ) + + data class DependencyEdit( + val id: String, + val file: String, + val consumer: String, + val edit: Edit, + ) + + data class Configuration( + val id: String, + val consumer: String, + ) + + data class Warning( + val id: String, + val file: String, + val severity: String, + val code: String, + val messageContains: String, + val count: Int, + val declaration: String, + val edit: Edit, + ) + + val ids: List + get() = scenarios.keys.toList() + + val files: List + get() = common.fixtures.map { it.file } + + val implementedIds: Set + get() = scenarios.filterValues { it.intellij.coverage != "not-implemented" }.keys + + val graph: String + get() = + Gson() + .toJson( + mapOf( + "xtc" to mapOf("compiler" to mapOf("sourceModules" to common.sourceModules)), + ), + ) + + fun validate(fixtures: Map) { + diagnostics.errors.forEach { expected -> + val text = expected.edit.apply(fixtures.getValue(diagnostics.file)) + require(offset(text, expected.rangeStart) <= offset(text, expected.rangeEnd)) + } + definitions.locations.forEach { location -> + offset(fixtures.getValue(definitions.file), location.cursor) + offset(fixtures.getValue(location.targetFile), location.target) + } + val incomplete = completion.edit.apply(fixtures.getValue(completion.file)) + offset(incomplete, completion.cursor) + completion.accepted.apply(incomplete) + offset(fixtures.getValue(dependencyNavigation.file), dependencyNavigation.location.cursor) + offset( + fixtures.getValue(dependencyNavigation.location.targetFile), + dependencyNavigation.location.target, + ) + dependencyEdit.edit.apply(fixtures.getValue(dependencyEdit.file)) + offset(fixtures.getValue(warning.file), warning.declaration) + warning.edit.apply(fixtures.getValue(warning.file)) + files.forEach { fixtures.getValue(it) } + } + + companion object { + fun read(path: Path): SharedScenarios { + val contents = Files.readString(path) + val hash = + HexFormat + .of() + .formatHex( + MessageDigest + .getInstance("SHA-256") + .digest(contents.toByteArray(Charsets.UTF_8)), + ) + val json = JsonParser.parseString(contents).asJsonObject + require(json["schemaVersion"].asInt == 2) { "Unsupported shared playbook schema" } + val gson = Gson() + val scenarios = + json.getAsJsonObject("cases").entrySet().associate { (id, value) -> + id to gson.fromJson(value, Scenario::class.java) + } + val expected = (1..279).map { "X$it" } + listOf("CFG1", "CFG2", "CFG3", "7a.8", "7a.9") + require(scenarios.keys.toList() == expected) { + "The catalog must describe the complete playbook in order" + } + scenarios.forEach { (id, scenario) -> + require(scenario.intellij.coverage in setOf("full", "partial", "not-implemented")) { + "Missing IntelliJ coverage for $id" + } + require( + scenario.intellij.coverage == "full" || + scenario.intellij.limitations.isNotEmpty(), + ) { + "Explain the IntelliJ gap for $id" + } + } + return SharedScenarios( + hash, + gson.fromJson(json["common"], Common::class.java), + scenarios, + ) + } + + fun text( + template: String, + vararg values: String, + ): String { + val slots = Regex("\\$\\{(\\d+)\\}") + val indices = slots.findAll(template).map { it.groupValues[1].toInt() }.toSet() + require(indices == values.indices.toSet()) { + "Template substitutions do not match: $template" + } + return slots.replace(template) { values[it.groupValues[1].toInt()] } + } + + fun offset( + text: String, + marked: String, + ): Int { + val marker = unique(marked, "§") + return unique(text, marked.replace("§", "")) + marker + } + + private fun unique( + text: String, + anchor: String, + ): Int { + require(anchor.isNotEmpty()) { "Empty shared playbook anchor" } + val start = text.indexOf(anchor) + require(start >= 0 && text.indexOf(anchor, start + 1) < 0) { + "Expected one shared playbook anchor: $anchor" + } + return start + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SignatureHints.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SignatureHints.kt new file mode 100644 index 00000000000..d6de144bfd8 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SignatureHints.kt @@ -0,0 +1,143 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.PsiManager +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.ui +import kotlin.time.Duration.Companion.seconds + +/** A snapshot of the response produced by IntelliJ's Parameter Info action. */ +data class Signature( + val label: String, + val parameters: List, + val activeParameter: Int?, + val documentation: String? = null, +) + +/** Inspect the native action's future and visible popup; reopen only after recorded focus loss. */ +fun Driver.signature( + editor: JEditorUiComponent, + at: Int, + keepOpen: Boolean = false, + inspectDocumentation: Boolean = false, + matches: (List) -> Boolean, +) { + dismissPopups() + focusEditor(editor) + val popup = ui.x("//div[@class='ParameterInfoComponent']") + val support = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + editor.editor.getCaretModel().moveToOffset(at) + val file = + requireNotNull( + service(singleProject()).findFile(editor.editor.getVirtualFile()), + ) + utility(LspFileSupport::class).getSupport(file).getSignatureHelpSupport() + } + editor.scrollToCaretNow() + val inspection = + PopupInspection(this, editor) { + invokeAction("ParameterInfo", component = editor.component) + } + invokeAction("ParameterInfo", component = editor.component) + + fun renderedHints() = + ui.xx("//div[@class='ParameterInfoComponent']//div[@class='JBHtmlPane']").list().map { + cast(it.component, ParameterHintText::class).getText() + } + awaitUi( + message = "native parameter information for offset $at", + timeout = 45.seconds, + errorMessage = { + val future = support.getValidLSPFuture() + val help = future?.takeIf { it.isDone() && !it.isCompletedExceptionally() }?.get() + val labels = + help?.getSignatures()?.map { item -> + "${item.getLabel()} active=${item.getActiveParameter()} parameters=" + + item.getParameters().map { it.getLabel().getLeft() } + } + "Native future: $future; signatures=$labels activeSignature=${help?.getActiveSignature()} " + + "global=${help?.getActiveParameter()}; rendered hints: ${renderedHints()}" + }, + ) { + inspection.recover() + val future = support.getValidLSPFuture() + if (future == null || !future.isDone() || future.isCompletedExceptionally()) { + return@awaitUi false + } + val help = future.get() + val signatures = + help?.getSignatures().orEmpty().map { item -> + val label = item.getLabel() + Signature( + label, + item.getParameters().map { parameter -> + val name = parameter.getLabel() + name.getLeft() + ?: name.getRight().let { + label.substring(it.getFirst(), it.getSecond()) + } + }, + item.getActiveParameter() ?: help?.getActiveParameter(), + if (inspectDocumentation) { + ClientProtocol(this).copy(item.getDocumentation()).let { + when { + it.isJsonPrimitive -> it.asString + it.isJsonObject -> it.asJsonObject["value"]?.asString + else -> null + } + } + } else { + null + }, + ) + } + if (!matches(signatures)) return@awaitUi false + val rendered = renderedHints() + if (signatures.isEmpty()) return@awaitUi rendered.isEmpty() + val activeSignature = help?.getActiveSignature() + rendered.size == signatures.size && + signatures.indices.all { index -> + val signature = signatures[index] + val html = rendered[index] + val bold = + Regex("]*)?>[\\s\\S]*?") + .findAll(html) + .map { plainText(it.value) } + .toList() + // LSP4IJ dims inactive overloads and only highlights enabled rows. With no + // selected overload it enables every row; parameter overrides still apply. + val enabled = + activeSignature == null || activeSignature < 0 || activeSignature == index + val active = + signature.activeParameter + ?.takeIf { enabled } + ?.let { signature.parameters.getOrNull(it) } + plainText(html) + .contains( + if (signature.parameters.isEmpty()) { + signature.label + } else { + signature.parameters.joinToString(", ") + }, + ) && bold == listOfNotNull(active) + } + } + if (!keepOpen && popup.present()) invokeAction("EditorEscape", component = editor.component) +} + +private fun plainText(html: String): String = + html + .replace(Regex("[\\s\\S]*?"), "") + .replace(Regex("<[^>]+>"), "") + .replace("<", "<") + .replace(">", ">") + .replace(" ", " ") + .replace("&", "&") + .replace(Regex("\\s+"), " ") + .trim() diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt new file mode 100644 index 00000000000..a7511cb56eb --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt @@ -0,0 +1,144 @@ +package org.xtclang.idea.playbook + +import com.google.gson.GsonBuilder +import com.google.gson.JsonParser +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.Project +import com.intellij.driver.sdk.VirtualFile +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.ui +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Real restarts and editor changes; the probe asserts that initialization overlaps every edit. */ +fun Driver.startupEditing( + fixtures: Map, + shared: SharedScenarios, +) { + val short = "module Startup {\n Int run() {\n return 42;\n }\n}\n" + val invalid = short.replace("42", "unknownDuringStartup") + val long = + "module Startup {\n" + + (1..30).joinToString("\n") { " Int method$it() {\n return $it;\n }" } + + "\n}\n" + val receipts = + buildList { + ParityWorkspace(this@startupEditing, "STARTUP", fixtures, shared).use { workspace -> + workspace.write("Startup.x", long) + val path = workspace.directory.resolve("Startup.x").toString() + val source = + requireNotNull( + utility(ParityFiles::class).getInstance().refreshAndFindFileByPath(path), + ) + val cold = + withContext(OnDispatcher.EDT) { + utility(StartupEdits::class).openAndEdit(singleProject(), source, invalid) + }.let { JsonParser.parseString(it).asJsonObject } + val initial = workspace.open("Startup.x") + check(initial.text == invalid) + workspace.published(initial.file) { it.isNotEmpty() } + workspace.errors(initial) + awaitUi( + "untouched startup notification fades without mouse or keyboard input", + 20.seconds, + ) { + ui.x { byVisibleText("Ecstasy Language Server Started") }.notPresent() + } + add( + cold.apply { + addProperty("pid", workspace.protocol.server().getCurrentProcessId()) + addProperty("version", workspace.version(initial)) + addProperty("startupBalloonHidden", true) + }, + ) + listOf("error", "repair", "shorten", "reopen").forEach { phase -> + val document = workspace.open("Startup.x") + workspace.replace(document, long) + workspace.clean(document) + val expected = if (phase in setOf("error", "reopen")) invalid else short + val transaction = + withContext(OnDispatcher.EDT) { + utility(StartupEdits::class) + .restartAndEdit( + singleProject(), + document.editor.editor.getVirtualFile(), + if (phase == "shorten") long else invalid, + expected, + phase == "reopen", + ) + }.let { JsonParser.parseString(it).asJsonObject } + val current = workspace.open("Startup.x") + check(current.text == expected) + val pid = requireNotNull(workspace.protocol.server().getCurrentProcessId()) + check(pid != transaction["previousPid"].asLong) { + "Startup test reused the old process" + } + val version = workspace.version(current) + workspace.published(current.file) { rows -> + if (expected == invalid) rows.isNotEmpty() else rows.isEmpty() + } + awaitUi("startup $phase diagnostics reach the editor", 45.seconds) { + current.editor.installedDiagnostics().let { diagnostics -> + if (expected == invalid) { + diagnostics.any { "unknownDuringStartup" in it.description } + } else { + diagnostics.isEmpty() + } + } + } + awaitUi("startup $phase folds match the shortened buffer", 45.seconds) { + folds(current.editor).let { ranges -> + (1..3) in ranges && ranges.all { it.last < 5 } + } + } + check(Files.readString(Path.of(URI(current.uri))) == long) { + "Startup edits were unexpectedly saved" + } + add( + transaction.deepCopy().apply { + addProperty("phase", phase) + addProperty("pid", pid) + addProperty("version", version) + addProperty( + "diagnostics", + if (expected == invalid) "unknownDuringStartup" else "clear", + ) + }, + ) + workspace.replace(current, short) + workspace.published(current.file) { it.isEmpty() } + workspace.clean(current) + awaitUi("startup $phase old process exits", 10.seconds) { + ProcessHandle + .of(transaction["previousPid"].asLong) + .map { !it.isAlive } + .orElse(true) + } + } + } + } + val report = Path.of(singleProject().getBasePath()).parent.resolve("startup-editing.json") + Files.writeString(report, GsonBuilder().setPrettyPrinting().create().toJson(receipts) + "\n") + println("IntelliJ startup editing receipt: $report") +} + +@Remote("org.xtclang.idea.playbook.probe.StartupEdits", plugin = "org.xtclang.playbook.probe") +interface StartupEdits { + fun openAndEdit( + project: Project, + file: VirtualFile, + text: String, + ): String + + fun restartAndEdit( + project: Project, + file: VirtualFile, + intermediate: String, + finalText: String, + reopen: Boolean, + ): String +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt new file mode 100644 index 00000000000..73c758f275d --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt @@ -0,0 +1,175 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.LockSemantics +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.PsiManager +import com.intellij.driver.sdk.getToolWindow +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +import com.intellij.driver.sdk.ui.components.common.toolwindows.structureToolWindow +import com.intellij.driver.sdk.ui.ui +import kotlin.time.Duration.Companion.seconds + +/** + * Inspect the actual Structure tree populated by LSP4IJ, including deliberately absent + * declarations. + */ +fun Driver.structure( + editor: JEditorUiComponent, + include: List, + exclude: List = emptyList(), +) { + val visible = withContext(OnDispatcher.EDT) { getToolWindow("Structure").isVisible() } + if (!visible) invokeAction("ActivateStructureToolWindow", component = editor.component) + val tree = ui.structureToolWindow().waitAndGetStructureTree() + awaitUi("Structure contains $include and excludes $exclude", 45.seconds) { + tree.expandAll() + val rows = tree.collectExpandedPaths().map { it.path.last() } + + fun contains(name: String) = + rows.any { + Regex("(? = + withContext(OnDispatcher.EDT) { + cast(editor.editor, FoldingEditor::class) + .getFoldingModel() + .getAllFoldRegions() + .filter { it.isValid() } + .map { + val firstLine = editor.document.getLineNumber(it.getStartOffset()) + val lastLine = editor.document.getLineNumber(it.getEndOffset()) + firstLine..lastLine + } + } + +/** + * Extend Selection must grow around the same caret position without moving or editing the document. + */ +fun Driver.selectionParents( + editor: JEditorUiComponent, + at: Int, +) { + val original = editor.text + val support = + withContext(OnDispatcher.EDT, semantics = LockSemantics.READ_ACTION) { + editor.editor.getSelectionModel().removeSelection() + editor.editor.getCaretModel().moveToOffset(at) + val file = + requireNotNull( + service(singleProject()).findFile(editor.editor.getVirtualFile()), + ) + utility(LspFileSupport::class).getSupport(file).getSelectionRangeSupport() + } + val previous = support.getValidLSPFuture() + + fun selection() = + withContext(OnDispatcher.EDT) { + cast(editor.editor.getSelectionModel(), SelectionOffsets::class).let { + it.getSelectionStart()..it.getSelectionEnd() + } + } + invokeAction("EditorSelectWord", component = editor.component) + awaitUi("native selection action receives compiler selection parents", 45.seconds) { + val future = support.getValidLSPFuture() + if ( + future == null || + future == previous || + !future.isDone() || + future.isCompletedExceptionally() + ) { + return@awaitUi false + } + + fun offsets(range: SourceRange): IntRange = + withContext(OnDispatcher.EDT) { + val start = + range.getStart().let { + editor.document.getLineStartOffset(it.getLine()) + it.getCharacter() + } + val end = + range.getEnd().let { + editor.document.getLineStartOffset(it.getLine()) + it.getCharacter() + } + start until end + } + future.get().any { selected -> + val range = offsets(selected.getRange()) + selected.getParent()?.let { parent -> + val enclosing = offsets(parent.getRange()) + at in range && enclosing.first <= range.first && enclosing.last >= range.last + } == true + } + } + awaitUi("first structural selection contains the cursor", 15.seconds) { + selection().let { at in it && it.first < it.last } + } + val first = selection() + invokeAction("EditorSelectWord", component = editor.component) + awaitUi("parent structural selection contains the first selection", 15.seconds) { + selection().let { it.first <= first.first && it.last >= first.last && it != first } + } + check(editor.text == original) + withContext(OnDispatcher.EDT) { editor.editor.getSelectionModel().removeSelection() } +} + +@Remote("com.intellij.openapi.editor.Editor") +interface FoldingEditor { + fun getFoldingModel(): EditorFolding +} + +@Remote("com.intellij.openapi.editor.FoldingModel") +interface EditorFolding { + fun getAllFoldRegions(): Array +} + +@Remote("com.intellij.openapi.editor.FoldRegion") +interface EditorFoldRegion { + fun isValid(): Boolean + + fun getStartOffset(): Int + + fun getEndOffset(): Int +} + +@Remote("com.intellij.openapi.editor.SelectionModel") +interface SelectionOffsets { + fun getSelectionStart(): Int + + fun getSelectionEnd(): Int +} + +@Remote( + "com.redhat.devtools.lsp4ij.features.selectionRange.LSPSelectionRangeSupport", + plugin = "com.redhat.devtools.lsp4ij", +) +interface SelectionRangeSupport { + fun getValidLSPFuture(): SelectionRangeFuture? +} + +@Remote("java.util.concurrent.CompletableFuture") +interface SelectionRangeFuture { + fun isDone(): Boolean + + fun isCompletedExceptionally(): Boolean + + fun get(): List +} + +@Remote("org.eclipse.lsp4j.SelectionRange", plugin = "com.redhat.devtools.lsp4ij") +interface ClientSelectionRange { + fun getRange(): SourceRange + + fun getParent(): ClientSelectionRange? +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaits.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaits.kt new file mode 100644 index 00000000000..fef8eec95a4 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaits.kt @@ -0,0 +1,62 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.WaitForException +import com.intellij.openapi.diagnostic.fileLogger +import kotlin.time.Duration +import kotlin.time.Duration.Companion.milliseconds +import kotlin.time.Duration.Companion.seconds +import kotlin.time.TimeSource + +private val logger = fileLogger() + +/** Observe readiness promptly without shortening the existing failure deadlines. */ +internal fun awaitUi( + message: String? = null, + timeout: Duration = 5.seconds, + errorMessage: (() -> String)? = null, + condition: () -> Boolean, +) { + awaitUi( + message, + timeout, + errorMessage = errorMessage?.let { describe -> { _: Boolean -> describe() } }, + getter = condition, + checker = { it }, + ) +} + +internal fun awaitUi( + message: String? = null, + timeout: Duration = 5.seconds, + errorMessage: ((T) -> String)? = null, + getter: () -> T, + checker: (T) -> Boolean, +): T { + // TODO LSP4IJ: UP29 — Driver waitFor checks a successful remote predicate again before + // returning. A connection restart between those reads produces an immediate false timeout. + // Keep this until Driver accepts each observation once and uses a monotonic deadline. + val started = TimeSource.Monotonic.markNow() + message?.let { logger.info("Await: '$it' with timeout $timeout") } + while (true) { + val result = getter() + if (checker(result)) { + message?.let { logger.info("Await: '$it' completed in ${started.elapsedNow()}") } + return result + } + val remaining = timeout - started.elapsedNow() + if (remaining <= Duration.ZERO) { + throw WaitForException( + timeout, + errorMessage?.invoke(result) + ?: ("Failed: $message" + if (result is Boolean) "" else ". Actual: $result"), + ) + } + Thread.sleep(minOf(remaining, 100.milliseconds).inWholeMilliseconds.coerceAtLeast(1)) + } +} + +internal fun awaitUiNotNull( + message: String? = null, + timeout: Duration = 5.seconds, + getter: () -> T?, +): T = awaitUi(message, timeout, getter = getter, checker = { it != null })!! diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt new file mode 100644 index 00000000000..bc39a8f6feb --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt @@ -0,0 +1,71 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.sdk.WaitForException +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertSame +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import java.util.concurrent.CancellationException +import java.util.concurrent.atomic.AtomicInteger +import kotlin.time.Duration + +class UiWaitsTest { + @Test + fun `a successful readiness observation is not checked again`() { + val checks = AtomicInteger() + val connection = Any() + val result = + awaitUi( + getter = { connection }, + checker = { checks.incrementAndGet() == 1 }, + ) + assertSame(connection, result) + assertEquals(1, checks.get()) + } + + @Test + fun `pending observations are refreshed until a ready value arrives`() { + val reads = AtomicInteger() + val ready = awaitUi(getter = reads::incrementAndGet, checker = { it == 2 }) + assertEquals(2, ready) + assertEquals(2, reads.get()) + } + + @Test + fun `an expired pending observation reports its value and fails`() { + val checks = AtomicInteger() + val failure = + assertThrows(WaitForException::class.java) { + awaitUi( + message = "connection starts", + timeout = Duration.ZERO, + getter = { "starting" }, + checker = { checks.incrementAndGet() > 1 }, + ) + } + assertEquals(1, checks.get()) + assertTrue(failure.message.orEmpty().contains("connection starts")) + assertTrue(failure.message.orEmpty().contains("starting")) + } + + @Test + fun `cancellation from a readiness read propagates without retry`() { + val failure = CancellationException("Retired connection") + assertSame( + failure, + assertThrows(CancellationException::class.java) { + awaitUi(getter = { throw failure }, checker = { true }) + }, + ) + } + + @Test + fun `nullable readiness returns the observed value or fails at its deadline`() { + val value = Any() + assertSame(value, awaitUiNotNull(timeout = Duration.ZERO) { value }) + assertThrows(WaitForException::class.java) { + awaitUiNotNull(timeout = Duration.ZERO) { null } + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt new file mode 100644 index 00000000000..7a4e7f1f17d --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt @@ -0,0 +1,192 @@ +package org.xtclang.idea.playbook + +import com.google.gson.JsonObject +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.model.OnDispatcher +import com.intellij.driver.sdk.FileEditorManager +import com.intellij.driver.sdk.invokeAction +import com.intellij.driver.sdk.singleProject +import org.eclipse.lsp4j.InlineCompletionTriggerKind +import java.nio.file.Path +import kotlin.time.Duration.Companion.seconds + +/** Native acceptance for an explicitly selected upstream build; shipping expectations stay intact. */ +internal fun ParityWorkspace.nativeLibraryContent( + source: ParityWorkspace.Document, + at: Int, + target: JsonObject, + imported: JsonObject, + monikerScheme: String, +) = with(driver) { + val uri = target.string("uri") + val start = target["range"].asJsonObject["start"].asJsonObject + val manager = service(singleProject()) + + fun navigate(): NativeLibraryFile { + val editor = source.editor + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(at) } + invokeAction("GotoDeclaration", component = editor.component) + awaitUi("native definition opens virtual library $uri", 30.seconds) { + withContext(OnDispatcher.EDT) { manager.getSelectedTextEditor()?.getVirtualFile()?.getPath() == uri } + } + return withContext(OnDispatcher.EDT) { + val selected = requireNotNull(manager.getSelectedTextEditor()) + val text = selected.getDocument().getText() + check( + ParityWorkspace.position(text, selected.getCaretModel().getOffset()) == + mapOf("line" to start.int("line"), "character" to start.int("character")), + ) + cast(selected.getVirtualFile(), NativeLibraryFile::class) + } + } + + val file = navigate() + check(!file.isWritable() && file.isValid()) + val text = withContext(OnDispatcher.EDT) { requireNotNull(manager.getSelectedTextEditor()).getDocument().getText() } + val content = protocol.query("workspace/textDocumentContent", mapOf("uri" to uri)).asJsonObject.string("text") + check(text == content) + val exported = + protocol + .query( + "textDocument/moniker", + mapOf("textDocument" to mapOf("uri" to uri), "position" to start), + ).rows() + .single() + check(exported == imported.deepCopy().apply { addProperty("kind", "export") }) + check(exported.string("scheme") == monikerScheme) + check( + protocol + .query( + "textDocument/formatting", + mapOf("textDocument" to mapOf("uri" to uri), "options" to mapOf("tabSize" to 4, "insertSpaces" to true)), + ).rows() + .isEmpty(), + ) + + val stamp = file.getModificationStamp() + // Replacing compiler inputs sends a real server-to-client content refresh for fetched views. + // Each library type needs an actual settings change, including later iterations of X254. + configure(null) + configure(listOf(SharedScenarios.SourceModule("LibraryViews", source.uri, emptyList()))) + awaitUi("server refresh reloads the existing virtual library", 30.seconds) { file.getModificationStamp() != stamp } + check(file.isValid() && !file.isWritable()) + check(withContext(OnDispatcher.EDT) { requireNotNull(manager.getSelectedTextEditor()).getDocument().getText() } == text) + + source.editor + val previous = protocol.server().getCurrentProcessId() + protocol.server().restart() + awaitUi("restart retires the old virtual library", 45.seconds) { !file.isValid() } + awaitUi("replacement server owns library navigation", 45.seconds) { + protocol.server().getCurrentProcessId()?.let { it != previous } == true + } + // Retained diagnostic paint is not readiness for the replacement process. Match the + // initial navigation's definition query before checking the new native editor target. + check(query("textDocument/definition", source, at).rows().single().string("uri") == uri) + clean(source) + val reopened = navigate() + check(reopened.isValid() && !reopened.isWritable() && !file.isValid()) + check(withContext(OnDispatcher.EDT) { requireNotNull(manager.getSelectedTextEditor()).getDocument().getText() } == text) +} + +internal fun ParityWorkspace.nativeInlineAlternatives( + document: ParityWorkspace.Document, + offset: Int, + source: String, +) = with(driver) { + // Partial identifiers can have no diagnostics. Await a real report, not an error + // that the compiler deliberately suppresses while completion is available. + trace.diagnostics(document.uri) { true } + val editor = document.editor + focusEditor(editor) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(offset) } + + fun visible() = withContext(OnDispatcher.EDT) { utility(InlineCompletionProbe::class).visibleText(editor.editor) } + + fun requests() = + trace.requests("textDocument/inlineCompletion").filter { it["textDocument"].asJsonObject.string("uri") == document.uri } + invokeAction("CallInlineCompletionAction", component = editor.component) + awaitUi( + "explicit inline invocation displays an alternative", + 30.seconds, + errorMessage = { + "Visible inline text: '${visible()}'; requests: ${requests()}" + }, + ) { visible() in setOf("other", "swer") } + check(requests().any { it["context"].asJsonObject.int("triggerKind") == InlineCompletionTriggerKind.Invoked.value }) + val first = visible() + invokeAction("NextInlineCompletionSuggestionAction", component = editor.component) + awaitUi( + "native inline cycling displays the other alternative", + 15.seconds, + ) { visible() in setOf("other", "swer") && visible() != first } + if (visible() != "swer") { + invokeAction("NextInlineCompletionSuggestionAction", component = editor.component) + awaitUi("answer alternative is selected", 15.seconds) { visible() == "swer" } + } + invokeAction("InsertInlineCompletionAction", component = editor.component) + val expected = source.replaceRange(offset - 2, offset, "answer") + awaitUi("native alternative acceptance preserves the suffix", 15.seconds) { document.text == expected } + clean(document) + invokeAction("\$Undo", component = editor.component) + awaitUi("inline Undo restores the ambiguous prefix", 15.seconds) { document.text == source } + settle(document) + withContext(OnDispatcher.EDT) { editor.editor.getCaretModel().moveToOffset(offset) } + + invokeAction("CodeCompletion", component = editor.component) + val lookupManager = utility(EditorLookupManager::class).getInstance(singleProject()) + awaitUi("native popup offers both values", 30.seconds, errorMessage = { + trace.capture(Path.of(System.getProperty("xtc.playbook.reports"), "inline-popup-trace.txt")) + "Native popup items: ${lookupManager.getActiveLookup()?.getItems()?.map { it.getLookupString() }}" + }) { + lookupManager + .getActiveLookup() + ?.getItems() + ?.map { it.getLookupString() } + ?.containsAll(listOf("answer", "another")) == true + } + val lookup = requireNotNull(lookupManager.getActiveLookup()) + // A programmatic selection does not exercise the popup's keyboard selection event. + // Drive the same bounded list navigation as a user so inline completion sees it. + repeat(lookup.getItems().size) { + if (lookup.getCurrentItem()?.getLookupString() != "answer") { + invokeAction("EditorDown", component = editor.component) + } + } + check(lookup.getCurrentItem()?.getLookupString() == "answer") + awaitUi( + "native inline request includes the selected popup replacement", + 30.seconds, + errorMessage = { "Missing selected popup context; requests: ${requests()}" }, + ) { + requests().any { + it["context"] + .asJsonObject["selectedCompletionInfo"] + ?.takeIf { value -> + value.isJsonObject + }?.asJsonObject + ?.let { selected -> + selected.string("text") == "answer" && + listOf("start" to offset - 2, "end" to offset).all { (boundary, index) -> + val actual = selected["range"].asJsonObject[boundary].asJsonObject + val expectedPosition = ParityWorkspace.position(source, index) + actual.int("line") == expectedPosition.getValue("line") && + actual.int("character") == expectedPosition.getValue("character") + } + } == true + } + } + check(visible().isEmpty()) { "A complete popup item must not gain an incompatible inline suffix" } + dismissPopups() + check(document.text == source) +} + +@Remote("com.intellij.openapi.vfs.VirtualFile") +internal interface NativeLibraryFile { + fun isValid(): Boolean + + fun isWritable(): Boolean + + fun getModificationStamp(): Long +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt new file mode 100644 index 00000000000..d828567808b --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt @@ -0,0 +1,50 @@ +package org.xtclang.idea.playbook + +import com.intellij.driver.client.Driver +import com.intellij.driver.client.Remote +import com.intellij.driver.client.service +import com.intellij.driver.sdk.singleProject +import java.nio.file.Path + +/** + * Use the installed client's transport for the same workspace notification as the VS Code driver. + */ +fun Driver.changeWorkspaceFolders( + removed: Path, + added: Path, +) { + fun folder(path: Path) = + new( + RemoteWorkspaceFolder::class, + path.toUri().toString().removeSuffix("/"), + path.fileName.toString(), + ) + val event = + new(RemoteWorkspaceFoldersEvent::class, listOf(folder(added)), listOf(folder(removed))) + val params = new(RemoteWorkspaceFoldersParams::class, event) + service(singleProject()) + .getStartedServers() + .single() + .getLanguageServer() + .getWorkspaceService() + .didChangeWorkspaceFolders(params) +} + +@Remote("org.eclipse.lsp4j.services.LanguageServer", plugin = "com.redhat.devtools.lsp4ij") +interface RemoteLanguageServer { + fun getWorkspaceService(): RemoteWorkspaceService +} + +@Remote("org.eclipse.lsp4j.services.WorkspaceService", plugin = "com.redhat.devtools.lsp4ij") +interface RemoteWorkspaceService { + fun didChangeWorkspaceFolders(params: RemoteWorkspaceFoldersParams) +} + +@Remote("org.eclipse.lsp4j.WorkspaceFolder", plugin = "com.redhat.devtools.lsp4ij") +interface RemoteWorkspaceFolder + +@Remote("org.eclipse.lsp4j.WorkspaceFoldersChangeEvent", plugin = "com.redhat.devtools.lsp4ij") +interface RemoteWorkspaceFoldersEvent + +@Remote("org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams", plugin = "com.redhat.devtools.lsp4ij") +interface RemoteWorkspaceFoldersParams diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CodeLensUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CodeLensUi.kt new file mode 100644 index 00000000000..f1b5fdb0a6c --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CodeLensUi.kt @@ -0,0 +1,48 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.codeInsight.codeVision.lensContextIfCreated +import com.intellij.codeInsight.codeVision.ui.model.ClickableTextCodeVisionEntry +import com.intellij.codeInsight.codeVision.ui.model.TextCodeVisionEntry +import com.intellij.openapi.editor.Editor +import java.awt.event.MouseEvent + +/** Inspect installed Code Vision entries and activate their real click handler without moving the pointer. */ +object CodeLensUi { + @JvmStatic + fun titles(editor: Editor): List { + val context = editor.lensContextIfCreated ?: return emptyList() + return context + .getValidPairResult() + .map { it.second } + .filterIsInstance() + .map { it.text } + .toList() + } + + @JvmStatic + fun click( + editor: Editor, + title: String, + ) { + val entry = + requireNotNull(editor.lensContextIfCreated) + .getValidPairResult() + .map { it.second } + .filterIsInstance() + .single { it.text == title } + val point = editor.offsetToXY(editor.caretModel.offset) + val event = + MouseEvent( + editor.contentComponent, + MouseEvent.MOUSE_CLICKED, + System.currentTimeMillis(), + 0, + point.x, + point.y, + 1, + false, + MouseEvent.BUTTON1, + ) + entry.onClick.invoke(event, editor) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ColorUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ColorUi.kt new file mode 100644 index 00000000000..0a9602f08bc --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ColorUi.kt @@ -0,0 +1,165 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.codeInsight.hints.LinearOrderInlayRenderer +import com.intellij.ide.IdeEventQueue +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.WriteIntentReadAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.markup.TextAttributes +import com.intellij.ui.colorpicker.ColorValuePanel +import java.awt.Color +import java.awt.Component +import java.awt.Container +import java.awt.Point +import java.awt.Window +import java.awt.event.KeyEvent +import java.awt.event.MouseEvent +import java.awt.geom.Rectangle2D +import java.awt.image.BufferedImage +import java.nio.file.Path +import javax.imageio.ImageIO +import javax.swing.SwingUtilities + +/** Exercise the rendered inlay handler and real chooser fields without moving the desktop mouse. */ +object ColorUi { + private fun swatches(editor: Editor) = + editor.inlayModel.getInlineElementsInRange(0, editor.document.textLength).filter { + it.renderer is LinearOrderInlayRenderer<*> && "ColorInlayPresentation" in it.renderer.toString() + } + + @JvmStatic + fun offsets(editor: Editor): List = swatches(editor).filter { it.bounds != null }.map { it.offset } + + @JvmStatic + fun rgba(editor: Editor): List = + swatches(editor).flatMap { inlay -> + val bounds = requireNotNull(inlay.bounds) + // Paint the installed renderer on transparency, retaining the swatch's actual alpha. + val image = BufferedImage(bounds.width, bounds.height, BufferedImage.TYPE_INT_ARGB) + val graphics = image.createGraphics() + try { + graphics.font = editor.contentComponent.font + inlay.renderer.paint( + inlay, + graphics, + Rectangle2D.Float(0f, 0f, bounds.width.toFloat(), bounds.height.toFloat()), + TextAttributes(), + ) + } finally { + graphics.dispose() + } + val color = Color(image.getRGB(bounds.width / 2, bounds.height / 2), true) + listOf(color.red, color.green, color.blue, color.alpha) + } + + @JvmStatic + fun open( + editor: Editor, + offset: Int, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + val inlay = swatches(editor).single { it.offset == offset } + val bounds = requireNotNull(inlay.bounds) { "Color swatch is not rendered" } + val event = + MouseEvent( + editor.contentComponent, + MouseEvent.MOUSE_CLICKED, + System.currentTimeMillis(), + 0, + bounds.x + 2, + bounds.y + bounds.height / 2, + 1, + false, + MouseEvent.BUTTON1, + ) + val renderer = inlay.renderer as LinearOrderInlayRenderer<*> + val point = Point(2, bounds.height / 2) + // referenceOnHover installs its clickable presentation only after receiving a hover. + // Match the context provided by normal IDE input; Driver EDT calls do not acquire it. + WriteIntentReadAction.run { + renderer.mouseMoved(event, point) + renderer.mouseClicked(event, point) + } + } + + private fun descendants(component: Component): Sequence = + sequenceOf(component) + + (component as? Container) + ?.components + .orEmpty() + .asSequence() + .flatMap(::descendants) + + private fun picker(): ColorValuePanel? = + Window + .getWindows() + .asSequence() + .filter { it.isShowing } + .flatMap(::descendants) + .filterIsInstance() + .singleOrNull { it.isShowing } + + @JvmStatic + fun visible(): Boolean = picker() != null + + @JvmStatic + fun change(channel: String) { + ApplicationManager.getApplication().assertIsDispatchThread() + val panel = requireNotNull(picker()) { "Native color chooser is not visible" } + val field = if (channel == "alpha") panel.alphaField else panel.hexField + field.requestFocusInWindow() + // These are actual Swing text controls; their normal document listener updates the picker. + field.text = + when (channel) { + "alpha" -> { + when (field.text) { + "100" -> "50" + "255" -> "128" + else -> error("Unexpected initial native alpha: ${field.text}") + } + } + + "rgb" -> { + // The native hex control accepts RRGGBBAA; six digits reset alpha to opaque. + "2060C080" + } + + else -> { + error("Unknown picker control: $channel") + } + } + } + + @JvmStatic + fun dismiss() { + val field = requireNotNull(picker()).hexField + IdeEventQueue.getInstance().postEvent( + KeyEvent(field, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_ESCAPE, KeyEvent.CHAR_UNDEFINED), + ) + } + + @JvmStatic + fun screenshot( + editor: Editor, + file: String, + ) { + capture(SwingUtilities.getWindowAncestor(editor.contentComponent), file) + picker()?.let { panel -> + capture(SwingUtilities.getWindowAncestor(panel), file.removeSuffix(".png") + "-chooser.png") + } + } + + private fun capture( + window: Window, + file: String, + ) { + val image = BufferedImage(window.width, window.height, BufferedImage.TYPE_INT_RGB) + val graphics = image.createGraphics() + try { + window.paintAll(graphics) + } finally { + graphics.dispose() + } + check(ImageIO.write(image, "png", Path.of(file).toFile())) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerImportPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerImportPage.kt new file mode 100644 index 00000000000..fdef71f2236 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerImportPage.kt @@ -0,0 +1,103 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.WriteIntentReadAction +import com.intellij.openapi.externalSystem.importing.ImportSpecBuilder +import com.intellij.openapi.externalSystem.service.execution.ProgressExecutionMode +import com.intellij.openapi.externalSystem.util.ExternalSystemUtil +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.DialogWrapper +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.util.ui.UIUtil +import org.jetbrains.plugins.gradle.settings.GradleProjectSettings +import org.jetbrains.plugins.gradle.settings.GradleSettings +import org.jetbrains.plugins.gradle.util.GradleConstants +import org.xtclang.idea.lsp.CompilerBuildModel +import org.xtclang.idea.lsp.CompilerProjectConfigurable +import javax.swing.JButton +import javax.swing.JComponent +import javax.swing.JList +import javax.swing.JTextArea + +/** Display the shipping Compiler settings component; activate its buttons without pointer input. */ +class CompilerImportPage private constructor( + private val project: Project, +) : DialogWrapper(project, false) { + private val page = CompilerProjectConfigurable(project) + private val component = page.createComponent() + + init { + title = "Ecstasy Compiler — import playbook" + isModal = false + selectBuildImport() + init() + } + + override fun createCenterPanel(): JComponent = component + + private fun selectBuildImport() { + UIUtil + .uiTraverser(component) + .filter(JList::class.java) + .single { it.name == "xtc.compiler.navigation" } + .setSelectedValue("Build import", true) + } + + fun click(label: String) { + ApplicationManager.getApplication().assertIsDispatchThread() + selectBuildImport() + UIUtil + .uiTraverser(component) + .filter(JButton::class.java) + .single { it.text == label } + .also { check(it.isShowing && it.isEnabled) { "$label: showing=${it.isShowing}, enabled=${it.isEnabled}" } } + .doClick() + } + + fun text(): String { + ApplicationManager.getApplication().assertIsDispatchThread() + return requireNotNull(UIUtil.uiTraverser(component).filter(JTextArea::class.java).single()).text + } + + fun accepted(): String = CompilerBuildModel.read(project).toString() + + fun description(): String = CompilerBuildModel.describe(project) + + fun resetPage() = page.reset() + + fun sync() { + val root = requireNotNull(project.basePath) + val settings = GradleSettings.getInstance(project) + if (settings.getLinkedProjectSettings(root) == null) { + settings.linkProject(GradleProjectSettings().apply { externalProjectPath = root }) + } + ExternalSystemUtil.refreshProject( + root, + ImportSpecBuilder(project, GradleConstants.SYSTEM_ID) + .use(ProgressExecutionMode.IN_BACKGROUND_ASYNC) + .withImportProjectData(false), + ) + } + + fun unlink() { + GradleSettings.getInstance(project).unlinkExternalProject(requireNotNull(project.basePath)) + } + + fun refreshReports() { + WriteIntentReadAction.run { + LocalFileSystem.getInstance().refreshAndFindFileByPath("${project.basePath}/.gradle/xtc")?.refresh(false, true) + } + } + + fun closePage() = close(CANCEL_EXIT_CODE) + + override fun dispose() { + page.disposeUIResources() + super.dispose() + } + + companion object { + @JvmStatic + fun open(project: Project): CompilerImportPage = CompilerImportPage(project).also { it.show() } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerLibrariesPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerLibrariesPage.kt new file mode 100644 index 00000000000..1fd0231addf --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerLibrariesPage.kt @@ -0,0 +1,114 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.JsonParser +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.Project +import com.intellij.util.xmlb.XmlSerializerUtil +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings +import org.xtclang.idea.lsp.CompilerBuildModel +import org.xtclang.idea.lsp.CompilerProjectConfigurable +import java.awt.Component +import java.awt.Container +import javax.swing.DefaultListModel +import javax.swing.JButton +import javax.swing.JCheckBox +import javax.swing.JList +import javax.swing.JTable +import javax.swing.table.DefaultTableModel + +/** Operates the installed settings components; no replacement compiler settings implementation. */ +object CompilerLibrariesPage { + private const val SERVER = "xtcLanguageServer" + + private fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + + @JvmStatic + fun content(project: Project): String? = + ProjectLanguageServerSettings.getInstance(project).getLanguageServerSettings(SERVER)?.configurationContent + + @JvmStatic + fun edit( + project: Project, + json: String, + action: String, + ): String? { + val before = content(project) + val page = CompilerProjectConfigurable(project) + val component = page.createComponent() + val navigation = descendants(component).filterIsInstance>().single { it.name == "xtc.compiler.navigation" } + navigation.setSelectedValue("Libraries and sources", true) + val values = JsonParser.parseString(json).asJsonObject + val controls = descendants(component).toList() + val inherit = controls.filterIsInstance().single { it.text == "Inherit binary libraries from Gradle" } + inherit.isSelected = values["modulePath"].isJsonNull + val list = controls.filterIsInstance>().single { it.name == "Binary library paths" } + + @Suppress("UNCHECKED_CAST") + val paths = list.model as DefaultListModel + paths.clear() + values["modulePath"].takeUnless { it.isJsonNull }?.asJsonArray?.forEach { paths.addElement(it.asString) } + val table = controls.filterIsInstance().single { it.name == "Library source attachments" } + val rows = table.model as DefaultTableModel + rows.rowCount = 0 + values["sourceAttachments"].asJsonArray.forEach { attachment -> + val value = attachment.asJsonObject + value["roots"].asJsonArray.forEach { rows.addRow(arrayOf(value["module"].asString, it.asString)) } + } + navigation.setSelectedValue("Build import", true) + navigation.setSelectedValue("Libraries and sources", true) + check(descendants(component).any { it === table }) { "Changing pages must preserve the edited controls" } + when (action) { + "cancel" -> { + check(content(project) == before) + } + + "reset" -> { + page.reset() + check(!page.isModified()) + check(content(project) == before) + } + + "invalid" -> { + check(runCatching { page.apply() }.exceptionOrNull() is ConfigurationException) + check(content(project) == before) + } + + "order" -> { + list.selectedIndex = 1 + controls.filterIsInstance().single { it.text == "Up" }.doClick() + page.apply() + page.reset() + check(!page.isModified()) + } + + "apply" -> { + page.apply() + page.reset() + check(!page.isModified()) + } + + else -> { + error("Unknown library page action: $action") + } + } + check(CompilerBuildModel.describe(project).contains("Bundled XDK: read-only")) + page.disposeUIResources() + return content(project) + } + + @JvmStatic + fun restore( + project: Project, + content: String?, + ) { + val store = ProjectLanguageServerSettings.getInstance(project) + val current = store.getLanguageServerSettings(SERVER) ?: return + val copy = XmlSerializerUtil.createCopy(current) + copy.configurationContent = content + store.updateSettings(SERVER, copy) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerReportPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerReportPage.kt new file mode 100644 index 00000000000..6c961104d8a --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerReportPage.kt @@ -0,0 +1,87 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import org.xtclang.idea.lsp.CompilerProjectConfigurable +import org.xtclang.idea.lsp.SourceModuleConfiguration +import java.awt.Component +import java.awt.Container +import java.util.concurrent.CompletableFuture +import javax.swing.JList +import javax.swing.JTextArea + +/** + * Real settings UI with a controlled asynchronous data source; all state is confined to the EDT. + */ +class CompilerReportPage private constructor( + private val project: Project, +) { + private val requests = mutableListOf>>() + private val page = + CompilerProjectConfigurable(project) { + CompletableFuture>().also(requests::add) + } + private val component = page.createComponent() + + init { + descendants(component) + .filterIsInstance>() + .single { it.name == "xtc.compiler.navigation" } + .setSelectedValue("Build import", true) + } + + fun reset() = page.reset() + + fun text(): String = + descendants(component) + .filterIsInstance() + .single { it.name == "xtc.compiler.details" } + .text + + fun complete( + index: Int, + name: String, + ): CompletableFuture { + ApplicationManager.getApplication().assertIsDispatchThread() + requests[index].complete( + listOf(SourceModuleConfiguration(name, "file:///$name.x", emptyList())), + ) + // Run after the page's invokeLater publication, so a stale-reply assertion cannot pass + // merely because the callback has not run yet. + return CompletableFuture().also { barrier -> + ApplicationManager.getApplication().invokeLater { barrier.complete(null) } + } + } + + fun dispose() = page.disposeUIResources() + + fun completeAndRestart( + index: Int, + name: String, + ): CompletableFuture { + ApplicationManager.getApplication().assertIsDispatchThread() + val barrier = complete(index, name) + // Retire the actual connection before the queued publication can run on the EDT. + LanguageServiceAccessor + .getInstance(project) + .startedServers + .single() + .restart() + return barrier + } + + private fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + + companion object { + @JvmStatic + fun open(project: Project): CompilerReportPage { + ApplicationManager.getApplication().assertIsDispatchThread() + return CompilerReportPage(project) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerSettingsPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerSettingsPage.kt new file mode 100644 index 00000000000..d4680c08acb --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/CompilerSettingsPage.kt @@ -0,0 +1,173 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.notification.Notification +import com.intellij.notification.NotificationsManager +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.Project +import com.intellij.util.xmlb.XmlSerializerUtil +import com.redhat.devtools.lsp4ij.settings.GlobalLanguageServerSettings +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings +import org.xtclang.idea.lsp.CompilerBuildModel +import org.xtclang.idea.lsp.CompilerProjectConfigurable +import java.awt.Component +import java.awt.Container +import java.net.URI +import java.nio.file.Path +import javax.swing.JCheckBox +import javax.swing.JTable + +/** Exercise the real project settings component and its Apply/Reset contract on the EDT. */ +object CompilerSettingsPage { + private const val SERVER = "xtcLanguageServer" + + @JvmStatic + fun useBuildModel(project: Project): String { + val page = CompilerProjectConfigurable(project) + + fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + descendants(page.createComponent()) + .filterIsInstance() + .single { + it.text == + "Use Gradle model or automatic source discovery" + }.isSelected = + true + if (page.isModified()) page.apply() + CompilerBuildModel.publish(project) + return CompilerBuildModel.describe(project) + } + + @JvmStatic + fun refreshBuildModel(project: Project) { + val before = + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings(SERVER) + ?.configurationContent + CompilerBuildModel.publish(project) + check( + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings(SERVER) + ?.configurationContent == before, + ) + } + + @JvmStatic + fun installProjectGraph(project: Project) { + ApplicationManager.getApplication().assertIsDispatchThread() + val page = CompilerProjectConfigurable(project) + val component = page.createComponent() + check(!page.isModified()) + + fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + val discovery = + descendants(component).filterIsInstance().single { + it.text == + "Use Gradle model or automatic source discovery" + } + check(!discovery.isSelected) + discovery.doClick() + check(page.isModified()) + page.reset() + check(!page.isModified() && !discovery.isSelected) + val table = descendants(component).filterIsInstance().single { it.name == "Ecstasy source modules" } + val before = + GlobalLanguageServerSettings + .getInstance() + .getLanguageServerSettings(SERVER) + ?.configurationContent + table.model.setValueAt("", 0, 0) + check(runCatching { page.apply() }.exceptionOrNull() is ConfigurationException) + check( + GlobalLanguageServerSettings + .getInstance() + .getLanguageServerSettings(SERVER) + ?.configurationContent == before, + ) + page.reset() + // Edit actual table cells, preserving the graph's meaning while exercising relative roots. + val root = Path.of(requireNotNull(project.basePath)) + (0 until table.rowCount).forEach { row -> + val absolute = Path.of(URI(table.model.getValueAt(row, 1).toString())) + table.model.setValueAt(root.relativize(absolute).toString(), row, 1) + } + check(page.isModified()) + page.apply() + check(!page.isModified()) + check( + GlobalLanguageServerSettings + .getInstance() + .getLanguageServerSettings(SERVER) + ?.configurationContent == before, + ) + check( + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings(SERVER) + ?.configurationContent != null, + ) + } + + @JvmStatic + fun dismissExpectedConfigurationError(project: Project) { + NotificationsManager + .getNotificationsManager() + .getNotificationsOfType(Notification::class.java, project) + .filter { it.content.contains("Cyclic source dependencies:") } + .forEach(Notification::expire) + } + + @JvmStatic + fun resourceRootsRoundTrip( + project: Project, + roots: String, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + val page = CompilerProjectConfigurable(project) + + fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + val table = descendants(page.createComponent()).filterIsInstance().single { it.name == "Ecstasy source modules" } + val original = table.model.getValueAt(0, 3) + table.model.setValueAt("[]", 0, 3) + page.reset() + check(table.model.getValueAt(0, 3) == original && !page.isModified()) + table.model.setValueAt(roots, 0, 3) + page.apply() + page.reset() + check(table.model.getValueAt(0, 3) == roots && !page.isModified()) + } + + @JvmStatic + fun content(project: Project): String? = + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings(SERVER) + ?.configurationContent + ?: GlobalLanguageServerSettings + .getInstance() + .getLanguageServerSettings(SERVER) + ?.configurationContent + + @JvmStatic + fun clearProjectGraph(project: Project) { + val store = ProjectLanguageServerSettings.getInstance(project) + val current = store.getLanguageServerSettings(SERVER) ?: return + val copy = XmlSerializerUtil.createCopy(current) + copy.configurationContent = null + store.updateSettings(SERVER, copy) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorDiagnostics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorDiagnostics.kt new file mode 100644 index 00000000000..b6a3f0eb65a --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorDiagnostics.kt @@ -0,0 +1,33 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.Gson +import com.intellij.codeInsight.daemon.impl.HighlightInfo +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.impl.DocumentMarkupModel +import com.intellij.openapi.project.Project + +/** Loaded only in the disposable test IDE; never included in the distributed XTC plugin. */ +object EditorDiagnostics { + @JvmStatic + fun read( + editor: Editor, + project: Project, + ): String { + val document = DocumentMarkupModel.forDocument(editor.document, project, false) + val diagnostics = + ( + editor.markupModel.allHighlighters.asSequence() + + document?.allHighlighters.orEmpty().asSequence() + ).mapNotNull(HighlightInfo::fromRangeHighlighter) + .filter { it.severity.name in setOf("ERROR", "WARNING") } + .map { + mapOf( + "severity" to it.severity.name, + "description" to it.description.orEmpty(), + "start" to it.startOffset, + ) + }.distinct() + .toList() + return Gson().toJson(diagnostics) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorUi.kt new file mode 100644 index 00000000000..6fa673b1ad6 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/EditorUi.kt @@ -0,0 +1,154 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.codeInsight.completion.CompletionPhase +import com.intellij.codeInsight.completion.impl.CompletionServiceImpl +import com.intellij.codeInsight.lookup.LookupManager +import com.intellij.ide.DataManager +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.ScrollType +import com.intellij.openapi.wm.IdeFocusManager +import com.intellij.openapi.wm.ToolWindowManager +import com.intellij.psi.PsiDocumentManager +import com.intellij.ui.AppIcon +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import org.xtclang.idea.lsp.XtcRenameHandler +import java.awt.KeyboardFocusManager +import java.awt.Window +import java.util.concurrent.FutureTask +import java.util.concurrent.TimeUnit.SECONDS +import javax.swing.JFrame +import javax.swing.JLabel +import javax.swing.SwingUtilities + +/** Small IDE-side observations avoid walking the entire Swing tree for every focus poll. */ +object EditorUi { + @JvmStatic + fun renameAvailable(editor: Editor): Boolean = + XtcRenameHandler() + .isAvailableOnDataContext( + DataManager.getInstance().getDataContext(editor.contentComponent), + ) + + @JvmStatic + fun renameState(editor: Editor): String { + val project = requireNotNull(editor.project) + val file = PsiDocumentManager.getInstance(project).getPsiFile(editor.document) + val available = renameAvailable(editor) + val servers = + LanguageServiceAccessor.getInstance(project).startedServers.map { wrapper -> + "pid=${wrapper.currentProcessId}, rename=${wrapper.serverCapabilitiesSync?.renameProvider}, " + + "opened=${wrapper.openedDocuments.map { it.file.path }}, " + + "enabled=${file?.let { wrapper.clientFeatures.renameFeature.isEnabled(it) }}, error=${wrapper.serverError}" + } + return "available=$available, file=$file, servers=$servers; ${focusState(editor)}" + } + + @JvmStatic + fun focusEditor(editor: Editor) { + ApplicationManager.getApplication().assertIsDispatchThread() + val manager = IdeFocusManager.getInstance(editor.project) + SwingUtilities.invokeLater { + if (!editor.isDisposed) { + // Component focus alone can leave Find Usages as the active tool window; + // editor actions then use its preview's context instead of this source tab. + editor.project?.let { ToolWindowManager.getInstance(it).activateEditorComponent() } + manager.requestFocus(editor.contentComponent, true) + } + } + } + + @JvmStatic + fun isEditorActive(editor: Editor): Boolean = + editor.contentComponent.isFocusOwner && + editor.project?.let { ToolWindowManager.getInstance(it).activeToolWindowId } == null && + DataManager + .getInstance() + .getDataContext(editor.contentComponent) + .getData(CommonDataKeys.EDITOR) === editor + + @JvmStatic + fun focusState(editor: Editor): String = + onSwing { + ApplicationManager.getApplication().assertIsDispatchThread() + val window = SwingUtilities.getWindowAncestor(editor.contentComponent) + val manager = KeyboardFocusManager.getCurrentKeyboardFocusManager() + "editorDisposed=${editor.isDisposed}, window=${window?.javaClass?.simpleName}, " + + "windowFocused=${window?.isFocused}, active=${manager.activeWindow?.javaClass?.simpleName}, " + + "focused=${manager.focusedWindow?.javaClass?.simpleName}, owner=${manager.focusOwner?.javaClass?.name}, " + + "editorFocused=${editor.contentComponent.isFocusOwner}, " + + "activeToolWindow=${editor.project?.let { ToolWindowManager.getInstance(it).activeToolWindowId}}" + } + + /** A disposable, unowned window exercises real focus loss without touching another app. */ + @JvmStatic + fun interruptFocus(): Window { + ApplicationManager.getApplication().assertIsDispatchThread() + return JFrame("Playbook focus interruption").apply { + defaultCloseOperation = JFrame.DISPOSE_ON_CLOSE + add(JLabel("Testing focus recovery; this window closes automatically.")) + setSize(420, 100) + isVisible = true + AppIcon.getInstance().requestFocus(this) + } + } + + @JvmStatic + fun closeCompletion(editor: Editor) { + ApplicationManager.getApplication().assertIsDispatchThread() + CompletionServiceImpl.currentCompletionProgressIndicator?.closeAndFinish(true) + LookupManager.getActiveLookup(editor)?.hideLookup(true) + // Reopening an unapplied popup after focus loss is a fresh invocation. Retaining the + // previous phase makes IDEA broaden it to second-invocation word completion instead. + CompletionServiceImpl.setCompletionPhase(CompletionPhase.NoCompletion) + } + + @JvmStatic + fun hasFocus(editor: Editor): Boolean = + onSwing { + ApplicationManager.getApplication().assertIsDispatchThread() + val frame = SwingUtilities.getWindowAncestor(editor.contentComponent) ?: return@onSwing false + val manager = KeyboardFocusManager.getCurrentKeyboardFocusManager() + sequenceOf(manager.activeWindow, manager.focusedWindow).filterNotNull().any { active -> + generateSequence(active) { it.owner }.any { it === frame } + } + } + + @JvmStatic + fun popupFocus( + editor: Editor, + restoring: Boolean, + ) = onSwing { + editor.project?.let { PlaybookProgress.focus(it, restoring) } + if (restoring) { + val frame = SwingUtilities.getWindowAncestor(editor.contentComponent) + AppIcon.getInstance().requestFocus(frame) + AppIcon.getInstance().requestFocus() + } + } + + /** UI observation/focus only: never run document or PSI changes through this path. */ + private fun onSwing(action: () -> T): T { + if (SwingUtilities.isEventDispatchThread()) return action() + val task = FutureTask(action) + SwingUtilities.invokeLater(task) + return try { + task.get(10, SECONDS) + } finally { + task.cancel(false) + } + } + + @JvmStatic + fun scrollToCaret(editor: Editor) { + ApplicationManager.getApplication().assertIsDispatchThread() + val model = editor.scrollingModel + model.disableAnimation() + try { + model.scrollToCaret(ScrollType.CENTER) + } finally { + model.enableAnimation() + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/FileTreeOperations.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/FileTreeOperations.kt new file mode 100644 index 00000000000..6e1c7f19af2 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/FileTreeOperations.kt @@ -0,0 +1,116 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.actionSystem.impl.SimpleDataContext +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.application.WriteIntentReadAction +import com.intellij.openapi.command.undo.UndoManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.psi.PsiManager +import com.intellij.refactoring.move.MoveHandler +import com.intellij.refactoring.move.MoveHandlerDelegate +import com.intellij.refactoring.rename.RenameHandlerRegistry +import org.xtclang.idea.lsp.XtcFileMoveHandler +import org.xtclang.idea.lsp.XtcFileRenameHandler + +/** Invoke the registered file-tree Rename handler, including its real dialog and preflight. */ +object FileTreeOperations { + @JvmStatic + fun globalHistoryAvailable( + project: Project, + redo: Boolean, + ): Boolean = + UndoManager.getInstance(project).let { + if (redo) it.isRedoAvailable(null) else it.isUndoAvailable(null) + } + + @JvmStatic + fun move( + project: Project, + paths: List, + destination: String, + ) { + val files = + paths.map { + requireNotNull(LocalFileSystem.getInstance().refreshAndFindFileByPath(it)) + } + val target = + requireNotNull(LocalFileSystem.getInstance().refreshAndFindFileByPath(destination)) + ReadAction.runBlocking { + val manager = PsiManager.getInstance(project) + val elements = + files + .map { + requireNotNull( + if (it.isDirectory) manager.findDirectory(it) else manager.findFile(it), + ) + }.toTypedArray() + val directory = requireNotNull(manager.findDirectory(target)) + val context = + SimpleDataContext + .builder() + .add(CommonDataKeys.PROJECT, project) + .add(CommonDataKeys.VIRTUAL_FILE_ARRAY, files.toTypedArray()) + .build() + val handlers = MoveHandlerDelegate.EP_NAME.extensionList + val selected = handlers.firstOrNull { it.canMove(elements, directory, null) } + check(selected is XtcFileMoveHandler) { + "Unexpected Move handler: ${selected?.javaClass?.name}; candidates=" + + handlers.joinToString { + "${it.javaClass.name}=${it.canMove(elements, directory, null)}" + } + + "; sources=" + + files.joinToString { "${it.path}: ${it.children?.map { child -> child.name }}" } + } + ApplicationManager.getApplication().invokeLater { + WriteIntentReadAction.run { + MoveHandler.doMove(project, elements, directory, context, null) + } + } + } + } + + @JvmStatic + fun loadDirectory(path: String) { + val directory = requireNotNull(LocalFileSystem.getInstance().refreshAndFindFileByPath(path)) + // External fixtures are otherwise unknown to VFS: first discovery is not a create event. + directory.children + } + + @JvmStatic + fun rename( + project: Project, + path: String, + ) { + val file = requireNotNull(LocalFileSystem.getInstance().refreshAndFindFileByPath(path)) + ReadAction.runBlocking { + val psi = + requireNotNull( + if (file.isDirectory) { + PsiManager.getInstance(project).findDirectory(file) + } else { + PsiManager.getInstance(project).findFile(file) + }, + ) + val context = + SimpleDataContext + .builder() + .add(CommonDataKeys.PROJECT, project) + .add(CommonDataKeys.VIRTUAL_FILE, file) + .add(CommonDataKeys.VIRTUAL_FILE_ARRAY, arrayOf(file)) + .add(CommonDataKeys.PSI_ELEMENT, psi) + .build() + ApplicationManager.getApplication().invokeLater { + WriteIntentReadAction.run { + val handler = RenameHandlerRegistry.getInstance().getRenameHandler(context) + check(handler is XtcFileRenameHandler) { + "Unexpected file Rename handler: $handler" + } + handler.invoke(project, arrayOf(psi), context) + } + } + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/HighlightingUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/HighlightingUi.kt new file mode 100644 index 00000000000..14b2c25ef3e --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/HighlightingUi.kt @@ -0,0 +1,34 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.ex.EditorEx +import com.intellij.openapi.editor.impl.DocumentMarkupModel +import org.jetbrains.plugins.textmate.language.syntax.lexer.TextMateElementType + +/** Inspect the installed lexical highlighter and semantic markup without changing either. */ +object HighlightingUi { + @JvmStatic + fun lexical( + editor: Editor, + offset: Int, + ): String { + val iterator = (editor as EditorEx).highlighter.createIterator(offset) + check(!iterator.atEnd()) { "No lexical token at $offset" } + check(iterator.tokenType is TextMateElementType) { + "Ecstasy must use the registered TextMate grammar, not ${iterator.tokenType}" + } + val attributes = iterator.textAttributes + return "${iterator.tokenType}|${attributes.foregroundColor?.rgb}|${attributes.fontType}" + } + + @JvmStatic + fun semanticOverlays( + editor: Editor, + offset: Int, + ): List = + ( + editor.markupModel.allHighlighters.toList() + + DocumentMarkupModel.forDocument(editor.document, editor.project, false)?.allHighlighters.orEmpty() + ).filter { it.isValid && offset >= it.startOffset && offset < it.endOffset } + .mapNotNull { it.textAttributesKey?.externalName?.takeIf { name -> name.startsWith("LSP_") } } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/InlineCompletionUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/InlineCompletionUi.kt new file mode 100644 index 00000000000..f74adcb41ab --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/InlineCompletionUi.kt @@ -0,0 +1,30 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.codeInsight.inline.completion.session.InlineCompletionSession +import com.intellij.ide.DataManager +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.WriteIntentReadAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.editor.actionSystem.TypedAction + +/** Observe native ghost text; insertion and dismissal still go through the real IDE actions. */ +object InlineCompletionUi { + @JvmStatic + fun type( + editor: Editor, + text: String, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + WriteIntentReadAction.run { + val context = DataManager.getInstance().getDataContext(editor.contentComponent) + text.forEach { TypedAction.getInstance().actionPerformed(editor, it, context) } + } + } + + @JvmStatic + fun visibleText(editor: Editor): String { + ApplicationManager.getApplication().assertIsDispatchThread() + val session = InlineCompletionSession.getOrNull(editor) ?: return "" + return if (session.context.isCurrentlyDisplaying()) session.context.textToInsert() else "" + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LanguageServicePage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LanguageServicePage.kt new file mode 100644 index 00000000000..50507eefb6e --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LanguageServicePage.kt @@ -0,0 +1,182 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.JsonParser +import com.intellij.application.options.CodeStyle +import com.intellij.codeInsight.actions.onSave.FormatOnSaveOptions +import com.intellij.openapi.project.Project +import com.intellij.psi.codeStyle.CodeStyleSettingsManager +import com.intellij.ui.components.JBCheckBox +import com.intellij.util.xmlb.XmlSerializerUtil +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings +import org.xtclang.idea.XtcIntelliJLanguage +import org.xtclang.idea.lsp.LanguageServiceProjectConfigurable +import java.awt.Component +import java.awt.Container +import javax.swing.JComboBox + +/** Exercise actual settings components on the EDT, without replaying editor mutations. */ +object LanguageServicePage { + private fun children(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(children(it)) } + } + + @JvmStatic + fun exercise(project: Project) { + val before = content(project) + val page = LanguageServiceProjectConfigurable(project) + try { + val components = children(page.createComponent()).toList() + val inherit = + components.filterIsInstance().single { + it.name == "xtc.service.inherit" + } + val hints = + components.filterIsInstance().single { it.name == "xtc.service.hints" } + val saving = + components.filterIsInstance>().single { it.name == "xtc.service.save" } + check(!saving.isEnabled) { "Unavailable LSP4IJ save hook must not be offered" } + inherit.isSelected = false + hints.isSelected = !hints.isSelected + check(page.isModified()) + page.reset() + check(!page.isModified() && content(project) == before) + val initial = hints.isSelected + inherit.isSelected = false + hints.isSelected = !initial + page.apply() + check( + !page.isModified() && + JsonParser + .parseString(content(project)) + .asJsonObject["xtc"] + .asJsonObject["languageService"] + .asJsonObject["inlayHints"] + .asBoolean != initial, + ) + } finally { + page.disposeUIResources() + restore(project, before) + } + } + + @JvmStatic + fun content(project: Project): String? = + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings("xtcLanguageServer") + ?.configurationContent + + @JvmStatic + fun restore( + project: Project, + content: String?, + ) { + val store = ProjectLanguageServerSettings.getInstance(project) + val current = store.getLanguageServerSettings("xtcLanguageServer") ?: return + val copy = XmlSerializerUtil.createCopy(current) + copy.configurationContent = content + store.updateSettings("xtcLanguageServer", copy) + } + + @JvmStatic + fun inlayHints( + project: Project, + enabled: Boolean, + ) { + booleanSetting(project, "xtc.service.hints", enabled) + } + + @JvmStatic + fun referenceCodeLens( + project: Project, + enabled: Boolean, + ) { + booleanSetting(project, "xtc.service.references", enabled) + } + + private fun booleanSetting( + project: Project, + name: String, + enabled: Boolean, + ) { + val page = LanguageServiceProjectConfigurable(project) + try { + val components = children(page.createComponent()).filterIsInstance().toList() + components.single { it.name == "xtc.service.inherit" }.isSelected = false + components.single { it.name == name }.isSelected = enabled + page.apply() + } finally { + page.disposeUIResources() + } + } + + @JvmStatic + fun transport( + project: Project, + value: String, + ) { + choiceSetting(project, "xtc.service.sync", value) + } + + @JvmStatic + fun adapter( + project: Project, + value: String, + ) { + choiceSetting(project, "xtc.service.adapter", value) + } + + private fun choiceSetting( + project: Project, + name: String, + value: String, + ) { + val page = LanguageServiceProjectConfigurable(project) + try { + val components = children(page.createComponent()).toList() + components + .filterIsInstance() + .single { it.name == "xtc.service.inherit" } + .isSelected = false + val choices = + components + .filterIsInstance>() + .single { it.name == name } + choices.selectedIndex = (0 until choices.itemCount).single { choices.getItemAt(it).toString() == value } + page.apply() + } finally { + page.disposeUIResources() + } + } + + @JvmStatic + fun saveFormatting( + project: Project, + enabled: Boolean, + ): Boolean { + val options = FormatOnSaveOptions.getInstance(project) + val previous = options.isRunOnSaveEnabled + options.isRunOnSaveEnabled = enabled + return previous + } + + @JvmStatic + fun indent( + project: Project, + value: Int, + ): Int { + val options = + requireNotNull( + CodeStyle + .getProjectOrDefaultSettings(project) + .getCommonSettings(XtcIntelliJLanguage) + .indentOptions, + ) + val previous = options.INDENT_SIZE + options.INDENT_SIZE = value + CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged() + return previous + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LargeFileProbe.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LargeFileProbe.kt new file mode 100644 index 00000000000..14a438c994e --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/LargeFileProbe.kt @@ -0,0 +1,112 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.Gson +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.application.WriteAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.editor.RangeMarker +import com.intellij.openapi.editor.impl.DocumentImpl +import com.intellij.openapi.editor.impl.DocumentMarkupModel +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.util.DocumentUtil +import kotlin.time.TimeSource + +/** Uses real platform documents, including an unattached control with no PSI, editor or LSP. */ +object LargeFileProbe { + @JvmStatic + fun plain( + markers: Int, + bulk: Boolean, + ): String { + ApplicationManager.getApplication().assertIsDispatchThread() + val text = "abcdefghij\n".repeat(80_000) + val document = DocumentImpl(text) + val retained = + List(markers) { index -> + val offset = index * (text.length / markers) + document.createRangeMarker(offset, offset + 5) + } + try { + val before = document.rangeMarkersSize + val start = TimeSource.Monotonic.markNow() + WriteAction.run { + DocumentUtil.executeInBulk(document, bulk) { + document.setText(text.take(text.length / 4) + "replacement\n") + } + } + val elapsed = start.elapsedNow() + check(retained.count(RangeMarker::isValid) == markers / 4) + return Gson().toJson( + mapOf( + "phase" to "plain-document", + "bulk" to bulk, + "characters" to text.length, + "markers" to before, + "remainingMarkers" to document.rangeMarkersSize, + "milliseconds" to elapsed.inWholeMilliseconds, + ), + ) + } finally { + retained.forEach(RangeMarker::dispose) + } + } + + @JvmStatic + fun snapshot(file: VirtualFile): String = + ReadAction.computeBlocking { Gson().toJson(snapshot(document(file))) } + + @JvmStatic + fun replace( + project: Project, + file: VirtualFile, + text: String, + ): String { + val document = ReadAction.computeBlocking { document(file) } + val before = ReadAction.computeBlocking, RuntimeException> { snapshot(document) } + val start = TimeSource.Monotonic.markNow() + WriteCommandAction.runWriteCommandAction(project) { document.setText(text) } + val elapsed = start.elapsedNow() + return Gson().toJson( + mapOf( + "phase" to "editor-replacement", + "before" to before, + "after" to ReadAction.computeBlocking, RuntimeException> { snapshot(document) }, + "milliseconds" to elapsed.inWholeMilliseconds, + ), + ) + } + + private fun document(file: VirtualFile): DocumentImpl { + ApplicationManager.getApplication().assertIsDispatchThread() + return requireNotNull(FileDocumentManager.getInstance().getDocument(file)) as DocumentImpl + } + + private fun snapshot(document: DocumentImpl): Map { + val kinds = + buildList { + document.processRangeMarkers { marker -> + add(marker.javaClass.name) + true + } + } + return mapOf( + "phase" to "editor-snapshot", + "characters" to document.textLength, + "markers" to document.rangeMarkersSize, + "nodes" to document.rangeMarkersNodeSize, + "markerClasses" to kinds.groupingBy { it }.eachCount(), + "markupModels" to + DocumentMarkupModel.getExistingMarkupModels(document).map { model -> + val highlights = model.allHighlighters + mapOf( + "class" to model.javaClass.name, + "highlighters" to highlights.size, + "attributes" to highlights.groupingBy { it.textAttributesKey?.externalName ?: "none" }.eachCount(), + ) + }, + ) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/NavigationChooser.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/NavigationChooser.kt new file mode 100644 index 00000000000..a44fbd400f3 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/NavigationChooser.kt @@ -0,0 +1,36 @@ +package org.xtclang.idea.playbook.probe + +import java.awt.Component +import java.awt.event.ActionEvent +import java.awt.event.KeyEvent +import javax.swing.JComponent +import javax.swing.JTable +import javax.swing.KeyStroke +import javax.swing.SwingUtilities + +/** Submit the actual Show Usages chooser without sending an untargeted desktop keystroke. */ +object NavigationChooser { + @JvmStatic + fun choose( + component: Component, + row: Int, + ) { + check(SwingUtilities.isEventDispatchThread()) + val table = component as JTable + check(table.isShowing) { "Navigation chooser is no longer visible" } + require(row in 0 until table.rowCount) { "Missing navigation row $row" } + // PopupChooserBuilder registers its Enter callback on this map. WHEN_FOCUSED can + // instead contain JTable's ordinary next-row action, which does not navigate. + val enter = KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0) + val bindings = table.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT) + require(enter in bindings.keys().orEmpty()) { "Navigation chooser has no local Enter binding" } + val binding = + requireNotNull(bindings.get(enter)) { + "Navigation chooser has no Enter binding" + } + val action = requireNotNull(table.actionMap.get(binding)) { "Navigation chooser has no Enter action" } + check(action.isEnabled) { "Navigation chooser Enter action is disabled" } + table.setRowSelectionInterval(row, row) + action.actionPerformed(ActionEvent(table, ActionEvent.ACTION_PERFORMED, enter.toString())) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PartialResults.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PartialResults.kt new file mode 100644 index 00000000000..c7da010b215 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PartialResults.kt @@ -0,0 +1,57 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.JSONUtils +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.lifecycle.LanguageServerLifecycleListener +import com.redhat.devtools.lsp4ij.lifecycle.LanguageServerLifecycleManager +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import java.util.concurrent.ConcurrentLinkedQueue + +/** Observe only the requested token; verbose console buffers can roll over during a long run. */ +class PartialResults private constructor( + private val project: Project, + private val token: String, +) : LanguageServerLifecycleListener { + private val batches = ConcurrentLinkedQueue() + + override fun handleLSPMessage( + message: Message, + consumer: MessageConsumer, + wrapper: LanguageServerWrapper, + ) { + if (message !is NotificationMessage || message.method != "$/progress") return + val params = JSONUtils.getLsp4jGson().toJsonTree(message.params).asJsonObject + if (params["token"]?.asString == token) batches.add(params["value"].toString()) + } + + fun values(): List = batches.toList() + + override fun handleStatusChanged(wrapper: LanguageServerWrapper) = Unit + + override fun handleError( + wrapper: LanguageServerWrapper, + error: Throwable, + ) = Unit + + override fun dispose() { + LanguageServerLifecycleManager + .getInstance(project) + .removeLanguageServerLifecycleListener(this) + } + + companion object { + @JvmStatic + fun listen( + project: Project, + token: String, + ): PartialResults = + PartialResults(project, token).also { + LanguageServerLifecycleManager + .getInstance(project) + .addLanguageServerLifecycleListener(it) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt new file mode 100644 index 00000000000..f0d1f061580 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt @@ -0,0 +1,84 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.wm.CustomStatusBarWidget +import com.intellij.openapi.wm.WindowManager +import java.beans.PropertyChangeListener +import javax.swing.JComponent +import javax.swing.JFrame +import javax.swing.JLabel + +/** Persistent progress belongs only to the disposable test IDE, never the shipping plugin. */ +object PlaybookProgress { + private const val ID = "XtcPlaybookProgress" + + @JvmStatic + fun install(project: Project) { + ApplicationManager.getApplication().assertIsDispatchThread() + val windows = WindowManager.getInstance() + windows + .getStatusBar(project) + ?.addWidget(Widget(windows.getFrame(project)), "before Position", project) + } + + @JvmStatic + fun update( + project: Project, + text: String, + detail: String, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + val status = requireNotNull(WindowManager.getInstance().getStatusBar(project)) + (status.getWidget(ID) as Widget).show(text, detail) + status.updateWidget(ID) + } + + @JvmStatic + fun focus( + project: Project, + restoring: Boolean, + ) { + val widget = WindowManager.getInstance().getStatusBar(project)?.getWidget(ID) as Widget + val text = widget.label.text.substringBefore(" [focus:") + update(project, text + if (restoring) " [focus: restoring]" else "", widget.label.toolTipText) + } + + @JvmStatic + fun text(project: Project): String = (WindowManager.getInstance().getStatusBar(project)?.getWidget(ID) as Widget).label.text + + private class Widget( + private val frame: JFrame?, + ) : CustomStatusBarWidget { + val label = JLabel("Ecstasy playbook: starting").apply { toolTipText = text } + private val originalTitle = frame?.title + private val titleListener = PropertyChangeListener { updateTitle() } + + init { + frame?.addPropertyChangeListener("title", titleListener) + updateTitle() + } + + fun show( + text: String, + detail: String, + ) { + label.text = text + label.toolTipText = detail + updateTitle() + } + + private fun updateTitle() { + if (frame != null && frame.title != label.text) frame.title = label.text + } + + override fun ID(): String = ID + + override fun getComponent(): JComponent = label + + override fun dispose() { + frame?.removePropertyChangeListener("title", titleListener) + if (frame != null) frame.title = originalTitle + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProgressUi.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProgressUi.kt new file mode 100644 index 00000000000..c4681d6f893 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProgressUi.kt @@ -0,0 +1,68 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.panel.ProgressPanel +import com.intellij.openapi.wm.WindowManager +import com.intellij.openapi.wm.ex.StatusBarEx +import com.intellij.util.ui.UIUtil +import java.awt.Window +import javax.swing.JProgressBar + +/** Observe real progress and activate its visible Cancel control without moving the pointer. */ +object ProgressUi { + private fun status(project: Project): StatusBarEx { + ApplicationManager.getApplication().assertIsDispatchThread() + return WindowManager.getInstance().getStatusBar(project) as StatusBarEx + } + + @JvmStatic + fun visible( + project: Project, + title: String, + ): Boolean = + status(project).backgroundProcessModels.any { (_, model) -> + model.title.contains(title) && model.isRunning() && model.isCancellable() + } + + @JvmStatic + fun show(project: Project) { + status(project).isProcessWindowOpen = true + } + + @JvmStatic + fun hide(project: Project) { + status(project).isProcessWindowOpen = false + } + + @JvmStatic + fun cancel( + project: Project, + title: String, + ): Boolean { + val model = + status(project) + .backgroundProcessModels + .map { it.second } + .singleOrNull { it.title.contains(title) && it.isRunning() && it.isCancellable() } ?: return false + val control = + Window + .getWindows() + .asSequence() + .filter { it.isShowing } + .flatMap { UIUtil.uiTraverser(it).filter(JProgressBar::class.java).asSequence() } + // The current IDE uses ProgressPanel, not ProgressComponent's legacy child panel. + // Match the unique displayed title to the project's running model; heavyweight + // popups can use Swing's shared owner frame rather than the project frame. + .mapNotNull(ProgressPanel::getProgressPanel) + .filter { it.labelText == model.title } + .mapNotNull { it.cancelButton } + .filter { it.isShowing && it.isEnabled } + .distinct() + .singleOrNull() ?: return false + return control.accessibleContext.accessibleAction.doAccessibleAction(0) + } + + @JvmStatic + fun alive(pid: Long): Boolean = ProcessHandle.of(pid).map { it.isAlive }.orElse(false) +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProjectLifecycle.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProjectLifecycle.kt new file mode 100644 index 00000000000..1ecc04eca67 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ProjectLifecycle.kt @@ -0,0 +1,96 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.ide.impl.OpenProjectTask +import com.intellij.ide.trustedProjects.TrustedProjects +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.application.WriteIntentReadAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.editor.Document +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.project.ex.ProjectManagerEx +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.wm.WindowManager +import kotlinx.coroutines.runBlocking +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CompletableFuture + +/** Native project lifetime; no fake language clients or process termination in the assertions. */ +object ProjectLifecycle { + @JvmStatic + fun open(path: String): CompletableFuture = + CompletableFuture.supplyAsync { + runBlocking { + // The driver created this fixture. Trust only its exact path in the disposable IDE. + TrustedProjects.setProjectTrusted(Path.of(path), true) + ProjectManagerEx.getInstanceEx().openProjectAsync( + Path.of(path), + OpenProjectTask { + forceOpenInNewFrame = true + isNewProject = !Files.exists(Path.of(path, ".idea")) + }, + ) + } + } + + @JvmStatic + fun close(project: Project): CompletableFuture = + CompletableFuture.supplyAsync { + runBlocking { ProjectManagerEx.getInstanceEx().forceCloseProjectAsync(project, save = true) } + } + + @JvmStatic + fun show( + project: Project, + path: String, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + WriteIntentReadAction.run { openFile(project, path) } + } + + @JvmStatic + fun edit( + project: Project, + path: String, + text: String, + ) { + ApplicationManager.getApplication().assertIsDispatchThread() + WriteIntentReadAction.run { + val file = openFile(project, path) + val document = + ReadAction.computeBlocking { + requireNotNull(FileDocumentManager.getInstance().getDocument(file)) + } + WriteCommandAction.runWriteCommandAction(project) { document.setText(text) } + } + } + + private fun openFile( + project: Project, + path: String, + ): VirtualFile = + requireNotNull(LocalFileSystem.getInstance().refreshAndFindFileByNioFile(Path.of(path))).also { + FileEditorManager.getInstance(project).openFile(it, false) + } + + @JvmStatic + fun text(path: String): String = + ReadAction.computeBlocking { + val file = requireNotNull(LocalFileSystem.getInstance().findFileByNioFile(Path.of(path))) + requireNotNull(FileDocumentManager.getInstance().getDocument(file)).text + } + + @JvmStatic + fun visible(project: Project): Boolean { + ApplicationManager.getApplication().assertIsDispatchThread() + return WindowManager + .getInstance() + .getIdeFrame(project) + ?.component + ?.isShowing == true + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/RefreshRequests.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/RefreshRequests.kt new file mode 100644 index 00000000000..a9a28ea78bd --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/RefreshRequests.kt @@ -0,0 +1,122 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.Gson +import com.intellij.ide.ui.UISettings +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.client.LanguageClientImpl +import com.redhat.devtools.lsp4ij.lifecycle.LanguageServerLifecycleListener +import com.redhat.devtools.lsp4ij.lifecycle.LanguageServerLifecycleManager +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentLinkedQueue + +/** Bounded to one scenario's lifetime; observe real refresh requests without replacing handlers. */ +class RefreshRequests private constructor( + private val project: Project, + private val owner: LanguageServerWrapper, +) : LanguageServerLifecycleListener { + private val requests = ConcurrentLinkedQueue() + + override fun handleLSPMessage( + message: Message, + consumer: MessageConsumer, + wrapper: LanguageServerWrapper, + ) { + if ( + wrapper === owner && + message is RequestMessage && + message.method.startsWith("workspace/") && + message.method.endsWith("/refresh") + ) { + requests.add(message.method) + } + } + + fun values(): List = requests.toList() + + fun negotiated(): List { + // Test-only observation of the actual handshake, not a second capability builder. + // Fail on an upstream representation change instead of silently skipping providers. + val initialization = + LanguageServerWrapper::class.java.getDeclaredField("initParams").run { + isAccessible = true + get(owner) as InitializeParams + } + // Read the handshake's data: the driver and installed plugin can expose different + // LSP4J API versions, especially for newer optional capabilities such as folding refresh. + val workspace = Gson().toJsonTree(requireNotNull(initialization.capabilities.workspace)).asJsonObject + return mapOf( + "diagnostics" to "diagnostic", + "semanticTokens" to "semanticTokens", + "inlayHint" to "inlayHint", + "codeLens" to "codeLens", + "foldingRange" to "foldingRange", + ).mapNotNull { (capability, method) -> + "workspace/$method/refresh".takeIf { + workspace.getAsJsonObject(capability)?.get("refreshSupport")?.asBoolean == true + } + } + } + + fun clear() = requests.clear() + + fun tabLimit(value: Int): Int { + ApplicationManager.getApplication().assertIsDispatchThread() + val settings = UISettings.getInstance() + val previous = settings.editorTabLimit + settings.editorTabLimit = value + return previous + } + + /** Exercise the real handlers while the write lock keeps their NBRA work queued. */ + fun burst(): CompletableFuture { + ApplicationManager.getApplication().assertIsDispatchThread() + check(owner.openedDocuments.size >= 16) { "Refresh regression needs many connected documents" } + val client = + LanguageServerWrapper::class.java.getDeclaredField("languageClient").run { + isAccessible = true + get(owner) as LanguageClientImpl + } + return WriteCommandAction.writeCommandAction(project).compute, RuntimeException> { + CompletableFuture.allOf( + *buildList { + repeat(64) { + add(client.refreshCodeLenses()) + add(client.refreshInlayHints()) + add(client.refreshSemanticTokens()) + } + }.toTypedArray(), + ) + } + } + + override fun handleStatusChanged(wrapper: LanguageServerWrapper) = Unit + + override fun handleError( + wrapper: LanguageServerWrapper, + error: Throwable, + ) = Unit + + override fun dispose() { + LanguageServerLifecycleManager + .getInstance(project) + .removeLanguageServerLifecycleListener(this) + } + + companion object { + @JvmStatic + fun listen(project: Project): RefreshRequests = + RefreshRequests(project, LanguageServiceAccessor.getInstance(project).startedServers.single()).also { + LanguageServerLifecycleManager + .getInstance(project) + .addLanguageServerLifecycleListener(it) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ServerRuntimePage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ServerRuntimePage.kt new file mode 100644 index 00000000000..117c2e6e969 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/ServerRuntimePage.kt @@ -0,0 +1,96 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.Project +import org.xtclang.idea.lsp.ExportServerLogsAction +import org.xtclang.idea.lsp.ServerRuntimeConfigurable +import java.awt.Component +import java.awt.Container +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS +import javax.swing.JButton +import javax.swing.JSpinner +import javax.swing.JTextArea + +/** Exercises the installed application settings page, including validation and explicit restart. */ +object ServerRuntimePage { + private fun descendants(component: Component): Sequence = + sequence { + yield(component) + if (component is Container) component.components.forEach { yieldAll(descendants(it)) } + } + + @JvmStatic + fun content(): String { + val page = ServerRuntimeConfigurable() + try { + val controls = descendants(page.createComponent()).toList() + return JsonObject() + .apply { + addProperty("vmOptions", controls.filterIsInstance().single().text) + controls.filterIsInstance().forEach { addProperty(it.name, it.value as Int) } + }.toString() + } finally { + page.disposeUIResources() + } + } + + @JvmStatic + fun edit( + json: String, + action: String, + ) { + val before = content() + val page = ServerRuntimeConfigurable() + try { + val controls = descendants(page.createComponent()).toList() + val values = JsonParser.parseString(json).asJsonObject + values["vmOptions"]?.let { controls.filterIsInstance().single().text = it.asString } + controls.filterIsInstance().forEach { spinner -> + values[spinner.name]?.let { spinner.value = it.asInt } + } + when (action) { + "cancel" -> { + check(content() == before) + } + + "reset" -> { + page.reset() + check(!page.isModified()) + check(content() == before) + } + + "invalid" -> { + check(runCatching { page.apply() }.exceptionOrNull() is ConfigurationException) + check(content() == before) + } + + "apply" -> { + page.apply() + check(!page.isModified()) + } + + "restart" -> { + controls.filterIsInstance().single { it.name == "xtc.runtime.restart" }.doClick() + } + + else -> { + error("Unknown runtime page action: $action") + } + } + } finally { + page.disposeUIResources() + } + } + + @JvmStatic + fun export( + project: Project, + destination: String, + ) { + // Driver calls this on a background thread. The bounded wait is for completion, not a delay. + ExportServerLogsAction.export(project, Path.of(destination)).get(30, SECONDS) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/SettingsPersistencePage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/SettingsPersistencePage.kt new file mode 100644 index 00000000000..a5d26b4b897 --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/SettingsPersistencePage.kt @@ -0,0 +1,56 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.ide.trustedProjects.TrustedProjects +import com.intellij.openapi.project.Project +import com.intellij.ui.components.JBCheckBox +import com.intellij.util.ui.UIUtil +import org.xtclang.idea.lsp.CompilerBuildModel +import org.xtclang.idea.lsp.CompilerProjectConfigurable +import javax.swing.JTable +import javax.swing.table.DefaultTableModel + +/** Configure the shipping UI; the second IDE launch reads its normal persisted settings. */ +object SettingsPersistencePage { + @JvmStatic + fun sources( + project: Project, + module: String, + uri: String, + ) { + val page = CompilerProjectConfigurable(project) + try { + val component = page.createComponent() + UIUtil + .uiTraverser(component) + .filter(JBCheckBox::class.java) + .single { it.text == "Use Gradle model or automatic source discovery" } + .isSelected = false + val model = + UIUtil + .uiTraverser(component) + .filter(JTable::class.java) + .single { it.name == "Ecstasy source modules" } + .model as DefaultTableModel + model.rowCount = 0 + model.addRow(arrayOf(module, uri, "", "[]")) + page.apply() + } finally { + page.disposeUIResources() + } + } + + @JvmStatic + fun rejectUntrustedImport(project: Project) { + val trusted = TrustedProjects.isProjectTrusted(project) + val before = CompilerBuildModel.read(project) + try { + TrustedProjects.setProjectTrusted(project, false) + var refusal: String? = null + CompilerBuildModel.refresh(project, true) { refusal = it } + check(refusal?.contains("Trust this project") == true) + check(CompilerBuildModel.read(project) == before) + } finally { + TrustedProjects.setProjectTrusted(project, trusted) + } + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/StartupEdits.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/StartupEdits.kt new file mode 100644 index 00000000000..c1c389596ef --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/StartupEdits.kt @@ -0,0 +1,86 @@ +package org.xtclang.idea.playbook.probe + +import com.google.gson.Gson +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.editor.Document +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.ServerStatus + +/** Execute one real editor transaction before the restarted server finishes initialization. */ +object StartupEdits { + @JvmStatic + fun openAndEdit( + project: Project, + file: VirtualFile, + text: String, + ): String { + ApplicationManager.getApplication().assertIsDispatchThread() + val clients = LanguageServiceAccessor.getInstance(project) + check(clients.startedServers.isEmpty()) { + "Cold-start edit requires a fresh language client" + } + FileEditorManager.getInstance(project).openFile(file, false) + val document = + ReadAction.computeBlocking { + requireNotNull(FileDocumentManager.getInstance().getDocument(file)) + } + WriteCommandAction.runWriteCommandAction(project) { document.setText(text) } + check(clients.startedServers.none { it.serverStatus == ServerStatus.started }) { + "Server initialized before the cold-start edit completed" + } + return Gson() + .toJson( + mapOf( + "phase" to "cold-open", + "stamp" to document.modificationStamp, + "text" to document.text, + ), + ) + } + + @JvmStatic + fun restartAndEdit( + project: Project, + file: VirtualFile, + intermediate: String, + finalText: String, + reopen: Boolean, + ): String { + ApplicationManager.getApplication().assertIsDispatchThread() + val server = LanguageServiceAccessor.getInstance(project).startedServers.single() + val previousPid = server.currentProcessId + val document = + ReadAction.computeBlocking { + requireNotNull(FileDocumentManager.getInstance().getDocument(file)) + } + server.restart() + val initialized = server.initializedServer + check(!initialized.isDone) { "Server initialized before the startup edit" } + val before = server.serverStatus.name + WriteCommandAction.runWriteCommandAction(project) { document.setText(intermediate) } + if (reopen) { + val editors = FileEditorManager.getInstance(project) + editors.closeFile(file) + editors.openFile(file, false) + } + WriteCommandAction.runWriteCommandAction(project) { document.setText(finalText) } + check(!initialized.isDone) { "Server initialized during the startup transaction" } + return Gson() + .toJson( + mapOf( + "previousPid" to previousPid, + "statusBefore" to before, + "statusAfter" to server.serverStatus.name, + "stamp" to document.modificationStamp, + "reopened" to reopen, + "text" to document.text, + ), + ) + } +} diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/WorkspaceEdits.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/WorkspaceEdits.kt new file mode 100644 index 00000000000..d7a6baec18a --- /dev/null +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/WorkspaceEdits.kt @@ -0,0 +1,29 @@ +package org.xtclang.idea.playbook.probe + +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.JSONUtils +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.xtclang.idea.lsp.XtcClientFeatures +import java.util.concurrent.CompletableFuture + +/** Exercise the installed client's application entry point without adding a server test RPC. */ +object WorkspaceEdits { + @JvmStatic + fun apply( + project: Project, + json: String, + ): CompletableFuture { + val features = + LanguageServiceAccessor + .getInstance(project) + .startedServers + .single() + .clientFeatures + as XtcClientFeatures + return features.applyEdit( + JSONUtils.getLsp4jGson().fromJson(json, ApplyWorkspaceEditParams::class.java), + ) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/PluginPaths.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/PluginPaths.kt index 6560c9f3260..55cd8e34236 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/PluginPaths.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/PluginPaths.kt @@ -9,26 +9,28 @@ import java.nio.file.Path /** * Shared utility for resolving files bundled with the XTC plugin. * - * Server JARs (LSP, DAP) are placed in the plugin's `bin/` directory -- NOT `lib/`. - * If placed in `lib/`, IntelliJ loads their bundled lsp4j classes which conflict - * with LSP4IJ's lsp4j. The `bin/` directory is not on IntelliJ's classloader path. + * Server JARs (LSP, DAP) are placed in the plugin's `bin/` directory -- NOT `lib/`. If placed in + * `lib/`, IntelliJ loads their bundled lsp4j classes which conflict with LSP4IJ's lsp4j. The `bin/` + * directory is not on IntelliJ's classloader path. */ object PluginPaths { - /** Plugin ID, must match `` in `META-INF/plugin.xml`. The single source - * of truth for the string — every other call site that needs it should - * reference `PluginPaths.PLUGIN_ID` rather than duplicate the literal. */ + /** + * Plugin ID, must match `` in `META-INF/plugin.xml`. The single source of truth for the + * string — every other call site that needs it should reference `PluginPaths.PLUGIN_ID` rather + * than duplicate the literal. + */ const val PLUGIN_ID = "org.xtclang.idea" private val logger = logger() /** * This plugin's own [PluginDescriptor], or `null` if it cannot be determined. * - * Resolved from our own classes' classloader, which the IntelliJ Platform loads - * via a [PluginAwareClassLoader] that carries the owning plugin's descriptor. This - * is the supported, public way for a plugin to obtain its own descriptor (path, - * version, id). It deliberately avoids `PluginManager.findEnabledPlugin(PluginId)` - * and `PluginManagerCore.getPlugin(PluginId)`, both of which are marked - * `@ApiStatus.Internal` as of 2026.2 and flagged by the Plugin Verifier. + * Resolved from our own classes' classloader, which the IntelliJ Platform loads via a + * [PluginAwareClassLoader] that carries the owning plugin's descriptor. This is the supported, + * public way for a plugin to obtain its own descriptor (path, version, id). It deliberately + * avoids `PluginManager.findEnabledPlugin(PluginId)` and + * `PluginManagerCore.getPlugin(PluginId)`, both of which are marked `@ApiStatus.Internal` as of + * 2026.2 and flagged by the Plugin Verifier. */ fun selfDescriptor(): PluginDescriptor? = (PluginPaths::class.java.classLoader as? PluginAwareClassLoader)?.pluginDescriptor @@ -48,9 +50,13 @@ object PluginPaths { selfDescriptor()?.pluginPath?.let { pluginPath -> val candidate = pluginPath.resolve("bin/$jarName") searchedPaths.add(candidate) - resolveInBin(pluginPath, jarName)?.let { return it } + resolveInBin(pluginPath, jarName)?.let { + return it + } logger.warn("$jarName not at expected location: $candidate") - logger.warn("Plugin directory contents: ${pluginPath.toFile().listFiles()?.map { it.name }}") + logger.warn( + "Plugin directory contents: ${pluginPath.toFile().listFiles()?.map { it.name }}", + ) } // Fallback: find via classloader (our class is in lib/, JAR is in bin/) @@ -58,7 +64,9 @@ object PluginPaths { val pluginDir = Path.of(classUrl.toURI()).parent.parent val candidate = pluginDir.resolve("bin/$jarName") searchedPaths.add(candidate) - resolveInBin(pluginDir, jarName)?.let { return it } + resolveInBin(pluginDir, jarName)?.let { + return it + } logger.warn("$jarName not found via classloader either: $candidate") } @@ -66,15 +74,17 @@ object PluginPaths { buildString { appendLine("$jarName not found. Searched locations:") searchedPaths.forEach { appendLine(" - $it") } - appendLine("JARs must be in bin/ (NOT lib/) to avoid classloader conflicts with LSP4IJ.") + appendLine( + "JARs must be in bin/ (NOT lib/) to avoid classloader conflicts with LSP4IJ.", + ) append("This is a plugin packaging issue. Please report it.") }, ) } /** - * Resolve a JAR in a plugin directory's `bin/` subdirectory. - * Returns the path if the file exists, null otherwise. + * Resolve a JAR in a plugin directory's `bin/` subdirectory. Returns the path if the file + * exists, null otherwise. */ internal fun resolveInBin( pluginDir: Path, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcCommenter.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcCommenter.kt index 1860a564818..a0f11110281 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcCommenter.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcCommenter.kt @@ -3,21 +3,21 @@ package org.xtclang.idea import com.intellij.lang.Commenter /** - * Tells IntelliJ what comment syntax XTC uses, enabling Ctrl+/ (toggle line comment) - * and Ctrl+Shift+/ (toggle block comment). + * Tells IntelliJ what comment syntax XTC uses, enabling Ctrl+/ (toggle line comment) and + * Ctrl+Shift+/ (toggle block comment). * * ## Why this lives in the IntelliJ plugin, not the LSP server * - * Comment toggling is a **text editing action**, not a language intelligence feature. - * When the user presses Ctrl+/, the IDE needs to insert or remove `//` immediately — - * no round-trip to the language server. The LSP specification has no "toggle comment" - * request; it intentionally leaves this to the client. + * Comment toggling is a **text editing action**, not a language intelligence feature. When the user + * presses Ctrl+/, the IDE needs to insert or remove `//` immediately — no round-trip to the + * language server. The LSP specification has no "toggle comment" request; it intentionally leaves + * this to the client. * - * What the LSP server *does* handle is **formatting comments** after they exist: - * the `textDocument/onTypeFormatting` handler auto-continues `*` prefixes inside - * doc comments on Enter, and `textDocument/formatting` aligns comment interior lines. - * Those are formatting concerns that benefit from AST context. But inserting the - * comment delimiters themselves is purely a client-side operation. + * What the LSP server *does* handle is **formatting comments** after they exist: the + * `textDocument/onTypeFormatting` handler auto-continues `*` prefixes inside doc comments on Enter, + * and `textDocument/formatting` aligns comment interior lines. Those are formatting concerns that + * benefit from AST context. But inserting the comment delimiters themselves is purely a client-side + * operation. */ class XtcCommenter : Commenter { override fun getLineCommentPrefix(): String = "// " diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcFileType.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcFileType.kt index 2cc9cea946f..974527cf83f 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcFileType.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcFileType.kt @@ -6,10 +6,10 @@ import javax.swing.Icon /** * Native IntelliJ file type for Ecstasy source files. * - * TextMate still provides syntax highlighting through [XtcTextMateBundleProvider], - * and LSP4IJ still routes language features by file-name pattern. This file type - * exists so IntelliJ knows that the installed Ecstasy plugin owns `*.x` files and - * does not offer unrelated Marketplace plugins for the extension. + * TextMate still provides syntax highlighting through [XtcTextMateBundleProvider], and LSP4IJ still + * routes language features by file-name pattern. This file type exists so IntelliJ knows that the + * installed Ecstasy plugin owns `*.x` files and does not offer unrelated Marketplace plugins for + * the extension. */ object XtcFileType : LanguageFileType(XtcIntelliJLanguage) { override fun getName(): String = "Ecstasy" diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIconProvider.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIconProvider.kt index 77d02e4550b..de220554380 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIconProvider.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIconProvider.kt @@ -6,9 +6,7 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile import javax.swing.Icon -/** - * Provides custom icons for XTC files (.x extension). - */ +/** Provides custom icons for XTC files (.x extension). */ class XtcIconProvider : IconProvider() { override fun getIcon( element: PsiElement, @@ -17,7 +15,8 @@ class XtcIconProvider : IconProvider() { companion object { val XTC_ICON: Icon? by lazy { - runCatching { IconLoader.getIcon("/icons/xtc.svg", XtcIconProvider::class.java) }.getOrNull() + runCatching { IconLoader.getIcon("/icons/xtc.svg", XtcIconProvider::class.java) } + .getOrNull() } } } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIntelliJLanguage.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIntelliJLanguage.kt index bae9ea54ec6..3aceadf4144 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIntelliJLanguage.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcIntelliJLanguage.kt @@ -5,16 +5,15 @@ import com.intellij.lang.Language /** * IntelliJ [Language] registration for XTC (Ecstasy). * - * This is a minimal Language singleton that enables IntelliJ platform features - * requiring a Language instance, such as Code Style settings. Syntax highlighting - * is provided by the TextMate bundle ([XtcTextMateBundleProvider]), not by this - * Language registration. + * This is a minimal Language singleton that enables IntelliJ platform features requiring a Language + * instance, such as Code Style settings. plugin.xml explicitly registers TextMate's syntax and + * editor highlighter providers for this language/file type, using [XtcTextMateBundleProvider]. * - * **Important:** The Language ID must NOT be `"xtc"` — that ID is used by the TextMate - * bundle (package.json `languages[0].id`). If both use the same ID, IntelliJ associates - * `.x` files with this Language instead of TextMate, breaking syntax highlighting - * (white background, no colors) and indentation (no PSI context for indent rules). - * Using `"Ecstasy"` avoids the collision while still anchoring Code Style settings. + * **Important:** The Language ID must NOT be `"xtc"` — that ID is used by the TextMate bundle + * (package.json `languages[0].id`). If both use the same ID, IntelliJ associates `.x` files with + * this Language instead of TextMate. Using `"Ecstasy"` keeps the bundle language and native file + * ownership distinct while anchoring Code Style settings. Native ownership requires the explicit + * highlighter registrations; registering the bundle alone does not supply a lexical highlighter. */ object XtcIntelliJLanguage : Language("Ecstasy") { private fun readResolve(): Any = XtcIntelliJLanguage diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcTextMateBundleProvider.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcTextMateBundleProvider.kt index 327a3a11e89..64977e31912 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcTextMateBundleProvider.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcTextMateBundleProvider.kt @@ -5,21 +5,13 @@ import org.jetbrains.plugins.textmate.api.TextMateBundleProvider import kotlin.io.path.exists /** - * Provides TextMate bundle for XTC syntax highlighting. - * The bundle is located in the plugin's lib/textmate directory. + * Provides TextMate bundle for XTC syntax highlighting. The bundle is located in the plugin's + * lib/textmate directory. * - * // TODO LSP: TextMate grammars are regex-based and have limitations: - * // - Cannot distinguish type names from variable names - * // - Cannot highlight based on semantic information (is this a field? parameter?) - * // - Regex patterns can be slow and fragile for complex syntax - * // - * // When the parallel compiler is complete, REPLACE TextMate with LSP semantic tokens: - * // 1. LSP server provides getSemanticTokens() using real lexer + symbol resolution - * // 2. IntelliJ LSP client receives and applies semantic token highlighting - * // 3. TextMate becomes fallback only (for when LSP is not available) - * // - * // See: PLAN_LSP_PARALLEL_LEXER.md (Phase 1 - Semantic Tokens) - * // See: XtcCompilerAdapter.getSemanticTokens() + * The grammar supplies lexical scopes, including escapes and control keywords. Compiler semantic + * tokens refine resolved names and their modifiers while leaving that lexical detail intact. + * plugin.xml explicitly binds TextMate's highlighter providers to the native Ecstasy file type; + * registering a bundle alone does not associate a highlighter with an independently owned file type. */ class XtcTextMateBundleProvider : TextMateBundleProvider { private val logger = logger() @@ -52,8 +44,8 @@ class XtcTextMateBundleProvider : TextMateBundleProvider { // List contents of textmate directory logger.warn("Contents of textmate: ${textmatePath.toFile().listFiles()?.map { it.name }}") - val bundle = TextMateBundleProvider.PluginBundle("XTC", textmatePath) - logger.warn("Registered TextMate bundle: XTC at $textmatePath") + val bundle = TextMateBundleProvider.PluginBundle("Ecstasy", textmatePath) + logger.warn("Registered TextMate bundle: Ecstasy at $textmatePath") return listOf(bundle) } } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/dap/XtcDebugAdapterFactory.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/dap/XtcDebugAdapterFactory.kt index 31de571e8bb..f3c505a4238 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/dap/XtcDebugAdapterFactory.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/dap/XtcDebugAdapterFactory.kt @@ -20,14 +20,13 @@ import org.xtclang.idea.PluginPaths import java.io.File /** - * Factory for creating XTC Debug Adapter (DAP) descriptors. + * Factory for creating Ecstasy Debug Adapter (DAP) descriptors. * - * Registered via the `com.redhat.devtools.lsp4ij.debugAdapterServer` extension point. - * LSP4IJ uses this factory to create DAP sessions when users launch debug configurations - * for `.x` files. + * Registered via the `com.redhat.devtools.lsp4ij.debugAdapterServer` extension point. LSP4IJ uses + * this factory to create DAP sessions when users launch debug configurations for `.x` files. * - * The DAP server runs out-of-process (same as the LSP server) using IntelliJ's JBR, - * for classloader isolation and crash safety. + * The DAP server runs out-of-process (same as the LSP server) using IntelliJ's JBR, for classloader + * isolation and crash safety. */ class XtcDebugAdapterFactory : DebugAdapterDescriptorFactory() { private val logger = logger() @@ -36,7 +35,7 @@ class XtcDebugAdapterFactory : DebugAdapterDescriptorFactory() { options: RunConfigurationOptions, environment: ExecutionEnvironment, ): DebugAdapterDescriptor { - logger.info("Creating XTC DAP descriptor") + logger.info("Creating Ecstasy DAP descriptor") return XtcDebugAdapterDescriptor(options, environment, serverDefinition) } @@ -47,26 +46,27 @@ class XtcDebugAdapterFactory : DebugAdapterDescriptorFactory() { } /** - * Descriptor that launches the XTC DAP server as an out-of-process Java application. + * Descriptor that launches the Ecstasy DAP server as an out-of-process Java application. * - * The DAP server communicates over stdio (JSON-RPC), matching the architecture of the - * LSP server. Uses IntelliJ's JBR java binary to spawn the server process. + * The DAP server communicates over stdio (JSON-RPC), matching the architecture of the LSP server. + * Uses IntelliJ's JBR java binary to spawn the server process. * * ## LSP vs DAP process lifecycle * * The LSP and DAP servers use different LSP4IJ base classes with different process models: * * - **LSP** ([org.xtclang.idea.lsp.XtcLspConnectionProvider]): Extends - * `OSProcessStreamConnectionProvider`. Uses [com.redhat.devtools.lsp4ij.server.JavaProcessCommandBuilder] - * to build the command line -- LSP4IJ owns the process lifecycle, calling `start()`/`stop()` as needed. - * LSP4IJ may auto-start the server concurrently when multiple `.x` files are opened, causing - * duplicate processes (see TODO in `XtcLspConnectionProvider` re: LSP4IJ issue #888). This - * requires an `AtomicBoolean` guard to suppress duplicate "server started" notifications. + * `OSProcessStreamConnectionProvider`. Uses + * [com.redhat.devtools.lsp4ij.server.JavaProcessCommandBuilder] to build the command line -- + * LSP4IJ owns the process lifecycle, calling `start()`/`stop()` as needed. LSP4IJ may auto-start + * the server concurrently when multiple `.x` files are opened, causing duplicate processes (see + * TODO in `XtcLspConnectionProvider` re: LSP4IJ issue #888). This requires an `AtomicBoolean` + * guard to suppress duplicate "server started" notifications. * - * - **DAP** (this class): Extends `DebugAdapterDescriptor`. We override [startServer] and return - * an `OSProcessHandler` -- we create and own the process directly. DAP sessions are always - * user-initiated (one `startServer()` call per "Debug" action), so there is no concurrent - * spawn race condition and no `AtomicBoolean` guard is needed. + * - **DAP** (this class): Extends `DebugAdapterDescriptor`. We override [startServer] and return an + * `OSProcessHandler` -- we create and own the process directly. DAP sessions are always + * user-initiated (one `startServer()` call per "Debug" action), so there is no concurrent spawn + * race condition and no `AtomicBoolean` guard is needed. */ class XtcDebugAdapterDescriptor( options: RunConfigurationOptions, @@ -98,7 +98,7 @@ class XtcDebugAdapterDescriptor( serverJar.toString(), ) - logger.info("Starting XTC DAP server: ${commandLine.commandLineString}") + logger.info("Starting Ecstasy DAP server: ${commandLine.commandLineString}") return OSProcessHandler(commandLine) } @@ -115,7 +115,7 @@ class XtcDebugAdapterDescriptor( override fun getDebugMode(): DebugMode = DebugMode.LAUNCH - override fun getServerReadyConfig(debugMode: DebugMode): ServerReadyConfig = ServerReadyConfig("XTC Debug Adapter") + override fun getServerReadyConfig(debugMode: DebugMode): ServerReadyConfig = ServerReadyConfig("Ecstasy Debug Adapter") override fun getFileType(): FileType? = FileTypeManager.getInstance().getFileTypeByExtension("x") diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEditorStartupActivity.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEditorStartupActivity.kt index 2c11567821b..dff5389e7d0 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEditorStartupActivity.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEditorStartupActivity.kt @@ -45,9 +45,11 @@ class XtcEditorStartupActivity : ProjectActivity { FileEditorManagerListener.FILE_EDITOR_MANAGER, object : FileEditorManagerListener { override fun selectionChanged(event: FileEditorManagerEvent) { - event.newFile?.takeIf { it.extension == "x" }?.let { - logXtcFile(project, it.path, "selectionChanged") - } + event.newFile + ?.takeIf { it.extension == "x" } + ?.let { + logXtcFile(project, it.path, "selectionChanged") + } } }, ) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEnterHandlerDelegate.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEnterHandlerDelegate.kt index bb86785fd08..750fc659552 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEnterHandlerDelegate.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/editor/XtcEnterHandlerDelegate.kt @@ -15,10 +15,10 @@ import com.intellij.psi.PsiDocumentManager import com.intellij.psi.PsiFile /** - * Repairs IntelliJ's local brace-split behavior for `.x` files when Enter is pressed - * after a declaration ending in `{`. LSP on-type formatting does not reliably receive - * the first Enter in this path, so we normalize the editor-local result here using the - * current IntelliJ code style settings. + * Repairs IntelliJ's local brace-split behavior for `.x` files when Enter is pressed after a + * declaration ending in `{`. LSP on-type formatting does not reliably receive the first Enter in + * this path, so we normalize the editor-local result here using the current IntelliJ code style + * settings. */ class XtcEnterHandlerDelegate : EnterHandlerDelegateAdapter() { private val logger = logger() @@ -211,11 +211,21 @@ class XtcEnterHandlerDelegate : EnterHandlerDelegateAdapter() { val fix = when { isCompactEmptyBlockSplit(document, currentLine) -> { - rewriteEmptyBlock(document, currentLine - 1, currentLine, currentLine + 1) + rewriteEmptyBlock( + document, + currentLine - 1, + currentLine, + currentLine + 1, + ) } isClosingBraceCaretAfterBadSplit(document, currentLine) -> { - rewriteEmptyBlock(document, currentLine - 2, currentLine - 1, currentLine) + rewriteEmptyBlock( + document, + currentLine - 2, + currentLine - 1, + currentLine, + ) } else -> { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt new file mode 100644 index 00000000000..584b54fcb58 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt @@ -0,0 +1,81 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.command.undo.BasicUndoableAction +import com.intellij.openapi.command.undo.UndoManager +import com.intellij.openapi.editor.Document +import com.intellij.openapi.fileEditor.FileDocumentManager +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.LSPIJUtils +import org.eclipse.lsp4j.WorkspaceEdit + +// TODO LSP4IJ: UP21 — WorkspaceEdit leaves closed documents unsaved and disconnected from the +// server. Persist only this transaction's closed documents, including after native Undo/Redo. +internal class ClosedRefactoringDocuments private constructor( + private val project: Project, + private val documents: List, +) { + fun beforeApply() { + if (documents.isEmpty()) return + UndoManager.getInstance(project).undoableActionPerformed( + object : BasicUndoableAction() { + override fun isGlobal() = true + + // Registered first: Undo saves after all text and path changes have been reversed. + override fun undo() = save() + + override fun redo() = Unit + }, + ) + } + + fun afterApply() { + if (documents.isEmpty()) return + save() + UndoManager.getInstance(project).undoableActionPerformed( + object : BasicUndoableAction() { + override fun isGlobal() = true + + override fun undo() = Unit + + // Registered last: Redo saves after the final text and paths have been restored. + override fun redo() = save() + }, + ) + } + + private fun save() { + val manager = FileDocumentManager.getInstance() + val editors = FileEditorManager.getInstance(project) + documents.forEach { document -> + val file = manager.getFile(document) + if (file != null && file.isValid && !editors.isFileOpen(file)) manager.saveDocument(document) + } + } + + companion object { + /** Refuse pre-existing unsaved closed buffers: they were not inputs to the server proof. */ + fun capture( + project: Project, + edit: WorkspaceEdit, + ): ClosedRefactoringDocuments? { + val manager = FileDocumentManager.getInstance() + val editors = FileEditorManager.getInstance(project) + val uris = + edit.changes.orEmpty().keys + + edit.documentChanges + .orEmpty() + .filter { it.isLeft } + .map { it.left.textDocument.uri } + val documents = + uris.distinct().mapNotNull { uri -> + val file = LSPIJUtils.findResourceFor(uri) ?: return null + if (editors.isFileOpen(file)) return@mapNotNull null + val document = manager.getDocument(file) ?: return null + if (manager.isDocumentUnsaved(document)) return null + document + } + return ClosedRefactoringDocuments(project, documents) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt new file mode 100644 index 00000000000..525dce22f75 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt @@ -0,0 +1,28 @@ +package org.xtclang.idea.lsp + +import com.redhat.devtools.lsp4ij.JSONUtils +import org.eclipse.lsp4j.CodeAction +import org.eclipse.lsp4j.Command +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage + +// TODO LSP4IJ: UP11 — resolved action edits need a version-checked write command, not an undo-transparent +// action. Route selection through the supported client-command extension until upstream preserves +// import/member-generation Undo/Redo (X105/X122). Listing or resolving alone never applies edits. +internal object CodeActionMessages { + const val COMMAND = "xtc.resolveCodeAction" + + fun incoming(next: MessageConsumer): MessageConsumer = + MessageConsumer { message -> + val results = (message as? ResponseMessage)?.result as? List<*> + results.orEmpty().forEach { item -> + val action = (item as? Either<*, *>)?.right as? CodeAction + if (action?.data != null && action.edit == null && action.command == null) { + val original = JSONUtils.getLsp4jGson().toJsonTree(action) + action.command = Command(action.title, COMMAND, listOf(original)) + } + } + next.consume(message) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt new file mode 100644 index 00000000000..ae8cf19ed8b --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt @@ -0,0 +1,310 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import com.google.gson.JsonNull +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import com.intellij.execution.configurations.GeneralCommandLine +import com.intellij.execution.process.CapturingProcessHandler +import com.intellij.ide.trustedProjects.TrustedProjects +import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.progress.ProgressIndicator +import com.intellij.openapi.progress.ProgressManager +import com.intellij.openapi.progress.Task +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import org.eclipse.lsp4j.DidChangeConfigurationParams +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicReference + +/** Portable evaluated Gradle contract. Uses only Community platform APIs. */ +object CompilerBuildModel { + const val PATH = ".gradle/xtc/lsp-model.json" + + fun parse(text: String): JsonObject { + val model = JsonParser.parseString(text).asJsonObject + require(model["schemaVersion"]?.asInt == 1 && model["sourceSets"]?.isJsonArray == true) { + "Unsupported Gradle compiler model; refresh build configuration" + } + val entries = model["sourceSets"].asJsonArray.map { it.asJsonObject } + model["importId"]?.let { require(it.isJsonPrimitive && it.asJsonPrimitive.isString) { "Invalid Gradle import identity" } } + model["buildRoots"]?.let { roots -> + require(roots.isJsonArray && roots.asJsonArray.all { URI(it.asString).scheme == "file" }) { "Invalid Gradle build roots" } + } + entries.forEach { entry -> + listOf( + "projectId", + "projectPath", + "projectDirectory", + "buildFile", + "sourceSet", + "resourceTask", + ).forEach { field -> + require( + entry[field]?.let { + it.isJsonPrimitive && + it.asJsonPrimitive.isString && + it.asString.isNotBlank() + } == true, + ) { + "Invalid build-model $field" + } + } + listOf( + "sourceRoots", + "sourceFiles", + "moduleRoots", + "resourceSourceRoots", + "resourceRoots", + "modulePath", + ).forEach { field -> + require(entry[field]?.isJsonArray == true) { "Missing build-model $field" } + entry[field].asJsonArray.forEach { + require(URI(it.asString).scheme == "file") { + "Build-model paths must be file URIs" + } + } + } + listOf("buildFile", "projectDirectory").forEach { + require(URI(entry[it].asString).scheme == "file") + } + require(entry["projectDependencies"]?.isJsonArray == true) + require( + entry["projectDependencies"].asJsonArray.all { + it.isJsonPrimitive && it.asJsonPrimitive.isString + }, + ) + } + require( + entries.map { it["projectId"].asString to it["sourceSet"].asString }.distinct().size == + entries.size, + ) { + "Duplicate Gradle source-set owner" + } + return model + } + + fun read(project: Project): JsonObject? { + val imports = project.service().model + return runCatching { imports.current() } + .getOrElse { + logger().warn("Cannot read Gradle compiler inputs; retaining previous import", it) + imports.retained() + }?.let(::parse) + } + + fun settings( + project: Project, + current: Any?, + ): JsonObject { + val settings = + Gson().toJsonTree(current).takeIf { it.isJsonObject }?.asJsonObject ?: JsonObject() + val model = read(project) + val xtc = + settings["xtc"]?.takeIf { it.isJsonObject }?.asJsonObject + ?: JsonObject().also { settings.add("xtc", it) } + val compiler = + xtc["compiler"]?.takeIf { it.isJsonObject }?.asJsonObject + ?: JsonObject().also { xtc.add("compiler", it) } + if (!compiler.has("sourceModules")) compiler.add("sourceModules", JsonNull.INSTANCE) + compiler.add("buildModels", Gson().toJsonTree(listOfNotNull(model))) + return settings + } + + fun describe(project: Project): String { + val origin = + if (SourceGraphConfiguration.read(CompilerSettings.content(project)) != null) { + "Explicit project override (preserved across Gradle refresh)" + } else { + "Gradle model when imported; workspace conventions otherwise" + } + val imports = project.service().model + val options = LibraryConfiguration.read(CompilerSettings.content(project)) + val libraries = + buildList { + add("Bundled XDK: read-only, always included") + add( + if (options.modulePath == + null + ) { + "External libraries: inherited from Gradle" + } else { + "External libraries: explicit project/application override (ordered)" + }, + ) + options.modulePath.orEmpty().forEachIndexed { index, path -> add(" ${index + 1}. $path") } + options.sourceAttachments.forEach { attachment -> + add("Attached sources for ${attachment.module} (read-only navigation):") + attachment.roots.forEachIndexed { index, path -> add(" ${index + 1}. $path") } + } + }.joinToString("\n") + val status = imports.description() + "\n" + libraries + val model = + read(project) + ?: return "$origin\n$status\nNo Gradle model imported. Manual paths also work without a build file." + return origin + + "\n$status\n\n" + + model["sourceSets"].asJsonArray.joinToString("\n\n") { value -> + val entry = value.asJsonObject + buildList { + add( + "${entry["projectPath"].asString} / ${entry["sourceSet"].asString} [Gradle model]", + ) + add("Build: ${entry["buildFile"].asString}") + val missing = entry["resourceRoots"].asJsonArray.count { !Files.exists(Path.of(URI(it.asString))) } + add("Processed resources: " + if (missing == 0) "ready" else "$missing missing; prepare generated resources") + listOf("sourceRoots", "resourceSourceRoots", "resourceRoots", "modulePath") + .forEach { kind -> + entry[kind].asJsonArray.forEach { item -> + val uri = URI(item.asString) + add( + " $kind: $uri" + + if (Files.exists(Path.of(uri))) { + "" + } else { + " [missing; prepare generated inputs]" + }, + ) + } + } + }.joinToString("\n") + } + } + + fun refresh( + project: Project, + prepare: Boolean, + finished: (String?) -> Unit, + ) { + if (!TrustedProjects.isProjectTrusted(project)) { + finished("Trust this project before running its Gradle build. Previous compiler inputs retained.") + return + } + val service = project.service() + ProgressManager + .getInstance() + .run( + object : Task.Backgroundable( + project, + if (prepare) "Prepare Ecstasy compiler inputs" else "Refresh Ecstasy compiler paths", + true, + ) { + private val result = + AtomicReference( + CompilerImport.Result( + CompilerImport.Outcome.CANCELLED, + "Import cancelled; previous compiler configuration retained.", + ), + ) + + override fun run(indicator: ProgressIndicator) { + val completed = + try { + service.run(indicator) { + importModel(project, service.model, indicator, prepare) + } + } catch (cancelled: ProcessCanceledException) { + throw cancelled + } catch (failure: Exception) { + CompilerImport.Result(CompilerImport.Outcome.FAILED, failure.message ?: "Compiler import failed") + } + result.set(completed) + } + + override fun onSuccess() = complete() + + override fun onCancel() = complete() + + private fun complete() { + if (project.isDisposed) return + val completed = result.get() + if (completed.outcome == CompilerImport.Outcome.SUCCEEDED) publish(project) + finished(completed.message.takeUnless { completed.outcome == CompilerImport.Outcome.SUCCEEDED }) + } + }, + ) + } + + private fun importModel( + project: Project, + model: CompilerImport, + indicator: ProgressIndicator, + prepare: Boolean, + ): CompilerImport.Result { + // A malformed existing report must not prevent an explicit refresh from repairing it. + runCatching { model.current() } + val operation = model.begin(prepare) + val result = + try { + val roots = CompilerWorkspaceModels.roots(project) + val wrapperName = if (System.getProperty("os.name").startsWith("Windows")) "gradlew.bat" else "gradlew" + val builds = roots.filter { Files.isRegularFile(it.resolve(wrapperName)) } + require(builds.isNotEmpty()) { "No Gradle wrapper here; configure manual paths instead" } + indicator.text = if (prepare) "Preparing generated sources and resources" else "Reading evaluated Gradle inputs" + val outputs = + builds.asSequence().map { root -> + indicator.checkCanceled() + indicator.text2 = root.toString() + val command = + GeneralCommandLine( + root.resolve(wrapperName).toString(), + "--init-script", + CompilerWorkspaceModels.importScript().toString(), + if (prepare) "prepareEcstasyWorkspaceModel" else "exportEcstasyWorkspaceModel", + "--console=plain", + ).withWorkDirectory(root.toFile()).withEnvironment("XTC_COMPILER_IMPORT_ID", operation.id) + CapturingProcessHandler(command).runProcessWithProgressIndicator(indicator) + } + val output = outputs.firstOrNull { it.isCancelled || it.isTimeout || it.exitCode != 0 } + when { + output?.isCancelled == true || indicator.isCanceled -> { + model.finish(operation, CompilerImport.Outcome.CANCELLED) + } + + output != null -> { + model.finish( + operation, + CompilerImport.Outcome.FAILED, + "Gradle import failed; previous compiler configuration retained.\n" + + (output.stdout + output.stderr).takeLast(8000), + ) + } + + else -> { + model.finish(operation, CompilerImport.Outcome.SUCCEEDED, cancelled = { indicator.isCanceled }) + } + } + } catch (cancelled: ProcessCanceledException) { + model.finish(operation, CompilerImport.Outcome.CANCELLED) + throw cancelled + } catch (failure: Exception) { + model.finish(operation, CompilerImport.Outcome.FAILED, "${failure.message}; previous compiler configuration retained.") + } + if (result.outcome == CompilerImport.Outcome.CANCELLED) throw ProcessCanceledException() + return result + } + + fun publish(project: Project) { + if (project.isDisposed) return + // The evaluated inputs changed, but persisted user settings did not. LSP4IJ suppresses + // no-op settings updates, so notify the existing connection directly. + LanguageServiceAccessor + .getInstance(project) + .startedServers + .filter { it.serverDefinition.id == CompilerSettings.SERVER_ID } + .forEach { wrapper -> + wrapper.initializedServer.thenAccept { server -> + if (!project.isDisposed) { + val current = CompilerSettings.store(project).getLanguageServerSettings(CompilerSettings.SERVER_ID) + server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(settings(project, current?.getLanguageServerConfiguration(project))), + ) + } + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdates.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdates.kt new file mode 100644 index 00000000000..e5abd132270 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdates.kt @@ -0,0 +1,73 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.service +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskId +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskNotificationListener +import com.intellij.openapi.externalSystem.model.task.ExternalSystemTaskType +import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.ProjectActivity +import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.openapi.vfs.newvfs.BulkFileListener +import com.intellij.openapi.vfs.newvfs.events.VFileEvent +import org.jetbrains.plugins.gradle.settings.GradleSettingsListener +import java.nio.file.Files +import java.nio.file.Path + +/** Consume atomic exports without rewriting user settings or restarting a server. */ +class CompilerBuildUpdates : ProjectActivity { + override suspend fun execute(project: Project) { + project.messageBus.connect(project).subscribe( + GradleSettingsListener.TOPIC, + object : GradleSettingsListener { + override fun onProjectsUnlinked(linkedProjectPaths: Set) { + project.service().rootsRemoved() + ApplicationManager.getApplication().executeOnPooledThread { + if (!project.isDisposed) CompilerBuildModel.publish(project) + } + } + }, + ) + project.messageBus.connect(project).subscribe( + VirtualFileManager.VFS_CHANGES, + object : BulkFileListener { + override fun after(events: List) { + val roots = CompilerWorkspaceModels.roots(project) + if (events.none { event -> roots.any { affectsModel(it, Path.of(event.path)) } }) return + ApplicationManager.getApplication().executeOnPooledThread { + if (!project.isDisposed) CompilerBuildModel.publish(project) + } + } + }, + ) + } + + companion object { + internal fun affectsModel( + root: Path, + changed: Path, + ): Boolean = + listOf(CompilerBuildModel.PATH, CompilerWorkspaceModels.PATH).any { + root.resolve(it).normalize().startsWith(changed.normalize()) + } + } +} + +/** Community Gradle sync has a public completion event; failed/cancelled syncs do not refresh. */ +class CompilerGradleSync : ExternalSystemTaskNotificationListener { + override fun onSuccess( + projectPath: String, + id: ExternalSystemTaskId, + ) { + if (id.projectSystemId.id != "GRADLE" || id.type != ExternalSystemTaskType.RESOLVE_PROJECT) return + val project = id.findProject() ?: return + val root = Path.of(projectPath) + // Opt into automatic export after the first explicit import. Do not run arbitrary builds + // merely because a folder is open, and do not interpret a Gradle script to guess its tasks. + if (listOf(CompilerBuildModel.PATH, CompilerWorkspaceModels.PATH).none { Files.isRegularFile(root.resolve(it)) }) return + CompilerBuildModel.refresh(project, false) { failure -> + if (failure != null) logger().warn(failure) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt new file mode 100644 index 00000000000..dc30c9ab3a5 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt @@ -0,0 +1,143 @@ +package org.xtclang.idea.lsp + +import java.time.Instant +import java.util.UUID +import java.util.concurrent.atomic.AtomicReference + +/** Owns one import and the last accepted report, independently of a settings dialog or connection. */ +internal class CompilerImport( + private val read: () -> String?, + private val importId: (String) -> String? = { null }, + private val validate: (String) -> Unit, +) { + enum class Outcome { SUCCEEDED, CANCELLED, FAILED } + + class Operation( + val prepare: Boolean, + ) { + val id: String = UUID.randomUUID().toString() + } + + data class Result( + val outcome: Outcome, + val message: String, + val finished: Instant = Instant.now(), + ) + + private data class Observed( + val text: String?, + ) + + private data class State( + val accepted: String? = null, + val observed: Observed? = null, + val operation: Operation? = null, + val result: Result? = null, + val retired: Boolean = false, + ) + + private val state = AtomicReference(State()) + + /** Watcher/configuration reads cannot publish a report from a still-running or failed build. */ + fun current(): String? { + while (true) { + val before = state.get() + if (before.retired) return null + if (before.operation != null) return before.accepted + val text = read() + if (before.observed == Observed(text)) { + if (state.get() === before) return before.accepted + continue + } + // Malformed external reports leave the accepted snapshot intact and remain actionable. + text?.let(validate) + // Task cancellation may precede the Gradle daemon's final write. A tagged report + // can only become accepted through its own successful finish callback. + if (before.observed != null && text != null && importId(text) != null && text != before.accepted) { + if (state.get() === before) return before.accepted + continue + } + val after = before.copy(accepted = text, observed = Observed(text)) + if (state.compareAndSet(before, after)) return text + } + } + + fun retained(): String? = state.get().accepted + + fun retire() { + state.set(State(retired = true)) + } + + fun begin(prepare: Boolean): Operation { + while (true) { + val before = state.get() + check(!before.retired) { "Compiler import owner has been retired" } + check(before.operation == null) { "An Ecstasy compiler import is already running" } + val operation = Operation(prepare) + if (state.compareAndSet(before, before.copy(operation = operation))) return operation + } + } + + /** Called only after the owned process ends. A late callback cannot finish a newer import. */ + fun finish( + operation: Operation, + outcome: Outcome, + detail: String? = null, + cancelled: () -> Boolean = { false }, + ): Result { + val before = state.get() + if (before.retired) return Result(Outcome.CANCELLED, "Compiler import owner has been retired.") + check(before.operation === operation) { "Compiler import no longer owns this result" } + val observed = runCatching(read) + val validation = + runCatching { + val text = requireNotNull(observed.getOrThrow()) { "Gradle did not export an Ecstasy compiler model" } + validate(text) + require(importId(text)?.let { it == operation.id } != false) { "Gradle report belongs to a different import" } + text + } + val result = + when { + outcome == Outcome.CANCELLED || cancelled() -> { + Result( + Outcome.CANCELLED, + "Import cancelled; previous compiler configuration retained.", + ) + } + + outcome == Outcome.FAILED -> { + Result(outcome, detail ?: "Gradle import failed; previous compiler configuration retained.") + } + + validation.isFailure -> { + Result( + Outcome.FAILED, + "${validation.exceptionOrNull()?.message}; previous compiler configuration retained.", + ) + } + + else -> { + Result(Outcome.SUCCEEDED, "Ecstasy compiler inputs imported.") + } + } + val after = + before.copy( + accepted = if (result.outcome == Outcome.SUCCEEDED) validation.getOrThrow() else before.accepted, + observed = if (observed.isSuccess) Observed(observed.getOrNull()) else before.observed, + operation = null, + result = result, + ) + if (!state.compareAndSet(before, after)) { + if (state.get().retired) return Result(Outcome.CANCELLED, "Compiler import owner has been retired.") + error("Compiler import ownership changed") + } + return result + } + + fun description(): String { + val current = state.get() + return current.operation?.let { + if (it.prepare) "Preparing generated Ecstasy inputs…" else "Refreshing evaluated Ecstasy compiler paths…" + } ?: current.result?.let { "${it.message}\nLast import: ${it.finished}" } ?: "No compiler import run in this IDE session." + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt new file mode 100644 index 00000000000..cb909ecb560 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt @@ -0,0 +1,57 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.Disposable +import com.intellij.openapi.components.Service +import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.progress.ProgressIndicator +import com.intellij.openapi.project.Project +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +@Service(Service.Level.PROJECT) +internal class CompilerImportService( + private val project: Project, +) : Disposable { + private fun createModel() = + CompilerImport( + read = { CompilerWorkspaceModels.read(CompilerWorkspaceModels.roots(project)) }, + validate = { CompilerBuildModel.parse(it) }, + importId = { CompilerBuildModel.parse(it)["importId"]?.asString }, + ) + + private val owner = AtomicReference(createModel()) + val model: CompilerImport get() = owner.get() + + private val disposed = AtomicBoolean() + private val active = AtomicReference() + + fun rootsRemoved() { + if (disposed.get()) return + // Keep a retired owner distinct from its replacement: the old producer still holds it. + owner.getAndSet(createModel()).retire() + if (disposed.get()) model.retire() + active.get()?.cancel() + } + + fun run( + indicator: ProgressIndicator, + operation: () -> T, + ): T { + if (disposed.get()) throw ProcessCanceledException() + check(active.compareAndSet(null, indicator)) { "An Ecstasy compiler import is already running" } + try { + // Closing the project may have raced with claiming this operation. + if (disposed.get()) indicator.cancel() + indicator.checkCanceled() + return operation() + } finally { + active.compareAndSet(indicator, null) + } + } + + override fun dispose() { + disposed.set(true) + model.retire() + active.get()?.cancel() + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt new file mode 100644 index 00000000000..7dcb5427a84 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt @@ -0,0 +1,108 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.fileChooser.FileChooser +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory +import com.intellij.openapi.project.Project +import com.intellij.ui.components.JBCheckBox +import com.intellij.ui.components.JBLabel +import com.intellij.ui.components.JBScrollPane +import com.intellij.ui.table.JBTable +import java.awt.BorderLayout +import java.awt.FlowLayout +import java.nio.file.Path +import javax.swing.JButton +import javax.swing.JPanel +import javax.swing.table.DefaultTableModel + +internal class CompilerLibrariesPanel( + private val project: Project, +) : JPanel(BorderLayout(0, 8)) { + private val inherited = JBCheckBox("Inherit binary libraries from Gradle", true) + private val binaries = OrderedPaths(project, "Binary library paths", false) + private val rows = DefaultTableModel(arrayOf("Binary module name", "Attached source directory (ordered)"), 0) + private val attachments = JBTable(rows).apply { name = "Library source attachments" } + val editing: Boolean get() = attachments.isEditing + + init { + inherited.addItemListener { binaries.editable(!inherited.isSelected) } + binaries.editable(false) + add( + JPanel(BorderLayout(0, 4)).apply { + add( + JBLabel("Bundled XDK: always available, read-only. Attach sources for navigation; they are not compiler dependencies."), + BorderLayout.NORTH, + ) + add(inherited, BorderLayout.SOUTH) + }, + BorderLayout.NORTH, + ) + add( + JPanel(BorderLayout(0, 8)).apply { + add(binaries, BorderLayout.NORTH) + add(JBScrollPane(attachments), BorderLayout.CENTER) + }, + BorderLayout.CENTER, + ) + add( + JPanel(FlowLayout(FlowLayout.LEADING)).apply { + add(JButton("Reload applied libraries").apply { addActionListener { CompilerBuildModel.publish(project) } }) + add(JButton("Add attachment").apply { addActionListener { rows.addRow(arrayOf("", "")) } }) + add( + JButton("Choose source directory").apply { + addActionListener { + val row = attachments.selectedRow + if (row >= 0) { + FileChooser.chooseFile(FileChooserDescriptorFactory.createSingleFolderDescriptor(), project, null)?.let { + rows.setValueAt(Path.of(it.path).toUri().toString(), row, 1) + } + } + } + }, + ) + add( + JButton( + "Remove attachment", + ).apply { addActionListener { attachments.selectedRows.sortedDescending().forEach(rows::removeRow) } }, + ) + listOf("Move attachment up" to -1, "Move attachment down" to 1).forEach { (label, offset) -> + add( + JButton(label).apply { + addActionListener { + val from = attachments.selectedRow + val to = from + offset + if (from >= 0 && + to in 0 until rows.rowCount + ) { + rows.moveRow(from, from, to) + attachments.setRowSelectionInterval(to, to) + } + } + }, + ) + } + }, + BorderLayout.SOUTH, + ) + } + + fun options(): LibraryOptions = + LibraryOptions( + if (inherited.isSelected) null else binaries.paths, + (0 until rows.rowCount) + .map { row -> rows.getValueAt(row, 0).toString().trim() to rows.getValueAt(row, 1).toString().trim() } + .groupBy({ it.first }, { it.second }) + .map { (module, roots) -> LibraryAttachment(module, roots) }, + ) + + fun stopEditing() { + attachments.cellEditor?.stopCellEditing() + } + + fun reset(options: LibraryOptions) { + attachments.cellEditor?.cancelCellEditing() + inherited.isSelected = options.modulePath == null + binaries.reset(options.modulePath.orEmpty()) + rows.rowCount = 0 + options.sourceAttachments.forEach { attachment -> attachment.roots.forEach { rows.addRow(arrayOf(attachment.module, it)) } } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerProjectConfigurable.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerProjectConfigurable.kt new file mode 100644 index 00000000000..20343453a7a --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerProjectConfigurable.kt @@ -0,0 +1,476 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import com.google.gson.JsonParser +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.fileChooser.FileChooser +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.options.Configurable +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.DialogWrapper +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.ui.components.JBCheckBox +import com.intellij.ui.components.JBLabel +import com.intellij.ui.components.JBList +import com.intellij.ui.components.JBScrollPane +import com.intellij.ui.dsl.builder.AlignX +import com.intellij.ui.dsl.builder.panel +import com.intellij.ui.table.JBTable +import com.intellij.util.ui.JBUI +import com.intellij.util.xmlb.XmlSerializerUtil +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.settings.LanguageServerSettings.LanguageServerDefinitionSettings +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings +import java.awt.BorderLayout +import java.awt.GridLayout +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.atomic.AtomicLong +import javax.swing.JButton +import javax.swing.JComponent +import javax.swing.JPanel +import javax.swing.JTextArea +import javax.swing.ListSelectionModel +import javax.swing.table.DefaultTableModel + +/** Project-local source roots, backed by the same LSP4IJ settings used by rename and Undo. */ +class CompilerProjectConfigurable + @JvmOverloads + constructor( + private val project: Project, + private val readModules: (Project) -> CompletableFuture> = + ::effectiveModules, + ) : Configurable { + private val discovery = JBCheckBox("Use Gradle model or automatic source discovery") + private val effective = + JTextArea(12, 60).apply { + isEditable = false + name = "xtc.compiler.details" + } + private val status = JBLabel("No build model imported") + private val rows = + DefaultTableModel( + arrayOf( + "Module", + "Root URI or relative path", + "Dependencies (comma separated)", + "Resource roots (JSON array; blank = automatic)", + ), + 0, + ) + private val table = JBTable(rows).apply { name = "Ecstasy source modules" } + private val libraries = CompilerLibrariesPanel(project) + private var originalLibraries = LibraryOptions() + + // Settings dialogs have an explicit reset/apply lifecycle. This snapshot prevents a stale + // dialog from overwriting a graph changed by rename or another settings editor. + private val reportRevision = AtomicLong() + private var original: List? = null + + override fun getDisplayName(): String = "Ecstasy Compiler" + + override fun createComponent(): JComponent { + val add = + JButton("Add module").apply { + addActionListener { rows.addRow(arrayOf("", "", "", "")) } + } + val remove = + JButton("Remove module").apply { + addActionListener { table.selectedRows.sortedDescending().forEach(rows::removeRow) } + } + + fun updateEnabled() { + table.isEnabled = !discovery.isSelected + add.isEnabled = !discovery.isSelected + remove.isEnabled = !discovery.isSelected + } + discovery.addItemListener { updateEnabled() } + reset() + updateEnabled() + val sourceActions = + JPanel(GridLayout(0, 3, 8, 8)).apply { + add(add) + add(remove) + add( + JButton("Choose module root").apply { + addActionListener { + val row = table.selectedRow + if (row >= 0 && !discovery.isSelected) { + FileChooser + .chooseFile( + FileChooserDescriptorFactory.createSingleFileDescriptor( + "x", + ), + project, + null, + )?.let { + rows.setValueAt( + Path.of(it.path).toUri().toString(), + row, + 1, + ) + } + } + } + }, + ) + add( + JButton("Add resource directory").apply { + addActionListener { + val row = table.selectedRow + if (row >= 0 && !discovery.isSelected) { + FileChooser + .chooseFile( + FileChooserDescriptorFactory + .createSingleFolderDescriptor(), + project, + null, + )?.let { directory -> + val current = + runCatching { + modules()?.get(row)?.resourceRoots.orEmpty() + }.getOrNull() + if (current != null) { + rows.setValueAt( + Gson() + .toJson( + ( + current + + Path + .of(directory.path) + .toUri() + .toString() + ).distinct(), + ), + row, + 3, + ) + } + } + } + } + }, + ) + add( + JButton("Order resource directories…").apply { + addActionListener { + val row = table.selectedRow + if (row >= 0 && !discovery.isSelected) { + table.cellEditor?.stopCellEditing() + val current = modules()?.get(row)?.resourceRoots + val paths = OrderedPaths(project, "Resource directories", true).apply { reset(current.orEmpty()) } + val automatic = JBCheckBox("Use automatic resource directories", current == null) + val dialog = + object : DialogWrapper(project) { + init { + title = "Ecstasy Resource Directories" + init() + } + + override fun createCenterPanel(): JComponent = + JPanel(BorderLayout(0, 8)).apply { + add(automatic, BorderLayout.NORTH) + add(paths, BorderLayout.CENTER) + } + } + if (dialog.showAndGet()) { + rows.setValueAt( + if (automatic.isSelected) "" else Gson().toJson(paths.paths), + row, + 3, + ) + } + } + } + }, + ) + add( + JButton("Reset to build model").apply { + addActionListener { discovery.isSelected = true } + }, + ) + } + + fun updateSourceActions() { + sourceActions.components.filterIsInstance().forEach { + it.isEnabled = it.text == "Reset to build model" || !discovery.isSelected + } + } + discovery.addItemListener { updateSourceActions() } + updateSourceActions() + val pages = + linkedMapOf( + "Source modules" to + JPanel(BorderLayout(0, 12)).apply { + add( + JPanel(BorderLayout(0, 8)).apply { + add(discovery, BorderLayout.NORTH) + add( + JBLabel( + "Project settings · Apply updates the running compiler.
" + + "Automatic: use evaluated Gradle inputs or discover sources.
" + + "Manual: the table replaces automatic roots; an empty table disables discovery.", + ), + BorderLayout.CENTER, + ) + }, + BorderLayout.NORTH, + ) + add(JBScrollPane(table), BorderLayout.CENTER) + add(sourceActions, BorderLayout.SOUTH) + }, + "Libraries and sources" to libraries, + "Build import" to + panel { + group("Gradle inputs") { + row { button("Refresh Gradle model") { refreshBuild(false) } } + .rowComment("Read the build model without generating resources.") + row { button("Prepare generated resources") { refreshBuild(true) } } + .rowComment("Refresh the model and generate the compiler's resource inputs.") + row { button("Open build file") { openBuildFile() } } + row { comment("Your manual source and library overrides are preserved.") } + } + group("Import status") { + row { cell(status) } + } + collapsibleGroup("Effective paths and import details") { + row { scrollCell(effective).align(AlignX.FILL) } + } + }, + ) + // Keep each page's draft, but size the dialog to the visible page instead of a hidden + // table. The IDE Settings window manages its own bounds. + val content = JPanel(BorderLayout()) + val navigation = + JBList(*pages.keys.toTypedArray()).apply { + name = "xtc.compiler.navigation" + accessibleContext.accessibleName = "Compiler settings pages" + selectionMode = ListSelectionModel.SINGLE_SELECTION + visibleRowCount = pages.size + fixedCellHeight = JBUI.scale(32) + border = JBUI.Borders.empty(4) + addListSelectionListener { + if (!it.valueIsAdjusting) { + selectedValue?.let { selected -> + content.removeAll() + content.add(pages.getValue(selected), BorderLayout.CENTER) + content.revalidate() + content.repaint() + } + } + } + selectedIndex = 0 + } + return JPanel(BorderLayout(JBUI.scale(16), 0)).apply { + add(JBScrollPane(navigation), BorderLayout.WEST) + add(content, BorderLayout.CENTER) + } + } + + private fun openBuildFile() { + val selected = table.selectedRow + val modules = runCatching { modules() }.getOrNull().orEmpty() + val model = runCatching { CompilerBuildModel.read(project) }.getOrNull() + val entries = + model + ?.get("sourceSets") + ?.asJsonArray + ?.map { it.asJsonObject } + .orEmpty() + val root = modules.getOrNull(selected)?.uri + val entry = + entries.firstOrNull { item -> + item["sourceFiles"].asJsonArray.any { it.asString == root } + } ?: entries.firstOrNull() + entry?.get("buildFile")?.asString?.let { uri -> + LocalFileSystem.getInstance().refreshAndFindFileByNioFile(Path.of(URI(uri)))?.let { + FileEditorManager.getInstance(project).openFile(it, true) + } + } + } + + private fun showReport(text: String) { + effective.text = text + effective.caretPosition = 0 + val summary = text.lineSequence().firstOrNull(String::isNotBlank).orEmpty() + status.text = if (summary.length > 100) summary.take(100) + "…" else summary + status.toolTipText = summary + } + + private fun refreshBuild(prepare: Boolean) { + val revision = reportRevision.incrementAndGet() + showReport("Reading evaluated Gradle inputs…") + CompilerBuildModel.refresh(project, prepare) { failure -> + if (!project.isDisposed && reportRevision.get() == revision) { + if (failure != null) showReport(failure) else refreshEffectivePaths() + } + } + } + + override fun disposeUIResources() { + reportRevision.incrementAndGet() + } + + private fun refreshEffectivePaths() { + val revision = reportRevision.incrementAndGet() + val settings = CompilerSettings.content(project) + val serviceSettings = LanguageServiceSettings.effective(project) + val accessor = LanguageServiceAccessor.getInstance(project) + val connections = + accessor.startedServers + .filter { it.serverDefinition.id == CompilerSettings.SERVER_ID } + .associateWith { it.languageServer } + val description = + runCatching { + CompilerBuildModel.describe(project) + }.getOrElse { it.message.orEmpty() } + showReport(description) + readModules(project).thenAccept { modules -> + ApplicationManager.getApplication().invokeLater { + // Check the request's original owner at publication, including a restart + // between reply receipt and this EDT callback that preserves all settings. + if ( + !project.isDisposed && + reportRevision.get() == revision && + CompilerSettings.content(project) == settings && + LanguageServiceSettings.effective(project) == serviceSettings && + connections.all { (wrapper, server) -> + wrapper in accessor.startedServers && wrapper.languageServer === server + } + ) { + showReport( + description + + "\n\nEffective compiler modules:\n" + + modules.joinToString("\n") { + "${it.name}: ${it.uri}\n Resources: ${it.resourceRoots.orEmpty().joinToString()}\n Dependencies: ${it.dependencies.joinToString()}" + }, + ) + } + } + } + } + + private fun modules(): List? = + if (discovery.isSelected) { + null + } else { + (0 until rows.rowCount).map { row -> + fun cell(column: Int) = + rows + .getValueAt(row, column) + ?.toString() + .orEmpty() + .trim() + SourceModuleConfiguration( + cell(0), + cell(1), + cell(2).split(',').map(String::trim).filter(String::isNotEmpty), + cell(3).takeIf(String::isNotBlank)?.let { value -> + val paths = + runCatching { + JsonParser.parseString(value) + }.getOrElse { + throw IllegalArgumentException( + "Resource roots must be a JSON array of paths", + it, + ) + } + require(paths.isJsonArray) { + "Resource roots must be a JSON array of paths" + } + paths.asJsonArray.map { path -> + require(path.isJsonPrimitive && path.asJsonPrimitive.isString) { + "Resource roots must contain only paths" + } + path.asString + } + }, + ) + } + } + + override fun isModified(): Boolean = + table.isEditing || libraries.editing || + runCatching { modules() != original || libraries.options() != originalLibraries }.getOrDefault(true) + + override fun reset() { + table.cellEditor?.cancelCellEditing() + refreshEffectivePaths() + original = SourceGraphConfiguration.read(CompilerSettings.content(project)) + originalLibraries = LibraryConfiguration.read(CompilerSettings.content(project)) + libraries.reset(originalLibraries) + discovery.isSelected = original == null + table.isEnabled = !discovery.isSelected + rows.rowCount = 0 + original.orEmpty().forEach { + rows.addRow( + arrayOf( + it.name, + it.uri, + it.dependencies.joinToString(", "), + it.resourceRoots?.let { paths -> Gson().toJson(paths) }.orEmpty(), + ), + ) + } + } + + override fun apply() { + table.cellEditor?.stopCellEditing() + libraries.stopEditing() + try { + val content = CompilerSettings.content(project) + require(SourceGraphConfiguration.read(content) == original) { + "Compiler source graph changed while this dialog was open. Reset before applying." + } + require(LibraryConfiguration.read(content) == originalLibraries) { + "Compiler libraries changed while this dialog was open. Reset before applying." + } + val next = modules() + val nextLibraries = libraries.options() + val graph = + SourceGraphConfiguration.configure( + LanguageServiceSettings.content(project) ?: content, + next, + Path.of(requireNotNull(project.basePath)).toUri(), + ) + val replacement = LibraryConfiguration.configure(graph, nextLibraries, Path.of(requireNotNull(project.basePath)).toUri()) + val settings = + ProjectLanguageServerSettings + .getInstance(project) + .getLanguageServerSettings(CompilerSettings.SERVER_ID) + ?: CompilerSettings + .store(project) + .getLanguageServerSettings(CompilerSettings.SERVER_ID) + val copy = + settings?.let(XmlSerializerUtil::createCopy) ?: LanguageServerDefinitionSettings() + copy.configurationContent = replacement + ProjectLanguageServerSettings + .getInstance(project) + .updateSettings(CompilerSettings.SERVER_ID, copy) + original = next + originalLibraries = nextLibraries + } catch (failure: IllegalArgumentException) { + throw ConfigurationException(failure.message ?: "Invalid compiler source graph") + } + } + } + +/** Read asynchronous module data; the UI checks request ownership when publishing it. */ +private fun effectiveModules(project: Project): CompletableFuture> { + val accessor = LanguageServiceAccessor.getInstance(project) + val requests = + accessor.startedServers + .filter { it.serverDefinition.id == CompilerSettings.SERVER_ID } + .map { wrapper -> + wrapper.initializedServer.thenCompose { server -> + (server as? XtcLanguageServer)?.compilerSourceModules() + ?: CompletableFuture.completedFuture(emptyList()) + } + } + return CompletableFuture.allOf(*requests.toTypedArray()).thenApply { + requests.flatMap { it.join() } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt new file mode 100644 index 00000000000..da806f3d226 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt @@ -0,0 +1,199 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.Disposable +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VFileProperty +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.newvfs.NewVirtualFile +import com.intellij.util.concurrency.AppExecutorUtil +import com.redhat.devtools.lsp4ij.JSONUtils +import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions +import org.eclipse.lsp4j.Registration +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +/** One connection owns its watch leases; duplicate registrations share a native watch. */ +internal class CompilerRootWatches( + private val watch: (Set) -> AutoCloseable, + private val refresh: (Set) -> CompletableFuture, +) : AutoCloseable { + private data class State( + val registrations: Map> = emptyMap(), + val lease: AutoCloseable = AutoCloseable {}, + val closed: Boolean = false, + ) { + val roots = registrations.values.flatten().toSet() + } + + private var state = State() + private val refreshing = AtomicBoolean() + + @Synchronized + fun replace( + added: Map>, + removed: Set = emptySet(), + ) { + if (state.closed) return + val registrations = (state.registrations - removed) + added + val roots = registrations.values.flatten().toSet() + val lease = if (roots == state.roots) state.lease else watch(roots) + if (lease !== state.lease) state.lease.close() + state = State(registrations, lease) + } + + fun refresh() { + val roots = synchronized(this) { if (state.closed) emptySet() else state.roots } + if (roots.isEmpty() || !refreshing.compareAndSet(false, true)) return + try { + refresh(roots).whenComplete { _, _ -> refreshing.set(false) } + } catch (failure: RuntimeException) { + refreshing.set(false) + throw failure + } + } + + @Synchronized + override fun close() { + state.lease.close() + state = State(closed = true) + } + + companion object { + /** Only explicit compiler roots, never a workspace-wide wildcard or arbitrary URI. */ + fun roots(registration: Registration): Set { + if ( + registration.method != "workspace/didChangeWatchedFiles" || + !registration.id.startsWith("xtc-resources-") + ) { + return emptySet() + } + val options = + JSONUtils.toModel( + registration.registerOptions, + DidChangeWatchedFilesRegistrationOptions::class.java, + ) + return options.watchers + .mapNotNull { watcher -> + val pattern = watcher.globPattern + runCatching { + if (pattern.isRight) { + // Flat ancestor watches are handled by LSP4IJ. Only own recursive + // compiler roots here; never recursively refresh a broad ancestor. + if (pattern.right.pattern != "**/*") return@mapNotNull null + val base = pattern.right.baseUri + Path.of(URI(if (base.isLeft) base.left.uri else base.right)) + } else { + pattern.left + .takeIf { it.endsWith("/**/*") } + ?.removeSuffix("/**/*") + ?.let(Path::of) + ?.takeIf(Path::isAbsolute) + } + }.getOrNull() + }.toSet() + } + } +} + +// TODO LSP4IJ: UP05 — dynamic watcher registrations need owned VFS roots and refresh while focused. +// Remove this bridge when upstream covers unknown/missing external roots and disposal (X124). + +/** + * Native watching marks VFS entries dirty but does not refresh while the IDE stays focused. Refresh + * just these compiler roots on the shared scheduler. No new thread or OS watcher is created per + * root. Unknown directories must be loaded for VFS to emit child create events. + */ +internal class CompilerVfsWatches : Disposable { + private val files = LocalFileSystem.getInstance() + private val roots = + CompilerRootWatches( + watch = { paths -> + val parents = + paths.mapNotNull { path -> + generateSequence(path.parent) { it.parent } + .firstOrNull(Files::isDirectory) + ?.toString() + } + val requests = + files.replaceWatchedRoots(emptySet(), paths.map(Path::toString), parents) + AutoCloseable { files.removeWatchedRoots(requests) } + }, + refresh = ::refresh, + ) + private val timer = + AppExecutorUtil + .getAppScheduledExecutorService() + .scheduleWithFixedDelay( + { + runCatching(roots::refresh).onFailure { + logger().warn("Cannot refresh compiler paths", it) + } + }, + 0, + 2, + TimeUnit.SECONDS, + ) + + fun register(registrations: List) { + roots.replace( + registrations + .associate { it.id to CompilerRootWatches.roots(it) } + .filterValues { it.isNotEmpty() }, + ) + roots.refresh() + } + + fun unregister(ids: Set) = roots.replace(emptyMap(), ids) + + private fun refresh(paths: Set): CompletableFuture { + val directories = + paths.mapNotNull { path -> + // Missing generated roots are observed through their nearest existing parent. Never + // recurse into that ancestor (which can be a large unrelated directory). + val existing = generateSequence(path) { it.parent }.firstOrNull(Files::isDirectory) + existing?.let(files::refreshAndFindFileByNioFile)?.let { it to (existing == path) } + } + ReadAction.runBlocking { + directories.forEach { (file, recursive) -> + load(file, recursive) + // A still-missing nested root can have a different nearest ancestor next time. + // Probe only that directory's children until the recursive root exists. + if (!recursive) (file as? NewVirtualFile)?.markDirty() + } + } + val result = CompletableFuture() + val groups = directories.groupBy({ it.second }, { it.first }) + CompletableFuture + .allOf( + *groups + .map { (recursive, group) -> + CompletableFuture().also { done -> + files.refreshFiles(group, true, recursive) { done.complete(null) } + } + }.toTypedArray(), + ).whenComplete { _, error -> + if (error == null) result.complete(null) else result.completeExceptionally(error) + } + return result + } + + private fun load( + file: VirtualFile, + recursive: Boolean, + ) { + if (!file.isValid || !file.isDirectory || file.`is`(VFileProperty.SYMLINK)) return + val children = file.children + if (recursive) children.filter { it.isDirectory }.forEach { load(it, true) } + } + + override fun dispose() { + timer.cancel(false) + roots.close() + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerSettings.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerSettings.kt new file mode 100644 index 00000000000..451584d547e --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerSettings.kt @@ -0,0 +1,33 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParser +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.settings.GlobalLanguageServerSettings +import com.redhat.devtools.lsp4ij.settings.LanguageServerSettings +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings + +/** One settings owner for configuration requests, the project UI and guarded rename history. */ +internal object CompilerSettings { + const val SERVER_ID = "xtcLanguageServer" + + fun store( + project: Project, + serverId: String = SERVER_ID, + ): LanguageServerSettings = + ProjectLanguageServerSettings.getInstance(project).takeIf { + ownsGraph(it.getLanguageServerSettings(serverId)?.configurationContent) + } ?: GlobalLanguageServerSettings.getInstance() + + /** A new service-only override must not steal ownership of an inherited compiler graph. */ + internal fun ownsGraph(content: String?): Boolean = + content != null && + !runCatching { + val settings = JsonParser.parseString(content).asJsonObject + val xtc = settings.getAsJsonObject("xtc") + settings.keySet() == setOf("xtc") && + xtc != null && + xtc.keySet().all { it == "languageService" } + }.getOrDefault(false) + + fun content(project: Project): String? = store(project).getLanguageServerSettings(SERVER_ID)?.configurationContent +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerWorkspaceModels.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerWorkspaceModels.kt new file mode 100644 index 00000000000..32b75c1f90d --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerWorkspaceModels.kt @@ -0,0 +1,77 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import com.intellij.openapi.application.PathManager +import com.intellij.openapi.project.Project +import org.jetbrains.plugins.gradle.settings.GradleSettings +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.StandardCopyOption +import java.security.MessageDigest + +/** Aggregate evaluated reports, never source directories guessed from build-script text. */ +internal object CompilerWorkspaceModels { + const val PATH = ".gradle/xtc/lsp-workspace.json" + + fun roots(project: Project): List = + (listOfNotNull(project.basePath) + GradleSettings.getInstance(project).linkedProjectsSettings.map { it.externalProjectPath }) + .map { Path.of(it).toAbsolutePath().normalize() } + .distinct() + .sorted() + + fun read(roots: List): String? { + val reports = + roots.mapNotNull { root -> + listOf(PATH, CompilerBuildModel.PATH) + .map(root::resolve) + .firstOrNull(Files::isRegularFile) + ?.let { CompilerBuildModel.parse(Files.readString(it)) } + } + return reports.takeIf { it.isNotEmpty() }?.let(::merge)?.toString() + } + + fun merge(reports: List): JsonObject { + val entries = + reports + .flatMap { it["sourceSets"].asJsonArray.toList() } + .groupBy { it.asJsonObject["projectId"].asString to it.asJsonObject["sourceSet"].asString } + .values + .map { matches -> + require(matches.distinct().size == 1) { "Conflicting Gradle source-set ownership" } + matches.first() + } + return JsonObject().apply { + addProperty("schemaVersion", 1) + reports + .mapNotNull { it["importId"]?.asString } + .distinct() + .takeIf { it.isNotEmpty() } + ?.let { addProperty("importId", it.joinToString(",")) } + add("sourceSets", JsonArray().apply { entries.forEach(::add) }) + add( + "buildRoots", + JsonArray().apply { + reports.flatMap { it["buildRoots"]?.asJsonArray?.toList().orEmpty() }.distinct().forEach(::add) + }, + ) + } + } + + fun importScript(): Path { + val contents = requireNotNull(javaClass.getResourceAsStream("/compiler-model.init.gradle")).use { it.readAllBytes() } + val digest = MessageDigest.getInstance("SHA-256").digest(contents).joinToString("") { "%02x".format(it) } + val file = Path.of(PathManager.getSystemPath(), "ecstasy", "compiler-import", digest, "compiler-model.init.gradle") + Files.createDirectories(file.parent) + if (!Files.exists(file)) { + val temporary = Files.createTempFile(file.parent, "import-", ".gradle") + try { + Files.write(temporary, contents) + Files.move(temporary, file, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING) + } finally { + Files.deleteIfExists(temporary) + } + } + return file + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConfigurationJson.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConfigurationJson.kt new file mode 100644 index 00000000000..572a55829d1 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConfigurationJson.kt @@ -0,0 +1,39 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import com.google.gson.TypeAdapter +import com.google.gson.TypeAdapterFactory +import com.google.gson.reflect.TypeToken +import com.google.gson.stream.JsonReader +import com.google.gson.stream.JsonWriter +import org.eclipse.lsp4j.DidChangeConfigurationParams + +// TODO LSP4IJ: UP08 — preserve explicit null configuration fields in the client Gson configuration. +// Remove this adapter once reset-to-discovery survives a wire round trip without it. + +/** Explicit null settings reset configuration; Gson's normal omission would keep the old value. */ +internal object ConfigurationJson : TypeAdapterFactory { + override fun create( + gson: Gson, + type: TypeToken, + ): TypeAdapter? { + if (type.rawType != DidChangeConfigurationParams::class.java) return null + val delegate = gson.getDelegateAdapter(this, type) + return object : TypeAdapter() { + override fun read(input: JsonReader): T = delegate.read(input) + + override fun write( + output: JsonWriter, + value: T, + ) { + val previous = output.serializeNulls + output.serializeNulls = true + try { + delegate.write(output, value) + } finally { + output.serializeNulls = previous + } + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConnectionLifetime.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConnectionLifetime.kt index 79102c1d4ba..753499af8a8 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConnectionLifetime.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ConnectionLifetime.kt @@ -8,7 +8,11 @@ internal class ConnectionLifetime( private val startProcess: () -> Unit, private val stopProcess: () -> Unit, ) { - private enum class State { NEW, STARTED, STOPPED } + private enum class State { + NEW, + STARTED, + STOPPED, + } private val state = AtomicReference(State.NEW) @@ -16,7 +20,9 @@ internal class ConnectionLifetime( synchronized(state) { when (state.get()) { State.STOPPED -> { - throw CannotStartProcessException("XTC LSP connection was stopped before startup completed") + throw CannotStartProcessException( + "Ecstasy LSP connection was stopped before startup completed", + ) } State.STARTED -> { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt new file mode 100644 index 00000000000..feb922c0f28 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt @@ -0,0 +1,74 @@ +package org.xtclang.idea.lsp + +import com.redhat.devtools.lsp4ij.JSONUtils +import org.eclipse.lsp4j.CodeActionParams +import org.eclipse.lsp4j.Diagnostic +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.RelatedFullDocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import java.util.UUID + +// TODO LSP4IJ: UP07 — every full report rebuilds/cancels lazy fixes, even when diagnostic equality +// suppresses annotation refresh. Give each delivered full report a client-only data revision so +// annotations acquire the new fixes. Restore the server's exact data before a code-action request. +// Remove when upstream retains equivalent lazy fixes or refreshes their owning annotations. +internal object DiagnosticQuickFixes { + private data class Presentation( + val original: Any?, + val revision: UUID, + ) + + fun incoming(message: Message): Message { + val response = message as? ResponseMessage ?: return message + val report = response.result as? DocumentDiagnosticReport ?: return message + val full = report.left ?: return message + if (full.items.isEmpty()) return message + val gson = JSONUtils.getLsp4jGson() + val revision = UUID.randomUUID() + // TODO LSP4IJ: UP06 — the shared Gson lacks our related-report discriminator. Preserve + // already decoded companion reports instead of sending them through that decoder again. + val copy = + RelatedFullDocumentDiagnosticReport( + full.items.map { diagnostic -> + gson.fromJson(gson.toJsonTree(diagnostic), Diagnostic::class.java).apply { + data = Presentation(diagnostic.data, revision) + } + }, + ).apply { + resultId = full.resultId + relatedDocuments = full.relatedDocuments + } + return ResponseMessage().apply { + jsonrpc = response.jsonrpc + rawId = response.rawId + error = response.error + result = DocumentDiagnosticReport(copy) + } + } + + fun outgoing(message: Message): Message { + val request = message as? RequestMessage ?: return message + val params = request.params as? CodeActionParams ?: return message + if (params.context.diagnostics.none { it.data is Presentation }) return message + val gson = JSONUtils.getLsp4jGson() + val copy = gson.fromJson(gson.toJsonTree(params), CodeActionParams::class.java) + copy.context.diagnostics = + params.context.diagnostics.map { diagnostic -> + gson.fromJson(gson.toJsonTree(diagnostic), Diagnostic::class.java).apply { + data = + when (val payload = diagnostic.data) { + is Presentation -> payload.original + else -> payload + } + } + } + return RequestMessage().apply { + jsonrpc = request.jsonrpc + rawId = request.rawId + method = request.method + this.params = copy + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticReportJson.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticReportJson.kt new file mode 100644 index 00000000000..bfc33f8a5ea --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticReportJson.kt @@ -0,0 +1,40 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import com.google.gson.TypeAdapter +import com.google.gson.TypeAdapterFactory +import com.google.gson.reflect.TypeToken +import org.eclipse.lsp4j.FullDocumentDiagnosticReport +import org.eclipse.lsp4j.UnchangedDocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.json.adapters.EitherTypeAdapter +import org.eclipse.lsp4j.jsonrpc.messages.Either + +// TODO LSP4IJ: UP06 — remove this adapter when the bundled LSP4J correctly decodes relatedDocuments +// unions by kind. The underlying defect is in LSP4J 1.0.0; keep full/unchanged transport +// regressions. + +/** + * LSP4J 1.0.0 discriminates the outer diagnostic report, but not the Either values inside + * relatedDocuments. Both alternatives are JSON objects; use their protocol kind instead of letting + * the ambiguous default adapter terminate the connection's message reader. + */ +internal object DiagnosticReportJson : TypeAdapterFactory { + private val report = + object : + TypeToken>() {} + + override fun create( + gson: Gson, + type: TypeToken, + ): TypeAdapter? { + if (type != report) return null + @Suppress("UNCHECKED_CAST") // The exact parameterized TypeToken above establishes T. + return EitherTypeAdapter( + gson, + report, + { it.isJsonObject && it.asJsonObject["kind"]?.asString == "full" }, + { it.isJsonObject && it.asJsonObject["kind"]?.asString == "unchanged" }, + ) + as TypeAdapter + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt new file mode 100644 index 00000000000..c18c6fe1c61 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt @@ -0,0 +1,110 @@ +package org.xtclang.idea.lsp + +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DocumentDiagnosticParams +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.CancelParams +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage + +// TODO LSP4IJ: UP07 — send previousResultId on automatic pulls and preserve unchanged quick fixes. +// Remove this bridge once upstream owns the result lifecycle (diagnostic and quick-fix tests). + +/** + * LSP4IJ 0.21 omits previousResultId from automatic pulls. Repeating an identical full report + * replaces and cancels its lazy quick fixes without refreshing unchanged annotations. Use the + * protocol's unchanged report for repeated pulls of the same editor snapshot. State belongs to one + * connection; edits and editor replacement require a full report again. + */ +internal class DiagnosticResultMessages( + private val snapshot: (String) -> DocumentStartupMessages.Snapshot?, +) { + private data class Key( + val uri: String, + val identifier: String?, + ) + + private data class Pending( + val key: Key, + val snapshot: DocumentStartupMessages.Snapshot, + ) + + private data class Result( + val snapshot: DocumentStartupMessages.Snapshot, + val id: String, + ) + + private val lock = Any() + private val requests = mutableMapOf() + private val results = mutableMapOf() + + fun outgoing(next: MessageConsumer): MessageConsumer = + MessageConsumer { message -> + val request = message as? RequestMessage + val params = request?.params as? DocumentDiagnosticParams + val current = params?.textDocument?.uri?.let(snapshot) + val forwarded = + synchronized(lock) { + val close = (message as? NotificationMessage)?.params as? DidCloseTextDocumentParams + val cancelled = (message as? NotificationMessage)?.params as? CancelParams + cancelled?.let { requests.remove(it.id) } + if (close != null) { + val uri = close.textDocument.uri + results.keys.removeIf { it.uri == uri } + requests.values.removeIf { it.key.uri == uri } + } + // Explicit callers own their previous-result policy. Only the installed + // synchronizer's typed, automatic request is enriched here. + if (params == null || current == null || params.previousResultId != null) { + message + } else { + val key = Key(params.textDocument.uri, params.identifier) + requests[request.id] = Pending(key, current) + val previous = results[key]?.takeIf { it.snapshot == current }?.id + if (previous == null) { + message + } else { + RequestMessage().apply { + id = request.id + method = request.method + this.params = + DocumentDiagnosticParams(params.textDocument).apply { + identifier = params.identifier + previousResultId = previous + workDoneToken = params.workDoneToken + partialResultToken = params.partialResultToken + } + } + } + } + } + next.consume(DiagnosticQuickFixes.outgoing(forwarded)) + } + + fun incoming(next: MessageConsumer): MessageConsumer = + MessageConsumer { message -> + val response = message as? ResponseMessage + val pending = synchronized(lock) { response?.let { requests[it.id] } } + val current = pending?.key?.uri?.let(snapshot) + val report = response?.result as? DocumentDiagnosticReport + val id = report?.left?.resultId ?: report?.right?.resultId + if (pending != null && response != null) { + synchronized(lock) { + // Snapshot lookup runs outside this lock. Close/cancel may retire the request + // while it runs; only the still-owned request can seed the result cache. + if ( + requests.remove(response.id, pending) && + current == pending.snapshot && + response.error == null && + id != null + ) { + results[pending.key] = Result(pending.snapshot, id) + } + } + } + // Client callbacks may re-enter the transport; never invoke them under our lock. + next.consume(DiagnosticQuickFixes.incoming(message)) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt new file mode 100644 index 00000000000..b278d310a16 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt @@ -0,0 +1,101 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.fileEditor.FileEditorManager +import com.intellij.openapi.vfs.VfsUtilCore +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.newvfs.BulkFileListener +import com.intellij.openapi.vfs.newvfs.events.VFileEvent +import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent +import com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent +import com.intellij.psi.PsiManager +import com.redhat.devtools.lsp4ij.LanguageServerItem +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import java.net.URI +import java.util.concurrent.CompletableFuture + +// TODO LSP4IJ: UP19 — reconnect open descendants when their directory moves or is renamed. +// Upstream onFileRenameAfter handles only the event's exact file. Remove this bridge once X162 +// passes Move/Undo/Redo and subsequent unsaved edits without stale document connections. +internal class DirectoryDocumentMoves( + private val wrapper: LanguageServerWrapper, + private val disposed: () -> Boolean, +) : BulkFileListener { + override fun before(events: List) { + if (closed()) return + // VFS invokes this under the write lock, before VirtualFile paths change. Capture the old + // URI now; after the event the same VirtualFile already reports its new path. Only this + // server's documents participate; overlapping directory events retire each document once. + val files = descendants(events) { wrapper.openedDocuments.map { it.file } } + files.forEach { file -> wrapper.toUri(file)?.let(::disconnect) } + files.forEach(::reconnectLater) + } + + private fun closed() = disposed() || wrapper.isDisposed || wrapper.project.isDisposed + + private fun reconnectLater(file: VirtualFile) { + ApplicationManager.getApplication().invokeLater { + if (closed() || !file.isValid || !FileEditorManager.getInstance(wrapper.project).isFileOpen(file)) return@invokeLater + val uri = wrapper.toUri(file) ?: return@invokeLater + // Use the normal public connection API and current document buffer after the VFS + // transaction. Do not wait for didOpen or compilation while holding the IDE write lock. + val connection = + ReadAction.computeBlocking>?, RuntimeException> { + val psi = PsiManager.getInstance(wrapper.project).findFile(file) ?: return@computeBlocking null + LanguageServiceAccessor + .getInstance(wrapper.project) + .getLanguageServers(psi, { it.serverWrapper === wrapper }, { it.serverWrapper === wrapper }) + } ?: return@invokeLater + connection + .whenComplete { _, failure -> + ApplicationManager.getApplication().invokeLater repair@{ + if (closed()) return@repair + val open = file.isValid && FileEditorManager.getInstance(wrapper.project).isFileOpen(file) + if (!open || wrapper.toUri(file) != uri) { + // A rapid Undo/Redo or tab close can overtake asynchronous connection. + // Retire only that attempt's file, never a different file reusing its URI. + if (wrapper.getOpenedDocument(uri)?.file === file) disconnect(uri) + if (open) reconnectLater(file) + } else if (failure != null) { + logger.warn("Cannot reconnect moved Ecstasy document $uri", failure) + } + } + } + } + } + + private fun disconnect(uri: URI) { + // The wrapper owns map removal, synchronizer disposal, didClose and diagnostic cleanup. + // It exposes no public equivalent. Reflect only this method, never its maps or locks. + disconnectMethod.invoke(wrapper, uri, false) + } + + companion object { + private val logger = logger() + + internal fun descendants( + events: List, + openedFiles: () -> List, + ): List { + val directories = + events.mapNotNull { event -> + when (event) { + is VFileMoveEvent -> event.file + is VFilePropertyChangeEvent -> event.file.takeIf { event.propertyName == VirtualFile.PROP_NAME } + else -> null + }?.takeIf { it.isDirectory } + } + if (directories.isEmpty()) return emptyList() + return openedFiles().distinct().filter { file -> directories.any { VfsUtilCore.isAncestor(it, file, true) } } + } + + internal val disconnectMethod by lazy { + LanguageServerWrapper::class.java + .getDeclaredMethod("disconnect", URI::class.java, Boolean::class.javaPrimitiveType) + .apply { check(trySetAccessible()) { "LSP4IJ document disconnect is inaccessible" } } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt new file mode 100644 index 00000000000..22149559144 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt @@ -0,0 +1,275 @@ +package org.xtclang.idea.lsp + +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.FoldingRangeRequestParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import java.net.URI + +// TODO LSP4IJ: UP09 — serialize didOpen/change/close and reject folding responses for retired editors. +// Remove this transport bridge once upstream passes startup typing and close/reopen regressions. + +/** + * LSP4IJ 0.21 dispatches didOpen independently of its ordered change/close executor. Keep changes + * behind that open, and bind folding responses to the requesting editor incarnation. State belongs + * to one transport and uses the client's existing versions and document identities. + */ +internal class DocumentStartupMessages( + private val snapshot: (String) -> Snapshot?, +) { + data class Snapshot( + val owner: Any, + val stamp: Long, + val text: String, + ) + + private data class Opening( + val owner: Any, + val change: NotificationMessage, + ) + + private data class Fold( + val uri: String, + val snapshot: Snapshot?, + ) + + // The actual transmitted version, never a second counter. A closed entry retains only the + // version ceiling: LSP4IJ can reuse versions on reopen, making old server edits ambiguous. + private data class Sent( + val owner: Any?, + val version: Int, + val text: String, + val retiredVersion: Int = -1, + ) + + private val lock = Any() + private val opening = mutableMapOf() + private val opened = mutableMapOf() + private val folds = mutableMapOf() + + /** Prove that an incoming edit's version describes this live editor incarnation and text. */ + fun editSnapshot( + uri: String, + version: Int?, + ): Snapshot? { + val current = snapshot(uri) ?: return null + return synchronized(lock) { + val sent = opened[URI(uri)] ?: return@synchronized null + current.takeIf { + sent.owner === it.owner && + sent.text == it.text && + (version == null || (version == sent.version && version > sent.retiredVersion)) + } + } + } + + fun isCurrent( + uri: String, + version: Int?, + expected: Snapshot, + ): Boolean = editSnapshot(uri, version) == expected + + fun outgoing(next: MessageConsumer): MessageConsumer = + MessageConsumer { message -> + val params = (message as? NotificationMessage)?.params + val uri = + when (params) { + is DidOpenTextDocumentParams -> { + params.textDocument.uri + } + + is DidChangeTextDocumentParams -> { + params.textDocument.uri + } + + is DidCloseTextDocumentParams -> { + params.textDocument.uri + } + + else -> { + ((message as? RequestMessage)?.params as? FoldingRangeRequestParams) + ?.textDocument + ?.uri + } + } + // The snapshot provider must not acquire an IDE read action: VFS rename can hold + // the write lock while waiting for this transport to finish didOpen/didChange. + // Capture before our lock so document lookup never runs under the transport lock. + val current = uri?.let(snapshot) + synchronized(lock) { + when (params) { + is DidOpenTextDocumentParams -> { + open(message, params, current, next) + } + + is DidChangeTextDocumentParams -> { + change(message, params, current, next) + } + + is DidCloseTextDocumentParams -> { + close(params.textDocument.uri, current, next) + } + + else -> { + if (message is RequestMessage && message.params is FoldingRangeRequestParams) { + val uri = (message.params as FoldingRangeRequestParams).textDocument.uri + folds[message.id] = Fold(uri, current) + } + next.consume(message) + } + } + } + } + + fun incoming(next: MessageConsumer): MessageConsumer = + MessageConsumer { message -> + val fold = synchronized(lock) { (message as? ResponseMessage)?.let { folds.remove(it.id) } } + val accepted = + if (fold != null && snapshot(fold.uri) != fold.snapshot) { + ResponseMessage().apply { + id = (message as ResponseMessage).id + error = + ResponseError( + ResponseErrorCode.ContentModified, + "Document changed during folding", + null, + ) + } + } else { + message + } + // Completing a client request can re-enter the transport. Never do so under lock. + next.consume(accepted) + } + + private fun open( + message: NotificationMessage, + params: DidOpenTextDocumentParams, + current: Snapshot?, + next: MessageConsumer, + ) { + val uri = params.textDocument.uri + if (current == null) { + close(uri, current, next) + return + } + if (opened[URI(uri)]?.owner === current.owner) return + retire(uri, next) + val pending = opening.remove(uri)?.takeIf { it.owner === current.owner }?.change + // Edits before the synchronizer was attached have no didChange. Read the actual buffer + // when no later version is queued; otherwise preserve the client's version/text pairing. + val item = params.textDocument + val latest = pending?.params as? DidChangeTextDocumentParams + val content = latest?.contentChanges?.lastOrNull()?.text ?: current.text + val version = latest?.textDocument?.version ?: item.version + next.consume( + notification( + message.method, + DidOpenTextDocumentParams(TextDocumentItem(uri, item.languageId, version, content)), + ), + ) + opened[URI(uri)] = + Sent(current.owner, version, content, opened[URI(uri)]?.retiredVersion ?: -1) + } + + private fun change( + message: NotificationMessage, + params: DidChangeTextDocumentParams, + current: Snapshot?, + next: MessageConsumer, + ) { + val uri = params.textDocument.uri + if (current == null) return + if (opened[URI(uri)]?.owner === current.owner) { + val before = opened.getValue(URI(uri)) + if (params.textDocument.version <= before.version) return + val text = applyChanges(before.text, params.contentChanges) + next.consume(message) + opened[URI(uri)] = before.copy(version = params.textDocument.version, text = text) + } else { + retire(uri, next) + val previous = opening[uri]?.takeIf { it.owner === current.owner }?.change + // Full sync needs only the latest version, not every intermediate buffer copy. + val latest = + listOfNotNull(previous, message).maxBy { + (it.params as DidChangeTextDocumentParams).textDocument.version + } + opening[uri] = Opening(current.owner, latest) + } + } + + private fun close( + uri: String, + current: Snapshot?, + next: MessageConsumer, + ) { + // A delayed close cannot discard the reopened incarnation, even before its open arrives. + if ( + current != null && + (opened[URI(uri)]?.owner === current.owner || opening[uri]?.owner === current.owner) + ) { + return + } + retire(uri, next) + opening.remove(uri) + } + + private fun retire( + uri: String, + next: MessageConsumer, + ) { + val before = opened[URI(uri)] + if (before?.owner != null) { + opened[URI(uri)] = + Sent(null, before.version, "", maxOf(before.version, before.retiredVersion)) + next.consume( + notification( + "textDocument/didClose", + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ), + ) + } + } + + /** Reconstruct only the wire text, including sequential incremental patches and CRLF. */ + private fun applyChanges( + text: String, + changes: List, + ): String = + changes.fold(text) { content, change -> + val range = change.range + if (range == null) { + change.text + } else { + val breaks = Regex("\r\n|\r|\n").findAll(content).toList() + val starts = listOf(0) + breaks.map { it.range.last + 1 } + val ends = breaks.map { it.range.first } + content.length + + fun offset(at: Position): Int { + require(at.line in starts.indices && at.character >= 0) + return starts[at.line] + + at.character.coerceAtMost(ends[at.line] - starts[at.line]) + } + content.replaceRange(offset(range.start), offset(range.end), change.text) + } + } + + private fun notification( + method: String, + params: Any, + ): NotificationMessage = + NotificationMessage().apply { + this.method = method + this.params = params + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt new file mode 100644 index 00000000000..5e73950ad4e --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt @@ -0,0 +1,90 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.Disposable +import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.progress.ProgressManager +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiFile +import com.intellij.util.concurrency.AppExecutorUtil +import com.redhat.devtools.lsp4ij.LSPFileSupport +import com.redhat.devtools.lsp4ij.LSPIJUtils +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.internal.VirtualFileCancelChecker +import com.redhat.devtools.lsp4ij.internal.editor.CodeVisionEditorFeature +import com.redhat.devtools.lsp4ij.internal.editor.DeclarativeInlayHintsEditorFeature +import com.redhat.devtools.lsp4ij.internal.editor.EditorFeature +import com.redhat.devtools.lsp4ij.internal.editor.SemanticTokensEditorFeature +import java.util.concurrent.Callable +import java.util.concurrent.CompletableFuture + +/** One coalesced read/UI pass per feature and connection, independent of document count. */ +internal class EditorRefresh( + private val project: Project, + private val owner: Disposable, + private val isDisposed: () -> Boolean, +) { + enum class Feature { + LENSES, + HINTS, + TOKENS, + } + + private val renderers = + mapOf( + Feature.LENSES to CodeVisionEditorFeature(), + Feature.HINTS to DeclarativeInlayHintsEditorFeature(), + Feature.TOKENS to SemanticTokensEditorFeature(), + ) + + private data class Presentation( + val file: PsiFile, + val editor: Editor, + val updates: List, + ) + + // TODO LSP4IJ: UP27 — upstream schedules one NBRA per connected document on the global + // executor. Coalesce the whole connection/feature instead. Reuse its cache/rendering bridges + // until EditorFeatureManager offers a bounded batch API with lifecycle cancellation. + fun request( + wrapper: LanguageServerWrapper, + feature: Feature, + ): CompletableFuture { + if (isDisposed() || project.isDisposed || wrapper.isDisposed) return CompletableFuture.completedFuture(null) + val rendering: EditorFeature = renderers.getValue(feature) + ReadAction + .nonBlocking( + Callable { + wrapper.openedDocuments.flatMap { opened -> + ProgressManager.checkCanceled() + val file = opened.file + if (!file.isValid) return@flatMap emptyList() + val editors = LSPIJUtils.editorsForFile(file, project).filterNot { it.isDisposed } + if (editors.isEmpty()) return@flatMap emptyList() + val psi = LSPIJUtils.getPsiFile(file, project) ?: return@flatMap emptyList() + if (!rendering.shouldProcess(psi)) return@flatMap emptyList() + rendering.clearLSPCache(psi) + editors.map { editor -> + Presentation(psi, editor, buildList { rendering.collectUiRunnable(editor, psi, this) }) + } + } + }, + ).expireWith(owner) + .expireWith(project) + .expireWhen { isDisposed() || wrapper.isDisposed } + .coalesceBy(this, feature) + .finishOnUiThread(ModalityState.any()) { presentations -> + presentations.filter { it.file.isValid && !it.editor.isDisposed }.forEach { presentation -> + rendering.clearEditorCache(presentation.editor, project) + LSPFileSupport.getSupport(presentation.file).restartDaemonCodeAnalyzerWithDebounce( + VirtualFileCancelChecker(presentation.file.virtualFile), + ) + presentation.updates.forEach(Runnable::run) + } + }.submit(AppExecutorUtil.getAppExecutorService()) + // A refresh acknowledges scheduling, as upstream does. Coalescing a superseded pass must + // not turn a successful server refresh request into an RPC cancellation/error. + return CompletableFuture.completedFuture(null) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ExportServerLogsAction.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ExportServerLogsAction.kt new file mode 100644 index 00000000000..1eb29b9aad5 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ExportServerLogsAction.kt @@ -0,0 +1,75 @@ +package org.xtclang.idea.lsp + +import com.intellij.notification.Notification +import com.intellij.notification.NotificationType +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.fileChooser.FileChooserFactory +import com.intellij.openapi.fileChooser.FileSaverDescriptor +import com.intellij.openapi.project.DumbAwareAction +import com.intellij.openapi.project.Project +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.ServerStatus +import java.nio.file.Files +import java.nio.file.Path +import java.util.Base64 +import java.util.concurrent.CompletableFuture + +/** Export through the connected server, without guessing another project's process log paths. */ +class ExportServerLogsAction : DumbAwareAction() { + override fun getActionUpdateThread() = ActionUpdateThread.BGT + + override fun update(event: AnActionEvent) { + event.presentation.isEnabled = event.project != null + } + + override fun actionPerformed(event: AnActionEvent) { + val project = event.project ?: return + val destination = + FileChooserFactory + .getInstance() + .createSaveFileDialog( + FileSaverDescriptor( + "Export Ecstasy Server Logs", + "ZIP of bounded recent server logs, timing traces and status. May contain local paths and logged diagnostics.", + "zip", + ), + project, + ).save("ecstasy-server-logs.zip") ?: return + export(project, destination.file.toPath()).whenComplete { _, failure -> + ApplicationManager.getApplication().invokeLater { + if (!project.isDisposed) { + Notification( + "XTC Language Server", + "Ecstasy server logs", + if (failure == + null + ) { + "Saved ${destination.file.name}" + } else { + "Log export failed: ${failure.message}. See the Language Servers log." + }, + if (failure == null) NotificationType.INFORMATION else NotificationType.ERROR, + ).notify(project) + } + } + } + } + + companion object { + fun export( + project: Project, + destination: Path, + ): CompletableFuture { + val wrapper = + LanguageServiceAccessor.getInstance(project).startedServers.firstOrNull { + it.serverDefinition.id == CompilerSettings.SERVER_ID && it.serverStatus == ServerStatus.started + } + ?: return CompletableFuture.runAsync { project.getService(ServerSupportLogs::class.java).export(destination) } + return wrapper.initializedServer.thenCompose { (it as XtcLanguageServer).exportLogs() }.thenAcceptAsync { bundle -> + Files.write(destination, Base64.getDecoder().decode(requireNotNull(bundle["base64"]))) + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/FileMoveTargets.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/FileMoveTargets.kt new file mode 100644 index 00000000000..7c4463ece09 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/FileMoveTargets.kt @@ -0,0 +1,41 @@ +package org.xtclang.idea.lsp + +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path + +/** Reject ambiguous or occupied destinations before requesting proof and again before applying. */ +internal object FileMoveTargets { + /** A single-source move may also change its basename; batches retain individual names. */ + fun inDirectory( + sources: List, + directory: Path, + name: String? = null, + ): Map? { + if (name != null && ( + sources.size != 1 || name.isBlank() || name != name.trim() || + name in setOf(".", "..") || name.any { it == '/' || it == '\\' } + ) + ) { + return null + } + return sources.associateWith { directory.resolve(name ?: it.fileName.toString()) }.takeIf(::valid) + } + + fun valid(moves: Map): Boolean = + moves.isNotEmpty() && + moves.values.distinct().size == moves.size && + moves.all { (from, to) -> + from.isAbsolute && + to.isAbsolute && + from == from.normalize() && + to == to.normalize() && + Files.exists(from, NOFOLLOW_LINKS) && + !Files.isSymbolicLink(from) && + !Files.exists(to, NOFOLLOW_LINKS) && + Files.isDirectory(to.parent) && + !to.startsWith(from) && + moves.keys.none(to::startsWith) && + moves.values.none { it != to && to.startsWith(it) } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageAdapter.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageAdapter.kt new file mode 100644 index 00000000000..99d2f4dfff7 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageAdapter.kt @@ -0,0 +1,23 @@ +package org.xtclang.idea.lsp + +/** The bundled default remains available for development builds that choose their own backend. */ +internal enum class LanguageAdapter( + val setting: String, + private val label: String, +) { + DEFAULT("default", "Bundled default"), + COMPILER("compiler", "Ecstasy Compiler"), + TREE_SITTER("treesitter", "Tree-sitter"), + ; + + fun launchArguments(): List = if (this == DEFAULT) emptyList() else listOf("-Dxtc.lsp.adapter=$setting") + + override fun toString(): String = label + + companion object { + fun fromSetting(setting: String): LanguageAdapter = + requireNotNull(entries.find { it.setting == setting }) { + "Language adapter must be default, compiler or treesitter" + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt new file mode 100644 index 00000000000..e48006d120b --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt @@ -0,0 +1,212 @@ +package org.xtclang.idea.lsp + +import com.google.gson.GsonBuilder +import com.google.gson.JsonObject +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.options.Configurable +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.Project +import com.intellij.ui.components.JBCheckBox +import com.intellij.ui.components.JBLabel +import com.intellij.ui.components.JBScrollPane +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import java.awt.BorderLayout +import java.awt.GridLayout +import java.util.concurrent.atomic.AtomicLong +import javax.swing.JButton +import javax.swing.JComboBox +import javax.swing.JComponent +import javax.swing.JPanel +import javax.swing.JTextArea + +class LanguageServiceApplicationConfigurable : LanguageServiceConfigurable(null) + +class LanguageServiceProjectConfigurable( + project: Project, +) : LanguageServiceConfigurable(project) + +/** Only the dialog draft is mutable; applying publishes a validated immutable value. */ +open class LanguageServiceConfigurable( + private val project: Project?, +) : Configurable { + private val inherit = + JBCheckBox("Use application language-service defaults").apply { + name = "xtc.service.inherit" + } + private val synchronization = + JComboBox(arrayOf("full", "incremental")).apply { name = "xtc.service.sync" } + private val adapter = + JComboBox(LanguageAdapter.entries.toTypedArray()).apply { + name = "xtc.service.adapter" + toolTipText = "Compiler provides semantic analysis; Tree-sitter provides syntax-based features." + } + private val saving = JComboBox(arrayOf("editor", "server")).apply { name = "xtc.service.save" } + private val hints = JBCheckBox("Show Ecstasy inlay hints").apply { name = "xtc.service.hints" } + private val references = JBCheckBox("Show Ecstasy reference counts").apply { name = "xtc.service.references" } + private val report = + JTextArea(14, 70).apply { + isEditable = false + name = "xtc.service.effective" + } + private val reportRevision = AtomicLong() + private var original: JsonObject? = null + private var initial = LanguageServiceConfiguration() + + override fun getDisplayName(): String = if (project == null) "Ecstasy Language Service Defaults" else "Ecstasy Language Service" + + override fun createComponent(): JComponent { + inherit.addItemListener { updateEnabled() } + reset() + return JPanel(BorderLayout(0, 12)).apply { + add( + JPanel(GridLayout(0, 2, 8, 8)).apply { + if (project != null) { + add(inherit) + add(JBLabel("Project overrides are stored with LSP4IJ.")) + } + add(JBLabel("Language adapter (restarts automatically)")) + add(adapter) + add(JBLabel("Text synchronization (restarts automatically)")) + add(synchronization) + add(JBLabel("Save formatting owner (restart required)")) + add(saving) + add(hints) + add(JBLabel("Native IDE inlay controls still apply.")) + add(references) + add(JBLabel("Applies immediately; Run lenses remain available.")) + }, + BorderLayout.NORTH, + ) + add( + JBLabel( + "Full is the default. Incremental sends changed text; it does not enable incremental compilation.
" + + "Server save edits are unavailable in LSP4IJ. Use Tools → Actions on Save → Reformat code.
" + + "Indentation and the compiler wrapping margin are configured under Editor → Code Style → Ecstasy.
" + + "Compiler paths remain under Ecstasy Compiler. Trace controls remain in Language Servers; JVM and log limits are under Ecstasy Server Runtime and Logs.", + ), + BorderLayout.CENTER, + ) + add( + JPanel(BorderLayout()).apply { + add(JBScrollPane(report), BorderLayout.CENTER) + add( + JButton("Refresh effective configuration").apply { + addActionListener { refreshReport() } + }, + BorderLayout.SOUTH, + ) + }, + BorderLayout.SOUTH, + ) + } + } + + private fun refreshReport() { + val revision = reportRevision.incrementAndGet() + val gson = GsonBuilder().setPrettyPrinting().serializeNulls().create() + val savedPreferences = LanguageServiceSettings.effective(project) + val savedRuntime = ServerRuntimeSettings.getInstance().state + val prefix = + "Configured preferences (" + + ( + if (project == null) { + "application" + } else if (original == null) { + "inherited application defaults" + } else { + "project overrides with application defaults" + } + ) + + "):\n" + + gson.toJson(savedPreferences) + + "\n\nSaved machine runtime (applies after explicit restart):\n" + + gson.toJson(savedRuntime) + "\n\n" + report.text = prefix + "Open an Ecstasy source file to start its language service." + val owner = project ?: return + val wrapper = + LanguageServiceAccessor.getInstance(owner).startedServers.firstOrNull { + it.serverDefinition.id == CompilerSettings.SERVER_ID + } ?: return + val connection = wrapper.languageServer + wrapper.initializedServer + .thenCompose { (it as XtcLanguageServer).languageServiceStatus() } + .whenComplete { status, failure -> + ApplicationManager.getApplication().invokeLater { + if (!owner.isDisposed && reportRevision.get() == revision && + LanguageServiceSettings.effective(owner) == savedPreferences && + ServerRuntimeSettings.getInstance().state == savedRuntime && + wrapper in LanguageServiceAccessor.getInstance(owner).startedServers && + wrapper.languageServer === connection + ) { + report.text = + prefix + + if (failure == null) { + "Effective running service:\n" + gson.toJson(status) + } else { + "Service status unavailable: ${failure.message}. See the Language Servers log." + } + } + } + } + } + + override fun disposeUIResources() { + reportRevision.incrementAndGet() + } + + private fun updateEnabled() { + val editable = project == null || !inherit.isSelected + adapter.isEnabled = editable + synchronization.isEnabled = editable + // TODO LSP4IJ: UP02 — enable server save formatting when native willSaveWaitUntil is implemented. + saving.isEnabled = false + saving.toolTipText = + "LSP4IJ does not implement server save edits. Use native Actions on Save." + hints.isEnabled = editable + references.isEnabled = editable + } + + private fun draft() = + LanguageServiceConfiguration( + synchronization.selectedItem as String, + saving.selectedItem as String, + hints.isSelected, + references.isSelected, + (adapter.selectedItem as LanguageAdapter).setting, + ) + + override fun isModified(): Boolean = + (project != null && inherit.isSelected != (original == null)) || + ((project == null || !inherit.isSelected) && draft() != initial) + + override fun reset() { + original = LanguageServiceConfiguration.section(LanguageServiceSettings.content(project)) + initial = LanguageServiceSettings.effective(project) + adapter.selectedItem = LanguageAdapter.fromSetting(initial.adapter) + inherit.isSelected = original == null + synchronization.selectedItem = initial.textSynchronization + saving.selectedItem = initial.saveFormatting + hints.isSelected = initial.inlayHints + references.isSelected = initial.referenceCodeLens + updateEnabled() + refreshReport() + } + + override fun apply() { + try { + val replacement = draft().takeUnless { project != null && inherit.isSelected } + val content = + LanguageServiceConfiguration.replace( + LanguageServiceSettings.content(project), + original, + replacement, + ) + LanguageServiceSettings.install(project, content) + reset() + } catch (failure: IllegalArgumentException) { + throw ConfigurationException( + failure.message ?: "Invalid Ecstasy language-service settings", + ) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfiguration.kt new file mode 100644 index 00000000000..db1fd672297 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfiguration.kt @@ -0,0 +1,144 @@ +package org.xtclang.idea.lsp + +import com.google.gson.GsonBuilder +import com.google.gson.JsonObject +import com.google.gson.JsonParser + +/** Immutable language-service preferences; source graphs remain owned by CompilerSettings. */ +internal data class LanguageServiceConfiguration( + val textSynchronization: String = "full", + val saveFormatting: String = "editor", + val inlayHints: Boolean = true, + val referenceCodeLens: Boolean = true, + val adapter: String = "default", +) { + init { + LanguageAdapter.fromSetting(adapter) + require(textSynchronization in setOf("full", "incremental")) { + "Text synchronization must be full or incremental" + } + require(saveFormatting in setOf("editor", "server")) { + "Save formatting must be owned by the editor or server" + } + } + + fun requiresRestart(previous: LanguageServiceConfiguration): Boolean = + adapter != previous.adapter || textSynchronization != previous.textSynchronization + + fun initializationOptions(nativeFormatOnSave: Boolean = false): Map = + mapOf( + "xtcDocumentSync" to + mapOf( + "incremental" to (textSynchronization == "incremental"), + "formatOnSave" to (saveFormatting == "server" && !nativeFormatOnSave), + ), + ) + + companion object { + private val gson = GsonBuilder().setPrettyPrinting().serializeNulls().create() + + private fun objectValue(content: String?): JsonObject = + if (content.isNullOrBlank()) { + JsonObject() + } else { + runCatching { JsonParser.parseString(content).asJsonObject } + .getOrElse { + throw IllegalArgumentException( + "Language-service settings must contain a JSON object", + it, + ) + } + } + + fun section(content: String?): JsonObject? = + objectValue(content) + .get("xtc") + ?.takeUnless { it.isJsonNull } + ?.let { xtc -> + require(xtc.isJsonObject) { "Ecstasy settings must be an object" } + xtc.asJsonObject + .get("languageService") + ?.takeUnless { it.isJsonNull } + ?.let { + require(it.isJsonObject) { + "Language-service settings must be an object" + } + it.asJsonObject.deepCopy() + } + } + + fun read( + global: String?, + project: String? = null, + ): LanguageServiceConfiguration { + val defaults = section(global) + val override = section(project) + + fun value(name: String) = override?.get(name) ?: defaults?.get(name) + + fun string( + name: String, + fallback: String, + ): String = + value(name)?.let { + require(it.isJsonPrimitive && it.asJsonPrimitive.isString) { + "$name must be a string" + } + it.asString + } ?: fallback + + fun boolean(name: String): Boolean = + value(name)?.let { + require(it.isJsonPrimitive && it.asJsonPrimitive.isBoolean) { + "$name must be a boolean" + } + it.asBoolean + } ?: true + return LanguageServiceConfiguration( + string("textSynchronization", "full"), + string("saveFormatting", "editor"), + boolean("inlayHints"), + boolean("referenceCodeLens"), + string("adapter", "default"), + ) + } + + /** Change only the selected adapter, retaining inherited preferences and compiler paths. */ + fun selectAdapter( + content: String?, + adapter: LanguageAdapter, + ): String { + val service = section(content) ?: JsonObject() + service.addProperty("adapter", adapter.setting) + val settings = objectValue(content) + val xtc = + settings["xtc"]?.takeUnless { it.isJsonNull }?.asJsonObject + ?: JsonObject().also { settings.add("xtc", it) } + xtc.add("languageService", service) + return gson.toJson(settings) + } + + /** + * Compare only our section, then merge into current content to preserve graph/Undo edits. + */ + fun replace( + content: String?, + expected: JsonObject?, + replacement: LanguageServiceConfiguration?, + ): String { + require(section(content) == expected) { + "Language-service settings changed while this page was open. Reset before applying." + } + val settings = objectValue(content) + val xtc = + settings["xtc"]?.takeUnless { it.isJsonNull }?.asJsonObject + ?: JsonObject().also { settings.add("xtc", it) } + if (replacement == null) { + xtc.remove("languageService") + } else { + xtc.add("languageService", gson.toJsonTree(replacement)) + } + return gson.toJson(settings) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServicePreferences.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServicePreferences.kt new file mode 100644 index 00000000000..335cd024d6c --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServicePreferences.kt @@ -0,0 +1,24 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.components.Service +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.project.Project +import java.util.concurrent.atomic.AtomicReference + +/** Invalid external JSON must not replace a live connection's last valid preferences. */ +@Service(Service.Level.PROJECT) +internal class LanguageServicePreferences( + private val project: Project, +) { + private val current = AtomicReference(LanguageServiceConfiguration()) + + fun read(): LanguageServiceConfiguration = + runCatching { + LanguageServiceSettings.effective(project) + }.onSuccess(current::set) + .getOrElse { + logger() + .warn("Invalid Ecstasy service settings; retaining previous values", it) + current.get() + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceSettings.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceSettings.kt new file mode 100644 index 00000000000..fc1a17db418 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceSettings.kt @@ -0,0 +1,33 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.project.Project +import com.intellij.util.xmlb.XmlSerializerUtil +import com.redhat.devtools.lsp4ij.settings.GlobalLanguageServerSettings +import com.redhat.devtools.lsp4ij.settings.LanguageServerSettings +import com.redhat.devtools.lsp4ij.settings.LanguageServerSettings.LanguageServerDefinitionSettings +import com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings + +/** Reuse the LSP4IJ persistence and notification owner for both settings scopes. */ +internal object LanguageServiceSettings { + fun store(project: Project?): LanguageServerSettings = + project?.let(ProjectLanguageServerSettings::getInstance) + ?: GlobalLanguageServerSettings.getInstance() + + fun content(project: Project?): String? = store(project).getLanguageServerSettings(CompilerSettings.SERVER_ID)?.configurationContent + + fun effective(project: Project?): LanguageServiceConfiguration = + LanguageServiceConfiguration.read(content(null), project?.let(::content)) + + fun validated(project: Project): LanguageServiceConfiguration = project.getService(LanguageServicePreferences::class.java).read() + + fun install( + project: Project?, + content: String, + ) { + val store = store(project) + val current = store.getLanguageServerSettings(CompilerSettings.SERVER_ID) + val copy = current?.let(XmlSerializerUtil::createCopy) ?: LanguageServerDefinitionSettings() + copy.configurationContent = content + store.updateSettings(CompilerSettings.SERVER_ID, copy) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt new file mode 100644 index 00000000000..b69c8bb09d3 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt @@ -0,0 +1,110 @@ +package org.xtclang.idea.lsp + +import com.google.gson.GsonBuilder +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path + +internal data class LibraryOptions( + val modulePath: List? = null, + val sourceAttachments: List = emptyList(), +) + +internal data class LibraryAttachment( + val module: String, + val roots: List, +) + +/** Library settings share the existing compiler owner and preserve unrelated graph/service fields. */ +internal object LibraryConfiguration { + private val gson = GsonBuilder().serializeNulls().setPrettyPrinting().create() + + fun read(content: String?): LibraryOptions = + content + ?.takeIf(String::isNotBlank) + ?.let { + JsonParser + .parseString(it) + .asJsonObject + .getAsJsonObject("xtc") + ?.getAsJsonObject("compiler") + ?.get("libraries") + }?.takeUnless { it.isJsonNull } + ?.let { element -> + require(element.isJsonObject) { "Libraries must be an object" } + val value = element.asJsonObject + + fun paths(raw: JsonElement): List { + require(raw.isJsonArray) { "Library paths must be an array" } + return raw.asJsonArray.map { path -> + require(path.isJsonPrimitive && path.asJsonPrimitive.isString) { "Library paths must be strings" } + path.asString + } + } + LibraryOptions( + value["modulePath"]?.takeUnless { it.isJsonNull }?.let(::paths), + value["sourceAttachments"] + ?.asJsonArray + ?.map { attachment -> + val entry = attachment.asJsonObject + LibraryAttachment(entry["module"].asString, paths(entry["roots"])) + }.orEmpty(), + ) + } ?: LibraryOptions() + + fun configure( + content: String?, + options: LibraryOptions, + base: URI, + ): String { + fun validate( + paths: List, + sources: Boolean, + ) { + val resolved = + paths.map { text -> + require(text.isNotBlank()) { "Library paths must not be blank" } + val uri = base.resolve(URI.create(text)) + require(uri.scheme == "file" && uri.query == null && uri.fragment == null) { "Library paths must be local files" } + val path = + Path + .of(uri) + .toFile() + .canonicalFile + .toPath() + require( + if (sources) { + Files.isDirectory(path) + } else { + Files.isDirectory(path) || + (Files.isRegularFile(path) && text.endsWith(".xtc")) + }, + ) { + "${if (sources) "Source directory" else "Library file or directory"} does not exist: $text" + } + path + } + require(resolved.distinct().size == resolved.size) { "Duplicate library path" } + } + options.modulePath?.let { validate(it, false) } + require( + options.sourceAttachments + .map { + it.module + }.distinct() + .size == options.sourceAttachments.size, + ) { "Duplicate source attachment module" } + options.sourceAttachments.forEach { + require(it.module.isNotBlank() && it.roots.isNotEmpty()) { "Attachments need a module name and source roots" } + validate(it.roots, true) + } + val settings = content?.takeIf(String::isNotBlank)?.let { JsonParser.parseString(it).asJsonObject } ?: JsonObject() + val xtc = settings.getAsJsonObject("xtc") ?: JsonObject().also { settings.add("xtc", it) } + val compiler = xtc.getAsJsonObject("compiler") ?: JsonObject().also { xtc.add("compiler", it) } + compiler.add("libraries", gson.toJsonTree(options)) + return gson.toJson(settings) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/OrderedPaths.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/OrderedPaths.kt new file mode 100644 index 00000000000..263542fb493 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/OrderedPaths.kt @@ -0,0 +1,103 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.fileChooser.FileChooser +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory +import com.intellij.openapi.project.Project +import com.intellij.ui.components.JBList +import com.intellij.ui.components.JBScrollPane +import java.awt.BorderLayout +import java.awt.Component +import java.awt.Container +import java.awt.FlowLayout +import java.nio.file.Path +import javax.swing.DefaultListModel +import javax.swing.JButton +import javax.swing.JPanel +import javax.swing.JTextField + +/** EDT-owned settings draft. Order is visible and empty is distinct from inherited defaults. */ +internal class OrderedPaths( + project: Project, + title: String, + directoriesOnly: Boolean, +) : JPanel(BorderLayout(0, 4)) { + private val pathModel = DefaultListModel() + private val list = + JBList(pathModel).apply { + name = title + visibleRowCount = 5 + } + private val input = JTextField(28).apply { name = "$title path" } + val paths: List get() = (0 until pathModel.size()).map(pathModel::get) + + init { + name = title + add(JBScrollPane(list), BorderLayout.CENTER) + add( + JPanel(FlowLayout(FlowLayout.LEADING)).apply { + add(input) + add( + JButton("Add path").apply { + addActionListener { + if (input.text.isNotBlank()) { + pathModel.addElement(input.text.trim()) + input.text = + "" + } + } + }, + ) + add( + JButton("Choose…").apply { + addActionListener { + val descriptor = + if (directoriesOnly) { + FileChooserDescriptorFactory.createSingleFolderDescriptor() + } else { + FileChooserDescriptorFactory.createSingleFileOrFolderDescriptor() + } + FileChooser + .chooseFile( + descriptor, + project, + null, + )?.let { pathModel.addElement(Path.of(it.path).toUri().toString()) } + } + }, + ) + add(JButton("Remove").apply { addActionListener { list.selectedIndices.sortedDescending().forEach(pathModel::remove) } }) + listOf("Up" to -1, "Down" to 1).forEach { (label, offset) -> + add( + JButton(label).apply { + addActionListener { + val from = list.selectedIndex + val to = from + offset + if (from >= 0 && + to in 0 until pathModel.size() + ) { + val value = pathModel.remove(from) + pathModel.add(to, value) + list.selectedIndex = to + } + } + }, + ) + } + }, + BorderLayout.SOUTH, + ) + } + + fun editable(value: Boolean) { + fun update(component: Component) { + component.isEnabled = value + if (component is Container) component.components.forEach(::update) + } + update(this) + } + + fun reset(paths: List) { + pathModel.clear() + paths.forEach(pathModel::addElement) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt new file mode 100644 index 00000000000..99e956c994f --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt @@ -0,0 +1,69 @@ +package org.xtclang.idea.lsp + +import org.eclipse.lsp4j.FileRename +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.jsonrpc.Endpoint +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CompletableFuture + +/** Connection-local, lexically scoped proof for the synchronous VFS portion of a guarded edit. */ +internal class PreflightedRenames { + private data class Move( + val from: Path, + val to: Path, + ) + + private val applying = ScopedValue.newInstance() + + fun apply( + from: Path, + to: Path, + change: () -> Unit, + ) { + ScopedValue.where(applying, Move(from, to)).run(change) + } + + // TODO LSP4IJ: UP03 — VFS preflight blocks the EDT even for an already approved edit. + // Its Move listener also sends oldPath -> oldPath. Those requests need no server edit. + // Keep real unowned renames and all notifications intact; remove when upstream accepts + // transaction ownership and correct move targets without waiting under the write lock. + fun endpoint(delegate: Endpoint): Endpoint = + object : Endpoint { + override fun request( + method: String, + parameter: Any?, + ): CompletableFuture<*> = + if (method == "workspace/willRenameFiles" && handled(parameter)) { + CompletableFuture.completedFuture(null) + } else { + delegate.request(method, parameter) + } + + override fun notify( + method: String, + parameter: Any?, + ) = delegate.notify(method, parameter) + } + + private fun handled(parameter: Any?): Boolean { + val files = (parameter as? RenameFilesParams)?.files ?: return false + if (files.isEmpty()) return false + return files.all { file -> + val move = localMove(file) ?: return@all false + move.from == move.to || (applying.isBound && move == applying.get()) + } + } + + private fun localMove(file: FileRename): Move? = + try { + // Path equality ignores a directory's optional trailing slash and file:/ vs file:///. + // Do not resolve symlinks, fold case or normalize '..': different spellings can carry + // meaning, and non-local URIs belong to the server's normal validation path. + val from = URI.create(file.oldUri) + val to = URI.create(file.newUri) + if (from.scheme != "file" || to.scheme != "file") null else Move(Path.of(from), Path.of(to)) + } catch (_: IllegalArgumentException) { + null + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SelectLanguageAdapterAction.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SelectLanguageAdapterAction.kt new file mode 100644 index 00000000000..6e168eeaf60 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SelectLanguageAdapterAction.kt @@ -0,0 +1,32 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.project.DumbAwareAction +import com.intellij.openapi.ui.popup.JBPopupFactory + +/** The existing settings listener owns restarting and reopening the project's documents. */ +class SelectLanguageAdapterAction : DumbAwareAction() { + override fun getActionUpdateThread(): ActionUpdateThread = ActionUpdateThread.BGT + + override fun update(event: AnActionEvent) { + event.presentation.isEnabled = event.project != null + } + + override fun actionPerformed(event: AnActionEvent) { + val project = event.project ?: return + val selected = LanguageAdapter.fromSetting(LanguageServiceSettings.validated(project).adapter) + JBPopupFactory + .getInstance() + .createPopupChooserBuilder(LanguageAdapter.entries) + .setTitle("Ecstasy Language Adapter (restarts server)") + .setSelectedValue(selected, true) + .setItemChosenCallback { adapter -> + if (!project.isDisposed && LanguageServiceSettings.effective(project).adapter != adapter.setting) { + val content = LanguageServiceConfiguration.selectAdapter(LanguageServiceSettings.content(project), adapter) + LanguageServiceSettings.install(project, content) + } + }.createPopup() + .showInBestPositionFor(event.dataContext) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt new file mode 100644 index 00000000000..71d756a80aa --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt @@ -0,0 +1,54 @@ +package org.xtclang.idea.lsp + +/** A bounded set of tuning flags; process entry points, agents and protocol properties stay owned by the launcher. */ +internal object ServerJvmOptions { + private val memory = Regex("(-Xms|-Xmx|-Xss|-XX:MaxMetaspaceSize=|-XX:ReservedCodeCacheSize=)([1-9][0-9]*)([kKmMgG])") + private val collector = Regex("-XX:\\+Use(G1|Serial|Parallel|Z)GC") + + fun validate(options: List): List { + val keys = + options.map { option -> + val size = memory.matchEntire(option) + when { + size != null -> { + size.groupValues[1] + } + + collector.matches(option) -> { + "collector" + } + + Regex("-XX:ActiveProcessorCount=[1-9][0-9]*").matches(option) -> { + require(option.substringAfter('=').toIntOrNull() in 1..1024) { "ActiveProcessorCount must be between 1 and 1024" } + "processors" + } + + else -> { + throw IllegalArgumentException( + "Unsupported JVM option: $option. Use heap/stack sizes, MaxMetaspaceSize, ReservedCodeCacheSize, ActiveProcessorCount or a supported collector.", + ) + } + } + } + require(keys.distinct().size == keys.size) { "Duplicate or conflicting JVM options" } + + fun bytes(prefix: String): Long? = + options.firstOrNull { it.startsWith(prefix) }?.let { + val match = requireNotNull(memory.matchEntire(it)) + val multiplier = + when (match.groupValues[3].lowercase()) { + "k" -> 1024L + "m" -> 1024L * 1024 + else -> 1024L * 1024 * 1024 + } + val count = match.groupValues[2].toLongOrNull() + require(count != null && count <= Long.MAX_VALUE / multiplier) { "JVM memory size is too large" } + count * multiplier + } + options.filter { memory.matches(it) }.forEach { bytes(requireNotNull(memory.matchEntire(it)).groupValues[1]) } + val initial = bytes("-Xms") + val maximum = bytes("-Xmx") + require(initial == null || maximum == null || initial <= maximum) { "Initial heap must not exceed maximum heap" } + return options.toList() + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeConfigurable.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeConfigurable.kt new file mode 100644 index 00000000000..5557bc9c987 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeConfigurable.kt @@ -0,0 +1,107 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.options.Configurable +import com.intellij.openapi.options.ConfigurationException +import com.intellij.openapi.project.ProjectManager +import com.intellij.ui.components.JBLabel +import com.intellij.ui.components.JBScrollPane +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import java.awt.BorderLayout +import java.awt.GridLayout +import javax.swing.JButton +import javax.swing.JComponent +import javax.swing.JPanel +import javax.swing.JSpinner +import javax.swing.JTextArea +import javax.swing.SpinnerNumberModel + +/** Editing a draft never restarts an active compiler. Restart is an explicit separate action. */ +class ServerRuntimeConfigurable : Configurable { + private val options = JTextArea(8, 65).apply { name = "xtc.runtime.vmOptions" } + private val history = JSpinner(SpinnerNumberModel(7, 1, 90, 1)).apply { name = "xtc.logs.historyDays" } + private val fileSize = JSpinner(SpinnerNumberModel(10, 1, 100, 1)).apply { name = "xtc.logs.maxFileMb" } + private val totalSize = JSpinner(SpinnerNumberModel(50, 1, 1000, 1)).apply { name = "xtc.logs.totalSizeMb" } + private val sessions = JSpinner(SpinnerNumberModel(5, 1, 20, 1)).apply { name = "xtc.logs.retainedSessions" } + private var original = ServerRuntimeSettings.Options() + + override fun getDisplayName(): String = "Ecstasy Server Runtime and Logs" + + override fun createComponent(): JComponent = + JPanel(BorderLayout(0, 8)).apply { + add( + JBLabel( + "Machine-local settings; uses IntelliJ's bundled Java runtime. Apply saves for the next server start.
One JVM option per line: -Xms/-Xmx/-Xss sizes, -XX:MaxMetaspaceSize, -XX:ReservedCodeCacheSize,
-XX:ActiveProcessorCount, or -XX:+UseG1GC/UseSerialGC/UseParallelGC/UseZGC. Sizes use K, M or G.
Example: -Xmx2G. No shell quoting, agents, classpath or system-property overrides.", + ), + BorderLayout.NORTH, + ) + add( + JPanel(BorderLayout(0, 8)).apply { + add(JBScrollPane(options), BorderLayout.CENTER) + add( + JPanel(GridLayout(0, 2, 8, 8)).apply { + add(JBLabel("Log history (days)")) + add(history) + add(JBLabel("Each log file (MB)")) + add(fileSize) + add(JBLabel("Rolled archive cap per stream (MB)")) + add(totalSize) + add(JBLabel("Retired process sessions to keep")) + add(sessions) + add(JBLabel("Active processes are protected. Old sessions are pruned on server start.")) + add(JBLabel("Tools → Export Ecstasy Server Logs saves bounded recent logs and status.")) + }, + BorderLayout.SOUTH, + ) + }, + BorderLayout.CENTER, + ) + add( + JButton("Restart running Ecstasy servers with saved settings").apply { + name = "xtc.runtime.restart" + addActionListener { + ServerRuntimeSettings.getInstance().launchArguments() + ProjectManager.getInstance().openProjects.filterNot { it.isDisposed }.forEach { project -> + LanguageServiceAccessor + .getInstance(project) + .startedServers + .filter { + it.serverDefinition.id == + CompilerSettings.SERVER_ID + }.forEach { it.restart() } + } + } + }, + BorderLayout.SOUTH, + ) + reset() + } + + private fun draft() = + original.copy( + vmOptions = options.text, + logHistoryDays = history.value as Int, + logMaxFileMb = fileSize.value as Int, + logTotalSizeMb = totalSize.value as Int, + logRetainedSessions = sessions.value as Int, + ) + + override fun isModified(): Boolean = draft() != original + + override fun reset() { + original = ServerRuntimeSettings.getInstance().state + options.text = original.vmOptions + history.value = original.logHistoryDays + fileSize.value = original.logMaxFileMb + totalSize.value = original.logTotalSizeMb + sessions.value = original.logRetainedSessions + } + + override fun apply() { + try { + ServerRuntimeSettings.getInstance().install(original, draft()) + reset() + } catch (failure: IllegalArgumentException) { + throw ConfigurationException(failure.message ?: "Invalid Ecstasy JVM options") + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt new file mode 100644 index 00000000000..96beaa11602 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt @@ -0,0 +1,62 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.components.RoamingType +import com.intellij.openapi.components.SerializablePersistentStateComponent +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.State +import com.intellij.openapi.components.Storage +import com.intellij.util.xmlb.annotations.OptionTag + +/** Application-owned and excluded from Settings Sync and project files. */ +@Service(Service.Level.APP) +@State(name = "EcstasyServerRuntime", storages = [Storage(value = "ecstasy-server-runtime.xml", roamingType = RoamingType.DISABLED)]) +internal class ServerRuntimeSettings : SerializablePersistentStateComponent(Options()) { + data class Options( + // Explicit bindings preserve immutable fields in IntelliJ's XML serializer. + @field:OptionTag @JvmField val vmOptions: String = "", + @field:OptionTag @JvmField val logHistoryDays: Int = 7, + @field:OptionTag @JvmField val logMaxFileMb: Int = 10, + @field:OptionTag @JvmField val logTotalSizeMb: Int = 50, + @field:OptionTag @JvmField val logRetainedSessions: Int = 5, + ) { + fun arguments(): List = + ServerJvmOptions.validate( + vmOptions + .lineSequence() + .map(String::trim) + .filter(String::isNotEmpty) + .toList(), + ) + } + + fun launchArguments(): List = state.let { it.arguments() + it.logArguments() } + + fun install( + expected: Options, + replacement: Options, + ) { + replacement.arguments() + replacement.logArguments() + updateState { + require(it == expected) { "Runtime settings changed while this page was open. Reset before applying." } + replacement + } + } + + companion object { + fun getInstance(): ServerRuntimeSettings = ApplicationManager.getApplication().getService(ServerRuntimeSettings::class.java) + } +} + +internal fun ServerRuntimeSettings.Options.logArguments(): List { + require(logHistoryDays in 1..90 && logMaxFileMb in 1..100 && logTotalSizeMb in logMaxFileMb..1000 && logRetainedSessions in 1..20) { + "Log retention: days 1–90, file MB 1–100, archive MB at least file MB and at most 1000, retired sessions 1–20" + } + return listOf( + "-Dxtc.logs.historyDays=$logHistoryDays", + "-Dxtc.logs.maxFileMb=$logMaxFileMb", + "-Dxtc.logs.totalSizeMb=$logTotalSizeMb", + "-Dxtc.logs.retainedSessions=$logRetainedSessions", + ) +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt new file mode 100644 index 00000000000..61e050051a3 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt @@ -0,0 +1,159 @@ +package org.xtclang.idea.lsp + +import com.google.gson.GsonBuilder +import com.intellij.openapi.components.SerializablePersistentStateComponent +import com.intellij.openapi.components.Service +import com.intellij.openapi.components.State +import com.intellij.openapi.components.Storage +import com.intellij.openapi.components.StoragePathMacros +import com.intellij.util.xmlb.annotations.OptionTag +import java.nio.ByteBuffer +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path +import java.nio.file.StandardOpenOption.READ +import java.time.Instant +import java.util.UUID +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream + +/** Last launch for this project, persisted locally across IDE restarts, never a shared settings file. */ +@Service(Service.Level.PROJECT) +@State(name = "EcstasySupportLogs", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)]) +internal class ServerSupportLogs : SerializablePersistentStateComponent(Session()) { + data class Session( + @field:OptionTag @JvmField val id: String = "", + @field:OptionTag @JvmField val started: String = "", + @field:OptionTag @JvmField val launcher: String = "No Ecstasy server launch recorded.", + @field:OptionTag @JvmField val directory: String = "", + @field:OptionTag @JvmField val traceDirectory: String = "", + ) + + fun begin(description: String): String = + UUID.randomUUID().toString().also { id -> + updateState { Session(id, Instant.now().toString(), description.takeLast(LAUNCH_LIMIT)) } + } + + fun append( + id: String, + text: String, + ) { + updateState { if (it.id == id) it.copy(launcher = (it.launcher + text).takeLast(LAUNCH_LIMIT)) else it } + } + + fun process( + id: String, + pid: Long, + ) { + val started = ProcessHandle.of(pid).flatMap { it.info().startInstant() }.orElse(null) ?: return + val root = + System.getProperty("xtc.logs.directory") + ?: System.getenv("XTC_LSP_LOG_DIR") + ?: "${System.getProperty("user.home")}/.xtc/logs/lsp" + val directory = Path.of(root).resolve("server-$pid-${started.toEpochMilli()}").toString() + val traces = System.getProperty("xtc.trace.directory") ?: System.getenv("XTC_LSP_TRACE_DIR") ?: directory + updateState { if (it.id == id) it.copy(directory = directory, traceDirectory = traces) else it } + } + + fun export(destination: Path) = archive(state, destination) + + companion object { + private const val LAUNCH_LIMIT = 64 * 1024 + private const val FILE_LIMIT = 512 * 1024 + private const val FILE_COUNT = 8 + private val sessionName = Regex("server-([0-9]+-[0-9]+)") + private val logName = Regex("(?:server(?:\\.[0-9.-]+)?\\.log|lsp-trace-[0-9.-]+\\.jsonl)") + + /** Export only a recorded connection's files; pruning may remove files while reading them. */ + internal fun archive( + session: Session, + destination: Path, + ) { + val process = + session.directory + .takeIf(String::isNotBlank) + ?.let { sessionName.matchEntire(Path.of(it).fileName.toString()) } + ?.groupValues + ?.get(1) + val files = + if (process == null) { + emptyList() + } else { + listOf(session.directory, session.traceDirectory) + .distinct() + .flatMap { directory -> + val root = Path.of(directory) + if (!Files.isDirectory(root, NOFOLLOW_LINKS)) { + emptyList() + } else { + Files.list(root).use { paths -> + paths + .filter { path -> + val name = path.fileName.toString() + Files.isRegularFile(path, NOFOLLOW_LINKS) && + if (directory == session.directory) { + logName.matches(name) + } else { + name.startsWith("lsp-trace-$process") && name.endsWith(".jsonl") + } + }.toList() + } + } + }.sortedByDescending { it.fileName.toString() } + .take(FILE_COUNT) + } + ZipOutputStream(Files.newOutputStream(destination)).use { zip -> + fun entry( + name: String, + bytes: ByteArray, + ) { + zip.putNextEntry(ZipEntry(name)) + zip.write(bytes) + zip.closeEntry() + } + val included = + files.mapIndexedNotNull { index, path -> + val tail = + runCatching { + Files.newByteChannel(path, setOf(READ, NOFOLLOW_LINKS)).use { channel -> + val size = channel.size() + val bytes = ByteBuffer.allocate(minOf(size, FILE_LIMIT.toLong()).toInt()) + channel.position((size - FILE_LIMIT).coerceAtLeast(0)) + while (bytes.hasRemaining() && channel.read(bytes) >= 0) { /* bounded tail */ } + size to bytes.array().copyOf(bytes.position()) + } + }.getOrNull() ?: return@mapIndexedNotNull null + val name = "$index-${path.fileName}" + entry(name, tail.second) + mapOf( + "entry" to name, + "originalBytes" to tail.first, + "includedBytes" to tail.second.size, + "truncated" to (tail.first > tail.second.size), + ) + } + val launcherBytes = session.launcher.toByteArray() + entry("launcher.log", launcherBytes.copyOfRange((launcherBytes.size - LAUNCH_LIMIT).coerceAtLeast(0), launcherBytes.size)) + entry( + "manifest.json", + GsonBuilder() + .setPrettyPrinting() + .create() + .toJson( + mapOf( + "schemaVersion" to 1, + "offline" to true, + "started" to session.started, + "directory" to session.directory, + "files" to included, + "scope" to ( + "Last launch recorded by this project; bounded launcher stderr and server log tails. " + + "No source files or IDE protocol consoles." + ), + ), + ).toByteArray(), + ) + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt new file mode 100644 index 00000000000..8c63928f209 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt @@ -0,0 +1,209 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.editor.Document +import com.intellij.openapi.vfs.VirtualFile +import com.redhat.devtools.lsp4ij.LSPIJUtils +import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextEdit +import org.eclipse.lsp4j.jsonrpc.messages.Either +import java.net.URI + +/** Preflight every target, then check the same document incarnations inside one undo command. */ +internal class ServerWorkspaceEdit + private constructor( + private val features: XtcClientFeatures, + private val ownership: DocumentStartupMessages, + private val server: Any, + private val label: String, + private val targets: List, + ) { + private data class Replacement( + val start: Int, + val end: Int, + val text: String, + ) + + private data class Target( + val uri: String, + val version: Int?, + val file: VirtualFile, + val document: Document, + val stamp: Long, + val snapshot: DocumentStartupMessages.Snapshot?, + val replacements: List, + ) { + fun current( + features: XtcClientFeatures, + ownership: DocumentStartupMessages, + ): Boolean = + file.isValid && + features.getFileUri(file) == URI(uri) && + file.isWritable && + document.isWritable && + document.modificationStamp == stamp && + ( + if (snapshot == null) { + features.serverWrapper.getOpenedDocument(features.getFileUri(file)) == null + } else { + ownership.isCurrent(uri, version, snapshot) + } + ) + } + + fun apply(): ApplyWorkspaceEditResponse = + WriteCommandAction + .writeCommandAction(features.project) + .withName(label) + .withGlobalUndo() + .compute { + if ( + features.project.isDisposed || + features.documents.get() !== ownership || + features.languageServer !== server || + !targets.all { it.current(features, ownership) } + ) { + refused("The connection or an edited document changed before application") + } else { + targets.forEach { target -> + target.replacements.asReversed().forEach { + target.document.replaceString(it.start, it.end, it.text) + } + } + ApplyWorkspaceEditResponse(true) + } + } + + companion object { + fun prepare( + features: XtcClientFeatures, + params: ApplyWorkspaceEditParams, + ): ServerWorkspaceEdit { + val ownership = + requireNotNull(features.documents.get()) { "Connection is not initialized" } + val server = requireNotNull(features.languageServer) { "Connection is not running" } + val edit = params.edit + require( + edit.changeAnnotations + .orEmpty() + .values + .none { it.needsConfirmation == true }, + ) { + "This edit requires confirmation; use the editor's refactoring action" + } + val changes = edit.documentChanges + val requests = + if (changes != null) { + changes.map { change -> + require(change.isLeft) { + "Use the guarded Ecstasy Rename/Move action for resource operations" + } + val document = change.left + Triple( + document.textDocument.uri, + document.textDocument.version, + textEdits(document.edits), + ) + } + } else { + edit.changes.orEmpty().map { (uri, edits) -> Triple(uri, null, edits) } + } + require(requests.map { URI(it.first) }.distinct().size == requests.size) { + "Duplicate document edits" + } + val targets = + requests.map { (uri, version, edits) -> + val file = + requireNotNull(features.findFileByUri(uri)) { + "Edit target is unavailable: $uri" + } + require(file.isValid && file.isWritable) { "Edit target is read-only: $uri" } + val document = + requireNotNull(LSPIJUtils.getDocument(file)) { + "Edit target has no document: $uri" + } + require(document.isWritable) { "Edit target is read-only: $uri" } + val opened = features.serverWrapper.getOpenedDocument(features.getFileUri(file)) + val snapshot = ownership.editSnapshot(uri, version) + require(if (opened != null) snapshot != null else version == null) { + "Edit version cannot be verified for this document incarnation: $uri" + } + Target( + uri, + version, + file, + document, + document.modificationStamp, + snapshot, + replacements(document, edits), + ) + } + return ServerWorkspaceEdit( + features, + ownership, + server, + params.label ?: "Ecstasy workspace edit", + targets, + ) + } + + private fun replacements( + document: Document, + edits: List, + ): List { + fun offset(at: Position): Int { + require(at.line in 0 until document.lineCount && at.character >= 0) { + "Invalid edit position" + } + val start = document.getLineStartOffset(at.line) + require(at.character <= document.getLineEndOffset(at.line) - start) { + "Edit exceeds line bounds" + } + val offset = start + at.character + val text = document.immutableCharSequence + require( + offset == 0 || + offset == text.length || + !Character.isSurrogatePair(text[offset - 1], text[offset]), + ) { + "Edit splits a UTF-16 surrogate pair" + } + return offset + } + val replacements = + edits + .map { + Replacement(offset(it.range.start), offset(it.range.end), it.newText) + .also { edit -> + require(edit.start <= edit.end) { "Reversed edit range" } + } + }.sortedWith(compareBy({ it.start }, { it.end })) + require( + replacements.zipWithNext().all { (a, b) -> a.end <= b.start && a.start != b.start }, + ) { + "Overlapping edits" + } + return replacements + } + + fun refused(reason: String) = ApplyWorkspaceEditResponse(false).apply { failureReason = reason } + + // IntelliJ's platform API declares List; LSP4IJ 0.21 also supplies LSP4J 1.0's + // List>. Its own compatibility adapter accepts both. + // Normalize at this boundary without unchecked casts or replacing platform libraries. + internal fun textEdits(edits: List<*>): List = + edits.map { edit -> + requireNotNull( + when (edit) { + is TextEdit -> edit + is Either<*, *> -> if (edit.isLeft) edit.left as? TextEdit else null + else -> null + }, + ) { + "Unsupported or malformed text edit (including snippet edits)" + } + } + } + } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt new file mode 100644 index 00000000000..2be8d31dde2 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt @@ -0,0 +1,185 @@ +package org.xtclang.idea.lsp + +import com.google.gson.GsonBuilder +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import com.google.gson.JsonParseException +import com.google.gson.JsonParser +import java.net.URI + +/** Pure settings transformation, shared by initial application, Undo and Redo. */ +internal object SourceGraphConfiguration { + private val gson = GsonBuilder().setPrettyPrinting().serializeNulls().create() + + /** A missing or null graph selects discovery; an empty array is explicitly empty. */ + fun read(content: String?): List? { + val settings = parse(content) + val compiler = child(settings, "xtc")?.let { child(it, "compiler") } ?: return null + val modules = compiler["sourceModules"]?.takeUnless { it.isJsonNull } ?: return null + require(modules.isJsonArray) { "Source modules must be an array" } + return modules.asJsonArray.map { module -> + val entry = objectValue(module) + val dependencies = entry["dependencies"]?.takeUnless { it.isJsonNull } + require(dependencies == null || dependencies.isJsonArray) { + "Source dependencies must be an array" + } + SourceModuleConfiguration( + stringValue(entry["name"]), + stringValue(entry["uri"]), + dependencies?.asJsonArray?.map(::stringValue).orEmpty(), + resourceRoots(entry), + ) + } + } + + /** Preserve unrelated LSP4IJ settings, including artifact and formatting configuration. */ + fun configure( + content: String?, + modules: List?, + base: URI, + ): String { + modules?.let { + it.forEach { module -> + require( + module.name.isNotBlank() && + module.uri.isNotBlank() && + module.dependencies.all(String::isNotBlank) && + module.resourceRoots.orEmpty().all(String::isNotBlank), + ) { + "Source module names, roots and dependencies must be non-blank strings" + } + } + canonical(it, base) + } + val settings = parse(content) + val xtc = child(settings, "xtc") ?: JsonObject().also { settings.add("xtc", it) } + val compiler = child(xtc, "compiler") ?: JsonObject().also { xtc.add("compiler", it) } + compiler.add("sourceModules", gson.toJsonTree(modules)) + return gson.toJson(settings) + } + + private fun child( + parent: JsonObject, + name: String, + ): JsonObject? = parent[name]?.takeUnless { it.isJsonNull }?.let(::objectValue) + + private fun parse(content: String?): JsonObject = + try { + if (content.isNullOrBlank()) { + JsonObject() + } else { + objectValue(JsonParser.parseString(content)) + } + } catch (failure: JsonParseException) { + throw IllegalArgumentException("Invalid compiler settings JSON", failure) + } + + fun replace( + content: String, + expected: List, + replacement: List, + base: URI, + ): String { + val settings = + try { + objectValue(JsonParser.parseString(content)) + } catch (failure: JsonParseException) { + throw IllegalArgumentException("Invalid compiler settings JSON", failure) + } + val compiler = objectValue(objectValue(settings["xtc"])["compiler"]) + val modules = + requireNotNull(compiler["sourceModules"]) { "Configured source modules are required" } + require(modules.isJsonArray) { "Configured source modules must be explicit" } + val current = + modules.asJsonArray.map { module -> + val entry = objectValue(module) + val dependencies = + entry["dependencies"] + ?.takeUnless { it.isJsonNull } + ?.let { + require(it.isJsonArray) { "Source dependencies must be an array" } + it.asJsonArray.map(::stringValue) + }.orEmpty() + SourceModuleConfiguration( + stringValue(entry["name"]), + stringValue(entry["uri"]), + dependencies, + resourceRoots(entry), + ) + } + require(canonical(current, base) == canonical(expected, base)) { + "Compiler source graph changed; rename was not applied" + } + compiler.add("sourceModules", gson.toJsonTree(replacement)) + return gson.toJson(settings) + } + + private fun objectValue(value: JsonElement?): JsonObject { + require(value?.isJsonObject == true) { "Compiler settings must contain an object" } + return value.asJsonObject + } + + private fun stringValue(value: JsonElement?): String { + require( + value?.isJsonPrimitive == true && + value.asJsonPrimitive.isString && + value.asString.isNotBlank(), + ) { + "Source module names, roots and dependencies must be non-blank strings" + } + return value.asString + } + + private fun resourceRoots(entry: JsonObject): List? = + entry["resourceRoots"] + ?.takeUnless { it.isJsonNull } + ?.let { values -> + require(values.isJsonArray) { "Resource roots must be an array" } + values.asJsonArray.map(::stringValue) + } + + private data class CanonicalModule( + val name: String, + val uri: URI, + val dependencies: Set, + val resources: List?, + ) + + private fun canonical( + modules: List, + base: URI, + ): Set { + require(modules.map { it.name }.distinct().size == modules.size) { + "Duplicate source module names" + } + val result = + modules.map { + val resources = + it.resourceRoots?.map { path -> + URI.create( + base + .resolve(path) + .normalize() + .toString() + .trimEnd('/') + "/", + ) + } + require(resources == null || resources.distinct().size == resources.size) { + "Duplicate resource roots" + } + require(resources.orEmpty().all { uri -> uri.scheme == "file" }) { + "Resource roots must use file URIs" + } + CanonicalModule( + it.name, + base.resolve(it.uri).normalize(), + it.dependencies.orEmpty().toSet(), + resources, + ) + } + require(result.map { it.uri }.distinct().size == modules.size) { + "Duplicate source module roots" + } + return result.toSet() + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt new file mode 100644 index 00000000000..796fd5286a4 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt @@ -0,0 +1,126 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.command.undo.BasicUndoableAction +import com.intellij.openapi.command.undo.UndoManager +import com.intellij.openapi.command.undo.UnexpectedUndoException +import com.intellij.openapi.project.Project +import com.intellij.util.xmlb.XmlSerializerUtil +import java.nio.file.Path + +/** LSP4IJ server settings join the same global command as source edits and resource moves. */ +internal class SourceGraphEdit + private constructor( + private val snapshot: Snapshot, + private val graph: SourceGraphReplacement, + ) { + fun isCurrent(): Boolean = snapshot.isCurrent() + + /** This action runs first on Redo, before any file is changed. Undo's guard is below. */ + fun beforeApply() { + UndoManager + .getInstance(snapshot.project) + .undoableActionPerformed( + object : BasicUndoableAction() { + override fun isGlobal(): Boolean = true + + override fun undo() = Unit + + override fun redo() { + replacement(graph.before, graph.after) + } + }, + ) + } + + fun apply() { + snapshot.install(replacement(graph.before, graph.after)) + UndoManager + .getInstance(snapshot.project) + .undoableActionPerformed( + object : BasicUndoableAction() { + override fun isGlobal(): Boolean = true + + override fun undo() = snapshot.install(replacement(graph.after, graph.before)) + + override fun redo() = snapshot.install(replacement(graph.before, graph.after)) + }, + ) + } + + private fun replacement( + expected: List, + next: List, + ): String = + try { + snapshot.replacement(expected, next) + } catch (failure: IllegalArgumentException) { + throw UnexpectedUndoException(failure.message) + } + + class Snapshot( + val project: Project, + val serverId: String, + val content: String?, + ) { + private val settings = CompilerSettings.store(project, serverId) + + fun isCurrent(): Boolean = + !project.isDisposed && + CompilerSettings.store(project, serverId) === settings && + settings.getLanguageServerSettings(serverId)?.configurationContent == content + + fun replacement(graph: SourceGraphReplacement): SourceGraphEdit { + SourceGraphConfiguration.replace( + requireNotNull(content) { + "Graph refactoring requires explicit LSP4IJ compiler settings" + }, + graph.before, + graph.after, + Path.of(requireNotNull(project.basePath)).toUri(), + ) + return SourceGraphEdit(this, graph) + } + + fun replacement( + expected: List, + next: List, + ): String = + SourceGraphConfiguration.replace( + requireSameStore(), + expected, + next, + Path.of(requireNotNull(project.basePath)).toUri(), + ) + + private fun requireSameStore(): String { + require(CompilerSettings.store(project, serverId) === settings) { + "Compiler configuration ownership changed; rename history was not applied" + } + return requireNotNull( + settings.getLanguageServerSettings(serverId)?.configurationContent, + ) + } + + fun install(content: String?) { + val current = requireNotNull(settings.getLanguageServerSettings(serverId)) + val copy = XmlSerializerUtil.createCopy(current) + copy.configurationContent = content + settings.updateSettings(serverId, copy) + } + } + + companion object { + fun capture( + project: Project, + serverId: String, + ): Snapshot = + Snapshot( + project, + serverId, + CompilerSettings + .store(project, serverId) + .getLanguageServerSettings(serverId) + ?.configurationContent, + ) + } + } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcClientFeatures.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcClientFeatures.kt new file mode 100644 index 00000000000..c02b344c743 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcClientFeatures.kt @@ -0,0 +1,158 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.psi.PsiFile +import com.redhat.devtools.lsp4ij.JSONUtils +import com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures +import com.redhat.devtools.lsp4ij.client.features.LSPInlayHintFeature +import com.redhat.devtools.lsp4ij.client.features.LSPRenameFeature +import com.redhat.devtools.lsp4ij.server.DefaultLauncherBuilder +import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InsertTextMode +import org.eclipse.lsp4j.jsonrpc.Launcher +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.RemoteEndpoint +import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints +import org.eclipse.lsp4j.services.LanguageServer +import java.net.URI +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicReference + +/** Client features and the document ownership of the current transport. */ +class XtcClientFeatures : LSPClientFeatures() { + internal val preflightedRenames = PreflightedRenames() + internal val documents = AtomicReference() + private val edits = ConcurrentHashMap.newKeySet>() + + // TODO LSP4IJ: UP04 — expose transmitted document versions and validate workspace/applyEdit inside + // its write command. Reuse our existing transport ownership until upstream provides this. + fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture { + val result = CompletableFuture() + edits.add(result) + result.whenComplete { _, _ -> edits.remove(result) } + CompletableFuture.runAsync { + try { + val edit = + ReadAction.computeBlocking { + ServerWorkspaceEdit.prepare(this, params) + } + ApplicationManager.getApplication().invokeLater { + if (!result.isDone) { + try { + result.complete(edit.apply()) + } catch (failure: RuntimeException) { + result.complete( + ServerWorkspaceEdit.refused(failure.message ?: "Edit failed"), + ) + } + } + } + } catch (failure: RuntimeException) { + result.complete( + ServerWorkspaceEdit.refused(failure.message ?: "Edit cannot be verified"), + ) + } + } + return result + } + + override fun dispose() { + documents.set(null) + edits.forEach { it.cancel(false) } + super.dispose() + } + + override fun initializeParams(params: InitializeParams) { + super.initializeParams(params) + // TODO LSP4IJ: UP25 — 0.21.0 has no workspace/textDocumentContent provider or refresh + // handler. Leave this capability absent so library navigation uses protected file views. + // Remove the fallback only after upstream supports read-only virtual document lifetimes. + // TODO LSP4IJ: UP02 — advertise save hooks only when DocumentContentSynchronizer + // actually dispatches them. Native Actions on Save owns formatting here. + params.capabilities?.textDocument?.synchronization?.apply { + willSave = false + willSaveWaitUntil = false + } + // TODO LSP4IJ: UP18 — snippet templates indent even when an item requests AsIs. + // Advertise the implemented mode until native X150 passes without this constraint. + params.capabilities?.textDocument?.completion?.apply { + insertTextMode = InsertTextMode.AdjustIndentation + completionItem?.insertTextModeSupport?.valueSet = listOf(InsertTextMode.AdjustIndentation) + } + } + + init { + setInlayHintFeature( + object : LSPInlayHintFeature() { + override fun isInlayHintSupported(file: PsiFile): Boolean = + LanguageServiceSettings.validated(file.project).inlayHints && + super.isInlayHintSupported(file) + }, + ) + // TODO LSP4IJ: UP04 — remove this override when native symbol rename checks document + // epochs. + // XtcRenameHandler supplies the guarded native entry point until then. + // Keep LSP4IJ's independent file-operation support enabled. + setRenameFeature( + object : LSPRenameFeature() { + override fun isRenameSupported(file: PsiFile): Boolean = false + }, + ) + } + + override fun createLauncherBuilder(): Launcher.Builder = + object : DefaultLauncherBuilder(this) { + @Suppress("UNCHECKED_CAST") // LSP4J's multiple-interface proxy factory returns Object. + override fun createProxy(remoteEndpoint: RemoteEndpoint): S { + val endpoint = preflightedRenames.endpoint(remoteEndpoint) + // Match Launcher.Builder: the single-interface overload derives its loader from + // that interface. The optional builder classLoader is normally null in LSP4IJ. + return if (localServices.size == 1 && remoteInterfaces.size == 1) { + ServiceEndpoints.toServiceObject(endpoint, remoteInterfaces.single()) + } else { + ServiceEndpoints.toServiceObject(endpoint, remoteInterfaces.toList>(), classLoader) as S + } + } + + private fun snapshot(uri: String): DocumentStartupMessages.Snapshot? { + if (project.isDisposed || serverWrapper.isDisposed) return null + val opened = serverWrapper.getOpenedDocument(URI(uri)) ?: return null + val document = opened.synchronizer?.document ?: return null + // File rename waits for didOpen while holding the IDE write lock. + // Transport hooks must use the document's lock-free immutable text; + // acquiring a read action here deadlocks that rename on the EDT. + return DocumentStartupMessages.Snapshot( + opened, + document.modificationStamp, + document.immutableCharSequence.toString(), + ) + } + + private val documents = + DocumentStartupMessages(::snapshot).also { + this@XtcClientFeatures.documents.set(it) + } + private val diagnostics = DiagnosticResultMessages(::snapshot) + + override fun wrapMessageConsumer(consumer: MessageConsumer): MessageConsumer { + val wrapped = super.wrapMessageConsumer(consumer) + return if (consumer is RemoteEndpoint) { + documents.incoming( + diagnostics.incoming(CodeActionMessages.incoming(wrapped)), + ) + } else { + documents.outgoing(diagnostics.outgoing(wrapped)) + } + } + }.configureGson { + // configureGson replaces the base callback; retain LSP4IJ's compatibility + // adapters. + JSONUtils.configureCompatibilityAdapters(it) + it.registerTypeAdapterFactory(ConfigurationJson) + it.registerTypeAdapterFactory(DiagnosticReportJson) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt new file mode 100644 index 00000000000..57d7bd960f0 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt @@ -0,0 +1,150 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.TextFieldWithBrowseButton +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiDirectory +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.psi.PsiReference +import com.intellij.refactoring.move.MoveCallback +import com.intellij.refactoring.move.MoveHandlerDelegate +import com.intellij.refactoring.ui.RefactoringDialog +import com.intellij.ui.DocumentAdapter +import com.intellij.ui.components.JBTextField +import com.redhat.devtools.lsp4ij.internal.CancellationSupport +import java.awt.GridLayout +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import javax.swing.JComponent +import javax.swing.JLabel +import javax.swing.JPanel +import javax.swing.event.DocumentEvent + +/** Community Move entry point: the compiler sees every old path before VFS changes anything. */ +class XtcFileMoveHandler : MoveHandlerDelegate() { + // TODO LSP4IJ: UP03 — remove this bridge when native Move preflights willRenameFiles and applies + // both URI parents and names in one version-checked undo command (shared X130 acceptance). + override fun canMove( + elements: Array, + targetContainer: PsiElement?, + reference: PsiReference?, + ): Boolean = + elements.isNotEmpty() && + elements.all { element -> + file(element)?.let(XtcFileOperations::isSourcePath) == true + } && + XtcFileOperations.server(elements.first().project) != null && + (targetContainer == null || targetContainer is PsiDirectory) + + override fun isValidTarget( + target: PsiElement?, + sources: Array, + ): Boolean = target is PsiDirectory + + override fun doMove( + project: Project, + elements: Array, + targetContainer: PsiElement?, + callback: MoveCallback?, + ) { + val files = elements.map { file(it) ?: return } + MoveDialog(project, files, (targetContainer as? PsiDirectory)?.virtualFile, callback).show() + } + + private fun file(element: PsiElement): VirtualFile? = + when (element) { + is PsiFile -> element.virtualFile + is PsiDirectory -> element.virtualFile + else -> null + } + + private class MoveDialog( + project: Project, + private val files: List, + target: VirtualFile?, + private val callback: MoveCallback?, + ) : RefactoringDialog(project, false) { + private val destination = + TextFieldWithBrowseButton().apply { + text = (target ?: files.first().parent).path + addBrowseFolderListener( + project, + FileChooserDescriptorFactory.createSingleFolderDescriptor(), + ) + addDocumentListener( + object : DocumentAdapter() { + override fun textChanged(event: DocumentEvent) = validateButtons() + }, + ) + } + + private val nameField = + JBTextField(files.first().name).apply { + accessibleContext.accessibleName = "New name" + document.addDocumentListener( + object : DocumentAdapter() { + override fun textChanged(event: DocumentEvent) = validateButtons() + }, + ) + } + + init { + title = "Move Ecstasy Sources" + init() + } + + override fun createCenterPanel(): JComponent = + JPanel(GridLayout(0, 1, 0, 4)).apply { + add(JLabel("Destination directory:")) + add(destination) + if (files.size == 1) { + add(JLabel("New name:")) + add(nameField) + } + } + + override fun getPreferredFocusedComponent(): JComponent = destination.textField + + override fun hasPreviewButton(): Boolean = false + + override fun areButtonsValid(): Boolean = targets()?.let(FileMoveTargets::valid) == true + + private fun targets(): Map? = + runCatching { + val directory = Path.of(destination.text).toAbsolutePath().normalize() + FileMoveTargets.inDirectory(files.map { Path.of(it.path) }, directory, nameField.text.takeIf { files.size == 1 }) + }.getOrNull() + + override fun doAction() { + val targets = targets() ?: return + if (!FileMoveTargets.valid(targets)) return + // Resolve an existing target for the later VFS move; never create directories before + // proof. + val wrapper = XtcFileOperations.server(project) ?: return + val requested = files.associateWith { targets.getValue(Path.of(it.path)) } + val cancellation = CancellationSupport() + val future = + CompletableFuture + .runAsync { + requireNotNull( + LocalFileSystem + .getInstance() + .refreshAndFindFileByNioFile(targets.values.first().parent), + ) { + "Destination directory no longer exists" + } + }.thenCompose { + cancellation.checkCanceled() + cancellation.execute(XtcRenameEdit.requestFileMoves(wrapper, requested)) + } + future.whenComplete { _, _ -> if (future.isCancelled) cancellation.cancel() } + close(OK_EXIT_CODE) + XtcFileOperations.submit(project, "Move Ecstasy sources", future) { + callback?.refactoringCompleted() + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt new file mode 100644 index 00000000000..02ba56b21c1 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt @@ -0,0 +1,78 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.progress.ProgressIndicator +import com.intellij.openapi.progress.Task +import com.intellij.openapi.progress.util.ProgressIndicatorUtils +import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.Messages +import com.intellij.openapi.vfs.VirtualFile +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import java.util.concurrent.CompletableFuture + +/** Shared preflight ownership for the file-tree Rename and Move actions. */ +internal object XtcFileOperations { + fun isSourcePath(file: VirtualFile): Boolean = + if (!file.isDirectory) { + file.extension == "x" + } else { + file.children.any { !it.isDirectory && it.extension == "x" } || + generateSequence(file) { it.parent } + .any { it.parent?.findChild("${it.name}.x")?.isDirectory == false } + } + + fun server(project: Project): LanguageServerWrapper? = + LanguageServiceAccessor.getInstance(project).startedServers.singleOrNull { + it.serverDefinition.id == CompilerSettings.SERVER_ID && + it.serverCapabilitiesSync + ?.workspace + ?.fileOperations + ?.willRename != null + } + + fun submit( + project: Project, + title: String, + future: CompletableFuture, + completed: () -> Unit = {}, + ) { + object : Task.Backgroundable(project, title, true) { + override fun run(indicator: ProgressIndicator) { + ProgressIndicatorUtils.awaitWithCheckCanceled(future.handle { _, _ -> null }) + } + + override fun onCancel() { + future.cancel(true) + } + }.queue() + future.whenComplete { edit, error -> + ApplicationManager.getApplication().invokeLater { + if (project.isDisposed || future.isCancelled) return@invokeLater + val failure = + when { + error != null -> { + "$title failed: ${error.cause?.message ?: error.message}" + } + + edit == null -> { + "The compiler cannot safely apply this operation. No changes were made." + } + + !edit.apply() -> { + "Sources or paths changed; invoke the operation again." + } + + else -> { + null + } + } + if (failure == null) { + completed() + } else { + Messages.showErrorDialog(project, failure, "Ecstasy Refactoring") + } + } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt new file mode 100644 index 00000000000..dbc735b3d3f --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt @@ -0,0 +1,99 @@ +package org.xtclang.idea.lsp + +import com.intellij.ide.TitledHandler +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.fileTypes.FileTypes +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.refactoring.rename.RenameHandler +import com.intellij.refactoring.ui.NameSuggestionsField +import com.intellij.refactoring.ui.RefactoringDialog +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import javax.swing.JComponent + +/** + * Request compiler proof before changing a file's physical path. IntelliJ's VFS before-event for + * rename runs after the filesystem mutation, too late for LSP4IJ's willRenameFiles listener. + */ +class XtcFileRenameHandler : + RenameHandler, + TitledHandler { + // TODO LSP4IJ: UP03 — move willRenameFiles preflight before the physical mutation; its current VFS + // before-event is already too late. Then this host-specific preflight can be removed. + override fun getActionTitle(): String = "Rename Ecstasy file and references" + + override fun isAvailableOnDataContext(dataContext: DataContext): Boolean { + if (CommonDataKeys.EDITOR.getData(dataContext) != null) return false + val project = CommonDataKeys.PROJECT.getData(dataContext) ?: return false + val file = selectedFile(dataContext) ?: return false + return XtcFileOperations.isSourcePath(file) && XtcFileOperations.server(project) != null + } + + override fun invoke( + project: Project, + editor: Editor, + file: PsiFile, + dataContext: DataContext, + ) = invoke(project, arrayOf(file), dataContext) + + override fun invoke( + project: Project, + elements: Array, + dataContext: DataContext, + ) { + val file = selectedFile(dataContext) ?: return + val wrapper = XtcFileOperations.server(project) ?: return + RenameDialog(project, file, wrapper).show() + } + + private fun selectedFile(context: DataContext): VirtualFile? { + val files = CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(context) + return if (files == null) { + CommonDataKeys.VIRTUAL_FILE.getData(context) + } else { + files.singleOrNull() + } + } + + private class RenameDialog( + project: Project, + private val file: VirtualFile, + private val wrapper: LanguageServerWrapper, + ) : RefactoringDialog(project, false) { + private val name = + NameSuggestionsField(arrayOf(file.name), project, FileTypes.PLAIN_TEXT, null) + + init { + title = "Rename" + name.addDataChangedListener { validateButtons() } + init() + } + + override fun createCenterPanel(): JComponent = name + + override fun getPreferredFocusedComponent(): JComponent = name.focusableComponent + + override fun hasPreviewButton(): Boolean = false + + override fun areButtonsValid(): Boolean = + name.enteredName.let { + it.isNotBlank() && + it != file.name && + it != "." && + it != ".." && + '/' !in it && + '\\' !in it && + (file.isDirectory || it.endsWith(".x")) + } + + override fun doAction() { + val future = XtcRenameEdit.requestFileRename(wrapper, file, name.enteredName) + close(OK_EXIT_CODE) + XtcFileOperations.submit(project, "Rename ${file.name}", future) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt new file mode 100644 index 00000000000..be94f2ae8dd --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt @@ -0,0 +1,138 @@ +package org.xtclang.idea.lsp + +import com.intellij.formatting.service.AsyncFormattingRequest +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.editor.EditorFactory +import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.progress.util.ProgressIndicatorUtils +import com.intellij.psi.PsiFile +import com.redhat.devtools.lsp4ij.LSPIJUtils +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.client.features.LSPFormattingFeature +import com.redhat.devtools.lsp4ij.features.formatting.LSPFormattingAndRangeBothService +import com.redhat.devtools.lsp4ij.features.formatting.LSPFormattingParams +import com.redhat.devtools.lsp4ij.features.formatting.LSPFormattingSupport +import java.util.concurrent.CancellationException +import java.util.concurrent.atomic.AtomicBoolean + +/** Format asynchronously with version-checked, undoable edits, including closed tabs. */ +class XtcFormattingService : LSPFormattingAndRangeBothService() { + override fun canSupportFormatting( + feature: LSPFormattingFeature, + file: PsiFile, + ): Boolean = + file.virtualFile?.extension == "x" && + feature.clientFeatures.isServerDefinition(CompilerSettings.SERVER_ID) && + super.canSupportFormatting(feature, file) + + // TODO LSP4IJ: UP12 — LSPFormattingSupport.format accepts a nullable editor but dereferences it + // after the reply. Apply edits to the captured document until upstream fixes that path. + override fun createFormattingTask(request: AsyncFormattingRequest): FormattingTask? { + val input = request.ioFile ?: return null + val source = LSPIJUtils.findResourceFor(input) ?: return null + val file = LSPIJUtils.getPsiFile(source, request.context.project) ?: return null + val document = LSPIJUtils.getDocument(source) ?: return null + val original = request.documentText + val stamp = document.modificationStamp + val application = ApplicationManager.getApplication() + val synchronous = document.getUserData(FORMAT_DOCUMENT_SYNCHRONOUSLY) == true || application.isHeadlessEnvironment + val modality = ModalityState.defaultModalityState() + val range = + request.formattingRanges.firstOrNull()?.takeUnless { it.length == original.length } + val support = LSPFormattingSupport(file) + val cancelled = AtomicBoolean() + + fun current() = + !cancelled.get() && !file.project.isDisposed && source.isValid && + document.modificationStamp == stamp && document.text == original + return object : FormattingTask { + override fun run() { + try { + if (cancelled.get()) return + val servers = + LanguageServiceAccessor + .getInstance(file.project) + .getLanguageServers( + file, + { + it.isServerDefinition(CompilerSettings.SERVER_ID) && + it.formattingFeature.isEnabled(file) + }, + { it.formattingFeature.isFormattingSupported(file) }, + ) + val server = + ProgressIndicatorUtils.awaitWithCheckCanceled(servers).singleOrNull() + if (server == null || cancelled.get()) { + request.onTextReady(null) + return + } + val options = + server.clientFeatures.formattingFeature.getFormattingOptions(file, null) + val result = + support.getFeatureData( + LSPFormattingParams(range, document, server, options), + ) + if (cancelled.get()) support.cancel() + val edits = result?.let { ProgressIndicatorUtils.awaitWithCheckCanceled(it) } + if (!ReadAction.computeBlocking(::current)) { + request.onTextReady(null) + } else { + // LSP ranges belong to the request, never to a concurrently edited buffer. + val snapshot = EditorFactory.getInstance().createDocument(original) + val formatted = LSPIJUtils.applyEdits(snapshot, edits ?: emptyList()) + when { + formatted == original -> { + request.onTextReady(null) + } + + synchronous -> { + request.onTextReady(formatted) + } + + else -> { + // TODO LSP4IJ: UP24 — IDEA's asynchronous formatting application is + // undo-transparent: standalone Format undoes but cannot redo. Keep + // synchronous Save's command; give asynchronous replies their own + // short write command without waiting for the server on the EDT. + application.invokeLater({ + try { + WriteCommandAction + .writeCommandAction(file.project) + .withName("Format Ecstasy") + .run { + if (current()) document.setText(formatted) + } + request.onTextReady(null) + } catch (failure: RuntimeException) { + request.onError("Ecstasy formatting error", failure.message ?: "Formatting failed") + } + }, modality) + } + } + } + } catch (cancelled: ProcessCanceledException) { + support.cancel() + throw cancelled + } catch (_: CancellationException) { + request.onTextReady(null) + } catch (failure: RuntimeException) { + request.onError( + "Ecstasy formatting error", + failure.cause?.message ?: failure.message ?: "Formatting failed", + ) + } + } + + override fun cancel(): Boolean { + cancelled.set(true) + support.cancel() + return true + } + + override fun isRunUnderProgress(): Boolean = true + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageClient.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageClient.kt index 291046ce203..3e3df22d347 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageClient.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageClient.kt @@ -1,57 +1,233 @@ package org.xtclang.idea.lsp import com.intellij.application.options.CodeStyle +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.fileEditor.FileEditorManager import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.VirtualFileManager +import com.intellij.psi.PsiManager +import com.intellij.psi.codeStyle.CodeStyleSettingsListener +import com.intellij.util.concurrency.SequentialTaskExecutor +import com.redhat.devtools.lsp4ij.LSPFileSupport +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor import com.redhat.devtools.lsp4ij.client.LanguageClientImpl -import org.eclipse.lsp4j.ConfigurationItem -import org.eclipse.lsp4j.ConfigurationParams +import com.redhat.devtools.lsp4ij.settings.LanguageServerSettingsListener +import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.RegistrationParams +import org.eclipse.lsp4j.UnregistrationParams import org.xtclang.idea.XtcIntelliJLanguage import java.util.concurrent.CompletableFuture +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference /** - * Custom LSP language client that bridges IntelliJ Code Style settings to the XTC LSP server. + * Refreshes compiler semantic caches and bridges Ecstasy Code Style settings. Compiler source + * modules use LSP4IJ's server Configuration settings under `xtc.compiler`; the base client owns + * section lookup, change notifications and listener disposal. * * When the LSP server sends a `workspace/configuration` request for section `"xtc.formatting"`, - * this client reads the current IntelliJ Code Style settings for the Ecstasy language and - * returns them as a JSON-compatible map. This implements Phase 3 of the formatting plan: - * IntelliJ Code Style settings flow to the LSP server as a fallback when no `xtc-format.toml` - * config file is present. - * - * Resolution chain (highest priority first): - * 1. `xtc-format.toml` in the project tree (not yet implemented) - * 2. IntelliJ Code Style settings (this client provides them) - * 3. LSP `FormattingOptions` from the editor (tabSize / insertSpaces) - * 4. XTC defaults (4-space indent, 8-space continuation, no tabs) + * this client reads the current IntelliJ Code Style settings for the Ecstasy language and returns + * them as a JSON-compatible map. Code Style changes refresh the server's immutable formatting + * snapshot. Compiler mode uses the right margin for bounded expression/list wrapping; + * `xtc-format.toml` is not implemented. */ class XtcLanguageClient( project: Project, ) : LanguageClientImpl(project) { - /** - * Handle `workspace/configuration` requests from the LSP server. - * - * The server requests configuration sections after initialization. For each - * [ConfigurationItem] with section `"xtc.formatting"`, we return the IntelliJ - * Code Style settings for XTC. Unknown sections get `null`. - */ - override fun configuration(params: ConfigurationParams): CompletableFuture> { - logger.info("workspace/configuration: ${params.items.map { it.section }}") - val results = params.items.map { item -> resolveConfigSection(item) } - return CompletableFuture.completedFuture(results) + private val editorRefresh = EditorRefresh(project, this) { isDisposed } + private val compilerWatches = CompilerVfsWatches() + private val preferences = AtomicReference(LanguageServiceSettings.validated(project)) + private val updateQueued = AtomicBoolean() + private val settingsStores = + listOf(LanguageServiceSettings.store(null), LanguageServiceSettings.store(project)) + private val settingsListener = + LanguageServerSettingsListener { event -> + if ( + event.languageServerId() == CompilerSettings.SERVER_ID && + event.configurationContentChanged() + ) { + if (updateQueued.compareAndSet(false, true)) { + ApplicationManager.getApplication().invokeLater { + updateQueued.set(false) + if (!isDisposed && !project.isDisposed) { + val next = LanguageServiceSettings.validated(project) + val before = preferences.getAndSet(next) + if (next.requiresRestart(before)) { + LanguageServiceAccessor + .getInstance(project) + .startedServers + .filter { it.serverDefinition.id == CompilerSettings.SERVER_ID } + .forEach { it.restart() } + } else { + if (before.inlayHints != next.inlayHints) refreshInlayHints() + if (before.referenceCodeLens != next.referenceCodeLens) refreshCodeLenses() + } + } + } + } + } + } + + init { + settingsStores.forEach { it.addSettingsListener(settingsListener) } + project.messageBus + .connect(this) + .subscribe( + CodeStyleSettingsListener.TOPIC, + CodeStyleSettingsListener { + if (!isDisposed && !project.isDisposed) triggerChangeConfiguration() + }, + ) + } + + override fun dispose() { + settingsStores.forEach { it.removeSettingsListener(settingsListener) } + compilerWatches.dispose() + super.dispose() + } + + override fun setServerWrapper(wrapper: LanguageServerWrapper) { + super.setServerWrapper(wrapper) + ApplicationManager + .getApplication() + .messageBus + .connect(this) + .subscribe(VirtualFileManager.VFS_CHANGES, DirectoryDocumentMoves(wrapper) { isDisposed }) } - private fun resolveConfigSection(item: ConfigurationItem): Any? = - when (item.section) { - FORMATTING_SECTION -> { - readFormattingSettings() + override fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture = + if (isDisposed || project.isDisposed) { + CompletableFuture.completedFuture(ServerWorkspaceEdit.refused("Connection is closed")) + } else { + (clientFeatures as XtcClientFeatures).applyEdit(params) + } + + override fun registerCapability(params: RegistrationParams): CompletableFuture = + super.registerCapability(params).thenRunAsync { + if (!isDisposed && !project.isDisposed) compilerWatches.register(params.registrations) + } + + override fun unregisterCapability(params: UnregistrationParams): CompletableFuture = + super.unregisterCapability(params).thenRun { + compilerWatches.unregister(params.unregisterations.map { it.id }.toSet()) + } + + // Rename/file-operation listeners can hold the IDE write lock while awaiting an LSP reply. + // Never acquire a read lock on the transport thread. Use the shared application pool and + // preserve notification order without creating a dedicated thread per connection. + private val semanticUpdates = + SequentialTaskExecutor.createSequentialApplicationPoolExecutor("Ecstasy semantic updates") + + // TODO LSP4IJ: UP13 — merge project/global configuration in createSettings upstream. + // LSP4IJ already subscribes/disposes listeners for both stores, but its default + // createSettings reads only the global store. Project settings take precedence here. + override fun createSettings(): Any? = + CompilerBuildModel.settings( + project, + CompilerSettings + .store(project, serverDefinition.id) + .getLanguageServerSettings(serverDefinition.id) + ?.getLanguageServerConfiguration(project), + ) + + override fun publishDiagnostics(params: PublishDiagnosticsParams) { + semanticUpdate { + clientFeatures.findFileByUri(params.uri)?.let(::invalidateSemanticFacts) + super.publishDiagnostics(params) + }.exceptionally { failure -> + if (!isDisposed && !project.isDisposed) { + logger.warn("Failed to publish Ecstasy diagnostics", failure) } + null + } + } - else -> { - logger.info("workspace/configuration: unknown section '${item.section}', returning null") - null + override fun refreshDiagnostics(): CompletableFuture = + semanticUpdate { + FileEditorManager.getInstance(project).openFiles.forEach(::invalidateSemanticFacts) + }.thenCompose { + if (isDisposed || project.isDisposed) { + CompletableFuture.completedFuture(null) + } else { + super.refreshDiagnostics() } } + override fun refreshCodeLenses(): CompletableFuture = refreshEditors(EditorRefresh.Feature.LENSES) + + override fun refreshInlayHints(): CompletableFuture = refreshEditors(EditorRefresh.Feature.HINTS) + + override fun refreshSemanticTokens(): CompletableFuture = refreshEditors(EditorRefresh.Feature.TOKENS) + + private fun refreshEditors(feature: EditorRefresh.Feature): CompletableFuture = + if (isDisposed || project.isDisposed) { + CompletableFuture.completedFuture(null) + } else { + editorRefresh.request(clientFeatures.serverWrapper, feature) + } + + private fun semanticUpdate(update: () -> Unit): CompletableFuture = + CompletableFuture.runAsync( + { if (!isDisposed && !project.isDisposed) update() }, + semanticUpdates, + ) + + private fun invalidateSemanticFacts(file: VirtualFile) { + ReadAction.runBlocking { + if (project.isDisposed) return@runBlocking + val psi = PsiManager.getInstance(project).findFile(file) ?: return@runBlocking + if (!LSPFileSupport.hasSupport(psi)) return@runBlocking + val support = LSPFileSupport.getSupport(psi) + // TODO LSP4IJ: UP14 — invalidate semantic caches on diagnostic refresh, not just PSI changes. + // A dependency can change this file's semantics without changing its PSI stamp. + // LSP4IJ keys these caches to that stamp, so retire completed results when the + // compiler publishes a new analysis. Pending requests already target the current + // compiler queue and retain their normal cancellation/version checks. + with(support) { + listOf( + completionSupport, + definitionSupport, + typeDefinitionSupport, + implementationSupport, + referenceSupport, + hoverSupport, + signatureHelpSupport, + highlightSupport, + prepareRenameSupport, + renameSupport, + intentionCodeActionSupport, + codeLensSupport, + documentSymbolSupport, + semanticTokensSupport, + inlayHintsSupport, + prepareTypeHierarchySupport, + typeHierarchySupertypesSupport, + typeHierarchySubtypesSupport, + prepareCallHierarchySupport, + callHierarchyIncomingCallsSupport, + callHierarchyOutgoingCallsSupport, + ).forEach { feature -> if (feature.future?.isDone == true) feature.cancel() } + } + } + } + + /** + * Specialize editor formatting and reference-lens preferences. Other sections preserve compiler graphs, + * null entries for unknown sections and the base client's asynchronous response ordering. + */ + override fun findSettings(section: String?): Any? = + when (section) { + FORMATTING_SECTION -> readFormattingSettings() + "xtc.codeLens" -> mapOf("references" to LanguageServiceSettings.validated(project).referenceCodeLens) + else -> super.findSettings(section) + } + private fun readFormattingSettings(): Map { val settings = CodeStyle.getProjectOrDefaultSettings(project) val commonSettings = settings.getCommonSettings(XtcIntelliJLanguage) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt new file mode 100644 index 00000000000..865615d8983 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt @@ -0,0 +1,43 @@ +package org.xtclang.idea.lsp + +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.WorkspaceEdit +import org.eclipse.lsp4j.jsonrpc.services.JsonRequest +import org.eclipse.lsp4j.services.LanguageServer +import java.util.concurrent.CompletableFuture + +/** Standard Rename remains available to clients that cannot persist compiler graph changes. */ +interface XtcLanguageServer : LanguageServer { + @JsonRequest("xtc/languageServiceStatus") + fun languageServiceStatus(): CompletableFuture> + + @JsonRequest("xtc/exportLogs") + fun exportLogs(): CompletableFuture> + + @JsonRequest("xtc/compilerSourceModules") + fun compilerSourceModules(): CompletableFuture> + + @JsonRequest("xtc/rename") + fun renameProposal(params: RenameParams): CompletableFuture + + @JsonRequest("xtc/renameFiles") + fun renameFilesProposal(params: RenameFilesParams): CompletableFuture +} + +data class RenameProposal( + val edit: WorkspaceEdit, + val graph: SourceGraphReplacement? = null, +) + +data class SourceGraphReplacement( + val before: List, + val after: List, +) + +data class SourceModuleConfiguration( + val name: String, + val uri: String, + val dependencies: List = emptyList(), + val resourceRoots: List? = null, +) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLspServerSupportProvider.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLspServerSupportProvider.kt index 8ea20add567..be4efb2e705 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLspServerSupportProvider.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLspServerSupportProvider.kt @@ -1,11 +1,18 @@ package org.xtclang.idea.lsp import com.intellij.execution.configurations.GeneralCommandLine -import com.intellij.notification.NotificationGroupManager +import com.intellij.notification.Notification import com.intellij.notification.NotificationType import com.intellij.openapi.diagnostic.logger import com.intellij.openapi.project.Project +import com.intellij.openapi.ui.popup.Balloon +import com.intellij.openapi.util.text.StringUtil +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.ui.BalloonImpl +import com.intellij.ui.ColorUtil +import com.intellij.util.ui.JBUI import com.redhat.devtools.lsp4ij.LanguageServerFactory +import com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures import com.redhat.devtools.lsp4ij.server.JavaProcessCommandBuilder import com.redhat.devtools.lsp4ij.server.OSProcessStreamConnectionProvider import org.eclipse.lsp4j.services.LanguageServer @@ -15,62 +22,78 @@ import java.util.Properties import java.util.concurrent.atomic.AtomicBoolean /** - * Shared build properties loaded once at class initialization time. - * This avoids concurrent getResourceAsStream() calls which can cause - * "Inflater wants input" errors in IntelliJ's PluginClassLoader. + * Shared build properties loaded once at class initialization time. This avoids concurrent + * getResourceAsStream() calls which can cause "Inflater wants input" errors in IntelliJ's + * PluginClassLoader. */ private object LspBuildProperties { private val logger = logger() val properties: Properties = Properties().apply { - LspBuildProperties::class.java.getResourceAsStream("/lsp-version.properties")?.use { load(it) } - ?: logger.error("lsp-version.properties not found in plugin resources!") + LspBuildProperties::class.java.getResourceAsStream("/lsp-version.properties")?.use { + load(it) + } ?: logger.error("lsp-version.properties not found in plugin resources!") } - val version: String get() = properties.getProperty("lsp.version", "?") - val adapter: String get() = properties.getProperty("lsp.adapter", "mock") - val buildTime: String get() = properties.getProperty("lsp.build.time", "?") - val buildInfo: String get() = "v$version built $buildTime" + val version: String + get() = properties.getProperty("lsp.version", "?") + + val adapter: String + get() = properties.getProperty("lsp.adapter", "compiler") + + val buildTime: String + get() = properties.getProperty("lsp.build.time", "?") + + val buildInfo: String + get() = "v$version built $buildTime" } /** * Factory for creating Ecstasy Language Server connections. * - * The server runs OUT-OF-PROCESS as a separate Java process for classloader isolation - * (avoids lsp4j version conflicts with LSP4IJ) and crash/memory isolation. It uses - * IntelliJ's own JBR 25 runtime via LSP4IJ's [JavaProcessCommandBuilder]. + * The server runs OUT-OF-PROCESS as a separate Java process for classloader isolation (avoids lsp4j + * version conflicts with LSP4IJ) and crash/memory isolation. It uses IntelliJ's own JBR 25 runtime + * via LSP4IJ's [JavaProcessCommandBuilder]. */ class XtcLanguageServerFactory : LanguageServerFactory { private val logger = logger() override fun createConnectionProvider(project: Project) = XtcLspConnectionProvider(project).also { - logger.info("Creating XTC LSP connection provider (out-of-process) - ${LspBuildProperties.buildInfo}") + logger.info( + "Creating Ecstasy LSP connection provider (out-of-process) - ${LspBuildProperties.buildInfo}", + ) } override fun createLanguageClient(project: Project) = XtcLanguageClient(project) - override fun getServerInterface(): Class = LanguageServer::class.java + override fun createClientFeatures(): LSPClientFeatures = XtcClientFeatures() + + override fun getServerInterface(): Class = XtcLanguageServer::class.java } /** * Out-of-process LSP server connection using LSP4IJ's [OSProcessStreamConnectionProvider]. * - * Uses [JavaProcessCommandBuilder] to resolve IntelliJ's JBR java binary and build - * the server command line. We use [OSProcessStreamConnectionProvider] (not the simpler - * [com.redhat.devtools.lsp4ij.server.ProcessStreamConnectionProvider]) because it - * leverages IntelliJ's [com.intellij.execution.process.OSProcessHandler] for proper - * process lifecycle management and stderr capture in LSP4IJ's Language Servers panel. + * Uses [JavaProcessCommandBuilder] to resolve IntelliJ's JBR java binary and build the server + * command line. We use [OSProcessStreamConnectionProvider] (not the simpler + * [com.redhat.devtools.lsp4ij.server.ProcessStreamConnectionProvider]) because it leverages + * IntelliJ's [com.intellij.execution.process.OSProcessHandler] for proper process lifecycle + * management and stderr capture in LSP4IJ's Language Servers panel. * - * The server JAR is in `bin/` (not `lib/`) to avoid classloader conflicts with - * LSP4IJ's bundled lsp4j. + * The server JAR is in `bin/` (not `lib/`) to avoid classloader conflicts with LSP4IJ's bundled + * lsp4j. */ class XtcLspConnectionProvider( private val project: Project, ) : OSProcessStreamConnectionProvider() { private val logger = logger() + private val support = project.getService(ServerSupportLogs::class.java) + private val launch = support.begin("Resolving the bundled Ecstasy server and Java runtime.\n") private val lifetime = ConnectionLifetime({ super.start() }, { super.stop() }) + private val adapter = LanguageAdapter.fromSetting(LanguageServiceSettings.validated(project).adapter) + private val adapterName = if (adapter == LanguageAdapter.DEFAULT) LspBuildProperties.adapter else adapter.setting companion object { private const val LSP_SERVER_JAR = "xtc-lsp-server.jar" @@ -82,13 +105,23 @@ class XtcLspConnectionProvider( private val startNotificationShown = AtomicBoolean(false) /** - * Resolve the LSP server JAR from a plugin directory. - * Returns the path to `bin/xtc-lsp-server.jar` if it exists, or null otherwise. + * Resolve the LSP server JAR from a plugin directory. Returns the path to + * `bin/xtc-lsp-server.jar` if it exists, or null otherwise. */ internal fun resolveServerJar(pluginDir: Path): Path? = PluginPaths.resolveInBin(pluginDir, LSP_SERVER_JAR) } init { + addLogErrorHandler { support.append(launch, it) } + try { + configureCommand() + } catch (failure: Exception) { + support.append(launch, "Startup failed: ${failure.message}\n") + throw failure + } + } + + private fun configureCommand() { val serverJar = findServerJar() // Log level: system property > environment variable > INFO default @@ -99,7 +132,7 @@ class XtcLspConnectionProvider( val semanticTokens = System.getProperty(SEMANTIC_TOKENS_SYSTEM_PROPERTY) ?: System.getenv(SEMANTIC_TOKENS_ENV) - ?: "false" + ?: "true" // JavaProcessCommandBuilder resolves IntelliJ's JBR java binary automatically // and handles debug port configuration from LSP4IJ's per-server settings. @@ -112,12 +145,17 @@ class XtcLspConnectionProvider( val jarIndex = commands.indexOf("-jar") commands.addAll( jarIndex, - listOf( - "-Dapple.awt.UIElement=true", // macOS: no dock icon - "-Djava.awt.headless=true", // No GUI components - "-Dxtc.logLevel=$logLevel", // Pass log level to LSP server - "-D$SEMANTIC_TOKENS_SYSTEM_PROPERTY=$semanticTokens", // Keep semantic tokens opt-in until client rendering is stable - ), + ServerRuntimeSettings.getInstance().launchArguments() + + adapter.launchArguments() + + listOf( + "-Dapple.awt.UIElement=true", // macOS: no dock icon + "-Djava.awt.headless=true", // No GUI components + "-Dxtc.logLevel=$logLevel", // Pass log level to LSP server + "-D$SEMANTIC_TOKENS_SYSTEM_PROPERTY=$semanticTokens", + ) + + listOf("xtc.trace.directory", "xtc.trace.level", "xtc.logs.directory", "xtc.lsp.colorPrototype").mapNotNull { key -> + System.getProperty(key)?.let { "-D$key=$it" } + }, ) // Convert to GeneralCommandLine for OSProcessStreamConnectionProvider. @@ -128,35 +166,65 @@ class XtcLspConnectionProvider( project.basePath?.let { withWorkDirectory(it) } } setCommandLine(commandLine) + support.append(launch, "Command: ${commandLine.commandLineString}\n") logger.info( - "XTC LSP command configured (v${LspBuildProperties.version}, " + - "adapter=${LspBuildProperties.adapter}, semanticTokens=$semanticTokens): ${commandLine.commandLineString}", + "Ecstasy LSP command configured (v${LspBuildProperties.version}, " + + "adapter=$adapterName, semanticTokens=$semanticTokens): ${commandLine.commandLineString}", ) } + override fun getInitializationOptions(rootUri: VirtualFile?): Any = + LanguageServiceSettings + .validated(project) + // TODO LSP4IJ: UP02 — native willSaveWaitUntil is absent; keep server save edits disabled. + .copy(saveFormatting = "editor") + .initializationOptions() + override fun start() { - logger.info("Starting XTC LSP Server (out-of-process via JBR)") + logger.info("Starting Ecstasy LSP Server (out-of-process via JBR)") + // TODO LSP4IJ: UP01 — make OS process start/stop atomic and reject starts after stop/disposal. // LSP4IJ starts on a pooled thread: project disposal or cancellation can stop the // provider first. Its OS provider otherwise starts even after its stop flag is set. if (project.isDisposed) lifetime.stop() - lifetime.start() + try { + lifetime.start() + pid?.let { support.process(launch, it) } + } catch (failure: Exception) { + support.append(launch, "Startup failed: ${failure.message}\n") + throw failure + } - logger.info("XTC LSP Server process started (v${LspBuildProperties.version}, adapter=${LspBuildProperties.adapter}, pid=$pid)") + logger.info( + "Ecstasy LSP Server process started (v${LspBuildProperties.version}, adapter=$adapterName, pid=$pid)", + ) if (startNotificationShown.compareAndSet(false, true)) { + val labelColor = ColorUtil.toHtmlColor(JBUI.CurrentTheme.ContextHelp.FOREGROUND) showNotification( title = "Ecstasy Language Server Started", - content = "Out-of-process server (v${LspBuildProperties.version}, adapter=${LspBuildProperties.adapter}, pid=$pid)", + content = + listOf( + "Version" to LspBuildProperties.version, + "Adapter" to adapterName, + "PID" to pid.toString(), + ).joinToString( + separator = "  ·  ", + prefix = "", + postfix = "", + ) { (label, value) -> + "$label ${StringUtil.escapeXmlEntities(value)}" + }, type = NotificationType.INFORMATION, ) } } override fun stop() { - logger.info("Stopping XTC LSP Server") + logger.info("Stopping Ecstasy LSP Server") lifetime.stop() - logger.info("XTC LSP Server stopped") + support.append(launch, "\nConnection stopped.\n") + logger.info("Ecstasy LSP Server stopped") } private fun findServerJar(): Path = PluginPaths.findServerJar(LSP_SERVER_JAR) @@ -166,10 +234,18 @@ class XtcLspConnectionProvider( content: String, type: NotificationType, ) { - NotificationGroupManager - .getInstance() - .getNotificationGroup("XTC Language Server") - .createNotification(title, content, type) - .notify(project) + object : Notification("XTC Language Server", title, content, type) { + override fun setBalloon(balloon: Balloon) { + super.setBalloon(balloon) + // The IDE timer pauses during interaction and hides only the balloon, keeping + // the startup details in Notifications and the log for later inspection. + (balloon as? BalloonImpl)?.apply { + startSmartFadeoutTimer(8_000) + // Smart fadeout alone waits for input before starting its clock. Also + // schedule it now so an untouched startup balloon disappears. + startFadeoutTimer(8_000) + } + } + }.notify(project) } } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcParameterInfoHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcParameterInfoHandler.kt new file mode 100644 index 00000000000..fdab1fcd787 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcParameterInfoHandler.kt @@ -0,0 +1,53 @@ +package org.xtclang.idea.lsp + +import com.intellij.lang.parameterInfo.CreateParameterInfoContext +import com.intellij.lang.parameterInfo.ParameterInfoUIContext +import com.redhat.devtools.lsp4ij.features.signatureHelp.LSPParameterInfoHandler +import com.redhat.devtools.lsp4ij.features.signatureHelp.LSPSignatureHelperPsiElement +import org.eclipse.lsp4j.SignatureInformation + +/** Missing parameter metadata means no safe active slot, not an empty signature. */ +class XtcParameterInfoHandler : LSPParameterInfoHandler() { + // Ecstasy's lexer can use plain-text or TextMate PSI. Leave other languages to LSP4IJ. + override fun findElementForParameterInfo(context: CreateParameterInfoContext): LSPSignatureHelperPsiElement? = + if (context.file.virtualFile?.extension == "x") { + super.findElementForParameterInfo(context) + } else { + null + } + + override fun updateUI( + signature: SignatureInformation, + context: ParameterInfoUIContext, + ) { + // TODO LSP4IJ: UP10 — refresh rendered overload metadata after retrigger; the popup keeps original + // SignatureInformation objects. Read current metadata for this overload when rendering. + val current = + (context.parameterOwner as? LSPSignatureHelperPsiElement) + ?.activeSignatureHelp + ?.signatures + ?.singleOrNull { it.label == signature.label } ?: signature + if (current.parameters.isNullOrEmpty()) { + context.setupUIComponentPresentation( + current.label, + -1, + -1, + !context.isUIComponentEnabled, + false, + false, + context.defaultParameterColor, + ) + } else { + // TODO LSP4IJ: UP10 — honor per-overload activeParameter and omit highlighting without + // metadata. + // LSP permits each overload to override the top-level active parameter. LSP4IJ's + // renderer otherwise reads only the shared context, which defaults to slot zero. + super.updateUI( + current, + object : ParameterInfoUIContext by context { + override fun getCurrentParameterIndex(): Int = current.activeParameter ?: context.currentParameterIndex + }, + ) + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt new file mode 100644 index 00000000000..ce896b4a7cc --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt @@ -0,0 +1,358 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.command.WriteCommandAction +import com.intellij.openapi.vfs.LocalFileSystem +import com.intellij.openapi.vfs.VirtualFile +import com.redhat.devtools.lsp4ij.DocumentContentSynchronizer +import com.redhat.devtools.lsp4ij.LSPIJUtils +import com.redhat.devtools.lsp4ij.LanguageServerWrapper +import com.redhat.devtools.lsp4ij.OpenedDocument +import com.redhat.devtools.lsp4ij.internal.CancellationSupport +import org.eclipse.lsp4j.CodeAction +import org.eclipse.lsp4j.FileRename +import org.eclipse.lsp4j.RenameFile +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceEdit +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageServer +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CompletableFuture + +// TODO LSP4IJ: UP04 — validate document versions before applying WorkspaceEdit; stale responses currently +// overwrite newer edits. Retire this snapshot guard after upstream provides equivalent checks. + +/** + * A rename response bound to the client documents that produced it. LSP4IJ 0.21 applies + * WorkspaceEdit without checking document versions. Keep the request's immutable snapshot until + * application and check it under the same write action as all of the returned edits. No second + * version counter or long-lived document listener is needed. + */ +class XtcRenameEdit + private constructor( + private val snapshot: Snapshot, + val edit: WorkspaceEdit, + private val graph: SourceGraphEdit? = null, + private val command: String = "Rename", + ) { + /** Return false without changing any file when the request's documents have been retired. */ + fun apply(): Boolean { + ApplicationManager.getApplication().assertIsDispatchThread() + val project = snapshot.wrapper.project + if (project.isDisposed) return false + return WriteCommandAction + .writeCommandAction(project) + .withName(command) + .withGlobalUndo() + .compute { + val resourceMoves = edit.documentChanges.orEmpty().filter { it.isRight } + val targets = + resourceMoves + .mapNotNull { + (it.right as? RenameFile)?.let { move -> + Path.of(URI(move.oldUri)) to Path.of(URI(move.newUri)) + } + }.toMap() + val local = LocalFileSystem.getInstance() + val resolved = + targets + .mapNotNull { (from, to) -> + val file = local.findFileByNioFile(from) ?: return@mapNotNull null + val parent = + local.findFileByNioFile(to.parent) ?: return@mapNotNull null + from to (file to parent) + }.toMap() + if ( + !snapshot.isCurrent() || + graph?.isCurrent() == false || + resourceMoves.size != targets.size || + resolved.size != targets.size || + (targets.isNotEmpty() && !FileMoveTargets.valid(targets)) + ) { + false + } else { + val closed = ClosedRefactoringDocuments.capture(project, edit) ?: return@compute false + closed.beforeApply() + graph?.beforeApply() + // TODO LSP4IJ: UP03 — 0.21 only renames the basename, ignoring a changed parent URI. + // Apply resource moves through VFS inside this same undo command. + // Scope the already approved physical operation so the VFS listener does + // not request another compiler edit while this write command holds the EDT. + val preflighted = (snapshot.wrapper.clientFeatures as XtcClientFeatures).preflightedRenames + edit.documentChanges.orEmpty().forEach { change -> + if (change.isRight && change.right is RenameFile) { + val move = change.right as RenameFile + val (file, parent) = resolved.getValue(Path.of(URI(move.oldUri))) + val target = Path.of(URI(move.newUri)) + if (file.parent != parent) { + preflighted.apply(Path.of(file.path), Path.of(parent.path, file.name)) { + file.move(this, parent) + } + } + if (file.name != target.fileName.toString()) { + preflighted.apply(Path.of(file.path), target) { + file.rename(this, target.fileName.toString()) + } + } + } else { + LSPIJUtils.applyWorkspaceEdit(WorkspaceEdit(listOf(change))) + } + } + if (edit.documentChanges == null) LSPIJUtils.applyWorkspaceEdit(edit) + graph?.apply() + closed.afterApply() + true + } + } + } + + private data class Buffer( + val opened: OpenedDocument, + val synchronizer: DocumentContentSynchronizer, + val stamp: Long, + val url: String, + ) { + fun isCurrent(): Boolean = + opened.file.isValid && + opened.file.url == url && + opened.synchronizer === synchronizer && + synchronizer.document.modificationStamp == stamp + } + + private data class FileMove( + val file: VirtualFile, + val url: String, + val stamp: Long, + val target: Path, + ) { + fun isCurrent(): Boolean = + file.isValid && + file.url == url && + file.modificationStamp == stamp && + FileMoveTargets.valid(mapOf(Path.of(file.path) to target)) + } + + private data class Snapshot( + val wrapper: LanguageServerWrapper, + val server: LanguageServer, + val buffers: List, + val moves: List = emptyList(), + ) { + fun isCurrent(): Boolean = + !wrapper.isDisposed && + wrapper.languageServer === server && + wrapper.openedDocuments.toSet() == buffers.map { it.opened }.toSet() && + buffers.all(Buffer::isCurrent) && + moves.all(FileMove::isCurrent) + + fun flush(): CompletableFuture = + CompletableFuture.allOf( + *buffers + .map { it.synchronizer } + .map { sync -> + sync.didOpenFuture.thenCompose { sync.flushPendingChanges() } + }.toTypedArray(), + ) + } + + companion object { + /** A selected lazy action shares Rename's document epoch and undo-command safeguards. */ + fun requestAction( + wrapper: LanguageServerWrapper, + action: CodeAction, + ): CompletableFuture = + ReadAction.computeBlocking, RuntimeException> { + val snapshot = capture(wrapper) + val cancellation = CancellationSupport() + val result = + snapshot + .flush() + .thenCompose { + cancellation.checkCanceled() + cancellation.execute( + snapshot.server.textDocumentService.resolveCodeAction(action), + ) + }.thenApply { resolved -> + resolved.edit?.let { + XtcRenameEdit(snapshot, it, command = resolved.title) + } + } + result.whenComplete { _, _ -> if (result.isCancelled) cancellation.cancel() } + result + } + + private fun capture(wrapper: LanguageServerWrapper): Snapshot = + Snapshot( + wrapper, + requireNotNull(wrapper.languageServer), + wrapper.openedDocuments.map { opened -> + val synchronizer = + requireNotNull(opened.synchronizer) { + "Rename source is no longer open" + } + Buffer( + opened, + synchronizer, + synchronizer.document.modificationStamp, + opened.file.url, + ) + }, + ) + + /** Rename is the single-source special case of a preflighted file operation. */ + fun requestFileRename( + wrapper: LanguageServerWrapper, + file: VirtualFile, + name: String, + ): CompletableFuture { + require( + name.isNotBlank() && name != "." && name != ".." && '/' !in name && '\\' !in name, + ) { + "Enter a single file or directory name" + } + return requestFileMoves( + wrapper, + mapOf(file to Path.of(file.parent.path, name)), + "Rename", + ) + } + + /** Request before disk mutation; VFS before-events are already too late for LSP proof. */ + fun requestFileMoves( + wrapper: LanguageServerWrapper, + requested: Map, + command: String = "Move", + ): CompletableFuture = + ReadAction.computeBlocking, RuntimeException> { + if (!FileMoveTargets.valid(requested.mapKeys { Path.of(it.key.path) })) { + return@computeBlocking CompletableFuture.completedFuture(null) + } + val projectRoot = wrapper.project.basePath?.let(Path::of) + if (projectRoot != null && requested.keys.any { projectRoot.startsWith(Path.of(it.path)) }) { + return@computeBlocking CompletableFuture.completedFuture(null) + } + val moves = + requested.map { (file, target) -> + FileMove(file, file.url, file.modificationStamp, target) + } + val snapshot = capture(wrapper).copy(moves = moves) + val operations = + moves.map { + FileRename( + wrapper.toUriString(it.file), + it.target + .toFile() + .toURI() + .toString(), + ) + } + val graph = SourceGraphEdit.capture(wrapper.project, wrapper.serverDefinition.id) + val cancellation = CancellationSupport() + val result = + snapshot + .flush() + .thenCompose { + cancellation.checkCanceled() + val server = snapshot.server + cancellation.execute( + if (server is XtcLanguageServer) { + server.renameFilesProposal(RenameFilesParams(operations)) + } else { + server.workspaceService.willRenameFiles(RenameFilesParams(operations)).thenApply { edit -> + edit?.let { + check(it.changes.isNullOrEmpty()) { "File operations require versioned edits" } + RenameProposal( + WorkspaceEdit().apply { + documentChanges = + buildList { + addAll(it.documentChanges.orEmpty()) + addAll( + operations.map { move -> + Either.forRight(RenameFile(move.oldUri, move.newUri)) + }, + ) + } + }, + ) + } + } + }, + ) + }.thenApply { proposal -> + proposal?.let { + check(it.edit.changes.isNullOrEmpty()) { "File operations require versioned edits" } + XtcRenameEdit(snapshot, it.edit, it.graph?.let(graph::replacement), command) + } + } + result.whenComplete { _, _ -> if (result.isCancelled) cancellation.cancel() } + result + } + + /** + * Capture before flushing changes or sending the request. Comparing only after the response + * arrives misses edits made while the compiler was calculating the rename. Open/close + * epochs are represented by the existing OpenedDocument identities. + */ + @JvmStatic + fun request( + wrapper: LanguageServerWrapper, + file: VirtualFile, + offset: Int, + name: String, + ): CompletableFuture = + ReadAction.computeBlocking, RuntimeException> { + val document = requireNotNull(LSPIJUtils.getDocument(file)) + requireNotNull(wrapper.getOpenedDocument(wrapper.toUri(file))) { + "Rename source is no longer open" + } + val snapshot = capture(wrapper) + val params = + RenameParams( + TextDocumentIdentifier(wrapper.toUriString(file)), + LSPIJUtils.toPosition(offset, document), + name, + ) + val graph = SourceGraphEdit.capture(wrapper.project, wrapper.serverDefinition.id) + val cancellation = CancellationSupport() + val result = + snapshot + .flush() + .thenCompose { + cancellation.checkCanceled() + val server = snapshot.server + cancellation.execute( + if (server is XtcLanguageServer) { + server.renameProposal(params) + } else { + server.textDocumentService.rename(params).thenApply { + it?.let(::RenameProposal) + } + }, + ) + }.thenApply { proposal -> + val edit = proposal?.edit + val hasEdits = + edit != null && + ( + !edit.documentChanges.isNullOrEmpty() || + !edit.changes.isNullOrEmpty() + ) + if (hasEdits) { + XtcRenameEdit( + snapshot, + edit, + proposal.graph?.let(graph::replacement), + ) + } else { + null + } + } + result.whenComplete { _, _ -> if (result.isCancelled) cancellation.cancel() } + result + } + } + } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt new file mode 100644 index 00000000000..e588f1e6b18 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt @@ -0,0 +1,259 @@ +package org.xtclang.idea.lsp + +import com.intellij.codeInsight.hint.HintManager +import com.intellij.openapi.actionSystem.CommonDataKeys +import com.intellij.openapi.actionSystem.DataContext +import com.intellij.openapi.application.ApplicationManager +import com.intellij.openapi.application.ReadAction +import com.intellij.openapi.editor.Editor +import com.intellij.openapi.fileTypes.FileTypes +import com.intellij.openapi.project.Project +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import com.intellij.refactoring.rename.RenameHandler +import com.intellij.refactoring.ui.NameSuggestionsField +import com.intellij.refactoring.ui.RefactoringDialog +import com.redhat.devtools.lsp4ij.LSPIJUtils +import com.redhat.devtools.lsp4ij.LanguageServerItem +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import com.redhat.devtools.lsp4ij.internal.CompletableFutures +import org.eclipse.lsp4j.PrepareRenameParams +import org.eclipse.lsp4j.RenameOptions +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.messages.Either +import java.util.concurrent.CompletableFuture +import javax.swing.JComponent + +// TODO LSP4IJ: UP04 — apply rename edits only after checking captured document versions/epochs. +// This native handler can go once upstream provides the guarded application used by XtcRenameEdit. + +/** Uses Community platform refactoring UI with an atomic stale-document guard. */ +class XtcRenameHandler : RenameHandler { + override fun isAvailableOnDataContext(dataContext: DataContext): Boolean { + val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return false + return file.virtualFile?.extension == "x" && + CommonDataKeys.EDITOR.getData(dataContext) != null && + LanguageServiceAccessor.getInstance(file.project).hasAny(file) { wrapper -> + wrapper.serverDefinition.id == SERVER_ID && + wrapper.clientFeatures.renameFeature.isEnabled(file) && + supportsRename(wrapper.serverCapabilitiesSync?.renameProvider) + } + } + + override fun invoke( + project: Project, + editor: Editor, + file: PsiFile, + dataContext: DataContext, + ) { + val offset = editor.caretModel.offset + val stamp = editor.document.modificationStamp + val future = + LanguageServiceAccessor + .getInstance(project) + .getLanguageServers( + file, + { + it.isServerDefinition(SERVER_ID) && + it.isEnabled(file.virtualFile) && + it.renameFeature.isEnabled(file) + }, + { supportsRename(it.serverWrapper.serverCapabilitiesSync?.renameProvider) }, + ).thenCompose { servers -> + val server = + servers.singleOrNull() + ?: return@thenCompose CompletableFuture.completedFuture(null) + prepare(server, file, editor, offset) + } + CompletableFutures.waitUntilDoneAsync(future, "Preparing rename", file) + future.whenComplete { prepared, error -> + ApplicationManager.getApplication().invokeLater { + if (project.isDisposed || editor.isDisposed) return@invokeLater + when { + error != null -> { + HintManager + .getInstance() + .showErrorHint( + editor, + "Rename failed: ${error.cause?.message ?: error.message}", + ) + } + + stamp != editor.document.modificationStamp -> { + HintManager + .getInstance() + .showErrorHint( + editor, + "Source changed; invoke Rename again.", + ) + } + + prepared == null -> { + HintManager + .getInstance() + .showErrorHint(editor, "This symbol cannot be renamed.") + } + + else -> { + RenameDialog(file, editor, offset, prepared).show() + } + } + } + } + } + + override fun invoke( + project: Project, + elements: Array, + dataContext: DataContext, + ) { + val editor = CommonDataKeys.EDITOR.getData(dataContext) ?: return + val file = CommonDataKeys.PSI_FILE.getData(dataContext) ?: return + invoke(project, editor, file, dataContext) + } + + private data class Prepared( + val server: LanguageServerItem, + val name: String, + ) + + private fun prepare( + server: LanguageServerItem, + file: PsiFile, + editor: Editor, + offset: Int, + ): CompletableFuture { + if (!server.isPrepareRenameSupported) { + return CompletableFuture.completedFuture( + ReadAction.computeBlocking { + LSPIJUtils.getWordRangeAt(editor.document, file, offset)?.let { + Prepared(server, editor.document.getText(it)) + } + }, + ) + } + val wrapper = server.serverWrapper + val opened = + wrapper.getOpenedDocument(wrapper.toUri(file.virtualFile)) + ?: return CompletableFuture.completedFuture(null) + val synchronizer = opened.synchronizer ?: return CompletableFuture.completedFuture(null) + val params = + ReadAction.computeBlocking { + PrepareRenameParams( + TextDocumentIdentifier(wrapper.toUriString(file.virtualFile)), + LSPIJUtils.toPosition(offset, editor.document), + ) + } + return synchronizer.didOpenFuture + .thenCompose { synchronizer.flushPendingChanges() } + .thenCompose { server.textDocumentService.prepareRename(params) } + .thenApply { result -> + if (result == null) return@thenApply null + ReadAction.computeBlocking { + val name = + when { + result.isSecond -> { + result.second.placeholder + } + + result.isFirst -> { + LSPIJUtils + .toTextRange(result.first, editor.document) + ?.let(editor.document::getText) + } + + result.third.isDefaultBehavior -> { + LSPIJUtils + .getWordRangeAt( + editor.document, + file, + offset, + )?.let(editor.document::getText) + } + + else -> { + null + } + } + name?.let { Prepared(server, it) } + } + } + } + + private class RenameDialog( + private val file: PsiFile, + private val editor: Editor, + private val offset: Int, + private val prepared: Prepared, + ) : RefactoringDialog(file.project, false) { + private val stamp = editor.document.modificationStamp + private val name = + NameSuggestionsField(arrayOf(prepared.name), file.project, FileTypes.PLAIN_TEXT, editor) + + init { + title = "Rename" + name.addDataChangedListener { validateButtons() } + init() + } + + override fun createCenterPanel(): JComponent = name + + override fun getPreferredFocusedComponent(): JComponent = name.focusableComponent + + override fun hasPreviewButton(): Boolean = false + + override fun areButtonsValid(): Boolean = name.enteredName.isNotBlank() && name.enteredName != prepared.name + + override fun doAction() { + if (editor.document.modificationStamp != stamp) { + setErrorText("Source changed; cancel and invoke Rename again.") + return + } + val future = + XtcRenameEdit.request( + prepared.server.serverWrapper, + file.virtualFile, + offset, + name.enteredName.trim(), + ) + close(OK_EXIT_CODE) + CompletableFutures.waitUntilDoneAsync(future, "Renaming ${prepared.name}", file) + future.whenComplete { edit, error -> + ApplicationManager.getApplication().invokeLater { + if (file.project.isDisposed || editor.isDisposed) return@invokeLater + when { + error != null -> { + HintManager + .getInstance() + .showErrorHint( + editor, + "Rename failed: ${error.cause?.message ?: error.message}", + ) + } + + edit == null -> { + HintManager + .getInstance() + .showErrorHint( + editor, + "This rename is not supported or would change another binding.", + ) + } + + !edit.apply() -> { + HintManager + .getInstance() + .showErrorHint(editor, "Sources changed; invoke Rename again.") + } + } + } + } + } + } + + private companion object { + const val SERVER_ID = "xtcLanguageServer" + + fun supportsRename(provider: Either?): Boolean = provider?.let { it.isRight || it.left == true } == true + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcResolveCodeAction.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcResolveCodeAction.kt new file mode 100644 index 00000000000..28d0791e245 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcResolveCodeAction.kt @@ -0,0 +1,24 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.actionSystem.AnActionEvent +import com.redhat.devtools.lsp4ij.commands.LSPCommand +import com.redhat.devtools.lsp4ij.commands.LSPCommandAction +import org.eclipse.lsp4j.CodeAction + +/** Only action selection resolves and applies an edit; the normal intention UI owns selection. */ +class XtcResolveCodeAction : LSPCommandAction() { + override fun commandPerformed( + command: LSPCommand, + event: AnActionEvent, + ) { + val project = event.project ?: return + val wrapper = getLanguageServer(event)?.serverWrapper ?: return + if (wrapper.serverDefinition.id != CompilerSettings.SERVER_ID) return + val action = command.getArgumentAt(0, CodeAction::class.java) ?: return + XtcFileOperations.submit( + project, + action.title, + XtcRenameEdit.requestAction(wrapper, action), + ) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcServerLogAction.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcServerLogAction.kt new file mode 100644 index 00000000000..40993bb3b33 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcServerLogAction.kt @@ -0,0 +1,34 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.actionSystem.ActionUpdateThread +import com.intellij.openapi.actionSystem.AnActionEvent +import com.intellij.openapi.project.DumbAwareAction +import com.intellij.openapi.wm.ToolWindowManager +import com.redhat.devtools.lsp4ij.LanguageServersRegistry +import com.redhat.devtools.lsp4ij.console.LSPConsoleToolWindowPanel +import com.redhat.devtools.lsp4ij.ui.LSPToolWindowId + +/** Reuse the connection's log console, including output retained after a server failure. */ +class XtcServerLogAction : DumbAwareAction() { + override fun getActionUpdateThread() = ActionUpdateThread.BGT + + override fun update(event: AnActionEvent) { + event.presentation.isEnabled = event.project != null + } + + override fun actionPerformed(event: AnActionEvent) { + val project = event.project ?: return + val window = + ToolWindowManager.getInstance(project).getToolWindow(LSPToolWindowId.LANGUAGE_SERVERS) + ?: return + if (window.isVisible) { + window.hide() + } else { + val definition = + LanguageServersRegistry + .getInstance() + .getServerDefinition(CompilerSettings.SERVER_ID) ?: return + window.activate { LSPConsoleToolWindowPanel.selectLogTab(definition, project) } + } + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizard.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizard.kt index 71ef7bbf88f..bfe44aa1c97 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizard.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizard.kt @@ -10,8 +10,8 @@ import org.xtclang.idea.XtcIconProvider import javax.swing.Icon /** - * Project generator for the New Project wizard in IntelliJ IDEA. - * Creates XTC projects by invoking the `xtc init` CLI command. + * Project generator for the New Project wizard in IntelliJ IDEA. Creates XTC projects by invoking + * the `xtc init` CLI command. */ class XtcNewProjectWizard : GeneratorNewProjectWizard { // `id` is the internal generator key used by IntelliJ to identify this wizard @@ -19,7 +19,8 @@ class XtcNewProjectWizard : GeneratorNewProjectWizard { // label shown in the New Project wizard's generator list. override val id = "XTC" override val name = "Ecstasy" - override val icon: Icon = XtcIconProvider.XTC_ICON ?: com.intellij.icons.AllIcons.FileTypes.Any_type + override val icon: Icon = + XtcIconProvider.XTC_ICON ?: com.intellij.icons.AllIcons.FileTypes.Any_type override val ordinal = 1000 override fun isEnabled() = true diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizardStep.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizardStep.kt index bdccaa60638..b72b818de69 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizardStep.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/project/XtcNewProjectWizardStep.kt @@ -20,15 +20,16 @@ import org.xvm.tool.XtcProjectCreator import kotlin.io.path.Path /** - * XTC-specific wizard step for the New Project wizard. - * Uses XtcProjectCreator (synced from javatools, compiled for Java 21). + * XTC-specific wizard step for the New Project wizard. Uses XtcProjectCreator (synced from + * javatools, compiled for Java 21). */ class XtcNewProjectWizardStep( parent: NewProjectWizardStep, ) : AbstractNewProjectWizardStep(parent) { private val logger = logger() - private val projectTypeProperty = propertyGraph.property(XtcProjectCreator.ProjectType.APPLICATION) + private val projectTypeProperty = + propertyGraph.property(XtcProjectCreator.ProjectType.APPLICATION) private val multiModuleProperty = propertyGraph.property(false) var projectType: XtcProjectCreator.ProjectType by projectTypeProperty @@ -37,7 +38,8 @@ class XtcNewProjectWizardStep( override fun setupUI(builder: Panel) { builder.apply { row("Project type:") { - comboBox(XtcProjectCreator.ProjectType.entries.toList()).bindItem(projectTypeProperty) + comboBox(XtcProjectCreator.ProjectType.entries.toList()) + .bindItem(projectTypeProperty) } row { checkBox("Multi-module project").bindSelected(multiModuleProperty) @@ -49,10 +51,11 @@ class XtcNewProjectWizardStep( val base = baseData ?: return logger.error("No base data available") val projectPath = Path(base.path).resolve(base.name) val xtcVersion = - PluginPaths.selfDescriptor()?.version - ?: XtcProjectCreator.DEFAULT_XTC_VERSION + PluginPaths.selfDescriptor()?.version ?: XtcProjectCreator.DEFAULT_XTC_VERSION - logger.info("Creating Ecstasy project: path=$projectPath, type=$projectType, multiModule=$multiModule, xtcVersion=$xtcVersion") + logger.info( + "Creating Ecstasy project: path=$projectPath, type=$projectType, multiModule=$multiModule, xtcVersion=$xtcVersion", + ) val creator = XtcProjectCreator(projectPath, projectType, multiModule, xtcVersion, null) val result = creator.create() @@ -66,7 +69,10 @@ class XtcNewProjectWizardStep( else -> { logger.error("Failed to create Ecstasy project: ${result.message}") - Messages.showErrorDialog("Failed to create Ecstasy project: ${result.message}", "Ecstasy Project Creation Failed") + Messages.showErrorDialog( + "Failed to create Ecstasy project: ${result.message}", + "Ecstasy Project Creation Failed", + ) } } } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfiguration.kt index a80681257e9..d54fa076354 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfiguration.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfiguration.kt @@ -18,10 +18,7 @@ import com.intellij.ui.dsl.builder.panel import org.jdom.Element import kotlin.io.path.Path -/** - * Run configuration for XTC applications. - * Invokes `xtc run` or the Gradle `runXtc` task. - */ +/** Run configuration for XTC applications. Invokes `xtc run` or the Gradle `runXtc` task. */ class XtcRunConfiguration( project: Project, factory: ConfigurationFactory, @@ -97,9 +94,7 @@ class XtcRunConfiguration( } } -/** - * Settings editor for XTC run configuration using Kotlin UI DSL. - */ +/** Settings editor for XTC run configuration using Kotlin UI DSL. */ class XtcRunSettingsEditor : SettingsEditor() { private var moduleName = "" private var methodName = "" @@ -112,7 +107,9 @@ class XtcRunSettingsEditor : SettingsEditor() { row("Module name:") { textField() .bindText(::moduleName) - .comment("The Ecstasy (.xtc) module to run (overrides build.gradle.kts default)") + .comment( + "The Ecstasy (.xtc) module to run (overrides build.gradle.kts default)", + ) } row("Method name:") { textField() diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationProducer.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationProducer.kt index 65a0f6e240e..676700fa997 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationProducer.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationProducer.kt @@ -8,8 +8,8 @@ import com.intellij.psi.PsiElement import com.intellij.psi.PsiFile /** - * Produces XTC run configurations automatically when the user invokes - * Run (Ctrl+Shift+R) from a .x file context. + * Produces XTC run configurations automatically when the user invokes Run (Ctrl+Shift+R) from a .x + * file context. * * This allows users to run XTC modules without manually creating run configurations. */ @@ -56,8 +56,8 @@ class XtcRunConfigurationProducer : LazyRunConfigurationProducer" declaration in the file content. + * Extract the module name from an XTC source file. Looks for "module " declaration in the + * file content. */ private fun extractModuleName(file: PsiFile): String? { val text = file.text diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationType.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationType.kt index 1a1c479f882..bfb2f9f2f8b 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationType.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/run/XtcRunConfigurationType.kt @@ -10,10 +10,9 @@ import javax.swing.Icon /** * Run configuration type for Ecstasy applications. * - * `getId()` and `XtcConfigurationFactory.getId()` are internal identifiers - * persisted in workspace.xml across sessions — they MUST remain stable - * ("XtcRunConfiguration", "XtcConfigurationFactory") even though everything - * user-facing now reads "Ecstasy". + * `getId()` and `XtcConfigurationFactory.getId()` are internal identifiers persisted in + * workspace.xml across sessions — they MUST remain stable ("XtcRunConfiguration", + * "XtcConfigurationFactory") even though everything user-facing now reads "Ecstasy". */ class XtcRunConfigurationType : ConfigurationType { override fun getDisplayName() = "Ecstasy Application" @@ -27,9 +26,7 @@ class XtcRunConfigurationType : ConfigurationType { override fun getConfigurationFactories(): Array = arrayOf(XtcConfigurationFactory(this)) } -/** - * Factory for creating Ecstasy run configurations. - */ +/** Factory for creating Ecstasy run configurations. */ class XtcConfigurationFactory( type: ConfigurationType, ) : ConfigurationFactory(type) { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcCodeStyleSettings.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcCodeStyleSettings.kt index 695d41afa7f..f40ddad5ac9 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcCodeStyleSettings.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcCodeStyleSettings.kt @@ -6,18 +6,18 @@ import com.intellij.psi.codeStyle.CustomCodeStyleSettings /** * XTC-specific code style settings that extend IntelliJ's standard settings. * - * These settings appear under Settings > Editor > Code Style > Ecstasy and control - * XTC-specific formatting options beyond what [com.intellij.psi.codeStyle.CommonCodeStyleSettings] - * provides. The LSP server reads these values when no project-level `xtc-format.toml` is present. + * These settings appear under Settings > Editor > Code Style > Ecstasy and control XTC-specific + * formatting options beyond what [com.intellij.psi.codeStyle.CommonCodeStyleSettings] provides. The + * LSP server reads these values when no project-level `xtc-format.toml` is present. */ class XtcCodeStyleSettings( container: CodeStyleSettings, ) : CustomCodeStyleSettings("XtcCodeStyleSettings", container) { /** - * Continuation indent for `extends`, `implements`, `incorporates`, `delegates` lines. - * XTC convention: double the normal indent (8 spaces by default). + * Continuation indent for `extends`, `implements`, `incorporates`, `delegates` lines. XTC + * convention: double the normal indent (8 spaces by default). */ @JvmField - @Suppress("ktlint:standard:property-naming") // IntelliJ settings serialization requires UPPER_SNAKE_CASE + @Suppress("ktlint:standard:property-naming") // IntelliJ settings serialization convention. var CONTINUATION_INDENT_SIZE: Int = 8 } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcLanguageCodeStyleSettingsProvider.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcLanguageCodeStyleSettingsProvider.kt index b8522119ca7..c49f7d92d8c 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcLanguageCodeStyleSettingsProvider.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/style/XtcLanguageCodeStyleSettingsProvider.kt @@ -15,8 +15,8 @@ import org.xtclang.idea.XtcIntelliJLanguage * - No tab characters * - 120-character right margin * - * These defaults match the XTC standard library source style. When a project-level - * `xtc-format.toml` config file is present, it takes precedence over these IDE settings. + * The compiler formatter uses the right margin for token-boundary expression/list wrapping. + * Literal/comment reflow and `xtc-format.toml` configuration are not implemented. */ class XtcLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider() { override fun getLanguage(): Language = XtcIntelliJLanguage @@ -42,9 +42,9 @@ class XtcLanguageCodeStyleSettingsProvider : LanguageCodeStyleSettingsProvider() "CONTINUATION_INDENT_SIZE", "TAB_SIZE", "USE_TAB_CHARACTER", - "SMART_TABS", - "KEEP_INDENTS_ON_EMPTY_LINES", ) + } else if (settingsType == SettingsType.WRAPPING_AND_BRACES_SETTINGS) { + consumer.showStandardOptions("RIGHT_MARGIN") } } diff --git a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml index b395f121208..ab04a3d19b9 100644 --- a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml +++ b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml @@ -31,7 +31,42 @@ com.redhat.devtools.lsp4ij + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -79,7 +138,7 @@ @@ -87,10 +146,10 @@ diff --git a/lang/intellij-plugin/src/main/resources/liveTemplates/XTC.xml b/lang/intellij-plugin/src/main/resources/liveTemplates/XTC.xml index b77ca608677..6ac354047c4 100644 --- a/lang/intellij-plugin/src/main/resources/liveTemplates/XTC.xml +++ b/lang/intellij-plugin/src/main/resources/liveTemplates/XTC.xml @@ -1,4 +1,4 @@ - + diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt new file mode 100644 index 00000000000..a74b4dc12e2 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt @@ -0,0 +1,96 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeActionCapabilities +import org.eclipse.lsp4j.CodeActionResolveSupportCapabilities +import org.eclipse.lsp4j.CompletionCapabilities +import org.eclipse.lsp4j.CompletionItemCapabilities +import org.eclipse.lsp4j.CompletionItemInsertTextModeSupportCapabilities +import org.eclipse.lsp4j.CompletionItemResolveSupportCapabilities +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InsertTextMode +import org.eclipse.lsp4j.SynchronizationCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.junit.jupiter.api.Test + +class ClientCapabilitiesTest { + @Test + fun `advertise the indentation mode implemented by native snippet expansion`() { + val completion = + CompletionCapabilities().apply { + completionItem = + CompletionItemCapabilities().apply { + snippetSupport = true + insertTextModeSupport = CompletionItemInsertTextModeSupportCapabilities(InsertTextMode.entries) + } + } + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = TextDocumentClientCapabilities().apply { this.completion = completion } + } + } + XtcLanguageServerFactory().createClientFeatures().initializeParams(params) + assertThat(completion.insertTextMode).isEqualTo(InsertTextMode.AdjustIndentation) + assertThat(completion.completionItem.insertTextModeSupport.valueSet).containsExactly(InsertTextMode.AdjustIndentation) + assertThat(completion.completionItem.snippetSupport).isTrue() + } + + @Test + fun `do not advertise save hooks that LSP4IJ does not dispatch`() { + val sync = + SynchronizationCapabilities().apply { + willSave = true + willSaveWaitUntil = true + didSave = true + } + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { synchronization = sync } + } + } + XtcLanguageServerFactory().createClientFeatures().initializeParams(params) + assertThat(sync.willSave).isFalse() + assertThat(sync.willSaveWaitUntil).isFalse() + assertThat(sync.didSave).isTrue() + } + + @Test + fun `negotiate lazy action edits and completion documentation resolve`() { + val actions = CodeActionResolveSupportCapabilities(listOf("edit", "command")) + val completion = CompletionItemResolveSupportCapabilities(listOf("documentation")) + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + codeAction = + CodeActionCapabilities().apply { resolveSupport = actions } + this.completion = + CompletionCapabilities().apply { + completionItem = + CompletionItemCapabilities().apply { + resolveSupport = completion + } + } + } + } + } + XtcLanguageServerFactory().createClientFeatures().initializeParams(params) + assertThat(actions.properties).containsExactly("edit", "command") + assertThat(completion.properties).containsExactly("documentation") + } + + @Test + fun `accept clients without action resolve capabilities`() { + val params = InitializeParams().apply { capabilities = ClientCapabilities() } + XtcLanguageServerFactory().createClientFeatures().initializeParams(params) + assertThat(params.capabilities.textDocument).isNull() + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt new file mode 100644 index 00000000000..2e2158e72ec --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt @@ -0,0 +1,42 @@ +package org.xtclang.idea.lsp + +import com.redhat.devtools.lsp4ij.JSONUtils +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.CodeAction +import org.eclipse.lsp4j.Command +import org.eclipse.lsp4j.WorkspaceEdit +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test + +class CodeActionMessagesTest { + @Test + fun `lazy selection carries original action without resolving or applying while listing`() { + val lazy = + CodeAction("Generate method").apply { + data = "detached-handle" + kind = "quickfix" + } + val eager = CodeAction("Import type").apply { edit = WorkspaceEdit(emptyMap()) } + val command = + CodeAction("Client action").apply { this.command = Command("Run", "existing") } + val message = + ResponseMessage().apply { + id = "7" + result = + listOf(lazy, eager, command).map { Either.forRight(it) } + } + CodeActionMessages.incoming { assertThat(it).isSameAs(message) }.consume(message) + assertThat(lazy.edit).isNull() + assertThat(lazy.command.command).isEqualTo(CodeActionMessages.COMMAND) + val original = JSONUtils.toModel(lazy.command.arguments.single(), CodeAction::class.java) + assertThat(original.data.toString()).contains("detached-handle") + assertThat(original.command).isNull() + assertThat(original.kind).isEqualTo("quickfix") + assertThat(eager.command).isNull() + assertThat(command.command.command).isEqualTo("existing") + val first = lazy.command + CodeActionMessages.incoming {}.consume(message) + assertThat(lazy.command).isSameAs(first) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt new file mode 100644 index 00000000000..82be4394ae1 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt @@ -0,0 +1,65 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class CompilerBuildModelTest { + private val entry = + mapOf( + "projectId" to "file:///workspace/#:library", + "projectPath" to ":library", + "projectDirectory" to "file:///workspace/library/", + "buildFile" to "file:///workspace/library/build.gradle.kts", + "sourceSet" to "main", + "sourceRoots" to listOf("file:///outside/custom-source/"), + "sourceFiles" to listOf("file:///outside/custom-source/Library.x"), + "moduleRoots" to listOf("file:///outside/custom-source/Library.x"), + "resourceSourceRoots" to listOf("file:///outside/assets/"), + "resourceRoots" to listOf("file:///workspace/build/processed/", "file:///fallback/"), + "resourceTask" to ":library:processXtcResources", + "projectDependencies" to emptyList(), + "modulePath" to listOf("file:///outside/Library.xtc"), + ) + + private fun text( + entries: List> = listOf(entry), + version: Int = 1, + ) = Gson().toJson(mapOf("schemaVersion" to version, "sourceSets" to entries)) + + @Test + fun `evaluated model preserves external paths resource precedence and main test ownership`() { + val model = CompilerBuildModel.parse(text(listOf(entry, entry + ("sourceSet" to "test")))) + assertThat(model["sourceSets"].asJsonArray).hasSize(2) + assertThat( + model["sourceSets"].asJsonArray[0].asJsonObject["resourceRoots"].asJsonArray.map { + it.asString + }, + ).containsExactly("file:///workspace/build/processed/", "file:///fallback/") + } + + @Test + fun `overlapping composite and linked roots deduplicate only identical owners`() { + val first = CompilerBuildModel.parse(text()) + val second = CompilerBuildModel.parse(text(listOf(entry + ("projectId" to "file:///other/#:library")))) + assertThat(CompilerWorkspaceModels.merge(listOf(first, first.deepCopy(), second))["sourceSets"].asJsonArray).hasSize(2) + val conflict = CompilerBuildModel.parse(text(listOf(entry + ("resourceRoots" to emptyList())))) + assertThatThrownBy { CompilerWorkspaceModels.merge(listOf(first, conflict)) }.hasMessageContaining("Conflicting") + } + + @Test + fun `unsupported malformed and duplicate models are refused before configuration changes`() { + listOf( + text(version = 2), + text(listOf(entry, entry)), + text(listOf(entry + ("sourceRoots" to listOf("relative/path")))), + text(listOf(entry + ("projectId" to 42))), + text(listOf(entry - "resourceRoots")), + text(listOf(entry + ("projectDependencies" to listOf(1)))), + ).forEach { value -> + assertThatThrownBy { CompilerBuildModel.parse(value) } + .isInstanceOf(IllegalArgumentException::class.java) + } + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt new file mode 100644 index 00000000000..131261ddcce --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt @@ -0,0 +1,18 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.nio.file.Path + +class CompilerBuildUpdatesTest { + @Test + fun `atomic report replacement and removal affect inputs but unrelated paths do not`() { + val root = Path.of("/workspace/app") + listOf(".gradle/xtc/lsp-model.json", ".gradle/xtc", ".gradle").forEach { + assertThat(CompilerBuildUpdates.affectsModel(root, root.resolve(it))).isTrue() + } + listOf("build.gradle.kts", ".gradle/xtc/temporary.json", "../other/.gradle/xtc/lsp-model.json").forEach { + assertThat(CompilerBuildUpdates.affectsModel(root, root.resolve(it))).isFalse() + } + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt new file mode 100644 index 00000000000..ca12d5a0679 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt @@ -0,0 +1,87 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.progress.EmptyProgressIndicatorBase +import com.intellij.openapi.progress.ProcessCanceledException +import com.intellij.openapi.project.Project +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import java.lang.reflect.Proxy +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class CompilerImportServiceTest { + /** Cancellation ownership is tested without installing an application-wide ProgressManager. */ + private class Indicator : EmptyProgressIndicatorBase(ModalityState.nonModal()) { + private val cancelled = AtomicBoolean() + + override fun cancel() { + cancelled.set(true) + } + + override fun isCanceled(): Boolean = cancelled.get() + } + + private fun service(): CompilerImportService { + val project = + Proxy.newProxyInstance(Project::class.java.classLoader, arrayOf(Project::class.java)) { _, method, _ -> + error("Progress ownership must not access project.${method.name}") + } as Project + return CompilerImportService(project) + } + + @Test + fun `project disposal cancels the owned indicator and rejects later work`() { + val service = service() + val indicator = Indicator() + service.run(indicator) { + service.dispose() + assertThat(indicator.isCanceled).isTrue() + } + assertThatThrownBy { service.run(Indicator()) { error("Must not start") } } + .isInstanceOf(ProcessCanceledException::class.java) + service.dispose() + } + + @Test + fun `unlinking a root cancels its producer and replaces rather than reuses its owner`() { + val service = service() + val owner = service.model + val operation = owner.begin(false) + val indicator = Indicator() + service.run(indicator) { + service.rootsRemoved() + assertThat(indicator.isCanceled).isTrue() + } + assertThat(service.model).isNotSameAs(owner) + assertThat(owner.finish(operation, CompilerImport.Outcome.SUCCEEDED).outcome).isEqualTo(CompilerImport.Outcome.CANCELLED) + service.dispose() + } + + @Test + fun `concurrent imports are refused and a completed owner releases the progress slot`() { + val service = service() + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val first = + CompletableFuture.runAsync { + service.run(Indicator()) { + started.countDown() + check(release.await(5, SECONDS)) + } + } + try { + assertThat(started.await(5, SECONDS)).isTrue() + assertThatThrownBy { service.run(Indicator()) { error("Overlapping import") } } + .hasMessageContaining("already running") + } finally { + release.countDown() + } + first.get(5, SECONDS) + assertThat(service.run(Indicator()) { "new owner" }).isEqualTo("new owner") + service.dispose() + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt new file mode 100644 index 00000000000..18e69b8e35b --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt @@ -0,0 +1,168 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +class CompilerImportTest { + private val disk = AtomicReference("original") + private val imports = CompilerImport(disk::get) { require(it != "invalid") { "Invalid model" } } + + @Test + fun `watchers retain accepted inputs until a successful import finishes`() { + assertThat(imports.current()).isEqualTo("original") + val operation = imports.begin(false) + disk.set("replacement") + assertThat(imports.current()).isEqualTo("original") + assertThat(imports.description()).contains("Refreshing") + assertThatThrownBy { imports.begin(true) }.hasMessageContaining("already running") + assertThat(imports.finish(operation, CompilerImport.Outcome.SUCCEEDED).outcome).isEqualTo(CompilerImport.Outcome.SUCCEEDED) + assertThat(imports.current()).isEqualTo("replacement") + assertThat(imports.description()).contains("Last import:") + } + + @Test + fun `cancelled and failed builds cannot publish even a valid newly written report`() { + listOf(CompilerImport.Outcome.CANCELLED, CompilerImport.Outcome.FAILED).forEach { outcome -> + disk.set("original") + imports.current() + val operation = imports.begin(true) + disk.set("unaccepted") + imports.finish(operation, outcome) + assertThat(imports.current()).isEqualTo("original") + assertThat(imports.current()).isEqualTo("original") + val retry = imports.begin(false) + imports.finish(retry, CompilerImport.Outcome.SUCCEEDED) + assertThat(imports.current()).isEqualTo("unaccepted") + } + } + + @Test + fun `invalid and absent successful reports retain the previous model and allow repair`() { + imports.current() + listOf("invalid", null).forEach { text -> + val operation = imports.begin(false) + disk.set(text) + val result = imports.finish(operation, CompilerImport.Outcome.SUCCEEDED) + assertThat(result.outcome).isEqualTo(CompilerImport.Outcome.FAILED) + assertThat(imports.current()).isEqualTo("original") + } + disk.set("externally repaired") + assertThat(imports.current()).isEqualTo("externally repaired") + disk.set(null) + assertThat(imports.current()).isNull() + } + + @Test + fun `late completion cannot retire a newer import or replace its model`() { + imports.current() + val first = imports.begin(false) + imports.finish(first, CompilerImport.Outcome.CANCELLED) + val next = imports.begin(true) + disk.set("new") + assertThatThrownBy { imports.finish(first, CompilerImport.Outcome.SUCCEEDED) }.hasMessageContaining("no longer owns") + assertThat(imports.current()).isEqualTo("original") + imports.finish(next, CompilerImport.Outcome.SUCCEEDED) + assertThat(imports.current()).isEqualTo("new") + } + + @Test + fun `cancellation during final report validation retains the accepted inputs`() { + imports.current() + val operation = imports.begin(false) + disk.set("new") + assertThat(imports.finish(operation, CompilerImport.Outcome.SUCCEEDED, cancelled = { true }).outcome) + .isEqualTo(CompilerImport.Outcome.CANCELLED) + assertThat(imports.current()).isEqualTo("original") + } + + @Test + fun `late daemon output remains rejected and cannot complete a different import`() { + val owner = CompilerImport(disk::get, importId = { it.substringAfter("@", "").takeIf(String::isNotEmpty) }) {} + owner.current() + val first = owner.begin(true) + owner.finish(first, CompilerImport.Outcome.CANCELLED) + disk.set("late@${first.id}") + assertThat(owner.current()).isEqualTo("original") + val second = owner.begin(false) + assertThat(owner.finish(second, CompilerImport.Outcome.SUCCEEDED).outcome).isEqualTo(CompilerImport.Outcome.FAILED) + assertThat(owner.current()).isEqualTo("original") + val retry = owner.begin(false) + disk.set("accepted@${retry.id}") + assertThat(owner.finish(retry, CompilerImport.Outcome.SUCCEEDED).outcome).isEqualTo(CompilerImport.Outcome.SUCCEEDED) + assertThat(owner.current()).isEqualTo("accepted@${retry.id}") + } + + @Test + fun `retirement prevents late validation or completion from publishing into a closed project`() { + imports.current() + val operation = imports.begin(false) + disk.set("late report") + imports.retire() + assertThat(imports.finish(operation, CompilerImport.Outcome.SUCCEEDED).outcome).isEqualTo(CompilerImport.Outcome.CANCELLED) + assertThat(imports.current()).isNull() + assertThat(imports.retained()).isNull() + assertThatThrownBy { imports.begin(false) }.hasMessageContaining("retired") + } + + @Test + fun `retirement during report validation wins over successful producer exit`() { + val validating = AtomicBoolean() + val captured = CountDownLatch(1) + val release = CountDownLatch(1) + val owner = + CompilerImport(disk::get) { + if (validating.get()) { + captured.countDown() + check(release.await(5, SECONDS)) + } + } + owner.current() + val operation = owner.begin(false) + validating.set(true) + val finished = CompletableFuture.supplyAsync { owner.finish(operation, CompilerImport.Outcome.SUCCEEDED) } + try { + assertThat(captured.await(5, SECONDS)).isTrue() + owner.retire() + } finally { + release.countDown() + } + assertThat(finished.get(5, SECONDS).outcome).isEqualTo(CompilerImport.Outcome.CANCELLED) + assertThat(owner.retained()).isNull() + } + + @Test + fun `a slow watcher cannot overwrite a newer accepted import`() { + val blocked = AtomicBoolean() + val captured = CountDownLatch(1) + val release = CountDownLatch(1) + val owner = + CompilerImport({ + val text = disk.get() + if (blocked.compareAndSet(true, false)) { + captured.countDown() + check(release.await(5, SECONDS)) + } + text + }) {} + owner.current() + disk.set("stale watcher") + blocked.set(true) + val watcher = CompletableFuture.supplyAsync(owner::current) + try { + assertThat(captured.await(5, SECONDS)).isTrue() + val operation = owner.begin(false) + disk.set("fresh import") + owner.finish(operation, CompilerImport.Outcome.SUCCEEDED) + } finally { + release.countDown() + } + assertThat(watcher.get(5, SECONDS)).isEqualTo("fresh import") + assertThat(owner.current()).isEqualTo("fresh import") + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt new file mode 100644 index 00000000000..82572662ecd --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt @@ -0,0 +1,92 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.Registration +import org.junit.jupiter.api.Test +import java.nio.file.Path +import java.util.concurrent.CompletableFuture + +class CompilerRootWatchesTest { + @Test + fun `watch leases share roots replace settings and close exactly once`() { + val acquired = mutableListOf>() + val released = mutableListOf>() + val refreshed = mutableListOf>() + val pending = CompletableFuture() + val watches = + CompilerRootWatches( + { paths -> + acquired.add(paths) + AutoCloseable { released.add(paths) } + }, + { paths -> + refreshed.add(paths) + pending + }, + ) + val a = setOf(Path.of("/external/missing")) + val b = setOf(Path.of("/other/source")) + watches.replace(mapOf("a" to a, "shared" to a)) + watches.replace(emptyMap(), setOf("shared")) + assertThat(acquired).containsExactly(a) + watches.refresh() + watches.refresh() + assertThat(refreshed).containsExactly(a) + watches.replace(mapOf("b" to b), setOf("a")) + assertThat(released).containsExactly(a) + pending.complete(null) + watches.refresh() + assertThat(refreshed).containsExactly(a, b) + watches.close() + watches.close() + watches.replace(mapOf("late" to a)) + watches.refresh() + assertThat(acquired).containsExactly(a, b) + assertThat(released).containsExactly(a, b) + assertThat(refreshed).containsExactly(a, b) + } + + @Test + fun `decode wire registrations including relative patterns without watching global globs`() { + fun registration( + id: String, + glob: String, + ) = Registration( + id, + "workspace/didChangeWatchedFiles", + JsonParser.parseString("""{"watchers":[{"globPattern":$glob}]}"""), + ) + assertThat( + CompilerRootWatches.roots( + registration("xtc-resources-a", "\"/outside/assets/**/*\""), + ), + ).containsExactly(Path.of("/outside/assets")) + assertThat( + CompilerRootWatches.roots( + registration( + "xtc-resources-b", + """{"baseUri":"file:///outside/source/","pattern":"**/*"}""", + ), + ), + ).containsExactly(Path.of("/outside/source")) + assertThat( + CompilerRootWatches.roots( + registration( + "xtc-resources-flat", + """{"baseUri":"file:///","pattern":"generated"}""", + ), + ), + ).isEmpty() + assertThat(CompilerRootWatches.roots(registration("xtc-file-watcher", "\"**/*.x\""))) + .isEmpty() + assertThat( + CompilerRootWatches.roots( + registration( + "xtc-resources-b", + """{"baseUri":"https://example.com/","pattern":"**/*"}""", + ), + ), + ).isEmpty() + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt new file mode 100644 index 00000000000..f33b738a12c --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt @@ -0,0 +1,53 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DidChangeConfigurationParams +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.jsonrpc.json.JsonRpcMethod +import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.junit.jupiter.api.Test + +class ConfigurationJsonTest { + @Test + fun `configuration notification preserves explicit discovery reset on the wire`() { + val settings = JsonParser.parseString("""{"xtc":{"compiler":{"sourceModules":null}}}""") + val message = + NotificationMessage().apply { + method = "workspace/didChangeConfiguration" + params = DidChangeConfigurationParams(settings) + } + val defaults = MessageJsonHandler(emptyMap()) + assertThat(defaults.serialize(message)).doesNotContain("sourceModules") + val wire = json.serialize(message) + val received = JsonParser.parseString(wire).asJsonObject["params"].asJsonObject["settings"] + assertThat(received).isEqualTo(settings) + assertThat(json.parseMessage(wire)).isEqualTo(message) + } + + @Test + fun `unrelated protocol fields keep their normal omission rules`() { + val message = + NotificationMessage().apply { + method = "textDocument/publishDiagnostics" + params = PublishDiagnosticsParams("file:///source.x", emptyList()) + } + assertThat(json.serialize(message)) + .isEqualTo(MessageJsonHandler(emptyMap()).serialize(message)) + assertThat(json.serialize(message)).doesNotContain("version") + } + + private val json = + MessageJsonHandler( + mapOf( + "workspace/didChangeConfiguration" to + JsonRpcMethod.notification( + "workspace/didChangeConfiguration", + DidChangeConfigurationParams::class.java, + ), + ), + ) { + it.registerTypeAdapterFactory(ConfigurationJson) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionLifetimeTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionLifetimeTest.kt index db19ed4bf2c..79417fbd096 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionLifetimeTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionLifetimeTest.kt @@ -14,9 +14,11 @@ class ConnectionLifetimeTest { fun `stopping before asynchronous startup cannot spawn an orphan`() { val started = AtomicInteger() val stopped = AtomicInteger() - val lifetime = ConnectionLifetime({ started.incrementAndGet() }, { stopped.incrementAndGet() }) + val lifetime = + ConnectionLifetime({ started.incrementAndGet() }, { stopped.incrementAndGet() }) lifetime.stop() - assertThatThrownBy { lifetime.start() }.isInstanceOf(CannotStartProcessException::class.java) + assertThatThrownBy { lifetime.start() } + .isInstanceOf(CannotStartProcessException::class.java) lifetime.stop() assertThat(started.get()).isZero() assertThat(stopped.get()).isEqualTo(1) @@ -26,13 +28,15 @@ class ConnectionLifetimeTest { fun `concurrent starts retain a single process and stop is final`() { val started = AtomicInteger() val stopped = AtomicInteger() - val lifetime = ConnectionLifetime({ started.incrementAndGet() }, { stopped.incrementAndGet() }) + val lifetime = + ConnectionLifetime({ started.incrementAndGet() }, { stopped.incrementAndGet() }) Executors.newVirtualThreadPerTaskExecutor().use { executor -> (1..20).map { executor.submit { lifetime.start() } }.forEach { it.get(10, SECONDS) } } lifetime.stop() lifetime.stop() - assertThatThrownBy { lifetime.start() }.isInstanceOf(CannotStartProcessException::class.java) + assertThatThrownBy { lifetime.start() } + .isInstanceOf(CannotStartProcessException::class.java) assertThat(started.get()).isEqualTo(1) assertThat(stopped.get()).isEqualTo(1) } @@ -83,9 +87,12 @@ class ConnectionLifetimeTest { }, { children.decrementAndGet() }, ) - assertThatThrownBy { lifetime.start() }.isInstanceOf(IllegalStateException::class.java).hasMessage("listener setup failed") + assertThatThrownBy { lifetime.start() } + .isInstanceOf(IllegalStateException::class.java) + .hasMessage("listener setup failed") lifetime.stop() assertThat(children.get()).isZero() - assertThatThrownBy { lifetime.start() }.isInstanceOf(CannotStartProcessException::class.java) + assertThatThrownBy { lifetime.start() } + .isInstanceOf(CannotStartProcessException::class.java) } } diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionProcessTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionProcessTest.kt index 5b9fc224250..98ee269a2d8 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionProcessTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConnectionProcessTest.kt @@ -12,14 +12,14 @@ import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS class ConnectionProcessTest { - @TempDir - lateinit var directory: Path + @TempDir lateinit var directory: Path @Test fun `cancelled startup never creates a child in the upstream provider`() { withProvider { provider, lifetime -> lifetime.stop() - assertThatThrownBy { lifetime.start() }.isInstanceOf(CannotStartProcessException::class.java) + assertThatThrownBy { lifetime.start() } + .isInstanceOf(CannotStartProcessException::class.java) assertThat(provider.pid).isNull() } } @@ -35,7 +35,8 @@ class ConnectionProcessTest { lifetime.stop() process.onExit().get(10, SECONDS) assertThat(process.isAlive).isFalse() - assertThatThrownBy { lifetime.start() }.isInstanceOf(CannotStartProcessException::class.java) + assertThatThrownBy { lifetime.start() } + .isInstanceOf(CannotStartProcessException::class.java) } } @@ -46,16 +47,17 @@ class ConnectionProcessTest { "class Probe { public static void main(String[] args) throws Exception { System.in.read(); } }", ) val provider = - object : OSProcessStreamConnectionProvider( - GeneralCommandLine( - ProcessHandle - .current() - .info() - .command() - .orElseThrow(), - source.toString(), - ), - ) { + object : + OSProcessStreamConnectionProvider( + GeneralCommandLine( + ProcessHandle + .current() + .info() + .command() + .orElseThrow(), + source.toString(), + ), + ) { fun child(): Process? = processHandler?.process } try { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt new file mode 100644 index 00000000000..b369cdc5f7f --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt @@ -0,0 +1,117 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParser +import com.redhat.devtools.lsp4ij.JSONUtils +import com.redhat.devtools.lsp4ij.features.diagnostics.LSPDiagnosticUtils +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.CodeActionContext +import org.eclipse.lsp4j.CodeActionParams +import org.eclipse.lsp4j.Diagnostic +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.FullDocumentDiagnosticReport +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.RelatedFullDocumentDiagnosticReport +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.UnchangedDocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test + +class DiagnosticQuickFixesTest { + @Test + fun `presentation preserves decoded full and unchanged companion reports`() { + val diagnostic = Diagnostic(Range(Position(2, 8), Position(2, 15)), "Unknown method") + val companion = FullDocumentDiagnosticReport(emptyList()).apply { resultId = "companion" } + val unchanged = UnchangedDocumentDiagnosticReport("unchanged") + val report = + RelatedFullDocumentDiagnosticReport(listOf(diagnostic)).apply { + resultId = "caller" + relatedDocuments = + mapOf( + "file:///Extract/Other.x" to Either.forLeft(companion), + "file:///Extract/Stable.x" to Either.forRight(unchanged), + ) + } + val response = + ResponseMessage().apply { + id = "pull" + result = DocumentDiagnosticReport(report) + } + val presented = ((DiagnosticQuickFixes.incoming(response) as ResponseMessage).result as DocumentDiagnosticReport).left + assertThat(presented.resultId).isEqualTo(report.resultId) + assertThat(presented.relatedDocuments).isEqualTo(report.relatedDocuments) + assertThat(presented.items.single().data).isNotNull() + assertThat(diagnostic.data).isNull() + assertThat(companion.items).isEmpty() + assertThat(unchanged.resultId).isEqualTo("unchanged") + } + + @Test + fun `identical full reports refresh annotations that own replaced lazy fixes`() { + val diagnostic = Diagnostic(Range(Position(2, 8), Position(2, 15)), "Unknown method") + val report = RelatedFullDocumentDiagnosticReport(listOf(diagnostic)).apply { resultId = "same-result" } + val response = + ResponseMessage().apply { + id = "pull" + result = DocumentDiagnosticReport(report) + } + + fun present() = ((DiagnosticQuickFixes.incoming(response) as ResponseMessage).result as DocumentDiagnosticReport).left.items + val first = present() + val second = present() + assertThat(LSPDiagnosticUtils.isDiagnosticsChanged(report.items, report.items)).isFalse() + assertThat(LSPDiagnosticUtils.isDiagnosticsChanged(first, second)).isTrue() + assertThat(first.single().range).isEqualTo(diagnostic.range) + val gson = JSONUtils.getLsp4jGson() + val displayed = gson.toJsonTree(first.single()).asJsonObject.apply { remove("data") } + assertThat(displayed).isEqualTo(gson.toJsonTree(diagnostic)) + assertThat(diagnostic.data).isNull() + assertThat((response.result as DocumentDiagnosticReport).left).isSameAs(report) + } + + @Test + fun `quick fix requests restore opaque server data and leave client diagnostics untouched`() { + listOf(null, JsonParser.parseString("{\"nested\":[1,true,\"payload\"]}")).forEach { payload -> + val diagnostic = Diagnostic(Range(Position(2, 8), Position(2, 15)), "Unknown method").apply { data = payload } + val response = + ResponseMessage().apply { + id = "pull" + result = DocumentDiagnosticReport(RelatedFullDocumentDiagnosticReport(listOf(diagnostic))) + } + val presented = ((DiagnosticQuickFixes.incoming(response) as ResponseMessage).result as DocumentDiagnosticReport).left.items + val params = CodeActionParams(TextDocumentIdentifier("file:///Missing.x"), diagnostic.range, CodeActionContext(presented)) + val request = + RequestMessage().apply { + id = "fix" + method = "textDocument/codeAction" + this.params = params + } + val restored = DiagnosticQuickFixes.outgoing(request) as RequestMessage + val actual = (restored.params as CodeActionParams).context.diagnostics.single() + assertThat(JSONUtils.getLsp4jGson().toJsonTree(actual.data)).isEqualTo(JSONUtils.getLsp4jGson().toJsonTree(payload)) + assertThat(restored.id).isEqualTo(request.id) + assertThat(restored.method).isEqualTo(request.method) + assertThat(presented.single().data).isNotEqualTo(actual.data) + assertThat(params.context.diagnostics.single()).isSameAs(presented.single()) + } + } + + @Test + fun `unrelated and empty reports retain message identity`() { + val ordinary = + RequestMessage().apply { + id = "ordinary" + method = "textDocument/hover" + } + assertThat(DiagnosticQuickFixes.outgoing(ordinary)).isSameAs(ordinary) + val empty = + ResponseMessage().apply { + id = "empty" + result = + DocumentDiagnosticReport(RelatedFullDocumentDiagnosticReport(emptyList())) + } + assertThat(DiagnosticQuickFixes.incoming(empty)).isSameAs(empty) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt new file mode 100644 index 00000000000..1e205f4380b --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt @@ -0,0 +1,79 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParseException +import com.google.gson.JsonParser +import com.redhat.devtools.lsp4ij.JSONUtils +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints +import org.eclipse.lsp4j.services.TextDocumentService +import org.junit.jupiter.api.Test + +class DiagnosticReportJsonTest { + @Test + fun `related full and unchanged reports decode without killing the message reader`() { + // Include the real method's response adapter as well as the client's Gson configuration. + assertThatThrownBy { read(handler(false), wire) } + .isInstanceOf(JsonParseException::class.java) + .hasMessageContaining("Ambiguous Either") + + val report = read(json, wire) + assertThat(report.relatedFullDocumentDiagnosticReport.resultId).isEqualTo("root-1") + val related = report.relatedFullDocumentDiagnosticReport.relatedDocuments + assertThat(related.getValue("file:///Library.x").left.resultId).isEqualTo("library-1") + assertThat(related.getValue("file:///Library.x").left.items).hasSize(1) + assertThat(related.getValue("file:///Other.x").right.resultId).isEqualTo("other-1") + val response = + ResponseMessage().apply { + setId("1") + result = report + } + assertThat(JsonParser.parseString(json.serialize(response)).asJsonObject["result"]) + .isEqualTo(JsonParser.parseString(wire)) + } + + @Test + fun `unchanged outer reports retain related changes and reject unknown kinds`() { + val unchanged = + wire.replace( + "\"kind\":\"full\",\"resultId\":\"root-1\",\"items\":[],", + "\"kind\":\"unchanged\",\"resultId\":\"root-1\",", + ) + val report = read(json, unchanged) + assertThat(report.relatedUnchangedDocumentDiagnosticReport.relatedDocuments).hasSize(2) + assertThatThrownBy { + read( + json, + wire.replace("\"kind\":\"unchanged\"", "\"kind\":\"unknown\""), + ) + }.isInstanceOf(JsonParseException::class.java) + } + + private fun handler(compatible: Boolean) = + MessageJsonHandler(ServiceEndpoints.getSupportedMethods(TextDocumentService::class.java)) { + JSONUtils.configureCompatibilityAdapters(it) + if (compatible) it.registerTypeAdapterFactory(DiagnosticReportJson) + }.apply { setMethodProvider { "textDocument/diagnostic" } } + + private fun read( + handler: MessageJsonHandler, + body: String, + ) = (handler.parseMessage("""{"jsonrpc":"2.0","id":"1","result":$body}""") as ResponseMessage) + .result as DocumentDiagnosticReport + + private val json = handler(true) + + private val wire = + """ + {"kind":"full","resultId":"root-1","items":[],"relatedDocuments":{ + "file:///Library.x":{"kind":"full","resultId":"library-1","items":[{ + "range":{"start":{"line":2,"character":3},"end":{"line":2,"character":7}}, + "severity":1,"message":"Unresolved name" + }]}, + "file:///Other.x":{"kind":"unchanged","resultId":"other-1"} + }} + """.trimIndent() +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt new file mode 100644 index 00000000000..aad9d2ce3a9 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt @@ -0,0 +1,210 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DocumentDiagnosticParams +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.RelatedFullDocumentDiagnosticReport +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.CancelParams +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test +import java.util.concurrent.atomic.AtomicReference + +class DiagnosticResultMessagesTest { + @Test + fun `close or cancellation during reply snapshot lookup cannot reinsert a retired result`() { + listOf(false, true).forEach { cancel -> + val duringSnapshot = AtomicReference<(() -> Unit)?>(null) + val guard = + DiagnosticResultMessages { + duringSnapshot.getAndSet(null)?.invoke() + initial + } + val sent = mutableListOf() + val outgoing = guard.outgoing(MessageConsumer { sent.add(it) }) + val incoming = guard.incoming(MessageConsumer {}) + + fun request(id: String) = + RequestMessage().apply { + this.id = id + method = "textDocument/diagnostic" + params = DocumentDiagnosticParams(TextDocumentIdentifier(uri)) + } + outgoing.consume(request("pending")) + duringSnapshot.set { + outgoing.consume( + NotificationMessage().apply { + method = if (cancel) "$/cancelRequest" else "textDocument/didClose" + params = + if (cancel) { + CancelParams().apply { id = "pending" } + } else { + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)) + } + }, + ) + } + incoming.consume( + ResponseMessage().apply { + id = "pending" + result = + DocumentDiagnosticReport( + RelatedFullDocumentDiagnosticReport(emptyList()).apply { + resultId = "retired" + }, + ) + }, + ) + outgoing.consume(request("next")) + assertThat( + ((sent.last() as RequestMessage).params as DocumentDiagnosticParams) + .previousResultId, + ).isNull() + } + } + + private val uri = "file:///Diagnostics.x" + private val initial = DocumentStartupMessages.Snapshot(Any(), 1, "module Diagnostics {}") + private val buffers = mutableMapOf(uri to initial) + private val sent = mutableListOf() + private val received = mutableListOf() + private val guard = DiagnosticResultMessages(buffers::get) + private val outgoing = guard.outgoing(MessageConsumer { sent.add(it) }) + private val incoming = guard.incoming(MessageConsumer { received.add(it) }) + + @Test + fun `repeated automatic pulls reuse the current report without changing its response`() { + assertThat(request("1").previousResultId).isNull() + val response = reply("1", "first") + assertThat(received.single()).isSameAs(response) + assertThat(request("2").previousResultId).isEqualTo("first") + reply("2", "second") + assertThat(request("3").previousResultId).isEqualTo("second") + } + + @Test + fun `an edit requires a fresh full report even if diagnostic contents would be identical`() { + request("1") + reply("1", "old") + buffers[uri] = initial.copy(stamp = 2, text = "module Diagnostics { }") + assertThat(request("2").previousResultId).isNull() + reply("2", "new") + assertThat(request("3").previousResultId).isEqualTo("new") + } + + @Test + fun `late results from an earlier edit cannot replace the current result ID`() { + request("old") + buffers[uri] = initial.copy(stamp = 2, text = "module Diagnostics { }") + request("new") + reply("new", "current") + reply("old", "obsolete") + assertThat(request("next").previousResultId).isEqualTo("current") + } + + @Test + fun `close and reopen clear pending and cached reports even with the same document stamp`() { + request("1") + reply("1", "old") + request("late") + outgoing.consume( + NotificationMessage().apply { + method = "textDocument/didClose" + params = DidCloseTextDocumentParams(TextDocumentIdentifier(uri)) + }, + ) + buffers[uri] = initial.copy(owner = Any()) + reply("late", "obsolete") + assertThat(request("next").previousResultId).isNull() + } + + @Test + fun `cancellation and errors do not seed a report the client never consumed`() { + request("cancelled") + outgoing.consume( + NotificationMessage().apply { + method = "$/cancelRequest" + params = CancelParams().apply { id = "cancelled" } + }, + ) + reply("cancelled", "ignored") + assertThat(request("failed").previousResultId).isNull() + incoming.consume( + ResponseMessage().apply { + id = "failed" + error = ResponseError(ResponseErrorCode.ContentModified, "changed", null) + }, + ) + assertThat(request("next").previousResultId).isNull() + } + + @Test + fun `explicit previous IDs and different providers keep their own request policy`() { + request("1") + reply("1", "cached") + assertThat(request("2", previous = "caller-owned").previousResultId) + .isEqualTo("caller-owned") + reply("2", "caller-result") + assertThat(request("3").previousResultId).isEqualTo("cached") + assertThat(request("4", identifier = "other").previousResultId).isNull() + } + + @Test + fun `a new connection and requests without an open document start without cached results`() { + request("1") + reply("1", "cached") + buffers.clear() + assertThat(request("closed").previousResultId).isNull() + buffers[uri] = initial + val reconnect = DiagnosticResultMessages(buffers::get) + val request = + RequestMessage().apply { + id = "new-connection" + method = "textDocument/diagnostic" + params = DocumentDiagnosticParams(TextDocumentIdentifier(uri)) + } + reconnect.outgoing(MessageConsumer { sent.add(it) }).consume(request) + assertThat(sent.last()).isSameAs(request) + } + + private fun request( + id: String, + previous: String? = null, + identifier: String = "xtc", + ): DocumentDiagnosticParams { + outgoing.consume( + RequestMessage().apply { + this.id = id + method = "textDocument/diagnostic" + params = + DocumentDiagnosticParams(TextDocumentIdentifier(uri)).apply { + previousResultId = previous + this.identifier = identifier + } + }, + ) + return (sent.last() as RequestMessage).params as DocumentDiagnosticParams + } + + private fun reply( + id: String, + resultId: String, + ): ResponseMessage = + ResponseMessage().apply { + this.id = id + result = + DocumentDiagnosticReport( + RelatedFullDocumentDiagnosticReport(emptyList()).apply { + this.resultId = resultId + }, + ) + incoming.consume(this) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt new file mode 100644 index 00000000000..015937d4f69 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt @@ -0,0 +1,66 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.vfs.VirtualFile +import com.intellij.openapi.vfs.newvfs.events.VFileMoveEvent +import com.intellij.openapi.vfs.newvfs.events.VFilePropertyChangeEvent +import com.intellij.testFramework.LightVirtualFile +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class DirectoryDocumentMovesTest { + @Test + fun `pinned LSP4IJ exposes the owned disconnect operation required by the directory move bridge`() { + val method = DirectoryDocumentMoves.disconnectMethod + assertThat(method.returnType).isEqualTo(Void.TYPE) + assertThat(method.trySetAccessible()).isTrue() + } + + @Test + fun `overlapping directory moves capture each open descendant once without unrelated files`() { + val root = File("root", directory = true) + val old = File("old", root, true) + val child = File("nested", old, true) + val source = File("App.x", old) + val member = File("Member.x", child) + val similarlyNamed = File("older", root, true) + val other = File("Other.x", similarlyNamed) + val moves = listOf(VFileMoveEvent(null, old, root), VFileMoveEvent(null, child, root)) + + assertThat(DirectoryDocumentMoves.descendants(moves) { listOf(source, member, source, other) }) + .containsExactly(source, member) + } + + @Test + fun `directory rename reconnects descendants but unrelated property changes do not inspect documents`() { + val root = File("root", directory = true) + val directory = File("old", root, true) + val source = File("App.x", directory) + val rename = VFilePropertyChangeEvent(null, directory, VirtualFile.PROP_NAME, "old", "new") + assertThat(DirectoryDocumentMoves.descendants(listOf(rename)) { listOf(source) }) + .containsExactly(source) + val writable = VFilePropertyChangeEvent(null, directory, VirtualFile.PROP_WRITABLE, true, false) + assertThat(DirectoryDocumentMoves.descendants(listOf(writable)) { error("Unrelated VFS event") }).isEmpty() + } + + @Test + fun `individual file moves and renames retain the upstream lifecycle handling`() { + val root = File("root", directory = true) + val source = File("App.x", root) + val events = + listOf( + VFileMoveEvent(null, source, root), + VFilePropertyChangeEvent(null, source, VirtualFile.PROP_NAME, "App.x", "Next.x"), + ) + assertThat(DirectoryDocumentMoves.descendants(events) { error("Handled by LSP4IJ") }).isEmpty() + } + + private class File( + name: String, + private val folder: VirtualFile? = null, + private val directory: Boolean = false, + ) : LightVirtualFile(name) { + override fun getParent(): VirtualFile? = folder + + override fun isDirectory(): Boolean = directory + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt new file mode 100644 index 00000000000..d6658a2c84f --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt @@ -0,0 +1,272 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.FoldingRange +import org.eclipse.lsp4j.FoldingRangeRequestParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test +import java.net.URI + +class DocumentStartupMessagesTest { + private val uri = "file:///Startup.x" + private val buffers = + mutableMapOf(uri to DocumentStartupMessages.Snapshot(Any(), 1, "module Startup {}")) + private val sent = mutableListOf() + private val received = mutableListOf() + private val guard = + DocumentStartupMessages { requested -> + buffers.entries.singleOrNull { URI(it.key) == URI(requested) }?.value + } + private val outgoing = guard.outgoing(MessageConsumer { sent.add(it) }) + private val incoming = guard.incoming(MessageConsumer { received.add(it) }) + + @Test + fun `server edits require the transmitted version and unchanged buffer ownership`() { + open("initial") + val initial = requireNotNull(guard.editSnapshot(uri, 1)) + assertThat(guard.editSnapshot(uri, 2)).isNull() + buffers[uri] = initial.copy(stamp = 2, text = "unsent typing") + assertThat(guard.editSnapshot(uri, 1)).isNull() + change(2, "unsent typing") + assertThat(guard.editSnapshot(uri, 1)).isNull() + assertThat(guard.editSnapshot(uri, 2)).isEqualTo(buffers[uri]) + assertThat(guard.isCurrent(uri, 1, initial)).isFalse() + val current = requireNotNull(guard.editSnapshot(uri, 2)) + buffers[uri] = current.copy(stamp = 3) + assertThat(guard.isCurrent(uri, 2, current)).isFalse() + } + + @Test + fun `reused versions after reopen cannot authorize an old server edit`() { + open("initial") + change(4, buffers.getValue(uri).text) + buffers.clear() + outgoing.consume( + notification( + "textDocument/didClose", + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ), + ) + assertThat(guard.editSnapshot(uri, 4)).isNull() + buffers[uri] = DocumentStartupMessages.Snapshot(Any(), 1, "module Reopened {}") + open("reopened") + assertThat(guard.editSnapshot(uri, 1)).isNull() + change(4, buffers.getValue(uri).text) + assertThat(guard.editSnapshot(uri, 4)).isNull() + change(5, buffers.getValue(uri).text) + assertThat(guard.editSnapshot(uri, 5)).isEqualTo(buffers[uri]) + } + + @Test + fun `sequential incremental changes preserve the actual wire version across CRLF and emoji`() { + val before = "// 😀\r\nmodule Startup {}\r\n" + buffers[uri] = buffers.getValue(uri).copy(text = before) + open(before) + val after = "// 😀!\r\nmodule Updated {}\r\n" + buffers[uri] = buffers.getValue(uri).copy(stamp = 2, text = after) + outgoing.consume( + notification( + "textDocument/didChange", + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 7), + listOf( + TextDocumentContentChangeEvent().apply { + range = Range(Position(0, 5), Position(0, 5)) + text = "!" + }, + TextDocumentContentChangeEvent().apply { + range = Range(Position(1, 7), Position(1, 14)) + text = "Updated" + }, + ), + ), + ), + ) + assertThat(guard.editSnapshot("file:/Startup.x", 7)).isEqualTo(buffers[uri]) + assertThat(guard.editSnapshot(uri, 7)).isEqualTo(buffers[uri]) + } + + @Test + fun `late older changes cannot regress the transmitted version or text`() { + open("initial") + change(4, buffers.getValue(uri).text) + change(3, "stale text") + assertThat(sent).hasSize(2) + assertThat(guard.editSnapshot(uri, 4)).isEqualTo(buffers[uri]) + assertThat(guard.editSnapshot(uri, 3)).isNull() + } + + @Test + fun `typing and replacement before asynchronous open preserve latest client version`() { + change(2, "module Startup { void run() {} }") + change(3, "module Startup {}") + assertThat(sent).isEmpty() + open("module Startup { stale opening snapshot }") + val params = (sent.single() as NotificationMessage).params as DidOpenTextDocumentParams + assertThat(params.textDocument.version).isEqualTo(3) + assertThat(params.textDocument.text).isEqualTo("module Startup {}") + change(4, "module Startup { Int value = 1; }") + assertThat((sent.last() as NotificationMessage).params) + .isInstanceOf(DidChangeTextDocumentParams::class.java) + } + + @Test + fun `edits before listener attachment replace the old initial snapshot`() { + open("old text captured before server initialized") + assertThat( + ((sent.single() as NotificationMessage).params as DidOpenTextDocumentParams) + .textDocument + .text, + ).isEqualTo(buffers.getValue(uri).text) + } + + @Test + fun `close before open cannot resurrect a closed document`() { + change(2, "intermediate text") + buffers.clear() + outgoing.consume( + notification( + "textDocument/didClose", + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ), + ) + open("late initial text") + assertThat(sent).isEmpty() + } + + @Test + fun `reopen retires old state and a delayed close cannot close the new incarnation`() { + open("initial text") + buffers[uri] = DocumentStartupMessages.Snapshot(Any(), 1, "module Reopened {}") + open("reopened") + outgoing.consume( + notification( + "textDocument/didClose", + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ), + ) + assertThat(sent.map { (it as NotificationMessage).method }) + .containsExactly( + "textDocument/didOpen", + "textDocument/didClose", + "textDocument/didOpen", + ) + assertThat( + ((sent.last() as NotificationMessage).params as DidOpenTextDocumentParams) + .textDocument + .text, + ).isEqualTo("module Reopened {}") + } + + @Test + fun `late close preserves changes queued for the reopened incarnation`() { + open("initial text") + buffers[uri] = + DocumentStartupMessages.Snapshot(Any(), 2, "module Reopened { Int value = 1; }") + change(2, buffers.getValue(uri).text) + outgoing.consume( + notification( + "textDocument/didClose", + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ), + ) + open("reopened snapshot before edit") + val params = (sent.last() as NotificationMessage).params as DidOpenTextDocumentParams + assertThat(params.textDocument.version).isEqualTo(2) + assertThat(params.textDocument.text).isEqualTo(buffers.getValue(uri).text) + assertThat(sent.map { (it as NotificationMessage).method }) + .containsExactly( + "textDocument/didOpen", + "textDocument/didClose", + "textDocument/didOpen", + ) + } + + @Test + fun `fold response for a shortened document is rejected before the editor sees old lines`() { + requestFolds() + buffers[uri] = buffers.getValue(uri).copy(stamp = 2, text = "") + respondFolds() + assertThat((received.single() as ResponseMessage).error.code) + .isEqualTo(ResponseErrorCode.ContentModified.value) + assertThat((received.single() as ResponseMessage).result).isNull() + } + + @Test + fun `reopened document with identical text and stamp rejects old folding response`() { + requestFolds() + buffers[uri] = buffers.getValue(uri).copy(owner = Any()) + respondFolds() + assertThat((received.single() as ResponseMessage).error.code) + .isEqualTo(ResponseErrorCode.ContentModified.value) + } + + @Test + fun `current folding response and ordinary notifications pass unchanged`() { + requestFolds() + val response = respondFolds() + assertThat(received.single()).isSameAs(response) + val trace = notification("$/setTrace", "verbose") + outgoing.consume(trace) + assertThat(sent.last()).isSameAs(trace) + } + + private fun requestFolds() = + outgoing.consume( + RequestMessage().apply { + id = "fold" + method = "textDocument/foldingRange" + params = FoldingRangeRequestParams(TextDocumentIdentifier(uri)) + }, + ) + + private fun respondFolds(): ResponseMessage = + ResponseMessage() + .apply { + id = "fold" + result = listOf(FoldingRange(0, 10)) + }.also(incoming::consume) + + private fun open(text: String) = + outgoing.consume( + notification( + "textDocument/didOpen", + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text)), + ), + ) + + private fun change( + version: Int, + text: String, + ) = outgoing.consume( + notification( + "textDocument/didChange", + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, version), + listOf(TextDocumentContentChangeEvent(text)), + ), + ), + ) + + private fun notification( + method: String, + params: Any, + ) = NotificationMessage().apply { + this.method = method + this.params = params + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt new file mode 100644 index 00000000000..658ac29e789 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt @@ -0,0 +1,61 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path + +class FileMoveTargetsTest { + @TempDir lateinit var root: Path + + @Test + fun `destination validation refuses collisions missing parents and ambiguous batches without writes`() { + val source = Files.createDirectories(root.resolve("source")) + val a = Files.writeString(source.resolve("A.x"), "class A {}") + val b = Files.writeString(source.resolve("B.x"), "class B {}") + val target = Files.createDirectory(root.resolve("target")) + val moved = target.resolve("A.x") + assertThat(FileMoveTargets.valid(mapOf(a to moved, b to target.resolve("B.x")))).isTrue() + listOf( + mapOf(a to b), + mapOf(a to moved, b to moved), + mapOf(source to source.resolve("child")), + mapOf(a to root.resolve("missing/A.x")), + mapOf(a to target.resolve("../target/A.x")), + ).forEach { assertThat(FileMoveTargets.valid(it)).isFalse() } + assertThat(Files.readString(a)).isEqualTo("class A {}") + assertThat(moved).doesNotExist() + // The compiler orders the independently moved child before the parent directory. + assertThat(FileMoveTargets.valid(mapOf(source to target.resolve("source"), a to moved))).isTrue() + Files.createSymbolicLink(moved, root.resolve("absent")) + assertThat(FileMoveTargets.valid(mapOf(a to moved))).isFalse() + } + + @Test + fun `single source move accepts a new basename while batches preserve names`() { + val source = Files.writeString(root.resolve("Old.x"), "class Old {}") + val other = Files.writeString(root.resolve("Other.x"), "class Other {}") + val destination = Files.createDirectory(root.resolve("target")) + assertThat(FileMoveTargets.inDirectory(listOf(source), destination, "New.x")) + .containsExactlyEntriesOf(mapOf(source to destination.resolve("New.x"))) + assertThat(FileMoveTargets.inDirectory(listOf(source, other), destination)) + .containsExactlyEntriesOf(mapOf(source to destination.resolve("Old.x"), other to destination.resolve("Other.x"))) + listOf("", " ", "../escape.x", "sub/File.x", "sub\\File.x", ".", "..").forEach { name -> + assertThat(FileMoveTargets.inDirectory(listOf(source), destination, name)).isNull() + } + assertThat(FileMoveTargets.inDirectory(listOf(source, other), destination, "New.x")).isNull() + assertThat(destination.resolve("New.x")).doesNotExist() + } + + @Test + fun `rechecking catches destination created while compiler proof is pending`() { + val source = Files.writeString(root.resolve("A.x"), "class A {}") + val target = root.resolve("B.x") + val moves = mapOf(source to target) + assertThat(FileMoveTargets.valid(moves)).isTrue() + Files.writeString(target, "class B {}") + assertThat(FileMoveTargets.valid(moves)).isFalse() + assertThat(Files.readString(target)).isEqualTo("class B {}") + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt new file mode 100644 index 00000000000..a1527398185 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt @@ -0,0 +1,94 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class LanguageServiceConfigurationTest { + @Test + fun `adapter selection preserves inherited preferences and compiler paths`() { + val global = """{"xtc":{"languageService":{"adapter":"compiler","inlayHints":false}}}""" + val project = """{"xtc":{"compiler":{"sourceModules":[]},"languageService":{"futureSetting":17}}}""" + val selected = LanguageServiceConfiguration.selectAdapter(project, LanguageAdapter.TREE_SITTER) + val before = LanguageServiceConfiguration.read(global, project) + val after = LanguageServiceConfiguration.read(global, selected) + assertThat(after).isEqualTo(before.copy(adapter = "treesitter")) + assertThat(selected).contains("\"sourceModules\": []", "\"futureSetting\": 17") + assertThat(selected).doesNotContain("inlayHints") + assertThat(after.requiresRestart(before)).isTrue() + assertThat(after.requiresRestart(after)).isFalse() + assertThat(after.copy(inlayHints = true).requiresRestart(after)).isFalse() + assertThat(LanguageAdapter.TREE_SITTER.launchArguments()).containsExactly("-Dxtc.lsp.adapter=treesitter") + assertThat(LanguageAdapter.COMPILER.launchArguments()).containsExactly("-Dxtc.lsp.adapter=compiler") + assertThat(LanguageAdapter.DEFAULT.launchArguments()).isEmpty() + val restored = LanguageServiceConfiguration.selectAdapter(selected, LanguageAdapter.DEFAULT) + assertThat(LanguageServiceConfiguration.read(global, restored).adapter).isEqualTo("default") + } + + @Test + fun `service preferences do not claim ownership of an inherited source graph`() { + assertThat(CompilerSettings.ownsGraph(null)).isFalse() + assertThat( + CompilerSettings.ownsGraph("""{"xtc":{"languageService":{"inlayHints":false,"referenceCodeLens":false}}}"""), + ).isFalse() + assertThat(CompilerSettings.ownsGraph("""{"xtc":{"compiler":{"sourceModules":[]}}}""")) + .isTrue() + assertThat(CompilerSettings.ownsGraph("{}")).isTrue() + assertThat(CompilerSettings.ownsGraph("{broken")).isTrue() + } + + @Test + fun `project fields inherit global defaults without replacing the compiler graph`() { + val global = + """{"xtc":{"languageService":{"textSynchronization":"incremental","inlayHints":false,"referenceCodeLens":false}}}""" + val project = + """{"xtc":{"languageService":{"saveFormatting":"server"},"compiler":{"sourceModules":[]}}}""" + assertThat(LanguageServiceConfiguration.read(global, project)) + .isEqualTo(LanguageServiceConfiguration("incremental", "server", false, false)) + val replaced = + LanguageServiceConfiguration.replace( + project, + LanguageServiceConfiguration.section(project), + null, + ) + assertThat(replaced).contains("sourceModules") + assertThat(LanguageServiceConfiguration.read(global, replaced)) + .isEqualTo(LanguageServiceConfiguration("incremental", "editor", false, false)) + } + + @Test + fun `stale service edits refuse while concurrent graph edits are preserved`() { + val original = """{"xtc":{"compiler":{"sourceModules":[]}}}""" + val changedGraph = original.replace("[]", "null") + val replaced = + LanguageServiceConfiguration.replace(changedGraph, null, LanguageServiceConfiguration()) + assertThat(replaced).contains("\"sourceModules\": null") + assertThatThrownBy { LanguageServiceConfiguration.replace(replaced, null, null) } + .hasMessageContaining("Reset before applying") + } + + @Test + fun `invalid values fail before persistence and native save formatting wins`() { + listOf( + """{"xtc":{"languageService":{"adapter":"mock"}}}""", + """{"xtc":{"languageService":{"adapter":null}}}""", + """{"xtc":{"languageService":{"referenceCodeLens":"false"}}}""", + "[]", + "{", + """{"xtc":{"languageService":{"textSynchronization":"patch"}}}""", + """{"xtc":{"languageService":{"inlayHints":"false"}}}""", + ).forEach { content -> + assertThatThrownBy { LanguageServiceConfiguration.read(content) } + .isInstanceOf(IllegalArgumentException::class.java) + } + val server = LanguageServiceConfiguration("incremental", "server") + assertThat(server.initializationOptions(true)) + .isEqualTo( + mapOf("xtcDocumentSync" to mapOf("incremental" to true, "formatOnSave" to false)), + ) + assertThat(server.initializationOptions(false)) + .isEqualTo( + mapOf("xtcDocumentSync" to mapOf("incremental" to true, "formatOnSave" to true)), + ) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt new file mode 100644 index 00000000000..f314ad7934c --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt @@ -0,0 +1,49 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path + +class LibraryConfigurationTest { + @TempDir lateinit var directory: Path + + @Test + fun `library round trip preserves unrelated settings and ordered roots`() { + Files.createDirectories(directory.resolve("primary")) + Files.createDirectories(directory.resolve("fallback")) + val options = LibraryOptions(listOf("fallback", "primary"), listOf(LibraryAttachment("Library", listOf("primary", "fallback")))) + val content = """{"xtc":{"compiler":{"sourceModules":[],"other":42},"languageService":{"inlayHints":false}}}""" + val replacement = LibraryConfiguration.configure(content, options, directory.toUri()) + assertThat(LibraryConfiguration.read(replacement)).isEqualTo(options) + assertThat(replacement).contains("42", "inlayHints", "sourceModules") + assertThat( + LibraryConfiguration.read(LibraryConfiguration.configure(replacement, LibraryOptions(), directory.toUri())).modulePath, + ).isNull() + assertThat( + LibraryConfiguration + .read( + LibraryConfiguration.configure(replacement, LibraryOptions(emptyList()), directory.toUri()), + ).modulePath, + ).isEmpty() + } + + @Test + fun `invalid missing aliased and remote paths cannot be persisted`() { + Files.createDirectories(directory.resolve("sources")) + listOf(listOf("missing"), listOf("https://example.org/library.xtc"), listOf("sources", "./sources")).forEach { paths -> + assertThatThrownBy { + LibraryConfiguration.configure(null, LibraryOptions(paths), directory.toUri()) + }.isInstanceOf(IllegalArgumentException::class.java) + } + assertThatThrownBy { + LibraryConfiguration.configure( + null, + LibraryOptions(sourceAttachments = listOf(LibraryAttachment("", listOf("sources")))), + directory.toUri(), + ) + }.hasMessageContaining("module name") + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LspServerJarResolutionTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LspServerJarResolutionTest.kt index 779d1451f09..5b74cf8a27b 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LspServerJarResolutionTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LspServerJarResolutionTest.kt @@ -12,24 +12,22 @@ import java.nio.file.Path /** * Tests for JAR resolution logic used by both the LSP and DAP servers. * - * The XTC plugin bundles server JARs in `bin/` (NOT `lib/`) to avoid classloader - * conflicts with LSP4IJ's own lsp4j classes. These tests verify that the resolution - * logic correctly finds JARs in the expected location and rejects incorrect layouts. + * The XTC plugin bundles server JARs in `bin/` (NOT `lib/`) to avoid classloader conflicts with + * LSP4IJ's own lsp4j classes. These tests verify that the resolution logic correctly finds JARs in + * the expected location and rejects incorrect layouts. * * ## Deployment scenarios covered - * - * | Scenario | Layout | - * |---------------------------|------------------------------------------| - * | Plugin sandbox (runIde) | `plugins/intellij-plugin/bin/` | - * | ZIP install (from disk) | `intellij-plugin/bin/` | - * | Marketplace install | `intellij-plugin/bin/` | + * | Scenario | Layout | + * |-------------------------|-------------------------------------| + * | Plugin sandbox (runIde) | `plugins/intellij-plugin/bin/` | + * | ZIP install (from disk) | `intellij-plugin/bin/` | + * | Marketplace install | `intellij-plugin/bin/` | * * All three scenarios use the same directory structure, so `resolveInBin` covers them all. */ @DisplayName("Server JAR Resolution") class LspServerJarResolutionTest { - @TempDir - lateinit var pluginDir: Path + @TempDir lateinit var pluginDir: Path // ======================================================================== // LSP Server JAR Resolution (via XtcLspConnectionProvider) diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt new file mode 100644 index 00000000000..a762347c21b --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt @@ -0,0 +1,152 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.FileRename +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.jsonrpc.Endpoint +import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints +import org.eclipse.lsp4j.services.LanguageServer +import org.junit.jupiter.api.Test +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Executors + +class PreflightedRenamesTest { + private val guard = PreflightedRenames() + private val pending = CompletableFuture() + private val notifications = mutableListOf>() + private val delegate = + object : Endpoint { + override fun request( + method: String, + parameter: Any?, + ): CompletableFuture = pending + + override fun notify( + method: String, + parameter: Any?, + ) { + notifications.add(method to parameter) + } + } + private val endpoint = guard.endpoint(delegate) + private val from = Path.of("/project/Before.x") + private val to = Path.of("/project/After.x") + private val rename = RenameFilesParams(listOf(FileRename(from.toUri().toString(), to.toUri().toString()))) + + @Test + fun `production launcher derives the plugin proxy loader without a configured class loader`() { + val features = XtcClientFeatures() + val output = ByteArrayOutputStream() + Executors.newSingleThreadExecutor().use { worker -> + val server = + features + .createLauncherBuilder() + .setLocalService(Any()) + .setRemoteInterface(XtcLanguageServer::class.java) + .setInput(ByteArrayInputStream(ByteArray(0))) + .setOutput(output) + .setExecutorService(worker) + .create() + .remoteProxy + features.preflightedRenames.apply(from, to) { + assertThat(server.workspaceService.willRenameFiles(rename)).isCompletedWithValue(null) + } + assertThat(output.size()).isZero() + val status = server.languageServiceStatus() + assertThat(status).isNotDone() + assertThat(output.toString(Charsets.UTF_8)).contains("xtc/languageServiceStatus") + status.cancel(false) + } + } + + @Test + fun `LSP service proxy preserves synchronous ownership through workspace service dispatch`() { + val server = ServiceEndpoints.toServiceObject(endpoint, LanguageServer::class.java) + guard.apply(from, to) { + assertThat(server.workspaceService.willRenameFiles(rename)).isCompletedWithValue(null) + } + assertThat(server.workspaceService.willRenameFiles(rename)).isSameAs(pending) + } + + @Test + fun `physical move no-op requests complete without reaching a blocked server`() { + listOf( + "file:///project/Before.x" to "file:/project/Before.x", + "file:///project/directory/" to "file:/project/directory", + "file:///project/name%20with%20spaces.x" to "file:/project/name%20with%20spaces.x", + ).forEach { (old, new) -> + val result = request(RenameFilesParams(listOf(FileRename(old, new)))) + assertThat(result).isCompletedWithValue(null) + } + assertThat(pending).isNotDone() + } + + @Test + fun `only the exact approved rename is handled inside its lexical scope`() { + assertThat(request(rename)).isSameAs(pending) + guard.apply(from, to) { + assertThat(request(rename)).isCompletedWithValue(null) + assertThat(request(RenameFilesParams(listOf(FileRename(to.toUri().toString(), from.toUri().toString()))))) + .isSameAs(pending) + assertThat(endpoint.request("workspace/willDeleteFiles", rename)).isSameAs(pending) + assertThat(PreflightedRenames().endpoint(delegate).request("workspace/willRenameFiles", rename)).isSameAs(pending) + } + assertThat(request(rename)).isSameAs(pending) + } + + @Test + fun `nested scopes restore their parent and exceptions retire all approval`() { + assertThatThrownBy { + guard.apply(from, to) { + guard.apply(to, from) { assertThat(request(rename)).isSameAs(pending) } + assertThat(request(rename)).isCompletedWithValue(null) + error("VFS operation failed") + } + }.isInstanceOf(IllegalStateException::class.java) + assertThat(request(rename)).isSameAs(pending) + } + + @Test + fun `unrelated worker requests cannot inherit an EDT approval`() { + Executors.newSingleThreadExecutor().use { worker -> + guard.apply(from, to) { + val otherThread = worker.submit> { request(rename) }.get() + assertThat(otherThread).isSameAs(pending) + assertThat(request(rename)).isCompletedWithValue(null) + } + } + } + + @Test + fun `unknown mixed malformed and meaningful URI changes retain normal validation`() { + listOf( + RenameFilesParams(emptyList()), + RenameFilesParams(listOf(FileRename(from.toUri().toString(), from.toUri().toString())) + rename.files), + RenameFilesParams(listOf(FileRename("untitled:buffer", "untitled:buffer"))), + RenameFilesParams(listOf(FileRename("file:/project/A/../Before.x", "file:/project/Before.x"))), + RenameFilesParams(listOf(FileRename("file:/project/a.x", "file:/project/A.x"))), + RenameFilesParams(listOf(FileRename("file:/broken uri", "file:/broken uri"))), + ).forEach { assertThat(request(it)).isSameAs(pending) } + assertThat(endpoint.request("workspace/willRenameFiles", null)).isSameAs(pending) + } + + @Test + fun `did notifications are unchanged and forwarded requests preserve cancellation`() { + guard.apply(from, to) { + endpoint.notify("workspace/didRenameFiles", rename) + endpoint.notify("workspace/didChangeWatchedFiles", rename) + } + assertThat(notifications).containsExactly( + "workspace/didRenameFiles" to rename, + "workspace/didChangeWatchedFiles" to rename, + ) + request(rename).cancel(false) + assertThat(pending).isCancelled() + } + + private fun request(params: RenameFilesParams): CompletableFuture<*> = endpoint.request("workspace/willRenameFiles", params) +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt new file mode 100644 index 00000000000..6da90463917 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt @@ -0,0 +1,40 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class ServerJvmOptionsTest { + @Test fun `supported options remain separate arguments`() { + val options = listOf("-Xms256M", "-Xmx2G", "-Xss1M", "-XX:ActiveProcessorCount=4", "-XX:+UseG1GC") + assertThat(ServerJvmOptions.validate(options)).isEqualTo(options) + } + + @Test fun `reject launch overrides duplicates conflicts and impossible heaps`() { + listOf( + listOf("-jar"), + listOf("@options"), + listOf("-Dxtc.adapter=mock"), + listOf("-javaagent:agent.jar"), + listOf("-Xmx1G -Xss1M"), + listOf("-Xmx2G", "-Xmx1G"), + listOf("-Xms2G", "-Xmx1G"), + listOf("-XX:+UseG1GC", "-XX:+UseZGC"), + listOf("-Xmx999999999999999999999G"), + ).forEach { + assertThatThrownBy { ServerJvmOptions.validate(it) }.isInstanceOf(IllegalArgumentException::class.java) + } + } + + @Test fun `retention validates before publishing the immutable machine snapshot`() { + val store = ServerRuntimeSettings() + val before = store.state + assertThatThrownBy { store.install(before, before.copy(logTotalSizeMb = 1)) }.isInstanceOf(IllegalArgumentException::class.java) + assertThat(store.state).isEqualTo(before) + val next = before.copy(vmOptions = "-Xmx1G", logHistoryDays = 2, logMaxFileMb = 1, logTotalSizeMb = 3, logRetainedSessions = 2) + store.install(before, next) + assertThat(store.launchArguments()).contains("-Xmx1G", "-Dxtc.logs.historyDays=2", "-Dxtc.logs.totalSizeMb=3") + assertThatThrownBy { store.install(before, before) }.hasMessageContaining("changed") + assertThat(store.state).isEqualTo(next) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt new file mode 100644 index 00000000000..34ea57d6041 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt @@ -0,0 +1,19 @@ +package org.xtclang.idea.lsp + +import com.intellij.util.xmlb.XmlSerializer +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test + +class ServerSettingsPersistenceTest { + @Test fun `runtime options survive IntelliJ XML serialization`() { + val options = ServerRuntimeSettings.Options("-Xmx768M", 2, 1, 3, 2) + val restored = XmlSerializer.deserialize(XmlSerializer.serialize(options), ServerRuntimeSettings.Options::class.java) + assertThat(restored).isEqualTo(options) + } + + @Test fun `offline support session survives IntelliJ XML serialization`() { + val session = ServerSupportLogs.Session("launch-id", "2026-10-06T10:00:00Z", "Failed launch", "/logs/server-123-456", "/traces") + val restored = XmlSerializer.deserialize(XmlSerializer.serialize(session), ServerSupportLogs.Session::class.java) + assertThat(restored).isEqualTo(session) + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt new file mode 100644 index 00000000000..16eb8583fff --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt @@ -0,0 +1,67 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path +import java.util.zip.ZipFile + +class ServerSupportLogsTest { + @TempDir lateinit var temporary: Path + + @Test fun `retired callbacks cannot replace a new project launch`() { + val store = ServerSupportLogs() + val old = store.begin("Old launch") + val current = store.begin("Current launch") + store.append(old, " Stale failure") + store.append(current, " Current failure") + val restored = ServerSupportLogs().apply { loadState(store.state) } + assertThat(restored.state.launcher).isEqualTo("Current launch Current failure") + assertThat(ServerSupportLogs().state.id).isEmpty() + } + + @Test fun `offline archive is session owned bounded and does not follow links`() { + val session = Files.createDirectory(temporary.resolve("server-123-456")) + val other = Files.createDirectory(temporary.resolve("server-789-456")) + Files.writeString(other.resolve("server.log"), "OTHER PROJECT") + Files.writeString(session.resolve("server.log"), "x".repeat(700_000) + "TAIL") + Files.writeString(session.resolve("Main.x"), "SOURCE CONTENT") + Files.createSymbolicLink(session.resolve("server.1.log"), other.resolve("server.log")) + val traces = Files.createDirectory(temporary.resolve("traces")) + Files.writeString(traces.resolve("lsp-trace-123-456.jsonl"), "OWN TRACE") + Files.writeString(traces.resolve("lsp-trace-789-456.jsonl"), "OTHER TRACE") + val destination = temporary.resolve("support.zip") + ServerSupportLogs.archive( + ServerSupportLogs.Session( + launcher = "FAILED LAUNCH", + directory = session.toString(), + traceDirectory = traces.toString(), + ), + destination, + ) + ZipFile(destination.toFile()).use { zip -> + val entries = + zip.entries().asSequence().associate { entry -> + entry.name to + zip.getInputStream(entry).use { it.readBytes().decodeToString() } + } + assertThat(entries.keys).hasSize(4) + assertThat(entries.filterKeys { it.endsWith("server.log") }.values.single()).hasSize(512 * 1024).endsWith("TAIL") + assertThat(entries.values.joinToString()).doesNotContain("OTHER PROJECT", "OTHER TRACE", "SOURCE CONTENT") + assertThat(entries.getValue("manifest.json")).contains("\"offline\": true", "\"truncated\": true") + } + } + + @Test fun `failure before a server process exists still exports launcher information`() { + val store = ServerSupportLogs() + val launch = store.begin("Java not found") + store.append(launch, "z".repeat(100_000)) + val destination = temporary.resolve("failed.zip") + store.export(destination) + ZipFile(destination.toFile()).use { zip -> + assertThat(zip.getEntry("launcher.log").size).isEqualTo(64 * 1024L) + assertThat(zip.size()).isEqualTo(2) + } + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt new file mode 100644 index 00000000000..bb722c3b411 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt @@ -0,0 +1,29 @@ +package org.xtclang.idea.lsp + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatIllegalArgumentException +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.TextEdit +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.junit.jupiter.api.Test + +class ServerWorkspaceEditTest { + @Test + fun `platform and LSP4IJ edit representations preserve the same edits`() { + val edit = TextEdit(Range(Position(0, 0), Position(0, 3)), "new") + assertThat(ServerWorkspaceEdit.textEdits(listOf(edit))).containsExactly(edit) + assertThat(ServerWorkspaceEdit.textEdits(listOf(Either.forLeft(edit)))) + .containsExactly(edit) + } + + @Test + fun `unsupported alternatives and malformed entries cannot become partial edits`() { + val edit = TextEdit(Range(Position(0, 0), Position(0, 3)), "new") + listOf(null, "malformed", Either.forRight(Any())).forEach { unsupported -> + assertThatIllegalArgumentException().isThrownBy { + ServerWorkspaceEdit.textEdits(listOf(edit, unsupported)) + } + } + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt new file mode 100644 index 00000000000..6911c5ddba9 --- /dev/null +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt @@ -0,0 +1,219 @@ +package org.xtclang.idea.lsp + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import java.net.URI + +class SourceGraphConfigurationTest { + @Test + fun `resource roots preserve order empty automatic and rename ownership`() { + val base = URI.create("file:///workspace/") + val modules = + listOf( + SourceModuleConfiguration( + "Library", + "Library.x", + resourceRoots = listOf("custom/", "fallback/"), + ), + ) + val content = SourceGraphConfiguration.configure(null, modules, base) + assertThat(SourceGraphConfiguration.read(content)).isEqualTo(modules) + val changed = modules.map { it.copy(name = "Renamed", uri = "Renamed.x") } + val replaced = SourceGraphConfiguration.replace(content, modules, changed, base) + assertThat(SourceGraphConfiguration.read(replaced)).isEqualTo(changed) + val different = modules.map { it.copy(resourceRoots = listOf("fallback/", "custom/")) } + assertThatThrownBy { SourceGraphConfiguration.replace(content, different, changed, base) } + .isInstanceOf(IllegalArgumentException::class.java) + listOf(null, emptyList()).forEach { resources -> + val graph = modules.map { it.copy(resourceRoots = resources) } + assertThat( + SourceGraphConfiguration.read( + SourceGraphConfiguration.configure(null, graph, base), + ), + ).isEqualTo(graph) + } + assertThatThrownBy { + SourceGraphConfiguration.configure( + null, + modules.map { it.copy(resourceRoots = listOf("custom/", "custom/")) }, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `project configuration preserves unrelated settings and distinguishes discovery from empty graph`() { + val explicit = SourceGraphConfiguration.configure(original, after, base) + assertThat(SourceGraphConfiguration.read(explicit)).isEqualTo(after) + assertThat(explicit).contains("42") + val automatic = SourceGraphConfiguration.configure(explicit, null, base) + assertThat(SourceGraphConfiguration.read(automatic)).isNull() + assertThat(automatic).contains("\"sourceModules\": null", "42") + val empty = SourceGraphConfiguration.configure(automatic, emptyList(), base) + assertThat(SourceGraphConfiguration.read(empty)).isEmpty() + assertThat(SourceGraphConfiguration.read(null)).isNull() + } + + @Test + fun `new project settings reject aliased roots and blank names before persistence`() { + val graph = + listOf( + SourceModuleConfiguration("Library", "Library.x"), + SourceModuleConfiguration("Other", "file:///workspace/Library.x"), + ) + assertThatThrownBy { SourceGraphConfiguration.configure(null, graph, base) } + .hasMessageContaining("Duplicate source module roots") + assertThatThrownBy { + SourceGraphConfiguration.configure( + null, + listOf(SourceModuleConfiguration("", "Library.x")), + base, + ) + }.hasMessageContaining("non-blank") + assertThat( + SourceGraphConfiguration.read( + SourceGraphConfiguration.configure(null, before, base), + ), + ).isEqualTo(before) + } + + private val base = URI("file:///workspace/") + private val before = listOf(SourceModuleConfiguration("Library", "file:///workspace/Library.x")) + private val after = listOf(SourceModuleConfiguration("Renamed", "file:///workspace/Renamed.x")) + private val original = + """{"xtc":{"compiler":{"sourceModules":[{"name":"Library","uri":"Library.x"}],"other":42},"formatting":null}}""" + + @Test + fun `replacement and undo preserve unrelated settings and omitted dependency lists`() { + val changed = SourceGraphConfiguration.replace(original, before, after, base) + assertThat(changed).contains("Renamed", "42", "\"formatting\": null") + val restored = SourceGraphConfiguration.replace(changed, after, before, base) + val compiler = + JsonParser + .parseString(restored) + .asJsonObject["xtc"] + .asJsonObject["compiler"] + .asJsonObject + assertThat( + compiler["sourceModules"] + .asJsonArray + .single() + .asJsonObject["name"] + .asString, + ).isEqualTo("Library") + assertThat(compiler["other"].asInt).isEqualTo(42) + assertThat(SourceGraphConfiguration.replace(restored, before, after, base)) + .isEqualTo(changed) + } + + @Test + fun `a different graph cannot be overwritten by a late proposal or undo`() { + assertThatThrownBy { + SourceGraphConfiguration.replace( + original.replace("Library", "Elsewhere"), + before, + after, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { SourceGraphConfiguration.replace(original, after, before, base) } + .isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `malformed intervening settings become a guarded refusal`() { + listOf( + "{", + "[]", + """{"xtc":1}""", + original.replace("\"Library.x\"", "null"), + original.replace("\"Library\"", "1"), + ).forEach { content -> + assertThatThrownBy { + SourceGraphConfiguration.replace(content, before, after, base) + }.isInstanceOf(IllegalArgumentException::class.java) + } + } + + @Test + fun `discovery and duplicate graph entries are not equivalent to an explicit graph`() { + assertThatThrownBy { + SourceGraphConfiguration.replace( + """{"xtc":{"compiler":{"sourceModules":null}}}""", + before, + after, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + val entry = """{"name":"Library","uri":"Library.x"}""" + assertThatThrownBy { + SourceGraphConfiguration.replace( + original.replace("[$entry]", "[$entry,$entry]"), + before, + after, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `history preserves unrelated edits but refuses independently replaced dependency edges`() { + val withEdge = + original.replace( + "\"uri\":\"Library.x\"", + "\"uri\":\"Library.x\",\"dependencies\":[\"Other\"]", + ) + val graph = + listOf( + SourceModuleConfiguration("Library", "file:///workspace/Library.x", listOf("Other")), + ) + val changed = + SourceGraphConfiguration.replace(withEdge, graph, after, base).replace("42", "17") + val restored = SourceGraphConfiguration.replace(changed, after, graph, base) + assertThat(restored).contains("17", "Other") + assertThatThrownBy { + SourceGraphConfiguration.replace( + restored.replace("Other", "Elsewhere"), + graph, + after, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `absolute roots in separate projects preserve the complete graph and refuse duplicate roots`() { + val graph = + listOf( + SourceModuleConfiguration("Library", "file:///libraries/Library.x"), + SourceModuleConfiguration("App", "file:///apps/App.x", listOf("Library")), + ) + val content = + """ + {"xtc":{"compiler":{"sourceModules":[ + {"name":"Library","uri":"file:///libraries/Library.x"}, + {"name":"App","uri":"file:///apps/App.x","dependencies":["Library"]} + ]}}} + """.trimIndent() + val next = + listOf( + SourceModuleConfiguration("Renamed", "file:///libraries/Renamed.x"), + graph[1].copy(dependencies = listOf("Renamed")), + ) + val changed = SourceGraphConfiguration.replace(content, graph, next, base) + assertThat(changed).contains("file:///apps/App.x", "file:///libraries/Renamed.x") + assertThat(SourceGraphConfiguration.replace(changed, next, graph, base)) + .contains("file:///libraries/Library.x") + assertThatThrownBy { + SourceGraphConfiguration.replace( + content.replace("file:///apps/App.x", "file:///libraries/Library.x"), + graph, + next, + base, + ) + }.isInstanceOf(IllegalArgumentException::class.java) + .hasMessageContaining("Duplicate source module roots") + } +} diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/BundledResourcesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/BundledResourcesTest.kt index 7c2ca7f400c..fb51b674664 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/BundledResourcesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/BundledResourcesTest.kt @@ -5,17 +5,15 @@ import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test /** - * Verifies that the resources referenced by plugin.xml and our Kotlin - * code actually exist on the runtime classpath. + * Verifies that the resources referenced by plugin.xml and our Kotlin code actually exist on the + * runtime classpath. * - * Counterpart to VS Code's `jar-bundling.test.ts` — guards the same - * class of failure: a build step (e.g. `copyTextMateToSandbox`, - * `copyLspServerToSandbox`) gets removed or renamed and the manifest - * keeps pointing at a path that no longer exists, with no signal until - * a user installs the plugin and gets a NullPointerException loading a - * `.x` file. The complementary `LspServerJarResolutionTest` already - * covers the LSP/DAP JAR path semantics; this test covers everything - * else our manifest claims should be there. + * Counterpart to VS Code's `jar-bundling.test.ts` — guards the same class of failure: a build step + * (e.g. `copyTextMateToSandbox`, `copyLspServerToSandbox`) gets removed or renamed and the manifest + * keeps pointing at a path that no longer exists, with no signal until a user installs the plugin + * and gets a NullPointerException loading a `.x` file. The complementary + * `LspServerJarResolutionTest` already covers the LSP/DAP JAR path semantics; this test covers + * everything else our manifest claims should be there. */ @DisplayName("Bundled plugin resources") class BundledResourcesTest { @@ -25,8 +23,9 @@ class BundledResourcesTest { @DisplayName("META-INF/plugin.xml is on the classpath") fun pluginXmlPresent() { assertThat(classLoader.getResource("META-INF/plugin.xml")) - .withFailMessage("META-INF/plugin.xml not on test classpath — the IntelliJ Platform won't see the plugin at all.") - .isNotNull + .withFailMessage( + "META-INF/plugin.xml not on test classpath — the IntelliJ Platform won't see the plugin at all.", + ).isNotNull } @Test @@ -50,7 +49,8 @@ class BundledResourcesTest { // Missing icon = file-type / wizard / run-config items render with // the generic file glyph instead of our chrome-X. assertThat(classLoader.getResource("icons/xtc.svg")) - .withFailMessage("icons/xtc.svg not on classpath — wizard, run-config, and file icons fall back to defaults.") - .isNotNull + .withFailMessage( + "icons/xtc.svg not on classpath — wizard, run-config, and file icons fall back to defaults.", + ).isNotNull } } diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/LiveTemplateRegistrationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/LiveTemplateRegistrationTest.kt index 3c2e5c155ee..4a391279899 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/LiveTemplateRegistrationTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/LiveTemplateRegistrationTest.kt @@ -8,16 +8,14 @@ import javax.xml.parsers.DocumentBuilderFactory /** * Verifies that the live-template file referenced by plugin.xml's - * `/liveTemplates/XTC` - * actually exists at the expected path, parses, and contains at least - * the snippet shortcuts we expose to users in the README + manual test - * plan. + * `/liveTemplates/XTC` actually exists at the expected + * path, parses, and contains at least the snippet shortcuts we expose to users in the README + + * manual test plan. * - * Counterpart to VS Code's `snippets.test.ts` — same failure mode: a - * snippet vanishes from the bundled file or its shortcut gets renamed, - * and users see the editor "still work" but tab-expansion silently - * fails. Without this test the only catch would be someone noticing - * during interactive QA. + * Counterpart to VS Code's `snippets.test.ts` — same failure mode: a snippet vanishes from the + * bundled file or its shortcut gets renamed, and users see the editor "still work" but + * tab-expansion silently fails. Without this test the only catch would be someone noticing during + * interactive QA. */ @DisplayName("Live template registration (liveTemplates/XTC.xml)") class LiveTemplateRegistrationTest { @@ -26,7 +24,9 @@ class LiveTemplateRegistrationTest { // classpath entries (see comment in PluginManifestTest). val resourceUrl = javaClass.classLoader.getResource("liveTemplates/XTC.xml") - ?: error("liveTemplates/XTC.xml not on test classpath — file path drifted from plugin.xml") + ?: error( + "liveTemplates/XTC.xml not on test classpath — file path drifted from plugin.xml", + ) DocumentBuilderFactory .newInstance() .apply { isNamespaceAware = false } @@ -104,7 +104,8 @@ class LiveTemplateRegistrationTest { val options = ctx.getElementsByTagName("option") (0 until options.length).any { oi -> val opt = options.item(oi) as Element - opt.getAttribute("name") == "OTHER" && opt.getAttribute("value") == "true" + opt.getAttribute("name") == "OTHER" && + opt.getAttribute("value") == "true" } } } diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/PluginManifestTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/PluginManifestTest.kt index 92e23029814..e2343989f0a 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/PluginManifestTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/manifest/PluginManifestTest.kt @@ -1,5 +1,6 @@ package org.xtclang.idea.manifest +import com.intellij.refactoring.move.MoveHandlerDelegate import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Test @@ -10,13 +11,11 @@ import javax.xml.parsers.DocumentBuilderFactory /** * Self-consistency tests for META-INF/plugin.xml. * - * Counterpart to VS Code's `activation.test.ts` — both guard the same failure - * mode: an extension point / command / configuration declared in the - * manifest but referenced from the wrong place in code (or vice versa) - * silently fails to register at runtime. We can't fully verify - * runtime registration without `BasePlatformTestCase`, but we can at - * least guarantee that the manifest carries every extension point our - * Kotlin code expects, and that the plugin ID matches the single + * Counterpart to VS Code's `activation.test.ts` — both guard the same failure mode: an extension + * point / command / configuration declared in the manifest but referenced from the wrong place in + * code (or vice versa) silently fails to register at runtime. We can't fully verify runtime + * registration without `BasePlatformTestCase`, but we can at least guarantee that the manifest + * carries every extension point our Kotlin code expects, and that the plugin ID matches the single * source of truth in `PluginPaths.PLUGIN_ID`. */ @DisplayName("Plugin manifest (META-INF/plugin.xml)") @@ -57,7 +56,9 @@ class PluginManifestTest { } @Test - @DisplayName("declares all required extension points (newProjectWizard, configurationType, lang.commenter, LSP server, ...)") + @DisplayName( + "declares all required extension points (newProjectWizard, configurationType, lang.commenter, LSP server, ...)", + ) fun declaresAllRequiredExtensions() { // Collect every child element name // across the entire manifest. We don't care which block they @@ -77,14 +78,20 @@ class PluginManifestTest { val required = setOf( - "notificationGroup", // notification group "XTC Language Server" used by XtcLspServerSupportProvider - "fileType", // registers *.x as Ecstasy so IntelliJ does not suggest unrelated plugins + "notificationGroup", // notification group "Ecstasy Language Server" used by + // XtcLspServerSupportProvider + "fileType", // registers *.x as Ecstasy so IntelliJ does not suggest unrelated + // plugins + "lang.syntaxHighlighterFactory", // use the TextMate grammar for our native file type + "editorHighlighterProvider", // use TextMate's scope-aware token storage "newProjectWizard.generator", // wizard entry created by XtcNewProjectWizard "configurationType", // run-config type created by XtcRunConfigurationType "runConfigurationProducer", // auto-create run configs from .x context "iconProvider", // file icons via XtcIconProvider "defaultLiveTemplates", // /liveTemplates/XTC snippets "lang.commenter", // Ctrl+/ via XtcCommenter + "renameHandler", // guarded compiler rename via XtcRenameHandler + "refactoring.moveHandler", // compiler preflight before native filesystem mutation "langCodeStyleSettingsProvider", // Settings -> Code Style -> Ecstasy "enterHandlerDelegate", // auto-indent on Enter "postStartupActivity", // XtcEditorStartupActivity @@ -104,10 +111,43 @@ class PluginManifestTest { ).isEmpty() } + @Test + fun fileMoveUsesThePlatformExtensionPoint() { + val elements = pluginXml.getElementsByTagName("*") + val registration = + (0 until elements.length) + .map { elements.item(it) as Element } + .single { + it.getAttribute("implementation") == "org.xtclang.idea.lsp.XtcFileMoveHandler" + } + val namespace = (registration.parentNode as Element).getAttribute("defaultExtensionNs") + assertThat("$namespace.${registration.tagName}").isEqualTo(MoveHandlerDelegate.EP_NAME.name) + } + + @Test + @DisplayName("requires only Community platform plugins and LSP4IJ") + fun communityDependencies() { + val elements = pluginXml.getElementsByTagName("depends") + val required = + (0 until elements.length) + .map { elements.item(it) as Element } + .filter { it.getAttribute("optional") != "true" } + .map { it.textContent.trim() } + assertThat(required) + .containsExactlyInAnyOrder( + "com.intellij.modules.platform", + "com.intellij.modules.java", + "com.intellij.gradle", + "org.jetbrains.plugins.textmate", + "com.redhat.devtools.lsp4ij", + ) + } + @Test @DisplayName("LSP server registration points at xtcLanguageServer and maps *.x") fun lspServerWiring() { - // The LSP server element lives inside . + // The LSP server element lives inside . // We look for the child with id="xtcLanguageServer" and the // with patterns="*.x". val servers = pluginXml.getElementsByTagName("server") @@ -116,8 +156,9 @@ class PluginManifestTest { .map { servers.item(it) as Element } .firstOrNull { it.getAttribute("id") == "xtcLanguageServer" } assertThat(server) - .withFailMessage("plugin.xml is missing the LSP4IJ element") - .isNotNull + .withFailMessage( + "plugin.xml is missing the LSP4IJ element", + ).isNotNull val mappings = pluginXml.getElementsByTagName("fileNamePatternMapping") val mapping = @@ -141,7 +182,9 @@ class PluginManifestTest { val fileType = (0 until fileTypes.length) .map { fileTypes.item(it) as Element } - .firstOrNull { it.getAttribute("implementationClass") == "org.xtclang.idea.XtcFileType" } + .firstOrNull { + it.getAttribute("implementationClass") == "org.xtclang.idea.XtcFileType" + } assertThat(fileType) .withFailMessage( "plugin.xml must register org.xtclang.idea.XtcFileType. Without this, IntelliJ treats .x as " + diff --git a/lang/lsp-server/README.md b/lang/lsp-server/README.md index 0aa79ed189c..51febced050 100644 --- a/lang/lsp-server/README.md +++ b/lang/lsp-server/README.md @@ -1,5 +1,10 @@ # Ecstasy LSP Server +Compiler mode provides diagnostics and semantic editor features across configured source and binary +dependencies. It is the default shipped adapter. Tree-sitter remains an explicit syntax-only +alternative. The [manual playbook](../doc/manual-test-plan.md) describes supported operations, +deliberate refusals, native test commands and client compatibility limits. + Language Server Protocol (LSP) implementation for the Ecstasy programming language. ## Overview @@ -46,39 +51,47 @@ The LSP server uses a pluggable adapter pattern to support different parsing bac │ MockAdapter │ │ TreeSitter- │ │ XdkAdapter │ │ │ │ Adapter │ │ │ │ │ │ │ │ │ -│ - Regex-based │ │ - Tree-sitter │ │ - Stub for │ -│ - For testing │ │ - Syntax AST │ │ future │ -│ │ │ - Default │ │ semantic │ +│ - Regex-based │ │ - Tree-sitter │ │ - Compiler │ +│ - For testing │ │ - Syntax AST │ │ - Diagnostics │ +│ │ │ │ │ - Default │ └───────────────┘ └───────────────┘ └───────────────┘ ``` ## Adapter Selection -The adapter is selected at **build time** via the `lsp.adapter` Gradle property. -The selection is embedded in `lsp-version.properties` inside the JAR. +Both the compiler and Tree-sitter adapters are bundled in the server JAR. Select an adapter +in VS Code with **Ecstasy: Switch Language Adapter**, in IntelliJ with **Tools → Switch Ecstasy +Language Adapter**, or with **Ctrl+Alt+X, then A** (**Control+Option+X, then A** on macOS). +The IDE saves the choice, restarts the server and reopens unsaved buffers. Compiler provides +semantic analysis; Tree-sitter provides syntax-based features without compiler type checking. + +The **Bundled default** choice uses `lsp.adapter` from `lsp-version.properties`, set by the +`lsp.adapter` Gradle property (normally `compiler`). A standalone server can override that +default at startup with `java -Dxtc.lsp.adapter=treesitter -jar `. Each server process +uses one adapter for its lifetime; switching requires a new process to negotiate capabilities. ### Available Adapters | Adapter | Value | Description | |---------|-------|-------------| | **Mock** | `mock` | Regex-based parsing. No native dependencies. Good for testing. | -| **Tree-sitter** (default) | `treesitter` | AST-based parsing using tree-sitter. Requires native library. | -| **XDK** | `xdk` | Stub adapter. All methods logged but return empty. Placeholder for future semantic integration. | +| **Tree-sitter** | `treesitter` | AST-based parsing using tree-sitter. Requires native library. | +| **XDK** (default) | `compiler` | Compiler diagnostics, semantic navigation and hierarchy across source graphs and binary dependencies. The full matching XDK library set is bundled. | ### Build Commands ```bash -# Build with Tree-sitter adapter (default) +# Build with the compiler adapter and bundled XDK (default) ./gradlew :lang:lsp-server:fatJar # Build with Mock adapter (no native dependencies) ./gradlew :lang:lsp-server:fatJar -Plsp.adapter=mock -# Build with XDK stub (all calls logged) -./gradlew :lang:lsp-server:fatJar -Plsp.adapter=xdk +# Build with the XTC compiler and bundled XDK modules +./gradlew :lang:lsp-server:fatJar -Plsp.adapter=compiler # Run IntelliJ with specific adapter -./gradlew :lang:intellij-plugin:runIde -Plsp.adapter=treesitter +./gradlew :lang:intellij-plugin:runIde -Plsp.adapter=compiler ``` ### Setting a Default Adapter @@ -86,7 +99,7 @@ The selection is embedded in `lsp-version.properties` inside the JAR. Create or edit `gradle.properties`: ```properties -lsp.adapter=treesitter +lsp.adapter=compiler ``` ### Verifying the Active Backend @@ -105,26 +118,121 @@ In IntelliJ: **View -> Tool Windows -> Language Servers** (LSP4IJ) to see server ### Backend Comparison -| Feature | Mock | Tree-sitter | Compiler Stub | -|---------|:----:|:-----------:|:-------------:| -| Symbol detection | Regex (basic) | AST-based (accurate) | None (logged) | -| Nested symbols | ❌ Limited | ✅ Full hierarchy | ❌ None | -| Syntax errors | ❌ Basic patterns | ✅ Precise location | ❌ None | -| Error recovery | ❌ None | ✅ Continues parsing | ❌ None | -| Rename | ✅ Same-file (text) | ✅ Same-file (AST) | ❌ None | -| Code actions | ✅ Organize imports | ✅ Organize/remove imports + doc comment + auto-import | ❌ None | -| Formatting | ✅ Trailing WS | ✅ Trailing WS + auto-indent | ❌ None | -| Folding ranges | ✅ Brace matching | ✅ AST node boundaries | ❌ None | -| Signature help | ❌ None | ✅ Same-file methods | ❌ None | -| Document links | ✅ Import regex | ✅ Import AST + workspace index navigation | ❌ None | +Compiler mode has implementations for all 27 capabilities in this project's adapter interface, +plus push/pull diagnostics and document/workspace synchronization. Several implementations remain +bounded; the interface does not cover every LSP feature. Debug inline values, notebooks and broader +refactorings remain incomplete. Color values have an opt-in, fixture-only constructor prototype; +a public Ecstasy color-library contract remains deferred. See the [explicit absent-feature inventory](../doc/plans/plan-ide-integration.md#compiler-completeness-snapshot) +and [acceptance reports](../doc/manual-test-plan.md#acceptance-reports). +Capability coverage, semantic completeness and native test coverage are tracked separately. +Compiler code actions include expression/local extraction, private helper extraction for contiguous +call statements and nested expressions, adjacent single-evaluation local inline, wider constant-local +inline, selected private zero-argument method/constant-property inline and safe deletion of unused +private methods/static constants. Compiler-selected types, stable captures, evaluation order and +all remaining bindings/dispatch must survive complete proposed-graph compilation. Mutable captures, +control-flow outputs, parameter substitution and externally visible safe deletion remain refused. + +Missing methods support exact generic-owner formals, method formals/constraints, conditional returns, +validated computed/chained receivers, singleton qualifiers, named arguments and typed expressions. +Destinations include writable source owners in the same module and reachable configured dependencies, +including closed companions. Required destination imports can accompany the member atomically, but +the action does not invent dependencies. Runtime Class/Type values without a proven source owner, +ambiguous inverse generic substitutions, binary/read-only destinations and unproven result contexts +remain refused. A bare zero-argument unknown constructor can create a same-module class; an unresolved +whole return value can create a same-owner read-only property with a TODO getter. Static properties +require an initializer; the action does not invent one. Property inline/delete require compiler-proven +constant values without runtime initialization. + +Type spelling reuses resolved module imports and the compiler's implicit Ecstasy module constants; +this does not restrict the module path to the core library. Recursive typedef identities support +diagnostics and rename; member generation refuses aliases whose destination spelling/import route +is not proven, without suppressing unrelated actions. Both source and binary dependency types +have extraction regression coverage. Shared X221–X242 exercise these operations in both editor drivers. +Explicit declaration lookup returns local/import-alias declarations or the inherited written +contracts of an overriding method/property, including multiple source targets. Definition and +implementation retain their separate meanings. Indexed library sources remain read-only. + +| Feature | Mock | Tree-sitter | XDK compiler | +|---------|------|-------------|--------------| +| Symbol detection | Regex | Syntax AST | Compiler AST | +| Syntax diagnostics | Basic patterns | Parser errors | Compiler errors | +| Semantic diagnostics | None | None | Compiler errors and warnings | +| Incomplete syntax | Limited | Error-tolerant parse | Recovers surrounding declarations/blocks; parse errors stop semantic compilation | +| Definition / references | By spelling | Syntax and workspace index | Source identities across discovered/configured graphs; definitions also use host-supplied dependency source indices | +| Symbol monikers | None | None | Artifact-versioned import/export/local identities; source, binary consumers and matching library declarations | +| Library document content | None | None | Negotiated LSP 3.18 content/refresh; revision-owned bundled source; read-only file fallback | +| Hover | Declaration | Declaration | Declaration and validated type | +| Highlights | By spelling | Syntax, read/write distinction | Resolved identities, read/write distinction | +| Completion | Basic | Context-aware | Bounded scope/member/static completion and compatible argument values | +| Inline completion | No | No | Compiler names/argument values; automatic ambiguity suppression and explicit alternatives | +| Rename | Basic | Implemented with syntax limits | Proven source families, parameter slots, aliases and cross-module/resource moves; graph proofs and versioned edits | +| Code actions / formatting | Basic | Implemented with syntax limits | Proven import/member/local fixes and bounded refactorings; lexer indentation, continuations and token-boundary wrapping with literal-preservation checks | +| Documentation actions | None | None | Compiler-derived declaration comment skeletons; parameter/return entries, preserved formatting and versioned edits | +| Folding / selection | Basic / none | Syntax AST | Compiler AST; strictly nested selection spans and exact closing-brace fold columns | +| Signature help | None | Same-file | Selected calls and compiler-fitted incomplete-call candidates | +| Document links | Imports | Workspace index | HTTP(S) URLs in comments/literals plus resolved module/type/alias/wildcard import sources | +| Workspace symbols | Limited | Workspace index | Discovered/configured source graph, including unopened modules; detached per-module index reused from editor/diagnostic compilation | +| Semantic tokens | None | Syntax-based | Resolved names, annotations and bundled-library ownership; declaration/read-only/static/abstract/write and documentation modifiers. Editor lexical scopes retain string/keyword/literal detail | +| Type-definition / implementations | None | None | Source type identities and nominal type/method implementation chains | +| Call hierarchy | None | None | Static selected calls across the complete discovered/configured source graph | +| Inlay hints | None | None | Inferred local/destructured types, lambda parameters/returns and selected positional parameter names | +| Type hierarchy | None | None | Declared extends/implements across the source graph, with generic parents | +| Code lenses / linked editing | Basic | Implemented | Module Run and source-reference counts with native navigation / identity-based local/lambda ranges and lexical import aliases | | Native library | Not needed | Required | Not needed | -| All LSP calls logged | ✅ | ✅ | ✅ | + +The compiler backend bundles the same complete library set as the XDK distribution through a shared +Gradle dependency bundle. All bundled modules are available as read-only binary dependencies, with +matching read-only source targets for declarations with unambiguous compiler metadata. Application sources are discovered from workspace +folders unless an explicit source graph is configured. `sourceModules: []` disables discovery; +`sourceModules: null` restores it. Discovery refreshes on startup, watched files, unsaved header edits, close and workspace-folder changes. +Invalid edited graphs retire old semantic facts and report `SOURCE-GRAPH` until repaired. Workspace symbol +search compiles unopened modules on demand and retains healthy independent modules when others fail. +The navigation index holds one detached build per configured root, reused only when captured source +text, resources and dependency binary/source-index revisions match. Editor and workspace-diagnostic +compilation seed the same index; changing a module rebuilds its affected dependency closure. Removed +roots are evicted, and stale/cancelled work cannot republish an older index generation. Refactoring +proofs remain fresh compilations. There is no disk-persistent index or incremental compiler; warm +queries still capture the graph inputs to detect closed-file changes without relying on watchers. +Workspace implementation and type/call hierarchy queries also include unopened consumers, joining +compiler identities across module artifacts. Hierarchy handles reject changed graph/source/binary +revisions. Detached query results are reused for unchanged inputs; healthy modules remain navigable +when independent neighbors fail. Exact references and refactoring require complete graph proof. +Graph rename additionally covers inline source types and static members through recompilation and +binding/dispatch comparison. Import actions remove proven-unused ordinary imports or sort contiguous +imports while retaining comments; they use versioned edits. Source property/accessor families, +explicit import aliases and member-file type/package moves also use compiler proof. Resource moves +include existing companion directories and simple discovery-owned module roots, updating closed +imports while retaining local package aliases. Versioned text edits precede file/directory moves. +They require client resource-operation support and reject collisions and symlinks. Qualified modules, +implicit package directories and bounded cross-module ownership moves use the L62 graph proof. +Explicit source-graph replacement is available through guarded native `xtc/rename` and +`xtc/renameFiles` proposals with configuration persistence and Undo; generic LSP clients cannot +apply that configuration change through a standard rename. The `construct` keyword stays unchanged. +Compiler mode handles `workspace/didRenameFiles` for local XTC files, refreshing old and new +locations even without watcher events. This also enables LSP4IJ's close/open sequence for renamed +buffers, preventing stale overlays when an open member is renamed again. +Unresolved public type imports are offered only when the proposed import repairs the complete graph +without changing known bindings. Automatic discovery can add source edges; explicit graphs must +already declare them. Binary contracts, annotation/delegation dispatch and unresolved graphs remain +outside rename scope. +Retention regressions exercise multiple module roots under a 512 MiB server heap and verify that +compiler objects are released after analysis. Native startup, focus-recovery and project lifecycle +tests cover restart, replacement and close/reopen. Both editor harnesses display completed and +remaining cases; negotiated LSP work-done progress is tested separately. + +The standalone server closes resources and exits when its stdio client disconnects, including +without a shutdown/exit handshake. Lifecycle regressions cover Tree-sitter, compiler and mock; +see the [server process lifecycle checks](../doc/manual-test-plan.md#server-process-lifecycle-acceptance). + +The compiler backend needs no external XDK installation or `XDK_HOME`. It compiles a module root +and its member tree together, including unsaved member files and packages. Non-file URIs remain +single-source inputs. Source roots use the normal file/same-name-directory module layout. ## Supported LSP Features The canonical feature matrix lives in -[`../doc/plans/plan-ide-integration.md`](../doc/plans/plan-ide-integration.md). -At a high level, the current tree-sitter-backed default provides: +[`../doc/plans/plan-ide-integration.md`](../doc/plans/plan-ide-integration.md#adapter-capability-matrix). +At a high level, the explicit Tree-sitter backend provides: - document symbols, same-file navigation, workspace-symbol search, and best-effort cross-file navigation - context-aware completion @@ -133,7 +241,222 @@ At a high level, the current tree-sitter-backed default provides: - document/range formatting and on-type formatting - selection ranges, linked editing, folding ranges, document links, and signature help -The XDK adapter remains a placeholder for future semantic/compiler-backed behavior. +The opt-in XDK adapter publishes versioned diagnostics and supports hover, definition, references, +highlights, outline, folding, selection, type-definition, implementation lookup and type hierarchy. Definitions and references span the +current module, including closed member files. Workspace-symbol search covers active module +sessions. Immutable Kotlin snapshots supply semantic queries; structural queries use per-source +ASTs. Member edits invalidate all sibling views, and filesystem notifications refresh disk inputs. +Closing an overlay restores the disk version while another document keeps the module session open. + +Type hierarchy follows direct declared `extends` and `implements` edges and retains generic parent +arguments. It requires successful compilation and source locations in the current module. Type-definition +uses copied type identities, including flow narrowing, generic parameters and union targets. +Implementation lookup follows nominal source types and actual method override chains, including +generic, inherited and default bodies. Ordinary properties lead to their effective accessor bodies +or written backing fields; getter/setter declarations have separate implementation sets. Composed +mixin accessors are included, with no standalone inspection of the mixin's constraint. +Ref/Var annotation accessors use the host's existing composed chains, respecting annotation order +and explicit property overrides. Native annotation storage has no invented source body. +A host-supplied dependency source index also permits +definition, type-definition and inherited implementation-body links into that dependency. It does +not supply synthetic redirect targets. A discovered/configured source graph supplies workspace +implementation queries across unopened source modules. Concrete +delegation follows compiler-selected method/property signatures; +interface-valued or cyclic delegates remain unresolved. No forwarding code is generated for lookup. Old hierarchy items cannot resolve into a new compilation. Hierarchy spans the complete source graph; unindexed binary types have no source hierarchy target. Exact references additionally compile all configured source modules, +including unopened consumers and source uses of bundled binary members. No persistent index or +source target for an unindexed binary is invented. + +Completion supplies visible locals/parameters with flow narrowing, implicit members, imported and +enclosing types, and static functions/constants. Qualified member prefixes and bare-name/empty +statement cursors return exact replacement edits. Empty final positional and pending named +argument slots, including direct final bare-name prefixes, offer compatible readable locals/parameters +and implicit properties/constants, fitted by the compiler with inference, conversions and +receiver-specific types. Suggestions combine applicable overloads without selecting one; edits +insert at empty slots or replace the original prefix token. +Qualified property prefixes, grouped values and slots before later written arguments now use +compiler argument-type fitting, preserving the written labels, groups and later arguments. +Compound operand expressions retain ordinary scope/member completion without argument-type filtering. Literal values are not synthesized. +Property reads follow compiler narrowing rules; ordinary properties do not gain local-variable flow +narrowing. Enclosing-instance and imported-constant enumeration remain follow-ups. +Signature help uses exact selected signatures +for completed calls. Incomplete qualified/implicit/static calls expose compiler-fitted candidates, +generic expected types and named parameter mappings, including a pending `name=|` and an existing +closing parenthesis. Function-valued calls also show their full function type while arguments are +missing, with active positional slots and no guessed parameter names or runtime targets. Constructor +candidates include overload filtering, named slots and defaults. Qualified/implicit inner, +virtual, annotated and formal construction reuse compiler preparation. Expected assignment/return +types constrain omitted class parameters; written arguments provide provisional class inference +that can change as more arguments are supplied. Parenthesized array initializers retain their +written dimension arguments and highlight the supplier parameter correctly. Anonymous construction +uses constructors declared inside the retained body or accessible superclass constructors, including +abstract and interface bases. Its declaration is source-owned within the cursor attempt; signature +queries do not emit forwarding constructors or run capture analysis. Repaired normal compilation +still checks the body and its captures. Single-dimensional array brackets support empty/final-name +size completion and signature help, including a missing closing bracket. Proposals fit the real +fixed-size Array constructor's Int parameter. A following supplier is parsed for recovery but is +outside that prefix proof; normal compilation still validates suppliers and element defaults. +Multidimensional construction remains unsupported. Candidates never claim final overload selection. Requests propagate cancellation +and reject stale document/module results. Missing call/group parentheses and index +brackets around the cursor retain completion and signature help at statement/outer-delimiter +boundaries; a cursor at EOF also tolerates missing block braces. Normal compiler diagnostics remain +visible until the text is repaired. Tuple, typed-tuple, list/set/collection and map closers also +recover around a cursor hole. Expression-bodied declarations/property initializers can lack their +terminator, and parameter defaults can lack `)` before a body. Incomplete property initializers use +their real source-owned compiler context. Member/return and parameter type prefixes use the +enclosing compiler scope, including flat qualified names, without registering incomplete declarations. +Explicit cursor queries also recover missing operands inside compound call arguments and validate +the complete proposed expression. Declaration/parameter names, missing map entries and +unterminated literal contents remain outside this recovery. The +[capability matrix](../doc/plans/plan-ide-integration.md) records the remaining syntax/callable limits. + +Declaration-header recovery retains malformed method names and source extents for outline and +folding. Compiler folds keep the heading line and a real closing brace visible; precise end columns +prevent a client from extending a method fold into following declarations. Unclosed bodies retain +their actual EOF boundary without a fabricated delimiter. Explicit cursor queries complete +member/return and parameter type prefixes through the +compiler, including unqualified empty parameter slots and flat qualified names such as +`ecstasy.text.Str`. Qualifiers respect visibility and aliases; candidates include inherited nested +types and typedefs. Only the final written identifier is replaced. No partial signatures or +parameter names are invented. Written leaf names inside parameterized/compound types and bounded +missing angle/group closers also work. These are visible-type suggestions; normal compilation +checks generic constraints. Registered class/method formals, empty generic slots and complete +parameterized qualifiers also work; aliases retain their substituted type. Mid-token queries +replace the entire final identifier, including a generic base before written type arguments. +Empty type operands, qualifier-middle edits, trailing dots, generic-method/multi-return declarations, +constraints and module/package composition headers now have bounded recovery. Unregistered header +formals retain their written constraints for bound-labelled completion and hover, including +sibling constraints and virtual child types reached through those bounds. Unresolved/cyclic bounds +hide outer names without producing candidates. They do not register fabricated components; see +[shared X91–X98 and X106–X108](../doc/manual-test-plan.md#xdkadapter-playbook) and the +[acceptance reports](../doc/manual-test-plan.md#acceptance-reports). + +Class/interface headers now retain their written name and body for structure when a bounded header +fails. Explicit queries complete visible types in `extends`, `implements`, `delegates`, ordinary +`incorporates` and `into`, including qualified/generic leaf names. Empty composition slots also +work. The retained type has no compiler component or inheritance facts; its body never registers +against the enclosing type. Suggestions prove visibility, while normal compilation checks legal +inheritance and constraints. Shared X95 exercises both editor consumers; X96 adds eight +generic/formal and whole-token acceptance variants. + +Static call hierarchy groups selected source call sites by method/lambda, including unopened source +modules. It does not expand virtual dispatch, function values, constructors or binary-only sources. +Semantic tokens classify resolved names and modifiers; highlights distinguish reads and writes. +Inlay hints show inferred local/destructured types, inferred lambda parameters and validated +lambda return types after successful compilation, plus selected positional +parameter names, omitting named arguments and synthetic defaults. These queries use copied facts +and expire with the module snapshot. +Both editor plugins enable compiler hints by default; native editor inlay settings also control +their display. Tree-sitter does not advertise inferred-type hints because it has no compiler type +model. Its negative capability test is active; compiler hint coverage is separate (X42/X259). + +Rename covers locals, lambda and method/constructor parameter slots, source method/property +families, primary-header properties, types, static members and aliases. Configured graphs also +support bounded mixin/delegation/annotation routes, qualified module/package moves and cross-module +ownership changes. Every proposal recompiles the affected graph and compares bindings and dispatch; +binary contracts and unsupported runtime/generated routes remain read-only or refused. +Versioned edits, current source/resource snapshots and complete graph proof are +mandatory. An explicit graph declares the proof boundary; omitted consumers remain unknown. +Completed function-valued calls expose signature types without invented runtime targets or parameter +names. Explicit cursor analysis also retains binary/conditional expressions and following call +arguments, while incomplete values still cannot emit code. Structure-only diagnostics map to source +declaration tokens where available; binary-only structures keep the document fallback. +Compiler-only editor features use the Java lexer for bounded formatting and HTTP(S) links, +compiler proof for ordinary import cleanup, and copied syntax/identities for module run lenses +and local linked editing. See the bounds in the capability matrix above. +A member parse failure clears the module's normal semantic answers until a later correction; +explicit cursor inspection is a separate attempt and stale ranges are not reused. Java parser +recovery retains available per-source syntax for outline, folding and selection, including valid +sibling files. Malformed statements may be omitted; their surrounding declarations can survive. +Compiler mode uses no Tree-sitter fallback or native parser. + +The adapter uses `compileModule(ModuleInfo, ...)`, with a fresh text/membership snapshot for each +attempt, and `semanticSnapshots(errors)` to copy per-source views sharing one identity domain and +inspect implementation chains through the cancellable host listener. The no-argument +`semanticSnapshots()` remains passive. Both implementation and explicit cursor inspection can build +TypeInfo on the serialized compiler worker; request threads query copied facts. +`Compilation.sourceTrees()` supplies structural +views even when parsing errors prevent an assembled `parsed()` tree. See the +[acceptance reports](../doc/manual-test-plan.md#acceptance-reports) for verification and +remaining limits. + +### Editor source-module configuration + +With a compiler build, VS Code can configure automatic source dependency compilation in workspace +settings, without a custom Kotlin host: + +```json +{ + "xtc.compiler.sourceModules": [ + { "name": "Library", "uri": "Library.x", "resourceRoots": ["assets/templates"] }, + { "name": "Consumer", "uri": "Consumer.x", "dependencies": ["Library"] } + ] +} +``` + +A root is an absolute `file:` URI or a URI relative to the sole workspace folder. Multi-root +workspaces use absolute file URIs; URI-encode spaces. Names must match the module declarations. +Settings changes apply without restarting and reanalyse open consumers at their existing versions. +Malformed, cyclic, overlapping or duplicate graphs are rejected as a whole, preserving the previous +graph and showing an error. Identical graphs preserve current analyses; an empty list clears the +configured graph and disables discovery. Setting `sourceModules` to `null` restores discovery. +No Gradle process is started by analysis or edits. + +Each module may supply ordered `resourceRoots` using the same URI rules. Explicit roots replace +conventions; an empty array disables resource lookup, and omitted/null uses compiler layout +deduction (including conventional `src/main/resources`). The evaluated Gradle model +can supply custom source sets and processed resource roots. IntelliJ exposes these +paths in **Languages & Frameworks > Ecstasy Compiler**, and VS Code uses the workspace schema. +Resource contents and root changes invalidate the owning module and its consumers, including +closed-file diagnostic pulls. Resource watchers cover external roots when the client supports +dynamic registration. The [manual playbook](../doc/manual-test-plan.md#xdkadapter-playbook) +covers model import, generated directories and paths/origins controls. + +Other LSP clients can send the same `{ "sourceModules": [...] }` object in +`initializationOptions.xtcCompiler`, answer `workspace/configuration` for `xtc.compiler`, or send +`workspace/didChangeConfiguration` with `{ "xtc": { "compiler": { "sourceModules": [...] } } }`. +Absent settings preserve the current host graph; late replies and replies after shutdown are ignored. +This configures source graphs only; binary artifacts/source indices still use the host API below. +The explicit Tree-sitter backend ignores compiler settings. + +### Dependency artifacts supplied by a host + +The Kotlin `Compilation.toDependency()` extension exports a successful module as immutable bytes +with a detached declaration source index. `XdkDependency.fromBinary(bytes)` accepts a binary without +source locations. A host installs the complete dependency set with +`XtcLanguageServer.replaceCompilerDependencies(dependencies)`; the server invalidates affected +analyses and republishes diagnostics at the current document versions. Direct adapter users can +call `XdkAdapter.replaceDependencies(dependencies)` and reschedule the returned scope keys themselves. +Each compiler attempt deserializes fresh structures. Symbol keys identify a constant only within +the exact artifact/source-index revision; they are not permanent identities across library rebuilds. + +This is the Kotlin host API. Editor clients also configure ordered libraries and attached sources +through their Compiler settings. There is no persistent workspace reference database. Bundled XDK sources use the matching `xtc-sources` Gradle +variant and existing compiler source/debug metadata. Library files open as read-only source views; +they do not become editable compiler sessions. Missing or ambiguous metadata supplies no target. See the +[acceptance reports](../doc/manual-test-plan.md#acceptance-reports) +for ownership, cancellation and replacement guarantees. + +### Automatic recompilation of configured source modules + +The host can register source roots and direct dependency edges with +`XtcLanguageServer.replaceCompilerSourceModules(listOf(XdkSourceModule(...), ...))`. +Edits, closes and watched file changes then rebuild affected dependencies and open consumers +automatically, preserving unchanged consumer document versions. Unsaved buffers override disk. +The whole source closure is captured before compiling in dependency order on the serialized worker; +100 ms debouncing coalesces edits. Only detached artifacts with matching source/dependency inputs +are reused, and obsolete compiler/cursor results cannot publish. + +Failed dependencies publish their original source diagnostics and block consumers with +`DEPENDENCY-FAILED`; an old successful artifact is not reused. Deleted/unreadable roots report +`SOURCE-UNAVAILABLE`, and a declared/configured module-name mismatch reports `PROJECT-MODULE`. +Blocked consumers have no semantic or structural views until dependencies recover. Cyclic or +overlapping source graphs are rejected. Closing sessions prunes unused cached artifacts. + +This uses the discovered workspace graph or an explicit host configuration. Discovery reads module +declarations and source import edges at startup and on watched-file changes; it does not infer Gradle +settings. Unsaved import-edge edits, document close and workspace-folder changes also refresh discovery. +It runs no Gradle build on edits and adds no Java/AST API. See the +[two-module host playbook](../doc/manual-test-plan.md#automatic-source-recompilation-host-checks). ## Context-Aware Completion @@ -164,19 +487,14 @@ is limited to the current class body's declarations. It cannot resolve: - Method overloads or return type narrowing - Conditional mixins or generic type parameters -### What a semantic compiler adapter would add +### Further compiler-backed completion work -A future compiler adapter with full type resolution would enable: -- **Type-resolved member access**: `person.` would show all members of `Person`, - including inherited ones from `Object`, `Hashable`, etc. -- **Overload-aware signatures**: Show all applicable overloads ranked by match quality -- **Smart import suggestions**: Suggest imports for unresolved types based on what - would make the code compile -- **Generic type inference**: Show members on `List` with `String`-substituted - type parameters -- **Scope-aware locals**: Only show variables that are actually in scope at the cursor, - respecting shadowing and block structure -- **Ranked results**: Sort completions by relevance (local > member > imported > global) +Compiler completion now enumerates accessible members with receiver-substituted types, visible +locals/parameters with shadowing and narrowing, and static/imported names in supported cursor +contexts. Incomplete-call signatures use compiler argument fitting and generic inference, without +claiming an overload has been selected. Remaining work includes broader malformed expressions and +callable forms, smart import suggestions and relevance ranking. The canonical matrix records the +precise supported contexts and exclusions. ## Key Components @@ -189,7 +507,9 @@ A future compiler adapter with full type resolution would enable: | `XtcLanguageConstants` | Shared keywords, built-in types, symbol mappings | | `MockAdapter` | Regex-based implementation for testing | | `TreeSitterAdapter` | Tree-sitter based syntax intelligence | -| `XdkAdapter` | Minimal placeholder for future compiler / semantic integration | +| `XdkAdapter` | Serialized compiler worker, bundled XDK, diagnostics and document lifecycle | +| `SemanticModel` / builder | Immutable per-analysis semantic facts and queries; no retained compiler objects | +| `XdkDependency` | Immutable module bytes, revisioned symbol keys and optional detached source locations | ## Building @@ -204,31 +524,6 @@ A future compiler adapter with full type resolution would enable: ./gradlew :lang:lsp-server:fatJar ``` -## Compiler-consumer validation - -Run the required compiler smoke test from the composite root: - -```bash -./gradlew :lang:lsp-server:test --tests org.xvm.lsp.adapter.CompilerConsumerTest --rerun-tasks --no-build-cache \ - -PincludeBuildLang=true -PincludeBuildAttachLang=true -``` - -The test compiles a module through the existing Java embedding API. Gradle supplies `javatools` -and the compiled Ecstasy/native-bridge module variants, including their dependencies. No installed -XDK, `XDK_HOME`, distribution archive or IDE packaging task is required. Missing module inputs fail -the test rather than skipping it. `compilerTestModules` declares the inputs, and a test JVM argument -provider carries their directories into the shared fixture without task-action access to Gradle's -project model. - -The shared CI change classifier includes compiler, libraries and shared build inputs in the core -checks and their cache fingerprint, independently of IDE publication. Its result gate requires -this suite to execute with zero skips, failures or errors. Add consumer suites to that gate as -later integration slices land. - -Lang's lifecycle leaves IntelliJ and VS Code packaging detached unless explicitly enabled with -`-PincludeBuildAttachIntellijPlugin=true` or `-PincludeBuildAttachVsCodeExtension=true`; direct plugin -tasks remain available. The root's existing lang inclusion defaults are unchanged. - ## Tree-sitter Native Library The tree-sitter adapter requires native libraries (`libtree-sitter-xtc`). These are built @@ -257,8 +552,8 @@ included build, which has no `gradle.properties` of its own. | Property | Default | Description | |----------|---------|-------------| | `log` | `INFO` | Log level for XTC LSP/DAP servers. Valid: `TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR` | -| `lsp.adapter` | `treesitter` | Parsing backend. Valid: `treesitter`, `mock`, `compiler` | -| `lsp.semanticTokens` | `true` | Enable semantic token highlighting (tree-sitter lexer-based) | +| `lsp.adapter` | `compiler` | Parsing backend. Valid: `treesitter`, `mock`, `compiler` | +| `lsp.semanticTokens` | `true` | Enable semantic token highlighting for the selected adapter | | `includeBuildLang` | `false` | Include `lang` as a composite build (IDE visibility, task addressability) | | `includeBuildAttachLang` | `false` | Wire lang lifecycle tasks to root build (requires `includeBuildLang=true`) | | `lsp.buildSearchableOptions` | `false` | Build IntelliJ searchable options index | @@ -304,7 +599,7 @@ The LSP server logs to both stderr (for IntelliJ's Language Servers panel) and a ### Log File Location ```bash -~/.xtc/logs/lsp-server.log +~/.xtc/logs/lsp/server-*/server.log ``` Log messages use SLF4J with a short class name (`%logger{0}`) to identify their source: @@ -320,10 +615,46 @@ Log messages use SLF4J with a short class name (`%logger{0}`) to identify their | `WorkspaceIndexer` | Background file scanner | | `WorkspaceIndex` | Symbol index | +### Compiler queue and API timing + +Execution tracing is enabled at INFO in ordinary runs and tests. Each process writes JSON lines to +`~/.xtc/logs/lsp/server--/lsp-trace--.jsonl`, as well as the existing stderr/server log. +Use `XTC_LSP_TRACE_DIR` or `-Dxtc.trace.directory` to select a directory; use +`XTC_LSP_TRACE_LEVEL=OFF` or `-Dxtc.trace.level=OFF` to disable it. System properties take precedence. +Files roll at 10 MB by default with seven days/50 MB of archives retained per stream. +Active files are additional. Machine-local plugin settings control these limits and keep up to +five stopped-server sessions; pruning runs on startup and protects active processes. Use +`XTC_LSP_LOG_DIR` or `-Dxtc.logs.directory` to change the parent directory for both logs; an +explicit trace-directory override remains independent. Export from either plugin saves bounded +recent tails and status, with truncation described in the ZIP manifest. + +Each event records sequence, wall time, process/thread, operation, URI and span/parent IDs. +Compiler queue events include `queueSize` and the ordered `queuedJobs` list, plus separate +`debouncingSize`/`debouncingJobs` and `runningSize`/`runningJobs`. Job labels have the form +`#91 compile file:///workspace/Startup.x`; cursor, project-query and rename-proof jobs are named too. +Submission, enqueue and execution counters distinguish debounce/coalescing from actual compiler work. +Cancellation of running work remains visible until the worker returns. `waitMs` includes debounce; +`runMs` measures execution and `elapsedMs` includes the entire job lifetime. + +Javatools spans time full/source-tree/cursor compilation, parser/lexer operations, dependency +read/write, TypeInfo queries, footprint inspection and semantic-copy phases. AST/constant getters +inside a copy phase are included in its duration rather than emitting a line for every getter. +`activeApiThreads` counts threads inside instrumented API boundaries; nested calls on the same +thread do not imply parallel compilation. A returned compilation can still contain diagnostics. +Request spans run from server receipt through reply serialization/write; they include asynchronous +queue waits, but not the editor's later rendering time. Notifications record document versions and +diagnostic counts. Source buffers, protocol payloads and exception messages are excluded. + +JVM test logs and traces share retained `build/reports/server-logs/server--/` directories; packaged-server tests use per-test +subdirectories there so their traces survive temporary workspace cleanup. IntelliJ playbook runs +place each child server's trace in their report's `server-trace/` directory, preserving restarts as +separate files. +The IntelliJ provider forwards the trace directory/level JVM properties to its child server. + ### Tailing Logs ```bash -tail -f ~/.xtc/logs/lsp-server.log +tail -f ~/.xtc/logs/lsp/server-*/server.log ``` ## Code Formatting @@ -332,13 +663,20 @@ The LSP server provides three levels of formatting support: | Capability | Trigger | What It Does | |------------|---------|--------------| -| **Document Formatting** | Reformat action (`Ctrl+Alt+L`) | Cleans trailing whitespace and final newline | -| **Range Formatting** | Format selection | Same cleanup on a selected region | -| **On-Type Formatting** | Typing `Enter`, `}`, `;` | AST-aware auto-indentation as you type | +| **Document Formatting** | Reformat action (`Ctrl+Alt+L`) | Adapter-specific indentation/layout plus configured whitespace cleanup | +| **Range Formatting** | Format selection | The same rules, confined to the selected lines | +| **On-Type Formatting** | Typing `Enter`, `}`, `;` | Auto-indentation using the adapter's syntax model; compiler mode uses its Java lexer | + +Compiler mode uses the Java lexer for nesting/operator continuation indentation, wrapping at +expression/list token boundaries using `maxLineWidth`, and standalone block-comment margins. +Code and literal token spellings are verified after formatting. Literal/template contents and +relative comment layout are preserved; comments are not reflowed and literals are not split. +Range formatting is confined to selected lines. On-type formatting adjusts indentation without +wrapping lines. A lexical error refuses formatting. Shared X249/X250 cover exact output and history. ### On-Type Formatting (Auto-Indent) -When you type a trigger character, the LSP server uses tree-sitter AST context to +In Tree-sitter mode, typing a trigger character uses the Tree-sitter AST context to determine the correct indentation and sends back edits to fix it. This is strictly better than regex-based TextMate indentation rules because it understands: @@ -413,7 +751,7 @@ but behaviors we must work around. | Issue | Impact | Workaround | Reference | |-------|--------|------------|-----------| | **Duplicate server spawning** | LSP4IJ may call `start()` concurrently for multiple `.x` files, briefly spawning extra LSP server processes | Harmless -- extras are killed within milliseconds. We guard notifications with `AtomicBoolean` to avoid duplicates | [lsp4ij#888](https://github.com/redhat-developer/lsp4ij/issues/888) | -| **"Show Logs" link in error popups** | When the LSP server returns an error (e.g., internal exception), the error notification shows "Show Logs" / "Disable error reporting". The "Show Logs" link opens `idea.log`, **not** the LSP server log file, and may be unclickable | Tail the actual LSP log directly: `tail -f ~/.xtc/logs/lsp-server.log`. Also check the LSP Console: **View -> Tool Windows -> Language Servers -> Logs tab** | LSP4IJ limitation | +| **"Show Logs" link in error popups** | When the LSP server returns an error (e.g., internal exception), the error notification shows "Show Logs" / "Disable error reporting". The "Show Logs" link opens `idea.log`, **not** the LSP server log file, and may be unclickable | Tail the actual LSP log directly: `tail -f ~/.xtc/logs/lsp/server-*/server.log`. Also check the LSP Console: **View -> Tool Windows -> Language Servers -> Logs tab** | LSP4IJ limitation | | **Error notification popup not actionable** | The `textDocument/semanticTokens Internal error` popup's links ("Show Logs", "Disable error reporting", "More") may not respond to clicks in some IntelliJ versions | The popup auto-dismisses. Check the LSP Console Logs tab for the actual server-side stack trace | LSP4IJ UI limitation | ### IntelliJ Platform Issues @@ -430,7 +768,7 @@ but behaviors we must work around. | Log | Location | Contents | |-----|----------|----------| -| LSP server log | `~/.xtc/logs/lsp-server.log` | All `XtcLanguageServer`, `TreeSitterAdapter`, `XtcParser`, `XtcQueryEngine` messages | +| LSP server log | `~/.xtc/logs/lsp/server-*/server.log` | All `XtcLanguageServer`, `TreeSitterAdapter`, `XtcParser`, `XtcQueryEngine` messages | | IntelliJ `idea.log` | Sandbox `log/idea.log` (path shown at `runIde` startup) | Platform errors, plugin loading, EDT violations | | LSP Console (IDE) | **View -> Tool Windows -> Language Servers -> Logs** | JSON-RPC traces, server stderr | | Gradle console | Terminal running `runIde` | Build output + tailed LSP log (real-time) | @@ -443,7 +781,7 @@ contain the actual LSP server error. Instead: 1. Open the **Language Servers** tool window (bottom panel, next to Terminal) 2. Select **Ecstasy Language Server** -> **Logs** tab 3. Look for `SEVERE:` or stack traces in the log output -4. Or tail the server log directly: `tail -f ~/.xtc/logs/lsp-server.log` +4. Or tail the server log directly: `tail -f ~/.xtc/logs/lsp/server-*/server.log` **When IntelliJ complains about "slow operations":** @@ -459,3 +797,56 @@ in slow operation reports: - [Tree-sitter Feature Matrix](../tree-sitter/doc/functionality.md) - What Tree-sitter can/cannot do - [Tree-sitter Integration Plan](../doc/plans/plan-tree-sitter.md) - Full implementation details - [Formatting Plan](../doc/plans/formatting-plan.md) - On-type formatting design, configuration architecture, industry survey + + +The server uses UTF-16 positions and adapts hover, outline, symbol +kinds and push diagnostic metadata to client capabilities. Long compiler queries support negotiated +work-done progress and request-owned cancellation. Refresh requests are coalesced for negotiated +diagnostics, semantic tokens, inlays, lenses and folding. `$/setTrace` accepts `off`, `messages` and +`verbose`; server `$/logTrace` messages include timing and correlation metadata without source +buffers. Client-supplied partial-result tokens now stream bounded batches for references, workspace +symbols/diagnostics, outlines, navigation and hierarchy relations. Ordinary requests retain full +responses. Initial Tree-sitter scanning reports its actual future lifetime and progress. + +The compiler adapter copies successful constant-initializer facts before temporary-method disposal, preserving +navigation and rename provenance without retaining clone ASTs. Shared X142/X143 exercise initializer +semantics and partial symbols. X140/X141 exercise UTF-16 positions and runtime trace settings. +Completion kinds and code-action forms/preferred metadata follow negotiation; legacy action clients +use one-use, revision-checked commands only if they support `workspace.applyEdit`. + +### Portable compiler symbol identities + +`textDocument/moniker` uses the `ecstasy-artifact-v1` scheme and scheme-level uniqueness. +Exported declarations and their binary consumers match by normalized artifact content and constant +index. Build timestamps, absolute checkout paths and optional source attachments do not define the +identity; code/signature/resource changes do. Private and method-local components are local. Register locals, +lambdas, unresolved bindings and unsuccessful compilations supply no IDs. Identical normalized +artifacts intentionally match across projects; module-name spelling alone never establishes a match. +See [acceptance reports](../doc/manual-test-plan.md#acceptance-reports). + +Matching bundled library declarations now answer moniker requests directly, including declarations +not previously referenced by a consumer. Ambiguous source spans still return no identity. +Supporting clients receive `ecstasy-library` URIs through LSP 3.18 `workspace/textDocumentContent`. +URIs include the binary and source-archive revisions; registered content comes from the matching +bundle, never from an arbitrary path or an editor overlay. Compiler input replacement refreshes +fetched views; immutable revision URIs keep their original text. Closing the server retires its +content registry. Line-ending normalization preserves declaration positions; other client text +rewrites withhold monikers until matching presentation is restored. Clients without this capability retain protected file views (LSP4IJ UP25). +External host source indexes remain host-owned file locations: they supply declaration ranges, +not verified source text, and are not silently promoted to virtual library snapshots. +Selected X31/X252/X253 pass in both editors; full-catalog/release acceptance remains separate. + +## Compiler inline completion + +Compiler mode negotiates `textDocument/inlineCompletion` separately from popup completion. +Suggestions reuse copied semantic facts and replace only a prefix ending at the caret. Automatic +requests offer one unambiguous name/value; explicit requests can offer alternatives. Selected +popup items constrain both range and extension. Imports requiring additional edits, snippets with +placeholders, mid-token edits and read-only libraries are excluded. Cancellation and newer +versions retire pending suggestions. No new embedding/AST API or generative service is involved. + +Both editor clients have native providers. IntelliJ's direct invocation currently sends Automatic +and omits popup selection (UP26); shared X255–X258 distinguish native from protocol coverage. + +See the [acceptance report format](../doc/manual-test-plan.md#acceptance-reports) for how native +and protocol coverage are recorded. diff --git a/lang/lsp-server/build.gradle.kts b/lang/lsp-server/build.gradle.kts index 257fee42795..8a4d2a7053b 100644 --- a/lang/lsp-server/build.gradle.kts +++ b/lang/lsp-server/build.gradle.kts @@ -1,4 +1,3 @@ -import org.gradle.process.CommandLineArgumentProvider import java.time.Instant plugins { @@ -46,19 +45,19 @@ plugins { // ============================================================================= // The LSP server can use different parsing backends: // -// treesitter - Tree-sitter parsing (DEFAULT, syntax-level intelligence, needs native lib) +// treesitter - Tree-sitter parsing (syntax-level intelligence, needs native lib) +// compiler - The XTC compiler with bundled XDK libraries (DEFAULT, diagnostics and semantics) // mock - Regex-based parsing (no native dependencies, for testing/fallback) // // Set via Gradle property: -Plsp.adapter=mock (to override default) // Or in gradle.properties: lsp.adapter=mock // -// Default is 'treesitter' which provides syntax-aware features (native library bundled). -// Use 'mock' for basic regex-based functionality if tree-sitter has issues. +// Default is 'compiler'. -Dxtc.lsp.adapter overrides it at server startup without rebuilding. // ============================================================================= // Resolve via xdkProperties which reads from the composite root's gradle.properties // (project.findProperty() only sees the included build's own gradle.properties, which doesn't exist) -val lspAdapter: String = xdkProperties.stringValue("lsp.adapter", "treesitter") -val lspSemanticTokens: String = xdkProperties.stringValue("lsp.semanticTokens", "false") +val lspAdapter: String = xdkProperties.stringValue("lsp.adapter", "compiler") +val lspSemanticTokens: String = xdkProperties.stringValue("lsp.semanticTokens", "true") // Log level: -Plog=DEBUG or XTC_LOG_LEVEL=DEBUG (default: INFO) // xdkProperties checks: env LOG -> gradle prop -> system prop -> composite root gradle.properties @@ -124,9 +123,9 @@ repositories { // Consume the tree-sitter native libraries for all supported platforms. // This library is built on-demand using Zig cross-compilation. -// Test consumers resolve the compiled modules from the composite, without a distribution archive. -val compilerTestModules = - configurations.create("compilerTestModules") { +// Consume the compiled XDK modules through the same variants as other composite consumers. +val compilerModules = + configurations.create("compilerModules") { isCanBeConsumed = false isCanBeResolved = true attributes { @@ -135,13 +134,17 @@ val compilerTestModules = } } -abstract class CompilerTestModulesArguments : CommandLineArgumentProvider { - @get:InputFiles - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val modules: ConfigurableFileCollection - - override fun asArguments(): Iterable = listOf("-Dxtc.test.modules=${modules.asPath}") -} +// Source archives are consumed as resources; no distribution installation or extraction task. +val compilerSources = + configurations.create("compilerSources") { + isCanBeConsumed = false + isCanBeResolved = true + isTransitive = false + attributes { + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.LIBRARY)) + attribute(LibraryElements.LIBRARY_ELEMENTS_ATTRIBUTE, objects.named("xtc-sources")) + } + } val treeSitterNativeLib = configurations.create("treeSitterNativeLib") { @@ -154,12 +157,15 @@ val treeSitterNativeLib = } dependencies { - testImplementation(libs.javatools) - compilerTestModules(libs.xdk.ecstasy) - compilerTestModules(libs.javatools.bridge) + compilerModules(libs.bundles.xdk.libraries) + compilerSources(libs.bundles.xdk.libraries) // Native library from tree-sitter project treeSitterNativeLib(project(path = ":tree-sitter", configuration = "nativeLibraryElements")) + // The XTC compiler, for the XdkAdapter: real diagnostics rather than a syntax approximation. + // Substituted from the included build when lang is built inside the composite. + implementation(libs.javatools) + // LSP4J - Eclipse LSP implementation for Java // Bundled in fat JAR for out-of-process execution (not provided by IntelliJ) implementation(libs.lang.lsp4j) @@ -203,24 +209,62 @@ val copyNativeLibToResources = into(layout.buildDirectory.dir("generated/resources/native-libraries")) } +val compilerModuleFiles = compilerModules.asFileTree.matching { include("**/*.xtc") } +val compilerModuleIndex = + tasks.register("compilerModuleIndex") { + destinationFile.set(layout.buildDirectory.file("generated/compiler/modules.properties")) + inputs.files(compilerModuleFiles).withPathSensitivity(PathSensitivity.NONE) + property("modules", compilerModuleFiles.elements.map { files -> files.map { it.asFile.name }.sorted().joinToString(",") }) + } + +val compilerSourceIndex = + tasks.register("compilerSourceIndex") { + destinationFile.set(layout.buildDirectory.file("generated/compiler/sources.properties")) + inputs.files(compilerSources).withPathSensitivity(PathSensitivity.NONE) + property("archives", compilerSources.elements.map { files -> files.map { it.asFile.name }.sorted().joinToString(",") }) + } + // Add native library resources to source sets sourceSets.main { resources.srcDir(copyNativeLibToResources) } +// Ensure native library is copied before processResources +val processResources = + tasks.named("processResources") { + dependsOn(copyNativeLibToResources) + from(compilerModuleFiles) { into("org/xvm/lsp/xdk") } + from(compilerModuleIndex) { into("org/xvm/lsp/xdk") } + from(compilerSourceIndex) { into("org/xvm/lsp/xdk") } + from(compilerSources) { into("org/xvm/lsp/xdk/sources") } + } + tasks.withType().configureEach { options.compilerArgs.add("-Xlint:deprecation") } val classes = tasks.named("classes") +tasks.withType().configureEach { + systemProperty( + "xtc.logs.directory", + layout.buildDirectory + .dir("reports/server-logs") + .get() + .asFile.absolutePath, + ) + // Keep traces in the same process-owned directory so closed-session retention covers both logs. +} + tasks.test { - jvmArgumentProviders.add( - objects.newInstance().apply { - modules.from(compilerTestModules) - }, + systemProperty( + "xtc.lsp.retentionCycles", + providers.gradleProperty("lsp.retentionCycles").orElse("120").get(), ) - useJUnitPlatform() + // Prefer the real logger over javatools' shaded no-op provider, without setting a provider + // property on Gradle's separate bootstrap classloader (which cannot see test dependencies). + classpath = configurations.testRuntimeClasspath.get().filter { it.name.startsWith("logback-classic-") } + classpath + useJUnitPlatform { excludeTags("compiler-stdio") } testLogging { events("failed") } @@ -239,6 +283,12 @@ tasks.test { dir = dir.parentFile } systemProperty("xtc.composite.root", dir.absolutePath) + inputs.files( + rootProject.layout.projectDirectory.file("doc/manual-test-plan.md"), + rootProject.layout.projectDirectory.file("test-fixtures/compiler-playbook/scenarios.json"), + rootProject.layout.projectDirectory.file("test-fixtures/semantic-highlighting/SemanticColors.x"), + rootProject.layout.projectDirectory.file("test-fixtures/color-values/ColorPrototype.x"), + ) } tasks.jar { @@ -270,7 +320,18 @@ val fatJar = configurations.runtimeClasspath .get() .filter { it.name.endsWith("jar") } - .map { zipTree(it) } + .map { jar -> + zipTree(jar).matching { + // javatools is itself a shaded jar and bundles slf4j-nop. Merged in with + // duplicatesStrategy = EXCLUDE, whose first entry wins, its NOP provider + // shadowed logback's and the server logged nothing at all - no compile + // times, no diagnostics counts, no errors. Keep the NOP binding out. + exclude("org/slf4j/nop/**") + if (jar.name.startsWith("javatools")) { + exclude("META-INF/services/org.slf4j.spi.SLF4JServiceProvider") + } + } + } }) // Exclude signature files from dependencies (they become invalid in fat JAR) @@ -289,6 +350,31 @@ tasks.test { systemProperty("xtc.lsp.jar", serverJar.get().asFile.absolutePath) } +// Exercise the distributed artifact through the production launcher, separately from unit tests. +val compilerStdioTest = + tasks.register("compilerStdioTest") { + group = "verification" + description = "Test the packaged compiler LSP over stdio (requires -Plsp.adapter=compiler)" + testClassesDirs = + sourceSets.test + .get() + .output.classesDirs + classpath = sourceSets.test.get().runtimeClasspath + useJUnitPlatform { includeTags("compiler-stdio") } + + val serverJar = fatJar.flatMap { it.archiveFile } + inputs.file(serverJar).withPropertyName("serverJar").withPathSensitivity(PathSensitivity.NONE) + dependsOn(fatJar) + systemProperty("xtc.lsp.jar", serverJar.get().asFile.absolutePath) + testLogging { events("failed") } + } + +tasks.check { + if (lspAdapter in listOf("compiler", "xtc", "full")) { + dependsOn(compilerStdioTest) + } +} + // ============================================================================= // Consumable configuration for IDE plugins // ============================================================================= @@ -331,3 +417,26 @@ val assemble = tasks.named("assemble") { dependsOn(fatJar) } + +// Shared native-editor fixtures are built through the same embedded compiler as the server. +tasks.register("prepareLibraryPlaybook") { + group = "verification" + description = "Build binary libraries and matching sources for both editor playbooks" + classpath = sourceSets.test.get().runtimeClasspath + // javatools shades a no-op provider. This standalone JVM can select Logback directly; + // do not put this property on Gradle's test-worker/bootstrap classloader. + systemProperty("slf4j.provider", "ch.qos.logback.classic.spi.LogbackServiceProvider") + systemProperty( + "xtc.logs.directory", + layout.buildDirectory + .dir("reports/library-playbook/server-logs") + .get() + .asFile.absolutePath, + ) + mainClass.set("org.xvm.lsp.adapter.PrepareLibraryPlaybook") + val scenarios = rootProject.layout.projectDirectory.file("test-fixtures/compiler-playbook/scenarios.json") + val output = layout.buildDirectory.dir("generated/compiler-playbook/libraries") + inputs.file(scenarios) + outputs.dir(output) + args(scenarios.asFile.absolutePath, output.get().asFile.absolutePath) +} diff --git a/lang/lsp-server/editor-setup.md b/lang/lsp-server/editor-setup.md index cf4ae0f50a3..71a64868927 100644 --- a/lang/lsp-server/editor-setup.md +++ b/lang/lsp-server/editor-setup.md @@ -5,7 +5,7 @@ text editors like Neovim, Emacs, or VS Code -- without using the IntelliJ plugin ## Prerequisites -- **Java 25+** is required for the default tree-sitter adapter (it uses the +- **Java 25+** is required for the server, including the optional tree-sitter adapter (which uses the Foreign Function & Memory API). Verify with `java -version`. - If you don't have Java 25, you can build with the regex-based mock adapter instead (see the build step below). @@ -52,7 +52,7 @@ echo -ne 'Content-Length: 73\r\n\r\n{"jsonrpc":"2.0","id":1,"method":"initialize ``` You should see a JSON-RPC response containing the server's capabilities. The -server also writes logs to `~/.xtc/logs/lsp-server.log`. +server also writes logs to `~/.xtc/logs/lsp/server-*/server.log`. ## Step 3: Connect to Your Editor @@ -449,7 +449,7 @@ nvim --headless \ After running any of the above, verify the server logged the requests: ```bash -tail -20 ~/.xtc/logs/lsp-server.log +tail -20 ~/.xtc/logs/lsp/server-*/server.log ``` You should see entries like: @@ -470,13 +470,13 @@ TreeSitterAdapter - parsed in 2.3ms, 0 errors, 31 symbols (query: 1.2ms) The LSP server writes detailed logs to: ``` -~/.xtc/logs/lsp-server.log +~/.xtc/logs/lsp/server-*/server.log ``` Tail the log while testing to see server activity in real time: ```bash -tail -f ~/.xtc/logs/lsp-server.log +tail -f ~/.xtc/logs/lsp/server-*/server.log ``` ### "UnsatisfiedLinkError" or tree-sitter failures diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AbstractAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AbstractAdapter.kt index f0f8051844d..2ed9b84b634 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AbstractAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AbstractAdapter.kt @@ -2,25 +2,7 @@ package org.xvm.lsp.adapter import org.slf4j.Logger import org.slf4j.LoggerFactory -import org.xvm.lsp.adapter.CallHierarchyItem -import org.xvm.lsp.adapter.CodeAction -import org.xvm.lsp.adapter.CodeLens -import org.xvm.lsp.adapter.DocumentHighlight -import org.xvm.lsp.adapter.DocumentLink -import org.xvm.lsp.adapter.FoldingRange -import org.xvm.lsp.adapter.FormattingConfig -import org.xvm.lsp.adapter.FormattingOptions -import org.xvm.lsp.adapter.InlayHint import org.xvm.lsp.adapter.LanguageConstants.toHoverMarkdown -import org.xvm.lsp.adapter.LinkedEditingRanges -import org.xvm.lsp.adapter.Position -import org.xvm.lsp.adapter.PrepareRenameResult -import org.xvm.lsp.adapter.Range -import org.xvm.lsp.adapter.SelectionRange -import org.xvm.lsp.adapter.SemanticTokens -import org.xvm.lsp.adapter.SignatureHelp -import org.xvm.lsp.adapter.TypeHierarchyItem -import org.xvm.lsp.adapter.WorkspaceEdit import org.xvm.lsp.model.Diagnostic import org.xvm.lsp.model.Location import org.xvm.lsp.model.SymbolInfo @@ -35,10 +17,9 @@ import org.xvm.lsp.model.SymbolInfo * - Utility method for position-in-range checking * - No-op [java.io.Closeable] implementation (override in subclasses that need cleanup) * - * Concrete adapters override only the methods they actually implement. - * All unimplemented methods log the full input parameters and return null/empty, - * so the log trace shows exactly what the IDE requested even when the feature - * is not yet available. + * Concrete adapters override only the methods they actually implement. All unimplemented methods + * log the full input parameters and return null/empty, so the log trace shows exactly what the IDE + * requested even when the feature is not yet available. * * Concrete implementations live in subpackages: * - `adapter.mock.MockAdapter` — regex-based testing adapter @@ -48,8 +29,8 @@ import org.xvm.lsp.model.SymbolInfo @Suppress("LoggingSimilarMessage") abstract class AbstractAdapter : Adapter { /** - * Logger instance for this adapter, using the concrete class name. - * Lazily initialized to use the actual subclass type. + * Logger instance for this adapter, using the concrete class name. Lazily initialized to use + * the actual subclass type. */ protected val logger: Logger by lazy { LoggerFactory.getLogger(this::class.java) @@ -110,7 +91,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): List { - logger.warn("[NOT IMPLEMENTED] getDocumentHighlights: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] getDocumentHighlights: uri={}, line={}, column={}", + uri, + line, + column, + ) return emptyList() } @@ -131,7 +117,11 @@ abstract class AbstractAdapter : Adapter { uri: String, content: String, ): List { - logger.warn("[NOT IMPLEMENTED] getDocumentLinks: uri={}, content={} bytes", uri, content.length) + logger.warn( + "[NOT IMPLEMENTED] getDocumentLinks: uri={}, content={} bytes", + uri, + content.length, + ) return emptyList() } @@ -144,7 +134,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): SignatureHelp? { - logger.warn("[NOT IMPLEMENTED] getSignatureHelp: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] getSignatureHelp: uri={}, line={}, column={}", + uri, + line, + column, + ) return null } @@ -153,7 +148,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): PrepareRenameResult? { - logger.warn("[NOT IMPLEMENTED] prepareRename: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] prepareRename: uri={}, line={}, column={}", + uri, + line, + column, + ) return null } @@ -224,11 +224,11 @@ abstract class AbstractAdapter : Adapter { ): List = formatContent(content, options, range) /** - * Basic formatting: trailing whitespace removal and final newline insertion. - * If [range] is non-null, only lines within that range are formatted. + * Basic formatting: trailing whitespace removal and final newline insertion. If [range] is + * non-null, only lines within that range are formatted. * - * Shared by all adapters -- override [formatDocument]/[formatRange] in subclasses - * that need different formatting logic. + * Shared by all adapters -- override [formatDocument]/[formatRange] in subclasses that need + * different formatting logic. */ private fun formatContent( content: String, @@ -259,7 +259,12 @@ abstract class AbstractAdapter : Adapter { } // Insert final newline if requested and missing (only for full-document format) - if (range == null && options.insertFinalNewline && content.isNotEmpty() && !content.endsWith("\n")) { + if ( + range == null && + options.insertFinalNewline && + content.isNotEmpty() && + !content.endsWith("\n") + ) { val lastLine = lines.size - 1 val lastCol = lines[lastLine].length add( @@ -291,7 +296,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): Location? { - logger.warn("[NOT IMPLEMENTED] findDeclaration: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] findDeclaration: uri={}, line={}, column={}", + uri, + line, + column, + ) return null } @@ -300,7 +310,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): Location? { - logger.warn("[NOT IMPLEMENTED] findTypeDefinition: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] findTypeDefinition: uri={}, line={}, column={}", + uri, + line, + column, + ) return null } @@ -389,7 +404,13 @@ abstract class AbstractAdapter : Adapter { ch: String, options: FormattingOptions, ): List { - logger.warn("[NOT IMPLEMENTED] onTypeFormatting: uri={}, line={}, column={}, ch='{}'", uri, line, column, ch) + logger.warn( + "[NOT IMPLEMENTED] onTypeFormatting: uri={}, line={}, column={}, ch='{}'", + uri, + line, + column, + ch, + ) return emptyList() } @@ -398,7 +419,12 @@ abstract class AbstractAdapter : Adapter { line: Int, column: Int, ): LinkedEditingRanges? { - logger.warn("[NOT IMPLEMENTED] getLinkedEditingRanges: uri={}, line={}, column={}", uri, line, column) + logger.warn( + "[NOT IMPLEMENTED] getLinkedEditingRanges: uri={}, line={}, column={}", + uri, + line, + column, + ) return null } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/Adapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/Adapter.kt index 4981b60f34e..5488d91ef10 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/Adapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/Adapter.kt @@ -1,38 +1,67 @@ package org.xvm.lsp.adapter +import org.xvm.lsp.index.WorkspaceIndex +import org.xvm.lsp.index.WorkspaceIndexer import org.xvm.lsp.model.CompilationResult import org.xvm.lsp.model.Diagnostic import org.xvm.lsp.model.Location import org.xvm.lsp.model.SymbolInfo +import org.xvm.lsp.treesitter.SemanticTokenEncoder import java.io.Closeable +import java.util.concurrent.CompletableFuture /** * Interface for adapting XTC compiler operations into clean, immutable results. * - * The adapter layer isolates the LSP server from the compiler's mutable internals. - * All results are immutable and thread-safe. + * The adapter layer isolates the LSP server from the compiler's mutable internals. All results are + * immutable and thread-safe. * * ## Available Implementations (in subpackages) - * - * | Adapter | Package | Backend | Use Case | - * |---------|---------|---------|----------| - * | `MockAdapter` | `adapter.mock` | Regex | Testing and fallback | - * | `TreeSitterAdapter` | `adapter.treesitter` | Tree-sitter | Syntax-aware (~80% LSP features) | - * | `XdkAdapter` | `adapter.xdk` | XDK Compiler | (future) Full semantic features | + * | Adapter | Package | Backend | Use Case | + * |---------------------|----------------------|--------------|----------------------------------| + * | `MockAdapter` | `adapter.mock` | Regex | Testing and fallback | + * | `TreeSitterAdapter` | `adapter.treesitter` | Tree-sitter | Syntax-aware (~80% LSP features) | + * | `XdkAdapter` | `adapter.xdk` | XDK Compiler | Compiler semantic features | * * ## Backend Selection * - * Select at build time: `./gradlew :lang:lsp-server:build -Plsp.adapter=treesitter` + * Select at startup with `-Dxtc.lsp.adapter=compiler`, or set the build default with `-Plsp.adapter=compiler`. * - * - `treesitter` (default): Syntax-aware parsing, requires native library + * - `compiler` (default): Compiler diagnostics and semantic features, with bundled XDK modules + * - `treesitter`: Syntax-aware parsing, requires native library * - `mock`: Regex-based, no native dependencies */ interface Adapter : Closeable { override fun close() {} + /** Schemes owned by this backend, never writable editor overlays. */ + val readOnlyDocumentSchemes: Set get() = emptySet() + + /** Resolve only a registered matching source, never an arbitrary client-provided file path. */ + fun readOnlyDocument(uri: String): ReadOnlyDocument? = null + + /** Features the server may advertise for this backend. */ + val capabilities: Set + get() = + AdapterCapability.entries + .filterNot { + it in + setOf( + AdapterCapability.DECLARATION, + AdapterCapability.TYPE_HIERARCHY, + AdapterCapability.TYPE_DEFINITION, + AdapterCapability.IMPLEMENTATION, + AdapterCapability.MONIKER, + AdapterCapability.CALL_HIERARCHY, + AdapterCapability.INLAY_HINT, + AdapterCapability.INLINE_COMPLETION, + AdapterCapability.DOCUMENT_COLOR, + ) + }.toSet() + /** - * Human-readable name of this adapter for display in logs and UI. - * Examples: "Mock", "TreeSitter", "Compiler" + * Human-readable name of this adapter for display in logs and UI. Examples: "Mock", + * "TreeSitter", "Compiler" */ val displayName: String get() = this::class.simpleName ?: "Unknown" @@ -40,17 +69,17 @@ interface Adapter : Closeable { /** * Perform a health check to verify the adapter is working correctly. * - * For adapters using native code (e.g., TreeSitterAdapter), this verifies - * that the native library is loaded and functional. + * For adapters using native code (e.g., TreeSitterAdapter), this verifies that the native + * library is loaded and functional. * * @return true if the adapter is healthy, false otherwise */ fun healthCheck(): Boolean = true /** - * Editor-provided formatting configuration from `workspace/configuration`. - * Set by the language server after receiving config from the client. - * Used by [FormattingConfig.resolve] as a fallback before LSP options. + * Editor-provided formatting configuration from `workspace/configuration`. Set by the language + * server after receiving config from the client. Used by [FormattingConfig.resolve] as a + * fallback before LSP options. */ var editorFormattingConfig: FormattingConfig? get() = null @@ -65,20 +94,22 @@ interface Adapter : Closeable { /** * Compile a source file and return the result. * - * **LSP capability:** Triggered by `textDocument/didOpen` and `textDocument/didChange`. - * The client sends the full document text; the server parses it and publishes diagnostics. + * **LSP capability:** Triggered by `textDocument/didOpen` and `textDocument/didChange`. The + * client sends the full document text; the server parses it and publishes diagnostics. * * **Editor activation:** Automatic -- triggered when a `.x` file is opened or edited. * * **Adapter implementations:** * - *Mock:* Regex-scans for module/class/interface/method/property patterns and ERROR markers. - * - *TreeSitter:* Incremental native parse with error-tolerant grammar; extracts symbols via queries. + * - *TreeSitter:* Incremental native parse with error-tolerant grammar; extracts symbols via + * queries. * - *Compiler:* Full semantic compilation with type resolution and cross-file analysis. * - * **Compiler upgrade path:** A compiler adapter would produce semantic diagnostics (type errors, - * unresolved references) and a richer symbol table with resolved types and cross-file links. + * **Compiler upgrade path:** A compiler adapter would produce semantic diagnostics (type + * errors, unresolved references) and a richer symbol table with resolved types and cross-file + * links. * - * @param uri the document URI + * @param uri the document URI * @param content the source code content * @return compilation result with diagnostics and symbols */ @@ -87,13 +118,32 @@ interface Adapter : Closeable { content: String, ): CompilationResult + /** + * Analyze a document without blocking the notification thread when the backend supports it. + * Superseded work completes with cancellation, not an empty successful analysis. + */ + fun compileAsync( + uri: String, + content: String, + ): CompletableFuture = + try { + CompletableFuture.completedFuture(compile(uri, content)) + } catch (e: Exception) { + CompletableFuture.failedFuture(e) + } + + /** Documents sharing this key must be analyzed and invalidated together. */ + fun analysisScope(uri: String): String = uri + + /** Scopes to refresh after an edit/close/filesystem event, in dependency order. */ + fun affectedAnalysisScopes(uri: String): Set = setOf(analysisScope(uri)) + /** * Get the cached compilation result for a document, if available. * - * Returns the result from the most recent [compile] call for this URI, - * or `null` if the document has not been compiled yet or has been closed. - * Used by the language server to avoid redundant re-compilation for requests - * like `documentSymbol` that can use the cached result. + * Returns the result from the most recent [compile] call for this URI, or `null` if the + * document has not been compiled yet or has been closed. Used by the language server to avoid + * redundant re-compilation for requests like `documentSymbol` that can use the cached result. */ fun getCachedResult(uri: String): CompilationResult? = null @@ -112,8 +162,8 @@ interface Adapter : Closeable { * **Compiler upgrade path:** Would return symbols with resolved types, cross-file qualified * names, and documentation extracted from doc comments. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return the symbol at that position, if any */ @@ -134,15 +184,15 @@ interface Adapter : Closeable { * - *VS Code:* Hover mouse over a symbol * * **Adapter implementations:** - * - *Mock/TreeSitter:* Default in [AbstractAdapter] -- calls [findSymbolAt] and - * formats the symbol's kind, name, and type signature as Markdown. + * - *Mock/TreeSitter:* Default in [AbstractAdapter] -- calls [findSymbolAt] and formats the + * symbol's kind, name, and type signature as Markdown. * - *Compiler:* Would add resolved types, inferred generics, and extracted doc comments. * * **Compiler upgrade path:** Full type signatures (e.g., `Person implements Hashable, Const`) * and rendered XDoc documentation. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return hover text (Markdown), if available */ @@ -164,16 +214,18 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Returns XTC keywords, built-in types, and symbols from the current document. - * - *TreeSitter:* Context-aware filtering: after `.` shows members, in type position shows - * only types, after `@` shows annotations, in `import` shows qualified names. - * - *Compiler:* Type-aware completion with member access, method overloads, and import suggestions. + * - *TreeSitter:* Context-aware filtering: after `.` shows members, in type position shows only + * types, after `@` shows annotations, in `import` shows qualified names. + * - *Compiler:* Type-aware completion with member access, method overloads, and import + * suggestions. * * **Compiler upgrade path:** Full semantic type resolution for member access and overloads. * - * @param uri the document URI - * @param line 0-based line number - * @param column 0-based column number - * @param triggerCharacter the character that triggered completion (e.g., `.`, `:`, `<`), or null + * @param uri the document URI + * @param line 0-based line number + * @param column 0-based column number + * @param triggerCharacter the character that triggered completion (e.g., `.`, `:`, `<`), or + * null * @return list of completion items */ fun getCompletions( @@ -183,6 +235,26 @@ interface Adapter : Closeable { triggerCharacter: String? = null, ): List + /** Compiler backends can schedule cursor work; cancellation applies only to this query. */ + fun getCompletionsAsync( + uri: String, + line: Int, + column: Int, + triggerCharacter: String? = null, + ): CompletableFuture> = + try { + CompletableFuture.completedFuture(getCompletions(uri, line, column, triggerCharacter)) + } catch (e: Exception) { + CompletableFuture.failedFuture(e) + } + + /** Plain-text ghost suggestions, with single-line replacement ranges and no other edits. */ + fun getInlineCompletionsAsync( + uri: String, + position: Position, + context: InlineCompletionContext, + ): CompletableFuture> = CompletableFuture.completedFuture(emptyList()) + /** * Find the definition of the symbol at a position. * @@ -197,11 +269,11 @@ interface Adapter : Closeable { * - *TreeSitter:* Searches AST declarations for a matching name in the same file. * - *Compiler:* Cross-file resolution via import paths and fully-qualified names. * - * **Compiler upgrade path:** Cross-file go-to-definition, resolving imports, inherited - * members, and overloaded method signatures. + * **Compiler upgrade path:** Cross-file go-to-definition, resolving imports, inherited members, + * and overloaded method signatures. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return location of the definition, if found */ @@ -221,8 +293,8 @@ interface Adapter : Closeable { * - *VS Code:* Shift+F12, or right-click -> Find All References * * **Adapter implementations:** - * - *Mock:* Returns only the declaration itself (when `includeDeclaration` is true), no - * actual usage search. + * - *Mock:* Returns only the declaration itself (when `includeDeclaration` is true), no actual + * usage search. * - *TreeSitter:* Text-matches the identifier name across all AST nodes in the same file. * Cannot distinguish shadowed locals or cross-file references. * - *Compiler:* Semantic reference search across the entire workspace. @@ -230,9 +302,9 @@ interface Adapter : Closeable { * **Compiler upgrade path:** Cross-file references, distinguishing reads vs writes, and * filtering by scope (e.g., only references within the same module). * - * @param uri the document URI - * @param line 0-based line number - * @param column 0-based column number + * @param uri the document URI + * @param line 0-based line number + * @param column 0-based column number * @param includeDeclaration whether to include the declaration itself * @return list of reference locations */ @@ -243,6 +315,19 @@ interface Adapter : Closeable { includeDeclaration: Boolean, ): List + /** Whole-project compiler queries may require cancellable work on the compiler worker. */ + fun findReferencesAsync( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): CompletableFuture> = + try { + CompletableFuture.completedFuture(findReferences(uri, line, column, includeDeclaration)) + } catch (failure: Exception) { + CompletableFuture.failedFuture(failure) + } + // ======================================================================== // Workspace lifecycle // ======================================================================== @@ -250,8 +335,8 @@ interface Adapter : Closeable { /** * Initialize the workspace after the server has started. * - * Called once during `initialize` with the workspace folder paths. - * Implementations can use this to start background indexing of all `*.x` files. + * Called once during `initialize` with the workspace folder paths. Implementations can use this + * to start background indexing of all `*.x` files. * * @param workspaceFolders list of workspace folder paths (file system paths, not URIs) * @param progressReporter optional callback for progress: (message, percentComplete) @@ -261,11 +346,20 @@ interface Adapter : Closeable { progressReporter: ((String, Int) -> Unit)? = null, ) {} + /** Completion/cancellation ownership for hosts that report background indexing progress. */ + fun initializeWorkspaceAsync( + workspaceFolders: List, + progressReporter: ((String, Int) -> Unit)? = null, + ): CompletableFuture { + initializeWorkspace(workspaceFolders, progressReporter) + return CompletableFuture.completedFuture(Unit) + } + /** * Notification that a watched file has changed on disk. * - * Called when the client reports file creation, modification, or deletion - * via `workspace/didChangeWatchedFiles`. + * Called when the client reports file creation, modification, or deletion via + * `workspace/didChangeWatchedFiles`. * * @param uri the file URI * @param changeType 1 = Created, 2 = Changed, 3 = Deleted (LSP FileChangeType values) @@ -278,8 +372,8 @@ interface Adapter : Closeable { /** * Notification that a document has been closed by the editor. * - * Implementations should release any resources held for the document (parsed trees, - * cached compilation results, etc.) to prevent memory accumulation. + * Implementations should release any resources held for the document (parsed trees, cached + * compilation results, etc.) to prevent memory accumulation. * * @param uri the document URI */ @@ -306,8 +400,8 @@ interface Adapter : Closeable { * **Compiler upgrade path:** Distinguish read/write highlights, skip string literals and * comments, handle shadowed variables correctly. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return list of highlight locations with their kind */ @@ -329,14 +423,14 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Returns empty (requires AST structure for meaningful results). - * - *TreeSitter:* Walks up from the leaf node at the position to the root, building a chain - * of progressively larger ranges (identifier -> expression -> statement -> block -> class). + * - *TreeSitter:* Walks up from the leaf node at the position to the root, building a chain of + * progressively larger ranges (identifier -> expression -> statement -> block -> class). * - *Compiler:* Same as TreeSitter (AST-based; no semantic info needed). * - * **Compiler upgrade path:** Minimal -- tree-sitter already provides excellent selection ranges. - * A compiler adapter would use the same approach from its own AST. + * **Compiler upgrade path:** Minimal -- tree-sitter already provides excellent selection + * ranges. A compiler adapter would use the same approach from its own AST. * - * @param uri the document URI + * @param uri the document URI * @param positions list of positions to get selection ranges for * @return list of selection ranges (one per input position) */ @@ -348,11 +442,12 @@ interface Adapter : Closeable { /** * Get folding ranges for a document. * - * **LSP capability:** `textDocument/foldingRange` -- provides collapsible regions in the - * editor gutter (classes, methods, imports, comments). + * **LSP capability:** `textDocument/foldingRange` -- provides collapsible regions in the editor + * gutter (classes, methods, imports, comments). * * **Editor activation:** - * - *IntelliJ:* Click fold arrows in gutter; Ctrl+Shift+Minus (fold all) / Ctrl+Shift+Plus (unfold all) + * - *IntelliJ:* Click fold arrows in gutter; Ctrl+Shift+Minus (fold all) / Ctrl+Shift+Plus + * (unfold all) * - *VS Code:* Click fold arrows in gutter; Ctrl+Shift+[ (fold) / Ctrl+Shift+] (unfold) * * **Adapter implementations:** @@ -361,8 +456,8 @@ interface Adapter : Closeable { * More accurate than brace matching (handles string literals, comments correctly). * - *Compiler:* Same as TreeSitter (structural feature, no semantic info needed). * - * **Compiler upgrade path:** Minimal -- tree-sitter provides excellent folding ranges. - * A compiler adapter could add region markers from structured comments. + * **Compiler upgrade path:** Minimal -- tree-sitter provides excellent folding ranges. A + * compiler adapter could add region markers from structured comments. * * @param uri the document URI * @return list of folding ranges @@ -379,14 +474,14 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Regex-matches `import` statements and returns the path portion as a link. - * - *TreeSitter:* Extracts import nodes from the AST and returns their locations. - * Target is null (cannot resolve cross-file paths without compiler). + * - *TreeSitter:* Extracts import nodes from the AST and returns their locations. Target is + * null (cannot resolve cross-file paths without compiler). * - *Compiler:* Resolves import paths to actual file URIs for clickable navigation. * - * **Compiler upgrade path:** Resolve `target` URIs so clicking an import opens the - * referenced module/package file. + * **Compiler upgrade path:** Resolve `target` URIs so clicking an import opens the referenced + * module/package file. * - * @param uri the document URI + * @param uri the document URI * @param content the source code content * @return list of document links */ @@ -402,8 +497,8 @@ interface Adapter : Closeable { /** * Get signature help for a function call at a position. * - * **LSP capability:** `textDocument/signatureHelp` -- shows parameter hints when the user - * types `(` or `,` inside a function call. Highlights the active parameter. + * **LSP capability:** `textDocument/signatureHelp` -- shows parameter hints when the user types + * `(` or `,` inside a function call. Highlights the active parameter. * * **Editor activation:** * - *IntelliJ:* Type `(` after a method name, or Ctrl+P inside argument list @@ -412,15 +507,15 @@ interface Adapter : Closeable { * **Adapter implementations:** * - *Mock:* Returns null (cannot extract method parameters from regex patterns). * - *TreeSitter:* Walks up to enclosing `call_expression`, finds the called method's - * declaration in the same file, extracts parameter nodes, and counts commas to determine - * the active parameter index. + * declaration in the same file, extracts parameter nodes, and counts commas to determine the + * active parameter index. * - *Compiler:* Resolves overloaded methods, cross-file signatures, and default values. * - * **Compiler upgrade path:** Cross-file method resolution, overload disambiguation, - * default parameter values, and documentation for each parameter. + * **Compiler upgrade path:** Cross-file method resolution, overload disambiguation, default + * parameter values, and documentation for each parameter. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return signature help info, if available */ @@ -430,6 +525,18 @@ interface Adapter : Closeable { column: Int, ): SignatureHelp? + /** Asynchronous counterpart for compiler-backed signature inspection. */ + fun getSignatureHelpAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture = + try { + CompletableFuture.completedFuture(getSignatureHelp(uri, line, column)) + } catch (e: Exception) { + CompletableFuture.failedFuture(e) + } + /** * Prepare rename operation -- check if rename is valid at position. * @@ -446,8 +553,8 @@ interface Adapter : Closeable { * **Compiler upgrade path:** Reject renames of built-in types, warn about cross-file impact, * and validate the new name doesn't conflict with existing declarations. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return rename range and placeholder text, or null if rename not allowed */ @@ -471,14 +578,16 @@ interface Adapter : Closeable { * - *Mock:* Whole-word text replacement across all lines in the same file. * - *TreeSitter:* Finds all identifier AST nodes with the same text in the same file and * produces edits for each occurrence. - * - *Compiler:* Cross-file rename with semantic analysis, updating imports and references. + * - *Compiler:* Locals/private ordinary-method parameters in a complete module, including + * captures and named labels. Recompiles proposed edits and compares bindings before returning + * versioned changes. Unsupported targets or incomplete binding coverage yield no edits. * - * **Compiler upgrade path:** Cross-file rename across the workspace, updating import paths, - * and handling constructor references and type aliases. + * **Compiler upgrade path:** Cross-file rename across the workspace, updating import paths, and + * handling constructor references and type aliases. * - * @param uri the document URI - * @param line 0-based line number - * @param column 0-based column number + * @param uri the document URI + * @param line 0-based line number + * @param column 0-based column number * @param newName the new name for the symbol * @return workspace edit with all changes, or null if rename failed */ @@ -489,11 +598,27 @@ interface Adapter : Closeable { newName: String, ): WorkspaceEdit? + /** + * Cancellable compiler validation may run on the serialized worker rather than a request + * thread. + */ + fun renameAsync( + uri: String, + line: Int, + column: Int, + newName: String, + ): CompletableFuture = + try { + CompletableFuture.completedFuture(rename(uri, line, column, newName)) + } catch (failure: Exception) { + CompletableFuture.failedFuture(failure) + } + /** * Get code actions for a range (quick fixes, refactorings). * - * **LSP capability:** `textDocument/codeAction` -- provides the lightbulb menu with quick - * fixes and refactoring suggestions. Actions can include workspace edits or commands. + * **LSP capability:** `textDocument/codeAction` -- provides the lightbulb menu with quick fixes + * and refactoring suggestions. Actions can include workspace edits or commands. * * **Editor activation:** * - *IntelliJ:* Alt+Enter (Intentions), or click lightbulb icon in gutter @@ -501,16 +626,16 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Offers "Organize Imports" when import statements are detected and unsorted. - * - *TreeSitter:* Same as Mock -- detects unsorted import nodes from the AST and offers - * a single edit to sort them. + * - *TreeSitter:* Same as Mock -- detects unsorted import nodes from the AST and offers a + * single edit to sort them. * - *Compiler:* Quick fixes for diagnostics (add import, fix typo), refactorings (extract * method, inline variable). * - * **Compiler upgrade path:** Diagnostic-driven quick fixes, extract/inline refactorings, - * and "add missing override" suggestions. + * **Compiler upgrade path:** Diagnostic-driven quick fixes, extract/inline refactorings, and + * "add missing override" suggestions. * - * @param uri the document URI - * @param range the range to get actions for + * @param uri the document URI + * @param range the range to get actions for * @param diagnostics diagnostics in the range * @return list of available code actions */ @@ -520,6 +645,12 @@ interface Adapter : Closeable { diagnostics: List, ): List + fun getCodeActionsAsync( + uri: String, + range: Range, + diagnostics: List, + ): CompletableFuture> = CompletableFuture.completedFuture(getCodeActions(uri, range, diagnostics)) + /** * Get semantic tokens for enhanced syntax highlighting. * @@ -532,7 +663,8 @@ interface Adapter : Closeable { * **Adapter implementations:** * - *Mock:* Returns null (no type information available). * - *TreeSitter:* Classifies tokens from the AST using [SemanticTokenEncoder] for enhanced - * highlighting beyond what TextMate provides. Opt-in via `-Plsp.semanticTokens=true`. + * highlighting beyond what TextMate provides. Enabled by default; disable via + * `-Plsp.semanticTokens=false`. * - *Compiler:* Full semantic token classification with type-aware highlighting. * * **Compiler upgrade path:** Classify every token with its semantic role (variable, parameter, @@ -543,11 +675,21 @@ interface Adapter : Closeable { */ fun getSemanticTokens(uri: String): SemanticTokens? + /** Recognized color values, independent of source-code semantic highlighting. */ + fun getDocumentColors(uri: String): List = emptyList() + + /** Offer an edit only for a currently recognized color expression at exactly this range. */ + fun getColorPresentations( + uri: String, + range: Range, + color: ColorValue, + ): List = emptyList() + /** * Get inlay hints (inline type annotations, parameter names). * - * **LSP capability:** `textDocument/inlayHint` -- shows inline annotations in the editor - * for inferred types and parameter names (e.g., `val x` shows `: Int` after the variable). + * **LSP capability:** `textDocument/inlayHint` -- shows inline annotations in the editor for + * inferred types and parameter names (e.g., `val x` shows `: Int` after the variable). * * **Editor activation:** Automatic -- hints appear inline when enabled. * - *IntelliJ:* Settings -> Editor -> Inlay Hints (toggle per category) @@ -558,10 +700,10 @@ interface Adapter : Closeable { * - *TreeSitter:* Returns empty (requires type inference). * - *Compiler:* Provides inferred type annotations and parameter name hints. * - * **Compiler upgrade path:** Show inferred types for `val` declarations, parameter names - * at call sites, and return type hints for methods without explicit return types. + * **Compiler upgrade path:** Show inferred types for `val` declarations, parameter names at + * call sites, and return type hints for methods without explicit return types. * - * @param uri the document URI + * @param uri the document URI * @param range the range to get hints for * @return list of inlay hints */ @@ -581,14 +723,13 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Removes trailing whitespace from all lines and inserts final newline if missing. - * - *TreeSitter:* Same as Mock (basic formatting; AST-aware indentation is possible but not - * yet implemented). - * - *Compiler:* Full code formatting with XTC style rules, indentation, and alignment. + * - *TreeSitter:* Syntax-tree indentation and whitespace cleanup; base cleanup without a tree. + * - *Compiler:* Token-preserving brace/parenthesis/bracket indentation and whitespace cleanup. * * **Compiler upgrade path:** AST-aware formatting with configurable style rules (brace * placement, indentation, blank lines between declarations). * - * @param uri the document URI + * @param uri the document URI * @param content the source code content * @param options formatting options (tab size, etc.) * @return list of text edits to apply @@ -609,17 +750,17 @@ interface Adapter : Closeable { * - *VS Code:* Select text, then Ctrl+K Ctrl+F (Format Selection) * * **Adapter implementations:** - * - *Mock:* Removes trailing whitespace only on lines within the specified range. - * Does not insert final newline (that's a whole-document concern). - * - *TreeSitter:* Same as Mock (range-scoped trailing whitespace removal). - * - *Compiler:* Full formatting within the range, re-indenting and aligning. + * - *Mock:* Removes trailing whitespace only on lines within the specified range. Does not + * insert final newline (that's a whole-document concern). + * - *TreeSitter:* Syntax-tree indentation and whitespace cleanup on selected lines. + * - *Compiler:* The same token-preserving indentation and whitespace cleanup on selected lines. * - * **Compiler upgrade path:** AST-aware range formatting that adjusts indentation relative - * to the surrounding context. + * **Compiler upgrade path:** AST-aware range formatting that adjusts indentation relative to + * the surrounding context. * - * @param uri the document URI + * @param uri the document URI * @param content the source code content - * @param range the range to format + * @param range the range to format * @param options formatting options * @return list of text edits to apply */ @@ -645,8 +786,8 @@ interface Adapter : Closeable { * initialization. Supports fuzzy name matching across all indexed `.x` files. * - *Compiler:* Searches a workspace-wide symbol index with full semantic resolution. * - * **Compiler upgrade path:** Add type-aware filtering, ranking by relevance, and - * support for qualified name search. + * **Compiler upgrade path:** Add type-aware filtering, ranking by relevance, and support for + * qualified name search. * * @param query search query string * @return list of matching symbols @@ -667,9 +808,9 @@ interface Adapter : Closeable { /** * Find the declaration of the symbol at a position. * - * **LSP capability:** `textDocument/declaration` -- navigates to the declaration site - * (as opposed to the definition site). In XTC's single-file-per-type model, this is - * less important than in C/C++ where declaration and definition can be in separate files. + * **LSP capability:** `textDocument/declaration` -- navigates to the declaration site (as + * opposed to the definition site). In XTC's single-file-per-type model, this is less important + * than in C/C++ where declaration and definition can be in separate files. * * **Editor activation:** * - *IntelliJ:* Ctrl+B on a symbol (if distinct from definition) @@ -679,8 +820,8 @@ interface Adapter : Closeable { * - *Mock/TreeSitter:* Not implemented -- cannot distinguish declaration from definition. * - *Compiler:* Resolves declaration site from semantic analysis. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return location of the declaration, if found */ @@ -690,12 +831,32 @@ interface Adapter : Closeable { column: Int, ): Location? + /** Portable identity at a resolved occurrence; unavailable bindings return no identity. */ + fun findMonikers( + uri: String, + line: Int, + column: Int, + ): List = emptyList() + + fun findMonikersAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture> = CompletableFuture.completedFuture(findMonikers(uri, line, column)) + + /** All written contracts when a member overrides more than one declaration. */ + fun findDeclarations( + uri: String, + line: Int, + column: Int, + ): List = listOfNotNull(findDeclaration(uri, line, column)) + /** * Find the type definition of the symbol at a position. * - * **LSP capability:** `textDocument/typeDefinition` -- navigates to the type of an - * expression or variable. E.g., from a variable `name` of type `String`, jumps to the - * `String` class definition. + * **LSP capability:** `textDocument/typeDefinition` -- navigates to the definition of an + * expression's or variable's type. For example, from a variable `name` of type `String`, + * jumps to the `String` class definition. * * **Editor activation:** * - *IntelliJ:* Ctrl+Shift+B on a variable or expression @@ -705,8 +866,8 @@ interface Adapter : Closeable { * - *Mock/TreeSitter:* Not implemented -- requires type inference. * - *Compiler:* Resolves the type of the expression and navigates to the type declaration. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return location of the type definition, if found */ @@ -716,11 +877,20 @@ interface Adapter : Closeable { column: Int, ): Location? + /** + * Multiple type targets, for example union operands; preserves the single-target adapter hook. + */ + fun findTypeDefinitions( + uri: String, + line: Int, + column: Int, + ): List = listOfNotNull(findTypeDefinition(uri, line, column)) + /** * Find implementations of the interface or abstract method at a position. * - * **LSP capability:** `textDocument/implementation` -- finds all concrete implementations - * of an interface, abstract class, or abstract method. + * **LSP capability:** `textDocument/implementation` -- finds all concrete implementations of an + * interface, abstract class, or abstract method. * * **Editor activation:** * - *IntelliJ:* Ctrl+Alt+B on an interface/abstract method @@ -730,8 +900,8 @@ interface Adapter : Closeable { * - *Mock/TreeSitter:* Not implemented -- requires type hierarchy and semantic analysis. * - *Compiler:* Walks the type hierarchy index to find all implementors. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return list of implementation locations */ @@ -754,12 +924,12 @@ interface Adapter : Closeable { * * **Adapter implementations:** * - *Mock:* Not implemented. - * - *TreeSitter:* Could extract the type declaration and its extends/implements clauses - * from the AST (Phase 1 -- tree-sitter can parse these syntactically). + * - *TreeSitter:* Could extract the type declaration and its extends/implements clauses from + * the AST (Phase 1 -- tree-sitter can parse these syntactically). * - *Compiler:* Full type resolution with generics and conditional mixins. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return list of type hierarchy items at the position */ @@ -772,8 +942,8 @@ interface Adapter : Closeable { /** * Get supertypes for a type hierarchy item. * - * **LSP capability:** `typeHierarchy/supertypes` -- returns the parents of a type - * (extends, implements, incorporates). + * **LSP capability:** `typeHierarchy/supertypes` -- returns the parents of a type (extends, + * implements, incorporates). * * @param item the type hierarchy item to get supertypes for * @return list of supertype items @@ -783,8 +953,8 @@ interface Adapter : Closeable { /** * Get subtypes for a type hierarchy item. * - * **LSP capability:** `typeHierarchy/subtypes` -- returns all types that extend/implement - * the given type. + * **LSP capability:** `typeHierarchy/subtypes` -- returns all types that extend/implement the + * given type. * * @param item the type hierarchy item to get subtypes for * @return list of subtype items @@ -794,8 +964,8 @@ interface Adapter : Closeable { /** * Prepare call hierarchy for the symbol at a position. * - * **LSP capability:** `callHierarchy/prepare` -- resolves the function/method at the cursor - * and returns it as a CallHierarchyItem. The client then calls [getIncomingCalls] and + * **LSP capability:** `callHierarchy/prepare` -- resolves the function/method at the cursor and + * returns it as a CallHierarchyItem. The client then calls [getIncomingCalls] and * [getOutgoingCalls] to navigate the call graph. * * **Editor activation:** @@ -807,8 +977,8 @@ interface Adapter : Closeable { * - *TreeSitter:* Could syntactically identify the method at cursor (Phase 2). * - *Compiler:* Full semantic resolution with overload disambiguation. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return list of call hierarchy items at the position */ @@ -821,8 +991,8 @@ interface Adapter : Closeable { /** * Get incoming calls for a call hierarchy item (who calls this function). * - * **LSP capability:** `callHierarchy/incomingCalls` -- returns all call sites that invoke - * the given function/method. + * **LSP capability:** `callHierarchy/incomingCalls` -- returns all call sites that invoke the + * given function/method. * * @param item the call hierarchy item to find callers for * @return list of incoming calls with caller info and call-site ranges @@ -832,8 +1002,8 @@ interface Adapter : Closeable { /** * Get outgoing calls for a call hierarchy item (what does this function call). * - * **LSP capability:** `callHierarchy/outgoingCalls` -- returns all functions/methods that - * the given function calls. + * **LSP capability:** `callHierarchy/outgoingCalls` -- returns all functions/methods that the + * given function calls. * * @param item the call hierarchy item to find callees for * @return list of outgoing calls with callee info and call-site ranges @@ -843,8 +1013,8 @@ interface Adapter : Closeable { /** * Get code lenses for a document. * - * **LSP capability:** `textDocument/codeLens` -- provides actionable inline annotations - * above declarations: reference counts ("3 references"), "Run Test", "Debug", "Implement". + * **LSP capability:** `textDocument/codeLens` -- provides actionable inline annotations above + * declarations: reference counts ("3 references"), "Run Test", "Debug", "Implement". * * **Editor activation:** Automatic -- annotations appear above methods, classes, etc. * @@ -858,6 +1028,12 @@ interface Adapter : Closeable { */ fun getCodeLenses(uri: String): List + /** Reference lenses may require a cancellable configured-graph query. */ + fun getCodeLensesAsync( + uri: String, + references: Boolean, + ): CompletableFuture> = CompletableFuture.completedFuture(getCodeLenses(uri)) + /** * Resolve a code lens (fill in the command/action lazily). * @@ -872,20 +1048,20 @@ interface Adapter : Closeable { /** * Format on type -- auto-format after typing a trigger character. * - * **LSP capability:** `textDocument/onTypeFormatting` -- auto-indent when pressing Enter, - * `}`, or `;`. Tree-sitter provides enough AST context to determine correct indentation. + * **LSP capability:** `textDocument/onTypeFormatting` -- auto-indent when pressing Enter, `}`, + * or `;`. Tree-sitter provides enough AST context to determine correct indentation. * * **Editor activation:** Automatic -- triggered after typing the trigger character. * * **Adapter implementations:** * - *Mock:* Not implemented. - * - *TreeSitter:* Could determine indentation level from AST context. - * - *Compiler:* Full context-aware formatting. + * - *TreeSitter:* Syntax-tree indentation on supported trigger characters. + * - *Compiler:* Current-line token-preserving indentation and whitespace cleanup. * - * @param uri the document URI - * @param line 0-based line number where the character was typed - * @param column 0-based column number - * @param ch the character that was typed (trigger character) + * @param uri the document URI + * @param line 0-based line number where the character was typed + * @param column 0-based column number + * @param ch the character that was typed (trigger character) * @param options formatting options * @return list of text edits to apply */ @@ -900,19 +1076,19 @@ interface Adapter : Closeable { /** * Get linked editing ranges for the symbol at a position. * - * **LSP capability:** `textDocument/linkedEditingRange` -- when renaming an identifier, - * all related occurrences update simultaneously in real-time (before committing the rename). + * **LSP capability:** `textDocument/linkedEditingRange` -- when renaming an identifier, all + * related occurrences update simultaneously in real-time (before committing the rename). * - * **Editor activation:** Automatic -- start editing an identifier and linked ranges - * update in real-time. + * **Editor activation:** Automatic -- start editing an identifier and linked ranges update in + * real-time. * * **Adapter implementations:** * - *Mock:* Not implemented. * - *TreeSitter:* Could identify the declaration and its same-file usages. * - *Compiler:* Semantic linked editing with scope awareness. * - * @param uri the document URI - * @param line 0-based line number + * @param uri the document URI + * @param line 0-based line number * @param column 0-based column number * @return linked editing ranges, if available */ diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCapability.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCapability.kt new file mode 100644 index 00000000000..207ef6664d1 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCapability.kt @@ -0,0 +1,33 @@ +package org.xvm.lsp.adapter + +/** Optional protocol features supported by an adapter, independent of document synchronization. */ +enum class AdapterCapability { + HOVER, + COMPLETION, + INLINE_COMPLETION, + DEFINITION, + DECLARATION, + REFERENCES, + DOCUMENT_SYMBOL, + DOCUMENT_HIGHLIGHT, + SELECTION_RANGE, + FOLDING_RANGE, + RENAME, + CODE_ACTION, + FORMATTING, + RANGE_FORMATTING, + ON_TYPE_FORMATTING, + DOCUMENT_LINK, + DOCUMENT_COLOR, + SIGNATURE_HELP, + SEMANTIC_TOKENS, + WORKSPACE_SYMBOL, + CODE_LENS, + LINKED_EDITING, + TYPE_HIERARCHY, + TYPE_DEFINITION, + IMPLEMENTATION, + MONIKER, + CALL_HIERARCHY, + INLAY_HINT, +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCodeActions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCodeActions.kt index 47865b77680..e8de4072f85 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCodeActions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterCodeActions.kt @@ -2,29 +2,25 @@ package org.xvm.lsp.adapter import org.slf4j.Logger import org.slf4j.LoggerFactory -import org.xvm.lsp.adapter.CodeAction import org.xvm.lsp.adapter.CodeAction.CodeActionKind -import org.xvm.lsp.adapter.Position -import org.xvm.lsp.adapter.Range -import org.xvm.lsp.adapter.TextEdit -import org.xvm.lsp.adapter.WorkspaceEdit import org.xvm.lsp.index.WorkspaceIndex import org.xvm.lsp.model.Location import org.xvm.lsp.model.SymbolInfo import org.xvm.lsp.model.SymbolInfo.SymbolKind /** - * Backend-agnostic code action provider that works with any [AdapterTree]/[AdapterNode] implementation. + * Backend-agnostic code action provider that works with any [AdapterTree]/[AdapterNode] + * implementation. * - * Currently offers: + * Currently, offers: * - Organize imports (sort alphabetically) * - Remove unused imports (names not referenced elsewhere in the file) * - Generate documentation comment (insert skeleton with @param entries) * - Auto-import (add import for unresolved type names found in workspace index) * - * Query-engine-specific data (imports, declarations, identifier lookups) is passed - * via [CodeActionQueryData] so that both tree-sitter and compiler adapters can - * provide the same information from their own backends. + * Query-engine-specific data (imports, declarations, identifier lookups) is passed via + * [CodeActionQueryData] so that both tree-sitter and compiler adapters can provide the same + * information from their own backends. * * Stateless — a single instance is shared across all code action requests. */ @@ -53,9 +49,7 @@ class AdapterCodeActions { ) } - /** - * Build all available code actions for the given document. - */ + /** Build all available code actions for the given document. */ fun getCodeActions( tree: AdapterTree, uri: String, @@ -71,7 +65,10 @@ class AdapterCodeActions { } ?: logger.info("getCodeActions: organize-imports not applicable") val removeUnused = buildRemoveUnusedImportActions(uri, queryData) - logger.info("getCodeActions: remove-unused-import candidates={}", removeUnused.map { it.title }) + logger.info( + "getCodeActions: remove-unused-import candidates={}", + removeUnused.map { it.title }, + ) addAll(removeUnused) val docComments = buildGenerateDocCommentActions(tree, uri, range, queryData.declarations) @@ -91,7 +88,10 @@ class AdapterCodeActions { ): CodeAction? { val importNodes = tree.root.children.filter { it.type == "import_statement" } if (importNodes.size < 2) { - logger.info("getCodeActions: organize-imports skipped, import count={}", importNodes.size) + logger.info( + "getCodeActions: organize-imports skipped, import count={}", + importNodes.size, + ) return null } @@ -109,7 +109,10 @@ class AdapterCodeActions { Position(firstImport.startLine, firstImport.startColumn), Position(lastImport.endLine, lastImport.endColumn), ) - val edit = WorkspaceEdit(mapOf(uri to listOf(TextEdit(replaceRange, sortedTexts.joinToString("\n"))))) + val edit = + WorkspaceEdit( + mapOf(uri to listOf(TextEdit(replaceRange, sortedTexts.joinToString("\n")))), + ) return CodeAction("Organize Imports", CodeActionKind.SOURCE_ORGANIZE_IMPORTS, edit = edit) } @@ -135,7 +138,11 @@ class AdapterCodeActions { val deleteRange = Range(Position(loc.startLine, 0), Position(loc.startLine + 1, 0)) val edit = WorkspaceEdit(mapOf(uri to listOf(TextEdit(deleteRange, "")))) - logger.info("getCodeActions: import '{}' marked unused at line {}", simpleName, loc.startLine) + logger.info( + "getCodeActions: import '{}' marked unused at line {}", + simpleName, + loc.startLine, + ) CodeAction("Remove unused import '$simpleName'", CodeActionKind.SOURCE, edit = edit) } } @@ -151,7 +158,10 @@ class AdapterCodeActions { declarations: List, ): List { val lines = tree.source.split("\n") - val filtered = declarations.filter { it.location.startLine in range.start.line..range.end.line } + val filtered = + declarations.filter { + it.location.startLine in range.start.line..range.end.line + } logger.info( "getCodeActions: doc-comment scan range={}..{} declarationsInRange={}", range.start.line, @@ -164,7 +174,11 @@ class AdapterCodeActions { // Check if there is already a doc comment above val prevLine = if (declLine > 0) lines[declLine - 1].trimEnd() else "" if (prevLine.endsWith("*/")) { - logger.info("getCodeActions: doc-comment skipped for '{}' at line {}, already documented", decl.name, declLine) + logger.info( + "getCodeActions: doc-comment skipped for '{}' at line {}, already documented", + decl.name, + declLine, + ) return@mapNotNull null } @@ -200,7 +214,9 @@ class AdapterCodeActions { val methodNode = node?.let { generateSequence(it) { n -> n.parent } - .firstOrNull { n -> n.type == "method_declaration" || n.type == "constructor_declaration" } + .firstOrNull { n -> + n.type == "method_declaration" || n.type == "constructor_declaration" + } } val paramsNode = methodNode?.childByFieldName("parameters") paramsNode @@ -253,7 +269,10 @@ class AdapterCodeActions { indexed.map { it.qualifiedName }, ) if (indexed.isEmpty()) { - logger.info("getCodeActions: no indexed candidates available for unresolved type '{}'", name) + logger.info( + "getCodeActions: no indexed candidates available for unresolved type '{}'", + name, + ) } indexed.map { symbol -> val importText = "import ${symbol.qualifiedName};" @@ -291,8 +310,8 @@ class AdapterCodeActions { } /** - * Find the line where a new import statement should be inserted. - * Inserts after the last existing import, or at line 0 if there are none. + * Find the line where a new import statement should be inserted. Inserts after the last + * existing import, or at line 0 if there are none. */ private fun findImportInsertLine(tree: AdapterTree): Int { val importNodes = tree.root.children.filter { it.type == "import_statement" } @@ -305,16 +324,16 @@ class AdapterCodeActions { } /** - * Pre-computed query data passed to [AdapterCodeActions] to decouple it from - * any specific query engine implementation. + * Pre-computed query data passed to [AdapterCodeActions] to decouple it from any specific query + * engine implementation. * - * The adapter (tree-sitter, compiler, etc.) populates this from its own backend - * before calling code action methods. + * The adapter (tree-sitter, compiler, etc.) populates this from its own backend before calling code + * action methods. * - * @property imports All import paths in the document (e.g., "ecstasy.collections.List") - * @property importLocations Import path → source location pairs for each import statement - * @property declarations All declarations found in the document - * @property findIdentifiers Callback to find all occurrences of a given identifier name + * @property imports All import paths in the document (e.g., "ecstasy.collections.List") + * @property importLocations Import path → source location pairs for each import statement + * @property declarations All declarations found in the document + * @property findIdentifiers Callback to find all occurrences of a given identifier name */ data class CodeActionQueryData( val imports: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt index 5a45d3df17b..6d33da1f476 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt @@ -2,9 +2,6 @@ package org.xvm.lsp.adapter import org.slf4j.Logger import org.slf4j.LoggerFactory -import org.xvm.lsp.adapter.Position -import org.xvm.lsp.adapter.Range -import org.xvm.lsp.adapter.TextEdit /** * Backend-agnostic formatter that works with any [AdapterTree]/[AdapterNode] implementation. @@ -102,23 +99,29 @@ class AdapterFormatter { options: FormattingOptions, ): List = buildFormattingEdits(tree, content, config, options, startLine = 0, endLine = null) - /** - * Format a range of lines within the document. - */ + /** Format a range of lines within the document. */ fun formatRange( tree: AdapterTree, content: String, range: Range, config: FormattingConfig, options: FormattingOptions, - ): List = buildFormattingEdits(tree, content, config, options, startLine = range.start.line, endLine = range.end.line) + ): List = + buildFormattingEdits( + tree, + content, + config, + options, + startLine = range.start.line, + endLine = range.end.line, + ) /** * Build formatting edits for a line range (or the whole document when [endLine] is null). * - * For each line, computes the correct indentation from the AST and emits an edit if - * the actual indentation differs. Also strips trailing whitespace and inserts a final - * newline for whole-document formatting. + * For each line, computes the correct indentation from the AST and emits an edit if the actual + * indentation differs. Also strips trailing whitespace and inserts a final newline for + * whole-document formatting. */ private fun buildFormattingEdits( tree: AdapterTree, @@ -147,7 +150,9 @@ class AdapterFormatter { // Trailing whitespace removal val trimmed = line.trimEnd() - if (trimmed.length < line.length && (options.trimTrailingWhitespace || isFullDocument)) { + if ( + trimmed.length < line.length && (options.trimTrailingWhitespace || isFullDocument) + ) { add( TextEdit( range = @@ -163,7 +168,12 @@ class AdapterFormatter { // Insert final newline if missing. XTC default is true; user can override // via editor settings (insertFinalNewline = false). - if (isFullDocument && options.insertFinalNewline && content.isNotEmpty() && !content.endsWith("\n")) { + if ( + isFullDocument && + options.insertFinalNewline && + content.isNotEmpty() && + !content.endsWith("\n") + ) { val lastIdx = lines.size - 1 val lastCol = lines[lastIdx].length add( @@ -184,12 +194,12 @@ class AdapterFormatter { /** * Compute the correct indentation for a single line based on its AST context. * - * Uses structural depth (counting indent-parent ancestors in the AST) rather than - * reading indentation from the source. This is critical for correctly formatting - * files that are already misindented. + * Uses structural depth (counting indent-parent ancestors in the AST) rather than reading + * indentation from the source. This is critical for correctly formatting files that are already + * misindented. * - * Returns [SKIP_INDENT] for lines inside string literals (must not be modified). - * Returns 0 for blank lines. + * Returns [SKIP_INDENT] for lines inside string literals (must not be modified). Returns 0 for + * blank lines. */ private fun computeLineIndent( tree: AdapterTree, @@ -211,8 +221,7 @@ class AdapterFormatter { // 3. Doc/block comment interior lines val commentAncestor = - generateSequence(node) { it.parent } - .firstOrNull { it.type in commentTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in commentTypes } if (commentAncestor != null && commentAncestor.startLine != lineIndex) { // Interior or closing line of a comment, not the opening line. // Align " *" one space to the right of the comment's structural indent. @@ -223,8 +232,7 @@ class AdapterFormatter { if (trimmed.startsWith("}")) { val enclosingBlock = generateSequence(node) { it.parent } - .firstOrNull { it.type in blockTypes || it.type in classBodyTypes } - ?: return 0 + .firstOrNull { it.type in blockTypes || it.type in classBodyTypes } ?: return 0 val ownerNode = enclosingBlock.parent ?: return 0 return countIndentDepth(ownerNode) * config.indentSize } @@ -232,10 +240,13 @@ class AdapterFormatter { // 5. Closing paren -> match the opening paren's construct if (trimmed.startsWith(")")) { val enclosing = - generateSequence(node) { it.parent }.firstOrNull { ancestor -> - val firstChild = ancestor.children.firstOrNull() - firstChild != null && firstChild.type == "(" && ancestor.startLine < lineIndex - } + generateSequence(node) { it.parent } + .firstOrNull { ancestor -> + val firstChild = ancestor.children.firstOrNull() + firstChild != null && + firstChild.type == "(" && + ancestor.startLine < lineIndex + } if (enclosing != null) { return countIndentDepth(enclosing) * config.indentSize } @@ -243,19 +254,23 @@ class AdapterFormatter { // 5b. Interior of multi-line paren construct -> continuation indent from owner val parenAncestor = - generateSequence(node) { it.parent }.firstOrNull { ancestor -> - val firstChild = ancestor.children.firstOrNull() - firstChild != null && firstChild.type == "(" && ancestor.startLine < lineIndex - } + generateSequence(node) { it.parent } + .firstOrNull { ancestor -> + val firstChild = ancestor.children.firstOrNull() + firstChild != null && firstChild.type == "(" && ancestor.startLine < lineIndex + } if (parenAncestor != null) { return countIndentDepth(parenAncestor) * config.indentSize + config.indentSize } // 6. Case labels -> same indent as switch - if (trimmed.startsWith("case ") || trimmed.startsWith("default:") || trimmed.startsWith("default ")) { + if ( + trimmed.startsWith("case ") || + trimmed.startsWith("default:") || + trimmed.startsWith("default ") + ) { val switchNode = - generateSequence(node) { it.parent } - .firstOrNull { it.type in switchTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in switchTypes } if (switchNode != null) { return countIndentDepth(switchNode) * config.indentSize } @@ -264,10 +279,10 @@ class AdapterFormatter { // 7. Continuation lines (extends, implements, incorporates, delegates) if (isContinuationLine(trimmed)) { val declNode = - generateSequence(node) { it.parent } - .firstOrNull { it.type in declarationTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in declarationTypes } if (declNode != null) { - return countIndentDepth(declNode) * config.indentSize + config.continuationIndentSize + return countIndentDepth(declNode) * config.indentSize + + config.continuationIndentSize } } @@ -276,14 +291,15 @@ class AdapterFormatter { } /** - * Count the number of indent-parent ancestors to determine structural nesting depth. - * Includes the node itself if it is an indent parent type. + * Count the number of indent-parent ancestors to determine structural nesting depth. Includes + * the node itself if it is an indent parent type. */ - internal fun countIndentDepth(node: AdapterNode): Int { + private fun countIndentDepth(node: AdapterNode): Int { var depth = 0 - generateSequence(node) { it.parent }.forEach { n -> - if (n.type in indentParentTypes) depth++ - } + generateSequence(node) { it.parent } + .forEach { n -> + if (n.type in indentParentTypes) depth++ + } return depth } @@ -291,9 +307,7 @@ class AdapterFormatter { // On-type formatting (auto-indent) // ======================================================================== - /** - * Handle a character typed by the user and return indentation edits. - */ + /** Handle a character typed by the user and return indentation edits. */ fun onTypeFormatting( tree: AdapterTree, line: Int, @@ -303,15 +317,14 @@ class AdapterFormatter { ): List = when (ch) { "\n" -> handleEnter(tree, line, config) - "}" -> handleCloseBrace(tree, line, column, config) - ")" -> handleCloseParen(tree, line, column, config) + "}", ")" -> handleCloseDelimiter(tree, line, column, ch.single()) ";" -> emptyList() else -> emptyList() } /** - * Handle Enter key: determine the correct indentation for the new line based on - * what the previous line ends with and the AST context. + * Handle Enter key: determine the correct indentation for the new line based on what the + * previous line ends with and the AST context. */ private fun handleEnter( tree: AdapterTree, @@ -337,7 +350,8 @@ class AdapterFormatter { // Doc/block comment continuation: insert " * " prefix on Enter inside comments. // Try multiple column positions since tree.nodeAt(line, 0) may return a node // outside the comment when there's leading whitespace. - val commentEdit = handleCommentContinuation(tree, prevLineIndex, line, lines, prevTrimmed, prevIndent) + val commentEdit = + handleCommentContinuation(tree, prevLineIndex, line, lines, prevTrimmed, prevIndent) if (commentEdit != null) return commentEdit // IntelliJ may compact an auto-inserted empty block onto the previous line as "{}" @@ -346,8 +360,13 @@ class AdapterFormatter { if (prevTrimmed.endsWith("{}")) { val bodyIndent = when { - isContinuationLine(prevTrimmed) -> findDeclarationIndent(tree, prevLineIndex) + config.indentSize - else -> prevIndent + config.indentSize + isContinuationLine(prevTrimmed) -> { + findDeclarationIndent(tree, prevLineIndex) + config.indentSize + } + + else -> { + prevIndent + config.indentSize + } } val closingIndent = when { @@ -384,8 +403,13 @@ class AdapterFormatter { val currentIndent = currentLine.takeWhile { it == ' ' }.length val bodyIndent = when { - isContinuationLine(prevTrimmed) -> findDeclarationIndent(tree, prevLineIndex) + config.indentSize - else -> prevIndent + config.indentSize + isContinuationLine(prevTrimmed) -> { + findDeclarationIndent(tree, prevLineIndex) + config.indentSize + } + + else -> { + prevIndent + config.indentSize + } } val closingIndent = when { @@ -416,14 +440,17 @@ class AdapterFormatter { val (reason, desiredIndent) = when { // Continuation keyword ending with '{' -> body indent from declaration start. - // Must be checked BEFORE the generic endsWith("{") to avoid matching as plain brace. + // Must be checked BEFORE the generic endsWith("{") to avoid matching as plain + // brace. isContinuationLine(prevTrimmed) && prevTrimmed.endsWith("{") -> { - "continuation-with-brace" to (findDeclarationIndent(tree, prevLineIndex) + config.indentSize) + "continuation-with-brace" to + (findDeclarationIndent(tree, prevLineIndex) + config.indentSize) } // Continuation keyword (extends, implements, etc.) NOT ending with '{' isContinuationLine(prevTrimmed) && !prevTrimmed.endsWith("{") -> { - "continuation" to (findDeclarationIndent(tree, prevLineIndex) + config.continuationIndentSize) + "continuation" to + (findDeclarationIndent(tree, prevLineIndex) + config.continuationIndentSize) } // Previous line ends with '{' -> indent one level deeper @@ -448,7 +475,8 @@ class AdapterFormatter { // Default: use AST context else -> { - "ast-context" to computeDesiredIndent(tree, prevLineIndex, prevIndent, config.indentSize) + "ast-context" to + computeDesiredIndent(tree, prevLineIndex, prevIndent, config.indentSize) } } @@ -475,55 +503,63 @@ class AdapterFormatter { } /** - * Handle closing brace: outdent the current line to match the line where the - * corresponding opening '{' lives. + * Outdent a closing brace or parenthesis to match its opening construct's line. + * Braces align with the block owner; parentheses align with the opening parenthesis. */ - private fun handleCloseBrace( + private fun handleCloseDelimiter( tree: AdapterTree, line: Int, column: Int, - config: FormattingConfig, + closing: Char, ): List { val source = tree.source val lines = source.split("\n") - if (line < 0 || line >= lines.size) return emptyList() + val currentLine = lines.getOrNull(line) ?: return emptyList() - val currentIndent = lines[line].takeWhile { it == ' ' }.length + val currentIndent = currentLine.takeWhile { it == ' ' }.length - // Find the '}' character position on the line and look up the AST node there. - // LSP sends the cursor position *after* the typed character, so try both the - // reported column and the actual '}' position on the line. - val braceCol = lines[line].indexOf('}') + // LSP reports the cursor after the typed character. Prefer the delimiter's actual + // position, falling back to the reported column if that lookup finds no node. + val delimiterCol = currentLine.indexOf(closing) val node = - tree.nodeAt(line, if (braceCol >= 0) braceCol else column) + tree.nodeAt(line, if (delimiterCol >= 0) delimiterCol else column) ?: tree.nodeAt(line, column) ?: return emptyList() - // Walk up to find the block or class_body that this '}' closes - val enclosingBlock = - generateSequence(node) { it.parent } - .firstOrNull { it.type in blockTypes || it.type in classBodyTypes } - ?: return emptyList() - - // Find the reference line: the construct that owns the block - val ownerNode = enclosingBlock.parent val refLine = - when (ownerNode?.type) { - in declarationTypes, - "method_declaration", - "function_declaration", - "constructor_declaration", - -> ownerNode!!.startLine - - in controlFlowTypes -> ownerNode!!.startLine - - else -> enclosingBlock.startLine + if (closing == '}') { + val enclosingBlock = + generateSequence(node) { it.parent } + .firstOrNull { it.type in blockTypes || it.type in classBodyTypes } + ?: return emptyList() + val ownerNode = enclosingBlock.parent + when (ownerNode?.type) { + in declarationTypes, + "method_declaration", + "function_declaration", + "constructor_declaration", + -> ownerNode!!.startLine + + in controlFlowTypes -> ownerNode!!.startLine + + else -> enclosingBlock.startLine + } + } else { + // Parameter lists, argument lists and conditions start with an opening + // parenthesis. Only align to an ancestor that starts on an earlier line. + val enclosing = + generateSequence(node) { it.parent } + .firstOrNull { ancestor -> + val firstChild = ancestor.children.firstOrNull() + firstChild != null && firstChild.type == "(" && ancestor.startLine < line + } ?: return emptyList() + enclosing.startLine } val desiredIndent = getLineIndent(source, refLine) - logger.info( - "onTypeFormatting[close-brace]: line={} column={} refLine={} desiredIndent={} currentIndent={}", + "onTypeFormatting[{}]: line={} column={} refLine={} desiredIndent={} currentIndent={}", + if (closing == '}') "close-brace" else "close-paren", line, column, refLine, @@ -536,58 +572,11 @@ class AdapterFormatter { return listOf(makeIndentEdit(line, currentIndent, desiredIndent)) } - /** - * Handle closing parenthesis: outdent to match the line where the opening '(' lives. - * This handles multi-line parameter lists, argument lists, and condition expressions. - */ - private fun handleCloseParen( - tree: AdapterTree, - line: Int, - column: Int, - @Suppress("UNUSED_PARAMETER") config: FormattingConfig, - ): List { - val source = tree.source - val lines = source.split("\n") - if (line < 0 || line >= lines.size) return emptyList() - - val currentIndent = lines[line].takeWhile { it == ' ' }.length - - // Find the ')' on this line and look up the AST node. - val parenCol = lines[line].indexOf(')') - val node = - tree.nodeAt(line, if (parenCol >= 0) parenCol else column) - ?: tree.nodeAt(line, column) - ?: return emptyList() - - // Walk up to find a node whose opening '(' is on a different line. - // Parenthesized constructs in tree-sitter: argument_list, parameter_list, - // parenthesized_expression, condition, etc. We look for any ancestor that - // starts with '(' (its first child is '(') and spans multiple lines. - val enclosing = - generateSequence(node) { it.parent }.firstOrNull { ancestor -> - val firstChild = ancestor.children.firstOrNull() - firstChild != null && firstChild.type == "(" && ancestor.startLine < line - } ?: return emptyList() - - val desiredIndent = getLineIndent(source, enclosing.startLine) - logger.info( - "onTypeFormatting[close-paren]: line={} column={} refLine={} desiredIndent={} currentIndent={}", - line, - column, - enclosing.startLine, - desiredIndent, - currentIndent, - ) - if (desiredIndent == currentIndent) return emptyList() - - return listOf(makeIndentEdit(line, currentIndent, desiredIndent)) - } - /** * Handle Enter inside a doc comment or block comment. * - * Returns a list of edits that insert " * " continuation prefix on the new line, - * aligned with the "*" on the opening line. Returns null if not inside a comment. + * Returns a list of edits that insert " * " continuation prefix on the new line, aligned with + * the "*" on the opening line. Returns null if not inside a comment. */ private fun handleCommentContinuation( tree: AdapterTree, @@ -678,13 +667,11 @@ class AdapterFormatter { lineIndex: Int, ): Boolean { val node = tree.nodeAt(lineIndex, 0) ?: return false - return generateSequence(node) { it.parent } - .any { it.type == "case_clause" } + return generateSequence(node) { it.parent }.any { it.type == "case_clause" } } - internal fun isInsideStringLiteral(node: AdapterNode): Boolean = - generateSequence(node) { it.parent } - .any { it.type in stringLiteralTypes } + private fun isInsideStringLiteral(node: AdapterNode): Boolean = + generateSequence(node) { it.parent }.any { it.type in stringLiteralTypes } private fun findDeclarationIndent( tree: AdapterTree, @@ -692,8 +679,7 @@ class AdapterFormatter { ): Int { val node = tree.nodeAt(lineIndex, 0) ?: return 0 val decl = - generateSequence(node) { it.parent } - .firstOrNull { it.type in declarationTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in declarationTypes } ?: return 0 return getLineIndent(tree.source, decl.startLine) } @@ -711,8 +697,7 @@ class AdapterFormatter { val node = tree.nodeAt(prevLineIndex, lastNonSpace) ?: return prevIndent val ancestor = - generateSequence(node) { it.parent } - .firstOrNull { it.type in indentParentTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in indentParentTypes } return if (ancestor != null) { val ownerLine = ancestor.parent?.startLine ?: ancestor.startLine @@ -745,8 +730,5 @@ class AdapterFormatter { newText = " ".repeat(desiredIndent), ) - private fun summarizeLine(text: String): String = - text - .replace("\t", "\\t") - .take(80) + private fun summarizeLine(text: String): String = text.replace("\t", "\\t").take(80) } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFutures.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFutures.kt new file mode 100644 index 00000000000..ec500a0b248 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFutures.kt @@ -0,0 +1,31 @@ +package org.xvm.lsp.adapter + +import java.util.concurrent.CompletableFuture + +/** Keep ownership of query cancellation when converting an asynchronous adapter result. */ +internal fun CompletableFuture.mapCancellable(transform: (T) -> R): CompletableFuture = + thenApply(transform).also { mapped -> + mapped.whenComplete { _, _ -> if (mapped.isCancelled) cancel(false) } + } + +/** Cancellation follows both stages, including cancellation while the second stage is starting. */ +internal fun CompletableFuture.composeCancellable(transform: (T) -> CompletableFuture): CompletableFuture { + val result = CompletableFuture() + result.whenComplete { _, _ -> if (result.isCancelled) cancel(false) } + whenComplete { value, failure -> + if (failure != null) { + result.completeExceptionally(failure) + } else if (!result.isDone) { + try { + val next = transform(value) + result.whenComplete { _, _ -> if (result.isCancelled) next.cancel(false) } + next.whenComplete { answer, error -> + if (error == null) result.complete(answer) else result.completeExceptionally(error) + } + } catch (error: Throwable) { + result.completeExceptionally(error) + } + } + } + return result +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt index 8b6bd0be764..a567b7021be 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt @@ -9,38 +9,57 @@ import org.xvm.lsp.model.SymbolInfo // from the internal compiler types. // ============================================================================ -/** - * A position in a text document (0-based line and column). - */ +/** Exact matching library source; its virtual URI includes the artifact/source revision. */ +data class ReadOnlyDocument( + val uri: String, + val text: String, +) + +/** Portable artifact identity. The scheme owns the identifier format, independently of snapshots. */ +data class SymbolMoniker( + val scheme: String, + val identifier: String, + val kind: Kind, +) { + enum class Kind { IMPORT, EXPORT, LOCAL } +} + +/** A position in a text document (0-based line and column). */ data class Position( val line: Int, val column: Int, ) -/** - * A range in a text document. - */ +/** A range in a text document. */ data class Range( val start: Position, val end: Position, ) -/** - * A text edit to apply to a document. - */ +/** A text edit to apply to a document. */ data class TextEdit( val range: Range, val newText: String, ) -/** - * Completion item for code completion. - */ +/** Inline suggestions may extend a selected popup item, but cannot make additional edits. */ +data class InlineCompletionContext( + val automatic: Boolean, + val selectedCompletion: TextEdit? = null, +) + +/** Completion item for code completion. */ data class CompletionItem( val label: String, val kind: CompletionKind, val detail: String, val insertText: String, + val textEdit: TextEdit? = null, + val documentation: String? = null, + val sortText: String? = null, + val additionalTextEdits: List = emptyList(), + /** Optional tab stops; insertText/textEdit always retain a literal, usable fallback. */ + val snippet: String? = null, ) { enum class CompletionKind { CLASS, @@ -50,12 +69,12 @@ data class CompletionItem( VARIABLE, KEYWORD, MODULE, + VALUE, + SNIPPET, } } -/** - * Document highlight for symbol highlighting. - */ +/** Document highlight for symbol highlighting. */ data class DocumentHighlight( val range: Range, val kind: HighlightKind, @@ -67,21 +86,19 @@ data class DocumentHighlight( } } -/** - * Selection range with optional parent for nested selections. - */ +/** Selection range with optional parent for nested selections. */ data class SelectionRange( val range: Range, val parent: SelectionRange? = null, ) -/** - * Folding range for code folding. - */ +/** Folding range for code folding. */ data class FoldingRange( val startLine: Int, val endLine: Int, val kind: FoldingKind? = null, + val startCharacter: Int? = null, + val endCharacter: Int? = null, ) { enum class FoldingKind { COMMENT, @@ -90,59 +107,54 @@ data class FoldingRange( } } -/** - * Document link for clickable paths. - */ +/** Document link for clickable paths. */ data class DocumentLink( val range: Range, val target: String?, val tooltip: String? = null, ) -/** - * Signature help for function calls. - */ +/** Signature help for function calls. */ data class SignatureHelp( val signatures: List, val activeSignature: Int = 0, val activeParameter: Int = 0, ) -/** - * Information about a function signature. - */ +/** Information about a function signature. */ data class SignatureInfo( val label: String, val documentation: String? = null, val parameters: List = emptyList(), + val activeParameter: Int? = null, ) -/** - * Information about a function parameter. - */ +/** Information about a function parameter. */ data class ParameterInfo( val label: String, val documentation: String? = null, ) -/** - * Result of prepare rename operation. - */ +/** Result of prepare rename operation. */ data class PrepareRenameResult( val range: Range, val placeholder: String, ) -/** - * Workspace edit containing changes to multiple documents. - */ +/** Workspace edit containing changes to multiple documents. */ data class WorkspaceEdit( val changes: Map>, -) + /** Require protocol document versions; hosts must not fall back to unversioned changes. */ + val versioned: Boolean = false, + /** Apply text edits before these file moves; destinations must not be overwritten. */ + val renames: Map = emptyMap(), +) { + init { + require(renames.isEmpty() || versioned) { "File moves require versioned document changes" } + } +} -/** - * Code action (quick fix or refactoring). - */ +/** Code action (quick fix or refactoring). */ data class CodeAction( val title: String, val kind: CodeActionKind, @@ -161,22 +173,19 @@ data class CodeAction( } } -/** - * Semantic tokens for enhanced syntax highlighting. - */ +/** Semantic tokens for enhanced syntax highlighting. */ data class SemanticTokens( val data: List, ) -/** - * Inlay hint for inline annotations. - */ +/** Inlay hint for inline annotations. */ data class InlayHint( val position: Position, val label: String, val kind: InlayHintKind, val paddingLeft: Boolean = false, val paddingRight: Boolean = false, + val tooltip: String? = null, ) { enum class InlayHintKind { TYPE, @@ -187,8 +196,8 @@ data class InlayHint( /** * Formatting options from client. * - * Defaults are XTC conventions (trim trailing whitespace, insert final newline). - * The editor can override these via LSP `FormattingOptions` properties. + * Defaults are XTC conventions (trim trailing whitespace, insert final newline). The editor can + * override these via LSP `FormattingOptions` properties. */ data class FormattingOptions( val tabSize: Int, @@ -213,6 +222,7 @@ data class TypeHierarchyItem( val range: Range, val selectionRange: Range, val detail: String? = null, + val data: String? = null, ) /** @@ -227,12 +237,13 @@ data class CallHierarchyItem( val range: Range, val selectionRange: Range, val detail: String? = null, + val data: String? = null, ) /** * An incoming call to a call hierarchy item (who calls it). * - * @param from the calling function/method + * @param from the calling function/method * @param fromRanges the specific call-site ranges within the caller */ data class CallHierarchyIncomingCall( @@ -243,7 +254,7 @@ data class CallHierarchyIncomingCall( /** * An outgoing call from a call hierarchy item (what it calls). * - * @param to the called function/method + * @param to the called function/method * @param fromRanges the specific call-site ranges within the caller */ data class CallHierarchyOutgoingCall( @@ -254,7 +265,7 @@ data class CallHierarchyOutgoingCall( /** * A code lens (actionable inline annotation above a declaration). * - * @param range the range this code lens applies to + * @param range the range this code lens applies to * @param command the command to execute when clicked (null until resolved) */ data class CodeLens( @@ -265,8 +276,8 @@ data class CodeLens( /** * A command associated with a code lens. * - * @param title display text (e.g., "3 references", "Run Test") - * @param command the command identifier to execute + * @param title display text (e.g., "3 references", "Run Test") + * @param command the command identifier to execute * @param arguments optional arguments to the command */ data class CodeLensCommand( @@ -278,7 +289,7 @@ data class CodeLensCommand( /** * Linked editing ranges -- ranges that should be edited simultaneously. * - * @param ranges the ranges that are linked + * @param ranges the ranges that are linked * @param wordPattern optional regex pattern that the new text must match */ data class LinkedEditingRanges( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterTree.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterTree.kt index be16ee626e1..66e4fdc1edd 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterTree.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterTree.kt @@ -3,9 +3,9 @@ package org.xvm.lsp.adapter /** * Backend-agnostic interface for a parsed syntax tree. * - * Implementations provide access to the root node, source text, and positional - * node lookup. Both tree-sitter and compiler-based parsers can implement this - * interface, allowing formatting and code action logic to work with either backend. + * Implementations provide access to the root node, source text, and positional node lookup. Both + * tree-sitter and compiler-based parsers can implement this interface, allowing formatting and code + * action logic to work with either backend. * * @see AdapterNode for the node interface */ @@ -22,7 +22,7 @@ interface AdapterTree { /** * Find the smallest node at the given position. * - * @param line 0-based line number + * @param line 0-based line number * @param column 0-based column number * @return the node at that position, or null if none */ @@ -35,9 +35,8 @@ interface AdapterTree { /** * Backend-agnostic interface for a syntax tree node. * - * Provides the subset of node operations needed by shared formatting and - * code action logic. Implementations wrap backend-specific node types - * (tree-sitter nodes, compiler AST nodes, etc.). + * Provides the subset of node operations needed by shared formatting and code action logic. + * Implementations wrap backend-specific node types (tree-sitter nodes, compiler AST nodes, etc.). * * @see AdapterTree for the tree interface */ @@ -75,8 +74,8 @@ interface AdapterNode { /** * Get a child node by field name. * - * Field-based lookup is preferred when available — it is O(1) and - * position-independent, unlike [childByType] which scans linearly. + * Field-based lookup is preferred when available — it is O(1) and position-independent, unlike + * [childByType] which scans linearly. */ fun childByFieldName(fieldName: String): AdapterNode? diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt new file mode 100644 index 00000000000..035c9b792d6 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt @@ -0,0 +1,24 @@ +package org.xvm.lsp.adapter + +/** sRGB channels and unpremultiplied alpha, normalized to the LSP interval [0, 1]. */ +data class ColorValue( + val red: Double, + val green: Double, + val blue: Double, + val alpha: Double, +) { + init { + require(listOf(red, green, blue, alpha).all { it.isFinite() && it in 0.0..1.0 }) + } +} + +data class DocumentColor( + val range: Range, + val color: ColorValue, +) + +/** One source spelling offered by a picker; application remains owned by the editor. */ +data class ColorPresentation( + val label: String, + val textEdit: TextEdit, +) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/FormattingConfig.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/FormattingConfig.kt index 15f8d4bb0cd..a9509b77098 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/FormattingConfig.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/FormattingConfig.kt @@ -1,13 +1,14 @@ package org.xvm.lsp.adapter /** - * Formatting configuration for XTC source files. + * Formatting configuration for Ecstasy source files. * * Resolution order (highest priority first): - * 1. `xtc-format.toml` in the project tree (not yet implemented) - * 2. Editor config from `workspace/configuration` (IntelliJ Code Style settings) - * 3. LSP `FormattingOptions` from the editor (tabSize / insertSpaces) - * 4. XTC defaults (4-space indent, 8-space continuation, no tabs) + * 1. Editor config from `workspace/configuration` (IntelliJ Code Style settings) + * 2. LSP `FormattingOptions` from the editor (tabSize / insertSpaces) + * 3. Ecstasy defaults (4-space indent, 8-space continuation, no tabs) + * + * File-scoped configuration and project-local `xtc-format.toml` lookup are not implemented. */ data class FormattingConfig( val indentSize: Int = 4, @@ -17,30 +18,26 @@ data class FormattingConfig( ) { companion object { /** XTC's opinionated defaults, matching lib_ecstasy conventions. */ - val DEFAULT = FormattingConfig() + val DEFAULT: FormattingConfig = FormattingConfig() /** - * Resolve the effective formatting config for a file. + * Resolve the effective formatting config from settings supplied by the caller. * - * Priority: xtc-format.toml (future) > editor config > LSP options > defaults. + * Priority: editor config > LSP options > defaults. This function performs no file lookup; + * any file-scoped settings must be resolved by the caller before applying this precedence. * - * @param fileUri the file being formatted (for future config file discovery) * @param lspOptions the LSP FormattingOptions from the current request * @param editorConfig editor-provided config from `workspace/configuration`, or null */ fun resolve( - fileUri: String, lspOptions: FormattingOptions, editorConfig: FormattingConfig? = null, ): FormattingConfig { - // TODO Phase 4: check for xtc-format.toml before other sources. if (editorConfig != null) return editorConfig return fromLspOptions(lspOptions) } - /** - * Create from LSP FormattingOptions (editor fallback). - */ + /** Create from LSP FormattingOptions (editor fallback). */ fun fromLspOptions(options: FormattingOptions): FormattingConfig = DEFAULT.copy( indentSize = if (options.insertSpaces) options.tabSize else DEFAULT.indentSize, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/LanguageConstants.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/LanguageConstants.kt index 07968931a40..43aa1b2a703 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/LanguageConstants.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/LanguageConstants.kt @@ -1,6 +1,5 @@ package org.xvm.lsp.adapter -import org.xvm.lsp.adapter.CompletionItem import org.xvm.lsp.adapter.CompletionItem.CompletionKind import org.xvm.lsp.model.SymbolInfo import org.xvm.lsp.model.SymbolInfo.SymbolKind @@ -29,10 +28,10 @@ object LanguageConstants { /** * Ecstasy language keywords for code completion. * - * Sourced from the Ecstasy language specification. - * See: javatools/src/main/java/org/xvm/compiler/Token.java + * Sourced from the Ecstasy language specification. See: + * javatools/src/main/java/org/xvm/compiler/Token.java */ - val KEYWORDS: List = + private val KEYWORDS: List = listOf( // Module structure "module", @@ -99,7 +98,7 @@ object LanguageConstants { * * Sourced from the Ecstasy standard library (lib_ecstasy). */ - val builtInTypes: List = + private val builtInTypes: List = listOf( // Integer types "Int", @@ -177,7 +176,7 @@ object LanguageConstants { * * Used to convert internal symbol representations to LSP protocol values. */ - val SYMBOL_TO_COMPLETION_KIND: Map = + private val SYMBOL_TO_COMPLETION_KIND: Map = mapOf( SymbolKind.MODULE to CompletionKind.MODULE, SymbolKind.PACKAGE to CompletionKind.MODULE, @@ -195,8 +194,8 @@ object LanguageConstants { ) /** - * Convert a symbol kind to a completion kind using the mapping. - * Falls back to VARIABLE for unmapped kinds. + * Convert a symbol kind to a completion kind using the mapping. Falls back to VARIABLE for + * unmapped kinds. */ fun toCompletionKind(kind: SymbolKind): CompletionKind = SYMBOL_TO_COMPLETION_KIND[kind] ?: CompletionKind.VARIABLE @@ -232,8 +231,19 @@ object LanguageConstants { fun declarationContextBuiltInTypeCompletions(): List = builtInTypes - .filterNot { it in setOf("Class", "Module", "Package", "Service", "Const", "Type", "Property", "Method") } - .map { type -> + .filterNot { + it in + setOf( + "Class", + "Module", + "Package", + "Service", + "Const", + "Type", + "Property", + "Method", + ) + }.map { type -> CompletionItem( label = type, kind = CompletionKind.CLASS, @@ -259,8 +269,8 @@ object LanguageConstants { /** * Format a symbol as Markdown hover text. * - * Produces a code block with the symbol's type signature (or kind + name), - * followed by documentation if available. + * Produces a code block with the symbol's type signature (or kind + name), followed by + * documentation if available. * * @return Markdown-formatted hover text */ diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/mock/MockAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/mock/MockAdapter.kt index 781775ac21c..014aef20786 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/mock/MockAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/mock/MockAdapter.kt @@ -1,7 +1,6 @@ package org.xvm.lsp.adapter.mock import org.xvm.lsp.adapter.AbstractAdapter -import org.xvm.lsp.adapter.Adapter import org.xvm.lsp.adapter.CodeAction import org.xvm.lsp.adapter.CodeAction.CodeActionKind import org.xvm.lsp.adapter.CompletionItem @@ -28,13 +27,16 @@ import java.util.concurrent.ConcurrentHashMap private val modulePattern = Regex("""^\s*module\s+([\w.]+)\s*\{?""", RegexOption.MULTILINE) /** Pattern to recognize class declarations. */ -private val classPattern = Regex("""^\s*(?:public\s+|private\s+|protected\s+)?class\s+(\w+)""", RegexOption.MULTILINE) +private val classPattern = + Regex("""^\s*(?:public\s+|private\s+|protected\s+)?class\s+(\w+)""", RegexOption.MULTILINE) /** Pattern to recognize interface declarations. */ -private val interfacePattern = Regex("""^\s*(?:public\s+|private\s+|protected\s+)?interface\s+(\w+)""", RegexOption.MULTILINE) +private val interfacePattern = + Regex("""^\s*(?:public\s+|private\s+|protected\s+)?interface\s+(\w+)""", RegexOption.MULTILINE) /** Pattern to recognize service declarations. */ -private val servicePattern = Regex("""^\s*(?:public\s+|private\s+|protected\s+)?service\s+(\w+)""", RegexOption.MULTILINE) +private val servicePattern = + Regex("""^\s*(?:public\s+|private\s+|protected\s+)?service\s+(\w+)""", RegexOption.MULTILINE) /** Pattern to recognize method declarations. */ private val methodPattern = @@ -62,8 +64,8 @@ private val identifierPattern = Regex("""\b(\w+)\b""") /** * Mock implementation of the XTC compiler adapter for testing. * - * This simulates what a real adapter would do by parsing XTC-like syntax - * and producing reasonable results without actually invoking the compiler. + * This simulates what a real adapter would do by parsing XTC-like syntax and producing reasonable + * results without actually invoking the compiler. * * This adapter implements all syntax-level LSP features using regex patterns: * - Document symbols and outline @@ -88,8 +90,8 @@ private val identifierPattern = Regex("""\b(\w+)\b""") * - Semantic error reporting (only ERROR markers work) * - Smart completion based on types * - * // TODO LSP: This is a MOCK - not connected to any real compiler! - * // Replace with AdapterImpl that uses the parallel compiler. + * // TODO LSP: This is a MOCK - not connected to any real compiler! // Replace with AdapterImpl + * that uses the parallel compiler. */ class MockAdapter : AbstractAdapter() { override val displayName: String = "Mock" @@ -100,9 +102,7 @@ class MockAdapter : AbstractAdapter() { private val compiledDocuments = ConcurrentHashMap() private val documentContents = ConcurrentHashMap() - /** - * Mock adapter is always healthy - no native code to verify. - */ + /** Mock adapter is always healthy - no native code to verify. */ override fun healthCheck(): Boolean { logger.info("healthCheck() -> true") return true @@ -128,7 +128,12 @@ class MockAdapter : AbstractAdapter() { // Check for basic syntax errors if (content.contains("{") && !content.contains("}")) { - add(Diagnostic.error(Location.ofLine(uri, lines.size - 1), "Unmatched opening brace")) + add( + Diagnostic.error( + Location.ofLine(uri, lines.size - 1), + "Unmatched opening brace", + ), + ) } } @@ -150,9 +155,36 @@ class MockAdapter : AbstractAdapter() { } // Parse type declarations (classes, interfaces, services) - addAll(parseTypeDeclarations(classPattern, content, uri, SymbolKind.CLASS, "class", "Class")) - addAll(parseTypeDeclarations(interfacePattern, content, uri, SymbolKind.INTERFACE, "interface", "Interface")) - addAll(parseTypeDeclarations(servicePattern, content, uri, SymbolKind.SERVICE, "service", "Service")) + addAll( + parseTypeDeclarations( + classPattern, + content, + uri, + SymbolKind.CLASS, + "class", + "Class", + ), + ) + addAll( + parseTypeDeclarations( + interfacePattern, + content, + uri, + SymbolKind.INTERFACE, + "interface", + "Interface", + ), + ) + addAll( + parseTypeDeclarations( + servicePattern, + content, + uri, + SymbolKind.SERVICE, + "service", + "Service", + ), + ) // Parse methods methodPattern.findAll(content).forEach { match -> @@ -207,10 +239,11 @@ class MockAdapter : AbstractAdapter() { val fileName = uri.substringAfterLast('/') logger.info("findSymbolAt(uri={}, line={}, column={})", fileName, line, column) val result = - compiledDocuments[uri] ?: run { - logger.info("findSymbolAt -> null (no compiled document)") - return null - } + compiledDocuments[uri] + ?: run { + logger.info("findSymbolAt -> null (no compiled document)") + return null + } val symbol = result.symbols.find { it.location.contains(line, column) } logger.info("findSymbolAt -> {}", symbol?.name ?: "null") return symbol @@ -255,7 +288,10 @@ class MockAdapter : AbstractAdapter() { // In the mock, just return the symbol's own location val location = findSymbolAt(uri, line, column)?.location - logger.info("findDefinition -> {}", location?.let { "${it.startLine}:${it.startColumn}" } ?: "null") + logger.info( + "findDefinition -> {}", + location?.let { "${it.startLine}:${it.startColumn}" } ?: "null", + ) return location } @@ -266,7 +302,13 @@ class MockAdapter : AbstractAdapter() { includeDeclaration: Boolean, ): List { val fileName = uri.substringAfterLast('/') - logger.info("findReferences(uri={}, line={}, column={}, includeDecl={})", fileName, line, column, includeDeclaration) + logger.info( + "findReferences(uri={}, line={}, column={}, includeDecl={})", + fileName, + line, + column, + includeDeclaration, + ) // Mock implementation: just return the declaration return listOfNotNull( @@ -330,7 +372,13 @@ class MockAdapter : AbstractAdapter() { // Find import blocks val importLines = lines.indices.filter { lines[it].trimStart().startsWith("import ") } if (importLines.size >= 2) { - add(FoldingRange(importLines.first(), importLines.last(), FoldingRange.FoldingKind.IMPORTS)) + add( + FoldingRange( + importLines.first(), + importLines.last(), + FoldingRange.FoldingKind.IMPORTS, + ), + ) } }.also { logger.info("folding ranges -> {} found", it.size) @@ -371,21 +419,19 @@ class MockAdapter : AbstractAdapter() { val word = getWordAt(content, line, column) ?: return null val edits = - content - .split("\n") - .flatMapIndexed { lineIdx, lineText -> - findWholeWordOccurrences(lineText, word) - .map { idx -> - TextEdit( - range = - Range( - start = Position(lineIdx, idx), - end = Position(lineIdx, idx + word.length), - ), - newText = newName, - ) - }.toList() - } + content.split("\n").flatMapIndexed { lineIdx, lineText -> + findWholeWordOccurrences(lineText, word) + .map { idx -> + TextEdit( + range = + Range( + start = Position(lineIdx, idx), + end = Position(lineIdx, idx + word.length), + ), + newText = newName, + ) + }.toList() + } if (edits.isEmpty()) return null logger.info("rename '{}' -> '{}' ({} occurrences)", word, newName, edits.size) @@ -497,8 +543,12 @@ class MockAdapter : AbstractAdapter() { lineText.indexOf(word, prev + word.length).takeIf { it >= 0 } }.filter { idx -> val before = if (idx > 0) lineText[idx - 1] else ' ' - val after = if (idx + word.length < lineText.length) lineText[idx + word.length] else ' ' - !before.isLetterOrDigit() && before != '_' && !after.isLetterOrDigit() && after != '_' + val after = + if (idx + word.length < lineText.length) lineText[idx + word.length] else ' ' + !before.isLetterOrDigit() && + before != '_' && + !after.isLetterOrDigit() && + after != '_' } private fun countLines( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt index ad22704a699..c5534db1668 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt @@ -1,7 +1,6 @@ package org.xvm.lsp.adapter.treesitter import org.xvm.lsp.adapter.AbstractAdapter -import org.xvm.lsp.adapter.Adapter import org.xvm.lsp.adapter.AdapterCodeActions import org.xvm.lsp.adapter.AdapterFormatter import org.xvm.lsp.adapter.CodeAction @@ -48,12 +47,11 @@ import org.xvm.lsp.treesitter.XtcParser import org.xvm.lsp.treesitter.XtcQueryEngine import org.xvm.lsp.treesitter.XtcTree import java.net.URI -import java.nio.file.Files import java.nio.file.Path +import java.util.concurrent.CompletableFuture import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicBoolean import kotlin.io.path.extension -import kotlin.io.path.readText import kotlin.time.measureTimedValue /** @@ -83,15 +81,14 @@ import kotlin.time.measureTimedValue * - Smart completion based on types * - Inlay hints (type annotations) * - * // TODO LSP: This adapter provides ~80% of LSP functionality without the compiler. - * // For full semantic features, combine with a CompilerAdapter via CompositeAdapter. + * // TODO LSP: This adapter provides ~80% of LSP functionality without the compiler. // For full + * semantic features, combine with a CompilerAdapter via CompositeAdapter. */ @Suppress("LoggingSimilarMessage") class TreeSitterAdapter : AbstractAdapter() { override val displayName: String = "TreeSitter" - @Volatile - override var editorFormattingConfig: FormattingConfig? = null + @Volatile override var editorFormattingConfig: FormattingConfig? = null private val parser: XtcParser = XtcParser() private val queryEngine: XtcQueryEngine = XtcQueryEngine(parser.getLanguage()) @@ -113,8 +110,16 @@ class TreeSitterAdapter : AbstractAdapter() { // Perform health check to verify native library is working logger.info("========================================") logger.info("initializing...") - logger.info("Java version: {} ({})", System.getProperty("java.version"), System.getProperty("java.vendor")) - logger.info("Platform: {} / {}", System.getProperty("os.name"), System.getProperty("os.arch")) + logger.info( + "Java version: {} ({})", + System.getProperty("java.version"), + System.getProperty("java.vendor"), + ) + logger.info( + "Platform: {} / {}", + System.getProperty("os.name"), + System.getProperty("os.arch"), + ) logger.info("========================================") if (!healthCheck()) { @@ -128,23 +133,23 @@ class TreeSitterAdapter : AbstractAdapter() { } /** - * Perform health check by delegating to the parser's health check. - * This verifies the native tree-sitter library is loaded and functional. + * Perform health check by delegating to the parser's health check. This verifies the native + * tree-sitter library is loaded and functional. */ override fun healthCheck(): Boolean = parser.healthCheck() companion object { /** - * Minimum Java version required for tree-sitter FFM API. - * Update when jtreesitter dependency changes its requirements. - * IntelliJ 2026.1+ ships with JBR 25 which satisfies this minimum. + * Minimum Java version required for tree-sitter FFM API. Update when jtreesitter dependency + * changes its requirements. IntelliJ 2026.1+ ships with JBR 25 which satisfies this + * minimum. */ - const val MIN_JAVA_VERSION = 25 + const val MIN_JAVA_VERSION: Int = 25 /** - * Conservative `http(s)` URL matcher for document-link extraction. - * Stops at the first whitespace, quote, angle bracket, or closing paren/bracket -- - * common sentence-terminating punctuation is then trimmed in [urlTrailingTrim]. + * Conservative `http(s)` URL matcher for document-link extraction. Stops at the first + * whitespace, quote, angle bracket, or closing paren/bracket -- common sentence-terminating + * punctuation is then trimmed in [urlTrailingTrim]. */ private val urlPattern = Regex("""\bhttps?://[^\s<>"'`)\]]+""") @@ -219,43 +224,46 @@ class TreeSitterAdapter : AbstractAdapter() { workspaceFolders: List, progressReporter: ((String, Int) -> Unit)?, ) { + initializeWorkspaceAsync(workspaceFolders, progressReporter) + } + + override fun initializeWorkspaceAsync( + workspaceFolders: List, + progressReporter: ((String, Int) -> Unit)?, + ): CompletableFuture { logger.info("initializeWorkspace: {} folders: {}", workspaceFolders.size, workspaceFolders) - indexer - .scanWorkspace(workspaceFolders, progressReporter) - .thenRun { + val scan = indexer.scanWorkspace(workspaceFolders, progressReporter) + scan.whenComplete { _, failure -> + if (failure == null) { indexReady.set(true) logger.info( "workspace index ready: {} symbols in {} files", workspaceIndex.symbolCount, workspaceIndex.fileCount, ) - }.exceptionally { e -> - logger.error("workspace indexing failed: {}", e.message, e) - null + } else if (!scan.isCancelled) { + logger.error("workspace indexing failed", failure) } + } + return scan } override fun didChangeWatchedFile( uri: String, changeType: Int, ) { - if (!indexReady.get()) { - logger.info("didChangeWatchedFile: index not ready, ignoring {}", uri.substringAfterLast('/')) - return - } - runCatching { when { isFileCreatedOrChanged(changeType) -> { val path = Path.of(URI(uri)) - if (path.extension == "x" && Files.exists(path)) { - indexer.reindexFile(uri, path.readText()) + if (path.extension == "x") { + indexer.refreshFile(uri) logger.info("re-indexed watched file: {}", uri.substringAfterLast('/')) } } isFileDeleted(changeType) -> { - indexer.removeFile(uri) + indexer.refreshFile(uri) logger.info("removed deleted file from index: {}", uri.substringAfterLast('/')) } } @@ -295,9 +303,7 @@ class TreeSitterAdapter : AbstractAdapter() { logger.error("parse failed for {}: {}", uri, e.message) return CompilationResult.failure( uri, - listOf( - Diagnostic.error(Location.of(uri, 0, 0), "Parse failed: ${e.message}"), - ), + listOf(Diagnostic.error(Location.of(uri, 0, 0), "Parse failed: ${e.message}")), ) } @@ -316,17 +322,14 @@ class TreeSitterAdapter : AbstractAdapter() { val diagnostics = if (tree.hasErrors) collectSyntaxErrors(tree.root, uri) else emptyList() // Extract symbols for document outline - val (symbols, queryElapsed) = measureTimedValue { queryEngine.findAllDeclarations(tree, uri) } + val (symbols, queryElapsed) = + measureTimedValue { queryEngine.findAllDeclarations(tree, uri) } val result = CompilationResult.withDiagnostics(uri, diagnostics, symbols) compilationResults[uri] = result - // Update workspace index with fresh symbols from this file - if (indexReady.get()) { - indexer.reindexFile(uri, content) - } else { - logger.info("workspace index not ready, skipping reindex for {}", uri.substringAfterLast('/')) - } + // Preserve editor ownership even while the initial disk scan is still running. + indexer.reindexFile(uri, content) logger.info( "parsed in {}, {} errors, {} symbols (query: {})", @@ -348,26 +351,27 @@ class TreeSitterAdapter : AbstractAdapter() { ): SymbolInfo? { logger.info("findSymbolAt: uri={}, line={}, column={}", uri, line, column) val tree = - parsedTrees[uri] ?: run { - logger.info("findSymbolAt: no parsed tree for uri") - return null - } + parsedTrees[uri] + ?: run { + logger.info("findSymbolAt: no parsed tree for uri") + return null + } return queryEngine.findDeclarationAt(tree, line, column, uri).also { result -> logger.info("findSymbolAt -> {}", result?.let { "'${it.name}' (${it.kind})" } ?: "null") } } /** - * Context-aware completion: filters and augments suggestions based on the AST - * context at the cursor position and the trigger character. + * Context-aware completion: filters and augments suggestions based on the AST context at the + * cursor position and the trigger character. * - * | Context | What to show | - * |---------|-------------| - * | After `.` | Same-class members (methods, properties) from enclosing class body | - * | In type position | Only types (classes, interfaces, enums, etc.) | - * | After `@` | Known annotation names | - * | In `import` statement | Qualified names from workspace index | - * | Default | Keywords + types + locals + imports (current behavior) | + * | Context | What to show | + * |-----------------------|--------------------------------------------------------------------| + * | After `.` | Same-class members (methods, properties) from enclosing class body | + * | In type position | Only types (classes, interfaces, enums, etc.) | + * | After `@` | Known annotation names | + * | In `import` statement | Qualified names from workspace index | + * | Default | Keywords + types + locals + imports (current behavior) | */ override fun getCompletions( uri: String, @@ -375,11 +379,16 @@ class TreeSitterAdapter : AbstractAdapter() { column: Int, triggerCharacter: String?, ): List { - logger.info("getCompletions: uri={}, line={}, column={}, trigger={}", uri, line, column, triggerCharacter) + logger.info( + "getCompletions: uri={}, line={}, column={}, trigger={}", + uri, + line, + column, + triggerCharacter, + ) val tree = parsedTrees[uri] - val contextNode = - tree?.nodeAt(line, maxOf(0, column - 1)) + val contextNode = tree?.nodeAt(line, maxOf(0, column - 1)) val context = if (tree != null) { classifyCompletionContext(contextNode, triggerCharacter) @@ -400,7 +409,10 @@ class TreeSitterAdapter : AbstractAdapter() { // After '.': show members from the enclosing class body if (tree != null) { val members = collectClassMembers(tree, uri, line, column) - logger.info("getCompletions: member-context candidates={}", members.map { it.label }) + logger.info( + "getCompletions: member-context candidates={}", + members.map { it.label }, + ) addAll(members) } } @@ -411,7 +423,16 @@ class TreeSitterAdapter : AbstractAdapter() { val declarationContext = contextNode?.text?.firstOrNull()?.isLowerCase() == true && generateSequence(contextNode) { it.parent } - .any { it.type in setOf("module_body", "class_body", "package_body", "enum_body", "block") } + .any { + it.type in + setOf( + "module_body", + "class_body", + "package_body", + "enum_body", + "block", + ) + } val builtIns = if (declarationContext) { declarationContextBuiltInTypeCompletions() @@ -430,10 +451,13 @@ class TreeSitterAdapter : AbstractAdapter() { addAll(builtIns) if (tree != null) { val fileTypes = - queryEngine - .findAllDeclarations(tree, uri) - .filter { it.kind in typeSymbolKinds } - logger.info("getCompletions: type-context fileTypes={}", fileTypes.map { it.name }) + queryEngine.findAllDeclarations(tree, uri).filter { + it.kind in typeSymbolKinds + } + logger.info( + "getCompletions: type-context fileTypes={}", + fileTypes.map { it.name }, + ) fileTypes.forEach { symbol -> add( CompletionItem( @@ -447,7 +471,10 @@ class TreeSitterAdapter : AbstractAdapter() { // Add workspace index types if (indexReady.get()) { val workspaceTypes = workspaceIndexTypeCompletions() - logger.info("getCompletions: type-context workspaceTypes(sample)={}", workspaceTypes.take(10).map { it.label }) + logger.info( + "getCompletions: type-context workspaceTypes(sample)={}", + workspaceTypes.take(10).map { it.label }, + ) addAll(workspaceTypes) } } @@ -464,12 +491,18 @@ class TreeSitterAdapter : AbstractAdapter() { insertText = name, ) } - logger.info("getCompletions: annotation-context common={}", common.map { it.label }) + logger.info( + "getCompletions: annotation-context common={}", + common.map { it.label }, + ) addAll(common) // Also add annotations found in the current file if (tree != null) { val fileAnnotations = collectAnnotationNames(tree.root) - logger.info("getCompletions: annotation-context fileAnnotations={}", fileAnnotations) + logger.info( + "getCompletions: annotation-context fileAnnotations={}", + fileAnnotations, + ) fileAnnotations.forEach { name -> add( CompletionItem( @@ -522,7 +555,10 @@ class TreeSitterAdapter : AbstractAdapter() { addAll(builtIns) if (tree != null) { val declarations = queryEngine.findAllDeclarations(tree, uri) - logger.info("getCompletions: default-context declarations={}", declarations.map { "${it.kind}:${it.name}" }) + logger.info( + "getCompletions: default-context declarations={}", + declarations.map { "${it.kind}:${it.name}" }, + ) declarations.mapTo(this) { it.toCompletionItem() } val imports = queryEngine.findImports(tree) logger.info("getCompletions: default-context imports={}", imports) @@ -530,7 +566,8 @@ class TreeSitterAdapter : AbstractAdapter() { } } } - }.distinctBy { it.label }.also { logger.info("getCompletions -> {} items ({})", it.size, context) } + }.distinctBy { it.label } + .also { logger.info("getCompletions -> {} items ({})", it.size, context) } } // ======================================================================== @@ -571,13 +608,23 @@ class TreeSitterAdapter : AbstractAdapter() { return CompletionContext.IMPORT } - "module_body", "class_body", "package_body", "interface_body", - "enum_body", "mixin_body", "service_body", "const_body", "block", + "module_body", + "class_body", + "package_body", + "interface_body", + "enum_body", + "mixin_body", + "service_body", + "const_body", + "block", -> { return CompletionContext.BODY } - "type_expression", "type_name", "generic_type" -> { + "type_expression", + "type_name", + "generic_type", + -> { return CompletionContext.TYPE } @@ -604,9 +651,7 @@ class TreeSitterAdapter : AbstractAdapter() { return CompletionContext.DEFAULT } - /** - * Collect method and property names from the enclosing class body for member completion. - */ + /** Collect method and property names from the enclosing class body for member completion. */ private fun collectClassMembers( tree: XtcTree, uri: String, @@ -618,8 +663,7 @@ class TreeSitterAdapter : AbstractAdapter() { // Walk up to find the enclosing class body val classBodyTypes = setOf("class_body", "module_body", "package_body", "enum_body") val classBody = - generateSequence(node) { it.parent } - .firstOrNull { it.type in classBodyTypes } + generateSequence(node) { it.parent }.firstOrNull { it.type in classBodyTypes } ?: return emptyList() // Extract declarations from the class body @@ -628,9 +672,14 @@ class TreeSitterAdapter : AbstractAdapter() { val name = child.childByFieldName("name")?.text ?: continue val kind = when (child.type) { - "method_declaration", "function_declaration" -> CompletionKind.METHOD + "method_declaration", + "function_declaration", + -> CompletionKind.METHOD + "constructor_declaration" -> CompletionKind.METHOD + "property_declaration" -> CompletionKind.PROPERTY + else -> continue } add( @@ -662,8 +711,8 @@ class TreeSitterAdapter : AbstractAdapter() { * Completions for a cursor inside a function/control-flow body. * * Order, most-relevant first; `distinctBy { it.label }` at the end of `getCompletions` - * preserves the first occurrence so this controls which "kind" wins when a name is offered - * by multiple sources. + * preserves the first occurrence so this controls which "kind" wins when a name is offered by + * multiple sources. * * 1. In-scope locals, parameters, enclosing-scope members. * 2. File-level declarations not on the cursor's scope chain. @@ -680,7 +729,9 @@ class TreeSitterAdapter : AbstractAdapter() { ): List = buildList { if (tree != null) { - queryEngine.enumerateInScope(tree, line, column, uri).mapTo(this) { it.toCompletionItem() } + queryEngine.enumerateInScope(tree, line, column, uri).mapTo(this) { + it.toCompletionItem() + } queryEngine.findAllDeclarations(tree, uri).mapTo(this) { it.toCompletionItem() } queryEngine.findImports(tree).mapTo(this) { it.toImportCompletionItem() } } @@ -722,9 +773,9 @@ class TreeSitterAdapter : AbstractAdapter() { /** * Find definition: same-file first, then cross-file via workspace index. * - * Searches AST declarations in the current file first. If not found and the - * workspace index is ready, falls back to cross-file lookup, preferring type - * declarations (classes, interfaces, etc.) over methods/properties. + * Searches AST declarations in the current file first. If not found and the workspace index is + * ready, falls back to cross-file lookup, preferring type declarations (classes, interfaces, + * etc.) over methods/properties. */ override fun findDefinition( uri: String, @@ -741,7 +792,12 @@ class TreeSitterAdapter : AbstractAdapter() { // `Boolean whitespace;` instead of the local declaration. val scoped = queryEngine.resolveByNameInScope(tree, line, column, name, uri) if (scoped != null) { - logger.info("definition '{}' -> scope-local {}:{}", name, scoped.startLine, scoped.startColumn) + logger.info( + "definition '{}' -> scope-local {}:{}", + name, + scoped.startLine, + scoped.startColumn, + ) return scoped } @@ -752,7 +808,12 @@ class TreeSitterAdapter : AbstractAdapter() { val decl = symbols.find { it.name == name } if (decl != null) { - logger.info("definition '{}' -> same-file {}:{}", name, decl.location.startLine, decl.location.startColumn) + logger.info( + "definition '{}' -> same-file {}:{}", + name, + decl.location.startLine, + decl.location.startColumn, + ) return decl.location } @@ -798,14 +859,13 @@ class TreeSitterAdapter : AbstractAdapter() { } /** - * Finds all identifier nodes with the same text in the current file's AST. - * When [includeDeclaration] is false, drops the location matching the - * declaration site -- looked up via [XtcQueryEngine.findAllDeclarations], - * whose returned location is the identifier-token range, so it compares - * equal to the matching entry from [XtcQueryEngine.findAllIdentifiers]. + * Finds all identifier nodes with the same text in the current file's AST. When + * [includeDeclaration] is false, drops the location matching the declaration site -- looked up + * via [XtcQueryEngine.findAllDeclarations], whose returned location is the identifier-token + * range, so it compares equal to the matching entry from [XtcQueryEngine.findAllIdentifiers]. * - * TODO: Same-file text matching only -- cannot distinguish shadowed locals. - * A compiler adapter would provide scope-aware, cross-file reference search. + * TODO: Same-file text matching only -- cannot distinguish shadowed locals. A compiler adapter + * would provide scope-aware, cross-file reference search. */ override fun findReferences( uri: String, @@ -820,22 +880,31 @@ class TreeSitterAdapter : AbstractAdapter() { allRefs } else { val declLocation = - queryEngine - .findAllDeclarations(tree, uri) - .find { it.name == name } - ?.location + queryEngine.findAllDeclarations(tree, uri).find { it.name == name }?.location declLocation?.let { decl -> allRefs.filter { it != decl } } ?: allRefs } - logger.info("references '{}' -> {} found (includeDecl={})", name, result.size, includeDeclaration) + logger.info( + "references '{}' -> {} found (includeDecl={})", + name, + result.size, + includeDeclaration, + ) if (result.isNotEmpty()) { result.forEach { loc -> - logger.info(" {}:{}:{}", loc.uri.substringAfterLast('/'), loc.startLine + 1, loc.startColumn + 1) + logger.info( + " {}:{}:{}", + loc.uri.substringAfterLast('/'), + loc.startLine + 1, + loc.startColumn + 1, + ) } } return result } - /** Resolves identifier at position, logging failures. Returns (tree, identifierText) or null. */ + /** + * Resolves identifier at position, logging failures. Returns (tree, identifierText) or null. + */ private fun getIdentifierAt( uri: String, line: Int, @@ -844,18 +913,25 @@ class TreeSitterAdapter : AbstractAdapter() { ): Pair? { val tree = parsedTrees[uri] ?: return null val id = - identifierNodeAt(tree, line, column) ?: return null.also { - logger.info("{}: no identifier at {}:{}:{}", op, uri.substringAfterLast('/'), line, column) - } + identifierNodeAt(tree, line, column) + ?: return null.also { + logger.info( + "{}: no identifier at {}:{}:{}", + op, + uri.substringAfterLast('/'), + line, + column, + ) + } return tree to id.text } /** * Finds the identifier node at the given position, tolerating a caret that sits at the * exclusive end of the word. When the user selects a whole identifier and invokes - * go-to-declaration (issue #459), the editor reports the caret at selection end -- one - * past the last character -- where [XtcTree.nodeAt] returns the *following* node. - * Retry one column to the left so the lookup still lands on the identifier. + * go-to-declaration (issue #459), the editor reports the caret at selection end -- one past the + * last character -- where [XtcTree.nodeAt] returns the *following* node. Retry one column to + * the left so the lookup still lands on the identifier. */ private fun identifierNodeAt( tree: XtcTree, @@ -884,8 +960,17 @@ class TreeSitterAdapter : AbstractAdapter() { logger.info("highlight '{}' -> {} occurrences", name, locations.size) return locations.map { loc -> val node = tree.nodeAt(loc.startLine, loc.startColumn) - val kind = if (node != null && isAssignmentTarget(node)) HighlightKind.WRITE else HighlightKind.READ - val range = Range(Position(loc.startLine, loc.startColumn), Position(loc.endLine, loc.endColumn)) + val kind = + if (node != null && isAssignmentTarget(node)) { + HighlightKind.WRITE + } else { + HighlightKind.READ + } + val range = + Range( + Position(loc.startLine, loc.startColumn), + Position(loc.endLine, loc.endColumn), + ) DocumentHighlight(range, kind) } } @@ -902,7 +987,10 @@ class TreeSitterAdapter : AbstractAdapter() { return LinkedEditingRanges( ranges = locations.map { loc -> - Range(Position(loc.startLine, loc.startColumn), Position(loc.endLine, loc.endColumn)) + Range( + Position(loc.startLine, loc.startColumn), + Position(loc.endLine, loc.endColumn), + ) }, ) } @@ -910,11 +998,15 @@ class TreeSitterAdapter : AbstractAdapter() { private fun isAssignmentTarget(node: XtcNode): Boolean { val parent = node.parent ?: return false return when (parent.type) { - "assignment_statement", "assignment_expression" -> { + "assignment_statement", + "assignment_expression", + -> { parent.childByFieldName("left")?.let { isOrContains(it, node) } == true } - "variable_declaration", "parameter" -> { + "variable_declaration", + "parameter", + -> { parent.childByFieldName("name")?.let { isSameNode(it, node) } == true } @@ -933,22 +1025,31 @@ class TreeSitterAdapter : AbstractAdapter() { a: XtcNode, b: XtcNode, ): Boolean = - a.startLine == b.startLine && a.startColumn == b.startColumn && - a.endLine == b.endLine && a.endColumn == b.endColumn + a.startLine == b.startLine && + a.startColumn == b.startColumn && + a.endLine == b.endLine && + a.endColumn == b.endColumn override fun getSelectionRanges( uri: String, positions: List, ): List { - logger.info("getSelectionRanges: uri={}, positions={}", uri, positions.map { "${it.line}:${it.column}" }) + logger.info( + "getSelectionRanges: uri={}, positions={}", + uri, + positions.map { "${it.line}:${it.column}" }, + ) val tree = - parsedTrees[uri] ?: run { - logger.info("getSelectionRanges: no parsed tree for uri") - return emptyList() + parsedTrees[uri] + ?: run { + logger.info("getSelectionRanges: no parsed tree for uri") + return emptyList() + } + return positions + .map { pos -> buildSelectionRange(tree, pos.line, pos.column) } + .also { + logger.info("getSelectionRanges -> {} ranges", it.size) } - return positions.map { pos -> buildSelectionRange(tree, pos.line, pos.column) }.also { - logger.info("getSelectionRanges -> {} ranges", it.size) - } } private fun buildSelectionRange( @@ -963,22 +1064,25 @@ class TreeSitterAdapter : AbstractAdapter() { val nodes = buildList { add(node) - generateSequence(node.parent) { it.parent }.forEach { ancestor -> - val prev = last() - if (ancestor.startLine != prev.startLine || - ancestor.startColumn != prev.startColumn || - ancestor.endLine != prev.endLine || - ancestor.endColumn != prev.endColumn - ) { - add(ancestor) + generateSequence(node.parent) { it.parent } + .forEach { ancestor -> + val prev = last() + if ( + ancestor.startLine != prev.startLine || + ancestor.startColumn != prev.startColumn || + ancestor.endLine != prev.endLine || + ancestor.endColumn != prev.endColumn + ) { + add(ancestor) + } } - } } // Build the SelectionRange chain from outermost (parent) to innermost (leaf) return nodes.foldRight(null) { n, parent -> SelectionRange( - range = Range(Position(n.startLine, n.startColumn), Position(n.endLine, n.endColumn)), + range = + Range(Position(n.startLine, n.startColumn), Position(n.endLine, n.endColumn)), parent = parent, ) } ?: fallback @@ -987,10 +1091,11 @@ class TreeSitterAdapter : AbstractAdapter() { override fun getFoldingRanges(uri: String): List { logger.info("getFoldingRanges: uri={}", uri.substringAfterLast('/')) val tree = - parsedTrees[uri] ?: run { - logger.info("getFoldingRanges: no parsed tree for uri") - return emptyList() - } + parsedTrees[uri] + ?: run { + logger.info("getFoldingRanges: no parsed tree for uri") + return emptyList() + } return buildList { collectFoldingRanges(tree.root, this) mergeConsecutiveLineComments(tree.root, this) @@ -1006,17 +1111,27 @@ class TreeSitterAdapter : AbstractAdapter() { // Foldable node types: declarations and block constructs that span multiple lines val foldKind = when (node.type) { - "class_declaration", "interface_declaration", "mixin_declaration", - "service_declaration", "const_declaration", "enum_declaration", - "method_declaration", "constructor_declaration", - "module_declaration", "package_declaration", + "class_declaration", + "interface_declaration", + "mixin_declaration", + "service_declaration", + "const_declaration", + "enum_declaration", + "method_declaration", + "constructor_declaration", + "module_declaration", + "package_declaration", -> { null } // no special kind = code region - "comment", "line_comment", "block_comment", "doc_comment" -> { + "comment", + "line_comment", + "block_comment", + "doc_comment", + -> { FoldingRange.FoldingKind.COMMENT } @@ -1071,16 +1186,18 @@ class TreeSitterAdapter : AbstractAdapter() { node: XtcNode, result: MutableList, ) { - if ((node.type == "comment" || node.type == "line_comment") && node.startLine == node.endLine) { + if ( + (node.type == "comment" || node.type == "line_comment") && + node.startLine == node.endLine + ) { result.add(node) } node.children.forEach { collectLineComments(it, result) } } /** - * TODO: Same-file rename only. Finds the identifier AST node at the cursor position. - * A compiler adapter would validate that the rename is semantically safe and preview - * cross-file impacts. + * TODO: Same-file rename only. Finds the identifier AST node at the cursor position. A compiler + * adapter would validate that the rename is semantically safe and preview cross-file impacts. */ override fun prepareRename( uri: String, @@ -1092,18 +1209,19 @@ class TreeSitterAdapter : AbstractAdapter() { logger.info("prepareRename '{}' at {}:{}", id.text, line, column) return PrepareRenameResult( - range = Range(Position(id.startLine, id.startColumn), Position(id.endLine, id.endColumn)), + range = + Range(Position(id.startLine, id.startColumn), Position(id.endLine, id.endColumn)), placeholder = id.text, ) } /** - * Scope-aware same-file rename. Resolves the declaration the cursor's identifier refers - * to, then renames only the occurrences that resolve to that same declaration -- so - * renaming a local variable no longer sweeps up every same-named variable and property - * in the file (issue #459). Falls back to whole-file text matching only when the name - * cannot be resolved to any in-scope declaration (e.g. an unresolved member access). - * A compiler adapter would rename across the workspace and update import paths. + * Scope-aware same-file rename. Resolves the declaration the cursor's identifier refers to, + * then renames only the occurrences that resolve to that same declaration -- so renaming a + * local variable no longer sweeps up every same-named variable and property in the file + * (issue #459). Falls back to whole-file text matching only when the name cannot be resolved to + * any in-scope declaration (e.g. an unresolved member access). A compiler adapter would rename + * across the workspace and update import paths. */ override fun rename( uri: String, @@ -1130,7 +1248,10 @@ class TreeSitterAdapter : AbstractAdapter() { val locations = if (declaration == null) { - logger.info("rename '{}': no in-scope declaration, falling back to text match", name) + logger.info( + "rename '{}': no in-scope declaration, falling back to text match", + name, + ) allOccurrences } else { allOccurrences.filter { loc -> @@ -1147,25 +1268,41 @@ class TreeSitterAdapter : AbstractAdapter() { } else -> { - queryEngine.resolveByNameInScope(tree, loc.startLine, loc.startColumn, name, uri) == declaration + queryEngine.resolveByNameInScope( + tree, + loc.startLine, + loc.startColumn, + name, + uri, + ) == declaration } } } } - logger.info("rename '{}' -> '{}' ({} of {} occurrences)", name, newName, locations.size, allOccurrences.size) + logger.info( + "rename '{}' -> '{}' ({} of {} occurrences)", + name, + newName, + locations.size, + allOccurrences.size, + ) val edits = locations.map { loc -> - val range = Range(Position(loc.startLine, loc.startColumn), Position(loc.endLine, loc.endColumn)) + val range = + Range( + Position(loc.startLine, loc.startColumn), + Position(loc.endLine, loc.endColumn), + ) TextEdit(range, newName) } return WorkspaceEdit(mapOf(uri to edits)) } /** - * True when [id] is the name identifier of a declaration node (variable, parameter, - * property, method, class, ...) rather than a reference to one -- detected by the parent - * declaring a `name` field that spans exactly this node. + * True when [id] is the name identifier of a declaration node (variable, parameter, property, + * method, class, ...) rather than a reference to one -- detected by the parent declaring a + * `name` field that spans exactly this node. */ private fun isDeclarationName(id: XtcNode): Boolean { val nameNode = id.parent?.childByFieldName("name") ?: return false @@ -1199,8 +1336,10 @@ class TreeSitterAdapter : AbstractAdapter() { val callTypes = setOf("call_expression", "generic_type") val callNode = generateSequence(node) { it.parent } - .firstOrNull { it.type in callTypes && (it.childByFieldName("arguments") ?: it.childByType("arguments")) != null } - ?: return null + .firstOrNull { + it.type in callTypes && + (it.childByFieldName("arguments") ?: it.childByType("arguments")) != null + } ?: return null // Extract function name using the 'function' field for call_expression, // falling back to type_name for generic_type nodes. @@ -1211,18 +1350,14 @@ class TreeSitterAdapter : AbstractAdapter() { "member_expression" -> funcNode.childByFieldName("member")?.text else -> null } - } - ?: callNode - .childByType("type_name") - ?.childByType("identifier") - ?.text - ?: return null + } ?: callNode.childByType("type_name")?.childByType("identifier")?.text ?: return null // Count commas before the cursor to determine active parameter val argsNode = callNode.childByFieldName("arguments") ?: callNode.childByType("arguments") val activeParam = argsNode?.children?.count { child -> - child.type == "," && (child.endLine < line || (child.endLine == line && child.endColumn <= column)) + child.type == "," && + (child.endLine < line || (child.endLine == line && child.endColumn <= column)) } ?: 0 // Find method declarations with matching name in same file @@ -1236,13 +1371,11 @@ class TreeSitterAdapter : AbstractAdapter() { methods.map { method -> // Find the method_declaration node to extract parameters val methodNode = - tree - .nodeAt(method.location.startLine, method.location.startColumn) - ?.let { findDeclarationNode(it, "method_declaration") } + tree.nodeAt(method.location.startLine, method.location.startColumn)?.let { + findDeclarationNode(it, "method_declaration") + } val params = - methodNode - ?.childByFieldName("parameters") - ?.let { extractParameters(it) } + methodNode?.childByFieldName("parameters")?.let { extractParameters(it) } ?: emptyList() val paramLabel = params.joinToString(", ") { p -> p.label } SignatureInfo( @@ -1251,7 +1384,12 @@ class TreeSitterAdapter : AbstractAdapter() { ) } - logger.info("signatureHelp '{}' -> {} signatures, active param {}", funcName, signatures.size, activeParam) + logger.info( + "signatureHelp '{}' -> {} signatures, active param {}", + funcName, + signatures.size, + activeParam, + ) return SignatureHelp( signatures = signatures, activeParameter = activeParam, @@ -1295,7 +1433,14 @@ class TreeSitterAdapter : AbstractAdapter() { declarations = queryEngine.findAllDeclarations(tree, uri), findIdentifiers = { name -> queryEngine.findAllIdentifiers(tree, name, uri) }, ) - return codeActions.getCodeActions(tree, uri, range, queryData, workspaceIndex, indexReady.get()) + return codeActions.getCodeActions( + tree, + uri, + range, + queryData, + workspaceIndex, + indexReady.get(), + ) } // ======================================================================== @@ -1308,13 +1453,13 @@ class TreeSitterAdapter : AbstractAdapter() { val declarations = queryEngine.findAllDeclarations(tree, uri) return buildList { - for (decl in declarations) { - if (decl.kind != SymbolKind.MODULE) continue + for ((name, _, kind, location) in declarations) { + if (kind != SymbolKind.MODULE) continue val range = Range( - start = Position(decl.location.startLine, decl.location.startColumn), - end = Position(decl.location.endLine, decl.location.endColumn), + start = Position(location.startLine, location.startColumn), + end = Position(location.endLine, location.endColumn), ) // "Run" lens — modules are the entry point in XTC @@ -1323,9 +1468,9 @@ class TreeSitterAdapter : AbstractAdapter() { range = range, command = CodeLensCommand( - title = "\u25B6 Run ${decl.name}", + title = "\u25B6 Run $name", command = "xtc.runModule", - arguments = listOf(uri, decl.name), + arguments = listOf(uri, name), ), ), ) @@ -1345,11 +1490,15 @@ class TreeSitterAdapter : AbstractAdapter() { options: FormattingOptions, ): List { val tree = - parsedTrees[uri] ?: run { - logger.info("formatDocument: no parsed tree for {}, falling back to base", uri.substringAfterLast('/')) - return super.formatDocument(uri, content, options) - } - val config = FormattingConfig.resolve(uri, options, editorFormattingConfig) + parsedTrees[uri] + ?: run { + logger.info( + "formatDocument: no parsed tree for {}, falling back to base", + uri.substringAfterLast('/'), + ) + return super.formatDocument(uri, content, options) + } + val config = FormattingConfig.resolve(options, editorFormattingConfig) return formatter.formatDocument(tree, content, config, options) } @@ -1360,11 +1509,15 @@ class TreeSitterAdapter : AbstractAdapter() { options: FormattingOptions, ): List { val tree = - parsedTrees[uri] ?: run { - logger.info("formatRange: no parsed tree for {}, falling back to base", uri.substringAfterLast('/')) - return super.formatRange(uri, content, range, options) - } - val config = FormattingConfig.resolve(uri, options, editorFormattingConfig) + parsedTrees[uri] + ?: run { + logger.info( + "formatRange: no parsed tree for {}, falling back to base", + uri.substringAfterLast('/'), + ) + return super.formatRange(uri, content, range, options) + } + val config = FormattingConfig.resolve(options, editorFormattingConfig) return formatter.formatRange(tree, content, range, config, options) } @@ -1380,11 +1533,15 @@ class TreeSitterAdapter : AbstractAdapter() { options: FormattingOptions, ): List { val tree = - parsedTrees[uri] ?: run { - logger.info("onTypeFormatting: no parsed tree for {}", uri.substringAfterLast('/')) - return emptyList() - } - val config = FormattingConfig.resolve(uri, options, editorFormattingConfig) + parsedTrees[uri] + ?: run { + logger.info( + "onTypeFormatting: no parsed tree for {}", + uri.substringAfterLast('/'), + ) + return emptyList() + } + val config = FormattingConfig.resolve(options, editorFormattingConfig) logger.info( "onTypeFormatting: uri={} line={} column={} ch='{}' config={}", uri.substringAfterLast('/'), @@ -1399,11 +1556,11 @@ class TreeSitterAdapter : AbstractAdapter() { /** * Emit document links for `http(s)` URLs that appear inside comments and string literals. * - * Imports deliberately do NOT participate -- navigation between an `import` and its - * declaration is the job of `textDocument/definition` (Cmd/Ctrl-click), which is - * position-aware and knows the difference between a package component and a type - * component. Document links are reserved for free-text content where there is no - * AST-level "declaration" to jump to (URLs in `// see https://...`, etc.). + * Imports deliberately do NOT participate -- navigation between an `import` and its declaration + * is the job of `textDocument/definition` (Cmd/Ctrl-click), which is position-aware and knows + * the difference between a package component and a type component. Document links are reserved + * for free-text content where there is no AST-level "declaration" to jump to (URLs in `// see + * https://...`, etc.). */ override fun getDocumentLinks( uri: String, @@ -1424,15 +1581,19 @@ class TreeSitterAdapter : AbstractAdapter() { } } } - logger.info("getDocumentLinks: {} URL links across {} comment/string nodes", links.size, hosts.size) + logger.info( + "getDocumentLinks: {} URL links across {} comment/string nodes", + links.size, + hosts.size, + ) return links } /** - * Convert an offset+length within a host node's text into an absolute LSP [Range]. - * URLs cannot contain whitespace, so the match is always single-line, but the host - * node (e.g., a block comment) may span many lines, so we walk the prefix counting - * newlines to find the absolute (line, column) for the start. + * Convert an offset+length within a host node's text into an absolute LSP [Range]. URLs cannot + * contain whitespace, so the match is always single-line, but the host node (e.g., a block + * comment) may span many lines, so we walk the prefix counting newlines to find the absolute + * (line, column) for the start. */ private fun rangeWithinText( text: String, @@ -1459,10 +1620,11 @@ class TreeSitterAdapter : AbstractAdapter() { override fun getSemanticTokens(uri: String): SemanticTokens? { logger.info("getSemanticTokens: uri={}", uri.substringAfterLast('/')) val tree = - parsedTrees[uri] ?: run { - logger.info("getSemanticTokens: no parsed tree for uri") - return null - } + parsedTrees[uri] + ?: run { + logger.info("getSemanticTokens: no parsed tree for uri") + return null + } val data = SemanticTokenEncoder().encode(tree.root) val tokenTypeCounts = data @@ -1472,23 +1634,29 @@ class TreeSitterAdapter : AbstractAdapter() { SemanticTokenLegend.tokenTypes.getOrNull(tokenTypeIndex) }.groupingBy { it } .eachCount() - logger.info("getSemanticTokens -> {} data items ({} tokens) types={}", data.size, data.size / 5, tokenTypeCounts) + logger.info( + "getSemanticTokens -> {} data items ({} tokens) types={}", + data.size, + data.size / 5, + tokenTypeCounts, + ) return data.takeIf { it.isNotEmpty() }?.let { SemanticTokens(it) } } /** * Release resources for a closed document. * - * Closes the native tree-sitter [XtcTree] for this URI, freeing its native memory - * (backed by `Arena.global()` / FFM). Without this, parsed trees accumulate for the - * JVM lifetime since [compile] intentionally does NOT close old trees eagerly -- see the - * race condition comment in [compile] for details. This method is the primary mechanism - * for reclaiming native tree memory when the editor closes a document. + * Closes the native tree-sitter [XtcTree] for this URI, freeing its native memory (backed by + * `Arena.global()` / FFM). Without this, parsed trees accumulate for the JVM lifetime since + * [compile] intentionally does NOT close old trees eagerly -- see the race condition comment in + * [compile] for details. This method is the primary mechanism for reclaiming native tree memory + * when the editor closes a document. */ override fun closeDocument(uri: String) { logger.info("closeDocument: uri={}", uri) parsedTrees.remove(uri)?.close() compilationResults.remove(uri) + indexer.closeDocument(uri) } override fun close() { @@ -1509,9 +1677,17 @@ class TreeSitterAdapter : AbstractAdapter() { buildList { val message = when { - node.isError -> "Syntax error: unexpected '${node.text.take(20)}${if (node.text.length > 20) "..." else ""}'" - node.isMissing -> "Syntax error: missing ${node.type}" - else -> null + node.isError -> { + "Syntax error: unexpected '${node.text.take(20)}${if (node.text.length > 20) "..." else ""}'" + } + + node.isMissing -> { + "Syntax error: missing ${node.type}" + } + + else -> { + null + } } if (message != null) { @@ -1524,9 +1700,7 @@ class TreeSitterAdapter : AbstractAdapter() { } // Recursively check children - node.children - .filter { it.hasError } - .forEach { addAll(collectSyntaxErrors(it, uri)) } + node.children.filter { it.hasError }.forEach { addAll(collectSyntaxErrors(it, uri)) } } private fun findIdentifierNode(node: XtcNode): XtcNode? = diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt new file mode 100644 index 00000000000..032c81467dc --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt @@ -0,0 +1,115 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.constants.ArrayConstant +import org.xvm.asm.constants.ByteConstant +import org.xvm.asm.constants.CharConstant +import org.xvm.asm.constants.DecimalAutoConstant +import org.xvm.asm.constants.DecimalConstant +import org.xvm.asm.constants.FPNConstant +import org.xvm.asm.constants.Float128Constant +import org.xvm.asm.constants.Float64Constant +import org.xvm.asm.constants.FloatConstant +import org.xvm.asm.constants.IntConstant +import org.xvm.asm.constants.LiteralConstant +import org.xvm.asm.constants.MapConstant +import org.xvm.asm.constants.RangeConstant +import org.xvm.asm.constants.RegExConstant +import org.xvm.asm.constants.SingletonConstant +import org.xvm.asm.constants.StringConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.UInt8ArrayConstant +import java.util.HexFormat + +/** Exact detached annotation values; display strings are not identities for compound values. */ +internal fun annotationIdentity( + value: Constant, + identity: (Constant) -> ProofIdentity, + depth: Int = 0, +): ProofIdentity? { + if (depth >= 64 || value.containsUnresolved()) return null + + fun nested(argument: Constant) = annotationIdentity(argument, identity, depth + 1) + + fun scalar(text: String) = ProofIdentity.Value(value.format, text) + + fun bytes(value: ByteArray) = scalar(HexFormat.of().formatHex(value)) + return when (value) { + is TypeConstant -> { + receiverIdentity(value, identity, depth + 1) + } + + is SingletonConstant -> { + identity(value.classConstant) + } + + is ByteConstant, is CharConstant, is IntConstant, is StringConstant, is LiteralConstant -> { + scalar(value.valueString) + } + + is FloatConstant -> { + scalar(value.value.toRawBits().toString()) + } + + is Float64Constant -> { + scalar(value.value.toRawBits().toString()) + } + + is Float128Constant -> { + bytes(value.value) + } + + is FPNConstant -> { + bytes(value.value) + } + + is DecimalConstant -> { + bytes(value.value.toByteArray()) + } + + is DecimalAutoConstant -> { + bytes(value.value.toByteArray()) + } + + is UInt8ArrayConstant -> { + bytes(value.value) + } + + is RegExConstant -> { + scalar("${value.flags}:${value.value}") + } + + is ArrayConstant -> { + ProofIdentity.TypeShape( + value.format, + listOf(nested(value.type) ?: return null) + value.value.map { nested(it) ?: return null }, + ) + } + + is MapConstant -> { + ProofIdentity.TypeShape( + value.format, + listOf(nested(value.type) ?: return null) + + value.value.entries.map { (key, item) -> + ProofIdentity.TypeShape(Constant.Format.MapEntry, listOf(nested(key) ?: return null, nested(item) ?: return null)) + }, + ) + } + + is RangeConstant -> { + ProofIdentity.TypeShape( + value.format, + listOf( + nested(value.first) ?: return null, + nested(value.last) ?: return null, + scalar("${value.isFirstExcluded}:${value.isLastExcluded}"), + ), + ) + } + + // Runtime handles, deferred computations and filesystem objects need their own contracts. + else -> { + null + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCallableIdentity.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCallableIdentity.kt new file mode 100644 index 00000000000..62f19a7d25c --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCallableIdentity.kt @@ -0,0 +1,66 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.ErrorListener +import org.xvm.asm.constants.MethodConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.UnionTypeConstant +import org.xvm.lsp.util.ExecutionTrace + +/** + * Preserve alternative receivers and recursive dispatch at the call site. A method constant can + * identify one written contract shared by different receivers; it cannot prove the site's route. + * Copy the compiler's selected legs before releasing its pool, without inventing runtime targets. + */ +internal fun callableIdentity( + receiver: TypeConstant, + method: MethodConstant, + errors: ErrorListener, + identity: (Constant) -> ProofIdentity, +): ProofIdentity? { + if (errors.isAbortDesired || receiver.isFormalType || receiver.containsUnresolved()) return null + val union = receiver.resolveTypedefs().removeAccess() is UnionTypeConstant + val info = ExecutionTrace.api("TypeConstant.ensureTypeInfo(rename-callable)") { receiver.ensureTypeInfo(errors) } + // The declaration can still have a formal return (First.T). Resolve its nested identity + // against this receiver before comparing concrete signatures. + val selected = + info.getMethodById(method) ?: info.getMethodBySignature(method.signature) + ?: return if (union) ProofIdentity.Unproven() else null + val dispatch = info.dispatch(selected, errors) + if (union) { + if (!dispatch.supported) return ProofIdentity.Unproven() + return dispatch.alternatives.singleOrNull()?.proof(identity) ?: ProofIdentity.Unproven() + } + if (dispatch.cycles.isEmpty() && dispatch.alternatives.isEmpty()) return null + val owner = receiverIdentity(receiver, identity) ?: return ProofIdentity.Unproven() + return dispatch.proof(owner, identity) +} + +/** Written contracts form the rename family; branch structure independently proves equivalence. */ +internal fun CompilerDispatch.proof( + owner: ProofIdentity, + identity: (Constant) -> ProofIdentity, +): ProofIdentity { + if (!supported || methods.isEmpty()) return ProofIdentity.Unproven() + return ProofIdentity.Composed( + owner, + methods.map(identity), + delegates.map(identity), + cycles.map { + ProofIdentity.Composed( + receiverIdentity(it.receiver, identity) ?: ProofIdentity.Unproven(), + it.contracts.map(identity), + emptyList(), + ) + }, + alternatives.map { it.proof(identity) }, + ) +} + +internal fun CompilerDispatch.Alternatives.proof(identity: (Constant) -> ProofIdentity): ProofIdentity = + ProofIdentity.Alternatives( + branches.mapTo(linkedSetOf()) { branch -> + val owner = receiverIdentity(branch.receiver, identity) ?: return ProofIdentity.Unproven() + branch.dispatch.proof(owner, identity) + }, + ) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCalls.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCalls.kt new file mode 100644 index 00000000000..e9748a6d9f0 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCalls.kt @@ -0,0 +1,53 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorListener +import org.xvm.asm.ModuleRepository +import org.xvm.compiler.Source +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.tool.ModuleInfo + +/** All normal, dependency and refactoring-proof compilations cross this timed boundary. */ +internal class CompilerCalls( + private val sourceCompiler: (Source, ModuleRepository?, ErrorListener) -> EmbeddingSupport.Compilation, + private val treeCompiler: (ModuleInfo, ModuleRepository?, ErrorListener) -> EmbeddingSupport.Compilation, + private val cursorCompiler: ( + Source, + ModuleInfo?, + Long, + ModuleRepository?, + ErrorListener, + ) -> EmbeddingSupport.PartialAnalysis, +) { + fun compileSource( + source: Source, + repository: ModuleRepository?, + errors: ErrorListener, + ): EmbeddingSupport.Compilation = + ExecutionTrace.api("EmbeddingSupport.compileModule(source)", source.fileName) { + sourceCompiler(source, repository, errors) + } + + fun compileTree( + sources: ModuleInfo, + repository: ModuleRepository?, + errors: ErrorListener, + ): EmbeddingSupport.Compilation = + ExecutionTrace.api( + "EmbeddingSupport.compileModule(tree)", + sources.sourceFile.toURI().toString(), + ) { + treeCompiler(sources, repository, errors) + } + + fun analyzeCursor( + source: Source, + sources: ModuleInfo?, + cursor: Long, + repository: ModuleRepository?, + errors: ErrorListener, + ): EmbeddingSupport.PartialAnalysis = + ExecutionTrace.api("EmbeddingSupport.analyzeIncomplete", source.fileName) { + cursorCompiler(source, sources, cursor, repository, errors) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDeclarations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDeclarations.kt new file mode 100644 index 00000000000..b2fc1868002 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDeclarations.kt @@ -0,0 +1,49 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorListener +import org.xvm.asm.constants.IdentityConstant +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.util.ExecutionTrace + +/** + * Written inherited contracts for an overriding member, copied before leaving the compiler worker. + */ +internal fun compilerDeclarationTargets( + nodes: List, + errors: ErrorListener, +): Map> = + buildMap { + nodes.filterIsInstance().forEach { node -> + if (errors.isAbortDesired) return emptyMap() + val structure = node.component as? ClassStructure ?: return@forEach + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(declaration)") { + structure.formalType.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + } + if (errors.hasSeriousErrors() || errors.isAbortDesired) return emptyMap() + val chains = + info.methods.values + .filter { !it.isFunction && !it.isCtorOrValidator } + .map { method -> + method.chain.mapNotNull { it.methodStructure?.identityConstant } + } + + info.properties.values.map { property -> + property.propertyBodies.map { it.identity } + } + chains.forEach { chain -> + val inherited = chain.filter { it.namespace != structure.identityConstant }.toSet() + if (inherited.isNotEmpty()) { + // Only the overriding host owns this relation. An inherited method used by a + // later class must not acquire that later class's unrelated contracts. + chain + .filter { it.namespace == structure.identityConstant } + .forEach { member -> + put(member, get(member).orEmpty() + inherited) + } + } + } + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt new file mode 100644 index 00000000000..bbd9027ac59 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt @@ -0,0 +1,183 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorListener +import org.xvm.asm.constants.MethodBody.Implementation +import org.xvm.asm.constants.MethodConstant +import org.xvm.asm.constants.MethodInfo +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeInfo +import org.xvm.asm.constants.UnionTypeConstant +import org.xvm.lsp.util.ExecutionTrace + +/** Written contracts and receiver properties behind a compiler-composed method. Worker-only. */ +internal data class CompilerDispatch( + val methods: List, + val delegates: List = emptyList(), + val supported: Boolean = true, + val cycles: List = emptyList(), + val alternatives: List = emptyList(), +) { + /** Branches stay separate even when their contracts share the same written declaration. */ + data class Alternatives( + val branches: List, + ) + + data class Branch( + val receiver: TypeConstant, + val dispatch: CompilerDispatch, + ) + + /** A finite back edge to written contracts; it does not identify an executable body. */ + data class Cycle( + val receiver: TypeConstant, + val contracts: List, + ) +} + +/** Follow existing dispatch metadata without generating optimized or forwarding method bodies. */ +internal fun TypeInfo.dispatch( + method: MethodInfo, + errors: ErrorListener, + visited: List> = emptyList(), +): CompilerDispatch { + val key = type to method + if ( + errors.isAbortDesired || + visited.size >= 64 + ) { + return CompilerDispatch(listOf(method.identity), supported = false) + } + if (visited.any { it.first == type && it.second === method }) { + // A delegating cycle can still have a finite, written interface contract. Retain where + // the route closes instead of inventing a terminal implementation or dropping the edge. + val contracts = method.closingContracts() + return if (!contracts.isNullOrEmpty() && + visited.any { (_, route) -> route.chain.any { it.implementation == Implementation.Delegating } } + ) { + CompilerDispatch(contracts, cycles = listOf(CompilerDispatch.Cycle(type, contracts))) + } else { + CompilerDispatch(listOf(method.identity), supported = false) + } + } + val seen = visited + key + val bodies = + method.chain.map { body -> + when (body.implementation) { + Implementation.Explicit, + Implementation.Default, + Implementation.Declared, + Implementation.Abstract, + Implementation.SansCode, + -> { + val declaration = body.methodStructure + CompilerDispatch( + listOf(declaration?.identityConstant ?: body.identity), + supported = declaration?.isSynthetic == false, + ) + } + + Implementation.FromInto -> { + body.intoMethodInfo?.let { dispatch(it, errors, seen) } + ?: CompilerDispatch(listOf(body.identity), supported = false) + } + + Implementation.Capped -> { + getNarrowingMethod(method)?.let { dispatch(it, errors, seen) } + ?: CompilerDispatch(listOf(body.identity), supported = false) + } + + Implementation.Delegating -> { + val receiver = body.propertyConstant + val receiverType = receiver?.let { findProperty(it)?.type } + val delegate = + receiverType?.let { + ExecutionTrace.api("TypeConstant.ensureTypeInfo(rename-delegate)") { + val view = if (it.isSingleUnderlyingClass(false)) it.ensureAccess(Access.PRIVATE) else it + view.ensureTypeInfo(errors) + } + } + val selected = delegate?.getMethodBySignature(body.signature) + if (selected == null) { + CompilerDispatch(listOf(body.identity), supported = false) + } else { + val target = delegate.dispatch(selected, errors, seen) + target.copy(delegates = listOf(receiver) + target.delegates) + } + } + + Implementation.Union -> { + val union = type.resolveTypedefs().removeAccess() as? UnionTypeConstant + if (union == null) { + CompilerDispatch(listOf(body.identity), supported = false) + } else { + val branches = + listOf(union.underlyingType to body.unionLeft, union.underlyingType2 to body.unionRight) + .map { (receiver, selected) -> + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(rename-union-leg)") { + receiver.ensureTypeInfo(errors) + } + CompilerDispatch.Branch(receiver, info.dispatch(selected, errors, seen)) + } + CompilerDispatch( + branches.flatMap { it.dispatch.methods }.distinct(), + supported = branches.all { it.dispatch.supported }, + alternatives = listOf(CompilerDispatch.Alternatives(branches)), + ) + } + } + + Implementation.Implicit, + Implementation.Field, + Implementation.Native, + -> { + // These bodies do not independently identify a written callable contract: + // assumed dispatch, generated accessors or runtime + // implementations. + CompilerDispatch(listOf(body.identity), supported = false) + } + + else -> { + CompilerDispatch(listOf(body.identity), supported = false) + } + } + } + return CompilerDispatch( + bodies.flatMap { it.methods }.distinct(), + bodies.flatMap { it.delegates }.distinct(), + bodies.all { it.supported } && !errors.hasSeriousErrors() && !errors.isAbortDesired, + bodies.flatMap { it.cycles }.distinct(), + bodies.flatMap { it.alternatives }.distinct(), + ) +} + +/** Only inspect the finite MethodBody tree; following delegate receivers again would loop. */ +private fun MethodInfo.closingContracts(depth: Int = 0): List? { + if (depth >= 64) return null + return chain + .flatMap { body -> + when (body.implementation) { + Implementation.Delegating -> { + emptyList() + } + + Implementation.Union -> { + val left = body.unionLeft.closingContracts(depth + 1) ?: return null + val right = body.unionRight.closingContracts(depth + 1) ?: return null + left + right + } + + Implementation.Declared, Implementation.Abstract, Implementation.Default, + Implementation.Explicit, Implementation.SansCode, + -> { + listOf(body.methodStructure?.takeUnless { it.isSynthetic }?.identityConstant ?: return null) + } + + else -> { + return null + } + } + }.distinct() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt new file mode 100644 index 00000000000..ebf2c0cbc93 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt @@ -0,0 +1,231 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorListener +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodBody +import org.xvm.asm.constants.MethodBody.Implementation +import org.xvm.asm.constants.MethodInfo +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.PropertyInfo +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeInfo +import org.xvm.compiler.ast.AstNode +import org.xvm.lsp.util.ExecutionTrace + +/** + * Worker-only inspection of successful source types. TypeInfo supplies actual member chains, + * including generic substitution and inherited bodies; no matching by method name or arity. + * Delegation follows compiler-selected signatures on statically known concrete receiver types. + */ +internal fun compilerImplementationTargets( + nodes: List, + errors: ErrorListener, +): Map> { + val targets = linkedMapOf>() + val inspection = + ErrorListener.cancellable(ErrorListener.collecting(errors::log), errors::isAbortDesired) + // A mixin's into type is a constraint, not an adopting host. + for (type in compilerSourceTypes(nodes, includeMixins = false)) { + if (inspection.isAbortDesired) return emptyMap() + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(implementation)") { + type.ensureTypeInfo(inspection) + } + if (inspection.hasSeriousErrors() || inspection.isAbortDesired) return emptyMap() + if (info.isClass && !info.isAbstract && !info.isSynthetic) { + (info.classChain.keys + info.defaultChain.keys).forEach { ancestor -> + targets.getOrPut(ancestor) { linkedSetOf() }.add(type.getSingleUnderlyingClass(false)) + } + } + // Anonymous classes are synthetic containers, but their written method bodies are targets. + val methods = + info.methods.values.filter { + it.identity.isTopLevel && !it.isFunction && !it.isCtorOrValidator && !it.isAbstract + } + for (method in methods) { + val implementation = info.methodImplementation(method, inspection) ?: continue + for (inherited in method.chain) { + val declaration = inherited.methodStructure?.identityConstant ?: continue + targets.getOrPut(declaration) { linkedSetOf() }.add(implementation) + } + } + info.propertyImplementationTargets(inspection).forEach { (declaration, implementations) -> + targets.getOrPut(declaration) { linkedSetOf() }.addAll(implementations) + } + } + return if (inspection.hasSeriousErrors() || inspection.isAbortDesired) emptyMap() else targets +} + +/** + * Copy property composition without constructing a separate Ref/Var implementation type or + * generating forwarding methods. The host's nested method chains already include Ref/Var annotation + * dispatch. Some mixin accessors live only in PropertyBody, with their compiler order and field + * identity. + */ +private fun TypeInfo.propertyImplementationTargets(errors: ErrorListener): Map> { + val targets = linkedMapOf>() + properties.values + .filter { !it.isConstant && !it.isFormalType } + .forEach { property -> + if (errors.isAbortDesired) return emptyMap() + for (getter in if (property.isVar) listOf(true, false) else listOf(true)) { + val accessor = if (getter) property.getterId else property.setterId + val method = getMethodById(accessor) + val written = property.writtenAccessors(getter) + val implementation = accessorImplementation(property, getter, errors) ?: continue + val declarations = + property.propertyBodies.map { it.identity } + + written.map { it.identity } + + method?.chain.orEmpty().mapNotNull(MethodBody::getMethodStructure).map { + it.identityConstant + } + declarations.forEach { declaration -> + targets.getOrPut(declaration) { linkedSetOf() }.add(implementation) + } + } + } + return targets +} + +/** + * Implicit adoption or injected/native storage has no written field implementation to navigate to. + */ +private fun PropertyInfo.sourceField(): IdentityConstant? = + fieldIdentity?.takeIf { field -> + propertyBodies.any { + it.identity == field && + it.implementation == Implementation.Explicit && + !it.isAbstract && + !it.isSynthetic + } + } + +/** A delegate's declared concrete type bounds lookup; an interface value has no selected body. */ +private fun TypeInfo.delegateType( + property: PropertyConstant, + errors: ErrorListener, +): TypeInfo? { + if (errors.isAbortDesired) return null + val target = findProperty(property)?.type ?: return null + if (!target.isSingleUnderlyingClass(false)) return null + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(target)") { + target.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + } + return info.takeIf { + it.isClass && !it.isAbstract && !errors.hasSeriousErrors() && !errors.isAbortDesired + } +} + +/** Worker-only lookup of an existing written body; never generate an optimized call chain. */ +internal fun TypeInfo.methodImplementation( + method: MethodInfo, + errors: ErrorListener, + visited: List> = emptyList(), +): IdentityConstant? { + val key = type to method + if (errors.isAbortDesired || visited.any { it.first == type && it.second === method } || visited.size >= 64) return null + // FromInto is abstract as a composition entry, even when its constraint has a written body. + // Follow that metadata; an abstract constraint still returns no executable implementation. + val body = method.chain.firstOrNull { !it.isAbstract || it.implementation == Implementation.FromInto } ?: return null + return when (body.implementation) { + Implementation.Explicit, + Implementation.Default, + -> { + body.methodStructure?.takeUnless { it.isSynthetic || it.isNative || it.isAbstract }?.identityConstant + } + + Implementation.Delegating -> { + val delegate = delegateType(body.propertyConstant ?: return null, errors) ?: return null + val selected = delegate.getMethodBySignature(body.signature) ?: return null + delegate.methodImplementation(selected, errors, visited + key) + } + + Implementation.FromInto -> { + body.intoMethodInfo?.let { methodImplementation(it, errors, visited + key) } + } + + Implementation.Capped -> { + getNarrowingMethod(method)?.let { methodImplementation(it, errors, visited + key) } + } + + else -> { + null + } + } +} + +private fun TypeInfo.accessorImplementation( + property: PropertyInfo, + getter: Boolean, + errors: ErrorListener, + visited: Set> = emptySet(), +): IdentityConstant? { + val key = type to property.identity + if (errors.isAbortDesired || key in visited || visited.size >= 64) return null + if (property.isDelegating) { + val delegate = delegateType(property.delegate, errors) ?: return null + val selected = delegate.findProperty(property.identity) ?: return null + return delegate.accessorImplementation(selected, getter, errors, visited + key) + } + val accessor = if (getter) property.getterId else property.setterId + val method = getMethodById(accessor) + // Optimizing redirects can generate methods. Keep that operation out of source inspection. + if ( + method?.chain?.any { + it.implementation in setOf(Implementation.Delegating, Implementation.Capped, Implementation.FromInto) + } == true + ) { + return methodImplementation(method, errors) + } + if (method != null) { + val chain = + if (getter) { + property.ensureOptimizedGetChain(this, null) + } else { + property.ensureOptimizedSetChain(this, null) + } + val body = chain?.firstOrNull() ?: return null + return when (body.implementation) { + Implementation.Explicit, + Implementation.Default, + -> body.methodStructure?.takeUnless { it.isSynthetic || it.isNative || it.isAbstract }?.identityConstant + + // Annotation/native storage is not a written accessor (for example Lazy.set). + Implementation.Field -> property.takeUnless { it.isRefAnnotated }?.sourceField() + + else -> null + } + } + // An annotated accessor needs its composed method chain; a field/body guess loses dispatch. + if (property.isRefAnnotated) return null + val written = property.writtenAccessors(getter) + // Explicit accessors precede interface defaults; storage overrides a default accessor. + val selected = + written.firstOrNull { it.implementation == Implementation.Explicit } + ?: written.firstOrNull() + return selected + ?.takeUnless { it.implementation == Implementation.Default && property.hasField() } + ?.identity ?: property.sourceField() +} + +private data class WrittenAccessor( + val implementation: Implementation, + val identity: IdentityConstant, +) + +private fun PropertyInfo.writtenAccessors(getter: Boolean): List = + propertyBodies + .filter { it.implementation in setOf(Implementation.Explicit, Implementation.Default) } + .mapNotNull { body -> + val member = + when { + getter && body.hasGetter() -> body.structure?.getter + !getter && body.hasSetter() -> body.structure?.setter + else -> null + } + member + ?.takeUnless { it.isAbstract || it.isNative } + ?.let { WrittenAccessor(body.implementation, it.identityConstant) } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt new file mode 100644 index 00000000000..4f10ef1002b --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt @@ -0,0 +1,99 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Component.Format +import org.xvm.asm.Constant +import org.xvm.asm.Constants.ECSTASY_MODULE +import org.xvm.asm.Constants.X_PKG_IMPORT +import org.xvm.asm.PackageStructure +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.ImportStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement + +/** Copy explicit aliases using resolved identity and lexical import ownership; retain no syntax. */ +internal fun compilerImportAliases( + nodes: List, + source: String?, + occurrences: Map, + constants: Map, +): List { + val imports = + nodes.filterIsInstance().filter { + it.source?.fileName == source && !it.isWildcard + } + return imports + // A written "as Box" is an alias even when it currently matches the imported name. + // Conditional imports use the compiler's resolved scope and retain its warning. + .filter { it.aliasToken !== it.qualifiedNameTokens.lastOrNull() } + .mapNotNull { alias -> + val token = alias.aliasToken ?: return@mapNotNull null + val identity = alias.importedIdentity ?: return@mapNotNull null + val target = constants[identity] + + fun range( + start: Long, + end: Long, + ) = SemanticModel.Range( + SemanticModel.Position( + Source.calculateLine(start), + Source.calculateOffset(start), + ), + SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end)), + ) + val uses = + nodes + .filter { it.source?.fileName == source } + .mapNotNull { node -> + val first = + when (node) { + is NameExpression -> { + node.nameToken.takeIf { node.leftExpression == null } + } + + is NamedTypeExpression -> { + node.nameBindings.firstOrNull()?.name() + } + + else -> { + null + } + } ?: return@mapNotNull null + if (first.valueText != alias.aliasName) return@mapNotNull null + val at = range(first.startPosition, first.endPosition) + val occurrence = occurrences[SemanticModel.SourceLocation(source, at)] + if (target == null || occurrence?.symbol != target) return@mapNotNull null + val owner = + generateSequence(node.parent) { it.parent } + .filterIsInstance() + .firstNotNullOfOrNull { block -> + imports.lastOrNull { + it.parentBlock === block && + it.aliasName == alias.aliasName && + it.startPosition < first.startPosition + } + } + at.takeIf { owner === alias } + }.distinct() + SemanticModel.ImportAlias( + alias.aliasName, + range(token.startPosition, token.endPosition), + uses, + ) + } +} + +/** The language's implicit module name, independent of the configured module path. */ +internal val implicitModuleAliases: Map = mapOf(ECSTASY_MODULE to X_PKG_IMPORT) + +/** Module-level imports can be spelled in any member of the same compilation. */ +internal fun compilerModuleAliases(nodes: List): Map = + nodes + .filterIsInstance() + .mapNotNull { node -> + val structure = node.component as? PackageStructure ?: return@mapNotNull null + if (!structure.isModuleImport || structure.parent.format != Format.MODULE) return@mapNotNull null + structure.importedModule.identityConstant.name to structure.identityConstant.pathString + }.toMap() + implicitModuleAliases diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt new file mode 100644 index 00000000000..746049f8732 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt @@ -0,0 +1,174 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component.Format +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodConstant +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.LiteralExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.UnaryMinusExpression +import org.xvm.lsp.util.ExecutionTrace + +/** Worker-only candidates; compiler constants are detached before the compilation is released. */ +internal data class CompilerMemberAction( + val owner: IdentityConstant, + val contract: MethodConstant, + val insertion: SemanticModel.Position, + val declaration: String, + val implementation: Boolean, + val requiredCount: Int, + val imports: List, +) + +/** + * Inherited methods from source and immutable dependency artifacts. A proposed declaration still + * needs a complete graph proof. + */ +internal fun compilerMemberActions( + nodes: List, + errors: ErrorListener, +): List { + val literalDefaults = + nodes + .filterIsInstance() + .mapNotNull { method -> + val identity = + (method.component as? MethodStructure)?.identityConstant + ?: return@mapNotNull null + identity to + method + .childNodes() + .filterIsInstance() + .mapNotNull { parameter -> + val value = parameter.value + val token = + when (value) { + is LiteralExpression -> { + value.literal.takeIf { + it.id in + setOf( + Token.Id.LIT_STRING, + Token.Id.LIT_CHAR, + Token.Id.LIT_INT, + ) + } + } + + is UnaryMinusExpression -> { + (value.childNodes().singleOrNull() as? LiteralExpression) + ?.literal + ?.takeIf { + it.id == Token.Id.LIT_INT + } + } + + else -> { + null + } + } ?: return@mapNotNull null + parameter.name to + if (value is UnaryMinusExpression) "-$token" else token.toString() + }.toMap() + }.toMap() + return nodes.filterIsInstance().flatMap { node -> + val structure = node.component as? ClassStructure ?: return@flatMap emptyList() + if (structure.format != Format.CLASS || structure.isSynthetic || errors.isAbortDesired) { + return@flatMap emptyList() + } + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(member-actions)") { + structure.formalType.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + } + if (errors.hasSeriousErrors() || errors.isAbortDesired) return@flatMap emptyList() + val end = node.ensureBody().endPosition + val insertion = + SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end) - 1) + val methods = + info.methods.values.filter { + it.identity.isTopLevel && it.isVirtual && !it.isCtorOrValidator + } + val external = + methods + .flatMap { + (it.signature.params + it.signature.returns).flatMap { type -> + type.memberClasses() + } + }.filter { it.moduleConstant != structure.identityConstant.moduleConstant } + val lexicalErrors = ErrorList() + val names = + ExecutionTrace.api("Lexer.lex(member-imports)") { + Lexer(node.source.clone(), lexicalErrors).asSequence().map { it.valueText }.toSet() + } + if (lexicalErrors.hasSeriousErrors()) return@flatMap emptyList() + val aliases = + XdkMemberActions.moduleAliases( + external.map { it.moduleConstant.name }, + implicitModuleAliases, + names, + ) + val moduleNode = + generateSequence(node as AstNode) { it.parent } + .filterIsInstance() + .lastOrNull { it.category.id == Token.Id.MODULE && it.source === node.source } + val importAt = + moduleNode?.ensureBody()?.startPosition?.let { + SemanticModel.Position(Source.calculateLine(it), Source.calculateOffset(it) + 1) + } ?: SemanticModel.Position(0, 0) + methods + .mapNotNull { method -> + val declaration = method.getTopmostMethodStructure(info) + if ( + declaration.containingClass == structure || + declaration.isSynthetic || + declaration.isNative || + method.isOp || + method.isAuto || + !info.dispatch(method, errors).supported + ) { + return@mapNotNull null + } + val signature = + memberSignature( + method.signature, + declaration, + structure.identityConstant, + aliases, + literalDefaults[declaration.identityConstant].orEmpty(), + ) ?: return@mapNotNull null + val access = if (method.access == Access.PROTECTED) "protected " else "" + CompilerMemberAction( + structure.identityConstant, + declaration.identityConstant, + insertion, + "$access$signature", + method.isAbstract, + info.methods.values.count { it.isAbstract && !it.isCtorOrValidator }, + (method.signature.params + method.signature.returns) + .asSequence() + .flatMap { it.memberClasses() } + .filter { it.constantPool.getImplicitlyImportedIdentity(it.name) != it } + .map { it.moduleConstant.name } + .distinct() + .filter { + it !in implicitModuleAliases && + it != structure.identityConstant.moduleConstant.name + }.map { + XdkMemberActions.Import( + importAt, + "package ${aliases.getValue(it)} import $it;", + ) + }.toList(), + ) + }.distinct() + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt new file mode 100644 index 00000000000..6c32b311a14 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt @@ -0,0 +1,235 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.CharConstant +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.DifferenceTypeConstant +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.ImmutableTypeConstant +import org.xvm.asm.constants.IntConstant +import org.xvm.asm.constants.IntersectionTypeConstant +import org.xvm.asm.constants.ParameterizedTypeConstant +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.RecursiveTypeConstant +import org.xvm.asm.constants.RelationalTypeConstant +import org.xvm.asm.constants.SignatureConstant +import org.xvm.asm.constants.SingletonConstant +import org.xvm.asm.constants.StringConstant +import org.xvm.asm.constants.TerminalTypeConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.asm.constants.UnionTypeConstant + +/** + * Render compiler-selected types, never a diagnostic display string or copied source expression. + */ +internal fun memberSignature( + signature: SignatureConstant, + declaration: MethodStructure, + owner: IdentityConstant, + modules: Map, + literalDefaults: Map, +): String? { + if ( + signature.paramCount != declaration.params.size || + declaration.params.any { it.annotations.isNotEmpty() } + ) { + return null + } + val formals = + declaration.params.take(declaration.typeParamCount).associate { parameter -> + val name = parameter.name?.takeIf(XdkRename::identifier) ?: return null + parameter.asTypeParameterConstant(declaration.identityConstant) to name + } + + fun render(type: TypeConstant): String? = type.memberSourceType(owner, formals, modules) + val typeParameters = + signature.params.take(declaration.typeParamCount).zip(formals.values).map { (type, name) -> + val constraint = type.paramTypes.singleOrNull() ?: return null + if (constraint == constraint.constantPool.typeObject()) { + name + } else { + "$name extends ${render(constraint) ?: return null}" + } + } + val parameters = + signature.params.zip(declaration.params).drop(declaration.typeParamCount).map { (type, parameter) -> + val name = parameter.name?.takeIf(XdkRename::identifier) ?: return null + val rendered = render(type) ?: return null + val default = + if (parameter.hasDefaultValue()) { + // Validated primitive constants are independent of their original + // expression/scope. + // Fresh declaration repair accepts only parser-owned literal tokens; the + // complete + // proposed compilation must validate both the original and generated defaults. + val value = + when (val constant = parameter.defaultValue) { + is StringConstant -> { + constant.valueString + } + + is CharConstant -> { + constant.valueString + } + + is IntConstant -> { + constant.valueString + } + + is SingletonConstant -> { + when (constant) { + constant.constantPool.valTrue() -> "ecstasy.Boolean.True" + constant.constantPool.valFalse() -> "ecstasy.Boolean.False" + constant.constantPool.valNull() -> "ecstasy.Nullable.Null" + else -> return null + } + } + + null -> { + literalDefaults[name] ?: return null + } + + else -> { + return null + } + } + " = $value" + } else { + "" + } + "$rendered $name$default" + } + val returns = + signature.returns.drop(if (declaration.isConditionalReturn) 1 else 0).map { + render(it) ?: return null + } + val result = + when (returns.size) { + 0 -> "void" + 1 -> returns.single() + else -> returns.joinToString(", ", "(", ")") + } + val generic = if (typeParameters.isEmpty()) "" else typeParameters.joinToString(", ", "<", "> ") + val conditional = if (declaration.isConditionalReturn) "conditional " else "" + return "$generic$conditional$result ${signature.name}(${parameters.joinToString(", ")})" +} + +/** + * Supported type constructors and this method's own formals. Recursive aliases require a proven + * destination spelling and import route before code generation can support them. + */ +internal fun TypeConstant.memberSourceType( + owner: IdentityConstant, + formals: Map, + modules: Map, +): String? { + return when (this) { + // A recursive alias is a TerminalTypeConstant without a single defining constant. + is RecursiveTypeConstant -> { + null + } + + is ParameterizedTypeConstant -> { + val base = underlyingType.memberSourceType(owner, formals, modules) ?: return null + val arguments = + paramTypes.map { + it.memberSourceType(owner, formals, modules) ?: return null + } + arguments.joinToString(", ", "$base<", ">") + } + + is ImmutableTypeConstant -> { + underlyingType.memberSourceType(owner, formals, modules)?.let { "immutable $it" } + } + + is RelationalTypeConstant -> { + val operator = + when (this) { + is UnionTypeConstant -> "|" + is IntersectionTypeConstant -> "+" + is DifferenceTypeConstant -> "-" + else -> return null + } + val left = underlyingType.memberSourceType(owner, formals, modules) ?: return null + val right = underlyingType2.memberSourceType(owner, formals, modules) ?: return null + "($left $operator $right)" + } + + is TerminalTypeConstant -> { + when (val identity = definingConstant) { + is TypeParameterConstant -> { + formals[identity] + } + + is PropertyConstant -> { + // Class formals are properties, not method type parameters. Only the exact + // owning class or an enclosing class can supply this spelling; an unrelated + // owner's identically named formal must never be substituted by name. + identity.name.takeIf { it -> + identity.isFormalType && XdkRename.identifier(it) && + generateSequence(owner) { it.parentConstant }.any { it == identity.parentConstant } + } + } + + is ClassConstant -> { + when { + constantPool.getImplicitlyImportedIdentity(identity.name) == identity -> { + identity.name + } + + identity.moduleConstant == owner.moduleConstant -> { + identity.pathString + } + + XdkAutoImports.target(identity) != null -> { + modules[identity.moduleConstant.name]?.let { + "$it.${identity.pathString}" + } + } + + else -> { + null + } + } + } + + else -> { + null + } + } + } + + else -> { + null + } + } +} + +/** Inspect only type structures whose source spelling the renderer understands. */ +internal fun TypeConstant.memberClasses(): List = + when (this) { + is RecursiveTypeConstant -> { + emptyList() + } + + is ParameterizedTypeConstant -> { + underlyingType.memberClasses() + paramTypes.flatMap { it.memberClasses() } + } + + is RelationalTypeConstant -> { + underlyingType.memberClasses() + underlyingType2.memberClasses() + } + + is ImmutableTypeConstant -> { + underlyingType.memberClasses() + } + + is TerminalTypeConstant -> { + listOfNotNull(definingConstant as? ClassConstant) + } + + else -> { + emptyList() + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt new file mode 100644 index 00000000000..0841f756cdc --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt @@ -0,0 +1,60 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodConstant +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.lsp.util.ExecutionTrace + +/** Worker-only dispatch facts. These compiler identities must never enter a published snapshot. */ +internal class CompilerMethodRelations( + val declarations: Set, + val chains: List, +) { + data class Chain( + val owner: IdentityConstant, + val methods: List, + val supported: Boolean, + val cycles: List = emptyList(), + val alternatives: List = emptyList(), + ) +} + +/** Inspect compiler-composed chains, including abstract contracts and generic substitutions. */ +internal fun compilerMethodRelations( + nodes: List, + errors: ErrorListener, +): CompilerMethodRelations { + val declarations = + nodes + .filterIsInstance() + .mapNotNull { it.component as? MethodStructure } + .filter { !it.isFunction && !it.isConstructor && it.identityConstant.isTopLevel } + .mapTo(linkedSetOf()) { it.identityConstant } + val chains = + compilerSourceTypes(nodes) + .flatMap { type -> + if (errors.isAbortDesired) return@flatMap emptyList() + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(method-relations)") { + type.ensureTypeInfo(errors) + } + info.methods.values + .filter { it.identity.isTopLevel && !it.isFunction && !it.isCtorOrValidator } + .map { method -> + val route = info.dispatch(method, errors) + CompilerMethodRelations.Chain( + type.getSingleUnderlyingClass(true), + // Rename families use written contracts behind redirects, just like call + // provenance. Generated cap/into identities are not editable declarations. + route.methods, + route.supported, + route.cycles, + route.alternatives, + ) + } + }.distinct() + return CompilerMethodRelations(declarations, chains) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt new file mode 100644 index 00000000000..462dcee58d8 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt @@ -0,0 +1,591 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component.Format +import org.xvm.asm.ConstantPool +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.Op +import org.xvm.asm.Register +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotatedTypeExpression +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.ExpressionStatement +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.LabeledExpression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.LiteralExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.compiler.ast.VariableTypeExpression +import org.xvm.lsp.adapter.xdk.XdkMissingMethods.Dispatch +import org.xvm.lsp.util.ExecutionTrace + +/** Attempt-owned types are detached by the shared compiler identity collector. */ +internal data class CompilerMissingMethod( + val candidate: XdkMissingMethods.Candidate, + val parameters: List = emptyList(), + val returns: List = emptyList(), + val formals: List = emptyList(), + val conditional: Boolean = false, +) { + /** Fresh declaration types and already detached body types never share compiler pools. */ + sealed interface Type { + data class Resolved( + val value: TypeConstant, + ) : Type + + data class Detached( + val value: ProofIdentity, + ) : Type + } +} + +/** Register types may leave the failed attempt only through the shared identity collector. */ +internal data class CompilerMissingInputs( + val localTypes: Map = emptyMap(), + val receivers: Map = emptyMap(), + val expressions: Map = emptyMap(), +) { + data class LocalType( + val source: String?, + val type: TypeConstant, + val destinationSources: Map = emptyMap(), + ) +} + +private data class MissingType( + val source: String, + val proof: CompilerMissingMethod.Type, + val imports: List = emptyList(), +) + +private data class MissingParameter( + val source: String, + val type: CompilerMissingMethod.Type, + val binding: XdkMissingMethods.ArgumentBinding? = null, + val imports: List = emptyList(), +) + +/** Read only fresh resolved declarations, never TypeInfo from a failed body-validation attempt. */ +internal fun compilerMissingMethods( + nodes: List, + errors: ErrorListener, + inputs: XdkMissingMethods.Inputs = XdkMissingMethods.Inputs(), + destinations: Map = emptyMap(), +): List = + nodes + .filterIsInstance() + .mapNotNull { call -> + val name = call.invokedExpression as? NameExpression ?: return@mapNotNull null + val qualified = name.leftExpression != null + if (call.isAsync || name.hasTrailingTypeParams() || name.isSuppressDeref || + (if (qualified) name.calleeLocation() !in inputs.receivers else !name.isSimpleName) || + !XdkRename.identifier(name.name) + ) { + return@mapNotNull null + } + val ancestors = generateSequence(call.parent) { it.parent }.toList() + val method = ancestors.filterIsInstance().firstOrNull() ?: return@mapNotNull null + if (ancestors.takeWhile { it !== method }.any { it is LambdaExpression || it is TypeCompositionStatement }) { + return@mapNotNull null + } + val owner = + generateSequence(method.parent) { it.parent }.filterIsInstance().firstOrNull() + ?: return@mapNotNull null + if (generateSequence(method.parent) { it.parent } + .takeWhile { it !== owner } + .any { it is MethodDeclarationStatement || it is LambdaExpression } + ) { + return@mapNotNull null + } + val declaration = method.component as? MethodStructure ?: return@mapNotNull null + val receiver = inputs.receivers[name.calleeLocation()] + val destination = + if (qualified) { + nodes.filterIsInstance().singleOrNull { it.location() == receiver?.destination } + } else { + owner + } + val external = destinations.entries.singleOrNull { it.value.location == receiver?.destination } + val structure = (destination?.component ?: external?.key?.component) as? ClassStructure ?: return@mapNotNull null + val crossOwner = destination !== owner + // Preserve concrete call types; never infer an owner's formal from equal actual types. + // Exact lexical owner formals are rendered by memberSourceType and re-proven later. + if (crossOwner && + (structure.format !in setOf(Format.CLASS, Format.CONST, Format.SERVICE) || structure.isSynthetic) + ) { + return@mapNotNull null + } + val dispatch = + if (qualified) { + inputs.receivers.getValue(name.calleeLocation()).dispatch + } else if (declaration.isFunction) { + Dispatch.STATIC + } else { + Dispatch.INSTANCE + } + if (structure.format !in setOf(Format.CLASS, Format.CONST, Format.SERVICE, Format.MODULE) || declaration.isConstructor || + errors.isAbortDesired + ) { + return@mapNotNull null + } + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(missing-method)") { + // Reopened artifacts carry source indexes, but their pools are not linked for + // TypeInfo. Inspect the dependency through this fresh caller's linked pool. + val type = + if (external == null) { + structure.formalType + } else { + declaration.constantPool.ensureTerminalTypeConstant(external.key) + } + type.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + } + if (errors.hasSeriousErrors() || errors.isAbortDesired || + info.methods.values.any { it.signature.name == name.name } || info.properties.values.any { it.name == name.name } + ) { + return@mapNotNull null + } + + fun enclosed(node: AstNode) = generateSequence(node.parent) { it.parent }.any { it === method } + // An unqualified call must not be redirected around a body-local declaration. + val locals = + nodes + .filterIsInstance() + .filter(::enclosed) + val localNames = locals.map { it.name }.toSet() + if (!qualified && + ( + name.name in localNames || declaration.params.any { it.name == name.name } || + nodes.filterIsInstance().any { it !== method && it.name == name.name && enclosed(it) } + ) + ) { + return@mapNotNull null + } + + val formals = + declaration.params.take(declaration.typeParamCount).associate { parameter -> + val spelling = parameter.name?.takeIf(XdkRename::identifier) ?: return@mapNotNull null + parameter.asTypeParameterConstant(declaration.identityConstant) to spelling + } + + fun render(type: TypeConstant): MissingType? { + val rendered = + external?.value?.render(type, structure, formals) + ?: type.missingMethodType(structure, formals = formals)?.let { XdkMissingMethods.TypeSource(it) } + ?: return null + return MissingType(rendered.source, CompilerMissingMethod.Type.Resolved(type), rendered.imports) + } + + fun localType(local: VariableDeclarationStatement) = + inputs.localTypes[local.declarationLocation()]?.let { evidence -> + val rendered = + if (external == null) { + evidence.source?.let { XdkMissingMethods.TypeSource(it) } + } else { + evidence.destinationSources[external.value.location] + } + rendered?.let { MissingType(it.source, CompilerMissingMethod.Type.Detached(evidence.identity), it.imports) } + } ?: local.takeIf { it.hasExplicitMethodType() }?.type?.let(::render) + val returns = + when (val statement = call.parent) { + is ExpressionStatement -> { + if (statement.expression === call) emptyList() else return@mapNotNull null + } + + is ReturnStatement -> { + if (statement.expressions?.singleOrNull() !== call) return@mapNotNull null + declaration.returnTypes.map { render(it) ?: return@mapNotNull null } + } + + is AssignmentStatement -> { + val local = statement.lValue as? VariableDeclarationStatement ?: return@mapNotNull null + if (statement.op.id != Token.Id.ASN || statement.rValue !== call || !local.hasExplicitMethodType()) { + return@mapNotNull null + } + listOf(localType(local) ?: return@mapNotNull null) + } + + else -> { + return@mapNotNull null + } + } + + val conditional = declaration.isConditionalReturn && call.parent is ReturnStatement + val writtenReturns = returns.drop(if (conditional) 1 else 0) + val result = + when (writtenReturns.size) { + 0 -> "void" + 1 -> writtenReturns.single().source + else -> writtenReturns.joinToString(", ", "(", ")") { it.source } + } + val constraints = + declaration.params.take(declaration.typeParamCount).map { parameter -> + val constraint = parameter.type.paramTypes.singleOrNull() ?: return@mapNotNull null + render(constraint) ?: return@mapNotNull null + } + val generic = + if (formals.isEmpty()) { + "" + } else { + formals.values.zip(constraints).joinToString(", ", "<", "> ") { (formal, bound) -> + "$formal extends ${bound.source}" + } + } + val arguments = call.childNodes().filter { it !== name } + val labels = arguments.filterIsInstance().map { it.name } + if (labels.distinct().size != labels.size || labels.any { !XdkRename.identifier(it) }) return@mapNotNull null + val argumentNames = + buildList { + arguments.forEachIndexed { index, argument -> + add( + (argument as? LabeledExpression)?.name + ?: generateSequence(index + 1) { it + 1 }.map { "arg$it" }.first { it !in labels && it !in this }, + ) + } + } + val parameters = + arguments.mapIndexed { index, wrapped -> + val argument = (wrapped as? LabeledExpression)?.underlyingExpression ?: wrapped + val argumentName = argumentNames[index] + val expression = + (argument as? NameExpression) + ?.takeIf { it.isSimpleName && !it.hasTrailingTypeParams() && !it.isSuppressDeref } + // The declaration pass has not resolved body scopes. Limit proposals to prior + // block locals, then require the completed compiler to bind this exact use to + // that declaration. No spelling-based binding escapes the repair proof. + val local = + expression?.let { value -> + ancestors.filterIsInstance().firstNotNullOfOrNull { block -> + locals.singleOrNull { local -> + val statement = local.parent as? AssignmentStatement + val site = statement?.takeIf { it.lValue === local && it.op.id == Token.Id.ASN } ?: local + local.name == value.name && site.parent === block && site.endPosition < call.startPosition + } + } + } + if (local != null) { + val rendered = localType(local) ?: return@mapNotNull null + val binding = XdkMissingMethods.ArgumentBinding(argument.location(), local.declarationLocation()) + return@mapIndexed MissingParameter("${rendered.source} $argumentName", rendered.proof, binding, rendered.imports) + } + val parameter = + expression?.let { value -> + declaration.params.singleOrNull { + it.name == value.name && + it.name !in localNames + } + } + val evidence = inputs.expressions[argument.location()] + if (parameter == null && evidence != null) { + val rendered = + if (external == null) { + evidence.source?.let { XdkMissingMethods.TypeSource(it) } + } else { + evidence.destinationSources[external.value.location] + } + rendered ?: return@mapNotNull null + return@mapIndexed MissingParameter( + "${rendered.source} $argumentName", + CompilerMissingMethod.Type.Detached(evidence.identity), + imports = rendered.imports, + ) + } + val type = parameter?.type ?: (argument as? LiteralExpression)?.getImplicitType(null) ?: return@mapNotNull null + // Untyped numeric literals still have a choice of runtime representation; do not + // turn the compiler's IntLiteral/FPLiteral implementation types into a public signature. + if (type == type.constantPool.typeIntLiteral() || type == type.constantPool.typeFPLiteral() || + parameter?.annotations?.isNotEmpty() == true + ) { + return@mapNotNull null + } + val rendered = render(type) ?: return@mapNotNull null + MissingParameter("${rendered.source} $argumentName", rendered.proof, imports = rendered.imports) + } + + fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + val signature = parameters.joinToString(", ") { it.source } + CompilerMissingMethod( + XdkMissingMethods.Candidate( + name.calleeLocation(), + name.name, + external?.value?.insertion + ?: position(requireNotNull(destination).ensureBody().endPosition).let { it.copy(column = it.column - 1) }, + external?.value?.location ?: requireNotNull(destination).location(), + "${if (crossOwner) "public" else "private"} ${if (dispatch == Dispatch.STATIC) "static " else ""}" + + "$generic${if (conditional) "conditional " else ""}$result ${name.name}($signature)", + dispatch, + parameters.mapNotNull { it.binding }, + if (crossOwner) structure.identityConstant.pathString else null, + importSource = external?.value?.importSource, + imports = + (parameters.flatMap { it.imports } + returns.flatMap { it.imports } + constraints.flatMap { it.imports }) + .distinct(), + ), + declaration.params.take(declaration.typeParamCount).map { CompilerMissingMethod.Type.Resolved(it.type) } + + parameters.map { it.type }, + returns.map { it.proof }, + formals.keys.map { it.type }, + conditional, + ) + }.distinct() + +/** + * Copy compiler-established body facts without resuming a failed body or requesting its TypeInfo. + * Inferred locals need a validated initializer; an explicit declaration needs its resolved register. + */ +internal fun EmbeddingSupport.Compilation.missingMethodInputs( + destinations: Map = emptyMap(), +): CompilerMissingInputs = + ExecutionTrace.api("Compilation.missingMethodInputs") { + val root = parsed() ?: return@api CompilerMissingInputs() + ConstantPool.withPool(pool()).use { + fun descendants(node: AstNode): Sequence = sequenceOf(node) + node.childNodes().asSequence().flatMap(::descendants) + val nodes = descendants(root).toList() + val receivers = + nodes + .filterIsInstance() + .mapNotNull { call -> + val callee = call.invokedExpression as? NameExpression ?: return@mapNotNull null + val receiver = callee.leftExpression ?: return@mapNotNull null + val receiverName = receiver as? NameExpression + if (!receiver.isValidated || !receiver.typeFit.isFit || + generateSequence(receiverName) { it.leftExpression as? NameExpression } + .any { it.isSuppressDeref || it.hasTrailingTypeParams() } + ) { + return@mapNotNull null + } + val identityAndDispatch = + when (val target = receiverName?.resolvedTarget) { + is ClassConstant -> { + target to + if ((target.component as? ClassStructure)?.isSingleton == + true + ) { + Dispatch.INSTANCE + } else { + Dispatch.STATIC + } + } + + is Register -> { + if (target.index < 0 && target.index !in setOf(Op.A_THIS, Op.A_PRIVATE)) { + return@mapNotNull null + } + val type = receiver.type ?: return@mapNotNull null + if (!type.isSingleUnderlyingClass(false)) return@mapNotNull null + type.getSingleUnderlyingClass(false) to Dispatch.INSTANCE + } + + else -> { + // Retain the validated computed/property receiver type. Never + // re-evaluate it or query TypeInfo on this failed attempt. + val type = receiver.type ?: return@mapNotNull null + if (!type.isSingleUnderlyingClass(false)) return@mapNotNull null + type.getSingleUnderlyingClass(false) to Dispatch.INSTANCE + } + } + val (identity, dispatch) = identityAndDispatch + // Dependency source indexes locate declarations but do not authorize edits. + // Only a matching destination from the configured source graph is writable. + val destination = + nodes + .filterIsInstance() + .singleOrNull { + (it.component as? ClassStructure)?.identityConstant == identity + }?.location() ?: destinations[identity]?.location ?: return@mapNotNull null + if (dispatch == Dispatch.STATIC && identity.component.format != Format.CLASS) return@mapNotNull null + callee.calleeLocation() to XdkMissingMethods.Receiver(destination, dispatch) + }.toMap() + val destinationLocations = receivers.values.mapTo(hashSetOf()) { it.destination } + val referencedDestinations = destinations.filterValues { it.location in destinationLocations } + val localTypes = + nodes + .filterIsInstance() + .mapNotNull { local -> + if (!local.hasExplicitMethodType()) { + if (local.childNodes().none { it is VariableTypeExpression }) return@mapNotNull null + val assignment = local.parent as? AssignmentStatement ?: return@mapNotNull null + if (assignment.lValue !== local || assignment.op.id != Token.Id.ASN || + !assignment.rValue.isValidated || !assignment.rValue.typeFit.isFit + ) { + return@mapNotNull null + } + } + val owner = generateSequence(local.parent) { it.parent }.filterIsInstance().firstOrNull() + val structure = owner?.component as? ClassStructure ?: return@mapNotNull null + val type = local.register?.originalType ?: return@mapNotNull null + val destinationSources = + referencedDestinations.entries + .mapNotNull { (identity, destination) -> + destination.render(type, identity.component as ClassStructure)?.let { + destination.location to + it + } + }.toMap() + val rendered = type.missingMethodType(structure) + if (rendered == null && destinationSources.isEmpty()) return@mapNotNull null + local.declarationLocation() to CompilerMissingInputs.LocalType(rendered, type, destinationSources) + }.toMap() + val expressions = + nodes + .filterIsInstance() + .flatMap { call -> + call.childNodes().filter { it !== call.invokedExpression }.map { + (it as? LabeledExpression)?.underlyingExpression ?: it + } + }.filterIsInstance() + .mapNotNull { expression -> + if (!expression.isValidated || !expression.typeFit.isFit || expression.valueCount != 1) return@mapNotNull null + val type = expression.type ?: return@mapNotNull null + if (type == pool().typeIntLiteral() || type == pool().typeFPLiteral()) return@mapNotNull null + val ancestors = generateSequence(expression.parent) { it.parent }.toList() + val ownerNode = ancestors.filterIsInstance().firstOrNull() + val owner = ownerNode?.component as? ClassStructure ?: return@mapNotNull null + val methodNode = ancestors.filterIsInstance().firstOrNull() + val method = methodNode?.component as? MethodStructure + val formals = + method?.params?.take(method.typeParamCount).orEmpty().associate { + val name = it.name ?: return@mapNotNull null + val parameter = it.asTypeParameterConstant(requireNotNull(method).identityConstant) + parameter to name + } + val destinations = + referencedDestinations.entries + .mapNotNull { (identity, destination) -> + val rendered = destination.render(type, identity.component as ClassStructure, formals) + rendered?.let { destination.location to it } + }.toMap() + val spelling = type.missingMethodType(owner, formals = formals) + val copied = CompilerMissingInputs.LocalType(spelling, type, destinations) + expression.location() to copied + }.toMap() + CompilerMissingInputs(localTypes, receivers, expressions) + } + } + +private fun VariableDeclarationStatement.hasExplicitMethodType(): Boolean = + childNodes().none { it is VariableTypeExpression || it is AnnotatedTypeExpression } + +private fun TypeConstant.missingMethodType( + owner: ClassStructure, + modules: Map = emptyMap(), + formals: Map = emptyMap(), +): String? = + // Declaration analysis resolves the identity but can retain its parser wrapper. + resolveTypedefs().memberSourceType(owner.identityConstant, formals, modules + implicitModuleAliases) + +/** Type spelling and only its required imports travel together across compiler attempts. */ +private fun XdkMissingMethods.Destination.render( + type: TypeConstant, + owner: ClassStructure, + formals: Map = emptyMap(), +): XdkMissingMethods.TypeSource? { + val resolved = type.resolveTypedefs() + val source = resolved.missingMethodType(owner, modules, formals) ?: return null + val required = + resolved + .memberClasses() + .filter { it.constantPool.getImplicitlyImportedIdentity(it.name) != it } + .mapNotNull { imports[it.moduleConstant.name] } + .distinct() + return XdkMissingMethods.TypeSource(source, required) +} + +/** Detach source destinations before their compilation and AST leave this worker attempt. */ +internal fun EmbeddingSupport.Compilation.missingMethodDestinations( + dependencies: Set = emptySet(), +): Map = + ExecutionTrace.api("Compilation.missingMethodDestinations") { + check(succeeded()) + ConstantPool.withPool(pool()).use { + fun descendants(node: AstNode): Sequence = sequenceOf(node) + node.childNodes().asSequence().flatMap(::descendants) + val nodes = descendants(requireNotNull(parsed())).filterIsInstance().toList() + // Reuse module-level imports and reserve all source names before planning new aliases. + // The host supplies only dependencies already available to this source module. + val existing = compilerModuleAliases(nodes) + val lexicalErrors = ErrorList() + val names = + nodes + .map { it.source } + .distinctBy { it.fileName } + .flatMap { source -> + ExecutionTrace.api("Lexer.lex(missing-method-imports)") { + Lexer(source.clone(), lexicalErrors).asSequence().map { it.valueText }.toList() + } + }.toSet() + if (lexicalErrors.hasSeriousErrors()) return@use emptyMap() + val aliases = XdkMemberActions.moduleAliases(dependencies - file().module.name, existing, names) + nodes + .mapNotNull { node -> + val structure = node.component as? ClassStructure ?: return@mapNotNull null + if (structure.format !in setOf(Format.CLASS, Format.CONST, Format.SERVICE) || + structure.isSynthetic + ) { + return@mapNotNull null + } + val moduleNode = + generateSequence(node as AstNode) { it.parent } + .filterIsInstance() + .lastOrNull { it.category.id == Token.Id.MODULE } + // A companion admits one top-level declaration. Module package imports + // belong in the root, even when the generated method belongs in a companion. + val module = moduleNode ?: return@mapNotNull null + val importAt = + module.ensureBody().startPosition.let { + SemanticModel.Position(Source.calculateLine(it), Source.calculateOffset(it) + 1) + } + val imports = + aliases.filterKeys { it !in existing }.mapValues { (module, alias) -> + XdkMemberActions.Import(importAt, " package $alias import $module;") + } + val end = node.ensureBody().endPosition + val declaration = node.location(node.nameToken.startPosition, node.nameToken.endPosition) + declaration to + XdkMissingMethods.Destination( + node.location(), + SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end) - 1), + aliases, + imports, + module.source.fileName, + ) + }.toMap() + } + } + +/** Reassociate only detached project-owned source locations with this attempt's fresh constants. */ +internal fun XdkDependencies.Open.missingMethodDestinations( + destinations: Map, +): Map = + declarations.entries + .mapNotNull { (identity, declaration) -> + val owner = identity as? ClassConstant ?: return@mapNotNull null + destinations[declaration.location]?.let { owner to it } + }.toMap() + +private fun AstNode.location( + start: Long = startPosition, + end: Long = endPosition, +): SemanticModel.SourceLocation { + fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + return SemanticModel.SourceLocation(source.fileName, SemanticModel.Range(position(start), position(end))) +} + +private fun VariableDeclarationStatement.declarationLocation() = location(nameToken.startPosition, nameToken.endPosition) + +private fun NameExpression.calleeLocation() = location(nameToken.startPosition, nameToken.endPosition) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPrimaryParameters.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPrimaryParameters.kt new file mode 100644 index 00000000000..87edba3adeb --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPrimaryParameters.kt @@ -0,0 +1,19 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.MethodStructure +import org.xvm.asm.PropertyStructure +import org.xvm.asm.constants.PropertyConstant + +/** + * The compiler creates one synthetic shorthand constructor from a class header. Its parameters + * initialize the correspondingly named properties. Both flags survive artifact serialization; + * explicit constructors have their own written parameter contracts and are not joined here. Read + * only on the compiler worker, then copy the property identity into detached source facts. + */ +internal fun MethodStructure.primaryProperty(index: Int): PropertyConstant? { + if (!isConstructor || !isSynthetic || !isShorthandConstructor) return null + val owner = identityConstant.namespace.component as? ClassStructure ?: return null + val parameter = params.getOrNull(index + typeParamCount) ?: return null + return (owner.getChild(parameter.name) as? PropertyStructure)?.identityConstant +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt new file mode 100644 index 00000000000..af0043f4b61 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt @@ -0,0 +1,69 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorListener +import org.xvm.asm.PropertyStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodBody.Implementation +import org.xvm.asm.constants.PropertyConstant +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.util.ExecutionTrace + +/** Attempt-owned property dispatch facts, including written getter/setter owners. */ +internal class CompilerPropertyRelations( + val declarations: Set = emptySet(), + val chains: List = emptyList(), +) { + data class Chain( + val owner: IdentityConstant, + val properties: List, + val supported: Boolean, + ) +} + +internal fun compilerPropertyRelations( + nodes: List, + errors: ErrorListener, +): CompilerPropertyRelations { + val primaryProperties = + nodes + .filterIsInstance() + .filter { it.parent is TypeCompositionStatement } + .mapNotNull { it.resolvedTarget as? PropertyConstant } + val declarations = + nodes + .filterIsInstance() + .mapNotNull { it.component as? PropertyStructure } + .filterNot { it.isStatic || it.isSynthetic } + .mapTo(linkedSetOf()) { it.identityConstant } + primaryProperties + val chains = + compilerSourceTypes(nodes) + .flatMap { type -> + if (errors.isAbortDesired) return@flatMap emptyList() + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(property-relations)") { + type.ensureTypeInfo(errors) + } + info.properties.values + .filter { property -> property.propertyBodies.any { it.identity in declarations } } + .map { property -> + CompilerPropertyRelations.Chain( + type.getSingleUnderlyingClass(true), + property.propertyBodies.map { it.identity }, + property.propertyBodies.all { + it.implementation in + setOf( + Implementation.Explicit, + Implementation.Declared, + Implementation.Default, + Implementation.FromInto, + Implementation.Delegating, + ) + }, + ) + } + }.distinct() + return CompilerPropertyRelations(declarations, chains) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt new file mode 100644 index 00000000000..699e99e29df --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt @@ -0,0 +1,213 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.lsp.util.ProgressLabels +import java.util.Locale +import java.util.concurrent.CompletableFuture +import java.util.concurrent.atomic.AtomicLong + +/** Observes the existing worker; no compiler work or future completion runs under this lock. */ +internal class CompilerQueueTrace { + private enum class Phase { + DEBOUNCING, + QUEUED, + RUNNING, + } + + private data class Job( + val span: ExecutionTrace.Span, + val submission: Long, + val phase: Phase, + ) + + private val queue = queues.incrementAndGet() + private val lock = Any() + private val submitted = AtomicLong() + private val enqueued = AtomicLong() + private val started = AtomicLong() + private val jobs = linkedMapOf() + private val readyOrder = linkedSetOf() + + fun task( + kind: String, + uri: String, + result: CompletableFuture<*>, + action: () -> Unit, + ): Work = Work(kind, uri, result, action) + + inner class Work + internal constructor( + kind: String, + uri: String, + private val result: CompletableFuture<*>, + private val action: () -> Unit, + ) : Runnable { + private val span = ExecutionTrace.span("compiler-queue", kind, uri) + + init { + synchronized(lock) { + jobs[span.id] = Job(span, submitted.incrementAndGet(), Phase.DEBOUNCING) + log(span, "submitted") + } + result.whenComplete { _, failure -> + synchronized(lock) { + val job = jobs[span.id] ?: return@synchronized + if (job.phase != Phase.RUNNING) { + jobs.remove(span.id) + readyOrder.remove(span.id) + log( + span, + "removed", + mapOf( + "outcome" to + if (result.isCancelled) { + "cancelled-before-start" + } else { + "completed-before-start" + }, + ), + ) + } else if (failure != null) { + // Cancellation signals do not prove that cooperative compiler work has + // returned. + log(span, "cancel-or-failure-signalled") + } + } + } + } + + /** Called at the existing executor submission, after any debounce interval. */ + fun ready(): Work = + apply { + synchronized(lock) { + jobs[span.id]?.let { jobs[span.id] = it.copy(phase = Phase.QUEUED) } + if (span.id in jobs) readyOrder += span.id + log(span, "queued", mapOf("enqueueOrder" to enqueued.incrementAndGet())) + } + } + + override fun run() { + val tracked = + synchronized(lock) { + val job = jobs[span.id] + if (job == null) { + false + } else { + jobs[span.id] = job.copy(phase = Phase.RUNNING) + readyOrder.remove(span.id) + log( + span, + "start", + mapOf( + "executionOrder" to started.incrementAndGet(), + "waitMs" to ExecutionTrace.elapsed(span.created), + ), + ) + true + } + } + // Preserve the original runnable even if cancellation raced with dequeueing it. + if (!tracked) { + action() + return + } + val begin = System.nanoTime() + try { + ExecutionTrace.within(span, action) + } finally { + synchronized(lock) { + jobs.remove(span.id) + val outcome = + when { + result.isCancelled -> "cancelled" + result.isCompletedExceptionally -> "failed" + result.isDone -> "completed" + else -> "incomplete" + } + log( + span, + "end", + mapOf("outcome" to outcome, "runMs" to ExecutionTrace.elapsed(begin)), + ) + } + } + } + } + + /** Copied metadata only; never enters compiler code or waits for the worker. */ + fun snapshot(): Map = + synchronized(lock) { + fun Job.label() = "#${this.span.id} ${this.span.operation} ${this.span.uri}" + val queued = readyOrder.mapNotNull(jobs::get).map { it.label() } + val waiting = jobs.values.filter { it.phase == Phase.DEBOUNCING }.map { it.label() } + val running = jobs.values.filter { it.phase == Phase.RUNNING }.map { it.label() } + mapOf( + "queue" to queue, + "submittedTotal" to submitted.get(), + "startedTotal" to started.get(), + "queueSize" to queued.size, + "queuedJobs" to queued, + "debouncingSize" to waiting.size, + "debouncingJobs" to waiting, + "runningSize" to running.size, + "runningJobs" to running, + ) + } + + /** UI text from copied queue metadata only; never inspects an AST or enters compiler locks. */ + fun progressDescription(): String? { + val current = synchronized(lock) { jobs.values.toList() } + + fun Job.label(): String { + val operation = + when (span.operation) { + "compile" -> { + "compiling" + } + + "project-REFERENCES" -> { + "finding references in" + } + + "project-DIAGNOSTICS" -> { + "checking diagnostics in" + } + + "rename-proof", "project-RENAME", "project-RENAME_PROPOSAL", "project-FILE_RENAME" -> { + "checking rename in" + } + + else -> { + val kind = + span.operation + .removePrefix("project-") + .removePrefix("cursor-") + .replace('_', ' ') + .lowercase(Locale.ROOT) + "checking $kind in" + } + } + return "$operation ${ProgressLabels.source(span.uri.orEmpty())}" + } + return buildList { + current.filter { it.phase == Phase.RUNNING }.forEach { add(it.label()) } + val queued = current.count { it.phase == Phase.QUEUED } + if (queued > 0) add("$queued ${if (queued == 1) "job" else "jobs"} queued") + current.firstOrNull { it.phase == Phase.DEBOUNCING }?.let { + add("waiting for edits to settle: ${it.label()}") + } + }.takeIf { it.isNotEmpty() }?.joinToString("; ") + } + + private fun log( + span: ExecutionTrace.Span, + event: String, + fields: Map = emptyMap(), + ) { + ExecutionTrace.event(span, event, snapshot() + fields) + } + + private companion object { + val queues = AtomicLong() + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt new file mode 100644 index 00000000000..de4053f370e --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt @@ -0,0 +1,85 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.constants.AnnotatedTypeConstant +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.ImmutableTypeConstant +import org.xvm.asm.constants.InnerChildTypeConstant +import org.xvm.asm.constants.ParameterizedTypeConstant +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.RecursiveTypeConstant +import org.xvm.asm.constants.RelationalTypeConstant +import org.xvm.asm.constants.ServiceTypeConstant +import org.xvm.asm.constants.TerminalTypeConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.asm.constants.VirtualChildTypeConstant + +/** Copy receiver substitutions and annotations; source identities still translate through edits. */ +internal fun receiverIdentity( + receiver: TypeConstant, + identity: (Constant) -> ProofIdentity, + depth: Int = 0, +): ProofIdentity? { + if (depth >= 64 || receiver.containsUnresolved()) return null + val type = receiver.resolveTypedefs().removeAccess() + + fun nested(type: TypeConstant) = receiverIdentity(type, identity, depth + 1) + return when (type) { + // Recursive types extend TerminalTypeConstant but have no single defining constant. + is RecursiveTypeConstant -> { + identity(type.typedef) + } + + is TerminalTypeConstant -> { + when (val declaration = type.definingConstant) { + is ClassConstant, is TypeParameterConstant -> identity(declaration) + is PropertyConstant -> declaration.takeIf { it.isFormalType }?.let(identity) + else -> null + } + } + + is ParameterizedTypeConstant -> { + val base = nested(type.underlyingType) ?: return null + val arguments = type.paramTypes.map { nested(it) ?: return null } + ProofIdentity.TypeShape(type.format, listOf(base) + arguments) + } + + is AnnotatedTypeConstant -> { + val base = nested(type.underlyingType) ?: return null + val annotation = nested(type.annotationType) ?: return null + val arguments = + type.annotationParams.map { annotationIdentity(it, identity, depth + 1) ?: return null } + ProofIdentity.TypeShape(type.format, listOf(base, annotation) + arguments) + } + + is ImmutableTypeConstant, is ServiceTypeConstant -> { + ProofIdentity.TypeShape(type.format, listOf(nested(type.underlyingType) ?: return null)) + } + + is VirtualChildTypeConstant -> { + val parent = nested(type.parentType) ?: return null + val origin = type.originParentType?.let { nested(it) ?: return null } + ProofIdentity.TypeShape(type.format, listOf(parent, identity(type.definingConstant)) + listOfNotNull(origin)) + } + + is InnerChildTypeConstant -> { + ProofIdentity.TypeShape(type.format, listOf(nested(type.parentType) ?: return null, identity(type.definingConstant))) + } + + is RelationalTypeConstant -> { + val operands = listOf(nested(type.underlyingType) ?: return null, nested(type.underlyingType2) ?: return null) + val components = + if (type.format == Constant.Format.DifferenceType) { + operands + } else { + listOf(ProofIdentity.Alternatives(operands.toSet())) + } + ProofIdentity.TypeShape(type.format, components) + } + + else -> { + null + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt new file mode 100644 index 00000000000..51caf6d3354 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt @@ -0,0 +1,558 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.Constant +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodConstant +import org.xvm.asm.constants.PackageConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.asm.constants.UnionTypeConstant +import org.xvm.lsp.util.ExecutionTrace +import java.io.File +import java.util.UUID + +/** Compiler-proven identities copied before an attempt's pool and AST can be released. */ +internal sealed interface ProofIdentity { + data class Source( + val location: SemanticModel.SourceLocation, + val format: Constant.Format, + val name: String, + ) : ProofIdentity + + data class Binary( + val key: XdkDependency.SymbolKey, + ) : ProofIdentity + + /** Generated method identities can reuse an artifact signature without occupying its table. */ + data class Method( + val parent: ProofIdentity, + val signature: XdkDependency.SymbolKey, + ) : ProofIdentity + + data class Parameter( + val method: ProofIdentity, + val index: Int, + ) : ProofIdentity + + /** Hidden generic binders do not share the visible value-parameter slot namespace. */ + data class MethodFormal( + val method: ProofIdentity, + val index: Int, + ) : ProofIdentity + + /** The single compiler-generated shorthand constructor, with no invented declaration span. */ + data class PrimaryConstructor( + val owner: ProofIdentity, + ) : ProofIdentity + + /** A mixin super register is relative to its written method and each adopting host's chain. */ + data class Super( + val method: ProofIdentity, + ) : ProofIdentity + + /** A predefined receiver is bound to its compiler-selected class and access view. */ + data class Receiver( + val owner: ProofIdentity, + val register: Int, + ) : ProofIdentity + + /** A generated forwarding method is identified by its host, written contracts and receivers. */ + data class Composed( + val owner: ProofIdentity, + val members: List, + val delegates: List, + val cycles: List = emptyList(), + val alternatives: List = emptyList(), + ) : ProofIdentity + + /** Unordered alternatives, each retaining its receiver, ordered contracts and delegates. */ + data class Alternatives( + val targets: Set, + ) : ProofIdentity + + /** Compiler type structure: base then ordered arguments; relational alternatives are sets. */ + data class TypeShape( + val format: Constant.Format, + val components: List, + ) : ProofIdentity + + /** Literal annotation arguments retain their constant kind as well as their exact value. */ + data class Value( + val format: Constant.Format, + val value: String, + ) : ProofIdentity + + /** An implicit package has a directory identity, with no invented source declaration. */ + data class Directory( + val path: String, + ) : ProofIdentity + + /** No cross-attempt equivalence is safe when neither a declaration nor artifact proves it. */ + data class Unproven( + val id: UUID = UUID.randomUUID(), + ) : ProofIdentity +} + +internal data class CompilerReceiver( + val owner: IdentityConstant, + val register: Int, +) + +internal class ProofRelations( + val declarations: Set = emptySet(), + val chains: List = emptyList(), +) { + data class Chain( + val owner: ProofIdentity, + val members: List, + val supported: Boolean, + val cycles: List = emptyList(), + val alternatives: List = emptyList(), + ) +} + +/** Attempt-owned inputs; copied into detached identities before leaving the compiler worker. */ +internal data class CompilerExtractionFacts( + val types: Map, + val stableValues: Set, + val sourceTypes: Map = emptyMap(), +) + +/** Types and stable value reads needed to prove a new helper's signature and captured inputs. */ +internal data class ExtractMethodFacts( + val types: Map = emptyMap(), + val stableValues: Set = emptySet(), + val sourceTypes: Map = emptyMap(), +) + +/** Detached comparison facts. Keeping compiler constants here retains every root's entire pool. */ +internal class CompilerRenameFacts( + val models: List, + val constants: Map, + val methods: ProofRelations = ProofRelations(), + val properties: ProofRelations = ProofRelations(), + val imports: List = emptyList(), + private val modules: Map = emptyMap(), + val memberActions: List = emptyList(), + val typeNames: List = emptyList(), + val typePaths: List = emptyList(), + val resourceValues: Map = emptyMap(), + val callables: Map = emptyMap(), + val removableLocals: Set = emptySet(), + val extraction: ExtractMethodFacts = ExtractMethodFacts(), + val missingMethods: List = emptyList(), + val methodSignatures: Map = emptyMap(), + val missingMethodInputs: XdkMissingMethods.Inputs = XdkMissingMethods.Inputs(), + val constantProperties: Set = emptySet(), +) { + /** Unchanged independent modules cannot acquire new bindings from a source edit elsewhere. */ + fun within(scopes: Set): CompilerRenameFacts = merge(modules.filterKeys(scopes::contains)) + + companion object { + fun merge(modules: Map): CompilerRenameFacts { + val attempts = modules.values + return CompilerRenameFacts( + attempts.flatMap { it.models }, + attempts.flatMap { it.constants.entries }.associate { it.toPair() }, + ProofRelations( + attempts.flatMapTo(linkedSetOf()) { it.methods.declarations }, + attempts.flatMap { it.methods.chains }, + ), + ProofRelations( + attempts.flatMapTo(linkedSetOf()) { it.properties.declarations }, + attempts.flatMap { it.properties.chains }, + ), + attempts.flatMap { it.imports }.distinct(), + modules, + attempts.flatMap { it.memberActions }, + attempts.flatMap { it.typeNames }.distinct(), + attempts.flatMap { it.typePaths }.distinct(), + attempts.flatMap { it.resourceValues.entries }.associate { it.toPair() }, + attempts.flatMap { it.callables.entries }.associate { it.toPair() }, + attempts.flatMapTo(linkedSetOf()) { it.removableLocals }, + ExtractMethodFacts( + attempts.flatMap { it.extraction.types.entries }.associate { it.toPair() }, + attempts.flatMapTo(linkedSetOf()) { it.extraction.stableValues }, + attempts.flatMap { it.extraction.sourceTypes.entries }.associate { it.toPair() }, + ), + attempts.flatMap { it.missingMethods }.distinct(), + attempts.flatMap { it.methodSignatures.entries }.associate { it.toPair() }, + XdkMissingMethods.Inputs( + attempts.flatMap { it.missingMethodInputs.localTypes.entries }.associate { it.toPair() }, + attempts.flatMap { it.missingMethodInputs.receivers.entries }.associate { it.toPair() }, + ), + attempts.flatMapTo(linkedSetOf()) { it.constantProperties }, + ) + } + } +} + +/** Resolve keys by compiler equality, never by a printed signature or type name. */ +internal fun captureRenameFacts( + models: List, + constants: Map, + dependencies: XdkDependencies.Open? = null, + methods: CompilerMethodRelations = CompilerMethodRelations(emptySet(), emptyList()), + properties: CompilerPropertyRelations = CompilerPropertyRelations(), + errors: ErrorListener? = null, + supers: Map = emptyMap(), + members: List = emptyList(), + receivers: Map = emptyMap(), + typeNames: List = emptyList(), + resourceValues: Map = emptyMap(), + callables: Map> = emptyMap(), + removableLocals: Set = emptySet(), + extraction: CompilerExtractionFacts? = null, + missingMethods: List = emptyList(), + missingInputs: CompilerMissingInputs = CompilerMissingInputs(), + constantProperties: Set = emptySet(), +): CompilerRenameFacts { + val declarations = + models + .flatMap { it.symbols } + .mapNotNull { symbol -> + val source = symbol.declarationSource ?: return@mapNotNull null + val range = symbol.declaration ?: return@mapNotNull null + constants[symbol.id]?.let { it to SemanticModel.SourceLocation(source, range) } + }.toMap() + val sourceMethods = + models + .flatMap { model -> + model.symbols.filter { + it.kind == SemanticModel.SymbolKind.METHOD && it.declaration != null && + it.declarationSource == model.sourceName + } + }.mapTo(hashSetOf()) { it.id } + val identities = mutableMapOf() + + fun identity(constant: Constant): ProofIdentity = + identities.getOrPut(constant) { + val value = constant as? IdentityConstant + val module = value?.moduleConstant?.name + val location = + declarations[constant] + ?: value?.let { dependencies?.declarations?.get(it)?.location } + val host = (constant as? MethodConstant)?.namespace + val sourceHost = + host != null && + (host in declarations || dependencies?.declarations?.containsKey(host) == true) + val packageParent = (constant as? PackageConstant)?.let { identity(it.parentConstant) } + when { + constant is TypeParameterConstant -> { + ProofIdentity.MethodFormal(identity(constant.method), constant.register) + } + + // Bundled source navigation must use the same artifact identity as binary-only + // views. + location != null && module !in XdkLibraries.moduleNames -> { + ProofIdentity.Source(location, constant.format, requireNotNull(value).name) + } + + constant is PackageConstant && + module !in XdkLibraries.moduleNames && + ( + packageParent is ProofIdentity.Source || + packageParent is ProofIdentity.Directory + ) -> { + val directory = + when (packageParent) { + is ProofIdentity.Source -> { + packageParent.location.sourceName?.let(::File)?.let { + File(it.parentFile, it.nameWithoutExtension) + } + } + + is ProofIdentity.Directory -> { + File(packageParent.path) + } + } + directory?.let { ProofIdentity.Directory(File(it, constant.name).path) } + ?: ProofIdentity.Unproven() + } + + constant is MethodConstant && + sourceHost && + (constant.component as? MethodStructure)?.let { + it.isSynthetic && it.isShorthandConstructor + } == true -> { + ProofIdentity.PrimaryConstructor(identity(host)) + } + + constant is MethodConstant && + module !in XdkLibraries.moduleNames && + sourceHost && + errors != null -> { + val structure = host.component as? ClassStructure + val info = + structure?.let { + ExecutionTrace.api("TypeConstant.ensureTypeInfo(rename-provenance)") { + it.formalType.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + } + } + val route = + info?.let { owner -> + owner.getMethodById(constant)?.let { owner.dispatch(it, errors) } + } + if ( + route == null || + !route.supported || + route.methods.isEmpty() || + constant in route.methods + ) { + ProofIdentity.Unproven() + } else { + route.proof(identity(host), ::identity) + } + } + + module != null && module in XdkLibraries.moduleNames -> { + val index = + XdkLibraries + .module(module) + .constantPool + .getConstant(constant) + ?.position + when { + index != null -> { + ProofIdentity.Binary( + XdkDependency.SymbolKey( + module, + XdkLibraries.revision(module), + index, + ), + ) + } + + constant is MethodConstant && !constant.isLambda -> { + val signature = + XdkLibraries + .module(module) + .constantPool + .getConstant(constant.signature) + ?.position + if (signature == null) { + ProofIdentity.Unproven() + } else { + ProofIdentity.Method( + identity(constant.parentConstant), + XdkDependency.SymbolKey( + module, + XdkLibraries.revision(module), + signature, + ), + ) + } + } + + else -> { + ProofIdentity.Unproven() + } + } + } + + module != null && dependencies?.revisions?.containsKey(module) == true -> { + val index = + dependencies.repository + .loadModule(module) + ?.constantPool + ?.getConstant(constant) + ?.position + if (index == null) { + ProofIdentity.Unproven() + } else { + ProofIdentity.Binary( + XdkDependency.SymbolKey( + module, + dependencies.revisions.getValue(module), + index, + ), + ) + } + } + + else -> { + ProofIdentity.Unproven() + } + } + } + + fun path(value: IdentityConstant): List = value.path.filter { it.format != Constant.Format.Module }.map { it.name } + + fun signatureType(type: CompilerMissingMethod.Type): ProofIdentity? = + when (type) { + is CompilerMissingMethod.Type.Resolved -> receiverIdentity(type.value, ::identity) + is CompilerMissingMethod.Type.Detached -> type.value + } + + fun cycle(cycle: CompilerDispatch.Cycle) = + ProofIdentity.Composed( + receiverIdentity(cycle.receiver, ::identity) ?: ProofIdentity.Unproven(), + cycle.contracts.map(::identity), + emptyList(), + ) + + val routedOwners = methods.chains.filter { it.cycles.isNotEmpty() || it.alternatives.isNotEmpty() }.mapTo(hashSetOf()) { it.owner } + val callableIdentities = + if (errors == null) { + emptyMap() + } else { + callables.values + .distinct() + .filter { (receiver, _) -> + receiver.resolveTypedefs().removeAccess() is UnionTypeConstant || + (receiver.isSingleUnderlyingClass(false) && receiver.getSingleUnderlyingClass(false) in routedOwners) + }.associateWith { (receiver, method) -> callableIdentity(receiver, method, errors, ::identity) } + } + + return CompilerRenameFacts( + models, + constants.mapValues { identity(it.value) } + + models + .distinctBy { it.id } + .flatMap { model -> + model.parameters.mapNotNull { (id, slot) -> + constants[slot.method]?.let { + id to ProofIdentity.Parameter(identity(it), slot.index) + } + } + }.toMap() + + supers.mapValues { ProofIdentity.Super(identity(it.value)) } + + receivers.mapValues { (_, receiver) -> ProofIdentity.Receiver(identity(receiver.owner), receiver.register) }, + ProofRelations( + methods.declarations.mapTo(linkedSetOf(), ::identity), + methods.chains.map { + ProofRelations.Chain( + identity(it.owner), + it.methods.map(::identity), + it.supported, + it.cycles.map(::cycle), + it.alternatives.map { choice -> choice.proof(::identity) }, + ) + }, + ), + ProofRelations( + properties.declarations.mapTo(linkedSetOf(), ::identity), + properties.chains.map { + ProofRelations.Chain( + identity(it.owner), + it.properties.map(::identity), + it.supported, + ) + }, + ), + constants.values.mapNotNull(XdkAutoImports::target).distinct(), + memberActions = + members.mapNotNull { member -> + val owner = + identity(member.owner) as? ProofIdentity.Source ?: return@mapNotNull null + val contract = identity(member.contract) + if ( + contract !is ProofIdentity.Source && + contract !is ProofIdentity.Binary && + !( + contract is ProofIdentity.Method && + contract.parent is ProofIdentity.Binary + ) + ) { + return@mapNotNull null + } + XdkMemberActions.Candidate( + owner.location, + contract, + member.insertion, + member.declaration, + member.implementation, + member.requiredCount, + member.imports, + ) + }, + typeNames = + typeNames.map { + TypeName(it.location, it.terminal, identity(it.target), it.target.moduleConstant.name, path(it.target), it.imported) + }, + typePaths = + (constants.values.filterIsInstance() + typeNames.map { it.target }) + .flatMap { it.path } + .filter { + it.format in setOf(Constant.Format.Module, Constant.Format.Package, Constant.Format.Class, Constant.Format.Typedef) + }.distinct() + .map { TypePath(identity(it), it.moduleConstant.name, path(it)) }, + resourceValues = resourceValues, + missingMethods = + missingMethods.mapNotNull { method -> + val parameters = method.parameters.map { signatureType(it) ?: return@mapNotNull null } + val returns = method.returns.map { signatureType(it) ?: return@mapNotNull null } + val formals = method.formals.map { receiverIdentity(it, ::identity) ?: return@mapNotNull null } + method.candidate.copy( + signature = + XdkMissingMethods.Signature( + parameters, + returns, + method.candidate.publicOwner != null, + formals, + method.conditional, + ), + ) + }, + methodSignatures = + constants + .filterKeys(sourceMethods::contains) + .mapNotNull { (id, constant) -> + val method = (constant as? MethodConstant)?.component as? MethodStructure ?: return@mapNotNull null + val parameters = method.params.map { receiverIdentity(it.type, ::identity) ?: return@mapNotNull null } + val returns = method.returnTypes.map { receiverIdentity(it, ::identity) ?: return@mapNotNull null } + val formals = + method.params + .take( + method.typeParamCount, + ).map { identity(it.asTypeParameterConstant(method.identityConstant)) } + id to + XdkMissingMethods.Signature( + parameters, + returns, + method.access == Access.PUBLIC, + formals, + method.isConditionalReturn, + ) + }.toMap(), + removableLocals = removableLocals, + constantProperties = constantProperties, + extraction = + ExtractMethodFacts( + extraction + ?.types + .orEmpty() + .mapNotNull { (at, type) -> receiverIdentity(type, ::identity)?.let { at to it } } + .toMap(), + extraction?.stableValues.orEmpty(), + extraction?.sourceTypes.orEmpty(), + ), + callables = + callables + .mapNotNull { (site, selected) -> + callableIdentities[selected]?.let { site to it } + }.toMap(), + missingMethodInputs = + XdkMissingMethods.Inputs( + missingInputs.localTypes.mapValues { (_, type) -> + XdkMissingMethods.LocalType( + type.source, + receiverIdentity(type.type, ::identity) ?: ProofIdentity.Unproven(), + type.destinationSources, + ) + }, + missingInputs.receivers, + missingInputs.expressions.mapValues { (_, type) -> + XdkMissingMethods.LocalType( + type.source, + receiverIdentity(type.type, ::identity) ?: ProofIdentity.Unproven(), + type.destinationSources, + ) + }, + ), + ) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt new file mode 100644 index 00000000000..0f5525bcbe7 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt @@ -0,0 +1,95 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.constants.FSNodeConstant +import org.xvm.asm.constants.FileStoreConstant +import org.xvm.asm.constants.StringConstant +import org.xvm.asm.constants.UInt8ArrayConstant +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.FileExpression +import org.xvm.compiler.ast.LiteralExpression +import java.security.MessageDigest +import java.util.HexFormat + +private val resourcePrefixes = setOf("$.", "$/", "#.", "#/") + +/** Detached resource values let a move reject changed contents even if both source graphs compile. */ +internal fun compilerResourceValues(nodes: List): Map = + nodes + .filterIsInstance() + .filter { node -> + // Parser lowers $path/#path includes directly to LiteralExpression. Their original + // source span is retained even though the literal token now contains the resource bytes. + node is FileExpression || + (node is LiteralExpression && node.source?.toString(node.startPosition, node.endPosition)?.take(2) in resourcePrefixes) + }.associate { node -> + fun position(at: Long) = SemanticModel.Position(Source.calculateLine(at), Source.calculateOffset(at)) + SemanticModel.SourceLocation( + node.source?.fileName, + SemanticModel.Range(position(node.startPosition), position(node.endPosition)), + ) to + resourceValue(node.toConstant()) + } + +private fun resourceValue(value: Constant?): String? { + val hash = MessageDigest.getInstance("SHA-256") + + fun bytes(value: ByteArray) { + hash.update(value.size.toString().toByteArray()) + hash.update(0.toByte()) + hash.update(value) + } + + fun text(value: String) = bytes(value.toByteArray()) + + fun append(value: Constant?): Boolean { + text(value?.format?.name ?: return false) + when (value) { + is StringConstant -> { + text(value.value) + } + + is UInt8ArrayConstant -> { + bytes(value.value) + } + + is FileStoreConstant -> { + text(value.path) + return append(value.value) + } + + is FSNodeConstant -> { + text(value.name) + // Filesystem timestamps change during ordinary Move/Undo. Content and names + // must remain equivalent; incidental timestamp metadata is not a binding. + when (value.format) { + Constant.Format.FSFile -> { + bytes(value.fileBytes) + } + + Constant.Format.FSDir -> { + val children = value.directoryContents.sortedBy { it.name } + text(children.size.toString()) + return children.all(::append) + } + + Constant.Format.FSLink -> { + return append(value.linkTarget) + } + + else -> { + return false + } + } + } + + else -> { + return false + } + } + return true + } + return if (append(value)) HexFormat.of().formatHex(hash.digest()) else null +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt new file mode 100644 index 00000000000..de0a7f028f0 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt @@ -0,0 +1,46 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.PackageStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.CompositionNode +import org.xvm.compiler.ast.ImportStatement +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.TypeCompositionStatement + +/** Copy resolved import destinations on the compiler worker; no path is guessed from spelling. */ +internal fun compilerSourceLinks( + nodes: List, + declaration: (IdentityConstant) -> SemanticModel.SourceLocation?, +): Map> = + nodes + .mapNotNull { node -> + val imported = + when (node) { + is ImportStatement -> { + // A wildcard resolves to its container, not to every exported child. + // Only use compiler-resolved identities; unsupported import syntax does + // not establish a destination merely by spelling a qualified name. + val names = node.qualifiedNameTokens + val first = node.aliasToken ?: names.firstOrNull() ?: return@mapNotNull null + val last = node.aliasToken ?: names.lastOrNull() ?: return@mapNotNull null + Triple(node.importedIdentity, first.startPosition, last.endPosition) + } + + is NamedTypeExpression -> { + val composition = node.parent as? CompositionNode.Import ?: return@mapNotNull null + val owner = (composition.parent as? TypeCompositionStatement)?.component as? PackageStructure + Triple(owner?.importedModule?.identityConstant, node.startPosition, node.endPosition) + } + + else -> { + return@mapNotNull null + } + } + val target = imported.first?.let(declaration)?.takeIf { it.sourceName != null } ?: return@mapNotNull null + + fun position(at: Long) = SemanticModel.Position(Source.calculateLine(at), Source.calculateOffset(at)) + node.source?.fileName to + SemanticModel.SourceLink(SemanticModel.Range(position(imported.second), position(imported.third)), target) + }.groupBy({ it.first }, { it.second }) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceTypes.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceTypes.kt new file mode 100644 index 00000000000..c411fa66444 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceTypes.kt @@ -0,0 +1,35 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component.Format +import org.xvm.asm.Constants.Access +import org.xvm.asm.constants.TypeConstant +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.TypeCompositionStatement + +/** + * Attempt-owned source types, including substitutions actually validated by the compiler. + * Conditional compositions may exist on Box without existing on its formal Box. + * Never invent instantiations or ask a bare type parameter for a private class view. + */ +internal fun compilerSourceTypes( + nodes: List, + includeMixins: Boolean = true, +): List { + val classes = + nodes + .filterIsInstance() + .mapNotNull { it.component as? ClassStructure } + .filter { includeMixins || it.format != Format.MIXIN } + .associateBy { it.identityConstant } + val instantiated = + nodes + .filterIsInstance() + .filter { it.isValidated && it.typeFit.isFit } + .mapNotNull { it.type } + .filter { !it.isFormalType && it.isSingleUnderlyingClass(false) && it.getSingleUnderlyingClass(false) in classes } + return (classes.values.map { it.formalType } + instantiated) + .map { it.ensureAccess(Access.PRIVATE) } + .distinct() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt new file mode 100644 index 00000000000..c0e958320cb --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt @@ -0,0 +1,63 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MultipleLValueStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.ParenthesizedExpression +import org.xvm.compiler.ast.SequentialAssignExpression +import org.xvm.compiler.ast.TupleExpression +import org.xvm.lsp.adapter.xdk.SemanticModel.Usage + +/** Syntactic assignment targets, paired later with resolved identities. Receivers remain reads. */ +internal fun compilerWrites(nodes: List): List> = + nodes.flatMap { node -> + when (node) { + is AssignmentStatement -> { + val usage = + when (node.category) { + AssignmentStatement.Category.InPlace, + AssignmentStatement.Category.CondLeft, + -> Usage.READ_WRITE + + else -> Usage.WRITE + } + writeTargets(node.lValue).map { it to usage } + } + + is SequentialAssignExpression -> { + node + .childNodes() + .asSequence() + .flatMap { writeTargets(it) } + .map { it to Usage.READ_WRITE } + .toList() + } + + else -> { + emptyList() + } + } + } + +private fun writeTargets(node: AstNode): List = + when (node) { + is NameExpression -> { + listOf(node) + } + + is MultipleLValueStatement, + is TupleExpression, + is ParenthesizedExpression, + -> { + node + .childNodes() + .asSequence() + .flatMap { writeTargets(it) } + .toList() + } + + else -> { + emptyList() + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt new file mode 100644 index 00000000000..dbb4245dbea --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt @@ -0,0 +1,108 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.TypedefConstant +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.ImportStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.NamedTypeExpression + +/** Worker-local syntax and resolved identities; only detached [TypeName] values survive capture. */ +internal data class CompilerTypeName( + val location: SemanticModel.SourceLocation, + val terminal: SemanticModel.Range, + val target: IdentityConstant, + val imported: Boolean, +) + +internal data class TypeName( + val location: SemanticModel.SourceLocation, + val terminal: SemanticModel.Range, + val target: ProofIdentity, + val module: String, + val path: List, + val imported: Boolean, +) + +internal data class TypePath( + val target: ProofIdentity, + val module: String, + val path: List, +) + +/** Capture written type names, including import clauses that are not expression children. */ +internal fun compilerTypeNames(nodes: List): List { + val importTokens = + nodes + .filterIsInstance() + .mapNotNull { it.source } + .distinctBy { it.fileName } + .associate { source -> + val lexer = Lexer(Source(source.toRawString()), ErrorList()) + source.fileName to generateSequence { if (lexer.hasNext()) lexer.next() else null }.toList() + } + return nodes + .mapNotNull { node -> + val source = node.source ?: return@mapNotNull null + val target = + when (node) { + is NamedTypeExpression -> node.nameBindings.lastOrNull()?.target() + is NameExpression -> node.resolvedTarget + is ImportStatement -> node.importedIdentity.takeUnless { node.isWildcard } + else -> null + } as? IdentityConstant ?: return@mapNotNull null + if (target !is ClassConstant && target !is TypedefConstant) return@mapNotNull null + val tokens = + when (node) { + is NamedTypeExpression -> { + node.nameBindings.map { it.name() } + } + + is NameExpression -> { + val chain = generateSequence(node) { it.leftExpression as? NameExpression }.toList().asReversed() + if (chain.first().leftExpression != null) return@mapNotNull null + chain.map { it.nameToken } + } + + is ImportStatement -> { + // The AST exposes import identity and spelling, but not its token list. Lex + // the source to recover positions; the compiler remains the identity authority. + val written = + importTokens + .getValue(source.fileName) + .asSequence() + .dropWhile { it.startPosition <= node.startPosition } + .takeWhile { it.startPosition < node.endPosition } + .filter { it.id == Token.Id.IDENTIFIER } + .take(node.qualifiedNameLength) + .toList() + if (written.map { it.valueText } != node.qualifiedName.toList()) return@mapNotNull null + written + } + + else -> { + return@mapNotNull null + } + } + if (tokens.isEmpty()) return@mapNotNull null + + fun range( + start: Long, + end: Long, + ) = SemanticModel.Range( + SemanticModel.Position(Source.calculateLine(start), Source.calculateOffset(start)), + SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end)), + ) + CompilerTypeName( + SemanticModel.SourceLocation(source.fileName, range(tokens.first().startPosition, tokens.last().endPosition)), + range(tokens.last().startPosition, tokens.last().endPosition), + target, + node is ImportStatement, + ) + }.distinct() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt new file mode 100644 index 00000000000..62f09b77d73 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt @@ -0,0 +1,70 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.util.ExecutionTrace + +internal data class LibraryDeclaration( + val path: List, + val range: SemanticModel.Range, + val firstLine: Int, + val lastLine: Int, +) + +/** Parse declaration spans shared by bundled and explicitly attached library sources. */ +internal fun libraryDeclarations( + text: String, + path: String, +): List { + val errors = ErrorList() + val root = + try { + ExecutionTrace.api("Parser.parseSource(library-source)") { + Parser(Source(text), errors).parseSource() + } + } catch (_: CompilerException) { + null + } + return buildList { + fun visit( + node: AstNode, + parents: List, + ) { + val token = + when (node) { + is TypeCompositionStatement -> node.nameToken + is MethodDeclarationStatement -> node.nameToken + is PropertyDeclarationStatement -> node.nameToken + else -> null + } + val module = node is TypeCompositionStatement && node.category.id == Token.Id.MODULE + val names = if (token == null || module) parents else parents + token.valueText + if (token != null) { + fun at(position: Long) = + SemanticModel.Position( + Source.calculateLine(position), + Source.calculateOffset(position), + ) + add( + LibraryDeclaration( + names, + SemanticModel.Range(at(token.startPosition), at(token.endPosition)), + Source.calculateLine(node.startPosition), + Source.calculateLine(node.endPosition), + ), + ) + } + node.childNodes().forEach { visit(it, names) } + } + if (root != null && !errors.hasSeriousErrors()) { + visit(root, path.substringBeforeLast('/', "").split('/').drop(1)) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt new file mode 100644 index 00000000000..b33d9613f88 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt @@ -0,0 +1,176 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.xdk.SemanticModel.Position +import org.xvm.lsp.adapter.xdk.SemanticModel.Range +import org.xvm.lsp.adapter.xdk.SemanticModel.Signature +import org.xvm.lsp.adapter.xdk.SemanticModel.SymbolId +import org.xvm.lsp.adapter.xdk.SemanticModel.SymbolKind +import org.xvm.lsp.adapter.xdk.SemanticModel.TypeId +import java.util.List.copyOf as immutableList + +/** + * Copied facts from one explicit partial-analysis attempt; retains no compiler or protocol objects. + */ +class PartialSemanticModel + internal constructor( + val semantics: SemanticModel, + sites: List, + syntaxCompletions: List = emptyList(), + ) { + enum class Kind { + NAME, + DECLARATION_NAME, + MEMBER_ACCESS, + CALL, + } + + /** Syntax-derived spelling policy, with no compiler type or inferred identity. */ + data class DeclarationName( + val base: String, + val basis: String, + ) + + /** A resolved upper bound or explicitly written recursive constraint, never a new identity. */ + data class Formal( + val name: String, + val constraint: TypeId?, + val declaration: Range, + val writtenConstraint: String? = null, + ) + + @ConsistentCopyVisibility + data class Member + internal constructor( + val symbol: SymbolId, + val name: String, + val kind: SymbolKind, + val type: TypeId?, + val signature: Signature?, + ) + + data class Argument( + val range: Range, + val label: String?, + val type: TypeId?, + ) + + /** Decoded identifier and its original UTF-16 replacement range; empty at an insertion slot. */ + data class MemberPrefix( + val text: String, + val range: Range, + ) + + @ConsistentCopyVisibility + data class CallCandidate + internal constructor( + val member: Member, + val arguments: List, + val converting: Boolean, + val constructor: Boolean = false, + ) + + @ConsistentCopyVisibility + data class FunctionCandidate + internal constructor( + val signature: Signature, + val arguments: List, + ) + + /** + * Members describe accessible names; callCandidates separately records compiler argument + * fitting. + */ + @ConsistentCopyVisibility + data class Site + internal constructor( + val kind: Kind, + val range: Range, + val operator: Range, + val receiver: Range?, + val receiverType: TypeId?, + val calleeName: String?, + val scope: SymbolId?, + val arguments: List, + val separators: List, + val members: List, + val memberPrefix: MemberPrefix? = null, + val callCandidates: List? = null, + val pendingArgumentName: String? = null, + val functions: List = emptyList(), + val argumentValues: List = emptyList(), + val argumentOffset: Int = 0, + val formals: List = emptyList(), + val argumentLiterals: List = emptyList(), + /** Written type spelling at an empty declaration-name slot; no resolved identity. */ + val declarationNameType: DeclarationName? = null, + val argumentExpressions: List = emptyList(), + val enclosingExpressions: List = emptyList(), + val argumentTemplates: List = emptyList(), + ) { + /** + * Source argument index only; no argument-to-parameter mapping exists for an incomplete + * call. + */ + fun argumentIndexAt(position: Position): Int? = + if (kind == Kind.CALL && position >= operator.end && position <= range.end) { + argumentOffset + separators.count { it < position } + } else { + null + } + + /** Compiler-proven mapping for a written argument, or a named/positional insertion slot. */ + fun parameterAt( + candidate: CallCandidate, + position: Position, + ): Int? = candidate.member.signature?.let { parameterAt(it, candidate.arguments, position) } + + fun parameterAt( + candidate: FunctionCandidate, + position: Position, + ): Int? = parameterAt(candidate.signature, candidate.arguments, position) + + private fun parameterAt( + signature: Signature, + mapping: List, + position: Position, + ): Int? { + val slot = argumentIndexAt(position) ?: return null + val parameters = signature.parameters + if (slot < arguments.size) { + return mapping.singleOrNull { it.range == arguments[slot].range }?.parameterIndex + } + if (pendingArgumentName != null) { + return parameters.indexOfFirst { it.name == pendingArgumentName }.takeIf { it >= 0 } + } + return slot.takeIf { it -> arguments.none { it.label != null } && it in parameters.indices } + } + + /** Expected type for this candidate, not an assertion that the overload is selected. */ + fun expectedTypeAt( + candidate: CallCandidate, + position: Position, + ): TypeId? = + parameterAt(candidate, position)?.let { + candidate.member.signature + ?.parameters + ?.getOrNull(it) + ?.type + } + + fun expectedTypeAt( + candidate: FunctionCandidate, + position: Position, + ): TypeId? = + parameterAt(candidate, position)?.let { + candidate.signature.parameters + .getOrNull(it) + ?.type + } + } + + val sites: List = immutableList(sites) + internal val syntaxCompletions: List = immutableList(syntaxCompletions) + + internal fun withSyntaxCompletions(items: List) = PartialSemanticModel(semantics, sites, items) + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt new file mode 100644 index 00000000000..bed7694c141 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt @@ -0,0 +1,566 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.SymbolMoniker +import java.util.UUID +import java.util.List.copyOf as immutableList +import java.util.Map.copyOf as immutableMap + +/** + * Immutable semantic facts copied from one compilation, with no compiler or LSP protocol objects. + * Queries can run concurrently without an ambient constant pool or error listener. IDs belong to + * this compilation snapshot; document views from one extraction share IDs. The adapter associates + * them with source versions. Missing facts are null, never inferred from spelling. + */ +class SemanticModel + internal constructor( + val id: UUID, + val status: Status, + val sourceName: String?, + private val facts: Facts, + occurrences: List, + expressions: List, + calls: List = emptyList(), + functionCalls: List = emptyList(), + imports: List = emptyList(), + lambdas: List = emptyList(), + sourceLinks: List = emptyList(), + monikers: Map = emptyMap(), + ) { + private val monikers = immutableMap(monikers) + + fun monikersAt( + line: Int, + column: Int, + ): List = listOfNotNull(symbolAt(line, column)?.id?.let(monikers::get)) + + internal fun withMonikers(values: Map): SemanticModel = + SemanticModel( + id, + status, + sourceName, + facts, + occurrences, + expressions, + calls, + functionCalls, + imports, + lambdas, + sourceLinks, + values, + ) + + enum class Status { + UNAVAILABLE, + PARTIAL, + COMPLETE, + } + + enum class SymbolKind { + MODULE, + PACKAGE, + TYPE, + METHOD, + PROPERTY, + VARIABLE, + PARAMETER, + TYPE_PARAMETER, + } + + enum class Role { + DECLARATION, + REFERENCE, + } + + enum class Usage { + READ, + WRITE, + READ_WRITE, + } + + enum class Modifier { + READONLY, + STATIC, + ABSTRACT, + } + + /** Resolved class format, including declarations supplied by binary dependencies. */ + enum class TypeCategory { + CLASS, + INTERFACE, + MIXIN, + SERVICE, + CONST, + ENUM, + ENUM_VALUE, + } + + enum class TypeForm { + NAMED, + FORMAL, + PARAMETERIZED, + IMMUTABLE, + SERVICE, + ACCESS, + ANNOTATED, + UNION, + INTERSECTION, + DIFFERENCE, + RECURSIVE, + OTHER, + } + + /** Zero-based line and UTF-16 column. */ + data class Position( + val line: Int, + val column: Int, + ) : Comparable { + init { + require(line >= 0 && column >= 0) { "Negative source position" } + } + + override fun compareTo(other: Position): Int = compareValuesBy(this, other, Position::line, Position::column) + } + + /** Half-open range within [sourceName]. */ + data class Range( + val start: Position, + val end: Position, + ) { + init { + require(start <= end) { "Reversed source range" } + } + + operator fun contains(position: Position): Boolean = position in start.. = immutableList(sourceLinks) + + /** + * Arguments are generic arguments; underlying types are a modifier's base or a relational + * type's ordered operands. Display text is for presentation, not identity or assignability. + */ + @ConsistentCopyVisibility + data class Type + internal constructor( + val id: TypeId, + val displayName: String, + val form: TypeForm, + val arguments: List, + val underlying: List, + val nullable: Boolean, + ) + + data class Parameter( + val name: String?, + val type: TypeId, + val typeParameter: Boolean, + val defaulted: Boolean, + ) + + /** Compiler-selected visible parameter slot, independent of its spelling and register. */ + data class ParameterSlot( + val method: SymbolId, + val index: Int, + ) + + /** Declared signature, including formal type parameters and conditional returns. */ + @ConsistentCopyVisibility + data class Signature + internal constructor( + val parameters: List, + val returns: List, + val conditional: Boolean, + ) + + data class Symbol( + val id: SymbolId, + val name: String, + val kind: SymbolKind, + val declaration: Range?, + val type: TypeId?, + val signature: Signature?, + val declarationSource: String?, + val modifiers: Set = emptySet(), + val inferred: Boolean = false, + val dependency: XdkDependency.SymbolKey? = null, + val renameable: Boolean = false, + val documentation: String? = null, + val typeCategory: TypeCategory? = null, + /** A static callable excluding constructors, as defined by the compiler. */ + val isFunction: Boolean = false, + /** Resolved ownership in the bundled XDK, independently of source attachments. */ + val isDefaultLibrary: Boolean = false, + /** First written header token, including modifiers/annotations; absent on generated declarations. */ + val headerStart: Position? = null, + ) + + /** A written name; a null symbol explicitly represents an unresolved occurrence. */ + data class Occurrence( + val range: Range, + val name: String, + val role: Role, + val symbol: SymbolId?, + val type: TypeId?, + val usage: Usage? = null, + /** This particular use names an annotation; ordinary uses retain their type category. */ + val annotation: Boolean = false, + ) + + data class ExpressionType( + val range: Range, + val type: TypeId, + ) + + /** Validated source lambda signature; capture parameters are absent from its function type. */ + data class LambdaSite( + val arrow: Range, + val signature: Signature, + ) + + data class CallArgument( + val range: Range, + val parameterIndex: Int, + val named: Boolean = false, + ) + + /** Selected signature after inference; parameter indices exclude hidden type parameters. */ + @ConsistentCopyVisibility + data class CallSite + internal constructor( + val range: Range, + val callee: Range, + val method: SymbolId, + val signature: Signature, + val arguments: List, + val caller: SymbolId? = null, + ) + + /** Validated function signature without an invented runtime target or parameter names. */ + @ConsistentCopyVisibility + data class FunctionCallSite + internal constructor( + val range: Range, + val callee: Range, + val signature: Signature, + val arguments: List, + ) + + /** + * Source callable boundaries, including lambdas whose compiler methods have synthetic names. + */ + data class Callable( + val symbol: SymbolId, + val location: SourceLocation, + val selection: Range, + ) + + data class Supertype( + val symbol: SymbolId, + val type: TypeId, + ) + + /** + * Direct declared extends/implements relationships, copied only from a successful compilation. + */ + data class TypeDeclaration( + val symbol: SymbolId, + val category: String, + val location: SourceLocation, + val parents: List, + ) + + /** + * Explicit local import name and the compiler-bound occurrences visible through this import. + */ + class ImportAlias( + val name: String, + val declaration: Range, + uses: List, + ) { + val uses: List = immutableList(uses) + } + + val imports: List = immutableList(imports) + + fun importAt( + line: Int, + column: Int, + ): ImportAlias? = + imports.singleOrNull { alias -> + val position = Position(line, column) + position in alias.declaration || alias.uses.any { position in it } + } + + val symbols: List = facts.symbols + val types: List = facts.types + val occurrences: List = immutableList(occurrences) + val expressions: List = immutableList(expressions) + val calls: List = immutableList(calls) + val functionCalls: List = immutableList(functionCalls) + val lambdas: List = immutableList(lambdas) + val typeDeclarations: Map = facts.typeDeclarations + val callables: Map = facts.callables + val parameters: Map = facts.parameters + + /** One immutable symbol/type table shared by every source view of the compilation. */ + internal class Facts( + symbols: Map, + types: Map, + typeDeclarations: Map = emptyMap(), + typeDefinitions: Map> = emptyMap(), + implementations: Map> = emptyMap(), + callables: Map = emptyMap(), + declarations: Map> = emptyMap(), + parameters: Map = emptyMap(), + ) { + val symbolsById: Map = immutableMap(symbols) + val typesById: Map = immutableMap(types) + val symbols: List = immutableList(symbols.values) + val types: List = immutableList(types.values) + val typeDeclarations: Map = immutableMap(typeDeclarations) + val typeDefinitions: Map> = immutableMap(typeDefinitions.mapValues { immutableList(it.value) }) + val implementations: Map> = immutableMap(implementations.mapValues { immutableList(it.value) }) + val callables: Map = immutableMap(callables) + val declarations: Map> = immutableMap(declarations.mapValues { immutableList(it.value) }) + val parameters: Map = immutableMap(parameters) + } + + /** IDs from another snapshot return no result. */ + fun symbol(id: SymbolId): Symbol? = facts.symbolsById[id] + + /** IDs from another snapshot return no result. */ + fun type(id: TypeId): Type? = facts.typesById[id] + + fun occurrenceAt( + line: Int, + column: Int, + ): Occurrence? { + val position = Position(line, column) + return occurrences + .filter { position in it.range } + .minWithOrNull(compareBy(INNERMOST) { it.range }) + } + + fun symbolAt( + line: Int, + column: Int, + ): Symbol? = occurrenceAt(line, column)?.symbol?.let(::symbol) + + /** An unresolved written name must not borrow an enclosing call's type. */ + fun typeAt( + line: Int, + column: Int, + ): Type? { + occurrenceAt(line, column)?.let { + return it.type?.let(::type) + } + val position = Position(line, column) + return expressions + .filter { position in it.range } + .minWithOrNull(compareBy(INNERMOST) { it.range }) + ?.type + ?.let(::type) + } + + fun definitionAt( + line: Int, + column: Int, + ): Range? = definitionLocationAt(line, column)?.takeIf { it.sourceName == sourceName }?.range + + /** Resolve a declaration in any source of this compilation, retaining its source identity. */ + fun definitionLocationAt( + line: Int, + column: Int, + ): SourceLocation? = + symbolAt(line, column)?.let { symbol -> + symbol.declaration?.let { SourceLocation(symbol.declarationSource, it) } + } + + /** Local aliases stay local; overrides expose their written contracts, never runtime bodies. */ + fun declarationLocationsAt( + line: Int, + column: Int, + ): List { + importAt(line, column)?.let { + return listOf(SourceLocation(sourceName, it.declaration)) + } + val symbol = symbolAt(line, column) ?: return emptyList() + return locations(facts.declarations[symbol.id].orEmpty()).ifEmpty { + locations(listOf(symbol.id)) + } + } + + /** + * Nominal types, flow-narrowed values and selected call results; never follow generic + * arguments. + */ + fun typeDefinitionLocationsAt( + line: Int, + column: Int, + ): List { + val symbol = symbolAt(line, column) + symbol + ?.takeIf { it.kind == SymbolKind.TYPE || it.kind == SymbolKind.TYPE_PARAMETER } + ?.let { + return locations(listOf(it.id)) + } + val position = Position(line, column) + val resultTypes = + if (symbol?.kind == SymbolKind.METHOD) { + val signature = + calls.firstOrNull { position in it.callee }?.signature + ?: symbol.signature.takeIf { + occurrenceAt(line, column)?.role == Role.DECLARATION + } + signature?.returns.orEmpty() + } else { + listOfNotNull(typeAt(line, column)?.id) + } + return locations(resultTypes.flatMap { facts.typeDefinitions[it].orEmpty() }) + } + + /** + * Worker inspection copies type/member implementations, including property fields and + * accessors. + */ + fun implementationLocationsAt( + line: Int, + column: Int, + ): List { + val symbol = symbolAt(line, column) ?: return emptyList() + return locations(facts.implementations[symbol.id].orEmpty()) + } + + private fun locations(ids: List): List = + ids + .mapNotNull { id -> + symbol(id)?.let { symbol -> + symbol.declaration?.let { SourceLocation(symbol.declarationSource, it) } + } + }.distinct() + + fun referencesAt( + line: Int, + column: Int, + includeDeclaration: Boolean, + ): List { + val symbol = symbolAt(line, column) ?: return emptyList() + return occurrences + .asSequence() + .filter { + it.symbol == symbol.id && (includeDeclaration || it.role != Role.DECLARATION) + }.map { it.range } + .distinct() + .sortedWith(SOURCE_ORDER) + .toList() + } + + internal companion object { + /** + * Merge detached tables after the compiler worker has proven cross-module identity aliases. + */ + fun joined( + models: List, + aliases: Map, + ): List { + fun canonical(id: SymbolId): SymbolId = aliases[id] ?: id + val tables = models.distinctBy { it.id }.map { it.facts } + val symbols = tables.flatMap { it.symbols }.associateBy { it.id } + val facts = + Facts( + symbols.values + .groupBy { canonical(it.id) } + .mapValues { (id, candidates) -> + symbols[id] ?: candidates.first().copy(id = id) + }, + tables.flatMap { it.types }.associateBy { it.id }, + tables + .flatMap { it.typeDeclarations.values } + .associate { declaration -> + val id = canonical(declaration.symbol) + id to + declaration.copy( + symbol = id, + parents = + declaration.parents.map { + it.copy(symbol = canonical(it.symbol)) + }, + ) + }, + tables + .flatMap { it.typeDefinitions.entries } + .associate { (type, ids) -> type to ids.map(::canonical).distinct() }, + tables + .flatMap { it.implementations.entries } + .groupBy({ canonical(it.key) }, { it.value }) + .mapValues { (_, groups) -> groups.flatten().map(::canonical).distinct() }, + tables + .flatMap { it.callables.values } + .associate { callable -> + val id = canonical(callable.symbol) + id to callable.copy(symbol = id) + }, + tables + .flatMap { it.declarations.entries } + .groupBy({ canonical(it.key) }, { it.value }) + .mapValues { (_, groups) -> groups.flatten().map(::canonical).distinct() }, + tables + .flatMap { it.parameters.entries } + .associate { (id, slot) -> + canonical(id) to slot.copy(method = canonical(slot.method)) + }, + ) + // Each source view shares its compilation's identity table, just like other facts. + // Copying the whole module table per companion would retain files × symbols entries. + val monikersBySnapshot = + models.distinctBy { it.id }.associate { model -> + model.id to immutableMap(model.monikers.mapKeys { canonical(it.key) }) + } + return models.map { model -> + SemanticModel( + model.id, + model.status, + model.sourceName, + facts, + model.occurrences.map { it.copy(symbol = it.symbol?.let(::canonical)) }, + model.expressions, + model.calls.map { + it.copy(method = canonical(it.method), caller = it.caller?.let(::canonical)) + }, + model.functionCalls, + model.imports, + lambdas = model.lambdas, + sourceLinks = model.sourceLinks, + monikers = monikersBySnapshot.getValue(model.id), + ) + } + } + + private val SOURCE_ORDER = compareBy(Range::start, Range::end) + private val INNERMOST = compareByDescending(Range::start).thenBy(Range::end) + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModelBuilder.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModelBuilder.kt new file mode 100644 index 00000000000..3286bdad805 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModelBuilder.kt @@ -0,0 +1,2089 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.Argument +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component.Composition +import org.xvm.asm.Component.Format +import org.xvm.asm.Constant +import org.xvm.asm.ConstantPool +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.Op +import org.xvm.asm.PackageStructure +import org.xvm.asm.PropertyStructure +import org.xvm.asm.Register +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.DeferredValueConstant +import org.xvm.asm.constants.FormalConstant +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodConstant +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.PseudoConstant +import org.xvm.asm.constants.SignatureConstant +import org.xvm.asm.constants.SingletonConstant +import org.xvm.asm.constants.TypeConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.asm.constants.TypedefConstant +import org.xvm.compiler.CursorBinding +import org.xvm.compiler.InvocationBinding +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotationExpression +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.ComponentStatement +import org.xvm.compiler.ast.CompositionNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.LabeledExpression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.NewExpression +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypeExpression +import org.xvm.compiler.ast.TypedefStatement +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.compiler.ast.VariableTypeExpression +import org.xvm.compiler.ast.partial.IncompleteLocalDeclaration +import org.xvm.compiler.ast.partial.IncompleteStatement +import org.xvm.lsp.adapter.xdk.SemanticModel.ExpressionType +import org.xvm.lsp.adapter.xdk.SemanticModel.Occurrence +import org.xvm.lsp.adapter.xdk.SemanticModel.Position +import org.xvm.lsp.adapter.xdk.SemanticModel.Range +import org.xvm.lsp.adapter.xdk.SemanticModel.Role +import org.xvm.lsp.adapter.xdk.SemanticModel.Signature +import org.xvm.lsp.adapter.xdk.SemanticModel.SourceLocation +import org.xvm.lsp.adapter.xdk.SemanticModel.Status +import org.xvm.lsp.adapter.xdk.SemanticModel.Symbol +import org.xvm.lsp.adapter.xdk.SemanticModel.SymbolId +import org.xvm.lsp.adapter.xdk.SemanticModel.SymbolKind +import org.xvm.lsp.adapter.xdk.SemanticModel.Type +import org.xvm.lsp.adapter.xdk.SemanticModel.TypeCategory +import org.xvm.lsp.adapter.xdk.SemanticModel.TypeForm +import org.xvm.lsp.adapter.xdk.SemanticModel.TypeId +import org.xvm.lsp.util.ExecutionTrace +import java.util.Collections +import java.util.IdentityHashMap +import java.util.UUID +import kotlin.jvm.optionals.getOrNull +import java.util.List.copyOf as immutableList +import java.util.Set.copyOf as immutableSet + +/** + * Copy facts while the calling thread exclusively owns the compilation. This does not resume + * validation or build TypeInfo. The result holds no compiler objects; each call creates a new + * identity domain, so a host normally keeps one snapshot per document version. + */ +fun EmbeddingSupport.Compilation.semanticSnapshot(): SemanticModel { + val documents = semanticSnapshots() + require(documents.size == 1) { + "Use semanticSnapshots() for a compilation containing multiple sources" + } + return documents.single() +} + +/** Copy all source views together, sharing symbol/type IDs within this compilation only. */ +fun EmbeddingSupport.Compilation.semanticSnapshots(): List = + ExecutionTrace.api("Compilation.semanticSnapshots") { + val builder = SemanticModelBuilder() + val pool = pool() ?: return@api builder.build(this) + return@api ConstantPool.withPool(pool).use { builder.build(this) } + } + +/** + * Explicit compiler-worker inspection of implementation chains, reporting through the host + * listener. + */ +fun EmbeddingSupport.Compilation.semanticSnapshots(errors: ErrorListener): List = + ExecutionTrace.api("Compilation.semanticSnapshots(type-info)") { + val builder = SemanticModelBuilder() + val pool = pool() ?: return@api builder.build(this) + return@api ConstantPool.withPool(pool).use { builder.build(this, errors) } + } + +/** + * Bindings for local renames or partial repairs; never resume failed validation or inspect + * TypeInfo. + */ +internal fun EmbeddingSupport.Compilation.renameFacts( + dependencies: XdkDependencies.Open, + includeMissingMethods: Boolean = false, + missingDestinations: Map = emptyMap(), +): CompilerRenameFacts = + ExecutionTrace.api("Compilation.renameFacts") { + ConstantPool.withPool(pool()).use { + val builder = + SemanticModelBuilder( + dependencies.declarations.filterKeys { it.moduleConstant != file()?.moduleId }, + ) + captureRenameFacts( + builder.build(this), + builder.constantBindings(), + dependencies, + supers = builder.superBindings(), + receivers = builder.receiverBindings(), + missingInputs = + if (includeMissingMethods) { + missingMethodInputs( + dependencies.missingMethodDestinations(missingDestinations), + ) + } else { + CompilerMissingInputs() + }, + ) + } + } + +/** Successful emission and detached semantic facts from a single extraction. */ +internal class CompiledSemantics( + val models: List, + val navigation: XdkNavigationIndex.Facts?, + val artifact: XdkDependency?, +) + +/** Emit once, then associate copied bindings with artifact identities only when compilation succeeds. */ +internal fun EmbeddingSupport.Compilation.compiledSemantics( + dependencies: XdkDependencies.Open, + errors: ErrorListener, + navigation: Boolean = false, +): CompiledSemantics = + ExecutionTrace.api("Compilation.compiledSemantics") { + ConstantPool.withPool(pool()).use { + val builder = + SemanticModelBuilder( + dependencies.declarations.filterKeys { it.moduleConstant != file()?.moduleId }, + ) + val models = builder.build(this, errors) + val bindings = builder.constantBindings() + val facts = + if (navigation && succeeded() && !errors.hasSeriousErrors()) { + captureRenameFacts( + models, + bindings, + dependencies, + errors = errors, + supers = builder.superBindings(), + receivers = builder.receiverBindings(), + ) + } else { + null + } + val artifact = + if (succeeded() && !errors.hasSeriousErrors()) { + XdkDependency.capture(this, builder.declarations()) + } else { + null + } + val monikers = artifact?.let { XdkMonikers.capture(it, bindings, dependencies) }.orEmpty() + val exported = models.map { it.withMonikers(monikers) } + CompiledSemantics( + exported, + facts?.let { XdkNavigationIndex.Facts(exported, it.constants) }, + artifact, + ) + } + } + +/** Graph proof facts retain dependency declaration associations and actual dispatch chains. */ +internal fun EmbeddingSupport.Compilation.projectRenameFacts( + dependencies: XdkDependencies.Open, + errors: ErrorListener, + includeMembers: Boolean = false, +): CompilerRenameFacts = + ExecutionTrace.api("Compilation.projectRenameFacts") { + ConstantPool.withPool(pool()).use { + val builder = + SemanticModelBuilder( + dependencies.declarations.filterKeys { it.moduleConstant != file()?.moduleId }, + ) + val models = builder.build(this, errors) + captureRenameFacts( + models, + builder.constantBindings(), + dependencies, + builder.methodRelations(this, errors), + builder.propertyRelations(this, errors), + errors, + builder.superBindings(), + if (includeMembers) builder.memberActions(this, errors) else emptyList(), + builder.receiverBindings(), + builder.typeNames(this), + builder.resourceValues(this), + builder.callableBindings(this), + if (includeMembers) builder.removableLocals(this) else emptySet(), + if (includeMembers) builder.extractionFacts(this) else null, + constantProperties = if (includeMembers) builder.constantProperties(this) else emptySet(), + ) + } + } + +/** + * Inspect only a fresh, successfully resolved declaration attempt; never resume a failed compiler. + */ +internal fun EmbeddingSupport.DeclarationAnalysis.memberActionFacts( + dependencies: XdkDependencies.Open, + errors: ErrorListener, + missingMethodInputs: XdkMissingMethods.Inputs = XdkMissingMethods.Inputs(), + missingDestinations: Map = emptyMap(), +): CompilerRenameFacts = + ExecutionTrace.api("DeclarationAnalysis.memberActionFacts") { + ConstantPool.withPool(pool()).use { + val builder = + SemanticModelBuilder( + dependencies.declarations.filterKeys { it.moduleConstant != file().moduleId }, + ) + builder.declarationFacts(this, dependencies, errors, missingMethodInputs, missingDestinations) + } + } + +/** + * Export only successful attempts, atomically pairing emitted bytes with their own source spans. + */ +fun EmbeddingSupport.Compilation.toDependency(): XdkDependency = + ExecutionTrace.api("Compilation.toDependency") { + require(succeeded()) { "A dependency artifact requires successful compilation" } + return@api ConstantPool.withPool(pool()).use { + val builder = SemanticModelBuilder() + builder.build(this) + XdkDependency.capture(this, builder.declarations()) + } + } + +internal fun EmbeddingSupport.Compilation.semanticSnapshots( + errors: ErrorListener, + dependencies: XdkDependencies.Open, +): List = + ExecutionTrace.api("Compilation.semanticSnapshots(dependencies)") { + ConstantPool.withPool(pool()).use { + SemanticModelBuilder( + dependencies.declarations.filterKeys { it.moduleConstant != file()?.moduleId }, + ).build(this, errors) + } + } + +/** + * Explicit worker-only member inspection followed by copying. Unlike ordinary semanticSnapshot, + * this may build receiver TypeInfo and report through [errors]. Never call it on a request thread + * or concurrently with another compilation using the same repository. + */ +fun EmbeddingSupport.PartialAnalysis.semanticSnapshot(errors: ErrorListener): PartialSemanticModel = + ExecutionTrace.api("PartialAnalysis.semanticSnapshot") { + val builder = SemanticModelBuilder() + if (errors.isAbortDesired) { + return@api PartialSemanticModel(builder.unavailable(), emptyList()) + } + val pool = + pool() + ?: return@api PartialSemanticModel(builder.unavailable(), emptyList()) + return@api ConstantPool.withPool(pool).use { builder.buildPartial(this, errors) } + } + +/** Compiler-worker extraction. All mutable state dies with the builder. */ +private class SemanticModelBuilder( + private val dependencies: Map = emptyMap(), +) { + private val id = UUID.randomUUID() + private val captureOrigins = IdentityHashMap() + private val capturedProperties = mutableMapOf() + private val registers = IdentityHashMap() + private val constants = linkedMapOf() + private val symbols = linkedMapOf() + private val typeIds = linkedMapOf() + private val types = linkedMapOf() + private val occurrences = linkedMapOf() + private val expressions = linkedMapOf() + private val callees = IdentityHashMap() + private val calls = linkedMapOf() + private val functionCalls = linkedMapOf() + private val lambdaSites = linkedMapOf() + private val callables = linkedMapOf() + private val callableNodes = IdentityHashMap() + private val parameters = mutableMapOf, SymbolId>() + private val supers = mutableMapOf() + + fun declarationFacts( + analysis: EmbeddingSupport.DeclarationAnalysis, + dependencies: XdkDependencies.Open, + errors: ErrorListener, + missingMethodInputs: XdkMissingMethods.Inputs, + missingDestinations: Map, + ): CompilerRenameFacts { + val nodes = nodesIn(analysis.ast()) + collect(nodes, emptyMap(), emptyMap(), analysis.pool()) + return captureRenameFacts( + finish(nodes, false, emptyMap(), emptyMap()), + constantBindings(), + dependencies, + compilerMethodRelations(nodes, errors), + compilerPropertyRelations(nodes, errors), + errors, + members = compilerMemberActions(nodes, errors), + missingMethods = + compilerMissingMethods( + nodes, + errors, + missingMethodInputs, + dependencies.missingMethodDestinations(missingDestinations), + ), + ) + } + + fun constantBindings(): Map = constants.entries.associate { (constant, id) -> id to constant } + + fun superBindings(): Map = supers.toMap() + + fun receiverBindings(): Map = + registers.entries + .mapNotNull { (register, id) -> + if (register.index !in setOf(Op.A_THIS, Op.A_PUBLIC, Op.A_PROTECTED, Op.A_PRIVATE, Op.A_STRUCT)) return@mapNotNull null + val type = register.type + if (!copyableType(type) || !type.isSingleUnderlyingClass(false)) return@mapNotNull null + id to CompilerReceiver(type.getSingleUnderlyingClass(false), register.index) + }.toMap() + + /** A selected declaration alone loses union alternatives and recursive receiver provenance. */ + fun callableBindings(compilation: EmbeddingSupport.Compilation): Map> = + nodesIn(requireNotNull(compilation.parsed())) + .filterIsInstance() + .flatMap { node -> + val method = (callees[node] ?: node.resolvedTarget) as? MethodConstant ?: return@flatMap emptyList() + val receiver = node.leftExpression?.let(::validatedType) ?: return@flatMap emptyList() + if (receiver.isTypeOfType) return@flatMap emptyList() + val selected = receiver to method + listOf( + location(node.source, node.nameToken.startPosition, node.nameToken.endPosition) to selected, + location(node.source, node.startPosition, node.endPosition) to selected, + ) + }.toMap() + + fun methodRelations( + compilation: EmbeddingSupport.Compilation, + errors: ErrorListener, + ): CompilerMethodRelations = compilerMethodRelations(nodesIn(requireNotNull(compilation.parsed())), errors) + + fun propertyRelations( + compilation: EmbeddingSupport.Compilation, + errors: ErrorListener, + ): CompilerPropertyRelations = compilerPropertyRelations(nodesIn(requireNotNull(compilation.parsed())), errors) + + fun memberActions( + compilation: EmbeddingSupport.Compilation, + errors: ErrorListener, + ): List = compilerMemberActions(nodesIn(requireNotNull(compilation.parsed())), errors) + + fun declarations(): Map = + constants.entries + .mapNotNull { (constant, id) -> + val identity = constant as? IdentityConstant ?: return@mapNotNull null + val symbol = symbols.getValue(id) + val range = symbol.declaration ?: return@mapNotNull null + if (symbol.declarationSource == null || symbol.dependency != null) { + return@mapNotNull null + } + identity to SourceLocation(symbol.declarationSource, range) + }.toMap() + + fun unavailable(): SemanticModel = + SemanticModel( + id, + Status.UNAVAILABLE, + null, + SemanticModel.Facts(symbols, types), + emptyList(), + emptyList(), + ) + + fun build( + compilation: EmbeddingSupport.Compilation, + errors: ErrorListener? = null, + ): List { + val root = compilation.parsed() ?: return listOf(unavailable()) + val nodes = nodesIn(root) + collect( + nodes, + compilation.callBindings(), + compilation.functionBindings(), + compilation.pool(), + compilation.succeeded(), + ) + compilation.initializerBindings().forEach { (owner, binding) -> + binding.references().forEach { reference -> + val span = reference.span() + val at = location(owner.source, span.startPosition(), span.endPosition()) + if (occurrences[at]?.symbol == null) { + occurrences.remove(at) + refer(reference.name(), at, reference.target(), reference.type()) + } + } + binding.expressions().forEach { expression -> + val span = expression.span() + type(expression.type())?.let { resolved -> + expressions[location(owner.source, span.startPosition(), span.endPosition())] = + resolved + } + } + binding.calls().forEach { call -> + copyCall( + owner.source, + location(owner.source, call.span().startPosition(), call.span().endPosition()), + location( + owner.source, + call.callee().startPosition(), + call.callee().endPosition(), + ).range, + call.binding(), + callableNodes[owner], + call.construction(), + ) + } + } + compilation.constructorBindings().forEach { (node, binding) -> + copyConstruction(node, binding) + } + val implementations = + if (compilation.succeeded() && errors != null) { + compilerImplementationTargets(nodes, errors) + } else { + emptyMap() + } + // An inherited accessor need not appear in a written call or the consumer's constant table. + // Inspect its linked compiler identity, never the unlinked artifact used as the index key. + implementations.values + .flatten() + .filter { it in dependencies } + .forEach { implementation -> + symbol(implementation, implementation.name, kind(implementation)) + } + val declarations = + if (compilation.succeeded() && errors != null) { + compilerDeclarationTargets(nodes, errors) + } else { + emptyMap() + } + (declarations.keys + declarations.values.flatten()).forEach { identity -> + symbol(identity, identity.name, kind(identity)) + } + return finish(nodes, compilation.succeeded(), implementations, declarations) + } + + private fun collect( + nodes: List, + bindings: Map, + functions: Map, + pool: ConstantPool?, + complete: Boolean = false, + ) { + dependencies.forEach { (identity, declaration) -> + // Artifact identities supply source associations, not semantic metadata: their pools + // have not linked core libraries. Read only the corresponding consumer-owned constant. + val linked = pool?.getConstant(identity) as? IdentityConstant ?: return@forEach + symbol(linked, linked.name, kind(linked), declaration.location) + } + nodes.filterIsInstance().forEach { + capturedProperties.putAll(it.captureOrigins) + it.sourceBindings?.let { bindings -> captureOrigins.putAll(bindings.captureOrigins) } + } + + val lambdas = nodes.filterIsInstance() + lambdas.forEach { + it.sourceBindings?.let { bindings -> captureOrigins.putAll(bindings.captureOrigins) } + } + lambdas.forEach { lambda -> + lambda.sourceBindings?.parameters?.forEach { binding -> + declare(binding.name(), binding.register(), SymbolKind.PARAMETER, lambda.source) + (normalized(binding.register()) as? Register)?.let(registers::get)?.let { id -> + symbols[id]?.let { + // Function types expose positional arguments, not the lambda's written + // names. + // The original register/source binding remains stable through nested + // captures. + symbols[id] = + it.copy( + inferred = + lambda.hasOnlyParamNames() && validatedType(lambda) != null, + renameable = complete, + ) + } + } + } + validatedType(lambda)?.let(::functionSignature)?.let { signature -> + val arrow = lambda.operator + val at = location(lambda.source, arrow.startPosition, arrow.endPosition) + lambdaSites[at] = SemanticModel.LambdaSite(at.range, signature) + } + } + // Parameters precede synthetic properties that share their source tokens. + nodes.filterIsInstance().forEach { + // Conditional incorporation names refer to the mixin's registered formals. Their + // parser representation is a Parameter, but they do not declare another formal. + if (it.parent is CompositionNode.Incorporates) return@forEach + val method = (it.parent as? MethodDeclarationStatement)?.component as? MethodStructure + val parameter = method?.params?.singleOrNull { parameter -> parameter.name == it.name } + if (it.resolvedTarget == null && method != null && parameter != null) { + // A bodyless method has no register. Its written parameter is still a real source + // declaration, identified by its resolved signature slot and original source span. + val at = location(it.source, it.nameToken.startPosition, it.nameToken.endPosition) + val symbol = SymbolId(id, symbols.size) + val kind = + if (parameter.isTypeParameter) { + SymbolKind.TYPE_PARAMETER + } else { + SymbolKind.PARAMETER + } + symbols[symbol] = + Symbol( + symbol, + it.name, + kind, + at.range, + type(parameter.type), + null, + at.sourceName, + ) + occurrences[at] = + Occurrence( + at.range, + it.name, + Role.DECLARATION, + symbol, + symbols.getValue(symbol).type, + ) + parameters[method.identityConstant to (parameter.index - method.typeParamCount)] = + symbol + return@forEach + } + declare( + it.nameToken, + it.resolvedTarget, + if (it.resolvedTarget is Register) { + SymbolKind.PARAMETER + } else { + kind(it.resolvedTarget) + }, + it.source, + ) + val register = normalized(it.resolvedTarget) as? Register + val id = register?.let(registers::get) + if (method != null && register != null && id != null) { + parameters[method.identityConstant to (register.index - method.typeParamCount)] = id + } + } + nodes.forEach { node -> + when (node) { + is VariableDeclarationStatement -> { + declare(node.nameToken, node.register, SymbolKind.VARIABLE, node.source) + (normalized(node.register) as? Register)?.let(registers::get)?.let { id -> + symbols[id] + ?.takeIf { it.kind == SymbolKind.VARIABLE } + ?.let { symbols[id] = it.copy(renameable = true) } + } + if (nodesIn(node).any { it is VariableTypeExpression }) { + (normalized(node.register) as? Register)?.let(registers::get)?.let { id -> + symbols[id]?.let { symbols[id] = it.copy(inferred = true) } + } + } + } + + is MethodDeclarationStatement -> { + declare(node.nameToken, identity(node), SymbolKind.METHOD, node.source) + identity(node)?.let(constants::get)?.let { id -> + symbols[id]?.let { + symbols[id] = it.copy(documentation = node.documentation?.trim()) + } + } + } + + is PropertyDeclarationStatement -> { + declare(node.nameToken, identity(node), SymbolKind.PROPERTY, node.source) + } + + is TypeCompositionStatement -> { + // Anonymous classes borrow the constructed type's span for their generated + // name. Keep their identity, but never present that span as a declaration: + // Packet already contains the written Packet and String references. + if (node.parent is NewExpression) { + symbol(identity(node), node.name, SymbolKind.TYPE) + } else { + declare(node.nameToken, identity(node), kind(identity(node)), node.source) + } + } + + is TypedefStatement -> { + declare(node.nameToken, identity(node), SymbolKind.TYPE, node.source) + } + } + if (node is InvocationExpression) { + val callee = node.invokedExpression + val method = bindings[node]?.method() ?: node.resolvedMethod + if (callee is NameExpression && method != null) callees[callee] = method + } + recordHeaderStart(node) + } + nodes.forEach { node -> + when (node) { + is MethodDeclarationStatement -> { + identity(node)?.let(constants::get)?.let { callable(node, it) } + } + + is LambdaExpression -> { + val method = node.lambda ?: return@forEach + val at = location(node.source, node.startPosition, node.startPosition) + symbol(method.identityConstant, "", SymbolKind.METHOD, at)?.let { + callable(node, it) + } + } + } + } + val writes = + compilerWrites(nodes).associate { (name, usage) -> + location(name.source, name.nameToken.startPosition, name.nameToken.endPosition) to + usage + } + nodes.forEach { node -> + val expressionType = validatedType(node as? Expression) + type(expressionType)?.let { + expressions[location(node.source, node.startPosition, node.endPosition)] = it + } + when (node) { + is Parameter -> { + val incorporation = node.parent as? CompositionNode.Incorporates ?: return@forEach + val type = incorporation.contribution?.typeConstant + val owner = + type + ?.takeIf { it.isSingleUnderlyingClass(false) } + ?.getSingleUnderlyingClass(false) + ?.component as? ClassStructure + val formal = (owner?.getChild(node.name) as? PropertyStructure)?.takeIf { it.isGenericTypeParameter } + refer(node.nameToken, formal?.identityConstant, formal?.type, node.source) + } + + is InvocationExpression -> { + bindings[node]?.let { copyCall(node, it) } + functions[node]?.let { copyFunctionCall(node, it) } + } + + is NameExpression -> { + val at = + location( + node.source, + node.nameToken.startPosition, + node.nameToken.endPosition, + ) + // A simple delegate clause is validated through its composition property, + // not by validating this NameExpression. Use the compiler-selected identity. + val delegate = + if (complete && node.isSimpleName) { + (node.parent as? CompositionNode.Delegates) + ?.takeIf { it.delegatee === node } + ?.contribution + ?.delegatePropertyConstant + } else { + null + } + val target = callees[node] ?: node.resolvedTarget ?: delegate + refer(node.nameToken, target, expressionType, node.source, writes[at]) + if (complete && target is Register && target.isSuper) { + val method = + generateSequence(node.parent) { it.parent } + .filterIsInstance() + .firstOrNull() + ?.component as? MethodStructure + val symbol = occurrences[at]?.symbol + if (method != null && symbol != null) { + supers[symbol] = method.identityConstant + } + } + } + + is NamedTypeExpression -> { + // Module import names bypass ordinary type-name resolution. Their authoritative + // identity is the package's linked imported module, not a spelling lookup. + val imported = + (node.parent as? CompositionNode.Import)?.let { + ( + (it.parent as? TypeCompositionStatement)?.component + as? PackageStructure + )?.importedModule + ?.identityConstant + } + node.nameBindings.forEach { + refer( + it.name(), + it.target() ?: imported, + expressionType.takeIf { _ -> it.name() === node.nameToken }, + node.source, + annotation = + node.parent is AnnotationExpression && it.name() === node.nameToken && + (normalized(it.target()) as? ClassConstant)?.component?.format == Format.ANNOTATION, + ) + } + } + } + } + parameters.forEach { (parameter, id) -> + val method = parameter.first.component as? MethodStructure ?: return@forEach + // Calls through function values are positional (COMPILER-141 rejects named arguments). + if (method.access == Access.PRIVATE && !method.isConstructor) { + symbols[id]?.let { symbols[id] = it.copy(renameable = true) } + } + } + } + + private fun finish( + nodes: List, + complete: Boolean, + implementations: Map> = emptyMap(), + declarations: Map> = emptyMap(), + ): List { + val hierarchy = if (complete) hierarchy(nodes) else emptyMap() + val sourceLinks = + if (complete) { + compilerSourceLinks(nodes) { identity -> + val target = symbol(identity, identity.name, kind(identity))?.let(symbols::get) + target?.declaration?.let { SourceLocation(target.declarationSource, it) } + } + } else { + emptyMap() + } + val parameterSlots = + parameters.entries + .filter { it.key.second >= 0 } + .mapNotNull { (binding, id) -> + // Partial signatures can have unresolved types: preserve their written symbols + // without claiming a validated callable slot for rename. + symbol(binding.first, binding.first.name, SymbolKind.METHOD)?.let { owner -> + id to SemanticModel.ParameterSlot(owner, binding.second) + } + }.toMap() + // Inferred/narrowed types can name a nominal type that never occurs in written source. + // Intern those declarations before freezing the tables. Their own declared types may + // introduce further IDs; process each once without iterating a map being mutated. + val definitionTargets = + buildMap { + while (true) { + val pending = typeIds.filterValues { it !in this } + if (pending.isEmpty()) break + pending.forEach { (constant, id) -> put(id, typeDefinitions(constant)) } + } + } + val facts = + SemanticModel.Facts( + symbols = symbols, + types = types, + typeDeclarations = hierarchy, + typeDefinitions = definitionTargets, + implementations = + implementations.entries + .mapNotNull { (target, implementations) -> + constants[target]?.let { + it to implementations.mapNotNull(constants::get) + } + }.toMap(), + callables = callables, + parameters = parameterSlots, + declarations = + declarations.entries + .mapNotNull { (target, contracts) -> + constants[target]?.let { it to contracts.mapNotNull(constants::get) } + }.toMap(), + ) + return immutableList( + nodes + .map { it.source?.fileName } + .distinct() + .map { source -> + SemanticModel( + id = id, + status = if (complete) Status.COMPLETE else Status.PARTIAL, + sourceName = source, + facts = facts, + occurrences = + occurrences + .filterKeys { it.sourceName == source } + .values + .sortedBy { it.range.start }, + expressions = + expressions + .filterKeys { it.sourceName == source } + .map { (location, type) -> + ExpressionType(location.range, type) + }, + calls = + calls + .filterKeys { it.sourceName == source } + .values + .sortedBy { it.range.start }, + functionCalls = + functionCalls + .filterKeys { it.sourceName == source } + .values + .sortedBy { it.range.start }, + imports = + if (complete) { + compilerImportAliases(nodes, source, occurrences, constants) + } else { + emptyList() + }, + lambdas = + if (complete) { + lambdaSites.filterKeys { it.sourceName == source }.values.toList() + } else { + emptyList() + }, + sourceLinks = sourceLinks[source].orEmpty(), + ) + }, + ) + } + + private fun copyCall( + node: InvocationExpression, + binding: InvocationBinding, + ) { + copyCall( + node.source, + location(node.source, node.startPosition, node.endPosition), + location( + node.source, + node.invokedExpression.startPosition, + node.invokedExpression.endPosition, + ).range, + binding, + caller(node), + ) + } + + private fun caller(node: AstNode): SymbolId? = + generateSequence(node.parent) { it.parent } + .firstOrNull { + it in callableNodes || + it is PropertyDeclarationStatement || + it is TypeCompositionStatement + }?.let(callableNodes::get) + + private fun copyCall( + source: Source?, + site: SourceLocation, + callee: Range, + binding: InvocationBinding, + caller: SymbolId?, + construction: Boolean = false, + ) { + val method = binding.method().component as? MethodStructure ?: return + val target = symbol(binding.method(), method.name, SymbolKind.METHOD) ?: return + binding.arguments().forEach { argument -> + val label = argument.label() ?: return@forEach + val parameter = parameter(method, argument.parameterIndex()) ?: return@forEach + val at = location(source, label.startPosition(), label.endPosition()) + occurrences[at] = + Occurrence( + at.range, + label.name(), + Role.REFERENCE, + parameter, + symbols[parameter]?.type, + ) + } + // Synthetic shorthand constructors have compiler-proven identity, but no invented + // declaration. + if ( + construction && + symbols[target]?.declaration == null && + !(method.isSynthetic && method.isShorthandConstructor) + ) { + return + } + val selected = signature(method, binding.signature(), visibleOnly = true) ?: return + calls[site] = + SemanticModel.CallSite( + range = site.range, + callee = callee, + method = target, + signature = selected, + arguments = + immutableList( + binding.arguments().map { + SemanticModel.CallArgument( + location(source, it.startPosition(), it.endPosition()).range, + it.parameterIndex(), + it.named(), + ) + }, + ), + caller = caller, + ) + } + + private fun parameter( + method: MethodStructure, + index: Int, + ): SymbolId? { + method.primaryProperty(index)?.let { property -> + return symbol(property, property.name, SymbolKind.PROPERTY) + } + val value = method.params.getOrNull(index + method.typeParamCount) ?: return null + return parameters.getOrPut(method.identityConstant to index) { + val id = SymbolId(this.id, symbols.size) + symbols[id] = + Symbol(id, value.name, SymbolKind.PARAMETER, null, type(value.type), null, null) + id + } + } + + private fun copyConstruction( + node: NewExpression, + binding: InvocationBinding, + ) { + val at = location(node.source, node.startPosition, node.endPosition) + copyCall(node.source, at, at.range, binding, caller(node), construction = true) + } + + private fun copyFunctionCall( + node: InvocationExpression, + binding: InvocationBinding.FunctionCall, + ) { + val signature = functionSignature(binding.type()) ?: return + val at = location(node.source, node.startPosition, node.endPosition) + val callee = node.invokedExpression + functionCalls[at] = + SemanticModel.FunctionCallSite( + range = at.range, + callee = location(node.source, callee.startPosition, callee.endPosition).range, + signature = signature, + arguments = + immutableList( + binding.arguments().map { + SemanticModel.CallArgument( + location(node.source, it.startPosition(), it.endPosition()).range, + it.parameterIndex(), + ) + }, + ), + ) + } + + private fun functionSignature(function: TypeConstant): Signature? { + val pool = function.constantPool + val parameters = pool.extractFunctionParams(function) ?: return null + val returns = pool.extractFunctionReturns(function) ?: return null + return Signature( + immutableList( + parameters.map { + SemanticModel.Parameter( + name = null, + type = type(it) ?: return null, + typeParameter = false, + defaulted = false, + ) + }, + ), + immutableList(returns.map { type(it) ?: return null }), + false, + ) + } + + private fun callable( + node: AstNode, + id: SymbolId, + ) { + val selection = symbols[id]?.declaration ?: return + val source = location(node.source, node.startPosition, node.endPosition) + if (selection.start < source.range.start || selection.end > source.range.end) return + callableNodes[node] = id + callables[id] = SemanticModel.Callable(id, source, selection) + } + + private fun validatedType(expression: Expression?): TypeConstant? = expression?.takeIf { it.isValidated && it.typeFit.isFit }?.type + + private fun sourceVariable(variable: CursorBinding.Variable): PartialSemanticModel.Member? { + val id = symbol(variable.register(), variable.name(), SymbolKind.VARIABLE) ?: return null + return PartialSemanticModel.Member( + id, + variable.name(), + symbols.getValue(id).kind, + type(variable.type()), + null, + ) + } + + private fun sourceProperty(property: CursorBinding.Property): PartialSemanticModel.Member? { + val id = + symbol(property.identity(), property.name(), kind(property.identity())) ?: return null + return PartialSemanticModel.Member( + id, + property.name(), + symbols.getValue(id).kind, + type(property.type()), + null, + ) + } + + fun buildPartial( + analysis: EmbeddingSupport.PartialAnalysis, + errors: ErrorListener, + ): PartialSemanticModel { + val source = + analysis.sites().singleOrNull()?.source + ?: return PartialSemanticModel(unavailable(), emptyList()) + val nodes = nodesIn(analysis.sourceTrees()) + collect( + nodes, + analysis.callBindings(), + analysis.functionBindings(), + analysis.pool(), + ) + val sites = + analysis.sites().map { site -> + val operation: IncompleteStatement = site.argumentCall.getOrNull() ?: site + val parents = generateSequence(site.parent) { it.parent }.toList() + val owner = + parents.filterIsInstance().firstOrNull()?.component + as? ClassStructure + val scope = + parents + .filterIsInstance() + .firstOrNull() + ?.let(::identity) + ?.let { constants[it] } + val receiver = site.receiver.orElse(null) + val receiverType = validatedType(receiver) + val cursor = analysis.cursorBindings()[site] + val callFacts = cursor?.callFacts() + val identity = (receiver as? NameExpression)?.resolvedTarget + val staticType = + when (identity) { + is ClassConstant -> identity.type + is TypedefConstant -> identity.referredToType + else -> null + } + val lookupKind = + when { + identity is SingletonConstant -> Lookup.IMPLICIT + staticType != null -> Lookup.STATIC + else -> Lookup.INSTANCE + } + val callee = (operation.target as? NameExpression)?.name.takeIf { operation.isCall } + val locals = + cursor + ?.variables() + .orEmpty() + .filter { it.readable() } + .mapNotNull(::sourceVariable) + val scopeMembers = + if ( + cursor != null && + owner != null && + !site.isTypeCompletion && + (site.isNameCompletion || receiver == null) + ) { + receiverMembers( + cursor.thisType(), + owner, + errors, + if (cursor.instance()) Lookup.IMPLICIT else Lookup.STATIC, + ).filter { member -> + cursor.variables().none { it.name() == member.name } + } + } else { + emptyList() + } + val scopeTypes = + cursor?.types().orEmpty().mapNotNull { named -> + val id = + symbol(named.identity(), named.name(), kind(named.identity())) + ?: return@mapNotNull null + PartialSemanticModel.Member( + id, + named.name(), + symbols[id]!!.kind, + type(named.type()), + null, + ) + } + val members = + if (site.isTypeCompletion) { + scopeTypes + } else if (site.isNameCompletion) { + locals.filter { local -> scopeTypes.none { it.symbol == local.symbol } } + + scopeMembers.filter { member -> + scopeTypes.none { it.name == member.name } + } + + scopeTypes + } else if (site.isCall && receiver == null) { + scopeMembers.filter { it.kind == SymbolKind.METHOD && it.name == callee } + } else if (receiverType != null && owner != null && !errors.isAbortDesired) { + receiverMembers( + staticType ?: receiverType, + owner, + errors, + lookupKind, + ).filter { + !site.isCall || (it.kind == SymbolKind.METHOD && it.name == callee) + } + } else { + emptyList() + } + PartialSemanticModel.Site( + kind = + when { + site.declarationType.isPresent -> PartialSemanticModel.Kind.DECLARATION_NAME + operation.isCall -> PartialSemanticModel.Kind.CALL + site.isNameCompletion -> PartialSemanticModel.Kind.NAME + else -> PartialSemanticModel.Kind.MEMBER_ACCESS + }, + range = location(site.source, site.startPosition, site.endPosition).range, + operator = + location( + site.source, + operation.operator.startPosition, + operation.operator.endPosition, + ).range, + receiver = + receiver?.let { + location(site.source, it.startPosition, it.endPosition).range + }, + receiverType = type(receiverType), + calleeName = callee, + scope = scope, + arguments = + immutableList( + (operation.leadingArguments + operation.arguments).map { + PartialSemanticModel.Argument( + location(site.source, it.startPosition, it.endPosition).range, + (it as? LabeledExpression)?.name, + type(validatedType(it)), + ) + }, + ), + separators = + immutableList( + operation.separators.map { + Position( + Source.calculateLine(it.startPosition), + Source.calculateOffset(it.startPosition), + ) + }, + ), + members = immutableList(members), + declarationNameType = + site.declarationType + .map { + XdkSyntaxCompletions.declarationName(it, (site.parent as? IncompleteLocalDeclaration)?.initializer) + }.orElse(null), + formals = + immutableList( + cursor?.formals().orEmpty().mapNotNull { formal -> + val bound = formal.constraint()?.let(::type) + if (bound == null && formal.writtenConstraint() == null) { + return@mapNotNull null + } + val token = formal.name() + PartialSemanticModel.Formal( + token.valueText, + bound, + location(site.source, token.startPosition, token.endPosition) + .range, + formal.writtenConstraint(), + ) + }, + ), + memberPrefix = + (site.argumentPrefix.orElse(null) ?: site.memberName.orElse(null))?.let { name -> + PartialSemanticModel.MemberPrefix( + site.completionPrefix, + location(site.source, name.startPosition, name.endPosition).range, + ) + } + ?: PartialSemanticModel.MemberPrefix( + "", + location(site.source, site.endPosition, site.endPosition).range, + ), + callCandidates = + callFacts + ?.takeIf { it.inspected() } + ?.candidates() + ?.let { candidates -> + immutableList( + candidates.mapNotNull { candidate -> + val method = + candidate.method().component as? MethodStructure + ?: return@mapNotNull null + val declaredSignature = + signature( + method, + candidate.signature(), + visibleOnly = true, + ) ?: return@mapNotNull null + val offset = if (candidate.receiverArgument()) 1 else 0 + val signature = + declaredSignature.copy( + parameters = + declaredSignature.parameters.drop(offset), + ) + val id = + symbol( + candidate.method(), + method.name, + SymbolKind.METHOD, + ) ?: return@mapNotNull null + val name = + when { + !method.isConstructor -> { + method.name + } + + method.containingClass.isAnonInnerClass -> { + "new ${operation.target.childNodes().filterIsInstance().single()}" + } + + else -> { + "new ${method.containingClass.name}" + } + } + PartialSemanticModel.CallCandidate( + member = + PartialSemanticModel.Member( + id, + name, + SymbolKind.METHOD, + null, + signature, + ), + arguments = + immutableList( + candidate + .arguments() + .filter { it.parameterIndex() >= offset } + .map { + SemanticModel.CallArgument( + location( + site.source, + it.startPosition(), + it.endPosition(), + ).range, + it.parameterIndex() - offset, + ) + }, + ), + converting = candidate.converting(), + constructor = method.isConstructor, + ) + }, + ) + }, + pendingArgumentName = operation.pendingArgumentName.orElse(null)?.valueText, + functions = + immutableList( + callFacts?.functions().orEmpty().mapNotNull { candidate -> + val signature = + functionSignature(candidate.type()) ?: return@mapNotNull null + PartialSemanticModel.FunctionCandidate( + signature, + immutableList( + candidate.arguments().map { + SemanticModel.CallArgument( + location( + site.source, + it.startPosition(), + it.endPosition(), + ).range, + it.parameterIndex(), + ) + }, + ), + ) + }, + ), + argumentValues = + immutableList( + callFacts?.argumentValues().orEmpty().mapNotNull(::sourceVariable) + + callFacts + ?.argumentProperties() + .orEmpty() + .mapNotNull(::sourceProperty), + ), + argumentOffset = operation.leadingArguments.size, + argumentLiterals = immutableList(callFacts?.argumentLiterals().orEmpty()), + argumentExpressions = immutableList(callFacts?.argumentExpressions().orEmpty()), + enclosingExpressions = immutableList(cursor?.enclosingExpressions().orEmpty()), + argumentTemplates = immutableList(cursor?.argumentTemplates().orEmpty()), + ) + } + return if (errors.isAbortDesired) { + PartialSemanticModel(unavailable(), emptyList()) + } else { + PartialSemanticModel( + finish(nodes, false).single { it.sourceName == source.fileName }, + sites, + ) + } + } + + private enum class Lookup { + INSTANCE, + STATIC, + IMPLICIT, + } + + private fun receiverMembers( + receiver: TypeConstant, + owner: ClassStructure, + errors: ErrorListener, + lookupKind: Lookup = Lookup.INSTANCE, + ): List { + // This explicit inspection owns its diagnostics. Ordinary snapshot extraction stays + // passive. + val lookup = + ErrorListener.cancellable(ErrorListener.collecting(errors::log), errors::isAbortDesired) + val info = + ExecutionTrace.api("TypeConstant.ensureTypeInfo(cursor-members)") { + receiver.ensureTypeInfo(owner.identityConstant, lookup) + } + if (lookup.hasSeriousErrors() || lookup.isAbortDesired) return emptyList() + val privateAccess = info.type.access == Access.PRIVATE + val methods = + info.methods.values + .filter { + it.identity.isTopLevel && + !it.isCtorOrValidator && + ( + lookupKind == Lookup.IMPLICIT || + it.isFunction == (lookupKind == Lookup.STATIC) + ) && + (privateAccess || it.isVisible(owner.identityConstant)) + }.mapNotNull { method -> + val structure = + method.getOptionalTopmostMethodStructure(info) ?: return@mapNotNull null + val signature = + signature(structure, method.signature, visibleOnly = true) + ?: return@mapNotNull null + val symbol = + symbol(structure.identityConstant, structure.name, SymbolKind.METHOD) + ?: return@mapNotNull null + PartialSemanticModel.Member( + symbol, + structure.name, + SymbolKind.METHOD, + null, + signature, + ) + } + val properties = + info + .ensurePropertiesByName() + .values + .filter { + (lookupKind != Lookup.STATIC || it.isConstant) && + (privateAccess || it.isVisible(owner.identityConstant)) + }.mapNotNull { property -> + val type = type(property.inferImmutable(receiver)) ?: return@mapNotNull null + val symbol = + symbol(property.identity, property.name, SymbolKind.PROPERTY) + ?: return@mapNotNull null + PartialSemanticModel.Member( + symbol, + property.name, + SymbolKind.PROPERTY, + type, + null, + ) + } + val children = + info.childInfosByName.values + .filter { + info.type.access.canSee(it.access) || + it.identity.classIdentity.isNestMateOf(owner.identityConstant) + }.mapNotNull { child -> + val id = + symbol(child.identity, child.name, SymbolKind.TYPE) + ?: return@mapNotNull null + PartialSemanticModel.Member( + id, + child.name, + SymbolKind.TYPE, + type(child.identity.type), + null, + ) + } + return (methods + properties + children).sortedWith( + compareBy({ it.name }, { it.kind }, { it.symbol.index }), + ) + } + + private fun hierarchy(nodes: List): Map = + buildMap { + for (node in nodes.filterIsInstance()) { + val component = node.component as? ClassStructure ?: continue + val id = constants[component.identityConstant] ?: continue + if (symbols[id]?.kind != SymbolKind.TYPE) continue + val parents = + component.contributionsAsList + .filter { + it.composition == Composition.Extends || + it.composition == Composition.Implements + }.mapNotNull { contribution -> + val type = contribution.typeConstant ?: return@mapNotNull null + if (type.containsUnresolved()) return@mapNotNull null + val parent = + type.getSingleUnderlyingClass(true) ?: return@mapNotNull null + val target = + symbol(parent, parent.name, SymbolKind.TYPE) + ?: return@mapNotNull null + SemanticModel.Supertype(target, type(type) ?: return@mapNotNull null) + } + put( + id, + SemanticModel.TypeDeclaration( + id, + node.category.id.TEXT + .orEmpty(), + location(node.source, node.startPosition, node.endPosition), + immutableList(parents), + ), + ) + } + } + + private fun recordHeaderStart(node: AstNode) { + val declaration = + when (node) { + is TypeCompositionStatement -> node.takeUnless { it.parent is NewExpression } + is MethodDeclarationStatement -> node + is PropertyDeclarationStatement -> node + is TypedefStatement -> node + else -> null + } ?: return + val target = declaration.component?.takeUnless { it.isSynthetic }?.identityConstant ?: return + val id = constants[target] ?: return + val symbol = symbols[id] ?: return + val start = location(declaration.source, declaration.startPosition, declaration.startPosition).range.start + symbols[id] = symbol.copy(headerStart = start) + } + + private fun declare( + token: Token?, + target: Argument?, + kind: SymbolKind, + source: Source?, + ) { + if (token == null) return + val location = location(source, token.startPosition, token.endPosition) + if (location in occurrences) return + val symbol = symbol(target, token.valueText, kind, location) + occurrences[location] = + Occurrence( + location.range, + token.valueText, + Role.DECLARATION, + symbol, + symbols[symbol]?.type, + ) + } + + private fun refer( + token: Token?, + target: Argument?, + expressionType: TypeConstant?, + source: Source?, + usage: SemanticModel.Usage? = null, + annotation: Boolean = false, + ) { + if (token == null) return + refer( + token.valueText, + location(source, token.startPosition, token.endPosition), + target, + expressionType, + usage, + annotation, + ) + } + + private fun refer( + name: String, + location: SourceLocation, + target: Argument?, + expressionType: TypeConstant?, + usage: SemanticModel.Usage? = null, + annotation: Boolean = false, + ) { + if (location in occurrences) return + val symbol = symbol(target, name, kind(target)) + // Failed name validation can leave a required/placeholder type on the expression. + val type = if (symbol == null) null else type(expressionType) ?: symbols[symbol]?.type + val access = + when (symbols[symbol]?.kind) { + SymbolKind.VARIABLE, + SymbolKind.PARAMETER, + SymbolKind.PROPERTY, + -> usage ?: SemanticModel.Usage.READ + + else -> null + } + occurrences[location] = + Occurrence(location.range, name, Role.REFERENCE, symbol, type, access, annotation) + } + + private fun symbol( + argument: Argument?, + name: String, + kind: SymbolKind, + declaration: SourceLocation? = null, + ): SymbolId? { + val target = normalized(argument) ?: return null + val existing = if (target is Register) registers[target] else constants[target as Constant] + if (existing != null) return existing + val symbol = SymbolId(id, symbols.size) + val dependency = + (target as? IdentityConstant)?.let { + dependencies[it] ?: XdkLibrarySources.declaration(it) + } + val location = declaration ?: dependency?.location + val component = (target as? IdentityConstant)?.component + if (target is Register) { + registers[target] = symbol + } else { + constants[target as Constant] = symbol + } + symbols[symbol] = + Symbol( + id = symbol, + name = name, + kind = kind, + declaration = location?.range, + type = type(declaredType(target)), + signature = signature(target), + declarationSource = location?.sourceName, + modifiers = modifiers(target), + dependency = dependency?.key, + documentation = + component?.documentation?.trim()?.takeIf { + it.isNotEmpty() + }, + typeCategory = + when (component?.format) { + Format.CLASS -> TypeCategory.CLASS + Format.INTERFACE -> TypeCategory.INTERFACE + Format.MIXIN -> TypeCategory.MIXIN + Format.SERVICE -> TypeCategory.SERVICE + Format.CONST -> TypeCategory.CONST + Format.ENUM -> TypeCategory.ENUM + Format.ENUMVALUE -> TypeCategory.ENUM_VALUE + else -> null + }, + isFunction = component is MethodStructure && component.isFunction, + isDefaultLibrary = (target as? IdentityConstant)?.moduleConstant?.name?.let { it in XdkLibraries.moduleNames } == true, + ) + return symbol + } + + private fun modifiers(target: Argument): Set = + immutableSet( + buildSet { + if (target is Register && !target.isWritable) add(SemanticModel.Modifier.READONLY) + val component = (target as? IdentityConstant)?.component + if (component?.isStatic == true) add(SemanticModel.Modifier.STATIC) + if (component?.isAbstract == true) add(SemanticModel.Modifier.ABSTRACT) + if (component is PropertyStructure && component.isConstant) { + add(SemanticModel.Modifier.READONLY) + } + }, + ) + + private fun normalized(argument: Argument?): Argument? { + if (argument is PropertyConstant && argument in capturedProperties) { + return normalized(capturedProperties[argument]) + } + if (argument is Register) { + var register = argument.originalRegister + val seen = Collections.newSetFromMap(IdentityHashMap()) + while (seen.add(register)) { + val origin = captureOrigins[register]?.originalRegister + if (origin == null) { + val type = register.type + val formal = + if (type.isTypeOfType && type.isParamsSpecified) { + type.getParamType(0) + } else { + null + } + if ( + formal != null && + !formal.containsUnresolved() && + formal.isSingleDefiningConstant + ) { + val parameter = formal.definingConstant + if ( + parameter is TypeParameterConstant && + parameter.register == register.index + ) { + return parameter + } + } + return register + } + register = origin + } + return register + } + var target = argument + if ( + target is TypeConstant && + !target.containsUnresolved() && + target.isSingleDefiningConstant + ) { + target = target.definingConstant + } + if (target is Constant && target.containsUnresolved()) return null + if (target is PseudoConstant) { + target = + when (target.format) { + Constant.Format.ThisClass, + Constant.Format.ParentClass, + Constant.Format.ChildClass, + -> target.declarationLevelClass + + else -> return null + } + } + return (target as? IdentityConstant)?.takeUnless { + it.containsUnresolved() || (it is MethodConstant && it.isNascent) + } + } + + private fun identity(statement: ComponentStatement): IdentityConstant? = statement.component?.identityConstant + + private fun declaredType(target: Argument): TypeConstant? = + when (target) { + is Register -> target.type + + is PropertyConstant -> (target.component as? PropertyStructure)?.type + + // its declared callable signature carries parameter/result types + is MethodConstant -> null + + is IdentityConstant -> if (target.format.isTypeable) target.type else null + + else -> null + } + + private fun kind(target: Argument?): SymbolKind = + when (target) { + is Register -> { + SymbolKind.VARIABLE + } + + is Constant -> { + when (target.format) { + Constant.Format.Module -> { + SymbolKind.MODULE + } + + Constant.Format.Package -> { + SymbolKind.PACKAGE + } + + Constant.Format.Method, + Constant.Format.MultiMethod, + -> { + SymbolKind.METHOD + } + + Constant.Format.Property -> { + if ((target as PropertyConstant).isFormalType) { + SymbolKind.TYPE_PARAMETER + } else { + SymbolKind.PROPERTY + } + } + + Constant.Format.TypeParameter, + Constant.Format.FormalTypeChild, + Constant.Format.DynamicFormal, + -> { + SymbolKind.TYPE_PARAMETER + } + + else -> { + SymbolKind.TYPE + } + } + } + + else -> { + SymbolKind.VARIABLE + } + } + + private fun signature(target: Argument): Signature? { + val method = ((target as? MethodConstant)?.component as? MethodStructure) ?: return null + return signature(method, method.identityConstant.signature) + } + + private fun signature( + method: MethodStructure, + signature: SignatureConstant, + visibleOnly: Boolean = false, + ): Signature? { + val parameterTypes = signature.rawParams + if (parameterTypes.size != method.paramCount) return null + val parameters = + method.paramArray.withIndex().drop(if (visibleOnly) method.typeParamCount else 0).map { (index, parameter) -> + SemanticModel.Parameter( + name = parameter.name, + type = type(parameterTypes[index]) ?: return null, + typeParameter = parameter.isTypeParameter, + defaulted = parameter.hasDefaultValue(), + ) + } + val returns = signature.rawReturns.map { type(it) ?: return null } + return Signature( + immutableList(parameters), + immutableList(returns), + method.isConditionalReturn, + ) + } + + private fun type(constant: TypeConstant?): TypeId? { + if (constant == null || !copyableType(constant)) return null + return copyType(constant) + } + + /** Copy a graph already checked by [copyableType]; recursive edges reuse interned IDs. */ + private fun copyType(constant: TypeConstant): TypeId { + typeIds[constant]?.let { + return it + } + val id = TypeId(id, typeIds.size) + typeIds[constant] = id // intern before following recursive type relationships + val arguments = + if (constant.isParamsSpecified && !constant.isRelationalType) { + constant.paramTypes.map(::copyType) + } else { + emptyList() + } + val underlying = + when { + constant.isRelationalType -> { + listOf(copyType(constant.underlyingType), copyType(constant.underlyingType2)) + } + + constant.isModifyingType -> { + listOf(copyType(constant.underlyingType)) + } + + else -> { + emptyList() + } + } + val form = + when (constant.format) { + Constant.Format.TerminalType -> { + if (constant.isFormalType) TypeForm.FORMAL else TypeForm.NAMED + } + + Constant.Format.ParameterizedType -> { + TypeForm.PARAMETERIZED + } + + Constant.Format.ImmutableType -> { + TypeForm.IMMUTABLE + } + + Constant.Format.ServiceType -> { + TypeForm.SERVICE + } + + Constant.Format.AccessType -> { + TypeForm.ACCESS + } + + Constant.Format.AnnotatedType -> { + TypeForm.ANNOTATED + } + + Constant.Format.UnionType -> { + TypeForm.UNION + } + + Constant.Format.IntersectionType -> { + TypeForm.INTERSECTION + } + + Constant.Format.DifferenceType -> { + TypeForm.DIFFERENCE + } + + Constant.Format.RecursiveType -> { + TypeForm.RECURSIVE + } + + else -> { + TypeForm.OTHER + } + } + types[id] = + Type( + id, + constant.valueString, + form, + immutableList(arguments), + immutableList(underlying), + constant.isNullable, + ) + return id + } + + /** A formal identity can be resolved while its written constraint is still unresolved. */ + private fun copyableType( + constant: TypeConstant, + seen: Set = emptySet(), + ): Boolean { + if (constant in seen) return true + if (constant.containsUnresolved() || !acyclicConstraint(constant)) return false + val visited = seen + constant + if ( + constant.isSingleDefiningConstant && constant.definingConstant is TypeParameterConstant + ) { + val formal = constant.definingConstant as TypeParameterConstant + val method = formal.method + if (method.isNascent || !copyableType(method.rawParams[formal.register], visited)) { + return false + } + } + if ( + constant.isParamsSpecified && + !constant.isRelationalType && + !constant.paramTypes.all { copyableType(it, visited) } + ) { + return false + } + return when { + constant.isRelationalType -> { + copyableType(constant.underlyingType, visited) && + copyableType(constant.underlyingType2, visited) + } + + constant.isModifyingType -> { + copyableType(constant.underlyingType, visited) + } + + else -> { + true + } + } + } + + /** + * Nullability follows formal bounds, so T extends T (including indirect cycles) is unsafe. A + * bound such as Iterable terminates at Iterable; its arguments may legally refer to T. Keep + * this bound walk separate from the recursive graph copied by copyableType. + */ + private fun acyclicConstraint( + constant: TypeConstant, + seen: Set = emptySet(), + ): Boolean { + if (constant in seen || constant.containsUnresolved()) return false + val visited = seen + constant + val formal = + if (constant.isSingleDefiningConstant) { + constant.definingConstant as? FormalConstant + } else { + null + } + return when { + formal is TypeParameterConstant && + ( + formal.method.isNascent || + formal.method.rawParams[formal.register].containsUnresolved() + ) -> { + false + } + + formal != null -> { + acyclicConstraint(formal.constraintType, visited) + } + + constant.isRelationalType -> { + acyclicConstraint(constant.underlyingType, visited) && + acyclicConstraint(constant.underlyingType2, visited) + } + + constant.isModifyingType -> { + acyclicConstraint(constant.underlyingType, visited) + } + + else -> { + true + } + } + } + + /** + * Resolve only type identity. Modifiers unwrap; relational operands retain multiple targets. + */ + private fun typeDefinitions( + constant: TypeConstant, + seen: Set = emptySet(), + ): List { + if (constant in seen || constant.containsUnresolved()) return emptyList() + val visited = seen + constant + val resolved = constant.resolveTypedefs() + return when { + resolved != constant -> { + typeDefinitions(resolved, visited) + } + + constant.isNullable -> { + // T? navigates to T, not to the Nullable marker that makes the union nullable. + typeDefinitions(constant.removeNullable(), visited) + } + + constant.isRelationalType -> { + val operands = + if (constant.format == Constant.Format.DifferenceType) { + listOf(constant.underlyingType) + } else { + listOf(constant.underlyingType, constant.underlyingType2) + } + operands.flatMap { typeDefinitions(it, visited) } + } + + constant.isModifyingType -> { + typeDefinitions(constant.underlyingType, visited) + } + + else -> { + val target = normalized(constant) + listOfNotNull( + constants[target] + ?: (target as? IdentityConstant)?.let { + symbol( + it, + it.name, + if (constant.isFormalType) { + SymbolKind.TYPE_PARAMETER + } else { + SymbolKind.TYPE + }, + ) + }, + ) + } + }.distinct() + } + + /** A value may be passed earlier only if the compiler proves its register cannot change. */ + fun extractionFacts(compilation: EmbeddingSupport.Compilation): CompilerExtractionFacts? { + if (!compilation.succeeded()) return null + val values = + registers.entries.mapNotNull { (register, id) -> + val symbol = symbols[id] ?: return@mapNotNull null + if (symbol.kind !in setOf(SymbolKind.VARIABLE, SymbolKind.PARAMETER)) return@mapNotNull null + val source = symbol.declarationSource ?: return@mapNotNull null + val range = symbol.declaration ?: return@mapNotNull null + Triple(SourceLocation(source, range), register.originalType, register.isEffectivelyFinal && !register.isVar) + } + val nodes = nodesIn(requireNotNull(compilation.parsed())) + val modules = compilerModuleAliases(nodes) + val expressionTypes = + nodes.filterIsInstance().mapNotNull { node -> + validatedType(node)?.let { location(node.source, node.startPosition, node.endPosition) to it } + } + val sourceTypes = + nodes + .filterIsInstance() + .mapNotNull { node -> + val type = validatedType(node) ?: return@mapNotNull null + val owner = generateSequence(node.parent) { it.parent }.filterIsInstance().firstOrNull() + val structure = owner?.component as? ClassStructure ?: return@mapNotNull null + val source = + type.resolveTypedefs().memberSourceType( + structure.identityConstant, + emptyMap(), + modules, + ) + ?: return@mapNotNull null + location(node.source, node.startPosition, node.endPosition) to source + }.toMap() + return CompilerExtractionFacts( + expressionTypes.toMap() + values.associate { it.first to it.second }, + values.filter { it.third }.mapTo(linkedSetOf()) { it.first }, + sourceTypes, + ) + } + + /** Inspect validated initializers on the worker; do not retain ASTs or infer purity from spelling. */ + fun removableLocals(compilation: EmbeddingSupport.Compilation): Set = + if (!compilation.succeeded()) { + emptySet() + } else { + nodesIn(requireNotNull(compilation.parsed())) + .filterIsInstance() + .mapNotNull { assignment -> + val local = assignment.lValue as? VariableDeclarationStatement ?: return@mapNotNull null + if (assignment.op.id != Token.Id.ASN || local.hasRefAnnotations() || + !assignment.rValue.isConstant || assignment.rValue.hasSideEffects() + ) { + return@mapNotNull null + } + location(local.source, local.nameToken.startPosition, local.nameToken.endPosition) + }.toSet() + } + + /** Static alone does not imply compile-time evaluation; runtime initializers stay in place. */ + fun constantProperties(compilation: EmbeddingSupport.Compilation): Set = + if (!compilation.succeeded()) { + emptySet() + } else { + nodesIn(requireNotNull(compilation.parsed())) + .filterIsInstance() + .mapNotNull { node -> + val property = node.component as? PropertyStructure ?: return@mapNotNull null + val value = property.initialValue ?: return@mapNotNull null + if (!property.isConstant || value is DeferredValueConstant || + value.containsUnresolved() || property.initializer != null + ) { + return@mapNotNull null + } + location(node.source, node.nameToken.startPosition, node.nameToken.endPosition) + }.toSet() + } + + fun resourceValues(compilation: EmbeddingSupport.Compilation): Map = + compilerResourceValues(nodesIn(requireNotNull(compilation.parsed()))) + + fun typeNames(compilation: EmbeddingSupport.Compilation): List = + compilerTypeNames(nodesIn(requireNotNull(compilation.parsed()))) + + private fun nodesIn(root: AstNode): List = nodesIn(listOf(root)) + + private fun nodesIn(roots: List): List { + val seen = Collections.newSetFromMap(IdentityHashMap()) + val nodes = roots.filter { seen.add(it) }.toMutableList() + var index = 0 + while (index < nodes.size) { + nodes[index++].childNodes().forEach { if (seen.add(it)) nodes.add(it) } + } + return nodes + } + + private fun location( + source: Source?, + start: Long, + end: Long, + ): SourceLocation = + SourceLocation( + source?.fileName, + Range( + Position(Source.calculateLine(start), Source.calculateOffset(start)), + Position(Source.calculateLine(end), Source.calculateOffset(end)), + ), + ) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt index 4811a376989..a308a610801 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt @@ -1,87 +1,2466 @@ package org.xvm.lsp.adapter.xdk +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.ModuleRepository +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode import org.xvm.lsp.adapter.AbstractAdapter -import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.AdapterCapability +import org.xvm.lsp.adapter.CallHierarchyIncomingCall +import org.xvm.lsp.adapter.CallHierarchyItem +import org.xvm.lsp.adapter.CallHierarchyOutgoingCall +import org.xvm.lsp.adapter.CodeAction +import org.xvm.lsp.adapter.CodeLens +import org.xvm.lsp.adapter.CodeLensCommand +import org.xvm.lsp.adapter.ColorPresentation +import org.xvm.lsp.adapter.ColorValue import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.DocumentColor +import org.xvm.lsp.adapter.DocumentHighlight +import org.xvm.lsp.adapter.DocumentLink +import org.xvm.lsp.adapter.FoldingRange +import org.xvm.lsp.adapter.FormattingConfig +import org.xvm.lsp.adapter.FormattingOptions +import org.xvm.lsp.adapter.InlayHint +import org.xvm.lsp.adapter.InlineCompletionContext +import org.xvm.lsp.adapter.LinkedEditingRanges +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.PrepareRenameResult +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.ReadOnlyDocument +import org.xvm.lsp.adapter.SelectionRange +import org.xvm.lsp.adapter.SemanticTokens +import org.xvm.lsp.adapter.SignatureHelp +import org.xvm.lsp.adapter.SymbolMoniker +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.adapter.TypeHierarchyItem +import org.xvm.lsp.adapter.WorkspaceEdit +import org.xvm.lsp.adapter.composeCancellable +import org.xvm.lsp.adapter.mapCancellable import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Diagnostic import org.xvm.lsp.model.Location import org.xvm.lsp.model.SymbolInfo -import org.xvm.lsp.util.WorkInProgress +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.tool.ModuleInfo +import java.io.File +import java.io.IOException +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.UUID +import java.util.concurrent.CancellationException +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.ScheduledFuture +import java.util.concurrent.ScheduledThreadPoolExecutor +import java.util.concurrent.ThreadFactory +import java.util.concurrent.ThreadPoolExecutor +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicReference +import kotlin.time.Duration.Companion.nanoseconds /** - * XDK compiler adapter — placeholder for future full compiler integration. + * Compiler diagnostics and semantic navigation for module source trees. * - * Returns null/empty for all operations. Use for testing LSP infrastructure - * without tree-sitter or regex parsing dependencies. + * A single worker serializes compilations sharing the embedding repository. Each module request + * captures source membership and text, with open editor buffers taking precedence over disk. A + * member edit replaces queued module work and cooperatively cancels the previous attempt. Only the + * current attempt can install its document views, together as one module snapshot. * - * To use: `./gradlew :lang:lsp-server:fatJar -Plsp.adapter=compiler` + * Copied semantic facts support cross-file navigation and declared type hierarchy within a module. + * Per-source AST roots supply outlines, folding and selection. The server supplies document + * versions and publishes diagnostics after checking that the whole module request is still current. + * The complete matching XDK library set is bundled; compilation does not start an interpreter. */ -@WorkInProgress("Awaiting full compiler integration") -class XdkAdapter : AbstractAdapter() { - override val displayName: String = "XDK (stub)" - - // TODO: Integrate with XTC compiler to produce real diagnostics and symbols. - // Needs: Lexer/Parser from Phase 1-3 for accurate AST - override fun compile( - uri: String, - content: String, - ): CompilationResult { - logger.info("compile: uri={}, content={} bytes (stub -- no diagnostics or symbols)", uri, content.length) - return CompilationResult.success(uri, emptyList()) - } +class XdkAdapter + internal constructor( + compileSource: (Source, ModuleRepository?, ErrorListener) -> EmbeddingSupport.Compilation, + compileTree: (ModuleInfo, ModuleRepository?, ErrorListener) -> EmbeddingSupport.Compilation, + analyzeCursor: ( + Source, + ModuleInfo?, + Long, + ModuleRepository?, + ErrorListener, + ) -> EmbeddingSupport.PartialAnalysis, + private val colorPrototype: Boolean = false, + ) : AbstractAdapter() { + private val compiler = CompilerCalls(compileSource, compileTree, analyzeCursor) + private val formattingPreferences = AtomicReference() - // TODO: Use compiler's symbol table to find symbol at position. - // Needs: Phase 4 NameResolver for symbol resolution - override fun findSymbolAt( - uri: String, - line: Int, - column: Int, - ): SymbolInfo? { - logger.info("findSymbolAt: uri={}, line={}, column={} (stub -- returning null)", uri, line, column) - return null - } + override var editorFormattingConfig: FormattingConfig? + get() = formattingPreferences.get() + set(value) = formattingPreferences.set(value) - // TODO: Provide type-aware completions from compiler's type system. - // Needs: Phase 5 TypeResolver for member completion after '.' - // Should include: local vars, members, imported types, smart ranking - override fun getCompletions( - uri: String, - line: Int, - column: Int, - triggerCharacter: String?, - ): List { - logger.info("getCompletions: uri={}, line={}, column={}, trigger={} (stub -- returning empty)", uri, line, column, triggerCharacter) - return emptyList() - } + internal constructor( + compileSource: (Source, ErrorListener) -> EmbeddingSupport.Compilation, + compileTree: (ModuleInfo, ErrorListener) -> EmbeddingSupport.Compilation, + analyzeCursor: (Source, ModuleInfo?, Long, ErrorListener) -> EmbeddingSupport.PartialAnalysis = + { source, sources, cursor, errors -> + analyzeIncomplete(source, sources, cursor, null, errors) + }, + ) : this( + { source, _, errors -> compileSource(source, errors) }, + { sources, _, errors -> compileTree(sources, errors) }, + { source, sources, cursor, _, errors -> analyzeCursor(source, sources, cursor, errors) }, + ) - // TODO: Resolve definition across files using compiler's symbol table. - // Needs: Phase 4 NameResolver for cross-file navigation - // Should handle: imports, inherited members, overloads - override fun findDefinition( - uri: String, - line: Int, - column: Int, - ): Location? { - logger.info("findDefinition: uri={}, line={}, column={} (stub -- returning null)", uri, line, column) - return null - } + internal constructor( + compileSource: (Source, ErrorListener) -> EmbeddingSupport.Compilation, + ) : this( + compileSource, + { sources, errs -> + XdkLibraries.configure() + EmbeddingSupport.instance().compileModule(sources, null, errs) + }, + ) + + constructor() : this(false) + + /** Opt-in test-fixture color contract; not a standard Ecstasy color library. */ + constructor(colorPrototype: Boolean) : + this( + { source, repository, errors -> + XdkLibraries.configure() + EmbeddingSupport.instance().compileModule(source, repository, errors) + }, + { sources, repository, errors -> + XdkLibraries.configure() + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + ::analyzeIncomplete, + colorPrototype, + ) + + internal fun compilerQueueSnapshot(): Map = queueTrace.snapshot() + + internal fun compilerProgressDescription(): String? = queueTrace.progressDescription() + + override val displayName: String = "XDK" + + override val capabilities: Set = + setOf( + AdapterCapability.COMPLETION, + AdapterCapability.INLINE_COMPLETION, + AdapterCapability.HOVER, + AdapterCapability.DEFINITION, + AdapterCapability.DECLARATION, + AdapterCapability.REFERENCES, + AdapterCapability.DOCUMENT_SYMBOL, + AdapterCapability.DOCUMENT_HIGHLIGHT, + AdapterCapability.SELECTION_RANGE, + AdapterCapability.FOLDING_RANGE, + AdapterCapability.WORKSPACE_SYMBOL, + AdapterCapability.TYPE_HIERARCHY, + AdapterCapability.TYPE_DEFINITION, + AdapterCapability.IMPLEMENTATION, + AdapterCapability.MONIKER, + AdapterCapability.CALL_HIERARCHY, + AdapterCapability.SIGNATURE_HELP, + AdapterCapability.SEMANTIC_TOKENS, + AdapterCapability.INLAY_HINT, + AdapterCapability.RENAME, + AdapterCapability.CODE_ACTION, + AdapterCapability.FORMATTING, + AdapterCapability.RANGE_FORMATTING, + AdapterCapability.ON_TYPE_FORMATTING, + AdapterCapability.DOCUMENT_LINK, + AdapterCapability.CODE_LENS, + AdapterCapability.LINKED_EDITING, + ) + if (colorPrototype) setOf(AdapterCapability.DOCUMENT_COLOR) else emptySet() + + private data class Discovery( + val folders: List = emptyList(), + val explicit: Boolean = false, + val buildModel: Boolean = false, + val problem: String? = null, + val catalog: XdkWorkspaceDiscovery.Catalog = XdkWorkspaceDiscovery.Catalog(), + ) + + private val discovery = AtomicReference(Discovery()) + + override fun initializeWorkspace( + workspaceFolders: List, + progressReporter: ((String, Int) -> Unit)?, + ) { + discovery.updateAndGet { + it.copy(folders = workspaceFolders.map { path -> File(path).canonicalFile }.distinct()) + } + refreshDiscoveredSources() + progressReporter?.invoke("Compiler source graph discovered", 100) + } + + /** Returns retired scopes so the server can rebuild current document versions. */ + fun refreshDiscoveredSources(): Set { + val settings = discovery.get() + if (settings.explicit) return emptySet() + val (buffers, previous) = synchronized(lifecycle) { overlays.toMap() to project } + val catalog = + settings.catalog.rescan(settings.folders, buffers) { + closed || discovery.get() !== settings + } + return installDiscoveredCatalog(catalog, settings, previous) { overlays == buffers } + } + + /** + * Incremental header update; callers refresh these scopes at their current document versions. + */ + fun updateDocument( + uri: String, + content: String, + ): Set { + val snapshot = + synchronized(lifecycle) { + if (closed || isLibraryDocument(uri) || overlays[uri] == content) { + return emptySet() + } + overlays[uri] = content + discovery.get() to project + } + return refreshDocumentHeader(uri, content, snapshot.first, snapshot.second) + } + + private fun refreshDocumentHeader( + uri: String, + content: String?, + settings: Discovery, + previous: XdkProject, + closing: Boolean = false, + ): Set { + val file = XdkSources.file(uri) ?: return emptySet() + if (settings.explicit || !XdkWorkspaceDiscovery.includes(settings.folders, file)) { + return emptySet() + } + val catalog = + settings.catalog.withSource(file, content) { closed || discovery.get() !== settings } + return installDiscoveredCatalog(catalog, settings, previous) { + if (closing) uri !in overlays else overlays[uri] == content + } + } + + private fun installDiscoveredCatalog( + catalog: XdkWorkspaceDiscovery.Catalog, + settings: Discovery, + previous: XdkProject, + current: () -> Boolean, + ): Set { + val candidate = runCatching { XdkProject(catalog.modules(previous.modules.values)) } + val failure = candidate.exceptionOrNull() + if (failure != null && failure !is IllegalArgumentException) throw failure + val problem = failure?.message + return installSourceModules( + (candidate.getOrNull() ?: previous).modules.values.toList(), + explicit = false, + force = settings.problem != problem, + ) { + !closed && + project === previous && + current() && + discovery.compareAndSet( + settings, + settings.copy(catalog = catalog, problem = problem), + ) + } + } + + fun changeWorkspaceFolders( + added: List, + removed: List, + ): Set { + val deleted = removed.mapNotNull(XdkSources::file).toSet() + discovery.updateAndGet { settings -> + settings.copy( + folders = + ( + settings.folders.filterNot { it in deleted } + + added.mapNotNull(XdkSources::file) + ).distinct(), + ) + } + return refreshDiscoveredSources() + } + + override val readOnlyDocumentSchemes = setOf(XdkLibrarySources.SCHEME) + + override fun readOnlyDocument(uri: String): ReadOnlyDocument? = + synchronized(lifecycle) { + if (closed) { + null + } else { + XdkLibrarySources.document(uri) ?: dependencies.modules.values.firstNotNullOfOrNull { dependency -> + dependency.documents[uri] ?: dependency.documents.values.find { it.uri == uri } + } + } + } + + private fun isLibraryDocument(uri: String): Boolean = + uri.startsWith("${XdkLibrarySources.SCHEME}:") || + XdkLibrarySources.owns(uri) || synchronized(lifecycle) { dependencies.modules.values.any { uri in it.documents } } + + internal fun libraryConfiguration(): List> = + synchronized(lifecycle) { + dependencies.modules.values.map { dependency -> + mapOf( + "module" to dependency.module, + "revision" to dependency.revision, + "readOnly" to true, + "sources" to dependency.documents.values.map { it.uri }, + ) + } + } + + /** Library queries share compiler serialization, cancellation, tracing and shutdown. */ + private fun libraryMonikers( + uri: String, + line: Int, + column: Int, + ): CompletableFuture> = + synchronized(lifecycle) { + if (closed) return CompletableFuture.failedFuture(IllegalStateException("XDK adapter is closed")) + val result = CompletableFuture>() + val task = + queueTrace.task("library-monikers", uri, result) { + try { + if (!result.isDone) result.complete(XdkLibrarySources.monikers(uri, line, column)) + } catch (failure: Exception) { + result.completeExceptionally(failure) + } catch (failure: Error) { + result.completeExceptionally(failure) + throw failure + } finally { + libraryQueries.remove(result) + } + } + libraryQueries[result] = task + result.whenComplete { _, _ -> + if (result.isCancelled) { + libraryQueries.remove(result) + compiles.remove(task) + } + } + compiles.execute(task.ready()) + result + } + + override fun healthCheck(): Boolean = runCatching { XdkLibraries.configure() }.isSuccess + + override fun compile( + uri: String, + content: String, + ): CompilationResult = compileAsync(uri, content).join() + + override fun analysisScope(uri: String): String = + synchronized(lifecycle) { + project.scope(uri)?.let { + return it + } + val root = XdkSources.moduleRoot(uri, overlays) + if (root == XdkSources.file(uri)) { + scopes[uri] ?: root?.toURI()?.toString() ?: uri + } else { + root?.toURI()?.toString() ?: uri + } + } + + /** One operation's ownership view; canonicalize each open path once, without a stale cache. */ + internal fun analysisScopes(uris: Collection): Map = + synchronized(lifecycle) { + val files = (uris + overlays.keys).distinct().associateWith(XdkSources::file) + val openFiles = overlays.keys.mapNotNull(files::get).toSet() + uris.associateWith { uri -> + val file = files[uri] + file?.let(project::scope) ?: file?.let { + val root = XdkSources.moduleRoot(it, openFiles) + if (root == file) scopes[uri] ?: root.toURI().toString() else root.toURI().toString() + } ?: uri + } + } + + /** Unsaved source buffers do not change the resource files read from disk. */ + internal fun affectedSourceScopes(uri: String): Set = synchronized(lifecycle) { project.affected(analysisScope(uri)) } + + override fun affectedAnalysisScopes(uri: String): Set = + synchronized(lifecycle) { + project.orderedScopes(inputScopes(uri).flatMapTo(linkedSetOf(), project::affected)) + } + + /** + * Only filesystem refreshes may coalesce against compiled inputs; editor changes still retire + * queries. + */ + internal fun changedFileScopes(uri: String): Set = + synchronized(lifecycle) { + val file = XdkSources.file(uri) + val openSources = + overlays + .mapNotNull { (uri, text) -> XdkSources.file(uri)?.let { it to text } } + .toMap() + val changed = + inputScopes(uri).filter { scope -> + val module = project.modules.values.firstOrNull { it.uri == scope } + val inputs = sourceInputs(scope) + val root = module?.root ?: XdkSources.file(scope) + file == null || + root == null || + inputs == null || + requests[scope]?.result?.isDone == false || + // A failed current analysis still owns its captured source inputs. + // Only fallback builds can predate a failure (e.g. a missing dependency); + // matching that old build must not suppress recovery after recreation. + ( + completed[scope]?.sourceInputs == null && + project.affected(scope).any { completed[it]?.succeeded == false } + ) || + !XdkFileChanges.unchanged(file, root, inputs, openSources) + } + project.orderedScopes(changed.flatMapTo(linkedSetOf(), project::affected)) + } + + private fun sourceInputs(scope: String): XdkSources.Inputs? = + completed[scope]?.sourceInputs + ?: project.modules.values + .firstOrNull { it.uri == scope } + ?.let { builds[it.name]?.key?.sources } + ?: diagnosticCache + .snapshot() + .builds[scope] + ?.key + ?.sources + + private fun inputScopes(uri: String): Set { + val file = XdkSources.file(uri)?.toPath() + val known = + project.modules.values + .map { it.uri } + .toSet() + requests.keys + completed.keys + val sources = + known.filter { scope -> + val root = XdkSources.file(scope)?.toPath() + if (root == null || file == null) { + scope == uri + } else { + val members = root.parent.resolve(root.fileName.toString().removeSuffix(".x")) + root.startsWith(file) || file.startsWith(members) + } + } + val resources = + project.resourceScopes(uri).filter { scope -> + val inputs = sourceInputs(scope) + requests[scope]?.result?.isDone == false || + inputs == null || + inputs.resources.entries.isNotEmpty() + } + return (sources + resources).toSet() + } + + fun inputWatchRoots(): Set = synchronized(lifecycle) { project.inputWatchRoots() } + + override fun compileAsync( + uri: String, + content: String, + ): CompletableFuture { + if (isLibraryDocument(uri)) { + return CompletableFuture.completedFuture(CompilationResult.success(uri, emptyList())) + } + updateDocument(uri, content) + val submission = + synchronized(lifecycle) { + if (closed) { + return CompletableFuture.failedFuture( + IllegalStateException("XDK adapter is closed"), + ) + } + overlays[uri] = content + val scope = analysisScope(uri) + scopes[uri] = scope + val sourceScopes = project.buildOrder(scope).mapTo(mutableSetOf(scope)) { it.uri } + val overlayScopes = analysisScopes(overlays.keys) + val request = + Request( + scope, + uri, + overlays.filterKeys { + overlayScopes[it] in sourceScopes + }, + dependencies, + project, + discovery.get().problem, + queueTrace, + ::runCompilation, + ) + request.result.whenComplete { _, _ -> + if (request.result.isCancelled) { + val obsolete = + synchronized(lifecycle) { + removeQueued(request) + if (requests.remove(scope, request)) { + completed.remove(scope) + retireQueries(scope) + } else { + emptyList() + } + } + obsolete.forEach { it.result.cancel(false) } + } + } + val (retired, obsoleteQueries) = retireRequests(project.affected(scope)) + requests[scope] = request + scheduled[request] = + debouncer.schedule( + { + synchronized(lifecycle) { + if (scheduled.remove(request) != null && !isStale(request)) { + compiles.execute(request.task.ready()) + } + } + }, + DEBOUNCE_MILLIS, + TimeUnit.MILLISECONDS, + ) + Submission(request = request, retired = retired, obsoleteQueries = obsoleteQueries) + } + submission.retired.forEach { it.result.cancel(false) } + submission.obsoleteQueries.forEach { it.result.cancel(false) } + return submission.request.result + } + + /** + * Compiler-worker probe for a current open document. Results contain copied facts only; they + * neither replace normal diagnostics nor install another module analysis. A newer cursor of the + * same query kind in this document, or any edit in its module, invalidates the request. + * Protocol consumers must also check their captured document version before publishing facts. + */ + internal fun analyzeAtAsync( + uri: String, + position: Position, + ): CompletableFuture = analyzeAtAsync(CursorKey(uri, CursorKind.PROBE), position) + + private fun analyzeAtAsync( + key: CursorKey, + position: Position, + ): CompletableFuture { + val uri = key.uri + val (request, previous) = + synchronized(lifecycle) { + if (closed) { + return CompletableFuture.failedFuture( + IllegalStateException("XDK adapter is closed"), + ) + } + if (discovery.get().problem != null) return CompletableFuture.completedFuture(null) + val compilation = + requests[analysisScope(uri)] ?: return CompletableFuture.completedFuture(null) + if (uri !in compilation.overlays) return CompletableFuture.completedFuture(null) + val request = + CursorRequest(compilation, key, position, queueTrace, ::runCursorAnalysis) + request.result.whenComplete { _, _ -> + if (request.result.isCancelled) { + synchronized(lifecycle) { + cursors.remove(key, request) + compiles.remove(request.task) + } + } + } + val previous = cursors.put(key, request) + previous?.let { compiles.remove(it.task) } + compiles.execute(request.task.ready()) + request to previous + } + previous?.result?.cancel(false) + return request.result + } + + private enum class CursorKind { + PROBE, + COMPLETION, + INLINE_COMPLETION, + SIGNATURE, + } + + private data class CursorKey( + val uri: String, + val kind: CursorKind, + ) + + private sealed interface QueryWork { + val result: CompletableFuture<*> + val task: CompilerQueueTrace.Work + } + + private sealed interface QueryRequest : QueryWork { + val compilation: Request + } + + private enum class ProjectQueryKind { + REFERENCES, + CODE_LENSES, + MONIKERS, + RENAME, + RENAME_PROPOSAL, + FILE_RENAME, + SYMBOLS, + NAVIGATION, + CODE_ACTIONS, + COMPLETION_IMPORTS, + DIAGNOSTICS, + } + + private data class ProjectQueryKey( + val uri: String, + val kind: ProjectQueryKind, + val range: Range? = null, + val requestId: UUID? = null, + ) + + private class ProjectRequest( + val key: ProjectQueryKey, + val project: XdkProject, + val dependencies: XdkDependencies, + val overlays: Map, + trace: CompilerQueueTrace, + work: (ProjectRequest) -> Unit, + ) : QueryWork { + override val result = CompletableFuture() + override val task = trace.task("project-${key.kind}", key.uri, result) { work(this) } + } + + private class CursorRequest( + override val compilation: Request, + val key: CursorKey, + val position: Position, + trace: CompilerQueueTrace, + work: (CursorRequest) -> Unit, + ) : QueryRequest { + val uri: String + get() = key.uri + + override val result = CompletableFuture() + override val task = trace.task("cursor-${key.kind}", uri, result) { work(this) } + } + + private class RenameRequest( + override val compilation: Request, + val module: ModuleAnalysis, + val uri: String, + val position: Position, + val name: String, + trace: CompilerQueueTrace, + work: (RenameRequest) -> Unit, + ) : QueryRequest { + override val result = CompletableFuture() + override val task = trace.task("rename-proof", uri, result) { work(this) } + } + + /** Called under lifecycle; future callbacks must run after releasing it. */ + private fun retireQueries(scope: String): List = + (cursors.values + renames.values) + .filter { it.compilation.scope == scope } + .onEach { + when (it) { + is CursorRequest -> cursors.remove(it.key, it) + is RenameRequest -> renames.remove(it.uri, it) + } + compiles.remove(it.task) + } + retireProjectQueries() + + /** Every graph query owns a complete configured snapshot, so any source change retires it. */ + private fun retireProjectQueries(): List = + projectQueries.values.toList().also { retired -> + projectQueries.clear() + navigationIndex.retire(project.modules.values.mapTo(hashSetOf()) { it.uri }) + retired.forEach { compiles.remove(it.task) } + } + + private fun projectQuery( + key: ProjectQueryKey, + unavailable: T, + query: (XdkProjectQueries) -> T, + ): CompletableFuture { + val (request, previous) = + synchronized(lifecycle) { + if (closed) { + return CompletableFuture.failedFuture( + IllegalStateException("XDK adapter is closed"), + ) + } + if (discovery.get().problem != null || project.scope(key.uri) == null) { + return CompletableFuture.completedFuture(unavailable) + } + val request = + ProjectRequest(key, project, dependencies, overlays.toMap(), queueTrace) { work: ProjectRequest -> + fun stale(): Boolean = projectQueries[key] !== work || work.result.isCancelled + try { + if (stale()) throw CancellationException() + val result = + try { + query( + XdkProjectQueries( + work.project, + work.overlays, + work.dependencies, + compiler::compileTree, + ::stale, + discoverImports = !discovery.get().explicit, + diagnosticCache = diagnosticCache, + navigationIndex = navigationIndex, + ), + ) + } catch (_: IOException) { + unavailable + } + synchronized(lifecycle) { if (stale()) throw CancellationException() } + work.result.complete(result) + } catch (_: CancellationException) { + work.result.cancel(false) + } catch (failure: Exception) { + work.result.completeExceptionally(failure) + } catch (failure: Error) { + work.result.completeExceptionally(failure) + throw failure + } finally { + projectQueries.remove(key, work) + } + } + request.result.whenComplete { _, _ -> + if (request.result.isCancelled) { + synchronized(lifecycle) { + projectQueries.remove(key, request) + compiles.remove(request.task) + } + } + } + val previous = projectQueries.put(key, request) + previous?.let { compiles.remove(it.task) } + compiles.execute(request.task.ready()) + request to previous + } + previous?.result?.cancel(false) + return request.result + } + + private fun isStale(request: CursorRequest): Boolean = + cursors[request.key] !== request || + request.result.isCancelled || + isStale(request.compilation) + + private fun runCursorAnalysis(request: CursorRequest) { + try { + if (isStale(request)) throw CancellationException() + val source = Source(request.compilation.overlays.getValue(request.uri), request.uri) + val errors = ErrorListener.cancellable(ErrorList()) { isStale(request) } + val cursor = cursorPosition(source, request.position, errors) + val facts = + cursor?.let { + val sources = captureSources(request.compilation) { isStale(request) } + val dependencies = + ( + completed[request.compilation.scope]?.inputs + ?: request.compilation.dependencies + ).open() + val model = + compiler + .analyzeCursor(source, sources, it, dependencies.repository, errors) + .semanticSnapshot(errors) + val syntax = + if (request.key.kind == CursorKind.COMPLETION) { + XdkSyntaxCompletions.complete( + source.toRawString(), + request.position, + model.sites.singleOrNull()?.declarationNameType, + ) { isStale(request) } + } else { + emptyList() + } + model.withSyntaxCompletions(syntax) + } + synchronized(lifecycle) { + if (isStale(request)) throw CancellationException() + } + request.result.complete(facts) + } catch (_: CancellationException) { + request.result.cancel(false) + } catch (e: Exception) { + request.result.completeExceptionally(e) + } catch (e: Error) { + request.result.completeExceptionally(e) + throw e + } finally { + cursors.remove(request.key, request) + } + } + + private fun runCompilation(request: Request) { + try { + if (isStale(request)) throw CancellationException() + val started = System.nanoTime() + val result = compileNow(request) + synchronized(lifecycle) { + if (isStale(request)) throw CancellationException() + completed[request.scope] = result + } + logger.info( + "compile: scope={}, {} document(s), {} diagnostic(s), compiled in {}", + request.scope, + result.documents.size, + result.diagnostics.size, + (System.nanoTime() - started).nanoseconds, + ) + // Future callbacks may publish diagnostics; never invoke them under the adapter lock. + request.result.complete( + CompilationResult.withDiagnostics( + request.uri, + result.diagnostics, + result.document(request.uri)?.symbols.orEmpty(), + result.documentUris, + ), + ) + } catch (_: CancellationException) { + request.result.cancel(false) + } catch (e: Exception) { + request.result.completeExceptionally(e) + } catch (e: Error) { + request.result.completeExceptionally(e) + throw e + } + } + + override fun getCachedResult(uri: String): CompilationResult? = + analysis(uri)?.let { CompilationResult.withDiagnostics(uri, it.diagnostics, it.symbols) } + + private class Request( + val scope: String, + val uri: String, + val overlays: Map, + val dependencies: XdkDependencies, + val project: XdkProject, + val problem: String?, + trace: CompilerQueueTrace, + work: (Request) -> Unit, + ) { + val result = CompletableFuture() + val task = trace.task("compile", scope, result) { work(this) } + } + + private data class Submission( + val request: Request, + val retired: List, + val obsoleteQueries: List, + ) + + /** Installed atomically, so all member views always belong to the same compilation. */ + private class ModuleAnalysis( + val documents: Map, + val diagnostics: List, + val dependencies: Set, + val succeeded: Boolean, + val dependencySources: Map, + val inputs: XdkDependencies, + val artifact: XdkDependency? = null, + val documentUris: Set = documents.keys, + val sourceInputs: XdkSources.Inputs? = null, + val sourceTexts: Map = emptyMap(), + ) { + private val semanticViews = + documents + .mapNotNull { (uri, analysis) -> analysis.semantics?.let { uri to it } } + .toMap() + val hierarchy = XdkHierarchy(semanticViews) + val calls = XdkCalls(semanticViews) + + fun document(uri: String): Analysis? = + documents[uri] + ?: documents.entries + .firstOrNull { + XdkSources.file(it.key) != null && + XdkSources.file(it.key) == XdkSources.file(uri) + }?.value + + fun sourceUri(name: String?): String? = + documents.entries.firstOrNull { it.value.semantics?.sourceName == name }?.key + ?: dependencySources[name] + ?: XdkLibrarySources.sourceUri(name) + } + + private data class Analysis( + val diagnostics: List = emptyList(), + val symbols: List = emptyList(), + val ast: AstNode? = null, + val semantics: SemanticModel? = null, + val colors: List = emptyList(), + ) + + private fun module(uri: String): ModuleAnalysis? = completed[analysisScope(uri)] + + private fun analysis(uri: String): Analysis? = module(uri)?.document(uri) + + /** Explicit source graph; changed inputs retire affected analyses and all pending attempts. */ + fun replaceSourceModules(modules: List): Set = installSourceModules(modules, explicit = true) + + fun discoverSourceModules(): Set { + discovery.updateAndGet { it.copy(explicit = false) } + return refreshDiscoveredSources() + } + + private fun installSourceModules( + modules: List, + explicit: Boolean, + force: Boolean = false, + current: () -> Boolean = { true }, + ): Set { + val replacement = XdkProject(modules) + val (retired, probes) = + synchronized(lifecycle) { + check(!closed) { "XDK adapter is closed" } + if (!current()) return emptySet() + val recovered = explicit && discovery.get().problem != null + val clearBuildArtifacts = discovery.get().buildModel + discovery.updateAndGet { + it.copy( + explicit = explicit, + buildModel = false, + problem = if (explicit) null else it.problem, + ) + } + if ( + !force && + !recovered && + !clearBuildArtifacts && + project.sameConfiguration(replacement) + ) { + return emptySet() + } + replaceProject( + replacement, + if (clearBuildArtifacts) XdkDependencies(emptyList()) else dependencies, + force || recovered, + ) + } + retired.forEach { it.result.cancel(false) } + probes.forEach { it.result.cancel(false) } + return replacement.orderedScopes( + retired.flatMapTo(linkedSetOf()) { replacement.affected(it.scope) + it.scope }, + ) + } + + /** Called under lifecycle. Only successful analyses with unchanged inputs may survive. */ + private fun replaceProject( + replacement: XdkProject, + artifacts: XdkDependencies, + force: Boolean = false, + ): Pair, List> { + val changed = + project.changedModules(replacement) + + (dependencies.modules.keys + artifacts.modules.keys).filter { + dependencies.modules[it]?.revision != artifacts.modules[it]?.revision + } + val retired = + requests.values + .filter { request -> + val analysis = completed[request.scope] + force || !request.result.isDone || analysis?.succeeded != true || + analysis.dependencies.any(changed::contains) || + project.buildOrder(request.scope).any { it.name in changed } || + replacement.buildOrder(request.scope).any { it.name in changed } || + request.overlays.keys.any { project.scope(it) != replacement.scope(it) } + }.mapTo(linkedSetOf()) { it.scope } + project = replacement + dependencies = artifacts + if (force) builds.clear() else builds.keys.removeAll { it in changed || it !in replacement.modules } + return retireRequests(retired) + } + + /** Called under lifecycle. Compiler-owned objects never enter the artifact cache. */ + private fun retireRequests(scopes: Set): Pair, List> { + val retired = scopes.mapNotNull { requests.remove(it) } + scopes.forEach(completed::remove) + retired.forEach(::removeQueued) + return retired to (scopes.flatMap(::retireQueries) + retireProjectQueries()) + } + + private fun removeQueued(request: Request) { + scheduled.remove(request)?.cancel(false) + compiles.remove(request.task) + } + + /** + * Atomically replace host-supplied artifacts and retire affected analyses/cursor probes. The + * host must reanalyse the returned scopes to publish diagnostics for current document versions; + * XtcLanguageServer.replaceCompilerDependencies performs that step under its lock. Failed and + * pending attempts are conservatively retried because their import set is incomplete. + */ + fun replaceDependencies(artifacts: List): Set { + val replacement = XdkDependencies(artifacts) + val (retired, probes) = + synchronized(lifecycle) { + check(!closed) { "XDK adapter is closed" } + discovery.updateAndGet { it.copy(buildModel = false) } + val changed = + (dependencies.modules.keys + replacement.modules.keys).filterTo(linkedSetOf()) { + dependencies.modules[it]?.revision != replacement.modules[it]?.revision + } + if (changed.isEmpty()) return emptySet() + dependencies = replacement + val retired = + requests.values.filter { + val analysis = completed[it.scope] + analysis == null || + !analysis.succeeded || + analysis.dependencies.any(changed::contains) + } + val probes = + retired.flatMap { request -> + requests.remove(request.scope, request) + completed.remove(request.scope) + removeQueued(request) + retireQueries(request.scope) + } + retireProjectQueries() + retired to probes + } + retired.forEach { it.result.cancel(false) } + probes.forEach { it.result.cancel(false) } + return synchronized(lifecycle) { + project.orderedScopes(retired.mapTo(linkedSetOf()) { it.scope }) + } + } + + /** + * Install one evaluated build snapshot; validation completes before either live input changes. + */ + internal fun replaceBuildInputs( + inputs: XdkBuildModel.Inputs, + automatic: Boolean = false, + ): Set { + val replacement = XdkProject(inputs.modules) + val artifacts = XdkDependencies(inputs.binaries) + val (retired, probes) = + synchronized(lifecycle) { + check(!closed) { "XDK adapter is closed" } + discovery.updateAndGet { + it.copy(explicit = !automatic, buildModel = !automatic, problem = null) + } + if ( + project.sameConfiguration(replacement) && + dependencies.modules.mapValues { it.value.revision } == + artifacts.modules.mapValues { it.value.revision } + ) { + return emptySet() + } + replaceProject(replacement, artifacts) + } + retired.forEach { it.result.cancel(false) } + probes.forEach { it.result.cancel(false) } + return replacement.orderedScopes(retired.mapTo(linkedSetOf()) { it.scope }) + } + + override fun closeDocument(uri: String) { + closeDocumentAndRefresh(uri) + } + + /** Closing restores disk headers and returns exactly the scopes retired by that change. */ + fun closeDocumentAndRefresh(uri: String): Set { + if (isLibraryDocument(uri)) return emptySet() + val (retired, obsoleteQueries) = + synchronized(lifecycle) { + val scope = scopes.remove(uri) ?: analysisScope(uri) + overlays.remove(uri) + retireRequests(project.affected(scope)).also { + val retained = + requests.keys + .flatMap { project.buildOrder(it).map(XdkSourceModule::name) } + .toSet() + builds.keys.retainAll(retained) + } + } + retired.forEach { it.result.cancel(false) } + obsoleteQueries.forEach { it.result.cancel(false) } + val (settings, previous) = synchronized(lifecycle) { discovery.get() to project } + val file = XdkSources.file(uri) + val disk = + try { + file + ?.takeIf { + !settings.explicit && + XdkWorkspaceDiscovery.includes(settings.folders, it) && + it.isFile + }?.readText() + } catch (_: IOException) { + null + } + return refreshDocumentHeader(uri, disk, settings, previous, closing = true) + + retired.map { it.scope } + } + + override fun close() { + val pending = + synchronized(lifecycle) { + closed = true + val pending = + requests.values.map { it.result } + + cursors.values.map { it.result } + + renames.values.map { it.result } + + projectQueries.values.map { it.result } + libraryQueries.keys + requests.clear() + cursors.clear() + renames.clear() + projectQueries.clear() + libraryQueries.clear() + diagnosticCache.clear() + navigationIndex.clear() + overlays.clear() + scopes.clear() + completed.clear() + builds.clear() + scheduled.values.forEach { it.cancel(false) } + scheduled.clear() + debouncer.shutdownNow() + compiles.queue.clear() + compiles.shutdown() + pending + } + pending.forEach { it.cancel(false) } + if (!compiles.awaitTermination(SHUTDOWN_SECONDS, TimeUnit.SECONDS)) compiles.shutdownNow() + } + + private fun isStale(request: Request): Boolean = requests[request.scope] !== request || request.result.isCancelled + + private fun captureSources( + request: Request, + cancelled: () -> Boolean, + ): XdkSources? = + XdkSources + .file(request.scope) + ?.takeIf { + it.isFile || + it != XdkSources.file(request.uri) || + File(it.parentFile, it.nameWithoutExtension).isDirectory + }?.let { root -> + val resources = + request.project.modules.values + .firstOrNull { it.root == root } + ?.resourceFiles + XdkSources.capture(root, request.overlays, resources, cancelled) + } + + private data class BuildKey( + val sources: XdkSources.Inputs, + val dependencies: Map, + ) - // TODO: Find all references using compiler's semantic model. - // Needs: Phase 4 NameResolver + workspace-wide index - // Should include: usages across all files, inherited references - override fun findReferences( - uri: String, - line: Int, - column: Int, - includeDeclaration: Boolean, - ): List { - logger.info( - "findReferences: uri={}, line={}, column={}, includeDeclaration={} (stub -- returning empty)", - uri, - line, - column, - includeDeclaration, + private data class CachedBuild( + val key: BuildKey, + val artifact: XdkDependency?, + val diagnostics: List, + val documentUris: Set, ) - return emptyList() + + /** + * Capture the complete dependency closure before compiling any of it. Retain detached + * dependency outputs; only the target analysis needs a live AST after this loop. + */ + private fun compileNow(request: Request): ModuleAnalysis { + request.problem?.let { + return unavailableProjectModule(request.uri, request.dependencies, "SOURCE-GRAPH", it) + } + val order = request.project.buildOrder(request.scope) + if (order.isEmpty()) { + return compileOne( + request, + request.uri, + captureSources(request) { isStale(request) }, + request.dependencies, + ) + } + val captured = + order.associateWith { module -> + try { + Result.success( + XdkSources.capture(module.root, request.overlays, module.resourceFiles) { + isStale(request) + }, + ) + } catch (failure: IOException) { + Result.failure(failure) + } + } + // A source-owned module must never fall back to an older host-supplied binary. + val artifacts = + request.dependencies.modules + .filterKeys { it !in request.project.modules } + .toMutableMap() + val diagnostics = mutableListOf() + val documents = linkedSetOf() + val target = + order + .asSequence() + .map { module -> + if (isStale(request)) throw CancellationException() + val closure = request.project.buildOrder(module.uri).mapTo(hashSetOf()) { it.name } + val inputs = + XdkDependencies( + artifacts.filterKeys { it !in request.project.modules || it in closure }.values.toList(), + ) + val sources = captured.getValue(module).getOrNull() + val uri = sources?.uri(module.root) ?: module.uri + val missing = + module.dependencies.filter { it !in artifacts && it !in XdkLibraries.moduleNames } + val key = + sources?.let { + BuildKey(it.inputs, inputs.modules.mapValues { (_, artifact) -> artifact.revision }) + } + val cached = + builds[module.name]?.takeIf { + key != null && it.key == key && module.uri != request.scope + } + val analysis = + when { + sources == null -> { + val reason = captured.getValue(module).exceptionOrNull()?.message + unavailableProjectModule( + uri, + inputs, + "SOURCE-UNAVAILABLE", + "Cannot read source for ${module.name}: $reason", + ) + } + + missing.isNotEmpty() -> { + unavailableProjectModule( + uri, + inputs, + "DEPENDENCY-FAILED", + "Dependencies unavailable: ${missing.joinToString()}", + sources.documentUris, + ) + } + + cached != null -> { + ModuleAnalysis( + documents = emptyMap(), + diagnostics = cached.diagnostics, + dependencies = emptySet(), + succeeded = cached.artifact != null, + dependencySources = emptyMap(), + inputs = inputs, + artifact = cached.artifact, + documentUris = cached.documentUris, + ) + } + + else -> { + compileOne(request, uri, sources, inputs, module.name) + } + } + synchronized(lifecycle) { + if (isStale(request)) throw CancellationException() + if (key != null) { + builds[module.name] = + CachedBuild( + key, + analysis.artifact, + analysis.diagnostics, + analysis.documentUris, + ) + } + } + analysis.artifact?.let { artifacts[module.name] = it } + diagnostics += analysis.diagnostics + documents += analysis.documentUris + analysis + }.last() + return ModuleAnalysis( + documents = target.documents, + diagnostics = diagnostics, + dependencies = target.dependencies + order.map { it.name }, + succeeded = target.succeeded, + dependencySources = target.dependencySources, + inputs = target.inputs, + artifact = target.artifact, + documentUris = documents, + sourceInputs = target.sourceInputs, + sourceTexts = target.sourceTexts, + ) + } + + private fun unavailableProjectModule( + uri: String, + inputs: XdkDependencies, + code: String, + message: String, + documents: Set = setOf(uri), + ) = ModuleAnalysis( + documents = emptyMap(), + diagnostics = + listOf( + Diagnostic( + Location(uri, 0, 0, 0, 0), + Diagnostic.Severity.ERROR, + message, + code, + SOURCE, + ), + ), + dependencies = emptySet(), + succeeded = false, + dependencySources = emptyMap(), + inputs = inputs, + documentUris = documents, + ) + + /** Compile and copy all source views on the single worker. */ + private fun compileOne( + request: Request, + uri: String, + sources: XdkSources?, + inputs: XdkDependencies, + moduleName: String? = null, + ): ModuleAnalysis { + val heard = ErrorList() + val errs = ErrorListener.cancellable(heard) { isStale(request) } + val source = Source(request.overlays[uri].orEmpty(), uri) + val dependencies = inputs.open() + val compilation = + if (sources == null) { + compiler.compileSource(source, dependencies.repository, errs) + } else { + compiler.compileTree(sources.freshCompilationInput(), dependencies.repository, errs) + } + if (isStale(request)) throw CancellationException() + if (sources != null && !sources.resourcesCurrent { isStale(request) }) { + throw CancellationException() + } + val footprint = + ExecutionTrace.api("EmbeddingSupport.footprint", request.uri) { + EmbeddingSupport.instance().footprint(compilation) + } + logger.info("compile: scope={} [{}]", request.scope, footprint) + val roots = + buildMap { + compilation.sourceTrees().forEach { putAll(XdkAst.rootsBySource(it)) } + } + val semantics = compilation.compiledSemantics(dependencies, errs, navigation = moduleName != null) + val colors = if (colorPrototype) XdkColorPrototype.capture(compilation) else emptyMap() + val navigation = semantics.navigation + val views = semantics.models + val sourceUris = sources?.sourceUris ?: roots.keys.associateWith { it } + val fallback = if (sources == null) source else Source("", sources.uri(sources.sourceFile)) + val declarations = XdkAst.declarationLocations(compilation.sourceTrees(), sourceUris) + val diagnostics = heard.errors.map { it.toDiagnostic(fallback, sourceUris, declarations) } + val documentUris = sources?.documentUris ?: setOf(uri) + val documents = + documentUris.associateWith { uri -> + val sourceName = sourceUris.entries.firstOrNull { it.value == uri }?.key ?: uri + val ast = roots[sourceName] + Analysis( + diagnostics.filter { it.location.uri == uri }, + XdkSymbols.of(uri, ast), + ast, + views.firstOrNull { it.sourceName == sourceName }, + colors[sourceName].orEmpty(), + ) + } + val dependencySources = + dependencies.declarations.values + .mapNotNull { declaration -> + val name = declaration.location.sourceName ?: return@mapNotNull null + val uri = XdkSources.sourceUri(name) + uri?.let { name to it } + }.toMap() + val artifact = semantics.artifact?.takeIf { moduleName != null } + if (artifact != null && artifact.module != moduleName) { + return unavailableProjectModule( + uri, + inputs, + "PROJECT-MODULE", + "Expected module $moduleName, found ${artifact.module}", + documentUris, + ) + } + if (navigation != null && artifact != null && sources != null) { + synchronized(lifecycle) { + if (isStale(request)) throw CancellationException() + navigationIndex.record( + request.project.modules + .getValue(artifact.module) + .uri, + XdkNavigationIndex.Build( + XdkNavigationIndex.Key( + artifact.module, + sources.inputs, + inputs.modules.mapValues { it.value.revision }, + ), + navigation, + artifact, + ), + ) + } + } + return ModuleAnalysis( + documents = documents, + diagnostics = diagnostics, + dependencies = + compilation + .file() + ?.moduleIds() + ?.mapTo(linkedSetOf()) { it.name } + .orEmpty(), + succeeded = compilation.succeeded(), + dependencySources = dependencySources, + inputs = inputs, + artifact = artifact, + sourceInputs = sources?.inputs, + sourceTexts = + sources + ?.inputs + ?.text + ?.entries + ?.associate { (file, text) -> file.path to text } + ?: mapOf(uri to request.overlays.getValue(uri)), + ) + } + + // ----- what the tree can answer -------------------------------------------------------------- + + override fun findSymbolAt( + uri: String, + line: Int, + column: Int, + ): SymbolInfo? = XdkSymbols.at(analysis(uri)?.symbols ?: emptyList(), line, column) + + /** The resolved occurrence and its inferred type, including flow-sensitive narrowing. */ + override fun getHoverInfo( + uri: String, + line: Int, + column: Int, + ): String? { + val model = analysis(uri)?.semantics + val hover = model?.let { XdkPresentation.hover(it, line, column) } + if (hover == null && model?.status != SemanticModel.Status.COMPLETE) { + val partial = analyzeAtAsync(uri, Position(line, column + 1)).join() + val site = partial?.sites?.singleOrNull() + val prefix = site?.memberPrefix + val text = currentText(uri)?.lineSequence()?.elementAtOrNull(line) + if ( + partial != null && + prefix != null && + text != null && + prefix.range.end.column <= text.length + ) { + val written = text.substring(prefix.range.start.column, prefix.range.end.column) + site.formals + .singleOrNull { it.name == written } + ?.let { + return "```xtc\n${XdkCursorQueries.formalDetail(partial, it)}\n```" + } + } + } + return hover + } + + /** Highlight only occurrences of the resolved target, including its declaration when known. */ + override fun getDocumentHighlights( + uri: String, + line: Int, + column: Int, + ): List { + val model = analysis(uri)?.semantics ?: return emptyList() + val symbol = model.symbolAt(line, column) ?: return emptyList() + return model.occurrences + .filter { it.symbol == symbol.id } + .map { + val kind = + when (it.usage) { + SemanticModel.Usage.READ -> DocumentHighlight.HighlightKind.READ + + SemanticModel.Usage.WRITE, + SemanticModel.Usage.READ_WRITE, + -> DocumentHighlight.HighlightKind.WRITE + + null -> DocumentHighlight.HighlightKind.TEXT + } + DocumentHighlight(it.range.toRange(), kind) + } + } + + override fun getSemanticTokens(uri: String): SemanticTokens? = + analysis(uri)?.semantics?.let { + XdkPresentation.tokens(it, XdkLexical.tokens(currentText(uri).orEmpty())) + } + + override fun getDocumentColors(uri: String): List = XdkColorPrototype.colors(analysis(uri)?.colors.orEmpty()) + + override fun getColorPresentations( + uri: String, + range: Range, + color: ColorValue, + ): List = + if (readOnlyDocument(uri) != null) { + emptyList() + } else { + listOfNotNull(analysis(uri)?.colors?.singleOrNull { it.range == range }?.presentation(color)) + } + + private fun currentText(uri: String): String? = + analysis(uri)?.ast?.source?.toRawString() ?: synchronized(lifecycle) { overlays[uri] } + + override fun formatDocument( + uri: String, + content: String, + options: FormattingOptions, + ): List = + if (isLibraryDocument(uri)) { + emptyList() + } else { + XdkLexical.format( + content, + FormattingConfig.resolve(options, editorFormattingConfig), + options, + ) + } + + override fun formatRange( + uri: String, + content: String, + range: Range, + options: FormattingOptions, + ): List = + if (isLibraryDocument(uri)) { + emptyList() + } else { + XdkLexical.format( + content, + FormattingConfig.resolve(options, editorFormattingConfig), + options, + range, + ) + } + + override fun onTypeFormatting( + uri: String, + line: Int, + column: Int, + ch: String, + options: FormattingOptions, + ): List = + currentText(uri) + ?.let { text -> + XdkLexical.format( + text, + FormattingConfig.resolve(options, editorFormattingConfig), + options, + Range( + Position(line, 0), + Position(line, text.lines().getOrNull(line)?.length ?: column), + ), + wrapLines = false, + ) + }.orEmpty() + + override fun getDocumentLinks( + uri: String, + content: String, + ): List { + val module = module(uri) + val document = module?.document(uri) + val links = + document + ?.takeIf { it.ast?.source?.toRawString() == content } + ?.semantics + ?.sourceLinks + .orEmpty() + .mapNotNull { link -> + module?.sourceUri(link.target.sourceName)?.let { target -> + DocumentLink(link.range.toRange(), target, "Open imported source") + } + } + return (XdkLexical.links(content) + links).distinctBy { it.range }.sortedBy { it.range.start.line } + } + + override fun getCodeLenses(uri: String): List = getCodeLensesAsync(uri, true).join() + + override fun getCodeLensesAsync( + uri: String, + references: Boolean, + ): CompletableFuture> { + val run = runLenses(uri) + return if (references && hasProject(uri) && !isLibraryDocument(uri)) { + projectQuery(ProjectQueryKey(uri, ProjectQueryKind.CODE_LENSES), emptyList()) { + run + it.referenceLenses(uri) + } + } else { + CompletableFuture.completedFuture(run) + } + } + + private fun runLenses(uri: String): List = + analysis(uri) + ?.symbols + .orEmpty() + .filter { it.kind == SymbolInfo.SymbolKind.MODULE } + .map { symbol -> + val at = symbol.location + CodeLens( + Range( + Position(at.startLine, at.startColumn), + Position(at.endLine, at.endColumn), + ), + CodeLensCommand( + "▶ Run ${symbol.name}", + "xtc.runModule", + listOf(uri, symbol.name), + ), + ) + } + + override fun getLinkedEditingRanges( + uri: String, + line: Int, + column: Int, + ): LinkedEditingRanges? { + if (isLibraryDocument(uri)) return null + val model = + analysis(uri)?.semantics?.takeIf { it.status == SemanticModel.Status.COMPLETE } + ?: return null + model.importAt(line, column)?.let { alias -> + val ranges = (listOf(alias.declaration) + alias.uses).distinct().sortedBy { it.start } + return ranges.takeIf { it.size > 1 }?.let { LinkedEditingRanges(it.map { range -> range.toRange() }) } + } + val symbol = + model.symbolAt(line, column)?.takeIf { + // Lambda arguments are positional and their names are local to this source. + // Method parameters have callable slots and may have named callers elsewhere; + // those require the complete graph rename operation, even for private methods. + it.renameable && it.declarationSource == model.sourceName && + ( + it.kind == SemanticModel.SymbolKind.VARIABLE || + (it.kind == SemanticModel.SymbolKind.PARAMETER && it.id !in model.parameters) + ) + } ?: return null + val ranges = + model.occurrences + .filter { it.symbol == symbol.id && it.name == symbol.name } + .map { it.range.toRange() } + .distinct() + .sortedWith(compareBy({ it.start.line }, { it.start.column })) + return ranges.takeIf { it.size > 1 }?.let { LinkedEditingRanges(it) } + } + + override fun getInlayHints( + uri: String, + range: Range, + ): List = analysis(uri)?.semantics?.let { XdkPresentation.hints(it, range) }.orEmpty() + + /** + * Blocks and declarations that span more than one line. An editor offers a fold per region, so + * a region per expression would be noise rather than help. + */ + override fun getFoldingRanges(uri: String): List = XdkAst.foldingRegions(analysis(uri)?.ast) + + /** + * Expanding a selection walks out through the tree, which is exactly what the parent chain of + * the innermost node containing the cursor is. + */ + override fun getSelectionRanges( + uri: String, + positions: List, + ): List { + val ast = analysis(uri)?.ast + return positions.map { position -> + XdkAst.chainAt(ast, position.line, position.column).map(XdkAst::rangeOf).distinct().fold(null as SelectionRange?) { + parent, + range, + -> + SelectionRange(range, parent) + } ?: SelectionRange(Range(position, position)) + } + } + + /** Compile the discovered/configured graph on the worker, including unopened modules. */ + override fun findWorkspaceSymbols(query: String): List { + val root = synchronized(lifecycle) { project.buildOrder().firstOrNull()?.uri } + return if (root == null) { + completed.values + .flatMap { it.documents.values } + .flatMap { flatten(it.symbols) } + .filter { query.isBlank() || it.name.contains(query, ignoreCase = true) } + } else { + projectQuery(ProjectQueryKey(root, ProjectQueryKind.SYMBOLS), emptyList()) { + it.symbols(query) + }.join() + } + } + + /** + * Detached current diagnostics for every configured/discovered root, including closed files. + */ + fun workspaceDiagnosticsAsync(): CompletableFuture> { + val root = + synchronized(lifecycle) { project.buildOrder().firstOrNull()?.uri } + ?: return CompletableFuture.completedFuture(emptyList()) + return projectQuery( + ProjectQueryKey(root, ProjectQueryKind.DIAGNOSTICS, requestId = UUID.randomUUID()), + emptyList(), + ) { + it.diagnostics() + } + } + + private fun flatten(symbols: List): List = + symbols.flatMap { + listOf(it) + flatten(it.children) + } + + // ----- what the name resolved to ------------------------------------------------------------- + + /** + * Resolve within the current module snapshot. Compiled dependencies with no source location + * cannot supply a navigable declaration. + */ + override fun findDefinition( + uri: String, + line: Int, + column: Int, + ): Location? { + if (module(uri) == null && hasProject(uri)) { + return workspaceNavigation(uri)?.definition(uri, line, column) + } + val module = module(uri) ?: return null + val declaration = + module.document(uri)?.semantics?.definitionLocationAt(line, column) ?: return null + return module.sourceUri(declaration.sourceName)?.let { + locationOf(it, declaration.range.toRange()) + } + } + + override fun findDeclaration( + uri: String, + line: Int, + column: Int, + ): Location? = findDeclarations(uri, line, column).singleOrNull() + + override fun findDeclarations( + uri: String, + line: Int, + column: Int, + ): List { + if (module(uri) == null && hasProject(uri)) { + return workspaceNavigation(uri)?.declarations(uri, line, column).orEmpty() + } + val module = module(uri) ?: return emptyList() + return module.locations( + module + .document(uri) + ?.semantics + ?.declarationLocationsAt(line, column) + .orEmpty(), + ) + } + + override fun findMonikers( + uri: String, + line: Int, + column: Int, + ): List = findMonikersAsync(uri, line, column).join() + + override fun findMonikersAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture> { + if (isLibraryDocument(uri)) return libraryMonikers(uri, line, column) + val current = module(uri) + if (current == null && hasProject(uri)) { + return projectQuery(ProjectQueryKey(uri, ProjectQueryKind.MONIKERS), emptyList()) { + it.navigation()?.monikers(uri, line, column).orEmpty() + } + } + return CompletableFuture.completedFuture( + current + ?.document(uri) + ?.semantics + ?.monikersAt(line, column) + .orEmpty(), + ) + } + + override fun findReferences( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): List = findReferencesAsync(uri, line, column, includeDeclaration).join() + + override fun findReferencesAsync( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): CompletableFuture> = + if (synchronized(lifecycle) { project.scope(uri) != null }) { + projectQuery(ProjectQueryKey(uri, ProjectQueryKind.REFERENCES), emptyList()) { + it.references(uri, line, column, includeDeclaration) + } + } else { + CompletableFuture.completedFuture( + moduleReferences(uri, line, column, includeDeclaration), + ) + } + + private fun moduleReferences( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): List { + val module = module(uri) ?: return emptyList() + val symbol = module.document(uri)?.semantics?.symbolAt(line, column) ?: return emptyList() + return module.documents + .flatMap { (sourceUri, document) -> + document.semantics + ?.occurrences + .orEmpty() + .filter { + it.symbol == symbol.id && + (includeDeclaration || it.role != SemanticModel.Role.DECLARATION) + }.map { locationOf(sourceUri, it.range.toRange()) } + }.distinct() + .sortedWith(compareBy(Location::uri, Location::startLine, Location::startColumn)) + } + + override fun prepareRename( + uri: String, + line: Int, + column: Int, + ): PrepareRenameResult? { + val model = module(uri)?.takeIf { it.succeeded }?.document(uri)?.semantics ?: return null + if (hasProject(uri)) { + model.importAt(line, column)?.let { alias -> + val position = SemanticModel.Position(line, column) + val range = (alias.uses + alias.declaration).single { position in it } + return PrepareRenameResult(range.toRange(), alias.name) + } + } + val symbol = + model.symbolAt(line, column)?.takeIf { + it.renameable || isProjectTarget(uri, model, it) + } ?: return null + if ( + symbol.kind == SemanticModel.SymbolKind.MODULE && + model.occurrenceAt(line, column)?.name != symbol.name + ) { + return null + } + val range = + model.occurrences + .firstOrNull { + it.symbol == symbol.id && + it.name != "super" && + SemanticModel.Position(line, column) in it.range + }?.range ?: return null + return PrepareRenameResult(range.toRange(), symbol.name) + } + + /** + * Eligibility is provisional; the worker proves binding/dispatch preservation before editing. + */ + private fun isProjectTarget( + uri: String, + model: SemanticModel, + symbol: SemanticModel.Symbol, + ): Boolean = + ( + symbol.kind == SemanticModel.SymbolKind.PACKAGE && + symbol.declarationSource == null && + symbol.dependency == null && + hasProject(uri) + ) || + ( + symbol.name != "construct" && + ( + symbol.kind != SemanticModel.SymbolKind.PARAMETER || + symbol.id in model.parameters + ) && + symbol.kind in + setOf( + SemanticModel.SymbolKind.METHOD, + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.PROPERTY, + SemanticModel.SymbolKind.PACKAGE, + SemanticModel.SymbolKind.MODULE, + SemanticModel.SymbolKind.PARAMETER, + ) && + ( + symbol.declarationSource + ?: model.parameters[symbol.id] + ?.method + ?.let(model::symbol) + ?.declarationSource + )?.let { + synchronized(lifecycle) { + project.scope(uri) != null && project.scope(it) != null + } + } == true + ) + + /** Hosts that own persistent source settings can accept the graph replacement with the edit. */ + fun renameProposalAsync( + uri: String, + line: Int, + column: Int, + newName: String, + ): CompletableFuture = + if (isProjectRename(uri, line, column)) { + projectQuery(ProjectQueryKey(uri, ProjectQueryKind.RENAME_PROPOSAL), null) { + it.renameProposal(uri, line, column, newName) + } + } else { + renameAsync(uri, line, column, newName).thenApply { it?.let(::XdkRenameProposal) } + } + + /** Preserve automatic resource roots when replacing only the graph's binary inputs. */ + internal fun sourceModuleInputs(): List = synchronized(lifecycle) { project.buildOrder() } + + /** Detached effective inputs for host settings views; never compiler-owned objects. */ + internal fun effectiveSourceModules(): List = + synchronized(lifecycle) { + project.buildOrder().map { module -> + XdkSourceModule( + module.name, + module.uri, + module.dependencies, + XdkResources.roots(module.root, module.resourceFiles).map { + it.toURI().toString() + }, + ) + } + } + + /** + * Compiler-proven edits for IDE file-tree operations; no filesystem or configuration writes. + */ + fun renameFilesAsync(files: Map): CompletableFuture = + renameFilesProposalAsync(files).mapCancellable { proposal -> + proposal?.takeIf { it.sourceModules == null }?.edit?.let { edit -> + val originals = files.keys.mapNotNull(XdkSources::file).toSet() + edit.copy(renames = edit.renames.filterKeys { XdkSources.file(it) !in originals }) + } + } + + /** Full file-move transaction with an optional host-owned source graph replacement. */ + fun renameFilesProposalAsync(files: Map): CompletableFuture { + // Refuse links before canonicalization loses the requested file identity or captures a + // source snapshot for an alias. The compiler proof below performs the remaining checks. + if ( + files.keys.any { uri -> + runCatching { Files.isSymbolicLink(Path.of(URI(uri))) }.getOrDefault(true) + } + ) { + return CompletableFuture.completedFuture(null) + } + val scope = + synchronized(lifecycle) { + files.keys.firstNotNullOfOrNull { uri -> + project.scope(uri) + ?: XdkSources.file(uri)?.toPath()?.let { path -> + project.modules.values + .firstOrNull { it.root.toPath().startsWith(path) } + ?.uri + } + } + } ?: return CompletableFuture.completedFuture(null) + return projectQuery(ProjectQueryKey(scope, ProjectQueryKind.FILE_RENAME), null) { + it.renameFilesProposal(files) + } + } + + override fun getCodeActions( + uri: String, + range: Range, + diagnostics: List, + ): List = getCodeActionsAsync(uri, range, diagnostics).join() + + override fun getCodeActionsAsync( + uri: String, + range: Range, + diagnostics: List, + ): CompletableFuture> = + if (hasProject(uri)) { + projectQuery(ProjectQueryKey(uri, ProjectQueryKind.CODE_ACTIONS, range), emptyList()) { + it.codeActions(uri, range) + } + } else { + CompletableFuture.completedFuture( + if (isLibraryDocument(uri)) { + emptyList() + } else { + analysis(uri) + ?.let { analysis -> + val model = analysis.semantics + val text = currentText(uri) + if (model == null || text == null) emptyList() else XdkDocumentation.actions(uri, text, range, model) + }.orEmpty() + }, + ) + } + + override fun rename( + uri: String, + line: Int, + column: Int, + newName: String, + ): WorkspaceEdit? = renameAsync(uri, line, column, newName).join() + + override fun renameAsync( + uri: String, + line: Int, + column: Int, + newName: String, + ): CompletableFuture { + if (isProjectRename(uri, line, column)) { + return projectQuery( + ProjectQueryKey(uri, ProjectQueryKind.RENAME), + null, + ) { + it.rename(uri, line, column, newName) + } + } + val (request, previous) = + synchronized(lifecycle) { + if (closed) { + return CompletableFuture.failedFuture( + IllegalStateException("XDK adapter is closed"), + ) + } + if (discovery.get().problem != null) return CompletableFuture.completedFuture(null) + val compilation = + requests[analysisScope(uri)] ?: return CompletableFuture.completedFuture(null) + val module = + completed[compilation.scope]?.takeIf { it.succeeded } + ?: return CompletableFuture.completedFuture(null) + if (prepareRename(uri, line, column) == null) { + return CompletableFuture.completedFuture(null) + } + val request = + RenameRequest( + compilation, + module, + uri, + Position(line, column), + newName, + queueTrace, + ::runRename, + ) + request.result.whenComplete { _, _ -> + if (request.result.isCancelled) { + synchronized(lifecycle) { + renames.remove(uri, request) + compiles.remove(request.task) + } + } + } + val previous = renames.put(uri, request) + previous?.let { compiles.remove(it.task) } + compiles.execute(request.task.ready()) + request to previous + } + previous?.result?.cancel(false) + return request.result + } + + private fun isProjectRename( + uri: String, + line: Int, + column: Int, + ): Boolean = + synchronized(lifecycle) { + module(uri)?.document(uri)?.semantics?.let { model -> + model.importAt(line, column) != null || + model.symbolAt(line, column)?.let { isProjectTarget(uri, model, it) } == true + } == true && project.scope(uri) != null + } + + private fun isStale(request: RenameRequest): Boolean = + renames[request.uri] !== request || + request.result.isCancelled || + isStale(request.compilation) + + /** Both proof attempts stay on the compiler worker and never replace the live analysis. */ + private fun runRename(request: RenameRequest) { + try { + val edit = proveRename(request) + synchronized(lifecycle) { + if (isStale(request)) throw CancellationException() + } + request.result.complete(edit) + } catch (_: CancellationException) { + request.result.cancel(false) + } catch (e: Exception) { + request.result.completeExceptionally(e) + } catch (e: Error) { + request.result.completeExceptionally(e) + throw e + } finally { + renames.remove(request.uri, request) + } + } + + private fun proveRename(request: RenameRequest): WorkspaceEdit? { + val module = request.module + + fun compile(texts: Map): CompilerRenameFacts? { + if (isStale(request)) throw CancellationException() + val heard = ErrorList() + val errors = ErrorListener.cancellable(heard) { isStale(request) } + val dependencies = module.inputs.open() + val repository = dependencies.repository + val sources = + module.sourceInputs?.let { + XdkSources.replay( + requireNotNull(XdkSources.file(request.compilation.scope)), + it, + texts, + ) + } + val compilation = + if (sources == null) { + compiler.compileSource( + Source(texts.getValue(request.compilation.uri), request.compilation.uri), + repository, + errors, + ) + } else { + compiler.compileTree(sources, repository, errors) + } + if (isStale(request)) throw CancellationException() + return if (compilation.succeeded() && !heard.hasSeriousErrors()) { + compilation.renameFacts(dependencies) + } else { + null + } + } + val source = module.document(request.uri)?.semantics?.sourceName ?: return null + val before = compile(module.sourceTexts) ?: return null + val plan = + XdkRename.plan( + before, + module.sourceTexts, + source, + request.position.line, + request.position.column, + request.name, + ) ?: return null + val after = compile(plan.proposed) ?: return null + if (!XdkRename.preservesBindings(before, after, plan)) return null + // A closed file can change without a watcher event. Refuse edits against that old snapshot. + if (module.sourceInputs != null) { + val current = + try { + captureSources(request.compilation) { isStale(request) }?.inputs + } catch (_: IOException) { + return null + } + if (current != module.sourceInputs) return null + } + return WorkspaceEdit( + plan.edits.keys.associate { name -> + (module.sourceUri(name) ?: return null) to plan.textEdits(name) + }, + versioned = true, + ) + } + + private fun hasProject(uri: String): Boolean = synchronized(lifecycle) { project.scope(uri) != null } + + private fun workspaceNavigation(uri: String): XdkWorkspaceNavigation? = + projectQuery( + ProjectQueryKey(uri, ProjectQueryKind.NAVIGATION), + null, + ) { + it.navigation() + }.join() + + override fun prepareTypeHierarchy( + uri: String, + line: Int, + column: Int, + ): List = + if (hasProject(uri)) { + workspaceNavigation(uri)?.prepareTypes(uri, line, column).orEmpty() + } else { + module(uri)?.hierarchy?.prepare(uri, line, column).orEmpty() + } + + override fun findTypeDefinition( + uri: String, + line: Int, + column: Int, + ): Location? = findTypeDefinitions(uri, line, column).firstOrNull() + + override fun findTypeDefinitions( + uri: String, + line: Int, + column: Int, + ): List { + if (module(uri) == null && hasProject(uri)) { + return workspaceNavigation(uri)?.typeDefinitions(uri, line, column).orEmpty() + } + val module = module(uri) ?: return emptyList() + return module.locations( + module + .document(uri) + ?.semantics + ?.typeDefinitionLocationsAt(line, column) + .orEmpty(), + ) + } + + override fun findImplementation( + uri: String, + line: Int, + column: Int, + ): List { + if (hasProject(uri)) { + return workspaceNavigation(uri)?.implementations(uri, line, column).orEmpty() + } + val module = module(uri) ?: return emptyList() + return module.locations( + module + .document(uri) + ?.semantics + ?.implementationLocationsAt(line, column) + .orEmpty(), + ) + } + + private fun ModuleAnalysis.locations(locations: List): List = + locations + .mapNotNull { target -> + sourceUri(target.sourceName)?.let { locationOf(it, target.range.toRange()) } + }.distinct() + .sortedWith(compareBy(Location::uri, Location::startLine, Location::startColumn)) + + override fun getSupertypes(item: TypeHierarchyItem): List = + if (hasProject(item.uri)) { + workspaceNavigation(item.uri)?.parents(item).orEmpty() + } else { + module(item.uri)?.hierarchy?.supertypes(item).orEmpty() + } + + override fun getSubtypes(item: TypeHierarchyItem): List = + if (hasProject(item.uri)) { + workspaceNavigation(item.uri)?.children(item).orEmpty() + } else { + module(item.uri)?.hierarchy?.subtypes(item).orEmpty() + } + + override fun prepareCallHierarchy( + uri: String, + line: Int, + column: Int, + ): List = + if (hasProject(uri)) { + workspaceNavigation(uri)?.prepareCalls(uri, line, column).orEmpty() + } else { + module(uri)?.calls?.prepare(uri, line, column).orEmpty() + } + + override fun getIncomingCalls(item: CallHierarchyItem): List = + if (hasProject(item.uri)) { + workspaceNavigation(item.uri)?.incoming(item).orEmpty() + } else { + module(item.uri)?.calls?.incoming(item).orEmpty() + } + + override fun getOutgoingCalls(item: CallHierarchyItem): List = + if (hasProject(item.uri)) { + workspaceNavigation(item.uri)?.outgoing(item).orEmpty() + } else { + module(item.uri)?.calls?.outgoing(item).orEmpty() + } + + private fun SemanticModel.Range.toRange(): Range = Range(Position(start.line, start.column), Position(end.line, end.column)) + + private fun locationOf( + uri: String, + range: Range, + ): Location = Location(uri, range.start.line, range.start.column, range.end.line, range.end.column) + + // ----- copied cursor facts ------------------------------------------------------------------- + + override fun getCompletions( + uri: String, + line: Int, + column: Int, + triggerCharacter: String?, + ): List = getCompletionsAsync(uri, line, column, triggerCharacter).join() + + override fun getCompletionsAsync( + uri: String, + line: Int, + column: Int, + triggerCharacter: String?, + ): CompletableFuture> { + val compilation = synchronized(lifecycle) { requests[analysisScope(uri)] } + val text = compilation?.overlays?.get(uri) + return analyzeAtAsync(CursorKey(uri, CursorKind.COMPLETION), Position(line, column)) + .composeCancellable { partial -> + val ordinary = partial?.let(XdkCursorQueries::completions).orEmpty() + val site = partial?.sites?.singleOrNull() + val prefix = site?.memberPrefix + if (compilation == null || isStale(compilation)) { + CompletableFuture.completedFuture(emptyList()) + } else if (text == null || site?.kind != PartialSemanticModel.Kind.NAME || + prefix == null || prefix.text.length < 2 || !hasProject(uri) + ) { + CompletableFuture.completedFuture(ordinary) + } else { + projectQuery(ProjectQueryKey(uri, ProjectQueryKind.COMPLETION_IMPORTS), emptyList()) { + if (isStale(compilation)) return@projectQuery emptyList() + val imports = it.importCompletions(uri, text, prefix, ordinary.map { item -> item.label }.toSet()) + if (isStale(compilation)) emptyList() else ordinary + imports + } + } + } + } + + override fun getInlineCompletionsAsync( + uri: String, + position: Position, + context: InlineCompletionContext, + ): CompletableFuture> { + val compilation = + synchronized(lifecycle) { requests[analysisScope(uri)] } + ?: return CompletableFuture.completedFuture(emptyList()) + val text = compilation.overlays[uri] ?: return CompletableFuture.completedFuture(emptyList()) + if (isLibraryDocument(uri) || !XdkInlineCompletions.eligible(text, position, context)) { + return CompletableFuture.completedFuture(emptyList()) + } + return analyzeAtAsync(CursorKey(uri, CursorKind.INLINE_COMPLETION), position) + .mapCancellable { partial -> + if (partial == null || isStale(compilation)) { + emptyList() + } else { + XdkInlineCompletions.project(text, position, context, XdkCursorQueries.completions(partial)) + } + } + } + + override fun getSignatureHelp( + uri: String, + line: Int, + column: Int, + ): SignatureHelp? = getSignatureHelpAsync(uri, line, column).join() + + override fun getSignatureHelpAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture { + if (line < 0 || column < 0) return CompletableFuture.completedFuture(null) + val position = SemanticModel.Position(line, column) + analysis(uri)?.semantics?.let { model -> + XdkCursorQueries.signatureHelp(model, position)?.let { + return CompletableFuture.completedFuture(it) + } + } + return analyzeAtAsync(CursorKey(uri, CursorKind.SIGNATURE), Position(line, column)) + .mapCancellable { it?.let { model -> XdkCursorQueries.signatureHelp(model, position) } } + } + + private val queueTrace = CompilerQueueTrace() + private val lifecycle = Any() + private var closed = false + + /** Replaced only under lifecycle; each request captures its immutable dependency set. */ + private var dependencies = XdkDependencies(emptyList()) + private var project = XdkProject(emptyList()) + private val builds = ConcurrentHashMap() + private val scheduled = mutableMapOf>() + private val debouncer = + ScheduledThreadPoolExecutor(1) { work -> + Thread(work, "xtc-debounce").apply { isDaemon = true } + }.apply { removeOnCancelPolicy = true } + + /** + * Only current, completed module analyses belong here; a member edit drops all previous views. + */ + private val completed = ConcurrentHashMap() + private val overlays = linkedMapOf() + private val scopes = mutableMapOf() + private val requests = ConcurrentHashMap() + private val cursors = ConcurrentHashMap() + private val renames = ConcurrentHashMap() + private val projectQueries = ConcurrentHashMap>() + private val libraryQueries = ConcurrentHashMap, CompilerQueueTrace.Work>() + private val diagnosticCache = XdkDiagnosticIndex() + private val navigationIndex = XdkNavigationIndex() + + /** + * A ThreadPoolExecutor rather than Executors.newSingleThreadExecutor, because the latter wraps + * the queue where nothing can see it, and how many documents are waiting is the number that + * says whether one-at-a-time has become a problem. + */ + private val compiles = + ThreadPoolExecutor( + 1, + 1, + 0L, + TimeUnit.MILLISECONDS, + LinkedBlockingQueue(), + ThreadFactory { r -> Thread(r, "xtc-compile").apply { isDaemon = true } }, + ) + + private companion object { + const val SOURCE = "xtc" + const val SHUTDOWN_SECONDS = 5L + const val DEBOUNCE_MILLIS = 100L + + /** Translate a UTF-16 editor position without consuming or modifying the compiler input. */ + fun cursorPosition( + source: Source, + position: Position, + errors: ErrorListener, + ): Long? { + if (position.line < 0 || position.column < 0) return null + val cursor = source.clone() + while ( + cursor.hasNext() && + ( + cursor.line < position.line || + (cursor.line == position.line && cursor.offset < position.column) + ) + ) { + if (errors.isAbortDesired) throw CancellationException() + cursor.next() + } + return cursor.position.takeIf { + cursor.line == position.line && cursor.offset == position.column + } + } + + fun analyzeIncomplete( + source: Source, + sources: ModuleInfo?, + cursor: Long, + repository: ModuleRepository?, + errors: ErrorListener, + ): EmbeddingSupport.PartialAnalysis { + XdkLibraries.configure() + val support = EmbeddingSupport.instance() + return if (sources == null) { + support.analyzeIncomplete(source, cursor, repository, errors) + } else { + support.analyzeIncomplete( + sources, + checkNotNull(XdkSources.file(source.fileName)), + cursor, + repository, + errors, + ) + } + } + } } -} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt new file mode 100644 index 00000000000..3375e575dc0 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt @@ -0,0 +1,179 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.XvmStructure +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypedefStatement +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement +import org.xvm.lsp.adapter.FoldingRange +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.model.Location +import java.util.IdentityHashMap + +/** + * Reading a compiled document's AST for the questions an editor asks about a position. + * + * The AST exposes children and source spans for folding and selection. Semantic queries use the + * compiler-owned snapshot cached by [XdkAdapter]. + */ +internal object XdkAst { + /** + * Attempt-local structure ownership; binary structures cannot acquire a guessed source span. + */ + fun declarationLocations( + roots: Collection, + sourceUris: Map, + ): Map { + val locations = IdentityHashMap() + + fun visit(node: AstNode) { + val declaration = + when (node) { + is TypeCompositionStatement -> node.component to node.nameToken + is MethodDeclarationStatement -> node.component to node.nameToken + is PropertyDeclarationStatement -> node.component to node.nameToken + is TypedefStatement -> node.component to node.nameToken + else -> null + } + val uri = node.source?.fileName?.let(sourceUris::get) + if (declaration != null && uri != null) { + val (structure, token) = declaration + if (structure != null && token != null) { + val range = spanOf(token.startPosition, token.endPosition) + locations[structure] = + Location( + uri, + range.start.line, + range.start.column, + range.end.line, + range.end.column, + ) + locations[structure.identityConstant] = locations.getValue(structure) + } + } + node.childNodes().forEach(::visit) + } + roots.forEach(::visit) + return locations + } + + /** Each source keeps its own structural root even when module assembly nests the trees. */ + fun rootsBySource(root: AstNode?): Map = + buildMap { + fun visit(node: AstNode) { + node.source?.fileName?.let { putIfAbsent(it, node) } + node.childNodes().forEach(::visit) + } + root?.let(::visit) + } + + /** + * The chain of nodes containing a position, outermost first, innermost last. + * + * Empty if the position is outside the document or the document did not parse. + */ + fun chainAt( + root: AstNode?, + line: Int, + column: Int, + ): List = + generateSequence(root?.takeIf { it.contains(line, column) }) { node -> + // Recovery and generated wrappers can overlap. At a sibling boundary prefer the + // node starting there; never expand into a child outside its written parent's span. + node + .childNodes() + .filter { + it.belongsTo(requireNotNull(root)) && it.contains(line, column) && + it.startPosition >= node.startPosition && it.endPosition <= node.endPosition + }.minWithOrNull(compareByDescending { it.startPosition }.thenBy { it.endPosition }) + }.toList() + + /** + * The regions worth collapsing: anything that spans more than one line and is a block or a + * declaration. An editor offers a fold per region, so a region per expression would be noise. + */ + fun foldingRegions(root: AstNode?): List { + val found = linkedMapOf, FoldingRange>() + val lines = + root + ?.source + ?.toRawString() + ?.lines() + .orEmpty() + + fun walk(node: AstNode) { + if (root != null && !node.belongsTo(root)) return + if ( + node is StatementBlock || + node is TypeCompositionStatement || + node is IncompleteDeclarationStatement + ) { + val start = lineOf(node.startPosition) + val end = lineOf(node.endPosition) + if (end > start) { + val column = columnOf(node.endPosition) + // Keep the heading line and a real closing brace visible. An unfinished + // region ends at its actual source boundary; never invent a delimiter. + val endCharacter = + if (lines.getOrNull(end)?.getOrNull(column - 1) == '}') { + column - 1 + } else { + column + } + found.putIfAbsent( + start to end, + FoldingRange(start, end, endCharacter = endCharacter), + ) + } + } + node.childList().forEach(::walk) + } + root?.let(::walk) + return found.values.toList() + } + + fun rangeOf(node: AstNode): Range = spanOf(node.startPosition, node.endPosition) + + fun spanOf( + start: Long, + end: Long, + ): Range = + Range( + start = Position(lineOf(start), columnOf(start)), + end = Position(lineOf(end), columnOf(end)), + ) + + private fun lineOf(position: Long): Int = Source.calculateLine(position) + + private fun columnOf(position: Long): Int = Source.calculateOffset(position) + + // Parent pointers are installed during compilation. Before that, children without their own + // Source inherit the source of the syntax tree being traversed. + private fun AstNode.belongsTo(root: AstNode): Boolean = source == null || source === root.source + + /** + * [AstNode.children] is an iterator that supports replacement during a compiler pass; a reader + * wants a list, taken once. + */ + private fun AstNode.childList(): List = childNodes().toList() + + private fun AstNode.contains( + line: Int, + column: Int, + ): Boolean { + val startLine = lineOf(startPosition) + val endLine = lineOf(endPosition) + if (line !in startLine..endLine) { + return false + } + if (line == startLine && column < columnOf(startPosition)) { + return false + } + return !(line == endLine && column > columnOf(endPosition)) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt new file mode 100644 index 00000000000..272637942cb --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt @@ -0,0 +1,111 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.ConstantPool +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.ModuleConstant +import org.xvm.asm.constants.MultiMethodConstant +import org.xvm.lsp.adapter.ReadOnlyDocument +import java.io.File +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.security.MessageDigest +import java.util.HexFormat + +/** Navigation-only snapshots: attachments never become writable project modules. */ +internal object XdkAttachedSources { + data class Index( + val declarations: Map, + val documents: Map, + ) + + private val directory by lazy { Files.createTempDirectory("xtc-attached-sources-") } + + fun read( + dependency: XdkDependency, + roots: List, + ): Index { + val binary = dependency.open() + return ConstantPool.withPool(binary.constantPool).use { + val identities = + binary.constantPool.constants + .toList() + .filterIsInstance() + .filter { it.moduleConstant?.name == dependency.module } + val files = + identities + .mapNotNull { identity -> + val component = identity.component ?: return@mapNotNull null + val owner = component as? ClassStructure ?: component.getContainingClass(false) + owner?.sourcePath?.value + }.distinct() + .associateWith { sourcePath -> + val relative = Path.of(sourcePath).normalize() + require( + !relative.isAbsolute && !relative.startsWith(".."), + ) { "Attachment requires a relative artifact source path: $sourcePath" } + val source = roots.asSequence().map { it.toPath().resolve(relative).toFile() }.firstOrNull(File::isFile) + requireNotNull(source) { "No attached source for ${dependency.module}: $sourcePath" } + val text = source.readText() + val revision = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(text.toByteArray())) + val snapshot = directory.resolve(dependency.revision).resolve(revision).resolve(relative) + Files.createDirectories(snapshot.parent) + synchronized(directory) { + if (!Files.exists(snapshot)) { + Files.writeString(snapshot, text) + check(snapshot.toFile().setReadOnly()) { "Cannot protect attached source $sourcePath" } + } + } + // URI authorities are case-insensitive (and lowercased by VS Code). + // Module identities are case-sensitive, so retain them in the path. + val uri = + URI( + XdkLibrarySources.SCHEME, + "attached", + "/${dependency.module}/${dependency.revision}/$revision/$sourcePath", + null, + null, + ).toASCIIString() + Triple( + snapshot + .toFile() + .canonicalFile + .toURI() + .toString(), + ReadOnlyDocument(uri, text), + libraryDeclarations(text, sourcePath), + ) + } + val declarations = + identities + .mapNotNull { identity -> + val component = identity.component ?: return@mapNotNull null + val owner = component as? ClassStructure ?: component.getContainingClass(false) + val source = files[owner?.sourcePath?.value] ?: return@mapNotNull null + val namespace = + generateSequence(identity) { it.namespace } + .takeWhile { it !is ModuleConstant } + .filterNot { it is MultiMethodConstant } + .map { it.name } + .toList() + .asReversed() + val method = component as? MethodStructure + method?.sourceText?.takeIf(String::isNotBlank)?.let { original -> + require( + source.second.text.contains(original.trim()), + ) { "Attached source does not match ${dependency.module}: ${owner?.sourcePath?.value}" } + } + val candidates = source.third.filter { it.path == namespace } + selectLibraryDeclaration( + candidates, + method?.takeIf { it.sourceText != null }?.sourceLineNumber, + ) { it.firstLine..it.lastLine } + ?.let { identity.position to SemanticModel.SourceLocation(source.first, it.range) } + }.toMap() + require(declarations.isNotEmpty()) { "No matching artifact declarations in attached sources for ${dependency.module}" } + Index(declarations, files.values.associate { it.first to it.second }) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt new file mode 100644 index 00000000000..b78336879ca --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt @@ -0,0 +1,145 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.asm.constants.ClassConstant +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.ModuleConstant +import org.xvm.asm.constants.PackageConstant +import org.xvm.asm.constants.TypedefConstant +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Lexer +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.util.ExecutionTrace + +/** Detached import candidates; only a successful whole-graph repair proof can publish an edit. */ +internal object XdkAutoImports { + data class Target( + val name: String, + val module: String, + val path: String, + ) + + private val bundled by lazy { + XdkLibraries.moduleNames + .flatMap { module -> + XdkLibraries.module(module).constantPool.constants.mapNotNull(::target).filter { + it.module == module + } + }.distinct() + .groupBy { it.name } + } + + fun targets( + name: String, + facts: CompilerRenameFacts, + ): List = + (facts.imports.filter { it.name == name } + bundled[name].orEmpty()) + .distinct() + .sortedWith(compareBy(Target::module, Target::path)) + + fun matchingTargets( + prefix: String, + facts: CompilerRenameFacts, + ): List = + (facts.imports + bundled.filterKeys { it.startsWith(prefix) }.values.flatten()) + .filter { it.name.startsWith(prefix) } + .distinct() + .sortedWith(compareBy(Target::name, Target::module, Target::path)) + + internal fun target(constant: Constant): Target? { + if (constant !is ClassConstant && constant !is TypedefConstant) return null + val identity = constant as IdentityConstant + if ( + identity.path.any { + ( + it !is ModuleConstant && + it !is PackageConstant && + it !is ClassConstant && + it !is TypedefConstant + ) || it.component?.access != Access.PUBLIC + } + ) { + return null + } + return Target(identity.name, identity.moduleConstant.name, identity.pathString) + } + + fun edit( + text: String, + owner: String, + target: Target, + ): XdkRename.Edit? { + val errors = ErrorList() + val root = + try { + ExecutionTrace.api("Parser.parseSource(auto-imports)") { + Parser(Source(text), errors).parseSource() + } + } catch (_: CompilerException) { + return null + } + if (errors.hasSeriousErrors()) return null + val tokens = + ExecutionTrace.api("Lexer.lex(auto-imports)") { + Lexer(Source(text), errors).asSequence().toList() + } + if (errors.hasSeriousErrors()) return null + val type = + root.childNodes().filterIsInstance().singleOrNull() + ?: return null + val newline = if ("\r\n" in text) "\r\n" else "\n" + val insertion = + if (type.category.id == Token.Id.MODULE) { + val position = type.ensureBody().startPosition + val at = + XdkRename.offset( + text, + SemanticModel.Position( + Source.calculateLine(position), + Source.calculateOffset(position), + ), + ) ?: return null + if (text.getOrNull(at) != '{') return null + at + 1 + } else { + // A member file may declare imports before its class; the compiler owns their + // scope. + 0 + } + val prefix: String + val declaration: String + when (target.module) { + owner -> { + prefix = "" + declaration = "" + } + + "ecstasy.xtclang.org" -> { + prefix = "ecstasy." + declaration = "" + } + + else -> { + val names = tokens.mapTo(hashSetOf()) { it.valueText } + val base = target.module.substringBefore('.').replaceFirstChar { it.lowercase() } + val alias = + generateSequence(0) { it + 1 } + .map { if (it == 0) base else "$base$it" } + .first { it !in names && XdkRename.identifier(it) } + prefix = "$alias." + declaration = "package $alias import ${target.module};$newline" + } + } + val imports = declaration + "import $prefix${target.path};$newline" + return XdkRename.Edit( + insertion, + insertion, + if (insertion == 0) imports else "$newline$imports", + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt new file mode 100644 index 00000000000..f1e09a42a65 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt @@ -0,0 +1,190 @@ +package org.xvm.lsp.adapter.xdk + +import com.google.gson.JsonObject +import java.io.File +import java.io.IOException + +/** Evaluated, versioned host input. No build-script parsing, Gradle execution or IDE dependency. */ +internal class XdkBuildModel private constructor( + private val sourceSets: List, +) { + data class SourceSet( + val project: String, + val name: String, + val sources: List, + val roots: Set, + val resources: List, + val dependencies: Set, + val modulePath: List, + ) + + data class Inputs( + val modules: List, + val binaries: List, + ) + + fun resolve(): Inputs = + try { + resolveFiles() + } catch (failure: IOException) { + throw IllegalArgumentException( + "Cannot read evaluated compiler inputs: ${failure.message}", + failure, + ) + } + + private fun resolveFiles(): Inputs { + val sources = sourceSets.flatMap { it.sources }.distinct() + val catalog = + sources.filter(File::isFile).fold(XdkWorkspaceDiscovery.Catalog()) { result, file -> + result.withSource(file, file.readText()) { false } + } + val roots = sourceSets.flatMap { it.roots }.toSet() + val discovered = catalog.modules(emptyList()).filter { it.root in roots } + val owners = + discovered.associateWith { module -> + sourceSets.singleOrNull { module.root in it.sources } + ?: throw IllegalArgumentException("Ambiguous build ownership for ${module.uri}") + } + + fun allowed(owner: SourceSet): Set { + val projects = + generateSequence(owner.dependencies) { current -> + ( + current + + sourceSets + .filter { it.project in current } + .flatMap { it.dependencies } + ).takeUnless { it == current } + }.last() + return owners + .filterValues { + it === owner || + it.project in projects || + (owner.name == "test" && it.project == owner.project && it.name == "main") + }.keys + .mapTo(linkedSetOf()) { it.name } + } + val modules = + discovered.map { module -> + val owner = owners.getValue(module) + XdkSourceModule( + module.name, + module.uri, + module.dependencies.intersect(allowed(owner)), + owner.resources, + ) + } + // Validate the complete source graph before a live configuration is replaced. + XdkProject(modules) + val binaries = + sourceSets + .asSequence() + .flatMap { it.modulePath } + .distinct() + .flatMap { root -> + when { + root.isDirectory -> { + root.listFiles().orEmpty().filter { it.isFile && it.extension == "xtc" } + } + + root.isFile && root.extension == "xtc" -> { + listOf(root) + } + + else -> { + emptyList() + } + } + }.distinct() + .map { XdkDependency.fromBinary(it.readBytes()) } + .filter { + it.module !in XdkLibraries.moduleNames && + it.module !in modules.map(XdkSourceModule::name) + }.groupBy { it.module } + .map { (name, values) -> + require(values.map { it.revision }.distinct().size == 1) { + "Conflicting build artifacts for $name" + } + values.first() + }.toList() + return Inputs(modules, binaries) + } + + companion object { + fun read(models: List): XdkBuildModel { + val entries = + models.flatMap { model -> + require( + model["schemaVersion"]?.let { + it.isJsonPrimitive && it.asJsonPrimitive.isNumber && it.asInt == 1 + } == true, + ) { + "Unsupported Gradle compiler model version" + } + require(model["sourceSets"]?.isJsonArray == true) { + "Gradle compiler model requires sourceSets" + } + model["sourceSets"].asJsonArray.map { value -> + require(value.isJsonObject) { "Gradle source sets must be objects" } + val entry = value.asJsonObject + + fun strings(key: String): List { + require(entry[key]?.isJsonArray == true) { + "Build-model $key must be an array" + } + return entry[key].asJsonArray.map { + require(it.isJsonPrimitive && it.asJsonPrimitive.isString) { + "Invalid build-model $key" + } + it.asString + } + } + + fun string(key: String): String { + val text = entry[key] + require( + text != null && + text.isJsonPrimitive && + text.asJsonPrimitive.isString && + text.asString.isNotBlank(), + ) { + "Build-model $key must be a non-blank string" + } + return text.asString + } + + fun files(key: String) = + strings(key).map { + requireNotNull(XdkSources.file(it)) { + "Build-model $key requires file URIs" + } + } + val project = string("projectId") + val name = string("sourceSet") + SourceSet( + project, + name, + files("sourceFiles"), + files("moduleRoots").toSet(), + files("resourceRoots").map { it.toURI().toString() }, + strings("projectDependencies").toSet(), + files("modulePath"), + ) + } + } + require(entries.map { it.project to it.name }.distinct().size == entries.size) { + "Duplicate Gradle source-set ownership" + } + require( + entries.flatMap { it.sources }.distinct().size == entries.sumOf { it.sources.size }, + ) { + "Overlapping Gradle module roots" + } + require(entries.all { it.sources.containsAll(it.roots) }) { + "Module roots must belong to their source set" + } + return XdkBuildModel(entries) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCalls.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCalls.kt new file mode 100644 index 00000000000..0a0baa46a96 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCalls.kt @@ -0,0 +1,88 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CallHierarchyIncomingCall +import org.xvm.lsp.adapter.CallHierarchyItem +import org.xvm.lsp.adapter.CallHierarchyOutgoingCall +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.model.SymbolInfo.SymbolKind + +/** + * Static selected calls in one successful module snapshot. No runtime dispatch or name matching. + */ +internal class XdkCalls( + views: Map, +) { + private val views = views.filterValues { it.status == SemanticModel.Status.COMPLETE } + private val model = this.views.values.firstOrNull() + private val callables = model?.callables.orEmpty() + private val calls = + this.views.values + .flatMap { it.calls } + .filter { it.caller != null && it.caller in callables && it.method in callables } + + fun prepare( + uri: String, + line: Int, + column: Int, + ): List { + val view = views[uri] ?: return emptyList() + view.symbolAt(line, column)?.let { + return listOfNotNull(item(it.id)) + } + val position = SemanticModel.Position(line, column) + val enclosing = + callables.values + .filter { + it.location.sourceName == view.sourceName && position in it.location.range + }.maxByOrNull { it.location.range.start } + return listOfNotNull(enclosing?.let { item(it.symbol) }) + } + + fun incoming(item: CallHierarchyItem): List { + val id = resolve(item) ?: return emptyList() + return calls + .filter { it.method == id } + .groupBy { it.caller } + .mapNotNull { (caller, calls) -> + caller?.let(::item)?.let { it -> + CallHierarchyIncomingCall(it, calls.map { it.callee.toRange() }.distinct()) + } + } + } + + fun outgoing(item: CallHierarchyItem): List { + val id = resolve(item) ?: return emptyList() + return calls + .filter { it.caller == id } + .groupBy { it.method } + .mapNotNull { (target, calls) -> + item(target)?.let { it -> + CallHierarchyOutgoingCall(it, calls.map { it.callee.toRange() }.distinct()) + } + } + } + + private fun resolve(item: CallHierarchyItem): SemanticModel.SymbolId? = + callables.keys.firstOrNull { item.data == token(it) && item.uri == item(it)?.uri } + + private fun item(id: SemanticModel.SymbolId): CallHierarchyItem? { + val callable = callables[id] ?: return null + val symbol = model?.symbol(id) ?: return null + val uri = + views.entries.firstOrNull { it.value.sourceName == callable.location.sourceName }?.key + ?: return null + return CallHierarchyItem( + symbol.name, + SymbolKind.METHOD, + uri, + callable.location.range.toRange(), + callable.selection.toRange(), + data = token(id), + ) + } + + private fun token(id: SemanticModel.SymbolId): String = "${id.snapshot}:${id.index}" + + private fun SemanticModel.Range.toRange(): Range = Range(Position(start.line, start.column), Position(end.line, end.column)) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt new file mode 100644 index 00000000000..4d611147c8a --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt @@ -0,0 +1,179 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.Component.Format +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.ByteConstant +import org.xvm.compiler.InvocationBinding +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.lsp.adapter.ColorPresentation +import org.xvm.lsp.adapter.ColorValue +import org.xvm.lsp.adapter.DocumentColor +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TextEdit +import org.xvm.util.PackedInteger +import kotlin.math.roundToInt +import java.util.List.copyOf as immutableList + +/** + * Opt-in L77 experiment for ColorPrototype.Rgba, an ordinary test-fixture const, not an XDK API. + * Only the resolved shorthand constructor with four UInt8 channels is recognized. No user code + * is executed. Copy source spans on the compiler worker; queries retain no AST/pool/constant. + */ +internal object XdkColorPrototype { + private const val MODULE = "ColorPrototype" + private const val TYPE = "Rgba" + private const val MAX_CHANNEL = 255 + private val names = listOf("red", "green", "blue", "alpha") + private val comments = setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) + private val newlines = Regex("\r\n|\r|\n") + + /** Source.toString(start, end) normalizes CRLF. Edits must retain the original text. */ + private class SourceText( + val text: String, + ) { + private val starts = listOf(0) + newlines.findAll(text).map { it.range.last + 1 }.toList() + + fun offset(at: Long): Int = starts[Source.calculateLine(at)] + Source.calculateOffset(at) + + fun slice( + start: Long, + end: Long, + ): String = text.substring(offset(start), offset(end)) + } + + private data class Construction( + val source: Source, + val start: Long, + val end: Long, + val binding: InvocationBinding, + ) + + internal data class Channel( + val parameter: Int, + val start: Int, + val end: Int, + val value: Int, + ) + + internal class Site( + val sourceName: String?, + val range: Range, + private val text: String, + channels: List, + private val trailingComma: Boolean, + ) { + private val channels = immutableList(channels) + val color = + channels.associate { it.parameter to it.value }.let { values -> + ColorValue( + values.getValue(0).toDouble() / MAX_CHANNEL, + values.getValue(1).toDouble() / MAX_CHANNEL, + values.getValue(2).toDouble() / MAX_CHANNEL, + (values[3] ?: MAX_CHANNEL).toDouble() / MAX_CHANNEL, + ) + } + + fun presentation(color: ColorValue): ColorPresentation { + val values = listOf(color.red, color.green, color.blue, color.alpha).map { (it * MAX_CHANNEL).roundToInt() } + val alpha = + if (channels.none { it.parameter == 3 } && values[3] != MAX_CHANNEL) { + text.dropLast(1) + (if (trailingComma) " " else ", ") + "alpha = ${values[3]})" + } else { + text + } + val replacement = + channels.sortedByDescending { it.start }.fold(alpha) { result, channel -> + result.replaceRange(channel.start, channel.end, values[channel.parameter].toString()) + } + return ColorPresentation("RGBA(${values.joinToString(", ")})", TextEdit(range, replacement)) + } + } + + fun capture(compilation: EmbeddingSupport.Compilation): Map> { + if (!compilation.succeeded()) return emptyMap() + return ConstantPool.withPool(compilation.pool()).use { + buildList { + compilation.constructorBindings().forEach { (node, binding) -> + node.source?.let { add(Construction(it, node.startPosition, node.endPosition, binding)) } + } + compilation.initializerBindings().forEach { (owner, initializer) -> + initializer.calls().filter { it.construction() }.forEach { call -> + owner.source?.let { add(Construction(it, call.span().startPosition(), call.span().endPosition(), call.binding())) } + } + } + }.groupBy { it.source } + .flatMap { (source, calls) -> + val raw = SourceText(source.toRawString()) + calls.mapNotNull { site(source.fileName, raw, it.start, it.end, it.binding) } + }.distinctBy { it.sourceName to it.range } + .groupBy { it.sourceName } + } + } + + private fun site( + sourceName: String?, + source: SourceText, + start: Long, + end: Long, + binding: InvocationBinding, + ): Site? { + val identity = binding.method() + val owner = identity.parentConstant.parentConstant + if (owner.moduleConstant.name != MODULE || owner.pathString != TYPE || owner.component?.format != Format.CONST) return null + val method = identity.component as? MethodStructure ?: return null + if (!method.isShorthandConstructor || !method.isConstructor || method.typeParamCount != 0) return null + val parameters = method.params.toList() + if (parameters.map { it.name } != names || parameters.any { it.type != method.constantPool.typeUInt8() }) return null + // A changed fixture/default is not silently interpreted as the prototype contract. + if ((parameters.last().defaultValue as? ByteConstant)?.value != MAX_CHANNEL) return null + val text = source.slice(start, end) + if (!text.endsWith(')')) return null + val channels = + binding.arguments().map { argument -> + val raw = source.slice(argument.startPosition(), argument.endPosition()) + val errors = ErrorList() + val tokens = Lexer(Source(raw), errors).asSequence().filter { it.id !in comments }.toList() + if (errors.hasSeriousErrors()) return null + val literal = + if (argument.named()) { + if (tokens.size != 3 || tokens[0].id != Token.Id.IDENTIFIER || tokens[1].id != Token.Id.ASN) return null + tokens[2] + } else { + tokens.singleOrNull() ?: return null + } + if (literal.id != Token.Id.LIT_INT) return null + val number = literal.value as? PackedInteger ?: return null + // The successful UInt8 compilation already enforces this; keep the projection bounded. + val integer = number.bigInteger + val value = integer.toInt() + if (integer != value.toBigInteger() || value !in 0..MAX_CHANNEL) return null + val prefix = source.offset(argument.startPosition()) - source.offset(start) + val argumentSource = SourceText(raw) + Channel( + argument.parameterIndex(), + prefix + argumentSource.offset(literal.startPosition), + prefix + argumentSource.offset(literal.endPosition), + value, + ) + } + val indices = channels.map { it.parameter } + if (indices.toSet().size != indices.size || indices.any { it !in 0..3 } || !(0..2).all { it in indices }) return null + val tail = + Lexer(Source(text.substring(channels.maxOf { it.end }, text.lastIndex)), ErrorList()) + .asSequence() + .filter { it.id !in comments } + .toList() + if (tail.isNotEmpty() && tail.map { it.id } != listOf(Token.Id.COMMA)) return null + return Site(sourceName, Range(position(start), position(end)), text, channels, tail.isNotEmpty()) + } + + private fun position(at: Long) = Position(Source.calculateLine(at), Source.calculateOffset(at)) + + fun colors(sites: List): List = sites.map { DocumentColor(it.range, it.color) } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt new file mode 100644 index 00000000000..bc12cb3b478 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt @@ -0,0 +1,336 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.CompletionItem.CompletionKind +import org.xvm.lsp.adapter.ParameterInfo +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.SignatureHelp +import org.xvm.lsp.adapter.SignatureInfo +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.adapter.xdk.SemanticModel.Position +import org.xvm.lsp.adapter.xdk.SemanticModel.Signature +import org.xvm.lsp.adapter.Position as AdapterPosition + +/** Editor queries over copied facts only: no AST, constant pool, resolution or source rewriting. */ +internal object XdkCursorQueries { + fun completions(model: PartialSemanticModel): List { + val site = model.sites.singleOrNull() ?: return model.syntaxCompletions + val prefix = + site.memberPrefix + ?: when (site.kind) { + PartialSemanticModel.Kind.CALL -> { + PartialSemanticModel.MemberPrefix( + "", + SemanticModel.Range(site.range.end, site.range.end), + ) + } + + else -> { + return model.syntaxCompletions + } + } + val range = + Range( + AdapterPosition(prefix.range.start.line, prefix.range.start.column), + AdapterPosition(prefix.range.end.line, prefix.range.end.column), + ) + val members = + if (site.kind == PartialSemanticModel.Kind.CALL) site.argumentValues else site.members + val ordinary = + members + .filter { it.name.startsWith(prefix.text) } + .map { member -> + CompletionItem( + label = member.name, + kind = + when (member.kind) { + SemanticModel.SymbolKind.METHOD -> CompletionKind.METHOD + + SemanticModel.SymbolKind.VARIABLE, + SemanticModel.SymbolKind.PARAMETER, + -> CompletionKind.VARIABLE + + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.TYPE_PARAMETER, + -> CompletionKind.CLASS + + else -> CompletionKind.PROPERTY + }, + detail = + member.signature?.let { + signature(model.semantics, member.name, it).label + } + ?: "${member.type?.let { model.semantics.type(it)?.displayName } ?: "?"} ${member.name}", + insertText = member.name, + textEdit = TextEdit(range, member.name), + documentation = model.semantics.symbol(member.symbol)?.documentation, + ) + }.distinctBy { it.label to it.detail } + val formals = + site.formals + .filter { it.name.startsWith(prefix.text) } + .map { formal -> + CompletionItem( + formal.name, + CompletionKind.CLASS, + formalDetail(model, formal), + formal.name, + TextEdit(range, formal.name), + ) + } + val literals = + site.argumentLiterals + .filter { it.startsWith(prefix.text) } + .map { text -> + CompletionItem( + text, + CompletionKind.VALUE, + "Literal fitting this argument", + text, + TextEdit(range, text), + ) + } + val instances = + site.argumentExpressions.map { text -> + CompletionItem(text, CompletionKind.VALUE, "Enclosing instance fitting this argument", text, TextEdit(range, text)) + } + val enclosing = + site.enclosingExpressions.map { text -> + CompletionItem(text, CompletionKind.VALUE, "Accessible enclosing instance", text, TextEdit(range, text)) + } + val templates = + site.argumentTemplates.map { text -> + CompletionItem( + text, + CompletionKind.SNIPPET, + "Lambda fitting this argument", + text, + TextEdit(range, text), + "Compiler-validated arity; replace the TODO() body before executing this code.", + snippet = text.replace("TODO()", $$"${1:TODO()}$0"), + ) + } + return ( + ordinary.filterNot { + it.label in site.argumentLiterals || it.label in site.argumentExpressions || + it.label in site.enclosingExpressions + } + + formals + literals + instances + enclosing + templates + model.syntaxCompletions + ).map { item -> item.copy(sortText = completionOrder(item)) } + .sortedBy { it.sortText } + } + + /** Stable client/server ordering; never imply that an incomplete overload was selected. */ + private fun completionOrder(item: CompletionItem): String { + val priority = + when (item.kind) { + CompletionKind.VARIABLE -> 0 + + CompletionKind.PROPERTY -> 1 + + CompletionKind.METHOD -> 2 + + CompletionKind.CLASS, + CompletionKind.INTERFACE, + -> 3 + + CompletionKind.MODULE -> 4 + + CompletionKind.VALUE -> 5 + + CompletionKind.KEYWORD -> 6 + + CompletionKind.SNIPPET -> 7 + } + return "$priority:${item.label.lowercase()}:${item.label}:${item.detail}" + } + + fun formalDetail( + model: PartialSemanticModel, + formal: PartialSemanticModel.Formal, + ): String = + "type parameter ${formal.name} extends " + + ( + formal.constraint?.let { model.semantics.type(it)?.displayName } + ?: "${formal.writtenConstraint} (written constraint)" + ) + + fun signatureHelp( + model: PartialSemanticModel, + position: Position, + ): SignatureHelp? { + val site = model.sites.singleOrNull() ?: return null + val slot = site.argumentIndexAt(position) ?: return null + if (site.functions.isNotEmpty()) { + val signatures = + site.functions.map { candidate -> + signature( + model.semantics, + site.calleeName ?: "function", + candidate.signature, + site.parameterAt(candidate, position), + "Function signature; written arguments fit, runtime target unknown.", + ) + } + return SignatureHelp( + signatures, + activeParameter = signatures.first().activeParameter ?: 0, + ) + } + site.callCandidates?.let { candidates -> + val signatures = + candidates + .sortedWith( + compareBy { it.converting } + .thenBy { candidate -> + candidate.member.signature + ?.let { + signature( + model.semantics, + candidate.member.name, + it, + constructor = candidate.constructor, + ).label + }.orEmpty() + }, + ).mapNotNull { candidate -> + candidate.member.signature?.let { + signature( + model = model.semantics, + name = candidate.member.name, + signature = it, + active = site.parameterAt(candidate, position), + documentation = + listOfNotNull( + model.semantics + .symbol(candidate.member.symbol) + ?.documentation, + "Candidate signature; written arguments fit, overload not selected.", + "Argument conversion required." + .takeIf { candidate.converting }, + ).joinToString("\n\n"), + constructor = candidate.constructor, + ) + } + }.distinctBy { it.label } + return signatures + .takeIf { it.isNotEmpty() } + ?.let { SignatureHelp(it, activeParameter = it.first().activeParameter ?: 0) } + } + val signatures = + site.members + .mapNotNull { member -> + member.signature?.let { candidate -> + // Positional source slots map directly. Named partial arguments need + // compiler + // mapping; do not infer one from commas or choose an applicable overload + // here. + val active = + slot.takeIf { it -> + site.arguments.none { it.label != null } && + it in candidate.parameters.indices + } + signature( + model.semantics, + member.name, + candidate, + active, + "Candidate signature; overload not selected.", + ) + } + }.distinctBy { it.label } + return signatures + .takeIf { it.isNotEmpty() } + ?.let { SignatureHelp(it, activeParameter = it.first().activeParameter ?: 0) } + } + + fun signatureHelp( + model: SemanticModel, + position: Position, + ): SignatureHelp? { + val methods = + model.calls.mapNotNull { call -> + model.symbol(call.method)?.let { + SignatureSite( + call.range, + call.callee, + it.name, + call.signature, + call.arguments, + it.documentation, + ) + } + } + val functions = + model.functionCalls.map { call -> + val name = + model.symbolAt(call.callee.end.line, call.callee.end.column - 1)?.name + ?: "function" + SignatureSite(call.range, call.callee, name, call.signature, call.arguments) + } + val call = + (methods + functions) + .filter { position > it.callee.end && position < it.range.end } + .minWithOrNull( + compareByDescending { it.range.start }.thenBy { it.range.end }, + ) ?: return null + val active = + (call.arguments.firstOrNull { position <= it.range.end } ?: call.arguments.lastOrNull()) + ?.parameterIndex + return SignatureHelp( + listOf(signature(model, call.name, call.signature, active, call.documentation)), + activeParameter = active ?: 0, + ) + } + + private data class SignatureSite( + val range: SemanticModel.Range, + val callee: SemanticModel.Range, + val name: String, + val signature: Signature, + val arguments: List, + val documentation: String? = null, + ) + + fun signature( + model: SemanticModel, + name: String, + signature: Signature, + active: Int? = null, + documentation: String? = null, + constructor: Boolean = false, + ): SignatureInfo { + val parameters = + signature.parameters.mapIndexed { index, parameter -> + ParameterInfo( + "${model.type(parameter.type)?.displayName ?: "?"}${parameter.name?.let { " $it" }.orEmpty()}" + + (if (parameter.defaulted) " = …" else ""), + "Parameter ${index + 1}${parameter.name?.let { ": $it" }.orEmpty()}. " + + if (parameter.defaulted) { + "Optional; a default value is declared." + } else { + "Required argument." + }, + ) + } + // The compiler signature includes the conditional success flag; source syntax does not. + val returnTypes = + if (signature.conditional) signature.returns.drop(1) else signature.returns + val returns = returnTypes.joinToString(", ") { model.type(it)?.displayName ?: "?" } + val result = if (returnTypes.size > 1) "($returns)" else returns.ifEmpty { "void" } + val returnLabel = + when { + constructor -> "" + signature.conditional -> "conditional $result " + else -> "$result " + } + return SignatureInfo( + "$returnLabel$name(${parameters.joinToString { it.label }})", + documentation, + // LSP defaults an absent/out-of-range active index to parameter zero. Keeping the + // signature label but omitting parameter metadata avoids a fabricated highlight. + parameters.takeIf { active != null }.orEmpty(), + active, + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt new file mode 100644 index 00000000000..239b52a79c5 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt @@ -0,0 +1,170 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.FileStructure +import org.xvm.asm.ModuleRepository +import org.xvm.asm.constants.IdentityConstant +import org.xvm.compiler.BuildRepository +import org.xvm.lsp.adapter.ReadOnlyDocument +import org.xvm.lsp.util.ExecutionTrace +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.DataOutputStream +import java.io.File +import java.security.MessageDigest +import java.util.HexFormat +import java.util.Map.copyOf as immutableMap + +/** + * An immutable artifact and its matching source index. Holds no compiler objects. The revision + * covers both bytes and locations; constant-table indices are meaningful only within that revision. + * Source text/files remain host-owned and must be replaced together with the artifact. + */ +class XdkDependency + private constructor( + val module: String, + private val artifact: ByteArray, + declarations: Map, + documents: Map = emptyMap(), + ) { + internal val documents: Map = immutableMap(documents) + + internal fun withSources(roots: List): XdkDependency { + val index = XdkAttachedSources.read(this, roots) + return XdkDependency(module, artifact, index.declarations, index.documents) + } + + internal val symbolIndex: XdkArtifactSymbols by lazy { XdkArtifactSymbols.capture(artifact) } + + val declarations: Map = immutableMap(declarations) + val revision: String = + ByteArrayOutputStream() + .also { bytes -> + DataOutputStream(bytes).use { output -> + output.writeInt(artifact.size) + output.write(artifact) + declarations.toSortedMap().forEach { (index, location) -> + output.writeInt(index) + val source = requireNotNull(location.sourceName).toByteArray(Charsets.UTF_8) + output.writeInt(source.size) + output.write(source) + output.writeInt(location.range.start.line) + output.writeInt(location.range.start.column) + output.writeInt(location.range.end.line) + output.writeInt(location.range.end.column) + } + } + }.toByteArray() + .let { HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(it)) } + + data class SymbolKey( + val module: String, + val revision: String, + val index: Int, + ) + + /** Returns a defensive copy; a host cannot mutate a queued request's dependency bytes. */ + fun bytes(): ByteArray = artifact.clone() + + /** A fresh compiler-owned tree for each attempt; no mutable repository survives publication. */ + internal fun open(): FileStructure = + ExecutionTrace.api("FileStructure.read(dependency)", module) { + FileStructure(ByteArrayInputStream(artifact)) + } + + companion object { + /** + * Binary-only libraries have identities and types, but deliberately no source locations. + */ + fun fromBinary(bytes: ByteArray): XdkDependency { + val owned = bytes.clone() + val file = + ExecutionTrace.api("FileStructure.read(binary)") { + FileStructure(ByteArrayInputStream(owned)) + } + return XdkDependency(file.module.name, owned, emptyMap()) + } + + internal fun capture( + compilation: EmbeddingSupport.Compilation, + locations: Map, + ): XdkDependency { + val artifact = + ByteArrayOutputStream() + .also { + ExecutionTrace.api("FileStructure.writeTo") { + compilation.file().writeTo(it) + } + }.toByteArray() + // Serialization establishes the artifact's constant indices. Resolve against its pool, + // since emission may have removed or re-registered constants from the source attempt. + val file = + ExecutionTrace.api("FileStructure.read(emitted)") { + FileStructure(ByteArrayInputStream(artifact)) + } + val declarations = + locations.entries + .mapNotNull { (identity, location) -> + file.constantPool + .getConstant(identity) + ?.position + ?.let { it to location } + }.toMap() + return XdkDependency(file.module.name, artifact, declarations) + } + } + } + +/** + * Worker-only join; compiler equality handles overloads and foreign pools, never display strings. + */ +internal data class DependencyDeclaration( + val key: XdkDependency.SymbolKey, + val location: SemanticModel.SourceLocation, +) + +internal class XdkDependencies( + dependencies: List, +) { + val modules: Map = immutableMap(dependencies.associateBy { it.module }) + + init { + require(modules.size == dependencies.size) { "Duplicate dependency module" } + require(modules.keys.none { it in XdkLibraries.moduleNames }) { + "Project dependencies cannot replace the bundled compiler libraries" + } + } + + /** Open only on the serialized compiler worker. Both results die with the attempt. */ + fun open(): Open { + val files = modules.mapValues { it.value.open() } + val repository = BuildRepository().apply { files.values.forEach { storeModule(it.module) } } + val declarations = + buildMap { + files.forEach { (name, file) -> + val dependency = modules.getValue(name) + dependency.declarations.forEach { (index, location) -> + val identity = file.constantPool.getConstant(index) as? IdentityConstant + check(identity != null && identity.moduleConstant.name == name) { + "Invalid dependency source index" + } + put( + identity, + DependencyDeclaration( + XdkDependency.SymbolKey(name, dependency.revision, index), + location, + ), + ) + } + } + } + return Open(repository, declarations, modules.mapValues { it.value.revision }, modules) + } + + class Open( + val repository: ModuleRepository, + val declarations: Map, + val revisions: Map, + val artifacts: Map = emptyMap(), + ) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt new file mode 100644 index 00000000000..b744d7c1d0b --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt @@ -0,0 +1,33 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.model.CompilationResult +import java.util.concurrent.atomic.AtomicReference + +/** Bounded to the current graph. Only detached diagnostics, source inputs and artifacts survive. */ +internal class XdkDiagnosticIndex { + data class Key( + val name: String, + val sources: XdkSources.Inputs, + val dependencies: Map, + ) + + data class Build( + val key: Key, + val result: CompilationResult, + val artifact: XdkDependency?, + ) + + data class Snapshot( + val revision: String? = null, + val results: List = emptyList(), + val builds: Map = emptyMap(), + ) + + private val current = AtomicReference(Snapshot()) + + fun snapshot(): Snapshot = current.get() + + fun replace(snapshot: Snapshot) = current.set(snapshot) + + fun clear() = current.set(Snapshot()) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt new file mode 100644 index 00000000000..46a9f3bf2b3 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt @@ -0,0 +1,96 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorListener +import org.xvm.asm.XvmStructure +import org.xvm.compiler.Source +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.Location +import java.io.File +import java.net.URI +import java.net.URISyntaxException +import org.xvm.util.Severity as XtcSeverity + +/** + * The compiler places a diagnostic in one of three ways, and Site being a closed set is what lets + * this be exhaustive rather than a hunt for whichever field happens to be populated. + */ +internal fun ErrorListener.ErrorInfo.toDiagnostic( + source: Source, + sourceUris: Map, + declarations: Map, +): Diagnostic { + val uri = source.fileName + val where = site() + val sourceUri = + if (where is ErrorListener.Site.In) { + sourceUris[where.source().fileName] + ?: if (where.source() === source) uri else where.source().diagnosticUri() + } else { + null + } + return Diagnostic( + location = + when (where) { + is ErrorListener.Site.In -> { + sourceUri?.let { spanOf(it, where) } ?: wholeDocument(uri) + } + + is ErrorListener.Site.At -> { + declarations[where.xs()] ?: wholeDocument(uri) + } + + // a whole-compilation failure belongs to the document, not to a line in it + else -> { + wholeDocument(uri) + } + }, + severity = severity.toLspSeverity(), + message = + if (where is ErrorListener.Site.In && sourceUri == null) { + "In ${where.source().fileName ?: "an unidentified source"}: $message" + } else { + message + }, + code = code, + source = "xtc", + ) +} + +/** Only absolute source identities can be published as navigable locations. */ +private fun Source.diagnosticUri(): String? { + val name = fileName ?: return null + val file = File(name) + if (file.isAbsolute) return file.toURI().toString() + return try { + URI(name).takeIf { it.isAbsolute }?.toString() + } catch (_: URISyntaxException) { + null + } +} + +private fun spanOf( + uri: String, + where: ErrorListener.Site.In, +): Location = + Location( + uri = uri, + startLine = Source.calculateLine(where.lPosStart()), + startColumn = Source.calculateOffset(where.lPosStart()), + endLine = Source.calculateLine(where.lPosEnd()), + endColumn = Source.calculateOffset(where.lPosEnd()), + ) + +private fun wholeDocument(uri: String): Location = Location(uri, 0, 0, 0, 0) + +private fun XtcSeverity.toLspSeverity(): Diagnostic.Severity = + when (this) { + XtcSeverity.FATAL, + XtcSeverity.ERROR, + -> Diagnostic.Severity.ERROR + + XtcSeverity.WARNING -> Diagnostic.Severity.WARNING + + XtcSeverity.INFO -> Diagnostic.Severity.INFORMATION + + XtcSeverity.NONE -> Diagnostic.Severity.HINT + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDocumentation.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDocumentation.kt new file mode 100644 index 00000000000..90843a01386 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDocumentation.kt @@ -0,0 +1,56 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CodeAction +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.adapter.WorkspaceEdit + +/** Comment-only edits at compiler-established written headers; no syntax or signature guessing. */ +internal object XdkDocumentation { + fun actions( + uri: String, + text: String, + selection: Range, + model: SemanticModel, + ): List { + val start = SemanticModel.Position(selection.start.line, selection.start.column) + val end = SemanticModel.Position(selection.end.line, selection.end.column) + val newline = Regex("\r\n|\r|\n").find(text)?.value ?: "\n" + return model.symbols + .mapNotNull { symbol -> + val header = symbol.headerStart ?: return@mapNotNull null + val name = symbol.declaration ?: return@mapNotNull null + if (symbol.declarationSource != model.sourceName || symbol.documentation != null || + start >= name.end || end < header + ) { + return@mapNotNull null + } + val offset = XdkRename.offset(text, header) ?: return@mapNotNull null + val lineStart = XdkRename.offset(text, header.copy(column = 0)) ?: return@mapNotNull null + val indent = text.substring(lineStart, offset) + // Preserve same-line siblings and ambiguous adjacent block comments, including empty docs. + if (indent.any { it != ' ' && it != '\t' } || text.take(lineStart).trimEnd().endsWith("*/")) return@mapNotNull null + val lines = + buildList { + add("/**") + add(" * TODO: add description.") + symbol.signature?.parameters?.forEach { parameter -> + parameter.name?.takeUnless { it == "_" }?.let { add(" * @param $it TODO") } + } + symbol.signature?.returns?.forEach { _ -> add(" * @return TODO") } + add(" */") + } + val at = Position(header.line, 0) + CodeAction( + "Generate documentation comment", + CodeAction.CodeActionKind.SOURCE, + edit = + WorkspaceEdit( + mapOf(uri to listOf(TextEdit(Range(at, at), lines.joinToString(newline, postfix = newline) { indent + it }))), + versioned = true, + ), + ) + }.distinct() + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkFileChanges.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkFileChanges.kt new file mode 100644 index 00000000000..477211c2550 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkFileChanges.kt @@ -0,0 +1,76 @@ +package org.xvm.lsp.adapter.xdk + +import java.io.File +import java.nio.file.FileVisitOption.FOLLOW_LINKS +import java.nio.file.Files +import java.nio.file.Path + +/** + * A delayed watcher event may describe inputs already compiled. Compare against that snapshot, not + * against an observation taken when the notification arrives. This uses no compiler APIs. Bound + * notification-thread I/O; uncertain or larger changes take the normal invalidation path. + */ +internal object XdkFileChanges { + private const val MAX_ENTRIES = 512L + private const val MAX_BYTES = 1_000_000L + + fun unchanged( + file: File, + root: File, + inputs: XdkSources.Inputs, + overlays: Map = emptyMap(), + ): Boolean = + runCatching { + val path = file.toPath() + val member = root.parentFile.resolve(root.nameWithoutExtension).toPath() + val sourceRoots = listOf(root.toPath(), member).filter { it.overlaps(path) } + val sourceEntries = + sourceRoots + .flatMap { entries(if (it.startsWith(path)) it else path) } + .filter { Files.isDirectory(it) || it.toString().endsWith(".x") } + .toSet() + val expectedSources = + (inputs.text.keys + inputs.directories) + .map { it.toPath() } + .filter { it.startsWith(path) } + .toSet() + if (sourceEntries != expectedSources || !bounded(sourceEntries)) return@runCatching false + if ( + sourceEntries.any { + Files.isRegularFile(it) && + (overlays[it.toFile()] ?: Files.readString(it)) != inputs.text[it.toFile()] + } + ) { + return@runCatching false + } + + if (inputs.resources.entries.isEmpty()) return@runCatching true + val resourceRoots = + inputs.resources.roots + .map { it.toPath() } + .filter { it.overlaps(path) } + val currentResources = + resourceRoots.flatMap { entries(if (it.startsWith(path)) it else path) }.toSet() + val expectedResources = inputs.resources.entries.filterKeys { Path.of(it).startsWith(path) } + if (!bounded(currentResources)) return@runCatching false + val current = + currentResources.associate { it.toString() to XdkResources.entry(it) { false } } + + resourceRoots.filterNot(Files::exists).associate { it.toString() to "missing" } + current == expectedResources + }.getOrDefault(false) + + private fun Path.overlaps(other: Path) = startsWith(other) || other.startsWith(this) + + private fun entries(root: Path): List = + if (!Files.exists(root)) { + emptyList() + } else { + Files.walk(root, FOLLOW_LINKS).use { paths -> + paths.limit(MAX_ENTRIES + 1).toList().also { require(it.size <= MAX_ENTRIES) } + } + } + + private fun bounded(paths: Set): Boolean = + paths.size <= MAX_ENTRIES && + paths.asSequence().filter(Files::isRegularFile).sumOf(Files::size) <= MAX_BYTES +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkHierarchy.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkHierarchy.kt new file mode 100644 index 00000000000..e3a5de566f7 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkHierarchy.kt @@ -0,0 +1,86 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TypeHierarchyItem +import org.xvm.lsp.model.SymbolInfo.SymbolKind + +/** Queries use copied declaration edges, never compiler objects or a new TypeInfo build. */ +internal class XdkHierarchy( + private val views: Map, +) { + private val model = views.values.firstOrNull() + private val types = model?.typeDeclarations.orEmpty() + private val children = + types.values + .flatMap { child -> + child.parents.map { it.symbol to child.symbol } + }.groupBy({ it.first }, { it.second }) + + fun prepare( + uri: String, + line: Int, + column: Int, + ): List = + views[uri] + ?.symbolAt(line, column) + ?.id + ?.let(::item) + ?.let(::listOf) + .orEmpty() + + fun supertypes(item: TypeHierarchyItem): List = + resolve(item)?.parents.orEmpty().mapNotNull { + item(it.symbol, model?.type(it.type)?.displayName) + } + + fun subtypes(item: TypeHierarchyItem): List = + resolve(item)?.let { children[it.symbol] }.orEmpty().mapNotNull { item(it) } + + /** + * IDs are bound to this compilation; an item returned before an edit cannot identify a new + * type. + */ + private fun resolve(item: TypeHierarchyItem): SemanticModel.TypeDeclaration? = + types.values.firstOrNull { + item.data == token(it.symbol) && item.uri == sourceUri(it.location.sourceName) + } + + private fun item( + id: SemanticModel.SymbolId, + detail: String? = null, + ): TypeHierarchyItem? { + val declaration = types[id] ?: return null + val symbol = model?.symbol(id) ?: return null + val uri = sourceUri(declaration.location.sourceName) ?: return null + return TypeHierarchyItem( + symbol.name, + when (declaration.category) { + "interface" -> SymbolKind.INTERFACE + + "mixin", + "annotation", + -> SymbolKind.MIXIN + + "service" -> SymbolKind.SERVICE + + "const" -> SymbolKind.CONST + + "enum" -> SymbolKind.ENUM + + else -> SymbolKind.CLASS + }, + uri, + declaration.location.range.toRange(), + (symbol.declaration ?: return null).toRange(), + detail ?: symbol.type?.let { model.type(it)?.displayName }, + token(id), + ) + } + + private fun sourceUri(name: String?): String? = views.entries.firstOrNull { it.value.sourceName == name }?.key + + private fun token(id: SemanticModel.SymbolId): String = "${id.snapshot}:${id.index}" + + private fun SemanticModel.Range.toRange(): Range = Range(Position(start.line, start.column), Position(end.line, end.column)) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt new file mode 100644 index 00000000000..35185abf594 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt @@ -0,0 +1,108 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Lexer +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.ImportStatement +import org.xvm.lsp.adapter.CodeAction.CodeActionKind +import org.xvm.lsp.util.ExecutionTrace + +/** + * Java parser ranges propose edits; XdkProjectQueries proves their semantic safety before + * publication. + */ +internal object XdkImports { + data class Candidate( + val title: String, + val kind: CodeActionKind, + val edits: List, + ) + + fun candidates(text: String): List { + val errors = ErrorList() + val root = + try { + ExecutionTrace.api("Parser.parseSource(imports)") { + Parser(Source(text), errors).parseSource() + } + } catch (_: CompilerException) { + return emptyList() + } + if (errors.hasSeriousErrors()) return emptyList() + val tokens = + ExecutionTrace.api("Lexer.lex(imports)") { + Lexer(Source(text), errors).asSequence().toList() + } + if (errors.hasSeriousErrors()) return emptyList() + val imports = + nodes(root) + .filterIsInstance() + .filter { !it.isWildcard && it.childNodes().none() } + .mapNotNull { node -> + val start = offset(text, node.startPosition) ?: return@mapNotNull null + val last = + tokens.firstOrNull { it.startPosition >= node.endPosition } + ?: return@mapNotNull null + if (last.id != Token.Id.SEMICOLON) return@mapNotNull null + node to + XdkRename.Edit( + start, + offset(text, last.endPosition) ?: return@mapNotNull null, + "", + ) + } + // Bound compiler proof work per request; explicit follow-up requests can remove further + // imports. + val removals = + imports.take(32).map { (node, edit) -> + Candidate( + "Remove unused import '${node.aliasName}'", + CodeActionKind.SOURCE, + listOf(edit), + ) + } + val groups = + imports + .groupBy { it.first.parent } + .values + .map { it.sortedBy { (_, edit) -> edit.start } } + val reorder = + groups.mapNotNull { group -> + if ( + group.size < 2 || + group.zipWithNext().any { (a, b) -> + text.substring(a.second.end, b.second.start).any { !it.isWhitespace() } + } + ) { + return@mapNotNull null + } + val written = group.map { (_, edit) -> text.substring(edit.start, edit.end) } + val sorted = written.sorted() + if (written == sorted) return@mapNotNull null + Candidate( + "Organize imports", + CodeActionKind.SOURCE_ORGANIZE_IMPORTS, + group.mapIndexed { index, (_, edit) -> edit.copy(text = sorted[index]) }, + ) + } + return reorder + removals + } + + private fun offset( + text: String, + position: Long, + ): Int? = + XdkRename.offset( + text, + SemanticModel.Position( + Source.calculateLine(position), + Source.calculateOffset(position), + ), + ) + + private fun nodes(root: AstNode): List = listOf(root) + root.childNodes().flatMap(::nodes) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkInlineCompletions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkInlineCompletions.kt new file mode 100644 index 00000000000..62db1bcb102 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkInlineCompletions.kt @@ -0,0 +1,52 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.InlineCompletionContext +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.TextEdit + +/** Conservative projection of compiler cursor facts; never adds imports or invents method bodies. */ +internal object XdkInlineCompletions { + fun eligible( + text: String, + position: Position, + context: InlineCompletionContext, + ): Boolean { + if (position.line < 0 || position.column < 0) return false + val line = text.lineSequence().elementAtOrNull(position.line) ?: return false + if (position.column > line.length || line.getOrNull(position.column)?.isJavaIdentifierPart() == true) return false + return !context.automatic || line.getOrNull(position.column - 1)?.isJavaIdentifierPart() == true + } + + fun project( + text: String, + position: Position, + context: InlineCompletionContext, + candidates: List, + ): List { + if (!eligible(text, position, context)) return emptyList() + val line = text.lineSequence().elementAt(position.line) + val suggestions = + candidates + .asSequence() + .filter { it.snippet == null && it.additionalTextEdits.isEmpty() } + .sortedBy { it.sortText ?: it.label } + .mapNotNull { it.textEdit } + .filter { edit -> + val start = edit.range.start + val end = edit.range.end + start.line == position.line && end == position && start.column in 0..end.column && + edit.newText.length > end.column - start.column && + edit.newText.startsWith(line.substring(start.column, end.column)) && + (!context.automatic || start.column < end.column) && + context.selectedCompletion.let { selected -> + selected == null || ( + selected.range == edit.range && + edit.newText.startsWith(selected.newText) && edit.newText.length > selected.newText.length + ) + } + }.distinct() + .toList() + return if (context.automatic) suggestions.singleOrNull()?.let(::listOf).orEmpty() else suggestions + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt new file mode 100644 index 00000000000..9e56d1ac132 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt @@ -0,0 +1,331 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.lsp.adapter.DocumentLink +import org.xvm.lsp.adapter.FormattingConfig +import org.xvm.lsp.adapter.FormattingOptions +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.treesitter.SemanticTokenLegend +import org.xvm.lsp.util.ExecutionTrace + +/** Java-lexer editing helpers. A failed lex never authorizes a source rewrite. */ +internal object XdkLexical { + private data class Span( + val id: Token.Id, + val range: Range, + val start: Int, + val end: Int, + val text: String, + ) + + private val newlines = Regex("\r\n|\r|\n") + private val urls = Regex("https?://[^\\s<>\"'`\\\\]+") + private val comments = setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) + private val strings = + setOf(Token.Id.LIT_STRING, Token.Id.LIT_CHAR, Token.Id.LIT_BINSTR, Token.Id.TEMPLATE) + + /** + * Over-approximate path expressions without interpreting paths or suppressing compiler errors. + */ + fun mayUseResources(text: String): Boolean = + lex(text)?.any { + it.id in + setOf( + Token.Id.DIV, + Token.Id.DIR_CUR, + Token.Id.DIR_PARENT, + Token.Id.BIN_FILE, + Token.Id.STR_FILE, + Token.Id.TEMPLATE, + ) + } ?: true + + private data class Nesting( + val braces: Int = 0, + val parentheses: Int = 0, + val brackets: Int = 0, + ) { + fun after(id: Token.Id): Nesting = + when (id) { + Token.Id.L_CURLY -> copy(braces = braces + 1) + Token.Id.R_CURLY -> copy(braces = (braces - 1).coerceAtLeast(0)) + Token.Id.L_PAREN, Token.Id.ASYNC_PAREN -> copy(parentheses = parentheses + 1) + Token.Id.R_PAREN -> copy(parentheses = (parentheses - 1).coerceAtLeast(0)) + Token.Id.L_SQUARE -> copy(brackets = brackets + 1) + Token.Id.R_SQUARE -> copy(brackets = (brackets - 1).coerceAtLeast(0)) + else -> this + } + } + + private val closers = setOf(Token.Id.R_CURLY, Token.Id.R_PAREN, Token.Id.R_SQUARE) + private val continuations = + setOf( + Token.Id.ASN, + Token.Id.ADD, + Token.Id.SUB, + Token.Id.MUL, + Token.Id.DIV, + Token.Id.MOD, + Token.Id.DIVREM, + Token.Id.COND_AND, + Token.Id.COND_OR, + Token.Id.COND_XOR, + Token.Id.COND_ELSE, + Token.Id.ADD_ASN, + Token.Id.SUB_ASN, + Token.Id.MUL_ASN, + Token.Id.DIV_ASN, + Token.Id.COND_ASN, + Token.Id.COND_NN_ASN, + Token.Id.LAMBDA, + ) + + fun format( + text: String, + config: FormattingConfig, + options: FormattingOptions, + range: Range? = null, + wrapLines: Boolean = true, + ): List { + val tokens = lex(text) ?: return emptyList() + val lines = text.split(newlines) + val lineTokens = tokens.groupBy { it.range.start.line } + val nesting = + lines.indices.runningFold(Nesting()) { depth, line -> + lineTokens[line].orEmpty().fold(depth) { current, token -> current.after(token.id) } + } + val previous = + lines.indices.runningFold(null as Token.Id?) { last, line -> + lineTokens[line].orEmpty().lastOrNull { it.id !in comments }?.id ?: last + } + val multiline = + tokens + .filter { it.range.start.line < it.range.end.line } + .flatMap { token -> (token.range.start.line..token.range.end.line).map { it to token } } + .toMap() + val newline = newlines.find(text)?.value ?: "\n" + val start = range?.start?.line ?: 0 + val end = range?.end?.let { if (it.column == 0 && it.line > start) it.line - 1 else it.line } ?: lines.lastIndex + val edits = + buildList { + (start..minOf(end, lines.lastIndex)).forEach { line -> + val value = lines[line] + val host = multiline[line] + // Literal bytes (including multiline/template indentation) are never reformatted. + // Shift only standalone block-comment margins, preserving their relative layout. + if (host != null && ( + host.id != Token.Id.ENC_COMMENT || + lines[host.range.start.line].take(host.range.start.column).isNotBlank() || + lines[host.range.end.line].drop(host.range.end.column).isNotBlank() + ) + ) { + return@forEach + } + val onLine = lineTokens[line].orEmpty() + val depth = onLine.takeWhile { it.id in closers }.fold(nesting[line]) { current, token -> current.after(token.id) } + val continuation = + depth.parentheses + depth.brackets > 0 || + previous[line] in continuations || onLine.firstOrNull()?.id == Token.Id.DOT + val base = depth.braces * config.indentSize + val leading = value.takeWhile { it == ' ' || it == '\t' }.length + val indent = + if (host != null && line > host.range.start.line) { + nesting[host.range.start.line].braces * config.indentSize + + (leading - host.range.start.column).coerceAtLeast(0) + } else { + base + if (continuation) config.continuationIndentSize else 0 + } + val whitespace = indentation(indent, config, options) + if (value.isNotBlank() && value.take(leading) != whitespace) { + add(TextEdit(Range(Position(line, 0), Position(line, leading)), whitespace)) + } + val trimmed = value.trimEnd(' ', '\t').length + if (options.trimTrailingWhitespace && trimmed < value.length && + host == null && onLine.none { it.range.end.column > trimmed } + ) { + add(TextEdit(Range(Position(line, trimmed), Position(line, value.length)), "")) + } + if (wrapLines && host == null && config.maxLineWidth > 0) { + addAll( + wrap( + line, + value, + onLine, + nesting[line], + leading, + indent, + newline, + config, + options, + ), + ) + } + } + if (range == null && options.insertFinalNewline && text.isNotEmpty() && !text.endsWith('\n') && !text.endsWith('\r')) { + val at = Position(lines.lastIndex, lines.last().length) + add(TextEdit(Range(at, at), newline)) + } + } + val proposed = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { result, edit -> + result.replaceRange(offset(text, edit.range.start), offset(text, edit.range.end), edit.newText) + } + + // Comment margins may move as a unit. Every other token, including every literal, must + // have identical spelling and tokenization; whitespace can be language-significant. + fun signature(items: List) = + items.map { + it.id to + if (it.id == Token.Id.ENC_COMMENT) { + it.text.split(newlines).map { line -> line.trimStart(' ', '\t') } + } else { + listOf(it.text) + } + } + return if (lex(proposed)?.let(::signature) == signature(tokens)) edits else emptyList() + } + + private fun indentation( + size: Int, + config: FormattingConfig, + options: FormattingOptions, + ): String = + if (config.insertSpaces) { + " ".repeat(size.coerceAtLeast(0)) + } else { + val tab = options.tabSize.coerceAtLeast(1) + "\t".repeat(size.coerceAtLeast(0) / tab) + " ".repeat(size.coerceAtLeast(0) % tab) + } + + /** Greedy width wrapping only at existing expression/list token boundaries. */ + private data class WrapPoint( + val start: Int, + val end: Int, + val indent: Int, + ) + + private fun wrap( + line: Int, + value: String, + tokens: List, + before: Nesting, + leading: Int, + indent: Int, + newline: String, + config: FormattingConfig, + options: FormattingOptions, + ): List { + val contexts = tokens.runningFold(before) { depth, token -> depth.after(token.id) } + val gaps = + tokens.zipWithNext().mapIndexedNotNull { index, (left, right) -> + val nested = contexts[index + 1] + val breakable = + left.id in continuations || + (left.id == Token.Id.COMMA && nested.parentheses + nested.brackets > 0) + val start = left.range.end.column + val end = right.range.start.column + WrapPoint(start, end, nested.braces * config.indentSize + config.continuationIndentSize).takeIf { + breakable && right.id !in closers && right.id !in comments && right.id != Token.Id.SEMICOLON && + value.substring(start, end).all { it == ' ' || it == '\t' } + } + } + + fun next( + start: Int, + width: Int, + ): WrapPoint? { + if (width + value.length - start <= config.maxLineWidth) return null + val remaining = gaps.filter { it.start > start } + return remaining.lastOrNull { width + it.start - start <= config.maxLineWidth } ?: remaining.firstOrNull() + } + return generateSequence(next(leading, indent)) { gap -> next(gap.end, gap.indent) } + .map { (start, end, continuedIndent) -> + TextEdit(Range(Position(line, start), Position(line, end)), newline + indentation(continuedIndent, config, options)) + }.toList() + } + + fun links(text: String): List = + lex(text) + .orEmpty() + .filter { it.id in comments || it.id in strings } + .flatMap { host -> + urls + .findAll(host.text) + .map { match -> + val target = match.value.trimEnd('.', ',', ';', ':', ')', ']', '}') + val start = host.start + match.range.first + DocumentLink( + Range( + XdkRename.position(text, start), + XdkRename.position(text, start + target.length), + ), + target, + target, + ) + }.toList() + } + + /** + * Documentation adds a semantic modifier absent from the lexical grammar. Leave ordinary + * syntax to the editor's grammar: broad string/keyword/number overlays erase finer escape, + * interpolation, control-keyword and literal scopes. No TextMate dependency is needed here. + */ + fun tokens(text: String): List> { + val lines = text.split(newlines) + return lex(text).orEmpty().flatMap { token -> + if (token.id != Token.Id.ENC_COMMENT || !token.text.startsWith("/**")) return@flatMap emptyList() + (token.range.start.line..token.range.end.line).mapNotNull { line -> + val start = if (line == token.range.start.line) token.range.start.column else 0 + val end = + if (line == token.range.end.line) token.range.end.column else lines[line].length + if (end == start) { + null + } else { + listOf( + line, + start, + end - start, + SemanticTokenLegend.typeIndex.getValue("comment"), + SemanticTokenLegend.modifierBitmask("documentation"), + ) + } + } + } + } + + private fun lex(text: String): List? { + val errors = ErrorList() + val tokens = + try { + ExecutionTrace.api("Lexer.lex(presentation)") { + Lexer(Source(text), errors).asSequence().toList() + } + } catch (_: CompilerException) { + return null + } + if (errors.hasSeriousErrors()) return null + // Index this immutable source once. Scanning all line breaks for every token makes even + // resource detection quadratic in file size, before compilation has started. + val lineStarts = listOf(0) + newlines.findAll(text).map { it.range.last + 1 }.toList() + return tokens.map { token -> + fun position(value: Long) = Position(Source.calculateLine(value), Source.calculateOffset(value)) + val range = Range(position(token.startPosition), position(token.endPosition)) + val start = lineStarts[range.start.line] + range.start.column + val end = lineStarts[range.end.line] + range.end.column + Span(token.id, range, start, end, text.substring(start, end)) + } + } + + private fun offset( + text: String, + at: Position, + ): Int = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt new file mode 100644 index 00000000000..969745ff184 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt @@ -0,0 +1,85 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.FileStructure +import org.xvm.compiler.BuildRepository +import org.xvm.lsp.util.ExecutionTrace +import java.security.MessageDigest +import java.util.HexFormat +import java.util.Properties +import java.util.Set.copyOf as immutableSet + +/** The compiler and its matching libraries travel together in the language server. */ +internal object XdkLibraries { + private data class Bundle( + val repository: BuildRepository, + val artifacts: Map, + ) + + private data class Artifact( + val revision: String, + val symbols: Lazy, + ) + + /** + * Package metadata can be displayed without constructing compiler constants or repositories. + */ + val packagedResources: List by lazy { + Properties() + .apply { resource("modules.properties").use { load(it) } } + .getProperty("modules") + .split(',') + .sorted() + } + + private val bundle by lazy { + val index = Properties().apply { resource("modules.properties").use { load(it) } } + val names = + checkNotNull(index.getProperty("modules")) { "Bundled XDK module index is missing" } + val repository = BuildRepository() + val artifacts = + names.split(',').associate { name -> + val bytes = resource(name).use { it.readBytes() } + val module = + ExecutionTrace.api("FileStructure.read(bundled-library)", name) { + FileStructure(bytes.inputStream()).module + } + repository.storeModule(module) + module.name to + Artifact( + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)), + lazy { XdkArtifactSymbols.capture(resource(name).use { it.readBytes() }) }, + ) + } + for (module in listOf("ecstasy.xtclang.org", "mack.xtclang.org", "_native.xtclang.org")) { + checkNotNull(repository.loadModule(module)) { "Bundled XDK is missing $module" } + } + Bundle(repository, artifacts) + } + + /** + * Every bundled library is reserved; workspace discovery must not turn it into editable source. + */ + val moduleNames: Set by lazy { immutableSet(bundle.repository.moduleNames) } + + private val configured by lazy { + ExecutionTrace.api("EmbeddingSupport.configure") { + EmbeddingSupport.instance().configure(bundle.repository, null) + } + } + + internal fun module(name: String) = bundle.repository.loadModule(name) + + internal fun revision(name: String): String = bundle.artifacts.getValue(name).revision + + internal fun symbolIndex(name: String): XdkArtifactSymbols? = bundle.artifacts[name]?.symbols?.value + + fun configure() { + configured + } + + private fun resource(name: String) = + checkNotNull(XdkLibraries::class.java.getResourceAsStream("/org/xvm/lsp/xdk/$name")) { + "Bundled XDK resource is missing: $name" + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt new file mode 100644 index 00000000000..bf16c25b8f2 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt @@ -0,0 +1,196 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ClassStructure +import org.xvm.asm.ConstantPool +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.ModuleConstant +import org.xvm.asm.constants.MultiMethodConstant +import org.xvm.lsp.adapter.ReadOnlyDocument +import org.xvm.lsp.adapter.SymbolMoniker +import java.net.URI +import java.nio.file.Files +import java.security.MessageDigest +import java.util.HexFormat +import java.util.Properties +import java.util.concurrent.ConcurrentHashMap +import java.util.zip.ZipInputStream + +/** + * Matching distribution sources. Parsed declaration ranges and text survive; compiler objects do + * not. + */ +internal object XdkLibrarySources { + private data class Entry( + val text: String, + val revision: String, + ) + + private data class SourceFile( + val module: String, + val path: String, + val uri: String, + val document: ReadOnlyDocument, + val declarations: List, + ) { + // Evaluated only on the compiler worker. The cached value contains copied ranges/IDs only. + val monikers by lazy { declarationMonikers(this) } + } + + const val SCHEME = "ecstasy-library" + + private val entries by lazy { + val archives = + Properties() + .apply { resource("sources.properties").use { load(it) } } + .getProperty("archives") + .split(',') + buildMap { + archives.forEach { archive -> + val bytes = resource("sources/$archive").use { it.readBytes() } + val revision = + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)) + ZipInputStream(bytes.inputStream()).use { zip -> + generateSequence { zip.nextEntry } + .filter { !it.isDirectory && it.name.endsWith(".x") } + .forEach { entry -> + val value = Entry(zip.readBytes().toString(Charsets.UTF_8), revision) + val previous = put(entry.name, value) + check(previous == null || previous.text == value.text) { + "Conflicting bundled source ${entry.name}" + } + } + } + } + } + } + private val directory by lazy { Files.createTempDirectory("xtc-library-sources-") } + private val sources = ConcurrentHashMap() + + private fun source(uri: String): SourceFile? { + if (sources.isEmpty()) return null + if (uri.startsWith("$SCHEME:")) return sources.values.find { it.document.uri == uri } + val canonical = XdkSources.file(uri)?.toURI()?.toString() ?: return null + return sources.values.find { it.uri == canonical } + } + + fun owns(uri: String): Boolean = source(uri) != null + + fun document(uri: String): ReadOnlyDocument? = source(uri)?.document + + /** A parsed name is not proof: require one exact artifact declaration at this source range. */ + fun monikers( + uri: String, + line: Int, + column: Int, + ): List { + if (line < 0 || column < 0) return emptyList() + val at = SemanticModel.Position(line, column) + return source(uri) + ?.monikers + ?.filterKeys { at in it } + ?.values + ?.singleOrNull() + ?.let(::listOf) + .orEmpty() + } + + private fun declarationMonikers(source: SourceFile): Map { + val module = XdkLibraries.module(source.module) ?: return emptyMap() + val symbols = XdkLibraries.symbolIndex(source.module) ?: return emptyMap() + return ConstantPool.withPool(module.constantPool).use { + module.constantPool.constants + .asSequence() + .toList() + .asSequence() + .filterIsInstance() + .filter { it.moduleConstant.name == source.module } + .mapNotNull { identity -> + val component = identity.component ?: return@mapNotNull null + val owner = component as? ClassStructure ?: component.getContainingClass(false) + if (owner?.sourcePath?.value != source.path) return@mapNotNull null + val target = declaration(identity) ?: return@mapNotNull null + val moniker = symbols.moniker(identity.position, imported = false) ?: return@mapNotNull null + target.location.range to moniker + }.groupBy({ it.first }, { it.second }) + .mapNotNull { (range, candidates) -> candidates.distinct().singleOrNull()?.let { range to it } } + .toList() + .toMap() + } + } + + fun sourceUri(name: String?): String? = name?.takeIf(::owns) + + /** Use the actual artifact identity, never a name search across library modules. */ + fun declaration(identity: IdentityConstant): DependencyDeclaration? { + val module = identity.moduleConstant?.name ?: return null + if (module !in XdkLibraries.moduleNames) return null + val binary = + XdkLibraries.module(module)?.constantPool?.getConstant(identity) as? IdentityConstant + ?: return null + val component = binary.component ?: return null + val owner = + component as? ClassStructure ?: component.getContainingClass(false) ?: return null + val path = owner.sourcePath?.value ?: return null + val entry = entries[path] ?: return null + val source = sources.computeIfAbsent(path) { parse(module, path, entry) } + val namespace = + generateSequence(binary) { it.namespace } + .takeWhile { it !is ModuleConstant } + .filterNot { it is MultiMethodConstant } + .map { it.name } + .toList() + .asReversed() + val candidates = source.declarations.filter { it.path == namespace } + val sourceLine = (component as? MethodStructure)?.takeIf { it.sourceText != null }?.sourceLineNumber + val selected = selectLibraryDeclaration(candidates, sourceLine) { it.firstLine..it.lastLine } + return selected?.let { + DependencyDeclaration( + XdkDependency.SymbolKey( + module, + "${XdkLibraries.revision(module)}:${entry.revision}", + binary.position, + ), + SemanticModel.SourceLocation(source.uri, it.range), + ) + } + } + + private fun parse( + module: String, + path: String, + entry: Entry, + ): SourceFile { + val text = entry.text + val file = directory.resolve(path).normalize() + require(file.startsWith(directory)) { "Invalid bundled source path" } + Files.createDirectories(file.parent) + Files.writeString(file, text) + check(file.toFile().setReadOnly()) { "Cannot protect bundled source $path" } + val declarations = libraryDeclarations(text, path) + val virtualUri = URI(SCHEME, module, "/${XdkLibraries.revision(module)}/${entry.revision}/$path", null, null).toASCIIString() + return SourceFile( + module, + path, + file + .toFile() + .canonicalFile + .toURI() + .toString(), + ReadOnlyDocument(virtualUri, text), + declarations, + ) + } + + private fun resource(name: String) = + checkNotNull(javaClass.getResourceAsStream("/org/xvm/lsp/xdk/$name")) { + "Bundled XDK source resource is missing: $name" + } +} + +/** A unique namespace suffices; overloaded names require one unambiguous debug-source span. */ +internal fun selectLibraryDeclaration( + candidates: List, + sourceLine: Int?, + lines: (T) -> IntRange, +): T? = candidates.singleOrNull() ?: sourceLine?.let { line -> candidates.singleOrNull { line in lines(it) } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt new file mode 100644 index 00000000000..e1ddcec28b5 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt @@ -0,0 +1,59 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotatedTypeExpression +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.TypeExpression +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.compiler.ast.VariableTypeExpression +import org.xvm.lsp.adapter.Range + +/** Plain, explicitly typed local initializers shared by conservative local refactorings. */ +internal object XdkLocalDeclarations { + /** Select only a local declared in this source view, never a member or dependency symbol. */ + fun selectedSymbol( + model: SemanticModel, + selection: Range, + ): SemanticModel.Symbol? { + val source = model.sourceName ?: return null + return model + .symbolAt(selection.start.line, selection.start.column) + ?.takeIf { it.kind == SemanticModel.SymbolKind.VARIABLE && it.declarationSource == source } + } + + data class Initializer( + val statement: AssignmentStatement, + val local: VariableDeclarationStatement, + val type: String, + ) + + fun initializer( + text: String, + statement: AssignmentStatement, + ): Initializer? { + if (statement.op.id != Token.Id.ASN) return null + val local = statement.lValue as? VariableDeclarationStatement ?: return null + val type = local.childNodes().filterIsInstance().singleOrNull() ?: return null + // Inference and Ref/Var annotations can change construction and storage semantics. + if (type is VariableTypeExpression || type is AnnotatedTypeExpression) return null + + fun offset(position: Long) = + XdkRename.offset( + text, + SemanticModel.Position(Source.calculateLine(position), Source.calculateOffset(position)), + ) + val start = offset(statement.startPosition) ?: return null + val typeStart = offset(type.startPosition) ?: return null + val typeEnd = offset(type.endPosition) ?: return null + val nameStart = offset(local.nameToken.startPosition) ?: return null + val nameEnd = offset(local.nameToken.endPosition) ?: return null + val valueStart = offset(statement.rValue.startPosition) ?: return null + if (text.substring(start, typeStart).isNotBlank() || text.substring(typeEnd, nameStart).isNotBlank() || + !Regex("\\s*=\\s*").matches(text.substring(nameEnd, valueStart)) + ) { + return null + } + return Initializer(statement, local, text.substring(typeStart, typeEnd)) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt new file mode 100644 index 00000000000..0dbe6305d77 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt @@ -0,0 +1,140 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.LiteralExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** + * Move a complete return expression or typed local initializer to an immediately preceding local + * without changing evaluation order. Initializers retain their written expected type. The graph proof must + * preserve relocated references and calls as well as every unaffected binding and dispatch chain. + */ +internal object XdkLocalExtraction { + data class Candidate( + val title: String, + val edits: List, + val relocation: XdkRename.Relocation, + ) + + private data class Selected( + val statement: AstNode, + val expression: Expression, + val type: String?, + ) + + fun candidate( + text: String, + selection: Range, + ): Candidate? { + val (start, end) = XdkRefactoringSyntax.selectionOffsets(text, selection) ?: return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "extract-local") ?: return null + + fun offset(position: Long): Int? = + XdkRename.offset(text, SemanticModel.Position(Source.calculateLine(position), Source.calculateOffset(position))) + + fun expressions( + node: AstNode, + method: MethodDeclarationStatement? = null, + ): List = + node.childNodes().flatMap { child -> + val owner = + when (node) { + is MethodDeclarationStatement -> node + is LambdaExpression -> null + else -> method + } + val candidate = + if (node is StatementBlock) { + when (child) { + is ReturnStatement -> { + child.expressions?.singleOrNull()?.let { + Selected(child, it, writtenReturnType(text, owner)) + } + } + + is AssignmentStatement -> { + XdkLocalDeclarations.initializer(text, child)?.let { + Selected(child, child.rValue, it.type) + } + } + + else -> { + null + } + } + } else { + null + } + listOfNotNull(candidate) + expressions(child, owner) + } + val selected = + expressions(root).singleOrNull { + offset(it.expression.startPosition) == start && offset(it.expression.endPosition) == end + } ?: return null + val literal = + (selected.expression as? LiteralExpression)?.literal?.id in setOf(Token.Id.LIT_INT, Token.Id.LIT_STRING, Token.Id.LIT_CHAR) + val type = + if (literal && selected.statement is ReturnStatement) { + "val" + } else { + selected.type ?: return null + } + val insertion = offset(selected.statement.startPosition) ?: return null + // Do not rewrite same-line siblings, labels, comments or expression-bodied declarations. + val indent = XdkRefactoringSyntax.indentBefore(text, insertion) ?: return null + val names = + ExecutionTrace.api("Lexer.lex(extract-local)") { + Lexer(Source(text), errors) + .asSequence() + .filter { it.id == Token.Id.IDENTIFIER } + .map { it.valueText } + .toSet() + } + if (errors.hasSeriousErrors()) return null + val name = generateSequence(0) { it + 1 }.map { if (it == 0) "extractedValue" else "extractedValue$it" }.first { it !in names } + val newline = Regex("\\r\\n|\\r|\\n").find(text)?.value ?: "\n" + val prefix = "$type $name = " + val declaration = XdkRename.Edit(insertion, insertion, "$prefix${text.substring(start, end)};$newline$indent") + return Candidate( + if (literal) "Extract literal to local variable" else "Extract expression to local variable", + listOf(declaration, XdkRename.Edit(start, end, name)), + XdkRename.Relocation(start, end, declaration, prefix.length), + ) + } + + fun writtenReturnType( + text: String, + declaration: MethodDeclarationStatement?, + ): String? { + val method = declaration?.takeUnless { it.isReturnConditional } ?: return null + val name = method.nameToken ?: return null + val result = + method + .childNodes() + .filterIsInstance() + .singleOrNull { it.type != null && it.endPosition <= name.startPosition } ?: return null + + fun offset(position: Long) = + XdkRename.offset( + text, + SemanticModel.Position(Source.calculateLine(position), Source.calculateOffset(position)), + ) + return text.substring( + offset(result.type.startPosition) ?: return null, + offset(result.type.endPosition) ?: return null, + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt new file mode 100644 index 00000000000..f0fdcf9f1e2 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt @@ -0,0 +1,203 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Inline single-use locals with either adjacent evaluation or compiler-proven constant values. */ +internal object XdkLocalInline { + data class Candidate( + val title: String, + val edits: List, + val relocation: XdkRename.Relocation, + val removed: Set, + val expression: SemanticModel.SourceLocation? = null, + ) + + private data class AdjacentUse( + val assignment: AssignmentStatement, + val statement: AstNode, + val expression: Expression, + val type: String, + ) + + fun candidate( + text: String, + selection: Range, + model: SemanticModel, + facts: CompilerRenameFacts, + ): Candidate? = adjacent(text, selection, model) ?: constant(text, selection, model, facts) + + private fun adjacent( + text: String, + selection: Range, + model: SemanticModel, + ): Candidate? { + val source = model.sourceName ?: return null + val selected = XdkLocalDeclarations.selectedSymbol(model, selection) ?: return null + val declaration = selected.declaration ?: return null + val uses = model.occurrences.filter { it.symbol == selected.id } + if (uses.size != 2 || uses.count { it.role == SemanticModel.Role.DECLARATION } != 1) return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "inline-local") ?: return null + + fun position(position: Long) = SemanticModel.Position(Source.calculateLine(position), Source.calculateOffset(position)) + + fun offset(position: Long) = XdkRename.offset(text, position(position)) + + fun pairs( + node: AstNode, + method: MethodDeclarationStatement? = null, + ): List { + val owner = + when (node) { + is MethodDeclarationStatement -> node + is LambdaExpression -> null + else -> method + } + val children = node.childNodes() + val adjacent = + if (node is StatementBlock) { + children.zipWithNext().mapNotNull { (first, second) -> + if (first !is AssignmentStatement) return@mapNotNull null + when (second) { + is ReturnStatement -> { + val expression = second.expressions?.singleOrNull() ?: return@mapNotNull null + val type = XdkLocalExtraction.writtenReturnType(text, owner) ?: return@mapNotNull null + AdjacentUse(first, second, expression, type) + } + + is AssignmentStatement -> { + XdkLocalDeclarations.initializer(text, second)?.let { + AdjacentUse(first, second, second.rValue, it.type) + } + } + + else -> { + null + } + } + } + } else { + emptyList() + } + return adjacent + children.flatMap { pairs(it, owner) } + } + val pair = + pairs(root).singleOrNull { + val local = it.assignment.lValue as? VariableDeclarationStatement + local != null && position(local.nameToken.startPosition) == declaration.start && + position(local.nameToken.endPosition) == declaration.end + } ?: return null + val assignment = pair.assignment + val initializer = XdkLocalDeclarations.initializer(text, assignment) ?: return null + val read = pair.expression as? NameExpression ?: return null + if (!read.isSimpleName || read.name != selected.name) return null + val readRange = SemanticModel.Range(position(read.startPosition), position(read.endPosition)) + if (uses.singleOrNull { it.role == SemanticModel.Role.REFERENCE }?.range != readRange) return null + if (initializer.type != pair.type) return null + val expression = assignment.rValue + val start = offset(assignment.startPosition) ?: return null + val valueStart = offset(expression.startPosition) ?: return null + val valueEnd = offset(expression.endPosition) ?: return null + val nextStart = offset(pair.statement.startPosition) ?: return null + val readStart = offset(read.startPosition) ?: return null + val readEnd = offset(read.endPosition) ?: return null + if (XdkRefactoringSyntax.indentBefore(text, start) == null) return null + val trailing = text.substring(valueEnd, nextStart) + // Do not erase comments or labels; the declaration must be on its own preceding line. + if (!Regex("\\s*;[ \\t]*(?:\\r\\n|\\r|\\n)[ \\t\\r\\n]*").matches(trailing)) return null + val destination = XdkRename.Edit(readStart, readEnd, text.substring(valueStart, valueEnd)) + val removed = + model.occurrences + .filter { + it.range == readRange || + ( + (XdkRename.offset(text, it.range.start) ?: -1) >= start && + (XdkRename.offset(text, it.range.end) ?: Int.MAX_VALUE) <= valueStart + ) + }.map { SemanticModel.SourceLocation(source, it.range) } + .toSet() + return Candidate( + if (pair.statement is ReturnStatement) "Inline returned local variable" else "Inline local variable into initializer", + listOf(XdkRename.Edit(start, nextStart, ""), destination), + XdkRename.Relocation(valueStart, valueEnd, destination, 0), + removed, + ) + } + + /** A constant needs no evaluation at its declaration; its sole read keeps its exact type. */ + private fun constant( + text: String, + selection: Range, + model: SemanticModel, + facts: CompilerRenameFacts, + ): Candidate? { + val source = model.sourceName ?: return null + val symbol = XdkLocalDeclarations.selectedSymbol(model, selection) ?: return null + val declared = SemanticModel.SourceLocation(source, symbol.declaration ?: return null) + if (declared !in facts.removableLocals) return null + val uses = model.occurrences.filter { it.symbol == symbol.id } + if (uses.size != 2 || uses.count { it.role == SemanticModel.Role.DECLARATION } != 1) return null + val use = uses.singleOrNull { it.role == SemanticModel.Role.REFERENCE && it.usage == SemanticModel.Usage.READ } ?: return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "inline-constant-local") ?: return null + + fun at(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + + fun offset(value: Long) = XdkRename.offset(text, at(value)) + val all = XdkRefactoringSyntax.tree(root).map { it.first }.toList() + val local = + all + .filterIsInstance() + .mapNotNull { XdkLocalDeclarations.initializer(text, it) } + .singleOrNull { at(it.local.nameToken.startPosition) == declared.range.start } ?: return null + val read = + all.filterIsInstance().singleOrNull { + it.isSimpleName && at(it.startPosition) == use.range.start && at(it.endPosition) == use.range.end + } ?: return null + val expression = local.statement.rValue + val valueStart = offset(expression.startPosition) ?: return null + val valueEnd = offset(expression.endPosition) ?: return null + val start = offset(local.statement.startPosition) ?: return null + val indent = XdkRefactoringSyntax.indentBefore(text, start) ?: return null + val lineStart = start - indent.length + // Keep comments and declaration trivia intact by refusing anything beyond whitespace. + val tail = Regex("[ \t]*;[ \t]*(?:\r\n|\r|\n)").find(text, valueEnd)?.takeIf { it.range.first == valueEnd } ?: return null + val header = + ExecutionTrace.api("Lexer.lex(inline-local-declaration)") { + Lexer(Source(text.substring(start, valueStart)), errors).asSequence().toList() + } + if (errors.hasSeriousErrors() || header.any { it.id in setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) }) return null + val destination = + XdkRename.Edit( + offset(read.startPosition) ?: return null, + offset(read.endPosition) ?: return null, + "(${text.substring(valueStart, valueEnd)})", + ) + val removed = + model.occurrences + .filter { + it.range == use.range || (XdkRename.offset(text, it.range.start) ?: -1) in start until valueStart + }.mapTo(linkedSetOf()) { SemanticModel.SourceLocation(source, it.range) } + return Candidate( + "Inline constant local variable", + listOf(XdkRename.Edit(lineStart, tail.range.last + 1, ""), destination), + XdkRename.Relocation(valueStart, valueEnd, destination, 1), + removed, + SemanticModel.SourceLocation(source, SemanticModel.Range(at(expression.startPosition), at(expression.endPosition))), + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt new file mode 100644 index 00000000000..fb6f2bf6745 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt @@ -0,0 +1,73 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Delete an unused plain local only after the compiler proves its initializer needs no evaluation. */ +internal object XdkLocalRemoval { + data class Candidate( + val edit: XdkRename.Edit, + val removed: Set, + ) + + fun candidate( + text: String, + selection: Range, + model: SemanticModel, + removable: Set, + ): Candidate? { + val source = model.sourceName ?: return null + val symbol = XdkLocalDeclarations.selectedSymbol(model, selection) ?: return null + val declaration = symbol.declaration ?: return null + if (SemanticModel.SourceLocation(source, declaration) !in removable) return null + val uses = model.occurrences.filter { it.symbol == symbol.id } + if (uses.singleOrNull()?.role != SemanticModel.Role.DECLARATION) return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "remove-local") ?: return null + + fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + + fun offset(value: Long) = XdkRename.offset(text, position(value)) + val candidate = + XdkRefactoringSyntax + .tree(root) + .map { it.first } + .filterIsInstance() + .flatMap { it.childNodes().asSequence() } + .filterIsInstance() + .mapNotNull { XdkLocalDeclarations.initializer(text, it) } + .singleOrNull { + position(it.local.nameToken.startPosition) == declaration.start && + position(it.local.nameToken.endPosition) == declaration.end + } + ?: return null + val start = offset(candidate.statement.startPosition) ?: return null + val end = offset(candidate.statement.endPosition) ?: return null + val indent = XdkRefactoringSyntax.indentBefore(text, start) ?: return null + val lineStart = start - indent.length + val tail = Regex("[ \t]*;").find(text, end)?.takeIf { it.range.first == end } ?: return null + val syntaxEnd = tail.range.last + 1 + val tokens = + ExecutionTrace.api("Lexer.lex(remove-local)") { + Lexer(Source(text.substring(start, syntaxEnd)), errors).asSequence().toList() + } + // Never discard a comment hidden among the declaration or expression tokens. + if (errors.hasSeriousErrors() || tokens.any { it.id in setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) }) return null + val lineEnd = Regex("[ \t]*(?:\r\n|\r|\n)").find(text, syntaxEnd)?.takeIf { it.range.first == syntaxEnd } + val edit = if (lineEnd == null) XdkRename.Edit(start, syntaxEnd, "") else XdkRename.Edit(lineStart, lineEnd.range.last + 1, "") + val removed = + model.occurrences + .filter { + (XdkRename.offset(text, it.range.start) ?: -1) >= start && + (XdkRename.offset(text, it.range.end) ?: Int.MAX_VALUE) <= syntaxEnd + }.map { SemanticModel.SourceLocation(source, it.range) } + .toSet() + return Candidate(edit, removed) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt new file mode 100644 index 00000000000..7ba10edcaf9 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt @@ -0,0 +1,138 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.Range + +/** Immutable contracts and atomic class-member edits. */ +internal object XdkMemberActions { + data class Import( + val position: SemanticModel.Position, + val declaration: String, + ) + + data class Edits( + val member: XdkRename.Edit, + val imports: List, + ) { + val all: List + get() = imports + member + } + + data class Candidate( + val owner: SemanticModel.SourceLocation, + val contract: ProofIdentity, + val insertion: SemanticModel.Position, + val declaration: String, + val implementation: Boolean, + val requiredCount: Int, + val imports: List, + ) { + val title: String + get() = "${if (implementation) "Implement" else "Override"} $declaration" + + fun selected( + source: String, + range: Range, + ): Boolean = + owner.sourceName == source && + owner.range.start <= SemanticModel.Position(range.end.line, range.end.column) && + owner.range.end >= SemanticModel.Position(range.start.line, range.start.column) + } + + data class Action( + val members: List, + ) { + init { + require(members.isNotEmpty()) + require(members.map { it.owner to it.insertion }.distinct().size == 1) + require( + members.size == 1 || + members.all { it.implementation && it.requiredCount == members.size }, + ) + } + + val title: String + get() = + members.singleOrNull()?.title ?: "Implement all required members (${members.size})" + + fun edit(text: String): Edits? { + val owner = members.first().owner + val at = XdkRename.offset(text, members.first().insertion) ?: return null + if (text.getOrNull(at) != '}') return null + val newline = if ("\r\n" in text) "\r\n" else "\n" + val lineStart = text.lastIndexOf('\n', at - 1) + 1 + val whitespace = text.substring(lineStart, at) + val ownLine = whitespace.all { it == ' ' || it == '\t' } + val ownerLine = text.lineSequence().elementAtOrNull(owner.range.start.line).orEmpty() + val indent = ownerLine.takeWhile { it == ' ' || it == '\t' } + val memberIndent = "$indent " + val body = + members.joinToString(newline) { member -> + "$memberIndent@Override$newline$memberIndent${member.declaration} {$newline" + + "$memberIndent TODO();$newline$memberIndent}$newline" + } + val memberEdit = + if (ownLine) { + XdkRename.Edit(lineStart, lineStart, body) + } else { + XdkRename.Edit(at, at, "$newline$body$indent") + } + val imports = importEdits(text, members.flatMap { it.imports }) ?: return null + return Edits(memberEdit, imports) + } + } + + /** Reuse existing aliases and allocate deterministic names that cannot capture source tokens. */ + fun moduleAliases( + modules: Collection, + existing: Map, + names: Set, + ): Map = + modules.distinct().sorted().fold(existing) { known, module -> + if (module in known) { + known + } else { + val base = module.substringBefore('.').replaceFirstChar { it.lowercase() } + val alias = + generateSequence(0) { it + 1 } + .map { if (it == 0) base else "$base$it" } + .first { it !in names && it !in known.values && XdkRename.identifier(it) } + known + (module to alias) + } + } + + /** Both member intentions and missing-method fixes apply imports and declarations atomically. */ + fun importEdits( + text: String, + imports: List, + ): List? { + val newline = Regex("\r\n|\r|\n").find(text)?.value ?: "\n" + return imports.distinct().groupBy { it.position }.map { (position, declarations) -> + val offset = XdkRename.offset(text, position) ?: return null + if (offset != 0 && text.getOrNull(offset - 1) != '{') return null + val block = declarations.joinToString(newline, postfix = newline) { it.declaration } + XdkRename.Edit(offset, offset, if (offset == 0) block else "$newline$block") + } + } + + fun actions( + candidates: List, + source: String, + range: Range, + ): List { + val selected = candidates.filter { it.selected(source, range) }.distinct() + val combined = + selected + .filter { it.implementation } + .groupBy { it.owner to it.insertion } + .values + .filter { it.size > 1 && it.size == it.first().requiredCount } + .map { Action(it.sortedBy(Candidate::declaration)) } + // A combined repair must contain all available required members, even when individual + // actions exceed the query limit. The compiler can accept implicitly abstract classes, so + // successful compilation alone does not establish that all required members were generated. + return ( + combined + + selected.sortedByDescending { it.implementation }.map { Action(listOf(it)) } + ).take(32) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt new file mode 100644 index 00000000000..de00d34dee5 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt @@ -0,0 +1,144 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotationExpression +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypeExpression +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Copy one private member expression at its use; declaration deletion is a separate proof. */ +internal object XdkMemberInline { + data class Candidate( + val title: String, + val source: String, + val expression: SemanticModel.Range, + val use: SemanticModel.Range, + val edit: XdkRename.Edit, + val ignored: Set, + ) + + fun candidate( + text: String, + selection: Range, + model: SemanticModel, + facts: CompilerRenameFacts, + ): Candidate? { + val source = model.sourceName ?: return null + val symbol = model.symbolAt(selection.start.line, selection.start.column) ?: return null + if (symbol.declarationSource != source) return null + val declaration = symbol.declaration ?: return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "inline-member") ?: return null + + val parents = XdkRefactoringSyntax.tree(root).toMap() + + fun at(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + + fun range(node: AstNode) = SemanticModel.Range(at(node.startPosition), at(node.endPosition)) + + fun owner(node: AstNode) = + generateSequence(parents[node]) { parents[it] }.filterIsInstance().firstOrNull() + val selected = SemanticModel.Position(selection.start.line, selection.start.column) + val read = + parents.keys.filterIsInstance().singleOrNull { + it.isSimpleName && !it.hasTrailingTypeParams() && !it.isSuppressDeref && selected in range(it) + } ?: return null + val (member, use, expression) = + when (symbol.kind) { + SemanticModel.SymbolKind.METHOD -> { + val signature = symbol.signature ?: return null + if (signature.parameters.isNotEmpty() || signature.conditional || signature.returns.size != 1) return null + val method = + parents.keys.filterIsInstance().singleOrNull { + it.nameToken?.startPosition?.let(::at) == declaration.start + } ?: return null + if (method.defaultAccess != Access.PRIVATE || method.isConstructor || method.isConstructorFinally) return null + val block = method.childNodes().filterIsInstance().singleOrNull() ?: return null + val returned = block.childNodes().singleOrNull() as? ReturnStatement ?: return null + val value = returned.expressions?.singleOrNull() ?: return null + val call = parents[read] as? InvocationExpression ?: return null + if (call.isAsync || call.childNodes().singleOrNull() !== read) return null + Triple(method, call, value) + } + + SemanticModel.SymbolKind.PROPERTY -> { + if (SemanticModel.SourceLocation(source, declaration) !in facts.constantProperties) return null + val property = + parents.keys.filterIsInstance().singleOrNull { + at(it.nameToken.startPosition) == declaration.start + } ?: return null + if (property.defaultAccess != Access.PRIVATE || !property.isStatic) return null + val value = + property + .childNodes() + .filterIsInstance() + .filterNot { it is TypeExpression } + .singleOrNull() ?: return null + if (property.childNodes().any { it is StatementBlock }) return null + Triple(property, read, value) + } + + else -> { + return null + } + } + if (owner(member) !== owner(use)) return null + // A local callable or closure can have a different receiver or capture environment. + if (generateSequence(parents[use]) { parents[it] } + .takeWhile { it !== owner(use) } + .any { it is LambdaExpression } + ) { + return null + } + if (XdkRefactoringSyntax.tree(member).any { (node, _) -> node is AnnotationExpression }) return null + if (XdkRefactoringSyntax.tree(expression).any { (node, _) -> + node is LambdaExpression || node is TypeCompositionStatement + } + ) { + return null + } + val expressionRange = range(expression) + val useRange = range(use) + if (useRange.start >= expressionRange.start && useRange.end <= expressionRange.end) return null + val from = XdkRename.offset(text, expressionRange.start) ?: return null + val to = XdkRename.offset(text, expressionRange.end) ?: return null + val value = text.substring(from, to) + val tokens = ExecutionTrace.api("Lexer.lex(inline-member)") { Lexer(Source(value), errors).asSequence().toList() } + if (errors.hasSeriousErrors() || tokens.any { it.id == Token.Id.BIT_AND || it.valueText == "super" }) return null + // Copying a recursive invocation or escaped self-reference is not method expansion. + if (model.occurrences.any { + it.symbol == symbol.id && it.range.start >= expressionRange.start && it.range.end <= expressionRange.end + } + ) { + return null + } + val start = XdkRename.offset(text, useRange.start) ?: return null + val end = XdkRename.offset(text, useRange.end) ?: return null + val ignored = + model.occurrences + .filter { it.range.start >= useRange.start && it.range.end <= useRange.end } + .mapTo(linkedSetOf()) { SemanticModel.SourceLocation(source, it.range) } + return Candidate( + if (symbol.kind == SemanticModel.SymbolKind.METHOD) "Inline private method call" else "Inline constant property", + source, + expressionRange, + useRange, + XdkRename.Edit(start, end, "($value)"), + ignored, + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt new file mode 100644 index 00000000000..6f29cbe7988 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt @@ -0,0 +1,245 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotatedTypeExpression +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.ExpressionStatement +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypeExpression +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.compiler.ast.VariableTypeExpression +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Same-owner expression extraction. Syntax proposes the helper; compiler facts prove the edit. */ +internal object XdkMethodExtraction { + data class Capture( + val declaration: SemanticModel.SourceLocation, + val kind: SemanticModel.SymbolKind, + val name: String, + val type: String, + val parameterOffset: Int, + val argumentOffset: Int, + ) + + data class Candidate( + val name: String, + val expression: SemanticModel.SourceLocation, + val insertion: XdkRename.Edit, + val replacement: XdkRename.Edit, + val relocation: XdkRename.Relocation, + val methodOffset: Int, + val captures: List, + val statements: Boolean = false, + ) { + val edits: List get() = listOf(replacement, insertion) + } + + fun candidate( + text: String, + selection: Range, + model: SemanticModel, + facts: ExtractMethodFacts, + ): Candidate? { + val source = model.sourceName ?: return null + val (start, end) = XdkRefactoringSyntax.selectionOffsets(text, selection) ?: return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "extract-method") ?: return null + + fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + + fun offset(value: Long) = XdkRename.offset(text, position(value)) + + fun spelling(node: AstNode): String? { + val from = offset(node.startPosition) ?: return null + val to = offset(node.endPosition) ?: return null + return text.substring(from, to) + } + + // A freshly parsed tree has no adopted parent links. Keep ownership local to this query. + val parents = XdkRefactoringSyntax.tree(root).toMap() + val all = parents.keys + val expression = + all.filterIsInstance().singleOrNull { offset(it.startPosition) == start && offset(it.endPosition) == end } + + fun statementEnd(node: AstNode): Int? { + val end = offset(node.endPosition) ?: return null + return Regex("[ \t]*;") + .find(text, end) + ?.takeIf { it.range.first == end } + ?.range + ?.last + ?.plus(1) + } + val statements = + if (expression != null) { + emptyList() + } else { + all + .filterIsInstance() + .map { block -> + block.childNodes().filter { node -> + (offset(node.startPosition) ?: -1) >= start && (offset(node.startPosition) ?: Int.MAX_VALUE) < end + } + }.singleOrNull { selected -> + selected.isNotEmpty() && selected.all { it is ExpressionStatement } && + offset(selected.first().startPosition) == start && statementEnd(selected.last()) == end + } ?: return null + } + val roots = expression?.let(::listOf) ?: statements + val ancestors = generateSequence(parents[roots.first()]) { parents[it] }.toList() + val method = ancestors.filterIsInstance().firstOrNull() ?: return null + if (ancestors.takeWhile { it !== method }.any { it is LambdaExpression || it is TypeCompositionStatement }) return null + // A local function has a different closure and cannot acquire a same-owner private method. + if (generateSequence(parents[method]) { parents[it] } + .takeWhile { it !is TypeCompositionStatement } + .any { it is MethodDeclarationStatement || it is LambdaExpression } + ) { + return null + } + val methodName = method.nameToken ?: return null + val owner = model.symbolAt(position(methodName.startPosition).line, position(methodName.startPosition).column) ?: return null + val signature = owner.signature ?: return null + if (owner.kind != SemanticModel.SymbolKind.METHOD || signature.conditional || + signature.parameters.any { it.typeParameter } + ) { + return null + } + + fun at(offset: Int) = XdkRename.position(text, offset).let { SemanticModel.Position(it.line, it.column) } + val range = SemanticModel.Range(at(start), at(end)) + val selectedLocation = SemanticModel.SourceLocation(source, range) + val resultType = + if (expression == null) { + "void" + } else { + when (val statement = parents[expression]) { + is ReturnStatement -> { + if (statement.expressions?.singleOrNull() === expression) { + XdkLocalExtraction.writtenReturnType(text, method) + } else { + null + } + } + + is AssignmentStatement -> { + if (statement.rValue === expression) XdkLocalDeclarations.initializer(text, statement)?.type else null + } + + else -> { + null + } + } ?: facts.sourceTypes[selectedLocation] ?: return null + } + val selectedNodes = roots.flatMap { XdkRefactoringSyntax.tree(it).map { entry -> entry.first }.toList() } + if (selectedNodes.any { + it is LambdaExpression || it is TypeCompositionStatement || (it is InvocationExpression && it.isAsync) + } + ) { + return null + } + val tokens = ExecutionTrace.api("Lexer.lex(extract-method)") { Lexer(Source(text), errors).asSequence().toList() } + if (errors.hasSeriousErrors()) return null + // Ref-taking and super dispatch need a different contract. Preserve bitwise expressions in + // a later slice once syntax distinguishes them from address-taking at the extraction site. + val selectedTokens = + ExecutionTrace.api("Lexer.lex(extract-method-selection)") { + Lexer(Source(text.substring(start, end)), errors).asSequence().toList() + } + if (selectedTokens.any { it.id == Token.Id.BIT_AND || it.valueText == "super" }) return null + if (expression != null && selectedLocation !in facts.types) return null + val occurrences = model.occurrences.filter { it.range.start >= range.start && it.range.end <= range.end } + val inputs = + occurrences + .mapNotNull { it.symbol?.let(model::symbol) } + .filter { it.kind in setOf(SemanticModel.SymbolKind.VARIABLE, SemanticModel.SymbolKind.PARAMETER) } + .distinctBy { it.id } + val captured = + inputs.map { symbol -> + val declaration = SemanticModel.SourceLocation(symbol.declarationSource, symbol.declaration ?: return null) + if (declaration.sourceName != source || declaration !in facts.stableValues || declaration !in facts.types) return null + if (occurrences.filter { it.symbol == symbol.id }.any { + it.role != SemanticModel.Role.REFERENCE || + it.usage != SemanticModel.Usage.READ + } + ) { + return null + } + val written = + all.singleOrNull { + val token = + when (it) { + is Parameter -> it.nameToken + is VariableDeclarationStatement -> it.nameToken + else -> null + } + token != null && position(token.startPosition) == declaration.range.start && + position(token.endPosition) == declaration.range.end + } ?: return null + val type = + when (written) { + is Parameter -> { + if (parents[written] !== method) return null + written.type + } + + is VariableDeclarationStatement -> { + written.childNodes().filterIsInstance().singleOrNull() ?: return null + } + + else -> { + return null + } + } + if (type is VariableTypeExpression || type is AnnotatedTypeExpression) return null + Capture(declaration, symbol.kind, symbol.name, spelling(type) ?: return null, 0, 0) + } + val names = tokens.filter { it.id == Token.Id.IDENTIFIER }.map { it.valueText }.toSet() + model.symbols.map { it.name } + val name = generateSequence(0) { it + 1 }.map { if (it == 0) "extractedMethod" else "extractedMethod$it" }.first { it !in names } + val methodStart = offset(method.startPosition) ?: return null + val indent = XdkRefactoringSyntax.indentBefore(text, methodStart) ?: return null + val insertionAt = offset(method.endPosition) ?: return null + val tail = text.substring(insertionAt).takeWhile { it != '\r' && it != '\n' } + if (tail.isNotBlank()) return null + val newline = Regex("\r\n|\r|\n").find(text)?.value ?: "\n" + val modifiers = if (SemanticModel.Modifier.STATIC in owner.modifiers) "private static" else "private" + val header = "$newline$newline$indent$modifiers $resultType " + val parameterPrefix = "$header$name(" + val parameters = captured.map { "${it.type} ${it.name}" } + val prefix = "$parameterPrefix${parameters.joinToString(", ")}) {$newline$indent ${if (expression == null) "" else "return "}" + val suffix = "${if (expression == null) "" else ";"}$newline$indent}" + val insertion = XdkRename.Edit(insertionAt, insertionAt, "$prefix${text.substring(start, end)}$suffix") + val replacement = + XdkRename.Edit( + start, + end, + "$name(${captured.joinToString(", ") { it.name }})${if (expression == null) ";" else ""}", + ) + return Candidate( + name, + selectedLocation, + insertion, + replacement, + XdkRename.Relocation(start, end, insertion, prefix.length), + header.length, + captured.mapIndexed { index, capture -> + capture.copy( + parameterOffset = parameterPrefix.length + parameters.take(index).sumOf { it.length + 2 } + capture.type.length + 1, + argumentOffset = name.length + 1 + captured.take(index).sumOf { it.name.length + 2 }, + ) + }, + expression == null, + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt new file mode 100644 index 00000000000..5f31938b8ad --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt @@ -0,0 +1,162 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.NewExpression +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.adapter.Range + +/** Syntax chooses a writable declaration; a complete compiler repair must prove its use. */ +internal object XdkMissingDeclarations { + data class Candidate( + val name: String, + val kind: SemanticModel.SymbolKind, + val use: SemanticModel.SourceLocation, + val edit: XdkRename.Edit, + private val expectedType: ProofIdentity? = null, + ) { + val title: String get() = "Create ${if (kind == SemanticModel.SymbolKind.TYPE) "class" else "read-only property"} '$name'" + + fun proves( + after: CompilerRenameFacts, + plan: XdkRename.Plan, + ): Boolean { + val source = use.sourceName ?: return false + val changed = plan.proposed[source] ?: return false + val at = plan.mapPosition(source, use.range.start) ?: return false + val model = after.models.singleOrNull { it.sourceName == source } ?: return false + val selected = model.symbolAt(at.line, at.column) ?: return false + val declaration = selected.declaration ?: return false + val insertedAt = (plan.map(source, edit.start) ?: return false) - edit.text.length + val declarationAt = XdkRename.offset(changed, declaration.start) ?: return false + if (selected.name != name || selected.kind != kind || selected.declarationSource != source || + declarationAt !in insertedAt until insertedAt + edit.text.length + ) { + return false + } + if (expectedType != null) { + val finish = plan.mapPosition(source, use.range.end) ?: return false + val location = + SemanticModel.SourceLocation( + source, + SemanticModel.Range(at, finish), + ) + val actual = after.extraction.types[location] ?: return false + if (!XdkRename.sameType(expectedType, actual, plan)) return false + } + return true + } + } + + fun candidates( + text: String, + selection: Range, + model: SemanticModel, + facts: CompilerRenameFacts, + ): List { + val source = model.sourceName ?: return emptyList() + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "missing-declarations") ?: return emptyList() + + val parents = XdkRefactoringSyntax.tree(root).toMap() + + fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + + fun location(node: AstNode) = + SemanticModel.SourceLocation(source, SemanticModel.Range(position(node.startPosition), position(node.endPosition))) + + fun selected(node: AstNode): Boolean { + val range = location(node).range + return range.start <= SemanticModel.Position(selection.end.line, selection.end.column) && + range.end >= SemanticModel.Position(selection.start.line, selection.start.column) + } + + fun insertion( + owner: TypeCompositionStatement, + declaration: String, + ): XdkRename.Edit? { + val at = XdkRename.offset(text, position(owner.ensureBody().endPosition))?.minus(1) ?: return null + if (text.getOrNull(at) != '}') return null + val newline = Regex("\r\n|\r|\n").find(text)?.value ?: "\n" + val indent = + text.lineSequence().elementAtOrNull(position(owner.startPosition).line).orEmpty().takeWhile { + it == ' ' || + it == '\t' + } + return XdkRename.Edit(at, at, "$newline$indent $declaration$newline$indent") + } + return parents.keys + .mapNotNull { node -> + when (node) { + is NewExpression -> { + if (node.isVirtualNew || node.hasSquareBrackets() || node.arguments.isNotEmpty() || + node.isComponentNode || node.unregisteredBody.isPresent + ) { + return@mapNotNull null + } + val type = node.childNodes().singleOrNull() as? NamedTypeExpression ?: return@mapNotNull null + val name = type.name.takeIf(XdkRename::identifier) ?: return@mapNotNull null + if (!selected(type)) return@mapNotNull null + val start = XdkRename.offset(text, position(type.startPosition)) ?: return@mapNotNull null + val end = XdkRename.offset(text, position(type.endPosition)) ?: return@mapNotNull null + if (text.substring(start, end) != name) return@mapNotNull null + val owner = + generateSequence(parents[node]) { parents[it] }.filterIsInstance().firstOrNull() + ?: return@mapNotNull null + if (owner.category.id != Token.Id.MODULE) return@mapNotNull null + Candidate( + name, + SemanticModel.SymbolKind.TYPE, + location(type), + insertion(owner, "class $name {}") ?: return@mapNotNull null, + ) + } + + is NameExpression -> { + if (!node.isSimpleName || node.hasTrailingTypeParams() || node.isSuppressDeref || + !selected(node) + ) { + return@mapNotNull null + } + val returned = parents[node] as? ReturnStatement ?: return@mapNotNull null + if (returned.expressions?.singleOrNull() !== node) return@mapNotNull null + val method = + generateSequence(parents[node]) { parents[it] }.filterIsInstance().firstOrNull() + ?: return@mapNotNull null + val owner = + parents[parents[method]] as? TypeCompositionStatement + ?: return@mapNotNull null.also { + System.err.println( + "DECL owner ${parents[method]} / ${parents[parents[method]]}", + ) + } + val methodToken = method.nameToken ?: return@mapNotNull null + val methodAt = position(methodToken.startPosition) + val declared = model.symbolAt(methodAt.line, methodAt.column) ?: return@mapNotNull null + if (declared.signature?.conditional != false || + declared.signature.parameters.any { it.typeParameter } + ) { + return@mapNotNull null + } + val type = XdkLocalExtraction.writtenReturnType(text, method) ?: return@mapNotNull null + val expected = facts.methodSignatures[declared.id]?.returns?.singleOrNull() ?: return@mapNotNull null + // Static properties are constants: the compiler requires a value and + // forbids custom getters. A repair cannot invent that value. + if (SemanticModel.Modifier.STATIC in declared.modifiers) return@mapNotNull null + val edit = insertion(owner, "private $type ${node.name}.get() { TODO(); }") ?: return@mapNotNull null + Candidate(node.name, SemanticModel.SymbolKind.PROPERTY, location(node), edit, expected) + } + + else -> { + null + } + } + }.distinct() + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt new file mode 100644 index 00000000000..49fd0f3b555 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt @@ -0,0 +1,183 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.Range + +/** Detached signatures from fresh declarations; only a complete repair proof can publish them. */ +internal object XdkMissingMethods { + enum class Dispatch { + INSTANCE, + STATIC, + } + + data class Receiver( + val destination: SemanticModel.SourceLocation, + val dispatch: Dispatch, + ) + + /** Source ownership and spelling context copied from a successfully compiled project module. */ + data class Destination( + val location: SemanticModel.SourceLocation, + val insertion: SemanticModel.Position, + val modules: Map, + val imports: Map = emptyMap(), + val importSource: String? = null, + ) + + data class TypeSource( + val source: String, + val imports: List = emptyList(), + ) + + data class Signature( + val parameters: List, + val returns: List, + val isPublic: Boolean = true, + val formals: List = emptyList(), + val conditional: Boolean = false, + ) + + /** Detached body evidence copied before the fresh declaration attempt checks eligibility. */ + data class Inputs( + val localTypes: Map = emptyMap(), + val receivers: Map = emptyMap(), + val expressions: Map = emptyMap(), + ) + + data class LocalType( + val source: String?, + val identity: ProofIdentity, + val destinationSources: Map = emptyMap(), + ) + + data class ArgumentBinding( + val use: SemanticModel.SourceLocation, + val declaration: SemanticModel.SourceLocation, + ) + + data class Candidate( + val callee: SemanticModel.SourceLocation, + val name: String, + val insertion: SemanticModel.Position, + val destination: SemanticModel.SourceLocation, + val declaration: String, + val dispatch: Dispatch, + val arguments: List = emptyList(), + val publicOwner: String? = null, + val signature: Signature? = null, + val importSource: String? = null, + val imports: List = emptyList(), + ) { + val title: String get() = publicOwner?.let { "Create public method '$name' in '$it'" } ?: "Create private method '$name'" + + fun selected( + source: String, + range: Range, + ): Boolean = + callee.sourceName == source && + callee.range.start <= SemanticModel.Position(range.end.line, range.end.column) && + callee.range.end >= SemanticModel.Position(range.start.line, range.start.column) + + fun edit( + text: String, + importText: String = text, + ): XdkMemberActions.Edits? { + val at = XdkRename.offset(text, insertion) ?: return null + if (text.getOrNull(at) != '}') return null + val newline = Regex("\r\n|\r|\n").find(text)?.value ?: "\n" + val lineStart = maxOf(text.lastIndexOf('\n', at - 1), text.lastIndexOf('\r', at - 1)) + 1 + val ownLine = text.substring(lineStart, at).all { it == ' ' || it == '\t' } + val indent = + text + .lineSequence() + .elementAtOrNull(destination.range.start.line) + .orEmpty() + .takeWhile { it == ' ' || it == '\t' } + val body = "$indent $declaration {$newline$indent TODO();$newline$indent }$newline" + val member = if (ownLine) XdkRename.Edit(lineStart, lineStart, body) else XdkRename.Edit(at, at, "$newline$body$indent") + return XdkMemberActions.Edits(member, XdkMemberActions.importEdits(importText, imports) ?: return null) + } + + /** Successful compilation must resolve this call to the inserted declaration itself. */ + fun bindsNewMethod( + after: CompilerRenameFacts, + plan: XdkRename.Plan, + edit: XdkRename.Edit, + ): Boolean { + val source = callee.sourceName ?: return false + val at = plan.mapPosition(source, callee.range.start) ?: return false + val model = after.models.singleOrNull { it.sourceName == source } ?: return false + val symbol = model.symbolAt(at.line, at.column) ?: return false + val declaration = symbol.declaration ?: return false + val targetSource = destination.sourceName ?: return false + val targetText = plan.proposed[targetSource] ?: return false + val declarationAt = XdkRename.offset(targetText, declaration.start) ?: return false + if (edit !in plan.edits[targetSource].orEmpty()) return false + val insertedAt = (plan.map(targetSource, edit.start) ?: return false) - edit.text.length + if (symbol.kind != SemanticModel.SymbolKind.METHOD || symbol.name != name || symbol.declarationSource != targetSource || + (SemanticModel.Modifier.STATIC in symbol.modifiers) != (dispatch == Dispatch.STATIC) || + declarationAt !in insertedAt until insertedAt + edit.text.length + ) { + return false + } + + if (signature != null || publicOwner != null) { + val expected = signature ?: return false + // A dependency use has a different snapshot-local symbol id. Read the signature + // from the exact source declaration selected by the completed caller instead. + val target = after.models.singleOrNull { it.sourceName == targetSource } ?: return false + val declared = target.symbolAt(declaration.start.line, declaration.start.column) ?: return false + if (declared.declaration != declaration || declared.name != name) return false + val actual = after.methodSignatures[declared.id] ?: return false + if (actual.isPublic != expected.isPublic || actual.conditional != expected.conditional || + actual.formals.size != expected.formals.size + ) { + return false + } + // New method formals are alpha-renamed binders. Compare their constraints and + // all uses by ordinal, never by source spelling or assignability. + val binders = expected.formals.zip(actual.formals).toMap() + + fun sameType( + old: ProofIdentity, + new: ProofIdentity, + ): Boolean = + when (old) { + in binders -> { + binders[old] == new + } + + is ProofIdentity.TypeShape if new is ProofIdentity.TypeShape -> { + old.format == new.format && old.components.size == new.components.size && + old.components.zip(new.components).all { (first, second) -> sameType(first, second) } + } + + is ProofIdentity.Alternatives if new is ProofIdentity.Alternatives -> { + old.targets.size == new.targets.size && + old.targets.all { first -> new.targets.any { sameType(first, it) } } + } + + else -> { + XdkRename.sameType(old, new, plan) + } + } + + fun equivalent( + expected: List, + actual: List, + ) = expected.size == actual.size && expected.zip(actual).all { (old, new) -> sameType(old, new) } + if (!equivalent(expected.parameters, actual.parameters) || !equivalent(expected.returns, actual.returns)) return false + } + + fun mapped(location: SemanticModel.SourceLocation): SemanticModel.Position? { + if (location.sourceName != source) return null + return plan.mapPosition(source, location.range.start) + } + return arguments.all { argument -> + val use = mapped(argument.use) ?: return@all false + val target = mapped(argument.declaration) ?: return@all false + val bound = model.symbolAt(use.line, use.column) ?: return@all false + bound.kind == SemanticModel.SymbolKind.VARIABLE && bound.declarationSource == source && bound.declaration?.start == target + } + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt new file mode 100644 index 00000000000..388f249a306 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt @@ -0,0 +1,132 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.ConstantPool +import org.xvm.asm.Constants.Access +import org.xvm.asm.FileStructure +import org.xvm.asm.constants.IdentityConstant +import org.xvm.asm.constants.MethodConstant +import org.xvm.asm.constants.MultiMethodConstant +import org.xvm.asm.constants.PropertyConstant +import org.xvm.lsp.adapter.SymbolMoniker +import org.xvm.lsp.util.ExecutionTrace +import java.io.ByteArrayOutputStream +import java.security.MessageDigest +import java.util.HexFormat +import java.util.Map.copyOf as immutableMap + +/** + * Content-addressed identities, version 1. Only build time and checkout directory are normalized; + * code, signatures, module versions, resources and relative source/debug information remain inputs. + * Original constant indices map to the normalized artifact's indices, never to display signatures. + * This table retains no compiler objects and does not change the host artifact or source index. + */ +internal class XdkArtifactSymbols private constructor( + private val revision: String, + entries: Map, +) { + private val entries = immutableMap(entries) + + private data class Entry( + val index: Int, + val exported: Boolean, + ) + + fun moniker( + index: Int, + imported: Boolean, + ): SymbolMoniker? = + entries[index]?.let { + SymbolMoniker( + "ecstasy-artifact-v1", + "$revision/${it.index}", + when { + !it.exported -> SymbolMoniker.Kind.LOCAL + imported -> SymbolMoniker.Kind.IMPORT + else -> SymbolMoniker.Kind.EXPORT + }, + ) + } + + companion object { + fun capture(bytes: ByteArray): XdkArtifactSymbols = + ExecutionTrace.api("FileStructure.symbolIdentities") { + val file = FileStructure(bytes.inputStream()) + ConstantPool.withPool(file.constantPool).use { + val identities = + file.constantPool.constants + .filterIsInstance() + .filter { + it.moduleConstant == file.moduleId && it.format in FORMATS && + (it !is MethodConstant || !it.isLambda) && it.component != null && + (it !is PropertyConstant || !it.isFormalType) + }.associateBy { it.position } + file.moduleIds().forEach { id -> + file.getModule(id).apply { + timestamp = null + sourceDir = null + } + } + val canonical = ByteArrayOutputStream().also(file::writeTo).toByteArray() + val revision = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(canonical)) + val entries = + identities + .mapNotNull { (original, identity) -> + val current = + file.constantPool.getConstant(identity) as? IdentityConstant + ?: return@mapNotNull null + val exported = + generateSequence(current) { it.parentConstant } + .filterNot { it is MultiMethodConstant } + .all { + it.component?.access in setOf(Access.PUBLIC, Access.PROTECTED) && + (it == current || it !is MethodConstant) + } + original to Entry(current.position, exported) + }.toMap() + XdkArtifactSymbols(revision, entries) + } + } + + private val FORMATS = + setOf( + Constant.Format.Module, + Constant.Format.Package, + Constant.Format.Class, + Constant.Format.Method, + Constant.Format.Property, + ) + } +} + +/** Run only on the compiler worker. Failed/partial attempts and unproven bindings supply no IDs. */ +internal object XdkMonikers { + fun capture( + artifact: XdkDependency, + bindings: Map, + dependencies: XdkDependencies.Open, + ): Map { + val own = artifact.open() + return bindings + .mapNotNull { (symbol, constant) -> + val identity = constant as? IdentityConstant ?: return@mapNotNull null + val module = identity.moduleConstant.name + val imported = module != artifact.module + val pool = + when { + !imported -> own.constantPool + module in dependencies.artifacts -> dependencies.repository.loadModule(module)?.constantPool + else -> XdkLibraries.module(module)?.constantPool + } ?: return@mapNotNull null + val index = pool.getConstant(identity)?.position ?: return@mapNotNull null + val table = + when { + !imported -> artifact.symbolIndex + module in dependencies.artifacts -> dependencies.artifacts.getValue(module).symbolIndex + else -> XdkLibraries.symbolIndex(module) + } + table?.moniker(index, imported)?.let { symbol to it } + }.toMap() + .let(::immutableMap) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt new file mode 100644 index 00000000000..d2242e78011 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt @@ -0,0 +1,37 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.TypeCompositionStatement + +/** Fresh, capture-free module aliases for a proposed ownership change. No compiler state escapes. */ +internal object XdkMoveImports { + data class Imports( + val aliases: Map, + val edit: XdkRename.Edit, + ) + + fun plan( + root: String, + texts: Collection, + modules: Set, + ): Imports? { + val errors = ErrorList() + val names = texts.flatMap { Lexer(Source(it), errors).asSequence().map { token -> token.valueText }.toList() }.toSet() + val aliases = XdkMemberActions.moduleAliases(modules, emptyMap(), names) + + fun nodes(node: AstNode): Sequence = sequenceOf(node) + node.childNodes().asSequence().flatMap(::nodes) + val parsed = Parser(Source(root), errors).parseSource() ?: return null + val module = + nodes(parsed).filterIsInstance().singleOrNull { it.category.id == Token.Id.MODULE } ?: return null + if (errors.hasSeriousErrors()) return null + val start = module.ensureBody().startPosition + val position = SemanticModel.Position(Source.calculateLine(start), Source.calculateOffset(start) + 1) + val declarations = aliases.map { (name, alias) -> XdkMemberActions.Import(position, " package $alias import $name;") } + return Imports(aliases, XdkMemberActions.importEdits(root, declarations)?.singleOrNull() ?: return null) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveInputs.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveInputs.kt new file mode 100644 index 00000000000..b57584f33c6 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveInputs.kt @@ -0,0 +1,40 @@ +package org.xvm.lsp.adapter.xdk + +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.util.concurrent.CancellationException + +/** Membership and bytes of moved inputs outside a module's existing resource roots. */ +internal class XdkMoveInputs private constructor( + private val roots: Set, + val entries: Map, +) { + fun isCurrent(cancelled: () -> Boolean): Boolean = capture(roots, cancelled)?.entries == entries + + companion object { + fun capture( + roots: Set, + cancelled: () -> Boolean, + ): XdkMoveInputs? { + val minimal = roots.filterTo(linkedSetOf()) { source -> roots.none { it != source && source.toPath().startsWith(it.toPath()) } } + return try { + val entries = + buildMap { + minimal.forEach { root -> + Files.walk(root.toPath()).use { paths -> + paths.forEach { path -> + if (cancelled()) throw CancellationException() + if (Files.isSymbolicLink(path)) throw IOException("Moved input contains a symbolic link") + put(path.toString(), XdkResources.entry(path, cancelled)) + } + } + } + } + XdkMoveInputs(minimal, entries) + } catch (_: IOException) { + null + } + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveOperations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveOperations.kt new file mode 100644 index 00000000000..59bcc0be7b3 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveOperations.kt @@ -0,0 +1,30 @@ +package org.xvm.lsp.adapter.xdk + +import java.io.File + +/** Simultaneous final destinations, expressed as child-first filesystem operations. */ +internal object XdkMoveOperations { + fun ordered(moves: Map): Map? { + // A child following its parent's exact mapping requires no second filesystem operation. + val minimal = + moves.filter { (source, destination) -> + moves.none { (parent, target) -> + parent != source && source.toPath().startsWith(parent.toPath()) && + target.toPath().resolve(parent.toPath().relativize(source.toPath())) == destination.toPath() + } + } + if (minimal.values.distinct().size != minimal.size || + minimal.any { (source, destination) -> + // Incoming paths under a moving source require a temporary staging transaction, + // which neither standard client resource edits nor our VFS command can promise. + minimal.keys.any { destination.toPath().startsWith(it.toPath()) } || + minimal.any { (other, target) -> other != source && destination.toPath().startsWith(target.toPath()) } + } + ) { + return null + } + return minimal.entries + .sortedWith(compareByDescending> { it.key.toPath().nameCount }.thenBy { it.key.path }) + .associate { it.toPair() } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt new file mode 100644 index 00000000000..8b925d190b6 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt @@ -0,0 +1,53 @@ +package org.xvm.lsp.adapter.xdk + +import java.util.concurrent.atomic.AtomicReference + +/** + * One detached build per configured root. Exact captured inputs permit reuse across graph revisions; + * no compiler, AST, constant pool or refactoring authorization survives here. + */ +internal class XdkNavigationIndex { + data class Key( + val name: String, + val sources: XdkSources.Inputs, + val dependencies: Map, + ) + + data class Facts( + val models: List, + val constants: Map, + ) + + data class Build( + val key: Key, + val facts: Facts?, + val artifact: XdkDependency?, + ) + + // Identity, rather than structural equality, fences publication after retirement or close. + class Snapshot( + val builds: Map = emptyMap(), + val navigation: Map = emptyMap(), + ) + + private val current = AtomicReference(Snapshot()) + + fun snapshot(): Snapshot = current.get() + + fun publish( + previous: Snapshot, + builds: Map, + navigation: Map = emptyMap(), + ) = current.compareAndSet(previous, Snapshot(builds.toMap(), navigation.toMap())) + + /** Called under the adapter lifecycle lock, after checking the editor request is current. */ + fun record( + uri: String, + build: Build, + ): Snapshot = current.updateAndGet { Snapshot(it.builds + (uri to build)) } + + /** Retain reusable roots while fencing work captured before the configuration/edit change. */ + fun retire(roots: Set): Snapshot = current.updateAndGet { Snapshot(it.builds.filterKeys(roots::contains)) } + + fun clear() = current.set(Snapshot()) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt new file mode 100644 index 00000000000..2dc789d1dc4 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt @@ -0,0 +1,252 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.InlayHint +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.SemanticTokens +import org.xvm.lsp.adapter.xdk.SemanticModel.Role +import org.xvm.lsp.adapter.xdk.SemanticModel.SymbolKind +import org.xvm.lsp.adapter.xdk.SemanticModel.TypeCategory +import org.xvm.lsp.adapter.xdk.SemanticModel.Usage +import org.xvm.lsp.treesitter.SemanticTokenLegend + +/** Presentation of copied compiler facts; the shared protocol legend does not load a parser. */ +internal object XdkPresentation { + /** Hover describes the resolved occurrence, never the declaration enclosing its body. */ + fun hover( + model: SemanticModel, + line: Int, + column: Int, + ): String? { + val symbol = model.symbolAt(line, column) + val type = model.typeAt(line, column)?.displayName + val position = SemanticModel.Position(line, column) + val signature = + model.calls.firstOrNull { it.method == symbol?.id && position in it.callee }?.signature + ?: symbol?.signature + return hover(model, symbol, type, signature) + } + + private fun hover( + model: SemanticModel, + symbol: SemanticModel.Symbol?, + type: String?, + signature: SemanticModel.Signature?, + ): String? { + val label = + when { + symbol != null && signature != null -> { + XdkCursorQueries.signature(model, symbol.name, signature).label + } + + symbol != null -> { + listOfNotNull(type ?: symbol.kind.name.lowercase(), symbol.name) + .joinToString(" ") + } + + else -> { + type + } + } ?: return null + return "```xtc\n$label\n```" + symbol?.documentation?.let { "\n\n$it" }.orEmpty() + } + + fun tokens( + model: SemanticModel, + lexical: List> = emptyList(), + ): SemanticTokens { + val tokens = + model.occurrences + .mapNotNull { occurrence -> + val symbol = occurrence.symbol?.let(model::symbol) ?: return@mapNotNull null + val range = occurrence.range + if (range.start.line != range.end.line || range.start == range.end) { + return@mapNotNull null + } + val type = + when (symbol.kind) { + SymbolKind.MODULE, + SymbolKind.PACKAGE, + -> { + "namespace" + } + + SymbolKind.TYPE -> { + if (occurrence.annotation) { + "decorator" + } else { + when (symbol.typeCategory) { + TypeCategory.CLASS, + TypeCategory.SERVICE, + -> "class" + + TypeCategory.INTERFACE, + TypeCategory.MIXIN, + -> "interface" + + TypeCategory.CONST -> "struct" + + TypeCategory.ENUM -> "enum" + + TypeCategory.ENUM_VALUE -> "enumMember" + + null -> "type" + } + } + } + + SymbolKind.TYPE_PARAMETER -> { + "typeParameter" + } + + SymbolKind.METHOD -> { + if (symbol.isFunction) "function" else "method" + } + + SymbolKind.PROPERTY -> { + "property" + } + + SymbolKind.VARIABLE -> { + "variable" + } + + SymbolKind.PARAMETER -> { + "parameter" + } + } + val modifiers = + buildList { + if (occurrence.role == Role.DECLARATION) add("declaration") + if (symbol.isDefaultLibrary) add("defaultLibrary") + addAll(symbol.modifiers.map { it.name.lowercase() }) + if (occurrence.usage == Usage.WRITE || occurrence.usage == Usage.READ_WRITE) { + add("modification") + } + } + listOf( + range.start.line, + range.start.column, + range.end.column - range.start.column, + SemanticTokenLegend.typeIndex.getValue(type), + SemanticTokenLegend.modifierBitmask(*modifiers.toTypedArray()), + ) + }.let { semantic -> + val coverage = TokenCoverage(semantic) + semantic + lexical.filterNot(coverage::overlaps) + }.sortedWith(compareBy({ it[0] }, { it[1] })) + return SemanticTokens( + tokens.flatMapIndexed { index, token -> + val previous = tokens.getOrNull(index - 1) + val lineDelta = token[0] - (previous?.get(0) ?: 0) + val columnDelta = token[1] - if (lineDelta == 0) previous?.get(1) ?: 0 else 0 + listOf(lineDelta, columnDelta, token[2], token[3], token[4]) + }, + ) + } + + fun hints( + model: SemanticModel, + range: Range, + ): List { + val requested = + SemanticModel.Range( + SemanticModel.Position(range.start.line, range.start.column), + SemanticModel.Position(range.end.line, range.end.column), + ) + val types = + model.occurrences + .asSequence() + .filter { + model.status == SemanticModel.Status.COMPLETE && + it.role == Role.DECLARATION && it.range.end in requested + }.mapNotNull { occurrence -> + val symbol = + occurrence.symbol?.let(model::symbol)?.takeIf { it.inferred } + ?: return@mapNotNull null + val type = symbol.type?.let(model::type) ?: return@mapNotNull null + InlayHint( + occurrence.range.end.toPosition(), + ": ${type.displayName}", + InlayHint.InlayHintKind.TYPE, + tooltip = + hover( + model, + symbol, + occurrence.type?.let(model::type)?.displayName, + symbol.signature, + ), + ) + }.toList() + val parameters = + model.calls.flatMap { call -> + call.arguments + .asSequence() + .filter { !it.named && it.range.start in requested } + .mapNotNull { argument -> + val name = + call.signature.parameters + .getOrNull(argument.parameterIndex) + ?.name + ?: return@mapNotNull null + InlayHint( + argument.range.start.toPosition(), + "$name:", + InlayHint.InlayHintKind.PARAMETER, + paddingRight = true, + tooltip = + model.symbol(call.method)?.let { symbol -> + "```xtc\n${XdkCursorQueries.signature(model, symbol.name, call.signature).label}\n```" + + symbol.documentation?.let { "\n\n$it" }.orEmpty() + }, + ) + }.toList() + } + val returns = + model.lambdas.filter { it.arrow.start in requested }.map { lambda -> + val types = lambda.signature.returns.mapNotNull { model.type(it)?.displayName } + val label = + when (types.size) { + 0 -> "void" + 1 -> types.single() + else -> types.joinToString(", ", "(", ")") + } + InlayHint( + lambda.arrow.start.toPosition(), + ": $label", + InlayHint.InlayHintKind.TYPE, + paddingRight = true, + ) + } + return (types + parameters + returns) + .distinct() + .sortedWith(compareBy({ it.position.line }, { it.position.column })) + } + + /** Prefix maxima handle nested/duplicate ranges, including a file written on one line. */ + private class TokenCoverage( + tokens: List>, + ) { + private val ordered = tokens.sortedWith(compareBy({ it[0] }, { it[1] })) + private val ends = + ordered + .runningFold(SemanticModel.Position(0, 0)) { previous, token -> + maxOf(previous, SemanticModel.Position(token[0], token[1] + token[2])) + }.drop(1) + + fun overlaps(token: List): Boolean { + val end = SemanticModel.Position(token[0], token[1] + token[2]) + var low = 0 + var high = ordered.size + // First semantic start at or after the lexical end. Only the preceding prefix can overlap. + while (low < high) { + val middle = (low + high) ushr 1 + val candidate = ordered[middle] + if (SemanticModel.Position(candidate[0], candidate[1]) < end) low = middle + 1 else high = middle + } + return low > 0 && ends[low - 1] > SemanticModel.Position(token[0], token[1]) + } + } + + private fun SemanticModel.Position.toPosition(): Position = Position(line, column) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt new file mode 100644 index 00000000000..b4d8dd8bb7d --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt @@ -0,0 +1,1506 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.ModuleRepository +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.CodeAction +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.WorkspaceEdit +import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.Location +import org.xvm.lsp.model.SymbolInfo +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.tool.ModuleInfo +import java.io.ByteArrayOutputStream +import java.io.DataOutputStream +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.security.MessageDigest +import java.util.HexFormat +import java.util.concurrent.CancellationException + +/** + * Worker-only whole-graph queries. Capture every configured module before compiling any of them. + * Proof compilations never update the live diagnostics, source build cache or editor buffers. Only + * detached locations/edits leave this object; compiler identities die with each query. + */ +internal class XdkProjectQueries( + private val project: XdkProject, + private val overlays: Map, + private val dependencies: XdkDependencies, + private val compileTree: (ModuleInfo, ModuleRepository?, ErrorListener) -> EmbeddingSupport.Compilation, + private val cancelled: () -> Boolean, + private val discoverImports: Boolean = false, + private val diagnosticCache: XdkDiagnosticIndex = XdkDiagnosticIndex(), + private val navigationIndex: XdkNavigationIndex = XdkNavigationIndex(), +) { + private val captured = + project.buildOrder().associateWith { module -> + try { + Result.success( + XdkSources.capture(module.root, overlays, module.resourceFiles, cancelled), + ) + } catch (failure: IOException) { + Result.failure(failure) + } + } + private val sources = + captured.mapNotNull { (module, result) -> result.getOrNull()?.let { module to it } }.toMap() + private val texts = + sources.values.flatMap { it.inputs.text.entries }.associate { it.key.path to it.value } + private val uris = sources.values.flatMap { it.sourceUris.entries }.associate { it.toPair() } + + /** Read-only graph compilation, with no live AST installation or artificial open buffers. */ + fun diagnostics(): List { + val revision = revision() + val previous = diagnosticCache.snapshot() + val previousNavigation = navigationIndex.snapshot() + val navigationBuilds = previousNavigation.builds.filterKeys { uri -> project.modules.values.any { it.uri == uri } }.toMutableMap() + if (previous.revision == revision && isCurrent()) return previous.results + val builds = linkedMapOf() + val artifacts = dependencies.modules.filterKeys { it !in project.modules }.toMutableMap() + val results = + project.buildOrder().map { module -> + checkCurrent() + val source = sources[module] + val uri = source?.uri(module.root) ?: module.uri + val missing = + module.dependencies.filter { + it !in artifacts && it !in XdkLibraries.moduleNames + } + if (source == null || missing.isNotEmpty()) { + CompilationResult.withDiagnostics( + uri, + listOf( + Diagnostic( + location = Location(uri, 0, 0, 0, 0), + severity = Diagnostic.Severity.ERROR, + message = + if (source == null) { + "Cannot read source for ${module.name}" + } else { + "Dependencies unavailable: ${missing.joinToString()}" + }, + code = + if (source == null) { + "SOURCE-UNAVAILABLE" + } else { + "DEPENDENCY-FAILED" + }, + source = "xtc", + ), + ), + emptyList(), + source?.documentUris ?: setOf(module.uri), + ) + } else { + val sourceDependencies = + project.buildOrder(module.uri).mapTo(hashSetOf()) { it.name } + val inputs = + artifacts.filterKeys { + it !in project.modules || it in sourceDependencies + } + val key = + XdkDiagnosticIndex.Key( + module.name, + source.inputs, + inputs.mapValues { it.value.revision }, + ) + val build = + previous.builds[module.uri]?.takeIf { it.key == key } + ?: run { + val open = XdkDependencies(inputs.values.toList()).open() + val heard = ErrorList() + val errors = ErrorListener.cancellable(heard, cancelled) + val compilation = compileTree(source.freshCompilationInput(), open.repository, errors) + checkCurrent() + val semantics = + if (compilation.succeeded() && !heard.hasSeriousErrors()) { + compilation.compiledSemantics(open, errors, navigation = true) + } else { + null + } + val navigation = semantics?.navigation + checkCurrent() + val declarations = + XdkAst.declarationLocations( + compilation.sourceTrees(), + source.sourceUris, + ) + val fallback = Source("", source.uri(module.root)) + val diagnostics = + heard.errors.map { + it.toDiagnostic(fallback, source.sourceUris, declarations) + } + val artifact = semantics?.artifact + val items = + if (artifact != null && artifact.module != module.name) { + listOf( + Diagnostic( + location = Location(uri, 0, 0, 0, 0), + severity = Diagnostic.Severity.ERROR, + message = + "Expected module ${module.name}, found ${artifact.module}", + code = "PROJECT-MODULE", + source = "xtc", + ), + ) + } else { + diagnostics + } + val validArtifact = artifact?.takeIf { it.module == module.name } + navigationBuilds[module.uri] = + XdkNavigationIndex.Build( + XdkNavigationIndex.Key(module.name, source.inputs, inputs.mapValues { it.value.revision }), + navigation?.takeIf { validArtifact != null }, + validArtifact, + ) + XdkDiagnosticIndex.Build( + key, + CompilationResult.withDiagnostics( + uri, + items, + emptyList(), + source.documentUris, + ), + artifact?.takeIf { it.module == module.name }, + ) + } + builds[module.uri] = build + build.artifact?.let { artifacts[module.name] = it } + build.result + } + } + if (!isCurrent()) throw CancellationException() + diagnosticCache.replace(XdkDiagnosticIndex.Snapshot(revision, results, builds.toMap())) + navigationIndex.publish(previousNavigation, navigationBuilds) + return results + } + + fun navigation(): XdkWorkspaceNavigation? { + val revision = revision() + val previous = navigationIndex.snapshot() + previous.navigation[revision]?.let { + return if (isCurrent()) it else null + } + val builds = navigationBuilds(previous) ?: return null + val models = builds.values.flatMap { it.facts?.models.orEmpty() } + val constants = + builds.values + .flatMap { + it.facts + ?.constants + .orEmpty() + .entries + }.associate { it.toPair() } + val declared = models.mapNotNull { model -> model.sourceName?.let { it to model.id } }.toMap() + val aliases = + models + .flatMap { it.symbols } + .distinctBy { it.id } + .groupBy { symbol -> constants[symbol.id] ?: symbol.location() ?: symbol.id } + .values + .flatMap { group -> + val canonical = + group.firstOrNull { declared[it.declarationSource] == it.id.snapshot } + ?: group.first() + group.map { it.id to canonical.id } + }.toMap() + // Implicit package directories contribute synthetic, unnamed source views. Merge their + // shared semantic facts, but only real source files can be workspace navigation targets. + val views = + SemanticModel + .joined(models, aliases) + .mapNotNull { model -> + model.sourceName?.let { uris.getValue(it) to model } + }.toMap() + if (!isCurrent()) return null + val complete = + project.modules.values.all { module -> + models.any { it.sourceName == module.root.path } + } + val dependencySources = + dependencies.modules.values + .flatMap { it.declarations.values } + .mapNotNull { it.sourceName } + .distinct() + .mapNotNull { name -> XdkSources.sourceUri(name)?.let { name to it } } + .toMap() + return XdkWorkspaceNavigation(views, revision, complete, dependencySources).also { + navigationIndex.publish(previous, builds, mapOf(revision to it)) + } + } + + /** Navigation may reuse exact detached builds; edit/refactoring proofs below always compile. */ + private fun navigationBuilds(previous: XdkNavigationIndex.Snapshot): Map? { + val artifacts = dependencies.modules.filterKeys { it !in project.modules }.toMutableMap() + val builds = + buildMap { + project.buildOrder().forEach { module -> + checkCurrent() + val source = sources[module] ?: return@forEach + if (module.dependencies.any { it !in artifacts && it !in XdkLibraries.moduleNames }) return@forEach + val closure = project.buildOrder(module.uri).mapTo(hashSetOf()) { it.name } + val inputs = artifacts.filterKeys { it !in project.modules || it in closure } + val key = XdkNavigationIndex.Key(module.name, source.inputs, inputs.mapValues { it.value.revision }) + val build = + previous.builds[module.uri]?.takeIf { it.key == key } ?: run { + val open = XdkDependencies(inputs.values.toList()).open() + val heard = ErrorList() + val errors = ErrorListener.cancellable(heard, cancelled) + val compilation = compileTree(source.freshCompilationInput(), open.repository, errors) + checkCurrent() + val semantics = + if (compilation.succeeded() && !heard.hasSeriousErrors() && + compilation.file()?.module?.name == module.name + ) { + compilation.compiledSemantics(open, errors, navigation = true) + } else { + null + } + checkCurrent() + if (semantics != null && !heard.hasSeriousErrors()) { + XdkNavigationIndex.Build(key, semantics.navigation, semantics.artifact) + } else { + XdkNavigationIndex.Build(key, null, null) + } + } + put(module.uri, build) + build.artifact?.let { artifacts[module.name] = it } + } + } + if (!isCurrent()) return null + return builds + } + + private fun revision(): String { + val bytes = + ByteArrayOutputStream() + .also { bytes -> + DataOutputStream(bytes).use { output -> + fun value(text: String) { + val utf8 = text.toByteArray(Charsets.UTF_8) + output.writeInt(utf8.size) + output.write(utf8) + } + project.buildOrder().forEach { module -> + value(module.name) + value(module.uri) + value(module.dependencies.sorted().joinToString("\u0000")) + value( + sources[module] + ?.inputs + ?.resources + ?.revision + .orEmpty(), + ) + } + uris.toSortedMap().forEach { (source, uri) -> + value(source) + value(uri) + } + texts.toSortedMap().forEach { (path, text) -> + value(path) + value(text) + } + dependencies.modules.toSortedMap().forEach { (name, artifact) -> + value(name) + value(artifact.revision) + } + } + }.toByteArray() + return "graph:" + + HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)) + } + + fun symbols(query: String): List = navigation()?.symbols(query).orEmpty() + + fun references( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): List = navigation()?.references(uri, line, column, includeDeclaration).orEmpty() + + fun referenceLenses(uri: String) = navigation()?.referenceLenses(uri).orEmpty() + + /** Rename ordinary source methods and their complete compiler override family in this graph. */ + fun rename( + uri: String, + line: Int, + column: Int, + name: String, + ): WorkspaceEdit? = renameProposal(uri, line, column, name)?.takeIf { it.sourceModules == null }?.edit + + fun renameProposal( + uri: String, + line: Int, + column: Int, + name: String, + ): XdkRenameProposal? = compile(texts)?.let { renameProposal(it, uri, line, column, name) } + + private fun renameProposal( + before: CompilerRenameFacts, + uri: String, + line: Int, + column: Int, + name: String, + ): XdkRenameProposal? { + val source = XdkSources.file(uri)?.path ?: return null + val model = before.models.singleOrNull { it.sourceName == source } ?: return null + model.importAt(line, column)?.let { alias -> + if (!XdkRename.identifier(name)) return null + val text = texts.getValue(source) + val replacements = + (alias.uses + alias.declaration).distinct().map { range -> + XdkRename.Edit( + XdkRename.offset(text, range.start) ?: return null, + XdkRename.offset(text, range.end) ?: return null, + name, + ) + } + val plan = XdkRename.Plan(texts, mapOf(source to replacements)) + val after = compile(plan.proposed) ?: return null + if (!preservesBindings(before, after, plan) || !isCurrent()) return null + return XdkRenameProposal( + WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + scope = renameScope(), + ) + } + val selected = model.symbolAt(line, column) ?: return null + val occurrence = model.occurrenceAt(line, column) ?: return null + val target = + before.callables[SemanticModel.SourceLocation(source, occurrence.range)] ?: before.constants[selected.id] ?: return null + val symbol = + before.models + .asSequence() + .flatMap { it.symbols.asSequence() } + .firstOrNull { + before.constants[it.id] in members(target) && + it.declaration != null && + it.declarationSource in texts + } ?: selected.takeIf { target is ProofIdentity.Directory } ?: return null + val declarationSource = + (target as? ProofIdentity.Directory)?.path ?: symbol.declarationSource ?: return null + if ( + target !is ProofIdentity.Directory && + (declarationSource !in texts || symbol.declaration == null) + ) { + return null + } + val targets = + when { + target is ProofIdentity.Parameter -> { + parameterFamily(before, target) ?: return null + } + + symbol.kind in + setOf( + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.PACKAGE, + SemanticModel.SymbolKind.MODULE, + ) -> { + setOf(target) + } + + symbol.kind in + setOf(SemanticModel.SymbolKind.METHOD, SemanticModel.SymbolKind.PROPERTY) && + SemanticModel.Modifier.STATIC in symbol.modifiers && + symbol.name != "construct" -> { + setOf(target) + } + + symbol.kind == SemanticModel.SymbolKind.PROPERTY -> { + propertyFamily(before, target) ?: return null + } + + symbol.kind == SemanticModel.SymbolKind.METHOD -> { + methodFamily(before, target) ?: return null + } + + else -> { + return null + } + } + val ids = before.constants.filterValues { members(it).all(targets::contains) }.keys + val base = XdkRename.plan(before, texts, source, line, column, name, ids) ?: return null + val file = File(declarationSource) + val renamedModule = + project.modules.values.singleOrNull { + it.root == file && symbol.kind == SemanticModel.SymbolKind.MODULE + } + val moduleName = + renamedModule?.let { + if (it.name.substringBefore('.') != symbol.name) return null + name + it.name.removePrefix(symbol.name) + } + if ( + renamedModule != null && + ( + (moduleName != renamedModule.name && moduleName in project.modules) || + moduleName in XdkLibraries.moduleNames + ) + ) { + return null + } + val moves = + if (target is ProofIdentity.Directory && name != symbol.name) { + XdkSourceMoves.directory( + file, + name, + texts, + sources.values.flatMap { it.inputs.directories }.toSet(), + ) ?: return null + } else if ( + symbol.kind in + setOf( + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.PACKAGE, + SemanticModel.SymbolKind.MODULE, + ) && name != symbol.name && file.nameWithoutExtension == symbol.name + ) { + XdkSourceMoves.plan( + file, + name, + texts, + sources.values.flatMap { it.inputs.directories }.toSet(), + ) ?: return null + } else { + XdkSourceMoves() + } + val graph = + if (renamedModule == null) { + project + } else { + XdkProject( + project.modules.values.map { module -> + XdkSourceModule( + if (module === renamedModule) { + requireNotNull(moduleName) + } else { + module.name + }, + File(moves.paths[module.root.path] ?: module.root.path) + .toURI() + .toString(), + module.dependencies.mapTo(linkedSetOf()) { + if (it == renamedModule.name) requireNotNull(moduleName) else it + }, + module.resourceRoots, + ) + }, + ) + } + val plan = XdkRename.Plan(texts, base.edits, moves.paths) + val after = compile(plan.proposed, moves = moves.paths, graph = graph) ?: return null + if (!preservesBindings(before, after, plan) || !isCurrent()) return null + return XdkRenameProposal( + WorkspaceEdit( + plan.edits.keys.associate { uris.getValue(it) to plan.textEdits(it) }, + versioned = true, + renames = + moves.resources.entries.associate { (from, to) -> + // Directory URIs must have the same shape even before the destination + // exists. + (uris[from] ?: File(from).toURI().toString().removeSuffix("/")) to + File(to).toURI().toString().removeSuffix("/") + }, + ), + graph.modules.values.toList().takeIf { + renamedModule != null && !discoverImports && !project.sameConfiguration(graph) + }, + project.modules.values.toList().takeIf { + renamedModule != null && !discoverImports && !project.sameConfiguration(graph) + }, + renameScope(), + ) + } + + /** Complete transaction for hosts that persist the graph with the requested resource moves. */ + fun renameFilesProposal(requested: Map): XdkRenameProposal? { + val inputOperations = + requested.entries + .associate { (from, to) -> + (XdkSources.file(from) ?: return null) to (XdkSources.file(to) ?: return null) + }.filter { (from, to) -> from != to } + val operations = XdkMoveOperations.ordered(inputOperations) ?: return null + if (operations.isEmpty()) return XdkRenameProposal(WorkspaceEdit(emptyMap(), versioned = true)) + if ( + operations.values.distinct().size != operations.size || + operations.any { (from, to) -> + !from.exists() || + !to.parentFile.isDirectory || + to.exists() || + Files.isSymbolicLink(from.toPath()) || + ( + from.isDirectory && + from.walkTopDown().any { Files.isSymbolicLink(it.toPath()) } + ) || + to.toPath().startsWith(from.toPath()) + } + ) { + return null + } + val before = compile(texts) ?: return null + val directories = sources.values.flatMap { it.inputs.directories }.toSet() + val rootOperations = + operations.filter { (from, to) -> + from.parentFile != to.parentFile && project.modules.values.any { it.root == from } + } + val rootRenames = + rootOperations.map { (from, to) -> + if (to.extension != "x") return null + XdkRename.fileNamePlan(before, texts, from.path, from.nameWithoutExtension, to.nameWithoutExtension) ?: return null + } + val rootMoves = + rootOperations.map { (from, to) -> + // Discovery cannot persist resource roots left outside the moved companion tree. + if (discoverImports) return null + XdkSourceMoves.plan(from, to, texts, directories) ?: return null + } + val typeOperations = + operations + .filter { (from, to) -> + from.isFile && from.parentFile != to.parentFile && from.path in texts && + project.modules.values.none { it.root == from } + } + val typeMove = + XdkTypeMoves.plan(before, typeOperations, texts, directories, project) ?: return null + val proposals = + operations + .map { (from, to) -> + if (from.parentFile != to.parentFile) return@map null + val selected = + before.models + .asSequence() + .flatMap { model -> + model.symbols.asSequence().mapNotNull { symbol -> + val directory = + (before.constants[symbol.id] as? ProofIdentity.Directory) + ?.path == from.path + val declaration = + symbol.declarationSource == from.path && + symbol.name == from.nameWithoutExtension && + symbol.kind in + setOf( + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.PACKAGE, + SemanticModel.SymbolKind.MODULE, + ) + when { + declaration && symbol.declaration != null -> { + Triple( + uris[from.path] ?: from.toURI().toString(), + symbol.declaration.start, + symbol.name, + ) + } + + directory -> { + model.occurrences + .firstOrNull { it.symbol == symbol.id } + ?.let { + Triple( + uris[model.sourceName] + ?: return@mapNotNull null, + it.range.start, + symbol.name, + ) + } + } + + else -> { + null + } + } + } + }.firstOrNull() ?: return@map null + val name = if (from.isDirectory) to.name else to.nameWithoutExtension + if (from.isFile && to.extension != "x") return null + val proposal = + renameProposal( + before, + selected.first, + selected.second.line, + selected.second.column, + name, + ) ?: return null + if ( + proposal.edit.renames.none { (old, new) -> + XdkSources.file(old) == from && XdkSources.file(new) == to + } + ) { + return null + } + proposal + }.filterNotNull() + val allMoves = + operations.entries.map { it.key to it.value } + + proposals + .flatMap { it.edit.renames.entries } + .map { (from, to) -> + requireNotNull(XdkSources.file(from)) to requireNotNull(XdkSources.file(to)) + } + + (listOf(typeMove.resources) + rootMoves.map { it.resources }) + .flatMap { it.entries } + .map { File(it.key) to File(it.value) } + if (allMoves.groupBy({ it.first }, { it.second }).values.any { it.distinct().size > 1 }) { + return null + } + val resources = XdkMoveOperations.ordered(allMoves.toMap()) ?: return null + val movedInputs = XdkMoveInputs.capture(resources.keys, cancelled) ?: return null + + // A destination module may acquire its first resource expression with an incoming type. + // Capture its configured lookup roots too, even when its original sources used none. + fun captureResources(): Map? = + try { + project.modules.values.associateWith { module -> + XdkResources.capture(module.root, module.resourceFiles, texts.values, cancelled) + } + } catch (_: IOException) { + null + } + val resourceSnapshots = captureResources() ?: return null + val paths = + (texts.keys.map(::File) + directories) + .mapNotNull { file -> + val move = + resources.entries.firstOrNull { (from, _) -> + file == from || file.toPath().startsWith(from.toPath()) + } ?: return@mapNotNull null + file.path to + move.value + .toPath() + .resolve(move.key.toPath().relativize(file.toPath())) + .toString() + }.toMap() + if (paths.isEmpty() || paths.values.distinct().size != paths.size) return null + val names = + project.buildOrder().associate { module -> + val destination = operations[module.root] + module.name to + if ( + destination != null && + destination.nameWithoutExtension != module.root.nameWithoutExtension + ) { + destination.nameWithoutExtension + + module.name.removePrefix(module.root.nameWithoutExtension) + } else { + module.name + } + } + if (names.values.distinct().size != names.size || + names.any { (before, after) -> before != after && after in XdkLibraries.moduleNames } + ) { + return null + } + val graph = + try { + XdkProject( + project.buildOrder().map { module -> + XdkSourceModule( + names.getValue(module.name), + File(paths[module.root.path] ?: module.root.path).toURI().toString(), + (module.dependencies + typeMove.dependencies[module.name].orEmpty()).mapTo(linkedSetOf()) { names[it] ?: it }, + ( + module.resourceFiles ?: sources[module]?.inputs?.resources?.roots?.takeIf { + File(paths[module.root.path] ?: module.root.path).parentFile != module.root.parentFile + } + )?.map { resource -> + val move = + resources.entries.firstOrNull { + resource.toPath().startsWith(it.key.toPath()) + } + ( + move + ?.value + ?.toPath() + ?.resolve(move.key.toPath().relativize(resource.toPath())) + ?.toFile() ?: resource + ).toURI() + .toString() + }, + ) + }, + ) + } catch (_: IllegalArgumentException) { + return null + } + val renamed = + proposals + .flatMap { it.edit.changes.entries } + .groupBy({ requireNotNull(XdkSources.file(it.key)).path }, { it.value }) + .mapValues { (path, changes) -> + val text = texts[path] ?: return null + changes + .flatten() + .distinct() + .map { edit -> + XdkRename.Edit( + XdkRename.offset( + text, + SemanticModel.Position( + edit.range.start.line, + edit.range.start.column, + ), + ) ?: return null, + XdkRename.offset( + text, + SemanticModel.Position( + edit.range.end.line, + edit.range.end.column, + ), + ) ?: return null, + edit.newText, + ) + }.sortedBy { it.start } + .also { ordered -> + if ( + ordered.zipWithNext().any { (first, next) -> + first.end > next.start + } + ) { + return null + } + } + } + val qualifications = typeMove.qualifications + val edits = + (renamed.entries + rootRenames.flatMap { it.edits.entries } + typeMove.edits.entries) + .groupBy({ it.key }, { it.value }) + .mapValues { (_, edits) -> edits.flatten().distinct().sortedWith(compareBy({ it.start }, { it.end })) } + if (edits.values.any { !XdkRename.disjoint(it) }) { + return null + } + val plan = XdkRename.Plan(texts, edits, paths, qualifications = qualifications, imports = typeMove.imports) + val resourceMoves = XdkResourceMoves(resources.mapKeys { it.key.path }.mapValues { it.value.path }, edits.keys, movedInputs.entries) + val after = + compile(plan.proposed, moves = paths, graph = graph, resourceMoves = resourceMoves, resourceSnapshots = resourceSnapshots) + ?: return null + if (!typeMove.provesDestinations(after) || !preservesBindings(before, after, plan, graph) || + !isCurrent() || !movedInputs.isCurrent(cancelled) || + resourceSnapshots != captureResources() + ) { + return null + } + val replacement = !discoverImports && !project.sameConfiguration(graph) + return XdkRenameProposal( + WorkspaceEdit( + edits.keys.associate { uris.getValue(it) to plan.textEdits(it) }, + versioned = true, + renames = + resources.entries.associate { (from, to) -> + from.toURI().toString().removeSuffix("/") to to.toURI().toString().removeSuffix("/") + }, + ), + graph.modules.values + .toList() + .takeIf { replacement }, + project.modules.values + .toList() + .takeIf { replacement }, + renameScope(), + ) + } + + private fun renameScope(): XdkRenameScope = + XdkRenameScope( + if (discoverImports) { + XdkRenameScope.Boundary.DISCOVERED_GRAPH + } else { + XdkRenameScope.Boundary.CONFIGURED_GRAPH + }, + project.buildOrder(), + uris.values.sorted(), + revision(), + ) + + private fun preservesBindings( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: XdkRename.Plan, + graph: XdkProject = project, + ): Boolean { + // Compilation still checks the complete graph. Binding/dispatch equivalence is needed + // for every edited module and its transitive consumers; independent roots have identical + // inputs and no path to an edited declaration. Unsupported dynamic bindings in those + // unrelated roots must not veto an otherwise proven edit. + val affected = + (plan.edits.keys + plan.moves.keys) + .mapNotNull { project.scope(it) } + .flatMapTo(linkedSetOf(), project::affected) + + plan.moves.values + .mapNotNull { graph.scope(it) } + .flatMap(graph::affected) + val movedScopes = + affected.mapTo(linkedSetOf()) { scope -> + val path = requireNotNull(XdkSources.file(scope)).path + File(plan.moves[path] ?: path).toURI().toString() + } + // Independent source graphs may share resources, so compare resource values across every + // module even when their declaration/dispatch proof can stay scoped to source consumers. + return (plan.moves.isEmpty() || XdkRename.preservesResources(before, after, plan)) && + XdkRename.preservesBindings(before.within(affected), after.within(movedScopes), plan) + } + + /** + * Candidate syntax is insufficient: every edit must compile and preserve all existing bindings. + */ + fun codeActions( + uri: String, + range: Range, + ): List { + val source = XdkSources.file(uri)?.path ?: return emptyList() + val text = texts[source] ?: return emptyList() + val before = compile(texts, Proof.REPAIR) ?: return emptyList() + val complete = + before.models.all { it.status == SemanticModel.Status.COMPLETE } && + project.modules.values.all { module -> + before.models.any { it.sourceName == module.root.path } + } + val actions = + if (!complete) { + autoImports(uri, source, text, range, before) + } else { + XdkImports.candidates(text).mapNotNull { candidate -> + checkCurrent() + val plan = XdkRename.Plan(texts, mapOf(source to candidate.edits)) + val after = compile(plan.proposed) ?: return@mapNotNull null + if (!XdkRename.preservesBindings(before, after, plan)) return@mapNotNull null + CodeAction( + candidate.title, + candidate.kind, + edit = + WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + val missing = + if (complete) { + emptyList() + } else { + before.missingMethods + .filter { it.selected(source, range) } + .distinctBy { Triple(it.destination, it.declaration, it.insertion) } + .take(8) + .mapNotNull { candidate -> + checkCurrent() + val destination = candidate.destination.sourceName ?: return@mapNotNull null + val targetText = texts[destination] ?: return@mapNotNull null + if (!Files.isWritable(File(destination).toPath())) return@mapNotNull null + val importSource = candidate.importSource ?: destination + val importText = texts[importSource] ?: return@mapNotNull null + val edit = candidate.edit(targetText, importText) ?: return@mapNotNull null + val changes = + listOf(destination to listOf(edit.member), importSource to edit.imports) + .filter { it.second.isNotEmpty() } + .groupBy({ it.first }, { it.second }) + .mapValues { (_, edits) -> edits.flatten() } + if (changes.keys.any { it !in uris || !Files.isWritable(File(it).toPath()) }) return@mapNotNull null + val plan = XdkRename.Plan(texts, changes) + val after = compile(plan.proposed) ?: return@mapNotNull null + if (!XdkRename.preservesKnownBindings(before, after, plan) || !candidate.bindsNewMethod(after, plan, edit.member)) { + return@mapNotNull null + } + CodeAction( + candidate.title, + CodeAction.CodeActionKind.QUICKFIX, + edit = WorkspaceEdit(changes.keys.associate { uris.getValue(it) to plan.textEdits(it) }, versioned = true), + ) + } + } + val declarations = + if (complete) { + emptyList() + } else { + // Header and body attempts to have separate snapshot identities for the same source. + before.models + .filter { it.sourceName == source } + .flatMap { model -> XdkMissingDeclarations.candidates(text, range, model, before) } + .distinct() + .mapNotNull { candidate -> + checkCurrent() + if (!Files.isWritable(File(source).toPath())) return@mapNotNull null + val plan = XdkRename.Plan(texts, mapOf(source to listOf(candidate.edit))) + val after = compile(plan.proposed, Proof.REPAIR) ?: return@mapNotNull null + if (!XdkRename.preservesKnownBindings(before, after, plan) || !candidate.proves(after, plan)) { + return@mapNotNull null + } + CodeAction( + candidate.title, + CodeAction.CodeActionKind.QUICKFIX, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + val members = + XdkMemberActions.actions(before.memberActions, source, range).mapNotNull { candidate -> + checkCurrent() + val edit = candidate.edit(text) ?: return@mapNotNull null + val plan = XdkRename.Plan(texts, mapOf(source to edit.all)) + val after = compile(plan.proposed) ?: return@mapNotNull null + if ( + !XdkRename.preservesMemberAdditions( + before, + after, + plan, + candidate.members, + edit.member, + ) + ) { + return@mapNotNull null + } + CodeAction( + candidate.title, + // The class may be valid until constructed, and a construction diagnostic can + // be elsewhere. This is a class intention, not a diagnostic-attached quick fix. + CodeAction.CodeActionKind.REFACTOR_REWRITE, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + val extraction = + if (complete) { + XdkLocalExtraction.candidate(text, range)?.let { candidate -> + checkCurrent() + val plan = + XdkRename.Plan( + texts, + mapOf(source to candidate.edits), + relocations = mapOf(source to listOf(candidate.relocation)), + ) + val after = compile(plan.proposed) ?: return@let null + if (!XdkRename.preservesKnownBindings(before, after, plan)) return@let null + CodeAction( + candidate.title, + CodeAction.CodeActionKind.REFACTOR_EXTRACT, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } else { + null + } + val inline = + if (complete) { + before.models.singleOrNull { it.sourceName == source }?.let { model -> + XdkLocalInline.candidate(text, range, model, before)?.let { candidate -> + checkCurrent() + val plan = + XdkRename.Plan( + texts, + mapOf(source to candidate.edits), + relocations = mapOf(source to listOf(candidate.relocation)), + ) + val after = compile(plan.proposed, Proof.REPAIR) ?: return@let null + if (candidate.expression != null && + !XdkRename.preservesRelocatedType(before, after, plan, candidate.expression) + ) { + return@let null + } + if (!XdkRename.preservesLocalRemoval(before, after, plan, candidate.removed)) return@let null + CodeAction( + candidate.title, + CodeAction.CodeActionKind.REFACTOR_INLINE, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + } else { + null + } + val memberInline = + if (complete) { + before.models.singleOrNull { it.sourceName == source }?.let { model -> + XdkMemberInline.candidate(text, range, model, before)?.let { candidate -> + checkCurrent() + val plan = XdkRename.Plan(texts, mapOf(source to listOf(candidate.edit))) + val after = compile(plan.proposed, Proof.REPAIR) ?: return@let null + if (!XdkRename.preservesMemberInline(before, after, plan, candidate)) return@let null + CodeAction( + candidate.title, + CodeAction.CodeActionKind.REFACTOR_INLINE, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + } else { + null + } + val removal = + if (complete) { + before.models.singleOrNull { it.sourceName == source }?.let { model -> + XdkLocalRemoval.candidate(text, range, model, before.removableLocals)?.let { candidate -> + checkCurrent() + val plan = XdkRename.Plan(texts, mapOf(source to listOf(candidate.edit))) + val after = compile(plan.proposed) ?: return@let null + if (!XdkRename.preservesLocalRemoval(before, after, plan, candidate.removed)) return@let null + CodeAction( + "Remove unused local variable", + CodeAction.CodeActionKind.REFACTOR_REWRITE, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + } else { + null + } + val safeDelete = + if (complete) { + before.models.singleOrNull { it.sourceName == source }?.let { model -> + XdkSafeDelete.candidate(text, range, model, before)?.let { candidate -> + checkCurrent() + val plan = XdkRename.Plan(texts, mapOf(source to listOf(candidate.edit))) + val after = compile(plan.proposed) ?: return@let null + if (!XdkRename.preservesSafeDelete(before, after, plan, candidate)) return@let null + CodeAction( + "Safely delete private member '${candidate.name}'", + CodeAction.CodeActionKind.REFACTOR_REWRITE, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + } else { + null + } + val method = + if (complete) { + before.models.singleOrNull { it.sourceName == source }?.let { model -> + XdkMethodExtraction.candidate(text, range, model, before.extraction)?.let { candidate -> + checkCurrent() + val plan = + XdkRename.Plan( + texts, + mapOf(source to candidate.edits), + relocations = mapOf(source to listOf(candidate.relocation)), + ) + val after = compile(plan.proposed, Proof.REPAIR) ?: return@let null + if (!XdkRename.preservesMethodExtraction(before, after, plan, candidate)) return@let null + CodeAction( + if (candidate.statements) "Extract statements to private method" else "Extract expression to private method", + CodeAction.CodeActionKind.REFACTOR_EXTRACT, + edit = WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + } + } else { + null + } + return if (isCurrent()) { + before.models + .filter { it.sourceName == source && Files.isWritable(File(source).toPath()) } + .flatMap { XdkDocumentation.actions(uri, text, range, it) } + .distinct() + + actions + missing + declarations + members + + listOfNotNull( + extraction, + inline, + memberInline, + removal, + safeDelete, + method, + ) + } else { + emptyList() + } + } + + private fun autoImports( + uri: String, + source: String, + text: String, + range: Range, + before: CompilerRenameFacts, + ): List { + val models = before.models.filter { it.sourceName == source } + val start = SemanticModel.Position(range.start.line, range.start.column) + val end = SemanticModel.Position(range.end.line, range.end.column) + val names = + models + .flatMap { it.occurrences } + .filter { it.symbol == null && it.range.start <= end && it.range.end >= start } + .map { it.name } + .distinct() + val owner = + project.modules.values.singleOrNull { it.uri == project.scope(uri) } + ?: return emptyList() + val actions = + names + .flatMap { XdkAutoImports.targets(it, before) } + .distinct() + .take(32) + .mapNotNull { target -> + checkCurrent() + val graph = importGraph(owner, target) ?: return@mapNotNull null + val edit = + XdkAutoImports.edit(text, owner.name, target) ?: return@mapNotNull null + val plan = XdkRename.Plan(texts, mapOf(source to listOf(edit))) + val after = compile(plan.proposed, graph = graph) ?: return@mapNotNull null + if (!XdkRename.preservesKnownBindings(before, after, plan)) { + return@mapNotNull null + } + CodeAction( + "Import '${target.path}' from ${target.module}", + CodeAction.CodeActionKind.QUICKFIX, + edit = + WorkspaceEdit(mapOf(uri to plan.textEdits(source)), versioned = true), + ) + } + return if (isCurrent()) actions else emptyList() + } + + /** A completion and its imports are one proof; never publish an import for an unfitted name. */ + fun importCompletions( + uri: String, + expectedText: String, + prefix: PartialSemanticModel.MemberPrefix, + visible: Set, + ): List { + val source = XdkSources.file(uri)?.path ?: return emptyList() + val text = texts[source]?.takeIf { it == expectedText } ?: return emptyList() + val start = XdkRename.offset(text, prefix.range.start) ?: return emptyList() + val end = XdkRename.offset(text, prefix.range.end) ?: return emptyList() + val written = text.substring(start, end) + if (!written.startsWith(prefix.text) || !XdkRename.identifier(written)) return emptyList() + val owner = project.modules.values.singleOrNull { it.uri == project.scope(uri) } ?: return emptyList() + val before = compile(texts, Proof.REPAIR) ?: return emptyList() + val items = + XdkAutoImports + .matchingTargets(prefix.text, before) + .filter { it.name !in visible } + .take(8) + .mapNotNull { target -> + checkCurrent() + val graph = importGraph(owner, target) ?: return@mapNotNull null + val imported = XdkAutoImports.edit(text, owner.name, target) ?: return@mapNotNull null + // LSP requires additional edits to be disjoint from the replacement edit. + if (imported.start in start..end) return@mapNotNull null + val replacement = XdkRename.Edit(start, end, target.name) + val plan = XdkRename.Plan(texts, mapOf(source to listOf(replacement, imported))) + val after = compile(plan.proposed, graph = graph) ?: return@mapNotNull null + if (!XdkRename.preservesKnownBindings(before, after, plan)) return@mapNotNull null + val edits = plan.textEdits(source) + CompletionItem( + target.name, + CompletionItem.CompletionKind.CLASS, + "${target.path} — import from ${target.module}", + target.name, + textEdit = edits.first(), + sortText = "7:${target.name}:${target.module}:${target.path}", + additionalTextEdits = edits.drop(1), + ) + } + return if (isCurrent()) items else emptyList() + } + + private fun importGraph( + owner: XdkSourceModule, + target: XdkAutoImports.Target, + ): XdkProject? { + val addedSource = target.module != owner.name && target.module in project.modules && target.module !in owner.dependencies + if (!addedSource) return project + if (!discoverImports) return null + return try { + XdkProject( + project.modules.values.map { + if (it.name == owner.name) XdkSourceModule(it.name, it.uri, it.dependencies + target.module, it.resourceRoots) else it + }, + ) + } catch (_: IllegalArgumentException) { + null + } + } + + private fun propertyFamily( + facts: CompilerRenameFacts, + target: ProofIdentity, + ): Set? { + val family = linkedSetOf(target) + do { + val previous = family.size + facts.properties.chains + .filter { chain -> chain.members.any(family::contains) } + .forEach { chain -> + if (!chain.supported) return null + family += chain.members + } + } while (family.size != previous) + if (!facts.properties.declarations.containsAll(family)) return null + return family + } + + private fun methodFamily( + facts: CompilerRenameFacts, + target: ProofIdentity, + ): Set? { + val family = members(target).toMutableSet() + do { + val previousSize = family.size + facts.methods.chains + .filter { chain -> chain.members.flatMap(::members).any(family::contains) } + .forEach { chain -> + if (!chain.supported) return null + family += chain.members.flatMap(::members) + } + // A written union call couples otherwise independent contracts: renaming only one + // leg would remove that call from the union's common method set. + (facts.constants.values + facts.callables.values) + .filterIsInstance() + .map(::members) + .filter { it.any(family::contains) } + .forEach { family += it } + } while (family.size != previousSize) + // A binary/library contract or synthetic method cannot be edited from configured sources. + if (!facts.methods.declarations.containsAll(family)) return null + return family + } + + private fun members(identity: ProofIdentity): Set = + when (identity) { + is ProofIdentity.Alternatives -> { + identity.targets.flatMapTo(linkedSetOf(), ::members) + } + + is ProofIdentity.Composed -> { + identity.members.flatMapTo(linkedSetOf(), ::members) + } + + is ProofIdentity.Parameter -> { + members(identity.method).mapTo(linkedSetOf()) { + ProofIdentity.Parameter(it, identity.index) + } + } + + else -> { + setOf(identity) + } + } + + /** Parameter names belong to callable slots, including differently named overrides. */ + private fun parameterFamily( + facts: CompilerRenameFacts, + target: ProofIdentity.Parameter, + ): Set? { + val declarations = + facts.models + .flatMap { it.symbols } + .filter { + it.declaration != null && it.declarationSource in texts + }.mapNotNull { symbol -> facts.constants[symbol.id]?.let { it to symbol } } + .toMap() + val method = declarations[target.method] + val methods = + if ( + method != null && + ( + method.name == "construct" || + SemanticModel.Modifier.STATIC in method.modifiers + ) + ) { + setOf(target.method) + } else { + methodFamily(facts, target.method) ?: return null + } + // InvocationExpression.testFunction rejects named arguments on function values. Escaping + // a selected method does not export parameter spellings; direct named calls and method + // value bindings are still checked by the complete before/after graph proof. + val parameters = methods.mapTo(linkedSetOf()) { ProofIdentity.Parameter(it, target.index) } + return parameters.takeIf { declarations.keys.containsAll(it) } + } + + private enum class Proof { + COMPLETE, + REPAIR, + } + + private fun compile( + text: Map, + proof: Proof = Proof.COMPLETE, + moves: Map = emptyMap(), + graph: XdkProject = project, + resourceMoves: XdkResourceMoves? = null, + resourceSnapshots: Map = emptyMap(), + ): CompilerRenameFacts? { + checkCurrent() + if (proof == Proof.COMPLETE && sources.size != graph.modules.size) return null + val artifacts = dependencies.modules.filterKeys { it !in project.modules }.toMutableMap() + val missingDestinations = mutableMapOf>() + val attempts = + graph + .buildOrder() + .mapNotNull { module -> + val source = + sources.entries + .firstOrNull { + (moves[it.key.root.path] ?: it.key.root.path) == module.root.path + }?.value ?: return@mapNotNull null + checkCurrent() + if ( + module.dependencies.any { + it !in artifacts && it !in XdkLibraries.moduleNames + } + ) { + if (proof != Proof.COMPLETE) return@mapNotNull null + return null + } + // An earlier independent root is not a dependency. Reopening all prior + // artifacts + // both admits undeclared source imports and retains quadratic compiler state. + // Host binaries remain available, including their transitive binary + // dependencies. + val sourceDependencies = + graph.buildOrder(module.uri).mapTo(hashSetOf()) { it.name } + val inputs = + artifacts.filterKeys { + it !in graph.modules || it in sourceDependencies + } + val destinations = + missingDestinations + .filterKeys(sourceDependencies::contains) + .values + .flatMap { it.entries } + .associate { it.toPair() } + val open = XdkDependencies(inputs.values.toList()).open() + val heard = ErrorList() + val errors = ErrorListener.cancellable(heard, cancelled) + val originalRoot = + sources.entries + .single { it.value === source } + .key.root + // Reassign captured source members to their proposed module, never to a + // guessed filesystem owner. Each module still uses its own resource policy. + if (text.keys.any { graph.scope(it) == null }) { + return null + } + val compilation = + try { + compileTree( + XdkSources.replay( + originalRoot, + resourceSnapshots.entries.firstOrNull { it.key.root == originalRoot }?.value?.let { + source.inputs.copy(resources = it) + } ?: source.inputs, + text, + moves, + resourceMoves, + if (moves.isEmpty()) listOf(source.inputs) else sources.values.map { it.inputs }, + ), + open.repository, + errors, + ) + } catch (_: XdkResourceMoves.UnprovenResource) { + return null + } + checkCurrent() + if ( + !compilation.succeeded() || + heard.hasSeriousErrors() || + compilation.file()?.module?.name != module.name + ) { + if (proof == Proof.REPAIR) { + val partial = compilation.renameFacts(open, includeMissingMethods = true, missingDestinations = destinations) + val fresh = XdkDependencies(inputs.values.toList()).open() + val declarationErrors = + ErrorListener.cancellable(ErrorList(), cancelled) + val declarations = + ExecutionTrace + .api( + "EmbeddingSupport.analyzeDeclarations(tree)", + module.uri, + ) { + EmbeddingSupport + .instance() + .analyzeDeclarations( + XdkSources.replay( + originalRoot, + source.inputs, + text, + moves, + ), + fresh.repository, + declarationErrors, + ) + }.orElse(null) + checkCurrent() + val headers = + declarations?.memberActionFacts( + fresh, + declarationErrors, + partial.missingMethodInputs, + destinations, + ) + val repaired = + if ( + headers != null && + !declarationErrors.hasSeriousErrors() && + !declarationErrors.isAbortDesired + ) { + CompilerRenameFacts.merge( + mapOf("headers" to headers, "partial" to partial), + ) + } else { + partial + } + return@mapNotNull module.uri to repaired + } + return null + } + val facts = + compilation.projectRenameFacts( + open, + errors, + includeMembers = proof == Proof.REPAIR, + ) + if (heard.hasSeriousErrors() || errors.isAbortDesired) { + checkCurrent() + if (proof != Proof.COMPLETE) return@mapNotNull null + return null + } + artifacts[module.name] = compilation.toDependency() + if (proof == Proof.REPAIR) { + missingDestinations[module.name] = + compilation.missingMethodDestinations(module.dependencies + XdkLibraries.moduleNames) + } + module.uri to facts + }.toMap() + checkCurrent() + return CompilerRenameFacts.merge(attempts) + } + + private fun checkCurrent() { + if (cancelled()) throw CancellationException() + } + + /** Reject changed closed sources/membership even when their watcher notification is delayed. */ + private fun isCurrent(): Boolean { + checkCurrent() + return try { + captured + .all { (module, original) -> + val current = + try { + XdkSources + .capture( + module.root, + overlays, + module.resourceFiles, + cancelled, + ).inputs + } catch (_: IOException) { + null + } + current == original.getOrNull()?.inputs + }.also { checkCurrent() } + } catch (_: IOException) { + false + } + } + + private fun SemanticModel.Symbol.location(): SemanticModel.SourceLocation? = + declaration?.let { range -> + declarationSource?.let { SemanticModel.SourceLocation(it, range) } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt new file mode 100644 index 00000000000..f374c8c87be --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt @@ -0,0 +1,87 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.lsp.util.ExecutionTrace + +/** Spelling only: callers must supply the compiler-resolved identity and desired qualification. */ +internal class XdkQualifiedName private constructor( + private val text: String, + private val tokens: List, +) { + val names = tokens.filterIndexed { index, _ -> index % 2 == 0 }.map { it.text } + + /** Replace prefix syntax, preserving every comment and whitespace character between tokens. */ + fun prefix( + count: Int, + replacement: List, + ): XdkRename.Edit { + val end = tokens[count * 2].start + val prefix = + tokens.take(count * 2).withIndex().toList().asReversed().fold(text.substring(0, end)) { value, (index, part) -> + val name = replacement.getOrNull(index / 2) + val spelling = + if (index % 2 == 0) { + name.orEmpty() + } else if (name == null) { + "" + } else { + "." + } + value.replaceRange(part.start, part.end, spelling) + } + val extra = replacement.drop(count).joinToString("") { "$it." } + return XdkRename.Edit(0, end, prefix + extra) + } + + private data class Part( + val id: Token.Id, + val start: Int, + val end: Int, + val text: String, + ) + + companion object { + private val comments = setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) + + fun parse(text: String): XdkQualifiedName? { + val tokens = lex(text) ?: return null + if (tokens.isEmpty() || tokens.size % 2 == 0 || + tokens.withIndex().any { (index, token) -> + token.id != if (index % 2 == 0) Token.Id.IDENTIFIER else Token.Id.DOT + } + ) { + return null + } + return XdkQualifiedName(text, tokens) + } + + /** A removed namespace can leave a comment before the call's first surviving token. */ + fun leadingTrivia(text: String): Int? = lex(text)?.let { it.firstOrNull()?.start ?: text.length } + + private fun lex(text: String): List? = + ExecutionTrace.api("Lexer.qualification(rename)") { + val errors = ErrorList() + val tokens = + try { + Lexer(Source(text), errors).asSequence().filter { it.id !in comments }.toList() + } catch (_: CompilerException) { + return@api null + } + if (errors.hasSeriousErrors()) return@api null + tokens.map { token -> + fun offset(position: Long) = + XdkRename.offset( + text, + SemanticModel.Position(Source.calculateLine(position), Source.calculateOffset(position)), + ) + val start = offset(token.startPosition) ?: return@api null + val end = offset(token.endPosition) ?: return@api null + Part(token.id, start, end, text.substring(start, end)) + } + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRefactoringSyntax.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRefactoringSyntax.kt new file mode 100644 index 00000000000..8b643fcc255 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRefactoringSyntax.kt @@ -0,0 +1,53 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.StatementBlock +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Source syntax shared by refactorings; eligibility and semantic proof remain with each action. */ +internal object XdkRefactoringSyntax { + /** Refactorings require a complete parse and retain the error list for subsequent lexing. */ + fun parse( + text: String, + errors: ErrorList, + operation: String, + ): StatementBlock? { + val root = + try { + ExecutionTrace.api("Parser.parseSource($operation)") { Parser(Source(text), errors).parseSource() } + } catch (_: CompilerException) { + return null + } + return root.takeUnless { errors.hasSeriousErrors() } + } + + /** Fresh parses have no adopted parent links; enumerate ownership without mutating the AST. */ + fun tree( + node: AstNode, + parent: AstNode? = null, + ): Sequence> = sequenceOf(node to parent) + node.childNodes().asSequence().flatMap { tree(it, node) } + + /** Nonempty, ordered selection in the original document's UTF-16 coordinates. */ + fun selectionOffsets( + text: String, + selection: Range, + ): Pair? { + val start = XdkRename.offset(text, SemanticModel.Position(selection.start.line, selection.start.column)) ?: return null + val end = XdkRename.offset(text, SemanticModel.Position(selection.end.line, selection.end.column)) ?: return null + return (start to end).takeIf { start < end } + } + + /** Return indentation only when no sibling, label or comment precedes this position. */ + fun indentBefore( + text: String, + offset: Int, + ): String? { + val lineStart = maxOf(text.lastIndexOf('\n', offset - 1), text.lastIndexOf('\r', offset - 1)) + 1 + return text.substring(lineStart, offset).takeIf { indent -> indent.all { it == ' ' || it == '\t' } } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt new file mode 100644 index 00000000000..2963dde0974 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt @@ -0,0 +1,1229 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.util.ExecutionTrace + +/** Worker-only edit planning and binding comparison. Compiler constants never escape this proof. */ +internal object XdkRename { + data class Edit( + val start: Int, + val end: Int, + val text: String, + ) + + /** Exact source text moved into an edit; old references must follow it during proof. */ + data class Relocation( + val start: Int, + val end: Int, + val destination: Edit, + val contentOffset: Int, + ) + + class Plan( + val original: Map, + val edits: Map>, + val moves: Map = emptyMap(), + private val relocations: Map> = emptyMap(), + val qualifications: Map> = emptyMap(), + val imports: Map> = emptyMap(), + ) { + init { + require(qualifications.all { (source, changes) -> changes.all { it in edits[source].orEmpty() } }) + require(imports.all { (source, changes) -> changes.all { it.start == it.end && it in edits[source].orEmpty() } }) + relocations.forEach { (source, moved) -> + moved.forEach { relocation -> + val insertion = relocation.destination + require(insertion in edits[source].orEmpty()) + require(relocation.start < relocation.end) + require( + original.getValue(source).substring(relocation.start, relocation.end) == + insertion.text.substring( + relocation.contentOffset, + relocation.contentOffset + relocation.end - relocation.start, + ), + ) { "A relocation must preserve the exact original text" } + } + require(moved.sortedBy { it.start }.zipWithNext().all { (first, next) -> first.end < next.start }) + } + } + + val proposed = + original.entries.associate { (source, text) -> + sourceAfter(source) to + edits[source] + .orEmpty() + .sortedWith(compareByDescending { it.start }.thenByDescending { it.end }) + .fold(text) { value, edit -> + value.replaceRange(edit.start, edit.end, edit.text) + } + } + + fun sourceAfter(source: String): String = moves[source] ?: source + + fun map( + source: String, + offset: Int, + ): Int? { + val edits = edits[source].orEmpty() + relocations[source].orEmpty().firstOrNull { offset in it.start..it.end }?.let { moved -> + val preceding = edits.filter { it != moved.destination && it.end <= moved.destination.start } + return moved.destination.start + preceding.sumOf { it.text.length - (it.end - it.start) } + + moved.contentOffset + offset - moved.start + } + if (edits.any { offset > it.start && offset < it.end }) return null + return offset + + edits.filter { it.end <= offset }.sumOf { it.text.length - (it.end - it.start) } + } + + /** Translate a written position to the proposed text of the same source file. */ + fun mapPosition( + source: String, + at: SemanticModel.Position, + ): SemanticModel.Position? { + val original = original[source] ?: return null + val changed = proposed[source] ?: return null + val offset = offset(original, at)?.let { map(source, it) } ?: return null + val mapped = position(changed, offset) + return SemanticModel.Position(mapped.line, mapped.column) + } + + fun textEdits(source: String): List = + // Keep prefix insertions separate for binding translation, but combine an insertion + // and a renamed token at the same position for clients requiring disjoint edits. + edits[source] + .orEmpty() + .groupBy { it.start } + .values + .map { sameStart -> + val ordered = sameStart.sortedBy { it.end } + require(ordered.dropLast(1).all { it.start == it.end }) + Edit(ordered.first().start, ordered.last().end, ordered.joinToString("") { it.text }) + }.map { edit -> + val text = original.getValue(source) + TextEdit(Range(position(text, edit.start), position(text, edit.end)), edit.text) + } + + fun callStart( + source: String, + offset: Int, + ): Int? { + val mapped = map(source, offset) ?: return null + val prefix = qualifications[source].orEmpty().singleOrNull { it.start == offset } ?: return mapped + val start = mapped - if (prefix.start == prefix.end) prefix.text.length else 0 + return start + (XdkQualifiedName.leadingTrivia(prefix.text) ?: return null) + } + } + + /** Rename the written type/module owning this file, before proving its final destination. */ + fun fileNamePlan( + facts: CompilerRenameFacts, + texts: Map, + source: String, + oldName: String, + newName: String, + ): Plan? { + if (!identifier(newName)) return null + if (oldName == newName) return Plan(texts, emptyMap()) + // Consumer snapshots carry their own symbol IDs for this same declaration. Select the + // declaring model here, then collect all equivalent consumer identities below. + val symbol = + facts.models.singleOrNull { it.sourceName == source }?.symbols?.singleOrNull { + it.declarationSource == source && it.name == oldName && it.declaration != null && + it.kind in setOf(SemanticModel.SymbolKind.TYPE, SemanticModel.SymbolKind.MODULE) + } ?: return null + val target = facts.constants[symbol.id] ?: return null + val at = requireNotNull(symbol.declaration).start + return plan(facts, texts, source, at.line, at.column, newName, facts.constants.filterValues { it == target }.keys) + } + + /** Same-position prefix insertion plus token replacement is ordered, not overlapping. */ + fun disjoint(edits: List): Boolean = + edits.sortedWith(compareBy({ it.start }, { it.end })).zipWithNext().all { (first, next) -> + first.end <= next.start && (first.start != next.start || (first.end == first.start && next.end > next.start)) + } + + fun plan( + facts: CompilerRenameFacts, + texts: Map, + source: String, + line: Int, + column: Int, + name: String, + targets: Set? = null, + ): Plan? { + if (!identifier(name) || facts.models.any { it.status != SemanticModel.Status.COMPLETE }) { + return null + } + val model = facts.models.singleOrNull { it.sourceName == source } ?: return null + val target = + model.symbolAt(line, column)?.takeIf { it.renameable || it.id in targets.orEmpty() } + ?: return null + if ( + target.kind == SemanticModel.SymbolKind.MODULE && + model.occurrenceAt(line, column)?.name != target.name + ) { + return null + } + val selected = targets ?: setOf(target.id) + if ( + facts.models + .flatMap { it.symbols } + .filter { it.id in selected } + .all { it.name == name } + ) { + return Plan(texts, emptyMap()) + } + val identities = + facts.constants + .filterKeys(selected::contains) + .values + .toSet() + val imports = facts.typeNames.filter { it.imported && it.target in identities }.groupBy { it.location.sourceName } + val edits = + facts.models + .filter { it.sourceName != null } + .associate { view -> + val sourceName = view.sourceName ?: return null + val text = texts[sourceName] ?: return null + val aliases = view.imports.flatMap { it.uses + it.declaration }.toSet() + val imported = imports[sourceName].orEmpty().map { it.terminal } + sourceName to + ( + view.occurrences + .filter { + it.symbol in selected && + it.range !in aliases && + it.name != "super" && + // Package aliases resolve to the imported module identity too; + // renaming that module must leave the caller's local alias + // intact. + ( + target.kind != SemanticModel.SymbolKind.MODULE || + it.name == target.name + ) + }.map { occurrence -> + val start = offset(text, occurrence.range.start) ?: return null + val end = offset(text, occurrence.range.end) ?: return null + if (text.substring(start, end) != occurrence.name) return null + Edit(start, end, name) + } + + imported.map { range -> + Edit(offset(text, range.start) ?: return null, offset(text, range.end) ?: return null, name) + } + ).distinct() + }.filterValues { it.isNotEmpty() } + return Plan(texts, edits) + } + + /** Compare every written binding, including names untouched by the edit, and selected calls. */ + fun preservesBindings( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val beforePrefixes = qualificationSites(before, plan.original, plan.qualifications) ?: return false + val afterQualifications = + (plan.qualifications.keys + plan.imports.keys) + .associateWith { source -> plan.qualifications[source].orEmpty() + plan.imports[source].orEmpty() } + .entries + .associate { (source, edits) -> + plan.sourceAfter(source) to + edits.map { edit -> + val mapped = plan.map(source, edit.start) ?: return false + val start = mapped - if (edit.start == edit.end) edit.text.length else 0 + Edit(start, start + edit.text.length, edit.text) + } + } + val afterPrefixes = qualificationSites(after, plan.proposed, afterQualifications) ?: return false + val expected = + edges(before, plan.original, plan::sourceAfter, ignored = beforePrefixes, callStart = plan::callStart) { source, offset -> + plan.map(source, offset) + } ?: return false + val actual = edges(after, plan.proposed, ignored = afterPrefixes) { _, offset -> offset } ?: return false + if (expected != actual) return false + // Relocation must retain import targets in the new lexical owner. Ordinary source + // actions may intentionally remove unused imports; their remaining bindings suffice. + if (plan.moves.isNotEmpty() && (!preservesImports(before, after, plan) || !preservesResources(before, after, plan))) return false + val expectedDispatch = + dispatch(before, plan.original, plan::sourceAfter) { source, offset -> + plan.map(source, offset) + } ?: return false + val actualDispatch = dispatch(after, plan.proposed) { _, offset -> offset } ?: return false + // New package imports inherit Object's methods. Only that new package's declaration + // acquires dispatch; existing declarations and every use still compare exactly. + val retainedDispatch = + actualDispatch + .filterNot { route -> + val owner = route.owner as? Target.Declaration ?: return@filterNot false + if (owner.kind != SemanticModel.SymbolKind.PACKAGE) return@filterNot false + plan.imports.any { (source, imports) -> + plan.sourceAfter(source) == owner.site.source && + imports.any { edit -> + val end = plan.map(source, edit.start) ?: return false + owner.site.start >= end - edit.text.length && owner.site.end <= end + } + } + }.toSet() + return expectedDispatch == retainedDispatch + } + + /** Only static namespace/type prefixes may disappear; terminal bindings and calls still match. */ + private fun qualificationSites( + facts: CompilerRenameFacts, + texts: Map, + qualifications: Map>, + ): Set? = + buildSet { + facts.models.forEach { model -> + val source = model.sourceName ?: return@forEach + if (source !in qualifications) return@forEach + val text = texts[source] ?: return null + model.occurrences.forEach { occurrence -> + val start = offset(text, occurrence.range.start) ?: return null + val end = offset(text, occurrence.range.end) ?: return null + if (qualifications[source].orEmpty().any { start >= it.start && end <= it.end }) { + val symbol = occurrence.symbol?.let(model::symbol) ?: return null + if (symbol.kind !in + setOf(SemanticModel.SymbolKind.MODULE, SemanticModel.SymbolKind.PACKAGE, SemanticModel.SymbolKind.TYPE) + ) { + return null + } + add(SemanticModel.SourceLocation(source, occurrence.range)) + } + } + } + } + + fun preservesResources( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + ): Boolean { + fun values( + facts: CompilerRenameFacts, + texts: Map, + moved: (String) -> String, + translate: (String, Int) -> Int?, + ): Map? = + facts.resourceValues.entries.associate { (location, value) -> + val site = translatedSite(location, texts, moved, translate) ?: return null + site to (value ?: return null) + } + val expected = values(before, plan.original, plan::sourceAfter, plan::map) ?: return false + return expected == values(after, plan.proposed, { it }) { _, offset -> offset } + } + + private fun preservesImports( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + ): Boolean { + fun imported( + facts: CompilerRenameFacts, + texts: Map, + moved: (String) -> String, + translate: (String, Int) -> Int?, + ): Map? = + facts.typeNames.filter { it.imported }.associate { name -> + val location = SemanticModel.SourceLocation(name.location.sourceName, name.terminal) + val site = translatedSite(location, texts, moved, translate) ?: return null + site to (composedTarget(name.target, texts, moved, translate) ?: return null) + } + val expected = imported(before, plan.original, plan::sourceAfter, plan::map) ?: return false + val actual = imported(after, plan.proposed, { it }) { _, offset -> offset } ?: return false + return expected == actual + } + + /** A repair may bind unresolved names, but cannot change any binding already established. */ + fun preservesKnownBindings( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val (expected, actual) = repairBindingComparison(before, after, plan) ?: return false + if (expected.any { (site, target) -> actual[site] != target }) return false + val knownDispatch = + dispatch(before, plan.original) { source, offset -> plan.map(source, offset) } + ?: return false + val actualDispatch = dispatch(after, plan.proposed) { _, offset -> offset } ?: return false + return actualDispatch.containsAll(knownDispatch) + } + + /** New helper parameters may replace only the selected reads; their call arguments still bind + * to the original stable values. Every moved call and every unaffected edge remains identical. */ + fun preservesMethodExtraction( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + candidate: XdkMethodExtraction.Candidate, + ): Boolean { + if (before.models.any { it.status != SemanticModel.Status.COMPLETE } || + after.models.any { it.status != SemanticModel.Status.COMPLETE } + ) { + return false + } + val source = candidate.expression.sourceName ?: return false + val original = plan.original[source] ?: return false + val proposed = plan.proposed[source] ?: return false + val insertion = candidate.insertion + val insertionStart = + insertion.start + + plan.edits[source] + .orEmpty() + .filter { it != insertion && it.end <= insertion.start } + .sumOf { it.text.length - (it.end - it.start) } + // The old expression maps to its relocated body, not to the replacement call. + val callStart = + candidate.replacement.start + + plan.edits[source] + .orEmpty() + .filter { it != candidate.replacement && it.end <= candidate.replacement.start } + .sumOf { it.text.length - (it.end - it.start) } + + fun oldSite(at: SemanticModel.SourceLocation): Site? { + if (at.sourceName != source) return null + val start = offset(original, at.range.start)?.let { plan.map(source, it) } ?: return null + val end = offset(original, at.range.end)?.let { plan.map(source, it) } ?: return null + return Site(source, start, end) + } + + fun location( + start: Int, + end: Int, + ): SemanticModel.SourceLocation { + fun at(offset: Int) = position(proposed, offset).let { SemanticModel.Position(it.line, it.column) } + return SemanticModel.SourceLocation(source, SemanticModel.Range(at(start), at(end))) + } + + fun sameType( + old: SemanticModel.SourceLocation, + new: SemanticModel.SourceLocation, + ): Boolean { + val expected = + before.extraction.types[old]?.let { type -> composedTarget(type, plan.original, { it }, plan::map) } ?: return false + val actual = + after.extraction.types[new]?.let { type -> composedTarget(type, plan.proposed, { it }) { _, at -> at } } ?: return false + return expected == actual + } + val expressionStart = insertionStart + candidate.relocation.contentOffset + val expressionEnd = expressionStart + candidate.relocation.end - candidate.relocation.start + if (!candidate.statements && !sameType(candidate.expression, location(expressionStart, expressionEnd))) return false + val expected = edges(before, plan.original, sourceParameters = true, translate = plan::map) ?: return false + val actual = edges(after, plan.proposed, sourceParameters = true) { _, at -> at } ?: return false + val captures = + candidate.captures.associate { capture -> + if (capture.declaration !in before.extraction.stableValues) return false + val parameterStart = insertionStart + capture.parameterOffset + val parameterEnd = parameterStart + capture.name.length + val parameter = Target.Declaration(Site(source, parameterStart, parameterEnd), SemanticModel.SymbolKind.PARAMETER) + val originalTarget = Target.Declaration(oldSite(capture.declaration) ?: return false, capture.kind) + if (actual[parameter.site] != parameter || + !sameType(capture.declaration, location(parameterStart, parameterEnd)) + ) { + return false + } + val argumentStart = callStart + capture.argumentOffset + if (actual[Site(source, argumentStart, argumentStart + capture.name.length)] != originalTarget) return false + parameter to originalTarget + } + if (expected.any { (site, target) -> + val value = actual[site] + val relocatedRead = + site.source == source && !site.call && + site.start >= expressionStart && site.end <= expressionEnd + value != target && (!relocatedRead || captures[value] != target) + } + ) { + return false + } + val helperStart = insertionStart + candidate.methodOffset + val helper = Target.Declaration(Site(source, helperStart, helperStart + candidate.name.length), SemanticModel.SymbolKind.METHOD) + if (actual[helper.site] != helper || + actual[Site(source, callStart, callStart + candidate.name.length, true)] != helper + ) { + return false + } + val model = after.models.singleOrNull { it.sourceName == source } ?: return false + if (candidate.statements) { + val at = position(proposed, helperStart) + val symbol = model.symbolAt(at.line, at.column) ?: return false + if (symbol.signature?.returns?.isNotEmpty() != false) return false + } + val call = model.calls.singleOrNull { it.callee == location(callStart, callStart + candidate.name.length).range } ?: return false + if (call.arguments.size != candidate.captures.size || + call.arguments.withIndex().any { (index, argument) -> + val capture = candidate.captures[index] + val start = callStart + capture.argumentOffset + argument.parameterIndex != index || argument.range != location(start, start + capture.name.length).range + } + ) { + return false + } + val oldDispatch = dispatch(before, plan.original, translate = plan::map) ?: return false + val newDispatch = dispatch(after, plan.proposed) { _, at -> at } ?: return false + // A fresh private helper can add its own chain, but cannot override or redirect old members. + return newDispatch + .filterNot { + it.members == listOf(helper) && it.supported && it.cycles.isEmpty() && it.alternatives.isEmpty() + }.toSet() == oldDispatch + } + + /** Contextual inference must not change the value's type when its expression moves. */ + fun preservesRelocatedType( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + expression: SemanticModel.SourceLocation, + ): Boolean { + val source = expression.sourceName ?: return false + val original = plan.original[source] ?: return false + val proposed = plan.proposed[source] ?: return false + + fun mapped(at: SemanticModel.Position): SemanticModel.Position? { + val offset = offset(original, at)?.let { plan.map(source, it) } ?: return null + return position(proposed, offset).let { SemanticModel.Position(it.line, it.column) } + } + val range = SemanticModel.Range(mapped(expression.range.start) ?: return false, mapped(expression.range.end) ?: return false) + val expected = before.extraction.types[expression] ?: return false + val actual = after.extraction.types[SemanticModel.SourceLocation(source, range)] ?: return false + return sameType(expected, actual, plan) + } + + /** Copy precisely the selected member body, preserving its bindings and the use's result type. */ + fun preservesMemberInline( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + candidate: XdkMemberInline.Candidate, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val source = candidate.source + val original = plan.original[source] ?: return false + val proposed = plan.proposed[source] ?: return false + val oldStart = offset(original, candidate.expression.start)?.let { plan.map(source, it) } ?: return false + val oldEnd = offset(original, candidate.expression.end)?.let { plan.map(source, it) } ?: return false + // This action has one replacement and deliberately keeps the member declaration. + if (plan.edits != mapOf(source to listOf(candidate.edit))) return false + val copyStart = candidate.edit.start + 1 + val expected = + edges(before, plan.original, ignored = candidate.ignored, ignoredCalls = candidate.ignored, translate = plan::map) + ?: return false + val copies = + expected + .filterKeys { it.source == source && it.start >= oldStart && it.end <= oldEnd } + .mapKeys { (site, _) -> site.copy(start = copyStart + site.start - oldStart, end = copyStart + site.end - oldStart) } + val actual = edges(after, plan.proposed) { _, at -> at } ?: return false + if (actual != expected + copies) return false + + fun at(offset: Int) = position(proposed, offset).let { SemanticModel.Position(it.line, it.column) } + val copied = SemanticModel.SourceLocation(source, SemanticModel.Range(at(copyStart), at(copyStart + oldEnd - oldStart))) + val oldType = before.extraction.types[SemanticModel.SourceLocation(source, candidate.use)] ?: return false + val newType = after.extraction.types[copied] ?: return false + if (!sameType(oldType, newType, plan)) return false + val oldDispatch = dispatch(before, plan.original, translate = plan::map) ?: return false + val newDispatch = dispatch(after, plan.proposed) { _, at -> at } ?: return false + return oldDispatch == newDispatch + } + + /** Removing a local may remove its declaration, written type and sole read, but no other edges. */ + fun preservesLocalRemoval( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + removed: Set, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val expected = edges(before, plan.original, ignored = removed, translate = plan::map) ?: return false + val actual = edges(after, plan.proposed) { _, offset -> offset } ?: return false + if (expected != actual) return false + val expectedDispatch = dispatch(before, plan.original, translate = plan::map) ?: return false + val actualDispatch = dispatch(after, plan.proposed) { _, offset -> offset } ?: return false + return expectedDispatch == actualDispatch + } + + /** Remove only the selected private member's own chain; every surviving route stays exact. */ + fun preservesSafeDelete( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + candidate: XdkSafeDelete.Candidate, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val expected = + edges(before, plan.original, ignored = candidate.removed, ignoredCalls = candidate.removed, translate = plan::map) + ?: return false + val actual = edges(after, plan.proposed) { _, at -> at } ?: return false + if (expected != actual) return false + val oldDispatch = dispatch(before, plan.original, removedMembers = setOf(candidate.identity), translate = plan::map) ?: return false + val newDispatch = dispatch(after, plan.proposed) { _, at -> at } ?: return false + return oldDispatch == newDispatch + } + + /** + * Allow exactly the selected implementations and their inherited effects; preserve other + * bindings. + */ + fun preservesMemberAdditions( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + candidates: List, + insertion: Edit, + ): Boolean { + if (after.models.any { it.status != SemanticModel.Status.COMPLETE }) return false + val owner = candidates.firstOrNull()?.owner ?: return false + if (candidates.any { it.owner != owner }) return false + val source = owner.sourceName ?: return false + val edits = plan.edits[source] ?: return false + if (plan.edits.size != 1 || insertion !in edits || edits.any { it.start != it.end }) { + return false + } + val insertionStart = + insertion.start + edits.filter { it.start < insertion.start }.sumOf { it.text.length } + val oldDispatch = + dispatch(before, plan.original) { path, offset -> plan.map(path, offset) } + ?.let(::memberDispatch) ?: return false + val importRanges = + edits + .filter { it != insertion } + .map { edit -> + val start = + edit.start + edits.filter { it.start < edit.start }.sumOf { it.text.length } + start until start + edit.text.length + } + // New package aliases have their own inherited dispatch chains. They have no previous + // owner to preserve; only package declarations inside the generated import insertions + // are excluded. All existing owners and every old reference still participate in proof. + val newDispatch = + dispatch(after, plan.proposed) { _, offset -> offset } + ?.let(::memberDispatch) + ?.filterNot { chain -> + val declaration = chain.owner as? Target.Declaration + declaration?.kind == SemanticModel.SymbolKind.PACKAGE && + declaration.site.source == source && + importRanges.any { + declaration.site.start in it && declaration.site.end - 1 in it + } + }?.toSet() ?: return false + + fun matches( + target: Target, + location: SemanticModel.SourceLocation, + ): Boolean { + val declaration = target as? Target.Declaration ?: return false + val path = location.sourceName ?: return false + val text = plan.original[path] ?: return false + val start = + offset(text, location.range.start)?.let { plan.map(path, it) } ?: return false + val end = offset(text, location.range.end)?.let { plan.map(path, it) } ?: return false + return declaration.site == Site(path, start, end) + } + + fun matchesContract( + target: Target, + contract: ProofIdentity, + ): Boolean = + when (contract) { + is ProofIdentity.Source -> { + if (contract.location.sourceName in plan.original) { + matches(target, contract.location) + } else { + target == Target.External(contract) + } + } + + is ProofIdentity.Binary, + is ProofIdentity.Method, + -> { + target == Target.External(contract) + } + + else -> { + false + } + } + val changed = newDispatch - oldDispatch + val additions = + candidates + .map { candidate -> + val removed = + (oldDispatch - newDispatch).singleOrNull { chain -> + matches(chain.owner, owner) && + chain.members.any { matchesContract(it, candidate.contract) } + } ?: return false + val added = + changed.singleOrNull { chain -> + chain.owner == removed.owner && + chain.members.containsAll(removed.members) && + (chain.members - removed.members.toSet()).size == 1 + } ?: return false + val member = + (added.members - removed.members.toSet()).single() as? Target.Declaration + ?: return false + if ( + member.kind != SemanticModel.SymbolKind.METHOD || + member.site.source != source || + member.site.start < insertionStart || + member.site.end > insertionStart + insertion.text.length + ) { + return false + } + member to removed + }.toMap() + if (additions.size != candidates.size) return false + // Removing exactly these new methods must reconstruct every original chain in order, + // including descendants, overloads and properties. Each method belongs to its own family. + if ( + changed.any { chain -> + !chain.supported || + chain.members.count { it in additions } != 1 || + chain.copy(members = chain.members.filterNot(additions::containsKey)) !in + oldDispatch + } + ) { + return false + } + if ( + newDispatch.mapTo(linkedSetOf()) { chain -> + if (chain in changed) { + chain.copy(members = chain.members.filterNot(additions::containsKey)) + } else { + chain + } + } != oldDispatch + ) { + return false + } + val (expected, actual) = repairBindingComparison(before, after, plan) ?: return false + val oldParameters = + memberParameterSlots(before, plan.original) { path, offset -> plan.map(path, offset) } + val newParameters = memberParameterSlots(after, plan.proposed) { _, offset -> offset } + + fun declarationSite( + target: Target, + chains: Set, + ): Site? = + when (target) { + is Target.Declaration -> { + target.site + } + + is Target.SourceProof -> { + target.site + } + + is Target.Composed -> { + (memberBridge(target, chains)?.firstOrNull() as? Target.Declaration)?.site + } + + else -> { + null + } + } + + fun selectedRebinding( + old: Target, + new: Target, + ): Boolean { + val oldSite = declarationSite(old, oldDispatch) + val newSite = declarationSite(new, newDispatch) ?: return false + return additions.any { (member, original) -> + member.site == newSite && + original.members.any { + it == old || + (oldSite != null && declarationSite(it, oldDispatch) == oldSite) + } + } + } + + return expected.all { (site, target) -> + val replacement = actual[site] ?: return@all false + if (target == replacement || selectedRebinding(target, replacement)) return@all true + // Named arguments may now identify the new override's parameter. Only the same + // compiler-proven slot on the selected method family is allowed to change owner. + val oldSlot = oldParameters[target] ?: target as? Target.Parameter ?: return@all false + val newSlot = + newParameters[replacement] ?: replacement as? Target.Parameter ?: return@all false + oldSlot.index == newSlot.index && selectedRebinding(oldSlot.method, newSlot.method) + } + } + + /** + * A generic cap is equivalent only when the compiler also supplies that exact written chain. + */ + private fun memberBridge( + target: Target.Composed, + chains: Set, + ): List? { + val owner = (target.owner as? Target.SourceProof)?.site ?: return null + if (target.delegates.isNotEmpty() || target.cycles.isNotEmpty() || target.alternatives.isNotEmpty()) return null + val written = + target.members.map { + when (it) { + is Target.SourceProof -> { + if (it.format != Constant.Format.Method) return null + Target.Declaration(it.site, SemanticModel.SymbolKind.METHOD) + } + + is Target.External -> { + if ( + it.constant !is ProofIdentity.Binary && + it.constant !is ProofIdentity.Method + ) { + return null + } + it + } + + else -> { + return null + } + } + } + return written.takeIf { + chains.any { chain -> + chain.supported && + (chain.owner as? Target.Declaration)?.site == owner && + chain.members == written + } + } + } + + private fun memberDispatch(chains: Set): Set = + chains.mapTo(linkedSetOf()) { chain -> + if (!chain.supported) return@mapTo chain + val members = + chain.members + .flatMap { target -> + if (target is Target.Composed) { + memberBridge(target, chains) ?: listOf(target) + } else { + listOf(target) + } + }.distinct() + // An inherited cap may belong to a base owner. Require the descendant's own explicit + // chain too, so a bridge never invents a new dispatch relationship for that descendant. + chain.copy(members = members).takeIf { it in chains } ?: chain + } + + private fun memberParameterSlots( + facts: CompilerRenameFacts, + texts: Map, + translate: (String, Int) -> Int?, + ): Map = + facts.models + .flatMap { it.symbols } + .mapNotNull { symbol -> + val identity = + facts.constants[symbol.id] as? ProofIdentity.Parameter ?: return@mapNotNull null + val source = symbol.declarationSource ?: return@mapNotNull null + val text = texts[source] ?: return@mapNotNull null + val range = symbol.declaration ?: return@mapNotNull null + val start = + offset(text, range.start)?.let { translate(source, it) } + ?: return@mapNotNull null + val end = + offset(text, range.end)?.let { translate(source, it) } ?: return@mapNotNull null + val slot = + composedTarget(identity, texts, { it }, translate) as? Target.Parameter + ?: return@mapNotNull null + Target.Declaration(Site(source, start, end), symbol.kind) to slot + }.toMap() + + private data class Dispatch( + val owner: Target, + val members: List, + val supported: Boolean, + val cycles: List = emptyList(), + val alternatives: List = emptyList(), + ) + + /** A compiling rename can add an override without changing any written name or call binding. */ + private fun dispatch( + facts: CompilerRenameFacts, + texts: Map, + moved: (String) -> String = { it }, + removedMembers: Set = emptySet(), + translate: (String, Int) -> Int?, + ): Set? { + val declarations = + facts.models + .flatMap { it.symbols } + .distinctBy { it.id } + .mapNotNull { symbol -> facts.constants[symbol.id]?.let { it to symbol } } + .toMap() + + fun target(constant: ProofIdentity): Target? { + if (constant is ProofIdentity.Directory) return Target.Directory(moved(constant.path)) + if (constant is ProofIdentity.Composed) { + return composedTarget(constant, texts, moved, translate) + } + val symbol = declarations[constant] ?: return Target.External(constant) + val source = symbol.declarationSource ?: return Target.External(constant) + val text = texts[source] ?: return Target.External(constant) + val range = symbol.declaration ?: return null + val start = offset(text, range.start)?.let { translate(source, it) } ?: return null + val end = offset(text, range.end)?.let { translate(source, it) } ?: return null + return Target.Declaration(Site(moved(source), start, end), symbol.kind) + } + return (facts.methods.chains + facts.properties.chains) + .filterNot { chain -> + chain.members.singleOrNull() in removedMembers && chain.supported && + chain.cycles.isEmpty() && chain.alternatives.isEmpty() + }.mapTo(linkedSetOf()) { chain -> + Dispatch( + target(chain.owner) ?: return null, + chain.members.map { target(it) ?: return null }, + chain.supported, + chain.cycles.map { composedTarget(it, texts, moved, translate) ?: return null }, + chain.alternatives.map { composedTarget(it, texts, moved, translate) ?: return null }, + ) + } + } + + /** Preserve every known input binding while requiring resolved bindings in the repaired source. */ + private fun repairBindingComparison( + before: CompilerRenameFacts, + after: CompilerRenameFacts, + plan: Plan, + ): Pair, Map>? { + val expected = edges(before, plan.original, allowUnresolved = true, sourceParameters = true, translate = plan::map) ?: return null + val actual = edges(after, plan.proposed, sourceParameters = true) { _, offset -> offset } ?: return null + return expected to actual + } + + /** Both range boundaries must survive the edit before a proof can compare their targets. */ + private fun translatedSite( + location: SemanticModel.SourceLocation, + texts: Map, + moved: (String) -> String, + translate: (String, Int) -> Int?, + ): Site? { + val source = location.sourceName ?: return null + val text = texts[source] ?: return null + val start = offset(text, location.range.start)?.let { translate(source, it) } ?: return null + val end = offset(text, location.range.end)?.let { translate(source, it) } ?: return null + return Site(moved(source), start, end) + } + + private data class Site( + val source: String, + val start: Int, + val end: Int, + val call: Boolean = false, + ) + + private sealed interface Target { + data class Declaration( + val site: Site, + val kind: SemanticModel.SymbolKind, + ) : Target + + data class External( + val constant: ProofIdentity, + ) : Target + + data class Directory( + val path: String, + ) : Target + + data class Parameter( + val method: Target, + val index: Int, + ) : Target + + data class MethodFormal( + val method: Target, + val index: Int, + ) : Target + + data class PrimaryConstructor( + val owner: Target, + ) : Target + + data class Super( + val method: Target, + ) : Target + + data class Receiver( + val owner: Target, + val register: Int, + ) : Target + + data class SourceProof( + val site: Site, + val format: Constant.Format, + ) : Target + + data class Composed( + val owner: Target, + val members: List, + val delegates: List, + val cycles: List, + val alternatives: List, + ) : Target + + data class Alternatives( + val targets: Set, + ) : Target + + data class TypeShape( + val format: Constant.Format, + val components: List, + ) : Target + } + + /** Compare detached compiler types, translating source identities through the proposed edit. */ + internal fun sameType( + before: ProofIdentity, + after: ProofIdentity, + plan: Plan, + ): Boolean { + val expected = composedTarget(before, plan.original, { it }, plan::map) ?: return false + val actual = composedTarget(after, plan.proposed, { it }) { _, at -> at } ?: return false + return expected == actual + } + + private fun composedTarget( + identity: ProofIdentity, + texts: Map, + moved: (String) -> String, + translate: (String, Int) -> Int?, + ): Target? { + return when (identity) { + is ProofIdentity.TypeShape -> { + Target.TypeShape(identity.format, identity.components.map { composedTarget(it, texts, moved, translate) ?: return null }) + } + + is ProofIdentity.Alternatives -> { + Target.Alternatives( + identity.targets.mapTo(linkedSetOf()) { + composedTarget(it, texts, moved, translate) ?: return null + }, + ) + } + + is ProofIdentity.Parameter -> { + composedTarget(identity.method, texts, moved, translate)?.let { + Target.Parameter(it, identity.index) + } + } + + is ProofIdentity.MethodFormal -> { + composedTarget(identity.method, texts, moved, translate)?.let { + Target.MethodFormal(it, identity.index) + } + } + + is ProofIdentity.PrimaryConstructor -> { + composedTarget(identity.owner, texts, moved, translate) + ?.let(Target::PrimaryConstructor) + } + + is ProofIdentity.Super -> { + composedTarget(identity.method, texts, moved, translate)?.let(Target::Super) + } + + is ProofIdentity.Receiver -> { + composedTarget(identity.owner, texts, moved, translate)?.let { Target.Receiver(it, identity.register) } + } + + is ProofIdentity.Composed -> { + val owner = composedTarget(identity.owner, texts, moved, translate) ?: return null + val members = + identity.members.map { + composedTarget(it, texts, moved, translate) ?: return null + } + val delegates = + identity.delegates.map { + composedTarget(it, texts, moved, translate) ?: return null + } + val cycles = identity.cycles.map { composedTarget(it, texts, moved, translate) ?: return null } + val alternatives = identity.alternatives.map { composedTarget(it, texts, moved, translate) ?: return null } + Target.Composed(owner, members, delegates, cycles, alternatives) + } + + is ProofIdentity.Source -> { + val source = identity.location.sourceName ?: return null + val text = texts[source] ?: return Target.External(identity) + val start = + offset(text, identity.location.range.start)?.let { translate(source, it) } + ?: return null + val end = + offset(text, identity.location.range.end)?.let { translate(source, it) } + ?: return null + Target.SourceProof(Site(moved(source), start, end), identity.format) + } + + else -> { + Target.External(identity) + } + } + } + + private fun edges( + facts: CompilerRenameFacts, + texts: Map, + moved: (String) -> String = { it }, + allowUnresolved: Boolean = false, + sourceParameters: Boolean = false, + ignored: Set = emptySet(), + ignoredCalls: Set = emptySet(), + callStart: ((String, Int) -> Int?)? = null, + translate: (String, Int) -> Int?, + ): Map? { + val declarations = + facts.models + .flatMap { it.symbols } + .filter { it.declaration != null && it.declarationSource in texts } + .mapNotNull { symbol -> facts.constants[symbol.id]?.let { it to symbol } } + .toMap() + + fun site( + source: String, + range: SemanticModel.Range, + call: Boolean = false, + ): Site? { + val text = texts[source] ?: return null + val start = + offset(text, range.start)?.let { (if (call) callStart else null)?.invoke(source, it) ?: translate(source, it) } + ?: return null + val end = offset(text, range.end)?.let { translate(source, it) } ?: return null + return Site(moved(source), start, end, call) + } + + fun target( + model: SemanticModel, + id: SemanticModel.SymbolId?, + ): Target? { + val original = id?.let(model::symbol) ?: return null + val identity = facts.constants[id] + val symbol = + if (original.declaration == null) { + declarations[facts.constants[id]] ?: original + } else { + original + } + // Before repairing an unresolved signature, the compiler can identify its written + // parameter but cannot assign a method slot yet. Compare the same source identity + // after repair; generated/composed slots still require their normal dispatch proof. + if ( + sourceParameters && + symbol.kind == SemanticModel.SymbolKind.PARAMETER && + symbol.declarationSource in texts && + symbol.declaration != null + ) { + return site(requireNotNull(symbol.declarationSource), symbol.declaration)?.let { + Target.Declaration(it, symbol.kind) + } + } + if (identity is ProofIdentity.Directory) return Target.Directory(moved(identity.path)) + if ( + identity is ProofIdentity.Composed || + identity is ProofIdentity.Alternatives || + identity is ProofIdentity.Super || + identity is ProofIdentity.Receiver || + identity is ProofIdentity.PrimaryConstructor || + identity is ProofIdentity.Parameter || + identity is ProofIdentity.MethodFormal + ) { + return composedTarget(identity, texts, moved, translate) + } + val source = symbol.declarationSource + val range = symbol.declaration + return if (source != null && range != null && source in texts) { + site(source, range)?.let { Target.Declaration(it, symbol.kind) } + } else { + facts.constants[id]?.let { Target.External(it) } + } + } + val result = linkedMapOf() + facts.models + .filter { it.sourceName != null } + .forEach { model -> + val source = model.sourceName ?: return null + model.occurrences.forEach { occurrence -> + if (SemanticModel.SourceLocation(source, occurrence.range) !in ignored && + (!allowUnresolved || occurrence.symbol != null) + ) { + val callable = facts.callables[SemanticModel.SourceLocation(source, occurrence.range)] + result[site(source, occurrence.range) ?: return null] = + if (callable != null) { + composedTarget(callable, texts, moved, translate) ?: return null + } else { + target(model, occurrence.symbol) ?: return null + } + } + } + model.calls.filter { SemanticModel.SourceLocation(source, it.callee) !in ignoredCalls }.forEach { call -> + val callable = facts.callables[SemanticModel.SourceLocation(source, call.callee)] + result[site(source, call.callee, true) ?: return null] = + if (callable != null) { + composedTarget(callable, texts, moved, translate) ?: return null + } else { + target(model, call.method) ?: return null + } + } + } + return result + } + + internal fun identifier(name: String): Boolean = + ExecutionTrace.api("Lexer.identifier(rename)") { + val errors = ErrorList() + val lexer = Lexer(Source(name), errors) + if (!lexer.hasNext()) return@api false + val token = lexer.next() + token.id == Token.Id.IDENTIFIER && + token.valueText == name && + !lexer.hasNext() && + !errors.hasSeriousErrors() + } + + private val newlines = Regex("\\r\\n|\\r|\\n") + + internal fun offset( + text: String, + position: SemanticModel.Position, + ): Int? { + val breaks = newlines.findAll(text).toList() + val start = + if (position.line == 0) { + 0 + } else { + breaks + .getOrNull(position.line - 1) + ?.range + ?.last + ?.plus(1) ?: return null + } + val end = breaks.getOrNull(position.line)?.range?.first ?: text.length + return (start + position.column).takeIf { it <= end } + } + + internal fun position( + text: String, + offset: Int, + ): Position { + val breaks = newlines.findAll(text).takeWhile { it.range.last < offset }.toList() + return Position( + breaks.size, + offset - ( + breaks + .lastOrNull() + ?.range + ?.last + ?.plus(1) ?: 0 + ), + ) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt new file mode 100644 index 00000000000..5d678700a5e --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt @@ -0,0 +1,44 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.WorkspaceEdit +import java.util.List.copyOf as immutableList + +/** + * A proven edit plus any replacement for a host-owned source graph. A host must persist the graph + * together with accepting the edit, then install it with replaceSourceModules. Creating a proposal + * never changes the adapter configuration or the filesystem. Ordinary LSP rename cannot persist + * arbitrary client settings, so it declines proposals requiring this additional host operation. + */ +class XdkRenameProposal + internal constructor( + val edit: WorkspaceEdit, + sourceModules: List? = null, + previousSourceModules: List? = null, + val scope: XdkRenameScope? = null, + ) { + val sourceModules: List? = sourceModules?.let(::immutableList) + val previousSourceModules: List? = previousSourceModules?.let(::immutableList) + } + +/** + * The source boundary actually checked by a project rename. Absolute roots may be outside IDE + * workspace folders. Hosts must register every intended consumer; this is not a promise about + * unregistered repositories, binaries or reflective string references. Null proposal scope means a + * local module proof, not an empty or globally complete graph. This receipt describes the input + * snapshot; it is not authorization to apply an edit after its document versions become stale. + */ +class XdkRenameScope + internal constructor( + val boundary: Boundary, + modules: List, + sourceUris: List, + val revision: String, + ) { + enum class Boundary { + CONFIGURED_GRAPH, + DISCOVERED_GRAPH, + } + + val modules: List = immutableList(modules) + val sourceUris: List = immutableList(sourceUris) + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt new file mode 100644 index 00000000000..3b59b38ac38 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt @@ -0,0 +1,80 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.tool.ResourceDir +import java.io.File + +/** Read proposed resource paths through original files, without moving anything during proof. */ +internal class XdkResourceMoves( + private val moves: Map, + private val changedSources: Set, + private val incoming: Map = emptyMap(), +) { + class UnprovenResource( + reason: String, + ) : RuntimeException(reason) + + fun path(file: File): File = + moves.entries.firstOrNull { file.toPath().startsWith(File(it.key).toPath()) }?.let { + File(it.value).toPath().resolve(File(it.key).toPath().relativize(file.toPath())).toFile() + } ?: file + + fun directory(resources: XdkResources): ResourceDir { + if (resources.entries.isEmpty()) return resources.directory() + // Extra move inputs are captured before replay and checked again before publication. + // Without that snapshot an incoming file could silently shadow a fallback resource. + if (moves.any { (from, to) -> + from !in resources.entries && from !in incoming && + resources.roots.any { root -> + val destination = File(to).toPath() + val proposedRoot = path(root).toPath() + !root.toPath().startsWith(from) && + (destination.startsWith(proposedRoot) || proposedRoot.startsWith(destination)) + } + } + ) { + throw UnprovenResource("The move introduces resources outside the captured roots") + } + val entries = + (resources.entries + incoming) + .filterValues { it != "missing" } + .keys + .map(::File) + .associateBy { path(it) } + return Directory(null, "", resources.roots.map(::path), entries) + } + + private inner class Directory( + parent: ResourceDir?, + name: String, + private val roots: List, + private val entries: Map, + ) : ResourceDir(parent, name, roots.mapNotNull(entries::get)) { + override fun getNames(): Set = + roots + .flatMap { root -> + entries.keys.filter { it.parentFile == root || (it == root && entries[it]?.isFile == true) } + }.map { it.name } + .toSortedSet(String.CASE_INSENSITIVE_ORDER) + + override fun getByName(name: String): Any? { + val children = + roots.mapNotNull { root -> + val child = if (entries[root]?.isFile == true && root.name.equals(name, ignoreCase = true)) root else File(root, name) + entries[child]?.let { child to it } + } + val first = children.firstOrNull() ?: return null + if (!first.second.isDirectory) { + if (first.second.path in + changedSources + ) { + throw UnprovenResource("A source edited by this move is also embedded as a resource") + } + // FileExpression reads the original bytes, but embeds the proposed basename. + return object : File(first.second.path) { + override fun getName(): String = name + } + } + return Directory(this, name, children.filter { it.second.isDirectory }.map { it.first }, entries) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResources.kt new file mode 100644 index 00000000000..bfb823c790f --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResources.kt @@ -0,0 +1,104 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.tool.ResourceDir +import java.io.File +import java.nio.file.FileVisitOption.FOLLOW_LINKS +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.BasicFileAttributes +import java.security.MessageDigest +import java.util.HexFormat +import java.util.concurrent.CancellationException +import java.util.List.copyOf as immutableList +import java.util.Map.copyOf as immutableMap + +/** + * Resource lookup uses compiler rules; cache keys retain only immutable filesystem observations. + */ +@ConsistentCopyVisibility +internal data class XdkResources + private constructor( + val roots: List, + val entries: Map, + ) { + val revision: String = + fingerprint( + (roots.map { it.path } + entries.toSortedMap().flatMap { listOf(it.key, it.value) }) + .joinToString("\u0000") + .toByteArray(), + ) + + fun directory(): ResourceDir = ResourceDir(roots.filter(File::exists)) + + companion object { + fun roots( + root: File, + configured: List?, + ): List = + configured + ?: if (root.parentFile.isDirectory) { + ResourceDir.forSource(root, true).locations.map { it.canonicalFile } + } else { + // A moved/deleted module container has no ModuleInfo yet. Retain its source + // directory as a missing input until discovery or host configuration replaces + // it. + listOf(root.parentFile.canonicalFile) + } + + fun capture( + root: File, + configured: List?, + source: Collection, + cancelled: () -> Boolean, + ): XdkResources { + val roots = roots(root, configured) + // Most modules do not use embedded resources. Use the compiler lexer conservatively: + // an uncertain lex or interpolation takes the full resource snapshot as well. + val entries = + buildMap { + if (source.any(XdkLexical::mayUseResources)) { + roots.forEach { location -> + if (cancelled()) throw CancellationException() + if (!location.exists()) { + put(location.path, "missing") + } else { + Files.walk(location.toPath(), FOLLOW_LINKS).use { paths -> + paths.forEach { path -> + if (cancelled()) throw CancellationException() + put(path.toString(), entry(path, cancelled)) + } + } + } + } + } + } + return XdkResources(immutableList(roots), immutableMap(entries)) + } + + internal fun entry( + path: Path, + cancelled: () -> Boolean, + ): String { + val attrs = Files.readAttributes(path, BasicFileAttributes::class.java) + val content = + if (attrs.isDirectory) { + "directory" + } else { + val hash = MessageDigest.getInstance("SHA-256") + Files.newInputStream(path).use { input -> + val buffer = ByteArray(8192) + while (true) { + if (cancelled()) throw CancellationException() + val read = input.read(buffer) + if (read < 0) break + hash.update(buffer, 0, read) + } + } + HexFormat.of().formatHex(hash.digest()) + } + return "$content:${attrs.creationTime()}:${attrs.lastModifiedTime()}" + } + + private fun fingerprint(bytes: ByteArray): String = HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes)) + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt new file mode 100644 index 00000000000..522673ac49d --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt @@ -0,0 +1,106 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.compiler.Lexer +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AnnotationExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.util.ExecutionTrace + +/** Private source members have bounded consumers; public APIs and initialization effects do not. */ +internal object XdkSafeDelete { + data class Candidate( + val name: String, + val identity: ProofIdentity, + val edit: XdkRename.Edit, + val removed: Set, + ) + + fun candidate( + text: String, + selection: Range, + model: SemanticModel, + facts: CompilerRenameFacts, + ): Candidate? { + val source = model.sourceName ?: return null + val symbol = model.symbolAt(selection.start.line, selection.start.column) ?: return null + if (symbol.declarationSource != source) return null + val declaration = symbol.declaration ?: return null + val identity = facts.constants[symbol.id] as? ProofIdentity.Source ?: return null + val errors = ErrorList() + val root = XdkRefactoringSyntax.parse(text, errors, "safe-delete") ?: return null + + val parents = XdkRefactoringSyntax.tree(root).toMap() + + fun at(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) + val member = + when (symbol.kind) { + SemanticModel.SymbolKind.METHOD -> { + parents.keys.filterIsInstance().singleOrNull { + it.nameToken?.startPosition?.let(::at) == declaration.start && it.defaultAccess == Access.PRIVATE && + !it.isConstructor && !it.isConstructorFinally && it.childNodes().any { child -> child is StatementBlock } + } + } + + SemanticModel.SymbolKind.PROPERTY -> { + if (SemanticModel.SourceLocation(source, declaration) !in facts.constantProperties) return null + parents.keys.filterIsInstance().singleOrNull { + at(it.nameToken.startPosition) == declaration.start && it.defaultAccess == Access.PRIVATE && it.isStatic && + it.childNodes().none { child -> child is StatementBlock } + } + } + + else -> { + null + } + } ?: return null + if (parents[parents[member]] !is TypeCompositionStatement) return null + if (XdkRefactoringSyntax.tree(member).any { (node, _) -> node is AnnotationExpression }) return null + val start = XdkRename.offset(text, at(member.startPosition)) ?: return null + val end = XdkRename.offset(text, at(member.endPosition)) ?: return null + val terminator = Regex("[ \t]*;").find(text, end)?.takeIf { it.range.first == end } + val syntaxEnd = terminator?.range?.last?.plus(1) ?: end + val tokens = + ExecutionTrace.api("Lexer.lex(safe-delete)") { + Lexer(Source(text.substring(start, syntaxEnd)), errors).asSequence().toList() + } + if (errors.hasSeriousErrors() || tokens.any { it.id in setOf(Token.Id.EOL_COMMENT, Token.Id.ENC_COMMENT) }) return null + val members = SemanticModel.Range(at(member.startPosition), at(member.endPosition)) + + fun inside( + path: String?, + range: SemanticModel.Range, + ) = path == source && range.start >= members.start && range.end <= members.end + // Include every configured source view, not just the active file's reference list. + if (facts.models.any { view -> + view.occurrences.any { occurrence -> + occurrence.symbol?.let(facts.constants::get) == identity && !inside(view.sourceName, occurrence.range) + } || view.calls.any { call -> facts.constants[call.method] == identity && !inside(view.sourceName, call.callee) } + } + ) { + return null + } + val removed = + facts.models + .filter { it.sourceName == source } + .flatMap { view -> + view.occurrences.map { it.range } + view.calls.map { it.callee } + }.filter { inside(source, it) } + .mapTo(linkedSetOf()) { SemanticModel.SourceLocation(source, it) } + val lineStart = maxOf(text.lastIndexOf('\n', start - 1), text.lastIndexOf('\r', start - 1)) + 1 + val lineEnd = Regex("[ \t]*(?:\r\n|\r|\n)").find(text, syntaxEnd)?.takeIf { it.range.first == syntaxEnd } + val edit = + if (text.substring(lineStart, start).isBlank() && lineEnd != null) { + XdkRename.Edit(lineStart, lineEnd.range.last + 1, "") + } else { + XdkRename.Edit(start, syntaxEnd, "") + } + return Candidate(symbol.name, identity, edit, removed) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt new file mode 100644 index 00000000000..3d74a9da3f9 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt @@ -0,0 +1,163 @@ +package org.xvm.lsp.adapter.xdk + +import java.io.File +import java.util.List.copyOf as immutableList +import java.util.Map.copyOf as immutableMap +import java.util.Set.copyOf as immutableSet + +/** + * Explicit host configuration; dependency names may identify source modules or binary artifacts. + */ +class XdkSourceModule( + val name: String, + uri: String, + dependencies: Set = emptySet(), + resourceRoots: List? = null, +) { + internal val root: File = + requireNotNull(XdkSources.file(uri)) { "A source module requires a file URI: $uri" } + val uri: String = root.toURI().toString() + val dependencies: Set = immutableSet(dependencies) + internal val resourceFiles: List? = + resourceRoots + ?.map { + requireNotNull(XdkSources.file(it)) { "A resource root requires a file URI: $it" } + }?.let(::immutableList) + val resourceRoots: List? = + resourceFiles?.map { it.toURI().toString().trimEnd('/') + "/" }?.let(::immutableList) + + init { + require(resourceFiles.orEmpty().all { !it.exists() || it.isDirectory }) { + "Resource roots must be directories" + } + require(resourceFiles == null || resourceFiles.distinct().size == resourceFiles.size) { + "Duplicate resource roots" + } + require(name.isNotBlank() && root.extension == "x") { + "A source module requires a name and an .x root" + } + require(name !in XdkLibraries.moduleNames) { + "Project sources cannot replace bundled compiler libraries" + } + } +} + +/** + * Immutable graph. Ordering and reverse edges are established before installing a configuration. + */ +internal class XdkProject( + modules: List, +) { + val modules: Map = immutableMap(modules.associateBy { it.name }) + + private data class Configuration( + val name: String, + val uri: String, + val dependencies: Set, + val resources: List?, + ) + + private val configuration = + modules.map { Configuration(it.name, it.uri, it.dependencies, it.resourceRoots) }.toSet() + private val ordered: List + + init { + require(this.modules.size == modules.size) { "Duplicate source module name" } + require(modules.map { it.root }.distinct().size == modules.size) { + "Duplicate source module root" + } + modules.forEach { owner -> + val members = File(owner.root.parentFile, owner.root.nameWithoutExtension).toPath() + require(modules.none { it !== owner && it.root.toPath().startsWith(members) }) { + "Overlapping source module roots: ${owner.uri}" + } + } + val visited = mutableSetOf() + val visiting = linkedSetOf() + val result = mutableListOf() + + fun visit(module: XdkSourceModule) { + if (module.name in visited) return + require(visiting.add(module.name)) { + "Cyclic source dependencies: ${visiting.joinToString(" -> ")} -> ${module.name}" + } + module.dependencies + .sorted() + .mapNotNull(this.modules::get) + .forEach(::visit) + visiting.remove(module.name) + visited += module.name + result += module + } + modules.forEach(::visit) + ordered = result.toList() + } + + fun scope(uri: String): String? = XdkSources.file(uri)?.let(::scope) + + fun scope(file: File): String? = + modules.values + .filter { + file == it.root || + file + .toPath() + .startsWith(File(it.root.parentFile, it.root.nameWithoutExtension).toPath()) + }.maxByOrNull { it.root.path.length } + ?.uri + + fun resourceScopes(uri: String): Set { + val file = XdkSources.file(uri)?.toPath() ?: return emptySet() + return modules.values + .filter { module -> + XdkResources.roots(module.root, module.resourceFiles).any { + file.startsWith(it.toPath()) || it.toPath().startsWith(file) + } + }.mapTo(linkedSetOf()) { it.uri } + } + + fun inputWatchRoots(): Set = + modules.values + .flatMap { + XdkResources.roots(it.root, it.resourceFiles) + it.root.parentFile + }.toSet() + + fun sameConfiguration(other: XdkProject): Boolean = configuration == other.configuration + + /** Added, removed and changed source inputs, independent of presentation order. */ + fun changedModules(other: XdkProject): Set = + ((configuration - other.configuration) + (other.configuration - configuration)) + .mapTo(linkedSetOf()) { it.name } + + /** Complete configured source graph, including modules that have never been opened. */ + fun buildOrder(): List = ordered + + fun buildOrder(scope: String): List { + val target = modules.values.firstOrNull { it.uri == scope } ?: return emptyList() + val names = mutableSetOf() + + fun include(module: XdkSourceModule) { + if (names.add(module.name)) { + module.dependencies.mapNotNull(modules::get).forEach(::include) + } + } + include(target) + return ordered.filter { it.name in names } + } + + /** Includes the changed module and all consumers, in dependency order. */ + fun affected(scope: String): Set { + val changed = modules.values.firstOrNull { it.uri == scope } ?: return setOf(scope) + val names = mutableSetOf(changed.name) + return buildSet { + ordered.forEach { module -> + if (module.name in names || module.dependencies.any(names::contains)) { + names += module.name + add(module.uri) + } + } + } + } + + fun orderedScopes(scopes: Set): Set = + ordered.map { it.uri }.filterTo(linkedSetOf()) { it in scopes }.apply { addAll(scopes) } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceMoves.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceMoves.kt new file mode 100644 index 00000000000..50f8bb59082 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceMoves.kt @@ -0,0 +1,104 @@ +package org.xvm.lsp.adapter.xdk + +import java.io.File +import java.nio.file.Files + +/** Separate compiler source remapping from the minimal, ordered client resource operations. */ +internal class XdkSourceMoves( + val paths: Map = emptyMap(), + val resources: Map = emptyMap(), +) { + companion object { + fun directory( + source: File, + name: String, + texts: Map, + directories: Set, + ): XdkSourceMoves? { + if (source !in directories || !source.isDirectory) return null + val destination = File(source.parentFile, name) + if ( + destination.exists() || + destination in directories || + File(source.parentFile, "$name.x").exists() + ) { + return null + } + if (source.walkTopDown().any { Files.isSymbolicLink(it.toPath()) }) return null + val prefix = source.toPath() + val entries = texts.keys.map(::File) + directories + val paths = + entries + .filter { it.toPath().startsWith(prefix) } + .associate { + it.path to + destination.toPath().resolve(prefix.relativize(it.toPath())).toString() + } + return XdkSourceMoves(paths, mapOf(source.path to destination.path)) + } + + fun plan( + source: File, + name: String, + texts: Map, + directories: Set, + ): XdkSourceMoves? = plan(source, File(source.parentFile, "$name.x"), texts, directories) + + fun plan( + source: File, + destination: File, + texts: Map, + directories: Set, + ): XdkSourceMoves? { + val companion = File(source.parentFile, source.nameWithoutExtension) + val movedCompanion = File(destination.parentFile, destination.nameWithoutExtension) + if ( + destination.exists() || + destination.path in texts || + movedCompanion.exists() || + movedCompanion in directories + ) { + return null + } + // Symlinks may escape the captured tree; a rename proof cannot establish their + // membership. + if ( + Files.isSymbolicLink(source.toPath()) || + ( + companion.exists() && + companion.walkTopDown().any { Files.isSymbolicLink(it.toPath()) } + ) + ) { + return null + } + val prefix = companion.toPath() + val members = texts.keys.map(::File).filter { it.toPath().startsWith(prefix) } + val descendants = directories.filter { it.toPath().startsWith(prefix) } + val paths = + buildMap { + put(source.path, destination.path) + (members + descendants).forEach { file -> + put( + file.path, + movedCompanion + .toPath() + .resolve(prefix.relativize(file.toPath())) + .toString(), + ) + } + } + val resources = + buildMap { + put(source.path, destination.path) + // A virtual companion with only unsaved files has no directory to rename, + // so the client cannot apply this resource operation safely. + if (companion.isDirectory) { + put(companion.path, movedCompanion.path) + } else if (members.isNotEmpty()) { + return null + } + } + return XdkSourceMoves(paths, resources) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt new file mode 100644 index 00000000000..6f1f8a7cd12 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt @@ -0,0 +1,223 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorListener +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.tool.ModuleInfo +import org.xvm.tool.ResourceDir +import java.io.File +import java.io.IOException +import java.net.URI +import java.nio.file.Files +import java.util.concurrent.CancellationException +import java.util.Map.copyOf as immutableMap +import java.util.Set.copyOf as immutableSet + +/** One module's immutable text and membership, with editor overlays taking precedence over disk. */ +internal class XdkSources + private constructor( + private val root: File, + text: Map, + directories: Set, + private val aliases: Map, + private val resources: XdkResources, + private val configuredResourceRoots: List?, + resourceMoves: XdkResourceMoves? = null, + ) : ModuleInfo(root, moduleName(root, text.getValue(root))) { + private val text = immutableMap(text) + private val resourceDirectory = resourceMoves?.directory(resources) ?: resources.directory() + + /** Cache identity contains only immutable input values, never a parsed ModuleInfo tree. */ + val inputs = Inputs(this.text, immutableSet(directories), immutableMap(aliases), resources) + + data class Inputs( + val text: Map, + val directories: Set, + val aliases: Map, + val resources: XdkResources, + ) + + private val entries: Map> = + buildMap> { + val boundary = File(root.parentFile, root.nameWithoutExtension) + for (directory in directories) { + getOrPut(directory) { linkedMapOf() } + if (directory != boundary) { + getOrPut(directory.parentFile) { linkedMapOf() }[directory] = + SourceEntry(directory, true) + } + } + for (file in text.keys.filter { it != root }) { + getOrPut(file.parentFile) { linkedMapOf() }[file] = SourceEntry(file, false) + var directory = file.parentFile + while (directory != boundary) { + getOrPut(directory.parentFile) { linkedMapOf() }[directory] = + SourceEntry(directory, true) + directory = directory.parentFile + } + } + }.mapValues { (_, files) -> files.values.sortedBy { it.file().name } } + + val sourceUris: Map = text.keys.associate { it.path to uri(it) } + + val documentUris: Set = text.keys.mapTo(linkedSetOf(), ::uri) + + fun uri(file: File): String = aliases[file] ?: file.toURI().toString() + + /** ModuleInfo memoizes parsed trees. Keep graph captures free of compiler-owned state. */ + fun freshCompilationInput(): XdkSources = replay(root, inputs, text.mapKeys { it.key.path }) + + fun resourcesCurrent(cancelled: () -> Boolean): Boolean = + resources == XdkResources.capture(root, configuredResourceRoots, text.values, cancelled) + + override fun getResourceDir(): ResourceDir = resourceDirectory + + override fun getSourceFile(): File = root + + override fun getSourceDir(): File = root.parentFile + + override fun isSourceTree(): Boolean = entries.isNotEmpty() + + override fun sourceEntries(directory: File): List = entries[directory].orEmpty() + + override fun readSource(file: File): CharArray = + text[file]?.toCharArray() + ?: throw IOException("Source is absent from this compilation snapshot: $file") + + private data class Overlay( + val uri: String, + val text: String, + ) + + companion object { + private fun moduleName( + root: File, + text: String, + ): String = + ExecutionTrace.api("Parser.parseModuleName(snapshot)", root.path) { + try { + Parser( + Source(text, root.path), + ErrorListener.silent(ErrorListener.Silence.DISCARD), + ).parseModuleNameIgnoreEverythingElse() + } catch (_: CompilerException) { + null + } ?: root.nameWithoutExtension + } + + /** Replay captured source text; resources use the captured paths without filesystem writes. */ + fun replay( + root: File, + inputs: Inputs, + text: Map, + moves: Map = emptyMap(), + resourceMoves: XdkResourceMoves? = null, + members: Collection = listOf(inputs), + ): XdkSources = + XdkSources( + File(moves[root.path] ?: root.path), + members + .flatMap { it.text.entries } + .filter { (file, _) -> + val target = File(moves[file.path] ?: file.path) + val targetRoot = File(moves[root.path] ?: root.path) + target == targetRoot || + target.toPath().startsWith(File(targetRoot.parentFile, targetRoot.nameWithoutExtension).toPath()) + }.associate { (file, original) -> + val path = moves[file.path] ?: file.path + File(path) to (text[path] ?: original) + }, + members.flatMap { it.directories }.map { File(moves[it.path] ?: it.path) }.filterTo(linkedSetOf()) { + val targetRoot = File(moves[root.path] ?: root.path) + it.toPath().startsWith(File(targetRoot.parentFile, targetRoot.nameWithoutExtension).toPath()) + }, + inputs.aliases.filterKeys { it.path !in moves }, + inputs.resources, + inputs.resources.roots, + resourceMoves, + ) + + /** Convert an actual indexed Source name, never an unresolved import spelling. */ + fun sourceUri(name: String): String? = + runCatching { URI(name).takeIf { it.isAbsolute }?.toString() }.getOrNull() + ?: file(name)?.toURI()?.toString() + + /** Canonical paths join editor URIs, compiler source names and filesystem notifications. */ + fun file(name: String): File? = + runCatching { + val uri = URI(name) + when (uri.scheme) { + "file" -> File(uri).canonicalFile + null if File(name).isAbsolute -> File(name).canonicalFile + else -> null + } + }.getOrNull() + + fun moduleRoot( + uri: String, + overlays: Map, + ): File? = file(uri)?.let { moduleRoot(it, overlays.keys.mapNotNull(::file).toSet()) } + + /** Reuse canonical overlay paths within one operation, never across filesystem changes. */ + fun moduleRoot( + file: File, + openFiles: Set, + ): File { + var root = file + var directory = file.parentFile + while (directory?.parentFile != null) { + val candidate = File(directory.parentFile, directory.name + ".x") + if (candidate.isFile || candidate in openFiles) root = candidate + directory = directory.parentFile + } + return root + } + + /** Runs on the compiler worker. No source or temporary file is written by an overlay. */ + fun capture( + root: File, + overlays: Map, + resourceRoots: List? = null, + cancelled: () -> Boolean, + ): XdkSources { + val buffers = + overlays.entries + .mapNotNull { (uri, text) -> file(uri)?.let { it to Overlay(uri, text) } } + .toMap() + val directory = File(root.parentFile, root.nameWithoutExtension).toPath() + val files = linkedSetOf(root) + val directories = linkedSetOf() + if (Files.isDirectory(directory)) { + Files.walk(directory).use { paths -> + paths.forEach { + if (cancelled()) throw CancellationException() + val file = it.toFile().canonicalFile + if (file.toPath().startsWith(directory)) { + if (Files.isDirectory(it)) { + directories += file + } else if (Files.isRegularFile(it) && file.extension == "x") { + files += file + } + } + } + } + } + files += buffers.keys.filter { it.toPath().startsWith(directory) } + val text = + files.associateWith { file -> + if (cancelled()) throw CancellationException() + buffers[file]?.text ?: file.readText() + } + return XdkSources( + root, + text, + directories, + buffers.filterKeys { it in files }.mapValues { it.value.uri }, + XdkResources.capture(root, resourceRoots, text.values, cancelled), + resourceRoots, + ) + } + } + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt new file mode 100644 index 00000000000..2bc21643db1 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt @@ -0,0 +1,168 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypedefStatement +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement +import org.xvm.lsp.model.Location +import org.xvm.lsp.model.SymbolInfo +import org.xvm.lsp.model.SymbolInfo.SymbolKind + +/** + * Turning parsed source into symbols. + * + * Symbols have to come from the AST rather than from the compiled structures, which is not obvious: + * a `ClassStructure` knows its name, its kind, its members and its type, and knows nothing whatever + * about the text it was written in. It carries no source position. An editor cannot use a symbol it + * cannot point at, so the structures - the richer, resolved side of the compiler - are the wrong + * source for this, and the AST is the right one. + * + * What that costs: an AST node knows what was written, not what it resolved to. Names here are as + * they appear in the source, not qualified or resolved, and nothing below a declaration is + * followed. That is enough for `documentSymbol` and for finding what the cursor is inside, and it + * is not enough for go-to-definition or completion, which need resolution rather than syntax. + */ +internal object XdkSymbols { + /** + * @param uri the document the AST was parsed from + * @param root the parsed source, or null if it did not parse + * @return the declarations it contains, nested as they are nested in the source + */ + fun of( + uri: String, + root: AstNode?, + ): List = root?.let { declarationsIn(uri, it, it.source) } ?: emptyList() + + /** + * Find the innermost declaration containing a position, which is what a request about "the + * thing the cursor is on" means when all we have is the shape of the source. + */ + fun at( + symbols: List, + line: Int, + column: Int, + ): SymbolInfo? = + symbols + .firstOrNull { contains(it.location, line, column) } + ?.let { outer -> + at(outer.children, line, column) ?: outer + } + + private fun contains( + where: Location, + line: Int, + column: Int, + ): Boolean { + if (line < where.startLine || line > where.endLine) { + return false + } + if (line == where.startLine && column < where.startColumn) { + return false + } + return !(line == where.endLine && column > where.endColumn) + } + + /** + * Walk the children of a node, collecting the declarations and recursing through everything + * else - a class sits inside a statement block inside a module, and only the declarations are + * worth reporting. + */ + private fun declarationsIn( + uri: String, + node: AstNode, + source: Source?, + ): List = + buildList { + node.childNodes().forEach { child -> + // Recovered syntax has no compilation parentage yet; an absent source inherits the + // enclosing syntax tree's source. An explicitly different source is a module member. + if (child.source != null && child.source !== source) return@forEach + val symbol = symbolOf(uri, child) + if (symbol == null) { + addAll(declarationsIn(uri, child, source)) + } else { + add(symbol.withChildren(declarationsIn(uri, child, source))) + } + } + } + + private fun symbolOf( + uri: String, + node: AstNode, + ): SymbolInfo? = + when (node) { + is TypeCompositionStatement -> { + node.name?.let { SymbolInfo.of(it, kindOf(node), rangeOf(uri, node)) } + } + + is MethodDeclarationStatement -> { + SymbolInfo.of(node.name, SymbolKind.METHOD, rangeOf(uri, node)) + } + + is PropertyDeclarationStatement -> { + SymbolInfo.of(node.name, SymbolKind.PROPERTY, rangeOf(uri, node)) + } + + is TypedefStatement -> { + // LSP has no alias kind; represent the written type declaration as a type. + SymbolInfo.of(node.nameToken.valueText, SymbolKind.CLASS, rangeOf(uri, node)) + } + + is IncompleteDeclarationStatement -> { + node.nameToken.orElse(null)?.let { + val kind = + if (node.kind == IncompleteDeclarationStatement.Kind.METHOD) { + SymbolKind.METHOD + } else { + SymbolKind.PROPERTY + } + SymbolInfo.of(it.valueText, kind, rangeOf(uri, node)) + } + } + + else -> { + null + } + } + + /** + * The category token is what the author wrote - `class`, `mixin`, `service` - so it is what the + * outline should say. + */ + private fun kindOf(node: TypeCompositionStatement): SymbolKind = + when (node.category.id) { + Token.Id.MODULE -> SymbolKind.MODULE + + Token.Id.PACKAGE -> SymbolKind.PACKAGE + + Token.Id.INTERFACE -> SymbolKind.INTERFACE + + Token.Id.MIXIN, + Token.Id.ANNOTATION, + -> SymbolKind.MIXIN + + Token.Id.SERVICE -> SymbolKind.SERVICE + + Token.Id.CONST -> SymbolKind.CONST + + Token.Id.ENUM -> SymbolKind.ENUM + + else -> SymbolKind.CLASS + } + + private fun rangeOf( + uri: String, + node: AstNode, + ): Location = + Location( + uri = uri, + startLine = Source.calculateLine(node.startPosition), + startColumn = Source.calculateOffset(node.startPosition), + endLine = Source.calculateLine(node.endPosition), + endColumn = Source.calculateOffset(node.endPosition), + ) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt new file mode 100644 index 00000000000..d1ab94d2d49 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt @@ -0,0 +1,506 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Lexer +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.Token +import org.xvm.compiler.Token.Id +import org.xvm.compiler.ast.AnnotatedTypeExpression +import org.xvm.compiler.ast.ArrayTypeExpression +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.BiTypeExpression +import org.xvm.compiler.ast.DecoratedTypeExpression +import org.xvm.compiler.ast.Expression +import org.xvm.compiler.ast.ForEachStatement +import org.xvm.compiler.ast.ForStatement +import org.xvm.compiler.ast.FunctionTypeExpression +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.LiteralExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.NewExpression +import org.xvm.compiler.ast.NullableTypeExpression +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.PropertyDeclarationStatement +import org.xvm.compiler.ast.StatementBlock +import org.xvm.compiler.ast.SwitchStatement +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.compiler.ast.TypeExpression +import org.xvm.compiler.ast.VariableDeclarationStatement +import org.xvm.compiler.ast.VariableTypeExpression +import org.xvm.compiler.ast.WhileStatement +import org.xvm.compiler.ast.partial.PartialSyntax +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.CompletionItem.CompletionKind +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.adapter.xdk.PartialSemanticModel.DeclarationName +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.CancellationException + +/** + * Worker-only syntax proposals from the Java parser and lexer. These are editing templates, not + * semantic bindings. No synthetic declaration is compiled or installed in an editor snapshot. + */ +internal object XdkSyntaxCompletions { + fun complete( + text: String, + position: Position, + declarationNameType: DeclarationName? = null, + cancelled: () -> Boolean, + ): List = + ExecutionTrace.api("Parser.syntaxCompletions") { + val cursor = + XdkRename.offset(text, SemanticModel.Position(position.line, position.column)) + ?: return@api emptyList() + val errors = ErrorListener.cancellable(ErrorList(), cancelled) + + fun checkCancellation() { + if (cancelled()) throw CancellationException() + } + + // Token positions are UTF-16 line/column pairs; index once instead of rescanning the + // document for every token and node in a large file. + val lineStarts = listOf(0) + newlines.findAll(text).map { it.range.last + 1 }.toList() + + fun offset(value: Long) = lineStarts[Source.calculateLine(value)] + Source.calculateOffset(value) + checkCancellation() + val tokens = + try { + Lexer(Source(text), errors).asSequence().onEach { checkCancellation() }.toList() + } catch (_: CompilerException) { + checkCancellation() + return@api emptyList() + } + if (errors.hasSeriousErrors()) return@api emptyList() + val containing = tokens.firstOrNull { offset(it.startPosition) < cursor && cursor <= offset(it.endPosition) } + val selected = containing?.takeIf { it.id == Id.IDENTIFIER || it.id in keywords } + if (containing != null && selected == null && + (cursor < offset(containing.endPosition) || containing.id in comments || containing.id.name.startsWith("LIT_")) + ) { + return@api emptyList() + } + val start = selected?.let { offset(it.startPosition) } ?: cursor + val end = selected?.let { offset(it.endPosition) } ?: cursor + val prefix = text.substring(start, cursor) + if (prefix.any { !Lexer.isIdentifierPart(it) }) return@api emptyList() + val range = selected?.let { XdkAst.spanOf(it.startPosition, it.endPosition) } ?: Range(position, position) + if (declarationNameType != null) { + return@api if (selected == null) declarationNames(declarationNameType, tokens, null, "", range) else emptyList() + } + val root = + try { + Parser(Source(text), errors).parseSource() + } catch (_: CompilerException) { + checkCancellation() + return@api emptyList() + } + checkCancellation() + + fun nodes( + node: AstNode, + ancestors: List, + ): List { + checkCancellation() + return listOf(Entry(node, ancestors)) + + PartialSyntax.children(node).use { it.toList() }.flatMap { nodes(it, ancestors + node) } + } + val entries = nodes(root, emptyList()) + val declaration = selected?.let { token -> entries.firstOrNull { it.name()?.startPosition == token.startPosition } } + if (declaration != null) { + return@api declarationNames(declaration, tokens, selected, prefix, range) + } + + val block = + entries + .filter { it.node is StatementBlock && it.node !== root } + .filter { offset(it.node.startPosition) < start && end <= offset(it.node.endPosition) } + .maxByOrNull { it.ancestors.size } + val context = + when { + block == null && tokens.all { it === selected || it.id in comments } -> Context.FILE + + block == null -> return@api emptyList() + + block.ancestors.lastOrNull() is TypeCompositionStatement || + block.ancestors.lastOrNull() is NewExpression -> Context.TYPE + + block.ancestors + .lastOrNull { + it is MethodDeclarationStatement || it is TypeCompositionStatement || + it is NewExpression || it is LambdaExpression || it is PropertyDeclarationStatement + }.let { it is MethodDeclarationStatement || it is LambdaExpression } -> Context.STATEMENT + + else -> return@api emptyList() + } + val previous = tokens.lastOrNull { offset(it.endPosition) <= start && it !== selected && it.id !in comments } + if (context != Context.FILE) { + val body = block!!.node as StatementBlock + val boundary = + previous?.let { token -> + (token.id == Id.L_CURLY && token.startPosition == body.startPosition) || + ( + token.id in setOf(Id.SEMICOLON, Id.R_CURLY) && + body.statements.any { + it.endPosition == token.endPosition || + ( + token.id == Id.SEMICOLON && offset(it.endPosition) <= offset(token.startPosition) && + text.substring(offset(it.endPosition), offset(token.startPosition)).isBlank() + ) + } + ) + } == true + if (!boundary) return@api emptyList() + } + // Only offer whole templates at a vacant slot. Completing an existing keyword must not + // duplicate its written condition, body, arguments or semicolon. + val next = tokens.firstOrNull { offset(it.startPosition) >= end && it !== selected && it.id !in comments } + val vacant = next == null || next.id == Id.R_CURLY + val lineStart = lineStarts[position.line] + val indent = text.substring(lineStart, start).takeWhile { it == ' ' || it == '\t' } + val newline = newlines.find(text)?.value ?: "\n" + val words = + when (context) { + Context.FILE -> { + fileKeywords + } + + Context.TYPE -> { + typeKeywords + } + + Context.STATEMENT -> { + val enclosing = + block!!.ancestors.takeLastWhile { + it !is MethodDeclarationStatement && it !is LambdaExpression && it !is NewExpression + } + val loop = enclosing.any { it is ForStatement || it is ForEachStatement || it is WhileStatement } + statementKeywords + + listOfNotNull( + Id.BREAK.takeIf { loop || enclosing.any { it is SwitchStatement } }, + Id.CONTINUE.takeIf { loop }, + ) + } + } + return@api buildList { + words.map { it.TEXT }.filter { it.startsWith(prefix) }.forEach { word -> + add( + CompletionItem( + word, + CompletionKind.KEYWORD, + "Ecstasy ${context.description} keyword", + word, + TextEdit(range, word), + sortText = "6:$word", + ), + ) + } + if (vacant) { + templates.filter { it.context == context && it.keyword.startsWith(prefix) }.forEach { template -> + fun layout(value: String) = value.replace("\n", "$newline$indent") + val plain = layout(template.plain) + add( + CompletionItem( + template.label, + CompletionKind.SNIPPET, + "Ecstasy ${context.description} template", + plain, + TextEdit(range, plain), + "Syntax template; review its name, condition and body before compiling.", + "7:${template.label}", + snippet = layout(template.snippet), + ), + ) + } + } + } + } + + private data class Entry( + val node: AstNode, + val ancestors: List, + ) { + fun name(): Token? = + when (node) { + is PropertyDeclarationStatement -> node.nameToken + is VariableDeclarationStatement -> node.nameToken + is Parameter -> node.nameToken + is MethodDeclarationStatement -> node.nameToken + is TypeCompositionStatement -> node.nameToken + else -> null + } + } + + private fun declarationNames( + entry: Entry, + tokens: List, + selected: Token, + prefix: String, + range: Range, + ): List { + val type = + when (val node = entry.node) { + is PropertyDeclarationStatement -> node.type + is Parameter -> node.type + is VariableDeclarationStatement -> node.childNodes().filterIsInstance().singleOrNull() + else -> null + } ?: return emptyList() + val initializer = (entry.ancestors.lastOrNull() as? AssignmentStatement)?.rValue + return declarationNames(declarationName(type, initializer) ?: return emptyList(), tokens, selected, prefix, range) + } + + /** Copy syntax while on the compiler worker. Presentation policy stays outside the AST. */ + internal fun declarationName( + type: TypeExpression, + initializer: Expression? = null, + ): DeclarationName? { + fun base(node: TypeExpression): String? = + when (node) { + is NamedTypeExpression -> { + node.nameToken?.valueText + } + + is FunctionTypeExpression -> { + "fn" + } + + is BiTypeExpression -> { + "value" + } + + is ArrayTypeExpression -> { + node + .childNodes() + .filterIsInstance() + .singleOrNull() + ?.let(::base) + ?.let { "${it}Array" } + } + + is NullableTypeExpression, is DecoratedTypeExpression, is AnnotatedTypeExpression -> { + node + .childNodes() + .filterIsInstance() + .singleOrNull() + ?.let(::base) + } + + else -> { + null + } + } + if (type is VariableTypeExpression) { + val suggestion = + when (initializer) { + is NewExpression -> { + initializer + .childNodes() + .filterIsInstance() + .firstOrNull() + ?.let(::base) + } + + is LiteralExpression -> { + when (initializer.literal.id) { + Id.LIT_STRING -> "text" + Id.LIT_CHAR -> "character" + Id.LIT_INT, Id.LIT_DEC -> "number" + Id.LIT_BINSTR -> "bytes" + else -> null + } + } + + is NameExpression -> { + initializer.name.takeUnless { it == "Null" }?.let { + if (it == "True" || it == "False") "flag" else it + } + } + + else -> { + null + } + } + return suggestion?.let { DeclarationName(it, "written $type initializer") } + } + return base(type)?.let { DeclarationName(it, "written type $type") } + } + + private fun declarationNames( + type: DeclarationName, + tokens: List, + selected: Token?, + prefix: String, + range: Range, + ): List { + // Acronyms retain their word boundary: HTTPClient -> httpClient, URL -> url. + val written = type.base + val capitals = written.takeWhile(Char::isUpperCase).length + val count = if (capitals > 1 && capitals < written.length) capitals - 1 else capitals.coerceAtLeast(1) + val base = written.take(count).lowercase() + written.drop(count) + if (!Lexer.isValidIdentifier(base)) return emptyList() + val occupied = + tokens + .filter { it.id == Id.IDENTIFIER && it.startPosition != selected?.startPosition } + .map { it.valueText } + .toSet() + val name = generateSequence(0) { it + 1 }.map { if (it == 0) base else "$base$it" }.first { it !in occupied } + if (!name.startsWith(prefix) || name == selected?.valueText) return emptyList() + return listOf( + CompletionItem( + name, + CompletionKind.VARIABLE, + "Name from ${type.basis}", + name, + TextEdit(range, name), + "Declaration name suggestion, not a resolved reference or rename.", + "0:$name", + ), + ) + } + + private enum class Context( + val description: String, + ) { + FILE("file"), + TYPE("member"), + STATEMENT("statement"), + } + + private data class Template( + val context: Context, + val keyword: String, + val label: String, + val plain: String, + val snippet: String, + ) + + private val comments = setOf(Id.EOL_COMMENT, Id.ENC_COMMENT) + private val newlines = Regex("\\r\\n|\\r|\\n") + private val fileKeywords = listOf(Id.MODULE, Id.PACKAGE, Id.CLASS, Id.INTERFACE, Id.SERVICE, Id.CONST, Id.ENUM, Id.MIXIN) + private val typeKeywords = + listOf( + Id.CLASS, + Id.INTERFACE, + Id.SERVICE, + Id.CONST, + Id.ENUM, + Id.MIXIN, + Id.TYPEDEF, + Id.IMPORT, + Id.VOID, + Id.PUBLIC, + Id.PROTECTED, + Id.PRIVATE, + Id.STATIC, + Id.PACKAGE, + Id.ANNOTATION, + Id.CONSTRUCT, + Id.CONDITIONAL, + Id.FUNCTION, + Id.IMMUTABLE, + ) + private val statementKeywords = + listOf( + Id.IF, + Id.WHILE, + Id.FOR, + Id.SWITCH, + Id.RETURN, + Id.THROW, + Id.TRY, + Id.ASSERT, + Id.VAL, + Id.VAR, + Id.DO, + Id.USING, + Id.FUNCTION, + Id.IMMUTABLE, + ) + private val keywords = (fileKeywords + typeKeywords + statementKeywords + listOf(Id.BREAK, Id.CONTINUE)).toSet() + private val templates = + listOf( + Template( + Context.FILE, + "module", + "module declaration", + "module Example {\n \n}", + $$"module ${1:Example} {\n $0\n}", + ), + Template( + Context.TYPE, + "class", + "class declaration", + "class Example {\n \n}", + $$"class ${1:Example} {\n $0\n}", + ), + Template( + Context.TYPE, + "void", + "void method", + "void run() {\n \n}", + $$"void ${1:run}() {\n $0\n}", + ), + Template( + Context.STATEMENT, + "if", + "if block", + "if (True) {\n \n}", + $$"if (${1:True}) {\n $0\n}", + ), + Template( + Context.STATEMENT, + "while", + "while loop", + "while (False) {\n \n}", + $$"while (${1:False}) {\n $0\n}", + ), + Template( + Context.STATEMENT, + "return", + "return value", + "return value;", + $$"return ${1:value};$0", + ), + Template( + Context.TYPE, + "interface", + "interface declaration", + "interface Example {\n \n}", + $$"interface ${1:Example} {\n $0\n}", + ), + Template( + Context.TYPE, + "service", + "service declaration", + "service Example {\n \n}", + $$"service ${1:Example} {\n $0\n}", + ), + Template( + Context.STATEMENT, + "for", + "for range", + "for (Int i : 0..<1) {\n \n}", + $$"for (Int i : ${1:0..<1}) {\n $0\n}", + ), + Template( + Context.STATEMENT, + "do", + "do loop", + "do {\n \n} while (False);", + $$"do {\n $0\n} while (${1:False});", + ), + Template( + Context.STATEMENT, + "try", + "try catch", + "try {\n \n} catch (Exception e) {\n}", + $$"try {\n $0\n} catch (${1:Exception} e) {\n}", + ), + ) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt new file mode 100644 index 00000000000..03f4e5b6f6c --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt @@ -0,0 +1,229 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.Constant +import java.io.File + +/** Plan qualification changes from resolved type identities, never from matching source text. */ +internal object XdkTypeMoves { + data class Proposal( + val edits: Map>, + val resources: Map, + val qualifications: Map>, + val destinations: List, + val imports: Map>, + val dependencies: Map>, + ) { + /** Even an unused moved declaration must acquire the intended compiler owner. */ + fun provesDestinations(facts: CompilerRenameFacts): Boolean = + destinations.all { destination -> + facts.typePaths.any { + val target = it.target as? ProofIdentity.Source + target?.location?.sourceName == destination.source && target.format == Constant.Format.Class && + it.module == destination.module && it.path == destination.path + } + } + } + + data class Destination( + val source: String, + val module: String, + val path: List, + ) + + fun plan( + facts: CompilerRenameFacts, + operations: Map, + texts: Map, + directories: Set, + project: XdkProject, + ): Proposal? { + val moves = + operations.map { (source, destination) -> + if (source.extension != "x" || destination.extension != "x" || !destination.parentFile.isDirectory) return null + val owner = + facts.typePaths.singleOrNull { + val target = it.target as? ProofIdentity.Source + target?.location?.sourceName == source.path && target.name == source.nameWithoutExtension && + target.format == Constant.Format.Class + } ?: return null + val targetModule = project.modules.values.singleOrNull { it.uri == project.scope(destination.path) } ?: return null + val namespace = namespace(facts, targetModule.name, destination.parentFile, texts, directories) ?: return null + // An inline declaration can occupy the destination without a file of its own. + // Reject before replay rather than presenting a duplicate component to the compiler. + if (facts.typePaths.any { + it.module == targetModule.name && it.path == namespace + destination.nameWithoutExtension && + it.target != owner.target + } + ) { + return null + } + val files = XdkSourceMoves.plan(source, destination, texts, directories) ?: return null + val rename = + XdkRename.fileNamePlan(facts, texts, source.path, source.nameWithoutExtension, destination.nameWithoutExtension) + ?: return null + Move(owner, targetModule.name, namespace + source.nameWithoutExtension, files, rename) + } + + fun owner(file: String): String? = + project.modules.values + .singleOrNull { it.uri == project.scope(file) } + ?.name + + fun movedOwner(file: String): String? = moves.singleOrNull { file in it.files.paths }?.module ?: owner(file) + + fun target(name: TypeName): Move? = + moves.singleOrNull { + name.module == it.owner.module && + name.path.take(it.owner.path.size) == it.owner.path + } + + fun changesModule(name: TypeName): Boolean { + val file = name.location.sourceName ?: return false + return target(name)?.let { it.module != it.owner.module } == true || movedOwner(file) != owner(file) + } + val dependencies = + facts.typeNames + .filter(::changesModule) + .mapNotNull { name -> + val file = name.location.sourceName ?: return null + val local = movedOwner(file) ?: return null + val module = target(name)?.module ?: name.module + if (local == module || module == "ecstasy.xtclang.org") null else local to module + }.groupBy({ it.first }, { it.second }) + .mapValues { it.value.toSet() } + val imports = + dependencies.mapValues { (module, required) -> + val root = + project.modules + .getValue(module) + .root.path + XdkMoveImports.plan(texts.getValue(root), texts.filterKeys { movedOwner(it) == module }.values, required) ?: return null + } + val qualifications = + facts.typeNames + .mapNotNull { name -> + val file = name.location.sourceName ?: return@mapNotNull null + val text = texts[file] ?: return null + val owners = moves.filter { name.module == it.owner.module && name.path.take(it.owner.path.size) == it.owner.path } + if (owners.size > 1) return null + val movedTarget = owners.singleOrNull() + val movedUse = moves.any { file in it.files.paths } + if (movedTarget == null && !movedUse) return@mapNotNull null + val start = XdkRename.offset(text, name.location.range.start) ?: return null + val end = XdkRename.offset(text, name.location.range.end) ?: return null + val spelling = XdkQualifiedName.parse(text.substring(start, end)) ?: return null + val written = spelling.names + val terminal = XdkRename.offset(text, name.terminal.start) ?: return null + val alias = + facts.models + .firstOrNull { it.sourceName == file } + ?.imports + .orEmpty() + .any { candidate -> candidate.uses.any { it.start == name.location.range.start } } + if (!name.imported && alias) return@mapNotNull null + if (written.last() != name.path.last()) return@mapNotNull null + val desired = movedTarget?.let { it.path + name.path.drop(it.owner.path.size) } ?: name.path + val localModule = movedOwner(file) ?: return null + val targetModule = movedTarget?.module ?: name.module + val qualified = + if (localModule == targetModule) { + desired + } else if (changesModule(name) && targetModule != "ecstasy.xtclang.org") { + listOf(imports.getValue(localModule).aliases.getValue(targetModule)) + desired + } else { + // Preserve the importing module's alias. A bare imported name remains bound + // through its rewritten import clause; never invent a module import here. + if (written.size <= name.path.size || written.takeLast(name.path.size) != name.path) return@mapNotNull null + written.dropLast(name.path.size) + desired + } + if (written == qualified) return@mapNotNull null + val suffix = + written + .asReversed() + .zip(qualified.asReversed()) + .takeWhile { it.first == it.second } + .size + if (suffix == 0) return null + val prefix = spelling.prefix(written.size - suffix, qualified.dropLast(suffix)) + if (start + prefix.end > terminal) return null + file to prefix.copy(start = start, end = start + prefix.end) + }.groupBy({ it.first }, { it.second }) + .mapValues { (_, value) -> value.distinct().sortedBy { it.start } } + val importEdits = + imports + .map { (module, planned) -> + project.modules + .getValue(module) + .root.path to listOf(planned.edit) + }.toMap() + val edits = + (qualifications.entries + moves.flatMap { it.rename.edits.entries } + importEdits.entries) + .groupBy({ it.key }, { it.value }) + .mapValues { (_, changes) -> changes.flatten().distinct().sortedWith(compareBy({ it.start }, { it.end })) } + if (edits.values.any { !XdkRename.disjoint(it) }) return null + val resources = moves.flatMap { it.files.resources.entries }.groupBy({ it.key }, { it.value }) + if (resources.values.any { it.distinct().size != 1 }) return null + val destinations = + moves.zip(operations.values).map { (move, destination) -> + Destination(destination.path, move.module, move.path.dropLast(1) + destination.nameWithoutExtension) + } + return Proposal(edits, resources.mapValues { it.value.first() }, qualifications, destinations, importEdits, dependencies) + } + + /** + * Extend the nearest compiler-owned package through captured implicit package directories. + * A companion source can change ownership, so only already resolved packages may cross it. + * Replay must subsequently prove the final declaration path, including unused declarations. + */ + private fun namespace( + facts: CompilerRenameFacts, + module: String, + destination: File, + texts: Map, + directories: Set, + ): List? { + val ancestors = generateSequence(destination) { it.parentFile }.toList() + val resolved = + ancestors.firstNotNullOfOrNull { candidate -> + facts.typePaths + .singleOrNull { it.module == module && directory(it.target) == candidate } + ?.let { candidate to it.path } + } ?: return null + val implicit = ancestors.takeWhile { it != resolved.first }.asReversed() + if (implicit.any { + it !in directories || !XdkRename.identifier(it.name) || File(it.parentFile, "${it.name}.x").path in texts + } + ) { + return null + } + return resolved.second + implicit.map { it.name } + } + + private data class Move( + val owner: TypePath, + val module: String, + val path: List, + val files: XdkSourceMoves, + val rename: XdkRename.Plan, + ) + + private fun directory(identity: ProofIdentity): File? = + when (identity) { + is ProofIdentity.Directory -> { + File(identity.path) + } + + is ProofIdentity.Source -> { + identity.location.sourceName + ?.let(::File) + ?.takeIf { + identity.format == Constant.Format.Module || + (identity.format == Constant.Format.Package && it.nameWithoutExtension == identity.name) + }?.let { File(it.parentFile, it.nameWithoutExtension) } + } + + else -> { + null + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt new file mode 100644 index 00000000000..3ce8ee50765 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt @@ -0,0 +1,167 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.CompilerException +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.CompositionNode +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.lsp.util.ExecutionTrace +import java.io.File +import java.nio.file.Files +import java.util.concurrent.CancellationException + +/** Parsed headers only: no compiler pools or ASTs survive discovery. */ +internal object XdkWorkspaceDiscovery { + private val excluded = + setOf(".git", ".gradle", ".idea", ".vscode-test", "build", "out", "node_modules") + + data class Header( + val text: String, + val module: String?, + val imports: Set, + val incomplete: Boolean, + ) + + class Catalog( + private val files: Map = emptyMap(), + ) { + fun withSource( + file: File, + text: String?, + cancelled: () -> Boolean, + ): Catalog = + if (text == null) { + Catalog(files - file) + } else { + Catalog(files + (file to header(file, text, files[file], cancelled))) + } + + fun modules(previous: Collection): List { + val roots = + files + .mapNotNull { (file, header) -> + val name = header.module ?: previous.firstOrNull { it.root == file }?.name + name?.takeUnless(XdkLibraries.moduleNames::contains)?.let { file to it } + }.toMap() + val names = roots.values.toSet() + return roots.entries + .sortedBy { it.key.path } + .map { (root, name) -> + val members = File(root.parentFile, root.nameWithoutExtension).toPath() + val headers = + files.filterKeys { it == root || it.toPath().startsWith(members) }.values + val imports = + headers.flatMap { it.imports } + + if (headers.any { it.incomplete }) { + previous.firstOrNull { it.root == root }?.dependencies.orEmpty() + } else { + emptySet() + } + XdkSourceModule( + name, + root.toURI().toString(), + imports.filterTo(linkedSetOf()) { it in names && it != name }, + ) + } + } + + fun rescan( + folders: List, + overlays: Map, + cancelled: () -> Boolean, + ): Catalog { + val buffers = + overlays + .mapNotNull { (uri, text) -> XdkSources.file(uri)?.let { it to text } } + .toMap() + val sources = + folders + .flatMap { folder -> + checkCurrent(cancelled) + if (!folder.isDirectory) { + emptyList() + } else { + folder + .walkTopDown() + .onEnter { + checkCurrent(cancelled) + it.name !in excluded && !Files.isSymbolicLink(it.toPath()) + }.filter { + it.isFile && + it.extension == "x" && + !Files.isSymbolicLink(it.toPath()) + }.map(File::getCanonicalFile) + .toList() + } + }.toSet() + buffers.keys.filter { includes(folders, it) } + return Catalog( + sources.associateWith { file -> + checkCurrent(cancelled) + header(file, buffers[file] ?: file.readText(), files[file], cancelled) + }, + ) + } + } + + fun includes( + folders: List, + file: File, + ): Boolean = + file.extension == "x" && + folders.any { folder -> + file.toPath().startsWith(folder.toPath()) && + folder.toPath().relativize(file.toPath()).none { it.toString() in excluded } + } + + fun scan( + folders: List, + overlays: Map, + previous: Collection, + cancelled: () -> Boolean, + ): List = Catalog().rescan(folders, overlays, cancelled).modules(previous) + + private fun header( + file: File, + text: String, + previous: Header?, + cancelled: () -> Boolean, + ): Header { + checkCurrent(cancelled) + if (previous?.text == text) return previous + val heard = ErrorList() + val errors = ErrorListener.cancellable(heard, cancelled) + val name = + ExecutionTrace.api("Parser.parseModuleName", file.toURI().toString()) { + Parser(Source(text, file.path), errors).parseModuleNameIgnoreEverythingElse() + } + val tree = + try { + ExecutionTrace.api( + "Parser.parseSource(partial-discovery)", + file.toURI().toString(), + ) { + Parser.forPartialAnalysis(Source(text, file.path), errors).parseSource() + } + } catch (_: CompilerException) { + null + } + checkCurrent(cancelled) + val imports = + tree + ?.let(::nodes) + .orEmpty() + .filterIsInstance() + .mapNotNull { (it.type as? NamedTypeExpression)?.name } + .toSet() + return Header(text, name, imports, tree == null || heard.hasSeriousErrors()) + } + + private fun checkCurrent(cancelled: () -> Boolean) { + if (cancelled()) throw CancellationException() + } + + private fun nodes(node: AstNode): List = listOf(node) + node.childNodes().flatMap(::nodes) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceNavigation.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceNavigation.kt new file mode 100644 index 00000000000..fb5cd2d4372 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceNavigation.kt @@ -0,0 +1,267 @@ +package org.xvm.lsp.adapter.xdk + +import org.xvm.lsp.adapter.CallHierarchyIncomingCall +import org.xvm.lsp.adapter.CallHierarchyItem +import org.xvm.lsp.adapter.CallHierarchyOutgoingCall +import org.xvm.lsp.adapter.CodeLens +import org.xvm.lsp.adapter.CodeLensCommand +import org.xvm.lsp.adapter.Position +import org.xvm.lsp.adapter.Range +import org.xvm.lsp.adapter.SymbolMoniker +import org.xvm.lsp.adapter.TypeHierarchyItem +import org.xvm.lsp.model.Location +import org.xvm.lsp.model.SymbolInfo + +/** Detached graph views. Hierarchy handles are bound to the exact source/configuration digest. */ +internal class XdkWorkspaceNavigation( + private val views: Map, + private val revision: String, + private val complete: Boolean, + private val dependencySources: Map, +) { + private val hierarchy = XdkHierarchy(views) + private val calls = XdkCalls(views) + private val sourceUris = + views.keys.mapNotNull { uri -> XdkSources.file(uri)?.let { it to uri } }.toMap() + + private fun sourceUri(uri: String): String = if (uri in views) uri else sourceUris[XdkSources.file(uri)] ?: uri + + fun symbols(query: String): List = + views + .flatMap { (uri, model) -> + model.symbols + .filter { symbol -> + symbol.declarationSource == model.sourceName && + symbol.declaration != null && + symbol.kind in + setOf( + SemanticModel.SymbolKind.TYPE, + SemanticModel.SymbolKind.METHOD, + SemanticModel.SymbolKind.PROPERTY, + SemanticModel.SymbolKind.MODULE, + SemanticModel.SymbolKind.PACKAGE, + ) && + (query.isBlank() || symbol.name.contains(query, ignoreCase = true)) + }.map { symbol -> + val range = requireNotNull(symbol.declaration) + SymbolInfo.of( + symbol.name, + when (symbol.kind) { + SemanticModel.SymbolKind.METHOD -> SymbolInfo.SymbolKind.METHOD + SemanticModel.SymbolKind.PROPERTY -> SymbolInfo.SymbolKind.PROPERTY + SemanticModel.SymbolKind.MODULE -> SymbolInfo.SymbolKind.MODULE + SemanticModel.SymbolKind.PACKAGE -> SymbolInfo.SymbolKind.PACKAGE + else -> SymbolInfo.SymbolKind.CLASS + }, + Location( + uri, + range.start.line, + range.start.column, + range.end.line, + range.end.column, + ), + ) + } + }.distinctBy { it.location } + .sortedBy { it.name } + + /** + * A reference list promises graph closure; partial navigation must not weaken rename callers. + */ + fun references( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): List { + if (!complete) return emptyList() + val target = views[sourceUri(uri)]?.symbolAt(line, column)?.id ?: return emptyList() + return views + .flatMap { (source, model) -> + model.occurrences + .filter { + it.symbol == target && + (includeDeclaration || it.role != SemanticModel.Role.DECLARATION) + }.map { + Location( + source, + it.range.start.line, + it.range.start.column, + it.range.end.line, + it.range.end.column, + ) + } + }.distinct() + .sortedWith(compareBy(Location::uri, Location::startLine, Location::startColumn)) + } + + /** One pass over occurrences, not one full reference search per declaration. */ + fun referenceLenses(uri: String): List { + if (!complete) return emptyList() + val model = views[sourceUri(uri)] ?: return emptyList() + val references = + views + .flatMap { (source, view) -> + view.occurrences + .filter { it.role != SemanticModel.Role.DECLARATION && it.symbol != null } + .map { occurrence -> + occurrence.symbol to + Location( + source, + occurrence.range.start.line, + occurrence.range.start.column, + occurrence.range.end.line, + occurrence.range.end.column, + ) + } + }.groupBy({ it.first }, { it.second }) + return model.symbols + .filter { + it.headerStart != null && it.declarationSource == model.sourceName && + it.kind in setOf(SemanticModel.SymbolKind.TYPE, SemanticModel.SymbolKind.METHOD, SemanticModel.SymbolKind.PROPERTY) + }.mapNotNull { symbol -> + val range = symbol.declaration ?: return@mapNotNull null + val locations = + references[symbol.id] + .orEmpty() + .distinct() + .sortedWith(compareBy(Location::uri, Location::startLine, Location::startColumn)) + + fun position( + line: Int, + column: Int, + ) = mapOf("line" to line, "character" to column) + CodeLens( + Range(Position(range.start.line, range.start.column), Position(range.end.line, range.end.column)), + CodeLensCommand( + "${locations.size} ${if (locations.size == 1) "reference" else "references"}", + "xtc.showReferences", + listOf( + uri, + position(range.start.line, range.start.column), + locations.map { location -> + mapOf( + "uri" to location.uri, + "range" to + mapOf( + "start" to position(location.startLine, location.startColumn), + "end" to position(location.endLine, location.endColumn), + ), + ) + }, + ), + ), + ) + }.distinctBy { it.range } + .sortedWith(compareBy({ it.range.start.line }, { it.range.start.column })) + } + + fun monikers( + uri: String, + line: Int, + column: Int, + ): List = views[sourceUri(uri)]?.monikersAt(line, column).orEmpty() + + fun definition( + uri: String, + line: Int, + column: Int, + ): Location? = + views[sourceUri(uri)]?.definitionLocationAt(line, column)?.let { + locations(listOf(it)).singleOrNull() + } + + fun typeDefinitions( + uri: String, + line: Int, + column: Int, + ): List = locations(views[sourceUri(uri)]?.typeDefinitionLocationsAt(line, column).orEmpty()) + + fun declarations( + uri: String, + line: Int, + column: Int, + ): List = locations(views[sourceUri(uri)]?.declarationLocationsAt(line, column).orEmpty()) + + fun implementations( + uri: String, + line: Int, + column: Int, + ): List = locations(views[sourceUri(uri)]?.implementationLocationsAt(line, column).orEmpty()) + + fun prepareTypes( + uri: String, + line: Int, + column: Int, + ): List = hierarchy.prepare(sourceUri(uri), line, column).map { it.copy(data = revision) } + + fun parents(item: TypeHierarchyItem): List = + current(item)?.let(hierarchy::supertypes).orEmpty().map { it.copy(data = revision) } + + fun children(item: TypeHierarchyItem): List = + current(item)?.let(hierarchy::subtypes).orEmpty().map { it.copy(data = revision) } + + fun prepareCalls( + uri: String, + line: Int, + column: Int, + ): List = calls.prepare(sourceUri(uri), line, column).map { it.copy(data = revision) } + + fun incoming(item: CallHierarchyItem): List = + current(item)?.let(calls::incoming).orEmpty().map { + it.copy(from = it.from.copy(data = revision)) + } + + fun outgoing(item: CallHierarchyItem): List = + current(item)?.let(calls::outgoing).orEmpty().map { + it.copy(to = it.to.copy(data = revision)) + } + + private fun current(item: TypeHierarchyItem): TypeHierarchyItem? = + item + .takeIf { it.data == revision } + ?.let { + hierarchy + .prepare(it.uri, it.selectionRange.start.line, it.selectionRange.start.column) + .singleOrNull { candidate -> + candidate.range == item.range && + candidate.selectionRange == item.selectionRange + } + } + + private fun current(item: CallHierarchyItem): CallHierarchyItem? = + item + .takeIf { it.data == revision } + ?.let { + calls + .prepare(it.uri, it.selectionRange.start.line, it.selectionRange.start.column) + .singleOrNull { candidate -> + candidate.range == item.range && + candidate.selectionRange == item.selectionRange + } + } + + private fun locations(locations: List): List = + locations + .mapNotNull { target -> + ( + views.entries.firstOrNull { it.value.sourceName == target.sourceName }?.key + ?: dependencySources[target.sourceName] + ?: XdkLibrarySources.sourceUri(target.sourceName) + )?.let { uri -> + val range = + Range( + Position(target.range.start.line, target.range.start.column), + Position(target.range.end.line, target.range.end.column), + ) + Location( + uri, + range.start.line, + range.start.column, + range.end.line, + range.end.column, + ) + } + }.distinct() + .sortedWith(compareBy(Location::uri, Location::startLine, Location::startColumn)) +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/IndexedSymbol.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/IndexedSymbol.kt index 0ea6c87f664..d18ed5bef64 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/IndexedSymbol.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/IndexedSymbol.kt @@ -7,14 +7,14 @@ import org.xvm.lsp.model.SymbolInfo.SymbolKind /** * Flat symbol entry optimized for cross-file lookup. * - * Unlike [SymbolInfo] which is a tree (with children for document outline), - * this is a flat entry suitable for indexing in maps and searching across files. + * Unlike [SymbolInfo] which is a tree (with children for document outline), this is a flat entry + * suitable for indexing in maps and searching across files. * - * @param name simple name (e.g., "HashMap") + * @param name simple name (e.g., "HashMap") * @param qualifiedName currently same as name; enhanced later for import resolution - * @param kind symbol kind (class, method, etc.) - * @param uri file URI where this symbol is declared - * @param location declaration range in the source file + * @param kind symbol kind (class, method, etc.) + * @param uri file URI where this symbol is declared + * @param location declaration range in the source file * @param containerName enclosing type/module name, if any */ data class IndexedSymbol( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndex.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndex.kt index d79fd9c6229..ecfac93deb2 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndex.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndex.kt @@ -9,8 +9,8 @@ import kotlin.concurrent.write /** * Workspace-wide symbol index for cross-file lookup. * - * Provides O(1) name-based lookup and fuzzy search across all indexed files. - * Thread-safe: concurrent reads are allowed during writes via [ReentrantReadWriteLock]. + * Provides O(1) name-based lookup and fuzzy search across all indexed files. Thread-safe: + * concurrent reads are allowed during writes via [ReentrantReadWriteLock]. * * ## Search algorithm (4-tier priority) * 1. Exact match (case-insensitive) @@ -36,14 +36,14 @@ class WorkspaceIndex { private val lock = ReentrantReadWriteLock() /** Total number of indexed symbols across all files. */ - val symbolCount: Int get() = lock.read { byUri.values.sumOf { it.size } } + val symbolCount: Int + get() = lock.read { byUri.values.sumOf { it.size } } /** Number of indexed files. */ - val fileCount: Int get() = byUri.size + val fileCount: Int + get() = byUri.size - /** - * Add symbols for a file. Replaces any previously indexed symbols for that URI. - */ + /** Add symbols for a file. Replaces any previously indexed symbols for that URI. */ fun addSymbols( uri: String, symbols: List, @@ -66,9 +66,7 @@ class WorkspaceIndex { } } - /** - * Remove all symbols for a file URI. - */ + /** Remove all symbols for a file URI. */ fun removeSymbolsForUri(uri: String) { lock.write { val count = byUri[uri]?.size ?: 0 @@ -89,9 +87,7 @@ class WorkspaceIndex { } } - /** - * Exact name lookup (case-insensitive). Returns all symbols with the given name. - */ + /** Exact name lookup (case-insensitive). Returns all symbols with the given name. */ fun findByName(name: String): List = lock.read { val results = byName[name.lowercase()]?.toList() ?: emptyList() @@ -122,7 +118,13 @@ class WorkspaceIndex { } val lowerQuery = query.lowercase() - logger.info("search: query='{}', limit={}, index has {} names across {} files", query, limit, byName.size, byUri.size) + logger.info( + "search: query='{}', limit={}, index has {} names across {} files", + query, + limit, + byName.size, + byUri.size, + ) return lock.read { val exact = mutableListOf() @@ -141,7 +143,8 @@ class WorkspaceIndex { } else -> { - // Check representative symbol for camelCase/subsequence (all have same name) + // Check representative symbol for camelCase/subsequence (all have same + // name) val name = symbols.firstOrNull()?.name ?: continue if (matchesCamelCase(query, name)) { camelCase.addAll(symbols) @@ -173,9 +176,7 @@ class WorkspaceIndex { } } - /** - * Clear all indexed data. - */ + /** Clear all indexed data. */ fun clear() { lock.write { byName.clear() @@ -187,8 +188,8 @@ class WorkspaceIndex { companion object { /** - * Check if query matches name via CamelCase initials. - * E.g., "HSM" matches "HashMap", "CCE" matches "ClassCastException". + * Check if query matches name via CamelCase initials. E.g., "HSM" matches "HashMap", "CCE" + * matches "ClassCastException". */ internal fun matchesCamelCase( query: String, @@ -215,8 +216,8 @@ class WorkspaceIndex { } /** - * Check if query is a subsequence of name (both lowercase). - * E.g., "hmap" is a subsequence of "hashmap". + * Check if query is a subsequence of name (both lowercase). E.g., "hmap" is a subsequence + * of "hashmap". */ internal fun matchesSubsequence( query: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt index db08da3252c..b56c0fdae35 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt @@ -6,16 +6,18 @@ import org.xvm.lsp.model.SymbolInfo import org.xvm.lsp.treesitter.XtcParser import org.xvm.lsp.treesitter.XtcQueryEngine import java.io.Closeable +import java.net.URI import java.nio.file.FileVisitResult import java.nio.file.Files import java.nio.file.Path import java.nio.file.SimpleFileVisitor import java.nio.file.attribute.BasicFileAttributes +import java.util.concurrent.CancellationException import java.util.concurrent.CompletableFuture import java.util.concurrent.ExecutorService import java.util.concurrent.Executors import java.util.concurrent.TimeUnit -import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicBoolean import kotlin.io.path.extension import kotlin.io.path.readText import kotlin.time.measureTimedValue @@ -23,14 +25,14 @@ import kotlin.time.measureTimedValue /** * Background workspace scanner that builds and maintains the [WorkspaceIndex]. * - * Uses a dedicated thread pool (not ForkJoinPool) to avoid starving LSP message handlers. - * Converts tree-sitter parsed [SymbolInfo] to flat [IndexedSymbol] entries for indexing. + * Uses a dedicated thread pool (not ForkJoinPool) to avoid starving LSP message handlers. Converts + * tree-sitter parsed [SymbolInfo] to flat [IndexedSymbol] entries for indexing. * * **Thread safety:** This indexer creates its own dedicated [XtcParser] and [XtcQueryEngine] - * instances, separate from the adapter's instances. This avoids race conditions since - * tree-sitter's native `Parser` is NOT thread-safe and the adapter's parser is used - * concurrently on the LSP message thread. All calls to the indexer's parser and query - * engine are serialized via [parseLock] to protect against concurrent indexer tasks. + * instances, separate from the adapter's instances. This avoids race conditions since tree-sitter's + * native `Parser` is NOT thread-safe and the adapter's parser is used concurrently on the LSP + * message thread. All calls to the indexer's parser and query engine are serialized via [parseLock] + * to protect against concurrent indexer tasks. * * @param index the workspace index to populate * @param language the tree-sitter language, used to create a dedicated parser and query engine @@ -39,6 +41,9 @@ import kotlin.time.measureTimedValue class WorkspaceIndexer( private val index: WorkspaceIndex, language: Language, + private val readFile: (Path) -> String? = { path -> + if (Files.isRegularFile(path)) path.readText() else null + }, ) : Closeable { private val logger = LoggerFactory.getLogger(WorkspaceIndexer::class.java) @@ -50,8 +55,18 @@ class WorkspaceIndexer( /** Serializes access to the non-thread-safe tree-sitter parser and query engine. */ private val parseLock = Any() + private val closed = AtomicBoolean() - private val threadPoolSize = minOf(Runtime.getRuntime().availableProcessors(), 4).coerceAtLeast(2) + // Owned by parseLock. Identity distinguishes reads across edits, closes and disk events; + // a later disk read must also supersede an earlier read of the same closed file. + private class Revision( + val open: Boolean, + ) + + private val revisions = mutableMapOf() + + private val threadPoolSize = + minOf(Runtime.getRuntime().availableProcessors(), 4).coerceAtLeast(2) private val threadPool: ExecutorService = Executors.newFixedThreadPool(threadPoolSize).also { @@ -63,7 +78,7 @@ class WorkspaceIndexer( } /** - * Scan all `*.x` files in the given workspace folders in parallel. + * Scan all `*.x` files in the given workspace folders on a background worker. * * @param folders list of workspace folder paths (file system paths, not URIs) * @param progressReporter optional callback for progress reporting: (message, percentComplete) @@ -72,85 +87,139 @@ class WorkspaceIndexer( fun scanWorkspace( folders: List, progressReporter: ((String, Int) -> Unit)? = null, - ): CompletableFuture = - CompletableFuture.supplyAsync({ - logger.info("starting workspace scan: {} folders: {}", folders.size, folders) - val (_, elapsed) = - measureTimedValue { - val files = collectXtcFiles(folders) - logger.info("found {} .x files to index", files.size) - - if (files.isEmpty()) { - progressReporter?.invoke("No .x files found", 100) - return@supplyAsync - } - - val indexed = AtomicInteger(0) - val total = files.size - - // Process files in parallel batches using the dedicated thread pool - val futures = - files.map { file -> - CompletableFuture.runAsync({ + ): CompletableFuture { + val canceled = AtomicBoolean() + return CompletableFuture + .supplyAsync( + { + logger.info("starting workspace scan: {} folders: {}", folders.size, folders) + val (_, elapsed) = + measureTimedValue { + val files = collectXtcFiles(folders) + logger.info("found {} .x files to index", files.size) + + if (files.isEmpty()) { + progressReporter?.invoke("No .x files found", 100) + return@supplyAsync + } + + // Parsing is serialized by parseLock. Do not enqueue children and join + // them on this same bounded pool: concurrent scans can starve all + // workers, + // and shutdownNow leaves queued CompletableFutures uncompleted. + files.forEachIndexed { index, file -> + if (closed.get() || canceled.get()) { + throw CancellationException("Workspace indexing retired") + } indexFile(file) - val done = indexed.incrementAndGet() - if (done % 50 == 0 || done == total) { - val percent = (done * 100) / total - progressReporter?.invoke("Indexing: $done/$total files", percent) - logger.info("progress: {}/{} files ({}%)", done, total, percent) + val done = index + 1 + if (done % 50 == 0 || done == files.size) { + progressReporter?.invoke( + "Indexing: $done/${files.size} files", + done * 100 / files.size, + ) } - }, threadPool) + } + progressReporter?.invoke("Indexing complete", 100) } - // Wait for all to complete - CompletableFuture.allOf(*futures.toTypedArray()).join() - progressReporter?.invoke("Indexing complete", 100) - } - - logger.info( - "workspace scan complete: {} symbols in {} files ({})", - index.symbolCount, - index.fileCount, - elapsed, - ) - }, threadPool) + logger.info( + "workspace scan complete: {} symbols in {} files ({})", + index.symbolCount, + index.fileCount, + elapsed, + ) + }, + threadPool, + ).also { result -> + result.whenComplete { _, _ -> if (result.isCancelled) canceled.set(true) } + } + } /** - * Re-index a single file. Called after compile() updates a file. - * Removes old symbols and re-indexes from the provided content. + * Re-index a single file. Called after compile() updates a file. Removes old symbols and + * re-indexes from the provided content. */ fun reindexFile( - uri: String, + sourceUri: String, content: String, ) { - val symbols = parseAndExtractSymbols(uri, content) - index.addSymbols(uri, symbols) + val uri = canonicalUri(sourceUri) + val symbols = + synchronized(parseLock) { + if (closed.get()) return + revisions[uri] = Revision(open = true) + parseAndExtractSymbols(uri, content).also { index.addSymbols(uri, it) } + } logger.info("reindexed {}: {} symbols", uri.substringAfterLast('/'), symbols.size) } - /** - * Remove all symbols for a file (e.g., when it's deleted). - */ - fun removeFile(uri: String) { - index.removeSymbolsForUri(uri) + /** Remove all symbols for a file (e.g., when it's deleted). */ + fun removeFile(sourceUri: String) { + val uri = canonicalUri(sourceUri) + synchronized(parseLock) { + if (closed.get()) return + revisions[uri] = Revision(open = false) + index.removeSymbolsForUri(uri) + } logger.info("removed symbols for {}", uri.substringAfterLast('/')) } + /** Disk notifications and scans never replace an open editor buffer. */ + fun refreshFile(uri: String) { + val source = URI.create(uri) + if (source.scheme == "file") indexFile(Path.of(source)) + } + + /** Closing an unsaved buffer restores the current disk file, or removes an absent file. */ + fun closeDocument(sourceUri: String) { + val uri = canonicalUri(sourceUri) + synchronized(parseLock) { + if (closed.get()) return + revisions[uri] = Revision(open = false) + index.removeSymbolsForUri(uri) + } + refreshFile(uri) + } + + private fun canonicalUri(uri: String): String = + URI.create(uri).let { + if (it.scheme == "file") { + Path + .of(it) + .normalize() + .toUri() + .toString() + } else { + uri + } + } + private fun indexFile(path: Path) { - val uri = path.toUri().toString() + val uri = path.normalize().toUri().toString() + val revision = + synchronized(parseLock) { + if (closed.get() || revisions[uri]?.open == true) return + Revision(open = false).also { revisions[uri] = it } + } try { - val content = path.readText() - val symbols = parseAndExtractSymbols(uri, content) - index.addSymbols(uri, symbols) - logger.info("indexed {}: {} symbols ({} bytes)", path.fileName, symbols.size, content.length) + val content = readFile(path) + synchronized(parseLock) { + if (closed.get() || revisions[uri] !== revision) return + if (content == null) { + index.removeSymbolsForUri(uri) + } else { + index.addSymbols(uri, parseAndExtractSymbols(uri, content)) + } + } } catch (e: Exception) { logger.warn("failed to index {}: {}", path, e.message) } } /** - * Parse a file and extract flat [IndexedSymbol] entries. - * Flattens the [SymbolInfo] tree so nested declarations are also indexed. + * Parse a file and extract flat [IndexedSymbol] entries. Flattens the [SymbolInfo] tree so + * nested declarations are also indexed. * * Synchronized on [parseLock] because tree-sitter's Parser is not thread-safe. */ @@ -160,11 +229,9 @@ class WorkspaceIndexer( ): List = synchronized(parseLock) { val tree = parser.parse(content) - try { + tree.use { tree -> val symbols = queryEngine.findAllDeclarations(tree, uri) flattenSymbols(symbols, uri, null) - } finally { - tree.close() } } @@ -218,14 +285,24 @@ class WorkspaceIndexer( } override fun close() { - logger.info("shutting down (index has {} symbols in {} files)", index.symbolCount, index.fileCount) + if (!closed.compareAndSet(false, true)) return + logger.info( + "shutting down (index has {} symbols in {} files)", + index.symbolCount, + index.fileCount, + ) threadPool.shutdown() if (!threadPool.awaitTermination(5, TimeUnit.SECONDS)) { logger.warn("thread pool did not terminate in 5s, forcing shutdown") threadPool.shutdownNow() } - queryEngine.close() - parser.close() + // A timed-out worker may still be inside native parsing. Never free either handle + // until that critical section finishes; queued scans see closed before entering it. + synchronized(parseLock) { + revisions.clear() + queryEngine.close() + parser.close() + } logger.info("closed") } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScanner.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScanner.kt index 8a9ba7df711..9cb76ef67ce 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScanner.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScanner.kt @@ -1,8 +1,6 @@ package org.xvm.lsp.lexer -/** - * Scanner state as a sealed hierarchy - illegal states are unrepresentable. - */ +/** Scanner state as a sealed hierarchy - illegal states are unrepresentable. */ sealed interface ScanState { val pos: Int @@ -25,9 +23,7 @@ sealed interface ScanState { ) : ScanState } -/** - * A single step result: optional token + next state. - */ +/** A single step result: optional token + next state. */ private data class Step( val token: TemplateScannerToken?, val next: ScanState, @@ -261,14 +257,21 @@ class TemplateScanner { skipComment(source, state) } - '\n', '\r' -> { + '\n', + '\r', + -> { if (!state.multiline) { Step( TemplateScannerToken.error(pos, pos, "Newline in template expression"), ScanState.Normal(pos), ) } else { - val skip = if (ch == '\r' && pos + 1 < source.length && source[pos + 1] == '\n') 2 else 1 + val skip = + if (ch == '\r' && pos + 1 < source.length && source[pos + 1] == '\n') { + 2 + } else { + 1 + } Step(null, state.copy(pos = pos + skip)) } } @@ -325,9 +328,9 @@ class TemplateScanner { '/' -> { // Single-line comment - find end of line val end = - (pos + 2 until source.length) - .firstOrNull { source[it] == '\n' || source[it] == '\r' } - ?: source.length + (pos + 2 until source.length).firstOrNull { + source[it] == '\n' || source[it] == '\r' + } ?: source.length Step(null, state.copy(pos = end)) } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScannerToken.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScannerToken.kt index 72682624fec..8b056bf6c61 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScannerToken.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/lexer/TemplateScannerToken.kt @@ -42,44 +42,45 @@ data class TemplateScannerToken( val endOffset: Int, val value: String? = null, ) { - val length: Int get() = endOffset - startOffset + val length: Int + get() = endOffset - startOffset companion object { fun templateStart( start: Int, end: Int, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_START, start, end) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_START, start, end) fun templateMultilineStart( start: Int, end: Int, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_MULTILINE_START, start, end) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_MULTILINE_START, start, end) fun content( start: Int, end: Int, value: String, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_CONTENT, start, end, value) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_CONTENT, start, end, value) fun exprStart( start: Int, end: Int, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_EXPR_START, start, end) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_EXPR_START, start, end) fun exprEnd( start: Int, end: Int, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_EXPR_END, start, end) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_EXPR_END, start, end) fun templateEnd( start: Int, end: Int, - ) = TemplateScannerToken(TemplateTokenType.TEMPLATE_END, start, end) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.TEMPLATE_END, start, end) fun error( start: Int, end: Int, message: String, - ) = TemplateScannerToken(TemplateTokenType.ERROR, start, end, message) + ): TemplateScannerToken = TemplateScannerToken(TemplateTokenType.ERROR, start, end, message) } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/CompilationResult.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/CompilationResult.kt index 17eaaabe2f5..bd087ade0d3 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/CompilationResult.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/CompilationResult.kt @@ -1,14 +1,15 @@ package org.xvm.lsp.model /** - * Immutable result of compiling an XTC source file. - * This is what the adapter layer produces from the compiler. + * Immutable result of compiling an XTC source file. This is what the adapter layer produces from + * the compiler. */ data class CompilationResult( val uri: String, val diagnostics: List, val symbols: List, val success: Boolean, + val documentUris: Set = setOf(uri), ) { companion object { fun success( @@ -20,9 +21,16 @@ data class CompilationResult( uri: String, diagnostics: List, symbols: List, + documentUris: Set = setOf(uri), ): CompilationResult { val hasErrors = diagnostics.any { it.severity == Diagnostic.Severity.ERROR } - return CompilationResult(uri, diagnostics.toList(), symbols.toList(), !hasErrors) + return CompilationResult( + uri, + diagnostics.toList(), + symbols.toList(), + !hasErrors, + documentUris.toSet(), + ) } fun failure( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Diagnostic.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Diagnostic.kt index 37950972302..a8975558233 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Diagnostic.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Diagnostic.kt @@ -1,8 +1,6 @@ package org.xvm.lsp.model -/** - * Immutable diagnostic (error, warning, hint). - */ +/** Immutable diagnostic (error, warning, hint). */ data class Diagnostic( val location: Location, val severity: Severity, @@ -24,16 +22,16 @@ data class Diagnostic( fun error( location: Location, message: String, - ): Diagnostic = Diagnostic(location, Severity.ERROR, message, null, "xtc") + ): Diagnostic = Diagnostic(location, Severity.ERROR, message, source = "xtc") fun warning( location: Location, message: String, - ): Diagnostic = Diagnostic(location, Severity.WARNING, message, null, "xtc") + ): Diagnostic = Diagnostic(location, Severity.WARNING, message, source = "xtc") fun info( location: Location, message: String, - ): Diagnostic = Diagnostic(location, Severity.INFORMATION, message, null, "xtc") + ): Diagnostic = Diagnostic(location, Severity.INFORMATION, message, source = "xtc") } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Location.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Location.kt index 4533394ef16..20a6ec201b2 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Location.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Location.kt @@ -1,8 +1,6 @@ package org.xvm.lsp.model -/** - * Immutable source location. - */ +/** Immutable source location. */ data class Location( val uri: String, val startLine: Int, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Lsp4jConversions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Lsp4jConversions.kt index 82c76ac613e..18cd935d479 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Lsp4jConversions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/Lsp4jConversions.kt @@ -1,6 +1,7 @@ package org.xvm.lsp.model import org.eclipse.lsp4j.CodeActionKind +import org.eclipse.lsp4j.DiagnosticRelatedInformation import org.eclipse.lsp4j.DiagnosticSeverity import org.eclipse.lsp4j.DocumentHighlightKind import org.eclipse.lsp4j.FileChangeType @@ -87,12 +88,20 @@ fun Diagnostic.Companion.fromLsp( source = lspDiagnostic.source, ) -/** Convert this Diagnostic to an LSP4J Diagnostic. */ -fun Diagnostic.toLsp(): org.eclipse.lsp4j.Diagnostic { +/** Keep foreign-source locations as related information when publishing for another document. */ +fun Diagnostic.toLsp(publicationUri: String = location.uri): org.eclipse.lsp4j.Diagnostic { val result = org.eclipse.lsp4j.Diagnostic() - result.range = location.toRange() + if (location.uri == publicationUri) { + result.range = location.toRange() + } else { + result.range = Range(Position(0, 0), Position(0, 0)) + result.relatedInformation = listOf(DiagnosticRelatedInformation(location.toLsp(), message)) + } result.severity = severity.toLsp() - result.message = Either.forLeft(message) + result.message = + Either.forLeft( + if (location.uri == publicationUri) message else "In ${location.uri}: $message", + ) result.source = source if (code != null) { result.code = Either.forLeft(code) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/SymbolInfo.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/SymbolInfo.kt index 8ee8e782042..abd5645fb2f 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/SymbolInfo.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/model/SymbolInfo.kt @@ -1,8 +1,6 @@ package org.xvm.lsp.model -/** - * Immutable symbol information extracted from compiled XTC. - */ +/** Immutable symbol information extracted from compiled XTC. */ data class SymbolInfo( val name: String, val qualifiedName: String, @@ -41,7 +39,8 @@ data class SymbolInfo( fun withChildren(newChildren: List): SymbolInfo = copy(children = newChildren) - fun withDocumentation(doc: String?): SymbolInfo = copy(documentation = doc) + // TODO: Do we need these? + // fun withDocumentation(doc: String?): SymbolInfo = copy(documentation = doc) - fun withTypeSignature(sig: String?): SymbolInfo = copy(typeSignature = sig) + // fun withTypeSignature(sig: String?): SymbolInfo = copy(typeSignature = sig) } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientPresentation.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientPresentation.kt new file mode 100644 index 00000000000..c2cc8c71e83 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientPresentation.kt @@ -0,0 +1,153 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.CompletionItemKind +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InsertTextMode +import org.eclipse.lsp4j.MarkupContent +import org.eclipse.lsp4j.MarkupKind +import org.eclipse.lsp4j.SymbolKind +import java.nio.file.Path + +/** Immutable wire-format choices, independent of compiler semantics and editor preferences. */ +internal data class ClientPresentation( + val markdownHover: Boolean = false, + val hierarchicalSymbols: Boolean = false, + val documentKinds: Set = legacyKinds, + val workspaceKinds: Set = legacyKinds, + val diagnosticVersions: Boolean = false, + val diagnosticRelatedInformation: Boolean = false, + val workspaceConfiguration: Boolean = false, + val completionKinds: Set = legacyCompletionKinds, + val completionSnippets: Boolean = false, + val inlineCompletion: Boolean = false, + val completionAsIs: Boolean = false, + val completionAdjustIndentation: Boolean = false, + val actionLiterals: Boolean = false, + val preferredActions: Boolean = false, + val applyEdit: Boolean = false, + val linkTooltips: Boolean = false, + val signatureActiveParameters: Boolean = false, + val pullDiagnosticRelatedInformation: Boolean = false, +) { + fun hover(markdown: String): MarkupContent = + if (markdownHover) { + MarkupContent(MarkupKind.MARKDOWN, markdown) + } else { + MarkupContent(MarkupKind.PLAINTEXT, plainText(markdown)) + } + + fun symbolKind( + kind: SymbolKind, + workspace: Boolean = false, + ): SymbolKind { + val supported = if (workspace) workspaceKinds else documentKinds + return kind.takeIf { it in supported } + ?: SymbolKind.Variable.takeIf { it in supported } + ?: supported.minByOrNull { it.value } + ?: SymbolKind.Variable + } + + fun completionKind(kind: CompletionItemKind): CompletionItemKind = + kind.takeIf { it in completionKinds } + ?: CompletionItemKind.Text.takeIf { it in completionKinds } + ?: completionKinds.minByOrNull { it.value } + ?: CompletionItemKind.Text + + val codeActions: Boolean + get() = actionLiterals || applyEdit + + /** Templates contain source indentation. Avoid a second indent by clients that always adjust it. */ + fun completionTemplate(text: String): String { + if (completionAsIs || !completionAdjustIndentation) return text + val newline = lineBreaks.find(text)?.value ?: "\n" + val lines = text.split(newline) + val indent = lines.drop(1).filter(String::isNotBlank).minOfOrNull { it.takeWhile(Char::isWhitespace).length } ?: 0 + return (lines.take(1) + lines.drop(1).map { it.drop(indent.coerceAtMost(it.takeWhile(Char::isWhitespace).length)) }) + .joinToString(newline) + } + + companion object { + const val APPLY_CODE_ACTION = "xtc.applyCodeAction" + private val lineBreaks = Regex("\\r\\n|\\r|\\n") + private val legacyCompletionKinds = + CompletionItemKind.entries + .filter { it.value <= CompletionItemKind.Reference.value } + .toSet() + private val legacyKinds = SymbolKind.entries.filter { it.value <= 18 }.toSet() + + fun read(params: InitializeParams): ClientPresentation { + val text = params.capabilities?.textDocument + return ClientPresentation( + markdownHover = + text?.hover?.contentFormat?.firstOrNull { + it == MarkupKind.MARKDOWN || it == MarkupKind.PLAINTEXT + } == MarkupKind.MARKDOWN, + hierarchicalSymbols = + text?.documentSymbol?.hierarchicalDocumentSymbolSupport == true, + documentKinds = + text + ?.documentSymbol + ?.symbolKind + ?.valueSet + ?.toSet() ?: legacyKinds, + workspaceKinds = + params.capabilities + ?.workspace + ?.symbol + ?.symbolKind + ?.valueSet + ?.toSet() + ?: legacyKinds, + diagnosticVersions = text?.publishDiagnostics?.versionSupport == true, + diagnosticRelatedInformation = text?.publishDiagnostics?.relatedInformation == true, + workspaceConfiguration = params.capabilities?.workspace?.configuration == true, + completionKinds = + text + ?.completion + ?.completionItemKind + ?.valueSet + ?.toSet() + ?: legacyCompletionKinds, + actionLiterals = text?.codeAction?.codeActionLiteralSupport != null, + completionSnippets = text?.completion?.completionItem?.snippetSupport == true, + inlineCompletion = text?.inlineCompletion != null, + completionAsIs = + text + ?.completion + ?.completionItem + ?.insertTextModeSupport + ?.valueSet + ?.contains(InsertTextMode.AsIs) == true, + completionAdjustIndentation = text?.completion?.insertTextMode == InsertTextMode.AdjustIndentation, + preferredActions = text?.codeAction?.isPreferredSupport == true, + applyEdit = params.capabilities?.workspace?.applyEdit == true, + linkTooltips = text?.documentLink?.tooltipSupport == true, + signatureActiveParameters = + text?.signatureHelp?.signatureInformation?.activeParameterSupport == true, + pullDiagnosticRelatedInformation = text?.diagnostic?.relatedInformation == true, + ) + } + + /** + * The empty kind selects everything; unknown client kinds are not invented or broadened. + */ + fun matchesActionKind( + kind: String, + only: List?, + ): Boolean = only == null || only.any { it.isEmpty() || kind == it || kind.startsWith("$it.") } + + /** Workspace folders take precedence, including an explicitly empty list. */ + fun workspaceUris(params: InitializeParams): List = + params.workspaceFolders?.map { it.uri } + ?: params.rootUri?.let(::listOf) + ?: params.rootPath?.let { listOf(Path.of(it).toUri().toString()) } + ?: emptyList() + + // Adapter hover uses fenced declarations, inline code and headings. Preserve their text. + private fun plainText(markdown: String): String = + markdown + .lineSequence() + .filterNot { it.trimStart().startsWith("```") } + .joinToString("\n") { it.replace(Regex("^#{1,6}\\s+"), "").replace("`", "") } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt new file mode 100644 index 00000000000..2692dd9c4e9 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt @@ -0,0 +1,88 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.services.LanguageClient +import org.slf4j.LoggerFactory +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +/** One outstanding refresh per provider; changes during a reply produce one follow-up refresh. */ +internal class ClientRefresh( + private val client: () -> LanguageClient?, +) : AutoCloseable { + enum class Feature { + DIAGNOSTICS, + TOKENS, + INLAYS, + LENSES, + FOLDING, + } + + private val enabled = AtomicReference>(emptySet()) + private val ready = AtomicBoolean() + private val closed = AtomicBoolean() + private val slots = Feature.entries.associateWith { Slot(it) } + + fun configure(features: Set) { + enabled.set(features.toSet()) + } + + fun initialized() { + ready.set(true) + } + + fun request(vararg features: Feature) { + if (!ready.get() || closed.get()) return + features.filter { it in enabled.get() }.forEach { slots.getValue(it).request() } + } + + private inner class Slot( + private val feature: Feature, + ) { + private val dirty = AtomicBoolean() + private val running = AtomicBoolean() + + fun request() { + dirty.set(true) + if (!running.compareAndSet(false, true)) return + // Scheduling, not transport, is the only operation performed under compiler locks. + CompletableFuture.runAsync { + dirty.set(false) + val work = + if (closed.get()) { + CompletableFuture.completedFuture(null) + } else { + runCatching { + client()?.let { + when (feature) { + Feature.DIAGNOSTICS -> it.refreshDiagnostics() + Feature.TOKENS -> it.refreshSemanticTokens() + Feature.INLAYS -> it.refreshInlayHints() + Feature.LENSES -> it.refreshCodeLenses() + Feature.FOLDING -> it.refreshFoldingRanges() + } + } ?: CompletableFuture.completedFuture(null) + }.getOrElse { CompletableFuture.failedFuture(it) } + } + work.orTimeout(10, SECONDS).whenComplete { _, failure -> + if (failure != null && !closed.get()) { + logger.debug("{} refresh failed", feature, failure) + } + running.set(false) + // Either this callback or a concurrent request wins the next CAS. Neither + // clears dirty until its worker starts, so a change cannot disappear. + if (dirty.get() && !closed.get()) request() + } + } + } + } + + override fun close() { + closed.set(true) + } + + companion object { + private val logger = LoggerFactory.getLogger(ClientRefresh::class.java) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt new file mode 100644 index 00000000000..0223678622d --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt @@ -0,0 +1,60 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.LogTraceParams +import org.eclipse.lsp4j.services.LanguageClient +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.CompletableFuture +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +/** + * Runtime client trace preferences affect protocol timing only, never expose source or payloads. + */ +internal class ClientTrace( + private val client: () -> LanguageClient?, +) : AutoCloseable { + private enum class Level( + val value: String, + ) { + OFF("off"), + MESSAGES("messages"), + VERBOSE("verbose"), + } + + private val level = AtomicReference(Level.OFF) + private val closed = AtomicBoolean() + + fun configure(value: String?) { + Level.entries.firstOrNull { it.value == value }?.let(level::set) + } + + fun completed( + span: ExecutionTrace.Span, + errorCode: Int?, + ) { + if (closed.get() || level.get() == Level.OFF) return + val elapsed = ExecutionTrace.elapsed(span.created) + CompletableFuture.runAsync { + val current = level.get() + if (!closed.get() && current != Level.OFF) { + runCatching { + client() + ?.logTrace( + LogTraceParams( + "${span.operation}: ${if (errorCode == null) "replied" else "error $errorCode"} in ${elapsed.toLong()} ms", + if (current == Level.VERBOSE) { + "request=${span.id}; parent=${span.parent}; boundary=response-written" + } else { + null + }, + ), + ) + } + } + } + } + + override fun close() { + closed.set(true) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt new file mode 100644 index 00000000000..75ea8efffc7 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt @@ -0,0 +1,14 @@ +package org.xvm.lsp.server + +import com.google.gson.GsonBuilder + +internal object CodeLensSettings { + fun references(raw: Any?): Boolean { + val value = GsonBuilder().serializeNulls().create().toJsonTree(raw) + if (value.isJsonNull) return true + require(value.isJsonObject) { "Ecstasy codeLens settings must be an object" } + val references = value.asJsonObject["references"] ?: return true + require(references.isJsonPrimitive && references.asJsonPrimitive.isBoolean) { "codeLens.references must be a boolean" } + return references.asBoolean + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerConfiguration.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerConfiguration.kt new file mode 100644 index 00000000000..34bff15f13c --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerConfiguration.kt @@ -0,0 +1,126 @@ +package org.xvm.lsp.server + +import com.google.gson.GsonBuilder +import com.google.gson.JsonElement +import org.xvm.lsp.adapter.xdk.XdkBuildModel +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI + +/** + * Strict editor configuration; absent values preserve the host graph and an empty list clears it. + */ +internal object CompilerConfiguration { + const val SECTION = "xtc.compiler" + const val INITIALIZATION_KEY = "xtcCompiler" + private val gson = GsonBuilder().serializeNulls().create() + + fun initial(options: Any?): JsonElement? = objectValue(options)?.get(INITIALIZATION_KEY) + + fun changed(settings: Any?): JsonElement? = objectValue(objectValue(settings)?.get("xtc"))?.get("compiler") + + /** Explicit presentation notifications do not request or replace the compiler graph. */ + fun presentationOnly(settings: Any?): Boolean = + objectValue(objectValue(settings)?.get("xtc"))?.keySet()?.let { sections -> + sections.isNotEmpty() && + sections.all { it in setOf("formatting", "presentation", "languageService") } + } == true + + fun automatic(raw: Any?): Boolean = objectValue(raw)?.get("sourceModules")?.isJsonNull == true + + fun buildModel( + raw: Any?, + includeExplicit: Boolean = false, + ): XdkBuildModel? { + val config = objectValue(raw) ?: return null + if (!includeExplicit && config["sourceModules"]?.let { !it.isJsonNull } == true) return null + val models = config["buildModels"]?.takeUnless { it.isJsonNull } ?: return null + require(models.isJsonArray) { "buildModels must be an array" } + return models.asJsonArray + .takeIf { !it.isEmpty } + ?.let { + XdkBuildModel.read( + it.map { model -> + require(model.isJsonObject) { "Build models must be objects" } + model.asJsonObject + }, + ) + } + } + + fun modules( + raw: Any?, + workspaceUris: List, + ): List? { + val config = objectValue(raw) ?: return null + val entries = config.get("sourceModules") ?: return null + if (entries.isJsonNull) return null + require(entries.isJsonArray) { "sourceModules must be an array" } + return entries.asJsonArray.map { entry -> + require(entry.isJsonObject) { "Each source module must be an object" } + val module = entry.asJsonObject + val name = string(module.get("name"), "name") + + fun resolve(value: String): String { + val path = URI.create(value) + val resolved = + if (path.isAbsolute) { + path + } else { + require(workspaceUris.size == 1) { + "Relative source/resource URIs require exactly one workspace folder; use file URIs otherwise" + } + URI.create(workspaceUris.single().trimEnd('/') + "/").resolve(path) + } + require(resolved.scheme == "file") { + "Source and resource roots must use file URIs" + } + return resolved.toString() + } + val uri = resolve(string(module.get("uri"), "uri")) + val resources = + module + .get("resourceRoots") + ?.takeUnless { it.isJsonNull } + ?.let { values -> + require(values.isJsonArray) { + "resourceRoots must be an array of file URIs" + } + values.asJsonArray.map { resolve(string(it, "resource root")) } + } + val dependencies = + module + .get("dependencies") + ?.let { values -> + require(values.isJsonArray) { + "dependencies must be an array of module names" + } + values.asJsonArray.map { string(it, "dependency") }.toSet() + }.orEmpty() + XdkSourceModule(name, uri, dependencies, resources) + } + } + + private fun objectValue(raw: Any?) = + gson + .toJsonTree(raw) + ?.takeUnless { it.isJsonNull } + ?.let { + require(it.isJsonObject) { "Compiler configuration must be an object" } + it.asJsonObject + } + + private fun string( + value: JsonElement?, + field: String, + ): String { + require( + value != null && + value.isJsonPrimitive && + value.asJsonPrimitive.isString && + value.asString.isNotBlank(), + ) { + "$field must be a non-blank string" + } + return value.asString + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerLibraries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerLibraries.kt new file mode 100644 index 00000000000..b03e8e77436 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CompilerLibraries.kt @@ -0,0 +1,143 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import com.google.gson.JsonElement +import com.google.gson.JsonObject +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkLibraries +import java.io.File +import java.io.IOException +import java.net.URI + +/** Ordered external binaries and navigation-only sources; bundled modules cannot be replaced. */ +internal data class CompilerLibraries( + val modulePath: List?, + val sourceAttachments: Map>, +) { + fun resolve(inherited: List): List = + try { + resolveInputs(inherited) + } catch (failure: IOException) { + throw IllegalArgumentException("Cannot read library inputs: ${failure.message}", failure) + } + + private fun resolveInputs(inherited: List): List { + val binaries = + modulePath + ?.flatMap { root -> + require(root.exists()) { + "Library path does not exist: $root" + } + if (root.isDirectory) { + root + .listFiles() + .orEmpty() + .filter { it.isFile && it.extension == "xtc" } + .sortedBy(File::getName) + } else { + require(root.isFile && root.extension == "xtc") { + "Library path must be a directory or .xtc file: $root" + } + listOf(root) + } + }?.map { file -> + try { + XdkDependency.fromBinary(file.readBytes()) + } catch ( + failure: Exception, + ) { + throw IllegalArgumentException("Cannot read library $file: ${failure.message}", failure) + } + }?.also { values -> + require(values.none { it.module in XdkLibraries.moduleNames }) { + "Library overrides cannot replace the bundled XDK" + } + }?.distinctBy { it.module } ?: inherited + require(sourceAttachments.keys.all { name -> binaries.any { it.module == name } }) { + "Every source attachment must name an effective external binary module" + } + return binaries.map { binary -> sourceAttachments[binary.module]?.let(binary::withSources) ?: binary } + } + + companion object { + fun read( + raw: Any?, + workspaceUris: List, + ): CompilerLibraries? { + val config = Gson().toJsonTree(raw)?.takeIf { it.isJsonObject }?.asJsonObject ?: return null + val value = config["libraries"]?.takeUnless { it.isJsonNull } ?: return null + require(value.isJsonObject) { + "libraries must be an object" + } + val settings = value.asJsonObject + require(settings.keySet().all { it in setOf("modulePath", "sourceAttachments") }) { + "Unknown library setting" + } + + fun paths(values: JsonElement): List { + require(values.isJsonArray) { + "Library paths must be an array" + } + val paths = + values.asJsonArray.map { element -> + require( + element.isJsonPrimitive && element.asJsonPrimitive.isString && + element.asString.isNotBlank(), + ) { + "Library paths must be non-blank strings" + } + val path = URI.create(element.asString) + val resolved = + if (path.isAbsolute) { + path + } else { + require(workspaceUris.size == 1) { + "Relative library paths require exactly one workspace folder" + } + URI(workspaceUris.single().trimEnd('/') + "/").resolve(path) + } + require(resolved.scheme == "file" && resolved.query == null && resolved.fragment == null) { + "Library paths must use local file URIs" + } + File(resolved).canonicalFile + } + require(paths.distinct().size == paths.size) { + "Duplicate library path" + } + return paths + } + val modules = settings["modulePath"]?.takeUnless { it.isJsonNull }?.let(::paths) + val attachments = + settings["sourceAttachments"] + ?.let { entries -> + require(entries.isJsonArray) { + "sourceAttachments must be an array" + } + entries.asJsonArray + .map { entry -> + require(entry.isJsonObject) { + "Each source attachment must be an object" + } + val item: JsonObject = entry.asJsonObject + val name = item["module"] + require( + name?.isJsonPrimitive == true && name.asJsonPrimitive.isString && + name.asString.isNotBlank(), + ) { + "Source attachment needs a module name" + } + val roots = paths(requireNotNull(item["roots"]) { "Source attachment needs ordered roots" }) + require(roots.isNotEmpty() && roots.all(File::isDirectory)) { + "Attachment roots must be existing source directories" + } + name.asString to roots + }.also { values -> + require(values.map { it.first }.distinct().size == values.size) { + "Duplicate source attachment module" + } + }.toMap() + }.orEmpty() + return CompilerLibraries(modules, attachments) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt new file mode 100644 index 00000000000..78df4458cbc --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt @@ -0,0 +1,236 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.WorkDoneProgressBegin +import org.eclipse.lsp4j.WorkDoneProgressCreateParams +import org.eclipse.lsp4j.WorkDoneProgressEnd +import org.eclipse.lsp4j.WorkDoneProgressNotification +import org.eclipse.lsp4j.WorkDoneProgressReport +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.slf4j.LoggerFactory +import java.util.UUID +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Executor +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors +import java.util.concurrent.RejectedExecutionException +import java.util.concurrent.TimeUnit.MILLISECONDS +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.TimeoutException +import java.util.concurrent.atomic.AtomicBoolean + +/** + * Serializes progress transport calls outside compiler/document locks. Each token owns only its + * request future, never the shared analysis it may be waiting for. Fast queries need no popup. + */ +internal class ConnectionProgress( + private val client: () -> LanguageClient?, + private val delayMillis: Long = 300, + private val dispatcher: ExecutorService = + Executors.newSingleThreadExecutor(Thread.ofVirtual().name("lsp-progress").factory()), + private val reportScheduler: Executor = CompletableFuture.delayedExecutor(1, SECONDS), + private val creationDeadline: () -> CompletableFuture = { + CompletableFuture().completeOnTimeout(null, 10, SECONDS) + }, +) : AutoCloseable { + private data class Entry( + val title: String, + val result: CompletableFuture<*>, + val details: (() -> String)?, + ) + + private val canCreate = AtomicBoolean() + private val closed = AtomicBoolean() + + // Confined to dispatcher, including completion and cancellation callbacks. + private val entries = mutableMapOf, Entry>() + private val begun = mutableSetOf>() + + fun initialized(supported: Boolean) { + canCreate.set(supported) + } + + fun track( + title: String, + suppliedToken: Either?, + result: CompletableFuture, + details: (() -> String)? = null, + ): CompletableFuture { + if (closed.get() || (suppliedToken == null && !canCreate.get())) return result + val token = suppliedToken ?: Either.forLeft("xtc-${UUID.randomUUID()}") + val entry = Entry(title, result, details) + val start = + Runnable { + dispatch { + if (closed.get() || result.isDone || token in entries) return@dispatch + entries[token] = entry + result.whenComplete { _, _ -> dispatch { finish(token, entry) } } + if (suppliedToken != null) { + begin(token, entry) + } else { + create(token, entry) + } + } + } + if (suppliedToken != null) { + start.run() + } else { + CompletableFuture.delayedExecutor(delayMillis, MILLISECONDS).execute(start) + } + return result + } + + private fun create( + token: Either, + entry: Entry, + ) { + val acknowledgement = + runCatching { + client()?.createProgress(WorkDoneProgressCreateParams(token)) + }.getOrNull() + if (acknowledgement == null) { + entries.remove(token, entry) + return + } + // Do not time out the RPC's own future: the client can still create the token later. + val bounded = CompletableFuture() + val deadline = creationDeadline() + deadline.thenRun { + bounded.completeExceptionally(TimeoutException("Progress creation timed out")) + } + bounded.whenComplete { _, failure -> + deadline.cancel(false) + dispatch { + if (failure == null) begin(token, entry) else entries.remove(token, entry) + } + } + acknowledgement.whenComplete { _, failure -> + if (failure != null) { + bounded.completeExceptionally(failure) + } else if (!bounded.complete(null)) { + dispatch { + // A successful late acknowledgement owns a client-side progress registration. + // Retire it without reattaching cancellation to work that outlived the deadline. + if (!closed.get()) { + send( + token, + WorkDoneProgressBegin().apply { + title = entry.title + cancellable = false + }, + ) + send(token, WorkDoneProgressEnd().apply { message = "Progress expired" }) + } + } + } + } + } + + private fun begin( + token: Either, + entry: Entry, + ) { + if (closed.get() || entries[token] !== entry) return + begun.add(token) + send( + token, + WorkDoneProgressBegin().apply { + title = entry.title + cancellable = !entry.result.isDone + message = entry.details?.invoke() + }, + ) + if (entry.result.isDone) { + finish(token, entry) + } else if (entry.details != null) { + scheduleReport(token, entry) + } + } + + private fun scheduleReport( + token: Either, + entry: Entry, + ) { + reportScheduler.execute { + dispatch { + if (closed.get() || entry.result.isDone || entries[token] !== entry || token !in begun) return@dispatch + send(token, WorkDoneProgressReport().apply { message = entry.details?.invoke() }) + scheduleReport(token, entry) + } + } + } + + private fun finish( + token: Either, + entry: Entry, + ) { + if (token !in begun && !closed.get()) return + if (!entries.remove(token, entry)) return + if (begun.remove(token)) { + send( + token, + WorkDoneProgressEnd().apply { + message = + when { + entry.result.isCancelled -> "Canceled" + entry.result.isCompletedExceptionally -> "Failed or superseded" + else -> "Completed" + } + }, + ) + } + } + + fun report( + result: CompletableFuture<*>, + message: String, + percent: Int, + ) = dispatch { + if (closed.get() || result.isDone) return@dispatch + entries.entries + .firstOrNull { it.value.result === result && it.key in begun } + ?.let { (token, _) -> + send( + token, + WorkDoneProgressReport().apply { + this.message = message + percentage = percent.coerceIn(0, 100) + }, + ) + } + } + + fun cancel(token: Either) = dispatch { entries[token]?.result?.cancel(false) } + + private fun send( + token: Either, + value: WorkDoneProgressNotification, + ) { + runCatching { client()?.notifyProgress(ProgressParams(token, Either.forLeft(value))) } + .onFailure { logger.debug("Unable to publish progress", it) } + } + + private fun dispatch(block: () -> Unit) { + try { + dispatcher.execute(block) + } catch (_: RejectedExecutionException) { + // Connection already closed. + } + } + + override fun close() { + if (!closed.compareAndSet(false, true)) return + dispatch { + entries.toMap().forEach { (token, entry) -> + entry.result.cancel(false) + finish(token, entry) + } + dispatcher.shutdown() + } + } + + companion object { + private val logger = LoggerFactory.getLogger(ConnectionProgress::class.java) + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt new file mode 100644 index 00000000000..0e29dfee3f0 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt @@ -0,0 +1,171 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.DocumentDiagnosticReport +import org.eclipse.lsp4j.FullDocumentDiagnosticReport +import org.eclipse.lsp4j.RelatedFullDocumentDiagnosticReport +import org.eclipse.lsp4j.RelatedUnchangedDocumentDiagnosticReport +import org.eclipse.lsp4j.UnchangedDocumentDiagnosticReport +import org.eclipse.lsp4j.WorkspaceDiagnosticReport +import org.eclipse.lsp4j.WorkspaceDocumentDiagnosticReport +import org.eclipse.lsp4j.WorkspaceFullDocumentDiagnosticReport +import org.eclipse.lsp4j.WorkspaceUnchangedDocumentDiagnosticReport +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.toLsp +import java.io.File +import java.net.URI +import java.util.UUID + +/** Called under the document lifecycle lock. IDs belong to this server and one document's items. */ +internal class DiagnosticReports { + private data class Entry( + val id: String, + val diagnostics: List, + ) + + private val entries = mutableMapOf() + + fun record( + uri: String, + diagnostics: List, + ) { + val key = identity(uri) + val items = + diagnostics.map { + it.copy(location = it.location.copy(uri = identity(it.location.uri))) + } + if (entries[key]?.diagnostics != items) { + entries[key] = Entry(UUID.randomUUID().toString(), items) + } + } + + fun record(results: List): Set { + val uris = results.flatMap { it.documentUris }.toSet() + uris.forEach { uri -> + record( + uri, + results + .flatMap { result -> + result.diagnostics.filter { + it.location.uri == uri || + (uri == result.uri && it.location.uri !in result.documentUris) + } + }.distinct(), + ) + } + return uris + } + + fun document( + uri: String, + previous: String?, + related: Set, + relatedInformation: Boolean, + ): DocumentDiagnosticReport { + recordIfAbsent(uri) + val entry = entries.getValue(identity(uri)) + val others = + related + .filter { it != uri } + .associateWith { other -> + Either.forLeft( + FullDocumentDiagnosticReport( + entries.getValue(identity(other)).diagnostics.map { + it.present(other, relatedInformation) + }, + ).apply { + resultId = entries.getValue(identity(other)).id + }, + ) + }.takeIf { it.isNotEmpty() } + return if (previous == entry.id) { + DocumentDiagnosticReport( + RelatedUnchangedDocumentDiagnosticReport(entry.id).apply { + relatedDocuments = others + }, + ) + } else { + DocumentDiagnosticReport( + RelatedFullDocumentDiagnosticReport( + entry.diagnostics.map { it.present(uri, relatedInformation) }, + ).apply { + resultId = entry.id + relatedDocuments = others + }, + ) + } + } + + fun workspace( + current: Set, + previous: Map, + versions: Map, + relatedInformation: Boolean, + ): WorkspaceDiagnosticReport { + // A removed root/file must explicitly clear the client's previous report. Do not retain + // tombstones indefinitely: an unknown old ID simply receives another empty full report. + val currentUris = current.associateBy(::identity) + val oldUris = previous.keys.associateBy(::identity) + val previousIds = previous.mapKeys { identity(it.key) } + val documentVersions = versions.mapKeys { identity(it.key) } + (oldUris - currentUris.keys).values.forEach { record(it, emptyList()) } + val result = + WorkspaceDiagnosticReport( + (oldUris + currentUris).values.sorted().map { uri -> + recordIfAbsent(uri) + val entry = entries.getValue(identity(uri)) + if (previousIds[identity(uri)] == entry.id) { + WorkspaceDocumentDiagnosticReport( + WorkspaceUnchangedDocumentDiagnosticReport( + entry.id, + uri, + documentVersions[identity(uri)], + ), + ) + } else { + WorkspaceDocumentDiagnosticReport( + WorkspaceFullDocumentDiagnosticReport( + entry.diagnostics.map { it.present(uri, relatedInformation) }, + uri, + documentVersions[identity(uri)], + ).apply { resultId = entry.id }, + ) + } + }, + ) + entries.keys.retainAll(currentUris.keys) + return result + } + + private fun recordIfAbsent(uri: String) { + if (identity(uri) !in entries) record(uri, emptyList()) + } + + fun includes( + uris: Set, + uri: String, + ): Boolean = + uris.any { + identity(it) == identity(uri) + } + + private fun Diagnostic.present( + uri: String, + relatedInformation: Boolean, + ) = (if (location.uri == identity(uri)) copy(location = location.copy(uri = uri)) else this) + .toLsp(uri) + .apply { if (!relatedInformation) this.relatedInformation = null } + + private fun identity(uri: String): String = + runCatching { + val parsed = URI(uri).normalize() + if (parsed.scheme == "file") { + File(parsed).canonicalFile.toURI().toString() + } else { + parsed.toString() + } + }.getOrDefault(uri) + + fun clear() = entries.clear() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentSynchronization.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentSynchronization.kt new file mode 100644 index 00000000000..f2750c7fefb --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentSynchronization.kt @@ -0,0 +1,52 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.TextDocumentSyncKind +import org.eclipse.lsp4j.TextDocumentSyncOptions + +/** Connection options. Full synchronization and no server save edits remain the defaults. */ +internal data class DocumentSynchronization( + val incremental: Boolean = false, + val formatOnSave: Boolean = false, + val willSave: Boolean = false, + val waitUntil: Boolean = false, +) { + fun capabilities() = + TextDocumentSyncOptions().apply { + openClose = true + change = + if (incremental) TextDocumentSyncKind.Incremental else TextDocumentSyncKind.Full + willSave = this@DocumentSynchronization.willSave + willSaveWaitUntil = waitUntil + setSave(true) + } + + companion object { + fun read(params: InitializeParams): DocumentSynchronization { + val options = + Gson() + .toJsonTree(params.initializationOptions) + ?.takeIf { it.isJsonObject } + ?.asJsonObject + ?.get("xtcDocumentSync") + ?.takeUnless { it.isJsonNull } + require(options == null || options.isJsonObject) { "xtcDocumentSync must be an object" } + + fun flag(name: String): Boolean { + val value = options?.asJsonObject?.get(name) ?: return false + require(value.isJsonPrimitive && value.asJsonPrimitive.isBoolean) { + "xtcDocumentSync.$name must be a boolean" + } + return value.asBoolean + } + val synchronization = params.capabilities?.textDocument?.synchronization + return DocumentSynchronization( + flag("incremental"), + flag("formatOnSave"), + synchronization?.willSave == true, + synchronization?.willSaveWaitUntil == true, + ) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentText.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentText.kt new file mode 100644 index 00000000000..8bdd6832cab --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DocumentText.kt @@ -0,0 +1,73 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextEdit + +/** UTF-16 coordinates against one immutable document, including CRLF and bare CR line endings. */ +internal class DocumentText( + private val text: String, +) { + private val breaks = Regex("\r\n|\r|\n").findAll(text).toList() + private val starts = listOf(0) + breaks.map { it.range.last + 1 } + private val ends = breaks.map { it.range.first } + text.length + + fun offset(position: Position): Int { + require(position.line in starts.indices && position.character >= 0) { + "Invalid text position" + } + val start = starts[position.line] + val offset = start + position.character.coerceAtMost(ends[position.line] - start) + require( + offset == 0 || + offset == text.length || + !Character.isSurrogatePair(text[offset - 1], text[offset]), + ) { + "Text position splits a surrogate pair" + } + return offset + } + + fun bounds(range: Range): Pair { + val start = offset(range.start) + val end = offset(range.end) + require(start <= end) { "Reversed text range" } + return start to end + } + + /** Validate the complete batch before publishing any of its intermediate versions. */ + fun change( + changes: List, + incremental: Boolean, + ): String = + changes.fold(text) { current, change -> + val range = change.range + if (range == null) { + change.text + } else { + require(incremental) { "Incremental changes were not negotiated" } + val (start, end) = DocumentText(current).bounds(range) + current.replaceRange(start, end, change.text) + } + } + + /** + * Range formatters may expand to whole lines; deduplicate identical edits, refuse conflicts. + */ + fun nonOverlapping(edits: List): List { + val ordered = + edits + .distinct() + .map { it to bounds(it.range) } + .sortedWith(compareBy({ it.second.first }, { it.second.second })) + require( + ordered.zipWithNext().all { (a, b) -> + a.second.second <= b.second.first && a.second.first != b.second.first + }, + ) { + "Formatter returned overlapping edits" + } + return ordered.map { it.first } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt new file mode 100644 index 00000000000..cdde8a03799 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt @@ -0,0 +1,78 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import org.xvm.lsp.adapter.FormattingConfig +import java.util.concurrent.atomic.AtomicReference + +/** A late configuration reply cannot undo a newer preference or restore a reset value. */ +internal class EditorFormattingState : AutoCloseable { + private data class Snapshot( + val revision: Long = 0, + val config: FormattingConfig? = null, + val closed: Boolean = false, + ) + + private val current = AtomicReference(Snapshot()) + val config: FormattingConfig? + get() = current.get().config + + @Synchronized + fun request(): Long = current.updateAndGet { it.copy(revision = it.revision + 1) }.revision + + @Synchronized + fun accept( + revision: Long, + raw: Any?, + install: (FormattingConfig?) -> Unit, + ): Boolean { + if (current.get().let { it.closed || it.revision != revision }) return false + val config = parse(raw) + current.set(Snapshot(revision, config)) + install(config) + return true + } + + @Synchronized + override fun close() { + current.updateAndGet { it.copy(closed = true) } + } + + companion object { + fun parse(raw: Any?): FormattingConfig? { + if (raw == null) return null + val element = Gson().toJsonTree(raw) + if (element.isJsonNull) return null + require(element.isJsonObject) { "Ecstasy formatting settings must be an object" } + val settings = element.asJsonObject + + fun integer( + name: String, + default: Int, + maximum: Int, + ): Int = + settings[name]?.let { + require(it.isJsonPrimitive && it.asJsonPrimitive.isNumber) { + "$name must be an integer" + } + val value = it.asString.toIntOrNull() + require(value != null && value in 1..maximum) { + "$name must be an integer from 1 to $maximum" + } + value + } ?: default + val spaces = + settings["insertSpaces"]?.let { + require(it.isJsonPrimitive && it.asJsonPrimitive.isBoolean) { + "insertSpaces must be a boolean" + } + it.asBoolean + } ?: true + return FormattingConfig( + integer("indentSize", 4, 32), + integer("continuationIndentSize", 8, 32), + spaces, + integer("maxLineWidth", 120, 1000), + ) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/FileChangeSnapshots.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/FileChangeSnapshots.kt new file mode 100644 index 00000000000..173e98188bf --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/FileChangeSnapshots.kt @@ -0,0 +1,40 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.FileEvent +import java.net.URI +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path +import java.security.MessageDigest + +/** + * Coalesce small-file duplicates by content. Directory and large-file events always propagate: + * fingerprinting an entire moved workspace on the notification thread would stall the server. + */ +internal class FileChangeSnapshots( + private val limit: Int = 512, +) { + private val observed = linkedMapOf>() + + @Synchronized + fun changed(events: List): List = + events + .associateBy { it.uri } + .values + .filter { event -> + val path = + runCatching { Path.of(URI(event.uri)).toAbsolutePath().normalize() }.getOrNull() + ?: return@filter true + val fingerprint = + runCatching { fingerprint(path) }.getOrNull() ?: return@filter true + val previous = observed.put(path, fingerprint) + while (observed.size > limit) observed.remove(observed.keys.first()) + previous != fingerprint + } + + private fun fingerprint(path: Path): List? { + if (!Files.exists(path, NOFOLLOW_LINKS)) return emptyList() + if (!Files.isRegularFile(path, NOFOLLOW_LINKS) || Files.size(path) > 1_000_000) return null + return MessageDigest.getInstance("SHA-256").digest(Files.readAllBytes(path)).toList() + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt index 4ae7a65a8e9..287f4f65660 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt @@ -5,30 +5,53 @@ import com.google.gson.JsonElement import com.google.gson.JsonObject /** - * Reads typed values out of LSP4J `initializationOptions` / `workspace/configuration` - * payloads, which arrive as either `Map<*, *>` or Gson `JsonObject` / `JsonElement` - * depending on how the client serialises them. Returns `null` when a key is absent, - * mistyped, or can't be coerced -- callers supply their own defaults. + * Reads typed values out of LSP4J `initializationOptions` / `workspace/configuration` payloads, + * which arrive as either `Map<*, *>` or Gson `JsonObject` / `JsonElement` depending on how the + * client serializes them. Returns `null` when a key is absent, mistyped, or can't be coerced -- + * callers supply their own defaults. */ internal object LspJsonOptions { - /** True iff [raw] is a JSON-object-shaped value (Map, JsonObject, or JsonElement holding an object). */ + /** + * True iff [raw] is a JSON-object-shaped value (Map, JsonObject, or JsonElement holding an + * object). + */ fun isObject(raw: Any?): Boolean = raw is Map<*, *> || raw is JsonObject || (raw is JsonElement && raw.isJsonObject) - /** Read [key] as a list of non-blank strings; returns `emptyList()` if missing or wrong shape. */ + /** + * Read [key] as a list of non-blank strings; returns `emptyList()` if missing or wrong shape. + */ fun stringList( raw: Any?, key: String, ): List { val value = lookup(raw, key) ?: return emptyList() return when (value) { - is List<*> -> value.mapNotNull { it?.toString()?.trim()?.takeIf(String::isNotEmpty) } - is JsonArray -> value.mapNotNull { stringOrNull(it) } - is JsonElement -> if (value.isJsonArray) value.asJsonArray.mapNotNull { stringOrNull(it) } else emptyList() - else -> emptyList() + is List<*> -> { + value.mapNotNull { it?.toString()?.trim()?.takeIf(String::isNotEmpty) } + } + + is JsonArray -> { + value.mapNotNull { stringOrNull(it) } + } + + is JsonElement -> { + if (value.isJsonArray) { + value.asJsonArray.mapNotNull { stringOrNull(it) } + } else { + emptyList() + } + } + + else -> { + emptyList() + } } } - /** Read [key] as an [Int]; coerces from numeric primitives or numeric strings, else returns null. */ + /** + * Read [key] as an [Int]; coerces from numeric primitives or numeric strings, else returns + * null. + */ fun int( raw: Any?, key: String, @@ -43,13 +66,23 @@ internal object LspJsonOptions { } is JsonElement -> { - value.takeUnless { it.isJsonNull }?.let { - when { - it.isJsonPrimitive && it.asJsonPrimitive.isNumber -> it.asInt - it.isJsonPrimitive && it.asJsonPrimitive.isString -> it.asString.toIntOrNull() - else -> null + value + .takeUnless { it.isJsonNull } + ?.let { + when { + it.isJsonPrimitive && it.asJsonPrimitive.isNumber -> { + it.asInt + } + + it.isJsonPrimitive && it.asJsonPrimitive.isString -> { + it.asString.toIntOrNull() + } + + else -> { + null + } + } } - } } else -> { @@ -72,13 +105,23 @@ internal object LspJsonOptions { } is JsonElement -> { - value.takeUnless { it.isJsonNull }?.let { - when { - it.isJsonPrimitive && it.asJsonPrimitive.isBoolean -> it.asBoolean - it.isJsonPrimitive && it.asJsonPrimitive.isString -> it.asString.toBooleanStrictOrNull() - else -> null + value + .takeUnless { it.isJsonNull } + ?.let { + when { + it.isJsonPrimitive && it.asJsonPrimitive.isBoolean -> { + it.asBoolean + } + + it.isJsonPrimitive && it.asJsonPrimitive.isString -> { + it.asString.toBooleanStrictOrNull() + } + + else -> { + null + } + } } - } } else -> { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt new file mode 100644 index 00000000000..90d80dd17d5 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt @@ -0,0 +1,93 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors +import java.util.concurrent.RejectedExecutionException +import java.util.concurrent.atomic.AtomicBoolean + +/** Publish detached result batches outside compiler/document locks, before the final response. */ +internal class PartialResults( + private val client: () -> LanguageClient?, + private val dispatcher: ExecutorService = + Executors.newSingleThreadExecutor(Thread.ofVirtual().name("lsp-partial-results").factory()), +) : AutoCloseable { + data class Plan( + val batches: List, + val remainder: T, + ) + + private val closed = AtomicBoolean() + private val pending = ConcurrentHashMap.newKeySet>() + + fun publish( + token: Either?, + source: CompletableFuture, + checkCurrent: () -> Unit, + plan: (T) -> Plan, + ): CompletableFuture { + if (token == null) return source + val result = CompletableFuture() + pending.add(result) + result.whenCompleteAsync { _, failure -> + pending.remove(result) + if (failure != null) source.cancel(false) + } + if (closed.get()) result.cancel(false) + source.whenComplete { value, failure -> + try { + dispatcher.execute { + if (result.isDone) return@execute + if (failure != null) { + result.completeExceptionally(failure) + return@execute + } + try { + checkCurrent() + val connection = client() + if (connection == null) { + result.complete(value) + return@execute + } + val publication = plan(value) + publication.batches.forEach { batch -> + if (result.isDone || closed.get()) return@execute + checkCurrent() + connection.notifyProgress(ProgressParams(token, Either.forRight(batch))) + } + checkCurrent() + result.complete(publication.remainder) + } catch (error: Exception) { + result.completeExceptionally(error) + } + } + } catch (_: RejectedExecutionException) { + result.cancel(false) + } + } + return result + } + + override fun close() { + if (!closed.compareAndSet(false, true)) return + pending.forEach { it.cancel(false) } + dispatcher.shutdown() + } + + companion object { + const val BATCH_SIZE = 64 + + fun list(values: List): Plan> = Plan(values.chunked(BATCH_SIZE), emptyList()) + + fun eitherLists(values: Either, List>): Plan, List>> = + if (values.isLeft) { + Plan(values.left.chunked(BATCH_SIZE), Either.forLeft(emptyList())) + } else { + Plan(values.right.chunked(BATCH_SIZE), Either.forRight(emptyList())) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt new file mode 100644 index 00000000000..3fffc4e406e --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt @@ -0,0 +1,129 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import java.util.concurrent.atomic.AtomicReference + +/** Enforces the wire lifecycle without restricting direct embedding/test use of server services. */ +internal class ProtocolLifecycle { + private enum class Phase { + NEW, + INITIALIZING, + READY, + RUNNING, + SHUTDOWN, + } + + private data class State( + val phase: Phase, + val initialization: Either? = null, + ) + + private val state = AtomicReference(State(Phase.NEW)) + private val output = AtomicReference() + + fun wrap( + next: MessageConsumer, + received: Boolean, + ): MessageConsumer { + val consumer = + MessageConsumer { message -> + if (!received) { + if (message is ResponseMessage) { + val previous = state.get() + if ( + previous.phase == Phase.INITIALIZING && + previous.initialization == message.rawId + ) { + state.compareAndSet( + previous, + State(if (message.error == null) Phase.READY else Phase.NEW), + ) + } + } + next.consume(message) + } else { + when (message) { + is RequestMessage -> { + val previous = state.get() + val permitted = + when (previous.phase) { + Phase.NEW if message.method == "initialize" -> { + state.compareAndSet( + previous, + State(Phase.INITIALIZING, message.rawId), + ) + } + + Phase.READY, Phase.RUNNING -> { + message.method != "initialize" && + (message.method != "shutdown" || state.compareAndSet(previous, State(Phase.SHUTDOWN))) + } + + else -> { + false + } + } + if (permitted) { + next.consume(message) + } else { + output + .get() + .consume( + ResponseMessage().apply { + rawId = message.rawId + error = + if ( + previous.phase == Phase.NEW || + previous.phase == Phase.INITIALIZING + ) { + ResponseError( + ResponseErrorCode.ServerNotInitialized, + "Server is not initialized", + null, + ) + } else { + ResponseError( + ResponseErrorCode.InvalidRequest, + "Request is invalid in the current server lifecycle", + null, + ) + } + }, + ) + } + } + + is NotificationMessage -> { + val previous = state.get() + if (message.method == "exit") { + next.consume(message) + } else if (message.method == "initialized") { + if ( + previous.phase == Phase.READY && + state.compareAndSet(previous, State(Phase.RUNNING)) + ) { + next.consume(message) + } + } else if (previous.phase == Phase.READY || previous.phase == Phase.RUNNING) { + next.consume(message) + } + } + + else -> { + next.consume( + message, + ) + } // Replies must still release outstanding client requests. + } + } + } + if (!received) output.set(consumer) + return consumer + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt new file mode 100644 index 00000000000..b973f8d8b11 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt @@ -0,0 +1,160 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.ConcurrentHashMap + +/** + * Server-side receive-to-write latency, including asynchronous waits and response serialization. + */ +internal class ProtocolTrace( + private val clientTrace: ClientTrace? = null, +) : AutoCloseable { + private val incoming = ConcurrentHashMap, ExecutionTrace.Span>() + private val outgoing = ConcurrentHashMap, ExecutionTrace.Span>() + + fun wrap( + next: MessageConsumer, + received: Boolean, + ): MessageConsumer = + MessageConsumer { message -> + val requests = if (received) incoming else outgoing + val replies = if (received) outgoing else incoming + when (message) { + is RequestMessage -> { + val span = ExecutionTrace.span("lsp-request", message.method) + requests[message.rawId] = span + ExecutionTrace.event( + span, + "start", + mapOf( + "rpcId" to message.id, + "direction" to if (received) "from-client" else "to-client", + ), + ) + try { + ExecutionTrace.within(span) { next.consume(message) } + } catch (failure: Throwable) { + requests.remove(message.rawId, span) + ExecutionTrace.event( + span, + "end", + mapOf("outcome" to "transport-failed", "failure" to failure.javaClass.name), + ) + throw failure + } + } + + is ResponseMessage -> { + val span = replies.remove(message.rawId) + val writeStarted = System.nanoTime() + if (!received) span?.let { ExecutionTrace.event(it, "reply-ready") } + try { + next.consume(message) + span?.let { + if (!received) clientTrace?.completed(it, message.error?.code) + ExecutionTrace.event( + it, + "end", + mapOf( + "rpcId" to message.id, + "outcome" to if (message.error == null) "replied" else "error", + "errorCode" to message.error?.code, + "writeMs" to if (received) null else ExecutionTrace.elapsed(writeStarted), + "boundary" to + if (received) { + "client-reply-received" + } else { + "server-reply-written" + }, + ), + ) + } + } catch (failure: Throwable) { + span?.let { + ExecutionTrace.event( + it, + "end", + mapOf( + "outcome" to "transport-failed", + "failure" to failure.javaClass.name, + ), + ) + } + throw failure + } + } + + is NotificationMessage -> { + val metadata = + when (val params = message.params) { + is DidOpenTextDocumentParams -> { + mapOf( + "uri" to params.textDocument.uri, + "version" to params.textDocument.version, + ) + } + + is DidChangeTextDocumentParams -> { + mapOf( + "uri" to params.textDocument.uri, + "version" to params.textDocument.version, + ) + } + + is DidCloseTextDocumentParams -> { + mapOf("uri" to params.textDocument.uri) + } + + is PublishDiagnosticsParams -> { + mapOf( + "uri" to params.uri, + "version" to params.version, + "diagnostics" to params.diagnostics.size, + ) + } + + else -> { + emptyMap() + } + } + val span = + ExecutionTrace.span( + "lsp-notification", + message.method, + metadata["uri"] as? String, + ) + ExecutionTrace.event(span, if (received) "received" else "sending", metadata) + try { + ExecutionTrace.within(span) { next.consume(message) } + ExecutionTrace.event(span, "dispatched") + } catch (failure: Throwable) { + ExecutionTrace.event(span, "failed", mapOf("failure" to failure.javaClass.name)) + throw failure + } + } + + else -> { + next.consume(message) + } + } + } + + override fun close() { + listOf(incoming, outgoing).forEach { requests -> + requests.forEach { (id, span) -> + if (requests.remove(id, span)) { + ExecutionTrace.event(span, "end", mapOf("outcome" to "transport-closed")) + } + } + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt new file mode 100644 index 00000000000..40e5c840d62 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt @@ -0,0 +1,109 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.TextDocumentContentRefreshParams +import org.eclipse.lsp4j.TextDocumentContentRegistrationOptions +import org.eclipse.lsp4j.TextDocumentContentResult +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.slf4j.LoggerFactory +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.ReadOnlyDocument +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicReference + +/** + * Per-connection ownership of revision-addressed library documents. Mapping a navigation target + * registers it; this is deliberately not a file reader or a client-controlled URI decoder. + * A new library revision has a new URI. Refresh revalidates an already fetched immutable view. + */ +internal class ReadOnlyDocuments( + private val adapter: Adapter, + private val client: () -> LanguageClient?, +) : AutoCloseable { + private data class State( + val enabled: Boolean = false, + val ready: Boolean = false, + val closed: Boolean = false, + val documents: Map = emptyMap(), + val fetched: Set = emptySet(), + ) + + private val state = AtomicReference(State()) + private val refreshing = ConcurrentHashMap>() + + fun configure(supported: Boolean) { + state.updateAndGet { if (it.closed) it else it.copy(enabled = supported && adapter.readOnlyDocumentSchemes.isNotEmpty()) } + } + + fun options(): TextDocumentContentRegistrationOptions? = + if (state.get().enabled) TextDocumentContentRegistrationOptions(adapter.readOnlyDocumentSchemes.sorted()) else null + + fun initialized() { + state.updateAndGet { it.copy(ready = !it.closed) } + } + + fun present(uri: String): String { + val current = state.get() + if (!current.enabled || current.closed) return uri + val document = adapter.readOnlyDocument(uri) ?: return uri + val installed = + state.updateAndGet { + if (it.closed) it else it.copy(documents = it.documents + (document.uri to document)) + } + return if (installed.closed) uri else document.uri + } + + fun content(uri: String): CompletableFuture { + val current = + state.updateAndGet { + if (it.closed || uri !in it.documents) it else it.copy(fetched = it.fetched + uri) + } + val document = current.documents[uri] + return if (!current.closed && current.enabled && document != null) { + CompletableFuture.completedFuture(TextDocumentContentResult(document.text)) + } else { + CompletableFuture.failedFuture( + ResponseErrorException(ResponseError(ResponseErrorCode.InvalidParams, "Unknown or retired library document", null)), + ) + } + } + + /** Called after compiler input replacement. One outstanding refresh per immutable document. */ + fun refresh(): CompletableFuture { + val current = state.get() + if (!current.ready || current.closed) return CompletableFuture.completedFuture(null) + return CompletableFuture.allOf(*current.fetched.map(::refreshDocument).toTypedArray()) + } + + private fun refreshDocument(uri: String): CompletableFuture { + val result = CompletableFuture() + refreshing.putIfAbsent(uri, result)?.let { return it } + // Never enter the client transport while holding compiler/document publication locks. + CompletableFuture.runAsync { + val reply = + runCatching { + if (state.get().closed) null else client()?.refreshTextDocumentContent(TextDocumentContentRefreshParams(uri)) + }.getOrElse { CompletableFuture.failedFuture(it) } ?: CompletableFuture.completedFuture(null) + reply.orTimeout(10, SECONDS).whenComplete { _, failure -> + refreshing.remove(uri, result) + if (failure != null && !state.get().closed) logger.debug("Library content refresh failed for {}", uri, failure) + result.complete(null) + } + } + return result + } + + override fun close() { + state.set(State(closed = true)) + refreshing.values.forEach { it.cancel(false) } + refreshing.clear() + } + + private companion object { + val logger = LoggerFactory.getLogger(ReadOnlyDocuments::class.java)!! + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt new file mode 100644 index 00000000000..ab91e00201e --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt @@ -0,0 +1,36 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.WorkspaceEdit +import org.xvm.lsp.adapter.xdk.XdkSourceModule + +/** Native Rename extension. A client must accept the graph and edit as one undoable transaction. */ +data class RenameProposal( + val edit: WorkspaceEdit, + val graph: SourceGraphReplacement? = null, + val scope: RenameScope? = null, +) + +data class SourceGraphReplacement( + val before: List, + val after: List, +) + +/** Wire data deliberately excludes compiler objects and XdkSourceModule's filesystem helpers. */ +data class SourceModuleConfiguration( + val name: String, + val uri: String, + val dependencies: List, + val resourceRoots: List? = null, +) { + internal constructor( + module: XdkSourceModule, + ) : this(module.name, module.uri, module.dependencies.sorted(), module.resourceRoots) +} + +/** Input graph receipt; does not claim knowledge of consumers outside these source roots. */ +data class RenameScope( + val boundary: String, + val modules: List, + val sourceUris: List, + val revision: String, +) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt new file mode 100644 index 00000000000..9bdbf39023c --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt @@ -0,0 +1,68 @@ +package org.xvm.lsp.server + +import com.google.gson.JsonPrimitive +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.util.UUID + +/** Bounded detached results, owned by one connection and guarded by the document lifecycle lock. */ +internal class ResolveReports( + private val limit: Int = 512, + private val characterLimit: Int = 2_000_000, +) { + private data class Entry( + val revision: Long, + val label: String, + val value: T, + val characters: Int, + ) + + private val entries = linkedMapOf>() + + fun remember( + revision: Long, + label: String, + value: T, + characters: Int, + ): String? { + // Oversized values remain eager rather than returning a handle which is already evicted. + if (characters > characterLimit) return null + val id = UUID.randomUUID().toString() + entries[id] = Entry(revision, label, value, characters) + while ( + entries.size > limit || entries.values.sumOf { it.characters } > characterLimit + ) { + entries.remove(entries.keys.first()) + } + return id + } + + fun resolve( + data: Any?, + revision: Long, + label: String, + consume: Boolean = false, + ): T { + val id = + when (data) { + is String -> data + is JsonPrimitive -> data.takeIf { it.isString }?.asString + else -> null + } + val entry = entries[id] + if (entry == null || entry.revision != revision || entry.label != label) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.ContentModified, + "Result expired or changed; request a fresh list", + null, + ), + ) + } + if (consume) entries.remove(id) + return entry.value + } + + fun clear() = entries.clear() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResourceFileWatchers.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResourceFileWatchers.kt new file mode 100644 index 00000000000..7ca7762fce0 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResourceFileWatchers.kt @@ -0,0 +1,250 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions +import org.eclipse.lsp4j.FileSystemWatcher +import org.eclipse.lsp4j.Registration +import org.eclipse.lsp4j.RegistrationParams +import org.eclipse.lsp4j.RelativePattern +import org.eclipse.lsp4j.Unregistration +import org.eclipse.lsp4j.UnregistrationParams +import org.eclipse.lsp4j.WatchKind +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.slf4j.LoggerFactory +import java.io.Closeable +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.UUID +import java.util.concurrent.CancellationException +import java.util.concurrent.CompletableFuture +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.TimeoutException +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference + +/** Serialize registration changes so a delayed removal cannot retire a newer root subscription. */ +internal class ResourceFileWatchers( + private val deadline: () -> CompletableFuture = { + CompletableFuture().completeOnTimeout(null, 10, SECONDS) + }, +) : Closeable { + private val closed = AtomicBoolean() + private val pending = ConcurrentHashMap.newKeySet>() + + // Unacknowledged removals remain retryable, but their IDs are never reusable as active watches. + private val retired = ConcurrentHashMap() + private val queue = + AtomicReference(CompletableFuture.completedFuture(emptyMap())) + + fun update( + client: LanguageClient, + roots: Set, + relativePatterns: Boolean = true, + ): CompletableFuture> { + if (closed.get()) return CompletableFuture.completedFuture(emptyMap()) + val result = CompletableFuture>() + val previous = queue.getAndSet(result) + previous + .thenComposeAsync { active -> + if (closed.get()) { + return@thenComposeAsync CompletableFuture.completedFuture(emptyMap()) + } + val next = + roots + .map { Watch.forRoot(it, relativePatterns) } + .associateWith { + active[it] ?: "xtc-resources-${UUID.randomUUID()}" + } + val added = next.filterKeys { it !in active } + val removed = + ( + active.filterKeys { it !in next }.values + + retired.entries.filter { it.value === client }.map { it.key } + ).distinct() + val register = + if (added.isEmpty()) { + CompletableFuture.completedFuture(null) + } else { + awaitReply(onLateSuccess = { unregister(client, added.values) }) { + client.registerCapability( + RegistrationParams( + added.map { (watch, id) -> + Registration( + id, + "workspace/didChangeWatchedFiles", + DidChangeWatchedFilesRegistrationOptions( + watch.patterns(), + ), + ) + }, + ), + ) + } + } + register + .handle { _, failure -> + if (failure != null) { + logger.warn( + "Resource watcher registration failed; retaining previous roots", + failure, + ) + CompletableFuture.completedFuture(active) + } else if (removed.isEmpty()) { + CompletableFuture.completedFuture(next) + } else { + awaitReply { unregister(client, removed) } + .handle { _, removalFailure -> + if (removalFailure == null) { + next + } else { + logger.warn( + "Resource watcher removal was not acknowledged; retiring its registration IDs", + removalFailure, + ) + // An eventual successful removal must never remove an ID + // reused by a later update, even for the same root. + next + } + } + } + }.thenCompose { it } + }.whenComplete { current, failure -> + if (failure == null) { + result.complete(current) + } else { + logger.warn("Resource watcher update failed", failure) + // Keep the queue usable even if a client implementation throws synchronously. + result.complete(previous.getNow(emptyMap())) + } + } + return result.thenApply { active -> active.entries.associate { it.key.root to it.value } } + } + + /** Bound each reply without cancelling the original RPC: it can still succeed on the client. */ + private fun awaitReply( + onLateSuccess: () -> Unit = {}, + action: () -> CompletableFuture, + ): CompletableFuture { + val result = CompletableFuture() + pending.add(result) + result.whenComplete { _, _ -> pending.remove(result) } + if (closed.get()) { + result.completeExceptionally(CancellationException("Resource watchers closed")) + return result + } + val timer = deadline() + timer.thenRun { + result.completeExceptionally(TimeoutException("Resource watcher reply timed out")) + } + result.whenComplete { _, _ -> timer.cancel(false) } + attempt(action).whenComplete { _, failure -> + if (failure != null) { + result.completeExceptionally(failure) + } else if (!result.complete(null)) { + onLateSuccess() + } + } + return result + } + + private fun unregister( + client: LanguageClient, + ids: Collection, + ): CompletableFuture { + ids.forEach { retired[it] = client } + val reply = + attempt { + client.unregisterCapability( + UnregistrationParams( + ids.map { + Unregistration(it, "workspace/didChangeWatchedFiles") + }, + ), + ) + } + reply.whenComplete { _, failure -> + if (failure == null) ids.forEach { retired.remove(it, client) } + } + return reply + } + + override fun close() { + if (closed.compareAndSet(false, true)) { + retired.clear() + pending.forEach { + it.completeExceptionally(CancellationException("Resource watchers closed")) + } + } + } + + /** Keep the recursive root plus a flat watch that survives root removal and creation. */ + private data class Watch( + val root: String, + val relative: Boolean, + val ancestor: String?, + val child: String?, + ) { + fun patterns(): List = + buildList { + add(pattern(root, "**/*")) + if (ancestor != null && child != null) add(pattern(ancestor, child)) + } + + private fun pattern( + base: String, + glob: String, + ) = FileSystemWatcher( + if (relative) { + Either.forRight(RelativePattern(Either.forRight(base), glob)) + } else { + Either.forLeft(URI.create(base).path.trimEnd('/') + "/" + glob) + }, + WatchKind.Create + WatchKind.Change + WatchKind.Delete, + ) + + companion object { + fun forRoot( + root: String, + relative: Boolean, + ): Watch { + val path = Path.of(URI.create(root)) + val ancestor = + generateSequence(path.parent) { it.parent }.firstOrNull(Files::isDirectory) + // Watch only the first missing child, never the ancestor's entire subtree. A + // membership event updates this plan as generated parent directories appear. + val child = + ancestor + ?.relativize(path) + ?.getName(0) + ?.toString() + ?.map { character -> + when (character) { + '*', + '?', + '[', + ']', + '{', + '}', + -> "[$character]" + + else -> character.toString() + } + }?.joinToString("") + return Watch(root, relative, ancestor?.toUri()?.toString(), child) + } + } + } + + private companion object { + val logger = LoggerFactory.getLogger(ResourceFileWatchers::class.java)!! + + fun attempt(action: () -> CompletableFuture): CompletableFuture = + try { + action() + } catch (failure: RuntimeException) { + CompletableFuture.failedFuture(failure) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt new file mode 100644 index 00000000000..97f35cf5d76 --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt @@ -0,0 +1,127 @@ +package org.xvm.lsp.server + +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SemanticTokens +import org.eclipse.lsp4j.SemanticTokensDelta +import org.eclipse.lsp4j.SemanticTokensEdit +import org.eclipse.lsp4j.jsonrpc.messages.Either +import java.net.URI +import java.nio.file.Path +import java.util.UUID + +/** Detached token arrays only. Access is serialized by the document lifecycle lock. */ +internal class SemanticTokenReports( + private val maximumReports: Int = 128, + private val maximumIntegers: Int = 1_000_000, +) { + private data class Report( + val uri: String, + val data: List, + ) + + private val reports = linkedMapOf() + + fun full( + uri: String, + data: List, + ): SemanticTokens { + val id = UUID.randomUUID().toString() + val snapshot = data.toList() + reports[id] = Report(key(uri), snapshot) + while ( + reports.size > maximumReports || reports.values.sumOf { it.data.size } > maximumIntegers + ) { + reports.remove(reports.keys.first()) + } + return SemanticTokens(id, snapshot.toList()) + } + + fun delta( + uri: String, + previousId: String, + data: List, + ): Either { + val previous = reports[previousId]?.takeIf { it.uri == key(uri) } + val current = full(uri, data) + if (previous == null) return Either.forLeft(current) + val old = previous.data + val next = current.data + val prefix = + old.indices + .asSequence() + .takeWhile { it < next.size && old[it] == next[it] } + .count() + val suffix = + (0 until minOf(old.size, next.size) - prefix) + .asSequence() + .takeWhile { old[old.lastIndex - it] == next[next.lastIndex - it] } + .count() + val edits = + if (old == next) { + emptyList() + } else { + listOf( + SemanticTokensEdit( + prefix, + old.size - prefix - suffix, + next.subList(prefix, next.size - suffix).toList(), + ), + ) + } + return Either.forRight(SemanticTokensDelta(edits, current.resultId)) + } + + fun retire(uri: String) { + reports.entries.removeIf { it.value.uri == key(uri) } + } + + fun clear() = reports.clear() + + private fun key(uri: String): String = + runCatching { + Path + .of(URI(uri)) + .normalize() + .toUri() + .toString() + }.getOrDefault(uri) + + companion object { + /** + * Filter intersecting single-line UTF-16 tokens, then encode relative to document start. + */ + fun range( + data: List, + range: Range, + ): SemanticTokens { + var line = 0 + var column = 0 + var previousLine = 0 + var previousColumn = 0 + val filtered = + buildList { + data.chunked(5).forEach { token -> + line += token[0] + column = if (token[0] == 0) column + token[1] else token[1] + if ( + line >= range.start.line && + line <= range.end.line && + ( + line != range.start.line || + column + token[2] > range.start.character + ) && + (line != range.end.line || column < range.end.character) && + range.start != range.end + ) { + add(line - previousLine) + add(if (line == previousLine) column - previousColumn else column) + addAll(token.drop(2)) + previousLine = line + previousColumn = column + } + } + } + return SemanticTokens(filtered) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SourceRootResolver.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SourceRootResolver.kt index ef207dc44ae..4e6d9014790 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SourceRootResolver.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SourceRootResolver.kt @@ -6,16 +6,16 @@ import java.io.File /** * Resolves extra `.x` source roots beyond the workspace folders the client opens. * - * The workspace indexer only walks `.x` files inside the workspace, so any module that - * exists only as a compiled `.xtc` binary (or whose source lives outside the user's open - * project) is invisible to navigation. This resolver lets callers point the indexer at - * additional source-tree roots so e.g. the XDK standard-library sources can be discovered - * even when the user is working on an unrelated project. + * The workspace indexer only walks `.x` files inside the workspace, so any module that exists only + * as a compiled `.xtc` binary (or whose source lives outside the user's open project) is invisible + * to navigation. This resolver lets callers point the indexer at additional source-tree roots so + * e.g. the XDK standard-library sources can be discovered even when the user is working on an + * unrelated project. * * Sources, all merged (deduped): - * 1. LSP `initializationOptions.xtcSourceRoots` -- an array of paths sent by the client - * 2. System property `-Dxtc.sourceRoots=${File.pathSeparator}` - * 3. Env var `XTC_SOURCE_ROOTS=${File.pathSeparator}` + * 1. LSP `initializationOptions.xtcSourceRoots` -- an array of paths sent by the client + * 2. System property `-Dxtc.sourceRoots=${File.pathSeparator}` + * 3. Env var `XTC_SOURCE_ROOTS=${File.pathSeparator}` * * The returned paths are absolute filesystem strings (not URIs), matching the contract of * [org.xvm.lsp.index.WorkspaceIndexer.scanWorkspace]. Non-existent paths are passed through @@ -23,9 +23,9 @@ import java.io.File * double-log the same dropped path during startup. */ internal object SourceRootResolver { - const val INIT_OPTION_KEY = "xtcSourceRoots" - const val SYSTEM_PROPERTY = "xtc.sourceRoots" - const val ENV_VAR = "XTC_SOURCE_ROOTS" + private const val INIT_OPTION_KEY = "xtcSourceRoots" + private const val SYSTEM_PROPERTY = "xtc.sourceRoots" + private const val ENV_VAR = "XTC_SOURCE_ROOTS" private val logger = LoggerFactory.getLogger(SourceRootResolver::class.java) @@ -33,9 +33,9 @@ internal object SourceRootResolver { * Resolve extra source roots from all configured inputs. * * @param initializationOptions raw init-options value from the LSP `initialize` request -- - * accepts `Map<*, *>`, Gson `JsonObject`/`JsonElement`, or null - * @param systemProperty defaults to `System.getProperty("xtc.sourceRoots")` - * @param envVar defaults to `System.getenv("XTC_SOURCE_ROOTS")` + * accepts `Map<*, *>`, Gson `JsonObject`/`JsonElement`, or null + * @param systemProperty defaults to `System.getProperty("xtc.sourceRoots")` + * @param envVar defaults to `System.getenv("XTC_SOURCE_ROOTS")` */ fun resolve( initializationOptions: Any?, @@ -59,9 +59,6 @@ internal object SourceRootResolver { } private fun parsePathList(raw: String?): List = - raw - ?.split(File.pathSeparatorChar) - ?.map { it.trim() } - ?.filter { it.isNotEmpty() } + raw?.split(File.pathSeparatorChar)?.map { it.trim() }?.filter { it.isNotEmpty() } ?: emptyList() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt index 2e5c6faae8c..79a932b3cf0 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt @@ -1,98 +1,60 @@ package org.xvm.lsp.server -import org.eclipse.lsp4j.CallHierarchyIncomingCall -import org.eclipse.lsp4j.CallHierarchyIncomingCallsParams -import org.eclipse.lsp4j.CallHierarchyItem -import org.eclipse.lsp4j.CallHierarchyOutgoingCall -import org.eclipse.lsp4j.CallHierarchyOutgoingCallsParams -import org.eclipse.lsp4j.CallHierarchyPrepareParams -import org.eclipse.lsp4j.CodeAction -import org.eclipse.lsp4j.CodeActionParams -import org.eclipse.lsp4j.CodeLens +import org.eclipse.lsp4j.ApplyWorkspaceEditParams +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.eclipse.lsp4j.CodeActionOptions import org.eclipse.lsp4j.CodeLensOptions -import org.eclipse.lsp4j.CodeLensParams -import org.eclipse.lsp4j.Command -import org.eclipse.lsp4j.CompletionItem -import org.eclipse.lsp4j.CompletionItemKind -import org.eclipse.lsp4j.CompletionList import org.eclipse.lsp4j.CompletionOptions -import org.eclipse.lsp4j.CompletionParams import org.eclipse.lsp4j.ConfigurationItem import org.eclipse.lsp4j.ConfigurationParams -import org.eclipse.lsp4j.DeclarationParams -import org.eclipse.lsp4j.DefinitionParams -import org.eclipse.lsp4j.DidChangeConfigurationParams -import org.eclipse.lsp4j.DidChangeTextDocumentParams -import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DiagnosticRegistrationOptions import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions -import org.eclipse.lsp4j.DidCloseTextDocumentParams -import org.eclipse.lsp4j.DidOpenTextDocumentParams -import org.eclipse.lsp4j.DidSaveTextDocumentParams -import org.eclipse.lsp4j.DocumentFormattingParams -import org.eclipse.lsp4j.DocumentHighlight -import org.eclipse.lsp4j.DocumentHighlightParams -import org.eclipse.lsp4j.DocumentLink import org.eclipse.lsp4j.DocumentLinkOptions -import org.eclipse.lsp4j.DocumentLinkParams import org.eclipse.lsp4j.DocumentOnTypeFormattingOptions -import org.eclipse.lsp4j.DocumentOnTypeFormattingParams -import org.eclipse.lsp4j.DocumentRangeFormattingParams -import org.eclipse.lsp4j.DocumentSymbol -import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.DocumentRangeFormattingOptions +import org.eclipse.lsp4j.ExecuteCommandOptions +import org.eclipse.lsp4j.ExecuteCommandParams +import org.eclipse.lsp4j.FileOperationFilter +import org.eclipse.lsp4j.FileOperationOptions +import org.eclipse.lsp4j.FileOperationPattern +import org.eclipse.lsp4j.FileOperationsServerCapabilities +import org.eclipse.lsp4j.FileOperationsWorkspaceCapabilities import org.eclipse.lsp4j.FileSystemWatcher -import org.eclipse.lsp4j.FoldingRange -import org.eclipse.lsp4j.FoldingRangeRequestParams -import org.eclipse.lsp4j.Hover -import org.eclipse.lsp4j.HoverParams -import org.eclipse.lsp4j.ImplementationParams import org.eclipse.lsp4j.InitializeParams import org.eclipse.lsp4j.InitializeResult import org.eclipse.lsp4j.InitializedParams -import org.eclipse.lsp4j.InlayHint -import org.eclipse.lsp4j.InlayHintParams -import org.eclipse.lsp4j.LinkedEditingRangeParams -import org.eclipse.lsp4j.LinkedEditingRanges -import org.eclipse.lsp4j.Location -import org.eclipse.lsp4j.LocationLink -import org.eclipse.lsp4j.MarkupContent -import org.eclipse.lsp4j.MarkupKind -import org.eclipse.lsp4j.ParameterInformation -import org.eclipse.lsp4j.Position -import org.eclipse.lsp4j.PrepareRenameDefaultBehavior -import org.eclipse.lsp4j.PrepareRenameParams -import org.eclipse.lsp4j.PrepareRenameResult +import org.eclipse.lsp4j.InlayHintRegistrationOptions +import org.eclipse.lsp4j.MessageParams +import org.eclipse.lsp4j.MessageType import org.eclipse.lsp4j.PublishDiagnosticsParams -import org.eclipse.lsp4j.Range -import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.ReferenceOptions import org.eclipse.lsp4j.Registration import org.eclipse.lsp4j.RegistrationParams +import org.eclipse.lsp4j.RenameFilesParams import org.eclipse.lsp4j.RenameOptions import org.eclipse.lsp4j.RenameParams -import org.eclipse.lsp4j.SelectionRange -import org.eclipse.lsp4j.SelectionRangeParams -import org.eclipse.lsp4j.SemanticTokens import org.eclipse.lsp4j.SemanticTokensLegend -import org.eclipse.lsp4j.SemanticTokensParams +import org.eclipse.lsp4j.SemanticTokensServerFull import org.eclipse.lsp4j.SemanticTokensWithRegistrationOptions import org.eclipse.lsp4j.ServerCapabilities -import org.eclipse.lsp4j.SignatureHelp +import org.eclipse.lsp4j.SetTraceParams import org.eclipse.lsp4j.SignatureHelpOptions -import org.eclipse.lsp4j.SignatureHelpParams -import org.eclipse.lsp4j.SignatureInformation import org.eclipse.lsp4j.SymbolInformation -import org.eclipse.lsp4j.TextDocumentSyncKind -import org.eclipse.lsp4j.TextEdit -import org.eclipse.lsp4j.TypeDefinitionParams -import org.eclipse.lsp4j.TypeHierarchyItem -import org.eclipse.lsp4j.TypeHierarchyPrepareParams -import org.eclipse.lsp4j.TypeHierarchySubtypesParams -import org.eclipse.lsp4j.TypeHierarchySupertypesParams import org.eclipse.lsp4j.WatchKind +import org.eclipse.lsp4j.WorkDoneProgressCancelParams +import org.eclipse.lsp4j.WorkDoneProgressParams +import org.eclipse.lsp4j.WorkspaceDiagnosticParams +import org.eclipse.lsp4j.WorkspaceDiagnosticReport import org.eclipse.lsp4j.WorkspaceEdit +import org.eclipse.lsp4j.WorkspaceFoldersOptions +import org.eclipse.lsp4j.WorkspaceServerCapabilities import org.eclipse.lsp4j.WorkspaceSymbol +import org.eclipse.lsp4j.WorkspaceSymbolOptions import org.eclipse.lsp4j.WorkspaceSymbolParams +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException import org.eclipse.lsp4j.jsonrpc.messages.Either -import org.eclipse.lsp4j.jsonrpc.messages.Either3 +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode import org.eclipse.lsp4j.jsonrpc.services.JsonRequest import org.eclipse.lsp4j.services.LanguageClient import org.eclipse.lsp4j.services.LanguageClientAware @@ -101,20 +63,29 @@ import org.eclipse.lsp4j.services.TextDocumentService import org.eclipse.lsp4j.services.WorkspaceService import org.slf4j.LoggerFactory import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.AdapterCapability import org.xvm.lsp.adapter.FormattingConfig +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkBuildModel +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkLibraries +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.XdkSources import org.xvm.lsp.model.Diagnostic -import org.xvm.lsp.model.SymbolInfo -import org.xvm.lsp.model.fmt -import org.xvm.lsp.model.fromLsp import org.xvm.lsp.model.toLsp -import org.xvm.lsp.model.toRange import org.xvm.lsp.treesitter.SemanticTokenLegend +import org.xvm.lsp.util.ExecutionTrace +import org.xvm.lsp.util.ProgressLabels +import org.xvm.lsp.util.ServerLogs +import java.io.IOException +import java.lang.management.ManagementFactory import java.net.URI import java.nio.file.Path import java.util.Properties import java.util.concurrent.CompletableFuture -import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicReference import kotlin.time.measureTimedValue /** @@ -122,19 +93,20 @@ import kotlin.time.measureTimedValue * * ## Implementation Status * - * All LSP methods are wired up to call the adapter and log their invocations. - * The actual implementation depends on the adapter: + * All LSP methods are wired up to call the adapter and log their invocations. The actual + * implementation depends on the adapter: * * - **MockAdapter**: Basic regex-based parsing, most features log "not implemented" - * - **TreeSitterAdapter**: Syntax-aware features (hover, completion, definition, references, symbols, folding, highlights) - * - **XdkAdapter**: (future) Full semantic features via XDK compiler + * - **TreeSitterAdapter**: Syntax-aware features (hover, completion, definition, references, + * symbols, folding, highlights) + * - **XdkAdapter**: Compiler diagnostics and semantic features via the embedding API * * ## Backend Selection * * Select backend at build time: `./gradlew :lang:lsp-server:build -Plsp.adapter=treesitter` * * @see org.xvm.lsp.adapter.Adapter - * @see org.xvm.lsp.adapter.TreeSitterAdapter + * @see org.xvm.lsp.adapter.treesitter.TreeSitterAdapter */ @Suppress("LoggingSimilarMessage") class XtcLanguageServer( @@ -143,16 +115,215 @@ class XtcLanguageServer( ) : LanguageServer, LanguageClientAware, AutoCloseable { - private var client: LanguageClient? = null + private val connectedClient = AtomicReference() + private val client: LanguageClient? + get() = connectedClient.get() - @Suppress("unused") - private var initialized = false + private val refresh = ClientRefresh { client } + internal val readOnlyDocuments = ReadOnlyDocuments(adapter) { client } + internal val clientTrace = ClientTrace { client } + internal val partialResults = PartialResults(client = { client }) + + override fun setTrace(params: SetTraceParams): Unit = clientTrace.configure(params.value) + + private data class EditCapabilities( + val versioned: Boolean = false, + val renameFiles: Boolean = false, + val fileWatchers: Boolean = false, + val relativeWatchPatterns: Boolean = false, + val resourceWatchers: Boolean = false, + ) + + private val clientPresentation = AtomicReference(ClientPresentation()) + internal val presentation: ClientPresentation + get() = clientPresentation.get() + + private val editCapabilities = AtomicReference(EditCapabilities()) + private val resourceFileWatchers = ResourceFileWatchers() + private val progress = ConnectionProgress(client = { client }) + private val supportsProgress = AtomicBoolean() + private val clientReady = CompletableFuture() + private val indexing = CompletableFuture() + + internal fun observeQuery( + method: String, + params: WorkDoneProgressParams?, + result: CompletableFuture, + uri: String = "", + ): CompletableFuture { + val title = + when (method) { + "textDocument/references" -> "Finding references" + + "textDocument/rename", + "xtc/rename", + "xtc/renameFiles", + "workspace/willRenameFiles", + -> "Checking rename" + + "textDocument/codeAction" -> "Checking code actions" + + "workspace/diagnostic" -> "Checking workspace" + + else -> null + } + return if (title != null || params?.workDoneToken != null) { + val folders = compilerSettings.get().folders + val subject = + if (uri.isNotBlank()) { + ProgressLabels.source(uri, folders) + } else { + "Workspace: " + folders.take(2).joinToString(", ") { ProgressLabels.source(it) }.ifEmpty { "configured source graph" } + + if (folders.size > 2) " (+${folders.size - 2} more)" else "" + } + progress.track(title ?: method, params?.workDoneToken, result) { + val activity = (adapter as? XdkAdapter)?.compilerProgressDescription() + "$subject · ${activity?.let { "Compiler: $it" } ?: "Checking current source state"}" + } + } else { + result + } + } + + override fun cancelProgress(params: WorkDoneProgressCancelParams): Unit = progress.cancel(params.token) + + internal val supportsVersionedEdits: Boolean + get() = editCapabilities.get().versioned + + internal val supportsFileRenames: Boolean + get() = editCapabilities.get().renameFiles + + private data class DiagnosticCapabilities( + val pull: Boolean = false, + val related: Boolean = false, + val refresh: Boolean = false, + ) + + private val diagnosticCapabilities = AtomicReference(DiagnosticCapabilities()) + internal val usesPullDiagnostics: Boolean + get() = diagnosticCapabilities.get().pull + + internal val supportsRelatedDiagnostics: Boolean + get() = diagnosticCapabilities.get().related + + internal fun refreshDiagnostics() = refresh.request(ClientRefresh.Feature.DIAGNOSTICS) + + internal fun workspaceDiagnostics(params: WorkspaceDiagnosticParams): CompletableFuture = + textDocumentService.workspaceDiagnostics(params) private val textDocumentService = XtcTextDocumentService(this, adapter) private val workspaceService = XtcWorkspaceService(this, adapter) - private val closed = AtomicBoolean() private val shutdownRequested = AtomicBoolean() + /** + * One immutable context per configuration request; late replies cannot replace newer settings. + */ + private data class CompilerSettings( + val folders: List = emptyList(), + val canRequest: Boolean = false, + val revision: Long = 0, + val closed: Boolean = false, + ) + + private data class ResolveCapabilities( + val completionDocumentation: Boolean = false, + val actionEdit: Boolean = false, + val lensCommand: Boolean = false, + val linkTarget: Boolean = false, + val hintTooltip: Boolean = false, + val symbolRange: Boolean = false, + ) + + private val documentSynchronization = AtomicReference(DocumentSynchronization()) + internal val synchronization: DocumentSynchronization + get() = documentSynchronization.get() + + private val resolveCapabilities = AtomicReference(ResolveCapabilities()) + internal val resolvesCompletionDocumentation: Boolean + get() = resolveCapabilities.get().completionDocumentation + + internal val resolvesCodeActionEdit: Boolean + get() = resolveCapabilities.get().actionEdit + + internal val resolvesCodeLensCommand: Boolean + get() = resolveCapabilities.get().lensCommand + + internal val resolvesDocumentLinkTarget: Boolean + get() = resolveCapabilities.get().linkTarget + + internal val resolvesInlayHintTooltip: Boolean + get() = resolveCapabilities.get().hintTooltip + + internal val resolvesWorkspaceSymbolRange: Boolean + get() = resolveCapabilities.get().symbolRange + + internal fun applyEdit(params: ApplyWorkspaceEditParams): CompletableFuture { + val current = + client?.takeUnless { compilerSettings.get().closed } + ?: return CompletableFuture.failedFuture( + IllegalStateException("Language client disconnected"), + ) + return current.applyEdit(params) + } + + internal fun executeCodeAction(params: ExecuteCommandParams): CompletableFuture = textDocumentService.executeCodeAction(params) + + internal fun workspaceSymbols( + params: WorkspaceSymbolParams, + ): CompletableFuture, List>> = textDocumentService.workspaceSymbols(params) + + internal fun resolveWorkspaceSymbol(symbol: WorkspaceSymbol): CompletableFuture = + textDocumentService.resolveWorkspaceSymbol(symbol) + + private data class TokenCapabilities( + val range: Boolean = false, + val delta: Boolean = false, + val refresh: Boolean = false, + ) + + private val tokenCapabilities = AtomicReference(TokenCapabilities()) + + internal fun requireSemanticTokenRequest(delta: Boolean) { + val options = tokenCapabilities.get() + if (!(if (delta) options.delta else options.range)) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.MethodNotFound, + "Semantic token operation was not negotiated", + null, + ), + ) + } + } + + internal fun refreshSemanticFeatures() = + refresh.request( + ClientRefresh.Feature.TOKENS, + ClientRefresh.Feature.INLAYS, + ClientRefresh.Feature.LENSES, + ClientRefresh.Feature.FOLDING, + ) + + private val fileOperationCapabilities = + AtomicReference(null) + + internal fun willRenameFiles(params: RenameFilesParams): CompletableFuture = + if (fileOperationCapabilities.get()?.willRename == true) { + textDocumentService.renameFiles(params) + } else { + CompletableFuture.failedFuture( + ResponseErrorException( + ResponseError( + ResponseErrorCode.MethodNotFound, + "File rename participation was not negotiated", + null, + ), + ), + ) + } + + private val compilerSettings = AtomicReference(CompilerSettings()) + companion object { private val logger = LoggerFactory.getLogger(XtcLanguageServer::class.java) private const val SEMANTIC_TOKENS_SYSTEM_PROPERTY = "xtc.lsp.semanticTokens" @@ -160,7 +331,9 @@ class XtcLanguageServer( private fun loadBuildInfo(): Properties = Properties().apply { - XtcLanguageServer::class.java.getResourceAsStream("/lsp-version.properties")?.use { load(it) } + XtcLanguageServer::class.java.getResourceAsStream("/lsp-version.properties")?.use { + load(it) + } } } @@ -175,23 +348,45 @@ class XtcLanguageServer( ).toBoolean() /** - * Editor-provided formatting configuration, received via `workspace/configuration`. - * This is populated after initialization by [requestFormattingConfig] and updated - * when the client sends `workspace/didChangeConfiguration`. + * Editor-provided formatting configuration, received via `workspace/configuration`. This is + * populated after initialization by [requestFormattingConfig] and updated when the client sends + * `workspace/didChangeConfiguration`. * * @see FormattingConfig.resolve */ - @Volatile - var editorFormattingConfig: FormattingConfig? = null - private set + private val formattingState = EditorFormattingState() + val editorFormattingConfig: FormattingConfig? + get() = formattingState.config + + fun refreshPresentation(): Unit = refresh.request(ClientRefresh.Feature.INLAYS, ClientRefresh.Feature.LENSES) + + /** Read the document's setting without retaining a second mutable configuration cache. */ + internal fun referenceCodeLens(uri: String): CompletableFuture { + val connection = client + if (!presentation.workspaceConfiguration || connection == null) return CompletableFuture.completedFuture(true) + val item = + ConfigurationItem().apply { + section = "xtc.codeLens" + scopeUri = uri + } + return CompletableFuture + .supplyAsync { connection.configuration(ConfigurationParams(listOf(item))) } + .thenCompose { it } + .orTimeout(10, SECONDS) + .thenApply { CodeLensSettings.references(it?.firstOrNull()) } + .exceptionally { failure -> + logger.warn("Reference CodeLens setting unavailable: {}", failure.message) + false + } + } /** * Helper to handle LSP requests with consistent logging and async execution. * - * @param method The name of the LSP method (e.g., "textDocument/hover") + * @param method The name of the LSP method (e.g., "textDocument/hover") * @param logParams A string describing the input parameters for logging * @param logResult A function that returns a string describing the result for logging - * @param block The actual implementation to execute + * @param block The actual implementation to execute */ fun supplyAsync( method: String, @@ -200,26 +395,182 @@ class XtcLanguageServer( block: () -> R, ): CompletableFuture { logger.info("{}: {}", method, logParams) + val trace = ExecutionTrace.current() return CompletableFuture.supplyAsync { - val (result, elapsed) = measureTimedValue { block() } + val (result, elapsed) = measureTimedValue { ExecutionTrace.within(trace, block) } logger.info("{}: {} in {}", method, logResult(result), elapsed) result } } override fun connect(client: LanguageClient) { - this.client = client + connectedClient.set(client) logger.info("connect: connected to language client") } override fun initialize(params: InitializeParams): CompletableFuture { + supportsProgress.set(params.capabilities?.window?.workDoneProgress == true) + clientPresentation.set(ClientPresentation.read(params)) + clientTrace.configure(params.trace) logServerBanner() logWorkspaceFolders(params) logClientCapabilities(params) + if (adapter is XdkAdapter) { + val folders = ClientPresentation.workspaceUris(params) + val settings = + CompilerSettings(folders, params.capabilities?.workspace?.configuration == true) + synchronized(compilerSettings) { + compilerSettings.set(settings) + try { + val raw = CompilerConfiguration.initial(params.initializationOptions) + val model = CompilerConfiguration.buildModel(raw) + if (applyLibraryConfiguration(raw, folders, model)) { + // All source and binary inputs were validated and installed together. + } else if (model != null) { + textDocumentService.refreshDependencies { + adapter.replaceBuildInputs(model.resolve()) + } + } else { + CompilerConfiguration + .modules(raw, folders) + ?.let(::replaceCompilerSourceModules) + } + } catch (e: IllegalArgumentException) { + return CompletableFuture.failedFuture( + ResponseErrorException( + ResponseError( + ResponseErrorCode.InvalidParams, + "Invalid ${CompilerConfiguration.SECTION}: ${e.message}", + null, + ), + ), + ) + } + } + } + + val pull = adapter is XdkAdapter && params.capabilities?.textDocument?.diagnostic != null + diagnosticCapabilities.set( + DiagnosticCapabilities( + pull, + pull && + params.capabilities + ?.textDocument + ?.diagnostic + ?.relatedDocumentSupport == true, + pull && params.capabilities + ?.workspace + ?.diagnostics + ?.refreshSupport == true, + ), + ) + val workspaceEdits = params.capabilities?.workspace?.workspaceEdit + editCapabilities.set( + EditCapabilities( + workspaceEdits?.documentChanges == true, + workspaceEdits?.resourceOperations?.contains("rename") == true, + params.capabilities + ?.workspace + ?.didChangeWatchedFiles + ?.dynamicRegistration == true, + params.capabilities + ?.workspace + ?.didChangeWatchedFiles + ?.relativePatternSupport == + true, + ), + ) + + try { + documentSynchronization.set(DocumentSynchronization.read(params)) + } catch (e: IllegalArgumentException) { + return CompletableFuture.failedFuture( + ResponseErrorException( + ResponseError(ResponseErrorCode.InvalidParams, e.message, null), + ), + ) + } + val textCapabilities = params.capabilities?.textDocument + resolveCapabilities.set( + ResolveCapabilities( + textCapabilities + ?.completion + ?.completionItem + ?.resolveSupport + ?.properties + ?.contains("documentation") == true, + presentation.actionLiterals && + textCapabilities?.codeAction?.dataSupport == true && + textCapabilities.codeAction.resolveSupport + ?.properties + ?.contains("edit") == + true, + textCapabilities?.codeLens?.let { + it.resolveSupport?.properties?.contains("command") != false + } == true, + textCapabilities?.documentLink != null, + textCapabilities + ?.inlayHint + ?.resolveSupport + ?.properties + ?.contains("tooltip") == + true, + params.capabilities + ?.workspace + ?.symbol + ?.resolveSupport + ?.properties + ?.contains("location.range") == true, + ), + ) + fileOperationCapabilities.set(params.capabilities?.workspace?.fileOperations) + val tokenRequests = + params.capabilities + ?.textDocument + ?.semanticTokens + ?.requests + val tokens = + semanticTokensEnabled && AdapterCapability.SEMANTIC_TOKENS in adapter.capabilities + tokenCapabilities.set( + TokenCapabilities( + tokens && tokenRequests?.range?.let { it.isRight || it.left == true } == true, + tokens && tokenRequests?.full?.right?.delta == true, + tokens && params.capabilities + ?.workspace + ?.semanticTokens + ?.refreshSupport == true, + ), + ) + + readOnlyDocuments.configure(params.capabilities?.workspace?.textDocumentContent != null) val capabilities = buildServerCapabilities() - initialized = true + val workspace = params.capabilities?.workspace + refresh.configure( + buildSet { + if (diagnosticCapabilities.get().refresh) add(ClientRefresh.Feature.DIAGNOSTICS) + if (tokenCapabilities.get().refresh) add(ClientRefresh.Feature.TOKENS) + if ( + workspace?.inlayHint?.refreshSupport == true && + AdapterCapability.INLAY_HINT in adapter.capabilities + ) { + add(ClientRefresh.Feature.INLAYS) + } + if ( + workspace?.codeLens?.refreshSupport == true && + AdapterCapability.CODE_LENS in adapter.capabilities + ) { + add(ClientRefresh.Feature.LENSES) + } + if ( + workspace?.foldingRange?.refreshSupport == true && + AdapterCapability.FOLDING_RANGE in adapter.capabilities + ) { + add(ClientRefresh.Feature.FOLDING) + } + }, + ) logger.info("initialize: Ecstasy Language Server initialized") // Health check before workspace indexing @@ -229,13 +580,12 @@ class XtcLanguageServer( } else { // Extract workspace folder paths and initialize workspace index val workspaceFolders = - params.workspaceFolders - ?.mapNotNull { folder -> - runCatching { Path.of(URI(folder.uri)).toString() } - .onFailure { logger.warn("initialize: invalid workspace folder URI: {}", folder.uri) } - .getOrNull() - } - ?: emptyList() + ClientPresentation.workspaceUris(params).mapNotNull { uri -> + runCatching { Path.of(URI(uri)).toString() } + .onFailure { + logger.warn("initialize: invalid workspace folder URI: {}", uri) + }.getOrNull() + } // Extra source roots (XDK source trees, etc.) from init options, sysprop, or env. // Lets the indexer find modules whose sources live outside the user's open project. @@ -243,13 +593,36 @@ class XtcLanguageServer( val folders = (workspaceFolders + extraRoots).distinct() if (folders.isNotEmpty()) { - adapter.initializeWorkspace(folders) { message, percent -> - logger.info("initialize: workspace indexing: {} ({}%)", message, percent) + if (params.workDoneToken != null) { + progress.track("Indexing workspace", params.workDoneToken, indexing) + } else { + clientReady.thenRun { + progress.track("Indexing workspace", null, indexing) + } + } + try { + val scan = + adapter.initializeWorkspaceAsync(folders) { message, percent -> + logger.info( + "initialize: workspace indexing: {} ({}%)", + message, + percent, + ) + progress.report(indexing, message, percent) + } + indexing.whenComplete { _, failure -> if (failure != null) scan.cancel(false) } + scan.whenComplete { _, failure -> + if (failure == null) { + indexing.complete(Unit) + } else { + indexing.completeExceptionally(failure) + } + } + } catch (failure: Throwable) { + indexing.completeExceptionally(failure) + throw failure } } - - // Register file watcher for *.x files (dynamic registration) - registerFileWatcher() } return CompletableFuture.completedFuture(InitializeResult(capabilities)) @@ -258,76 +631,175 @@ class XtcLanguageServer( /** * LSP: initialized notification. * - * Called after the client sends the `initialized` notification, signaling that the - * handshake is complete and the server can send requests to the client. - * We use this to pull formatting configuration from the client via `workspace/configuration`. + * Called after the client sends the `initialized` notification, signaling that the handshake is + * complete and the server can send requests to the client. We use this to pull formatting + * configuration from the client via `workspace/configuration`. */ override fun initialized(params: InitializedParams?) { + progress.initialized(supportsProgress.get()) + clientReady.complete(null) + refresh.initialized() + readOnlyDocuments.initialized() logger.info("initialized: handshake complete, requesting editor configuration") + if ( + editCapabilities + .getAndUpdate { + it.copy( + fileWatchers = false, + resourceWatchers = it.resourceWatchers || it.fileWatchers, + ) + }.fileWatchers + ) { + registerFileWatcher() + updateResourceWatchers() + } requestFormattingConfig() + requestCompilerConfig() + } + + /** Apply explicit notification settings, or pull them from configuration-capable clients. */ + fun changeCompilerConfig(raw: Any?) { + if (adapter !is XdkAdapter) return + val value = + try { + if (CompilerConfiguration.presentationOnly(raw)) return + CompilerConfiguration.changed(raw) + } catch (e: IllegalArgumentException) { + nextCompilerSettings() + reportCompilerConfigError(e) + return + } + val settings = nextCompilerSettings() + if (value == null) requestCompilerConfig(settings) else applyCompilerConfig(value, settings) + } + + private fun nextCompilerSettings(): CompilerSettings = + synchronized(compilerSettings) { + compilerSettings.updateAndGet { it.copy(revision = it.revision + 1) } + } + + private fun requestCompilerConfig(settings: CompilerSettings = nextCompilerSettings()) { + if (adapter !is XdkAdapter) return + val currentClient = client ?: return + if (settings.closed || !settings.canRequest) return + currentClient + .configuration( + ConfigurationParams( + listOf(ConfigurationItem().apply { section = CompilerConfiguration.SECTION }), + ), + ).thenAccept { values -> applyCompilerConfig(values?.firstOrNull(), settings) } + .exceptionally { failure -> + logger.warn( + "workspace/configuration: compiler settings request failed: {}", + failure.message, + ) + null + } + } + + private fun applyCompilerConfig( + raw: Any?, + settings: CompilerSettings, + ) { + synchronized(compilerSettings) { + if (settings.closed || compilerSettings.get() !== settings) return + try { + val model = CompilerConfiguration.buildModel(raw) + if (applyLibraryConfiguration(raw, settings.folders, model)) { + updateResourceWatchers() + } else if (model != null) { + val inputs = model.resolve() + textDocumentService.refreshDependencies { + (adapter as XdkAdapter).replaceBuildInputs(inputs) + } + updateResourceWatchers() + } else if (CompilerConfiguration.automatic(raw)) { + textDocumentService.refreshDependencies { + (adapter as XdkAdapter).discoverSourceModules() + } + updateResourceWatchers() + } else { + CompilerConfiguration + .modules(raw, settings.folders) + ?.let(::replaceCompilerSourceModules) + } + } catch (e: IllegalArgumentException) { + reportCompilerConfigError(e) + } + } + } + + private fun applyLibraryConfiguration( + raw: Any?, + folders: List, + model: XdkBuildModel?, + ): Boolean { + val libraries = CompilerLibraries.read(raw, folders) ?: return false + val compiler = adapter as XdkAdapter + val explicit = CompilerConfiguration.modules(raw, folders) + val evaluated = + model?.resolve() + ?: if (libraries.modulePath == null) CompilerConfiguration.buildModel(raw, includeExplicit = true)?.resolve() else null + val inputs = + XdkBuildModel.Inputs( + explicit ?: if (model != + null + ) { + evaluated!!.modules + } else { + compiler.sourceModuleInputs() + }, + evaluated?.binaries.orEmpty(), + ) + val replacement = inputs.copy(binaries = libraries.resolve(inputs.binaries)) + val automatic = model == null && explicit == null && CompilerConfiguration.automatic(raw) + textDocumentService.refreshDependencies { + compiler.replaceBuildInputs(replacement, automatic) + if (automatic) compiler.discoverSourceModules() else emptySet() + } + return true + } + + private fun reportCompilerConfigError(failure: IllegalArgumentException) { + val message = + "Invalid ${CompilerConfiguration.SECTION}; previous source configuration retained: ${failure.message}" + logger.warn(message) + client?.showMessage(MessageParams(MessageType.Error, message)) } /** * Request formatting configuration from the client via `workspace/configuration`. * - * Sends a request for section `"xtc.formatting"`. The client (e.g., [XtcLanguageClient] - * in IntelliJ) responds with IntelliJ Code Style settings. The response is parsed into - * an [FormattingConfig] and stored as [editorFormattingConfig]. + * Sends a request for section `"xtc.formatting"`. The client (e.g., `XtcLanguageClient` in + * IntelliJ) responds with IntelliJ Code Style settings. The response is parsed into an + * [FormattingConfig] and stored as [editorFormattingConfig]. */ fun requestFormattingConfig() { + if (!presentation.workspaceConfiguration || compilerSettings.get().closed) return val c = client ?: return + val revision = formattingState.request() val item = ConfigurationItem().apply { section = "xtc.formatting" } - logger.info("workspace/configuration: requesting section='{}'", item.section) c .configuration(ConfigurationParams(listOf(item))) .thenAccept { results -> - logger.info("workspace/configuration: raw response={}", results) - val config = results?.firstOrNull() - val formattingConfig = parseFormattingConfig(config) - if (formattingConfig != null) { - editorFormattingConfig = formattingConfig - adapter.editorFormattingConfig = formattingConfig - logger.info( - "workspace/configuration: effective formatting config from client -> {} (fallback if absent would be request LSP FormattingOptions, then defaults)", - formattingConfig, - ) - } else { + if ( + formattingState.accept(revision, results?.firstOrNull()) { + adapter.editorFormattingConfig = it + } + ) { logger.info( - "workspace/configuration: no usable formatting config from client (type={}); effective config will come from per-request LSP FormattingOptions or XTC defaults", - config?.javaClass?.name ?: "null", + "workspace/configuration: effective formatting config={}", + editorFormattingConfig, ) } - }.exceptionally { ex -> - logger.warn("initialized: failed to get formatting config: {}", ex.message) + }.exceptionally { failure -> + logger.warn( + "Invalid or unavailable formatting configuration; previous values retained: {}", + failure.message, + ) null } } - private fun parseFormattingConfig(raw: Any?): FormattingConfig? { - if (!LspJsonOptions.isObject(raw)) return null - - val indentSize = LspJsonOptions.int(raw, "indentSize") ?: 4 - val continuationIndentSize = LspJsonOptions.int(raw, "continuationIndentSize") ?: 8 - val insertSpaces = LspJsonOptions.boolean(raw, "insertSpaces") ?: true - val maxLineWidth = LspJsonOptions.int(raw, "maxLineWidth") ?: 120 - val tabSize = LspJsonOptions.int(raw, "tabSize") - logger.info( - "workspace/configuration: parsed config type={} indentSize={} continuationIndentSize={} tabSize={} insertSpaces={} maxLineWidth={}", - raw?.javaClass?.name, - indentSize, - continuationIndentSize, - tabSize, - insertSpaces, - maxLineWidth, - ) - return FormattingConfig( - indentSize = indentSize, - continuationIndentSize = continuationIndentSize, - insertSpaces = insertSpaces, - maxLineWidth = maxLineWidth, - ) - } - private fun logServerBanner() { val pid = ProcessHandle.current().pid() logger.info("initialize: ========================================") @@ -347,95 +819,42 @@ class XtcLanguageServer( } /** - * Log which LSP capabilities the client advertises. - * - * NOTE: The chained ?. calls look verbose but are necessary -- LSP4J is a Java library - * where all these capability fields are nullable. This is idiomatic for Java interop. - * - * ## LSP Capabilities Reference - * - * Each capability is annotated with: - * - What it does for the end user - * - What adapter level is needed to implement it properly: - * - **mock**: regex-based, no parse tree needed - * - **treesitter**: requires syntax tree (structural parsing) - * - **compiler**: requires XTC compiler integration (type resolution, semantic analysis) - * - * ### Currently implemented (server advertises these): - * - * | Capability | Description | Adapter | - * |--------------------|--------------------------------------------------------|------------| - * | hover | Tooltip with type/doc info on mouse-over | treesitter | - * | completion | Code completion suggestions (., :, < triggers) | treesitter | - * | definition | Go-to-definition (jump to where a symbol is declared) | treesitter | - * | references | Find all references to a symbol in the current file | treesitter | - * | documentSymbol | Outline view / breadcrumbs (classes, methods, fields) | treesitter | - * | formatting | Whole-document code formatting | treesitter | - * | rangeFormatting | Format a selected range of code | treesitter | - * | rename | Rename a symbol across the file | treesitter | - * | codeAction | Quick fixes and refactorings (lightbulb menu) | treesitter | - * | documentHighlight | Highlight other occurrences of symbol under cursor | treesitter | - * | selectionRange | Smart expand/shrink selection based on syntax | treesitter | - * | foldingRange | Code folding regions (classes, methods, blocks) | treesitter | - * | inlayHint | Inline hints (parameter names, inferred types) | treesitter | - * - * ### Not yet implemented: - * - * | Capability | Description | Adapter needed | - * |--------------------|--------------------------------------------------------|-----------------| - * | signatureHelp | Parameter hints while typing a method call | treesitter | - * | documentLink | Clickable links in code (import paths, URLs) | treesitter | - * | declaration | Go-to-declaration (vs definition, for interfaces) | compiler | - * | typeDefinition | Jump to the type definition of a variable | compiler (types)| - * | implementation | Find implementations of an interface/abstract method | compiler (types)| - * | codeLens | Inline actionable info above functions (run, #refs) | compiler | - * | colorProvider | Color swatches in editor for color literals | mock | - * | onTypeFormatting | Auto-format as you type (e.g., indent after {) | treesitter | - * | typeHierarchy | Show super/subtypes of a class (hierarchy tree) | compiler (full) | - * | callHierarchy | Show callers/callees of a function (call tree) | compiler (full) | - * | semanticTokens | Token-level semantic highlighting (types vs vars) | treesitter | - * | moniker | Cross-project symbol identity for indexing | compiler (full) | - * | linkedEditingRange | Edit matching tags/names simultaneously | treesitter | - * | inlineValue | Show variable values inline during debugging | compiler (full) | - * | diagnostic | Pull-based diagnostics (vs push via publishDiagnostics)| compiler | - * | workspaceSymbol | Search symbols across all files in workspace | compiler (sym) | + * Log client feature declarations, independently of the selected adapter. Provider availability + * is defined by [AdapterCapability] and [buildServerCapabilities]; detailed implementation + * limits live in lang/doc/plans/plan-ide-integration.md. */ private fun logClientCapabilities(params: InitializeParams) { val td = params.capabilities?.textDocument val supportedFeatures = listOfNotNull( - // Implemented (server advertises these) - td?.hover?.let { "hover" }, // treesitter: tooltip info - td?.completion?.let { "completion" }, // treesitter: code completions - td?.definition?.let { "definition" }, // treesitter: go-to-definition - td?.references?.let { "references" }, // treesitter: find references - td?.documentSymbol?.let { "documentSymbol" }, // treesitter: outline/breadcrumbs - td?.formatting?.let { "formatting" }, // treesitter: format document - td?.rename?.let { "rename" }, // treesitter: rename symbol - td?.codeAction?.let { "codeAction" }, // treesitter: quick fixes - td?.semanticTokens?.let { "semanticTokens" }, // compiler(sym): semantic highlighting - td?.documentHighlight?.let { "documentHighlight" }, // treesitter: highlight occurrences - td?.selectionRange?.let { "selectionRange" }, // treesitter: smart selection - td?.foldingRange?.let { "foldingRange" }, // treesitter: code folding - td?.signatureHelp?.let { "signatureHelp" }, // treesitter: parameter hints - td?.inlayHint?.let { "inlayHint" }, // treesitter: inline hints - td?.documentLink?.let { "documentLink" }, // treesitter: clickable links - td?.onTypeFormatting?.let { "onTypeFormatting" }, // treesitter: auto-indent - // Not yet implemented (uncomment as we add support) - // td?.synchronization?.let { "synchronization" }, // built-in: doc sync events - // td?.rangeFormatting?.let { "rangeFormatting" }, // treesitter: format selection - // td?.declaration?.let { "declaration" }, // compiler: go-to-declaration - // td?.typeDefinition?.let { "typeDefinition" }, // compiler(types): jump to type - // td?.implementation?.let { "implementation" }, // compiler(types): find impls - td?.codeLens?.let { "codeLens" }, // treesitter: run/compile actions on modules - // td?.colorProvider?.let { "colorProvider" }, // mock: color swatches - // td?.publishDiagnostics?.let { "publishDiagnostics" }, // compiler: error reporting - // td?.typeHierarchy?.let { "typeHierarchy" }, // compiler(full): type tree - // td?.callHierarchy?.let { "callHierarchy" }, // compiler(full): call tree - // td?.moniker?.let { "moniker" }, // compiler(full): cross-project IDs - td?.linkedEditingRange?.let { "linkedEditingRange" }, // treesitter: linked edits - // td?.inlineValue?.let { "inlineValue" }, // compiler(full): debug values - // td?.diagnostic?.let { "diagnostic" }, // compiler: pull diagnostics + td?.hover?.let { "hover" }, + td?.completion?.let { "completion" }, + td?.definition?.let { "definition" }, + td?.declaration?.let { "declaration" }, + td?.typeDefinition?.let { "typeDefinition" }, + td?.implementation?.let { "implementation" }, + td?.moniker?.let { "moniker" }, + td?.references?.let { "references" }, + td?.documentSymbol?.let { "documentSymbol" }, + td?.formatting?.let { "formatting" }, + td?.rangeFormatting?.let { "rangeFormatting" }, + td?.onTypeFormatting?.let { "onTypeFormatting" }, + td?.rename?.let { "rename" }, + td?.codeAction?.let { "codeAction" }, + td?.semanticTokens?.let { "semanticTokens" }, + td?.documentHighlight?.let { "documentHighlight" }, + td?.selectionRange?.let { "selectionRange" }, + td?.foldingRange?.let { "foldingRange" }, + td?.signatureHelp?.let { "signatureHelp" }, + td?.inlayHint?.let { "inlayHint" }, + td?.documentLink?.let { "documentLink" }, + td?.codeLens?.let { "codeLens" }, + td?.typeHierarchy?.let { "typeHierarchy" }, + td?.callHierarchy?.let { "callHierarchy" }, + td?.linkedEditingRange?.let { "linkedEditingRange" }, + td?.synchronization?.let { "synchronization" }, + td?.publishDiagnostics?.let { "publishDiagnostics" }, + td?.diagnostic?.let { "diagnostic" }, ) if (supportedFeatures.isNotEmpty()) { logger.info("initialize: client capabilities: {}", supportedFeatures.joinToString(", ")) @@ -445,25 +864,39 @@ class XtcLanguageServer( /** * Build the server capabilities that we advertise to the client. * - * Each capability here corresponds to an LSP method that the server handles. - * See [logClientCapabilities] for a full reference table of all LSP capabilities, - * what they do, and what adapter level is required. + * Each provider corresponds to a method handled by the selected adapter. Optional response + * fields are negotiated separately through [ClientPresentation] and the resolve options. */ private fun buildServerCapabilities(): ServerCapabilities = ServerCapabilities().apply { - // Text document sync - Full means the client sends the entire document on each change. - // Incremental sync (sending only deltas) is more efficient but requires diffing logic. - textDocumentSync = Either.forLeft(TextDocumentSyncKind.Full) + positionEncoding = "utf-16" + if (adapter is XdkAdapter) experimental = mapOf("xtcRenameProposal" to 1, "xtcFileMoveProposal" to 1) + if (usesPullDiagnostics) { + diagnosticProvider = + DiagnosticRegistrationOptions(true, true).apply { + identifier = "xtc" + workDoneProgress = true + } + } + textDocumentSync = Either.forRight(synchronization.capabilities()) // --- Core navigation (treesitter) --- hoverProvider = Either.forLeft(true) completionProvider = CompletionOptions().apply { triggerCharacters = listOf(".", ":", "<") - resolveProvider = false + resolveProvider = resolvesCompletionDocumentation } + if (presentation.inlineCompletion && AdapterCapability.INLINE_COMPLETION in adapter.capabilities) { + inlineCompletionProvider = Either.forLeft(true) + } definitionProvider = Either.forLeft(true) - referencesProvider = Either.forLeft(true) + referencesProvider = + if (adapter is XdkAdapter) { + Either.forRight(ReferenceOptions().apply { workDoneProgress = true }) + } else { + Either.forLeft(true) + } documentSymbolProvider = Either.forLeft(true) // --- Structural features (treesitter) --- @@ -472,32 +905,69 @@ class XtcLanguageServer( foldingRangeProvider = Either.forLeft(true) // --- Editing features (treesitter) --- - renameProvider = Either.forRight(RenameOptions().apply { prepareProvider = true }) - codeActionProvider = Either.forLeft(true) + renameProvider = + Either.forRight( + RenameOptions().apply { + prepareProvider = true + workDoneProgress = true + }, + ) + codeActionProvider = + if (!presentation.codeActions) { + Either.forLeft(false) + } else if (resolvesCodeActionEdit) { + Either.forRight(CodeActionOptions().apply { resolveProvider = true }) + } else { + Either.forLeft(true) + } + if ( + !presentation.actionLiterals && + presentation.applyEdit && + AdapterCapability.CODE_ACTION in adapter.capabilities + ) { + executeCommandProvider = + ExecuteCommandOptions(listOf(ClientPresentation.APPLY_CODE_ACTION)) + } documentFormattingProvider = Either.forLeft(true) - documentRangeFormattingProvider = Either.forLeft(true) + documentRangeFormattingProvider = + Either.forRight(DocumentRangeFormattingOptions().apply { rangesSupport = true }) documentOnTypeFormattingProvider = DocumentOnTypeFormattingOptions("\n").apply { moreTriggerCharacter = listOf("}", ";", ")") } - // inlayHintProvider = Either.forLeft(true) // not implemented in TreeSitterAdapter yet + if (AdapterCapability.INLAY_HINT in adapter.capabilities) { + inlayHintProvider = + if (resolvesInlayHintTooltip) { + Either.forRight( + InlayHintRegistrationOptions().apply { resolveProvider = true }, + ) + } else { + Either.forLeft(true) + } + } // documentLinkProvider: URLs in comments / string literals. // See TreeSitterAdapter.getDocumentLinks for the matcher. - documentLinkProvider = DocumentLinkOptions() + documentLinkProvider = DocumentLinkOptions(resolvesDocumentLinkTarget) - signatureHelpProvider = SignatureHelpOptions(listOf("(", ",")) + signatureHelpProvider = + SignatureHelpOptions( + if (adapter is XdkAdapter) listOf("(", ",", "[") else listOf("(", ","), + ) - // Semantic tokens: enabled by default. Disable with -Plsp.semanticTokens=false if needed. - if (semanticTokensEnabled) { + // Semantic tokens: enabled by default. Disable with -Plsp.semanticTokens=false if + // needed. + if ( + semanticTokensEnabled && AdapterCapability.SEMANTIC_TOKENS in adapter.capabilities + ) { logger.info( "semantic tokens ENABLED via {} ({} types, {} modifiers)", - System - .getProperty(SEMANTIC_TOKENS_SYSTEM_PROPERTY) - ?.let { "system property $SEMANTIC_TOKENS_SYSTEM_PROPERTY=$it" } - ?: System - .getenv(SEMANTIC_TOKENS_ENV) - ?.let { "environment $SEMANTIC_TOKENS_ENV=$it" } + System.getProperty(SEMANTIC_TOKENS_SYSTEM_PROPERTY)?.let { + "system property $SEMANTIC_TOKENS_SYSTEM_PROPERTY=$it" + } + ?: System.getenv(SEMANTIC_TOKENS_ENV)?.let { + "environment $SEMANTIC_TOKENS_ENV=$it" + } ?: "build property lsp.semanticTokens=${buildInfo.getProperty("lsp.semanticTokens", "true")}", SemanticTokenLegend.tokenTypes.size, SemanticTokenLegend.tokenModifiers.size, @@ -509,36 +979,149 @@ class XtcLanguageServer( SemanticTokenLegend.tokenTypes, SemanticTokenLegend.tokenModifiers, ) - full = Either.forLeft(true) + full = + if (tokenCapabilities.get().delta) { + Either.forRight(SemanticTokensServerFull(true)) + } else { + Either.forLeft(true) + } + if (tokenCapabilities.get().range) range = Either.forLeft(true) } } else { logger.warn( "semantic tokens DISABLED via {}", - System - .getProperty(SEMANTIC_TOKENS_SYSTEM_PROPERTY) - ?.let { "system property $SEMANTIC_TOKENS_SYSTEM_PROPERTY=$it" } - ?: System - .getenv(SEMANTIC_TOKENS_ENV) - ?.let { "environment $SEMANTIC_TOKENS_ENV=$it" } + System.getProperty(SEMANTIC_TOKENS_SYSTEM_PROPERTY)?.let { + "system property $SEMANTIC_TOKENS_SYSTEM_PROPERTY=$it" + } + ?: System.getenv(SEMANTIC_TOKENS_ENV)?.let { + "environment $SEMANTIC_TOKENS_ENV=$it" + } ?: "build property lsp.semanticTokens=${buildInfo.getProperty("lsp.semanticTokens", "true")}", ) } // --- Workspace features --- - workspaceSymbolProvider = Either.forLeft(true) + workspaceSymbolProvider = + if (resolvesWorkspaceSymbolRange) { + Either.forRight(WorkspaceSymbolOptions(true)) + } else { + Either.forLeft(true) + } + if (adapter is XdkAdapter) { + workspace = + WorkspaceServerCapabilities().apply { + fileOperations = + FileOperationsServerCapabilities().apply { + val filters = + FileOperationOptions( + listOf( + FileOperationFilter( + FileOperationPattern("**/*.x"), + "file", + ), + FileOperationFilter( + FileOperationPattern("**").apply { + matches = "folder" + }, + "file", + ), + ), + ) + val client = fileOperationCapabilities.get() + if (client?.willRename == true && supportsVersionedEdits) { + willRename = filters + } + if (client?.willCreate == true) willCreate = filters + if (client?.willDelete == true) willDelete = filters + if (client?.didRename == true) didRename = filters + if (client?.didCreate == true) didCreate = filters + if (client?.didDelete == true) didDelete = filters + } + textDocumentContent = readOnlyDocuments.options() + workspaceFolders = + WorkspaceFoldersOptions().apply { + supported = true + changeNotifications = Either.forRight(true) + } + } + } // Code lenses: Run action on module declarations (TreeSitterAdapter) - codeLensProvider = CodeLensOptions(false) + codeLensProvider = CodeLensOptions(resolvesCodeLensCommand) - // Linked editing: rename-on-type for same-name identifiers (same-file, TreeSitterAdapter) + // Linked editing: rename-on-type for same-name identifiers (same-file, + // TreeSitterAdapter) linkedEditingRangeProvider = Either.forLeft(true) - // Not yet advertised (enable when implemented) - // declarationProvider = Either.forLeft(true) // compiler: go-to-declaration - // typeDefinitionProvider = Either.forLeft(true) // compiler(types): jump to type - // implementationProvider = Either.forLeft(true) // compiler(types): find implementations - // typeHierarchyProvider = Either.forLeft(true) // compiler(full): type tree - // callHierarchyProvider = Either.forLeft(true) // compiler(full): call tree + // Advertise only operations implemented by the selected backend. + if (AdapterCapability.HOVER !in adapter.capabilities) hoverProvider = null + if (AdapterCapability.COMPLETION !in adapter.capabilities) completionProvider = null + if (AdapterCapability.DEFINITION !in adapter.capabilities) definitionProvider = null + if (AdapterCapability.REFERENCES !in adapter.capabilities) referencesProvider = null + if (AdapterCapability.DOCUMENT_SYMBOL !in adapter.capabilities) { + documentSymbolProvider = null + } + if (AdapterCapability.DOCUMENT_HIGHLIGHT !in adapter.capabilities) { + documentHighlightProvider = null + } + if (AdapterCapability.SELECTION_RANGE !in adapter.capabilities) { + selectionRangeProvider = null + } + if (AdapterCapability.FOLDING_RANGE !in adapter.capabilities) { + foldingRangeProvider = null + } + if ( + AdapterCapability.RENAME !in adapter.capabilities || + (adapter is XdkAdapter && !supportsVersionedEdits) + ) { + renameProvider = null + } + if (AdapterCapability.CODE_ACTION !in adapter.capabilities) codeActionProvider = null + if (AdapterCapability.FORMATTING !in adapter.capabilities) { + documentFormattingProvider = null + } + if (AdapterCapability.RANGE_FORMATTING !in adapter.capabilities) { + documentRangeFormattingProvider = null + } + if (AdapterCapability.ON_TYPE_FORMATTING !in adapter.capabilities) { + documentOnTypeFormattingProvider = null + } + if (AdapterCapability.DOCUMENT_LINK !in adapter.capabilities) { + documentLinkProvider = null + } + if (AdapterCapability.SIGNATURE_HELP !in adapter.capabilities) { + signatureHelpProvider = null + } + if (AdapterCapability.WORKSPACE_SYMBOL !in adapter.capabilities) { + workspaceSymbolProvider = null + } + if (AdapterCapability.CODE_LENS !in adapter.capabilities) codeLensProvider = null + if (AdapterCapability.LINKED_EDITING !in adapter.capabilities) { + linkedEditingRangeProvider = null + } + + // Compiler semantic navigation. + if (AdapterCapability.DOCUMENT_COLOR in adapter.capabilities) { + colorProvider = Either.forLeft(true) + } + if (AdapterCapability.TYPE_DEFINITION in adapter.capabilities) { + typeDefinitionProvider = Either.forLeft(true) + } + if (AdapterCapability.DECLARATION in adapter.capabilities) { + declarationProvider = Either.forLeft(true) + } + if (AdapterCapability.IMPLEMENTATION in adapter.capabilities) { + implementationProvider = Either.forLeft(true) + } + if (AdapterCapability.MONIKER in adapter.capabilities) { + monikerProvider = Either.forLeft(true) + } + if (AdapterCapability.TYPE_HIERARCHY in adapter.capabilities) { + typeHierarchyProvider = Either.forLeft(true) + } + if (AdapterCapability.CALL_HIERARCHY in adapter.capabilities) { + callHierarchyProvider = Either.forLeft(true) + } } override fun shutdown(): CompletableFuture { @@ -548,11 +1131,25 @@ class XtcLanguageServer( return CompletableFuture.completedFuture(null) } - /** Release resources once, whether shutdown is explicit or the transport disconnects. */ + /** Release resources on both a protocol shutdown and an abrupt transport disconnect. */ override fun close() { - if (closed.compareAndSet(false, true)) { - initialized = false - adapter.close() + val alreadyClosed = + synchronized(compilerSettings) { + compilerSettings.getAndUpdate { it.copy(closed = true) }.closed + } + if (alreadyClosed) return + clientReady.cancel(false) + indexing.cancel(false) + formattingState.close() + resourceFileWatchers.close() + refresh.close() + readOnlyDocuments.close() + clientTrace.close() + progress.close() + partialResults.close() + editCapabilities.set(EditCapabilities()) + adapter.use { + textDocumentService.close() } } @@ -570,7 +1167,7 @@ class XtcLanguageServer( override fun getWorkspaceService(): WorkspaceService = workspaceService // ========================================================================= - // Custom XTC LSP Methods + // Custom Ecstasy LSP Methods // ========================================================================= // // LSP allows servers to define custom methods beyond the standard protocol. @@ -593,6 +1190,26 @@ class XtcLanguageServer( // // ========================================================================= + /** Effective immutable inputs for host configuration views and explicit override creation. */ + @JsonRequest("xtc/compilerSourceModules") + fun compilerSourceModules(): CompletableFuture> = + supplyAsync( + "xtc/compilerSourceModules", + "effective inputs", + ) { + (adapter as? XdkAdapter) + ?.effectiveSourceModules() + ?.map(::SourceModuleConfiguration) + .orEmpty() + } + + /** Hosts opting into this extension own persistence and undo of explicit graph replacements. */ + @JsonRequest("xtc/rename") + fun renameProposal(params: RenameParams): CompletableFuture = textDocumentService.renameProposal(params) + + @JsonRequest("xtc/renameFiles") + fun renameFilesProposal(params: RenameFilesParams): CompletableFuture = textDocumentService.renameFilesProposal(params) + /** * Custom health check method that clients can call to verify the server is working. * @@ -605,10 +1222,57 @@ class XtcLanguageServer( * * Usage from client: Send JSON-RPC request with method "xtc/health check" * - * NOTE: Called at runtime via JSON-RPC by LSP clients (e.g., IntelliJ plugin, VS Code extension) - * sending a request with method "xtc/health check". LSP4J dispatches via reflection. + * NOTE: Called at runtime via JSON-RPC by LSP clients (e.g., IntelliJ plugin, VS Code + * extension) sending a request with method "xtc/health check". LSP4J dispatches via reflection. */ @Suppress("unused") + @JsonRequest("xtc/languageServiceStatus") + fun languageServiceStatus(): CompletableFuture> = + CompletableFuture.completedFuture( + mapOf( + "adapter" to adapter.displayName, + "version" to version, + "pid" to ProcessHandle.current().pid(), + "runtime" to System.getProperty("java.runtime.version"), + "jvmOptions" to ManagementFactory.getRuntimeMXBean().inputArguments.filter { it.startsWith("-X") }, + "logs" to ServerLogs.status(), + "textSynchronization" to if (synchronization.incremental) "incremental" else "full", + "serverSaveFormatting" to + (synchronization.formatOnSave && synchronization.waitUntil), + "saveHookSupported" to synchronization.waitUntil, + "formatting" to editorFormattingConfig, + "semanticTokens" to + ( + semanticTokensEnabled && + AdapterCapability.SEMANTIC_TOKENS in adapter.capabilities + ), + "capabilities" to buildServerCapabilities(), + "bundledXdk" to + if (adapter is XdkAdapter) { + mapOf("readOnly" to true, "modules" to XdkLibraries.packagedResources) + } else { + null + }, + "compilerLibraries" to (adapter as? XdkAdapter)?.libraryConfiguration(), + "compilerQueue" to (adapter as? XdkAdapter)?.compilerQueueSnapshot(), + "heap" to + ManagementFactory.getMemoryMXBean().heapMemoryUsage.let { + mapOf( + "usedBytes" to it.used, + "committedBytes" to it.committed, + "maxBytes" to it.max, + ) + }, + ), + ) + + @Suppress("unused") // JSON-RPC entry point invoked by both editor plugins through LSP4J. + @JsonRequest("xtc/exportLogs") + fun exportLogs(): CompletableFuture> = + supplyAsync("xtc/exportLogs", "bounded server log archive") { + ServerLogs.export(languageServiceStatus().join()) + } + @JsonRequest("xtc/healthCheck") fun healthCheck(): CompletableFuture> = supplyAsync( @@ -622,14 +1286,15 @@ class XtcLanguageServer( "version" to version, "adapter" to adapter.displayName, "buildTime" to buildTime, - "message" to if (healthy) "Ecstasy Language Server is healthy" else "Health check failed", + "message" to + if (healthy) "Ecstasy Language Server is healthy" else "Health check failed", ) } /** - * Register a file watcher for `**/*.x` files via dynamic capability registration. - * This enables the client to notify us when XTC files are created, changed, or deleted - * on disk (outside of the editor), which we use to keep the workspace index up to date. + * Register source file watchers (and workspace resource changes in compiler mode). This enables + * the client to notify us when XTC files are created, changed, or deleted on disk (outside + * the editor), which we use to keep the workspace index up to date. */ private fun registerFileWatcher() { val currentClient = client ?: return @@ -637,7 +1302,7 @@ class XtcLanguageServer( DidChangeWatchedFilesRegistrationOptions( listOf( FileSystemWatcher( - Either.forLeft("**/*.x"), + Either.forLeft(if (adapter is XdkAdapter) "**/*" else "**/*.x"), WatchKind.Create + WatchKind.Change + WatchKind.Delete, ), ), @@ -648,20 +1313,114 @@ class XtcLanguageServer( "workspace/didChangeWatchedFiles", watcherOptions, ) - currentClient.registerCapability(RegistrationParams(listOf(registration))) - logger.info("initialize: registered file watcher for **/*.x") + currentClient.registerCapability(RegistrationParams(listOf(registration))).whenComplete { + _, + failure, + -> + if (failure == null) { + logger.info("initialized: registered file watcher for **/*.x") + } else { + logger.warn("initialized: client rejected file watcher registration", failure) + } + } } // ========================================================================= // Helper Methods // ========================================================================= + fun changeCompilerWorkspaceFolders( + added: List, + removed: List, + ) { + val compiler = adapter as? XdkAdapter ?: return + synchronized(compilerSettings) { + compilerSettings.updateAndGet { settings -> + settings.copy( + folders = (settings.folders.filterNot { it in removed } + added).distinct(), + revision = settings.revision + 1, + ) + } + } + try { + textDocumentService.refreshDependencies { + compiler.changeWorkspaceFolders(added, removed) + } + updateResourceWatchers() + } catch (failure: IllegalArgumentException) { + reportCompilerConfigError(failure) + } catch (failure: IOException) { + logger.warn("Workspace discovery failed; keeping previous graph: {}", failure.message) + } + } + + fun refreshCompilerDiscovery() { + val compiler = adapter as? XdkAdapter ?: return + try { + textDocumentService.refreshDependencies { compiler.refreshDiscoveredSources() } + updateResourceWatchers() + } catch (failure: IllegalArgumentException) { + reportCompilerConfigError(failure) + } catch (failure: IOException) { + logger.warn("Source discovery failed; keeping previous graph: {}", failure.message) + } + } + + private fun updateResourceWatchers() { + val compiler = adapter as? XdkAdapter ?: return + val currentClient = client ?: return + if (!editCapabilities.get().resourceWatchers) return + val folders = compilerSettings.get().folders.mapNotNull { XdkSources.file(it)?.toPath() } + val external = + compiler + .inputWatchRoots() + .filter { root -> + folders.none { root.toPath().startsWith(it) } + }.mapTo(linkedSetOf()) { it.toURI().toString() } + resourceFileWatchers.update( + currentClient, + external, + editCapabilities.get().relativeWatchPatterns, + ) + } + + fun refreshForFile(uri: String): Unit = textDocumentService.refreshForFile(uri) + + /** Host API; project discovery/configuration is separate from installing matching artifacts. */ + fun replaceCompilerDependencies(dependencies: List) { + val compiler = adapter as? XdkAdapter ?: error("Compiler dependencies require XdkAdapter") + textDocumentService.refreshDependencies { compiler.replaceDependencies(dependencies) } + } + + /** + * Host-supplied source roots/edges enable automatic dependency builds on editor/file events. + */ + fun replaceCompilerSourceModules(modules: List) { + val compiler = adapter as? XdkAdapter ?: error("Compiler source modules require XdkAdapter") + textDocumentService.refreshDependencies { compiler.replaceSourceModules(modules) } + updateResourceWatchers() + } + fun publishDiagnostics( uri: String, diagnostics: List, + version: Int? = null, ) { + if (usesPullDiagnostics) return val currentClient = client ?: return - val lspDiagnostics = diagnostics.map { it.toLsp() } - currentClient.publishDiagnostics(PublishDiagnosticsParams(uri, lspDiagnostics)) + val options = presentation + val lspDiagnostics = + diagnostics.map { + it.toLsp(uri).apply { + if (!options.diagnosticRelatedInformation) relatedInformation = null + } + } + currentClient.publishDiagnostics( + PublishDiagnosticsParams( + uri, + lspDiagnostics, + version.takeIf { options.diagnosticVersions }, + ), + ) } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt index 9551fec56c1..af597d88d5f 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt @@ -3,13 +3,15 @@ package org.xvm.lsp.server import org.eclipse.lsp4j.jsonrpc.Launcher -import org.eclipse.lsp4j.launch.LSPLauncher +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.RemoteEndpoint import org.eclipse.lsp4j.services.LanguageClient import org.slf4j.LoggerFactory import org.xvm.lsp.adapter.Adapter import org.xvm.lsp.adapter.mock.MockAdapter import org.xvm.lsp.adapter.treesitter.TreeSitterAdapter import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.util.ServerLogs import java.io.InputStream import java.io.OutputStream import java.lang.invoke.MethodHandles @@ -25,14 +27,15 @@ import kotlin.system.exitProcess * - For socket communication: `java -jar xtc-lsp.jar --socket 5007` * * Adapter Selection: - * - The adapter is selected at build time via: ./gradlew :lang:lsp-server:fatJar -Plsp.adapter=treesitter - * - Default is 'treesitter' (syntax-aware, requires native library bundled in JAR) + * - Select at startup with `java -Dxtc.lsp.adapter=treesitter -jar xtc-lsp.jar`. + * - Otherwise use the build default, configured with `-Plsp.adapter=compiler`. + * - The shipped default is 'compiler'; both real adapters are bundled in the same JAR. + * - Use 'compiler' for real diagnostics from the Ecstasy compiler and its bundled XDK libraries * - Use 'mock' for regex-based features (no native dependencies) * - * Important: This LSP server uses stdio for communication. All logging goes to stderr - * to keep stdout clean for the JSON-RPC protocol. + * Important: This LSP server uses stdio for communication. All logging goes to stderr to keep + * stdout clean for the JSON-RPC protocol. */ - private val logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()) // Static initializer runs before any SLF4J initialization to suppress @@ -43,9 +46,7 @@ private val initBlock = System.setProperty("slf4j.internal.verbosity", "WARN") } -/** - * Load build properties from the embedded lsp-version.properties file. - */ +/** Load build properties from the embedded lsp-version.properties file. */ private fun loadBuildProperties(): Properties = Properties().apply { Thread @@ -55,38 +56,75 @@ private fun loadBuildProperties(): Properties = ?.use { load(it) } } -/** - * Adapter backend types for the LSP server. - */ -private enum class AdapterBackend( +/** Adapter backend types for the LSP server. */ +internal enum class AdapterBackend( val displayName: String, ) { MOCK("Mock"), TREE_SITTER("Tree-sitter"), - COMPILER("XTC Compiler"), + COMPILER("Ecstasy Compiler"), + ; + + companion object { + /** An IDE restart can override the bundled default without rebuilding the server. */ + fun resolve( + buildSetting: String?, + startupSetting: String?, + ): AdapterBackend = fromSetting(startupSetting ?: buildSetting) + + fun fromSetting(setting: String? = null): AdapterBackend = + when (setting?.lowercase()) { + "mock" -> { + MOCK + } + + "treesitter", + "tree-sitter", + -> { + TREE_SITTER + } + + null, + "compiler", + "xtc", + "full", + -> { + COMPILER + } + + else -> { + throw IllegalArgumentException( + "Unknown lsp.adapter '$setting'; expected treesitter, compiler or mock", + ) + } + } + } } /** - * Create the appropriate adapter based on build configuration. + * Create the appropriate adapter for this server process. * - * @param adapterType The adapter type from build properties: "mock", "treesitter", or "compiler" + * @param requested The backend selected at startup or by the build default * @return The configured adapter and which backend is active */ -private fun createAdapter(adapterType: String): Pair = - when (adapterType.lowercase()) { - "compiler", "xtc", "full" -> { - // Stub adapter - all methods log warnings, no actual compiler integration yet - // TODO: Replace with real compiler adapter when parallel compiler integration is ready - // See PLAN_LSP_PARALLEL_LEXER.md for the integration roadmap - logger.info("using compiler stub adapter - all LSP calls will be logged but return empty results") - XdkAdapter() to AdapterBackend.COMPILER +private fun createAdapter(requested: AdapterBackend): Pair = + when (requested) { + AdapterBackend.COMPILER -> { + logger.info("using the Ecstasy compiler for diagnostics and document symbols") + val colorPrototype = + (System.getProperty("xtc.lsp.colorPrototype") ?: System.getenv("XTC_LSP_COLOR_PROTOTYPE")) == "true" + if (colorPrototype) logger.info("enabling experimental ColorPrototype.Rgba color values") + XdkAdapter(colorPrototype) to AdapterBackend.COMPILER } - "treesitter", "tree-sitter" -> { + AdapterBackend.TREE_SITTER -> { try { TreeSitterAdapter() to AdapterBackend.TREE_SITTER } catch (e: UnsatisfiedLinkError) { - logger.error("tree-sitter native library not found, falling back to mock adapter", e) + logger.error( + "tree-sitter native library not found, falling back to mock adapter", + e, + ) logger.warn( "to use tree-sitter, build the native library: ./gradlew :lang:tree-sitter:buildAllNativeLibrariesOnDemand", ) @@ -97,7 +135,7 @@ private fun createAdapter(adapterType: String): Pair = } } - else -> { + AdapterBackend.MOCK -> { MockAdapter() to AdapterBackend.MOCK } } @@ -109,16 +147,17 @@ fun main( @Suppress("UNUSED_EXPRESSION") initBlock - // Load build properties to determine adapter type + // Read the process override before constructing any adapter or negotiating capabilities. val buildProps = loadBuildProperties() - val adapterType = buildProps.getProperty("lsp.adapter", "mock") + val requested = AdapterBackend.resolve(buildProps.getProperty("lsp.adapter"), System.getProperty("xtc.lsp.adapter")) val version = buildProps.getProperty("lsp.version", "unknown") - // Create adapter based on build configuration - val (adapter, backend) = createAdapter(adapterType) + // Each connection owns a single adapter for its lifetime. + val (adapter, backend) = createAdapter(requested) // Log startup banner prominently - val logFile = "${System.getProperty("user.home")}/.xtc/logs/lsp-server.log" + val logFile = ServerLogs.directory.resolve("server.log").toString() + runCatching { ServerLogs.prune() }.onFailure { logger.warn("Could not prune inactive Ecstasy log sessions", it) } logger.info("========================================") logger.info("Ecstasy Language Server v$version") logger.info("backend: ${backend.displayName}") @@ -127,18 +166,22 @@ fun main( when (backend) { AdapterBackend.TREE_SITTER -> { - logger.info("tree-sitter provides: syntax highlighting, document symbols, completions, go-to-definition") + logger.info( + "tree-sitter provides: syntax highlighting, document symbols, completions, go-to-definition", + ) } AdapterBackend.COMPILER -> { - logger.warn("XTC Compiler adapter is a STUB - all methods log but return empty results") - logger.info("when implemented, will provide: full semantic analysis, type inference, cross-file navigation") + logger.info("compiler features are negotiated with the connected editor during initialization") + logger.info("the compiler uses the XDK libraries bundled with this server") } AdapterBackend.MOCK -> { logger.info("mock backend provides: basic symbol detection (regex-based)") - if (adapterType.lowercase() in listOf("treesitter", "tree-sitter")) { - logger.warn("tree-sitter was requested but failed to initialize - check native library") + if (requested == AdapterBackend.TREE_SITTER) { + logger.warn( + "tree-sitter was requested but failed to initialize - check native library", + ) } } } @@ -155,10 +198,7 @@ fun main( } } -/** - * Launch the server using stdio for communication. - * This is what VS Code and most editors use. - */ +/** Launch the server using stdio for communication. This is what VS Code and most editors use. */ fun launchStdio( server: XtcLanguageServer, input: InputStream, @@ -167,9 +207,25 @@ fun launchStdio( // Own the dispatcher as well as the server: LSP4J's default cached platform-thread // executor survives EOF, and adapter workers can keep the JVM alive indefinitely. val executor = Executors.newVirtualThreadPerTaskExecutor() + val trace = ProtocolTrace(server.clientTrace) + val lifecycle = ProtocolLifecycle() try { val launcher: Launcher = - LSPLauncher.createServerLauncher(server, input, output, executor) { it } + object : Launcher.Builder() { + override fun wrapMessageConsumer(consumer: MessageConsumer): MessageConsumer = + lifecycle.wrap( + trace.wrap( + super.wrapMessageConsumer(consumer), + received = consumer is RemoteEndpoint, + ), + received = consumer is RemoteEndpoint, + ) + }.setLocalService(server) + .setRemoteInterface(LanguageClient::class.java) + .setInput(input) + .setOutput(output) + .setExecutorService(executor) + .create() server.connect(launcher.remoteProxy) launcher.startListening().get() } catch (e: Exception) { @@ -184,6 +240,7 @@ fun launchStdio( } } } finally { + trace.close() executor.shutdownNow() server.close() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt index e5061112fb6..f3860a0ef24 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt @@ -1,5 +1,7 @@ package org.xvm.lsp.server +import com.google.gson.JsonPrimitive +import org.eclipse.lsp4j.ApplyWorkspaceEditParams import org.eclipse.lsp4j.CallHierarchyIncomingCall import org.eclipse.lsp4j.CallHierarchyIncomingCallsParams import org.eclipse.lsp4j.CallHierarchyItem @@ -10,6 +12,10 @@ import org.eclipse.lsp4j.CodeAction import org.eclipse.lsp4j.CodeActionParams import org.eclipse.lsp4j.CodeLens import org.eclipse.lsp4j.CodeLensParams +import org.eclipse.lsp4j.Color +import org.eclipse.lsp4j.ColorInformation +import org.eclipse.lsp4j.ColorPresentation +import org.eclipse.lsp4j.ColorPresentationParams import org.eclipse.lsp4j.Command import org.eclipse.lsp4j.CompletionItem import org.eclipse.lsp4j.CompletionItemKind @@ -21,6 +27,9 @@ import org.eclipse.lsp4j.DidChangeTextDocumentParams import org.eclipse.lsp4j.DidCloseTextDocumentParams import org.eclipse.lsp4j.DidOpenTextDocumentParams import org.eclipse.lsp4j.DidSaveTextDocumentParams +import org.eclipse.lsp4j.DocumentColorParams +import org.eclipse.lsp4j.DocumentDiagnosticParams +import org.eclipse.lsp4j.DocumentDiagnosticReport import org.eclipse.lsp4j.DocumentFormattingParams import org.eclipse.lsp4j.DocumentHighlight import org.eclipse.lsp4j.DocumentHighlightParams @@ -28,8 +37,10 @@ import org.eclipse.lsp4j.DocumentLink import org.eclipse.lsp4j.DocumentLinkParams import org.eclipse.lsp4j.DocumentOnTypeFormattingParams import org.eclipse.lsp4j.DocumentRangeFormattingParams +import org.eclipse.lsp4j.DocumentRangesFormattingParams import org.eclipse.lsp4j.DocumentSymbol import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.ExecuteCommandParams import org.eclipse.lsp4j.FoldingRange import org.eclipse.lsp4j.FoldingRangeRequestParams import org.eclipse.lsp4j.Hover @@ -37,35 +48,63 @@ import org.eclipse.lsp4j.HoverParams import org.eclipse.lsp4j.ImplementationParams import org.eclipse.lsp4j.InlayHint import org.eclipse.lsp4j.InlayHintParams +import org.eclipse.lsp4j.InlineCompletionItem +import org.eclipse.lsp4j.InlineCompletionList +import org.eclipse.lsp4j.InlineCompletionParams +import org.eclipse.lsp4j.InlineCompletionTriggerKind +import org.eclipse.lsp4j.InsertTextFormat +import org.eclipse.lsp4j.InsertTextMode import org.eclipse.lsp4j.LinkedEditingRangeParams import org.eclipse.lsp4j.LinkedEditingRanges import org.eclipse.lsp4j.Location import org.eclipse.lsp4j.LocationLink import org.eclipse.lsp4j.MarkupContent import org.eclipse.lsp4j.MarkupKind +import org.eclipse.lsp4j.Moniker +import org.eclipse.lsp4j.MonikerKind +import org.eclipse.lsp4j.MonikerParams import org.eclipse.lsp4j.ParameterInformation +import org.eclipse.lsp4j.PartialResultParams import org.eclipse.lsp4j.Position import org.eclipse.lsp4j.PrepareRenameDefaultBehavior import org.eclipse.lsp4j.PrepareRenameParams import org.eclipse.lsp4j.PrepareRenameResult import org.eclipse.lsp4j.Range import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.RenameFile +import org.eclipse.lsp4j.RenameFileOptions +import org.eclipse.lsp4j.RenameFilesParams import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.ResourceOperation import org.eclipse.lsp4j.SelectionRange import org.eclipse.lsp4j.SelectionRangeParams import org.eclipse.lsp4j.SemanticTokens +import org.eclipse.lsp4j.SemanticTokensDelta +import org.eclipse.lsp4j.SemanticTokensDeltaParams import org.eclipse.lsp4j.SemanticTokensParams +import org.eclipse.lsp4j.SemanticTokensRangeParams import org.eclipse.lsp4j.SignatureHelp import org.eclipse.lsp4j.SignatureHelpParams import org.eclipse.lsp4j.SignatureInformation import org.eclipse.lsp4j.SymbolInformation +import org.eclipse.lsp4j.TextDocumentEdit import org.eclipse.lsp4j.TextEdit import org.eclipse.lsp4j.TypeDefinitionParams import org.eclipse.lsp4j.TypeHierarchyItem import org.eclipse.lsp4j.TypeHierarchyPrepareParams import org.eclipse.lsp4j.TypeHierarchySubtypesParams import org.eclipse.lsp4j.TypeHierarchySupertypesParams +import org.eclipse.lsp4j.UniquenessLevel +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WillSaveTextDocumentParams +import org.eclipse.lsp4j.WorkDoneProgressParams +import org.eclipse.lsp4j.WorkspaceDiagnosticParams +import org.eclipse.lsp4j.WorkspaceDiagnosticReport +import org.eclipse.lsp4j.WorkspaceDiagnosticReportPartialResult import org.eclipse.lsp4j.WorkspaceEdit +import org.eclipse.lsp4j.WorkspaceSymbol +import org.eclipse.lsp4j.WorkspaceSymbolLocation +import org.eclipse.lsp4j.WorkspaceSymbolParams import org.eclipse.lsp4j.jsonrpc.ResponseErrorException import org.eclipse.lsp4j.jsonrpc.messages.Either import org.eclipse.lsp4j.jsonrpc.messages.Either3 @@ -74,27 +113,45 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode import org.eclipse.lsp4j.services.TextDocumentService import org.slf4j.LoggerFactory import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.AdapterCapability +import org.xvm.lsp.adapter.CodeLensCommand +import org.xvm.lsp.adapter.ColorValue +import org.xvm.lsp.adapter.FormattingConfig +import org.xvm.lsp.adapter.InlineCompletionContext +import org.xvm.lsp.adapter.SymbolMoniker +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRenameProposal +import org.xvm.lsp.model.CompilationResult import org.xvm.lsp.model.Diagnostic import org.xvm.lsp.model.SymbolInfo import org.xvm.lsp.model.fmt import org.xvm.lsp.model.fromLsp import org.xvm.lsp.model.toLsp import org.xvm.lsp.model.toRange +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.CancellationException import java.util.concurrent.CompletableFuture +import java.util.concurrent.CompletionException import java.util.concurrent.ConcurrentHashMap -import org.xvm.lsp.adapter.CallHierarchyIncomingCall as AdapterCallHierarchyIncomingCall +import java.util.concurrent.TimeUnit.SECONDS +import kotlin.time.Duration.Companion.nanoseconds import org.xvm.lsp.adapter.CallHierarchyItem as AdapterCallHierarchyItem -import org.xvm.lsp.adapter.CallHierarchyOutgoingCall as AdapterCallHierarchyOutgoingCall +import org.xvm.lsp.adapter.CodeLens as AdapterCodeLens import org.xvm.lsp.adapter.CompletionItem as AdapterCompletionItem +import org.xvm.lsp.adapter.DocumentLink as AdapterDocumentLink import org.xvm.lsp.adapter.FormattingOptions as AdapterFormattingOptions import org.xvm.lsp.adapter.Position as AdapterPosition import org.xvm.lsp.adapter.Range as AdapterRange import org.xvm.lsp.adapter.SelectionRange as AdapterSelectionRange +import org.xvm.lsp.adapter.TextEdit as AdapterTextEdit import org.xvm.lsp.adapter.TypeHierarchyItem as AdapterTypeHierarchyItem +import org.xvm.lsp.adapter.WorkspaceEdit as AdapterWorkspaceEdit +import org.xvm.lsp.model.Location as AdapterLocation +import org.xvm.lsp.model.Location as DiagnosticLocation /** - * Text document service for Ecstasy Language Server. - * Handles document synchronization and language features. + * Text document service for Ecstasy Language Server. Handles document synchronization and language + * features. */ class XtcTextDocumentService( private val server: XtcLanguageServer, @@ -104,90 +161,636 @@ class XtcTextDocumentService( private val logger = LoggerFactory.getLogger(XtcTextDocumentService::class.java) } - // ConcurrentHashMap is required because didOpen/didChange/didClose write on the LSP - // message thread, while documentSymbol, formatting, documentLink, etc. read from - // CompletableFuture.supplyAsync handlers on the ForkJoinPool. - private val openDocuments = ConcurrentHashMap() + private class Document( + val content: String, + val version: Int, + val scope: String, + val analysis: CompletableFuture, + ) + + // Completion callbacks and notifications must agree on which document may publish. + private val lifecycle = Any() + private val openDocuments = ConcurrentHashMap() + private var closed = false + private val diagnosticReports = DiagnosticReports() + private val semanticTokenReports = SemanticTokenReports() + private val completionReports = ResolveReports() + private val actionReports = ResolveReports() + private val lensReports = ResolveReports() + private val linkReports = ResolveReports() + private val hintReports = ResolveReports() + private val symbolReports = ResolveReports() + + private fun clearResolveReports() = + listOf( + completionReports, + actionReports, + lensReports, + linkReports, + hintReports, + symbolReports, + ).forEach { it.clear() } + + private fun requireResolve( + enabled: Boolean, + feature: String, + ) { + if (!enabled) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.MethodNotFound, + "$feature resolution was not negotiated", + null, + ), + ) + } + } + + private var diagnosticRevision = 0L + private val publishedByScope = mutableMapOf>() + private val pendingQueries = mutableMapOf, String>() private fun supplyAsync( method: String, logParams: String, logResult: (R) -> String = { "completed" }, + uri: String? = null, + sourceOnly: Boolean = false, + progress: WorkDoneProgressParams? = null, + partial: ((R) -> PartialResults.Plan)? = null, block: () -> R, - ): CompletableFuture = server.supplyAsync(method, logParams, logResult, block) + ): CompletableFuture { + val document = uri?.let { openDocuments[it] } + return queryAsync( + method, + uri.orEmpty(), + request = { + server.supplyAsync(method, logParams, logResult) { + synchronized(lifecycle) { + if (closed || (uri != null && openDocuments[uri] !== document)) { + throw contentModified() + } + block() + } + } + }, + workspace = uri == null, + sourceOnly = sourceOnly, + progress = progress, + partial = partial, + ) { + it + } + } - /** - * LSP: textDocument/didOpen - * @see org.eclipse.lsp4j.services.TextDocumentService.didOpen - */ - override fun didOpen(params: DidOpenTextDocumentParams) { - val uri = params.textDocument.uri - val content = params.textDocument.text + /** A semantic query owns its backend future, while the module analysis remains shared. */ + private fun queryAsync( + method: String, + uri: String, + request: () -> CompletableFuture, + workspace: Boolean = false, + sourceOnly: Boolean = false, + progress: WorkDoneProgressParams? = null, + partial: ((R) -> PartialResults.Plan)? = null, + convert: (T) -> R, + ): CompletableFuture { + val result = CompletableFuture() + val trace = ExecutionTrace.span("lsp-query", method, uri) + ExecutionTrace.event(trace, "start") + val (document, documents, revision) = + synchronized(lifecycle) { + if (closed) return CompletableFuture.failedFuture(contentModified()) + pendingQueries[result] = uri + Triple( + openDocuments[uri], + if (workspace) openDocuments.toMap() else null, + diagnosticRevision, + ) + } + + fun stale(): Boolean = + closed || + openDocuments[uri] !== document || + (documents != null && documents != openDocuments) + val started = System.nanoTime() + logger.info("{}: {}", method, uri) + // A JSON-RPC cancellation can complete this future under the transport's request lock. + // Release that thread before entering either the document or compiler lifecycle. + result.whenCompleteAsync { _, failure -> + synchronized(lifecycle) { pendingQueries.remove(result) } + val outcome = if (failure == null) "completed" else "canceled or failed" + ExecutionTrace.event(trace, "end", mapOf("outcome" to outcome)) + logger.info( + "{}: {} in {}", + method, + outcome, + (System.nanoTime() - started).nanoseconds, + ) + } + val ready = + if (sourceOnly) { + CompletableFuture.completedFuture(null) + } else if (documents != null) { + CompletableFuture.allOf( + *documents.values + .map { it.analysis } + .distinct() + .toTypedArray(), + ) + } else { + document?.analysis ?: CompletableFuture.completedFuture(null) + } + ready + .handle { _, _ -> ExecutionTrace.event(trace, "analysis-ready") } + .thenRunAsync { + val work = + synchronized(lifecycle) { + if (result.isDone) return@thenRunAsync + if (stale()) throw contentModified() + ExecutionTrace.event(trace, "started") + ExecutionTrace.within(trace, request) + } + // Register after starting work: prior cancellation still schedules backend cleanup. + result.whenCompleteAsync { _, failure -> if (failure != null) work.cancel(false) } + // Do not occupy the compiler worker while acquiring the publication lock: a + // synchronous navigation request can hold it while awaiting that same worker. + work.whenCompleteAsync { value, failure -> + ExecutionTrace.event(trace, "backend-complete") + synchronized(lifecycle) { + if (!result.isDone) { + when { + stale() -> { + result.completeExceptionally(contentModified()) + } - logger.info("textDocument/didOpen: {} ({} bytes)", uri, content.length) - openDocuments[uri] = content + failure != null -> { + val cause = + generateSequence(failure) { + (it as? CompletionException)?.cause + }.last() + if (cause is CancellationException) { + result.cancel(false) + } else { + result.completeExceptionally(cause) + } + } - val result = adapter.compile(uri, content) - logger.info("textDocument/didOpen: compiled, {} diagnostics", result.diagnostics.size) - server.publishDiagnostics(uri, result.diagnostics) + else -> { + try { + val converted = convert(value) + ExecutionTrace.event(trace, "converted") + result.complete(converted) + } catch (e: Exception) { + result.completeExceptionally(e) + } catch (e: Error) { + result.completeExceptionally(e) + throw e + } + } + } + } + } + } + }.whenComplete { _, failure -> + if (failure != null) result.completeExceptionally(failure) + } + val publication = + if (partial == null) { + result + } else { + server.partialResults.publish( + (progress as? PartialResultParams)?.partialResultToken, + result, + { + synchronized(lifecycle) { + if (stale() || diagnosticRevision != revision) throw contentModified() + } + }, + partial, + ) + } + return server.observeQuery(method, progress, publication, uri) + } + + private fun contentModified() = + ResponseErrorException( + ResponseError( + ResponseErrorCode.ContentModified, + "Document changed during analysis", + null, + ), + ) + + /** Called under lifecycle; retire the public result even if a backend ignores cancellation. */ + private fun invalidateQueries(uris: Set) { + pendingQueries + .filterValues { it in uris } + .keys + .toList() + .forEach { it.completeExceptionally(contentModified()) } + } + + override fun didOpen(params: DidOpenTextDocumentParams) { + synchronized(lifecycle) { + if (closed) return + logger.info( + "textDocument/didOpen: {} version={}", + params.textDocument.uri, + params.textDocument.version, + ) + analyse( + params.textDocument.uri, + params.textDocument.text, + params.textDocument.version, + ) + } } - /** - * LSP: textDocument/didChange - * @see org.eclipse.lsp4j.services.TextDocumentService.didChange - */ override fun didChange(params: DidChangeTextDocumentParams) { - val uri = params.textDocument.uri - val changes = params.contentChanges - if (changes.isNullOrEmpty()) { - logger.warn("textDocument/didChange: no content changes for: {}", uri) - return - } - logger.info( - "textDocument/didChange: {} change(s): {}", - changes.size, - changes.joinToString(" | ") { change -> - val rangeText = change.range?.fmt() ?: "" - val preview = - change.text - .replace("\n", "\\n") - .replace("\t", "\\t") - .take(80) - "range=$rangeText len=${change.text.length} text='$preview'" - }, + synchronized(lifecycle) { + if (closed) return + val uri = params.textDocument.uri + val previous = openDocuments[uri] ?: return + val version = params.textDocument.version + if (version <= previous.version) return + val changes = params.contentChanges + if (changes.isNullOrEmpty()) return + val content = + try { + DocumentText(previous.content) + .change(changes, server.synchronization.incremental) + } catch (e: IllegalArgumentException) { + logger.warn( + "textDocument/didChange: ignoring invalid changes for {}: {}", + uri, + e.message, + ) + return + } + analyse(uri, content, version) + } + } + + /** Refresh the changed module, then open source consumers in dependency order. */ + private fun analyse( + uri: String, + content: String, + version: Int, + ) { + diagnosticRevision++ + clearResolveReports() + val compiler = adapter as? XdkAdapter + val changedGraph = compiler?.updateDocument(uri, content).orEmpty() + analyseOne(uri, content, version) + refreshScopes( + ( + changedGraph + + (compiler?.affectedSourceScopes(uri) ?: adapter.affectedAnalysisScopes(uri)) + ) - + adapter.analysisScope(uri), ) - val content = changes.first().text + } + + private fun refreshScopes(scopes: Set) { + if (scopes.isEmpty()) return + val ownership = analysisScopes() + scopes + .mapNotNull { scope -> + openDocuments.entries.firstOrNull { + it.value.scope == scope || ownership[it.key] == scope + } + }.distinctBy { ownership[it.key] } + .forEach { (uri, document) -> + analyseOne(uri, document.content, document.version, ownership) + } + } + + private fun analysisScopes(): Map = + if (adapter is XdkAdapter) { + adapter.analysisScopes(openDocuments.keys) + } else { + openDocuments.keys.associateWith(adapter::analysisScope) + } + + /** A member edit replaces the analysis future for every open document in that module. */ + private fun analyseOne( + uri: String, + content: String, + version: Int, + ownership: Map? = null, + ) { + val analysis = adapter.compileAsync(uri, content) + val scope = adapter.analysisScope(uri) + val currentScopes = ownership ?: analysisScopes() + val affected = + openDocuments.filter { (otherUri, document) -> + otherUri == uri || document.scope == scope || currentScopes[otherUri] == scope + } + val current = + affected + .mapValues { (_, document) -> + Document(document.content, document.version, scope, analysis) + }.toMutableMap() + current[uri] = Document(content, version, scope, analysis) + openDocuments.putAll(current) + invalidateQueries(current.keys) + affected.values + .map { it.analysis } + .distinct() + .filter { it !== analysis } + .forEach { it.cancel(false) } + // Publication must not block the compiler worker behind a queued navigation request. + analysis.whenCompleteAsync { result, failure -> + synchronized(lifecycle) { + if ( + !closed && + current.all { (documentUri, document) -> + openDocuments[documentUri] === document + } + ) { + if (failure == null) { + publish(scope, result) + } else if (failure !is CancellationException) { + logger.error("analysis failed: uri={}, version={}", uri, version, failure) + publish( + scope, + CompilationResult.failure( + uri, + listOf( + Diagnostic( + location = DiagnosticLocation(uri, 0, 0, 0, 0), + severity = Diagnostic.Severity.ERROR, + message = + "Ecstasy analysis failed; see the language server log for details", + code = "ANALYSIS-FAILED", + source = "xtc", + ), + ), + ), + ) + } + } + } + } + } - logger.info("textDocument/didChange: {} ({} bytes)", uri, content.length) - openDocuments[uri] = content + /** A source closure publishes together; artifact-only foreign sources remain related info. */ + private fun publish( + scope: String, + result: CompilationResult, + ) { + val previous = publishedByScope.put(scope, result.documentUris).orEmpty() + clearUnowned(previous - result.documentUris) + result.documentUris.forEach { uri -> + val diagnostics = + result.diagnostics.filter { + it.location.uri == uri || + (uri == result.uri && it.location.uri !in result.documentUris) + } + diagnosticReports.record(uri, diagnostics) + server.publishDiagnostics(uri, diagnostics, openDocuments[uri]?.version) + } + server.refreshDiagnostics() + server.refreshSemanticFeatures() + // Root discovery can change after file creation/removal; release publications of old + // scopes. + val inactive = + publishedByScope.keys.filter { key -> openDocuments.values.none { it.scope == key } } + inactive.forEach { key -> + clearUnowned(publishedByScope.remove(key).orEmpty() - result.documentUris) + } + } - val result = adapter.compile(uri, content) - logger.info("textDocument/didChange: compiled, {} diagnostics", result.diagnostics.size) - server.publishDiagnostics(uri, result.diagnostics) + private fun clearUnowned(uris: Set) { + uris + .filter { uri -> publishedByScope.values.none { uri in it } } + .forEach { + diagnosticReports.record(it, emptyList()) + server.publishDiagnostics(it, emptyList(), openDocuments[it]?.version) + } } - /** - * LSP: textDocument/didClose - * @see org.eclipse.lsp4j.services.TextDocumentService.didClose - */ override fun didClose(params: DidCloseTextDocumentParams) { - val uri = params.textDocument.uri - logger.info("textDocument/didClose: {}", uri) - openDocuments.remove(uri) - adapter.closeDocument(uri) - server.publishDiagnostics(uri, emptyList()) + synchronized(lifecycle) { + val uri = params.textDocument.uri + logger.info("textDocument/didClose: {}", uri) + diagnosticRevision++ + clearResolveReports() + val affected = adapter.affectedAnalysisScopes(uri) + semanticTokenReports.retire(uri) + val document = openDocuments.remove(uri) + invalidateQueries(setOf(uri)) + document?.analysis?.cancel(false) + val retired = + if (adapter is XdkAdapter) { + adapter.closeDocumentAndRefresh(uri) + } else { + adapter.closeDocument(uri) + emptySet() + } + if (closed) return + diagnosticReports.record(uri, emptyList()) + server.publishDiagnostics(uri, emptyList(), document?.version) + server.refreshDiagnostics() + server.refreshSemanticFeatures() + refreshScopes(affected + retired) + if (openDocuments.values.none { it.scope == document?.scope }) { + clearUnowned(publishedByScope.remove(document?.scope).orEmpty() - uri) + } + } } - /** - * LSP: textDocument/didSave - * @see org.eclipse.lsp4j.services.TextDocumentService.didSave - */ + /** Re-read closed files and module membership after filesystem notifications. */ + fun refreshForFile(uri: String) { + synchronized(lifecycle) { + // An open buffer is authoritative, including when its disk file is created or + // deleted. Recompiling identical overlays here cancels otherwise current queries. + // didChange already propagates edits; didClose re-reads disk and membership. + if (closed || openDocuments.containsKey(uri)) return + val affected = + (adapter as? XdkAdapter)?.changedFileScopes(uri) + ?: adapter.affectedAnalysisScopes(uri) + if (affected.isEmpty()) return + diagnosticRevision++ + clearResolveReports() + server.refreshDiagnostics() + server.refreshSemanticFeatures() + refreshScopes(affected + publishedByScope.filterValues { uri in it }.keys) + } + } + + /** Dependency replacement and versioned reanalysis share the diagnostic publication lock. */ + internal fun refreshDependencies(replace: () -> Set) { + synchronized(lifecycle) { + if (closed) return + val affected = replace() + server.readOnlyDocuments.refresh() + if (affected.isEmpty()) return + diagnosticRevision++ + clearResolveReports() + refreshScopes(affected) + server.refreshDiagnostics() + server.refreshSemanticFeatures() + } + } + + fun close() { + synchronized(lifecycle) { + closed = true + val documents = openDocuments.toMap() + openDocuments.clear() + invalidateQueries(pendingQueries.values.toSet()) + publishedByScope.clear() + diagnosticReports.clear() + semanticTokenReports.clear() + clearResolveReports() + documents.forEach { (uri, document) -> + document.analysis.cancel(false) + adapter.closeDocument(uri) + } + } + } + + /** The pre-save notification is observational; only didSave refreshes filesystem inputs. */ + override fun willSave(params: WillSaveTextDocumentParams) { + logger.info("textDocument/willSave: {} reason={}", params.textDocument.uri, params.reason) + } + + // Saving does not wait for compilation. The formatter uses the captured source and the same + // version guard as formatting requests; returned edits are applied only by the client. + override fun willSaveWaitUntil(params: WillSaveTextDocumentParams): CompletableFuture> = + supplyAsync( + "textDocument/willSaveWaitUntil", + params.textDocument.uri, + { "${it.size} edits" }, + uri = params.textDocument.uri, + sourceOnly = true, + ) { + requireResolve(server.synchronization.waitUntil, "Save edits") + if (!server.synchronization.formatOnSave) return@supplyAsync emptyList() + val uri = params.textDocument.uri + val content = openDocuments[uri]?.content ?: return@supplyAsync emptyList() + val config = server.editorFormattingConfig ?: FormattingConfig.DEFAULT + adapter + .formatDocument( + uri, + content, + AdapterFormattingOptions( + tabSize = config.indentSize, + insertSpaces = config.insertSpaces, + ), + ).map { TextEdit(it.range.toLsp(), it.newText) } + } + override fun didSave(params: DidSaveTextDocumentParams) { logger.info("textDocument/didSave: {}", params.textDocument.uri) + refreshForFile(params.textDocument.uri) + } + + override fun diagnostic(params: DocumentDiagnosticParams): CompletableFuture = + pullDiagnostics( + "textDocument/diagnostic", + params.textDocument.uri, + params.identifier, + workspace = false, + progress = params, + ) { results -> + val current = diagnosticReports.record(results) + // Unknown or removed documents have an empty report, never an old cached error. + if (!diagnosticReports.includes(current, params.textDocument.uri)) { + diagnosticReports.record(params.textDocument.uri, emptyList()) + } + diagnosticReports.document( + params.textDocument.uri, + params.previousResultId, + if (server.supportsRelatedDiagnostics) current else emptySet(), + relatedInformation = server.presentation.pullDiagnosticRelatedInformation, + ) + } + + internal fun workspaceDiagnostics(params: WorkspaceDiagnosticParams): CompletableFuture = + pullDiagnostics( + "workspace/diagnostic", + "", + params.identifier, + workspace = true, + progress = params, + partial = { report -> + PartialResults.Plan( + report.items + .chunked(PartialResults.BATCH_SIZE) + .map(::WorkspaceDiagnosticReportPartialResult), + WorkspaceDiagnosticReport(emptyList()), + ) + }, + ) { results -> + val current = diagnosticReports.record(results) + diagnosticReports.workspace( + current, + params.previousResultIds.orEmpty().associate { it.uri to it.value }, + openDocuments.mapValues { it.value.version }, + relatedInformation = server.presentation.pullDiagnosticRelatedInformation, + ) + } + + private fun pullDiagnostics( + method: String, + uri: String, + identifier: String?, + workspace: Boolean, + progress: WorkDoneProgressParams, + partial: ((T) -> PartialResults.Plan)? = null, + convert: (List) -> T, + ): CompletableFuture { + if (!server.usesPullDiagnostics || (identifier != null && identifier != "xtc")) { + return CompletableFuture.failedFuture( + ResponseErrorException( + ResponseError( + ResponseErrorCode.InvalidParams, + "Compiler pull diagnostics were not negotiated for this provider", + null, + ), + ), + ) + } + val revision = synchronized(lifecycle) { diagnosticRevision } + return queryAsync( + method, + uri, + request = { + val open = openDocuments[uri] + if (!workspace && open != null) { + CompletableFuture.completedFuture(listOf(open.analysis.join())) + } else { + (adapter as XdkAdapter).workspaceDiagnosticsAsync() + } + }, + progress = progress, + partial = partial, + convert = { results -> + if (revision != diagnosticRevision) throw contentModified() + val owned = results.flatMap { it.documentUris }.toSet() + // A closed member can belong to an open standalone module, outside the + // configured graph. Preserve that module's current analysis for document pulls + // too, without including unrelated standalone modules in the report. + val all = + results + + openDocuments + .filterKeys { !diagnosticReports.includes(owned, it) } + .values + .map { it.analysis.join() } + .filter { + workspace || diagnosticReports.includes(it.documentUris, uri) + } + convert(all) + }, + workspace = true, + ) } /** * LSP: textDocument/hover + * * @see org.eclipse.lsp4j.services.TextDocumentService.hover */ override fun hover(params: HoverParams): CompletableFuture = @@ -195,46 +798,135 @@ class XtcTextDocumentService( "textDocument/hover", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> if (result == null) "no result" else "found symbol" }, + uri = params.textDocument.uri, ) { - adapter.getHoverInfo(params.textDocument.uri, params.position.line, params.position.character)?.let { - Hover().apply { - contents = - Either.forRight( - MarkupContent().apply { - kind = MarkupKind.MARKDOWN - value = it - }, - ) + adapter + .getHoverInfo( + params.textDocument.uri, + params.position.line, + params.position.character, + )?.let { + Hover().apply { + contents = Either.forRight(server.presentation.hover(it)) + } } - } } /** * LSP: textDocument/completion + * * @see org.eclipse.lsp4j.services.TextDocumentService.completion */ override fun completion(params: CompletionParams): CompletableFuture, CompletionList>> = - supplyAsync( + queryAsync( "textDocument/completion", - "${params.textDocument.uri} at ${params.position.fmt()} trigger=${params.context?.triggerKind}/${params.context?.triggerCharacter}", - { result -> - val items = result.left - val preview = items.take(5).joinToString { it.label } - "${items.size} items${if (preview.isNotEmpty()) " [$preview]" else ""}" + params.textDocument.uri, + { + adapter.getCompletionsAsync( + params.textDocument.uri, + params.position.line, + params.position.character, + params.context?.triggerCharacter, + ) }, - ) { - val trigger = params.context?.triggerCharacter + progress = params, + ) { completions -> val items = - adapter.getCompletions(params.textDocument.uri, params.position.line, params.position.character, trigger).map { c -> + completions.map { c -> CompletionItem(c.label).apply { - kind = toCompletionItemKind(c.kind) + kind = server.presentation.completionKind(toCompletionItemKind(c.kind)) + val snippet = c.snippet?.takeIf { server.presentation.completionSnippets } + + fun insertion(text: String) = if (c.snippet == null) text else server.presentation.completionTemplate(text) + insertTextFormat = if (snippet == null) InsertTextFormat.PlainText else InsertTextFormat.Snippet + if (c.snippet != null && server.presentation.completionAsIs) insertTextMode = InsertTextMode.AsIs detail = c.detail - insertText = c.insertText + insertText = insertion(snippet ?: c.insertText) + val handle = + c.documentation + ?.takeIf { server.resolvesCompletionDocumentation } + ?.let { + completionReports.remember( + diagnosticRevision, + c.label, + it, + it.length, + ) + } + if (handle == null) { + documentation = c.documentation?.let { Either.forLeft(it) } + } else { + data = handle + } + sortText = c.sortText + textEdit = + c.textEdit?.let { Either.forLeft(TextEdit(it.range.toLsp(), insertion(snippet ?: it.newText))) } + additionalTextEdits = c.additionalTextEdits.map { TextEdit(it.range.toLsp(), it.newText) } } } Either.forLeft(items) } + override fun inlineCompletion( + params: InlineCompletionParams, + ): CompletableFuture, InlineCompletionList>> { + if (!server.presentation.inlineCompletion || AdapterCapability.INLINE_COMPLETION !in adapter.capabilities) { + return CompletableFuture.failedFuture( + ResponseErrorException(ResponseError(ResponseErrorCode.MethodNotFound, "Inline completion was not negotiated", null)), + ) + } + val context = + InlineCompletionContext( + automatic = params.context.triggerKind == InlineCompletionTriggerKind.Automatic, + selectedCompletion = + params.context.selectedCompletionInfo?.let { + AdapterTextEdit(toAdapterRange(it.range), it.text) + }, + ) + return queryAsync( + "textDocument/inlineCompletion", + params.textDocument.uri, + { + adapter.getInlineCompletionsAsync( + params.textDocument.uri, + AdapterPosition(params.position.line, params.position.character), + context, + ) + }, + progress = params, + ) { edits -> + Either.forRight( + InlineCompletionList( + edits.map { edit -> + InlineCompletionItem(Either.forLeft(edit.newText)).apply { + range = edit.range.toLsp() + filterText = edit.newText + } + }, + ), + ) + } + } + + override fun resolveCompletionItem(item: CompletionItem): CompletableFuture = + supplyAsync("completionItem/resolve", item.label) { + if (!server.resolvesCompletionDocumentation) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.MethodNotFound, + "Completion resolution was not negotiated", + null, + ), + ) + } + if (item.data != null) { + val documentation = + completionReports.resolve(item.data, diagnosticRevision, item.label) + if (item.documentation == null) item.documentation = Either.forLeft(documentation) + } + item + } + private fun toCompletionItemKind(kind: AdapterCompletionItem.CompletionKind): CompletionItemKind = when (kind) { AdapterCompletionItem.CompletionKind.CLASS -> CompletionItemKind.Class @@ -244,10 +936,13 @@ class XtcTextDocumentService( AdapterCompletionItem.CompletionKind.VARIABLE -> CompletionItemKind.Variable AdapterCompletionItem.CompletionKind.KEYWORD -> CompletionItemKind.Keyword AdapterCompletionItem.CompletionKind.MODULE -> CompletionItemKind.Module + AdapterCompletionItem.CompletionKind.VALUE -> CompletionItemKind.Value + AdapterCompletionItem.CompletionKind.SNIPPET -> CompletionItemKind.Snippet } /** * LSP: textDocument/definition + * * @see org.eclipse.lsp4j.services.TextDocumentService.definition */ override fun definition(params: DefinitionParams): CompletableFuture, List>> = @@ -262,39 +957,47 @@ class XtcTextDocumentService( "found ${loc.uri.substringAfterLast('/')}@${loc.range.start.fmt()}" } }, + uri = params.textDocument.uri, + progress = params, + partial = PartialResults::eitherLists, ) { - adapter.findDefinition(params.textDocument.uri, params.position.line, params.position.character)?.let { - Either.forLeft, List>(listOf(it.toLsp())) - } ?: Either.forLeft(emptyList()) + adapter + .findDefinition( + params.textDocument.uri, + params.position.line, + params.position.character, + )?.let { + Either.forLeft(listOf(it.toLsp())) + } ?: Either.forLeft(emptyList()) } /** * LSP: textDocument/references + * * @see org.eclipse.lsp4j.services.TextDocumentService.references */ override fun references(params: ReferenceParams): CompletableFuture> = - supplyAsync( + queryAsync( "textDocument/references", - "${params.textDocument.uri} at ${params.position.fmt()}", - { result -> - val preview = - result.take(5).joinToString { loc -> - "${loc.uri.substringAfterLast('/')}@${loc.range.start.fmt()}" - } - "${result.size} references${if (preview.isNotEmpty()) " [$preview]" else ""}" - }, - ) { - adapter - .findReferences( + params.textDocument.uri, + { + adapter.findReferencesAsync( params.textDocument.uri, params.position.line, params.position.character, params.context.isIncludeDeclaration, - ).map { it.toLsp() } + ) + }, + workspace = true, + progress = params, + partial = PartialResults::list, + ) { references -> + references.map { it.toLsp() } } /** * LSP: textDocument/documentSymbol + * * @see org.eclipse.lsp4j.services.TextDocumentService.documentSymbol */ override fun documentSymbol(params: DocumentSymbolParams): CompletableFuture>> = @@ -302,22 +1005,39 @@ class XtcTextDocumentService( "textDocument/documentSymbol", params.textDocument.uri, { result -> "${result.size} symbols" }, + uri = params.textDocument.uri, + progress = params, + partial = PartialResults::list, ) { val uri = params.textDocument.uri - val result = - adapter.getCachedResult(uri) ?: openDocuments[uri]?.let { content -> - adapter.compile(uri, content) - } ?: return@supplyAsync emptyList() + val result = adapter.getCachedResult(uri) ?: return@supplyAsync emptyList() - result.symbols.map { symbol -> - Either.forRight(toDocumentSymbol(symbol)) + if (server.presentation.hierarchicalSymbols) { + result.symbols.map { Either.forRight(toDocumentSymbol(it)) } + } else { + flatSymbols(result.symbols).map { Either.forLeft(it) } } } + private fun flatSymbols( + symbols: List, + container: String? = null, + ): List = + symbols.flatMap { symbol -> + listOf( + SymbolInformation( + symbol.name, + server.presentation.symbolKind(symbol.kind.toLsp()), + symbol.location.toLsp(), + container, + ), + ) + flatSymbols(symbol.children, symbol.name) + } + private fun toDocumentSymbol(symbol: SymbolInfo): DocumentSymbol = DocumentSymbol().apply { name = symbol.name - kind = symbol.kind.toLsp() + kind = server.presentation.symbolKind(symbol.kind.toLsp()) range = symbol.location.toRange() selectionRange = symbol.location.toRange() if (symbol.typeSignature != null) { @@ -330,6 +1050,7 @@ class XtcTextDocumentService( /** * LSP: textDocument/documentHighlight + * * @see org.eclipse.lsp4j.services.TextDocumentService.documentHighlight */ override fun documentHighlight(params: DocumentHighlightParams): CompletableFuture> = @@ -337,6 +1058,7 @@ class XtcTextDocumentService( "textDocument/documentHighlight", "${params.textDocument.uri} pos=${params.position.fmt()}", { result -> "${result.size} highlights" }, + uri = params.textDocument.uri, ) { adapter .getDocumentHighlights( @@ -353,6 +1075,7 @@ class XtcTextDocumentService( /** * LSP: textDocument/selectionRange + * * @see org.eclipse.lsp4j.services.TextDocumentService.selectionRange */ override fun selectionRange(params: SelectionRangeParams): CompletableFuture> = @@ -360,19 +1083,23 @@ class XtcTextDocumentService( "textDocument/selectionRange", "${params.textDocument.uri} positions=${params.positions.map { it.fmt() }}", { result -> "${result.size} ranges" }, + uri = params.textDocument.uri, ) { val adapterPositions = params.positions.map { AdapterPosition(it.line, it.character) } - adapter.getSelectionRanges(params.textDocument.uri, adapterPositions).map { toLspSelectionRange(it) } + adapter.getSelectionRanges(params.textDocument.uri, adapterPositions).map { + toLspSelectionRange(it) + } } - private fun toLspSelectionRange(range: AdapterSelectionRange): org.eclipse.lsp4j.SelectionRange = - org.eclipse.lsp4j.SelectionRange().apply { + private fun toLspSelectionRange(range: AdapterSelectionRange): SelectionRange = + SelectionRange().apply { this.range = range.range.toLsp() this.parent = range.parent?.let { toLspSelectionRange(it) } } /** * LSP: textDocument/foldingRange + * * @see org.eclipse.lsp4j.services.TextDocumentService.foldingRange */ override fun foldingRange(params: FoldingRangeRequestParams): CompletableFuture> = @@ -380,16 +1107,20 @@ class XtcTextDocumentService( "textDocument/foldingRange", params.textDocument.uri, { result -> "${result.size} ranges" }, + uri = params.textDocument.uri, ) { adapter.getFoldingRanges(params.textDocument.uri).map { r -> FoldingRange(r.startLine, r.endLine).apply { kind = r.kind?.toLsp() + startCharacter = r.startCharacter + endCharacter = r.endCharacter } } } /** * LSP: textDocument/documentLink + * * @see org.eclipse.lsp4j.services.TextDocumentService.documentLink */ override fun documentLink(params: DocumentLinkParams): CompletableFuture> = @@ -397,52 +1128,84 @@ class XtcTextDocumentService( "textDocument/documentLink", params.textDocument.uri, { result -> "${result.size} links" }, + uri = params.textDocument.uri, ) { val uri = params.textDocument.uri - val content = openDocuments[uri] ?: return@supplyAsync emptyList() + val content = openDocuments[uri]?.content ?: return@supplyAsync emptyList() adapter.getDocumentLinks(uri, content).map { l -> DocumentLink().apply { range = l.range.toLsp() - target = l.target - tooltip = l.tooltip + val handle = + if (server.resolvesDocumentLinkTarget) { + linkReports.remember( + diagnosticRevision, + range.fmt(), + l, + (l.target?.length ?: 0) + (l.tooltip?.length ?: 0), + ) + } else { + null + } + if (handle == null) { + target = l.target?.let(server.readOnlyDocuments::present) + tooltip = l.tooltip.takeIf { server.presentation.linkTooltips } + } else { + data = handle + } } } } /** * LSP: textDocument/signatureHelp + * * @see org.eclipse.lsp4j.services.TextDocumentService.signatureHelp */ override fun signatureHelp(params: SignatureHelpParams): CompletableFuture = - supplyAsync( + queryAsync( "textDocument/signatureHelp", - "${params.textDocument.uri} at ${params.position.fmt()}", - { result -> if (result == null) "no result" else "${result.signatures.size} signatures" }, - ) { - adapter.getSignatureHelp(params.textDocument.uri, params.position.line, params.position.character)?.let { help -> + params.textDocument.uri, + { + adapter.getSignatureHelpAsync( + params.textDocument.uri, + params.position.line, + params.position.character, + ) + }, + progress = params, + ) { result -> + result?.let { help -> SignatureHelp().apply { signatures = help.signatures.map { s -> SignatureInformation().apply { label = s.label documentation = s.documentation?.let { Either.forLeft(it) } + activeParameter = + s.activeParameter.takeIf { + server.presentation.signatureActiveParameters + } parameters = s.parameters.map { p -> ParameterInformation().apply { label = Either.forLeft(p.label) - documentation = p.documentation?.let { Either.forLeft(it) } + documentation = + p.documentation?.let { Either.forLeft(it) } } } } } activeSignature = help.activeSignature - activeParameter = help.activeParameter + activeParameter = + help.signatures.getOrNull(help.activeSignature)?.activeParameter + ?: help.activeParameter } } } /** * LSP: textDocument/prepareRename + * * @see org.eclipse.lsp4j.services.TextDocumentService.prepareRename */ override fun prepareRename( @@ -452,104 +1215,301 @@ class XtcTextDocumentService( "textDocument/prepareRename", "${params.textDocument.uri} at ${params.position.fmt()}", { _ -> "valid" }, + uri = params.textDocument.uri, ) { - adapter.prepareRename(params.textDocument.uri, params.position.line, params.position.character)?.let { result -> - Either3.forSecond( - PrepareRenameResult().apply { - range = result.range.toLsp() - placeholder = result.placeholder - }, + adapter + .prepareRename( + params.textDocument.uri, + params.position.line, + params.position.character, + )?.let { result -> + Either3.forSecond( + PrepareRenameResult().apply { + range = result.range.toLsp() + placeholder = result.placeholder + }, + ) + } + ?: throw ResponseErrorException( + ResponseError( + ResponseErrorCode.InvalidParams, + "Rename not allowed at this position", + null, + ), ) - } ?: throw ResponseErrorException( - ResponseError( - ResponseErrorCode.InvalidParams, - "Rename not allowed at this position", - null, - ), - ) } /** * LSP: textDocument/rename + * * @see org.eclipse.lsp4j.services.TextDocumentService.rename */ override fun rename(params: RenameParams): CompletableFuture = - supplyAsync( + queryAsync( "textDocument/rename", - "${params.textDocument.uri} at ${params.position.fmt()} -> '${params.newName}'", - { result -> if (result == null) "no edit" else "${result.changes?.size ?: 0} files changed" }, - ) { - adapter - .rename( + params.textDocument.uri, + { + adapter.renameAsync( params.textDocument.uri, params.position.line, params.position.character, params.newName, - )?.let { edit -> - WorkspaceEdit().apply { - changes = - edit.changes.mapValues { (_, edits) -> - edits.map { e -> - TextEdit().apply { - range = e.range.toLsp() - newText = e.newText - } - } - } - } - } + ) + }, + workspace = true, + progress = params, + ) { edit -> + edit?.let(::protocolEdit) } /** - * LSP: textDocument/codeAction - * @see org.eclipse.lsp4j.services.TextDocumentService.codeAction + * Same lifecycle/version checks as standard Rename; settings are never installed by a proposal. */ - override fun codeAction(params: CodeActionParams): CompletableFuture>> = - supplyAsync( - "textDocument/codeAction", - "${params.textDocument.uri} range=${params.range.fmt()} diagnostics=${params.context.diagnostics?.size ?: 0}", - { result -> - val titles = - result.take(5).joinToString { either -> - either.right?.title ?: either.left?.title ?: "" - } - "${result.size} actions${if (titles.isNotEmpty()) " [$titles]" else ""}" + fun renameProposal(params: RenameParams): CompletableFuture { + val compiler = + adapter as? XdkAdapter ?: return rename(params).thenApply { it?.let(::RenameProposal) } + return queryAsync( + "xtc/rename", + params.textDocument.uri, + { + compiler.renameProposalAsync( + params.textDocument.uri, + params.position.line, + params.position.character, + params.newName, + ) }, + workspace = true, + ) { proposal -> proposal?.let(::protocolProposal) } + } + + private fun protocolProposal(proposal: XdkRenameProposal): RenameProposal? = + protocolEdit(proposal.edit)?.let { edit -> + RenameProposal( + edit, + proposal.sourceModules?.let { modules -> + SourceGraphReplacement( + requireNotNull(proposal.previousSourceModules).map(::SourceModuleConfiguration), + modules.map(::SourceModuleConfiguration), + ) + }, + proposal.scope?.let { scope -> + RenameScope(scope.boundary.name, scope.modules.map(::SourceModuleConfiguration), scope.sourceUris, scope.revision) + }, + ) + } + + /** Unlike willRenameFiles, this proposal includes the requested moves and host graph update. */ + internal fun renameFilesProposal(params: RenameFilesParams): CompletableFuture { + val compiler = adapter as? XdkAdapter ?: return CompletableFuture.completedFuture(null) + val first = params.files.firstOrNull() ?: return CompletableFuture.completedFuture(null) + if (!server.supportsVersionedEdits || !server.supportsFileRenames || + params.files + .map { it.oldUri } + .distinct() + .size != params.files.size ) { - val adapterDiagnostics = params.context.diagnostics.map { Diagnostic.fromLsp(params.textDocument.uri, it) } + return CompletableFuture.completedFuture(null) + } + return queryAsync( + "xtc/renameFiles", + first.oldUri, + { compiler.renameFilesProposalAsync(params.files.associate { it.oldUri to it.newUri }) }, + workspace = true, + ) { proposal -> proposal?.let(::protocolProposal) } + } - adapter - .getCodeActions( + internal fun renameFiles(params: RenameFilesParams): CompletableFuture { + val compiler = adapter as? XdkAdapter ?: return CompletableFuture.completedFuture(null) + val first = params.files.firstOrNull() ?: return CompletableFuture.completedFuture(null) + if ( + !server.supportsVersionedEdits || + params.files + .map { it.oldUri } + .distinct() + .size != params.files.size + ) { + return CompletableFuture.completedFuture(null) + } + return queryAsync( + "workspace/willRenameFiles", + first.oldUri, + { compiler.renameFilesAsync(params.files.associate { it.oldUri to it.newUri }) }, + workspace = true, + ) { + it?.let(::protocolEdit) + } + } + + private fun canConvertEdit(edit: AdapterWorkspaceEdit): Boolean = + (!edit.versioned || server.supportsVersionedEdits) && + ( + edit.renames.isEmpty() || + (edit.versioned && server.supportsVersionedEdits && server.supportsFileRenames) + ) + + /** Called while the document lifecycle is locked, after the query's version checks. */ + private fun protocolEdit(edit: AdapterWorkspaceEdit): WorkspaceEdit? { + if (!canConvertEdit(edit)) return null + val changes = + edit.changes.mapValues { (_, edits) -> + edits.map { TextEdit(it.range.toLsp(), it.newText) } + } + return WorkspaceEdit().apply { + if (edit.versioned) { + this.changes = null + documentChanges = + changes.map { (uri, edits) -> + Either.forLeft( + TextDocumentEdit( + VersionedTextDocumentIdentifier(uri, openDocuments[uri]?.version), + edits.map { Either.forLeft(it) }, + ), + ) + } + + edit.renames.map { (from, to) -> + Either.forRight( + RenameFile(from, to, RenameFileOptions(false, false)), + ) + } + } else { + this.changes = changes + } + } + } + + override fun codeAction(params: CodeActionParams): CompletableFuture>> = + queryAsync( + "textDocument/codeAction", + params.textDocument.uri, + { + adapter.getCodeActionsAsync( params.textDocument.uri, toAdapterRange(params.range), - adapterDiagnostics, - ).map { a -> - Either.forRight( - CodeAction().apply { - title = a.title - kind = a.kind.toLsp() - isPreferred = a.isPreferred - a.edit?.let { e -> - edit = - WorkspaceEdit().apply { - changes = - e.changes.mapValues { (_, edits) -> - edits.map { te -> - TextEdit().apply { - this.range = te.range.toLsp() - newText = te.newText - } - } - } - } - } - }, + params.context.diagnostics.orEmpty().map { + Diagnostic.fromLsp(params.textDocument.uri, it) + }, + ) + }, + workspace = true, + progress = params, + ) { actions -> + if (!server.presentation.codeActions) return@queryAsync emptyList() + actions.mapNotNull { action -> + val kind = action.kind.toLsp() + if (!ClientPresentation.matchesActionKind(kind, params.context.only)) { + return@mapNotNull null + } + val edit = action.edit + if (edit != null && !canConvertEdit(edit)) return@mapNotNull null + val handle = + edit + ?.takeIf { + server.resolvesCodeActionEdit || !server.presentation.actionLiterals + }?.let { + actionReports.remember( + diagnosticRevision, + action.title, + it, + it.changes.values.sumOf { edits -> + edits.sumOf { change -> change.newText.length + 64 } + }, + ) + } + if (!server.presentation.actionLiterals) { + // Legacy clients receive a bounded opaque handle, never an arbitrary edit to + // execute. + if (handle == null) return@mapNotNull null + return@mapNotNull Either.forLeft( + Command( + action.title, + ClientPresentation.APPLY_CODE_ACTION, + listOf(handle, action.title), + ), ) } + val proposed = if (handle == null) edit?.let(::protocolEdit) else null + Either.forRight( + CodeAction().apply { + title = action.title + this.kind = action.kind.toLsp() + isPreferred = + action.isPreferred.takeIf { server.presentation.preferredActions } + this.edit = proposed + data = handle + }, + ) + } + } + + internal fun executeCodeAction(params: ExecuteCommandParams): CompletableFuture = + queryAsync( + "workspace/executeCommand", + "", + sourceOnly = true, + request = { + requireResolve( + server.presentation.applyEdit && + !server.presentation.actionLiterals && + params.command == ClientPresentation.APPLY_CODE_ACTION, + "Code action command", + ) + val arguments = params.arguments.orEmpty() + val title = + when (val value = arguments.getOrNull(1)) { + is String -> value + is JsonPrimitive -> value.takeIf { it.isString }?.asString + else -> null + } ?: throw contentModified() + val edit = + actionReports.resolve( + arguments.firstOrNull(), + diagnosticRevision, + title, + consume = true, + ) + val proposed = protocolEdit(edit) ?: throw contentModified() + // Do not hold the document lifecycle while entering the JSON-RPC transport. + CompletableFuture + .supplyAsync { + server.applyEdit(ApplyWorkspaceEditParams(proposed, title)) + }.thenCompose { it } + .orTimeout(10, SECONDS) + }, + ) { response -> + if (!response.isApplied) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.RequestFailed, + response.failureReason ?: "Client refused the code action", + null, + ), + ) + } + response + } + + override fun resolveCodeAction(action: CodeAction): CompletableFuture = + supplyAsync("codeAction/resolve", action.title) { + if (!server.resolvesCodeActionEdit) { + throw ResponseErrorException( + ResponseError( + ResponseErrorCode.MethodNotFound, + "Code action resolution was not negotiated", + null, + ), + ) + } + if (action.data != null) { + val edit = actionReports.resolve(action.data, diagnosticRevision, action.title) + if (action.edit == null) action.edit = protocolEdit(edit) ?: throw contentModified() + } + action } /** * LSP: textDocument/semanticTokens/full + * * @see org.eclipse.lsp4j.services.TextDocumentService.semanticTokensFull */ override fun semanticTokensFull(params: SemanticTokensParams): CompletableFuture = @@ -557,18 +1517,51 @@ class XtcTextDocumentService( "textDocument/semanticTokens/full", params.textDocument.uri, { result -> - if (result == null) "no tokens" else "${result.data.size} items (${result.data.size / 5} tokens)" + if (result == null) { + "no tokens" + } else { + "${result.data.size} items (${result.data.size / 5} tokens)" + } }, + uri = params.textDocument.uri, ) { adapter.getSemanticTokens(params.textDocument.uri)?.let { tokens -> - SemanticTokens().apply { - data = tokens.data - } + semanticTokenReports.full(params.textDocument.uri, tokens.data) } } + override fun semanticTokensFullDelta( + params: SemanticTokensDeltaParams, + ): CompletableFuture?> = + supplyAsync( + "textDocument/semanticTokens/full/delta", + params.textDocument.uri, + uri = params.textDocument.uri, + ) { + server.requireSemanticTokenRequest(delta = true) + semanticTokenReports.delta( + params.textDocument.uri, + params.previousResultId, + adapter.getSemanticTokens(params.textDocument.uri)?.data.orEmpty(), + ) + } + + override fun semanticTokensRange(params: SemanticTokensRangeParams): CompletableFuture = + supplyAsync( + "textDocument/semanticTokens/range", + params.textDocument.uri, + uri = params.textDocument.uri, + ) { + server.requireSemanticTokenRequest(delta = false) + SemanticTokenReports.range( + adapter.getSemanticTokens(params.textDocument.uri)?.data.orEmpty(), + params.range, + ) + } + /** * LSP: textDocument/inlayHint + * * @see org.eclipse.lsp4j.services.TextDocumentService.inlayHint */ override fun inlayHint(params: InlayHintParams): CompletableFuture> = @@ -576,6 +1569,7 @@ class XtcTextDocumentService( "textDocument/inlayHint", "${params.textDocument.uri} range=${params.range.fmt()}", { result -> "${result.size} hints" }, + uri = params.textDocument.uri, ) { adapter.getInlayHints(params.textDocument.uri, toAdapterRange(params.range)).map { h -> InlayHint().apply { @@ -584,12 +1578,65 @@ class XtcTextDocumentService( kind = h.kind.toLsp() paddingLeft = h.paddingLeft paddingRight = h.paddingRight + val handle = + h.tooltip + ?.takeIf { server.resolvesInlayHintTooltip } + ?.let { + hintReports.remember( + diagnosticRevision, + hintKey(this), + it, + it.length, + ) + } + if (handle == null) { + tooltip = + h.tooltip?.let { + Either.forRight(MarkupContent(MarkupKind.MARKDOWN, it)) + } + } else { + data = handle + } } } } + override fun documentColor(params: DocumentColorParams): CompletableFuture> = + supplyAsync("textDocument/documentColor", params.textDocument.uri, uri = params.textDocument.uri, progress = params) { + requireResolve(AdapterCapability.DOCUMENT_COLOR in adapter.capabilities, "Color values") + if (!openDocuments.containsKey(params.textDocument.uri)) return@supplyAsync emptyList() + adapter.getDocumentColors(params.textDocument.uri).map { + ColorInformation(it.range.toLsp(), Color(it.color.red, it.color.green, it.color.blue, it.color.alpha)) + } + } + + override fun colorPresentation(params: ColorPresentationParams): CompletableFuture> = + supplyAsync("textDocument/colorPresentation", params.textDocument.uri, uri = params.textDocument.uri, progress = params) { + requireResolve(AdapterCapability.DOCUMENT_COLOR in adapter.capabilities, "Color values") + val color = params.color + val range = params.range + if (listOf(color.red, color.green, color.blue, color.alpha).any { !it.isFinite() || it !in 0.0..1.0 } || + listOf(range.start.line, range.start.character, range.end.line, range.end.character).any { it < 0 } || + range.start.line > range.end.line || (range.start.line == range.end.line && range.start.character > range.end.character) + ) { + throw ResponseErrorException( + ResponseError(ResponseErrorCode.InvalidParams, "Expected normalized RGBA channels and an ordered source range", null), + ) + } + if (!openDocuments.containsKey(params.textDocument.uri)) return@supplyAsync emptyList() + adapter + .getColorPresentations( + params.textDocument.uri, + toAdapterRange(range), + ColorValue(color.red, color.green, color.blue, color.alpha), + ).map { + ColorPresentation(it.label).apply { textEdit = TextEdit(it.textEdit.range.toLsp(), it.textEdit.newText) } + } + } + /** * LSP: textDocument/formatting + * * @see org.eclipse.lsp4j.services.TextDocumentService.formatting */ override fun formatting(params: DocumentFormattingParams): CompletableFuture> = @@ -597,9 +1644,10 @@ class XtcTextDocumentService( "textDocument/formatting", params.textDocument.uri, { result -> "${result.size} edits" }, + uri = params.textDocument.uri, ) { val uri = params.textDocument.uri - val content = openDocuments[uri] ?: return@supplyAsync emptyList() + val content = openDocuments[uri]?.content ?: return@supplyAsync emptyList() val options = toAdapterFormattingOptions(params.options) adapter.formatDocument(uri, content, options).map { e -> TextEdit().apply { @@ -611,6 +1659,7 @@ class XtcTextDocumentService( /** * LSP: textDocument/rangeFormatting + * * @see org.eclipse.lsp4j.services.TextDocumentService.rangeFormatting */ override fun rangeFormatting(params: DocumentRangeFormattingParams): CompletableFuture> = @@ -618,9 +1667,10 @@ class XtcTextDocumentService( "textDocument/rangeFormatting", "${params.textDocument.uri} range=${params.range.fmt()}", { result -> "${result.size} edits" }, + uri = params.textDocument.uri, ) { val uri = params.textDocument.uri - val content = openDocuments[uri] ?: return@supplyAsync emptyList() + val content = openDocuments[uri]?.content ?: return@supplyAsync emptyList() val options = toAdapterFormattingOptions(params.options) adapter.formatRange(uri, content, toAdapterRange(params.range), options).map { e -> TextEdit().apply { @@ -630,8 +1680,71 @@ class XtcTextDocumentService( } } + override fun rangesFormatting(params: DocumentRangesFormattingParams): CompletableFuture> = + supplyAsync( + "textDocument/rangesFormatting", + params.textDocument.uri, + { "${it.size} edits" }, + uri = params.textDocument.uri, + ) { + val uri = params.textDocument.uri + val content = openDocuments[uri]?.content ?: return@supplyAsync emptyList() + val text = DocumentText(content) + try { + params.ranges.forEach { text.bounds(it) } + val options = toAdapterFormattingOptions(params.options) + text.nonOverlapping( + params.ranges.flatMap { range -> + adapter.formatRange(uri, content, toAdapterRange(range), options).map { + TextEdit(it.range.toLsp(), it.newText) + } + }, + ) + } catch (e: IllegalArgumentException) { + throw ResponseErrorException( + ResponseError(ResponseErrorCode.InvalidParams, e.message, null), + ) + } + } + + override fun moniker(params: MonikerParams): CompletableFuture> = + queryAsync( + "textDocument/moniker", + params.textDocument.uri, + request = { + val uri = params.textDocument.uri + val library = adapter.readOnlyDocument(uri) + val rendered = openDocuments[uri]?.content + + // LSP 3.18 permits client whitespace normalization in read-only views. EOL + // normalization preserves positions; other rewrites cannot reuse binary ranges. + fun lines(text: String) = text.replace("\r\n", "\n").replace('\r', '\n') + if (library != null && rendered != null && lines(rendered) != lines(library.text)) { + CompletableFuture.completedFuture(emptyList()) + } else { + adapter.findMonikersAsync(uri, params.position.line, params.position.character) + } + }, + progress = params, + partial = PartialResults::list, + ) { monikers -> + monikers.map { + Moniker( + it.scheme, + it.identifier, + UniquenessLevel.Scheme, + when (it.kind) { + SymbolMoniker.Kind.IMPORT -> MonikerKind.Import + SymbolMoniker.Kind.EXPORT -> MonikerKind.Export + SymbolMoniker.Kind.LOCAL -> MonikerKind.Local + }, + ) + } + } + /** * LSP: textDocument/declaration + * * @see org.eclipse.lsp4j.services.TextDocumentService.declaration */ override fun declaration(params: DeclarationParams): CompletableFuture, List>> = @@ -639,14 +1752,23 @@ class XtcTextDocumentService( "textDocument/declaration", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> if (result.left.isEmpty()) "no result" else "found" }, + uri = params.textDocument.uri, + progress = params, + partial = PartialResults::eitherLists, ) { - adapter.findDeclaration(params.textDocument.uri, params.position.line, params.position.character)?.let { - Either.forLeft, List>(listOf(it.toLsp())) - } ?: Either.forLeft(emptyList()) + Either.forLeft( + adapter + .findDeclarations( + params.textDocument.uri, + params.position.line, + params.position.character, + ).map { it.toLsp() }, + ) } /** * LSP: textDocument/typeDefinition + * * @see org.eclipse.lsp4j.services.TextDocumentService.typeDefinition */ override fun typeDefinition(params: TypeDefinitionParams): CompletableFuture, List>> = @@ -654,14 +1776,23 @@ class XtcTextDocumentService( "textDocument/typeDefinition", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> if (result.left.isEmpty()) "no result" else "found" }, + uri = params.textDocument.uri, + progress = params, + partial = PartialResults::eitherLists, ) { - adapter.findTypeDefinition(params.textDocument.uri, params.position.line, params.position.character)?.let { - Either.forLeft, List>(listOf(it.toLsp())) - } ?: Either.forLeft(emptyList()) + Either.forLeft( + adapter + .findTypeDefinitions( + params.textDocument.uri, + params.position.line, + params.position.character, + ).map { it.toLsp() }, + ) } /** * LSP: textDocument/implementation + * * @see org.eclipse.lsp4j.services.TextDocumentService.implementation */ override fun implementation(params: ImplementationParams): CompletableFuture, List>> = @@ -669,14 +1800,23 @@ class XtcTextDocumentService( "textDocument/implementation", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> "${result.left.size} locations" }, + uri = params.textDocument.uri, + progress = params, + partial = PartialResults::eitherLists, ) { Either.forLeft( - adapter.findImplementation(params.textDocument.uri, params.position.line, params.position.character).map { it.toLsp() }, + adapter + .findImplementation( + params.textDocument.uri, + params.position.line, + params.position.character, + ).map { it.toLsp() }, ) } /** * LSP: typeHierarchy/prepareTypeHierarchy + * * @see org.eclipse.lsp4j.services.TextDocumentService.prepareTypeHierarchy */ override fun prepareTypeHierarchy(params: TypeHierarchyPrepareParams): CompletableFuture> = @@ -684,14 +1824,21 @@ class XtcTextDocumentService( "typeHierarchy/prepare", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> "${result.size} items" }, + uri = params.textDocument.uri, ) { - adapter.prepareTypeHierarchy(params.textDocument.uri, params.position.line, params.position.character).map { - it.toLsp(params.textDocument.uri) - } + adapter + .prepareTypeHierarchy( + params.textDocument.uri, + params.position.line, + params.position.character, + ).map { + it.toLsp(params.textDocument.uri) + } } /** * LSP: typeHierarchy/supertypes + * * @see org.eclipse.lsp4j.services.TextDocumentService.typeHierarchySupertypes */ override fun typeHierarchySupertypes(params: TypeHierarchySupertypesParams): CompletableFuture> = @@ -699,12 +1846,16 @@ class XtcTextDocumentService( "typeHierarchy/supertypes", params.item.name, { result -> "${result.size} items" }, + uri = params.item.uri, + progress = params, + partial = PartialResults::list, ) { adapter.getSupertypes(params.item.toAdapter()).map { it.toLsp(params.item.uri) } } /** * LSP: typeHierarchy/subtypes + * * @see org.eclipse.lsp4j.services.TextDocumentService.typeHierarchySubtypes */ override fun typeHierarchySubtypes(params: TypeHierarchySubtypesParams): CompletableFuture> = @@ -712,12 +1863,16 @@ class XtcTextDocumentService( "typeHierarchy/subtypes", params.item.name, { result -> "${result.size} items" }, + uri = params.item.uri, + progress = params, + partial = PartialResults::list, ) { adapter.getSubtypes(params.item.toAdapter()).map { it.toLsp(params.item.uri) } } /** * LSP: callHierarchy/prepare + * * @see org.eclipse.lsp4j.services.TextDocumentService.prepareCallHierarchy */ override fun prepareCallHierarchy(params: CallHierarchyPrepareParams): CompletableFuture> = @@ -725,6 +1880,7 @@ class XtcTextDocumentService( "callHierarchy/prepare", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> "${result.size} items" }, + uri = params.textDocument.uri, ) { adapter .prepareCallHierarchy( @@ -736,6 +1892,7 @@ class XtcTextDocumentService( /** * LSP: callHierarchy/incomingCalls + * * @see org.eclipse.lsp4j.services.TextDocumentService.callHierarchyIncomingCalls */ override fun callHierarchyIncomingCalls(params: CallHierarchyIncomingCallsParams): CompletableFuture> = @@ -743,6 +1900,9 @@ class XtcTextDocumentService( "callHierarchy/incomingCalls", params.item.name, { result -> "${result.size} calls" }, + uri = params.item.uri, + progress = params, + partial = PartialResults::list, ) { adapter.getIncomingCalls(params.item.toAdapterCallItem()).map { c -> CallHierarchyIncomingCall().apply { @@ -754,6 +1914,7 @@ class XtcTextDocumentService( /** * LSP: callHierarchy/outgoingCalls + * * @see org.eclipse.lsp4j.services.TextDocumentService.callHierarchyOutgoingCalls */ override fun callHierarchyOutgoingCalls(params: CallHierarchyOutgoingCallsParams): CompletableFuture> = @@ -761,6 +1922,9 @@ class XtcTextDocumentService( "callHierarchy/outgoingCalls", params.item.name, { result -> "${result.size} calls" }, + uri = params.item.uri, + progress = params, + partial = PartialResults::list, ) { adapter.getOutgoingCalls(params.item.toAdapterCallItem()).map { c -> CallHierarchyOutgoingCall().apply { @@ -772,26 +1936,173 @@ class XtcTextDocumentService( /** * LSP: textDocument/codeLens + * * @see org.eclipse.lsp4j.services.TextDocumentService.codeLens */ override fun codeLens(params: CodeLensParams): CompletableFuture> = - supplyAsync( + queryAsync( "textDocument/codeLens", params.textDocument.uri, - { result -> "${result.size} lenses" }, - ) { - adapter.getCodeLenses(params.textDocument.uri).map { l -> + { codeLensWork(params.textDocument.uri) }, + workspace = true, + ) { lenses -> + lenses.map { l -> CodeLens().apply { range = l.range.toLsp() l.command?.let { cmd -> - command = Command(cmd.title, cmd.command) + val handle = + if (server.resolvesCodeLensCommand) { + lensReports.remember( + diagnosticRevision, + range.fmt(), + cmd.copy(arguments = cmd.arguments.toList()), + cmd.title.length + + cmd.command.length + + cmd.arguments.sumOf { it.toString().length }, + ) + } else { + null + } + if (handle == null) { + command = Command(cmd.title, cmd.command, cmd.arguments.toList()) + } else { + data = handle + } } } } } + private fun codeLensWork(uri: String): CompletableFuture> { + val result = CompletableFuture>() + val settings = server.referenceCodeLens(uri) + result.whenComplete { _, failure -> if (failure != null) settings.cancel(false) } + settings.whenComplete { enabled, failure -> + if (!result.isDone) { + if (failure != null) { + result.completeExceptionally(failure) + } else { + try { + val work = adapter.getCodeLensesAsync(uri, enabled) + result.whenComplete { _, error -> if (error != null) work.cancel(false) } + work.whenComplete { lenses, error -> + if (error == null) result.complete(lenses) else result.completeExceptionally(error) + } + } catch (error: Exception) { + result.completeExceptionally(error) + } + } + } + } + return result + } + + override fun resolveCodeLens(lens: CodeLens): CompletableFuture = + supplyAsync("codeLens/resolve", lens.range.fmt()) { + requireResolve(server.resolvesCodeLensCommand, "Code lens") + if (lens.data != null) { + val command = lensReports.resolve(lens.data, diagnosticRevision, lens.range.fmt()) + lens.command = Command(command.title, command.command, command.arguments.toList()) + } + lens + } + + override fun documentLinkResolve(link: DocumentLink): CompletableFuture = + supplyAsync("documentLink/resolve", link.range.fmt()) { + requireResolve(server.resolvesDocumentLinkTarget, "Document link") + if (link.data != null) { + val resolved = linkReports.resolve(link.data, diagnosticRevision, link.range.fmt()) + link.target = resolved.target?.let(server.readOnlyDocuments::present) + link.tooltip = resolved.tooltip.takeIf { server.presentation.linkTooltips } + } + link + } + + private fun hintKey(hint: InlayHint) = "${hint.position.fmt()}:${hint.label}" + + override fun resolveInlayHint(hint: InlayHint): CompletableFuture = + supplyAsync("inlayHint/resolve", hint.position.fmt()) { + requireResolve(server.resolvesInlayHintTooltip, "Inlay hint") + if (hint.data != null) { + hint.tooltip = + Either.forRight( + MarkupContent( + MarkupKind.MARKDOWN, + hintReports.resolve(hint.data, diagnosticRevision, hintKey(hint)), + ), + ) + } + hint + } + + internal fun workspaceSymbols( + params: WorkspaceSymbolParams, + ): CompletableFuture, List>> = + supplyAsync( + "workspace/symbol", + params.query, + progress = params, + partial = PartialResults::eitherLists, + ) { + val symbols = adapter.findWorkspaceSymbols(params.query) + if (!server.resolvesWorkspaceSymbolRange) { + return@supplyAsync Either.forLeft( + symbols.map { + SymbolInformation( + it.name, + server.presentation.symbolKind(it.kind.toLsp(), workspace = true), + it.location.toLsp(), + ) + }, + ) + } + Either.forRight( + symbols.map { symbol -> + WorkspaceSymbol().apply { + name = symbol.name + kind = server.presentation.symbolKind(symbol.kind.toLsp(), workspace = true) + val handle = + if (server.resolvesWorkspaceSymbolRange) { + symbolReports.remember( + diagnosticRevision, + "$name:$kind", + symbol.location, + symbol.location.uri.length + 64, + ) + } else { + null + } + if (handle == null) { + location = Either.forLeft(symbol.location.toLsp()) + } else { + location = Either.forRight(WorkspaceSymbolLocation(symbol.location.uri)) + data = handle + } + } + }, + ) + } + + internal fun resolveWorkspaceSymbol(symbol: WorkspaceSymbol): CompletableFuture = + supplyAsync("workspaceSymbol/resolve", symbol.name) { + requireResolve(server.resolvesWorkspaceSymbolRange, "Workspace symbol") + if (symbol.data != null) { + symbol.location = + Either.forLeft( + symbolReports + .resolve( + symbol.data, + diagnosticRevision, + "${symbol.name}:${symbol.kind}", + ).toLsp(), + ) + } + symbol + } + /** * LSP: textDocument/onTypeFormatting + * * @see org.eclipse.lsp4j.services.TextDocumentService.onTypeFormatting */ override fun onTypeFormatting(params: DocumentOnTypeFormattingParams): CompletableFuture> = @@ -804,15 +2115,13 @@ class XtcTextDocumentService( } else { val preview = result.take(2).joinToString("; ") { edit -> - val text = - edit.newText - .replace("\n", "\\n") - .replace("\t", "\\t") + val text = edit.newText.replace("\n", "\\n").replace("\t", "\\t") "${edit.range.start.line}:${edit.range.start.character}-${edit.range.end.line}:${edit.range.end.character}='$text'" } "${result.size} edits [$preview]" } }, + uri = params.textDocument.uri, ) { val options = AdapterFormattingOptions( @@ -836,6 +2145,7 @@ class XtcTextDocumentService( /** * LSP: textDocument/linkedEditingRange + * * @see org.eclipse.lsp4j.services.TextDocumentService.linkedEditingRange */ override fun linkedEditingRange(params: LinkedEditingRangeParams): CompletableFuture = @@ -843,6 +2153,7 @@ class XtcTextDocumentService( "textDocument/linkedEditingRange", "${params.textDocument.uri} at ${params.position.fmt()}", { result -> "${result.ranges?.size ?: 0} ranges" }, + uri = params.textDocument.uri, ) { adapter .getLinkedEditingRanges( @@ -861,21 +2172,24 @@ class XtcTextDocumentService( // Conversion helpers for hierarchy types // ==================================================================== - private fun AdapterTypeHierarchyItem.toLsp(defaultUri: String): org.eclipse.lsp4j.TypeHierarchyItem { + private fun AdapterLocation.toLsp(): Location = + Location(server.readOnlyDocuments.present(uri), Range(Position(startLine, startColumn), Position(endLine, endColumn))) + + private fun AdapterTypeHierarchyItem.toLsp(defaultUri: String): TypeHierarchyItem { val resolvedUri = this.uri.ifEmpty { defaultUri } - return org.eclipse.lsp4j - .TypeHierarchyItem( - this.name, - this.kind.toLsp(), - resolvedUri, - this.range.toLsp(), - this.selectionRange.toLsp(), - ).apply { - this.detail = this@toLsp.detail - } + return TypeHierarchyItem( + this.name, + this.kind.toLsp(), + server.readOnlyDocuments.present(resolvedUri), + this.range.toLsp(), + this.selectionRange.toLsp(), + ).apply { + this.detail = this@toLsp.detail + this.data = this@toLsp.data + } } - private fun org.eclipse.lsp4j.TypeHierarchyItem.toAdapter(): AdapterTypeHierarchyItem = + private fun TypeHierarchyItem.toAdapter(): AdapterTypeHierarchyItem = AdapterTypeHierarchyItem( name = name, kind = SymbolInfo.SymbolKind.CLASS, @@ -883,22 +2197,29 @@ class XtcTextDocumentService( range = toAdapterRange(range), selectionRange = toAdapterRange(selectionRange), detail = detail, + data = + when (val value = data) { + is String -> value + is JsonPrimitive -> if (value.isString) value.asString else null + else -> null + }, ) - private fun AdapterCallHierarchyItem.toLspCallItem(): org.eclipse.lsp4j.CallHierarchyItem { + private fun AdapterCallHierarchyItem.toLspCallItem(): CallHierarchyItem { val result = - org.eclipse.lsp4j.CallHierarchyItem( + CallHierarchyItem( this.name, this.kind.toLsp(), - this.uri, + server.readOnlyDocuments.present(this.uri), this.range.toLsp(), this.selectionRange.toLsp(), ) result.detail = this.detail + result.data = this.data return result } - private fun org.eclipse.lsp4j.CallHierarchyItem.toAdapterCallItem(): AdapterCallHierarchyItem = + private fun CallHierarchyItem.toAdapterCallItem(): AdapterCallHierarchyItem = AdapterCallHierarchyItem( name = name, kind = SymbolInfo.SymbolKind.METHOD, @@ -906,9 +2227,15 @@ class XtcTextDocumentService( range = toAdapterRange(range), selectionRange = toAdapterRange(selectionRange), detail = detail, + data = + when (val value = data) { + is String -> value + is JsonPrimitive -> value.takeIf { it.isString }?.asString + else -> null + }, ) - private fun toAdapterRange(range: org.eclipse.lsp4j.Range) = + private fun toAdapterRange(range: Range) = AdapterRange( AdapterPosition(range.start.line, range.start.character), AdapterPosition(range.end.line, range.end.character), diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcWorkspaceService.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcWorkspaceService.kt index d3b9e5eef55..ddbb4976fe4 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcWorkspaceService.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcWorkspaceService.kt @@ -1,20 +1,31 @@ package org.xvm.lsp.server +import org.eclipse.lsp4j.CreateFilesParams +import org.eclipse.lsp4j.DeleteFilesParams import org.eclipse.lsp4j.DidChangeConfigurationParams import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams +import org.eclipse.lsp4j.ExecuteCommandParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.RenameFilesParams import org.eclipse.lsp4j.SymbolInformation +import org.eclipse.lsp4j.TextDocumentContentParams +import org.eclipse.lsp4j.TextDocumentContentResult +import org.eclipse.lsp4j.WorkspaceDiagnosticParams +import org.eclipse.lsp4j.WorkspaceDiagnosticReport +import org.eclipse.lsp4j.WorkspaceEdit import org.eclipse.lsp4j.WorkspaceSymbol import org.eclipse.lsp4j.WorkspaceSymbolParams import org.eclipse.lsp4j.jsonrpc.messages.Either import org.eclipse.lsp4j.services.WorkspaceService import org.slf4j.LoggerFactory import org.xvm.lsp.adapter.Adapter -import org.xvm.lsp.model.toLsp import java.util.concurrent.CompletableFuture /** - * Workspace service for Ecstasy Language Server. - * Handles workspace-wide features like symbol search and configuration changes. + * Workspace service for Ecstasy Language Server. Handles workspace-wide features like symbol search + * and configuration changes. */ class XtcWorkspaceService( private val server: XtcLanguageServer, @@ -24,54 +35,95 @@ class XtcWorkspaceService( private val logger = LoggerFactory.getLogger(XtcWorkspaceService::class.java) } - private fun supplyAsync( - method: String, - logParams: String, - logResult: (R) -> String = { "completed" }, - block: () -> R, - ): CompletableFuture = server.supplyAsync(method, logParams, logResult, block) + private val fileChanges = FileChangeSnapshots() + + override fun textDocumentContent(params: TextDocumentContentParams): CompletableFuture = + server.readOnlyDocuments.content(params.uri) + + override fun diagnostic(params: WorkspaceDiagnosticParams): CompletableFuture = + server.workspaceDiagnostics(params) /** * LSP: workspace/didChangeConfiguration + * * @see org.eclipse.lsp4j.services.WorkspaceService.didChangeConfiguration */ override fun didChangeConfiguration(params: DidChangeConfigurationParams) { - logger.info("workspace/didChangeConfiguration: re-requesting formatting config") + logger.info("workspace/didChangeConfiguration: updating editor configuration") server.requestFormattingConfig() + server.refreshPresentation() + server.changeCompilerConfig(params.settings) } /** * LSP: workspace/didChangeWatchedFiles + * * @see org.eclipse.lsp4j.services.WorkspaceService.didChangeWatchedFiles */ override fun didChangeWatchedFiles(params: DidChangeWatchedFilesParams) { logger.info("workspace/didChangeWatchedFiles: {} changes", params.changes.size) - for (change in params.changes) { - adapter.didChangeWatchedFile(change.uri, change.type.value) + refreshFiles(params.changes) + } + + override fun willRenameFiles(params: RenameFilesParams): CompletableFuture = server.willRenameFiles(params) + + // Creation/deletion do not justify rewriting references. Post-operation diagnostics reflect + // the new membership; a pre-operation hook must never modify files speculatively. + override fun willCreateFiles(params: CreateFilesParams): CompletableFuture = CompletableFuture.completedFuture(null) + + override fun willDeleteFiles(params: DeleteFilesParams): CompletableFuture = CompletableFuture.completedFuture(null) + + override fun didCreateFiles(params: CreateFilesParams): Unit = + refreshFiles(params.files.map { FileEvent(it.uri, FileChangeType.Created) }) + + override fun didDeleteFiles(params: DeleteFilesParams): Unit = + refreshFiles(params.files.map { FileEvent(it.uri, FileChangeType.Deleted) }) + + /** File operations can arrive without watcher notifications, especially after a client edit. */ + override fun didRenameFiles(params: RenameFilesParams) { + logger.info("workspace/didRenameFiles: {} renames", params.files.size) + refreshFiles( + params.files.flatMap { + listOf( + FileEvent(it.oldUri, FileChangeType.Deleted), + FileEvent(it.newUri, FileChangeType.Created), + ) + }, + ) + } + + private fun refreshFiles(events: List) { + val changes = fileChanges.changed(events) + if (changes.isEmpty()) return + // Resource content changes cannot add/remove source modules. Membership events still + // rediscover, including directory events that can contain several source files. + if (changes.any { it.type != FileChangeType.Changed || it.uri.endsWith(".x") }) { + server.refreshCompilerDiscovery() } + changes + .distinctBy { it.uri to it.type } + .forEach { change -> + adapter.didChangeWatchedFile(change.uri, change.type.value) + server.refreshForFile(change.uri) + } + } + + override fun didChangeWorkspaceFolders(params: DidChangeWorkspaceFoldersParams) { + server.changeCompilerWorkspaceFolders( + params.event.added.map { it.uri }, + params.event.removed.map { it.uri }, + ) } /** * LSP: workspace/symbol + * * @see org.eclipse.lsp4j.services.WorkspaceService.symbol */ - @Suppress("ktlint:standard:function-signature") - override fun symbol( - params: WorkspaceSymbolParams, - ): CompletableFuture, List>> = - supplyAsync( - "workspace/symbol", - "query='${params.query}'", - { result -> "${result.right.size} symbols" }, - ) { - Either.forRight( - adapter.findWorkspaceSymbols(params.query).map { s -> - WorkspaceSymbol().apply { - name = s.name - kind = s.kind.toLsp() - location = Either.forLeft(s.location.toLsp()) - } - }, - ) - } + override fun symbol(params: WorkspaceSymbolParams): CompletableFuture, List>> = + server.workspaceSymbols(params) + + override fun executeCommand(params: ExecuteCommandParams): CompletableFuture = server.executeCodeAction(params) + + override fun resolveWorkspaceSymbol(symbol: WorkspaceSymbol): CompletableFuture = server.resolveWorkspaceSymbol(symbol) } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/Platform.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/Platform.kt index a9527f0d702..61da2992d4d 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/Platform.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/Platform.kt @@ -1,8 +1,6 @@ package org.xvm.lsp.treesitter -/** - * Native library platform detection for tree-sitter. - */ +/** Native library platform detection for tree-sitter. */ object Platform { /** Platform identifier (e.g., "darwin-arm64", "linux-x64", "windows-x64") */ val id: String @@ -11,7 +9,7 @@ object Platform { val libExtension: String /** Library name prefix ("lib" on Unix, empty on Windows) */ - val libPrefix: String + private val libPrefix: String init { val osName = System.getProperty("os.name").lowercase() @@ -43,9 +41,15 @@ object Platform { } } - /** Returns the native library filename for the given base name (e.g., "tree-sitter-xtc" -> "libtree-sitter-xtc.dylib") */ + /** + * Returns the native library filename for the given base name (e.g., "tree-sitter-xtc" -> + * "libtree-sitter-xtc.dylib") + */ fun libraryFileName(baseName: String): String = "$libPrefix$baseName$libExtension" - /** Returns the resource path for a native library (e.g., "/native/darwin-arm64/libtree-sitter-xtc.dylib") */ + /** + * Returns the resource path for a native library (e.g., + * "/native/darwin-arm64/libtree-sitter-xtc.dylib") + */ fun resourcePath(baseName: String): String = "/native/$id/${libraryFileName(baseName)}" } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt index 05c9e82c6fb..65e93f73149 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt @@ -3,8 +3,8 @@ package org.xvm.lsp.treesitter /** * Standard LSP semantic token legend: token types and modifiers. * - * These lists are sent to the client during initialization so it knows how to interpret - * the integer indices in the token data array. + * These lists are sent to the client during initialization so it knows how to interpret the integer + * indices in the token data array. */ object SemanticTokenLegend { val tokenTypes: List = @@ -57,8 +57,8 @@ object SemanticTokenLegend { /** * Walks an XTC tree-sitter AST and produces LSP semantic token data. * - * A fresh instance should be created per request (accumulates mutable state). - * Thread-safe since each `supplyAsync` gets its own instance. + * A fresh instance should be created per request (accumulates mutable state). Thread-safe since + * each `supplyAsync` gets its own instance. */ class SemanticTokenEncoder { private val declarationKeywords = @@ -139,7 +139,11 @@ class SemanticTokenEncoder { node.childByFieldName("name") ?: node.children.firstOrNull { it.type == "identifier" } ?: return - emitToken(name, "enumMember", SemanticTokenLegend.modifierBitmask("declaration", "readonly")) + emitToken( + name, + "enumMember", + SemanticTokenLegend.modifierBitmask("declaration", "readonly"), + ) markClassified(name) } @@ -153,7 +157,8 @@ class SemanticTokenEncoder { node.childByFieldName("name")?.let { id -> val deprecated = "deprecated".takeIf { hasDeprecatedAnnotation(node) } - val mods = buildModifiers(node, *listOfNotNull("declaration", deprecated).toTypedArray()) + val mods = + buildModifiers(node, *listOfNotNull("declaration", deprecated).toTypedArray()) emitToken(id, "method", mods) } @@ -193,7 +198,8 @@ class SemanticTokenEncoder { node.childByFieldName("name")?.let { id -> val deprecated = "deprecated".takeIf { hasDeprecatedAnnotation(node) } - val mods = buildModifiers(node, *listOfNotNull("declaration", deprecated).toTypedArray()) + val mods = + buildModifiers(node, *listOfNotNull("declaration", deprecated).toTypedArray()) emitToken(id, "property", mods) } } @@ -267,9 +273,11 @@ class SemanticTokenEncoder { } private fun emitDeclarationKeyword(node: XtcNode) { - node.children.firstOrNull { it.text in declarationKeywords }?.let { keyword -> - emitToken(keyword, "keyword", 0) - } + node.children + .firstOrNull { it.text in declarationKeywords } + ?.let { keyword -> + emitToken(keyword, "keyword", 0) + } } private fun emitModifierTokens(node: XtcNode) { @@ -283,9 +291,15 @@ class SemanticTokenEncoder { emitToken(child, "modifier", 0) } - child.text == "override" || child.text == "final" || child.text == "native" || - child.text == "lazy" || child.text == "atomic" || child.text == "inject" || - child.text == "delegate" || child.text == "readonly" || child.text == "immutable" -> { + child.text == "override" || + child.text == "final" || + child.text == "native" || + child.text == "lazy" || + child.text == "atomic" || + child.text == "inject" || + child.text == "delegate" || + child.text == "readonly" || + child.text == "immutable" -> { emitToken(child, "modifier", 0) } } @@ -297,7 +311,11 @@ class SemanticTokenEncoder { for (child in typeParams.children) { if (child.type == "type_parameter") { child.childByFieldName("name")?.let { id -> - emitToken(id, "typeParameter", SemanticTokenLegend.modifierBitmask("declaration")) + emitToken( + id, + "typeParameter", + SemanticTokenLegend.modifierBitmask("declaration"), + ) markClassified(id) } child.childByFieldName("constraint")?.let { constraint -> @@ -343,8 +361,7 @@ class SemanticTokenEncoder { private fun hasDeprecatedAnnotation(node: XtcNode): Boolean = node.children.any { child -> - child.type == "annotation" && - child.childByFieldName("name")?.text == "Deprecated" + child.type == "annotation" && child.childByFieldName("name")?.text == "Deprecated" } private fun buildModifiers( @@ -402,27 +419,27 @@ class SemanticTokenEncoder { var prevLine = 0 var prevColumn = 0 - for (token in sortedTokens) { - val deltaLine = token.line - prevLine - val deltaStart = if (deltaLine == 0) token.column - prevColumn else token.column + for ((line, column, length, tokenType, tokenModifiers) in sortedTokens) { + val deltaLine = line - prevLine + val deltaStart = if (deltaLine == 0) column - prevColumn else column result.add(deltaLine) result.add(deltaStart) - result.add(token.length) - result.add(token.tokenType) - result.add(token.tokenModifiers) + result.add(length) + result.add(tokenType) + result.add(tokenModifiers) - prevLine = token.line - prevColumn = token.column + prevLine = line + prevColumn = column } return result } /** - * Unique key for deduplication. Combines line, column, AND node type to avoid - * collisions when a parent and child node share the same start position - * (e.g., `type_name` wrapping an `identifier` at the same column). + * Unique key for deduplication. Combines line, column, AND node type to avoid collisions when a + * parent and child node share the same start position (e.g., `type_name` wrapping an + * `identifier` at the same column). */ private fun nodeKey(node: XtcNode): Long { val typeHash = node.type.hashCode().toLong() and 0xFFFFL diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/TreeSitterLibraryLookup.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/TreeSitterLibraryLookup.kt index 2f0e0913c7a..804f76f4814 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/TreeSitterLibraryLookup.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/TreeSitterLibraryLookup.kt @@ -10,8 +10,8 @@ import java.nio.file.StandardCopyOption /** * Custom NativeLibraryLookup implementation that loads libtree-sitter from JAR resources. * - * jtreesitter requires the tree-sitter runtime library (libtree-sitter) but doesn't bundle it. - * This implementation extracts the library from JAR resources to a temp file and loads it. + * jtreesitter requires the tree-sitter runtime library (libtree-sitter) but doesn't bundle it. This + * implementation extracts the library from JAR resources to a temp file and loads it. * * The library is bundled at: /native//libtree-sitter. */ diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcNode.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcNode.kt index 65712a27a72..517cf0f35cb 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcNode.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcNode.kt @@ -11,253 +11,215 @@ import java.util.Optional * * ## API Completeness Note * - * This class exposes the complete tree-sitter Node API, even though not all methods are - * currently used. The unused methods are intentionally included for future LSP features: + * This class exposes the complete tree-sitter Node API, even though not all methods are currently + * used. The unused methods are intentionally included for future LSP features: * - * | Method | Future Use | - * |--------|------------| - * | `child(index)`, `namedChild(index)` | Manual tree traversal for formatting, folding | - * | `childCount`, `namedChildCount` | Iteration bounds for tree walking | - * | `namedChildren` | Filtered iteration for semantic analysis | - * | `nextSibling`, `prevSibling` | Folding ranges, statement grouping | - * | `nextNamedSibling`, `prevNamedSibling` | Sibling-aware code actions | - * | `startByte`, `endByte` | Byte-level edits, incremental parsing | - * | `isNamed` | Filtering named vs anonymous (punctuation) nodes | + * | Method | Future Use | + * |----------------------------------------|--------------------------------------------------| + * | `child(index)`, `namedChild(index)` | Manual tree traversal for formatting, folding | + * | `childCount`, `namedChildCount` | Iteration bounds for tree walking | + * | `namedChildren` | Filtered iteration for semantic analysis | + * | `nextSibling`, `prevSibling` | Folding ranges, statement grouping | + * | `nextNamedSibling`, `prevNamedSibling` | Sibling-aware code actions | + * | `startByte`, `endByte` | Byte-level edits, incremental parsing | + * | `isNamed` | Filtering named vs anonymous (punctuation) nodes | * * @see XtcTree * @see XtcQueryEngine */ -class XtcNode internal constructor( - private val tsNode: Node, - private val source: String, -) : AdapterNode { - /** - * The type of this node (e.g., "class_declaration", "identifier"). - */ - override val type: String - get() = tsNode.type - - /** - * The text content of this node from the source. - * - * Tree-sitter reports byte offsets into a UTF-8 representation, but Java Strings use - * UTF-16 code units. For ASCII-only sources (all current XTC code), byte offsets equal - * character offsets. For non-ASCII sources we convert via the UTF-8 byte array to get - * the correct substring. - */ - override val text: String - get() { - val start = tsNode.startByte - val end = tsNode.endByte - // Defensive: guard against stale byte offsets (e.g., from incremental parse without Tree.edit()) - if (start < 0 || end < start || end > source.length) { - return "" - } - // Fast path: if all chars are ASCII, byte offsets == char offsets - if (source - .asSequence() - .drop(start) - .take(end - start) - .all { it.code < 128 } - ) { - return source.substring(start, end) +class XtcNode + internal constructor( + private val tsNode: Node, + private val source: String, + ) : AdapterNode { + /** The type of this node (e.g., "class_declaration", "identifier"). */ + override val type: String + get() = tsNode.type + + /** + * The text content of this node from the source. + * + * Tree-sitter reports byte offsets into a UTF-8 representation, but Java Strings use UTF-16 + * code units. For ASCII-only sources (all current XTC code), byte offsets equal character + * offsets. For non-ASCII sources we convert via the UTF-8 byte array to get the correct + * substring. + */ + override val text: String + get() { + val start = tsNode.startByte + val end = tsNode.endByte + // Defensive: guard against stale byte offsets (e.g., from incremental parse without + // Tree.edit()) + if (start !in 0..end || end > source.length) { + return "" + } + // Fast path: if all chars are ASCII, byte offsets == char offsets + if (source + .asSequence() + .drop(start) + .take(end - start) + .all { it.code < 128 } + ) { + return source.substring(start, end) + } + // Slow path: convert to UTF-8 bytes, slice, and decode back + val utf8 = source.toByteArray(Charsets.UTF_8) + if (end > utf8.size) return "" + return String(utf8, start, end - start, Charsets.UTF_8) } - // Slow path: convert to UTF-8 bytes, slice, and decode back - val utf8 = source.toByteArray(Charsets.UTF_8) - if (end > utf8.size) return "" - return String(utf8, start, end - start, Charsets.UTF_8) - } - - /** - * Whether this is a named node (appears in grammar rules) vs anonymous (literal tokens). - */ - @Suppress("unused") // TODO: Will be used for semantic tokens - filtering out punctuation nodes - val isNamed: Boolean - get() = tsNode.isNamed - - /** - * Whether this node represents a syntax error. - */ - override val isError: Boolean - get() = tsNode.isError - - /** - * Whether this node is missing (inserted by error recovery). - */ - override val isMissing: Boolean - get() = tsNode.isMissing - - /** - * Whether this node or any of its descendants has an error. - */ - val hasError: Boolean - get() = tsNode.hasError() - - /** - * The 0-based start line of this node. - */ - override val startLine: Int - get() = tsNode.startPoint.row() - - /** - * The 0-based start column of this node. - */ - override val startColumn: Int - get() = tsNode.startPoint.column() - - /** - * The 0-based end line of this node. - */ - override val endLine: Int - get() = tsNode.endPoint.row() - - /** - * The 0-based end column of this node. - */ - override val endColumn: Int - get() = tsNode.endPoint.column() - - /** - * The byte offset where this node starts. - */ - @Suppress("unused") // TODO: Will be used for incremental parsing and byte-level text edits - val startByte: Int - get() = tsNode.startByte - - /** - * The byte offset where this node ends. - */ - @Suppress("unused") // TODO: Will be used for incremental parsing and byte-level text edits - val endByte: Int - get() = tsNode.endByte - - /** - * The number of children this node has. - */ - @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding - val childCount: Int - get() = tsNode.childCount - - /** - * The number of named children this node has. - */ - @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding - val namedChildCount: Int - get() = tsNode.namedChildCount - - /** - * Get the parent node, or null if this is the root. - */ - override val parent: XtcNode? - get() = tsNode.parent.wrap() - - /** - * Get a child node by index. - */ - @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding - fun child(index: Int): XtcNode? = tsNode.getChild(index).wrap() - - /** - * Get a named child node by index. - */ - @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding - fun namedChild(index: Int): XtcNode? = tsNode.getNamedChild(index).wrap() - - /** - * Get a child node by field name (O(1) lookup via tree-sitter's internal field table). - * - * The XTC grammar defines field names on all major constructs via `field()` in grammar.js, - * enabling direct semantic access to named children. This is the **preferred** API for - * navigating the AST because: - * - * - **O(1) performance**: tree-sitter resolves fields via a compile-time field table, - * unlike [childByType] which scans all children linearly (O(n)). - * - **Position-independent**: fields identify children by semantic role, not by their - * position among siblings. Adding optional children (e.g., annotations, modifiers) - * before a node won't break field-based lookups. - * - **Self-documenting**: `node.childByFieldName("name")` is clearer than - * `node.childByType("identifier")` which could match any identifier child. - * - * ## Available Fields by Node Type - * - * **Declarations**: `name`, `type_params`, `body`, `return_type`, `parameters`, `type`, `value` - * **Expressions**: `function`, `arguments`, `object`, `member`, `left`, `right`, `size` - * **Statements**: `condition`, `consequence`, `alternative`, `body`, `iterable`, `label` - * **Other**: `path`/`alias` (imports), `constraint` (type_parameter), `default` (parameter) - * - * ## What Can Be Built on Top of Fields - * - * Fields unlock higher-level features that were previously fragile or impossible: - * - **Tree-sitter queries** can use `name:` field syntax for robust pattern matching - * - **Rename refactoring** can reliably find the `name` field of any declaration - * - **Signature help** can extract `parameters` and `return_type` without positional guessing - * - **Semantic tokens** can classify `member` vs `object` in member expressions - * - **Code navigation** can distinguish a method's `body` from its `parameters` - * - * @see childByType for fallback when a grammar node type lacks field definitions - */ - override fun childByFieldName(fieldName: String): XtcNode? = tsNode.getChildByFieldName(fieldName).wrap() - - /** - * Get the first child node with the given type (O(n) linear scan). - * - * This is a **fallback** for nodes that don't have field definitions in the grammar, - * or for querying anonymous/keyword children (e.g., `"construct"`, `"static"`). - * - * Prefer [childByFieldName] when a field is available -- it is faster (O(1)), - * position-independent, and self-documenting. - */ - override fun childByType(nodeType: String): XtcNode? = children.find { it.type == nodeType } - - /** - * Get all children of this node. - */ - override val children: List - get() = (0 until tsNode.childCount).mapNotNull { child(it) } - - /** - * Get all named children of this node. - */ - @Suppress("unused") // TODO: Will be used for semantic analysis and document symbols - val namedChildren: List - get() = (0 until tsNode.namedChildCount).mapNotNull { namedChild(it) } - - /** - * Get the next sibling node. - */ - @Suppress("unused") // TODO: Will be used for folding ranges and statement grouping - val nextSibling: XtcNode? - get() = tsNode.nextSibling.wrap() - - /** - * Get the previous sibling node. - */ - @Suppress("unused") // TODO: Will be used for folding ranges and statement grouping - val prevSibling: XtcNode? - get() = tsNode.prevSibling.wrap() - - /** - * Get the next named sibling node. - */ - @Suppress("unused") // TODO: Will be used for sibling-aware code actions - val nextNamedSibling: XtcNode? - get() = tsNode.nextNamedSibling.wrap() - - /** - * Get the previous named sibling node. - */ - @Suppress("unused") // TODO: Will be used for sibling-aware code actions - val prevNamedSibling: XtcNode? - get() = tsNode.prevNamedSibling.wrap() - - /** - * Get the underlying tree-sitter node for advanced operations. - */ - @Suppress("unused") // TODO: Will be used for advanced tree-sitter operations not covered by this wrapper - internal fun getTsNode(): Node = tsNode - - /** Convert a Java Optional to an XtcNode?, avoiding Java's Optional.map() in favor of Kotlin's ?. */ - private fun Optional.wrap(): XtcNode? = orElse(null)?.let { XtcNode(it, source) } - override fun toString(): String = "XtcNode(type=$type, range=[$startLine:$startColumn-$endLine:$endColumn])" -} + /** Whether this is a named node (appears in grammar rules) vs anonymous (literal tokens). */ + @Suppress("unused") // TODO: Will be used for semantic tokens - filtering out punctuation nodes + val isNamed: Boolean + get() = tsNode.isNamed + + /** Whether this node represents a syntax error. */ + override val isError: Boolean + get() = tsNode.isError + + /** Whether this node is missing (inserted by error recovery). */ + override val isMissing: Boolean + get() = tsNode.isMissing + + /** Whether this node or any of its descendants has an error. */ + val hasError: Boolean + get() = tsNode.hasError() + + /** The 0-based start line of this node. */ + override val startLine: Int + get() = tsNode.startPoint.row() + + /** The 0-based start column of this node. */ + override val startColumn: Int + get() = tsNode.startPoint.column() + + /** The 0-based end line of this node. */ + override val endLine: Int + get() = tsNode.endPoint.row() + + /** The 0-based end column of this node. */ + override val endColumn: Int + get() = tsNode.endPoint.column() + + /** The byte offset where this node starts. */ + @Suppress("unused") // TODO: Will be used for incremental parsing and byte-level text edits + val startByte: Int + get() = tsNode.startByte + + /** The byte offset where this node ends. */ + @Suppress("unused") // TODO: Will be used for incremental parsing and byte-level text edits + val endByte: Int + get() = tsNode.endByte + + /** The number of children this node has. */ + @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding + val childCount: Int + get() = tsNode.childCount + + /** The number of named children this node has. */ + @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding + val namedChildCount: Int + get() = tsNode.namedChildCount + + /** Get the parent node, or null if this is the root. */ + override val parent: XtcNode? + get() = tsNode.parent.wrap() + + /** Get a child node by index. */ + @Suppress("unused") // TODO: Will be used for manual tree traversal in formatting and folding + fun child(index: Int): XtcNode? = tsNode.getChild(index).wrap() + + /** Get a named child node by index. */ + @Suppress("unused") + private // TODO: Will be used for manual tree traversal in formatting and folding + fun namedChild(index: Int): XtcNode? = tsNode.getNamedChild(index).wrap() + + /** + * Get a child node by field name (O(1) lookup via tree-sitter's internal field table). + * + * The XTC grammar defines field names on all major constructs via `field()` in grammar.js, + * enabling direct semantic access to named children. This is the **preferred** API for + * navigating the AST because: + * + * - **O(1) performance**: tree-sitter resolves fields via a compile-time field table, unlike + * [childByType] which scans all children linearly (O(n)). + * - **Position-independent**: fields identify children by semantic role, not by their position + * among siblings. Adding optional children (e.g., annotations, modifiers) before a node won't + * break field-based lookups. + * - **Self-documenting**: `node.childByFieldName("name")` is clearer than + * `node.childByType("identifier")` which could match any identifier child. + * + * ## Available Fields by Node Type + * + * **Declarations**: `name`, `type_params`, `body`, `return_type`, `parameters`, `type`, `value` + * **Expressions**: `function`, `arguments`, `object`, `member`, `left`, `right`, `size` + * **Statements**: `condition`, `consequence`, `alternative`, `body`, `iterable`, `label` + * **Other**: `path`/`alias` (imports), `constraint` (type_parameter), `default` (parameter) + * + * ## What Can Be Built on Top of Fields + * + * Fields unlock higher-level features that were previously fragile or impossible: + * - **Tree-sitter queries** can use `name:` field syntax for robust pattern matching + * - **Rename refactoring** can reliably find the `name` field of any declaration + * - **Signature help** can extract `parameters` and `return_type` without positional guessing + * - **Semantic tokens** can classify `member` vs `object` in member expressions + * - **Code navigation** can distinguish a method's `body` from its `parameters` + * + * @see childByType for fallback when a grammar node type lacks field definitions + */ + override fun childByFieldName(fieldName: String): XtcNode? = tsNode.getChildByFieldName(fieldName).wrap() + + /** + * Get the first child node with the given type (O(n) linear scan). + * + * This is a **fallback** for nodes that don't have field definitions in the grammar, or for + * querying anonymous/keyword children (e.g., `"construct"`, `"static"`). + * + * Prefer [childByFieldName] when a field is available -- it is faster (O(1)), + * position-independent, and self-documenting. + */ + override fun childByType(nodeType: String): XtcNode? = children.find { it.type == nodeType } + + /** Get all children of this node. */ + override val children: List + get() = (0 until tsNode.childCount).mapNotNull { child(it) } + + /** Get all named children of this node. */ + @Suppress("unused") // TODO: Will be used for semantic analysis and document symbols + val namedChildren: List + get() = (0 until tsNode.namedChildCount).mapNotNull { namedChild(it) } + + /** Get the next sibling node. */ + @Suppress("unused") // TODO: Will be used for folding ranges and statement grouping + val nextSibling: XtcNode? + get() = tsNode.nextSibling.wrap() + + /** Get the previous sibling node. */ + @Suppress("unused") // TODO: Will be used for folding ranges and statement grouping + val prevSibling: XtcNode? + get() = tsNode.prevSibling.wrap() + + /** Get the next named sibling node. */ + @Suppress("unused") // TODO: Will be used for sibling-aware code actions + val nextNamedSibling: XtcNode? + get() = tsNode.nextNamedSibling.wrap() + + /** Get the previous named sibling node. */ + @Suppress("unused") // TODO: Will be used for sibling-aware code actions + val prevNamedSibling: XtcNode? + get() = tsNode.prevNamedSibling.wrap() + + /** Get the underlying tree-sitter node for advanced operations. */ + @Suppress( + "unused", + ) // TODO: Will be used for advanced tree-sitter operations not covered by this wrapper + internal fun getTsNode(): Node = tsNode + + /** + * Convert a Java Optional to an XtcNode?, avoiding Java's Optional.map() in favor of + * Kotlin's ?. + */ + private fun Optional.wrap(): XtcNode? = orElse(null)?.let { XtcNode(it, source) } + + override fun toString(): String = "XtcNode(type=$type, range=[$startLine:$startColumn-$endLine:$endColumn])" + } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcParser.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcParser.kt index 0f2000f3e4e..3b05d7cdfb3 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcParser.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcParser.kt @@ -13,201 +13,201 @@ import java.nio.file.StandardCopyOption /** * Parser wrapper for XTC sources using Tree-sitter. * - * Provides fast, incremental, error-tolerant parsing for syntax-level intelligence. - * This parser works even on incomplete or syntactically invalid code, making it ideal - * for real-time editor features like completion and symbol outline. + * Provides fast, incremental, error-tolerant parsing for syntax-level intelligence. This parser + * works even on incomplete or syntactically invalid code, making it ideal for real-time editor + * features like completion and symbol outline. * - * Note: This requires the compiled tree-sitter-xtc grammar library to be available - * as a native library. The grammar is generated from XtcLanguage.kt by TreeSitterGenerator. + * Note: This requires the compiled tree-sitter-xtc grammar library to be available as a native + * library. The grammar is generated from XtcLanguage.kt by TreeSitterGenerator. */ -class XtcParser private constructor( - private val language: Language, - logInit: Boolean, -) : Closeable { - private val parser: Parser = Parser() - - @Volatile - private var closed = false - - init { - parser.setLanguage(language) - if (logInit) { - logger.info("initialized with tree-sitter native library") - } - } - - /** - * Create a parser that loads the native XTC grammar library. - * This is the primary constructor for the adapter's parser. - */ - constructor() : this(loadXtcLanguage(), logInit = true) - - /** - * Create a parser using a pre-loaded language. - * Used by [org.xvm.lsp.index.WorkspaceIndexer] to create a dedicated parser - * instance without reloading the native library. - */ - constructor(language: Language) : this(language, logInit = false) - - /** - * Perform a health check to verify the parser and native library work correctly. - * Parses a minimal XTC snippet and verifies the tree structure is correct. - */ - fun healthCheck(): Boolean = - runCatching { - parse("module test { }").use { tree -> - val root = tree.root - (root.type == "source_file" && root.childCount > 0 && !root.hasError).also { valid -> - if (valid) { - logger.info("health check PASSED: parsed test module successfully") - } else { - logger.warn( - "health check FAILED: root={}, children={}, hasError={}", - root.type, - root.childCount, - root.hasError, - ) - } - } +class XtcParser + private constructor( + private val language: Language, + logInit: Boolean, + ) : Closeable { + private val parser: Parser = Parser() + + // All native parser access and disposal share this instance's monitor. + private var closed = false + + init { + parser.setLanguage(language) + if (logInit) { + logger.info("initialized with tree-sitter native library") } - }.onFailure { logger.error("health check FAILED: {}", it.message) } - .getOrDefault(false) - - /** - * Parse source code into a syntax tree. - * - * @param source the XTC source code to parse - * @return the parsed syntax tree - * @throws IllegalStateException if the parser has been closed - */ - fun parse(source: String): XtcTree = parse(source, null) - - /** - * Parse source code into a syntax tree. - * - * Always performs a full reparse. Tree-sitter's incremental parsing (`parser.parse(source, oldTree)`) - * requires the caller to call `Tree.edit()` on the old tree first, describing exactly which byte - * ranges changed. Without `Tree.edit()`, incremental parsing produces nodes with **stale byte - * offsets** from the old tree, causing `StringIndexOutOfBoundsException` when accessing `node.text` - * after document edits (e.g., rename "console" to "apa" shortens the document, but old byte offsets - * still reference the longer source). - * - * Since the LSP protocol sends full document content on each change (not diffs), we don't have - * the edit information needed for `Tree.edit()`. Full reparse is still very fast (sub-millisecond - * for typical XTC files) so the performance impact is negligible. - * - * @param source the XTC source code to parse - * @param oldTree ignored (retained for API compatibility; see doc above) - * @return the parsed syntax tree - * @throws IllegalStateException if the parser has been closed - */ - @Suppress("UNUSED_PARAMETER") - fun parse( - source: String, - oldTree: XtcTree?, - ): XtcTree { - check(!closed) { "Parser has been closed" } - logger.info("parse: {} bytes", source.length) - val tree = - parser - .parse(source) - .orElseThrow { IllegalStateException("Failed to parse source") } - return XtcTree(tree, source) - } - - /** - * Get the language used by this parser. - */ - fun getLanguage(): Language = language - - override fun close() { - if (!closed) { - closed = true - parser.close() - logger.info("closed") } - } - companion object { - private val logger = LoggerFactory.getLogger(XtcParser::class.java) - private const val GRAMMAR_LIBRARY_NAME = "tree-sitter-xtc" - private const val LANGUAGE_FUNCTION = "tree_sitter_xtc" + /** + * Create a parser that loads the native XTC grammar library. This is the primary constructor + * for the adapter's parser. + */ + constructor() : this(loadXtcLanguage(), logInit = true) - // Arena for native library - must remain open while language is in use - private val arena: Arena = Arena.global() + /** + * Create a parser using a preloaded language. Used by [org.xvm.lsp.index.WorkspaceIndexer] to + * create a dedicated parser instance without reloading the native library. + */ + constructor(language: Language) : this(language, logInit = false) /** - * Load the XTC tree-sitter language from native library. - * The library is bundled at /native//. + * Perform a health check to verify the parser and native library work correctly. Parses a + * minimal XTC snippet and verifies the tree structure is correct. */ - private fun loadXtcLanguage(): Language { - val resourcePath = Platform.resourcePath(GRAMMAR_LIBRARY_NAME) - val libraryFileName = Platform.libraryFileName(GRAMMAR_LIBRARY_NAME) - - logger.info("loadXtcLanguage: loading XTC grammar from: {}", resourcePath) - - // Try to load from resources (bundled in JAR) - XtcParser::class.java.getResourceAsStream(resourcePath)?.use { inputStream -> - val tempFile = Files.createTempFile(GRAMMAR_LIBRARY_NAME, Platform.libExtension) - tempFile.toFile().deleteOnExit() - Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING) - - logger.info("loadXtcLanguage: extracted {} to {}", libraryFileName, tempFile) - val language = loadLanguageFromPath(tempFile) - logger.info("loadXtcLanguage: successfully loaded XTC tree-sitter grammar (FFM API)") - return language - } + fun healthCheck(): Boolean = + runCatching { + parse("module test { }").use { tree -> + val root = tree.root + (root.type == "source_file" && root.childCount > 0 && !root.hasError).also { valid -> + if (valid) { + logger.info("health check PASSED: parsed test module successfully") + } else { + logger.warn( + "health check FAILED: root={}, children={}, hasError={}", + root.type, + root.childCount, + root.hasError, + ) + } + } + } + }.onFailure { logger.error("health check FAILED: {}", it.message) } + .getOrDefault(false) - // Fallback: try to load from system library path - logger.info("loadXtcLanguage: resource not found, trying system library path") - try { - return loadLanguageFromSystemPath() - } catch (e: Exception) { - logger.error( - "loadXtcLanguage: failed to load XTC tree-sitter grammar. " + - "The native library is not available. " + - "Run './gradlew :lang:tree-sitter:ensureNativeLibraryUpToDate' to compile it.", - ) - throw IllegalStateException( - "XTC tree-sitter grammar not available at $resourcePath. Build it with: " + - "./gradlew :lang:tree-sitter:ensureNativeLibraryUpToDate", - e, - ) - } - } + /** + * Parse source code into a syntax tree. + * + * @param source the XTC source code to parse + * @return the parsed syntax tree + * @throws IllegalStateException if the parser has been closed + */ + fun parse(source: String): XtcTree = parse(source, null) /** - * Load the XTC language from a native library at the given path. + * Parse source code into a syntax tree. + * + * Always performs a full reparse. Tree-sitter's incremental parsing (`parser.parse(source, + * oldTree)`) requires the caller to call `Tree.edit()` on the old tree first, describing + * exactly which byte ranges changed. Without `Tree.edit()`, incremental parsing produces nodes + * with **stale byte offsets** from the old tree, causing `StringIndexOutOfBoundsException` when + * accessing `node.text` after document edits (e.g., rename "console" to "apa" shortens the + * document, but old byte offsets still reference the longer source). * - * Uses Java's Foreign Function & Memory API to load the library and - * look up the tree_sitter_xtc language function symbol. + * Since the LSP protocol sends full document content on each change (not diffs), we don't have + * the edit information needed for `Tree.edit()`. Full reparse is still very fast + * (sub-millisecond for typical XTC files) so the performance impact is negligible. + * + * @param source the XTC source code to parse + * @param oldTree ignored (retained for API compatibility; see doc above) + * @return the parsed syntax tree + * @throws IllegalStateException if the parser has been closed */ - private fun loadLanguageFromPath(path: Path): Language { - logger.info("loadLanguageFromPath: {}", path) + @Suppress("UNUSED_PARAMETER") + @Synchronized + fun parse( + source: String, + oldTree: XtcTree?, + ): XtcTree { + check(!closed) { "Parser has been closed" } + logger.info("parse: {} bytes", source.length) + val tree = + parser.parse(source).orElseThrow { IllegalStateException("Failed to parse source") } + return XtcTree(tree, source) + } - // Create a symbol lookup for the library - val symbols = SymbolLookup.libraryLookup(path, arena) + /** Get the language used by this parser. */ + fun getLanguage(): Language = language - // Load the language using jtreesitter's Language.load() API - return Language.load(symbols, LANGUAGE_FUNCTION) + @Synchronized + override fun close() { + if (!closed) { + closed = true + parser.close() + logger.info("closed") + } } - /** - * Load the XTC language from the system library path. - * - * This assumes the library is installed in a standard system location - * or in java.library.path. - */ - private fun loadLanguageFromSystemPath(): Language { - logger.info("loadLanguageFromSystemPath: loading from system path") + companion object { + private val logger = LoggerFactory.getLogger(XtcParser::class.java) + private const val GRAMMAR_LIBRARY_NAME = "tree-sitter-xtc" + private const val LANGUAGE_FUNCTION = "tree_sitter_xtc" + + // Arena for native library - must remain open while language is in use + private val arena: Arena = Arena.global() + + /** + * Load the XTC tree-sitter language from native library. The library is bundled at + * /native//. + */ + private fun loadXtcLanguage(): Language { + val resourcePath = Platform.resourcePath(GRAMMAR_LIBRARY_NAME) + val libraryFileName = Platform.libraryFileName(GRAMMAR_LIBRARY_NAME) + + logger.info("loadXtcLanguage: loading XTC grammar from: {}", resourcePath) + + // Try to load from resources (bundled in JAR) + XtcParser::class.java.getResourceAsStream(resourcePath)?.use { inputStream -> + val tempFile = Files.createTempFile(GRAMMAR_LIBRARY_NAME, Platform.libExtension) + tempFile.toFile().deleteOnExit() + Files.copy(inputStream, tempFile, StandardCopyOption.REPLACE_EXISTING) + + logger.info("loadXtcLanguage: extracted {} to {}", libraryFileName, tempFile) + val language = loadLanguageFromPath(tempFile) + logger.info( + "loadXtcLanguage: successfully loaded XTC tree-sitter grammar (FFM API)", + ) + return language + } - // Map to platform-specific library name - val libraryName = System.mapLibraryName(GRAMMAR_LIBRARY_NAME) + // Fallback: try to load from system library path + logger.info("loadXtcLanguage: resource not found, trying system library path") + try { + return loadLanguageFromSystemPath() + } catch (e: Exception) { + logger.error( + "loadXtcLanguage: failed to load XTC tree-sitter grammar. " + + "The native library is not available. " + + "Run './gradlew :lang:tree-sitter:ensureNativeLibraryUpToDate' to compile it.", + ) + throw IllegalStateException( + "XTC tree-sitter grammar not available at $resourcePath. Build it with: " + + "./gradlew :lang:tree-sitter:ensureNativeLibraryUpToDate", + e, + ) + } + } + + /** + * Load the XTC language from a native library at the given path. + * + * Uses Java's Foreign Function & Memory API to load the library and look up the + * tree_sitter_xtc language function symbol. + */ + private fun loadLanguageFromPath(path: Path): Language { + logger.info("loadLanguageFromPath: {}", path) - // Try to load from system library path - val symbols = SymbolLookup.libraryLookup(libraryName, arena) + // Create a symbol lookup for the library + val symbols = SymbolLookup.libraryLookup(path, arena) + + // Load the language using jtreesitter's Language.load() API + return Language.load(symbols, LANGUAGE_FUNCTION) + } - return Language.load(symbols, LANGUAGE_FUNCTION) + /** + * Load the XTC language from the system library path. + * + * This assumes the library is installed in a standard system location or in + * java.library.path. + */ + private fun loadLanguageFromSystemPath(): Language { + logger.info("loadLanguageFromSystemPath: loading from system path") + + // Map to platform-specific library name + val libraryName = System.mapLibraryName(GRAMMAR_LIBRARY_NAME) + + // Try to load from system library path + val symbols = SymbolLookup.libraryLookup(libraryName, arena) + + return Language.load(symbols, LANGUAGE_FUNCTION) + } } } -} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueries.kt index 3d1dfc353af..d606e650ff5 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueries.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueries.kt @@ -3,18 +3,15 @@ package org.xvm.lsp.treesitter /** * Tree-sitter query patterns for Ecstasy language constructs. * - * These S-expression queries match specific patterns in the syntax tree, - * enabling extraction of declarations, references, and other language elements. + * These S-expression queries match specific patterns in the syntax tree, enabling extraction of + * declarations, references, and other language elements. * - * The XTC grammar defines field names (via field() in grammar.js), enabling - * field-based query syntax like `(class_declaration name: (type_name) @name)`. - * This is more robust than positional matching and aligns with tree-sitter best practices. + * The XTC grammar defines field names (via field() in grammar.js), enabling field-based query + * syntax like `(class_declaration name: (type_name) @name)`. This is more robust than positional + * matching and aligns with tree-sitter best practices. */ internal object XtcQueries { - /** - * Find all method declarations. - * Uses field-based matching on name and parameters fields. - */ + /** Find all method declarations. Uses field-based matching on name and parameters fields. */ val methodDeclarations = """ (method_declaration @@ -23,17 +20,13 @@ internal object XtcQueries { ) @declaration """.trimIndent() - /** - * Find all identifiers (for reference finding). - */ + /** Find all identifiers (for reference finding). */ val identifiers = """ (identifier) @id """.trimIndent() - /** - * Find import statements. - */ + /** Find import statements. */ val imports = """ (import_statement @@ -42,8 +35,8 @@ internal object XtcQueries { """.trimIndent() /** - * Find comment and string-literal nodes -- the host nodes for free-text content - * (URLs, file paths, etc.) that may need document links. + * Find comment and string-literal nodes -- the host nodes for free-text content (URLs, file + * paths, etc.) that may need document links. */ val commentsAndStrings = """ @@ -55,16 +48,15 @@ internal object XtcQueries { """.trimIndent() /** - * Combined query for all declarations (for document symbols). - * Uses field-based matching for robust, position-independent queries. + * Combined query for all declarations (for document symbols). Uses field-based matching for + * robust, position-independent queries. * - * Typedefs are declarations too (issue #459: cmd-click on `JsonArray` found - * nothing because typedefs were absent here and thus from the workspace index). + * Typedefs are declarations too (issue #459: cmd-click on `JsonArray` found nothing because + * typedefs were absent here and thus from the workspace index). * - * Shorthand constructor parameters of class-like declarations (`const - * Point(Int x, Int y)`) declare properties in Ecstasy, so each parameter - * name is captured as a @property. Issue #459: go-to-declaration on - * `structure.y` jumped to an unrelated `y` in doc/archive because the + * Shorthand constructor parameters of class-like declarations (`const Point(Int x, Int y)`) + * declare properties in Ecstasy, so each parameter name is captured as a @property. Issue #459: + * go-to-declaration on `structure.y` jumped to an unrelated `y` in doc/archive because the * shorthand-declared `y` was invisible to the same-file lookup. */ val allDeclarations = diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt index 8dc58eb3294..246247523e4 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt @@ -12,8 +12,8 @@ import java.io.Closeable /** * Query engine for extracting information from XTC syntax trees. * - * Uses Tree-sitter queries to find declarations, references, and other - * language constructs in parsed source code. + * Uses Tree-sitter queries to find declarations, references, and other language constructs in + * parsed source code. */ @Suppress("LoggingSimilarMessage") class XtcQueryEngine( @@ -27,9 +27,7 @@ class XtcQueryEngine( private val importsQuery: Query = Query(language, XtcQueries.imports) private val commentsAndStringsQuery: Query = Query(language, XtcQueries.commentsAndStrings) - /** - * Find all declarations in the tree for document symbols. - */ + /** Find all declarations in the tree for document symbols. */ fun findAllDeclarations( tree: XtcTree, uri: String, @@ -63,9 +61,7 @@ class XtcQueryEngine( } } - /** - * Find all method declarations. - */ + /** Find all method declarations. */ fun findMethodDeclarations( tree: XtcTree, uri: String, @@ -92,9 +88,7 @@ class XtcQueryEngine( } } - /** - * Find all identifiers with a given name (for find references). - */ + /** Find all identifiers with a given name (for find references). */ fun findAllIdentifiers( tree: XtcTree, name: String, @@ -112,7 +106,12 @@ class XtcQueryEngine( logger.info("findAllIdentifiers '{}' -> {} match(es)", name, matches.size) if (matches.isNotEmpty()) { matches.forEach { loc -> - logger.info(" {}:{}:{}", loc.uri.substringAfterLast('/'), loc.startLine + 1, loc.startColumn + 1) + logger.info( + " {}:{}:{}", + loc.uri.substringAfterLast('/'), + loc.startLine + 1, + loc.startColumn + 1, + ) } } } @@ -130,22 +129,22 @@ class XtcQueryEngine( /** * Resolve a name reference at the given cursor position to the nearest in-scope declaration. * - * Walks the AST upward from the cursor and, for each enclosing scope, asks whether that - * scope declares anything matching the requested name: - * - A `block` (function body, control-flow body) contributes local variables declared - * *before* the cursor position. Forward references are not allowed for locals. - * - A `method_declaration` / `function_declaration` contributes its parameters. - * - A `class_body` / `module_body` / `package_body` / `interface_body` / `enum_body` / - * `mixin_body` / `service_body` / `const_body` contributes its declared members - * (properties, methods, getters, nested types). Class/module members are visible - * throughout the body, so no before-cursor restriction applies. + * Walks the AST upward from the cursor and, for each enclosing scope, asks whether that scope + * declares anything matching the requested name: + * - A `block` (function body, control-flow body) contributes local variables declared *before* + * the cursor position. Forward references are not allowed for locals. + * - A `method_declaration` / `function_declaration` contributes its parameters. + * - A `class_body` / `module_body` / `package_body` / `interface_body` / `enum_body` / + * `mixin_body` / `service_body` / `const_body` contributes its declared members (properties, + * methods, getters, nested types). Class/module members are visible throughout the body, so + * no before-cursor restriction applies. * - * The first matching declaration in the enclosing chain wins, modelling shadowing. The - * returned [Location] points at the declaration's `name` field (the identifier), so - * cmd-click lands on the name itself rather than the head of the statement. + * The first matching declaration in the enclosing chain wins, modeling shadowing. The returned + * [Location] points at the declaration's `name` field (the identifier), so cmd-click lands on + * the name itself rather than the head of the statement. * - * Returns `null` if no enclosing scope declares the name. Callers should then consult - * the workspace symbol index for a cross-file fallback. + * Returns `null` if no enclosing scope declares the name. Callers should then consult the + * workspace symbol index for a cross-file fallback. */ fun resolveByNameInScope( tree: XtcTree, @@ -163,18 +162,31 @@ class XtcQueryEngine( findLocalVariableInBlock(current, name, line, column) } - "method_declaration", "function_declaration", "constructor_declaration" -> { + "method_declaration", + "function_declaration", + "constructor_declaration", + -> { findParameterInMethod(current, name) } - "class_body", "module_body", "package_body", "interface_body", - "enum_body", "mixin_body", "service_body", "const_body", + "class_body", + "module_body", + "package_body", + "interface_body", + "enum_body", + "mixin_body", + "service_body", + "const_body", -> { findMemberInBody(current, name) } - "class_declaration", "const_declaration", "service_declaration", - "mixin_declaration", "enum_declaration", "annotation_declaration", + "class_declaration", + "const_declaration", + "service_declaration", + "mixin_declaration", + "enum_declaration", + "annotation_declaration", -> { // Shorthand constructor parameters (`const Point(Int x, Int y)`) // declare properties, visible throughout the class body. @@ -204,12 +216,12 @@ class XtcQueryEngine( /** * Enumerate all in-scope declarations visible at the given cursor position. * - * Same scope walk as [resolveByNameInScope], but returns the full set of visible - * declarations rather than the first matching one. Used by the completion provider - * to surface locals, parameters, and enclosing-scope members in the BODY context. + * Same scope walk as [resolveByNameInScope], but returns the full set of visible declarations + * rather than the first matching one. Used by the completion provider to surface locals, + * parameters, and enclosing-scope members in the BODY context. * - * Entries from inner scopes are listed first; if an outer scope declares a name - * already declared in an inner scope, the outer one is omitted (shadowing). + * Entries from inner scopes are listed first; if an outer scope declares a name already + * declared in an inner scope, the outer one is omitted (shadowing). */ fun enumerateInScope( tree: XtcTree, @@ -228,18 +240,31 @@ class XtcQueryEngine( enumerateLocalsInBlock(current, line, column, uri) } - "method_declaration", "function_declaration", "constructor_declaration" -> { + "method_declaration", + "function_declaration", + "constructor_declaration", + -> { enumerateParameters(current, uri) } - "class_body", "module_body", "package_body", "interface_body", - "enum_body", "mixin_body", "service_body", "const_body", + "class_body", + "module_body", + "package_body", + "interface_body", + "enum_body", + "mixin_body", + "service_body", + "const_body", -> { enumerateMembers(current, uri) } - "class_declaration", "const_declaration", "service_declaration", - "mixin_declaration", "enum_declaration", "annotation_declaration", + "class_declaration", + "const_declaration", + "service_declaration", + "mixin_declaration", + "enum_declaration", + "annotation_declaration", -> { enumerateShorthandProperties(current, uri) } @@ -259,9 +284,9 @@ class XtcQueryEngine( } /** - * Search a function/control-flow `block` for a `variable_declaration` whose name field - * matches and whose declaration position precedes the cursor. Forward references aren't - * legal Ecstasy, so a declaration at or after the cursor is ignored. + * Search a function/control-flow `block` for a `variable_declaration` whose name field matches + * and whose declaration position precedes the cursor. Forward references aren't legal Ecstasy, + * so a declaration at or after the cursor is ignored. */ private fun findLocalVariableInBlock( block: XtcNode, @@ -277,8 +302,8 @@ class XtcQueryEngine( .firstOrNull { it.text == name } /** - * Enumerate every local variable declared in the given block before the cursor position. - * The returned [SymbolInfo.location] points at the name identifier. + * Enumerate every local variable declared in the given block before the cursor position. The + * returned [SymbolInfo.location] points at the name identifier. */ private fun enumerateLocalsInBlock( block: XtcNode, @@ -315,9 +340,7 @@ class XtcQueryEngine( .firstOrNull { it.text == name } } - /** - * Enumerate every named parameter of a method/function/constructor declaration. - */ + /** Enumerate every named parameter of a method/function/constructor declaration. */ private fun enumerateParameters( method: XtcNode, uri: String, @@ -349,8 +372,8 @@ class XtcQueryEngine( ?.firstOrNull { it.text == name } /** - * Enumerate every shorthand constructor parameter of a class-like declaration as a - * PROPERTY symbol. + * Enumerate every shorthand constructor parameter of a class-like declaration as a PROPERTY + * symbol. */ private fun enumerateShorthandProperties( declaration: XtcNode, @@ -368,9 +391,9 @@ class XtcQueryEngine( .orEmpty() /** - * Search a class/module/package/interface/enum/mixin/service/const body for a declared - * member (property, method, getter, nested type) whose name matches. Members are visible - * throughout the body, so position is not constrained. + * Search a class/module/package/interface/enum/mixin/service/const body for a declared member + * (property, method, getter, nested type) whose name matches. Members are visible throughout + * the body, so position is not constrained. */ private fun findMemberInBody( body: XtcNode, @@ -382,9 +405,7 @@ class XtcQueryEngine( .mapNotNull { it.childByFieldName("name") } .firstOrNull { it.text == name } - /** - * Enumerate every declared member (property, method, getter, nested type) in a body. - */ + /** Enumerate every declared member (property, method, getter, nested type) in a body. */ private fun enumerateMembers( body: XtcNode, uri: String, @@ -398,7 +419,7 @@ class XtcQueryEngine( }.toList() private companion object { - // Used by findScopeMembers to recognise declarations inside a body. + // Used by findScopeMembers to recognize declarations inside a body. private val memberNodeKinds = mapOf( "property_declaration" to SymbolKind.PROPERTY, @@ -452,9 +473,7 @@ class XtcQueryEngine( ) } - /** - * Find the declaration containing a given position. - */ + /** Find the declaration containing a given position. */ fun findDeclarationAt( tree: XtcTree, line: Int, @@ -501,9 +520,7 @@ class XtcQueryEngine( typeSignature = typeSignature, ) - /** - * Find imports in the tree (text only). - */ + /** Find imports in the tree (text only). */ fun findImports(tree: XtcTree): List { logger.info("findImports") return buildList { @@ -519,9 +536,7 @@ class XtcQueryEngine( } } - /** - * Find imports in the tree with their source locations. - */ + /** Find imports in the tree with their source locations. */ fun findImportLocations( tree: XtcTree, uri: String, @@ -551,9 +566,9 @@ class XtcQueryEngine( /** * Find all comment and string-literal nodes, returned as `(text, location)` pairs. * - * These are the host nodes that may contain URLs, file paths, or other free-text - * content the editor can hyperlink. Caller is responsible for scanning the text - * and computing per-match ranges relative to the host node's start position. + * These are the host nodes that may contain URLs, file paths, or other free-text content the + * editor can hyperlink. Caller is responsible for scanning the text and computing per-match + * ranges relative to the host node's start position. */ fun findCommentAndStringNodes( tree: XtcTree, @@ -586,7 +601,12 @@ class XtcQueryEngine( handler(captures) } } - logger.info("executeQuery '{}': {} pattern(s), {} match(es)", queryName, query.patternCount, matchCount) + logger.info( + "executeQuery '{}': {} pattern(s), {} match(es)", + queryName, + query.patternCount, + matchCount, + ) } override fun close() { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcTree.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcTree.kt index a13d8dce23d..c82cb2eadfd 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcTree.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcTree.kt @@ -8,84 +8,80 @@ import java.io.Closeable /** * Wrapper around a Tree-sitter parsed tree for XTC sources. * - * The tree provides access to the syntax structure of the source code. - * It can be used for incremental reparsing when the source changes. + * The tree provides access to the syntax structure of the source code. It can be used for + * incremental reparsing when the source changes. */ -class XtcTree internal constructor( - internal val tsTree: Tree, - override val source: String, -) : AdapterTree, - Closeable { - @Volatile - private var closed = false +class XtcTree + internal constructor( + internal val tsTree: Tree, + override val source: String, + ) : AdapterTree, + Closeable { + @Volatile private var closed = false - /** - * Get the root node of the syntax tree. - */ - override val root: XtcNode - get() { - checkNotClosed() - return XtcNode(tsTree.rootNode, source) - } + /** Get the root node of the syntax tree. */ + override val root: XtcNode + get() { + checkNotClosed() + return XtcNode(tsTree.rootNode, source) + } - /** - * Check if this tree has any syntax errors. - */ - override val hasErrors: Boolean - get() { + /** Check if this tree has any syntax errors. */ + override val hasErrors: Boolean + get() { + checkNotClosed() + return root.hasError + } + + /** + * Find the smallest node at the given position. + * + * @param line 0-based line number + * @param column 0-based column number + * @return the node at that position, or null if none + */ + override fun nodeAt( + line: Int, + column: Int, + ): XtcNode? { checkNotClosed() - return root.hasError + val point = Point(line, column) + return tsTree.rootNode + .getDescendant(point, point) + .orElse(null) + ?.let { XtcNode(it, source) } } - /** - * Find the smallest node at the given position. - * - * @param line 0-based line number - * @param column 0-based column number - * @return the node at that position, or null if none - */ - override fun nodeAt( - line: Int, - column: Int, - ): XtcNode? { - checkNotClosed() - val point = Point(line, column) - return tsTree.rootNode - .getDescendant(point, point) - .orElse(null) - ?.let { XtcNode(it, source) } - } - - /** - * Find the deepest named node at the given position. - * - * Named nodes are nodes that appear in the grammar (e.g., "class_declaration"), - * as opposed to anonymous nodes (e.g., literal punctuation like "{"). - * - * @param line 0-based line number - * @param column 0-based column number - * @return the named node at that position, or null if none - */ - @Suppress("unused") - fun namedNodeAt( - line: Int, - column: Int, - ): XtcNode? { - var node = nodeAt(line, column) - while (node != null && !node.isNamed) { - node = node.parent + /** + * Find the deepest named node at the given position. + * + * Named nodes are nodes that appear in the grammar (e.g., "class_declaration"), as opposed to + * anonymous nodes (e.g., literal punctuation like "{"). + * + * @param line 0-based line number + * @param column 0-based column number + * @return the named node at that position, or null if none + */ + @Suppress("unused") + fun namedNodeAt( + line: Int, + column: Int, + ): XtcNode? { + var node = nodeAt(line, column) + while (node != null && !node.isNamed) { + node = node.parent + } + return node } - return node - } - override fun close() { - if (!closed) { - closed = true - tsTree.close() + override fun close() { + if (!closed) { + closed = true + tsTree.close() + } } - } - private fun checkNotClosed() { - check(!closed) { "Tree has been closed" } + private fun checkNotClosed() { + check(!closed) { "Tree has been closed" } + } } -} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt new file mode 100644 index 00000000000..afac344518e --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt @@ -0,0 +1,121 @@ +package org.xvm.lsp.util + +import com.google.gson.Gson +import org.slf4j.LoggerFactory +import java.time.Instant +import java.util.concurrent.CancellationException +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong + +/** Timing metadata only: never logs source buffers, protocol payloads or compiler objects. */ +internal object ExecutionTrace { + data class Span( + val id: Long, + val parent: Long?, + val kind: String, + val operation: String, + val uri: String?, + val created: Long = System.nanoTime(), + ) + + private data class Context( + val span: Span, + val apiDepth: Int, + ) + + private val logger = LoggerFactory.getLogger("org.xvm.lsp.trace") + private val gson = Gson() + private val ids = AtomicLong() + private val events = AtomicLong() + private val context = ThreadLocal() + private val apiThreads = AtomicInteger() + private val pid = ProcessHandle.current().pid() + + fun span( + kind: String, + operation: String, + uri: String? = null, + ): Span = Span(ids.incrementAndGet(), context.get()?.span?.id, kind, operation, uri) + + fun event( + span: Span, + event: String, + fields: Map = emptyMap(), + ) { + if (!logger.isInfoEnabled) return + val thread = Thread.currentThread() + logger.info( + "{}", + gson.toJson( + mapOf( + "sequence" to events.incrementAndGet(), + "time" to Instant.now().toString(), + "pid" to pid, + "thread" to thread.name.ifEmpty { "${if (thread.isVirtual) "virtual" else "platform"}-${thread.threadId()}" }, + "threadId" to thread.threadId(), + "id" to span.id, + "parent" to span.parent, + "kind" to span.kind, + "operation" to span.operation, + "uri" to span.uri, + "event" to event, + "elapsedMs" to elapsed(span.created), + ) + fields, + ), + ) + } + + fun elapsed(start: Long): Double = (System.nanoTime() - start) / 1_000_000.0 + + fun current(): Span? = context.get()?.span + + fun within( + span: Span?, + action: () -> T, + ): T { + if (span == null) return action() + val previous = context.get() + context.set(Context(span, previous?.apiDepth ?: 0)) + return try { + action() + } finally { + if (previous == null) context.remove() else context.set(previous) + } + } + + /** Counts threads inside compiler APIs, rather than counting nested calls as parallelism. */ + fun api( + operation: String, + uri: String? = null, + action: () -> T, + ): T { + val span = span("javatools", operation, uri) + val previous = context.get() + val depth = previous?.apiDepth ?: 0 + context.set(Context(span, depth + 1)) + val active = if (depth == 0) apiThreads.incrementAndGet() else apiThreads.get() + event(span, "start", mapOf("activeApiThreads" to active, "depth" to depth)) + return try { + action().also { + event( + span, + "end", + mapOf("outcome" to "returned", "activeApiThreads" to apiThreads.get()), + ) + } + } catch (failure: Throwable) { + event( + span, + "end", + mapOf( + "outcome" to if (failure is CancellationException) "cancelled" else "failed", + "failure" to failure.javaClass.name, + ), + ) + throw failure + } finally { + if (depth == 0) apiThreads.decrementAndGet() + if (previous == null) context.remove() else context.set(previous) + } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt new file mode 100644 index 00000000000..6430e8fdd5c --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt @@ -0,0 +1,34 @@ +package org.xvm.lsp.util + +import java.net.URI + +/** Short source labels for progress UI; no filesystem access or source contents. */ +internal object ProgressLabels { + fun source( + uri: String, + roots: List = emptyList(), + ): String { + fun path(value: String) = runCatching { URI(value).path?.trimEnd('/') }.getOrNull() + val source = path(uri) + val root = roots.mapNotNull(::path).filter { source?.startsWith("$it/") == true }.maxByOrNull { it.length } + val label = + when { + source.isNullOrEmpty() -> { + uri.substringBefore('?').substringBefore('#') + } + + root != null -> { + source.removePrefix("$root/") + } + + else -> { + source + .split('/') + .filter(String::isNotEmpty) + .takeLast(2) + .joinToString("/") + } + } + return label.replace(Regex("[\\p{Cntrl}]"), " ").take(160).ifEmpty { "workspace" } + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt new file mode 100644 index 00000000000..25eb3dcd9af --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt @@ -0,0 +1,221 @@ +package org.xvm.lsp.util + +import ch.qos.logback.core.PropertyDefinerBase +import com.google.gson.GsonBuilder +import java.io.ByteArrayOutputStream +import java.nio.ByteBuffer +import java.nio.channels.FileChannel +import java.nio.channels.OverlappingFileLockException +import java.nio.file.Files +import java.nio.file.LinkOption.NOFOLLOW_LINKS +import java.nio.file.Path +import java.nio.file.StandardOpenOption.CREATE +import java.nio.file.StandardOpenOption.READ +import java.nio.file.StandardOpenOption.WRITE +import java.time.Instant +import java.util.Base64 +import java.util.zip.ZipEntry +import java.util.zip.ZipOutputStream +import kotlin.jvm.optionals.getOrNull + +internal data class LogRetention( + val historyDays: Int = 7, + val maxFileMb: Int = 10, + val totalSizeMb: Int = 50, + val retainedSessions: Int = 5, +) { + init { + require(historyDays in 1..90 && maxFileMb in 1..100 && totalSizeMb in maxFileMb..1000 && retainedSessions in 1..20) { + "Invalid server log retention limits" + } + } + + companion object { + fun read(): LogRetention { + fun value( + name: String, + fallback: Int, + ) = System.getProperty("xtc.logs.$name")?.toIntOrNull() ?: fallback + return LogRetention(value("historyDays", 7), value("maxFileMb", 10), value("totalSizeMb", 50), value("retainedSessions", 5)) + } + } +} + +/** Owned session files only. Never traverses source roots, other applications' logs, or symlinks. */ +internal object ServerLogs { + val process = TraceProcessId().propertyValue + val root: Path = + Path.of( + System.getProperty( + "xtc.logs.directory", + ) ?: System.getenv("XTC_LSP_LOG_DIR") ?: "${System.getProperty("user.home")}/.xtc/logs/lsp", + ) + val directory: Path = root.resolve("server-$process") + val traceDirectory: Path = + Path.of(System.getProperty("xtc.trace.directory") ?: System.getenv("XTC_LSP_TRACE_DIR") ?: directory.toString()) + val retention = LogRetention.read() + private val sessionName = Regex("server-([0-9]+)-([0-9]+)") + private val logName = Regex("(?:server(?:\\.[0-9.-]+)?\\.log|lsp-trace-[0-9.-]+\\.jsonl)") + private const val FILE_LIMIT = 512 * 1024 + private const val EXPORT_LIMIT = 4 * 1024 * 1024 + + fun status(): Map = + mapOf( + "directory" to directory.toString(), + "traceDirectory" to traceDirectory.toString(), + "retention" to retention, + "exportLimitBytes" to EXPORT_LIMIT, + "exportFileLimitBytes" to FILE_LIMIT, + ) + + /** Active sessions are protected, including a PID with an unknown start time. */ + fun prune( + root: Path = this.root, + policy: LogRetention = retention, + now: Instant = Instant.now(), + active: (Long, Long) -> Boolean = ::isActive, + ): Int { + if (!Files.isDirectory(root, NOFOLLOW_LINKS)) return 0 + return FileChannel.open(root.resolve(".retention.lock"), CREATE, WRITE, NOFOLLOW_LINKS).use { channel -> + val lock = + try { + channel.tryLock() + } catch (_: OverlappingFileLockException) { + null + } + lock?.use { pruneClosed(root, policy, now, active) } ?: 0 + } + } + + private fun pruneClosed( + root: Path, + policy: LogRetention, + now: Instant, + active: (Long, Long) -> Boolean, + ): Int { + val candidates = + Files + .list(root) + .use { paths -> paths.filter { Files.isDirectory(it, NOFOLLOW_LINKS) }.toList() } + .mapNotNull { path -> sessionName.matchEntire(path.fileName.toString())?.let { path to it } } + .filter { (_, name) -> + val pid = name.groupValues[1].toLongOrNull() + val started = name.groupValues[2].toLongOrNull() + pid != null && pid > 0 && started != null && !active(pid, started) + }.map { (path, _) -> path to Files.list(path).use { files -> files.toList() } } + .filter { (_, files) -> files.all { Files.isRegularFile(it, NOFOLLOW_LINKS) && logName.matches(it.fileName.toString()) } } + .map { (path, files) -> + Triple( + path, + files, + ( + files.map { Files.getLastModifiedTime(it).toInstant() } + + Files.getLastModifiedTime(path).toInstant() + ).max(), + ) + }.sortedByDescending { it.third } + return candidates + .filterIndexed { index, candidate -> + index >= policy.retainedSessions || + candidate.third.isBefore(now.minusSeconds(policy.historyDays * 86400L)) + }.count { (path, files, _) -> + files.forEach(Files::deleteIfExists) + Files.deleteIfExists(path) + } + } + + private fun isActive( + pid: Long, + started: Long, + ): Boolean { + val process = ProcessHandle.of(pid).getOrNull() ?: return false + if (!process.isAlive) return false + val actualStart = process.info().startInstant().getOrNull() + return actualStart == null || actualStart.toEpochMilli() == started + } + + fun export(status: Map): Map { + val files = + listOf(directory, traceDirectory) + .distinct() + .flatMap { path -> + if (Files.isDirectory(path, NOFOLLOW_LINKS)) { + Files.list(path).use { stream -> + stream + .filter { file -> + Files.isRegularFile(file, NOFOLLOW_LINKS) && + ( + if (path == + directory + ) { + logName.matches(file.fileName.toString()) + } else { + file.fileName.toString().startsWith("lsp-trace-$process") && + file.fileName.toString().endsWith(".jsonl") + } + ) + }.toList() + } + } else { + emptyList() + } + }.sortedByDescending { Files.getLastModifiedTime(it).toMillis() } + .take(EXPORT_LIMIT / FILE_LIMIT) + return archive(files, status) + } + + internal fun archive( + files: List, + status: Map, + ): Map { + val output = ByteArrayOutputStream() + ZipOutputStream(output).use { zip -> + val included = + files.take(EXPORT_LIMIT / FILE_LIMIT).mapIndexedNotNull { index, path -> + if (!Files.isRegularFile(path, NOFOLLOW_LINKS)) return@mapIndexedNotNull null + // Read at most one tail, even when a producer continues appending during export. + val entry = + Files.newByteChannel(path, setOf(READ, NOFOLLOW_LINKS)).use { channel -> + val size = channel.size() + val bytes = ByteBuffer.allocate(minOf(size, FILE_LIMIT.toLong()).toInt()) + channel.position((size - FILE_LIMIT).coerceAtLeast(0)) + while (bytes.hasRemaining() && channel.read(bytes) >= 0) { /* bounded snapshot */ } + val name = "$index-${path.fileName}" + zip.putNextEntry(ZipEntry(name)) + zip.write(bytes.array(), 0, bytes.position()) + zip.closeEntry() + mapOf( + "entry" to name, + "originalBytes" to size, + "includedBytes" to bytes.position(), + "truncated" to (size > bytes.position()), + ) + } + entry + } + zip.putNextEntry(ZipEntry("manifest.json")) + zip.write( + GsonBuilder() + .setPrettyPrinting() + .create() + .toJson( + mapOf( + "schemaVersion" to 1, + "createdAt" to Instant.now().toString(), + "service" to status, + "files" to included, + "scope" to + "Recent server logs and execution traces; IDE protocol consoles are separate. Each file is a bounded tail and may start mid-line.", + ), + ).toByteArray(), + ) + zip.closeEntry() + } + return mapOf("fileName" to "ecstasy-server-$process.zip", "base64" to Base64.getEncoder().encodeToString(output.toByteArray())) + } +} + +/** Logback evaluates this before opening either process-owned rolling appender. */ +class ServerLogDirectory : PropertyDefinerBase() { + override fun getPropertyValue(): String = ServerLogs.directory.toString() +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt new file mode 100644 index 00000000000..fdfa86b0bfe --- /dev/null +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt @@ -0,0 +1,12 @@ +package org.xvm.lsp.util + +import ch.qos.logback.core.PropertyDefinerBase +import java.time.Instant + +/** Keep concurrent IDE servers and test JVMs in separate trace files, including PID reuse. */ +class TraceProcessId : PropertyDefinerBase() { + override fun getPropertyValue(): String = + ProcessHandle.current().let { + "${it.pid()}-${it.info().startInstant().orElseGet(Instant::now).toEpochMilli()}" + } +} diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/WorkInProgress.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/WorkInProgress.kt index 8f0da2eed93..1113c674a1a 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/WorkInProgress.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/WorkInProgress.kt @@ -1,10 +1,12 @@ +@file:Suppress("unused") + package org.xvm.lsp.util /** * Marks a class, function, or property as work in progress. * - * Use this annotation to indicate that an implementation is intentionally - * incomplete or placeholder, awaiting future development. + * Use this annotation to indicate that an implementation is intentionally incomplete or + * placeholder, awaiting future development. * * @param reason Optional description of what's pending or why it's incomplete */ diff --git a/lang/lsp-server/src/main/resources/logback.xml b/lang/lsp-server/src/main/resources/logback.xml index 0b3ebbd5a10..81e76ffd667 100644 --- a/lang/lsp-server/src/main/resources/logback.xml +++ b/lang/lsp-server/src/main/resources/logback.xml @@ -6,8 +6,7 @@ is reserved for JSON-RPC protocol messages. Any output to stdout will corrupt the protocol stream. - Log file location: ~/.xtc/logs/lsp-server.log - Tail with: tail -f ~/.xtc/logs/lsp-server.log + Logs are isolated under ~/.xtc/logs/lsp/server-PID-START/. Retention is configured by the IDE. --> @@ -21,14 +20,16 @@ - + + - ${user.home}/.xtc/logs/lsp-server.log + ${LOG_DIRECTORY}/server.log - ${user.home}/.xtc/logs/lsp-server.%d{yyyy-MM-dd}.%i.log - 10MB - 7 - 50MB + ${LOG_DIRECTORY}/server.%d{yyyy-MM-dd}.%i.log + ${xtc.logs.maxFileMb:-10}MB + ${xtc.logs.historyDays:-7} + true + ${xtc.logs.totalSizeMb:-50}MB %d{HH:mm:ss.SSS} %-5level %logger{0} - %msg%n @@ -42,6 +43,24 @@ + + + + + ${TRACE_DIRECTORY}/lsp-trace-${TRACE_PROCESS}.jsonl + + ${TRACE_DIRECTORY}/lsp-trace-${TRACE_PROCESS}.%d{yyyy-MM-dd}.%i.jsonl + ${xtc.logs.maxFileMb:-10}MB + ${xtc.logs.historyDays:-7} + true + ${xtc.logs.totalSizeMb:-50}MB + + %msg%n + + + + + diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt new file mode 100644 index 00000000000..14bb4ea783f --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt @@ -0,0 +1,55 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import java.util.concurrent.CompletableFuture + +class AdapterFuturesTest { + @Test + fun `cancellation before analysis completes does not start proof`() { + val analysis = CompletableFuture() + val proof = CompletableFuture() + val result = analysis.composeCancellable { proof } + result.cancel(false) + assertThat(analysis).isCancelled() + assertThat(proof).isNotDone() + } + + @Test + fun `cancellation follows a running proof`() { + val proof = CompletableFuture() + val result = CompletableFuture.completedFuture("analysis").composeCancellable { proof } + result.cancel(false) + assertThat(proof).isCancelled() + } + + @Test + fun `cancellation during proof creation is delivered to the new future`() { + val analysis = CompletableFuture() + val proof = CompletableFuture() + val cancelled = CompletableFuture() + val result = + analysis.composeCancellable { + cancelled.complete(Unit) + proof + } + cancelled.thenRun { result.cancel(false) } + analysis.complete("analysis") + assertThat(proof).isCancelled() + } + + @Test + fun `proof values and construction failures complete the outer request`() { + val value = + CompletableFuture.completedFuture("analysis").composeCancellable { + CompletableFuture.completedFuture(42) + } + assertThat(value.join()).isEqualTo(42) + val failure = AssertionError("proof failed") + val failed = + CompletableFuture.completedFuture("analysis").composeCancellable { + throw failure + } + assertThat(failed.handle { _, error -> error }.join()).isSameAs(failure) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeActionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeActionTest.kt index 3ffd3b1a387..d8b124eb403 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeActionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeActionTest.kt @@ -24,10 +24,10 @@ class CodeActionTest : TreeSitterTestBase() { @DisplayName("getCodeActions()") inner class CodeActionTests { /** - * `buildOrganizeImportsAction` searches `tree.root.children` for `import_statement` - * nodes. In XTC, imports are often nested inside the module body, so whether - * the grammar places them at root level is grammar-dependent. We use root-level - * imports and assert the method completes without error. + * `buildOrganizeImportsAction` searches `tree.root.children` for `import_statement` nodes. + * In XTC, imports are often nested inside the module body, so whether the grammar places + * them at root level is grammar-dependent. We use root-level imports and assert the method + * completes without error. */ @Test @DisplayName("should suggest organize imports when unsorted at root level") @@ -48,8 +48,8 @@ class CodeActionTest : TreeSitterTestBase() { } /** - * Already-sorted imports (alphabetically) should never trigger the - * "Organize Imports" action, regardless of where the grammar places them. + * Already-sorted imports (alphabetically) should never trigger the "Organize Imports" + * action, regardless of where the grammar places them. */ @Test @DisplayName("should not suggest organize imports when sorted") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeLensTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeLensTest.kt index 57f502edf13..380eeb6cb10 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeLensTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CodeLensTest.kt @@ -6,9 +6,7 @@ import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance -/** - * Tests for `textDocument/codeLens` — inline Run actions on module declarations. - */ +/** Tests for `textDocument/codeLens` — inline Run actions on module declarations. */ @DisplayName("CodeLens") @TestInstance(TestInstance.Lifecycle.PER_CLASS) class CodeLensTest : TreeSitterTestBase() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt new file mode 100644 index 00000000000..9eea3e372c5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt @@ -0,0 +1,249 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.FileStructure +import org.xvm.asm.ModuleRepository +import org.xvm.asm.MultiMethodStructure +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.lsp.adapter.xdk.semanticSnapshots +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.lang.reflect.Modifier +import java.time.Instant +import java.util.Collections +import java.util.IdentityHashMap +import java.util.UUID +import java.util.concurrent.Executors + +class CompilerBoundaryRequirementsTest { + @ParameterizedTest + @ValueSource(strings = ["id", "&id", "pick", "&pick"]) + fun `bound generic functions emit readable executable artifacts`(reference: String) { + val result = + compile( + "module Boundary { static T id(T value) = value; T pick(T value) = value; function Int(Int) make() = $reference; }", + ) + val artifact = bytes(result) + val restored = FileStructure(ByteArrayInputStream(artifact)).module + assertThat(restored.name).isEqualTo("Boundary") + assertThat((restored.getChild("make") as MultiMethodStructure).methods().single().ops) + .isNotEmpty() + assertPure(result.semanticSnapshot()) + } + + @Test + fun `serialized dependency identities match source declarations without conflating overloads or modules`() { + val library = + compile( + "module Library { static Int pick(Int n) = n; static String pick(String n) = n; }", + ) + val other = compile("module Other { static Int pick(Int n) = n; }") + val repository = repository(library, other) + val source = + "module Consumer { package lib import Library; package other import Other; " + + "Int run() = lib.pick(1) + other.pick(2); String text() = lib.pick(\"x\"); }" + val consumer = compile(source, repository) + val methods = + nodes(library.parsed()).filterIsInstance().filter { + it.nameToken.valueText == "pick" + } + val calls = consumer.callBindings().values.filter { it.method().name == "pick" } + assertThat(calls).hasSize(3) + assertThat(calls.map { it.method() }.distinct()).hasSize(3) + val sourceModel = library.semanticSnapshot() + val consumerModel = consumer.semanticSnapshot() + methods.forEach { declaration -> + val call = calls.single { it.method() == declaration.component.identityConstant } + assertThat(call.method()).isNotSameAs(declaration.component.identityConstant) + val name = declaration.nameToken + val at = + SemanticModel.Position( + Source.calculateLine(name.startPosition), + Source.calculateOffset(name.startPosition), + ) + val symbol = sourceModel.symbolAt(at.line, at.column)!! + assertThat(symbol.declarationSource).isEqualTo("file:///Boundary.x") + assertThat(consumerModel.symbol(symbol.id)).isNull() + } + consumerModel.calls.forEach { call -> + assertThat(consumerModel.symbol(call.method)!!.declaration).isNull() + assertThat( + consumerModel.definitionLocationAt( + call.callee.start.line, + call.callee.end.column - 1, + ), + ).isNull() + } + } + + @Test + fun `a fresh dependency repository changes selected types and rejects removed members`() { + fun dependency(type: String) = + compile( + "module Library { static $type value() = ${if (type == "Int") "1" else "\"new\""}; }", + ) + val text = + "module Consumer { package lib import Library; void run() { var value = lib.value(); } }" + val first = compile(text, repository(dependency("Int"))).semanticSnapshot() + val second = compile(text, repository(dependency("String"))).semanticSnapshot() + + fun returned(model: SemanticModel): String = + model + .type( + model.calls + .single() + .signature.returns + .single(), + )!! + .displayName + assertThat(returned(first)).isEqualTo("Int") + assertThat(returned(second)).isEqualTo("String") + assertThat(second.symbol(first.calls.single().method)).isNull() + val errors = ErrorList() + val removed = repository(compile("module Library { static Int replacement() = 1; }")) + val failed = + EmbeddingSupport + .instance() + .compileModule(Source(text, "file:///Boundary.x"), removed, errors) + assertThat(failed.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(returned(first)).isEqualTo("Int") + } + + @Test + fun `semantic inspection preserves emitted bytes and copied snapshots contain no compiler objects`() { + val source = + "module Boundary { interface Reader { Int read(); @RO Int value { @Override Int get() = 1; } } " + + "class Value implements Reader { @Override Int read() = 1; @Override Int value = 2; } " + + "class Computed implements Reader { @Override Int read() = 2; @Override Int value.get() = 3; } " + + "class Forward(Reader target) delegates Reader(target) {} " + + "class Concrete(Value target) delegates Reader(target) {} " + + "class Outer(Concrete target) delegates Reader(target) {} " + + "annotation Tracked into Var { @Override T get() = super(); @Override void set(T value) { super(value); } } " + + "class Annotated { @Tracked Int value = 1; @Lazy Int later.calc() = 2; } " + + "Int run(Int captured) { function Int() fn = () -> captured; return fn(); } }" + val baseline = bytes(compile(source)) + val inspected = compile(source) + val errors = ErrorList() + val snapshot = inspected.semanticSnapshots(errors).single() + assertThat(errors.errors).isEmpty() + assertThat(bytes(inspected)).containsExactly(*baseline) + assertPure(snapshot) + } + + @Test + fun `retained snapshots remain isolated through repeated replacement and concurrent pool free queries`() { + val snapshots = + (0 until 20).map { version -> + compile( + "module Boundary { Int run(Int n) { var value = n + $version; return value; } }", + ).semanticSnapshot() + } + assertThat(snapshots.map { it.id }.distinct()).hasSize(20) + Executors.newFixedThreadPool(4).use { executor -> + snapshots + .map { snapshot -> + executor.submit { + repeat(20) { + val local = snapshot.symbols.single { it.name == "value" } + assertThat(snapshot.type(local.type!!)!!.displayName).isEqualTo("Int") + assertThat(snapshot.occurrences.count { it.symbol == local.id }) + .isEqualTo(2) + assertPure(snapshot) + } + } + }.forEach { it.get() } + } + } + + private fun compile( + text: String, + repository: ModuleRepository? = null, + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule(Source(text, "file:///Boundary.x"), repository, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result + } + + private fun bytes(result: EmbeddingSupport.Compilation): ByteArray { + // Compilation timestamps deliberately vary; normalize only that metadata before comparison. + result.module().timestamp = result.pool().ensureTimeConstant(Instant.EPOCH) + return ByteArrayOutputStream().also { result.file().writeTo(it) }.toByteArray() + } + + private fun repository(vararg dependencies: EmbeddingSupport.Compilation): ModuleRepository = + BuildRepository().apply { + dependencies.forEach { + storeModule(FileStructure(ByteArrayInputStream(bytes(it))).module) + } + } + + private fun nodes(node: AstNode): List = + listOf(node) + + node + .children() + .iterator() + .asSequence() + .flatMap(::nodes) + .toList() + + private fun assertPure(root: SemanticModel) { + val seen = Collections.newSetFromMap(IdentityHashMap()) + + fun visit(value: Any?) { + if (value == null || !seen.add(value)) return + when (value) { + is String, + is Number, + is Boolean, + is Enum<*>, + is UUID, + -> { + return + } + + is Iterable<*> -> { + value.forEach(::visit) + } + + is Map<*, *> -> { + value.forEach { (key, item) -> + visit(key) + visit(item) + } + } + + else -> { + assertThat( + value.javaClass.name.startsWith(SemanticModel::class.java.name) || + value is XdkDependency.SymbolKey, + ).describedAs("Detached snapshot value: %s", value.javaClass.name) + .isTrue() + value.javaClass.declaredFields + .filterNot { Modifier.isStatic(it.modifiers) } + .forEach { + it.isAccessible = true + visit(it.get(value)) + } + } + } + } + visit(root) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt new file mode 100644 index 00000000000..aaeb4c8a4dc --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt @@ -0,0 +1,366 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.ModuleRepository +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.lsp.adapter.xdk.PartialSemanticModel +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import java.util.concurrent.Executors + +class CompilerCallSiteTest { + @Test + fun `function valued calls expose typed signatures without inventing method targets`() { + val source = "module Calls { Int apply(function Int(Int) fn) { return fn(42); } }" + val compilation = compile(source) + val model = compilation.semanticSnapshot() + assertThat(model.calls).isEmpty() + val call = model.functionCalls.single() + assertThat(call.signature.parameters.map { model.type(it.type)!!.displayName }) + .containsExactly("Int") + assertThat(call.signature.parameters.map { it.name }).containsExactly(null) + assertThat(call.signature.returns.map { model.type(it)!!.displayName }) + .containsExactly("Int") + assertThat(compilation.functionBindings()).hasSize(1) + val node = compilation.functionBindings().keys.single() + assertThat(compilation.functionBindings()[node.clone() as InvocationExpression]).isNull() + assertThatThrownBy { (compilation.functionBindings() as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).success).isTrue() + val help = requireNotNull(adapter.getSignatureHelp(URI, 0, source.indexOf("42") + 1)) + assertThat(help.signatures.single().label).isEqualTo("Int fn(Int)") + assertThat(help.activeParameter).isZero() + } + } + + @Test + fun `matching returns cannot validate a function call with invalid arguments`() { + val prefix = + "module Calls { function Int(Int, String) make(function Int(Int, String) fn) = fn; " + + "Int apply(function Int(Int, String) fn) = make(fn)(" + assertThat(compile("${prefix}1, \"x\"); }").functionBindings()).hasSize(1) + for (arguments in listOf("1", "True, \"x\"")) { + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule(Source("$prefix$arguments); }", URI), null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(result.functionBindings()).isEmpty() + } + } + + @Test + fun `super records the inherited body and instantiated signature`() { + val model = + compile( + "module Calls { class Base { T pick(T value) = value; } " + + "class Child extends Base { @Override String pick(String value) = super(value); } }", + ).semanticSnapshot() + val call = model.calls.single() + assertThat(model.symbol(call.method)!!.name).isEqualTo("pick") + assertThat( + model + .symbol(call.method)!! + .declaration!! + .start.column, + ).isEqualTo(33) + assertThat(call.signature.parameters.map { model.type(it.type)!!.displayName }) + .containsExactly("String") + assertThat(call.signature.returns.map { model.type(it)!!.displayName }) + .containsExactly("String") + } + + @Test + fun `selected generic calls copy instantiated signatures and named argument ordering`() { + val source = + """ + module Calls { + T echo(T value, T backup) { return value; } + void run() { String text = echo(backup = "b", value = "a"); } + } + """.trimIndent() + val compilation = compile(source) + val model = compilation.semanticSnapshot() + val call = model.calls.single { model.symbol(it.method)?.name == "echo" } + assertThat(call.signature.parameters.map { it.name }).containsExactly("value", "backup") + assertThat(call.signature.parameters.map { model.type(it.type)!!.displayName }).allSatisfy { + assertThat(it).contains("String") + } + assertThat(call.signature.returns.map { model.type(it)!!.displayName }) + .containsExactly("String") + assertThat(call.arguments.map { it.parameterIndex }).containsExactly(1, 0) + assertThat(call.arguments).allSatisfy { assertThat(it.named).isTrue() } + assertThat( + call.arguments.map { + source + .lines()[it.range.start.line] + .substring(it.range.start.column, it.range.end.column) + }, + ).containsExactly("backup = \"b\"", "value = \"a\"") + + ConstantPool.withPool(compilation.pool()).use { + val node = + nodes(compilation.parsed()).filterIsInstance().single { + it.resolvedMethod?.name == "echo" + } + assertThat(compilation.callBindings()[node]).isNotNull() + assertThat(compilation.callBindings()[node.clone() as InvocationExpression]).isNull() + assertThatThrownBy { (compilation.callBindings() as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + } + + @Test + fun `defaulted arguments retain their declaration metadata without synthetic source ranges`() { + val model = + compile( + "module Calls { Int add(Int left = 1, Int right = 2) { return left; } void run() { Int n = add(right = 4); } }", + ).semanticSnapshot() + val call = model.calls.single { model.symbol(it.method)?.name == "add" } + assertThat(call.signature.parameters).hasSize(2).allSatisfy { + assertThat(it.defaulted).isTrue() + } + assertThat(call.arguments.map { it.parameterIndex }).containsExactly(1) + assertThat( + call.arguments + .single() + .range.start, + ).isLessThan( + call.arguments + .single() + .range.end, + ) + } + + @Test + fun `foreign receiver candidates use substituted types and exclude inaccessible members`() { + val library = + compile( + """ + module Library { + class Box { + Element echo(Element value) { return value; } + String choose(String value) { return value; } + Int choose(Int value) { return value; } + String label = "box"; + private String secret() { return "hidden"; } + private String hidden = "hidden"; + } + } + """.trimIndent(), + ) + val repository = BuildRepository().apply { storeModule(library.module()) } + val model = + partial( + "module Consumer { package Lib import Library; void run(Lib.Box box) { box.", + repository, + ) + val site = model.sites.single() + assertThat(model.semantics.status).isEqualTo(SemanticModel.Status.PARTIAL) + assertThat(model.semantics.symbol(site.scope!!)!!.name).isEqualTo("run") + assertThat(site.members.map { it.name }) + .contains("echo", "choose", "label") + .doesNotContain("secret", "hidden") + val echo = site.members.single { it.name == "echo" }.signature!! + assertThat(echo.parameters.map { model.semantics.type(it.type)!!.displayName }) + .containsExactly("String") + assertThat(echo.returns.map { model.semantics.type(it)!!.displayName }) + .containsExactly("String") + assertThat(site.members.filter { it.name == "choose" }).hasSize(2) + assertThatThrownBy { (site.members as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @Test + fun `incomplete call candidates have no selected overload and count only top-level commas`() { + val model = + partial( + "module Calls { void run(String text) { text.indexOf(text.replace(\"a,b\", \"c\"), startAt = 2, ", + ) + val site = model.sites.single() + assertThat(site.kind).isEqualTo(PartialSemanticModel.Kind.CALL) + assertThat(site.calleeName).isEqualTo("indexOf") + assertThat(site.members).isNotEmpty().allSatisfy { + assertThat(it.name).isEqualTo("indexOf") + } + assertThat(site.argumentIndexAt(site.range.end)).isEqualTo(2) + assertThat(site.arguments.map { it.label }).containsExactly(null, "startAt") + assertThat(model.semantics.calls.map { model.semantics.symbol(it.method)?.name }) + .contains("replace") + .doesNotContain("indexOf") + } + + @Test + fun `inherited receiver type parameters are instantiated at the selected call`() { + val model = + compile( + """ + module Inherited { + class Base { Element echo(Element value) { return value; } } + class Child extends Base {} + String run(Child child) { return child.echo("ok"); } + } + """.trimIndent(), + ).semanticSnapshot() + val call = model.calls.single { model.symbol(it.method)?.name == "echo" } + assertThat(call.signature.parameters.map { model.type(it.type)!!.displayName }) + .containsExactly("String") + assertThat(call.signature.returns.map { model.type(it)!!.displayName }) + .containsExactly("String") + } + + @Test + fun `own receiver exposes private members and copied facts survive other compilations and threads`() { + val model = + partial( + "module Own { private String hidden = \"value\"; private Int secret() {return 1;} void run() { this.", + ) + val site = model.sites.single() + assertThat(site.members.map { it.name }).contains("hidden", "secret") + val firstScope = site.scope!! + val next = partial("module Own { void run(Int number) { number.") + assertThat(next.semantics.symbol(firstScope)).isNull() + Executors.newSingleThreadExecutor().use { executor -> + val copied = + executor + .submit> { + site.members + .filter { it.name == "hidden" } + .map { model.semantics.type(it.type!!)!!.displayName } + }.get() + assertThat(copied).containsExactly("String") + } + assertThatThrownBy { (model.semantics.calls as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @Test + fun `failed overload resolution and partial applications cannot claim selected call facts`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val failed = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Failed { Int choose(Int n) { return n; } void run() { Int n = choose(\"bad\"); }", + URI, + ), + null, + errors, + ) + assertThat(failed.succeeded()).isFalse() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(failed.semanticSnapshot().calls).isEmpty() + val binding = + compile( + "module Binding { Int choose(Int n) { return n; } void run() { function Int(Int) fn = &choose(_); Int n = fn(1); } }", + ).semanticSnapshot() + assertThat(binding.calls).isEmpty() + assertThat(binding.functionCalls).hasSize(1) + assertThat( + binding.functionCalls + .single() + .signature.parameters, + ).allSatisfy { + assertThat(it.name).isNull() + } + } + + @Test + fun `nested compilation records surviving lambdas anonymous methods and initializers once`() { + val compilation = + compile( + """ + module Nested { + String label = 1.toString(); + String run(Int number) { + function String() fn = () -> number.toString(); + Object object = new Object() { + @Override String toString() { return number.toString(); } + }; + return object.toString(); + } + } + """.trimIndent(), + ) + val model = compilation.semanticSnapshot() + val calls = model.calls.filter { model.symbol(it.method)?.name == "toString" } + assertThat(calls).hasSize(4) + assertThat(calls.map { it.range.start.line }).containsExactly(1, 3, 5, 7) + assertThat( + calls.map { it.signature.returns.map { type -> model.type(type)!!.displayName } }, + ).allSatisfy { assertThat(it).containsExactly("String") } + assertThat(nodes(compilation.parsed()).toSet()).containsAll(compilation.callBindings().keys) + } + + @Test + fun `unknown receiver and cancellation do not produce candidate members`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete( + Source("module Missing { void run() { unknown.", URI), + null, + errors, + ) + val model = analysis.semanticSnapshot(errors) + assertThat(model.sites.single().receiverType).isNull() + assertThat(model.sites.single().members).isEmpty() + val cancelled = analysis.semanticSnapshot(ErrorListener.cancellable(errors) { true }) + assertThat(cancelled.sites).isEmpty() + assertThat(cancelled.semantics.status).isEqualTo(SemanticModel.Status.UNAVAILABLE) + } + + private fun compile(text: String): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, URI), null, errors) + assertThat(errors.errors).describedAs("%s", errors.errors).isEmpty() + assertThat(result.succeeded()).isTrue() + return result + } + + private fun partial( + text: String, + repository: ModuleRepository? = null, + ): PartialSemanticModel { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), repository, errors) + .semanticSnapshot(errors) + assertThat(errors.errors.map { it.code }) + .describedAs("%s", errors.errors) + .containsExactly(Parser.UNEXPECTED_EOF) + return result + } + + private fun nodes(root: AstNode): Sequence = + sequence { + yield(root) + for (child in root.children()) yieldAll(nodes(child)) + } + + private companion object { + const val URI = "untitled:Calls.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallableProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallableProofTest.kt new file mode 100644 index 00000000000..d649672a87f --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallableProofTest.kt @@ -0,0 +1,221 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.CompilerRenameFacts +import org.xvm.lsp.adapter.xdk.ProofIdentity +import org.xvm.lsp.adapter.xdk.SemanticModel.SourceLocation +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.projectRenameFacts + +/** Stable detached facts must preserve every alternative and back edge across compiler attempts. */ +class CompilerCallableProofTest { + @Test + fun `fresh union proofs are equivalent but losing a target or its receiver is not`() { + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + Int use(First | Second target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)).isTrue() + val at = readSite(after, text) + val choice = after.callables.getValue(at) as ProofIdentity.Alternatives + assertThat(choice.targets).hasSize(2) + val branches = choice.targets.map { it as ProofIdentity.Composed } + val lost = choice.copy(targets = setOf(branches.first())) + val rebound = choice.copy(targets = setOf(branches.first().copy(owner = branches.last().owner), branches.last())) + val owner = branches.first().owner as ProofIdentity.Source + val missingSource = owner.copy(location = owner.location.copy(sourceName = null)) + val untranslatable = choice.copy(targets = setOf(branches.first().copy(owner = missingSource), branches.last())) + listOf(lost, rebound, untranslatable).forEach { damaged -> + assertThat(XdkRename.preservesBindings(before, replacing(after, at, damaged), plan)).isFalse() + } + val unreadable = replacing(after, at, untranslatable) + assertThat(XdkRename.preservesBindings(unreadable, unreadable, plan)).isFalse() + } + + @Test + fun `recursive contract proof retains the closing edge and delegate receiver`() { + val text = + """ + module App { + interface Api { Int read(); } + class Loop(Loop next) delegates Api(next) {} + Int use(Loop target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)).isTrue() + val at = readSite(after, text) + val route = after.callables.getValue(at) as ProofIdentity.Composed + assertThat(route.cycles).isNotEmpty() + assertThat(route.delegates).isNotEmpty() + listOf(route.copy(cycles = emptyList()), route.copy(delegates = emptyList())).forEach { damaged -> + assertThat(XdkRename.preservesBindings(before, replacing(after, at, damaged), plan)).isFalse() + } + } + + private fun replacing( + facts: CompilerRenameFacts, + at: SourceLocation, + identity: ProofIdentity, + ) = CompilerRenameFacts(facts.models, facts.constants, facts.methods, facts.properties, callables = facts.callables + (at to identity)) + + @Test + fun `nested delegate alternatives remain part of the call and declaration dispatch proofs`() { + val text = + """ + module App { + interface Api { Int read(); } + class First implements Api { @Override Int read() = 1; } + class Second implements Api { @Override Int read() = 2; } + class Forward(First | Second target) delegates Api(target) {} + Int use(Forward value) = value.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)).isTrue() + val at = readSite(after, text) + val route = after.callables.getValue(at) as ProofIdentity.Composed + val choice = route.alternatives.single() as ProofIdentity.Alternatives + assertThat(choice.targets).hasSize(2) + assertThat(route.delegates).isNotEmpty() + assertThat(after.methods.chains.any { it.alternatives.isNotEmpty() }).isTrue() + val lost = route.copy(alternatives = listOf(choice.copy(targets = setOf(choice.targets.first())))) + listOf(lost, route.copy(delegates = emptyList()), route.copy(alternatives = emptyList())).forEach { changed -> + assertThat(XdkRename.preservesBindings(before, replacing(after, at, changed), plan)).isFalse() + } + } + + @Test + fun `a changed generic argument cannot preserve the callable proof`() { + val text = + """ + module App { + class First(T value) { Int read() = 1; } + class Second(T value) { Int read() = 2; } + Int use(First | Second target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)).isTrue() + val at = readSite(after, text) + val choice = after.callables.getValue(at) as ProofIdentity.Alternatives + val branches = choice.targets.map { it as ProofIdentity.Composed } + val owner = branches.first().owner as ProofIdentity.TypeShape + val other = branches.last().owner as ProofIdentity.TypeShape + assertThat(owner.components.last()).isNotEqualTo(other.components.last()) + val substituted = owner.copy(components = listOf(owner.components.first(), other.components.last())) + val changed = choice.copy(targets = setOf(branches.first().copy(owner = substituted), branches.last())) + assertThat(XdkRename.preservesBindings(before, replacing(after, at, changed), plan)).isFalse() + } + + @Test + fun `annotation constructor arguments participate in callable equivalence`() { + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + annotation Mark(String label) into Object {} + Int use((@Mark("one") First) | (@Mark("two") Second) target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)).isTrue() + val at = readSite(after, text) + val choice = after.callables.getValue(at) as ProofIdentity.Alternatives + val branches = choice.targets.map { it as ProofIdentity.Composed } + val owner = branches.first().owner as ProofIdentity.TypeShape + val argument = owner.components.last() as ProofIdentity.Value + val changedOwner = owner.copy(components = owner.components.dropLast(1) + argument.copy(value = "changed")) + val changed = choice.copy(targets = setOf(branches.first().copy(owner = changedOwner), branches.last())) + assertThat(XdkRename.preservesBindings(before, replacing(after, at, changed), plan)).isFalse() + } + + @ParameterizedTest + @ValueSource(strings = ["Float64", "Dec64"]) + fun `changed numeric annotation arguments cannot preserve dispatch`(type: String) { + val first = "$type:1.5" + val second = "$type:2.5" + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + annotation Mark($type value) into Object {} + Int use((@Mark($first) First) | Second target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val changed = text.replace(first, second) + val after = facts(changed) + val start = text.indexOf(first) + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(XdkRename.Edit(start, start + first.length, second)))) + assertThat(XdkRename.preservesBindings(before, facts(text), XdkRename.Plan(mapOf(SOURCE to text), emptyMap()))).isTrue() + assertThat(XdkRename.preservesBindings(before, after, plan)).isFalse() + } + + @ParameterizedTest + @ValueSource(strings = ["List", "Map"]) + fun `nested type arguments remain equivalent across fresh pools`(argument: String) { + val text = + """ + module App { + class First(T value) { T read() = value; } + class Second(T value) { T read() = value; } + $argument use(First<$argument> | Second<$argument> target) = target.read(); + } + """.trimIndent() + val before = facts(text) + val after = facts(text) + val plan = XdkRename.Plan(mapOf(SOURCE to text), emptyMap()) + assertThat(XdkRename.preservesBindings(before, after, plan)) + .describedAs(after.callables.toString()) + .isTrue() + } + + private fun readSite( + facts: CompilerRenameFacts, + text: String, + ): SourceLocation { + val prefix = text.take(text.lastIndexOf("read")) + val line = prefix.count { it == '\n' } + val column = prefix.substringAfterLast('\n').length + return SourceLocation(SOURCE, requireNotNull(facts.models.single().occurrenceAt(line, column)).range) + } + + private fun facts(text: String): CompilerRenameFacts { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = EmbeddingSupport.instance().compileModule(Source(text, SOURCE), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + return compilation.projectRenameFacts(XdkDependencies(emptyList()).open(), errors).also { + assertThat(errors.hasSeriousErrors()).describedAs(errors.errors.toString()).isFalse() + } + } + + private companion object { + const val SOURCE = "App.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerConsumerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerConsumerTest.kt index 02a5dcd4c71..6145dd1b533 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerConsumerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerConsumerTest.kt @@ -5,13 +5,18 @@ import org.junit.jupiter.api.Test import org.xvm.api.EmbeddingSupport import org.xvm.asm.ErrorList -/** A required consumer of the existing Java API; no installed XDK or later listener API is needed. */ +/** + * A required consumer of the existing Java API; no installed XDK or later listener API is needed. + */ class CompilerConsumerTest { @Test fun `compiled variants support a real embedding compilation`() { CompilerTestSupport.configure() val errors = ErrorList(100) - val module = EmbeddingSupport.instance().compile("module Consumer { Int answer() = 42; }", null, errors) + val module = + EmbeddingSupport + .instance() + .compile("module Consumer { Int answer() = 42; }", null, errors) assertThat(errors.errors).isEmpty() assertThat(module).isNotNull() diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDispatchRoutesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDispatchRoutesTest.kt new file mode 100644 index 00000000000..83df1cbc44c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDispatchRoutesTest.kt @@ -0,0 +1,238 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.ConstantPool +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.asm.MethodStructure +import org.xvm.asm.constants.MethodBody.Implementation +import org.xvm.asm.constants.TypeInfo +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.lsp.adapter.xdk.compilerMethodRelations +import org.xvm.lsp.adapter.xdk.compilerPropertyRelations +import org.xvm.lsp.adapter.xdk.compilerSourceTypes +import org.xvm.lsp.adapter.xdk.dispatch +import org.xvm.lsp.adapter.xdk.methodImplementation + +/** Real compiler chains: a body category alone must never manufacture an editable declaration. */ +class CompilerDispatchRoutesTest { + @Test + fun `rename relations inspect conditional methods and properties on validated concrete hosts`() { + CompilerTestSupport.configure() + val text = + """ + module Routes { + class Box(T value) incorporates conditional Textual {} + static mixin Textual into Box { + Int size() = value.size; + Int length.get() = value.size; + } + Int read(Box text) = text.size() + text.length; + Int unrelated(Box number) = number.value; + } + """.trimIndent() + val errors = ErrorList() + val compilation = EmbeddingSupport.instance().compileModule(Source(text, "Routes.x"), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + + fun nodes(node: AstNode): List = listOf(node) + node.childNodes().flatMap(::nodes) + val nodes = nodes(requireNotNull(compilation.parsed())) + ConstantPool.withPool(compilation.pool()).use { + val methods = compilerMethodRelations(nodes, errors).chains.filter { it.owner.name == "Box" } + assertThat(methods).anySatisfy { chain -> + assertThat(chain.supported).isTrue() + assertThat(chain.methods.map { it.namespace.name to it.name }).contains("Textual" to "size") + } + val properties = compilerPropertyRelations(nodes, errors).chains.filter { it.owner.name == "Box" } + assertThat(properties).anySatisfy { chain -> + assertThat(chain.supported).isTrue() + assertThat(chain.properties.map { it.parentConstant.name to it.name }).contains("Textual" to "length") + } + val types = compilerSourceTypes(nodes) + assertThat(types).doesNotHaveDuplicates() + assertThat(types).noneMatch { it.isFormalType } + } + assertThat(errors.hasSeriousErrors()).describedAs(errors.errors.toString()).isFalse() + } + + @Test + fun `capped implementation lookup follows the compiler narrowing method`() { + inspect( + "interface Api { Api self(); } class Box implements Api { @Override Box self() = this; }", + ) { module, errors -> + val owner = type(module, "Box", errors) + val capped = owner.methods.values.filter { method -> method.chain.any { it.implementation == Implementation.Capped } } + assertThat(capped).isNotEmpty() + capped.forEach { method -> + assertThat(owner.methodImplementation(method, errors)?.namespace?.name).isEqualTo("Box") + } + } + } + + @Test + fun `into implementation lookup follows the existing constraint method`() { + inspect("class Base { Int value() = 1; } mixin Mix into Base {}") { module, errors -> + val owner = type(module, "Mix", errors) + val method = owner.methods.values.single { it.identity.name == "value" } + assertThat(method.chain.map { it.implementation }).contains(Implementation.FromInto) + assertThat(owner.methodImplementation(method, errors)?.namespace?.name).isEqualTo("Base") + } + } + + @Test + fun `written interface default and class override retain their actual source contracts`() { + inspect( + "interface Api { Int read() = 1; } class Box implements Api { @Override Int read() = 2; }", + ) { module, errors -> + val owner = type(module, "Box", errors) + val method = owner.methods.values.single { it.identity.name == "read" } + assertThat(method.chain.map { it.implementation }) + .contains(Implementation.Explicit, Implementation.Default) + val route = owner.dispatch(method, errors) + assertThat(route.supported).isTrue() + assertThat(route.methods.map { it.namespace.name }) + .containsExactlyInAnyOrder("Box", "Api") + } + } + + @Test + fun `bodyless written class methods retain source contracts without executable targets`() { + inspect("class Base { Int read(); } class Actual extends Base { @Override Int read() = 1; }") { module, errors -> + val owner = type(module, "Base", errors) + val method = owner.methods.values.single { it.identity.name == "read" } + assertThat(method.chain.map { it.implementation }).contains(Implementation.SansCode) + val route = owner.dispatch(method, errors) + assertThat(route.supported).isTrue() + assertThat(route.methods.map { it.namespace.name to it.name }).containsExactly("Base" to "read") + assertThat(owner.methodImplementation(method, errors)).isNull() + } + } + + @Test + fun `delegate provenance retains receiver and written interface and implementation`() { + inspect( + "interface Api { Int read(); } class Actual implements Api { @Override Int read() = 1; } " + + "class Box(Actual target) delegates Api(target) {}", + ) { module, errors -> + val owner = type(module, "Box", errors) + val method = owner.methods.values.single { it.identity.name == "read" } + assertThat(method.chain.map { it.implementation }).contains(Implementation.Delegating) + val route = owner.dispatch(method, errors) + assertThat(route.supported).isTrue() + assertThat(route.delegates.map { it.name }).containsExactly("target") + assertThat(route.methods.map { it.namespace.name }).contains("Actual", "Api") + } + } + + @Test + fun `union type retains separate receiver routes alongside its written contracts`() { + inspect("class First { Int read() = 1; } class Second { Int read() = 2; }") { module, errors -> + val first = (module.getChild("First") as ClassStructure).formalType + val second = (module.getChild("Second") as ClassStructure).formalType + val owner = + module.constantPool.ensureUnionTypeConstant(first, second).ensureTypeInfo(errors) + val method = owner.methods.values.single { it.identity.name == "read" } + assertThat(method.chain.map { it.implementation }).contains(Implementation.Union) + val dispatch = owner.dispatch(method, errors) + assertThat(dispatch.supported).isTrue() + assertThat(dispatch.methods.map { it.namespace.name }).containsExactlyInAnyOrder("First", "Second") + val branches = dispatch.alternatives.single().branches + assertThat(branches.map { it.receiver }).containsExactlyInAnyOrder(first, second) + branches.forEach { branch -> + assertThat(branch.dispatch.supported).isTrue() + assertThat( + branch.dispatch.methods + .single() + .namespace, + ).isEqualTo(branch.receiver.definingConstant) + } + } + } + + @Test + fun `recursive delegation retains a finite contract and explicit closing edge`() { + inspect("interface Api { Int read(); } class Loop(Loop next) delegates Api(next) {}") { module, errors -> + val owner = type(module, "Loop", errors) + val method = owner.methods.values.single { it.identity.name == "read" } + val route = owner.dispatch(method, errors) + assertThat(route.supported).isTrue() + assertThat(route.methods.map { it.namespace.name to it.name }).containsExactly("Api" to "read") + assertThat(route.delegates.map { it.name }).containsExactly("next") + val cycle = route.cycles.single() + assertThat(cycle.receiver.getSingleUnderlyingClass(false).name).isEqualTo("Loop") + assertThat(cycle.contracts.map { it.namespace.name to it.name }).containsExactly("Api" to "read") + assertThat(owner.methodImplementation(method, errors)).isNull() + } + } + + @Test + fun `generated shorthand constructors do not become independently editable methods`() { + inspect("class Box(Int value = 1) {} Box make() = new Box(value = 2);") { module, errors -> + val owner = type(module, "Box", errors) + val constructors = + owner.methods.values.filter { + it.isCtorOrValidator && it.chain.any { body -> body.isSynthetic } + } + assertThat(constructors).isNotEmpty() + constructors.forEach { assertThat(owner.dispatch(it, errors).supported).isFalse() } + } + } + + @Test + fun `implicit virtual child constructors remain construction contracts rather than renameable methods`() { + inspect( + "class Base { class Child { construct() {} construct(Int value) {} } } " + + "class Derived extends Base { @Override class Child {} }", + ) { module, errors -> + val child = (module.getChild("Derived") as ClassStructure).getChild("Child") as ClassStructure + val owner = child.formalType.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) + val implicit = owner.methods.values.filter { it.chain.any { body -> body.implementation == Implementation.Implicit } } + // Implicit bodies have no MethodStructure for isCtorOrValidator to classify. Inspect + // their original declaration identity, without treating it as an executable body here. + val declarations = implicit.mapNotNull { it.identity.component as? MethodStructure } + assertThat(declarations.filter { it.isConstructor }).isNotEmpty() + implicit.forEach { assertThat(owner.dispatch(it, errors).supported).isFalse() } + } + } + + @Test + fun `native binary methods do not acquire written executable implementation targets`() { + inspect("") { module, errors -> + val info = module.constantPool.typeString().ensureTypeInfo(errors) + val native = info.methods.values.filter { method -> method.chain.firstOrNull()?.implementation == Implementation.Native } + assertThat(native).isNotEmpty() + native.forEach { method -> assertThat(info.methodImplementation(method, errors)).isNull() } + } + } + + private fun type( + module: ClassStructure, + name: String, + errors: ErrorList, + ): TypeInfo = + (module.getChild(name) as ClassStructure) + .formalType + .ensureAccess(Access.PRIVATE) + .ensureTypeInfo(errors) + + private fun inspect( + body: String, + check: (ClassStructure, ErrorList) -> Unit, + ) { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule(Source("module Routes { $body }", "Routes.x"), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + ConstantPool.withPool(compilation.pool()).use { + check(requireNotNull(compilation.file()).module, errors) + } + assertThat(errors.hasSeriousErrors()).describedAs(errors.errors.toString()).isFalse() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerEmissionAuditTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerEmissionAuditTest.kt new file mode 100644 index 00000000000..4ac25a5d06b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerEmissionAuditTest.kt @@ -0,0 +1,155 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.Component +import org.xvm.asm.ErrorList +import org.xvm.asm.FileStructure +import org.xvm.asm.MethodStructure +import org.xvm.asm.ModuleRepository +import org.xvm.asm.MultiMethodStructure +import org.xvm.asm.op.GP_Sub +import org.xvm.asm.op.Invoke_01 +import org.xvm.asm.op.JumpInt +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.Source +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream + +/** Source-level probes for the remaining post-validation TypeInfo consumers. */ +class CompilerEmissionAuditTest { + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `generic atomic operations preserve their warning across source and dependency use`(external: Boolean) { + val base = "class Base { @Atomic Int count = 1; }" + val repository = + if (external) { + BuildRepository().apply { + storeModule(roundTrip(compile("module Library { $base }")).module) + } + } else { + null + } + val declarations = if (external) "package lib import Library; import lib.Base;" else base + val source = + "module Emission { $declarations " + + "class Derived extends Base { @Atomic @Override Int count = 2; } " + + "Int run(Derived value) { value.count += 1; return ++value.count; } }" + val result = compile(source, repository, expectedCodes = listOf("VERIFY-75")) + assertThat(restoredMethod(result).ops).isNotEmpty() + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "Bit", + "Nibble", + "Char", + "Int8", + "Int16", + "Int32", + "Int64", + "Int128", + "IntN", + "UInt8", + "UInt16", + "UInt32", + "UInt64", + "UInt128", + "UIntN", + ], + ) + fun `dense switches retain conversion instructions in serialized bytecode`(type: String) { + val first = + if (type == "Char") { + "'a'" + } else if (type == "Bit") { + "0" + } else { + "1" + } + val second = + if (type == "Char") { + "'b'" + } else if (type == "Bit") { + "1" + } else { + "2" + } + val result = + compile( + "module Emission { Int run($type value) { switch (value) { " + + "case $first: return 10; case $second: return 20; default: return 30; } } }", + ) + val method = restoredMethod(result) + assertThat(method.ops.filterIsInstance()).hasSize(1) + assertThat(method.ops.filterIsInstance()).hasSize(if (type == "Int64") 0 else 1) + assertThat(method.ops.filterIsInstance()).hasSize(if (type == "Bit") 0 else 1) + } + + @Test + fun `enum switches avoid ordinal conversion in serialized code`() { + val result = + compile( + "module Emission { enum Choice { First, Second } Int run(Choice value) { switch (value) { " + + "case First: return 1; case Second: return 2; } } }", + ) + val method = restoredMethod(result) + assertThat(method.ops.filterIsInstance()).isEmpty() + assertThat(method.ops.filterIsInstance()).isEmpty() + } + + @ParameterizedTest + @ValueSource(strings = ["Int run() = ++count;", "void run() { count -= 1; }"]) + fun `invalid atomic operations produce source errors before emission`(method: String) { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = + Source( + "module Emission { @Atomic String count = \"x\"; $method }", + "file:///Emission.x", + ) + val result = EmbeddingSupport.instance().compileModule(source, null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5").anyMatch { + it.startsWith("COMPILER-") + } + } + + private fun compile( + source: String, + repository: ModuleRepository? = null, + expectedCodes: List = emptyList(), + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule(Source(source, "file:///Emission.x"), repository, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors.map { it.code }).containsExactlyElementsOf(expectedCodes) + return result + } + + private fun restoredMethod( + result: EmbeddingSupport.Compilation, + vararg owners: String, + ): MethodStructure { + val restored = + owners.fold(roundTrip(result).module as Component) { parent, name -> + parent.getChild(name) + } + return (restored.getChild("run") as MultiMethodStructure).methods().single() + } + + private fun roundTrip(result: EmbeddingSupport.Compilation): FileStructure { + val bytes = ByteArrayOutputStream().also { result.file().writeTo(it) }.toByteArray() + return FileStructure(ByteArrayInputStream(bytes)) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt new file mode 100644 index 00000000000..3820a3d7288 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt @@ -0,0 +1,57 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.projectRenameFacts + +class CompilerExtractMethodFactsTest { + @Test + fun `capture evidence distinguishes stable values from writes and reference storage`() { + CompilerTestSupport.configure() + val text = + """ + module Extract { + Int read(Int input, Int changed) { + Int fixed = input + 1; + Int mutable = input; + @Volatile Int stored = input; + changed++; + mutable++; + return fixed + mutable + stored + changed; + } + } + """.trimIndent() + val errors = ErrorList() + val compiled = EmbeddingSupport.instance().compileModule(Source(text, "Extract.x"), null, errors) + assertThat(compiled.succeeded()).describedAs(errors.errors.toString()).isTrue() + val facts = compiled.projectRenameFacts(XdkDependencies(emptyList()).open(), errors, includeMembers = true) + val model = facts.models.single() + val stable = + model.symbols + .filter { symbol -> + facts.extraction.stableValues.any { it.sourceName == symbol.declarationSource && it.range == symbol.declaration } + }.map { it.name } + assertThat(stable).contains("input", "fixed").doesNotContain("changed", "mutable", "stored") + assertThat(facts.extraction.types).isNotEmpty() + } + + @Test + fun `normal rename facts do not collect extraction types`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val compiled = + EmbeddingSupport.instance().compileModule( + Source("module Extract { Int read(Int x) = x; }", "Extract.x"), + null, + errors, + ) + assertThat(compiled.succeeded()).isTrue() + val facts = compiled.projectRenameFacts(XdkDependencies(emptyList()).open(), errors) + assertThat(facts.extraction.types).isEmpty() + assertThat(facts.extraction.stableValues).isEmpty() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt new file mode 100644 index 00000000000..ef37976eb33 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt @@ -0,0 +1,106 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.CompilerRenameFacts +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkMethodExtraction +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.projectRenameFacts + +class CompilerMethodExtractionProofTest { + @Test + fun `compiling helper with swapped arguments fails capture proof`() { + val text = + """ + module Extract { + Int read(Int first, Int other) { + return first - other; + } + } + """.trimIndent() + val before = facts(text) + val at = text.positionOf("return first - other", "first - other") + val candidate = + requireNotNull( + XdkMethodExtraction.candidate( + text, + Range(at, Position(at.line, at.column + "first - other".length)), + before.models.single(), + before.extraction, + ), + ) + + fun plan(value: XdkMethodExtraction.Candidate) = + XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to value.edits), relocations = mapOf(SOURCE to listOf(value.relocation))) + val correct = plan(candidate) + assertThat(XdkRename.preservesMethodExtraction(before, facts(correct.proposed.getValue(SOURCE)), correct, candidate)).isTrue() + + val swapped = candidate.copy(replacement = candidate.replacement.copy(text = "extractedMethod(other, first)")) + val wrong = plan(swapped) + assertThat(XdkRename.preservesMethodExtraction(before, facts(wrong.proposed.getValue(SOURCE)), wrong, swapped)).isFalse() + } + + @Test + fun `helper relocation cannot silently change a same named call target`() { + val text = + """ + module Extract { + class First { + Int step() = 1; + Int read() { + return step(); + } + } + class Other { + Int step() = 2; + } + } + """.trimIndent() + val before = facts(text) + val at = text.positionOf("return step()", "step()") + val candidate = + requireNotNull( + XdkMethodExtraction.candidate( + text, + Range(at, Position(at.line, at.column + "step()".length)), + before.models.single(), + before.extraction, + ), + ) + val correct = + XdkRename.Plan( + mapOf(SOURCE to text), + mapOf(SOURCE to candidate.edits), + relocations = mapOf(SOURCE to listOf(candidate.relocation)), + ) + assertThat(XdkRename.preservesMethodExtraction(before, facts(correct.proposed.getValue(SOURCE)), correct, candidate)).isTrue() + // Rename the original step and add a new step at a different source location. The moved + // call still compiles but binds to the new method, so its relocation must be rejected. + val rename = XdkRename.Edit(text.indexOf("Int step()") + 4, text.indexOf("Int step()") + 8, "changed") + val redirected = candidate.insertion.copy(text = candidate.insertion.text + "\n Int step() = 3;") + val altered = candidate.copy(insertion = redirected, relocation = candidate.relocation.copy(destination = redirected)) + val wrong = + XdkRename.Plan( + mapOf(SOURCE to text), + mapOf(SOURCE to altered.edits + rename), + relocations = mapOf(SOURCE to listOf(altered.relocation)), + ) + assertThat(XdkRename.preservesMethodExtraction(before, facts(wrong.proposed.getValue(SOURCE)), wrong, altered)).isFalse() + } + + private fun facts(text: String): CompilerRenameFacts { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, SOURCE), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.projectRenameFacts(XdkDependencies(emptyList()).open(), errors, includeMembers = true) + } + + private companion object { + const val SOURCE = "Extract.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt new file mode 100644 index 00000000000..812c92d00aa --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt @@ -0,0 +1,404 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.CompilerRenameFacts +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkMissingMethods.Dispatch +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.memberActionFacts +import org.xvm.lsp.adapter.xdk.missingMethodDestinations +import org.xvm.lsp.adapter.xdk.projectRenameFacts +import org.xvm.lsp.adapter.xdk.renameFacts +import org.xvm.lsp.adapter.xdk.toDependency + +class CompilerMissingMethodProofTest { + @Test + fun `imports shift the inserted method but cannot weaken its source signature proof`() { + CompilerTestSupport.configure() + val librarySource = "/proof/Library.x" + val libraryText = "module Library { class Other {} }" + val text = + """ + module Missing { + package lib import Library; + package shared import Types; + Object read(lib.Other peer, shared.Value value) { + val local = value; + return peer.missing(local); + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val types = embedding.compileModule(Source("module Types { class Value {} }", "/proof/Types.x"), null, errors) + assertThat(types.succeeded()).isTrue() + val typeDependency = types.toDependency() + val library = embedding.compileModule(Source(libraryText, librarySource), null, errors) + assertThat(library.succeeded()).isTrue() + val dependencies = XdkDependencies(listOf(library.toDependency(), typeDependency)) + val destinations = library.missingMethodDestinations(setOf("Types")) + val open = dependencies.open() + val failed = embedding.compileModule(Source(text, SOURCE), open.repository, ErrorList()) + assertThat(failed.succeeded()).isFalse() + val inputs = failed.renameFacts(open, includeMissingMethods = true, missingDestinations = destinations).missingMethodInputs + val fresh = dependencies.open() + val declarations = embedding.analyzeDeclarations(Source(text, SOURCE), fresh.repository, errors).orElseThrow() + val candidate = declarations.memberActionFacts(fresh, errors, inputs, destinations).missingMethods.single() + val edits = requireNotNull(candidate.edit(libraryText)) + assertThat(edits.imports).hasSize(1) + + fun proves(member: XdkRename.Edit): Boolean { + val plan = XdkRename.Plan(mapOf(SOURCE to text, librarySource to libraryText), mapOf(librarySource to (edits.imports + member))) + val nextErrors = ErrorList() + val typeInputs = XdkDependencies(listOf(typeDependency)).open() + val nextLibrary = + embedding.compileModule( + Source(plan.proposed.getValue(librarySource), librarySource), + typeInputs.repository, + nextErrors, + ) + assertThat(nextLibrary.succeeded()).describedAs(nextErrors.errors.toString()).isTrue() + val next = XdkDependencies(listOf(nextLibrary.toDependency(), typeDependency)).open() + val caller = embedding.compileModule(Source(text, SOURCE), next.repository, nextErrors) + assertThat(caller.succeeded()).describedAs(nextErrors.errors.toString()).isTrue() + val after = + CompilerRenameFacts.merge( + mapOf( + "library" to nextLibrary.projectRenameFacts(typeInputs, nextErrors), + "caller" to caller.projectRenameFacts(next, nextErrors), + ), + ) + return candidate.bindsNewMethod(after, plan, member) + } + assertThat(proves(edits.member)).isTrue() + assertThat(proves(edits.member.copy(text = edits.member.text.replace("types.Value arg1", "Object arg1")))).isFalse() + assertThat(proves(edits.member.copy(text = edits.member.text.replace("public Object", "public String")))).isFalse() + } + + @Test + fun `cross module proof reads the actual destination signature and rejects compatible substitutions`() { + CompilerTestSupport.configure() + val librarySource = "/proof/Library.x" + val libraryText = "module Library { class Other {} }" + val text = + """ + module Missing { + package lib import Library; + Object read(lib.Other peer, String value) { + return peer.missing(value); + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val library = embedding.compileModule(Source(libraryText, librarySource), null, errors) + assertThat(library.succeeded()).describedAs(errors.errors.toString()).isTrue() + val dependencies = XdkDependencies(listOf(library.toDependency())) + val destinations = library.missingMethodDestinations() + val open = dependencies.open() + val failed = embedding.compileModule(Source(text, SOURCE), open.repository, ErrorList()) + assertThat(failed.succeeded()).isFalse() + val inputs = failed.renameFacts(open, includeMissingMethods = true, missingDestinations = destinations).missingMethodInputs + val fresh = dependencies.open() + val declarations = embedding.analyzeDeclarations(Source(text, SOURCE), fresh.repository, errors).orElseThrow() + val headers = declarations.memberActionFacts(fresh, errors, inputs, destinations) + assertThat(errors.errors).isEmpty() + val candidate = headers.missingMethods.single() + val edit = requireNotNull(candidate.edit(libraryText)).member + + fun proves(proposed: XdkRename.Edit): Boolean { + val plan = XdkRename.Plan(mapOf(SOURCE to text, librarySource to libraryText), mapOf(librarySource to listOf(proposed))) + val nextErrors = ErrorList() + val nextLibrary = embedding.compileModule(Source(plan.proposed.getValue(librarySource), librarySource), null, nextErrors) + assertThat(nextLibrary.succeeded()).describedAs(nextErrors.errors.toString()).isTrue() + val next = XdkDependencies(listOf(nextLibrary.toDependency())).open() + val caller = embedding.compileModule(Source(text, SOURCE), next.repository, nextErrors) + assertThat(caller.succeeded()).describedAs(nextErrors.errors.toString()).isTrue() + val after = + CompilerRenameFacts.merge( + mapOf( + "library" to nextLibrary.projectRenameFacts(XdkDependencies(emptyList()).open(), nextErrors), + "caller" to caller.projectRenameFacts(next, nextErrors), + ), + ) + return candidate.bindsNewMethod(after, plan, proposed) + } + assertThat(proves(edit)).isTrue() + assertThat(proves(edit.copy(text = edit.text.replace("String arg1", "Object arg1")))).isFalse() + assertThat(proves(edit.copy(text = edit.text.replace("public Object", "public String")))).isFalse() + } + + @Test + fun `cross owner local and initializer proof rejects compatible signature changes and argument rebinding`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + class Other {} + Object read(Other peer, String value) { + var first = value; + var other = "other"; + Object result = peer.missing(first); + return result; + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val failed = embedding.compileModule(Source(text, SOURCE), null, ErrorList()) + assertThat(failed.succeeded()).isFalse() + val inputs = failed.renameFacts(XdkDependencies(emptyList()).open(), includeMissingMethods = true).missingMethodInputs + val errors = ErrorList() + val declarations = embedding.analyzeDeclarations(Source(text, SOURCE), null, errors).orElseThrow() + val headers = declarations.memberActionFacts(XdkDependencies(emptyList()).open(), errors, inputs) + assertThat(errors.errors).isEmpty() + val candidate = headers.missingMethods.single() + assertThat(candidate.declaration).isEqualTo("public Object missing(String arg1)") + assertThat(candidate.arguments).hasSize(1) + val edit = requireNotNull(candidate.edit(text)).member + + fun proves( + proposed: XdkRename.Edit, + additional: List = emptyList(), + ): Boolean { + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to (additional + proposed))) + val compiledErrors = ErrorList() + val compiled = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, compiledErrors) + assertThat(compiled.succeeded()).describedAs(compiledErrors.errors.toString()).isTrue() + val after = compiled.projectRenameFacts(XdkDependencies(emptyList()).open(), compiledErrors) + return candidate.bindsNewMethod(after, plan, proposed) + } + assertThat(proves(edit)).isTrue() + // Both changes still compile, but change the API requested by the original local types. + assertThat(proves(edit.copy(text = edit.text.replace("String arg1", "Object arg1")))).isFalse() + assertThat(proves(edit.copy(text = edit.text.replace("public Object", "public String")))).isFalse() + val argument = text.indexOf("first);") + val redirect = XdkRename.Edit(argument, argument + "first".length, "other") + assertThat(proves(edit, listOf(redirect))).isFalse() + } + + @Test + fun `cross owner repair must retain signature identity even when another return type compiles`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + class Other {} + Object read(Other peer) { + return peer.missing(); + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val failed = embedding.compileModule(Source(text, SOURCE), null, ErrorList()) + assertThat(failed.succeeded()).isFalse() + val errors = ErrorList() + val declarations = embedding.analyzeDeclarations(Source(text, SOURCE), null, errors).orElseThrow() + val inputs = failed.renameFacts(XdkDependencies(emptyList()).open(), includeMissingMethods = true).missingMethodInputs + val headers = declarations.memberActionFacts(XdkDependencies(emptyList()).open(), errors, inputs) + assertThat(errors.errors).isEmpty() + val candidate = headers.missingMethods.single() + assertThat(candidate.declaration).isEqualTo("public Object missing()") + val edit = requireNotNull(candidate.edit(text)).member + + fun proves(edit: XdkRename.Edit): Boolean { + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(edit))) + val compiledErrors = ErrorList() + val compiled = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, compiledErrors) + assertThat(compiled.succeeded()).describedAs(compiledErrors.errors.toString()).isTrue() + val after = compiled.projectRenameFacts(XdkDependencies(emptyList()).open(), compiledErrors) + return candidate.bindsNewMethod(after, plan, edit) + } + assertThat(proves(edit)).isTrue() + // A String still fits the caller's Object result, but it changes the requested API. + assertThat(proves(edit.copy(text = edit.text.replace("public Object", "public String")))).isFalse() + } + + @Test + fun `class qualifier proof requires static dispatch and the inserted owner declaration`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + class Other { static Int missing(Int value) = value; } + class Box { + Int read(Int value) { + return Box.missing(value); + } + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val failed = embedding.compileModule(Source(text, SOURCE), null, errors) + assertThat(failed.succeeded()).isFalse() + val inputs = failed.renameFacts(XdkDependencies(emptyList()).open(), includeMissingMethods = true).missingMethodInputs + assertThat(inputs.receivers.values.map { it.dispatch }).containsExactly(Dispatch.STATIC) + val declarationErrors = ErrorList() + val analysis = embedding.analyzeDeclarations(Source(text, SOURCE), null, declarationErrors).orElseThrow() + val headers = analysis.memberActionFacts(XdkDependencies(emptyList()).open(), declarationErrors, inputs) + assertThat(declarationErrors.errors).isEmpty() + val candidate = headers.missingMethods.single() + assertThat(candidate.declaration).isEqualTo("private static Int64 missing(Int64 arg1)") + val edit = requireNotNull(candidate.edit(text)).member + + fun compile(plan: XdkRename.Plan): CompilerRenameFacts { + val repairedErrors = ErrorList() + val repaired = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, repairedErrors) + assertThat(repaired.succeeded()).describedAs(repairedErrors.errors.toString()).isTrue() + return repaired.projectRenameFacts(XdkDependencies(emptyList()).open(), repairedErrors) + } + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(edit))) + val after = compile(plan) + assertThat(candidate.bindsNewMethod(after, plan, edit)).isTrue() + assertThat(candidate.copy(dispatch = Dispatch.INSTANCE).bindsNewMethod(after, plan, edit)).isFalse() + val qualifier = text.indexOf("Box.missing") + val redirect = XdkRename.Edit(qualifier, qualifier + "Box".length, "Other") + val wrong = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(redirect, edit))) + assertThat(candidate.bindsNewMethod(compile(wrong), wrong, edit)).isFalse() + } + + @Test + fun `repair must preserve a same owner receiver even when another receiver compiles`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + class Box { + Int read(Box peer, Int value) { + return peer.missing(value); + } + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val failed = embedding.compileModule(Source(text, SOURCE), null, errors) + assertThat(failed.succeeded()).isFalse() + val partial = failed.renameFacts(XdkDependencies(emptyList()).open()) + val inputs = failed.renameFacts(XdkDependencies(emptyList()).open(), includeMissingMethods = true).missingMethodInputs + assertThat(inputs.receivers).hasSize(1) + val declarationErrors = ErrorList() + val analysis = embedding.analyzeDeclarations(Source(text, SOURCE), null, declarationErrors).orElseThrow() + val headers = analysis.memberActionFacts(XdkDependencies(emptyList()).open(), declarationErrors, inputs) + assertThat(declarationErrors.errors).isEmpty() + val candidate = headers.missingMethods.single() + assertThat(candidate.declaration).isEqualTo("private Int64 missing(Int64 arg1)") + val edit = requireNotNull(candidate.edit(text)).member + val before = CompilerRenameFacts.merge(mapOf("headers" to headers, "partial" to partial)) + + fun proves(plan: XdkRename.Plan): Boolean { + val repairedErrors = ErrorList() + val repaired = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, repairedErrors) + assertThat(repaired.succeeded()).describedAs(repairedErrors.errors.toString()).isTrue() + val after = repaired.projectRenameFacts(XdkDependencies(emptyList()).open(), repairedErrors) + assertThat(candidate.bindsNewMethod(after, plan, edit)).isTrue() + return XdkRename.preservesKnownBindings(before, after, plan) + } + assertThat(proves(XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(edit))))).isTrue() + // Both receivers select the new method, but switching instances changes program behavior. + val receiver = text.indexOf("peer.missing") + val redirect = XdkRename.Edit(receiver, receiver + "peer".length, "this") + assertThat(proves(XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(redirect, edit))))).isFalse() + } + + @Test + fun `inferred local evidence remains detached and repair must bind the exact argument declaration`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + Int read(Int value) { + var first = value; + var other = value + 1; + Int result = missing(first); + return result; + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val failed = embedding.compileModule(Source(text, SOURCE), null, errors) + assertThat(failed.succeeded()).isFalse() + val inputs = failed.renameFacts(XdkDependencies(emptyList()).open(), includeMissingMethods = true).missingMethodInputs + assertThat(inputs.localTypes.values.map { it.source }).containsExactlyInAnyOrder("Int64", "Int64", "Int64") + val declarationErrors = ErrorList() + val analysis = embedding.analyzeDeclarations(Source(text, SOURCE), null, declarationErrors).orElseThrow() + val headers = analysis.memberActionFacts(XdkDependencies(emptyList()).open(), declarationErrors, inputs) + assertThat(declarationErrors.errors).isEmpty() + val candidate = headers.missingMethods.single() + assertThat(candidate.declaration).isEqualTo("private Int64 missing(Int64 arg1)") + assertThat(candidate.arguments).hasSize(1) + val edit = requireNotNull(candidate.edit(text)).member + + fun proves(plan: XdkRename.Plan): Boolean { + val repairedErrors = ErrorList() + val repaired = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, repairedErrors) + assertThat(repaired.succeeded()).describedAs(repairedErrors.errors.toString()).isTrue() + return candidate.bindsNewMethod(repaired.projectRenameFacts(XdkDependencies(emptyList()).open(), repairedErrors), plan, edit) + } + assertThat(proves(XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(edit))))).isTrue() + // This compiles with the same signature, but the argument now denotes a different local. + val argument = text.indexOf("first);") + val redirect = XdkRename.Edit(argument, argument + "first".length, "other") + assertThat(proves(XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(redirect, edit))))).isFalse() + } + + @Test + fun `fresh parameter signature repairs a failed body without changing known bindings`() { + CompilerTestSupport.configure() + val text = + """ + module Missing { + Int keep(Int value) = value; + Int keep(String value) = value.size; + Int read(Int value) { + Int previous = keep(value); + return missing(value); + } + } + """.trimIndent() + val embedding = EmbeddingSupport.instance() + val errors = ErrorList() + val failed = embedding.compileModule(Source(text, SOURCE), null, errors) + assertThat(failed.succeeded()).isFalse() + val partial = failed.renameFacts(XdkDependencies(emptyList()).open()) + val declarationErrors = ErrorList() + val analysis = + embedding + .analyzeDeclarations(Source(text, SOURCE), null, declarationErrors) + .orElseThrow() + + val headers = analysis.memberActionFacts(XdkDependencies(emptyList()).open(), declarationErrors) + assertThat(declarationErrors.errors).isEmpty() + assertThat(headers.missingMethods.map { it.declaration }).containsExactly("private Int64 missing(Int64 arg1)") + val candidate = headers.missingMethods.single() + val edit = requireNotNull(candidate.edit(text)).member + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(edit))) + val compiledErrors = ErrorList() + val compiled = embedding.compileModule(Source(plan.proposed.getValue(SOURCE), SOURCE), null, compiledErrors) + assertThat(compiled.succeeded()).describedAs(compiledErrors.errors.toString()).isTrue() + val after = compiled.projectRenameFacts(XdkDependencies(emptyList()).open(), compiledErrors) + assertThat(candidate.bindsNewMethod(after, plan, edit)).isTrue() + val before = CompilerRenameFacts.merge(mapOf("headers" to headers, "partial" to partial)) + assertThat(XdkRename.preservesKnownBindings(before, after, plan)).isTrue() + + // The graph still compiles if an existing call switches overloads, but that is not a + // repair of the missing method. The old compiler-selected call must remain unchanged. + val call = text.indexOf("keep(value)") + val redirect = XdkRename.Edit(call, call + "keep(value)".length, "keep(\"text\")") + val wrong = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to listOf(redirect, edit))) + val redirectedErrors = ErrorList() + val redirected = embedding.compileModule(Source(wrong.proposed.getValue(SOURCE), SOURCE), null, redirectedErrors) + assertThat(redirected.succeeded()).describedAs(redirectedErrors.errors.toString()).isTrue() + val redirectedFacts = redirected.projectRenameFacts(XdkDependencies(emptyList()).open(), redirectedErrors) + assertThat(XdkRename.preservesKnownBindings(before, redirectedFacts, wrong)).isFalse() + } + + private companion object { + const val SOURCE = "Missing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPoolScopeTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPoolScopeTest.kt new file mode 100644 index 00000000000..23a63a85385 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPoolScopeTest.kt @@ -0,0 +1,102 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.FileStructure +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.lsp.adapter.xdk.semanticSnapshots +import org.xvm.lsp.adapter.xdk.toDependency + +/** Compiler entry points own their scopes; detached LSP facts need no ambient compiler context. */ +class CompilerPoolScopeTest { + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `compilation snapshot and artifact extraction preserve the caller pool`(bound: Boolean) { + CompilerTestSupport.configure() + assertThat(ConstantPool.getCurrentPool()).isNull() + val caller = if (bound) FileStructure("Unrelated").constantPool else null + val source = "module Scoped { Int read(Int value) = value; }" + val errors = ErrorList() + val model = + ConstantPool.withPool(caller).use { + val compilation = + EmbeddingSupport.instance().compileModule(Source(source, URI), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + + val snapshot = compilation.semanticSnapshots(errors).single() + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + assertThat(compilation.toDependency().module).isEqualTo("Scoped") + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + snapshot + } + + assertThat(ConstantPool.getCurrentPool()).isNull() + val use = source.lastIndexOf("value") + assertThat(model.definitionAt(0, use)).isNotNull() + assertThat(model.typeAt(0, use)?.displayName).contains("Int") + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `failed compilation still exports semantic facts and restores the caller pool`(bound: Boolean) { + CompilerTestSupport.configure() + assertThat(ConstantPool.getCurrentPool()).isNull() + val caller = if (bound) FileStructure("Unrelated").constantPool else null + ConstantPool.withPool(caller).use { + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule( + Source("module Broken { Int read() = missing; }", URI), + null, + errors, + ) + assertThat(compilation.succeeded()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + assertThat(compilation.semanticSnapshot().status).isEqualTo(SemanticModel.Status.PARTIAL) + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + } + assertThat(ConstantPool.getCurrentPool()).isNull() + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `partial member inspection binds the attempt pool and restores the caller pool`(bound: Boolean) { + CompilerTestSupport.configure() + assertThat(ConstantPool.getCurrentPool()).isNull() + val caller = if (bound) FileStructure("Unrelated").constantPool else null + val prefix = "module Editing { Int read(String value) { return value." + ConstantPool.withPool(caller).use { + val errors = ErrorList() + val source = Source("$prefix; } }", URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.position = 0 + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(analysis.pool()).describedAs(errors.errors.toString()).isNotNull() + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + val snapshot = analysis.semanticSnapshot(errors) + assertThat( + snapshot.sites + .single() + .members + .map { it.name }, + ).contains("size") + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(ConstantPool.getCurrentPool()).isSameAs(caller) + } + assertThat(ConstantPool.getCurrentPool()).isNull() + } + + private companion object { + const val URI = "file:///Scoped.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerProjectTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerProjectTest.kt new file mode 100644 index 00000000000..6e212ef9152 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerProjectTest.kt @@ -0,0 +1,383 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component.Composition +import org.xvm.asm.ConstantPool +import org.xvm.asm.Constants.Access +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.InvocationExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.lsp.adapter.xdk.semanticSnapshots +import org.xvm.tool.ModuleInfo +import java.io.File +import java.io.IOException +import java.nio.file.Path + +/** API feasibility probes. These do not enable project features in the shipped adapter. */ +class CompilerProjectTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `file and directory convenience entry points use the same module assembly`(useDirectory: Boolean) { + val root = fixture() + CompilerTestSupport.configure() + val errors = ErrorList() + val output = BuildRepository() + val succeeded = + EmbeddingSupport + .instance() + .compile(if (useDirectory) root.parentFile else root, null, output, errors) + assertThat(succeeded).describedAs(errors.errors.toString()).isTrue() + assertThat(output.loadModule("Project").getChild("Child")).isNotNull() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `cancelled tree and file inputs do not parse or invent an error`() { + val root = fixture() + CompilerTestSupport.configure() + val errors = ErrorList() + val listener = ErrorListener.cancellable(errors) { true } + val sources = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray = error("Cancelled source must not be read") + } + val support = EmbeddingSupport.instance() + val result = support.compileModule(sources, null, listener) + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(support.compile(root, null, null, listener)).isFalse() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `member overlays compile together and diagnostics retain the member source`() { + val root = fixture() + val member = directory.resolve("Project/Child.x").toFile() + val disk = member.readText() + val invalid = disk.replace("String answer()", "MissingType missing; String answer()") + val errors = ErrorList() + val failed = compile(root, mapOf(File(member.parentFile, "./Child.x") to invalid), errors) + assertThat(failed.succeeded()).isFalse() + val unresolved = errors.errors.single { it.code == "COMPILER-38" } + val site = unresolved.site() as ErrorListener.Site.In + assertThat(site.source().fileName).isEqualTo(member.path) + assertThat(site.source().toString(site.lPosStart(), site.lPosEnd())) + .isEqualTo("MissingType") + assertThat(member.readText()).isEqualTo(disk) + + val fixed = compile(root, mapOf(member to disk.replace("disk", "overlay"))) + assertThat(fixed.succeeded()).isTrue() + assertThat(nodes(fixed.parsed()).mapNotNull { it.source?.fileName }.toSet()) + .contains(root.path, member.path) + assertThat(member.readText()).isEqualTo(disk) + } + + @Test + fun `copied source views share identities and retain cross file declaration locations`() { + val root = fixture() + val compilation = compile(root) + assertThat(compilation.succeeded()).isTrue() + val views = compilation.semanticSnapshots() + val main = views.single { it.sourceName == root.path } + val member = + views.single { + it.sourceName == directory.resolve("Project/Child.x").toString() + } + val declaration = + member.occurrences.single { + it.name == "answer" && it.role == SemanticModel.Role.DECLARATION + } + val use = main.occurrences.single { it.name == "answer" } + assertThat(use.symbol).isEqualTo(declaration.symbol) + assertThat(main.definitionLocationAt(use.range.start.line, use.range.start.column)) + .isEqualTo(SemanticModel.SourceLocation(member.sourceName, declaration.range)) + assertThat(main.definitionAt(use.range.start.line, use.range.start.column)).isNull() + + val echo = + main.occurrences.single { + it.name == "echo" && it.role == SemanticModel.Role.DECLARATION + } + val references = + views.flatMap { view -> + view.occurrences.filter { it.symbol == echo.symbol }.map { view.sourceName to it } + } + assertThat(references).hasSize(3) + assertThat(references.map { it.first }.toSet()) + .containsExactlyInAnyOrder(main.sourceName, member.sourceName) + assertThat(views.map { it.id }.toSet()).hasSize(1) + assertThatThrownBy { compilation.semanticSnapshot() } + .hasMessageContaining("Use semanticSnapshots()") + + // A later compilation cannot alter earlier copied locations or re-use their IDs. + val next = compile(root).semanticSnapshots() + assertThat(next.map { it.id }.toSet()).doesNotContain(main.id) + assertThat( + main.definitionLocationAt(use.range.start.line, use.range.start.column)?.sourceName, + ).isEqualTo(member.sourceName) + } + + @Test + fun `equal offsets and names in different files remain distinct in semantic views`() { + val root = fixture() + val members = + listOf("Alpha", "Bravo").map { name -> + directory.resolve("Project/$name.x").toFile().apply { + writeText("class $name { Int shared = 1; }") + } + } + val compilation = compile(root) + assertThat(compilation.succeeded()).isTrue() + val views = compilation.semanticSnapshots() + val declarations = + members.map { file -> + views.single { it.sourceName == file.path }.occurrences.single { it.name == "shared" } + } + assertThat(declarations[0].range).isEqualTo(declarations[1].range) + assertThat(declarations[0].symbol).isNotEqualTo(declarations[1].symbol) + } + + @Test + fun `cross file overloads and same named parameters preserve distinct bindings`() { + val root = fixture() + val compilation = compile(root) + assertThat(compilation.succeeded()).isTrue() + val views = compilation.semanticSnapshots() + val main = views.single { it.sourceName == root.path } + val member = views.single { it.sourceName != root.path } + val calls = main.occurrences.filter { it.name == "choose" } + val declarations = member.occurrences.filter { it.name == "choose" } + assertThat(calls).hasSize(2) + assertThat(calls.map { it.symbol }.toSet()).hasSize(2) + assertThat(calls.map { it.symbol }) + .containsExactlyInAnyOrderElementsOf(declarations.map { it.symbol }) + assertThat( + calls.map { call -> + main + .type( + main + .symbol(call.symbol!!)!! + .signature!! + .returns + .single(), + )!! + .displayName + }, + ).containsExactly("String", "Int") + val mainParameters = + main.occurrences + .filter { it.name == "value" } + .map { it.symbol } + .toSet() + val memberParameters = + member.occurrences + .filter { it.name == "value" } + .map { it.symbol } + .toSet() + assertThat(mainParameters).isNotEmpty().doesNotContainAnyElementsOf(memberParameters) + } + + @Test + fun `cross file selected calls resolve to source declarations by compiler identity`() { + val root = fixture() + val compilation = compile(root) + assertThat(compilation.succeeded()).isTrue() + ConstantPool.withPool(compilation.pool()).use { + val nodes = nodes(compilation.parsed()) + val declaration = + nodes.filterIsInstance().single { + it.nameToken.valueText == "answer" + } + val call = + nodes.filterIsInstance().single { + it.resolvedMethod?.name == "answer" + } + assertThat(call.resolvedMethod).isEqualTo(declaration.component.identityConstant) + assertThat(call.source.fileName).isEqualTo(root.path) + assertThat(declaration.source.fileName) + .isEqualTo(directory.resolve("Project/Child.x").toString()) + + val echo = + nodes.filterIsInstance().single { + it.nameToken.valueText == "echo" + } + val references = + nodes.filterIsInstance().filter { + it.resolvedMethod == echo.component.identityConstant + } + assertThat(references.map { it.source.fileName }.toSet()) + .containsExactlyInAnyOrder( + root.path, + directory.resolve("Project/Child.x").toString(), + ) + } + } + + @Test + fun `generic hierarchy and receiver members expose substituted callable types`() { + val compilation = compile(fixture()) + assertThat(compilation.succeeded()).isTrue() + ConstantPool.withPool(compilation.pool()).use { + val child = compilation.module().getChild("Child") as ClassStructure + val base = compilation.module().getChild("Base") as ClassStructure + val parent = + child.contributionsAsList + .single { it.composition == Composition.Extends } + .typeConstant + assertThat(parent.getSingleUnderlyingClass(true)).isEqualTo(base.identityConstant) + assertThat(parent.paramTypes.toList()).containsExactly(compilation.pool().typeString()) + val subtypes = + listOf(base, child).filter { type -> + type.contributionsAsList.any { + it.composition == Composition.Extends && + it.typeConstant.getSingleUnderlyingClass(true) == base.identityConstant + } + } + assertThat(subtypes).containsExactly(child) + + val errors = ErrorList() + val members = child.formalType.ensureAccess(Access.PUBLIC).ensureTypeInfo(errors) + assertThat(members.methods.keys.map { it.name }) + .contains("answer", "echo", "choose") + .doesNotContain("secret") + val echo = members.methods.values.single { it.signature.name == "echo" } + assertThat(echo.signature.params.toList()) + .containsExactly(compilation.pool().typeString()) + assertThat(echo.signature.returns.toList()) + .containsExactly(compilation.pool().typeString()) + val calls = + nodes(compilation.parsed()).filterIsInstance().filter { + it.resolvedMethod?.name == "echo" + } + assertThat(calls).hasSize(2).allSatisfy { call -> + assertThat(call.type).isEqualTo(compilation.pool().typeString()) + val receiver = (call.invokedExpression as NameExpression).leftExpression + if (receiver != null) assertThat(receiver.type).isNotNull() + } + assertThat(errors.errors).isEmpty() + } + } + + @Test + fun `incomplete member overlay is unavailable and does not reuse the last valid tree`() { + val root = fixture() + assertThat(compile(root).succeeded()).isTrue() + val member = directory.resolve("Project/Child.x").toFile() + val errors = ErrorList() + val result = compile(root, mapOf(member to "class Child { void run() { this. } }"), errors) + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(errors.errors).anySatisfy { error -> + assertThat(error.code).startsWith("PARSER-") + assertThat((error.site() as ErrorListener.Site.In).source().fileName) + .isEqualTo(member.path) + } + } + + @Test + fun `source read failure reaches the host`() { + val root = fixture() + val sources = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray = throw IOException("probe read failure") + } + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(sources, null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.errors).anySatisfy { assertThat(it.code).isEqualTo("LAUNCHER-04") } + } + + @Test + fun `new files outside disk discovery are an explicit prototype boundary`() { + val root = fixture() + assertThatThrownBy { + OverlaySources( + root, + mapOf(directory.resolve("Project/New.x").toFile() to "class New {}"), + ) + }.isInstanceOf(UnsupportedOperationException::class.java) + .hasMessageContaining("Overlay-only source discovery") + } + + private fun fixture(): File { + directory = directory.toRealPath() + val root = directory.resolve("Project.x").toFile() + root.writeText( + """ + module Project { + class Base { Element echo(Element value) = value; } + String run() { Child child = new Child(); return child.answer() + child.echo("root") + child.choose("text"); } + Int number() { Child child = new Child(); return child.choose(1); } + } + """.trimIndent(), + ) + val member = directory.resolve("Project/Child.x").toFile() + member.parentFile.mkdirs() + member.writeText( + """ + class Child extends Base { + String answer() = echo("disk"); + String choose(String value) = value; + Int choose(Int value) = value; + private Int secret() = 42; + } + """.trimIndent(), + ) + return root + } + + private fun compile( + root: File, + overlays: Map = emptyMap(), + errors: ErrorList = ErrorList(), + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + return EmbeddingSupport + .instance() + .compileModule(OverlaySources(root, overlays), null, errors) + } + + /** + * Fresh discovery per attempt; source files and resources retain their original disk identity. + */ + private class OverlaySources( + root: File, + overlays: Map, + ) : ModuleInfo(root, false) { + private val text = overlays.mapKeys { it.key.canonicalFile } + + init { + if (text.keys.any { !it.isFile }) { + throw UnsupportedOperationException( + "Overlay-only source discovery is not implemented", + ) + } + } + + override fun readSource(file: File): CharArray = text[file]?.toCharArray() ?: super.readSource(file) + } + + private fun nodes(root: AstNode): List = + buildList { + fun visit(node: AstNode) { + add(node) + node.children().forEachRemaining { visit(it) } + } + visit(root) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRelocationProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRelocationProofTest.kt new file mode 100644 index 00000000000..b128159ef05 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRelocationProofTest.kt @@ -0,0 +1,76 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.CompilerRenameFacts +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkLocalExtraction +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.projectRenameFacts + +/** A compiling relocation can silently capture names; its original edges must be compared. */ +class CompilerRelocationProofTest { + @Test + fun `static qualification changes must preserve the final type even when both programs compile`() { + val text = "module Extract { package tools { class Box {} } package util { class Box {} } tools.Box make() = new tools.Box(); }" + val edits = Regex("tools\\.").findAll(text).map { XdkRename.Edit(it.range.first, it.range.last + 1, "util.") }.toList() + val plan = XdkRename.Plan(mapOf(SOURCE to text), mapOf(SOURCE to edits), qualifications = mapOf(SOURCE to edits)) + assertThat(XdkRename.preservesBindings(facts(text), facts(plan.proposed.getValue(SOURCE)), plan)).isFalse() + } + + @Test + fun `expression relocation preserves its parameter binding only in the original scope`() { + val text = + """ + module Extract { + Int first(Int input) { + return input + 1; + } + Int second(Int input) { + return 0; + } + } + """.trimIndent() + val expression = "input + 1" + val at = text.positionOf("return $expression", expression) + val candidate = requireNotNull(XdkLocalExtraction.candidate(text, Range(at, Position(at.line, at.column + expression.length)))) + val plan = + XdkRename.Plan( + mapOf(SOURCE to text), + mapOf(SOURCE to candidate.edits), + relocations = + mapOf(SOURCE to listOf(candidate.relocation)), + ) + val before = facts(text) + assertThat(XdkRename.preservesKnownBindings(before, facts(plan.proposed.getValue(SOURCE)), plan)).isTrue() + + // Same text, but a different method's identically named parameter: both programs compile. + val start = text.indexOf(expression) + val destination = text.indexOf("return 0;") + val prefix = "Int extractedValue = " + val insertion = XdkRename.Edit(destination, destination, "$prefix$expression;\n ") + val moved = XdkRename.Relocation(start, start + expression.length, insertion, prefix.length) + val captured = + XdkRename.Plan( + mapOf(SOURCE to text), + mapOf(SOURCE to listOf(insertion, XdkRename.Edit(start, start + expression.length, "0"))), + relocations = mapOf(SOURCE to listOf(moved)), + ) + assertThat(XdkRename.preservesKnownBindings(before, facts(captured.proposed.getValue(SOURCE)), captured)).isFalse() + } + + private fun facts(text: String): CompilerRenameFacts { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, SOURCE), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.projectRenameFacts(XdkDependencies(emptyList()).open(), errors) + } + + private companion object { + const val SOURCE = "Extract.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRenameRequirementsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRenameRequirementsTest.kt new file mode 100644 index 00000000000..66c8761db74 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerRenameRequirementsTest.kt @@ -0,0 +1,103 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.semanticSnapshot + +/** Compiler facts required by bounded rename: successful recompilation alone is insufficient. */ +class CompilerRenameRequirementsTest { + @Test + fun `bodyless method parameters have source declaration identities without invented registers`() { + val text = "module Rename { interface Mapper { T map(T value); } }" + val model = compile(text) + val parameter = requireNotNull(model.symbolAt(0, text.indexOf("value"))) + assertThat(parameter.kind).isEqualTo(SemanticModel.SymbolKind.PARAMETER) + assertThat(parameter.declaration?.start?.column).isEqualTo(text.indexOf("value")) + assertThat(parameter.renameable).isFalse() + assertThat(model.occurrences).noneMatch { it.symbol == null } + } + + @Test + fun `identity edits rename captures without touching a shadowing lambda parameter`() { + val source = + "module Rename { Int run() { Int local = 1; function Int() f = () -> local; " + + "function Int(Int) g = (Int local) -> local; return f() + g(2); } }" + val before = compile(source) + val renamed = rename(source, before, source.indexOf("local"), "renamed") + val after = compile(renamed) + assertThat(renamed).contains("Int renamed = 1", "f = () -> renamed", "(Int local) -> local") + assertThat(sourceBindings(after)).isEqualTo(sourceBindings(before)) + } + + @Test + fun `a compiling rename can silently capture an unrelated property reference`() { + val source = + "module Rename { Int value = 10; Int run() { Int local = 1; return local + value; } }" + val before = compile(source) + val renamed = rename(source, before, source.indexOf("local"), "value") + val after = compile(renamed) + assertThat(sourceBindings(after)).isNotEqualTo(sourceBindings(before)) + assertThat(before.symbolAt(0, source.lastIndexOf("value"))!!.kind) + .isEqualTo(SemanticModel.SymbolKind.PROPERTY) + assertThat(after.symbolAt(0, renamed.lastIndexOf("value"))!!.kind) + .isEqualTo(SemanticModel.SymbolKind.VARIABLE) + } + + @Test + fun `named argument labels bind to their selected method parameter before argument rewriting`() { + val source = "module Rename { Int pick(Int input) = input; Int run() = pick(input = 1); }" + val before = compile(source) + val declaration = source.indexOf("input") + assertThat(before.referencesAt(0, declaration, true)).hasSize(3) + assertThat(before.symbolAt(0, source.lastIndexOf("input"))) + .isEqualTo(before.symbolAt(0, declaration)) + val renamed = rename(source, before, declaration, "other") + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(renamed, URI), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.hasSeriousErrors()).isFalse() + } + + private fun compile(text: String): SemanticModel { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, URI), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.semanticSnapshot() + } + + /** + * Single-line fixtures make edit mapping explicit; production rename also needs version checks. + */ + private fun rename( + text: String, + model: SemanticModel, + column: Int, + name: String, + ): String = + model + .referencesAt(0, column, true) + .sortedByDescending { it.start.column } + .fold(text) { edited, range -> + edited.replaceRange(range.start.column, range.end.column, name) + } + + /** + * Compare edges to declaration ordinals, which survive changed spelling and shifted offsets. + */ + private fun sourceBindings(model: SemanticModel): List = + model.occurrences.map { occurrence -> + val target = occurrence.symbol?.let(model::symbol) + model.occurrences.indexOfFirst { + it.role == SemanticModel.Role.DECLARATION && it.range == target?.declaration + } + } + + private companion object { + const val URI = "file:///Rename.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt index 0ac57f383b6..9ef9493ff32 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerTestSupport.kt @@ -3,35 +3,18 @@ package org.xvm.lsp.adapter import org.xvm.api.EmbeddingSupport import org.xvm.api.InterpreterConnector import org.xvm.asm.ConstantPool -import org.xvm.asm.DirRepository import org.xvm.asm.FileStructure import org.xvm.asm.LinkedRepository import org.xvm.asm.ModuleStructure -import org.xvm.compiler.BuildRepository import org.xvm.compiler.InstantRepository +import org.xvm.lsp.adapter.xdk.XdkLibraries import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream -import java.io.File -/** Uses only the compiled-module variants supplied by Gradle, including on a clean checkout. */ +/** Tests use the same bundled module artifacts as the production server. */ internal object CompilerTestSupport { - private val repository by lazy { - val paths = requireNotNull(System.getProperty("xtc.test.modules")) { "Gradle must supply compiler test modules" } - val repositories = - paths.split(File.pathSeparatorChar).map { path -> - val directory = File(path) - check(directory.isDirectory) { "Missing compiled-module directory: $directory" } - DirRepository(directory, true) - } - LinkedRepository(true, BuildRepository(), *repositories.toTypedArray()).also { repository -> - for (name in listOf("ecstasy.xtclang.org", "mack.xtclang.org", "_native.xtclang.org")) { - checkNotNull(repository.loadModule(name)) { "Missing required compiler module: $name" } - } - } - } - fun configure() { - EmbeddingSupport.instance().configure(repository, null) + XdkLibraries.configure() } /** Executes the serialized module's no-argument run method through the interpreter. */ diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompletionTest.kt index 2a3af635a9a..9391346c2a8 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompletionTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Completion tests for [TreeSitterAdapter]. * - * Exercises keyword completions, built-in type completions, and context-aware - * completions derived from document symbols after compilation. + * Exercises keyword completions, built-in type completions, and context-aware completions derived + * from document symbols after compilation. */ @DisplayName("TreeSitterAdapter - Completions") @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -23,8 +23,8 @@ class CompletionTest : TreeSitterTestBase() { @DisplayName("getCompletions()") inner class CompletionTests { /** - * Even without any compiled document, the adapter should offer XTC keywords - * (`class`, `interface`, `module`) from [XtcLanguageConstants]. + * Even without any compiled document, the adapter should offer XTC keywords (`class`, + * `interface`, `module`) from [XtcLanguageConstants]. */ @Test @DisplayName("should return keywords") @@ -53,8 +53,8 @@ class CompletionTest : TreeSitterTestBase() { } /** - * After compiling, the adapter re-queries declarations from the parse tree - * and includes them as completion items. "Person" should appear. + * After compiling, the adapter re-queries declarations from the parse tree and includes + * them as completion items. "Person" should appear. */ @Test @DisplayName("should include document symbols after compile") @@ -75,8 +75,8 @@ class CompletionTest : TreeSitterTestBase() { } /** - * After compiling a class with multiple methods, completions should include - * those method names alongside keywords and built-in types. + * After compiling a class with multiple methods, completions should include those method + * names alongside keywords and built-in types. */ @Test @DisplayName("should include method names from compiled source") @@ -118,8 +118,8 @@ class CompletionTest : TreeSitterTestBase() { @DisplayName("completions at call sites -- tree-sitter-specific") inner class CompletionsAtCallSiteTests { /** - * Completions inside a class body should include all sibling method names - * and the class name itself, since the adapter adds all document symbols. + * Completions inside a class body should include all sibling method names and the class + * name itself, since the adapter adds all document symbols. */ @Test @DisplayName("should include sibling methods in completions") @@ -143,7 +143,8 @@ class CompletionTest : TreeSitterTestBase() { """.trimIndent() ts.compile(uri, source) - val completions = logged("shouldIncludeSiblingMethodsInCompletions", ts.getCompletions(uri, 9, 12)) + val completions = + logged("shouldIncludeSiblingMethodsInCompletions", ts.getCompletions(uri, 9, 12)) logger.info(" completion labels: {}", completions.map { it.label }) assertThat(completions).anyMatch { it.label == "add" } @@ -153,8 +154,8 @@ class CompletionTest : TreeSitterTestBase() { } /** - * After compiling source with multiple classes, completions should include - * all class names and method names from the entire file. + * After compiling source with multiple classes, completions should include all class names + * and method names from the entire file. */ @Test @DisplayName("should include symbols from multiple classes") @@ -177,7 +178,8 @@ class CompletionTest : TreeSitterTestBase() { """.trimIndent() ts.compile(uri, source) - val completions = logged("shouldIncludeSymbolsFromMultipleClasses", ts.getCompletions(uri, 0, 0)) + val completions = + logged("shouldIncludeSymbolsFromMultipleClasses", ts.getCompletions(uri, 0, 0)) logger.info(" completion labels: {}", completions.map { it.label }) assertThat(completions).anyMatch { it.label == "Parser" } @@ -187,7 +189,9 @@ class CompletionTest : TreeSitterTestBase() { } @Test - @DisplayName("should include module-level @Inject property when completing inside a function body") + @DisplayName( + "should include module-level @Inject property when completing inside a function body", + ) fun shouldIncludeModuleLevelInjectPropertyInBodyCompletions() { val uri = freshUri() val source = @@ -216,8 +220,8 @@ class CompletionTest : TreeSitterTestBase() { /** * Function-local variables declared above the cursor must appear in BODY-context - * completions. This complements the @Inject test above and exercises the AST - * scope-walk path (variable_declaration found in the enclosing block). + * completions. This complements the @Inject test above and exercises the AST scope-walk + * path (variable_declaration found in the enclosing block). */ @Test @DisplayName("should include function-local variable in body completions") @@ -244,9 +248,7 @@ class CompletionTest : TreeSitterTestBase() { assertThat(completions).anyMatch { it.label == "greeting" } } - /** - * Method parameters must appear in BODY-context completions inside the method body. - */ + /** Method parameters must appear in BODY-context completions inside the method body. */ @Test @DisplayName("should include method parameters in body completions") fun shouldIncludeMethodParametersInBodyCompletions() { @@ -306,8 +308,8 @@ class CompletionTest : TreeSitterTestBase() { /** * Function-local variables and parameters should rank ahead of generic keywords like - * `return` in the response. The user typed an identifier prefix; the closest-scope - * names are the most likely target. + * `return` in the response. The user typed an identifier prefix; the closest-scope names + * are the most likely target. */ @Test @DisplayName("should rank function-local variable ahead of body keywords") @@ -336,17 +338,16 @@ class CompletionTest : TreeSitterTestBase() { /** * Slightly complex example: a method that mixes - * - a function-local variable (`localVar`) - * - method parameters (`a`, `b`) - * - sibling class members (`classProperty`, `helper`) - * - module-level declarations (`moduleProperty`, `Calculator`) - * - built-in types (`Int`, `String`) - * - body keywords (`return`) + * - a function-local variable (`localVar`) + * - method parameters (`a`, `b`) + * - sibling class members (`classProperty`, `helper`) + * - module-level declarations (`moduleProperty`, `Calculator`) + * - built-in types (`Int`, `String`) + * - body keywords (`return`) * - * The response order should reflect relevance: anything reachable through the AST - * scope walk (local, parameters, class members, module-level) is more relevant than - * built-in types or keywords. Within the scope-walked items, inner scopes outrank - * outer scopes. + * The response order should reflect relevance: anything reachable through the AST scope + * walk (local, parameters, class members, module-level) is more relevant than built-in + * types or keywords. Within the scope-walked items, inner scopes outrank outer scopes. */ @Test @DisplayName("should order completions by scope distance with mixed kinds") @@ -381,18 +382,19 @@ class CompletionTest : TreeSitterTestBase() { val labels = completions.map { it.label } // Every name is reachable. - assertThat(labels).contains( - "localVar", - "a", - "b", - "classProperty", - "helper", - "moduleProperty", - "Calculator", - "Int", - "String", - "return", - ) + assertThat(labels) + .contains( + "localVar", + "a", + "b", + "classProperty", + "helper", + "moduleProperty", + "Calculator", + "Int", + "String", + "return", + ) // Scope-walked items rank ahead of built-in types and keywords. assertThat(labels.indexOf("localVar")).isLessThan(labels.indexOf("Int")) @@ -432,7 +434,8 @@ class CompletionTest : TreeSitterTestBase() { """.trimIndent() ts.compile(uri, source) - val completions = logged("shouldIncludeBodyContextSuggestions", ts.getCompletions(uri, 7, 8)) + val completions = + logged("shouldIncludeBodyContextSuggestions", ts.getCompletions(uri, 7, 8)) logger.info(" completion labels: {}", completions.take(20).map { it.label }) assertThat(completions).anyMatch { it.label == "add" } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/DocumentFormattingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/DocumentFormattingTest.kt index fe22732e4f4..d75581dad0d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/DocumentFormattingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/DocumentFormattingTest.kt @@ -16,8 +16,8 @@ import kotlin.time.measureTimedValue /** * Unit tests for AST-aware document formatting (`textDocument/formatting`) in [TreeSitterAdapter]. * - * Each test provides an XTC source with incorrect indentation and asserts the formatter - * produces the correctly indented version. + * Each test provides an XTC source with incorrect indentation and asserts the formatter produces + * the correctly indented version. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -27,7 +27,8 @@ class DocumentFormattingTest { private var adapter: TreeSitterAdapter? = null private val uriCounter = AtomicInteger(0) - private val ts: TreeSitterAdapter get() = adapter!! + private val ts: TreeSitterAdapter + get() = adapter!! private fun freshUri(): String = "file:///docfmt${uriCounter.incrementAndGet()}.x" @@ -56,9 +57,7 @@ class DocumentFormattingTest { // Test helpers // ======================================================================== - /** - * Compile [input], format the whole document, apply edits, and return the result. - */ + /** Compile [input], format the whole document, apply edits, and return the result. */ private fun formatWhole(input: String): String { val uri = freshUri() ts.compile(uri, input) @@ -85,9 +84,7 @@ class DocumentFormattingTest { return applyEdits(input, edits) } - /** - * Compile [input], format, assert that no edits are returned. - */ + /** Compile [input], format, assert that no edits are returned. */ private fun assertNoEdits(input: String) { val uri = freshUri() ts.compile(uri, input) @@ -96,9 +93,9 @@ class DocumentFormattingTest { } /** - * Compile [input], format, and assert the result equals [expected]. - * Automatically appends `\n` to [expected] if it doesn't already end with one, - * since the formatter always ensures a final newline. + * Compile [input], format, and assert the result equals [expected]. Automatically appends `\n` + * to [expected] if it doesn't already end with one, since the formatter always ensures a final + * newline. */ private fun assertFormatsTo( input: String, @@ -110,8 +107,8 @@ class DocumentFormattingTest { } /** - * Apply text edits to source. Edits are applied in reverse order (bottom-to-top, - * right-to-left) to preserve positions of earlier edits. + * Apply text edits to source. Edits are applied in reverse order (bottom-to-top, right-to-left) + * to preserve positions of earlier edits. */ private fun applyEdits( source: String, @@ -136,7 +133,8 @@ class DocumentFormattingTest { if (startLine == endLine) { // Single-line edit: replace within the line val line = lines[startLine] - lines[startLine] = line.substring(0, startCol) + edit.newText + line.substring(endCol) + lines[startLine] = + line.substring(0, startCol) + edit.newText + line.substring(endCol) } else { // Multi-line edit: splice lines val prefix = lines[startLine].substring(0, startCol) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt index 8b8538ca226..6ee045d7edf 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt @@ -124,6 +124,23 @@ class EmbeddingDiagnosticsTest { assertThat(errors.errors).isEmpty() } + @Test + fun `compound assignment with no operator reports an invalid operation before code generation`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = + Source( + "module MissingOperator { class Counter {} void run() { Counter value = new Counter(); value += 1; } }", + "file:///MissingOperator.x", + ) + val result = EmbeddingSupport.instance().compileModule(source, null, errors) + assertThat(result.succeeded()).isFalse() + assertThat(errors.errors.map { it.code }) + .describedAs(errors.errors.toString()) + .contains("COMPILER-50") + .doesNotContain("COMPILER-69", "EMB-5") + } + @Test fun `unexpected compiler failures are not hidden by an earlier source error`() { CompilerTestSupport.configure() diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FoldingRangeTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FoldingRangeTest.kt index 07e6c1523d3..413a7ba35a9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FoldingRangeTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FoldingRangeTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Folding range tests for [TreeSitterAdapter]. * - * Exercises the detection of foldable code blocks including declarations, - * comments, and nested structures. + * Exercises the detection of foldable code blocks including declarations, comments, and nested + * structures. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -25,9 +25,9 @@ class FoldingRangeTest : TreeSitterTestBase() { @DisplayName("getFoldingRanges()") inner class FoldingRangeTests { /** - * A module containing a class containing a method produces at least 2 multi-line - * foldable blocks (module_declaration + class_declaration; the method_declaration - * may or may not match depending on the grammar's node types). + * A module containing a class containing a method produces at least 2 multi-line foldable + * blocks (module_declaration + class_declaration; the method_declaration may or may not + * match depending on the grammar's node types). */ @Test @DisplayName("should find declaration blocks") @@ -52,9 +52,9 @@ class FoldingRangeTest : TreeSitterTestBase() { } /** - * A multi-line block comment should be foldable. The adapter recognizes - * "comment" and "block_comment" node types. If the grammar uses a different - * node type, this test still passes because we only assert overall count. + * A multi-line block comment should be foldable. The adapter recognizes "comment" and + * "block_comment" node types. If the grammar uses a different node type, this test still + * passes because we only assert overall count. */ @Test @DisplayName("should detect comment blocks") @@ -81,8 +81,8 @@ class FoldingRangeTest : TreeSitterTestBase() { } /** - * Deeply nested declarations (module > class > inner class > method) should - * each produce a foldable range, giving at least 4 ranges. + * Deeply nested declarations (module > class > inner class > method) should each produce a + * foldable range, giving at least 4 ranges. */ @Test @DisplayName("should fold deeply nested declarations") @@ -103,7 +103,11 @@ class FoldingRangeTest : TreeSitterTestBase() { ts.compile(uri, source) val ranges = logged("shouldFoldDeeplyNestedDeclarations", ts.getFoldingRanges(uri)) - logger.info(" folding ranges ({}): {}", ranges.size, ranges.map { "L${it.startLine}-L${it.endLine}" }) + logger.info( + " folding ranges ({}): {}", + ranges.size, + ranges.map { "L${it.startLine}-L${it.endLine}" }, + ) // module + Outer + Inner + method = at least 4 assertThat(ranges).hasSizeGreaterThanOrEqualTo(4) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FormattingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FormattingTest.kt index e0c895b6331..e30947e9993 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FormattingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/FormattingTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Formatting tests for [TreeSitterAdapter]. * - * Exercises whole-document formatting and range-based formatting, including - * trailing whitespace removal and final newline insertion. + * Exercises whole-document formatting and range-based formatting, including trailing whitespace + * removal and final newline insertion. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -25,9 +25,9 @@ class FormattingTest : TreeSitterTestBase() { @DisplayName("formatDocument()") inner class FormatTests { /** - * Lines with trailing spaces ("myapp { ") should produce edits that replace - * the trailing whitespace with empty strings. Two lines have trailing spaces, - * so we expect at least 2 edits. + * Lines with trailing spaces ("myapp { ") should produce edits that replace the trailing + * whitespace with empty strings. Two lines have trailing spaces, so we expect at least 2 + * edits. */ @Test @DisplayName("should remove trailing whitespace") @@ -42,8 +42,8 @@ class FormattingTest : TreeSitterTestBase() { } /** - * When `insertFinalNewline` is true and the source doesn't end with `\n`, - * the formatter should append one via a zero-width insertion at EOF. + * When `insertFinalNewline` is true and the source doesn't end with `\n`, the formatter + * should append one via a zero-width insertion at EOF. */ @Test @DisplayName("should insert final newline") @@ -56,8 +56,8 @@ class FormattingTest : TreeSitterTestBase() { } /** - * A file that already ends with `\n` and has no trailing whitespace is - * "clean" -- the formatter should produce zero edits. + * A file that already ends with `\n` and has no trailing whitespace is "clean" -- the + * formatter should produce zero edits. */ @Test @DisplayName("should return empty for clean file") @@ -86,8 +86,8 @@ class FormattingTest : TreeSitterTestBase() { @DisplayName("formatRange()") inner class FormatRangeTests { /** - * Only lines 1-2 are inside the range, so edits must be confined to those lines. - * Lines 0 and 3 also have trailing whitespace but must be left untouched. + * Only lines 1-2 are inside the range, so edits must be confined to those lines. Lines 0 + * and 3 also have trailing whitespace but must be left untouched. */ @Test @DisplayName("should only format within range") @@ -112,8 +112,8 @@ class FormattingTest : TreeSitterTestBase() { } /** - * `insertFinalNewline` is a whole-document concern. The adapter's `formatContent` - * skips it when a range is provided, so no `\n` edit should appear. + * `insertFinalNewline` is a whole-document concern. The adapter's `formatContent` skips it + * when a range is provided, so no `\n` edit should appear. */ @Test @DisplayName("should not insert final newline for range") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/InlayHintTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/InlayHintTest.kt index ac6904b451c..07172085006 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/InlayHintTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/InlayHintTest.kt @@ -1,62 +1,30 @@ package org.xvm.lsp.adapter import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Disabled import org.junit.jupiter.api.DisplayName -import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test -import org.junit.jupiter.api.TestInstance +import org.xvm.lsp.adapter.treesitter.TreeSitterAdapter /** - * Inlay hint tests for [TreeSitterAdapter]. - * - * Exercises inlay hint generation (currently disabled, pending compiler - * type inference support). - * - * All tests are skipped (not failed) when the tree-sitter native library is unavailable. + * [TreeSitterAdapter] has no inferred-type hints. Compiler hint behavior is covered by + * [XdkPresentationTest] and [XdkInferredPresentationTest], independently of this syntax adapter. */ @DisplayName("TreeSitterAdapter - Inlay Hints") -@TestInstance(TestInstance.Lifecycle.PER_CLASS) class InlayHintTest : TreeSitterTestBase() { - // ======================================================================== - // getInlayHints() -- future - // ======================================================================== - - @Nested - @DisplayName("getInlayHints() -- future") - inner class InlayHintTests { - /** - * TODO: Inlay hints show inferred type annotations inline (e.g., `val x` displays - * `: Int` after the variable). This requires the compiler's type inference engine; - * tree-sitter alone cannot determine types. - */ - @Test - @Disabled("Inlay hints not yet implemented -- requires compiler type inference") - @DisplayName("should return type hints for val declarations") - fun shouldReturnTypeHints() { - val uri = freshUri() - ts.compile( - uri, - """ - module myapp { - class Person { - void test() { - val x = 42; - } - } + @Test + fun `syntax adapter neither advertises nor fabricates inferred type hints`() { + val uri = freshUri() + val source = + """ + module myapp { + void run() { + var count = 42; } - """.trimIndent(), - ) - - val range = - Range( - Position(0, 0), - Position(7, 0), - ) - val hints = ts.getInlayHints(uri, range) + } + """.trimIndent() + ts.compile(uri, source) - // TODO: Once implemented, assert a TYPE hint appears after "val x" with label ": Int" or similar. - assertThat(hints).isNotEmpty - } + assertThat(ts.capabilities).doesNotContain(AdapterCapability.INLAY_HINT) + assertThat(ts.getInlayHints(uri, Range(Position(0, 0), Position(4, 1)))).isEmpty() } } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/LinkedEditingRangeTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/LinkedEditingRangeTest.kt index ace8c4ebbb3..77d833d291c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/LinkedEditingRangeTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/LinkedEditingRangeTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Linked editing range tests for [TreeSitterAdapter]. * - * Exercises same-file identifier linking: when the cursor is on an identifier, - * all same-name occurrences are returned so editors can rename them simultaneously. + * Exercises same-file identifier linking: when the cursor is on an identifier, all same-name + * occurrences are returned so editors can rename them simultaneously. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/ManualCompositionFixture.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/ManualCompositionFixture.kt new file mode 100644 index 00000000000..dcefb123741 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/ManualCompositionFixture.kt @@ -0,0 +1,26 @@ +package org.xvm.lsp.adapter + +import java.nio.file.Path + +/** Reuse a manual compiler program as a standalone module, including its named support types. */ +internal fun manualCompositionFixture( + name: String, + vararg supportTypes: String, +): String { + val root = + Path + .of(requireNotNull(System.getProperty("xtc.composite.root"))) + .resolve("manualTests/src/main/x/jit/jit_tests") + val source = root.resolve("basic/$name.x").toFile().readText() + val support = supportTypes.joinToString("") { "\n" + root.resolve("$it.x").toFile().readText() } + return source.replaceFirst("package $name {", "module $name {$support") +} + +internal fun String.positionOf( + anchor: String, + within: String = anchor, +): Position { + val at = indexOf(anchor).also { require(it >= 0) { "Missing fixture anchor: $anchor" } } + anchor.indexOf(within) + val preceding = take(at) + return Position(preceding.count { it == '\n' }, preceding.substringAfterLast('\n').length) +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/MockAdapterTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/MockAdapterTest.kt index 6404ba1abeb..b11fb396bd2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/MockAdapterTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/MockAdapterTest.kt @@ -34,8 +34,9 @@ class MockAdapterTest { assertThat(result.success).isTrue() assertThat(result.diagnostics).isEmpty() - assertThat(result.symbols) - .anyMatch { it.name == "myapp" && it.kind == SymbolInfo.SymbolKind.MODULE } + assertThat(result.symbols).anyMatch { + it.name == "myapp" && it.kind == SymbolInfo.SymbolKind.MODULE + } } @Test @@ -52,8 +53,9 @@ class MockAdapterTest { val result = adapter.compile("file:///test.x", source) assertThat(result.success).isTrue() - assertThat(result.symbols) - .anyMatch { it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS } + assertThat(result.symbols).anyMatch { + it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS + } } @Test @@ -69,8 +71,9 @@ class MockAdapterTest { val result = adapter.compile("file:///test.x", source) - assertThat(result.symbols) - .anyMatch { it.name == "Runnable" && it.kind == SymbolInfo.SymbolKind.INTERFACE } + assertThat(result.symbols).anyMatch { + it.name == "Runnable" && it.kind == SymbolInfo.SymbolKind.INTERFACE + } } @Test @@ -86,8 +89,9 @@ class MockAdapterTest { val result = adapter.compile("file:///test.x", source) - assertThat(result.symbols) - .anyMatch { it.name == "UserService" && it.kind == SymbolInfo.SymbolKind.SERVICE } + assertThat(result.symbols).anyMatch { + it.name == "UserService" && it.kind == SymbolInfo.SymbolKind.SERVICE + } } @Test @@ -106,8 +110,9 @@ class MockAdapterTest { val result = adapter.compile("file:///test.x", source) - assertThat(result.symbols) - .anyMatch { it.name == "getName" && it.kind == SymbolInfo.SymbolKind.METHOD } + assertThat(result.symbols).anyMatch { + it.name == "getName" && it.kind == SymbolInfo.SymbolKind.METHOD + } } @Test @@ -140,8 +145,7 @@ class MockAdapterTest { val result = adapter.compile("file:///test.x", source) - assertThat(result.diagnostics) - .anyMatch { it.message.contains("Unmatched") } + assertThat(result.diagnostics).anyMatch { it.message.contains("Unmatched") } } } @@ -321,8 +325,7 @@ class MockAdapterTest { adapter.compile("file:///test.x", source) val ranges = adapter.getFoldingRanges("file:///test.x") - assertThat(ranges) - .anyMatch { it.kind == FoldingRange.FoldingKind.IMPORTS } + assertThat(ranges).anyMatch { it.kind == FoldingRange.FoldingKind.IMPORTS } } } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/NavigationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/NavigationTest.kt index 816ac814eee..eb71a9963c1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/NavigationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/NavigationTest.kt @@ -14,8 +14,8 @@ import java.util.stream.Stream /** * Navigation tests for [TreeSitterAdapter]. * - * Exercises definition lookup, references, document highlights, document links, - * cross-file navigation, and workspace symbol search. + * Exercises definition lookup, references, document highlights, document links, cross-file + * navigation, and workspace symbol search. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -30,8 +30,8 @@ class NavigationTest : TreeSitterTestBase() { @DisplayName("findDefinition()") inner class DefinitionTests { /** - * Cursor on "Person" at line 1 should resolve to the class_declaration - * starting at that same line (same-file, name-based match). + * Cursor on "Person" at line 1 should resolve to the class_declaration starting at that + * same line (same-file, name-based match). */ @Test @DisplayName("should find class definition") @@ -63,9 +63,9 @@ class NavigationTest : TreeSitterTestBase() { } /** - * Cmd-click on a call to a doc-commented method must land on the - * method's identifier line, not on the doc-comment opener -- exercises - * the [XtcQueryEngine.findMethodDeclarations] path for call resolution. + * Cmd-click on a call to a doc-commented method must land on the method's identifier line, + * not on the doc-comment opener -- exercises the [XtcQueryEngine.findMethodDeclarations] + * path for call resolution. */ @Test @DisplayName("should find doc-commented method definition on the name line") @@ -121,7 +121,8 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on 'add' in call at line 6, col 12 - val definition = logged("shouldFindMethodDefinitionFromCallSite", ts.findDefinition(uri, 6, 12)) + val definition = + logged("shouldFindMethodDefinitionFromCallSite", ts.findDefinition(uri, 6, 12)) assertThat(definition).isNotNull assertThat(definition!!.startLine).isEqualTo(2) @@ -146,7 +147,8 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on `whitespace` in `whitespace(test)` -- line 6, column 15 - val definition = logged("shouldResolveLocalBeforeClassMember", ts.findDefinition(uri, 6, 15)) + val definition = + logged("shouldResolveLocalBeforeClassMember", ts.findDefinition(uri, 6, 15)) assertThat(definition).isNotNull // The local on line 5 (the function-typed declaration), not the field on line 2 @@ -154,10 +156,10 @@ class NavigationTest : TreeSitterTestBase() { } /** - * Forward references are not legal Ecstasy: a local declared on a later line - * must NOT be returned as the resolution of an identifier above the - * declaration. The scope walk filters `variable_declaration`s by their end - * position relative to the cursor; this test guards that filter. + * Forward references are not legal Ecstasy: a local declared on a later line must NOT be + * returned as the resolution of an identifier above the declaration. The scope walk filters + * `variable_declaration`s by their end position relative to the cursor; this test guards + * that filter. */ @Test @DisplayName("should not resolve to local declared after the cursor") @@ -190,8 +192,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * A local in an inner block must shadow a same-named local in an outer block. - * Cmd-click on the inner reference must return the inner declaration. + * A local in an inner block must shadow a same-named local in an outer block. Cmd-click on + * the inner reference must return the inner declaration. */ @Test @DisplayName("should prefer inner-block local over outer-block local") @@ -221,12 +223,11 @@ class NavigationTest : TreeSitterTestBase() { /** * The scope walk short-circuits when it finds a match in an enclosing scope. If the - * referenced name does NOT exist in any enclosing scope, resolution must fall through - * to the same-file declarations and (if not found there) the workspace index. + * referenced name does NOT exist in any enclosing scope, resolution must fall through to + * the same-file declarations and (if not found there) the workspace index. * - * This is the same-file half of that fall-through. (The cross-file workspace path - * requires an indexed multi-file workspace, which the unit-test harness does not - * easily set up.) + * This is the same-file half of that fall-through. (The cross-file workspace path requires + * an indexed multi-file workspace, which the unit-test harness does not easily set up.) */ @Test @DisplayName("should fall through to same-file declarations when name is not in scope") @@ -258,8 +259,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * Method parameters must resolve to themselves, not to any same-named workspace - * symbol. This is the simpler half of the scope-walk fix. + * Method parameters must resolve to themselves, not to any same-named workspace symbol. + * This is the simpler half of the scope-walk fix. */ @Test @DisplayName("should resolve to method parameter") @@ -284,8 +285,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * When the cursor is on a class name used as a return type in a different - * class, go-to-definition should navigate to the class declaration. + * When the cursor is on a class name used as a return type in a different class, + * go-to-definition should navigate to the class declaration. */ @Test @DisplayName("should find class definition from type usage") @@ -307,7 +308,8 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on 'Person' in return type at line 5, col 8 - val definition = logged("shouldFindClassDefinitionFromTypeUsage", ts.findDefinition(uri, 5, 8)) + val definition = + logged("shouldFindClassDefinitionFromTypeUsage", ts.findDefinition(uri, 5, 8)) assertThat(definition).isNotNull assertThat(definition!!.startLine).isEqualTo(1) @@ -315,11 +317,10 @@ class NavigationTest : TreeSitterTestBase() { /** * Issue #459: `structure.y` in reflect.x navigated to an unrelated `y` in - * doc/archive/examples because `y`, declared only as a shorthand constructor - * parameter of `const Point(Int x, Int y)`, was invisible to the same-file - * declaration lookup and fell through to the workspace-wide name grep. - * Shorthand constructor parameters declare properties, so the same-file - * lookup must find them. + * doc/archive/examples because `y`, declared only as a shorthand constructor parameter of + * `const Point(Int x, Int y)`, was invisible to the same-file declaration lookup and fell + * through to the workspace-wide name grep. Shorthand constructor parameters declare + * properties, so the same-file lookup must find them. */ @Test @DisplayName("should resolve const shorthand property in same file") @@ -337,7 +338,11 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on `y` in `structure.y` -- line 3, col 18 - val definition = logged("shouldResolveConstShorthandPropertyInSameFile", ts.findDefinition(uri, 3, 18)) + val definition = + logged( + "shouldResolveConstShorthandPropertyInSameFile", + ts.findDefinition(uri, 3, 18), + ) assertThat(definition).isNotNull assertThat(definition!!.uri).isEqualTo(uri) @@ -346,9 +351,9 @@ class NavigationTest : TreeSitterTestBase() { } /** - * Issue #459: with the whole word selected, the editor reports the caret at - * the exclusive end of the identifier, where nodeAt() returns the following - * token. The definition lookup must tolerate that by retrying one column left. + * Issue #459: with the whole word selected, the editor reports the caret at the exclusive + * end of the identifier, where nodeAt() returns the following token. The definition lookup + * must tolerate that by retrying one column left. */ @Test @DisplayName("should find definition when caret is at the end of the word") @@ -374,8 +379,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * Issue #459: cmd-click on a typedef name found nothing. Same-file case: - * the scope walk must treat a typedef_declaration as a body member. + * Issue #459: cmd-click on a typedef name found nothing. Same-file case: the scope walk + * must treat a typedef_declaration as a body member. */ @Test @DisplayName("should navigate to typedef declaration in same file") @@ -393,7 +398,8 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on `Name` usage -- line 3, col 8 - val definition = logged("shouldNavigateToTypedefInSameFile", ts.findDefinition(uri, 3, 8)) + val definition = + logged("shouldNavigateToTypedefInSameFile", ts.findDefinition(uri, 3, 8)) assertThat(definition).isNotNull assertThat(definition!!.startLine).isEqualTo(1) @@ -408,10 +414,9 @@ class NavigationTest : TreeSitterTestBase() { @DisplayName("findReferences()") inner class FindReferencesTests { /** - * `includeDeclaration = false` should drop exactly one location: the declaration - * itself. With both flag values exercised on the same source, the `false` result - * must contain every identifier-token from the `true` result minus the - * declaration site. + * `includeDeclaration = false` should drop exactly one location: the declaration itself. + * With both flag values exercised on the same source, the `false` result must contain every + * identifier-token from the `true` result minus the declaration site. */ @Test @DisplayName("should exclude declaration when includeDeclaration is false") @@ -451,8 +456,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * A method referenced in multiple call sites should produce locations - * for the declaration and each call. + * A method referenced in multiple call sites should produce locations for the declaration + * and each call. */ @Test @DisplayName("should find method references across declaration and calls") @@ -480,7 +485,11 @@ class NavigationTest : TreeSitterTestBase() { "shouldFindMethodReferencesAcrossDeclarationAndCalls", ts.findReferences(uri, 2, 12, includeDeclaration = true), ) - logger.info(" references ({}): {}", refs.size, refs.map { "L${it.startLine}:${it.startColumn}" }) + logger.info( + " references ({}): {}", + refs.size, + refs.map { "L${it.startLine}:${it.startColumn}" }, + ) // declaration + 2 call sites = at least 3 assertThat(refs).hasSizeGreaterThanOrEqualTo(3) @@ -495,9 +504,9 @@ class NavigationTest : TreeSitterTestBase() { @DisplayName("getDocumentHighlights()") inner class DocumentHighlightTests { /** - * "Person" appears as the class name, a return type, and in a `new` expression. - * The adapter finds all identifier nodes with the same text, so we expect at - * least 2 highlights (declaration + usages). + * "Person" appears as the class name, a return type, and in a `new` expression. The adapter + * finds all identifier nodes with the same text, so we expect at least 2 highlights + * (declaration + usages). */ @Test @DisplayName("should highlight all occurrences of identifier") @@ -531,8 +540,8 @@ class NavigationTest : TreeSitterTestBase() { } /** - * A method name used in both declaration and multiple call sites should - * produce highlights at all locations. + * A method name used in both declaration and multiple call sites should produce highlights + * at all locations. */ @Test @DisplayName("should highlight method name at declaration and call sites") @@ -555,8 +564,16 @@ class NavigationTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on 'add' at declaration, line 2, col 12 - val highlights = logged("shouldHighlightMethodAtDeclarationAndCalls", ts.getDocumentHighlights(uri, 2, 12)) - logger.info(" highlights ({}): {}", highlights.size, highlights.map { "L${it.range.start.line}:${it.range.start.column}" }) + val highlights = + logged( + "shouldHighlightMethodAtDeclarationAndCalls", + ts.getDocumentHighlights(uri, 2, 12), + ) + logger.info( + " highlights ({}): {}", + highlights.size, + highlights.map { "L${it.range.start.line}:${it.range.start.column}" }, + ) // declaration + 2 call sites = at least 3 assertThat(highlights).hasSizeGreaterThanOrEqualTo(3) @@ -572,9 +589,9 @@ class NavigationTest : TreeSitterTestBase() { @TestInstance(TestInstance.Lifecycle.PER_CLASS) inner class DocumentLinkTests { /** - * One row per syntactic position the URL detector must recognise (line/block/doc - * comment, string literal, trailing-punctuation strip). Assertions are uniform; a - * new comment- or string-node type costs one extra row instead of a new method. + * One row per syntactic position the URL detector must recognise (line/block/doc comment, + * string literal, trailing-punctuation strip). Assertions are uniform; a new comment- or + * string-node type costs one extra row instead of a new method. */ @Suppress("unused") fun singleUrlCases(): Stream = @@ -662,7 +679,8 @@ class NavigationTest : TreeSitterTestBase() { assertThat(links[0].range.start.column).isEqualTo(expectedColumn) // The detected range must cover exactly the (post-strip) URL -- catches mistakes // like the trailing-punctuation case where the dot must NOT be in the range. - assertThat(links[0].range.end.column - links[0].range.start.column).isEqualTo(expectedTarget.length) + assertThat(links[0].range.end.column - links[0].range.start.column) + .isEqualTo(expectedTarget.length) } @Test @@ -680,10 +698,12 @@ class NavigationTest : TreeSitterTestBase() { val links = ts.getDocumentLinks(uri, source) - assertThat(links).extracting({ it.target }).containsExactlyInAnyOrder( - "https://a.test", - "https://b.test", - ) + assertThat(links) + .extracting({ it.target }) + .containsExactlyInAnyOrder( + "https://a.test", + "https://b.test", + ) } @Test @@ -802,7 +822,8 @@ class NavigationTest : TreeSitterTestBase() { // Since initializeWorkspace scans files on disk and our test URIs are synthetic, // the index gets populated via compile() -> reindexFile() once the index is ready. // To test findWorkspaceSymbols, we trigger indexing manually by calling it: - // After compile, the indexReady flag is still false (no initializeWorkspace was called). + // After compile, the indexReady flag is still false (no initializeWorkspace was + // called). // We test via initializeWorkspace with a temp dir containing the same files. val results = ts.findWorkspaceSymbols("Person") // Without initializeWorkspace called, results may be empty (index not ready) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/OnTypeFormattingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/OnTypeFormattingTest.kt index 858610ad5d3..e35f252efc1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/OnTypeFormattingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/OnTypeFormattingTest.kt @@ -16,9 +16,8 @@ import java.util.concurrent.atomic.AtomicInteger * Unit tests for on-type formatting (`textDocument/onTypeFormatting`) in [TreeSitterAdapter]. * * Tests parse XTC snippets via the tree-sitter native parser, then call - * [TreeSitterAdapter.onTypeFormatting] with a trigger character and cursor position, - * asserting that the returned [TextEdit] list produces the correct - * indentation. + * [TreeSitterAdapter.onTypeFormatting] with a trigger character and cursor position, asserting that + * the returned [TextEdit] list produces the correct indentation. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -28,7 +27,8 @@ class OnTypeFormattingTest { private var adapter: TreeSitterAdapter? = null private val uriCounter = AtomicInteger(0) - private val ts: TreeSitterAdapter get() = adapter!! + private val ts: TreeSitterAdapter + get() = adapter!! private fun freshUri(): String = "file:///fmt${uriCounter.incrementAndGet()}.x" @@ -54,8 +54,8 @@ class OnTypeFormattingTest { } /** - * Helper: compile source, then call onTypeFormatting. - * Returns the desired indent (new text length) or -1 if no edit was returned. + * Helper: compile source, then call onTypeFormatting. Returns the desired indent (new text + * length) or -1 if no edit was returned. */ private fun formatAfterTrigger( source: String, @@ -77,9 +77,7 @@ class OnTypeFormattingTest { } } - /** - * Helper: compile source, call onTypeFormatting, return the raw edit list. - */ + /** Helper: compile source, call onTypeFormatting, return the raw edit list. */ private fun formatEdits( source: String, line: Int, @@ -139,7 +137,9 @@ class OnTypeFormattingTest { } @Test - @DisplayName("auto-closed method brace skeleton keeps blank body line and aligned closing brace") + @DisplayName( + "auto-closed method brace skeleton keeps blank body line and aligned closing brace", + ) fun autoClosedMethodBraceSkeleton() { val source = """ @@ -498,7 +498,8 @@ class OnTypeFormattingTest { tabSize = 2, insertSpaces = true, ) - val edits = ts.onTypeFormatting(uri, line = 1, column = 0, ch = "\n", options = customOptions) + val edits = + ts.onTypeFormatting(uri, line = 1, column = 0, ch = "\n", options = customOptions) assertThat(edits).isNotEmpty assertThat( edits diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/PrepareLibraryPlaybook.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/PrepareLibraryPlaybook.kt new file mode 100644 index 00000000000..8cc8da19f15 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/PrepareLibraryPlaybook.kt @@ -0,0 +1,38 @@ +package org.xvm.lsp.adapter + +import ch.qos.logback.classic.LoggerContext +import com.google.gson.JsonParser +import org.slf4j.LoggerFactory +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Files +import java.nio.file.Path + +/** Builds the shared binary fixture from source; no checked-in compiler artifacts or external XDK. */ +object PrepareLibraryPlaybook { + @JvmStatic + fun main(args: Array) { + check( + LoggerFactory.getILoggerFactory() is LoggerContext, + ) { "Playbook fixture logging must use Logback, not javatools' no-op provider" } + val fixture = + JsonParser + .parseString(Files.readString(Path.of(args[0]))) + .asJsonObject + .getAsJsonObject("common") + .getAsJsonObject("librarySettings") + val output = Path.of(args[1]) + CompilerTestSupport.configure() + listOf("one" to fixture["source"].asString, "two" to fixture["replacementSource"].asString).forEach { (name, text) -> + val errors = ErrorList() + val compilation = EmbeddingSupport.instance().compileModule(Source(text, fixture["sourceFile"].asString), null, errors) + check(compilation.succeeded()) { errors.errors.toString() } + val folder = Files.createDirectories(output.resolve(name)) + Files.write(folder.resolve(fixture["binaryFile"].asString), compilation.toDependency().bytes()) + } + val sources = Files.createDirectories(output.resolve("sources")) + Files.writeString(sources.resolve(fixture["sourceFile"].asString), fixture["source"].asString) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RenameTest.kt index 720a0fa7bf6..58f24825821 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RenameTest.kt @@ -9,8 +9,7 @@ import org.junit.jupiter.api.TestInstance /** * Rename tests for [TreeSitterAdapter]. * - * Exercises prepare-rename and rename-all-occurrences via AST-based - * identifier matching. + * Exercises prepare-rename and rename-all-occurrences via AST-based identifier matching. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -25,8 +24,8 @@ class RenameTest : TreeSitterTestBase() { @DisplayName("rename()") inner class RenameTests { /** - * `prepareRename` finds the identifier AST node at the cursor and returns - * its text as the placeholder. Cursor on "Person" should yield exactly that. + * `prepareRename` finds the identifier AST node at the cursor and returns its text as the + * placeholder. Cursor on "Person" should yield exactly that. */ @Test @DisplayName("should prepare rename for identifier") @@ -48,8 +47,8 @@ class RenameTest : TreeSitterTestBase() { } /** - * Renaming "Person" to "Human" should produce edits for every identifier node - * with text "Person" in the file -- at least the declaration and usage sites. + * Renaming "Person" to "Human" should produce edits for every identifier node with text + * "Person" in the file -- at least the declaration and usage sites. */ @Test @DisplayName("should rename all occurrences") @@ -87,12 +86,11 @@ class RenameTest : TreeSitterTestBase() { } /** - * Issue #459: renaming a local variable renamed ALL same-named variables and - * properties in the file. Rename must be scope-aware: only occurrences that - * resolve to the same declaration as the cursor's identifier get edited. - * Here `count` exists as a module property, as a local in run(), and as a - * local in other() -- renaming the local in run() must leave the property - * and other()'s local untouched. + * Issue #459: renaming a local variable renamed ALL same-named variables and properties in + * the file. Rename must be scope-aware: only occurrences that resolve to the same + * declaration as the cursor's identifier get edited. Here `count` exists as a module + * property, as a local in run(), and as a local in other() -- renaming the local in run() + * must leave the property and other()'s local untouched. */ @Test @DisplayName("should rename only the scoped local variable") @@ -127,8 +125,8 @@ class RenameTest : TreeSitterTestBase() { } /** - * Renaming an outer variable must not touch an inner declaration that - * shadows it (nor the shadowed uses, which resolve to the inner one). + * Renaming an outer variable must not touch an inner declaration that shadows it (nor the + * shadowed uses, which resolve to the inner one). */ @Test @DisplayName("should not rename a shadowing inner declaration") @@ -161,8 +159,8 @@ class RenameTest : TreeSitterTestBase() { } /** - * Issue #459: with the whole identifier selected, the caret sits at the - * exclusive end of the word. prepareRename must still find the identifier. + * Issue #459: with the whole identifier selected, the caret sits at the exclusive end of + * the word. prepareRename must still find the identifier. */ @Test @DisplayName("should prepare rename when caret is at the end of the word") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SelectionRangeTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SelectionRangeTest.kt index 7191b8302bf..3c523888b06 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SelectionRangeTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SelectionRangeTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Selection range tests for [TreeSitterAdapter]. * - * Exercises the tree-sitter-specific AST-based selection range expansion, - * including nested call site scenarios. + * Exercises the tree-sitter-specific AST-based selection range expansion, including nested call + * site scenarios. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -25,9 +25,9 @@ class SelectionRangeTest : TreeSitterTestBase() { @DisplayName("getSelectionRanges() -- tree-sitter-specific") inner class SelectionRangeTests { /** - * From a leaf identifier ("name" inside a return statement), the adapter walks - * the AST parent chain, deduplicating nodes with identical ranges. The result - * should have depth >= 3 (e.g., identifier -> expression -> block -> declaration). + * From a leaf identifier ("name" inside a return statement), the adapter walks the AST + * parent chain, deduplicating nodes with identical ranges. The result should have depth >= + * 3 (e.g., identifier -> expression -> block -> declaration). */ @Test @DisplayName("should produce nested chain from identifier to root") @@ -52,9 +52,9 @@ class SelectionRangeTest : TreeSitterTestBase() { } /** - * Each parent range must strictly contain (or equal) its child range -- the - * selection never shrinks as you walk outward. We linearize positions as - * `line * 10000 + column` for a simple numeric comparison. + * Each parent range must strictly contain (or equal) its child range -- the selection never + * shrinks as you walk outward. We linearize positions as `line * 10000 + column` for a + * simple numeric comparison. */ @Test @DisplayName("should produce widening chain where each parent contains child") @@ -90,8 +90,8 @@ class SelectionRangeTest : TreeSitterTestBase() { } /** - * At least one range in the chain should span more than zero characters, - * proving the selection is meaningful (not just point ranges everywhere). + * At least one range in the chain should span more than zero characters, proving the + * selection is meaningful (not just point ranges everywhere). */ @Test @DisplayName("should produce at least one range wider than a single point") @@ -114,9 +114,10 @@ class SelectionRangeTest : TreeSitterTestBase() { ).single() val hasNonPointRange = - generateSequence(selection) { it.parent }.any { sel -> - linearize(sel.range.start) != linearize(sel.range.end) - } + generateSequence(selection) { it.parent } + .any { sel -> + linearize(sel.range.start) != linearize(sel.range.end) + } assertThat(hasNonPointRange).isTrue() } @@ -131,9 +132,9 @@ class SelectionRangeTest : TreeSitterTestBase() { @DisplayName("selection ranges at call sites -- tree-sitter-specific") inner class SelectionRangesAtCallSiteTests { /** - * Starting from an argument literal (`1` in `add(1, 2)`), the selection - * range chain should walk outward through argument list, call expression, - * statement, block, method, class, module -- at least 4 levels deep. + * Starting from an argument literal (`1` in `add(1, 2)`), the selection range chain should + * walk outward through argument list, call expression, statement, block, method, class, + * module -- at least 4 levels deep. */ @Test @DisplayName("should walk outward from call argument") @@ -165,8 +166,8 @@ class SelectionRangeTest : TreeSitterTestBase() { } /** - * Starting from a nested call argument (`1` in `negate(1)` inside - * `add(negate(1), 2)`), the chain should be even deeper -- at least 5 levels. + * Starting from a nested call argument (`1` in `negate(1)` inside `add(negate(1), 2)`), the + * chain should be even deeper -- at least 5 levels. */ @Test @DisplayName("should walk outward from nested call argument") @@ -201,8 +202,8 @@ class SelectionRangeTest : TreeSitterTestBase() { } /** - * Multiple cursor positions in a single request should each produce an - * independent selection range chain. + * Multiple cursor positions in a single request should each produce an independent + * selection range chain. */ @Test @DisplayName("should handle multiple positions independently") @@ -229,7 +230,10 @@ class SelectionRangeTest : TreeSitterTestBase() { Position(2, 12), // 'add' in declaration ) val selections = ts.getSelectionRanges(uri, positions) - logger.info("[TEST] shouldHandleMultiplePositionsIndependently -> {} selections", selections.size) + logger.info( + "[TEST] shouldHandleMultiplePositionsIndependently -> {} selections", + selections.size, + ) selections.forEachIndexed { i, sel -> logSelectionChain("shouldHandleMultiplePositionsIndependently[$i]", sel) } @@ -258,7 +262,15 @@ class SelectionRangeTest : TreeSitterTestBase() { val chain = generateSequence(sel) { it.parent }.toList() chain.forEachIndexed { i, s -> val r = s.range - logger.info(" [{}] level {} -> L{}:{}-L{}:{}", test, i, r.start.line, r.start.column, r.end.line, r.end.column) + logger.info( + " [{}] level {} -> L{}:{}-L{}:{}", + test, + i, + r.start.line, + r.start.column, + r.end.line, + r.end.column, + ) } } } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticModelTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticModelTest.kt new file mode 100644 index 00000000000..3fb129501b5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticModelTest.kt @@ -0,0 +1,390 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.NewExpression +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import java.util.concurrent.Executors + +/** Consumer contracts: all semantic answers survive independently of the compiler and its pool. */ +class SemanticModelTest { + @ParameterizedTest + @ValueSource(strings = ["plain", "😀"]) + fun `folded property initializers retain source identities and expression types`(marker: String) { + val source = + "module Folded { String marker = \"$marker\"; Int /*declaration*/value = 1; " + + "Int copy = /*use*/value; Int sum = /*sum*/value + 2; }" + val compilation = compile(source) + val model = compilation.semanticSnapshot() + val declared = occurrence(model, source, "declaration") + listOf("use", "sum").forEach { marker -> + val reference = occurrence(model, source, marker) + assertThat(reference.symbol).isEqualTo(declared.symbol) + assertThat(reference.type).isEqualTo(declared.type) + assertThat(reference.usage).isEqualTo(SemanticModel.Usage.READ) + } + assertThat(compilation.initializerBindings().values.flatMap { it.references() }).anyMatch { + it.name() == "value" + } + assertThatThrownBy { (compilation.initializerBindings() as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @Test + fun `failed initializer probes do not publish clone facts and real methods retain source facts`() { + val rejected = compile("module Failed { Int copy = absent; }", succeeds = false) + assertThat(rejected.initializerBindings()).isEmpty() + val source = + "module Runtime { Int /*declaration*/value = make(); Int copy = /*use*/value; static Int make() = 1; }" + val model = compile(source).semanticSnapshot() + assertThat(occurrence(model, source, "use").symbol) + .isEqualTo(occurrence(model, source, "declaration").symbol) + } + + @Test + fun `class and method type parameters point to their written declarations`() { + val source = + """ + module Formals { + class Box { + /*classUse*/Element echo(Element value) = value; + } + interface Mapper { /*abstractUse*/Item map(Item value); } + /*methodUse*/Value identity(Value value) { + Type type = /*valueUse*/Value; + return value; + } + } + """.trimIndent() + val model = compile(source).semanticSnapshot() + for ( + (declaration, uses) in + listOf( + "classDecl" to listOf("classUse"), + "methodDecl" to listOf("methodUse", "valueUse"), + "abstractDecl" to listOf("abstractUse"), + ) + ) { + val declared = occurrence(model, source, declaration) + assertThat(model.symbol(declared.symbol!!)!!.kind) + .isEqualTo(SemanticModel.SymbolKind.TYPE_PARAMETER) + uses.forEach { + assertThat(occurrence(model, source, it).symbol).isEqualTo(declared.symbol) + } + } + } + + @Test + fun `anonymous class capture points to the enclosing parameter`() { + val source = + """ + module Anonymous { + String run(String /*declaration*/text) { + Object value = new Object() { + @Override String toString() = /*capture*/text; + }; + return value.toString(); + } + } + """.trimIndent() + val model = compile(source).semanticSnapshot() + val declared = occurrence(model, source, "declaration") + val captured = occurrence(model, source, "capture") + assertThat(captured.symbol).isEqualTo(declared.symbol) + assertThat(model.symbol(captured.symbol!!)!!.declaration).isEqualTo(declared.range) + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `nested and mutable anonymous captures retain source identity`(mutable: Boolean) { + val declaration = + if (mutable) { + "@Volatile Int /*declaration*/value = 1;" + } else { + "Int /*declaration*/value = 1;" + } + val body = + if (mutable) { + "return (++/*capture*/value).toString();" + } else { + "function String() f = () -> /*capture*/value.toString(); return f();" + } + val source = + """ + module AnonymousCaptures { + String run() { + $declaration + Object object = new Object() { + @Override String toString() { $body } + String shadow(Int /*shadowDecl*/value) = /*shadowUse*/value.toString(); + }; + return object.toString(); + } + } + """.trimIndent() + val compilation = compile(source) + val model = compilation.semanticSnapshot() + assertThat(occurrence(model, source, "capture").symbol) + .isEqualTo(occurrence(model, source, "declaration").symbol) + assertThat(occurrence(model, source, "shadowUse").symbol) + .isEqualTo(occurrence(model, source, "shadowDecl").symbol) + assertThat(occurrence(model, source, "shadowUse").symbol) + .isNotEqualTo(occurrence(model, source, "declaration").symbol) + + fun nodes(node: AstNode): Sequence = + sequence { + yield(node) + for (child in node.children()) yieldAll(nodes(child)) + } + val expression = nodes(compilation.parsed()).filterIsInstance().single() + val bindings = requireNotNull(expression.sourceBindings) + val copy = expression.clone() as NewExpression + assertThat(copy.sourceBindings).isNull() + assertThat(copy.captureOrigins).isEmpty() + assertThat(expression.sourceBindings).isSameAs(bindings) + assertThat(bindings.captureOrigins).isNotEmpty() + } + + @Test + fun `overloads aliases and nested generic types keep distinct bindings`() { + val source = + """ + module Bindings { + typedef List> as /*aliasDecl*/Names; + Int /*intDecl*/choose(Int value) = value; + String /*stringDecl*/choose(String value) = value; + void run(/*aliasUse*/Names names) { + Int number = /*intUse*/choose(1); + String text = /*stringUse*/choose("ok"); + } + } + """.trimIndent() + val model = compile(source).semanticSnapshot() + for ( + (declaration, use) in + listOf("aliasDecl" to "aliasUse", "intDecl" to "intUse", "stringDecl" to "stringUse") + ) { + assertThat(occurrence(model, source, use).symbol) + .isEqualTo(occurrence(model, source, declaration).symbol) + } + assertThat(occurrence(model, source, "intUse").symbol) + .isNotEqualTo(occurrence(model, source, "stringUse").symbol) + } + + @Test + fun `declared and narrowed types share a symbol while preserving each expression type`() { + val source = + """ + module Narrowed { + String text(Object /*declaration*/value) { + if (/*test*/value.is(String)) { return /*narrowed*/value; } + return /*original*/value.toString(); + } + } + """.trimIndent() + val model = compile(source).semanticSnapshot() + val declaration = occurrence(model, source, "declaration") + val narrowed = occurrence(model, source, "narrowed") + assertThat(model.status).isEqualTo(SemanticModel.Status.COMPLETE) + assertThat(model.sourceName).isEqualTo(URI) + assertThat(narrowed.symbol).isEqualTo(declaration.symbol) + assertThat(model.type(declaration.type!!)!!.displayName).contains("Object") + assertThat(model.type(narrowed.type!!)!!.displayName).contains("String") + assertThat(occurrence(model, source, "original").symbol).isEqualTo(declaration.symbol) + } + + @Test + fun `structured types and callable signatures retain their relationships`() { + val source = + """ + module Signatures { + List /*method*/copy(List /*parameter*/values, Int count = 1) { + return values; + } + } + """.trimIndent() + val model = compile(source).semanticSnapshot() + val method = model.symbol(occurrence(model, source, "method").symbol!!)!! + val signature = method.signature!! + assertThat(signature.parameters.map { it.name!! }).containsExactly("values", "count") + assertThat(signature.parameters.last().defaulted).isTrue() + assertThat(signature.conditional).isFalse() + val list = model.type(signature.parameters.first().type)!! + assertThat(list.form).isEqualTo(SemanticModel.TypeForm.PARAMETERIZED) + assertThat(signature.returns).containsExactly(list.id) + val element = model.type(list.arguments.single())!! + assertThat(element.nullable).isTrue() + assertThat(element.form).isEqualTo(SemanticModel.TypeForm.UNION) + assertThat(element.underlying).hasSize(2) + assertThat(model.types).allSatisfy { type -> + assertThat(type.arguments + type.underlying).allSatisfy { + assertThat(model.type(it)).isNotNull() + } + } + } + + @Test + fun `failed validation retains resolved facts and explicitly unresolved occurrences`() { + val source = + """ + module Partial { + Int /*good*/valid(Int value) { return value; } + Int broken() { return /*missing*/missing; } + } + """.trimIndent() + val model = compile(source, succeeds = false).semanticSnapshot() + assertThat(model.status).isEqualTo(SemanticModel.Status.PARTIAL) + assertThat(occurrence(model, source, "good").symbol).isNotNull() + val missing = occurrence(model, source, "missing") + assertThat(missing.symbol).isNull() + assertThat(missing.type).isNull() + val at = position(source, "missing") + assertThat(model.definitionAt(at.line, at.column)).isNull() + assertThat(model.implementationLocationsAt(at.line, at.column)).isEmpty() + assertThat(model.implementationLocationsAt(0, 0)).isEmpty() + assertThat(model.referencesAt(at.line, at.column, true)).isEmpty() + assertThat(model.typeAt(at.line, at.column)).isNull() + } + + @Test + fun `a resolved qualified prefix survives an unresolved final segment`() { + val source = + "module PartialType { class Outer {} void use(/*prefix*/Outer. /*missing*/Absent value) {} }" + val model = compile(source, succeeds = false).semanticSnapshot() + assertThat(occurrence(model, source, "prefix").symbol).isNotNull() + assertThat(occurrence(model, source, "missing").symbol).isNull() + } + + @Test + fun `cancellation before parsing yields an unavailable empty snapshot`() { + CompilerTestSupport.configure() + val compilation = + EmbeddingSupport + .instance() + .compileModule( + Source("module Cancelled {}", URI), + null, + ErrorListener.cancellable(ErrorList()) { true }, + ) + val model = compilation.semanticSnapshot() + assertThat(model.status).isEqualTo(SemanticModel.Status.UNAVAILABLE) + assertThat(model.symbols).isEmpty() + assertThat(model.occurrences).isEmpty() + assertThat(model.types).isEmpty() + } + + @Test + fun `snapshot identities and nested collections cannot be reused or changed by a host`() { + val compilation = + compile("module Immutable { Int value = 1; Int read() { return value; } }") + val first = compilation.semanticSnapshot() + val second = compilation.semanticSnapshot() + assertThat(second.id).isNotEqualTo(first.id) + assertThat(second.symbol(first.symbols.first().id)).isNull() + assertThat(second.type(first.types.first().id)).isNull() + assertThatThrownBy { (first.symbols as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + assertThatThrownBy { (first.occurrences as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + val signature = requireNotNull(first.symbols.first { it.signature != null }.signature) + assertThatThrownBy { (first.types.first().underlying as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @Test + fun `concurrent queries need no ambient pool and remain valid after another compilation`() { + val source = + "module Concurrent { Int read(Int /*declaration*/value) { return /*use*/value; } }" + val model = compile(source).semanticSnapshot() + val at = position(source, "use") + val expected = occurrence(model, source, "declaration").range + Executors.newFixedThreadPool(4).use { readers -> + val queries = + (1..32).map { + readers.submit { + assertThat(ConstantPool.getCurrentPool()).isNull() + repeat(25) { + assertThat(model.definitionAt(at.line, at.column)).isEqualTo(expected) + assertThat(model.typeAt(at.line, at.column)!!.displayName) + .contains("Int") + assertThat(model.referencesAt(at.line, at.column, true)).hasSize(2) + } + } + } + compile("module Concurrent { String read(String value) { return value; } }") + queries.forEach { it.get() } + } + assertThat(model.definitionAt(at.line, at.column)).isEqualTo(expected) + } + + @Test + fun `cloning a validated lambda cannot expose its original context bindings`() { + val compilation = + compile( + "module Cloned { Int read(Int value) { function Int() f = () -> value; return f(); } }", + ) + + fun nodes(node: AstNode): Sequence = + sequence { + yield(node) + for (child in node.children()) yieldAll(nodes(child)) + } + val lambda = nodes(compilation.parsed()).filterIsInstance().single() + val bindings = requireNotNull(lambda.sourceBindings) + val origins = bindings.captureOrigins + assertThat(origins).hasSize(1) + val copy = lambda.clone() as LambdaExpression + assertThat(copy.sourceBindings).isNull() + assertThat(lambda.sourceBindings).isSameAs(bindings) + assertThat(bindings.captureOrigins).isEqualTo(origins) + } + + private fun compile( + source: String, + succeeds: Boolean = true, + ): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule(Source(source, URI), null, errors) + assertThat(compilation.succeeded()) + .describedAs(errors.errors.toString()) + .isEqualTo(succeeds) + return compilation + } + + private fun occurrence( + model: SemanticModel, + source: String, + marker: String, + ): SemanticModel.Occurrence = position(source, marker).let { model.occurrenceAt(it.line, it.column)!! } + + private fun position( + source: String, + marker: String, + ): SemanticModel.Position { + val comment = "/*$marker*/" + val start = source.indexOf(comment) + require(start >= 0) + val offset = start + comment.length + return SemanticModel.Position( + source.take(offset).count { it == '\n' }, + offset - source.lastIndexOf('\n', offset) - 1, + ) + } + + private companion object { + const val URI = "file:///Semantic.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticTokenTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticTokenTest.kt index 47c55b98cb4..c2682a14de0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticTokenTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SemanticTokenTest.kt @@ -10,9 +10,8 @@ import org.xvm.lsp.treesitter.SemanticTokenLegend /** * Semantic token tests for [TreeSitterAdapter]. * - * Exercises semantic token generation for various declaration types, type - * references, call expressions, and regression scenarios involving document - * edits that change document length. + * Exercises semantic token generation for various declaration types, type references, call + * expressions, and regression scenarios involving document edits that change document length. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -85,7 +84,10 @@ class SemanticTokenTest : TreeSitterTestBase() { // "Person" should be classified as "class" with "declaration" modifier // IntArray: [line, column, length, tokenType, tokenModifiers] - val classToken = decoded.find { it[3] == semanticTypeIndex["class"] && it[2] == "Person".length } + val classToken = + decoded.find { + it[3] == semanticTypeIndex["class"] && it[2] == "Person".length + } assertThat(classToken).isNotNull assertThat(hasSemanticModifier(classToken!![4], "declaration")).isTrue() } @@ -108,7 +110,10 @@ class SemanticTokenTest : TreeSitterTestBase() { assertThat(tokens).isNotNull val decoded = decodeSemanticTokens(tokens!!.data) - val ifaceToken = decoded.find { it[3] == semanticTypeIndex["interface"] && it[2] == "Runnable".length } + val ifaceToken = + decoded.find { + it[3] == semanticTypeIndex["interface"] && it[2] == "Runnable".length + } assertThat(ifaceToken).isNotNull assertThat(hasSemanticModifier(ifaceToken!![4], "declaration")).isTrue() } @@ -136,7 +141,10 @@ class SemanticTokenTest : TreeSitterTestBase() { val decoded = decodeSemanticTokens(tokens!!.data) logger.info("[TEST] method decl tokens: {}", decoded.map { it.toList() }) - val methodToken = decoded.find { it[3] == semanticTypeIndex["method"] && it[2] == "getName".length } + val methodToken = + decoded.find { + it[3] == semanticTypeIndex["method"] && it[2] == "getName".length + } assertThat(methodToken).isNotNull assertThat(hasSemanticModifier(methodToken!![4], "declaration")).isTrue() } @@ -162,7 +170,10 @@ class SemanticTokenTest : TreeSitterTestBase() { val decoded = decodeSemanticTokens(tokens!!.data) logger.info("[TEST] property decl tokens: {}", decoded.map { it.toList() }) - val propToken = decoded.find { it[3] == semanticTypeIndex["property"] && it[2] == "name".length } + val propToken = + decoded.find { + it[3] == semanticTypeIndex["property"] && it[2] == "name".length + } assertThat(propToken).isNotNull assertThat(hasSemanticModifier(propToken!![4], "declaration")).isTrue() } @@ -370,10 +381,10 @@ class SemanticTokenTest : TreeSitterTestBase() { /** * Regression test for StringIndexOutOfBoundsException after rename. * - * Reproduces the exact bug: compile a document with "Console console", rename - * "console" to "apa" (making the document shorter), then request semantic tokens. - * Previously crashed because incremental parsing (passing oldTree without Tree.edit()) - * produced nodes with stale byte offsets from the longer original source. + * Reproduces the exact bug: compile a document with "Console console", rename "console" to + * "apa" (making the document shorter), then request semantic tokens. Previously crashed + * because incremental parsing (passing oldTree without Tree.edit()) produced nodes with + * stale byte offsets from the longer original source. */ @Test @DisplayName("should return semantic tokens after rename shortens document") @@ -382,7 +393,7 @@ class SemanticTokenTest : TreeSitterTestBase() { val originalSource = """ module myapp { - void run(String[] args=[]) { + void run(String[] args = []) { @Inject Console console; if (args.empty) { console.print("Hello!"); @@ -414,7 +425,8 @@ class SemanticTokenTest : TreeSitterTestBase() { assertThat(result2.success).isTrue() // Step 5: Semantic tokens on shorter document must NOT crash - // Previously threw: StringIndexOutOfBoundsException: Range [178, 178 + 238) out of bounds + // Previously threw: StringIndexOutOfBoundsException: Range [178, 178 + 238) out of + // bounds val tokens2 = ts.getSemanticTokens(uri) assertThat(tokens2).isNotNull assertThat(tokens2!!.data).isNotEmpty @@ -426,9 +438,9 @@ class SemanticTokenTest : TreeSitterTestBase() { } /** - * Regression test: compile -> rename (longer) -> semantic tokens. - * The reverse case: document grows after rename. Verifies we don't - * have off-by-one errors in the opposite direction. + * Regression test: compile -> rename (longer) -> semantic tokens. The reverse case: + * document grows after rename. Verifies we don't have off-by-one errors in the opposite + * direction. */ @Test @DisplayName("should return semantic tokens after rename lengthens document") @@ -461,8 +473,8 @@ class SemanticTokenTest : TreeSitterTestBase() { } /** - * Regression test: multiple rapid edits on same URI. - * Simulates fast typing where compile is called many times in quick succession. + * Regression test: multiple rapid edits on same URI. Simulates fast typing where compile is + * called many times in quick succession. */ @Test @DisplayName("should handle rapid sequential recompilations") @@ -483,8 +495,8 @@ class SemanticTokenTest : TreeSitterTestBase() { } /** - * Verify folding ranges also work after rename (they use line/column, not byte offsets, - * so they should always work -- but good to verify the full adapter pipeline). + * Verify folding ranges also work after rename (they use line/column, not byte offsets, so + * they should always work -- but good to verify the full adapter pipeline). */ @Test @DisplayName("should return folding ranges after rename") @@ -493,7 +505,7 @@ class SemanticTokenTest : TreeSitterTestBase() { val originalSource = """ module myapp { - void run(String[] args=[]) { + void run(String[] args = []) { @Inject Console console; console.print("Hello!"); } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SignatureHelpTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SignatureHelpTest.kt index e144ae16cf3..c87f6377106 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SignatureHelpTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/SignatureHelpTest.kt @@ -9,8 +9,8 @@ import org.junit.jupiter.api.TestInstance /** * Signature help tests for [TreeSitterAdapter]. * - * Exercises signature help resolution, active parameter tracking, overloads, - * nested calls, and edge cases in call pattern parsing. + * Exercises signature help resolution, active parameter tracking, overloads, nested calls, and edge + * cases in call pattern parsing. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -25,17 +25,18 @@ class SignatureHelpTest : TreeSitterTestBase() { @DisplayName("getSignatureHelp() -- tree-sitter-specific") inner class SignatureHelpTests { /** - * When the cursor is inside the argument list of `add(1, 2)`, the adapter - * walks up to the enclosing `call_expression`, extracts the function name, - * and looks up the matching `method_declaration`. The result should contain - * one signature with the full label and both parameters. + * When the cursor is inside the argument list of `add(1, 2)`, the adapter walks up to the + * enclosing `call_expression`, extracts the function name, and looks up the matching + * `method_declaration`. The result should contain one signature with the full label and + * both parameters. */ @Test @DisplayName("should return signature help for method call") fun shouldReturnSignatureHelpForMethodCall() { val uri = freshUri() ts.compile(uri, calculatorSource()) - val help = logged("shouldReturnSignatureHelpForMethodCall", ts.getSignatureHelp(uri, 6, 16)) + val help = + logged("shouldReturnSignatureHelpForMethodCall", ts.getSignatureHelp(uri, 6, 16)) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(1) @@ -46,8 +47,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * With the cursor on the first argument (before the comma) in `add(1, 2)`, - * the active parameter should be 0. + * With the cursor on the first argument (before the comma) in `add(1, 2)`, the active + * parameter should be 0. */ @Test @DisplayName("should report first parameter as active before comma") @@ -61,9 +62,9 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * With the cursor after the comma in `add(1, 2)`, the adapter counts commas - * before the cursor to determine the active parameter index. The active - * parameter should be 1 (the second parameter). + * With the cursor after the comma in `add(1, 2)`, the adapter counts commas before the + * cursor to determine the active parameter index. The active parameter should be 1 (the + * second parameter). */ @Test @DisplayName("should track active parameter based on comma position") @@ -77,8 +78,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * Cursor on a class name (not inside a call expression) should return null - * because there is no enclosing `call_expression` node to provide context. + * Cursor on a class name (not inside a call expression) should return null because there is + * no enclosing `call_expression` node to provide context. */ @Test @DisplayName("should return null outside call expression") @@ -93,14 +94,15 @@ class SignatureHelpTest : TreeSitterTestBase() { """.trimIndent() ts.compile(uri, source) - val help = logged("shouldReturnNullOutsideCallExpression", ts.getSignatureHelp(uri, 1, 10)) + val help = + logged("shouldReturnNullOutsideCallExpression", ts.getSignatureHelp(uri, 1, 10)) assertThat(help).isNull() } /** - * A call to a zero-parameter method `greet()` should produce a signature - * with an empty parameter list and label "greet()". + * A call to a zero-parameter method `greet()` should produce a signature with an empty + * parameter list and label "greet()". */ @Test @DisplayName("should return signature help for no-arg call") @@ -108,7 +110,8 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, greeterSource()) // line 6: ` greet();` -- col 18 = ')' inside call - val help = logged("shouldReturnSignatureHelpForNoArgCall", ts.getSignatureHelp(uri, 6, 18)) + val help = + logged("shouldReturnSignatureHelpForNoArgCall", ts.getSignatureHelp(uri, 6, 18)) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(1) @@ -117,8 +120,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * Cursor on the first argument of a three-parameter call `clamp(5, 0, 100)` - * should report activeParameter = 0 and show the full three-param label. + * Cursor on the first argument of a three-parameter call `clamp(5, 0, 100)` should report + * activeParameter = 0 and show the full three-param label. */ @Test @DisplayName("should report first param active for three-arg call") @@ -126,7 +129,11 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, clampSource()) // line 6: ` clamp(5, 0, 100);` -- col 18 = '5' - val help = logged("shouldReportFirstParamActiveForThreeArgCall", ts.getSignatureHelp(uri, 6, 18)) + val help = + logged( + "shouldReportFirstParamActiveForThreeArgCall", + ts.getSignatureHelp(uri, 6, 18), + ) assertThat(help).isNotNull assertThat(help!!.activeParameter).isEqualTo(0) @@ -134,8 +141,7 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * Cursor on the second argument of `clamp(5, 0, 100)` should report - * activeParameter = 1. + * Cursor on the second argument of `clamp(5, 0, 100)` should report activeParameter = 1. */ @Test @DisplayName("should report second param active for three-arg call") @@ -143,31 +149,36 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, clampSource()) // line 6: ` clamp(5, 0, 100);` -- col 21 = '0' - val help = logged("shouldReportSecondParamActiveForThreeArgCall", ts.getSignatureHelp(uri, 6, 21)) + val help = + logged( + "shouldReportSecondParamActiveForThreeArgCall", + ts.getSignatureHelp(uri, 6, 21), + ) assertThat(help).isNotNull assertThat(help!!.activeParameter).isEqualTo(1) } - /** - * Cursor on the third argument of `clamp(5, 0, 100)` should report - * activeParameter = 2. - */ + /** Cursor on the third argument of `clamp(5, 0, 100)` should report activeParameter = 2. */ @Test @DisplayName("should report third param active for three-arg call") fun shouldReportThirdParamActiveForThreeArgCall() { val uri = freshUri() ts.compile(uri, clampSource()) // line 6: ` clamp(5, 0, 100);` -- col 24 = '1' (first digit of 100) - val help = logged("shouldReportThirdParamActiveForThreeArgCall", ts.getSignatureHelp(uri, 6, 24)) + val help = + logged( + "shouldReportThirdParamActiveForThreeArgCall", + ts.getSignatureHelp(uri, 6, 24), + ) assertThat(help).isNotNull assertThat(help!!.activeParameter).isEqualTo(2) } /** - * Two methods with the same name but different parameter counts should - * produce two signatures in the result. + * Two methods with the same name but different parameter counts should produce two + * signatures in the result. */ @Test @DisplayName("should return multiple signatures for overloads") @@ -175,15 +186,19 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, overloadedFormatSource()) // line 9: ` format("hello", 10);` -- col 19 = '"' inside call - val help = logged("shouldReturnMultipleSignaturesForOverloads", ts.getSignatureHelp(uri, 9, 19)) + val help = + logged( + "shouldReturnMultipleSignaturesForOverloads", + ts.getSignatureHelp(uri, 9, 19), + ) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(2) } /** - * In `add(negate(1), 2)`, cursor on `1` inside `negate(1)` should - * return the signature for `negate`, not `add`. + * In `add(negate(1), 2)`, cursor on `1` inside `negate(1)` should return the signature for + * `negate`, not `add`. */ @Test @DisplayName("should return signature help for inner nested call") @@ -191,7 +206,11 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, nestedCallSource()) // line 9: ` add(negate(1), 2);` -- col 23 = '1' - val help = logged("shouldReturnSignatureHelpForInnerNestedCall", ts.getSignatureHelp(uri, 9, 23)) + val help = + logged( + "shouldReturnSignatureHelpForInnerNestedCall", + ts.getSignatureHelp(uri, 9, 23), + ) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(1) @@ -199,8 +218,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * In `add(negate(1), 2)`, cursor on `2` should return the signature - * for `add` with activeParameter = 1. + * In `add(negate(1), 2)`, cursor on `2` should return the signature for `add` with + * activeParameter = 1. */ @Test @DisplayName("should return signature help for outer nested call") @@ -208,7 +227,11 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, nestedCallSource()) // line 9: ` add(negate(1), 2);` -- col 27 = '2' - val help = logged("shouldReturnSignatureHelpForOuterNestedCall", ts.getSignatureHelp(uri, 9, 27)) + val help = + logged( + "shouldReturnSignatureHelpForOuterNestedCall", + ts.getSignatureHelp(uri, 9, 27), + ) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(1) @@ -217,8 +240,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * A call to a sibling method from a different method in the same class - * should resolve correctly via same-file lookup. + * A call to a sibling method from a different method in the same class should resolve + * correctly via same-file lookup. */ @Test @DisplayName("should resolve cross-method call in same class") @@ -226,7 +249,8 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, crossMethodSource()) // line 6: ` return repeat(s, width);` -- col 26 = 's' - val help = logged("shouldResolveCrossMethodCallInSameClass", ts.getSignatureHelp(uri, 6, 26)) + val help = + logged("shouldResolveCrossMethodCallInSameClass", ts.getSignatureHelp(uri, 6, 26)) assertThat(help).isNotNull assertThat(help!!.signatures).hasSize(1) @@ -235,16 +259,18 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * Cursor on the fifth argument of a five-parameter method should - * report activeParameter = 4 and the signature should have 5 params. + * Cursor on the fifth argument of a five-parameter method should report activeParameter = 4 + * and the signature should have 5 params. */ @Test @DisplayName("should track active param in five-param method") fun shouldTrackActiveParamInFiveParamMethod() { val uri = freshUri() ts.compile(uri, fiveParamSource()) - // line 6: ` execute("run", 30, True, 5, "out.log");` -- col 40 = '"' of "out.log" - val help = logged("shouldTrackActiveParamInFiveParamMethod", ts.getSignatureHelp(uri, 6, 40)) + // line 6: ` execute("run", 30, True, 5, "out.log");` -- col 40 = '"' of + // "out.log" + val help = + logged("shouldTrackActiveParamInFiveParamMethod", ts.getSignatureHelp(uri, 6, 40)) assertThat(help).isNotNull assertThat(help!!.activeParameter).isEqualTo(4) @@ -252,8 +278,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * When the cursor is right at the opening paren of a call, the adapter - * should still find the enclosing call expression and return a result. + * When the cursor is right at the opening paren of a call, the adapter should still find + * the enclosing call expression and return a result. */ @Test @DisplayName("should return signature help at open paren") @@ -261,15 +287,15 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, calculatorSource()) // line 6: ` add(1, 2);` -- col 15 = '(' - val help = logged("shouldReturnSignatureHelpAtOpenParen", ts.getSignatureHelp(uri, 6, 15)) + val help = + logged("shouldReturnSignatureHelpAtOpenParen", ts.getSignatureHelp(uri, 6, 15)) assertThat(help).isNotNull assertThat(help!!.signatures[0].label).isEqualTo("add(Int a, Int b)") } /** - * Calling a method that has no declaration in the file should return null - * without crashing. + * Calling a method that has no declaration in the file should return null without crashing. */ @Test @DisplayName("should return null when called method not in file") @@ -277,7 +303,8 @@ class SignatureHelpTest : TreeSitterTestBase() { val uri = freshUri() ts.compile(uri, unknownMethodSource()) // line 3: ` unknown(42);` -- col 20 = '4' - val help = logged("shouldReturnNullWhenCalledMethodNotInFile", ts.getSignatureHelp(uri, 3, 20)) + val help = + logged("shouldReturnNullWhenCalledMethodNotInFile", ts.getSignatureHelp(uri, 3, 20)) assertThat(help).isNull() } @@ -406,8 +433,8 @@ class SignatureHelpTest : TreeSitterTestBase() { @DisplayName("call pattern edge cases -- tree-sitter-specific") inner class CallPatternParsingTests { /** - * `new Box(42)` is a constructor invocation, not a method call. Signature - * help should return null (no matching method declaration) without crashing. + * `new Box(42)` is a constructor invocation, not a method call. Signature help should + * return null (no matching method declaration) without crashing. */ @Test @DisplayName("should return null for new expression") @@ -437,8 +464,8 @@ class SignatureHelpTest : TreeSitterTestBase() { } /** - * Cursor on the method name in a declaration (not a call) should return null - * because the declaration's parameters node is not named "arguments". + * Cursor on the method name in a declaration (not a call) should return null because the + * declaration's parameters node is not named "arguments". */ @Test @DisplayName("should return null at method declaration site") @@ -457,14 +484,15 @@ class SignatureHelpTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on 'add' in declaration at line 2, col 12 - val help = logged("shouldReturnNullAtMethodDeclarationSite", ts.getSignatureHelp(uri, 2, 12)) + val help = + logged("shouldReturnNullAtMethodDeclarationSite", ts.getSignatureHelp(uri, 2, 12)) assertThat(help).isNull() } /** - * Cursor on a bare identifier that is not a call expression (e.g., a - * variable reference) should return null. + * Cursor on a bare identifier that is not a call expression (e.g., a variable reference) + * should return null. */ @Test @DisplayName("should return null for bare identifier reference") @@ -484,7 +512,11 @@ class SignatureHelpTest : TreeSitterTestBase() { ts.compile(uri, source) // cursor on 'x' in `Int y = x;` at line 4, col 20 - val help = logged("shouldReturnNullForBareIdentifierReference", ts.getSignatureHelp(uri, 4, 20)) + val help = + logged( + "shouldReturnNullForBareIdentifierReference", + ts.getSignatureHelp(uri, 4, 20), + ) assertThat(help).isNull() } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterAdapterTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterAdapterTest.kt index 7221b724af6..a5e82dc4e88 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterAdapterTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterAdapterTest.kt @@ -11,8 +11,8 @@ import org.xvm.lsp.model.SymbolInfo /** * Core adapter contract tests for [TreeSitterAdapter]. * - * Exercises lifecycle, compile, symbol lookup, and hover -- the fundamental - * operations that every [Adapter] must support. + * Exercises lifecycle, compile, symbol lookup, and hover -- the fundamental operations that every + * [Adapter] must support. * * All tests are skipped (not failed) when the tree-sitter native library is unavailable. */ @@ -49,8 +49,8 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { @DisplayName("compile()") inner class CompileTests { /** - * A minimal `module myapp { }` should parse without errors and produce a - * MODULE symbol named "myapp" via the tree-sitter declaration query. + * A minimal `module myapp { }` should parse without errors and produce a MODULE symbol + * named "myapp" via the tree-sitter declaration query. */ @Test @DisplayName("should parse module declaration") @@ -65,13 +65,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.symbols) - .anyMatch { it.name == "myapp" && it.kind == SymbolInfo.SymbolKind.MODULE } + assertThat(result.symbols).anyMatch { + it.name == "myapp" && it.kind == SymbolInfo.SymbolKind.MODULE + } } /** - * A class nested inside a module should produce a CLASS symbol. - * Verifies the query pattern `(class_declaration (identifier) @name)`. + * A class nested inside a module should produce a CLASS symbol. Verifies the query pattern + * `(class_declaration (identifier) @name)`. */ @Test @DisplayName("should parse class declaration") @@ -88,15 +89,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.symbols) - .anyMatch { it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS } + assertThat(result.symbols).anyMatch { + it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS + } } /** - * Gene's email preamble: navigation jumped to the doc-comment line, not the - * declaration itself. The symbol's location must point at the identifier - * (`Person`) so cmd-click lands on the `class Person {` line, not on the - * doc-comment prefix that precedes it. + * Gene's email preamble: navigation jumped to the doc-comment line, not the declaration + * itself. The symbol's location must point at the identifier (`Person`) so cmd-click lands + * on the `class Person {` line, not on the doc-comment prefix that precedes it. */ @Test @DisplayName("symbol location should be the name identifier, not the doc-comment prefix") @@ -120,8 +121,8 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { } /** - * Verifies tree-sitter recognizes the `interface` keyword and maps it - * to [SymbolInfo.SymbolKind.INTERFACE] via `interface_declaration`. + * Verifies tree-sitter recognizes the `interface` keyword and maps it to + * [SymbolInfo.SymbolKind.INTERFACE] via `interface_declaration`. */ @Test @DisplayName("should parse interface declaration") @@ -137,8 +138,9 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) - assertThat(result.symbols) - .anyMatch { it.name == "Runnable" && it.kind == SymbolInfo.SymbolKind.INTERFACE } + assertThat(result.symbols).anyMatch { + it.name == "Runnable" && it.kind == SymbolInfo.SymbolKind.INTERFACE + } } /** @@ -162,16 +164,16 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) - assertThat(result.symbols) - .anyMatch { it.name == "getName" && it.kind == SymbolInfo.SymbolKind.METHOD } + assertThat(result.symbols).anyMatch { + it.name == "getName" && it.kind == SymbolInfo.SymbolKind.METHOD + } } /** - * Short-form property getter at module level: `Int val2.get() = 43;`. - * The grammar's `module_body` rule must accept `property_getter_declaration`, - * not just `property_declaration` and `method_declaration`. Same shape was - * already accepted inside class bodies; this guards against regression of - * the parity fix. + * Short-form property getter at module level: `Int val2.get() = 43;`. The grammar's + * `module_body` rule must accept `property_getter_declaration`, not just + * `property_declaration` and `method_declaration`. Same shape was already accepted inside + * class bodies; this guards against regression of the parity fix. */ @Test @DisplayName("should parse module-level property getter") @@ -187,13 +189,12 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Same shape inside a package body. `package_body` mirrors `module_body` - * for top-level declarations; both should now accept the short-form getter. + * Same shape inside a package body. `package_body` mirrors `module_body` for top-level + * declarations; both should now accept the short-form getter. */ @Test @DisplayName("should parse package-level property getter") @@ -211,16 +212,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Fractional duration literal: `Duration:0.5S` (half a second). The - * `duration_literal` regex previously accepted only integer values per - * unit, so `manualTests/.../services.x`'s `Timeout(Duration:0.5S, True)` - * call failed to parse. ISO-8601 allows fractional values; the grammar - * regex now matches `[0-9]+(\.[0-9]+)?` per unit. + * Fractional duration literal: `Duration:0.5S` (half a second). The `duration_literal` + * regex previously accepted only integer values per unit, so `manualTests/.../services.x`'s + * `Timeout(Duration:0.5S, True)` call failed to parse. ISO-8601 allows fractional values; + * the grammar regex now matches `[0-9]+(\.[0-9]+)?` per unit. */ @Test @DisplayName("should parse fractional duration literal") @@ -240,19 +239,17 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Multi-return destructuring assignment with annotations on each element: - * `(@Future Int v1, @Future Int v2) = svc.multiReturn(...)`. This is how - * services declare async future variables via tuple destructuring. The - * `tuple_assignment_element` rule previously accepted typed forms but - * not leading annotations, so `manualTests/.../services.x`'s multi-return - * call site failed to parse. The rule now allows `repeat($.annotation)` - * before both the val/var-prefixed form and the type-prefixed form, with - * a 2-way conflict declared between `parameter` and `tuple_assignment_element`. + * Multi-return destructuring assignment with annotations on each element: `(@Future Int + * v1, @Future Int v2) = svc.multiReturn(...)`. This is how services declare async future + * variables via tuple destructuring. The `tuple_assignment_element` rule previously + * accepted typed forms but not leading annotations, so `manualTests/.../services.x`'s + * multi-return call site failed to parse. The rule now allows `repeat($.annotation)` before + * both the val/var-prefixed form and the type-prefixed form, with a 2-way conflict declared + * between `parameter` and `tuple_assignment_element`. */ @Test @DisplayName("should parse annotated multi-return destructuring assignment") @@ -270,18 +267,16 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Access-qualified type expressions (issue #459): a visibility modifier - * is a legal type prefix per the BNF's ExtendedPrefixTypeExpression, in - * the same slot as `immutable`. Used in cast contexts: - * `.as(protected JsonMapStore)` and the parenthesized - * `.revealAs((protected TxManager))` from the jsondb tests. - * The `access_qualified_type` rule carries prec.dynamic(-1) so that - * `protected Int x;` still parses as modifier + property declaration. + * Access-qualified type expressions (issue #459): a visibility modifier is a legal type + * prefix per the BNF's ExtendedPrefixTypeExpression, in the same slot as `immutable`. Used + * in cast contexts: `.as(protected JsonMapStore)` and the parenthesized + * `.revealAs((protected TxManager))` from the jsondb tests. The + * `access_qualified_type` rule carries prec.dynamic(-1) so that `protected Int x;` still + * parses as modifier + property declaration. */ @Test @DisplayName("should parse access-qualified type in cast expressions") @@ -301,15 +296,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Parameterized virtual child types (issue #459): `Base.Child2` - * in value position must parse as a single `member_type`, not as a - * `member_expression` followed by an unparseable ``. From - * `manualTests/src/main/x/generics.x`. + * Parameterized virtual child types (issue #459): `Base.Child2` in value + * position must parse as a single `member_type`, not as a `member_expression` followed by + * an unparseable ``. From `manualTests/src/main/x/generics.x`. */ @Test @DisplayName("should parse parameterized virtual child type in value position") @@ -329,8 +322,7 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** @@ -539,10 +531,9 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { } /** - * The exact shape of `manualTests/annos.x#testAnnotations3()` reported - * as a false negative in issue #459: a local `annotation ... into ...` - * declaration inside a method body, followed by annotated local classes - * that use it. Must produce no ERROR diagnostics. + * The exact shape of `manualTests/annos.x#testAnnotations3()` reported as a false negative + * in issue #459: a local `annotation ... into ...` declaration inside a method body, + * followed by annotated local classes that use it. Must produce no ERROR diagnostics. */ @Test @DisplayName("should parse local annotation declaration with annotated local classes") @@ -579,18 +570,16 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Stacked switch labels: `default: case 0..39: ...` (or any combination - * of `case ...:` and `default:` labels for the same arm). The grammar's - * `case_clause` and `expression_case_clause` rules previously allowed a - * leading `repeat(case :)` followed by a final `case ` - * or `default` -- so `default:` could only appear as the final label, - * not stacked before another `case:`. The repeat now accepts both - * `case :` and `default:`. From `manualTests/.../StringBufferTest.x`. + * Stacked switch labels: `default: case 0..39: ...` (or any combination of `case ...:` and + * `default:` labels for the same arm). The grammar's `case_clause` and + * `expression_case_clause` rules previously allowed a leading `repeat(case :)` + * followed by a final `case ` or `default` -- so `default:` could only appear as + * the final label, not stacked before another `case:`. The repeat now accepts both `case + * :` and `default:`. From `manualTests/.../StringBufferTest.x`. */ @Test @DisplayName("should parse stacked default+case switch labels") @@ -613,16 +602,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Constructor short-form body: `construct(...) = expr;`. The grammar - * already supported short-form `= expr;` for methods and getters but - * not constructors, so `@Override construct(String s) = TODO();` - * (a placeholder constructor signature satisfying a contract) failed - * to parse. From `manualTests/.../StringBufferTest.x`. + * Constructor short-form body: `construct(...) = expr;`. The grammar already supported + * short-form `= expr;` for methods and getters but not constructors, so `@Override + * construct(String s) = TODO();` (a placeholder constructor signature satisfying a + * contract) failed to parse. From `manualTests/.../StringBufferTest.x`. */ @Test @DisplayName("should parse short-form constructor body") @@ -640,15 +627,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Single-element tuple expression with explicit trailing comma: `(x,)`. - * Disambiguates a 1-tuple from a parenthesized expression `(x)`. The - * grammar previously accepted only empty `()` and 2+ element tuples; - * `manualTests/.../tuple.x`'s `(1.toInt(), )` therefore failed. + * Single-element tuple expression with explicit trailing comma: `(x,)`. Disambiguates a + * 1-tuple from a parenthesized expression `(x)`. The grammar previously accepted only empty + * `()` and 2+ element tuples; `manualTests/.../tuple.x`'s `(1.toInt(), )` therefore failed. */ @Test @DisplayName("should parse single-element tuple with trailing comma") @@ -667,19 +652,17 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Annotation on a for-loop's variable initializer: - * `for (@Watch(x) Int i = 3; i > 0; --i) {}`. The `for_var_declarations` - * rule previously accepted only `Type identifier = expr` without - * leading annotations. Local-variable annotations like `@Watch(...)` - * (used in `manualTests/.../annos.x` for property-watch tracing) needed - * the same `repeat($.annotation)` prefix that other variable-declaration - * forms already had. Without this, the for-loop init failed to parse - * and cascade errors propagated through the rest of the method body. + * Annotation on a for-loop's variable initializer: `for (@Watch(x) Int i = 3; i > 0; --i) + * {}`. The `for_var_declarations` rule previously accepted only `Type identifier = expr` + * without leading annotations. Local-variable annotations like `@Watch(...)` (used in + * `manualTests/.../annos.x` for property-watch tracing) needed the same + * `repeat($.annotation)` prefix that other variable-declaration forms already had. Without + * this, the for-loop init failed to parse and cascade errors propagated through the rest of + * the method body. */ @Test @DisplayName("should parse annotation on for-loop variable initializer") @@ -697,18 +680,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Package-import resource-provider clause: - * `package foo import bar.Baz inject(Int n, String _) using ProviderType;`. - * The `inject(parameters)` clause declares the formal parameters the - * resource provider must supply; `using ` names the - * provider class. Both clauses are optional. From - * `manualTests/.../container.x`'s - * `package contained import TestContained inject(Int value, String _) using SimpleResourceProvider;`. + * Package-import resource-provider clause: `package foo import bar.Baz inject(Int n, String + * _) using ProviderType;`. The `inject(parameters)` clause declares the formal parameters + * the resource provider must supply; `using ` names the provider class. Both + * clauses are optional. From `manualTests/.../container.x`'s `package contained import + * TestContained inject(Int value, String _) using SimpleResourceProvider;`. */ @Test @DisplayName("should parse package-import resource-provider clause") @@ -726,15 +706,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Wildcard `_` is allowed as the name in a `val`/`var` declaration to - * discard a value: `val _ = r.eof;`. The grammar's - * `variable_declaration` `name:` field must accept the wildcard token - * in addition to identifiers. Without this, the standard library's + * Wildcard `_` is allowed as the name in a `val`/`var` declaration to discard a value: `val + * _ = r.eof;`. The grammar's `variable_declaration` `name:` field must accept the wildcard + * token in addition to identifiers. Without this, the standard library's * "evaluate-and-discard" idiom fails to parse. */ @Test @@ -758,16 +736,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Bare relative-path literal as a primary expression: - * `File f = ./IO.x;` or `Directory d = ../shared;`. Distinct from - * `#path` (binary file embed) and `$./path` (string file embed) -- - * this form is a runtime File/Directory reference. Without this - * rule, `manualTests/.../IO.x` failed at the assignment. + * Bare relative-path literal as a primary expression: `File f = ./IO.x;` or `Directory d = + * ../shared;`. Distinct from `#path` (binary file embed) and `$./path` (string file embed) + * -- this form is a runtime File/Directory reference. Without this rule, + * `manualTests/.../IO.x` failed at the assignment. */ @Test @DisplayName("should parse relative path literal as expression") @@ -787,18 +763,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * `assert:rnd(N) cond` is the sample-rate form of assert -- the - * assertion fires roughly once per N invocations. The argument list - * is specific to `assert:rnd`; the other variants (`assert:arg`, - * `assert:bounds`, ...) do not take args, and in particular - * `assert:arg (Type x, ...) := expr` uses the parens for a tuple - * destructuring conditional declaration, not a variant arg list. - * The grammar must distinguish these. + * `assert:rnd(N) cond` is the sample-rate form of assert -- the assertion fires roughly + * once per N invocations. The argument list is specific to `assert:rnd`; the other variants + * (`assert:arg`, `assert:bounds`, ...) do not take args, and in particular `assert:arg + * (Type x, ...) := expr` uses the parens for a tuple destructuring conditional declaration, + * not a variant arg list. The grammar must distinguish these. */ @Test @DisplayName("should parse assert:rnd with sample-rate argument") @@ -818,16 +791,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Companion to `shouldParseAssertRndWithSampleRate`: confirms that - * `assert:arg` followed by a tuple destructuring conditional - * declaration still parses (regression guard -- an earlier attempt - * at the assert:rnd fix accidentally swallowed `(Type x, ...)` as - * a variant arg list, breaking `lib_net/.../UriTemplate.x`). + * Companion to `shouldParseAssertRndWithSampleRate`: confirms that `assert:arg` followed by + * a tuple destructuring conditional declaration still parses (regression guard -- an + * earlier attempt at the assert:rnd fix accidentally swallowed `(Type x, ...)` as a variant + * arg list, breaking `lib_net/.../UriTemplate.x`). */ @Test @DisplayName("should parse assert:arg with tuple destructuring conditional") @@ -848,17 +819,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * `assert !(Type x := expr)` -- negated typed conditional binding. - * Used to assert that a `:=` conditional form *fails* to bind - * (e.g. asserting that `next()` does not yield a value of the - * named type). The negation cannot be expressed by simply wrapping - * a `_expression` because the parenthesized form contains a - * `conditional_declaration`, which is not an expression. + * `assert !(Type x := expr)` -- negated typed conditional binding. Used to assert that a + * `:=` conditional form *fails* to bind (e.g. asserting that `next()` does not yield a + * value of the named type). The negation cannot be expressed by simply wrapping a + * `_expression` because the parenthesized form contains a `conditional_declaration`, which + * is not an expression. */ @Test @DisplayName("should parse negated typed conditional in assert") @@ -878,21 +847,18 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Version literal: `v:` followed by a version string. Covers the - * full range of forms Ecstasy supports -- simple integer - * (`v:1`), dotted (`v:1.2`, `v:5.6.7.8`), pre-release identifier - * alone (`v:beta2`), pre-release suffix on a dotted version - * (`v:5.6.7.8-alpha`, `v:1.2-beta5`), concatenated pre-release - * (`v:1.2beta5`), and build-metadata suffix (`v:1.2beta5+123-456.abc`). - * Without this, `typed_literal` would try to parse `v` as a type - * expression, `:` as the typed-literal separator, and the rest as - * a regular literal -- which works only for trivial cases and - * fails on multi-segment dotted versions and pre-release suffixes. + * Version literal: `v:` followed by a version string. Covers the full range of forms + * Ecstasy supports -- simple integer (`v:1`), dotted (`v:1.2`, `v:5.6.7.8`), pre-release + * identifier alone (`v:beta2`), pre-release suffix on a dotted version (`v:5.6.7.8-alpha`, + * `v:1.2-beta5`), concatenated pre-release (`v:1.2beta5`), and build-metadata suffix + * (`v:1.2beta5+123-456.abc`). Without this, `typed_literal` would try to parse `v` as a + * type expression, `:` as the typed-literal separator, and the rest as a regular literal -- + * which works only for trivial cases and fails on multi-segment dotted versions and + * pre-release suffixes. */ @Test @DisplayName("should parse version literals across all supported forms") @@ -916,17 +882,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Qualified-new with annotations between `new` and the type: - * `outer.new @Anno(args) Type()`. The unqualified `new` form - * already accepted `repeat($.annotation)` between the keyword and - * the type; the qualified form did not, so - * `manualTests/.../annos.x`'s `new Parent().new @Parent.Anno(descr) Parent.Child()` - * failed to parse. + * Qualified-new with annotations between `new` and the type: `outer.new @Anno(args) + * Type()`. The unqualified `new` form already accepted `repeat($.annotation)` between the + * keyword and the type; the qualified form did not, so `manualTests/.../annos.x`'s `new + * Parent().new @Parent.Anno(descr) Parent.Child()` failed to parse. */ @Test @DisplayName("should parse qualified-new with annotation") @@ -951,16 +914,14 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Annotation on a type inside a parenthesized type expression: - * `(@AutoFreezable Freezable)? o = Null`. The parens scope the - * annotation + type so the `?` nullable wrap applies to the - * whole annotated form. `parenthesized_type` previously took - * only `type_expression`; it now accepts leading annotations. + * Annotation on a type inside a parenthesized type expression: `(@AutoFreezable Freezable)? + * o = Null`. The parens scope the annotation + type so the `?` nullable wrap applies to the + * whole annotated form. `parenthesized_type` previously took only `type_expression`; it now + * accepts leading annotations. */ @Test @DisplayName("should parse annotated type inside parenthesized type") @@ -979,17 +940,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Tuple-assignment with `_` wildcard discarding a slot: - * `(_, Int index) = collection.binarySearch(...);`. Used - * pervasively in `lib_ecstasy/.../maps/*.x` to ignore the - * boolean returned alongside the typed slot of interest. - * Without the wildcard alternative on `tuple_assignment_element`, - * 14 lib_*/ files plus `manualTests/.../maps.x` failed to parse. + * Tuple-assignment with `_` wildcard discarding a slot: `(_, Int index) = + * collection.binarySearch(...);`. Used pervasively in `lib_ecstasy/.../maps/*.x` to ignore + * the boolean returned alongside the typed slot of interest. Without the wildcard + * alternative on `tuple_assignment_element`, 14 lib_*/ files plus `manualTests/.../maps.x` + * failed to parse. */ @Test @DisplayName("should parse tuple-assignment with wildcard slot") @@ -1014,15 +973,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Companion to wildcard tuple-assignment: a for-loop iterating - * a Map with both keys and values discarded -- `for ((_, _) : map)`. - * The `for_tuple_destructure` rule previously required typed - * bindings on every element; it now accepts wildcards too. + * Companion to wildcard tuple-assignment: a for-loop iterating a Map with both keys and + * values discarded -- `for ((_, _) : map)`. The `for_tuple_destructure` rule previously + * required typed bindings on every element; it now accepts wildcards too. */ @Test @DisplayName("should parse for-loop with wildcard tuple destructure") @@ -1047,14 +1004,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.success).isTrue() - assertThat(result.diagnostics) - .noneMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).noneMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * A missing closing brace should still parse (tree-sitter is error-tolerant), - * but the resulting tree must contain ERROR or MISSING nodes that get reported - * as diagnostics with severity ERROR. + * A missing closing brace should still parse (tree-sitter is error-tolerant), but the + * resulting tree must contain ERROR or MISSING nodes that get reported as diagnostics with + * severity ERROR. */ @Test @DisplayName("should detect syntax errors") @@ -1069,14 +1025,13 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.diagnostics).isNotEmpty() - assertThat(result.diagnostics) - .anyMatch { it.severity == Diagnostic.Severity.ERROR } + assertThat(result.diagnostics).anyMatch { it.severity == Diagnostic.Severity.ERROR } } /** - * Tree-sitter's error-recovery means a valid class followed by a malformed one - * should yield both diagnostics (for the broken class) and the valid "Person" - * symbol -- the key advantage over a traditional parser that would bail out. + * Tree-sitter's error-recovery means a valid class followed by a malformed one should yield + * both diagnostics (for the broken class) and the valid "Person" symbol -- the key + * advantage over a traditional parser that would bail out. */ @Test @DisplayName("should perform error-tolerant parsing") @@ -1094,14 +1049,15 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { val result = ts.compile(uri, source) assertThat(result.diagnostics).isNotEmpty() - assertThat(result.symbols) - .anyMatch { it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS } + assertThat(result.symbols).anyMatch { + it.name == "Person" && it.kind == SymbolInfo.SymbolKind.CLASS + } } /** - * Re-compiling the same URI with different content exercises the incremental - * parsing path (the adapter passes the old tree to `parser.parse`). Both - * compilations must succeed independently. + * Re-compiling the same URI with different content exercises the incremental parsing path + * (the adapter passes the old tree to `parser.parse`). Both compilations must succeed + * independently. */ @Test @DisplayName("should support incremental re-parsing") @@ -1124,8 +1080,8 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { @DisplayName("findSymbolAt()") inner class FindSymbolAtTests { /** - * Positioning the cursor on the class name "Person" (line 1, col 10) should - * resolve to the class declaration via the AST node lookup + declaration query. + * Positioning the cursor on the class name "Person" (line 1, col 10) should resolve to the + * class declaration via the AST node lookup + declaration query. */ @Test @DisplayName("should find class symbol at cursor position") @@ -1157,10 +1113,10 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { } /** - * Same doc-comment landing fix as [XtcQueryEngine.findAllDeclarations] but - * for the [XtcQueryEngine.findDeclarationAt] path (cursor-inside-a-declaration). - * The returned symbol's location must point at the identifier line, not at - * the leading doc-comment opener. + * Same doc-comment landing fix as [XtcQueryEngine.findAllDeclarations] but for the + * [XtcQueryEngine.findDeclarationAt] path (cursor-inside-a-declaration). The returned + * symbol's location must point at the identifier line, not at the leading doc-comment + * opener. */ @Test @DisplayName("doc-commented method symbol location should be the name identifier") @@ -1226,9 +1182,8 @@ class TreeSitterAdapterTest : TreeSitterTestBase() { @DisplayName("getHoverInfo()") inner class HoverTests { /** - * Hover delegates to [TreeSitterAdapter.findSymbolAt] then formats the symbol - * as Markdown. Positioning on "Person" should produce hover text containing - * the class name. + * Hover delegates to [TreeSitterAdapter.findSymbolAt] then formats the symbol as Markdown. + * Positioning on "Person" should produce hover text containing the class name. */ @Test @DisplayName("should return hover info for class") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterTestBase.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterTestBase.kt index abee819eb7e..a6d31732a43 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterTestBase.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TreeSitterTestBase.kt @@ -13,12 +13,11 @@ import java.util.concurrent.atomic.AtomicInteger /** * Shared base class for all tree-sitter adapter tests. * - * Provides adapter lifecycle management (setup, teardown, native-library - * availability check) and common helper methods so that each focused test - * class only contains the tests themselves. + * Provides adapter lifecycle management (setup, teardown, native-library availability check) and + * common helper methods so that each focused test class only contains the tests themselves. * - * All tests are skipped (not failed) when the tree-sitter native library - * is unavailable, making this safe to run in any environment. + * All tests are skipped (not failed) when the tree-sitter native library is unavailable, making + * this safe to run in any environment. */ @TestInstance(TestInstance.Lifecycle.PER_CLASS) abstract class TreeSitterTestBase { @@ -27,13 +26,13 @@ abstract class TreeSitterTestBase { private val uriCounter = AtomicInteger(0) /** Shorthand accessor -- safe because [assumeAvailable] guards every test. */ - protected val ts: TreeSitterAdapter get() = adapter!! + protected val ts: TreeSitterAdapter + get() = adapter!! /** - * Returns a unique URI per call so each test gets a fresh parse tree. - * Re-using the same URI across tests would trigger incremental parsing against - * a stale tree whose byte offsets don't match the new source, causing - * [StringIndexOutOfBoundsException] inside the native parser. + * Returns a unique URI per call so each test gets a fresh parse tree. Re-using the same URI + * across tests would trigger incremental parsing against a stale tree whose byte offsets don't + * match the new source, causing [StringIndexOutOfBoundsException] inside the native parser. */ protected fun freshUri(): String = "file:///t1st${uriCounter.incrementAndGet()}.x" diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt index 63f7d253820..087f0bc1dad 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt @@ -209,7 +209,7 @@ class TypeInfoDiagnosticsTest { * This one is worth its own test because master loses it. Compiling this source there reports * nothing at all - the compiler parks the file on a silence for the duration of a compilation, * and the warning goes into it - so the annotation is silently ignored and the author is never - * told. + * told. See the appendix of docs/errs.md. */ @Test fun `a duplicated property annotation is reported, which master swallows`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoFinalCompositionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoFinalCompositionTest.kt new file mode 100644 index 00000000000..7f1fb78b072 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoFinalCompositionTest.kt @@ -0,0 +1,333 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.FileStructure +import org.xvm.asm.constants.AnnotatedTypeConstant +import org.xvm.asm.constants.TypeInfo +import org.xvm.compiler.BuildRepository +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.NewExpression +import java.util.Properties + +/** Final compositions must be correct even when earlier fit/composition probes were silent. */ +class TypeInfoFinalCompositionTest { + @Test + fun `fresh serialized array XML and XODB compositions retain substituted members and supers`() { + val repository = freshRepository() + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module LoadedAudit { package xml import xml.xtclang.org; package db import jsondb.xtclang.org; }", + ), + repository, + errors, + ) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors).isEmpty() + + fun classes(moduleName: String): List { + val module = + compilation + .file() + .getModule(compilation.file().moduleIds().single { it.name == moduleName }) + return components(if (module.isFingerprint) module.fingerprintOrigin else module) + .filterIsInstance() + } + + val arrays = + classes("ecstasy.xtclang.org").filter { + it.identityConstant.pathString.contains(".elementAt(Int).Object:") + } + assertThat(arrays).hasSize(7) + val expected = + mapOf( + "asBooleanArray" to "Boolean", + "asNibbleArray" to "Nibble", + "asByteArray" to "Byte", + "asBitArray" to "Bit", + ) + arrays.forEach { type -> + inspect(type) { info -> + val pool = type.constantPool + val assigned = + info.properties.entries + .single { it.key.pathString.endsWith(".element.assigned") } + .value + assertThat(assigned.type).isEqualTo(pool.typeBoolean()) + val referent = + info.properties.entries + .single { it.key.pathString.endsWith(".element.assigned.Referent") } + .value + .type + assertThat(referent.getParamType(0)).isEqualTo(pool.typeBoolean()) + val getter = + info.methods.entries + .single { it.key.pathString.endsWith(".element.get()") } + .value + val setter = + info.methods.entries + .single { it.key.pathString.contains(".element.set(") } + .value + assertThat(setter.signature.params.toList()) + .containsExactlyElementsOf(getter.signature.returns.toList()) + val element = + getter.signature.returns + .single() + .valueString + val name = type.identityConstant.pathString + assertThat(element) + .endsWith( + expected.entries.firstOrNull { name.contains(it.key) }?.value ?: "NumType", + ) + val assignedGetter = + info.methods.entries + .single { it.key.pathString.endsWith(".element.assigned.get()") } + .value + assertThat(assignedGetter.chain.toList()).anySatisfy { + assertThat(it.identity.pathString).isEqualTo("NakedRef.get()") + assertThat(it.signature.returns.toList()).containsExactly(pool.typeBoolean()) + } + } + } + + val cursor = + classes("xml.xtclang.org").single { + it.identityConstant.pathString.endsWith("ContentList.cursor(Int).Cursor:1") + } + inspect(cursor) { info -> + val value = + info.properties.entries + .single { it.key.name == "value" } + .value.type + assertThat(value.valueString).isEqualTo("xml:Content") + val insert = info.methods.values.single { it.signature.name == "insert" } + assertThat(insert.signature.params.toList()).containsExactly(value) + assertThat(insert.chain.toList()).anySatisfy { + assertThat(it.identity.pathString).contains("List.Cursor.insert") + assertThat(it.signature.params.toList()).containsExactly(value) + } + } + + val maps = + classes("jsondb.xtclang.org").filter { + it.identityConstant.pathString.contains(".dbChildren.calc().Map:") + } + assertThat(maps).hasSize(7) + maps.forEach { type -> + inspect(type) { info -> + if (type.identityConstant.pathString.endsWith(".calc().Map:1")) { + val get = + info.methods.entries + .single { it.key.pathString.endsWith(".Map:1.get(String)") } + .value + assertThat(get.signature.params.toList()) + .containsExactly(type.constantPool.typeString()) + assertThat(get.signature.returns.map { it.valueString }) + .containsExactly("Boolean", "oodb:DBObject") + assertThat(get.chain.toList()).anySatisfy { + assertThat(it.identity.pathString).contains("Map.get(") + assertThat(it.signature.returns.toList()) + .containsExactlyElementsOf(get.signature.returns.toList()) + } + } + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["1, 2", "offset, length"]) + fun `Parsed annotations with constant and runtime arguments retain final node metadata`(arguments: String) { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = parsedSource(arguments) + val compilation = + EmbeddingSupport + .instance() + .compileModule(Source(source, "untitled:ParsedAudit.x"), freshRepository(), errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors).isEmpty() + ConstantPool.withPool(compilation.pool()).use { + val created = nodes(compilation.parsed()).filterIsInstance().single() + val info = created.type.ensureTypeInfo(errors) + assertThat(errors.errors).describedAs(created.type.valueString).isEmpty() + for (name in listOf("offset", "length")) { + assertThat(info.ensurePropertiesByName()[name]!!.type) + .isEqualTo(compilation.pool().typeInt64()) + } + assertThat(info.ensurePropertiesByName()["text"]!!.type) + .isEqualTo(compilation.pool().typeString()) + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "1", "\"bad\", 2", "1, 2, 3"]) + fun `invalid Parsed constructor arguments are rejected through the host listener`(arguments: String) { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule( + Source(parsedSource(arguments), "untitled:ParsedAudit.x"), + freshRepository(), + errors, + ) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors).noneMatch { it.code == "EMB-5" } + } + + @Test + fun `invalid supplied annotation constants still report after a silent TypeInfo lookup`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule(Source(parsedSource("1, 2")), freshRepository(), errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors).isEmpty() + ConstantPool.withPool(compilation.pool()).use { + val pool = compilation.pool() + val type = + nodes(compilation.parsed()).filterIsInstance().single().type + as AnnotatedTypeConstant + val invalid = + pool.ensureAnnotatedTypeConstant( + type.annotationClass, + arrayOf(pool.ensureStringConstant("bad"), pool.ensureIntConstant(2)), + type.underlyingType, + ) + invalid.ensureTypeInfo() + val reported = ErrorList() + invalid.ensureTypeInfo(reported) + assertThat(reported.errors.map { it.code }).containsExactly("COMPILER-140") + invalid.ensureTypeInfo(reported) + assertThat(reported.errors).hasSize(1) + } + } + + @Test + fun `missing declaration annotation arguments and incompatible targets remain errors`() { + CompilerTestSupport.configure() + for ( + source in + listOf( + "module Missing { annotation Tag(Int value) into Object; @Tag class Value {} }", + parsedSource("1, 2").replace("@ContentNode Data(text)", "Data(text)"), + ) + ) { + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule(Source(source), freshRepository(), errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isFalse() + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors).noneMatch { it.code == "EMB-5" } + } + } + + private fun parsedSource(arguments: String): String = + """ + module ParsedAudit { + package xml import xml.xtclang.org; + import xml.Data; + import xml.impl.ContentNode; + import xml.impl.Node; + import xml.impl.Parsed; + Node create(Int offset, Int length, String text) { + return new @Parsed($arguments) @ContentNode Data(text); + } + } + """.trimIndent() + + @Test + fun `fresh anonymous property has Boolean assigned metadata and invalid override reports to host`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val source = + """ + module ArrayAudit { + Var elementAt(Int index) { + return new Object() { + Byte element { + @Override Boolean assigned.get() = index < 4; + @Override Byte get() { assert:bounds assigned; return 1; } + @Override void set(Byte value) {} + } + }.&element; + } + } + """.trimIndent() + val compilation = EmbeddingSupport.instance().compileModule(Source(source), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + ConstantPool.withPool(compilation.pool()).use { + val created = nodes(compilation.parsed()).filterIsInstance().single() + val info = created.type.ensureTypeInfo(errors) + assertThat( + info.properties.entries + .single { it.key.pathString.endsWith(".element.assigned") } + .value + .type, + ).isEqualTo(compilation.pool().typeBoolean()) + } + assertThat(errors.errors).isEmpty() + + val invalidErrors = ErrorList() + val invalid = + source.replace("@Override void set(Byte value) {}", "@Override void absent() {}") + val rejected = + EmbeddingSupport.instance().compileModule(Source(invalid), null, invalidErrors) + assertThat(rejected.succeeded()).isFalse() + assertThat(invalidErrors.errors).anyMatch { + it.code.startsWith("VERIFY-") && it.message.contains("absent") + } + assertThat(invalidErrors.errors).noneMatch { it.code == "EMB-5" } + } + + private fun inspect( + type: ClassStructure, + check: (TypeInfo) -> Unit, + ) { + ConstantPool.withPool(type.constantPool).use { + val errors = ErrorList() + val info = type.formalType.ensureTypeInfo(errors) + assertThat(errors.errors).describedAs(type.identityConstant.pathString).isEmpty() + check(info) + } + } + + private fun components(root: Component): List = listOf(root) + root.children().flatMap(::components) + + private fun freshRepository(): BuildRepository { + val repository = BuildRepository() + val index = Properties().apply { resource("modules.properties").use { load(it) } } + for (name in index.getProperty("modules").split(',')) { + resource(name).use { repository.storeModule(FileStructure(it).module) } + } + return repository + } + + private fun nodes(root: AstNode): List = + buildList { + add(root) + root.children().forEachRemaining { addAll(nodes(it)) } + } + + private fun resource(name: String) = + checkNotNull(javaClass.getResourceAsStream("/org/xvm/lsp/xdk/$name")) { + "Missing compiled test module: $name" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt new file mode 100644 index 00000000000..35acd8d924b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt @@ -0,0 +1,159 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport.Compilation +import org.xvm.asm.ErrorListener +import org.xvm.asm.FileStructure +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS + +class XdkAdapterLifecycleTest { + private class PausedCompiler { + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val seen = CopyOnWriteArrayList() + + fun compile( + source: Source, + errs: ErrorListener, + ): Compilation { + val text = source.toRawString() + seen.add(text) + if (seen.size == 1) { + started.countDown() + check(release.await(10, SECONDS)) { "test did not release compiler" } + } + // Deliberately ignore cancellation: even a compiler stage that cannot stop must + // never install its old result after an edit, close or shutdown. + errs.error("PARSER-03", ErrorListener.`in`(source, 0, 1), text) + return Compilation.forFile(FileStructure("Fixture")) + } + + fun awaitStart() = check(started.await(10, SECONDS)) { "compiler did not start" } + } + + @Test + fun `a running analysis cannot overwrite a newer edit`() { + val compiler = PausedCompiler() + XdkAdapter(compiler::compile).use { adapter -> + val old = adapter.compileAsync(URI, "old") + try { + compiler.awaitStart() + val latest = adapter.compileAsync(URI, "latest") + assertThat(old.isCancelled).isTrue() + assertThat(adapter.getCachedResult(URI)).isNull() + compiler.release.countDown() + val result = latest.get(10, SECONDS) + assertThat(result.diagnostics.single().message).contains("latest") + assertThat(adapter.getCachedResult(URI)).isEqualTo(result) + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `queued edits are coalesced before they reach the compiler`() { + val compiler = PausedCompiler() + XdkAdapter(compiler::compile).use { adapter -> + val blocker = adapter.compileAsync("file:///Other.x", "blocker") + try { + compiler.awaitStart() + val superseded = (1..50).map { adapter.compileAsync(URI, "edit$it") } + val latest = adapter.compileAsync(URI, "latest") + assertThat(superseded).allMatch { it.isCancelled } + compiler.release.countDown() + blocker.get(10, SECONDS) + latest.get(10, SECONDS) + assertThat(compiler.seen).containsExactly("blocker", "latest") + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `closing and reopening does not revive the old analysis`() { + val compiler = PausedCompiler() + XdkAdapter(compiler::compile).use { adapter -> + val old = adapter.compileAsync(URI, "old") + try { + compiler.awaitStart() + adapter.closeDocument(URI) + assertThat(old.isCancelled).isTrue() + assertThat(adapter.getCachedResult(URI)).isNull() + val reopened = adapter.compileAsync(URI, "reopened") + compiler.release.countDown() + assertThat( + reopened + .get(10, SECONDS) + .diagnostics + .single() + .message, + ).contains("reopened") + adapter.closeDocument(URI) + assertThat(adapter.getCachedResult(URI)).isNull() + assertThat(adapter.findWorkspaceSymbols("")).isEmpty() + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `a caller can cancel an active analysis without leaving a cache entry`() { + val compiler = PausedCompiler() + XdkAdapter(compiler::compile).use { adapter -> + val cancelled = adapter.compileAsync(URI, "cancelled") + try { + compiler.awaitStart() + assertThat(cancelled.cancel(false)).isTrue() + compiler.release.countDown() + // Completion of the next job proves that the cancelled worker has finished. + adapter.compileAsync("file:///Other.x", "next").get(10, SECONDS) + assertThat(adapter.getCachedResult(URI)).isNull() + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `selection has one response per cursor even without a parsed tree`() { + XdkAdapter().use { adapter -> + val positions = listOf(Position(0, 0), Position(1, 2)) + val ranges = adapter.getSelectionRanges(URI, positions) + assertThat(ranges.map { it.range.start }).containsExactlyElementsOf(positions) + assertThat(ranges.map { it.range.end }).containsExactlyElementsOf(positions) + } + } + + @Test + fun `shutdown cancels active and queued work and releases cached analyses`() { + val compiler = PausedCompiler() + val adapter = XdkAdapter(compiler::compile) + try { + val running = adapter.compileAsync(URI, "running") + compiler.awaitStart() + val queued = adapter.compileAsync("file:///Other.x", "queued") + running.whenComplete { _, _ -> compiler.release.countDown() } + adapter.close() + assertThat(running.isCancelled).isTrue() + assertThat(queued.isCancelled).isTrue() + assertThat(adapter.getCachedResult(URI)).isNull() + assertThat(adapter.compileAsync(URI, "too late").isCompletedExceptionally).isTrue() + assertThat(compiler.seen).containsExactly("running") + } finally { + compiler.release.countDown() + adapter.close() + } + } + + private companion object { + const val URI = "file:///Lifecycle.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterTest.kt new file mode 100644 index 00000000000..5a192134c8e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterTest.kt @@ -0,0 +1,486 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.SymbolInfo +import java.util.concurrent.CancellationException + +/** + * What an editor is told when the XTC compiler itself analyses a document. + * + * Gradle supplies compiled XDK modules as declared test inputs. Missing libraries fail the suite + * instead of silently skipping compiler coverage. + */ +class XdkAdapterTest { + private fun adapter(): XdkAdapter { + CompilerTestSupport.configure() + return XdkAdapter() + } + + @Test + fun `a clean module produces no diagnostics`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Clean.x", CLEAN) + + assertThat(result.diagnostics).isEmpty() + assertThat(result.success).isTrue() + } + } + + @Test + fun `a syntax error is reported where it is, with the compiler's own code`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Broken.x", MISSING_SEMICOLON) + + assertThat(result.diagnostics).isNotEmpty() + assertThat(result.success).isFalse() + + val first = result.diagnostics.first() + assertThat(first.severity).isEqualTo(Diagnostic.Severity.ERROR) + assertThat(first.code).isNotNull() + assertThat(first.source).isEqualTo("xtc") + assertThat(first.location.uri).isEqualTo("file:///Broken.x") + // the fault is on the third line of the document, not at its start + assertThat(first.location.startLine).isEqualTo(2) + } + } + + /** + * The point of using the compiler rather than a grammar: an error that is not a syntax error. + */ + @Test + fun `an unresolvable name is reported, which a grammar could not find`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Semantic.x", UNRESOLVABLE_NAME) + + assertThat(result.diagnostics).isNotEmpty() + assertThat(result.diagnostics.map { it.message }).anySatisfy { + assertThat(it).contains("NoSuchTypeAnywhere") + } + } + } + + /** + * Every diagnostic belongs to the document it came from and is placeable in it, which is all a + * problem view needs. + */ + @Test + fun `every diagnostic is placed in the document it came from`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Broken.x", MISSING_SEMICOLON) + + assertThat(result.diagnostics).allSatisfy { d -> + assertThat(d.location.uri).isEqualTo("file:///Broken.x") + assertThat(d.location.startLine).isGreaterThanOrEqualTo(0) + assertThat(d.location.endLine).isGreaterThanOrEqualTo(d.location.startLine) + assertThat(d.message).isNotBlank() + } + } + } + + /** + * Compilations are serialised, so concurrent requests are answered correctly rather than racing + * through a compiler that was never built for two at once. + */ + @Test + fun `concurrent requests are answered one at a time`() { + adapter().use { xdk -> + val results = + (1..8) + .toList() + .parallelStream() + .map { n -> + xdk.compile("file:///Doc$n.x", MISSING_SEMICOLON) + }.toList() + + assertThat(results).hasSize(8) + results.forEachIndexed { i, r -> + assertThat(r.uri).isEqualTo("file:///Doc${i + 1}.x") + assertThat(r.diagnostics).isNotEmpty() + assertThat(r.diagnostics).allSatisfy { + assertThat(it.location.uri).isEqualTo("file:///Doc${i + 1}.x") + } + } + } + } + + /** + * Concurrent edits either produce an analysis of their own text or are explicitly cancelled. + */ + @Test + fun `concurrent edits are answered or cancelled`() { + adapter().use { xdk -> + val uri = "file:///Racing.x" + val results = + (1..6) + .toList() + .parallelStream() + .map { n -> + try { + xdk.compile(uri, "module Racing { void run() { Int x$n = ; } }") + } catch (_: CancellationException) { + null + } + }.toList() + + assertThat(results).hasSize(6) + assertThat(results.filterNotNull()).isNotEmpty().allSatisfy { r -> + assertThat(r.uri).isEqualTo(uri) + assertThat(r.diagnostics).allSatisfy { assertThat(it.location.uri).isEqualTo(uri) } + } + } + } + + /** + * The outline an editor draws. Symbols have to come from the AST rather than the compiled + * structures, because a ClassStructure knows its name, kind and members and nothing about where + * it was written - and a symbol you cannot point at is no use to an editor. + */ + @Test + fun `declarations are reported with the place they were written`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Outline.x", OUTLINE) + + val module = result.symbols.single() + assertThat(module.name).isEqualTo("Outline") + assertThat(module.kind).isEqualTo(SymbolInfo.SymbolKind.MODULE) + + val names = module.children.map { it.name } + assertThat(names).contains("Point", "helper") + + val point = module.children.single { it.name == "Point" } + assertThat(point.kind).isEqualTo(SymbolInfo.SymbolKind.CONST) + assertThat(point.location.uri).isEqualTo("file:///Outline.x") + assertThat(point.location.startLine) + .`as`("Point is declared on the second line") + .isEqualTo(1) + assertThat(point.children.map { it.name }).contains("distance") + } + } + + /** + * And the cursor can be placed in one. This is what every request phrased as "the thing I am + * pointing at" needs underneath it. + */ + @Test + fun `the innermost declaration containing a position is found`() { + adapter().use { xdk -> + xdk.compile("file:///Outline.x", OUTLINE) + + val inner = xdk.findSymbolAt("file:///Outline.x", 2, 20) + assertThat(inner?.name).`as`("inside Point.distance").isEqualTo("distance") + + val outer = xdk.findSymbolAt("file:///Outline.x", 1, 10) + assertThat(outer?.name).`as`("on Point itself").isEqualTo("Point") + + assertThat(xdk.findSymbolAt("file:///Nothing.x", 0, 0)) + .`as`("a document nobody compiled") + .isNull() + } + } + + /** A request that should not recompile gets the last analysis instead. */ + @Test + fun `the cached analysis is handed back without recompiling`() { + adapter().use { xdk -> + assertThat(xdk.getCachedResult("file:///Outline.x")).isNull() + + val fresh = xdk.compile("file:///Outline.x", OUTLINE) + val cached = xdk.getCachedResult("file:///Outline.x") + + assertThat(cached).isNotNull() + assertThat(cached!!.symbols.map { it.name }).isEqualTo(fresh.symbols.map { it.name }) + } + } + + /** + * A warning about a type is produced when the type is laid out and again whenever a later stage + * asks for that type, so the same warning reaches the listener more than once. An editor must + * not show it twice, which is why the adapter collects through the same ErrorList the + * compiler's own front end uses rather than keeping everything it hears. + */ + @Test + fun `a warning heard twice is shown once`() { + adapter().use { xdk -> + val result = xdk.compile("file:///Dup.x", DUPLICATE_ANNOTATION) + + val warnings = result.diagnostics.filter { it.code == "VERIFY-75" } + assertThat(warnings).`as`("the annotation warning, once").hasSize(1) + assertThat(warnings.single().severity).isEqualTo(Diagnostic.Severity.WARNING) + assertThat(warnings.single().message).contains("Atomic", "duplicates") + assertThat(warnings.single().location.startLine).isEqualTo(5) + assertThat(warnings.single().location.startColumn) + .isEqualTo(DUPLICATE_ANNOTATION.lines()[5].indexOf("x =")) + assertThat(warnings.single().location.endColumn) + .isEqualTo(warnings.single().location.startColumn + 1) + } + } + + // ----- what the tree can answer, without knowing what anything means ---------------------- + + /** Folding is pure shape: a block that spans more than one line can be collapsed. */ + @Test + fun `blocks and declarations spanning more than a line can be folded`() { + adapter().use { xdk -> + xdk.compile("file:///Uses.x", USES) + + val folds = xdk.getFoldingRanges("file:///Uses.x") + + assertThat(folds).isNotEmpty() + assertThat(folds).allSatisfy { assertThat(it.endLine).isGreaterThan(it.startLine) } + assertThat(folds.map { it.startLine }).`as`("the module body folds").contains(0) + } + } + + /** + * Expanding a selection is walking out through the tree, so each range has to contain the one + * it came from. Asserting the containment rather than specific spans keeps this a test of the + * walk and not of where the parser happens to put a node boundary. + */ + @Test + fun `a selection expands outward through enclosing nodes`() { + adapter().use { xdk -> + xdk.compile("file:///Uses.x", USES) + + val ranges = xdk.getSelectionRanges("file:///Uses.x", listOf(Position(3, 20))) + + val innermost = ranges.single() + var range: SelectionRange? = innermost + var steps = 0 + while (range?.parent != null) { + val parent = range.parent!! + assertThat(parent.range.start.line).isLessThanOrEqualTo(range.range.start.line) + assertThat(parent.range.end.line).isGreaterThanOrEqualTo(range.range.end.line) + range = parent + steps++ + } + assertThat(steps).`as`("more than one level to expand through").isGreaterThan(1) + } + } + + /** Highlight the declaration and both uses of the resolved local. */ + @Test + fun `the other places the same name is written are highlighted`() { + adapter().use { xdk -> + xdk.compile("file:///Uses.x", USES) + + val highlights = xdk.getDocumentHighlights("file:///Uses.x", 3, 20) + + // the declaration and both uses: a variable's name is a name in the tree too + assertThat(highlights).hasSize(3) + assertThat(highlights.map { it.range.start.line }).containsExactly(2, 3, 3) + + assertThat(xdk.getDocumentHighlights("file:///Uses.x", 0, 0)) + .`as`("not on a name") + .isEmpty() + } + } + + /** + * The half of hover that no grammar can supply: what the compiler decided the expression under + * the cursor is. + */ + @Test + fun `hover says what the expression under the cursor resolved to`() { + adapter().use { xdk -> + xdk.compile("file:///Uses.x", USES) + + val hover = xdk.getHoverInfo("file:///Uses.x", 3, 20) + + assertThat(hover).`as`("the type of count").contains("Int") + } + } + + /** + * Across the documents this server has compiled - which is the ones that have been opened, not + * the whole project. + */ + @Test + fun `symbols can be searched across compiled documents`() { + adapter().use { xdk -> + xdk.compile("file:///Outline.x", OUTLINE) + xdk.compile("file:///Uses.x", USES) + + assertThat(xdk.findWorkspaceSymbols("dist").map { it.name }) + .`as`("nested in one document") + .contains("distance") + assertThat(xdk.findWorkspaceSymbols("Uses").map { it.name }).contains("Uses") + assertThat(xdk.findWorkspaceSymbols("nothing-called-this")).isEmpty() + } + } + + // ----- what a name resolved to ------------------------------------------------------------ + + /** Narrowed uses share the original register with the source declaration. */ + @Test + fun `a local variable's declaration is found from a use of it`() { + adapter().use { xdk -> + xdk.compile("file:///Resolve.x", RESOLVE) + + val at = xdk.findDefinition("file:///Resolve.x", 14, 20) + + assertThat(at?.startLine).`as`("Int count = 1").isEqualTo(13) + assertThat(at?.uri).isEqualTo("file:///Resolve.x") + } + } + + /** + * A class declared in this document. Nothing is written as a name where a class is declared, so + * this goes through the declaration's component identity rather than through an occurrence. + */ + @Test + fun `a type's declaration is found from a use of it`() { + adapter().use { xdk -> + xdk.compile("file:///Resolve.x", RESOLVE) + + val at = xdk.findDefinition("file:///Resolve.x", 15, 8) + + assertThat(at?.startLine).`as`("const Point(Int x, Int y)").isEqualTo(1) + } + } + + /** + * The point of resolution over text search: two properties spelled the same, on different + * classes, are different things. + */ + @Test + fun `references follow what the name means, not how it is spelled`() { + adapter().use { xdk -> + xdk.compile("file:///Resolve.x", RESOLVE) + + val holders = xdk.findReferences("file:///Resolve.x", 9, 19, includeDeclaration = true) + + assertThat(holders.map { it.startLine }) + .`as`("Holder.x, declared and used - not Point.x on line 3") + .containsExactlyInAnyOrder(7, 9) + } + } + + /** + * A method call. The name in one resolves to nothing by itself - which method `sum` is depends + * on what it is called on and with what - so this goes through the invocation, which is where + * the compiler decided it. + */ + @Test + fun `a method's declaration is found from a call to it`() { + adapter().use { xdk -> + xdk.compile("file:///Resolve.x", RESOLVE) + + val at = xdk.findDefinition("file:///Resolve.x", 16, 18) + + assertThat(at?.startLine).`as`("Int sum()").isEqualTo(2) + } + } + + /** + * Declared in the core library, which this document has no place to point at. Saying nothing is + * the honest answer; jumping to another mention of Int in the same file would not be. + */ + @Test + fun `a name declared in another module has nothing here to point at`() { + adapter().use { xdk -> + xdk.compile("file:///Resolve.x", RESOLVE) + + assertThat(xdk.findDefinition("file:///Resolve.x", 13, 8)) + .`as`("bundled Int source") + .isNotNull() + assertThat(xdk.findDefinition("file:///Resolve.x", 0, 0)) + .`as`("not on anything") + .isNull() + } + } + + private companion object { + val OUTLINE = + """ + module Outline { + const Point(Int x, Int y) { + Int distance() { return x + y; } + } + void helper() {} + } + """.trimIndent() + + val CLEAN = + """ + module Clean { + void run() { + @Inject Console console; + console.print("hello"); + } + } + """.trimIndent() + + val MISSING_SEMICOLON = + """ + module Broken { + void run() { + @Inject Console console + console.print("no semicolon above"); + } + } + """.trimIndent() + + val UNRESOLVABLE_NAME = + """ + module Semantic { + void run() { + NoSuchTypeAnywhere x = 1; + } + } + """.trimIndent() + + /** `count` is written twice on the fourth line, both times as a use. */ + val USES = + """ + module Uses { + void run() { + Int count = 1; + Int total = count + count; + } + } + """.trimIndent() + + /** + * Two properties called `x` on different classes, a local used twice, and a type used where + * it is not declared. Line numbers are asserted, so the shape matters. + */ + val RESOLVE = + """ + module Resolve { + const Point(Int x, Int y) { + Int sum() { + return x + y; + } + } + class Holder { + Int x = 5; + Int get() { + return x; + } + } + void run() { + Int count = 1; + Int total = count + count; + Point p = new Point(1, 2); + Int s = p.sum(); + } + } + """.trimIndent() + + /** Redeclares an annotation the base property already has: VERIFY-75, a warning. */ + val DUPLICATE_ANNOTATION = + """ + module DupAnno { + class Base { + @Atomic Int x = 1; + } + class Derived extends Base { + @Atomic @Override Int x = 2; + } + } + """.trimIndent() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt new file mode 100644 index 00000000000..3860673e3ee --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt @@ -0,0 +1,263 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ClassStructure +import org.xvm.asm.Component +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.NewExpression +import org.xvm.compiler.ast.TypeCompositionStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.toDependency + +class XdkAnonymousConstructorTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "new Base(\"x\", te|) { String read() = text; }", + "new Base(second = \"x\", first = te|) { String read() = text; }", + "new @Tagged Base(\"x\", te|) { String read() = text; }", + "new AbstractBase(\"x\", te|) { @Override String read() = text; }", + "new Object(1, te|) { construct(Int first, String second) {} String read() = text; }", + "new Reader(\"x\", te|) { construct(String first, String second) {} @Override String read() = text; }", + "new Base(\"x\", te|) { Int next() { return ++captured; } }", + "Base value = new Base(\"x\", te|) { String read() = text; }", + "new Base(\"x\", te|) { String text = \"body\"; String read() = text; }", + ], + ) + fun `anonymous constructors fit written arguments without emitting or capturing the body`(expression: String) { + val prefix = HEADER + expression.substringBefore('|') + val suffix = expression.substringAfter('|') + "; } }" + val complete = prefix.dropLast(2) + "text" + suffix + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, complete).diagnostics).describedAs(expression).isEmpty() + val cached = adapter.compile(URI, prefix + suffix) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(expression).isNotNull() + assertThat(help!!.signatures.map { it.label }).noneMatch { it.contains(":1") } + assertThat(help.signatures.map { it.activeParameter }) + .containsOnly(if (expression.contains("first = te")) 0 else 1) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).describedAs(expression).containsExactly("text") + assertThat(items.single().textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "text", + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, complete).diagnostics).isEmpty() + } + val (analysis, errors) = analyze(prefix, suffix) + assertThat(errors.errors.map { it.code }) + .describedAs(expression) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + val creation = site.target as NewExpression + val shell = creation.children().filterIsInstance().single() + val owner = shell.component.parent + assertThat(creation.sourceBindings).isNull() + assertThat(creation.captureOrigins).isEmpty() + assertThat(site.source.toRawString()).isEqualTo(prefix + suffix) + assertThat(owner.children().filterIsInstance()) + .containsExactly(shell.component as ClassStructure) + assertThat(components(owner).filterIsInstance()).allSatisfy { method -> + assertThat(method.hasOps()).isFalse() + } + val nodes = analysis.sourceTrees().flatMap(::nodes) + assertThat(nodes.filterIsInstance()).contains(creation) + assertThat(analysis.cursorBindings()[site]!!.candidates()).isNotEmpty() + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "new AbstractBase(\"x\", te|) {}", + "new Reader(\"x\", te|) { @Override String read() = text; }", + "new Base(True, te|) { String read() = text; }", + "new Missing(\"x\", te|) {}", + "new Base(\"x\", missing = te|) {}", + ], + ) + fun `invalid anonymous construction never offers an ordinary base call`(expression: String) { + val prefix = HEADER + expression.substringBefore('|') + val suffix = expression.substringAfter('|') + "; } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, prefix.dropLast(2) + "text" + suffix).diagnostics) + .isNotEmpty() + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(expression) + .isNull() + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(expression) + .isEmpty() + } + } + + @Test + fun `empty anonymous slots show real constructors instead of an unusable generated default`() { + for (prefixValue in listOf("", "te")) { + val prefix = HEADER + "new Base($prefixValue" + val suffix = ") { String read() = text; }; } }" + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.map { it.label }) + .containsExactly("new Base(String first, String second)") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("text") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .doesNotContain("textNumber") + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["public", "protected", "private"]) + fun `anonymous superclass constructors respect dependency access`(access: String) { + CompilerTestSupport.configure() + val errors = ErrorList() + val dependency = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Library { class Base { $access construct(T first, T second) {} } }", + "Library.x", + ), + null, + errors, + ) + assertThat(errors.errors).isEmpty() + val prefix = + "module Editing { package lib import Library; void run(String text, Int textNumber) { new lib.Base(\"x\", te" + val suffix = ") { String read() = text; }; } }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(dependency.toDependency())) + val completed = adapter.compile(URI, prefix.dropLast(2) + "text" + suffix) + assertThat(completed.diagnostics.isEmpty()).isEqualTo(access != "private") + adapter.compile(URI, prefix + suffix) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help != null).isEqualTo(access != "private") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyElementsOf(if (access == "private") emptyList() else listOf("text")) + } + } + + @Test + fun `interface default constructors keep their zero argument signature`() { + val prefix = HEADER + "new Reader(" + val suffix = ") { @Override String read() = text; }; } }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + assertThat(cached.diagnostics).isEmpty() + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.map { it.label }).containsExactly("new Reader()") + assertThat(adapter.getCompletions(URI, 0, prefix.length)).isEmpty() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `editing the anonymous declaration invalidates its candidate types`() { + val prefix = HEADER + "new Object(1, te" + XdkAdapter().use { adapter -> + for ( + (type, selected) in + listOf("String" to "text", "Int" to "textNumber", "String" to "text") + ) { + val suffix = ") { construct(Int first, $type second) {} }; } }" + assertThat(adapter.compile(URI, prefix.dropLast(2) + selected + suffix).diagnostics) + .isEmpty() + val cached = adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly(selected) + assertThat( + adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures.map { + it.label + }, + ).containsExactly("new Object(Int first, $type second)") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + } + + @Test + fun `missing argument closer keeps the anonymous body and its source constructor`() { + val prefix = HEADER + "new Reader(\"x\", te" + val suffix = + " { construct(String first, String second) {} @Override String read() = text; }; } }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + assertThat(cached.diagnostics).isNotEmpty() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("text") + assertThat( + adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures.map { it.label }, + ).containsExactly("new Reader(String first, String second)") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, prefix.dropLast(2) + "text)" + suffix).diagnostics) + .isEmpty() + } + } + + @Test + fun `anonymous cursor respects cancellation and the first error budget`() { + CompilerTestSupport.configure() + val prefix = HEADER + "new Base(\"x\", te" + val suffix = ") { String read() = text; }; } }" + for ( + errors in + listOf( + ErrorList(ErrorList.FIRST_ERROR), + ErrorListener.cancellable(ErrorList()) { true }, + ) + ) { + val source = Source(prefix + suffix, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val analysis = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(analysis.pool()).isNull() + assertThat(analysis.cursorBindings()).isEmpty() + } + } + + private fun analyze( + prefix: String, + suffix: String, + ): Pair { + CompilerTestSupport.configure() + val source = Source(prefix + suffix, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + return EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) to errors + } + + private fun nodes(node: AstNode): List = listOf(node) + node.children().flatMap(::nodes) + + private fun components(component: Component): List = listOf(component) + component.children().flatMap(::components) + + private companion object { + const val URI = "untitled:Editing.x" + const val HEADER = + "module Editing { class Base { construct(T first, T second) {} } annotation Tagged into Object {} " + + "@Abstract class AbstractBase { construct(String first, String second) {} String read(); } " + + "interface Reader { String read(); } void run(String text, Int textNumber) { Int captured = 1; " + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt new file mode 100644 index 00000000000..a5718355265 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt @@ -0,0 +1,387 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import java.nio.file.Path +import java.util.concurrent.Executors + +class XdkArgumentCompletionTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @CsvSource( + delimiter = '|', + ignoreLeadingAndTrailingWhitespace = false, + value = + [ + "pair(|number", + "pair(1, |text", + "pair(second = |text", + "pair(second = \"x\", first = |number", + "box.pair(|text", + "new Box(|text", + "fn(|number", + "fn(1, |text", + ], + ) + fun `missing positional and named values offer compatible visible variables`( + call: String, + expected: String, + ) { + val prefix = "$HEADER $call" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); Int later = 1; } }") + val cached = adapter.getCachedResult(URI) + val items = adapter.getCompletions(URI, 0, prefix.length) + val names = + listOf(expected) + if (expected == "text") MODULE_NAMES + "\"\"" else listOf("0") + assertThat(items.map { it.label }) + .describedAs(call) + .containsExactlyInAnyOrderElementsOf(names) + assertThat(items.single { it.label == expected }.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length), Position(0, prefix.length)), + expected, + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + // Inserting the offered source name produces a real compilable call when all slots + // are supplied; the probe itself never claims that selection. + if (call.endsWith(", ") || call.contains("first =")) { + assertThat(adapter.compile(URI, "$prefix$expected); } }").diagnostics).isEmpty() + } + } + } + + @Test + fun `overload alternatives contribute values without choosing a winner`() { + val prefix = "$HEADER choose(" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrderElementsOf( + listOf("text", "number", "0", "\"\"") + MODULE_NAMES, + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures).hasSize(2) + } + } + + @ParameterizedTest + @CsvSource( + delimiter = '|', + value = + [ + "pair(nu|number", + "pair(1, te|text", + "pair(second = te|text", + "pair(second = \"x\", first = nu|number", + "box.pair(te|text", + "new Box(te|text", + "new Box(value = te|text", + "fn(nu|number", + "fn(1, te|text", + ], + ) + fun `typed argument prefixes offer fitting values and replace exactly their token`( + call: String, + expected: String, + ) { + val prefix = "$HEADER Int textNumber = 1; String numberText = \"x\"; $call" + XdkAdapter().use { adapter -> + val original = "$prefix); Int later = 1; } }" + adapter.compile(URI, original) + val cached = adapter.getCachedResult(URI) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).describedAs(call).containsExactly(expected) + assertThat(items.single().textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + expected, + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures).hasSize(1) + if (call !in listOf("pair(nu", "pair(second = te", "fn(nu")) { + assertThat( + adapter + .compile( + URI, + original.replaceRange(prefix.length - 2, prefix.length, expected), + ).diagnostics, + ).isEmpty() + } + } + } + + @Test + fun `typed prefix preserves fitting overload alternatives and rejects other same prefix values`() { + val prefix = + "$HEADER Int valueNumber = 1; String valueText = \"x\"; Boolean valueFlag = True; choose(va" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("valueNumber", "valueText") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures).hasSize(2) + } + } + + @Test + fun `no value is inserted into a completed unknown or incompatible argument slot`() { + XdkAdapter().use { adapter -> + for ( + call in + listOf( + "pair(1", + "pair(1, \"x\", ", + "pair(unknown =", + "pair(first = 1, first =", + "pair(True, ", + "missing(", + "pair(unknown = te", + "pair(first = 1, first = nu", + "pair(True, te", + "pair(1, missing", + "missing(te", + ) + ) { + val prefix = "$HEADER $call" + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(call) + .isEmpty() + } + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "generic(", + "genericPair(\"x\", ", + "widen(", + "generic(t", + "genericPair(\"x\", t", + "widen(n", + ], + ) + fun `inference and conversions agree with compiling each proposed source value`(call: String) { + val prefix = "$HEADER $call" + val typed = call.substringAfterLast('(').substringAfterLast(',').trim() + val before = prefix.dropLast(typed.length) + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val offered = adapter.getCompletions(URI, 0, prefix.length).map { it.label } + val compilable = + ( + listOf( + "number", + "text", + "flag", + "box", + "fn", + "classByName", + "classes", + "modulesByPath", + "version", + "True", + "False", + "Null", + "0", + "\"\"", + "0.0", + "' '", + "#00", + "[]", + "Map:[]", + "Tuple:()", + "this", + "this.Editing", + ) + MODULE_NAMES + ).filter { it.startsWith(typed) } + .filter { variable -> + adapter.compile(URI, "$before$variable); } }").diagnostics.isEmpty() + } + assertThat(offered) + .describedAs(call) + .isNotEmpty() + .containsExactlyInAnyOrderElementsOf(compilable) + if (call == "widen(") assertThat(offered).containsExactly("number", "0") + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "v", "t"]) + fun `argument values honor narrowing assignment state and lexical shadowing`(typed: String) { + XdkAdapter().use { adapter -> + for ( + (setup, expected) in + listOf( + "" to listOf("text"), + "if (value.is(String)) { " to listOf("text", "value"), + "Int text; " to emptyList(), + "{ String closed = \"x\"; } " to listOf("text"), + ) + ) { + val prefix = + "module Editing { void take(String value) {} void run(Object value, String text) { ${setup}take($typed" + val suffix = if (setup.startsWith("if")) "); } } }" else "); } }" + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .describedAs(setup) + .containsExactlyInAnyOrderElementsOf( + (expected + MODULE_NAMES + "\"\"").filter { it.startsWith(typed) }, + ) + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "te"]) + fun `proposed values leave original arguments diagnostics and selected call facts untouched`(typed: String) { + CompilerTestSupport.configure() + val prefix = "$HEADER pair(second = $typed" + val text = "$prefix); } }" + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(site.arguments).isEmpty() + assertThat(site.source.toRawString()).isEqualTo(text) + val binding = analysis.cursorBindings()[site]!! + assertThat(binding.argumentValues().map { it.name() }).containsExactly("text") + assertThat(binding.candidates()).hasSize(1) + assertThatThrownBy { (binding.argumentValues() as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + val snapshot = analysis.semanticSnapshot(errors) + val copied = snapshot.sites.single() + assertThat(copied.memberPrefix!!.text).isEqualTo(typed) + assertThat(copied.memberPrefix.range.start.column).isEqualTo(prefix.length - typed.length) + assertThat(copied.memberPrefix.range.end.column).isEqualTo(prefix.length) + assertThat(copied.pendingArgumentName).isEqualTo("second") + assertThat(snapshot.semantics.calls).isEmpty() + assertThat(snapshot.semantics.functionCalls).isEmpty() + XdkAdapter().use { adapter -> adapter.compile("untitled:Other.x", "module Other {}") } + Executors.newSingleThreadExecutor().use { executor -> + assertThat( + executor + .submit> { + snapshot.sites + .single() + .argumentValues + .map { it.name } + }.get(), + ).containsExactlyInAnyOrderElementsOf( + listOf("text") + MODULE_NAMES.filter { it.startsWith(typed) }, + ) + } + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + + @ParameterizedTest + @ValueSource(strings = ["", "value"]) + fun `argument completion tracks unsaved module signatures`(typed: String) { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { void take(Int value) {} } }") + val prefix = + "class Child extends Base { void run(Int valueNumber, String valueText) { take($typed" + member.writeText("$prefix); } }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val uri = member.toURI().toString() + adapter.compile(rootUri, root.readText().replace("Int value", "String value")) + adapter.compile(uri, member.readText()) + val cached = adapter.getCachedResult(uri) + assertThat(adapter.getCompletions(uri, 0, prefix.length).map { it.label }) + .containsExactlyElementsOf( + listOf("valueText") + if (typed.isEmpty()) listOf("\"\"") else emptyList(), + ) + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + adapter.compile(rootUri, root.readText()) + assertThat(adapter.getCompletions(uri, 0, prefix.length).map { it.label }) + .containsExactlyElementsOf( + listOf("valueNumber") + if (typed.isEmpty()) listOf("0") else emptyList(), + ) + assertThat(root.readText()).contains("Int value") + } + } + + @ParameterizedTest + @ValueSource(strings = ["te", "\\u0074e"]) + fun `prefix edits use original UTF16 ranges with CRLF and escaped identifiers`(typed: String) { + val line = "void run(String text, Int textNumber) { /* 😀 */ take(value = $typed" + val header = "module Editing {\r\nvoid take(String value) {}\r\n" + XdkAdapter().use { adapter -> + val original = "$header$line); } }" + val cached = adapter.compile(URI, original) + val edit = adapter.getCompletions(URI, 2, line.length).single().textEdit!! + assertThat(edit) + .isEqualTo( + TextEdit( + Range(Position(2, line.length - typed.length), Position(2, line.length)), + "text", + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter + .compile(URI, "$header${line.dropLast(typed.length)}${edit.newText}); } }") + .diagnostics, + ).isEmpty() + } + } + + @Test + fun `argument values fit their calls including compound operand prefixes`() { + XdkAdapter().use { adapter -> + for ( + (call, suffix, expected) in + listOf( + Triple("pair(nu", ", text)", "number"), + Triple("pair(1, (te", "))", "text"), + Triple("pair(1 + nu", ", text)", "number"), + Triple("pair(1, text.si", ")", null), + ) + ) { + val prefix = "$HEADER Int textNumber = 1; String numberText = \"x\"; $call" + adapter.compile(URI, "$prefix$suffix; } }") + val names = adapter.getCompletions(URI, 0, prefix.length).map { it.label } + if (expected == null) { + assertThat(names).describedAs(call).isEmpty() + } else { + assertThat(names).describedAs(call).contains(expected) + } + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + val MODULE_NAMES = listOf("qualifiedName", "simpleName") + const val HEADER = + "module Editing { void pair(Int first, String second) {} " + + "void choose(Int value) {} void choose(String value) {} " + + " void generic(U value) {} void genericPair(U first, U second) {} void widen(Int128 value) {} " + + "class Box { construct(T value) {} void pair(T value) {} } " + + "void run(Int number, String text, Boolean flag, Box box, function Int(Int, String) fn) { Int unread;" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt new file mode 100644 index 00000000000..835c6326059 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt @@ -0,0 +1,75 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkArgumentContextTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "pair(nu§, \"x\")", + "pair((nu§), \"x\")", + "pair(((nu§)), \"x\")", + "pair(§, \"x\")", + "pair(number = nu§, text = \"x\")", + "pair(number = §, text = \"x\")", + "pair(box.nu§, \"x\")", + "pair((box.nu§), \"x\")", + "pair(number = box.nu§, text = \"x\")", + "fn(nu§, \"x\")", + "fn((nu§), \"x\")", + "new Pair(nu§, \"x\")", + ], + ) + fun `argument candidates fit the entire written call and preserve surrounding syntax`(call: String) { + val marked = HEADER + call + "; } }" + val at = marked.indexOf('§') + val text = marked.replace("§", "") + val typed = if (marked[at - 1] == 'u') 2 else 0 + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, text) + val items = adapter.getCompletions(URI, 0, at) + assertThat(items.map { it.label }) + .describedAs(call) + .contains("number") + .doesNotContain("numberText", "numberHidden") + val edit = items.single { it.label == "number" }.textEdit + assertThat(edit) + .isEqualTo(TextEdit(Range(Position(0, at - typed), Position(0, at)), "number")) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter.compile(URI, text.replaceRange(at - typed, at, "number")).diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "pair(nu§, True)", + "pair((nu§), True)", + "pair(box.nu§, True)", + "pair(number = nu§, unknown = 1)", + ], + ) + fun `later incompatible arguments cannot produce a fitting suggestion`(call: String) { + val marked = HEADER + call + "; } }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Arguments.x" + const val HEADER = + "module Arguments { class Pair(Int number, String text) {} " + + "class Box { Int number = 1; String numberText = \"x\"; private Int numberHidden = 2; } " + + "void pair(Int number, String text) {} void run(Int number, String numberText, Box box, " + + "function void(Int, String) fn) { " + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt new file mode 100644 index 00000000000..25acf518def --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt @@ -0,0 +1,183 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.NewExpression +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkArrayDimensionTest { + @ParameterizedTest + @ValueSource( + strings = + ["new Int[nu|]", "new Int[nu|", "Int[] result = new Int[nu|]", "new String[nu|](\"x\")"], + ) + fun `dimension prefixes fit the size parameter and replace only the original token`(expression: String) { + val prefix = HEADER + expression.substringBefore('|') + val suffix = expression.substringAfter('|') + "; } }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(expression).isNotNull() + assertThat(help!!.signatures.map { it.activeParameter }).containsOnly(0) + assertThat(help.signatures.map { it.parameters.first().label }).containsOnly("Int size") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).describedAs(expression).containsExactly("number") + assertThat(items.single().textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "number", + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + val correctedSuffix = if (']' in suffix) suffix else "]$suffix" + assertThat( + adapter + .compile(URI, prefix.dropLast(2) + "number" + correctedSuffix) + .diagnostics, + ).isEmpty() + } + } + + @Test + fun `empty size slot exposes the fixed size constructor without copy or mutability overloads`() { + val prefix = HEADER + "new String[" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix]; } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures).hasSize(1) + assertThat( + help.signatures + .single() + .parameters + .first() + .label, + ).isEqualTo("Int size") + assertThat(help.signatures.single().activeParameter).isZero() + val names = adapter.getCompletions(URI, 0, prefix.length).map { it.label } + assertThat(names).contains("number").doesNotContain("numberText", "numberArray") + } + } + + @ParameterizedTest + @ValueSource(strings = ["new String[\"x\"|]", "new String[2, nu|]", "new Missing[nu|]"]) + fun `invalid dimensions do not claim an applicable constructor`(expression: String) { + val prefix = HEADER + expression.substringBefore('|') + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + expression.substringAfter('|') + "; } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(expression) + .isNull() + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(expression) + .isEmpty() + } + } + + @Test + fun `a prefix before another dimension keeps lexical completion without constructor fitting`() { + val prefix = HEADER + "new Int[nu" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix, 2]; } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("number", "numberText", "numberArray") + } + } + + @Test + fun `dimensions use compiler readability narrowing and property validation`() { + val prefix = + "module Dimensions { Int countProperty = 2; String countText = \"x\"; " + + "void run(Int? countMaybe, Int countParameter) { Int countUnassigned; " + + "if (countMaybe != Null) { new Int[co" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix]; } } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("countMaybe", "countParameter", "countProperty") + val changed = + prefix + .replace("Int countProperty", "String countProperty") + .replace("= 2;", "= \"two\";") + adapter.compile(URI, "$changed]; } } }") + assertThat(adapter.getCompletions(URI, 0, changed.length).map { it.label }) + .containsExactlyInAnyOrder("countMaybe", "countParameter") + } + } + + @ParameterizedTest + @ValueSource(strings = ["new Int[word.si|]", "new Int[word.si|"]) + fun `member cursor inside a dimension retains its own receiver`(expression: String) { + val prefix = + "module Dimensions { void run(String word) { " + expression.substringBefore('|') + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + expression.substringAfter('|') + "; } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("size") + } + } + + @Test + fun `complete numeric size fits while an incompatible literal is rejected`() { + val prefix = HEADER + "new Int[2" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix]; } }") + assertThat( + adapter + .getSignatureHelp(URI, 0, prefix.length)!! + .signatures + .single() + .activeParameter, + ).isZero() + } + } + + @Test + fun `embedding dimension query preserves source ownership and reports its hole once`() { + CompilerTestSupport.configure() + val prefix = HEADER + "new String[nu" + val text = "$prefix]; } Int later = 42; }" + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(site.isCall).isTrue() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat((site.target as NewExpression).sourceBindings).isNull() + assertThat(site.target.parent).isSameAs(site) + assertThat(site.leadingArguments).isEmpty() + assertThat(analysis.cursorBindings()[site]!!.candidates()).hasSize(1) + assertThat(analysis.cursorBindings()[site]!!.argumentValues().map { it.name() }) + .containsExactly("number") + for ( + listener in + listOf( + ErrorList(ErrorList.FIRST_ERROR), + ErrorListener.cancellable(ErrorList()) { true }, + ) + ) { + source.reset() + val stopped = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, listener) + assertThat(stopped.pool()).isNull() + assertThat(stopped.cursorBindings()).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Dimensions.x" + const val HEADER = + "module Dimensions { void run(Int number, String numberText, String[] numberArray) { " + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt new file mode 100644 index 00000000000..232a4f8b38e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt @@ -0,0 +1,218 @@ +package org.xvm.lsp.adapter + +import com.google.gson.Gson +import com.google.gson.JsonObject +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkBuildModel +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkBuildModelTest { + @TempDir lateinit var directory: Path + + @BeforeEach + fun canonicalDirectory() { + directory = directory.toRealPath() + } + + @Test + fun `binary module path is imported and removing the model retires its artifacts`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule( + Source("module BinaryLibrary { static Int value() = 7; }", "BinaryLibrary.x"), + null, + errors, + ) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + val binary = + directory.resolve("BinaryLibrary.xtc").toFile().apply { + writeBytes(compilation.toDependency().bytes()) + } + val consumer = + source( + "Consumer.x", + "module Consumer { package lib import BinaryLibrary; Int run() = lib.value(); }", + ) + val inputs = + entry("app", "main", listOf(consumer)).apply { + add("modulePath", Gson().toJsonTree(listOf(binary.toURI().toString()))) + } + XdkAdapter().use { adapter -> + adapter.replaceBuildInputs(model(inputs).resolve()) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + adapter.replaceSourceModules(listOf(XdkSourceModule("Consumer", consumer))) + assertThat( + adapter.compile(consumer, Path.of(URI(consumer)).toFile().readText()).success, + ).isFalse() + } + } + + @Test + fun `evaluated source dependencies and processed resources compile across projects`() { + CompilerTestSupport.configure() + val library = + source("library/code/Assets.x", "module Assets { static String text() = $./data.txt; }") + val consumer = + source( + "app/code/App.x", + "module App { package assets import Assets; String run() = assets.text(); }", + ) + val processed = directory.resolve("outside-layout/processed").toFile().apply { mkdirs() } + processed.resolve("data.txt").writeText("filtered resource") + val inputs = + model( + entry( + "library", + "main", + listOf(library), + resources = listOf(processed.toURI().toString()), + ), + entry("app", "main", listOf(consumer), dependencies = listOf("library")), + ).resolve() + assertThat(inputs.modules.single { it.name == "App" }.dependencies) + .containsExactly("Assets") + XdkAdapter().use { adapter -> + adapter.replaceBuildInputs(inputs) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat( + adapter.effectiveSourceModules().single { it.name == "Assets" }.resourceRoots, + ).containsExactly(processed.toURI().toString()) + adapter.replaceBuildInputs( + model(entry("library", "main", listOf(library), resources = emptyList())).resolve(), + ) + assertThat(adapter.effectiveSourceModules().map { it.name }).containsExactly("Assets") + assertThat( + adapter + .workspaceDiagnosticsAsync() + .get(30, SECONDS) + .single() + .success, + ).isFalse() + adapter.replaceBuildInputs(inputs) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `test sees own main and declared project dependencies but unrelated main does not`() { + val main = source("main/Main.x", "module Main {}") + val tests = source("test/Tests.x", "module Tests { package main import Main; }") + val unrelated = source("unrelated/Other.x", "module Other { package main import Main; }") + val inputs = + model( + entry("one", "main", listOf(main)), + entry("one", "test", listOf(tests)), + entry("two", "main", listOf(unrelated)), + ).resolve() + assertThat(inputs.modules.single { it.name == "Tests" }.dependencies) + .containsExactly("Main") + assertThat(inputs.modules.single { it.name == "Other" }.dependencies).isEmpty() + } + + @Test + fun `member imports contribute dependencies and excluded files are not mistaken for roots`() { + val library = source("Library.x", "module Library {}") + val app = source("App.x", "module App {}") + val member = source("App/Member.x", "class Member { package lib import Library; }") + val appEntry = + entry("app", "main", listOf(app, member), dependencies = listOf("lib")).apply { + add("moduleRoots", Gson().toJsonTree(listOf(app))) + } + val inputs = model(entry("lib", "main", listOf(library)), appEntry).resolve() + assertThat(inputs.modules.map { it.name }).containsExactlyInAnyOrder("Library", "App") + assertThat(inputs.modules.single { it.name == "App" }.dependencies) + .containsExactly("Library") + } + + @Test + fun `malformed overlapping and cyclic imports preserve the installed graph`() { + CompilerTestSupport.configure() + val root = source("Good.x", "module Good {}") + val valid = entry("app", "main", listOf(root)) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("Good", root))) + listOf( + valid.deepCopy().apply { addProperty("sourceFiles", "not an array") }, + valid.deepCopy().apply { + add("sourceFiles", Gson().toJsonTree(listOf("https://example.org/Bad.x"))) + }, + valid.deepCopy().apply { addProperty("projectId", 1) }, + ).forEach { bad -> + assertThatThrownBy { adapter.replaceBuildInputs(model(bad).resolve()) } + .isInstanceOf(IllegalArgumentException::class.java) + } + assertThatThrownBy { model(valid, valid).resolve() }.hasMessageContaining("Duplicate") + assertThatThrownBy { + model(valid, valid.deepCopy().apply { addProperty("projectId", "other") }) + .resolve() + }.hasMessageContaining("Overlapping") + val first = source("First.x", "module First { package second import Second; }") + val second = source("Second.x", "module Second { package first import First; }") + assertThatThrownBy { + adapter.replaceBuildInputs( + model(entry("app", "main", listOf(first, second))).resolve(), + ) + }.hasMessageContaining("Cyclic") + assertThat(adapter.effectiveSourceModules().map { it.name }).containsExactly("Good") + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + private fun source( + path: String, + text: String, + ): String = + directory + .resolve(path) + .toFile() + .apply { + parentFile.mkdirs() + writeText(text) + }.toURI() + .toString() + + private fun entry( + project: String, + sourceSet: String, + sources: List, + resources: List = emptyList(), + dependencies: List = emptyList(), + ): JsonObject = + Gson() + .toJsonTree( + mapOf( + "projectId" to project, + "sourceSet" to sourceSet, + "sourceFiles" to sources, + "moduleRoots" to sources, + "resourceRoots" to resources, + "projectDependencies" to dependencies, + "modulePath" to emptyList(), + ), + ).asJsonObject + + private fun model(vararg entries: JsonObject): XdkBuildModel = + XdkBuildModel.read( + listOf( + JsonObject().apply { + addProperty("schemaVersion", 1) + add("sourceSets", Gson().toJsonTree(entries.toList())) + }, + ), + ) +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt new file mode 100644 index 00000000000..d5e0f52157c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt @@ -0,0 +1,165 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkCallHierarchyTest { + @TempDir lateinit var directory: Path + + @Test + fun `selected overloads group repeated call sites and recursive edges retain exact owners`() { + val source = + """ + module Calls { + Int /*integer*/pick(Int value) = value; + String /*string*/pick(String value) = value; + Int /*run*/run() { Int n = /*one*/pick(1); return /*two*/pick(n); } + Int /*recursive*/recursive(Int n) { if (n == 0) { return 0; } return recursive(n - 1); } + } + """.trimIndent() + withSource(source) { adapter -> + val target = prepare(adapter, source, "integer") + val incoming = adapter.getIncomingCalls(target).single() + assertThat(incoming.from.name).isEqualTo("run") + assertThat(incoming.fromRanges) + .containsExactly(range(source, "one", "pick"), range(source, "two", "pick")) + val outgoing = adapter.getOutgoingCalls(prepare(adapter, source, "run")).single() + assertThat(outgoing.to).isEqualTo(target) + assertThat(outgoing.fromRanges).isEqualTo(incoming.fromRanges) + assertThat(adapter.getIncomingCalls(prepare(adapter, source, "string"))).isEmpty() + val recursive = prepare(adapter, source, "recursive") + assertThat(adapter.getIncomingCalls(recursive).single().from).isEqualTo(recursive) + assertThat(adapter.getOutgoingCalls(recursive).single().to).isEqualTo(recursive) + } + } + + @Test + fun `lambda calls belong to the lambda and dynamic function invocation is not guessed`() { + val source = + """ + module Calls { + Int /*leaf*/leaf(Int value) = value; + Int /*outer*/run(Int captured) { + function Int(Int) fn = (Int n) -> /*call*/leaf(n + captured); + return fn(1); + } + } + """.trimIndent() + withSource(source) { adapter -> + val leaf = prepare(adapter, source, "leaf") + val caller = adapter.getIncomingCalls(leaf).single().from + assertThat(caller.name).isEqualTo("") + assertThat(caller.range.start.line).isEqualTo(3) + assertThat(adapter.getOutgoingCalls(caller).single().to).isEqualTo(leaf) + assertThat(adapter.getOutgoingCalls(prepare(adapter, source, "outer"))).isEmpty() + } + } + + @Test + fun `anonymous method calls do not become calls from the factory`() { + val source = + """ + module Calls { + interface Reader { Int read(); } + static Int /*leaf*/leaf() = 1; + Reader /*factory*/make() { + return new Reader() { @Override Int /*body*/read() = leaf(); }; + } + } + """.trimIndent() + withSource(source) { adapter -> + val incoming = adapter.getIncomingCalls(prepare(adapter, source, "leaf")).single() + assertThat(incoming.from.selectionRange).isEqualTo(range(source, "body", "read")) + assertThat(adapter.getOutgoingCalls(prepare(adapter, source, "factory"))).isEmpty() + } + } + + @Test + fun `interface calls record the static selected declaration rather than possible runtime overrides`() { + val source = + """ + module Calls { + interface Named { String /*interface*/name(); } + class Value implements Named { @Override String /*override*/name() = "value"; } + String /*caller*/run(Named value) = value.name(); + } + """.trimIndent() + withSource(source) { adapter -> + val outgoing = adapter.getOutgoingCalls(prepare(adapter, source, "caller")).single() + assertThat(outgoing.to).isEqualTo(prepare(adapter, source, "interface")) + assertThat(adapter.getIncomingCalls(prepare(adapter, source, "override"))).isEmpty() + } + } + + @Test + fun `closed member callers use their own source and old hierarchy items expire after edits`() { + directory = directory.toRealPath() + val root = directory.resolve("Calls.x").toFile() + val member = directory.resolve("Calls/Worker.x").toFile() + val source = "module Calls { static Int /*target*/target(Int n) = n; }" + root.writeText(source) + member.parentFile.mkdirs() + member.writeText("class Worker { Int run() = target(1); }") + XdkAdapter().use { adapter -> + val uri = root.toURI().toString() + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val target = prepare(adapter, source, "target", uri) + val caller = adapter.getIncomingCalls(target).single().from + assertThat(caller.uri).isEqualTo(member.toURI().toString()) + assertThat( + adapter + .getOutgoingCalls(caller) + .single() + .to.uri, + ).isEqualTo(uri) + assertThat(adapter.compile(uri, "\n$source").diagnostics).isEmpty() + assertThat(adapter.getIncomingCalls(target)).isEmpty() + assertThat(adapter.getOutgoingCalls(caller)).isEmpty() + val current = prepare(adapter, "\n$source", "target", uri) + assertThat(adapter.getIncomingCalls(current)).hasSize(1) + assertThat(adapter.compile(member.toURI().toString(), "class Worker {").success) + .isFalse() + assertThat(adapter.getIncomingCalls(current)).isEmpty() + } + } + + private fun withSource( + source: String, + test: (XdkAdapter) -> Unit, + ) { + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, source) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + test(adapter) + } + } + + private fun prepare( + adapter: XdkAdapter, + source: String, + marker: String, + uri: String = URI, + ): CallHierarchyItem { + val at = range(source, marker, "").start + return adapter.prepareCallHierarchy(uri, at.line, at.column).single() + } + + private fun range( + source: String, + marker: String, + name: String, + ): Range { + val token = "/*$marker*/" + val offset = source.indexOf(token).also { check(it >= 0) } + token.length + val line = source.take(offset).count { it == '\n' } + val column = offset - source.lastIndexOf('\n', offset - 1) - 1 + return Range(Position(line, column), Position(line, column + name.length)) + } + + private companion object { + const val URI = "file:///Calls.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallableBreadthTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallableBreadthTest.kt new file mode 100644 index 00000000000..57f94587b8d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallableBreadthTest.kt @@ -0,0 +1,45 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +/** Callable patterns used by platform callbacks; only a resolved function type authorizes a call. */ +class XdkCallableBreadthTest { + @ParameterizedTest + @ValueSource( + strings = [ + "function Int(Int) fn = &inc; fn(§);", + "function Int(Int)[] callbacks = [n -> n + 1]; callbacks[0](§);", + "function Int(Int)? fn = candidate; if (fn != Null) { fn(§); }", + "make()(§);", + ], + ) + fun `function values returned indexed captured and narrowed preserve signatures and argument fitting`(body: String) { + val marked = + "module Editing { Int inc(Int n) = n + 1; function Int(Int) make() = n -> n + 1; " + + "void run(function Int(Int)? candidate) { $body } }" + val at = marked.indexOf('§') + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, marked.replace("§", "0")).diagnostics).describedAs(body).isEmpty() + val cached = adapter.compile(URI, source) + val help = adapter.getSignatureHelp(URI, 0, at)!! + assertThat( + help.signatures + .single() + .parameters + .single() + .label, + ).isEqualTo("Int") + assertThat(help.activeParameter).isZero() + assertThat(adapter.getCompletions(URI, 0, at).map { it.label }).contains("0").doesNotContain("True", "Null", "\"\"") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCandidateProbeTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCandidateProbeTest.kt new file mode 100644 index 00000000000..5557ace9e2e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCandidateProbeTest.kt @@ -0,0 +1,169 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.partial.IncompleteStatement + +/** Method and constructor probes must share fitting rules without publishing speculative syntax. */ +class XdkCandidateProbeTest { + @ParameterizedTest + @ValueSource(strings = ["pair", "new Box"]) + fun `named generic candidates preserve source order and leave defaults unwritten`(callee: String) { + val analysis = analyze("$callee(second = text, first = text, §)") + val site = analysis.sites().single() + val candidates = analysis.cursorBindings().getValue(site).candidates() + val candidate = candidates.single() + assertThat( + candidate + .signature() + .rawParams + .takeLast(3) + .map { it.valueString }, + ).containsExactly("String", "String", "Int") + assertThat(candidate.arguments().map { it.parameterIndex() }).containsExactly(1, 0) + assertThat(candidate.arguments().map { it.label().name() }).containsExactly("second", "first") + assertThat(site.arguments).hasSize(2) + site.arguments.zip(candidate.arguments()).forEach { (written, mapping) -> + assertThat(mapping.startPosition()).isEqualTo(written.startPosition) + assertThat(mapping.endPosition()).isEqualTo(written.endPosition) + assertThat(written.parent).isSameAs(site) + } + assertThat(candidate.converting()).isFalse() + assertThatThrownBy { (candidates as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + assertThatThrownBy { (candidate.arguments() as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + assertNoSelectedCall(analysis, site) + } + + @ParameterizedTest + @ValueSource(strings = ["widen", "new Wide"]) + fun `conversion candidates retain their expected type without converting source arguments`(callee: String) { + val analysis = analyze("$callee(number, §)") + val site = analysis.sites().single() + val candidate = + analysis + .cursorBindings() + .getValue(site) + .candidates() + .single() + assertThat(candidate.converting()).isTrue() + assertThat(candidate.signature().rawParams.map { it.valueString }) + .containsExactly("Int128", "String") + assertThat(candidate.arguments().single().parameterIndex()).isZero() + assertThat( + site.arguments + .single() + .type.valueString, + ).isEqualTo("Int") + assertThat(site.arguments.single().parent).isSameAs(site) + assertNoSelectedCall(analysis, site) + } + + @ParameterizedTest + @ValueSource(strings = ["apply", "new Apply"]) + fun `captured lambda trials leave the written lambda unvalidated and independently cloned`(callee: String) { + val analysis = analyze("$callee((Int value) -> value + number, §)") + val site = analysis.sites().single() + val candidate = + analysis + .cursorBindings() + .getValue(site) + .candidates() + .single() + assertThat( + candidate + .signature() + .rawParams + .last() + .valueString, + ).isEqualTo("Int") + val lambda = site.arguments.single() as LambdaExpression + assertThat(lambda.isValidated).isFalse() + assertThat(lambda.parent).isSameAs(site) + val copiedSite = site.clone() as IncompleteStatement + val copiedLambda = copiedSite.arguments.single() as LambdaExpression + assertThat(copiedLambda).isNotSameAs(lambda) + assertThat(copiedLambda.parent).isSameAs(copiedSite) + assertThat(copiedLambda.isValidated).isFalse() + assertThat(site.toDumpString()).isEqualTo(copiedSite.toDumpString()) + assertNoSelectedCall(analysis, site) + } + + @ParameterizedTest + @ValueSource(strings = ["pair", "new Box"]) + fun `aborted candidate attempts publish no facts and do not poison the next attempt`(callee: String) { + val call = "$callee(second = text, first = text, §)" + listOf(ErrorList(ErrorList.FIRST_ERROR), ErrorListener.cancellable(ErrorList()) { true }) + .forEach { listener -> + val stopped = analyze(call, listener) + assertThat(stopped.pool()).isNull() + assertThat(stopped.cursorBindings()).isEmpty() + assertThat(stopped.callBindings()).isEmpty() + val recovered = analyze(call) + assertThat(recovered.cursorBindings().getValue(recovered.sites().single()).candidates()) + .hasSize(1) + } + } + + private fun assertNoSelectedCall( + analysis: EmbeddingSupport.PartialAnalysis, + site: IncompleteStatement, + ) { + assertThat(analysis.callBindings()).isEmpty() + // Ordinary calls elsewhere in the module still publish their real selected bindings. + val ordinaryCall = analysis.functionBindings().keys.single() + assertThat(ordinaryCall.source.toString(ordinaryCall.startPosition, ordinaryCall.endPosition)) + .isEqualTo("fn(second)") + assertThat(ordinaryCall.endPosition).isLessThan(site.startPosition) + assertThat(site.target.isValidated).isFalse() + } + + private fun analyze( + call: String, + listener: ErrorListener? = null, + ): EmbeddingSupport.PartialAnalysis { + CompilerTestSupport.configure() + val marked = + """ + module CandidateProbe { + class Box { construct(T first, T second, Int count = 0) {} } + class Wide { construct(Int128 first, String second) {} } + class Apply { construct(function Int(Int) fn, Int second) {} } + T pair(T first, T second, Int count = 0) = first; + void widen(Int128 first, String second) {} + Int apply(function Int(Int) fn, Int second) = fn(second); + void run(String text, Int number) { + $call; + } + } + """.trimIndent() + val text = marked.replace("§", "") + val source = Source(text, "untitled:CandidateProbe.x") + repeat(marked.indexOf('§')) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, listener ?: errors) + if (listener == null) { + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + assertThat( + analysis + .sites() + .single() + .source + .toRawString(), + ).isEqualTo(text) + } + return analysis + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt new file mode 100644 index 00000000000..e06019f87a1 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt @@ -0,0 +1,182 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path +import kotlin.io.path.readText + +class XdkColorValueTest { + @Test + fun `fixture exposes initializers and reordered named channels only when enabled`() { + val source = Path.of(System.getProperty("xtc.composite.root"), "lang/test-fixtures/color-values/ColorPrototype.x").readText() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.capabilities).doesNotContain(AdapterCapability.DOCUMENT_COLOR) + assertThat(adapter.getDocumentColors(URI)).isEmpty() + } + XdkAdapter(colorPrototype = true).use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.capabilities).contains(AdapterCapability.DOCUMENT_COLOR) + assertThat(adapter.getDocumentColors(URI).map { it.color }) + .containsExactlyInAnyOrder(rgba(255, 128, 0), rgba(200, 80, 40, 128)) + } + } + + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + fun `picker edits preserve named order comments aliases and UTF-16 source text`(newline: String) { + val source = + """ + module ColorPrototype { + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + typedef Rgba as Tint; + Tint sample() { + String marker = "😀"; return new Tint(blue = /* keep blue */ 0x40, + red = 100, green = 2_0); + } + } + """.trimIndent().replace("\n", newline) + XdkAdapter(colorPrototype = true).use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val color = adapter.getDocumentColors(URI).single() + assertThat(color.color).isEqualTo(rgba(100, 20, 64)) + val wanted = rgba(1, 2, 3, 64) + val edit = adapter.getColorPresentations(URI, color.range, wanted).single().textEdit + val expected = source.replace("0x40", "3").replace("red = 100", "red = 1").replace("green = 2_0)", "green = 2, alpha = 64)") + val changed = apply(source, edit) + assertThat(changed).isEqualTo(expected) + assertThat(adapter.compile(URI, changed).diagnostics).isEmpty() + val after = adapter.getDocumentColors(URI).single() + assertThat(after.color).isEqualTo(wanted) + assertThat(apply(changed, adapter.getColorPresentations(URI, after.range, wanted).single().textEdit)).isEqualTo(changed) + } + } + + @Test + fun `resolved binary import alias works and an unrelated same-named type does not`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val library = EmbeddingSupport.instance().compileModule(Source(library(), URI), null, errors) + assertThat(library.succeeded()).describedAs(errors.errors.toString()).isTrue() + val binary = XdkDependency.fromBinary(library.toDependency().bytes()) + val source = + """ + module Consumer { + package colors import ColorPrototype; + import colors.Rgba as Tint; + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + Tint chosen() = new Tint(10, 20, 30); + Rgba unrelated() = new Rgba(10, 20, 30); + } + """.trimIndent() + XdkAdapter(colorPrototype = true).use { adapter -> + adapter.replaceDependencies(listOf(binary)) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + val color = adapter.getDocumentColors(CONSUMER).single() + assertThat(color.color).isEqualTo(rgba(10, 20, 30)) + val changed = apply(source, adapter.getColorPresentations(CONSUMER, color.range, rgba(40, 50, 60)).single().textEdit) + assertThat(changed).isEqualTo(source.replace("new Tint(10, 20, 30)", "new Tint(40, 50, 60)")) + assertThat(adapter.compile(CONSUMER, changed).diagnostics).isEmpty() + assertThat(adapter.getDocumentColors(CONSUMER).single().color).isEqualTo(rgba(40, 50, 60)) + } + } + + @ParameterizedTest + @ValueSource(strings = ["red, 2, 3", "one, 2, 3", "1 + 2, 2, 3"]) + fun `dynamic values references and compound constant expressions are not rewritten`(arguments: String) { + val source = + library( + """ + Rgba value(UInt8 red) { + UInt8 one = 1; + return new Rgba($arguments); + } + """.trimIndent(), + ) + XdkAdapter(colorPrototype = true).use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getDocumentColors(URI)).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["new Rgba(256, 2, 3)", "new Rgba(-1, 2, 3)", "new Rgba(1, 2,", "new Rgba(1, 2, missing)"]) + fun `invalid edits and close clear all prior colors and presentations`(expression: String) { + val valid = library("Rgba value() = new Rgba(1, 2, 3);") + XdkAdapter(colorPrototype = true).use { adapter -> + assertThat(adapter.compile(URI, valid).diagnostics).isEmpty() + val before = adapter.getDocumentColors(URI).single() + assertThat(adapter.getColorPresentations(URI, Range(Position(0, 0), Position(0, 1)), before.color)).isEmpty() + assertThat(adapter.compile(URI, library("Rgba value() = $expression;")).success).isFalse() + assertThat(adapter.getDocumentColors(URI)).isEmpty() + assertThat(adapter.getColorPresentations(URI, before.range, before.color)).isEmpty() + assertThat(adapter.compile(URI, valid).diagnostics).isEmpty() + adapter.closeDocument(URI) + assertThat(adapter.getDocumentColors(URI)).isEmpty() + assertThat(adapter.getColorPresentations(URI, before.range, before.color)).isEmpty() + } + } + + @Test + fun `changed fixture defaults are not treated as the known color contract`() { + XdkAdapter(colorPrototype = true).use { adapter -> + val source = library("Rgba value() = new Rgba(1, 2, 3);").replace("alpha = 255", "alpha = 0") + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getDocumentColors(URI)).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["1, 2, 3", "1, 2, 3,", "red = 1, blue = 3, green = 2, /* trailing */"]) + fun `continuous picker values quantize to bytes and alpha insertion preserves trailing separators`(arguments: String) { + val source = library("Rgba value() = new Rgba($arguments);") + XdkAdapter(colorPrototype = true).use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val before = adapter.getDocumentColors(URI).single() + val chosen = ColorValue(0.5, 0.25, 0.75, 0.5) + val changed = apply(source, adapter.getColorPresentations(URI, before.range, chosen).single().textEdit) + assertThat(adapter.compile(URI, changed).diagnostics).isEmpty() + assertThat(adapter.getDocumentColors(URI).single().color).isEqualTo(rgba(128, 64, 191, 128)) + if ("/* trailing */" in source) assertThat(changed).contains("/* trailing */") + } + } + + private fun library(body: String = "") = + """ + module ColorPrototype { + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + $body + } + """.trimIndent() + + private fun rgba( + red: Int, + green: Int, + blue: Int, + alpha: Int = 255, + ) = ColorValue(red / 255.0, green / 255.0, blue / 255.0, alpha / 255.0) + + private fun apply( + source: String, + edit: TextEdit, + ): String { + fun offset(at: Position): Int { + val starts = listOf(0) + Regex("\r\n|\r|\n").findAll(source).map { it.range.last + 1 }.toList() + return starts[at.line] + at.column + } + return source.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + + private companion object { + const val URI = "file:///ColorPrototype.x" + const val CONSUMER = "file:///Consumer.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt new file mode 100644 index 00000000000..9a478f24344 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt @@ -0,0 +1,201 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.TimeUnit.SECONDS + +class XdkCompletionSignatureTest { + @Test + fun `member completion preserves overloads substitutes receiver types and excludes private members`() { + val prefix = "$BOX void run(Box box) { box." + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix } }") + val diagnostics = adapter.getCachedResult(URI) + val items = adapter.getCompletionsAsync(URI, 0, prefix.length, ".").get(10, SECONDS) + assertThat(items.map { it.label }) + .contains("echo", "choose", "label") + .doesNotContain("secret", "hidden") + assertThat(items.single { it.label == "echo" }.detail) + .isEqualTo("String echo(String value)") + assertThat(items.filter { it.label == "choose" }).hasSize(2) + assertThat(items.single { it.label == "label" }.kind) + .isEqualTo(CompletionItem.CompletionKind.PROPERTY) + assertThat(items).allSatisfy { assertThat(it.insertText).isEqualTo(it.label) } + assertThat(adapter.getCachedResult(URI)).isEqualTo(diagnostics) + } + } + + @Test + fun `incomplete qualified calls return candidate overloads without inventing selection`() { + XdkAdapter().use { adapter -> + for (name in listOf("echo", "choose")) { + val prefix = "$BOX void run(Box box) { box.$name(" + adapter.compile(URI, "$prefix } }") + val model = + adapter.analyzeAtAsync(URI, Position(0, prefix.length)).get(10, SECONDS)!! + assertThat(model.sites.single().callCandidates).describedAs(name).isNotEmpty() + val help = adapter.getSignatureHelpAsync(URI, 0, prefix.length).get(10, SECONDS)!! + assertThat(help.signatures).hasSize(if (name == "echo") 1 else 2) + assertThat(help.signatures).allSatisfy { + assertThat(it.documentation).contains("overload not selected") + assertThat(it.activeParameter).isZero() + } + if (name == "echo") { + assertThat(help.signatures.single().label) + .isEqualTo("String echo(String value)") + } + } + } + } + + @Test + fun `partial positional slots exclude commas in strings and nested calls`() { + val prefix = + "module Editing { void run(String text) { text.indexOf(text.replace(\"a,b\", \"c\"), " + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix } }") + val help = adapter.getSignatureHelpAsync(URI, 0, prefix.length).get(10, SECONDS)!! + assertThat(help.signatures).isNotEmpty().allSatisfy { + assertThat(it.label).startsWith("conditional Int indexOf(") + assertThat(it.activeParameter).isEqualTo(1) + } + } + } + + @Test + fun `partial named arguments show labels without a fabricated parameter highlight`() { + val prefix = "module Editing { void run(String text) { text.indexOf(\"x\", startAt = 2, " + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix } }") + val help = adapter.getSignatureHelpAsync(URI, 0, prefix.length).get(10, SECONDS)!! + assertThat(help.signatures).isNotEmpty().allSatisfy { + assertThat(it.label).contains("indexOf(", "startAt") + assertThat(it.parameters).isEmpty() + assertThat(it.activeParameter).isNull() + } + } + } + + @Test + fun `completed unqualified generic calls map named arguments to their actual parameters`() { + val source = + "module Editing { T echo(T value, T backup) { return value; } " + + "void run() { String text = echo(backup = \"b\", value = \"a\"); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + for ((argument, expected) in listOf("backup = \"b\"" to 1, "value = \"a\"" to 0)) { + val help = + adapter.getSignatureHelp(URI, 0, source.indexOf(argument) + argument.length)!! + assertThat(help.signatures.single().label) + .isEqualTo("String echo(String value, String backup)") + assertThat(help.activeParameter).isEqualTo(expected) + assertThat(help.signatures.single().activeParameter).isEqualTo(expected) + assertThat(help.signatures.single().documentation).isNull() + } + assertThat(adapter.getSignatureHelp(URI, 0, source.indexOf("echo"))).isNull() + } + } + + @Test + fun `selected defaulted arguments and nested calls use compiler mappings`() { + val source = + "module Editing { Int add(Int left = 1, Int right = 2) { return left; } " + + "void run() { Int n = add(right = add(left = 3)); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val inner = + adapter.getSignatureHelp(URI, 0, source.indexOf("left = 3") + "left = 3".length)!! + assertThat(inner.activeParameter).isZero() + assertThat(inner.signatures.single().label) + .isEqualTo("Int add(Int left = …, Int right = …)") + val outer = + adapter.getSignatureHelp(URI, 0, source.lastIndexOf("right =") + "right =".length)!! + assertThat(outer.activeParameter).isEqualTo(1) + } + } + + @Test + fun `unsupported or unresolved cursor contexts return no invented results`() { + XdkAdapter().use { adapter -> + for (body in listOf("missing.", "box.noSuchPrefix", "noSuchPrefix", "1 + * box.")) { + val prefix = "$BOX void run(Box box) { $body" + adapter.compile(URI, "$prefix } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)).isEmpty() + } + val prefix = "$BOX void run(Box box) { missing(" + adapter.compile(URI, "$prefix } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + } + } + + @Test + fun `typed member completion filters candidates and replaces only the original identifier`() { + XdkAdapter().use { adapter -> + for (typed in listOf("ec", "ch", "\\u0065c")) { + val prefix = "$BOX void run(Box box) { box.$typed" + val text = "$prefix; } Int later() = 42; }" + adapter.compile(URI, text) + val cached = adapter.getCachedResult(URI) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .containsOnly(if (typed == "ch") "choose" else "echo") + assertThat(items).hasSize(if (typed == "ch") 2 else 1) + items.forEach { item -> + val edit = item.textEdit!! + assertThat(edit.range.start) + .isEqualTo(Position(0, prefix.length - typed.length)) + assertThat(edit.range.end).isEqualTo(Position(0, prefix.length)) + val applied = + text.replaceRange( + edit.range.start.column, + edit.range.end.column, + edit.newText, + ) + assertThat(applied) + .isEqualTo( + prefix.dropLast(typed.length) + item.label + "; } Int later() = 42; }", + ) + } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + } + + @Test + fun `signature help works with editor-inserted closing parentheses`() { + XdkAdapter().use { adapter -> + for (suffix in listOf("echo(", "choose(", "echo(\"x\", ")) { + val prefix = "$BOX void run(Box box) { box.$suffix" + adapter.compile(URI, "$prefix); } }") + val cached = adapter.getCachedResult(URI) + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures).hasSize(if (suffix == "choose(") 2 else 1) + assertThat(help.signatures).allSatisfy { + if (suffix.endsWith(", ")) { + // XTC permits a trailing comma: this call is complete and uses its + // selected compiler signature, rather than a candidate from a probe. + assertThat(it.documentation).isNull() + assertThat(cached!!.diagnostics).isEmpty() + } else { + assertThat(it.documentation) + .describedAs(suffix) + .contains("overload not selected") + } + assertThat(it.label) + .contains(if (suffix == "choose(") "choose(" else "echo(String value)") + } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + const val BOX = + "module Editing { class Box { " + + "Element echo(Element value) { return value; } " + + "Int choose(Int n) { return n; } String choose(String s) { return s; } " + + "String label = \"box\"; private String hidden = \"hidden\"; private Int secret() { return 1; } }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt new file mode 100644 index 00000000000..4449fd3267e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt @@ -0,0 +1,195 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkCompoundHeaderTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(List value) {}", + "void damaged(Map> value) {}", + "void damaged(Map value) {}", + "List property;", + "List damaged() = [\"x\"] ;", + "void damaged(Str§ | Int value) {}", + "void damaged(Int | Str§ value) {}", + "void damaged(Object + Str§ value) {}", + "void damaged(Object - Str§ value) {}", + "void damaged((Int | Str§) value) {}", + "void damaged(List<(Int | Str§)> value) {}", + "void damaged(List value) {}", + "void damaged(List value) {}", + "void damaged(immutable List value) {}", + "void damaged(function Str§(Int) value) {}", + "void damaged(function void(Str§) value) {}", + "void damaged(function (Int, Str§)(Int) value) {}", + "void damaged(function void(List) value) {}", + "void damaged(Function<, > value) {}", + "void damaged(Function<, > value) {}", + ], + ) + fun `written leaf type prefixes complete inside parameterized and compound headers`(declaration: String) { + val prefix = + "module Headers { String StringValue = \"x\"; " + declaration.substringBefore('§') + val suffix = declaration.substringAfter('§') + " Int later = 1; }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .describedAs(declaration) + .contains("String") + .doesNotContain("StringValue") + assertThat(items.single { it.label == "String" }.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 3), Position(0, prefix.length)), + "String", + ), + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, prefix.dropLast(3) + "String" + suffix).diagnostics) + .describedAs(declaration) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "List", + "Map>", + "Object + ecstasy.text.Str§", + ], + ) + fun `qualified leaf types use compiler lookup and replace only their final token`(type: String) { + val prefix = "module Headers { void damaged(" + type.substringBefore('§') + val suffix = type.substringAfter('§') + " value) {} }" + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + val item = + adapter.getCompletions(URI, 0, prefix.length).single { it.label == "StringBuffer" } + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 3), Position(0, prefix.length)), + "StringBuffer", + ), + ) + assertThat( + adapter.compile(URI, prefix.dropLast(3) + "StringBuffer" + suffix).diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(List + val cached = adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .describedAs(declaration) + .contains("String") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(cached.diagnostics).isNotEmpty() + assertThat(adapter.compile(URI, prefix.dropLast(3) + "String" + suffix).diagnostics) + .isNotEmpty() + } + } + + @Test + fun `a selected nested leaf at actual EOF does not require generated source`() { + val text = "module Headers { void damaged(Map + val cached = adapter.compile(URI, text) + assertThat(adapter.getCompletions(URI, 0, text.length).map { it.label }) + .contains("String") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, text.dropLast(3) + "String>> value) {} }").diagnostics) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["void damaged(List.§ value) {}"]) + fun `unsupported shapes do not invent a generic owner operand or formal scope`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('§') + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + declaration.substringAfter('§') + " }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(declaration) + .isEmpty() + } + } + + @Test + fun `embedding retains only the owned written leaf and preserves listener stopping`() { + CompilerTestSupport.configure() + val prefix = "module Headers { void damaged(Map text) = text.measure() + text.length;") + val uri = source("Library", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("text.$member", member) + val edit = requireNotNull(adapter.rename(uri, at.line, at.column, "width")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace(member, "width")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["measure", "length"]) + fun `conditional members instantiated only by a closed consumer join source rename`(member: String) { + val library = library() + val consumer = + "module Consumer { package lib import Library; Int read(lib.Box text) = text.measure() + text.length; }" + val libraryUri = source("Library", library) + val consumerUri = source("Consumer", consumer) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", libraryUri), + XdkSourceModule("Consumer", consumerUri, setOf("Library")), + ), + ) + assertThat(adapter.compile(libraryUri, library).diagnostics).isEmpty() + val at = library.positionOf("Int $member", member) + val edit = requireNotNull(adapter.rename(libraryUri, at.line, at.column, "width")) + assertThat(edit.changes.keys).containsExactlyInAnyOrder(libraryUri, consumerUri) + assertThat(apply(library, edit.changes.getValue(libraryUri))) + .isEqualTo(library.replace(member, "width")) + assertThat(apply(consumer, edit.changes.getValue(consumerUri))) + .isEqualTo(consumer.replace(member, "width")) + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(library) + assertThat(directory.resolve("Consumer.x").toFile().readText()).isEqualTo(consumer) + } + } + + @Test + fun `mutually exclusive conditional methods remain separate rename families`() { + val text = + """ + module Library { + class Box(T value) + incorporates conditional Textual + incorporates conditional Numeric {} + static mixin Textual into Box { Int measure() = value.size; } + static mixin Numeric into Box { Int measure() = 1; } + Int read(Box text, Box number) = text.measure() + number.measure(); + } + """.trimIndent() + val uri = source("Library", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("text.measure", "measure") + val edit = requireNotNull(adapter.rename(uri, at.line, at.column, "width")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo( + text + .replace("Int measure() = value.size", "Int width() = value.size") + .replace("text.measure()", "text.width()"), + ) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `conditional contract collision refuses even when the proposed graph compiles`() { + val library = + """ + module Library { + interface First { Int read(); } + interface Second { Int fetch(); } + class Box incorporates conditional Both {} + static mixin Both into Box implements First, Second {} + } + """.trimIndent() + val consumer = "module Consumer { package lib import Library; lib.Box? saved = Null; }" + val libraryUri = source("Library", library) + val consumerUri = source("Consumer", consumer) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", libraryUri), + XdkSourceModule("Consumer", consumerUri, setOf("Library")), + ), + ) + assertThat(adapter.compile(consumerUri, consumer).diagnostics).isEmpty() + val at = library.positionOf("Int read", "read") + assertThat(adapter.rename(libraryUri, at.line, at.column, "fetch")).isNull() + assertThat(adapter.compile(libraryUri, library.replace("read", "fetch")).diagnostics).isEmpty() + assertThat(adapter.compile(consumerUri, consumer).diagnostics).isEmpty() + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(library) + assertThat(directory.resolve("Consumer.x").toFile().readText()).isEqualTo(consumer) + } + } + + private fun library(body: String = ""): String = + """ + module Library { + class Box(T value) incorporates conditional Textual {} + static mixin Textual into Box { + Int measure() = value.size; + Int length.get() = value.size; + } + $body + } + """.trimIndent() + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + .toURI() + .toString() + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { current, edit -> + fun offset(position: Position) = current.splitToSequence('\n').take(position.line).sumOf { it.length + 1 } + position.column + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt new file mode 100644 index 00000000000..52349cfa27c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt @@ -0,0 +1,217 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkBuildModel +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS + +class XdkConfigurationReplacementTest { + @TempDir lateinit var directory: Path + + @Test + fun `source and build replacements retain independent successful analyses`() { + CompilerTestSupport.configure() + val library = source("Library", "module Library { static Int value() = 1; }") + val consumer = + source( + "Consumer", + """ + module Consumer { + package lib import Library; + Int read() = lib.value(); + } + """.trimIndent(), + setOf("Library"), + ) + val independent = source("Independent", "module Independent { Int value = 42; }") + val graph = listOf(library, consumer, independent) + listOf(false, true).forEach { buildModel -> + XdkAdapter().use { adapter -> + fun replace(modules: List) = + if (buildModel) { + adapter.replaceBuildInputs(XdkBuildModel.Inputs(modules, emptyList())) + } else { + adapter.replaceSourceModules(modules) + } + replace(graph) + assertThat(adapter.compile(independent.uri, text(independent)).success).isTrue() + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isTrue() + val retained = adapter.getCachedResult(independent.uri) + val replacement = XdkSourceModule("Library", library.uri, resourceRoots = emptyList()) + assertThat(replace(listOf(replacement, consumer, independent))).containsExactly(consumer.uri) + assertThat(adapter.getCachedResult(independent.uri)).isEqualTo(retained) + assertThat(adapter.getCachedResult(consumer.uri)).isNull() + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isTrue() + assertThat(replace(listOf(consumer, independent))).containsExactly(consumer.uri) + assertThat(adapter.getCachedResult(independent.uri)).isEqualTo(retained) + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isFalse() + // Failed import sets are incomplete: adding the missing source must retry them. + assertThat(replace(graph)).containsExactly(consumer.uri) + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isTrue() + } + } + } + + @Test + fun `changing a transitive dependency or source location retires its consumers`() { + CompilerTestSupport.configure() + val base = source("Base", "module Base { static Int value() = 1; }") + val middle = + source( + "Middle", + """ + module Middle { + package base import Base; + static Int value() = base.value(); + } + """.trimIndent(), + setOf("Base"), + ) + val app = + source( + "App", + """ + module App { + package middle import Middle; + Int read() = middle.value(); + } + """.trimIndent(), + setOf("Middle"), + ) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(base, middle, app)) + assertThat(adapter.compile(app.uri, text(app)).success).isTrue() + val missingEdge = XdkSourceModule("Middle", middle.uri) + assertThat(adapter.replaceSourceModules(listOf(base, missingEdge, app))).containsExactly(app.uri) + assertThat(adapter.compile(app.uri, text(app)).success).isFalse() + adapter.replaceSourceModules(listOf(base, middle, app)) + assertThat(adapter.compile(app.uri, text(app)).success).isTrue() + val moved = source("moved/Base", "module Base { static String value() = \"changed\"; }") + assertThat(adapter.replaceSourceModules(listOf(moved, middle, app))).containsExactly(app.uri) + assertThat(adapter.compile(app.uri, text(app)).success).isFalse() + } + } + + @Test + fun `binary replacement and source shadowing retire consumers but preserve unrelated buffers`() { + CompilerTestSupport.configure() + + fun library( + type: String, + value: String, + ) = EmbeddingSupport + .instance() + .compileModule( + Source("module Library { static $type value() = $value; }", "Library.x"), + null, + ErrorList(), + ).also { assertThat(it.succeeded()).isTrue() } + .toDependency() + val number = library("Int", "1") + val string = library("String", "\"changed\"") + val consumer = + source( + "Consumer", + """ + module Consumer { + package lib import Library; + Int read() = lib.value(); + } + """.trimIndent(), + setOf("Library"), + ) + val independent = source("Independent", "module Independent { Int value = 42; }") + val sourceLibrary = source("Library", "module Library { static String value() = \"source\"; }") + XdkAdapter().use { adapter -> + val graph = listOf(consumer, independent) + adapter.replaceBuildInputs(XdkBuildModel.Inputs(graph, listOf(number))) + assertThat(adapter.compile(independent.uri, text(independent)).success).isTrue() + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isTrue() + val retained = adapter.getCachedResult(independent.uri) + assertThat(adapter.replaceBuildInputs(XdkBuildModel.Inputs(graph, listOf(string)))) + .containsExactly(consumer.uri) + assertThat(adapter.getCachedResult(independent.uri)).isEqualTo(retained) + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isFalse() + adapter.replaceBuildInputs(XdkBuildModel.Inputs(graph, listOf(number))) + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isTrue() + assertThat(adapter.replaceBuildInputs(XdkBuildModel.Inputs(graph + sourceLibrary, listOf(number)))) + .containsExactly(consumer.uri) + assertThat(adapter.getCachedResult(independent.uri)).isEqualTo(retained) + assertThat(adapter.compile(consumer.uri, text(consumer)).success).isFalse() + } + } + + @Test + fun `a real graph replacement cancels pending work even for unchanged inputs`() { + CompilerTestSupport.configure() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val support = EmbeddingSupport.instance() + val added = source("Added", "module Added {}") + XdkAdapter { source, errors -> + entered.countDown() + check(release.await(10, SECONDS)) + support.compileModule(source, null, errors) + }.use { adapter -> + val pending = adapter.compileAsync("untitled:Pending.x", "module Pending {}") + try { + check(entered.await(10, SECONDS)) + assertThat(adapter.replaceSourceModules(listOf(added))).containsExactly("untitled:Pending.x") + assertThat(pending.isCancelled).isTrue() + release.countDown() + assertThat(adapter.compile(added.uri, text(added)).success).isTrue() + assertThat(adapter.getCachedResult("untitled:Pending.x")).isNull() + } finally { + release.countDown() + } + } + } + + @Test + fun `batched ownership follows unsaved roots canonical aliases and later filesystem changes`() { + val root = directory.resolve("Outer.x").toFile().canonicalFile + val member = directory.resolve("Outer/Member.x").toFile().canonicalFile + member.parentFile.mkdirs() + member.writeText("class Member {}") + val rootUri = root.toURI().toString() + val memberUri = member.toURI().toString() + val alias = + member.parentFile + .resolve("../Outer/Member.x") + .toURI() + .toString() + XdkAdapter().use { adapter -> + adapter.updateDocument(rootUri, "module Outer {}") + assertThat(adapter.analysisScopes(listOf(memberUri, alias, "untitled:Other.x"))) + .containsEntry(memberUri, rootUri) + .containsEntry(alias, rootUri) + .containsEntry("untitled:Other.x", "untitled:Other.x") + adapter.closeDocument(rootUri) + assertThat(adapter.analysisScopes(listOf(memberUri))).containsEntry(memberUri, memberUri) + root.writeText("module Outer {}") + assertThat(adapter.analysisScopes(listOf(memberUri))).containsEntry(memberUri, rootUri) + } + } + + private fun source( + path: String, + text: String, + dependencies: Set = emptySet(), + ): XdkSourceModule { + val file = directory.resolve("$path.x").toFile().canonicalFile + file.parentFile.mkdirs() + file.writeText(text) + return XdkSourceModule(file.nameWithoutExtension, file.toURI().toString(), dependencies) + } + + private fun text(module: XdkSourceModule): String = module.root.readText() +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt new file mode 100644 index 00000000000..fa2c058ab21 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt @@ -0,0 +1,381 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.PosixFilePermission + +class XdkCrossModuleMissingMethodsTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["peer", "lib.Other"]) + fun `configured dependency receives the public method with exact dispatch`(receiver: String) { + query( + caller("Int", "return $receiver.§missing(value);"), + signature = "public ${if (receiver == "peer") "" else "static "}Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "List", "(Int | String)", "lib.Value"]) + fun `closed companion renders inferred local and initializer types in the destination module`(type: String) { + query( + caller( + type, + """ + val local = value; + $type result = lib.Other.§missing(local); + return result; + """.trimIndent(), + ), + library = "module Library { class Value {} }", + companion = "class Other {}", + signature = "public static ${type.replace( + "Int", + "Int64", + ).removePrefix("lib.")} missing(${type.replace("Int", "Int64").removePrefix("lib.")} arg1)", + ) + } + + @Test + fun `third module signature uses the destination import alias rather than the caller alias`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + library = + """ + module Library { + package types import Types; + class Other {} + } + """.trimIndent(), + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public types.Value missing(types.Value arg1)", + ) + } + + @Test + fun `inferred third module local uses the destination alias`() { + query( + caller( + "shared.Value", + """ + val local = value; + shared.Value result = peer.§missing(local); + return result; + """.trimIndent(), + "package shared import Types;", + ), + library = "module Library { package types import Types; class Other {} }", + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public types.Value missing(types.Value arg1)", + ) + } + + @Test + fun `caller owned type would introduce a reverse dependency and is refused`() { + query(caller("Value", "return peer.§missing(value);", "class Value {}")) + } + + @Test + fun `repair imports signature types from an existing destination dependency`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public types.Value missing(types.Value arg1)", + imports = listOf("package types import Types;"), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["shared.Value", "List", "(shared.Value | String)"]) + fun `companion local and initializer types add one import for repeated signature types`(type: String) { + query( + caller( + type, + """ + val local = value; + $type result = lib.Other.§missing(local); + return result; + """.trimIndent(), + "package shared import Types;", + ), + library = "module Library {}", + companion = "// Destination stays in its own file.\nclass Other {}", + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public static ${type.replace("shared.", "types.")} missing(${type.replace("shared.", "types.")} arg1)", + imports = listOf("package types import Types;"), + ) + } + + @Test + fun `new import aliases avoid names in the destination and its companions`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + library = "module Library { Int types = 1; }", + companion = "class Other { Int types1 = 2; }", + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public types2.Value missing(types2.Value arg1)", + imports = listOf("package types2 import Types;"), + ) + } + + @Test + fun `multiple required modules with the same alias stem remain distinct`() { + query( + """ + module App { + package lib import Library; + package first import Types.one.org; + package second import Types.two.org; + first.Value read(lib.Other peer, second.Value value) { + return peer.§missing(value); + } + } + """.trimIndent(), + extra = + mapOf( + "Types.one.org" to "module Types.one.org { class Value {} }", + "Types.two.org" to "module Types.two.org { class Value {} }", + ), + libraryDependencies = setOf("Types.one.org", "Types.two.org"), + signature = "public types.Value missing(types1.Value arg1)", + imports = listOf("package types import Types.one.org;", "package types1 import Types.two.org;"), + ) + } + + @Test + fun `unused available modules do not acquire imports`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + extra = mapOf("Types" to "module Types { class Value {} }", "Unused" to "module Unused {}"), + libraryDependencies = setOf("Types", "Unused"), + signature = "public types.Value missing(types.Value arg1)", + imports = listOf("package types import Types;"), + ) + } + + @Test + fun `new imports preserve destination CRLF line endings`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + library = "module Library {\r\n class Other {}\r\n}", + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + signature = "public types.Value missing(types.Value arg1)", + imports = listOf("package types import Types;"), + ) + } + + @Test + fun `caller dependency does not authorize adding an edge to the destination`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + extra = mapOf("Types" to "module Types { class Value {} }"), + ) + } + + @Test + fun `read only module root refuses a writable companion repair that needs an import`() { + query( + caller("shared.Value", "return peer.§missing(value);", "package shared import Types;"), + library = "module Library {}", + companion = "class Other {}", + extra = mapOf("Types" to "module Types { class Value {} }"), + libraryDependencies = setOf("Types"), + readOnlyRoot = true, + ) + } + + @Test + fun `unrelated source root is not an implicit dependency`() { + query(caller("Int", "return peer.§missing(value);"), callerDependencies = emptySet()) + } + + @Test + fun `whole graph failure in a closed consumer prevents publication`() { + query(caller("Int", "return peer.§missing(value);"), brokenConsumer = true) + } + + @ParameterizedTest + @ValueSource( + strings = [ + "interface Other {}", + "class Other { Int missing(String value) = 1; }", + "class Other { class Object {} }", + ], + ) + fun `unsupported or ambiguous destination remains a refusal`(target: String) { + query(caller("Object", "return peer.§missing(value);"), library = "module Library { $target }") + } + + @Test + fun `read only dependency source cannot become an edit target`() { + query(caller("Int", "return peer.§missing(value);"), readOnly = true) + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `binary source indexes do not grant source ownership`(indexed: Boolean) { + CompilerTestSupport.configure() + val library = directory.resolve("Library.x").toFile().canonicalFile + val text = "module Library { class Other {} }" + library.writeText(text) + val errors = ErrorList() + val compiled = EmbeddingSupport.instance().compileModule(Source(text, library.path), null, errors) + assertThat(compiled.succeeded()).describedAs(errors.errors.toString()).isTrue() + val dependency = compiled.toDependency().let { if (indexed) it else XdkDependency.fromBinary(it.bytes()) } + query(caller("Int", "return peer.§missing(value);"), binary = dependency) + } + + private fun caller( + type: String, + body: String, + declarations: String = "", + ): String = + """ + module App { + package lib import Library; + $declarations + $type read(lib.Other peer, $type value) { + ${body.prependIndent(" ")} + } + } + """.trimIndent() + + @Test + fun `generic dependency owner accepts an exact concrete signature`() { + query( + """ + module App { + package lib import Library; + Object read(lib.Other peer, Object value) { + return peer.§missing(value); + } + } + """.trimIndent(), + library = "module Library { class Other {} }", + signature = "public Object missing(Object arg1)", + ) + } + + private fun query( + marked: String, + library: String = "module Library { class Other {} }", + companion: String? = null, + signature: String? = null, + imports: List = emptyList(), + extra: Map = emptyMap(), + libraryDependencies: Set = emptySet(), + callerDependencies: Set = setOf("Library") + extra.keys, + brokenConsumer: Boolean = false, + readOnly: Boolean = false, + readOnlyRoot: Boolean = false, + binary: XdkDependency? = null, + ) { + CompilerTestSupport.configure() + + fun write( + name: String, + text: String, + ) = directory.resolve(name).toFile().canonicalFile.apply { + parentFile.mkdirs() + writeText(text) + } + val original = marked.replace("§", "") + val source = write("App.x", original) + val root = write("Library.x", library) + val target = companion?.let { write("Library/Other.x", it) } ?: root + val targetOriginal = target.readText() + val modules = + buildList { + extra.forEach { (name, text) -> add(XdkSourceModule(name, write("$name.x", text).toURI().toString())) } + if (binary == null) add(XdkSourceModule("Library", root.toURI().toString(), libraryDependencies)) + add(XdkSourceModule("App", source.toURI().toString(), callerDependencies)) + if (brokenConsumer) { + add( + XdkSourceModule( + "Consumer", + write("Consumer.x", "module Consumer { Missing value; }").toURI().toString(), + setOf("Library"), + ), + ) + } + } + val protectedFile = if (readOnlyRoot) root else target + val permissions = Files.getPosixFilePermissions(protectedFile.toPath()) + try { + if (readOnly || readOnlyRoot) { + Files.setPosixFilePermissions( + protectedFile.toPath(), + permissions - setOf(PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE), + ) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.replaceSourceModules(modules) + if (binary != null) adapter.replaceDependencies(listOf(binary)) + val uri = source.toURI().toString() + val targetUri = target.toURI().toString() + val diagnostics = adapter.compile(uri, original).diagnostics + assertThat(diagnostics).isNotEmpty().noneMatch { it.code == "EMB-5" } + val at = XdkRename.position(original, marked.indexOf('§')) + val actions = adapter.getCodeActions(uri, Range(at, at), diagnostics).filter { it.title.contains("method 'missing'") } + if (signature == null) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).describedAs("Diagnostics: %s", diagnostics).hasSize(1) + val edit = requireNotNull(actions.single().edit) + assertThat(edit.versioned).isTrue() + val rootUri = root.toURI().toString() + val originals = mapOf(rootUri to library, targetUri to targetOriginal) + assertThat(edit.changes.keys).containsExactlyInAnyOrderElementsOf( + if (imports.isNotEmpty()) setOf(targetUri, rootUri) else setOf(targetUri), + ) + val changed = + edit.changes.mapValues { (changedUri, edits) -> + val originalText = originals.getValue(changedUri) + + fun offset(position: Position) = + requireNotNull(XdkRename.offset(originalText, SemanticModel.Position(position.line, position.column))) + edits.sortedByDescending { offset(it.range.start) }.fold(originalText) { text, change -> + text.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + } + } + assertThat(changed.getValue(targetUri)).contains(signature) + val rootText = changed[rootUri] ?: library + imports.forEach { assertThat(rootText).containsOnlyOnce(it) } + val packageImports = Regex("\\bpackage\\s+\\w+\\s+import\\s+") + assertThat(packageImports.findAll(rootText).count()).isEqualTo(packageImports.findAll(library).count() + imports.size) + if ("\r\n" in library) assertThat(rootText.replace("\r\n", "")).doesNotContain("\n") + changed.forEach { (changedUri, text) -> adapter.compile(changedUri, text) } + assertThat(adapter.compile(targetUri, changed.getValue(targetUri)).diagnostics).isEmpty() + assertThat(adapter.compile(uri, original).diagnostics).isEmpty() + } + assertThat(source.readText()).isEqualTo(original) + assertThat(target.readText()).isEqualTo(targetOriginal) + assertThat(root.readText()).isEqualTo(library) + } + } finally { + if (readOnly || readOnlyRoot) Files.setPosixFilePermissions(protectedFile.toPath(), permissions) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt new file mode 100644 index 00000000000..b3861851b67 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt @@ -0,0 +1,497 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.PosixFilePermission + +class XdkCrossOwnerMissingMethodsTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["peer", "Other", "Missing.Other"]) + fun `source receiver selects the destination and explicit public dispatch`(receiver: String) { + query( + """ + module Missing { + class Other {} + class Caller { + Int read(Other peer, Int value) { + return $receiver.§missing(value); + } + } + } + """.trimIndent(), + signature = "public ${if (receiver == "peer") "" else "static "}Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "List", "(Int | String)", "Value"]) + fun `signature retains compiler type identities in a closed companion`(type: String) { + query( + """ + module Missing { + class Value {} + $type read(Other peer, $type value) { + return peer.§missing(value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public ${type.replace("Int", "Int64")} missing(${type.replace("Int", "Int64")} arg1)", + ) + } + + @Test + fun `static void repair edits only the companion and leaves caller untouched`() { + query( + """ + module Missing { + void run() { + Other.§missing("text"); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public static void missing(String arg1)", + ) + } + + @Test + fun `destination type shadowing cannot silently change the signature`() { + query( + """ + module Missing { + Object read(Other peer) { + return peer.§missing(); + } + } + """.trimIndent(), + companion = "class Other { class Object {} }", + ) + } + + @ParameterizedTest + @ValueSource( + strings = ["interface Other {}", "class Other { Int missing(String value) = 1; }"], + ) + fun `unsupported destination or existing member never acquires another method`(target: String) { + query( + """ + module Missing { + Int read(Other peer, Int value) { + return peer.§missing(value); + } + } + """.trimIndent(), + companion = target, + ) + } + + @ParameterizedTest + @ValueSource(strings = ["String", "Type", "Class"]) + fun `binary and runtime type receivers are not source destinations`(type: String) { + query( + """ + module Missing { + class Other {} + Int read($type peer, Int value) { + return peer.§missing(value); + } + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Int local = value;", "var local = value;", "val local = value;", "Int local; local = value;"]) + fun `compiler established local arguments retain their type in another owner`(local: String) { + query( + """ + module Missing { + class Other {} + Int read(Other peer, Int value) { + $local + return peer.§missing(local); + } + } + """.trimIndent(), + signature = "public Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "List", "(Int | String)", "Value"]) + fun `closed companion receives exact local argument and initializer result types`(type: String) { + query( + """ + module Missing { + class Value {} + $type read($type value) { + val local = value; + $type result = Other.§missing(local); + return result; + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public static ${type.replace("Int", "Int64")} missing(${type.replace("Int", "Int64")} arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["peer", "Other", "Missing.Other"]) + fun `initializer repair combines local parameter and literal evidence`(receiver: String) { + query( + """ + module Missing { + class Other {} + Int read(Other peer, Int value) { + var local = value; + Int result = $receiver.§missing(local, value, "text"); + return result; + } + } + """.trimIndent(), + signature = "public ${if (receiver == "peer") "" else "static "}Int64 missing(Int64 arg1, Int64 arg2, String arg3)", + ) + } + + @Test + fun `local receiver and zero argument typed initializer retain instance dispatch`() { + query( + """ + module Missing { + Int read(Other peer) { + var other = peer; + Int result = other.§missing(); + return result; + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public Int64 missing()", + ) + } + + @Test + fun `void call accepts an inferred local argument`() { + query( + """ + module Missing { + void run(String value) { + var local = value; + Other.§missing(local); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public static void missing(String arg1)", + ) + } + + @Test + fun `initializer destination shadowing refuses a different but assignable return type`() { + query( + """ + module Missing { + Object read(Other peer) { + Object result = peer.§missing(); + return result; + } + } + """.trimIndent(), + companion = "class Other { class Object {} }", + ) + } + + @ParameterizedTest + @ValueSource( + strings = [ + "var result = peer.§missing(value); return result;", + "Int local; return peer.§missing(local);", + "var local = unknown; return peer.§missing(local);", + "return peer.§missing(local); Int local = value;", + "if (value > 0) { Int local = value; } return peer.§missing(local);", + ], + ) + fun `unproven local contexts do not acquire public signatures`(body: String) { + query( + """ + module Missing { + class Other {} + Int read(Other peer, Int value) { + $body + } + } + """.trimIndent(), + ) + } + + @Test + fun `broken neighbor prevents a repair from being published`() { + query( + """ + module Missing { + class Other {} + Int read(Other peer, Int value) { + return peer.§missing(value); + } + Int broken() = "wrong"; + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "String", "List"]) + fun `generic destination retains the concrete caller types without guessing a formal`(type: String) { + query( + """ + module Missing { + $type read(Other<$type> peer, $type value) { + return peer.§missing(value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public ${type.replace("Int", "Int64")} missing(${type.replace("Int", "Int64")} arg1)", + ) + } + + @Test + fun `nested destination can refer to the exact enclosing class formal`() { + query( + """ + module Missing { + class Outer { + class Other {} + Element read(Other peer, Element value) { + return peer.§missing(value); + } + } + } + """.trimIndent(), + signature = "public Element missing(Element arg1)", + owner = "Outer.Other", + ) + } + + @Test + fun `generic missing method declares its own constrained binder in the destination`() { + query( + """ + module Missing { + List read(Other peer, Value value) { + return peer.§missing(value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public List missing(Value arg1)", + ) + } + + @Test + fun `conditional cross owner result is kept conditional`() { + query( + """ + module Missing { + conditional Int read(Other peer, Int value) { + return peer.§missing(value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public conditional Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["make()", "holder.peer", "makeHolder().peer", "new Other()"]) + fun `validated computed and chained receivers select one source destination`(receiver: String) { + query( + """ + module Missing { + class Holder(Other peer) {} + Other make() = new Other(); + Holder makeHolder() = new Holder(make()); + Int read(Holder holder, Int value) { + return $receiver.§missing(value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["static const", "static service"]) + fun `written singleton receiver creates an instance method`(category: String) { + query( + """ + module Missing { + $category Other {} + Int read(Int value) { + return Other.§missing(value); + } + } + """.trimIndent(), + signature = "public Int64 missing(Int64 arg1)", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["value + Int:1", "makeValue(value)", "holder.value"]) + fun `validated computed arguments retain exact types and evaluation sites`(argument: String) { + query( + """ + module Missing { + class Holder(Int value) {} + Int makeValue(Int value) = value + 1; + Int read(Other peer, Holder holder, Int value) { + return peer.§missing($argument); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public Int64 missing(Int64 arg1)", + ) + } + + @Test + fun `named and positional arguments generate unique parameter names in source order`() { + query( + """ + module Missing { + Int read(Other peer, Int value) { + return peer.§missing(value, label = "text", arg1 = value); + } + } + """.trimIndent(), + companion = "class Other {}", + signature = "public Int64 missing(Int64 arg2, String label, Int64 arg1)", + ) + } + + @Test + fun `duplicate argument labels refuse creation`() { + query( + """ + module Missing { + class Other {} + Int read(Other peer, Int value) { + return peer.§missing(label = value, label = value); + } + } + """.trimIndent(), + ) + } + + @Test + fun `same spelling in unrelated generic owners cannot substitute type identity`() { + query( + """ + module Missing { + class Other {} + class Caller { + Element read(Other peer, Element value) { + return peer.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `read only companion is never offered as an edit destination`() { + val target = directory.resolve("Missing/Other.x") + Files.createDirectories(target.parent) + Files.writeString(target, "class Other {}") + val permissions = Files.getPosixFilePermissions(target) + try { + Files.setPosixFilePermissions( + target, + permissions - setOf(PosixFilePermission.OWNER_WRITE, PosixFilePermission.GROUP_WRITE, PosixFilePermission.OTHERS_WRITE), + ) + query( + """ + module Missing { + Int read(Other peer, Int value) { + return peer.§missing(value); + } + } + """.trimIndent(), + ) + } finally { + Files.setPosixFilePermissions(target, permissions) + } + } + + private fun query( + marked: String, + companion: String? = null, + signature: String? = null, + owner: String = "Other", + ) { + CompilerTestSupport.configure() + val source = directory.resolve("Missing.x").toFile().canonicalFile + val original = marked.replace("§", "") + source.writeText(original) + val target = + if (companion == null) { + source + } else { + directory.resolve("Missing/Other.x").toFile().canonicalFile.apply { + parentFile.mkdirs() + writeText(companion) + } + } + val targetOriginal = target.readText() + val uri = source.toURI().toString() + val targetUri = target.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val diagnostics = adapter.compile(uri, original).diagnostics + assertThat(diagnostics).isNotEmpty().noneMatch { it.code == "EMB-5" } + val at = XdkRename.position(original, marked.indexOf('§')) + val actions = adapter.getCodeActions(uri, Range(at, at), diagnostics).filter { it.title.contains("method 'missing'") } + if (signature == null) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).describedAs("Diagnostics: %s", diagnostics).hasSize(1) + val action = actions.single() + assertThat(action.title).isEqualTo("Create public method 'missing' in '$owner'") + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys).containsExactly(targetUri) + val changed = + edit.changes + .getValue(targetUri) + .sortedByDescending { + it.range.start.line * targetOriginal.length + + it.range.start.column + }.fold(targetOriginal) { text, change -> + fun offset(position: Position) = + requireNotNull(XdkRename.offset(targetOriginal, SemanticModel.Position(position.line, position.column))) + text.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + } + assertThat(changed).contains(signature, "TODO();") + assertThat(adapter.compile(targetUri, changed).diagnostics).isEmpty() + if (target != source) assertThat(adapter.compile(uri, original).diagnostics).isEmpty() + } + assertThat(source.readText()).isEqualTo(original) + assertThat(target.readText()).isEqualTo(targetOriginal) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt new file mode 100644 index 00000000000..8e8cc3cbda4 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt @@ -0,0 +1,84 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkCursorPresentationTest { + @Test + fun `completion carries source documentation and stable ordering across fresh attempts`() { + val prefix = + """ + module Editing { + /** Keeps the selected text. */ + String choose(String value, String backup = "") = value; + Int choose(Int value) = value; + void run() { ch + """.trimIndent() + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } }") + val at = Position(prefix.lines().lastIndex, prefix.lines().last().length) + val first = adapter.getCompletions(URI, at.line, at.column) + assertThat(first.filter { it.label == "choose" }).hasSize(2) + assertThat(first.single { it.detail.startsWith("String choose") }.documentation) + .contains("Keeps the selected text.") + assertThat(first.map { it.sortText }).doesNotContainNull().isSorted() + adapter.compile(URI, "$prefix; } } ") + assertThat(adapter.getCompletions(URI, at.line, at.column)).isEqualTo(first) + } + } + + @Test + fun `candidate documentation retains named active parameter and default information`() { + val prefix = + """ + module Editing { + /** Keeps the selected text. */ + String choose(String value, String backup = "") = value; + void run() { choose(backup = "x", value = + """.trimIndent() + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val help = + adapter.getSignatureHelp( + URI, + prefix.lines().lastIndex, + prefix.lines().last().length, + )!! + val signature = help.signatures.single() + assertThat(signature.documentation) + .contains("Keeps the selected text.", "overload not selected") + assertThat(signature.activeParameter).isZero() + assertThat(signature.parameters[0].documentation) + .contains("Parameter 1: value", "Required") + assertThat(signature.parameters[1].documentation) + .contains("Parameter 2: backup", "Optional") + } + } + + @Test + fun `selected call documentation survives completed named argument mapping`() { + val source = + """ + module Editing { + /** Keeps the selected text. */ + String choose(String value, String backup = "") = value; + void run() { choose(backup = "x", value = "y"); } + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val line = source.lines()[3] + val signature = + adapter.getSignatureHelp(URI, 3, line.indexOf("\"y\"") + 1)!!.signatures.single() + assertThat(signature.documentation) + .contains("Keeps the selected text.") + .doesNotContain("overload not selected") + assertThat(signature.activeParameter).isZero() + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt new file mode 100644 index 00000000000..53b949273d1 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt @@ -0,0 +1,328 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.FileStructure +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.PartialSemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.tool.ModuleInfo +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS + +class XdkCursorRequestTest { + @TempDir lateinit var directory: Path + + private class PausedCursor { + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val threads = CopyOnWriteArrayList() + + fun analyze( + source: Source, + sources: ModuleInfo?, + cursor: Long, + errors: ErrorListener, + ): EmbeddingSupport.PartialAnalysis { + threads.add(Thread.currentThread().name) + if (threads.size == 1) { + started.countDown() + check(release.await(10, SECONDS)) { "test did not release cursor analysis" } + } + // Deliberately finish with an independent listener: even an uninterruptible compiler + // stage must not deliver old facts after edit, close, cancellation or shutdown. + CompilerTestSupport.configure() + val listener = if (errors.isAbortDesired) ErrorList() else errors + val support = EmbeddingSupport.instance() + return if (sources == null) { + support.analyzeIncomplete(source, cursor, null, listener) + } else { + support.analyzeIncomplete( + sources, + Path.of(URI(source.fileName)).toFile(), + cursor, + null, + listener, + ) + } + } + + fun adapter(): XdkAdapter = + XdkAdapter( + { _, _ -> EmbeddingSupport.Compilation.forFile(FileStructure("Fixture")) }, + { _, _ -> EmbeddingSupport.Compilation.forFile(FileStructure("Fixture")) }, + ::analyze, + ) + + fun awaitStart() = check(started.await(10, SECONDS)) { "cursor analysis did not start" } + } + + @Test + fun `a running cursor cannot return facts after an edit`() { + val compiler = PausedCursor() + compiler.adapter().use { adapter -> + adapter.compile(URI, text("String")) + val old = adapter.analyzeAtAsync(URI, position("String")) + try { + compiler.awaitStart() + val edit = adapter.compileAsync(URI, text("Int")) + assertThat(old.isCancelled).isTrue() + compiler.release.countDown() + edit.get(10, SECONDS) + val latest = adapter.analyzeAtAsync(URI, position("Int")).get(10, SECONDS)!! + assertThat(receiverType(latest)).contains("Int") + assertThat(compiler.threads).containsOnly("xtc-compile") + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `cursor requests are coalesced and cancellation leaves normal diagnostics intact`() { + val compiler = PausedCursor() + compiler.adapter().use { adapter -> + adapter.compile(URI, text("String")) + val cached = adapter.getCachedResult(URI) + val running = adapter.analyzeAtAsync(URI, position("String")) + try { + compiler.awaitStart() + val superseded = (1..20).map { adapter.analyzeAtAsync(URI, position("String")) } + val latest = adapter.analyzeAtAsync(URI, position("String")) + assertThat(running.isCancelled).isTrue() + assertThat(superseded).allMatch { it.isCancelled } + assertThat(latest.cancel(false)).isTrue() + compiler.release.countDown() + adapter.compileAsync("untitled:Barrier.x", text("String")).get(10, SECONDS) + assertThat(compiler.threads).hasSize(1) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `completion and signature queries do not cancel one another`() { + val compiler = PausedCursor() + compiler.adapter().use { adapter -> + adapter.compile(URI, text("String")) + val completion = adapter.getCompletionsAsync(URI, 0, prefix("String").length) + try { + compiler.awaitStart() + val signature = adapter.getSignatureHelpAsync(URI, 0, prefix("String").length) + assertThat(completion.isDone).isFalse() + compiler.release.countDown() + assertThat(completion.get(10, SECONDS).map { it.label }).contains("size") + assertThat(signature.get(10, SECONDS)).isNull() + assertThat(compiler.threads).hasSize(2) + } finally { + compiler.release.countDown() + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["member", "empty", "typed", "property"]) + fun `canceling converted completion results cancels their compiler request`(kind: String) { + val compiler = PausedCursor() + val prefix = + when (kind) { + "member" -> { + prefix("String") + } + + "empty" -> { + "module Editing { void take(String value) {} void run(String text) { take(" + } + + "property" -> { + "module Editing { String text = \"x\"; void take(String value) {} void run() { take(te" + } + + else -> { + "module Editing { void take(String value) {} void run(String text) { take(te" + } + } + val source = "$prefix } }" + compiler.adapter().use { adapter -> + adapter.compile(URI, source) + val running = adapter.getCompletionsAsync(URI, 0, prefix.length) + try { + compiler.awaitStart() + assertThat(running.cancel(false)).isTrue() + compiler.release.countDown() + adapter.compileAsync("untitled:Barrier.x", text("String")).get(10, SECONDS) + assertThat(running.isCancelled).isTrue() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains( + if (kind == "member") { + "size" + } else { + "text" + }, + ) + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `close and reopen invalidate a running cursor`() { + val compiler = PausedCursor() + compiler.adapter().use { adapter -> + adapter.compile(URI, text("String")) + val old = adapter.analyzeAtAsync(URI, position("String")) + try { + compiler.awaitStart() + adapter.closeDocument(URI) + assertThat(old.isCancelled).isTrue() + assertThat(adapter.analyzeAtAsync(URI, position("String")).get(10, SECONDS)) + .isNull() + val reopened = adapter.compileAsync(URI, text("Int")) + compiler.release.countDown() + reopened.get(10, SECONDS) + assertThat( + receiverType( + adapter.analyzeAtAsync(URI, position("Int")).get(10, SECONDS)!!, + ), + ).contains("Int") + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `shutdown cancels running and queued cursor work`() { + val compiler = PausedCursor() + val adapter = compiler.adapter() + try { + adapter.compile(URI, text("String")) + val running = adapter.analyzeAtAsync(URI, position("String")) + compiler.awaitStart() + val other = "untitled:Other.x" + val compilation = adapter.compileAsync(other, text("String")) + val queued = adapter.analyzeAtAsync(other, position("String")) + running.whenComplete { _, _ -> compiler.release.countDown() } + adapter.close() + assertThat(running.isCancelled).isTrue() + assertThat(compilation.isCancelled).isTrue() + assertThat(queued.isCancelled).isTrue() + assertThat(compiler.threads).hasSize(1) + assertThat(adapter.analyzeAtAsync(URI, position("String")).isCompletedExceptionally) + .isTrue() + } finally { + compiler.release.countDown() + adapter.close() + } + } + + @Test + fun `real module cursor requests use overlays without replacing diagnostics`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { Int value = 1; } }") + val prefix = "class Child extends Base { Int run() { return value." + member.writeText("$prefix } }") + XdkAdapter().use { adapter -> + adapter.compile( + root.toURI().toString(), + "module Editing { class Base { String value = \"overlay\"; } }", + ) + val uri = member.toURI().toString() + adapter.compile(uri, member.readText()) + val cached = adapter.getCachedResult(uri) + val snapshot = + adapter.analyzeAtAsync(uri, Position(0, prefix.length)).get(10, SECONDS)!! + assertThat(receiverType(snapshot)).contains("String") + assertThat(snapshot.semantics.sourceName).isEqualTo(member.path) + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + assertThat(adapter.analyzeAtAsync(uri, Position(100, 0)).get(10, SECONDS)).isNull() + assertThat(root.readText()).contains("Int value") + } + } + + @Test + fun `editing a module root cancels its member cursor request`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + val diskRoot = "module Editing { class Base { Int value = 1; } }" + root.writeText(diskRoot) + val prefix = "class Child extends Base { Int run() { return value." + member.writeText("$prefix } }") + val rootUri = root.toURI().toString() + val uri = member.toURI().toString() + val compiler = PausedCursor() + compiler.adapter().use { adapter -> + adapter.compile( + rootUri, + "module Editing { class Base { String value = \"overlay\"; } }", + ) + adapter.compile(uri, member.readText()) + val old = adapter.analyzeAtAsync(uri, Position(0, prefix.length)) + try { + compiler.awaitStart() + val edited = adapter.compileAsync(rootUri, diskRoot) + assertThat(old.isCancelled).isTrue() + compiler.release.countDown() + edited.get(10, SECONDS) + val latest = + adapter.analyzeAtAsync(uri, Position(0, prefix.length)).get(10, SECONDS)!! + assertThat(receiverType(latest)).contains("Int") + assertThat(compiler.threads).hasSize(2).containsOnly("xtc-compile") + } finally { + compiler.release.countDown() + } + } + } + + @Test + fun `editor positions retain UTF-16 columns across CRLF and source escapes`() { + val prefix = " Int run(String value) { String text = \"😀\\u0041\"; return value." + val text = "module Editing {\r\n$prefix }\r\n}" + XdkAdapter().use { adapter -> + adapter.compile(URI, text) + val snapshot = + adapter.analyzeAtAsync(URI, Position(1, prefix.length)).get(10, SECONDS)!! + assertThat(receiverType(snapshot)).contains("String") + assertThat( + snapshot.sites + .single() + .range.end.line, + ).isEqualTo(1) + assertThat( + snapshot.sites + .single() + .range.end.column, + ).isEqualTo(prefix.length) + listOf(Position(-1, 0), Position(0, -1), Position(1, prefix.length + 100)).forEach { + assertThat(adapter.analyzeAtAsync(URI, it).get(10, SECONDS)).isNull() + } + } + } + + private fun prefix(type: String): String = "module Editing { Int run($type value) { return value." + + private fun text(type: String): String = prefix(type) + " } }" + + private fun position(type: String): Position = Position(0, prefix(type).length) + + private fun receiverType(model: PartialSemanticModel): String = model.semantics.type(model.sites.single().receiverType!!)!!.displayName + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt new file mode 100644 index 00000000000..997183d10c3 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt @@ -0,0 +1,44 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkCyclicRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = [ + "class Loop(Loop next) delegates Api(next) {} Int use(Loop target) = target.read();", + "class First(Second next) delegates Api(next) {} class Second(First next) delegates Api(next) {} " + + "Int use(First target) = target.read();", + "class Dynamic(Api next) delegates Api(next) {} Int use(Dynamic target) = target.read();", + ], + ) + fun `recursive or dynamic delegation renames written contracts without inventing implementations`(body: String) { + val text = "module App { interface Api { Int read(); } $body }" + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + val uri = source.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + listOf(text.indexOf("read"), text.lastIndexOf("read")).forEach { entry -> + val edit = requireNotNull(adapter.rename(uri, 0, entry, "fetch")) + val changed = + edit.changes + .getValue(uri) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange(change.range.start.column, change.range.end.column, change.newText) + } + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + } + assertThat(adapter.findImplementation(uri, 0, text.indexOf("read"))).isEmpty() + assertThat(source.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt new file mode 100644 index 00000000000..05cff24e5db --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt @@ -0,0 +1,225 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkDeclarationHeaderTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Int value, Str) {}", + "void damaged(Int ) {}", + "void damaged(Int value {}", + ], + ) + fun `unfinished parameter headers preserve their written name and following declarations`(declaration: String) { + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, "module Headers { $declaration Int later = 1; }") + assertThat(result.diagnostics).isNotEmpty() + assertThat( + result.symbols + .single() + .children + .map { it.name }, + ).containsExactly("damaged", "later") + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Str| value) {}", + "void damaged(Int first, Str| second) {}", + "Str| property;", + "Str| damaged() = \"x\";", + ], + ) + fun `header type prefixes expose compiler types with exact edits`(declaration: String) { + val prefix = + "module Headers { String StringValue = \"x\"; " + declaration.substringBefore('|') + val suffix = declaration.substringAfter('|') + " Int later = 1; }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .describedAs(declaration) + .contains("String") + .doesNotContain("StringValue") + assertThat(items.single { it.label == "String" }.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 3), Position(0, prefix.length)), + "String", + ), + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, prefix.dropLast(3) + "String" + suffix).diagnostics) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "Int first, "]) + fun `an empty parameter type slot does not invent a parameter name`(parameters: String) { + val prefix = "module Headers { void damaged($parameters" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix) {} Int later = 1; }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("String") + } + } + + @ParameterizedTest + @ValueSource(strings = ["void damaged(Str|", "void damaged(Str| {}", "Str|;"]) + fun `type completion works without a written name or closing delimiter`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('|') + val suffix = declaration.substringAfter('|') + " }" + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("String") + assertThat(adapter.compile(URI, prefix.dropLast(3) + "String" + suffix).diagnostics) + .isNotEmpty() + } + } + + @Test + fun `header types use enclosing names imports aliases and compiler shadowing`() { + XdkAdapter().use { adapter -> + for ( + (setup, name) in + listOf( + "import ecstasy.text.StringBuffer as Buffer;" to "Buffer", + "import ecstasy.text.*;" to "StringBuffer", + "class ItemType {}" to "ItemType", + "typedef String as Text;" to "Text", + ) + ) { + val prefix = + "module Headers { $setup class Nested { void damaged(${name.dropLast(1)}" + adapter.compile(URI, "$prefix value) {} } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .describedAs(setup) + .contains(name) + } + val prefix = "module Headers { Int String = 1; void damaged(Str" + adapter.compile(URI, "$prefix value) {} }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .doesNotContain("String") + } + } + + @ParameterizedTest + @ValueSource(strings = ["void damaged(Int va|) {}"]) + fun `unsupported headers do not guess types parameters or signatures`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('|') + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + declaration.substringAfter('|') + " }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(declaration) + .isEmpty() + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + } + } + + @Test + fun `header queries follow unsaved module types and invalidate after root changes`() { + val root = directory.resolve("Headers.x").toFile().canonicalFile + val child = directory.resolve("Headers/Child.x").toFile().canonicalFile + child.parentFile.mkdirs() + root.writeText("module Headers { class ItemDisk {} }") + val prefix = "class Child { void damaged(Ite" + child.writeText("$prefix value) {} }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val childUri = child.toURI().toString() + adapter.compile(rootUri, "module Headers { class ItemOverlay {} }") + adapter.compile(childUri, child.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .contains("ItemOverlay") + .doesNotContain("ItemDisk") + adapter.compile(rootUri, root.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .contains("ItemDisk") + .doesNotContain("ItemOverlay") + assertThat(root.readText()).contains("ItemDisk") + } + } + + @Test + fun `broken headers keep source folds and outline but discard old semantics`() { + val valid = "module Headers { void damaged(Int value) {} Int later = 1; }" + val text = + "module Headers {\r\n void damaged(Int) {\r\n Int hidden = 1;\r\n }\r\n Int later = 1; }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, valid).success).isTrue() + assertThat(adapter.compile(URI, text).success).isFalse() + assertThat(adapter.findWorkspaceSymbols("damaged")).hasSize(1) + assertThat(adapter.findWorkspaceSymbols("hidden")).isEmpty() + assertThat(adapter.getFoldingRanges(URI)).anySatisfy { + assertThat(it.startLine).isEqualTo(1) + assertThat(it.endLine).isEqualTo(3) + } + assertThat(adapter.findDefinition(URI, 1, 7)).isNull() + assertThat(adapter.findReferences(URI, 1, 7, true)).isEmpty() + assertThat(adapter.getSignatureHelp(URI, 1, 18)).isNull() + assertThat(adapter.compile(URI, valid).success).isTrue() + } + } + + @Test + fun `embedding header queries own their cursor but register no incomplete declaration`() { + CompilerTestSupport.configure() + val prefix = "module Headers { void damaged(Str" + val text = "$prefix value) { Int hidden = 1; } Int later = 1; }" + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(site.isTypeCompletion).isTrue() + assertThat(site.source.toRawString()).isEqualTo(text) + val declaration = site.parent as IncompleteDeclarationStatement + assertThat(declaration.nameToken.orElseThrow().valueText).isEqualTo("damaged") + assertThat(declaration.component).isSameAs(declaration.parent.component) + assertThat(declaration.component.getChild("damaged")).isNull() + assertThat(declaration.component.getChild("hidden")).isNull() + assertThat(analysis.cursorBindings()[site]!!.types().map { it.name() }).contains("String") + for ( + listener in + listOf( + ErrorList(ErrorList.FIRST_ERROR), + ErrorListener.cancellable(ErrorList()) { true }, + ) + ) { + source.reset() + val stopped = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, listener) + assertThat(stopped.pool()).isNull() + assertThat(stopped.cursorBindings()).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Headers.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationProvenanceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationProvenanceTest.kt new file mode 100644 index 00000000000..65753f18402 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationProvenanceTest.kt @@ -0,0 +1,132 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.Register +import org.xvm.asm.constants.PropertyConstant +import org.xvm.asm.constants.TypeParameterConstant +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.LambdaExpression +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.TypeCompositionStatement + +/** AST4 investigates ownership across declaration-only, validated and generated-method phases. */ +class XdkDeclarationProvenanceTest { + @Test + fun `declaration analysis binds formals and primary properties before value registers exist`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val declarations = + EmbeddingSupport + .instance() + .analyzeDeclarations( + Source( + """ + module Provenance { + class Box(T value) { + U echo(U input) = input; + } + interface Api { V read(V argument); } + } + """.trimIndent(), + URI, + ), + null, + errors, + ).orElseThrow() + assertThat(errors.errors).isEmpty() + val parameters = nodes(declarations.ast()).filterIsInstance().toList() + val primary = parameters.single { it.name == "value" && it.parent is TypeCompositionStatement } + assertThat(primary.resolvedTarget).isInstanceOf(PropertyConstant::class.java) + assertThat((primary.resolvedTarget as PropertyConstant).name).isEqualTo("value") + assertThat(parameters.single { it.name == "T" }.resolvedTarget) + .isInstanceOf(PropertyConstant::class.java) + listOf("U", "V").forEach { name -> + assertThat(parameters.single { it.name == name }.resolvedTarget) + .isInstanceOf(TypeParameterConstant::class.java) + } + listOf("input", "argument").forEach { name -> + assertThat(parameters.single { it.name == name }.resolvedTarget).isNull() + } + } + + @Test + fun `used parameters receive their lexical registers without inventing bodyless registers`() { + val compilation = + compile( + """ + module Provenance { + interface Api { Int read(Int argument); } + T identity(T value) = value; + } + """.trimIndent(), + ) + val all = nodes(compilation.parsed()).toList() + val value = all.filterIsInstance().single { it.name == "value" } + val register = value.resolvedTarget as Register + val use = all.filterIsInstance().single { it.name == "value" } + assertThat(use.resolvedTarget).isSameAs(register) + // The method formal consumes register zero, but is a separate source identity. + assertThat(register.index).isEqualTo(1) + assertThat(all.filterIsInstance().single { it.name == "T" }.resolvedTarget) + .isInstanceOf(TypeParameterConstant::class.java) + val bodyless = all.filterIsInstance().single { it.name == "argument" } + assertThat(bodyless.parent).isInstanceOf(MethodDeclarationStatement::class.java) + assertThat(bodyless.resolvedTarget).isNull() + } + + @Test + fun `typed lambda parameters exclude captures while cloned syntax keeps historical scalar targets`() { + val compilation = + compile( + """ + module Provenance { + Int run(Int outer) { + function Int(Int) fn = (Int value) -> value + outer; + return fn(1); + } + } + """.trimIndent(), + ) + val lambda = nodes(compilation.parsed()).filterIsInstance().single() + val parameter = nodes(lambda).filterIsInstance().single { it.name == "value" } + val bindings = requireNotNull(lambda.sourceBindings) + val binding = bindings.parameters.single() + assertThat(binding.register()).isSameAs(parameter.resolvedTarget) + assertThat(binding.name().valueText).isEqualTo("value") + assertThat(bindings.captureOrigins).hasSize(1) + assertThat(binding.register().index).isEqualTo(1) + val clone = lambda.clone() as LambdaExpression + assertThat(clone.sourceBindings).isNull() + val copiedParameter = nodes(clone).filterIsInstance().single { it.name == "value" } + assertThat(copiedParameter).isNotSameAs(parameter) + // An AST clone is not a new compilation attempt. The existing scalar association is copied, + // while the generated method/context association is deliberately unavailable on the clone. + assertThat(copiedParameter.resolvedTarget).isSameAs(parameter.resolvedTarget) + assertThat(lambda.sourceBindings).isSameAs(bindings) + } + + private fun compile(text: String): EmbeddingSupport.Compilation { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, URI), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(errors.errors).isEmpty() + return result + } + + private fun nodes(root: AstNode): Sequence = + sequence { + yield(root) + for (child in root.childNodes()) yieldAll(nodes(child)) + } + + private companion object { + const val URI = "untitled:Provenance.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt new file mode 100644 index 00000000000..d7aed47ffbd --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt @@ -0,0 +1,121 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.model.Location +import java.net.URI +import java.nio.file.Path + +class XdkDeclarationTest { + @TempDir lateinit var directory: Path + + @Test + fun `overrides expose all written contracts rather than selecting an implementation`() { + val text = + "module Declarations { interface A { Int /*a*/read(); } interface B { Int /*b*/read(); } " + + "class Both implements A, B { @Override Int /*body*/read() = 1; } " + + "Int use(Both value) = value. /*use*/read(); }" + withSource(text) { adapter, uri -> + val at = text.indexOf("/*use*/") + "/*use*/".length + assertThat(adapter.findDeclarations(uri, 0, at).map { it.startColumn }) + .containsExactlyInAnyOrder(offset(text, "a"), offset(text, "b")) + assertThat(adapter.findDefinition(uri, 0, at)?.startColumn) + .isEqualTo(offset(text, "body")) + assertThat(adapter.findDeclaration(uri, 0, at)).isNull() + } + } + + @Test + fun `property overrides navigate to their written contract`() { + val text = + "module Declarations { interface Named { @RO String /*contract*/name; } " + + "class Stored implements Named { @Override String name = \"x\"; } " + + "String run(Stored stored) = stored. /*use*/name; }" + withSource(text) { adapter, uri -> + assertThat(adapter.findDeclarations(uri, 0, offset(text, "use")).map { it.startColumn }) + .containsExactly(offset(text, "contract")) + } + } + + @Test + fun `locals and import aliases retain lexical declarations and library targets stay indexed`() { + val text = + "module Declarations { import ecstasy.text.StringBuffer as /*alias*/Buffer; " + + "Buffer run(String /*local*/text) { /*aliasUse*/Buffer buffer = new Buffer(); " + + "buffer.append(/*localUse*/text); return buffer; } }" + withSource(text) { adapter, uri -> + mapOf("aliasUse" to "alias", "localUse" to "local").forEach { (use, declaration) -> + assertThat( + adapter.findDeclarations(uri, 0, offset(text, use)).map { it.startColumn }, + ).containsExactly(offset(text, declaration)) + } + val library = + adapter.findDeclarations(uri, 0, text.indexOf("String /*local*/")).single() + assertThat(written(library)).isEqualTo("String") + adapter.closeDocument(uri) + assertThat(adapter.findDeclarations(uri, 0, offset(text, "localUse"))).isEmpty() + } + } + + @Test + fun `closed consumer declarations resolve across the discovered graph`() { + val library = "module Library { interface Api { Int /*contract*/read(); } }" + val consumer = + "module Consumer { package lib import Library; Int use(lib.Api api) = api.read(); }" + directory.resolve("Library.x").toFile().writeText(library) + val file = directory.resolve("Consumer.x").toFile().apply { writeText(consumer) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val targets = + adapter.findDeclarations( + file.canonicalFile.toURI().toString(), + 0, + consumer.indexOf("read"), + ) + assertThat(targets).hasSize(1) + assertThat(targets.single().startColumn).isEqualTo(offset(library, "contract")) + assertThat(written(targets.single())).isEqualTo("read") + } + } + + @Test + fun `broken names do not reuse a previous declaration`() { + val text = "module Declarations { Int value = 1; Int run() = /*use*/value; }" + withSource(text) { adapter, uri -> + assertThat(adapter.findDeclarations(uri, 0, offset(text, "use"))).hasSize(1) + adapter.compile(uri, text.replace("/*use*/value", "/*use*/missing")) + assertThat(adapter.findDeclarations(uri, 0, offset(text, "use"))).isEmpty() + } + } + + private fun withSource( + text: String, + action: (XdkAdapter, String) -> Unit, + ) { + val uri = + directory + .resolve("Declarations.x") + .toFile() + .canonicalFile + .toURI() + .toString() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + action(adapter, uri) + } + } + + private fun offset( + text: String, + marker: String, + ): Int = text.indexOf("/*$marker*/") + marker.length + 4 + + private fun written(location: Location): String = + Path + .of(URI(location.uri)) + .toFile() + .readLines()[location.startLine] + .substring(location.startColumn, location.endColumn) +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt new file mode 100644 index 00000000000..155e5d9ccf9 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt @@ -0,0 +1,62 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +class XdkDelegatedRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["read", "value"]) + fun `nested generic delegates join written contracts and implementations across modules`(member: String) { + val library = "module Library { interface Api { T value; T read(); } }" + val consumer = + "module Consumer { package lib import Library; " + + "class Engine implements lib.Api { @Override String value = \"text\"; @Override String read() = value; } " + + "class Forward(Engine target) delegates lib.Api(target) {} " + + "class Outer(Forward target) delegates lib.Api(target) {} " + + "String use(Outer box) = box.read() + box.value; }" + val libraryUri = source("Library", library) + val consumerUri = source("Consumer", consumer) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", libraryUri), + XdkSourceModule("Consumer", consumerUri, setOf("Library")), + ), + ) + assertThat(adapter.compile(consumerUri, consumer).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(consumerUri, 0, consumer.lastIndexOf(member), "renamed")) + assertThat(edit.changes.keys).containsExactlyInAnyOrder(libraryUri, consumerUri) + assertThat(apply(library, edit.changes.getValue(libraryUri))).isEqualTo(library.replace(member, "renamed")) + assertThat(apply(consumer, edit.changes.getValue(consumerUri))).isEqualTo(consumer.replace(member, "renamed")) + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(library) + assertThat(directory.resolve("Consumer.x").toFile().readText()).isEqualTo(consumer) + } + } + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + .toURI() + .toString() + + private fun apply( + text: String, + edits: List, + ): String = + edits.sortedByDescending { it.range.start.column }.fold(text) { current, edit -> + current.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt new file mode 100644 index 00000000000..ae8287dd4bf --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt @@ -0,0 +1,246 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.MethodStructure +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkDelimiterRecoveryTest { + @TempDir lateinit var directory: Path + + @Test + fun `ambiguous grouping cannot invent an argument separator`() { + val prefix = "$HEADER return pair((value.si" + XdkAdapter().use { adapter -> + val original = "$prefix, 2); } }" + val baseline = adapter.compile(URI, original) + assertThat(baseline.diagnostics).isNotEmpty() + // The written comma belongs to a tuple until the user closes the group. + assertThat(adapter.getCompletions(URI, 0, prefix.length)).isEmpty() + assertThat(adapter.getCachedResult(URI)).isEqualTo(baseline) + val grouped = original.replace("value.si,", "value.si),") + adapter.compile(URI, grouped) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("size") + assertThat( + adapter.compile(URI, grouped.replace("value.si)", "value.size)")).diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "(value.|", + "((value.si|", + "use((value.|", + "values[value.|", + "use(values[value.|", + "values[value.si|]", + "use((value.si|)", + "values[(value.si|]", + "use(values[value.si|)", + ], + ) + fun `completion retains a cursor inside unclosed grouping calls and indexes`(expression: String) { + val prefix = "$HEADER return ${expression.substringBefore('|')}" + val suffix = expression.substringAfter('|') + ";" + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, "$prefix$suffix } Int later() = 42; }") + assertThat(result.diagnostics).isNotEmpty() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .describedAs(expression) + .contains("size") + assertThat(adapter.getCachedResult(URI)).isEqualTo(result) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "(pair(1, ", + "use((pair(1, ", + "values[pair(1, ", + "use(values[pair(1, ", + "(fn(", + "use(new Box(", + ], + ) + fun `signature help retains the innermost call with missing enclosing delimiters`(expression: String) { + val prefix = "$HEADER return $expression" + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, "$prefix; } Int later() = 42; }") + assertThat(result.diagnostics).isNotEmpty() + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(expression).isNotNull() + val signature = requireNotNull(help).signatures.single() + assertThat(signature.label) + .isEqualTo( + when { + expression.contains("fn(") -> "Int fn(Int)" + expression.contains("new Box(") -> "new Box(Int n)" + else -> "Int pair(Int first, Int second)" + }, + ) + assertThat(signature.activeParameter) + .isEqualTo(if (expression.contains("pair(")) 1 else 0) + assertThat(adapter.getCachedResult(URI)).isEqualTo(result) + } + } + + @Test + fun `cursor at EOF retains missing expression and block delimiters`() { + val prefix = "$HEADER return use((value." + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("size") + } + } + + @ParameterizedTest + @ValueSource( + strings = + ["(pair(1, va", "use((pair(1, va", "values[pair(1, va", "use(fn(va", "use(new Box(va"], + ) + fun `typed argument prefixes still fit inside missing enclosing delimiters`(expression: String) { + val prefix = "$HEADER Int valueNumber = 1; return $expression" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, "$prefix; } Int later() = 42; }") + assertThat(cached.diagnostics).isNotEmpty() + val item = adapter.getCompletions(URI, 0, prefix.length).single() + assertThat(item.label).isEqualTo("valueNumber") + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "valueNumber", + ), + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures).hasSize(1) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `cursor statements can end at the enclosing brace without a semicolon`() { + for ( + statement in + listOf("(value.", "1 + value.", "return (value.", "Int result = (value.") + ) { + val prefix = "$HEADER $statement" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix } Int later() = 42; }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .describedAs(statement) + .contains("size") + } + } + } + + @Test + fun `unrelated syntax damage is not repaired by a cursor hole`() { + XdkAdapter().use { adapter -> + for ( + (expression, suffix) in + listOf( + "(value." to "; } void broken( { } }", + "values[1 + , value." to "; } }", + "(value." to " + ; } }", + ) + ) { + val prefix = "$HEADER return $expression" + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(expression + suffix) + .isEmpty() + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["; } }", ""]) + fun `delimiter recovery preserves the original source and never emits the incomplete method`(suffix: String) { + CompilerTestSupport.configure() + val prefix = "$HEADER return use((value.si" + val text = prefix + suffix + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat(site.endPosition).isEqualTo(cursor) + val method = + generateSequence(site.parent) { it.parent } + .filterIsInstance() + .first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + val model = analysis.semanticSnapshot(errors) + assertThat( + model.sites + .single() + .members + .map { it.name }, + ).contains("size") + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + + @Test + fun `missing delimiters in a member use unsaved module overlays and flow narrowing`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { Int value = 1; } }") + val prefix = + "class Child extends Base { Int run(Object other) { if (other.is(String)) { return (value.si" + member.writeText("$prefix; } return 0; } }") + XdkAdapter().use { adapter -> + adapter.compile( + root.toURI().toString(), + "module Editing { class Base { String value = \"overlay\"; } }", + ) + val uri = member.toURI().toString() + adapter.compile(uri, member.readText()) + val cached = adapter.getCachedResult(uri) + val model = adapter.analyzeAtAsync(uri, Position(0, prefix.length)).get(10, SECONDS)!! + assertThat( + model.sites + .single() + .members + .map { it.name }, + ).contains("size") + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + val narrowed = prefix.replace("(value.si", "(other.si") + adapter.compile(uri, "$narrowed; } return 0; } }") + assertThat(adapter.getCompletions(uri, 0, narrowed.length).map { it.label }) + .contains("size") + assertThat(root.readText()).contains("Int value") + } + } + + private companion object { + const val URI = "untitled:Editing.x" + const val HEADER = + "module Editing { Int use(Int n) = n; Int pair(Int first, Int second) = first; " + + "class Box { construct(Int n) {} } " + + "Int run(String value, Int[] values, function Int(Int) fn) {" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt new file mode 100644 index 00000000000..24612480acf --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt @@ -0,0 +1,407 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.semanticSnapshots +import org.xvm.lsp.adapter.xdk.toDependency +import org.xvm.tool.ModuleInfo +import java.nio.file.Path +import java.util.concurrent.CancellationException +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class XdkDependencyTest { + @TempDir lateinit var directory: Path + + @Test + fun `detached keys join source declarations across recompilation without conflating overloads or modules`() { + val library = dependency(LIBRARY) + val other = dependency("module Other { static Int pick(Int n) = n; }", "file:///Other.x") + val dependencies = XdkDependencies(listOf(library, other)) + val text = + "module Consumer { package lib import Library; package other import Other; " + + "Int run() = lib.pick(1) + other.pick(2); String text() = lib.pick(\"x\"); }" + + fun snapshot() = + dependencies.open().let { inputs -> + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule(Source(text, CONSUMER), inputs.repository, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + result.semanticSnapshots(errors, inputs).single() + } + val first = snapshot() + val second = snapshot() + val keys = first.calls.map { first.symbol(it.method)!!.dependency } + assertThat(keys).doesNotContainNull().doesNotHaveDuplicates() + assertThat(keys.map { it!!.module }).containsExactly("Library", "Other", "Library") + assertThat(second.calls.map { second.symbol(it.method)!!.dependency }).isEqualTo(keys) + assertThat(second.symbol(first.calls.first().method)).isNull() + first.calls.forEach { call -> + val symbol = first.symbol(call.method)!! + val artifact = if (symbol.dependency!!.module == "Library") library else other + assertThat(symbol.declaration) + .isEqualTo(artifact.declarations.getValue(symbol.dependency.index).range) + } + assertThatThrownBy { (library.declarations as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + val bytes = library.bytes() + bytes.fill(0) + assertThat(library.bytes()).isNotEqualTo(bytes) + } + + @Test + fun `adapter navigates dependency overloads and inferred return types while binary only libraries have no source`() { + val library = dependency(LIBRARY) + val source = + "module Consumer { package lib import Library; void run() { var value = lib.make(); Int n = lib.pick(1); } }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(library)) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + val definition = adapter.findDefinition(CONSUMER, 0, source.indexOf("pick"))!! + assertThat(definition.uri).isEqualTo(LIBRARY_URI) + assertThat(definition.startColumn).isEqualTo(LIBRARY.indexOf("pick")) + val type = adapter.findTypeDefinitions(CONSUMER, 0, source.indexOf("value")).single() + assertThat(type.uri).isEqualTo(LIBRARY_URI) + assertThat(type.startColumn).isEqualTo(LIBRARY.indexOf("Box")) + assertThat( + adapter.replaceDependencies(listOf(XdkDependency.fromBinary(library.bytes()))), + ).containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(adapter.findDefinition(CONSUMER, 0, source.indexOf("pick"))).isNull() + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + assertThat(adapter.findDefinition(CONSUMER, 0, source.indexOf("pick"))).isNull() + assertThat(adapter.findTypeDefinitions(CONSUMER, 0, source.indexOf("value"))).isEmpty() + } + } + + @Test + fun `inherited property accessors use dependency source indices and expire on replacement`() { + val text = "module Library { class Base { String name.get() = \"library\"; } }" + val library = dependency(text) + val source = + "module Consumer { package lib import Library; class Child extends lib.Base {} " + + "String read(Child child) = child.name; }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(library)) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + val target = adapter.findImplementation(CONSUMER, 0, source.indexOf("name")).single() + assertThat(target.uri).isEqualTo(LIBRARY_URI) + assertThat(target.startColumn).isEqualTo(text.indexOf("get")) + adapter.replaceDependencies(listOf(dependency("\n$text"))) + assertThat(adapter.findImplementation(CONSUMER, 0, source.indexOf("name"))).isEmpty() + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + assertThat( + adapter + .findImplementation(CONSUMER, 0, source.indexOf("name")) + .single() + .startLine, + ).isEqualTo(1) + adapter.replaceDependencies(listOf(XdkDependency.fromBinary(library.bytes()))) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + assertThat(adapter.findImplementation(CONSUMER, 0, source.indexOf("name"))).isEmpty() + } + } + + @Test + fun `annotation accessors use dependency source indices and expire on replacement`() { + val text = + "module Library { annotation Read into Ref { @Override T get() = super(); } " + + "class Base { @Read String name = \"library\"; } }" + val library = dependency(text) + val source = + "module Consumer { package lib import Library; class Child extends lib.Base {} " + + "String read(Child child) = child.name; }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(library)) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + val target = adapter.findImplementation(CONSUMER, 0, source.indexOf("name")).single() + assertThat(target.uri).isEqualTo(LIBRARY_URI) + assertThat(target.startColumn).isEqualTo(text.indexOf("get")) + adapter.replaceDependencies(listOf(dependency("\n$text"))) + assertThat(adapter.findImplementation(CONSUMER, 0, source.indexOf("name"))).isEmpty() + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + assertThat( + adapter + .findImplementation(CONSUMER, 0, source.indexOf("name")) + .single() + .startLine, + ).isEqualTo(1) + adapter.replaceDependencies(listOf(XdkDependency.fromBinary(library.bytes()))) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + assertThat(adapter.findImplementation(CONSUMER, 0, source.indexOf("name"))).isEmpty() + } + } + + @Test + fun `dependency replacement invalidates consumers but leaves unrelated successful sessions intact`() { + val first = dependency(LIBRARY) + val moved = dependency("\n$LIBRARY") + val source = "module Consumer { package lib import Library; Int run() = lib.pick(1); }" + val unrelated = "file:///Unrelated.x" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(first)) + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + assertThat(adapter.compile(unrelated, "module Unrelated { Int value = 1; }").success) + .isTrue() + val cached = adapter.getHoverInfo(unrelated, 0, 23) + assertThat(adapter.replaceDependencies(listOf(first))).isEmpty() + assertThat(adapter.replaceDependencies(listOf(moved))) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(adapter.getCachedResult(CONSUMER)).isNull() + assertThat(adapter.getCachedResult(unrelated)).isNotNull() + assertThat(adapter.getHoverInfo(unrelated, 0, 23)).isEqualTo(cached) + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + assertThat(adapter.findDefinition(CONSUMER, 0, source.indexOf("pick"))!!.startLine) + .isEqualTo(1) + assertThat(adapter.replaceDependencies(emptyList())) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(adapter.compile(CONSUMER, source).success).isFalse() + assertThat(adapter.replaceDependencies(listOf(first))) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + } + } + + @Test + fun `module member source locations survive serialization and source modules override their configured artifact`() { + directory = directory.toRealPath() + val root = directory.resolve("Library.x").toFile() + val member = directory.resolve("Library/Box.x").toFile() + root.writeText("module Library { static Box make() = new Box(); }") + member.parentFile.mkdirs() + member.writeText("class Box {}") + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport.instance().compileModule(ModuleInfo(root, false), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val artifact = result.toDependency() + val source = + "module Consumer { package lib import Library; void run() { var box = lib.make(); } }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(artifact)) + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + assertThat(adapter.findTypeDefinitions(CONSUMER, 0, source.indexOf("box")).single().uri) + .isEqualTo(member.toURI().toString()) + val uri = root.toURI().toString() + val current = "module Library { static Int local() = 1; Int run() = local(); }" + assertThat(adapter.compile(uri, current).success).isTrue() + assertThat(adapter.findDefinition(uri, 0, current.lastIndexOf("local"))!!.uri) + .isEqualTo(uri) + } + } + + @Test + fun `replacing a dependency cancels pending compilation before it can publish old facts`() { + val first = dependency(LIBRARY) + val moved = dependency("\n$LIBRARY") + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val held = AtomicBoolean(false) + val support = EmbeddingSupport.instance() + val source = "module Consumer { package lib import Library; Int run() = lib.pick(1); }" + XdkAdapter( + { text, repository, errors -> + if (!held.getAndSet(true)) { + entered.countDown() + check(release.await(10, SECONDS)) + } + support.compileModule(text, repository, errors) + }, + { sources, repository, errors -> + support.compileModule(sources, repository, errors) + }, + { text, _, cursor, repository, errors -> + support.analyzeIncomplete(text, cursor, repository, errors) + }, + ).use { adapter -> + adapter.replaceDependencies(listOf(first)) + val old = adapter.compileAsync(CONSUMER, source) + try { + check(entered.await(10, SECONDS)) + assertThat(adapter.replaceDependencies(listOf(moved))) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(old.isCancelled).isTrue() + val latest = adapter.compileAsync(CONSUMER, source) + release.countDown() + assertThat(latest.get(20, SECONDS).success).isTrue() + assertThat( + adapter.findDefinition(CONSUMER, 0, source.indexOf("pick"))!!.startLine, + ).isEqualTo(1) + } finally { + release.countDown() + } + } + } + + @Test + fun `dependency replacement cancels an old cursor probe and new candidates use the replacement`() { + val first = dependency("module Library { class Box { Int number = 1; } }") + val next = dependency("module Library { class Box { String label = \"new\"; } }") + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val held = AtomicBoolean(false) + val support = EmbeddingSupport.instance() + val source = + "module Consumer { package lib import Library; void run(lib.Box box) { box. } }" + val cursor = source.indexOf("box.") + 4 + XdkAdapter( + { text, repository, errors -> support.compileModule(text, repository, errors) }, + { sources, repository, errors -> + support.compileModule(sources, repository, errors) + }, + { text, _, cursor, repository, errors -> + if (!held.getAndSet(true)) { + entered.countDown() + check(release.await(10, SECONDS)) + } + val heard = ErrorList() + support + .analyzeIncomplete( + text, + cursor, + repository, + ErrorListener.tee(errors, heard), + ).also { + if (!errors.isAbortDesired) { + assertThat(heard.errors.map { it.code }) + .describedAs(heard.errors.toString()) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + } + }, + ).use { adapter -> + adapter.replaceDependencies(listOf(first)) + adapter.compile(CONSUMER, source) + val old = adapter.getCompletionsAsync(CONSUMER, 0, cursor, ".") + try { + check(entered.await(10, SECONDS)) + assertThat(adapter.replaceDependencies(listOf(next))) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(old.isCompletedExceptionally).isTrue() + assertThatThrownBy { old.join() } + .hasRootCauseInstanceOf(CancellationException::class.java) + release.countDown() + adapter.compile(CONSUMER, source) + val candidates = + adapter.getCompletionsAsync(CONSUMER, 0, cursor, ".").get(20, SECONDS) + assertThat(candidates.map { it.label }) + .contains("label") + .doesNotContain("number") + } finally { + release.countDown() + } + } + } + + @Test + fun `generic dependency methods retain their declaration and instantiated selected signature`() { + val text = "module Library { class Box { T echo(T value) = value; } }" + val artifact = dependency(text) + val source = + "module Consumer { package lib import Library; class Child extends lib.Box {} " + + "String run(Child box) = box.echo(\"x\"); }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(artifact)) + assertThat(adapter.compile(CONSUMER, source).diagnostics).isEmpty() + val target = adapter.findDefinition(CONSUMER, 0, source.indexOf("echo"))!! + assertThat(target.uri).isEqualTo(LIBRARY_URI) + assertThat(target.startColumn).isEqualTo(text.indexOf("echo")) + assertThat(adapter.findImplementation(CONSUMER, 0, source.indexOf("echo"))) + .containsExactly(target) + val help = adapter.getSignatureHelp(CONSUMER, 0, source.indexOf("\"x\""))!! + assertThat(help.signatures.single().label).contains("String").doesNotContain(" T") + } + } + + @Test + fun `a changed transitive artifact invalidates the linked consumer`() { + val baseText = "module Base { static Int value() = 1; }" + val base = dependency(baseText, "file:///Base.x") + val moved = dependency("\n$baseText", "file:///Base.x") + val inputs = XdkDependencies(listOf(base)).open() + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Library { package base import Base; static Int value() = base.value(); }", + LIBRARY_URI, + ), + inputs.repository, + errors, + ) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val library = result.toDependency() + val source = "module Consumer { package lib import Library; Int run() = lib.value(); }" + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(base, library)) + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + assertThat(adapter.replaceDependencies(listOf(moved, library))) + .containsExactly(adapter.analysisScope(CONSUMER)) + assertThat(adapter.getCachedResult(CONSUMER)).isNull() + assertThat(adapter.compile(CONSUMER, source).success).isTrue() + } + } + + @Test + fun `invalid replacement is atomic and failed compilation cannot supply an artifact`() { + val artifact = dependency(LIBRARY) + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(artifact)) + assertThatThrownBy { + adapter.replaceDependencies(listOf(artifact, artifact)) + }.isInstanceOf(IllegalArgumentException::class.java) + assertThat( + adapter + .compile( + CONSUMER, + "module Consumer { package lib import Library; Int run() = lib.pick(1); }", + ).success, + ).isTrue() + } + val errors = ErrorList() + val failed = + EmbeddingSupport + .instance() + .compileModule( + Source("module Broken { Missing value; }", "file:///Broken.x"), + null, + errors, + ) + assertThatThrownBy { failed.toDependency() } + .isInstanceOf(IllegalArgumentException::class.java) + } + + private fun dependency( + source: String, + uri: String = LIBRARY_URI, + ): XdkDependency { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(source, uri), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.toDependency() + } + + private companion object { + const val CONSUMER = "file:///Consumer.x" + const val LIBRARY_URI = "file:///Library.x" + const val LIBRARY = + "module Library { class Box {} static Box make() = new Box(); static Int pick(Int n) = n; static String pick(String n) = n; }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt new file mode 100644 index 00000000000..3adf2319b23 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt @@ -0,0 +1,150 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicInteger +import kotlin.time.TimeSource + +class XdkDiagnosticIndexTest { + @TempDir lateinit var directory: Path + + @Test + fun `an early resource failure with an enum default returns diagnostics instead of crashing`() { + CompilerTestSupport.configure() + val root = + directory.resolve("Defaults.x").toFile().apply { + writeText( + """ + module Defaults { + enum Kind default(Plain) {Plain, Fancy} + String template = $./missing.txt; + } + """.trimIndent(), + ) + } + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf(XdkSourceModule("Defaults", root.toURI().toString())), + ) + val reports = adapter.workspaceDiagnosticsAsync().get(30, SECONDS) + assertThat(reports).hasSize(1) + assertThat(reports.single().success).isFalse() + assertThat(reports.single().diagnostics.map { it.code }).contains("PARSER-24") + assertThat(reports.single().diagnostics.map { it.code }).doesNotContain("EMB-5") + root.writeText("module Defaults { enum Kind default(Plain) {Plain, Fancy} }") + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `closed graph pulls reuse unchanged roots and rebuild only changed dependency closure`() { + CompilerTestSupport.configure() + val compiled = ConcurrentHashMap() + val leaves = + (0 until 20).map { index -> + val name = "Node$index" + val file = + directory.resolve("$name.x").toFile().apply { + writeText("module $name { static Int value() = 1; }") + } + XdkSourceModule(name, file.toURI().toString()) + } + val app = + directory.resolve("App.x").toFile().apply { + writeText( + "module App { package dependency import Node0; Int run() = dependency.value(); }", + ) + } + val graph = leaves + XdkSourceModule("App", app.toURI().toString(), setOf("Node0")) + XdkAdapter( + { source, repository, errors -> + EmbeddingSupport.instance().compileModule(source, repository, errors) + }, + { sources, repository, errors -> + compiled + .computeIfAbsent(sources.sourceFile.nameWithoutExtension) { + AtomicInteger() + }.incrementAndGet() + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { _, _, _, _, _ -> error("Diagnostics must not invoke cursor analysis") }, + ).use { adapter -> + adapter.replaceSourceModules(graph) + val cold = TimeSource.Monotonic.markNow() + val first = adapter.workspaceDiagnosticsAsync().get(30, SECONDS) + val coldTime = cold.elapsedNow() + assertThat(first).hasSize(21).allMatch { it.success } + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(21) + val warm = TimeSource.Monotonic.markNow() + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).isEqualTo(first) + val warmTime = warm.elapsedNow() + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(21) + directory + .resolve("Node0.x") + .toFile() + .writeText("module Node0 { static String value() = \"changed\"; }") + val changed = adapter.workspaceDiagnosticsAsync().get(30, SECONDS) + assertThat(changed.single { it.uri.endsWith("App.x") }.diagnostics).isNotEmpty() + assertThat(compiled.getValue("Node0").get()).isEqualTo(2) + assertThat(compiled.getValue("App").get()).isEqualTo(2) + assertThat(compiled.filterKeys { it != "Node0" && it != "App" }.values).allMatch { + it.get() == 1 + } + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(23) + adapter.replaceSourceModules(graph.filter { it.name != "Node19" }) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).hasSize(20) + adapter.replaceSourceModules(graph) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).hasSize(21) + assertThat(compiled.getValue("Node19").get()).isEqualTo(2) + println( + "Closed diagnostic graph: roots=21, cold=$coldTime, warm=$warmTime, initialCompiles=21, unchangedCompiles=0, editedClosureCompiles=2", + ) + } + } + + @Test + fun `concurrent pull consumers do not cancel each other and cancellation owns only its request`() { + CompilerTestSupport.configure() + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val file = + directory.resolve("Concurrent.x").toFile().apply { writeText("module Concurrent {}") } + val adapter = + XdkAdapter( + { source, repository, errors -> + EmbeddingSupport.instance().compileModule(source, repository, errors) + }, + { sources, repository, errors -> + started.countDown() + check(release.await(20, SECONDS)) + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { _, _, _, _, _ -> error("No cursor analysis") }, + ) + try { + adapter.replaceSourceModules( + listOf(XdkSourceModule("Concurrent", file.toURI().toString())), + ) + val first = adapter.workspaceDiagnosticsAsync() + assertThat(started.await(10, SECONDS)).isTrue() + val canceled = adapter.workspaceDiagnosticsAsync() + val second = adapter.workspaceDiagnosticsAsync() + assertThat(canceled.cancel(false)).isTrue() + release.countDown() + assertThat(first.get(20, SECONDS)).allMatch { it.success } + assertThat(second.get(20, SECONDS)).isEqualTo(first.join()) + assertThat(canceled.isCancelled).isTrue() + } finally { + release.countDown() + adapter.close() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt new file mode 100644 index 00000000000..f2709ae493d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt @@ -0,0 +1,149 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.xvm.api.EmbeddingSupport.Compilation +import org.xvm.asm.ErrorListener +import org.xvm.asm.FileStructure +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.model.Location +import java.util.concurrent.TimeUnit.SECONDS + +class XdkDiagnosticTest { + @Test + fun `binary only structure diagnostics keep the document fallback`() { + XdkAdapter { _, errors -> + val binary = FileStructure("Binary") + errors.error( + "PARSER-03", + ErrorListener.at(binary.module.identityConstant), + "identifier", + ) + Compilation.forFile(binary) + }.use { adapter -> + assertThat( + adapter + .compile(URI, "module Current {}") + .diagnostics + .single() + .location, + ).isEqualTo(Location(URI, 0, 0, 0, 0)) + } + } + + @Test + fun `foreign source locations retain their identity`() { + for (name in listOf("file:///Other.x", "untitled:Other.x")) { + XdkAdapter { _, errs -> + val foreign = Source("\n broken", name) + reportAtEnd(foreign, errs) + }.use { adapter -> + val diagnostic = adapter.compile(URI, "module Current {}").diagnostics.single() + assertThat(diagnostic.location).isEqualTo(Location(name, 1, 8, 1, 8)) + } + } + } + + @Test + fun `relative and unnamed foreign sources do not borrow current document positions`() { + for (name in listOf(null, "Other.x")) { + XdkAdapter { _, errs -> + reportAtEnd( + if (name == null) Source("\n broken") else Source("\n broken", name), + errs, + ) + }.use { adapter -> + val diagnostic = adapter.compile(URI, "module Current {}").diagnostics.single() + assertThat(diagnostic.location).isEqualTo(Location(URI, 0, 0, 0, 0)) + assertThat(diagnostic.message) + .startsWith("In ${name ?: "an unidentified source"}:") + } + } + } + + @Test + fun `source offsets preserve UTF-16 columns CRLF and end of file`() { + for (newline in listOf("\n", "\r\n")) { + val text = "// first$newline// café 😀" + XdkAdapter(::reportAtEnd).use { adapter -> + val diagnostic = adapter.compile(URI, text).diagnostics.single() + assertThat(diagnostic.location) + .isEqualTo(Location(URI, 1, "// café 😀".length, 1, "// café 😀".length)) + } + } + } + + @Test + fun `an unexpected failure is not mislabeled as unavailable configuration`() { + XdkAdapter { _, _ -> throw IllegalStateException("unexpected compiler failure") } + .use { adapter -> + assertThatThrownBy { + adapter.compileAsync(URI, "module Current {}").get(10, SECONDS) + }.hasCauseInstanceOf(IllegalStateException::class.java) + .hasRootCauseMessage("unexpected compiler failure") + assertThat(adapter.getCachedResult(URI)).isNull() + } + } + + @Test + fun `actual compiler positions account for supplementary characters and line endings`() { + CompilerTestSupport.configure() + for (newline in listOf("\n", "\r\n")) { + val line = " Int read() { /* café 😀 */ return missing; }" + val text = "module Unicode {$newline$line$newline}" + XdkAdapter().use { adapter -> + val diagnostics = adapter.compile(URI, text).diagnostics + val start = line.indexOf("missing") + assertThat(diagnostics.map { it.location }) + .contains(Location(URI, 1, start, 1, start + "missing".length)) + assertThat(diagnostics.map { it.code }).doesNotContain("EMB-5") + } + } + } + + @Test + fun `incomplete edits clear obsolete navigation and recover after correction`() { + CompilerTestSupport.configure() + XdkAdapter().use { adapter -> + val valid = "module Editing { Int oldMethod() { return 1; } }" + for ( + incomplete in + listOf( + "", + "module Editing {", + "module Editing { void run() { console.", + "module Editing { String s = \"", + ) + ) { + assertThat(adapter.compile(URI, valid).success).isTrue() + val broken = adapter.compile(URI, incomplete) + assertThat(broken.success).isFalse() + assertThat(broken.diagnostics).isNotEmpty() + assertThat(broken.diagnostics.map { it.code }).doesNotContain("EMB-5") + assertThat(adapter.findWorkspaceSymbols("oldMethod")).isEmpty() + val repaired = adapter.compile(URI, valid) + assertThat(repaired.diagnostics).isEmpty() + assertThat(adapter.findWorkspaceSymbols("oldMethod")).isNotEmpty() + } + } + } + + private fun reportAtEnd( + source: Source, + errs: ErrorListener, + ): Compilation { + while (source.hasNext()) source.next() + errs.error( + "PARSER-03", + ErrorListener.`in`(source, source.position, source.position), + "identifier", + ) + return Compilation.forFile(FileStructure("Fixture")) + } + + private companion object { + const val URI = "file:///Current.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt new file mode 100644 index 00000000000..1e53ff58f7d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt @@ -0,0 +1,93 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkDocumentationTest { + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + fun `generic method skeleton preserves tabs newlines and compiler parameter names`(newline: String) { + val text = + """ + module Documentation { + ${'\t'} T echo(T input, Int count = 1) = input; + } + """.trimIndent().replace("\n", newline) + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + val action = docs(adapter, text, "echo").single() + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + val insertion = edit.changes.getValue(URI).single() + assertThat(insertion.range.start).isEqualTo(Position(1, 0)) + assertThat(insertion.newText).isEqualTo( + listOf( + "/**", + " * TODO: add description.", + " * @param T TODO", + " * @param input TODO", + " * @param count TODO", + " * @return TODO", + " */", + ).joinToString(newline, postfix = newline) { "\t$it" }, + ) + val changed = text.replace("\t", insertion.newText + "\t") + assertThat(adapter.compile(URI, changed).diagnostics).isEmpty() + assertThat(docs(adapter, changed, "echo")).isEmpty() + } + } + + @Test + fun `declarations get comments while bodies locals and same-line siblings do not`() { + val text = + """ + module Documentation { + class Item {} + Int value = 1; + void run() { + Int local = value; + assert local == 1; + } + Int first() = 1; Int second() = 2; + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + listOf("Documentation", "Item", "value", "run").forEach { assertThat(docs(adapter, text, it)).hasSize(1) } + listOf("local", "assert", "second").forEach { assertThat(docs(adapter, text, it)).isEmpty() } + } + } + + @ParameterizedTest + @ValueSource(strings = ["/** Existing documentation. */", "/** */", "/* Keep this note. */"]) + fun `existing comments are never duplicated or replaced`(comment: String) { + val text = + """ + module Documentation { + $comment + Int value = 1; + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + assertThat(docs(adapter, text, "value")).isEmpty() + } + } + + private fun docs( + adapter: XdkAdapter, + text: String, + anchor: String, + ): List { + val prefix = text.take(text.indexOf(anchor)).split(Regex("\r\n|\r|\n")) + val position = Position(prefix.lastIndex, prefix.last().length) + return adapter.getCodeActions(URI, Range(position, position), emptyList()).filter { it.title == "Generate documentation comment" } + } + + private companion object { + const val URI = "untitled:Documentation.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt new file mode 100644 index 00000000000..3e06ec0b49c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt @@ -0,0 +1,129 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename + +class XdkEditingTest { + @Test + fun `live editor settings reach every formatter and reset to request options`() { + val text = "module Editing {\nInt value = 1;\n}\n" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + listOf(FormattingConfig(indentSize = 2), FormattingConfig(indentSize = 6), null) + .forEach { config -> + adapter.editorFormattingConfig = config + val indent = " ".repeat(config?.indentSize ?: OPTIONS.tabSize) + val expected = text.replace("\nInt", "\n${indent}Int") + assertThat(apply(text, adapter.formatDocument(URI, text, OPTIONS))) + .isEqualTo(expected) + assertThat( + apply( + text, + adapter.formatRange( + URI, + text, + Range(Position(1, 0), Position(2, 0)), + OPTIONS, + ), + ), + ).isEqualTo(expected) + assertThat(apply(text, adapter.onTypeFormatting(URI, 1, 0, "\n", OPTIONS))) + .isEqualTo(expected) + } + } + } + + @Test + fun `formatting preserves CRLF literal contents and comments and is stable after application`() { + val text = + "module Editing {\r\nInt run() {\r\nString text = \"two spaces\"; \r\n" + + "// 😀 https://example.com/docs. \r\nreturn 1; \r\n}\r\n}" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + val formatted = apply(text, adapter.formatDocument(URI, text, OPTIONS)) + assertThat(formatted) + .contains( + "\r\n Int run()", + "\r\n return 1;\r\n", + "\"two spaces\"", + "https://example.com/docs. \r\n", + ) + assertThat(formatted).endsWith("}\r\n") + assertThat(adapter.compile(URI, formatted).diagnostics).isEmpty() + assertThat(adapter.formatDocument(URI, formatted, OPTIONS)).isEmpty() + val ranged = + adapter.formatRange(URI, text, Range(Position(4, 0), Position(5, 0)), OPTIONS) + assertThat(ranged.map { it.range.start.line }).containsOnly(4) + assertThat(adapter.onTypeFormatting(URI, 5, 1, "}", OPTIONS)).isEmpty() + val broken = "module Editing { String text = \"unfinished" + assertThat(adapter.formatDocument(URI, broken, OPTIONS)).isEmpty() + } + } + + @Test + fun `links use Java lexical hosts and exact UTF16 ranges`() { + val text = + "module Editing {\r\n// 😀 https://example.com/docs.\r\nString url = \"https://example.org/path\";\r\n}" + XdkAdapter().use { adapter -> + val links = adapter.getDocumentLinks(URI, text) + assertThat(links.map { it.target }) + .containsExactly("https://example.com/docs", "https://example.org/path") + assertThat(links.first().range.start).isEqualTo(Position(1, 6)) + links.forEach { link -> + val start = offset(text, link.range.start) + val end = offset(text, link.range.end) + assertThat(text.substring(start, end)).isEqualTo(link.target) + } + } + } + + @Test + fun `linked locals follow resolved identity and module lenses use the existing client command`() { + val text = + "module Editing { Int one() { Int value = 1; return value; } Int two() { Int value = 2; return value; } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + val linked = + requireNotNull(adapter.getLinkedEditingRanges(URI, 0, text.indexOf("value"))) + assertThat(linked.ranges).hasSize(2) + assertThat(linked.ranges.map { it.start.column }) + .containsExactly(text.indexOf("value"), text.indexOf("return value") + 7) + assertThat(adapter.getLinkedEditingRanges(URI, 0, text.indexOf("Int"))).isNull() + val lens = adapter.getCodeLenses(URI).single() + assertThat(lens.command!!.command).isEqualTo("xtc.runModule") + assertThat(lens.command.arguments).containsExactly(URI, "Editing") + adapter.closeDocument(URI) + assertThat(adapter.getCodeLenses(URI)).isEmpty() + assertThat(adapter.getLinkedEditingRanges(URI, 0, text.indexOf("value"))).isNull() + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith( + compareByDescending { it.range.start.line } + .thenByDescending { it.range.start.column }, + ).fold(text) { current, edit -> + current.replaceRange( + offset(text, edit.range.start), + offset(text, edit.range.end), + edit.newText, + ) + } + + private fun offset( + text: String, + at: Position, + ): Int = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + + private companion object { + const val URI = "untitled:Editing.x" + val OPTIONS = FormattingOptions(4, true) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt new file mode 100644 index 00000000000..378fa3b2565 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt @@ -0,0 +1,130 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkEnclosingValueCompletionTest { + @Test + fun `shared enclosing fixtures accept their exact edits and compile`() { + val scenarios = Path.of(System.getProperty("xtc.composite.root"), "lang/test-fixtures/compiler-playbook/scenarios.json") + val data = + JsonParser + .parseString(scenarios.toFile().readText()) + .asJsonObject + .getAsJsonObject("cases") + .getAsJsonObject("X152") + .getAsJsonObject("values") + val uri = "file:///${data["file"].asString}" + XdkAdapter().use { adapter -> + data.getAsJsonArray("variants").forEach { row -> + val variant = row.asJsonObject + val source = variant["source"].asString + val anchor = variant["anchor"].asString + val before = source.substring(0, source.indexOf(anchor) + anchor.length) + val at = Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + adapter.compile(uri, source) + val item = adapter.getCompletions(uri, at.line, at.column).single { it.label == variant["label"].asString } + val edit = requireNotNull(item.textEdit) + + fun offset(position: Position) = source.lineSequence().take(position.line).sumOf { it.length + 1 } + position.column + val accepted = source.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + assertThat(accepted).isEqualTo(variant["expected"].asString) + assertThat(adapter.compile(uri, accepted).diagnostics).describedAs(accepted).isEmpty() + } + } + } + + @Test + fun `enclosing instances fit ordinary named and qualified argument slots`() { + listOf("§" to "this.Outer", "value = §" to "this.Outer", "this.Ou§" to "Outer").forEach { (argument, expected) -> + val marked = "module Editing { class Outer { void take(Outer value) {} class Nested { void run() { take($argument); } } } }" + val source = marked.replace("§", "") + val at = marked.indexOf('§') + XdkAdapter().use { adapter -> + val original = adapter.compile(URI, source) + assertThat(adapter.compile(URI, marked.replace(argument, "this.Outer")).diagnostics) + .describedAs("ordinary enclosing call: %s", marked) + .isEmpty() + adapter.compile(URI, source) + val items = adapter.getCompletions(URI, 0, at) + assertThat( + items.map { + it.label + }, + ).describedAs("%s: %s", marked, adapter.analyzeAtAsync(URI, Position(0, at)).join()?.sites).contains(expected) + val item = items.single { it.label == expected } + assertThat(item.detail).contains("Enclosing instance") + assertThat(adapter.getCachedResult(URI)).isEqualTo(original) + val edit = item.textEdit!! + assertThat( + adapter.compile(URI, source.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText)).diagnostics, + ).isEmpty() + } + } + } + + @Test + fun `static boundaries and incompatible argument types reject enclosing instances`() { + listOf( + "class Outer { static void run() { take(§); } } void take(Outer value) {}", + "class Outer { static class Nested { void run() { take(§); } } } void take(Outer value) {}", + "class Outer { void run() { take(§); } } void take(Int value) {}", + ).forEach { body -> + val marked = "module Editing { $body }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))) + .describedAs(marked) + .noneMatch { it.detail.contains("Enclosing instance") } + } + } + } + + @Test + fun `ordinary returns initializers and operands complete enclosing instances`() { + listOf( + "Owner current() = thi§;" to "this.Owner", + "Owner current() = this.Ow§;" to "Owner", + "void run() { Owner current = thi§; }" to "this.Owner", + "Boolean same(Owner owner) = owner == thi§;" to "this.Owner", + ).forEach { (body, expected) -> + val marked = "module Editing { class Owner { class Nested { $body } } }" + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, source) + val item = adapter.getCompletions(URI, 0, marked.indexOf('§')).single { it.label == expected } + assertThat(item.detail).isEqualTo("Accessible enclosing instance") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + val edit = item.textEdit!! + assertThat( + adapter.compile(URI, source.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText)).diagnostics, + ).describedAs(marked) + .isEmpty() + } + } + } + + @Test + fun `ordinary enclosing proposals respect static boundaries and expected types`() { + listOf( + "class Owner { static Owner current() = thi§; }", + "class Owner { static class Nested { Owner current() = thi§; } }", + "class Owner { Int current() = thi§; }", + ).forEach { body -> + val marked = "module Editing { $body }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))) + .describedAs(marked) + .noneMatch { it.label == "this.Owner" } + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt new file mode 100644 index 00000000000..b4b66fa7b41 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt @@ -0,0 +1,200 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkProject +import org.xvm.lsp.adapter.xdk.XdkProjectQueries +import org.xvm.lsp.adapter.xdk.XdkRenameScope +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.io.File +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkExternalRenameTest { + @TempDir lateinit var directory: Path + + @Test + fun `explicit roots include closed external consumers and report the precise scope`() { + val library = source("workspace/Library.x", LIBRARY) + val consumer = source("external/Consumer.x", CONSUMER) + val omitted = source("unknown/Other.x", CONSUMER.replace("Consumer", "Other")) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(library.parent)) + adapter.replaceSourceModules(graph(library, consumer)) + assertThat(adapter.compile(library.toURI().toString(), LIBRARY).diagnostics).isEmpty() + val proposal = + requireNotNull( + adapter + .renameProposalAsync( + library.toURI().toString(), + 0, + LIBRARY.indexOf("input"), + "value", + ).get(30, SECONDS), + ) + val scope = requireNotNull(proposal.scope) + assertThat(scope.boundary).isEqualTo(XdkRenameScope.Boundary.CONFIGURED_GRAPH) + assertThat(scope.modules.map { it.name }).containsExactly("Library", "Consumer") + assertThat(scope.sourceUris) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + assertThat(proposal.edit.changes.keys) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + assertThat(apply(CONSUMER, proposal.edit, consumer)) + .isEqualTo(CONSUMER.replace("input", "value")) + assertThat(omitted.readText()).isEqualTo(CONSUMER.replace("Consumer", "Other")) + assertThat(library.readText()).isEqualTo(LIBRARY) + assertThat(consumer.readText()).isEqualTo(CONSUMER) + } + } + + @Test + fun `external unsaved consumers participate in proof and input revision`() { + val library = source("workspace/Library.x", LIBRARY) + val consumer = source("external/Consumer.x", CONSUMER) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(library.parent)) + adapter.replaceSourceModules(graph(library, consumer)) + assertThat(adapter.compile(library.toURI().toString(), LIBRARY).diagnostics).isEmpty() + val before = + requireNotNull( + adapter + .renameProposalAsync( + library.toURI().toString(), + 0, + LIBRARY.indexOf("input"), + "value", + ).get(30, SECONDS), + ) + val overlay = + CONSUMER.replace("box.pick(input = 1)", "box.pick(input = 1) + box.pick(input = 2)") + assertThat(adapter.compile(consumer.toURI().toString(), overlay).diagnostics).isEmpty() + val after = + requireNotNull( + adapter + .renameProposalAsync( + library.toURI().toString(), + 0, + LIBRARY.indexOf("input"), + "value", + ).get(30, SECONDS), + ) + assertThat(requireNotNull(after.scope).revision) + .isNotEqualTo(requireNotNull(before.scope).revision) + assertThat(apply(overlay, after.edit, consumer)) + .isEqualTo(overlay.replace("input", "value")) + assertThat(consumer.readText()).isEqualTo(CONSUMER) + } + } + + @Test + fun `missing registered external consumer cannot silently narrow proof`() { + val library = source("workspace/Library.x", LIBRARY) + val missing = directory.toRealPath().resolve("missing/Consumer.x").toFile() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(graph(library, missing)) + assertThat(adapter.compile(library.toURI().toString(), LIBRARY).diagnostics).isEmpty() + assertThat( + adapter.rename(library.toURI().toString(), 0, LIBRARY.indexOf("input"), "value"), + ).isNull() + } + } + + @Test + fun `external disk changes during proof invalidate the proposal`() { + val library = source("workspace/Library.x", LIBRARY) + val consumer = source("external/Consumer.x", CONSUMER) + CompilerTestSupport.configure() + val query = + XdkProjectQueries( + XdkProject(graph(library, consumer)), + emptyMap(), + XdkDependencies(emptyList()), + { sources, repository, errors -> + // Captured source inputs have already been fixed before the first compiler + // call. + consumer.writeText("\n$CONSUMER") + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { false }, + ) + assertThat( + query.renameProposal( + library.toURI().toString(), + 0, + LIBRARY.indexOf("input"), + "value", + ), + ).isNull() + assertThat(library.readText()).isEqualTo(LIBRARY) + } + + @Test + fun `automatic discovery receipt does not claim external consumers`() { + val library = source("workspace/Library.x", LIBRARY) + val consumer = source("external/Consumer.x", CONSUMER) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(library.parent)) + assertThat(adapter.compile(library.toURI().toString(), LIBRARY).diagnostics).isEmpty() + val proposal = + requireNotNull( + adapter + .renameProposalAsync( + library.toURI().toString(), + 0, + LIBRARY.indexOf("input"), + "value", + ).get(30, SECONDS), + ) + assertThat(requireNotNull(proposal.scope).boundary) + .isEqualTo(XdkRenameScope.Boundary.DISCOVERED_GRAPH) + assertThat(requireNotNull(proposal.scope).sourceUris) + .containsExactly(library.toURI().toString()) + assertThat(proposal.edit.changes.keys).containsExactly(library.toURI().toString()) + assertThat(consumer.readText()).isEqualTo(CONSUMER) + } + } + + private fun source( + path: String, + text: String, + ): File = + directory.toRealPath().resolve(path).toFile().apply { + parentFile.mkdirs() + writeText(text) + } + + private fun graph( + library: File, + consumer: File, + ): List = + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + ) + + private fun apply( + text: String, + edit: WorkspaceEdit, + file: File, + ): String = + edit.changes + .getValue(file.toURI().toString()) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } + + private companion object { + const val LIBRARY = "module Library { class Box { Int pick(Int input) = input; } }" + const val CONSUMER = + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(input = 1); }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt new file mode 100644 index 00000000000..3329f5dc9f0 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt @@ -0,0 +1,130 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkFileChanges +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.XdkSources +import java.nio.file.Files +import java.nio.file.Path + +class XdkFileChangesTest { + @TempDir lateinit var directory: Path + + @Test + fun `late creation events match compiled inputs but content and membership changes do not`() { + directory = directory.toRealPath() + val root = + directory.resolve("App.x").toFile().apply { + writeText("module App { String resource = \$./data.txt; }") + } + val member = + directory.resolve("App/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box {}") + } + val resource = directory.resolve("data.txt").toFile().apply { writeText("original") } + val inputs = XdkSources.capture(root, emptyMap()) { false }.inputs + listOf(root, member, member.parentFile, resource, root.parentFile).forEach { + assertThat(XdkFileChanges.unchanged(it, root, inputs)).describedAs(it.path).isTrue() + } + val stamp = Files.getLastModifiedTime(resource.toPath()) + resource.writeText("modified") + Files.setLastModifiedTime(resource.toPath(), stamp) + assertThat(XdkFileChanges.unchanged(resource, root, inputs)).isFalse() + assertThat(XdkFileChanges.unchanged(root.parentFile, root, inputs)).isFalse() + member.writeText("class Other {}") + assertThat(XdkFileChanges.unchanged(member, root, inputs)).isFalse() + member.writeText("class Box {}") + val added = member.parentFile.resolve("New.x").apply { writeText("class New {}") } + assertThat(XdkFileChanges.unchanged(added, root, inputs)).isFalse() + assertThat(XdkFileChanges.unchanged(member.parentFile, root, inputs)).isFalse() + Files.delete(member.toPath()) + assertThat(XdkFileChanges.unchanged(member, root, inputs)).isFalse() + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `incomplete overlay owns no-op proofs while real closed source and resource changes retire them`(resourceChange: Boolean) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + val root = + directory.resolve("App.x").toFile().apply { + writeText("module App { String resource = \$./data.txt; void run() {} }") + } + val member = + directory.resolve("App/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box {}") + } + val resource = directory.resolve("data.txt").toFile().apply { writeText("first") } + val overlay = + root.readText().replace("void run() {}", "void run() { Int size = resource.si; }") + XdkAdapter().use { adapter -> + val uri = root.toURI().toString() + assertThat(adapter.compile(uri, overlay).success).isFalse() + listOf(root, root.parentFile, member).forEach { + assertThat(adapter.changedFileScopes(it.toURI().toString())) + .describedAs(it.path) + .isEmpty() + } + if (resourceChange) { + resource.writeText("second") + } else { + member.writeText("class Box { Int changed = 1; }") + } + assertThat(adapter.changedFileScopes(root.parentFile.toURI().toString())).contains(uri) + } + } + + @Test + fun `no-op source events preserve consumers and real changes include the dependency closure`() { + val library = + directory.resolve("Library.x").toFile().apply { + writeText("module Library { class Box {} }") + } + val consumer = + directory.resolve("Consumer.x").toFile().apply { + writeText( + "module Consumer { package lib import Library; lib.Box create() = new lib.Box(); }", + ) + } + val modules = + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + ) + val adapter = XdkAdapter() + try { + adapter.replaceSourceModules(modules) + assertThat(adapter.compile(consumer.toURI().toString(), consumer.readText()).success) + .isTrue() + assertThat(adapter.changedFileScopes(library.toURI().toString())).isEmpty() + val original = library.readText() + Files.delete(library.toPath()) + assertThat(adapter.compile(consumer.toURI().toString(), consumer.readText()).success) + .isFalse() + library.writeText(original) + assertThat(adapter.changedFileScopes(library.toURI().toString())) + .containsExactlyInAnyOrderElementsOf(modules.map { it.uri }) + assertThat(adapter.compile(consumer.toURI().toString(), consumer.readText()).success) + .isTrue() + assertThat( + adapter.changedFileScopes(directory.resolve("settings.json").toUri().toString()), + ).isEmpty() + val members = Files.createDirectory(directory.resolve("Library")) + Files.writeString(members.resolve("New.x"), "class New {}") + assertThat(adapter.changedFileScopes(members.toUri().toString())) + .containsExactlyInAnyOrderElementsOf(modules.map { it.uri }) + library.writeText("module Library { class Changed {} }") + assertThat(adapter.changedFileScopes(library.toURI().toString())) + .containsExactlyInAnyOrderElementsOf(modules.map { it.uri }) + } finally { + adapter.close() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt new file mode 100644 index 00000000000..2034609f4e5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt @@ -0,0 +1,214 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkFileOperationsTest { + @TempDir lateinit var directory: Path + + @Test + fun `batch file rename proves the combined edits and leaves requested resource operations to host`() { + write("App.x", "module App { Box make() = new Box(); Other second() = new Other(); }") + write("App/Box.x", "class Box {}") + write("App/Other.x", "class Other {}") + session { adapter -> + val moves = + mapOf( + uri("App/Box.x") to uri("App/Crate.x"), + uri("App/Other.x") to uri("App/Second.x"), + ) + val edit = requireNotNull(adapter.renameFilesAsync(moves).get(30, SECONDS)) + assertThat(edit.versioned).isTrue() + assertThat(edit.renames).isEmpty() + assertThat(Files.readString(directory.resolve("App.x"))).contains("new Box()") + apply(edit, moves) + assertThat(Files.readString(directory.resolve("App.x"))) + .contains("new Crate()", "new Second()") + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `implicit package folder rename updates references and all descendants`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + session { adapter -> + val moves = mapOf(uri("App/tools") to uri("App/util")) + val edit = requireNotNull(adapter.renameFilesAsync(moves).get(30, SECONDS)) + assertThat(edit.renames).isEmpty() + apply(edit, moves) + assertThat(Files.readString(directory.resolve("App.x"))) + .contains("util.Box") + .doesNotContain("tools.Box") + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `discovered module file rename updates imports and returns only companion move`() { + write("Library.x", "module Library {}") + write("Library/Box.x", "class Box {}") + write( + "Consumer.x", + "module Consumer { package lib import Library; lib.Box make() = new lib.Box(); }", + ) + session { adapter -> + val moves = mapOf(uri("Library.x") to uri("Renamed.x")) + val edit = requireNotNull(adapter.renameFilesAsync(moves).get(30, SECONDS)) + assertThat(edit.renames.keys.map { Path.of(URI(it)).fileName.toString() }) + .containsExactly("Library") + apply(edit, moves) + assertThat(Files.readString(directory.resolve("Consumer.x"))) + .contains("package lib import Renamed") + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `moving a module container without changing names preserves the discovered graph`() { + write( + "old/App.x", + "module App { Box make() = new Box(); static String text() = $./data.txt; }", + ) + write("old/App/Box.x", "class Box {}") + write("old/data.txt", "resource survives the proposed move") + session { adapter -> + val moves = mapOf(uri("old") to uri("moved")) + val edit = requireNotNull(adapter.renameFilesAsync(moves).get(30, SECONDS)) + assertThat(edit.changes).isEmpty() + assertThat(directory.resolve("moved")).doesNotExist() + apply(edit, moves) + assertThat(Files.readString(directory.resolve("moved/data.txt"))) + .isEqualTo("resource survives the proposed move") + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `batch moves to another directory preserve discovered imports and embedded resources`() { + write( + "old/App.x", + "module App { Box make() = new Box(); static String text() = $./data.txt; }", + ) + write("old/App/Box.x", "class Box {}") + write("old/data.txt", "resource") + write("second/Library.x", "module Library { Int number() = 1; }") + write( + "Consumer.x", + "module Consumer { package app import App; package lib import Library; app.Box make() = new app.Box(); Int number() = lib.number(); }", + ) + Files.createDirectory(directory.resolve("destination")) + session { adapter -> + val moves = + mapOf( + uri("old") to uri("destination/old"), + uri("second") to uri("destination/second"), + ) + val edit = requireNotNull(adapter.renameFilesAsync(moves).get(30, SECONDS)) + assertThat(edit.changes).isEmpty() + apply(edit, moves) + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(Files.readString(directory.resolve("destination/old/data.txt"))) + .isEqualTo("resource") + } + } + + @Test + fun `configured root moves collisions symlinks and overlapping requests are refused without writes`() { + write("App.x", "module App { Box make() = new Box(); }") + write("App/Box.x", "class Box {}") + write("App/Occupied.x", "class Occupied {}") + session { adapter -> + assertThat( + adapter + .renameFilesAsync(mapOf(uri("App/Box.x") to uri("App/Occupied.x"))) + .get(30, SECONDS), + ).isNull() + assertThat( + adapter + .renameFilesAsync( + mapOf(uri("App") to uri("Other"), uri("App/Box.x") to uri("App/New.x")), + ).get(30, SECONDS), + ).isNull() + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("App.x")))) + assertThat( + adapter + .renameFilesAsync(mapOf(uri("App.x") to uri("Renamed.x"))) + .get(30, SECONDS), + ).isNull() + Files.createSymbolicLink(directory.resolve("alias.x"), directory.resolve("App/Box.x")) + assertThat( + adapter + .renameFilesAsync(mapOf(uri("alias.x") to uri("renamed.x"))) + .get(30, SECONDS), + ).isNull() + assertThat(Files.readString(directory.resolve("App.x"))).contains("new Box()") + } + } + + private fun session(body: (XdkAdapter) -> Unit) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + body(adapter) + } + } + + private fun write( + file: String, + source: String, + ) { + directory.resolve(file).toFile().apply { + parentFile.mkdirs() + writeText(source) + } + } + + private fun uri(file: String) = + directory + .resolve(file) + .toFile() + .toURI() + .toString() + .removeSuffix("/") + + private fun apply( + edit: WorkspaceEdit, + requested: Map, + ) { + edit.changes.forEach { (uri, changes) -> + val path = Path.of(URI(uri)) + val text = Files.readString(path) + + fun offset(position: Position): Int = text.lineSequence().take(position.line).sumOf { it.length + 1 } + position.column + Files.writeString( + path, + changes + .sortedByDescending { offset(it.range.start) } + .fold(text) { value, change -> + value.replaceRange( + offset(change.range.start), + offset(change.range.end), + change.newText, + ) + }, + ) + } + (edit.renames + requested).forEach { (from, to) -> + Files.move(Path.of(URI(from)), Path.of(URI(to))) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt new file mode 100644 index 00000000000..eba0f1944bb --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt @@ -0,0 +1,40 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkFoldingTest { + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n"]) + fun `method folds stop before their closing brace and following same-line declarations`(newline: String) { + listOf("Int value", "Int", "Int value, Str").forEach { parameters -> + val closingLine = " /* 😀 */ } Int later = 1; }" + val text = + listOf( + "module Folds {", + " void damaged($parameters) {", + " Int hidden = 1;", + closingLine, + ).joinToString(newline) + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, text) + assertThat(result.success) + .describedAs(parameters) + .isEqualTo(parameters == "Int value") + val folds = adapter.getFoldingRanges(URI) + val method = folds.single { it.startLine == 1 } + assertThat(method.endLine).isEqualTo(3) + assertThat(method.startCharacter).isNull() + assertThat(method.endCharacter).isEqualTo(closingLine.indexOf('}')) + val module = folds.single { it.startLine == 0 } + assertThat(module.endCharacter).isEqualTo(closingLine.lastIndexOf('}')) + } + } + } + + private companion object { + const val URI = "file:///Folds.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt new file mode 100644 index 00000000000..2da2aec3917 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt @@ -0,0 +1,127 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkLexical +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkFormattingBreadthTest { + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + fun `shared formatter scenarios preserve compilation line endings and idempotence`(newline: String) { + val catalog = Path.of(System.getProperty("xtc.composite.root"), "lang/test-fixtures/compiler-playbook/scenarios.json") + val cases = JsonParser.parseString(catalog.toFile().readText()).asJsonObject["cases"].asJsonObject + listOf("X249", "X250").forEach { id -> + val data = cases[id].asJsonObject["values"].asJsonObject + val text = data["source"].asString.replace("\n", newline) + val expected = data["expected"].asString.replace("\n", newline) + XdkAdapter().use { adapter -> + val uri = "untitled:" + data["file"].asString + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val changed = apply(text, adapter.formatDocument(uri, text, OPTIONS)) + assertThat(changed).describedAs(id).isEqualTo(expected) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(adapter.formatDocument(uri, changed, OPTIONS)).isEmpty() + } + } + } + + @Test + fun `range and on type formatting preserve unrelated lines and do not wrap while typing`() { + val text = + """ + module Layout { + Int sum(Int first, Int second, Int third) = first + second + third; + Int unchanged = 1; + } + """.trimIndent() + val config = FormattingConfig(maxLineWidth = 40) + val range = Range(Position(1, 0), Position(2, 0)) + val changed = apply(text, XdkLexical.format(text, config, OPTIONS, range)) + assertThat(changed).contains("\nInt unchanged = 1;\n}").doesNotEndWith("\n") + assertThat(changed.lines().size).isGreaterThan(text.lines().size) + val typing = apply(text, XdkLexical.format(text, config, OPTIONS, range, wrapLines = false)) + assertThat(typing.lines()).hasSameSizeAs(text.lines()) + assertThat(typing).contains("\n Int sum(") + assertThat(XdkLexical.format(changed, config, OPTIONS.copy(insertFinalNewline = false))) + .allSatisfy { assertThat(it.range.start.line).isEqualTo(changed.lines().lastIndex - 1) } + } + + @Test + fun `multiline literals templates and unterminated lexemes never have their contents reformatted`() { + val samples = + listOf( + """ + module Literals { + String value = \|keep this indent + |and "quoted" content + ; + } + """.trimIndent(), + "module Literals { String value = $\"keep {1 + 2}\"; }", + ) + samples.forEach { source -> + XdkAdapter().use { adapter -> assertThat(adapter.compile("untitled:Literals.x", source).diagnostics).isEmpty() } + val formatted = apply(source, XdkLexical.format(source, FormattingConfig(maxLineWidth = 20), OPTIONS)) + assertThat(formatted).contains(source.substringAfter("= ").substringBeforeLast(";")) + } + assertThat(XdkLexical.format("module Bad { String text = \"unfinished", FormattingConfig.DEFAULT, OPTIONS)).isEmpty() + } + + @Test + fun `wrapping honors configured indentation continuation width and tabs`() { + val text = + """ + module Layout { + Int sum(Int first, Int second, Int third) = first + second + third; + } + """.trimIndent() + val options = FormattingOptions(2, true) + listOf(true, false).forEach { spaces -> + val config = FormattingConfig(2, 6, spaces, 40) + val formatted = apply(text, XdkLexical.format(text, config, options)) + val lines = formatted.lines().filter(String::isNotEmpty) + assertThat(lines[1]).startsWith(if (spaces) " Int" else "\tInt") + assertThat(lines.drop(2).dropLast(1)) + .isNotEmpty() + .allSatisfy { line -> + assertThat(line.takeWhile(Char::isWhitespace)).isEqualTo(if (spaces) " " else "\t\t\t\t") + } + assertThat(XdkLexical.format(formatted, config, options)).isEmpty() + XdkAdapter().use { adapter -> assertThat(adapter.compile("untitled:Layout.x", formatted).diagnostics).isEmpty() } + } + } + + @Test + fun `wrapping compact declarations uses nesting at each break and remains stable`() { + val text = + """ + module Compact { Int sum(Int first, Int second, Int third) = first + second + third; } + """.trimIndent() + val config = FormattingConfig(maxLineWidth = 40) + val formatted = apply(text, XdkLexical.format(text, config, OPTIONS)) + assertThat(formatted.lines()).hasSizeGreaterThan(1) + assertThat(XdkLexical.format(formatted, config, OPTIONS)).isEmpty() + XdkAdapter().use { adapter -> assertThat(adapter.compile("untitled:Compact.x", formatted).diagnostics).isEmpty() } + } + + private fun apply( + text: String, + edits: List, + ): String { + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + return edits.sortedByDescending { offset(it.range.start) }.fold(text) { changed, edit -> + changed.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + } + + private companion object { + val OPTIONS = FormattingOptions(4, true) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt new file mode 100644 index 00000000000..8b96af25968 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt @@ -0,0 +1,241 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Source +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkGenericHeaderTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Li§st value) {}", + "void damaged(Li§ value) {}", + "interface Damaged extends Li§st {}", + "void damaged(Map> value) {}", + "Li§st damaged() = [\"x\"];", + ], + ) + fun `generic base completion preserves all written type arguments`(declaration: String) { + completion( + "module Headers { $declaration }", + "List", + 2, + if (declaration.contains("Li§st")) 2 else 0, + ) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(List<§> value) {}", + "void damaged(Map value) {}", + "void damaged(Map<§, Int> value) {}", + "void damaged(Map> value) {}", + "void damaged(List>> value) {}", + "List<§> property;", + "List<§> damaged() = [\"x\"];", + "interface Damaged extends List<§> {}", + ], + ) + fun `empty generic slots insert visible types at the cursor`(declaration: String) { + completion("module Headers { $declaration }", "String", 0, 0) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Str§ing value) {}", + "void damaged(List value) {}", + "void damaged(Map value) {}", + "void damaged((Int | Str§ing) value) {}", + "interface Damaged extends List {}", + ], + ) + fun `mid token queries replace the entire final identifier`(declaration: String) { + val qualified = declaration.contains("ecstasy.text") + completion( + "module Headers { $declaration }", + if (qualified) "StringBuffer" else "String", + 3, + if (qualified) 9 else 3, + ) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Ele§ value) {}", + "void damaged(List value) {}", + "void damaged(List<§> value) {}", + "Ele§ property;", + "class Nested { void damaged(Ele§ value) {} }", + ], + ) + fun `registered class formals are type candidates in their real enclosing scope`(declaration: String) { + completion( + "module Headers { class Container { $declaration } }", + "Element", + if (declaration.contains("Ele")) 3 else 0, + 0, + ) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "Owner.Ite§", + "Owner.Ite§m", + "Owner>.Ite§", + "Owner.Nested.Ite§", + "Owner.Ali§", + ], + ) + fun `parameterized qualifiers preserve compiler type substitution`(type: String) { + val alias = type.contains("Ali") + completion( + "module Headers { class Owner { class Item {} static class Nested { static class Item {} } " + + "typedef Element as Alias; } void damaged($type value) {} }", + if (alias) "Alias" else "Item", + 3, + if (type.endsWith("§m")) 1 else 0, + ) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "Missing.Ite§", + "Owner.Ite§", + "Value.Ite§", + "Owner.Hid§", + ], + ) + fun `invalid or inaccessible parameterized qualifiers never fall back to local names`(type: String) { + val marked = + "module Headers { class ItemOutside {} class HiddenOutside {} String Value = \"x\"; " + + "class Owner { class Item {} private class Hidden {} } void damaged($type value) {} }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))).isEmpty() + } + } + + @Test + fun `a valid generic method exposes its formal as a type instead of a local value`() { + val marked = "module Headers { void use(Element value) { Ele§; } }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + val candidates = + adapter.getCompletions(URI, 0, marked.indexOf('§')).filter { it.label == "Element" } + assertThat(candidates).hasSize(1) + assertThat(candidates.single().kind).isEqualTo(CompletionItem.CompletionKind.CLASS) + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "Type", "Type"]) + fun `a value shadowing a formal keeps its value completion`(type: String) { + val marked = + "module Headers { class Container { void use($type Element) { Ele§; } } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, marked.replace("Ele§;", "return;")).diagnostics) + .isEmpty() + adapter.compile(URI, marked.replace("§", "")) + val item = + adapter.getCompletions(URI, 0, marked.indexOf('§')).single { it.label == "Element" } + assertThat(item.kind).isEqualTo(CompletionItem.CompletionKind.VARIABLE) + } + } + + @ParameterizedTest + @ValueSource(strings = ["List<§", "Map + val cached = adapter.compile(URI, text) + assertThat(adapter.getCompletions(URI, 0, text.length).map { it.label }) + .contains("String") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(cached.diagnostics).isNotEmpty() + } + } + + @Test + fun `parameterized query facts retain no qualifier binding or validation on source syntax`() { + CompilerTestSupport.configure() + val marked = + "module Headers { class Owner { typedef Element as Alias; } void damaged(Owner.Ali§ value) {} }" + val text = marked.replace("§", "") + val source = Source(text, URI) + repeat(marked.indexOf('§')) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + val site = analysis.sites().single() + val syntax = site.target as NamedTypeExpression + assertThat(syntax.isValidated).isFalse() + assertThat(syntax.nameBindings.map { it.target() }).containsOnlyNulls() + val candidate = analysis.cursorBindings()[site]!!.types().single { it.name() == "Alias" } + assertThat(candidate.type().valueString).isEqualTo("String") + assertThat(source.toRawString()).isEqualTo(text) + val stopped = + EmbeddingSupport + .instance() + .analyzeIncomplete(source.clone(), cursor, null, ErrorList(1)) + assertThat(stopped.cursorBindings()).isEmpty() + val cancelled = ErrorListener.cancellable(ErrorList()) { true } + assertThat( + EmbeddingSupport + .instance() + .analyzeIncomplete(source.clone(), cursor, null, cancelled) + .cursorBindings(), + ).isEmpty() + } + + private fun completion( + marked: String, + selected: String, + before: Int, + after: Int, + ) { + val at = marked.indexOf('§') + val text = marked.replace("§", "") + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, text) + val item = adapter.getCompletions(URI, 0, at).single { it.label == selected } + assertThat(item.kind).isEqualTo(CompletionItem.CompletionKind.CLASS) + assertThat(item.textEdit) + .isEqualTo( + TextEdit(Range(Position(0, at - before), Position(0, at + after)), selected), + ) + assertThat(adapter.getSignatureHelp(URI, 0, at)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter + .compile( + URI, + text.take(at - before) + selected + text.substring(at + after), + ).diagnostics, + ).describedAs(marked) + .isEmpty() + } + } + + private companion object { + const val URI = "untitled:Headers.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt new file mode 100644 index 00000000000..fcadab9da59 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt @@ -0,0 +1,457 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkGraphMoveTest { + @TempDir lateinit var directory: Path + + @Test + fun `container proposal remaps explicit source and resource roots while preserving dependencies`() { + write("old/App.x", "module App { Box make() = new Box(); static String text() = $./data.txt; }") + write("old/App/Box.x", "class Box {}") + write("old/assets/data.txt", "moved resource") + write("fallback/data.txt", "fallback resource") + write("Consumer.x", "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }") + session { adapter -> + val graph = + listOf( + XdkSourceModule("App", uri("old/App.x"), resourceRoots = listOf(uri("old/assets"), uri("fallback"))), + XdkSourceModule("Consumer", uri("Consumer.x"), setOf("App"), emptyList()), + ) + adapter.replaceSourceModules(graph) + val moves = mapOf(uri("old") to uri("moved")) + assertThat(adapter.renameFilesAsync(moves).get(30, SECONDS)).isNull() + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + assertThat(proposal.previousSourceModules!!.map { it.uri }).containsExactlyInAnyOrderElementsOf(graph.map { it.uri }) + val after = requireNotNull(proposal.sourceModules) + val app = after.single { it.name == "App" } + assertThat(app.root).isEqualTo(directory.resolve("moved/App.x").toFile()) + assertThat( + app.resourceFiles, + ).containsExactly(directory.resolve("moved/assets").toFile(), directory.resolve("fallback").toFile()) + assertThat(after.single { it.name == "Consumer" }.resourceRoots).isEmpty() + assertThat(proposal.edit.renames).containsExactlyEntriesOf(moves) + assertThat(directory.resolve("moved")).doesNotExist() + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + apply(proposal.edit) + adapter.replaceSourceModules(after) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(Files.readString(directory.resolve("moved/assets/data.txt"))).isEqualTo("moved resource") + } + } + + @Test + fun `moving only the module root carries its companion and pins default resources left behind`() { + write("old/App.x", "module App { Box make() = new Box(); static String text() = $./data.txt; }") + write("old/App/Box.x", "class Box {}") + write("old/data.txt", "keep original resource lookup") + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")))) + val moves = mapOf(uri("old/App.x") to uri("target/App.x")) + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + assertThat(proposal.edit.renames).containsEntry(uri("old/App"), uri("target/App")) + assertThat(proposal.sourceModules!!.single().resourceFiles).containsExactly(directory.resolve("old").toFile()) + assertThat(proposal.previousSourceModules!!.single().resourceRoots).isNull() + apply(proposal.edit) + adapter.replaceSourceModules(proposal.sourceModules!!) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(directory.resolve("old/data.txt")).exists() + } + } + + @Test + fun `batch relocation remaps separately moved resources and retains disabled and missing roots`() { + write("old/App.x", "module App { static String text() = $./data.txt; }") + write("assets/data.txt", "resource") + write("Disabled.x", "module Disabled {}") + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri("old/App.x"), resourceRoots = listOf(uri("assets"), uri("missing"))), + XdkSourceModule("Disabled", uri("Disabled.x"), resourceRoots = emptyList()), + ), + ) + val moves = mapOf(uri("old") to uri("new"), uri("assets") to uri("new-assets")) + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + assertThat(proposal.sourceModules!!.single { it.name == "App" }.resourceFiles) + .containsExactly(directory.resolve("new-assets").toFile(), directory.resolve("missing").toFile()) + assertThat(proposal.sourceModules!!.single { it.name == "Disabled" }.resourceRoots).isEmpty() + apply(proposal.edit) + adapter.replaceSourceModules(proposal.sourceModules!!) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `resource lookup removed by a companion move refuses without reading the old path as future state`() { + write("old/App.x", "module App { static String text() = $./App/data.txt; }") + write("old/App/data.txt", "would be left under a different relative path") + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")))) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(adapter.renameFilesProposalAsync(mapOf(uri("old/App.x") to uri("target/App.x"))).get(30, SECONDS)).isNull() + assertThat(directory.resolve("old/App/data.txt")).exists() + assertThat(directory.resolve("target/App.x")).doesNotExist() + } + } + + @Test + fun `a moved resource must not silently select different fallback bytes in an independent module`() { + write("old/App.x", "module App {}") + write("old/App/data.txt", "original") + write("fallback/App/data.txt", "different fallback") + write("Independent.x", "module Independent { static String text() = $./App/data.txt; }") + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri("old/App.x")), + XdkSourceModule("Independent", uri("Independent.x"), resourceRoots = listOf(uri("old"), uri("fallback"))), + ), + ) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(adapter.renameFilesProposalAsync(mapOf(uri("old/App.x") to uri("target/App.x"))).get(30, SECONDS)).isNull() + assertThat(directory.resolve("old/App/data.txt")).exists() + } + } + + @Test + fun `uncaptured incoming resources cannot be approved using the old fallback lookup`() { + write("old/App.x", "module App { static String text() = $./data.txt; }") + write("assets/keep.txt", "existing root") + write("fallback/data.txt", "original") + write("incoming/data.txt", "would shadow the fallback") + session { adapter -> + adapter.replaceSourceModules( + listOf(XdkSourceModule("App", uri("old/App.x"), resourceRoots = listOf(uri("assets"), uri("fallback")))), + ) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val moves = mapOf(uri("old") to uri("moved"), uri("incoming/data.txt") to uri("assets/data.txt")) + assertThat(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)).isNull() + assertThat(directory.resolve("assets/data.txt")).doesNotExist() + } + } + + @ParameterizedTest + @CsvSource( + "String,$./data.txt", + "Byte[],#./data.txt", + "File,File:./data.txt", + "Directory,Directory:./nested/", + "FileStore,FileStore:./nested/", + ) + fun `container relocation preserves every embedded resource representation`( + type: String, + literal: String, + ) { + write( + "old/App.x", + "module App { import ecstasy.fs.File; import ecstasy.fs.Directory; import ecstasy.fs.FileStore; " + + "static $type value() = $literal; }", + ) + write("old/assets/data.txt", "resource bytes") + write("old/assets/nested/data.txt", "nested resource") + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x"), resourceRoots = listOf(uri("old/assets"))))) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val proposal = requireNotNull(adapter.renameFilesProposalAsync(mapOf(uri("old") to uri("new"))).get(30, SECONDS)) + apply(proposal.edit) + adapter.replaceSourceModules(requireNotNull(proposal.sourceModules)) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `interacting companion moves and directories outside module ownership refuse before replay`() { + write("old/App.x", "module App { tools.Box make() = new tools.Box(); }") + write("old/App/tools/Box.x", "class Box {}") + write("old/App/util/Marker.x", "class Marker {}") + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")))) + listOf( + mapOf(uri("old/App.x") to uri("target/App.x"), uri("old/App/tools/Box.x") to uri("old/App/util/Box.x")), + mapOf(uri("old/App/tools") to uri("target/tools")), + // VS Code participates again when applying the first proposal's companion move. + mapOf(uri("old/App") to uri("target/App")), + ).forEach { moves -> assertThat(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)).isNull() } + assertThat(directory.resolve("old/App/tools/Box.x")).exists() + } + } + + @Test + fun `parent and independently relocated child apply child first regardless of request order`() { + write("old/App.x", "module App {}") + write("old/App/Box.x", "class Box {}") + write("old/App/Box/Part.x", "static class Part {}") + write("old/App/Box/data.txt", "keep bytes") + write("Other.x", "module Other {}") + Files.createDirectories(directory.resolve("Other")) + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")), XdkSourceModule("Other", uri("Other.x")))) + val moves = mapOf(uri("old") to uri("new"), uri("old/App/Box.x") to uri("Other/Box.x")) + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + val reversed = + requireNotNull(adapter.renameFilesProposalAsync(moves.entries.reversed().associate { it.toPair() }).get(30, SECONDS)) + assertThat( + proposal.edit.renames.entries + .toList(), + ).isEqualTo( + reversed.edit.renames.entries + .toList(), + ) + assertThat( + proposal.edit.renames.keys + .last(), + ).isEqualTo(uri("old")) + apply(proposal.edit) + adapter.replaceSourceModules(requireNotNull(proposal.sourceModules)) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(Files.readString(directory.resolve("Other/Box/data.txt"))).isEqualTo("keep bytes") + assertThat(directory.resolve("new/App/Box.x")).doesNotExist() + } + } + + @Test + fun `redundant child relocation is coalesced with its parent`() { + write("old/App.x", "module App {}") + write("old/App/Box.x", "class Box {}") + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")))) + val proposal = + requireNotNull( + adapter + .renameFilesProposalAsync( + mapOf( + uri("old") to uri("new"), + uri("old/App/Box.x") to uri("new/App/Box.x"), + ), + ).get(30, SECONDS), + ) + assertThat(proposal.edit.renames).containsExactlyEntriesOf(mapOf(uri("old") to uri("new"))) + apply(proposal.edit) + adapter.replaceSourceModules(requireNotNull(proposal.sourceModules)) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `existing destination and overlapping requests refuse a graph transaction`() { + write("old/App.x", "module App {}") + write("taken/Keep.x", "module Keep {}") + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")))) + listOf( + mapOf(uri("old") to uri("taken")), + mapOf(uri("old") to uri("new"), uri("old/App.x") to uri("elsewhere/App.x")), + ).forEach { moves -> assertThat(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)).isNull() } + assertThat(directory.resolve("old/App.x")).exists() + } + } + + @ParameterizedTest + @CsvSource("App,false", "App,true", "App.example.org,false", "App.example.org,true") + fun `root rename and move replace dependencies and preserve resource policy`( + moduleName: String, + customResources: Boolean, + ) { + write( + "old/App.x", + """ + module $moduleName { + Box make() = new Box(); + static String text() = $./data.txt; + } + """.trimIndent(), + ) + write("old/App/Box.x", "class Box {}") + write("old/data.txt", "default resource") + write("assets/data.txt", "custom resource") + write("fallback/data.txt", "fallback resource") + write( + "Consumer.x", + """ + module Consumer { + package app import $moduleName; + app.Box make() = new app.Box(); + String read() = app.text(); + } + """.trimIndent(), + ) + Files.createDirectories(directory.resolve("target")) + val renamed = "Renamed" + moduleName.removePrefix("App") + session { adapter -> + val roots = if (customResources) listOf(uri("assets"), uri("fallback")) else null + val graph = + listOf( + XdkSourceModule(moduleName, uri("old/App.x"), resourceRoots = roots), + XdkSourceModule("Consumer", uri("Consumer.x"), setOf(moduleName), emptyList()), + ) + adapter.replaceSourceModules(graph) + val requested = mapOf(uri("old/App.x") to uri("target/Renamed.x")) + assertThat(adapter.renameFilesAsync(requested).get(30, SECONDS)).isNull() + val proposal = requireNotNull(adapter.renameFilesProposalAsync(requested).get(30, SECONDS)) + val after = requireNotNull(proposal.sourceModules) + assertThat(proposal.edit.renames).containsEntry(uri("old/App"), uri("target/Renamed")) + assertThat(proposal.previousSourceModules!!.single { it.name == moduleName }.resourceFiles) + .isEqualTo(if (customResources) listOf("assets", "fallback").map { directory.resolve(it).toFile() } else null) + assertThat(after.single { it.name == renamed }.root).isEqualTo(directory.resolve("target/Renamed.x").toFile()) + assertThat(after.single { it.name == renamed }.resourceFiles) + .containsExactlyElementsOf( + (if (customResources) listOf("assets", "fallback") else listOf("old")).map { directory.resolve(it).toFile() }, + ) + assertThat(after.single { it.name == "Consumer" }.dependencies).containsExactly(renamed) + assertThat(after.single { it.name == "Consumer" }.resourceRoots).isEmpty() + assertThat(directory.resolve("target/Renamed.x")).doesNotExist() + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + apply(proposal.edit) + adapter.replaceSourceModules(after) + assertThat(Files.readString(directory.resolve("target/Renamed.x"))).contains("module $renamed") + assertThat(Files.readString(directory.resolve("Consumer.x"))).contains("package app import $renamed", "app.Box", "app.text()") + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val reverse = adapter.renameFilesProposalAsync(requested.entries.associate { (from, to) -> to to from }).get(30, SECONDS) + requireNotNull(reverse) + apply(reverse.edit) + adapter.replaceSourceModules(requireNotNull(reverse.sourceModules)) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(Files.readString(directory.resolve("Consumer.x"))).contains("package app import $moduleName") + } + } + + @Test + fun `captured incoming resources permit unchanged lookups and retain new unused bytes`() { + write("old/App.x", "module App { static String text() = $./data.txt; }") + write("assets/keep.txt", "existing root") + write("fallback/data.txt", "original") + write("incoming/data.txt", "original") + write("incoming/extra.txt", "previously outside every root") + session { adapter -> + adapter.replaceSourceModules( + listOf(XdkSourceModule("App", uri("old/App.x"), resourceRoots = listOf(uri("assets"), uri("fallback")))), + ) + val moves = + mapOf( + uri("old") to uri("moved"), + uri("incoming/data.txt") to uri("assets/data.txt"), + uri("incoming/extra.txt") to uri("assets/extra.txt"), + ) + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + apply(proposal.edit) + adapter.replaceSourceModules(requireNotNull(proposal.sourceModules)) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(Files.readString(directory.resolve("assets/extra.txt"))).isEqualTo("previously outside every root") + } + } + + @Test + fun `root rename and move refuse a duplicate module identity without touching files or graph`() { + write("old/App.x", "module App {}") + write("Taken.x", "module Taken {}") + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("old/App.x")), XdkSourceModule("Taken", uri("Taken.x")))) + val requested = mapOf(uri("old/App.x") to uri("target/Taken.x")) + assertThat(adapter.renameFilesProposalAsync(requested).get(30, SECONDS)).isNull() + assertThat(Files.readString(directory.resolve("old/App.x"))).isEqualTo("module App {}") + assertThat(directory.resolve("target/Taken.x")).doesNotExist() + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `several relocated module renames update interdependent imports in one proposal`() { + write("old/Library.x", "module Library { class Item {} }") + write( + "old/Consumer.x", + """ + module Consumer { + package lib import Library; + lib.Item make() = new lib.Item(); + } + """.trimIndent(), + ) + Files.createDirectories(directory.resolve("target")) + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", uri("old/Library.x")), + XdkSourceModule("Consumer", uri("old/Consumer.x"), setOf("Library")), + ), + ) + val moves = mapOf(uri("old/Library.x") to uri("target/Api.x"), uri("old/Consumer.x") to uri("target/Client.x")) + val proposal = requireNotNull(adapter.renameFilesProposalAsync(moves).get(30, SECONDS)) + val graph = requireNotNull(proposal.sourceModules) + assertThat(graph.map { it.name }).containsExactlyInAnyOrder("Api", "Client") + assertThat(graph.single { it.name == "Client" }.dependencies).containsExactly("Api") + apply(proposal.edit) + adapter.replaceSourceModules(graph) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat( + Files.readString(directory.resolve("target/Client.x")), + ).contains("module Client", "package lib import Api", "lib.Item") + } + } + + private fun session(body: (XdkAdapter) -> Unit) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + body(adapter) + } + } + + private fun write( + file: String, + source: String, + ) { + directory.resolve(file).toFile().apply { + parentFile.mkdirs() + writeText(source) + } + } + + private fun uri(file: String) = + directory + .resolve(file) + .toFile() + .toURI() + .toString() + .removeSuffix("/") + + private fun apply(edit: WorkspaceEdit) { + edit.changes.forEach { (uri, changes) -> + val path = Path.of(URI(uri)) + val text = Files.readString(path) + + fun offset(position: Position): Int = text.lineSequence().take(position.line).sumOf { it.length + 1 } + position.column + Files.writeString( + path, + changes + .sortedByDescending { offset(it.range.start) } + .fold(text) { value, change -> + value.replaceRange( + offset(change.range.start), + offset(change.range.end), + change.newText, + ) + }, + ) + } + edit.renames.forEach { (from, to) -> + Files.move(Path.of(URI(from)), Path.of(URI(to))) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt new file mode 100644 index 00000000000..09f80e93662 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt @@ -0,0 +1,153 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkHeaderSlotsTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(ecstasy.text.§ value) {}", + "void damaged(List value) {}", + "ecstasy.text.§ property;", + "void damaged(ecstasy.te§xt.StringBuffer value) {}", + "void damaged((Int | §) value) {}", + "void damaged((§ | Int) value) {}", + "void damaged(Int | § value) {}", + "void damaged(List<(Int | §)> value) {}", + "void damaged(Object + § value) {}", + "void damaged(Object - § value) {}", + "(Int, Str§) damaged() { return 1, \"x\"; }", + "(Int count, Str§ text) damaged() { return 1, \"x\"; }", + "conditional Str§ damaged() { return True, \"x\"; }", + " void damaged(Str§ value) {}", + " Str§ damaged(T value) = \"x\";", + " void damaged(T value) {}", + "class Damaged {}", + "package damaged incorporates SharedMi§ {} mixin SharedMixin into Package {}", + ], + ) + fun `header slots retain exact edits and compile after acceptance`(declaration: String) { + val marked = "module Headers { $declaration }" + val at = marked.indexOf('§') + val before = + when { + declaration.contains("te§xt") -> 2 + declaration.contains("Str§") -> 3 + declaration.contains("SharedMi§") -> 8 + else -> 0 + } + val after = if (declaration.contains("te§xt")) 2 else 0 + val selected = + when { + declaration.contains("te§xt") -> "text" + declaration.contains("text.§") -> "StringBuffer" + declaration.contains("SharedMi§") -> "SharedMixin" + else -> "String" + } + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, source) + val item = adapter.getCompletions(URI, 0, at).single { it.label == selected } + assertThat(item.textEdit) + .describedAs(declaration) + .isEqualTo( + TextEdit(Range(Position(0, at - before), Position(0, at + after)), selected), + ) + assertThat(adapter.getSignatureHelp(URI, 0, at)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter + .compile(URI, source.replaceRange(at - before, at + after, selected)) + .diagnostics, + ).describedAs(declaration) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = [" void damaged(Str§ value) {}", "class Damaged extends Str§ {}"], + ) + fun `unregistered formals shadow outer types and report their own bound`(declaration: String) { + val marked = "module Headers { $declaration }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat( + adapter + .getCompletions(URI, 0, marked.indexOf('§')) + .single { it.label == "String" } + .detail, + ).isEqualTo("type parameter String extends Object") + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "module Headers incorporates SharedMi§ { class Hidden {} }", + "module Headers implements ecstasy.text.Str§ { class Hidden {} }", + ], + ) + fun `module header queries preserve diagnostics and never expose an invented body scope`(marked: String) { + XdkAdapter().use { adapter -> + val source = marked.replace("§", "") + val cached = adapter.compile(URI, source) + val completions = adapter.getCompletions(URI, 0, marked.indexOf('§')) + assertThat(cached.diagnostics).isNotEmpty() + assertThat(cached.diagnostics.map { it.code }).doesNotContain("EMB-5") + if (marked.contains("ecstasy.text.Str")) { + val input = Source(source, URI) + repeat(marked.indexOf('§')) { input.next() } + val cursor = input.position + input.reset() + val errors = ErrorList() + val analysis = + EmbeddingSupport.instance().analyzeIncomplete(input, cursor, null, errors) + val root = analysis.sites().single().parent as IncompleteTypeCompositionStatement + assertThat(root.component.getChild("Hidden")).isNull() + assertThat(errors.errors.map { it.code }) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat( + completions.map { + it.label + }, + ).describedAs( + "%s; sites=%s; bindings=%s", + errors.errors, + analysis.sites(), + analysis.cursorBindings(), + ).contains("String") + } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `trailing parameterized qualifier resolves its written arguments`() { + val prefix = "module Headers { class Owner { class Item {} } void damaged(Owner." + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix value) {} }") + val item = adapter.getCompletions(URI, 0, prefix.length).single { it.label == "Item" } + assertThat(item.textEdit) + .isEqualTo( + TextEdit(Range(Position(0, prefix.length), Position(0, prefix.length)), "Item"), + ) + assertThat(adapter.compile(URI, "${prefix}Item value) {} }").diagnostics).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Headers.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt new file mode 100644 index 00000000000..97d1f1baae5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt @@ -0,0 +1,68 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class XdkImportCompletionLifecycleTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["edit", "close", "configuration", "cancel"]) + fun `retired import proof cannot publish ordinary or import completions`(change: String) { + CompilerTestSupport.configure() + val support = EmbeddingSupport.instance() + val hold = AtomicBoolean() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val text = "module App { Doc value; }" + val uri = + directory + .resolve("App.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + if (hold.compareAndSet(true, false)) { + entered.countDown() + check(release.await(20, SECONDS)) + // A callback that ignores cancellation still cannot publish stale edits. + support.compileModule(sources, repository, ErrorList()) + } else { + support.compileModule(sources, repository, errors) + } + }, + { source, _, cursor, repository, errors -> support.analyzeIncomplete(source, cursor, repository, errors) }, + ).use { adapter -> + try { + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.compile(uri, text) + hold.set(true) + val query = adapter.getCompletionsAsync(uri, 0, text.indexOf("Doc") + 3, null) + assertThat(entered.await(20, SECONDS)).isTrue() + when (change) { + "edit" -> adapter.compileAsync(uri, "\n$text") + "close" -> adapter.closeDocument(uri) + "configuration" -> adapter.replaceSourceModules(emptyList()) + "cancel" -> query.cancel(false) + else -> error("Unknown change: $change") + } + assertThat(query.isCancelled).isTrue() + } finally { + release.countDown() + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt new file mode 100644 index 00000000000..9588740d46d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt @@ -0,0 +1,103 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +class XdkImportCompletionTest { + @TempDir lateinit var directory: Path + + @Test + fun `completion replaces the whole token and imports a bundled type atomically`() { + val text = "module App {\r\n // 😀 retain UTF-16 positions\r\n Doc§ument value;\r\n}" + withCompletions(text) { adapter, uri, original, items -> + val item = items.single { it.label == "Document" && it.additionalTextEdits.isNotEmpty() } + assertThat(item.detail).contains("xml.xtclang.org") + assertThat(item.textEdit?.newText).isEqualTo("Document") + assertThat(item.textEdit?.range).isEqualTo(Range(Position(2, 4), Position(2, 12))) + val changed = apply(original, listOf(requireNotNull(item.textEdit)) + item.additionalTextEdits) + assertThat(changed).contains("import xml.Document;", "// 😀 retain UTF-16 positions\r\n") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `same named source candidates retain separate imports and private types are absent`() { + source("First", "module First { class Widget {} private class Widden {} }") + source("Second", "module Second { class Widget {} }") + withCompletions("module App { Wid§ value; }") { _, _, _, items -> + val imported = items.filter { it.additionalTextEdits.isNotEmpty() } + assertThat(imported.map { it.label }).containsExactly("Widget", "Widget") + assertThat(imported.map { it.detail }).containsExactly( + "Widget — import from First", + "Widget — import from Second", + ) + } + } + + @Test + fun `explicit graph does not gain an undeclared dependency through completion`() { + val library = source("Library", "module Library { class Widget {} }") + val text = "module App { Wid value; }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("Library", library), XdkSourceModule("App", uri))) + adapter.compile(uri, text) + assertThat(adapter.getCompletions(uri, 0, text.indexOf("Wid") + 3).filter { it.additionalTextEdits.isNotEmpty() }).isEmpty() + } + } + + @Test + fun `unrelated errors and qualified member sites do not get import edits`() { + listOf( + "module App { Doc§ value; Missing broken; }", + "module App { void run(String text) { text.Doc§; } }", + ).forEach { marked -> + withCompletions(marked) { _, _, _, items -> + assertThat(items.filter { it.additionalTextEdits.isNotEmpty() }).isEmpty() + } + } + } + + private fun withCompletions( + marked: String, + check: (XdkAdapter, String, String, List) -> Unit, + ) { + val offset = marked.indexOf('§') + val text = marked.replace("§", "") + val before = text.take(offset) + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.compile(uri, text) + val items = adapter.getCompletions(uri, before.count { it == '\n' }, before.substringAfterLast('\n').length) + check(adapter, uri, text, items) + } + } + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { current, edit -> + fun offset(position: Position) = current.splitToSequence('\n').take(position.line).sumOf { it.length + 1 } + position.column + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt new file mode 100644 index 00000000000..746671232b5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt @@ -0,0 +1,144 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS + +class XdkIncompleteCallTest { + @Test + fun `written argument types filter overloads without selecting an unfinished call`() { + XdkAdapter().use { adapter -> + for ((argument, expected) in listOf("\"x\"" to "String", "1" to "Int")) { + val prefix = "$BOX void run(Box box) { box.choose($argument, " + adapter.compile(URI, "$prefix); } }") + val cached = adapter.getCachedResult(URI) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(prefix).isNotNull() + requireNotNull(help) + assertThat(help.signatures).hasSize(1) + assertThat(help.signatures.single().label) + .isEqualTo("$expected choose($expected first, $expected second)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + assertThat(help.signatures.single().documentation) + .contains("written arguments fit", "overload not selected") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + } + + @Test + fun `candidate inference copies expected types and named argument mappings`() { + XdkAdapter().use { adapter -> + for ( + (call, parameter) in + listOf( + "pair(\"x\", second =" to 1, + "pair(second = \"x\", first =" to 0, + "pair(second = \"x\"" to 1, + "generic(\"x\", " to 1, + ) + ) { + val prefix = "$BOX void run(Box box) { box.$call" + adapter.compile(URI, "$prefix); } }") + val position = Position(0, prefix.length) + val model = adapter.analyzeAtAsync(URI, position).get(10, SECONDS)!! + val site = model.sites.single() + val candidate = site.callCandidates!!.single() + val cursor = SemanticModel.Position(0, prefix.length) + assertThat(site.parameterAt(candidate, cursor)) + .describedAs(call) + .isEqualTo(parameter) + assertThat( + model.semantics.type(site.expectedTypeAt(candidate, cursor)!!)!!.displayName, + ).isEqualTo("String") + Executors.newSingleThreadExecutor().use { executor -> + assertThat( + executor + .submit { + model.semantics + .type(site.expectedTypeAt(candidate, cursor)!!)!! + .displayName + }.get(), + ).isEqualTo("String") + } + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().activeParameter) + .describedAs(call) + .isEqualTo(parameter) + assertThat(help.signatures.single().label).contains("String first", "String second") + assertThat(model.semantics.calls.map { model.semantics.symbol(it.method)?.name }) + .doesNotContain("pair", "generic") + } + } + } + + @Test + fun `invalid names duplicate bindings and incompatible arguments produce no applicable signatures`() { + XdkAdapter().use { adapter -> + for (call in listOf("pair(unknown =", "pair(first = \"x\", first =", "choose(True, ")) { + val prefix = "$BOX void run(Box box) { box.$call" + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(call) + .isNull() + } + } + } + + @Test + fun `unbound generic parameters remain formal and do not become Object`() { + val prefix = "$BOX void run(Box box) { box.generic(" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().label) + .contains("U first", "U second") + .doesNotContain("Pending", "Object") + } + } + + @Test + fun `candidate fitting checks a captured lambda without selecting or emitting the call`() { + val prefix = + "module Editing { Int apply(function Int(Int) fn, Int second) = fn(second); " + + "void run(Int outer) { apply((Int n) -> n + outer, " + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, "${prefix}1); } }").diagnostics).isEmpty() + adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().label).contains("apply(", "Int second") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + assertThat(help.signatures.single().documentation).contains("overload not selected") + } + } + + @Test + fun `unqualified and static call candidates use compiler lookup`() { + XdkAdapter().use { adapter -> + for (receiver in listOf("", "Box.")) { + val prefix = + "module Editing { class Box { static String pair(String first, String second) = first; " + + "void run() { ${receiver}pair(\"x\", " + assertThat(adapter.compile(URI, "$prefix\"y\"); } } }").diagnostics).isEmpty() + adapter.compile(URI, "$prefix); } } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(receiver).isNotNull() + requireNotNull(help) + assertThat(help.signatures.single().label) + .isEqualTo("String pair(String first, String second)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + const val BOX = + "module Editing { class Box { " + + "String choose(String first, String second) = first; Int choose(Int first, Int second) = first; " + + "T pair(T first, T second) = first; U generic(U first, U second) = first; }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt new file mode 100644 index 00000000000..bf980a6d40d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt @@ -0,0 +1,95 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkIncompleteConstructorTest { + @Test + fun `constructor signatures fit written arguments without selecting an overload`() { + val declarations = + "module Editing { class Box { construct(Int first, String second) {} " + + "construct(String first, Int second = 0) {} } void run() { new Box(" + XdkAdapter().use { adapter -> + for ( + (argument, labels) in + listOf( + "" to + listOf( + "new Box(Int first, String second)", + "new Box(String first, Int second = …)", + ), + "1, " to listOf("new Box(Int first, String second)"), + "\"x\", " to listOf("new Box(String first, Int second = …)"), + ) + ) { + val prefix = declarations + argument + adapter.compile(URI, "$prefix); } }") + val cached = adapter.getCachedResult(URI) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(prefix).isNotNull() + assertThat(help!!.signatures.map { it.label }) + .containsExactlyInAnyOrderElementsOf(labels) + assertThat(help.signatures.map { it.activeParameter }) + .containsOnly(if (argument.isEmpty()) 0 else 1) + assertThat(help.signatures.map { it.documentation }).allMatch { + it!!.contains("overload not selected") + } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + } + + @Test + fun `generic constructors retain argument names and inferred expected types in value positions`() { + val declarations = + "module Editing { class Box { construct(T first, T second) {} } void run() { " + XdkAdapter().use { adapter -> + for ( + (call, parameter) in + listOf( + "new Box(\"x\", " to 1, + "Box box = new Box(second = \"x\", first =" to 0, + ) + ) { + val prefix = declarations + call + adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(prefix).isNotNull() + assertThat(help!!.signatures.single().label) + .isEqualTo("new Box(String first, String second)") + assertThat(help.signatures.single().activeParameter).isEqualTo(parameter) + } + } + } + + @Test + fun `inaccessible abstract and incompatible constructors have no candidates`() { + val declarations = + "module Editing { class Box { construct(Int first, String second) {} } " + + "class Hidden { private construct(Int value) {} } @Abstract class AbstractBox { construct(Int value) {} } " + + "void run() { " + XdkAdapter().use { adapter -> + for ( + call in + listOf( + "new Box(True, ", + "new Box(unknown =", + "new Hidden(", + "new AbstractBox(", + "new Missing(", + ) + ) { + val prefix = declarations + call + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(call) + .isNull() + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt new file mode 100644 index 00000000000..93c9f687455 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt @@ -0,0 +1,128 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS + +class XdkIncompleteFunctionTest { + @ParameterizedTest + @ValueSource(strings = ["fn(", "fn(1", "fn(1, "]) + fun `function parameters supply signatures and positional expected types`(call: String) { + val prefix = "module Editing { void run(function Int(Int, String) fn) { $call" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val cached = adapter.getCachedResult(URI) + val position = Position(0, prefix.length) + val model = adapter.analyzeAtAsync(URI, position).get(10, SECONDS)!! + val site = model.sites.single() + val candidate = site.functions.single() + val cursor = SemanticModel.Position(position.line, position.column) + val parameter = if (call.endsWith(", ")) 1 else 0 + assertThat(site.callCandidates).isEmpty() + assertThat(candidate.signature.parameters.map { it.name }).containsOnlyNulls() + assertThat(site.parameterAt(candidate, cursor)).isEqualTo(parameter) + Executors.newSingleThreadExecutor().use { executor -> + assertThat( + executor + .submit { + model.semantics + .type(site.expectedTypeAt(candidate, cursor)!!)!! + .displayName + }.get(), + ).isEqualTo(if (parameter == 0) "Int" else "String") + } + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().label).isEqualTo("Int fn(Int, String)") + assertThat(help.signatures.single().activeParameter).isEqualTo(parameter) + assertThat(help.signatures.single().documentation).contains("runtime target unknown") + assertThat(model.semantics.calls).isEmpty() + assertThat(model.semantics.functionCalls).isEmpty() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `function properties and narrowed nullable locals use the current compiler type`() { + for ( + (prefix, suffix) in + listOf( + "module Editing { class Holder(function T(T, String) fn) {} void run(Holder holder) { holder.fn(1, " to + "); } }", + "module Editing { void run(function Int(Int, String)? fn) { if (fn != Null) { fn(1, " to + "); } } }", + ) + ) { + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(prefix).isNotNull() + assertThat(help!!.signatures.single().label).isEqualTo("Int fn(Int, String)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + } + } + } + + @Test + fun `function producing expressions supply types without inventing a callee name`() { + val prefix = + "module Editing { function Int(Int, String) make(function Int(Int, String) fn) = fn; " + + "void run(function Int(Int, String) fn) { make(fn)(1, " + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().label).isEqualTo("Int function(Int, String)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + } + } + + @Test + fun `captured lambda arguments are fitted against the known function parameter`() { + val prefix = + "module Editing { void run(function Int(function Int(Int), String) fn, Int captured) { " + + "fn((Int n) -> n + captured, " + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length)!! + assertThat(help.signatures.single().label).contains("fn(", "String)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + } + } + + @ParameterizedTest + @ValueSource(strings = ["fn(True, ", "fn(name =", "fn(name = 1, ", "fn(1, \"x\", True, "]) + fun `function signatures reject incompatible named and excess written arguments`(call: String) { + val prefix = "module Editing { void run(function Int(Int, String) fn) { $call" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + } + } + + @Test + fun `unreadable nullable and shadowing nonfunctions do not acquire signatures`() { + for ( + prefix in + listOf( + "module Editing { void run() { function Int(Int) fn; fn(", + "module Editing { void run(function Int(Int)? fn) { fn(", + "module Editing { Int fn(Int n) = n; void run() { Int fn = 1; fn(", + ) + ) { + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(prefix) + .isNull() + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt new file mode 100644 index 00000000000..b242c969d87 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt @@ -0,0 +1,132 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path + +class XdkIndexLifecycleTest { + @TempDir lateinit var directory: Path + + @Test + fun `joining graph views preserves document lambda import and expression facts`() { + CompilerTestSupport.configure() + val text = + "module Joined { import ecstasy.maps.ListMap as MapImpl; " + + "function Int(Int) create() { MapImpl map = new MapImpl(); return (Int input) -> input + map.size; } }" + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, "Joined.x"), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + val before = result.semanticSnapshot() + assertThat(before.lambdas).hasSize(1) + assertThat(before.sourceLinks).hasSize(1) + assertThat(before.imports).hasSize(1) + val joined = SemanticModel.joined(listOf(before), emptyMap()).single() + assertThat(joined.lambdas).isEqualTo(before.lambdas) + assertThat(joined.sourceLinks).isEqualTo(before.sourceLinks) + assertThat(joined.imports).isEqualTo(before.imports) + assertThat(joined.expressions).isEqualTo(before.expressions) + assertThat(joined.functionCalls).isEqualTo(before.functionCalls) + assertThat(joined.occurrences).isEqualTo(before.occurrences) + assertThat(joined.calls).isEqualTo(before.calls) + } + + @Test + fun `replacing binary indices source authority and graphs retires old hierarchy handles`() { + CompilerTestSupport.configure() + val libraryText = "module Library { class Box { Int pick(Int value) = value; } }" + val first = source("first/Library.x", libraryText) + val second = source("second/Library.x", libraryText) + val sourceText = "module Library {\n class Box { Int pick(Int value) = value + 1; }\n}" + val editable = source("editable/Library.x", sourceText) + val text = + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); " + + "Int forward(lib.Box box) = run(box); }" + val consumer = source("Consumer.x", text) + val firstArtifact = artifact(libraryText, first) + val secondArtifact = artifact(libraryText, second) + val graph = XdkSourceModule("Consumer", consumer, setOf("Library")) + val call = text.indexOf("pick") + val run = text.indexOf("forward") + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(firstArtifact)) + adapter.replaceSourceModules(listOf(graph)) + assertThat(adapter.findDefinition(consumer, 0, call)?.uri).isEqualTo(first) + val old = adapter.prepareCallHierarchy(consumer, 0, run).single() + assertThat( + adapter + .getOutgoingCalls(old) + .single() + .to.name, + ).isEqualTo("run") + + adapter.replaceDependencies(listOf(secondArtifact)) + assertThat(adapter.getOutgoingCalls(old)).isEmpty() + assertThat(adapter.findDefinition(consumer, 0, call)?.uri).isEqualTo(second) + val indexed = adapter.prepareCallHierarchy(consumer, 0, run).single() + assertThat(indexed.data).isNotEqualTo(old.data) + assertThat( + adapter + .getOutgoingCalls(indexed) + .single() + .to.name, + ).isEqualTo("run") + + adapter.replaceDependencies(listOf(XdkDependency.fromBinary(secondArtifact.bytes()))) + assertThat(adapter.getOutgoingCalls(indexed)).isEmpty() + assertThat(adapter.findDefinition(consumer, 0, call)).isNull() + val binary = adapter.prepareCallHierarchy(consumer, 0, run).single() + assertThat( + adapter + .getOutgoingCalls(binary) + .single() + .to.name, + ).isEqualTo("run") + + adapter.replaceSourceModules(listOf(XdkSourceModule("Library", editable), graph)) + assertThat(adapter.getOutgoingCalls(binary)).isEmpty() + assertThat(adapter.findDefinition(consumer, 0, call)?.uri).isEqualTo(editable) + assertThat(adapter.findDefinition(consumer, 0, call)?.startLine).isEqualTo(1) + val current = adapter.prepareCallHierarchy(consumer, 0, run).single() + assertThat( + adapter + .getOutgoingCalls(current) + .single() + .to.name, + ).isEqualTo("run") + + adapter.replaceSourceModules(emptyList()) + assertThat(adapter.findWorkspaceSymbols("Box")).isEmpty() + assertThat(adapter.getOutgoingCalls(current)).isEmpty() + } + } + + private fun source( + path: String, + text: String, + ): String = + directory.resolve(path).toFile().let { + it.parentFile.mkdirs() + it.writeText(text) + it.canonicalFile.toURI().toString() + } + + private fun artifact( + text: String, + uri: String, + ): XdkDependency { + val errors = ErrorList() + val result = EmbeddingSupport.instance().compileModule(Source(text, uri), null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.toDependency() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt new file mode 100644 index 00000000000..f20fa1e73a2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt @@ -0,0 +1,78 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkInferredPresentationTest { + @Test + fun `lambda parameter and return hints use the function signature without captures`() { + val source = + "module Inferred { Int run(Int captured) { function Int(Int) fn = (value) -> value + captured; return fn(1); } }" + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, source) + assertThat(result.diagnostics).isEmpty() + val types = + adapter.getInlayHints(URI, ALL).filter { it.kind == InlayHint.InlayHintKind.TYPE } + assertThat(types.map { it.position.column }) + .containsExactly(source.indexOf("value)") + 5, source.indexOf("->")) + assertThat(types.map { it.label }).containsExactly(": Int", ": Int") + assertThat(adapter.getHoverInfo(URI, 0, source.indexOf("value)"))).contains("Int") + assertThat( + adapter.getInlayHints( + URI, + Range( + Position(0, source.indexOf("->")), + Position(0, source.indexOf("->") + 2), + ), + ), + ).hasSize(1) + } + } + + @Test + fun `explicit lambda parameters omit type hints but retain the inferred return`() { + val source = + "module Inferred { void run() { function String(String) fn = (String value) -> value; } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getInlayHints(URI, ALL).map { it.label }).containsExactly(": String") + } + } + + @Test + fun `destructured declarations show their independently inferred result types`() { + val source = + "module Inferred { (Int, String) pair() = (1, \"x\"); void run() { (var number, var text) = pair(); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getInlayHints(URI, ALL).map { it.label }) + .containsExactly(": Int", ": String") + assertThat(adapter.getHoverInfo(URI, 0, source.indexOf("number,"))).contains("Int") + assertThat(adapter.getHoverInfo(URI, 0, source.indexOf("text)"))).contains("String") + } + } + + @Test + fun `replacement failure repair and close never retain inferred lambda facts`() { + val source = "module Inferred { void run() { function Int(Int) fn = (value) -> value; } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getInlayHints(URI, ALL).map { it.label }) + .containsExactly(": Int", ": Int") + assertThat(adapter.compile(URI, source.replace("-> value", "-> missing")).success) + .isFalse() + assertThat(adapter.getInlayHints(URI, ALL)).isEmpty() + assertThat(adapter.compile(URI, source.replace("Int", "String")).diagnostics).isEmpty() + assertThat(adapter.getInlayHints(URI, ALL).map { it.label }) + .containsExactly(": String", ": String") + adapter.closeDocument(URI) + assertThat(adapter.getInlayHints(URI, ALL)).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Inferred.x" + val ALL = Range(Position(0, 0), Position(100, 0)) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt new file mode 100644 index 00000000000..0323f1e6c7f --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt @@ -0,0 +1,64 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.treesitter.SemanticTokenLegend +import java.nio.file.Files +import java.nio.file.Path + +class XdkInitializerTest { + @Test + fun `folded initializer participates in hover navigation references tokens and complete rename`( + @TempDir directory: Path, + ) { + val source = "module Folded { Int value = 1; Int copy = value; Int run() = value; }" + val path = directory.resolve("Folded.x") + Files.writeString(path, source) + val uri = path.toUri().toString() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("Folded", uri))) + assertThat(adapter.compile(uri, source).success).isTrue() + val offset = source.indexOf("value; Int run") + assertThat(adapter.getHoverInfo(uri, 0, offset)).contains("Int") + assertThat(adapter.findDefinition(uri, 0, offset)?.startColumn) + .isEqualTo(source.indexOf("value")) + assertThat(adapter.findReferences(uri, 0, offset, true)).hasSize(3) + val tokens = + requireNotNull(adapter.getSemanticTokens(uri)) + .data + .chunked(5) + .runningFold(listOf(0, 0, 0, 0, 0)) { previous, delta -> + listOf( + previous[0] + delta[0], + if (delta[0] == 0) previous[1] + delta[1] else delta[1], + ) + delta.drop(2) + }.drop(1) + val reference = tokens.single { it[0] == 0 && it[1] == offset } + assertThat(SemanticTokenLegend.tokenTypes[reference[3]]).isEqualTo("property") + assertThat(adapter.prepareRename(uri, 0, offset)).isNotNull() + val edits = + requireNotNull(adapter.rename(uri, 0, offset, "number")).changes.getValue(uri) + assertThat(edits).hasSize(3) + val renamed = + edits + .sortedByDescending { it.range.start.column } + .fold(source) { text, edit -> + text.replaceRange( + edit.range.start.column, + edit.range.end.column, + edit.newText, + ) + } + assertThat(renamed).doesNotContain("value").contains("copy = number") + assertThat(adapter.compile(uri, renamed).success).isTrue() + adapter.closeDocument(uri) + assertThat( + adapter.compile(uri, source.replace("copy = value", "copy = missing")).success, + ).isFalse() + assertThat(adapter.prepareRename(uri, 0, offset)).isNull() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt new file mode 100644 index 00000000000..3bf82c6b43e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt @@ -0,0 +1,142 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkInlineCompletions +import java.util.concurrent.TimeUnit.SECONDS + +class XdkInlineCompletionTest { + @ParameterizedTest + @ValueSource(strings = ["consume(ans§);", "consume(ans§", "consume(value = ans§);", "Int result = ans§;"]) + fun `compiler suggests compatible names in complete and incomplete source`(statement: String) { + val marked = + """ + module App { + void consume(Int value) {} + void run() { + Int answer = 42; + String answerText = "wrong type"; + $statement + } + } + """.trimIndent() + val source = marked.replace("§", "") + val before = marked.substringBefore('§') + val position = Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + XdkAdapter().use { adapter -> + adapter.compile(URI, source) + val cached = adapter.getCachedResult(URI) + val items = adapter.getInlineCompletionsAsync(URI, position, InlineCompletionContext(false)).get(30, SECONDS) + assertThat(items.map { it.newText }).contains("answer") + if (statement.startsWith("consume")) assertThat(items.map { it.newText }).doesNotContain("answerText") + assertThat(items.map { it.range }).containsOnly(Range(position.copy(column = position.column - 3), position)) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `member and literal suggestions use compiler facts and explicit requests can fill empty values`() { + listOf( + Triple("Int result = box.ans§;", true, listOf("answer")), + Triple("flag(Tr§);", true, listOf("True")), + Triple("flag(§);", false, listOf("False", "True")), + ).forEach { (statement, automatic, expected) -> + val marked = + """ + module App { + class Box { Int answer = 42; } + void flag(Boolean value) {} + void run(Box box) { + $statement + } + } + """.trimIndent() + val before = marked.substringBefore('§') + val at = Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + val edits = adapter.getInlineCompletionsAsync(URI, at, InlineCompletionContext(automatic)).get(30, SECONDS) + assertThat(edits.map { it.newText }).describedAs(statement).containsExactlyInAnyOrderElementsOf(expected) + } + } + } + + @Test + fun `unopened and library documents never trigger inline analysis`() { + XdkAdapter().use { adapter -> + listOf(URI, "ecstasy-library://ecstasy.xtclang.org/revision/String.x").forEach { uri -> + assertThat(adapter.getInlineCompletionsAsync(uri, Position(0, 0), InlineCompletionContext(false)).get(5, SECONDS)) + .isEmpty() + assertThat(adapter.getCachedResult(uri)).isNull() + } + } + } + + @Test + fun `automatic ambiguity disappears as the user continues typing`() { + val marked = + """ + module App { + void run() { + Int answer = 1; + Int another = 2; + Int result = an§; + } + } + """.trimIndent() + val before = marked.substringBefore('§') + val at = Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getInlineCompletionsAsync(URI, at, InlineCompletionContext(true)).get(30, SECONDS)).isEmpty() + assertThat(adapter.getInlineCompletionsAsync(URI, at, InlineCompletionContext(false)).get(30, SECONDS).map { it.newText }) + .contains("answer", "another") + adapter.compile(URI, marked.replace("an§", "ans")) + val next = at.copy(column = at.column + 1) + assertThat(adapter.getInlineCompletionsAsync(URI, next, InlineCompletionContext(true)).get(30, SECONDS).map { it.newText }) + .containsExactly("answer") + } + } + + @Test + fun `projection excludes extra edits snippets middle tokens and invalid positions`() { + val at = Position(0, 2) + val range = Range(Position(0, 0), at) + val item = CompletionItem("answer", CompletionItem.CompletionKind.VARIABLE, "Int", "answer", TextEdit(range, "answer")) + val automatic = InlineCompletionContext(true) + + fun project( + text: String, + items: List = listOf(item), + ) = XdkInlineCompletions.project(text, at, automatic, items) + assertThat(project("an;")).containsExactly(TextEdit(range, "answer")) + assertThat(project("another")).isEmpty() + assertThat(project("an;", listOf(item.copy(snippet = "answer")))).isEmpty() + assertThat(project("an;", listOf(item.copy(additionalTextEdits = listOf(TextEdit(range, "import")))))).isEmpty() + assertThat(project("an;", listOf(item, item))).hasSize(1) + listOf(Position(-1, 0), Position(0, -1), Position(0, 4), Position(1, 0)).forEach { + assertThat(XdkInlineCompletions.eligible("an;", it, automatic)).isFalse() + } + } + + @Test + fun `popup selection must be extended with exactly its replacement range`() { + val at = Position(0, 2) + val range = Range(Position(0, 0), at) + val edit = TextEdit(range, "answer") + val item = CompletionItem("answer", CompletionItem.CompletionKind.VARIABLE, "Int", "answer", edit) + + fun selected(selection: TextEdit) = XdkInlineCompletions.project("an;", at, InlineCompletionContext(false, selection), listOf(item)) + assertThat(selected(TextEdit(range, "ans"))).containsExactly(edit) + assertThat(selected(edit)).isEmpty() + assertThat(selected(TextEdit(range, "another"))).isEmpty() + assertThat(selected(TextEdit(Range(at, at), "ans"))).isEmpty() + } + + companion object { + private const val URI = "file:///App.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL63SharedActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL63SharedActionsTest.kt new file mode 100644 index 00000000000..9bf1e2e1065 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL63SharedActionsTest.kt @@ -0,0 +1,95 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +/** Exact same edits and refusals as both editor drivers, before any native UI run. */ +class XdkL63SharedActionsTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest(name = "X{0} shared refactoring") + @ValueSource(ints = [221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242]) + fun `shared action produces exact source or a documented refusal`(number: Int) { + CompilerTestSupport.configure() + val scenarios = Path.of(System.getProperty("xtc.composite.root"), "lang/test-fixtures/compiler-playbook/scenarios.json") + val data = + JsonParser + .parseString(scenarios.toFile().readText()) + .asJsonObject + .getAsJsonObject("cases") + .getAsJsonObject("X$number") + .getAsJsonObject("values") + + fun write( + name: String, + text: String, + ) = directory.resolve(name).toFile().canonicalFile.apply { + parentFile.mkdirs() + writeText(text) + } + val text = data["source"].asString + val file = write(data["file"].asString, text) + data.getAsJsonArray("files")?.forEach { row -> + val extra = row.asJsonObject + write(extra["file"].asString, extra["source"].asString) + } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + data.getAsJsonArray("sourceModules")?.let { modules -> + adapter.replaceSourceModules( + modules.map { row -> + val module = row.asJsonObject + XdkSourceModule( + module["name"].asString, + directory + .resolve(module["uri"].asString) + .toFile() + .toURI() + .toString(), + module + .getAsJsonArray("dependencies") + ?.map { it.asString } + ?.toSet() + .orEmpty(), + ) + }, + ) + } + val diagnostics = adapter.compile(uri, text).diagnostics + if (data["initiallyValid"]?.asBoolean == false) { + assertThat(diagnostics).isNotEmpty() + } else { + assertThat(diagnostics).isEmpty() + } + val start = data["selectionOffset"]?.asInt ?: text.indexOf(data["selected"].asString) + val end = start + data["selected"].asString.length + val range = Range(XdkRename.position(text, start), XdkRename.position(text, end)) + val actions = adapter.getCodeActions(uri, range, diagnostics).filter { it.title == data["title"].asString } + if (data["refused"]?.asBoolean == true) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).hasSize(1) + val edit = requireNotNull(actions.single().edit) + assertThat(edit.versioned).isTrue() + + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + val changed = + edit.changes.getValue(uri).sortedByDescending { offset(it.range.start) }.fold(text) { current, change -> + current.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + } + assertThat(changed).isEqualTo(data["expected"].asString) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + assertThat(file.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL65ClosureTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL65ClosureTest.kt new file mode 100644 index 00000000000..649004faa89 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkL65ClosureTest.kt @@ -0,0 +1,106 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.selectLibraryDeclaration +import org.xvm.lsp.treesitter.SemanticTokenLegend +import java.nio.file.Path + +/** Backend assertions consume exactly the sources and locations used by both native drivers. */ +class XdkL65ClosureTest { + @Test + fun `shared composed implementation variants navigate only to written bodies`() { + val data = scenario("X153") + val uri = "file:///${data["file"].asString}" + XdkAdapter().use { adapter -> + data.getAsJsonArray("variants").forEach { row -> + val variant = row.asJsonObject + val source = variant["source"].asString + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val at = position(source, variant["anchor"].asString, variant["offset"].asInt) + val targets = adapter.findImplementation(uri, at.line, at.column) + assertThat(targets.map { Position(it.startLine, it.startColumn) }) + .describedAs(source) + .containsExactlyInAnyOrderElementsOf(variant.getAsJsonArray("targets").map { position(source, it.asString) }) + assertThat(targets).allMatch { it.uri == uri } + } + } + } + + @Test + fun `shared destructuring and indexed mutations distinguish reads from writes`() { + val data = scenario("X154") + val uri = "file:///${data["file"].asString}" + val source = data["source"].asString + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + data.getAsJsonArray("accesses").forEach { row -> + val access = row.asJsonObject + val at = position(source, access["anchor"].asString, access["offset"].asInt) + val highlights = adapter.getDocumentHighlights(uri, at.line, at.column) + val highlight = highlights.single { it.range.start == at } + assertThat(highlight.kind).describedAs(access.toString()).isEqualTo( + if (access["write"].asBoolean) { + DocumentHighlight.HighlightKind.WRITE + } else { + DocumentHighlight.HighlightKind.READ + }, + ) + } + val tokens = + requireNotNull(adapter.getSemanticTokens(uri)) + .data + .chunked(5) + .fold(emptyList>()) { result, tuple -> + val previous = result.lastOrNull() + val line = (previous?.get(0) ?: 0) + tuple[0] + val column = (if (tuple[0] == 0) previous?.get(1) ?: 0 else 0) + tuple[1] + result + listOf(listOf(line, column, tuple[2], tuple[3], tuple[4])) + } + data.getAsJsonArray("accesses").forEach { row -> + val access = row.asJsonObject + val at = position(source, access["anchor"].asString, access["offset"].asInt) + val token = tokens.single { it[0] == at.line && it[1] == at.column } + assertThat(token[4] and SemanticTokenLegend.modifierBitmask("modification") != 0) + .describedAs(access.toString()) + .isEqualTo(access["write"].asBoolean) + } + } + } + + @Test + fun `binary overload matching requires unique debug source evidence`() { + val declarations = listOf(3..7, 10..14) + assertThat(selectLibraryDeclaration(declarations, 4) { it }).isEqualTo(3..7) + assertThat(selectLibraryDeclaration(declarations, 14) { it }).isEqualTo(10..14) + assertThat(selectLibraryDeclaration(declarations, null) { it }).isNull() + assertThat(selectLibraryDeclaration(declarations, 8) { it }).isNull() + assertThat(selectLibraryDeclaration(listOf(3..7, 7..10), 7) { it }).isNull() + assertThat(selectLibraryDeclaration(listOf(3..7), null) { it }).isEqualTo(3..7) + } + + private fun scenario(id: String): JsonObject { + val file = + Path + .of(System.getProperty("xtc.composite.root")) + .resolve("lang/test-fixtures/compiler-playbook/scenarios.json") + return JsonParser + .parseString(file.toFile().readText()) + .asJsonObject + .getAsJsonObject("cases") + .getAsJsonObject(id) + .getAsJsonObject("values") + } + + private fun position( + source: String, + anchor: String, + offset: Int = 0, + ): Position { + val before = source.substring(0, source.indexOf(anchor) + offset) + return Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt new file mode 100644 index 00000000000..4a913f6cafb --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt @@ -0,0 +1,129 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkLambdaRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["(Int input) -> input", "input -> input"]) + fun `typed and inferred parameters rename from declaration or use`(lambda: String) { + val text = "module App { Int run() { function Int(Int) f = $lambda; return f(1); } }" + workspace(text) { adapter, uri -> + Regex("\\binput\\b").findAll(text).forEach { match -> + assertThat(adapter.prepareRename(uri, 0, match.range.first)?.placeholder) + .isEqualTo("input") + val edit = requireNotNull(adapter.rename(uri, 0, match.range.first, "value")) + assertThat(apply(text, edit, uri)).isEqualTo(text.replace("input", "value")) + } + } + } + + @Test + fun `nested captures retain the parameter while sibling bindings stay unchanged`() { + val text = + "module App { Int run() { function Int(Int) f = (Int input) -> { " + + "function Int() nested = () -> input; return nested(); }; " + + "function Int(Int) g = (Int input) -> input; return f(1) + g(2); } }" + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("input"), "value")) + assertThat(apply(text, edit, uri)) + .isEqualTo( + text + .replace("f = (Int input)", "f = (Int value)") + .replace("() -> input", "() -> value"), + ) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "function Int(Int) saved() = (Int input) -> input;", + "Int consume(function Int(Int) f) = f(1); Int run() = consume((Int input) -> input);", + ], + ) + fun `returned and passed lambdas keep local parameter names`(body: String) { + val text = "module App { $body }" + workspace(text) { adapter, uri -> + assertThat( + apply( + text, + requireNotNull(adapter.rename(uri, 0, text.indexOf("input"), "value")), + uri, + ), + ).isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `lambda rename rejects capture even if the resulting code still compiles`() { + val text = + "module App { Int run() { Int value = 7; function Int(Int) f = (Int input) -> input + value; return f(1); } }" + workspace(text) { adapter, uri -> + listOf("value", "return", "bad name").forEach { name -> + assertThat(adapter.rename(uri, 0, text.indexOf("input"), name)).isNull() + } + assertThat(adapter.getCachedResult(uri)?.diagnostics).isEmpty() + } + } + + @Test + fun `standalone compilation needs no generated callable identity for a lambda`() { + val text = "module App { function Int(Int) saved() = input -> input; }" + XdkAdapter().use { adapter -> + val uri = + directory + .resolve("App.x") + .toFile() + .toURI() + .toString() + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + assertThat( + apply( + text, + requireNotNull(adapter.rename(uri, 0, text.indexOf("input"), "value")), + uri, + ), + ).isEqualTo(text.replace("input", "value")) + } + } + + private fun workspace( + text: String, + check: (XdkAdapter, String) -> Unit, + ) { + directory = directory.toRealPath() + val file = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = file.toURI().toString() + assertThat(adapter.compile(uri, text).diagnostics).describedAs(text).isEmpty() + check(adapter, uri) + assertThat(file.readText()).isEqualTo(text) + } + } + + private fun apply( + text: String, + edit: WorkspaceEdit, + uri: String, + ): String = + edit.changes + .getValue(uri) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt new file mode 100644 index 00000000000..5f9856ba78d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt @@ -0,0 +1,41 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Timeout +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkLexical +import org.xvm.lsp.treesitter.SemanticTokenLegend + +class XdkLexicalTest { + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + fun `documentation comments retain their modifier on every line only`(newline: String) { + val lines = + listOf("/** A document.", " * Another line. */", "/* Ordinary block. */", "// Ordinary line.", "\"/** Not a comment. */\"") + val tokens = XdkLexical.tokens(lines.joinToString(newline)) + assertThat(tokens).hasSize(2) + assertThat(tokens.map { SemanticTokenLegend.tokenTypes[it[3]] }) + .containsExactly("comment", "comment") + assertThat(tokens.map { it[4] }) + .containsExactly( + SemanticTokenLegend.modifierBitmask("documentation"), + SemanticTokenLegend.modifierBitmask("documentation"), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + @Timeout(10) + fun `large lexical projections preserve UTF-16 spans without rescanning per token`(newline: String) { + val lines = (0 until 2000).map { "String text$it = \"😀\"; /** line $it */" } + val source = lines.joinToString(newline, postfix = newline) + assertThat(XdkLexical.mayUseResources(source)).isFalse() + val tokens = XdkLexical.tokens(source) + assertThat(tokens).hasSize(lines.size) + lines.forEachIndexed { line, text -> + assertThat(tokens[line].take(3)) + .containsExactly(line, text.indexOf("/**"), text.length - text.indexOf("/**")) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt new file mode 100644 index 00000000000..cdc627e12f1 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt @@ -0,0 +1,101 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkLibraries +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.PosixFilePermission + +class XdkLibrariesTest { + @TempDir lateinit var directory: Path + + @Test + fun `the production bundle resolves the complete distribution library set`() { + val names = + listOf( + "aggregate", + "cli", + "collections", + "convert", + "crypto", + "ecstasy", + "json", + "jsondb", + "metrics", + "net", + "oodb", + "runner", + "runner_client", + "sec", + "web", + "webauth", + "webcli", + "xenia", + "xml", + "xunit", + "xunit_db", + "xunit_engine", + "mack", + "_native", + ).map { "$it.xtclang.org" } + assertThat(XdkLibraries.moduleNames).containsExactlyInAnyOrderElementsOf(names) + val imports = + names + .filterNot { it.startsWith("mack.") || it.startsWith("_native.") } + .mapIndexed { index, name -> "package lib$index import $name;" } + .joinToString(" ") + XdkAdapter().use { adapter -> + val result = adapter.compile("untitled:Libraries.x", "module Libraries { $imports }") + assertThat(result.diagnostics).isEmpty() + assertThat(result.success).isTrue() + } + } + + @Test + fun `bundled library source is navigable read only and cannot be renamed or replaced`() { + val uri = directory.resolve("App.x").toUri().toString() + val source = + "module App { package xml import xml.xtclang.org; void accept(xml.Document doc) {} }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val column = source.indexOf("Document") + assertThat(adapter.prepareRename(uri, 0, column)).isNull() + val target = requireNotNull(adapter.findDefinition(uri, 0, column)) + val path = Path.of(URI(target.uri)) + val line = Files.readAllLines(path)[target.startLine] + assertThat(line.substring(target.startColumn, target.endColumn)).isEqualTo("Document") + assertThat(Files.getPosixFilePermissions(path)) + .doesNotContain( + PosixFilePermission.OWNER_WRITE, + PosixFilePermission.GROUP_WRITE, + PosixFilePermission.OTHERS_WRITE, + ) + assertThat(adapter.compile(target.uri, Files.readString(path)).diagnostics).isEmpty() + assertThat(adapter.rename(target.uri, target.startLine, target.startColumn, "Renamed")) + .isNull() + assertThat( + adapter.formatDocument( + target.uri, + Files.readString(path), + FormattingOptions(4, true), + ), + ).isEmpty() + } + assertThatThrownBy { XdkSourceModule("xml.xtclang.org", uri) } + .isInstanceOf(IllegalArgumentException::class.java) + val binary = + checkNotNull(javaClass.getResourceAsStream("/org/xvm/lsp/xdk/xml.xtc")).use { + XdkDependency.fromBinary(it.readBytes()) + } + assertThatThrownBy { XdkDependencies(listOf(binary)) } + .isInstanceOf(IllegalArgumentException::class.java) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt new file mode 100644 index 00000000000..fa3ffebc8c9 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt @@ -0,0 +1,62 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path + +class XdkLibraryDocumentsTest { + @Test + fun `matching bundled sources have revision owned content and monikers in file and virtual views`() { + XdkAdapter().use { adapter -> + val uri = "file:///App.x" + val text = "module App { package xml import xml.xtclang.org; void accept(xml.Document doc) {} }" + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.indexOf("Document") + val imported = adapter.findMonikers(uri, 0, at).single() + val location = requireNotNull(adapter.findDefinition(uri, 0, at)) + val document = requireNotNull(adapter.readOnlyDocument(location.uri)) + assertThat(URI(document.uri).scheme).isEqualTo("ecstasy-library") + assertThat(URI(document.uri).authority).isEqualTo("xml.xtclang.org") + assertThat(document.text).isEqualTo(Files.readString(Path.of(URI(location.uri)))) + assertThat(adapter.readOnlyDocument(document.uri)).isEqualTo(document) + listOf(location.uri, document.uri).forEach { source -> + assertThat(adapter.findMonikers(source, location.startLine, location.startColumn)) + .containsExactly(imported.copy(kind = SymbolMoniker.Kind.EXPORT)) + assertThat(adapter.compile(source, "module Forged {} ").diagnostics).isEmpty() + assertThat(adapter.prepareRename(source, location.startLine, location.startColumn)).isNull() + assertThat(adapter.formatDocument(source, document.text, FormattingOptions(4, true))).isEmpty() + assertThat(adapter.readOnlyDocument(source)?.text).isEqualTo(document.text) + } + adapter.replaceDependencies(emptyList()) + assertThat(adapter.readOnlyDocument(document.uri)).isEqualTo(document) + assertThat(adapter.readOnlyDocument(document.uri.replace("/xml/", "/../"))).isNull() + assertThat(adapter.readOnlyDocument("file:///etc/passwd")).isNull() + assertThat(adapter.findMonikers(document.uri, 0, 0)).isEmpty() + adapter.close() + assertThat(adapter.readOnlyDocument(document.uri)).isNull() + } + } + + @Test + fun `library view resolves declarations never previously queried by the consumer`() { + XdkAdapter().use { adapter -> + val text = "module App { String text = \"value\"; }" + assertThat(adapter.compile("file:///App.x", text).diagnostics).isEmpty() + val location = requireNotNull(adapter.findDefinition("file:///App.x", 0, text.indexOf("String"))) + val document = requireNotNull(adapter.readOnlyDocument(location.uri)) + val lines = document.text.lines() + val line = lines.indexOfFirst { it.contains("Int size.get()") } + assertThat(line).isNotNegative() + val column = lines[line].indexOf("size") + val moniker = adapter.findMonikers(document.uri, line, column).single() + assertThat(moniker.kind).isEqualTo(SymbolMoniker.Kind.EXPORT) + val consumer = "module App { Int length(String text) = text.size; }" + assertThat(adapter.compile("file:///App.x", consumer).diagnostics).isEmpty() + assertThat(adapter.findMonikers("file:///App.x", 0, consumer.indexOf("size"))) + .containsExactly(moniker.copy(kind = SymbolMoniker.Kind.IMPORT)) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt new file mode 100644 index 00000000000..b839b88f3af --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt @@ -0,0 +1,106 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename + +class XdkLinkedEditingTest { + @ParameterizedTest + @ValueSource(strings = ["(Int input) -> input", "input -> input"]) + fun `lambda parameters link only their own declaration and uses`(lambda: String) { + val text = + """ + module Linked { + Int run() { + function Int(Int) first = $lambda; + function Int(Int) second = $lambda; + return first(1) + second(2); + } + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + listOf(2, 3).forEach { line -> + val ranges = requireNotNull(adapter.getLinkedEditingRanges(URI, line, text.lines()[line].indexOf("input"))).ranges + assertThat(ranges).hasSize(2) + assertThat(ranges.map { it.start.line }).containsOnly(line) + } + adapter.compile(URI, text.replace("first(1)", "missing(1)")) + assertThat(adapter.getLinkedEditingRanges(URI, 2, text.lines()[2].indexOf("input"))).isNull() + adapter.closeDocument(URI) + assertThat(adapter.getLinkedEditingRanges(URI, 3, text.lines()[3].indexOf("input"))).isNull() + } + } + + @Test + fun `nested lambda capture preserves lexical identity and UTF16 positions`() { + val text = + """ + module Linked { + Int run() { + /* 😀 */ function Int(Int) first = (Int input) -> { + function Int() nested = () -> input; + return nested(); + }; + function Int(Int) second = (Int input) -> input; + return first(1) + second(2); + } + } + """.trimIndent().replace("\n", "\r\n") + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + val at = XdkRename.position(text, text.indexOf("input")) + val ranges = requireNotNull(adapter.getLinkedEditingRanges(URI, at.line, at.column)).ranges + assertThat(ranges.map { it.start }).containsExactly(at, XdkRename.position(text, text.indexOf("input;"))) + } + } + + @Test + fun `callable parameters and member names require graph rename rather than linked editing`() { + val text = + """ + module Linked { + private Int pick(Int privateInput) = privateInput; + Int exposed(Int publicInput) = publicInput; + Int run() = pick(privateInput = 1) + exposed(publicInput = 2); + class Box(Int value) { Int read() = value; } + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + listOf("privateInput", "publicInput", "value", "pick", "exposed").forEach { name -> + val at = XdkRename.position(text, text.indexOf(name)) + assertThat(adapter.getLinkedEditingRanges(URI, at.line, at.column)).describedAs(name).isNull() + } + } + } + + @Test + fun `explicit same-spelling aliases retain their lexical ownership`() { + val text = + """ + module Linked { + package types { class Box {} } + Int first() { import types.Box as Box; Box value = new Box(); return 1; } + Int second() { import types.Box as Box; Box value = new Box(); return 2; } + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + listOf(2, 3).forEach { line -> + val at = text.lines()[line].indexOf("as Box") + 3 + val ranges = requireNotNull(adapter.getLinkedEditingRanges(URI, line, at)).ranges + assertThat(ranges).hasSize(3) + assertThat(ranges.map { it.start.line }).containsOnly(line) + assertThat(ranges.first().start.column).isEqualTo(at) + } + } + } + + private companion object { + const val URI = "untitled:Linked.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt new file mode 100644 index 00000000000..006e042e367 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt @@ -0,0 +1,136 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkLiteralCompletionTest { + @ParameterizedTest + @ValueSource(strings = ["Int", "String", "Boolean", "String?"]) + fun `literal proposals fit and compile in positional and named slots`(type: String) { + val expected = + when (type) { + "Int" -> listOf("0") + "String" -> listOf("\"\"") + "Boolean" -> listOf("False", "True") + else -> listOf("Null", "\"\"") + } + listOf("", "value = ").forEach { label -> + val source = + "module Editing { void take($type value) {} void run() { take($label§); } }" + verify(source, expected) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void run() { take(1 + §); }", + "void run() { take(§, 2); }", + "void run() { function void(Int) fn = value -> {}; fn(§); }", + "class Box(Int value) {} void run() { Box box = new Box(§); }", + ], + ) + fun `literal fitting preserves compound later function and constructor arguments`(body: String) { + verify("module Editing { void take(Int value, Int later = 2) {} $body }", listOf("0")) + } + + @Test + fun `literals come from all fitting overloads without selecting one`() { + verify( + "module Editing { void take(Int value) {} void take(String value) {} void run() { take(§); } }", + listOf("0", "\"\""), + ) + } + + @Test + fun `additional scalar and empty collection literals compile using the expected type`() { + listOf( + "Char" to listOf("' '"), + "Dec" to listOf("0", "0.0"), + "ecstasy.numbers.Float64" to listOf("0", "0.0"), + "Byte[]" to listOf("#00", "[]"), + "String[]" to listOf("[]"), + "Map" to listOf("[]", "Map:[]"), + "Tuple" to listOf("Tuple:()"), + ).forEach { (type, expected) -> + listOf("", "value = ").forEach { label -> + verify("module Editing { void take($type value) {} void run() { take($label§); } }", expected) + } + } + } + + @Test + fun `literal insertion must validate the entire operator expression`() { + verify( + "module Editing { void take(Boolean value) {} void run() { take(1 == §); } }", + listOf("0"), + ) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void run() { take(§, 1); }", + "void run() { missing(§); }", + "void run(String text) { take(text.§, \"\"); }", + ], + ) + fun `invalid other arguments and qualified reads cannot acquire literal proposals`(body: String) { + verify("module Editing { void take(String value, String later) {} $body }", emptyList()) + } + + @Test + fun `a literal prefix replaces the whole token in UTF16 source`() { + val marked = + "module Editing {\r\n void take(Boolean value) {} void run() { /* 😀 */ take(Tr§ue); }\r\n}" + val source = marked.replace("§", "") + val line = source.lines()[1] + val start = line.indexOf("True") + XdkAdapter().use { adapter -> + adapter.compile(URI, source) + val literal = + adapter.getCompletions(URI, 1, start + 2).single { + it.kind == CompletionItem.CompletionKind.VALUE + } + assertThat(literal.label).isEqualTo("True") + assertThat(literal.textEdit) + .isEqualTo(TextEdit(Range(Position(1, start), Position(1, start + 4)), "True")) + } + } + + private fun verify( + marked: String, + expected: List, + ) { + val at = marked.indexOf('§') + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val baseline = adapter.compile(URI, source) + val literals = + adapter.getCompletions(URI, 0, at).filter { + it.kind == CompletionItem.CompletionKind.VALUE + } + assertThat(literals.map { it.label }) + .describedAs("%s: %s", marked, baseline.diagnostics) + .containsExactlyInAnyOrderElementsOf(expected) + assertThat(adapter.getCachedResult(URI)).isEqualTo(baseline) + literals.forEach { item -> + assertThat(item.textEdit) + .isEqualTo(TextEdit(Range(Position(0, at), Position(0, at)), item.label)) + assertThat( + adapter.compile(URI, source.replaceRange(at, at, item.label)).diagnostics, + ).isEmpty() + adapter.compile(URI, source) + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt new file mode 100644 index 00000000000..30f58462de6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt @@ -0,0 +1,185 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkLiteralRecoveryTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "(1, value.si|)", + "Tuple:(1, value.si|)", + "[value.si|]", + "List:[value.si|]", + "Set:[value.si|]", + "[\"key\" = value.si|]", + "Map:[\"key\" = value.si|]", + "[(1, value.si|)]", + ], + ) + fun `missing literal closers preserve member completion and following declarations`(expression: String) { + val prefix = + "module Editing { Object run(String value) { return " + expression.substringBefore('|') + val suffix = "; } Int later() = 42; }" + XdkAdapter().use { adapter -> + val complete = prefix.dropLast(2) + "size" + expression.substringAfter('|') + suffix + assertThat(adapter.compile(URI, complete).diagnostics).describedAs(complete).isEmpty() + val cached = adapter.compile(URI, prefix + suffix) + assertThat(cached.diagnostics).isNotEmpty() + val item = adapter.getCompletions(URI, 0, prefix.length).single { it.label == "size" } + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "size", + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "Int run(String value) = value.si|;", + "Int size = \"x\".si|;", + "void run(Int size = Int64.Ma|) {}", + "class Holder(Int size = \"x\".si|) {}", + ], + ) + fun `declaration values retain their context with missing terminators`(declaration: String) { + val prefix = "module Editing { " + declaration.substringBefore('|') + val closing = declaration.substringAfter('|') + XdkAdapter().use { adapter -> + assertThat( + adapter + .compile( + URI, + prefix.dropLast(2) + + (if (declaration.contains("Int64")) "MaxValue" else "size") + + closing + + " }", + ).diagnostics, + ).isEmpty() + val cached = adapter.compile(URI, prefix + closing.drop(1) + " }") + val source = Source(prefix + closing.drop(1) + " }", URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(analysis.cursorBindings()) + .describedAs("%s: %s", declaration, errors.errors.map { it.code }) + .isNotEmpty() + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + // The same default cursor is reachable through both parameter and property syntax. + // Publication must preserve its identity once, with its actual validation binding. + val site = analysis.sites().single() + assertThat(analysis.cursorBindings()).containsKey(site) + assertThat(site.source.toRawString()).isEqualTo(source.toRawString()) + val owner = + generateSequence(site.parent) { it.parent } + .filterIsInstance() + .firstOrNull() + if (owner != null) assertThat((owner.component as MethodStructure).hasOps()).isFalse() + assertThat( + adapter.getCompletions(URI, 0, prefix.length).map { + it.label + }, + ).describedAs(declaration) + .contains(if (declaration.contains("Int64")) "MaxValue" else "size") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @ParameterizedTest + @ValueSource( + strings = + ["(1, pair(1, va", "[pair(1, va", "[1 = pair(1, va", "Tuple:(1, pair(1, va"], + ) + fun `literal nesting retains the inner argument fitter and signature`(expression: String) { + val prefix = + "module Editing { Int pair(Int first, Int second) = first; " + + "Object run(Int value, String valueText) { return $expression" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, "$prefix; } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("value") + assertThat( + adapter + .getSignatureHelp(URI, 0, prefix.length)!! + .signatures + .single() + .activeParameter, + ).isEqualTo(1) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `unrelated missing syntax is not repaired by a literal cursor`() { + for (expression in listOf("[1 + , value.si", "(, value.si", "[1 = , value.si")) { + val prefix = "module Editing { Object run(String value) { return $expression" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(expression) + .isEmpty() + } + } + } + + @Test + fun `missing literal value operand retains lexical completion and ordinary errors`() { + val prefix = "module Editing { Object run(String value) { return [1 = value.size +" + XdkAdapter().use { adapter -> + val baseline = adapter.compile(URI, "$prefix; } }") + assertThat(baseline.diagnostics).isNotEmpty() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("value") + assertThat(adapter.getCachedResult(URI)).isEqualTo(baseline) + } + } + + @Test + fun `declaration recovery respects cancellation and a first error budget`() { + CompilerTestSupport.configure() + val prefix = "module Editing { void run(Int size = Int64.Ma" + val text = "$prefix {} }" + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + for ( + errors in + listOf( + ErrorList(ErrorList.FIRST_ERROR), + ErrorListener.cancellable(ErrorList()) { true }, + ) + ) { + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), cursor, null, errors) + assertThat(analysis.pool()).isNull() + assertThat(analysis.cursorBindings()).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt new file mode 100644 index 00000000000..77021524bb2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt @@ -0,0 +1,177 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicInteger + +class XdkLiveWorkspaceTest { + @TempDir lateinit var directory: Path + + @Test + fun `unsaved imports and new module buffers update the discovered graph and close restores disk`() { + val library = source("Library", "module Library { static String answer() = \"ok\"; }") + val original = "module App {}" + val app = source("App", original) + val changed = "module App { package lib import Library; String run() = lib.answer(); }" + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(app, original).success).isTrue() + assertThat(adapter.compile(app, changed).diagnostics).isEmpty() + assertThat(adapter.findDefinition(app, 0, changed.indexOf("answer"))!!.uri) + .isEqualTo(library) + val fresh = + directory + .resolve("Fresh.x") + .toFile() + .canonicalFile + .toURI() + .toString() + assertThat(adapter.compile(fresh, "module Fresh { class OnlyInBuffer {} }").success) + .isTrue() + assertThat(adapter.findWorkspaceSymbols("OnlyInBuffer")).hasSize(1) + adapter.closeDocument(fresh) + assertThat(adapter.findWorkspaceSymbols("OnlyInBuffer")).isEmpty() + adapter.closeDocument(app) + assertThat(adapter.compile(app, original).success).isTrue() + assertThat(directory.resolve("App.x").toFile().readText()).isEqualTo(original) + } + } + + @Test + fun `folder changes update imports and an explicit graph keeps authority`() { + val first = directory.resolve("first").toFile().also { it.mkdirs() } + val second = directory.resolve("second").toFile().also { it.mkdirs() } + val app = + first.resolve("App.x").also { + it.writeText( + "module App { package lib import Library; String run() = lib.answer(); }", + ) + } + second.resolve("Library.x").writeText("module Library { static String answer() = \"ok\"; }") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(first.path)) + assertThat(adapter.compile(app.toURI().toString(), app.readText()).success).isFalse() + adapter.changeWorkspaceFolders(listOf(second.toURI().toString()), emptyList()) + assertThat(adapter.compile(app.toURI().toString(), app.readText()).success).isTrue() + adapter.changeWorkspaceFolders(emptyList(), listOf(second.toURI().toString())) + assertThat(adapter.compile(app.toURI().toString(), app.readText()).success).isFalse() + adapter.replaceSourceModules(emptyList()) + adapter.changeWorkspaceFolders(listOf(second.toURI().toString()), emptyList()) + assertThat(adapter.findWorkspaceSymbols("answer")).isEmpty() + adapter.discoverSourceModules() + assertThat(adapter.findWorkspaceSymbols("answer")).hasSize(1) + } + } + + @Test + fun `detached graph results are reused and closed file edits invalidate the cache`() { + CompilerTestSupport.configure() + val text = "module Healthy { class Base {} class Child extends Base {} }" + val root = source("Healthy", text) + source("Neighbor", "module Neighbor {}") + val attempts = AtomicInteger() + XdkAdapter( + { source, errors -> + EmbeddingSupport.instance().compileModule(source, null, errors) + }, + { sources, errors -> + attempts.incrementAndGet() + EmbeddingSupport.instance().compileModule(sources, null, errors) + }, + ).use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val item = adapter.prepareTypeHierarchy(root, 0, text.indexOf("Base")).single() + assertThat(attempts.get()).isEqualTo(2) + assertThat(adapter.getSubtypes(item).single().name).isEqualTo("Child") + assertThat(adapter.findWorkspaceSymbols("Child")).hasSize(1) + assertThat(adapter.findReferences(root, 0, text.indexOf("Base"), true)).hasSize(2) + assertThat(attempts.get()).isEqualTo(2) + source("Neighbor", "module Neighbor { Missing broken; }") + assertThat(adapter.getSubtypes(item)).isEmpty() + val current = adapter.prepareTypeHierarchy(root, 0, text.indexOf("Base")).single() + assertThat(adapter.getSubtypes(current).single().name).isEqualTo("Child") + assertThat(adapter.findReferences(root, 0, text.indexOf("Base"), true)).isEmpty() + assertThat(adapter.rename(root, 0, text.indexOf("Base"), "Renamed")).isNull() + assertThat(adapter.getCachedResult(root)).isNull() + } + } + + @Test + fun `invalid unsaved graphs clear facts report diagnostics and recover from another file`() { + val app = source("App", "module App {}") + val library = source("Library", "module Library { class Target {} }") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(app, "module App {}").success).isTrue() + assertThat(adapter.findWorkspaceSymbols("Target")).hasSize(1) + assertThat(adapter.compile(app, "module Library {}").diagnostics).anyMatch { + it.code == "SOURCE-GRAPH" + } + assertThat(adapter.findWorkspaceSymbols("Target")).isEmpty() + // The rejected edited header remains in the catalog. Correct a different file. + assertThat(adapter.compile(library, "module Renamed { class Target {} }").success) + .isTrue() + assertThat(adapter.findWorkspaceSymbols("Target")).hasSize(1) + assertThat( + adapter.compile(app, "module Library { package other import Renamed; }").success, + ).isTrue() + assertThat( + adapter + .compile(library, "module Renamed { package other import Library; }") + .diagnostics, + ).anyMatch { it.code == "SOURCE-GRAPH" } + adapter.closeDocument(library) + assertThat(adapter.compile(app, "module App {}").success).isTrue() + assertThat(adapter.findWorkspaceSymbols("Target")).hasSize(1) + } + } + + @Test + fun `a missing source dependency cannot use an older host binary during partial navigation`() { + CompilerTestSupport.configure() + val library = + EmbeddingSupport + .instance() + .compileModule( + Source("module Missing { class Base {} }"), + null, + ErrorList(), + ).toDependency() + val healthy = source("Healthy", "module Healthy { class Visible {} }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Missing; class Hidden extends lib.Base {} }", + ) + val missing = directory.resolve("Missing.x").toUri().toString() + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(library)) + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Healthy", healthy), + XdkSourceModule("Missing", missing), + XdkSourceModule("Consumer", consumer, setOf("Missing")), + ), + ) + assertThat(adapter.findWorkspaceSymbols("Visible")).hasSize(1) + assertThat(adapter.findWorkspaceSymbols("Hidden")).isEmpty() + } + } + + private fun source( + name: String, + text: String, + ): String = + directory.resolve("$name.x").toFile().let { + it.writeText(text) + it.canonicalFile.toURI().toString() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt new file mode 100644 index 00000000000..9fdd18ca551 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt @@ -0,0 +1,143 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkLocalExtractionTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["Int|42", "String|\"hello 😀\"", "Char|'x'"]) + fun `extract literal preserves exact source and existing bindings`(example: String) { + val (type, literal) = example.split('|') + val text = + "module Extract {\r\n $type read() {\r\n Int extractedValue = 1;\r\n" + + " assert extractedValue == 1;\r\n return $literal;\r\n }\r\n}" + query(text, literal) { adapter, uri, actions -> + val edit = requireNotNull(actions.single { it.title.endsWith("to local variable") }.edit) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys).containsExactly(uri) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat( + changed, + ).contains("val extractedValue1 = $literal;\r\n return extractedValue1;", "assert extractedValue == 1;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(directory.resolve("Extract.x").toFile().readText()).isEqualTo(text) + } + } + + @ParameterizedTest + @ValueSource(strings = ["read()", "1 + 2", "\"abc\".size"]) + fun `whole returned calls and compound expressions preserve evaluation and bindings`(expression: String) { + val text = "module Extract {\n Int read() {\n return $expression;\n }\n}" + query(text, expression) { adapter, uri, actions -> + val action = actions.single { it.title.endsWith("to local variable") } + assertThat(action.title).isEqualTo("Extract expression to local variable") + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat( + changed, + ).isEqualTo(text.replace("return $expression;", "Int extractedValue = $expression;\n return extractedValue;")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int|input + input|Int input", "Int8|input + 1|Int8 input", "Boolean|input && probe()|Boolean input"]) + fun `extraction preserves written expected type and references inside the moved expression`(example: String) { + val (type, expression, parameter) = example.split('|') + val text = "module Extract {\n Boolean probe() = True;\n $type read($parameter) {\n return $expression;\n }\n}" + query(text, expression) { adapter, uri, actions -> + val action = actions.single { it.title.endsWith("to local variable") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains("$type extractedValue = $expression;\n return extractedValue;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `expected function type and captured parameter survive relocation`() { + val expression = "() -> input" + val text = "module Extract {\n function Int() read(Int input) {\n return $expression;\n }\n}" + query(text, expression) { adapter, uri, actions -> + val action = actions.single { it.title.endsWith("to local variable") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains("function Int() extractedValue = () -> input;\n return extractedValue;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `conditional return and partial short circuit selections refuse extraction`() { + val conditional = "module Extract {\n conditional Int read(String input) {\n return input.indexOf('a');\n }\n}" + query(conditional, "input.indexOf('a')") { _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + val partial = "module Extract {\n Boolean read(Boolean first, Boolean second) {\n return first && second;\n }\n}" + query(partial, "first") { _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + + @Test + fun `broken known neighbor prevents refactoring publication`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + val text = "module Extract {\n Int read() {\n return 42;\n }\n}" + query(text, "42") { _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["4", "", "42;"]) + fun `partial empty and oversized selections have no extraction`(selected: String) { + val text = "module Extract {\n Int read() {\n return 42;\n }\n}" + query(text, selected) { _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + + @Test + fun `same line siblings are not reformatted by extraction`() { + val text = "module Extract { Int read() { return 42; } }" + query(text, "42") { _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + + private fun query( + text: String, + selected: String, + check: (XdkAdapter, String, List) -> Unit, + ) { + val uri = + directory + .resolve("Extract.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + val at = text.positionOf("return $selected", selected) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.compile(uri, text) + check(adapter, uri, adapter.getCodeActions(uri, Range(at, Position(at.line, at.column + selected.length)), emptyList())) + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { current, edit -> + fun offset(position: Position) = current.splitToSequence('\n').take(position.line).sumOf { it.length + 1 } + position.column + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt new file mode 100644 index 00000000000..311fad7201a --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt @@ -0,0 +1,219 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkLocalInitializerActionsTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["Int|input + probe()", "Int8|input + 1", "function Int()|() -> input", "Int[]|[input, probe()]"]) + fun `extract an entire initializer retains its contextual type and evaluation order`(example: String) { + val (type, expression) = example.split('|') + val parameter = if (type == "Int8") "Int8" else "Int" + val marked = + """ + module Extract { + Int probe() = 1; + $type read($parameter input) { + $type result = §$expression§; + return result; + } + } + """.trimIndent() + query(marked) { adapter, uri, text, actions -> + val action = actions.single { it.title.endsWith("to local variable") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).isEqualTo( + text.replace( + "$type result = $expression;", + "$type extractedValue = $expression;\n $type result = extractedValue;", + ), + ) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `extraction preserves initializer comments CRLF and fresh local naming`() { + val marked = + """ + module Extract { + Int read(Int extractedValue) { + Int result = §extractedValue /* 😀 */ + 1§; + return result; + } + } + """.trimIndent().replace("\n", "\r\n") + query(marked) { adapter, uri, text, actions -> + val action = actions.single { it.title.endsWith("to local variable") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains("Int extractedValue1 = extractedValue /* 😀 */ + 1;\r\n Int result = extractedValue1;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "val result = §input + 1§;", "Int result = §input§ + 1;", + "Int result = input + 1; Int other = §input + 2§;", "Int result /* declaration comment */ = §input + 1§;", + ], + ) + fun `extraction refuses inference partial selections same line siblings and declaration annotations`(body: String) { + query( + """ + module Extract { + Int read(Int input) { + $body + return result; + } + } + """.trimIndent(), + ) { _, _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + + @Test + fun `property initializer and conditional declaration are not local extraction sites`() { + listOf( + """ + module Extract { + Int value = §1 + 2§; + } + """.trimIndent(), + """ + module Extract { + Int read(String input) { + if (Int result := §input.indexOf('a')§) { + return result; + } + return -1; + } + } + """.trimIndent(), + ).forEach { marked -> + query(marked) { _, _, _, actions -> + assertThat(actions.filter { it.title.endsWith("to local variable") }).isEmpty() + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int|input + probe()", "Int8|input + 1", "function Int()|() -> input", "Int[]|[input, probe()]"]) + fun `inline into adjacent typed initializer keeps expected type and exactly one evaluation`(example: String) { + val (type, expression) = example.split('|') + val parameter = if (type == "Int8") "Int8" else "Int" + query( + """ + module Extract { + Int probe() = 1; + $type read($parameter input) { + $type value = $expression; + $type result = §value§; + return result; + } + } + """.trimIndent(), + ) { adapter, uri, text, actions -> + val action = actions.single { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE } + assertThat(action.title).isEqualTo("Inline local variable into initializer") + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).isEqualTo( + text.replace( + "$type value = $expression;\n $type result = value;", + "$type result = $expression;", + ), + ) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "val result = §value§;", "Int result = §value§ + value;", + "Int result = probe() + §value§;", "probe();\n Int result = §value§;", + "// keep explanation\n Int result = §value§;", + "Int8 result = §value§.toInt8();", "function Int() result = () -> §value§;", + ], + ) + fun `inline refuses changed contexts multiple uses delayed evaluation intervening statements and comments`(use: String) { + query( + """ + module Extract { + Int probe() = 1; + void read() { + Int value = probe(); + $use + } + } + """.trimIndent(), + ) { _, _, _, actions -> + assertThat(actions.filter { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE }).isEmpty() + } + } + + @Test + fun `different written destination type refuses inline even when conversion would compile`() { + query( + """ + module Extract { + Int read() { + Int8 value = 1; + Int result = §value§; + return result; + } + } + """.trimIndent(), + ) { _, _, _, actions -> + assertThat(actions.filter { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE }).isEmpty() + } + } + + private fun query( + marked: String, + check: (XdkAdapter, String, String, List) -> Unit, + ) { + CompilerTestSupport.configure() + val start = marked.indexOf('§') + val end = marked.lastIndexOf('§').let { if (it == start) it else it - 1 } + val text = marked.replace("§", "") + val file = + directory + .resolve("Extract.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val range = Range(XdkRename.position(text, start), XdkRename.position(text, end)) + check(adapter, uri, text, adapter.getCodeActions(uri, range, emptyList())) + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith( + compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }, + ).fold(text) { current, edit -> + fun offset(position: Position) = + requireNotNull( + XdkRename.offset(text, SemanticModel.Position(position.line, position.column)), + ) + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt new file mode 100644 index 00000000000..fc443fef41d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt @@ -0,0 +1,146 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkLocalInlineTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = [ + "Int|input + probe()|Int input", "Boolean|input && probe() == 1|Boolean input", + "Int8|input + 1|Int8 input", "function Int()|() -> input|Int input", "String|\"hello 😀\"|Int input", + ], + ) + fun `inline adjacent returned local preserves expected type bindings and single evaluation`(example: String) { + val (type, expression, parameter) = example.split('|') + val text = + "module Inline {\r\n Int probe() = 1;\r\n $type read($parameter) {\r\n" + + " $type value = $expression;\r\n return value;\r\n }\r\n}" + query(text) { adapter, uri, actions -> + val action = actions.single { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE } + assertThat(action.title).isEqualTo("Inline returned local variable") + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("$type value = $expression;\r\n return value;", "return $expression;")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(directory.resolve("Inline.x").toFile().readText()).isEqualTo(text) + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "Int value = probe();\n assert value > 0;\n return value;", + "Int value = probe();\n value = 3;\n return value;", + "Int value = probe();\n return value + value;", + "Int value = probe(); // retained comment\n return value;", + "Int value = probe(); return value;", + "Int8 value = 1;\n return value;", + "val value = probe();\n return value;", + "Int value = probe();\n probe();\n return value;", + ], + ) + fun `refuse repeated reads writes changed expected type intervening statements and comments`(body: String) { + val text = "module Inline {\n Int probe() = 1;\n Int read() {\n $body\n }\n}" + query(text) { _, _, actions -> + assertThat(actions.filter { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE }).isEmpty() + } + } + + @Test + fun `broken graph prevents inline publication`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + query("module Inline {\n Int read() {\n Int value = 1;\n return value;\n }\n}") { _, _, actions -> + assertThat(actions.filter { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE }).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["value + probe()", "probe() + value", "accept(value)"]) + fun `constant local can move across statements into a nested expression without changing type`(use: String) { + val text = + """ + module Inline { + Int probe() = 1; + Int accept(Int input) = input; + Int read() { + Int value = 1 + 2; + probe(); + return $use; + } + } + """.trimIndent() + query(text, select = "Int value") { adapter, uri, actions -> + val action = actions.single { it.title == "Inline constant local variable" } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).doesNotContain("Int value =").contains("return ${use.replace("value", "(1 + 2)")};") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "Int8 value = 1;", + "Int value = /* keep me */ 1;", + "Int value = 1; // keep me", + "Int value = probe();", + ], + ) + fun `wider inline refuses contextual widening lost comments and deferred effects`(declaration: String) { + val text = + """ + module Inline { + Int probe() = 1; + Int read() { + $declaration + probe(); + return value + 2; + } + } + """.trimIndent() + query(text, select = declaration) { _, _, actions -> + assertThat(actions.filter { it.kind == CodeAction.CodeActionKind.REFACTOR_INLINE }).isEmpty() + } + } + + private fun query( + text: String, + select: String = "return value", + check: (XdkAdapter, String, List) -> Unit, + ) { + val uri = + directory + .resolve("Inline.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + val at = text.positionOf(select, "value") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.compile(uri, text) + check(adapter, uri, adapter.getCodeActions(uri, Range(at, at), emptyList())) + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { current, edit -> + fun offset(position: Position) = current.splitToSequence('\n').take(position.line).sumOf { it.length + 1 } + position.column + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt new file mode 100644 index 00000000000..84fd9c4b537 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt @@ -0,0 +1,137 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkLocalRemovalTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["Int|1 + 2", "Int8|42", "Boolean|True", "String|\"hello 😀 // literal\"", "String?|Null"]) + fun `remove unused compiler constants without changing surrounding behavior`(example: String) { + val (type, expression) = example.split('|') + query( + """ + module Remove { + Int read(Int input) { + $type §unused = $expression; + return input + 1; + } + } + """.trimIndent(), + ) { adapter, uri, text, action -> + val changed = apply(text, requireNotNull(action).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace(" $type unused = $expression;\n", "")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `removal retains leading and trailing comments CRLF and unrelated same named locals`() { + query( + """ + module Remove { + Int read() { + // retain context + Int §unused = 42; // retain explanation + return 1; + } + Int other() { + Int unused = 2; + return unused; + } + } + """.trimIndent().replace("\n", "\r\n"), + ) { adapter, uri, text, action -> + val changed = apply(text, requireNotNull(action).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("Int unused = 42;", "")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "Int §unused = probe();", "Boolean §unused = probe() == 1 && False;", + "Int §unused = input;", "@Volatile Int §unused = 42;", + "Int §unused = 1 /* retain */ + 2;", "Int §unused = 42; unused = 3;", + "Int §unused = 42; assert unused == 42;", "val §unused = 42;", + ], + ) + fun `refuse runtime evaluation storage annotations internal comments writes reads and inferred locals`(declaration: String) { + query( + """ + module Remove { + Int probe() = 1; + Int read(Int input) { + $declaration + return input; + } + } + """.trimIndent(), + ) { _, _, _, action -> assertThat(action).isNull() } + } + + @Test + fun `broken known graph prevents removal even for an unused constant`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + query( + """ + module Remove { + void read() { + Int §unused = 42; + } + } + """.trimIndent(), + ) { _, _, _, action -> assertThat(action).isNull() } + } + + private fun query( + marked: String, + check: (XdkAdapter, String, String, WorkspaceEdit?) -> Unit, + ) { + CompilerTestSupport.configure() + val at = marked.indexOf('§') + val text = marked.replace("§", "") + val file = + directory + .resolve("Remove.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val position = XdkRename.position(text, at) + val action = + adapter + .getCodeActions(uri, Range(position, position), emptyList()) + .singleOrNull { it.title == "Remove unused local variable" } + check(adapter, uri, text, action?.edit) + assertThat(file.readText()).isEqualTo(text) + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith( + compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }, + ).fold(text) { current, edit -> + fun offset(position: Position) = + requireNotNull( + XdkRename.offset(text, SemanticModel.Position(position.line, position.column)), + ) + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt new file mode 100644 index 00000000000..a09fc3c5f5f --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt @@ -0,0 +1,132 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkManualCompositionRenameTest { + @TempDir lateinit var directory: Path + + @Test + fun `manual covariant self family renames without touching the unrelated mixin`() { + val text = manualCompositionFixture("mixinTests") + val uri = source("mixinTests", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("Root self();", "self") + val edit = requireNotNull(adapter.rename(uri, at.line, at.column, "copySelf")) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(edit.changes.getValue(uri)).hasSize(4) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).contains("Root copySelf();", "Base copySelf()", "Mix copySelf()", "Mix! self()") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val renamed = changed.positionOf("Root copySelf();", "copySelf") + val reverse = requireNotNull(adapter.rename(uri, renamed.line, renamed.column, "self")) + assertThat(apply(changed, reverse.changes.getValue(uri))).isEqualTo(text) + } + } + + @Test + fun `manual conditional mixin rename preserves other conditional compositions`() { + val text = manualCompositionFixture("condMixinTests", "TestConsole") + val uri = source("condMixinTests", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val constraint = text.positionOf("MixN", "Element") + val declaration = text.positionOf("static mixin MixN + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("Root self();", "self") + assertThat(adapter.rename(uri, at.line, at.column, "occupied")).isNull() + assertThat(directory.resolve("mixinTests.x").toFile().readText()).isEqualTo(text) + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int size()", "text.size()"]) + fun `conditional source composition renames from declaration and concrete receiver`(anchor: String) { + val text = conditionalSource() + val uri = source("Conditional", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf(anchor, "size") + val edit = requireNotNull(adapter.rename(uri, at.line, at.column, "width")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("Int size()", "Int width()").replace("text.size()", "text.width()")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val renamed = changed.positionOf("text.width()", "width") + val reverse = requireNotNull(adapter.rename(uri, renamed.line, renamed.column, "size")) + assertThat(apply(changed, reverse.changes.getValue(uri))).isEqualTo(text) + assertThat(directory.resolve("Conditional.x").toFile().readText()).isEqualTo(text) + } + } + + @Test + fun `conditional composition rename refuses a concrete host collision`() { + val text = conditionalSource("Int width() = 0;") + val uri = source("Conditional", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("Int size()", "size") + assertThat(adapter.rename(uri, at.line, at.column, "width")).isNull() + assertThat(directory.resolve("Conditional.x").toFile().readText()).isEqualTo(text) + } + } + + private fun conditionalSource(body: String = ""): String = + """ + module Conditional { + class Box(T value) incorporates conditional Textual { Int size(); $body } + static mixin Textual into Box { @Override Int size() = value.size; } + Int read(Box text) = text.size(); + Int unrelated(Box number) = number.value; + } + """.trimIndent() + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedWith(compareByDescending { it.range.start.line }.thenByDescending { it.range.start.column }) + .fold(text) { current, edit -> + fun offset(position: Position) = current.splitToSequence('\n').take(position.line).sumOf { it.length + 1 } + position.column + current.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt new file mode 100644 index 00000000000..7cc748e0db4 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt @@ -0,0 +1,64 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkManualCompositionTest { + @TempDir lateinit var directory: Path + + @Test + fun `manual covariant mixin redirects navigate to written source methods`() { + val text = manualCompositionFixture("mixinTests") + val uri = source("mixinTests", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val root = text.positionOf("Root self();", "self") + val base = text.positionOf("Base self()", "self") + val implementations = adapter.findImplementation(uri, root.line, root.column) + assertThat(implementations.map { Position(it.startLine, it.startColumn) }).contains(base) + implementations.forEach { target -> + assertThat(target.uri).isEqualTo(uri) + assertThat(text.lines()[target.startLine].substring(target.startColumn, target.endColumn)).isEqualTo("self") + } + assertThat(adapter.getCachedResult(uri)!!.diagnostics).isEmpty() + } + } + + @Test + fun `manual interface delegation never invents a runtime receiver implementation`() { + val text = manualCompositionFixture("delegationTests") + val uri = source("delegationTests", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.positionOf("String showText();", "showText") + val targets = adapter.findImplementation(uri, at.line, at.column) + assertThat(targets.map { Position(it.startLine, it.startColumn) }) + .containsExactlyInAnyOrder( + text.positionOf("@Override String showText() = name;", "showText"), + text.positionOf("@Override String showText() = super()", "showText"), + ) + targets.forEach { target -> + val line = text.lines()[target.startLine] + assertThat(line.substring(target.startColumn, target.endColumn)).isEqualTo("showText") + assertThat(line).doesNotContain("delegates") + } + } + } + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt new file mode 100644 index 00000000000..5788630fe4e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt @@ -0,0 +1,641 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path + +class XdkMemberActionsTest { + @TempDir lateinit var directory: Path + + @Test + fun `recursive inherited signatures do not suppress unrelated member actions`() { + val text = + """ + module App { + typedef (Int | Array) as Doc; + class Base { + T read(T value) = value; + Int count() = 1; + } + class Box extends Base {} + } + """.trimIndent() + workspace(text) { adapter, uri -> + val offered = actions(adapter, uri, text) + assertThat(offered.map { it.title }).contains("Override Int64 count()") + assertThat(offered).noneMatch { it.title.startsWith("Override ") && " read(" in it.title } + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int read(Int value);", "void read();", "String read(String value);"]) + fun `implement ordinary abstract source contracts with compiler proven stubs`(signature: String) { + val text = "module App { interface Api { $signature } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + assertThat(action.kind).isEqualTo(CodeAction.CodeActionKind.REFACTOR_REWRITE) + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).contains("@Override", "TODO();") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(actions(adapter, uri, changed).filter { it.title.startsWith("Implement ") }) + .isEmpty() + } + } + + @Test + fun `override inherited concrete source method without changing unrelated bindings`() { + val text = + "module App { class Base { Int read(Int value) = value; } class Box extends Base { String keep = \"ok\"; } }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title.startsWith("Override ") && it.title.contains(" read(") + } + assertThat(action.kind).isEqualTo(CodeAction.CodeActionKind.REFACTOR_REWRITE) + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `overloads remain separate compiler selected actions`() { + val text = + "module App { interface Api { Int read(Int value); String read(String value); } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val actions = + actions(adapter, uri, text).filter { + it.title.startsWith("Implement ") && !it.title.startsWith("Implement all") + } + assertThat(actions).hasSize(2) + actions.forEach { action -> + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + } + + @Test + fun `all required overloads repair an instantiated class atomically`() { + val text = + """ + module App { + interface Api { Int read(Int value); String read(String value); } + class Box implements Api {} + class Child extends Box {} + Box make() = new Box(); + String use(Child child) = child.read(value = "text"); + } + """.trimIndent() + workspace(text, initiallyValid = false) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement all required members (2)" + } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(edit.changes.getValue(uri)).hasSize(1) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed.split("@Override")).hasSize(3) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(actions(adapter, uri, changed).filter { it.title.startsWith("Implement ") }) + .isEmpty() + } + } + + @Test + fun `bulk generation preserves existing implementations and property bindings`() { + val text = + """ + module App { + interface Api { Int first(); String second(); void third(); } + class Box implements Api { @Override Int first() = 7; String label = "keep"; } + Box make() = new Box(); + String use(Box box) = box.second() + box.label; + } + """.trimIndent() + workspace(text, initiallyValid = false) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement all required members (2)" + } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains("@Override Int first() = 7; String label = \"keep\";") + assertThat(changed.split("first()")).hasSize(3) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `all required action refuses an unsupported remainder on an implicitly abstract class`() { + val text = + "module App { interface Api { void first(); void second(); List read(List value = []); } " + + "class Box implements Api {} }" + workspace(text) { adapter, uri -> + val offered = actions(adapter, uri, text) + assertThat(offered.map { it.title }) + .contains("Implement void first()", "Implement void second()") + assertThat(offered).noneMatch { it.title.startsWith("Implement all") } + } + } + + @Test + fun `specialized generic contract uses the compiler substituted signature`() { + val text = + "module App { interface Api { T read(T value); } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title.startsWith("Implement String read(String value)") + } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `CRLF indentation and Unicode text survive generation`() { + val text = + "module App {\r\n interface Api { void read(); }\r\n // café\r\n class Box implements Api {\r\n }\r\n}\r\n" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed) + .contains(" // café\r\n", " @Override\r\n", " TODO();\r\n") + assertThat(changed.replace("\r\n", "")).doesNotContain("\n") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "conditional Int read();", + "(Int, Int) read();", + "Int read(Int value = 1);", + "List read();", + " T read(T value);", + " T read(T value);", + "conditional (Int, String) read();", + "Map> read();", + "(Int | String) read();", + "String? read(String? value);", + "immutable List read();", + "List<(Int | String)> read();", + "String read(String value = \"line\\n\\\"quoted\\\"\");", + ], + ) + fun `broader signatures preserve the compiler contract`(signature: String) { + val text = "module App { interface Api { $signature } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(actions(adapter, uri, changed).filter { it.title.startsWith("Implement ") }) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["List read(List value = []);"]) + fun `unrenderable types and nonliteral defaults remain refusals`(signature: String) { + val text = "module App { interface Api { $signature } class Box implements Api {} }" + workspace(text) { adapter, uri -> + assertThat(actions(adapter, uri, text).filter { it.title.contains(" read(") }).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "Int read(Int value = -2);", + "String read(String value = \"text\");", + "Char read(Char value = 'x');", + ], + ) + fun `fresh declaration repair retains literal defaults`(signature: String) { + val text = + "module App { interface Api { $signature } class Box implements Api {} " + + "Box make() = new Box(); void use(Box box) { box.read(); } }" + workspace(text, initiallyValid = false) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains(signature.substringAfter("value = ").substringBefore(')')) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `validated computed primitive default is rendered as its constant value`() { + val text = + "module App { interface Api { Int read(Int value = 1 + 2); } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement Int64 read(Int64 value = 3)" + } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `unvalidated computed defaults remain a refusal during fresh declaration repair`() { + val text = + "module App { interface Api { Int read(Int value = 1 + 2); } class Box implements Api {} Box make() = new Box(); }" + workspace(text, initiallyValid = false) { adapter, uri -> + assertThat(actions(adapter, uri, text).filter { it.title.contains(" read(") }).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = ["Boolean read(Boolean value = True);", "String? read(String? value = Null);"], + ) + fun `validated singleton defaults use explicit XDK identities`(signature: String) { + val text = "module App { interface Api { $signature } class Box implements Api {} }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains(" = ecstasy.") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `existing call intentionally selects the generated override`() { + val text = + "module App { class Base { Int read() = 1; } class Box extends Base {} Int use(Box box) = box.read(); }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title.startsWith("Override ") && it.title.contains(" read(") + } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `source contract in another module generates an edit only in the implementing class`() { + val library = "module Library { interface Api { String read(String value); } }" + val file = directory.resolve("Library.x").toFile().apply { writeText(library) } + val text = "module App { package lib import Library; class Box implements lib.Api {} }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(adapter.compile(uri, apply(text, edit.changes.getValue(uri))).diagnostics) + .isEmpty() + assertThat(file.readText()).isEqualTo(library) + } + } + + @Test + fun `foreign signature types add qualified imports without shadowing existing names`() { + val library = "module Library { class Value {} interface Api { Value read(Value value); } }" + val file = directory.resolve("Library.x").toFile().apply { writeText(library) } + val text = + "module App { package lib import Library; class Box implements lib.Api { Int library = 1; } " + + "Int keep(Box box) = box.library; }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement library1.Value read(library1.Value value)" + } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(edit.changes.getValue(uri)).hasSize(2) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).contains("package library1 import Library;", "Int library = 1;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(file.readText()).isEqualTo(library) + } + } + + @Test + fun `bulk foreign signatures share one import while repairing construction`() { + directory + .resolve("Library.x") + .toFile() + .writeText( + "module Library { class Value {} interface Api { Value read(); void write(Value value); } }", + ) + val text = + "module App { package lib import Library; class Box implements lib.Api {} Box make() = new Box(); }" + workspace(text, initiallyValid = false) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement all required members (2)" + } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed.split("package library import Library;")).hasSize(2) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `binary contracts generate only user source edits with or without indexed sources`(indexed: Boolean) { + CompilerTestSupport.configure() + val errors = ErrorList() + val library = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Library { interface Api { String read(String value); } }", + "file:///Library.x", + ), + null, + errors, + ) + assertThat(library.succeeded()).describedAs(errors.errors.toString()).isTrue() + val indexedDependency = library.toDependency() + val dependency = + if (indexed) indexedDependency else XdkDependency.fromBinary(indexedDependency.bytes()) + val original = dependency.bytes() + val text = + "module App { package lib import Library; class Box implements lib.Api {} " + + "Box make() = new Box(); String use(Box box) = box.read(value = \"text\"); }" + workspace(text, initiallyValid = false, dependencies = listOf(dependency)) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement String read(String value)" + } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(adapter.compile(uri, apply(text, edit.changes.getValue(uri))).diagnostics) + .isEmpty() + assertThat(dependency.bytes()).isEqualTo(original) + } + } + + @Test + fun `bundled Iterator contract repairs construction without editing the XDK`() { + val text = "module App { class Box implements Iterator {} Box make() = new Box(); }" + workspace(text, initiallyValid = false) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title == "Implement conditional String next()" + } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(adapter.compile(uri, apply(text, edit.changes.getValue(uri))).diagnostics) + .isEmpty() + } + } + + @Test + fun `bundled concrete method override preserves descendant and selected call bindings`() { + val text = + "module App { class Box implements Iterator { @Override conditional String next() = False; } " + + "class Child extends Box {} String use(Child child) = child.take(); }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title == "Override String take()" } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `descendants inherit the generated override in compiler dispatch order`() { + val text = + "module App { class Base { Int read() = 1; } class Box extends Base {} class Child extends Box {} }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title.startsWith("Override ") && it.title.contains(" read(") + } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `named arguments and descendant overrides preserve overload and property bindings`() { + val text = + """ + module App { + class Base { + Int read(Int value) = value; + String read(String value) = value; + String label = "keep"; + } + class Box extends Base {} + class Child extends Box { @Override Int read(Int value) = super(value); } + Int use(Box box) = box.read(value = 1); + Int child(Child child) = child.read(value = 2); + String other(Box box) = box.read(value = "text") + box.label; + } + """.trimIndent() + workspace(text) { adapter, uri -> + val actions = + actions(adapter, uri, text).filter { + it.title.startsWith("Override ") && it.title.contains(" read(") + } + assertThat(actions).hasSize(2) + actions.forEach { action -> + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed) + .contains("child.read(value = 2)", "box.read(value = \"text\") + box.label") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + } + + @Test + fun `generic override supports inferred calls and descendant dispatch`() { + val text = + """ + module App { + class Base { T read(T value) = value; } + class Box extends Base {} + class Child extends Box {} + String use(Child child) = child.read(value = "text"); + } + """.trimIndent() + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title.startsWith("Override ") } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `closed dependent module follows the new override without receiving edits`() { + val consumer = + "module Consumer { package app import App; class Child extends app.Box {} Int use(Child child) = child.read(); }" + val file = directory.resolve("Consumer.x").toFile().apply { writeText(consumer) } + val text = "module App { class Base { Int read() = 1; } class Box extends Base {} }" + workspace(text) { adapter, uri -> + val action = + actions(adapter, uri, text).single { + it.title.startsWith("Override ") && it.title.contains(" read(") + } + val edit = requireNotNull(action.edit) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(adapter.compile(uri, apply(text, edit.changes.getValue(uri))).diagnostics) + .isEmpty() + assertThat(file.readText()).isEqualTo(consumer) + } + } + + @Test + fun `a broken neighbor prevents publishing a generation edit`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + val text = "module App { interface Api { Int read(); } class Box implements Api {} }" + workspace(text) { adapter, uri -> assertThat(actions(adapter, uri, text)).isEmpty() } + } + + @Test + fun `missing implementation at construction can be repaired from fresh declarations`() { + val text = + "module App { interface Api { String read(String value); } class Box implements Api {} Box make() = new Box(); }" + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = source.toURI().toString() + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + val action = actions(adapter, uri, text).single { it.title.startsWith("Implement ") } + assertThat( + adapter + .compile( + uri, + apply(text, requireNotNull(action.edit).changes.getValue(uri)), + ).diagnostics, + ).isEmpty() + assertThat(source.readText()).isEqualTo(text) + } + } + + @Test + fun `declaration errors do not produce a member fix from failed type information`() { + val text = + "module App { interface Api { Missing read(); } class Box implements Api {} Box make() = new Box(); }" + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = source.toURI().toString() + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + assertThat(actions(adapter, uri, text)).isEmpty() + } + } + + @Test + fun `code generation only appears at the selected class declaration`() { + val text = "module App { interface Api { Int read(); } class Box implements Api {} }" + workspace(text) { adapter, uri -> + assertThat( + adapter + .getCodeActions( + uri, + Range(Position(0, 0), Position(0, 0)), + emptyList(), + ).filter { it.title.startsWith("Implement ") }, + ).isEmpty() + } + } + + private fun actions( + adapter: XdkAdapter, + uri: String, + text: String, + ): List { + val offset = text.indexOf("Box") + val at = position(text, offset) + return adapter.getCodeActions(uri, Range(at, at), emptyList()) + } + + private fun position( + text: String, + offset: Int, + ): Position = + Position( + text.take(offset).count { it == '\n' }, + offset - text.lastIndexOf('\n', offset - 1) - 1, + ) + + private fun apply( + text: String, + edits: List, + ): String { + fun offset(at: Position): Int = text.split('\n').take(at.line).sumOf { it.length + 1 } + at.column + return edits + .sortedByDescending { offset(it.range.start) } + .fold(text) { result, edit -> + result.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + } + + private fun workspace( + text: String, + initiallyValid: Boolean = true, + dependencies: List = emptyList(), + check: (XdkAdapter, String) -> Unit, + ) { + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + adapter.replaceDependencies(dependencies) + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = source.toURI().toString() + assertThat(adapter.compile(uri, text).diagnostics.isEmpty()) + .describedAs(text) + .isEqualTo(initiallyValid) + check(adapter, uri) + assertThat(source.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt new file mode 100644 index 00000000000..19f740eb1b9 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt @@ -0,0 +1,148 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkMemberInlineTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["1 + 2", "probe() + probe()", "stored + probe()"]) + fun `inline method call preserves bindings evaluation order and its declaration`(expression: String) { + query( + """ + module Inline { + Int stored = 3; + Int probe() = ++stored; + private Int helper() { return $expression; } + Int read() { return 2 * §helper(); } + } + """.trimIndent(), + "Inline private method call", + "return 2 * ($expression);", + ) + } + + @Test + fun `static constant property uses compiler validated value at selected read`() { + query( + """ + module Inline { + private static Int amount = 1 + 2; + Int read() { return 2 * §amount; } + } + """.trimIndent(), + "Inline constant property", + "return 2 * (1 + 2);", + ) + } + + @Test + fun `caller name capture refuses an otherwise valid body copy`() { + query( + """ + module Inline { + Int stored = 3; + private Int helper() { return stored; } + Int read(Int stored) { return §helper(); } + } + """.trimIndent(), + "Inline private method call", + null, + ) + } + + @ParameterizedTest + @ValueSource( + strings = [ + "Int helper() { return 1; }", + "private Int helper(Int value = 1) { return value; }", + "private Int helper() { Int value = 1; return value; }", + "private Int helper() { return helper(); }", + ], + ) + fun `public parameterized multi statement and recursive methods stay unchanged`(method: String) { + query( + """ + module Inline { + $method + Int read() { return §helper(); } + } + """.trimIndent(), + "Inline private method call", + null, + ) + } + + @Test + fun `instance property reads cannot discard their initialization or getter behavior`() { + query( + """ + module Inline { + private Int amount = 3; + Int read() { return §amount; } + } + """.trimIndent(), + "Inline constant property", + null, + ) + } + + @Test + fun `static runtime initializer is not repeated at its read`() { + query( + """ + module Inline { + private static Int amount = compute(); + static Int compute() = 3; + Int read() { return §amount; } + } + """.trimIndent(), + "Inline constant property", + null, + ) + } + + private fun query( + marked: String, + title: String, + expected: String?, + ) { + CompilerTestSupport.configure() + val text = marked.replace("§", "") + val file = + directory + .resolve("Inline.x") + .toFile() + .canonicalFile + .apply { writeText(text) } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = XdkRename.position(text, marked.indexOf('§')) + val actions = adapter.getCodeActions(uri, Range(at, at), emptyList()).filter { it.title == title } + if (expected == null) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).hasSize(1) + val edit = requireNotNull(actions.single().edit) + assertThat(edit.versioned).isTrue() + val change = edit.changes.getValue(uri).single() + + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + val changed = text.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + assertThat(changed).contains(expected) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + assertThat(file.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt new file mode 100644 index 00000000000..38c9765ce05 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt @@ -0,0 +1,381 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path + +class XdkMethodExtractionTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = ["Int|input + step()", "Int8|input + 1", "Boolean|input && flag()", "String|input + \"!\"", "Int[]|[input, step()]"], + ) + fun `whole return expression keeps its type calls and stable inputs`(example: String) { + val (type, expression) = example.split('|') + val inputType = if (type == "Int[]") "Int" else type + query( + """ + module Extract { + Int step() = 2; + Boolean flag() = True; + $type read($inputType input) { + return §$expression§; + } + } + """.trimIndent(), + ) { text, changed -> + assertThat( + changed, + ).contains("return extractedMethod(input);", "private $type extractedMethod($inputType input)", "return $expression;") + assertThat(changed).isNotEqualTo(text) + } + } + + @Test + fun `typed initializer captures a stable local and preserves overload selection`() { + query( + """ + module Extract { + Int step(Int input) = input; + Int step(String input) = input.size; + Int read(Int input) { + Int fixed = input + 1; + Int result = §step(fixed) + input§; + return result; + } + } + """.trimIndent(), + ) { _, changed -> + assertThat( + changed, + ).contains( + "Int result = extractedMethod(fixed, input);", + "private Int extractedMethod(Int fixed, Int input)", + "return step(fixed) + input;", + ) + } + } + + @Test + fun `generic owner and implicit instance receiver stay in the same type`() { + query( + """ + module Extract { + class Box { + Element identity(Element value) = value; + Element read(Element input) { + return §identity(input)§; + } + } + } + """.trimIndent(), + ) { _, changed -> + assertThat(changed).contains("private Element extractedMethod(Element input)", "return identity(input);") + } + } + + @Test + fun `static helper preserves comments unicode CRLF and chooses a fresh name`() { + query( + """ + module Extract { + Int extractedMethod() = 0; + static Int read(Int input) { + return §input /* 😀 */ + 1§; + } + } + """.trimIndent().replace("\n", "\r\n"), + ) { _, changed -> + assertThat( + changed, + ).contains( + "return extractedMethod1(input);", + "private static Int extractedMethod1(Int input) {\r\n return input /* 😀 */ + 1;", + ) + } + } + + @Test + fun `constant expression needs no invented parameter`() { + query( + """ + module Extract { + Int read() { + return §1 + 2§; + } + } + """.trimIndent(), + ) { _, changed -> + assertThat(changed).contains("return extractedMethod();", "private Int extractedMethod()") + } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "input++;\nreturn §input + 1§;", + "@Volatile Int stored = input;\nreturn §stored + 1§;", + "Int changed = input;\nchanged++;\nreturn §changed + 1§;", + ], + ) + fun `refuse mutation and reference storage`(body: String) { + refused( + """ + module Extract { + Int read(Int input) { + ${body.replace("\n", "\n ")} + } + } + """.trimIndent(), + ) + } + + @Test + fun `method formals lambda creation and conditional returns remain explicit refusals`() { + listOf( + """ + module Extract { + Element read(Element input) { + return §input§; + } + } + """.trimIndent(), + """ + module Extract { + function Int() read(Int input) { + return §() -> input§; + } + } + """.trimIndent(), + """ + module Extract { + conditional Int read(String input) { + return §input.indexOf('a')§; + } + } + """.trimIndent(), + ).forEach(::refused) + } + + @Test + fun `stateful calls retain their order around a stable input`() { + query( + """ + module Extract { + class Counter { + Int count = 0; + Int next() = ++count; + Int read(Int input) { + return §next() + input + next()§; + } + } + } + """.trimIndent(), + ) { _, changed -> + assertThat(changed).contains("return extractedMethod(input);", "return next() + input + next();") + assertThat(Regex("return next\\(\\) ").findAll(changed).count()).isEqualTo(1) + } + } + + @Test + fun `anonymous class creation is not moved across a closure boundary`() { + refused( + """ + module Extract { + interface Reader { Int read(); } + Reader make(Int input) { + return §new Reader() { @Override Int read() = input; }§; + } + } + """.trimIndent(), + ) + } + + @Test + fun `broken graph neighbor refuses publication`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + refused( + """ + module Extract { + Int read(Int input) { + return §input + 1§; + } + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["return §input§ + 1;", "val value = §input + 1§;\nreturn value;", "return accept(§input + step()§);"]) + fun `nested expressions and inferred initializers retain the compiler selected result type`(body: String) { + query( + """ + module Extract { + Int step() = 1; + Int accept(Int value) = value; + Int read(Int input) { + ${body.replace("\n", "\n ")} + } + } + """.trimIndent(), + ) { _, changed -> assertThat(changed).contains("private Int64 extractedMethod(Int input)") } + } + + @Test + fun `condition extraction keeps its original branch evaluation site`() { + query( + """ + module Extract { + Int read(Int input) { + if (§input > 0§) { + return 1; + } + return 0; + } + } + """.trimIndent(), + ) { _, changed -> assertThat(changed).contains("if (extractedMethod(input))", "private Boolean extractedMethod(Int input)") } + } + + @Test + fun `contiguous call statements move once in their original order`() { + query( + """ + module Extract { + void emit(Int value) {} + void run(Int input) { + §emit(input); + emit(input + 1);§ + } + } + """.trimIndent(), + title = "Extract statements to private method", + ) { _, changed -> + assertThat(changed) + .contains("private void extractedMethod(Int input)", "extractedMethod(input);") + .containsOnlyOnce("emit(input);") + .containsOnlyOnce("emit(input + 1);") + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `inferred result from another module uses its source alias for source and binary dependencies`(binary: Boolean) { + CompilerTestSupport.configure() + val library = + directory.resolve("Library.x").toFile().canonicalFile.apply { + writeText("module Library { class Value {} }") + } + query( + """ + module Extract { + package lib import Library; + Object read(lib.Value input) { + val value = §input§; + return value; + } + } + """.trimIndent(), + configure = { adapter, uri -> + if (binary) { + val errors = ErrorList() + val compiled = EmbeddingSupport.instance().compileModule(Source(library.readText(), library.path), null, errors) + assertThat(compiled.succeeded()).describedAs("%s", errors).isTrue() + adapter.replaceDependencies(listOf(compiled.toDependency())) + } + adapter.replaceSourceModules( + buildList { + if (!binary) add(XdkSourceModule("Library", library.toURI().toString())) + add(XdkSourceModule("Extract", uri, setOf("Library"))) + }, + ) + }, + ) { _, changed -> assertThat(changed).contains("private lib.Value extractedMethod(lib.Value input)") } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "if (input > 0) { emit(input); }", + "Int local = input;", + "input++;", + ], + ) + fun `statement extraction refuses intervening control flow declarations and mutable captures`(middle: String) { + query( + """ + module Extract { + void emit(Int value) {} + void run(Int input) { + §emit(input); + $middle + emit(input);§ + } + } + """.trimIndent(), + expected = false, + title = "Extract statements to private method", + ) { _, _ -> error("Unexpected statement extraction") } + } + + private fun refused(marked: String) = query(marked, expected = false) { _, _ -> error("Unexpected extraction") } + + private fun query( + marked: String, + expected: Boolean = true, + title: String = "Extract expression to private method", + configure: (XdkAdapter, String) -> Unit = { _, _ -> }, + check: (String, String) -> Unit, + ) { + CompilerTestSupport.configure() + val start = marked.indexOf('§') + val end = marked.lastIndexOf('§') - 1 + val text = marked.replace("§", "") + val file = + directory + .resolve("Extract.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + configure(adapter, uri) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val range = Range(XdkRename.position(text, start), XdkRename.position(text, end)) + val action = adapter.getCodeActions(uri, range, emptyList()).singleOrNull { it.title == title } + if (!expected) { + assertThat(action).isNull() + return + } + val edit = requireNotNull(action) { "No method extraction for $marked" }.edit!! + assertThat(edit.versioned).isTrue() + val changed = + edit.changes + .getValue(uri) + .sortedWith( + compareByDescending { + it.range.start.line + }.thenByDescending { it.range.start.column }, + ).fold(text) { value, change -> + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + value.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + } + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(file.readText()).isEqualTo(text) + check(text, changed) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt new file mode 100644 index 00000000000..43244711a83 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt @@ -0,0 +1,134 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.PartialSemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkMissingDeclarationNameTest { + @Test + fun `missing property and parameter names use written types without registering bindings`() { + XdkAdapter().use { adapter -> + listOf( + "String § = \"\";" to "string", + "String string = \"\"; String § = \"\";" to "string1", + "List § = [];" to "list", + "void run(String §) {}" to "string", + "void run(Int count, List § = []) {}" to "list", + "class Item { construct(String §) {} }" to "string", + "class Item(String §) {}" to "string", + "String? § = Null;" to "string", + "String[] § = [];" to "stringArray", + "immutable String § = \"\";" to "string", + "void run((String | Int) §) {}" to "value", + "void run(function String(Int)? §) {}" to "fn", + "class Item(String?[] §) {}" to "stringArray", + ).forEach { (header, expected) -> + val marked = "module Editing { $header Int later = 1; }" + val text = marked.replace("§", "") + val at = Position(0, marked.indexOf('§')) + val original = adapter.compile(URI, text) + assertThat(original.diagnostics).describedAs(marked).isNotEmpty() + val items = adapter.getCompletions(URI, at.line, at.column) + assertThat(items).describedAs(marked).hasSize(1) + val item = items.single() + assertThat(item.label).describedAs(marked).isEqualTo(expected) + assertThat(item.textEdit).isEqualTo(TextEdit(Range(at, at), expected)) + assertThat(adapter.getCachedResult(URI)).isEqualTo(original) + val partial = adapter.analyzeAtAsync(URI, at).join()!! + val site = partial.sites.single() + assertThat(site.kind).isEqualTo(PartialSemanticModel.Kind.DECLARATION_NAME) + assertThat(site.members).isEmpty() + assertThat(site.receiverType).isNull() + assertThat(site.callCandidates).isNull() + assertThat(adapter.compile(URI, text.replaceRange(at.column, at.column, item.insertText)).diagnostics) + .describedAs(marked) + .isEmpty() + } + } + } + + @Test + fun `EOF and Unicode CRLF slots keep exact insertion coordinates`() { + XdkAdapter().use { adapter -> + listOf( + "module Editing { String §", + "module Editing { void run(String §", + "module Editing { class Item(String §", + "module Editing {\r\n /* 😀 */ String §;\r\n}", + ).forEach { marked -> + val before = marked.substringBefore('§') + val position = Position(before.count { it == '\n' }, before.substringAfterLast('\n').length) + adapter.compile(URI, marked.replace("§", "")) + val item = adapter.getCompletions(URI, position.line, position.column).single() + assertThat(item.textEdit).describedAs(marked).isEqualTo(TextEdit(Range(position, position), "string")) + } + } + } + + @Test + fun `written names and ambiguous value positions do not become empty declaration slots`() { + XdkAdapter().use { adapter -> + listOf( + "String §already;", + "void run(String §already) {}", + "void run() { String §; }", + "void run(String value) { value §; }", + "void run() { call(String §); }", + "String text = \"String §\";", + "String /* § */ value;", + "List + val marked = "module Editing { $header }" + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))) + .describedAs(marked) + .noneMatch { it.detail.startsWith("Name from written type") } + } + } + } + + @Test + fun `inferred local names use retained initializer syntax and exact edits`() { + listOf( + "val § = \"hello\";" to "text", + "var § = 42;" to "number", + "val § = True;" to "flag", + "val § = new StringBuffer();" to "stringBuffer", + "val te§ = \"hello\";" to "text", + "String st§ = \"hello\";" to "string", + "String[] st§ = [];" to "stringArray", + ).forEach { (declaration, expected) -> + val marked = "module Editing { void run() { $declaration Int later = 1; } }" + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, source) + val item = adapter.getCompletions(URI, 0, marked.indexOf('§')).single { it.label == expected } + assertThat(item.detail).startsWith("Name from written") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + val edit = item.textEdit!! + assertThat( + adapter.compile(URI, source.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText)).diagnostics, + ).describedAs(marked) + .isEmpty() + } + } + } + + @Test + fun `inference without a useful written clue does not invent a type or name`() { + listOf("val § = Null;", "var § = unknown();", "val §;", "value § = 1;").forEach { declaration -> + val marked = "module Editing { void run() { $declaration } }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))) + .describedAs(marked) + .noneMatch { it.detail.startsWith("Name from written") } + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt new file mode 100644 index 00000000000..3b3485921e8 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt @@ -0,0 +1,119 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkMissingDeclarationsTest { + @TempDir lateinit var directory: Path + + @Test + fun `create missing zero argument class repairs the selected constructor use`() { + query( + """ + module App { + Object make() { + return new §Widget(); + } + } + """.trimIndent(), + "Create class 'Widget'", + "class Widget {}", + ) + } + + @ParameterizedTest + @ValueSource(strings = ["", "static "]) + fun `read only property repairs instance access but never invents a static constant value`(modifier: String) { + query( + """ + module App { + ${modifier}Int read() { + return §missing; + } + } + """.trimIndent(), + "Create read-only property 'missing'", + if (modifier.isEmpty()) "private Int missing.get() { TODO(); }" else null, + ) + } + + @ParameterizedTest + @ValueSource(strings = ["new §Widget(Int:1)", "new package.§Widget()", "new §Widget()"]) + fun `constructor requirements and unknown destination owners are not guessed`(expression: String) { + query( + """ + module App { + Object make() { + return $expression; + } + } + """.trimIndent(), + "Create class 'Widget'", + null, + ) + } + + @Test + fun `neighbor error prevents publication of a new declaration`() { + query( + """ + module App { + Int broken() = "wrong"; + Object make() { + return new §Widget(); + } + } + """.trimIndent(), + "Create class 'Widget'", + null, + ) + } + + private fun query( + marked: String, + title: String, + expected: String?, + ) { + CompilerTestSupport.configure() + val text = marked.replace("§", "") + val file = + directory + .resolve("App.x") + .toFile() + .canonicalFile + .apply { writeText(text) } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val diagnostics = adapter.compile(uri, text).diagnostics + assertThat(diagnostics).isNotEmpty().noneMatch { it.code == "EMB-5" } + val at = XdkRename.position(text, marked.indexOf('§')) + val actions = adapter.getCodeActions(uri, Range(at, at), diagnostics).filter { it.title == title } + if (expected == null) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).describedAs("Diagnostics: %s", diagnostics).hasSize(1) + val edit = requireNotNull(actions.single().edit) + assertThat(edit.versioned).isTrue() + val changed = + edit.changes + .getValue(uri) + .sortedByDescending { it.range.start.line * text.length + it.range.start.column } + .fold(text) { current, change -> + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + current.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + } + assertThat(changed).contains(expected) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + assertThat(file.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt new file mode 100644 index 00000000000..7e9d6d607b4 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt @@ -0,0 +1,688 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkMissingMethodsTest { + @TempDir lateinit var directory: Path + + @Test + fun `recursive aliases without a proven source spelling refuse method generation`() { + refused( + """ + module Missing { + typedef (Int | Array) as Doc; + Doc run(Doc value) = §missing(value); + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @CsvSource(value = ["Int;Int64", "String;String", "List;List", "(Int | String);(Int64 | String)"], delimiter = ';') + fun `resolved parameter and return types produce a private method`( + type: String, + rendered: String, + ) { + query( + """ + module Missing { + $type read($type value) { + return §missing(value); + } + } + """.trimIndent(), + ) { changed -> + assertThat(changed).contains("private $rendered missing($rendered arg1)", "TODO();", "return missing(value);") + } + } + + @Test + fun `statement call creates void method with no arguments`() { + query( + """ + module Missing { + void run() { + §missing(); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private void missing()") } + } + + @ParameterizedTest + @ValueSource(strings = ["this", "this:private", "peer"]) + fun `same owner receiver creates an instance method`(receiver: String) { + query( + """ + module Missing { + class Box { + Int read(Box peer, Int value) { + return $receiver.§missing(value); + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)").doesNotContain("private static") } + } + + @ParameterizedTest + @ValueSource(strings = ["", "static "]) + fun `class qualifier creates a static method regardless of the caller`(modifier: String) { + query( + """ + module Missing { + class Box { + ${modifier}Int read(Int value) { + return Box.§missing(value); + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private static Int64 missing(Int64 arg1)", "Box.missing(value)") } + } + + @Test + fun `fully qualified owner preserves typed initializer and local argument evidence`() { + query( + """ + module Missing { + class Box { + void read(List values) { + val other = values; + List result = Missing.Box.§missing(other); + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private static List missing(List arg1)", "Missing.Box.missing(other)") } + } + + @Test + fun `class qualifier supports a void call and multiple compatible sites`() { + query( + """ + module Missing { + class Box { + void run() { + Box.§missing(); + Box.missing(); + } + } + } + """.trimIndent(), + wholeFile = true, + ) { assertThat(it).contains("private static void missing()") } + } + + @Test + fun `parameter shadowing the class name remains an instance receiver`() { + query( + """ + module Missing { + class Box { + static Int read(Missing.Box Box, Int value) { + return Box.§missing(value); + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)").doesNotContain("private static Int64 missing") } + } + + @ParameterizedTest + @ValueSource(strings = ["Other", "Outer.Box", "type", "clz"]) + fun `other owner and runtime type qualifiers remain refusals`(qualifier: String) { + refused( + """ + module Missing { + class Other {} + class Outer { class Box {} } + class Box { + Int read(Type type, Class clz, Int value) { + return $qualifier.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `a class spelling shadowed by a different instance type does not establish ownership`() { + refused( + """ + module Missing { + class Box { + Int read(String Box, Int value) { + return Box.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `module singleton qualifier retains instance dispatch`() { + query( + """ + module Missing { + static Int read(Int value) { + return Missing.§missing(value); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)").doesNotContain("private static") } + } + + @Test + fun `explicit type arguments do not acquire a guessed static signature`() { + refused( + """ + module Missing { + class Box { + Int read(Int value) { + return Box.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `existing static overload is not mistaken for a missing declaration`() { + refused( + """ + module Missing { + class Box { + static Int missing(Int value) = value; + Int read(String value) { + return Box.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `module this receiver supports a void statement call`() { + query( + """ + module Missing { + void run() { + this.§missing(); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private void missing()") } + } + + @Test + fun `selecting only the receiver does not offer creation for the member`() { + refused( + """ + module Missing { + class Box { + Int read(Box peer, Int value) { + return §peer.missing(value); + } + } + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Box other = peer;", "var other = peer;", "val other = peer;"]) + fun `same owner local receiver supports typed initializer result in a static caller`(local: String) { + query( + """ + module Missing { + class Box { + static Int read(Box peer, Int value) { + $local + Int result = other.§missing(value); + return result; + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)").doesNotContain("private static Int64 missing") } + } + + @Test + fun `explicit receiver bypasses unrelated local name shadowing`() { + query( + """ + module Missing { + Int read(Int value) { + Int missing = value; + return this.§missing(missing); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)", "this.missing(missing)") } + } + + @ParameterizedTest + @ValueSource(strings = ["other", "otherPeer", "base", "super", "this:public", "this:protected", "this:struct"]) + fun `unproven receiver owners and access views remain refusals`(receiver: String) { + refused( + """ + module Missing { + class Base {} + class Other {} + class Box extends Base { + Box make() = this; + Int read(Other other, Base base, String otherPeer, Int value) { + return $receiver.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `a class with the same short name is not the same receiver owner`() { + refused( + """ + module Missing { + class Outer { class Box {} } + class Box { + Int read(Outer.Box peer, Int value) { + return peer.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `this in a static caller does not invent instance access`() { + refused( + """ + module Missing { + class Box { + static Int read(Int value) { + return this.§missing(value); + } + } + } + """.trimIndent(), + ) + } + + @Test + fun `qualified existing overload remains an overload error`() { + refused( + """ + module Missing { + Int missing(Int value) = value; + Int read(String value) { + return this.§missing(value); + } + } + """.trimIndent(), + ) + } + + @Test + fun `static typed return preserves CRLF and unicode`() { + query( + """ + module Missing { + // 😀 keep the call's UTF-16 position + static Int read(Int value) { + return §missing(value); + } + } + """.trimIndent().replace("\n", "\r\n"), + ) { assertThat(it).contains("private static Int64 missing(Int64 arg1) {\r\n TODO();\r\n") } + } + + @Test + fun `class owner and multiple returns preserve their written contract`() { + query( + """ + module Missing { + class Box { + (Int, String) read(Int value, String label) { + return §missing(value, label, value); + } + } + } + """.trimIndent(), + ) { assertThat(it).contains(" private (Int64, String) missing(Int64 arg1, String arg2, Int64 arg3)") } + } + + @Test + fun `literal types come from the compiler without exposing numeric literal internals`() { + query( + """ + module Missing { + void run() { + §missing("hello", 'x', Int8:7); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private void missing(String arg1, Char arg2, Int8 arg3)") } + } + + @ParameterizedTest + @ValueSource(strings = ["Int other = value;", "var other = value;", "val other = value;", "Int other; other = value;"]) + fun `compiler typed local arguments retain their declaration binding`(local: String) { + query( + """ + module Missing { + Int read(Int value) { + $local + return §missing(other); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)") } + } + + @ParameterizedTest + @CsvSource(value = ["Int;Int64", "String;String", "List;List", "(Int | String);(Int64 | String)"], delimiter = ';') + fun `explicit initializer result supplies the method return type`( + type: String, + rendered: String, + ) { + query( + """ + module Missing { + void read($type value) { + $type result = §missing(value); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private $rendered missing($rendered arg1)") } + } + + @Test + fun `typed initializer and inferred local arguments compose in a static class method`() { + query( + """ + module Missing { + class Box { + static String read(Int value) { + var other = value; + String result = §missing(other, other, "hello"); + return result; + } + } + } + """.trimIndent(), + ) { assertThat(it).contains("private static String missing(Int64 arg1, Int64 arg2, String arg3)") } + } + + @Test + fun `local arguments in an enclosing block keep their compiler type`() { + query( + """ + module Missing { + Int read(Int value) { + List values = [value]; + if (value > 0) { + return §missing(values); + } + return value; + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing(List arg1)") } + } + + @ParameterizedTest + @ValueSource( + strings = [ + "return §missing(other); Int other = value;", + "if (value > 0) { Int other = value; } return §missing(other);", + "Int other; return §missing(other);", + "var other = unknown(); return §missing(other);", + "var other = §missing(other); return other;", + "var result = §missing(value); return result;", + "val result = §missing(value); return result;", + "Int result = 1 + §missing(value); return result;", + "Int result = value; result = §missing(value); return result;", + ], + ) + fun `unproven local scopes and initializer types remain refusals`(body: String) { + refused( + """ + module Missing { + Int read(Int value) { + $body + } + } + """.trimIndent(), + ) + } + + @Test + fun `a broad selection offers one repair for repeated calls with matching signatures`() { + query( + """ + module Missing { + Int read(Int value) { + return §missing(value); + } + Int other(Int value) { + return missing(value); + } + } + """.trimIndent(), + wholeFile = true, + ) { assertThat(it).contains("private Int64 missing(Int64 arg1)") } + } + + @ParameterizedTest + @CsvSource( + value = [ + "True;True arg1", "Null;Null arg1", "value + 1;Int64 arg1", "value = value;Int64 value", + "() -> value;Function, Tuple> arg1", "[value];immutable Array arg1", + ], + delimiter = ';', + ) + fun `compiler established argument types replace earlier conservative refusals`( + argument: String, + parameter: String, + ) { + query( + """ + module Missing { + Int read(Int value) { + return §missing($argument); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Int64 missing($parameter)", "return missing($argument);") } + } + + @ParameterizedTest + @ValueSource(strings = ["1", "1.5"]) + fun `unproven argument types do not invent a signature`(argument: String) { + refused( + """ + module Missing { + Int read(Int value) { + return §missing($argument); + } + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource( + strings = [ + "return 1 + §missing(value);", + "return §missing(value);", + ], + ) + fun `unsupported receiver argument and expected type contexts remain refusals`(body: String) { + refused( + """ + module Missing { + Int read(Int value) { + $body + } + } + """.trimIndent(), + ) + } + + @Test + fun `existing overload is not mistaken for an absent method`() { + refused( + """ + module Missing { + Int missing(Int value) = value; + Int read(String text) { + return §missing(text); + } + } + """.trimIndent(), + ) + } + + @ParameterizedTest + @ValueSource(strings = ["Object", "Const", "Hashable"]) + fun `method formals and constraints are declared by the generated sibling`(bound: String) { + query( + """ + module Missing { + Value read(Value value) { + return §missing(value); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private Value missing(Value arg1)") } + } + + @Test + fun `conditional return context preserves the conditional signature`() { + query( + """ + module Missing { + conditional Int read(Int value) { + return §missing(value); + } + } + """.trimIndent(), + ) { assertThat(it).contains("private conditional Int64 missing(Int64 arg1)") } + } + + @Test + fun `lambda cannot acquire a method with its enclosing method return type`() { + refused( + """ + module Missing { + Int read(Int value) { + function Int () compute = () -> { return §missing(value); }; + return compute(); + } + } + """.trimIndent(), + ) + } + + @Test + fun `a partial repair cannot hide an unrelated broken source graph`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Unknown value; }") + refused( + """ + module Missing { + void run() { + §missing(); + } + } + """.trimIndent(), + ) + } + + @Test + fun `existing calls and overload selection survive the addition`() { + query( + """ + module Missing { + Int keep(Int value) = value; + Int keep(String value) = value.size; + Int read(Int value) { + Int previous = keep(value); + return §missing(value); + } + } + """.trimIndent(), + ) { assertThat(it).contains("Int previous = keep(value);", "private Int64 missing(Int64 arg1)") } + } + + private fun refused(marked: String) = query(marked, expected = false) { error("Unexpected method creation") } + + private fun query( + marked: String, + expected: Boolean = true, + wholeFile: Boolean = false, + check: (String) -> Unit, + ) { + CompilerTestSupport.configure() + val offset = marked.indexOf('§') + val text = marked.replace("§", "") + val file = + directory + .resolve("Missing.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val diagnostics = adapter.compile(uri, text).diagnostics + assertThat(diagnostics).isNotEmpty() + assertThat(diagnostics).noneMatch { it.code == "EMB-5" } + val at = XdkRename.position(text, offset) + val end = if (wholeFile) XdkRename.position(text, text.length) else at + val action = + adapter.getCodeActions(uri, Range(at, end), diagnostics).singleOrNull { + it.title == + "Create private method 'missing'" + } + if (!expected) { + assertThat(action).isNull() + return + } + assertThat(action).describedAs("Missing method action for %s%nCompiler diagnostics: %s", marked, diagnostics).isNotNull() + assertThat(action!!.kind).isEqualTo(CodeAction.CodeActionKind.QUICKFIX) + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys).containsExactly(uri) + val changed = + edit.changes + .getValue(uri) + .sortedByDescending { it.range.start.line * text.length + it.range.start.column } + .fold(text) { value, change -> + fun at(position: Position) = + requireNotNull(XdkRename.offset(text, SemanticModel.Position(position.line, position.column))) + value.replaceRange(at(change.range.start), at(change.range.end), change.newText) + } + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(adapter.getCodeActions(uri, Range(at, at), emptyList()).filter { it.title == action.title }).isEmpty() + assertThat(file.readText()).isEqualTo(text) + check(changed) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt new file mode 100644 index 00000000000..72feba196ed --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt @@ -0,0 +1,284 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSources +import org.xvm.tool.ModuleInfo +import java.io.File +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class XdkModuleSessionTest { + @Test + fun `structure warnings belong to a closed member declaration and follow its overlay`() { + CompilerTestSupport.configure() + val root = directory.resolve("Project.x").toFile().canonicalFile + root.writeText("module Project { class Base { @Atomic Int x = 1; } }") + val member = directory.resolve("Project/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + val text = "class Child extends Base { @Atomic @Override Int x = 2; }" + member.writeText(text) + XdkAdapter().use { adapter -> + val warning = + adapter.compile(root.toURI().toString(), root.readText()).diagnostics.single { + it.code == "VERIFY-75" + } + assertThat(warning.location.uri).isEqualTo(member.toURI().toString()) + assertThat(warning.location.startColumn).isEqualTo(text.indexOf("x = 2")) + assertThat(adapter.getCachedResult(root.toURI().toString())!!.diagnostics).isEmpty() + val overlay = "// shifted\n$text" + val shifted = + adapter.compile(member.toURI().toString(), overlay).diagnostics.single { + it.code == "VERIFY-75" + } + assertThat(shifted.location.startLine).isEqualTo(1) + assertThat( + adapter + .compile(member.toURI().toString(), text.replace("@Atomic ", "")) + .diagnostics, + ).isEmpty() + } + } + + @TempDir lateinit var directory: Path + + @Test + fun `source snapshots preserve implicit packages containing only resources`() { + val (root, _) = fixture() + val resources = directory.resolve("Project/spare").toFile() + resources.mkdirs() + File(resources, "data.txt").writeText("resource") + CompilerTestSupport.configure() + val errors = ErrorList() + val input = XdkSources.capture(root, emptyMap()) { false } + val result = EmbeddingSupport.instance().compileModule(input, null, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + assertThat(result.module().getChild("spare")).isNotNull() + } + + @Test + fun `member edits invalidate the whole module and preserve source URI aliases`() { + val (root, member) = fixture() + val rootUri = root.toURI().toString() + val alias = member.parentFile.toURI().toString() + "../Project/Child.x" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(rootUri, root.readText()).success).isTrue() + val errors = + adapter.compile( + alias, + member + .readText() + .replace("String answer()", "MissingType missing; String answer()"), + ) + assertThat(errors.success).isFalse() + assertThat(errors.documentUris).containsExactlyInAnyOrder(rootUri, alias) + assertThat( + errors.diagnostics + .single { it.code == "COMPILER-38" } + .location.uri, + ).isEqualTo(alias) + val fixed = adapter.compile(alias, member.readText()) + assertThat(fixed.diagnostics).isEmpty() + assertThat( + adapter + .getCachedResult(rootUri) + ?.symbols + ?.single() + ?.name, + ).isEqualTo("Project") + assertThat( + adapter + .getCachedResult(alias) + ?.symbols + ?.single() + ?.name, + ).isEqualTo("Child") + adapter.closeDocument(alias) + assertThat(adapter.compile(rootUri, root.readText()).diagnostics).isEmpty() + adapter.closeDocument(rootUri) + assertThat(adapter.findWorkspaceSymbols("")).isEmpty() + } + } + + @Test + fun `new unsaved members and implicit packages participate without writing files`() { + val (root, _) = fixture() + val added = directory.resolve("Project/pkg/Added.x").toFile() + XdkAdapter().use { adapter -> + adapter.compile(root.toURI().toString(), root.readText()) + val result = + adapter.compile(added.toURI().toString(), "class Added extends Base {}") + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + assertThat(result.documentUris).contains(added.toURI().toString()) + assertThat( + adapter + .findWorkspaceSymbols("Added") + .single() + .location.uri, + ).isEqualTo(added.toURI().toString()) + assertThat(added.exists()).isFalse() + assertThat(added.parentFile.exists()).isFalse() + } + } + + @Test + fun `definitions and references span module members while highlights remain local`() { + val (root, member) = fixture() + val rootUri = root.toURI().toString() + val memberUri = member.toURI().toString() + XdkAdapter().use { adapter -> + val result = adapter.compile(rootUri, root.readText()) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + val call = root.readLines()[2].indexOf("answer") + assertThat(adapter.findDefinition(rootUri, 2, call)?.uri).isEqualTo(memberUri) + val echo = root.readLines()[1].indexOf("echo") + val references = adapter.findReferences(rootUri, 1, echo, true) + assertThat(references).hasSize(3) + assertThat(references.map { it.uri }.toSet()) + .containsExactlyInAnyOrder(rootUri, memberUri) + assertThat(adapter.getDocumentHighlights(rootUri, 1, echo)).hasSize(2) + assertThat(adapter.findWorkspaceSymbols("Child")).hasSize(1) + } + } + + @Test + fun `hierarchy follows generic extends and interface implements edges across files`() { + val (root, member) = fixture() + val source = + root + .readText() + .replace( + "class Base", + "interface Named {} class Base implements Named", + ) + XdkAdapter().use { adapter -> + val uri = root.toURI().toString() + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val base = + adapter.prepareTypeHierarchy(uri, 1, source.lines()[1].indexOf("Base")).single() + val child = adapter.getSubtypes(base).single() + assertThat(child.name).isEqualTo("Child") + assertThat(child.uri).isEqualTo(member.toURI().toString()) + val parent = adapter.getSupertypes(child).single() + assertThat(parent.name).isEqualTo("Base") + assertThat(parent.detail).contains("String") + val implemented = adapter.getSupertypes(base).single() + assertThat(implemented.name).isEqualTo("Named") + assertThat(implemented.kind) + .isEqualTo(org.xvm.lsp.model.SymbolInfo.SymbolKind.INTERFACE) + assertThat(adapter.getSubtypes(implemented).single().name).isEqualTo("Base") + adapter.compile(uri, source) + assertThat(adapter.getSubtypes(base)).isEmpty() + } + } + + @Test + fun `broken member parsing clears module navigation and correction restores it`() { + val (root, member) = fixture() + XdkAdapter().use { adapter -> + val uri = root.toURI().toString() + val call = root.readLines()[2].indexOf("answer") + assertThat(adapter.compile(uri, root.readText()).success).isTrue() + assertThat(adapter.findDefinition(uri, 2, call)).isNotNull() + assertThat(adapter.compile(member.toURI().toString(), "class Child {").success) + .isFalse() + assertThat(adapter.findDefinition(uri, 2, call)).isNull() + assertThat(adapter.findWorkspaceSymbols("answer")).isEmpty() + assertThat(adapter.findWorkspaceSymbols("Child")).hasSize(1) + assertThat(adapter.compile(member.toURI().toString(), member.readText()).success) + .isTrue() + assertThat(adapter.findDefinition(uri, 2, call)?.uri) + .isEqualTo(member.toURI().toString()) + } + } + + @Test + fun `cancellation while loading a member does not invent a parser fatal`() { + val (root, member) = fixture() + val cancelled = AtomicBoolean() + val sources = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray { + val text = super.readSource(file) + if (file == member) cancelled.set(true) + return text + } + } + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule(sources, null, ErrorListener.cancellable(errors, cancelled::get)) + assertThat(cancelled.get()).isTrue() + assertThat(compilation.succeeded()).isFalse() + assertThat(compilation.parsed()).isNull() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `editing a sibling cancels running module work and installs only the latest inputs`() { + val (root, member) = fixture() + CompilerTestSupport.configure() + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val first = AtomicBoolean(true) + val embedding = EmbeddingSupport.instance() + XdkAdapter( + { source, errors -> embedding.compileModule(source, null, errors) }, + { sources, errors -> + if (first.getAndSet(false)) { + started.countDown() + check(release.await(10, SECONDS)) + } + embedding.compileModule(sources, null, errors) + }, + ).use { adapter -> + val old = adapter.compileAsync(root.toURI().toString(), root.readText()) + try { + check(started.await(10, SECONDS)) + val latest = + adapter.compileAsync( + member.toURI().toString(), + member + .readText() + .replace("String answer()", "MissingType missing; String answer()"), + ) + assertThat(old.isCancelled).isTrue() + assertThat(adapter.getCachedResult(root.toURI().toString())).isNull() + release.countDown() + assertThat(latest.get(20, SECONDS).diagnostics).anyMatch { + it.code == "COMPILER-38" + } + assertThat(adapter.getCachedResult(member.toURI().toString())?.diagnostics) + .anyMatch { it.code == "COMPILER-38" } + } finally { + release.countDown() + } + } + } + + private fun fixture(): Pair { + directory = directory.toRealPath() + val root = directory.resolve("Project.x").toFile() + root.writeText( + """ + module Project { + class Base { Element echo(Element value) = value; } + String run() { Child child = new Child(); return child.answer() + child.echo("root"); } + } + """.trimIndent(), + ) + val member = directory.resolve("Project/Child.x").toFile() + member.parentFile.mkdirs() + member.writeText("class Child extends Base { String answer() = echo(\"member\"); }") + return root to member + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt new file mode 100644 index 00000000000..9581d9e32b6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt @@ -0,0 +1,212 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.CompiledSemantics +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.compiledSemantics +import java.nio.file.Files +import java.nio.file.Path + +class XdkMonikerTest { + @TempDir lateinit var directory: Path + + @Test + fun `unchanged recompilation and checkout relocation preserve exports while versions and overloads differ`() { + val first = compile(LIBRARY, "file:///first/Library.x") + val second = compile(LIBRARY, "file:///second/Library.x") + val changed = compile(LIBRARY.replace("= n;", "= n + 1;"), "file:///other/Library.x") + val exports = + first.models.single().symbols.filter { it.name == "pick" }.map { symbol -> + val at = requireNotNull(symbol.declaration).start + first.models + .single() + .monikersAt(at.line, at.column) + .single() + } + assertThat(exports).hasSize(2).doesNotHaveDuplicates().allMatch { it.kind == SymbolMoniker.Kind.EXPORT } + assertThat(moniker(first, LIBRARY, "pick(Int")) + .isEqualTo(moniker(second, LIBRARY, "pick(Int")) + .isNotEqualTo(moniker(changed, LIBRARY, "pick(Int")) + assertThat(first.artifact!!.bytes()).isNotEqualTo(second.artifact!!.bytes()) + assertThat(monikers(first.models.single(), LIBRARY, "T>")).isEmpty() + } + + @Test + fun `source indexed and binary only imports match exact exported overloads`() { + val library = compile(LIBRARY) + val source = library.artifact!! + val binary = XdkDependency.fromBinary(source.bytes()) + assertThat(source.revision).isNotEqualTo(binary.revision) + listOf(source, binary).forEach { dependency -> + val consumer = compile(CONSUMER, dependencies = listOf(dependency)) + listOf( + "pick(Int" to "pick(1)", + "pick(String" to "pick(\"x\")", + "Box" to "Box", + "echo(T" to "echo(1)", + "echo(T" to "echo(\"x\")", + ).forEach { (declaration, use) -> + val exported = moniker(library, LIBRARY, declaration) + assertThat(moniker(consumer, CONSUMER, use)) + .isEqualTo(exported.copy(kind = SymbolMoniker.Kind.IMPORT)) + } + } + } + + @Test + fun `private declarations stay local and registers unresolved names and failed attempts have no artifact identity`() { + val text = + """ + module App { + private Int hidden = 1; + private class Hidden { Int value = 2; } + Int read(Int argument) { + class Nested { Int answer = 1; } + Nested nested = new Nested(); + Int local = argument + nested.answer; + return local + hidden; + } + } + """.trimIndent() + val result = compile(text) + assertThat(moniker(result, text, "hidden =").kind).isEqualTo(SymbolMoniker.Kind.LOCAL) + assertThat(moniker(result, text, "value =").kind).isEqualTo(SymbolMoniker.Kind.LOCAL) + assertThat(moniker(result, text, "Nested {").kind).isEqualTo(SymbolMoniker.Kind.LOCAL) + assertThat(moniker(result, text, "answer =").kind).isEqualTo(SymbolMoniker.Kind.LOCAL) + listOf("argument)", "local =", "local +").forEach { marker -> + assertThat(monikers(result.models.single(), text, marker)).isEmpty() + } + XdkAdapter().use { adapter -> + val uri = "file:///App.x" + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val failed = text.replace("return local + hidden;", "return missing;") + assertThat(adapter.compile(uri, failed).success).isFalse() + val at = position(text, "hidden =") + assertThat(adapter.findMonikers(uri, at.line, at.column)).isEmpty() + } + } + + @Test + fun `dependency replacement retires old IDs and binary source attachment does not change portable identity`() { + val source = compile(LIBRARY).artifact!! + XdkAdapter().use { adapter -> + val uri = "file:///Consumer.x" + val at = position(CONSUMER, "pick(1)") + adapter.replaceDependencies(listOf(source)) + assertThat(adapter.compile(uri, CONSUMER).diagnostics).isEmpty() + val first = adapter.findMonikers(uri, at.line, at.column).single() + adapter.replaceDependencies(listOf(XdkDependency.fromBinary(source.bytes()))) + assertThat(adapter.findMonikers(uri, at.line, at.column)).isEmpty() + assertThat(adapter.compile(uri, CONSUMER).diagnostics).isEmpty() + assertThat(adapter.findMonikers(uri, at.line, at.column)).containsExactly(first) + adapter.replaceDependencies(listOf(compile(LIBRARY.replace("= n;", "= n + 1;")).artifact!!)) + assertThat(adapter.findMonikers(uri, at.line, at.column)).isEmpty() + assertThat(adapter.compile(uri, CONSUMER).diagnostics).isEmpty() + assertThat(adapter.findMonikers(uri, at.line, at.column).single()).isNotEqualTo(first) + } + } + + @Test + fun `closed graph views preserve import and export kinds and expire after replacement`() { + val library = directory.resolve("Library.x").also { Files.writeString(it, LIBRARY) } + val consumer = directory.resolve("Consumer.x").also { Files.writeString(it, CONSUMER) } + val graph = + listOf( + XdkSourceModule("Library", library.toUri().toString()), + XdkSourceModule("Consumer", consumer.toUri().toString(), setOf("Library")), + ) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(graph) + val declaration = position(LIBRARY, "pick(Int") + val use = position(CONSUMER, "pick(1)") + val exported = adapter.findMonikers(library.toUri().toString(), declaration.line, declaration.column).single() + assertThat(exported.kind).isEqualTo(SymbolMoniker.Kind.EXPORT) + assertThat(adapter.findMonikers(consumer.toUri().toString(), use.line, use.column)) + .containsExactly(exported.copy(kind = SymbolMoniker.Kind.IMPORT)) + Files.writeString(library, LIBRARY.replace("= n;", "= n + 1;")) + assertThat(adapter.findMonikers(consumer.toUri().toString(), use.line, use.column).single().identifier) + .isNotEqualTo(exported.identifier) + adapter.replaceSourceModules(emptyList()) + assertThat(adapter.findMonikers(consumer.toUri().toString(), use.line, use.column)).isEmpty() + } + } + + @Test + fun `bundled binary declarations provide stable imported identities without retaining a compiler pool`() { + val text = "module App { String text = \"hello\"; }" + val first = compile(text) + val second = compile(text) + assertThat(moniker(first, text, "String")) + .isEqualTo(moniker(second, text, "String")) + .matches { it.kind == SymbolMoniker.Kind.IMPORT } + } + + private fun compile( + text: String, + uri: String = "file:///Library.x", + dependencies: List = emptyList(), + ): CompiledSemantics { + CompilerTestSupport.configure() + val errors = ErrorList() + val inputs = XdkDependencies(dependencies).open() + val result = EmbeddingSupport.instance().compileModule(Source(text, uri), inputs.repository, errors) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.compiledSemantics(inputs, errors) + } + + private fun moniker( + result: CompiledSemantics, + text: String, + marker: String, + ) = monikers(result.models.single(), text, marker).single() + + private fun monikers( + model: SemanticModel, + text: String, + marker: String, + ): List { + val at = position(text, marker) + return model.monikersAt(at.line, at.column) + } + + private fun position( + text: String, + marker: String, + ): Position { + val offset = text.indexOf(marker) + require(offset >= 0) { marker } + return Position(text.take(offset).count { it == '\n' }, offset - text.lastIndexOf('\n', offset) - 1) + } + + companion object { + private val LIBRARY = + """ + module Library { + static Int pick(Int n) = n; + static String pick(String s) = s; + static T echo(T value) = value; + class Box {} + } + """.trimIndent() + private val CONSUMER = + """ + module Consumer { + package lib import Library; + Int number() = lib.pick(1); + String text() = lib.pick("x"); + lib.Box? box = Null; + Int echoed() = lib.echo(1); + String echoedText() = lib.echo("x"); + } + """.trimIndent() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt new file mode 100644 index 00000000000..011ba9c5017 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt @@ -0,0 +1,30 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkMoveInputs +import java.nio.file.Files +import java.nio.file.Path + +class XdkMoveInputsTest { + @TempDir lateinit var directory: Path + + @Test + fun `move snapshots detect changed bytes and newly introduced resource members`() { + val file = Files.writeString(directory.resolve("data.txt"), "before") + val captured = requireNotNull(XdkMoveInputs.capture(setOf(directory.toFile())) { false }) + assertThat(captured.isCurrent { false }).isTrue() + Files.writeString(file, "after") + assertThat(captured.isCurrent { false }).isFalse() + val next = requireNotNull(XdkMoveInputs.capture(setOf(directory.toFile())) { false }) + Files.writeString(directory.resolve("extra.txt"), "added") + assertThat(next.isCurrent { false }).isFalse() + } + + @Test + fun `incoming symbolic links cannot acquire captured ownership`() { + Files.createSymbolicLink(directory.resolve("link"), directory.resolve("missing")) + assertThat(XdkMoveInputs.capture(setOf(directory.toFile())) { false }).isNull() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt new file mode 100644 index 00000000000..8ae33462167 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt @@ -0,0 +1,233 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkNavigationIndex +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.XdkWorkspaceNavigation +import java.lang.ref.WeakReference +import java.nio.file.Files +import java.nio.file.Path +import java.time.Duration +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.atomic.AtomicInteger +import kotlin.time.TimeSource + +class XdkNavigationIndexTest { + @TempDir lateinit var directory: Path + + private val compiled = ConcurrentHashMap() + + @ParameterizedTest + @ValueSource(ints = [32, 128]) + fun `navigation reuses independent roots and rebuilds only an edited dependency closure`(count: Int) { + val last = "Node${count - 1}" + val roots = (0 until count).map { source("Node$it", "module Node$it { class Box {} }") } + val consumer = + source( + "Consumer", + """ + module Consumer { + package lib import Node0; + class Child extends lib.Box {} + } + """.trimIndent(), + setOf("Node0"), + ) + val graph = roots + consumer + adapter().use { adapter -> + adapter.replaceSourceModules(graph) + val cold = TimeSource.Monotonic.markNow() + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(count) + val coldTime = cold.elapsedNow() + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(count + 1) + val warm = TimeSource.Monotonic.markNow() + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(count) + val warmTime = warm.elapsedNow() + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(count + 1) + val hierarchy = adapter.prepareTypeHierarchy(roots.first().uri, 0, 21).single() + assertThat(adapter.getSubtypes(hierarchy).single().uri).isEqualTo(consumer.uri) + + source("Node0", "module Node0 { class Box { Int added = 1; } }") + assertThat(adapter.findWorkspaceSymbols("added")).hasSize(1) + assertThat(compiled.getValue("Node0").get()).isEqualTo(2) + assertThat(compiled.getValue("Consumer").get()).isEqualTo(2) + assertThat(compiled.filterKeys { it != "Node0" && it != "Consumer" }.values).allMatch { it.get() == 1 } + assertThat(adapter.getSubtypes(hierarchy)).isEmpty() + + // Broken independent roots allow partial navigation but never complete references. + source(last, "module $last { Missing broken; }") + assertThat(adapter.findWorkspaceSymbols("Child")).hasSize(1) + assertThat(adapter.findReferences(roots.first().uri, 0, 21, true)).isEmpty() + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(count - 1) + assertThat(compiled.getValue(last).get()).isEqualTo(2) + source(last, "module $last { class Box {} }") + assertThat(adapter.findReferences(roots.first().uri, 0, 21, true)).hasSize(2) + assertThat(compiled.getValue(last).get()).isEqualTo(3) + + adapter.replaceSourceModules(graph.filter { it.name != last }) + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(count - 1) + adapter.replaceSourceModules(graph) + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(count) + assertThat(compiled.getValue(last).get()).isEqualTo(4) + println( + "Navigation graph: roots=${count + 1}, cold=$coldTime, warm=$warmTime, initialCompiles=${count + 1}, unchangedCompiles=0, editedClosureCompiles=2", + ) + } + } + + @Test + fun `first graph navigation reuses editor builds and unsaved source inputs`() { + val text = "module Library { class Box {} }" + val library = source("Library", text) + val consumerText = "module Consumer { package lib import Library; class Child extends lib.Box {} }" + val consumer = source("Consumer", consumerText, setOf("Library")) + val independent = source("Independent", "module Independent { class Unrelated {} }") + adapter().use { adapter -> + adapter.replaceSourceModules(listOf(library, consumer, independent)) + // LSP clients use file:/// while File.toURI (the graph key) renders file:/. + val editorUri = + directory + .resolve("Consumer.x") + .toRealPath() + .toUri() + .toString() + assertThat(editorUri).isNotEqualTo(consumer.uri) + assertThat(adapter.compile(editorUri, consumerText).diagnostics).isEmpty() + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(2) + assertThat(adapter.findReferences(library.uri, 0, text.indexOf("Box"), true)).hasSize(2) + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(3) + val changed = "module Library { class Box { Int added = 1; } }" + assertThat(adapter.compile(library.uri, changed).diagnostics).isEmpty() + assertThat(adapter.findWorkspaceSymbols("added")).hasSize(1) + assertThat(compiled.getValue("Library").get()).isEqualTo(2) + assertThat(compiled.getValue("Consumer").get()).isEqualTo(2) + assertThat(compiled.getValue("Independent").get()).isEqualTo(1) + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(text) + } + } + + @Test + fun `graph diagnostics seed navigation including failed roots without duplicate compilation`() { + val library = source("Library", "module Library { class Box {} }") + val consumer = + source("Consumer", "module Consumer { package lib import Library; class Child extends lib.Box {} }", setOf("Library")) + val broken = source("Broken", "module Broken { Missing value; }") + adapter().use { adapter -> + adapter.replaceSourceModules(listOf(library, consumer, broken)) + assertThat(adapter.workspaceDiagnosticsAsync().join()).hasSize(3).anyMatch { !it.success } + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(3) + assertThat(adapter.findWorkspaceSymbols("Child")).hasSize(1) + assertThat(adapter.findReferences(library.uri, 0, 23, true)).isEmpty() + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(3) + source("Broken", "module Broken {}") + assertThat(adapter.workspaceDiagnosticsAsync().join()).allMatch { it.success } + assertThat(adapter.findReferences(library.uri, 0, 23, true)).hasSize(2) + assertThat(compiled.values.sumOf { it.get() }).isEqualTo(4) + } + } + + @Test + fun `resource replacement with unchanged timestamp invalidates only dependent navigation`() { + val resources = directory.resolve("assets").toFile().apply { mkdirs() } + val resource = resources.resolve("data.txt").apply { writeText("first") } + val library = + source("Library", "module Library { static String text() = $./data.txt; }").let { + XdkSourceModule(it.name, it.uri, resourceRoots = listOf(resources.toURI().toString())) + } + val consumer = source("Consumer", "module Consumer { package lib import Library; String run() = lib.text(); }", setOf("Library")) + val independent = source("Independent", "module Independent { class Unrelated {} }") + adapter().use { adapter -> + adapter.replaceSourceModules(listOf(library, consumer, independent)) + assertThat(adapter.findWorkspaceSymbols("run")).hasSize(1) + val stamp = Files.getLastModifiedTime(resource.toPath()) + resource.writeText("other") + Files.setLastModifiedTime(resource.toPath(), stamp) + assertThat(adapter.findWorkspaceSymbols("run")).hasSize(1) + assertThat(compiled.getValue("Library").get()).isEqualTo(2) + assertThat(compiled.getValue("Consumer").get()).isEqualTo(2) + assertThat(compiled.getValue("Independent").get()).isEqualTo(1) + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `retained graph navigation releases all compilation pools and ASTs`(diagnosticsFirst: Boolean) { + CompilerTestSupport.configure() + val observed = mutableListOf>() + val roots = (0 until 32).map { source("Node$it", "module Node$it { class Box {} }") } + XdkAdapter( + { source, repository, errors -> EmbeddingSupport.instance().compileModule(source, repository, errors) }, + { sources, repository, errors -> + assertThat(repository?.moduleNames).isEmpty() + if (observed.size == 48) { + System.gc() + assertThat(observed.count { it.get() != null }) + .describedAs("earlier compiler attempts retained while the graph is still compiling") + .isZero() + } + EmbeddingSupport.instance().compileModule(sources, repository, errors).also { result -> + observed += WeakReference(result) + result.pool()?.let { observed += WeakReference(it) } + result.sourceTrees().forEach { observed += WeakReference(it) } + } + }, + { _, _, _, _, _ -> error("No cursor analysis") }, + ).use { adapter -> + adapter.replaceSourceModules(roots) + if (diagnosticsFirst) assertThat(adapter.workspaceDiagnosticsAsync().join()).allMatch { it.success } + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(32) + assertThat(observed).hasSizeGreaterThanOrEqualTo(96) + await().atMost(Duration.ofSeconds(20)).untilAsserted { + System.gc() + assertThat(observed.count { it.get() != null }).describedAs("compiler objects retained by detached index").isZero() + } + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(32) + assertThat(observed).hasSize(96) + } + } + + @Test + fun `retirement and close reject publication from an older index generation`() { + val index = XdkNavigationIndex() + val navigation = mapOf("graph" to XdkWorkspaceNavigation(emptyMap(), "graph", true, emptyMap())) + val beforeEdit = index.snapshot() + index.retire(emptySet()) + assertThat(index.publish(beforeEdit, emptyMap(), navigation)).isFalse() + val beforeClose = index.snapshot() + index.clear() + assertThat(index.publish(beforeClose, emptyMap(), navigation)).isFalse() + assertThat(index.snapshot().navigation).isEmpty() + assertThat(index.publish(index.snapshot(), emptyMap(), navigation)).isTrue() + index.retire(emptySet()) + assertThat(index.snapshot().navigation).isEmpty() + } + + private fun adapter(): XdkAdapter { + CompilerTestSupport.configure() + return XdkAdapter( + { source, repository, errors -> EmbeddingSupport.instance().compileModule(source, repository, errors) }, + { sources, repository, errors -> + compiled.computeIfAbsent(sources.sourceFile.nameWithoutExtension) { AtomicInteger() }.incrementAndGet() + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { _, _, _, _, _ -> error("Navigation must not invoke cursor analysis") }, + ) + } + + private fun source( + name: String, + text: String, + dependencies: Set = emptySet(), + ): XdkSourceModule = + directory.resolve("$name.x").toFile().let { + it.writeText(text) + XdkSourceModule(name, it.canonicalFile.toURI().toString(), dependencies) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt new file mode 100644 index 00000000000..30d36efcdd9 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt @@ -0,0 +1,521 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.model.Location + +class XdkNavigationTest { + @Test + fun `a local definition points to its declaration name and excludes it from ordinary references`() { + val source = + """ + module Local { + Int read() { + Int /*declaration*/count; + /*write*/count = 1; + return /*read*/count; + } + } + """.trimIndent() + withSource(source) { adapter -> + val read = position(source, "read") + val declaration = span(source, "declaration", "count") + assertThat(adapter.findDefinition(URI, read.line, read.column)).isEqualTo(declaration) + assertThat(adapter.findReferences(URI, read.line, read.column, false)) + .containsExactly(span(source, "write", "count"), span(source, "read", "count")) + assertThat(adapter.findReferences(URI, read.line, read.column, true)) + .containsExactly( + declaration, + span(source, "write", "count"), + span(source, "read", "count"), + ) + val atDeclaration = position(source, "declaration") + assertThat(adapter.findReferences(URI, atDeclaration.line, atDeclaration.column, false)) + .containsExactly(span(source, "write", "count"), span(source, "read", "count")) + } + } + + @Test + fun `sibling scopes can use the same spelling without sharing references or highlights`() { + val source = + """ + module Siblings { + Int choose(Boolean flag) { + if (flag) { + Int /*firstDeclaration*/value = 1; + return /*firstRead*/value; + } else { + Int /*secondDeclaration*/value = 2; + return /*secondRead*/value; + } + } + } + """.trimIndent() + withSource(source) { adapter -> + for (prefix in listOf("first", "second")) { + val read = position(source, "${prefix}Read") + val declaration = span(source, "${prefix}Declaration", "value") + val use = span(source, "${prefix}Read", "value") + assertThat(adapter.findDefinition(URI, read.line, read.column)) + .isEqualTo(declaration) + assertThat(adapter.findReferences(URI, read.line, read.column, true)) + .containsExactly(declaration, use) + assertThat( + adapter.getDocumentHighlights(URI, read.line, read.column).map { it.range }, + ).containsExactly(range(declaration), range(use)) + } + } + } + + @Test + fun `identical register slots in different methods do not share a declaration`() { + val source = + """ + module Methods { + Int first() { Int /*firstDeclaration*/value = 1; return /*firstRead*/value; } + Int second() { Int /*secondDeclaration*/value = 2; return /*secondRead*/value; } + } + """.trimIndent() + withSource(source) { adapter -> + val read = position(source, "secondRead") + assertThat(adapter.findDefinition(URI, read.line, read.column)) + .isEqualTo(span(source, "secondDeclaration", "value")) + assertThat(adapter.findReferences(URI, read.line, read.column, true)) + .containsExactly( + span(source, "secondDeclaration", "value"), + span(source, "secondRead", "value"), + ) + assertThat(adapter.getDocumentHighlights(URI, read.line, read.column).map { it.range }) + .containsExactly( + range(span(source, "secondDeclaration", "value")), + range(span(source, "secondRead", "value")), + ) + } + } + + @Test + fun `narrowed and original uses lead to the same local declaration`() { + val source = + """ + module Narrowing { + String text(Object input) { + Object /*declaration*/value = input; + if (/*test*/value.is(String)) { + return /*narrowed*/value; + } + return /*original*/value.toString(); + } + } + """.trimIndent() + withSource(source) { adapter -> + val narrowed = position(source, "narrowed") + assertThat(adapter.findDefinition(URI, narrowed.line, narrowed.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, narrowed.line, narrowed.column, false)) + .containsExactly( + span(source, "test", "value"), + span(source, "narrowed", "value"), + span(source, "original", "value"), + ) + } + } + + @Test + fun `a local passed to a call resolves to the local rather than the called method`() { + val source = + """ + module Arguments { + Int twice(Int n) { return n + n; } + Int read() { + Int /*declaration*/value = 1; + return /*call*/twice(/*argument*/value) + twice(/*literal*/1); + } + } + """.trimIndent() + withSource(source) { adapter -> + val argument = position(source, "argument") + assertThat(adapter.findDefinition(URI, argument.line, argument.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, argument.line, argument.column, false)) + .containsExactly(span(source, "argument", "value")) + val call = position(source, "call") + assertThat(adapter.findDefinition(URI, call.line, call.column)?.startLine).isEqualTo(1) + val literal = position(source, "literal") + assertThat(adapter.findDefinition(URI, literal.line, literal.column)).isNull() + assertThat(adapter.findDefinition(URI, argument.line, argument.column + "value".length)) + .isNull() + } + } + + @Test + fun `a local shadowing a property highlights only the local`() { + val source = + """ + module Shadow { + class Holder { + Int value = 1; + Int read() { + Int /*declaration*/value = 2; + return /*local*/value + this.value; + } + } + } + """.trimIndent() + withSource(source) { adapter -> + val local = position(source, "local") + assertThat(adapter.findReferences(URI, local.line, local.column, false)) + .containsExactly(span(source, "local", "value")) + assertThat( + adapter.getDocumentHighlights(URI, local.line, local.column).map { it.range }, + ).containsExactly( + range(span(source, "declaration", "value")), + range(span(source, "local", "value")), + ) + } + } + + @Test + fun `an unresolved name does not borrow a declaration or highlights by spelling`() { + val source = + """ + module Unresolved { + Int good() { Int value = 1; return value; } + Int broken() { return /*unresolved*/value; } + } + """.trimIndent() + CompilerTestSupport.configure() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).success).isFalse() + val unresolved = position(source, "unresolved") + assertThat(adapter.findDefinition(URI, unresolved.line, unresolved.column)).isNull() + assertThat(adapter.findReferences(URI, unresolved.line, unresolved.column, true)) + .isEmpty() + assertThat(adapter.getDocumentHighlights(URI, unresolved.line, unresolved.column)) + .isEmpty() + } + } + + @Test + fun `member declarations and qualified calls use their identifier spans`() { + val source = + """ + module Members { + class /*typeDeclaration*/Box { + Int /*propertyDeclaration*/value = 1; + Int /*methodDeclaration*/read() { return /*propertyUse*/value; } + } + Int use() { + /*typeUse*/Box box = new /*construction*/Box(); + return /*call*/box.read(); + } + } + """.trimIndent() + withSource(source) { adapter -> + val method = position(source, "methodDeclaration") + val callStart = + position(source, "call").let { it.copy(column = it.column + "box.".length) } + val call = + Location( + URI, + callStart.line, + callStart.column, + callStart.line, + callStart.column + "read".length, + ) + val methodDeclaration = span(source, "methodDeclaration", "read") + assertThat(adapter.findDefinition(URI, callStart.line, callStart.column)) + .isEqualTo(methodDeclaration) + assertThat(adapter.findReferences(URI, method.line, method.column, false)) + .containsExactly(call) + assertThat( + adapter.getDocumentHighlights(URI, method.line, method.column).map { it.range }, + ).containsExactly(range(methodDeclaration), range(call)) + + val type = position(source, "typeDeclaration") + assertThat(adapter.findReferences(URI, type.line, type.column, false)) + .containsExactly( + span(source, "typeUse", "Box"), + span(source, "construction", "Box"), + ) + val property = position(source, "propertyDeclaration") + assertThat(adapter.findReferences(URI, property.line, property.column, false)) + .containsExactly(span(source, "propertyUse", "value")) + } + } + + @Test + fun `parameter definitions and declaration queries use the retained register identity`() { + val source = + """ + module Parameters { + Int first(Int value) { return value; } + Int second(Int /*declaration*/value) { return /*firstUse*/value + /*secondUse*/value; } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "firstUse") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, use.line, use.column, false)) + .containsExactly( + span(source, "firstUse", "value"), + span(source, "secondUse", "value"), + ) + val declaration = position(source, "declaration") + assertThat(adapter.findReferences(URI, declaration.line, declaration.column, true)) + .containsExactly( + span(source, "declaration", "value"), + span(source, "firstUse", "value"), + span(source, "secondUse", "value"), + ) + } + } + + @Test + fun `constructor generated properties point to their source parameter`() { + val source = + """ + module Constructed { + const Box(Int /*declaration*/value) { + Int read() { return /*use*/value; } + } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "use") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + val declaration = position(source, "declaration") + assertThat(adapter.findReferences(URI, declaration.line, declaration.column, true)) + .containsExactly(span(source, "declaration", "value"), span(source, "use", "value")) + } + } + + @Test + fun `a narrowed parameter still points to its declaration`() { + val source = + """ + module ParameterNarrowing { + String text(Object /*declaration*/value) { + if (value.is(String)) { return /*use*/value; } + return value.toString(); + } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "use") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + } + } + + @Test + fun `a captured local points to its enclosing source declaration`() { + val source = + """ + module Captures { + Int run() { + Int /*declaration*/value = 1; + function Int() fn = () -> /*capture*/value; + return fn(); + } + } + """.trimIndent() + withSource(source) { adapter -> + val capture = position(source, "capture") + assertThat(adapter.findDefinition(URI, capture.line, capture.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, capture.line, capture.column, true)) + .containsExactly( + span(source, "declaration", "value"), + span(source, "capture", "value"), + ) + } + } + + @Test + fun `generic and qualified type uses select only the resolved type name`() { + val source = + """ + module Types { + class /*outerDeclaration*/Outer { class /*declaration*/Nested {} } + void use(List values) {} + } + """.trimIndent() + withSource(source) { adapter -> + val qualifier = position(source, "qualified") + val inner = qualifier.copy(column = qualifier.column + "Outer.".length) + val declaration = span(source, "declaration", "Nested") + assertThat(adapter.findDefinition(URI, inner.line, inner.column)).isEqualTo(declaration) + assertThat(adapter.findDefinition(URI, qualifier.line, qualifier.column)) + .isEqualTo(span(source, "outerDeclaration", "Outer")) + val typeEnd = inner.column + "Nested".length + assertThat(adapter.findDefinition(URI, inner.line, typeEnd)).isNull() + assertThat(adapter.findReferences(URI, inner.line, inner.column, false)) + .containsExactly(Location(URI, inner.line, inner.column, inner.line, typeEnd)) + } + } + + @Test + fun `nested lambdas retain capture origins through both generated methods`() { + val source = + """ + module NestedCaptures { + Int run(Int /*declaration*/value) { + function Int() outer = () -> { + function Int() inner = () -> /*nested*/value; + return inner() + /*outer*/value; + }; + return outer() + /*direct*/value; + } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "nested") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, use.line, use.column, false)) + .containsExactly( + span(source, "nested", "value"), + span(source, "outer", "value"), + span(source, "direct", "value"), + ) + } + } + + @Test + fun `lambda parameters have source declarations for inferred and explicit parameter types`() { + for (parameter in listOf("", "Int ")) { + val source = + """ + module LambdaParameters { + Int run() { + function Int(Int) first = ($parameter/*firstDeclaration*/value) -> /*firstUse*/value; + function Int(Int) second = ($parameter/*secondDeclaration*/value) -> /*secondUse*/value; + return first(1) + second(2); + } + } + """.trimIndent() + withSource(source) { adapter -> + for (prefix in listOf("first", "second")) { + val use = position(source, "${prefix}Use") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "${prefix}Declaration", "value")) + assertThat(adapter.findReferences(URI, use.line, use.column, true)) + .containsExactly( + span(source, "${prefix}Declaration", "value"), + span(source, "${prefix}Use", "value"), + ) + } + } + } + } + + @Test + fun `mutable capture dereferencing preserves the source declaration`() { + val source = + """ + module MutableCapture { + Int run() { + @Volatile Int /*declaration*/value = 0; + function Int() increment = () -> ++/*capture*/value; + return increment() + /*direct*/value; + } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "capture") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.findReferences(URI, use.line, use.column, false)) + .containsExactly(span(source, "capture", "value"), span(source, "direct", "value")) + } + } + + @Test + fun `an inherited qualified type keeps the written qualifier and declaring member identities`() { + val source = + """ + module InheritedTypes { + class Base { class /*memberDeclaration*/Nested {} } + class /*qualifierDeclaration*/Derived extends Base {} + void use(/*qualifier*/Derived. /*member*/Nested value) {} + } + """.trimIndent() + withSource(source) { adapter -> + val qualifier = position(source, "qualifier") + val member = position(source, "member") + assertThat(adapter.findDefinition(URI, qualifier.line, qualifier.column)) + .isEqualTo(span(source, "qualifierDeclaration", "Derived")) + assertThat(adapter.findDefinition(URI, member.line, member.column)) + .isEqualTo(span(source, "memberDeclaration", "Nested")) + } + } + + @Test + fun `capturing a narrowed parameter retains its declaration and the narrower hover type`() { + val source = + """ + module NarrowedCapture { + String text(Object /*declaration*/value) { + if (value.is(String)) { + function String() read = () -> /*capture*/value; + return read(); + } + return value.toString(); + } + } + """.trimIndent() + withSource(source) { adapter -> + val use = position(source, "capture") + assertThat(adapter.findDefinition(URI, use.line, use.column)) + .isEqualTo(span(source, "declaration", "value")) + assertThat(adapter.getHoverInfo(URI, use.line, use.column)).contains("String") + } + } + + private fun withSource( + source: String, + check: (XdkAdapter) -> Unit, + ) { + CompilerTestSupport.configure() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + check(adapter) + } + } + + private fun position( + source: String, + marker: String, + ): Position { + val comment = "/*$marker*/" + val start = source.indexOf(comment) + require(start >= 0) { "Missing marker: $marker" } + val offset = start + comment.length + return Position( + source.take(offset).count { it == '\n' }, + offset - source.lastIndexOf('\n', offset) - 1, + ) + } + + private fun span( + source: String, + marker: String, + name: String, + ): Location = + position(source, marker).let { + Location(URI, it.line, it.column, it.line, it.column + name.length) + } + + private fun range(location: Location): Range = + Range( + Position(location.startLine, location.startColumn), + Position(location.endLine, location.endColumn), + ) + + private companion object { + const val URI = "file:///Navigation.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt new file mode 100644 index 00000000000..81f2efa85f2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt @@ -0,0 +1,117 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkOperandCompletionTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "module Operands { Int number = 1; String numberText = \"x\"; class Inner { " + + "void pair(Int count) {} void run() { pair(nu§); } } }", + "module Operands { class Values { static Int number = 1; static String numberText = \"x\"; } " + + "import Values.number; import Values.numberText; void pair(Int count) {} void run() { pair(nu§); } }", + ], + ) + fun `enclosing and imported values still require ordinary read and argument validation`(marked: String) { + val at = marked.indexOf('§') + XdkAdapter().use { adapter -> + val source = marked.replace("§", "") + adapter.compile(URI, source) + assertThat(adapter.getCompletions(URI, 0, at).map { it.label }) + .contains("number") + .doesNotContain("numberText") + assertThat(adapter.compile(URI, source.replaceRange(at - 2, at, "number")).diagnostics) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "pair(1 + nu§, \"x\")", + "pair(1 + §, \"x\")", + "pair(-nu§, \"x\")", + "pair(2 * (1 + nu§), \"x\")", + "pair(number = 1 + nu§, text = \"x\")", + "pair(1 + box.nu§, \"x\")", + "fn(1 + nu§, \"x\")", + "new Pair(1 + nu§, \"x\")", + ], + ) + fun `candidate insertion must fit the whole compound argument and later slots`(call: String) { + val marked = XdkArgumentContextFixture.HEADER + call + "; } }" + val at = marked.indexOf('§') + val source = marked.replace("§", "") + val prefix = if (marked[at - 1] == 'u') 2 else 0 + XdkAdapter().use { adapter -> + val normal = adapter.compile(URI, source) + val items = adapter.getCompletions(URI, 0, at) + assertThat(items.map { it.label }) + .describedAs(call) + .contains("number") + .doesNotContain("numberText", "numberHidden") + assertThat(items.single { it.label == "number" }.textEdit) + .isEqualTo(TextEdit(Range(Position(0, at - prefix), Position(0, at)), "number")) + assertThat(adapter.getCachedResult(URI)).isEqualTo(normal) + assertThat( + adapter.compile(URI, source.replaceRange(at - prefix, at, "number")).diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = ["pair(1 + nu§, True)", "pair(-nu§, True)", "pair(number = 1 + nu§, unknown = 1)"], + ) + fun `incompatible later arguments reject compound suggestions`(call: String) { + val marked = XdkArgumentContextFixture.HEADER + call + "; } }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["type.accept(nu§)", "box.acceptReceiver(nu§)"]) + fun `type value and receiver rewritten functions retain visible parameter mapping`(call: String) { + val marked = + "module Operands { class Box { static void accept(Int count) {} " + + "static void acceptReceiver(Box receiver, Int count) {} } " + + "void run(Type type, Box box, Int number, String numberText) { $call; } }" + val at = marked.indexOf('§') + XdkAdapter().use { adapter -> + val source = marked.replace("§", "") + adapter.compile(URI, source) + assertThat(adapter.getCompletions(URI, 0, at).map { it.label }) + .contains("number") + .doesNotContain("numberText") + val help = adapter.getSignatureHelp(URI, 0, at) + assertThat( + help + ?.signatures + ?.single() + ?.parameters + ?.map { it.label }, + ).containsExactly("Int count") + assertThat(adapter.compile(URI, source.replaceRange(at - 2, at, "number")).diagnostics) + .isEmpty() + } + } + + private companion object { + const val URI = "untitled:Operands.x" + } +} + +private object XdkArgumentContextFixture { + const val HEADER = + "module Operands { class Pair(Int number, String text) {} " + + "class Box { Int number = 1; String numberText = \"x\"; private Int numberHidden = 2; } " + + "void pair(Int number, String text) {} void run(Int number, String numberText, Box box, " + + "function void(Int, String) fn) { " +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt new file mode 100644 index 00000000000..7c9ad72abe0 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt @@ -0,0 +1,187 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkParameterRenameTest { + @TempDir lateinit var directory: Path + + @Test + fun `public parameter rename includes closed callers and only the selected overload`() { + val library = + "module Library { class Box { Int pick(Int input, Int count = 1) = input + count; " + + "String pick(String input) = input; } }" + val consumer = + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(count = 2, input = 1); }" + val other = + "module Other { package lib import Library; String run(lib.Box box) = box.pick(input = \"text\"); }" + workspace(mapOf("Library" to library, "Consumer" to consumer, "Other" to other)) { adapter -> + assertThat(adapter.compile(uri("Library"), library).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("Library"), 0, library.indexOf("input"), "value")) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys).containsExactlyInAnyOrder(uri("Library"), uri("Consumer")) + assertThat(apply(library, edit, "Library")) + .contains("Int value", "= value + count", "String pick(String input) = input") + assertThat(apply(consumer, edit, "Consumer")).contains("count = 2, value = 1") + } + } + + @Test + fun `override slot rename joins differently named declarations and named callers`() { + val library = "module Library { interface Mapper { T map(T input); } }" + val consumer = + "module Consumer { package lib import Library; class Mapper implements lib.Mapper { " + + "@Override String map(String argument) = argument; } " + + "String run(lib.Mapper api, Mapper impl) = api.map(input = \"a\") + impl.map(argument = \"b\"); }" + workspace(mapOf("Library" to library, "Consumer" to consumer)) { adapter -> + assertThat(adapter.compile(uri("Consumer"), consumer).diagnostics).isEmpty() + val edit = + requireNotNull( + adapter.rename(uri("Consumer"), 0, consumer.indexOf("argument"), "value"), + ) + assertThat(apply(library, edit, "Library")).contains("T map(T value)") + assertThat(apply(consumer, edit, "Consumer")) + .contains( + "String value) = value", + "api.map(value = \"a\")", + "impl.map(value = \"b\")", + ) + } + } + + @Test + fun `constructor parameter rename starts at a cross-module named argument`() { + val library = + "module Library { class Box { construct(Int input, Int count = 1) { total = input + count; } Int total; } }" + val consumer = + "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, input = 1); }" + workspace(mapOf("Library" to library, "Consumer" to consumer)) { adapter -> + assertThat(adapter.compile(uri("Consumer"), consumer).diagnostics).isEmpty() + val at = consumer.indexOf("input") + assertThat(adapter.prepareRename(uri("Consumer"), 0, at)).isNotNull() + val edit = requireNotNull(adapter.rename(uri("Consumer"), 0, at, "value")) + assertThat(apply(library, edit, "Library")) + .contains("construct(Int value", "total = value + count") + assertThat(apply(consumer, edit, "Consumer")).contains("count = 2, value = 1") + } + } + + @Test + fun `generic method parameter uses its visible slot and refuses local capture`() { + val text = + "module App { T pick(T input, Int count = 1) = input; String run() = pick(count = 2, input = \"yes\"); }" + workspace(mapOf("App" to text)) { adapter -> + assertThat(adapter.compile(uri("App"), text).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("App"), 0, text.lastIndexOf("input"), "value")) + assertThat(apply(text, edit, "App")).contains("T value", "= value", "value = \"yes\"") + assertThat(adapter.rename(uri("App"), 0, text.indexOf("input"), "count")).isNull() + } + } + + @Test + fun `escaped method values retain their bindings during public parameter rename`() { + val text = + "module App { Int pick(Int input) = input; Int run() { function Int(Int) f = &pick; return f(1); } }" + workspace(mapOf("App" to text)) { adapter -> + assertThat(adapter.compile(uri("App"), text).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(uri("App"), 0, text.indexOf("input"), "value")) + assertThat(apply(text, edit, "App")).isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `function values reject named arguments independently of the rename implementation`() { + val text = + "module App { Int pick(Int input) = input; Int run() { function Int(Int) f = &pick; return f(input = 1); } }" + XdkAdapter().use { adapter -> + val result = adapter.compile(uri("App"), text) + assertThat(result.diagnostics.map { it.code }).contains("COMPILER-141") + assertThat(adapter.rename(uri("App"), 0, text.indexOf("input"), "value")).isNull() + } + } + + @Test + fun `closed consumers mix selected method values with direct named calls`() { + val library = + "module Library { class Box { Int pick(Int input) = input; } class Other { String pick(String input) = input; } }" + val consumer = + "module Consumer { package lib import Library; Int run(lib.Box box) { " + + "function Int(Int) f = box.pick; return f(1) + box.pick(input = 2); } " + + "String text(lib.Other box) = box.pick(input = \"text\"); }" + workspace(mapOf("Library" to library, "Consumer" to consumer)) { adapter -> + assertThat(adapter.compile(uri("Consumer"), consumer).diagnostics).isEmpty() + assertThat(adapter.compile(uri("Library"), library).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("Library"), 0, library.indexOf("input"), "value")) + assertThat(apply(library, edit, "Library")) + .isEqualTo(library.replace("Int input) = input", "Int value) = value")) + assertThat(apply(consumer, edit, "Consumer")) + .isEqualTo(consumer.replace("input = 2", "value = 2")) + } + } + + @Test + fun `delegated parameter slots include differently named receiver declarations`() { + val library = "module Library { interface Api { Int read(Int input); } }" + val consumer = + "module Consumer { package lib import Library; " + + "class Actual implements lib.Api { @Override Int read(Int argument) = argument; } " + + "class Forward(Actual target) delegates lib.Api(target) {} " + + "Int use(Forward value, Actual actual) = value.read(input = 1) + actual.read(argument = 2); }" + workspace(mapOf("Library" to library, "Consumer" to consumer)) { adapter -> + assertThat(adapter.compile(uri("Consumer"), consumer).diagnostics).isEmpty() + assertThat(adapter.compile(uri("Library"), library).diagnostics).isEmpty() + val edit = + requireNotNull( + adapter.rename(uri("Library"), 0, library.indexOf("input"), "number"), + ) + assertThat(apply(library, edit, "Library")) + .isEqualTo(library.replace("input", "number")) + assertThat(apply(consumer, edit, "Consumer")) + .isEqualTo(consumer.replace("input", "number").replace("argument", "number")) + } + } + + private fun workspace( + sources: Map, + action: (XdkAdapter) -> Unit, + ) { + sources.forEach { (name, text) -> directory.resolve("$name.x").toFile().writeText(text) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + action(adapter) + sources.forEach { (name, text) -> + assertThat(directory.resolve("$name.x").toFile().readText()).isEqualTo(text) + } + } + } + + private fun apply( + text: String, + edit: WorkspaceEdit, + module: String, + ): String = + edit.changes + .getValue(uri(module)) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } + + private fun uri(module: String): String = + directory + .resolve("$module.x") + .toFile() + .canonicalFile + .toURI() + .toString() +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPartialAnalysisTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPartialAnalysisTest.kt new file mode 100644 index 00000000000..2e293399ce2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPartialAnalysisTest.kt @@ -0,0 +1,952 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ConstantPool +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.asm.MethodStructure +import org.xvm.asm.ModuleRepository +import org.xvm.asm.ModuleStructure +import org.xvm.compiler.CursorBinding +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AssignmentStatement +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.compiler.ast.NameExpression +import org.xvm.compiler.ast.Parameter +import org.xvm.compiler.ast.ReturnStatement +import org.xvm.compiler.ast.partial.IncompleteExpression +import org.xvm.compiler.ast.partial.IncompleteStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.tool.ModuleInfo +import java.io.File +import java.nio.file.Path +import java.util.concurrent.atomic.AtomicBoolean + +/** A real compiler consumer of the bounded partial-analysis API; no fallback parser or mock. */ +class XdkPartialAnalysisTest { + @ParameterizedTest + @ValueSource( + strings = + [ + "return 1 + value.|;", + "return flag ? value.| : 0;", + "return flag ? 0 : value.|;", + "return work(value.|, 2);", + "return work(1 + value.|, 2);", + ], + ) + fun `compound values and following arguments preserve the cursor context`(statement: String) { + CompilerTestSupport.configure() + val marked = + "module Editing { Int work(Int first, Int second) = first; Int run(String value, Boolean flag) { $statement } }" + val prefix = marked.substringBefore('|') + val text = marked.replace("|", "") + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(analysis.pool()).describedAs(errors.errors.toString()).isNotNull() + val site = analysis.sites().single() + assertThat(site.receiver.orElseThrow().isValidated) + .describedAs(errors.errors.toString()) + .isTrue() + assertThat( + analysis + .semanticSnapshot(errors) + .sites + .single() + .members + .map { it.name }, + ).contains("size") + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat( + ( + parents(site).filterIsInstance().first().component + as MethodStructure + ).hasOps(), + ).isFalse() + } + + @Test + fun `cursor facts publish surviving sites and release retries and discarded clones`() { + CompilerTestSupport.configure() + val prefix = "module Editing { void run(Int item) { ite" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source("$prefix; } }", URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + val binding = analysis.cursorBindings().getValue(site) + val clone = site.clone() as IncompleteStatement + val collector = CursorBinding.Collector() + collector.record(site, binding) + collector.record(clone, binding) + collector.begin(site) + assertThat(collector.finish(analysis.sourceTrees())).isEmpty() + collector.record(site, binding) + collector.record(clone, binding) + val published = collector.finish(analysis.sourceTrees()) + assertThat(published.keys).containsExactly(site) + assertThat(collector.finish(listOf(clone))).isEmpty() + assertThatThrownBy { (published as MutableMap).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + assertThatThrownBy { (binding.variables() as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + } + + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["return ", "Int result = ", "var result = ", "result = "]) + fun `incomplete values preserve their assignment or return context`(statement: String) { + CompilerTestSupport.configure() + for (operation in listOf("value.", "value.indexOf(\"x\", ")) { + val setup = if (statement == "result = ") "Int result = 0; " else "" + val prefix = "module Editing { Int run(String value) { $setup$statement$operation" + val text = "$prefix } Int later() = 42; }" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }) + .describedAs(errors.errors.toString()) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + assertThat(site.parent).isInstanceOf(IncompleteExpression::class.java) + assertThat(site.parent.parent) + .isInstanceOf( + if (statement == "return ") { + ReturnStatement::class.java + } else { + AssignmentStatement::class.java + }, + ) + val receiver = site.receiver.orElseThrow() + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("String") + } + assertThat((site.parent as IncompleteExpression).isValidated).isFalse() + val method = parents(site).filterIsInstance().first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + assertThat( + analysis + .semanticSnapshot(errors) + .sites + .single() + .members + .map { it.name }, + ).contains(if (site.isCall) "indexOf" else "size") + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "work(value.", + "work(inner(value.", + "work(text = value.", + "return work(value.", + "Int result = work(value.", + "work(value.indexOf(\"x\", ", + ], + ) + fun `nested incomplete arguments retain one innermost cursor site`(statement: String) { + CompilerTestSupport.configure() + val prefix = + "module Editing { Int work(String text) = 1; String inner(String text) = text; " + + "Int run(String value) { $statement" + val closing = + when { + statement.contains("indexOf") -> "" + statement.contains("inner") -> "))" + else -> ")" + } + val text = "$prefix$closing; } Int later() = 42; }" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }) + .describedAs(errors.errors.toString()) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + val receiver = site.receiver.orElseThrow() as NameExpression + assertThat(receiver.name).isEqualTo("value") + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat(site.endPosition).isEqualTo(position(prefix)) + val copied = analysis.semanticSnapshot(errors).sites.single() + assertThat(copied.members.map { it.name }).contains(if (site.isCall) "indexOf" else "size") + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val method = parents(site).filterIsInstance().first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + } + + @ParameterizedTest + @ValueSource(strings = ["", "si"]) + fun `partial value clones own their nested syntax`(member: String) { + val prefix = "module Editing { Int run(String value) { return work(value.$member" + val text = "$prefix); } }" + val errors = ErrorList() + val parser = Parser.forPartialAnalysis(Source(text, URI), position(prefix), errors) + val tree = parser.parseSource() + val expression = descendants(tree).filterIsInstance().first() + val clone = expression.clone() as IncompleteExpression + val originalNames = descendants(expression).filterIsInstance().toList() + val clonedNames = descendants(clone).filterIsInstance().toList() + assertThat(clonedNames.map { it.name }) + .containsExactlyElementsOf(originalNames.map { it.name }) + originalNames.zip(clonedNames).forEach { (original, copied) -> + assertThat(copied).isNotSameAs(original) + assertThat(copied.isValidated).isFalse() + } + assertThat(clone.endPosition).isEqualTo(expression.endPosition) + val copiedSite = + descendants(clone).filterIsInstance().single { !it.isCall } + assertThat(copiedSite.memberName.map { it.valueText }.orElse("")).isEqualTo(member) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + + @Test + fun `a local initializer retains the same receiver binding as ordinary compilation`() { + CompilerTestSupport.configure() + val prefix = "module Editing { String value = \"property\"; Int run() { Int value = value." + val errors = ErrorList() + val complete = + EmbeddingSupport + .instance() + .compileModule(Source("${prefix}size; return value; } }", URI), null, errors) + assertThat(complete.succeeded()).describedAs(errors.errors.toString()).isTrue() + val expected = + descendants(complete.parsed()).filterIsInstance().single { + it.name == "value" && it.parent is NameExpression + } + val expectedSymbol = expected.resolvedTarget.toString() + val partialErrors = ErrorList() + val partial = + EmbeddingSupport + .instance() + .analyzeIncomplete( + Source("$prefix } }", URI), + position(prefix), + null, + partialErrors, + ) + assertThat(partialErrors.errors.map { it.code }) + .containsExactly(Parser.INCOMPLETE_EXPRESSION) + val receiver = + partial + .sites() + .single() + .receiver + .orElseThrow() as NameExpression + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + assertThat(receiver.resolvedTarget.toString()).isEqualTo(expectedSymbol) + } + + @Test + fun `unsupported value prefixes remain unavailable and unknown receivers retain diagnostics`() { + CompilerTestSupport.configure() + for (statement in listOf("work(flag ? value.", "return (value. +")) { + val prefix = "module Editing { Int run(String value, Boolean flag) { $statement" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source("$prefix; } }", URI), position(prefix), null, errors) + assertThat(errors.hasSeriousErrors()).isTrue() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(analysis.pool()).isNull() + assertThat(analysis.sites()).isEmpty() + } + val prefix = "module Editing { Int run() { return work(missing." + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source("$prefix); } }", URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }) + .contains(Parser.INCOMPLETE_EXPRESSION, "COMPILER-38") + .doesNotContain("EMB-5") + val receiver = + analysis + .sites() + .single() + .receiver + .orElseThrow() + assertThat(receiver.isValidated && receiver.typeFit.isFit).isFalse() + } + + @ParameterizedTest + @ValueSource( + strings = ["value.", "value.indexOf(", "value.indexOf(\"x\", ", "value.indexOf(\"x\""], + ) + fun `cursor analysis retains following declarations without changing source`(operation: String) { + for (terminator in listOf("", ";")) { + val prefix = "module Editing { void run(String value) { $operation" + val text = "$prefix$terminator } Int later() = 42; }" + CompilerTestSupport.configure() + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat(site.endPosition).isEqualTo(position(prefix)) + val receiver = site.receiver.orElseThrow() as NameExpression + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("String") + } + val later = + descendants(analysis.sourceTrees().single()) + .filterIsInstance() + .single { + it.name == "later" + } + assertThat(Source.calculateOffset(later.startPosition)) + .isEqualTo(text.indexOf("Int later")) + val method = parents(site).filterIsInstance().first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + + val compileErrors = ErrorList() + assertThat( + EmbeddingSupport + .instance() + .compileModule(Source(text, URI), null, compileErrors) + .succeeded(), + ).isFalse() + assertThat(compileErrors.hasSeriousErrors()).isTrue() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "value.", + "return value.", + "Int result = value.", + "return work(value.", + "value.si", + "return value.si", + "value.indexOf(", + ], + ) + fun `cursor analysis in a module member uses the unsaved root and member snapshot`(statement: String) { + CompilerTestSupport.configure() + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { Int value = 1; } }") + member.writeText("class Child extends Base { void run() {} }") + val prefix = "class Child extends Base { Int run() { $statement" + val closing = + if (statement.contains("work(") || statement.contains("indexOf(")) ");" else "" + val overlay = "$prefix$closing } Int later() = 42; }" + val sources = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray = + when (file) { + root -> { + "module Editing { class Base { String value = \"overlay\"; Int work(String text) = text.size; } }" + } + + member -> { + overlay + } + + else -> { + error("Unexpected source $file") + } + }.toCharArray() + } + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(sources, member, position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + assertThat(site.source.fileName).isEqualTo(member.path) + assertThat(site.source.toRawString()).isEqualTo(overlay) + val receiver = site.receiver.orElseThrow() as NameExpression + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("String") + } + val snapshot = analysis.semanticSnapshot(errors) + assertThat(snapshot.semantics.sourceName).isEqualTo(member.path) + val copied = snapshot.sites.single() + assertThat(snapshot.semantics.type(copied.receiverType!!)!!.displayName).contains("String") + if (site.isCall) { + assertThat(copied.members.map { it.name }).containsOnly("indexOf") + } else { + assertThat(copied.members.map { it.name }).contains("size", "indexOf") + } + assertThat(snapshot.semantics.symbols.mapNotNull { it.declarationSource }) + .contains(root.path, member.path) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + assertThat(root.readText()).contains("Int value") + assertThat(member.readText()).doesNotContain("value.") + } + + @Test + fun `cursors outside supported boundaries and unrelated syntax errors yield no partial facts`() { + CompilerTestSupport.configure() + for ( + (prefix, suffix) in + listOf( + "module Editing { void run(String value) { value." to "size; } }", + "module Editing { void run(String value) { value.indexOf(" to "\"x\"); } }", + "module Editing { void run(String value) { value." to " } void broken( { }", + ) + ) { + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(prefix + suffix, URI), position(prefix), null, errors) + assertThat(analysis.pool()).describedAs("%s|%s", prefix, suffix).isNull() + assertThat(analysis.sites()).isEmpty() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + } + + @ParameterizedTest + @ValueSource(strings = ["si|ze", "ind|exOf(\"x\")"]) + fun `mid token member cursors replace the complete existing name`(member: String) { + val prefix = "module Editing { void run(String value) { value." + member.substringBefore('|') + val source = prefix + member.substringAfter('|') + "; } }" + val name = member.replace("|", "").substringBefore('(') + XdkAdapter().use { adapter -> + adapter.compile(URI, source) + val completions = adapter.getCompletions(URI, 0, prefix.length).filter { it.label == name } + val start = prefix.length - member.substringBefore('|').length + assertThat(completions).isNotEmpty() + assertThat(completions.map { it.textEdit }).containsOnly( + TextEdit(Range(Position(0, start), Position(0, start + name.length)), name), + ) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "value.si", + "return value.si", + "Int result = value.si", + "work(value.si", + "getValue().si", + "work(getValue().si", + ], + ) + fun `typed member prefixes retain compiler context and their original token`(statement: String) { + CompilerTestSupport.configure() + val prefix = + "module Editing { String getValue() = \"text\"; Int work(Int n) = n; Int run(String value) { $statement" + val text = + prefix + (if (statement.startsWith("work(")) ");" else ";") + " } Int later() = 42; }" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat(site.memberName.orElseThrow().valueText).isEqualTo("si") + assertThat(site.memberName.orElseThrow().startPosition) + .isEqualTo(position(prefix.dropLast(2))) + assertThat(site.memberName.orElseThrow().endPosition).isEqualTo(position(prefix)) + val model = analysis.semanticSnapshot(errors) + val copied = model.sites.single() + assertThat(copied.memberPrefix!!.text).isEqualTo("si") + assertThat(model.semantics.type(copied.receiverType!!)!!.displayName).isEqualTo("String") + assertThat(copied.members.map { it.name }).contains("size") + val method = parents(site).filterIsInstance().first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + assertThat( + descendants(analysis.sourceTrees().single()) + .filterIsInstance() + .map { it.name } + .toList(), + ).contains("later") + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "value.indexOf(", + "value.indexOf(\"x\"", + "value.indexOf(\"x\", ", + "return value.indexOf(", + "work(value.indexOf(", + "getValue().indexOf(", + ], + ) + fun `a call cursor before a closing parenthesis retains the intact prefix`(statement: String) { + CompilerTestSupport.configure() + val prefix = + "module Editing { String getValue() = \"text\"; Int work(Int n) = n; Int run(String value) { $statement" + val text = + prefix + (if (statement.startsWith("work(")) "));" else ");") + " } Int later() = 42; }" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(site.isCall).isTrue() + assertThat(site.source.toRawString()).isEqualTo(text) + assertThat(site.endPosition).isEqualTo(position(prefix)) + val model = analysis.semanticSnapshot(errors) + assertThat(model.sites.single().members).isNotEmpty() + assertThat(model.semantics.calls.map { model.semantics.symbol(it.method)?.name }) + .doesNotContain("indexOf") + val method = parents(site).filterIsInstance().first() + assertThat((method.component as MethodStructure).hasOps()).isFalse() + assertThat( + descendants(analysis.sourceTrees().single()) + .filterIsInstance() + .map { it.name } + .toList(), + ).contains("later") + } + + @Test + fun `cursor-selected complete syntax remains valid in ordinary compilation`() { + CompilerTestSupport.configure() + for ( + (prefix, suffix) in + listOf( + "module Editing { Int run(String value) { return value.size" to "; } }", + "module Editing { void run(String value) { value.indexOf(\"x\"" to "); } }", + ) + ) { + val text = prefix + suffix + val errors = ErrorList() + val partial = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), position(prefix), null, errors) + assertThat(partial.sites()).hasSize(1) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val normal = ErrorList() + assertThat( + EmbeddingSupport + .instance() + .compileModule(Source(text, URI), null, normal) + .succeeded(), + ).describedAs(normal.errors.toString()) + .isTrue() + assertThat(normal.errors).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "si"]) + fun `cursor analysis preserves flow narrowing and UTF-16 positions inside closing blocks`(member: String) { + CompilerTestSupport.configure() + for (newline in listOf("\n", "\r\n")) { + val prefix = + "module Editing {$newline void run(Object value) {$newline if (value.is(String)) { /* 😀 */ value.$member" + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source("$prefix } } }", URI), position(prefix), null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + assertThat(Source.calculateLine(site.endPosition)).isEqualTo(2) + assertThat(Source.calculateOffset(site.endPosition)) + .isEqualTo(prefix.lines().last().length) + val receiver = site.receiver.orElseThrow() + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("String") + } + } + } + + @Test + fun `a syntax error in another member prevents partial module semantics`() { + CompilerTestSupport.configure() + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { String value = \"text\"; } }") + val prefix = "class Child extends Base { void run() { value." + member.writeText("$prefix } }") + File(member.parentFile, "Broken.x").writeText("class Broken { void broken( { }") + val errors = ErrorList() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(ModuleInfo(root, false), member, position(prefix), null, errors) + assertThat(analysis.pool()).isNull() + assertThat(analysis.sites()).isEmpty() + assertThat(errors.errors).anyMatch { it.code != Parser.INCOMPLETE_EXPRESSION } + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + } + + @ParameterizedTest + @ValueSource( + strings = ["value.", "value.si", "value.indexOf(", "val", "value.indexOf(startAt ="], + ) + fun `module cursor diagnostics honor cancellation and budgets without duplicates`(operation: String) { + CompilerTestSupport.configure() + val root = directory.resolve("Editing.x").toFile().canonicalFile + val prefix = "module Editing { void run(String value) { $operation" + root.writeText( + prefix + + (if (operation.endsWith("(") || operation.endsWith("=")) ");" else ";") + + " } }", + ) + val support = EmbeddingSupport.instance() + val delivered = mutableListOf() + val analysis = + support.analyzeIncomplete( + ModuleInfo(root, false), + root, + position(prefix), + null, + ErrorListener { delivered.add(it) }, + ) + assertThat(analysis.pool()).isNotNull() + assertThat(delivered.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + + val errors = ErrorList(ErrorList.FIRST_ERROR) + val budgeted = + support.analyzeIncomplete(ModuleInfo(root, false), root, position(prefix), null, errors) + assertThat(budgeted.pool()).isNull() + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + + val cancelled = AtomicBoolean() + val listener = + ErrorListener.cancellable( + ErrorListener.collecting { cancelled.set(true) }, + cancelled::get, + ) + assertThat( + support + .analyzeIncomplete( + ModuleInfo(root, false), + root, + position(prefix), + null, + listener, + ).pool(), + ).isNull() + assertThat(cancelled.get()).isTrue() + val unread = + object : ModuleInfo(root, false) { + override fun readSource(file: File): CharArray = error("A cancelled attempt must not read source") + } + assertThat( + support + .analyzeIncomplete(unread, root, position(prefix), null, listener) + .sourceTrees(), + ).isEmpty() + } + + private fun position(prefix: String): Long { + val source = Source(prefix) + while (source.hasNext()) source.next() + return source.position + } + + @Test + fun `trailing member access resolves the receiver without producing a compiled method`() { + val text = "module Editing { @Inject Console console; void run() { console." + val (analysis, errors) = analyze(text) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + assertThat(analysis.pool()).isNotNull() + val site = analysis.sites().single() + assertThat(site.isCall).isFalse() + val receiver = site.receiver.orElseThrow() as NameExpression + assertThat(receiver.isValidated).isTrue() + assertThat(receiver.resolvedTarget).isNotNull() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("Console") + } + val method = parents(site).filterIsInstance().first() + assertThat(method.name).isEqualTo("run") + assertThat((method.component as MethodStructure).hasOps()).isFalse() + + val compileErrors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule(Source(text, URI), null, compileErrors) + assertThat(compilation.succeeded()).isFalse() + assertThat(compilation.file()).isNull() + assertThat(compilation.parsed()).isNull() + } + + @Test + fun `unfinished calls retain real argument and separator positions and resolve method parameters`() { + val text = "module Editing { void run(String text) { text.indexOf(\"a,b\", " + val (analysis, errors) = analyze(text) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + val site = analysis.sites().single() + assertThat(site.isCall).isTrue() + assertThat(Source.calculateOffset(site.operator.startPosition)) + .isEqualTo(text.indexOf("(\"a,b\"")) + assertThat(site.arguments).hasSize(1) + assertThat(site.separators.map { Source.calculateOffset(it.startPosition) }) + .containsExactly(text.lastIndexOf(',')) + val receiver = site.receiver.orElseThrow() as NameExpression + val method = parents(site).filterIsInstance().first() + val parameter = descendants(method).filterIsInstance().first() + assertThat(receiver.resolvedTarget).isSameAs(parameter.resolvedTarget) + assertThat(receiver.isValidated).isTrue() + assertThat(site.arguments.single().isValidated).isTrue() + assertThat((site.target as NameExpression).resolvedTarget).isNull() + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + assertThat(receiver.type.valueString).contains("String") + assertThat( + site.arguments + .single() + .type.valueString, + ).contains("String") + } + assertThat((method.component as MethodStructure).hasOps()).isFalse() + } + + @Test + fun `unrelated malformed statements prevent partial semantic analysis`() { + val (analysis, errors) = analyze("module Editing { void run() { Int broken = ; console.") + assertThat(errors.errors).isNotEmpty() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(analysis.pool()).isNull() + assertThat(analysis.sites()).isEmpty() + } + + @Test + fun `scope and flow narrowing come from this attempt and survive a later compilation`() { + val snapshots = + listOf("String", "Int").map { type -> + val text = "module Editing { void run(Object value) { if (value.is($type)) { value." + val (analysis, errors) = analyze(text) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + val receiver = + analysis + .sites() + .single() + .receiver + .orElseThrow() + assertThat(receiver.isValidated && receiver.typeFit.isFit).isTrue() + val copiedType = + ConstantPool.withPool(requireNotNull(analysis.pool())).use { + receiver.type.valueString + } + assertThat(copiedType).contains(type) + copiedType + } + assertThat(snapshots[0]).contains("String").doesNotContain("Int") + assertThat(snapshots[1]).contains("Int").doesNotContain("String") + } + + @Test + fun `unknown receiver reports its real error without inventing a type`() { + val (analysis, errors) = analyze("module Editing { void run() { missing.") + assertThat(errors.errors.map { it.code }) + .contains(Parser.UNEXPECTED_EOF) + .doesNotContain("EMB-5") + assertThat(errors.errors).anySatisfy { assertThat(it.code).startsWith("COMPILER-") } + val receiver = + analysis + .sites() + .single() + .receiver + .orElseThrow() as NameExpression + assertThat(receiver.resolvedTarget).isNull() + assertThat(receiver.isValidated && receiver.typeFit.isFit).isFalse() + } + + @Test + fun `nested calls named arguments and UTF-16 positions retain only top-level separators`() { + for (newline in listOf("\n", "\r\n")) { + val text = + "module Editing {$newline void run(String text) {$newline" + + " /* 😀 */ text.indexOf(text.replace(\"x\", \"y\"), start = 2, " + val (analysis, errors) = analyze(text) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + val site = analysis.sites().single() + assertThat(site.arguments).hasSize(2) + assertThat(site.arguments).allSatisfy { + assertThat(it.isValidated && it.typeFit.isFit).isTrue() + } + assertThat(site.separators).hasSize(2) + val line = text.lines().last() + assertThat(site.separators.map { Source.calculateOffset(it.startPosition) }) + .containsExactly( + line.indexOf(", start"), + line.lastIndexOf(','), + ) + assertThat(Source.calculateLine(site.endPosition)).isEqualTo(2) + assertThat(Source.calculateOffset(site.endPosition)).isEqualTo(line.length) + assertThat((site.target as NameExpression).resolvedTarget).isNull() + } + } + + @Test + fun `calls without arguments retain the callee and scope but do not select an overload`() { + for (suffix in listOf("work(", "work(1", "work(1,")) { + val (analysis, errors) = + analyze( + "module Editing { void work(Int n) {} void work(String s) {} void run() { $suffix", + ) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + val site = analysis.sites().single() + assertThat((site.target as NameExpression).name).isEqualTo("work") + assertThat((site.target as NameExpression).resolvedTarget).isNull() + assertThat(site.receiver).isEmpty() + assertThat(parents(site).filterIsInstance().first().name) + .isEqualTo("run") + assertThat(site.arguments).hasSize(if ('1' in suffix) 1 else 0) + assertThat(site.separators).hasSize(if (suffix.endsWith(',')) 1 else 0) + } + } + + @Test + fun `unsupported contexts and lexer failures never enter semantic analysis`() { + for ( + body in + listOf( + "return value.", + "Int result = value.", + "work(value.", + "work(value(", + "work(\"unterminated", + ) + ) { + val (analysis, errors) = analyze("module Editing { void run(String value) { $body") + assertThat(errors.errors).isNotEmpty() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(analysis.pool()).isNull() + assertThat(analysis.sites()).isEmpty() + } + val (complete, errors) = analyze("module Editing { void run() {} }") + assertThat(errors.errors).isEmpty() + assertThat(complete.sites()).isEmpty() + assertThat(complete.pool()).isNull() + } + + @Test + fun `EOF reaches a non-deduplicating host once and cancellation and budgets stop analysis`() { + CompilerTestSupport.configure() + val text = "module Editing { void run(String value) { value." + val delivered = mutableListOf() + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete(Source(text, URI), null, ErrorListener { delivered.add(it) }) + assertThat(delivered.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + assertThat(analysis.pool()).isNotNull() + + val errors = ErrorList(ErrorList.FIRST_ERROR) + val budgeted = + EmbeddingSupport.instance().analyzeIncomplete(Source(text, URI), null, errors) + assertThat(errors.errors).hasSize(1) + assertThat(budgeted.pool()).isNull() + + val cancelled = AtomicBoolean() + val listener = + ErrorListener.cancellable( + ErrorListener.collecting { cancelled.set(true) }, + cancelled::get, + ) + val interrupted = + EmbeddingSupport.instance().analyzeIncomplete(Source(text, URI), null, listener) + assertThat(cancelled.get()).isTrue() + assertThat(interrupted.pool()).isNull() + val alreadyCancelled = + EmbeddingSupport.instance().analyzeIncomplete(Source(text, URI), null, listener) + assertThat(alreadyCancelled.sourceTrees()).isEmpty() + } + + @Test + fun `cancellation during repository loading cannot advance into another compiler phase`() { + CompilerTestSupport.configure() + val cancelled = AtomicBoolean() + val errors = ErrorList() + val input = + object : ModuleRepository { + override fun getModuleNames(): Set = emptySet() + + override fun loadModule(name: String): ModuleStructure? { + cancelled.set(true) + return null + } + + override fun storeModule(module: ModuleStructure) { + error("An input repository must not receive compiled output") + } + } + val analysis = + EmbeddingSupport + .instance() + .analyzeIncomplete( + Source("module Editing { void run(String value) { value.", URI), + input, + ErrorListener.cancellable(errors, cancelled::get), + ) + assertThat(cancelled.get()).isTrue() + assertThat(errors.errors.map { it.code }).containsExactly(Parser.UNEXPECTED_EOF) + assertThat(analysis.pool()).isNull() + assertThat( + analysis + .sites() + .single() + .receiver + .orElseThrow() + .isValidated, + ).isFalse() + } + + private fun analyze(text: String): Pair { + CompilerTestSupport.configure() + val errors = ErrorList() + return EmbeddingSupport.instance().analyzeIncomplete(Source(text, URI), null, errors) to + errors + } + + private fun parents(node: AstNode): Sequence = generateSequence(node.parent) { it.parent } + + private fun descendants(node: AstNode): Sequence = + sequence { + yield(node) + for (child in node.children()) yieldAll(descendants(child)) + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPlatformRegressionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPlatformRegressionTest.kt new file mode 100644 index 00000000000..ec353a83ae8 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPlatformRegressionTest.kt @@ -0,0 +1,193 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.MethodStructure +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.AstNode +import org.xvm.compiler.ast.MethodDeclarationStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +/** Minimized real-source failures from the platform demo, without a sibling checkout dependency. */ +class XdkPlatformRegressionTest { + @ParameterizedTest + @ValueSource(strings = ["typedef (Int | Array) as Doc;", "package json import json.xtclang.org; import json.Doc;"]) + fun `recursive local and bundled types retain workspace diagnostics and rename proofs`( + declaration: String, + @TempDir directory: Path, + ) { + val source = + """ + module Demo { + $declaration + private Doc echo(Doc input) = input; + Doc run(Doc value) = echo(input = value); + } + """.trimIndent() + val file = + directory + .toRealPath() + .resolve("Demo.x") + .toFile() + .apply { writeText(source) } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("Demo", uri))) + val reports = adapter.workspaceDiagnosticsAsync().get(30, SECONDS) + assertThat(reports).isNotEmpty().allMatch { it.success } + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(uri, 2, source.lines()[2].indexOf("input"), "renamed")) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(edit.changes.getValue(uri)).hasSize(3).allSatisfy { + assertThat(it.newText).isEqualTo("renamed") + assertThat(it.range.start.line).isEqualTo(it.range.end.line) + assertThat(source.lines()[it.range.start.line].substring(it.range.start.column, it.range.end.column)).isEqualTo("input") + } + assertThat(adapter.compile(uri, source.replace("input", "renamed")).diagnostics).isEmpty() + } + } + + @Test + fun `platform CircularBuffer compiles and keeps anonymous enclosing receivers and named signatures`() { + val source = javaClass.getResource("/platform/CircularBuffer.x")!!.readText() + + fun position( + text: String, + anchor: String, + ): Position { + val prefix = text.substring(0, text.indexOf(anchor) + anchor.length) + return Position(prefix.count { it == '\n' }, prefix.substringAfterLast('\n').length) + } + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val incomplete = source.replace("this.CircularBuffer.size", "this.CircularBuffer.si") + adapter.compile(URI, incomplete) + val at = position(incomplete, "this.CircularBuffer.si") + val input = Source(incomplete) + repeat(incomplete.indexOf("this.CircularBuffer.si") + "this.CircularBuffer.si".length) { input.next() } + val cursor = input.position + input.reset() + val errors = ErrorList() + val tree = Parser.forPartialAnalysis(input, cursor, errors).parseSource() + assertThat(errors.errors.map { it.code }) + .describedAs(tree.toDumpString()) + .containsOnly(Parser.INCOMPLETE_EXPRESSION) + val analysisErrors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(Source(incomplete), cursor, null, analysisErrors) + assertThat(analysis.pool()).describedAs("%s", analysisErrors).isNotNull + assertThat(analysis.sites()).hasSize(1) + assertThat(analysis.sites().single().source).describedAs("%s", analysisErrors).isNotNull() + val site = analysis.sites().single() + assertThat(site.target.isValidated).describedAs("%s", analysisErrors).isTrue() + assertThat(analysis.cursorBindings()).containsKey(site) + val method = + generateSequence(site as AstNode) { it.parent } + .filterIsInstance() + .first() + .component as MethodStructure + assertThat(method.hasOps()).isFalse() + val items = adapter.getCompletions(URI, at.line, at.column) + assertThat(items.map { it.label }).describedAs("partial=%s", adapter.analyzeAtAsync(URI, at).join()?.sites).contains("size") + val size = items.single { it.label == "size" } + assertThat(size.detail).contains("Int") + assertThat( + size.textEdit!! + .range.start.column, + ).isEqualTo(at.column - 2) + val constructor = source.replace("new Element?[capacity]", "new Element?[cap]") + adapter.compile(URI, constructor) + val dimension = position(constructor, "new Element?[cap") + assertThat(adapter.getCompletions(URI, dimension.line, dimension.column).map { it.label }).contains("capacity") + adapter.compile(URI, source) + val call = position(source, "toArray().toString(sep, pre, post, limit, trunc, ") + val help = adapter.getSignatureHelp(URI, call.line, call.column)!! + assertThat(help.signatures[help.activeSignature].parameters.map { it.label }).anyMatch { "render" in it } + assertThat(help.activeParameter).isEqualTo(5) + } + } + + @Test + fun `real platform buffer callback accepts an expected type lambda template`() { + val fixture = javaClass.getResource("/platform/CircularBuffer.x")!!.readText() + val line = " void inspect() { val buffer = new CircularBuffer(4); buffer.toString(render = ); }" + val source = fixture.replace("module PlatformBuffer {", "module PlatformBuffer {\n$line") + val row = source.lines().indexOf(line) + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, source) + val item = adapter.getCompletions(URI, row, line.indexOf("render = ") + 9).single { it.label == "(arg1) -> TODO()" } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, source.replace("render = )", "render = ${item.insertText})")).diagnostics).isEmpty() + } + } + + @Test + fun `hover names the selected generic callee rather than its enclosing method`() { + CompilerTestSupport.configure() + val source = "module Demo { T echo(T value) = value; String run() = echo(\"value\"); }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getHoverInfo(URI, 0, source.lastIndexOf("echo"))) + .contains("String echo(String value)") + .doesNotContain("run") + } + } + + @Test + fun `whitespace before a written argument retains selected signature metadata and named mapping`() { + CompilerTestSupport.configure() + listOf( + "echo(\"a\", §\"b\")" to 1, + "echo(backup = \"b\", §value = \"a\")" to 0, + ).forEach { (call, parameter) -> + val marked = + "module Demo { T echo(T value, T backup) = value; String run() = $call; }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, marked.replace("§", "")).diagnostics).isEmpty() + val help = adapter.getSignatureHelp(URI, 0, marked.indexOf('§'))!! + assertThat(help.signatures.single().parameters).hasSize(2) + assertThat(help.activeParameter).isEqualTo(parameter) + } + } + } + + @Test + fun `member completion before existing parentheses retains direct and chained receivers`() { + CompilerTestSupport.configure() + listOf("text.tr§()", "text.trim().tr§()").forEach { expression -> + val marked = "module Demo { String run(String text) = $expression; }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + val items = + adapter.getCompletionsAsync(URI, 0, marked.indexOf('§')).get(30, SECONDS) + assertThat(items.map { it.label }).contains("trim") + } + } + } + + @Test + fun `narrowed JSON object resolves its underlying bundled map type`() { + CompilerTestSupport.configure() + val source = + "module Demo { package json import json.xtclang.org; import json.Doc; import json.JsonObject; " + + "String run(Doc value) { assert value.is(JsonObject); return value.toString(); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val locations = adapter.findTypeDefinitions(URI, 0, source.lastIndexOf("value")) + assertThat(locations).isNotEmpty() + assertThat(locations.map { it.uri }).anyMatch { it.endsWith("/Map.x") } + } + } + + private companion object { + const val URI = "file:///Demo.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt new file mode 100644 index 00000000000..61abc31c718 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt @@ -0,0 +1,86 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkPresentation +import org.xvm.lsp.treesitter.SemanticTokenLegend +import java.util.UUID + +class XdkPresentationScaleTest { + @Test + fun `semantic coverage preserves adjacent lexical tokens and excludes nested overlaps`() { + val ranges = listOf(30 to 40, 10 to 20, 10 to 12, 14 to 16) + val model = model(ranges.map { (start, end) -> span(0, start, end) }) + val number = SemanticTokenLegend.typeIndex.getValue("number") + val lexical = + listOf(0 to 10, 9 to 10, 9 to 11, 12 to 13, 20 to 30, 39 to 41, 40 to 41) + .map { (start, end) -> listOf(0, start, end - start, number, 0) } + + listOf(listOf(1, 12, 1, number, 0)) + val decoded = + XdkPresentation + .tokens(model, lexical) + .data + .chunked(5) + .runningFold(listOf(0, 0, 0, 0, 0)) { previous, token -> + listOf(previous[0] + token[0], token[1] + if (token[0] == 0) previous[1] else 0) + token.drop(2) + }.drop(1) + assertThat(decoded.filter { it[3] == number }).containsExactly( + listOf(0, 0, 10, number, 0), + listOf(0, 9, 1, number, 0), + listOf(0, 20, 10, number, 0), + listOf(0, 40, 1, number, 0), + listOf(1, 12, 1, number, 0), + ) + assertThat(XdkPresentation.tokens(model(emptyList()), lexical).data).hasSize(lexical.size * 5) + } + + @Test + fun `large inferred model preserves tooltips and half open hint boundaries`() { + val model = model((0 until 5_000).map { span(it, 4, 9) }) + val all = XdkPresentation.hints(model, Range(Position(0, 0), Position(5_000, 0))) + assertThat(all).hasSize(5_000) + assertThat(all.first().tooltip).isEqualTo("```xtc\nInt local0\n```\n\nDocumentation 0") + assertThat(all.last().tooltip).isEqualTo("```xtc\nInt local4999\n```\n\nDocumentation 4999") + listOf(0, 2_500, 4_999).forEach { line -> + assertThat(XdkPresentation.hints(model, Range(Position(line, 9), Position(line, 10)))) + .containsExactly(all[line]) + assertThat(XdkPresentation.hints(model, Range(Position(line, 0), Position(line, 9)))).isEmpty() + assertThat(all[line].tooltip).isEqualTo(XdkPresentation.hover(model, line, 4)) + } + } + + private fun span( + line: Int, + start: Int, + end: Int, + ) = SemanticModel.Range(SemanticModel.Position(line, start), SemanticModel.Position(line, end)) + + private fun model(ranges: List): SemanticModel { + val snapshot = UUID.randomUUID() + val typeId = SemanticModel.TypeId(snapshot, 0) + val type = SemanticModel.Type(typeId, "Int", SemanticModel.TypeForm.NAMED, emptyList(), emptyList(), false) + val symbols = + ranges.mapIndexed { index, range -> + SemanticModel.Symbol( + SemanticModel.SymbolId(snapshot, index), + "local$index", + SemanticModel.SymbolKind.VARIABLE, + range, + typeId, + null, + "Scale.x", + inferred = true, + documentation = "Documentation $index", + ) + } + return SemanticModel( + snapshot, + SemanticModel.Status.COMPLETE, + "Scale.x", + SemanticModel.Facts(symbols.associateBy { it.id }, mapOf(typeId to type)), + symbols.map { SemanticModel.Occurrence(it.declaration!!, it.name, SemanticModel.Role.DECLARATION, it.id, typeId) }, + emptyList(), + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt new file mode 100644 index 00000000000..4df4cf15eba --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt @@ -0,0 +1,247 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.treesitter.SemanticTokenLegend + +class XdkPresentationTest { + @ParameterizedTest + @ValueSource(strings = ["plain", "😀"]) + fun `hover retains reference positions after string literals`(marker: String) { + XdkAdapter().use { adapter -> + listOf( + "module Presentation { Int run() { String marker = \"$marker\"; Int value = 1; return value; } }", + "module Presentation { private Int run(Int value) { String marker = \"$marker\"; return value; } }", + ).forEach { source -> + assertThat(adapter.compile(URI, source).success).isTrue() + val offset = source.lastIndexOf("value") + assertThat(adapter.getHoverInfo(URI, 0, offset)) + .describedAs(source) + .contains("Int") + assertThat(adapter.prepareRename(URI, 0, offset)?.range) + .describedAs(source) + .isEqualTo(Range(Position(0, offset), Position(0, offset + "value".length))) + } + } + } + + @Test + fun `anonymous construction tokens never overlap during recovery`() { + val source = + """ + module Presentation { + class Packet { construct(T first, T second) {} } + Int pair(Int first, Int second) = first; + Int edit(String word) { return 1 + word.size; } + void run(String text) { + new Packet("anonymous", text) { String read() = text; }; + } + } + """.trimIndent() + XdkAdapter().use { adapter -> + listOf(source, source.replace("return 1 + word.size;", "return pair((pair(1, ;")) + .forEach { text -> + val compilation = adapter.compile(URI, text) + assertThat(compilation.success).isEqualTo(text == source) + val tokens = adapter.getSemanticTokens(URI)?.let(::decode).orEmpty() + // Parser recovery may produce no AST. It must not retain stale tokens. + if (text == source) assertThat(tokens).isNotEmpty() + tokens.zipWithNext().forEach { (previous, next) -> + assertThat(previous[0] < next[0] || previous[1] + previous[2] <= next[1]) + .describedAs("overlapping tokens: %s then %s", previous, next) + .isTrue() + } + } + } + } + + @Test + fun `semantic tokens classify resolved declarations references modifiers and writes`() { + val source = + """ + module Presentation { + interface /*api*/Reader { Int read(); } + class Box { Int value = 0; } + static Int /*method*/run(Box /*parameter*/box) { + Int /*local*/count = 0; + /*write*/count = 1; + /*compound*/count += 2; + ++/*increment*/count; + /*receiver*/box. /*field*/value = /*read*/count; + return box.value; + } + } + """.trimIndent() + withSource(source) { adapter -> + val tokens = decode(adapter.getSemanticTokens(URI)!!) + + fun token(marker: String): List = + tokens.single { + it.take(2) == at(source, marker).let { listOf(it.line, it.column) } + } + assertThat(SemanticTokenLegend.tokenTypes[token("api")[3]]).isEqualTo("interface") + assertThat(SemanticTokenLegend.tokenTypes[token("parameter")[3]]).isEqualTo("parameter") + assertThat(SemanticTokenLegend.tokenTypes[token("local")[3]]).isEqualTo("variable") + assertThat( + token("method")[4] and + SemanticTokenLegend.modifierBitmask("static", "declaration"), + ).isEqualTo(SemanticTokenLegend.modifierBitmask("static", "declaration")) + listOf("write", "compound", "increment", "field").forEach { + assertThat(token(it)[4] and SemanticTokenLegend.modifierBitmask("modification")) + .isNotZero() + } + listOf("receiver", "read").forEach { + assertThat(token(it)[4] and SemanticTokenLegend.modifierBitmask("modification")) + .isZero() + } + val local = at(source, "local") + val highlights = adapter.getDocumentHighlights(URI, local.line, local.column) + assertThat(highlights.map { it.kind }) + .containsExactly( + DocumentHighlight.HighlightKind.TEXT, + DocumentHighlight.HighlightKind.WRITE, + DocumentHighlight.HighlightKind.WRITE, + DocumentHighlight.HighlightKind.WRITE, + DocumentHighlight.HighlightKind.READ, + ) + } + } + + @Test + fun `hints use selected parameter mapping omit named and defaulted arguments and show inferred locals`() { + val source = + """ + module Presentation { + Int pick(Int first, Int second = 2) = first + second; + String pick(String text) = text; + Int run() { + var /*inferred*/number = pick(/*positional*/1); + val /*text*/label = pick(/*string*/"text"); + Int explicit = pick(second = 3, first = 4); + return number + explicit + label.size; + } + } + """.trimIndent() + withSource(source) { adapter -> + val hints = adapter.getInlayHints(URI, ALL) + assertThat(hints.map { it.label }) + .containsExactly(": Int", "first:", ": String", "text:") + assertThat(hints.map { it.position }) + .containsExactly( + at(source, "inferred").let { it.copy(column = it.column + "number".length) }, + at(source, "positional"), + at(source, "text").let { it.copy(column = it.column + "label".length) }, + at(source, "string"), + ) + assertThat( + adapter.getInlayHints(URI, Range(Position(5, 0), Position(6, 0))).map { + it.label + }, + ).containsExactly(": String", "text:") + val readonly = + decode(adapter.getSemanticTokens(URI)!!).single { + it.take(2) == at(source, "text").let { listOf(it.line, it.column) } + } + assertThat(readonly[4] and SemanticTokenLegend.modifierBitmask("readonly")).isNotZero() + } + } + + @Test + fun `captured reads retain parameter identity and shadowed names stay separate`() { + val source = + """ + module Presentation { + Int run(Int /*outer*/value) { + function Int() fn = () -> /*capture*/value; + function Int(Int) other = (Int value) -> value; + return fn() + other(1); + } + } + """.trimIndent() + withSource(source) { adapter -> + val outer = at(source, "outer") + val highlights = adapter.getDocumentHighlights(URI, outer.line, outer.column) + assertThat(highlights.map { it.range.start }) + .containsExactly(outer, at(source, "capture")) + val capture = + decode(adapter.getSemanticTokens(URI)!!).single { + it.take(2) == at(source, "capture").let { listOf(it.line, it.column) } + } + assertThat(SemanticTokenLegend.tokenTypes[capture[3]]).isEqualTo("parameter") + } + } + + @Test + fun `failed inference never becomes an Object type hint or a token for the unresolved name`() { + XdkAdapter().use { adapter -> + val source = "module Presentation { void run() { var value = /*unknown*/missing; } }" + assertThat(adapter.compile(URI, source).success).isFalse() + assertThat(adapter.getInlayHints(URI, ALL)).isEmpty() + val unknown = at(source, "unknown") + assertThat(decode(adapter.getSemanticTokens(URI)!!).map { it.take(2) }) + .doesNotContain(listOf(unknown.line, unknown.column)) + } + } + + @Test + fun `failed replacement and close never keep previous tokens or hints`() { + val source = "module Presentation { Int run() { var count = 1; return count; } }" + withSource(source) { adapter -> + assertThat(adapter.getInlayHints(URI, ALL)).hasSize(1) + assertThat(adapter.compile(URI, "module Presentation {").success).isFalse() + assertThat(adapter.getInlayHints(URI, ALL)).isEmpty() + // Current lexical keywords remain; no previous resolved names may leak into the failed + // replacement. + assertThat( + adapter + .getSemanticTokens(URI) + ?.let(::decode) + .orEmpty() + .map { it.take(2) }, + ).doesNotContain(listOf(0, source.indexOf("count"))) + adapter.closeDocument(URI) + assertThat(adapter.getSemanticTokens(URI)).isNull() + } + } + + private fun withSource( + source: String, + test: (XdkAdapter) -> Unit, + ) { + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, source) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + test(adapter) + } + } + + private fun at( + source: String, + marker: String, + ): Position { + val token = "/*$marker*/" + val offset = source.indexOf(token).also { check(it >= 0) } + token.length + return Position( + source.take(offset).count { it == '\n' }, + offset - source.lastIndexOf('\n', offset - 1) - 1, + ) + } + + private fun decode(tokens: SemanticTokens): List> = + tokens.data + .chunked(5) + .runningFold(listOf(0, 0, 0, 0, 0)) { previous, delta -> + listOf( + previous[0] + delta[0], + if (delta[0] == 0) previous[1] + delta[1] else delta[1], + ) + delta.drop(2) + }.drop(1) + + private companion object { + const val URI = "file:///Presentation.x" + val ALL = Range(Position(0, 0), Position(100, 0)) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt new file mode 100644 index 00000000000..6a2451a7686 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt @@ -0,0 +1,112 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkPrimaryParameterRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(ints = [0, 1, 2]) + fun `header label and property select the same proven rename`(occurrence: Int) { + val text = + "module App { class Box(Int input) {} Box make() = new Box(input = 1); Int use(Box box) = box.input; }" + workspace(mapOf("App" to text)) { adapter -> + val at = + Regex("\\binput\\b") + .findAll(text) + .toList()[occurrence] + .range.first + val edit = requireNotNull(adapter.rename(uri("App"), 0, at, "value")) + assertThat(apply(text, edit, "App")).isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `closed consumer labels use the emitted shorthand property association`() { + val library = + "module Library { class Box(Int input, Int count = 1) {} class Other(Int input) {} }" + val consumer = + "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, input = 1); " + + "Int read(lib.Box box) = box.input; lib.Other other() = new lib.Other(input = 3); }" + workspace(mapOf("Library" to library, "Consumer" to consumer)) { adapter -> + val edit = + requireNotNull(adapter.rename(uri("Library"), 0, library.indexOf("input"), "value")) + assertThat(edit.changes.keys).containsExactlyInAnyOrder(uri("Library"), uri("Consumer")) + assertThat(apply(library, edit, "Library")) + .contains("Box(Int value", "Other(Int input)") + assertThat(apply(consumer, edit, "Consumer")) + .contains("count = 2, value = 1", "box.value", "Other(input = 3)") + } + } + + @Test + fun `generic shorthand properties preserve type arguments and default slots`() { + val text = + "module App { class Box(Element input, Int count = 1) {} " + + "Box make() = new Box(input = \"yes\"); }" + workspace(mapOf("App" to text)) { adapter -> + val edit = + requireNotNull(adapter.rename(uri("App"), 0, text.lastIndexOf("input"), "value")) + assertThat(apply(text, edit, "App")).isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `colliding property and captured local changes are rejected`() { + val text = + "module App { class Box(Int input, Int count) { Int read() { Int local = 7; return input + local; } } }" + workspace(mapOf("App" to text)) { adapter -> + listOf("count", "local").forEach { name -> + assertThat(adapter.rename(uri("App"), 0, text.indexOf("input"), name)).isNull() + } + } + } + + private fun workspace( + sources: Map, + check: (XdkAdapter) -> Unit, + ) { + directory = directory.toRealPath() + sources.forEach { (module, text) -> + directory.resolve("$module.x").toFile().writeText(text) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val first = sources.entries.first() + assertThat(adapter.compile(uri(first.key), first.value).diagnostics).isEmpty() + check(adapter) + sources.forEach { (module, text) -> + assertThat(directory.resolve("$module.x").toFile().readText()).isEqualTo(text) + } + } + } + + private fun apply( + text: String, + edit: WorkspaceEdit, + module: String, + ): String = + edit.changes + .getValue(uri(module)) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } + + private fun uri(module: String): String = + directory + .resolve("$module.x") + .toFile() + .toURI() + .toString() +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt new file mode 100644 index 00000000000..98425dc7c3c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt @@ -0,0 +1,99 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkProject +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Files +import java.nio.file.Path + +class XdkProjectGraphTest { + @TempDir lateinit var directory: Path + + @Test + fun `watch roots include unopened sources and missing resources`() { + val source = directory.resolve("external/sources/Library.x") + val assets = directory.resolve("generated/assets") + val project = + XdkProject( + listOf( + XdkSourceModule( + "Library", + source.toUri().toString(), + resourceRoots = listOf(assets.toUri().toString()), + ), + ), + ) + assertThat(project.inputWatchRoots()) + .containsExactlyInAnyOrder( + source.parent.toFile().canonicalFile, + assets.toFile().canonicalFile, + ) + } + + @Test + fun `resource membership survives missing source containers and generated ancestors`() { + val container = Files.createDirectory(directory.resolve("source")) + val root = container.resolve("App.x") + val assets = directory.resolve("generated/assets") + val implicit = XdkProject(listOf(XdkSourceModule("App", root.toUri().toString()))) + Files.delete(container) + assertThat(implicit.inputWatchRoots()).contains(container.toFile().canonicalFile) + assertThat(implicit.resourceScopes(container.toUri().toString())) + .contains( + root + .toFile() + .canonicalFile + .toURI() + .toString(), + ) + val explicit = + XdkProject( + listOf( + XdkSourceModule( + "App", + root.toUri().toString(), + resourceRoots = listOf(assets.toUri().toString()), + ), + ), + ) + assertThat(explicit.resourceScopes(assets.parent.toUri().toString())) + .containsExactly( + root + .toFile() + .canonicalFile + .toURI() + .toString(), + ) + assertThat(explicit.resourceScopes(directory.resolve("unrelated").toUri().toString())) + .isEmpty() + } + + @Test + fun `affected modules include transitive diamond consumers once in dependency order`() { + fun module( + name: String, + vararg dependencies: String, + ) = XdkSourceModule( + name, + directory.resolve("$name.x").toUri().toString(), + dependencies.toSet(), + ) + + val base = module("Base") + val left = module("Left", "Base") + val right = module("Right", "Base") + val leaf = module("Leaf", "Left", "Right") + val unrelated = module("Unrelated") + val project = XdkProject(listOf(unrelated, leaf, right, left, base)) + + assertThat(project.affected(base.uri)) + .containsExactly(base.uri, left.uri, right.uri, leaf.uri) + assertThat(project.affected(left.uri)).containsExactly(left.uri, leaf.uri) + assertThat(project.affected(leaf.uri)).containsExactly(leaf.uri) + assertThat(project.affected(unrelated.uri)).containsExactly(unrelated.uri) + assertThat(project.affected("untitled:Standalone.x")) + .containsExactly("untitled:Standalone.x") + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt new file mode 100644 index 00000000000..f8c2735ee73 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt @@ -0,0 +1,104 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.awaitility.Awaitility.await +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkProject +import org.xvm.lsp.adapter.xdk.XdkProjectQueries +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.lang.ref.WeakReference +import java.nio.file.Path +import java.time.Duration +import java.util.concurrent.CancellationException +import java.util.concurrent.atomic.AtomicBoolean + +/** A bounded graph proof workload, independent of editor focus and client-side caches. */ +class XdkProjectProofMemoryTest { + @TempDir lateinit var directory: Path + + enum class Outcome { + SUCCESS, + REJECTED, + CANCELLED, + FAILURE, + } + + @ParameterizedTest + @EnumSource(Outcome::class) + fun `property rename across 24 independent roots releases proof attempts`(outcome: Outcome) { + CompilerTestSupport.configure() + val observed = mutableListOf>() + val cancelled = AtomicBoolean() + val modules = + (0 until 24).map { index -> + val file = directory.toRealPath().resolve("Root$index.x").toFile() + file.writeText( + "module Root$index { class Box { Int number = 1; Int read() = number; } }", + ) + XdkSourceModule("Root$index", file.toURI().toString()) + } + val query = + XdkProjectQueries( + XdkProject(modules), + emptyMap(), + XdkDependencies(emptyList()), + { sources, repository, errors -> + // No configured root imports another: the proof must not deserialize preceding + // roots just to leave their declarations and pools unused in this compilation. + assertThat(repository?.moduleNames).isEmpty() + EmbeddingSupport.instance().compileModule(sources, repository, errors).also { compilation -> + observed += WeakReference(compilation) + compilation.pool()?.let { observed += WeakReference(it) } + compilation.sourceTrees().forEach { observed += WeakReference(it) } + if (observed.size > 24 * 3) { + when (outcome) { + Outcome.CANCELLED -> cancelled.set(true) + Outcome.FAILURE -> error("proof compiler failure") + else -> Unit + } + } + } + }, + cancelled::get, + ) + val target = modules.first() + val at = "module Root0 { class Box { Int ".length + when (outcome) { + Outcome.SUCCESS -> { + val edit = requireNotNull(query.rename(target.uri, 0, at, "count")) + assertThat(edit.changes.keys).containsExactly(target.uri) + assertThat(edit.changes.getValue(target.uri)).hasSize(2) + } + + Outcome.REJECTED -> { + assertThat(query.rename(target.uri, 0, at, "read")).isNull() + } + + Outcome.CANCELLED -> { + assertThatThrownBy { query.rename(target.uri, 0, at, "count") } + .isInstanceOf(CancellationException::class.java) + } + + Outcome.FAILURE -> { + assertThatThrownBy { query.rename(target.uri, 0, at, "count") } + .isInstanceOf(IllegalStateException::class.java) + .hasMessage("proof compiler failure") + } + } + assertThat(observed).hasSizeGreaterThanOrEqualTo(48) + await().atMost(Duration.ofSeconds(20)).pollInterval(Duration.ofMillis(100)).untilAsserted { + System.gc() + assertThat(observed.count { it.get() != null }) + .describedAs("retained graph proof attempts, pools and AST roots") + .isZero() + } + println( + "24-root $outcome property proof: compiler objects released=${observed.size}, heap budget=${Runtime.getRuntime().maxMemory()} bytes", + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt new file mode 100644 index 00000000000..88eeafa1dd6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt @@ -0,0 +1,301 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class XdkProjectQueryLifecycleTest { + @TempDir lateinit var directory: Path + + enum class Feature { + REFERENCES, + CODE_LENSES, + RENAME, + } + + private enum class Change { + EDIT, + CLOSE, + CONFIGURATION, + REPOSITORY, + CANCEL, + } + + @Test + fun `real adapter queries include unopened modules without installing proof results`() { + Session().use { session -> + val adapter = session.adapter + assertThat( + adapter + .findReferences(session.libraryUri, 0, LIBRARY.indexOf("pick"), true) + .map { it.uri }, + ).containsExactlyInAnyOrder(session.libraryUri, session.consumerUri) + val edit = + requireNotNull( + adapter.rename(session.libraryUri, 0, LIBRARY.indexOf("pick"), "choose"), + ) + assertThat(edit.changes.keys) + .containsExactlyInAnyOrder(session.libraryUri, session.consumerUri) + assertThat(adapter.getCachedResult(session.consumerUri)).isNull() + assertThat(adapter.getCachedResult(session.libraryUri)?.diagnostics).isEmpty() + assertThat(session.consumer.readText()).isEqualTo(CONSUMER) + } + } + + @Test + fun `graph queries use unsaved consumer text and return its URI alias`() { + Session().use { session -> + val alias = + session.consumer + .toPath() + .toUri() + .toString() + val overlay = CONSUMER.replace("box.pick(1)", "box.pick(1) + box.pick(2)") + assertThat(session.adapter.compile(alias, overlay).success).isTrue() + val references = + session.adapter.findReferences( + session.libraryUri, + 0, + LIBRARY.indexOf("pick"), + false, + ) + assertThat(references).hasSize(2) + assertThat(references.map { it.uri }).containsOnly(alias) + val edit = + requireNotNull( + session.adapter.rename(session.libraryUri, 0, LIBRARY.indexOf("pick"), "choose"), + ) + assertThat(edit.changes.getValue(alias)).hasSize(2) + assertThat(session.consumer.readText()).isEqualTo(CONSUMER) + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `changes in another module and client cancellation retire blocked graph work`(feature: Feature) { + for (change in Change.entries) { + Session().use { session -> + val retained = session.adapter.getCachedResult(session.libraryUri) + session.hold.set(true) + val query = session.request(feature) + assertThat(session.entered.await(20, SECONDS)) + .describedAs("$feature / $change") + .isTrue() + when (change) { + Change.EDIT -> { + session.adapter.compileAsync(session.consumerUri, "\n$CONSUMER") + } + + Change.CLOSE -> { + session.adapter.closeDocument(session.consumerUri) + } + + Change.CONFIGURATION -> { + session.adapter.replaceSourceModules(listOf(session.libraryModule)) + } + + Change.REPOSITORY -> { + session.adapter.replaceDependencies(listOf(session.other)) + } + + Change.CANCEL -> { + query.cancel(false) + } + } + assertThat(query.isCancelled).describedAs("$feature / $change").isTrue() + session.release.countDown() + assertThat(session.adapter.getCachedResult(session.libraryUri)).isEqualTo(retained) + if (change == Change.CONFIGURATION) { + // The independent library stays valid, but graph queries must use the new graph. + when (feature) { + Feature.REFERENCES -> { + assertThat( + session.adapter + .findReferences(session.libraryUri, 0, LIBRARY.indexOf("pick"), true) + .map { it.uri }, + ).containsExactly(session.libraryUri) + } + + Feature.CODE_LENSES -> { + val lenses = session.adapter.getCodeLenses(session.libraryUri) + assertThat(lenses.filter { it.command?.command == "xtc.showReferences" }) + .allMatch { it.command!!.title == "0 references" } + } + + Feature.RENAME -> { + assertThat( + session.adapter + .rename(session.libraryUri, 0, LIBRARY.indexOf("pick"), "choose") + ?.changes + ?.keys, + ).containsExactly(session.libraryUri) + } + } + } + } + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `closed disk changes without watcher delivery reject the captured graph`(feature: Feature) { + Session().use { session -> + session.hold.set(true) + val query = session.request(feature) + assertThat(session.entered.await(20, SECONDS)).isTrue() + session.consumer.writeText("\n$CONSUMER") + session.release.countDown() + val result = query.get(30, SECONDS) + when (feature) { + Feature.REFERENCES -> { + assertThat(result as List<*>).isEmpty() + } + + Feature.CODE_LENSES -> { + assertThat((result as List<*>).filterIsInstance().map { it.command?.command }) + .containsExactly("xtc.runModule") + } + + Feature.RENAME -> { + assertThat(result).isNull() + } + } + assertThat(session.adapter.getCachedResult(session.libraryUri)?.diagnostics).isEmpty() + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `a newer query supersedes blocked work but preserves shared analysis`(feature: Feature) { + Session().use { session -> + session.hold.set(true) + val old = session.request(feature) + assertThat(session.entered.await(20, SECONDS)).isTrue() + val current = session.request(feature) + assertThat(old.isCancelled).isTrue() + session.release.countDown() + assertThat(current.get(30, SECONDS)).isNotNull() + assertThat(session.adapter.getCachedResult(session.libraryUri)?.success).isTrue() + } + } + + @Test + fun `background code actions do not cancel a quick fix requested at another range`() { + Session().use { session -> + session.hold.set(true) + val quickFix = + session.adapter.getCodeActionsAsync( + session.libraryUri, + Range(Position(0, 25), Position(0, 29)), + emptyList(), + ) + assertThat(session.entered.await(20, SECONDS)).isTrue() + val background = + session.adapter.getCodeActionsAsync( + session.libraryUri, + Range(Position(0, 0), Position(0, 0)), + emptyList(), + ) + assertThat(quickFix.isCancelled).isFalse() + session.release.countDown() + assertThat(quickFix.get(30, SECONDS)).allMatch { + it.kind == CodeAction.CodeActionKind.REFACTOR_REWRITE + } + assertThat(background.get(30, SECONDS).map { it.title }).containsExactly("Generate documentation comment") + } + } + + private inner class Session : AutoCloseable { + val library = + directory + .toRealPath() + .resolve("Library.x") + .toFile() + .apply { writeText(LIBRARY) } + val consumer = + directory + .toRealPath() + .resolve("Consumer.x") + .toFile() + .apply { writeText(CONSUMER) } + val libraryUri = library.toURI().toString() + val consumerUri = consumer.toURI().toString() + val libraryModule = XdkSourceModule("Library", libraryUri) + val hold = AtomicBoolean() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + private val support = EmbeddingSupport.instance() + val other = + run { + CompilerTestSupport.configure() + support + .compileModule(Source("module Other {}", "Other.x"), null, ErrorList()) + .toDependency() + } + val adapter = + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + if (hold.compareAndSet(true, false)) { + entered.countDown() + check(release.await(20, SECONDS)) + // Even a compiler callback ignoring cancellation cannot publish an obsolete + // proof. + support.compileModule(sources, repository, ErrorList()) + } else { + support.compileModule(sources, repository, errors) + } + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ) + + init { + adapter.replaceSourceModules( + listOf(libraryModule, XdkSourceModule("Consumer", consumerUri, setOf("Library"))), + ) + val result = adapter.compile(libraryUri, LIBRARY) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + } + + fun request(feature: Feature): CompletableFuture<*> = + when (feature) { + Feature.REFERENCES -> { + adapter.findReferencesAsync(libraryUri, 0, LIBRARY.indexOf("pick"), true) + } + + Feature.CODE_LENSES -> { + adapter.getCodeLensesAsync(libraryUri, true) + } + + Feature.RENAME -> { + adapter.renameAsync(libraryUri, 0, LIBRARY.indexOf("pick"), "choose") + } + } + + override fun close() { + release.countDown() + adapter.close() + } + } + + private companion object { + const val LIBRARY = "module Library { class Box { Int pick(Int value) = value; } }" + const val CONSUMER = + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt new file mode 100644 index 00000000000..3656c484d05 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt @@ -0,0 +1,573 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependencies +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.XdkProject +import org.xvm.lsp.adapter.xdk.XdkProjectQueries +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.io.File +import java.nio.file.Path + +class XdkProjectQueryTest { + @TempDir lateinit var directory: Path + + @Test + fun `graph proofs cannot resolve an undeclared import from an unrelated earlier root`() { + CompilerTestSupport.configure() + val library = + source("Library", "module Library { class Box { Int pick(Int value) = value; } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }", + ) + + fun graph(dependencies: Set) = + XdkProjectQueries( + XdkProject( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString(), dependencies), + ), + ), + emptyMap(), + XdkDependencies(emptyList()), + { sources, repository, errors -> + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { false }, + ) + + val incomplete = graph(emptySet()) + assertThat( + incomplete.rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ).isNull() + assertThat(incomplete.symbols("run")).isEmpty() + val configured = graph(setOf("Library")) + val edit = + requireNotNull( + configured.rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ) + assertThat(edit.changes.keys) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + } + + @Test + fun `implicit packages retain companion references without becoming workspace documents`() { + val library = source("Library", "module Library { Int run() = tools.Helper.pick(); }") + val helper = + directory.toRealPath().resolve("Library/tools/Helper.x").toFile().apply { + parentFile.mkdirs() + writeText("class Helper { static Int pick() = 42; }") + } + val query = query(library) + assertThat(query.diagnostics()).isNotEmpty().allMatch { it.success } + val references = query.references(library.toURI().toString(), 0, library.readText().indexOf("pick"), true) + assertThat(references.map { it.uri }).containsExactlyInAnyOrder(library.toURI().toString(), helper.toURI().toString()) + assertThat(query.symbols("Helper").map { it.location.uri }).containsExactly(helper.toURI().toString()) + } + + @Test + fun `references join serialized identities across unopened consumers and separate overloads`() { + val library = + source( + "Library", + "module Library { class Box { Int pick(Int value) = value; String pick(String value) = value; } }", + ) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }", + ) + val other = + source( + "Other", + "module Other { package lib import Library; String run(lib.Box box) = box.pick(\"text\"); }", + ) + val query = query(library, consumer, other) + val references = + query.references( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + true, + ) + assertThat(references.map { it.uri }) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + assertThat( + query + .references( + consumer.toURI().toString(), + 0, + consumer.readText().indexOf("pick"), + false, + ).map { it.uri }, + ).containsExactly(consumer.toURI().toString()) + } + + @Test + fun `member rename rebuilds consumers without changing a same-name overload`() { + val library = + source( + "Library", + "module Library { class Box { Int pick(Int value) = value; String pick(String value) = value; } }", + ) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }", + ) + val other = + source( + "Other", + "module Other { package lib import Library; String run(lib.Box box) = box.pick(\"text\"); }", + ) + val edit = + requireNotNull( + query(library, consumer, other) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + assertThat(apply(library, edit)) + .contains("Int choose(Int value)", "String pick(String value)") + assertThat(apply(consumer, edit)).contains("box.choose(1)") + assertThat(library.readText()).contains("Int pick(Int value)") + } + + @Test + fun `override rename includes base and child declarations and both dispatch entry points`() { + val library = + source("Library", "module Library { class Base { Int pick(Int value) = value; } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; " + + "class Child extends lib.Base { @Override Int pick(Int value) = value + 1; } " + + "Int run(lib.Base base, Child child) = base.pick(1) + child.pick(2); }", + ) + val edit = + requireNotNull( + query(library, consumer) + .rename( + consumer.toURI().toString(), + 0, + consumer.readText().indexOf("pick"), + "choose", + ), + ) + assertThat(edit.changes.getValue(library.toURI().toString())).hasSize(1) + assertThat(edit.changes.getValue(consumer.toURI().toString())).hasSize(3) + assertThat(apply(consumer, edit)) + .contains("Int choose(Int value)", "base.choose(1)", "child.choose(2)") + } + + @Test + fun `generic interface contracts and concrete overrides belong to the same rename family`() { + val library = + source("Library", "module Library { interface Mapper { T map(T value); } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; " + + "class Mapper implements lib.Mapper { @Override String map(String value) = value; } " + + "String run(lib.Mapper api, Mapper impl) = api.map(\"a\") + impl.map(\"b\"); }", + ) + val edit = + requireNotNull( + query(library, consumer) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("map("), + "convert", + ), + ) + assertThat(edit.changes.getValue(consumer.toURI().toString())).hasSize(3) + assertThat(apply(consumer, edit)) + .contains("String convert(String value)", "api.convert(\"a\")", "impl.convert(\"b\")") + } + + @Test + fun `super calls preserve their selected body while renaming an override family`() { + val library = + source("Library", "module Library { class Base { Int pick(Int value) = value; } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; " + + "class Child extends lib.Base { @Override Int pick(Int value) = super(value); } " + + "Int run(Child child) = child.pick(value = 1); }", + ) + val base = artifact("Library", library.readText()) + artifact("Consumer", consumer.readText(), base) + val edit = + requireNotNull( + query(library, consumer) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ) + assertThat(apply(consumer, edit)) + .contains("Int choose(Int value) = super(value)", "child.choose(value = 1)") + } + + @Test + fun `a compiling rename that captures an untouched consumer call is rejected`() { + val library = + source( + "Library", + "module Library { class Box { Int pick(Int value) = value; Int choose(Object value) = 0; } }", + ) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Box box) = box.choose(1); }", + ) + val query = query(library, consumer) + assertThat( + query.rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ).isNull() + // Success alone is insufficient: the unchanged call now selects the renamed Int overload. + val changed = artifact("Library", library.readText().replace("pick", "choose")) + artifact("Consumer", consumer.readText(), changed) + } + + @Test + fun `a compiling rename that merges independent abstract dispatch contracts is rejected`() { + val library = + source( + "Library", + "module Library { interface First { Int pick(Int value); } interface Second { Int choose(Int value); } }", + ) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; interface Both extends lib.First, lib.Second {} }", + ) + val query = query(library, consumer) + assertThat( + query.rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ).isNull() + val changed = artifact("Library", library.readText().replace("pick", "choose")) + artifact("Consumer", consumer.readText(), changed) + } + + @Test + fun `a source override of an external binary contract cannot be renamed`() { + val binary = + artifact("Library", "module Library { interface API { Int pick(Int value); } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; class Box implements lib.API { @Override Int pick(Int value) = value; } }", + ) + val query = + XdkProjectQueries( + XdkProject( + listOf( + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + ), + ), + emptyMap(), + XdkDependencies(listOf(binary)), + { sources, repository, errors -> + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { false }, + ) + assertThat( + query.rename( + consumer.toURI().toString(), + 0, + consumer.readText().indexOf("pick"), + "choose", + ), + ).isNull() + } + + @Test + fun `bundled XDK members resolve across modules but binaries and their contracts cannot be renamed`() { + val library = + source( + "Library", + "module Library { conditional Int run(String text) = text.indexOf('a'); }", + ) + val consumer = + source( + "Consumer", + "module Consumer { class Named { @Override String toString() = \"name\"; } " + + "conditional Int run(String text) = text.indexOf('b'); }", + ) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString()), + ), + ) + for (file in listOf(library, consumer)) { + val result = adapter.compile(file.toURI().toString(), file.readText()) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + val at = file.readText().indexOf("indexOf") + assertThat( + adapter + .getSignatureHelp(file.toURI().toString(), 0, at + "indexOf(".length) + ?.signatures + ?.single() + ?.label, + ).contains("indexOf", "Char") + assertThat(adapter.prepareRename(file.toURI().toString(), 0, at)).isNull() + assertThat(adapter.rename(file.toURI().toString(), 0, at, "locate")).isNull() + } + val references = + adapter.findReferences( + library.toURI().toString(), + 0, + library.readText().indexOf("indexOf"), + true, + ) + // Source occurrences of the same resolved binary member join; there is no source + // declaration to invent. + assertThat(references.map { it.uri }) + .containsExactlyInAnyOrder(library.toURI().toString(), consumer.toURI().toString()) + val at = consumer.readText().indexOf("toString") + assertThat(adapter.getHoverInfo(consumer.toURI().toString(), 0, at)) + .contains("String", "toString") + assertThat(adapter.rename(consumer.toURI().toString(), 0, at, "describe")).isNull() + } + } + + @Test + fun `references and override edits include closed members of transitive consumers`() { + val library = + source("Library", "module Library { class Base { Int pick(Int value) = value; } }") + val bridge = + source( + "Bridge", + "module Bridge { package lib import Library; class Middle extends lib.Base {} }", + ) + val consumer = + source( + "Consumer", + "module Consumer { package mid import Bridge; Int run(Child child) = child.pick(1); }", + ) + val member = + directory.toRealPath().resolve("Consumer/Child.x").toFile().apply { + parentFile.mkdirs() + writeText( + "class Child extends mid.Middle { @Override Int pick(Int value) = value; }", + ) + } + CompilerTestSupport.configure() + val query = + XdkProjectQueries( + XdkProject( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Bridge", bridge.toURI().toString(), setOf("Library")), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Bridge")), + ), + ), + emptyMap(), + XdkDependencies(emptyList()), + { sources, repository, errors -> + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { false }, + ) + assertThat( + query + .references( + member.toURI().toString(), + 0, + member.readText().indexOf("pick"), + true, + ).map { it.uri }, + ).containsExactlyInAnyOrder(member.toURI().toString(), consumer.toURI().toString()) + val edit = + requireNotNull( + query.rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ) + assertThat(edit.changes.keys) + .containsExactlyInAnyOrder( + library.toURI().toString(), + member.toURI().toString(), + consumer.toURI().toString(), + ) + } + + @Test + fun `invalid identifiers and incomplete consumers reject method edits`() { + val library = + source("Library", "module Library { class Box { Int pick(Int value) = value; } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }", + ) + for (name in listOf("return", "bad name", "choose()")) { + assertThat( + query(library, consumer) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + name, + ), + ).isNull() + } + consumer.writeText(consumer.readText().replace("box.pick(1)", "box.")) + assertThat( + query(library, consumer) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + "choose", + ), + ).isNull() + assertThat( + query(library, consumer) + .references( + library.toURI().toString(), + 0, + library.readText().indexOf("pick"), + true, + ), + ).isEmpty() + } + + @Test + fun `delegated method rename joins contracts and concrete receiver implementations across modules`() { + val library = source("Library", "module Library { interface Api { Int read(); } }") + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; " + + "class Actual implements lib.Api { @Override Int read() = 1; } " + + "class Forward(Actual target) delegates lib.Api(target) {} " + + "Int use(Forward value) = value.read(); }", + ) + val edit = + requireNotNull( + query(library, consumer) + .rename( + library.toURI().toString(), + 0, + library.readText().indexOf("read"), + "fetch", + ), + ) + assertThat(apply(library, edit)).contains("Int fetch()") + assertThat(apply(consumer, edit)).contains("Int fetch()", "value.fetch()") + } + + private fun artifact( + name: String, + text: String, + vararg dependencies: XdkDependency, + ): XdkDependency { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport + .instance() + .compileModule( + Source(text, "$name.x"), + XdkDependencies(dependencies.toList()).open().repository, + errors, + ) + assertThat(compilation.succeeded()).describedAs(errors.toString()).isTrue() + return compilation.toDependency() + } + + private fun source( + name: String, + text: String, + ): File = + directory + .toRealPath() + .resolve("$name.x") + .toFile() + .apply { writeText(text) } + + private fun query(vararg files: File): XdkProjectQueries { + CompilerTestSupport.configure() + val modules = + files.mapIndexed { index, file -> + val dependencies = if (index == 0) emptySet() else setOf(files[0].nameWithoutExtension) + XdkSourceModule( + file.nameWithoutExtension, + file.toURI().toString(), + dependencies, + ) + } + return XdkProjectQueries( + XdkProject(modules), + emptyMap(), + XdkDependencies(emptyList()), + { sources, repository, errors -> + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { false }, + ) + } + + private fun apply( + file: File, + edit: WorkspaceEdit, + ): String = + edit.changes + .getValue(file.toURI().toString()) + .sortedByDescending { it.range.start.column } + .fold(file.readText()) { text, change -> + text.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt new file mode 100644 index 00000000000..b8132d056be --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt @@ -0,0 +1,302 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.io.File +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CancellationException +import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicInteger + +class XdkProjectTest { + @TempDir lateinit var directory: Path + + @Test + fun `source artifacts are reused only when captured sources and dependency revisions match`() { + val (library, consumer) = fixture() + val calls = ConcurrentHashMap() + val support = EmbeddingSupport.instance() + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + calls + .computeIfAbsent(sources.sourceFile.name) { AtomicInteger() } + .incrementAndGet() + support.compileModule(sources, repository, errors) + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ).use { adapter -> + configure(adapter, library, consumer) + assertThat(adapter.compile(consumer.toURI().toString(), CONSUMER).success).isTrue() + assertThat(adapter.compile(consumer.toURI().toString(), "\n$CONSUMER").success) + .isTrue() + assertThat(calls.getValue("Library.x").get()).isEqualTo(1) + assertThat(calls.getValue("Consumer.x").get()).isEqualTo(2) + library.writeText(INCOMPATIBLE) + assertThat(adapter.compile(consumer.toURI().toString(), CONSUMER).success).isFalse() + assertThat(calls.getValue("Library.x").get()).isEqualTo(2) + } + } + + @Test + fun `rapid edits cancel obsolete futures and coalesce before entering the compiler`() { + val (library, consumer) = fixture() + val calls = AtomicInteger() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val support = EmbeddingSupport.instance() + XdkAdapter( + { source, repository, errors -> + entered.countDown() + check(release.await(10, SECONDS)) + support.compileModule(source, repository, errors) + }, + { sources, repository, errors -> + calls.incrementAndGet() + support.compileModule(sources, repository, errors) + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ).use { adapter -> + configure(adapter, library, consumer) + val blocker = adapter.compileAsync("untitled:Hold.x", "module Hold {}") + try { + check(entered.await(10, SECONDS)) + val requests = + (1..30).map { + adapter.compileAsync(consumer.toURI().toString(), CONSUMER + " // $it") + } + release.countDown() + assertThat(blocker.get(30, SECONDS).success).isTrue() + assertThat(requests.last().get(30, SECONDS).success).isTrue() + assertThat(requests.dropLast(1)).allMatch { it.isCancelled } + assertThat(calls.get()).isEqualTo(2) + } finally { + release.countDown() + } + } + } + + @Test + fun `all closed source members are captured before the first dependency enters compilation`() { + val (library, consumer) = fixture() + val member = File(consumer.parentFile, "Consumer/Extra.x") + member.parentFile.mkdirs() + member.writeText("class Extra {}") + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val held = AtomicBoolean() + val support = EmbeddingSupport.instance() + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + if (!held.getAndSet(true)) { + entered.countDown() + check(release.await(10, SECONDS)) + } + support.compileModule(sources, repository, errors) + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ).use { adapter -> + configure(adapter, library, consumer) + val first = adapter.compileAsync(consumer.toURI().toString(), CONSUMER) + try { + check(entered.await(10, SECONDS)) + member.writeText("class Extra { MissingType broken; }") + release.countDown() + assertThat(first.get(30, SECONDS).success).isTrue() + assertThat(adapter.compile(consumer.toURI().toString(), CONSUMER).diagnostics) + .anyMatch { it.code == "COMPILER-38" } + } finally { + release.countDown() + } + } + } + + @Test + fun `an uncooperative old dependency compilation cannot cache or publish stale facts`() { + val (library, consumer) = fixture() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val held = AtomicBoolean() + val support = EmbeddingSupport.instance() + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + if (!held.getAndSet(true)) { + entered.countDown() + check(release.await(10, SECONDS)) + support.compileModule(sources, repository, ErrorList()) + } else { + support.compileModule(sources, repository, errors) + } + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ).use { adapter -> + configure(adapter, library, consumer) + val old = adapter.compileAsync(consumer.toURI().toString(), CONSUMER) + try { + check(entered.await(10, SECONDS)) + adapter.compileAsync(library.toURI().toString(), INCOMPATIBLE) + val latest = adapter.compileAsync(consumer.toURI().toString(), CONSUMER) + assertThat(old.isCancelled).isTrue() + assertThat(adapter.getCachedResult(consumer.toURI().toString())).isNull() + release.countDown() + assertThat(latest.get(30, SECONDS).success).isFalse() + assertThat(adapter.getCachedResult(consumer.toURI().toString())?.success) + .isFalse() + } finally { + release.countDown() + } + } + } + + @Test + fun `dependency edits cancel cursor work and new probes see rebuilt member types`() { + val (library, consumer) = fixture() + library.writeText("module Library { class Box { Int number = 1; } }") + val source = + "module Consumer { package lib import Library; void run(lib.Box box) { box. } }" + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val held = AtomicBoolean() + val support = EmbeddingSupport.instance() + XdkAdapter( + { text, repository, errors -> support.compileModule(text, repository, errors) }, + { sources, repository, errors -> + support.compileModule(sources, repository, errors) + }, + { text, sources, cursor, repository, errors -> + if (!held.getAndSet(true)) { + entered.countDown() + check(release.await(10, SECONDS)) + } + support.analyzeIncomplete( + sources, + File(URI(text.fileName)), + cursor, + repository, + errors, + ) + }, + ).use { adapter -> + configure(adapter, library, consumer) + adapter.compile(consumer.toURI().toString(), source) + val cursor = source.indexOf("box.") + 4 + val old = adapter.getCompletionsAsync(consumer.toURI().toString(), 0, cursor, ".") + try { + check(entered.await(10, SECONDS)) + adapter.compileAsync( + library.toURI().toString(), + "module Library { class Box { String label = \"new\"; } }", + ) + assertThat(old.isCompletedExceptionally).isTrue() + assertThatThrownBy { old.join() } + .hasRootCauseInstanceOf(CancellationException::class.java) + release.countDown() + adapter.compile(consumer.toURI().toString(), source) + val candidates = + adapter + .getCompletionsAsync(consumer.toURI().toString(), 0, cursor, ".") + .get(20, SECONDS) + assertThat(candidates.map { it.label }) + .contains("label") + .doesNotContain("number") + } finally { + release.countDown() + } + } + } + + @Test + fun `cyclic duplicate and mismatched module configurations fail explicitly without replacing a valid graph`() { + val (library, consumer) = fixture() + XdkAdapter().use { adapter -> + configure(adapter, library, consumer) + val uri = consumer.toURI().toString() + assertThat(adapter.compile(uri, CONSUMER).success).isTrue() + val cached = adapter.getCachedResult(uri) + assertThatThrownBy { + adapter.replaceSourceModules( + listOf( + XdkSourceModule( + "Library", + library.toURI().toString(), + setOf("Consumer"), + ), + XdkSourceModule("Consumer", uri, setOf("Library")), + ), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + .hasMessageContaining("Cyclic") + assertThatThrownBy { + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", uri), + XdkSourceModule("Library", library.toURI().toString()), + ), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + .hasMessageContaining("Duplicate") + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + adapter.replaceSourceModules( + listOf(XdkSourceModule("Wrong", library.toURI().toString())), + ) + assertThat(adapter.compile(library.toURI().toString(), LIBRARY).diagnostics).anyMatch { + it.code == "PROJECT-MODULE" + } + } + } + + private fun configure( + adapter: XdkAdapter, + library: File, + consumer: File, + ) = adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + ), + ) + + private fun fixture(): Pair { + CompilerTestSupport.configure() + val library = + directory + .toRealPath() + .resolve("Library.x") + .toFile() + .also { it.writeText(LIBRARY) } + val consumer = + directory + .toRealPath() + .resolve("Consumer.x") + .toFile() + .also { it.writeText(CONSUMER) } + return library to consumer + } + + private companion object { + const val LIBRARY = "module Library { static Int value() = 1; }" + const val INCOMPATIBLE = "module Library { static String value() = \"text\"; }" + const val CONSUMER = + "module Consumer { package lib import Library; Int run() = lib.value(); }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt new file mode 100644 index 00000000000..d442e077867 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt @@ -0,0 +1,270 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import java.nio.file.Path +import java.util.concurrent.Executors + +class XdkPropertyArgumentCompletionTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @CsvSource( + delimiter = '|', + value = + [ + "take(|valueNumber", + "take(va|valueNumber", + "take(value = va|valueNumber", + "widen(va|valueNumber", + "box.take(va|valueText", + "new Box(va|valueText", + "fn(va|valueText", + ], + ) + fun `property argument values use compiler fitting and exact edits`( + call: String, + expected: String, + ) { + val header = + "module Editing { " + + "Int valueNumber = 1; String valueText = \"x\"; Boolean valueFlag = True; " + + "void take(Int value) {} void widen(Int128 value) {} " + + "class Box { construct(T value) {} void take(T value) {} } " + + "void run(Box box, function void(String) fn) { " + val prefix = "$header$call" + val typed = if (call.endsWith("va")) "va" else "" + XdkAdapter().use { adapter -> + val original = "$prefix); } }" + val cached = adapter.compile(URI, original) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .describedAs(call) + .containsExactlyElementsOf( + listOf(expected) + if (typed.isEmpty()) listOf("0") else emptyList(), + ) + val item = items.single { it.label == expected } + assertThat(item.kind).isEqualTo(CompletionItem.CompletionKind.PROPERTY) + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range( + Position(0, prefix.length - typed.length), + Position(0, prefix.length), + ), + expected, + ), + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter + .compile( + URI, + original.replaceRange( + prefix.length - typed.length, + prefix.length, + expected, + ), + ).diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "static "]) + fun `implicit reads respect inherited access and availability of this`(modifier: String) { + val prefix = + "module Editing { class Base { protected Int valueProtected = 1; private Int valueHidden = 2; } " + + "class Child extends Base { private Int valueOwn = 3; static Int valueConstant = 4; static String valueText = \"x\"; " + + "static void take(Int value) {} ${modifier}void run() { take(va" + XdkAdapter().use { adapter -> + val text = "$prefix); } } }" + adapter.compile(URI, text) + val items = adapter.getCompletions(URI, 0, prefix.length) + val expected = + if (modifier.isEmpty()) { + listOf("valueConstant", "valueOwn", "valueProtected") + } else { + listOf("valueConstant") + } + assertThat(items.map { it.label }).containsExactlyInAnyOrderElementsOf(expected) + for (item in items) { + assertThat( + adapter + .compile( + URI, + text.replaceRange(prefix.length - 2, prefix.length, item.label), + ).diagnostics, + ).isEmpty() + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int value;", "String value = \"x\";", "Int value = 2;"]) + fun `unreadable or incompatible locals still shadow properties`(local: String) { + val prefix = + "module Editing { Int value = 1; void take(Int input) {} void run() { $local take(va" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + if (local == "Int value = 2;") { + assertThat(items.map { it.label }).containsExactly("value") + assertThat(items.single().kind).isEqualTo(CompletionItem.CompletionKind.VARIABLE) + } else { + assertThat(items).isEmpty() + } + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "if (value.is(String)) { "]) + fun `property completion does not invent local variable narrowing`(guard: String) { + val prefix = + "module Editing { Object value = \"x\"; void take(String text) {} void run() { $guard take(va" + val suffix = if (guard.isEmpty()) "); } }" else "); } } }" + XdkAdapter().use { adapter -> + assertThat( + adapter.compile(URI, "${prefix.dropLast(2)}value$suffix").diagnostics.map { + it.code + }, + ).contains("COMPILER-150") + adapter.compile(URI, "$prefix$suffix") + assertThat(adapter.getCompletions(URI, 0, prefix.length)).isEmpty() + } + } + + @Test + fun `generic inherited property has its receiver substitution`() { + val prefix = + "module Editing { class Base { T value; } class Child extends Base { " + + "void take(String text) {} void run() { take(va" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).containsExactly("value") + assertThat(items.single().detail).contains("String") + assertThat(adapter.compile(URI, "${prefix.dropLast(2)}value); } } }").diagnostics) + .isEmpty() + } + } + + @Test + fun `properties preserve overload alternatives`() { + val prefix = + "module Editing { Int valueNumber = 1; String valueText = \"x\"; Boolean valueFlag = True; " + + "void choose(Int value) {} void choose(String value) {} void run() { choose(va" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("valueNumber", "valueText") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures).hasSize(2) + } + } + + @Test + fun `formal type properties retain their semantic kind as argument values`() { + val prefix = + "module Editing { class Box { void take(Type value) {} void run() { take(Va" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix); } } }") + val item = adapter.getCompletions(URI, 0, prefix.length).single() + assertThat(item.label).isEqualTo("Value") + assertThat(item.kind).isEqualTo(CompletionItem.CompletionKind.CLASS) + assertThat(item.detail).contains("Type") + assertThat(adapter.compile(URI, "${prefix.dropLast(2)}Value); } } }").diagnostics) + .isEmpty() + } + } + + @Test + fun `property values follow unsaved dependency types and keep source identity`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Child.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Base { Int value = 1; } }") + val prefix = "class Child extends Base { void take(String text) {} void run() { take(va" + member.writeText("$prefix); } }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val uri = member.toURI().toString() + adapter.compile(rootUri, root.readText()) + adapter.compile(uri, member.readText()) + assertThat(adapter.getCompletions(uri, 0, prefix.length)).isEmpty() + adapter.compile( + rootUri, + root.readText().replace("Int value = 1", "String value = \"x\""), + ) + val cached = adapter.getCachedResult(uri) + assertThat(adapter.getCompletions(uri, 0, prefix.length).single().detail) + .contains("String") + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + assertThat(adapter.compile(uri, "${prefix.dropLast(2)}value); } }").diagnostics) + .isEmpty() + assertThat(adapter.findDefinition(uri, 0, prefix.length - 1)!!.uri).isEqualTo(rootUri) + adapter.compile(rootUri, root.readText()) + adapter.compile(uri, member.readText()) + assertThat(adapter.getCompletions(uri, 0, prefix.length)).isEmpty() + assertThat(root.readText()).contains("Int value = 1") + } + } + + @Test + fun `accepted property facts are immutable detached and do not change syntax or diagnostics`() { + CompilerTestSupport.configure() + val prefix = + "module Editing { String value = \"x\"; void take(String text) {} void run() { take(va" + val text = "$prefix); } }" + val source = Source(text) + val cursor = Source(text).apply { repeat(prefix.length) { next() } }.position + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + val site = analysis.sites().single() + val binding = analysis.cursorBindings()[site]!! + assertThat(binding.argumentValues()).isEmpty() + assertThat(binding.argumentProperties().map { it.name() }).containsExactly("value") + assertThatThrownBy { (binding.argumentProperties() as MutableList).clear() } + .isInstanceOf(UnsupportedOperationException::class.java) + assertThat( + binding + .withTypes(emptyList()) + .withCandidates(emptyList()) + .withFunctions(emptyList()) + .withArgumentValues(emptyList()) + .argumentProperties(), + ).isEqualTo(binding.argumentProperties()) + assertThat(site.arguments).isEmpty() + assertThat(source.toRawString()).isEqualTo(text) + val snapshot = analysis.semanticSnapshot(errors) + assertThat(snapshot.semantics.calls).isEmpty() + assertThat(snapshot.semantics.functionCalls).isEmpty() + XdkAdapter().use { adapter -> adapter.compile("untitled:Other.x", "module Other {}") } + Executors.newSingleThreadExecutor().use { executor -> + assertThat( + executor + .submit> { + snapshot.sites + .single() + .argumentValues + .map { it.name } + }.get(), + ).containsExactly("value") + } + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt new file mode 100644 index 00000000000..0d07aaae325 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt @@ -0,0 +1,240 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.NamedTypeExpression +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkQualifiedHeaderTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(ecstasy.text.Str| value) {}", + "ecstasy.text.Str| property;", + "ecstasy.text.Str| damaged() = new StringBuffer();", + ], + ) + fun `qualified type prefixes replace only the final token`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('|') + val suffix = declaration.substringAfter('|') + " Int later = 1; }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).describedAs(declaration).contains("StringBuffer") + assertThat(items.single { it.label == "StringBuffer" }.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 3), Position(0, prefix.length)), + "StringBuffer", + ), + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat( + adapter.compile(URI, prefix.dropLast(3) + "StringBuffer" + suffix).diagnostics, + ).isEmpty() + } + } + + @Test + fun `qualified scope includes accessible types and excludes values and private children`() { + val prefix = + "module Headers { class Owner { class ItemPublic {} private class ItemPrivate {} " + + "protected class ItemProtected {} static Int ItemValue = 1; typedef String as ItemAlias; } " + + "void damaged(Owner.Ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix value) {} }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("ItemPublic", "ItemAlias") + } + } + + @ParameterizedTest + @ValueSource(strings = ["Owner", "Alias"]) + fun `class qualifiers and imported aliases include inherited nested types`(qualifier: String) { + val declarations = + "module Headers { import Owner as Alias; class Base { class ItemBase {} } " + + "class Owner extends Base { class ItemOwn {} } " + val prefix = declarations + "void damaged($qualifier.Ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix value) {} }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).containsExactlyInAnyOrder("ItemBase", "ItemOwn") + for (item in items) { + assertThat( + adapter + .compile(URI, prefix.dropLast(3) + item.label + " value) {} }") + .diagnostics, + ).isEmpty() + } + } + } + + @Test + fun `private nested types remain available inside their owning class`() { + val prefix = + "module Headers { class Owner { private class ItemPrivate {} void damaged(Owner.Ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix value) {} } }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("ItemPrivate") + assertThat( + adapter + .compile(URI, prefix.dropLast(3) + "ItemPrivate value) {} } }") + .diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(strings = ["Owner.Hidden.Ite", "Alias.Ite", "missing.Str", "number.Str"]) + fun `inaccessible unknown and value qualifiers do not fall back to enclosing names`(qualified: String) { + val alias = if (qualified.startsWith("Alias.")) "import Owner.Hidden as Alias; " else "" + val prefix = + "module Headers { class Owner { class ItemPublic {} private class Hidden { class Item {} } } " + + alias + + "Int number = 1; void damaged($qualified" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, "$prefix value) {} }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(qualified) + .isEmpty() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(cached.diagnostics.map { it.code }).doesNotContain("EMB-5") + val control = prefix.removeSuffix(qualified) + "Owner.Ite" + adapter.compile(URI, "$control value) {} }") + assertThat(adapter.getCompletions(URI, 0, control.length).map { it.label }) + .containsExactly("ItemPublic") + } + } + + @Test + fun `package import aliases resolve through the bundled XDK`() { + val prefix = "module Headers { import ecstasy.text as Text; void damaged(Text.Str" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix value) {} }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("StringBuffer") + assertThat( + adapter + .compile(URI, prefix.dropLast(3) + "StringBuffer value) {} }") + .diagnostics, + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(ecstasy.text.Str|", + "void damaged(ecstasy.text.Str| {}", + "ecstasy.text.Str|;", + ], + ) + fun `qualified type queries keep diagnostics for missing names and delimiters`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('|') + val suffix = declaration.substringAfter('|') + " }" + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .contains("StringBuffer") + assertThat( + adapter.compile(URI, prefix.dropLast(3) + "StringBuffer" + suffix).diagnostics, + ).isNotEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(missing.te|xt.StringBuffer value) {}", + "void damaged(missing.text.| value) {}", + " void damaged(ecstasy.text.Str| value) {}", + ], + ) + fun `unsupported cursor positions and generic headers remain empty`(declaration: String) { + val prefix = "module Headers { " + declaration.substringBefore('|') + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + declaration.substringAfter('|') + " }") + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(declaration) + .isEmpty() + } + } + + @Test + fun `qualified queries follow unsaved nested types and root invalidation`() { + val root = directory.resolve("Headers.x").toFile().canonicalFile + val child = directory.resolve("Headers/Child.x").toFile().canonicalFile + child.parentFile.mkdirs() + root.writeText("module Headers { class Owner { class ItemDisk {} } }") + val prefix = "class Child { void damaged(Owner.Ite" + child.writeText("$prefix value) {} }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val childUri = child.toURI().toString() + adapter.compile(rootUri, "module Headers { class Owner { class ItemOverlay {} } }") + adapter.compile(childUri, child.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .containsExactly("ItemOverlay") + adapter.compile(rootUri, root.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .containsExactly("ItemDisk") + assertThat(root.readText()).contains("ItemDisk") + } + } + + @Test + fun `embedding type queries preserve qualifier syntax without validating or caching it`() { + CompilerTestSupport.configure() + val prefix = "module Headers { void damaged(ecstasy.text.Str" + val text = "$prefix value) {} Int later = 1; }" + val source = Source(text, URI) + repeat(prefix.length) { source.next() } + val cursor = source.position + source.reset() + val errors = ErrorList() + val analysis = EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, errors) + assertThat(errors.errors.map { it.code }).containsExactly(Parser.INCOMPLETE_EXPRESSION) + val site = analysis.sites().single() + val type = site.target as NamedTypeExpression + assertThat(type.parent).isSameAs(site) + assertThat(type.source.toRawString()).isEqualTo(text) + assertThat(type.names).containsExactly("ecstasy", "text", "Str") + assertThat(type.isValidated).isFalse() + assertThat(type.nameBindings.map { it.target() }).containsOnlyNulls() + assertThat(analysis.cursorBindings()[site]!!.types().map { it.name() }) + .contains("StringBuffer") + for ( + listener in + listOf( + ErrorList(ErrorList.FIRST_ERROR), + ErrorListener.cancellable(ErrorList()) { true }, + ) + ) { + source.reset() + val stopped = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, listener) + assertThat(stopped.pool()).isNull() + assertThat(stopped.cursorBindings()).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Headers.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt new file mode 100644 index 00000000000..34de5fea11e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt @@ -0,0 +1,145 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshot +import org.xvm.tool.ModuleInfo +import java.nio.file.Path + +class XdkRecoveryTest { + @TempDir lateinit var directory: Path + + @Test + fun `recovered syntax is available without an assembled or validated module`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule( + Source("module Editing { void run() { console.; } Int after = 1; }", URI), + null, + errors, + ) + assertThat(errors.errors).isNotEmpty() + assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") + assertThat(result.succeeded()).isFalse() + assertThat(result.parsed()).isNull() + assertThat(result.file()).isNull() + assertThat(result.pool()).isNull() + assertThat(result.sourceTrees()).hasSize(1) + assertThat( + result + .sourceTrees() + .single() + .source.fileName, + ).isEqualTo(URI) + val semantics = result.semanticSnapshot() + assertThat(semantics.status).isEqualTo(SemanticModel.Status.UNAVAILABLE) + assertThat(semantics.symbols).isEmpty() + assertThat(semantics.occurrences).isEmpty() + assertThat(semantics.types).isEmpty() + } + + @Test + fun `unfinished bodies retain current outline folding and UTF-16 selection without stale semantics`() { + for (newline in listOf("\n", "\r\n")) { + XdkAdapter().use { adapter -> + val valid = "module Editing { Int old = 1; Int read() { return old; } }" + assertThat(adapter.compile(URI, valid).success).isTrue() + assertThat(adapter.findDefinition(URI, 0, valid.lastIndexOf("old"))).isNotNull() + val text = + "module Editing {$newline void current() {$newline class Local {} /* café 😀 */ console." + val result = adapter.compile(URI, text) + assertThat(result.success).isFalse() + assertThat(result.diagnostics.map { it.code }).doesNotContain("EMB-5") + assertThat(adapter.findWorkspaceSymbols("old")).isEmpty() + assertThat(adapter.findWorkspaceSymbols("current")).hasSize(1) + assertThat(adapter.findWorkspaceSymbols("Local")).hasSize(1) + assertThat(adapter.getFoldingRanges(URI)).anySatisfy { + assertThat(it.startLine).isEqualTo(1) + assertThat(it.endLine).isEqualTo(2) + assertThat(it.endCharacter).isEqualTo(text.lines().last().length) + } + val cursor = Position(2, text.lines().last().length) + val selection = adapter.getSelectionRanges(URI, listOf(cursor)).single() + assertThat(selection.range.end).isEqualTo(cursor) + assertThat(selection.range.start.line).isLessThan(cursor.line) + assertThat(adapter.findDefinition(URI, 1, 10)).isNull() + assertThat(adapter.findReferences(URI, 1, 10, true)).isEmpty() + assertThat(adapter.prepareTypeHierarchy(URI, 0, 8)).isEmpty() + assertThat(adapter.compile(URI, valid).success).isTrue() + assertThat(adapter.findWorkspaceSymbols("current")).isEmpty() + assertThat(adapter.findDefinition(URI, 0, valid.lastIndexOf("old"))).isNotNull() + adapter.closeDocument(URI) + assertThat(adapter.findWorkspaceSymbols("")).isEmpty() + } + } + } + + @Test + fun `a broken member retains independent syntax for siblings without linking partial trees`() { + directory = directory.toRealPath() + val root = directory.resolve("Project.x").toFile() + root.writeText("module Project { class Base {} }") + val member = directory.resolve("Project/Child.x").toFile() + member.parentFile.mkdirs() + member.writeText("class Child extends Base { void changing() { this.") + val sibling = directory.resolve("Project/Sibling.x").toFile() + sibling.writeText("class Sibling { Int value = 1; }") + CompilerTestSupport.configure() + val errors = ErrorList() + val input = ModuleInfo(root, false) + val compilation = EmbeddingSupport.instance().compileModule(input, null, errors) + assertThat(compilation.parsed()).isNull() + assertThat(compilation.file()).isNull() + assertThat(compilation.sourceTrees().map { it.source.fileName }) + .containsExactlyInAnyOrder(root.path, member.path, sibling.path) + assertThat(input.parsedSources).containsExactlyElementsOf(compilation.sourceTrees()) + + XdkAdapter().use { adapter -> + val uri = root.toURI().toString() + val result = adapter.compile(uri, root.readText()) + assertThat(result.diagnostics).isNotEmpty().allSatisfy { + assertThat(it.location.uri).isEqualTo(member.toURI().toString()) + } + assertThat( + adapter + .findWorkspaceSymbols("Base") + .single() + .location.uri, + ).isEqualTo(uri) + assertThat( + adapter + .findWorkspaceSymbols("changing") + .single() + .location.uri, + ).isEqualTo(member.toURI().toString()) + assertThat( + adapter + .findWorkspaceSymbols("Sibling") + .single() + .location.uri, + ).isEqualTo(sibling.toURI().toString()) + assertThat( + adapter.findDefinition( + member.toURI().toString(), + 0, + member.readText().indexOf("Base"), + ), + ).isNull() + val rootSymbols = adapter.getCachedResult(uri)!!.symbols + assertThat(rootSymbols.single().children.map { it.name }).containsExactly("Base") + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt new file mode 100644 index 00000000000..fce44da8826 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt @@ -0,0 +1,117 @@ +package org.xvm.lsp.adapter + +import com.google.gson.Gson +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +class XdkReferenceLensTest { + @TempDir lateinit var directory: Path + + @Test + fun `shared lenses count closed consumers exclude declarations and distinguish overloads`() { + val data = scenario("X279") + + fun text(key: String) = data[key].asString + val file = + directory + .resolve(text("file")) + .toFile() + .canonicalFile + .apply { writeText(text("source")) } + val consumer = + directory.resolve(text("consumerFile")).toFile().canonicalFile.apply { + writeText(text("consumer")) + } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule(text("module"), uri), + XdkSourceModule(text("consumerModule"), consumer.toURI().toString(), setOf(text("module"))), + ), + ) + assertThat(adapter.compile(uri, text("source")).diagnostics).isEmpty() + + fun lenses() = adapter.getCodeLenses(uri).filter { it.command?.command == "xtc.showReferences" } + val initial = lenses() + assertThat(initial.map { it.range.start.line }).containsExactly(1, 2, 3, 4) + assertThat(initial.map { it.command!!.title }).containsExactly( + text("initialTitle"), + "0 references", + "0 references", + "0 references", + ) + val command = initial.first().command!! + val arguments = Gson().toJsonTree(command.arguments).asJsonArray + assertThat(arguments[0].asString).isEqualTo(uri) + assertThat(arguments[1].asJsonObject["line"].asInt).isEqualTo(1) + val locations = arguments[2].asJsonArray + assertThat(locations.map { it.asJsonObject["uri"].asString }) + .containsExactlyInAnyOrder(uri, consumer.toURI().toString()) + assertThat( + locations.map { location -> + location.asJsonObject["range"] + .asJsonObject["start"] + .asJsonObject["line"] + .asInt + }, + ).containsExactlyInAnyOrder(2, 4) + assertThat(adapter.getCodeLensesAsync(uri, false).join().map { it.command!!.command }) + .containsExactly("xtc.runModule") + assertThat(adapter.compile(consumer.toURI().toString(), text("changedConsumer")).diagnostics).isEmpty() + assertThat(lenses().first().command!!.title).isEqualTo(text("changedTitle")) + adapter.compile(consumer.toURI().toString(), text("consumer").replace("lib.read(2)", "missing()")) + assertThat(lenses()).isEmpty() + assertThat(adapter.getCodeLenses(uri).map { it.command!!.command }).containsExactly("xtc.runModule") + } + } + + @Test + fun `shared documentation action works in the configured project without changing disk`() { + val data = scenario("X278") + val source = data["source"].asString + val file = + directory + .resolve(data["file"].asString) + .toFile() + .canonicalFile + .apply { writeText(source) } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("Documentation", uri))) + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val action = + adapter + .getCodeActions(uri, Range(Position(1, 10), Position(1, 10)), emptyList()) + .single { it.title == data["title"].asString } + val edit = + action.edit!! + .changes + .getValue(uri) + .single() + val changed = source.replace(" ", edit.newText + " ") + assertThat(changed).isEqualTo(data["expected"].asString) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + assertThat(file.readText()).isEqualTo(source) + } + } + + private fun scenario(id: String) = + JsonParser + .parseString( + Path + .of( + System.getProperty("xtc.composite.root"), + "lang/test-fixtures/compiler-playbook/scenarios.json", + ).toFile() + .readText(), + ).asJsonObject["cases"] + .asJsonObject[id] + .asJsonObject["values"] + .asJsonObject +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameBoundaryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameBoundaryTest.kt new file mode 100644 index 00000000000..35ea32c13d2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameBoundaryTest.kt @@ -0,0 +1,116 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +/** Explicit refusal and scope boundaries; compiling a fixture alone is not evidence for rename. */ +class XdkRenameBoundaryTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = + [ + "Int use() { function Int(Int) f = &pick; return f(1); }", + "function Int(Int) saved() = &pick;", + "Int consume(function Int(Int) f) = f(1); Int use() = consume(&pick);", + ], + ) + fun `stored returned and passed method values retain positional invocation`(use: String) { + val text = "module App { Int pick(Int input) = input; $use }" + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("input"), "value")) + val changed = + edit.changes + .getValue(uri) + .sortedByDescending { it.range.start.column } + .fold(text) { value, change -> + value.replaceRange( + change.range.start.column, + change.range.end.column, + change.newText, + ) + } + assertThat(changed).isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `union receiver dispatch renames all compiler proven alternatives`() { + val text = + "module App { class First { Int read() = 1; } class Second { Int read() = 2; } " + + "Int use(First | Second target) = target.read(); }" + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("read"), "fetch")) + assertThat(edit.changes.getValue(uri)).hasSize(3).allMatch { it.newText == "fetch" } + } + } + + @Test + fun `bodyless source class contract renames together with its concrete implementation`() { + val text = + "module App { class Base { Int read(); } class Actual extends Base { @Override Int read() = 1; } " + + "Int use(Base target) = target.read(); }" + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("read"), "fetch")) + val changed = + edit.changes.getValue(uri).sortedByDescending { it.range.start.column }.fold(text) { value, change -> + value.replaceRange(change.range.start.column, change.range.end.column, change.newText) + } + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `an explicit graph does not claim or edit omitted consumers`() { + directory = directory.toRealPath() + val text = "module App { class Box { Int pick(Int input) = input; } }" + val consumer = + "module Consumer { package lib import App; Int run(lib.Box box) = box.pick(input = 1); }" + val outside = directory.resolve("Consumer.x").toFile().apply { writeText(consumer) } + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + val uri = source.toURI().toString() + adapter.initializeWorkspace(listOf(directory.toString())) + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri))) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("pick"), "choose")) + assertThat(edit.changes.keys).containsExactly(uri) + assertThat(outside.readText()).isEqualTo(consumer) + // Registering the omitted consumer changes the proof boundary and includes its call. + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri), + XdkSourceModule("Consumer", outside.toURI().toString(), setOf("App")), + ), + ) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + assertThat( + requireNotNull(adapter.rename(uri, 0, text.indexOf("pick"), "choose")) + .changes + .keys, + ).containsExactlyInAnyOrder(uri, outside.toURI().toString()) + } + } + + private fun workspace( + text: String, + check: (XdkAdapter, String) -> Unit, + ) { + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = source.toURI().toString() + assertThat(adapter.compile(uri, text).diagnostics).describedAs(text).isEmpty() + check(adapter, uri) + assertThat(source.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt new file mode 100644 index 00000000000..fae4805bdae --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt @@ -0,0 +1,228 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicInteger + +class XdkRenameTest { + @TempDir lateinit var directory: Path + + @Test + fun `local rename preserves captures and shadowed lambda parameters`() { + val text = + "module Rename { Int run() { Int local = 1; function Int() f = () -> local; " + + "function Int(Int) g = (Int local) -> local; return f() + g(2); } }" + withSource(text) { adapter -> + val edit = requireNotNull(adapter.rename(URI, 0, text.indexOf("local"), "renamed")) + assertThat(edit.versioned).isTrue() + val changed = apply(text, edit.changes.getValue(URI)) + assertThat(changed) + .contains("Int renamed = 1", "f = () -> renamed", "(Int local) -> local") + assertThat(adapter.getCachedResult(URI)?.success).isTrue() + assertThat(adapter.prepareRename(URI, 0, text.indexOf("local"))?.placeholder) + .isEqualTo("local") + assertThat(adapter.compile(URI, changed).success).isTrue() + } + } + + @Test + fun `successful compilation alone does not allow capturing an untouched property`() { + val text = + "module Rename { Int value = 10; Int run() { Int local = 1; return local + value; } }" + withSource(text) { adapter -> + assertThat(adapter.rename(URI, 0, text.indexOf("local"), "value")).isNull() + assertThat(adapter.getCachedResult(URI)?.diagnostics).isEmpty() + } + } + + @Test + fun `private parameter rename covers reordered named defaults and generic visible indices`() { + val text = + "module Rename { private T pick(T input, Int count = 1) = input; String run() = pick(count = 2, input = \"yes\"); }" + withSource(text) { adapter -> + val edit = requireNotNull(adapter.rename(URI, 0, text.lastIndexOf("input"), "value")) + assertThat(edit.changes.getValue(URI)).hasSize(3) + val changed = apply(text, edit.changes.getValue(URI)) + assertThat(changed).contains("T value", "= value", "value = \"yes\"") + assertThat(adapter.compile(URI, changed).success).isTrue() + } + } + + @Test + fun `overloaded parameter labels only edit the selected private method`() { + val text = + "module Rename { private Int pick(Int input) = input; private String pick(String input) = input; " + + "Int run() = pick(input = 1); String text() = pick(input = \"one\"); }" + withSource(text) { adapter -> + val edit = requireNotNull(adapter.rename(URI, 0, text.indexOf("input"), "number")) + val changed = apply(text, edit.changes.getValue(URI)) + assertThat(changed) + .contains( + "Int number) = number", + "pick(number = 1)", + "String input) = input", + "pick(input = \"one\")", + ) + assertThat(adapter.compile(URI, changed).success).isTrue() + } + } + + @Test + fun `public contracts require a configured project`() { + val text = + "module Rename { class Item(Int value) {} " + + "Int run(Int input) { function Int(Int) f = (Int argument) -> argument; return f(input); } }" + withSource(text) { adapter -> + for (name in listOf("Item", "value", "run", "input")) { + assertThat(adapter.prepareRename(URI, 0, text.indexOf(name))) + .describedAs(name) + .isNull() + assertThat(adapter.rename(URI, 0, text.indexOf(name), "other")) + .describedAs(name) + .isNull() + } + } + } + + @Test + fun `private parameter rename preserves positional method value calls`() { + val text = + "module Rename { private Int pick(Int input) = input; Int run() { function Int(Int) f = &pick; return f(1); } }" + withSource(text) { adapter -> + val edit = requireNotNull(adapter.rename(URI, 0, text.indexOf("input"), "value")) + assertThat(apply(text, edit.changes.getValue(URI))) + .isEqualTo(text.replace("input", "value")) + } + } + + @Test + fun `collisions keywords and non-identifiers produce no edits or live diagnostics`() { + val text = + "module Rename { Int run() { Int local = 1; Int other = 2; return local + other; } }" + withSource(text) { adapter -> + for (name in listOf("other", "return", "", "two words", "x.y", "x/*comment*/")) { + assertThat(adapter.rename(URI, 0, text.indexOf("local"), name)) + .describedAs(name) + .isNull() + } + assertThat(adapter.getCachedResult(URI)?.diagnostics).isEmpty() + } + } + + @Test + fun `UTF16 positions and CRLF newlines survive a longer name`() { + val text = + "module Rename {\r\n Int run() { String emoji = \"🙂\"; Int local = 1; return local; }\r\n}" + withSource(text) { adapter -> + val column = text.lines()[1].indexOf("local") + val edit = requireNotNull(adapter.rename(URI, 1, column, "longerName")) + val changed = apply(text, edit.changes.getValue(URI)) + assertThat(changed).contains("Int longerName = 1", "return longerName;") + assertThat(adapter.compile(URI, changed).success).isTrue() + } + } + + @Test + fun `rename in a closed member uses the whole module and rejects changed disk membership`() { + directory = directory.toRealPath() + val root = directory.resolve("Rename.x").toFile() + val member = directory.resolve("Rename/Child.x").toFile() + val text = "module Rename {}" + val child = + "class Child { private Int pick(Int input) = input; Int run() = pick(input = 1); }" + root.writeText(text) + member.parentFile.mkdirs() + member.writeText(child) + val uri = root.toURI().toString() + XdkAdapter().use { adapter -> + val compilation = adapter.compile(uri, text) + assertThat(compilation.diagnostics).isEmpty() + val edit = + requireNotNull( + adapter.rename(member.toURI().toString(), 0, child.indexOf("input"), "value"), + ) + assertThat(edit.changes.keys).containsExactly(member.toURI().toString()) + assertThat(apply(child, edit.changes.getValue(member.toURI().toString()))) + .contains("value = 1") + assertThat(member.readText()).isEqualTo(child) + member.writeText("\n$child") + assertThat( + adapter.rename(member.toURI().toString(), 0, child.indexOf("input"), "value"), + ).isNull() + } + } + + @Test + fun `editing or closing the module cancels a blocked rename proof`() { + for (close in listOf(false, true)) { + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val attempts = AtomicInteger() + val adapter = + XdkAdapter { source, errors -> + if (attempts.incrementAndGet() == 2) { + entered.countDown() + check(release.await(20, SECONDS)) + } + EmbeddingSupport.instance().compileModule(source, null, errors) + } + val text = "module Rename { Int run() { Int local = 1; return local; } }" + try { + CompilerTestSupport.configure() + assertThat(adapter.compile(URI, text).success).isTrue() + val result = adapter.renameAsync(URI, 0, text.indexOf("local"), "value") + assertThat(entered.await(20, SECONDS)).isTrue() + if (close) adapter.closeDocument(URI) else adapter.compileAsync(URI, "\n$text") + assertThat(result.isCancelled).isTrue() + } finally { + release.countDown() + adapter.close() + } + } + } + + private fun withSource( + text: String, + test: (XdkAdapter) -> Unit, + ) { + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, text) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + test(adapter) + } + } + + private fun apply( + text: String, + edits: List, + ): String { + fun offset(position: Position): Int = + ( + if (position.line == 0) { + 0 + } else { + Regex("\r\n|\r|\n") + .findAll(text) + .elementAt(position.line - 1) + .range.last + 1 + } + ) + position.column + return edits + .sortedWith( + compareByDescending { it.range.start.line } + .thenByDescending { it.range.start.column }, + ).fold(text) { value, edit -> + value.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + } + + private companion object { + const val URI = "file:///Rename.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt new file mode 100644 index 00000000000..3acc41af521 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt @@ -0,0 +1,159 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkResourceInputsTest { + @TempDir lateinit var directory: Path + + @Test + fun `conventional Gradle resources resolve without changing source ownership`() { + CompilerTestSupport.configure() + val source = + directory.resolve("app/src/main/x/Assets.x").toFile().apply { + parentFile.mkdirs() + writeText("module Assets { static String text() = $./data.txt; }") + } + directory.resolve("app/src/main/resources/data.txt").toFile().apply { + parentFile.mkdirs() + writeText("template") + } + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf(XdkSourceModule("Assets", source.toURI().toString())), + ) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(adapter.compile(source.toURI().toString(), source.readText()).diagnostics) + .isEmpty() + } + } + + @Test + fun `custom resource contents invalidate only their source consumer closure including same timestamp edits`() { + CompilerTestSupport.configure() + directory = directory.toRealPath() + val roots = directory.resolve("custom-assets").toFile().apply { mkdirs() } + val resource = roots.resolve("data.txt").apply { writeText("first") } + val library = + directory.resolve("Assets.x").toFile().apply { + writeText("module Assets { static String text() = $./data.txt; }") + } + val consumer = + directory.resolve("Consumer.x").toFile().apply { + writeText( + "module Consumer { package assets import Assets; String run() = assets.text(); }", + ) + } + val independent = + directory.resolve("Independent.x").toFile().apply { writeText("module Independent {}") } + val compiled = mutableListOf() + XdkAdapter( + { source, repository, errors -> + EmbeddingSupport.instance().compileModule(source, repository, errors) + }, + { sources, repository, errors -> + compiled += sources.sourceFile.nameWithoutExtension + EmbeddingSupport.instance().compileModule(sources, repository, errors) + }, + { _, _, _, _, _ -> error("No cursor analysis") }, + ).use { adapter -> + val graph = + listOf( + XdkSourceModule( + "Assets", + library.toURI().toString(), + resourceRoots = listOf(roots.toURI().toString()), + ), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Assets")), + XdkSourceModule("Independent", independent.toURI().toString()), + ) + adapter.replaceSourceModules(graph) + + fun pull() = adapter.workspaceDiagnosticsAsync().get(30, SECONDS) + assertThat(pull()).allMatch { it.success } + assertThat(compiled).containsExactly("Assets", "Consumer", "Independent") + compiled.clear() + assertThat(pull()).allMatch { it.success } + assertThat(compiled).isEmpty() + val timestamp = Files.getLastModifiedTime(resource.toPath()) + resource.writeText("other") + Files.setLastModifiedTime(resource.toPath(), timestamp) + assertThat(adapter.affectedAnalysisScopes(resource.toURI().toString())) + .contains(library.toURI().toString(), consumer.toURI().toString()) + .doesNotContain(independent.toURI().toString()) + assertThat(pull()).allMatch { it.success } + assertThat(compiled).containsExactly("Assets", "Consumer") + compiled.clear() + Files.delete(resource.toPath()) + val missing = pull() + assertThat( + missing + .single { it.uri == library.toURI().toString() } + .diagnostics + .map { it.code }, + ).contains("PARSER-24") + assertThat( + missing + .single { it.uri == consumer.toURI().toString() } + .diagnostics + .map { it.code }, + ).contains("DEPENDENCY-FAILED") + resource.writeText("again") + assertThat(pull()).allMatch { it.success } + adapter.replaceSourceModules( + graph.map { + if (it.name == "Assets") { + XdkSourceModule(it.name, it.uri, resourceRoots = emptyList()) + } else { + it + } + }, + ) + assertThat(pull().single { it.uri == library.toURI().toString() }.success).isFalse() + } + } + + @Test + fun `a missing configured resource directory can be created after a failed pull`() { + CompilerTestSupport.configure() + val source = + directory.resolve("Assets.x").toFile().apply { + writeText("module Assets { static String text() = $./data.txt; }") + } + val root = directory.resolve("generated-resources").toFile() + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule( + "Assets", + source.toURI().toString(), + resourceRoots = listOf(root.toURI().toString()), + ), + ), + ) + assertThat( + adapter + .workspaceDiagnosticsAsync() + .get(30, SECONDS) + .single() + .success, + ).isFalse() + root.mkdirs() + root.resolve("data.txt").writeText("generated") + assertThat( + adapter + .workspaceDiagnosticsAsync() + .get(30, SECONDS) + .single() + .success, + ).isTrue() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt new file mode 100644 index 00000000000..12034120e9a --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt @@ -0,0 +1,317 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path + +class XdkResourceRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["class", "package"]) + fun `member rename moves its companion tree and reverse rename restores the sources`(kind: String) { + val original = + mapOf( + "App.x" to + if (kind == "class") { + "module App { Box make() = new Box(); void accept(Box.Nested value) {} }" + } else { + "module App { void accept(Box.Nested value) {} }" + }, + "App/Box.x" to "$kind Box {}", + "App/Box/Nested.x" to "class Nested {}", + ) + original.forEach { (file, text) -> source(file, text) } + val root = uri("App.x") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(root, original.getValue("App.x")).diagnostics).isEmpty() + val edit = + requireNotNull( + adapter.rename(root, 0, original.getValue("App.x").indexOf("Box"), "Renamed"), + ) + assertThat(edit.renames).hasSize(2) + assertThat(edit.renames).containsEntry(uri("App/Box.x"), uri("App/Renamed.x")) + assertThat(edit.renames) + .containsEntry( + uri("App/Box").removeSuffix("/"), + uri("App/Renamed").removeSuffix("/"), + ) + original.forEach { (file, text) -> + assertThat(directory.resolve(file).toFile().readText()).isEqualTo(text) + } + apply(edit) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val changed = directory.resolve("App.x").toFile().readText() + assertThat(adapter.compile(root, changed).diagnostics).isEmpty() + assertThat(adapter.findDefinition(root, 0, changed.indexOf("Nested"))?.uri) + .isEqualTo(uri("App/Renamed/Nested.x")) + apply(requireNotNull(adapter.rename(root, 0, changed.indexOf("Renamed"), "Box"))) + } + original.forEach { (file, text) -> + assertThat(directory.resolve(file).toFile().readText()).isEqualTo(text) + } + } + + @Test + fun `discovered module rename updates closed imports and moves root and companion directory`() { + val library = "module Library { Box make() = new Box(); }" + val consumer = + "module Consumer { package lib import Library; lib.Box make() = new lib.Box(); }" + source("Library.x", library) + source("Library/Box.x", "class Box {}") + source("Consumer.x", consumer) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri("Library.x"), library).diagnostics).isEmpty() + assertThat(adapter.compile(uri("Consumer.x"), consumer).diagnostics).isEmpty() + // A use of the local package alias must not rename the imported module instead. + val alias = consumer.indexOf("lib.Box") + assertThat(adapter.prepareRename(uri("Consumer.x"), 0, alias)).isNull() + assertThat(adapter.rename(uri("Consumer.x"), 0, alias, "Renamed")).isNull() + val edit = + requireNotNull( + adapter.rename(uri("Library.x"), 0, library.indexOf("Library"), "Renamed"), + ) + assertThat(edit.renames).hasSize(2) + assertThat(edit.changes.keys) + .containsExactlyInAnyOrder(uri("Library.x"), uri("Consumer.x")) + apply(edit) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val changed = directory.resolve("Consumer.x").toFile().readText() + assertThat(changed).contains("package lib import Renamed") + assertThat(adapter.compile(uri("Consumer.x"), changed).diagnostics).isEmpty() + assertThat(adapter.findDefinition(uri("Consumer.x"), 0, changed.indexOf("Box"))?.uri) + .isEqualTo(uri("Renamed/Box.x")) + assertThat( + adapter + .compile( + uri("Renamed.x"), + directory.resolve("Renamed.x").toFile().readText(), + ).diagnostics, + ).isEmpty() + apply( + requireNotNull( + adapter.rename(uri("Renamed.x"), 0, library.indexOf("Library"), "Library"), + ), + ) + } + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(library) + assertThat(directory.resolve("Consumer.x").toFile().readText()).isEqualTo(consumer) + } + + @Test + fun `explicit module configuration and existing destination directories prevent resource edits`() { + val text = "module App { Box make() = new Box(); }" + source("App.x", text) + source("App/Box.x", "class Box { construct() {} }") + Files.createDirectories(directory.resolve("App/Renamed")) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules(listOf(XdkSourceModule("App", uri("App.x")))) + assertThat(adapter.compile(uri("App.x"), text).diagnostics).isEmpty() + assertThat(adapter.rename(uri("App.x"), 0, text.indexOf("App"), "Changed")).isNull() + assertThat(adapter.rename(uri("App.x"), 0, text.indexOf("Box"), "Renamed")).isNull() + val member = directory.resolve("App/Box.x").toFile().readText() + assertThat(adapter.rename(uri("App/Box.x"), 0, member.indexOf("construct"), "build")) + .isNull() + } + } + + @Test + fun `type rename includes constructor uses but does not rename the construct keyword`() { + val text = "module App { class Box { construct(Int value) {} } Box make() = new Box(1); }" + source("App.x", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri("App.x"), text).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("App.x"), 0, text.indexOf("Box"), "Renamed")) + assertThat(edit.changes.getValue(uri("App.x"))).hasSize(3) + apply(edit) + val changed = directory.resolve("App.x").toFile().readText() + assertThat(changed).contains("construct(Int value)", "new Renamed(1)") + assertThat(adapter.compile(uri("App.x"), changed).diagnostics).isEmpty() + assertThat(adapter.rename(uri("App.x"), 0, changed.indexOf("value"), "argument")) + .isNotNull() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "interface Api { Int read(); } class Forward(Api target) delegates Api(target) {} Int use(Forward value) = value.read();", + "class Base { Int read = 1; } mixin Loud into Base { @Override Int read.get() = 2; } " + + "class Host extends Base incorporates Loud {} Int use(Host value) = value.read;", + "class Holder { @Lazy Int read.calc() = 1; } Int use(Holder value) = value.read;", + "class Base { Int read() = 1; } mixin Loud into Base { @Override Int read() = super() + 1; } " + + "class Host extends Base incorporates Loud {} Int use(Host value) = value.read();", + ], + ) + fun `delegation mixin and annotated property rename preserves written contracts`(body: String) { + val text = "module App { $body }" + source("App.x", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri("App.x"), text).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("App.x"), 0, text.indexOf("read"), "changed")) + assertThat(edit.changes.getValue(uri("App.x"))) + .hasSize(Regex("\\bread\\b").findAll(text).count()) + assertThat(directory.resolve("App.x").toFile().readText()).isEqualTo(text) + apply(edit) + assertThat( + adapter + .compile(uri("App.x"), directory.resolve("App.x").toFile().readText()) + .diagnostics, + ).isEmpty() + } + } + + @Test + fun `qualified discovered module rename preserves the domain and local import alias`() { + val library = "module Library.example.org { class Box {} }" + val consumer = + "module Consumer { package lib import Library.example.org; lib.Box make() = new lib.Box(); }" + source("Library.x", library) + source("Consumer.x", consumer) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri("Library.x"), library).diagnostics).isEmpty() + val edit = + requireNotNull( + adapter.rename(uri("Library.x"), 0, library.indexOf("Library"), "Renamed"), + ) + apply(edit) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val changed = directory.resolve("Consumer.x").toFile().readText() + assertThat(changed).contains("package lib import Renamed.example.org", "new lib.Box()") + assertThat(adapter.compile(uri("Consumer.x"), changed).diagnostics).isEmpty() + } + } + + @Test + fun `implicit package rename moves only its directory and supports reverse rename`() { + val text = "module App { void accept(tools.deep.Box value) {} }" + source("App.x", text) + source("App/tools/deep/Box.x", "class Box {}") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri("App.x"), text).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri("App.x"), 0, text.indexOf("tools"), "helpers")) + assertThat(edit.renames) + .containsExactlyEntriesOf( + mapOf( + uri("App/tools").removeSuffix("/") to uri("App/helpers").removeSuffix("/"), + ), + ) + apply(edit) + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val changed = directory.resolve("App.x").toFile().readText() + assertThat(adapter.compile(uri("App.x"), changed).diagnostics).isEmpty() + assertThat(adapter.findDefinition(uri("App.x"), 0, changed.indexOf("Box"))?.uri) + .isEqualTo(uri("App/helpers/deep/Box.x")) + apply( + requireNotNull(adapter.rename(uri("App.x"), 0, changed.indexOf("helpers"), "tools")), + ) + } + assertThat(directory.resolve("App.x").toFile().readText()).isEqualTo(text) + assertThat(directory.resolve("App/tools/deep/Box.x")).exists() + } + + @Test + fun `host proposal includes explicit graph replacement without installing it prematurely`() { + val library = "module Library.example.org { class Box {} }" + val consumer = + "module Consumer { package lib import Library.example.org; lib.Box make() = new lib.Box(); }" + source("Library.x", library) + source("Consumer.x", consumer) + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library.example.org", uri("Library.x")), + XdkSourceModule("Consumer", uri("Consumer.x"), setOf("Library.example.org")), + ), + ) + assertThat(adapter.compile(uri("Library.x"), library).diagnostics).isEmpty() + assertThat(adapter.rename(uri("Library.x"), 0, library.indexOf("Library"), "Renamed")) + .isNull() + val proposal = + requireNotNull( + adapter + .renameProposalAsync( + uri("Library.x"), + 0, + library.indexOf("Library"), + "Renamed", + ).join(), + ) + val modules = requireNotNull(proposal.sourceModules) + assertThat(modules.map { it.name }) + .containsExactlyInAnyOrder("Renamed.example.org", "Consumer") + assertThat(modules.single { it.name == "Consumer" }.dependencies) + .containsExactly("Renamed.example.org") + assertThat(adapter.compile(uri("Library.x"), library).diagnostics).isEmpty() + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(library) + apply(proposal.edit) + adapter.replaceSourceModules(modules) + val changed = directory.resolve("Consumer.x").toFile().readText() + assertThat(adapter.compile(uri("Consumer.x"), changed).diagnostics).isEmpty() + } + } + + private fun apply(edit: WorkspaceEdit) { + edit.changes.forEach { (uri, edits) -> + val file = Path.of(URI(uri)).toFile() + val original = file.readText() + + fun offset(position: Position) = + original.lineSequence().take(position.line).sumOf { it.length + 1 } + + position.column + file.writeText( + edits + .sortedByDescending { offset(it.range.start) } + .fold(original) { text, change -> + text.replaceRange( + offset(change.range.start), + offset(change.range.end), + change.newText, + ) + }, + ) + } + edit.renames.forEach { (from, to) -> Files.move(Path.of(URI(from)), Path.of(URI(to))) } + } + + private fun source( + file: String, + text: String, + ) = directory.resolve(file).toFile().apply { + parentFile.mkdirs() + writeText(text) + } + + private fun uri(file: String): String = + directory + .resolve(file) + .toFile() + .canonicalFile + .toURI() + .toString() +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt new file mode 100644 index 00000000000..7ad49b4648b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt @@ -0,0 +1,194 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.io.File +import java.lang.ref.WeakReference +import java.net.URI +import java.nio.file.Path +import java.time.Duration +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.TimeUnit.NANOSECONDS +import java.util.concurrent.TimeUnit.SECONDS + +/** + * A bounded repeatable retention workload; it is not a multi-hour editor soak or a performance SLA. + */ +class XdkRetentionTest { + @TempDir lateinit var directory: Path + + @Test + fun `source graphs replacements rename and cursor proofs release attempts after close and shutdown`() { + CompilerTestSupport.configure() + directory = directory.toRealPath() + val support = EmbeddingSupport.instance() + val observed = CopyOnWriteArrayList>() + + fun observe(value: Any?) { + if (value != null) observed.add(WeakReference(value)) + } + + fun EmbeddingSupport.Compilation.observe(): EmbeddingSupport.Compilation = + also { + observe(it) + observe(it.pool()) + it.sourceTrees().forEach(::observe) + } + val artifacts = + (1..2).map { version -> + support + .compileModule( + Source("module External { static Int value() = $version; }", "External.x"), + null, + ErrorList(), + ).toDependency() + } + val library = + directory.resolve("Library.x").toFile().apply { + writeText( + "module Library { package ext import External; static Int value() = ext.value(); class Box { Int number = 1; } }", + ) + } + val consumer = directory.resolve("Consumer.x").toFile().apply { writeText(CONSUMER) } + val uri = consumer.toURI().toString() + val timings = mutableListOf() + val adapter = + XdkAdapter( + { source, repository, errors -> + support.compileModule(source, repository, errors).observe() + }, + { sources, repository, errors -> + support.compileModule(sources, repository, errors).observe() + }, + { source, sources, cursor, repository, errors -> + support + .analyzeIncomplete( + sources, + File(URI(source.fileName)), + cursor, + repository, + errors, + ).also { + observe(it) + observe(it.pool()) + it.sourceTrees().forEach(::observe) + } + }, + ) + try { + adapter.replaceSourceModules( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", uri, setOf("Library")), + ), + ) + val cursorCases = + listOf( + "box." to "number", + "take(" to "box", + "take(bo" to "box", + "textTake(te" to "textValue", + "new String[2](te" to "textValue", + "new Int[si|];" to "sizeValue", + "Object parts = [textValue.si" to "size", + "new Object(te|) { construct(String value) {} Int read() = box.number; };" to + "textValue", + ) + repeat(CYCLES) { cycle -> + adapter.replaceDependencies(listOf(artifacts[cycle % artifacts.size])) + val started = System.nanoTime() + val storm = + (0..7).map { edit -> adapter.compileAsync(uri, "$CONSUMER // $cycle:$edit") } + assertThat(storm.last().get(30, SECONDS).success).isTrue() + assertThat(storm.dropLast(1)).allMatch { it.isCancelled } + timings += System.nanoTime() - started + val renamed = + adapter + .renameAsync(uri, 0, CONSUMER.indexOf("local"), "renamed") + .get(30, SECONDS) + assertThat(renamed?.changes?.get(uri)).hasSize(2) + val headerQuery = cycle % (cursorCases.size + 1) == cursorCases.size + val (marked, expected) = + when { + !headerQuery -> { + cursorCases[cycle % cursorCases.size] + } + + (cycle / (cursorCases.size + 1)) % 4 == 0 -> { + "void probe(Str| value) {}" to "String" + } + + (cycle / (cursorCases.size + 1)) % 4 == 1 -> { + "void probe(Map> value) {}" to "String" + } + + (cycle / (cursorCases.size + 1)) % 4 == 2 -> { + "interface Probe extends List {}" to "String" + } + + else -> { + "void probe(ecstasy.text.Str| value) {}" to "StringBuffer" + } + } + val prefix = marked.substringBefore('|') + val incomplete = + if (headerQuery) { + CONSUMER.replace("void probe(lib.Box box) {}", marked.replace("|", "")) + } else { + CONSUMER.replace("box) {}", "box) { ${marked.replace("|", "")} }") + } + assertThat(adapter.compile(uri, incomplete).success).isFalse() + val column = incomplete.lastIndexOf(prefix) + prefix.length + assertThat( + adapter.getCompletionsAsync(uri, 0, column).get(30, SECONDS).map { + it.label + }, + ).contains(expected) + val cancelled = adapter.getCompletionsAsync(uri, 0, column) + cancelled.cancel(false) + assertThat(adapter.compile(uri, CONSUMER).success).isTrue() + val rename = adapter.renameAsync(uri, 0, CONSUMER.indexOf("local"), "renamed") + adapter.closeDocument(uri) + assertThat(rename.isCancelled).isTrue() + assertThat(adapter.getCachedResult(uri)).isNull() + } + assertReleased(observed) + } finally { + adapter.close() + } + assertReleased(observed) + val millis = timings.map(NANOSECONDS::toMillis).sorted() + println( + "Retention workload: cycles=$CYCLES, edit requests=${CYCLES * 8}, weak references=${observed.size}, retained=0, " + + "rebuild p50=${millis[millis.size / 2]}ms, p95=${millis[(millis.size * 0.95).toInt()]}ms (includes debounce)", + ) + } + + private fun assertReleased(observed: List>) { + await().atMost(Duration.ofSeconds(20)).pollInterval(Duration.ofMillis(100)).untilAsserted { + System.gc() + assertThat(observed.count { it.get() != null }) + .describedAs("compiler attempts, roots and pools still strongly reachable") + .isZero() + } + } + + private companion object { + val CYCLES = + System.getProperty("xtc.lsp.retentionCycles", "120").toInt().also { + require(it >= 2) { "Retention workload requires at least two cycles" } + } + const val CONSUMER = + "module Consumer { package lib import Library; " + + "String textValue = \"x\"; Int sizeValue = 2; void textTake(String value) {} " + + "Int run() { Int local = lib.value(); return local; } void take(lib.Box value) {} void probe(lib.Box box) {} }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt new file mode 100644 index 00000000000..11038fdc3ff --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt @@ -0,0 +1,108 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkSafeDeleteTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = [ + "private Int §unused() { return 1; }", + "private Int §unused(Int input) { return input + kept(); }", + "private static Int §unused = 3;", + ], + ) + fun `unused private methods and constants leave every surviving binding intact`(member: String) { + query(member, "", expected = true) + } + + @ParameterizedTest + @ValueSource( + strings = [ + "Int §unused() { return 1; }", + "protected Int §unused() { return 1; }", + "private Int §unused = kept();", + "private static Int §unused = kept();", + "private Int §unused() { /* keep this explanation */ return 1; }", + ], + ) + fun `unknown external consumers initialization and comment loss refuse removal`(member: String) { + query(member, "", expected = false) + } + + @Test + fun `referenced method is not removed`() { + query("private Int §unused() { return 1; }", "Int caller() { return unused(); }", expected = false) + } + + @Test + fun `public member with a closed companion consumer is refused`() { + val consumer = + directory.resolve("Delete/Nested.x").toFile().apply { + parentFile.mkdirs() + writeText("class Nested { Int read() { return Delete.unused(); } }") + } + query("static Int §unused() { return 1; }", "", expected = false) + assertThat(consumer.readText()).contains("return Delete.unused();") + } + + @Test + fun `broken configured neighbor refuses the entire proposal`() { + directory.resolve("Broken.x").toFile().writeText("module Broken { Missing value; }") + query("private Int §unused() { return 1; }", "", expected = false) + } + + private fun query( + member: String, + consumer: String, + expected: Boolean, + ) { + CompilerTestSupport.configure() + val marked = + """ + module Delete { + $member + static Int kept() { return 2; } + Int run() { return kept(); } + $consumer + } + """.trimIndent() + val text = marked.replace("§", "") + val file = + directory + .resolve("Delete.x") + .toFile() + .canonicalFile + .apply { writeText(text) } + val uri = file.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = XdkRename.position(text, marked.indexOf('§')) + val actions = adapter.getCodeActions(uri, Range(at, at), emptyList()).filter { it.title.startsWith("Safely delete") } + if (!expected) { + assertThat(actions).isEmpty() + } else { + assertThat(actions).hasSize(1) + val edit = requireNotNull(actions.single().edit) + assertThat(edit.versioned).isTrue() + val change = edit.changes.getValue(uri).single() + + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + val changed = text.replaceRange(offset(change.range.start), offset(change.range.end), change.newText) + assertThat(changed).doesNotContain("unused").contains("return kept();") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + assertThat(file.readText()).isEqualTo(text) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt new file mode 100644 index 00000000000..93582b35fd9 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt @@ -0,0 +1,187 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkScopeCompletionTest { + @TempDir lateinit var directory: Path + + @Test + fun `an empty statement cursor offers parameters without changing the source`() { + val prefix = "module Editing { void run(String item) { " + XdkAdapter().use { adapter -> + val original = adapter.compile(URI, "$prefix} }") + assertThat(original.diagnostics).isEmpty() + val item = adapter.getCompletions(URI, 0, prefix.length).single { it.label == "item" } + assertThat(item.textEdit!!.range) + .isEqualTo(Range(Position(0, prefix.length), Position(0, prefix.length))) + assertThat(adapter.getCachedResult(URI)).isEqualTo(original) + } + } + + @Test + fun `bare names expose visible parameters and preceding assigned locals`() { + val prefix = + "module Editing { void run(String itemParameter) { Int itemLocal = 1; Int itemUnassigned; item" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; Int itemLater = 2; } }") + val cached = adapter.getCachedResult(URI) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .containsExactlyInAnyOrder("itemParameter", "itemLocal") + assertThat(items.single { it.label == "itemParameter" }.detail) + .isEqualTo("String itemParameter") + assertThat(items.single { it.label == "itemLocal" }.detail).isEqualTo("Int itemLocal") + assertThat(items).allSatisfy { + assertThat(it.kind).isEqualTo(CompletionItem.CompletionKind.VARIABLE) + assertThat(it.textEdit!!.range) + .isEqualTo(Range(Position(0, prefix.length - 4), Position(0, prefix.length))) + } + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `completion uses flow narrowed variables in the selected branch`() { + val prefix = "module Editing { void run(Object item) { if (item.is(String)) { ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } } }") + assertThat( + adapter + .getCompletions(URI, 0, prefix.length) + .single { it.label == "item" } + .detail, + ).isEqualTo("String item") + } + } + + @Test + fun `locals from a closed block are absent and local declarations shadow properties`() { + val prefix = + "module Editing { String item = \"outer\"; void run() { { Int itemClosed = 1; } Int item = 2; ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).containsExactly("item") + assertThat(items.single().detail).isEqualTo("Int item") + } + } + + @Test + fun `scope completion retains returns and assignment value context`() { + XdkAdapter().use { adapter -> + for (statement in listOf("return ite", "Int result = ite", "Int item = ite")) { + val prefix = "module Editing { Int run(Int item) { $statement" + adapter.compile(URI, "$prefix; } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + if (statement.startsWith("Int item")) { + assertThat(items).isEmpty() + } else { + assertThat(items.map { it.label }).describedAs(statement).contains("item") + assertThat(items.single { it.label == "item" }.detail).isEqualTo("Int item") + } + } + } + } + + @Test + fun `implicit members preserve generic substitution and local shadowing`() { + val prefix = + "module Editing { class Box { T item; private T itemMethod() = item; void run(T itemLocal) { ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).contains("item", "itemMethod", "itemLocal") + assertThat(items.single { it.label == "itemMethod" }.detail).contains("T itemMethod()") + } + } + + @Test + fun `static receivers expose functions constants and nested types with access checks`() { + val prefix = + "module Editing { class Box { static Int itemFunction() = 1; static Int itemConstant = 2; " + + "Int itemProperty = 3; Int itemMethod() = 4; private static Int itemHidden() = 5; " + + "static class itemType {} private static class itemPrivate {} } void run() { Box.item" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .contains("itemFunction", "itemConstant", "itemType") + .doesNotContain("itemProperty", "itemMethod", "itemHidden", "itemPrivate") + assertThat(items.single { it.label == "itemType" }.kind) + .isEqualTo(CompletionItem.CompletionKind.CLASS) + } + } + + @Test + fun `a static function has no implicit instance members`() { + val prefix = + "module Editing { class Box { Int itemProperty = 1; Int itemMethod() = 2; " + + "static Int itemFunction() = 3; static void run() { item" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix; } } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .contains("itemFunction") + .doesNotContain("itemProperty", "itemMethod") + } + } + + @Test + fun `type names come from contextual lookup including imports and enclosing types`() { + XdkAdapter().use { adapter -> + for ( + (setup, name) in + listOf( + "" to "String", + "import ecstasy.text.StringBuffer as Buffer;" to "Buffer", + "import ecstasy.text.*;" to "StringBuffer", + "class ItemType {}" to "ItemType", + ) + ) { + val prefix = + "module Editing { $setup class Nested { void run() { ${name.dropLast(1)}" + adapter.compile(URI, "$prefix; } } }") + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }).describedAs(setup).contains(name) + } + } + } + + @Test + fun `implicit member completion follows unsaved module overlays and root invalidation`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val child = directory.resolve("Editing/Child.x").toFile().canonicalFile + child.parentFile.mkdirs() + root.writeText("module Editing { class Base { Int item = 1; } }") + val prefix = "class Child extends Base { void run() { ite" + child.writeText("$prefix; } }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val childUri = child.toURI().toString() + adapter.compile(rootUri, "module Editing { class Base { String item = \"overlay\"; } }") + adapter.compile(childUri, child.readText()) + assertThat( + adapter + .getCompletions(childUri, 0, prefix.length) + .single { it.label == "item" } + .detail, + ).isEqualTo("String item") + adapter.compile(rootUri, root.readText()) + assertThat( + adapter + .getCompletions(childUri, 0, prefix.length) + .single { it.label == "item" } + .detail, + ).isEqualTo("Int item") + assertThat(root.readText()).contains("Int item") + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt new file mode 100644 index 00000000000..f91b204d004 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt @@ -0,0 +1,313 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.toDependency +import org.xvm.lsp.treesitter.SemanticTokenLegend +import java.nio.file.Path +import kotlin.io.path.readText + +class XdkSemanticColorTest { + @Test + fun `shared highlighting improvements preserve lexical gaps through damage and repair`() { + val scenarios = + JsonParser + .parseString(root.resolve("lang/test-fixtures/compiler-playbook/scenarios.json").readText()) + .asJsonObject + .getAsJsonObject("cases") + XdkAdapter().use { adapter -> + val categories = scenarios.getAsJsonObject("X276").getAsJsonObject("values") + val source = categories["source"].asString + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(URI))) + categories.getAsJsonArray("tokens").forEach { item -> + val expected = item.asJsonObject + val offset = source.indexOf(expected["anchor"].asString) + expected["offset"].asInt + val line = source.take(offset).count { it == '\n' } + val column = source.take(offset).substringAfterLast('\n').length + val token = tokens.single { it.line == line && it.column == column } + assertThat(token.text).isEqualTo(expected["text"].asString) + assertThat(token.kind).isEqualTo(expected["type"].asString) + assertThat("defaultLibrary" in token.modifiers).isEqualTo(expected["library"].asBoolean) + } + val lexical = scenarios.getAsJsonObject("X277").getAsJsonObject("values") + val text = lexical["source"].asString + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + val before = requireNotNull(adapter.getSemanticTokens(URI)) + val initial = decode(text, before) + lexical.getAsJsonArray("lexical").forEach { item -> + val start = text.indexOf(item.asString) + val line = text.take(start).count { it == '\n' } + val column = text.take(start).substringAfterLast('\n').length + assertThat(initial).noneMatch { + it.line == line && it.column < column + item.asString.length && it.column + it.text.length > column + } + } + assertThat(initial).anySatisfy { + assertThat(it.text).isEqualTo("value") + assertThat(it.line).isEqualTo(text.lines().indexOfFirst { line -> "interpolated()" in line }) + assertThat(it.kind).isEqualTo("property") + } + lexical.getAsJsonArray("recovery").forEach { item -> + val edit = item.asJsonObject + val damaged = text.replace(edit["from"].asString, edit["to"].asString) + assertThat(adapter.compile(URI, damaged).success).isFalse() + val current = adapter.getSemanticTokens(URI)?.let { decode(damaged, it) }.orEmpty() + assertThat(current.filter { it.kind != "comment" }).allSatisfy { + assertThat(it.text).matches("[A-Za-z_][A-Za-z_0-9]*") + } + assertThat(adapter.compile(URI, text).diagnostics).isEmpty() + assertThat(adapter.getSemanticTokens(URI)).isEqualTo(before) + } + } + } + + @Test + fun `annotations use resolved identities without recoloring ordinary type uses`() { + val source = + """ + module Annotations { + import ecstasy.annotations.Override as Replaces; + class Base { String describe() = "base"; } + annotation Marked into Object {} + @Marked class Child extends Base { + @Replaces String describe() = "child"; + } + @ecstasy.annotations.Override String toString() = "annotations"; + class Other { + class Override {} + Override echo(Override value) = value; + } + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(URI))) + assertThat(tokens.filter { it.kind == "decorator" }.map { it.text }) + .containsExactly("Marked", "Replaces", "Override") + assertThat(tokens.filter { it.kind == "decorator" && "defaultLibrary" in it.modifiers }.map { it.text }) + .containsExactly("Replaces", "Override") + assertThat(tokens.filter { it.text == "Override" && it.line >= 8 }) + .isNotEmpty() + .allSatisfy { + assertThat(it.kind).isEqualTo("class") + assertThat(it.modifiers).doesNotContain("defaultLibrary") + } + } + } + + @Test + fun `library modifier follows bundled ownership not type spelling or any binary dependency`() { + CompilerTestSupport.configure() + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule( + Source("module External { class JsonObject {} }", "file:///External.x"), + null, + errors, + ) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + val binary = XdkDependency.fromBinary(compilation.toDependency().bytes()) + val source = + """ + module Ownership { + package json import json.xtclang.org; + package external import External; + import json.JsonObject as Document; + class JsonObject {} + Document library(Document value) = value; + JsonObject local(JsonObject value) = value; + external.JsonObject binary(external.JsonObject value) = value; + List core(List value) = value; + } + """.trimIndent() + XdkAdapter().use { adapter -> + adapter.replaceDependencies(listOf(binary)) + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(URI))) + assertThat(tokens.filter { it.text in setOf("Document", "List", "String") }) + .isNotEmpty() + .allSatisfy { assertThat(it.modifiers).contains("defaultLibrary") } + assertThat(tokens.filter { it.text == "JsonObject" && it.line >= 4 }) + .hasSize(5) + .allSatisfy { assertThat(it.modifiers).doesNotContain("defaultLibrary") } + assertThat(tokens.filter { it.text == "value" }) + .isNotEmpty() + .allSatisfy { assertThat(it.modifiers).doesNotContain("defaultLibrary") } + } + } + + @ParameterizedTest + @ValueSource(strings = ["class Marked {}", "mixin Marked into Object {}", ""]) + fun `annotation syntax alone never supplies decorator semantics`(declaration: String) { + val source = + """ + module InvalidAnnotation { + $declaration + @Marked class Child {} + } + """.trimIndent() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).success).isFalse() + val tokens = adapter.getSemanticTokens(URI)?.let { decode(source, it) }.orEmpty() + assertThat(tokens).noneMatch { it.kind == "decorator" } + } + } + + @Test + fun `real platform buffer distinguishes resolved categories and lexical text`() { + val source = javaClass.getResource("/platform/CircularBuffer.x")!!.readText() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(URI))) + listOf( + "CircularBuffer" to "class", + "Element" to "typeParameter", + "UniformIndexed" to "interface", + "Iterator" to "interface", + "contents" to "property", + "indexFor" to "method", + ).forEach { (name, kind) -> + assertThat(tokens.filter { it.text == name }) + .describedAs(name) + .isNotEmpty() + .allSatisfy { assertThat(it.kind).isEqualTo(kind) } + } + // Identical spelling has different roles; a lexical naming rule cannot decide these. + assertThat(tokens.filter { it.text == "index" }.map { it.kind }) + .contains("parameter", "property", "variable") + assertThat(tokens.filter { it.text == "head" && "modification" in it.modifiers }).isNotEmpty() + assertThat(tokens).anySatisfy { + assertThat(it.kind).isEqualTo("comment") + assertThat(it.modifiers).contains("documentation") + } + assertThat(tokens).noneMatch { it.text == "\"[]\"" } // Preserve the lexical annotation argument scope. + } + } + + @ParameterizedTest + @ValueSource(strings = ["this.CircularBuffer.si", "this.CircularBuffer."]) + fun `platform editing never reuses stale tokens and repair restores classification`(replacement: String) { + val source = javaClass.getResource("/platform/CircularBuffer.x")!!.readText() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + val before = requireNotNull(adapter.getSemanticTokens(URI)) + val changed = "// Shift every declaration while editing.\n" + source.replace("this.CircularBuffer.size", replacement) + assertThat(adapter.compile(URI, changed).success).isFalse() + // If recovery cannot build an AST, an empty result lets the editor use TextMate. + val tokens = adapter.getSemanticTokens(URI)?.let { decode(changed, it) }.orEmpty() + assertThat(tokens.filter { it.kind != "comment" }).noneMatch { it.line == 0 } + assertThat(tokens.filter { it.kind in setOf("class", "property", "method", "typeParameter") }) + .allSatisfy { assertThat(it.text).matches("[A-Za-z_][A-Za-z_0-9]*") } + assertThat(tokens).noneMatch { it.text == "si" } + tokens.zipWithNext().forEach { (previous, next) -> + assertThat(previous.line < next.line || previous.column + previous.text.length <= next.column) + .describedAs("tokens must not overlap: %s then %s", previous, next) + .isTrue() + } + assertThat(adapter.compile(URI, source).diagnostics).isEmpty() + assertThat(adapter.getSemanticTokens(URI)).isEqualTo(before) + } + } + + @Test + fun `demo classifies resolved identities regardless of spelling or source availability`() { + val source = root.resolve("lang/test-fixtures/semantic-highlighting/SemanticColors.x").readText() + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, source) + assertThat(result.diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(URI))) + listOf( + "counter" to "class", + "Reader" to "interface", + "Mode" to "enum", + "Quiet" to "enumMember", + "Loud" to "enumMember", + "Count" to "parameter", + "count" to "property", + "next" to "variable", + "create" to "function", + "read" to "method", + // List comes from the bundled binary library, with no local type declaration. + "List" to "interface", + ).forEach { (name, kind) -> + assertThat(tokens.filter { it.text == name }) + .describedAs(name) + .isNotEmpty() + .allSatisfy { assertThat(it.kind).isEqualTo(kind) } + } + assertThat(tokens.filter { it.text == "Quiet" }).hasSize(2) + assertThat(tokens.filter { it.text == "create" }).hasSize(2) + assertThat(tokens.filter { it.text == "create" }) + .allSatisfy { assertThat(it.modifiers).contains("static") } + assertThat(tokens.single { it.text == "construct" }.kind).isEqualTo("method") + assertThat(tokens.single { it.text.startsWith("/**") }.modifiers).containsExactly("documentation") + assertThat(tokens.single { it.text == "count" && "modification" in it.modifiers }.line) + .isEqualTo(source.lines().indexOfFirst { "count += 1" in it }) + } + } + + @Test + fun `shared semantic playbook classifications remain valid`() { + val cases = + JsonParser + .parseString(root.resolve("lang/test-fixtures/compiler-playbook/scenarios.json").readText()) + .asJsonObject + .getAsJsonObject("cases") + val data = cases.getAsJsonObject("X154").getAsJsonObject("values") + val source = data["source"].asString + val uri = "file:///${data["file"].asString}" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + val tokens = decode(source, requireNotNull(adapter.getSemanticTokens(uri))) + data.getAsJsonArray("tokenKinds").forEach { row -> + val expected = row.asJsonObject + assertThat(tokens).anySatisfy { + assertThat(it.text).isEqualTo(expected["name"].asString) + assertThat(it.kind).isEqualTo(expected["type"].asString) + } + } + } + } + + private data class Token( + val line: Int, + val column: Int, + val text: String, + val kind: String, + val modifiers: Set, + ) + + private fun decode( + source: String, + tokens: SemanticTokens, + ): List { + val lines = source.lines() + return tokens.data + .chunked(5) + .runningFold(Token(0, 0, "", "", emptySet())) { previous, delta -> + val line = previous.line + delta[0] + val column = (if (delta[0] == 0) previous.column else 0) + delta[1] + Token( + line, + column, + lines[line].substring(column, column + delta[2]), + SemanticTokenLegend.tokenTypes[delta[3]], + SemanticTokenLegend.tokenModifiers.filterIndexed { index, _ -> delta[4] and (1 shl index) != 0 }.toSet(), + ) + }.drop(1) + } + + private companion object { + const val URI = "file:///SemanticColors.x" + val root: Path = Path.of(System.getProperty("xtc.composite.root")) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt new file mode 100644 index 00000000000..069e98c2fd4 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt @@ -0,0 +1,758 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.semanticSnapshots +import org.xvm.lsp.model.Location +import java.nio.file.Path +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS +import java.net.URI as SourceUri + +class XdkSemanticLookupTest { + @TempDir lateinit var directory: Path + + private fun assertBundledType( + locations: List, + name: String, + ) { + assertThat(locations).hasSize(1) + val target = locations.single() + val text = Path.of(SourceUri(target.uri)).toFile().readLines()[target.startLine] + assertThat(text.substring(target.startColumn, target.endColumn)).isEqualTo(name) + } + + @Test + fun `generic selected returns navigate to the inferred source type and not the formal`() { + val source = + """ + module Lookups { + class /*item*/Item {} + T identity(T value) = value; + void run(Item value) { /*call*/identity(value); } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(types(adapter, source, "call")) + .containsExactly(location(source, "item", "Item")) + } + } + + @Test + fun `covariant source overrides remain distinct from unrelated return types`() { + val source = + """ + module Lookups { + class Value {} + class Detail extends Value {} + class Base { Value /*base*/make() = new Value(); } + class Child extends Base { @Override Detail /*child*/make() = new Detail(); } + class Unrelated { Detail make() = new Detail(); } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "base")) + .containsExactly( + location(source, "base", "make"), + location(source, "child", "make"), + ) + } + } + + @Test + fun `lookup results retain their source snapshot and stale closed document queries return nothing`() { + val source = + "module Lookups { class /*item*/Item {} void run(Item value) { /*use*/value.toString(); } }" + withSource(source) { adapter -> + val old = types(adapter, source, "use") + adapter.closeDocument(URI) + assertThat(types(adapter, source, "use")).isEmpty() + assertThat(implementations(adapter, source, "item")).isEmpty() + assertThat(adapter.compile(URI, "\n$source").success).isTrue() + assertThat(types(adapter, "\n$source", "use")) + .containsExactly(location("\n$source", "item", "Item")) + assertThat(old).containsExactly(location(source, "item", "Item")) + } + } + + @Test + fun `type definitions follow nominal parameterized nullable and aliased value types`() { + val source = + """ + module Lookups { + class /*box*/Box {} + class /*item*/Item {} + typedef Box as /*alias*/Items; + void run(Box box, Box? optional, Items alias) { + /*boxUse*/box.toString(); + /*nullableUse*/optional.toString(); + /*aliasUse*/alias.toString(); + } + } + """.trimIndent() + withSource(source) { adapter -> + for (use in listOf("boxUse", "nullableUse", "aliasUse")) { + assertThat(types(adapter, source, use)) + .containsExactly(location(source, "box", "Box")) + } + assertThat(types(adapter, source, "alias")) + .containsExactly(location(source, "alias", "Items")) + } + } + + @Test + fun `type definitions follow narrowing union operands and selected call return types`() { + val source = + """ + module Lookups { + class /*first*/First {} + class /*second*/Second {} + First /*declaration*/make() = new First(); + void run(First|Second value) { + /*union*/value.toString(); + if (value.is(First)) { /*narrowed*/value.toString(); } + /*call*/make(); + } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(types(adapter, source, "union")) + .containsExactly( + location(source, "first", "First"), + location(source, "second", "Second"), + ) + for (use in listOf("narrowed", "call", "declaration")) { + assertThat(types(adapter, source, use)) + .containsExactly(location(source, "first", "First")) + } + } + } + + @Test + fun `unresolved formals lead to their parameter declaration instead of their constraint`() { + val source = + """ + module Lookups { + class Box { + T echo(T value) = /*use*/value; + } + U echo(U value) = /*methodUse*/value; + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(types(adapter, source, "use")) + .containsExactly(location(source, "formal", "T")) + assertThat(types(adapter, source, "methodUse")) + .containsExactly(location(source, "methodFormal", "U")) + } + } + + @Test + fun `abstract parameter declarations expose their compiler signature type without a register`() { + val source = + "module Lookups { interface Mapper { T map(T /*use*/value); String text(String /*library*/value); } }" + withSource(source) { adapter -> + assertThat(types(adapter, source, "use")) + .containsExactly(location(source, "formal", "T")) + assertBundledType(types(adapter, source, "library"), "String") + } + } + + @Test + fun `library types navigate to matching sources and unresolved names have no invented target`() { + val source = "module Lookups { void run(String value) { /*use*/value.toString(); } }" + withSource(source) { adapter -> + assertBundledType(types(adapter, source, "use"), "String") + val broken = source.replace("/*use*/value", "/*use*/missing") + assertThat(adapter.compile(URI, broken).success).isFalse() + assertThat(types(adapter, broken, "use")).isEmpty() + } + } + + @Test + fun `type implementations are transitive concrete source types and exclude unrelated shapes`() { + val source = + """ + module Lookups { + interface /*api*/Named { String name(); } + @Abstract class Partial implements Named {} + class /*base*/Base extends Partial { @Override String name() = "base"; } + class /*child*/Child extends Base {} + class Unrelated { String name() = "other"; } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "api")) + .containsExactly( + location(source, "base", "Base"), + location(source, "child", "Child"), + ) + } + } + + @Test + fun `method implementations use generic override chains and exclude overloads and unrelated names`() { + val source = + """ + module Lookups { + interface Mapper { T /*api*/map(T value); } + class TextMapper implements Mapper { + @Override String /*base*/map(String value) = value; + String map(Int value) = value.toString(); + } + class Child extends TextMapper { @Override String /*child*/map(String value) = value; } + class Inherited extends TextMapper {} + class Unrelated { String map(String value) = value; } + void run(Mapper mapper) { mapper. /*call*/map("value"); } + } + """.trimIndent() + withSource(source) { adapter -> + for (use in listOf("api", "call", "base")) { + assertThat(implementations(adapter, source, use)) + .describedAs(use) + .containsExactly( + location(source, "base", "map"), + location(source, "child", "map"), + ) + } + assertThat(implementations(adapter, source, "child")) + .containsExactly(location(source, "child", "map")) + } + } + + @Test + fun `delegation resolves concrete method and property targets without guessing unrelated bodies`() { + val source = + """ + module Lookups { + interface Api { T /*api*/map(T value); @RO T /*property*/name; } + class Engine implements Api { + @Override String /*method*/map(String value) = value; + String map(Int value) = value.toString(); + @Override String name. /*getter*/get() = "engine"; + } + class Forward(Engine target) delegates Api(target) {} + class Outer(Forward target) delegates Api(target) {} + class Unrelated { String map(String value) = value; String name.get() = "unrelated"; } + String read(Outer forward) = forward. /*call*/map(forward. /*use*/name); + } + """.trimIndent() + withSource(source) { adapter -> + for (marker in listOf("api", "call")) { + assertThat(implementations(adapter, source, marker)) + .describedAs(marker) + .containsExactly(location(source, "method", "map")) + } + for (marker in listOf("property", "use")) { + assertThat(implementations(adapter, source, marker)) + .describedAs(marker) + .containsExactly(location(source, "getter", "get")) + } + } + } + + @Test + fun `interface valued and cyclic delegates have no invented implementation`() { + val source = + """ + module Lookups { + interface Api { Int /*api*/read(); @RO Int /*property*/value; } + class Dynamic(Api target) delegates Api(target) {} + class Cyclic(Cyclic target) delegates Api(target) {} + class Unrelated { Int read() = 1; Int value = 1; } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "api")).isEmpty() + assertThat(implementations(adapter, source, "property")).isEmpty() + } + } + + @Test + fun `property implementations follow generic contracts and exclude unrelated declarations`() { + val source = + """ + module Lookups { + interface Named { T /*api*/name; } + class Stored implements Named { @Override String /*field*/name = "stored"; } + class Computed implements Named { @Override String name { String /*getter*/get() = "computed"; } } + class Inherited extends Stored {} + class Unrelated { String name = "other"; } + String read(Named value) = value. /*use*/name; + } + """.trimIndent() + withSource(source) { adapter -> + for (use in listOf("api", "use")) { + assertThat(implementations(adapter, source, use)) + .describedAs(use) + .containsExactlyInAnyOrder( + location(source, "field", "name"), + location(source, "getter", "get"), + ) + } + } + } + + @Test + fun `property and accessor queries retain separate effective getter and setter chains`() { + val source = + """ + module Lookups { + class Base { + Int /*property*/value { + Int /*get*/get() = 1; + void /*set*/set(Int value) {} + } + } + class Child extends Base { + @Override Int value { Int /*override*/get() = 2; } + } + class Unrelated { Int value { Int get() = 3; void set(Int value) {} } } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "property")) + .containsExactlyInAnyOrder( + location(source, "get", "get"), + location(source, "set", "set"), + location(source, "override", "get"), + ) + assertThat(implementations(adapter, source, "get")) + .containsExactlyInAnyOrder( + location(source, "get", "get"), + location(source, "override", "get"), + ) + assertThat(implementations(adapter, source, "set")) + .containsExactly(location(source, "set", "set")) + } + } + + @Test + fun `default property getters yield to fields and deduplicate inherited bodies`() { + val source = + """ + module Lookups { + interface Named { @RO String /*api*/name { @Override String /*default*/get() = "default"; } } + class First implements Named {} + class Inherited extends First {} + class Stored implements Named { @Override String /*field*/name = "stored"; } + interface Missing { @RO String /*missing*/name; } + Int size(String value) = value. /*binary*/size; + } + """.trimIndent() + withSource(source) { adapter -> + for (use in listOf("api", "default")) { + assertThat(implementations(adapter, source, use)) + .containsExactlyInAnyOrder( + location(source, "default", "get"), + location(source, "field", "name"), + ) + } + assertThat(implementations(adapter, source, "missing")).isEmpty() + assertThat(implementations(adapter, source, "binary")).isEmpty() + } + } + + @Test + fun `covariant property accessors navigate through written narrowing implementations`() { + val source = + """ + module Lookups { + interface Api { @RO Object /*api*/value; } + class Box implements Api { @Override String value. /*getter*/get() = "text"; } + Object read(Api box) = box. /*use*/value; + } + """.trimIndent() + withSource(source) { adapter -> + listOf("api", "use").forEach { marker -> + assertThat(implementations(adapter, source, marker)).containsExactly(location(source, "getter", "get")) + } + } + } + + @Test + fun `conditional mixin invocation navigates to its selected written body`() { + val source = + """ + module Lookups { + class Box(T value) incorporates conditional Textual {} + static mixin Textual into Box { Int /*body*/size() = value.size; } + Int run(Box text) = text. /*call*/size(); + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "call")).containsExactly(location(source, "body", "size")) + } + } + + @Test + fun `short property accessors and composed mixins retain written getter targets`() { + val source = + """ + module Lookups { + class Base { Int /*field*/value = 1; } + mixin Loud into Base { @Override Int value. /*getter*/get() = 2; } + mixin Unused into Base { @Override Int value.get() = 3; } + class Host extends Base incorporates Loud {} + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(adapter.getCachedResult(URI)!!.diagnostics).isEmpty() + assertThat(implementations(adapter, source, "field")) + .containsExactlyInAnyOrder( + location(source, "field", "value"), + location(source, "getter", "get"), + ) + assertThat(implementations(adapter, source, "getter")) + .containsExactly(location(source, "getter", "get")) + } + } + + @Test + fun `annotated property implementations use the composed generic getter and setter`() { + val source = + """ + module Lookups { + interface HasValue { T /*api*/value; } + annotation Tracked into Var { + @Override T /*getter*/get() = super(); + @Override void /*setter*/set(T value) { super(value); } + } + annotation Unused into Var { @Override T get() = super(); } + class Holder implements HasValue { @Override @Tracked Int /*property*/value = 1; } + class Child extends Holder {} + Int read(Holder holder) = holder. /*use*/value; + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(adapter.getCachedResult(URI)!!.diagnostics).isEmpty() + for (marker in listOf("api", "property", "use")) { + assertThat(implementations(adapter, source, marker)) + .describedAs(marker) + .containsExactlyInAnyOrder( + location(source, "getter", "get"), + location(source, "setter", "set"), + ) + } + } + } + + @Test + fun `annotation order and explicit accessors retain their effective source targets`() { + val source = + """ + module Lookups { + annotation First into Var { + @Override T /*firstGet*/get() = super(); + @Override void /*firstSet*/set(T value) { super(value); } + } + annotation Second into Var { + @Override T /*secondGet*/get() = super(); + @Override void /*secondSet*/set(T value) { super(value); } + } + class Holder { + @First @Second Int /*forward*/value = 1; + @Second @First Int /*reverse*/other = 2; + @First @Second Int /*custom*/written { + @Override Int /*explicitGet*/get() = 3; + @Override void /*explicitSet*/set(Int value) {} + } + } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(adapter.getCachedResult(URI)!!.diagnostics).isEmpty() + for ( + (property, getter, setter) in + listOf( + Triple("forward", "firstGet", "firstSet"), + Triple("reverse", "secondGet", "secondSet"), + Triple("custom", "explicitGet", "explicitSet"), + ) + ) { + assertThat(implementations(adapter, source, property)) + .describedAs(property) + .containsExactlyInAnyOrder( + location(source, getter, "get"), + location(source, setter, "set"), + ) + } + assertThat(implementations(adapter, source, "explicitGet")) + .containsExactly(location(source, "explicitGet", "get")) + assertThat(implementations(adapter, source, "explicitSet")) + .containsExactly(location(source, "explicitSet", "set")) + } + } + + @Test + fun `Ref annotations and concrete delegates reach the written getter`() { + val source = + """ + module Lookups { + interface Named { @RO String /*api*/name; } + annotation Read into Ref { @Override T /*getter*/get() = super(); } + class Holder implements Named { @Override @Read String /*property*/name = "value"; } + class Forward(Holder target) delegates Named(target) {} + String read(Forward forward) = forward. /*use*/name; + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(adapter.getCachedResult(URI)!!.diagnostics).isEmpty() + for (marker in listOf("api", "property", "use")) { + assertThat(implementations(adapter, source, marker)) + .describedAs(marker) + .containsExactly(location(source, "getter", "get")) + } + } + } + + @Test + fun `abstract delegated and annotated properties have no invented implementation`() { + val source = + """ + module Lookups { + interface Named { @RO String /*api*/name; } + class Forward(Named target) delegates Named(target) {} + @Abstract class Missing { @Abstract Int /*abstract*/value; } + class Delayed { @Lazy Int /*lazy*/value.calc() = 1; } + } + """.trimIndent() + withSource(source) { adapter -> + for (use in listOf("api", "abstract", "lazy")) { + assertThat(implementations(adapter, source, use)).describedAs(use).isEmpty() + } + } + } + + @Test + fun `property implementations in closed members track overlays and disappear after failure`() { + directory = directory.toRealPath() + val root = directory.resolve("Lookups.x").toFile() + val member = directory.resolve("Lookups/Child.x").toFile() + val source = "module Lookups { interface Named { @RO String /*api*/name; } }" + val original = + "class Child implements Named { @Override String name { String /*getter*/get() = \"child\"; } }" + root.writeText(source) + member.parentFile.mkdirs() + member.writeText(original) + val rootUri = root.toURI().toString() + val memberUri = member.toURI().toString() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(rootUri, source).diagnostics).isEmpty() + assertThat(implementations(adapter, source, "api", rootUri)) + .containsExactly(location(original, "getter", "get", memberUri)) + val changed = "\n\n$original" + assertThat(adapter.compile(memberUri, changed).diagnostics).isEmpty() + assertThat(implementations(adapter, source, "api", rootUri)) + .containsExactly(location(changed, "getter", "get", memberUri)) + assertThat(adapter.compile(memberUri, "class Child {").success).isFalse() + assertThat(implementations(adapter, source, "api", rootUri)).isEmpty() + adapter.closeDocument(memberUri) + assertThat(adapter.compile(rootUri, source).diagnostics).isEmpty() + assertThat(implementations(adapter, source, "api", rootUri)) + .containsExactly(location(original, "getter", "get", memberUri)) + } + } + + @Test + fun `default interface bodies are source implementations and inherited results are deduplicated`() { + val source = + """ + module Lookups { + interface Named { String /*default*/name() = "default"; } + class First implements Named {} + class Second extends First {} + class Other implements Named { @Override String /*override*/name() = "other"; } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(implementations(adapter, source, "default")) + .containsExactly( + location(source, "default", "name"), + location(source, "override", "name"), + ) + } + } + + @Test + fun `mixin method targets come from the final host composition without standalone diagnostics`() { + val source = + """ + module Lookups { + class Base { String /*base*/name() = "base"; } + mixin Loud into Base { @Override String /*mixin*/name() = "loud"; } + mixin Unused into Base { @Override String name() = "unused"; } + class Host extends Base incorporates Loud {} + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(adapter.getCachedResult(URI)!!.diagnostics).isEmpty() + assertThat(implementations(adapter, source, "base")) + .containsExactly( + location(source, "base", "name"), + location(source, "mixin", "name"), + ) + } + } + + @Test + fun `anonymous implementation bodies retain captured source type targets`() { + val source = + """ + module Lookups { + class /*item*/Item {} + interface Named { Item /*api*/name(); } + Named make(Item value) { + return new Named() { + @Override Item /*implementation*/name() = /*capture*/value; + }; + } + } + """.trimIndent() + withSource(source) { adapter -> + assertThat(types(adapter, source, "capture")) + .containsExactly(location(source, "item", "Item")) + assertThat(implementations(adapter, source, "api")) + .containsExactly(location(source, "implementation", "name")) + } + } + + @Test + fun `cancelled implementation inspection publishes no partial implementation set`() { + CompilerTestSupport.configure() + val source = "module Lookups { interface /*api*/Named {} class Item implements Named {} }" + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule(Source(source, URI), null, errors) + assertThat(compilation.succeeded()).isTrue() + val cancelled = ErrorListener.cancellable(errors) { true } + val model = compilation.semanticSnapshots(cancelled).single() + val at = position(source, "api") + assertThat(model.implementationLocationsAt(at.line, at.column)).isEmpty() + assertThat(errors.errors).isEmpty() + } + + @Test + fun `closed member targets and unsaved edits share the current module snapshot`() { + directory = directory.toRealPath() + val root = directory.resolve("Lookups.x").toFile() + val member = directory.resolve("Lookups/Child.x").toFile() + val source = + "module Lookups { interface /*api*/Named { String /*method*/name(); } void run(Child child) { /*use*/child.name(); } }" + root.writeText(source) + member.parentFile.mkdirs() + val original = + "class /*child*/Child implements Named { @Override String /*impl*/name() = \"child\"; }" + member.writeText(original) + val rootUri = root.toURI().toString() + val memberUri = member.toURI().toString() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(rootUri, source).diagnostics).isEmpty() + assertThat(types(adapter, source, "use", rootUri)) + .containsExactly(location(original, "child", "Child", memberUri)) + assertThat(implementations(adapter, source, "method", rootUri)) + .containsExactly(location(original, "impl", "name", memberUri)) + val changed = "\n\n$original" + assertThat(adapter.compile(memberUri, changed).diagnostics).isEmpty() + assertThat(types(adapter, source, "use", rootUri)) + .containsExactly(location(changed, "child", "Child", memberUri)) + assertThat(implementations(adapter, source, "method", rootUri)) + .containsExactly(location(changed, "impl", "name", memberUri)) + assertThat(adapter.compile(memberUri, "class Child {").success).isFalse() + assertThat(types(adapter, source, "use", rootUri)).isEmpty() + assertThat(implementations(adapter, source, "api", rootUri)).isEmpty() + adapter.closeDocument(memberUri) + assertThat(adapter.compile(rootUri, source).diagnostics).isEmpty() + assertThat(types(adapter, source, "use", rootUri)) + .containsExactly(location(original, "child", "Child", memberUri)) + } + } + + @Test + fun `copied lookups survive on another thread without a constant pool`() { + CompilerTestSupport.configure() + val source = + "module Lookups { interface Named {} class /*type*/Item implements /*api*/Named {} " + + "void run(Item value) { /*use*/value.toString(); } }" + val errors = ErrorList() + val compilation = + EmbeddingSupport.instance().compileModule(Source(source, URI), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + val passive = compilation.semanticSnapshots().single() + val inspected = compilation.semanticSnapshots(errors).single() + assertThat(errors.errors).isEmpty() + val use = position(source, "use") + val api = position(source, "api") + Executors.newSingleThreadExecutor().use { executor -> + executor + .submit { + assertThat(passive.typeDefinitionLocationsAt(use.line, use.column)).hasSize(1) + assertThat(passive.implementationLocationsAt(api.line, api.column)).isEmpty() + assertThat(inspected.implementationLocationsAt(api.line, api.column)).hasSize(1) + }.get(10, SECONDS) + } + } + + private fun withSource( + source: String, + test: (XdkAdapter) -> Unit, + ) { + XdkAdapter().use { adapter -> + val result = adapter.compile(URI, source) + assertThat(result.success).describedAs(result.diagnostics.toString()).isTrue() + test(adapter) + } + } + + private fun types( + adapter: XdkAdapter, + source: String, + marker: String, + uri: String = URI, + ): List { + val position = position(source, marker) + return adapter.findTypeDefinitions(uri, position.line, position.column) + } + + private fun implementations( + adapter: XdkAdapter, + source: String, + marker: String, + uri: String = URI, + ): List { + val position = position(source, marker) + return adapter.findImplementation(uri, position.line, position.column) + } + + private fun position( + source: String, + marker: String, + ): Position { + val token = "/*$marker*/" + val offset = source.indexOf(token).also { check(it >= 0) } + token.length + return Position( + source.take(offset).count { it == '\n' }, + offset - source.lastIndexOf('\n', offset - 1) - 1, + ) + } + + private fun location( + source: String, + marker: String, + name: String, + uri: String = URI, + ): Location { + val start = position(source, marker) + return Location(uri, start.line, start.column, start.line, start.column + name.length) + } + + private companion object { + const val URI = "file:///Lookups.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt new file mode 100644 index 00000000000..a1903d1123c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt @@ -0,0 +1,125 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.SemanticModel +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkLibrarySources +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkSourceLinksTest { + @TempDir lateinit var directory: Path + + @Test + fun `module and aliased type import links resolve to source without spelling guesses`() { + val library = source("Library", "module Library { class Box {} }") + val text = + "module Consumer {\r\n package lib import Library;\r\n" + + " import lib.Box as Crate;\r\n Crate value = new Crate();\r\n}" + val uri = source("Consumer", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val links = adapter.getDocumentLinks(uri, text) + assertThat(links.map { it.target }).containsExactly(library, library) + assertThat(links.map { spelling(text, it.range) }).containsExactly("Library", "Crate") + assertThat(adapter.getDocumentLinks(uri, text.replace("Library;", "Missing;"))).isEmpty() + adapter.compile(uri, text.replace("Library;", "Missing;")) + assertThat(adapter.getDocumentLinks(uri, text.replace("Library;", "Missing;"))).isEmpty() + } + } + + @Test + fun `bundled explicit and wildcard import links retain read only source ownership`() { + val text = "module Links { import ecstasy.maps.ListMap as MapImpl; MapImpl value = new MapImpl(); }" + XdkAdapter().use { adapter -> + val uri = "untitled:Links.x" + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val link = adapter.getDocumentLinks(uri, text).single() + assertThat(spelling(text, link.range)).isEqualTo("MapImpl") + assertThat(XdkLibrarySources.owns(requireNotNull(link.target))).isTrue() + assertThat(adapter.formatDocument(link.target, "module ReadOnly {}", FormattingOptions(4, true))).isEmpty() + val wildcard = "module Links { import ecstasy.collections.*; }" + assertThat(adapter.compile(uri, wildcard).diagnostics).isEmpty() + val container = adapter.getDocumentLinks(uri, wildcard).single() + assertThat(spelling(wildcard, container.range)).isEqualTo("ecstasy.collections") + assertThat(XdkLibrarySources.owns(requireNotNull(container.target))).isTrue() + } + } + + @Test + fun `explicit import aliases link only uses in the resolved lexical scope`() { + val text = + "module Aliases { class Box {} class Other {}\n" + + " Int first() { import Box as Item; Item value = new Item(); return 1; }\n" + + " Int second() { import Other as Item; Item value = new Item(); return 2; }\n}" + XdkAdapter().use { adapter -> + val uri = "untitled:Aliases.x" + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + listOf(1, 2).forEach { line -> + val column = text.lines()[line].indexOf("Item") + val linked = requireNotNull(adapter.getLinkedEditingRanges(uri, line, column)) + assertThat(linked.ranges).hasSize(3) + assertThat(linked.ranges.map { it.start.line }).containsOnly(line) + assertThat(linked.ranges.map { spelling(text, it) }).containsOnly("Item") + } + assertThat(adapter.getLinkedEditingRanges(uri, 0, text.indexOf("Box"))).isNull() + adapter.compile(uri, text.replace("new Item()", "new Missing()")) + assertThat(adapter.getLinkedEditingRanges(uri, 1, text.lines()[1].indexOf("Item"))).isNull() + } + } + + @Test + fun `wildcard imports link resolved containers and unsupported conditional syntax cannot invent links`() { + val library = source("Library", "module Library { package tools { class Box {} } }") + listOf("import lib.tools.*;", "if (true) { import lib.tools.Box as Crate; }").forEach { statement -> + val text = + """ + module Consumer { + package lib import Library; + $statement + } + """.trimIndent().replace("\n", "\r\n") + val uri = source("Consumer", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val result = adapter.compile(uri, text) + if (statement.startsWith("if")) { + // The parser does not currently construct conditional ImportStatements from source. + assertThat(result.success).isFalse() + assertThat(result.diagnostics).isNotEmpty() + assertThat(result.diagnostics.map { it.code }).doesNotContain("EMB-5") + assertThat(adapter.getDocumentLinks(uri, text)).isEmpty() + } else { + assertThat(result.diagnostics).isEmpty() + val links = adapter.getDocumentLinks(uri, text) + assertThat(links.map { it.target }).containsExactly(library, library) + assertThat(links.map { spelling(text, it.range) }).containsExactly("Library", "lib.tools") + } + assertThat(adapter.getDocumentLinks(uri, text.replace("lib.tools", "lib.missing"))).isEmpty() + } + } + } + + private fun source( + name: String, + text: String, + ): String = + directory + .resolve("$name.x") + .toFile() + .also { it.writeText(text) } + .canonicalFile + .toURI() + .toString() + + private fun spelling( + text: String, + range: Range, + ): String { + fun offset(at: Position) = requireNotNull(XdkRename.offset(text, SemanticModel.Position(at.line, at.column))) + return text.substring(offset(range.start), offset(range.end)) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt new file mode 100644 index 00000000000..ae5552d5eec --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt @@ -0,0 +1,216 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkSpecializedConstructorTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = + [ + "outer.new Part(\"x\", te", + "box.new(\"x\", te", + "new @Tagged Box(\"x\", te", + "Box result = new Box(\"x\", te", + "new Box(\"x\", te", + "new String[2](te", + "outer.new @Tagged Part(\"x\", te", + "outer.new Outer.Part(\"x\", te", + "new String[2](supply = te", + "new @Tagged Box(second = \"x\", first = te", + ], + ) + fun `specialized constructor arguments preserve compiler rules and source edits`(call: String) { + val prefix = HEADER + call + val complete = "${prefix.dropLast(2)}text); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, complete).diagnostics).describedAs(call).isEmpty() + val cached = adapter.compile(URI, "$prefix); } }") + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).describedAs(call).isNotNull() + assertThat(help!!.signatures.map { it.label }).allMatch { it.contains("String") } + assertThat(help.signatures.map { it.activeParameter }) + .containsOnly(if (call.contains("first = te")) 0 else 1) + val items = adapter.getCompletions(URI, 0, prefix.length) + assertThat(items.map { it.label }) + .containsExactlyInAnyOrderElementsOf( + if (call == "new Box(\"x\", te") { + listOf("text", "textNumber") + } else { + listOf("text") + }, + ) + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + for (suggestion in items) { + // Omitted class parameters can be inferred again as more arguments are supplied. + assertThat( + adapter + .compile(URI, "${prefix.dropLast(2)}${suggestion.label}); } }") + .diagnostics, + ).isEmpty() + } + val item = items.single { it.label == "text" } + assertThat(item.label).isEqualTo("text") + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "text", + ), + ) + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "module Editing { class Box { construct(T first, T second) {} } Box run(String text, Int textNumber) { return new Box(\"x\", te", + "module Editing { class Outer { class Part { construct(T first, T second) {} } void run(T text, Int textNumber) { new Part(text, te", + "module Editing { class Box { construct(String first, String second) {} } void run(String text, Int textNumber) { new T(\"x\", te", + ], + ) + fun `expected return types implicit parents and formal constructors use real contexts`(prefix: String) { + val suffix = if (prefix.contains("class Outer")) "); } } }" else "); } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, prefix.dropLast(2) + "text" + suffix).diagnostics) + .isEmpty() + val cached = adapter.compile(URI, prefix + suffix) + assertThat( + adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures.map { + it.activeParameter + }, + ).containsOnly(1) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("text") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + @Test + fun `array supplier supports elements without a default and rejects invalid dimensions`() { + val header = + "module Editing { class Item { construct(String text) {} } " + + "void run(Item valueItem, function Item(Int) valueSupplier, Int valueNumber) { " + XdkAdapter().use { adapter -> + for (argument in listOf("valueItem", "valueSupplier")) { + assertThat(adapter.compile(URI, header + "new Item[2]($argument); } }").diagnostics) + .isEmpty() + } + val prefix = header + "new Item[2](va" + adapter.compile(URI, "$prefix); } }") + assertThat( + adapter.getSignatureHelp(URI, 0, prefix.length)!!.signatures.map { + it.activeParameter + }, + ).containsOnly(1) + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactlyInAnyOrder("valueItem", "valueSupplier") + for ( + call in + listOf("new Item[True](va", "new Item[2, 3](va", "new Item[2](unknown = va") + ) { + val invalid = header + call + adapter.compile(URI, "$invalid); } }") + assertThat(adapter.getSignatureHelp(URI, 0, invalid.length)) + .describedAs(call) + .isNull() + } + } + } + + @Test + fun `specialized constructors reject unavailable parents and inaccessible or incompatible arguments`() { + XdkAdapter().use { adapter -> + for ( + call in + listOf( + "outer.new Part(True, te", + "box.new(True, te", + "text.new Part(\"x\", te", + "new Outer.Part(\"x\", te", + "new @Missing Box(\"x\", te", + ) + ) { + val prefix = HEADER + call + adapter.compile(URI, "$prefix); } }") + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)) + .describedAs(call) + .isNull() + assertThat(adapter.getCompletions(URI, 0, prefix.length)) + .describedAs(call) + .isEmpty() + } + } + } + + @Test + fun `anonymous construction exposes its inherited constructor`() { + val prefix = HEADER + "new Box(\"x\", te" + val suffix = ") { String extra = \"value\"; }; } }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, prefix.dropLast(2) + "text" + suffix).diagnostics) + .isEmpty() + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNotNull() + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("text") + } + } + + @Test + fun `constructor probes use unsaved sibling type definitions`() { + val root = directory.resolve("Editing.x").toFile().canonicalFile + val member = directory.resolve("Editing/Member.x").toFile().canonicalFile + member.parentFile.mkdirs() + root.writeText("module Editing { class Box { construct(T first, Int second) {} } }") + val prefix = + "class Member { void run(String text, Int textNumber) { Box value = new Box(\"x\", te" + member.writeText("$prefix); } }") + val uri = member.toURI().toString() + XdkAdapter().use { adapter -> + adapter.compile( + root.toURI().toString(), + root.readText().replace("Int second", "T second"), + ) + adapter.compile(uri, member.readText()) + val cached = adapter.getCachedResult(uri) + assertThat(adapter.getCompletions(uri, 0, prefix.length).map { it.label }) + .containsExactly("text") + assertThat(adapter.getCachedResult(uri)).isEqualTo(cached) + adapter.compile(root.toURI().toString(), root.readText()) + assertThat(adapter.getCompletions(uri, 0, prefix.length).map { it.label }) + .containsExactly("textNumber") + assertThat(root.readText()).contains("Int second") + } + } + + @Test + fun `array initializer still has a signature after the written supplier`() { + val prefix = HEADER + "new String[2](\"supplied\"" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, "$prefix); } }") + assertThat(cached.diagnostics).isEmpty() + val help = adapter.getSignatureHelp(URI, 0, prefix.length) + assertThat(help).isNotNull() + assertThat(help!!.signatures.single().activeParameter).isEqualTo(1) + assertThat(help.signatures.single().label).contains("supply") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + } + } + + private companion object { + const val URI = "untitled:Editing.x" + const val HEADER = + "module Editing { class Box { construct(T first, T second) {} } annotation Tagged into Object {} " + + "class Outer { class Part { construct(T first, T second) {} } } " + + "void run(Outer outer, Box box, String text, Int textNumber) { " + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt new file mode 100644 index 00000000000..3bfe4a1b079 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt @@ -0,0 +1,68 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkRename +import java.nio.file.Path + +class XdkStructuralTest { + @ParameterizedTest + @ValueSource(strings = ["\n", "\r\n", "\r"]) + fun `damaged shared fixtures retain current structure and strictly nested selections`(newline: String) { + val catalog = Path.of(System.getProperty("xtc.composite.root"), "lang/test-fixtures/compiler-playbook/scenarios.json") + val data = + JsonParser + .parseString( + catalog.toFile().readText(), + ).asJsonObject["cases"] + .asJsonObject["X248"] + .asJsonObject["values"] + .asJsonObject + XdkAdapter().use { adapter -> + data["variants"].asJsonArray.forEach { row -> + val variant = row.asJsonObject + val text = variant["source"].asString.replace("\n", newline) + val result = adapter.compile(URI, text) + assertThat(result.diagnostics).describedAs(text).isNotEmpty() + assertThat(result.diagnostics.map { it.code }).doesNotContain("EMB-5") + assertThat(adapter.findWorkspaceSymbols("").map { it.name }).containsAll(variant["symbols"].asJsonArray.map { it.asString }) + val folds = adapter.getFoldingRanges(URI) + assertThat(folds).anySatisfy { + assertThat(it.startLine).isEqualTo(variant["foldLine"].asInt) + assertThat(it.endLine).isEqualTo(4) + assertThat(it.endCharacter).isEqualTo(text.lines()[4].indexOf('}')) + } + val at = XdkRename.position(text, text.indexOf(variant["anchor"].asString)) + val chain = generateSequence(adapter.getSelectionRanges(URI, listOf(at)).single()) { it.parent }.map { it.range }.toList() + assertThat(chain).hasSizeGreaterThan(1).doesNotHaveDuplicates() + chain.forEach { + assertThat(compare(it.start, at)).isLessThanOrEqualTo(0) + assertThat(compare(it.end, at)).isGreaterThanOrEqualTo(0) + } + chain.zipWithNext().forEach { (child, parent) -> + assertThat(compare(parent.start, child.start)).isLessThanOrEqualTo(0) + assertThat(compare(parent.end, child.end)).isGreaterThanOrEqualTo(0) + assertThat(parent).isNotEqualTo(child) + } + } + assertThat(adapter.compile(URI, data["repaired"].asString).diagnostics).isEmpty() + assertThat(adapter.findWorkspaceSymbols("").map { it.name }).containsExactly("Structure", "repaired") + assertThat(adapter.getFoldingRanges(URI)).isEmpty() + adapter.closeDocument(URI) + assertThat(adapter.findWorkspaceSymbols("")).isEmpty() + assertThat(adapter.getFoldingRanges(URI)).isEmpty() + } + } + + private fun compare( + first: Position, + second: Position, + ) = compareValuesBy(first, second, Position::line, Position::column) + + private companion object { + const val URI = "untitled:Structure.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt new file mode 100644 index 00000000000..63270ae6dc2 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt @@ -0,0 +1,204 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSyntaxCompletions +import java.util.concurrent.CancellationException + +class XdkSyntaxCompletionTest { + @Test + fun `declaration names use written types and replace the entire identifier`() { + listOf( + "module Editing { String str§ange; }" to "string", + "module Editing { void run(String str§ange) {} }" to "string", + "module Editing { void run() { String str§ange = \"\"; } }" to "string", + "module Editing { ecstasy.text.StringBuffer str§ange; }" to "stringBuffer", + "module Editing { List li§stName; }" to "list", + "module Editing { HTTPClient ht§; class HTTPClient {} }" to "httpClient", + "module Editing { String? str§ange; }" to "string", + "module Editing { String[] str§ange; }" to "stringArray", + "module Editing { void run(function String(Int)? f§unc) {} }" to "fn", + "module Editing { void run((String | Int) va§l) {} }" to "value", + ).forEach { (marked, expected) -> + val item = complete(marked).single() + assertThat(item.label).describedAs(marked).isEqualTo(expected) + assertThat( + item.textEdit!! + .range.end.column - item.textEdit.range.start.column, + ).isEqualTo( + marked.substringAfter('§').takeWhile(Char::isJavaIdentifierPart).length + + marked.substringBefore('§').takeLastWhile(Char::isJavaIdentifierPart).length, + ) + assertThat(item.documentation).contains("not a resolved reference") + } + } + + @Test + fun `name proposals avoid existing identifiers and do not invent inferred or method names`() { + assertThat(complete("module Editing { String string; String st§; }").single().label).isEqualTo("string1") + listOf( + "module Editing { void ru§n() {} }", + "module Editing { class Cl§assName {} }", + "module Editing { void run() { val va§lue = 1; } }", + "module Editing { String string; void run() { string§; } }", + ).forEach { assertThat(complete(it)).describedAs(it).isEmpty() } + } + + @Test + fun `file member and statement contexts have distinct keywords and templates`() { + assertThat(complete("mo§").map { it.label }).containsExactly("module", "module declaration") + assertThat(complete("module Editing { cl§ }").map { it.label }).containsExactly("class", "class declaration") + assertThat(complete("module Editing { void run() { wh§ } }").map { it.label }).containsExactly("while", "while loop") + assertThat( + complete("module Editing { § }").map { + it.label + }, + ).contains("class", "void method").doesNotContain("return", "if block", "module") + assertThat( + complete("module Editing { void run() { § } }").map { + it.label + }, + ).contains("return", "if block").doesNotContain("void method", "module") + } + + @Test + fun `loop keywords respect nested callable boundaries`() { + assertThat(complete("module Editing { void run() { while (True) { § } } }").map { it.label }) + .contains("break", "continue") + assertThat(complete("module Editing { void run() { § } }").map { it.label }) + .doesNotContain("break", "continue") + assertThat(complete("module Editing { Object make() = new Object() { § }; }").map { it.label }) + .contains("void method", "class declaration") + .doesNotContain("return", "break") + assertThat(complete("module Editing { void run() { while (True) { function void() fn = () -> { § }; } } }").map { it.label }) + .contains("if block") + .doesNotContain("break", "continue") + } + + @Test + fun `additional templates compile as inserted and stay in their declaration or statement context`() { + listOf("interface declaration", "service declaration", "for range", "do loop", "try catch").forEach { label -> + val declaration = label.endsWith("declaration") + val marked = if (declaration) "module Editing { § }" else "module Editing { void run() { § } }" + val item = complete(marked).single { it.label == label } + XdkAdapter().use { adapter -> + assertThat(adapter.compile(URI, marked.replace("§", item.insertText)).diagnostics).describedAs(label).isEmpty() + } + } + } + + @Test + fun `keywords preserve written syntax and templates only fill vacant slots`() { + val keyword = complete("module Editing { void run() { ret§urn; } }").single() + assertThat(keyword.label).isEqualTo("return") + assertThat( + keyword.textEdit!! + .range.end.column - keyword.textEdit.range.start.column, + ).isEqualTo(6) + assertThat(keyword.snippet).isNull() + assertThat(complete("module Editing { void run() { wh§ile (False) {} } }").map { it.label }).containsExactly("while") + assertThat(complete("module Editing { void run() { Int value = 1; if§ } }").map { it.label }).contains("if block") + } + + @Test + fun `syntax completion stays out of literals comments member accesses arguments and types`() { + listOf( + "module Editing { // cl§\n}", + "module Editing { /* cl§ */ }", + "module Editing { String text = \"cl§\"; }", + "module Editing { String text = \"unterminated cl§ }", + "module Editing { void run() { text.cl§; } }", + "module Editing { void run() { call(cl§); } }", + "module Editing { void run() { Int value = cl§; } }", + "module Editing { void run() { return cl§; } }", + "module Editing { List value; }", + "module Editing { class Type extends cl§ {} }", + "module Editing { void run() { if (cl§) {} } }", + ).forEach { assertThat(complete(it)).describedAs(it).isEmpty() } + } + + @Test + fun `template edits preserve CRLF Unicode columns and indentation`() { + val marked = "module Editing {\r\n void run() {\r\n // 😀\r\n if§\r\n }\r\n}" + val template = complete(marked).single { it.label == "if block" } + assertThat( + template.textEdit, + ).isEqualTo(TextEdit(Range(Position(3, 8), Position(3, 10)), "if (True) {\r\n \r\n }")) + assertThat(template.snippet).isEqualTo("if (${'$'}{1:True}) {\r\n ${'$'}0\r\n }") + val name = complete("module Editing { /* 😀 */ String st§uff; }").single() + assertThat( + name.textEdit!! + .range.start.column, + ).isEqualTo("module Editing { /* 😀 */ String ".length) + } + + @Test + fun `parser work honors cancellation`() { + assertThatThrownBy { XdkSyntaxCompletions.complete("module Editing {}", Position(0, 0)) { true } } + .isInstanceOf(CancellationException::class.java) + } + + @Test + fun `adjacent punctuation and unfinished block boundaries retain statement context`() { + listOf( + "module Editing { void run() {§} }", + "module Editing { void run() { Int value = 1;§} }", + "module Editing { void run() { if§", + ).forEach { assertThat(complete(it).map { item -> item.label }).describedAs(it).contains("if block") } + assertThat(complete("module Editing { void run() { broken + ; if§ } }")).isEmpty() + } + + @Test + fun `lone carriage returns use the same original token coordinates`() { + val text = "module Editing {\r void run() {\r if\r }\r}" + val item = XdkSyntaxCompletions.complete(text, Position(2, 10)) { false }.single { it.label == "if block" } + assertThat(item.textEdit!!.range).isEqualTo(Range(Position(2, 8), Position(2, 10))) + assertThat(item.insertText).isEqualTo("if (True) {\r \r }") + } + + @Test + fun `adapter publishes syntax templates alongside compiler facts without changing diagnostics`() { + XdkAdapter().use { adapter -> + val marked = "module Editing { void run(String item) { § } }" + val text = marked.replace("§", "") + val original = adapter.compile(URI, text) + assertThat(original.diagnostics).isEmpty() + val items = adapter.getCompletions(URI, 0, marked.indexOf('§')) + assertThat(items.map { it.label }).contains("item", "if block", "return") + assertThat(adapter.getCachedResult(URI)).isEqualTo(original) + val template = items.single { it.label == "if block" } + val edited = text.replaceRange(marked.indexOf('§'), marked.indexOf('§'), template.insertText) + assertThat(adapter.compile(URI, edited).diagnostics).isEmpty() + } + } + + @Test + fun `adapter offers a name in a parsed declaration and a template in an unfinished file`() { + XdkAdapter().use { adapter -> + val text = "module Editing { String st; }" + adapter.compile(URI, text) + assertThat(adapter.getCompletions(URI, 0, text.indexOf("st;") + 2).map { it.label }).contains("string") + adapter.compile(URI, "mo") + assertThat(adapter.getCompletions(URI, 0, 2).map { it.label }).contains("module declaration") + } + } + + private fun complete(marked: String): List { + val before = marked.substringBefore('§') + return XdkSyntaxCompletions.complete( + marked.replace("§", ""), + Position( + before.count { + it == '\n' + }, + before.substringAfterLast('\n').length, + ), + ) { false } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTeachingWorkspaceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTeachingWorkspaceTest.kt new file mode 100644 index 00000000000..6bb3561c53a --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTeachingWorkspaceTest.kt @@ -0,0 +1,237 @@ +package org.xvm.lsp.adapter + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkWorkspaceDiscovery +import java.lang.ref.WeakReference +import java.nio.file.Path +import java.time.Duration + +/** + * The actual teaching sources, including companion files and all automatically discovered roots. + */ +class XdkTeachingWorkspaceTest { + @TempDir lateinit var directory: Path + + @Test + fun `delegate property names use the compiler composition binding`() { + val text = + "module Delegate { interface Api {} class Engine implements Api {} " + + "class Forward(Engine target) delegates Api(target) {} }" + val uri = directory.resolve("Delegate.x").toUri().toString() + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val use = text.lastIndexOf("target") + assertThat(adapter.findDefinition(uri, 0, use)?.startColumn) + .isEqualTo(text.indexOf("target")) + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `transitive consumers allow proven annotations but reject an incomplete graph`(broken: Boolean) { + val library = "module Library { class Box { Int number = 1; } }" + val unrelated = + "annotation Tracked into Var { @Override T get() = super(); } class State { @Tracked Int value = 1; } " + val use = if (broken) "box.number + missing" else "box.number" + val consumer = + "module Consumer { package lib import Library; $unrelated Int use(lib.Box box) = $use; }" + val libraryFile = directory.resolve("Library.x").toFile().apply { writeText(library) } + val consumerFile = directory.resolve("Consumer.x").toFile().apply { writeText(consumer) } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val uri = libraryFile.canonicalFile.toURI().toString() + val consumerUri = consumerFile.toURI().toString() + assertThat(adapter.compile(uri, library).diagnostics).isEmpty() + assertThat(adapter.compile(consumerUri, consumer).success).isEqualTo(!broken) + val edit = adapter.rename(uri, 0, library.indexOf("number"), "amount") + if (broken) { + assertThat(edit).isNull() + } else { + val changes = requireNotNull(edit).changes + assertThat(changes.values).allMatch { it.size == 1 } + assertThat(changes).hasSize(2) + changes.forEach { (document, edits) -> + val before = if (document.endsWith("/Library.x")) library else consumer + val change = edits.single() + assertThat(before.substring(change.range.start.column, change.range.end.column)) + .isEqualTo("number") + assertThat(change.newText).isEqualTo("amount") + } + assertThat(adapter.compile(uri, library.replace("number", "amount")).diagnostics) + .isEmpty() + assertThat( + adapter + .compile(consumerUri, consumer.replace("box.number", "box.amount")) + .diagnostics, + ).isEmpty() + } + assertThat(libraryFile.readText()).isEqualTo(library) + assertThat(consumerFile.readText()).isEqualTo(consumer) + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `teaching workspace property proof preserves live buffers and releases compiler state`(multipleBuffers: Boolean) { + CompilerTestSupport.configure() + val fixtures = teachingSources() + fixtures.forEach { (name, text) -> + directory.resolve(name).toFile().apply { + parentFile.mkdirs() + writeText(text) + } + } + val roots = + XdkWorkspaceDiscovery.scan(listOf(directory.toFile()), emptyMap(), emptyList()) { + false + } + // The original 24-root workspace now also includes 7a.9's initially empty Broken module. + assertThat(roots).hasSize(25) + val observed = mutableListOf>() + XdkAdapter( + { source, repository, errors -> + EmbeddingSupport.instance().compileModule(source, repository, errors) + }, + { sources, repository, errors -> + EmbeddingSupport.instance().compileModule(sources, repository, errors).also { compilation -> + observed += WeakReference(compilation) + compilation.pool()?.let { observed += WeakReference(it) } + compilation.sourceTrees().forEach { observed += WeakReference(it) } + } + }, + { _, _, _, _, _ -> error("A complete rename proof must not use cursor recovery") }, + ).use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val target = "X102/PropertyRename.x" + val text = fixtures.getValue(target) + + fun uri(file: String) = + directory + .resolve(file) + .toFile() + .canonicalFile + .toURI() + .toString() + val buffers = + buildMap { + put(uri(target), text + "\n// Unsaved target buffer\n") + if (multipleBuffers) { + listOf("Navigation.x", "Consumer.x", "Project/Child.x", "Properties.x") + .forEach { file -> + put( + uri(file), + "// Unsaved neighboring buffer\n" + fixtures.getValue(file), + ) + } + } + } + buffers.forEach { (uri, source) -> + assertThat(adapter.compile(uri, source).success).describedAs(uri).isTrue() + } + val diagnostics = + buffers.keys.associateWith { adapter.getCachedResult(it)?.diagnostics } + observed.clear() + val edit = + requireNotNull(adapter.rename(uri(target), 0, text.indexOf("value"), "amount")) + assertThat(edit.changes.keys).containsExactly(uri(target)) + assertThat(edit.changes.getValue(uri(target))).hasSize(4) + assertThat(edit.renames).isEmpty() + assertThat(buffers.keys.associateWith { adapter.getCachedResult(it)?.diagnostics }) + .isEqualTo(diagnostics) + released(observed) + + val collision = + buffers + .getValue(uri(target)) + .replace("class Base {", "class Base { Int amount = 3;") + assertThat(adapter.compile(uri(target), collision).success).isTrue() + val beforeRefusal = adapter.getCachedResult(uri(target)) + observed.clear() + assertThat(adapter.rename(uri(target), 0, collision.indexOf("value"), "amount")) + .isNull() + assertThat(adapter.getCachedResult(uri(target))).isEqualTo(beforeRefusal) + released(observed) + fixtures.forEach { (file, source) -> + assertThat(directory.resolve(file).toFile().readText()).isEqualTo(source) + } + println( + "Teaching workspace: roots=${roots.size}, open buffers=${buffers.size}, heap=${Runtime.getRuntime().maxMemory()} bytes", + ) + } + } + + private fun released(observed: List>) { + assertThat(observed).isNotEmpty() + await().atMost(Duration.ofSeconds(20)).pollInterval(Duration.ofMillis(100)).untilAsserted { + System.gc() + assertThat(observed.count { it.get() != null }) + .describedAs("query-owned compiler objects") + .isZero() + } + } + + /** Mirror the native workspace inputs, reading the shared catalog instead of copying XTC. */ + private fun teachingSources(): Map { + val lang = Path.of(System.getProperty("xtc.composite.root")).resolve("lang") + val catalog = + JsonParser + .parseString( + lang + .resolve("test-fixtures/compiler-playbook/scenarios.json") + .toFile() + .readText(), + ).asJsonObject + val manual = lang.resolve("doc/manual-test-plan.md").toFile().readText() + val blocks = + Regex("```xtc\\n([\\s\\S]*?)\\n```").findAll(manual).map { it.groupValues[1] }.toList() + val cases = catalog.getAsJsonObject("cases") + + fun values(id: String) = cases.getAsJsonObject(id).getAsJsonObject("values") + return buildMap { + catalog.getAsJsonObject("common").getAsJsonArray("fixtures").forEach { element -> + val fixture = element.asJsonObject + val pattern = Regex(fixture["pattern"].asString, RegexOption.MULTILINE) + put(fixture["file"].asString, blocks.single(pattern::containsMatchIn) + "\n") + } + listOf("X101", "X102", "X103", "X104").forEach { id -> + val data = values(id) + put( + (if (id == "X101") "" else "$id/") + data["file"].asString, + data["source"].asString, + ) + } + values("X99").let { data -> + put("X99/" + data["file"].asString, data["original"].asString) + put("X99/" + data["library"].asString, data["libraryText"].asString) + } + values("X100").let { data -> + put("X100/" + data["file"].asString, data["source"].asString) + put("X100/" + data["neighbor"].asString, data["neighborText"].asString) + } + values("X103").let { data -> + put("X103/" + data["member"].asString, data["memberSource"].asString) + } + values("X103").let { data -> + put("X103/" + data["companion"].asString, data["companionSource"].asString) + } + values("X105").let { data -> + put("X105/" + data["file"].asString, "module AutoImports {}") + put("X105/" + data["library"].asString, data["libraryText"].asString) + } + values("7a.9").let { data -> + put( + data["file"].asString, + data["moduleStart"].asString + data["moduleEnd"].asString, + ) + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt new file mode 100644 index 00000000000..0699d96f363 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt @@ -0,0 +1,195 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.asm.ErrorListener +import org.xvm.compiler.Parser +import org.xvm.compiler.Source +import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkTypeHeaderTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource( + strings = + [ + "class Damaged extends Ba§", + "interface Damaged extends Ba§", + "class Damaged implements Ba§", + "mixin Damaged into Ba§", + "class Damaged incorporates Ba§", + "class Damaged delegates Ba§(value)", + "class Damaged implements List", + "class Damaged implements List", + "class Damaged implements List + source.reset() + val stopped = + EmbeddingSupport.instance().analyzeIncomplete(source, cursor, null, listener) + assertThat(stopped.pool()).isNull() + assertThat(stopped.cursorBindings()).isEmpty() + } + } + + @Test + fun `qualified composition lookup excludes private types and value members`() { + val prefix = + "module Headers { import Owner as Alias; class Owner { class ItemPublic {} " + + "private class ItemHidden {} static Int ItemValue = 1; } class Damaged extends Alias.Ite" + XdkAdapter().use { adapter -> + adapter.compile(URI, "$prefix {} }") + assertThat(adapter.getCompletions(URI, 0, prefix.length).map { it.label }) + .containsExactly("ItemPublic") + } + } + + @Test + fun `a composition prefix at actual EOF completes without fabricated source`() { + val text = "module Headers { interface Damaged extends List + val cached = adapter.compile(URI, text) + assertThat(adapter.getCompletions(URI, 0, text.length).map { it.label }) + .contains("String") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(adapter.compile(URI, text.dropLast(3) + "String> {} }").diagnostics) + .isEmpty() + } + } + + @Test + fun `accepting a base type edits only the prefix and restores compiler diagnostics`() { + val prefix = "module Headers { class Base {} class Damaged extends Ba" + val suffix = " { Int inside = 1; } Int later = 2; }" + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, prefix + suffix) + val item = adapter.getCompletions(URI, 0, prefix.length).single { it.label == "Base" } + assertThat(item.textEdit) + .isEqualTo( + TextEdit( + Range(Position(0, prefix.length - 2), Position(0, prefix.length)), + "Base", + ), + ) + assertThat(adapter.getSignatureHelp(URI, 0, prefix.length)).isNull() + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + assertThat(cached.diagnostics).isNotEmpty() + assertThat(adapter.compile(URI, prefix.dropLast(2) + "Base" + suffix).diagnostics) + .isEmpty() + } + } + + @Test + fun `member file header lookup sees unsaved root types and preserves disk source`() { + val root = directory.resolve("Headers.x").toFile().canonicalFile + val child = directory.resolve("Headers/Child.x").toFile().canonicalFile + child.parentFile.mkdirs() + root.writeText("module Headers { class Owner { class ItemDisk {} } }") + val prefix = "class Child extends Owner.Ite" + child.writeText("$prefix { Int inside = 1; }") + XdkAdapter().use { adapter -> + val rootUri = root.toURI().toString() + val childUri = child.toURI().toString() + adapter.compile(rootUri, "module Headers { class Owner { class ItemOverlay {} } }") + val cached = adapter.compile(childUri, child.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .containsExactly("ItemOverlay") + assertThat(adapter.getCachedResult(childUri)?.diagnostics).isEqualTo(cached.diagnostics) + assertThat(adapter.getCachedResult(childUri)?.symbols).isEqualTo(cached.symbols) + adapter.compile(rootUri, root.readText()) + assertThat(adapter.getCompletions(childUri, 0, prefix.length).map { it.label }) + .containsExactly("ItemDisk") + assertThat(root.readText()).contains("ItemDisk") + } + } + + @Test + fun `malformed class headers retain outline and folds without stale semantic facts`() { + val text = + "module Headers {\n class Damaged extends {\n Int inside = 1;\n }\n Int later = 2;\n}" + XdkAdapter().use { adapter -> + adapter.compile(URI, "module Headers { class Old {} }") + val result = adapter.compile(URI, text) + assertThat(result.diagnostics).isNotEmpty() + assertThat(result.diagnostics.map { it.code }).doesNotContain("EMB-5") + assertThat(adapter.findWorkspaceSymbols("").map { it.name }) + .contains("Damaged", "inside", "later") + .doesNotContain("Old") + assertThat(adapter.getFoldingRanges(URI)).anySatisfy { + assertThat(it.startLine).isEqualTo(1) + assertThat(it.endLine).isEqualTo(3) + } + assertThat(adapter.prepareTypeHierarchy(URI, 1, 8)).isEmpty() + assertThat(adapter.findDefinition(URI, 2, 8)).isNull() + assertThat(adapter.compile(URI, text.replace("extends {", "{")).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "class Damaged extends Missing.Ow§ner.Base", + "class Damaged extends Owner.§", + "module Headers extends Missing.Ba§", + ], + ) + fun `unsupported composition prefixes do not invent a scope`(declaration: String) { + val prefix = + (if (declaration.startsWith("module")) "" else "module Headers { ") + + declaration.substringBefore('§') + val suffix = + declaration.substringAfter('§') + + " {}" + + if (declaration.startsWith("module")) "" else " }" + XdkAdapter().use { adapter -> + adapter.compile(URI, prefix + suffix) + assertThat(adapter.getCompletions(URI, 0, prefix.length)).isEmpty() + } + } + + private companion object { + const val URI = "untitled:Headers.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt new file mode 100644 index 00000000000..b05591f1c85 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt @@ -0,0 +1,650 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkTypeMoveTest { + @TempDir lateinit var directory: Path + + @Test + fun `moving a type between packages rewrites constructors and closed consumers`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box { static Int number() = 1; }") + write("App/util/Marker.x", "class Marker {}") + write( + "Consumer.x", + "module Consumer { package app import App; app.tools.Box make() = new app.tools.Box(); Int read() = app.tools.Box.number(); }", + ) + session { adapter -> + move(adapter) + assertThat(read("App.x")).contains("util.Box").doesNotContain("tools.Box") + assertThat(read("Consumer.x")).contains("app.util.Box", "app.util.Box.number()").doesNotContain("tools.Box") + } + } + + @Test + fun `moving a type preserves explicit import aliases and qualifies bare names`() { + write("App.x", "module App { import tools.Box as Crate; Crate make() = new Crate(); }") + write("App/tools/Box.x", "class Box {}") + write("App/tools/Factory.x", "class Factory { Box make() = new Box(); }") + write("App/util/Marker.x", "class Marker {}") + session { adapter -> + move(adapter) + assertThat(read("App.x")).contains("import util.Box as Crate", "Crate make() = new Crate()") + assertThat(read("App/tools/Factory.x")).contains("util.Box make() = new util.Box()") + } + } + + @Test + fun `moving companion sources preserves resources and old package sibling bindings`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box { Helper make() = new Helper(); }") + write("App/tools/Helper.x", "class Helper {}") + write("App/tools/Box/Part.x", "class Part {}") + write("App/tools/Box/data.txt", "keep this resource") + write("App/util/Marker.x", "class Marker {}") + session { adapter -> + val edit = move(adapter) + assertThat(edit.renames).containsEntry(uri("App/tools/Box"), uri("App/util/Box")) + assertThat(read("App/util/Box/data.txt")).isEqualTo("keep this resource") + assertThat(read("App/util/Box/Part.x")).isEqualTo("class Part {}") + assertThat(read("App/util/Box.x")).contains("tools.Helper") + } + } + + @Test + fun `move leaves unrelated same named types and their calls unchanged`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); other.Box second() = new other.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("App/other/Box.x", "class Box {}") + write("App/util/Marker.x", "class Marker {}") + session { adapter -> + move(adapter) + assertThat(read("App.x")).contains("util.Box make() = new util.Box()", "other.Box second() = new other.Box()") + } + } + + @Test + fun `target collisions refuse the entire move`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("App/util/Box.x", "class Box {}") + write("Other.x", "module Other {}") + write("Other/Marker.x", "class Marker {}") + session { adapter -> + assertThat(adapter.renameFilesAsync(request()).get(30, SECONDS)).isNull() + assertThat(read("App.x")).contains("tools.Box") + assertThat(directory.resolve("App/tools/Box.x")).exists() + } + } + + @Test + fun `moving back to the module namespace removes package prefixes and preserves nested types`() { + write("App.x", "module App { package other import Other; tools.Box.Part make() = new tools.Box.Part(); }") + write("Other.x", "module Other {}") + write("App/tools/Box.x", "class Box {}") + write("App/tools/Box/Part.x", "static class Part {}") + session { adapter -> + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val requested = mapOf(uri("App/tools/Box.x") to uri("App/Box.x")) + val edit = requireNotNull(adapter.renameFilesAsync(requested).get(30, SECONDS)) + apply(edit, requested) + assertThat(read("App.x")).isEqualTo("module App { package other import Other; Box.Part make() = new Box.Part(); }") + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `a compiling move that would capture an unrelated bare method is refused`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools.x", "package tools { static Int number() = 1; }") + write("App/tools/Box.x", "class Box { Int read() = number(); }") + write("App/util.x", "package util { static Int number() = 2; }") + write("App/util/Marker.x", "class Marker {}") + session { adapter -> + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(adapter.renameFilesAsync(request()).get(30, SECONDS)).isNull() + assertThat(read("App/tools/Box.x")).isEqualTo("class Box { Int read() = number(); }") + // Compilation alone accepts the wrong package's number(). The refusal above must + // come from comparing the original call target, not from a syntax/access failure. + write("App.x", read("App.x").replace("tools.Box", "util.Box")) + Files.move(directory.resolve("App/tools/Box.x"), directory.resolve("App/util/Box.x")) + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `occupied companion directory refuses a move before touching files`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("App/util/Marker.x", "class Marker {}") + write("App/util/Box/data.txt", "unrelated existing resource") + session { adapter -> + assertThat(adapter.renameFilesAsync(request()).get(30, SECONDS)).isNull() + assertThat(read("App/util/Box/data.txt")).isEqualTo("unrelated existing resource") + assertThat(directory.resolve("App/tools/Box.x")).exists() + } + } + + @Test + fun `configured source graph includes closed consumers without replacing settings`() { + write("App.x", "module App {}") + write("App/tools/Box.x", "class Box {}") + write("App/util/Marker.x", "class Marker {}") + write("Consumer.x", "module Consumer { package app import App; import app.tools.Box; Box make() = new Box(); }") + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri("App.x")), + XdkSourceModule("Consumer", uri("Consumer.x"), setOf("App")), + ), + ) + val edit = requireNotNull(adapter.renameFilesAsync(request()).get(30, SECONDS)) + apply(edit, request()) + assertThat(read("Consumer.x")).contains("import app.util.Box", "Box make() = new Box()") + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `same package file rename preserves explicit aliases and updates implicit imports`() { + write( + "App.x", + """ + module App { + import tools.Box as Kept; + Kept make() = new Kept(); + } + """.trimIndent(), + ) + write("App/tools/Box.x", "class Box {}") + write( + "Consumer.x", + """ + module Consumer { + package app import App; + import app.tools.Box; + Box make() = new Box(); + } + """.trimIndent(), + ) + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("App/tools/Crate.x"))) + assertThat(read("App.x")).contains("import tools.Crate as Kept", "Kept make() = new Kept()") + assertThat(read("Consumer.x")).contains("import app.tools.Crate", "Crate make() = new Crate()") + } + } + + @Test + fun `rename and package move preserve aliases bare names companions and closed consumers together`() { + write( + "App.x", + """ + module App { + import tools.Box as Kept; + Kept make() = new Kept(); + tools.Box.Part part() = new tools.Box.Part(); + } + """.trimIndent(), + ) + write( + "App/tools/Box.x", + """ + class Box { + static Int number() = 1; + Helper make() = new Helper(); + } + """.trimIndent(), + ) + write("App/tools/Box/Part.x", "static class Part {}") + write("App/tools/Box/data.txt", "preserved resource") + write("App/tools/Helper.x", "class Helper {}") + write("App/tools/Factory.x", "class Factory { Box make() = new Box(); }") + write("App/tools/Crate.x", "class Crate {}") + write("App/util/Marker.x", "class Marker {}") + write( + "Consumer.x", + """ + module Consumer { + package app import App; + import app.tools.Box; + Box make() = new Box(); + Int value() = app.tools.Box.number(); + } + """.trimIndent(), + ) + val original = sourceTexts() + session { adapter -> + val requested = mapOf(uri("App/tools/Box.x") to uri("App/util/Crate.x")) + val edit = move(adapter, requested) + assertThat(edit.renames).containsEntry(uri("App/tools/Box"), uri("App/util/Crate")) + assertThat(read("App.x")).contains("import util.Crate as Kept", "Kept make() = new Kept()", "util.Crate.Part") + assertThat(read("App/tools/Factory.x")).contains("util.Crate make() = new util.Crate()") + assertThat(read("App/util/Crate.x")).contains("class Crate", "tools.Helper make() = new tools.Helper()") + assertThat(read("Consumer.x")).contains("import app.util.Crate", "Crate make() = new Crate()", "app.util.Crate.number()") + assertThat(read("App/tools/Crate.x")).isEqualTo("class Crate {}") + assertThat(read("App/util/Crate/data.txt")).isEqualTo("preserved resource") + assertThat(read("App/util/Crate/Part.x")).isEqualTo("static class Part {}") + move(adapter, requested.entries.associate { (from, to) -> to to from }) + // Reverse refactoring need not restore the original shortest spelling (Undo does). + assertThat(sourceTexts().keys).containsExactlyInAnyOrderElementsOf(original.keys) + assertThat(read("App/tools/Box.x")).contains("class Box") + assertThat(read("Consumer.x")).contains("app.tools.Box") + } + } + + @Test + fun `combined move refuses collisions and invalid names without partial edits`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("App/util.x", "package util { class Taken {} }") + write("App/util/Marker.x", "class Marker {}") + val original = sourceTexts() + session { adapter -> + listOf("Taken", "not-a-name").forEach { name -> + val requested = mapOf(uri("App/tools/Box.x") to uri("App/util/$name.x")) + assertThat(adapter.renameFilesAsync(requested).get(30, SECONDS)).isNull() + } + assertThat(sourceTexts()).isEqualTo(original) + } + } + + @Test + fun `combined move refuses a changed unqualified method target even when final sources compile`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools.x", "package tools { static Int number() = 1; }") + write("App/tools/Box.x", "class Box { Int read() = number(); }") + write("App/util.x", "package util { static Int number() = 2; }") + write("App/util/Marker.x", "class Marker {}") + session { adapter -> + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val requested = mapOf(uri("App/tools/Box.x") to uri("App/util/Crate.x")) + assertThat(adapter.renameFilesAsync(requested).get(30, SECONDS)).isNull() + write("App.x", read("App.x").replace("tools.Box", "util.Crate")) + write("App/tools/Box.x", read("App/tools/Box.x").replace("class Box", "class Crate")) + Files.move(directory.resolve("App/tools/Box.x"), directory.resolve("App/util/Crate.x")) + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } + } + + @Test + fun `interacting type moves are planned together independently of request order`() { + write( + "App.x", + """ + module App { + left.First first() = new left.First(); + right.Second second() = new right.Second(); + } + """.trimIndent(), + ) + write("App/left/First.x", "class First { right.Second next() = new right.Second(); }") + write("App/right/Second.x", "class Second { left.First next() = new left.First(); }") + write("App/util/Marker.x", "class Marker {}") + write( + "Consumer.x", + """ + module Consumer { + package app import App; + app.left.First first() = new app.left.First(); + app.right.Second second() = new app.right.Second(); + } + """.trimIndent(), + ) + session { adapter -> + val requested = mapOf(uri("App/left/First.x") to uri("App/util/Alpha.x"), uri("App/right/Second.x") to uri("App/util/Beta.x")) + val first = requireNotNull(adapter.renameFilesAsync(requested).get(30, SECONDS)) + val reversed = requireNotNull(adapter.renameFilesAsync(requested.entries.reversed().associate { it.toPair() }).get(30, SECONDS)) + assertThat(first.changes).isEqualTo(reversed.changes) + assertThat(first.renames).isEqualTo(reversed.renames) + apply(first, requested) + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(read("App/util/Alpha.x")).contains("class Alpha", "util.Beta next() = new util.Beta()") + assertThat(read("App/util/Beta.x")).contains("class Beta", "util.Alpha next() = new util.Alpha()") + assertThat(read("Consumer.x")).contains("app.util.Alpha", "app.util.Beta").doesNotContain("app.left", "app.right") + } + } + + @Test + fun `one invalid type destination refuses the entire interacting batch`() { + write("App.x", "module App { left.First first() = new left.First(); right.Second second() = new right.Second(); }") + write("App/left/First.x", "class First {}") + write("App/right/Second.x", "class Second {}") + write("App/util.x", "package util { class Taken {} }") + write("App/util/Marker.x", "class Marker {}") + val original = sourceTexts() + session { adapter -> + val requested = mapOf(uri("App/left/First.x") to uri("App/util/Alpha.x"), uri("App/right/Second.x") to uri("App/util/Taken.x")) + assertThat(adapter.renameFilesAsync(requested).get(30, SECONDS)).isNull() + assertThat(sourceTexts()).isEqualTo(original) + assertThat(directory.resolve("App/util/Alpha.x")).doesNotExist() + } + } + + @Test + fun `empty nested destination acquires compiler package ownership without marker sources`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("Consumer.x", "module Consumer { package app import App; app.tools.Box make() = new app.tools.Box(); }") + Files.createDirectories(directory.resolve("App/util/nested")) + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("App/util/nested/Parcel.x"))) + assertThat(read("App.x")).contains("util.nested.Parcel") + assertThat(read("Consumer.x")).contains("app.util.nested.Parcel") + assertThat(read("App/util/nested/Parcel.x")).isEqualTo("class Parcel {}") + } + } + + @Test + fun `unused type moves into an explicit empty package`() { + write("App.x", "module App {}") + write("App/tools/Box.x", "class Box {}") + write("App/util.x", "package util {}") + Files.createDirectories(directory.resolve("App/util")) + session { adapter -> + move(adapter) + assertThat(read("App/util/Box.x")).isEqualTo("class Box {}") + } + } + + @Test + fun `unowned empty destinations or destinations below a class remain refused`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("App/Owner.x", "class Owner {}") + write("Other.x", "module Other {}") + listOf("App/Owner/nested", "Other/empty", "unowned").forEach { Files.createDirectories(directory.resolve(it)) } + session { adapter -> + val original = sourceTexts() + listOf("App/Owner/nested", "unowned").forEach { destination -> + assertThat(adapter.renameFilesAsync(mapOf(uri("App/tools/Box.x") to uri("$destination/Box.x"))).get(30, SECONDS)) + .describedAs(destination) + .isNull() + } + assertThat(sourceTexts()).isEqualTo(original) + } + } + + @Test + fun `qualified imports and calls retain comments whitespace and explicit aliases during combined moves`() { + write( + "App.x", + """ + module App { + import tools /* namespace */ . Box as Crate; + Crate make() = new Crate(); + tools /* type */ . Box direct() = new tools . /* constructor */ Box(); + } + """.trimIndent(), + ) + write("App/tools/Box.x", "class Box { static Int number() = 1; }") + write( + "Consumer.x", + """ + module Consumer { + package app import App; + app /* alias */ . tools /* package */ . Box make() = new app . tools . Box(); + Int read() = app.tools /* call */ . Box.number(); + } + """.trimIndent(), + ) + Files.createDirectories(directory.resolve("App/util/nested")) + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("App/util/nested/Parcel.x"))) + assertThat(read("App.x")).contains( + "import util /* namespace */ . nested.Parcel as Crate;", + "Crate make() = new Crate();", + "util /* type */ . nested.Parcel direct() = new util . /* constructor */ nested.Parcel();", + ) + assertThat(read("Consumer.x")).contains( + "app /* alias */ . util /* package */ . nested.Parcel make() = new app . util . nested.Parcel();", + "app.util /* call */ . nested.Parcel.number()", + ) + } + } + + @Test + fun `removing a namespace preserves line comments unicode and CRLF in nested types and calls`() { + val text = + """ + module App { + tools /* α */ . Box.Part make() = new tools // keep namespace note + . Box.Part(); + } + """.trimIndent().replace("\n", "\r\n") + write("App.x", text) + write("App/tools/Box.x", "class Box {}") + write("App/tools/Box/Part.x", "static class Part {}") + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("App/Box.x"))) + assertThat( + read("App.x"), + ).isEqualTo(text.replace("tools /* α */ .", " /* α */ ").replace("tools //", " //").replace(". Box.Part();", " Box.Part();")) + } + } + + @Test + fun `commented names do not weaken refusal when a move changes an unqualified call binding`() { + write("App.x", "module App { tools /* target */ . Box make() = new tools . Box(); }") + write("App/tools.x", "package tools { static Int number() = 1; }") + write("App/tools/Box.x", "class Box { Int read() = number(); }") + write("App/util.x", "package util { static Int number() = 2; }") + Files.createDirectories(directory.resolve("App/util")) + session { adapter -> + val original = sourceTexts() + assertThat(adapter.renameFilesAsync(request()).get(30, SECONDS)).isNull() + assertThat(sourceTexts()).isEqualTo(original) + } + } + + @Test + fun `cross module ownership updates closed consumers and proposes required dependencies`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box { static Int number() = 1; }") + write("App/tools/Box/Part.x", "static class Part {}") + write("App/tools/Box/data.txt", "companion bytes") + write("Other.x", "module Other {}") + Files.createDirectories(directory.resolve("Other/empty")) + write( + "Consumer.x", + """ + module Consumer { + package app import App; + import app.tools.Box as Crate; + Crate make() = new Crate(); + Int read() = app.tools.Box.number(); + } + """.trimIndent(), + ) + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri("App.x")), + XdkSourceModule("Other", uri("Other.x")), + XdkSourceModule("Consumer", uri("Consumer.x"), setOf("App")), + ), + ) + val requested = mapOf(uri("App/tools/Box.x") to uri("Other/empty/Parcel.x")) + assertThat(adapter.renameFilesAsync(requested).get(30, SECONDS)).isNull() + val proposal = requireNotNull(adapter.renameFilesProposalAsync(requested).get(30, SECONDS)) + val graph = requireNotNull(proposal.sourceModules) + assertThat(graph.single { it.name == "App" }.dependencies).contains("Other") + assertThat(graph.single { it.name == "Consumer" }.dependencies).contains("App", "Other") + assertThat(read("App.x")).doesNotContain("import Other") + apply(proposal.edit, requested) + adapter.replaceSourceModules(graph) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + assertThat(read("Consumer.x")).contains("import Other;", "other.empty.Parcel as Crate", "Crate make() = new Crate()") + assertThat(read("Other/empty/Parcel/data.txt")).isEqualTo("companion bytes") + } + } + + @Test + fun `discovered cross module move retains the moved type's source dependency`() { + write("App.x", "module App {}") + write("App/tools/Helper.x", "class Helper {}") + write("App/tools/Box.x", "class Box { Helper make() = new Helper(); }") + write("Other.x", "module Other {}") + Files.createDirectories(directory.resolve("Other")) + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("Other/Box.x"))) + assertThat(read("Other.x")).contains("import App;") + assertThat(read("Other/Box.x")).contains("app.tools.Helper") + } + } + + @Test + fun `a fresh module alias cannot capture an existing source name`() { + write("App.x", "module App { Int other = 7; tools.Box make() = new tools.Box(); }") + write("App/tools/Box.x", "class Box {}") + write("Other.x", "module Other {}") + Files.createDirectories(directory.resolve("Other")) + session { adapter -> + move(adapter, mapOf(uri("App/tools/Box.x") to uri("Other/Box.x"))) + assertThat(read("App.x")).contains("package other1 import Other;", "Int other = 7;", "other1.Box make()") + } + } + + @ParameterizedTest + @ValueSource(booleans = [true, false]) + fun `incoming resource expressions use the destination module's captured lookup policy`(sameBytes: Boolean) { + write("App.x", "module App {}") + write("App/Box.x", "class Box { static String text() = $/data.txt; }") + write("Other.x", "module Other {}") + write("original/data.txt", "expected bytes") + write("destination/data.txt", if (sameBytes) "expected bytes" else "changed bytes") + Files.createDirectories(directory.resolve("Other")) + session { adapter -> + adapter.replaceSourceModules( + listOf( + XdkSourceModule("App", uri("App.x"), resourceRoots = listOf(uri("original"))), + XdkSourceModule("Other", uri("Other.x"), resourceRoots = listOf(uri("destination"))), + ), + ) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val requested = mapOf(uri("App/Box.x") to uri("Other/Box.x")) + val proposal = adapter.renameFilesProposalAsync(requested).get(30, SECONDS) + if (sameBytes) { + apply(requireNotNull(proposal).edit, requested) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + } else { + assertThat(proposal).isNull() + assertThat(directory.resolve("App/Box.x")).exists() + } + } + } + + @Test + fun `cross module move refuses cyclic dependencies atomically`() { + write("App.x", "module App { tools.Box make() = new tools.Box(); }") + write("App/tools/Helper.x", "class Helper {}") + write("App/tools/Box.x", "class Box { Helper make() = new Helper(); }") + write("Other.x", "module Other {}") + Files.createDirectories(directory.resolve("Other")) + session { adapter -> + val original = sourceTexts() + val requested = mapOf(uri("App/tools/Box.x") to uri("Other/Box.x")) + assertThat(adapter.renameFilesProposalAsync(requested).get(30, SECONDS)).isNull() + assertThat(sourceTexts()).isEqualTo(original) + } + } + + private fun sourceTexts(): Map = + directory + .toFile() + .walkTopDown() + .filter { it.isFile } + .associate { it.relativeTo(directory.toFile()).path to it.readText() } + + private fun request() = mapOf(uri("App/tools/Box.x") to uri("App/util/Box.x")) + + private fun read(file: String) = Files.readString(directory.resolve(file)) + + private fun move( + adapter: XdkAdapter, + requested: Map = request(), + ): WorkspaceEdit { + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + val edit = requireNotNull(adapter.renameFilesAsync(requested).get(30, SECONDS)) + assertThat(edit.versioned).isTrue() + apply(edit, requested) + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.workspaceDiagnosticsAsync().get(30, SECONDS)).allMatch { it.success } + return edit + } + + private fun session(body: (XdkAdapter) -> Unit) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + body(adapter) + } + } + + private fun write( + file: String, + source: String, + ) { + directory.resolve(file).toFile().apply { + parentFile.mkdirs() + writeText(source) + } + } + + private fun uri(file: String) = + directory + .resolve(file) + .toFile() + .toURI() + .toString() + .removeSuffix("/") + + private fun apply( + edit: WorkspaceEdit, + requested: Map, + ) { + edit.changes.forEach { (uri, changes) -> + val path = Path.of(URI(uri)) + val text = Files.readString(path) + + fun offset(position: Position): Int = + if (position.line == 0) { + position.column + } else { + Regex("\\r\\n|\\r|\\n") + .findAll(text) + .elementAt(position.line - 1) + .range.last + 1 + position.column + } + Files.writeString( + path, + changes + .sortedByDescending { offset(it.range.start) } + .fold(text) { value, change -> + value.replaceRange( + offset(change.range.start), + offset(change.range.end), + change.newText, + ) + }, + ) + } + (edit.renames + requested).forEach { (from, to) -> + Files.move(Path.of(URI(from)), Path.of(URI(to))) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt new file mode 100644 index 00000000000..cd47d127c93 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt @@ -0,0 +1,150 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkUnionDelegationRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(ints = [0, 1, 3]) + fun `a union inside two delegate layers retains every contract and receiver`(entry: Int) { + val text = + """ + module App { + interface Api { Int read(); } + class First implements Api { @Override Int read() = 1; } + class Second implements Api { @Override Int read() = 2; } + class Forward(First | Second target) delegates Api(target) {} + class Outer(Forward target) delegates Api(target) {} + Int use(Outer value) = value.read(); + } + """.trimIndent() + renamed( + text, + Regex("read") + .findAll(text) + .elementAt(entry) + .range.first, + ) + } + + @Test + fun `generic union delegate branches preserve concrete substitutions`() { + val text = + """ + module App { + interface Api { T read(); } + class First(T item) implements Api { @Override T read() = item; } + class Second(T item) implements Api { @Override T read() = item; } + class Forward(First | Second target) delegates Api(target) {} + Int use(Forward value) = value.read(); + } + """.trimIndent() + renamed(text, text.lastIndexOf("read")) + } + + @Test + fun `an implicit call to a generated union delegate joins the written family`() { + val text = + """ + module App { + interface Api { Int read(); } + class First implements Api { @Override Int read() = 1; } + class Second implements Api { @Override Int read() = 2; } + class Forward(First | Second target) delegates Api(target) { + Int use() = read(); + } + } + """.trimIndent() + renamed(text, text.lastIndexOf("read")) + } + + @Test + fun `one recursive alternative does not hide the other branch`() { + val text = + """ + module App { + interface Api { Int read(); } + class Forward(Left | Right target) delegates Api(target) {} + class Left(Forward target) delegates Api(target) {} + class Right implements Api { @Override Int read() = 1; } + Int use(Forward value) = value.read(); + } + """.trimIndent() + renamed(text, text.lastIndexOf("read")) + } + + @Test + fun `a collision on one delegate branch refuses all edits`() { + val text = + """ + module App { + interface Api { Int read(); } + class First implements Api { + @Override Int read() = 1; + Int fetch() = 3; + } + class Second implements Api { @Override Int read() = 2; } + class Forward(First | Second target) delegates Api(target) {} + Int use(Forward value) = value.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + assertThat(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).isNull() + } + } + + private fun renamed( + text: String, + offset: Int, + ) { + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.renameAt(uri, text, offset, "fetch")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val reverse = requireNotNull(adapter.renameAt(uri, changed, changed.indexOf("fetch"), "read")) + assertThat(apply(changed, reverse.changes.getValue(uri))).isEqualTo(text) + } + } + + private fun workspace( + text: String, + check: (XdkAdapter, String) -> Unit, + ) { + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + val uri = source.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).describedAs(text).isEmpty() + check(adapter, uri) + assertThat(source.readText()).isEqualTo(text) + } + } + + private fun XdkAdapter.renameAt( + uri: String, + text: String, + offset: Int, + name: String, + ): WorkspaceEdit? { + val prefix = text.take(offset) + return rename(uri, prefix.count { it == '\n' }, prefix.substringAfterLast('\n').length, name) + } + + private fun apply( + text: String, + edits: List, + ): String { + fun offset(position: Position) = text.lineSequence().take(position.line).sumOf { it.length + 1 } + position.column + return edits.sortedByDescending { offset(it.range.start) }.fold(text) { value, edit -> + value.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt new file mode 100644 index 00000000000..d2eb592cc17 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt @@ -0,0 +1,323 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.MethodSource +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +class XdkUnionRenameTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(ints = [0, 1, 2]) + fun `either declaration or the union call renames every possible contract`(entry: Int) { + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + Int use(First | Second target) = target.read(); + class Other { Int read() = 3; } + } + """.trimIndent() + workspace(text) { adapter, uri -> + val offset = + Regex("read") + .findAll(text) + .elementAt(entry) + .range.first + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, offset, "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch").replace("fetch() = 3", "read() = 3")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val undo = requireNotNull(adapter.renameAt(uri, changed, changed.indexOf("fetch"), "read")) + assertThat(apply(changed, undo.changes.getValue(uri))).isEqualTo(text) + } + } + + @Test + fun `nested union legs and direct consumers retain the same targets`() { + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + class Third { Int read() = 3; } + Int use(First | Second | Third target) = target.read(); + Int direct(Second target) = target.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `a closed cross-module union consumer joins otherwise independent source families`() { + val first = + """ + module First { + class Box { Int read() = 1; } + } + """.trimIndent() + val second = + """ + module Second { + class Box { Int read() = 2; } + } + """.trimIndent() + val consumer = + """ + module Consumer { + package one import First; + package two import Second; + Int use(one.Box | two.Box target) = target.read(); + } + """.trimIndent() + val sources = mapOf("First" to first, "Second" to second, "Consumer" to consumer) + val uris = + sources.mapValues { (name, text) -> + directory + .resolve("$name.x") + .toFile() + .apply { writeText(text) } + .canonicalFile + .toURI() + .toString() + } + XdkAdapter().use { adapter -> + adapter.replaceSourceModules( + sources.keys.map { name -> + XdkSourceModule(name, uris.getValue(name), if (name == "Consumer") setOf("First", "Second") else emptySet()) + }, + ) + assertThat(adapter.compile(uris.getValue("First"), first).diagnostics).isEmpty() + val edit = requireNotNull(adapter.renameAt(uris.getValue("First"), first, first.indexOf("read"), "fetch")) + assertThat(edit.changes.keys).containsExactlyInAnyOrderElementsOf(uris.values) + sources.forEach { (name, text) -> + assertThat(apply(text, edit.changes.getValue(uris.getValue(name)))).isEqualTo(text.replace("read", "fetch")) + } + } + } + + @Test + fun `renaming a receiver class may reorder union legs without rebinding calls`() { + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + Int use(First | Second target) = target.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + val edit = requireNotNull(adapter.renameAt(uri, text, text.indexOf("First"), "Zed")) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("First", "Zed")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `collisions on one leg refuse the entire edit`() { + val text = + """ + module App { + class First { + Int read() = 1; + Int fetch() = 3; + } + class Second { Int read() = 2; } + Int use(First | Second target) = target.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + assertThat(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).isNull() + } + } + + @ParameterizedTest + @ValueSource(strings = ["Int", "String", "List", "Map", "List"]) + fun `generic union receivers preserve nested substitutions during rename`(argument: String) { + val text = + """ + module App { + class First(T value) { T read() = value; } + class Second(T value) { T read() = value; } + $argument use(First<$argument> | Second<$argument> target) = target.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val reverse = requireNotNull(adapter.renameAt(uri, changed, changed.indexOf("fetch"), "read")) + assertThat(apply(changed, reverse.changes.getValue(uri))).isEqualTo(text) + } + } + + @ParameterizedTest + @MethodSource("annotatedReceivers") + fun `annotated union receivers preserve annotation values and composed methods`(text: String) { + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `virtual child union receivers retain their enclosing source ownership`() { + val text = + """ + module App { + class Owner { + class First { Int read() = 1; } + class Second { Int read() = 2; } + Int use(First | Second target) = target.read(); + } + } + """.trimIndent() + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @ParameterizedTest + @MethodSource("annotationArguments") + fun `compound and numeric annotation arguments permit proven union rename`(argument: Pair) { + val (type, value) = argument + val text = + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + annotation Mark($type value) into Object {} + Int use(Boolean choose) { + (@Mark($value) First) | Second target = choose ? new @Mark($value) First() : new Second(); + return target.read(); + } + } + """.trimIndent() + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.lastIndexOf("read"), "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `renaming a substituted source type translates identities inside receiver type arguments`() { + val text = + """ + module App { + class Payload {} + class First(T value) { T read() = value; } + class Second(T value) { T read() = value; } + Payload use(First | Second target) = target.read(); + } + """.trimIndent() + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.indexOf("Payload"), "Content")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("Payload", "Content")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `source formal arguments retain their declaration identity`() { + val text = + """ + module App { + class First(T value) { T read() = value; } + class Second(T value) { T read() = value; } + class Consumer(First | Second target) { + T use() = target.read(); + } + } + """.trimIndent() + workspace(text) { adapter, uri -> + val changed = apply(text, requireNotNull(adapter.renameAt(uri, text, text.indexOf("read"), "fetch")).changes.getValue(uri)) + assertThat(changed).isEqualTo(text.replace("read", "fetch")) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + private fun workspace( + text: String, + check: (XdkAdapter, String) -> Unit, + ) { + val source = directory.resolve("App.x").toFile().apply { writeText(text) } + val uri = source.toURI().toString() + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).describedAs(text).isEmpty() + check(adapter, uri) + assertThat(source.readText()).isEqualTo(text) + } + } + + private fun XdkAdapter.renameAt( + uri: String, + text: String, + offset: Int, + name: String, + ): WorkspaceEdit? { + val prefix = text.take(offset) + return rename(uri, prefix.count { it == '\n' }, prefix.substringAfterLast('\n').length, name) + } + + private fun apply( + text: String, + edits: List, + ): String { + fun offset(position: Position) = text.lineSequence().take(position.line).sumOf { it.length + 1 } + position.column + return edits.sortedByDescending { offset(it.range.start) }.fold(text) { value, edit -> + value.replaceRange(offset(edit.range.start), offset(edit.range.end), edit.newText) + } + } + + private companion object { + @JvmStatic + fun annotationArguments() = + listOf( + "Byte" to "Byte:7", + "Float64" to "Float64:1.5", + "Dec64" to "Dec64:1.5", + "Int[]" to "Int[]:[1, 2]", + "Map" to "Map:[\"one\" = 1]", + "Range" to "Int:1.." to "Tuple:(Int:1, \"two\")", + ) + + @JvmStatic + fun annotatedReceivers() = + listOf( + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + annotation Mark(String label) into Object {} + Int use((@Mark("one") First) | (@Mark("two") Second) target) = target.read(); + } + """.trimIndent(), + """ + module App { + class First { Int read() = 1; } + class Second { Int read() = 2; } + annotation Loud into First { @Override Int read() = super() + 1; } + Int use((@Loud First) | Second target) = target.read(); + } + """.trimIndent(), + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt new file mode 100644 index 00000000000..5a0d254d997 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt @@ -0,0 +1,54 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkValueTemplateTest { + @Test + fun `lambda templates fit expected arity and compile in named ordinary and constructor arguments`() { + listOf( + "void take(function Int(Int) action) {} void run() { take(§); }" to "(arg1) -> TODO()", + "void take(function void() action) {} void run() { take(action = §); }" to "() -> TODO()", + "void take(function Int(Int, Int) action) {} void run() { take(§); }" to "(arg1, arg2) -> TODO()", + "class Box(function String(Int) action) {} void run() { Box box = new Box(§); }" to "(arg1) -> TODO()", + "void run(function void(function Int(Int)) callback) { callback(§); }" to "(arg1) -> TODO()", + ).forEach { (body, expected) -> + val marked = "module Editing { $body }" + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val cached = adapter.compile(URI, source) + val item = adapter.getCompletions(URI, 0, marked.indexOf('§')).single { it.label == expected } + assertThat(item.kind).isEqualTo(CompletionItem.CompletionKind.SNIPPET) + assertThat(item.snippet).contains("${'$'}{1:TODO()}${'$'}0") + assertThat(adapter.getCachedResult(URI)).isEqualTo(cached) + val edit = item.textEdit!! + assertThat( + adapter.compile(URI, source.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText)).diagnostics, + ).describedAs(marked) + .isEmpty() + } + } + } + + @Test + fun `template trials refuse nonfunction slots incompatible siblings and qualified prefixes`() { + listOf( + "void take(Int value) {} void run() { take(§); }", + "void take(function Int(Int) action, String value) {} void run() { take(§, 1); }", + "void take(function Int(Int) action) {} void run(String text) { take(text.§); }", + ).forEach { body -> + val marked = "module Editing { $body }" + XdkAdapter().use { adapter -> + adapter.compile(URI, marked.replace("§", "")) + assertThat(adapter.getCompletions(URI, 0, marked.indexOf('§'))) + .describedAs(marked) + .noneMatch { it.detail == "Lambda fitting this argument" } + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt new file mode 100644 index 00000000000..fead0dd3ff5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt @@ -0,0 +1,108 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.XdkWorkspaceDiscovery +import java.nio.file.Path +import java.util.concurrent.CancellationException + +class XdkWorkspaceDiscoveryTest { + @TempDir lateinit var directory: Path + + @Test + fun `discovery reads module imports ignores generated and bundled sources and honors buffers`() { + val base = source("Base.x", "module Base { class Box {} }") + val app = source("App.x", "module App { package lib import Base; }") + source("App/Member.x", "class Member {}") + source("build/Generated.x", "module Generated {}") + source("Xml.x", "module xml.xtclang.org {}") + source("Comment.x", "// module Fake {}\nclass Ordinary {}") + val modules = + XdkWorkspaceDiscovery.scan(listOf(directory.toFile()), emptyMap(), emptyList()) { + false + } + assertThat(modules.map { it.name }).containsExactly("App", "Base") + assertThat(modules.first().dependencies).containsExactly("Base") + val edited = + XdkWorkspaceDiscovery.scan( + listOf(directory.toFile()), + mapOf(app to "module App {}", base to "module Base { class Box {} }"), + modules, + ) { + false + } + assertThat(edited.first().dependencies).isEmpty() + assertThatThrownBy { + XdkWorkspaceDiscovery.scan(listOf(directory.toFile()), emptyMap(), modules) { true } + }.isInstanceOf(CancellationException::class.java) + } + + @Test + fun `unopened graph resolves imports and indexes independent healthy modules despite a broken neighbor`() { + val base = source("Base.x", "module Base { static Int answer() = 42; }") + val appText = "module App { package lib import Base; Int run() = lib.answer(); }" + val app = source("App.x", appText) + source("Broken.x", "module Broken { Missing value; }") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat( + adapter + .findWorkspaceSymbols("answer") + .single() + .location.uri, + ).isEqualTo(base) + assertThat(adapter.compile(app, appText).diagnostics).isEmpty() + assertThat(adapter.findDefinition(app, 0, appText.indexOf("answer"))!!.uri) + .isEqualTo(base) + val added = source("Added.x", "module Added { class AddedType {} }") + adapter.refreshDiscoveredSources() + assertThat( + adapter + .findWorkspaceSymbols("AddedType") + .single() + .location.uri, + ).isEqualTo(added) + directory.resolve("Added.x").toFile().delete() + adapter.refreshDiscoveredSources() + assertThat(adapter.findWorkspaceSymbols("AddedType")).isEmpty() + } + } + + @Test + fun `explicit graphs win and invalid replacements preserve automatic discovery`() { + val base = source("Base.x", "module Base { class Box {} }") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val module = XdkSourceModule("Base", base) + assertThatThrownBy { adapter.replaceSourceModules(listOf(module, module)) } + .isInstanceOf(IllegalArgumentException::class.java) + val other = source("Other.x", "module Other { class OtherBox {} }") + adapter.refreshDiscoveredSources() + assertThat( + adapter + .findWorkspaceSymbols("OtherBox") + .single() + .location.uri, + ).isEqualTo(other) + adapter.replaceSourceModules(emptyList()) + adapter.refreshDiscoveredSources() + assertThat(adapter.findWorkspaceSymbols("Box")).isEmpty() + adapter.discoverSourceModules() + assertThat(adapter.findWorkspaceSymbols("Box")).hasSize(2) + } + } + + private fun source( + name: String, + text: String, + ): String = + directory.resolve(name).toFile().let { + it.parentFile.mkdirs() + it.writeText(text) + it.canonicalFile.toURI().toString() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt new file mode 100644 index 00000000000..141469595f8 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt @@ -0,0 +1,98 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path + +class XdkWorkspaceNavigationTest { + @TempDir lateinit var directory: Path + + @Test + fun `unopened consumers contribute implementations subtype edges and selected incoming calls`() { + val libraryText = + "module Library { class Base { Int pick(Int value) = value; String pick(String value) = value; } }" + val library = source("Library", libraryText) + val consumerText = + "module Consumer { package lib import Library; class Child extends lib.Base { " + + "@Override Int pick(Int value) = value + 1; } Int run(lib.Base box) = box.pick(1); }" + val consumer = source("Consumer", consumerText) + source("Unrelated", "module Unrelated { class Base { Int pick(Int value) = value; } }") + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(library, libraryText).diagnostics).isEmpty() + val type = + adapter.prepareTypeHierarchy(library, 0, libraryText.indexOf("Base")).single() + val child = adapter.getSubtypes(type).single() + assertThat(child.uri).isEqualTo(consumer) + assertThat(child.name).isEqualTo("Child") + assertThat(adapter.getSupertypes(child).single().uri).isEqualTo(library) + assertThat( + adapter.findImplementation(library, 0, libraryText.indexOf("pick")).map { + it.uri + }, + ).contains(consumer) + .doesNotContain(directory.resolve("Unrelated.x").toUri().toString()) + val method = + adapter.prepareCallHierarchy(library, 0, libraryText.indexOf("pick")).single() + val incoming = adapter.getIncomingCalls(method).single() + assertThat(incoming.from.uri).isEqualTo(consumer) + assertThat(incoming.from.name).isEqualTo("run") + assertThat( + incoming.fromRanges + .single() + .start.column, + ).isEqualTo(consumerText.lastIndexOf("box.pick")) + assertThat( + adapter + .getOutgoingCalls(incoming.from) + .single() + .to.uri, + ).isEqualTo(library) + val other = + adapter.prepareCallHierarchy(library, 0, libraryText.lastIndexOf("pick")).single() + assertThat(adapter.getIncomingCalls(other)).isEmpty() + assertThat(adapter.findDefinition(consumer, 0, consumerText.lastIndexOf("pick"))!!.uri) + .isEqualTo(library) + assertThat(adapter.getCachedResult(consumer)).isNull() + } + } + + @Test + fun `hierarchy handles reject closed file edits without relying on watcher timing`() { + val libraryText = "module Library { class Base {} }" + val library = source("Library", libraryText) + source( + "Consumer", + "module Consumer { package lib import Library; class Child extends lib.Base {} }", + ) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + val alias = + directory + .toFile() + .canonicalFile + .toURI() + .toString() + "./Library.x" + assertThat(alias).isNotEqualTo(library) + val item = adapter.prepareTypeHierarchy(alias, 0, libraryText.indexOf("Base")).single() + assertThat(adapter.getSubtypes(item)).hasSize(1) + source("Consumer", "module Consumer { package lib import Library; class Child {} }") + assertThat(adapter.getSubtypes(item)).isEmpty() + val fresh = + adapter.prepareTypeHierarchy(library, 0, libraryText.indexOf("Base")).single() + assertThat(fresh.data).isNotEqualTo(item.data) + assertThat(adapter.getSubtypes(fresh)).isEmpty() + } + } + + private fun source( + name: String, + text: String, + ): String = + directory.resolve("$name.x").toFile().let { + it.writeText(text) + it.canonicalFile.toURI().toString() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt new file mode 100644 index 00000000000..a3b7344eb60 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt @@ -0,0 +1,349 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path + +class XdkWorkspaceRefactoringTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(strings = ["Box", "answer", "number"]) + fun `inline types static functions and constants rename through unopened consumers`(name: String) { + val libraryText = + "module Library { class Box {} static Int answer() = number; static Int number = 42; }" + val library = source("Library", libraryText) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; lib.Box make() = new lib.Box(); Int run() = lib.answer() + lib.number; }", + ) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(library, libraryText).diagnostics).isEmpty() + val position = libraryText.indexOf(name) + assertThat(adapter.prepareRename(library, 0, position)).isNotNull() + val edit = requireNotNull(adapter.rename(library, 0, position, "renamed")) + assertThat(edit.versioned).isTrue() + assertThat(edit.changes.keys).containsExactlyInAnyOrder(library, consumer) + assertThat( + edit.changes.values + .flatten() + .map { it.newText }, + ).containsOnly("renamed") + assertThat(directory.resolve("Library.x").toFile().readText()).isEqualTo(libraryText) + } + } + + @Test + fun `import cleanup preserves used aliases and comments and returns only compiled proofs`() { + val text = + "module App {\r\n" + + " import ecstasy.text.StringBuffer as Buffer;\r\n" + + " import ecstasy.maps.HashMap;\r\n" + + " // Keep this comment 😀\r\n" + + " Buffer make() = new Buffer();\r\n}" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val actions = + adapter.getCodeActions(uri, Range(Position(0, 0), Position(5, 0)), emptyList()) + assertThat(actions.map { it.title }) + .contains("Organize imports", "Remove unused import 'HashMap'") + .doesNotContain("Remove unused import 'Buffer'") + actions.forEach { action -> + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + val changed = + edit.changes + .getValue(uri) + .sortedWith( + compareByDescending { it.range.start.line } + .thenByDescending { it.range.start.column }, + ).fold(text) { value, replacement -> + fun offset(position: Position): Int = + value.splitToSequence("\n").take(position.line).sumOf { + it.length + 1 + } + position.column + value.replaceRange( + offset(replacement.range.start), + offset(replacement.range.end), + replacement.newText, + ) + } + assertThat(changed).contains("// Keep this comment 😀", "Buffer make()") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + } + + @Test + fun `binary types and broken graphs do not yield source refactoring edits`() { + val text = + "module App { package xml import xml.xtclang.org; void use(xml.Document doc) {} }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + assertThat(adapter.rename(uri, 0, text.indexOf("Document"), "Renamed")).isNull() + source("Broken", "module Broken { Missing value; }") + adapter.refreshDiscoveredSources() + assertThat( + adapter + .getCodeActions( + uri, + Range(Position(0, 0), Position(0, text.length)), + emptyList(), + ).map { it.title }, + ).containsExactly("Generate documentation comment") + } + } + + @Test + fun `property families rename declarations and uses without changing accessor names or unrelated properties`() { + val text = + "module Library { class Base { Int value { Int get() = 1; void set(Int value) {} } } " + + "class Child extends Base { @Override Int value { Int get() = 2; } } class Other { Int value = 3; } }" + val library = source("Library", text) + val consumer = + source( + "Consumer", + "module Consumer { package lib import Library; Int run(lib.Base base, lib.Child child) = base.value + child.value; }", + ) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(library, text).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(library, 0, text.indexOf("value"), "amount")) + assertThat(edit.changes.getValue(library)).hasSize(2) + assertThat(edit.changes.getValue(consumer)).hasSize(2) + assertThat(edit.renames).isEmpty() + val changed = apply(text, edit.changes.getValue(library)) + assertThat(changed) + .contains("Int get() = 1", "void set(Int value)", "Other { Int value = 3") + assertThat(adapter.compile(library, changed).diagnostics).isEmpty() + assertThat( + adapter + .compile( + consumer, + apply( + directory.resolve("Consumer.x").toFile().readText(), + edit.changes.getValue(consumer), + ), + ).diagnostics, + ).isEmpty() + } + } + + @Test + fun `property renames reject accidental capture of an existing derived property`() { + val text = + "module Library { class Base { Int value = 1; } class Child extends Base { Int amount = 2; } }" + val library = source("Library", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(library, text).diagnostics).isEmpty() + assertThat(adapter.rename(library, 0, text.indexOf("value"), "amount")).isNull() + } + } + + @Test + fun `member type rename proves new source membership and returns a nonoverwriting file move`() { + val text = "module App { Item make() = new Item(); }" + val uri = source("App", text) + val member = + directory.resolve("App/Item.x").toFile().also { + it.parentFile.mkdirs() + it.writeText("class Item {}") + } + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val edit = requireNotNull(adapter.rename(uri, 0, text.indexOf("Item"), "Renamed")) + assertThat(edit.renames) + .containsExactlyEntriesOf( + mapOf( + member.canonicalFile.toURI().toString() to + member.parentFile + .resolve("Renamed.x") + .canonicalFile + .toURI() + .toString(), + ), + ) + assertThat(edit.changes).hasSize(2) + assertThat(member.readText()).isEqualTo("class Item {}") + member.parentFile.resolve("Renamed.x").writeText("class Renamed {}") + assertThat(adapter.rename(uri, 0, text.indexOf("Item"), "Renamed")).isNull() + } + } + + @Test + fun `explicit import aliases rename locally while the imported binary identity stays unchanged`() { + val text = + "module App { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val at = text.indexOf("as Buffer") + 3 + assertThat(adapter.prepareRename(uri, 0, at)?.placeholder).isEqualTo("Buffer") + val edit = requireNotNull(adapter.rename(uri, 0, at, "Builder")) + assertThat(edit.changes.getValue(uri)).hasSize(3) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed) + .contains( + "import ecstasy.text.StringBuffer as Builder", + "Builder make() = new Builder()", + ) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `alias renames reject a new name that captures an existing binding`() { + val text = + "module App { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); String text = \"ok\"; }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + assertThat(adapter.rename(uri, 0, text.indexOf("as Buffer") + 3, "String")).isNull() + } + } + + @Test + fun `auto import repairs a missing bundled type without changing known bindings`() { + val text = "module App { String text = \"ok\"; Document echo(Document doc) = doc; }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + val at = text.indexOf("Document") + val actions = + adapter.getCodeActions(uri, Range(Position(0, at), Position(0, at)), emptyList()) + val action = actions.single { it.title == "Import 'Document' from xml.xtclang.org" } + val edit = requireNotNull(action.edit) + assertThat(edit.versioned).isTrue() + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed).contains("import xml.Document;", "String text = \"ok\"") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + @Test + fun `auto import proves a newly discovered source dependency and rejects inaccessible candidates`() { + source("Library", "module Library { class Widget {} private class Hidden {} }") + val text = "module App { Widget make() = new Widget(); }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + val at = text.indexOf("Widget") + val action = + adapter + .getCodeActions(uri, Range(Position(0, at), Position(0, at)), emptyList()) + .single { it.title == "Import 'Widget' from Library" } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed) + .contains("package library import Library;", "import library.Widget;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + val hidden = text.replace("Widget", "Hidden") + assertThat(adapter.compile(uri, hidden).diagnostics).isNotEmpty() + val actions = + adapter.getCodeActions( + uri, + Range(Position(0, 0), Position(0, hidden.length)), + emptyList(), + ) + assertThat(actions).noneMatch { it.title.startsWith("Import 'Hidden'") } + // Creating a new local type is valid; importing the other module's private type is not. + val create = actions.single { it.title == "Create class 'Hidden'" } + val local = apply(hidden, requireNotNull(create.edit).changes.getValue(uri)) + assertThat(adapter.compile(uri, local).diagnostics).isEmpty() + } + } + + @Test + fun `auto import withholds edits when a separate error prevents a complete proof`() { + val text = "module App { Document echo(Document doc) = doc; Missing broken; }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + assertThat( + adapter + .getCodeActions( + uri, + Range(Position(0, 0), Position(0, text.length)), + emptyList(), + ).map { it.title }, + ).containsExactly("Generate documentation comment") + } + } + + @Test + fun `auto import offers separate proven choices and respects explicit dependency configuration`() { + val first = source("First", "module First { class Widget {} }") + source("Second", "module Second { class Widget {} }") + val text = "module App { Widget make() = new Widget(); }" + val uri = source("App", text) + val range = Range(Position(0, 13), Position(0, 19)) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + assertThat(adapter.getCodeActions(uri, range, emptyList()).map { it.title }) + .containsExactly("Import 'Widget' from First", "Import 'Widget' from Second") + adapter.replaceSourceModules( + listOf(XdkSourceModule("App", uri), XdkSourceModule("First", first)), + ) + assertThat(adapter.compile(uri, text).diagnostics).isNotEmpty() + assertThat(adapter.getCodeActions(uri, range, emptyList())).isEmpty() + } + } + + @Test + fun `alias use rename preserves a nested alias with the same spelling`() { + val text = + "module App { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); " + + "class Nested { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); } }" + val uri = source("App", text) + XdkAdapter().use { adapter -> + adapter.initializeWorkspace(listOf(directory.toString())) + assertThat(adapter.compile(uri, text).diagnostics).isEmpty() + val edit = + requireNotNull(adapter.rename(uri, 0, text.indexOf("Buffer make"), "Builder")) + assertThat(edit.changes.getValue(uri)).hasSize(3) + val changed = apply(text, edit.changes.getValue(uri)) + assertThat(changed) + .endsWith( + "class Nested { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); } }", + ) + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + + private fun apply( + text: String, + edits: List, + ): String = + edits + .sortedByDescending { it.range.start.column } + .fold(text) { value, edit -> + value.replaceRange(edit.range.start.column, edit.range.end.column, edit.newText) + } + + private fun source( + name: String, + text: String, + ): String = + directory.resolve("$name.x").toFile().let { + it.writeText(text) + it.canonicalFile.toURI().toString() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt new file mode 100644 index 00000000000..2bf68dda42c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt @@ -0,0 +1,212 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.adapter.xdk.XdkAdapter + +class XdkWrittenFormalTest { + private val uri = "untitled:Formals.x" + + @Test + fun `a self reference in a written generic bound requires a real guarding class`() { + listOf( + "> void damaged(Element value) {}", + "class Damaged> {}", + " + Stringable> void damaged(Element value) {}", + ).forEach { header -> + val marked = "module Formals { interface Chain {} $header }" + val at = marked.indexOf('§') + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + val items = adapter.getCompletions(uri, 0, at) + assertThat(items.map { it.label }).describedAs(marked).contains("Element") + assertThat(items.single { it.label == "Element" }.detail).contains("incomplete", "written constraint") + assertThat(adapter.compile(uri, marked.replace("Ele§", "Element")).diagnostics).isEmpty() + } + } + listOf("Ele§", "Ele§ + Stringable", "Missing", "Chain + Missing").forEach { bound -> + val marked = "module Formals { interface Chain {} void damaged(Element value) {} }" + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + assertThat(adapter.getCompletions(uri, 0, marked.indexOf('§')).map { it.label }) + .describedAs(marked) + .doesNotContain("Element") + } + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + " void damaged(Ele§ value) {}", + "class Damaged(Ele§ value) {}", + " void damaged(Oth§ value) {}", + " void damaged(Str§ value) {}", + ], + ) + fun `written formals have their own bound and exact completion edit`(header: String) { + val marked = "module Formals { $header }" + val at = marked.indexOf('§') + val source = marked.replace("§", "") + val name = + when { + "Oth§" in marked -> "Other" + "Str§" in marked -> "String" + else -> "Element" + } + XdkAdapter().use { adapter -> + val baseline = adapter.compile(uri, source) + val item = adapter.getCompletions(uri, 0, at).single { it.label == name } + assertThat(item.detail) + .isEqualTo( + "type parameter $name extends ${if (name == "String") "Object" else "String"}", + ) + assertThat(item.textEdit) + .isEqualTo(TextEdit(Range(Position(0, at - 3), Position(0, at)), name)) + assertThat(adapter.getCachedResult(uri)).isEqualTo(baseline) + assertThat(adapter.compile(uri, source.replaceRange(at - 3, at, name)).diagnostics) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + " void damaged(Ele§ value) {}", + " void damaged(Ele§ value) {}", + " void damaged(Ele§ value) {}", + " void damaged(List value) {}", + ], + ) + fun `unresolved and cyclic bounds cannot borrow an outer type`(header: String) { + val marked = "module Formals { class Element {} $header }" + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + assertThat(adapter.getCompletions(uri, 0, marked.indexOf('§')).map { it.label }) + .doesNotContain("Element") + } + } + + @Test + fun `incomplete header hover describes a written formal as a bound`() { + val source = "module Formals { void damaged(Element value }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, source).success).isFalse() + val partial = + adapter.analyzeAtAsync(uri, Position(0, source.lastIndexOf("Element") + 3)).join() + assertThat(partial?.sites?.flatMap { it.formals }?.map { it.name }) + .describedAs("partial=%s", partial?.sites) + .contains("Element") + assertThat(adapter.getHoverInfo(uri, 0, source.lastIndexOf("Element") + 2)) + .contains("type parameter Element extends String") + } + } + + @Test + fun `recursive arguments in a concrete formal bound remain copyable`() { + val source = + "module Formals { interface Chain {} > void run(Element value) {} }" + XdkAdapter().use { adapter -> + assertThat(adapter.compile(uri, source).diagnostics).isEmpty() + assertThat(adapter.getHoverInfo(uri, 0, source.indexOf("value"))).contains("Element") + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "> void damaged(Ele§ value) {}", + "class Damaged>(Ele§ value) {}", + ", Other extends Element> void damaged(Ele§ value) {}", + "> void damaged(List value) {}", + ], + ) + fun `recursive written constraints preserve names without inventing types`(header: String) { + val marked = "module Formals { interface Chain {} $header }" + val at = marked.indexOf('§') + val source = marked.replace("§", "") + XdkAdapter().use { adapter -> + val baseline = adapter.compile(uri, source) + val item = adapter.getCompletions(uri, 0, at).single { it.label == "Element" } + assertThat(item.detail).contains("extends Chain<", "(written constraint)") + assertThat(item.textEdit) + .isEqualTo(TextEdit(Range(Position(0, at - 3), Position(0, at)), "Element")) + val formal = + adapter + .analyzeAtAsync(uri, Position(0, at)) + .join()!! + .sites + .single() + .formals + .single { it.name == "Element" } + assertThat(formal.constraint).isNull() + assertThat(formal.writtenConstraint).startsWith("Chain<") + assertThat(adapter.getCachedResult(uri)).isEqualTo(baseline) + assertThat(adapter.compile(uri, source.replaceRange(at - 3, at, "Element")).diagnostics) + .isEmpty() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "> void damaged(Ele§ value) {}", + ", Other extends Other> void damaged(Ele§ value) {}", + "> void damaged(Element.It§ value) {}", + ], + ) + fun `syntax-only recursion cannot authorize unknown bounds or member lookup`(header: String) { + val marked = "module Formals { interface Chain {} class Element {} $header }" + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + assertThat(adapter.getCompletions(uri, 0, marked.indexOf('§'))).isEmpty() + } + } + + @Test + fun `compound recursive written bounds preserve every operand and the recursion guard`() { + listOf("Chain + Stringable", "immutable Chain", "Chain?").forEach { bound -> + val marked = "module Formals { interface Chain {} void damaged(Ele§ value) {} }" + XdkAdapter().use { adapter -> + val source = marked.replace("§", "") + adapter.compile(uri, source) + val at = marked.indexOf('§') + val item = adapter.getCompletions(uri, 0, at).single { it.label == "Element" } + assertThat(item.detail).contains("written constraint") + assertThat(adapter.compile(uri, source.replaceRange(at - 3, at, "Element")).diagnostics).isEmpty() + } + } + listOf("Element?", "Element + Stringable", "Chain + Missing").forEach { bound -> + val marked = "module Formals { interface Chain {} void damaged(Ele§ value) {} }" + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + assertThat(adapter.getCompletions(uri, 0, marked.indexOf('§')).map { it.label }).doesNotContain("Element") + } + } + } + + @Test + fun `formal qualifier uses the resolved parameterized constraint`() { + val marked = + "module Formals { class Owner { class Item {} typedef T as Alias; static class ItemStatic {} } " + + "> void damaged(Element.It§ value) {} }" + XdkAdapter().use { adapter -> + adapter.compile(uri, marked.replace("§", "")) + val item = + adapter.getCompletions(uri, 0, marked.indexOf('§')).single { it.label == "Item" } + assertThat(item.detail).contains("String") + assertThat(adapter.compile(uri, marked.replace("It§", "Item")).diagnostics).isEmpty() + listOf("Ali§", "ItemSt§").forEach { leaf -> + val rejected = marked.replace("It§", leaf) + adapter.compile(uri, rejected.replace("§", "")) + assertThat(adapter.getCompletions(uri, 0, rejected.indexOf('§'))).isEmpty() + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexTest.kt index a33ade7ebbc..315eaf8b01c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexTest.kt @@ -13,8 +13,8 @@ import java.util.concurrent.Executors /** * Unit tests for [WorkspaceIndex]. * - * Tests add/remove/search/findByName operations and the 4-tier fuzzy matching algorithm. - * No native library needed -- pure Kotlin data structures. + * Tests add/remove/search/findByName operations and the 4-tier fuzzy matching algorithm. No native + * library needed -- pure Kotlin data structures. */ @DisplayName("WorkspaceIndex") class WorkspaceIndexTest { @@ -48,7 +48,10 @@ class WorkspaceIndexTest { @Test @DisplayName("should add symbols and update counts") fun shouldAddSymbols() { - index.addSymbols("file:///a.x", listOf(symbol("Foo", uri = "file:///a.x"), symbol("Bar", uri = "file:///a.x"))) + index.addSymbols( + "file:///a.x", + listOf(symbol("Foo", uri = "file:///a.x"), symbol("Bar", uri = "file:///a.x")), + ) assertThat(index.symbolCount).isEqualTo(2) assertThat(index.fileCount).isEqualTo(1) @@ -68,7 +71,10 @@ class WorkspaceIndexTest { @DisplayName("should replace symbols when re-indexing same URI") fun shouldReplaceOnReindex() { index.addSymbols("file:///a.x", listOf(symbol("Foo", uri = "file:///a.x"))) - index.addSymbols("file:///a.x", listOf(symbol("Bar", uri = "file:///a.x"), symbol("Baz", uri = "file:///a.x"))) + index.addSymbols( + "file:///a.x", + listOf(symbol("Bar", uri = "file:///a.x"), symbol("Baz", uri = "file:///a.x")), + ) assertThat(index.symbolCount).isEqualTo(2) assertThat(index.findByName("Foo")).isEmpty() @@ -161,7 +167,13 @@ class WorkspaceIndexTest { @Test @DisplayName("should match by prefix") fun shouldMatchPrefix() { - index.addSymbols("file:///a.x", listOf(symbol("HashMap", uri = "file:///a.x"), symbol("HashSet", uri = "file:///a.x"))) + index.addSymbols( + "file:///a.x", + listOf( + symbol("HashMap", uri = "file:///a.x"), + symbol("HashSet", uri = "file:///a.x"), + ), + ) val results = index.search("hash") assertThat(results).hasSize(2) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexerTest.kt index afd29793920..7f88143c890 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/index/WorkspaceIndexerTest.kt @@ -14,12 +14,15 @@ import org.xvm.lsp.model.SymbolInfo.SymbolKind import org.xvm.lsp.treesitter.XtcParser import java.nio.file.Files import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean /** * Integration tests for [WorkspaceIndexer]. * - * Requires the tree-sitter native library. Tests are skipped (not failed) - * when the native library is unavailable. + * Requires the tree-sitter native library. Tests are skipped (not failed) when the native library + * is unavailable. */ @DisplayName("WorkspaceIndexer") @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -41,6 +44,135 @@ class WorkspaceIndexerTest { parser?.close() } + @Test + fun `an open buffer wins over an in flight disk read and survives disk deletion`( + @TempDir directory: Path, + ) { + val path = directory.resolve("Owner.x") + // Java File URIs use file:/ while LSP/Path URIs use file:///; ownership is identical. + val uri = path.toFile().toURI().toString() + Files.writeString(path, "module Owner { class Disk {} }") + val read = CompletableFuture() + val release = CompletableFuture() + val index = WorkspaceIndex() + WorkspaceIndexer(index, requireNotNull(parser).getLanguage()) { file -> + (if (Files.isRegularFile(file)) Files.readString(file) else null).also { + read.complete(null) + release.get(5, SECONDS) + } + }.use { indexer -> + val scan = indexer.scanWorkspace(listOf(directory.toString())) + try { + read.get(5, SECONDS) + indexer.reindexFile(uri, "module Owner { class Buffer {} }") + } finally { + release.complete(null) + } + scan.get(5, SECONDS) + assertThat(index.findByName("Disk")).isEmpty() + assertThat(index.findByName("Buffer")).hasSize(1) + Files.delete(path) + indexer.refreshFile(uri) + assertThat(index.findByName("Buffer")).hasSize(1) + indexer.closeDocument(uri) + assertThat(index.findByName("Buffer")).isEmpty() + } + } + + @Test + fun `closing restores disk and reopening invalidates an older close read`( + @TempDir directory: Path, + ) { + val path = directory.resolve("Owner.x") + val uri = path.toUri().toString() + Files.writeString(path, "module Owner { class Disk {} }") + val block = AtomicBoolean(false) + val read = CompletableFuture() + val release = CompletableFuture() + val index = WorkspaceIndex() + WorkspaceIndexer(index, requireNotNull(parser).getLanguage()) { file -> + Files.readString(file).also { + if (block.get()) { + read.complete(null) + release.get(5, SECONDS) + } + } + }.use { indexer -> + indexer.reindexFile(uri, "module Owner { class FirstBuffer {} }") + indexer.closeDocument(uri) + assertThat(index.findByName("Disk")).hasSize(1) + assertThat(index.findByName("FirstBuffer")).isEmpty() + indexer.reindexFile(uri, "module Owner { class FirstBuffer {} }") + block.set(true) + val closing = CompletableFuture.runAsync { indexer.closeDocument(uri) } + try { + read.get(5, SECONDS) + indexer.reindexFile(uri, "module Owner { class Reopened {} }") + } finally { + release.complete(null) + } + closing.get(5, SECONDS) + assertThat(index.findByName("Disk")).isEmpty() + assertThat(index.findByName("Reopened")).hasSize(1) + } + } + + @Test + fun `buffers opened before the initial scan are indexed and closing observes disk deletion`( + @TempDir directory: Path, + ) { + val path = directory.resolve("Owner.x") + val uri = path.toUri().toString() + Files.writeString(path, "module Owner { class Disk {} }") + val index = WorkspaceIndex() + WorkspaceIndexer(index, requireNotNull(parser).getLanguage()).use { indexer -> + indexer.reindexFile(uri, "module Owner { class Buffer {} }") + indexer.scanWorkspace(listOf(directory.toString())).get(5, SECONDS) + assertThat(index.findByName("Buffer")).hasSize(1) + assertThat(index.findByName("Disk")).isEmpty() + indexer.closeDocument(uri) + Files.delete(path) + indexer.refreshFile(uri) + assertThat(index.symbolCount).isZero() + } + } + + @Test + fun `concurrent workspace scans do not starve their own bounded executor`( + @TempDir directory: Path, + ) { + Files.writeString(directory.resolve("Concurrent.x"), "module Concurrent { class Value {} }") + val index = WorkspaceIndex() + WorkspaceIndexer(index, requireNotNull(parser).getLanguage()).use { indexer -> + val scans = List(12) { indexer.scanWorkspace(listOf(directory.toString())) } + CompletableFuture.allOf(*scans.toTypedArray()).get(15, SECONDS) + assertThat(index.findByName("Value")).hasSize(1) + indexer.close() + indexer.reindexFile( + directory.resolve("Concurrent.x").toUri().toString(), + "module Replaced {}", + ) + assertThat(index.findByName("Value")).hasSize(1) + assertThat(index.findByName("Replaced")).isEmpty() + } + } + + @Test + fun `concurrent native parser requests retain independent source trees`() { + XtcParser(requireNotNull(parser).getLanguage()).use { shared -> + val requests = + List(40) { number -> + CompletableFuture.runAsync { + val source = "module Parallel$number { Int value = $number; }" + shared.parse(source).use { tree -> + assertThat(tree.root.text).isEqualTo(source) + } + } + } + CompletableFuture.allOf(*requests.toTypedArray()).get(15, SECONDS) + } + } + // ======================================================================== // Scan workspace // ======================================================================== @@ -93,11 +225,10 @@ class WorkspaceIndexerTest { } /** - * Issue #459: cmd-click on `JsonArray` (a typedef in json.x, used from - * JsonArrayBuilder.x) found nothing because typedef declarations never - * reached the workspace index. Shorthand constructor parameters - * (`const Point(Int x, Int y)`) declare properties and must be indexed - * too -- `structure.y` navigated to an unrelated file without this. + * Issue #459: cmd-click on `JsonArray` (a typedef in json.x, used from JsonArrayBuilder.x) + * found nothing because typedef declarations never reached the workspace index. Shorthand + * constructor parameters (`const Point(Int x, Int y)`) declare properties and must be + * indexed too -- `structure.y` navigated to an unrelated file without this. */ @Test @DisplayName("should index typedefs and shorthand constructor properties") @@ -119,13 +250,11 @@ class WorkspaceIndexerTest { indexer.scanWorkspace(listOf(tempDir.toString())).join() - assertThat(index.findByName("JsonArray")) - .isNotEmpty - .allMatch { it.kind == SymbolKind.CLASS } + assertThat(index.findByName("JsonArray")).isNotEmpty.allMatch { + it.kind == SymbolKind.CLASS + } assertThat(index.findByName("x")).isNotEmpty - assertThat(index.findByName("y")) - .isNotEmpty - .allMatch { it.kind == SymbolKind.PROPERTY } + assertThat(index.findByName("y")).isNotEmpty.allMatch { it.kind == SymbolKind.PROPERTY } indexer.close() } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/lexer/TemplateScannerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/lexer/TemplateScannerTest.kt index a1aa684c226..0ed76a7c16c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/lexer/TemplateScannerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/lexer/TemplateScannerTest.kt @@ -19,8 +19,9 @@ class TemplateScannerTest { val tokens = scanner.tokenize(source) assertThat(tokens) - .describedAs("Source: '$source' (length=${source.length}), chars: ${source.map { it.code }}") - .hasSize(2) + .describedAs( + "Source: '$source' (length=${source.length}), chars: ${source.map { it.code }}", + ).hasSize(2) assertThat(tokens[0].type).isEqualTo(TemplateTokenType.TEMPLATE_START) assertThat(tokens[1].type).isEqualTo(TemplateTokenType.TEMPLATE_END) } @@ -273,7 +274,8 @@ class TemplateScannerTest { @Test @DisplayName("should handle regular strings between templates") fun regularStringsBetween() { - val source = """val x = "not a template"; val y = ${'$'}"template {v}"; val z = "also not";""" + val source = + """val x = "not a template"; val y = ${'$'}"template {v}"; val z = "also not";""" val tokens = scanner.tokenize(source) // Should only find one template @@ -291,9 +293,7 @@ class TemplateScannerTest { val source = "\$|Hello World" val tokens = scanner.tokenize(source) - assertThat(tokens) - .describedAs("Tokens for multiline template: $tokens") - .hasSize(3) + assertThat(tokens).describedAs("Tokens for multiline template: $tokens").hasSize(3) assertThat(tokens[0].type).isEqualTo(TemplateTokenType.TEMPLATE_MULTILINE_START) assertThat(tokens[1].type).isEqualTo(TemplateTokenType.TEMPLATE_CONTENT) assertThat(tokens[1].value).isEqualTo("Hello World") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt new file mode 100644 index 00000000000..286391d2b30 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt @@ -0,0 +1,42 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatIllegalArgumentException +import org.junit.jupiter.api.Test + +class AdapterBackendTest { + @Test + fun `startup selection overrides the bundled default and rejects invalid overrides`() { + assertThat(AdapterBackend.resolve("compiler", "treesitter")).isEqualTo(AdapterBackend.TREE_SITTER) + assertThat(AdapterBackend.resolve("treesitter", "compiler")).isEqualTo(AdapterBackend.COMPILER) + assertThat(AdapterBackend.resolve("mock", null)).isEqualTo(AdapterBackend.MOCK) + assertThat(AdapterBackend.resolve(null, null)).isEqualTo(AdapterBackend.COMPILER) + assertThatIllegalArgumentException().isThrownBy { AdapterBackend.resolve("compiler", "typo") } + } + + @Test + fun `compiler is the default`() { + assertThat(AdapterBackend.fromSetting()).isEqualTo(AdapterBackend.COMPILER) + assertThat(AdapterBackend.fromSetting(null)).isEqualTo(AdapterBackend.COMPILER) + listOf("treesitter", "tree-sitter", "TreeSitter").forEach { + assertThat(AdapterBackend.fromSetting(it)).isEqualTo(AdapterBackend.TREE_SITTER) + } + } + + @Test + fun `explicit adapter names and aliases remain supported`() { + listOf("compiler", "xtc", "full").forEach { + assertThat(AdapterBackend.fromSetting(it)).isEqualTo(AdapterBackend.COMPILER) + } + assertThat(AdapterBackend.fromSetting("mock")).isEqualTo(AdapterBackend.MOCK) + } + + @Test + fun `unknown settings cannot silently select mock`() { + listOf("xdk", "compielr", "").forEach { + assertThatIllegalArgumentException() + .isThrownBy { AdapterBackend.fromSetting(it) } + .withMessageContaining("Unknown lsp.adapter") + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityContractTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityContractTest.kt new file mode 100644 index 00000000000..08f56f471c6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityContractTest.kt @@ -0,0 +1,100 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ServerCapabilities +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.AdapterCapability +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.adapter.xdk.XdkAdapter + +/** Every adapter flag has exactly one advertised provider; absent flags cannot leak a provider. */ +class CapabilityContractTest { + @ParameterizedTest + @EnumSource(AdapterCapability::class) + fun `a single adapter feature advertises only its provider`(feature: AdapterCapability) { + val adapter = + object : Adapter by MockAdapter() { + override val capabilities = setOf(feature) + } + XtcLanguageServer(adapter).use { server -> + val capabilities = server.initialize(editorInitializeParams()).join().capabilities + assertThat(providers(capabilities)).containsExactly(provider(feature)) + assertThat(capabilities.experimental).isNull() + } + } + + @Test + fun `an adapter without features advertises only synchronization and position encoding`() { + val adapter = + object : Adapter by MockAdapter() { + override val capabilities = emptySet() + } + XtcLanguageServer(adapter).use { server -> + val capabilities = server.initialize(editorInitializeParams()).join().capabilities + assertThat(Gson().toJsonTree(capabilities).asJsonObject.keySet()) + .containsExactlyInAnyOrder("textDocumentSync", "positionEncoding") + } + } + + @Test + fun `compiler capability inventory has no unsupported standard or experimental providers`() { + CompilerTestSupport.configure() + val adapter = XdkAdapter() + XtcLanguageServer(adapter).use { server -> + val capabilities = server.initialize(editorInitializeParams()).join().capabilities + // This client has not negotiated versioned edits or pull diagnostics. + assertThat(providers(capabilities)) + .containsExactlyInAnyOrderElementsOf( + (adapter.capabilities - AdapterCapability.RENAME).map(::provider), + ) + assertThat(capabilities.experimental).isEqualTo(mapOf("xtcRenameProposal" to 1, "xtcFileMoveProposal" to 1)) + assertThat(capabilities.workspace.workspaceFolders.supported).isTrue() + assertThat(capabilities.workspace.fileOperations).hasAllNullFieldsOrProperties() + assertThat(capabilities.notebookDocumentSync).isNull() + } + } + + private fun providers(capabilities: ServerCapabilities) = + Gson() + .toJsonTree(capabilities) + .asJsonObject + .keySet() + .filter { it.endsWith("Provider") } + + private fun provider(feature: AdapterCapability): String = + when (feature) { + AdapterCapability.HOVER -> "hoverProvider" + AdapterCapability.COMPLETION -> "completionProvider" + AdapterCapability.INLINE_COMPLETION -> "inlineCompletionProvider" + AdapterCapability.DEFINITION -> "definitionProvider" + AdapterCapability.DECLARATION -> "declarationProvider" + AdapterCapability.REFERENCES -> "referencesProvider" + AdapterCapability.DOCUMENT_SYMBOL -> "documentSymbolProvider" + AdapterCapability.DOCUMENT_HIGHLIGHT -> "documentHighlightProvider" + AdapterCapability.SELECTION_RANGE -> "selectionRangeProvider" + AdapterCapability.FOLDING_RANGE -> "foldingRangeProvider" + AdapterCapability.RENAME -> "renameProvider" + AdapterCapability.CODE_ACTION -> "codeActionProvider" + AdapterCapability.FORMATTING -> "documentFormattingProvider" + AdapterCapability.RANGE_FORMATTING -> "documentRangeFormattingProvider" + AdapterCapability.ON_TYPE_FORMATTING -> "documentOnTypeFormattingProvider" + AdapterCapability.DOCUMENT_LINK -> "documentLinkProvider" + AdapterCapability.DOCUMENT_COLOR -> "colorProvider" + AdapterCapability.SIGNATURE_HELP -> "signatureHelpProvider" + AdapterCapability.SEMANTIC_TOKENS -> "semanticTokensProvider" + AdapterCapability.WORKSPACE_SYMBOL -> "workspaceSymbolProvider" + AdapterCapability.CODE_LENS -> "codeLensProvider" + AdapterCapability.LINKED_EDITING -> "linkedEditingRangeProvider" + AdapterCapability.TYPE_HIERARCHY -> "typeHierarchyProvider" + AdapterCapability.TYPE_DEFINITION -> "typeDefinitionProvider" + AdapterCapability.IMPLEMENTATION -> "implementationProvider" + AdapterCapability.MONIKER -> "monikerProvider" + AdapterCapability.CALL_HIERARCHY -> "callHierarchyProvider" + AdapterCapability.INLAY_HINT -> "inlayHintProvider" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt new file mode 100644 index 00000000000..2e6e9537e42 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt @@ -0,0 +1,406 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ApplyWorkspaceEditResponse +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeActionContext +import org.eclipse.lsp4j.CodeActionParams +import org.eclipse.lsp4j.CompletionCapabilities +import org.eclipse.lsp4j.CompletionItemKind +import org.eclipse.lsp4j.CompletionItemKindCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentLinkCapabilities +import org.eclipse.lsp4j.DocumentLinkParams +import org.eclipse.lsp4j.ExecuteCommandParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SignatureHelpCapabilities +import org.eclipse.lsp4j.SignatureHelpParams +import org.eclipse.lsp4j.SignatureInformationCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceEditCapabilities +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.ParameterInfo +import org.xvm.lsp.adapter.SignatureInfo +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.Diagnostic +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import org.xvm.lsp.adapter.CodeAction as Action +import org.xvm.lsp.adapter.DocumentLink as AdapterLink +import org.xvm.lsp.adapter.Position as AdapterPosition +import org.xvm.lsp.adapter.Range as AdapterRange +import org.xvm.lsp.adapter.SignatureHelp as AdapterSignatureHelp +import org.xvm.lsp.adapter.TextEdit as AdapterEdit +import org.xvm.lsp.adapter.WorkspaceEdit as AdapterWorkspaceEdit + +class CapabilityNegotiationTest { + @Test + fun `link tooltips honor missing false and true support on eager and resolved results`() { + listOf(null, false, true).forEach { support -> + val adapter = + object : Adapter by MockAdapter() { + override fun getDocumentLinks( + uri: String, + content: String, + ) = listOf( + AdapterLink( + AdapterRange(AdapterPosition(0, 0), AdapterPosition(0, 6)), + URI, + "Open source", + ), + ) + } + XtcLanguageServer(adapter).use { server -> + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + if (support != null) { + documentLink = + DocumentLinkCapabilities().apply { + tooltipSupport = support + } + } + } + } + } + val options = + server + .initialize(params) + .join() + .capabilities.documentLinkProvider + assertThat(options.resolveProvider).isEqualTo(support != null) + open(server) + val documents = server.textDocumentService + val link = + documents + .documentLink(DocumentLinkParams(TextDocumentIdentifier(URI))) + .join() + .single() + if (support != null) { + assertThat(link.tooltip).isNull() + assertThat(link.target).isNull() + documents.documentLinkResolve(link).join() + } + assertThat(link.target).isEqualTo(URI) + assertThat(link.tooltip).isEqualTo("Open source".takeIf { support == true }) + } + } + } + + @Test + fun `signature metadata is negotiated while legacy active parameter follows selected overload`() { + listOf(null, false, true).forEach { support -> + val adapter = + object : Adapter by MockAdapter() { + override fun getSignatureHelpAsync( + uri: String, + line: Int, + column: Int, + ) = CompletableFuture.completedFuture( + AdapterSignatureHelp( + listOf( + SignatureInfo( + "read(Int first)", + parameters = listOf(ParameterInfo("Int first")), + activeParameter = 0, + ), + SignatureInfo( + "read(Int first, Int second)", + parameters = + listOf( + ParameterInfo("Int first"), + ParameterInfo("Int second"), + ), + activeParameter = 1, + ), + ), + activeSignature = 1, + activeParameter = 0, + ), + ) + } + XtcLanguageServer(adapter).use { server -> + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + if (support != null) { + signatureHelp = + SignatureHelpCapabilities().apply { + signatureInformation = + SignatureInformationCapabilities().apply { + activeParameterSupport = support + } + } + } + } + } + } + server.initialize(params).join() + open(server) + val help = + server.textDocumentService + .signatureHelp( + SignatureHelpParams(TextDocumentIdentifier(URI), Position(0, 0)), + ).join()!! + assertThat(help.activeSignature).isEqualTo(1) + assertThat(help.activeParameter).isEqualTo(1) + assertThat(help.signatures.map { it.activeParameter }) + .containsExactlyElementsOf( + if (support == true) listOf(0, 1) else listOf(null, null), + ) + assertThat( + help.signatures + .last() + .parameters + .map { it.label.left }, + ).containsExactly("Int first", "Int second") + } + } + } + + @Test + fun `completion kinds fall back while action kinds preserve hierarchical and empty filtering`() { + val params = editorInitializeParams() + params.capabilities.textDocument.completion = + CompletionCapabilities().apply { + completionItemKind = + CompletionItemKindCapabilities(listOf(CompletionItemKind.Variable)) + } + val presentation = ClientPresentation.read(params) + assertThat(presentation.completionKind(CompletionItemKind.Method)) + .isEqualTo(CompletionItemKind.Variable) + assertThat(ClientPresentation.matchesActionKind("source.organizeImports", listOf("source"))) + .isTrue() + assertThat(ClientPresentation.matchesActionKind("source.organizeImports", listOf(""))) + .isTrue() + assertThat(ClientPresentation.matchesActionKind("source.organizeImports", emptyList())) + .isFalse() + assertThat( + ClientPresentation.matchesActionKind("source.organizeImports", listOf("quickfix")), + ).isFalse() + } + + @Test + fun `literal actions omit unnegotiated preferred metadata and keep supported edits eager`() { + XtcLanguageServer(adapter()).use { server -> + val params = + editorInitializeParams().apply { + capabilities.textDocument.codeAction.isPreferredSupport = false + capabilities.workspace.workspaceEdit = + WorkspaceEditCapabilities().apply { documentChanges = true } + } + server.initialize(params).join() + open(server) + val action = + server.textDocumentService + .codeAction(actions()) + .join() + .single() + .right + assertThat(action.isPreferred).isNull() + assertThat(action.data).isNull() + assertThat( + action.edit.documentChanges + .single() + .left.textDocument.version, + ).isEqualTo(1) + assertThat(action.kind).isEqualTo("quickfix") + } + } + + @Test + fun `legacy commands own one bounded handle and applying edits tolerates its change notification`() { + XtcLanguageServer(adapter()).use { server -> + val client = mock(LanguageClient::class.java) + server.connect(client) + val capabilities = server.initialize(legacy()).join().capabilities + assertThat(capabilities.executeCommandProvider.commands) + .containsExactly(ClientPresentation.APPLY_CODE_ACTION) + open(server) + val action = + server.textDocumentService + .codeAction(actions()) + .join() + .single() + .left + doAnswer { + change(server) + CompletableFuture.completedFuture(ApplyWorkspaceEditResponse(true)) + }.`when`(client) + .applyEdit(any()) + val command = ExecuteCommandParams(action.command, action.arguments) + assertThat(server.workspaceService.executeCommand(command).join()) + .isInstanceOf(ApplyWorkspaceEditResponse::class.java) + assertThatThrownBy { server.workspaceService.executeCommand(command).join() } + .hasMessageContaining("expired or changed") + verify(client, times(1)).applyEdit(any()) + } + } + + @Test + fun `stale commands and clients without either action form cannot apply edits`() { + XtcLanguageServer(adapter()).use { server -> + val client = mock(LanguageClient::class.java) + server.connect(client) + server.initialize(legacy()).join() + open(server) + val action = + server.textDocumentService + .codeAction(actions()) + .join() + .single() + .left + change(server) + assertThatThrownBy { + server.workspaceService + .executeCommand(ExecuteCommandParams(action.command, action.arguments)) + .join() + }.hasMessageContaining("expired or changed") + verify(client, times(0)).applyEdit(any()) + } + XtcLanguageServer(adapter()).use { server -> + assertThat( + server + .initialize(InitializeParams()) + .join() + .capabilities + .codeActionProvider + .left, + ).isFalse() + open(server) + assertThat(server.textDocumentService.codeAction(actions()).join()).isEmpty() + } + } + + @Test + fun `client refusal is reported and disconnect retires an unanswered edit`() { + listOf(false, true).forEach { stalled -> + XtcLanguageServer(adapter()).use { server -> + val client = mock(LanguageClient::class.java) + val sent = CompletableFuture() + doAnswer { + sent.complete(null) + if (stalled) { + CompletableFuture() + } else { + CompletableFuture.completedFuture( + ApplyWorkspaceEditResponse(false).apply { + failureReason = "Read-only document" + }, + ) + } + }.`when`(client) + .applyEdit(any()) + server.connect(client) + server.initialize(legacy()).join() + open(server) + val command = + server.textDocumentService + .codeAction(actions()) + .join() + .single() + .left + val applying = + server.workspaceService.executeCommand( + ExecuteCommandParams(command.command, command.arguments), + ) + sent.get(5, SECONDS) + if (stalled) server.close() + assertThatThrownBy { applying.get(5, SECONDS) } + .hasMessageContaining(if (stalled) "changed" else "Read-only document") + } + } + } + + private fun legacy() = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + workspace = + WorkspaceClientCapabilities().apply { + applyEdit = true + workspaceEdit = + WorkspaceEditCapabilities().apply { documentChanges = true } + } + } + } + + private fun open(server: XtcLanguageServer) = + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, "module Action {}")), + ) + + private fun change(server: XtcLanguageServer) = + server.textDocumentService.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, 2), + listOf(TextDocumentContentChangeEvent("// fixed\nmodule Action {}")), + ), + ) + + private fun actions() = + CodeActionParams( + TextDocumentIdentifier(URI), + Range(Position(0, 0), Position(0, 0)), + CodeActionContext(emptyList()).apply { only = listOf("") }, + ) + + private fun adapter(): Adapter = + object : Adapter by MockAdapter() { + override fun getCodeActionsAsync( + uri: String, + range: AdapterRange, + diagnostics: List, + ) = CompletableFuture.completedFuture( + listOf( + Action( + "Fix", + Action.CodeActionKind.QUICKFIX, + edit = + AdapterWorkspaceEdit( + mapOf( + uri to + listOf( + AdapterEdit( + AdapterRange( + AdapterPosition(0, 0), + AdapterPosition(0, 0), + ), + "// fixed\n", + ), + ), + ), + versioned = true, + ), + isPreferred = true, + ), + ), + ) + } + + private companion object { + const val URI = "file:///Action.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt new file mode 100644 index 00000000000..7f6adc9711a --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt @@ -0,0 +1,130 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.LogTraceParams +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.mockito.Answers +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.mockito.Mockito.verifyNoInteractions +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.CompletableFuture +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +internal class ClientNotificationsTest { + @ParameterizedTest + @EnumSource(ClientRefresh.Feature::class) + fun `each negotiated refresh recovers after refusal and retires pending replies on close`(feature: ClientRefresh.Feature) { + val methods = + mapOf( + ClientRefresh.Feature.DIAGNOSTICS to "refreshDiagnostics", + ClientRefresh.Feature.TOKENS to "refreshSemanticTokens", + ClientRefresh.Feature.INLAYS to "refreshInlayHints", + ClientRefresh.Feature.LENSES to "refreshCodeLenses", + ClientRefresh.Feature.FOLDING to "refreshFoldingRanges", + ) + val calls = LinkedBlockingQueue>>() + val client = + mock(LanguageClient::class.java) { call -> + if (call.method.name in methods.values) { + CompletableFuture().also { calls.add(call.method.name to it) } + } else { + Answers.RETURNS_DEFAULTS.answer(call) + } + } + ClientRefresh { client }.use { refresh -> + refresh.configure(setOf(feature)) + refresh.request(*ClientRefresh.Feature.entries.toTypedArray()) + assertThat(calls).isEmpty() + refresh.initialized() + refresh.request(*ClientRefresh.Feature.entries.toTypedArray()) + val first = requireNotNull(calls.poll(5, SECONDS)) + assertThat(first.first).isEqualTo(methods.getValue(feature)) + repeat(20) { refresh.request(*ClientRefresh.Feature.entries.toTypedArray()) } + assertThat(calls).isEmpty() + first.second.completeExceptionally(IllegalStateException("Client refused refresh")) + val retry = requireNotNull(calls.poll(5, SECONDS)) + assertThat(retry.first).isEqualTo(first.first) + refresh.request(feature) + refresh.close() + retry.second.complete(null) + refresh.request(*ClientRefresh.Feature.entries.toTypedArray()) + assertThat(calls).isEmpty() + } + } + + @Test + fun `refresh waits for initialized honors negotiation and coalesces outstanding work`() { + val client = mock(LanguageClient::class.java) + val calls = LinkedBlockingQueue>() + doAnswer { CompletableFuture().also { calls.add(it) } } + .`when`(client) + .refreshCodeLenses() + ClientRefresh { client } + .use { refresh -> + refresh.configure(setOf(ClientRefresh.Feature.LENSES)) + refresh.request(ClientRefresh.Feature.LENSES) + verifyNoInteractions(client) + refresh.initialized() + refresh.request(ClientRefresh.Feature.INLAYS, ClientRefresh.Feature.FOLDING) + verifyNoInteractions(client) + refresh.request(ClientRefresh.Feature.LENSES) + val first = requireNotNull(calls.poll(5, SECONDS)) + repeat(50) { refresh.request(ClientRefresh.Feature.LENSES) } + assertThat(calls).isEmpty() + first.complete(null) + val second = requireNotNull(calls.poll(5, SECONDS)) + refresh.request(ClientRefresh.Feature.LENSES) + refresh.close() + second.complete(null) + refresh.request(ClientRefresh.Feature.LENSES) + assertThat(calls).isEmpty() + } + } + + @Test + fun `runtime trace switches between off messages and verbose without request payloads`() { + val client = mock(LanguageClient::class.java) + val messages = LinkedBlockingQueue() + doAnswer { call -> + messages.add(call.getArgument(0)) + null + }.`when`(client) + .logTrace(any()) + ClientTrace { client } + .use { trace -> + val span = + ExecutionTrace.span( + "lsp-request", + "textDocument/hover", + "file:///private/source.x", + ) + trace.completed(span, null) + verifyNoInteractions(client) + trace.configure("messages") + trace.completed(span, null) + val message = requireNotNull(messages.poll(5, SECONDS)) + assertThat(message.message) + .contains("textDocument/hover", "replied") + .doesNotContain("private") + assertThat(message.verbose).isNull() + trace.configure("verbose") + trace.completed(span, ResponseErrorCode.ContentModified.value) + val verbose = requireNotNull(messages.poll(5, SECONDS)) + assertThat(verbose.verbose).contains("request=${span.id}").doesNotContain("private") + trace.configure("off") + trace.completed(span, null) + assertThat(messages).isEmpty() + trace.close() + trace.configure("verbose") + trace.completed(span, null) + assertThat(messages).isEmpty() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt new file mode 100644 index 00000000000..93c4e26c6df --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt @@ -0,0 +1,165 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeActionCapabilities +import org.eclipse.lsp4j.CodeActionKindCapabilities +import org.eclipse.lsp4j.CodeActionLiteralSupportCapabilities +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentSymbolCapabilities +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.HoverCapabilities +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.InlineCompletionCapabilities +import org.eclipse.lsp4j.MarkupKind +import org.eclipse.lsp4j.PublishDiagnosticsCapabilities +import org.eclipse.lsp4j.SignatureHelpCapabilities +import org.eclipse.lsp4j.SignatureInformationCapabilities +import org.eclipse.lsp4j.SymbolKind +import org.eclipse.lsp4j.SymbolKindCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.mockito.Mockito.verifyNoInteractions +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Location +import org.xvm.lsp.model.SymbolInfo + +class ClientPresentationTest { + @Test + fun `minimal client receives no unnegotiated configuration or refresh requests`() { + XtcLanguageServer(MockAdapter()).use { server -> + val client = mock(LanguageClient::class.java) + server.connect(client) + server.initialize(InitializeParams()).join() + server.initialized(InitializedParams()) + server.refreshPresentation() + verifyNoInteractions(client) + } + } + + @Test + fun `server advertises UTF16 and serves flat symbols without hierarchical support`() { + val uri = "file:///Presentation.x" + val child = SymbolInfo.of("Child", SymbolInfo.SymbolKind.CLASS, Location(uri, 1, 4, 1, 18)) + val module = + SymbolInfo + .of("Presentation", SymbolInfo.SymbolKind.MODULE, Location(uri, 0, 0, 2, 1)) + .withChildren(listOf(child)) + val adapter = + object : Adapter by MockAdapter() { + override fun getCachedResult(uri: String) = CompilationResult.success(uri, listOf(module)) + } + XtcLanguageServer(adapter).use { server -> + assertThat( + server + .initialize(InitializeParams()) + .join() + .capabilities.positionEncoding, + ).isEqualTo("utf-16") + server.textDocumentService.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem(uri, "xtc", 1, "module Presentation {\n class Child {}\n}"), + ), + ) + val symbols = + server.textDocumentService + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .join() + assertThat(symbols).isNotEmpty().allSatisfy { assertThat(it.isLeft).isTrue() } + assertThat(symbols.map { it.left.name }).contains("Presentation", "Child") + assertThat(symbols.single { it.left.name == "Child" }.left.containerName) + .isEqualTo("Presentation") + } + } + + @Test + fun `minimal clients get plain hover legacy symbol kinds and no optional diagnostics`() { + val policy = ClientPresentation.read(InitializeParams()) + val hover = policy.hover("### Value\n```xtc\nInt value\n```\nUse `value`.") + assertThat(hover.kind).isEqualTo(MarkupKind.PLAINTEXT) + assertThat(hover.value).isEqualTo("Value\nInt value\nUse value.") + assertThat(policy.hierarchicalSymbols).isFalse() + assertThat(policy.symbolKind(SymbolKind.TypeParameter)).isEqualTo(SymbolKind.Variable) + assertThat(policy.diagnosticVersions).isFalse() + assertThat(policy.diagnosticRelatedInformation).isFalse() + } + + @Test + fun `client preference order and advertised kind set govern presentation`() { + val params = + editorInitializeParams().apply { + capabilities.textDocument.hover = + HoverCapabilities().apply { + contentFormat = listOf(MarkupKind.PLAINTEXT, MarkupKind.MARKDOWN) + } + capabilities.textDocument.documentSymbol.symbolKind = + SymbolKindCapabilities(listOf(SymbolKind.Class)) + } + val policy = ClientPresentation.read(params) + assertThat(policy.markdownHover).isFalse() + assertThat(policy.hierarchicalSymbols).isTrue() + assertThat(policy.symbolKind(SymbolKind.TypeParameter)).isEqualTo(SymbolKind.Class) + assertThat(policy.diagnosticVersions).isTrue() + params.capabilities.textDocument.hover.contentFormat = listOf(MarkupKind.MARKDOWN) + assertThat(ClientPresentation.read(params).hover("`Int`").value).isEqualTo("`Int`") + } + + @Test + fun `workspace folder list overrides legacy roots including explicitly empty folders`() { + // Older LSP clients may still send these protocol-deprecated fields. + val params = InitializeParams().apply { rootPath = "/tmp/old" } + assertThat(ClientPresentation.workspaceUris(params)).containsExactly("file:///tmp/old") + params.rootUri = "file:///tmp/new" + assertThat(ClientPresentation.workspaceUris(params)).containsExactly("file:///tmp/new") + params.workspaceFolders = listOf(WorkspaceFolder("file:///tmp/folder", "folder")) + assertThat(ClientPresentation.workspaceUris(params)).containsExactly("file:///tmp/folder") + params.workspaceFolders = emptyList() + assertThat(ClientPresentation.workspaceUris(params)).isEmpty() + } +} + +/** Real editor capabilities for tests that assert rich outlines and versioned publications. */ +internal fun editorInitializeParams() = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + workspace = WorkspaceClientCapabilities().apply { configuration = true } + textDocument = + TextDocumentClientCapabilities().apply { + inlineCompletion = InlineCompletionCapabilities() + signatureHelp = + SignatureHelpCapabilities().apply { + signatureInformation = + SignatureInformationCapabilities().apply { + activeParameterSupport = true + } + } + codeAction = + CodeActionCapabilities().apply { + codeActionLiteralSupport = + CodeActionLiteralSupportCapabilities( + CodeActionKindCapabilities(listOf("")), + ) + isPreferredSupport = true + } + documentSymbol = + DocumentSymbolCapabilities().apply { + hierarchicalDocumentSymbolSupport = true + } + publishDiagnostics = + PublishDiagnosticsCapabilities().apply { + versionSupport = true + relatedInformation = true + } + } + } + } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt new file mode 100644 index 00000000000..4e166389d1e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt @@ -0,0 +1,18 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class CodeLensSettingsTest { + @Test + fun `references default on and accept only an explicit boolean override`() { + listOf(null, emptyMap(), mapOf("references" to true)).forEach { + assertThat(CodeLensSettings.references(it)).isTrue() + } + assertThat(CodeLensSettings.references(mapOf("references" to false))).isFalse() + listOf(false, "false", mapOf("references" to "false"), mapOf("references" to null)).forEach { + assertThatThrownBy { CodeLensSettings.references(it) }.isInstanceOf(IllegalArgumentException::class.java) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt new file mode 100644 index 00000000000..d1104a45795 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt @@ -0,0 +1,138 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.Color +import org.eclipse.lsp4j.ColorPresentationParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentColorParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.TimeUnit.SECONDS + +class ColorValueProtocolTest { + @Test + fun `color provider requires opt in and disabled requests report method not found`() { + CompilerTestSupport.configure() + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + assertThat( + server + .initialize(InitializeParams()) + .get(10, SECONDS) + .capabilities.colorProvider, + ).isNull() + assertThatThrownBy { server.textDocumentService.documentColor(DocumentColorParams(DOCUMENT)).get(10, SECONDS) } + .hasCauseInstanceOf(ResponseErrorException::class.java) + .satisfies({ failure -> + assertThat( + (failure.cause as ResponseErrorException).responseError.code, + ).isEqualTo(ResponseErrorCode.MethodNotFound.value) + }) + } + } + + @Test + fun `picker range refers to the current document and replacement compiles to the requested color`() { + withServer { server -> + val service = server.textDocumentService + val before = service.documentColor(DocumentColorParams(DOCUMENT)).get(30, SECONDS).single() + assertThat(before.color).isEqualTo(Color(1.0, 128 / 255.0, 0.0, 1.0)) + val chosen = Color(0.0, 1.0, 0.0, 64 / 255.0) + val presentation = service.colorPresentation(ColorPresentationParams(DOCUMENT, chosen, before.range)).get(10, SECONDS).single() + assertThat(presentation.additionalTextEdits).isNullOrEmpty() + assertThat(presentation.textEdit.range).isEqualTo(before.range) + assertThat(presentation.textEdit.newText).isEqualTo("new Rgba(0, 255, 0, alpha = 64)") + val changed = SOURCE.replace("new Rgba(255, 128, 0)", presentation.textEdit.newText) + service.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(DOCUMENT.uri, 2), + listOf(TextDocumentContentChangeEvent(changed)), + ), + ) + val after = service.documentColor(DocumentColorParams(DOCUMENT)).get(30, SECONDS).single() + assertThat(after.color).isEqualTo(chosen) + // The old range no longer covers the whole constructor. + assertThat(service.colorPresentation(ColorPresentationParams(DOCUMENT, chosen, before.range)).get(10, SECONDS)).isEmpty() + service.didClose(DidCloseTextDocumentParams(DOCUMENT)) + assertThat(service.documentColor(DocumentColorParams(DOCUMENT)).get(10, SECONDS)).isEmpty() + assertThat(service.colorPresentation(ColorPresentationParams(DOCUMENT, chosen, after.range)).get(10, SECONDS)).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(doubles = [-0.1, 1.1, Double.NaN, Double.POSITIVE_INFINITY]) + fun `invalid picker channels return invalid params without disturbing the document`(red: Double) { + withServer { server -> + val service = server.textDocumentService + val before = service.documentColor(DocumentColorParams(DOCUMENT)).get(30, SECONDS).single() + assertThatThrownBy { + service.colorPresentation(ColorPresentationParams(DOCUMENT, Color(red, 0.0, 0.0, 1.0), before.range)).get(10, SECONDS) + }.hasCauseInstanceOf(ResponseErrorException::class.java) + .satisfies({ failure -> + assertThat( + (failure.cause as ResponseErrorException).responseError.code, + ).isEqualTo(ResponseErrorCode.InvalidParams.value) + }) + assertThat(service.documentColor(DocumentColorParams(DOCUMENT)).get(10, SECONDS)).containsExactly(before) + } + } + + @Test + fun `reversed presentation ranges return invalid params`() { + withServer { server -> + val request = ColorPresentationParams(DOCUMENT, Color(0.0, 0.0, 0.0, 1.0), Range(Position(3, 2), Position(2, 2))) + assertThatThrownBy { server.textDocumentService.colorPresentation(request).get(30, SECONDS) } + .hasCauseInstanceOf(ResponseErrorException::class.java) + .satisfies({ failure -> + assertThat( + (failure.cause as ResponseErrorException).responseError.code, + ).isEqualTo(ResponseErrorCode.InvalidParams.value) + }) + } + } + + private fun withServer(test: (XtcLanguageServer) -> Unit) { + CompilerTestSupport.configure() + XtcLanguageServer(XdkAdapter(colorPrototype = true)).use { server -> + server.connect(mock(LanguageClient::class.java)) + assertThat( + server + .initialize(InitializeParams()) + .get(10, SECONDS) + .capabilities.colorProvider.left, + ).isTrue() + server.initialized(InitializedParams()) + server.textDocumentService.didOpen(DidOpenTextDocumentParams(TextDocumentItem(DOCUMENT.uri, "xtc", 1, SOURCE))) + test(server) + } + } + + private companion object { + val DOCUMENT = TextDocumentIdentifier("file:///ColorPrototype.x") + val SOURCE = + """ + module ColorPrototype { + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + Rgba sample() = new Rgba(255, 128, 0); + } + """.trimIndent() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt new file mode 100644 index 00000000000..c3a55c9ca54 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt @@ -0,0 +1,450 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.awaitility.Awaitility.await +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.ConfigurationParams +import org.eclipse.lsp4j.DidChangeConfigurationParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.MessageParams +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +class CompilerConfigurationTest { + @Test + fun `unchanged library settings preserve discovered resource inputs and cached compilation`() { + Session().use { session -> + val config = + mapOf( + "sourceModules" to null, + "libraries" to mapOf("modulePath" to null, "sourceAttachments" to emptyList()), + ) + session.configure(config) + session.open() + session.expect(false) + val before = session.adapter.getCachedResult(session.uri) + val submitted = session.adapter.compilerQueueSnapshot()["submittedTotal"] + + repeat(3) { + session.configure(config) + assertThat(session.adapter.getCachedResult(session.uri)).isEqualTo(before) + assertThat(session.adapter.compilerQueueSnapshot()["submittedTotal"]) + .isEqualTo(submitted) + } + + // Repeated settings may still discover a new file; equality of raw JSON is insufficient. + directory.resolve("Added.x").toFile().writeText("module Added {}") + session.configure(config) + assertThat(session.adapter.effectiveSourceModules().map { it.name }) + .containsExactlyInAnyOrder("Library", "Consumer", "Added") + assertThat(session.adapter.getCachedResult(session.uri)).isEqualTo(before) + assertThat(session.adapter.compilerQueueSnapshot()["submittedTotal"]) + .isEqualTo(submitted) + session.expect(false) + } + } + + @Test + fun `presentation updates preserve pending graph configuration and cached compilation`() { + Session(pull = true).use { session -> + session.open() + session.expect(false) + val before = session.adapter.getCachedResult(session.uri) + session.server.initialized(InitializedParams()) + val pending = requireNotNull(session.requests.poll(10, SECONDS)) + val submitted = session.adapter.compilerQueueSnapshot()["submittedTotal"] + session.server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams( + mapOf("xtc" to mapOf("presentation" to emptyMap())), + ), + ) + assertThat(session.requests).isEmpty() + assertThat(session.adapter.getCachedResult(session.uri)).isEqualTo(before) + assertThat(session.adapter.compilerQueueSnapshot()["submittedTotal"]) + .isEqualTo(submitted) + pending.complete(listOf(mapOf("sourceModules" to emptyList()))) + session.expect(true) + } + } + + @TempDir lateinit var directory: Path + + @Test + fun `explicit source graph overrides build imports and absent reports retain discovery`() { + val explicit = + mapOf( + "sourceModules" to emptyList(), + "buildModels" to listOf(mapOf("schemaVersion" to 99)), + ) + assertThat(CompilerConfiguration.buildModel(explicit)).isNull() + assertThat(CompilerConfiguration.modules(explicit, emptyList())).isEmpty() + assertThat( + CompilerConfiguration.buildModel( + mapOf("sourceModules" to null, "buildModels" to emptyList()), + ), + ).isNull() + assertThatThrownBy { + CompilerConfiguration.buildModel( + mapOf("sourceModules" to null, "buildModels" to listOf(1)), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { + CompilerConfiguration.buildModel( + mapOf( + "sourceModules" to null, + "buildModels" to listOf(mapOf("schemaVersion" to 99)), + ), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + } + + @Test + fun `resource root configuration distinguishes automatic empty and ordered custom paths`() { + directory = directory.toRealPath() + val workspace = directory.toUri().toString() + + fun configuration(resources: List?) = + mapOf( + "sourceModules" to + listOf( + mapOf( + "name" to "Library", + "uri" to "Library.x", + "resourceRoots" to resources, + ), + ), + ) + val module = + CompilerConfiguration + .modules( + configuration(listOf("custom/", "fallback/")), + listOf(workspace), + )!! + .single() + assertThat(module.resourceRoots) + .containsExactly( + directory + .resolve("custom") + .toFile() + .toURI() + .toString() + .trimEnd('/') + "/", + directory + .resolve("fallback") + .toFile() + .toURI() + .toString() + .trimEnd('/') + "/", + ) + assertThat( + CompilerConfiguration + .modules(configuration(null), listOf(workspace))!! + .single() + .resourceRoots, + ).isNull() + assertThat( + CompilerConfiguration + .modules(configuration(emptyList()), listOf(workspace))!! + .single() + .resourceRoots, + ).isEmpty() + assertThatThrownBy { + CompilerConfiguration.modules( + configuration(listOf("custom/", "custom/")), + listOf(workspace), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { + CompilerConfiguration.modules( + configuration(listOf("https://example.com/resources/")), + listOf(workspace), + ) + }.isInstanceOf(IllegalArgumentException::class.java) + assertThat(SourceModuleConfiguration(module).resourceRoots).isEqualTo(module.resourceRoots) + } + + @Test + fun `explicit null restores automatic discovery through JSON and host maps`() { + assertThat(CompilerConfiguration.automatic(mapOf("sourceModules" to null))).isTrue() + val settings = JsonParser.parseString("""{"xtc":{"compiler":{"sourceModules":null}}}""") + assertThat(CompilerConfiguration.automatic(CompilerConfiguration.changed(settings))) + .isTrue() + assertThat(CompilerConfiguration.automatic(emptyMap())).isFalse() + } + + @Test + fun `strict configuration accepts maps and wire JSON while preserving absent versus empty`() { + val workspace = directory.toUri().toString() + for (raw in listOf(CONFIG, Gson().toJsonTree(CONFIG))) { + val modules = requireNotNull(CompilerConfiguration.modules(raw, listOf(workspace))) + assertThat(modules.map { it.name }).containsExactly("Library", "Consumer") + assertThat(modules.last().dependencies).containsExactly("Library") + assertThat(modules.first().uri) + .isEqualTo( + directory + .resolve("Library.x") + .toFile() + .canonicalFile + .toURI() + .toString(), + ) + } + assertThat(CompilerConfiguration.modules(null, emptyList())).isNull() + assertThat(CompilerConfiguration.modules(emptyMap(), emptyList())).isNull() + assertThat( + CompilerConfiguration.modules( + mapOf("sourceModules" to emptyList()), + emptyList(), + ), + ).isEmpty() + for ( + raw in + listOf( + mapOf("sourceModules" to 1), + mapOf("sourceModules" to listOf(1)), + mapOf("sourceModules" to listOf(mapOf("name" to 1, "uri" to "Library.x"))), + mapOf( + "sourceModules" to + listOf(mapOf("name" to "Library", "uri" to "https://example.org/Library.x")), + ), + ) + ) { + assertThatThrownBy { CompilerConfiguration.modules(raw, listOf(workspace)) } + .isInstanceOf(IllegalArgumentException::class.java) + } + assertThatThrownBy { + CompilerConfiguration.modules(CONFIG, listOf(workspace, "file:///other/")) + }.hasMessageContaining("exactly one workspace") + } + + @Test + fun `initialization and settings updates rebuild unchanged editor documents`() { + Session().use { session -> + session.open() + session.expect(false) + session.configure(mapOf("sourceModules" to emptyList())) + session.expect(true) + session.configure(CONFIG) + session.expect(false) + val before = session.adapter.getCachedResult(session.uri) + session.configure(CONFIG) + assertThat(session.adapter.getCachedResult(session.uri)).isEqualTo(before) + session.server.textDocumentService.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem( + session.libraryUri, + "xtc", + 1, + "module Library { static String value() = \"text\"; }", + ), + ), + ) + session.expect(true) + session.server.textDocumentService.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(session.libraryUri, 2), + listOf(TextDocumentContentChangeEvent(LIBRARY)), + ), + ) + session.expect(false) + } + } + + @Test + fun `cyclic and malformed updates report a problem without retiring the working graph`() { + Session().use { session -> + session.open() + session.expect(false) + session.configure( + mapOf( + "sourceModules" to + listOf( + mapOf( + "name" to "Library", + "uri" to "Library.x", + "dependencies" to listOf("Consumer"), + ), + mapOf( + "name" to "Consumer", + "uri" to "Consumer.x", + "dependencies" to listOf("Library"), + ), + ), + ), + ) + session.configure(mapOf("sourceModules" to "bad")) + assertThat(session.messages).hasSize(2) + assertThat(session.messages.first().message).contains("Cyclic", "retained") + assertThat(session.adapter.compile(session.uri, CONSUMER).success).isTrue() + } + } + + @Test + fun `late configuration replies and replies after shutdown cannot replace newer settings`() { + Session(pull = true).use { session -> + session.open() + session.expect(false) + session.server.initialized(InitializedParams()) + val old = requireNotNull(session.requests.poll(10, SECONDS)) + session.server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(emptyMap()), + ) + val latest = requireNotNull(session.requests.poll(10, SECONDS)) + latest.complete(listOf(mapOf("sourceModules" to emptyList()))) + session.expect(true) + old.complete(listOf(CONFIG)) + assertThat(session.adapter.compile(session.uri, CONSUMER).success).isFalse() + session.server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(emptyMap()), + ) + val superseded = requireNotNull(session.requests.poll(10, SECONDS)) + session.server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(mapOf("xtc" to "invalid")), + ) + superseded.complete(listOf(CONFIG)) + assertThat(session.adapter.compile(session.uri, CONSUMER).success).isFalse() + assertThat(session.messages).hasSize(1) + session.server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(emptyMap()), + ) + val closing = requireNotNull(session.requests.poll(10, SECONDS)) + session.close() + closing.complete(listOf(CONFIG)) + assertThat(session.messages).hasSize(1) + } + } + + private inner class Session( + pull: Boolean = false, + ) : AutoCloseable { + val adapter = XdkAdapter() + val server = XtcLanguageServer(adapter) + val published = CopyOnWriteArrayList() + val messages = CopyOnWriteArrayList() + val requests = LinkedBlockingQueue>>() + val uri = + directory + .resolve("Consumer.x") + .toFile() + .canonicalFile + .toURI() + .toString() + val libraryUri = + directory + .resolve("Library.x") + .toFile() + .canonicalFile + .toURI() + .toString() + + init { + directory.resolve("Library.x").toFile().writeText(LIBRARY) + directory.resolve("Consumer.x").toFile().writeText(CONSUMER) + val client = mock(LanguageClient::class.java) + doAnswer { + published.add(it.getArgument(0)) + null + }.`when`(client) + .publishDiagnostics(any()) + doAnswer { + messages.add(it.getArgument(0)) + null + }.`when`(client) + .showMessage(any()) + doAnswer { call -> + if ( + call + .getArgument(0) + .items + .single() + .section == + CompilerConfiguration.SECTION + ) { + CompletableFuture>().also(requests::add) + } else { + CompletableFuture.completedFuture(listOf(emptyMap())) + } + }.`when`(client) + .configuration(any()) + server.connect(client) + server + .initialize( + InitializeParams().apply { + workspaceFolders = + listOf(WorkspaceFolder(directory.toUri().toString(), "test")) + initializationOptions = + mapOf(CompilerConfiguration.INITIALIZATION_KEY to CONFIG) + capabilities = + ClientCapabilities().apply { + textDocument = editorInitializeParams().capabilities.textDocument + workspace = + WorkspaceClientCapabilities().apply { configuration = pull } + } + }, + ).get(20, SECONDS) + } + + fun open() = + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, CONSUMER)), + ) + + fun configure(config: Any) = + server.workspaceService.didChangeConfiguration( + DidChangeConfigurationParams(mapOf("xtc" to mapOf("compiler" to config))), + ) + + fun expect(errors: Boolean) { + await().atMost(30, SECONDS).untilAsserted { + val last = published.lastOrNull { it.uri == uri } + assertThat(last).isNotNull() + assertThat(last!!.version).isEqualTo(7) + assertThat(last.diagnostics.isNotEmpty()).isEqualTo(errors) + } + } + + override fun close() { + server.shutdown().get(20, SECONDS) + } + } + + private companion object { + const val LIBRARY = "module Library { static Int value() = 1; }" + const val CONSUMER = + "module Consumer { package lib import Library; Int run() = lib.value(); }" + val CONFIG = + mapOf( + "sourceModules" to + listOf( + mapOf("name" to "Library", "uri" to "Library.x"), + mapOf( + "name" to "Consumer", + "uri" to "Consumer.x", + "dependencies" to listOf("Library"), + ), + ), + ) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt new file mode 100644 index 00000000000..cd9224be61b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt @@ -0,0 +1,115 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkBuildModel +import org.xvm.lsp.adapter.xdk.XdkLibraries +import org.xvm.lsp.adapter.xdk.toDependency +import java.io.ByteArrayOutputStream +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path + +class CompilerLibrariesTest { + @TempDir lateinit var directory: Path + + @Test + fun `paths preserve order distinguish inheritance and empty and reject aliases or ambiguous relative owners`() { + val base = directory.toUri().toString() + + fun read(paths: Any?) = CompilerLibraries.read(mapOf("libraries" to mapOf("modulePath" to paths)), listOf(base))!! + assertThat(CompilerLibraries.read(emptyMap(), listOf(base))).isNull() + assertThat(read(null).modulePath).isNull() + assertThat(read(emptyList()).modulePath).isEmpty() + assertThat( + read(listOf("second", "first")).modulePath, + ).containsExactly(directory.resolve("second").toFile().canonicalFile, directory.resolve("first").toFile().canonicalFile) + assertThatThrownBy { read(listOf("first", "./first")) }.hasMessageContaining("Duplicate") + assertThatThrownBy { read(listOf("https://example.org/library.xtc")) }.hasMessageContaining("local file") + assertThatThrownBy { + CompilerLibraries.read(mapOf("libraries" to mapOf("modulePath" to listOf("relative"))), listOf(base, "file:///other/")) + }.hasMessageContaining("exactly one") + assertThatThrownBy { read(listOf("missing")).resolve(emptyList()) }.hasMessageContaining("does not exist") + } + + @Test + fun `ordered external binaries and attached navigation stay read only and expire on removal`() { + CompilerTestSupport.configure() + val text = + """ + module Attached { + static Int value() = 7; + } + """.trimIndent() + val errors = ErrorList() + val compilation = EmbeddingSupport.instance().compileModule(Source(text, "Attached.x"), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + val binary = directory.resolve("Attached.xtc") + Files.write(binary, compilation.toDependency().bytes()) + val sources = Files.createDirectories(directory.resolve("sources")) + Files.writeString(sources.resolve("Attached.x"), text) + val raw = + mapOf( + "libraries" to + mapOf( + "modulePath" to listOf(binary.toUri().toString()), + "sourceAttachments" to listOf(mapOf("module" to "Attached", "roots" to listOf(sources.toUri().toString()))), + ), + ) + val libraries = CompilerLibraries.read(raw, emptyList())!! + val artifacts = libraries.resolve(emptyList()) + val uri = directory.resolve("Consumer.x").toUri().toString() + val consumer = "module Consumer { package lib import Attached; Int run() = lib.value(); }" + XdkAdapter().use { adapter -> + adapter.replaceBuildInputs(XdkBuildModel.Inputs(emptyList(), artifacts)) + assertThat(adapter.compile(uri, consumer).success).isTrue() + val target = requireNotNull(adapter.findDefinition(uri, 0, consumer.indexOf("value"))) + assertThat(target.startLine).isEqualTo(1) + assertThat(target.uri).isNotEqualTo(sources.resolve("Attached.x").toUri().toString()) + val document = requireNotNull(adapter.readOnlyDocument(target.uri)) + assertThat(document.text).isEqualTo(text) + val virtual = URI.create(document.uri) + assertThat(virtual.host).isEqualTo("attached") + assertThat(virtual.path).startsWith("/Attached/") + assertThat(adapter.readOnlyDocument(virtual.toASCIIString())).isEqualTo(document) + assertThat(adapter.effectiveSourceModules()).isEmpty() + adapter.replaceDependencies(emptyList()) + assertThat(adapter.readOnlyDocument(target.uri)).isNull() + assertThat(adapter.compile(uri, consumer).success).isFalse() + } + Files.writeString(sources.resolve("Attached.x"), text.replace("7", "8")) + assertThatThrownBy { libraries.resolve(emptyList()) }.hasMessageContaining("does not match") + } + + @Test + fun `earlier module path wins while unknown attachments and bundled overrides fail`() { + CompilerTestSupport.configure() + val artifacts = + listOf(1, 2).map { number -> + val errors = ErrorList() + val compiled = + EmbeddingSupport.instance().compileModule( + Source("module Duplicate { static Int value() = $number; }"), + null, + errors, + ) + assertThat(compiled.succeeded()).isTrue() + directory.resolve("$number.xtc").toFile().apply { writeBytes(compiled.toDependency().bytes()) } + } + val settings = CompilerLibraries(artifacts, emptyMap()) + assertThat(settings.resolve(emptyList()).single().bytes()).isEqualTo(artifacts.first().readBytes()) + assertThatThrownBy { settings.copy(sourceAttachments = mapOf("Unknown" to listOf(directory.toFile()))).resolve(emptyList()) } + .hasMessageContaining("effective external binary") + val bundled = XdkLibraries.module("ecstasy.xtclang.org")!! + val bytes = ByteArrayOutputStream().also { bundled.fileStructure.writeTo(it) }.toByteArray() + val file = directory.resolve("bundled.xtc").toFile().apply { writeBytes(bytes) } + assertThatThrownBy { CompilerLibraries(listOf(file), emptyMap()).resolve(emptyList()) }.hasMessageContaining("bundled XDK") + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt new file mode 100644 index 00000000000..60cb0a4b92e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt @@ -0,0 +1,324 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.WorkDoneProgressBegin +import org.eclipse.lsp4j.WorkDoneProgressCreateParams +import org.eclipse.lsp4j.WorkDoneProgressKind +import org.eclipse.lsp4j.WorkDoneProgressReport +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Executors +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicReference + +class ConnectionProgressTest { + private class Session : AutoCloseable { + val dispatcher = Executors.newSingleThreadExecutor() + val client = mock(LanguageClient::class.java) + val created = LinkedBlockingQueue() + val events = LinkedBlockingQueue() + val ack = CompletableFuture() + val deadline = CompletableFuture() + val reports = LinkedBlockingQueue() + val progress = ConnectionProgress({ client }, 0, dispatcher, { reports.add(it) }) { deadline } + + init { + doAnswer { call -> + created.add(call.getArgument(0)) + ack + }.`when`(client) + .createProgress(any()) + doAnswer { call -> + events.add(call.getArgument(0)) + null + }.`when`(client) + .notifyProgress(any()) + } + + fun event() = requireNotNull(events.poll(5, SECONDS)) { "Missing progress notification" } + + fun flush() { + dispatcher.submit {}.get(5, SECONDS) + } + + override fun close() { + progress.close() + assertThat(dispatcher.awaitTermination(5, SECONDS)).isTrue() + } + } + + @Test + fun `live details change without compiler calls and stop on completion or close`() { + listOf(false, true).forEach { close -> + Session().use { session -> + val work = CompletableFuture() + val details = AtomicReference("Consumer.x · Compiler: compiling Library.x; 1 job queued") + session.progress.track("References", Either.forLeft("references"), work, details::get) + assertThat((session.event().value.left as WorkDoneProgressBegin).message).isEqualTo(details.get()) + session.flush() + details.set("Consumer.x · Compiler: finding references in Consumer.x") + session.reports.remove().run() + assertThat((session.event().value.left as WorkDoneProgressReport).message).isEqualTo(details.get()) + session.flush() + val pending = session.reports.remove() + if (close) session.progress.close() else work.complete("done") + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + pending.run() + if (close) assertThat(session.dispatcher.awaitTermination(5, SECONDS)).isTrue() else session.flush() + assertThat(session.events).isEmpty() + assertThat(session.reports).isEmpty() + } + } + } + + @Test + fun `timed out creation releases a late client registration without canceling the query`() { + listOf(false, true).forEach { completed -> + Session().use { session -> + session.progress.initialized(true) + val work = CompletableFuture() + session.progress.track("References", null, work) + val token = requireNotNull(session.created.poll(5, SECONDS)).token + session.deadline.complete(null) + session.flush() + assertThat(session.ack.isDone).isFalse() + assertThat(session.events).isEmpty() + if (completed) work.complete("done") + session.ack.complete(null) + val begin = session.event() + assertThat(begin.token).isEqualTo(token) + assertThat((begin.value.left as WorkDoneProgressBegin).cancellable).isFalse() + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + session.progress.cancel(token) + session.flush() + assertThat(work.isDone).isEqualTo(completed) + work.complete("done") + session.flush() + assertThat(work.join()).isEqualTo("done") + assertThat(session.events).isEmpty() + } + } + } + + @Test + fun `late creation after connection close sends no progress to a retired client`() { + Session().use { session -> + session.progress.initialized(true) + val work = CompletableFuture() + session.progress.track("References", null, work) + assertThat(session.created.poll(5, SECONDS)).isNotNull() + session.progress.close() + assertThat(session.dispatcher.awaitTermination(5, SECONDS)).isTrue() + session.ack.complete(null) + assertThat(session.events).isEmpty() + assertThat(work.isCancelled).isTrue() + } + } + + @Test + fun `index progress reports belong to the live operation and finish after its future`() { + Session().use { session -> + val work = CompletableFuture() + session.progress.track("Indexing", Either.forLeft("index"), work) + session.event() + session.progress.report(work, "Indexing: 50/100 files", 50) + val report = session.event().value.left as WorkDoneProgressReport + assertThat(report.percentage).isEqualTo(50) + assertThat(report.message).contains("50/100") + assertThat(work.isDone).isFalse() + work.complete(Unit) + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + session.progress.report(work, "Late report", 100) + session.flush() + assertThat(session.events).isEmpty() + } + } + + @Test + fun `supplied tokens work before initialized and end once`() { + Session().use { session -> + val work = CompletableFuture() + val token = Either.forRight(7) + session.progress.track("Indexing", token, work) + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.begin) + assertThat(session.created).isEmpty() + work.complete("done") + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + session.progress.cancel(token) + session.flush() + assertThat(work.join()).isEqualTo("done") + assertThat(session.events).isEmpty() + } + } + + @Test + fun `server tokens wait for handshake and create acknowledgement and cancel only owned work`() { + Session().use { session -> + val unreported = CompletableFuture() + session.progress.track("Before handshake", null, unreported) + session.flush() + assertThat(session.created).isEmpty() + session.progress.initialized(true) + val work = CompletableFuture() + session.progress.track("Rename", null, work) + val token = requireNotNull(session.created.poll(5, SECONDS)).token + assertThat(session.events).isEmpty() + session.ack.complete(null) + assertThat(session.event().token).isEqualTo(token) + session.progress.cancel(token) + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + assertThat(work.isCancelled).isTrue() + assertThat(unreported.isDone).isFalse() + } + } + + @Test + fun `late creation and rejected creation cannot resurrect completed work`() { + listOf(false, true).forEach { reject -> + Session().use { session -> + session.progress.initialized(true) + val work = CompletableFuture() + session.progress.track("References", null, work) + assertThat(session.created.poll(5, SECONDS)).isNotNull() + work.complete("done") + session.flush() + if (reject) { + session.ack.completeExceptionally(IllegalStateException("unsupported")) + } else { + session.ack.complete(null) + } + session.flush() + if (!reject) { + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.begin) + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + } + assertThat(session.events).isEmpty() + assertThat(work.join()).isEqualTo("done") + } + } + } + + @Test + fun `completion cancellation and close racing still end each started token once`() { + Session().use { session -> + val work = List(40) { CompletableFuture() } + work.forEachIndexed { index, result -> + session.progress.track("Query $index", Either.forRight(index), result) + } + val started = work.map { session.event() } + assertThat(started).allSatisfy { + assertThat(it.value.left.kind).isEqualTo(WorkDoneProgressKind.begin) + } + val completions = + work.mapIndexed { index, result -> + CompletableFuture.runAsync { + if (index % 2 == 0) { + result.complete("done") + } else { + session.progress.cancel(Either.forRight(index)) + } + } + } + CompletableFuture.runAsync { session.progress.close() } + CompletableFuture.allOf(*completions.toTypedArray()).get(5, SECONDS) + val ended = work.map { session.event() } + assertThat(ended).allSatisfy { + assertThat(it.value.left.kind).isEqualTo(WorkDoneProgressKind.end) + } + assertThat(ended.map { it.token }) + .containsExactlyInAnyOrderElementsOf(started.map { it.token }) + assertThat(work).allSatisfy { assertThat(it.isDone).isTrue() } + } + } + + @Test + fun `the same client token belongs independently to each connection`() { + Session().use { first -> + Session().use { second -> + val token = Either.forLeft("shared-token") + val one = CompletableFuture() + val two = CompletableFuture() + first.progress.track("First project", token, one) + second.progress.track("Second project", token, two) + assertThat(first.event().token).isEqualTo(token) + assertThat(second.event().token).isEqualTo(token) + first.progress.cancel(token) + assertThat( + first + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + first.progress.close() + assertThat(one.isCancelled).isTrue() + assertThat(two.isDone).isFalse() + second.progress.report(two, "Second project still running", 75) + assertThat( + second + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.report) + two.complete("done") + assertThat( + second + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + assertThat(two.join()).isEqualTo("done") + } + } + } + + @Test + fun `disconnect ends and cancels active progress`() { + Session().use { session -> + val work = CompletableFuture() + session.progress.track("Checking workspace", Either.forLeft("owned"), work) + session.event() + session.progress.close() + assertThat( + session + .event() + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + assertThat(work.isCancelled).isTrue() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt new file mode 100644 index 00000000000..257a44b3a58 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt @@ -0,0 +1,107 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DiagnosticCapabilities +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.PublishDiagnosticsCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.Location + +class DiagnosticPresentationTest { + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `pull diagnostics negotiate related information independently of push and related documents`(support: Boolean) { + val presentation = + ClientPresentation.read( + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + publishDiagnostics = + PublishDiagnosticsCapabilities().apply { + relatedInformation = !support + } + diagnostic = + DiagnosticCapabilities().apply { + relatedInformation = support + relatedDocumentSupport = true + } + } + } + }, + ) + assertThat(presentation.diagnosticRelatedInformation).isEqualTo(!support) + assertThat(presentation.pullDiagnosticRelatedInformation).isEqualTo(support) + val reports = DiagnosticReports() + val root = "file:///Root.x" + val consumer = "file:///Consumer.x" + val origin = Location("file:/External.x", 2, 3, 2, 10) + val error = Diagnostic.error(origin, "External failure") + reports.record(root, listOf(error)) + reports.record(consumer, listOf(error)) + val first = + reports + .document( + root, + null, + setOf(root, consumer), + presentation.pullDiagnosticRelatedInformation, + ).left + val unchanged = + reports + .document( + root, + first.resultId, + setOf(root, consumer), + presentation.pullDiagnosticRelatedInformation, + ).right + assertThat(unchanged.resultId).isEqualTo(first.resultId) + val workspace = + reports.workspace( + setOf(root, consumer), + emptyMap(), + emptyMap(), + presentation.pullDiagnosticRelatedInformation, + ) + val diagnostics = + first.items + + first.relatedDocuments + .getValue(consumer) + .left.items + + unchanged.relatedDocuments + .getValue(consumer) + .left.items + + workspace.items.flatMap { it.left.items } + assertThat(diagnostics).hasSize(5).allSatisfy { diagnostic -> + assertThat(diagnostic.message.left).contains(origin.uri, "External failure") + assertThat(diagnostic.range.start.line).isZero() + if (support) { + assertThat( + diagnostic.relatedInformation + .single() + .location.uri, + ).isEqualTo(origin.uri) + assertThat( + diagnostic.relatedInformation + .single() + .location.range.start.line, + ).isEqualTo(2) + } else { + assertThat(diagnostic.relatedInformation).isNull() + } + } + val cached = + reports.workspace( + setOf(root, consumer), + workspace.items.associate { it.left.uri to it.left.resultId }, + emptyMap(), + presentation.pullDiagnosticRelatedInformation, + ) + assertThat(cached.items).allSatisfy { assertThat(it.isRight).isTrue() } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt new file mode 100644 index 00000000000..68f00d2317b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt @@ -0,0 +1,287 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentRangesFormattingParams +import org.eclipse.lsp4j.FormattingOptions +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SynchronizationCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.TextDocumentSaveReason +import org.eclipse.lsp4j.TextDocumentSyncKind +import org.eclipse.lsp4j.TextEdit +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WillSaveTextDocumentParams +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.model.CompilationResult +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import org.xvm.lsp.adapter.FormattingOptions as AdapterFormattingOptions +import org.xvm.lsp.adapter.Position as AdapterPosition +import org.xvm.lsp.adapter.Range as AdapterRange +import org.xvm.lsp.adapter.TextEdit as AdapterTextEdit + +class DocumentSynchronizationTest { + private val uri = "file:///Sync.x" + private val id = TextDocumentIdentifier(uri) + private val save = WillSaveTextDocumentParams(id, TextDocumentSaveReason.Manual) + + private fun params( + incremental: Boolean = false, + format: Boolean = false, + ) = InitializeParams().apply { + initializationOptions = + mapOf( + "xtcDocumentSync" to + mapOf("incremental" to incremental, "formatOnSave" to format), + ) + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + synchronization = SynchronizationCapabilities(true, true, true) + } + } + } + + @Test + fun `sequential patches use UTF16 and preserve all line endings`() { + val original = "a😀b\r\nsecond\rlast\n" + val updated = + DocumentText(original) + .change( + listOf( + patch(0, 1, 0, 3, "XY"), + patch(0, 3, 1, 3, "!\r\nQ"), + patch(2, 0, 2, 4, "end"), + ), + true, + ) + assertThat(updated).isEqualTo("aXY!\r\nQond\rend\n") + assertThat( + DocumentText(updated) + .change( + listOf(TextDocumentContentChangeEvent("reset"), patch(0, 5, 0, 5, "!")), + true, + ), + ).isEqualTo("reset!") + assertThat(DocumentText("abc").offset(Position(0, 99))).isEqualTo(3) + } + + @Test + fun `invalid patches and overlapping formatter edits fail safely`() { + val text = DocumentText("a😀b\r\nline") + listOf( + patch(0, 2, 0, 3, ""), + patch(2, 0, 2, 0, ""), + patch(1, 2, 0, 0, ""), + patch(0, -1, 0, 0, ""), + ).forEach { change -> + assertThatThrownBy { text.change(listOf(change), true) } + .isInstanceOf(IllegalArgumentException::class.java) + } + assertThatThrownBy { text.change(listOf(patch(0, 0, 0, 0, "x")), false) } + .hasMessageContaining("not negotiated") + val edit = TextEdit(Range(Position(0, 0), Position(0, 3)), "x") + assertThat(text.nonOverlapping(listOf(edit, edit))).containsExactly(edit) + assertThatThrownBy { + text.nonOverlapping( + listOf(edit, TextEdit(Range(Position(0, 1), Position(0, 4)), "y")), + ) + }.hasMessageContaining("overlapping") + } + + @Test + fun `save edits do not wait for compilation or mutate the server document`() { + val compilation = CompletableFuture() + val adapter = + object : Adapter by MockAdapter() { + override fun compileAsync( + uri: String, + content: String, + ) = compilation + + override fun formatDocument( + uri: String, + content: String, + options: AdapterFormattingOptions, + ) = listOf( + AdapterTextEdit( + AdapterRange(AdapterPosition(0, 0), AdapterPosition(0, 0)), + content, + ), + ) + } + XtcLanguageServer(adapter).use { server -> + server.connect(mock(LanguageClient::class.java)) + val caps = + server + .initialize(params(incremental = true, format = true)) + .get(10, SECONDS) + .capabilities + assertThat(caps.textDocumentSync.right.change) + .isEqualTo(TextDocumentSyncKind.Incremental) + assertThat(caps.textDocumentSync.right.willSaveWaitUntil).isTrue() + val documents = server.textDocumentService + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, "a😀b\r\nline")), + ) + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(patch(0, 1, 0, 3, "XY")), + ), + ) + documents.willSave(save) + assertThat( + documents + .willSaveWaitUntil(save) + .get(10, SECONDS) + .single() + .newText, + ).isEqualTo("aXYb\r\nline") + assertThat(compilation.isDone).isFalse() + // A malformed batch must not publish even its valid prefix or advance the version. + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 3), + listOf(patch(0, 0, 0, 1, "Z"), patch(9, 0, 9, 0, "!")), + ), + ) + assertThat( + documents + .willSaveWaitUntil(save) + .get(10, SECONDS) + .single() + .newText, + ).isEqualTo("aXYb\r\nline") + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 3), + listOf(patch(0, 0, 0, 1, "Q")), + ), + ) + assertThat( + documents + .willSaveWaitUntil(save) + .get(10, SECONDS) + .single() + .newText, + ).isEqualTo("QXYb\r\nline") + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(TextDocumentContentChangeEvent("old")), + ), + ) + assertThat( + documents + .willSaveWaitUntil(save) + .get(10, SECONDS) + .single() + .newText, + ).isEqualTo("QXYb\r\nline") + } + } + + @Test + fun `full sync still refuses patches and save formatting is opt in`() { + CompilerTestSupport.configure() + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + val caps = server.initialize(params()).get(10, SECONDS).capabilities + assertThat(caps.textDocumentSync.right.change).isEqualTo(TextDocumentSyncKind.Full) + assertThat(caps.documentRangeFormattingProvider.right.rangesSupport).isTrue() + val documents = server.textDocumentService + val source = "module Sync {\r\nInt value = 1;\r\nString text = \"😀\";\r\n}" + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source))) + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(patch(0, 0, 0, 1, "!")), + ), + ) + assertThat(documents.willSaveWaitUntil(save).get(10, SECONDS)).isEmpty() + val edits = + documents + .rangesFormatting( + DocumentRangesFormattingParams( + id, + FormattingOptions(4, true), + listOf( + Range(Position(2, 0), Position(3, 0)), + Range(Position(1, 0), Position(2, 0)), + Range(Position(1, 0), Position(3, 0)), + ), + ), + ).get(30, SECONDS) + assertThat(edits).hasSize(2) + assertThat(edits.map { it.range.start.line }).containsExactly(1, 2) + assertThat(edits.map { it.newText }).containsOnly(" ") + assertThat( + documents + .rangesFormatting( + DocumentRangesFormattingParams( + id, + FormattingOptions(4, true), + emptyList(), + ), + ).get(10, SECONDS), + ).isEmpty() + assertThatThrownBy { + documents + .rangesFormatting( + DocumentRangesFormattingParams( + id, + FormattingOptions(4, true), + listOf(Range(Position(10, 0), Position(10, 1))), + ), + ).get(10, SECONDS) + }.hasMessageContaining("Invalid text position") + } + } + + @Test + fun `invalid initialization and unnegotiated save requests are rejected`() { + XtcLanguageServer(MockAdapter()).use { server -> + assertThatThrownBy { + server + .initialize( + InitializeParams().apply { + initializationOptions = + mapOf("xtcDocumentSync" to mapOf("incremental" to "yes")) + }, + ).get(10, SECONDS) + }.hasMessageContaining("must be a boolean") + server.initialize(InitializeParams()).get(10, SECONDS) + assertThatThrownBy { + server.textDocumentService.willSaveWaitUntil(save).get(10, SECONDS) + }.hasMessageContaining("not negotiated") + } + } + + private fun patch( + line: Int, + column: Int, + endLine: Int, + endColumn: Int, + text: String, + ) = TextDocumentContentChangeEvent().apply { + range = Range(Position(line, column), Position(endLine, endColumn)) + this.text = text + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt new file mode 100644 index 00000000000..68fe1388517 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt @@ -0,0 +1,50 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class EditorFormattingStateTest { + @Test + fun `disconnect rejects delayed configuration and requests created after close`() { + val state = EditorFormattingState() + val pending = state.request() + state.close() + listOf(pending, state.request()).forEach { revision -> + assertThat(state.accept(revision, mapOf("indentSize" to 2)) { error("closed install") }) + .isFalse() + } + } + + @Test + fun `late replies cannot replace current settings or restore reset configuration`() { + val state = EditorFormattingState() + val old = state.request() + val newest = state.request() + assertThat(state.accept(newest, mapOf("indentSize" to 2)) {}).isTrue() + assertThat(state.accept(old, mapOf("indentSize" to 8)) { error("stale install") }).isFalse() + assertThat(state.config?.indentSize).isEqualTo(2) + state.accept(state.request(), null) {} + assertThat(state.config).isNull() + assertThat(state.accept(newest, mapOf("indentSize" to 2)) { error("reset undone") }) + .isFalse() + } + + @Test + fun `malformed settings retain the previous valid snapshot`() { + val state = EditorFormattingState() + state.accept(state.request(), mapOf("indentSize" to 2)) {} + listOf( + mapOf("indentSize" to 0), + mapOf("indentSize" to 1.5), + mapOf("indentSize" to "2"), + mapOf("insertSpaces" to "false"), + mapOf("continuationIndentSize" to 1000000), + ).forEach { invalid -> + assertThatThrownBy { + state.accept(state.request(), invalid) { error("invalid install") } + }.isInstanceOf(IllegalArgumentException::class.java) + assertThat(state.config?.indentSize).isEqualTo(2) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt new file mode 100644 index 00000000000..578bd044fd5 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt @@ -0,0 +1,213 @@ +package org.xvm.lsp.server + +import ch.qos.logback.classic.Logger +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.AppenderBase +import com.google.gson.JsonObject +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test +import org.slf4j.LoggerFactory +import org.xvm.lsp.adapter.xdk.CompilerQueueTrace +import org.xvm.lsp.util.ExecutionTrace +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS + +class ExecutionTraceTest { + @Test + fun `queue lists preserve order and cancellation does not pretend running work stopped`() { + Recording().use { trace -> + val queue = CompilerQueueTrace() + val running = CompletableFuture() + val next = CompletableFuture() + val retired = CompletableFuture() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val first = + queue.task("compile", "file:///Alpha.x", running) { + entered.countDown() + check(release.await(10, SECONDS)) + } + val second = + queue.task("cursor-COMPLETION", "file:///Beta.x", next) { next.complete(Unit) } + queue + .task("compile", "file:///Gamma.x", retired) { error("Retired job executed") } + .also { it.ready() } + second.ready() + Executors.newSingleThreadExecutor().use { executor -> + val execution = executor.submit(first.ready()) + try { + check(entered.await(10, SECONDS)) + running.cancel(false) + val snapshot = trace.entries.last() + assertThat(snapshot["queueSize"].asInt).isEqualTo(2) + val status = queue.snapshot() + assertThat(status["queueSize"]).isEqualTo(2) + assertThat(status["runningSize"]).isEqualTo(1) + assertThat(queue.progressDescription()).isEqualTo("compiling Alpha.x; 2 jobs queued") + assertThat(status["queuedJobs"].toString()).contains("Gamma.x", "Beta.x") + assertThat(snapshot["queuedJobs"].asJsonArray.map { it.asString }) + .satisfiesExactly( + { assertThat(it).contains("compile", "Gamma.x") }, + { assertThat(it).contains("cursor-COMPLETION", "Beta.x") }, + ) + assertThat(snapshot["runningSize"].asInt).isEqualTo(1) + assertThat(snapshot["runningJobs"].toString()).contains("Alpha.x") + retired.cancel(false) + assertThat(trace.entries.last()["queueSize"].asInt).isEqualTo(1) + } finally { + release.countDown() + } + execution.get(10, SECONDS) + } + second.run() + assertThat(queue.progressDescription()).isNull() + val final = trace.entries.last() + assertThat(final["queueSize"].asInt).isZero() + assertThat(final["runningSize"].asInt).isZero() + assertThat(final["debouncingSize"].asInt).isZero() + assertThat(final["startedTotal"].asInt).isEqualTo(2) + assertThat( + trace.entries + .filter { it["event"].asString == "end" } + .first()["outcome"] + .asString, + ).isEqualTo("cancelled") + } + } + + @Test + fun `API trace exposes overlap without counting nesting as parallel threads`() { + Recording().use { trace -> + val entered = CountDownLatch(2) + val release = CountDownLatch(1) + Executors.newVirtualThreadPerTaskExecutor().use { executor -> + val work = + (1..2).map { index -> + executor.submit { + ExecutionTrace.api("overlap-$index") { + entered.countDown() + check(release.await(10, SECONDS)) + ExecutionTrace.api("nested-$index") { Unit } + } + } + } + try { + check(entered.await(10, SECONDS)) + assertThat( + trace.entries + .filter { it["event"].asString == "start" } + .map { it["activeApiThreads"].asInt }, + ).contains(2) + } finally { + release.countDown() + } + work.forEach { it.get(10, SECONDS) } + } + assertThat( + trace.entries.filter { + it["operation"].asString.startsWith("nested") && + it["event"].asString == "start" + }, + ).allSatisfy { assertThat(it["depth"].asInt).isEqualTo(1) } + assertThat(trace.entries).allSatisfy { + assertThat(it["thread"].asString).isEqualTo("virtual-${it["threadId"].asLong}") + } + assertThatThrownBy { + ExecutionTrace.api("exceptional") { error("source text must not be logged") } + }.isInstanceOf(IllegalStateException::class.java) + ExecutionTrace.api("after-failure") { Unit } + assertThat(trace.entries.toString()).doesNotContain("source text must not be logged") + assertThat( + trace.entries + .last { + it["operation"].asString == "after-failure" && + it["event"].asString == "start" + }["activeApiThreads"] + .asInt, + ).isEqualTo(1) + } + } + + @Test + fun `protocol trace ends at the reply and separates bidirectional IDs`() { + Recording().use { trace -> + ProtocolTrace().use { protocol -> + val received = protocol.wrap(MessageConsumer {}, received = true) + val sent = protocol.wrap(MessageConsumer {}, received = false) + received.consume( + RequestMessage().apply { + setId(7) + method = "textDocument/hover" + }, + ) + sent.consume( + RequestMessage().apply { + setId(7) + method = "workspace/configuration" + }, + ) + assertThat(trace.entries.map { it["event"].asString }) + .containsExactly("start", "start") + sent.consume( + ResponseMessage().apply { + setId(7) + result = "hover" + }, + ) + received.consume( + ResponseMessage().apply { + setId(7) + result = emptyList() + }, + ) + val replies = trace.entries.filter { it["event"].asString == "end" } + assertThat(replies.map { it["operation"].asString }) + .containsExactly("textDocument/hover", "workspace/configuration") + assertThat(replies.map { it["boundary"].asString }) + .containsExactly("server-reply-written", "client-reply-received") + assertThat(replies).allSatisfy { + assertThat(it["elapsedMs"].asDouble).isGreaterThanOrEqualTo(0.0) + } + assertThat(replies.first()["writeMs"].asDouble).isGreaterThanOrEqualTo(0.0) + assertThat(replies.last().has("writeMs")).isFalse() + assertThat(trace.entries.filter { it["event"].asString == "reply-ready" }.map { it["operation"].asString }) + .containsExactly("textDocument/hover") + received.consume( + RequestMessage().apply { + id = "pending" + method = "textDocument/completion" + }, + ) + } + assertThat(trace.entries.last()["outcome"].asString).isEqualTo("transport-closed") + } + } + + private class Recording : AutoCloseable { + val entries = CopyOnWriteArrayList() + private val logger = LoggerFactory.getLogger("org.xvm.lsp.trace") as Logger + private val appender = + object : AppenderBase() { + override fun append(event: ILoggingEvent) { + entries += JsonParser.parseString(event.formattedMessage).asJsonObject + } + }.apply { start() } + + init { + logger.addAppender(appender) + } + + override fun close() { + logger.detachAppender(appender) + appender.stop() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt new file mode 100644 index 00000000000..70d36b1bf68 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt @@ -0,0 +1,44 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path + +class FileChangeSnapshotsTest { + @TempDir lateinit var directory: Path + + @Test + fun `operation and watcher duplicates coalesce but equal timestamp content changes survive`() { + val file = directory.resolve("Member.x") + Files.writeString(file, "first") + val stamp = Files.getLastModifiedTime(file) + val events = FileChangeSnapshots() + val created = FileEvent(file.toUri().toString(), FileChangeType.Created) + val changed = FileEvent(created.uri, FileChangeType.Changed) + assertThat(events.changed(listOf(created, created))).containsExactly(created) + assertThat(events.changed(listOf(changed))).isEmpty() + Files.writeString(file, "other") + Files.setLastModifiedTime(file, stamp) + assertThat(events.changed(listOf(changed))).containsExactly(changed) + Files.delete(file) + val deleted = FileEvent(created.uri, FileChangeType.Deleted) + assertThat(events.changed(listOf(deleted))).containsExactly(deleted) + assertThat(events.changed(listOf(deleted))).isEmpty() + Files.writeString(file, "other") + assertThat(events.changed(listOf(created))).containsExactly(created) + } + + @Test + fun `folder notifications always reach discovery without scanning trees on the notification thread`() { + val events = FileChangeSnapshots() + val folder = FileEvent(directory.toUri().toString(), FileChangeType.Created) + assertThat(events.changed(listOf(folder))).hasSize(1) + assertThat(events.changed(listOf(folder))).hasSize(1) + Files.writeString(directory.resolve("Child.x"), "class Child {}") + assertThat(events.changed(listOf(folder))).hasSize(1) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FormattingConfigRoundTripTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FormattingConfigRoundTripTest.kt index 423d14e3199..31bcf6c2d4d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FormattingConfigRoundTripTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FormattingConfigRoundTripTest.kt @@ -6,7 +6,6 @@ import org.eclipse.lsp4j.ConfigurationParams import org.eclipse.lsp4j.DidOpenTextDocumentParams import org.eclipse.lsp4j.DocumentOnTypeFormattingParams import org.eclipse.lsp4j.FormattingOptions -import org.eclipse.lsp4j.InitializeParams import org.eclipse.lsp4j.InitializedParams import org.eclipse.lsp4j.Position import org.eclipse.lsp4j.TextDocumentIdentifier @@ -30,7 +29,6 @@ import java.util.concurrent.CompletableFuture /** * Round-trip test for formatting config flow: - * * ``` * IntelliJ Code Style → XtcLanguageClient.configuration() * → workspace/configuration response @@ -41,9 +39,9 @@ import java.util.concurrent.CompletableFuture * ``` * * This test creates a real [XtcLanguageServer] with [TreeSitterAdapter], wires a mock - * [LanguageClient] that responds to `workspace/configuration` with custom indent settings, - * and verifies that `textDocument/onTypeFormatting` produces indentation matching the - * client-provided config rather than the XTC defaults. + * [LanguageClient] that responds to `workspace/configuration` with custom indent settings, and + * verifies that `textDocument/onTypeFormatting` produces indentation matching the client-provided + * config rather than the XTC defaults. */ @DisplayName("FormattingConfigRoundTrip") @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -68,37 +66,46 @@ class FormattingConfigRoundTripTest { } /** - * Create a mock [LanguageClient] that responds to `workspace/configuration` requests - * with the given formatting settings map. + * Create a mock [LanguageClient] that responds to `workspace/configuration` requests with the + * given formatting settings map. */ private fun mockClientWithConfig(config: Map): LanguageClient { val client = mock(LanguageClient::class.java) - `when`(client.configuration(org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java))) - .thenReturn(CompletableFuture.completedFuture(listOf(config))) + `when`( + client.configuration( + org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java), + ), + ).thenReturn(CompletableFuture.completedFuture(listOf(config))) return client } private fun mockClientWithJsonConfig(config: JsonObject): LanguageClient { val client = mock(LanguageClient::class.java) - `when`(client.configuration(org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java))) - .thenReturn(CompletableFuture.completedFuture(listOf(config))) + `when`( + client.configuration( + org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java), + ), + ).thenReturn(CompletableFuture.completedFuture(listOf(config))) return client } /** - * Create a mock client that returns null/empty for `workspace/configuration`, - * simulating a client that doesn't support the XTC config section. + * Create a mock client that returns null/empty for `workspace/configuration`, simulating a + * client that doesn't support the XTC config section. */ private fun mockClientWithoutConfig(): LanguageClient { val client = mock(LanguageClient::class.java) - `when`(client.configuration(org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java))) - .thenReturn(CompletableFuture.completedFuture(listOf(null))) + `when`( + client.configuration( + org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java), + ), + ).thenReturn(CompletableFuture.completedFuture(listOf(null))) return client } /** - * Full server lifecycle: initialize → initialized → didOpen → onTypeFormatting. - * Returns the indent size from the first TextEdit, or -1 if no edits. + * Full server lifecycle: initialize → initialized → didOpen → onTypeFormatting. Returns the + * indent size from the first TextEdit, or -1 if no edits. */ private fun formatWithServer( server: XtcLanguageServer, @@ -154,7 +161,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() // Trigger the initialized callback — this sends workspace/configuration server.initialized(InitializedParams()) @@ -181,7 +188,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) // Allow async config request to complete @@ -207,7 +214,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithJsonConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -224,7 +231,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithoutConfig() val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -250,7 +257,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) // Wait for async config to be stored @@ -270,7 +277,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithoutConfig() val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -296,7 +303,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -324,7 +331,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -366,7 +373,7 @@ class FormattingConfigRoundTripTest { val client = mockClientWithConfig(customConfig) val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -415,16 +422,19 @@ class FormattingConfigRoundTripTest { ) var callCount = 0 val client = mock(LanguageClient::class.java) - `when`(client.configuration(org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java))) - .thenAnswer { - val idx = callCount.coerceAtMost(configs.size - 1) - callCount++ - CompletableFuture.completedFuture(listOf(configs[idx])) - } + `when`( + client.configuration( + org.mockito.ArgumentMatchers.any(ConfigurationParams::class.java), + ), + ).thenAnswer { + val idx = callCount.coerceAtMost(configs.size - 1) + callCount++ + CompletableFuture.completedFuture(listOf(configs[idx])) + } val server = XtcLanguageServer(adapter!!) server.connect(client) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.initialized(InitializedParams()) Thread.sleep(100) @@ -439,7 +449,9 @@ class FormattingConfigRoundTripTest { Thread.sleep(100) // Verify the server re-requested and stored the updated config - assertThat(callCount).describedAs("configuration() should have been called twice").isEqualTo(2) + assertThat(callCount) + .describedAs("configuration() should have been called twice") + .isEqualTo(2) assertThat(server.editorFormattingConfig!!.indentSize).isEqualTo(6) assertThat(server.editorFormattingConfig!!.continuationIndentSize).isEqualTo(12) } @@ -458,7 +470,7 @@ class FormattingConfigRoundTripTest { insertSpaces = true, ) - val resolved = FormattingConfig.resolve("file:///test.x", lspOptions, editorConfig) + val resolved = FormattingConfig.resolve(lspOptions, editorConfig) assertThat(resolved.indentSize) .describedAs("editor config (3) should win over LSP options (4)") @@ -475,7 +487,7 @@ class FormattingConfigRoundTripTest { insertSpaces = true, ) - val resolved = FormattingConfig.resolve("file:///test.x", lspOptions, null) + val resolved = FormattingConfig.resolve(lspOptions, null) assertThat(resolved.indentSize) .describedAs("LSP tabSize should be used when no editor config") @@ -491,7 +503,7 @@ class FormattingConfigRoundTripTest { insertSpaces = false, ) - val resolved = FormattingConfig.resolve("file:///test.x", lspOptions, null) + val resolved = FormattingConfig.resolve(lspOptions, null) // When insertSpaces is false, XTC falls back to its own default indent size assertThat(resolved.indentSize) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt new file mode 100644 index 00000000000..2057812241e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt @@ -0,0 +1,109 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.WorkDoneProgressBegin +import org.eclipse.lsp4j.WorkDoneProgressCancelParams +import org.eclipse.lsp4j.WorkDoneProgressKind +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.mock.MockAdapter +import java.util.concurrent.CompletableFuture +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +class IndexingProgressTest { + @Test + fun `query progress identifies the requested source or workspace`() { + val client = mock(LanguageClient::class.java) + val events = LinkedBlockingQueue() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client).notifyProgress(any()) + XtcLanguageServer(MockAdapter()).use { server -> + server.connect(client) + server + .initialize( + InitializeParams().apply { + workspaceFolders = listOf(WorkspaceFolder("file:///code/platform/", "platform")) + }, + ).get(5, SECONDS) + listOf( + Triple("textDocument/references", "file:///code/platform/src/Consumer.x", "src/Consumer.x"), + Triple("workspace/diagnostic", "", "Workspace: configured source graph"), + ).forEach { (method, uri, expected) -> + val work = CompletableFuture() + val params = ReferenceParams().apply { workDoneToken = Either.forLeft(method) } + server.observeQuery(method, params, work, uri) + val begin = events.poll(5, SECONDS).value.left as WorkDoneProgressBegin + assertThat(begin.message).startsWith(expected).doesNotContain("Waiting for analysis or running compiler query") + work.complete(Unit) + assertThat( + events + .poll(5, SECONDS) + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + } + } + } + + @Test + fun `initialization reports the actual background scan lifetime and owns cancellation`() { + listOf(false, true).forEach { cancel -> + val scan = CompletableFuture() + val adapter = + object : Adapter by MockAdapter() { + override fun initializeWorkspaceAsync( + workspaceFolders: List, + progressReporter: ((String, Int) -> Unit)?, + ) = scan + } + val client = mock(LanguageClient::class.java) + val events = LinkedBlockingQueue() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client) + .notifyProgress(any()) + XtcLanguageServer(adapter).use { server -> + server.connect(client) + val token = Either.forLeft("indexing") + server + .initialize( + InitializeParams().apply { + workspaceFolders = + listOf(WorkspaceFolder("file:///workspace/", "workspace")) + workDoneToken = token + }, + ).get(5, SECONDS) + assertThat( + events + .poll(5, SECONDS) + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.begin) + assertThat(scan.isDone).isFalse() + assertThat(events).isEmpty() + if (cancel) { + server.cancelProgress(WorkDoneProgressCancelParams(token)) + } else { + scan.complete(Unit) + } + assertThat( + events + .poll(5, SECONDS) + .value.left.kind, + ).isEqualTo(WorkDoneProgressKind.end) + assertThat(scan.isCancelled).isEqualTo(cancel) + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt new file mode 100644 index 00000000000..02817503640 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt @@ -0,0 +1,149 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InlineCompletionContext +import org.eclipse.lsp4j.InlineCompletionParams +import org.eclipse.lsp4j.InlineCompletionTriggerKind +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SelectedCompletionInfo +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.AdapterCapability +import org.xvm.lsp.adapter.TextEdit +import org.xvm.lsp.adapter.mock.MockAdapter +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CompletionException +import java.util.concurrent.TimeUnit.SECONDS +import org.xvm.lsp.adapter.InlineCompletionContext as AdapterContext +import org.xvm.lsp.adapter.Position as AdapterPosition +import org.xvm.lsp.adapter.Range as AdapterRange + +class InlineCompletionProtocolTest { + @Test + fun `inline completion negotiates independently and round trips selection trigger and plain edits`() { + val received = CompletableFuture() + val adapter = + inlineAdapter { context -> + received.complete(context) + CompletableFuture.completedFuture(listOf(TextEdit(AdapterRange(AdapterPosition(0, 0), AdapterPosition(0, 2)), "answer"))) + } + XtcLanguageServer(adapter).use { server -> + server.connect(mock(LanguageClient::class.java)) + val capabilities = server.initialize(editorInitializeParams()).join().capabilities + assertThat(capabilities.inlineCompletionProvider.left).isTrue() + val request = + params().apply { + context.selectedCompletionInfo = SelectedCompletionInfo(Range(Position(0, 0), Position(0, 2)), "ans") + } + val item = + server.textDocumentService + .inlineCompletion(request) + .get(5, SECONDS) + .right.items + .single() + assertThat(item.insertText.left).isEqualTo("answer") + assertThat(item.filterText).isEqualTo("answer") + assertThat(item.range).isEqualTo(request.context.selectedCompletionInfo.range) + assertThat(item.command).isNull() + assertThat(received.join()).isEqualTo( + AdapterContext(false, TextEdit(AdapterRange(AdapterPosition(0, 0), AdapterPosition(0, 2)), "ans")), + ) + } + } + + @Test + fun `unnegotiated clients and unsupported adapters cannot invoke inline completion`() { + listOf(false, true).forEach { advertised -> + val adapter = if (advertised) inlineAdapter { error("Unnegotiated request reached adapter") } else MockAdapter() + XtcLanguageServer(adapter).use { server -> + val initialize = editorInitializeParams().apply { if (advertised) capabilities.textDocument.inlineCompletion = null } + assertThat( + server + .initialize(initialize) + .join() + .capabilities.inlineCompletionProvider, + ).isNull() + val failure = + assertThrows(CompletionException::class.java) { + server.textDocumentService.inlineCompletion(params()).join() + } + assertThat(failure.cause).isInstanceOf(ResponseErrorException::class.java) + assertThat((failure.cause as ResponseErrorException).responseError.code) + .isEqualTo(ResponseErrorCode.MethodNotFound.value) + } + } + } + + @Test + fun `typing or cancellation retires an inline query before it can publish`() { + listOf(false, true).forEach { cancel -> + val entered = CompletableFuture() + val canceled = CompletableFuture() + val pending = + CompletableFuture>().apply { + whenComplete { _, _ -> if (isCancelled) canceled.complete(Unit) } + } + val adapter = + inlineAdapter { + entered.complete(Unit) + pending + } + XtcLanguageServer(adapter).use { server -> + server.connect(mock(LanguageClient::class.java)) + server.initialize(editorInitializeParams()).join() + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, "module App {}"))) + val result = documents.inlineCompletion(params()) + entered.get(5, SECONDS) + if (cancel) { + result.cancel(false) + } else { + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, 2), + listOf(TextDocumentContentChangeEvent("module Changed {}")), + ), + ) + } + assertThatThrownBy { result.get(5, SECONDS) }.isInstanceOf(Exception::class.java) + canceled.get(5, SECONDS) + assertThat(pending.isCancelled).isTrue() + } + } + } + + private fun inlineAdapter(query: (AdapterContext) -> CompletableFuture>) = + object : Adapter by MockAdapter() { + override val capabilities = setOf(AdapterCapability.INLINE_COMPLETION) + + override fun getInlineCompletionsAsync( + uri: String, + position: AdapterPosition, + context: AdapterContext, + ) = query(context) + } + + private fun params() = + InlineCompletionParams( + TextDocumentIdentifier(URI), + Position(0, 2), + InlineCompletionContext(InlineCompletionTriggerKind.Invoked), + ) + + companion object { + private const val URI = "file:///App.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt new file mode 100644 index 00000000000..3fbaa3a488d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt @@ -0,0 +1,166 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.DefinitionParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.Location +import org.eclipse.lsp4j.MonikerParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentContentCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentContentParams +import org.eclipse.lsp4j.TextDocumentContentRefreshParams +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.net.URI +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +class LibraryContentProtocolTest { + @Test + fun `library content is negotiated per connection and cannot read unregistered or retired URIs`() { + listOf(false, true).forEach { supported -> + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + val params = + editorInitializeParams().apply { + if (supported) capabilities.workspace.textDocumentContent = TextDocumentContentCapabilities() + } + val capabilities = server.initialize(params).join().capabilities + assertThat(capabilities.workspace.textDocumentContent != null).isEqualTo(supported) + val target = definition(server) + if (supported) { + assertThat(URI(target.uri).scheme).isEqualTo("ecstasy-library") + val content = + server.workspaceService + .textDocumentContent(TextDocumentContentParams(target.uri)) + .join() + .text + assertThat(content.lines()[target.range.start.line]).contains("const String") + XtcLanguageServer(XdkAdapter()).use { other -> + other.initialize(params).join() + refused(other, target.uri) + } + refused(server, target.uri + "/../secret") + server.close() + refused(server, target.uri) + } else { + assertThat(URI(target.uri).scheme).isEqualTo("file") + assertThat(Files.readString(Path.of(URI(target.uri)))).contains("const String") + refused(server, target.uri) + } + refused(server, "file:///etc/passwd") + } + } + XtcLanguageServer(MockAdapter()).use { server -> + val params = editorInitializeParams().apply { capabilities.workspace.textDocumentContent = TextDocumentContentCapabilities() } + assertThat( + server + .initialize(params) + .join() + .capabilities.workspace + ?.textDocumentContent, + ).isNull() + } + } + + @Test + fun `fetched library views refresh and untrusted client presentations never replace artifact content`() { + val notifications = LinkedBlockingQueue() + val reply = CompletableFuture() + val client = mock(LanguageClient::class.java) + doAnswer { invocation -> + notifications.add(invocation.getArgument(0)) + reply + }.`when`(client).refreshTextDocumentContent(any()) + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(client) + server + .initialize( + editorInitializeParams().apply { + capabilities.workspace.textDocumentContent = + TextDocumentContentCapabilities() + }, + ).join() + server.initialized(InitializedParams()) + val target = definition(server) + server.readOnlyDocuments.refresh() + assertThat(notifications).isEmpty() // Navigating alone does not fetch content. + val workspace = server.workspaceService + val params = TextDocumentContentParams(target.uri) + val text = workspace.textDocumentContent(params).join().text + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(target.uri, "xtc", 1, text.replace("\n", "\r\n")))) + val moniker = MonikerParams(TextDocumentIdentifier(target.uri), target.range.start) + val original = documents.moniker(moniker).get(30, SECONDS) + assertThat(original).hasSize(1) + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(target.uri, 2), + listOf(TextDocumentContentChangeEvent("module Forged {}")), + ), + ) + assertThat(documents.moniker(moniker).get(30, SECONDS)).isEmpty() + assertThat(workspace.textDocumentContent(params).join().text).isEqualTo(text) + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(target.uri, 3), + listOf(TextDocumentContentChangeEvent(text.replace("const String", "const String"))), + ), + ) + assertThat(documents.moniker(moniker).get(30, SECONDS)).isEmpty() + documents.didChange( + DidChangeTextDocumentParams(VersionedTextDocumentIdentifier(target.uri, 4), listOf(TextDocumentContentChangeEvent(text))), + ) + assertThat(documents.moniker(moniker).get(30, SECONDS)).isEqualTo(original) + server.replaceCompilerDependencies(emptyList()) + assertThat(notifications.poll(10, SECONDS)?.uri).isEqualTo(target.uri) + server.readOnlyDocuments.refresh() + assertThat(notifications).isEmpty() + assertThat(workspace.textDocumentContent(params).join().text).isEqualTo(text) + val acknowledged = server.readOnlyDocuments.refresh() + reply.complete(null) + acknowledged.get(10, SECONDS) + server.readOnlyDocuments.refresh() + assertThat(notifications.poll(10, SECONDS)?.uri).isEqualTo(target.uri) + server.close() + server.readOnlyDocuments.refresh() + assertThat(notifications).isEmpty() + } + } + + private fun definition(server: XtcLanguageServer): Location { + val uri = "file:///App.x" + val text = "module App { String text = \"value\"; }" + server.textDocumentService.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text))) + return server.textDocumentService + .definition( + DefinitionParams(TextDocumentIdentifier(uri), Position(0, text.indexOf("String"))), + ).get(30, SECONDS) + .left + .single() + } + + private fun refused( + server: XtcLanguageServer, + uri: String, + ) { + assertThatThrownBy { server.workspaceService.textDocumentContent(TextDocumentContentParams(uri)).join() } + .hasCauseInstanceOf(ResponseErrorException::class.java) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspIntegrationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspIntegrationTest.kt index 6e6c7bfd252..57371ad4481 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspIntegrationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspIntegrationTest.kt @@ -13,7 +13,6 @@ import org.eclipse.lsp4j.DocumentSymbolParams import org.eclipse.lsp4j.FoldingRangeRequestParams import org.eclipse.lsp4j.FormattingOptions import org.eclipse.lsp4j.HoverParams -import org.eclipse.lsp4j.InitializeParams import org.eclipse.lsp4j.InlayHintParams import org.eclipse.lsp4j.Position import org.eclipse.lsp4j.PrepareRenameParams @@ -34,8 +33,8 @@ import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.junit.jupiter.api.TestInstance import org.mockito.ArgumentCaptor -import org.mockito.Mockito.atLeastOnce import org.mockito.Mockito.mock +import org.mockito.Mockito.timeout import org.mockito.Mockito.verify import org.xvm.lsp.adapter.Adapter import org.xvm.lsp.adapter.mock.MockAdapter @@ -45,10 +44,10 @@ import java.nio.file.Path import java.nio.file.Paths /** - * Integration test that exercises the LSP server against real `.x` source files from - * the repository. Unlike [XtcLanguageServerTest], which uses synthetic inline snippets - * with the [MockAdapter], this test opens actual XTC standard library and - * manual test files and verifies that each LSP capability returns meaningful results. + * Integration test that exercises the LSP server against real `.x` source files from the + * repository. Unlike [XtcLanguageServerTest], which uses synthetic inline snippets with the + * [MockAdapter], this test opens actual XTC standard library and manual test files and verifies + * that each LSP capability returns meaningful results. * * ## How to run * @@ -57,38 +56,37 @@ import java.nio.file.Paths * --tests "org.xvm.lsp.server.LspIntegrationTest" * ``` * - * The test **must** be run via Gradle (not directly from IntelliJ) because the build - * passes the system property `xtc.composite.root` to the test JVM. This property points - * to the top-level project root (found via the `version.properties` marker file, using - * the same approach as `XdkPropertiesService.compositeRootDirectory()`), which is needed - * to locate the real `.x` source files in `lib_ecstasy/` and `manualTests/`. + * The test **must** be run via Gradle (not directly from IntelliJ) because the build passes the + * system property `xtc.composite.root` to the test JVM. This property points to the top-level + * project root (found via the `version.properties` marker file, using the same approach as + * `XdkPropertiesService.compositeRootDirectory()`), which is needed to locate the real `.x` source + * files in `lib_ecstasy/` and `manualTests/`. * * ## Adapter selection * - * At startup (`@BeforeAll`), the test tries to instantiate a [TreeSitterAdapter]. If the - * native tree-sitter library is available (which it is when the `tree-sitter` subproject - * has been built), all tests run against the real syntax-aware parser. If the native lib - * is unavailable (e.g. in a CI environment without native builds), the test falls back to - * [MockAdapter] and the tests still pass -- they just exercise regex-based parsing. + * At startup (`@BeforeAll`), the test tries to instantiate a [TreeSitterAdapter]. If the native + * tree-sitter library is available (which it is when the `tree-sitter` subproject has been built), + * all tests run against the real syntax-aware parser. If the native lib is unavailable (e.g. in a + * CI environment without native builds), the test falls back to [MockAdapter] and the tests still + * pass -- they just exercise regex-based parsing. * * ## Test files - * - * | File | Path | Why | - * |------|------|-----| - * | `Boolean.x` | `lib_ecstasy/.../ecstasy/Boolean.x` | Enum with methods, `@Op` annotations, many self-references | - * | `Exception.x` | `lib_ecstasy/.../ecstasy/Exception.x` | Const class, constructor, properties, methods | - * | `Closeable.x` | `lib_ecstasy/.../ecstasy/Closeable.x` | Small interface with doc comments | - * | `TestSimple.x` | `lang/lsp-server/src/test/resources/fixtures/TestSimple.x` | Stable local fixture for module with `@Inject`, local vars | + * |File |Path |Why | + * |--------------|----------------------------------------------------------|----------------------------------------------------------| + * |`Boolean.x` |`lib_ecstasy/.../ecstasy/Boolean.x` |Enum with methods, `@Op` annotations, many self-references| + * |`Exception.x` |`lib_ecstasy/.../ecstasy/Exception.x` |Const class, constructor, properties, methods | + * |`Closeable.x` |`lib_ecstasy/.../ecstasy/Closeable.x` |Small interface with doc comments | + * |`TestSimple.x`|`lang/lsp-server/src/test/resources/fixtures/TestSimple.x`|Stable local fixture for module with `@Inject`, local vars| * * ## How it works * - * Each test creates a fresh [XtcLanguageServer] with a mock [LanguageClient] (`@BeforeEach`), - * then opens a real file via `didOpen` and calls the LSP method under test (hover, completion, + * Each test creates a fresh [XtcLanguageServer] with a mock [LanguageClient] (`@BeforeEach`), then + * opens a real file via `didOpen` and calls the LSP method under test (hover, completion, * definition, etc.). Assertions verify the server returns structurally correct, non-empty results. * - * Symbol positions are found dynamically using [findPosition], which locates a string in the - * file content and converts it to a 0-based `(line, column)` position. This makes the tests - * resilient to edits in the test files. + * Symbol positions are found dynamically using [findPosition], which locates a string in the file + * content and converts it to a 0-based `(line, column)` position. This makes the tests resilient to + * edits in the test files. * * @see XtcLanguageServerTest for unit tests with synthetic snippets */ @@ -120,7 +118,8 @@ class LspIntegrationTest { "Boolean.x" to loadTestFile(root, "lib_ecstasy/src/main/x/ecstasy/Boolean.x"), "Exception.x" to loadTestFile(root, "lib_ecstasy/src/main/x/ecstasy/Exception.x"), "Closeable.x" to loadTestFile(root, "lib_ecstasy/src/main/x/ecstasy/Closeable.x"), - "TestSimple.x" to loadTestFile(root, "lang/lsp-server/src/test/resources/fixtures/TestSimple.x"), + "TestSimple.x" to + loadTestFile(root, "lang/lsp-server/src/test/resources/fixtures/TestSimple.x"), ) } @@ -129,14 +128,17 @@ class LspIntegrationTest { server = XtcLanguageServer(adapter) mockClient = mock(LanguageClient::class.java) server.connect(mockClient) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() } // ======================================================================== // Helpers // ======================================================================== - private fun createTreeSitterAdapterOrNull(): TreeSitterAdapter? = runCatching { TreeSitterAdapter() }.getOrNull() + private fun createTreeSitterAdapterOrNull(): TreeSitterAdapter? = + runCatching { + TreeSitterAdapter() + }.getOrNull() private fun resolveProjectRoot(): Path { val root = @@ -161,9 +163,9 @@ class LspIntegrationTest { } /** - * Find the 0-based (line, column) of an occurrence of [text] in [content]. - * Starts searching from [startIndex] (default 0) so callers can skip past - * occurrences in comments or other non-code regions. + * Find the 0-based (line, column) of an occurrence of [text] in [content]. Starts searching + * from [startIndex] (default 0) so callers can skip past occurrences in comments or other + * non-code regions. */ private fun findPosition( content: String, @@ -171,7 +173,9 @@ class LspIntegrationTest { startIndex: Int = 0, ): Position { val idx = content.indexOf(text, startIndex) - assertThat(idx).describedAs("'$text' not found in content from index $startIndex").isGreaterThanOrEqualTo(0) + assertThat(idx) + .describedAs("'$text' not found in content from index $startIndex") + .isGreaterThanOrEqualTo(0) val line = content.substring(0, idx).count { it == '\n' } val lastNewline = content.lastIndexOf('\n', idx - 1) val col = idx - lastNewline - 1 @@ -207,7 +211,7 @@ class LspIntegrationTest { openFile("TestSimple.x") val captor = ArgumentCaptor.forClass(PublishDiagnosticsParams::class.java) - verify(mockClient, atLeastOnce()).publishDiagnostics(captor.capture()) + verify(mockClient, timeout(5000).atLeastOnce()).publishDiagnostics(captor.capture()) val published = captor.value assertThat(published.uri).isEqualTo(testFiles.getValue("TestSimple.x").uri) @@ -511,8 +515,9 @@ class LspIntegrationTest { val ranges = server.textDocumentService - .selectionRange(SelectionRangeParams(TextDocumentIdentifier(tf.uri), listOf(pos))) - .get() + .selectionRange( + SelectionRangeParams(TextDocumentIdentifier(tf.uri), listOf(pos)), + ).get() assertThat(ranges).isNotNull() assertThat(ranges).hasSize(1) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspProcessLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspProcessLifecycleTest.kt index 42916181f37..6bbe8aab152 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspProcessLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LspProcessLifecycleTest.kt @@ -14,24 +14,22 @@ import org.eclipse.lsp4j.TextDocumentItem import org.eclipse.lsp4j.WorkspaceFolder import org.eclipse.lsp4j.launch.LSPLauncher import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Tag import org.junit.jupiter.api.io.TempDir import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.Arguments import org.junit.jupiter.params.provider.MethodSource -import java.io.File import java.nio.file.Files import java.nio.file.Path -import java.util.Properties import java.util.concurrent.CompletableFuture import java.util.concurrent.Executors import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS -import java.util.jar.JarFile /** Runs the production launcher in child JVMs; fallback cleanup must not hide a failure to exit. */ +@Tag("compiler-stdio") class LspProcessLifecycleTest { - @TempDir - lateinit var directory: Path + @TempDir lateinit var directory: Path enum class Termination { EOF_BEFORE_INITIALIZE, @@ -47,18 +45,11 @@ class LspProcessLifecycleTest { backend: String, termination: Termination, ) { - val jar = Path.of(requireNotNull(System.getProperty("xtc.lsp.jar")) { "Run the Gradle test task" }) - // Override only build selection, exercising every shipped backend from the packaged classes. - // The resource directory precedes the JAR, avoiding an extra distribution build per backend. - val resources = Files.createDirectory(directory.resolve("resources")) - val properties = Properties() - JarFile(jar.toFile()).use { archive -> - archive.getInputStream(archive.getJarEntry("lsp-version.properties")).use { properties.load(it) } - } - properties.setProperty("lsp.adapter", backend) - Files.newOutputStream(resources.resolve("lsp-version.properties")).use { properties.store(it, null) } + val jar = + Path.of(requireNotNull(System.getProperty("xtc.lsp.jar")) { "Run compilerStdioTest" }) + // Exercise the same runtime override used by IDE adapter switching, from one packaged JAR. val workspace = Files.createDirectory(directory.resolve("workspace")) - val source = "module Lifecycle { Int value=1; }" + val source = "module Lifecycle { Int value = 1; }" val file = Files.writeString(workspace.resolve("Lifecycle.x"), source) val stderr = directory.resolve("stderr.log") val process = @@ -70,10 +61,11 @@ class LspProcessLifecycleTest { .orElseThrow(), "--enable-native-access=ALL-UNNAMED", "-Duser.home=$directory", - "-cp", - "$resources${File.pathSeparator}$jar", - "org.xvm.lsp.server.XtcLanguageServerLauncherKt", - ).redirectError(stderr.toFile()).start() + "-Dxtc.lsp.adapter=$backend", + "-jar", + jar.toString(), + ).redirectError(stderr.toFile()) + .start() val executor = Executors.newVirtualThreadPerTaskExecutor() val diagnostics = LinkedBlockingQueue() val client = @@ -95,7 +87,15 @@ class LspProcessLifecycleTest { CompletableFuture.completedFuture(params.items.map { emptyMap() }) } try { - val launcher = LSPLauncher.createClientLauncher(client, process.inputStream, process.outputStream, executor) { it } + val launcher = + LSPLauncher.createClientLauncher( + client, + process.inputStream, + process.outputStream, + executor, + ) { + it + } launcher.startListening() val server = launcher.remoteProxy if (termination != Termination.EOF_BEFORE_INITIALIZE) { @@ -103,28 +103,40 @@ class LspProcessLifecycleTest { .initialize( InitializeParams().apply { capabilities = ClientCapabilities() - workspaceFolders = listOf(WorkspaceFolder(workspace.toUri().toString(), "Lifecycle")) + workspaceFolders = + listOf(WorkspaceFolder(workspace.toUri().toString(), "Lifecycle")) }, ).get(20, SECONDS) server.initialized(InitializedParams()) server.textDocumentService.didOpen( - DidOpenTextDocumentParams(TextDocumentItem(file.toUri().toString(), "xtc", 1, source)), + DidOpenTextDocumentParams( + TextDocumentItem(file.toUri().toString(), "xtc", 1, source), + ), ) - assertThat(diagnostics.poll(20, SECONDS)).describedAs("Server must have processed the document").isNotNull() + assertThat(diagnostics.poll(20, SECONDS)) + .describedAs("Server must have processed the document") + .isNotNull() } - val shutdown = termination in setOf(Termination.SHUTDOWN_THEN_EOF, Termination.SHUTDOWN_AND_EXIT) + val shutdown = + termination in setOf(Termination.SHUTDOWN_THEN_EOF, Termination.SHUTDOWN_AND_EXIT) if (shutdown) server.shutdown().get(20, SECONDS) when (termination) { - Termination.EXIT_WITHOUT_SHUTDOWN, Termination.SHUTDOWN_AND_EXIT -> server.exit() + Termination.EXIT_WITHOUT_SHUTDOWN, + Termination.SHUTDOWN_AND_EXIT, + -> server.exit() + else -> process.outputStream.close() } assertThat(process.waitFor(10, SECONDS)) - .describedAs("$backend survived $termination; pid=${process.pid()}\n${Files.readString(stderr)}") - .isTrue() + .describedAs( + "$backend survived $termination; pid=${process.pid()}\n${Files.readString(stderr)}", + ).isTrue() assertThat(process.exitValue()).isEqualTo(if (shutdown) 0 else 1) assertThat(Files.readString(stderr)).doesNotContain("falling back to mock") + assertThat(Files.readString(stderr)).contains("backend: ${AdapterBackend.fromSetting(backend).displayName}") } finally { - // Only after the exit assertion: never count forcibly reaping a leaked child as success. + // Only after the exit assertion: never count forcibly reaping a leaked child as + // success. if (process.isAlive) process.destroyForcibly() check(process.waitFor(10, SECONDS)) { "Could not reap test server ${process.pid()}" } process.outputStream.close() @@ -136,7 +148,7 @@ class LspProcessLifecycleTest { companion object { @JvmStatic fun terminations(): List = - listOf("treesitter", "mock").flatMap { backend -> + listOf("treesitter", "compiler", "mock").flatMap { backend -> Termination.entries.map { Arguments.of(backend, it) } } } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt new file mode 100644 index 00000000000..d247e30a605 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt @@ -0,0 +1,114 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.MonikerKind +import org.eclipse.lsp4j.MonikerParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.UniquenessLevel +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.SymbolMoniker +import org.xvm.lsp.adapter.mock.MockAdapter +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.TimeUnit.SECONDS + +class MonikerProtocolTest { + @Test + fun `monikers use protocol kinds scheme uniqueness and optional partial results`() { + val values = SymbolMoniker.Kind.entries.map { SymbolMoniker("test", it.name, it) } + val adapter = + object : Adapter by MockAdapter() { + override fun findMonikersAsync( + uri: String, + line: Int, + column: Int, + ) = CompletableFuture.completedFuture(values) + } + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client).notifyProgress(any()) + XtcLanguageServer(adapter).use { server -> + server.connect(client) + server.initialize(editorInitializeParams()).join() + val params = MonikerParams(TextDocumentIdentifier(URI), Position(0, 0)) + val expected = server.textDocumentService.moniker(params).get(5, SECONDS) + assertThat(expected.map { it.kind }).containsExactly(MonikerKind.Import, MonikerKind.Export, MonikerKind.Local) + assertThat(expected.map { it.unique }).containsOnly(UniquenessLevel.Scheme) + params.partialResultToken = Either.forLeft("monikers") + assertThat(server.textDocumentService.moniker(params).get(5, SECONDS)).isEmpty() + assertThat(events.single().token).isEqualTo(params.partialResultToken) + assertThat(events.single().value.right as List<*>).containsExactlyElementsOf(expected) + } + } + + @Test + fun `edited and canceled moniker requests retire their backend and publish no partial IDs`() { + listOf(false, true).forEach { cancel -> + val entered = CompletableFuture() + val pending = CompletableFuture>() + val adapter = + object : Adapter by MockAdapter() { + override fun findMonikersAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture> { + entered.complete(Unit) + return pending + } + } + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client).notifyProgress(any()) + XtcLanguageServer(adapter).use { server -> + server.connect(client) + server.initialize(editorInitializeParams()).join() + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, "module App {}"))) + val params = + MonikerParams(TextDocumentIdentifier(URI), Position(0, 7)).apply { + partialResultToken = Either.forLeft("monikers") + } + val result = documents.moniker(params) + entered.get(5, SECONDS) + if (cancel) { + result.cancel(false) + } else { + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, 2), + listOf(TextDocumentContentChangeEvent("module Changed {}")), + ), + ) + } + assertThatThrownBy { result.get(5, SECONDS) }.isInstanceOf(Exception::class.java) + pending.complete(listOf(SymbolMoniker("test", "stale", SymbolMoniker.Kind.EXPORT))) + assertThat(events).isEmpty() + } + } + } + + companion object { + private const val URI = "file:///App.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt new file mode 100644 index 00000000000..e02babc7aec --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt @@ -0,0 +1,168 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.WorkspaceSymbolParams +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.Location +import org.xvm.lsp.model.SymbolInfo +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class PartialResultsTest { + @Test + fun `partial batches precede an empty final response and preserve every item once`() { + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client) + .notifyProgress(any()) + PartialResults({ client }).use { publisher -> + val values = (0..129).toList() + val source = CompletableFuture>() + val token = Either.forRight(12) + val result = publisher.publish(token, source, {}, PartialResults::list) + source.complete(values) + assertThat(result.get(5, SECONDS)).isEmpty() + assertThat(events.map { it.token }).containsOnly(token) + assertThat(events.map { (it.value.right as List<*>).size }).containsExactly(64, 64, 2) + assertThat(events.flatMap { it.value.right as List<*> }) + .containsExactlyElementsOf(values) + val normal = CompletableFuture.completedFuture(values) + assertThat( + publisher.publish( + null, + normal, + { error("No partial publication") }, + PartialResults::list, + ), + ).isSameAs(normal) + } + } + + @Test + fun `canceled stale and disconnected requests stop after the in flight batch`() { + listOf("cancel", "stale", "close").forEach { mode -> + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + val entered = CompletableFuture() + val release = CompletableFuture() + val current = AtomicBoolean(true) + val dispatcher = Executors.newSingleThreadExecutor() + doAnswer { + events.add(it.getArgument(0)) + entered.complete(null) + release.get(5, SECONDS) + null + }.`when`(client) + .notifyProgress(any()) + PartialResults({ client }, dispatcher).use { publisher -> + val result = + publisher.publish( + Either.forLeft("partial"), + CompletableFuture.completedFuture((0..129).toList()), + { check(current.get()) { "Snapshot changed" } }, + PartialResults::list, + ) + try { + entered.get(5, SECONDS) + when (mode) { + "cancel" -> result.cancel(false) + "stale" -> current.set(false) + "close" -> publisher.close() + } + } finally { + release.complete(null) + } + assertThatThrownBy { result.get(5, SECONDS) }.isInstanceOf(Exception::class.java) + if (mode != "close") { + dispatcher.submit {}.get(5, SECONDS) + } else { + assertThat(dispatcher.awaitTermination(5, SECONDS)).isTrue() + } + assertThat(events).hasSize(1) + } + } + } + + @Test + fun `failed backend requests publish no partial payload`() { + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client) + .notifyProgress(any()) + PartialResults({ client }).use { publisher -> + val result = + publisher.publish( + Either.forLeft("failed"), + CompletableFuture.failedFuture>(IllegalStateException("failed")), + {}, + PartialResults::list, + ) + assertThatThrownBy { result.get(5, SECONDS) }.hasMessageContaining("failed") + assertThat(events).isEmpty() + } + } + + @Test + fun `workspace symbol endpoint uses protocol lists and keeps normal results when no token is supplied`() { + val symbols = + (0..129).map { + SymbolInfo.of( + "Value$it", + SymbolInfo.SymbolKind.CLASS, + Location.of("file:///Values.x", it, 0), + ) + } + val adapter = + object : Adapter by MockAdapter() { + override fun findWorkspaceSymbols(query: String) = symbols + } + val client = mock(LanguageClient::class.java) + val events = CopyOnWriteArrayList() + doAnswer { + events.add(it.getArgument(0)) + null + }.`when`(client) + .notifyProgress(any()) + XtcLanguageServer(adapter).use { server -> + server.connect(client) + server.initialize(editorInitializeParams()).join() + val query = + WorkspaceSymbolParams("Value").apply { + partialResultToken = Either.forLeft("symbols") + } + assertThat( + server.workspaceService + .symbol(query) + .get(5, SECONDS) + .left, + ).isEmpty() + assertThat(events.flatMap { it.value.right as List<*> }).hasSize(130) + query.partialResultToken = null + assertThat( + server.workspaceService + .symbol(query) + .get(5, SECONDS) + .left, + ).hasSize(130) + assertThat(events).hasSize(3) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt new file mode 100644 index 00000000000..e18ff727c47 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt @@ -0,0 +1,21 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.xvm.lsp.util.ProgressLabels + +class ProgressLabelsTest { + @Test + fun `source labels retain useful paths and select the most specific workspace root`() { + assertThat(ProgressLabels.source("file:///code/platform/src/Consumer.x", listOf("file:///code/", "file:///code/platform/"))) + .isEqualTo("src/Consumer.x") + assertThat(ProgressLabels.source("file:///code/My%20App/Library.x")) + .isEqualTo("My App/Library.x") + assertThat(ProgressLabels.source("untitled:Scratch.x")) + .isEqualTo("untitled:Scratch.x") + assertThat(ProgressLabels.source("file:///code/Consumer%0A.x")) + .isEqualTo("code/Consumer .x") + assertThat(ProgressLabels.source("invalid path with spaces")) + .isEqualTo("invalid path with spaces") + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt new file mode 100644 index 00000000000..144b7677eaa --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt @@ -0,0 +1,112 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.jsonrpc.MessageConsumer +import org.eclipse.lsp4j.jsonrpc.messages.Message +import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage +import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage +import org.junit.jupiter.api.Test + +class ProtocolLifecycleTest { + @Test + fun `failed initialization can retry and late client replies survive shutdown`() { + val delivered = mutableListOf() + val lifecycle = ProtocolLifecycle() + val outgoing = lifecycle.wrap(MessageConsumer {}, false) + val incoming = lifecycle.wrap(MessageConsumer(delivered::add), true) + + fun initialize(id: String) = + incoming.consume( + RequestMessage().apply { + setId(id) + method = "initialize" + }, + ) + initialize("failed") + outgoing.consume( + ResponseMessage().apply { + setId("failed") + error = ResponseError(ResponseErrorCode.InvalidParams, "Invalid initialization", null) + }, + ) + initialize("retry") + outgoing.consume( + ResponseMessage().apply { + setId("retry") + result = emptyMap() + }, + ) + incoming.consume( + RequestMessage().apply { + setId("shutdown") + method = "shutdown" + }, + ) + val reply = + ResponseMessage().apply { + setId("pending-configuration") + result = emptyList() + } + incoming.consume(reply) + assertThat(delivered.filterIsInstance().map { it.id }).containsExactly("failed", "retry", "shutdown") + assertThat(delivered.last()).isSameAs(reply) + } + + @Test + fun `handshake gates requests notifications duplicate initialization and shutdown`() { + val delivered = mutableListOf() + val written = mutableListOf() + val lifecycle = ProtocolLifecycle() + val outgoing = lifecycle.wrap(MessageConsumer { written.add(it) }, false) + val incoming = lifecycle.wrap(MessageConsumer { delivered.add(it) }, true) + + fun request( + id: Int, + method: String, + ) = incoming.consume( + RequestMessage().apply { + setId(id) + this.method = method + }, + ) + + fun notify(method: String) = incoming.consume(NotificationMessage().apply { this.method = method }) + + notify("textDocument/didOpen") + request(1, "textDocument/hover") + assertThat(delivered).isEmpty() + assertThat((written.last() as ResponseMessage).error.code) + .isEqualTo(ResponseErrorCode.ServerNotInitialized.value) + request(2, "initialize") + request(3, "textDocument/hover") + assertThat(delivered).hasSize(1) + outgoing.consume( + ResponseMessage().apply { + setId(2) + result = emptyMap() + }, + ) + notify("initialized") + notify("initialized") + request(4, "initialize") + assertThat((written.last() as ResponseMessage).error.code) + .isEqualTo(ResponseErrorCode.InvalidRequest.value) + request(5, "textDocument/hover") + request(6, "shutdown") + request(7, "textDocument/hover") + notify("textDocument/didChange") + notify("exit") + assertThat( + delivered.map { + when (it) { + is RequestMessage -> it.method + is NotificationMessage -> it.method + else -> "response" + } + }, + ).containsExactly("initialize", "initialized", "textDocument/hover", "shutdown", "exit") + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt new file mode 100644 index 00000000000..d14cefd0d14 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt @@ -0,0 +1,86 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.HoverParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.junit.jupiter.api.Test +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.CompilationResult +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicInteger + +class RequestOwnershipTest { + @Test + fun `canceling a request awaiting analysis does not cancel another reader or shared analysis`() { + val analysis = CompletableFuture() + val queries = AtomicInteger() + val adapter = + object : Adapter by MockAdapter() { + override fun compileAsync( + uri: String, + content: String, + ) = analysis + + override fun getHoverInfo( + uri: String, + line: Int, + column: Int, + ): String { + queries.incrementAndGet() + return "Int" + } + } + XtcLanguageServer(adapter).use { server -> + val uri = "file:///Owned.x" + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, "module Owned {}")), + ) + val params = HoverParams(TextDocumentIdentifier(uri), Position(0, 0)) + val canceled = server.textDocumentService.hover(params) + val surviving = server.textDocumentService.hover(params) + canceled.cancel(false) + assertThat(analysis.isDone).isFalse() + analysis.complete(MockAdapter().compile(uri, "module Owned {}")) + assertThat( + surviving + .get(5, SECONDS) + .contents.right.value, + ).isEqualTo("Int") + assertThat(queries.get()).isEqualTo(1) + assertThat(canceled.isCancelled).isTrue() + } + } + + @Test + fun `close immediately retires a reader even when analysis ignores cancellation`() { + val analysis = + object : CompletableFuture() { + override fun cancel(mayInterruptIfRunning: Boolean) = false + } + val adapter = + object : Adapter by MockAdapter() { + override fun compileAsync( + uri: String, + content: String, + ) = analysis + } + XtcLanguageServer(adapter).use { server -> + val uri = "file:///Owned.x" + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, "module Owned {}")), + ) + val reader = + server.textDocumentService.hover( + HoverParams(TextDocumentIdentifier(uri), Position(0, 0)), + ) + server.close() + assertThat(reader.isCompletedExceptionally).isTrue() + assertThat(analysis.isDone).isFalse() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt new file mode 100644 index 00000000000..9dc055c6db8 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt @@ -0,0 +1,29 @@ +package org.xvm.lsp.server + +import com.google.gson.JsonPrimitive +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test + +class ResolveReportsTest { + @Test + fun `handles preserve detached values and reject foreign stale tampered and evicted requests`() { + val reports = ResolveReports(limit = 1, characterLimit = 20) + val handle = reports.remember(7, "entry", "payload", 7) + assertThat(reports.resolve(JsonPrimitive(handle), 7, "entry")).isEqualTo("payload") + listOf( + { reports.resolve(handle, 8, "entry") }, + { reports.resolve(handle, 7, "other") }, + { ResolveReports().resolve(handle, 7, "entry") }, + { reports.resolve(mapOf("id" to handle), 7, "entry") }, + ).forEach { request -> + assertThatThrownBy { request() }.hasMessageContaining("expired or changed") + } + assertThat(reports.remember(7, "large", "oversized", 21)).isNull() + assertThat(reports.resolve(handle, 7, "entry")).isEqualTo("payload") + reports.remember(7, "next", "replacement", 11) + assertThatThrownBy { reports.resolve(handle, 7, "entry") } + .hasMessageContaining("expired or changed") + reports.clear() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt new file mode 100644 index 00000000000..530098b0f39 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt @@ -0,0 +1,232 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DidChangeWatchedFilesRegistrationOptions +import org.eclipse.lsp4j.RegistrationParams +import org.eclipse.lsp4j.UnregistrationParams +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +class ResourceFileWatchersTest { + @TempDir lateinit var directory: Path + + @Test + fun `failed removals are retried without reusing the retired registration`() { + val client = mock(LanguageClient::class.java) + val removals = CopyOnWriteArrayList() + doAnswer { CompletableFuture.completedFuture(null) } + .`when`(client) + .registerCapability(any()) + doAnswer { + removals += it.getArgument(0) + if (removals.size == 1) { + CompletableFuture.failedFuture(IllegalStateException("not removed")) + } else { + CompletableFuture.completedFuture(null) + } + }.`when`(client) + .unregisterCapability(any()) + ResourceFileWatchers().use { watchers -> + val root = setOf("file:///external/a/") + val first = watchers.update(client, root).get(5, SECONDS) + assertThat(watchers.update(client, emptySet()).get(5, SECONDS)).isEmpty() + val next = watchers.update(client, root).get(5, SECONDS) + assertThat(next.values).doesNotContainAnyElementsOf(first.values) + assertThat(removals).hasSize(2) + assertThat(removals.last().unregisterations.map { it.id }) + .containsExactlyElementsOf(first.values) + watchers.update(client, root).get(5, SECONDS) + assertThat(removals).hasSize(2) + } + } + + @Test + fun `stalled registration releases queue and late success removes only abandoned IDs`() { + val client = mock(LanguageClient::class.java) + val registered = LinkedBlockingQueue() + val removed = LinkedBlockingQueue() + val replies = LinkedBlockingQueue>() + val deadlines = LinkedBlockingQueue>() + doAnswer { + registered.add(it.getArgument(0)) + CompletableFuture().also(replies::add) + }.`when`(client) + .registerCapability(any()) + doAnswer { + removed.add(it.getArgument(0)) + CompletableFuture.completedFuture(null) + }.`when`(client) + .unregisterCapability(any()) + ResourceFileWatchers { CompletableFuture().also(deadlines::add) } + .use { watchers -> + val first = watchers.update(client, setOf("file:///external/a/")) + val oldId = + registered + .poll(5, SECONDS) + .registrations + .single() + .id + val lateReply = replies.poll(5, SECONDS) + deadlines.poll(5, SECONDS).complete(null) + assertThat(first.get(5, SECONDS)).isEmpty() + val second = watchers.update(client, setOf("file:///external/a/")) + val newId = + registered + .poll(5, SECONDS) + .registrations + .single() + .id + replies.poll(5, SECONDS).complete(null) + assertThat(second.get(5, SECONDS).values).containsExactly(newId) + lateReply.complete(null) + assertThat(removed.poll(5, SECONDS).unregisterations.map { it.id }) + .containsExactly(oldId) + assertThat(newId).isNotEqualTo(oldId) + assertThat( + watchers.update(client, setOf("file:///external/a/")).get(5, SECONDS).values, + ).containsExactly(newId) + } + } + + @Test + fun `stalled removal never lets subsequent roots reuse an ID being removed`() { + val client = mock(LanguageClient::class.java) + val deadlines = LinkedBlockingQueue>() + val removal = CompletableFuture() + doAnswer { CompletableFuture.completedFuture(null) } + .`when`(client) + .registerCapability(any()) + doAnswer { removal }.`when`(client).unregisterCapability(any()) + ResourceFileWatchers { CompletableFuture().also(deadlines::add) } + .use { watchers -> + val root = setOf("file:///external/a/") + val first = watchers.update(client, root).get(5, SECONDS) + deadlines.poll(5, SECONDS) // completed registration's cancelled timer + val empty = watchers.update(client, emptySet()) + deadlines.poll(5, SECONDS).complete(null) + assertThat(empty.get(5, SECONDS)).isEmpty() + val replacing = watchers.update(client, root) + deadlines.poll(5, SECONDS) // replacement registration's cancelled timer + deadlines.poll(5, SECONDS).complete(null) // retire the unanswered removal retry + val second = replacing.get(5, SECONDS) + assertThat(second.values).doesNotContainAnyElementsOf(first.values) + removal.complete(null) + assertThat(watchers.update(client, root).get(5, SECONDS)).isEqualTo(second) + } + } + + @Test + fun `disconnect releases outstanding and queued watcher updates`() { + val client = mock(LanguageClient::class.java) + val started = CompletableFuture() + doAnswer { + started.complete(null) + CompletableFuture() + }.`when`(client) + .registerCapability(any()) + val watchers = ResourceFileWatchers() + val first = watchers.update(client, setOf("file:///external/a/")) + val second = watchers.update(client, setOf("file:///external/b/")) + started.get(5, SECONDS) + watchers.close() + assertThat(first.get(5, SECONDS)).isEmpty() + assertThat(second.get(5, SECONDS)).isEmpty() + assertThat(watchers.update(client, setOf("file:///external/c/")).join()).isEmpty() + } + + @Test + fun `missing roots watch only their next child and replace plans as directories appear`() { + val client = mock(LanguageClient::class.java) + val registrations = CopyOnWriteArrayList() + val removals = CopyOnWriteArrayList() + doAnswer { + registrations += it.getArgument(0) + CompletableFuture.completedFuture(null) + }.`when`(client) + .registerCapability(any()) + doAnswer { + removals += it.getArgument(0) + CompletableFuture.completedFuture(null) + }.`when`(client) + .unregisterCapability(any()) + val watchers = ResourceFileWatchers() + val root = directory.resolve("generated/assets") + val uri = root.toUri().toString() + watchers.update(client, setOf(uri)).get(10, SECONDS) + + fun patterns() = + ( + registrations + .last() + .registrations + .single() + .registerOptions + as DidChangeWatchedFilesRegistrationOptions + ).watchers + .map { it.globPattern.right } + assertThat(patterns().map { it.baseUri.right to it.pattern }) + .containsExactly( + uri to "**/*", + directory.toUri().toString() to "generated", + ) + Files.createDirectory(root.parent) + watchers.update(client, setOf(uri)).get(10, SECONDS) + assertThat(patterns().last().baseUri.right).isEqualTo(root.parent.toUri().toString()) + assertThat(patterns().last().pattern).isEqualTo("assets") + assertThat(removals).hasSize(1) + Files.createDirectory(root) + watchers.update(client, setOf(uri)).get(10, SECONDS) + assertThat(registrations).hasSize(2) + // Retain the parent subscription even while the root exists, for delete/recreate. + Files.delete(root) + watchers.update(client, setOf(uri)).get(10, SECONDS) + assertThat(registrations).hasSize(2) + } + + @Test + fun `external root registrations and removals remain ordered across delayed client replies`() { + val client = mock(LanguageClient::class.java) + val registrations = CopyOnWriteArrayList() + val removals = CopyOnWriteArrayList() + val first = CompletableFuture() + doAnswer { + registrations += it.getArgument(0) + if (registrations.size == 1) { + first + } else { + CompletableFuture.completedFuture(null) + } + }.`when`(client) + .registerCapability(any()) + doAnswer { + removals += it.getArgument(0) + CompletableFuture.completedFuture(null) + }.`when`(client) + .unregisterCapability(any()) + val watchers = ResourceFileWatchers() + val a = watchers.update(client, setOf("file:///external/a/")) + val b = watchers.update(client, setOf("file:///external/b/")) + assertThat(a.isDone).isFalse() + assertThat(b.isDone).isFalse() + first.complete(null) + val firstIds = a.get(10, SECONDS) + val secondIds = b.get(10, SECONDS) + assertThat(secondIds.keys).containsExactly("file:///external/b/") + assertThat(removals.single().unregisterations.map { it.id }) + .containsExactly(firstIds.values.single()) + watchers.update(client, emptySet()).get(10, SECONDS) + assertThat(registrations).hasSize(2) + assertThat(removals.last().unregisterations.map { it.id }) + .containsExactly(secondIds.values.single()) + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt new file mode 100644 index 00000000000..40b12683aa1 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt @@ -0,0 +1,71 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.junit.jupiter.api.Test + +class SemanticTokenReportsTest { + @Test + fun `delta reconstructs insertions removals integer edits and unchanged results`() { + val reports = SemanticTokenReports() + val samples = + listOf( + emptyList(), + listOf(0, 2, 3, 1, 0), + listOf(1, 2, 3, 1, 0), + listOf(1, 2, 3, 1, 0, 0, 8, 4, 2, 0), + ) + samples.forEach { old -> + samples.forEach { next -> + val before = reports.full("file:///test.x", old) + val delta = reports.delta("file:///test.x", before.resultId, next).right + val applied = old.toMutableList() + delta.edits + .sortedByDescending { it.start } + .forEach { edit -> + applied.subList(edit.start, edit.start + edit.deleteCount).clear() + applied.addAll(edit.start, edit.data.orEmpty()) + } + assertThat(applied).isEqualTo(next) + if (old == next) assertThat(delta.edits).isEmpty() + } + } + } + + @Test + fun `unknown foreign closed and restarted ids fall back to full`() { + val reports = SemanticTokenReports() + val previous = reports.full("file:///test.x", listOf(0, 0, 1, 0, 0)) + assertThat(reports.delta("file:///other.x", previous.resultId, emptyList()).isLeft).isTrue() + assertThat( + SemanticTokenReports() + .delta("file:///test.x", previous.resultId, emptyList()) + .isLeft, + ).isTrue() + reports.retire("file:/test.x") + assertThat(reports.delta("file:///test.x", previous.resultId, emptyList()).isLeft).isTrue() + } + + @Test + fun `history limits bound both report count and total token memory`() { + listOf(SemanticTokenReports(maximumReports = 1), SemanticTokenReports(maximumIntegers = 5)) + .forEach { reports -> + val first = reports.full("file:///test.x", listOf(0, 0, 1, 0, 0)) + reports.full("file:///test.x", listOf(1, 0, 1, 0, 0)) + assertThat(reports.delta("file:///test.x", first.resultId, emptyList()).isLeft) + .isTrue() + } + } + + @Test + fun `range reencodes from document origin and includes intersecting UTF16 tokens`() { + val full = listOf(2, 5, 3, 0, 1, 0, 5, 4, 1, 0, 3, 2, 7, 2, 0) + assertThat(SemanticTokenReports.range(full, Range(Position(2, 8), Position(5, 3))).data) + .containsExactly(2, 10, 4, 1, 0, 3, 2, 7, 2, 0) + assertThat(SemanticTokenReports.range(full, Range(Position(2, 6), Position(2, 7))).data) + .containsExactly(2, 5, 3, 0, 1) + assertThat(SemanticTokenReports.range(full, Range(Position(2, 6), Position(2, 6))).data) + .isEmpty() + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SourceRootResolverTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SourceRootResolverTest.kt index ea1314795ae..86837564fd3 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SourceRootResolverTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SourceRootResolverTest.kt @@ -13,15 +13,10 @@ import java.nio.file.Path @DisplayName("SourceRootResolver") class SourceRootResolverTest { - @TempDir - lateinit var tmp: Path + @TempDir lateinit var tmp: Path /** Create a real directory under [tmp] so the existence-check passes. */ - private fun realDir(name: String): String = - tmp - .resolve(name) - .also { it.toFile().mkdirs() } - .toString() + private fun realDir(name: String): String = tmp.resolve(name).also { it.toFile().mkdirs() }.toString() private fun pathList(vararg paths: String): String = paths.joinToString(File.pathSeparator) @@ -56,20 +51,23 @@ class SourceRootResolverTest { @DisplayName("should ignore non-array value at xtcSourceRoots") fun shouldIgnoreNonArrayValue() { val opts = mapOf("xtcSourceRoots" to "not-a-list") - assertThat(SourceRootResolver.resolve(opts, systemProperty = null, envVar = null)).isEmpty() + assertThat(SourceRootResolver.resolve(opts, systemProperty = null, envVar = null)) + .isEmpty() } @Test @DisplayName("should return empty for null init options") fun shouldReturnEmptyForNull() { - assertThat(SourceRootResolver.resolve(null, systemProperty = null, envVar = null)).isEmpty() + assertThat(SourceRootResolver.resolve(null, systemProperty = null, envVar = null)) + .isEmpty() } @Test @DisplayName("should return empty when key absent") fun shouldReturnEmptyWhenKeyAbsent() { val opts = mapOf("otherKey" to listOf("/some/path")) - assertThat(SourceRootResolver.resolve(opts, systemProperty = null, envVar = null)).isEmpty() + assertThat(SourceRootResolver.resolve(opts, systemProperty = null, envVar = null)) + .isEmpty() } } @@ -82,7 +80,8 @@ class SourceRootResolverTest { val a = realDir("a") val b = realDir("b") - val roots = SourceRootResolver.resolve(null, systemProperty = pathList(a, b), envVar = null) + val roots = + SourceRootResolver.resolve(null, systemProperty = pathList(a, b), envVar = null) assertThat(roots).containsExactly(a, b) } @@ -92,7 +91,12 @@ class SourceRootResolverTest { fun shouldIgnoreBlankEntries() { val a = realDir("a") - val roots = SourceRootResolver.resolve(null, systemProperty = "${File.pathSeparator}$a${File.pathSeparator}", envVar = null) + val roots = + SourceRootResolver.resolve( + null, + systemProperty = "${File.pathSeparator}$a${File.pathSeparator}", + envVar = null, + ) assertThat(roots).containsExactly(a) } @@ -121,7 +125,8 @@ class SourceRootResolverTest { val c = realDir("c") val opts = mapOf("xtcSourceRoots" to listOf(a, b)) - val roots = SourceRootResolver.resolve(opts, systemProperty = pathList(b, c), envVar = a) + val roots = + SourceRootResolver.resolve(opts, systemProperty = pathList(b, c), envVar = a) // a, b from init; c new from sysprop; b duplicate; a duplicate from env assertThat(roots).containsExactly(a, b, c) @@ -142,7 +147,8 @@ class SourceRootResolverTest { @Test @DisplayName("should return empty when all sources are null/empty") fun shouldReturnEmptyForAllNull() { - assertThat(SourceRootResolver.resolve(null, systemProperty = null, envVar = null)).isEmpty() + assertThat(SourceRootResolver.resolve(null, systemProperty = null, envVar = null)) + .isEmpty() assertThat(SourceRootResolver.resolve(null, systemProperty = "", envVar = "")).isEmpty() } } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt new file mode 100644 index 00000000000..d5e9d7d6ad3 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt @@ -0,0 +1,50 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DidChangeWatchedFilesCapabilities +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.RegistrationParams +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.mock +import org.mockito.Mockito.never +import org.mockito.Mockito.times +import org.mockito.Mockito.verify +import org.mockito.Mockito.`when` +import org.xvm.lsp.adapter.mock.MockAdapter +import java.util.concurrent.CompletableFuture + +class StartupRegistrationTest { + @Test + fun `watcher requests wait for initialized and require negotiated dynamic registration`() { + listOf(false, true).forEach { supported -> + val client = mock(LanguageClient::class.java) + `when`(client.registerCapability(any())) + .thenReturn(CompletableFuture.completedFuture(null)) + XtcLanguageServer(MockAdapter()).use { server -> + server.connect(client) + val params = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + workspace = + WorkspaceClientCapabilities().apply { + didChangeWatchedFiles = + DidChangeWatchedFilesCapabilities(supported) + } + } + } + assertThat(server.initialize(params).join().capabilities).isNotNull() + verify(client, never()).registerCapability(any(RegistrationParams::class.java)) + server.initialized(InitializedParams()) + server.initialized(InitializedParams()) + verify(client, times(if (supported) 1 else 0)) + .registerCapability(any(RegistrationParams::class.java)) + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt new file mode 100644 index 00000000000..af0b30d959d --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt @@ -0,0 +1,183 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.TypeHierarchyPrepareParams +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.TypeHierarchyItem +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.CompilationResult +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CountDownLatch +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS + +class XdkCallbackServerTest { + enum class Callback { + ANALYSIS, + QUERY, + } + + @Test + fun `transport cancellation returns while a navigation request holds the document lock`() { + val navigationEntered = CountDownLatch(1) + val release = CountDownLatch(1) + val pending = CompletableFuture>() + val backend = + object : Adapter by MockAdapter() { + override fun getCompletionsAsync( + uri: String, + line: Int, + column: Int, + triggerCharacter: String?, + ) = pending + + override fun prepareTypeHierarchy( + uri: String, + line: Int, + column: Int, + ): List { + navigationEntered.countDown() + check(release.await(10, SECONDS)) + return emptyList() + } + } + val server = XtcLanguageServer(backend) + server.connect(mock(LanguageClient::class.java)) + val documents = server.textDocumentService + val document = TextDocumentIdentifier("file:///Healthy.x") + try { + documents.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem(document.uri, "xtc", 1, "module Healthy {}"), + ), + ) + val response = documents.completion(CompletionParams(document, Position(0, 0))) + await().atMost(10, SECONDS).until { pending.numberOfDependents > 0 } + val navigation = + documents.prepareTypeHierarchy(TypeHierarchyPrepareParams(document, Position(0, 0))) + assertThat(navigationEntered.await(10, SECONDS)).isTrue() + // LSP4J cancels while holding its request-map lock. Completion of a different + // response can need that lock, so cancellation must return without waiting here. + val canceled = CompletableFuture.supplyAsync { response.cancel(false) } + assertThat(canceled.get(3, SECONDS)).isTrue() + release.countDown() + assertThat(navigation.get(5, SECONDS)).isEmpty() + await().atMost(5, SECONDS).until { pending.isCancelled } + } finally { + release.countDown() + server.shutdown().get(10, SECONDS) + } + } + + @ParameterizedTest + @EnumSource(Callback::class) + fun `compiler completion releases its worker before publishing under the document lock`(callback: Callback) { + val release = CountDownLatch(1) + val navigationEntered = CountDownLatch(1) + val callbackQueued = CompletableFuture>() + val uri = "file:///Healthy.x" + val source = "module Healthy {}" + Executors.newSingleThreadExecutor().use { worker -> + val delegate = MockAdapter() + val backend = + object : Adapter by delegate { + override fun compileAsync( + uri: String, + content: String, + ): CompletableFuture = + if (uri.endsWith("Neighbor.x")) { + CompletableFuture + .supplyAsync( + { + check(release.await(10, SECONDS)) + delegate.compile(uri, content) + }, + worker, + ).also { callbackQueued.complete(it) } + } else { + CompletableFuture.completedFuture(delegate.compile(uri, content)) + } + + override fun getCompletionsAsync( + uri: String, + line: Int, + column: Int, + triggerCharacter: String?, + ): CompletableFuture> = + CompletableFuture + .supplyAsync( + { + check(release.await(10, SECONDS)) + emptyList() + }, + worker, + ).also { callbackQueued.complete(it) } + + override fun prepareTypeHierarchy( + uri: String, + line: Int, + column: Int, + ): List { + navigationEntered.countDown() + // Model the compiler adapter's synchronous bridge onto its serial worker. + // The bound also releases the lock if this regression reintroduces the + // cycle. + return worker + .submit> { emptyList() } + .get(5, SECONDS) + } + } + val server = XtcLanguageServer(backend) + server.connect(mock(LanguageClient::class.java)) + val documents = server.textDocumentService + try { + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source)), + ) + when (callback) { + Callback.ANALYSIS -> { + documents.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem("file:///Neighbor.x", "xtc", 1, source), + ), + ) + } + + Callback.QUERY -> { + documents.completion( + CompletionParams(TextDocumentIdentifier(uri), Position(0, 0)), + ) + } + } + val pending = callbackQueued.get(10, SECONDS) + await().atMost(10, SECONDS).until { pending.numberOfDependents > 0 } + // A barrier behind the pending work proves that its callback precedes navigation. + val queued = CountDownLatch(1) + worker.execute { queued.countDown() } + val result = + documents.prepareTypeHierarchy( + TypeHierarchyPrepareParams(TextDocumentIdentifier(uri), Position(0, 0)), + ) + assertThat(navigationEntered.await(10, SECONDS)).isTrue() + release.countDown() + assertThat(queued.await(3, SECONDS)).isTrue() + assertThat(result.get(5, SECONDS)).isEmpty() + } finally { + release.countDown() + server.shutdown().get(10, SECONDS) + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt new file mode 100644 index 00000000000..85cd8ccf1be --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt @@ -0,0 +1,373 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DidSaveTextDocumentParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.ReferenceContext +import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.SignatureHelpParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.EnumSource +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.CompletionItem +import org.xvm.lsp.adapter.ParameterInfo +import org.xvm.lsp.adapter.SignatureHelp +import org.xvm.lsp.adapter.SignatureInfo +import org.xvm.lsp.adapter.WorkspaceEdit +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Location +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.ExecutionException +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicInteger + +class XdkCursorServerTest { + enum class Feature { + COMPLETION, + SIGNATURE, + RENAME, + REFERENCES, + } + + private class Pending( + val future: CompletableFuture<*>, + val finish: () -> Unit, + ) + + private class Backend( + val gate: CompletableFuture = CompletableFuture.completedFuture(Unit), + val ignoreCancellation: Boolean = false, + val module: Boolean = false, + private val delegate: MockAdapter = MockAdapter(), + ) : Adapter by delegate { + val pending = LinkedBlockingQueue() + val cancellations = AtomicInteger() + val canceled = LinkedBlockingQueue() + val analyses = CopyOnWriteArrayList>() + + override fun analysisScope(uri: String): String = if (module) "module" else uri + + override fun compileAsync( + uri: String, + content: String, + ): CompletableFuture = gate.thenApply { delegate.compile(uri, content) }.also { analyses.add(it) } + + private fun query(value: T): CompletableFuture { + val result = + object : CompletableFuture() { + override fun cancel(mayInterruptIfRunning: Boolean): Boolean { + cancellations.incrementAndGet() + val changed = !ignoreCancellation && super.cancel(mayInterruptIfRunning) + canceled.add(changed) + return changed + } + } + pending.add(Pending(result) { result.complete(value) }) + return result + } + + override fun getCompletionsAsync( + uri: String, + line: Int, + column: Int, + triggerCharacter: String?, + ): CompletableFuture> = + query( + listOf( + CompletionItem( + "member", + CompletionItem.CompletionKind.PROPERTY, + "Int member", + "member", + ), + ), + ) + + override fun getSignatureHelpAsync( + uri: String, + line: Int, + column: Int, + ): CompletableFuture = + query( + SignatureHelp( + listOf( + SignatureInfo( + "Int call(Int a, Int b)", + parameters = listOf(ParameterInfo("Int a"), ParameterInfo("Int b")), + activeParameter = 1, + ), + ), + activeParameter = 1, + ), + ) + + fun next(): Pending = checkNotNull(pending.poll(10, SECONDS)) { "query did not reach backend" } + + override fun renameAsync( + uri: String, + line: Int, + column: Int, + newName: String, + ): CompletableFuture = query(WorkspaceEdit(emptyMap())) + + override fun findReferencesAsync( + uri: String, + line: Int, + column: Int, + includeDeclaration: Boolean, + ): CompletableFuture> = query(listOf(Location(uri, line, column, line, column + 1))) + } + + private class Session( + val backend: Backend, + ) : AutoCloseable { + val server = XtcLanguageServer(backend) + val documents = server.textDocumentService + + init { + server.connect(mock(LanguageClient::class.java)) + } + + fun open(uri: String = URI) = + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, "module Editing {}")), + ) + + fun change(uri: String = URI) = + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(TextDocumentContentChangeEvent("module Changed {}")), + ), + ) + + fun request( + feature: Feature, + uri: String = URI, + ): CompletableFuture<*> = + when (feature) { + Feature.COMPLETION -> { + documents.completion( + CompletionParams(TextDocumentIdentifier(uri), Position(0, 0)), + ) + } + + Feature.SIGNATURE -> { + documents.signatureHelp( + SignatureHelpParams(TextDocumentIdentifier(uri), Position(0, 0)), + ) + } + + Feature.RENAME -> { + documents.rename( + RenameParams(TextDocumentIdentifier(uri), Position(0, 0), "renamed"), + ) + } + + Feature.REFERENCES -> { + documents.references( + ReferenceParams( + TextDocumentIdentifier(uri), + Position(0, 0), + ReferenceContext(true), + ), + ) + } + } + + override fun close() { + server.shutdown().get(10, SECONDS) + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `filesystem notifications for an open overlay preserve pending queries`(feature: Feature) { + val backend = Backend() + Session(backend).use { session -> + session.open() + val response = session.request(feature) + val work = backend.next() + // Created, changed and deleted disk files are all masked by the open buffer. + for (kind in FileChangeType.values()) { + session.server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams(listOf(FileEvent(URI, kind))), + ) + } + session.documents.didSave(DidSaveTextDocumentParams(TextDocumentIdentifier(URI))) + assertThat(backend.analyses).hasSize(1) + assertThat(work.future.isCancelled).isFalse() + work.finish() + assertThat(response.get(10, SECONDS)).isNotNull() + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `client cancellation reaches the cursor backend without canceling shared analysis`(feature: Feature) { + val backend = Backend() + Session(backend).use { session -> + session.open() + val response = session.request(feature) + val work = backend.next() + assertThat(response.cancel(false)).isTrue() + assertThat(backend.canceled.poll(10, SECONDS)).isTrue() + assertThat(work.future.isCancelled).isTrue() + assertThat(backend.analyses.single().isCancelled).isFalse() + val latest = session.request(feature) + backend.next().finish() + assertThat(latest.get(10, SECONDS)).isNotNull() + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `cancellation while waiting for analysis does not start cursor work`(feature: Feature) { + val backend = Backend(gate = CompletableFuture()) + Session(backend).use { session -> + session.open() + val canceled = session.request(feature) + assertThat(canceled.cancel(false)).isTrue() + assertThat(backend.analyses.single().isCancelled).isFalse() + backend.gate.complete(Unit) + val latest = session.request(feature) + backend.next().finish() + assertThat(latest.get(10, SECONDS)).isNotNull() + assertThat(backend.pending).isEmpty() + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `an edit rejects late facts even when the backend ignores cancellation`(feature: Feature) { + val backend = Backend(ignoreCancellation = true) + Session(backend).use { session -> + session.open() + val old = session.request(feature) + val work = backend.next() + session.change() + assertModified(old) + work.finish() + assertModified(old) + assertThat(backend.canceled.poll(10, SECONDS)).isFalse() + assertThat(backend.cancellations.get()).isPositive() + } + } + + @ParameterizedTest + @EnumSource(Feature::class) + fun `a module edit invalidates member queries without changing their document version`(feature: Feature) { + val backend = Backend(ignoreCancellation = true, module = true) + Session(backend).use { session -> + session.open() + val member = "file:///Editing/Child.x" + session.open(member) + val old = session.request(feature, member) + val work = backend.next() + session.change() + work.finish() + assertModified(old) + } + } + + @Test + fun `cross-module queries reject changed consumers even when the backend ignores cancellation`() { + for (feature in listOf(Feature.REFERENCES, Feature.RENAME)) { + val backend = Backend(ignoreCancellation = true) + Session(backend).use { session -> + session.open() + val consumer = "file:///Consumer.x" + session.open(consumer) + val old = session.request(feature) + val work = backend.next() + session.change(consumer) + work.finish() + assertModified(old) + } + } + } + + @Test + fun `close and reopen with the same version cannot publish old cursor facts`() { + val backend = Backend(ignoreCancellation = true) + Session(backend).use { session -> + session.open() + val old = session.request(Feature.SIGNATURE) + val work = backend.next() + session.documents.didClose(DidCloseTextDocumentParams(TextDocumentIdentifier(URI))) + session.open() + work.finish() + assertModified(old) + val latest = session.request(Feature.SIGNATURE) + backend.next().finish() + assertThat(latest.get(10, SECONDS)).isNotNull() + } + } + + @Test + fun `shutdown retires cursor work even when the backend ignores cancellation`() { + val backend = Backend(ignoreCancellation = true) + Session(backend).use { session -> + session.open() + val old = session.request(Feature.COMPLETION) + val work = backend.next() + session.close() + work.finish() + assertModified(old) + } + } + + @Test + fun `signature conversion preserves per-signature parameter mapping`() { + val backend = Backend() + Session(backend).use { session -> + session.server.initialize(editorInitializeParams()).join() + session.open() + val response = + session.documents.signatureHelp( + SignatureHelpParams(TextDocumentIdentifier(URI), Position(0, 0)), + ) + backend.next().finish() + val help = response.get(10, SECONDS)!! + assertThat(help.activeParameter).isEqualTo(1) + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + assertThat( + help.signatures + .single() + .parameters + .map { it.label.left }, + ).containsExactly("Int a", "Int b") + } + } + + private fun assertModified(result: CompletableFuture<*>) { + val failure = assertThrows { result.get(10, SECONDS) } + val error = failure.cause as ResponseErrorException + assertThat(error.responseError.code).isEqualTo(ResponseErrorCode.ContentModified.value) + } + + private companion object { + const val URI = "file:///Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt new file mode 100644 index 00000000000..9c9d263fedc --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt @@ -0,0 +1,103 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.mockito.Mockito.mock +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.ExecutionException +import java.util.concurrent.TimeUnit.SECONDS + +class XdkCursorWatchTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `directory creation preserves an incomplete cursor only when effective inputs match`(changed: Boolean) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + val root = + directory.resolve("App.x").toFile().apply { writeText("module App { void run() {} }") } + val member = + directory.resolve("App/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box {}") + } + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val support = EmbeddingSupport.instance() + val adapter = + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + support.compileModule(sources, repository, errors) + }, + { source, _, cursor, repository, errors -> + entered.countDown() + check(release.await(20, SECONDS)) + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ) + val server = XtcLanguageServer(adapter) + server.connect(mock(LanguageClient::class.java)) + try { + val uri = root.toURI().toString() + val overlay = + "module App { void run() { String text = \"hello\"; Int size = text.si; } }" + val document = TextDocumentIdentifier(uri) + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, overlay)), + ) + server.textDocumentService + .documentSymbol(DocumentSymbolParams(document)) + .get(30, SECONDS) + val cursor = + server.textDocumentService.completion( + CompletionParams( + document, + Position(0, overlay.indexOf("text.si") + "text.si".length), + ), + ) + assertThat(entered.await(20, SECONDS)).isTrue() + if (changed) member.writeText("class Box { Int added = 1; }") + server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams( + listOf( + FileEvent(directory.toUri().toString(), FileChangeType.Created), + FileEvent(root.toURI().toString(), FileChangeType.Created), + ), + ), + ) + release.countDown() + if (changed) { + val failure = assertThrows { cursor.get(30, SECONDS) } + val error = failure.cause as ResponseErrorException + assertThat(error.responseError.code) + .isEqualTo(ResponseErrorCode.ContentModified.value) + } else { + assertThat(cursor.get(30, SECONDS).left.map { it.label }).contains("size") + } + } finally { + release.countDown() + server.shutdown().get(20, SECONDS) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt new file mode 100644 index 00000000000..84f0ea90be4 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt @@ -0,0 +1,62 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.DeclarationParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.TimeUnit.SECONDS + +class XdkDeclarationServerTest { + @Test + fun `declaration is advertised only by the implementing adapter and preserves multiple wire targets`() { + listOf(MockAdapter(), XdkAdapter()).forEach { adapter -> + val server = XtcLanguageServer(adapter) + server.connect(mock(LanguageClient::class.java)) + try { + val capabilities = + server.initialize(InitializeParams()).get(20, SECONDS).capabilities + if (adapter is XdkAdapter) { + assertThat(capabilities.declarationProvider.left).isTrue() + val uri = "file:///Declarations.x" + val text = + "module Declarations { interface A { Int read(); } interface B { Int read(); } " + + "class Both implements A, B { @Override Int read() = 1; } Int use(Both value) = value.read(); }" + val documents = server.textDocumentService + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text)), + ) + val targets = + documents + .declaration( + DeclarationParams( + TextDocumentIdentifier(uri), + Position(0, text.lastIndexOf("read")), + ), + ).get(30, SECONDS) + .left + assertThat(targets).hasSize(2) + assertThat(targets.map { it.range.start.character }) + .containsExactly( + text.indexOf("read"), + text.indexOf( + "read", + text.indexOf("read") + 1, + ), + ) + } else { + assertThat(capabilities.declarationProvider).isNull() + } + } finally { + server.shutdown().get(20, SECONDS) + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt new file mode 100644 index 00000000000..d3c1e5d642c --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt @@ -0,0 +1,282 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeLensCapabilities +import org.eclipse.lsp4j.CodeLensParams +import org.eclipse.lsp4j.CodeLensResolveSupportCapabilities +import org.eclipse.lsp4j.ConfigurationParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentLinkCapabilities +import org.eclipse.lsp4j.DocumentLinkParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InlayHintCapabilities +import org.eclipse.lsp4j.InlayHintParams +import org.eclipse.lsp4j.InlayHintResolveSupportCapabilities +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SymbolCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.WorkspaceSymbolParams +import org.eclipse.lsp4j.WorkspaceSymbolResolveSupportCapabilities +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.mock +import org.mockito.Mockito.`when` +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicReference + +class XdkFeatureResolveProtocolTest { + @TempDir lateinit var root: Path + private val source = + """ + module Resolve { + /** Read a number. */ + Int read(Int value) = value; + void run() { var result = read(1); } + // https://xtclang.org/ + } + """.trimIndent() + + @Test + fun `lens link hint and symbol resolve preserve identity and expire together`() { + session(true) { server, uri -> + val documents = server.textDocumentService + val id = TextDocumentIdentifier(uri) + val lens = documents.codeLens(CodeLensParams(id)).get(30, SECONDS).single { it.range.start.line == 0 } + val link = documents.documentLink(DocumentLinkParams(id)).get(30, SECONDS).single() + val hint = + documents + .inlayHint(InlayHintParams(id, Range(Position(0, 0), Position(6, 0)))) + .get(30, SECONDS) + .first { it.data != null } + val symbol = + server.workspaceService + .symbol(WorkspaceSymbolParams("Resolve")) + .get(30, SECONDS) + .right + .first() + assertThat(lens.command).isNull() + assertThat(link.target).isNull() + assertThat(hint.tooltip).isNull() + assertThat(symbol.location.isRight).isTrue() + val range = lens.range + val label = hint.label + assertThat( + documents + .resolveCodeLens(lens) + .get(10, SECONDS) + .command.arguments, + ).containsExactly(uri, "Resolve") + assertThat(lens.range).isEqualTo(range) + assertThat(documents.documentLinkResolve(link).get(10, SECONDS).target) + .isEqualTo("https://xtclang.org/") + assertThat( + documents + .resolveInlayHint(hint) + .get(10, SECONDS) + .tooltip.right.value, + ).isNotBlank() + assertThat(hint.label).isEqualTo(label) + assertThat( + server.workspaceService + .resolveWorkspaceSymbol(symbol) + .get(10, SECONDS) + .location + .isLeft, + ).isTrue() + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(TextDocumentContentChangeEvent("\n$source")), + ), + ) + listOf<() -> Any>( + { documents.resolveCodeLens(lens).get(10, SECONDS) }, + { documents.documentLinkResolve(link).get(10, SECONDS) }, + { documents.resolveInlayHint(hint).get(10, SECONDS) }, + { server.workspaceService.resolveWorkspaceSymbol(symbol).get(10, SECONDS) }, + ).forEach { call -> + assertThatThrownBy { call() }.hasMessageContaining("expired or changed") + } + } + } + + @Test + fun `clients without resolve capabilities receive complete eager results`() { + session(false) { server, uri -> + val documents = server.textDocumentService + val id = TextDocumentIdentifier(uri) + val lens = documents.codeLens(CodeLensParams(id)).get(30, SECONDS).single { it.range.start.line == 0 } + assertThat(lens.command.arguments).containsExactly(uri, "Resolve") + assertThat(lens.data).isNull() + assertThat( + documents + .documentLink(DocumentLinkParams(id)) + .get(30, SECONDS) + .single() + .target, + ).isNotBlank() + assertThat( + documents + .inlayHint(InlayHintParams(id, Range(Position(0, 0), Position(6, 0)))) + .get(30, SECONDS), + ).anyMatch { it.tooltip != null } + assertThat( + server.workspaceService + .symbol(WorkspaceSymbolParams("Resolve")) + .get(30, SECONDS) + .left, + ).allMatch { it.location.range != null } + assertThatThrownBy { documents.resolveCodeLens(lens).get(10, SECONDS) } + .hasMessageContaining("not negotiated") + } + } + + @Test + fun `scoped reference preference changes immediately and never removes Run`() { + val preference = AtomicReference(mapOf("references" to true)) + val requested = AtomicReference() + val client = mock(LanguageClient::class.java) + `when`(client.configuration(any())).thenAnswer { call -> + val params = call.getArgument(0) + requested.set(params) + CompletableFuture.completedFuture(params.items.map { preference.get() }) + } + session(false, client, true) { server, uri -> + val params = CodeLensParams(TextDocumentIdentifier(uri)) + + fun lenses() = server.textDocumentService.codeLens(params).get(30, SECONDS) + val enabled = lenses() + assertThat(enabled.map { it.command.command }).contains("xtc.runModule", "xtc.showReferences") + val reference = enabled.single { it.range.start.line == 2 } + assertThat(reference.command.title).isEqualTo("1 reference") + assertThat( + requested + .get() + .items + .single() + .scopeUri, + ).isEqualTo(uri) + assertThat( + requested + .get() + .items + .single() + .section, + ).isEqualTo("xtc.codeLens") + preference.set(mapOf("references" to false)) + assertThat(lenses().map { it.command.command }).containsExactly("xtc.runModule") + preference.set(mapOf("references" to "invalid")) + assertThat(lenses().map { it.command.command }).containsExactly("xtc.runModule") + preference.set(mapOf("references" to true)) + assertThat(lenses().map { it.command.title }).contains("1 reference") + } + } + + @Test + fun `reference resolve expires after consumer edits just like Run`() { + session(true) { server, uri -> + val documents = server.textDocumentService + val reference = + documents + .codeLens(CodeLensParams(TextDocumentIdentifier(uri))) + .get(30, SECONDS) + .single { it.range.start.line == 2 } + assertThat(reference.command).isNull() + assertThat( + documents + .resolveCodeLens(reference) + .get(10, SECONDS) + .command.title, + ).isEqualTo("1 reference") + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 2), + listOf(TextDocumentContentChangeEvent(source.replace("read(1)", "read(1) + read(2)"))), + ), + ) + assertThatThrownBy { documents.resolveCodeLens(reference).get(10, SECONDS) }.hasMessageContaining("expired or changed") + } + } + + private fun session( + lazy: Boolean, + client: LanguageClient = mock(LanguageClient::class.java), + configuration: Boolean = false, + body: (XtcLanguageServer, String) -> Unit, + ) { + CompilerTestSupport.configure() + val file = + root + .resolve("Resolve.x") + .toFile() + .canonicalFile + .apply { writeText(source) } + val uri = file.toURI().toString() + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(client) + server + .initialize( + InitializeParams().apply { + workspaceFolders = + listOf(WorkspaceFolder(root.toUri().toString(), "Resolve")) + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + if (lazy) { + codeLens = + CodeLensCapabilities().apply { + resolveSupport = + CodeLensResolveSupportCapabilities( + listOf("command"), + ) + } + documentLink = DocumentLinkCapabilities() + inlayHint = + InlayHintCapabilities().apply { + resolveSupport = + InlayHintResolveSupportCapabilities( + listOf("tooltip"), + ) + } + } + } + workspace = + WorkspaceClientCapabilities().apply { + this.configuration = configuration + if (lazy) { + symbol = + SymbolCapabilities().apply { + resolveSupport = + WorkspaceSymbolResolveSupportCapabilities( + listOf("location.range"), + ) + } + } + } + } + }, + ).get(30, SECONDS) + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source)), + ) + body(server, uri) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt new file mode 100644 index 00000000000..d3c8f0ed227 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt @@ -0,0 +1,430 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.DefinitionParams +import org.eclipse.lsp4j.DiagnosticSeverity +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentHighlightParams +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.ReferenceContext +import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.mock.MockAdapter +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkDependency +import org.xvm.lsp.adapter.xdk.toDependency +import org.xvm.lsp.model.CompilationResult +import org.xvm.lsp.model.Diagnostic +import org.xvm.lsp.model.Location +import java.util.concurrent.CompletableFuture +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.SECONDS + +class XdkLanguageServerTest { + @Test + fun `host dependency replacement republishes diagnostics for unchanged consumer versions`() { + CompilerTestSupport.configure() + + fun dependency(type: String): XdkDependency { + val value = if (type == "Int") "1" else "\"text\"" + val errors = ErrorList() + val result = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Library { static $type value() = $value; }", + "file:///Library.x", + ), + null, + errors, + ) + assertThat(result.succeeded()).describedAs(errors.errors.toString()).isTrue() + return result.toDependency() + } + val first = dependency("Int") + val incompatible = dependency("String") + val source = "module Protocol { package lib import Library; Int run() = lib.value(); }" + Session(XdkAdapter()).use { session -> + session.server.replaceCompilerDependencies(listOf(first)) + session.open(source, 7) + assertThat(session.next().diagnostics).isEmpty() + session.server.replaceCompilerDependencies(listOf(incompatible)) + val failed = session.next() + assertThat(failed.version).isEqualTo(7) + assertThat(failed.diagnostics).isNotEmpty() + assertThat(failed.diagnostics.map { it.code.left }).doesNotContain("ANALYSIS-FAILED") + session.server.replaceCompilerDependencies(listOf(first)) + val restored = session.next() + assertThat(restored.version).isEqualTo(7) + assertThat(restored.diagnostics).isEmpty() + val target = + session.documents + .definition( + DefinitionParams( + TextDocumentIdentifier(URI), + Position(0, source.indexOf("value")), + ), + ).get(10, SECONDS) + .left + .single() + assertThat(target.uri).isEqualTo("file:///Library.x") + } + } + + @Test + fun `exit supplies the process status according to whether shutdown was requested`() { + val statuses = mutableListOf() + val server = XtcLanguageServer(MockAdapter(), statuses::add) + server.exit() + assertThat(statuses).containsExactly(1) + server.shutdown().get(10, SECONDS) + server.exit() + assertThat(statuses).containsExactly(1, 0) + } + + @Test + fun `a foreign diagnostic publishes as related information and clears with its owner`() { + val foreign = Location("file:///Dependency.x", 4, 2, 4, 7) + val adapter = + object : Adapter by MockAdapter() { + override fun compileAsync( + uri: String, + content: String, + ): CompletableFuture = + CompletableFuture.completedFuture( + CompilationResult.failure( + uri, + listOf(Diagnostic.error(foreign, "dependency failure")), + ), + ) + } + Session(adapter).use { session -> + session.open("module Current {}") + val publication = session.next() + assertThat(publication.uri).isEqualTo(URI) + val diagnostic = publication.diagnostics.single() + assertThat(diagnostic.range).isEqualTo(Range(Position(0, 0), Position(0, 0))) + assertThat(diagnostic.message.left).contains(foreign.uri) + val related = diagnostic.relatedInformation.single() + assertThat(related.location.uri).isEqualTo(foreign.uri) + assertThat(related.location.range).isEqualTo(Range(Position(4, 2), Position(4, 7))) + session.closeDocument() + assertThat(session.next().diagnostics).isEmpty() + assertThat(session.published).isEmpty() + } + } + + private class Session( + adapter: Adapter, + ) : AutoCloseable { + val published = LinkedBlockingQueue() + val server = XtcLanguageServer(adapter) + val documents = server.textDocumentService + + init { + val client = mock(LanguageClient::class.java) + doAnswer { call -> + published.add(call.getArgument(0)) + null + }.`when`(client) + .publishDiagnostics(org.mockito.ArgumentMatchers.any()) + server.connect(client) + server.initialize(editorInitializeParams()).get() + } + + fun open( + content: String, + version: Int = 1, + ) = documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", version, content)), + ) + + fun change( + content: String, + version: Int, + ) = documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, version), + listOf(TextDocumentContentChangeEvent(content)), + ), + ) + + fun closeDocument() = documents.didClose(DidCloseTextDocumentParams(TextDocumentIdentifier(URI))) + + fun next(): PublishDiagnosticsParams = checkNotNull(published.poll(30, SECONDS)) { "no diagnostic publication" } + + override fun close() { + server.shutdown().get(10, SECONDS) + } + } + + /** + * An uncooperative backend can finish after cancellation; publication still must be guarded. + */ + private class PendingAdapter( + private val delegate: MockAdapter = MockAdapter(), + ) : Adapter by delegate { + private data class Pending( + val uri: String, + val content: String, + val result: CompletableFuture, + ) + + private val pending = mutableListOf() + val count: Int + get() = pending.size + + override fun compileAsync( + uri: String, + content: String, + ): CompletableFuture { + val result = + object : CompletableFuture() { + override fun cancel(mayInterruptIfRunning: Boolean): Boolean = false + } + pending.add(Pending(uri, content, result)) + return result + } + + fun finish(index: Int) { + val item = pending[index] + item.result.complete(delegate.compile(item.uri, item.content)) + } + + fun fail(index: Int) { + pending[index] + .result + .completeExceptionally(IllegalArgumentException("compiler failure")) + } + } + + @Test + fun `only the newest version publishes even if old analysis finishes last`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("// ERROR: old") + session.change("module Latest {}", 2) + session.change("// ERROR: out of order", 1) + assertThat(adapter.count).isEqualTo(2) + adapter.finish(1) + val latest = session.next() + assertThat(latest.version).isEqualTo(2) + assertThat(latest.diagnostics).isEmpty() + adapter.finish(0) + assertThat(session.published).isEmpty() + } + } + + @Test + fun `closed and reopened documents reject results from the previous lifetime`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("// ERROR: old", 10) + session.closeDocument() + assertThat(session.next().diagnostics).isEmpty() + session.open("module Reopened {}") + adapter.finish(0) + assertThat(session.published).isEmpty() + adapter.finish(1) + assertThat(session.next().version).isEqualTo(1) + session.close() + session.change("// ERROR: after shutdown", 2) + assertThat(adapter.count).isEqualTo(2) + } + } + + @Test + fun `shutdown prevents pending analysis from publishing`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("// ERROR: pending") + session.close() + adapter.finish(0) + assertThat(session.published).isEmpty() + } + } + + @Test + fun `document features wait for analysis without recompiling`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("module Waiting {}") + val outline = + session.documents.documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(URI))) + assertThat(outline.isDone).isFalse() + adapter.finish(0) + assertThat(outline.get(10, SECONDS).map { it.right.name }).contains("Waiting") + assertThat(adapter.count).isEqualTo(1) + } + } + + @Test + fun `a feature request cannot use a different document version`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("module Old {}") + val outline = + session.documents.documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(URI))) + session.change("module New {}", 2) + adapter.finish(0) + assertThatThrownBy { outline.get(10, SECONDS) } + .hasMessageContaining("Document changed during analysis") + } + } + + @Test + fun `unexpected analysis failure is visible and a later edit recovers`() { + val adapter = PendingAdapter() + Session(adapter).use { session -> + session.open("module Failed {}") + adapter.fail(0) + assertThat( + session + .next() + .diagnostics + .single() + .code.left, + ).isEqualTo("ANALYSIS-FAILED") + session.change("module Recovered {}", 2) + adapter.finish(1) + assertThat(session.next().diagnostics).isEmpty() + } + } + + @Test + fun `compiler diagnostics travel through LSP with severity codes spans and versions`() { + CompilerTestSupport.configure() + Session(XdkAdapter()).use { session -> + session.open("module Broken {\n void run() {\n Int n = ;\n }\n}") + val syntax = session.next() + assertThat(syntax.uri).isEqualTo(URI) + assertThat(syntax.version).isEqualTo(1) + assertThat(syntax.diagnostics).anySatisfy { diagnostic -> + assertThat(diagnostic.severity).isEqualTo(DiagnosticSeverity.Error) + assertThat(diagnostic.code.left).startsWith("PARSER-") + assertThat(diagnostic.range.start.line).isEqualTo(2) + assertThat(diagnostic.source).isEqualTo("xtc") + } + session.change("module Semantic { void run() { NoSuchTypeAnywhere x = 1; } }", 2) + val semantic = session.next() + assertThat(semantic.version).isEqualTo(2) + assertThat(semantic.diagnostics).anySatisfy { + assertThat(it.message.left).contains("NoSuchTypeAnywhere") + assertThat(it.severity).isEqualTo(DiagnosticSeverity.Error) + } + session.change( + "module Warning { class Base { @Atomic Int x = 1; } class Derived extends Base { @Atomic @Override Int x = 2; } }", + 3, + ) + val warning = session.next() + assertThat(warning.version).isEqualTo(3) + assertThat(warning.diagnostics).anySatisfy { + assertThat(it.code.left).isEqualTo("VERIFY-75") + assertThat(it.severity).isEqualTo(DiagnosticSeverity.Warning) + } + session.change("module Clean {}", 4) + assertThat(session.next().diagnostics).isEmpty() + session.closeDocument() + assertThat(session.next().diagnostics).isEmpty() + } + } + + @Test + fun `local navigation survives protocol conversion and honors includeDeclaration`() { + CompilerTestSupport.configure() + Session(XdkAdapter()).use { session -> + val source = "module Navigation { Int read() { Int value; value = 1; return value; } }" + session.open(source) + assertThat(session.next().diagnostics).isEmpty() + val document = TextDocumentIdentifier(URI) + val declaration = source.indexOf("value;") + val write = source.indexOf("value =") + val read = source.lastIndexOf("value;") + val cursor = Position(0, read) + + fun range(start: Int) = Range(Position(0, start), Position(0, start + "value".length)) + + val definition = + session.documents + .definition(DefinitionParams(document, cursor)) + .get(10, SECONDS) + .left + .single() + assertThat(definition.uri).isEqualTo(URI) + assertThat(definition.range).isEqualTo(range(declaration)) + for (includeDeclaration in listOf(false, true)) { + val references = + session.documents + .references( + ReferenceParams(document, cursor, ReferenceContext(includeDeclaration)), + ).get(10, SECONDS) + assertThat(references).allMatch { it.uri == URI } + val expected = + if (includeDeclaration) { + listOf(declaration, write, read) + } else { + listOf(write, read) + } + assertThat(references.map { it.range }) + .containsExactlyElementsOf(expected.map(::range)) + } + val highlights = + session.documents + .documentHighlight(DocumentHighlightParams(document, cursor)) + .get(10, SECONDS) + assertThat(highlights.map { it.range }) + .containsExactly(range(declaration), range(write), range(read)) + } + } + + @Test + fun `compiler mode advertises only its implemented features`() { + Session(XdkAdapter()).use { session -> + val capabilities = + session.server + .initialize(InitializeParams()) + .get() + .capabilities + assertThat(capabilities.hoverProvider.left).isTrue() + assertThat(capabilities.definitionProvider.left).isTrue() + assertThat(capabilities.typeDefinitionProvider.left).isTrue() + assertThat(capabilities.implementationProvider.left).isTrue() + assertThat(capabilities.documentSymbolProvider.left).isTrue() + assertThat(capabilities.foldingRangeProvider.left).isTrue() + assertThat(capabilities.completionProvider).isNotNull() + assertThat(capabilities.renameProvider).isNull() + assertThat(capabilities.documentFormattingProvider.left).isTrue() + assertThat(capabilities.documentLinkProvider).isNotNull() + assertThat(capabilities.codeLensProvider).isNotNull() + assertThat(capabilities.linkedEditingRangeProvider.left).isTrue() + assertThat(capabilities.signatureHelpProvider).isNotNull() + assertThat(capabilities.semanticTokensProvider).isNotNull() + assertThat(capabilities.callHierarchyProvider.left).isTrue() + assertThat(capabilities.inlayHintProvider.left).isTrue() + } + } + + private companion object { + const val URI = "file:///Protocol.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt new file mode 100644 index 00000000000..61f715359f8 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt @@ -0,0 +1,269 @@ +package org.xvm.lsp.server + +import com.google.gson.JsonPrimitive +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.FileOperationsWorkspaceCapabilities +import org.eclipse.lsp4j.FileRename +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.TypeHierarchyPrepareParams +import org.eclipse.lsp4j.TypeHierarchySubtypesParams +import org.eclipse.lsp4j.TypeHierarchySupertypesParams +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.io.File +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.TimeUnit.SECONDS + +class XdkModuleServerTest { + @TempDir lateinit var directory: Path + + @Test + fun `member diagnostics use their own versions and sibling edits refresh them`() { + val (root, member) = fixture() + Session().use { session -> + session.open(root, root.readText(), 1) + session.expect(member, 0, null, false) + var mark = session.published.size + session.open(member, member.readText(), 7) + session.expect(member, mark, 7, false) + mark = session.published.size + session.change(root, root.readText().replace("Base", "Renamed"), 2) + session.expect(member, mark, 7, true) + session.expect(root, mark, 2, false) + mark = session.published.size + session.change(root, root.readText(), 3) + session.expect(member, mark, 7, false) + mark = session.published.size + session.change(member, "class Child extends Base { MissingType absent; }", 8) + session.expect(member, mark, 8, true) + mark = session.published.size + session.closeDocument(member) + session.expect(member, mark, null, false) + mark = session.published.size + session.closeDocument(root) + session.expect(root, mark, 3, false) + session.expect(member, mark, null, false) + } + } + + @Test + fun `file creation and deletion refresh closed module members and clear removed diagnostics`() { + val (root, member) = fixture() + Session().use { session -> + session.open(root, root.readText(), 1) + session.expect(member, 0, null, false) + val added = directory.resolve("Project/Added.x").toFile() + added.writeText("class Added { MissingType absent; }") + var mark = session.published.size + session.watched(added, FileChangeType.Created) + session.expect(added, mark, null, true) + mark = session.published.size + assertThat(added.delete()).isTrue() + session.watched(added, FileChangeType.Deleted) + session.expect(added, mark, null, false) + } + } + + @Test + fun `file rename notifications refresh members and retire old diagnostic locations without watcher events`() { + val (root, member) = fixture() + Session().use { session -> + val capability = + session.server + .initialize( + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + workspace = + WorkspaceClientCapabilities().apply { + fileOperations = + FileOperationsWorkspaceCapabilities().apply { + didRename = true + } + } + } + }, + ).get(10, SECONDS) + .capabilities + .workspace + .fileOperations + .didRename + assertThat(capability.filters.map { it.pattern.glob }).contains("**/*.x", "**") + assertThat(capability.filters.map { it.scheme }).containsOnly("file") + session.open(root, root.readText(), 1) + session.expect(member, 0, null, false) + val renamed = member.resolveSibling("Renamed.x") + var mark = session.published.size + Files.move(member.toPath(), renamed.toPath()) + renamed.writeText("class Renamed extends Base { MissingType absent; }") + session.server.workspaceService.didRenameFiles( + RenameFilesParams( + listOf(FileRename(member.toURI().toString(), renamed.toURI().toString())), + ), + ) + session.expect(renamed, mark, null, true) + session.expect(member, mark, null, false) + mark = session.published.size + Files.move(renamed.toPath(), member.toPath()) + member.writeText("class Child extends Base {}") + session.server.workspaceService.didRenameFiles( + RenameFilesParams( + listOf(FileRename(renamed.toURI().toString(), member.toURI().toString())), + ), + ) + session.expect(renamed, mark, null, false) + session.expect(member, mark, null, false) + } + } + + @Test + fun `hierarchy data survives protocol conversion and obsolete items cannot select new declarations`() { + val (root, member) = fixture() + Session().use { session -> + val capabilities = + session.server + .initialize(InitializeParams()) + .get(10, SECONDS) + .capabilities + assertThat(capabilities.typeHierarchyProvider.left).isTrue() + session.open(root, root.readText(), 1) + val hierarchy = + session.documents + .prepareTypeHierarchy( + TypeHierarchyPrepareParams( + TextDocumentIdentifier(root.toURI().toString()), + Position(0, root.readText().indexOf("Base")), + ), + ).get(30, SECONDS) + .single() + hierarchy.data = JsonPrimitive(hierarchy.data as String) + val child = + session.documents + .typeHierarchySubtypes(TypeHierarchySubtypesParams(hierarchy)) + .get(10, SECONDS) + .single() + assertThat(child.uri).isEqualTo(member.toURI().toString()) + child.data = JsonPrimitive(child.data as String) + val base = + session.documents + .typeHierarchySupertypes(TypeHierarchySupertypesParams(child)) + .get(10, SECONDS) + .single() + assertThat(base.uri).isEqualTo(root.toURI().toString()) + session.change(root, root.readText(), 2) + assertThat( + session.documents + .typeHierarchySubtypes(TypeHierarchySubtypesParams(hierarchy)) + .get(30, SECONDS), + ).isEmpty() + } + } + + private fun fixture(): Pair { + directory = directory.toRealPath() + val root = directory.resolve("Project.x").toFile() + root.writeText("module Project { class Base {} }") + val member = directory.resolve("Project/Child.x").toFile() + member.parentFile.mkdirs() + member.writeText("class Child extends Base {}") + return root to member + } + + private class Session : AutoCloseable { + val server = XtcLanguageServer(XdkAdapter()) + val documents = server.textDocumentService + val published = CopyOnWriteArrayList() + + init { + val client = mock(LanguageClient::class.java) + doAnswer { call -> + published.add(call.getArgument(0)) + null + }.`when`(client) + .publishDiagnostics(any()) + server.connect(client) + server.initialize(editorInitializeParams()).get() + } + + fun open( + file: File, + text: String, + version: Int, + ) = documents.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem(file.toURI().toString(), "xtc", version, text), + ), + ) + + fun change( + file: File, + text: String, + version: Int, + ) = documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(file.toURI().toString(), version), + listOf(TextDocumentContentChangeEvent(text)), + ), + ) + + fun closeDocument(file: File) = + documents.didClose( + DidCloseTextDocumentParams(TextDocumentIdentifier(file.toURI().toString())), + ) + + fun watched( + file: File, + kind: FileChangeType, + ) = server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams(listOf(FileEvent(file.toURI().toString(), kind))), + ) + + fun expect( + file: File, + after: Int, + version: Int?, + errors: Boolean, + ) { + await().atMost(30, SECONDS).untilAsserted { + val matches = + published.drop(after).filter { + it.uri == file.toURI().toString() && it.version == version + } + assertThat(matches).isNotEmpty() + assertThat(matches.last().diagnostics.isNotEmpty()) + .describedAs("Publications: %s", published.drop(after)) + .isEqualTo(errors) + assertThat(matches.last().diagnostics).noneMatch { + it.code?.left == "ANALYSIS-FAILED" || it.code?.left == "EMB-5" + } + } + } + + override fun close() { + server.shutdown().get(10, SECONDS) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt new file mode 100644 index 00000000000..2c7a2fd401e --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt @@ -0,0 +1,467 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.awaitility.Awaitility.await +import org.eclipse.lsp4j.DefinitionParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.WorkspaceFoldersChangeEvent +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.api.EmbeddingSupport +import org.xvm.asm.ErrorList +import org.xvm.compiler.Source +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import org.xvm.lsp.adapter.xdk.toDependency +import java.io.File +import java.nio.file.Files +import java.nio.file.Path +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.TimeUnit.SECONDS + +class XdkProjectServerTest { + @TempDir lateinit var directory: Path + + @Test + fun `resource deletion and repair refresh an unchanged open consumer`() { + CompilerTestSupport.configure() + val library = file("Library", "module Library { static String value() = $./data.txt; }") + val consumerText = + "module Consumer { package lib import Library; String run() = lib.value(); }" + val consumer = file("Consumer", consumerText) + val resources = directory.resolve("assets").toFile().apply { mkdirs() } + val resource = resources.resolve("data.txt").apply { writeText("template") } + Session(library, consumer).use { session -> + session.server.replaceCompilerSourceModules( + listOf( + XdkSourceModule( + "Library", + library.toURI().toString(), + resourceRoots = listOf(resources.toURI().toString()), + ), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + ), + ) + session.open(consumer, consumerText, 1) + session.expect(consumer, 0, 1, false) + val deletion = session.published.size + Files.delete(resource.toPath()) + session.watched(resource, FileChangeType.Deleted) + session.expect(consumer, deletion, 1, true, "DEPENDENCY-FAILED") + val repair = session.published.size + resource.writeText("repaired") + session.watched(resource, FileChangeType.Created) + session.expect(consumer, repair, 1, false) + } + } + + @Test + fun `unsaved dependency edits recompile an unchanged consumer and closing restores disk`() { + val library = file("Library", LIBRARY) + val consumer = file("Consumer", CONSUMER) + Session(library, consumer).use { session -> + session.open(consumer, CONSUMER, 7) + session.expect(consumer, 0, 7, false) + var mark = session.published.size + session.open(library, INCOMPATIBLE, 2) + session.expect(consumer, mark, 7, true) + session.expect(library, mark, 2, false) + assertThat(library.readText()).isEqualTo(LIBRARY) + mark = session.published.size + session.change(library, LIBRARY, 3) + session.expect(consumer, mark, 7, false) + val target = + session.documents + .definition( + DefinitionParams( + TextDocumentIdentifier(consumer.toURI().toString()), + Position(0, CONSUMER.indexOf("value")), + ), + ).get(20, SECONDS) + .left + .single() + assertThat(target.uri).isEqualTo(library.toURI().toString()) + mark = session.published.size + session.change(library, INCOMPATIBLE, 4) + session.expect(consumer, mark, 7, true) + mark = session.published.size + session.closeDocument(library) + session.expect(consumer, mark, 7, false) + mark = session.published.size + session.open(library, INCOMPATIBLE, 1) + session.expect(consumer, mark, 7, true) + } + } + + @Test + fun `failed dependency publishes its own diagnostics and blocks old consumer facts until correction`() { + val library = file("Library", LIBRARY) + val consumer = file("Consumer", CONSUMER) + Session(library, consumer).use { session -> + session.open(consumer, CONSUMER, 7) + session.expect(consumer, 0, 7, false) + var mark = session.published.size + session.open(library, "module Library { MissingType broken; }", 2) + session.expect(library, mark, 2, true) + session.expect(consumer, mark, 7, true, "DEPENDENCY-FAILED") + assertThat( + session.adapter.findDefinition( + consumer.toURI().toString(), + 0, + CONSUMER.indexOf("value"), + ), + ).isNull() + mark = session.published.size + session.change(library, LIBRARY, 3) + session.expect(consumer, mark, 7, false) + session.expect(library, mark, 3, false) + } + } + + @Test + fun `closed dependency files are rebuilt on filesystem events including deletion and restoration`() { + val library = file("Library", LIBRARY) + val consumer = file("Consumer", CONSUMER) + Session(library, consumer).use { session -> + session.open(consumer, CONSUMER, 7) + session.expect(consumer, 0, 7, false) + var mark = session.published.size + library.writeText(INCOMPATIBLE) + session.watched(library, FileChangeType.Changed) + session.expect(consumer, mark, 7, true) + mark = session.published.size + assertThat(library.delete()).isTrue() + session.watched(library, FileChangeType.Deleted) + session.expect(library, mark, null, true, "SOURCE-UNAVAILABLE") + session.expect(consumer, mark, 7, true, "DEPENDENCY-FAILED") + mark = session.published.size + library.writeText(LIBRARY) + session.watched(library, FileChangeType.Created) + session.expect(consumer, mark, 7, false) + session.expect(library, mark, null, false) + } + } + + @Test + fun `unsaved and removed dependency members update consumers and clear deleted source diagnostics`() { + val library = file("Library", LIBRARY) + val consumer = file("Consumer", CONSUMER) + val member = directory.resolve("Library/Extra.x").toFile() + Session(library, consumer).use { session -> + session.open(consumer, CONSUMER, 7) + session.expect(consumer, 0, 7, false) + var mark = session.published.size + session.open(member, "class Extra { MissingType broken; }", 2) + session.expect(member, mark, 2, true) + session.expect(consumer, mark, 7, true, "DEPENDENCY-FAILED") + assertThat(member.exists()).isFalse() + mark = session.published.size + session.closeDocument(member) + session.expect(consumer, mark, 7, false) + member.parentFile.mkdirs() + member.writeText("class Extra { MissingType broken; }") + mark = session.published.size + session.watched(member, FileChangeType.Created) + session.expect(member, mark, null, true) + session.expect(consumer, mark, 7, true) + mark = session.published.size + assertThat(member.delete()).isTrue() + session.watched(member, FileChangeType.Deleted) + session.expect(consumer, mark, 7, false) + session.expect(member, mark, null, false) + } + } + + @Test + fun `transitive source changes rebuild consumers while an unrelated session stays available`() { + val library = file("Library", LIBRARY) + val bridge = + file( + "Bridge", + "module Bridge { package lib import Library; static Int value() = lib.value(); }", + ) + val consumer = file("Consumer", CONSUMER.replace("import Library", "import Bridge")) + val unrelated = file("Unrelated", "module Unrelated { Int value = 1; }") + Session(library, consumer, bridge).use { session -> + session.open(consumer, consumer.readText(), 7) + session.expect(consumer, 0, 7, false) + session.open(unrelated, unrelated.readText(), 1) + session.expect(unrelated, 0, 1, false) + val cached = session.adapter.getCachedResult(unrelated.toURI().toString()) + var mark = session.published.size + session.open(library, INCOMPATIBLE, 2) + session.expect(bridge, mark, null, true) + session.expect(consumer, mark, 7, true, "DEPENDENCY-FAILED") + assertThat(session.adapter.getCachedResult(unrelated.toURI().toString())) + .isEqualTo(cached) + assertThat(session.published.drop(mark)).noneMatch { + it.uri == unrelated.toURI().toString() + } + mark = session.published.size + session.change(library, LIBRARY, 3) + session.expect(consumer, mark, 7, false) + session.expect(bridge, mark, null, false) + } + } + + @Test + fun `binary replacement rebuilds open source dependencies before their consumers`() { + CompilerTestSupport.configure() + + fun artifact(type: String) = + EmbeddingSupport + .instance() + .compileModule( + Source( + "module Binary { static $type value() =" + + (if (type == "Int") "1" else "\"text\"") + + "; }", + "file:///Binary.x", + ), + null, + ErrorList(), + ).toDependency() + val first = artifact("Int") + val incompatible = artifact("String") + val library = + file( + "Library", + "module Library { package base import Binary; static Int value() = base.value(); }", + ) + val consumer = file("Consumer", CONSUMER) + Session(library, consumer).use { session -> + session.server.replaceCompilerDependencies(listOf(first)) + session.open(consumer, CONSUMER, 7) + session.expect(consumer, 0, 7, false) + session.open(library, library.readText(), 2) + session.expect(library, 0, 2, false) + var mark = session.published.size + session.server.replaceCompilerDependencies(listOf(incompatible)) + session.expect(library, mark, 2, true) + session.expect(consumer, mark, 7, true, "DEPENDENCY-FAILED") + mark = session.published.size + session.server.replaceCompilerDependencies(listOf(first)) + session.expect(library, mark, 2, false) + session.expect(consumer, mark, 7, false) + } + } + + @Test + fun `closing one consumer does not clear shared dependency errors owned by another`() { + val library = file("Library", "module Library { MissingType broken; }") + val consumer = file("Consumer", CONSUMER) + val other = file("Other", CONSUMER.replace("module Consumer", "module Other")) + Session(library, consumer).use { session -> + session.server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("Library", library.toURI().toString()), + XdkSourceModule("Consumer", consumer.toURI().toString(), setOf("Library")), + XdkSourceModule("Other", other.toURI().toString(), setOf("Library")), + ), + ) + session.open(consumer, consumer.readText(), 7) + session.expect(consumer, 0, 7, true) + session.open(other, other.readText(), 1) + session.expect(other, 0, 1, true) + val mark = session.published.size + session.closeDocument(consumer) + assertThat(session.published.drop(mark)).noneMatch { + it.uri == library.toURI().toString() && it.diagnostics.isEmpty() + } + } + } + + @Test + fun `live discovered edges propagate unsaved edits and folder notifications refresh diagnostics`() { + val first = + directory + .toRealPath() + .resolve("first") + .toFile() + .also { it.mkdirs() } + val library = first.resolve("Library.x").also { it.writeText(LIBRARY) } + val consumer = first.resolve("Consumer.x").also { it.writeText("module Consumer {}") } + Session(library, consumer, automatic = true).use { session -> + session.open(consumer, consumer.readText(), 7) + session.expect(consumer, 0, 7, false) + var mark = session.published.size + session.change(consumer, CONSUMER, 8) + session.expect(consumer, mark, 8, false) + mark = session.published.size + session.open(library, INCOMPATIBLE, 2) + session.expect(consumer, mark, 8, true) + session.closeDocument(library) + session.expect(consumer, mark, 8, false) + + val second = directory.resolve("second").toFile().also { it.mkdirs() } + val external = + second.resolve("External.x").also { + it.writeText("module External { static Int value() = 1; }") + } + val folder = WorkspaceFolder(second.toURI().toString(), "second") + // The second folder is outside the original folder's automatic scan. + session.server.workspaceService.didChangeWorkspaceFolders( + DidChangeWorkspaceFoldersParams( + WorkspaceFoldersChangeEvent(listOf(folder), emptyList()), + ), + ) + mark = session.published.size + session.change(consumer, CONSUMER.replace("Library", "External"), 9) + session.expect(consumer, mark, 9, false) + mark = session.published.size + session.server.workspaceService.didChangeWorkspaceFolders( + DidChangeWorkspaceFoldersParams( + WorkspaceFoldersChangeEvent(emptyList(), listOf(folder)), + ), + ) + session.expect(consumer, mark, 9, true) + assertThat(external.isFile).isTrue() + } + } + + private fun file( + name: String, + text: String, + ): File { + directory = directory.toRealPath() + return directory.resolve("$name.x").toFile().also { it.writeText(text) } + } + + private class Session( + library: File, + consumer: File, + bridge: File? = null, + automatic: Boolean = false, + ) : AutoCloseable { + val adapter = XdkAdapter() + val server = XtcLanguageServer(adapter) + val documents = server.textDocumentService + val published = CopyOnWriteArrayList() + + init { + val client = mock(LanguageClient::class.java) + doAnswer { call -> + published.add(call.getArgument(0)) + null + }.`when`(client) + .publishDiagnostics(any()) + server.connect(client) + if (automatic) { + server + .initialize( + editorInitializeParams().apply { + workspaceFolders = + listOf( + WorkspaceFolder( + consumer.parentFile.toURI().toString(), + "workspace", + ), + ) + }, + ).get(30, SECONDS) + } else { + server.initialize(editorInitializeParams()).get() + server.replaceCompilerSourceModules( + buildList { + add(XdkSourceModule("Library", library.toURI().toString())) + bridge?.let { + add(XdkSourceModule("Bridge", it.toURI().toString(), setOf("Library"))) + } + add( + XdkSourceModule( + "Consumer", + consumer.toURI().toString(), + setOf(if (bridge == null) "Library" else "Bridge"), + ), + ) + }, + ) + } + } + + fun open( + file: File, + text: String, + version: Int, + ) = documents.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem(file.toURI().toString(), "xtc", version, text), + ), + ) + + fun change( + file: File, + text: String, + version: Int, + ) = documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(file.toURI().toString(), version), + listOf(TextDocumentContentChangeEvent(text)), + ), + ) + + fun closeDocument(file: File) = + documents.didClose( + DidCloseTextDocumentParams(TextDocumentIdentifier(file.toURI().toString())), + ) + + fun watched( + file: File, + kind: FileChangeType, + ) = server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams(listOf(FileEvent(file.toURI().toString(), kind))), + ) + + fun expect( + file: File, + after: Int, + version: Int?, + errors: Boolean, + code: String? = null, + ) { + await().atMost(30, SECONDS).untilAsserted { + val last = + published.drop(after).lastOrNull { + it.uri == file.toURI().toString() && it.version == version + } + assertThat(last).describedAs(published.drop(after).toString()).isNotNull() + assertThat(last!!.diagnostics.isNotEmpty()) + .describedAs(last.toString()) + .isEqualTo(errors) + assertThat(last.diagnostics).noneMatch { + it.code?.left in setOf("ANALYSIS-FAILED", "EMB-5") + } + if (code != null) assertThat(last.diagnostics).anyMatch { it.code?.left == code } + } + } + + override fun close() { + server.shutdown().get(10, SECONDS) + } + } + + private companion object { + const val LIBRARY = "module Library { static Int value() = 1; }" + const val INCOMPATIBLE = "module Library { static String value() = \"text\"; }" + const val CONSUMER = + "module Consumer { package lib import Library; Int run() = lib.value(); }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt new file mode 100644 index 00000000000..3a245fddc15 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt @@ -0,0 +1,444 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.awaitility.Awaitility.await +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DiagnosticCapabilities +import org.eclipse.lsp4j.DiagnosticWorkspaceCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentDiagnosticParams +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.PreviousResultId +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.ServerCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceDiagnosticParams +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.ArgumentMatchers.any +import org.mockito.Mockito.doAnswer +import org.mockito.Mockito.mock +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.nio.file.Path +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicInteger + +class XdkPullDiagnosticsTest { + @TempDir lateinit var directory: Path + + @Test + fun `opening closed rename consumers and editing their dependency keeps analyses available`() { + val sources = + mapOf( + "Contracts" to "module Contracts { interface Mapper { T map(T value); } }", + "Uses" to + "module Uses { package api import Contracts; class Mapper implements api.Mapper { @Override String map(String value) = value; } String run(api.Mapper mapper) = mapper.map(\"a\"); }", + "Dormant" to + "module Dormant { package api import Contracts; String run(api.Mapper mapper) = mapper.map(\"b\"); }", + ) + val uris = + sources.mapValues { (name, source) -> + directory + .resolve("$name.x") + .toFile() + .apply { writeText(source) } + .toPath() + .toUri() + .toString() + } + Session().use { session -> + session.server.replaceCompilerSourceModules( + uris.map { (name, uri) -> + XdkSourceModule( + name, + uri, + if (name == "Contracts") emptySet() else setOf("Contracts"), + ) + }, + ) + session.open(uris.getValue("Contracts"), sources.getValue("Contracts"), 1) + assertThat(session.pull(uris.getValue("Contracts")).left.items).isEmpty() + sources + .filterKeys { it != "Contracts" } + .forEach { (name, source) -> + session.open(uris.getValue(name), source, 1) + } + sources.forEach { (name, source) -> + session.change(uris.getValue(name), source.replace("map(", "convert("), 2) + } + uris.values.forEach { uri -> + assertThat(session.pull(uri).left.items).isEmpty() + assertThat( + session.server.textDocumentService + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .get(10, SECONDS), + ).isNotEmpty() + } + } + } + + @Test + fun `pull negotiates one channel and unchanged reports become full after repair`() { + Session().use { session -> + assertThat(session.capabilities.diagnosticProvider.identifier).isEqualTo("xtc") + session.open(URI, BROKEN, 1) + val first = session.pull(URI).left + assertThat(first.items).isNotEmpty() + assertThat(first.resultId).isNotBlank() + assertThat(session.pull(URI, first.resultId).right.resultId).isEqualTo(first.resultId) + session.change(URI, VALID, 2) + val repaired = session.pull(URI, first.resultId).left + assertThat(repaired.items).isEmpty() + assertThat(repaired.resultId).isNotEqualTo(first.resultId) + await().atMost(10, SECONDS).until { session.refreshes.get() > 0 } + assertThat(session.published).isEmpty() + } + } + + @Test + fun `unnegotiated clients keep push diagnostics and cannot pull`() { + Session(pull = false).use { session -> + assertThat(session.capabilities.diagnosticProvider).isNull() + session.open(URI, BROKEN, 1) + await().atMost(10, SECONDS).until { session.published.isNotEmpty() } + assertThat(session.published.last().diagnostics).isNotEmpty() + assertThatThrownBy { session.pull(URI) }.hasMessageContaining("not negotiated") + assertThat(session.refreshes.get()).isZero() + } + } + + @Test + fun `workspace pull diagnoses unopened roots and does not install editor buffers`() { + val file = directory.resolve("Pull.x").toFile().apply { writeText(BROKEN) } + Session().use { session -> + session.server.replaceCompilerSourceModules( + listOf(XdkSourceModule("Pull", file.toURI().toString())), + ) + val first = + session + .workspace() + .items + .single() + .left + assertThat(first.items).isNotEmpty() + assertThat(first.version == null).isTrue() + assertThat(session.adapter.getCachedResult(first.uri)).isNull() + assertThat( + session + .workspace(listOf(PreviousResultId(first.uri, first.resultId))) + .items + .single() + .right + .resultId, + ).isEqualTo(first.resultId) + // No watcher notification: a read-only query still recaptures and compares disk inputs. + file.writeText(VALID) + val fixed = + session + .workspace(listOf(PreviousResultId(first.uri, first.resultId))) + .items + .single() + .left + assertThat(fixed.items).isEmpty() + assertThat(fixed.resultId).isNotEqualTo(first.resultId) + } + } + + @Test + fun `closed member pulls retain standalone module errors and follow watched file changes`() { + val root = directory.resolve("Pull.x").toFile().apply { writeText(VALID) } + val members = directory.resolve("Pull").toFile().apply { mkdirs() } + val member = members.resolve("Bad.x") + val uri = member.toPath().toUri().toString() + Session().use { session -> + session.server.replaceCompilerSourceModules(emptyList()) + session.open(root.toURI().toString(), VALID, 1) + assertThat(session.pull(root.toURI().toString()).left.items).isEmpty() + + fun watched(type: FileChangeType) = + session.server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams(listOf(FileEvent(uri, type))), + ) + + member.writeText("class Bad extends Missing {}") + watched(FileChangeType.Created) + val broken = session.pull(uri).left + assertThat(broken.items).isNotEmpty() + assertThat( + broken.items + .first() + .range.start.character, + ).isEqualTo(18) + assertThat(session.pull(member.toURI().toString(), broken.resultId).right.resultId) + .isEqualTo(broken.resultId) + + member.writeText("class Bad {}") + watched(FileChangeType.Changed) + val repaired = session.pull(uri, broken.resultId).left + assertThat(repaired.items).isEmpty() + assertThat(repaired.resultId).isNotEqualTo(broken.resultId) + + member.writeText("class Bad extends Missing {}") + watched(FileChangeType.Changed) + val brokenAgain = session.pull(uri).left + assertThat(brokenAgain.items).isNotEmpty() + assertThat(member.delete()).isTrue() + watched(FileChangeType.Deleted) + assertThat(session.pull(uri, brokenAgain.resultId).left.items).isEmpty() + } + } + + @Test + fun `related closed documents and failed dependencies survive document pull`() { + val lib = + directory.resolve("Library.x").toFile().apply { + writeText("module Library { MissingType value; }") + } + val app = + directory.resolve("Pull.x").toFile().apply { + writeText("module Pull { package lib import Library; }") + } + Session().use { session -> + session.server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("Library", lib.toURI().toString()), + XdkSourceModule("Pull", app.toURI().toString(), setOf("Library")), + ), + ) + session.open(app.toURI().toString(), app.readText(), 7) + val report = session.pull(app.toURI().toString()).left + assertThat(report.items.map { it.code.left }).contains("DEPENDENCY-FAILED") + assertThat( + report.relatedDocuments + .getValue(lib.canonicalFile.toURI().toString()) + .left + .items, + ).isNotEmpty() + val workspace = session.workspace().items.map { it.left } + assertThat(workspace.single { it.uri == app.toURI().toString() }.version).isEqualTo(7) + assertThat( + workspace.single { it.uri == app.toURI().toString() }.items.map { it.code.left }, + ).contains("DEPENDENCY-FAILED") + assertThat( + workspace.single { it.uri == lib.canonicalFile.toURI().toString() }.version == + null, + ).isTrue() + } + } + + @Test + fun `closing an unsaved buffer restores disk diagnostics and removed roots clear old reports`() { + val file = directory.resolve("Pull.x").toFile().apply { writeText(VALID) } + val uri = file.toURI().toString() + Session().use { session -> + session.server.replaceCompilerSourceModules(listOf(XdkSourceModule("Pull", uri))) + session.open(uri, BROKEN, 3) + val broken = session.pull(uri).left + session.server.textDocumentService.didClose( + DidCloseTextDocumentParams(TextDocumentIdentifier(uri)), + ) + assertThat(session.pull(uri, broken.resultId).left.items).isEmpty() + file.writeText(BROKEN) + val previous = + session + .workspace() + .items + .single() + .left + session.server.replaceCompilerSourceModules(emptyList()) + val removed = + session + .workspace(listOf(PreviousResultId(uri, previous.resultId))) + .items + .single() + .left + assertThat(removed.items).isEmpty() + assertThat(removed.version == null).isTrue() + } + } + + @Test + fun `result identifiers are scoped to a document and a server`() { + val id = + Session().use { session -> + session.open(URI, VALID, 1) + val first = session.pull(URI).left.resultId + assertThat(session.pull("file:///Elsewhere.x", first).isLeft).isTrue() + first + } + Session(related = false).use { session -> + session.open(URI, VALID, 1) + val report = session.pull(URI, id).left + assertThat(report.resultId).isNotEqualTo(id) + assertThat(report.relatedDocuments).isNull() + assertThatThrownBy { session.pull(URI, identifier = "unknown") } + .hasMessageContaining("not negotiated") + } + } + + @Test + fun `canceling a waiting pull preserves shared compilation and an edit rejects late reports`() { + CompilerTestSupport.configure() + val started = CountDownLatch(1) + val release = CountDownLatch(1) + val adapter = + XdkAdapter { source, errors -> + started.countDown() + check(release.await(20, SECONDS)) + EmbeddingSupport.instance().compileModule(source, null, errors) + } + try { + Session(adapter = adapter).use { session -> + session.open(URI, BROKEN, 1) + assertThat(started.await(10, SECONDS)).isTrue() + val params = DocumentDiagnosticParams(TextDocumentIdentifier(URI)) + val canceled = session.server.textDocumentService.diagnostic(params) + assertThat(canceled.cancel(false)).isTrue() + val stale = session.server.textDocumentService.diagnostic(params) + session.change(URI, VALID, 2) + release.countDown() + assertThatThrownBy { stale.get(10, SECONDS) } + .hasMessageContaining("Document changed") + assertThat(session.pull(URI).left.items).isEmpty() + assertThat(canceled.isCancelled).isTrue() + } + } finally { + release.countDown() + } + } + + @Test + fun `closed document pulls preserve error ranges across equivalent file URI spellings`() { + val file = directory.resolve("Pull.x").toFile().apply { writeText(BROKEN) } + Session().use { session -> + session.server.replaceCompilerSourceModules( + listOf(XdkSourceModule("Pull", file.toURI().toString())), + ) + val first = session.pull(file.toPath().toUri().toString()).left + assertThat(first.items).isNotEmpty() + assertThat( + first.items + .first() + .range.start.character, + ).isEqualTo(BROKEN.indexOf("missing")) + assertThat(first.items.first().relatedInformation).isNull() + val second = session.pull(file.canonicalFile.toURI().toString(), first.resultId) + assertThat(second.right.resultId).isEqualTo(first.resultId) + } + } + + private class Session( + pull: Boolean = true, + related: Boolean = true, + val adapter: XdkAdapter = XdkAdapter(), + ) : AutoCloseable { + val server = XtcLanguageServer(adapter) + val published = CopyOnWriteArrayList() + val refreshes = AtomicInteger() + val capabilities: org.eclipse.lsp4j.ServerCapabilities + + init { + val client = mock(LanguageClient::class.java) + doAnswer { + published += it.getArgument(0) + null + }.`when`(client) + .publishDiagnostics(any()) + doAnswer { + refreshes.incrementAndGet() + CompletableFuture.completedFuture(null) + }.`when`(client) + .refreshDiagnostics() + server.connect(client) + capabilities = + server + .initialize( + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + if (pull) { + diagnostic = + DiagnosticCapabilities().apply { + relatedDocumentSupport = related + } + } + } + workspace = + WorkspaceClientCapabilities().apply { + diagnostics = + DiagnosticWorkspaceCapabilities().apply { + refreshSupport = true + } + } + } + }, + ).get(10, SECONDS) + .capabilities + server.initialized(InitializedParams()) + } + + fun open( + uri: String, + text: String, + version: Int, + ) = server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", version, text)), + ) + + fun change( + uri: String, + text: String, + version: Int, + ) = server.textDocumentService.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, version), + listOf(TextDocumentContentChangeEvent(text)), + ), + ) + + fun pull( + uri: String, + previous: String? = null, + identifier: String? = null, + ) = server.textDocumentService + .diagnostic( + DocumentDiagnosticParams(TextDocumentIdentifier(uri)).apply { + previousResultId = previous + this.identifier = identifier + }, + ).get(30, SECONDS) + + fun workspace(previous: List = emptyList()) = + server.workspaceService.diagnostic(WorkspaceDiagnosticParams(previous)).get(30, SECONDS) + + override fun close() = server.close() + } + + private companion object { + const val URI = "file:///Pull.x" + const val VALID = "module Pull { Int run() = 1; }" + const val BROKEN = "module Pull { Int run() = missing; }" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt new file mode 100644 index 00000000000..13f0ed8a481 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt @@ -0,0 +1,729 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeActionContext +import org.eclipse.lsp4j.CodeActionParams +import org.eclipse.lsp4j.DidChangeWatchedFilesParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.FileChangeType +import org.eclipse.lsp4j.FileEvent +import org.eclipse.lsp4j.FileOperationsWorkspaceCapabilities +import org.eclipse.lsp4j.FileRename +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.ReferenceContext +import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.RenameFile +import org.eclipse.lsp4j.RenameFilesParams +import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceEditCapabilities +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import org.mockito.Mockito.mock +import org.xvm.api.EmbeddingSupport +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import org.xvm.lsp.adapter.xdk.XdkSourceModule +import java.net.URI +import java.nio.file.Path +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +class XdkRenameServerTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @CsvSource("false,false", "false,true", "true,false", "true,true") + fun `late watched creation preserves a rename proof only while its inputs are unchanged`( + changed: Boolean, + proposal: Boolean, + ) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + val root = + directory.resolve("App.x").toFile().apply { + writeText("module App { Box make() = new Box(); }") + } + val member = + directory.resolve("App/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box {}") + } + val hold = AtomicBoolean() + val entered = CountDownLatch(1) + val release = CountDownLatch(1) + val support = EmbeddingSupport.instance() + val adapter = + XdkAdapter( + { source, repository, errors -> support.compileModule(source, repository, errors) }, + { sources, repository, errors -> + if (hold.compareAndSet(true, false)) { + entered.countDown() + check(release.await(20, SECONDS)) + } + support.compileModule(sources, repository, errors) + }, + { source, _, cursor, repository, errors -> + support.analyzeIncomplete(source, cursor, repository, errors) + }, + ) + val server = XtcLanguageServer(adapter) + server.connect(mock(LanguageClient::class.java)) + try { + server + .initialize( + parameters().apply { + capabilities.workspace.workspaceEdit.resourceOperations = listOf("rename") + capabilities.workspace.fileOperations = + FileOperationsWorkspaceCapabilities().apply { willRename = true } + }, + ).get(20, SECONDS) + val uri = root.toURI().toString() + server.replaceCompilerSourceModules(listOf(XdkSourceModule("App", uri))) + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, root.readText())), + ) + server.textDocumentService + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .get(30, SECONDS) + hold.set(true) + val params = + RenameFilesParams( + listOf( + FileRename( + member.toURI().toString(), + directory.resolve("App/Crate.x").toUri().toString(), + ), + ), + ) + val proof = if (proposal) server.renameFilesProposal(params).thenApply { it?.edit } else server.willRenameFiles(params) + assertThat(entered.await(20, SECONDS)).isTrue() + if (changed) member.writeText("class Box { Int extra = 1; }") + server.workspaceService.didChangeWatchedFiles( + DidChangeWatchedFilesParams( + listOf(root.parentFile, member).map { + FileEvent(it.toURI().toString(), FileChangeType.Created) + }, + ), + ) + release.countDown() + if (changed) { + assertThatThrownBy { proof.get(30, SECONDS) } + .hasCauseInstanceOf(ResponseErrorException::class.java) + } else { + assertThat(proof.get(30, SECONDS)?.documentChanges).isNotEmpty() + } + assertThat(root.readText()).contains("new Box()") + } finally { + release.countDown() + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `file move proposal includes the configured graph and all resource operations without applying them`() { + directory = directory.toRealPath() + val root = + directory.resolve("old/App.x").toFile().apply { + parentFile.mkdirs() + writeText("module App {}") + } + val target = directory.resolve("moved").toFile() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server + .initialize( + parameters().apply { + capabilities.workspace.workspaceEdit.resourceOperations = listOf("rename") + capabilities.workspace.fileOperations = FileOperationsWorkspaceCapabilities().apply { willRename = true } + }, + ).get(20, SECONDS) + server.replaceCompilerSourceModules(listOf(XdkSourceModule("App", root.toURI().toString(), resourceRoots = emptyList()))) + val params = RenameFilesParams(listOf(FileRename(root.parentFile.toURI().toString(), target.toURI().toString()))) + assertThat(server.willRenameFiles(params).get(30, SECONDS)).isNull() + val proposal = requireNotNull(server.renameFilesProposal(params).get(30, SECONDS)) + assertThat( + proposal.graph!! + .before + .single() + .uri, + ).isEqualTo(root.toURI().toString()) + assertThat( + proposal.graph!! + .after + .single() + .uri, + ).isEqualTo( + directory + .resolve("moved/App.x") + .toFile() + .toURI() + .toString(), + ) + assertThat( + proposal.graph!! + .after + .single() + .resourceRoots, + ).isEmpty() + assertThat( + proposal.edit.documentChanges + .single() + .right, + ).isInstanceOf(RenameFile::class.java) + assertThat(proposal.scope!!.boundary).isEqualTo("CONFIGURED_GRAPH") + assertThat(root).exists() + assertThat(target).doesNotExist() + assertThat( + server + .compilerSourceModules() + .get(30, SECONDS) + .single() + .uri, + ).isEqualTo(root.toURI().toString()) + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `cross module type proposal carries dependency replacement and versioned source edits`() { + directory = directory.toRealPath() + val root = directory.resolve("App.x").toFile().apply { writeText("module App { Box make() = new Box(); }") } + val other = directory.resolve("Other.x").toFile().apply { writeText("module Other {}") } + val member = + directory.resolve("App/Box.x").toFile().apply { + parentFile.mkdirs() + writeText("class Box {}") + } + val target = directory.resolve("Other/Box.x").toFile().apply { parentFile.mkdirs() } + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server + .initialize( + parameters().apply { + capabilities.workspace.workspaceEdit.resourceOperations = listOf("rename") + capabilities.workspace.fileOperations = FileOperationsWorkspaceCapabilities().apply { willRename = true } + }, + ).get(20, SECONDS) + server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("App", root.toURI().toString()), + XdkSourceModule("Other", other.toURI().toString()), + ), + ) + val params = RenameFilesParams(listOf(FileRename(member.toURI().toString(), target.toURI().toString()))) + assertThat(server.willRenameFiles(params).get(30, SECONDS)).isNull() + val proposal = requireNotNull(server.renameFilesProposal(params).get(30, SECONDS)) + assertThat( + proposal.graph!! + .before + .single { it.name == "App" } + .dependencies, + ).isEmpty() + assertThat( + proposal.graph!! + .after + .single { it.name == "App" } + .dependencies, + ).containsExactly("Other") + assertThat(proposal.edit.documentChanges.filter { it.isLeft }).isNotEmpty() + assertThat( + proposal.edit.documentChanges + .filter { it.isRight } + .single() + .right, + ).isInstanceOf(RenameFile::class.java) + assertThat( + server + .compilerSourceModules() + .get(30, SECONDS) + .single { it.name == "App" } + .dependencies, + ).isEmpty() + assertThat(member).exists() + assertThat(target).doesNotExist() + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `host rename carries the expected graph and replacement without applying either`() { + directory = directory.toRealPath() + val library = directory.resolve("Library.x").toFile() + val consumer = directory.resolve("Consumer.x").toFile() + val text = "module Library.example.org { class Box {} }" + val use = + "module Consumer { package lib import Library.example.org; lib.Box make() = new lib.Box(); }" + library.writeText(text) + consumer.writeText(use) + val uri = library.toURI().toString() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server + .initialize( + parameters().apply { + capabilities.workspace.workspaceEdit.resourceOperations = listOf("rename") + }, + ).get(20, SECONDS) + server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("Library.example.org", uri), + XdkSourceModule( + "Consumer", + consumer.toURI().toString(), + setOf("Library.example.org"), + ), + ), + ) + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, text))) + documents + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .get(30, SECONDS) + val params = + RenameParams( + TextDocumentIdentifier(uri), + Position(0, text.indexOf("Library")), + "Renamed", + ) + val proposal = requireNotNull(server.renameProposal(params).get(30, SECONDS)) + val scope = requireNotNull(proposal.scope) + assertThat(scope.boundary).isEqualTo("CONFIGURED_GRAPH") + assertThat(scope.modules.map { it.name }) + .containsExactlyInAnyOrder("Library.example.org", "Consumer") + assertThat(scope.sourceUris).containsExactlyInAnyOrder(uri, consumer.toURI().toString()) + assertThat(scope.revision).startsWith("graph:") + val graph = requireNotNull(proposal.graph) + assertThat(graph.before.map { it.name }) + .containsExactlyInAnyOrder("Library.example.org", "Consumer") + assertThat(graph.after.map { it.name }) + .containsExactlyInAnyOrder("Renamed.example.org", "Consumer") + assertThat(graph.after.single { it.name == "Consumer" }.dependencies) + .containsExactly("Renamed.example.org") + assertThat( + proposal.edit.documentChanges + .first() + .left.textDocument.version, + ).isEqualTo(7) + assertThat( + proposal.edit.documentChanges + .last() + .right, + ).isInstanceOf(RenameFile::class.java) + assertThat(documents.rename(params).get(30, SECONDS)).isNull() + assertThat(library.readText()).isEqualTo(text) + assertThat(consumer.readText()).isEqualTo(use) + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `rename negotiates document changes and includes open and closed file versions`() { + directory = directory.toRealPath() + val root = directory.resolve("Rename.x").toFile() + val member = directory.resolve("Rename/Child.x").toFile() + val text = "module Rename { Int run() { Int local = 1; return local; } }" + val child = + "class Child { private Int pick(Int input) = input; Int run() = pick(input = 1); }" + root.writeText(text) + member.parentFile.mkdirs() + member.writeText(child) + val uri = root.toURI().toString() + val memberUri = member.toURI().toString() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + val capabilities = server.initialize(parameters()).get(20, SECONDS).capabilities + assertThat(capabilities.renameProvider.right.prepareProvider).isTrue() + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, text))) + documents + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .get(30, SECONDS) + val params = + RenameParams( + TextDocumentIdentifier(memberUri), + Position(0, child.indexOf("input")), + "value", + ) + val edit = requireNotNull(documents.rename(params).get(30, SECONDS)) + assertThat(edit.changes).isNull() + val changes = edit.documentChanges.associate { it.left.textDocument.uri to it.left } + assertThat(changes.keys).containsExactly(memberUri) + val rootEdit = + requireNotNull( + documents + .rename( + RenameParams( + TextDocumentIdentifier(uri), + Position(0, text.indexOf("local")), + "value", + ), + ).get(30, SECONDS), + ) + assertThat( + rootEdit.documentChanges + .single() + .left.textDocument.version, + ).isEqualTo(7) + assertThat(changes.getValue(memberUri).textDocument.version).isNull() + assertThat( + changes + .getValue(memberUri) + .edits + .first() + .left.newText, + ).isEqualTo("value") + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(memberUri, "xtc", 3, child)), + ) + val openEdit = requireNotNull(documents.rename(params).get(30, SECONDS)) + assertThat( + openEdit.documentChanges + .first { it.left.textDocument.uri == memberUri } + .left + .textDocument + .version, + ).isEqualTo(3) + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `configured graph references and rename include closed consumers with correct edit versions`() { + directory = directory.toRealPath() + val library = directory.resolve("Library.x").toFile() + val consumer = directory.resolve("Consumer.x").toFile() + val text = "module Library { class Box { Int pick(Int value) = value; } }" + val use = + "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(1); }" + library.writeText(text) + consumer.writeText(use) + val uri = library.toURI().toString() + val consumerUri = consumer.toURI().toString() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server.initialize(parameters()).get(20, SECONDS) + server.replaceCompilerSourceModules( + listOf( + XdkSourceModule("Library", uri), + XdkSourceModule("Consumer", consumerUri, setOf("Library")), + ), + ) + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, text))) + val document = TextDocumentIdentifier(uri) + val at = Position(0, text.indexOf("pick")) + val references = + documents + .references(ReferenceParams(document, at, ReferenceContext(true))) + .get(30, SECONDS) + assertThat(references.map { it.uri }).containsExactlyInAnyOrder(uri, consumerUri) + val params = RenameParams(document, at, "choose") + val edit = requireNotNull(documents.rename(params).get(30, SECONDS)) + assertThat(edit.changes).isNull() + val changes = edit.documentChanges.associate { it.left.textDocument.uri to it.left } + assertThat(changes.keys).containsExactlyInAnyOrder(uri, consumerUri) + assertThat(changes.getValue(uri).textDocument.version).isEqualTo(7) + assertThat(changes.getValue(consumerUri).textDocument.version).isNull() + val overlay = use.replace("box.pick(1)", "box.pick(1) + box.pick(2)") + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(consumerUri, "xtc", 3, overlay)), + ) + documents + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(consumerUri))) + .get(30, SECONDS) + val openEdit = requireNotNull(documents.rename(params).get(30, SECONDS)) + val consumerEdit = + openEdit.documentChanges.single { it.left.textDocument.uri == consumerUri }.left + assertThat(consumerEdit.textDocument.version).isEqualTo(3) + assertThat(consumerEdit.edits).hasSize(2) + assertThat(consumer.readText()).isEqualTo(use) + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `clients without versioned edits receive no compiler rename capability or unsafe fallback`() { + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + assertThat( + server + .initialize(InitializeParams()) + .get(20, SECONDS) + .capabilities + .renameProvider, + ).isNull() + val text = "module Rename { Int run() { Int local = 1; return local; } }" + val uri = "file:///Rename.x" + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text)), + ) + assertThat( + server.textDocumentService + .rename( + RenameParams( + TextDocumentIdentifier(uri), + Position(0, text.indexOf("local")), + "value", + ), + ).get(30, SECONDS), + ).isNull() + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @ParameterizedTest + @CsvSource( + "false, false, false, false", + "true, false, false, false", + "false, true, false, false", + "true, true, false, false", + "false, true, true, false", + "true, true, true, false", + "false, true, true, true", + "true, true, true, true", + ) + fun `missing method quick fix targets source destination with its own document version`( + open: Boolean, + separateModule: Boolean, + requiresImport: Boolean, + companion: Boolean, + ) { + directory = directory.toRealPath() + val type = if (requiresImport) "shared.Value" else "Int" + val text = + """ + module Missing { + ${if (separateModule) "package lib import Library;" else ""} + ${if (requiresImport) "package shared import Types;" else ""} + $type read(${if (separateModule) "lib.Other" else "Other"} peer, $type value) { + return peer.missing(value); + } + } + """.trimIndent() + val source = directory.resolve("Missing.x").toFile().apply { writeText(text) } + val target = + directory + .resolve( + if (companion) { + "Library/Other.x" + } else if (separateModule) { + "Library.x" + } else { + "Missing/Other.x" + }, + ).toFile() + .apply { + parentFile.mkdirs() + writeText(if (separateModule && !companion) "module Library { class Other {} }" else "class Other {}") + } + val root = if (companion) directory.resolve("Library.x").toFile().apply { writeText("module Library {}") } else target + val rootOriginal = root.readText() + val types = directory.resolve("Types.x").toFile().apply { writeText("module Types { class Value {} }") } + val targetOriginal = target.readText() + val uri = source.toURI().toString() + val targetUri = target.toURI().toString() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server.initialize(parameters()).get(20, SECONDS) + server.replaceCompilerSourceModules( + buildList { + if (requiresImport) add(XdkSourceModule("Types", types.toURI().toString())) + val typeDependency = if (requiresImport) setOf("Types") else emptySet() + if (separateModule) add(XdkSourceModule("Library", root.toURI().toString(), typeDependency)) + add(XdkSourceModule("Missing", uri, (if (separateModule) setOf("Library") else emptySet()) + typeDependency)) + }, + ) + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, text))) + if (open) documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(targetUri, "xtc", 13, target.readText()))) + if (open && companion) { + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(root.toURI().toString(), "xtc", 17, rootOriginal))) + } + val line = text.lines().indexOfFirst { "peer.missing" in it } + val at = Position(line, text.lines()[line].indexOf("missing")) + val actions = + documents + .codeAction(CodeActionParams(TextDocumentIdentifier(uri), Range(at, at), CodeActionContext(emptyList()))) + .get(30, SECONDS) + val edit = actions.single { it.isRight && it.right.title == "Create public method 'missing' in 'Other'" }.right.edit + assertThat(edit.changes).isNull() + assertThat(edit.documentChanges).hasSize(if (companion) 2 else 1) + val change = edit.documentChanges.map { it.left }.single { URI(it.textDocument.uri) == URI(targetUri) } + if (companion) { + val rootChange = edit.documentChanges.map { it.left }.single { URI(it.textDocument.uri) == root.toURI() } + assertThat(rootChange.textDocument.version).isEqualTo(if (open) 17 else null) + assertThat(rootChange.edits).hasSize(1) + assertThat( + rootChange.edits + .single() + .left.newText, + ).contains("package types import Types;") + } + assertThat(URI(change.textDocument.uri)).isEqualTo(URI(targetUri)) + assertThat(change.textDocument.version).isEqualTo(if (open) 13 else null) + assertThat(change.edits).hasSize(if (requiresImport && !companion) 2 else 1) + val generated = change.edits.joinToString("\n") { it.left.newText } + if (requiresImport) { + assertThat(generated).contains("public types.Value missing(types.Value arg1)") + if (!companion) assertThat(generated).contains("package types import Types;") + } else { + assertThat(generated).contains("public Int64 missing(Int64 arg1)") + } + assertThat(source.readText()).isEqualTo(text) + assertThat(target.readText()).isEqualTo(targetOriginal) + assertThat(root.readText()).isEqualTo(rootOriginal) + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `compiler import actions preserve the document version on the wire`() { + val text = "module Imports { import ecstasy.text.StringBuffer; }" + val file = directory.resolve("Imports.x").toFile().apply { writeText(text) } + val uri = file.toURI().toString() + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + server.initialize(parameters()).get(20, SECONDS) + server.replaceCompilerSourceModules(listOf(XdkSourceModule("Imports", uri))) + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, text))) + val actions = + documents + .codeAction( + CodeActionParams( + TextDocumentIdentifier(uri), + Range(Position(0, 0), Position(0, text.length)), + CodeActionContext(emptyList()), + ), + ).get(30, SECONDS) + val edit = actions.single { it.isRight && it.right.title == "Remove unused import 'StringBuffer'" }.right.edit + assertThat(edit.changes).isNull() + assertThat( + edit.documentChanges + .single() + .left.textDocument.version, + ).isEqualTo(7) + assertThat( + edit.documentChanges + .single() + .left.edits + .single() + .left.newText, + ).isEmpty() + } finally { + server.shutdown().get(20, SECONDS) + } + } + + @Test + fun `member file rename requires resource operation support and follows versioned text edits`() { + directory = directory.toRealPath() + val root = + directory.resolve("App.x").toFile().also { + it.writeText("module App { Item make() = new Item(); }") + } + val member = + directory.resolve("App/Item.x").toFile().also { + it.parentFile.mkdirs() + it.writeText("class Item {}") + } + directory.resolve("App/Item/Nested.x").toFile().also { + it.parentFile.mkdirs() + it.writeText("class Nested {}") + } + listOf(false, true).forEach { enabled -> + val server = XtcLanguageServer(XdkAdapter()) + server.connect(mock(LanguageClient::class.java)) + try { + val params = + parameters().apply { + workspaceFolders = + listOf(WorkspaceFolder(directory.toUri().toString(), "workspace")) + capabilities.workspace.workspaceEdit.resourceOperations = + if (enabled) listOf("rename") else emptyList() + } + server.initialize(params).get(20, SECONDS) + val documents = server.textDocumentService + val uri = root.toURI().toString() + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, root.readText())), + ) + documents + .documentSymbol(DocumentSymbolParams(TextDocumentIdentifier(uri))) + .get(30, SECONDS) + val edit = + documents + .rename( + RenameParams( + TextDocumentIdentifier(uri), + Position(0, root.readText().indexOf("Item")), + "Renamed", + ), + ).get(30, SECONDS) + if (!enabled) { + assertThat(edit).isNull() + } else { + val changes = requireNotNull(edit).documentChanges + assertThat(changes.takeWhile { it.isLeft }).hasSize(2) + val moves = changes.dropWhile { it.isLeft }.map { it.right as RenameFile } + assertThat(moves).hasSize(2) + val move = moves.first() + assertThat(move.oldUri).isEqualTo(member.toURI().toString()) + assertThat(move.newUri).endsWith("/Renamed.x") + assertThat(move.options.overwrite).isFalse() + assertThat(moves.last().oldUri).endsWith("/App/Item") + assertThat(moves.last().newUri).endsWith("/App/Renamed") + assertThat(moves).allMatch { + it.options.overwrite == false && it.options.ignoreIfExists == false + } + } + assertThat(member.isFile).isTrue() + } finally { + server.shutdown().get(20, SECONDS) + } + } + } + + private fun parameters() = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = editorInitializeParams().capabilities.textDocument + workspace = + WorkspaceClientCapabilities().apply { + workspaceEdit = + WorkspaceEditCapabilities().apply { documentChanges = true } + } + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt new file mode 100644 index 00000000000..9d6653b7143 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt @@ -0,0 +1,220 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CodeActionCapabilities +import org.eclipse.lsp4j.CodeActionContext +import org.eclipse.lsp4j.CodeActionKindCapabilities +import org.eclipse.lsp4j.CodeActionLiteralSupportCapabilities +import org.eclipse.lsp4j.CodeActionParams +import org.eclipse.lsp4j.CodeActionResolveSupportCapabilities +import org.eclipse.lsp4j.CompletionCapabilities +import org.eclipse.lsp4j.CompletionItemCapabilities +import org.eclipse.lsp4j.CompletionItemResolveSupportCapabilities +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceEditCapabilities +import org.eclipse.lsp4j.WorkspaceFolder +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.nio.file.Path +import java.util.concurrent.TimeUnit.SECONDS + +class XdkResolveProtocolTest { + @TempDir lateinit var directory: Path + + private val uri: String + get() = directory.resolve("Resolve.x").toUri().toString() + + @Test + fun `completion resolves documentation without changing insertion and rejects an old snapshot`() { + val source = + "module Resolve { class Box { /** Reads a value. */ Int read() = 1; } Int run(Box box) = box.re; }" + session(true, source) { server -> + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source))) + val item = + service + .completion( + CompletionParams( + TextDocumentIdentifier(uri), + Position(0, source.lastIndexOf("re;") + 2), + ), + ).get(30, SECONDS) + .left + .single { it.label == "read" } + assertThat(item.documentation).isNull() + assertThat(item.data).isNotNull() + val insertion = item.textEdit + val resolved = service.resolveCompletionItem(item).get(10, SECONDS) + assertThat(resolved.documentation.left).contains("Reads a value") + assertThat(resolved.textEdit).isEqualTo(insertion) + service.didClose(DidCloseTextDocumentParams(TextDocumentIdentifier(uri))) + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source))) + assertThatThrownBy { service.resolveCompletionItem(item).get(10, SECONDS) } + .hasMessageContaining("expired or changed") + } + } + + @Test + fun `import completion publishes atomic additional edits before resolve`() { + val source = "module Resolve { Doc value; }" + session(true, source) { server -> + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source))) + val item = + service + .completion(CompletionParams(TextDocumentIdentifier(uri), Position(0, source.indexOf("Doc") + 3))) + .get(30, SECONDS) + .left + .single { it.label == "Document" } + assertThat(item.textEdit.left.newText).isEqualTo("Document") + assertThat(item.additionalTextEdits.single().newText).contains("import xml.Document;") + } + } + + @Test + fun `code action lazily converts versioned edits and refuses changed inputs`() { + val source = "module Resolve { import ecstasy.text.StringBuffer; }" + session(true, source) { server -> + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 7, source))) + val action = + service + .codeAction( + CodeActionParams( + TextDocumentIdentifier(uri), + Range(Position(0, 0), Position(0, source.length)), + CodeActionContext(emptyList()), + ), + ).get(30, SECONDS) + .single { it.right.title == "Remove unused import 'StringBuffer'" } + .right + assertThat(action.edit).isNull() + assertThat(action.data).isNotNull() + val resolved = service.resolveCodeAction(action).get(10, SECONDS) + assertThat( + resolved.edit.documentChanges + .single() + .left.textDocument.version, + ).isEqualTo(7) + assertThat( + resolved.edit.documentChanges + .single() + .left.edits + .single() + .left.newText, + ).isEmpty() + service.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(uri, 8), + listOf(TextDocumentContentChangeEvent("\n$source")), + ), + ) + assertThatThrownBy { service.resolveCodeAction(action).get(10, SECONDS) } + .hasMessageContaining("expired or changed") + } + } + + @Test + fun `clients without data and edit resolve support receive eager actions`() { + val source = "module Resolve { import ecstasy.text.StringBuffer; }" + session(false, source) { server -> + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, source))) + val action = + service + .codeAction( + CodeActionParams( + TextDocumentIdentifier(uri), + Range(Position(0, 0), Position(0, source.length)), + CodeActionContext(emptyList()), + ), + ).get(30, SECONDS) + .single { it.right.title == "Remove unused import 'StringBuffer'" } + .right + assertThat(action.edit).isNotNull() + assertThat(action.data).isNull() + assertThatThrownBy { service.resolveCodeAction(action).get(10, SECONDS) } + .hasMessageContaining("not negotiated") + } + } + + private fun session( + resolve: Boolean, + source: String, + body: (XtcLanguageServer) -> Unit, + ) { + CompilerTestSupport.configure() + directory = directory.toRealPath() + directory.resolve("Resolve.x").toFile().writeText(source) + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + val capabilities = + server + .initialize( + InitializeParams().apply { + workspaceFolders = + listOf(WorkspaceFolder(directory.toUri().toString(), "Resolve")) + this.capabilities = + ClientCapabilities().apply { + workspace = + WorkspaceClientCapabilities().apply { + workspaceEdit = + WorkspaceEditCapabilities().apply { + documentChanges = true + } + } + textDocument = + TextDocumentClientCapabilities().apply { + completion = + CompletionCapabilities().apply { + completionItem = + CompletionItemCapabilities().apply { + if (resolve) { + resolveSupport = + CompletionItemResolveSupportCapabilities( + listOf("documentation"), + ) + } + } + } + codeAction = + CodeActionCapabilities().apply { + codeActionLiteralSupport = + CodeActionLiteralSupportCapabilities( + CodeActionKindCapabilities(listOf("")), + ) + dataSupport = resolve + resolveSupport = + CodeActionResolveSupportCapabilities( + listOf("edit"), + ) + } + } + } + }, + ).get(30, SECONDS) + .capabilities + assertThat(capabilities.completionProvider.resolveProvider).isEqualTo(resolve) + assertThat(capabilities.codeActionProvider.isRight).isEqualTo(resolve) + body(server) + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt new file mode 100644 index 00000000000..39908d8f1ff --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt @@ -0,0 +1,146 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.SemanticTokensCapabilities +import org.eclipse.lsp4j.SemanticTokensClientCapabilitiesRequests +import org.eclipse.lsp4j.SemanticTokensClientCapabilitiesRequestsFull +import org.eclipse.lsp4j.SemanticTokensDeltaParams +import org.eclipse.lsp4j.SemanticTokensParams +import org.eclipse.lsp4j.SemanticTokensRangeParams +import org.eclipse.lsp4j.SemanticTokensWorkspaceCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.mockito.Mockito.timeout +import org.mockito.Mockito.verify +import org.mockito.Mockito.`when` +import org.xvm.lsp.adapter.CompilerTestSupport +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.CompletableFuture +import java.util.concurrent.TimeUnit.SECONDS + +class XdkSemanticTokenProtocolTest { + @Test + fun `compiler token delta survives edits and resets on reopen with negotiated refresh`() { + CompilerTestSupport.configure() + XtcLanguageServer(XdkAdapter()).use { server -> + val client = mock(LanguageClient::class.java) + `when`(client.refreshSemanticTokens()) + .thenReturn(CompletableFuture.completedFuture(null)) + server.connect(client) + val capabilities = + server + .initialize( + InitializeParams().apply { + this.capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + semanticTokens = + SemanticTokensCapabilities().apply { + requests = + SemanticTokensClientCapabilitiesRequests( + SemanticTokensClientCapabilitiesRequestsFull( + true, + ), + true, + ) + } + } + workspace = + WorkspaceClientCapabilities().apply { + semanticTokens = + SemanticTokensWorkspaceCapabilities(true) + } + } + }, + ).get() + .capabilities + server.initialized(InitializedParams()) + assertThat(capabilities.semanticTokensProvider.full.right.delta).isTrue() + assertThat(capabilities.semanticTokensProvider.range.left).isTrue() + val service = server.textDocumentService + val document = TextDocumentIdentifier("file:///Tokens.x") + val source = "module Tokens { Int value = 1; Int run() = value; }" + service.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(document.uri, "xtc", 1, source)), + ) + val before = service.semanticTokensFull(SemanticTokensParams(document)).get(30, SECONDS) + assertThat(before.data).isNotEmpty() + val unchanged = + service + .semanticTokensFullDelta(SemanticTokensDeltaParams(document, before.resultId)) + .get(30, SECONDS) + assertThat(unchanged.right.edits).isEmpty() + service.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(document.uri, 2), + listOf(TextDocumentContentChangeEvent("\r\n$source")), + ), + ) + val delta = + service + .semanticTokensFullDelta(SemanticTokensDeltaParams(document, before.resultId)) + .get(30, SECONDS) + .right + assertThat(delta.edits).hasSize(1) + assertThat(delta.edits.single().data).containsExactly(1) + val range = + service + .semanticTokensRange( + SemanticTokensRangeParams(document, Range(Position(1, 0), Position(2, 0))), + ).get(30, SECONDS) + assertThat(range.data) + .isEqualTo( + service.semanticTokensFull(SemanticTokensParams(document)).get(30, SECONDS).data, + ) + service.didClose(DidCloseTextDocumentParams(document)) + service.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(document.uri, "xtc", 1, source)), + ) + assertThat( + service + .semanticTokensFullDelta( + SemanticTokensDeltaParams(document, before.resultId), + ).get(30, SECONDS) + .isLeft, + ).isTrue() + verify(client, timeout(5000).atLeastOnce()).refreshSemanticTokens() + } + } + + @Test + fun `unnegotiated extension requests are refused without changing full tokens`() { + CompilerTestSupport.configure() + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + val capabilities = server.initialize(InitializeParams()).get().capabilities + assertThat(capabilities.semanticTokensProvider.full.left).isTrue() + assertThat(capabilities.semanticTokensProvider.range).isNull() + assertThatThrownBy { + server.textDocumentService + .semanticTokensFullDelta( + SemanticTokensDeltaParams( + TextDocumentIdentifier("file:///Tokens.x"), + "old", + ), + ).get(10, SECONDS) + }.hasMessageContaining("not negotiated") + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt new file mode 100644 index 00000000000..0e3bdf505bf --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt @@ -0,0 +1,2079 @@ +package org.xvm.lsp.server + +import com.google.gson.Gson +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.CallHierarchyIncomingCallsParams +import org.eclipse.lsp4j.CallHierarchyOutgoingCallsParams +import org.eclipse.lsp4j.CallHierarchyPrepareParams +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.Color +import org.eclipse.lsp4j.ColorPresentationParams +import org.eclipse.lsp4j.CompletionItemKind +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.ConfigurationParams +import org.eclipse.lsp4j.DefinitionParams +import org.eclipse.lsp4j.DiagnosticCapabilities +import org.eclipse.lsp4j.DidChangeTextDocumentParams +import org.eclipse.lsp4j.DidCloseTextDocumentParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.DocumentColorParams +import org.eclipse.lsp4j.DocumentDiagnosticParams +import org.eclipse.lsp4j.DocumentHighlightParams +import org.eclipse.lsp4j.DocumentLinkCapabilities +import org.eclipse.lsp4j.DocumentLinkParams +import org.eclipse.lsp4j.DocumentRangesFormattingParams +import org.eclipse.lsp4j.DocumentSymbolCapabilities +import org.eclipse.lsp4j.DocumentSymbolParams +import org.eclipse.lsp4j.FoldingRangeRequestParams +import org.eclipse.lsp4j.FormattingOptions +import org.eclipse.lsp4j.HoverParams +import org.eclipse.lsp4j.ImplementationParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InitializedParams +import org.eclipse.lsp4j.InlayHintParams +import org.eclipse.lsp4j.InlineCompletionCapabilities +import org.eclipse.lsp4j.InlineCompletionContext +import org.eclipse.lsp4j.InlineCompletionParams +import org.eclipse.lsp4j.InlineCompletionTriggerKind +import org.eclipse.lsp4j.MessageActionItem +import org.eclipse.lsp4j.MessageParams +import org.eclipse.lsp4j.MonikerKind +import org.eclipse.lsp4j.MonikerParams +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.ProgressParams +import org.eclipse.lsp4j.PublishDiagnosticsCapabilities +import org.eclipse.lsp4j.PublishDiagnosticsParams +import org.eclipse.lsp4j.Range +import org.eclipse.lsp4j.ReferenceContext +import org.eclipse.lsp4j.ReferenceParams +import org.eclipse.lsp4j.RenameParams +import org.eclipse.lsp4j.SelectionRangeParams +import org.eclipse.lsp4j.SemanticTokensCapabilities +import org.eclipse.lsp4j.SemanticTokensClientCapabilitiesRequests +import org.eclipse.lsp4j.SemanticTokensClientCapabilitiesRequestsFull +import org.eclipse.lsp4j.SemanticTokensDeltaParams +import org.eclipse.lsp4j.SemanticTokensParams +import org.eclipse.lsp4j.SemanticTokensRangeParams +import org.eclipse.lsp4j.ShowMessageRequestParams +import org.eclipse.lsp4j.SignatureHelpCapabilities +import org.eclipse.lsp4j.SignatureHelpParams +import org.eclipse.lsp4j.SignatureInformationCapabilities +import org.eclipse.lsp4j.SynchronizationCapabilities +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentContentCapabilities +import org.eclipse.lsp4j.TextDocumentContentChangeEvent +import org.eclipse.lsp4j.TextDocumentContentParams +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.TextDocumentSaveReason +import org.eclipse.lsp4j.TextDocumentSyncKind +import org.eclipse.lsp4j.TypeDefinitionParams +import org.eclipse.lsp4j.TypeHierarchyPrepareParams +import org.eclipse.lsp4j.TypeHierarchySubtypesParams +import org.eclipse.lsp4j.TypeHierarchySupertypesParams +import org.eclipse.lsp4j.VersionedTextDocumentIdentifier +import org.eclipse.lsp4j.WillSaveTextDocumentParams +import org.eclipse.lsp4j.WorkspaceClientCapabilities +import org.eclipse.lsp4j.WorkspaceDiagnosticParams +import org.eclipse.lsp4j.WorkspaceEditCapabilities +import org.eclipse.lsp4j.jsonrpc.Launcher +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.Either +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.eclipse.lsp4j.jsonrpc.services.JsonRequest +import org.eclipse.lsp4j.services.LanguageClient +import org.eclipse.lsp4j.services.LanguageServer +import org.junit.jupiter.api.Tag +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import java.nio.file.Files +import java.nio.file.Path +import java.util.Properties +import java.util.concurrent.CompletableFuture +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.ExecutionException +import java.util.concurrent.Executors +import java.util.concurrent.Future +import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit.NANOSECONDS +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.TimeoutException +import java.util.jar.JarEntry +import java.util.jar.JarFile +import java.util.jar.JarOutputStream + +/** + * Process tests consume the actual fat JAR, including its manifest, resources and logging setup. + */ +@Tag("compiler-stdio") +class XdkStdioTest { + interface ServiceStatusServer : LanguageServer { + @JsonRequest("xtc/languageServiceStatus") + fun languageServiceStatus(): CompletableFuture> + } + + @TempDir lateinit var directory: Path + + @Test + fun `packaged color prototype uses the opt in launcher and round trips picker edits`() { + val source = + """ + module ColorPrototype { + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + Rgba sample() = new Rgba(255, 128, 0); + } + """.trimIndent() + Session(packagedJar(), directory, colorPrototype = true).use { session -> + session.initialize() + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val before = session.await(service.documentColor(DocumentColorParams(document))).single() + assertThat(before.color).isEqualTo(Color(1.0, 128 / 255.0, 0.0, 1.0)) + val wanted = Color(0.0, 0.0, 1.0, 128 / 255.0) + val edit = session.await(service.colorPresentation(ColorPresentationParams(document, wanted, before.range))).single().textEdit + assertThat(edit.newText).isEqualTo("new Rgba(0, 0, 255, alpha = 128)") + assertThat(edit.range).isEqualTo(before.range) + session.change(source.replace("new Rgba(255, 128, 0)", edit.newText), 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + assertThat(session.await(service.documentColor(DocumentColorParams(document))).single().color).isEqualTo(wanted) + session.shutdownAndExit() + } + } + + @Test + fun `packaged inline completion preserves UTF16 positions and source suffixes`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + val text = + """ + module Stdio { + void run() { + Int answer = 42; + /* 😀 */ Int result = ans; + } + } + """.trimIndent() + session.open(text) + val line = text.lines()[3] + val at = Position(3, line.indexOf("ans;") + 3) + val result = + session + .await( + session.server.textDocumentService.inlineCompletion( + InlineCompletionParams( + TextDocumentIdentifier(URI), + at, + InlineCompletionContext(InlineCompletionTriggerKind.Automatic), + ), + ), + ).right.items + .single() + assertThat(result.insertText.left).isEqualTo("answer") + assertThat(result.range.start).isEqualTo(Position(3, at.character - 3)) + assertThat(result.range.end).isEqualTo(at) + assertThat(result.command).isNull() + } + } + + @Test + fun `packaged library content and direct monikers use negotiated immutable virtual documents`() { + Session(packagedJar(), directory).use { session -> + session.initialize(libraryContent = true) + val text = "module Stdio { String text = \"value\"; }" + session.open(text) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val target = + session + .await( + documents.definition(DefinitionParams(TextDocumentIdentifier(URI), Position(0, text.indexOf("String")))), + ).left + .single() + assertThat(target.uri).startsWith("ecstasy-library:") + val workspace = session.server.workspaceService + val content = session.await(workspace.textDocumentContent(TextDocumentContentParams(target.uri))).text + assertThat(content.lines()[target.range.start.line]).contains("const String") + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(target.uri, "xtc", 1, content))) + val imported = + session + .await( + documents.moniker(MonikerParams(TextDocumentIdentifier(URI), Position(0, text.indexOf("String")))), + ).single() + val exported = session.await(documents.moniker(MonikerParams(TextDocumentIdentifier(target.uri), target.range.start))).single() + assertThat(exported.identifier).isEqualTo(imported.identifier) + assertThat(exported.kind).isEqualTo(MonikerKind.Export) + assertThatThrownBy { session.await(workspace.textDocumentContent(TextDocumentContentParams("ecstasy-library:///unknown.x"))) } + .hasCauseInstanceOf(ResponseErrorException::class.java) + session.shutdownAndExit() + } + } + + @Test + fun `packaged monikers round trip stable identities partial results and changed artifacts`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + val text = "module Stdio { Int value = 1; }" + session.open(text) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val params = MonikerParams(TextDocumentIdentifier(URI), Position(0, text.indexOf("value"))) + val documents = session.server.textDocumentService + val first = session.await(documents.moniker(params)).single() + assertThat(first.scheme).isEqualTo("ecstasy-artifact-v1") + assertThat(first.kind).isEqualTo(MonikerKind.Export) + params.partialResultToken = Either.forLeft("moniker-partial") + assertThat(session.await(documents.moniker(params))).isEmpty() + val parts = session.progress.filter { it.token.left == "moniker-partial" } + assertThat(parts).hasSize(1) + assertThat(Gson().toJsonTree(parts.single().value.right).asJsonArray.single()) + .isEqualTo(Gson().toJsonTree(first)) + params.partialResultToken = null + session.change(text, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + assertThat(session.await(documents.moniker(params))).containsExactly(first) + session.change(text.replace("= 1", "= 2"), 3) + assertThat(session.diagnosticsAt(3).diagnostics).isEmpty() + assertThat(session.await(documents.moniker(params)).single().identifier).isNotEqualTo(first.identifier) + session.change("module Stdio { Missing value; }", 4) + assertThat(session.diagnosticsAt(4).diagnostics).isNotEmpty() + params.position = Position(0, 23) + assertThat(session.await(documents.moniker(params))).isEmpty() + session.shutdownAndExit() + } + } + + @Test + fun `separate connections isolate identical document URIs progress tokens and shutdown`() { + val jar = packagedJar() + Session(jar, Files.createDirectory(directory.resolve("first"))).use { first -> + Session(jar, Files.createDirectory(directory.resolve("second"))).use { second -> + first.initialize() + second.initialize() + val other = "module Stdio { String run() { String value = \"two\"; return value; } }" + first.open(VALID) + second.open(other) + assertThat(first.diagnosticsAt(1).diagnostics).isEmpty() + assertThat(second.diagnosticsAt(1).diagnostics).isEmpty() + assertThat(first.status()["pid"]).isNotEqualTo(second.status()["pid"]) + + fun references( + session: Session, + source: String, + ) = session.server.textDocumentService.references( + ReferenceParams(TextDocumentIdentifier(URI), Position(0, source.indexOf("value")), ReferenceContext(true)).apply { + workDoneToken = Either.forLeft("same-token") + partialResultToken = Either.forLeft("same-partial-token") + }, + ) + val one = references(first, VALID) + val two = references(second, other) + first.await(one) + second.await(two) + assertThat(first.progress.filter { it.token.left == "same-partial-token" }).isNotEmpty() + assertThat(second.progress.filter { it.token.left == "same-partial-token" }).isNotEmpty() + first.shutdownAndExit() + // The other connection still owns its buffer, semantic state and transport. + val hover = + second.await( + second.server.textDocumentService.hover( + HoverParams(TextDocumentIdentifier(URI), Position(0, other.indexOf("value"))), + ), + ) + assertThat(hover.contents.right.value).contains("String") + second.change(VALID, 2) + assertThat(second.diagnosticsAt(2).diagnostics).isEmpty() + second.verifySemantics(VALID, "value", "Int") + second.shutdownAndExit() + } + } + } + + @Test + fun `unknown methods and malformed parameters cannot kill the packaged message reader`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(VALID) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + listOf( + Triple("xtc/unknownMethod", emptyMap(), ResponseErrorCode.MethodNotFound), + Triple( + "textDocument/hover", + mapOf( + "textDocument" to mapOf("uri" to URI), + "position" to mapOf("line" to "not-a-number", "character" to 0), + ), + // TODO LSP4IJ: UP15 — underlying LSP4J 1.0.0 labels valid JSON with bad parameter + // types ParseError. Switch to InvalidParams after its decoder is repaired; + // retain reader-recovery checks. See docs/errs-upstream-issues.md (UP15). + ResponseErrorCode.ParseError, + ), + ).forEach { (method, params, code) -> + val failure = assertThrows { session.await(session.request(method, params)) } + assertThat(failure.cause).isInstanceOf(ResponseErrorException::class.java) + assertThat((failure.cause as ResponseErrorException).responseError.code).isEqualTo(code.value) + session.verifySemantics(VALID, "value", "Int") + } + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `optional signature and link fields follow negotiated capabilities over stdio`(richPresentation: Boolean) { + Session(packagedJar(), directory).use { session -> + session.initialize(richPresentation = richPresentation) + val source = + """ + module Stdio { + Int read(Int first, Int second) = first + second; + void run() { Int result = read(1, 2); } + // https://xtclang.org/ + } + """.trimIndent() + session.open(source) + val documents = session.server.textDocumentService + val id = TextDocumentIdentifier(URI) + val help = + session.await( + documents.signatureHelp( + SignatureHelpParams(id, Position(2, source.lines()[2].indexOf("2)"))), + ), + )!! + assertThat(help.activeParameter).isEqualTo(1) + assertThat(help.signatures.single().activeParameter) + .isEqualTo(1.takeIf { richPresentation }) + val link = session.await(documents.documentLink(DocumentLinkParams(id))).single() + val resolved = + if (richPresentation) { + assertThat(link.target).isNull() + session.await(documents.documentLinkResolve(link)).also { + assertThat(it.tooltip).isNotBlank() + } + } else { + link.also { assertThat(it.tooltip).isNull() } + } + assertThat(resolved.target).isEqualTo("https://xtclang.org/") + session.shutdownAndExit() + } + } + + @Test + fun `partial references and workspace diagnostics cross the packaged transport without duplicate final items`( + @TempDir directory: Path, + ) { + Session(packagedJar(), directory).use { session -> + session.initialize(pullDiagnostics = true) + val source = "module Stdio { Int value = 1; Int copy = value; Int run() = value; }" + session.open(source) + val params = + ReferenceParams( + TextDocumentIdentifier(URI), + Position(0, source.lastIndexOf("value")), + ReferenceContext(true), + ).apply { partialResultToken = Either.forLeft("references") } + assertThat(session.await(session.server.textDocumentService.references(params))) + .isEmpty() + val references = + session.progress + .filter { it.token.left == "references" } + .flatMap { Gson().toJsonTree(it.value.right).asJsonArray.toList() } + assertThat(references).hasSize(3) + assertThat(references.map { it.asJsonObject["uri"].asString }).containsOnly(URI) + val diagnostics = + WorkspaceDiagnosticParams().apply { partialResultToken = Either.forRight(17) } + assertThat(session.await(session.server.workspaceService.diagnostic(diagnostics)).items) + .isEmpty() + val reports = + session.progress + .filter { it.token.isRight && it.token.right == 17 } + .flatMap { + Gson() + .toJsonTree(it.value.right) + .asJsonObject["items"] + .asJsonArray + .toList() + } + assertThat(reports).hasSize(1) + assertThat(reports.single().asJsonObject["uri"].asString).isEqualTo(URI) + assertThat(reports.single().asJsonObject["items"].asJsonArray).isEmpty() + session.shutdownAndExit() + } + } + + @Test + fun `packaged transport rejects requests outside the initialized lifecycle`() { + Session(packagedJar(), directory).use { session -> + fun rejected( + result: CompletableFuture<*>, + code: ResponseErrorCode, + ) { + val failure = assertThrows { session.await(result) } + val error = failure.cause as ResponseErrorException + assertThat(error.responseError.code).isEqualTo(code.value) + } + + val hover = HoverParams(TextDocumentIdentifier(URI), Position(0, 0)) + rejected( + session.server.textDocumentService.hover(hover), + ResponseErrorCode.ServerNotInitialized, + ) + session.initialize() + rejected( + session.server.initialize(InitializeParams()), + ResponseErrorCode.InvalidRequest, + ) + session.open(VALID) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + session.await(session.server.shutdown()) + rejected( + session.server.textDocumentService.hover(hover), + ResponseErrorCode.InvalidRequest, + ) + session.server.exit() + session.expectExit(0) + } + } + + @Test + fun `incremental patches save hooks and formatting ranges round trip the packaged transport`() { + Session(packagedJar(), directory).use { session -> + session.initialize(documentSync = mapOf("incremental" to true, "formatOnSave" to true)) + val status = session.status() + assertThat(status["textSynchronization"].asString).isEqualTo("incremental") + assertThat(status["serverSaveFormatting"].asBoolean).isTrue() + assertThat(status["pid"].asLong).isPositive() + assertThat(status["compilerQueue"].asJsonObject["queueSize"].asInt).isZero() + val heap = status["heap"].asJsonObject + assertThat(heap["usedBytes"].asLong) + .isPositive() + .isLessThanOrEqualTo(heap["committedBytes"].asLong) + assertThat(heap["committedBytes"].asLong).isLessThanOrEqualTo(heap["maxBytes"].asLong) + val source = "module Stdio { // 😀\r\nInt value = 1;\r\n}" + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val id = TextDocumentIdentifier(URI) + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, 2), + listOf( + TextDocumentContentChangeEvent().apply { + range = Range(Position(1, 0), Position(1, 3)) + text = "String" + }, + TextDocumentContentChangeEvent().apply { + range = Range(Position(1, 15), Position(1, 16)) + text = "\"text\"" + }, + ), + ), + ) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + val save = WillSaveTextDocumentParams(id, TextDocumentSaveReason.Manual) + documents.willSave(save) + val saved = session.await(documents.willSaveWaitUntil(save)) + assertThat(saved.map { it.newText }).contains(" ", "\r\n") + val ranges = + listOf(Range(Position(1, 0), Position(2, 0)), Range(Position(1, 0), Position(1, 5))) + val edits = + session.await( + documents.rangesFormatting( + DocumentRangesFormattingParams(id, FormattingOptions(4, true), ranges), + ), + ) + assertThat(edits).hasSize(1) + assertThat(edits.single().newText).isEqualTo(" ") + // Resolving save edits never applies them; a second request sees the same source. + assertThat(session.await(documents.willSaveWaitUntil(save))).isEqualTo(saved) + session.shutdownAndExit() + } + } + + @Test + fun `semantic token range and deltas round trip the packaged transport`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + val source = "module Stdio { Int answer() = 42; }" + session.open(source) + val documents = session.server.textDocumentService + val id = TextDocumentIdentifier(URI) + val full = session.await(documents.semanticTokensFull(SemanticTokensParams(id))) + assertThat(full.data).isNotEmpty() + session.change("\n$source", 2) + val delta = + session + .await( + documents.semanticTokensFullDelta( + SemanticTokensDeltaParams(id, full.resultId), + ), + ).right + val patched = full.data.toMutableList() + delta.edits + .sortedByDescending { it.start } + .forEach { edit -> + patched.subList(edit.start, edit.start + edit.deleteCount).clear() + patched.addAll(edit.start, edit.data.orEmpty()) + } + assertThat(patched) + .isEqualTo( + session.await(documents.semanticTokensFull(SemanticTokensParams(id))).data, + ) + assertThat( + session + .await( + documents.semanticTokensRange( + SemanticTokensRangeParams(id, Range(Position(1, 0), Position(2, 0))), + ), + ).data, + ).isEqualTo(patched) + documents.didClose(DidCloseTextDocumentParams(id)) + session.open(source) + assertThat( + session + .await( + documents.semanticTokensFullDelta( + SemanticTokensDeltaParams(id, full.resultId), + ), + ).isLeft, + ).isTrue() + } + } + + @Test + fun `configured resource roots and resource-only changes round trip through packaged diagnostics`() { + val source = + directory.resolve("Assets.x").toFile().apply { + writeText("module Assets { static String text() = $./data.txt; }") + } + val root = directory.resolve("custom-resources").toFile().apply { mkdirs() } + val resource = root.resolve("data.txt").apply { writeText("template") } + Session(packagedJar(), directory).use { session -> + session.initialize( + pullDiagnostics = true, + sourceModules = + listOf( + mapOf( + "name" to "Assets", + "uri" to source.toURI().toString(), + "resourceRoots" to listOf(root.toURI().toString()), + ), + ), + ) + val params = DocumentDiagnosticParams(TextDocumentIdentifier(source.toURI().toString())) + + fun pull() = session.await(session.server.textDocumentService.diagnostic(params)).left + assertThat(pull().items).isEmpty() + Files.delete(resource.toPath()) + assertThat(pull().items.map { it.code.left }).contains("PARSER-24") + resource.writeText("repaired") + assertThat(pull().items).isEmpty() + } + } + + @Test + fun `packaged server records queue contents compiler calls and reply latency`() { + val traceDirectory = + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open("module Stdio { Int answer() = 42; }") + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + session.await( + session.server.textDocumentService.documentSymbol( + DocumentSymbolParams(TextDocumentIdentifier(URI)), + ), + ) + session.traceDirectory + } + val entries = + Files.list(traceDirectory).use { files -> + files + .filter { it.fileName.toString().startsWith("lsp-trace-") } + .toList() + .flatMap { Files.readAllLines(it) } + .map { JsonParser.parseString(it).asJsonObject } + } + assertThat(entries).isNotEmpty() + val jobs = entries.filter { it["kind"].asString == "compiler-queue" } + assertThat(jobs).anySatisfy { + assertThat(it["event"].asString).isEqualTo("queued") + assertThat(it["queueSize"].asInt).isPositive() + assertThat(it["queuedJobs"].toString()).contains("compile", "Stdio.x") + } + assertThat(entries).anySatisfy { + assertThat(it["operation"]?.asString).startsWith("EmbeddingSupport.compileModule") + assertThat(it["event"].asString).isEqualTo("end") + assertThat(it["elapsedMs"].asDouble).isPositive() + } + assertThat(entries).anySatisfy { + assertThat(it["operation"]?.asString).isEqualTo("textDocument/documentSymbol") + assertThat(it["event"].asString).isEqualTo("end") + assertThat(it["boundary"].asString).isEqualTo("server-reply-written") + } + assertThat(entries.toString()).doesNotContain("Int answer() = 42") + } + + @Test + fun `property implementations round trip fields and accessors through the packaged server`() { + val source = + "module Stdio { interface Named { @RO String name; } " + + "class Stored implements Named { @Override String name = \"stored\"; } " + + "class Computed implements Named { @Override String name.get() = \"computed\"; } " + + "class Unrelated { String name = \"other\"; } Int size(String text) = text.size; }" + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val id = TextDocumentIdentifier(URI) + + fun lookup(at: Int) = + session + .await(documents.implementation(ImplementationParams(id, Position(0, at)))) + .left + val targets = lookup(source.indexOf("name;")) + assertThat(targets.map { it.uri }).containsOnly(URI) + assertThat(targets.map { it.range }) + .containsExactlyInAnyOrder( + Range( + Position(0, source.indexOf("name =")), + Position(0, source.indexOf("name =") + 4), + ), + Range( + Position(0, source.indexOf("get()")), + Position(0, source.indexOf("get()") + 3), + ), + ) + assertThat(lookup(source.lastIndexOf("size"))).isEmpty() + session.change("module Stdio {", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isNotEmpty() + assertThat(lookup(7)).isEmpty() + session.change(source, 3) + assertThat(session.diagnosticsAt(3).diagnostics).isEmpty() + assertThat(lookup(source.indexOf("name;"))).isEqualTo(targets) + session.shutdownAndExit() + } + } + + @Test + fun `private parameter rename round trips versioned edits and rejects silent capture`() { + val source = + "module Stdio { private Int pick(Int input) = input; Int run() = pick(input = 1); }" + Session(packagedJar(), directory).use { session -> + session.initialize(versionedEdits = true) + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val result = + requireNotNull( + session.await( + documents.rename( + RenameParams( + document, + Position(0, source.lastIndexOf("input")), + "value", + ), + ), + ), + ) + // LSP4J reconstructs its default empty map when the wire omits the changes member. + assertThat(result.changes).isNullOrEmpty() + val edit = result.documentChanges.single().left + assertThat(edit.textDocument.uri).isEqualTo(URI) + assertThat(edit.textDocument.version).isEqualTo(1) + assertThat(edit.edits.map { it.left.newText }) + .containsExactly("value", "value", "value") + val changed = + edit.edits + .map { it.left } + .sortedByDescending { it.range.start.character } + .fold(source) { text, change -> + text.replaceRange( + change.range.start.character, + change.range.end.character, + change.newText, + ) + } + session.change(changed, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + val capture = + "module Stdio { Int value = 10; Int run() { Int local = 1; return local + value; } }" + session.change(capture, 3) + assertThat(session.diagnosticsAt(3).diagnostics).isEmpty() + assertThat( + session.await( + documents.rename( + RenameParams(document, Position(0, capture.indexOf("local")), "value"), + ), + ), + ).isNull() + } + } + + @Test + fun `compiler call hierarchy tokens and hints round trip and reject stale items`() { + val source = + "module Stdio { static Int leaf(Int input) = input; Int run() { var n = leaf(1); return n; } }" + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val leaf = + session + .await( + documents.prepareCallHierarchy( + CallHierarchyPrepareParams( + document, + Position(0, source.indexOf("leaf")), + ), + ), + ).single() + val incoming = + session + .await( + documents.callHierarchyIncomingCalls(CallHierarchyIncomingCallsParams(leaf)), + ).single() + assertThat(incoming.from.name).isEqualTo("run") + assertThat(incoming.fromRanges) + .containsExactly( + Range( + Position(0, source.lastIndexOf("leaf")), + Position( + 0, + source.lastIndexOf("leaf") + 4, + ), + ), + ) + val outgoing = + session + .await( + documents.callHierarchyOutgoingCalls( + CallHierarchyOutgoingCallsParams(incoming.from), + ), + ).single() + assertThat(outgoing.to).isEqualTo(leaf) + val hints = + session.await( + documents.inlayHint( + InlayHintParams(document, Range(Position(0, 0), Position(1, 0))), + ), + ) + assertThat(hints.map { it.label.left }).containsExactly(": Int", "input:") + assertThat( + session.await(documents.semanticTokensFull(SemanticTokensParams(document))).data, + ).isNotEmpty() + session.change("\n$source", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + assertThat( + session.await( + documents.callHierarchyIncomingCalls(CallHierarchyIncomingCallsParams(leaf)), + ), + ).isEmpty() + assertThat( + session.await( + documents.callHierarchyOutgoingCalls( + CallHierarchyOutgoingCallsParams(incoming.from), + ), + ), + ).isEmpty() + } + } + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `bundled compiler handles edits and shutdown without an external XDK`(invalidXdkHome: Boolean) { + Session(packagedJar(), directory, invalidXdkHome).use { session -> + session.initialize() + session.open(BROKEN) + val errors = session.diagnosticsAt(1).diagnostics + assertThat(errors).isNotEmpty() + assertThat(errors.map { it.code.left }) + .doesNotContain("XDK-UNAVAILABLE", "ANALYSIS-FAILED", "EMB-5") + assertThat(errors.any { it.message.left.contains("missing") }).isTrue() + + for (version in 2..101) { + session.change(if (version == 101) VALID else BROKEN, version) + } + assertThat(session.diagnosticsAt(101).diagnostics).isEmpty() + val symbols = + session.await( + session.server.textDocumentService.documentSymbol( + DocumentSymbolParams(TextDocumentIdentifier(URI)), + ), + ) + assertThat(symbols).isNotEmpty() + session.verifySemantics(VALID, "value", "Int") + + session.server.textDocumentService.didClose( + DidCloseTextDocumentParams(TextDocumentIdentifier(URI)), + ) + assertThat(session.diagnosticsAt(101).diagnostics).isEmpty() + session.open(REOPENED) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + session.verifySemantics(REOPENED, "label", "String") + + for (version in 2..20) session.change(BROKEN, version) + session.shutdownAndExit() + } + } + + @Test + fun `packaged module diagnostics cross file definitions and hierarchy round trip over stdio`() { + directory = directory.toRealPath() + val root = directory.resolve("Multi.x").toFile() + val member = directory.resolve("Multi/Child.x").toFile() + val source = + "module Multi { interface Named { String name(); } " + + "class Base implements Named { @Override String name() = \"base\"; } Child make() = new Child(); }" + root.writeText(source) + member.parentFile.mkdirs() + member.writeText("class Child extends Base { @Override String name() = \"child\"; }") + Session(packagedJar(), directory).use { session -> + session.initialize() + val rootId = TextDocumentIdentifier(root.toURI().toString()) + val memberId = TextDocumentIdentifier(member.toURI().toString()) + val documents = session.server.textDocumentService + documents.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(rootId.uri, "xtc", 1, source)), + ) + assertThat(session.diagnosticsFor(memberId.uri, null).diagnostics).isEmpty() + val definition = + session + .await( + documents.definition( + DefinitionParams(rootId, Position(0, source.indexOf("Child"))), + ), + ).left + .single() + assertThat(definition.uri).isEqualTo(memberId.uri) + val typeDefinition = + session + .await( + documents.typeDefinition( + TypeDefinitionParams(rootId, Position(0, source.indexOf("make"))), + ), + ).left + .single() + assertThat(typeDefinition.uri).isEqualTo(memberId.uri) + assertThat(typeDefinition.range).isEqualTo(Range(Position(0, 6), Position(0, 11))) + for (name in listOf("Named", "name")) { + val implementations = + session + .await( + documents.implementation( + ImplementationParams(rootId, Position(0, source.indexOf(name))), + ), + ).left + assertThat(implementations.map { it.uri }) + .containsExactlyInAnyOrder(rootId.uri, memberId.uri) + } + val base = + session + .await( + documents.prepareTypeHierarchy( + TypeHierarchyPrepareParams(rootId, Position(0, source.indexOf("Base"))), + ), + ).single() + val child = + session + .await(documents.typeHierarchySubtypes(TypeHierarchySubtypesParams(base))) + .single() + assertThat(child.uri).isEqualTo(memberId.uri) + assertThat( + session + .await( + documents.typeHierarchySupertypes(TypeHierarchySupertypesParams(child)), + ).single() + .uri, + ).isEqualTo(rootId.uri) + documents.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem( + memberId.uri, + "xtc", + 7, + "class Child extends Base { MissingType absent; }", + ), + ), + ) + assertThat(session.diagnosticsFor(memberId.uri, 7).diagnostics).anyMatch { + it.code.left == "COMPILER-38" + } + documents.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(memberId.uri, 8), + listOf(TextDocumentContentChangeEvent(member.readText())), + ), + ) + assertThat(session.diagnosticsFor(memberId.uri, 8).diagnostics).isEmpty() + assertThat( + session.await( + documents.typeHierarchySubtypes(TypeHierarchySubtypesParams(base)), + ), + ).isEmpty() + session.shutdownAndExit() + } + } + + @Test + fun `type definition preserves multiple union targets over stdio and follows narrowing`() { + val source = + """ + module Stdio { + class First {} + class Second {} + void run(First|Second value) { + value.toString(); + if (value.is(First)) { value.toString(); } + } + } + """.trimIndent() + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(source) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val documents = session.server.textDocumentService + val id = TextDocumentIdentifier(URI) + val union = + session + .await(documents.typeDefinition(TypeDefinitionParams(id, Position(4, 8)))) + .left + assertThat(union.map { it.range.start.line }).containsExactly(1, 2) + assertThat(union.map { it.uri }).containsOnly(URI) + val narrowedPosition = Position(5, source.lines()[5].lastIndexOf("value")) + val narrowed = + session + .await(documents.typeDefinition(TypeDefinitionParams(id, narrowedPosition))) + .left + assertThat(narrowed).containsExactly(union.first()) + session.shutdownAndExit() + } + } + + @Test + fun `Java only compiler preserves structure through incomplete edits over stdio`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(VALID) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + val text = "module Stdio {\n void editing() {\n /* café 😀 */ console." + session.change(text, 2) + val errors = session.diagnosticsAt(2).diagnostics + assertThat(errors).isNotEmpty() + assertThat(errors.map { it.code.left }).doesNotContain("ANALYSIS-FAILED", "EMB-5") + val document = TextDocumentIdentifier(URI) + val service = session.server.textDocumentService + val symbols = session.await(service.documentSymbol(DocumentSymbolParams(document))) + assertThat( + symbols + .single() + .right.children + .map { it.name }, + ).containsExactly("editing") + val folds = session.await(service.foldingRange(FoldingRangeRequestParams(document))) + assertThat(folds).anySatisfy { + assertThat(it.startLine).isEqualTo(1) + assertThat(it.endLine).isEqualTo(2) + assertThat(it.endCharacter).isEqualTo(text.lines().last().length) + } + val cursor = Position(2, text.lines().last().length) + val selected = + session + .await(service.selectionRange(SelectionRangeParams(document, listOf(cursor)))) + .single() + assertThat(selected.range.end).isEqualTo(cursor) + assertThat(selected.range.start.line).isLessThan(2) + assertThat( + session + .await(service.definition(DefinitionParams(document, Position(1, 10)))) + .left, + ).isEmpty() + session.change(REOPENED, 3) + assertThat(session.diagnosticsAt(3).diagnostics).isEmpty() + session.verifySemantics(REOPENED, "label", "String") + session.shutdownAndExit() + } + assertThat(Files.readString(directory.resolve("stderr.log"))) + .doesNotContain("loadXtcLanguage", "TreeSitterAdapter") + } + + @Test + fun `recovered method fold boundaries survive transport without capturing following declarations`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(VALID) + assertThat(session.diagnosticsAt(1).diagnostics).isEmpty() + listOf("\n", "\r\n").forEachIndexed { index, newline -> + val closingLine = " /* 😀 */ } Int later = 1; }" + val text = + listOf( + "module Stdio {", + " void damaged(Int) {", + " Int hidden = 1;", + closingLine, + ).joinToString(newline) + val version = index + 2 + session.change(text, version) + assertThat(session.diagnosticsAt(version).diagnostics).isNotEmpty() + val folds = + session.await( + session.server.textDocumentService.foldingRange( + FoldingRangeRequestParams(TextDocumentIdentifier(URI)), + ), + ) + val method = folds.single { it.startLine == 1 } + assertThat(method.endLine).isEqualTo(3) + assertThat(method.startCharacter).isNull() + assertThat(method.endCharacter).isEqualTo(closingLine.indexOf('}')) + } + session.shutdownAndExit() + } + } + + @Test + fun `compiler completion and signature requests round trip over stdio`() { + val declarations = + "module Stdio { class Box { Element echo(Element value) { return value; } " + + "Int choose(Int n) { return n; } String choose(String text) { return text; } " + + "String label = \"box\"; private Int secret() { return 1; } } " + val prefix = "${declarations}void run(Box box) { box." + Session(packagedJar(), directory).use { session -> + session.initialize() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + session.open("$prefix } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val items = + session + .await( + service.completion(CompletionParams(document, Position(0, prefix.length))), + ).left + assertThat(items.map { it.label }) + .contains("echo", "choose", "label") + .doesNotContain("secret") + assertThat(items.single { it.label == "echo" }.detail) + .isEqualTo("String echo(String value)") + assertThat(items.filter { it.label == "choose" }).hasSize(2) + + val call = "${prefix}echo(" + session.change("$call } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isNotEmpty() + val help = + session.await( + service.signatureHelp(SignatureHelpParams(document, Position(0, call.length))), + ) + assertThat(help.signatures.single().label).isEqualTo("String echo(String value)") + assertThat(help.signatures.single().activeParameter).isZero() + assertThat( + help.signatures + .single() + .documentation.left, + ).contains("overload not selected") + + val overloaded = "${prefix}choose(" + session.change("$overloaded } }", 3) + assertThat(session.diagnosticsAt(3).diagnostics).isNotEmpty() + assertThat( + session + .await( + service.signatureHelp( + SignatureHelpParams(document, Position(0, overloaded.length)), + ), + ).signatures, + ).hasSize(2) + + val complete = + "module Stdio { T echo(T value, T backup) { return value; } " + + "void run() { String text = echo(backup = \"b\", value = \"a\"); } }" + session.change(complete, 4) + assertThat(session.diagnosticsAt(4).diagnostics).isEmpty() + val selected = + session.await( + service.signatureHelp( + SignatureHelpParams( + document, + Position( + 0, + complete.indexOf("backup = \"b\"") + "backup = \"b\"".length, + ), + ), + ), + ) + assertThat(selected.signatures.single().label) + .isEqualTo("String echo(String value, String backup)") + assertThat(selected.activeParameter).isEqualTo(1) + assertThat(selected.signatures.single().activeParameter).isEqualTo(1) + + session.change("$prefix } }", 5) + assertThat(session.diagnosticsAt(5).diagnostics).isNotEmpty() + repeat(10) { + service + .completion(CompletionParams(document, Position(0, prefix.length))) + .cancel(false) + } + val latest = + session + .await( + service.completion(CompletionParams(document, Position(0, prefix.length))), + ).left + assertThat(latest.map { it.label }).contains("echo") + session.shutdownAndExit() + } + assertThat(Files.readString(directory.resolve("stderr.log"))) + .doesNotContain("TreeSitterAdapter", "loadXtcLanguage") + } + + @Test + fun `completion documentation ordering and literal kinds round trip over stdio`() { + val header = + "module Stdio { /** Keeps the selected text. */ String choose(String value, String backup = \"\") = value; " + val prefix = "$header void run() { ch" + Session(packagedJar(), directory).use { session -> + session.initialize() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + session.open("$prefix; } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val items = + session + .await( + service.completion(CompletionParams(document, Position(0, prefix.length))), + ).left + val item = items.single { it.label == "choose" } + assertThat(item.documentation.left).contains("Keeps the selected text.") + assertThat(item.sortText).isNotBlank() + val call = "$header void run() { choose(value = " + session.change("$call); } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isNotEmpty() + val cursor = Position(0, call.length) + val values = session.await(service.completion(CompletionParams(document, cursor))).left + assertThat(values.single { it.label == "\"\"" }.kind) + .isEqualTo(CompletionItemKind.Value) + val signature = + session + .await(service.signatureHelp(SignatureHelpParams(document, cursor))) + .signatures + .single() + assertThat(signature.documentation.left) + .contains("Keeps the selected text.", "overload not selected") + assertThat(signature.activeParameter).isZero() + assertThat(signature.parameters[1].documentation.left).contains("Optional") + session.shutdownAndExit() + } + } + + @Test + fun `typed completion edits and auto-closed signature help round trip over stdio`() { + val prefix = + "module Stdio {\r\n Int run(Object value) {\r\n if (value.is(String)) { /* 😀 */ return value.si" + val text = "$prefix; } return 0; }\r\n}" + Session(packagedJar(), directory).use { session -> + session.initialize() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + session.open(text) + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val cursor = Position(2, prefix.lines().last().length) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + assertThat(items.map { it.label }).contains("size") + assertThat(items).allSatisfy { assertThat(it.label).startsWith("si") } + val edit = items.single { it.label == "size" }.textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(2, cursor.character - 2), cursor)) + assertThat(edit.newText).isEqualTo("size") + val changed = text.replaceRange(prefix.length - 2, prefix.length, edit.newText) + session.change(changed, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + + val call = "module Stdio { void run(String value) { value.indexOf(" + session.change("$call); } Int later() = 42; }", 3) + assertThat(session.diagnosticsAt(3).diagnostics).isNotEmpty() + val help = + session.await( + service.signatureHelp(SignatureHelpParams(document, Position(0, call.length))), + ) + assertThat(help.signatures).hasSize(2).allSatisfy { + assertThat(it.label).startsWith("conditional Int indexOf(") + assertThat(it.activeParameter).isZero() + assertThat(it.documentation.left).contains("overload not selected") + } + val completed = "$call\"x\"" + session.change("$completed); } Int later() = 42; }", 4) + assertThat(session.diagnosticsAt(4).diagnostics).isEmpty() + val selected = + session.await( + service.signatureHelp( + SignatureHelpParams(document, Position(0, completed.length)), + ), + ) + assertThat(selected.signatures).hasSize(1) + assertThat(selected.signatures.single().documentation).isNull() + assertThat(selected.signatures.single().activeParameter).isZero() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource( + strings = + ["(values[value.si", "(1, value.si", "[1 = value.si", "Tuple:(1, value.si"], + ) + fun `missing enclosing delimiters preserve cursor queries and ordinary diagnostics over stdio`(expression: String) { + val header = + "module Stdio { Int pair(Int first, Int second) = first; Object run(String value, Int[] values) { return " + Session(packagedJar(), directory).use { session -> + session.initialize() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val prefix = header + expression + session.open("$prefix; } }") + val diagnostics = session.diagnosticsAt(1).diagnostics + assertThat(diagnostics).isNotEmpty() + val cursor = Position(0, prefix.length) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + val edit = items.single { it.label == "size" }.textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(0, prefix.length - 2), cursor)) + + val call = "$header(values[pair(1, " + session.change("$call; } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isNotEmpty() + val help = + session.await( + service.signatureHelp(SignatureHelpParams(document, Position(0, call.length))), + ) + assertThat(help.signatures.single().label).isEqualTo("Int pair(Int first, Int second)") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + assertThat( + help.signatures + .single() + .documentation.left, + ).contains("overload not selected") + session.change("${call}2)]); } }", 3) + assertThat(session.diagnosticsAt(3).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "take(first = 1, second =", + "fn(1, ", + "new Box(", + "take(1, te", + "take(first = 1, second = te", + "fn(1, te", + "new Box(te", + "new Box(value = te", + "new String[2](te", + "new String[2](supply = te", + "Box box = new Box(te", + "new @Tagged Box(te", + ], + ) + fun `argument value edits round trip and clear diagnostics after acceptance`(call: String) { + val prefix = + "module Stdio { annotation Tagged into Object {} void take(Int first, String second) {} " + + "class Box { construct(T value) {} } " + + "void run(Int number, String text, Boolean flag, function Int(Int, String) fn) { Int textNumber = 1; /* 😀 */ $call" + val typed = if (call.endsWith("te")) 2 else 0 + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open("$prefix); } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val cursor = Position(0, prefix.length) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + assertThat(items.map { it.label }) + .containsExactlyInAnyOrderElementsOf( + if (typed == 0) { + listOf("text", "qualifiedName", "simpleName", "\"\"") + } else { + listOf("text") + }, + ) + val edit = items.single { it.label == "text" }.textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(0, prefix.length - typed), cursor)) + assertThat(edit.newText).isEqualTo("text") + val help = session.await(service.signatureHelp(SignatureHelpParams(document, cursor))) + assertThat( + help.signatures + .single() + .documentation.left, + ).contains( + if (call.startsWith("fn(")) { + "runtime target unknown" + } else { + "overload not selected" + }, + ) + if (call.startsWith("new String[2]")) { + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + } + session.change("${prefix.dropLast(typed)}${edit.newText}); } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "void damaged(Str§ value) {}", + "Str§ property;", + "void damaged(ecstasy.text.Str§ value) {}", + "ecstasy.text.Str§ property;", + "void damaged(Map> value) {}", + "List property;", + "void damaged((Int | Str§) value) {}", + "interface Damaged extends List {}", + ], + ) + fun `declaration type edits preserve UTF16 positions and clear diagnostics over stdio`(declaration: String) { + val prefix = "module Stdio {\r\n /* 😀 */ " + declaration.substringBefore('§') + val suffix = declaration.substringAfter('§') + "\r\n Int later = 1; }" + val selected = if (declaration.contains("ecstasy.text.")) "StringBuffer" else "String" + val column = prefix.substringAfterLast('\n').length + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(prefix + suffix) + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val cursor = Position(1, column) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + val edit = items.single { it.label == selected }.textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(1, column - 3), cursor)) + assertThat(edit.newText).isEqualTo(selected) + assertThat( + session + .await(service.signatureHelp(SignatureHelpParams(document, cursor))) + ?.signatures + .orEmpty(), + ).isEmpty() + session.change(prefix.dropLast(3) + edit.newText + suffix, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @Test + fun `generic header completions preserve whole token edits over stdio`() { + val variants = + listOf( + "List<§>" to "Element", + "List" to "Element", + "Owner.Ali§" to "Alias", + "List" to "String", + "Li§st" to "List", + "Li§" to "List", + ) + Session(packagedJar(), directory).use { session -> + session.initialize() + variants.forEachIndexed { index, (marked, selected) -> + val prefix = + "module Stdio {\r\n /* 😀 */ class Owner { typedef T as Alias; } " + + "class Container { void damaged(" + + marked.substringBefore('§') + val suffix = marked.substringAfter('§') + " value) {} } }" + val version = index * 2 + 1 + if (index == 0) { + session.open(prefix + suffix) + } else { + session.change(prefix + suffix, version) + } + session.diagnosticsAt(version) + val column = prefix.substringAfterLast('\n').length + val before = prefix.takeLastWhile { it.isLetterOrDigit() || it == '_' }.length + val after = suffix.takeWhile { it.isLetterOrDigit() || it == '_' }.length + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val cursor = Position(1, column) + val items = + session.await(service.completion(CompletionParams(document, cursor))).left + val edit = items.single { it.label == selected }.textEdit.left + assertThat(edit.range) + .isEqualTo(Range(Position(1, column - before), Position(1, column + after))) + assertThat(edit.newText).isEqualTo(selected) + assertThat( + session + .await(service.signatureHelp(SignatureHelpParams(document, cursor))) + ?.signatures + .orEmpty(), + ).isEmpty() + session.change( + prefix.dropLast(before) + edit.newText + suffix.drop(after), + version + 1, + ) + assertThat(session.diagnosticsAt(version + 1).diagnostics).isEmpty() + } + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource(strings = ["new Int[nu|]", "new Int[nu|", "new String[nu|](\"x\")"]) + fun `array size edits and signature fitting round trip over stdio`(expression: String) { + val prefix = + "module Stdio { void run(Int number, String numberText) { /* 😀 */ " + + expression.substringBefore('|') + val suffix = expression.substringAfter('|') + "; } }" + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(prefix + suffix) + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val cursor = Position(0, prefix.length) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + assertThat(items.map { it.label }).containsExactly("number") + val edit = items.single().textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(0, prefix.length - 2), cursor)) + assertThat(edit.newText).isEqualTo("number") + val help = session.await(service.signatureHelp(SignatureHelpParams(document, cursor))) + assertThat( + help.signatures + .single() + .parameters + .first() + .label.left, + ).isEqualTo("Int size") + assertThat(help.signatures.single().activeParameter).isZero() + val completedSuffix = if (']' in suffix) suffix else "]$suffix" + session.change(prefix.dropLast(2) + edit.newText + completedSuffix, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource( + strings = + [ + "new Box(\"x\", te|) { String read() = text; }", + "new Reader(\"x\", te|) { construct(String first, String second) {} @Override String read() = text; }", + ], + ) + fun `anonymous constructor edits and signatures round trip through the packaged server`(expression: String) { + val prefix = + "module Stdio { class Box { construct(T first, T second) {} } interface Reader { String read(); } " + + "void run(String text, Int textNumber) { /* 😀 */ " + + expression.substringBefore('|') + val suffix = expression.substringAfter('|') + "; } }" + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open(prefix + suffix) + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val cursor = Position(0, prefix.length) + val help = session.await(service.signatureHelp(SignatureHelpParams(document, cursor))) + assertThat(help.signatures.single().label) + .contains("String first, String second") + .doesNotContain(":1") + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + val items = session.await(service.completion(CompletionParams(document, cursor))).left + assertThat(items.map { it.label }).containsExactly("text") + val edit = items.single().textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(0, prefix.length - 2), cursor)) + session.change(prefix.dropLast(2) + edit.newText + suffix, 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "static "]) + fun `property and constant argument edits preserve receiver rules over stdio`(modifier: String) { + val prefix = + "module Stdio { class Values { String textProperty = \"x\"; static String textConstant = \"c\"; " + + "Int textNumber = 1; static void take(String value) {} ${modifier}void run() { take(value = te" + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open("$prefix); } } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val cursor = Position(0, prefix.length) + val items = + session + .await( + session.server.textDocumentService.completion( + CompletionParams(TextDocumentIdentifier(URI), cursor), + ), + ).left + assertThat(items.map { it.label }) + .containsExactlyInAnyOrderElementsOf( + if (modifier.isEmpty()) { + listOf("textProperty", "textConstant") + } else { + listOf("textConstant") + }, + ) + val selected = + items.single { + it.label == if (modifier.isEmpty()) "textProperty" else "textConstant" + } + assertThat(selected.detail).contains("String") + val edit = selected.textEdit.left + assertThat(edit.range).isEqualTo(Range(Position(0, prefix.length - 2), cursor)) + session.change("${prefix.dropLast(2)}${edit.newText}); } } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @Test + fun `scope completion and inferred named signatures round trip over stdio`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + val service = session.server.textDocumentService + val document = TextDocumentIdentifier(URI) + val prefix = "module Stdio { Int run(Int item) { return ite" + session.open("$prefix; } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val items = + session + .await( + service.completion(CompletionParams(document, Position(0, prefix.length))), + ).left + val item = items.single { it.label == "item" } + assertThat(item.detail).isEqualTo("Int item") + assertThat(item.textEdit.left.range) + .isEqualTo(Range(Position(0, prefix.length - 3), Position(0, prefix.length))) + session.change(prefix.dropLast(3) + item.textEdit.left.newText + "; } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + + val call = + "module Stdio { T pair(T first, T second) = first; " + + "void run() { pair(second = \"x\", first =" + session.change("$call); } }", 3) + assertThat(session.diagnosticsAt(3).diagnostics).isNotEmpty() + val help = + session.await( + service.signatureHelp(SignatureHelpParams(document, Position(0, call.length))), + ) + assertThat(help.signatures).hasSize(1) + assertThat(help.signatures.single().label) + .isEqualTo("String pair(String first, String second)") + assertThat(help.signatures.single().activeParameter).isZero() + assertThat( + help.signatures + .single() + .documentation.left, + ).contains("overload not selected") + session.change("$call\"y\"); } }", 4) + assertThat(session.diagnosticsAt(4).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource(strings = ["function", "constructor"]) + fun `incomplete function and constructor signatures round trip over stdio`(kind: String) { + val prefix = + if (kind == "function") { + "module Stdio { void run(function Int(Int, String) fn) { fn(1, " + } else { + "module Stdio { class Box { construct(Int first, String second) {} } void run() { new Box(1, " + } + Session(packagedJar(), directory).use { session -> + session.initialize() + session.open("$prefix); } }") + assertThat(session.diagnosticsAt(1).diagnostics).isNotEmpty() + val help = + session.await( + session.server.textDocumentService.signatureHelp( + SignatureHelpParams(TextDocumentIdentifier(URI), Position(0, prefix.length)), + ), + ) + assertThat(help.signatures.single().label) + .isEqualTo( + if (kind == "function") { + "Int fn(Int, String)" + } else { + "new Box(Int first, String second)" + }, + ) + assertThat(help.signatures.single().activeParameter).isEqualTo(1) + session.change("$prefix\"x\"); } }", 2) + assertThat(session.diagnosticsAt(2).diagnostics).isEmpty() + session.shutdownAndExit() + } + } + + @ParameterizedTest + @ValueSource(strings = ["", "in"]) + fun `module overlays feed completion and root edits invalidate member requests over stdio`(memberPrefix: String) { + directory = directory.toRealPath() + val root = directory.resolve("Multi.x").toFile() + val member = directory.resolve("Multi/Child.x").toFile() + root.writeText("module Multi { class Base { Int value = 1; } }") + member.parentFile.mkdirs() + val prefix = "class Child extends Base { Int run() { return value.$memberPrefix" + member.writeText("$prefix } }") + Session(packagedJar(), directory).use { session -> + session.initialize() + val rootId = TextDocumentIdentifier(root.toURI().toString()) + val memberId = TextDocumentIdentifier(member.toURI().toString()) + val service = session.server.textDocumentService + service.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem( + rootId.uri, + "xtc", + 1, + "module Multi { class Base { String value = \"overlay\"; } }", + ), + ), + ) + service.didOpen( + DidOpenTextDocumentParams( + TextDocumentItem(memberId.uri, "xtc", 1, member.readText()), + ), + ) + val before = + session + .await( + service.completion(CompletionParams(memberId, Position(0, prefix.length))), + ).left + assertThat(before.map { it.label }).contains("indexOf") + service.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(rootId.uri, 2), + listOf(TextDocumentContentChangeEvent(root.readText())), + ), + ) + val after = + session + .await( + service.completion(CompletionParams(memberId, Position(0, prefix.length))), + ).left + if (memberPrefix.isEmpty()) assertThat(after).isNotEmpty() + assertThat(after.map { it.label }).doesNotContain("indexOf") + assertThat(root.readText()).contains("Int value") + session.shutdownAndExit() + } + } + + @Test + fun `an invalid packaged backend setting fails startup explicitly`() { + val invalid = directory.resolve("invalid-setting.jar") + JarFile(packagedJar().toFile()).use { original -> + JarOutputStream(Files.newOutputStream(invalid)).use { output -> + for (entry in original.entries()) { + output.putNextEntry(JarEntry(entry.name)) + original.getInputStream(entry).use { input -> + if (entry.name == "lsp-version.properties") { + Properties().apply { + load(input) + setProperty("lsp.adapter", "compielr") + store(output, null) + } + } else { + input.copyTo(output) + } + } + output.closeEntry() + } + } + } + Session(invalid, directory).use { it.expectExit(1) } + assertThat(Files.readString(directory.resolve("stderr.log"))) + .contains("Unknown lsp.adapter 'compielr'; expected treesitter, compiler or mock") + } + + @Test + fun `a missing bundled bootstrap reports an analysis failure`() { + val damaged = directory.resolve("damaged.jar") + JarFile(packagedJar().toFile()).use { original -> + assertThat(original.getJarEntry(BOOTSTRAP)).isNotNull() + JarOutputStream(Files.newOutputStream(damaged)).use { output -> + for (entry in original.entries()) { + if (entry.name == BOOTSTRAP) continue + output.putNextEntry(JarEntry(entry.name)) + original.getInputStream(entry).use { it.copyTo(output) } + output.closeEntry() + } + } + } + Session(damaged, directory).use { session -> + session.initialize() + session.open(VALID) + val errors = session.diagnosticsAt(1).diagnostics + assertThat(errors.map { it.code.left }).containsExactly("ANALYSIS-FAILED") + session.shutdownAndExit() + } + assertThat(Files.readString(directory.resolve("stderr.log"))) + .contains("Bundled XDK resource is missing: mack.xtc") + } + + @Test + fun `exit without shutdown terminates with failure status`() { + Session(packagedJar(), directory).use { session -> + session.initialize() + session.server.exit() + session.expectExit(1) + } + } + + private fun packagedJar(): Path { + val jar = + Path.of( + requireNotNull(System.getProperty("xtc.lsp.jar")) { + "Run the compilerStdioTest Gradle task" + }, + ) + JarFile(jar.toFile()).use { archive -> + val properties = Properties() + archive.getInputStream(archive.getJarEntry("lsp-version.properties")).use { + properties.load(it) + } + assertThat(properties.getProperty("lsp.adapter")) + .describedAs("compilerStdioTest requires -Plsp.adapter=compiler") + .isIn("compiler", "xtc", "full") + } + return jar + } + + @Test + fun `pull reports round trip full unchanged and repaired over packaged stdio`() { + Session(packagedJar(), directory).use { session -> + session.initialize(pullDiagnostics = true) + session.open(BROKEN) + val params = DocumentDiagnosticParams(TextDocumentIdentifier(URI)) + val first = session.await(session.server.textDocumentService.diagnostic(params)).left + assertThat(first.items).isNotEmpty() + params.previousResultId = first.resultId + assertThat( + session + .await(session.server.textDocumentService.diagnostic(params)) + .right + .resultId, + ).isEqualTo(first.resultId) + session.change(VALID, 2) + val fixed = session.await(session.server.textDocumentService.diagnostic(params)).left + assertThat(fixed.items).isEmpty() + assertThat(fixed.resultId).isNotEqualTo(first.resultId) + session.shutdownAndExit() + } + } + + private class Session( + jar: Path, + directory: Path, + invalidXdkHome: Boolean = false, + private val colorPrototype: Boolean = false, + ) : AutoCloseable { + // Keep child traces after JUnit deletes its temporary workspace, without mixing sessions. + val traceDirectory = + System.getProperty("xtc.trace.directory")?.let { + Path.of(it).resolve(directory.fileName) + } ?: directory.resolve(".xtc/logs") + private val stderr = directory.resolve("stderr.log") + private val published = LinkedBlockingQueue() + val progress = CopyOnWriteArrayList() + private val executor = Executors.newVirtualThreadPerTaskExecutor() + private val process = + ProcessBuilder( + ProcessHandle + .current() + .info() + .command() + .orElseThrow(), + "-ea", + "-Duser.home=$directory", + "-Dxtc.trace.directory=$traceDirectory", + "-Dxtc.trace.level=INFO", + "-jar", + jar.toString(), + ).apply { + directory(directory.toFile()) + environment().remove("XDK_HOME") + environment()["XTC_LSP_COLOR_PROTOTYPE"] = colorPrototype.toString() + if (invalidXdkHome) { + environment()["XDK_HOME"] = directory.resolve("absent-xdk").toString() + } + redirectError(stderr.toFile()) + }.start() + private val client = + object : LanguageClient { + override fun publishDiagnostics(params: PublishDiagnosticsParams) { + published.add(params) + } + + override fun notifyProgress(params: ProgressParams) { + progress.add(params) + } + + override fun telemetryEvent(value: Any?) = Unit + + override fun showMessage(params: MessageParams) = Unit + + override fun showMessageRequest(params: ShowMessageRequestParams): CompletableFuture = + CompletableFuture.completedFuture(null) + + override fun logMessage(params: MessageParams) = Unit + + override fun configuration(params: ConfigurationParams): CompletableFuture> = + CompletableFuture.completedFuture(params.items.map { emptyMap() }) + } + private val launcher = + try { + Launcher + .Builder() + .setLocalService(client) + .setRemoteInterface(ServiceStatusServer::class.java) + .setInput(process.inputStream) + .setOutput(process.outputStream) + .setExecutorService(executor) + .create() + } catch (e: Exception) { + process.destroyForcibly() + process.waitFor(10, SECONDS) + executor.shutdownNow() + throw e + } + private val listening = launcher.startListening() + val server = launcher.remoteProxy + + fun status() = Gson().toJsonTree(await(server.languageServiceStatus())).asJsonObject + + fun request( + method: String, + params: Any, + ): CompletableFuture = launcher.remoteEndpoint.request(method, params) + + fun initialize( + versionedEdits: Boolean = false, + pullDiagnostics: Boolean = false, + sourceModules: List>? = null, + documentSync: Map = emptyMap(), + richPresentation: Boolean = true, + libraryContent: Boolean = false, + ) { + val initialized = + await( + server.initialize( + InitializeParams().apply { + initializationOptions = + buildMap { + sourceModules?.let { + put("xtcCompiler", mapOf("sourceModules" to it)) + } + put("xtcDocumentSync", documentSync) + } + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + inlineCompletion = InlineCompletionCapabilities() + if (richPresentation) { + signatureHelp = + SignatureHelpCapabilities().apply { + signatureInformation = + SignatureInformationCapabilities() + .apply { + activeParameterSupport = true + } + } + documentLink = + DocumentLinkCapabilities().apply { + tooltipSupport = true + } + } + documentSymbol = + DocumentSymbolCapabilities().apply { + hierarchicalDocumentSymbolSupport = true + } + publishDiagnostics = + PublishDiagnosticsCapabilities().apply { + versionSupport = true + relatedInformation = true + } + if (documentSync.isNotEmpty()) { + synchronization = + SynchronizationCapabilities(true, true, true) + } + if (pullDiagnostics) { + diagnostic = DiagnosticCapabilities() + } + semanticTokens = + SemanticTokensCapabilities().apply { + requests = + SemanticTokensClientCapabilitiesRequests( + SemanticTokensClientCapabilitiesRequestsFull( + true, + ), + true, + ) + } + } + workspace = + WorkspaceClientCapabilities().apply { + if (libraryContent) textDocumentContent = TextDocumentContentCapabilities() + workspaceEdit = + WorkspaceEditCapabilities().apply { + documentChanges = versionedEdits + } + } + } + }, + ), + ) + assertThat(initialized.capabilities.textDocumentSync.right.change) + .isEqualTo( + if (documentSync["incremental"] == true) { + TextDocumentSyncKind.Incremental + } else { + TextDocumentSyncKind.Full + }, + ) + assertThat(initialized.capabilities.definitionProvider.left).isTrue() + assertThat(initialized.capabilities.typeDefinitionProvider.left).isTrue() + assertThat(initialized.capabilities.implementationProvider.left).isTrue() + assertThat(initialized.capabilities.monikerProvider.left).isTrue() + assertThat(initialized.capabilities.callHierarchyProvider.left).isTrue() + assertThat(initialized.capabilities.inlayHintProvider.left).isTrue() + if (colorPrototype) { + assertThat(initialized.capabilities.colorProvider.left).isTrue() + } else { + assertThat(initialized.capabilities.colorProvider).isNull() + } + assertThat(initialized.capabilities.semanticTokensProvider).isNotNull() + assertThat(initialized.capabilities.hoverProvider.left).isTrue() + assertThat(initialized.capabilities.referencesProvider.right.workDoneProgress).isTrue() + assertThat(initialized.capabilities.documentHighlightProvider.left).isTrue() + assertThat(initialized.capabilities.completionProvider).isNotNull() + if (versionedEdits) { + assertThat(initialized.capabilities.renameProvider.right.prepareProvider).isTrue() + } else { + assertThat(initialized.capabilities.renameProvider).isNull() + } + assertThat(initialized.capabilities.signatureHelpProvider).isNotNull() + assertThat(initialized.capabilities.signatureHelpProvider.triggerCharacters) + .contains("[") + server.initialized(InitializedParams()) + } + + fun verifySemantics( + content: String, + name: String, + type: String, + ) { + val document = TextDocumentIdentifier(URI) + val declaration = content.indexOf(name) + val reference = content.lastIndexOf(name) + val cursor = Position(0, reference) + val declaredRange = + Range(Position(0, declaration), Position(0, declaration + name.length)) + val usedRange = Range(cursor, Position(0, reference + name.length)) + val service = server.textDocumentService + val hover = await(service.hover(HoverParams(document, cursor))) + assertThat(hover.contents.right.value).contains(type) + val definitions = await(service.definition(DefinitionParams(document, cursor))).left + assertThat(definitions.map { it.uri }).containsExactly(URI) + assertThat(definitions.map { it.range }).containsExactly(declaredRange) + val references = + await(service.references(ReferenceParams(document, cursor, ReferenceContext(true)))) + assertThat(references.map { it.uri }).containsOnly(URI) + assertThat(references.map { it.range }).containsExactly(declaredRange, usedRange) + val highlights = + await(service.documentHighlight(DocumentHighlightParams(document, cursor))) + assertThat(highlights.map { it.range }).containsExactly(declaredRange, usedRange) + } + + fun open(content: String) = + server.textDocumentService.didOpen( + DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, content)), + ) + + fun change( + content: String, + version: Int, + ) = server.textDocumentService.didChange( + DidChangeTextDocumentParams( + VersionedTextDocumentIdentifier(URI, version), + listOf(TextDocumentContentChangeEvent(content)), + ), + ) + + fun diagnosticsAt(version: Int): PublishDiagnosticsParams { + val deadline = System.nanoTime() + SECONDS.toNanos(30) + while (true) { + val remaining = deadline - System.nanoTime() + val publication = + if (remaining > 0) published.poll(remaining, NANOSECONDS) else null + checkNotNull(publication) { "No diagnostics for version $version. ${log()}" } + assertThat(publication.uri).isEqualTo(URI) + assertThat(publication.version).isLessThanOrEqualTo(version) + if (publication.version == version) return publication + } + } + + fun diagnosticsFor( + uri: String, + version: Int?, + ): PublishDiagnosticsParams { + val deadline = System.nanoTime() + SECONDS.toNanos(30) + while (true) { + val remaining = deadline - System.nanoTime() + val publication = + if (remaining > 0) published.poll(remaining, NANOSECONDS) else null + checkNotNull(publication) { "No diagnostics for $uri version $version. ${log()}" } + if (publication.uri == uri && publication.version == version) return publication + } + } + + fun await(future: Future): T = + try { + future.get(30, SECONDS) + } catch (e: TimeoutException) { + throw AssertionError("LSP request timed out. ${log()}", e) + } + + fun shutdownAndExit() { + await(server.shutdown()) + server.exit() + expectExit(0) + } + + fun expectExit(status: Int) { + assertThat(process.waitFor(10, SECONDS)) + .describedAs("Server did not exit. ${log()}") + .isTrue() + assertThat(process.exitValue()).describedAs(log()).isEqualTo(status) + } + + private fun log(): String = Files.readString(stderr).takeLast(8192) + + override fun close() { + try { + if (process.isAlive) process.destroyForcibly() + check(process.waitFor(10, SECONDS)) { "Could not terminate test server. ${log()}" } + } finally { + listening.cancel(true) + executor.shutdownNow() + process.outputStream.close() + process.inputStream.close() + } + } + } + + private companion object { + const val URI = "file:///Stdio.x" + const val VALID = "module Stdio { Int run() { Int value = 1; return value; } }" + const val REOPENED = + "module Stdio { String run() { String label = \"ok\"; return label; } }" + const val BROKEN = "module Stdio { Int run() { return missing; } }" + const val BOOTSTRAP = "org/xvm/lsp/xdk/mack.xtc" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt new file mode 100644 index 00000000000..e6e94118dc6 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt @@ -0,0 +1,143 @@ +package org.xvm.lsp.server + +import org.assertj.core.api.Assertions.assertThat +import org.eclipse.lsp4j.ClientCapabilities +import org.eclipse.lsp4j.CompletionCapabilities +import org.eclipse.lsp4j.CompletionItemCapabilities +import org.eclipse.lsp4j.CompletionItemInsertTextModeSupportCapabilities +import org.eclipse.lsp4j.CompletionParams +import org.eclipse.lsp4j.DidOpenTextDocumentParams +import org.eclipse.lsp4j.InitializeParams +import org.eclipse.lsp4j.InsertTextFormat +import org.eclipse.lsp4j.InsertTextMode +import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.TextDocumentClientCapabilities +import org.eclipse.lsp4j.TextDocumentIdentifier +import org.eclipse.lsp4j.TextDocumentItem +import org.eclipse.lsp4j.services.LanguageClient +import org.junit.jupiter.api.Test +import org.mockito.Mockito.mock +import org.xvm.lsp.adapter.xdk.XdkAdapter +import java.util.concurrent.TimeUnit.SECONDS + +class XdkSyntaxCompletionProtocolTest { + @Test + fun `missing declaration names use a plain empty edit at the original Unicode position`() { + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + server.initialize(params(true)).get(30, SECONDS) + val line = " /* 😀 */ String " + val text = "module Editing {\r\n$line= \"\";\r\n}" + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, text))) + val position = Position(1, line.length) + val item = + service + .completion(CompletionParams(TextDocumentIdentifier(URI), position)) + .get(30, SECONDS) + .left + .single() + assertThat(item.label).isEqualTo("string") + assertThat(item.insertTextFormat).isEqualTo(InsertTextFormat.PlainText) + assertThat(item.textEdit.left.range.start).isEqualTo(position) + assertThat(item.textEdit.left.range.end).isEqualTo(position) + assertThat(item.textEdit.left.newText).isEqualTo("string") + assertThat(item.additionalTextEdits).isEmpty() + } + } + + @Test + fun `only snippet capable clients receive placeholders and indentation modes are negotiated`() { + listOf(false, true).forEach { snippets -> + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + server.initialize(params(snippets)).get(30, SECONDS) + val text = "module Editing {\n void run() {\n if\n }\n}" + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, text))) + val items = + service + .completion(CompletionParams(TextDocumentIdentifier(URI), Position(2, 10))) + .get(30, SECONDS) + .left + val item = items.single { it.label == "if block" } + assertThat(item.insertTextFormat) + .isEqualTo(if (snippets) InsertTextFormat.Snippet else InsertTextFormat.PlainText) + assertThat(item.insertTextMode).isEqualTo(if (snippets) InsertTextMode.AsIs else null) + assertThat(item.textEdit.left.newText).isEqualTo(item.insertText) + assertThat(item.textEdit.left.range.start).isEqualTo(Position(2, 8)) + assertThat(item.textEdit.left.range.end).isEqualTo(Position(2, 10)) + assertThat(item.insertText).contains("\n ", "\n }") + if (snippets) { + assertThat(item.insertText).contains("${'$'}{1:True}", "${'$'}0") + } else { + assertThat(item.insertText).doesNotContain("$").contains("if (True)") + } + assertThat(items.single { it.label == "if" }.insertTextFormat).isEqualTo(InsertTextFormat.PlainText) + } + } + } + + @Test + fun `lambda templates negotiate a selected body or plain text without placeholder leakage`() { + listOf(false, true).forEach { snippets -> + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + server.initialize(params(snippets)).get(30, SECONDS) + val text = "module Editing { void take(function Int(Int) action) {} void run() { take(); } }" + val service = server.textDocumentService + service.didOpen(DidOpenTextDocumentParams(TextDocumentItem(URI, "xtc", 1, text))) + val item = + service + .completion(CompletionParams(TextDocumentIdentifier(URI), Position(0, text.lastIndexOf("take(") + 5))) + .get(30, SECONDS) + .left + .single { it.label == "(arg1) -> TODO()" } + assertThat(item.insertTextFormat).isEqualTo(if (snippets) InsertTextFormat.Snippet else InsertTextFormat.PlainText) + assertThat(item.textEdit.left.newText).isEqualTo(item.insertText) + assertThat(item.insertText).isEqualTo(if (snippets) "(arg1) -> ${'$'}{1:TODO()}${'$'}0" else "(arg1) -> TODO()") + } + } + } + + @Test + fun `clients that only adjust indentation receive relative template bodies`() { + val initialize = + params(false).apply { + capabilities.textDocument.completion.insertTextMode = InsertTextMode.AdjustIndentation + } + val policy = ClientPresentation.read(initialize) + assertThat(policy.completionTemplate("if (True) {\r\n \r\n }")) + .isEqualTo("if (True) {\r\n \r\n}") + assertThat(policy.completionTemplate("return value;")).isEqualTo("return value;") + assertThat(ClientPresentation.read(params(true)).completionTemplate("if (True) {\n \n }")) + .isEqualTo("if (True) {\n \n }") + } + + private fun params(snippets: Boolean) = + InitializeParams().apply { + capabilities = + ClientCapabilities().apply { + textDocument = + TextDocumentClientCapabilities().apply { + completion = + CompletionCapabilities().apply { + completionItem = + CompletionItemCapabilities().apply { + snippetSupport = snippets + if (snippets) { + insertTextModeSupport = + CompletionItemInsertTextModeSupportCapabilities( + listOf(InsertTextMode.AsIs), + ) + } + } + } + } + } + } + + private companion object { + const val URI = "untitled:Editing.x" + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XtcLanguageServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XtcLanguageServerTest.kt index 9a334e88c79..8a6eaa02034 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XtcLanguageServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XtcLanguageServerTest.kt @@ -35,6 +35,7 @@ import org.junit.jupiter.api.Nested import org.junit.jupiter.api.Test import org.mockito.ArgumentCaptor import org.mockito.Mockito.mock +import org.mockito.Mockito.timeout import org.mockito.Mockito.verify import org.xvm.lsp.adapter.mock.MockAdapter @@ -86,17 +87,17 @@ class XtcLanguageServerTest { val params = DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen(params) val captor = ArgumentCaptor.forClass(PublishDiagnosticsParams::class.java) - verify(mockClient).publishDiagnostics(captor.capture()) + verify(mockClient, timeout(5000)).publishDiagnostics(captor.capture()) val published = captor.value assertThat(published.uri).isEqualTo(uri) - assertThat(published.diagnostics) - .hasSize(1) - .allMatch { it.message?.left?.contains("test error") == true } + assertThat(published.diagnostics).hasSize(1).allMatch { + it.message?.left?.contains("test error") == true + } } @Test @@ -113,11 +114,11 @@ class XtcLanguageServerTest { val params = DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)) - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen(params) val captor = ArgumentCaptor.forClass(PublishDiagnosticsParams::class.java) - verify(mockClient).publishDiagnostics(captor.capture()) + verify(mockClient, timeout(5000)).publishDiagnostics(captor.capture()) assertThat(captor.value.diagnostics).isEmpty() } @@ -134,7 +135,7 @@ class XtcLanguageServerTest { } """.trimIndent() - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen( DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)), ) @@ -158,7 +159,7 @@ class XtcLanguageServerTest { } """.trimIndent() - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen( DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)), ) @@ -184,7 +185,7 @@ class XtcLanguageServerTest { @BeforeEach fun initServer() { - val result = server.initialize(InitializeParams()).get() + val result = server.initialize(editorInitializeParams()).get() caps = result.capabilities } @@ -199,19 +200,29 @@ class XtcLanguageServerTest { assertThat(caps.documentSymbolProvider?.left).describedAs("documentSymbol").isTrue() // Tree-sitter features - assertThat(caps.documentHighlightProvider?.left).describedAs("documentHighlight").isTrue() + assertThat(caps.documentHighlightProvider?.left) + .describedAs("documentHighlight") + .isTrue() assertThat(caps.selectionRangeProvider?.left).describedAs("selectionRange").isTrue() assertThat(caps.foldingRangeProvider?.left).describedAs("foldingRange").isTrue() assertThat(caps.documentLinkProvider).describedAs("documentLink").isNotNull() assertThat(caps.signatureHelpProvider).describedAs("signatureHelp").isNotNull() // Editing features - assertThat(caps.renameProvider?.right).describedAs("rename (with prepareProvider)").isNotNull() - assertThat(caps.renameProvider?.right?.prepareProvider).describedAs("rename prepareProvider").isTrue() + assertThat(caps.renameProvider?.right) + .describedAs("rename (with prepareProvider)") + .isNotNull() + assertThat(caps.renameProvider?.right?.prepareProvider) + .describedAs("rename prepareProvider") + .isTrue() assertThat(caps.codeActionProvider?.left).describedAs("codeAction").isTrue() assertThat(caps.documentFormattingProvider?.left).describedAs("formatting").isTrue() - assertThat(caps.documentRangeFormattingProvider?.left).describedAs("rangeFormatting").isTrue() - assertThat(caps.documentOnTypeFormattingProvider).describedAs("onTypeFormatting").isNotNull() + assertThat(caps.documentRangeFormattingProvider?.right?.rangesSupport) + .describedAs("rangeFormatting") + .isTrue() + assertThat(caps.documentOnTypeFormattingProvider) + .describedAs("onTypeFormatting") + .isNotNull() assertThat(caps.documentOnTypeFormattingProvider?.firstTriggerCharacter) .describedAs("onTypeFormatting firstTrigger") .isEqualTo("\n") @@ -219,13 +230,15 @@ class XtcLanguageServerTest { .describedAs("onTypeFormatting moreTriggers") .containsExactly("}", ";", ")") assertThat(caps.codeLensProvider).describedAs("codeLens").isNotNull() - assertThat(caps.linkedEditingRangeProvider?.left).describedAs("linkedEditingRange").isTrue() + assertThat(caps.linkedEditingRangeProvider?.left) + .describedAs("linkedEditingRange") + .isTrue() // Workspace features assertThat(caps.workspaceSymbolProvider?.left).describedAs("workspaceSymbol").isTrue() // Sync - assertThat(caps.textDocumentSync?.left).describedAs("textDocumentSync").isNotNull() + assertThat(caps.textDocumentSync?.right).describedAs("textDocumentSync").isNotNull() } @Test @@ -299,7 +312,7 @@ class XtcLanguageServerTest { } """.trimIndent() - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen( DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)), ) @@ -311,7 +324,7 @@ class XtcLanguageServerTest { val uri = "file:///dirty.x" val content = "module myapp { \n class Person { \n }\n}" - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen( DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)), ) @@ -376,8 +389,9 @@ class XtcLanguageServerTest { val highlights = server.textDocumentService - .documentHighlight(DocumentHighlightParams(TextDocumentIdentifier(uri), Position(3, 10))) - .get() + .documentHighlight( + DocumentHighlightParams(TextDocumentIdentifier(uri), Position(3, 10)), + ).get() assertThat(highlights).isNotEmpty() } @@ -419,7 +433,10 @@ class XtcLanguageServerTest { val edits = server.textDocumentService .formatting( - DocumentFormattingParams(TextDocumentIdentifier(uri), FormattingOptions(4, true)), + DocumentFormattingParams( + TextDocumentIdentifier(uri), + FormattingOptions(4, true), + ), ).get() assertThat(edits).isNotEmpty() @@ -432,7 +449,7 @@ class XtcLanguageServerTest { val uri = "file:///dirty2.x" val content = "module myapp { \n class Person { \n }\n}" - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() server.textDocumentService.didOpen( DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, content)), ) @@ -461,8 +478,9 @@ class XtcLanguageServerTest { val result = server.textDocumentService - .prepareRename(PrepareRenameParams(TextDocumentIdentifier(uri), Position(3, 10))) - .get() + .prepareRename( + PrepareRenameParams(TextDocumentIdentifier(uri), Position(3, 10)), + ).get() assertThat(result).isNotNull() assertThat(result.second.placeholder).isEqualTo("Person") @@ -521,8 +539,9 @@ class XtcLanguageServerTest { val result = server.textDocumentService - .signatureHelp(SignatureHelpParams(TextDocumentIdentifier(uri), Position(5, 10))) - .get() + .signatureHelp( + SignatureHelpParams(TextDocumentIdentifier(uri), Position(5, 10)), + ).get() assertThat(result).isNull() } @@ -551,7 +570,7 @@ class XtcLanguageServerTest { @Test @DisplayName("should complete without error") fun shouldCompleteWithoutError() { - server.initialize(InitializeParams()).get() + server.initialize(editorInitializeParams()).get() val future = server.shutdown() diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/treesitter/SemanticTokensVsTextMateTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/treesitter/SemanticTokensVsTextMateTest.kt index 7483f5cfa53..a56572dac97 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/treesitter/SemanticTokensVsTextMateTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/treesitter/SemanticTokensVsTextMateTest.kt @@ -13,25 +13,24 @@ import org.slf4j.Logger import org.slf4j.LoggerFactory /** - * Demonstrates the concrete benefits of LSP semantic tokens over TextMate/tree-sitter - * pattern-based highlighting (`highlights.scm`). + * Demonstrates the concrete benefits of LSP semantic tokens over TextMate/tree-sitter pattern-based + * highlighting (`highlights.scm`). * * ## Why This Test Exists * - * TextMate grammars (and tree-sitter `highlights.scm` queries) assign highlight groups - * using **pattern matching**: they see structure like `(identifier) @variable` and apply - * a single scope. This means every `identifier` node gets the same color regardless of - * whether it's a variable, a parameter, a property, a method name, or a type name used - * as a value. + * TextMate grammars (and tree-sitter `highlights.scm` queries) assign highlight groups using + * **pattern matching**: they see structure like `(identifier) @variable` and apply a single scope. + * This means every `identifier` node gets the same color regardless of whether it's a variable, a + * parameter, a property, a method name, or a type name used as a value. * - * LSP semantic tokens use the **full AST context** to classify each identifier by its - * semantic role. The encoder walks parent nodes, field names, and sibling context to - * assign precise token types (class, interface, method, property, parameter, type, - * decorator, namespace) and modifiers (declaration, static, abstract, readonly). + * LSP semantic tokens use the **full AST context** to classify each identifier by its semantic + * role. The encoder walks parent nodes, field names, and sibling context to assign precise token + * types (class, interface, method, property, parameter, type, decorator, namespace) and modifiers + * (declaration, static, abstract, readonly). * - * This test parses realistic XTC source code and verifies that the semantic token - * encoder produces classifications that TextMate fundamentally cannot -- each test - * documents the specific limitation it demonstrates. + * This test parses realistic XTC source code and verifies that the semantic token encoder produces + * classifications that TextMate fundamentally cannot -- each test documents the specific limitation + * it demonstrates. * * ## What TextMate Highlights Look Like * @@ -45,8 +44,8 @@ import org.slf4j.LoggerFactory * (property_declaration name: (identifier) @variable.member) * ``` * - * These patterns can match some contexts, but they fail when the same identifier text - * appears in multiple roles, or when modifiers and precise type distinctions matter. + * These patterns can match some contexts, but they fail when the same identifier text appears in + * multiple roles, or when modifiers and precise type distinctions matter. */ @DisplayName("Semantic Tokens vs TextMate -- Benefit Demonstration") @TestInstance(TestInstance.Lifecycle.PER_CLASS) @@ -81,8 +80,8 @@ class SemanticTokensVsTextMateTest { // ======================================================================== /** - * A decoded semantic token with human-readable fields extracted from the - * delta-encoded integer array. + * A decoded semantic token with human-readable fields extracted from the delta-encoded integer + * array. */ data class DecodedToken( val line: Int, @@ -136,7 +135,10 @@ class SemanticTokensVsTextMateTest { type: String, ): DecodedToken? = find { it.text == text && it.tokenType == type } - private fun List.findAllByText(text: String): List = filter { it.text == text } + private fun List.findAllByText(text: String): List = + filter { + it.text == text + } // ======================================================================== // Benefit 1: Same identifier, different semantic roles @@ -146,10 +148,10 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 1: Disambiguating identical identifiers by context") inner class IdentifierDisambiguation { /** - * TextMate limitation: `name` appears 3 times -- as a parameter, a property, and - * inside an assignment. TextMate's `(identifier) @variable` colors all three the - * same. The more specific patterns `(parameter name: ...)` and - * `(property_declaration name: ...)` can only match the declaration sites. + * TextMate limitation: `name` appears 3 times -- as a parameter, a property, and inside an + * assignment. TextMate's `(identifier) @variable` colors all three the same. The more + * specific patterns `(parameter name: ...)` and `(property_declaration name: ...)` can only + * match the declaration sites. * * Semantic tokens classify each occurrence precisely: * - `name` in `String name` (parameter) -> `parameter` + `declaration` @@ -187,8 +189,9 @@ class SemanticTokensVsTextMateTest { // Parameter declaration: name -> "parameter" with "declaration" val paramToken = tokens.findByTextAndType("name", "parameter") assertThat(paramToken) - .describedAs("TextMate sees parameter 'name' as @variable.parameter; semantic tokens give it the distinct 'parameter' type") - .isNotNull + .describedAs( + "TextMate sees parameter 'name' as @variable.parameter; semantic tokens give it the distinct 'parameter' type", + ).isNotNull assertThat(paramToken!!.modifiers).contains("declaration") // The two should be DIFFERENT token types -- TextMate can't do this @@ -200,11 +203,11 @@ class SemanticTokensVsTextMateTest { /** * TextMate limitation: Both `getName` at the declaration site and `getName` at the - * member-call site are identifiers. TextMate can match them separately but CANNOT - * carry the `declaration` modifier. Themes can't distinguish the definition from usage. + * member-call site are identifiers. TextMate can match them separately but CANNOT carry the + * `declaration` modifier. Themes can't distinguish the definition from usage. * - * Semantic tokens distinguish: the declaration has `declaration` modifier, the - * member-call site doesn't. This lets themes dim or bold declaration-site identifiers. + * Semantic tokens distinguish: the declaration has `declaration` modifier, the member-call + * site doesn't. This lets themes dim or bold declaration-site identifiers. */ @Test @DisplayName("method at declaration site carries 'declaration' modifier, call site doesn't") @@ -228,15 +231,17 @@ class SemanticTokensVsTextMateTest { val methodTokens = tokens.filter { it.text == "getValue" && it.tokenType == "method" } assertThat(methodTokens) - .describedAs("'getValue' should appear as method at both declaration and member-call site") - .hasSizeGreaterThanOrEqualTo(2) + .describedAs( + "'getValue' should appear as method at both declaration and member-call site", + ).hasSizeGreaterThanOrEqualTo(2) val declToken = methodTokens.find { "declaration" in it.modifiers } val callToken = methodTokens.find { "declaration" !in it.modifiers } assertThat(declToken) - .describedAs("Declaration site should have 'declaration' modifier -- TextMate has no modifier concept") - .isNotNull + .describedAs( + "Declaration site should have 'declaration' modifier -- TextMate has no modifier concept", + ).isNotNull assertThat(callToken) .describedAs("Call site should NOT have 'declaration' modifier") .isNotNull @@ -251,8 +256,8 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 2: Distinguishing class/interface/enum/const/service/mixin declarations") inner class TypeDeclarationDistinction { /** - * TextMate limitation: `highlights.scm` maps ALL type declarations to the same - * scope `@type.definition`: + * TextMate limitation: `highlights.scm` maps ALL type declarations to the same scope + * `@type.definition`: * ``` * (class_declaration name: (type_name) @type.definition) * (interface_declaration name: (type_name) @type.definition) @@ -260,8 +265,8 @@ class SemanticTokensVsTextMateTest { * (const_declaration name: (type_name) @type.definition) * ``` * - * Semantic tokens assign DISTINCT types: `class`, `interface`, `enum`, `struct` - * (for const). Themes can color classes blue, interfaces green, enums orange. + * Semantic tokens assign DISTINCT types: `class`, `interface`, `enum`, `struct` (for + * const). Themes can color classes blue, interfaces green, enums orange. */ @Test @DisplayName("each XTC type category gets a distinct semantic token type") @@ -285,13 +290,15 @@ class SemanticTokensVsTextMateTest { // Semantic tokens: each gets its own type val personToken = tokens.findByTextAndType("Person", "class") assertThat(personToken) - .describedAs("'Person' should be 'class', not generic 'type' -- TextMate sees @type.definition") - .isNotNull + .describedAs( + "'Person' should be 'class', not generic 'type' -- TextMate sees @type.definition", + ).isNotNull val runnableToken = tokens.findByTextAndType("Runnable", "interface") assertThat(runnableToken) - .describedAs("'Runnable' should be 'interface' -- TextMate can't distinguish from class") - .isNotNull + .describedAs( + "'Runnable' should be 'interface' -- TextMate can't distinguish from class", + ).isNotNull val colorToken = tokens.findByTextAndType("Color", "enum") assertThat(colorToken) @@ -307,7 +314,9 @@ class SemanticTokensVsTextMateTest { ) assertThat(types) .hasSize(3) - .describedAs("class, interface, enum should map to 3 distinct token types -- TextMate gives them all @type.definition") + .describedAs( + "class, interface, enum should map to 3 distinct token types -- TextMate gives them all @type.definition", + ) } /** @@ -330,8 +339,9 @@ class SemanticTokensVsTextMateTest { val constToken = tokens.findByTextAndType("Point", "struct") if (constToken != null) { assertThat(constToken.modifiers) - .describedAs("'const' maps to 'struct' with both 'declaration' and 'readonly' -- TextMate has no modifier system") - .contains("declaration", "readonly") + .describedAs( + "'const' maps to 'struct' with both 'declaration' and 'readonly' -- TextMate has no modifier system", + ).contains("declaration", "readonly") } } } @@ -344,14 +354,13 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 3: Type references in type positions are classified as 'type'") inner class TypeReferenceClassification { /** - * TextMate can match `(type_name) @type` but this is a STRUCTURAL match -- it works - * for explicit type expressions. The problem is that TextMate also matches - * `(identifier) @variable` as a catch-all, and the LAST match wins in TextMate - * ordering. If `(identifier) @variable` appears after `(type_name) @type`, the - * type coloring is lost. + * TextMate can match `(type_name) @type` but this is a STRUCTURAL match -- it works for + * explicit type expressions. The problem is that TextMate also matches + * `(identifier) @variable` as a catch-all, and the LAST match wins in TextMate ordering. If + * `(identifier) @variable` appears after `(type_name) @type`, the type coloring is lost. * - * Semantic tokens don't have ordering conflicts. Each token is classified exactly - * once based on its AST context. + * Semantic tokens don't have ordering conflicts. Each token is classified exactly once + * based on its AST context. */ @Test @DisplayName("return type, parameter type, and property type all classified as 'type'") @@ -381,13 +390,13 @@ class SemanticTokensVsTextMateTest { } /** - * TextMate highlight queries cannot express "this identifier is a type because - * it appears as a return type vs a variable." Both `String` in `String name` - * and `name` are identifiers at the text level. + * TextMate highlight queries cannot express "this identifier is a type because it appears + * as a return type vs a variable." Both `String` in `String name` and `name` are + * identifiers at the text level. * - * Semantic tokens classify `String` as `type` and `name` as `property` because - * the encoder examines the parent node context: is this child in a `type_expression` - * position, or a `name` field of a `property_declaration`? + * Semantic tokens classify `String` as `type` and `name` as `property` because the encoder + * examines the parent node context: is this child in a `type_expression` position, or a + * `name` field of a `property_declaration`? */ @Test @DisplayName("'String' as type vs 'name' as property -- both identifiers, different tokens") @@ -407,14 +416,9 @@ class SemanticTokensVsTextMateTest { val stringToken = tokens.findByTextAndType("String", "type") val nameToken = tokens.findByTextAndType("name", "property") - assertThat(stringToken) - .describedAs("'String' should be classified as 'type'") - .isNotNull - assertThat(nameToken) - .describedAs("'name' should be classified as 'property'") - .isNotNull - assertThat(stringToken!!.tokenType) - .isNotEqualTo(nameToken!!.tokenType) + assertThat(stringToken).describedAs("'String' should be classified as 'type'").isNotNull + assertThat(nameToken).describedAs("'name' should be classified as 'property'").isNotNull + assertThat(stringToken!!.tokenType).isNotEqualTo(nameToken!!.tokenType) } } @@ -431,11 +435,11 @@ class SemanticTokensVsTextMateTest { * (annotation name: (qualified_name) @attribute) * ``` * - * This works, but it's a single scope. Semantic tokens classify annotation names - * as `decorator` -- a distinct token type that themes can style independently from - * types, variables, and keywords. More importantly, TextMate's `@attribute` is - * often themed identically to other constructs, while `decorator` is universally - * styled with a distinct color in all major themes. + * This works, but it's a single scope. Semantic tokens classify annotation names as + * `decorator` -- a distinct token type that themes can style independently from types, + * variables, and keywords. More importantly, TextMate's `@attribute` is often themed + * identically to other constructs, while `decorator` is universally styled with a distinct + * color in all major themes. */ @Test @DisplayName("annotation name classified as 'decorator', not 'variable' or 'type'") @@ -464,8 +468,9 @@ class SemanticTokensVsTextMateTest { // Verify it's NOT classified as a type (a common TextMate mistake) val typeToken = injectTokens.find { it.tokenType == "type" } assertThat(typeToken) - .describedAs("'Inject' should not also appear as 'type' -- semantic tokens prevent double-classification") - .isNull() + .describedAs( + "'Inject' should not also appear as 'type' -- semantic tokens prevent double-classification", + ).isNull() } } @@ -477,13 +482,13 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 5: Modifier bitmasks carry semantic information TextMate cannot") inner class ModifierBitmasks { /** - * TextMate has NO concept of modifiers. A `static` method looks the same as a - * regular method -- both are `@function`. A `static` property looks the same as - * an instance property -- both are `@variable.member`. + * TextMate has NO concept of modifiers. A `static` method looks the same as a regular + * method -- both are `@function`. A `static` property looks the same as an instance + * property -- both are `@variable.member`. * - * Semantic tokens carry modifier bitmasks: `static`, `abstract`, `readonly`, - * `declaration`. Themes and editor UI can use these to italicize static members, - * strikethrough deprecated ones, or bold declarations. + * Semantic tokens carry modifier bitmasks: `static`, `abstract`, `readonly`, `declaration`. + * Themes and editor UI can use these to italicize static members, strikethrough deprecated + * ones, or bold declarations. * * IntelliJ and VS Code both support modifier-based styling: * - VS Code: `semanticTokenColors: { "method.static": { "fontStyle": "italic" } }` @@ -507,14 +512,13 @@ class SemanticTokensVsTextMateTest { logTokens("static method", tokens) val parseToken = tokens.findByTextAndType("parse", "method") - assertThat(parseToken) - .describedAs("'parse' should be classified as 'method'") - .isNotNull + assertThat(parseToken).describedAs("'parse' should be classified as 'method'").isNotNull if (parseToken != null) { assertThat(parseToken.modifiers) - .describedAs("static method should carry 'static' modifier -- TextMate has no modifier concept") - .contains("static") + .describedAs( + "static method should carry 'static' modifier -- TextMate has no modifier concept", + ).contains("static") assertThat(parseToken.modifiers) .describedAs("declaration site should carry 'declaration' modifier") .contains("declaration") @@ -530,13 +534,13 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 6: Property access vs method call on member expressions") inner class MemberExpressionContext { /** - * TextMate challenge: In `obj.foo`, is `foo` a property or a method? - * TextMate's `highlights.scm` handles `call_expression > member_expression` for - * method calls but falls through to `(identifier) @variable` for property access. + * TextMate challenge: In `obj.foo`, is `foo` a property or a method? TextMate's + * `highlights.scm` handles `call_expression > member_expression` for method calls but falls + * through to `(identifier) @variable` for property access. * - * Semantic tokens walk the AST: if the parent is a `call_expression`, the member - * is classified as `method`; otherwise it's `property`. This distinction lets - * themes color method calls differently from property reads. + * Semantic tokens walk the AST: if the parent is a `call_expression`, the member is + * classified as `method`; otherwise it's `property`. This distinction lets themes color + * method calls differently from property reads. */ @Test @DisplayName("member method call vs member property access get different token types") @@ -561,16 +565,23 @@ class SemanticTokensVsTextMateTest { logTokens("member call vs property", tokens) // this.getName() -- "getName" at call site should be "method" - val getNameCallTokens = tokens.filter { it.text == "getName" && it.tokenType == "method" } + val getNameCallTokens = + tokens.filter { + it.text == "getName" && it.tokenType == "method" + } assertThat(getNameCallTokens) .describedAs("'getName' should appear as 'method' (both declaration and call site)") .hasSizeGreaterThanOrEqualTo(1) // this.name -- "name" in member_expression (not call) should be "property" - val namePropertyTokens = tokens.filter { it.text == "name" && it.tokenType == "property" } + val namePropertyTokens = + tokens.filter { + it.text == "name" && it.tokenType == "property" + } assertThat(namePropertyTokens) - .describedAs("'name' in this.name should be 'property' -- TextMate would just say @variable") - .isNotEmpty + .describedAs( + "'name' in this.name should be 'property' -- TextMate would just say @variable", + ).isNotEmpty } } @@ -582,10 +593,10 @@ class SemanticTokensVsTextMateTest { @DisplayName("Benefit 7: Combined -- realistic code shows all advantages at once") inner class CombinedBenefits { /** - * This test uses a realistic XTC class that exercises all the disambiguation - * capabilities simultaneously. It verifies that in a single file, the semantic - * token encoder produces a richer token set than TextMate could, and that every - * token type in the output is meaningful. + * This test uses a realistic XTC class that exercises all the disambiguation capabilities + * simultaneously. It verifies that in a single file, the semantic token encoder produces a + * richer token set than TextMate could, and that every token type in the output is + * meaningful. * * TextMate would produce roughly: * - All type names -> @type or @type.definition (no class/interface/enum distinction) @@ -657,8 +668,9 @@ class SemanticTokensVsTextMateTest { // Verify declaration modifiers exist val declTokens = tokens.filter { "declaration" in it.modifiers } assertThat(declTokens) - .describedAs("Multiple tokens should carry 'declaration' modifier -- TextMate has no modifier support") - .hasSizeGreaterThanOrEqualTo(5) + .describedAs( + "Multiple tokens should carry 'declaration' modifier -- TextMate has no modifier support", + ).hasSizeGreaterThanOrEqualTo(5) } } @@ -670,8 +682,8 @@ class SemanticTokensVsTextMateTest { @DisplayName("Enum values, constructor calls, and deprecated") inner class EnumNewDeprecatedTests { /** - * Enum values inside an enum body should be classified as `enumMember` - * with `declaration` and `readonly` modifiers. + * Enum values inside an enum body should be classified as `enumMember` with `declaration` + * and `readonly` modifiers. */ @Test @DisplayName("should classify enum values as enumMember") @@ -699,8 +711,8 @@ class SemanticTokensVsTextMateTest { } /** - * In `new Foo()`, the `Foo` identifier should be classified as `type` - * (not `method`) because it's a constructor invocation. + * In `new Foo()`, the `Foo` identifier should be classified as `type` (not `method`) + * because it's a constructor invocation. */ @Test @DisplayName("should classify new expression callee as type") @@ -725,13 +737,14 @@ class SemanticTokensVsTextMateTest { // the callee should be type, not method. Check that no method token // exists for "Foo" inside the new expression line. val fooTokens = tokens.findAllByText("Foo") - // The class declaration should be "class", the new-expression usage should be "type" (not "method") + // The class declaration should be "class", the new-expression usage should be "type" + // (not "method") assertThat(fooTokens).noneMatch { it.tokenType == "method" } } /** - * A method annotated with `@Deprecated` should have the `deprecated` modifier - * in its semantic token. + * A method annotated with `@Deprecated` should have the `deprecated` modifier in its + * semantic token. */ @Test @DisplayName("should add deprecated modifier for @Deprecated annotation") diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt new file mode 100644 index 00000000000..141ea39d396 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt @@ -0,0 +1,79 @@ +package org.xvm.lsp.util + +import com.google.gson.JsonParser +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.io.ByteArrayInputStream +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.FileTime +import java.time.Instant +import java.util.Base64 +import java.util.zip.ZipInputStream + +class ServerLogsTest { + @TempDir lateinit var root: Path + + @Test fun `pruning keeps live processes and foreign files while bounding closed sessions and age`() { + val now = Instant.parse("2026-10-06T12:00:00Z") + + fun session( + id: Int, + ageDays: Long, + file: String = "server.log", + ): Path { + val path = Files.createDirectory(root.resolve("server-$id-1")) + val time = FileTime.from(now.minusSeconds(ageDays * 86400)) + Files.writeString(path.resolve(file), "retained log") + Files.setLastModifiedTime(path.resolve(file), time) + Files.setLastModifiedTime(path, time) + return path + } + val live = session(1, 60) + val newest = session(2, 0) + val older = session(3, 1) + val expired = session(4, 30) + val foreign = session(5, 60, "unrelated.txt") + val target = Files.createDirectory(root.resolve("unrelated")) + Files.createSymbolicLink(root.resolve("server-6-1"), target) + assertThat(ServerLogs.prune(root, LogRetention(retainedSessions = 1), now) { pid, _ -> pid == 1L }).isEqualTo(2) + assertThat(live).exists() + assertThat(newest).exists() + assertThat(foreign).exists() + assertThat(target).exists() + assertThat(older).doesNotExist() + assertThat(expired).doesNotExist() + } + + @Test fun `exports bounded tails with a manifest and excludes symlinks`() { + val log = root.resolve("server.log") + Files.writeString(log, "a".repeat(600_000) + "END") + val secret = root.resolve("other.txt").also { Files.writeString(it, "outside input") } + val link = root.resolve("linked.log").also { Files.createSymbolicLink(it, secret) } + val archive = ServerLogs.archive(listOf(log, link), mapOf("pid" to 42)) + val contents = + ZipInputStream(ByteArrayInputStream(Base64.getDecoder().decode(archive.getValue("base64")))).use { zip -> + buildMap { generateSequence { zip.nextEntry }.forEach { put(it.name, zip.readBytes()) } } + } + assertThat(contents.keys).containsExactly("0-server.log", "manifest.json") + assertThat(contents.getValue("0-server.log").size).isEqualTo(512 * 1024) + assertThat(String(contents.getValue("0-server.log"))).endsWith("END") + val manifest = JsonParser.parseString(String(contents.getValue("manifest.json"))).asJsonObject + assertThat( + manifest["files"] + .asJsonArray + .single() + .asJsonObject["truncated"] + .asBoolean, + ).isTrue() + assertThat(manifest["service"].asJsonObject["pid"].asInt).isEqualTo(42) + } + + @Test fun `retention rejects impossible limits`() { + assertThatThrownBy { LogRetention(totalSizeMb = 1) }.isInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { LogRetention(historyDays = 0) }.isInstanceOf(IllegalArgumentException::class.java) + assertThatThrownBy { LogRetention(retainedSessions = 0) }.isInstanceOf(IllegalArgumentException::class.java) + } +} diff --git a/lang/lsp-server/src/test/resources/platform/CircularBuffer.x b/lang/lsp-server/src/test/resources/platform/CircularBuffer.x new file mode 100644 index 00000000000..a4a5c192cd7 --- /dev/null +++ b/lang/lsp-server/src/test/resources/platform/CircularBuffer.x @@ -0,0 +1,132 @@ +// From platform b8be627, common/src/main/x/common/utils/CircularBuffer.x. +// Unchanged class, wrapped in a standalone module for reproducible compiler-adapter tests. +module PlatformBuffer { + /** + * The CircularBuffer holds up to a specified capacity of elements, and once it reaches that number, + * each additional element causes the oldest element to be removed. The elements are arranged in + * the order from newest to oldest. + */ + class CircularBuffer + implements UniformIndexed + implements Appender + implements Iterable { + // ----- constructors -------------------------------------------------------------------------- + + /** + * Construct a CircularBuffer of the specified capacity. + * + * @param capacity the maximum number of elements in the buffer + */ + construct(Int capacity) { + this.contents = new Element?[capacity]; + } + + // ----- properties ---------------------------------------------------------------------------- + + @Override + public/private Int size; + + /** + * The allocated storage capacity of the CircularBuffer. + */ + Int capacity.get() = contents.size; + + /** + * An array that holds the elements of the CircularBuffer + */ + protected/private Element?[] contents; + + /** + * The index of the next element to add at the `head`. The index starts at zero, and is never + * reset, so the index into the underlying `contents` array is the modulo of the `head` and the + * size of the `contents` array. The size of the CircularBuffer is `head` until it's `full`. + */ + protected/private Int head; + + // ----- public interface ---------------------------------------------------------------------- + + /** + * Clear the buffer. + */ + CircularBuffer clear() { + contents.fill(Null); + head = 0; + size = 0; + return this; + } + + // ----- UniformIndexed interface -------------------------------------------------------------- + + @Override + @Op("[]") + Element getElement(Int index) { + return contents[indexFor(index)].as(Element); + } + + @Override + @Op("[]=") + void setElement(Int index, Element value) { + contents[indexFor(index)] = value; + } + + // ----- Iterable interface -------------------------------------------------------------------- + + @Override + Iterator iterator() { + return new Iterator() { + Int index = 0; + + @Override + conditional Element next() { + return index < this.CircularBuffer.size + ? (True, this.CircularBuffer[index++]) + : False; + } + }; + } + + // ----- Appender interface -------------------------------------------------------------------- + + @Override + @Op("+") + CircularBuffer add(Element value) { + Int index = head; + contents[index++] = value; + head = index == capacity ? 0 : index; + if (size < capacity) { + size++; + } + return this; + } + + // ----- Object interface ---------------------------------------------------------------------- + + @Override + String toString( + String sep = ", ", + String? pre = "[", + String? post = "]", + Int? limit = Null, + String trunc = "...", + function String(Element)? render = Null) { + + return toArray().toString(sep, pre, post, limit, trunc, render); + } + + // ----- internal ------------------------------------------------------------------------------ + + /** + * Given an absolute index, where zero is the "newest" element, determine the corresponding + * index in the contents array. + * + * @param index an index + * + * @return a corresponding index into the contents array + */ + protected Int indexFor(Int index) { + assert:bounds 0 <= index < size; + index = head - 1 - index; + return index < 0 ? capacity + index : index; + } + } +} diff --git a/lang/scripts/compiler-workload.py b/lang/scripts/compiler-workload.py new file mode 100644 index 00000000000..5d9f5012d3d --- /dev/null +++ b/lang/scripts/compiler-workload.py @@ -0,0 +1,504 @@ +#!/usr/bin/env python3 +"""Measure the packaged compiler on real graphs and generated fixtures without editing user sources. + +The sampler observes heap and queue metadata over the same stdio connection. Optional RSS sampling +and post-GC checkpoints supplement it. Timings include transport/debounce; sampled peaks are lower +bounds, and post-GC heap includes intentional caches rather than proving object reachability. +Run this separately from other compiler workloads to keep timings interpretable. +""" + +import argparse +import collections +import concurrent.futures +import hashlib +import json +import math +import pathlib +import subprocess +import sys +import threading +import time +import urllib.parse +from types import SimpleNamespace + + +class RpcError(RuntimeError): + def __init__(self, error): + super().__init__(str(error)) + self.code = error["code"] + + +class Session: + def __init__(self, args, directory, modules): + self.args, self.directory, self.modules = args, directory, modules + self.lock = threading.Lock() + self.pending = {} + self.next_id = 0 + self.timings = [] + self.samples = [] + self.sampling_errors = [] + self.stop_sampling = threading.Event() + self.stderr = (directory / "stderr.log").open("wb") + self.process = subprocess.Popen([ + args.java, "--enable-native-access=ALL-UNNAMED", f"-Xmx{args.heap}", + f"-Duser.home={directory}", f"-Dxtc.trace.directory={directory / 'trace'}", + "-jar", str(args.jar), + ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=self.stderr) + self.reader = threading.Thread(target=self._read, daemon=True) + self.sampler = threading.Thread(target=self._sample, daemon=True) + self.reader.start() + + def _write_locked(self, message): + payload = json.dumps(message).encode("utf-8") + self.process.stdin.write(f"Content-Length: {len(payload)}\r\n\r\n".encode() + payload) + self.process.stdin.flush() + + def notify(self, method, params): + with self.lock: + self._write_locked({"jsonrpc": "2.0", "method": method, "params": params}) + + def request(self, method, params): + future = concurrent.futures.Future() + with self.lock: + self.next_id += 1 + request_id = self.next_id + self.pending[request_id] = future + self._write_locked({"jsonrpc": "2.0", "id": request_id, "method": method, "params": params}) + return request_id, future + + def call(self, method, params=None, *, record=True): + started = time.perf_counter() + _, future = self.request(method, params) + try: + return future.result(self.args.timeout) + finally: + if record: + self.timings.append({"method": method, "ms": (time.perf_counter() - started) * 1000}) + + def _read(self): + try: + while True: + headers = {} + while (line := self.process.stdout.readline()) not in (b"\r\n", b"\n"): + if not line: + raise EOFError("Server closed stdout") + key, value = line.decode("ascii").split(":", 1) + headers[key.lower()] = value.strip() + size = int(headers["content-length"]) + payload = self.process.stdout.read(size) + if len(payload) != size: + raise EOFError("Truncated protocol message") + message = json.loads(payload) + if "method" in message: + if "id" in message: + method = message["method"] + if method == "workspace/configuration": + result = [{"sourceModules": self.modules} if item.get("section") == "xtc.compiler" else {} + for item in message["params"]["items"]] + elif method == "workspace/workspaceFolders": + result = [{"uri": self.args.workspace.as_uri(), "name": "workload"}] + elif method == "workspace/applyEdit": + result = {"applied": False, "failureReason": "Read-only workload host"} + else: + result = None + with self.lock: + self._write_locked({"jsonrpc": "2.0", "id": message["id"], "result": result}) + else: + with self.lock: + future = self.pending.pop(message["id"]) + if "error" in message: + future.set_exception(RpcError(message["error"])) + else: + future.set_result(message.get("result")) + except Exception as error: + with self.lock: + pending, self.pending = self.pending, {} + for future in pending.values(): + future.set_exception(error) + + def _sample(self): + next_rss = 0.0 + with (self.directory / "samples.jsonl").open("w") as output: + while not self.stop_sampling.is_set(): + try: + status = self.call("xtc/languageServiceStatus", record=False) + queue = status["compilerQueue"] + assert queue["queueSize"] == len(queue["queuedJobs"]) + sample = {"time": time.monotonic(), "heap": status["heap"], "queue": queue} + if self.args.sample_rss and time.monotonic() >= next_rss: + rss = subprocess.run(["ps", "-o", "rss=", "-p", str(self.process.pid)], + check=True, capture_output=True, text=True, timeout=5) + sample["rssBytes"] = int(rss.stdout.strip()) * 1024 + next_rss = time.monotonic() + 1 + self.samples.append(sample) + output.write(json.dumps(sample) + "\n") + output.flush() + except Exception as error: + self.sampling_errors.append(str(error)) + return + self.stop_sampling.wait(0.2) + + def collect_heap(self, cycle): + """Collect live heap between edits; never include diagnostic pauses in request latency.""" + # GC.class_histogram performs a full GC unless -all is specified. Its live-object counts + # distinguish retained classes from allocation churn without retaining a full heap dump. + command = "GC.class_histogram" if self.args.heap_histograms else "GC.run" + collected = subprocess.run([self.args.jcmd, str(self.process.pid), command], + check=True, capture_output=True, text=True, timeout=30) + if self.args.heap_histograms: + assert "Total" in collected.stdout, collected.stdout + (self.directory / f"heap-{cycle}.txt").write_text(collected.stdout) + else: + assert "Command executed successfully" in collected.stdout, collected.stdout + return self.call("xtc/languageServiceStatus", record=False)["heap"] + + def initialize(self): + self.call("initialize", { + "processId": None, + "workspaceFolders": [{"uri": self.args.workspace.as_uri(), "name": "workload"}], + "capabilities": {"workspace": {"configuration": True, "diagnostics": {"refreshSupport": True}}, + "textDocument": {"diagnostic": {"dynamicRegistration": False}}}, + "initializationOptions": {"xtcCompiler": {"sourceModules": self.modules}}, + }) + self.notify("initialized", {}) + status = self.call("xtc/languageServiceStatus") + assert status["compilerQueue"] is not None, "Build with -Plsp.adapter=compiler" + self.sampler.start() + + def finish(self, disconnect=False): + self.stop_sampling.set() + if self.sampler.ident is not None: + self.sampler.join(self.args.timeout + 1) + assert not self.sampler.is_alive(), "Sampler did not finish" + assert not self.sampling_errors, self.sampling_errors + if disconnect: + self.process.stdin.close() + else: + self.call("shutdown") + self.notify("exit", None) + code = self.process.wait(15) + assert code == (1 if disconnect else 0), f"Unexpected server exit: {code}" + return code + + def cleanup(self): + # Cleanup never turns a failure to exit into a successful lifecycle assertion. + self.stop_sampling.set() + if self.process.poll() is None: + self.process.kill() + self.process.wait(15) + self.reader.join(5) + if self.sampler.ident is not None: + self.sampler.join(self.args.timeout + 1) + self.process.stdin.close() + self.process.stdout.close() + self.stderr.close() + + +def inventory(root): + return {str(file.relative_to(root)): hashlib.sha256(file.read_bytes()).hexdigest() + for file in sorted(root.rglob("*.x")) if ".git" not in file.parts} + + +def percentiles(values): + ordered = sorted(values) + return {"count": len(ordered), "p50Ms": ordered[math.ceil(len(ordered) * 0.5) - 1], + "p95Ms": ordered[math.ceil(len(ordered) * 0.95) - 1], "maxMs": ordered[-1]} + + +def trace_statistics(directory): + timings = collections.defaultdict(list) + phases = collections.defaultdict(list) + previous = {} + max_active = 0 + largest_queue = [] + for file in sorted((directory / "trace").glob("*.jsonl")): + for line in file.open(): + event = json.loads(line) + max_active = max(max_active, event.get("activeApiThreads", 0)) + jobs = event.get("queuedJobs", []) + if len(jobs) > len(largest_queue): + largest_queue = jobs + if event.get("kind") == "javatools" and event.get("event") == "end": + timings[event["operation"]].append(event["elapsedMs"]) + if event.get("kind") == "lsp-query" and event["event"] in ( + "start", "analysis-ready", "started", "backend-complete", "converted"): + phase = event["event"] + if phase != "start" and event["id"] in previous: + phases[f"{event['operation']}:{phase}"].append(event["elapsedMs"] - previous[event["id"]]) + previous[event["id"]] = event["elapsedMs"] + if event.get("writeMs") is not None: + phases[f"{event['operation']}:write"].append(event["writeMs"]) + return {"apiTimings": {name: percentiles(values) for name, values in sorted(timings.items())}, + "queryPhaseTimings": {name: percentiles(values) for name, values in sorted(phases.items())}, + "maxActiveApiThreads": max_active, "largestTracedQueue": largest_queue, + "traceDirectory": str(directory / "trace")} + + +def run(args): + graph = json.loads(args.graph.read_text()) + modules = [{**module, "uri": (args.workspace / module["uri"]).resolve().as_uri(), + **({"resourceRoots": [(args.workspace / root).resolve().as_uri() for root in module["resourceRoots"]]} + if "resourceRoots" in module else {})} for module in graph["modules"]] + source_file = args.workspace / graph["document"] + source = source_file.read_text() + before = inventory(args.workspace) + offset = source.index(graph["anchor"]) + position = {"line": source[:offset].count("\n"), + "character": len(source[:offset].rsplit("\n", 1)[-1].encode("utf-16-le")) // 2} + document = {"uri": source_file.as_uri()} + report = {"workspace": str(args.workspace), "sourceFiles": len(before), "modules": modules, + "cyclesPerSession": args.cycles, "sessions": [], "heapLimit": args.heap, + "gcEvery": args.gc_every, "heapHistograms": args.heap_histograms, + "jarSha256": hashlib.sha256(args.jar.read_bytes()).hexdigest(), + "limits": "Sampled peaks are lower bounds; post-GC heap includes intentional caches. This is a controlled workload, not multi-hour interactive acceptance."} + try: + for restart in range(args.restarts + 1): + directory = args.output / f"session-{restart}" + directory.mkdir() + session = Session(args, directory, modules) + result = {"pid": session.process.pid, "cancellations": 0, "exit": None, "heapCheckpoints": []} + report["sessions"].append(result) + try: + session.initialize() + reports = session.call("workspace/diagnostic", {})["items"] + errors = [item for item in reports if any(d.get("severity", 1) == 1 for d in item.get("items", []))] + assert not errors, errors + result["diagnosticDocuments"] = len(reports) + session.notify("textDocument/didOpen", {"textDocument": {**document, "languageId": "xtc", "version": 1, "text": source}}) + for cycle in range(args.cycles): + session.notify("textDocument/didChange", {"textDocument": {**document, "version": cycle + 2}, + "contentChanges": [{"text": source + f"\n// workload {restart}:{cycle}\n"}]}) + request_id, pending = session.request("textDocument/references", { + "textDocument": document, "position": position, "context": {"includeDeclaration": True}}) + assert not pending.done(), "Cancellation race not exercised" + session.notify("$/cancelRequest", {"id": request_id}) + try: + pending.result(args.timeout) + raise AssertionError("Reference completed before cancellation took effect") + except RpcError as error: + assert error.code == -32800, error + result["cancellations"] += 1 + assert session.call("textDocument/documentSymbol", {"textDocument": document}) + assert session.call("textDocument/hover", {"textDocument": document, "position": position}) + diagnostics = session.call("textDocument/diagnostic", {"textDocument": document}) + assert not diagnostics.get("items"), diagnostics + if args.gc_every and (cycle + 1) % args.gc_every == 0: + result["heapCheckpoints"].append({"cycle": cycle + 1, "heap": session.collect_heap(cycle + 1)}) + if (cycle + 1) % 100 == 0: + print(f"session {restart + 1}: {cycle + 1}/{args.cycles} edit/cancel cycles", flush=True) + session.notify("textDocument/didClose", {"textDocument": document}) + result["exit"] = session.finish(disconnect=restart % 2 == 1) + result["status"] = "passed" + print(f"session {restart + 1}/{args.restarts + 1}: PID {session.process.pid} exited, {args.cycles} cycles", flush=True) + finally: + result["timings"] = {method: percentiles([t["ms"] for t in session.timings if t["method"] == method]) + for method in sorted({t["method"] for t in session.timings})} + result["sampledPeakHeapBytes"] = max((s["heap"]["usedBytes"] for s in session.samples), default=0) + result["sampledPeakRssBytes"] = max((s.get("rssBytes", 0) for s in session.samples), default=None) if args.sample_rss else None + result["maxQueueSize"] = max((s["queue"]["queueSize"] for s in session.samples), default=0) + result["maxRunningJobs"] = max((s["queue"]["runningSize"] for s in session.samples), default=0) + result["samplingErrors"] = session.sampling_errors + session.cleanup() + result.update(trace_statistics(directory)) + report["status"] = "passed" + except (Exception, KeyboardInterrupt) as error: + report["status"], report["error"] = "failed", str(error) + raise + finally: + report["sourcesUnchanged"] = before == inventory(args.workspace) + (args.output / "results.json").write_text(json.dumps(report, indent=2) + "\n") + assert report["sourcesUnchanged"], "Workspace source contents changed during the workload" + + +def semantic_workload(args): + """Measure detached queries after diagnostics confirm compilation has completed.""" + report = {"jarSha256": hashlib.sha256(args.jar.read_bytes()).hexdigest(), "cases": []} + try: + for methods in args.semantic_methods: + for inferred in (False, True): + directory = args.output / f"methods-{methods}-{'inferred' if inferred else 'plain'}" + workspace = directory / "workspace" + workspace.mkdir(parents=True) + source_file = workspace / "LargeFile.x" + body = "var local = value; return local;" if inferred else "return value;" + source = "module LargeFile {\n static Int value = 1;\n" + "\n".join( + f" Int read{index}() {{ {body} }}" for index in range(methods) + ) + "\n}\n" + source_file.write_text(source) + document = {"uri": source_file.as_uri()} + session_args = SimpleNamespace(**{**vars(args), "workspace": workspace}) + session = Session(session_args, directory, [{"name": "LargeFile", **document}]) + result = {"methods": methods, "inferred": inferred, "pid": session.process.pid, "queries": []} + report["cases"].append(result) + try: + session.initialize() + session.notify("textDocument/didOpen", {"textDocument": { + **document, "languageId": "xtc", "version": 1, "text": source, + }}) + diagnostics = session.call("textDocument/diagnostic", {"textDocument": document}) + assert not diagnostics.get("items"), diagnostics + queue = session.call("xtc/languageServiceStatus")["compilerQueue"] + result["compilesBeforeQueries"] = queue["startedTotal"] + position = {"line": 1, "character": 16} + queries = [ + ("textDocument/hover", {"position": position}), + ("textDocument/references", {"position": position, "context": {"includeDeclaration": True}}), + ("textDocument/inlayHint", {"range": {"start": {"line": 2, "character": 0}, "end": {"line": 3, "character": 0}}}), + ("textDocument/inlayHint", {"range": {"start": {"line": 0, "character": 0}, "end": {"line": methods + 3, "character": 0}}}), + ("textDocument/semanticTokens/full", {}), + ] + for cycle in range(args.cycles): + for method, params in queries: + started = time.perf_counter() + reply = session.call(method, {"textDocument": document, **params}) + elapsed = (time.perf_counter() - started) * 1000 + if method.endswith("hover"): + assert reply and "Int" in str(reply), reply + elif method.endswith("references"): + assert len(reply) == methods + 1, len(reply) + elif method.endswith("inlayHint"): + expected = (1 if params["range"]["start"]["line"] == 2 else methods) if inferred else 0 + assert len(reply) == expected, (len(reply), expected) + else: + assert len(reply["data"]) >= methods * 5, len(reply["data"]) + measurement = {"method": method, "cycle": cycle, "params": params, "ms": elapsed, + "responseBytes": len(json.dumps(reply).encode())} + result["queries"].append(measurement) + print(f"{methods} methods, inferred={inferred}: {method} {elapsed:.1f} ms", flush=True) + result["queueAfterQueries"] = session.call("xtc/languageServiceStatus")["compilerQueue"] + result["exit"] = session.finish() + result["status"] = "passed" + finally: + result["sampledPeakHeapBytes"] = max((s["heap"]["usedBytes"] for s in session.samples), default=0) + session.cleanup() + result.update(trace_statistics(directory)) + report["status"] = "passed" + except (Exception, KeyboardInterrupt) as error: + report["status"], report["error"] = "failed", str(error) + raise + finally: + (args.output / "results.json").write_text(json.dumps(report, indent=2) + "\n") + + +def configuration_workload(args): + """Replace generated graphs, or add/remove an unrelated root beside a real project graph.""" + workspace = args.workspace.resolve() if args.workspace else args.output / "workspace" + if args.workspace: + modules = [{**module, "uri": (workspace / module["uri"]).resolve().as_uri(), + **({"resourceRoots": [(workspace / root).resolve().as_uri() for root in module["resourceRoots"]]} + if "resourceRoots" in module else {})} for module in json.loads(args.graph.read_text())["modules"]] + added = args.output / "ConfigurationProbe.x" + added.write_text("module ConfigurationProbe { Int value = 1; }\n") + replacement = modules + [{"name": "ConfigurationProbe", "uri": added.as_uri()}] + changes = [("add-unrelated", replacement), ("unchanged", replacement), ("remove-unrelated", modules)] + else: + workspace.mkdir() + modules = [] + for index in range(args.configuration_documents): + source = workspace / f"Module{index}.x" + source.write_text(f"module Module{index} {{\n Int value = {index};\n}}\n") + modules.append({"name": source.stem, "uri": source.as_uri()}) + replacement = [{**module, **({"resourceRoots": []} if index == 0 else {})} + for index, module in enumerate(modules)] + changes = [("one-module", replacement), ("unchanged", replacement), + ("remove-graph", []), ("restore-graph", modules)] + session_args = SimpleNamespace(**{**vars(args), "workspace": workspace}) + session = Session(session_args, args.output, modules) + before_sources = inventory(workspace) + report = {"jarSha256": hashlib.sha256(args.jar.read_bytes()).hexdigest(), + "workspace": str(workspace), "pid": session.process.pid, "changes": [], + "limits": "Dispatch timing is the first status reply after the notification; settled timing includes current document symbols. No GUI or timeout extension."} + + def check_documents(): + results = {} + for document in documents: + symbols = session.call("textDocument/documentSymbol", {"textDocument": document}) + assert symbols, document + diagnostic = session.call("textDocument/diagnostic", {"textDocument": document}) + assert not any(item.get("severity", 1) == 1 for item in diagnostic.get("items", [])), diagnostic + results[document["uri"]] = {"symbols": symbols, "diagnostics": diagnostic.get("items", [])} + return results + + try: + session.initialize() + if args.workspace: + reports = session.call("workspace/diagnostic", {})["items"] + assert not any(d.get("severity", 1) == 1 for item in reports for d in item.get("items", [])), reports + documents = [{"uri": uri} for uri in sorted({item["uri"] for item in reports})[:args.configuration_documents]] + else: + documents = [{"uri": module["uri"]} for module in modules] + assert documents, "No source documents in the selected graph" + report["documents"] = len(documents) + for document in documents: + session.notify("textDocument/didOpen", {"textDocument": { + **document, "languageId": "xtc", "version": 1, + "text": pathlib.Path(urllib.parse.unquote(urllib.parse.urlsplit(document["uri"]).path)).read_text(), + }}) + baseline = check_documents() + for cycle in range(args.cycles): + for label, graph in changes: + before = session.call("xtc/languageServiceStatus")["compilerQueue"] + started = time.perf_counter() + session.notify("workspace/didChangeConfiguration", { + "settings": {"xtc": {"compiler": {"sourceModules": graph}}}}) + session.call("xtc/languageServiceStatus") + dispatch_ms = (time.perf_counter() - started) * 1000 + assert check_documents() == baseline, f"Document symbols or diagnostics changed after {label}" + after = session.call("xtc/languageServiceStatus")["compilerQueue"] + change = {"cycle": cycle, "change": label, "dispatchMs": dispatch_ms, + "settledMs": (time.perf_counter() - started) * 1000, + "submitted": after["submittedTotal"] - before["submittedTotal"], + "started": after["startedTotal"] - before["startedTotal"]} + report["changes"].append(change) + print(json.dumps(change), flush=True) + report["exit"] = session.finish() + report["status"] = "passed" + except (Exception, KeyboardInterrupt) as error: + report["status"], report["error"] = "failed", str(error) + raise + finally: + session.cleanup() + report["sourcesUnchanged"] = before_sources == inventory(workspace) + report.update(trace_statistics(args.output)) + (args.output / "results.json").write_text(json.dumps(report, indent=2) + "\n") + assert report["sourcesUnchanged"], "Configuration workload modified source content" + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--workspace", type=pathlib.Path) + parser.add_argument("--jar", required=True, type=pathlib.Path) + parser.add_argument("--graph", type=pathlib.Path, default=pathlib.Path(__file__).resolve().parents[1] / "test-fixtures/compiler-workload/platform.json") + parser.add_argument("--output", required=True, type=pathlib.Path) + parser.add_argument("--java", default="java") + parser.add_argument("--heap", default="2g") + parser.add_argument("--cycles", type=int, default=10) + parser.add_argument("--restarts", type=int, default=2) + parser.add_argument("--timeout", type=float, default=120) + parser.add_argument("--sample-rss", action="store_true", help="Sample owned server RSS through ps on macOS/Linux") + parser.add_argument("--gc-every", type=int, default=0, help="Record post-GC heap every N project edit cycles (0 disables)") + parser.add_argument("--jcmd", default="jcmd") + parser.add_argument("--heap-histograms", action="store_true", + help="Retain live-object histograms at the --gc-every project checkpoints") + parser.add_argument("--semantic-methods", nargs="+", type=int, + help="Generate isolated large files and measure queries after compilation") + parser.add_argument("--configuration-documents", type=int, + help="Measure graph replacement with this many generated buffers, or up to this many real --workspace documents") + args = parser.parse_args() + assert args.cycles > 0 and args.restarts >= 0 and args.timeout > 0 + assert args.gc_every >= 0 + assert not (args.semantic_methods and args.configuration_documents), "Select one generated workload" + assert not args.heap_histograms or (args.gc_every > 0 and not args.semantic_methods and not args.configuration_documents), "Histograms require project editing --gc-every checkpoints" + assert not args.sample_rss or sys.platform in ("darwin", "linux"), "RSS sampler supports macOS/Linux" + args.jar, args.output = args.jar.resolve(), args.output.resolve() + args.output.mkdir(parents=True, exist_ok=False) + if args.configuration_documents is not None: + assert args.configuration_documents > 0 + configuration_workload(args) + elif args.semantic_methods: + assert all(count > 0 for count in args.semantic_methods) + semantic_workload(args) + else: + assert args.workspace is not None, "--workspace is required for the project workload" + args.workspace = args.workspace.resolve() + run(args) diff --git a/lang/test-fixtures/color-values/ColorPrototype.x b/lang/test-fixtures/color-values/ColorPrototype.x new file mode 100644 index 00000000000..5474c09670a --- /dev/null +++ b/lang/test-fixtures/color-values/ColorPrototype.x @@ -0,0 +1,17 @@ +/** + * L77 fixture only: byte-valued sRGB with straight (unpremultiplied) alpha. + * This is not a public XDK graphics/color API. + */ +module ColorPrototype { + const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {} + + Rgba accent = new Rgba(255, 128, 0); + + Rgba translucent() { + return new Rgba(blue = 40, red = 200, alpha = 128, green = 80); + } + + // Dynamic values and color-looking text deliberately have no swatch. + Rgba dynamic(UInt8 red) = new Rgba(red, 128, 0); + String ordinaryText = "#ff8000"; +} diff --git a/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts b/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts new file mode 100644 index 00000000000..5a1eacbf20c --- /dev/null +++ b/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts @@ -0,0 +1,49 @@ +import java.util.Properties +import java.util.concurrent.TimeUnit + +// A real Gradle producer with deterministic publication/exit gates. The XTC plugin's evaluated +// model is tested by TestKit; this fixture isolates the two IDEs' import and cancellation lifecycle. +abstract class ExportPlaybookModel : DefaultTask() { + @get:Internal + abstract val controlDirectory: DirectoryProperty + + @get:Internal + abstract val report: RegularFileProperty + + @TaskAction + fun export() { + val control = controlDirectory.get().asFile + val request = Properties().apply { control.resolve("request.properties").reader().use(::load) } + val operation = request.getProperty("operation") + val target = report.get().asFile + target.parentFile.mkdirs() + when (request.getProperty("output")) { + "valid" -> control.resolve("model.json").copyTo(target, overwrite = true) + "invalid" -> target.writeText("{") + "missing" -> target.delete() + else -> error("Unknown fixture output") + } + control.resolve("invocations.txt").appendText("$operation:$name\n") + val released = control.resolve("$operation.release") + control.resolve("$operation.pid").writeText(ProcessHandle.current().pid().toString()) + control.resolve("$operation.started").writeText(name) + try { + val deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(90) + while (!released.exists()) { + check(System.nanoTime() < deadline) { "Playbook did not release import $operation" } + Thread.sleep(50) + } + check(request.getProperty("outcome") == "success") { "Deliberate playbook import failure" } + } finally { + control.resolve("$operation.finished").writeText(name) + } + } +} + +listOf("exportXtcLspModel", "prepareXtcLspModel").forEach { taskName -> + tasks.register(taskName) { + controlDirectory.set(layout.projectDirectory.dir(".compiler-import-playbook")) + report.set(layout.projectDirectory.file(".gradle/xtc/lsp-model.json")) + doNotTrackState("Each invocation is controlled by the editor playbook") + } +} diff --git a/lang/test-fixtures/compiler-playbook/compiler-import/gradle.properties b/lang/test-fixtures/compiler-playbook/compiler-import/gradle.properties new file mode 100644 index 00000000000..abd3dad4ab1 --- /dev/null +++ b/lang/test-fixtures/compiler-playbook/compiler-import/gradle.properties @@ -0,0 +1,4 @@ +org.gradle.configuration-cache=true +org.gradle.daemon.idletimeout=60000 +org.gradle.jvmargs=-Xmx256m +org.gradle.workers.max=1 diff --git a/lang/test-fixtures/compiler-playbook/compiler-import/settings.gradle.kts b/lang/test-fixtures/compiler-playbook/compiler-import/settings.gradle.kts new file mode 100644 index 00000000000..35e967c23ee --- /dev/null +++ b/lang/test-fixtures/compiler-playbook/compiler-import/settings.gradle.kts @@ -0,0 +1 @@ +rootProject.name = "ecstasy-compiler-import-playbook" diff --git a/lang/test-fixtures/compiler-playbook/scenarios.json b/lang/test-fixtures/compiler-playbook/scenarios.json new file mode 100644 index 00000000000..63dc442d33b --- /dev/null +++ b/lang/test-fixtures/compiler-playbook/scenarios.json @@ -0,0 +1,8837 @@ +{ + "schemaVersion": 2, + "common": { + "sourceModules": [ + { + "name": "Library", + "uri": "Library.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Library" + ] + } + ], + "fixtures": [ + { + "file": "Navigation.x", + "pattern": "^module Navigation\\s*\\{" + }, + { + "file": "Editing.x", + "pattern": "^module Editing\\s*\\{" + }, + { + "file": "Project.x", + "pattern": "^module Project\\s*\\{" + }, + { + "file": "Lookups.x", + "pattern": "^module Lookups\\s*\\{" + }, + { + "file": "Consumers.x", + "pattern": "^module Consumers\\s*\\{" + }, + { + "file": "Library.x", + "pattern": "^module Library\\s*\\{" + }, + { + "file": "Consumer.x", + "pattern": "^module Consumer\\s*\\{" + }, + { + "file": "Rename.x", + "pattern": "^module Rename\\s*\\{" + }, + { + "file": "Contracts.x", + "pattern": "^module Contracts\\s*\\{" + }, + { + "file": "Uses.x", + "pattern": "^module Uses\\s*\\{" + }, + { + "file": "Dormant.x", + "pattern": "^module Dormant\\s*\\{" + }, + { + "file": "Properties.x", + "pattern": "^module Properties\\s*\\{" + }, + { + "file": "Advanced.x", + "pattern": "^module Advanced\\s*\\{" + }, + { + "file": "DupAnno.x", + "pattern": "^module DupAnno\\s*\\{" + }, + { + "file": "Project/Child.x", + "pattern": "^class Child extends Base \\{" + } + ], + "editing": { + "file": "Editing.x", + "run": "void run(Box box, String itemParameter, Object value) {}", + "inspect": "void inspect(T itemLocal) {}" + }, + "project": { + "root": "Project.x", + "member": "Project/Child.x" + }, + "dependency": { + "consumer": "Consumer.x", + "stringLibrary": "module Library { static String value() = \"text\"; }", + "brokenLibrary": "module Library { MissingType broken; }", + "blockedCode": "DEPENDENCY-FAILED" + }, + "graph": { + "sourceModules": [ + { + "name": "Contracts", + "uri": "Contracts.x", + "dependencies": [] + }, + { + "name": "Uses", + "uri": "Uses.x", + "dependencies": [ + "Contracts" + ] + }, + { + "name": "Dormant", + "uri": "Dormant.x", + "dependencies": [ + "Contracts" + ] + } + ], + "root": "Contracts.x", + "referenceAnchor": "map(" + }, + "compilerImport": { + "file": "ImportAssets.x", + "module": "ImportAssets", + "source": "module ImportAssets {\n String text = $./data.txt;\n}\n", + "resource": "processed/data.txt", + "contents": "Shared compiler import fixture\n", + "model": { + "schemaVersion": 1, + "sourceSets": [ + { + "projectId": "${workspace}#:app", + "projectPath": ":app", + "projectDirectory": "${workspace}/", + "buildFile": "${workspace}/build.gradle.kts", + "sourceSet": "main", + "sourceRoots": [ + "${workspace}/" + ], + "sourceFiles": [ + "${workspace}/ImportAssets.x" + ], + "moduleRoots": [ + "${workspace}/ImportAssets.x" + ], + "resourceSourceRoots": [ + "${workspace}/raw/" + ], + "resourceRoots": [ + "${workspace}/processed/" + ], + "resourceTask": ":app:processXtcResources", + "projectDependencies": [], + "modulePath": [] + } + ] + } + }, + "librarySettings": { + "module": "PlaybookLibrary", + "binaryFile": "PlaybookLibrary.xtc", + "sourceFile": "PlaybookLibrary.x", + "source": "module PlaybookLibrary {\n static Int answer() = 1;\n}\n", + "replacementSource": "module PlaybookLibrary {\n static Int answer() = 2;\n}\n", + "consumer": "module LibraryConsumer {\n package lib import PlaybookLibrary;\n Int run() = lib.answer();\n}\n", + "consumerFile": "LibraryConsumer.x", + "anchor": "answer", + "definitionLine": 1 + }, + "runtimeSettings": { + "source": "module RuntimeSettings {\n Int value = 1;\n}\n", + "file": "RuntimeSettings.x", + "vmOptions": [ + "-Xmx768M" + ], + "invalidOptions": [ + "-Xms2G", + "-Xmx1G" + ], + "logs": { + "historyDays": 2, + "maxFileMb": 1, + "totalSizeMb": 3, + "retainedSessions": 2 + }, + "startupFailureOptions": [ + "-Xmx1K" + ] + }, + "colorPrototype": { + "cases": [ + "X273", + "X274", + "X275" + ], + "file": "ColorPrototype.x", + "source": "/**\n * L77 fixture only: byte-valued sRGB with straight (unpremultiplied) alpha.\n * This is not a public XDK graphics/color API.\n */\nmodule ColorPrototype {\n const Rgba(UInt8 red, UInt8 green, UInt8 blue, UInt8 alpha = 255) {}\n\n Rgba accent = new Rgba(255, 128, 0);\n\n Rgba translucent() {\n return new Rgba(blue = 40, red = 200, alpha = 128, green = 80);\n }\n\n // Dynamic values and color-looking text deliberately have no swatch.\n Rgba dynamic(UInt8 red) = new Rgba(red, 128, 0);\n String ordinaryText = \"#ff8000\";\n}\n", + "colors": [ + { + "anchor": "new Rgba(255, 128, 0)", + "rgba": [ + 255, + 128, + 0, + 255 + ] + }, + { + "anchor": "new Rgba(blue = 40, red = 200, alpha = 128, green = 80)", + "rgba": [ + 200, + 80, + 40, + 128 + ] + } + ], + "invalid": { + "from": "new Rgba(255, 128, 0)", + "to": "new Rgba(256, 128, 0)" + }, + "themes": { + "vscode": [ + "Default Dark Modern", + "Default Light Modern", + "Default High Contrast" + ], + "intellij": [ + "Darcula", + "IntelliJ Light", + "High contrast" + ] + } + } + }, + "cases": { + "X1": { + "title": "diagnostics, outline, folding and selection", + "manual": [ + "Outline/folding rendering and Expand Selection keyboard behavior" + ], + "values": { + "file": "Navigation.x", + "symbolNames": [ + "Navigation", + "Holder", + "value", + "read", + "text" + ], + "minimumFolds": 3, + "anchor": "return value;", + "offset": 7 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X2": { + "title": "source errors clear after unsaved corrections", + "manual": [ + "Problems rows/icons/filter/count rendering and clicking a row; automated next-problem navigation is checked" + ], + "values": { + "file": "Navigation.x", + "errors": [ + { + "edit": { + "from": "Int value = 2;", + "to": "String value = 2;" + }, + "severity": "ERROR", + "codePrefix": "COMPILER-", + "messageContains": "", + "rangeStart": "§String value = 2;", + "rangeEnd": "return §value + this.value;" + }, + { + "edit": { + "from": "Object value = input;", + "to": "Object value = missing;" + }, + "severity": "ERROR", + "codePrefix": "COMPILER-", + "messageContains": "missing", + "rangeStart": "Object value = §missing;", + "rangeEnd": "Object value = missing§;" + } + ], + "cleanAppend": "// ERROR: test\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X3": { + "title": "narrowed hover preserves declaration identity", + "manual": [ + "Hover popup presentation" + ], + "values": { + "file": "Navigation.x", + "narrowUse": "return value;", + "offset": 7, + "wideUse": "return value.toString()", + "narrowedType": { + "source": "String", + "flags": "" + }, + "declaredType": { + "source": "Object", + "flags": "" + }, + "targetCount": 1, + "declaration": "Object value = input" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X4": { + "title": "local and property references remain separate", + "manual": [], + "values": { + "targetCount": 1, + "file": "Navigation.x", + "locations": [ + { + "cursor": "return §value + this.value;", + "targetFile": "Navigation.x", + "target": "Int §value = 2;" + }, + { + "cursor": "this.§value", + "targetFile": "Navigation.x", + "target": "Int §value = 1;" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X5": { + "title": "library source navigation and parser recovery", + "manual": [], + "values": { + "file": "Navigation.x", + "libraryType": "String text", + "missingCloser": "}", + "parserCodePrefix": "PARSER-", + "retainedSymbol": "Holder", + "anchor": "return value;", + "offset": 7, + "libraryTarget": "String" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X6": { + "title": "receiver members and exact prefix replacement", + "manual": [ + "Completion popup appearance and keyboard acceptance" + ], + "values": { + "bodies": [ + "box.§;", + "box.it§;" + ], + "label": "item", + "type": { + "source": "String", + "flags": "" + }, + "excludedLabel": "itemMethod", + "acceptedExpression": "box.item;", + "validStatement": "String selected = box.item;" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X7": { + "title": "expression receiver completion", + "manual": [], + "values": { + "file": "Editing.x", + "edit": { + "from": "void run(Box box, String itemParameter, Object value) {}", + "to": "void run(Box box, String itemParameter, Object value) { Int size = getValue().si; }" + }, + "cursor": "getValue().si§;", + "label": "size", + "accepted": { + "from": "getValue().si;", + "to": "getValue().size;" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X8": { + "title": "flow-narrowed receiver completion", + "manual": [], + "values": { + "body": "if (value.is(String)) { Int size = value.si§; }", + "label": "size", + "acceptedSuffix": ".size;" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X9": { + "title": "scope excludes unreadable and later locals", + "manual": [], + "values": { + "body": "Int itemLocal = 1; Int itemUnassigned; item§; Int itemLater = 2;", + "include": [ + "itemLocal", + "itemParameter" + ], + "exclude": [ + "itemUnassigned", + "itemLater" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X10": { + "title": "empty statement scope inserts without deleting syntax", + "manual": [], + "values": { + "body": "§", + "label": "itemParameter" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X11": { + "title": "implicit members respect shadowing and closed scopes", + "manual": [], + "values": { + "body": "ite§;", + "include": [ + "item", + "itemMethod", + "itemLocal" + ], + "label": "item", + "genericType": { + "source": "\\bT\\b", + "flags": "" + }, + "shadowedBody": "{ Int itemClosed = 1; } Int item = 2; ite§;", + "shadowedType": { + "source": "Int", + "flags": "" + }, + "excludedLabel": "itemClosed" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X12": { + "title": "static completion enforces access and receiver kind", + "manual": [], + "values": { + "body": "Tools.item§;", + "include": [ + "itemFunction", + "itemConstant", + "itemType" + ], + "exclude": [ + "itemProperty", + "itemMethod", + "itemHidden", + "itemPrivate" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X13": { + "title": "implicit, aliased and wildcard imported names", + "manual": [], + "values": { + "variants": [ + { + "prefix": "Strin", + "name": "String", + "addition": "" + }, + { + "prefix": "Buffe", + "name": "Buffer", + "addition": "import ecstasy.text.StringBuffer as Buffer;" + }, + { + "prefix": "StringBuffe", + "name": "StringBuffer", + "addition": "import ecstasy.text.*;" + } + ], + "body": "${0}§;", + "moduleStart": "module Editing {", + "moduleWithImport": "module Editing { ${0}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X14": { + "title": "completion edits preserve emoji and CRLF positions", + "manual": [], + "values": { + "file": "Editing.x", + "method": "void run(Box box, String itemParameter, Object value) {}", + "incompleteMethod": "void run(Box box, String itemParameter, Object value) { /* 😀 */ Int size = getValue().si§; }", + "lineBreak": { + "source": "\\n", + "flags": "g" + }, + "label": "size", + "prefix": "getValue().si;", + "accepted": "getValue().size;" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X15": { + "title": "written arguments filter incomplete overloads", + "manual": [ + "Parameter hint popup switches without retaining the previous overload" + ], + "values": { + "variants": [ + { + "argument": "\"x\"", + "type": "String" + }, + { + "argument": "1", + "type": "Int" + } + ], + "body": "box.choose(${0}, §);", + "expected": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X16": { + "title": "named arguments map to the declared parameter", + "manual": [], + "values": { + "body": "box.pair(second = \"x\", first = §);", + "expected": 1, + "type": { + "source": "String", + "flags": "" + }, + "activeParameter": 0, + "replaceFrom": "first = )", + "replaceWith": "first = \"y\")", + "call": "box.pair", + "callOffset": 4, + "completedArgument": "first = \"y\"", + "argumentOffset": 9 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X17": { + "title": "generic inference retains unbound formals", + "manual": [], + "values": { + "variants": [ + { + "body": "box.generic(\"x\", §);", + "type": "String" + }, + { + "body": "box.generic(§);", + "type": "U" + } + ], + "typePattern": "\\b${0}\\b" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X18": { + "title": "incompatible and invalid named arguments have no candidate", + "manual": [], + "values": { + "bodies": [ + "box.choose(True, §);", + "box.pair(unknown = §);", + "box.pair(first = \"x\", first = §);" + ], + "signatureCount": 0 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X19": { + "title": "implicit instance and static signatures", + "manual": [], + "values": { + "receivers": [ + true, + false + ], + "instanceBody": "pair(itemLocal, §);", + "staticBody": "Tools.join(\"x\", §);", + "replaceFrom": "class Tools {", + "replaceWith": "class Tools { static String join(String first, String second) = first;", + "activeParameter": 1, + "instanceArgument": "itemLocal", + "staticArgument": "\"y\"" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X20": { + "title": "named-slot ambiguity and selected overload navigation", + "manual": [], + "values": { + "body": "box.pair(second = \"x\", §);", + "bodies": [ + "box.choose(\"x\", \"y\"); §", + "box.choose(1, 2); §" + ], + "anchor": "box.choose", + "offset": 4, + "targetCount": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X21": { + "title": "closed module members participate in navigation", + "manual": [], + "values": { + "anchor": "Base", + "file": "Project/Child.x", + "symbolName": "Child", + "uses": [ + "Base", + "echo(\"member\")" + ], + "targetCount": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X22": { + "title": "direct generic type hierarchy spans files", + "manual": [ + "Type-hierarchy tree rendering and editor action availability" + ], + "values": { + "baseDeclaration": "Base", + "childCount": 1, + "childName": "Child", + "file": "Project/Child.x", + "display": "${0} ${1}", + "typeArgument": "String", + "interfaceName": "Named", + "interfaceDeclaration": "interface Named", + "offset": 10, + "baseName": "Base" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X23": { + "title": "unsaved root edits invalidate sibling diagnostics", + "manual": [], + "values": { + "memberFile": "Project/Child.x", + "rootFile": "Project.x", + "replaceFrom": "class Base", + "replaceWith": "class Renamed", + "diagnosticCode": "EMB-5" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X24": { + "title": "member completion sees the unsaved root type", + "manual": [], + "values": { + "memberFile": "Project/Child.x", + "method": "String answer()", + "incompleteMethod": "void inspect() { ite§; }\n String answer()", + "types": [ + "Int", + "String", + "Int" + ], + "rootFile": "Project.x", + "replaceFrom": "Int item = 1;", + "replaceWith": "${0} item = ${1};", + "integerType": "Int", + "integerValue": "1", + "stringValue": "\"overlay\"", + "label": "item" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X25": { + "title": "named unsaved virtual member joins the module", + "manual": [ + "Named nonexistent file is supplied through the LSP client; an ordinary Untitled editor is not equivalent" + ], + "values": { + "file": "Project/pkg/Added.x", + "version": 1, + "text": "class Added extends Base {}", + "anchor": "Base", + "name": "Added" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X26": { + "title": "discarded member overlay restores disk semantics", + "manual": [], + "values": { + "file": "Project/Child.x", + "anchor": "Base", + "replaceWith": "Missing", + "expected": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X27": { + "title": "real file-watcher creation and deletion refresh the module", + "manual": [], + "values": { + "file": "Project/Bad.x", + "text": "class Bad extends Missing {}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X28": { + "title": "obsolete type hierarchy items cannot resolve", + "manual": [], + "values": { + "file": "Project/Child.x", + "anchor": "Child", + "replaceWith": "class Child {}", + "contains": "Base" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X29": { + "title": "rapid edits and cursor requests converge on current text", + "manual": [], + "values": { + "incompleteBody": "box.choose(1, §);", + "integerCall": "choose(1,", + "stringCall": "choose(\"x\",", + "completedBody": "box.choose(1, 2); §", + "completedCall": "choose(1, 2)", + "offset": 10, + "pattern": { + "source": "Int", + "flags": "" + }, + "memberFile": "Project/Child.x", + "rootFile": "Project.x", + "replaceFrom": "Int item = 1;", + "replaceWith": "Int item = ${0};", + "anchor": "echo(\"member\")", + "expected": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X30": { + "title": "cancellation, close, reopen and server restart recover", + "manual": [ + "Dismissing the actual popup and keyboard responsiveness" + ], + "values": { + "body": "box.it§;", + "cancellationCodes": [ + -32800, + -32801 + ], + "file": "Editing.x", + "label": "item" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X31": { + "title": "compiler capabilities advertise supported features and omit unavailable providers", + "manual": [ + "Native formatting behavior is additionally exercised by X102 and X107" + ], + "values": { + "file": "Navigation.x", + "unsupportedCapabilities": [ + "inlineValueProvider" + ], + "prototypeCapability": "colorProvider", + "tabSize": 4, + "supportedCapabilities": [ + "declarationProvider", + "documentFormattingProvider", + "documentRangeFormattingProvider", + "codeActionProvider", + "codeLensProvider", + "monikerProvider" + ], + "unformatted": "module Navigation {\nInt value=1;\n}\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X32": { + "title": "invalid argument slots do not invent completions", + "manual": [], + "values": { + "bodies": [ + "box.pair(unknown = §);", + "box.pair(first = \"x\", first = §);", + "box.pair(True, §);", + "box.pair(\"x\", \"y\", §);", + "missing(§);" + ], + "completionCount": 0 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X33": { + "title": "type definitions follow variable and return types", + "manual": [], + "values": { + "file": "Lookups.x", + "variants": [ + { + "anchor": "mapper.map", + "expected": "Mapper" + }, + { + "anchor": "make();", + "expected": "TextMapper" + }, + { + "anchor": "make() =", + "expected": "TextMapper" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X34": { + "title": "union and narrowed types have distinct source targets", + "manual": [ + "Multiple-target chooser and peek rendering" + ], + "values": { + "file": "Lookups.x", + "narrowedUse": "value.toString();", + "narrowedOffset": 0, + "narrowedTargets": [ + "TextMapper" + ], + "unionUse": " value.toString();", + "unionOffset": 8, + "unionTargets": [ + "TextMapper", + "Unrelated" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X35": { + "title": "formal types and matching library sources resolve", + "manual": [], + "values": { + "file": "Lookups.x", + "formalUse": "T value", + "offset": 2, + "formalTargets": [ + "T" + ], + "binaryUse": "String map", + "binaryTargets": [ + "String" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X36": { + "title": "nominal implementation lookup excludes structural lookalikes", + "manual": [], + "values": { + "file": "Lookups.x", + "anchor": "Mapper", + "implementations": [ + "Child", + "Inherited", + "TextMapper" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X37": { + "title": "method implementation lookup preserves overload identity", + "manual": [], + "values": { + "file": "Lookups.x", + "overrides": { + "source": "@Override String map", + "flags": "g" + }, + "declarationPrefix": "@Override String ", + "declarationOffset": 2, + "declaration": "T map", + "use": "mapper.map", + "useOffset": 7, + "methodName": "map(", + "targetCount": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X38": { + "title": "cross-file semantic lookups track current member positions", + "manual": [], + "values": { + "rootFile": "Project.x", + "replaceFrom": "module Project {", + "rootWithFactory": "module Project {\n Child make() = new Child();", + "factory": "Child make", + "targetCount": 1, + "memberFile": "Project/Child.x", + "method": "echo(", + "lineShift": 2, + "replaceWith": "class Child extends" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X39": { + "title": "call hierarchy groups sites by caller and overload", + "manual": [ + "Call hierarchy view rendering and click navigation" + ], + "values": { + "file": "Consumers.x", + "callee": "Int leaf", + "offset": 4, + "edgeCount": 2, + "callerName": "run", + "lambdaName": "lambda", + "lambdaSites": 1, + "caller": "Int run", + "sitesPerCallee": [ + 1, + 2 + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X40": { + "title": "lambda calls retain their owner and dynamic calls have no static edge", + "manual": [], + "values": { + "file": "Consumers.x", + "callee": "Int leaf", + "offset": 4, + "lambdaName": "lambda", + "edgeCount": 1, + "lambdaCall": "leaf(seed)", + "dynamicCall": "fn();" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X41": { + "title": "semantic tokens and highlights distinguish identity and access", + "manual": [ + "Theme colors and the token inspector UI" + ], + "values": { + "file": "Consumers.x", + "tokenKinds": [ + { + "name": "leaf", + "type": "function" + }, + { + "name": "seed", + "type": "parameter" + }, + { + "name": "number", + "type": "variable" + } + ], + "methodName": "leaf", + "staticModifier": "static", + "declarationModifier": "declaration", + "anchor2": "number +=", + "variableName": "number", + "writeModifier": "modification", + "anchor": "return number", + "offset": 7 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X42": { + "title": "inlay hints show inferred locals, lambda signatures and positional argument names", + "manual": [ + "Inline hint layout and visual distinction from source text" + ], + "values": { + "file": "Consumers.x", + "hints": [ + "Int", + "String", + "input:", + "text:" + ], + "anchor": "number +=", + "typeHintCount": 3, + "excludedHint": "extra:", + "inferred": { + "source": "module Consumers {\n (Int, String) pair() = (1, \"x\");\n void run() {\n (var number, var text) = pair();\n function Int(Int) fn = (value) -> value;\n number += 1;\n }\n}", + "hints": [ + "Int", + "String" + ], + "anchor": "number +=", + "typeHintCount": 4, + "excludedHint": "extra:" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X43": { + "title": "call hierarchy rejects old snapshots and recovers after parse failure", + "manual": [], + "values": { + "file": "Consumers.x", + "anchor": "Int run", + "offset": 4, + "replaceFrom": " Int run", + "shiftedDeclaration": "\n Int run", + "lineShift": 1, + "edgeCount": 2, + "replaceWith": "Int broken(\n Int run" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X44": { + "title": "incoming calls include closed members and current overlays", + "manual": [], + "values": { + "memberFile": "Project/Child.x", + "memberDeclaration": "class Child extends Base {", + "memberWithCall": "class Child extends Base {\n Int callTarget() = target(1);", + "rootFile": "Project.x", + "replaceFrom": "module Project {", + "replaceWith": "module Project {\n static Int target(Int n) = n;", + "anchor": "Int target", + "offset": 4, + "lineShift": 2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X45": { + "title": "source dependencies compile and navigate without opening the library", + "manual": [], + "values": { + "file": "Consumer.x", + "location": { + "cursor": "lib.§value", + "targetFile": "Library.x", + "target": "Int §value() = 1;" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X46": { + "title": "unsaved library type changes recompile unchanged consumers", + "manual": [], + "values": { + "diagnosticCode": "DEPENDENCY-FAILED", + "file": "Library.x", + "consumer": "Consumer.x", + "edit": { + "from": "Int value() = 1", + "to": "String value() = \"text\"" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X47": { + "title": "dependency failure owns diagnostics and removes stale navigation", + "manual": [], + "values": { + "file": "Library.x", + "compilerCodePrefix": "COMPILER-", + "anchor": "lib.value", + "offset": 4 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X48": { + "title": "discarding a dependency overlay restores the disk artifact", + "manual": [], + "values": { + "file": "Library.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X49": { + "title": "watched dependency deletion and restoration propagate", + "manual": [], + "values": { + "file": "Library.x", + "diagnosticCode": "SOURCE-UNAVAILABLE" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X50": { + "title": "unsaved and saved dependency members invalidate and recover consumers", + "manual": [ + "Named unsaved member uses LSP didOpen/didClose; filesystem half uses the real VS Code watcher" + ], + "values": { + "file": "Library/Extra.x", + "text": "class Extra { MissingType broken; }", + "version": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X51": { + "title": "rapid dependency changes converge without damaging unrelated sessions", + "manual": [], + "values": { + "unrelatedFile": "Navigation.x", + "libraryFile": "Library.x", + "anchor": "lib.value", + "offset": 4 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X52": { + "title": "transitive source changes propagate through the dependency graph", + "manual": [], + "values": { + "bridgeText": "module Bridge { package lib import Library; static Int value() = lib.value(); }", + "libraryFile": "Library.x", + "bridgeFile": "Bridge.x", + "consumerFile": "Consumer.x", + "replaceFrom": "import Library", + "bridgeImport": "import Bridge", + "sourceModules": [ + { + "name": "Library", + "uri": "Library.x", + "dependencies": [] + }, + { + "name": "Bridge", + "uri": "Bridge.x", + "dependencies": [ + "Library" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Bridge" + ] + } + ], + "brokenBridge": "module Bridge { MissingType broken; }", + "anchor": "lib.value", + "offset": 4 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X53": { + "title": "local rename updates declarations and captured uses", + "manual": [ + "F2 input widget, preview and undo interaction" + ], + "values": { + "file": "Rename.x", + "anchor": "local", + "replaceWith": "renamed", + "replaceFrom": { + "source": "\\blocal\\b", + "flags": "g" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X54": { + "title": "private parameter rename includes named argument labels", + "manual": [], + "values": { + "file": "Rename.x", + "anchors": [ + "Int input", + "input = 1" + ], + "declaration": "Int input", + "callOffset": 4, + "declarationOffset": 0, + "replaceWith": "number", + "replaceFrom": { + "source": "\\binput\\b", + "flags": "g" + }, + "namedArgument": "number = 1" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X55": { + "title": "rename rejects silent capture of an untouched property use", + "manual": [], + "values": { + "file": "Rename.x", + "anchor": "local", + "newName": "value" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X56": { + "title": "unsupported member and public parameter renames produce no edits", + "manual": [], + "values": { + "file": "Rename.x", + "variants": [ + "pick(", + "Rename", + "value =" + ], + "newName": "replacement", + "replaceFrom": "private Int pick", + "replaceWith": "Int pick", + "anchor": "Int input", + "offset": 4 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X57": { + "title": "rename carries the buffer version and obsolete requests cannot mutate text", + "manual": [ + "Deterministic in-flight edit/close/sibling races run in XdkCursorServerTest; this editor race may complete before closing" + ], + "values": { + "file": "Rename.x", + "anchor": "local", + "newName": "renamed", + "cancellationCodes": [ + -32800, + -32801 + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X58": { + "title": "rename recovers after syntax errors and rejects invalid or conflicting names", + "manual": [], + "values": { + "file": "Rename.x", + "replaceFrom": "Int run()", + "replaceWith": "Int broken(\n Int run()", + "anchor": "local", + "newName": "renamed", + "invalidNames": [ + "not-a-name", + "captured" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X59": { + "title": "exact references include unopened configured consumers", + "manual": [ + "References panel presentation" + ], + "values": { + "referenceCount": 3, + "modules": [ + "Contracts", + "Uses", + "Dormant" + ], + "moduleFile": "${0}.x", + "closedConsumer": "Dormant.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X60": { + "title": "generic override rename edits every configured declaration and call", + "manual": [ + "F2 preview, confirmation and undo across files" + ], + "values": { + "anchor": "map(", + "replaceWith": "convert", + "moduleCount": 3, + "modules": [ + "Contracts", + "Uses", + "Dormant" + ], + "file": "${0}.x", + "replaceFrom": { + "source": "\\bmap\\b", + "flags": "g" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X61": { + "title": "graph rename rejects silent capture of an untouched overload call", + "manual": [ + "A separate compiler regression verifies the rejected source still compiles and would change overload selection" + ], + "values": { + "anchor": "pick(", + "newName": "choose", + "file": "Contracts.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X62": { + "title": "bundled XDK members resolve but their binary contracts cannot be renamed", + "manual": [], + "values": { + "file": "Uses.x", + "searchCall": "indexOf(", + "searchName": { + "source": "indexOf", + "flags": "" + }, + "searchArgumentType": { + "source": "Char", + "flags": "" + }, + "searchRename": "locate", + "descriptionCall": "toString(", + "descriptionType": { + "source": "String", + "flags": "" + }, + "descriptionRename": "describe" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X63": { + "title": "graph queries use unsaved consumer text and fail closed on an incomplete graph", + "manual": [ + "Controlled edit/close/configuration/dependency/cancellation races run in XdkProjectQueryLifecycleTest and XdkCursorServerTest" + ], + "values": { + "file": "Dormant.x", + "replaceFrom": "mapper.map(\"c\")", + "addedReference": "mapper.map(\"c\") + mapper.map(\"d\")", + "consumerReferenceCount": 2, + "anchor": "map(", + "newName": "convert", + "replaceWith": "module Dormant { MissingType broken; }", + "referenceCount": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X64": { + "title": "property implementations preserve generic identity and written accessor targets", + "manual": [ + "Implementation chooser/peek rendering and clicking the selected getter" + ], + "values": { + "file": "Properties.x", + "locations": [ + { + "anchor": "String name = \"stored\"", + "offset": 7, + "length": 4 + }, + { + "anchor": "String name.get()", + "offset": 12, + "length": 3 + } + ], + "anchor2": "T name", + "offset2": 2, + "anchor": "value.name", + "offset": 6 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X65": { + "title": "property getters setters and default field overrides have distinct implementation sets", + "manual": [], + "values": { + "file": "Properties.x", + "variants": [ + { + "anchor": "Int value {", + "offset": 4, + "expected": [ + { + "anchor": "Int get()", + "offset": 4, + "length": 3 + }, + { + "anchor": "void set(", + "offset": 5, + "length": 3 + }, + { + "anchor": "Int value.get()", + "offset": 10, + "length": 3 + } + ] + }, + { + "anchor": "Int get()", + "offset": 4, + "expected": [ + { + "anchor": "Int get()", + "offset": 4, + "length": 3 + }, + { + "anchor": "Int value.get()", + "offset": 10, + "length": 3 + } + ] + }, + { + "anchor": "void set(", + "offset": 5, + "expected": [ + { + "anchor": "void set(", + "offset": 5, + "length": 3 + } + ] + }, + { + "anchor": "String label {", + "offset": 7, + "expected": [ + { + "anchor": "String get()", + "offset": 7, + "length": 3 + }, + { + "anchor": "String label =", + "offset": 7, + "length": 5 + } + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X66": { + "title": "abstract delegated annotated and binary properties have no invented source targets", + "manual": [], + "values": { + "file": "Properties.x", + "variants": [ + { + "anchor": "String absent", + "offset": 7 + }, + { + "anchor": "Int later", + "offset": 4 + }, + { + "anchor": "text.size", + "offset": 5 + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X67": { + "title": "closed property accessors track unsaved positions and recover after parse failure", + "manual": [], + "values": { + "original": "class Member implements Named { @Override String name.get() = \"member\"; }", + "memberFile": "Properties/Member.x", + "rootFile": "Properties.x", + "anchor": "T name", + "lineShift": 2, + "targetCount": 3, + "declarationLine": 0, + "getter": "get", + "replaceWith": "class Member {" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X68": { + "title": "concrete chained delegation reaches written method and getter bodies", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "use": "forward.map", + "offset": 8, + "declaration": "String map(String value) = value", + "declarationOffset": 7, + "length": 3 + }, + { + "use": "forward.name", + "offset": 8, + "declaration": "String name.get()", + "declarationOffset": 12, + "length": 3 + } + ], + "targetCount": 1 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X69": { + "title": "super navigation reaches its inherited body", + "manual": [], + "values": { + "file": "Advanced.x", + "anchor": "super(value)", + "inheritedDeclaration": "T pick(T value)", + "inheritedOffset": 2, + "targetCount": 1, + "targetLength": 4, + "override": "String pick(String value)", + "overrideOffset": 7, + "renameRejection": "Rename not allowed at this position" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X70": { + "title": "function calls show signature types without guessed parameter names", + "manual": [], + "values": { + "file": "Advanced.x", + "anchor": "fn(42)", + "offset": 4, + "signature": "Int fn(Int)", + "targetCount": 1, + "targetName": "fn" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X71": { + "title": "compound conditional and trailing argument edits preserve completion scope", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "1 + word.", + "flag ? word. : 0", + "flag ? 0 : word.", + "pair(word., 2)" + ], + "replaceFrom": "return 1 + word.size;", + "replaceWith": "return ${0};", + "anchor": "word.", + "offset": 5, + "label": "size" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X72": { + "title": "annotation accessors follow compiler composition and explicit overrides", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "use": "meter.count", + "getter": "Referent get() = super()", + "getterOffset": 9, + "setter": "void set(Referent value)", + "setterOffset": 5 + }, + { + "use": "meter.computed", + "getter": "Int get() = 3", + "getterOffset": 4, + "setter": "void set(Int value)", + "setterOffset": 5 + } + ], + "offset": 6, + "targetCount": 2, + "targetLength": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X73": { + "title": "incomplete function values retain signatures and reject incompatible arguments", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "call": "fn(", + "active": 0 + }, + { + "call": "fn(1, ", + "active": 1 + } + ], + "replaceFrom": "fn(1, \"x\")", + "incompleteExpression": "${0})", + "callAnchor": "Int applyPair(function Int(Int, String) fn) = ${0}", + "signature": "Int fn(Int, String)", + "invalidExpression": "fn(True, )", + "invalidCall": "fn(True, ", + "offset": 9, + "rejectedSignatureCount": 0 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X74": { + "title": "incomplete generic constructors map positional and named arguments", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "call": "new Packet(", + "active": 0 + }, + { + "call": "new Packet(\"a\", ", + "active": 1 + }, + { + "call": "new Packet(second = \"b\", first =", + "active": 0 + } + ], + "replaceFrom": "new Packet(\"a\", \"b\")", + "replaceWith": "${0})", + "signature": "new Packet(String first, String second)" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X75": { + "title": "missing grouping and index delimiters retain member completion", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "(word.si", + "pair((word.si", + "word[word.si" + ], + "replaceFrom": "return 1 + word.size;", + "replaceWith": "return ${0};", + "anchor": "word.si", + "offset": 7, + "label": "size" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X76": { + "title": "missing nested call delimiters retain the innermost signature", + "manual": [], + "values": { + "file": "Advanced.x", + "expression": "pair((pair(1, ", + "replaceFrom": "return 1 + word.size;", + "replaceWith": "return ${0};", + "signature": "Int pair(Int first, Int second)", + "activeParameter": 1, + "tokensNonOverlapping": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X77": { + "title": "argument value completions fit methods functions and generic constructors", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "original": "take(1, text)", + "prefix": "take(1, " + }, + { + "original": "take(1, text)", + "prefix": "take(first = 1, second =" + }, + { + "original": "fn(1, text)", + "prefix": "fn(1, " + }, + { + "original": "new Packet(\"a\", text)", + "prefix": "new Packet(\"a\", " + }, + { + "original": "new Packet(\"a\", text)", + "prefix": "new Packet(second = \"b\", first =" + } + ], + "replaceWith": "${0})", + "labels": [ + "\"\"", + "qualifiedName", + "simpleName", + "text" + ], + "label": "text" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X78": { + "title": "argument completion preserves overload alternatives until insertion", + "manual": [], + "values": { + "file": "Advanced.x", + "selections": [ + "number", + "text" + ], + "replaceFrom": "choose(text);", + "anchor": "choose();", + "offset": 7, + "labels": [ + "\"\"", + "0", + "number", + "qualifiedName", + "simpleName", + "text" + ], + "signatureCount": 2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X79": { + "title": "typed argument prefixes fit values and replace exactly the identifier", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "original": "take(1, text)", + "prefix": "take(1, te" + }, + { + "original": "take(1, text)", + "prefix": "take(first = 1, second = te" + }, + { + "original": "fn(1, text)", + "prefix": "fn(1, te" + }, + { + "original": "new Packet(\"a\", text)", + "prefix": "new Packet(\"a\", te" + }, + { + "original": "new Packet(\"a\", text)", + "prefix": "new Packet(second = \"b\", first = te" + } + ], + "replaceFrom": "${0})", + "labels": [ + "text" + ], + "replacementStartDelta": -2, + "replaceWith": "${0}text)", + "prefixLength": 2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X80": { + "title": "typed argument prefixes preserve compatible overload alternatives", + "manual": [], + "values": { + "file": "Advanced.x", + "labels": [ + "valueNumber", + "valueText" + ], + "replaceFrom": "choose(valueText);", + "anchor": "choose(va);", + "offset": 9, + "signatureCount": 2, + "replacementStartDelta": -2, + "acceptedCall": "choose(${0});" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X81": { + "title": "implicit property and constant values fit positional and named arguments", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "takeValue(va", + "takeValue(value = va" + ], + "replaceFrom": "takeValue(valueText)", + "replaceWith": "${0})", + "labels": [ + "valueConstant", + "valueText" + ], + "label": "valueText", + "type": { + "source": "String", + "flags": "" + }, + "replacementStartDelta": -2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X82": { + "title": "static argument completion offers compatible constants without an instance receiver", + "manual": [], + "values": { + "file": "Advanced.x", + "replaceFrom": "takeValue(valueConstant)", + "anchor": "takeValue(va)", + "offset": 12, + "labels": [ + "valueConstant" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X83": { + "title": "specialized constructors preserve receiver type and exact argument edits", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "outer.new Part(\"a\", text)", + "packet.new(\"a\", text)", + "new @Marked Packet(\"a\", text)" + ], + "replaceFrom": "text)", + "prefix": "te", + "replaceWith": "${0})", + "signature": { + "source": "String first, String second", + "flags": "" + }, + "activeParameter": 1, + "labels": [ + "text" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X84": { + "title": "omitted constructor types distinguish required from provisional inference", + "manual": [], + "values": { + "file": "Advanced.x", + "constructors": [ + "Packet inferred = new Packet(\"a\", text)", + " new Packet(\"a\", text)" + ], + "replaceFrom": "text)", + "prefix": "te", + "replaceWith": "${0})", + "signature": { + "source": "String first, String second", + "flags": "" + }, + "requiredContext": "inferred", + "requiredLabels": [ + "text" + ], + "provisionalLabels": [ + "text", + "textNumber" + ], + "label": "text" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X85": { + "title": "array dimensions precede the active initializer argument", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "new String[2](te", + "new String[2](supply = te" + ], + "replaceFrom": "new String[2](text)", + "replaceWith": "${0})", + "activeParameter": 1, + "parameter": { + "source": "supply", + "flags": "" + }, + "labels": [ + "text" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X86": { + "title": "tuple and collection literals retain cursor completion with missing closers", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + "(1, word.si", + "Tuple:(1, word.si", + "[word.si", + "[\"key\" = word.si" + ], + "replaceFrom": "[word.size]", + "label": "size", + "replacementStartDelta": -2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X87": { + "title": "declaration recovery preserves initializer and default-value contexts", + "manual": [], + "values": { + "file": "Advanced.x", + "variants": [ + { + "prefix": "Int declaredSize(String word) = word.si", + "suffix": "", + "selected": "size", + "terminator": ";" + }, + { + "prefix": "Int declaredSize = \"x\".si", + "suffix": "", + "selected": "size", + "terminator": ";" + }, + { + "prefix": "void defaults(Int size = Int64.Ma", + "suffix": " {}", + "selected": "MaxValue", + "terminator": ")" + }, + { + "prefix": "class Holder(Int size = \"x\".si", + "suffix": " {}", + "selected": "size", + "terminator": ")" + } + ], + "replaceFrom": { + "source": "}\\s*$", + "flags": "" + }, + "replaceWith": "${0}${1}\n}\n", + "prefixLength": 2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X88": { + "title": "anonymous construction uses inherited signatures and preserves captured locals", + "manual": [], + "values": { + "file": "Advanced.x", + "original": "new Packet(\"anonymous\", text) {", + "prefix": "new Packet(\"anonymous\", te", + "replaceWith": "${0}) {", + "signature": "new Packet(String first, String second)", + "activeParameter": 1, + "labels": [ + "text" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X89": { + "title": "anonymous interface constructors survive missing call closers", + "manual": [], + "values": { + "file": "Advanced.x", + "original": "new CursorReader(\"a\", text)", + "prefix": "new CursorReader(\"a\", te", + "closers": [ + ")", + "" + ], + "signature": "new CursorReader(String first, String second)", + "activeParameter": 1, + "labels": [ + "text" + ], + "replacementStartDelta": -2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X90": { + "title": "array dimension completion fits size and survives a missing bracket", + "manual": [], + "values": { + "file": "Advanced.x", + "original": "new String[2](text)", + "prefix": "new String[te", + "variants": [ + { + "suffix": "](text)", + "repairedSuffix": "](text)", + "validAfterAcceptance": true + }, + { + "suffix": "", + "repairedSuffix": "](text)", + "validAfterAcceptance": false + } + ], + "signature": "new Array(Int size, String | function String(Int) supply)", + "activeParameter": 0, + "labels": [ + "textNumber" + ], + "replacementStartDelta": -2 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X91": { + "title": "declaration type prefixes resolve compiler types and replace only the written prefix", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { String StringValue = \"x\"; ${0} Int later = 1; }", + "variants": [ + "void damaged(Str| value) {}", + "void damaged(Int first, Str| second) {}", + "Str| property;", + "Str| damaged() = \"x\";" + ], + "include": [ + "String" + ], + "exclude": [ + "StringValue" + ], + "selected": "String", + "prefixLength": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X92": { + "title": "unfinished method headers retain outline and folds without leaking body declarations", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing {\n void damaged(${0}) {\n Int hidden = 1;\n }\n Int later = 1; }", + "parameters": [ + "Int", + "Int value, Str" + ], + "symbols": [ + "Editing", + "damaged", + "later" + ], + "excludedSymbol": "hidden", + "foldStart": 1, + "foldEnd": 3, + "repairedParameters": "Int value" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X93": { + "title": "qualified declaration types honor visibility and replace only the final name", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { import Owner as Alias; class Base { class ItemBase {} } class Owner extends Base { class ItemPublic {} private class ItemPrivate {} protected class ItemProtected {} static Int ItemValue = 1; typedef String as ItemAlias; } String StringValue = \"x\"; ${0} Int later = 1; }", + "variants": [ + { + "declaration": "void damaged(ecstasy.text.Str| value) {}", + "include": [ + "StringBuffer" + ], + "selected": "StringBuffer" + }, + { + "declaration": "ecstasy.text.Str| property;", + "include": [ + "StringBuffer" + ], + "selected": "StringBuffer" + }, + { + "declaration": "ecstasy.text.Str| damaged() = new StringBuffer();", + "include": [ + "StringBuffer" + ], + "selected": "StringBuffer" + }, + { + "declaration": "void damaged(Owner.Ite| value) {}", + "include": [ + "ItemBase", + "ItemPublic", + "ItemAlias" + ], + "selected": "ItemPublic" + }, + { + "declaration": "void damaged(Alias.Ite| value) {}", + "include": [ + "ItemBase", + "ItemPublic", + "ItemAlias" + ], + "selected": "ItemAlias" + } + ], + "exclude": [ + "ItemPrivate", + "ItemProtected", + "ItemValue", + "StringValue" + ], + "prefixLength": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X94": { + "title": "parameterized and compound header types preserve leaf edits and missing-delimiter diagnostics", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { String StringValue = \"x\"; ${0} Int later = 1; }", + "marker": "§", + "variants": [ + { + "declaration": "void damaged(Map> value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Map> value) {}" + }, + { + "declaration": "void damaged(Map value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Map value) {}" + }, + { + "declaration": "List property;", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "List property;" + }, + { + "declaration": "List damaged() = [\"x\"];", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "List damaged() = [\"x\"];" + }, + { + "declaration": "void damaged(List<(Int | Str§)> value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List<(Int | String)> value) {}" + }, + { + "declaration": "void damaged(Object + Str§ value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Object + String value) {}" + }, + { + "declaration": "void damaged(Object - Str§ value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Object - String value) {}" + }, + { + "declaration": "void damaged(List value) {}", + "selected": "StringBuffer", + "include": [ + "StringBuffer" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List value) {}" + }, + { + "declaration": "void damaged(Map> value) {}" + } + ], + "exclude": [ + "StringValue" + ], + "prefixLength": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X95": { + "title": "class and interface composition headers complete visible types without inventing inheritance", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { class Base {} interface Face {} mixin Feature {} class Owner { static class Nested {} } String StringValue = \"x\"; ${0} Int later = 1; }", + "marker": "§", + "variants": [ + { + "declaration": "class Damaged extends Bas§ { Int inside = 1; }", + "selected": "Base", + "include": [ + "Base" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged extends Base { Int inside = 1; }" + }, + { + "declaration": "interface Damaged extends Fac§ {}", + "selected": "Face", + "include": [ + "Face" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "interface Damaged extends Face {}" + }, + { + "declaration": "class Damaged implements Fac§ {}", + "selected": "Face", + "include": [ + "Face" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged implements Face {}" + }, + { + "declaration": "mixin Damaged into Bas§ {}", + "selected": "Base", + "include": [ + "Base" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "mixin Damaged into Base {}" + }, + { + "declaration": "class Damaged incorporates Fea§ {}", + "selected": "Feature", + "include": [ + "Feature" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged incorporates Feature {}" + }, + { + "declaration": "class Damaged(Face value) delegates Fac§(value) {}", + "selected": "Face", + "include": [ + "Face" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged(Face value) delegates Face(value) {}" + }, + { + "declaration": "class Damaged extends Owner.Nes§ {}", + "selected": "Nested", + "include": [ + "Nested" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged extends Owner.Nested {}" + }, + { + "declaration": "interface Damaged extends List {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "interface Damaged extends List {}" + }, + { + "declaration": "interface Damaged extends List {}" + } + ], + "exclude": [ + "StringValue" + ], + "prefixLength": 3 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X96": { + "title": "generic type completion resolves formals and parameterized qualifiers with whole-token edits", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { class Owner { class Item {} typedef Element as Alias; } class Container { ${0} } }", + "marker": "§", + "variants": [ + { + "declaration": "void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Element value) {}" + }, + { + "declaration": "void damaged(List<§> value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List value) {}" + }, + { + "declaration": "void damaged(Map<§, Int> value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Map value) {}" + }, + { + "declaration": "void damaged(Owner.Ite§ value) {}", + "selected": "Item", + "include": [ + "Item" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Owner.Item value) {}" + }, + { + "declaration": "void damaged(Owner.Ali§ value) {}", + "selected": "Alias", + "include": [ + "Alias" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Owner.Alias value) {}" + }, + { + "declaration": "void damaged(List value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 3, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List value) {}" + }, + { + "declaration": "void damaged(ecstasy.text.Str§ingBuffer value) {}", + "selected": "StringBuffer", + "include": [ + "StringBuffer" + ], + "prefixLength": 3, + "suffixLength": 9, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(ecstasy.text.StringBuffer value) {}" + }, + { + "declaration": "void damaged(Owner.Ite§m value) {}", + "selected": "Item", + "include": [ + "Item" + ], + "prefixLength": 3, + "suffixLength": 1, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Owner.Item value) {}" + } + ], + "prefixLength": 0, + "exclude": [ + "value", + "damaged" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X97": { + "title": "argument fitting through qualifiers and groups before later arguments", + "manual": [], + "values": { + "file": "Editing.x", + "marker": "§", + "source": "module Editing { class Pair(Int number, String text) {} class Box { Int number = 1; String numberText = \"x\"; private Int numberHidden = 2; } /** Accepts a number and text. */ void pair(Int number, String text) {} void flag(Boolean value) {} void nullable(String? value) {} void character(Char value) {} void decimal(Dec value) {} void binary(Byte[] value) {} void array(String[] value) {} void map(Map value) {} void tuple(Tuple value) {} void run(Int number, String numberText, Box box, function void(Int, String) fn) { ${0}; } }", + "variants": [ + { + "declaration": "pair(nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(number, \"x\")" + }, + { + "declaration": "pair((nu§), \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair((number), \"x\")" + }, + { + "declaration": "pair(((nu§)), \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(((number)), \"x\")" + }, + { + "declaration": "pair(§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(number, \"x\")" + }, + { + "declaration": "pair(number = nu§, text = \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(number = number, text = \"x\")" + }, + { + "declaration": "pair(box.nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(box.number, \"x\")" + }, + { + "declaration": "pair((box.nu§), \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair((box.number), \"x\")" + }, + { + "declaration": "fn(nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "fn(number, \"x\")" + }, + { + "declaration": "new Pair(nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "new Pair(number, \"x\")" + }, + { + "declaration": "pair(1 + nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(1 + number, \"x\")" + }, + { + "declaration": "pair(1 + §, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(1 + number, \"x\")" + }, + { + "declaration": "pair(-nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(-number, \"x\")" + }, + { + "declaration": "pair(2 * (1 + nu§), \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(2 * (1 + number), \"x\")" + }, + { + "declaration": "pair(1 + box.nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(1 + box.number, \"x\")" + }, + { + "declaration": "fn(1 + nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "fn(1 + number, \"x\")" + }, + { + "declaration": "new Pair(1 + nu§, \"x\")", + "selected": "number", + "include": [ + "number" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "new Pair(1 + number, \"x\")" + }, + { + "declaration": "pair(§, \"x\")", + "selected": "0", + "include": [ + "0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(0, \"x\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0, + "signatureDocumentationContains": "Accepts a number and text." + }, + { + "declaration": "pair(number = §, text = \"x\")", + "selected": "0", + "include": [ + "0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(number = 0, text = \"x\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0, + "signatureDocumentationContains": "Accepts a number and text." + }, + { + "declaration": "pair(1 + §, \"x\")", + "selected": "0", + "include": [ + "0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(1 + 0, \"x\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0, + "signatureDocumentationContains": "Accepts a number and text." + }, + { + "declaration": "fn(§, \"x\")", + "selected": "0", + "include": [ + "0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "fn(0, \"x\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "new Pair(§, \"x\")", + "selected": "0", + "include": [ + "0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "new Pair(0, \"x\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "pair(1, §)", + "selected": "\"\"", + "include": [ + "\"\"" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "pair(1, \"\")", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 1, + "signatureDocumentationContains": "Accepts a number and text.", + "exclude": [ + "numberHidden" + ] + }, + { + "declaration": "flag(Tr§)", + "selected": "True", + "include": [ + "True" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "flag(True)", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "nullable(Nu§)", + "selected": "Null", + "include": [ + "Null" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "nullable(Null)", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "character(§)", + "selected": "' '", + "include": [ + "' '" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "character(' ')", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "decimal(§)", + "selected": "0.0", + "include": [ + "0.0" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "decimal(0.0)", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "binary(§)", + "selected": "#00", + "include": [ + "#00" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "binary(#00)", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "array(§)", + "selected": "[]", + "include": [ + "[]" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "array([])", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "map(§)", + "selected": "Map:[]", + "include": [ + "Map:[]" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "map(Map:[])", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + }, + { + "declaration": "tuple(§)", + "selected": "Tuple:()", + "include": [ + "Tuple:()" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "tuple(Tuple:())", + "metadata": { + "kind": 12, + "detailContains": "Literal fitting", + "sorted": true + }, + "activeParameter": 0 + } + ], + "prefixLength": 2, + "exclude": [ + "numberText", + "numberHidden" + ], + "callContext": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X98": { + "title": "generic base completion preserves written type arguments", + "manual": [], + "values": { + "file": "Editing.x", + "marker": "§", + "source": "module Editing { ${0} }", + "variants": [ + { + "declaration": "void damaged(Li§st value) {}", + "selected": "List", + "include": [ + "List" + ], + "prefixLength": 2, + "suffixLength": 2, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List value) {}" + }, + { + "declaration": "void damaged(Li§ value) {}", + "selected": "List", + "include": [ + "List" + ], + "prefixLength": 2, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List value) {}" + }, + { + "declaration": "void damaged(Map> value) {}", + "selected": "List", + "include": [ + "List" + ], + "prefixLength": 2, + "suffixLength": 2, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Map> value) {}" + } + ], + "prefixLength": 2, + "exclude": [ + "value", + "damaged" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X99": { + "title": "unsaved discovered imports refresh dependent diagnostics", + "manual": [], + "values": { + "file": "LiveConsumer.x", + "original": "module LiveConsumer {}", + "changed": "module LiveConsumer { package lib import LiveLibrary; Int run() = lib.value(); }", + "library": "LiveLibrary.x", + "libraryText": "module LiveLibrary { static Int value() = 1; }", + "brokenLibrary": "module LiveLibrary { static String value() = \"text\"; }", + "anchor": "value()", + "target": "value" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X100": { + "title": "healthy graph navigation survives a broken neighbor", + "manual": [], + "values": { + "file": "HealthyGraph.x", + "source": "module HealthyGraph { class Base {} class Child extends Base {} }", + "neighbor": "BrokenNeighbor.x", + "neighborText": "module BrokenNeighbor {}", + "brokenNeighbor": "module BrokenNeighbor { Missing broken; }", + "anchor": "Base", + "child": "Child" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X101": { + "title": "bundled XDK definition and type targets open matching read-only source", + "manual": [ + "Verify the library editor refuses saving changes to the original read-only file." + ], + "values": { + "file": "LibrarySource.x", + "source": "module LibrarySource { package xml import xml.xtclang.org; void accept(xml.Document document, String text) {} }", + "types": [ + "Document", + "String" + ], + "variable": "text", + "variableType": "String", + "renameRejection": "Rename not allowed at this position" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X102": { + "title": "property rename includes source accessor families", + "manual": [ + "Inspect the preview before applying and undo after verification." + ], + "values": { + "file": "PropertyRename.x", + "source": "module PropertyRename { class Base { Int value { Int get() = 1; } } class Child extends Base { @Override Int value { Int get() = 2; } } Int run(Base base, Child child) = base.value + child.value; }", + "anchor": "value", + "replacement": "amount", + "edits": 4 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X103": { + "title": "member type rename moves its file and companion directory after source edits", + "manual": [ + "Inspect the preview before applying and undo after verification." + ], + "values": { + "file": "MoveType.x", + "source": "module MoveType { Item make() = new Item(); void accept(Item.Nested value) {} }", + "member": "MoveType/Item.x", + "memberSource": "class Item {}", + "destination": "MoveType/Renamed.x", + "companion": "MoveType/Item/Nested.x", + "companionSource": "class Nested {}", + "companionDestination": "MoveType/Renamed/Nested.x", + "anchor": "Item", + "replacement": "Renamed" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X104": { + "title": "explicit alias rename preserves the imported declaration", + "manual": [ + "Inspect the preview before applying and undo after verification." + ], + "values": { + "file": "AliasRename.x", + "source": "module AliasRename { import ecstasy.text.StringBuffer as Buffer; Buffer make() = new Buffer(); }", + "anchor": "Buffer;", + "replacement": "Builder", + "edits": 3, + "preserved": "ecstasy.text.StringBuffer" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X105": { + "title": "auto import repairs a bundled or discovered source type", + "manual": [ + "Inspect the preview before applying and undo after verification." + ], + "values": { + "file": "AutoImports.x", + "completions": [ + { + "source": "module AutoImports { Doc§ument value; }", + "label": "Document", + "importText": "import xml.Document;", + "expected": "module AutoImports {\npackage xml import xml.xtclang.org;\nimport xml.Document;\n Document value; }" + }, + { + "source": "module AutoImports { Wid§get value; }", + "label": "Widget", + "importText": "import importLibrary.Widget;", + "expected": "module AutoImports {\npackage importLibrary import ImportLibrary;\nimport importLibrary.Widget;\n Widget value; }" + } + ], + "variants": [ + { + "source": "module AutoImports { Document echo(Document doc) = doc; }", + "anchor": "Document", + "title": "Import 'Document' from xml.xtclang.org", + "importText": "import xml.Document;" + }, + { + "source": "module AutoImports { Widget make() = new Widget(); }", + "anchor": "Widget", + "title": "Import 'Widget' from ImportLibrary", + "importText": "import importLibrary.Widget;" + } + ], + "library": "ImportLibrary.x", + "libraryText": "module ImportLibrary { class Widget {} }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X106": { + "title": "function and type-sequence headers complete their written leaf types", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { ${0} }", + "marker": "§", + "prefixLength": 3, + "exclude": [ + "value", + "damaged" + ], + "variants": [ + { + "declaration": "void damaged(function Str§(Int) value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(function String(Int) value) {}" + }, + { + "declaration": "void damaged(function void(Str§) value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(function void(String) value) {}" + }, + { + "declaration": "void damaged(function (Int, Str§)(Int) value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(function (Int, String)(Int) value) {}" + }, + { + "declaration": "void damaged(function void(List) value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(function void(List) value) {}" + }, + { + "declaration": "void damaged(Function<, > value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Function<, > value) {}" + }, + { + "declaration": "void damaged(Function<, > value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Function<, > value) {}" + }, + { + "declaration": "void damaged(function void(Str§ value) {}", + "selected": "String", + "include": [ + "String" + ], + "validAfterAcceptance": false, + "repairedDeclaration": "void damaged(function void(String) value) {}" + }, + { + "declaration": "void damaged(Function<, > value) {}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X107": { + "title": "empty type operands and qualifier edits preserve exact ranges", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { ${0} }", + "marker": "§", + "prefixLength": 0, + "exclude": [ + "value", + "damaged" + ], + "variants": [ + { + "declaration": "void damaged(ecstasy.text.§ value) {}", + "selected": "StringBuffer", + "include": [ + "StringBuffer" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(ecstasy.text.StringBuffer value) {}" + }, + { + "declaration": "void damaged(ecstasy.te§xt.StringBuffer value) {}", + "selected": "text", + "include": [ + "text" + ], + "prefixLength": 2, + "suffixLength": 2, + "initiallyValid": true, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(ecstasy.text.StringBuffer value) {}" + }, + { + "declaration": "void damaged((Int | §) value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged((Int | String) value) {}" + }, + { + "declaration": "void damaged(List<(Int | §)> value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(List<(Int | String)> value) {}" + }, + { + "declaration": "void damaged(Object + § value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Object + String value) {}" + }, + { + "declaration": "void damaged(Object - § value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged(Object - String value) {}" + }, + { + "declaration": "void damaged((§ | Int) value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 0, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "void damaged((String | Int) value) {}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X108": { + "title": "multiple-return generic and package headers complete in the enclosing scope", + "manual": [], + "values": { + "file": "Editing.x", + "source": "module Editing { ${0} }", + "marker": "§", + "prefixLength": 3, + "exclude": [ + "value", + "damaged" + ], + "variants": [ + { + "declaration": "(Int, Str§) damaged() { return 1, \"x\"; }", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "(Int, String) damaged() { return 1, \"x\"; }" + }, + { + "declaration": "(Int count, Str§ text) damaged() { return 1, \"x\"; }", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "(Int count, String text) damaged() { return 1, \"x\"; }" + }, + { + "declaration": " void damaged(Str§ value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " void damaged(String value) {}" + }, + { + "declaration": " Str§ damaged(T value) = \"x\";", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " String damaged(T value) = \"x\";" + }, + { + "declaration": " void damaged(T value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " void damaged(T value) {}" + }, + { + "declaration": "class Damaged {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged {}" + }, + { + "declaration": "package damaged incorporates SharedMi§ {} mixin SharedMixin into Package {}", + "selected": "SharedMixin", + "include": [ + "SharedMixin" + ], + "prefixLength": 8, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "package damaged incorporates SharedMixin {} mixin SharedMixin into Package {}" + }, + { + "declaration": " void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " void damaged(Element value) {}" + }, + { + "declaration": "class Damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "class Damaged(Element value) {}" + }, + { + "declaration": " void damaged(Oth§ value) {}", + "selected": "Other", + "include": [ + "Other" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " void damaged(Other value) {}" + }, + { + "declaration": " void damaged(Str§ value) {}", + "selected": "String", + "include": [ + "String" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": " void damaged(String value) {}" + }, + { + "declaration": "interface Chain {} > void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} > void damaged(Element value) {}", + "metadata": { + "detailContains": "(written constraint)", + "sorted": true + } + }, + { + "declaration": "interface Chain {} class Damaged>(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} class Damaged>(Element value) {}", + "metadata": { + "detailContains": "(written constraint)", + "sorted": true + } + }, + { + "declaration": "interface Chain {} + Stringable> void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} + Stringable> void damaged(Element value) {}", + "metadata": { + "detailContains": "(written constraint)", + "sorted": true + } + }, + { + "declaration": "interface Chain {} > void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} > void damaged(Element value) {}", + "metadata": { + "detailContains": "(written constraint)", + "sorted": true + } + }, + { + "declaration": "interface Chain {} ?> void damaged(Ele§ value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} ?> void damaged(Element value) {}", + "metadata": { + "detailContains": "(written constraint)", + "sorted": true + } + }, + { + "declaration": "interface Chain {} > void damaged(Element value) {}", + "selected": "Element", + "include": [ + "Element" + ], + "prefixLength": 3, + "suffixLength": 0, + "initiallyValid": false, + "validAfterAcceptance": true, + "repairedDeclaration": "interface Chain {} > void damaged(Element value) {}", + "metadata": { + "detailContains": "(incomplete)", + "sorted": true + } + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X109": { + "title": "public parameter rename updates a closed caller and preserves another overload", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "input", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { class Box { Int pick(Int input, Int count = 1) = input + count; String pick(String input) = input; } }", + "destination": "Library.x", + "expected": "module Library { class Box { Int pick(Int value, Int count = 1) = value + count; String pick(String input) = input; } }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(count = 2, input = 1); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; Int run(lib.Box box) = box.pick(count = 2, value = 1); }" + }, + { + "file": "Other.x", + "source": "module Other { package lib import Library; String run(lib.Box box) = box.pick(input = \"text\"); }", + "destination": "Other.x", + "expected": "module Other { package lib import Library; String run(lib.Box box) = box.pick(input = \"text\"); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X110": { + "title": "override parameter slots join differently named declarations and callers", + "manual": [], + "values": { + "file": "Consumer.x", + "anchor": "argument", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { interface Mapper { T map(T input); } }", + "destination": "Library.x", + "expected": "module Library { interface Mapper { T map(T value); } }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; class Mapper implements lib.Mapper { @Override String map(String argument) = argument; } String run(lib.Mapper api, Mapper impl) = api.map(input = \"a\") + impl.map(argument = \"b\"); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; class Mapper implements lib.Mapper { @Override String map(String value) = value; } String run(lib.Mapper api, Mapper impl) = api.map(value = \"a\") + impl.map(value = \"b\"); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X111": { + "title": "constructor parameter rename starts at a named argument", + "manual": [], + "values": { + "file": "Consumer.x", + "anchor": "input", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { class Box { construct(Int input, Int count = 1) { total = input + count; } Int total; } }", + "destination": "Library.x", + "expected": "module Library { class Box { construct(Int value, Int count = 1) { total = value + count; } Int total; } }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, input = 1); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, value = 1); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X112": { + "title": "delegation rename joins the contract and concrete receiver", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library { interface Api { Int read(); } }", + "destination": "Library.x", + "expected": "module Library { interface Api { Int fetch(); } }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; class Actual implements lib.Api { @Override Int read() = 1; } class Forward(Actual target) delegates lib.Api(target) {} Int use(Forward value) = value.read(); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; class Actual implements lib.Api { @Override Int fetch() = 1; } class Forward(Actual target) delegates lib.Api(target) {} Int use(Forward value) = value.fetch(); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X113": { + "title": "mixin method rename preserves contextual super", + "manual": [], + "values": { + "file": "App.x", + "anchor": "read", + "replacement": "changed", + "files": [ + { + "file": "App.x", + "source": "module App { class Base { Int read() = 1; } mixin Loud into Base { @Override Int read() = super() + 1; } class Host extends Base incorporates Loud {} Int use(Host value) = value.read(); }", + "destination": "App.x", + "expected": "module App { class Base { Int changed() = 1; } mixin Loud into Base { @Override Int changed() = super() + 1; } class Host extends Base incorporates Loud {} Int use(Host value) = value.changed(); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X114": { + "title": "mixin property rename updates declarations and access", + "manual": [], + "values": { + "file": "App.x", + "anchor": "read", + "replacement": "changed", + "files": [ + { + "file": "App.x", + "source": "module App { class Base { Int read = 1; } mixin Loud into Base { @Override Int read.get() = 2; } class Host extends Base incorporates Loud {} Int use(Host value) = value.read; }", + "destination": "App.x", + "expected": "module App { class Base { Int changed = 1; } mixin Loud into Base { @Override Int changed.get() = 2; } class Host extends Base incorporates Loud {} Int use(Host value) = value.changed; }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X115": { + "title": "annotated property rename preserves its Lazy implementation", + "manual": [], + "values": { + "file": "App.x", + "anchor": "read", + "replacement": "changed", + "files": [ + { + "file": "App.x", + "source": "module App { class Holder { @Lazy Int read.calc() = 1; } Int use(Holder value) = value.read; }", + "destination": "App.x", + "expected": "module App { class Holder { @Lazy Int changed.calc() = 1; } Int use(Holder value) = value.changed; }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X116": { + "title": "qualified module rename preserves its domain and alias and moves its companion", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "Library", + "replacement": "Renamed", + "files": [ + { + "file": "Library.x", + "source": "module Library.example.org {}", + "destination": "Renamed.x", + "expected": "module Renamed.example.org {}" + }, + { + "file": "Library/Box.x", + "source": "class Box {}", + "destination": "Renamed/Box.x", + "expected": "class Box {}" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library.example.org; lib.Box make() = new lib.Box(); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Renamed.example.org; lib.Box make() = new lib.Box(); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X117": { + "title": "implicit package references retain real files and rename moves the nested directory", + "manual": [], + "values": { + "file": "App.x", + "anchor": "tools", + "replacement": "helpers", + "navigationAnchor": "Box", + "navigationFiles": [ + "App.x", + "App/tools/deep/Box.x" + ], + "files": [ + { + "file": "App.x", + "source": "module App { void accept(tools.deep.Box value) {} }", + "destination": "App.x", + "expected": "module App { void accept(helpers.deep.Box value) {} }" + }, + { + "file": "App/tools/deep/Box.x", + "source": "class Box {}", + "destination": "App/helpers/deep/Box.x", + "expected": "class Box {}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X118": { + "title": "explicit module rename persists graph roots and edges with Undo and Redo", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "Library", + "replacement": "Renamed", + "files": [ + { + "file": "Library.x", + "source": "module Library.example.org {}", + "destination": "Renamed.x", + "expected": "module Renamed.example.org {}" + }, + { + "file": "Library/Box.x", + "source": "class Box {}", + "destination": "Renamed/Box.x", + "expected": "class Box {}" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library.example.org; lib.Box make() = new lib.Box(); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Renamed.example.org; lib.Box make() = new lib.Box(); }" + } + ], + "sourceModules": [ + { + "name": "Library.example.org", + "uri": "Library.x", + "dependencies": [], + "resourceRoots": [ + "assets", + "fallback" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Library.example.org" + ] + } + ], + "projectSettingsRoundTrip": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X119": { + "title": "primary constructor property rename joins its header, label and closed consumer access", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "input", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { class Box(Int input, Int count = 1) {} class Other(Int input) {} }", + "destination": "Library.x", + "expected": "module Library { class Box(Int value, Int count = 1) {} class Other(Int input) {} }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, input = 1); Int read(lib.Box box) = box.input; lib.Other other() = new lib.Other(input = 3); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; lib.Box make() = new lib.Box(count = 2, value = 1); Int read(lib.Box box) = box.value; lib.Other other() = new lib.Other(input = 3); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X120": { + "title": "lambda parameter rename follows nested captures and preserves sibling bindings", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "input", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { Int run() { function Int(Int) f = (Int input) -> { function Int() nested = () -> input; return nested(); }; function Int(Int) g = (Int input) -> input; return f(1) + g(2); } }", + "destination": "Library.x", + "expected": "module Library { Int run() { function Int(Int) f = (Int value) -> { function Int() nested = () -> value; return nested(); }; function Int(Int) g = (Int input) -> input; return f(1) + g(2); } }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X121": { + "title": "escaped method parameter rename preserves positional calls and unrelated members", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "input", + "replacement": "value", + "files": [ + { + "file": "Library.x", + "source": "module Library { class Box { Int pick(Int input) = input; } class Other { String pick(String input) = input; } }", + "destination": "Library.x", + "expected": "module Library { class Box { Int pick(Int value) = value; } class Other { String pick(String input) = input; } }" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package lib import Library; Int run(lib.Box box) { function Int(Int) f = box.pick; return f(1) + box.pick(input = 2); } String text(lib.Other box) = box.pick(input = \"text\"); }", + "destination": "Consumer.x", + "expected": "module Consumer { package lib import Library; Int run(lib.Box box) { function Int(Int) f = box.pick; return f(1) + box.pick(value = 2); } String text(lib.Other box) = box.pick(input = \"text\"); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X122": { + "title": "implement and override source and bundled methods with native Undo and Redo", + "manual": [], + "values": { + "file": "Actions.x", + "anchor": "Box", + "variants": [ + { + "source": "module Actions { interface Api { String read(String value); } class Box implements Api {} }", + "title": "Implement String read(String value)", + "expected": "module Actions { interface Api { String read(String value); } class Box implements Api {\n @Override\n String read(String value) {\n TODO();\n }\n} }" + }, + { + "source": "module Actions { class Base { String read(String value) = value; } class Box extends Base {} }", + "title": "Override String read(String value)", + "expected": "module Actions { class Base { String read(String value) = value; } class Box extends Base {\n @Override\n String read(String value) {\n TODO();\n }\n} }" + }, + { + "source": "module Actions { interface Api { String read(String value); } class Box implements Api {} Box make() = new Box(); }", + "title": "Implement String read(String value)", + "expected": "module Actions { interface Api { String read(String value); } class Box implements Api {\n @Override\n String read(String value) {\n TODO();\n }\n} Box make() = new Box(); }", + "initiallyValid": false + }, + { + "source": "module Actions { interface Api { conditional (String, List) read(Int value = 1); } class Box implements Api {} }", + "title": "Implement conditional (String, List) read(Int64 value = 1)", + "expected": "module Actions { interface Api { conditional (String, List) read(Int value = 1); } class Box implements Api {\n @Override\n conditional (String, List) read(Int64 value = 1) {\n TODO();\n }\n} }", + "initiallyValid": true + }, + { + "source": "module Actions { interface Api { T read(T value); } class Box implements Api {} }", + "title": "Implement T read(T value)", + "expected": "module Actions { interface Api { T read(T value); } class Box implements Api {\n @Override\n T read(T value) {\n TODO();\n }\n} }", + "initiallyValid": true + }, + { + "source": "module Actions { class Base { String read(String value) = value; } class Box extends Base {} class Child extends Box {} String use(Child child) = child.read(value = \"text\"); }", + "title": "Override String read(String value)", + "expected": "module Actions { class Base { String read(String value) = value; } class Box extends Base {\n @Override\n String read(String value) {\n TODO();\n }\n} class Child extends Box {} String use(Child child) = child.read(value = \"text\"); }", + "initiallyValid": true + }, + { + "source": "module Actions { interface Api { String read(String value); void reset(); } class Box implements Api {} Box make() = new Box(); }", + "title": "Implement all required members (2)", + "expected": "module Actions { interface Api { String read(String value); void reset(); } class Box implements Api {\n @Override\n String read(String value) {\n TODO();\n }\n\n @Override\n void reset() {\n TODO();\n }\n} Box make() = new Box(); }", + "initiallyValid": false + }, + { + "source": "module Actions { class Box implements Iterator {} Box make() = new Box(); }", + "title": "Implement conditional String next()", + "expected": "module Actions { class Box implements Iterator {\n @Override\n conditional String next() {\n TODO();\n }\n} Box make() = new Box(); }", + "initiallyValid": false + }, + { + "source": "module Actions { interface Api { (Int | String) read(); } class Box implements Api {} }", + "title": "Implement (Int64 | String) read()", + "expected": "module Actions { interface Api { (Int | String) read(); } class Box implements Api {\n @Override\n (Int64 | String) read() {\n TODO();\n }\n} }", + "initiallyValid": true + }, + { + "source": "module Actions { interface Api { Int read(Int value = 1); } class Box implements Api {} Box make() = new Box(); }", + "title": "Implement Int64 read(Int64 value = 1)", + "expected": "module Actions { interface Api { Int read(Int value = 1); } class Box implements Api {\n @Override\n Int64 read(Int64 value = 1) {\n TODO();\n }\n} Box make() = new Box(); }", + "initiallyValid": false + }, + { + "source": "module Actions { interface Api { String read(String value); } class Box implements Api { @Override String read(String value) = value; } }", + "title": "", + "expected": "module Actions { interface Api { String read(String value); } class Box implements Api { @Override String read(String value) = value; } }" + } + ], + "refusalMember": "read" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X123": { + "title": "pull diagnostics report full, unchanged and repaired results through the installed client", + "manual": [], + "values": { + "file": "Navigation.x", + "broken": "module Navigation { Int run() = missing; }", + "repaired": "module Navigation { Int run() = 1; }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X124": { + "title": "unopened missing external roots repair through editor watchers and settings replacement", + "manual": [], + "values": { + "file": "Assets.x", + "text": "module Assets { static String text() = $./data.txt; }", + "resource": "data.txt", + "resourceDirectory": "generated/assets", + "contents": "resource repaired", + "module": "Assets" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X125": { + "title": "platform generic hover, written-call signature, chained completion and narrowed bundled type", + "manual": [], + "values": { + "file": "Demo.x", + "hoverSource": "module Demo { T echo(T value) = value; String run() = echo(\"value\"); }", + "hoverAnchor": "echo(\"value\")", + "hoverExpected": "String echo(String value)", + "signatures": [ + { + "text": "module Demo { T echo(T value, T backup) = value; String run() = echo(\"a\", §\"b\"); }", + "active": 1 + }, + { + "text": "module Demo { T echo(T value, T backup) = value; String run() = echo(backup = \"b\", §value = \"a\"); }", + "active": 0 + } + ], + "completions": [ + "module Demo { String run(String text) = text.tr§(); }", + "module Demo { String run(String text) = text.trim().tr§(); }" + ], + "completion": "trim", + "narrowedSource": "module Demo { package json import json.xtclang.org; import json.Doc; import json.JsonObject; String run(Doc value) { assert value.is(JsonObject); return value.toString(); } }", + "narrowedAnchor": "value.toString", + "targetSuffix": "/Map.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X126": { + "title": "semantic token result IDs, range and delta negotiation survive edit and close", + "manual": [], + "values": { + "file": "Tokens.x", + "source": "module Tokens { Int value = 1; Int run() = value; }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X127": { + "title": "lazy action edits resolve through installed transport and reject obsolete handles", + "manual": [], + "values": { + "file": "Resolve.x", + "actionTitle": "Remove unused import 'StringBuffer'", + "source": "module Resolve { import ecstasy.text.StringBuffer; }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X128": { + "title": "native file and package-folder rename apply compiler-proven reference edits", + "manual": [], + "values": { + "variants": [ + { + "root": "file/App.x", + "source": "module App { Box make() = new Box(); }", + "member": "file/App/Box.x", + "memberSource": "class Box {}", + "from": "file/App/Box.x", + "to": "file/App/Crate.x", + "newName": "Crate.x", + "expected": "module App { Crate make() = new Crate(); }", + "target": "file/App/Crate.x", + "targetSource": "class Crate {}" + }, + { + "root": "folder/App.x", + "source": "module App { tools.Box make() = new tools.Box(); }", + "member": "folder/App/tools/Box.x", + "memberSource": "class Box {}", + "from": "folder/App/tools", + "to": "folder/App/util", + "newName": "util", + "expected": "module App { util.Box make() = new util.Box(); }", + "target": "folder/App/util/Box.x", + "targetSource": "class Box {}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X129": { + "title": "evaluated Gradle model refresh and explicit override ownership", + "manual": [ + "Running the real Gradle wrapper from the compiler paths UI; exporter tasks have TestKit coverage" + ], + "values": { + "file": "GradleAssets.x", + "source": "module GradleAssets { static String text() = $./data.txt; }", + "resource": "processed/data.txt", + "contents": "prepared resource", + "model": { + "schemaVersion": 1, + "sourceSets": [ + { + "projectId": "${workspace}#:app", + "projectPath": ":app", + "projectDirectory": "${workspace}/", + "buildFile": "${workspace}/build.gradle.kts", + "sourceSet": "main", + "sourceRoots": [ + "${workspace}/" + ], + "sourceFiles": [ + "${workspace}/GradleAssets.x" + ], + "moduleRoots": [ + "${workspace}/GradleAssets.x" + ], + "resourceSourceRoots": [ + "${workspace}/raw/" + ], + "resourceRoots": [ + "${workspace}/processed/" + ], + "resourceTask": ":app:processXtcResources", + "projectDependencies": [], + "modulePath": [] + } + ] + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X130": { + "title": "native batch container Move preserves resources and supports one Undo and Redo", + "manual": [], + "values": { + "sources": [ + "old", + "second" + ], + "destination": "destination", + "consumer": "Consumer.x", + "files": [ + { + "file": "old/App.x", + "text": "module App { Box make() = new Box(); static String text() = $./data.txt; }" + }, + { + "file": "old/App/Box.x", + "text": "class Box {}" + }, + { + "file": "old/data.txt", + "text": "moved resource" + }, + { + "file": "second/Library.x", + "text": "module Library { Int number() = 1; }" + }, + { + "file": "Consumer.x", + "text": "module Consumer { package app import App; package lib import Library; app.Box make() = new app.Box(); Int number() = lib.number(); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X131": { + "title": "negotiated code lens document link inlay hint and workspace symbol resolution", + "manual": [], + "values": { + "file": "Resolve.x", + "module": "Resolve", + "source": "module Resolve {\n /** Read a number. */\n Int read(Int value) = value;\n void run() { var result = read(1); }\n // https://xtclang.org/\n}\n", + "link": "https://xtclang.org/" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X132": { + "title": "multiple-range formatting and negotiated save hooks", + "manual": [], + "values": { + "file": "SaveRanges.x", + "module": "SaveRanges", + "source": "module SaveRanges {\nInt value = 1;\nString text = \"😀\";\n}\n", + "ranges": [ + { + "start": { + "line": 2, + "character": 0 + }, + "end": { + "line": 3, + "character": 0 + } + }, + { + "start": { + "line": 1, + "character": 0 + }, + "end": { + "line": 2, + "character": 0 + } + }, + { + "start": { + "line": 1, + "character": 0 + }, + "end": { + "line": 3, + "character": 0 + } + } + ], + "lines": [ + 1, + 2 + ], + "indent": " " + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X133": { + "title": "linked editing follows local identity and refuses nonlocal or broken symbols", + "manual": [ + "Native linked-edit typing and highlight appearance depend on the editor; this case verifies the installed LSP response." + ], + "values": { + "file": "Linked.x", + "module": "Linked", + "source": "module Linked { Int one() { Int value = 1; return value; } Int two() { Int value = 2; return value; } Int identity(Int input) = input; }", + "anchor": "value", + "uses": [ + "value = 1", + "value; } Int two" + ], + "refused": [ + "Int one", + "input)" + ], + "broken": "module Linked { Missing broken; Int one() { Int value = 1; return value; } }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X134": { + "title": "unopened external source changes refresh local consumers without manual VFS refresh", + "manual": [ + "Watch retirement, shared-root ownership and repeated project close/reopen are separate lifecycle acceptance checks." + ], + "values": { + "file": "ExternalConsumer.x", + "module": "ExternalConsumer", + "consumer": "module ExternalConsumer { package lib import ExternalLibrary; Int answer() = lib.answer(); }", + "libraryFile": "ExternalLibrary.x", + "libraryModule": "ExternalLibrary", + "library": "module ExternalLibrary { static Int answer() = 1; }", + "brokenLibrary": "module ExternalLibrary { static String answer() = \"text\"; }" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X135": { + "title": "server log view opens and hides without changing source", + "manual": [ + "Physical shortcut dispatch, remapped keymaps and alternate dock locations" + ], + "values": { + "file": "LogPanel.x", + "source": "module LogPanel {}\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X136": { + "title": "language-service preferences preserve graph ownership and expose effective state", + "manual": [ + "Physical Settings dialog navigation, keyboard accessibility and defaults across a fresh IDE session." + ], + "values": { + "file": "ServicePreferences.x", + "source": "module ServicePreferences { Int count = 1; }\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X137": { + "title": "transport preference restarts one service and preserves unsaved buffers", + "manual": [ + "Connection options are window/project scoped; remote filesystem execution is not enabled." + ], + "values": { + "file": "ServiceTransport.x", + "source": "module ServiceTransport { Int value = 1; }\n", + "edited": "module ServiceTransport { Int value = 2; } // unsaved\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X138": { + "title": "live formatting settings retain valid values and expose save ownership", + "manual": [ + "IntelliJ server save edits are unavailable in LSP4IJ; use native Actions on Save.", + "Per-folder VS Code native format-on-save overrides and physical Code Style editing." + ], + "values": { + "file": "ServiceFormatting.x", + "source": "module ServiceFormatting {\nInt value = 1;\n}\n", + "indent": 2, + "expectedIndent": " " + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X139": { + "title": "native save formatting and server save edits have a single owner", + "manual": [ + "LSP4IJ has no server save hook; its case verifies native Actions on Save instead." + ], + "values": { + "file": "ServiceSave.x", + "source": "module ServiceSave {\nInt value = 1;\n}\n", + "formatted": "module ServiceSave {\n Int value = 1;\n}\n", + "closedFile": "ClosedServiceSave.x", + "closedSource": "module ClosedServiceSave {\nInt value = 1;\n}\n", + "closedFormatted": "module ClosedServiceSave {\n Int value = 1;\n}\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X140": { + "title": "UTF-16 negotiation preserves navigation after an astral character", + "manual": [], + "values": { + "file": "ProtocolEncoding.x", + "source": "module ProtocolEncoding { Int run() { String marker = \"😀\"; Int value = 1; return value; } }\n", + "anchor": "value", + "expected": "Int" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X141": { + "title": "runtime client trace changes between messages and verbose without source payloads", + "manual": [ + "Inspect long-operation progress and cancellation using a large configured project." + ], + "values": { + "file": "ProtocolTrace.x", + "source": "module ProtocolTrace { Int value = 1; }\n", + "anchor": "value", + "levels": [ + "messages", + "verbose" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X142": { + "title": "constant property initializer retains semantic navigation and rename", + "manual": [], + "values": { + "file": "X142/FoldedInitializer.x", + "module": "FoldedInitializer", + "source": "module FoldedInitializer { Int value = 1; Int copy = value; Int run() = value; }\n", + "anchor": "value; Int run", + "name": "value", + "newName": "number", + "references": 3, + "expected": "Int" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X143": { + "title": "partial workspace symbols have bounded batches and no duplicate final items", + "manual": [], + "values": { + "file": "X143/PartialSymbols.x", + "module": "PartialSymbols", + "declarations": 130, + "prefix": "PartialItem", + "token": "xtc-playbook-partial-symbols", + "batchSize": 64 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X144": { + "title": "installed client rejects stale versioned edits and preserves native Undo", + "manual": [], + "values": { + "file": "GuardedEdit.x", + "module": "GuardedEdit", + "source": "module GuardedEdit { Int value = 1; }\n", + "changed": "module GuardedEdit { Int value = 2; }\n", + "otherFile": "OtherEdit.x", + "otherModule": "OtherEdit", + "otherSource": "module OtherEdit { Int value = 1; }\n", + "otherChanged": "module OtherEdit { Int value = 3; }\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X145": { + "title": "visible compiler progress, native cancel and disconnect with pending work", + "manual": [], + "values": { + "file": "ProgressWork.x", + "module": "ProgressWork", + "source": "module ProgressWork { static Int value = 1; }\n", + "methods": 5000, + "uiMethods": 20000 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X146": { + "title": "dependency edits refresh untouched consumer presentation", + "manual": [], + "values": { + "library": "RefreshLibrary.x", + "consumer": "RefreshConsumer.x", + "libraryModule": "RefreshLibrary", + "consumerModule": "RefreshConsumer", + "original": "module RefreshLibrary { static Int make() = 1; }\n", + "changed": "module RefreshLibrary { static String make() = \"one\"; }\n", + "source": "module RefreshConsumer {\n package lib import RefreshLibrary;\n void run() { var value = lib.make(); }\n}\n", + "before": "Int", + "after": "String" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X147": { + "title": "late settings and retired connection reports cannot replace current state", + "manual": [], + "values": { + "file": "ReportOwnership.x", + "module": "ReportOwnership", + "source": "module ReportOwnership { Int value = 1; }\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X148": { + "title": "extract a literal return value with native undo and redo", + "manual": [], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read() {\n return 42;\n }\n}", + "selected": "42", + "title": "Extract literal to local variable", + "expected": "module Extract {\n Int read() {\n val extractedValue = 42;\n return extractedValue;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X149": { + "title": "declaration name completion replaces whole tokens and avoids occupied names", + "manual": [], + "values": { + "file": "Naming.x", + "variants": [ + { + "source": "module Naming {\n String strange = \"\";\n}", + "anchor": "strange", + "prefix": "str", + "label": "string", + "expected": "module Naming {\n String string = \"\";\n}" + }, + { + "source": "module Naming {\n String string = \"\";\n String stuff = \"\";\n}", + "anchor": "stuff", + "prefix": "st", + "label": "string1", + "expected": "module Naming {\n String string = \"\";\n String string1 = \"\";\n}" + }, + { + "source": "module Naming {\n void run(String strange) {}\n}", + "anchor": "strange", + "prefix": "str", + "label": "string", + "expected": "module Naming {\n void run(String string) {}\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X150": { + "title": "contextual compiler snippets insert templates and navigate placeholders", + "manual": [ + "Inspect completion popup layout and compare file, member and statement keyword lists." + ], + "values": { + "file": "Templates.x", + "variants": [ + { + "source": "module Templates {\n cl\n}", + "anchor": "cl", + "label": "class declaration", + "selected": "Example", + "expected": "module Templates {\n class Example {\n \n }\n}" + }, + { + "source": "module Templates {\n void run() {\n if\n }\n}", + "anchor": "if", + "label": "if block", + "selected": "True", + "expected": "module Templates {\n void run() {\n if (True) {\n \n }\n }\n}" + }, + { + "source": "module Templates {\n interface\n}", + "anchor": "interface", + "label": "interface declaration", + "selected": "Example", + "expected": "module Templates {\n interface Example {\n \n }\n}" + }, + { + "source": "module Templates {\n void run() {\n for\n }\n}", + "anchor": "for", + "label": "for range", + "selected": "0..<1", + "expected": "module Templates {\n void run() {\n for (Int i : 0..<1) {\n \n }\n }\n}" + }, + { + "source": "module Templates {\n void run() {\n do\n }\n}", + "anchor": "do", + "label": "do loop", + "selected": "False", + "expected": "module Templates {\n void run() {\n do {\n \n } while (False);\n }\n}" + }, + { + "source": "module Templates {\n void run() {\n try\n }\n}", + "anchor": "try", + "label": "try catch", + "selected": "Exception", + "expected": "module Templates {\n void run() {\n try {\n \n } catch (Exception e) {\n }\n }\n}" + }, + { + "source": "module Templates {\n void take(function Int(Int) action) {}\n void run() { take(); }\n}", + "anchor": "take();", + "prefix": "take(", + "label": "(arg1) -> TODO()", + "expected": "module Templates {\n void take(function Int(Int) action) {}\n void run() { take((arg1) -> TODO()); }\n}", + "selected": "TODO()", + "finalAnchor": "(arg1) -> TODO()" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X151": { + "title": "missing property and parameter names insert without replacing written types", + "manual": [], + "values": { + "file": "MissingNames.x", + "variants": [ + { + "source": "module MissingNames {\n String string = \"\";\n String = \"\";\n}", + "anchor": "module MissingNames {\n String string = \"\";\n String ", + "label": "string1", + "expected": "module MissingNames {\n String string = \"\";\n String string1 = \"\";\n}" + }, + { + "source": "module MissingNames {\n void run(String ) {}\n}", + "anchor": "module MissingNames {\n void run(String ", + "label": "string", + "expected": "module MissingNames {\n void run(String string) {}\n}" + }, + { + "source": "module MissingNames {\n class Item(List ) {}\n}", + "anchor": "module MissingNames {\n class Item(List ", + "label": "list", + "expected": "module MissingNames {\n class Item(List list) {}\n}" + }, + { + "source": "module MissingNames {\n String? = Null;\n}", + "anchor": "module MissingNames {\n String? ", + "label": "string", + "expected": "module MissingNames {\n String? string = Null;\n}" + }, + { + "source": "module MissingNames {\n String[] = [];\n}", + "anchor": "module MissingNames {\n String[] ", + "label": "stringArray", + "expected": "module MissingNames {\n String[] stringArray = [];\n}" + }, + { + "source": "module MissingNames {\n void run(function String(Int)? ) {}\n}", + "anchor": "module MissingNames {\n void run(function String(Int)? ", + "label": "fn", + "expected": "module MissingNames {\n void run(function String(Int)? fn) {}\n}" + }, + { + "source": "module MissingNames {\n void run((String | Int) ) {}\n}", + "anchor": "module MissingNames {\n void run((String | Int) ", + "label": "value", + "expected": "module MissingNames {\n void run((String | Int) value) {}\n}" + }, + { + "source": "module MissingNames {\n void run() {\n val = \"hello\";\n }\n}", + "anchor": "val ", + "label": "text", + "expected": "module MissingNames {\n void run() {\n val text = \"hello\";\n }\n}" + }, + { + "source": "module MissingNames {\n void run() {\n var = 42;\n }\n}", + "anchor": "var ", + "label": "number", + "expected": "module MissingNames {\n void run() {\n var number = 42;\n }\n}" + }, + { + "source": "module MissingNames {\n void run() {\n val = new StringBuffer();\n }\n}", + "anchor": "val ", + "label": "stringBuffer", + "expected": "module MissingNames {\n void run() {\n val stringBuffer = new StringBuffer();\n }\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X152": { + "title": "enclosing instances and indexed function values use compiler-validated argument completion", + "manual": [], + "values": { + "file": "SemanticValues.x", + "variants": [ + { + "source": "module SemanticValues {\n class Owner {\n void take(Owner value) {}\n class Nested {\n void run() { take(thi); }\n }\n }\n}", + "anchor": "take(thi", + "label": "this.Owner", + "expected": "module SemanticValues {\n class Owner {\n void take(Owner value) {}\n class Nested {\n void run() { take(this.Owner); }\n }\n }\n}" + }, + { + "source": "module SemanticValues {\n class Owner {\n void take(Owner value) {}\n class Nested {\n void run() { take(this.Ow); }\n }\n }\n}", + "anchor": "take(this.Ow", + "label": "Owner", + "expected": "module SemanticValues {\n class Owner {\n void take(Owner value) {}\n class Nested {\n void run() { take(this.Owner); }\n }\n }\n}" + }, + { + "source": "module SemanticValues {\n void run() {\n function Int(Int)[] callbacks = [n -> n + 1];\n callbacks[0]();\n }\n}", + "anchor": "callbacks[0](", + "label": "0", + "expected": "module SemanticValues {\n void run() {\n function Int(Int)[] callbacks = [n -> n + 1];\n callbacks[0](0);\n }\n}" + }, + { + "source": "module SemanticValues {\n class Owner {\n class Nested {\n Owner current() = thi;\n }\n }\n}", + "anchor": "= thi", + "label": "this.Owner", + "expected": "module SemanticValues {\n class Owner {\n class Nested {\n Owner current() = this.Owner;\n }\n }\n}" + }, + { + "source": "module SemanticValues {\n class Owner {\n class Nested {\n Owner current() = this.Ow;\n }\n }\n}", + "anchor": "= this.Ow", + "label": "Owner", + "expected": "module SemanticValues {\n class Owner {\n class Nested {\n Owner current() = this.Owner;\n }\n }\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X153": { + "title": "property redirects and conditional mixins navigate to written bodies", + "manual": [], + "values": { + "file": "Dispatch.x", + "variants": [ + { + "source": "module Dispatch {\n interface Api { @RO Object value; }\n class Box implements Api { @Override String value.get() = \"text\"; }\n Object read(Api box) = box.value;\n}", + "anchor": "box.value", + "offset": 4, + "kind": "implementation", + "targets": [ + "get()" + ] + }, + { + "source": "module Dispatch {\n class Box(T value) incorporates conditional Textual {}\n static mixin Textual into Box { Int size() = value.size; }\n Int run(Box text) = text.size();\n}", + "anchor": "text.size", + "offset": 5, + "kind": "implementation", + "targets": [ + "size()" + ] + }, + { + "source": "module Dispatch {\n interface Api { Int read(); }\n class Dynamic(Api target) delegates Api(target) {}\n Int run(Dynamic value) = value.read();\n}", + "anchor": "value.read", + "offset": 6, + "kind": "implementation", + "targets": [] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X154": { + "title": "resolved semantic colors distinguish types, functions, enum values and writes", + "manual": [ + "Theme colors and token inspector presentation" + ], + "values": { + "file": "Access.x", + "source": "module Access {\n enum Mode {Quiet, Loud}\n class Box { Int value = 0; }\n Int size(List input) = input.size;\n static Int run(Box box, Int[] values, Int index) {\n Mode mode = Mode.Quiet;\n Int left = 0;\n Int right = 0;\n (left, right) = (1, 2);\n ++box.value;\n values[index] = left;\n return left + right + box.value;\n }\n}", + "tokenKinds": [ + { + "name": "box", + "type": "parameter" + }, + { + "name": "left", + "type": "variable" + }, + { + "name": "value", + "type": "property" + }, + { + "name": "run", + "type": "function" + }, + { + "name": "Mode", + "type": "enum" + }, + { + "name": "Quiet", + "type": "enumMember" + }, + { + "name": "List", + "type": "interface" + } + ], + "methodName": "run", + "staticModifier": "static", + "declarationModifier": "declaration", + "anchor2": "left, right", + "variableName": "left", + "writeModifier": "modification", + "anchor": "return left", + "offset": 7, + "accesses": [ + { + "anchor": "left, right", + "offset": 0, + "write": true + }, + { + "anchor": "right) =", + "offset": 0, + "write": true + }, + { + "anchor": "++box.value", + "offset": 6, + "write": true + }, + { + "anchor": "++box.value", + "offset": 2, + "write": false + }, + { + "anchor": "values[index]", + "offset": 0, + "write": false + }, + { + "anchor": "values[index]", + "offset": 7, + "write": false + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X155": { + "title": "conditional source composition rename follows concrete generic receivers", + "manual": [], + "values": { + "file": "Conditional.x", + "anchor": "size()", + "replacement": "width", + "files": [ + { + "file": "Conditional.x", + "source": "module Conditional {\n class Box(T value) incorporates conditional Textual { Int size(); }\n static mixin Textual into Box { @Override Int size() = value.size; }\n Int read(Box text) = text.size();\n Int unrelated(Box number) = number.value;\n}", + "destination": "Conditional.x", + "expected": "module Conditional {\n class Box(T value) incorporates conditional Textual { Int width(); }\n static mixin Textual into Box { @Override Int width() = value.size; }\n Int read(Box text) = text.width();\n Int unrelated(Box number) = number.value;\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X156": { + "title": "extract a returned expression while preserving parameter and call bindings", + "manual": [], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n return input + step();\n }\n}", + "selected": "input + step()", + "title": "Extract expression to local variable", + "expected": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int extractedValue = input + step();\n return extractedValue;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X157": { + "title": "inline an adjacent returned local without duplicating evaluation or changing bindings", + "manual": [], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int value = input + step();\n return value;\n }\n}", + "selected": "value", + "title": "Inline returned local variable", + "expected": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n return input + step();\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X158": { + "title": "resolve import source links and link only explicit alias names", + "manual": [ + "Modifier-click the displayed import link to inspect its target in the editor." + ], + "values": { + "file": "ImportLinks.x", + "module": "ImportLinks", + "source": "module ImportLinks {\n package lib import LinkLibrary;\n import lib.Box as Crate;\n Crate value = new Crate();\n}", + "libraryFile": "LinkLibrary.x", + "libraryModule": "LinkLibrary", + "library": "module LinkLibrary { class Box {} }", + "alias": "Crate", + "linkNames": [ + "LinkLibrary", + "Crate" + ], + "aliasUses": [ + "Crate;", + "Crate value", + "Crate()" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X159": { + "title": "conditional member rename updates a closed consumer without a formal host contract", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "measure()", + "replacement": "width", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n class Box(T value) incorporates conditional Textual {}\n static mixin Textual into Box { Int measure() = value.size; }\n}", + "destination": "Library.x", + "expected": "module Library {\n class Box(T value) incorporates conditional Textual {}\n static mixin Textual into Box { Int width() = value.size; }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n Int read(lib.Box text) = text.measure();\n Int unrelated(lib.Box number) = number.value;\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n Int read(lib.Box text) = text.width();\n Int unrelated(lib.Box number) = number.value;\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X160": { + "title": "nested generic delegation joins property contracts and closed consumer uses during rename", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "value;", + "replacement": "content", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n interface Api { T value; T read(); }\n}", + "destination": "Library.x", + "expected": "module Library {\n interface Api { T content; T read(); }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n class Engine implements lib.Api {\n @Override String value = \"text\";\n @Override String read() = value;\n }\n class Forward(Engine target) delegates lib.Api(target) {}\n class Outer(Forward target) delegates lib.Api(target) {}\n String use(Outer box) = box.read() + box.value;\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n class Engine implements lib.Api {\n @Override String content = \"text\";\n @Override String read() = content;\n }\n class Forward(Engine target) delegates lib.Api(target) {}\n class Outer(Forward target) delegates lib.Api(target) {}\n String use(Outer box) = box.read() + box.content;\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X161": { + "title": "cross-package type Move rewrites aliases and closed consumers with companion resources and Undo/Redo", + "manual": [], + "values": { + "root": "App.x", + "from": "App/tools/Box.x", + "to": "App/util/Box.x", + "destination": "App/util", + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App { import tools.Box as Crate; Crate make() = new Crate(); }", + "destination": "App.x", + "expected": "module App { import util.Box as Crate; Crate make() = new Crate(); }" + }, + { + "file": "App/tools/Box.x", + "source": "class Box { Helper make() = new Helper(); static Int number() = 1; }", + "destination": "App/util/Box.x", + "expected": "class Box { tools.Helper make() = new tools.Helper(); static Int number() = 1; }" + }, + { + "file": "App/tools/Helper.x", + "source": "class Helper {}", + "destination": "App/tools/Helper.x", + "expected": "class Helper {}" + }, + { + "file": "App/tools/Box/Part.x", + "source": "class Part {}", + "destination": "App/util/Box/Part.x", + "expected": "class Part {}" + }, + { + "file": "App/tools/Box/data.txt", + "source": "keep this resource", + "destination": "App/util/Box/data.txt", + "expected": "keep this resource" + }, + { + "file": "App/util/Marker.x", + "source": "class Marker {}", + "destination": "App/util/Marker.x", + "expected": "class Marker {}" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package app import App; app.tools.Box make() = new app.tools.Box(); Int read() = app.tools.Box.number(); }", + "destination": "Consumer.x", + "expected": "module Consumer { package app import App; app.util.Box make() = new app.util.Box(); Int read() = app.util.Box.number(); }" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X162": { + "title": "configured container Move persists ordered source/resource roots with closed consumers and Undo/Redo", + "manual": [], + "values": { + "root": "old/App.x", + "afterRoot": "target/old/App.x", + "consumer": "Consumer.x", + "from": "old", + "to": "target/old", + "destination": "target", + "modules": [ + { + "name": "App", + "uri": "old/App.x", + "dependencies": [], + "resourceRoots": [ + "old/assets", + "fallback" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ], + "resourceRoots": [] + } + ], + "afterModules": [ + { + "name": "App", + "uri": "target/old/App.x", + "dependencies": [], + "resourceRoots": [ + "target/old/assets", + "fallback" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ], + "resourceRoots": [] + } + ], + "files": [ + { + "file": "old/App.x", + "destination": "target/old/App.x", + "source": "module App { Box make() = new Box(); static String text() = $./data.txt; }", + "expected": "module App { Box make() = new Box(); static String text() = $./data.txt; }" + }, + { + "file": "old/App/Box.x", + "destination": "target/old/App/Box.x", + "source": "class Box {}", + "expected": "class Box {}" + }, + { + "file": "old/assets/data.txt", + "destination": "target/old/assets/data.txt", + "source": "first resource root", + "expected": "first resource root" + }, + { + "file": "fallback/data.txt", + "destination": "fallback/data.txt", + "source": "fallback resource", + "expected": "fallback resource" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }", + "expected": "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }" + }, + { + "file": "target/Keep.txt", + "destination": "target/Keep.txt", + "source": "existing destination", + "expected": "existing destination" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X163": { + "title": "module-root Move carries its companion and pins default resources left behind through Undo/Redo", + "manual": [], + "values": { + "root": "old/App.x", + "afterRoot": "target/App.x", + "consumer": "Consumer.x", + "from": "old/App.x", + "to": "target/App.x", + "destination": "target", + "modules": [ + { + "name": "App", + "uri": "old/App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ], + "resourceRoots": [] + } + ], + "afterModules": [ + { + "name": "App", + "uri": "target/App.x", + "dependencies": [], + "resourceRoots": [ + "old" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ], + "resourceRoots": [] + } + ], + "files": [ + { + "file": "old/App.x", + "destination": "target/App.x", + "source": "module App { Box make() = new Box(); static String text() = $./data.txt; }", + "expected": "module App { Box make() = new Box(); static String text() = $./data.txt; }" + }, + { + "file": "old/App/Box.x", + "destination": "target/App/Box.x", + "source": "class Box {}", + "expected": "class Box {}" + }, + { + "file": "old/data.txt", + "destination": "old/data.txt", + "source": "resource stays at original path", + "expected": "resource stays at original path" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }", + "expected": "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }" + }, + { + "file": "target/Keep.txt", + "destination": "target/Keep.txt", + "source": "existing destination", + "expected": "existing destination" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X164": { + "title": "union method rename joins both source contracts and a closed cross-module consumer with Undo", + "manual": [], + "values": { + "file": "First.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "First.x", + "source": "module First {\n class Box { Int read() = 1; }\n}", + "destination": "First.x", + "expected": "module First {\n class Box { Int fetch() = 1; }\n}" + }, + { + "file": "Second.x", + "source": "module Second {\n class Box { Int read() = 2; }\n}", + "destination": "Second.x", + "expected": "module Second {\n class Box { Int fetch() = 2; }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package one import First;\n package two import Second;\n Int use(one.Box | two.Box target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package one import First;\n package two import Second;\n Int use(one.Box | two.Box target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X165": { + "title": "cyclic delegation renames its written contract and closed consumer without inventing a runtime target", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n interface Api { Int read(); }\n}", + "destination": "Library.x", + "expected": "module Library {\n interface Api { Int fetch(); }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n class First(Second next) delegates lib.Api(next) {}\n class Second(First next) delegates lib.Api(next) {}\n Int use(First target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n class First(Second next) delegates lib.Api(next) {}\n class Second(First next) delegates lib.Api(next) {}\n Int use(First target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X166": { + "title": "generic union receiver rename preserves concrete substitutions and closed consumer with Undo", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n class First(T item) { T read() = item; }\n class Second(T item) { T read() = item; }\n}", + "destination": "Library.x", + "expected": "module Library {\n class First(T item) { T fetch() = item; }\n class Second(T item) { T fetch() = item; }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n Int use(lib.First | lib.Second target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n Int use(lib.First | lib.Second target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X167": { + "title": "annotated union receiver rename preserves annotation arguments and closed consumer with Undo", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n class First { Int read() = 1; }\n class Second { Int read() = 2; }\n annotation Mark(String label) into Object {}\n}", + "destination": "Library.x", + "expected": "module Library {\n class First { Int fetch() = 1; }\n class Second { Int fetch() = 2; }\n annotation Mark(String label) into Object {}\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n Int use((@lib.Mark(\"one\") lib.First) | (@lib.Mark(\"two\") lib.Second) target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n Int use((@lib.Mark(\"one\") lib.First) | (@lib.Mark(\"two\") lib.Second) target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X168": { + "title": "nested union delegation rename preserves both branches and closed consumer with Undo", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n interface Api { Int read(); }\n class First implements Api { @Override Int read() = 1; }\n class Second implements Api { @Override Int read() = 2; }\n}", + "destination": "Library.x", + "expected": "module Library {\n interface Api { Int fetch(); }\n class First implements Api { @Override Int fetch() = 1; }\n class Second implements Api { @Override Int fetch() = 2; }\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n class Forward(lib.First | lib.Second target) delegates lib.Api(target) {}\n class Outer(Forward target) delegates lib.Api(target) {}\n Int use(Outer target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n class Forward(lib.First | lib.Second target) delegates lib.Api(target) {}\n class Outer(Forward target) delegates lib.Api(target) {}\n Int use(Outer target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X169": { + "title": "rename and move a type together with aliases closed consumers companions and Undo/Redo", + "manual": [], + "values": { + "root": "App.x", + "from": "App/tools/Box.x", + "to": "App/util/Parcel.x", + "destination": "App/util", + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App { import tools.Box as Crate; Crate make() = new Crate(); }", + "destination": "App.x", + "expected": "module App { import util.Parcel as Crate; Crate make() = new Crate(); }" + }, + { + "file": "App/tools/Box.x", + "source": "class Box { Helper make() = new Helper(); static Int number() = 1; }", + "destination": "App/util/Parcel.x", + "expected": "class Parcel { tools.Helper make() = new tools.Helper(); static Int number() = 1; }" + }, + { + "file": "App/tools/Helper.x", + "source": "class Helper {}", + "destination": "App/tools/Helper.x", + "expected": "class Helper {}" + }, + { + "file": "App/tools/Factory.x", + "destination": "App/tools/Factory.x", + "source": "class Factory { Box make() = new Box(); }", + "expected": "class Factory { util.Parcel make() = new util.Parcel(); }" + }, + { + "file": "App/tools/Box/Part.x", + "source": "class Part {}", + "destination": "App/util/Parcel/Part.x", + "expected": "class Part {}" + }, + { + "file": "App/tools/Box/data.txt", + "source": "keep this resource", + "destination": "App/util/Parcel/data.txt", + "expected": "keep this resource" + }, + { + "file": "App/util/Marker.x", + "source": "class Marker {}", + "destination": "App/util/Marker.x", + "expected": "class Marker {}" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package app import App; app.tools.Box make() = new app.tools.Box(); Int read() = app.tools.Box.number(); }", + "destination": "Consumer.x", + "expected": "module Consumer { package app import App; app.util.Parcel make() = new app.util.Parcel(); Int read() = app.util.Parcel.number(); }" + } + ], + "newName": "Parcel.x", + "consumer": "Consumer.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X170": { + "title": "rename and relocate a module root while preserving dependencies resources and Undo/Redo", + "manual": [], + "values": { + "root": "old/App.x", + "afterRoot": "target/Renamed.x", + "consumer": "Consumer.x", + "from": "old/App.x", + "to": "target/Renamed.x", + "destination": "target", + "modules": [ + { + "name": "App", + "uri": "old/App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ], + "resourceRoots": [] + } + ], + "afterModules": [ + { + "name": "Renamed", + "uri": "target/Renamed.x", + "dependencies": [], + "resourceRoots": [ + "old" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Renamed" + ], + "resourceRoots": [] + } + ], + "files": [ + { + "file": "old/App.x", + "destination": "target/Renamed.x", + "source": "module App { Box make() = new Box(); static String text() = $./data.txt; }", + "expected": "module Renamed { Box make() = new Box(); static String text() = $./data.txt; }" + }, + { + "file": "old/App/Box.x", + "destination": "target/Renamed/Box.x", + "source": "class Box {}", + "expected": "class Box {}" + }, + { + "file": "old/data.txt", + "destination": "old/data.txt", + "source": "resource stays at original path", + "expected": "resource stays at original path" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer { package app import App; app.Box make() = new app.Box(); String read() = app.text(); }", + "expected": "module Consumer { package app import Renamed; app.Box make() = new app.Box(); String read() = app.text(); }" + }, + { + "file": "target/Keep.txt", + "destination": "target/Keep.txt", + "source": "existing destination", + "expected": "existing destination" + } + ], + "newName": "Renamed.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X171": { + "title": "interacting type moves preserve mutual references and closed consumers in one Undo/Redo transaction", + "manual": [], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "from": "App/left/First.x", + "to": "App/util/First.x", + "destination": "App/util", + "moves": [ + { + "from": "App/left/First.x", + "to": "App/util/First.x" + }, + { + "from": "App/right/Second.x", + "to": "App/util/Second.x" + } + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "destination": "App.x", + "source": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}", + "expected": "module App {\n util.First first() = new util.First();\n util.Second second() = new util.Second();\n}" + }, + { + "file": "App/left/First.x", + "destination": "App/util/First.x", + "source": "class First { right.Second next() = new right.Second(); }", + "expected": "class First { util.Second next() = new util.Second(); }" + }, + { + "file": "App/right/Second.x", + "destination": "App/util/Second.x", + "source": "class Second { left.First next() = new left.First(); }", + "expected": "class Second { util.First next() = new util.First(); }" + }, + { + "file": "App/util/Marker.x", + "destination": "App/util/Marker.x", + "source": "class Marker {}", + "expected": "class Marker {}" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}", + "expected": "module Consumer {\n package app import App;\n app.util.First make() = new app.util.First();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X172": { + "title": "installed compiler proposal refuses an entire move batch with one colliding declaration", + "manual": [ + "This checks the installed proposal response without applying a move. VS Code cannot veto arbitrary Explorer moves when participation returns no edits." + ], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "from": "App/left/First.x", + "to": "App/util/First.x", + "destination": "App/util", + "moves": [ + { + "from": "App/left/First.x", + "to": "App/util/First.x" + }, + { + "from": "App/right/Second.x", + "to": "App/util/Taken.x" + } + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "destination": "App.x", + "source": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}", + "expected": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}" + }, + { + "file": "App/left/First.x", + "destination": "App/left/First.x", + "source": "class First { right.Second next() = new right.Second(); }", + "expected": "class First { right.Second next() = new right.Second(); }" + }, + { + "file": "App/right/Second.x", + "destination": "App/right/Second.x", + "source": "class Second { left.First next() = new left.First(); }", + "expected": "class Second { left.First next() = new left.First(); }" + }, + { + "file": "App/util/Marker.x", + "destination": "App/util/Marker.x", + "source": "class Marker {}", + "expected": "class Marker {}" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}", + "expected": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}" + }, + { + "file": "App/util.x", + "destination": "App/util.x", + "source": "package util { class Taken {} }", + "expected": "package util { class Taken {} }" + } + ], + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X173": { + "title": "move and rename into an empty nested package preserving comments aliases closed consumers companions and Undo/Redo", + "manual": [ + "Create App/util/nested as empty directories before configuring the graph. Keep Consumer closed. Move Box.x with New name Parcel.x; inspect comments, aliases, companions and resources after Move, Undo and Redo." + ], + "values": { + "root": "App.x", + "from": "App/tools/Box.x", + "to": "App/util/nested/Parcel.x", + "destination": "App/util/nested", + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App {\n import tools /* namespace */ . Box as Crate;\n Crate make() = new Crate();\n}", + "destination": "App.x", + "expected": "module App {\n import util /* namespace */ . nested.Parcel as Crate;\n Crate make() = new Crate();\n}" + }, + { + "file": "App/tools/Box.x", + "source": "class Box { Helper make() = new Helper(); static Int number() = 1; }", + "destination": "App/util/nested/Parcel.x", + "expected": "class Parcel { tools.Helper make() = new tools.Helper(); static Int number() = 1; }" + }, + { + "file": "App/tools/Helper.x", + "source": "class Helper {}", + "destination": "App/tools/Helper.x", + "expected": "class Helper {}" + }, + { + "file": "App/tools/Factory.x", + "destination": "App/tools/Factory.x", + "source": "class Factory { Box make() = new Box(); }", + "expected": "class Factory { util.nested.Parcel make() = new util.nested.Parcel(); }" + }, + { + "file": "App/tools/Box/Part.x", + "source": "class Part {}", + "destination": "App/util/nested/Parcel/Part.x", + "expected": "class Part {}" + }, + { + "file": "App/tools/Box/data.txt", + "source": "keep this resource", + "destination": "App/util/nested/Parcel/data.txt", + "expected": "keep this resource" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app /* alias */ . tools /* package */ . Box make() = new app . tools . Box();\n Int read() = app.tools /* call */ . Box.number();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package app import App;\n app /* alias */ . util /* package */ . nested.Parcel make() = new app . util . nested.Parcel();\n Int read() = app.util /* call */ . nested.Parcel.number();\n}" + } + ], + "newName": "Parcel.x", + "consumer": "Consumer.x", + "directories": [ + "App/util/nested" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X174": { + "title": "remove a package prefix preserving line comments nested types CRLF and Undo/Redo", + "manual": [ + "Move Box.x to the module companion root App. Inspect both comments and the nested Part constructor; Undo and Redo must restore exact source contents and paths." + ], + "values": { + "root": "App.x", + "from": "App/tools/Box.x", + "to": "App/Box.x", + "destination": "App", + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + } + ], + "files": [ + { + "file": "App.x", + "destination": "App.x", + "source": "module App {\r\n tools /* α */ . Box.Part make() = new tools // keep namespace note\r\n . Box.Part();\r\n}", + "expected": "module App {\r\n /* α */ Box.Part make() = new // keep namespace note\r\n Box.Part();\r\n}" + }, + { + "file": "App/tools/Box.x", + "destination": "App/Box.x", + "source": "class Box {}", + "expected": "class Box {}" + }, + { + "file": "App/tools/Box/Part.x", + "destination": "App/Box/Part.x", + "source": "static class Part {}", + "expected": "static class Part {}" + } + ], + "directories": [] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X175": { + "title": "empty package destination still refuses an inline collision for the entire move batch", + "manual": [ + "This checks the installed proposal response without applying a move. VS Code cannot veto arbitrary Explorer moves when participation returns no edits." + ], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "from": "App/left/First.x", + "to": "App/util/First.x", + "destination": "App/util", + "moves": [ + { + "from": "App/left/First.x", + "to": "App/util/First.x" + }, + { + "from": "App/right/Second.x", + "to": "App/util/Taken.x" + } + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "destination": "App.x", + "source": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}", + "expected": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}" + }, + { + "file": "App/left/First.x", + "destination": "App/left/First.x", + "source": "class First { right.Second next() = new right.Second(); }", + "expected": "class First { right.Second next() = new right.Second(); }" + }, + { + "file": "App/right/Second.x", + "destination": "App/right/Second.x", + "source": "class Second { left.First next() = new left.First(); }", + "expected": "class Second { left.First next() = new left.First(); }" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}", + "expected": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}" + }, + { + "file": "App/util.x", + "destination": "App/util.x", + "source": "package util { class Taken {} }", + "expected": "package util { class Taken {} }" + } + ], + "refused": true, + "directories": [ + "App/util" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X176": { + "title": "interacting moves into an empty package preserve mutual references closed consumers and Undo/Redo", + "manual": [ + "Create App/util as an empty directory. Move First.x and Second.x together; both mutual references and the closed Consumer must update atomically and survive Undo/Redo." + ], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "from": "App/left/First.x", + "to": "App/util/First.x", + "destination": "App/util", + "moves": [ + { + "from": "App/left/First.x", + "to": "App/util/First.x" + }, + { + "from": "App/right/Second.x", + "to": "App/util/Second.x" + } + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "destination": "App.x", + "source": "module App {\n left.First first() = new left.First();\n right.Second second() = new right.Second();\n}", + "expected": "module App {\n util.First first() = new util.First();\n util.Second second() = new util.Second();\n}" + }, + { + "file": "App/left/First.x", + "destination": "App/util/First.x", + "source": "class First { right.Second next() = new right.Second(); }", + "expected": "class First { util.Second next() = new util.Second(); }" + }, + { + "file": "App/right/Second.x", + "destination": "App/util/Second.x", + "source": "class Second { left.First next() = new left.First(); }", + "expected": "class Second { util.First next() = new util.First(); }" + }, + { + "file": "Consumer.x", + "destination": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.left.First make() = new app.left.First();\n}", + "expected": "module Consumer {\n package app import App;\n app.util.First make() = new app.util.First();\n}" + } + ], + "directories": [ + "App/util" + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X177": { + "title": "extract a typed local initializer preserving evaluation and comments with Undo/Redo", + "manual": [ + "Select the whole initializer, invoke Extract expression to local variable, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int result = input /* keep */ + step();\n return result;\n }\n}", + "selected": "input /* keep */ + step()", + "title": "Extract expression to local variable", + "expected": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int extractedValue = input /* keep */ + step();\n Int result = extractedValue;\n return result;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X178": { + "title": "inline a local into an adjacent typed initializer preserving lambda context and captures", + "manual": [ + "Invoke Inline local variable into initializer on value. Inspect the retained function type/capture and perform Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n function Int() read(Int input) {\n function Int() value = () -> input;\n function Int() result = value;\n return result;\n }\n}", + "selected": "value", + "title": "Inline local variable into initializer", + "expected": "module Extract {\n function Int() read(Int input) {\n function Int() result = () -> input;\n return result;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X179": { + "title": "remove an unused compiler constant while retaining comments and native history", + "manual": [ + "Invoke Remove unused local variable. Both comments stay, diagnostics stay clear and Undo/Redo/Undo restore exact text." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int input) {\n // keep context\n Int unused = 1 + 2; // keep explanation\n return input;\n }\n}", + "selected": "unused", + "title": "Remove unused local variable", + "expected": "module Extract {\n Int read(Int input) {\n // keep context\n // keep explanation\n return input;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X180": { + "title": "installed code actions refuse deleting an unused initializer that must execute", + "manual": [ + "Query actions at unused. Remove unused local variable must be absent and the document must stay unchanged. This is an installed-connection refusal assertion." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int unused = step();\n return input;\n }\n}", + "selected": "unused", + "title": "Remove unused local variable", + "expected": "module Extract {\n Int step() = 1;\n Int read(Int input) {\n Int unused = step();\n return input;\n }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X181": { + "title": "extract a private helper preserving stable input overloads comments and Undo/Redo", + "manual": [ + "Select the complete expression and inspect the private-method action. Apply it, check exact helper signature/body, diagnostics and Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int step(Int value) = value;\n Int step(String value) = value.size;\n Int read(Int input) {\n return input /* keep */ + step(input);\n }\n}", + "selected": "input /* keep */ + step(input)", + "title": "Extract expression to private method", + "expected": "module Extract {\n Int step(Int value) = value;\n Int step(String value) = value.size;\n Int read(Int input) {\n return extractedMethod(input);\n }\n\n private Int extractedMethod(Int input) {\n return input /* keep */ + step(input);\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X182": { + "title": "extract a typed initializer to a helper with explicit stable local and parameter inputs", + "manual": [ + "Select the complete expression and inspect the private-method action. Apply it, check exact helper signature/body, diagnostics and Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int input) {\n Int fixed = input + 1;\n Int result = fixed + input;\n return result;\n }\n}", + "selected": "fixed + input", + "title": "Extract expression to private method", + "expected": "module Extract {\n Int read(Int input) {\n Int fixed = input + 1;\n Int result = extractedMethod(fixed, input);\n return result;\n }\n\n private Int extractedMethod(Int fixed, Int input) {\n return fixed + input;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X183": { + "title": "extract within a generic owner preserving the implicit receiver and type parameter", + "manual": [ + "Select the complete expression and inspect the private-method action. Apply it, check exact helper signature/body, diagnostics and Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n Element identity(Element value) = value;\n Element read(Element input) {\n return identity(input);\n }\n }\n}", + "selected": "identity(input)", + "title": "Extract expression to private method", + "expected": "module Extract {\n class Box {\n Element identity(Element value) = value;\n Element read(Element input) {\n return extractedMethod(input);\n }\n\n private Element extractedMethod(Element input) {\n return identity(input);\n }\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X184": { + "title": "refuse method extraction when a captured input is mutable", + "manual": [ + "Select the complete expression and inspect the private-method action. It must be absent; the installed-connection refusal leaves source unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int input) {\n input++;\n return input + 1;\n }\n}", + "selected": "input + 1", + "title": "Extract expression to private method", + "expected": "module Extract {\n Int read(Int input) {\n input++;\n return input + 1;\n }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X185": { + "title": "create a private void method from a missing call", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n void run() {\n missing();\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n void run() {\n missing();\n }\n private void missing() {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X186": { + "title": "create a private method from resolved parameter and return types", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int value, String label) {\n return missing(value, label);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read(Int value, String label) {\n return missing(value, label);\n }\n private Int64 missing(Int64 arg1, String arg2) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X187": { + "title": "create a static method using compiler typed literal arguments", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n static String read() {\n return missing(\"hello\", 'x', Int8:7);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n static String read() {\n return missing(\"hello\", 'x', Int8:7);\n }\n private static String missing(String arg1, Char arg2, Int8 arg3) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X188": { + "title": "refuse missing method creation for an ambiguous numeric argument", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No method creation action may appear; the error and source remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read() {\n return missing(1);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read() {\n return missing(1);\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X189": { + "title": "create a private method from a typed block local", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int value) {\n Int other = value;\n return missing(other);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read(Int value) {\n Int other = value;\n return missing(other);\n }\n private Int64 missing(Int64 arg1) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X190": { + "title": "create a static method from an inferred local and typed initializer", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n static String read(Int value) {\n var other = value;\n String result = missing(other, \"hello\");\n return result;\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n static String read(Int value) {\n var other = value;\n String result = missing(other, \"hello\");\n return result;\n }\n private static String missing(Int64 arg1, String arg2) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X191": { + "title": "create a method with a parameterized initializer result", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n void run(List values) {\n List result = missing(values);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n void run(List values) {\n List result = missing(values);\n }\n private List missing(List arg1) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X192": { + "title": "refuse missing method creation without an initializer result type", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No method creation action may appear; the error and source remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n void run(Int value) {\n var result = missing(value);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n void run(Int value) {\n var result = missing(value);\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X193": { + "title": "create a private method through an explicit this receiver", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int value) {\n return this.missing(value);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read(Int value) {\n return this.missing(value);\n }\n private Int64 missing(Int64 arg1) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X194": { + "title": "create an instance method through a same owner local in a static caller", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n static Int read(Box peer, Int value) {\n var other = peer;\n Int result = other.missing(value);\n return result;\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n static Int read(Box peer, Int value) {\n var other = peer;\n Int result = other.missing(value);\n return result;\n }\n private Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X195": { + "title": "refuse private method creation in the caller for another receiver owner", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No private method creation action may appear in the caller. The explicitly public action names the destination owner; X201 tests applying it. Inspecting actions leaves the error and source unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Other {}\n class Box {\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Other {}\n class Box {\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X196": { + "title": "refuse private method creation through a public this view", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No method creation action may appear; the error and source remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int value) {\n return this:public.missing(value);\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read(Int value) {\n return this:public.missing(value);\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X197": { + "title": "create a static method from a class qualifier in an instance caller", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n Int read(Int value) {\n return Box.missing(value);\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n Int read(Int value) {\n return Box.missing(value);\n }\n private static Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X198": { + "title": "create a static method through a fully qualified owner and typed initializer", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n void read(List values) {\n val other = values;\n List result = Extract.Box.missing(other);\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n void read(List values) {\n val other = values;\n List result = Extract.Box.missing(other);\n }\n private static List missing(List arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X199": { + "title": "refuse static method creation through a runtime Type value", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No method creation action may appear; the error and source remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n Int read(Type type, Int value) {\n return type.missing(value);\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n Int read(Type type, Int value) {\n return type.missing(value);\n }\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X200": { + "title": "keep instance dispatch when a parameter shadows the class name", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the named method action; inspect its signature and TODO() body. The error clears. Undo restores the source and error; Redo clears it again, then Undo restores the original." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n static Int read(Extract.Box Box, Int value) {\n return Box.missing(value);\n }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n static Int read(Extract.Box Box, Int value) {\n return Box.missing(value);\n }\n private Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X201": { + "title": "create a public instance method in another source class", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Other {}\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Extract {\n class Other {\n public Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X202": { + "title": "create a public static method in a closed companion with Undo and Redo", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n String read(String value) {\n return Other.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "class Other {\n public static String missing(String arg1) {\n TODO();\n }\n}", + "initiallyValid": false, + "destinationFile": "Extract/Other.x", + "destinationSource": "class Other {}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X203": { + "title": "refuse a destination that changes the requested return type identity", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Object read(Other peer) {\n return peer.missing();\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "class Other { class Object {} }", + "initiallyValid": false, + "destinationFile": "Extract/Other.x", + "destinationSource": "class Other { class Object {} }", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X204": { + "title": "refuse missing method generation in an interface", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n interface Other {}\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Extract {\n interface Other {}\n Int read(Other peer, Int value) {\n return peer.missing(value);\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X205": { + "title": "create a public method from an inferred local argument", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Other {}\n Int read(Other peer, Int value) {\n val local = value;\n return peer.missing(local);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Extract {\n class Other {\n public Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n Int read(Other peer, Int value) {\n val local = value;\n return peer.missing(local);\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X206": { + "title": "create a companion method from a local argument and typed initializer", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n List read(List values) {\n val local = values;\n List result = Other.missing(local);\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "class Other {\n public static List missing(List arg1) {\n TODO();\n }\n}", + "initiallyValid": false, + "destinationFile": "Extract/Other.x", + "destinationSource": "class Other {}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X207": { + "title": "refuse a typed initializer repair when the destination shadows its result type", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Object read(Other peer) {\n Object result = peer.missing();\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "class Other { class Object {} }", + "initiallyValid": false, + "destinationFile": "Extract/Other.x", + "destinationSource": "class Other { class Object {} }", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X208": { + "title": "refuse a public signature for an inferred missing call result", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Other {}\n Int read(Int value) {\n var result = Other.missing(value);\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Extract {\n class Other {}\n Int read(Int value) {\n var result = Other.missing(value);\n return result;\n }\n}", + "initiallyValid": false, + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X209": { + "title": "create a public method in a closed source dependency with Undo and Redo", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n Int read(lib.Other peer, Int value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Library {\n class Other {\n public Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false, + "destinationFile": "Library.x", + "destinationSource": "module Library {\n class Other {}\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X210": { + "title": "create a dependency companion method from local and initializer types", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n lib.Value read(lib.Value value) {\n val local = value;\n lib.Value result = lib.Other.missing(local);\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "class Other {\n public static Value missing(Value arg1) {\n TODO();\n }\n}", + "initiallyValid": false, + "destinationFile": "Library/Other.x", + "destinationSource": "class Other {}", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n class Value {}\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X211": { + "title": "refuse a missing method that would introduce a reverse module dependency", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n class Value {}\n Value read(lib.Other peer, Value value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Library {\n class Other {}\n}", + "initiallyValid": false, + "destinationFile": "Library.x", + "destinationSource": "module Library {\n class Other {}\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X212": { + "title": "render a missing method using the destination module import alias", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n package shared import Types;\n shared.Value read(lib.Other peer, shared.Value value) {\n val local = value;\n shared.Value result = peer.missing(local);\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Library {\n package types import Types;\n class Other {\n public types.Value missing(types.Value arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false, + "destinationFile": "Library.x", + "destinationSource": "module Library {\n package types import Types;\n class Other {}\n}", + "files": [ + { + "file": "Types.x", + "source": "module Types {\n class Value {}\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X213": { + "title": "add a destination import and public method as one undoable repair", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n package shared import Types;\n shared.Value read(lib.Other peer, shared.Value value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Library {\n package types import Types;\n\n class Other {\n public types.Value missing(types.Value arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false, + "destinationFile": "Library.x", + "destinationSource": "module Library {\n class Other {}\n}", + "files": [ + { + "file": "Types.x", + "source": "module Types {\n class Value {}\n}" + } + ], + "sourceModules": [ + { + "name": "Types", + "uri": "Types.x", + "dependencies": [] + }, + { + "name": "Library", + "uri": "Library.x", + "dependencies": [ + "Types" + ] + }, + { + "name": "Extract", + "uri": "Extract.x", + "dependencies": [ + "Library", + "Types" + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X214": { + "title": "avoid companion alias collisions when importing inferred argument types", + "manual": [ + "Place the caret on missing and inspect Quick Fix. Apply the public method action and inspect the destination, signature and TODO() body. The caller error clears. Undo restores the destination and error; Redo clears it again, then Undo restores the original. The caller stays unchanged when editing a companion file.", + "The import belongs in Library.x and the method in Library/Other.x. Keep both closed initially. One Undo/Redo must restore both files atomically, including the original comment and properties." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n package shared import Types;\n shared.Value read(shared.Value value) {\n val local = value;\n shared.Value result = lib.Other.missing(local);\n return result;\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "// Destination comment.\nclass Other {\n Int types1 = 2;\n public static types2.Value missing(types2.Value arg1) {\n TODO();\n }\n}", + "initiallyValid": false, + "destinationFile": "Library/Other.x", + "destinationSource": "// Destination comment.\nclass Other {\n Int types1 = 2;\n}", + "files": [ + { + "file": "Types.x", + "source": "module Types {\n class Value {}\n}" + }, + { + "file": "Library.x", + "source": "module Library {\n Int types = 1;\n}", + "expected": "module Library {\n package types2 import Types;\n\n Int types = 1;\n}" + } + ], + "sourceModules": [ + { + "name": "Types", + "uri": "Types.x", + "dependencies": [] + }, + { + "name": "Library", + "uri": "Library.x", + "dependencies": [ + "Types" + ] + }, + { + "name": "Extract", + "uri": "Extract.x", + "dependencies": [ + "Library", + "Types" + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X215": { + "title": "refuse a signature import when only the caller has the dependency", + "manual": [ + "Place the caret on missing and inspect Quick Fix. No public-method creation action may appear; the source and error remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n package shared import Types;\n shared.Value read(lib.Other peer, shared.Value value) {\n return peer.missing(value);\n }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Library {\n class Other {}\n}", + "initiallyValid": false, + "destinationFile": "Library.x", + "destinationSource": "module Library {\n class Other {}\n}", + "files": [ + { + "file": "Types.x", + "source": "module Types {\n class Value {}\n}" + } + ], + "sourceModules": [ + { + "name": "Types", + "uri": "Types.x", + "dependencies": [] + }, + { + "name": "Library", + "uri": "Library.x", + "dependencies": [] + }, + { + "name": "Extract", + "uri": "Extract.x", + "dependencies": [ + "Library", + "Types" + ] + } + ], + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X216": { + "title": "cross-module type move persists imports dependencies companions and native Undo/Redo", + "manual": [], + "values": { + "root": "App.x", + "afterRoot": "App.x", + "consumer": "Consumer.x", + "to": "Other/Box.x", + "destination": "Other", + "directories": [ + "Other" + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Other", + "uri": "Other.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "afterModules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [ + "Other" + ] + }, + { + "name": "Other", + "uri": "Other.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App", + "Other" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App {\n tools.Box make() = new tools.Box();\n}", + "expected": "module App {\n package other import Other;\n\n other.Box make() = new other.Box();\n}", + "destination": "App.x" + }, + { + "file": "Other.x", + "source": "module Other {}", + "expected": "module Other {}", + "destination": "Other.x" + }, + { + "file": "App/tools/Box.x", + "source": "class Box {}", + "expected": "class Box {}", + "destination": "Other/Box.x" + }, + { + "file": "App/tools/Box/Part.x", + "source": "static class Part {}", + "expected": "static class Part {}", + "destination": "Other/Box/Part.x" + }, + { + "file": "App/tools/Box/data.txt", + "source": "companion bytes", + "expected": "companion bytes", + "destination": "Other/Box/data.txt" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.tools.Box make() = new app.tools.Box();\n}", + "expected": "module Consumer {\n package other import Other;\n\n package app import App;\n other.Box make() = new other.Box();\n}", + "destination": "Consumer.x" + } + ], + "from": "App/tools/Box.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X217": { + "title": "cross-module move refuses cyclic dependencies without edits or settings changes", + "manual": [], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "to": "Other/Box.x", + "destination": "Other", + "directories": [ + "Other" + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Other", + "uri": "Other.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App {\n tools.Box make() = new tools.Box();\n}", + "expected": "module App {\n tools.Box make() = new tools.Box();\n}", + "destination": "App.x" + }, + { + "file": "Other.x", + "source": "module Other {}", + "expected": "module Other {}", + "destination": "Other.x" + }, + { + "file": "App/tools/Box.x", + "source": "class Box { Helper make() = new Helper(); }", + "expected": "class Box { Helper make() = new Helper(); }", + "destination": "App/tools/Box.x" + }, + { + "file": "App/tools/Box/Part.x", + "source": "static class Part {}", + "expected": "static class Part {}", + "destination": "App/tools/Box/Part.x" + }, + { + "file": "App/tools/Box/data.txt", + "source": "companion bytes", + "expected": "companion bytes", + "destination": "App/tools/Box/data.txt" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.tools.Box make() = new app.tools.Box();\n}", + "expected": "module Consumer {\n package app import App;\n app.tools.Box make() = new app.tools.Box();\n}", + "destination": "Consumer.x" + }, + { + "file": "App/tools/Helper.x", + "source": "class Helper {}", + "expected": "class Helper {}", + "destination": "App/tools/Helper.x" + } + ], + "from": "App/tools/Box.x", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X218": { + "title": "overlapping parent and child moves preserve companions with native Undo/Redo", + "manual": [ + "Submit the independently moved child before its parent to reach the VS Code Undo check. Apply succeeds, but Undo fails in VS Code 1.140.0 because inverse operations retain their original order (UP23). Keep this acceptance test failing until the host is repaired. IntelliJ passes the complete transaction. Verify companions, closed consumers, Undo and Redo." + ], + "values": { + "root": "App.x", + "consumer": "Consumer.x", + "to": "App/util/Box.x", + "destination": "App/util", + "directories": [ + "App/util" + ], + "moves": [ + { + "from": "App/tools/Box.x", + "to": "App/util/Box.x" + }, + { + "from": "App/tools", + "to": "App/util/tools" + } + ], + "modules": [ + { + "name": "App", + "uri": "App.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "App.x", + "source": "module App {\n tools.Box make() = new tools.Box();\n}", + "expected": "module App {\n util.Box make() = new util.Box();\n}", + "destination": "App.x" + }, + { + "file": "App/tools/Box.x", + "source": "class Box {}", + "expected": "class Box {}", + "destination": "App/util/Box.x" + }, + { + "file": "App/tools/Helper.x", + "source": "class Helper {}", + "expected": "class Helper {}", + "destination": "App/util/tools/Helper.x" + }, + { + "file": "App/tools/Box/Part.x", + "source": "static class Part {}", + "expected": "static class Part {}", + "destination": "App/util/Box/Part.x" + }, + { + "file": "App/tools/Box/data.txt", + "source": "preserved bytes", + "expected": "preserved bytes", + "destination": "App/util/Box/data.txt" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package app import App;\n app.tools.Box make() = new app.tools.Box();\n}", + "expected": "module Consumer {\n package app import App;\n app.util.Box make() = new app.util.Box();\n}", + "destination": "Consumer.x" + } + ], + "from": "App/tools/Box.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X219": { + "title": "module relocation captures incoming resource bytes with native Undo/Redo", + "manual": [], + "values": { + "root": "old/App.x", + "afterRoot": "assets/old/App.x", + "consumer": "Consumer.x", + "to": "assets/old", + "destination": "assets", + "modules": [ + { + "name": "App", + "uri": "old/App.x", + "dependencies": [], + "resourceRoots": [ + "assets", + "fallback" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "afterModules": [ + { + "name": "App", + "uri": "assets/old/App.x", + "dependencies": [], + "resourceRoots": [ + "assets", + "fallback" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "App" + ] + } + ], + "files": [ + { + "file": "old/App.x", + "source": "module App { static String text() = $./data.txt; }", + "expected": "module App { static String text() = $./data.txt; }", + "destination": "assets/old/App.x" + }, + { + "file": "old/extra.txt", + "source": "incoming bytes", + "expected": "incoming bytes", + "destination": "assets/old/extra.txt" + }, + { + "file": "assets/keep.txt", + "source": "existing root", + "expected": "existing root", + "destination": "assets/keep.txt" + }, + { + "file": "fallback/data.txt", + "source": "fallback bytes", + "expected": "fallback bytes", + "destination": "fallback/data.txt" + }, + { + "file": "Consumer.x", + "source": "module Consumer { package app import App; String text() = app.text(); }", + "expected": "module Consumer { package app import App; String text() = app.text(); }", + "destination": "Consumer.x" + } + ], + "from": "old" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X220": { + "title": "typed numeric annotation values survive family rename and closed-consumer Undo", + "manual": [], + "values": { + "file": "Library.x", + "anchor": "read()", + "replacement": "fetch", + "files": [ + { + "file": "Library.x", + "source": "module Library {\n class First { Int read() = 1; }\n class Second { Int read() = 2; }\n annotation Mark(Float64 label) into Object {}\n}", + "destination": "Library.x", + "expected": "module Library {\n class First { Int fetch() = 1; }\n class Second { Int fetch() = 2; }\n annotation Mark(Float64 label) into Object {}\n}" + }, + { + "file": "Consumer.x", + "source": "module Consumer {\n package lib import Library;\n Int use((@lib.Mark(Float64:1.5) lib.First) | (@lib.Mark(Float64:2.5) lib.Second) target) = target.read();\n}", + "destination": "Consumer.x", + "expected": "module Consumer {\n package lib import Library;\n Int use((@lib.Mark(Float64:1.5) lib.First) | (@lib.Mark(Float64:2.5) lib.Second) target) = target.fetch();\n}" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X221": { + "title": "create a missing method using the exact generic owner formal", + "manual": [ + "Select missing and inspect Create private method 'missing'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Box {\n Element read(Element value) { return missing(value); }\n }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n class Box {\n Element read(Element value) { return missing(value); }\n private Element missing(Element arg1) {\n TODO();\n }\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X222": { + "title": "create a constrained generic missing method", + "manual": [ + "Select missing and inspect Create private method 'missing'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Value read(Value value) { return missing(value); }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Value read(Value value) { return missing(value); }\n private Value missing(Value arg1) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X223": { + "title": "create a conditional missing method preserving result arity", + "manual": [ + "Select missing and inspect Create private method 'missing'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n conditional Int read(Int value) { return missing(value); }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n conditional Int read(Int value) { return missing(value); }\n private conditional Int64 missing(Int64 arg1) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X224": { + "title": "create a public method on a compiler validated call receiver", + "manual": [ + "Select missing and inspect Create public method 'missing' in 'Other'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n class Other {\n }\n Other peer() = new Other();\n Int read(Int value) { return peer().missing(value); }\n}", + "selected": "missing", + "title": "Create public method 'missing' in 'Other'", + "expected": "module Extract {\n class Other {\n public Int64 missing(Int64 arg1) {\n TODO();\n }\n }\n Other peer() = new Other();\n Int read(Int value) { return peer().missing(value); }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X225": { + "title": "create named parameters from computed argument types", + "manual": [ + "Select missing and inspect Create private method 'missing'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read(Int value) { return missing(amount = value + 1); }\n}", + "selected": "missing", + "title": "Create private method 'missing'", + "expected": "module Extract {\n Int read(Int value) { return missing(amount = value + 1); }\n private Int64 missing(Int64 amount) {\n TODO();\n }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X226": { + "title": "create a missing zero argument class", + "manual": [ + "Select Widget and inspect Create class 'Widget'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Object make() { return new Widget(); }\n}", + "selected": "Widget", + "title": "Create class 'Widget'", + "expected": "module Extract {\n Object make() { return new Widget(); }\n\n class Widget {}\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X227": { + "title": "create a read only instance property with the required result type", + "manual": [ + "Select missing and inspect Create read-only property 'missing'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int read() { return missing; }\n}", + "selected": "missing", + "title": "Create read-only property 'missing'", + "expected": "module Extract {\n Int read() { return missing; }\n\n private Int missing.get() { TODO(); }\n}", + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X228": { + "title": "refuse inventing an initializer for a missing static constant", + "manual": [ + "Select missing and inspect Create read-only property 'missing'. Verify refusal and unchanged sources." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n static Int read() { return missing; }\n}", + "selected": "missing", + "title": "Create read-only property 'missing'", + "expected": "module Extract {\n static Int read() { return missing; }\n}", + "refused": true, + "initiallyValid": false + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X229": { + "title": "extract a nested expression using its compiler selected type", + "manual": [ + "Select input + 1 and inspect Extract expression to private method. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int accept(Int value) = value;\n Int read(Int input) {\n return accept(input + 1);\n }\n}", + "selected": "input + 1", + "title": "Extract expression to private method", + "expected": "module Extract {\n Int accept(Int value) = value;\n Int read(Int input) {\n return accept(extractedMethod(input));\n }\n\n private Int64 extractedMethod(Int input) {\n return input + 1;\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X230": { + "title": "extract contiguous statements preserving order with native history", + "manual": [ + "Select emit(input); and inspect Extract statements to private method. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n void emit(Int value) {}\n void run(Int input) {\n emit(input);\n emit(input + 1);\n }\n}", + "selected": "emit(input);\n emit(input + 1);", + "title": "Extract statements to private method", + "expected": "module Extract {\n void emit(Int value) {}\n void run(Int input) {\n extractedMethod(input);\n }\n\n private void extractedMethod(Int input) {\n emit(input);\n emit(input + 1);\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X231": { + "title": "refuse statement extraction of mutable captures", + "manual": [ + "Select emit(input); and inspect Extract statements to private method. Verify refusal and unchanged sources." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n void emit(Int value) {}\n void run(Int input) {\n emit(input);\n input++;\n emit(input + 1);\n }\n}", + "selected": "emit(input);\n input++;\n emit(input + 1);", + "title": "Extract statements to private method", + "expected": "module Extract {\n void emit(Int value) {}\n void run(Int input) {\n emit(input);\n input++;\n emit(input + 1);\n }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X232": { + "title": "inline a constant local across an intervening statement", + "manual": [ + "Select value and inspect Inline constant local variable. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int probe() = 1;\n Int read() {\n Int value = 1 + 2;\n probe();\n return value + probe();\n }\n}", + "selected": "value", + "title": "Inline constant local variable", + "expected": "module Extract {\n Int probe() = 1;\n Int read() {\n probe();\n return (1 + 2) + probe();\n }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X233": { + "title": "inline one private call while retaining the method declaration", + "manual": [ + "Select helper(); and inspect Inline private method call. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int probe() = 1;\n private Int helper() { return probe() + 2; }\n Int read() { return helper(); }\n}", + "selected": "helper();", + "title": "Inline private method call", + "expected": "module Extract {\n Int probe() = 1;\n private Int helper() { return probe() + 2; }\n Int read() { return (probe() + 2); }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X234": { + "title": "inline a private constant read while retaining its declaration", + "manual": [ + "Select amount; and inspect Inline constant property. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private static Int amount = 1 + 2;\n Int read() { return amount; }\n}", + "selected": "amount;", + "title": "Inline constant property", + "expected": "module Extract {\n private static Int amount = 1 + 2;\n Int read() { return (1 + 2); }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X235": { + "title": "refuse method inline when a caller parameter would capture a member", + "manual": [ + "Select helper(); and inspect Inline private method call. Verify refusal and unchanged sources." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int stored = 3;\n private Int helper() { return stored; }\n Int read(Int stored) { return helper(); }\n}", + "selected": "helper();", + "title": "Inline private method call", + "expected": "module Extract {\n Int stored = 3;\n private Int helper() { return stored; }\n Int read(Int stored) { return helper(); }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X236": { + "title": "safely delete an unused private method with parameter and call bindings", + "manual": [ + "Select unused and inspect Safely delete private member 'unused'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private Int unused(Int input) { return input + kept(); }\n Int kept() = 2;\n Int read() { return kept(); }\n}", + "selected": "unused", + "title": "Safely delete private member 'unused'", + "expected": "module Extract {\n Int kept() = 2;\n Int read() { return kept(); }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X237": { + "title": "refuse deleting a public API with unknown external consumers", + "manual": [ + "Select unused and inspect Safely delete private member 'unused'. Verify refusal and unchanged sources." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n Int unused(Int input) { return input + kept(); }\n Int kept() = 2;\n Int read() { return kept(); }\n}", + "selected": "unused", + "title": "Safely delete private member 'unused'", + "expected": "module Extract {\n Int unused(Int input) { return input + kept(); }\n Int kept() = 2;\n Int read() { return kept(); }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X238": { + "title": "safely delete an unused private compiler constant", + "manual": [ + "Select unused and inspect Safely delete private member 'unused'. Apply it, inspect exact text and diagnostics, then Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private static Int unused = 3;\n Int read() { return 2; }\n}", + "selected": "unused", + "title": "Safely delete private member 'unused'", + "expected": "module Extract {\n Int read() { return 2; }\n}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X239": { + "title": "extract an imported module type using its real source alias", + "manual": [ + "Select the initializer input in val value = input; and invoke Extract expression to private method. Verify lib.Value in the helper signature, unchanged Library source, diagnostics and Undo/Redo/Undo." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n package lib import Library;\n Object read(lib.Value input) {\n val value = input;\n return value;\n }\n}", + "selected": "input", + "title": "Extract expression to private method", + "expected": "module Extract {\n package lib import Library;\n Object read(lib.Value input) {\n val value = extractedMethod(input);\n return value;\n }\n\n private lib.Value extractedMethod(lib.Value input) {\n return input;\n }\n}", + "files": [ + { + "file": "Library.x", + "source": "module Library { class Value {} }" + } + ], + "sourceModules": [ + { + "name": "Library", + "uri": "Library.x" + }, + { + "name": "Extract", + "uri": "Extract.x", + "dependencies": [ + "Library" + ] + } + ], + "selectionOffset": 104 + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X240": { + "title": "refuse inline requiring argument substitution", + "manual": [ + "Select helper(); and inspect Inline private method call. Verify refusal and unchanged sources." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private Int helper(Int value = 1) { return value; }\n Int read() { return helper(); }\n}", + "selected": "helper();", + "title": "Inline private method call", + "expected": "module Extract {\n private Int helper(Int value = 1) { return value; }\n Int read() { return helper(); }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X241": { + "title": "refuse repeating a static runtime initializer during inline", + "manual": [ + "Inspect the action at amount; it must be absent and the source must remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private static Int amount = compute();\n static Int compute() = 3;\n Int read() { return amount; }\n}", + "selected": "amount;", + "title": "Inline constant property", + "expected": "module Extract {\n private static Int amount = compute();\n static Int compute() = 3;\n Int read() { return amount; }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X242": { + "title": "refuse deleting a static runtime initializer", + "manual": [ + "Inspect the action at amount; it must be absent and the source must remain unchanged." + ], + "values": { + "file": "Extract.x", + "source": "module Extract {\n private static Int amount = compute();\n static Int compute() = 3;\n Int read() { return 2; }\n}", + "selected": "amount", + "title": "Safely delete private member 'amount'", + "expected": "module Extract {\n private static Int amount = compute();\n static Int compute() = 3;\n Int read() { return 2; }\n}", + "refused": true + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X243": { + "title": "open the resolved wildcard import container", + "manual": [ + "Open the linked import target and verify the matching Library source. Conditional imports still show COMPILER-29; the condition is not applied." + ], + "values": { + "file": "ImportLinks.x", + "module": "ImportLinks", + "source": "module ImportLinks {\n package lib import LinkLibrary;\n import lib.tools.*;\n}", + "libraryFile": "LinkLibrary.x", + "libraryModule": "LinkLibrary", + "library": "module LinkLibrary { package tools { class Box {} } }", + "alias": "", + "linkNames": [ + "LinkLibrary", + "lib.tools" + ], + "aliasUses": [] + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Link target and protocol ranges are asserted; modifier-click presentation remains manual." + ] + } + }, + "X244": { + "title": "refuse guessed import links for unsupported conditional source syntax", + "manual": [ + "Open the conditional import fixture. The parser must report an ordinary error; no guessed semantic import link is returned. Conditional import syntax is not currently supported." + ], + "values": { + "file": "ImportLinks.x", + "module": "ImportLinks", + "source": "module ImportLinks {\n package lib import LinkLibrary;\n if (true) { import lib.tools.Box as Crate; }\n}", + "libraryFile": "LinkLibrary.x", + "libraryModule": "LinkLibrary", + "library": "module LinkLibrary { package tools { class Box {} } }", + "alias": "", + "linkNames": [], + "aliasUses": [] + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Link target and protocol ranges are asserted; modifier-click presentation remains manual." + ] + } + }, + "X245": { + "title": "link lambda parameters and nested captures without sibling names", + "manual": [ + "Request linked editing at each marked name and verify exact lexical ranges. Callable parameters must use Rename instead." + ], + "values": { + "file": "Linked.x", + "source": "module Linked {\n Int run() {\n function Int(Int) first = (Int input) -> {\n function Int() nested = () -> input;\n return nested();\n };\n function Int(Int) second = (Int input) -> input + 1;\n return first(1) + second(2);\n }\n}", + "queries": [ + { + "anchor": "input) -> {", + "occurrences": [ + "input) -> {", + "input;" + ] + }, + { + "anchor": "input) -> input", + "occurrences": [ + "input) -> input", + "input + 1" + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Uses the installed linked-editing request; automatic typing presentation remains a manual host check." + ] + } + }, + "X246": { + "title": "refuse document linked editing for callable parameters and members", + "manual": [ + "Request linked editing at each marked name and verify exact lexical ranges. Callable parameters must use Rename instead." + ], + "values": { + "file": "Linked.x", + "source": "module Linked {\n private Int pick(Int privateInput) = privateInput;\n Int exposed(Int publicInput) = publicInput;\n Int run() = pick(privateInput = 1) + exposed(publicInput = 2);\n class Box(Int value) { Int read() = value; }\n}", + "queries": [ + { + "anchor": "privateInput", + "occurrences": [] + }, + { + "anchor": "publicInput", + "occurrences": [] + }, + { + "anchor": "value", + "occurrences": [] + }, + { + "anchor": "pick", + "occurrences": [] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Uses the installed linked-editing request; automatic typing presentation remains a manual host check." + ] + } + }, + "X247": { + "title": "link an explicit same-spelling alias without changing its imported target", + "manual": [ + "Request linked editing at each marked name and verify exact lexical ranges. Callable parameters must use Rename instead." + ], + "values": { + "file": "Linked.x", + "source": "module Linked {\n package types { class Box {} }\n Int first() { import types.Box as Box; Box value = new Box(); return 1; }\n Int second() { import types.Box as Box; Box other = new Box(); return 2; }\n}", + "queries": [ + { + "anchor": "Box; Box value", + "occurrences": [ + "Box; Box value", + "Box value", + "Box(); return 1" + ] + }, + { + "anchor": "Box; Box other", + "occurrences": [ + "Box; Box other", + "Box other", + "Box(); return 2" + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Uses the installed linked-editing request; automatic typing presentation remains a manual host check." + ] + } + }, + "X248": { + "title": "retain outline folding and strictly expanding selection in damaged syntax", + "manual": [ + "Open each damaged source variant. Inspect Alias/current/later in the outline, fold current and expand selection at its name. Repair the source and verify stale entries disappear." + ], + "values": { + "file": "Structure.x", + "variants": [ + { + "source": "module Structure {\n typedef Int as Alias;\n Object current(String value) {\n /* 😀 */ return [value.si;\n }\n Int later() = 42;\n}", + "symbols": [ + "Structure", + "Alias", + "current", + "later" + ], + "anchor": "current", + "foldLine": 2 + }, + { + "source": "module Structure {\n typedef Int as Alias;\n Object current(String value) {\n /* 😀 */ return (1, value.si;\n }\n Int later() = 42;\n}", + "symbols": [ + "Structure", + "Alias", + "current", + "later" + ], + "anchor": "current", + "foldLine": 2 + }, + { + "source": "module Structure {\n typedef Int as Alias;\n Object current(String value) {\n /* 😀 */ return use(value.si;\n }\n Int later() = 42;\n}", + "symbols": [ + "Structure", + "Alias", + "current", + "later" + ], + "anchor": "current", + "foldLine": 2 + }, + { + "source": "module Structure {\n typedef Int as Alias;\n void current(Int value, Str) {\n /* 😀 */ Int retained = 1;\n }\n Int later() = 42;\n}", + "symbols": [ + "Structure", + "Alias", + "current", + "later" + ], + "anchor": "current", + "foldLine": 2 + } + ], + "repaired": "module Structure { Int repaired() = 1; }" + }, + "intellij": { + "coverage": "full", + "limitations": [ + "Uses installed outline, folding and selection providers; interactive selection keybindings remain manual." + ] + } + }, + "X249": { + "title": "format continuation and standalone comment margins without changing literals", + "manual": [ + "Run Format Document, inspect the shared expected layout, repeat to check stability, then Undo/Redo/Undo. Literal contents must stay unchanged." + ], + "values": { + "file": "Layout.x", + "source": "module Layout {\nInt run() {\nString literal = \"two spaces\";\nInt value =\n1 +\n2;\n/* comment\n * keep relative layout and { braces }\n */\nreturn value;\n}\n}\n", + "expected": "module Layout {\n Int run() {\n String literal = \"two spaces\";\n Int value =\n 1 +\n 2;\n /* comment\n * keep relative layout and { braces }\n */\n return value;\n }\n}\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X250": { + "title": "wrap a long expression at token boundaries using the configured margin", + "manual": [ + "Run Format Document, inspect the shared expected layout, repeat to check stability, then Undo/Redo/Undo. Literal contents must stay unchanged." + ], + "values": { + "file": "Wrap.x", + "source": "module Wrap {\nString run() {\nreturn \"1111111111111111111111111111111111111111\" + \"2222222222222222222222222222222222222222\" + \"3333333333333333333333333333333333333333\";\n}\n}\n", + "expected": "module Wrap {\n String run() {\n return \"1111111111111111111111111111111111111111\" + \"2222222222222222222222222222222222222222\" +\n \"3333333333333333333333333333333333333333\";\n }\n}\n" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X251": { + "title": "refresh a detached graph index after unsaved edits, broken neighbors and root replacement", + "manual": [ + "Find references to Node0.Box with Consumer closed; expect its declaration and Consumer use. Repeat, add a field to Node0 without saving, and search for the new field. Break an independent root: complete references must be empty while healthy workspace symbols remain. Repair it, remove and restore one configured root, and repeat workspace-symbol search." + ], + "values": { + "roots": 16, + "rootName": "Node${0}", + "rootFile": "Node${0}.x", + "rootSource": "module Node${0} {\n class Box {}\n}\n", + "file": "Node0.x", + "anchor": "Box", + "replacement": "class Box { Int added = 1; }", + "addedSymbol": "added", + "symbol": "Box", + "referenceCount": 2, + "consumer": { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Node0" + ], + "source": "module Consumer {\n package lib import Node0;\n class Child extends lib.Box {}\n}\n" + }, + "brokenSource": "module Node${0} {\n Missing value;\n}\n", + "replaceFrom": "class Box {}" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X252": { + "title": "match portable export and import identities across closed source roots and artifact replacement", + "manual": [ + "Inspect textDocument/moniker in the language-server trace. Both editors exercise the connected protocol; neither has a native moniker panel. Library stays closed for the initial query." + ], + "values": { + "libraryFile": "X252/Library.x", + "librarySource": "module Library {\n static Int pick(Int n) = n;\n static String pick(String s) = s;\n}\n", + "consumerFile": "X252/Consumer.x", + "consumerSource": "module Consumer {\n package lib import Library;\n Int number() = lib.pick(1);\n String text() = lib.pick(\"x\");\n}\n", + "sourceModules": [ + { + "name": "Library", + "uri": "X252/Library.x", + "dependencies": [] + }, + { + "name": "Consumer", + "uri": "X252/Consumer.x", + "dependencies": [ + "Library" + ] + } + ], + "declaration": "pick(Int", + "reference": "pick(1)", + "overload": "pick(\"x\")", + "replaceFrom": "= n;", + "replaceWith": "= n + 1;", + "scheme": "ecstasy-artifact-v1", + "unique": "scheme" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X253": { + "title": "distinguish local and bundled identities and withhold IDs for incomplete source", + "manual": [ + "Inspect protocol monikers for the private property and bundled String type. Register locals and failed attempts have no artifact identity." + ], + "values": { + "file": "X253/Identities.x", + "source": "module Identities {\n private String hidden = \"text\";\n String read() {\n String local = hidden;\n return local;\n }\n}\n", + "private": "hidden =", + "library": "String hidden", + "local": "local =", + "replaceFrom": "return local;", + "replaceWith": "return missing;", + "scheme": "ecstasy-artifact-v1", + "unique": "scheme" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X254": { + "title": "open matching library content read only and resolve artifact monikers in the library view", + "manual": [ + "Navigate to bundled String and xml.Document. VS Code opens revision-owned virtual content; IntelliJ opens the protected file fallback. Verify edits and formatting cannot change these library views." + ], + "values": { + "file": "X254/LibraryViews.x", + "source": "module LibraryViews {\n package xml import xml.xtclang.org;\n void accept(String text, xml.Document document) {}\n}\n", + "types": [ + "String", + "Document" + ], + "scheme": "ecstasy-library", + "monikerScheme": "ecstasy-artifact-v1" + }, + "intellij": { + "coverage": "partial", + "limitations": [ + "UP25: LSP4IJ 0.21.0 has no workspace/textDocumentContent provider; tests use the supported read-only file fallback and direct library monikers." + ] + } + }, + "X255": { + "title": "accept and undo a compiler-backed inline name", + "manual": [ + "Place the caret at § (remove the marker). Invoke inline completion, accept the ghost suffix, then undo once." + ], + "values": { + "file": "X255/InlineValues.x", + "source": "module InlineValues {\n void run() {\n Int answer = 42;\n Int result = ans§;\n }\n}\n", + "mode": "accept", + "expected": "answer" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X256": { + "title": "dismiss ghost text and keep suggestions current while typing", + "manual": [ + "Place the caret at § (remove the marker). Dismiss the ghost suggestion, type w after ans, then accept the updated suffix." + ], + "values": { + "file": "X256/InlineValues.x", + "source": "module InlineValues {\n void run() {\n Int answer = 42;\n Int result = ans§;\n }\n}\n", + "mode": "typing", + "expected": "answer" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X257": { + "title": "respect ambiguity and the selected completion item", + "manual": [ + "Place the caret at § (remove the marker). Check automatic ambiguity suppression, invoke alternatives explicitly, and test popup-selection range/extension constraints." + ], + "values": { + "file": "X257/InlineValues.x", + "source": "module InlineValues {\n void run() {\n Int answer = 42;\n Int another = 7;\n Int result = an§;\n }\n}\n", + "mode": "selection", + "expected": "answer" + }, + "intellij": { + "coverage": "partial", + "limitations": [ + "UP26: LSP4IJ 0.21.0 labels direct invocations Automatic and omits selectedCompletionInfo; explicit alternatives and selection constraints are asserted over its connection, not the native UI." + ] + } + }, + "X258": { + "title": "complete a type-compatible value in an unfinished call", + "manual": [ + "Place the caret at § (remove the marker). Invoke and accept answer in the incomplete Int argument; answerText must not be offered. Repair the missing delimiters and verify diagnostics clear." + ], + "values": { + "file": "X258/InlineValues.x", + "source": "module InlineValues {\n void consume(Int value) {}\n void run() {\n Int answer = 42;\n String answerText = \"incompatible\";\n consume(ans§\n }\n}\n", + "mode": "incomplete", + "expected": "answer" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X259": { + "title": "rendered consumer hints follow dependency and settings changes across restart", + "manual": [ + "Disable inlay hints, change the dependency return type, re-enable hints and restart the server without editing the consumer.", + "Verify visible hints change from Int to String and back; disabling hints removes them." + ], + "values": { + "library": "RefreshLibrary.x", + "consumer": "RefreshConsumer.x", + "libraryModule": "RefreshLibrary", + "consumerModule": "RefreshConsumer", + "original": "module RefreshLibrary { static Int make() = 1; }\n", + "changed": "module RefreshLibrary { static String make() = \"one\"; }\n", + "source": "module RefreshConsumer {\n package lib import RefreshLibrary;\n package json import json.xtclang.org;\n import json.Doc;\n private Doc echo(Doc value) = value;\n void run() { var value = lib.make(); }\n}\n", + "before": "Int", + "after": "String" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X260": { + "title": "Cancel generated-input import, refuse overlap and retry through native controls", + "manual": [], + "values": { + "steps": [ + { + "operation": "cancel", + "prepare": true, + "output": "valid", + "outcome": "success", + "cancel": true, + "duplicate": true, + "expected": "cancelled", + "accepted": false + }, + { + "operation": "retry", + "prepare": false, + "output": "valid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "imported", + "accepted": true + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X261": { + "title": "Failed Gradle output retains compiler paths until a successful retry", + "manual": [], + "values": { + "steps": [ + { + "operation": "failed", + "prepare": false, + "output": "valid", + "outcome": "failure", + "cancel": false, + "duplicate": false, + "expected": "failed", + "accepted": false + }, + { + "operation": "retry", + "prepare": false, + "output": "valid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "imported", + "accepted": true + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X262": { + "title": "Successful tasks with malformed or missing models retain inputs and recover", + "manual": [], + "values": { + "steps": [ + { + "operation": "malformed", + "prepare": false, + "output": "invalid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "retained", + "accepted": false + }, + { + "operation": "missing", + "prepare": false, + "output": "missing", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "retained", + "accepted": false + }, + { + "operation": "repair", + "prepare": false, + "output": "valid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "imported", + "accepted": true + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X263": { + "title": "Import nested composite builds atomically and report generated-resource readiness", + "manual": [ + "Refresh compiler paths in a root with nested included builds. Check all three source owners, resource status and no partial publication while Gradle is pending." + ], + "values": { + "steps": [ + { + "operation": "refresh", + "prepare": false, + "output": "valid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "imported", + "accepted": true + } + ], + "included": [ + { + "folder": "included", + "module": "ImportIncluded" + }, + { + "folder": "included/nested", + "module": "ImportNested" + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X264": { + "title": "Refresh imported compiler inputs after native Gradle synchronization", + "manual": [ + "After an initial import, synchronize Gradle in IntelliJ or finish a Gradle task in VS Code. Changed inputs must reach diagnostics automatically without changing explicit settings." + ], + "values": { + "steps": [ + { + "operation": "refresh", + "prepare": false, + "output": "valid", + "outcome": "success", + "cancel": false, + "duplicate": false, + "expected": "imported", + "accepted": true, + "automatic": true + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X265": { + "title": "Close a project or remove a workspace folder during compiler import", + "manual": [ + "Start a gated import in a second project/folder and close/remove it. The producer must retire, late output must not restore its inputs, and the original project must remain usable." + ], + "values": { + "operation": "retirement" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X266": { + "title": "ordered library settings apply reset cancel and retain the bundled XDK", + "values": {}, + "manual": [ + "Library/source path picker appearance and layout" + ], + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X267": { + "title": "attached binary sources navigate read only and settings survive server restart", + "values": {}, + "manual": [], + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X268": { + "title": "invalid library replacements retain accepted inputs and ordered resources remain distinct", + "values": {}, + "manual": [], + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X269": { + "title": "machine-local JVM settings apply only on restart and invalid options retain the working server", + "manual": [ + "Reopen the IDE to verify application/user persistence and review Settings layout." + ], + "values": {}, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X270": { + "title": "log retention survives restart and the installed export saves a bounded server archive", + "manual": [ + "Choose a destination and cancel through the native export save dialog; inspect the ZIP in the operating system archive viewer." + ], + "values": {}, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X271": { + "title": "offline log export after stopping the server preserves the owning session", + "manual": [], + "values": {}, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X272": { + "title": "failed JVM launch exports support details and corrected settings recover", + "manual": [], + "values": {}, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X273": { + "title": "color swatches and native picker edits preserve positional and named arguments", + "manual": [], + "values": { + "mode": "apply" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X274": { + "title": "dismiss the color picker without edits and verify native Undo and Redo", + "manual": [], + "values": { + "mode": "history" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X275": { + "title": "invalid color source removes stale swatches and repair restores them", + "manual": [], + "values": { + "mode": "recovery" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X276": { + "title": "resolved annotations and bundled library ownership use theme semantic categories", + "manual": [], + "values": { + "file": "Highlighting.x", + "source": "module Highlighting {\n package json import json.xtclang.org;\n import ecstasy.annotations.Override as Replaces;\n class Base { String describe() = \"base\"; }\n annotation Marked into Object {}\n @Marked class Child extends Base {\n @Replaces String describe() = \"child\";\n }\n @ecstasy.annotations.Override String toString() = \"annotations\";\n class Other {\n class Override {}\n Override echo(Override value) = value;\n }\n json.JsonObject library(json.JsonObject value) = value;\n class JsonObject {}\n JsonObject local(JsonObject value) = value;\n}\n", + "tokens": [ + { + "anchor": "@Marked", + "offset": 1, + "text": "Marked", + "type": "decorator", + "library": false + }, + { + "anchor": "@Replaces", + "offset": 1, + "text": "Replaces", + "type": "decorator", + "library": true + }, + { + "anchor": "@ecstasy.annotations.Override", + "offset": 21, + "text": "Override", + "type": "decorator", + "library": true + }, + { + "anchor": "class Override", + "offset": 6, + "text": "Override", + "type": "class", + "library": false + }, + { + "anchor": "Override echo", + "offset": 0, + "text": "Override", + "type": "class", + "library": false + }, + { + "anchor": "json.JsonObject library", + "offset": 5, + "text": "JsonObject", + "type": "type", + "library": true + }, + { + "anchor": "JsonObject local", + "offset": 0, + "text": "JsonObject", + "type": "class", + "library": false + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X277": { + "title": "theme lexical detail survives semantic overlays and damaged source repair", + "manual": [], + "values": { + "file": "ThemeFallback.x", + "source": "module ThemeFallback {\n /** A documented class. */\n class Sample {\n Int value = 0x2A;\n String literal = \"control\\ttext\";\n Int first() { return 1; }\n static String escaped() = \"escaped\\ntext\";\n String interpolated() = $\"value={value}\";\n String describe() {\n return \"ready\";\n }\n }\n}\n", + "lexical": [ + "0x2A", + "static", + "\\n", + "return", + "\"ready\"" + ], + "recovery": [ + { + "from": "\"escaped\\ntext\"", + "to": "\"escaped" + }, + { + "from": "$\"value={value}\"", + "to": "$\"value={value.}\"" + }, + { + "from": "Int value = 0x2A;", + "to": "Int value = ;" + } + ], + "matchingStyles": [ + { + "left": "\"control\\ttext\"", + "right": "\"escaped\\ntext\"", + "length": 15 + }, + { + "left": "return 1;", + "right": "return \"ready\";", + "length": 6 + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X278": { + "title": "generate compiler documentation with exact parameter names and native history", + "manual": [], + "values": { + "file": "Documentation.x", + "source": "module Documentation {\n T echo(\n T input,\n Int count = 1) = input;\n}\n", + "expected": "module Documentation {\n /**\n * TODO: add description.\n * @param T TODO\n * @param input TODO\n * @param count TODO\n * @return TODO\n */\n T echo(\n T input,\n Int count = 1) = input;\n}\n", + "anchor": "echo", + "title": "Generate documentation comment" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "X279": { + "title": "reference lenses count closed consumers, navigate natively and toggle live", + "manual": [], + "values": { + "file": "LensLibrary.x", + "module": "LensLibrary", + "source": "module LensLibrary {\n Int read(Int value) = value;\n String read(String value) = value;\n Int unused() = 0;\n void run() { assert read(1) == 1; }\n}\n", + "consumerFile": "LensConsumer.x", + "consumerModule": "LensConsumer", + "consumer": "module LensConsumer {\n package lib import LensLibrary;\n Int result() = lib.read(2);\n}\n", + "changedConsumer": "module LensConsumer {\n package lib import LensLibrary;\n Int result() = lib.read(2) + lib.read(3);\n}\n", + "anchor": "read(Int", + "initialTitle": "2 references", + "changedTitle": "3 references" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "CFG1": { + "title": "removing and restoring source settings replaces the graph", + "manual": [], + "values": { + "consumer": "Consumer.x" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "CFG2": { + "title": "invalid cyclic settings preserve the last valid graph", + "manual": [], + "values": { + "pattern": { + "source": "cycl|invalid|reject", + "flags": "i" + }, + "sourceModules": [ + { + "name": "Library", + "uri": "Library.x", + "dependencies": [ + "Consumer" + ] + }, + { + "name": "Consumer", + "uri": "Consumer.x", + "dependencies": [ + "Library" + ] + } + ] + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "CFG3": { + "title": "unchanged source settings preserve current semantics", + "manual": [], + "values": {}, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "7a.8": { + "title": "compiler-only duplicate annotation warning is delivered exactly once", + "manual": [ + "Problems warning icon/code/count rendering and clicking a row" + ], + "values": { + "warningCount": 1, + "errorFile": "Navigation.x", + "replaceFrom": "Int value = 2;", + "replaceWith": "String value = 2;", + "file": "DupAnno.x", + "severity": "WARNING", + "code": "VERIFY-75", + "messageContains": "annotation", + "count": 1, + "declaration": "@Atomic @Override Int §x", + "edit": { + "from": "@Atomic @Override", + "to": "@Override" + } + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + }, + "7a.9": { + "title": "bad source has a bounded diagnostic result", + "manual": [], + "values": { + "file": "Broken.x", + "moduleStart": "module Broken {\n", + "declarationCount": 150, + "declaration": "Missing${0} value${1};", + "moduleEnd": "\n}", + "maximumErrors": 100, + "diagnosticCode": "EMB-5" + }, + "intellij": { + "coverage": "full", + "limitations": [] + } + } + } +} diff --git a/lang/test-fixtures/compiler-workload/platform.json b/lang/test-fixtures/compiler-workload/platform.json new file mode 100644 index 00000000000..e1dee4e2d3a --- /dev/null +++ b/lang/test-fixtures/compiler-workload/platform.json @@ -0,0 +1,18 @@ +{ + "description": "Explicit platform demo graph; resource overrides are inputs, never inferred from Gradle source text.", + "modules": [ + { "name": "auth.xqiz.it", "uri": "auth/src/main/x/auth.x", "dependencies": [] }, + { "name": "githubCLI.xqiz.it", "uri": "githubCLI/src/main/x/githubCLI.x", "dependencies": [] }, + { "name": "common.xqiz.it", "uri": "common/src/main/x/common.x", "dependencies": ["auth.xqiz.it"] }, + { "name": "stub.xqiz.it", "uri": "stub/src/main/x/stub.x", "dependencies": [] }, + { "name": "challenge.xqiz.it", "uri": "challenge/src/main/x/challenge.x", "dependencies": ["common.xqiz.it"] }, + { "name": "host.xqiz.it", "uri": "host/src/main/x/host.x", "dependencies": ["common.xqiz.it", "challenge.xqiz.it", "stub.xqiz.it"] }, + { "name": "proxy_manager.xqiz.it", "uri": "proxy/src/main/x/proxy.x", "dependencies": ["common.xqiz.it"] }, + { "name": "platformDB.xqiz.it", "uri": "platformDB/src/main/x/platformDB.x", "dependencies": ["common.xqiz.it"] }, + { "name": "platformCLI.xqiz.it", "uri": "platformCLI/src/main/x/platformCLI.x", "dependencies": ["auth.xqiz.it", "common.xqiz.it"] }, + { "name": "platformUI.xqiz.it", "uri": "platformUI/src/main/x/platformUI.x", "dependencies": ["common.xqiz.it", "challenge.xqiz.it", "auth.xqiz.it"], "resourceRoots": ["platformUI/gui/dist"] }, + { "name": "kernel.xqiz.it", "uri": "kernel/src/main/x/kernel.x", "dependencies": ["common.xqiz.it", "platformDB.xqiz.it"] } + ], + "document": "githubCLI/src/main/x/githubCLI.x", + "anchor": "sendRequest(HttpMethod" +} diff --git a/lang/test-fixtures/semantic-highlighting/SemanticColors.x b/lang/test-fixtures/semantic-highlighting/SemanticColors.x new file mode 100644 index 00000000000..9799219703c --- /dev/null +++ b/lang/test-fixtures/semantic-highlighting/SemanticColors.x @@ -0,0 +1,29 @@ +/** Compiler-resolved highlighting, using the editor's own theme colors. */ +module SemanticColors { + enum Mode {Quiet, Loud} + + interface Reader { Int read(Int Count); } + + // Lowercase type and uppercase parameter are intentional: spelling is not identity. + class counter implements Reader { + Int count = 0; + + construct() {} + + @Override + Int read(Int Count) { + var next = Count + count; + count += 1; + return next; + } + } + + static counter create() = new counter(); + + Int run() { + counter item = create(); + Mode mode = Mode.Quiet; + List values = [item.read(1)]; + return values[0]; + } +} diff --git a/lang/test-fixtures/semantic-highlighting/semantic-colors.code-workspace b/lang/test-fixtures/semantic-highlighting/semantic-colors.code-workspace new file mode 100644 index 00000000000..139701ab408 --- /dev/null +++ b/lang/test-fixtures/semantic-highlighting/semantic-colors.code-workspace @@ -0,0 +1,16 @@ +{ + "folders": [{ "path": "." }], + "settings": { + "[xtc]": { + "editor.semanticHighlighting.enabled": true + }, + "editor.semanticTokenColorCustomizations": { + "rules": { + "*.declaration:xtc": { "bold": true }, + "*.modification:xtc": { "underline": true }, + "function.static:xtc": { "italic": true }, + "comment.documentation:xtc": { "italic": true } + } + } + } +} diff --git a/lang/tree-sitter/build.gradle.kts b/lang/tree-sitter/build.gradle.kts index 0dd82f0775e..e998201a7c0 100644 --- a/lang/tree-sitter/build.gradle.kts +++ b/lang/tree-sitter/build.gradle.kts @@ -33,27 +33,31 @@ val dslProject: Project = project(":dsl") val osName: String = System.getProperty("os.name")?.lowercase() ?: "" val osArch: String = System.getProperty("os.arch") ?: "" -val treeSitterPlatform: String = when { - osName.contains("mac") && osArch in listOf("aarch64", "arm64") -> "macos-arm64" - osName.contains("mac") -> "macos-x64" - osName.contains("linux") && osArch in listOf("amd64", "x86_64") -> "linux-x64" - osName.contains("linux") && osArch in listOf("aarch64", "arm64") -> "linux-arm64" - else -> "unsupported" -} +val treeSitterPlatform: String = + when { + osName.contains("mac") && osArch in listOf("aarch64", "arm64") -> "macos-arm64" + osName.contains("mac") -> "macos-x64" + osName.contains("linux") && osArch in listOf("amd64", "x86_64") -> "linux-x64" + osName.contains("linux") && osArch in listOf("aarch64", "arm64") -> "linux-arm64" + else -> "unsupported" + } val treeSitterPlatformSupported = treeSitterPlatform != "unsupported" -val nativeLibExt: String = when { - osName.contains("windows") -> "dll" - osName.contains("mac") -> "dylib" - else -> "so" -} +val nativeLibExt: String = + when { + osName.contains("windows") -> "dll" + osName.contains("mac") -> "dylib" + else -> "so" + } // ============================================================================= // Directory Layout // ============================================================================= -val treeSitterCliVersion: String = libs.versions.lang.tree.sitter.cli.get() +val treeSitterCliVersion: String = + libs.versions.lang.tree.sitter.cli + .get() val treeSitterCliDir: Provider = layout.buildDirectory.dir("tree-sitter-cli/$treeSitterCliVersion/$treeSitterPlatform") val generatedDir: Provider = layout.buildDirectory.dir("generated") val nativeOutputDir: Provider = layout.buildDirectory.dir("native") @@ -82,13 +86,19 @@ val scannerCFile: Provider = generatedDir.map { it.dir("src").file( val treeSitterCliExe: Provider = treeSitterCliDir.map { it.file("tree-sitter").asFile.absolutePath } val nativeLibFile: Provider = nativeOutputDir.map { it.file("libtree-sitter-xtc.$nativeLibExt") } -val nativeToolChecksums = Properties().apply { - providers.fileContents(layout.projectDirectory.file("native-tool-checksums.properties")) - .asText.get().reader().use { load(it) } -} +val nativeToolChecksums = + Properties().apply { + providers + .fileContents(layout.projectDirectory.file("native-tool-checksums.properties")) + .asText + .get() + .reader() + .use { load(it) } + } -fun nativeToolChecksum(key: String): String = nativeToolChecksums.getProperty(key) - ?: throw GradleException("Pin the SHA-256 for '$key' in native-tool-checksums.properties before using this tool version") +fun nativeToolChecksum(key: String): String = + nativeToolChecksums.getProperty(key) + ?: throw GradleException("Pin the SHA-256 for '$key' in native-tool-checksums.properties before using this tool version") // ============================================================================= // Tree-sitter CLI Download @@ -97,74 +107,84 @@ fun nativeToolChecksum(key: String): String = nativeToolChecksums.getProperty(ke /** * Download tree-sitter CLI gzipped binary for the current platform. */ -val downloadTreeSitterCliGz = tasks.register("downloadTreeSitterCliGz") { - group = "tree-sitter" - description = "Download tree-sitter CLI gzipped binary" - enabled = treeSitterPlatformSupported - - val version = treeSitterCliVersion - val platform = treeSitterPlatform - val url = "https://github.com/tree-sitter/tree-sitter/releases/download/v$version/tree-sitter-$platform.gz" - val destPath = treeSitterCliDir.get().asFile.absolutePath - val destGzFile = treeSitterCliDir.map { it.file("tree-sitter-$platform.gz") } - src(url) - dest(destGzFile) - overwrite(false) - tempAndMove(true) - quiet(true) - inputs.property("cliVersion", version) - outputs.file(destGzFile) - - logTimed("[tree-sitter] Downloading CLI v$version ($platform)...\n[tree-sitter] URL: $url\n[tree-sitter] Dest: $destPath") { elapsed -> - val size = File(destPath).walkTopDown().filter { it.isFile }.sumOf { it.length() }.humanSize() - "[tree-sitter] CLI download complete ($size in $elapsed) -> $destPath" +val downloadTreeSitterCliGz = + tasks.register("downloadTreeSitterCliGz") { + group = "tree-sitter" + description = "Download tree-sitter CLI gzipped binary" + enabled = treeSitterPlatformSupported + + val version = treeSitterCliVersion + val platform = treeSitterPlatform + val url = "https://github.com/tree-sitter/tree-sitter/releases/download/v$version/tree-sitter-$platform.gz" + val destPath = treeSitterCliDir.get().asFile.absolutePath + val destGzFile = treeSitterCliDir.map { it.file("tree-sitter-$platform.gz") } + src(url) + dest(destGzFile) + overwrite(false) + tempAndMove(true) + quiet(true) + inputs.property("cliVersion", version) + outputs.file(destGzFile) + + logTimed( + "[tree-sitter] Downloading CLI v$version ($platform)...\n[tree-sitter] URL: $url\n[tree-sitter] Dest: $destPath", + ) { elapsed -> + val size = + File(destPath) + .walkTopDown() + .filter { it.isFile } + .sumOf { it.length() } + .humanSize() + "[tree-sitter] CLI download complete ($size in $elapsed) -> $destPath" + } } -} -val verifyTreeSitterCli = tasks.register("verifyTreeSitterCli") { - dependsOn(downloadTreeSitterCliGz) - enabled = treeSitterPlatformSupported - src(treeSitterCliDir.map { it.file("tree-sitter-$treeSitterPlatform.gz") }) - algorithm("SHA-256") - if (treeSitterPlatformSupported) { - checksum(nativeToolChecksum("tree-sitter-cli.$treeSitterCliVersion.$treeSitterPlatform")) +val verifyTreeSitterCli = + tasks.register("verifyTreeSitterCli") { + dependsOn(downloadTreeSitterCliGz) + enabled = treeSitterPlatformSupported + src(treeSitterCliDir.map { it.file("tree-sitter-$treeSitterPlatform.gz") }) + algorithm("SHA-256") + if (treeSitterPlatformSupported) { + checksum(nativeToolChecksum("tree-sitter-cli.$treeSitterCliVersion.$treeSitterPlatform")) + } } -} /** * Extract the tree-sitter CLI from the downloaded gzip file. * Uses Java's built-in GZIPInputStream for platform independence. */ -val extractTreeSitterCli = tasks.register("extractTreeSitterCli") { - group = "tree-sitter" - description = "Extract tree-sitter CLI from gzip" - dependsOn(verifyTreeSitterCli) - enabled = treeSitterPlatformSupported +val extractTreeSitterCli = + tasks.register("extractTreeSitterCli") { + group = "tree-sitter" + description = "Extract tree-sitter CLI from gzip" + dependsOn(verifyTreeSitterCli) + enabled = treeSitterPlatformSupported - val gzFile = treeSitterCliDir.map { it.file("tree-sitter-$treeSitterPlatform.gz").asFile } - val outputFile = treeSitterCliDir.map { it.file("tree-sitter").asFile } - val version = treeSitterCliVersion - val platform = treeSitterPlatform + val gzFile = treeSitterCliDir.map { it.file("tree-sitter-$treeSitterPlatform.gz").asFile } + val outputFile = treeSitterCliDir.map { it.file("tree-sitter").asFile } + val version = treeSitterCliVersion + val platform = treeSitterPlatform - inputs.file(gzFile) - outputs.file(outputFile) + inputs.file(gzFile) + outputs.file(outputFile) - doLast { - val input = gzFile.get() - val output = outputFile.get() + doLast { + val input = gzFile.get() + val output = outputFile.get() - logger.info("[tree-sitter] Extracting CLI v$version ($platform) from ${input.name}...") + logger.info("[tree-sitter] Extracting CLI v$version ($platform) from ${input.name}...") - GZIPInputStream(input.inputStream().buffered()).use { gzIn -> - output.outputStream().buffered().use { out -> - gzIn.copyTo(out) + GZIPInputStream(input.inputStream().buffered()).use { gzIn -> + output.outputStream().buffered().use { out -> + gzIn.copyTo(out) + } } - } - output.setExecutable(true) - logger.info("[tree-sitter] CLI extracted to: ${output.absolutePath}") + output.setExecutable(true) + logger.info("[tree-sitter] CLI extracted to: ${output.absolutePath}") + } } -} // ============================================================================= // Tree-sitter Runtime Library Download @@ -172,81 +192,87 @@ val extractTreeSitterCli = tasks.register("extractTreeSitterCli") { // Download tree-sitter source to build the runtime library (libtree-sitter). // This is needed by jtreesitter at runtime in addition to our grammar library. -val treeSitterRuntimeVersion: String = libs.versions.lang.tree.sitter.cli.get() +val treeSitterRuntimeVersion: String = + libs.versions.lang.tree.sitter.cli + .get() val treeSitterSourceDir: Provider = layout.buildDirectory.dir("tree-sitter-source/$treeSitterRuntimeVersion") val treeSitterSourceArchive = layout.buildDirectory.file("downloads/tree-sitter-$treeSitterRuntimeVersion.tar.gz") /** * Download tree-sitter source code for building the runtime library. */ -val downloadTreeSitterSource = tasks.register("downloadTreeSitterSource") { - group = "tree-sitter" - description = "Download tree-sitter source for runtime library build" - - val version = treeSitterRuntimeVersion - val url = "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v$version.tar.gz" - val destFile = treeSitterSourceArchive - val destPath = destFile.get().asFile.absolutePath - src(url) - dest(destFile) - overwrite(false) - tempAndMove(true) - quiet(true) - // Declare the downloaded tar.gz as a Gradle output so the standard - // up-to-date check re-runs the task when the file goes missing on - // disk (e.g. partial cleanup). Without this, the previous custom - // onlyIf interacted with the Download plugin's internal - // `.lastmodified` marker and the configuration cache to silently - // report the task as SKIPPED while leaving destPath nonexistent, - // which crashed extractTreeSitterSource at FileNotFoundException. - outputs.file(destFile) - - logTimed("[tree-sitter] Downloading source v$version...\n[tree-sitter] URL: $url\n[tree-sitter] Dest: $destPath") { elapsed -> - val size = File(destPath).length().humanSize() - "[tree-sitter] Source download complete ($size in $elapsed) -> $destPath" +val downloadTreeSitterSource = + tasks.register("downloadTreeSitterSource") { + group = "tree-sitter" + description = "Download tree-sitter source for runtime library build" + + val version = treeSitterRuntimeVersion + val url = "https://github.com/tree-sitter/tree-sitter/archive/refs/tags/v$version.tar.gz" + val destFile = treeSitterSourceArchive + val destPath = destFile.get().asFile.absolutePath + src(url) + dest(destFile) + overwrite(false) + tempAndMove(true) + quiet(true) + // Declare the downloaded tar.gz as a Gradle output so the standard + // up-to-date check re-runs the task when the file goes missing on + // disk (e.g. partial cleanup). Without this, the previous custom + // onlyIf interacted with the Download plugin's internal + // `.lastmodified` marker and the configuration cache to silently + // report the task as SKIPPED while leaving destPath nonexistent, + // which crashed extractTreeSitterSource at FileNotFoundException. + outputs.file(destFile) + + logTimed("[tree-sitter] Downloading source v$version...\n[tree-sitter] URL: $url\n[tree-sitter] Dest: $destPath") { elapsed -> + val size = File(destPath).length().humanSize() + "[tree-sitter] Source download complete ($size in $elapsed) -> $destPath" + } } -} -val verifyTreeSitterSource = tasks.register("verifyTreeSitterSource") { - dependsOn(downloadTreeSitterSource) - src(treeSitterSourceArchive) - algorithm("SHA-256") - checksum(nativeToolChecksum("tree-sitter-source.$treeSitterRuntimeVersion")) -} +val verifyTreeSitterSource = + tasks.register("verifyTreeSitterSource") { + dependsOn(downloadTreeSitterSource) + src(treeSitterSourceArchive) + algorithm("SHA-256") + checksum(nativeToolChecksum("tree-sitter-source.$treeSitterRuntimeVersion")) + } /** * Extract tree-sitter source code. */ -val extractTreeSitterSource = tasks.register("extractTreeSitterSource") { - group = "tree-sitter" - description = "Extract tree-sitter source" - dependsOn(verifyTreeSitterSource) - val tarGzFile = treeSitterSourceArchive.map { it.asFile } - val outputDir = treeSitterSourceDir.map { it.asFile } - val version = treeSitterRuntimeVersion - - inputs.file(tarGzFile) - outputs.dir(outputDir) - - doLast { - val input = tarGzFile.get() - val outDir = outputDir.get() - - logger.info("[tree-sitter] Extracting source v$version from ${input.name}...") - - GZIPInputStream(input.inputStream().buffered()).use { gzIn -> - TarArchiveInputStream(gzIn).use { tarIn -> - SafeTarExtractor.extract(tarIn, outDir) +val extractTreeSitterSource = + tasks.register("extractTreeSitterSource") { + group = "tree-sitter" + description = "Extract tree-sitter source" + dependsOn(verifyTreeSitterSource) + val tarGzFile = treeSitterSourceArchive.map { it.asFile } + val outputDir = treeSitterSourceDir.map { it.asFile } + val version = treeSitterRuntimeVersion + + inputs.file(tarGzFile) + outputs.dir(outputDir) + + doLast { + val input = tarGzFile.get() + val outDir = outputDir.get() + + logger.info("[tree-sitter] Extracting source v$version from ${input.name}...") + + GZIPInputStream(input.inputStream().buffered()).use { gzIn -> + TarArchiveInputStream(gzIn).use { tarIn -> + SafeTarExtractor.extract(tarIn, outDir) + } } + logger.info("[tree-sitter] Source extracted to: ${outDir.absolutePath}") } - logger.info("[tree-sitter] Source extracted to: ${outDir.absolutePath}") } -} // Path to extracted tree-sitter source lib directory -val treeSitterLibSrc: Provider = treeSitterSourceDir.map { - it.dir("tree-sitter-$treeSitterRuntimeVersion/lib") -} +val treeSitterLibSrc: Provider = + treeSitterSourceDir.map { + it.dir("tree-sitter-$treeSitterRuntimeVersion/lib") + } // ============================================================================= // Zig Compiler Download (for Cross-Compilation) @@ -254,19 +280,22 @@ val treeSitterLibSrc: Provider = treeSitterSourceDir.map { // Zig enables building native libraries for ALL platforms from ANY host machine. // Download once, build everywhere - no platform-specific toolchains needed. -val zigVersion: String = libs.versions.lang.zig.get() +val zigVersion: String = + libs.versions.lang.zig + .get() // Use persistent cache directory instead of build directory val zigDir: File = File(zigCacheDir, zigVersion) // Detect host platform for Zig download (format: {os}-{arch}) -val zigPlatform: String = when { - osName.contains("mac") && osArch in listOf("aarch64", "arm64") -> "aarch64-macos" - osName.contains("mac") && osArch in listOf("amd64", "x86_64") -> "x86_64-macos" - osName.contains("linux") && osArch in listOf("amd64", "x86_64") -> "x86_64-linux" - osName.contains("linux") && osArch in listOf("aarch64", "arm64") -> "aarch64-linux" - osName.contains("windows") && osArch in listOf("amd64", "x86_64") -> "x86_64-windows" - else -> "unsupported" -} +val zigPlatform: String = + when { + osName.contains("mac") && osArch in listOf("aarch64", "arm64") -> "aarch64-macos" + osName.contains("mac") && osArch in listOf("amd64", "x86_64") -> "x86_64-macos" + osName.contains("linux") && osArch in listOf("amd64", "x86_64") -> "x86_64-linux" + osName.contains("linux") && osArch in listOf("aarch64", "arm64") -> "aarch64-linux" + osName.contains("windows") && osArch in listOf("amd64", "x86_64") -> "x86_64-windows" + else -> "unsupported" + } val zigPlatformSupported = zigPlatform != "unsupported" val zigExtractDir = File(zigDir, "extracted-$zigPlatform") @@ -277,106 +306,114 @@ val zigExeName = if (osName.contains("windows")) "zig.exe" else "zig" * Download Zig compiler archive for the current platform. * Downloads to persistent cache in ~/.gradle/caches/zig// */ -val downloadZig = tasks.register("downloadZig") { - group = "zig" - description = "Download Zig compiler for cross-compilation" - enabled = zigPlatformSupported - - val version = zigVersion - val platform = zigPlatform - val ext = zigArchiveExt - val url = "https://ziglang.org/download/$version/zig-$platform-$version.$ext" - val destFile = File(zigDir, "zig-$platform-$version.$ext") - val destPath = destFile.absolutePath - src(url) - dest(destFile) - overwrite(false) - tempAndMove(true) - quiet(true) - outputs.file(destFile) - - logTimed("[zig] Downloading Zig compiler v$version ($platform)...\n[zig] URL: $url\n[zig] Dest: $destPath") { elapsed -> - val size = File(destPath).length().humanSize() - "[zig] Zig download complete ($size in $elapsed) -> $destPath" +val downloadZig = + tasks.register("downloadZig") { + group = "zig" + description = "Download Zig compiler for cross-compilation" + enabled = zigPlatformSupported + + val version = zigVersion + val platform = zigPlatform + val ext = zigArchiveExt + val url = "https://ziglang.org/download/$version/zig-$platform-$version.$ext" + val destFile = File(zigDir, "zig-$platform-$version.$ext") + val destPath = destFile.absolutePath + src(url) + dest(destFile) + overwrite(false) + tempAndMove(true) + quiet(true) + outputs.file(destFile) + + logTimed("[zig] Downloading Zig compiler v$version ($platform)...\n[zig] URL: $url\n[zig] Dest: $destPath") { elapsed -> + val size = File(destPath).length().humanSize() + "[zig] Zig download complete ($size in $elapsed) -> $destPath" + } } -} -val verifyZig = tasks.register("verifyZig") { - dependsOn(downloadZig) - enabled = zigPlatformSupported - src(downloadZig.map { it.dest }) - algorithm("SHA-256") - if (zigPlatformSupported) { - checksum(nativeToolChecksum("zig.$zigVersion.$zigPlatform")) +val verifyZig = + tasks.register("verifyZig") { + dependsOn(downloadZig) + enabled = zigPlatformSupported + src(downloadZig.map { it.dest }) + algorithm("SHA-256") + if (zigPlatformSupported) { + checksum(nativeToolChecksum("zig.$zigVersion.$zigPlatform")) + } } -} /** * Extract the Zig compiler from the downloaded archive. * Uses Gradle's zipTree for .zip files, and Apache Commons Compress for .tar.xz files. * Pure Java implementation - no system executables required. */ -abstract class ExtractZigTask @Inject constructor( - private val fsOps: FileSystemOperations, - private val archiveOps: ArchiveOperations -) : DefaultTask() { - - @get:InputFile - abstract val archiveFile: RegularFileProperty - - @get:OutputDirectory - abstract val outputDir: DirectoryProperty - - @get:Input - abstract val archiveExt: Property - - @TaskAction - fun extract() { - val archive = archiveFile.get().asFile - val outDir = outputDir.get().asFile - - logger.info("[zig] Extracting Zig compiler from ${archive.name}...") - logger.info("[zig] Archive: ${archive.absolutePath}") - logger.info("[zig] Target: ${outDir.absolutePath}") - - val duration = measureTime { - if (archiveExt.get() == "zip") { - fsOps.copy { - from(archiveOps.zipTree(archive)) - into(outDir) +abstract class ExtractZigTask + @Inject + constructor( + private val fsOps: FileSystemOperations, + private val archiveOps: ArchiveOperations, + ) : DefaultTask() { + @get:InputFile + abstract val archiveFile: RegularFileProperty + + @get:OutputDirectory + abstract val outputDir: DirectoryProperty + + @get:Input + abstract val archiveExt: Property + + @TaskAction + fun extract() { + val archive = archiveFile.get().asFile + val outDir = outputDir.get().asFile + + logger.info("[zig] Extracting Zig compiler from ${archive.name}...") + logger.info("[zig] Archive: ${archive.absolutePath}") + logger.info("[zig] Target: ${outDir.absolutePath}") + + val duration = + measureTime { + if (archiveExt.get() == "zip") { + fsOps.copy { + from(archiveOps.zipTree(archive)) + into(outDir) + } + } else { + // Use Apache Commons Compress for .tar.xz extraction (pure Java) + extractTarXz(archive, outDir) + } } - } else { - // Use Apache Commons Compress for .tar.xz extraction (pure Java) - extractTarXz(archive, outDir) - } - } - logger.info("[zig] Zig compiler extracted in $duration to: ${outDir.absolutePath}") - } + logger.info("[zig] Zig compiler extracted in $duration to: ${outDir.absolutePath}") + } - private fun extractTarXz(archive: File, outDir: File) { - outDir.mkdirs() + private fun extractTarXz( + archive: File, + outDir: File, + ) { + outDir.mkdirs() - archive.inputStream().buffered().use { fileIn -> - XZCompressorInputStream(fileIn).use { xzIn -> - TarArchiveInputStream(xzIn).use { tarIn -> - SafeTarExtractor.extract(tarIn, outDir) + archive.inputStream().buffered().use { fileIn -> + XZCompressorInputStream(fileIn).use { xzIn -> + TarArchiveInputStream(xzIn).use { tarIn -> + SafeTarExtractor.extract(tarIn, outDir) + } } } } } -} -val extractZig = tasks.register("extractZig") { - group = "zig" - description = "Extract Zig compiler from archive (cached in ~/.gradle/caches/zig/)" - dependsOn(verifyZig) - enabled = zigPlatformSupported +val extractZig = + tasks.register("extractZig") { + group = "zig" + description = "Extract Zig compiler from archive (cached in ~/.gradle/caches/zig/)" + dependsOn(verifyZig) + enabled = zigPlatformSupported - archiveFile.set(layout.file(downloadZig.map { it.dest })) - outputDir.set(zigExtractDir) - archiveExt.set(zigArchiveExt) -} + archiveFile.set(layout.file(downloadZig.map { it.dest })) + outputDir.set(zigExtractDir) + archiveExt.set(zigArchiveExt) + } val zigExe: String = File(zigExtractDir, "zig-$zigPlatform-$zigVersion/$zigExeName").absolutePath @@ -387,83 +424,89 @@ val zigExe: String = File(zigExtractDir, "zig-$zigPlatform-$zigVersion/$zigExeNa /** * Copy generated grammar.js and scanner.c from the DSL project. */ -val copyGrammar = tasks.register("copyGrammar") { - dependsOn(dslProject.tasks.named("generateTreeSitter")) - sourceFile.set(dslProject.layout.buildDirectory.file("generated/grammar.js")) - outputFile.set(grammarJsFile) -} +val copyGrammar = + tasks.register("copyGrammar") { + dependsOn(dslProject.tasks.named("generateTreeSitter")) + sourceFile.set(dslProject.layout.buildDirectory.file("generated/grammar.js")) + outputFile.set(grammarJsFile) + } -val copyScanner = tasks.register("copyScanner") { - dependsOn(dslProject.tasks.named("generateScannerC")) - sourceFile.set(dslProject.layout.buildDirectory.file("generated/src/scanner.c")) - outputFile.set(scannerCFile) -} +val copyScanner = + tasks.register("copyScanner") { + dependsOn(dslProject.tasks.named("generateScannerC")) + sourceFile.set(dslProject.layout.buildDirectory.file("generated/src/scanner.c")) + outputFile.set(scannerCFile) + } -val copyGrammarFiles = tasks.register("copyGrammarFiles") { - group = "tree-sitter" - description = "Copy grammar files from DSL project" - dependsOn(copyGrammar, copyScanner) -} +val copyGrammarFiles = + tasks.register("copyGrammarFiles") { + group = "tree-sitter" + description = "Copy grammar files from DSL project" + dependsOn(copyGrammar, copyScanner) + } /** * Generate tree-sitter.json config file for ABI version 15 support. * This eliminates the warning about missing config file. */ -val generateTreeSitterConfig = tasks.register("generateTreeSitterConfig") { - group = "tree-sitter" - description = "Generate tree-sitter.json (grammar manifest) and tree-sitter-user-config.json (CLI parser-directories)" - dependsOn(copyGrammarFiles) - - val outputFile = generatedDir.map { it.file("tree-sitter.json") } - // The CLI loads a user-level config from `~/.config/tree-sitter/config.json` - // (or wherever XDG points) listing `parser-directories` that hold grammars. - // Without it, every `tree-sitter parse` / `tree-sitter generate` invocation - // prints a multi-line "Warning: You have not configured any parser - // directories!" -- a million times in CI when we parse 880 files. Generate - // a minimal local config and pass it via `--config-path` everywhere; that - // both silences the warning and is hermetic (no $HOME dependency). - val userConfigFile = generatedDir.map { it.file("tree-sitter-user-config.json") } - val xdkVersion = project.version.toString() - - inputs.property("version", xdkVersion) - outputs.file(outputFile) - outputs.file(userConfigFile) - - val workDirPathProvider = generatedDir.map { it.asFile.absolutePath } - - doLast { - val configJson = """ - { - "grammars": [ +val generateTreeSitterConfig = + tasks.register("generateTreeSitterConfig") { + group = "tree-sitter" + description = "Generate tree-sitter.json (grammar manifest) and tree-sitter-user-config.json (CLI parser-directories)" + dependsOn(copyGrammarFiles) + + val outputFile = generatedDir.map { it.file("tree-sitter.json") } + // The CLI loads a user-level config from `~/.config/tree-sitter/config.json` + // (or wherever XDG points) listing `parser-directories` that hold grammars. + // Without it, every `tree-sitter parse` / `tree-sitter generate` invocation + // prints a multi-line "Warning: You have not configured any parser + // directories!" -- a million times in CI when we parse 880 files. Generate + // a minimal local config and pass it via `--config-path` everywhere; that + // both silences the warning and is hermetic (no $HOME dependency). + val userConfigFile = generatedDir.map { it.file("tree-sitter-user-config.json") } + val xdkVersion = project.version.toString() + + inputs.property("version", xdkVersion) + outputs.file(outputFile) + outputs.file(userConfigFile) + + val workDirPathProvider = generatedDir.map { it.asFile.absolutePath } + + doLast { + val configJson = + """ { - "name": "xtc", - "camelcase": "XTC", - "scope": "source.xtc", - "path": ".", - "file-types": ["x", "xtc"] - } - ], - "metadata": { - "version": "$xdkVersion", - "license": "Apache-2.0", - "description": "XTC (Ecstasy) grammar for tree-sitter", - "links": { - "repository": "https://github.com/xtclang/xvm" + "grammars": [ + { + "name": "xtc", + "camelcase": "XTC", + "scope": "source.xtc", + "path": ".", + "file-types": ["x", "xtc"] + } + ], + "metadata": { + "version": "$xdkVersion", + "license": "Apache-2.0", + "description": "XTC (Ecstasy) grammar for tree-sitter", + "links": { + "repository": "https://github.com/xtclang/xvm" + } + } } - } - } - """.trimIndent() - outputFile.get().asFile.writeText(configJson + "\n") + """.trimIndent() + outputFile.get().asFile.writeText(configJson + "\n") - val workDirPath = workDirPathProvider.get() - val userConfigJson = """ - { - "parser-directories": ["$workDirPath"] - } - """.trimIndent() - userConfigFile.get().asFile.writeText(userConfigJson + "\n") + val workDirPath = workDirPathProvider.get() + val userConfigJson = + """ + { + "parser-directories": ["$workDirPath"] + } + """.trimIndent() + userConfigFile.get().asFile.writeText(userConfigJson + "\n") + } } -} // ============================================================================= // Grammar Validation @@ -473,69 +516,73 @@ val generateTreeSitterConfig = tasks.register("generateTreeSitterConfig") { * Validate that the generated tree-sitter grammar compiles. * This runs `tree-sitter generate` which validates grammar.js and produces parser.c */ -val validateTreeSitterGrammar = tasks.register("validateTreeSitterGrammar") { - group = "tree-sitter" - description = "Validate tree-sitter grammar compiles" - dependsOn(copyGrammarFiles, extractTreeSitterCli, generateTreeSitterConfig) - enabled = treeSitterPlatformSupported - - // Declare inputs for proper up-to-date checking - inputs.file(grammarJsFile) - inputs.file(generatedDir.map { it.file("tree-sitter.json") }) - // scanner.c is copied to src/ by copyGrammarFiles and read by tree-sitter generate - inputs.file(scannerCFile) - inputs.property("cliVersion", treeSitterCliVersion) - inputs.file(treeSitterCliDir.map { it.file("tree-sitter") }).withPathSensitivity(PathSensitivity.NONE) - - // Declare outputs - tree-sitter generate produces these files - outputs.file(generatedDir.map { it.file("src/parser.c") }) - outputs.dir(generatedDir.map { it.dir("src/tree_sitter") }) - outputs.file(generatedDir.map { it.file("src/grammar.json") }) - outputs.file(generatedDir.map { it.file("src/node-types.json") }) - - workingDir(generatedDir) - executable(treeSitterCliExe.get()) - // Use native QuickJS runtime instead of requiring system Node.js. - // (`tree-sitter generate` doesn't accept --config-path; only `parse` - // does. generate doesn't emit the parser-directories warning anyway.) - args("generate", "--js-runtime", "native") - - // Capture paths at configuration time for logging in doFirst - val cliExePath = treeSitterCliExe.get() - val grammarJsPath = grammarJsFile.get().asFile.absolutePath - val workDirPath = generatedDir.get().asFile.absolutePath - val buildDirPath = layout.buildDirectory.get().asFile.absolutePath - - doFirst { - logger.info( - """ +val validateTreeSitterGrammar = + tasks.register("validateTreeSitterGrammar") { + group = "tree-sitter" + description = "Validate tree-sitter grammar compiles" + dependsOn(copyGrammarFiles, extractTreeSitterCli, generateTreeSitterConfig) + enabled = treeSitterPlatformSupported + + // Declare inputs for proper up-to-date checking + inputs.file(grammarJsFile) + inputs.file(generatedDir.map { it.file("tree-sitter.json") }) + // scanner.c is copied to src/ by copyGrammarFiles and read by tree-sitter generate + inputs.file(scannerCFile) + inputs.property("cliVersion", treeSitterCliVersion) + inputs.file(treeSitterCliDir.map { it.file("tree-sitter") }).withPathSensitivity(PathSensitivity.NONE) + + // Declare outputs - tree-sitter generate produces these files + outputs.file(generatedDir.map { it.file("src/parser.c") }) + outputs.dir(generatedDir.map { it.dir("src/tree_sitter") }) + outputs.file(generatedDir.map { it.file("src/grammar.json") }) + outputs.file(generatedDir.map { it.file("src/node-types.json") }) + + workingDir(generatedDir) + executable(treeSitterCliExe.get()) + // Use native QuickJS runtime instead of requiring system Node.js. + // (`tree-sitter generate` doesn't accept --config-path; only `parse` + // does. generate doesn't emit the parser-directories warning anyway.) + args("generate", "--js-runtime", "native") + + // Capture paths at configuration time for logging in doFirst + val cliExePath = treeSitterCliExe.get() + val grammarJsPath = grammarJsFile.get().asFile.absolutePath + val workDirPath = generatedDir.get().asFile.absolutePath + val buildDirPath = + layout.buildDirectory + .get() + .asFile.absolutePath + + doFirst { + logger.info( + """ |========== TREE-SITTER GRAMMAR VALIDATION ========== |tree-sitter CLI: $cliExePath |grammar.js: $grammarJsPath |working dir: $workDirPath |==================================================== - """.trimMargin() - ) + """.trimMargin(), + ) - // Assert we're using the build tree's tree-sitter CLI, not a system installation - require(cliExePath.startsWith(buildDirPath)) { - "ASSERTION FAILED: tree-sitter CLI must be from build directory!\n" + - " Expected prefix: $buildDirPath\n" + - " Actual path: $cliExePath\n" + - " This suggests a system-installed tree-sitter is being used." - } + // Assert we're using the build tree's tree-sitter CLI, not a system installation + require(cliExePath.startsWith(buildDirPath)) { + "ASSERTION FAILED: tree-sitter CLI must be from build directory!\n" + + " Expected prefix: $buildDirPath\n" + + " Actual path: $cliExePath\n" + + " This suggests a system-installed tree-sitter is being used." + } - // Assert grammar.js is from the build tree - require(grammarJsPath.startsWith(buildDirPath)) { - "ASSERTION FAILED: grammar.js must be from build directory!\n" + - " Expected prefix: $buildDirPath\n" + - " Actual path: $grammarJsPath\n" + - " This suggests grammar.js from an unexpected location is being used." - } + // Assert grammar.js is from the build tree + require(grammarJsPath.startsWith(buildDirPath)) { + "ASSERTION FAILED: grammar.js must be from build directory!\n" + + " Expected prefix: $buildDirPath\n" + + " Actual path: $grammarJsPath\n" + + " This suggests grammar.js from an unexpected location is being used." + } - logger.info("Assertions passed: Using build-local tree-sitter and grammar.js") + logger.info("Assertions passed: Using build-local tree-sitter and grammar.js") + } } -} // ============================================================================= // Parse Testing @@ -548,264 +595,283 @@ val validateTreeSitterGrammar = tasks.register("validateTreeSitterGrammar" * Supports filtering via -PtestFiles=pattern to test specific files. * Shows timing information sorted by parse time. */ -abstract class TreeSitterParseTestTask @Inject constructor( - private val execOps: ExecOperations -) : DefaultTask() { - - @get:Input - abstract val cliPath: Property - - @get:Input - abstract val workDir: Property - - @get:Input - abstract val libDirs: ListProperty - - @get:Input - abstract val rootDir: Property - - @get:Input - @get:Optional - abstract val fileFilter: Property - - @get:Input - @get:Optional - abstract val showTiming: Property - - @get:InputFiles - abstract val skipListFiles: ConfigurableFileCollection - - data class ParseResult(val relativePath: String, val success: Boolean, val timeMs: Long) +abstract class TreeSitterParseTestTask + @Inject + constructor( + private val execOps: ExecOperations, + ) : DefaultTask() { + @get:Input + abstract val cliPath: Property + + @get:Input + abstract val workDir: Property + + @get:Input + abstract val libDirs: ListProperty + + @get:Input + abstract val rootDir: Property + + @get:Input + @get:Optional + abstract val fileFilter: Property + + @get:Input + @get:Optional + abstract val showTiming: Property + + @get:InputFiles + abstract val skipListFiles: ConfigurableFileCollection + + data class ParseResult( + val relativePath: String, + val success: Boolean, + val timeMs: Long, + ) - @get:Input - abstract val buildDirPath: Property + @get:Input + abstract val buildDirPath: Property - @TaskAction - fun run() { - val cliAbsPath = cliPath.get() - val workDirAbsPath = workDir.get().absolutePath - val buildDir = buildDirPath.get() + @TaskAction + fun run() { + val cliAbsPath = cliPath.get() + val workDirAbsPath = workDir.get().absolutePath + val buildDir = buildDirPath.get() - // Log paths for debugging build issues - logger.info( - """ + // Log paths for debugging build issues + logger.info( + """ |========== TREE-SITTER PARSE TEST ========== |tree-sitter CLI: $cliAbsPath |working dir: $workDirAbsPath |build dir: $buildDir |============================================= - """.trimMargin() - ) - - // Assert we're using the build tree's tree-sitter CLI - require(cliAbsPath.startsWith(buildDir)) { - "ASSERTION FAILED: tree-sitter CLI must be from build directory!\n" + - " Expected prefix: $buildDir\n" + - " Actual path: $cliAbsPath\n" + - " This suggests a system-installed tree-sitter is being used." - } + """.trimMargin(), + ) - // Assert working directory is in the build tree (contains grammar.js) - require(workDirAbsPath.startsWith(buildDir)) { - "ASSERTION FAILED: working directory must be in build directory!\n" + - " Expected prefix: $buildDir\n" + - " Actual path: $workDirAbsPath\n" + - " This suggests grammar.js from an unexpected location is being used." - } + // Assert we're using the build tree's tree-sitter CLI + require(cliAbsPath.startsWith(buildDir)) { + "ASSERTION FAILED: tree-sitter CLI must be from build directory!\n" + + " Expected prefix: $buildDir\n" + + " Actual path: $cliAbsPath\n" + + " This suggests a system-installed tree-sitter is being used." + } - logger.info("Assertions passed: Using build-local tree-sitter and grammar") + // Assert working directory is in the build tree (contains grammar.js) + require(workDirAbsPath.startsWith(buildDir)) { + "ASSERTION FAILED: working directory must be in build directory!\n" + + " Expected prefix: $buildDir\n" + + " Actual path: $workDirAbsPath\n" + + " This suggests grammar.js from an unexpected location is being used." + } - val filter = fileFilter.orNull - val showTimingInfo = showTiming.getOrElse(true) + logger.info("Assertions passed: Using build-local tree-sitter and grammar") - // Maintained sources only. Build output holds copies of some sources (e.g. lib_ecstasy's - // implicit.x), which would double-count them and make the totals depend on what has been - // built, and archived examples are not maintained. - val excludedDirNames = setOf("build", "archive") - var xtcFiles = libDirs.get().flatMap { libDir -> - libDir.walkTopDown() - .onEnter { it == libDir || it.name !in excludedDirNames && !it.name.startsWith(".") } - .filter { it.isFile && it.extension == "x" } - .toList() - } + val filter = fileFilter.orNull + val showTimingInfo = showTiming.getOrElse(true) - // Apply skip list (paths relative to compositeRoot, with `#` comments). - // The skip list is split across two companion files so the real - // grammar debt (parked.txt) isn't drowned out by files that are - // permanently excluded from the XTC compile (intentional.txt): - // - treeSitterParseSkipList.intentional.txt - // - treeSitterParseSkipList.parked.txt - val skipFiles = skipListFiles.files.filter { it.isFile } - val skipPaths: Set = skipFiles.flatMap { f -> - f.useLines { lines -> - lines.map { it.substringBefore('#').trim() } - .filter { it.isNotEmpty() } - .toList() - } - }.toSet() - if (skipPaths.isNotEmpty()) { - val rootPath = rootDir.get() - val (toCheck, skipped) = xtcFiles.partition { file -> - file.relativeTo(rootPath).invariantSeparatorsPath !in skipPaths - } - val skippedPaths = skipped.map { it.relativeTo(rootPath).invariantSeparatorsPath }.toSet() - val staleEntries = skipPaths - skippedPaths - if (staleEntries.isNotEmpty()) { - logger.warn( - "Skip-list contains ${staleEntries.size} stale entries (no matching .x file): " + - staleEntries.sorted().joinToString(", ") - ) - } - val sources = skipFiles.joinToString(", ") { it.name } - logger.lifecycle("Tree-sitter parse skip list: ${skipped.size} file(s) excluded (see $sources)") - xtcFiles = toCheck - } + // Exclude generated copies and archived examples from the maintained source corpus. + val excludedDirNames = setOf("build", "archive") + var xtcFiles = + libDirs.get().flatMap { libDir -> + libDir + .walkTopDown() + .onEnter { it == libDir || (it.name !in excludedDirNames && !it.name.startsWith(".")) } + .filter { it.isFile && it.extension == "x" } + .toList() + } - // Apply filter if specified - if (!filter.isNullOrBlank()) { - val patterns = filter.split(",").map { it.trim() } - xtcFiles = xtcFiles.filter { file -> - val relativePath = file.relativeTo(rootDir.get()).path - patterns.any { pattern -> - relativePath.contains(pattern, ignoreCase = true) || - file.name.contains(pattern, ignoreCase = true) + // Apply skip list (paths relative to compositeRoot, with `#` comments). + // The skip list is split across two companion files so the real + // grammar debt (parked.txt) isn't drowned out by files that are + // permanently excluded from the XTC compile (intentional.txt): + // - treeSitterParseSkipList.intentional.txt + // - treeSitterParseSkipList.parked.txt + val skipFiles = skipListFiles.files.filter { it.isFile } + val skipPaths: Set = + skipFiles + .flatMap { f -> + f.useLines { lines -> + lines + .map { it.substringBefore('#').trim() } + .filter { it.isNotEmpty() } + .toList() + } + }.toSet() + if (skipPaths.isNotEmpty()) { + val rootPath = rootDir.get() + val (toCheck, skipped) = + xtcFiles.partition { file -> + file.relativeTo(rootPath).invariantSeparatorsPath !in skipPaths + } + val skippedPaths = skipped.map { it.relativeTo(rootPath).invariantSeparatorsPath }.toSet() + val staleEntries = skipPaths - skippedPaths + if (staleEntries.isNotEmpty()) { + logger.warn( + "Skip-list contains ${staleEntries.size} stale entries (no matching .x file): " + + staleEntries.sorted().joinToString(", "), + ) } + val sources = skipFiles.joinToString(", ") { it.name } + logger.lifecycle("Tree-sitter parse skip list: ${skipped.size} file(s) excluded (see $sources)") + xtcFiles = toCheck } - logger.info("Filter '$filter' matched ${xtcFiles.size} files") - } - if (xtcFiles.isEmpty()) { - logger.warn("No .x files found matching filter") - return - } - - // Warmup: parse the first file once to factor out tree-sitter CLI startup time. - // The CLI loads the grammar shared library and initializes internal state on first run, - // which adds ~1 second overhead that would skew timing for the first file. - // --config-path silences the "no parser directories" warning that would - // otherwise spam the log with 4 lines per parse invocation (~3500 lines - // for the full corpus on CI). - val userConfigPath = workDir.get().resolve("tree-sitter-user-config.json").absolutePath - val warmupFile = xtcFiles.first() - execOps.exec { - workingDir(workDir.get()) - executable(cliPath.get()) - args("parse", "--quiet", "--config-path", userConfigPath, warmupFile.absolutePath) - isIgnoreExitValue = true - } + // Apply filter if specified + if (!filter.isNullOrBlank()) { + val patterns = filter.split(",").map { it.trim() } + xtcFiles = + xtcFiles.filter { file -> + val relativePath = file.relativeTo(rootDir.get()).path + patterns.any { pattern -> + relativePath.contains(pattern, ignoreCase = true) || + file.name.contains(pattern, ignoreCase = true) + } + } + logger.info("Filter '$filter' matched ${xtcFiles.size} files") + } - logger.info("Testing tree-sitter parser on ${xtcFiles.size} XTC files...") + if (xtcFiles.isEmpty()) { + logger.warn("No .x files found matching filter") + return + } - val results = xtcFiles.map { file -> - val startNanos = System.nanoTime() - val result = execOps.exec { + // Warmup: parse the first file once to factor out tree-sitter CLI startup time. + // The CLI loads the grammar shared library and initializes internal state on first run, + // which adds ~1 second overhead that would skew timing for the first file. + // --config-path silences the "no parser directories" warning that would + // otherwise spam the log with 4 lines per parse invocation (~3500 lines + // for the full corpus on CI). + val userConfigPath = workDir.get().resolve("tree-sitter-user-config.json").absolutePath + val warmupFile = xtcFiles.first() + execOps.exec { workingDir(workDir.get()) executable(cliPath.get()) - args("parse", "--quiet", "--config-path", userConfigPath, file.absolutePath) + args("parse", "--quiet", "--config-path", userConfigPath, warmupFile.absolutePath) isIgnoreExitValue = true } - val elapsedMs = (System.nanoTime() - startNanos) / 1_000_000 - val relativePath = file.relativeTo(rootDir.get()).path - ParseResult(relativePath, result.exitValue == 0, elapsedMs) - } - val passed = results.count { it.success } - val failed = results.size - passed - val failures = results.filter { !it.success }.map { it.relativePath } - - // Log at lifecycle level so the summary appears in CI output without --info, - // and so a developer running this task locally sees the result without - // having to know about Gradle's logger levels. - logger.lifecycle("Tree-sitter parse results: $passed passed, $failed failed") - - if (failures.isNotEmpty()) { - val maxFailuresLogged = 20 - logger.lifecycle("Failed files (first $maxFailuresLogged):") - failures.take(maxFailuresLogged).forEach { logger.lifecycle(" - $it") } - if (failures.size > maxFailuresLogged) { - logger.lifecycle(" ... and ${failures.size - maxFailuresLogged} more") + logger.info("Testing tree-sitter parser on ${xtcFiles.size} XTC files...") + + val results = + xtcFiles.map { file -> + val startNanos = System.nanoTime() + val result = + execOps.exec { + workingDir(workDir.get()) + executable(cliPath.get()) + args("parse", "--quiet", "--config-path", userConfigPath, file.absolutePath) + isIgnoreExitValue = true + } + val elapsedMs = (System.nanoTime() - startNanos) / 1_000_000 + val relativePath = file.relativeTo(rootDir.get()).path + ParseResult(relativePath, result.exitValue == 0, elapsedMs) + } + + val passed = results.count { it.success } + val failed = results.size - passed + val failures = results.filter { !it.success }.map { it.relativePath } + + // Log at lifecycle level so the summary appears in CI output without --info, + // and so a developer running this task locally sees the result without + // having to know about Gradle's logger levels. + logger.lifecycle("Tree-sitter parse results: $passed passed, $failed failed") + + if (failures.isNotEmpty()) { + val maxFailuresLogged = 20 + logger.lifecycle("Failed files (first $maxFailuresLogged):") + failures.take(maxFailuresLogged).forEach { logger.lifecycle(" - $it") } + if (failures.size > maxFailuresLogged) { + logger.lifecycle(" ... and ${failures.size - maxFailuresLogged} more") + } } - } - // Show timing information if requested or if filter is active (smaller dataset) - if (showTimingInfo || (!filter.isNullOrBlank() && xtcFiles.size <= 50)) { - logger.info("Parse timing (slowest first):") - results.sortedByDescending { it.timeMs }.forEach { r -> - val status = if (r.success) "OK" else "FAIL" - logger.info(" %5dms [%4s] %s".format(r.timeMs, status, r.relativePath)) + // Show timing information if requested or if filter is active (smaller dataset) + if (showTimingInfo || (!filter.isNullOrBlank() && xtcFiles.size <= 50)) { + logger.info("Parse timing (slowest first):") + results.sortedByDescending { it.timeMs }.forEach { r -> + val status = if (r.success) "OK" else "FAIL" + logger.info(" %5dms [%4s] %s".format(r.timeMs, status, r.relativePath)) + } + val totalMs = results.sumOf { it.timeMs } + val avgMs = if (results.isNotEmpty()) totalMs / results.size else 0 + val sortedTimes = results.map { it.timeMs }.sorted() + val medianMs = + if (sortedTimes.isNotEmpty()) { + val mid = sortedTimes.size / 2 + if (sortedTimes.size % 2 == 0) (sortedTimes[mid - 1] + sortedTimes[mid]) / 2 else sortedTimes[mid] + } else { + 0 + } + logger.info("Total: ${totalMs}ms, Average: ${avgMs}ms, Median: ${medianMs}ms per file (${results.size} files)") } - val totalMs = results.sumOf { it.timeMs } - val avgMs = if (results.isNotEmpty()) totalMs / results.size else 0 - val sortedTimes = results.map { it.timeMs }.sorted() - val medianMs = if (sortedTimes.isNotEmpty()) { - val mid = sortedTimes.size / 2 - if (sortedTimes.size % 2 == 0) (sortedTimes[mid - 1] + sortedTimes[mid]) / 2 else sortedTimes[mid] - } else 0 - logger.info("Total: ${totalMs}ms, Average: ${avgMs}ms, Median: ${medianMs}ms per file (${results.size} files)") - } - // Fail the task once the full corpus has been processed and reported, so - // the build surfaces every parse failure in one pass. Throwing earlier - // would cut the failure list off mid-run; logging without throwing (the - // previous behaviour) let regressions accumulate silently for months. - if (failures.isNotEmpty()) { - throw GradleException( - "tree-sitter parse failed for ${failures.size} file(s) out of ${results.size}. " + - "See the lifecycle log above for the full list.", - ) + // Fail the task once the full corpus has been processed and reported, so + // the build surfaces every parse failure in one pass. Throwing earlier + // would cut the failure list off mid-run; logging without throwing (the + // previous behaviour) let regressions accumulate silently for months. + if (failures.isNotEmpty()) { + throw GradleException( + "tree-sitter parse failed for ${failures.size} file(s) out of ${results.size}. " + + "See the lifecycle log above for the full list.", + ) + } } } -} -val testTreeSitterParse = tasks.register("testTreeSitterParse") { - group = "tree-sitter" - description = "Test tree-sitter parsing on XDK library files. Use -PtestFiles=pattern to filter. Timing shown by default; use -PshowTiming=false to disable." - dependsOn(validateTreeSitterGrammar) - enabled = treeSitterPlatformSupported - - // Use XdkPropertiesService to find composite root (works for included builds) - val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) - - cliPath.set(treeSitterCliExe) - // NOTE: Setting workDir to the generated directory allows tree-sitter CLI to find the grammar - // via tree-sitter.json in the current directory. This produces harmless warnings about - // "not configured any parser directories" because there's no global ~/.config/tree-sitter/config.json, - // but the CLI still works correctly by discovering the grammar from the working directory. - workDir.set(generatedDir.map { it.asFile }) - buildDirPath.set(layout.buildDirectory.map { it.asFile.absolutePath }) - rootDir.set(compositeRoot) - - // Sweep the XDK standard library (lib_*/) AND manualTests/src/main/x/ as - // a unified parse-correctness corpus. The lib_* sources are the canonical - // grammar surface; manualTests/ holds end-to-end test files that exercise - // language features the standard library does not -- service async-call - // shapes, multi-return destructuring, package-import resource providers, - // etc. Both must parse cleanly for the grammar to be considered correct, - // and including manualTests/ here means CI catches regressions on those - // shapes without needing per-file unit tests for everything. The native - // bridge (javatools_bridge/) and the compatibility kit (tck/) are swept - // too. Archived examples (archive/ directories) and build output are not. - val xdkLibDirs = compositeRoot.listFiles { f -> - f.isDirectory && f.name.startsWith("lib_") - }?.toList() ?: emptyList() - val manualTestsDir = File(compositeRoot, "manualTests/src/main/x").takeIf { it.isDirectory } - val otherSourceDirs = listOf("javatools_bridge", "tck").map { File(compositeRoot, it) }.filter { it.isDirectory } - libDirs.set(xdkLibDirs + otherSourceDirs + listOfNotNull(manualTestsDir)) - - // Skip list of .x files deliberately excluded from the parse sweep. - // Two companion files: `intentional.txt` for files with genuine syntax - // errors that the reference compiler also rejects, and `parked.txt` for - // genuine grammar gaps that we want to fix. Either may be absent; both - // are loaded when present. - skipListFiles.from( - layout.projectDirectory.file("treeSitterParseSkipList.intentional.txt"), - layout.projectDirectory.file("treeSitterParseSkipList.parked.txt"), - ) +val testTreeSitterParse = + tasks.register("testTreeSitterParse") { + group = "tree-sitter" + description = + "Test tree-sitter parsing on XDK library files. Use -PtestFiles=pattern to filter. Timing shown by default; use -PshowTiming=false to disable." + dependsOn(validateTreeSitterGrammar) + enabled = treeSitterPlatformSupported + + // Use XdkPropertiesService to find composite root (works for included builds) + val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) + + cliPath.set(treeSitterCliExe) + // NOTE: Setting workDir to the generated directory allows tree-sitter CLI to find the grammar + // via tree-sitter.json in the current directory. This produces harmless warnings about + // "not configured any parser directories" because there's no global ~/.config/tree-sitter/config.json, + // but the CLI still works correctly by discovering the grammar from the working directory. + workDir.set(generatedDir.map { it.asFile }) + buildDirPath.set(layout.buildDirectory.map { it.asFile.absolutePath }) + rootDir.set(compositeRoot) + + // Sweep the XDK standard library (lib_*/) AND manualTests/src/main/x/ as + // a unified parse-correctness corpus. The lib_* sources are the canonical + // grammar surface; manualTests/ holds end-to-end test files that exercise + // language features the standard library does not -- service async-call + // shapes, multi-return destructuring, package-import resource providers, + // etc. Both must parse cleanly for the grammar to be considered correct, + // and including manualTests/ here means CI catches regressions on those + // shapes without needing per-file unit tests for everything. The native + // bridge (javatools_bridge/) and the compatibility kit (tck/) are swept + // too. Archived examples (archive/ directories) and build output are not. + val xdkLibDirs = + compositeRoot + .listFiles { f -> + f.isDirectory && f.name.startsWith("lib_") + }?.toList() ?: emptyList() + val manualTestsDir = File(compositeRoot, "manualTests/src/main/x").takeIf { it.isDirectory } + val otherSourceDirs = listOf("javatools_bridge", "tck").map { File(compositeRoot, it) }.filter { it.isDirectory } + libDirs.set(xdkLibDirs + otherSourceDirs + listOfNotNull(manualTestsDir)) + + // Skip list of .x files deliberately excluded from the parse sweep. + // Two companion files: `intentional.txt` for files with genuine syntax + // errors that the reference compiler also rejects, and `parked.txt` for + // genuine grammar gaps that we want to fix. Either may be absent; both + // are loaded when present. + skipListFiles.from( + layout.projectDirectory.file("treeSitterParseSkipList.intentional.txt"), + layout.projectDirectory.file("treeSitterParseSkipList.parked.txt"), + ) - // Support filtering via -PtestFiles=pattern (comma-separated patterns) - fileFilter.set(providers.gradleProperty("testFiles")) - showTiming.set(providers.gradleProperty("showTiming").map { it.toBoolean() }) -} + // Support filtering via -PtestFiles=pattern (comma-separated patterns) + fileFilter.set(providers.gradleProperty("testFiles")) + showTiming.set(providers.gradleProperty("showTiming").map { it.toBoolean() }) + } // ============================================================================= // Zig Cross-Compilation @@ -815,79 +881,89 @@ val testTreeSitterParse = tasks.register("testTreeSitte /** * Task to cross-compile native library using Zig. */ -abstract class ZigCrossCompileTask @Inject constructor( - private val execOps: ExecOperations -) : DefaultTask() { - - @get:InputFile - @get:PathSensitive(PathSensitivity.NONE) - abstract val zigCompiler: RegularFileProperty - - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val zigLibraries: DirectoryProperty - - @get:Input - val compilerArguments: List get() = NativeLibraryCommands.compilerFlags - - @get:Input - abstract val targetTriple: Property // e.g., "aarch64-macos" - - @get:Input - abstract val targetPlatform: Property // e.g., "darwin-arm64" - - @get:InputFile - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val parserC: RegularFileProperty - - @get:InputFile - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val scannerC: RegularFileProperty - - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val includeDir: DirectoryProperty - - @get:OutputFile - abstract val outputLib: RegularFileProperty - - @TaskAction - fun compile() { - val outputFile = outputLib.get().asFile - outputFile.parentFile.mkdirs() - - val platform = targetPlatform.get() - val triple = targetTriple.get() - logger.info("[zig] Starting cross-compile for $platform ($triple)...") +abstract class ZigCrossCompileTask + @Inject + constructor( + private val execOps: ExecOperations, + ) : DefaultTask() { + @get:InputFile + @get:PathSensitive(PathSensitivity.NONE) + abstract val zigCompiler: RegularFileProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val zigLibraries: DirectoryProperty + + @get:Input + val compilerArguments: List get() = NativeLibraryCommands.compilerFlags + + @get:Input + abstract val targetTriple: Property // e.g., "aarch64-macos" + + @get:Input + abstract val targetPlatform: Property // e.g., "darwin-arm64" + + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val parserC: RegularFileProperty + + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val scannerC: RegularFileProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val includeDir: DirectoryProperty + + @get:OutputFile + abstract val outputLib: RegularFileProperty + + @TaskAction + fun compile() { + val outputFile = outputLib.get().asFile + outputFile.parentFile.mkdirs() + + val platform = targetPlatform.get() + val triple = targetTriple.get() + logger.info("[zig] Starting cross-compile for $platform ($triple)...") + + val duration = + measureTime { + execOps.exec { + executable(zigCompiler.get().asFile) + args( + NativeLibraryCommands.grammar( + triple, + includeDir.get().asFile, + parserC.get().asFile, + scannerC.get().asFile, + outputFile, + ), + ) + } + } - val duration = measureTime { - execOps.exec { - executable(zigCompiler.get().asFile) - args(NativeLibraryCommands.grammar( - triple, includeDir.get().asFile, parserC.get().asFile, scannerC.get().asFile, outputFile - )) - } + logger.info("[zig] Finished $platform in $duration -> ${outputFile.name}") } - - logger.info("[zig] Finished $platform in $duration -> ${outputFile.name}") } -} // Cross-compilation target mapping -val crossCompileTargets = mapOf( - "darwin-arm64" to "aarch64-macos", - "darwin-x64" to "x86_64-macos", - "linux-x64" to "x86_64-linux-gnu", - "linux-arm64" to "aarch64-linux-gnu", - "windows-x64" to "x86_64-windows-gnu" -) +val crossCompileTargets = + mapOf( + "darwin-arm64" to "aarch64-macos", + "darwin-x64" to "x86_64-macos", + "linux-x64" to "x86_64-linux-gnu", + "linux-arm64" to "aarch64-linux-gnu", + "windows-x64" to "x86_64-windows-gnu", + ) // Library extension for each platform -fun libExtForPlatform(platform: String): String = when { - platform.startsWith("darwin") -> "dylib" - platform.startsWith("windows") -> "dll" - else -> "so" -} +fun libExtForPlatform(platform: String): String = + when { + platform.startsWith("darwin") -> "dylib" + platform.startsWith("windows") -> "dll" + else -> "so" + } // Register cross-compile tasks for XTC grammar library crossCompileTargets.forEach { (platform, zigTarget) -> @@ -920,60 +996,62 @@ crossCompileTargets.forEach { (platform, zigTarget) -> /** * Task to cross-compile the tree-sitter runtime library using Zig. */ -abstract class ZigBuildRuntimeTask @Inject constructor( - private val execOps: ExecOperations -) : DefaultTask() { +abstract class ZigBuildRuntimeTask + @Inject + constructor( + private val execOps: ExecOperations, + ) : DefaultTask() { + @get:InputFile + @get:PathSensitive(PathSensitivity.NONE) + abstract val zigCompiler: RegularFileProperty - @get:InputFile - @get:PathSensitive(PathSensitivity.NONE) - abstract val zigCompiler: RegularFileProperty + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val zigLibraries: DirectoryProperty - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val zigLibraries: DirectoryProperty + @get:Input + val compilerArguments: List get() = NativeLibraryCommands.compilerFlags - @get:Input - val compilerArguments: List get() = NativeLibraryCommands.compilerFlags + @get:Input + abstract val targetTriple: Property - @get:Input - abstract val targetTriple: Property + @get:Input + abstract val targetPlatform: Property - @get:Input - abstract val targetPlatform: Property + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val libSrcDir: DirectoryProperty - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val libSrcDir: DirectoryProperty + @get:OutputFile + abstract val outputLib: RegularFileProperty - @get:OutputFile - abstract val outputLib: RegularFileProperty + @TaskAction + fun compile() { + val outputFile = outputLib.get().asFile + outputFile.parentFile.mkdirs() - @TaskAction - fun compile() { - val outputFile = outputLib.get().asFile - outputFile.parentFile.mkdirs() + val platform = targetPlatform.get() + val triple = targetTriple.get() + val libSrc = libSrcDir.get().asFile - val platform = targetPlatform.get() - val triple = targetTriple.get() - val libSrc = libSrcDir.get().asFile + logger.info("[zig] Building tree-sitter runtime for $platform ($triple)...") - logger.info("[zig] Building tree-sitter runtime for $platform ($triple)...") + val libC = File(libSrc, "src/lib.c") + if (!libC.exists()) { + throw GradleException("tree-sitter lib.c not found at: ${libC.absolutePath}") + } - val libC = File(libSrc, "src/lib.c") - if (!libC.exists()) { - throw GradleException("tree-sitter lib.c not found at: ${libC.absolutePath}") - } + val duration = + measureTime { + execOps.exec { + executable(zigCompiler.get().asFile) + args(NativeLibraryCommands.runtime(triple, libSrc, outputFile)) + } + } - val duration = measureTime { - execOps.exec { - executable(zigCompiler.get().asFile) - args(NativeLibraryCommands.runtime(triple, libSrc, outputFile)) - } + logger.info("[zig] Finished tree-sitter runtime $platform in $duration -> ${outputFile.name}") } - - logger.info("[zig] Finished tree-sitter runtime $platform in $duration -> ${outputFile.name}") } -} // Register cross-compile tasks for tree-sitter runtime library crossCompileTargets.forEach { (platform, zigTarget) -> @@ -998,74 +1076,83 @@ crossCompileTargets.forEach { (platform, zigTarget) -> /** * Build all native libraries (grammar + runtime) for all platforms using Zig. */ -val buildAllNativeLibraries = tasks.register("buildAllNativeLibraries") { - group = "tree-sitter" - description = "Build grammar and runtime libraries for all platforms using Zig" - enabled = zigPlatformSupported - - crossCompileTargets.keys.forEach { platform -> - val safePlatform = platform.replace("-", "_") - dependsOn("buildNativeLibrary_$safePlatform") - dependsOn("buildTreeSitterRuntime_$safePlatform") +val buildAllNativeLibraries = + tasks.register("buildAllNativeLibraries") { + group = "tree-sitter" + description = "Build grammar and runtime libraries for all platforms using Zig" + enabled = zigPlatformSupported + + crossCompileTargets.keys.forEach { platform -> + val safePlatform = platform.replace("-", "_") + dependsOn("buildNativeLibrary_$safePlatform") + dependsOn("buildTreeSitterRuntime_$safePlatform") + } } -} /** * Populate the persistent cache with native libraries for all platforms. * This is useful for CI to warm the cache, or for developers building all platforms locally. * Libraries are stored in ~/.gradle/caches/tree-sitter-xtc/// */ -val populateNativeLibraryCache = tasks.register("populateNativeLibraryCache") { - group = "tree-sitter" - description = "Build and cache native libraries for all platforms" - dependsOn(buildAllNativeLibraries) - - // Capture providers and values at configuration time (not script references) - val crossBuildDir = layout.buildDirectory.dir("native-cross") - val grammarFileValue = grammarJsFile.map { it.asFile } - val scannerFileValue = scannerCFile.map { it.asFile } - val parserDirValue = generatedDir.map { it.dir("src").asFile } - val runtimeDirValue = treeSitterLibSrc.map { it.asFile } - val compilerValue = file(zigExe) - val compilerLibrariesValue = compilerValue.parentFile.resolve("lib") - val targets = crossCompileTargets - val cliVersionValue = treeSitterCliVersion - val zigVersionValue = zigVersion - val cacheDirValue = nativeLibCacheDir - // Capture the map with pre-computed extensions to avoid script function reference - val platformsWithExtensions = crossCompileTargets.keys.associateWith { libExtForPlatform(it) } - - inputs.dir(crossBuildDir) - inputs.files(grammarFileValue, scannerFileValue, compilerValue).withPathSensitivity(PathSensitivity.NONE) - inputs.files(parserDirValue, runtimeDirValue, compilerLibrariesValue).withPathSensitivity(PathSensitivity.RELATIVE) - inputs.property("cliVersion", cliVersionValue) - inputs.property("zigVersion", zigVersionValue) - inputs.property("targets", targets) - inputs.property("extensions", platformsWithExtensions) - inputs.property("compilerArguments", NativeLibraryCommands.compilerFlags) - - doLastTask { - val nativeInputs = NativeLibraryInputs( - cliVersionValue, zigVersionValue, compilerValue, compilerLibrariesValue, - grammarFileValue.get(), scannerFileValue.get(), parserDirValue.get(), runtimeDirValue.get() - ) - platformsWithExtensions.forEach { (platform, ext) -> - val hash = nativeInputs.fingerprint(platform, targets.getValue(platform), ext) - - val cacheDir = File(cacheDirValue, "$hash/$platform") - val sourceDir = crossBuildDir.get().dir(platform).asFile - NativeLibraryCache.getOrBuild(cacheDir, ext) { grammar, runtime -> - File(sourceDir, "libtree-sitter-xtc.$ext").copyTo(grammar) - File(sourceDir, "libtree-sitter.$ext").copyTo(runtime) +val populateNativeLibraryCache = + tasks.register("populateNativeLibraryCache") { + group = "tree-sitter" + description = "Build and cache native libraries for all platforms" + dependsOn(buildAllNativeLibraries) + + // Capture providers and values at configuration time (not script references) + val crossBuildDir = layout.buildDirectory.dir("native-cross") + val grammarFileValue = grammarJsFile.map { it.asFile } + val scannerFileValue = scannerCFile.map { it.asFile } + val parserDirValue = generatedDir.map { it.dir("src").asFile } + val runtimeDirValue = treeSitterLibSrc.map { it.asFile } + val compilerValue = file(zigExe) + val compilerLibrariesValue = compilerValue.parentFile.resolve("lib") + val targets = crossCompileTargets + val cliVersionValue = treeSitterCliVersion + val zigVersionValue = zigVersion + val cacheDirValue = nativeLibCacheDir + // Capture the map with pre-computed extensions to avoid script function reference + val platformsWithExtensions = crossCompileTargets.keys.associateWith { libExtForPlatform(it) } + + inputs.dir(crossBuildDir) + inputs.files(grammarFileValue, scannerFileValue, compilerValue).withPathSensitivity(PathSensitivity.NONE) + inputs.files(parserDirValue, runtimeDirValue, compilerLibrariesValue).withPathSensitivity(PathSensitivity.RELATIVE) + inputs.property("cliVersion", cliVersionValue) + inputs.property("zigVersion", zigVersionValue) + inputs.property("targets", targets) + inputs.property("extensions", platformsWithExtensions) + inputs.property("compilerArguments", NativeLibraryCommands.compilerFlags) + + doLastTask { + val nativeInputs = + NativeLibraryInputs( + cliVersionValue, + zigVersionValue, + compilerValue, + compilerLibrariesValue, + grammarFileValue.get(), + scannerFileValue.get(), + parserDirValue.get(), + runtimeDirValue.get(), + ) + platformsWithExtensions.forEach { (platform, ext) -> + val hash = nativeInputs.fingerprint(platform, targets.getValue(platform), ext) + + val cacheDir = File(cacheDirValue, "$hash/$platform") + val sourceDir = crossBuildDir.get().dir(platform).asFile + NativeLibraryCache.getOrBuild(cacheDir, ext) { grammar, runtime -> + File(sourceDir, "libtree-sitter-xtc.$ext").copyTo(grammar) + File(sourceDir, "libtree-sitter.$ext").copyTo(runtime) + } + logger.info("Cached native libraries: $platform (hash: ${hash.take(12)}...)") } - logger.info("Cached native libraries: $platform (hash: ${hash.take(12)}...)") - } - logger.info("") - logger.info("Native libraries cached in: ${cacheDirValue.absolutePath}") - logger.info("Cache will be used automatically on subsequent builds.") + logger.info("") + logger.info("Native libraries cached in: ${cacheDirValue.absolutePath}") + logger.info("Cache will be used automatically on subsequent builds.") + } } -} // ============================================================================= // Multi-Platform Native Library Build with Caching @@ -1077,126 +1164,136 @@ val populateNativeLibraryCache = tasks.register("populateNativeLibraryCache") { * Task that builds native libraries for ALL platforms using Zig cross-compilation. * Libraries are cached in ~/.gradle/caches/tree-sitter-xtc/// */ -abstract class BuildAllNativeLibrariesOnDemandTask @Inject constructor( - private val execOps: ExecOperations -) : DefaultTask() { - - @get:InputFile - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val grammarFile: RegularFileProperty - - @get:InputFile - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val scannerFile: RegularFileProperty - - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val parserSrcDir: DirectoryProperty - - @get:Input - abstract val cliVersion: Property - - @get:Input - abstract val zigVersion: Property - - @get:InputFile - @get:PathSensitive(PathSensitivity.NONE) - abstract val zigCompiler: RegularFileProperty - - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val zigLibraries: DirectoryProperty - - @get:InputDirectory - @get:PathSensitive(PathSensitivity.RELATIVE) - abstract val runtimeSources: DirectoryProperty - - @get:Input - val compilerArguments: List get() = NativeLibraryCommands.compilerFlags - - @get:Input - abstract val cacheDir: Property - - @get:Input - abstract val platforms: MapProperty // platform -> zigTarget - - @get:Input - abstract val platformExtensions: MapProperty // platform -> extension - - @get:OutputDirectory - abstract val outputDir: DirectoryProperty - - @TaskAction - fun execute() { - val platformMap = platforms.get() - val extMap = platformExtensions.get() - val cacheRoot = File(cacheDir.get()) - val outDir = outputDir.get().asFile - val zigPath = zigCompiler.get().asFile - val treeSitterLibSrc = runtimeSources.get().asFile - val parserC = File(parserSrcDir.get().asFile, "parser.c") - val scannerC = scannerFile.get().asFile - val includeDir = parserSrcDir.get().asFile - val nativeInputs = NativeLibraryInputs( - cliVersion.get(), zigVersion.get(), zigPath, zigLibraries.get().asFile, - grammarFile.get().asFile, scannerC, includeDir, treeSitterLibSrc - ) +abstract class BuildAllNativeLibrariesOnDemandTask + @Inject + constructor( + private val execOps: ExecOperations, + ) : DefaultTask() { + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val grammarFile: RegularFileProperty + + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val scannerFile: RegularFileProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val parserSrcDir: DirectoryProperty + + @get:Input + abstract val cliVersion: Property + + @get:Input + abstract val zigVersion: Property + + @get:InputFile + @get:PathSensitive(PathSensitivity.NONE) + abstract val zigCompiler: RegularFileProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val zigLibraries: DirectoryProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val runtimeSources: DirectoryProperty + + @get:Input + val compilerArguments: List get() = NativeLibraryCommands.compilerFlags + + @get:Input + abstract val cacheDir: Property + + @get:Input + abstract val platforms: MapProperty // platform -> zigTarget + + @get:Input + abstract val platformExtensions: MapProperty // platform -> extension + + @get:OutputDirectory + abstract val outputDir: DirectoryProperty + + @TaskAction + fun execute() { + val platformMap = platforms.get() + val extMap = platformExtensions.get() + val cacheRoot = File(cacheDir.get()) + val outDir = outputDir.get().asFile + val zigPath = zigCompiler.get().asFile + val treeSitterLibSrc = runtimeSources.get().asFile + val parserC = File(parserSrcDir.get().asFile, "parser.c") + val scannerC = scannerFile.get().asFile + val includeDir = parserSrcDir.get().asFile + val nativeInputs = + NativeLibraryInputs( + cliVersion.get(), + zigVersion.get(), + zigPath, + zigLibraries.get().asFile, + grammarFile.get().asFile, + scannerC, + includeDir, + treeSitterLibSrc, + ) - logger.info( - """ + logger.info( + """ |[zig] ========== NATIVE LIBRARY CROSS-COMPILATION ========== |[zig] Platforms: ${platformMap.keys.joinToString(", ")} |[zig] Zig compiler: $zigPath |[zig] Cache dir: ${cacheRoot.absolutePath} - """.trimMargin() - ) - - var builtCount = 0 - - val totalDuration = measureTime { - platformMap.forEach { (platform, zigTarget) -> - val ext = extMap[platform] ?: error("No extension for platform: $platform") - val hash = nativeInputs.fingerprint(platform, zigTarget, ext) - val cachedDir = File(cacheRoot, "$hash/$platform") - val cachedGrammarLib = File(cachedDir, "libtree-sitter-xtc.$ext") - val cachedRuntimeLib = File(cachedDir, "libtree-sitter.$ext") - - val platformOutDir = File(outDir, platform) - platformOutDir.mkdirs() - val grammarOutput = File(platformOutDir, "libtree-sitter-xtc.$ext") - val runtimeOutput = File(platformOutDir, "libtree-sitter.$ext") + """.trimMargin(), + ) - NativeLibraryCache.getOrBuild(cachedDir, ext) { grammar, runtime -> - logger.info("[zig] $platform: BUILDING with Zig ($zigTarget)...") - val duration = measureTime { - execOps.exec { - executable(zigPath) - args(NativeLibraryCommands.grammar(zigTarget, includeDir, parserC, scannerC, grammar)) - } - execOps.exec { - executable(zigPath) - args(NativeLibraryCommands.runtime(zigTarget, treeSitterLibSrc, runtime)) + var builtCount = 0 + + val totalDuration = + measureTime { + platformMap.forEach { (platform, zigTarget) -> + val ext = extMap[platform] ?: error("No extension for platform: $platform") + val hash = nativeInputs.fingerprint(platform, zigTarget, ext) + val cachedDir = File(cacheRoot, "$hash/$platform") + val cachedGrammarLib = File(cachedDir, "libtree-sitter-xtc.$ext") + val cachedRuntimeLib = File(cachedDir, "libtree-sitter.$ext") + + val platformOutDir = File(outDir, platform) + platformOutDir.mkdirs() + val grammarOutput = File(platformOutDir, "libtree-sitter-xtc.$ext") + val runtimeOutput = File(platformOutDir, "libtree-sitter.$ext") + + NativeLibraryCache.getOrBuild(cachedDir, ext) { grammar, runtime -> + logger.info("[zig] $platform: BUILDING with Zig ($zigTarget)...") + val duration = + measureTime { + execOps.exec { + executable(zigPath) + args(NativeLibraryCommands.grammar(zigTarget, includeDir, parserC, scannerC, grammar)) + } + execOps.exec { + executable(zigPath) + args(NativeLibraryCommands.runtime(zigTarget, treeSitterLibSrc, runtime)) + } + } + logger.info("[zig] $platform: compiled in $duration") + builtCount++ } + + // Copy to output + cachedGrammarLib.copyTo(grammarOutput, overwrite = true) + cachedRuntimeLib.copyTo(runtimeOutput, overwrite = true) } - logger.info("[zig] $platform: compiled in $duration") - builtCount++ } - // Copy to output - cachedGrammarLib.copyTo(grammarOutput, overwrite = true) - cachedRuntimeLib.copyTo(runtimeOutput, overwrite = true) - } - } - - logger.info( - """ + logger.info( + """ |[zig] ========================================================== |[zig] Result: $builtCount built, ${platformMap.size - builtCount} from cache, total: $totalDuration |[zig] ========================================================== - """.trimMargin() - ) + """.trimMargin(), + ) + } } -} // Output directory for all platform libraries val nativeLibOutputDir: Provider = layout.buildDirectory.dir("native-out") @@ -1205,25 +1302,26 @@ val nativeLibOutputDir: Provider = layout.buildDirectory.dir("native- * Build native libraries for ALL platforms on-demand with caching. * This is the task that consumers should depend on. */ -val buildAllNativeLibrariesOnDemand = tasks.register("buildAllNativeLibrariesOnDemand") { - group = "tree-sitter" - description = "Build native libraries for all platforms (cached in ~/.gradle/caches/)" - dependsOn(copyGrammarFiles, extractZig, extractTreeSitterSource, validateTreeSitterGrammar) - enabled = zigPlatformSupported - - grammarFile.set(grammarJsFile) - scannerFile.set(scannerCFile) - parserSrcDir.set(generatedDir.map { it.dir("src") }) - cliVersion.set(treeSitterCliVersion) - this.zigVersion.set(libs.versions.lang.zig) - zigCompiler.set(file(zigExe)) - zigLibraries.set(file(zigExe).parentFile.resolve("lib")) - runtimeSources.set(treeSitterLibSrc) - cacheDir.set(nativeLibCacheDir.absolutePath) - platforms.set(crossCompileTargets) - platformExtensions.set(crossCompileTargets.keys.associateWith { libExtForPlatform(it) }) - outputDir.set(nativeLibOutputDir) -} +val buildAllNativeLibrariesOnDemand = + tasks.register("buildAllNativeLibrariesOnDemand") { + group = "tree-sitter" + description = "Build native libraries for all platforms (cached in ~/.gradle/caches/)" + dependsOn(copyGrammarFiles, extractZig, extractTreeSitterSource, validateTreeSitterGrammar) + enabled = zigPlatformSupported + + grammarFile.set(grammarJsFile) + scannerFile.set(scannerCFile) + parserSrcDir.set(generatedDir.map { it.dir("src") }) + cliVersion.set(treeSitterCliVersion) + this.zigVersion.set(libs.versions.lang.zig) + zigCompiler.set(file(zigExe)) + zigLibraries.set(file(zigExe).parentFile.resolve("lib")) + runtimeSources.set(treeSitterLibSrc) + cacheDir.set(nativeLibCacheDir.absolutePath) + platforms.set(crossCompileTargets) + platformExtensions.set(crossCompileTargets.keys.associateWith { libExtForPlatform(it) }) + outputDir.set(nativeLibOutputDir) + } // ============================================================================= // Consumable Configuration for LSP Server @@ -1233,17 +1331,18 @@ val buildAllNativeLibrariesOnDemand = tasks.register-/`; active files are additional to archive +caps, active processes are protected and stopped sessions are pruned on the next server start. +Legacy shared logs and the IDE's own protocol console are outside this policy. + +**Ecstasy: Export Server Logs** saves a ZIP of recent log tails and current status from +the connected server (at most eight 512 KiB tails, 4 MiB log input). It may contain local paths and +logged diagnostics; it does not collect source files. When stopped, export uses only this project’s +last recorded launch, including up to 64 KiB of launcher output. Its manifest identifies offline +mode and truncation and lists included files; another project’s logs are never substituted. Shared +X269–X272 cover settings, restart, live/offline export and recovery from an intentional failed JVM +launch. Full editor exit/reopen has a separate two-process persistence test. Native OS save-dialog +layout and rollover stress remain manual checks. See the [acceptance report format](../doc/manual-test-plan.md#acceptance-reports) for +how these checks are recorded. + +### Test artifact retention + +Gradle editor test tasks finish by pruning disposable report payloads. The five newest completed +runs retain workspaces and diagnostic logs; older runs keep compact result files. Change the count +with `-PplaybookRetainedRuns=N`, or pin a run with a `.keep-artifacts` file in its report directory. +Unfinished runs are preserved for investigation. Cleanup never follows workspace symlinks or +removes Git worktrees. Direct npm runs record completion too; the next Gradle run performs pruning, +or run `:lang:vscode-extension:pruneCompilerPlaybookReports` explicitly with both lang flags. +The IDE download cache remains shared across runs and is not deleted by retention. + +The UI completion gate also runs `node scripts/run-vscode-tests.cjs --settings-persistence` +after building the extension. Two complete editor launches share one disposable profile/workspace +and verify persisted settings and user/workspace precedence. The native Settings UI groups +Compiler, Language Service, Formatting, and Runtime and Logs. The effective status command +reports saved origins, ignored scope overrides and connection changes awaiting restart. +Untrusted and virtual workspaces are explicitly unsupported; remote hosts remain unvalidated. + +### Experimental color-value picker + +The opt-in L77 prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared +fixture. It supplies native color swatches and picker edits separately from normal semantic +highlighting. It is disabled for ordinary launches and does not introduce a public XDK color API. +Shared X273–X275 exercise picker edits, dismissal, native Undo/Redo and diagnostic recovery. +See the [prototype recipe and acceptance](../doc/manual-test-plan.md#l77-color-value-prototype). + +The playbook keeps its startup theme. For explicit theme comparisons, the Node/npm launcher accepts +`--playbook --cases=X277 --theme="Default Light Modern"` and the optional `--lexical-baseline`. +It writes settings before opening VS Code and exports rendered lexical styles for comparison; +there are no live theme/highlighting toggles. See the +[highlighting checks](../doc/manual-test-plan.md#theme-based-highlighting-follow-up-x276x277). + +### Compiler documentation and reference counts + +The compiler adapter offers **Generate documentation comment** on written declaration headers. +It preserves indentation and line endings, derives method parameter/return entries from the +compiler signature, and leaves existing comments intact. The edit participates in normal Undo/Redo. + +Reference CodeLens shows source type, method and property usage counts across the complete configured +source graph, including unopened consumers. Counts exclude declarations and are omitted while the +graph cannot be resolved. Clicking a count opens the editor's native references view. External +consumers outside the configured graph are not counted. Run lenses remain independent. + +Set `xtc.codeLens.references` (default `true`) to toggle counts live. The native `editor.codeLens` +setting still controls whether VS Code renders lenses. diff --git a/lang/vscode-extension/build.gradle.kts b/lang/vscode-extension/build.gradle.kts index 862d02c466d..7c8238d8fdd 100644 --- a/lang/vscode-extension/build.gradle.kts +++ b/lang/vscode-extension/build.gradle.kts @@ -18,7 +18,10 @@ plugins { } node { - version.set(libs.versions.lang.node.asProvider()) + version.set( + libs.versions.lang.node + .asProvider(), + ) download.set(true) } @@ -29,168 +32,166 @@ tasks.withType().configureEach { inputs.property("nodeVersion", node.ve tasks.withType().configureEach { inputs.property("nodeVersion", node.version) } // Configuration to consume TextMate grammar from root project -val textMateGrammar = configurations.create("textMateGrammar") { - isCanBeConsumed = false - isCanBeResolved = true - attributes { - attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION)) - attribute(Usage.USAGE_ATTRIBUTE, objects.named("textmate-grammar")) +val textMateGrammar = + configurations.create("textMateGrammar") { + isCanBeConsumed = false + isCanBeResolved = true + attributes { + attribute(Category.CATEGORY_ATTRIBUTE, objects.named(Category.DOCUMENTATION)) + attribute(Usage.USAGE_ATTRIBUTE, objects.named("textmate-grammar")) + } } -} dependencies { textMateGrammar(project(path = ":dsl", configuration = "textMateElements")) } // Copy TextMate grammar files -val copyTextMateGrammar = tasks.register("copyTextMateGrammar") { - description = "Copy TextMate grammar from generated output" - from(textMateGrammar) { - include("xtc.tmLanguage.json") +val copyTextMateGrammar = + tasks.register("copyTextMateGrammar") { + description = "Copy TextMate grammar from generated output" + from(textMateGrammar) { + include("xtc.tmLanguage.json") + } + into(layout.projectDirectory.dir("syntaxes")) } - into(layout.projectDirectory.dir("syntaxes")) -} // Copy language configuration -val copyLanguageConfig = tasks.register("copyLanguageConfig") { - description = "Copy language configuration from generated output" - val languageConfig = textMateGrammar.asFileTree.matching { - include("language-configuration.json") +val copyLanguageConfig = + tasks.register("copyLanguageConfig") { + description = "Copy language configuration from generated output" + val languageConfig = + textMateGrammar.asFileTree.matching { + include("language-configuration.json") + } + sourceFile.set(layout.file(languageConfig.elements.map { it.single().asFile })) + outputFile.set(layout.projectDirectory.file("language-configuration.json")) } - sourceFile.set(layout.file(languageConfig.elements.map { it.single().asFile })) - outputFile.set(layout.projectDirectory.file("language-configuration.json")) -} // Copy LSP server fat JAR (self-contained with all dependencies: LSP4J, tree-sitter, Logback) -val copyLspServer = tasks.register("copyLspServer") { - description = "Copy LSP server fat JAR" - sourceFile.set(project(":lsp-server").tasks.named("fatJar").flatMap { it.archiveFile }) - outputFile.set(layout.projectDirectory.file("server/lsp-server.jar")) -} +val copyLspServer = + tasks.register("copyLspServer") { + description = "Copy LSP server fat JAR" + sourceFile.set(project(":lsp-server").tasks.named("fatJar").flatMap { it.archiveFile }) + outputFile.set(layout.projectDirectory.file("server/lsp-server.jar")) + } // Copy DAP server JAR (bundled for debugging support) -val copyDapServer = tasks.register("copyDapServer") { - description = "Copy DAP server JAR" - sourceFile.set(project(":dap-server").tasks.named("jar").flatMap { it.archiveFile }) - outputFile.set(layout.projectDirectory.file("server/dap-server.jar")) -} +val copyDapServer = + tasks.register("copyDapServer") { + description = "Copy DAP server JAR" + sourceFile.set(project(":dap-server").tasks.named("jar").flatMap { it.archiveFile }) + outputFile.set(layout.projectDirectory.file("server/dap-server.jar")) + } // Copy LICENSE from repository root -val copyLicense = tasks.register("copyLicense") { - description = "Copy LICENSE from repository root" - val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) - sourceFile.set(File(compositeRoot, "LICENSE.md")) - outputFile.set(layout.projectDirectory.file("LICENSE.md")) -} +val copyLicense = + tasks.register("copyLicense") { + description = "Copy LICENSE from repository root" + val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) + sourceFile.set(File(compositeRoot, "LICENSE.md")) + outputFile.set(layout.projectDirectory.file("LICENSE.md")) + } // Generate the marketplace icon (xtc.png, 256x256) and the language file icon // (xtc-file.png, 32x32) from doc/logo/x.jpg in the repository root. We derive // PNGs rather than checking them in so the single JPEG source of truth in // doc/logo/ stays canonical; the `sharp` devDependency handles the conversion. -val generateIcons = tasks.register("generateIcons") { - description = "Generate VS Code marketplace and file icons from doc/logo/x.jpg" - dependsOn(tasks.named("npmInstall")) - val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) - val sourceLogo = File(compositeRoot, "doc/logo/x.jpg") - val outDir = layout.projectDirectory.dir("icons") - val scriptFile = layout.projectDirectory.file("scripts/generate-icons.cjs") - script.set(scriptFile.asFile) - args.set(listOf(sourceLogo.absolutePath, outDir.asFile.absolutePath)) - inputs.file(sourceLogo) - inputs.file(scriptFile) - outputs.file(outDir.file("xtc.png")) - outputs.file(outDir.file("xtc-file.png")) -} +val generateIcons = + tasks.register("generateIcons") { + description = "Generate VS Code marketplace and file icons from doc/logo/x.jpg" + dependsOn(tasks.named("npmInstall")) + val compositeRoot = XdkPropertiesService.compositeRootDirectory(projectDir) + val sourceLogo = File(compositeRoot, "doc/logo/x.jpg") + val outDir = layout.projectDirectory.dir("icons") + val scriptFile = layout.projectDirectory.file("scripts/generate-icons.cjs") + script.set(scriptFile.asFile) + args.set(listOf(sourceLogo.absolutePath, outDir.asFile.absolutePath)) + inputs.file(sourceLogo) + inputs.file(scriptFile) + outputs.file(outDir.file("xtc.png")) + outputs.file(outDir.file("xtc-file.png")) + } // Configure the plugin-provided npmInstall task (runs `npm install` using the pinned Node) -val npmInstall = tasks.named("npmInstall") { - mustRunAfter(copyLanguageConfig, copyTextMateGrammar, copyLicense) -} +val npmInstall = + tasks.named("npmInstall") { + mustRunAfter(copyLanguageConfig, copyTextMateGrammar, copyLicense) + } // Compile TypeScript -val npmCompile = tasks.register("npmCompile") { - description = "Compile TypeScript" - dependsOn(npmInstall) - args.set(listOf("run", "compile")) +val copyCompilerModelImport = + tasks.register("copyCompilerModelImport") { + from(rootProject.layout.projectDirectory.file("gradle/compiler-model.init.gradle")) + into(layout.projectDirectory.dir("resources")) + } - inputs.dir(layout.projectDirectory.dir("src")) - inputs.file(layout.projectDirectory.file("tsconfig.json")) - inputs.files(layout.projectDirectory.file("package.json"), layout.projectDirectory.file("package-lock.json")) - outputs.dir(layout.projectDirectory.dir("out")) -} +val npmCompile = + tasks.register("npmCompile") { + description = "Compile TypeScript" + dependsOn(npmInstall, copyCompilerModelImport) + args.set(listOf("run", "compile")) + + inputs.dir(layout.projectDirectory.dir("src")) + inputs.file(layout.projectDirectory.file("tsconfig.json")) + inputs.files(layout.projectDirectory.file("package.json"), layout.projectDirectory.file("package-lock.json")) + inputs.file(rootProject.layout.projectDirectory.file("test-fixtures/compiler-playbook/scenarios.json")) + outputs.dir(layout.projectDirectory.dir("out")) + } // Bundle separately from tsc's development output so packaging never rewrites npmCompile's files. val bundledExtension = layout.buildDirectory.file("bundle/extension.js") -val npmBundle = tasks.register("npmBundle") { - description = "Bundle extension runtime dependencies for packaging" - dependsOn(npmInstall) - val scriptFile = layout.projectDirectory.file("scripts/bundle.cjs") - script.set(scriptFile.asFile) - args.set(bundledExtension.map { listOf(it.asFile.absolutePath) }) - inputs.dir(layout.projectDirectory.dir("src")) - inputs.file(scriptFile) - inputs.files(layout.projectDirectory.file("package.json"), layout.projectDirectory.file("package-lock.json")) - outputs.file(bundledExtension) -} +val npmBundle = + tasks.register("npmBundle") { + description = "Bundle extension runtime dependencies for packaging" + dependsOn(npmInstall) + val scriptFile = layout.projectDirectory.file("scripts/bundle.cjs") + script.set(scriptFile.asFile) + args.set(bundledExtension.map { listOf(it.asFile.absolutePath) }) + inputs.dir(layout.projectDirectory.dir("src")) + inputs.file(scriptFile) + inputs.files(layout.projectDirectory.file("package.json"), layout.projectDirectory.file("package-lock.json")) + outputs.file(bundledExtension) + } // A suffix affects only the staged manifest and resulting artifact, never the source package.json. val vscodeVersionSuffix = providers.gradleProperty("vscode.version.suffix").orElse("") -val extensionVersion = providers.fileContents(layout.projectDirectory.file("package.json")).asText - .zip(vscodeVersionSuffix) { content, suffix -> - val manifest = JsonSlurper().parseText(content) as Map<*, *> - val version = manifest["version"] as String - require(suffix.isEmpty() || Regex("[0-9A-Za-z.-]+").matches(suffix)) { - "vscode.version.suffix must contain only letters, digits, dots and hyphens" - } - if (suffix.isEmpty()) version else "${version.substringBefore("-")}-$suffix" - } - -// The Marketplace takes only major.minor.patch, and every upload needs a new version, so a Marketplace -// pre-release replaces the patch with a build number, such as CI's run number. Like the suffix, this -// changes only the staged manifest and resulting artifact. -val marketplaceBuild = providers.gradleProperty("vscode.marketplace.build").orElse("") -val marketplaceVersion = providers.fileContents(layout.projectDirectory.file("package.json")).asText - .zip(marketplaceBuild) { content, build -> - require(build.isNotEmpty()) { "A Marketplace package requires -Pvscode.marketplace.build=" } - val version = (JsonSlurper().parseText(content) as Map<*, *>)["version"] as String - val (major, minor, patch) = requireNotNull(Regex("(\\d+)\\.(\\d+)\\.(\\d+)").matchEntire(version)) { - "The package.json version $version is not major.minor.patch" - }.destructured - val number = build.toIntOrNull() - // above the patch, so that a pre-release never takes the version of a release - require(number != null && number > patch.toInt()) { - "vscode.marketplace.build must be an integer above $patch, the patch of $version; was $build" +val extensionVersion = + providers + .fileContents(layout.projectDirectory.file("package.json")) + .asText + .zip(vscodeVersionSuffix) { content, suffix -> + val manifest = JsonSlurper().parseText(content) as Map<*, *> + val version = manifest["version"] as String + require(suffix.isEmpty() || Regex("[0-9A-Za-z.-]+").matches(suffix)) { + "vscode.version.suffix must contain only letters, digits, dots and hyphens" + } + if (suffix.isEmpty()) version else "${version.substringBefore("-")}-$suffix" } - "$major.$minor.$number" - } - -// vsce resolves the README's relative links against the repository root, but the README is in this -// directory, so links such as ../doc/manual-test-plan.md need it as their base -val readmeBaseUrls = listOf( - "--baseContentUrl", "https://github.com/xtclang/xvm/blob/master/lang/vscode-extension", - "--baseImagesUrl", "https://github.com/xtclang/xvm/raw/master/lang/vscode-extension", -) -fun registerStagePackage(name: String, directory: Provider, version: Provider) = - tasks.register(name) { +val packageDirectory = layout.buildDirectory.dir("package") +val stagePackage = + tasks.register("stagePackage") { description = "Stage extension files and versioned metadata without modifying sources" dependsOn(npmCompile) from(layout.projectDirectory) { include("package.json", ".vscodeignore", "README.md", "snippets/**") } from(copyLicense, copyLanguageConfig) + from(copyCompilerModelImport) { into("resources") } from(copyTextMateGrammar) { into("syntaxes") } from(listOf(copyLspServer, copyDapServer)) { into("server") } from(generateIcons) { into("icons") } from(npmBundle) { into("out") } - into(directory) + into(packageDirectory) - val stagedManifest = directory.map { it.file("package.json") } - inputs.property("extensionVersion", version) + val stagedManifest = packageDirectory.map { it.file("package.json") } + val packagedVersion = extensionVersion + inputs.property("extensionVersion", packagedVersion) doLast { val manifestFile = stagedManifest.get().asFile val manifest = (JsonSlurper().parse(manifestFile) as Map<*, *>).toMutableMap() - manifest["version"] = version.get() + manifest["version"] = packagedVersion.get() // Gradle already built the icons and bundle; vsce must not run the source-tree npm hook. val scripts = manifest["scripts"] as? Map<*, *> if (scripts != null) manifest["scripts"] = scripts.filterKeys { it != "vscode:prepublish" } @@ -198,44 +199,25 @@ fun registerStagePackage(name: String, directory: Provider, version: } } -fun registerPackage( - name: String, - taskDescription: String, - stage: TaskProvider, - archive: Provider, - vsceOptions: List, -) = tasks.register(name) { - description = taskDescription - dependsOn(npmInstall) - script.set(layout.projectDirectory.file("node_modules/@vscode/vsce/vsce")) - workingDir.set(stage.flatMap { it.destinationDirectory }) - args.set(archive.map { listOf("package", "--no-dependencies") + vsceOptions + listOf("--out", it.asFile.absolutePath) }) - inputs.files(stage) - inputs.file(layout.projectDirectory.file("package-lock.json")) - outputs.file(archive) - doFirst { - archive.get().asFile.parentFile.mkdirs() - } -} - -val packageDirectory = layout.buildDirectory.dir("package") -val stagePackage = registerStagePackage("stagePackage", packageDirectory, extensionVersion) val extensionArchive = layout.buildDirectory.file(extensionVersion.map { "distributions/xtc-language-$it.vsix" }) -val packageExtension = registerPackage( - "packageExtension", "Package the staged VS Code extension", stagePackage, extensionArchive, readmeBaseUrls, -) - -// A package of its own, so the extension's VSIX keeps the extension's version -val marketplacePackageDirectory = layout.buildDirectory.dir("marketplace/package") -val stageMarketplacePackage = registerStagePackage("stageMarketplacePackage", marketplacePackageDirectory, marketplaceVersion) -val marketplaceArchive = layout.buildDirectory.file(marketplaceVersion.map { "marketplace/xtc-language-$it.vsix" }) -val packageMarketplaceExtension = registerPackage( - "packageMarketplaceExtension", - "Package the staged VS Code extension as a Marketplace pre-release (requires -Pvscode.marketplace.build)", - stageMarketplacePackage, - marketplaceArchive, - readmeBaseUrls + "--pre-release", -) +val packageExtension = + tasks.register("packageExtension") { + description = "Package the staged VS Code extension" + dependsOn(npmInstall) + script.set(layout.projectDirectory.file("node_modules/@vscode/vsce/vsce")) + workingDir.set(stagePackage.flatMap { it.destinationDirectory }) + args.set(extensionArchive.map { listOf("package", "--no-dependencies", "--out", it.asFile.absolutePath) }) + inputs.files(stagePackage) + inputs.file(layout.projectDirectory.file("package-lock.json")) + outputs.file(extensionArchive) + val archive = extensionArchive + doFirst { + archive + .get() + .asFile.parentFile + .mkdirs() + } + } // Headless integration test: launches VS Code via @vscode/test-electron with // the extension loaded from the build tree, opens src/test/fixtures/hello.x, @@ -247,55 +229,110 @@ val packageMarketplaceExtension = registerPackage( // Not wired into `check` by default because on headless Linux runners this // needs `xvfb-run` (or a similar virtual display). The intent is that local // developers run it explicitly, and CI opt-in via xvfb if/when desired. -val testVscodeExtension = tasks.register("testVscodeExtension") { +val pruneTestReports = + listOf("compiler-playbook", "extension-tests", "project-lifecycle", "explorer-probe").map { report -> + tasks.register("prune${report.split('-').joinToString("") { it.replaceFirstChar(Char::uppercase) }}Reports") { + group = "verification" + description = "Retain five completed $report payloads and all compact results" + reportsDirectory.set(layout.buildDirectory.dir("reports/$report")) + retainedRuns.set(providers.gradleProperty("playbookRetainedRuns").map(String::toInt).orElse(5)) + } + } + +val testVscodeExtension = + tasks.register("testVscodeExtension") { + group = "verification" + description = "Run headless integration tests for the VS Code extension" + dependsOn(npmCompile, copyTextMateGrammar, copyLanguageConfig, copyLspServer, copyDapServer, copyLicense, generateIcons) + args.set(listOf("run", "test:vscode")) + finalizedBy(pruneTestReports) + // Cache directory used by @vscode/test-electron to keep the downloaded + // VS Code build across runs; declared as input so a corrupted cache + // can be cleared by `./gradlew :lang:vscode-extension:clean`. + inputs.dir(layout.projectDirectory.dir("src/test")) + } + +// Runs the compiler playbook inside VS Code and the host/protocol checks referenced by it. +tasks.register("testCompilerPlaybook") { group = "verification" - description = "Run headless integration tests for the VS Code extension" - dependsOn(npmCompile, copyTextMateGrammar, copyLanguageConfig, copyLspServer, copyDapServer, copyLicense, generateIcons) - args.set(listOf("run", "test:vscode")) - // Cache directory used by @vscode/test-electron to keep the downloaded - // VS Code build across runs; declared as input so a corrupted cache - // can be cleared by `./gradlew :lang:vscode-extension:clean`. + description = "Run XdkAdapter playbook in VS Code" + finalizedBy(pruneTestReports) + dependsOn(":lsp-server:prepareLibraryPlaybook") + val selectedCases = providers.gradleProperty("compilerPlaybookCases") + val multiRoot = + providers.gradleProperty("compilerPlaybookMultiRoot").map(String::toBoolean).orElse(false) + val cancelUi = providers.gradleProperty("compilerPlaybookCancelUi").map(String::toBoolean).orElse(false) + dependsOn("assemble") + if (!selectedCases.isPresent) { + dependsOn(":lsp-server:test", ":lsp-server:compilerStdioTest") + } + args.set( + selectedCases + .orElse("") + .zip(multiRoot) { cases, multipleRoots -> + listOf("run", "test:playbook", "--") + + listOfNotNull( + cases.takeIf(String::isNotEmpty)?.let { "--cases=$it" }, + "--multi-root".takeIf { multipleRoots }, + ) + }.zip(cancelUi) { arguments, visibleCancel -> arguments + listOfNotNull("--cancel-ui".takeIf { visibleCancel }) }, + ) inputs.dir(layout.projectDirectory.dir("src/test")) + inputs.file(layout.projectDirectory.file("../doc/manual-test-plan.md")) + inputs.file(rootProject.layout.projectDirectory.file("test-fixtures/compiler-playbook/scenarios.json")) + // Test execution is intentional on every invocation; reports are retained per run. } // Main build task - configure the existing task from base plugin -val build = tasks.named("build") { - dependsOn(packageExtension) -} +val build = + tasks.named("build") { + dependsOn(packageExtension) + } // Assemble prepares all resources without packaging -val assemble = tasks.named("assemble") { - dependsOn(copyTextMateGrammar, copyLanguageConfig, copyLspServer, copyDapServer, copyLicense, npmCompile, generateIcons) -} +val assemble = + tasks.named("assemble") { + dependsOn(copyTextMateGrammar, copyLanguageConfig, copyLspServer, copyDapServer, copyLicense, npmCompile, generateIcons) + } // Launch VS Code with extension loaded for testing -val runCode = tasks.register("runCode") { - group = "run" - description = "Launch VS Code with the extension loaded for testing" - dependsOn(assemble) - - val extensionPath = layout.projectDirectory.asFile.absolutePath - val fixturesPath = layout.projectDirectory.dir("src/test/fixtures").asFile.absolutePath - // Capture PATH + OS at config time so the doFirst stays CC-safe (no - // System.getenv / System.getProperty calls inside the task action). - val pathEnv = providers.environmentVariable("PATH").orElse("").get() - val isWindows = providers.systemProperty("os.name").get().lowercase().contains("windows") - val candidateBinaries = if (isWindows) listOf("code.cmd", "code.exe") else listOf("code") - - commandLine("code", "--extensionDevelopmentPath=$extensionPath", fixturesPath) - - // Preflight: fail fast with a useful message if `code` isn't on PATH, - // rather than letting Exec emit a cryptic `exit code 127`. The CLI is - // an optional VS Code install step ("Shell Command: Install 'code' - // command in PATH") that surprises developers who installed VS Code - // via the .app/.dmg without running that command palette action. - doFirst { - val found = pathEnv.split(File.pathSeparator).any { dir -> - candidateBinaries.any { name -> File(dir, name).canExecute() } - } - if (!found) { - throw GradleException( - """ +val runCode = + tasks.register("runCode") { + group = "run" + description = "Launch VS Code with the extension loaded for testing" + dependsOn(assemble) + + val extensionPath = layout.projectDirectory.asFile.absolutePath + val fixturesPath = + layout.projectDirectory + .dir("src/test/fixtures") + .asFile.absolutePath + // Capture PATH + OS at config time so the doFirst stays CC-safe (no + // System.getenv / System.getProperty calls inside the task action). + val pathEnv = providers.environmentVariable("PATH").orElse("").get() + val isWindows = + providers + .systemProperty("os.name") + .get() + .lowercase() + .contains("windows") + val candidateBinaries = if (isWindows) listOf("code.cmd", "code.exe") else listOf("code") + + commandLine("code", "--extensionDevelopmentPath=$extensionPath", fixturesPath) + + // Preflight: fail fast with a useful message if `code` isn't on PATH, + // rather than letting Exec emit a cryptic `exit code 127`. The CLI is + // an optional VS Code install step ("Shell Command: Install 'code' + // command in PATH") that surprises developers who installed VS Code + // via the .app/.dmg without running that command palette action. + doFirst { + val found = + pathEnv.split(File.pathSeparator).any { dir -> + candidateBinaries.any { name -> File(dir, name).canExecute() } + } + if (!found) { + throw GradleException( + """ |The `code` CLI is not on PATH, so this task cannot launch VS Code. | |How to fix: open VS Code, press Cmd+Shift+P (macOS) or Ctrl+Shift+P @@ -306,20 +343,21 @@ val runCode = tasks.register("runCode") { |Alternative that needs no PATH change: open lang/vscode-extension/ |in VS Code and press F5 — that launches the Extension Development Host |directly from the IDE, equivalent to what this task does from the CLI. - """.trimMargin(), - ) + """.trimMargin(), + ) + } } } -} -val clean = tasks.named("clean") { - delete(layout.projectDirectory.dir("out")) - delete(layout.projectDirectory.dir("node_modules")) - delete(layout.projectDirectory.dir("server")) - delete(layout.projectDirectory.file("syntaxes/xtc.tmLanguage.json")) - delete(layout.projectDirectory.file("language-configuration.json")) - delete(layout.projectDirectory.file("icons/xtc.png")) - delete(layout.projectDirectory.file("icons/xtc-file.png")) - delete(layout.projectDirectory.dir(".vscode-test")) - delete(fileTree(layout.projectDirectory) { include("*.vsix") }) -} +val clean = + tasks.named("clean") { + delete(layout.projectDirectory.dir("out")) + delete(layout.projectDirectory.dir("node_modules")) + delete(layout.projectDirectory.dir("server")) + delete(layout.projectDirectory.file("syntaxes/xtc.tmLanguage.json")) + delete(layout.projectDirectory.file("language-configuration.json")) + delete(layout.projectDirectory.file("icons/xtc.png")) + delete(layout.projectDirectory.file("icons/xtc-file.png")) + delete(layout.projectDirectory.dir(".vscode-test")) + delete(fileTree(layout.projectDirectory) { include("*.vsix") }) + } diff --git a/lang/vscode-extension/package-lock.json b/lang/vscode-extension/package-lock.json index 385df5358c5..63684642677 100644 --- a/lang/vscode-extension/package-lock.json +++ b/lang/vscode-extension/package-lock.json @@ -10,26 +10,32 @@ "license": "Apache-2.0", "dependencies": { "jdk-utils": "^0.7.0", - "vscode-languageclient": "^11.0.0" + "jsonc-parser": "^3.3.1", + "vscode-languageclient": "^11.0.0", + "yazl": "^3.3.1" }, "devDependencies": { "@eslint/js": "^10.0.1", "@types/glob": "^8.1.0", "@types/mocha": "^10.0.10", - "@types/node": "~22.15.35", - "@types/vscode": "1.101.0", + "@types/node": "^24.0.0", + "@types/vscode": "1.140.0", + "@types/yauzl": "^3.4.0", + "@types/yazl": "^3.3.1", "@vscode/test-electron": "^3.1.0", "@vscode/vsce": "^4.1.0", "esbuild": "^0.28.2", "eslint": "^10.14.0", "glob": "^13.0.7", "mocha": "^12.0.3", + "playwright-core": "^1.63.0", "sharp": "^0.35.5", "typescript": "^6.0.3", - "typescript-eslint": "^8.72.2" + "typescript-eslint": "^8.72.2", + "yauzl": "^3.4.0" }, "engines": { - "vscode": "^1.101.0" + "vscode": "^1.140.0" } }, "node_modules/@azure/abort-controller": { @@ -1845,22 +1851,42 @@ "license": "MIT" }, "node_modules/@types/node": { - "version": "22.15.35", - "resolved": "https://registry.npmjs.org/@types/node/-/node-22.15.35.tgz", - "integrity": "sha512-stV91mHxlWpDksiUiivmFfQzjy2JLlb2NUTxKipiANEbxBZsdbDU9fSrT7SHY4uoCXAxYfJZVasn3x2/hqpd3g==", + "version": "24.19.1", + "resolved": "https://registry.npmjs.org/@types/node/-/node-24.19.1.tgz", + "integrity": "sha512-aS3/DG0oM05K0RIXXP+hKjinGG5IgSSVGzswZxW3O0sS3pH4/fycXundUC9XsszgKCk4gHXylTEK6hyFxVxnoQ==", "dev": true, "license": "MIT", "dependencies": { - "undici-types": "~6.21.0" + "undici-types": ">=7.24.0 <7.24.7" } }, "node_modules/@types/vscode": { - "version": "1.101.0", - "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.101.0.tgz", - "integrity": "sha512-ZWf0IWa+NGegdW3iU42AcDTFHWW7fApLdkdnBqwYEtHVIBGbTu0ZNQKP/kX3Ds/uMJXIMQNAojHR4vexCEEz5Q==", + "version": "1.140.0", + "resolved": "https://registry.npmjs.org/@types/vscode/-/vscode-1.140.0.tgz", + "integrity": "sha512-KAB5RBZIgnUhpp4mKsVJPPHLOQKu6WtGy2xpfWjaajEXO4xIIC+UESAXLiQ9ewx6QD1uBmvC/S8BJzkbvv4bRA==", "dev": true, "license": "MIT" }, + "node_modules/@types/yauzl": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-3.4.0.tgz", + "integrity": "sha512-NRPn5w6h8dhcnmx3YIRQcqMywY/+nND/uOkJessedcrowO3C0AssHp3tMJpxKAwOhFOo0OV1y9VtsC5hbKKBAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, + "node_modules/@types/yazl": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/@types/yazl/-/yazl-3.3.1.tgz", + "integrity": "sha512-DIWfCKpsTp6hE5BDBHV3+fIL/bLUF9Bv13iDrWnMlmhQpH67buNvI291ZauQ1xcccxK3FqQ9honnXpq4R8NMuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "8.72.2", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.72.2.tgz", @@ -2309,6 +2335,26 @@ "win32" ] }, + "node_modules/@vscode/vsce/node_modules/buffer-crc32": { + "version": "0.2.13", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", + "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + } + }, + "node_modules/@vscode/vsce/node_modules/yazl": { + "version": "2.5.1", + "resolved": "https://registry.npmjs.org/yazl/-/yazl-2.5.1.tgz", + "integrity": "sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==", + "dev": true, + "license": "MIT", + "dependencies": { + "buffer-crc32": "~0.2.3" + } + }, "node_modules/acorn": { "version": "8.17.0", "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.17.0.tgz", @@ -2425,13 +2471,12 @@ "license": "ISC" }, "node_modules/buffer-crc32": { - "version": "0.2.13", - "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", - "integrity": "sha512-VO9Ht/+p3SN7SKWqcrgEzjGbRSJYTx+Q1pTQC0wrWqHx0vpJraQ6GtHx8tvcg1rlK1byhU5gccxgOgj7B0TDkQ==", - "dev": true, + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-1.0.0.tgz", + "integrity": "sha512-Db1SbgBS/fg/392AblrMJk97KggmvYhr4pB5ZIMTWtaivCPMWLkmb7m21cJvpvgK+J3nsU2CmmixNBZx4vFj/w==", "license": "MIT", "engines": { - "node": "*" + "node": ">=8.0.0" } }, "node_modules/buffer-equal-constant-time": { @@ -3511,7 +3556,6 @@ "version": "3.3.1", "resolved": "https://registry.npmjs.org/jsonc-parser/-/jsonc-parser-3.3.1.tgz", "integrity": "sha512-HUgH65KyejrUFPvHFPbqOY0rsFip3Bo5wb4ngvdi1EpCYWUQDC5V+Y7mZws+DLkr4M//zQJoanu1SP+87Dv1oQ==", - "dev": true, "license": "MIT" }, "node_modules/jsonwebtoken": { @@ -4144,6 +4188,19 @@ "dev": true, "license": "ISC" }, + "node_modules/playwright-core": { + "version": "1.63.0", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.63.0.tgz", + "integrity": "sha512-rYCsBF/M5HjUch52bbtVONEFjv6Xu8sm8h72dNlR5bzIE1fvC/bxgspzkjSfU+MweEMmPM8KJebG6nnyxo5mCg==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + }, "node_modules/prelude-ls": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/prelude-ls/-/prelude-ls-1.2.1.tgz", @@ -4733,9 +4790,9 @@ "license": "MIT" }, "node_modules/undici-types": { - "version": "6.21.0", - "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", - "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", + "version": "7.24.6", + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-7.24.6.tgz", + "integrity": "sha512-WRNW+sJgj5OBN4/0JpHFqtqzhpbnV0GuB+OozA9gCL7a993SmU+1JBZCzLNxYsbMfIeDL+lTsphD5jN5N+n0zg==", "dev": true, "license": "MIT" }, @@ -4916,13 +4973,12 @@ } }, "node_modules/yazl": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/yazl/-/yazl-2.5.1.tgz", - "integrity": "sha512-phENi2PLiHnHb6QBVot+dJnaAZ0xosj7p3fWl+znIjBDlnMI2PsZCJZ306BPTFOaHf5qdDEI8x5qFrSOBN5vrw==", - "dev": true, + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/yazl/-/yazl-3.3.1.tgz", + "integrity": "sha512-BbETDVWG+VcMUle37k5Fqp//7SDOK2/1+T7X8TD96M3D9G8jK5VLUdQVdVjGi8im7FGkazX7kk5hkU8X4L5Bng==", "license": "MIT", "dependencies": { - "buffer-crc32": "~0.2.3" + "buffer-crc32": "^1.0.0" } }, "node_modules/yocto-queue": { diff --git a/lang/vscode-extension/package.json b/lang/vscode-extension/package.json index fea2aa5fb37..166e132dce4 100644 --- a/lang/vscode-extension/package.json +++ b/lang/vscode-extension/package.json @@ -11,7 +11,7 @@ "url": "https://github.com/xtclang/xvm" }, "engines": { - "vscode": "^1.101.0" + "vscode": "^1.140.0" }, "categories": [ "Programming Languages", @@ -71,6 +71,22 @@ } ], "commands": [ + { + "command": "xtc.configureCompilerPaths", + "title": "Ecstasy: Configure Compiler Paths" + }, + { + "command": "xtc.showCompilerPaths", + "title": "Ecstasy: Show Effective Compiler Paths" + }, + { + "command": "xtc.refreshCompilerBuild", + "title": "Ecstasy: Refresh Gradle Compiler Model" + }, + { + "command": "xtc.prepareCompilerInputs", + "title": "Ecstasy: Prepare Generated Compiler Resources" + }, { "command": "xtc.createProject", "title": "Ecstasy: Create New Project" @@ -83,9 +99,51 @@ "command": "xtc.restartServer", "title": "Ecstasy: Restart Language Server" }, + { + "command": "xtc.selectLanguageAdapter", + "title": "Ecstasy: Switch Language Adapter" + }, { "command": "xtc.showServerOutput", "title": "Ecstasy: Show Language Server Output" + }, + { + "command": "xtc.hideServerOutput", + "title": "Ecstasy: Hide Language Server Output" + }, + { + "command": "xtc.openLanguageSettings", + "title": "Ecstasy: Open Language Service Settings" + }, + { + "command": "xtc.showLanguageServiceStatus", + "title": "Ecstasy: Show Effective Language Service Configuration" + }, + { + "command": "xtc.configureCompilerLibraries", + "title": "Configure Libraries and Sources", + "category": "Ecstasy" + }, + { + "command": "xtc.exportServerLogs", + "title": "Export Server Logs", + "category": "Ecstasy" + } + ], + "keybindings": [ + { + "command": "xtc.selectLanguageAdapter", + "key": "ctrl+alt+x a", + "when": "editorLangId == xtc" + }, + { + "command": "xtc.showServerOutput", + "key": "ctrl+alt+x l" + }, + { + "command": "xtc.hideServerOutput", + "key": "ctrl+alt+x l", + "when": "view.workbench.panel.output.visible && activeOutputChannel =~ /xtclang\\.xtc-language\\.Ecstasy Language Server(?:\\.\\d+)?\\.log$/" } ], "taskDefinitions": [ @@ -120,66 +178,293 @@ } } ], - "configuration": { - "title": "Ecstasy", - "properties": { - "xtc.java.home": { - "type": "string", - "default": "", - "description": "Path to Java 25+ installation (overrides JAVA_HOME). Leave empty to use JAVA_HOME or 'java' in PATH.", - "scope": "machine-overridable" - }, - "xtc.trace.server": { - "type": "string", - "enum": [ - "off", - "messages", - "verbose" - ], - "default": "off", - "description": "Trace communication between VS Code and the XTC Language Server (for debugging)" - }, - "xtc.inlayHints.enabled": { - "type": "boolean", - "default": true, - "description": "Enable inlay hints from the XTC Language Server" - }, - "xtc.formatting.indentSize": { - "type": "integer", - "default": 4, - "description": "Number of spaces per indentation level" - }, - "xtc.formatting.continuationIndentSize": { - "type": "integer", - "default": 8, - "description": "Number of spaces for continuation lines" - }, - "xtc.formatting.tabSize": { - "type": "integer", - "default": 4, - "description": "Tab width in spaces" - }, - "xtc.formatting.insertSpaces": { - "type": "boolean", - "default": true, - "description": "Use spaces instead of tabs" - }, - "xtc.formatting.maxLineWidth": { - "type": "integer", - "default": 120, - "description": "Maximum line width (right margin)" - }, - "xtc.sourceRoots": { - "type": "array", - "items": { - "type": "string" + "configuration": [ + { + "title": "Ecstasy: Compiler", + "order": 0, + "properties": { + "xtc.compiler.sourceModules": { + "type": [ + "array", + "null" + ], + "default": null, + "scope": "window", + "description": "Compiler source graph: null discovers workspace modules; [] disables discovery; an array supplies explicit roots and edges. Changes apply without restarting. Relative URIs require a single workspace folder.", + "items": { + "type": "object", + "required": [ + "name", + "uri" + ], + "additionalProperties": false, + "properties": { + "name": { + "type": "string", + "minLength": 1, + "description": "Declared Ecstasy module name." + }, + "uri": { + "type": "string", + "minLength": 1, + "description": "Module .x root: a file URI or URI relative to the workspace folder." + }, + "dependencies": { + "type": "array", + "items": { + "type": "string" + }, + "uniqueItems": true, + "default": [], + "description": "Names of source modules this module imports." + }, + "resourceRoots": { + "type": [ + "array", + "null" + ], + "items": { + "type": "string", + "minLength": 1 + }, + "uniqueItems": true, + "default": null, + "description": "Ordered resource root file URIs or workspace-relative paths. Omitted/null uses compiler layout deduction; [] disables resources. Explicit roots replace the defaults. Gradle model imports use evaluated processed resources unless an explicit graph overrides them." + } + } + } + }, + "xtc.sourceRoots": { + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "description": "Extra source root directories the Ecstasy Language Server indexes (e.g. XDK standard-library sources). Accepts absolute paths to directories that contain .x source files outside the open workspace. Window-wide; folder overrides are not used. Changes restart the server and restore open buffers. Compiler dependencies and read-only navigation sources belong in xtc.compiler.libraries.", + "scope": "machine-overridable" + }, + "xtc.compiler.libraries": { + "type": "object", + "scope": "window", + "default": { + "modulePath": null, + "sourceAttachments": [] + }, + "additionalProperties": false, + "description": "Ordered external compiler libraries and matching read-only source attachments. Workspace-owned; use absolute file URIs in multi-root workspaces. Apply without restarting. Bundled XDK is always included.", + "properties": { + "modulePath": { + "type": [ + "array", + "null" + ], + "default": null, + "items": { + "type": "string" + }, + "description": "Null inherits Gradle binary inputs; [] removes external libraries. Entries are directories or .xtc files; first module wins. Relative paths require one workspace folder." + }, + "sourceAttachments": { + "type": "array", + "default": [], + "items": { + "type": "object", + "required": [ + "module", + "roots" + ], + "additionalProperties": false, + "properties": { + "module": { + "type": "string", + "description": "Exact external binary module name." + }, + "roots": { + "type": "array", + "minItems": 1, + "items": { + "type": "string" + }, + "description": "Ordered matching source directories, used only for read-only navigation." + } + } + } + } + } + } + } + }, + { + "title": "Ecstasy: Language Service", + "order": 1, + "properties": { + "xtc.languageService.adapter": { + "type": "string", + "enum": ["default", "compiler", "treesitter"], + "enumItemLabels": ["Bundled default", "Ecstasy Compiler", "Tree-sitter"], + "enumDescriptions": [ + "Use the adapter chosen by this server build (normally Compiler).", + "Compiler diagnostics, type resolution and semantic features.", + "Syntax-based analysis without compiler type checking." + ], + "default": "default", + "scope": "window", + "description": "Language server adapter. Changes restart the server and restore open buffers. Also available through Ecstasy: Switch Language Adapter." + }, + "xtc.trace.server": { + "type": "string", + "enum": [ + "off", + "messages", + "verbose" + ], + "default": "off", + "description": "Trace communication between VS Code and the Ecstasy Language Server (for debugging)", + "scope": "window" + }, + "xtc.codeLens.references": { + "type": "boolean", + "default": true, + "description": "Show compiler reference counts for declarations in the configured source graph. Applies immediately; Run lenses are independent. Native editor.codeLens controls also apply.", + "scope": "resource" + }, + "xtc.inlayHints.enabled": { + "type": "boolean", + "default": true, + "description": "Show Ecstasy inlay hints. Applies immediately; the native editor.inlayHints.enabled preference also applies.", + "scope": "resource" + }, + "xtc.languageService.textSynchronization": { + "type": "string", + "enum": [ + "full", + "incremental" + ], + "enumDescriptions": [ + "Send the full document after changes (default).", + "Send changed text ranges. This does not enable incremental compilation." + ], + "default": "full", + "scope": "window", + "description": "Text transport to the language server. Changing this automatically restarts the server and resynchronizes all open buffers. It does not enable incremental compilation." + }, + "xtc.languageService.saveFormatting": { + "type": "string", + "enum": [ + "editor", + "server" + ], + "enumDescriptions": [ + "Use the native editor.formatOnSave preference.", + "Request server save edits when native format-on-save is disabled." + ], + "default": "editor", + "scope": "window", + "description": "Owner of save formatting. Native format-on-save takes precedence to prevent duplicate edits. Changing this automatically restarts the server and resynchronizes open buffers." + } + } + }, + { + "title": "Ecstasy: Formatting", + "order": 2, + "properties": { + "xtc.formatting.indentSize": { + "type": "integer", + "default": 4, + "description": "Number of spaces per indentation level", + "scope": "window", + "minimum": 1, + "maximum": 32 + }, + "xtc.formatting.continuationIndentSize": { + "type": "integer", + "default": 8, + "description": "Number of spaces for continuation lines", + "scope": "window", + "minimum": 1, + "maximum": 32 }, - "default": [], - "description": "Extra source root directories the XTC Language Server indexes (e.g. XDK standard-library sources). Accepts absolute paths to directories that contain .x source files outside the open workspace.", - "scope": "machine-overridable" + "xtc.formatting.tabSize": { + "type": "integer", + "default": 4, + "description": "Tab width in spaces", + "scope": "window", + "minimum": 1, + "maximum": 32, + "deprecationMessage": "This legacy value does not affect the formatter. Use editor.tabSize for visual tab width." + }, + "xtc.formatting.insertSpaces": { + "type": "boolean", + "default": true, + "description": "Use spaces instead of tabs", + "scope": "window" + }, + "xtc.formatting.maxLineWidth": { + "type": "integer", + "default": 120, + "description": "Compiler adapter target width for expression and list wrapping at safe token boundaries; literals and comments are not split.", + "scope": "window", + "minimum": 1, + "maximum": 1000 + } + } + }, + { + "title": "Ecstasy: Runtime and Logs", + "order": 3, + "properties": { + "xtc.java.home": { + "type": "string", + "default": "", + "description": "Java 25+ installation for this language-server connection. Leave empty for JDK discovery or a cached/downloaded runtime. Invalid explicit paths are rejected. Changing this restarts the server and restores open buffers; folder overrides are not used.", + "scope": "machine-overridable" + }, + "xtc.java.vmOptions": { + "type": "array", + "items": { + "type": "string" + }, + "default": [], + "scope": "machine", + "description": "Advanced LSP JVM options, one argument per item. Supports -Xms/-Xmx/-Xss sizes (K/M/G), MaxMetaspaceSize, ReservedCodeCacheSize, ActiveProcessorCount and G1/Serial/Parallel/Z collectors. Restart Language Server to apply. Workspace overrides are ignored." + }, + "xtc.server.logs": { + "type": "object", + "scope": "machine", + "default": { + "historyDays": 7, + "maxFileMb": 10, + "totalSizeMb": 50, + "retainedSessions": 5 + }, + "additionalProperties": false, + "properties": { + "historyDays": { + "type": "integer", + "minimum": 1, + "maximum": 90 + }, + "maxFileMb": { + "type": "integer", + "minimum": 1, + "maximum": 100 + }, + "totalSizeMb": { + "type": "integer", + "minimum": 1, + "maximum": 1000 + }, + "retainedSessions": { + "type": "integer", + "minimum": 1, + "maximum": 20 + } + }, + "description": "Machine-local server log retention. Restart to apply. Caps apply separately to server and timing archives; current files are additional. Closed sessions are pruned on server start; active processes are protected. Archive cap must be at least the per-file cap." + } } } - }, + ], "configurationDefaults": { "files.associations": { "*.x": "xtc" @@ -253,27 +538,34 @@ "watch": "node scripts/bundle.cjs out/extension.js --watch", "package": "vsce package", "lint": "eslint src/", - "test:vscode": "node ./scripts/run-vscode-tests.cjs" + "test:vscode": "node ./scripts/run-vscode-tests.cjs", + "test:playbook": "node ./scripts/run-vscode-tests.cjs --playbook" }, "dependencies": { "jdk-utils": "^0.7.0", - "vscode-languageclient": "^11.0.0" + "jsonc-parser": "^3.3.1", + "vscode-languageclient": "^11.0.0", + "yazl": "^3.3.1" }, "devDependencies": { "@eslint/js": "^10.0.1", "@types/glob": "^8.1.0", "@types/mocha": "^10.0.10", - "@types/node": "~22.15.35", - "@types/vscode": "1.101.0", + "@types/node": "^24.0.0", + "@types/vscode": "1.140.0", + "@types/yauzl": "^3.4.0", + "@types/yazl": "^3.3.1", "@vscode/test-electron": "^3.1.0", "@vscode/vsce": "^4.1.0", "esbuild": "^0.28.2", "eslint": "^10.14.0", "glob": "^13.0.7", "mocha": "^12.0.3", + "playwright-core": "^1.63.0", "sharp": "^0.35.5", "typescript": "^6.0.3", - "typescript-eslint": "^8.72.2" + "typescript-eslint": "^8.72.2", + "yauzl": "^3.4.0" }, "overrides": { "diff": "^8.0.3", @@ -283,5 +575,18 @@ "allowScripts": { "esbuild": true, "@vscode/vsce-sign": true - } + }, + "capabilities": { + "untrustedWorkspaces": { + "supported": false, + "description": "Ecstasy launches Java and can import Gradle builds. Trust the workspace to enable the extension." + }, + "virtualWorkspaces": { + "supported": false, + "description": "The compiler and Gradle import require files available to the extension host filesystem." + } + }, + "extensionKind": [ + "workspace" + ] } diff --git a/lang/vscode-extension/scripts/bundle.cjs b/lang/vscode-extension/scripts/bundle.cjs index 18ff7352f56..1fa7378481a 100644 --- a/lang/vscode-extension/scripts/bundle.cjs +++ b/lang/vscode-extension/scripts/bundle.cjs @@ -4,8 +4,8 @@ // // The extension runs on the Node that ships inside VS Code's Electron, not on the Node that // builds it. package.json states that runtime once: engines.vscode is the oldest supported -// VS Code, and @types/node pins the Node bundled with that release (VS Code 1.101 ships -// Node 22.15). The esbuild target is derived from the @types/node major so they cannot drift. +// VS Code, and @types/node pins the Node major bundled with that release (VS Code 1.140 ships +// Node 24). The esbuild target is derived from the @types/node major so they cannot drift. // // Usage: node scripts/bundle.cjs [--watch] diff --git a/lang/vscode-extension/scripts/run-vscode-tests.cjs b/lang/vscode-extension/scripts/run-vscode-tests.cjs index 51d430060eb..7805bb03977 100644 --- a/lang/vscode-extension/scripts/run-vscode-tests.cjs +++ b/lang/vscode-extension/scripts/run-vscode-tests.cjs @@ -34,12 +34,12 @@ function hasCommand(name) { const needsHeadlessWrapper = process.platform === 'linux' && !process.env.DISPLAY; let launcher = process.execPath; -let launcherArgs = [RUNNER]; +let launcherArgs = [RUNNER, ...process.argv.slice(2)]; if (needsHeadlessWrapper) { if (hasCommand('xvfb-run')) { launcher = 'xvfb-run'; - launcherArgs = ['-a', process.execPath, RUNNER]; + launcherArgs = ['-a', process.execPath, RUNNER, ...process.argv.slice(2)]; console.log('[test:vscode] Linux without DISPLAY detected — wrapping with xvfb-run for headless mode.'); } else { console.warn('[test:vscode] WARNING: running on Linux without DISPLAY and without xvfb-run on PATH.'); diff --git a/lang/vscode-extension/src/adapter-selection.ts b/lang/vscode-extension/src/adapter-selection.ts new file mode 100644 index 00000000000..f0937f98b2c --- /dev/null +++ b/lang/vscode-extension/src/adapter-selection.ts @@ -0,0 +1,23 @@ +import * as vscode from 'vscode'; +import { readServiceSettings } from './editor-settings'; +import { ServiceSettings, parseServiceSettings } from './service-settings'; + +const choices: ReadonlyArray = [ + { adapter: 'compiler', label: 'Ecstasy Compiler', detail: 'Compiler diagnostics, type resolution and semantic features.' }, + { adapter: 'treesitter', label: 'Tree-sitter', detail: 'Syntax-based analysis without compiler type checking.' }, + { adapter: 'default', label: 'Bundled default', detail: 'Use the adapter chosen by this server build (normally Compiler).' }, +]; + +/** Persist only the window's adapter choice; the configuration listener owns the restart. */ +export async function selectLanguageAdapter(requested?: unknown): Promise { + const current = readServiceSettings().adapter; + const selected = requested === undefined + ? (await vscode.window.showQuickPick(choices.map(choice => ({ + ...choice, description: choice.adapter === current ? 'Selected' : undefined, + })), { title: 'Ecstasy: Switch Language Adapter', placeHolder: 'Selecting a different adapter restarts the language server. Unsaved buffers are preserved.' }))?.adapter + : parseServiceSettings({ adapter: requested }).adapter; + if (selected === undefined || selected === current) return; + const target = vscode.workspace.workspaceFolders?.length + ? vscode.ConfigurationTarget.Workspace : vscode.ConfigurationTarget.Global; + await vscode.workspace.getConfiguration('xtc').update('languageService.adapter', selected, target); +} diff --git a/lang/vscode-extension/src/build-model.ts b/lang/vscode-extension/src/build-model.ts new file mode 100644 index 00000000000..26ab6d01cf9 --- /dev/null +++ b/lang/vscode-extension/src/build-model.ts @@ -0,0 +1,84 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { isDeepStrictEqual } from 'node:util'; + +export interface BuildSourceSet { + projectId: string; + projectPath: string; + projectDirectory: string; + buildFile: string; + sourceSet: string; + sourceRoots: string[]; + sourceFiles: string[]; + moduleRoots: string[]; + resourceSourceRoots: string[]; + resourceRoots: string[]; + resourceTask: string; + projectDependencies: string[]; + modulePath: string[]; +} +export interface BuildModel { schemaVersion: 1; sourceSets: BuildSourceSet[]; buildRoots?: string[]; importId?: string } +export const modelPath = '.gradle/xtc/lsp-model.json'; +export const workspaceModelPath = '.gradle/xtc/lsp-workspace.json'; + +export function mergeBuildModels(models: BuildModel[]): BuildModel { + const entries = new Map(); + for (const entry of models.flatMap(model => model.sourceSets)) { + const key = JSON.stringify([entry.projectId, entry.sourceSet]); + const previous = entries.get(key); + if (previous && !isDeepStrictEqual(previous, entry)) throw new Error('Conflicting Gradle source-set ownership.'); + entries.set(key, entry); + } + const identities = [...new Set(models.flatMap(model => model.importId ? [model.importId] : []))]; + return { schemaVersion: 1, sourceSets: [...entries.values()], buildRoots: [...new Set(models.flatMap(model => model.buildRoots ?? []))], + ...(identities.length === 0 ? {} : { importId: identities.join(',') }) }; +} + +export function readCompilerReport(folder: string): string | undefined { + const report = [workspaceModelPath, modelPath].map(file => path.join(folder, file)).find(file => fs.existsSync(file)); + return report === undefined ? undefined : fs.readFileSync(report, 'utf8'); +} + +/** Read Gradle's evaluated contract; neither host interprets build script text. */ +export function readBuildModel(folder: string): BuildModel | undefined { + const text = readCompilerReport(folder); + return text === undefined ? undefined : parseBuildModel(text); +} + +export function parseBuildModel(text: string): BuildModel { + const model = JSON.parse(text) as BuildModel; + if (model.schemaVersion !== 1 || !Array.isArray(model.sourceSets)) throw new Error('Unsupported Ecstasy Gradle model; refresh build configuration.'); + const owners = new Set(); + if (model.importId !== undefined && typeof model.importId !== 'string') throw new Error('Invalid Gradle import identity.'); + if (model.buildRoots !== undefined && (!Array.isArray(model.buildRoots) || + model.buildRoots.some(uri => typeof uri !== 'string' || new URL(uri).protocol !== 'file:'))) throw new Error('Invalid Gradle build roots.'); + for (const entry of model.sourceSets) { + for (const field of ['projectId', 'projectPath', 'projectDirectory', 'buildFile', 'sourceSet', 'resourceTask'] as const) { + if (typeof entry?.[field] !== 'string' || !entry[field].trim()) throw new Error(`Invalid Gradle ${field}`); + } + const owner = JSON.stringify([entry.projectId, entry.sourceSet]); + if (!entry.projectId || !entry.sourceSet || owners.has(owner)) throw new Error('Invalid or duplicate Gradle source-set owner.'); + owners.add(owner); + for (const field of ['sourceRoots', 'sourceFiles', 'moduleRoots', 'resourceSourceRoots', 'resourceRoots', 'modulePath'] as const) { + if (!Array.isArray(entry[field]) || entry[field].some(uri => typeof uri !== 'string' || new URL(uri).protocol !== 'file:')) throw new Error(`Invalid Gradle ${field}`); + } + if (new URL(entry.buildFile).protocol !== 'file:' || new URL(entry.projectDirectory).protocol !== 'file:') throw new Error('Invalid Gradle project location'); + if (!Array.isArray(entry.projectDependencies) || entry.projectDependencies.some(item => typeof item !== 'string')) throw new Error('Invalid Gradle project dependencies'); + } + return model; +} + +export function describeBuildModel(model: BuildModel): string { + return model.sourceSets.map(entry => { + const paths = (kind: string, values: string[]) => values.map(uri => { + const file = new URL(uri); + return ` ${kind}: ${uri}${fs.existsSync(file) ? '' : ' [missing; prepare generated inputs]'}`; + }); + const missing = entry.resourceRoots.filter(uri => !fs.existsSync(new URL(uri))).length; + return [`${entry.projectPath} / ${entry.sourceSet} [Gradle model]`, ` Build: ${entry.buildFile}`, + ` Processed resources: ${missing === 0 ? 'ready' : `${missing} missing; prepare generated resources`}`, + ...paths('Source root', entry.sourceRoots), ...paths('Resource input', entry.resourceSourceRoots), + ...paths('Processed resource (compiler input)', entry.resourceRoots), ...paths('Binary module path', entry.modulePath), + ` Prepare: ${entry.resourceTask}`].join('\n'); + }).join('\n\n'); +} diff --git a/lang/vscode-extension/src/commands.ts b/lang/vscode-extension/src/commands.ts index 47a6171f263..90262d28f67 100644 --- a/lang/vscode-extension/src/commands.ts +++ b/lang/vscode-extension/src/commands.ts @@ -1,4 +1,5 @@ import * as vscode from 'vscode'; +import type { Location, Position } from 'vscode-languageclient/node'; import { createXtcRunTask, XtcTaskDefinition, XtcTaskProvider } from './task-provider'; @@ -16,6 +17,15 @@ function validateProjectName(value: string): string | null { export function registerCommands(context: vscode.ExtensionContext, outputChannel: vscode.OutputChannel): void { context.subscriptions.push( + vscode.commands.registerCommand('xtc.openLanguageSettings', () => + vscode.commands.executeCommand('workbench.action.openSettings', '@ext:xtclang.xtc-language')), + vscode.commands.registerCommand('xtc.showReferences', (uri: string, at: Position, locations: Location[]) => { + const targets = locations.map(({ uri, range }) => new vscode.Location( + vscode.Uri.parse(uri), new vscode.Range( + range.start.line, range.start.character, range.end.line, range.end.character))); + return vscode.commands.executeCommand('editor.action.showReferences', + vscode.Uri.parse(uri), new vscode.Position(at.line, at.character), targets); + }), vscode.commands.registerCommand('xtc.runModule', async (_uri: string, moduleName: string) => { if (!moduleName) { const input = await vscode.window.showInputBox({ @@ -41,6 +51,10 @@ export function registerCommands(context: vscode.ExtensionContext, outputChannel outputChannel.show(); }), + vscode.commands.registerCommand('xtc.hideServerOutput', () => { + outputChannel.hide(); + }), + vscode.commands.registerCommand('xtc.createProject', async () => { const projectName = await vscode.window.showInputBox({ prompt: 'Enter project name', @@ -73,7 +87,7 @@ export function registerCommands(context: vscode.ExtensionContext, outputChannel } const parentPath = folderUri[0].fsPath; - const terminal = vscode.window.createTerminal('XTC'); + const terminal = vscode.window.createTerminal('Ecstasy'); terminal.show(); terminal.sendText(`xtc init "${projectName}" --type ${projectType.toLowerCase()} --dir "${parentPath}"`); diff --git a/lang/vscode-extension/src/compiler-import.ts b/lang/vscode-extension/src/compiler-import.ts new file mode 100644 index 00000000000..fd1d0890179 --- /dev/null +++ b/lang/vscode-extension/src/compiler-import.ts @@ -0,0 +1,81 @@ +import { randomUUID } from 'node:crypto'; + +/** Import ownership and accepted report contents; no mutable parsed model escapes this owner. */ +export class CompilerImport { + private state: Readonly<{ + accepted?: string; + observed?: Readonly<{ text?: string }>; + operation?: Readonly<{ prepare: boolean; id: string }>; + result?: ImportResult; + retired?: boolean; + }> = {}; + + constructor(private readonly read: () => string | undefined, private readonly validate: (text: string) => unknown, + private readonly importId: (text: string) => string | undefined = () => undefined) { } + + current(): string | undefined { + if (this.state.retired) return undefined; + if (this.state.operation) return this.state.accepted; + const text = this.read(); + if (this.state.observed && this.state.observed.text === text) return this.state.accepted; + if (text !== undefined) this.validate(text); + // A cancelled Gradle client may leave its daemon finishing the export. Only finish() + // can accept a changed report tagged by an IDE import in this session. + if (this.state.observed && text !== undefined && this.importId(text) && text !== this.state.accepted) return this.state.accepted; + this.state = { ...this.state, accepted: text, observed: { text } }; + return text; + } + + retained(): string | undefined { return this.state.accepted; } + + retire(): void { this.state = { retired: true }; } + + isRunning(operation: Readonly<{ prepare: boolean }>): boolean { return this.state.operation === operation; } + + begin(prepare: boolean): Readonly<{ prepare: boolean; id: string }> { + if (this.state.retired) throw new Error('Compiler import owner has been retired.'); + if (this.state.operation) throw new Error('An Ecstasy compiler import is already running for this folder.'); + const operation = Object.freeze({ prepare, id: randomUUID() }); + this.state = { ...this.state, operation }; + return operation; + } + + finish(operation: Readonly<{ prepare: boolean; id: string }>, outcome: ImportResult['outcome'], detail?: string): ImportResult { + if (this.state.retired) return { outcome: 'cancelled', message: 'Compiler import owner has been retired.', finished: new Date().toISOString() }; + if (this.state.operation !== operation) throw new Error('Compiler import no longer owns this result.'); + let observed = this.state.observed; + let accepted = this.state.accepted; + let result: ImportResult; + try { + const text = this.read(); + observed = { text }; + if (outcome === 'succeeded') { + if (text === undefined) throw new Error('Gradle did not export an Ecstasy compiler model.'); + this.validate(text); + if (this.importId(text) && this.importId(text) !== operation.id) throw new Error('Gradle report belongs to a different import.'); + accepted = text; + } + result = { outcome, message: outcome === 'succeeded' ? 'Ecstasy compiler inputs imported.' : + outcome === 'cancelled' ? 'Import cancelled; previous compiler configuration retained.' : + detail ?? 'Gradle import failed; previous compiler configuration retained.', finished: new Date().toISOString() }; + } catch (error) { + result = { outcome: outcome === 'cancelled' ? 'cancelled' : 'failed', + message: outcome === 'cancelled' ? 'Import cancelled; previous compiler configuration retained.' : + `${error}; previous compiler configuration retained.`, finished: new Date().toISOString() }; + } + this.state = { accepted, observed, result }; + return result; + } + + description(): string { + const { operation, result } = this.state; + return operation ? (operation.prepare ? 'Preparing generated Ecstasy inputs…' : 'Refreshing evaluated Ecstasy compiler paths…') : + result ? `${result.message}\nLast import: ${result.finished}` : 'No compiler import run in this editor session.'; + } +} + +export interface ImportResult { + readonly outcome: 'succeeded' | 'cancelled' | 'failed'; + readonly message: string; + readonly finished: string; +} diff --git a/lang/vscode-extension/src/compiler-paths.ts b/lang/vscode-extension/src/compiler-paths.ts new file mode 100644 index 00000000000..e72e2e3a8fb --- /dev/null +++ b/lang/vscode-extension/src/compiler-paths.ts @@ -0,0 +1,246 @@ +import { configureCompilerLibraries, editOrderedPaths, libraryOptions } from './library-settings'; +import { randomUUID } from 'node:crypto'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { BuildModel, describeBuildModel, mergeBuildModels, modelPath, parseBuildModel, readCompilerReport, workspaceModelPath } from './build-model'; +import { CompilerImport } from './compiler-import'; +import { runCompilerTask } from './compiler-task'; +import { getClient, updateCompilerConfiguration } from './lsp-client'; +import { compilerSourceModules } from './rename-proposal'; +import { SourceModule } from './source-graph-configuration'; + +const imports = new Map(); +const activeImports = new Map(); + +function importOwner(folder: vscode.WorkspaceFolder): CompilerImport { + const key = folder.uri.toString(); + const existing = imports.get(key); + if (existing) return existing; + if (folder.uri.scheme !== 'file') throw new Error('Compiler build import requires a local workspace folder.'); + const owner = new CompilerImport(() => readCompilerReport(folder.uri.fsPath), parseBuildModel, text => parseBuildModel(text).importId); + imports.set(key, owner); + return owner; +} + +export function compilerBuildModels(): BuildModel[] { + const folders = vscode.workspace.workspaceFolders ?? []; + const active = new Set(folders.map(folder => folder.uri.toString())); + for (const [key, owner] of imports) if (!active.has(key)) { + owner.retire(); + activeImports.get(key)?.cancel(); + imports.delete(key); + } + const models = folders.flatMap(folder => { + if (folder.uri.scheme !== 'file') return []; + const owner = importOwner(folder); + try { + owner.current(); + } catch (error) { console.warn(`Invalid Ecstasy Gradle model; retaining previous import: ${error}`); } + const text = owner.retained(); + return text === undefined ? [] : [parseBuildModel(text)]; + }); + return models.length === 0 ? [] : [mergeBuildModels(models)]; +} + +async function folder(): Promise { + const folders = vscode.workspace.workspaceFolders ?? []; + return folders.length === 1 ? folders[0] : vscode.window.showWorkspaceFolderPick(); +} + +/** Native task execution keeps build ownership and output visible and never starts Gradle in LSP. */ +export async function refreshCompilerBuild(prepare = false, selected?: vscode.WorkspaceFolder): Promise { + const owner = selected ?? await folder(); + if (!owner) return; + if (!vscode.workspace.isTrusted) throw new Error('Trust this workspace before running its Gradle build.'); + if (owner.uri.scheme !== 'file') throw new Error('Compiler build import requires a local workspace folder.'); + const key = owner.uri.toString(); + if (activeImports.has(key)) throw new Error('An Ecstasy compiler import is already running for this folder.'); + const model = importOwner(owner); + compilerBuildModels(); + const claim = model.begin(prepare); + const cancellation = new vscode.CancellationTokenSource(); + activeImports.set(key, cancellation); + try { + const wrapper = vscode.Uri.joinPath(owner.uri, process.platform === 'win32' ? 'gradlew.bat' : 'gradlew'); + await vscode.workspace.fs.stat(wrapper); + const operation = randomUUID(); + const task = new vscode.Task({ type: 'xtc-model', prepare, operation }, owner, + prepare ? 'Prepare compiler inputs' : 'Refresh compiler paths', 'Ecstasy', + new vscode.ProcessExecution(wrapper.fsPath, ['--init-script', path.resolve(__dirname, '../resources/compiler-model.init.gradle'), + prepare ? 'prepareEcstasyWorkspaceModel' : 'exportEcstasyWorkspaceModel', '--console=plain'], + { cwd: owner.uri.fsPath, env: { XTC_COMPILER_IMPORT_ID: claim.id } })); + const outcome = await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: `Ecstasy — ${prepare ? 'preparing generated inputs' : 'refreshing compiler paths'} (${owner.name})`, + cancellable: true, + }, async (progress, token) => { + const cancel = token.onCancellationRequested(() => cancellation.cancel()); + try { + if (token.isCancellationRequested) cancellation.cancel(); + progress.report({ message: owner.uri.fsPath }); + return await runCompilerTask(task, cancellation.token, message => progress.report({ message })); + } finally { cancel.dispose(); } + }); + const result = model.finish(claim, cancellation.token.isCancellationRequested ? 'cancelled' : outcome.outcome, outcome.message); + if (result.outcome === 'failed') throw new Error(result.message); + if (result.outcome === 'succeeded' && imports.get(key) === model) await updateCompilerConfiguration(); + } catch (error) { + // finish only while this operation still owns the model; a validation failure already + // retired it. Errors are rethrown below after recording the terminal model state. + if (model.isRunning(claim)) model.finish(claim, cancellation.token.isCancellationRequested ? 'cancelled' : 'failed', String(error)); + if (!cancellation.token.isCancellationRequested) throw error; + } finally { + if (activeImports.get(key) === cancellation) activeImports.delete(key); + cancellation.dispose(); + } +} + +/** A dialog owns a settings snapshot only until another edit, import or connection replaces it. */ +export class CompilerPathDraft implements vscode.Disposable { + private readonly invalid = new AbortController(); + private readonly connection = getClient(); + private readonly snapshot = this.inputs(); + private readonly subscriptions: vscode.Disposable[]; + + constructor(changed: vscode.Event) { + const invalidate = () => this.invalid.abort(); + this.subscriptions = [changed(invalidate), + vscode.workspace.onDidChangeWorkspaceFolders(invalidate), + vscode.workspace.onDidChangeConfiguration(event => { + if (event.affectsConfiguration('xtc.compiler')) invalidate(); + })]; + } + + private inputs(): string { + return JSON.stringify([compilerSourceModules(), + vscode.workspace.workspaceFolders?.map(folder => folder.uri.toString()), + vscode.workspace.workspaceFile?.toString()]); + } + + assertCurrent(): void { + if (this.invalid.signal.aborted || this.connection !== getClient() || this.snapshot !== this.inputs()) { + throw new Error('Compiler paths changed while this dialog was open. Reopen Configure Compiler Paths to edit the current configuration.'); + } + } + + dispose(): void { + this.invalid.abort(); + this.subscriptions.forEach(subscription => subscription.dispose()); + } +} + +export function registerCompilerPaths(context: vscode.ExtensionContext): void { + const output = vscode.window.createOutputChannel('Ecstasy Compiler Paths'); + const draftsChanged = new vscode.EventEmitter(); + const report = async () => { + output.clear(); + output.appendLine(compilerSourceModules() === null ? 'Effective source origin: Gradle model where imported; workspace conventions otherwise.' : 'Effective source origin: explicit workspace override (preserved across Gradle refresh).'); + output.appendLine('Bundled XDK: read-only, always included.'); + const libraries = libraryOptions(); + output.appendLine(libraries.modulePath === null ? 'External binary paths: inherited from Gradle.' : 'External binary paths: explicit workspace/application override (ordered).'); + output.appendLine(JSON.stringify(libraries, null, 2)); + const modules = await getClient()?.sendRequest('xtc/compilerSourceModules'); + output.appendLine(JSON.stringify(modules ?? [], null, 2)); + compilerBuildModels().forEach(model => output.appendLine(describeBuildModel(model))); + (vscode.workspace.workspaceFolders ?? []).forEach(folder => { + const model = imports.get(folder.uri.toString()); + if (model) output.appendLine(`${folder.name}: ${model.description()}`); + }); + output.show(true); + }; + const configure = async () => { + draftsChanged.fire(); + const draft = new CompilerPathDraft(draftsChanged.event); + try { + const selection = await vscode.window.showQuickPick(['Show effective paths', 'Refresh Gradle model', 'Prepare generated resources', 'Open build file', 'Edit explicit module paths', 'Reset to build model / discovery']); + if (selection === 'Show effective paths') return report(); + if (selection === 'Refresh Gradle model') return refreshCompilerBuild(); + if (selection === 'Prepare generated resources') return refreshCompilerBuild(true); + if (selection === 'Reset to build model / discovery') { + draft.assertCurrent(); + await vscode.workspace.getConfiguration('xtc.compiler').update('sourceModules', null, vscode.ConfigurationTarget.Workspace); + return; + } + if (selection === 'Open build file') { + const entries = compilerBuildModels().flatMap(model => model.sourceSets); + const selected = await vscode.window.showQuickPick([...new Set(entries.map(entry => entry.buildFile))]); + if (selected) await vscode.window.showTextDocument(vscode.Uri.parse(selected)); + return; + } + if (selection !== 'Edit explicit module paths') return; + const owner = await folder(); + if (!owner) return; + const effective = (compilerSourceModules() as SourceModule[] | null) ?? await getClient()?.sendRequest('xtc/compilerSourceModules') ?? []; + const chosen = await vscode.window.showQuickPick([...effective.map(module => module.name), 'Add module']); + if (!chosen) return; + const previous = effective.find(module => module.name === chosen); + const name = previous?.name ?? await vscode.window.showInputBox({ prompt: 'Module name' }); + if (!name) return; + const mode = await vscode.window.showQuickPick(['Choose module file', 'Enter module path']); + if (!mode) return; + const uri = mode === 'Choose module file' + ? (await vscode.window.showOpenDialog({ canSelectFiles: true, canSelectFolders: false, canSelectMany: false, filters: { Ecstasy: ['x'] }, defaultUri: owner.uri }))?.[0]?.toString() + : await vscode.window.showInputBox({ prompt: 'Module root file URI or path relative to the selected workspace folder', value: previous?.uri }); + if (!uri) return; + const resourceMode = await vscode.window.showQuickPick(['Automatic resource directories', 'Edit ordered resource directories']); + if (!resourceMode) return; + const resources = resourceMode === 'Automatic resource directories' ? null : + await editOrderedPaths(previous?.resourceRoots ?? [], 'Ecstasy Resource Directories', true); + if (resources === undefined) return; + const dependencies = await vscode.window.showInputBox({ prompt: 'Source dependencies, separated by commas', value: previous?.dependencies?.join(', ') ?? '' }); + if (dependencies === undefined) return; + const resolve = (value: string) => value.startsWith('file:') ? vscode.Uri.parse(value).toString() : vscode.Uri.file(path.resolve(owner.uri.fsPath, value)).toString(); + const roots = resources; + if (roots !== null && (!Array.isArray(roots) || roots.some(value => typeof value !== 'string'))) throw new Error('Resource roots must be a JSON array of paths.'); + const replacement: SourceModule = { name, uri: resolve(uri), dependencies: dependencies.split(',').map(value => value.trim()).filter(Boolean), resourceRoots: roots === null ? null : (roots as string[]).map(resolve) }; + draft.assertCurrent(); + await vscode.workspace.getConfiguration('xtc.compiler').update('sourceModules', [...effective.filter(module => module.name !== name), replacement], vscode.ConfigurationTarget.Workspace); + } finally { + draft.dispose(); + } + }; + const guarded = (action: () => Promise) => async () => { + try { await action(); } catch (error) { void vscode.window.showErrorMessage(String(error)); } + }; + const watchers = new Map(); + const changed = () => { draftsChanged.fire(); void updateCompilerConfiguration().catch(error => output.appendLine(String(error))); }; + const watch = (owner: vscode.WorkspaceFolder) => { + const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(owner, '{' + modelPath + ',' + workspaceModelPath + '}')); + watchers.set(owner.uri.toString(), vscode.Disposable.from(watcher, watcher.onDidCreate(changed), watcher.onDidChange(changed), watcher.onDidDelete(changed))); + }; + (vscode.workspace.workspaceFolders ?? []).forEach(watch); + context.subscriptions.push(output, { dispose: () => { + draftsChanged.fire(); + draftsChanged.dispose(); + watchers.forEach(watcher => watcher.dispose()); + activeImports.forEach(cancellation => cancellation.cancel()); + imports.forEach(model => model.retire()); + imports.clear(); + } }, + vscode.tasks.onDidEndTaskProcess(event => { + const task = event.execution.task; + const owner = task.scope; + // Observe the public task lifecycle rather than depending on a particular Java or + // Gradle extension. Our own xtc-model tasks cannot recursively trigger another export. + if (event.exitCode !== 0 || task.definition.type !== 'gradle' || !owner || typeof owner === 'number' || + !vscode.workspace.isTrusted || owner.uri.scheme !== 'file' || activeImports.has(owner.uri.toString()) || + readCompilerReport(owner.uri.fsPath) === undefined) return; + void refreshCompilerBuild(false, owner).catch(error => output.appendLine(`Automatic compiler refresh failed: ${error}`)); + }), + vscode.workspace.onDidChangeWorkspaceFolders(event => { + event.removed.forEach(owner => { + const key = owner.uri.toString(); + activeImports.get(key)?.cancel(); + imports.get(key)?.retire(); + imports.delete(key); + watchers.get(key)?.dispose(); watchers.delete(key); + }); + event.added.forEach(watch); + changed(); + }), + vscode.commands.registerCommand('xtc.configureCompilerLibraries', guarded(configureCompilerLibraries)), + vscode.commands.registerCommand('xtc.configureCompilerPaths', guarded(configure)), + vscode.commands.registerCommand('xtc.showCompilerPaths', guarded(report)), + vscode.commands.registerCommand('xtc.refreshCompilerBuild', guarded(() => refreshCompilerBuild())), + vscode.commands.registerCommand('xtc.prepareCompilerInputs', guarded(() => refreshCompilerBuild(true)))); +} diff --git a/lang/vscode-extension/src/compiler-task.ts b/lang/vscode-extension/src/compiler-task.ts new file mode 100644 index 00000000000..985fe92bab0 --- /dev/null +++ b/lang/vscode-extension/src/compiler-task.ts @@ -0,0 +1,43 @@ +import * as vscode from 'vscode'; +import { ImportResult } from './compiler-import'; + +type TaskHost = Pick; + +/** Wait for the owned native task, including cancellation before executeTask returns its handle. */ +export async function runCompilerTask(task: vscode.Task, token: vscode.CancellationToken, + report: (message: string) => void, host: TaskHost = vscode.tasks): Promise> { + if (token.isCancellationRequested) return { outcome: 'cancelled', message: 'Import cancelled.' }; + const retired = new AbortController(); + // Defer dispatch until listeners are installed: even an immediately exiting process is observed. + const started = Promise.resolve().then(() => token.isCancellationRequested ? undefined : host.executeTask(task)); + const owns = (execution: vscode.TaskExecution) => execution.task.definition.operation === task.definition.operation; + const subscriptions: vscode.Disposable[] = []; + try { + return await new Promise((resolve, reject) => { + subscriptions.push( + host.onDidEndTaskProcess(event => { + if (!owns(event.execution)) return; + resolve(token.isCancellationRequested ? { outcome: 'cancelled', message: 'Import cancelled.' } : + event.exitCode === 0 ? { outcome: 'succeeded', message: 'Validating evaluated compiler inputs…' } : + { outcome: 'failed', message: `Gradle import failed (${event.exitCode ?? 'no exit code'}); previous compiler configuration retained. See the Ecstasy task terminal.` }); + }), + host.onDidEndTask(event => { + if (owns(event.execution)) resolve(token.isCancellationRequested ? { outcome: 'cancelled', message: 'Import cancelled.' } : + { outcome: 'failed', message: 'Gradle task ended without a successful process result; previous compiler configuration retained.' }); + }), + token.onCancellationRequested(() => { + report('Cancelling Gradle import…'); + void started.then(execution => { + if (!retired.signal.aborted) execution?.terminate(); + }, () => { /* Dispatch failure is handled by the task promise below. */ }); + }), + ); + void started.then(execution => { + if (!execution) resolve({ outcome: 'cancelled', message: 'Import cancelled.' }); + }, reject); + }); + } finally { + retired.abort(); + subscriptions.forEach(subscription => subscription.dispose()); + } +} diff --git a/lang/vscode-extension/src/debug-adapter.ts b/lang/vscode-extension/src/debug-adapter.ts index 04c5d84fe41..833476a41f0 100644 --- a/lang/vscode-extension/src/debug-adapter.ts +++ b/lang/vscode-extension/src/debug-adapter.ts @@ -14,7 +14,7 @@ export class XtcDebugAdapterDescriptorFactory implements vscode.DebugAdapterDesc const serverJar = this.context.asAbsolutePath(path.join('server', 'dap-server.jar')); if (!fs.existsSync(serverJar)) { void vscode.window.showErrorMessage( - 'XTC DAP server JAR not found. Build lang:vscode-extension to enable debugging.', + 'Ecstasy DAP server JAR not found. Build lang:vscode-extension to enable debugging.', 'Show Build Command' ).then(choice => { if (choice === 'Show Build Command') { diff --git a/lang/vscode-extension/src/editor-settings.ts b/lang/vscode-extension/src/editor-settings.ts new file mode 100644 index 00000000000..f527d2009da --- /dev/null +++ b/lang/vscode-extension/src/editor-settings.ts @@ -0,0 +1,30 @@ +import * as vscode from 'vscode'; +import { parseServiceSettings, ServiceSettings } from './service-settings'; + +export function readServiceSettings(resource?: vscode.Uri): ServiceSettings { + const config = vscode.workspace.getConfiguration('xtc', resource); + return parseServiceSettings({ + adapter: config.get('languageService.adapter'), + textSynchronization: config.get('languageService.textSynchronization'), + saveFormatting: config.get('languageService.saveFormatting'), + inlayHints: config.get('inlayHints.enabled'), + }); +} + +export function nativeFormatOnSave(document: vscode.TextDocument): boolean { + return vscode.workspace.getConfiguration('editor', { uri: document.uri, languageId: document.languageId }).get('formatOnSave', false); +} + +export function formattingSettings(): object { + const config = vscode.workspace.getConfiguration('xtc.formatting'); + const integer = (key: string, fallback: number, maximum = 32) => { + const value = config.get(key, fallback); + if (typeof value !== 'number' || !Number.isInteger(value) || value < 1 || value > maximum) { + throw new Error(`Ecstasy formatting.${key} must be an integer from 1 to ${maximum}`); + } + return value; + }; + const insertSpaces = config.get('insertSpaces', true); + if (typeof insertSpaces !== 'boolean') throw new Error('Ecstasy formatting.insertSpaces must be a boolean'); + return { indentSize: integer('indentSize', 4), continuationIndentSize: integer('continuationIndentSize', 8), maxLineWidth: integer('maxLineWidth', 120, 1000), insertSpaces }; +} diff --git a/lang/vscode-extension/src/extension.ts b/lang/vscode-extension/src/extension.ts index 3387c4bb7cc..bf18d7bce90 100644 --- a/lang/vscode-extension/src/extension.ts +++ b/lang/vscode-extension/src/extension.ts @@ -7,19 +7,28 @@ // TextMate remains as the fast-paint fallback during server startup. import * as fs from 'node:fs'; +import * as os from 'node:os'; import * as path from 'node:path'; import * as vscode from 'vscode'; import { createStatusBar, updateStatusBar } from './status-bar'; -import { startLanguageClient, restartLanguageClient, stopLanguageClient, applyTraceConfig } from './lsp-client'; +import { startLanguageClient, restartLanguageClient, stopLanguageClient, applyTraceConfig, updateCompilerConfiguration, updateEditorConfiguration, getClient, connectionSettingsChanged } from './lsp-client'; +import { selectLanguageAdapter } from './adapter-selection'; import { XtcTaskProvider } from './task-provider'; import { XtcDebugAdapterDescriptorFactory, XtcDebugConfigurationProvider } from './debug-adapter'; import { registerCommands } from './commands'; +import { registerCompilerPaths } from './compiler-paths'; +import { readServiceSettings } from './editor-settings'; +import { compilerSettingsLocation } from './rename-proposal'; +import { runtimeJvmOptions, runtimeLogArguments } from './runtime-settings'; +import { configuredSettings, configurationProperties } from './settings-report'; +import { supportLogs } from './support-logs'; +import { serviceFailure } from './service-notifications'; function ensureXtcLanguageAssociation(document: vscode.TextDocument): void { if (document.fileName.endsWith('.x') && document.languageId !== 'xtc') { vscode.languages.setTextDocumentLanguage(document, 'xtc').then( - () => console.log(`Set language to XTC for ${document.fileName}`), + () => console.log(`Set language to Ecstasy for ${document.fileName}`), err => console.error(`Failed to set language for ${document.fileName}:`, err) ); } @@ -54,7 +63,7 @@ async function fixFilesAssociation(context: vscode.ExtensionContext): Promise { @@ -92,7 +101,7 @@ export function activate(context: vscode.ExtensionContext): void { vscode.workspace.textDocuments.forEach(ensureXtcLanguageAssociation); // Create output channel for LSP server - const outputChannel = vscode.window.createOutputChannel('XTC Language Server', { log: true }); + const outputChannel = vscode.window.createOutputChannel('Ecstasy Language Server', { log: true }); // Register all providers and UI components const statusBar = createStatusBar(); @@ -105,39 +114,96 @@ export function activate(context: vscode.ExtensionContext): void { ); registerCommands(context, outputChannel); + registerCompilerPaths(context); // Setup LSP server const serverJar = context.asAbsolutePath(path.join('server', 'lsp-server.jar')); const serverExists = fs.existsSync(serverJar); // Register LSP-related commands - context.subscriptions.push( + const effectiveOutput = vscode.window.createOutputChannel('Ecstasy Effective Configuration'); + // Owned by this UI command on the extension event loop, never by compiler callbacks. + let effectiveRevision = 0; + context.subscriptions.push(effectiveOutput, + { dispose: () => { effectiveRevision++; } }, + vscode.workspace.onDidChangeConfiguration(() => { effectiveRevision++; }), + vscode.commands.registerCommand('xtc.showLanguageServiceStatus', async () => { + const revision = ++effectiveRevision; + const resource = vscode.window.activeTextEditor?.document.uri; + const settings = vscode.workspace.getConfiguration('xtc', resource); + const configured = configuredSettings(configurationProperties(context.extension.packageJSON.contributes.configuration), resource); + const running = getClient(); + const effective = running?.isRunning() + ? await running.sendRequest('xtc/languageServiceStatus').catch(error => ({ status: 'unavailable', reason: String(error) })) + : { status: 'not running; open an Ecstasy file or check the server log' }; + if (revision !== effectiveRevision || running !== getClient()) return undefined; + const restartRequired = (() => { + try { return running?.isRunning() ? connectionSettingsChanged() : false; } + catch (error) { return { invalidSavedSettings: String(error) }; } + })(); + const report = { configured, effective, restartRequired, activeDocument: resource?.toString(), nativeFormatOnSave: vscode.workspace.getConfiguration('editor', { uri: resource, languageId: 'xtc' }).get('formatOnSave', false), sourceRoots: settings.get('sourceRoots', []), compilerPaths: 'Ecstasy: Show Effective Compiler Paths', log: 'Ecstasy: Show Language Server Output' }; + effectiveOutput.clear(); + effectiveOutput.appendLine(JSON.stringify(report, null, 2)); + effectiveOutput.show(true); + return report; + }), + vscode.commands.registerCommand('xtc.exportServerLogs', async (target?: vscode.Uri) => { + const connection = getClient(); + const destination = target ?? await vscode.window.showSaveDialog({ title: 'Export Ecstasy Server Logs (includes local paths and logged diagnostics)', filters: { 'ZIP archives': ['zip'] }, defaultUri: vscode.Uri.file(path.join(os.homedir(), 'ecstasy-server-logs.zip')) }); + if (!destination) return; + const archive = connection?.isRunning() + ? Buffer.from((await connection.sendRequest<{ base64: string }>('xtc/exportLogs')).base64, 'base64') + : await supportLogs(context).export(); + await vscode.workspace.fs.writeFile(destination, archive); + outputChannel.info(`Exported Ecstasy server logs to ${destination.fsPath}`); + }), vscode.commands.registerCommand('xtc.restartServer', async () => { if (serverExists) { await restartLanguageClient(context, serverJar, outputChannel); } else { - vscode.window.showWarningMessage('XTC Language Server JAR not found. Build the extension first.'); + vscode.window.showWarningMessage('Ecstasy Language Server JAR not found. Build the extension first.'); } }), + vscode.commands.registerCommand('xtc.selectLanguageAdapter', selectLanguageAdapter), + vscode.workspace.onDidChangeTextDocument(event => { + if (event.document.uri.toString() === compilerSettingsLocation()?.uri.toString()) { + void updateCompilerConfiguration().catch(error => outputChannel.error(`Compiler settings edit rejected: ${error}`)); + } + }), vscode.workspace.onDidChangeConfiguration(event => { + if (event.affectsConfiguration('xtc.java.vmOptions') || event.affectsConfiguration('xtc.server.logs')) { + try { + runtimeJvmOptions(); + runtimeLogArguments(); + void vscode.window.showInformationMessage('Ecstasy runtime/log settings saved. Restart the language server to apply them.', 'Restart now', 'Open Settings') + .then(choice => choice === 'Restart now' ? vscode.commands.executeCommand('xtc.restartServer') + : choice === 'Open Settings' ? vscode.commands.executeCommand('workbench.action.openSettings', 'xtc.java.vmOptions') : undefined); + } catch (error) { serviceFailure(`Invalid Ecstasy runtime/log settings; running server retained: ${error}`); } + } if (event.affectsConfiguration('xtc.trace.server')) { void applyTraceConfig(); } + if (event.affectsConfiguration('xtc.compiler.sourceModules') || event.affectsConfiguration('xtc.compiler.libraries')) { + void updateCompilerConfiguration().catch(error => outputChannel.error(`Compiler configuration update failed: ${error}`)); + } + if (event.affectsConfiguration('xtc.formatting') || event.affectsConfiguration('xtc.inlayHints.enabled') || event.affectsConfiguration('xtc.codeLens.references')) { + void updateEditorConfiguration().catch(error => outputChannel.error(`Editor configuration update failed: ${error}`)); + } const needsRestart = serverExists && ( - event.affectsConfiguration('xtc.java.home') || - event.affectsConfiguration('xtc.sourceRoots') + event.affectsConfiguration('xtc.java.home') || event.affectsConfiguration('xtc.sourceRoots') || + event.affectsConfiguration('xtc.languageService') ); if (needsRestart) { - const changed = event.affectsConfiguration('xtc.java.home') ? 'Java path' : 'Source roots'; - vscode.window.showInformationMessage( - `${changed} changed. Restart the language server?`, - 'Restart', 'Later' - ).then(choice => { - if (choice === 'Restart') { - void restartLanguageClient(context, serverJar, outputChannel); - } - }); + try { + readServiceSettings(); + if (!connectionSettingsChanged()) return; + outputChannel.info('Connection settings changed; restarting Ecstasy and resynchronizing open buffers.'); + void restartLanguageClient(context, serverJar, outputChannel).catch(error => outputChannel.error(`Ecstasy restart failed: ${error}`)); + } catch (error) { + outputChannel.error(`Invalid Ecstasy settings; previous connection retained: ${error}`); + serviceFailure(`Invalid Ecstasy settings; previous connection retained: ${error}`); + } } }) ); @@ -145,13 +211,13 @@ export function activate(context: vscode.ExtensionContext): void { // Start language server or show build instructions if (serverExists) { updateStatusBar('starting'); - startLanguageClient(context, serverJar, outputChannel); + void startLanguageClient(context, serverJar, outputChannel).catch(error => outputChannel.error(`Ecstasy startup failed: ${error}`)); } else { const buildCmd = './gradlew :lang:vscode-extension:assemble -PincludeBuildLang=true -PincludeBuildAttachLang=true'; - console.log('XTC Language Server JAR not found at:', serverJar); + console.log('Ecstasy Language Server JAR not found at:', serverJar); console.log(`Build with: ${buildCmd}`); void vscode.window.showErrorMessage( - 'XTC Language Server JAR not found. Build lang:vscode-extension to enable LSP features.', + 'Ecstasy Language Server JAR not found. Build lang:vscode-extension to enable LSP features.', 'Show Build Command' ).then(choice => { if (choice === 'Show Build Command') { diff --git a/lang/vscode-extension/src/java.ts b/lang/vscode-extension/src/java.ts index ce786d455c4..2c08c14a210 100644 --- a/lang/vscode-extension/src/java.ts +++ b/lang/vscode-extension/src/java.ts @@ -58,7 +58,10 @@ function configuredJavaHome(): string | null { } const javaExe = process.platform === "win32" ? "java.exe" : "java"; const candidate = path.join(configured, "bin", javaExe); - return fs.existsSync(candidate) ? candidate : null; + if (!fs.existsSync(candidate)) { + throw new Error(`Ecstasy java.home has no ${javaExe} executable: ${configured}. Correct the setting or reset it to use automatic discovery.`); + } + return candidate; } function jreStorageDir(context: vscode.ExtensionContext): string { diff --git a/lang/vscode-extension/src/library-configuration.ts b/lang/vscode-extension/src/library-configuration.ts new file mode 100644 index 00000000000..bd6b06582f9 --- /dev/null +++ b/lang/vscode-extension/src/library-configuration.ts @@ -0,0 +1,46 @@ +import * as fs from 'node:fs'; +import * as path from 'node:path'; +import { fileURLToPath, pathToFileURL } from 'node:url'; + +export interface SourceAttachment { readonly module: string; readonly roots: readonly string[] } +export interface LibraryOptions { readonly modulePath: readonly string[] | null; readonly sourceAttachments: readonly SourceAttachment[] } +export const inheritedLibraries: LibraryOptions = Object.freeze({ modulePath: null, sourceAttachments: [] }); + +/** Window-owned library inputs. Relative paths need exactly one root; sources are navigation only. */ +export function normalizeLibraries(raw: unknown, folders: readonly string[], checkFiles = false): LibraryOptions { + if (raw === undefined || raw === null) return inheritedLibraries; + if (typeof raw !== 'object' || Array.isArray(raw)) throw new Error('Libraries must be an object.'); + const value = raw as Record; + if (Object.keys(value).some(key => !['modulePath', 'sourceAttachments'].includes(key))) throw new Error('Unknown library setting.'); + const paths = (input: unknown, sources: boolean): string[] => { + if (!Array.isArray(input) || input.some(item => typeof item !== 'string' || !item.trim())) throw new Error('Library paths must be non-blank strings.'); + const result = input.map((text: string) => { + const uri = /^[a-z][a-z\d+.-]*:/i.test(text) ? new URL(text) : (() => { + if (folders.length !== 1) throw new Error('Relative library paths require exactly one workspace folder.'); + return new URL(text, folders[0].replace(/\/?$/, '/')); + })(); + if (uri.protocol !== 'file:' || uri.search || uri.hash) throw new Error('Library paths must use local file URIs.'); + const file = path.resolve(fileURLToPath(uri)); + const canonical = fs.existsSync(file) ? fs.realpathSync(file) : file; + if (checkFiles) { + const stat = fs.statSync(canonical, { throwIfNoEntry: false }); + if (!stat || (sources ? !stat.isDirectory() : !stat.isDirectory() && !(stat.isFile() && canonical.endsWith('.xtc')))) { + throw new Error(`${sources ? 'Source directory' : 'Library file or directory'} does not exist: ${text}`); + } + } + return pathToFileURL(canonical).toString(); + }); + if (new Set(result).size !== result.length) throw new Error('Duplicate library path.'); + return result; + }; + if (value.sourceAttachments !== undefined && !Array.isArray(value.sourceAttachments)) throw new Error('sourceAttachments must be an array.'); + const attachments = (value.sourceAttachments as unknown[] | undefined ?? []).map(raw => { + const attachment = raw as Partial | null; + if (!attachment || typeof attachment.module !== 'string' || !attachment.module.trim()) throw new Error('Source attachment needs a module name.'); + const roots = paths(attachment.roots, true); + if (!roots.length) throw new Error('Source attachment needs ordered roots.'); + return { module: attachment.module, roots }; + }); + if (new Set(attachments.map(item => item.module)).size !== attachments.length) throw new Error('Duplicate source attachment module.'); + return { modulePath: value.modulePath == null ? null : paths(value.modulePath, false), sourceAttachments: attachments }; +} diff --git a/lang/vscode-extension/src/library-settings.ts b/lang/vscode-extension/src/library-settings.ts new file mode 100644 index 00000000000..a60b5b7f524 --- /dev/null +++ b/lang/vscode-extension/src/library-settings.ts @@ -0,0 +1,79 @@ +import * as vscode from 'vscode'; +import { getClient, updateCompilerConfiguration } from './lsp-client'; +import { LibraryOptions, normalizeLibraries } from './library-configuration'; + +export function libraryOptions(): LibraryOptions { + return normalizeLibraries(vscode.workspace.getConfiguration('xtc.compiler').get('libraries'), + (vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.toString())); +} + +/** Edits are local until Apply. Escape from any step discards the complete path-list draft. */ +export async function editOrderedPaths(initial: readonly string[], title: string, directoriesOnly: boolean): Promise { + let draft = [...initial]; + while (true) { + const items = [ + { label: 'Use these paths', index: -1 }, { label: 'Choose path…', index: -2 }, { label: 'Enter path…', index: -3 }, + ...draft.map((value, index) => ({ label: `${index + 1}. ${value}`, index })) + ]; + const picked = await vscode.window.showQuickPick(items, { title, placeHolder: 'Paths are searched in this order; select a path to move or remove it' }); + if (!picked) return undefined; + if (picked.index === -1) return draft; + if (picked.index === -2) { + const chosen = await vscode.window.showOpenDialog({ title, canSelectMany: true, canSelectFolders: true, canSelectFiles: !directoriesOnly }); + if (!chosen) return undefined; + draft = [...draft, ...chosen.map(uri => uri.toString())]; + } else if (picked.index === -3) { + const value = await vscode.window.showInputBox({ title, prompt: 'File URI or path relative to the single workspace folder' }); + if (value === undefined) return undefined; + if (value.trim()) draft = [...draft, value.trim()]; + } else { + const action = await vscode.window.showQuickPick(['Move up', 'Move down', 'Remove'], { title }); + if (!action) return undefined; + const index = picked.index; + if (action === 'Remove') draft = draft.filter((_, at) => at !== index); + else { + const to = index + (action === 'Move up' ? -1 : 1); + if (to >= 0 && to < draft.length) [draft[index], draft[to]] = [draft[to], draft[index]]; + } + } + } +} + +export async function configureCompilerLibraries(): Promise { + const settings = vscode.workspace.getConfiguration('xtc.compiler'); + const before = JSON.stringify(settings.get('libraries')); + const folders = (vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.toString()); + const connection = getClient(); + let draft = libraryOptions(); + while (true) { + const picked = await vscode.window.showQuickPick([ + 'Apply library settings', 'Reload applied libraries', 'Edit binary paths', 'Inherit Gradle libraries', 'Add source attachment', + ...draft.sourceAttachments.map(item => `Edit sources: ${item.module}`) + ], { title: 'Ecstasy Libraries and Sources', placeHolder: 'Bundled XDK is always included and read-only. Attached sources provide navigation only.' }); + if (!picked) return; + if (picked === 'Reload applied libraries') { await updateCompilerConfiguration(); return; } + if (picked === 'Apply library settings') { + if (before !== JSON.stringify(vscode.workspace.getConfiguration('xtc.compiler').get('libraries')) || connection !== getClient() || + JSON.stringify(folders) !== JSON.stringify((vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.toString()))) { + throw new Error('Compiler libraries or workspace changed while this dialog was open. Reopen it to edit current settings.'); + } + await settings.update('libraries', normalizeLibraries(draft, folders, true), vscode.ConfigurationTarget.Workspace); + return; + } + if (picked === 'Inherit Gradle libraries') { draft = { ...draft, modulePath: null }; continue; } + if (picked === 'Edit binary paths') { + const paths = await editOrderedPaths(draft.modulePath ?? [], 'Ecstasy Binary Library Paths', false); + if (paths === undefined) return; + draft = { ...draft, modulePath: paths }; + continue; + } + const module = picked === 'Add source attachment' + ? await vscode.window.showInputBox({ title: 'Ecstasy Source Attachment', prompt: 'Exact binary module name' }) + : picked.slice('Edit sources: '.length); + if (!module) return; + const previous = draft.sourceAttachments.find(item => item.module === module); + const roots = await editOrderedPaths(previous?.roots ?? [], `Ecstasy Sources: ${module}`, true); + if (roots === undefined) return; + draft = { ...draft, sourceAttachments: [...draft.sourceAttachments.filter(item => item.module !== module), ...(roots.length ? [{ module, roots }] : [])] }; + } +} diff --git a/lang/vscode-extension/src/lsp-client.ts b/lang/vscode-extension/src/lsp-client.ts index 8bc3e84525b..d42ea84a828 100644 --- a/lang/vscode-extension/src/lsp-client.ts +++ b/lang/vscode-extension/src/lsp-client.ts @@ -6,24 +6,59 @@ import { Executable, LanguageClient, LanguageClientOptions, + MessageTransports, ServerOptions, State, Trace, TransportKind } from 'vscode-languageclient/node'; +import { formattingSettings, nativeFormatOnSave, readServiceSettings } from './editor-settings'; +import { synchronizationOptions } from './service-settings'; +import { compilerBuildModels } from './compiler-paths'; +import { BuildModel } from './build-model'; +import { LibraryOptions } from './library-configuration'; +import { libraryOptions } from './library-settings'; import { buildJvmArgs, findJavaExecutable } from './java'; +import { compilerSourceModules, moveWithConfiguration, renameWithConfiguration } from './rename-proposal'; import { updateStatusBar } from './status-bar'; +import { runtimeJvmOptions, runtimeLogArguments } from './runtime-settings'; +import { supportLogs } from './support-logs'; +import { serviceFailure } from './service-notifications'; let client: LanguageClient | undefined; -let crashCount = 0; -let hasEverReachedRunning = false; +let activeConnectionKey: string | undefined; + +function connectionKey(): string { + const settings = readServiceSettings(); + const config = vscode.workspace.getConfiguration('xtc'); + return JSON.stringify([settings.adapter, settings.textSynchronization, settings.saveFormatting, config.get('java.home', ''), config.get('sourceRoots', []), runtimeJvmOptions(), runtimeLogArguments()]); +} + +export function connectionSettingsChanged(): boolean { return connectionKey() !== activeConnectionKey; } + const MAX_CRASH_RESTARTS = 3; export function getClient(): LanguageClient | undefined { return client; } +function compilerConfiguration(): { sourceModules: unknown[] | null; buildModels: BuildModel[]; libraries: LibraryOptions } { + return { sourceModules: compilerSourceModules(), buildModels: compilerBuildModels(), libraries: libraryOptions() }; +} + +export async function updateEditorConfiguration(): Promise { + if (client?.state === State.Running) { + await client.sendNotification('workspace/didChangeConfiguration', { settings: { xtc: { presentation: {} } } }); + } +} + +export async function updateCompilerConfiguration(): Promise { + if (client?.state === State.Running) { + await client.sendNotification('workspace/didChangeConfiguration', { settings: { xtc: { compiler: compilerConfiguration() } } }); + } +} + /** Stop the client safely, swallowing any state-related throws. */ async function safeStop(): Promise { const c = client; @@ -38,12 +73,31 @@ async function safeStop(): Promise { } } -export async function startLanguageClient(context: vscode.ExtensionContext, serverJar: string, outputChannel: vscode.LogOutputChannel): Promise { - const javaExecutable = await findJavaExecutable(context); +let starting: Promise | undefined; +export function startLanguageClient(context: vscode.ExtensionContext, serverJar: string, outputChannel: vscode.LogOutputChannel): Promise { + starting ??= startConnection(context, serverJar, outputChannel).finally(() => { starting = undefined; }); + return starting; +} + +async function startConnection(context: vscode.ExtensionContext, serverJar: string, outputChannel: vscode.LogOutputChannel): Promise { + // These counters belong to this connection, including its automatic crash restarts. + let crashCount = 0; + let hasEverReachedRunning = false; + const preferences = readServiceSettings(); + const requestedKey = connectionKey(); + let lastFormatting = formattingSettings(); + const logs = supportLogs(context); + let launch = logs.begin('Resolving the Java runtime.\n'); + const javaExecutable = await findJavaExecutable(context).catch(error => { + logs.finish(launch, `Startup failed: ${error}\n`); + serviceFailure(`Cannot start Ecstasy: ${error}`); + throw error; + }); const logLevel = process.env.XTC_LOG_LEVEL?.toUpperCase() ?? 'INFO'; - const jvmArgs = buildJvmArgs(serverJar, logLevel); + const adapterArgs = preferences.adapter === 'default' ? [] : [`-Dxtc.lsp.adapter=${preferences.adapter}`]; + const jvmArgs = [...runtimeJvmOptions(), ...runtimeLogArguments(), ...adapterArgs, ...buildJvmArgs(serverJar, logLevel)]; - outputChannel.appendLine('Starting XTC Language Server...'); + outputChannel.appendLine('Starting Ecstasy Language Server...'); outputChannel.appendLine(`Java: ${javaExecutable}`); outputChannel.appendLine(`Args: ${jvmArgs.join(' ')}`); outputChannel.appendLine(`JAR: ${serverJar}`); @@ -63,29 +117,54 @@ export async function startLanguageClient(context: vscode.ExtensionContext, serv }; const clientOptions: LanguageClientOptions = { - documentSelector: [{ scheme: 'file', language: 'xtc' }], + documentSelector: [{ scheme: 'file', language: 'xtc' }, { scheme: 'ecstasy-library', language: 'xtc' }], outputChannel, traceOutputChannel: outputChannel, synchronize: { fileEvents: vscode.workspace.createFileSystemWatcher('**/*.x') }, initializationOptions: { - inlayHintsEnabled: vscode.workspace.getConfiguration('xtc').get('inlayHints.enabled', true), - xtcSourceRoots: vscode.workspace.getConfiguration('xtc').get('sourceRoots', []) + xtcDocumentSync: synchronizationOptions(preferences, false), + xtcSourceRoots: vscode.workspace.getConfiguration('xtc').get('sourceRoots', []), + xtcCompiler: compilerConfiguration() }, + // Our startup handler reports one actionable failure; the default handler also opens + // raw JSON-RPC and initialization popups for the same failed process. + initializationFailedHandler: () => false, middleware: { + provideInlayHints: (document, range, token, next) => { + const value = vscode.workspace.getConfiguration('xtc', document.uri).get('inlayHints.enabled', true); + const enabled = typeof value === 'boolean' ? value : preferences.inlayHints; + return enabled ? next(document, range, token) : []; + }, + // Save ownership is checked per document at the instant of saving, including + // language/folder overrides changed after initialization. + willSaveWaitUntil: (event, next) => nativeFormatOnSave(event.document) ? Promise.resolve([]) : next(event), + provideRenameEdits: (document, position, name, token, next) => { + if (client !== connection) return null; + return connection.initializeResult?.capabilities.experimental?.xtcRenameProposal === 1 + ? renameWithConfiguration(connection, document, position, name, token) + : next(document, position, name, token); + }, workspace: { + willRenameFiles: (event, next) => { + if (client !== connection) return Promise.resolve(null); + return connection.initializeResult?.capabilities.experimental?.xtcFileMoveProposal === 1 + ? moveWithConfiguration(connection, event) : next(event); + }, configuration: (params: ConfigurationParams) => { return params.items.map(item => { + if (item.section === 'xtc.compiler') { + return compilerConfiguration(); + } + if (item.section === 'xtc.codeLens') { + const resource = item.scopeUri ? vscode.Uri.parse(item.scopeUri) : undefined; + return { references: vscode.workspace.getConfiguration('xtc.codeLens', resource).get('references', true) }; + } if (item.section === 'xtc.formatting') { - const config = vscode.workspace.getConfiguration('xtc.formatting'); - return { - indentSize: config.get('indentSize', 4), - continuationIndentSize: config.get('continuationIndentSize', 8), - tabSize: config.get('tabSize', 4), - insertSpaces: config.get('insertSpaces', true), - maxLineWidth: config.get('maxLineWidth', 120) - }; + try { lastFormatting = formattingSettings(); } + catch (error) { outputChannel.warn(`Retaining previous Ecstasy formatting settings: ${error}`); } + return lastFormatting; } return {}; }); @@ -100,11 +179,12 @@ export async function startLanguageClient(context: vscode.ExtensionContext, serv return { action: ErrorAction.Shutdown }; }, closed: () => { + if (client !== connection) return { action: CloseAction.DoNotRestart, handled: true }; if (!hasEverReachedRunning) { // Server died before reaching Running state (startup crash). // Do not restart to avoid unhandled rejection issues in vscode-languageclient. updateStatusBar('error'); - return { action: CloseAction.DoNotRestart }; + return { action: CloseAction.DoNotRestart, handled: true }; } crashCount++; if (crashCount <= MAX_CRASH_RESTARTS) { @@ -112,29 +192,64 @@ export async function startLanguageClient(context: vscode.ExtensionContext, serv return { action: CloseAction.Restart }; } updateStatusBar('stopped'); - void vscode.window.showErrorMessage( - `XTC Language Server crashed ${crashCount} times and will not be restarted. Use "Ecstasy: Restart Language Server" to restart manually.` - ); - return { action: CloseAction.DoNotRestart }; + serviceFailure(`Ecstasy Language Server crashed ${crashCount} times and will not restart automatically.`); + return { action: CloseAction.DoNotRestart, handled: true }; } } }; - client = new LanguageClient( + const connection = new class extends LanguageClient { + override async start(): Promise { + try { + // TODO VSCODE: UP28 — start() can lose its internal startup promise when the + // transport closes during initialize. Observe the idempotent in-flight start + // immediately too, before upstream clears it, so its rejection is handled. + // Remove when upstream always returns/observes that captured promise (X272). + const starting = super.start(); + await Promise.all([starting, super.start()]); + if (!this.isRunning()) throw new Error('Server stopped before initialization completed'); + } catch (error) { + if (client === this) { + const message = error instanceof Error ? error.message : String(error); + logs.finish(launch, `Startup failed: ${message}\n`); + serviceFailure('Ecstasy Language Server could not start. Check the Java runtime and JVM options; details are in the server log.'); + updateStatusBar('error'); + } + throw error; + } + } + + override error(message: string, data?: unknown, showNotification?: boolean | 'force'): void { + // Keep full transport detail in Output. Startup/disconnect notifications have an owner. + super.error(message, data, this.isRunning() ? showNotification : false); + } + + override stop(timeout?: number): Promise { + // Upstream invokes void stop() during initialization failure, when stopping is invalid. + return super.stop(timeout).catch(() => {}); + } + + protected override async createMessageTransports(encoding: string): Promise { + const id = logs.begin(`Java: ${javaExecutable}\nArguments: ${jvmArgs.join(' ')}\n`); + launch = id; + const transports = await super.createMessageTransports(encoding); + this.serverProcess?.stderr?.on('data', (chunk: Buffer) => logs.append(id, chunk.toString())); + this.serverProcess?.once('exit', (code, signal) => logs.finish(id, `\nProcess exited: code=${code}, signal=${signal ?? 'none'}\n`)); + return transports; + } + }( 'xtcLanguageServer', - 'XTC Language Server', + 'Ecstasy Language Server', serverOptions, clientOptions ); - // Patch stop() to suppress internal rejections from vscode-languageclient. - // The library calls `void this.stop()` during initialization failures, creating - // unhandled rejections. This patch ensures stop() always resolves. - const originalStop = client.stop.bind(client); - (client as unknown as { stop: (timeout?: number) => Promise }).stop = - (timeout?: number) => originalStop(timeout).catch(() => {}); + client = connection; + activeConnectionKey = requestedKey; + const startingClient = client; client.onDidChangeState(({ newState }) => { + if (client !== connection) return; const stateMap = { [State.Starting]: 'starting' as const, [State.Running]: 'ready' as const, @@ -144,6 +259,15 @@ export async function startLanguageClient(context: vscode.ExtensionContext, serv if (newState === State.Running) { crashCount = 0; hasEverReachedRunning = true; + const id = launch; + void connection.sendRequest<{ adapter: string; logs: { directory: string; traceDirectory: string } }>('xtc/languageServiceStatus') + .then(status => { + if (client === connection && connection.isRunning()) { + logs.remember(id, status); + updateStatusBar('ready', status.adapter); + } + }) + .catch(error => outputChannel.warn(`Could not record Ecstasy log session: ${error}`)); } const status = stateMap[newState as keyof typeof stateMap]; @@ -154,34 +278,42 @@ export async function startLanguageClient(context: vscode.ExtensionContext, serv updateStatusBar('starting'); - void client.start().catch(err => { + await startingClient.start().catch(err => { + if (client !== connection) throw err; const message = err?.message ?? String(err); - console.warn('XTC Language Server failed to start:', message); - - if (message.includes('UnsupportedClassVersionError') || message.includes('class file version')) { - void vscode.window.showErrorMessage( - 'XTC Language Server requires Java 25+. Set the "xtc.java.home" setting to your Java 25 installation path.', - 'Open Settings' - ).then(choice => { - if (choice === 'Open Settings') { - void vscode.commands.executeCommand('workbench.action.openSettings', 'xtc.java.home'); - } - }); - } + console.warn('Ecstasy Language Server failed to start:', message); updateStatusBar('error'); - client = undefined; + if (client === startingClient) client = undefined; + throw err; }); + await applyTraceConfig(); } -export async function restartLanguageClient(context: vscode.ExtensionContext, serverJar: string, outputChannel: vscode.LogOutputChannel): Promise { - crashCount = 0; - hasEverReachedRunning = false; - await safeStop(); - await startLanguageClient(context, serverJar, outputChannel); +// Restart requests share one operation; settings changes during startup are applied in order. +let restarting: Promise | undefined; +let restartRevision = 0; +export function restartLanguageClient(context: vscode.ExtensionContext, serverJar: string, outputChannel: vscode.LogOutputChannel): Promise { + readServiceSettings(); + runtimeJvmOptions(); // Validate before stopping a working connection. + runtimeLogArguments(); + formattingSettings(); // Reject malformed settings before stopping a valid connection. + restartRevision++; + restarting ??= (async () => { + let applied: number; + do { + applied = restartRevision; + await starting?.catch(() => {}); + await safeStop(); + await startLanguageClient(context, serverJar, outputChannel); + } while (applied !== restartRevision); + })().finally(() => { restarting = undefined; }); + return restarting; } export async function stopLanguageClient(): Promise { + await restarting?.catch(() => {}); + await starting?.catch(() => {}); await safeStop(); } diff --git a/lang/vscode-extension/src/rename-proposal.ts b/lang/vscode-extension/src/rename-proposal.ts new file mode 100644 index 00000000000..ae28897ef2d --- /dev/null +++ b/lang/vscode-extension/src/rename-proposal.ts @@ -0,0 +1,154 @@ +import * as vscode from 'vscode'; +import { LanguageClient, RenameFile, RenameFilesParams, RenameParams, RequestType, TextDocumentEdit, WorkspaceEdit } from 'vscode-languageclient/node'; +import { SourceModule, sourceGraphEdits, sourceGraphKey, sourceModulesIn } from './source-graph-configuration'; + +interface RenameProposal { + edit: WorkspaceEdit; + graph?: { before: SourceModule[]; after: SourceModule[] }; +} +const renameRequest = new RequestType('xtc/rename'); +const moveRequest = new RequestType('xtc/renameFiles'); + +/** VS Code's native Rename saves affected files when files.refactoring.autoSave is enabled. + * Without that save, moving an edited model can lose its text Undo. Reordering the operations + * fixes Undo but breaks Redo, so refuse this unsupported editor configuration before editing. + */ +function editsMovedSource(edit: WorkspaceEdit): boolean { + const changes = edit.documentChanges ?? []; + const moves = changes.filter(RenameFile.is).map(move => new URL(move.oldUri).href.replace(/\/$/, '')); + return changes.filter(TextDocumentEdit.is).some(change => { + const uri = new URL(change.textDocument.uri).href; + return moves.some(from => uri === from || uri.startsWith(from + '/')); + }); +} + +/** Workspace settings only: a refactoring must not rewrite settings shared by other projects. */ +export function compilerSettingsLocation(): { uri: vscode.Uri; path: string[] } | undefined { + if (vscode.workspace.workspaceFile?.scheme === 'file') { + return { uri: vscode.workspace.workspaceFile, path: ['settings', 'xtc.compiler.sourceModules'] }; + } + const folders = vscode.workspace.workspaceFolders; + if (folders?.length !== 1) return undefined; + return { uri: vscode.Uri.joinPath(folders[0].uri, '.vscode', 'settings.json'), path: ['xtc.compiler.sourceModules'] }; +} + +/** Unsaved settings participate in the same transaction and Undo as unsaved source documents. */ +export function compilerSourceModules(): unknown[] | null { + const location = compilerSettingsLocation(); + const document = location && vscode.workspace.textDocuments.find(document => !document.isClosed && document.uri.toString() === location.uri.toString()); + // The document is current even immediately after a save, before VS Code's settings + // watcher has refreshed getConfiguration(). Falling back then briefly reinstalls the old graph. + if (document) { + const value = sourceModulesIn(document.getText(), location!.path); + if (value === null || Array.isArray(value)) return value; + } + return vscode.workspace.getConfiguration('xtc.compiler').get('sourceModules', null); +} + +/** Settings scopes and workspace topology are part of the proposal's immutable input. */ +function configurationState(uri: vscode.Uri): string { + return JSON.stringify({ + workspace: vscode.workspace.workspaceFile?.toString(), + folders: vscode.workspace.workspaceFolders?.map(folder => folder.uri.toString()), + graph: compilerSourceModules(), + overrides: folderOverrides(uri) + }); +} + +function folderOverrides(uri: vscode.Uri): unknown[] { + // In a single-folder window, .vscode/settings.json is the workspace graph itself. + // VS Code also reports it as workspaceFolderValue when queried with that folder's URI. + if (!vscode.workspace.workspaceFile && vscode.workspace.workspaceFolders?.length === 1) return []; + // In a workspace file, each folder can override the graph independently. + const roots = vscode.workspace.workspaceFolders?.map(folder => folder.uri) ?? [uri]; + return roots.map(uri => vscode.workspace.getConfiguration('xtc.compiler', uri).inspect('sourceModules')?.workspaceFolderValue); +} + +export async function renameWithConfiguration( + client: LanguageClient, + document: vscode.TextDocument, + position: vscode.Position, + newName: string, + token: vscode.CancellationToken, +): Promise { + return proposalWithConfiguration(client, document.uri, token, + () => client.sendRequest(renameRequest, { + textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), + position: client.code2ProtocolConverter.asPosition(position), newName + }, token).catch(error => client.handleFailedRequest(renameRequest, token, error, null, false))); +} + +/** File-operation participation contributes only extra edits; VS Code owns the requested moves. */ +export async function moveWithConfiguration(client: LanguageClient, event: vscode.FileWillRenameEvent): Promise { + // TODO VSCODE: UP23 — the participant cannot replace/reorder host moves. Independently + // relocated children must precede parents for apply, but native Undo also replays inverse + // moves in forward order. Overlapping moves need an upstream reversal fix as well as + // a complete replacement transaction; server ordering alone cannot make them safe. + if (!event.files.length) return null; + const settings = compilerSettingsLocation()?.uri.toString(); + const owned = [...(vscode.workspace.workspaceFolders ?? []).map(folder => folder.uri.toString()), ...(settings ? [settings] : [])]; + if (event.files.some(move => owned.some(uri => uri === move.oldUri.toString() || uri.startsWith(move.oldUri.toString().replace(/\/$/, '') + '/')))) { + throw new Error('Moving workspace roots or their settings requires reopening the workspace.'); + } + return proposalWithConfiguration(client, event.files[0].oldUri, event.token, + () => client.sendRequest(moveRequest, { + files: event.files.map(move => ({ oldUri: move.oldUri.toString(), newUri: move.newUri.toString() })) + }, event.token).catch(error => client.handleFailedRequest(moveRequest, event.token, error, null, false)), + event.files.map(move => move.oldUri.toString())); +} + +async function proposalWithConfiguration( + client: LanguageClient, + uri: vscode.Uri, + token: vscode.CancellationToken, + request: () => Promise, + hostMoves: readonly string[] = [], +): Promise { + const documents = vscode.workspace.textDocuments.filter(value => !value.isClosed && value.languageId === 'xtc') + .map(document => ({ document, version: document.version })); + const location = compilerSettingsLocation(); + const config = compilerSourceModules(); + const state = configurationState(uri); + const folders = vscode.workspace.workspaceFolders; + const base = folders?.length === 1 ? folders[0].uri.toString().replace(/\/?$/, '/') : undefined; + const settings = config && location ? await vscode.workspace.openTextDocument(location.uri).then(value => value, () => undefined) : undefined; + const snapshot = settings && { version: settings.version, text: settings.getText() }; + const proposal = await request(); + if (!proposal || token.isCancellationRequested) return null; + if (proposal.graph) { + if (!settings || !snapshot || !location || !Array.isArray(config) + || folderOverrides(uri).some(value => value !== undefined) + || sourceGraphKey(config, base) !== sourceGraphKey(proposal.graph.before, base)) { + throw new Error('This refactoring needs an explicit graph in this workspace’s settings. Global or folder overrides cannot be rewritten safely.'); + } + if (settings.isClosed || settings.version !== snapshot.version || configurationState(uri) !== state) { + throw new Error('Compiler settings changed while calculating the refactoring. Try again.'); + } + const edits = sourceGraphEdits(snapshot.text, location.path, proposal.graph.before, proposal.graph.after, base); + const configurationEdit: TextDocumentEdit = { + textDocument: { uri: settings.uri.toString(), version: snapshot.version }, + edits: edits.map(edit => ({ + range: client.code2ProtocolConverter.asRange(new vscode.Range(settings.positionAt(edit.offset), settings.positionAt(edit.offset + edit.length))), + newText: edit.content + })) + }; + proposal.edit = { ...proposal.edit, documentChanges: [...(proposal.edit.documentChanges ?? []), configurationEdit] }; + } + if (editsMovedSource(proposal.edit) && !vscode.workspace.getConfiguration('files.refactoring').get('autoSave', true)) { + throw new Error('Moving an edited source file requires files.refactoring.autoSave in VS Code so Undo and Redo preserve its contents.'); + } + const originals = new Set(hostMoves.map(uri => new URL(uri).href.replace(/\/$/, ''))); + const edit = { ...proposal.edit, documentChanges: proposal.edit.documentChanges?.filter(change => + !RenameFile.is(change) || !originals.has(new URL(change.oldUri).href.replace(/\/$/, ''))) }; + const converted = await client.protocol2CodeConverter.asWorkspaceEdit(edit, token); + if (token.isCancellationRequested) return null; + if (proposal.graph && (settings?.isClosed || settings?.version !== snapshot?.version || configurationState(uri) !== state)) { + throw new Error('Compiler settings changed while converting the refactoring. Try again.'); + } + if (documents.some(({ document, version }) => document.isClosed || document.version !== version) + || vscode.workspace.textDocuments.filter(value => !value.isClosed && value.languageId === 'xtc').length !== documents.length) { + throw new Error('Source documents changed while calculating the refactoring. Try again.'); + } + if (!client.validateWorkspaceEdit(proposal.edit)) throw new Error('Documents changed while calculating the refactoring. Try again.'); + return converted ?? null; +} diff --git a/lang/vscode-extension/src/runtime-settings.ts b/lang/vscode-extension/src/runtime-settings.ts new file mode 100644 index 00000000000..cb188667964 --- /dev/null +++ b/lang/vscode-extension/src/runtime-settings.ts @@ -0,0 +1,46 @@ +import * as vscode from 'vscode'; + +/** One argument per element; a bounded tuning surface cannot replace the launcher or LSP transport. */ +export function validateJvmOptions(raw: unknown): string[] { + if (!Array.isArray(raw) || raw.some(value => typeof value !== 'string')) throw new Error('JVM options must be an array of strings.'); + const memory = /^(-Xms|-Xmx|-Xss|-XX:MaxMetaspaceSize=|-XX:ReservedCodeCacheSize=)([1-9][0-9]*)([kKmMgG])$/; + const sizes = new Map(); + const keys = raw.map((option: string) => { + const match = memory.exec(option); + if (match) { + const bytes = BigInt(match[2]) * ({ k: 1024n, m: 1048576n, g: 1073741824n }[match[3].toLowerCase()]!); + if (bytes > 9223372036854775807n) throw new Error('JVM memory size is too large.'); + sizes.set(match[1], bytes); + return match[1]; + } + if (/^-XX:\+Use(G1|Serial|Parallel|Z)GC$/.test(option)) return 'collector'; + if (/^-XX:ActiveProcessorCount=[1-9][0-9]*$/.test(option) && Number(option.split('=')[1]) <= 1024) return 'processors'; + throw new Error(`Unsupported JVM option: ${option}. Use heap/stack sizes, MaxMetaspaceSize, ReservedCodeCacheSize, ActiveProcessorCount or a supported collector.`); + }); + if (new Set(keys).size !== keys.length) throw new Error('Duplicate or conflicting JVM options.'); + if (sizes.has('-Xms') && sizes.has('-Xmx') && sizes.get('-Xms')! > sizes.get('-Xmx')!) throw new Error('Initial heap must not exceed maximum heap.'); + return [...raw]; +} + +export function runtimeJvmOptions(): string[] { + // Ignore workspace JSON even if written manually: runtime tuning is machine-local. + return validateJvmOptions(vscode.workspace.getConfiguration('xtc').inspect('java.vmOptions')?.globalValue ?? []); +} + + +export interface LogRetention { historyDays: number; maxFileMb: number; totalSizeMb: number; retainedSessions: number } +export const defaultRetention: Readonly = Object.freeze({ historyDays: 7, maxFileMb: 10, totalSizeMb: 50, retainedSessions: 5 }); +export function validateLogRetention(raw: unknown): LogRetention { + if (raw === undefined) return { ...defaultRetention }; + if (!raw || typeof raw !== 'object' || Array.isArray(raw)) throw new Error('Log retention must be an object.'); + if (Object.keys(raw).some(key => !(key in defaultRetention))) throw new Error('Unknown log retention setting.'); + const value = { ...defaultRetention, ...raw }; + if (!Object.values(value).every(Number.isInteger) || value.historyDays < 1 || value.historyDays > 90 || value.maxFileMb < 1 || value.maxFileMb > 100 || value.totalSizeMb < value.maxFileMb || value.totalSizeMb > 1000 || value.retainedSessions < 1 || value.retainedSessions > 20) throw new Error('Log retention: days 1–90, file MB 1–100, archive MB at least file MB and at most 1000, retired sessions 1–20.'); + return value; +} +export function runtimeLogOptions(): LogRetention { + return validateLogRetention(vscode.workspace.getConfiguration('xtc').inspect('server.logs')?.globalValue); +} +export function runtimeLogArguments(): string[] { + return Object.entries(runtimeLogOptions()).map(([key, value]) => `-Dxtc.logs.${key}=${value}`); +} diff --git a/lang/vscode-extension/src/service-notifications.ts b/lang/vscode-extension/src/service-notifications.ts new file mode 100644 index 00000000000..048900fefaa --- /dev/null +++ b/lang/vscode-extension/src/service-notifications.ts @@ -0,0 +1,11 @@ +import * as vscode from 'vscode'; + +/** Standard notification actions; support export also works after a failed launch. */ +export function serviceFailure(message: string): void { + void vscode.window.showErrorMessage(message, 'Open Settings', 'Show Logs', 'Export Logs').then(choice => { + if (choice === 'Open Settings') return vscode.commands.executeCommand('workbench.action.openSettings', '@ext:xtclang.xtc-language'); + if (choice === 'Show Logs') return vscode.commands.executeCommand('xtc.showServerOutput'); + if (choice === 'Export Logs') return vscode.commands.executeCommand('xtc.exportServerLogs'); + return undefined; + }); +} diff --git a/lang/vscode-extension/src/service-settings.ts b/lang/vscode-extension/src/service-settings.ts new file mode 100644 index 00000000000..a5726852705 --- /dev/null +++ b/lang/vscode-extension/src/service-settings.ts @@ -0,0 +1,35 @@ +/** Connection preferences are window scoped; formatting and presentation use their native scope. */ +export interface ServiceSettings { + readonly adapter: 'default' | 'compiler' | 'treesitter'; + readonly textSynchronization: 'full' | 'incremental'; + readonly saveFormatting: 'editor' | 'server'; + readonly inlayHints: boolean; +} + +export function parseServiceSettings(raw: Record): ServiceSettings { + const adapter = raw.adapter === undefined ? 'default' : raw.adapter; + const textSynchronization = raw.textSynchronization === undefined ? 'full' : raw.textSynchronization; + const saveFormatting = raw.saveFormatting === undefined ? 'editor' : raw.saveFormatting; + const inlayHints = raw.inlayHints === undefined ? true : raw.inlayHints; + if (adapter !== 'default' && adapter !== 'compiler' && adapter !== 'treesitter') { + throw new Error('Language adapter must be default, compiler or treesitter'); + } + if (textSynchronization !== 'full' && textSynchronization !== 'incremental') { + throw new Error('Text synchronization must be full or incremental'); + } + if (saveFormatting !== 'editor' && saveFormatting !== 'server') { + throw new Error('Save formatting must be owned by the editor or server'); + } + if (typeof inlayHints !== 'boolean') { + throw new Error('Inlay hints must be a boolean'); + } + return Object.freeze({ adapter, textSynchronization, saveFormatting, inlayHints }); +} + +/** Native format-on-save wins: never run two formatters for a save. */ +export function synchronizationOptions(settings: ServiceSettings, nativeFormatOnSave: boolean): object { + return { + incremental: settings.textSynchronization === 'incremental', + formatOnSave: settings.saveFormatting === 'server' && !nativeFormatOnSave, + }; +} diff --git a/lang/vscode-extension/src/settings-report.ts b/lang/vscode-extension/src/settings-report.ts new file mode 100644 index 00000000000..aa7a8011b80 --- /dev/null +++ b/lang/vscode-extension/src/settings-report.ts @@ -0,0 +1,33 @@ +import * as vscode from 'vscode'; +import { runtimeJvmOptions, runtimeLogOptions } from './runtime-settings'; + +interface Setting { scope?: string; default?: unknown; deprecationMessage?: string } +interface Group { properties: Record } + +/** Also accepts the original single settings group for older packaged manifests. */ +export function configurationProperties(groups: Group | Group[]): Record { + return Object.assign({}, ...[groups].flat().map(group => group.properties)); +} + +/** Report the scope actually read by the launcher, not an ignored folder or machine override. */ +export function configuredSettings(properties: Record, resource?: vscode.Uri): object { + const window = vscode.workspace.getConfiguration('xtc'); + const document = vscode.workspace.getConfiguration('xtc', resource); + return Object.fromEntries(Object.entries(properties).map(([name, definition]) => { + const key = name.slice('xtc.'.length); + const config = definition.scope === 'resource' ? document : window; + const inspected = config.inspect(key); + const folder = document.inspect(key)?.workspaceFolderValue; + const machine = definition.scope === 'machine'; + const origin = machine ? (inspected?.globalValue !== undefined ? 'user' : 'default') + : definition.scope === 'resource' && folder !== undefined ? 'folder' + : inspected?.workspaceValue !== undefined ? 'workspace' + : inspected?.globalValue !== undefined ? 'user' : 'default'; + const read = () => key === 'java.vmOptions' ? runtimeJvmOptions() + : key === 'server.logs' ? runtimeLogOptions() : config.get(key, definition.default); + const value = (() => { try { return { value: read() }; } catch (error) { return { invalid: String(error) }; } })(); + return [key, { ...value, origin, scope: definition.scope, + ...(machine && inspected?.workspaceValue !== undefined ? { ignoredWorkspaceOverride: true } : {}), + ...(definition.scope !== 'resource' && folder !== undefined ? { ignoredFolderOverride: true } : {}) }]; + })); +} diff --git a/lang/vscode-extension/src/source-graph-configuration.ts b/lang/vscode-extension/src/source-graph-configuration.ts new file mode 100644 index 00000000000..033a1642128 --- /dev/null +++ b/lang/vscode-extension/src/source-graph-configuration.ts @@ -0,0 +1,46 @@ +import { Edit, modify, parse, ParseError } from 'jsonc-parser'; + +export interface SourceModule { name: string; uri: string; dependencies?: string[]; resourceRoots?: string[] | null } + +export function sourceModulesIn(text: string, path: string[]): unknown { + const errors: ParseError[] = []; + const value = parse(text, errors, { allowTrailingComma: true }); + if (errors.length) throw new Error('Fix the workspace settings JSON before renaming a configured module.'); + return path.reduce((current, key) => current?.[key], value); +} + +/** Match the server's URI rules: relative roots require exactly one workspace folder. */ +export function sourceGraphKey(value: unknown, base?: string): string { + if (!Array.isArray(value)) throw new Error('Module rename requires an explicit source graph in workspace settings.'); + const string = (value: unknown): value is string => typeof value === 'string' && value.trim().length > 0; + const modules = value.map(module => { + if (!module || !string(module.name) || !string(module.uri) + || (module.dependencies !== undefined && (!Array.isArray(module.dependencies) || !module.dependencies.every(string))) + || (module.resourceRoots != null && (!Array.isArray(module.resourceRoots) || !module.resourceRoots.every(string)))) { + throw new Error('Source modules require non-blank names, file URIs and dependency names.'); + } + const uri = new URL(module.uri, base); + if (uri.protocol !== 'file:') throw new Error('Source module roots must use file URIs.'); + const resources: string[] | null = module.resourceRoots == null ? null : module.resourceRoots.map((path: string) => { + const root = new URL(path, base); + if (root.protocol !== 'file:') throw new Error('Resource roots must use file URIs.'); + return root.href.replace(/\/+$/, '') + '/'; + }); + if (resources && new Set(resources).size !== resources.length) throw new Error('Duplicate resource roots.'); + return { name: module.name, uri: uri.href, resourceRoots: resources, dependencies: [...new Set(module.dependencies ?? [])].sort() }; + }); + if (new Set(modules.map(module => module.name)).size !== modules.length + || new Set(modules.map(module => module.uri)).size !== modules.length) { + throw new Error('Duplicate source module names or roots.'); + } + return JSON.stringify(modules.sort((left, right) => left.name.localeCompare(right.name))); +} + +/** Preserve comments, workspace folders and unrelated settings; never create an absent graph. */ +export function sourceGraphEdits(text: string, path: string[], before: SourceModule[], after: SourceModule[], base?: string): Edit[] { + if (sourceGraphKey(sourceModulesIn(text, path), base) !== sourceGraphKey(before, base)) { + throw new Error('Compiler source graph changed; rename was not applied.'); + } + sourceGraphKey(after, base); + return modify(text, path, after, { formattingOptions: { insertSpaces: true, tabSize: 4 } }); +} diff --git a/lang/vscode-extension/src/status-bar.ts b/lang/vscode-extension/src/status-bar.ts index a3c943afb3e..24aa2074c2f 100644 --- a/lang/vscode-extension/src/status-bar.ts +++ b/lang/vscode-extension/src/status-bar.ts @@ -7,33 +7,34 @@ export function createStatusBar(): vscode.StatusBarItem { return statusBarItem; } -export function updateStatusBar(state: 'starting' | 'ready' | 'stopped' | 'error'): void { +export function updateStatusBar(state: 'starting' | 'ready' | 'stopped' | 'error', adapter?: string): void { if (!statusBarItem) { return; } switch (state) { case 'starting': - statusBarItem.text = '$(sync~spin) XTC'; - statusBarItem.tooltip = 'XTC Language Server: Starting...'; + statusBarItem.text = '$(sync~spin) Ecstasy'; + statusBarItem.tooltip = 'Ecstasy Language Server: Starting...'; statusBarItem.backgroundColor = undefined; statusBarItem.command = 'xtc.showServerOutput'; break; case 'ready': - statusBarItem.text = '$(check) XTC'; - statusBarItem.tooltip = 'XTC Language Server: Ready'; + const label = adapter === 'XDK' ? 'Compiler' : adapter === 'TreeSitter' ? 'Tree-sitter' : adapter; + statusBarItem.text = `$(check) Ecstasy${label ? ` · ${label}` : ''}`; + statusBarItem.tooltip = `Ecstasy Language Server: Ready${label ? ` (${label})` : ''} — click to switch adapter`; statusBarItem.backgroundColor = undefined; - statusBarItem.command = 'xtc.showServerOutput'; + statusBarItem.command = 'xtc.selectLanguageAdapter'; break; case 'stopped': - statusBarItem.text = '$(error) XTC'; - statusBarItem.tooltip = 'XTC Language Server: Stopped - click to restart'; + statusBarItem.text = '$(error) Ecstasy'; + statusBarItem.tooltip = 'Ecstasy Language Server: Stopped - click to restart'; statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.errorBackground'); statusBarItem.command = 'xtc.restartServer'; break; case 'error': - statusBarItem.text = '$(warning) XTC'; - statusBarItem.tooltip = 'XTC Language Server: Error - click to restart'; + statusBarItem.text = '$(warning) Ecstasy'; + statusBarItem.tooltip = 'Ecstasy Language Server: Error - click to restart'; statusBarItem.backgroundColor = new vscode.ThemeColor('statusBarItem.warningBackground'); statusBarItem.command = 'xtc.restartServer'; break; diff --git a/lang/vscode-extension/src/support-logs.ts b/lang/vscode-extension/src/support-logs.ts new file mode 100644 index 00000000000..bc12e3b7599 --- /dev/null +++ b/lang/vscode-extension/src/support-logs.ts @@ -0,0 +1,105 @@ +import { constants } from 'node:fs'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import { randomUUID } from 'node:crypto'; +import { ZipFile } from 'yazl'; +import type { ExtensionContext, Memento } from 'vscode'; + +const FILE_LIMIT = 512 * 1024; +const FILE_COUNT = 8; +const LAUNCH_LIMIT = 64 * 1024; +const STATE_KEY = 'xtc.supportSession'; +const sessions = new WeakMap(); + +interface Session { + readonly id: string; + readonly started: string; + readonly launcher: string; + readonly logs?: { directory: string; traceDirectory: string }; +} + +export function supportLogs(context: ExtensionContext): SupportLogs { + const existing = sessions.get(context); + if (existing) return existing; + const created = new SupportLogs(context.workspaceState); + sessions.set(context, created); + return created; +} + +/** One immutable snapshot at a time, owned by the extension event loop and workspace storage. */ +export class SupportLogs { + private current: Session; + constructor(private readonly storage: Memento) { + this.current = storage.get(STATE_KEY) ?? { id: '', started: '', launcher: 'No Ecstasy server launch recorded.' }; + } + + begin(description: string): string { + const id = randomUUID(); + this.current = { id, started: new Date().toISOString(), launcher: description.slice(-LAUNCH_LIMIT) }; + this.save(); + return id; + } + + append(id: string, text: string): void { + if (this.current.id === id) this.current = { ...this.current, launcher: (this.current.launcher + text).slice(-LAUNCH_LIMIT) }; + } + + remember(id: string, status: { logs: { directory: string; traceDirectory: string } }): void { + if (this.current.id !== id) return; + this.current = { ...this.current, logs: status.logs }; + this.save(); + } + + finish(id: string, text: string): void { + if (this.current.id !== id) return; + this.append(id, text); + this.save(); + } + + private save(): void { + // VS Code owns persistence ordering. Stale connection callbacks cannot replace this snapshot. + void this.storage.update(STATE_KEY, this.current); + } + + async export(): Promise { return offlineArchive(this.current); } +} + +/** A recorded session only; never look for the newest directory in the shared log root. */ +export async function offlineArchive(session: Session): Promise { + const entries: { name: string; bytes: Buffer; originalBytes: number }[] = []; + const logs = session.logs; + const process = logs && /^server-(\d+-\d+)$/.exec(path.basename(logs.directory))?.[1]; + if (logs && process) { + for (const directory of [...new Set([logs.directory, logs.traceDirectory])]) { + if (!(await fs.lstat(directory).catch(() => undefined))?.isDirectory()) continue; + const files = (await fs.readdir(directory, { withFileTypes: true })).filter(file => file.isFile() && + (directory === logs.directory ? /^(server(?:\.[0-9.-]+)?\.log|lsp-trace-[0-9.-]+\.jsonl)$/.test(file.name) + : file.name.startsWith(`lsp-trace-${process}`) && file.name.endsWith('.jsonl'))); + for (const file of files.sort((a, b) => b.name.localeCompare(a.name))) { + if (entries.length >= FILE_COUNT) break; + const handle = await fs.open(path.join(directory, file.name), constants.O_RDONLY | constants.O_NOFOLLOW).catch(() => undefined); + if (!handle) continue; // A retained log can disappear during pruning. + try { + const stat = await handle.stat(); + if (!stat.isFile()) continue; + const bytes = Buffer.alloc(Math.min(stat.size, FILE_LIMIT)); + const { bytesRead } = await handle.read(bytes, 0, bytes.length, Math.max(0, stat.size - FILE_LIMIT)); + entries.push({ name: `${entries.length}-${file.name}`, bytes: bytes.subarray(0, bytesRead), originalBytes: stat.size }); + } finally { await handle.close(); } + } + } + } + const zip = new ZipFile(); + const chunks: Buffer[] = []; + const result = new Promise((resolve, reject) => { + zip.on('error', reject); + zip.outputStream.on('error', reject).on('data', chunk => chunks.push(chunk)).on('end', () => resolve(Buffer.concat(chunks))); + }); + for (const entry of entries) zip.addBuffer(entry.bytes, entry.name); + zip.addBuffer(Buffer.from(session.launcher).subarray(-LAUNCH_LIMIT), 'launcher.log'); + zip.addBuffer(Buffer.from(JSON.stringify({ schemaVersion: 1, offline: true, started: session.started, + scope: 'Last launch recorded by this workspace; bounded launcher stderr and server log tails. No source files or IDE protocol consoles.', + logs: session.logs, files: entries.map(({ name, bytes, originalBytes }) => ({ entry: name, originalBytes, includedBytes: bytes.length, truncated: originalBytes > bytes.length })) }, null, 2)), 'manifest.json'); + zip.end(); + return result; +} diff --git a/lang/vscode-extension/src/test/archive.ts b/lang/vscode-extension/src/test/archive.ts new file mode 100644 index 00000000000..26cc863a59b --- /dev/null +++ b/lang/vscode-extension/src/test/archive.ts @@ -0,0 +1,19 @@ +import { Entry, fromBuffer } from 'yauzl'; + +/** Read the actual ZIP contents in both extension and shared playbook acceptance tests. */ +export function readArchive(bytes: Buffer): Promise> { + return new Promise((resolve, reject) => fromBuffer(bytes, { lazyEntries: true }, (error, zip) => { + if (error) { reject(error); return; } + const entries = new Map(); + zip.on('error', reject).on('end', () => resolve(entries)); + zip.on('entry', (entry: Entry) => zip.openReadStream(entry, (failure, stream) => { + if (failure) { zip.close(); reject(failure); return; } + const chunks: Buffer[] = []; + stream.on('error', reject).on('data', chunk => chunks.push(chunk)).on('end', () => { + entries.set(entry.fileName, Buffer.concat(chunks)); + zip.readEntry(); + }); + })); + zip.readEntry(); + })); +} diff --git a/lang/vscode-extension/src/test/explorer-move-trace.ts b/lang/vscode-extension/src/test/explorer-move-trace.ts new file mode 100644 index 00000000000..656927ae743 --- /dev/null +++ b/lang/vscode-extension/src/test/explorer-move-trace.ts @@ -0,0 +1,64 @@ +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import type { LanguageClient } from 'vscode-languageclient/node'; +import { focusTestWindow } from './native-focus'; + +/** Trace native commands and events. Observers never waitUntil, apply edits or replay actions. */ +export class ExplorerMoveTrace { + private readonly started = performance.now(); + private readonly events: { milliseconds: number; event: string; data?: unknown }[] = []; + private readonly subscriptions: vscode.Disposable[]; + + constructor(private readonly report: string, root: vscode.Uri, connection?: LanguageClient) { + this.record('window-state', vscode.window.state); + const files = (event: vscode.FileRenameEvent) => event.files.map(file => ({ + oldUri: file.oldUri.toString(), newUri: file.newUri.toString() + })); + const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(root, '**/*')); + this.subscriptions = [watcher, + vscode.window.onDidChangeWindowState(state => this.record('window-state', state)), + vscode.workspace.onWillRenameFiles(event => this.record('willRenameFiles', files(event))), + vscode.workspace.onDidRenameFiles(event => this.record('didRenameFiles', files(event))), + watcher.onDidCreate(uri => this.record('created', uri.toString())), + watcher.onDidDelete(uri => this.record('deleted', uri.toString())), + watcher.onDidChange(uri => this.record('changed', uri.toString()))]; + if (connection) { + const original = connection.sendRequest; + connection.sendRequest = ((...args: unknown[]) => { + const method = typeof args[0] === 'string' ? args[0] : (args[0] as { method?: string })?.method; + const response = Reflect.apply(original, connection, args) as Promise; + if (method !== 'workspace/willRenameFiles') return response; + this.record('lsp-request', args[1]); + return response.then(value => { + this.record('lsp-reply', value); + return value; + }, error => { this.record('lsp-error', String(error)); throw error; }); + }) as typeof connection.sendRequest; + this.subscriptions.push({ dispose: () => { connection.sendRequest = original; } }); + } + } + + record(event: string, data?: unknown): void { + this.events.push({ milliseconds: performance.now() - this.started, event, data }); + } + + async command(id: string, ...args: unknown[]): Promise { + await focusTestWindow(); + this.record('command-start', { id, args }); + try { + const value = await vscode.commands.executeCommand(id, ...args); + this.record('command-end', id); + return value; + } catch (error) { + this.record('command-error', { id, error: error instanceof Error ? error.stack : String(error) }); + throw error; + } + } + + async save(): Promise { + this.subscriptions.forEach(subscription => subscription.dispose()); + await fs.mkdir(path.dirname(this.report), { recursive: true }); + await fs.writeFile(this.report, JSON.stringify({ vscode: vscode.version, events: this.events }, null, 2) + '\n'); + } +} diff --git a/lang/vscode-extension/src/test/explorer-move.ts b/lang/vscode-extension/src/test/explorer-move.ts new file mode 100644 index 00000000000..59ba2c3edec --- /dev/null +++ b/lang/vscode-extension/src/test/explorer-move.ts @@ -0,0 +1,43 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import type { ExplorerMoveTrace } from './explorer-move-trace'; + +/** Native batch move, shared with the extension-free host reproduction. Never replay Paste. */ +export async function cutAndPasteDirectories(sources: readonly vscode.Uri[], destination: vscode.Uri, trace: ExplorerMoveTrace): Promise { + const expected = sources.map(source => source.fsPath).sort(); + const deadline = Date.now() + 30_000; + await trace.command('workbench.files.action.refreshFilesExplorer'); + // Only selection is repeatable: watcher refreshes can retire Explorer nodes before Cut. + for (;;) { + try { + await trace.command('workbench.files.action.focusFilesExplorer'); + await trace.command('workbench.files.action.collapseExplorerFolders'); + await trace.command('revealInExplorer', sources[0]); + for (const _ of sources.slice(1)) await trace.command('list.expandSelectionDown'); + await trace.command('copyFilePath'); + const selected = (await vscode.env.clipboard.readText()).split(/\r?\n/).sort(); + if (JSON.stringify(selected) === JSON.stringify(expected)) { + await trace.command('filesExplorer.cut'); + break; + } + } catch (error) { + if (!String(error).includes('Data tree node not found')) throw error; + } + assert.ok(Date.now() < deadline, `Explorer did not select ${expected.join(', ')}`); + await new Promise(resolve => setTimeout(resolve, 75)); + } + await trace.command('revealInExplorer', destination); + return trace.command('filesExplorer.paste').then( + () => undefined, + (error: unknown) => { + // The host can move successfully, then repaint retired Cut nodes in its finally + // block. Return only that failure so callers can collect Undo/Redo evidence before + // failing. Other errors abort immediately. No VS Code internals are patched. + // TODO VSCODE: UP16 — retire this diagnostic capture after native Paste safely + // clears stale Cut items; the scenario must keep failing while the host throws. + assert.ok(error instanceof Error && error.message.includes('Data tree node not found') + && error.stack?.includes('itemsCopied'), String(error)); + return error; + } + ); +} diff --git a/lang/vscode-extension/src/test/explorer-probe/index.ts b/lang/vscode-extension/src/test/explorer-probe/index.ts new file mode 100644 index 00000000000..1e650c522da --- /dev/null +++ b/lang/vscode-extension/src/test/explorer-probe/index.ts @@ -0,0 +1,75 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { cutAndPasteDirectories } from '../explorer-move'; +import { ExplorerMoveTrace } from '../explorer-move-trace'; +import { catalog } from '../playbook/shared'; + +/** Reproduce X130 using plain text and an empty extension, without Ecstasy or an LSP client. */ +export async function run(): Promise { + const root = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, 'X130'); + const data = catalog.cases.X130.values; + const sources = data.sources; + const trace = new ExplorerMoveTrace(path.join(process.env.XTC_EXPLORER_PROBE_REPORT!, 'move-trace.json'), root); + const completed: string[] = []; + const refreshDuringMove = process.env.XTC_EXPLORER_PROBE_REFRESH === 'true'; + const report = { vscode: vscode.version, ecstasyLoaded: !!vscode.extensions.getExtension('xtclang.xtc-language'), refreshDuringMove, completed }; + // Model a refresh while an asynchronous rename participant is pending. This uses only + // public host APIs and supplies no edit; there is no compiler, delay or vendor patch. + const participant = refreshDuringMove ? vscode.workspace.onWillRenameFiles(event => { + event.waitUntil(trace.command('workbench.files.action.refreshFilesExplorer').then(() => undefined)); + }) : undefined; + try { + assert.strictEqual(report.ecstasyLoaded, false, 'The host probe must not load the Ecstasy extension'); + for (const file of data.files) { + const target = path.join(root.fsPath, file.file); + await fs.mkdir(path.dirname(target), { recursive: true }); + await fs.writeFile(target, file.text); + } + await fs.mkdir(path.join(root.fsPath, data.destination)); + const consumer = await vscode.workspace.openTextDocument(vscode.Uri.joinPath(root, data.consumer)); + await vscode.window.showTextDocument(consumer, { preview: false }); + const failure = await cutAndPasteDirectories(sources.map(source => vscode.Uri.joinPath(root, source)), vscode.Uri.joinPath(root, data.destination), trace); + async function verify(moved: boolean) { + const deadline = Date.now() + 10_000; + for (;;) { + try { await verifyContents(moved); return; } + catch (error) { + if (Date.now() >= deadline) throw error; + // Native command completion precedes filesystem events. Observe only; + // never replay the move, Undo or Redo while waiting for their effects. + await new Promise(resolve => setTimeout(resolve, 75)); + } + } + } + async function verifyContents(moved: boolean) { + for (const folder of sources) { + const current = path.join(root.fsPath, ...(moved ? [data.destination, folder] : [folder])); + assert.ok((await fs.stat(current)).isDirectory()); + const previous = path.join(root.fsPath, ...(moved ? [folder] : [data.destination, folder])); + await assert.rejects(fs.stat(previous), { code: 'ENOENT' }); + } + for (const file of data.files) { + const prefix = moved && sources.some(source => file.file.startsWith(source + '/')) ? [data.destination] : []; + assert.strictEqual(await fs.readFile(path.join(root.fsPath, ...prefix, file.file), 'utf8'), file.text); + } + } + await verify(true); + completed.push('Move and contents'); + for (const [action, moved] of [['undo', false], ['redo', true]] as const) { + await trace.command('workbench.files.action.focusFilesExplorer'); + await trace.command(action); + await verify(moved); + completed.push(action + ' and contents'); + } + if (failure) throw failure; + await fs.writeFile(path.join(process.env.XTC_EXPLORER_PROBE_REPORT!, 'results.json'), JSON.stringify({ ...report, status: 'not-reproduced' }, null, 2) + '\n'); + console.log('Explorer host defect not reproduced in this attempt; this is not proof that X130 is fixed.'); + } catch (error) { + await fs.writeFile(path.join(process.env.XTC_EXPLORER_PROBE_REPORT!, 'results.json'), JSON.stringify({ + ...report, status: 'failed', error: error instanceof Error ? error.stack : String(error) + }, null, 2) + '\n'); + throw error; + } finally { participant?.dispose(); await trace.save(); } +} diff --git a/lang/vscode-extension/src/test/lifecycle/index.ts b/lang/vscode-extension/src/test/lifecycle/index.ts new file mode 100644 index 00000000000..99d5d4a4909 --- /dev/null +++ b/lang/vscode-extension/src/test/lifecycle/index.ts @@ -0,0 +1,113 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { getClient } from '../../lsp-client'; +import { catalog } from '../playbook/shared'; +import { client, eventually } from '../playbook/support'; + +/** Native windows with separate extension hosts; the launcher preserves the reopened profile. */ +export async function run(): Promise { + const directory = process.env.XTC_LIFECYCLE_REPORT!; + const sharedProcess = process.env.XTC_LIFECYCLE_SHARED_PROCESS === 'true'; + const folderName = vscode.workspace.workspaceFolders![0].name; + const previouslyClosed = await fs.stat(path.join(directory, 'closing.json')).then(() => true, error => { + if ((error as NodeJS.ErrnoException).code === 'ENOENT') return false; + throw error; + }); + // Later windows inherit the main process environment. Derive their phase from their workspace + // and the completed close receipt, never from the secondary CLI's environment. + const role = sharedProcess ? (folderName === 'primary' ? 'primary' : previouslyClosed ? 'reopened' : 'closing') + : process.env.XTC_LIFECYCLE_ROLE!; + assert.ok(directory && ['primary', 'closing', 'reopened'].includes(role)); + const applicationPid = process.ppid; + const data = catalog.cases.X145.values; + const folder = vscode.workspace.workspaceFolders![0].uri; + const uri = vscode.Uri.joinPath(folder, data.file); + const type = role === 'primary' ? 'Int' : 'String'; + const value = role === 'primary' ? '7' : '"second"'; + const text = `module ${data.module} {\n static ${type} value = ${value};\n${Array.from({ length: data.methods }, (_, index) => + ` ${type} read${index}() { return value; }`).join('\n')}\n}\n// unsaved ${type}\n`; + const receipt = async (name: string, value: object) => { + const file = path.join(directory, `${name}.json`); + await fs.writeFile(`${file}.tmp`, JSON.stringify({ applicationPid, ...value }, null, 2) + '\n'); + await fs.rename(`${file}.tmp`, file); + }; + const wait = async (name: string) => { + const deadline = Date.now() + 120_000; + while (Date.now() < deadline) { + const failures = (await fs.readdir(directory)).filter(file => file.endsWith('-failure.json')); + assert.deepStrictEqual(failures, [], `Another window failed while waiting for ${name}`); + try { return JSON.parse(await fs.readFile(path.join(directory, `${name}.json`), 'utf8')); } + catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; } + await new Promise(resolve => setTimeout(resolve, 100)); + } + assert.fail(`Missing ${name} lifecycle receipt`); + }; + try { + assert.strictEqual(vscode.workspace.getConfiguration('files').get('hotExit'), 'onExitAndWindowClose', + 'Native window close requires the application-level hot-exit preference'); + const extension = vscode.extensions.all.find(item => item.packageJSON.name === 'xtc-language'); + assert.ok(extension); + await extension.activate(); + const document = await vscode.workspace.openTextDocument(uri); + await vscode.window.showTextDocument(document, { preview: false, preserveFocus: true }); + await eventually(async () => !!getClient()?.initializeResult, Boolean, 'Compiler starts in native window'); + const health = await client().sendRequest<{ adapter: string }>('xtc/healthCheck'); + assert.strictEqual(health.adapter, 'XDK'); + const status = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + const params = { textDocument: { uri: uri.toString() }, position: { line: 1, character: text.split('\n')[1].indexOf('value') } }; + if (role === 'reopened') { + await eventually(async () => ({ restored: document.getText() === text, length: document.getText().length, + dirty: document.isDirty, prefix: document.getText().slice(0, 100) }), value => value.restored, + 'Hot exit restores the actual unsaved source'); + assert.ok(document.isDirty, 'The reopened buffer remains unsaved'); + const previous = await wait('closing'); + assert.notStrictEqual(status.pid, previous.pid); + const hover = await client().sendRequest('textDocument/hover', params); + assert.ok(JSON.stringify(hover).includes('String value')); + await receipt('reopened', { pid: status.pid, previousPid: previous.pid, restored: true, status: 'passed' }); + return; + } + await receipt(`${role}-ready`, { pid: status.pid }); + const other = await wait(role === 'primary' ? 'closing-ready' : 'primary-ready'); + assert.notStrictEqual(status.pid, other.pid, 'Each window owns its compiler process'); + const edit = new vscode.WorkspaceEdit(); + edit.replace(uri, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), text); + assert.ok(await vscode.workspace.applyEdit(edit)); + const pending = client().sendRequest('textDocument/references', { + ...params, context: { includeDeclaration: true } + }); + let completed = false; + const outcome = pending.then(value => ({ value, error: undefined }), (error: unknown) => ({ value: undefined, error })) + .finally(() => { completed = true; }); + if (role === 'closing') { + await eventually(async () => { + assert.ok(!completed, 'Work remains pending until project close'); + return client().sendRequest<{ compilerQueue: { runningSize: number } }>('xtc/languageServiceStatus'); + }, value => value.compilerQueue.runningSize > 0, 'Compiler work started before native close'); + assert.ok(!completed, 'Close overlaps a pending reference request'); + assert.ok(document.isDirty); + await receipt('closing', { pid: status.pid, pending: true, unsaved: true }); + // The native window owns shutdown and hot-exit backup. No client.stop or process kill. + // Keep the other window alive in shared mode; otherwise quit the disposable instance. + await vscode.commands.executeCommand(sharedProcess ? 'workbench.action.closeWindow' : 'workbench.action.quit'); + // Native shutdown owns backup and extension deactivation; this dies with the host. + await new Promise(() => {}); + return; + } + await wait('reopened'); + const result = await outcome; + assert.ifError(result.error); + assert.strictEqual(result.value?.length, data.methods + 1); + assert.strictEqual(document.getText(), text); + assert.ok(document.isDirty); + assert.strictEqual((await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus')).pid, status.pid); + const hover = await client().sendRequest('textDocument/hover', params); + assert.ok(JSON.stringify(hover).includes('Int value')); + await receipt('primary', { pid: status.pid, references: result.value.length, unsavedPreserved: true, status: 'passed' }); + } catch (error) { + await receipt(`${role}-failure`, { error: String(error), stack: (error as Error).stack }); + throw error; + } +} diff --git a/lang/vscode-extension/src/test/native-focus.ts b/lang/vscode-extension/src/test/native-focus.ts new file mode 100644 index 00000000000..cb13e3cec38 --- /dev/null +++ b/lang/vscode-extension/src/test/native-focus.ts @@ -0,0 +1,23 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { WorkbenchUi } from './workbenchUi'; + +/** Native Undo dispatch depends on window focus as well as the selected editor/Explorer. */ +export async function focusTestWindow(): Promise { + if (vscode.window.state.focused) return; + // Activate through the host, without pointer movement or replaying the pending action. + await vscode.commands.executeCommand('workbench.action.focusWindow'); + const deadline = Date.now() + 5_000; + while (!vscode.window.state.focused && Date.now() < deadline) { + await new Promise(resolve => setTimeout(resolve, 25)); + } + assert.ok(vscode.window.state.focused, 'Native action requires the test window to be focused'); +} + +export async function focusTestEditor(document: vscode.TextDocument): Promise { + await focusTestWindow(); + await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false }); + const ui = await WorkbenchUi.connect(); + try { await ui.focusEditor(); } + finally { await ui.close(); } +} diff --git a/lang/vscode-extension/src/test/playbook/advanced.ts b/lang/vscode-extension/src/test/playbook/advanced.ts new file mode 100644 index 00000000000..fbe235a7faf --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/advanced.ts @@ -0,0 +1,530 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { CallHierarchyItem, CallHierarchyOutgoingCall, SemanticTokens, DocumentDiagnosticReport } from 'vscode-languageclient/node'; +import { scenarioRegex, scenarioText } from './shared'; +import { client, diagnostics, fixture, label, noErrors, playbook, position, symbolNames, symbols, targets } from './support'; + +async function assertTokenRanges(document: vscode.TextDocument): Promise { + const tokens = await client().sendRequest('textDocument/semanticTokens/full', { textDocument: { uri: document.uri.toString() } }); + if (!tokens) return; // A failed parse may have no AST or semantic token report. + let line = 0; + let column = 0; + let previousEnd = 0; + for (let i = 0; i < tokens.data.length; i += 5) { + const [deltaLine, deltaColumn, length] = tokens.data.slice(i, i + 3); + line += deltaLine; + column = deltaLine === 0 ? column + deltaColumn : deltaColumn; + assert.ok(deltaLine > 0 || column >= previousEnd, `Overlapping tokens at ${line}:${column}`); + assert.ok(length > 0); + previousEnd = column + length; + } +} + +export function advancedCases(): void { + playbook('X68', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { use, offset, declaration, declarationOffset, length } of data.variants) { + const found = await targets(document, 'Implementation', position(document, use, offset)); + const at = position(document, declaration, declarationOffset); + assert.strictEqual(found.length, data.targetCount); + assert.strictEqual(found[0].uri.toString(), document.uri.toString()); + assert.ok(found[0].range.isEqual(new vscode.Range(at, at.translate(0, length)))); + } + }); + + playbook('X69', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + const found = await targets(document, 'Definition', position(document, data.anchor)); + const at = position(document, data.inheritedDeclaration, data.inheritedOffset); + assert.strictEqual(found.length, data.targetCount); + assert.ok(found[0].range.isEqual(new vscode.Range(at, at.translate(0, data.targetLength)))); + const items = await client().sendRequest('textDocument/prepareCallHierarchy', { + textDocument: { uri: document.uri.toString() }, position: position(document, data.override, data.overrideOffset) + }); + assert.strictEqual(items.length, data.targetCount); + const outgoing = await client().sendRequest('callHierarchy/outgoingCalls', { item: items[0] }); + assert.strictEqual(outgoing.length, data.targetCount); + assert.strictEqual(outgoing[0].to.selectionRange.start.line, at.line); + assert.strictEqual(outgoing[0].to.selectionRange.start.character, at.character); + await assert.rejects(client().sendRequest('textDocument/prepareRename', { + textDocument: { uri: document.uri.toString() }, position: position(document, data.anchor) + }), { message: data.renameRejection }); + }); + + playbook('X70', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + const help = await workspace.signature(document, position(document, data.anchor, data.offset)); + assert.strictEqual(help?.signatures[0].label, data.signature); + const found = await targets(document, 'Definition', position(document, data.anchor)); + assert.strictEqual(found.length, data.targetCount); + assert.strictEqual(document.getText(found[0].range), data.targetName); + }); + + playbook('X71', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const expression of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, expression))); + const found = await workspace.completion(document, position(document, data.anchor, data.offset)); + assert.ok(found.some(item => item.label === data.label), expression); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X72', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { use, getter, getterOffset, setter, setterOffset } of data.variants) { + const found = await targets(document, 'Implementation', position(document, use, data.offset)); + assert.strictEqual(found.length, data.targetCount); + for (const at of [position(document, getter, getterOffset), position(document, setter, setterOffset)]) { + const range = new vscode.Range(at, at.translate(0, data.targetLength)); + assert.ok(found.some(target => target.uri.toString() === document.uri.toString() && target.range.isEqual(range)), use); + } + } + }); + + playbook('X73', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { call, active } of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.incompleteExpression, call))); + const anchor = scenarioText(data.callAnchor, call); + const help = await workspace.signature(document, position(document, anchor, anchor.length)); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter, active); + } + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.invalidExpression)); + const rejected = await workspace.signature(document, position(document, data.invalidCall, data.offset)); + assert.ok(!rejected || rejected.signatures.length === data.rejectedSignatureCount); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X74', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { call, active } of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, call))); + const help = await workspace.signature(document, position(document, call, call.length)); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter, active); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X75', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const expression of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, expression))); + await diagnostics(document.uri, values => values.length > 0, 'Missing delimiter diagnostics'); + const found = await workspace.completion(document, position(document, data.anchor, data.offset)); + assert.ok(found.some(item => item.label === data.label), expression); + assert.ok(vscode.languages.getDiagnostics(document.uri).length > 0, 'Cursor queries preserve diagnostics'); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X76', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + if (data.tokensNonOverlapping) await assertTokenRanges(document); + const expression = data.expression; + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, expression))); + await diagnostics(document.uri, values => values.length > 0, 'Missing nested call diagnostics'); + if (data.tokensNonOverlapping) await assertTokenRanges(document); + const help = await workspace.signature(document, position(document, expression, expression.length)); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter, data.activeParameter); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X77', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { original, prefix } of data.variants) { + await workspace.replace(document, fixture(data.file).replace(original, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Missing argument diagnostics'); + const at = position(document, prefix, prefix.length, true); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(item => item.label).sort(), data.labels, prefix); + const selected = items.find(item => item.label === data.label)!; + assert.ok(selected.range instanceof vscode.Range && selected.range.isEqual(new vscode.Range(at, at))); + await workspace.accept(document, selected); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X78', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const selected of data.selections) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.anchor)); + await diagnostics(document.uri, values => values.length > 0, 'Missing overloaded argument diagnostics'); + const at = position(document, data.anchor, data.offset); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(item => item.label).sort(), data.labels); + assert.strictEqual((await workspace.signature(document, at))?.signatures.length, data.signatureCount); + await workspace.accept(document, items.find(item => item.label === selected)!); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X79', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { original, prefix } of data.variants) { + await workspace.replace(document, fixture(data.file).replace(original, scenarioText(data.replaceFrom, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved argument prefix diagnostics'); + const at = position(document, prefix, prefix.length, true); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(item => item.label), data.labels, prefix); + assert.ok(items[0].range instanceof vscode.Range && items[0].range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + const before = document.getText(); + await workspace.accept(document, items[0]); + assert.strictEqual(document.getText(), before.replace(scenarioText(data.replaceFrom, prefix), scenarioText(data.replaceWith, prefix.slice(0, -data.prefixLength)))); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X80', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const selected of data.labels) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.anchor)); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved overloaded prefix diagnostics'); + const at = position(document, data.anchor, data.offset); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(item => item.label).sort(), data.labels); + assert.strictEqual((await workspace.signature(document, at))?.signatures.length, data.signatureCount); + const chosen = items.find(item => item.label === selected)!; + assert.ok(chosen.range instanceof vscode.Range && chosen.range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + await workspace.accept(document, chosen); + assert.ok(document.getText().includes(scenarioText(data.acceptedCall, selected))); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X81', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const prefix of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved property argument prefix'); + const at = position(document, prefix, prefix.length); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label).sort(), data.labels); + const selected = items.find(item => label(item) === data.label)!; + assert.strictEqual(selected.kind, vscode.CompletionItemKind.Property); + assert.match(selected.detail ?? '', scenarioRegex(data.type)); + assert.ok(selected.range instanceof vscode.Range && selected.range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + await workspace.accept(document, selected); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X82', async (workspace, data) => { + const document = await workspace.open(data.file); + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.anchor)); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved constant argument prefix'); + const at = position(document, data.anchor, data.offset); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + assert.strictEqual(items[0].kind, vscode.CompletionItemKind.Property); + await workspace.accept(document, items[0]); + await noErrors(document.uri); + assert.strictEqual(document.getText(), fixture(data.file)); + }); + + playbook('X83', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const original of data.variants) { + const prefix = original.replace(data.replaceFrom, data.prefix); + await workspace.replace(document, fixture(data.file).replace(original, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved constructor prefix'); + const at = position(document, prefix, prefix.length); + const help = await workspace.signature(document, at); + assert.match(help?.signatures[0].label ?? '', scenarioRegex(data.signature)); + assert.strictEqual(help?.signatures[0].activeParameter ?? help?.activeParameter, data.activeParameter); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + await workspace.accept(document, items[0]); + assert.strictEqual(document.getText(), fixture(data.file)); + await noErrors(document.uri); + } + }); + + playbook('X84', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const original of data.constructors) { + const prefix = original.replace(data.replaceFrom, data.prefix); + await workspace.replace(document, fixture(data.file).replace(original, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved inferred constructor prefix'); + const at = position(document, prefix, prefix.length); + assert.match((await workspace.signature(document, at))?.signatures[0].label ?? '', scenarioRegex(data.signature)); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label).sort(), original.includes(data.requiredContext) ? data.requiredLabels : data.provisionalLabels); + await workspace.accept(document, items.find(item => label(item) === data.label)!); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + }); + + playbook('X85', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const prefix of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved array initializer'); + const at = position(document, prefix, prefix.length); + const help = await workspace.signature(document, at); + assert.strictEqual(help?.signatures[0].activeParameter ?? help?.activeParameter, data.activeParameter); + assert.match(help?.signatures[0].label ?? '', scenarioRegex(data.parameter)); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + await workspace.accept(document, items[0]); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + }); + + playbook('X86', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const expression of data.variants) { + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, expression)); + await diagnostics(document.uri, values => values.length > 0, 'Missing literal delimiter'); + const at = position(document, expression, expression.length); + const item = (await workspace.completion(document, at)).find(item => label(item) === data.label); + assert.ok(item); + assert.ok(item.range instanceof vscode.Range && item.range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + await workspace.accept(document, item); + await diagnostics(document.uri, values => values.length > 0, 'Accepting a member must not repair delimiters'); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + } + }); + + playbook('X87', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const { prefix, suffix, selected, terminator } of data.variants) { + const text = fixture(data.file).replace(scenarioRegex(data.replaceFrom), scenarioText(data.replaceWith, prefix, suffix)); + await workspace.replace(document, text); + await diagnostics(document.uri, values => values.length > 0, 'Incomplete declaration'); + const at = position(document, prefix, prefix.length); + const item = (await workspace.completion(document, at)).find(item => label(item) === selected); + assert.ok(item, prefix); + await workspace.accept(document, item); + await workspace.replace(document, text.replace(prefix, prefix.slice(0, -data.prefixLength) + selected + terminator)); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X88', async (workspace, data) => { + const document = await workspace.open(data.file); + const original = data.original; + const prefix = data.prefix; + await workspace.replace(document, fixture(data.file).replace(original, scenarioText(data.replaceWith, prefix))); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved anonymous constructor argument'); + const at = position(document, prefix, prefix.length); + const help = await workspace.signature(document, at); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter ?? help?.activeParameter, data.activeParameter); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + await workspace.accept(document, items[0]); + assert.strictEqual(document.getText(), fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X89', async (workspace, data) => { + const document = await workspace.open(data.file); + const original = data.original; + const prefix = data.prefix; + for (const closer of data.closers) { + await workspace.replace(document, fixture(data.file).replace(original, prefix + closer)); + await diagnostics(document.uri, values => values.length > 0, 'Incomplete anonymous interface construction'); + const at = position(document, prefix, prefix.length); + const help = await workspace.signature(document, at); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter ?? help?.activeParameter, data.activeParameter); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + assert.ok(items[0].range instanceof vscode.Range && items[0].range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + await workspace.accept(document, items[0]); + if (closer) await noErrors(document.uri); + else await diagnostics(document.uri, values => values.length > 0, 'Missing constructor closer remains a diagnostic'); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + } + }); + + playbook('X90', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + const text = fixture(data.file).replace(data.original, data.prefix + variant.suffix); + await workspace.replace(document, text); + await diagnostics(document.uri, values => values.length > 0, 'Incomplete array size'); + const at = position(document, data.prefix, data.prefix.length); + const help = await workspace.signature(document, at); + assert.strictEqual(help?.signatures[0].label, data.signature); + assert.strictEqual(help?.signatures[0].activeParameter ?? help?.activeParameter, data.activeParameter); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.deepStrictEqual(items.map(label), data.labels); + assert.ok(items[0].range instanceof vscode.Range && items[0].range.isEqual(new vscode.Range(at.translate(0, data.replacementStartDelta), at))); + await workspace.accept(document, items[0]); + const accepted = data.prefix.slice(0, data.replacementStartDelta) + data.labels[0]; + assert.strictEqual(document.getText(), fixture(data.file).replace(data.original, accepted + variant.suffix)); + if (variant.validAfterAcceptance) await noErrors(document.uri); + else await diagnostics(document.uri, values => values.length > 0, 'Missing bracket remains a diagnostic'); + await workspace.replace(document, fixture(data.file).replace(data.original, accepted + variant.repairedSuffix)); + await noErrors(document.uri); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + } + }); + + playbook('X91', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + const marked = scenarioText(data.source, variant); + const cursor = marked.indexOf('|'); + await workspace.replace(document, marked.replace('|', '')); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved declaration type'); + const at = document.positionAt(cursor); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.ok(data.include.every(name => items.some(item => label(item) === name))); + assert.ok(data.exclude.every(name => items.every(item => label(item) !== name))); + assert.ok(!(await workspace.signature(document, at))?.signatures.length); + const selected = items.find(item => label(item) === data.selected)!; + assert.ok(selected.range instanceof vscode.Range && selected.range.isEqual(new vscode.Range(at.translate(0, -data.prefixLength), at))); + await workspace.accept(document, selected); + assert.strictEqual(document.getText(), marked.slice(0, cursor - data.prefixLength) + data.selected + marked.slice(cursor + 1)); + await noErrors(document.uri); + } + }); + + playbook('X92', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const parameters of data.parameters) { + await workspace.replace(document, scenarioText(data.source, parameters)); + await diagnostics(document.uri, values => values.length > 0, 'Unfinished method header'); + const names = symbolNames(await symbols(document)); + assert.ok(data.symbols.every(name => names.includes(name))); + assert.ok(!names.includes(data.excludedSymbol)); + const folds = await vscode.commands.executeCommand('vscode.executeFoldingRangeProvider', document.uri); + assert.ok(folds?.some(fold => fold.start === data.foldStart && fold.end === data.foldEnd)); + await workspace.replace(document, scenarioText(data.source, data.repairedParameters)); + await noErrors(document.uri); + } + }); + + playbook('X93', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + const marked = scenarioText(data.source, variant.declaration); + const cursor = marked.indexOf('|'); + await workspace.replace(document, marked.replace('|', '')); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved declaration type'); + const at = document.positionAt(cursor); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.ok(variant.include.every(name => items.some(item => label(item) === name))); + assert.ok(data.exclude.every(name => items.every(item => label(item) !== name))); + assert.ok(!(await workspace.signature(document, at))?.signatures.length); + const selected = items.find(item => label(item) === variant.selected)!; + assert.ok(selected.range instanceof vscode.Range && selected.range.isEqual(new vscode.Range(at.translate(0, -data.prefixLength), at))); + await workspace.accept(document, selected); + assert.strictEqual(document.getText(), marked.slice(0, cursor - data.prefixLength) + variant.selected + marked.slice(cursor + 1)); + await noErrors(document.uri); + } + }); + + typeHeaderCases(['X94', 'X95', 'X96', 'X97', 'X98']); +} + +export function typeHeaderCases(ids: readonly ('X94' | 'X95' | 'X96' | 'X97' | 'X98' | 'X106' | 'X107' | 'X108')[]): void { + for (const id of ids) { + playbook(id, async (workspace, data) => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + const marked = scenarioText(data.source, variant.declaration); + const cursor = marked.indexOf(data.marker); + const before = 'prefixLength' in variant ? variant.prefixLength : data.prefixLength; + const after = 'suffixLength' in variant ? variant.suffixLength : 0; + await workspace.replace(document, marked.replace(data.marker, '')); + if ('initiallyValid' in variant && variant.initiallyValid) await noErrors(document.uri); + else await diagnostics(document.uri, values => values.length > 0, 'Unresolved nested declaration type'); + const at = document.positionAt(cursor); + const items = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.ok(variant.include.every(name => items.some(item => label(item) === name))); + const excluded = ('exclude' in variant ? variant.exclude : undefined) ?? data.exclude; + assert.ok(excluded.every(name => items.every(item => label(item) !== name))); + const help = await workspace.signature(document, at); + if ('callContext' in data && data.callContext) assert.ok(help?.signatures.length); + else assert.ok(!help?.signatures.length); + if ('activeParameter' in variant) assert.ok(help?.signatures.every(signature => (signature.activeParameter ?? help.activeParameter) === variant.activeParameter)); + const signatureDocumentation = 'signatureDocumentationContains' in variant ? variant.signatureDocumentationContains : undefined; + if (signatureDocumentation) assert.ok(help?.signatures.some(signature => { + const documentation = typeof signature.documentation === 'string' ? signature.documentation : signature.documentation?.value; + return documentation?.includes(signatureDocumentation); + })); + const selected = items.find(item => label(item) === variant.selected)!; + const metadata = 'metadata' in variant ? variant.metadata : undefined; + if (metadata) { + assert.ok(selected.detail?.includes(metadata.detailContains)); + if ('kind' in metadata && typeof metadata.kind === 'number') assert.strictEqual(selected.kind, metadata.kind - 1); // VS Code's enum is zero-based; LSP is one-based. + if (metadata.sorted) assert.ok(selected.sortText?.length); + } + assert.ok(selected.range instanceof vscode.Range && selected.range.isEqual(new vscode.Range(at.translate(0, -before), at.translate(0, after)))); + await workspace.accept(document, selected); + assert.strictEqual(document.getText(), marked.slice(0, cursor - before) + variant.selected + marked.slice(cursor + data.marker.length + after)); + if (variant.validAfterAcceptance) await noErrors(document.uri); + else await diagnostics(document.uri, values => values.length > 0, 'Missing type delimiters remain diagnostics'); + await workspace.replace(document, scenarioText(data.source, variant.repairedDeclaration)); + await noErrors(document.uri); + } + }); + } +} + +export function pullDiagnosticCases(): void { + playbook('X123', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.ok(client().initializeResult?.capabilities.diagnosticProvider); + const params = { textDocument: { uri: document.uri.toString() }, identifier: 'xtc' }; + try { + await workspace.replace(document, data.broken); + await diagnostics(document.uri, values => values.some(value => value.severity === vscode.DiagnosticSeverity.Error), 'Pull errors reach Problems'); + const first = await client().sendRequest('textDocument/diagnostic', params); + assert.strictEqual(first.kind, 'full'); + assert.ok(first.resultId); + const unchanged = await client().sendRequest('textDocument/diagnostic', { ...params, previousResultId: first.resultId }); + assert.strictEqual(unchanged.kind, 'unchanged'); + await workspace.replace(document, data.repaired); + await noErrors(document.uri); + const repaired = await client().sendRequest('textDocument/diagnostic', { ...params, previousResultId: first.resultId }); + assert.strictEqual(repaired.kind, 'full'); + assert.ok(repaired.kind === 'full' && repaired.items.length === 0); + assert.notStrictEqual(repaired.resultId, first.resultId); + } finally { + await workspace.replace(document, fixture(data.file)); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/colors.ts b/lang/vscode-extension/src/test/playbook/colors.ts new file mode 100644 index 00000000000..6c6f1b602f8 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/colors.ts @@ -0,0 +1,156 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { focusTestEditor } from '../native-focus'; +import { WorkbenchUi } from '../workbenchUi'; +import { catalog } from './shared'; +import { client, diagnostics, eventually, noErrors, playbook, symbols } from './support'; + +const scenario = catalog.common.colorPrototype; + +/** Capture the startup theme without repainting the workbench through other themes. */ +export async function captureColorTheme(ui: WorkbenchUi, id: string): Promise { + const theme = vscode.workspace.getConfiguration('workbench').get('colorTheme', 'current'); + await ui.screenshot(`${id}-${theme.replace(/ /g, '-')}`); +} + +function bytes(color: vscode.Color): number[] { + return [color.red, color.green, color.blue, color.alpha].map(channel => Math.round(channel * 255)); +} + +async function colors(document: vscode.TextDocument): Promise { + const values = await vscode.commands.executeCommand('vscode.executeDocumentColorProvider', document.uri) ?? []; + return values.sort((left, right) => left.range.start.compareTo(right.range.start)); +} + +async function renderedColors(ui: WorkbenchUi, expected: number[][]): Promise { + await eventually(() => ui.page.locator('.editor-group-container.active .colorpicker-color-decoration') + .evaluateAll(elements => elements.map(element => { + const css = element.ownerDocument.defaultView!.getComputedStyle(element).backgroundColor; + const channels = css.match(/[\d.]+/g)?.map(Number) ?? []; + return [...channels.slice(0, 3), Math.round((channels[3] ?? 1) * 255)]; + })), values => values.length === expected.length && values.every((color, index) => + color.every((channel, component) => Math.abs(channel - expected[index][component]) <= 1)), + 'Rendered swatches match the current constructor values'); +} + +/** Real workbench hover, sliders and source edits; never calls colorPresentation directly. */ +async function openPicker(ui: WorkbenchUi, index: number): Promise { + const decoration = ui.page.locator('.editor-group-container.active .colorpicker-color-decoration').nth(index); + await decoration.hover({ timeout: 10_000 }); + await ui.page.locator('.colorpicker-body:visible').waitFor({ state: 'visible', timeout: 10_000 }); +} + +async function changeSlider(ui: WorkbenchUi, name: 'opacity' | 'hue'): Promise { + const strip = ui.page.locator(`.colorpicker-body:visible .${name}-strip`); + const bounds = await strip.boundingBox(); + assert.ok(bounds); + const presentation = ui.page.locator('.colorpicker-header:visible .picked-color-presentation'); + const previous = await presentation.textContent(); + // Renderer input leaves the desktop pointer untouched. One click is one native edit. + await strip.click({ position: { x: bounds.width / 2, y: bounds.height / 2 }, timeout: 5_000 }); + const css = await ui.page.locator('.colorpicker-header:visible .picked-color') + .evaluate(element => element.ownerDocument.defaultView!.getComputedStyle(element).backgroundColor); + const rgba = css.match(/[\d.]+/g)?.map(Number); + assert.ok(rgba && rgba.length >= 3, `Native picker color: ${css}`); + const displayed = await eventually(() => presentation.textContent(), + value => value !== previous && /^RGBA\(\d+, \d+, \d+, \d+\)$/.test(value ?? ''), + 'Native picker receives the changed color presentation'); + const selected = displayed!.match(/\d+/g)!.map(Number); + const rendered = [...rgba.slice(0, 3), Math.round((rgba[3] ?? 1) * 255)]; + // CSS serializes alpha with reduced precision. The picker label gives exact byte values; + // independently compare its channels to the rendered selection within one byte. + assert.ok(selected.every((channel, index) => Math.abs(channel - rendered[index]) <= 1), `${displayed}; rendered ${css}`); + return selected; +} + +async function dismiss(ui: WorkbenchUi): Promise { + await vscode.commands.executeCommand('editor.action.hideHover'); + await ui.page.locator('.colorpicker-body:visible').waitFor({ state: 'hidden', timeout: 5_000 }); +} + +function expectedSource(source: string, anchor: string, rgba: number[]): string { + const [red, green, blue, alpha] = rgba; + const replacement = anchor.includes('blue =') + ? `new Rgba(blue = ${blue}, red = ${red}, alpha = ${alpha}, green = ${green})` + : `new Rgba(${red}, ${green}, ${blue}${alpha === 255 ? '' : `, alpha = ${alpha}`})`; + assert.ok(source.includes(anchor)); + return source.replace(anchor, replacement); +} + +export function colorCases(): void { + for (const id of ['X273', 'X274', 'X275'] as const) { + playbook(id, async (workspace, data) => { + assert.strictEqual(client().initializeResult?.capabilities.colorProvider, true, 'Launcher enables the color prototype for this selection'); + const document = await workspace.open(scenario.file, scenario.source); + await noErrors(document.uri); + const editor = await vscode.window.showTextDocument(document); + const config = vscode.workspace.getConfiguration('editor', document.uri); + const previousDecorators = config.inspect('colorDecorators')?.workspaceValue; + await config.update('colorDecorators', true, vscode.ConfigurationTarget.Workspace); + const ui = await WorkbenchUi.connect(); + const rendered = () => ui.page.locator('.editor-group-container.active .colorpicker-color-decoration').count(); + try { + await focusTestEditor(document); + const initial = await colors(document); + assert.deepStrictEqual(initial.map(info => ({ anchor: document.getText(info.range), rgba: bytes(info.color) })), scenario.colors); + await eventually(rendered, count => count === 2, 'Exactly two native color swatches, no dynamic/string swatches'); + await renderedColors(ui, scenario.colors.map(color => color.rgba)); + await ui.screenshot(`${id}-swatches`); + if (data.mode === 'recovery') { + await workspace.replace(document, scenario.source.replace(scenario.invalid.from, scenario.invalid.to)); + await diagnostics(document.uri, values => values.some(value => value.severity === vscode.DiagnosticSeverity.Error), 'Out-of-range channel is diagnosed'); + assert.deepStrictEqual(await colors(document), []); + await eventually(rendered, count => count === 0, 'Failed compilation retires rendered color swatches'); + await workspace.replace(document, scenario.source); + await noErrors(document.uri); + await eventually(rendered, count => count === 2, 'Repair restores both native color swatches'); + await renderedColors(ui, scenario.colors.map(color => color.rgba)); + return; + } + await openPicker(ui, 0); + await ui.screenshot(`${id}-picker`); + if (data.mode === 'history') { + await dismiss(ui); + assert.strictEqual(document.getText(), scenario.source, 'Dismissal without selecting a color preserves all source'); + await openPicker(ui, 0); + } + const changed = await changeSlider(ui, 'opacity'); + assert.notStrictEqual(changed[3], 255, 'The native alpha control changed opacity'); + const expected = expectedSource(scenario.source, scenario.colors[0].anchor, changed); + await eventually(async () => document.getText(), text => text === expected, 'Native picker applies exact source-preserving alpha edit'); + await dismiss(ui); + await noErrors(document.uri); + assert.deepStrictEqual(bytes((await colors(document))[0].color), changed); + await renderedColors(ui, [changed, scenario.colors[1].rgba]); + if (data.mode === 'history') { + await focusTestEditor(document); + await vscode.commands.executeCommand('undo'); + await eventually(async () => document.getText(), text => text === scenario.source, 'One Undo restores the original constructor'); + await symbols(document); + assert.deepStrictEqual(bytes((await colors(document))[0].color), scenario.colors[0].rgba); + await renderedColors(ui, scenario.colors.map(color => color.rgba)); + await vscode.commands.executeCommand('redo'); + await eventually(async () => document.getText(), text => text === expected, 'One Redo restores the picker edit'); + await noErrors(document.uri); + await renderedColors(ui, [changed, scenario.colors[1].rgba]); + } else { + await openPicker(ui, 1); + const named = await changeSlider(ui, 'hue'); + assert.notDeepStrictEqual(named.slice(0, 3), scenario.colors[1].rgba.slice(0, 3)); + const namedSource = expectedSource(expected, scenario.colors[1].anchor, named); + await eventually(async () => document.getText(), text => text === namedSource, 'Named arguments keep their order and labels'); + await dismiss(ui); + await noErrors(document.uri); + assert.deepStrictEqual(bytes((await colors(document))[1].color), named); + await renderedColors(ui, [changed, named]); + await captureColorTheme(ui, id); + } + editor.selection = new vscode.Selection(0, 0, 0, 0); + } finally { + await vscode.commands.executeCommand('editor.action.hideHover'); + await ui.close(); + await config.update('colorDecorators', previousDecorators, vscode.ConfigurationTarget.Workspace); + } + }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/compilerImports.ts b/lang/vscode-extension/src/test/playbook/compilerImports.ts new file mode 100644 index 00000000000..8b7e9ec1ac0 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/compilerImports.ts @@ -0,0 +1,238 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { modelPath, workspaceModelPath } from '../../build-model'; +import { compilerBuildModels, refreshCompilerBuild } from '../../compiler-paths'; +import { updateCompilerConfiguration } from '../../lsp-client'; +import { WorkbenchUi } from '../workbenchUi'; +import { catalog, sharedScenarioPath } from './shared'; +import { client, diagnostics, eventually, noErrors, playbook } from './support'; + +/** Same real Gradle producer and gated outcomes as the IntelliJ driver; no mocked task/progress. */ +export function compilerImportCases(): void { + for (const id of ['X260', 'X261', 'X262', 'X263', 'X264'] as const) { + playbook(id, async (workspace, data) => { + const root = vscode.workspace.workspaceFolders![0].uri.fsPath; + assert.strictEqual(root, path.join(process.env.XTC_PLAYBOOK_REPORT_DIR!, 'workspace')); + const fixture = catalog.common.compilerImport; + const shared = path.dirname(sharedScenarioPath); + const repository = path.resolve(shared, '../../..'); + const files = ['build.gradle.kts', 'settings.gradle.kts', 'gradle.properties', 'gradlew', 'gradlew.bat', + 'gradle/wrapper/gradle-wrapper.jar', 'gradle/wrapper/gradle-wrapper.properties']; + for (const file of files) assert.ok(!await fs.stat(path.join(root, file)).then(() => true, () => false), `Isolated fixture must not replace ${file}`); + const control = path.join(root, '.compiler-import-playbook'); + const report = path.join(root, modelPath); + const aggregate = path.join(root, workspaceModelPath); + const previousAggregate = await fs.readFile(aggregate).catch(() => undefined); + const previousReport = await fs.readFile(report).catch(() => undefined); + const settings = vscode.workspace.getConfiguration('xtc.compiler'); + const previousGraph = settings.inspect('sourceModules')?.workspaceValue; + const ui = await WorkbenchUi.connect(); + async function startImport(command: string) { + const pending = vscode.commands.executeCommand(command); + if (vscode.workspace.workspaceFolders!.length > 1) { + const picker = ui.page.locator('.quick-input-widget:visible'); + await picker.getByPlaceholder('Select workspace folder').waitFor({ state: 'visible', timeout: 10_000 }); + await picker.locator('.monaco-list-row') + .filter({ has: ui.page.getByText(vscode.workspace.workspaceFolders![0].name, { exact: true }) }) + .click({ timeout: 5_000 }); + await picker.waitFor({ state: 'hidden', timeout: 10_000 }); + } + return { pending }; + } + const executions = new Set(); + const tasks = vscode.tasks.onDidStartTask(event => { + if (event.execution.task.definition.type === 'xtc-model') executions.add(event.execution); + }); + const finished = vscode.tasks.onDidEndTask(event => executions.delete(event.execution)); + const initial = JSON.parse(JSON.stringify(fixture.model).split('${workspace}').join(vscode.Uri.file(workspace.directory).toString())); + const replacement = structuredClone(initial); + replacement.sourceSets[0].resourceRoots = []; + const accepted = () => compilerBuildModels().map(model => JSON.stringify(model)); + try { + await fs.rm(aggregate, { force: true }); + await fs.mkdir(control, { recursive: true }); + await fs.writeFile(path.join(control, 'invocations.txt'), ''); + for (const file of files) { + await fs.mkdir(path.dirname(path.join(root, file)), { recursive: true }); + const source = file.includes('gradlew') || file.startsWith('gradle/') ? path.join(repository, file) : path.join(shared, 'compiler-import', file); + await fs.copyFile(source, path.join(root, file)); + } + if (process.platform !== 'win32') await fs.chmod(path.join(root, 'gradlew'), 0o755); + if ('included' in data) { + for (const build of data.included) { + const target = path.join(root, build.folder); + await fs.mkdir(path.join(target, '.compiler-import-playbook'), { recursive: true }); + await fs.copyFile(path.join(shared, 'compiler-import/build.gradle.kts'), path.join(target, 'build.gradle.kts')); + await fs.writeFile(path.join(target, 'settings.gradle.kts'), `rootProject.name = "${build.module}"\n` + + (build.folder === 'included' ? 'includeBuild("nested")\n' : '')); + const nested = JSON.parse(JSON.stringify(fixture.model).split('${workspace}').join(vscode.Uri.file(target).toString()) + .split(fixture.module).join(build.module)); + await fs.writeFile(path.join(target, `${build.module}.x`), fixture.source.replace(fixture.module, build.module)); + await fs.mkdir(path.join(target, 'processed'), { recursive: true }); + await fs.writeFile(path.join(target, fixture.resource), fixture.contents); + const gate = path.join(target, '.compiler-import-playbook'); + await fs.writeFile(path.join(gate, 'model.json'), JSON.stringify(nested)); + await fs.writeFile(path.join(gate, 'request.properties'), 'operation=included\noutput=valid\noutcome=success\n'); + await fs.writeFile(path.join(gate, 'included.release'), 'release\n'); + } + await fs.appendFile(path.join(root, 'settings.gradle.kts'), '\nincludeBuild("included")\n'); + } + await workspace.write(fixture.file, fixture.source); + await workspace.write(fixture.resource, fixture.contents); + await fs.mkdir(path.dirname(report), { recursive: true }); + await fs.writeFile(report, JSON.stringify(initial)); + await settings.update('sourceModules', null, vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + const document = await workspace.open(fixture.file); + await noErrors(document.uri); + const baseline = accepted(); + assert.strictEqual(baseline.length, 1); + for (const step of data.steps) { + const operation = `${id}-${step.operation}`; + console.log(`[${operation}] Starting native Gradle import`); + await fs.writeFile(path.join(control, 'model.json'), JSON.stringify(replacement)); + await fs.writeFile(path.join(control, 'request.properties'), + `operation=${operation}\noutput=${step.output}\noutcome=${step.outcome}\n`); + await vscode.commands.executeCommand('notifications.clearAll'); + const command = step.prepare ? 'xtc.prepareCompilerInputs' : 'xtc.refreshCompilerBuild'; + const pending = 'automatic' in step ? (async () => { + const task = new vscode.Task({ type: 'gradle', operation: `${id}-sync` }, vscode.workspace.workspaceFolders![0], + 'Synchronize compiler fixture', 'Gradle', new vscode.ProcessExecution(path.join(root, 'gradlew'), ['help', '--console=plain'], { cwd: root })); + await vscode.tasks.executeTask(task); + await eventually(async () => fs.stat(aggregate).then(() => true, () => false), Boolean, 'Automatic import publishes its aggregate'); + })() : (await startImport(command)).pending; + await eventually(async () => fs.readFile(path.join(control, `${operation}.started`), 'utf8').catch(() => ''), + value => value === (step.prepare ? 'prepareXtcLspModel' : 'exportXtcLspModel'), 'Real Gradle task published its output and is waiting'); + // Force a consumer read while the task is gated, even if the watcher has not fired. + await updateCompilerConfiguration(); + assert.deepStrictEqual(accepted(), baseline, 'Pending output cannot replace the accepted report'); + await noErrors(document.uri); + if (step.duplicate) { + await (await startImport('xtc.refreshCompilerBuild')).pending; + await ui.page.locator('.notification-list-item').filter({ hasText: 'already running' }).waitFor({ state: 'visible' }); + assert.strictEqual(executions.size, 1, 'Duplicate request must not create another native task'); + } + if (step.cancel) { + await vscode.commands.executeCommand('notifications.showList'); + const notification = ui.page.locator('.notification-list-item').filter({ hasText: 'preparing generated inputs' }); + const button = notification.getByRole('button', { name: 'Cancel', exact: true }); + await button.waitFor({ state: 'visible', timeout: 10_000 }); + await ui.screenshot(`${id}-before-cancel`); + await button.click({ timeout: 5_000 }); + } else await fs.writeFile(path.join(control, `${operation}.release`), 'release\n'); + await pending; + console.log(`[${operation}] Native import returned; checking accepted inputs`); + await eventually(async () => executions.size, count => count === 0, 'Owned native task has ended'); + // Retire a Gradle daemon action even if the client process was terminated first. + await fs.writeFile(path.join(control, `${operation}.release`), 'cleanup\n'); + const producer = Number(await fs.readFile(path.join(control, `${operation}.pid`), 'utf8')); + assert.ok(Number.isSafeInteger(producer) && producer > 0); + await eventually(async () => { + if (await fs.stat(path.join(control, `${operation}.finished`)).then(() => true, () => false)) return true; + // Native cancellation may kill the producer JVM, so its finally cannot run. + try { process.kill(producer, 0); return false; } + catch (error) { return step.cancel && (error as NodeJS.ErrnoException).code === 'ESRCH'; } + }, Boolean, 'Gradle fixture action retired or its cancelled JVM exited'); + await updateCompilerConfiguration(); + await client().sendRequest('xtc/healthCheck'); + if (step.accepted) { + assert.notDeepStrictEqual(accepted(), baseline); + if ('included' in data) { + assert.strictEqual(compilerBuildModels()[0].sourceSets.length, data.included.length + 1); + assert.strictEqual(compilerBuildModels()[0].buildRoots?.length, data.included.length + 1); + } + await diagnostics(document.uri, values => values.length > 0, 'Successful retry accepts the model without resource roots'); + } else { + assert.deepStrictEqual(accepted(), baseline, 'Rejected output remains rejected after later reads'); + await noErrors(document.uri); + } + await vscode.commands.executeCommand('xtc.showCompilerPaths'); + await eventually(async () => vscode.workspace.textDocuments.filter(item => item.uri.scheme === 'output').map(item => item.getText()).join('\n'), + text => text.includes('Last import:') && text.toLowerCase().includes(step.expected), 'Paths view records the completed import outcome'); + assert.strictEqual((await fs.readFile(path.join(control, 'invocations.txt'), 'utf8')).split('\n').filter(line => line.startsWith(`${operation}:`)).length, 1); + assert.strictEqual(document.getText(), fixture.source); + } + if (id === 'X264') { + await fs.writeFile(aggregate, '{'); + assert.doesNotThrow(() => compilerBuildModels()); + await fs.writeFile(aggregate, JSON.stringify(initial)); + // No explicit configuration publication: the shipping filesystem watcher owns it. + await noErrors(document.uri); + assert.strictEqual(settings.inspect('sourceModules')?.workspaceValue, null); + } + } finally { + executions.forEach(execution => execution.terminate()); + for (const step of data.steps) await fs.writeFile(path.join(control, `${id}-${step.operation}.release`), 'cleanup\n'); + tasks.dispose(); finished.dispose(); + await vscode.commands.executeCommand('notifications.hideList'); + await ui.close(); + for (const file of files) await fs.rm(path.join(root, file), { force: true }); + if (previousReport) await fs.writeFile(report, previousReport); else await fs.rm(report, { force: true }); + if (previousAggregate) await fs.writeFile(aggregate, previousAggregate); else await fs.rm(aggregate, { force: true }); + if ('included' in data) await fs.rm(path.join(root, 'included'), { recursive: true, force: true }); + await settings.update('sourceModules', previousGraph, vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + } + }); + } + playbook('X265', async (workspace, data) => { + const primary = vscode.workspace.workspaceFolders![0]; + const root = path.join(path.dirname(primary.uri.fsPath), 'retired-import'); + const fixture = catalog.common.compilerImport; + const shared = path.dirname(sharedScenarioPath); + const repository = path.resolve(shared, '../../..'); + const control = path.join(root, '.compiler-import-playbook'); + await fs.mkdir(control, { recursive: true }); + for (const file of ['build.gradle.kts', 'settings.gradle.kts', 'gradle.properties', 'gradlew', 'gradlew.bat', + 'gradle/wrapper/gradle-wrapper.jar', 'gradle/wrapper/gradle-wrapper.properties']) { + await fs.mkdir(path.dirname(path.join(root, file)), { recursive: true }); + await fs.copyFile(file.includes('gradlew') || file.startsWith('gradle/') ? path.join(repository, file) : + path.join(shared, 'compiler-import', file), path.join(root, file)); + } + if (process.platform !== 'win32') await fs.chmod(path.join(root, 'gradlew'), 0o755); + const model = JSON.parse(JSON.stringify(fixture.model).split('${workspace}').join(vscode.Uri.file(root).toString())); + await fs.writeFile(path.join(root, fixture.file), fixture.source); + await fs.mkdir(path.join(root, 'processed'), { recursive: true }); + await fs.writeFile(path.join(root, fixture.resource), fixture.contents); + await fs.mkdir(path.dirname(path.join(root, modelPath)), { recursive: true }); + await fs.writeFile(path.join(root, modelPath), JSON.stringify(model)); + await fs.writeFile(path.join(control, 'model.json'), JSON.stringify(model)); + await fs.writeFile(path.join(control, 'request.properties'), `operation=${data.operation}\noutput=valid\noutcome=success\n`); + const original = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + const uri = vscode.Uri.file(root); + const added = new Promise(resolve => { + const listener = vscode.workspace.onDidChangeWorkspaceFolders(event => { + if (event.added.some(folder => folder.uri.toString() === uri.toString())) { listener.dispose(); resolve(); } + }); + }); + assert.ok(vscode.workspace.updateWorkspaceFolders(vscode.workspace.workspaceFolders!.length, 0, { uri })); + await added; + const secondary = vscode.workspace.workspaceFolders!.find(folder => folder.uri.toString() === uri.toString())!; + const pending = refreshCompilerBuild(false, secondary); + try { + await eventually(async () => fs.stat(path.join(control, `${data.operation}.started`)).then(() => true, () => false), Boolean, 'Secondary import is pending'); + assert.ok(vscode.workspace.updateWorkspaceFolders(secondary.index, 1)); + await eventually(async () => !vscode.workspace.workspaceFolders?.some(folder => folder.uri.toString() === uri.toString()), Boolean, 'Folder removed through native workspace API'); + await pending; + await fs.writeFile(path.join(control, `${data.operation}.release`), 'late completion\n'); + const producer = Number(await fs.readFile(path.join(control, `${data.operation}.pid`), 'utf8')); + await eventually(async () => { + if (await fs.stat(path.join(control, `${data.operation}.finished`)).then(() => true, () => false)) return true; + try { process.kill(producer, 0); return false; } + catch (error) { return (error as NodeJS.ErrnoException).code === 'ESRCH'; } + }, Boolean, 'Removed folder producer retired'); + assert.ok(compilerBuildModels().flatMap(item => item.sourceSets).every(entry => entry.projectDirectory !== model.sourceSets[0].projectDirectory)); + assert.strictEqual((await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus')).pid, original.pid); + await workspace.write('Survivor.x', 'module Survivor {}\n'); + const survivor = await workspace.open('Survivor.x'); + await noErrors(survivor.uri); + } finally { + await fs.writeFile(path.join(control, `${data.operation}.release`), 'cleanup\n'); + const remaining = vscode.workspace.workspaceFolders?.find(folder => folder.uri.toString() === uri.toString()); + if (remaining) vscode.workspace.updateWorkspaceFolders(remaining.index, 1); + await pending.catch(() => {}); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/completion.ts b/lang/vscode-extension/src/test/playbook/completion.ts new file mode 100644 index 00000000000..d1bf8ce031b --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/completion.ts @@ -0,0 +1,174 @@ +import * as assert from 'node:assert'; +import { SignatureHelp } from 'vscode-languageclient/node'; +import { editScenario, scenarioOffset, scenarioRegex, scenarioText, shared } from './shared'; +import { client, fixture, label, noErrors, playbook, position, targets } from './support'; + +export function completionCases(): void { + playbook('X6', async (workspace, data) => { + for (const body of data.bodies) { + const { document, at } = await workspace.editing(body); + const items = await workspace.completion(document, at); + const item = items.find(value => label(value) === data.label); + assert.ok(item, JSON.stringify(items)); + assert.match(item.detail ?? '', scenarioRegex(data.type)); + assert.ok(!items.some(value => label(value) === data.excludedLabel)); + await workspace.accept(document, item); + assert.ok(document.getText().includes(data.acceptedExpression)); + await workspace.replace(document, document.getText().replace(data.acceptedExpression, data.validStatement)); + await noErrors(document.uri); + } + }); + + playbook('X7', async (workspace, data) => { + const scenario = shared.completion; + const document = await workspace.open(scenario.file); + const incomplete = editScenario(fixture(scenario.file), scenario.edit); + await workspace.replace(document, incomplete); + const at = document.positionAt(scenarioOffset(incomplete, scenario.cursor)); + const item = (await workspace.completion(document, at)).find(value => label(value) === scenario.label); + assert.ok(item); + await workspace.accept(document, item); + assert.strictEqual(document.getText(), editScenario(incomplete, scenario.accepted)); + await noErrors(document.uri); + }); + + playbook('X8', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const item = (await workspace.completion(document, at)).find(value => label(value) === data.label); + assert.ok(item); + await workspace.accept(document, item); + assert.ok(document.getText().includes(data.acceptedSuffix)); + await noErrors(document.uri); + }); + + playbook('X9', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const names = (await workspace.completion(document, at)).map(label); + for (const name of data.include) { assert.ok(names.includes(name), names.join(', ')); } + for (const name of data.exclude) { assert.ok(!names.includes(name), names.join(', ')); } + }); + + playbook('X10', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const before = document.getText(); + const offset = document.offsetAt(at); + const item = (await workspace.completion(document, at)).find(value => label(value) === data.label); + assert.ok(item); + await workspace.accept(document, item); + assert.strictEqual(document.getText(), before.slice(0, offset) + data.label + before.slice(offset)); + }); + + playbook('X11', async (workspace, data) => { + const first = await workspace.editing(data.body, true); + const initial = await workspace.completion(first.document, first.at); + for (const name of data.include) { assert.ok(initial.some(item => label(item) === name)); } + assert.match(initial.find(item => label(item) === data.label)?.detail ?? '', scenarioRegex(data.genericType)); + const second = await workspace.editing(data.shadowedBody, true); + const shadowed = await workspace.completion(second.document, second.at); + assert.match(shadowed.find(item => label(item) === data.label)?.detail ?? '', scenarioRegex(data.shadowedType)); + assert.ok(!shadowed.some(item => label(item) === data.excludedLabel)); + }); + + playbook('X12', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const names = (await workspace.completion(document, at)).map(label); + for (const name of data.include) { assert.ok(names.includes(name), names.join(', ')); } + for (const name of data.exclude) { assert.ok(!names.includes(name), names.join(', ')); } + }); + + playbook('X13', async (workspace, data) => { + for (const { prefix, name, addition } of data.variants) { + const { document, at } = await workspace.editing(scenarioText(data.body, prefix), false, + text => text.replace(data.moduleStart, scenarioText(data.moduleWithImport, addition))); + assert.ok((await workspace.completion(document, at)).some(item => label(item) === name), name); + } + }); + + playbook('X14', async (workspace, data) => { + const text = fixture(data.file).replace(data.method, + data.incompleteMethod).replace(scenarioRegex(data.lineBreak), '\r\n'); + // Load the CRLF file from disk: inserting CRLF text into an LF document normalizes it. + const document = await workspace.open(data.file, text.replace('§', '')); + const at = document.positionAt(text.indexOf('§')); + const before = document.getText(); + const item = (await workspace.completion(document, at)).find(value => label(value) === data.label); + assert.ok(item); + await workspace.accept(document, item); + assert.strictEqual(document.getText(), before.replace(data.prefix, data.accepted)); + assert.ok(document.getText().includes('\r\n')); + await noErrors(document.uri); + }); + + playbook('X15', async (workspace, data) => { + for (const { argument, type } of data.variants) { + const { document, at } = await workspace.editing(scenarioText(data.body, argument)); + const result = await workspace.signature(document, at); + assert.ok(result); + assert.strictEqual(result?.signatures.length, data.expected, JSON.stringify(result)); + assert.match(result.signatures[0].label, new RegExp(type)); + assert.strictEqual(result.signatures[0].activeParameter ?? result.activeParameter, data.expected); + } + }); + + playbook('X16', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const result = await workspace.signature(document, at); + assert.ok(result); + assert.strictEqual(result?.signatures.length, data.expected); + assert.match(result.signatures[0].label, scenarioRegex(data.type)); + assert.strictEqual(result.signatures[0].activeParameter ?? result.activeParameter, data.activeParameter); + await workspace.replace(document, document.getText().replace(data.replaceFrom, data.replaceWith)); + await noErrors(document.uri); + assert.strictEqual((await targets(document, 'Definition', position(document, data.call, data.callOffset))).length, data.expected); + assert.ok((await workspace.signature(document, position(document, data.completedArgument, data.argumentOffset)))?.signatures.length); + }); + + playbook('X17', async (workspace, data) => { + for (const { body, type } of data.variants) { + const { document, at } = await workspace.editing(body); + const result = await workspace.signature(document, at); + assert.ok(result?.signatures.length); + assert.match(result.signatures[0].label, new RegExp(scenarioText(data.typePattern, type))); + } + }); + + playbook('X18', async (workspace, data) => { + for (const body of data.bodies) { + const { document, at } = await workspace.editing(body); + assert.strictEqual((await workspace.signature(document, at))?.signatures.length ?? data.signatureCount, data.signatureCount, body); + } + }); + + playbook('X19', async (workspace, data) => { + for (const instance of (data.receivers as (boolean)[])) { + const { document, at } = await workspace.editing(instance ? data.instanceBody : data.staticBody, instance, + text => text.replace(data.replaceFrom, data.replaceWith)); + const result = await workspace.signature(document, at); + assert.ok(result?.signatures.length); + assert.strictEqual(result.signatures[0].activeParameter ?? result.activeParameter, data.activeParameter); + const offset = document.offsetAt(at); + await workspace.replace(document, document.getText().slice(0, offset) + (instance ? data.instanceArgument : data.staticArgument) + document.getText().slice(offset)); + await noErrors(document.uri); + } + }); + + playbook('X20', async (workspace, data) => { + const incomplete = await workspace.editing(data.body); + // LSP defaults the active index to zero. The server suppresses parameter metadata + // in this ambiguous slot, retaining the signature label without a guessed highlight. + const result = await client().sendRequest('textDocument/signatureHelp', { + textDocument: { uri: incomplete.document.uri.toString() }, position: incomplete.at + }); + assert.ok(result?.signatures.length); + assert.deepStrictEqual(result.signatures[0].parameters, []); + const locations = []; + for (const body of data.bodies) { + const { document } = await workspace.editing(body); + await noErrors(document.uri); + const selected = await targets(document, 'Definition', position(document, data.anchor, data.offset)); + assert.strictEqual(selected.length, data.targetCount); + locations.push(selected[0]); + } + assert.ok(!locations[0].range.isEqual(locations[1].range)); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/dependencies.ts b/lang/vscode-extension/src/test/playbook/dependencies.ts new file mode 100644 index 00000000000..1171acf61a7 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/dependencies.ts @@ -0,0 +1,173 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { catalog, editScenario, scenarioOffset, scenarioRegex, shared } from './shared'; +import { client, diagnosticCode, diagnostics, eventually, fixture, noErrors, playbook, position, symbols, targets, Workspace } from './support'; + +const { stringLibrary, brokenLibrary } = catalog.common.dependency; + +async function blocked(workspace: Workspace): Promise { + await diagnostics(workspace.uri(catalog.common.dependency.consumer), values => values.some(item => diagnosticCode(item) === catalog.common.dependency.blockedCode), 'Blocked consumer'); +} + +async function linked(document: vscode.TextDocument, workspace: Workspace): Promise { + await symbols(document); + await noErrors(document.uri); + const location = shared.dependencyNavigation.location; + const result = await targets(document, 'Definition', document.positionAt(scenarioOffset(document.getText(), location.cursor))); + assert.strictEqual(result.length, 1); + assert.strictEqual(result[0].uri.toString(), workspace.uri(location.targetFile).toString()); +} + +export function dependencyCases(): void { + playbook('X45', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const location = shared.dependencyNavigation.location; + assert.ok(!vscode.window.visibleTextEditors.some(editor => editor.document.uri.toString() === workspace.uri(location.targetFile).toString())); + await linked(consumer, workspace); + const result = await targets(consumer, 'Definition', consumer.positionAt(scenarioOffset(consumer.getText(), location.cursor))); + const target = await vscode.workspace.openTextDocument(result[0].uri); + assert.strictEqual(target.offsetAt(result[0].range.start), scenarioOffset(target.getText(), location.target)); + }); + + playbook('X46', async (workspace, data) => { + const scenario = shared.dependencyEdit; + const consumer = await workspace.dependencies(); + assert.strictEqual(consumer.uri.toString(), workspace.uri(scenario.consumer).toString()); + const version = consumer.version; + const library = await workspace.open(scenario.file); + await workspace.replace(library, editScenario(fixture(scenario.file), scenario.edit)); + const result = await diagnostics(consumer.uri, values => values.length > 0, 'Consumer type mismatch'); + assert.ok(result.every(item => diagnosticCode(item) !== data.diagnosticCode)); + assert.strictEqual(consumer.version, version); + assert.strictEqual(await fs.readFile(library.uri.fsPath, 'utf8'), fixture(scenario.file)); + await workspace.replace(library, fixture(scenario.file)); + await noErrors(consumer.uri); + }); + + playbook('X47', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const library = await workspace.open(data.file); + await workspace.replace(library, brokenLibrary); + await diagnostics(library.uri, values => values.some(item => diagnosticCode(item).startsWith(data.compilerCodePrefix)), 'Library compiler diagnostic'); + await blocked(workspace); + assert.deepStrictEqual(await targets(consumer, 'Definition', position(consumer, data.anchor, data.offset)), []); + await workspace.replace(library, fixture(data.file)); + await noErrors(library.uri); + await linked(consumer, workspace); + }); + + playbook('X48', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const library = await workspace.open(data.file); + await workspace.replace(library, stringLibrary); + await diagnostics(consumer.uri, values => values.length > 0, 'Consumer type mismatch'); + await workspace.discard(library); + await linked(consumer, workspace); + assert.strictEqual((await workspace.open(data.file)).getText(), fixture(data.file)); + }); + + playbook('X49', async (workspace, data) => { + const consumer = await workspace.dependencies(); + await fs.unlink(workspace.uri(data.file).fsPath); + await diagnostics(workspace.uri(data.file), values => values.some(item => diagnosticCode(item) === data.diagnosticCode), 'Missing dependency source'); + await blocked(workspace); + await workspace.write(data.file); + await linked(consumer, workspace); + await noErrors(workspace.uri(data.file)); + }); + + playbook('X50', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const uri = workspace.uri(data.file); + const text = data.text; + try { + await client().sendNotification('textDocument/didOpen', { textDocument: { uri: uri.toString(), languageId: 'xtc', version: data.version, text } }); + await diagnostics(uri, values => values.length > 0, 'Virtual member diagnostic'); + await blocked(workspace); + } finally { + await client().sendNotification('textDocument/didClose', { textDocument: { uri: uri.toString() } }); + } + await linked(consumer, workspace); + await noErrors(uri); + await workspace.write(data.file, text); + await diagnostics(uri, values => values.length > 0, 'Saved member diagnostic'); + await blocked(workspace); + await fs.unlink(uri.fsPath); + await linked(consumer, workspace); + await noErrors(uri); + }); + + playbook('X51', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const unrelated = await workspace.open(data.unrelatedFile); + const library = await workspace.open(data.libraryFile); + const queries = []; + for (let index = 0; index < 10; index++) { + await workspace.replace(library, index % 2 ? fixture(data.libraryFile) : brokenLibrary, false); + queries.push(targets(consumer, 'Definition', position(consumer, data.anchor, data.offset))); + } + await Promise.all(queries); + await linked(consumer, workspace); + await noErrors(unrelated.uri); + assert.ok((await symbols(unrelated)).length); + }); + + playbook('X52', async (workspace, data) => { + const bridgeText = data.bridgeText; + await workspace.write(data.libraryFile); + await workspace.write(data.bridgeFile, bridgeText); + await workspace.write(data.consumerFile, fixture(data.consumerFile).replace(data.replaceFrom, data.bridgeImport)); + await workspace.configure(data.sourceModules.map(module => ({ ...module, uri: workspace.uri(module.uri).toString() }))); + const consumer = await workspace.open(data.consumerFile); + await noErrors(consumer.uri); + const library = await workspace.open(data.libraryFile); + await workspace.replace(library, stringLibrary); + await diagnostics(workspace.uri(data.bridgeFile), values => values.length > 0, 'Transitive type mismatch'); + await blocked(workspace); + await workspace.replace(library, brokenLibrary); + await blocked(workspace); + await workspace.replace(library, fixture(data.libraryFile)); + await noErrors(consumer.uri); + const bridge = await workspace.open(data.bridgeFile); + await workspace.replace(bridge, data.brokenBridge); + await blocked(workspace); + await workspace.replace(bridge, bridgeText); + await noErrors(consumer.uri); + assert.strictEqual((await targets(consumer, 'Definition', position(consumer, data.anchor, data.offset)))[0].uri.toString(), bridge.uri.toString()); + }); +} + +export function configurationCases(): void { + playbook('CFG1', async (workspace, data) => { + const consumer = await workspace.dependencies(); + assert.strictEqual(consumer.uri.toString(), workspace.uri(shared.configuration.consumer).toString()); + await workspace.configure([]); + await diagnostics(consumer.uri, values => values.length > 0, 'Removed dependency graph'); + await workspace.configureSharedGraph(); + await linked(consumer, workspace); + }); + + playbook('CFG2', async (workspace, data) => { + const consumer = await workspace.dependencies(); + // Observe the error notification as well as unchanged navigation: otherwise a query + // could race ahead of processing the rejected configuration. + let rejected = false; + const registration = client().onNotification('window/showMessage', (message: { message: string }) => { + if (scenarioRegex(data.pattern).test(message.message)) { rejected = true; } + }); + try { + await workspace.configure(data.sourceModules.map(module => ({ ...module, uri: workspace.uri(module.uri).toString() }))); + await eventually(async () => rejected, Boolean, 'Invalid configuration rejected'); + await linked(consumer, workspace); + } finally { registration.dispose(); } + }); + + playbook('CFG3', async (workspace, data) => { + const consumer = await workspace.dependencies(); + const version = consumer.version; + await client().sendNotification('workspace/didChangeConfiguration', { settings: null }); + await linked(consumer, workspace); + assert.strictEqual(consumer.version, version); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/documentation.ts b/lang/vscode-extension/src/test/playbook/documentation.ts new file mode 100644 index 00000000000..b7ac50aa86c --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/documentation.ts @@ -0,0 +1,32 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { focusTestEditor } from '../native-focus'; +import { discovered } from './liveWorkspace'; +import { eventually, noErrors, playbook, position } from './support'; + +export function documentationCases(): void { + playbook('X278', async (workspace, data) => { + await workspace.write(data.file, data.source); + await discovered(workspace, async () => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + const actions = async () => vscode.commands.executeCommand( + 'vscode.executeCodeActionProvider', document.uri, + new vscode.Range(position(document, data.anchor), position(document, data.anchor)), + vscode.CodeActionKind.Source.value, 100); + const found = await eventually(actions, values => !!values?.some(action => action.title === data.title), 'Compiler documentation action'); + const action = found!.find(action => action.title === data.title)!; + assert.ok(action.edit); + assert.ok(await vscode.workspace.applyEdit(action.edit)); + assert.strictEqual(document.getText(), data.expected); + await noErrors(document.uri); + assert.ok(!(await actions())?.some(action => action.title === data.title), 'Existing documentation is retained'); + for (const [command, expected] of [['undo', data.source], ['redo', data.expected], ['undo', data.source]]) { + await focusTestEditor(document); + await vscode.commands.executeCommand(command); + await eventually(async () => document.getText(), text => text === expected, `${command} documentation`); + await noErrors(document.uri); + } + }); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/editingClosure.ts b/lang/vscode-extension/src/test/playbook/editingClosure.ts new file mode 100644 index 00000000000..9140e9ec152 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/editingClosure.ts @@ -0,0 +1,102 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { client, diagnostics, noErrors, playbook, position, symbolNames, symbols } from './support'; + +export function editingClosureCases(ids: readonly ('X158' | 'X243' | 'X244')[] = ['X158']): void { + ids.forEach(id => playbook(id, async (workspace, data) => { + await workspace.write(data.libraryFile, data.library); + await workspace.write(data.file, data.source); + await workspace.configure([ + { name: data.libraryModule, uri: workspace.uri(data.libraryFile).toString() }, + { name: data.module, uri: workspace.uri(data.file).toString(), dependencies: [data.libraryModule] } + ]); + const document = await workspace.open(data.file); + if (id === 'X244') { + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Unsupported conditional import syntax'); + } else { + await noErrors(document.uri); + } + const links = await vscode.commands.executeCommand('vscode.executeLinkProvider', document.uri, 100); + assert.deepStrictEqual(links?.map(link => document.getText(link.range)), data.linkNames); + assert.ok(links?.every(link => link.target?.fsPath === workspace.uri(data.libraryFile).fsPath)); + if (data.alias) { + const result = await client().sendRequest('textDocument/linkedEditingRange', { + textDocument: { uri: document.uri.toString() }, position: position(document, data.alias) + }); + assert.deepStrictEqual(result.ranges.map(range => document.offsetAt(new vscode.Position(range.start.line, range.start.character))), + data.aliasUses.map(anchor => data.source.indexOf(anchor))); + } + if (links?.length) { + const library = await vscode.workspace.openTextDocument(links[0].target!); + await vscode.window.showTextDocument(library); + assert.strictEqual(library.getText(), data.library); + } + })); +} + +export function linkedScopeCases(): void { + (['X245', 'X246', 'X247'] as const).forEach(id => playbook(id, async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + for (const query of data.queries) { + const result = await client().sendRequest('textDocument/linkedEditingRange', { + textDocument: { uri: document.uri.toString() }, position: position(document, query.anchor) + }); + const starts = result?.ranges.map(range => document.offsetAt(new vscode.Position(range.start.line, range.start.character))) ?? []; + assert.deepStrictEqual(starts, query.occurrences.map(anchor => data.source.indexOf(anchor))); + assert.strictEqual(document.getText(), data.source); + } + })); +} + +export function structuralRecoveryCases(): void { + playbook('X248', async (workspace, data) => { + const document = await workspace.open(data.file, data.variants[0].source); + for (const variant of data.variants) { + await workspace.replace(document, variant.source); + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Current damaged source diagnostics'); + const names = symbolNames(await symbols(document)); + assert.ok(variant.symbols.every(name => names.includes(name))); + const folds = await vscode.commands.executeCommand('vscode.executeFoldingRangeProvider', document.uri); + assert.ok(folds?.some(fold => fold.start === variant.foldLine && fold.end >= variant.foldLine + 1)); + const at = position(document, variant.anchor); + const ranges = await vscode.commands.executeCommand('vscode.executeSelectionRangeProvider', document.uri, [at]); + let selection: vscode.SelectionRange | undefined = ranges?.[0]; + assert.ok(selection?.parent); + while (selection) { + assert.ok(selection.range.contains(at)); + if (selection.parent) { + assert.ok(selection.parent.range.contains(selection.range)); + assert.ok(!selection.parent.range.isEqual(selection.range)); + } + selection = selection.parent; + } + } + await workspace.replace(document, data.repaired); + await noErrors(document.uri); + assert.deepStrictEqual(symbolNames(await symbols(document)), ['Structure', 'repaired']); + }); +} + +export function formattingBreadthCases(): void { + (['X249', 'X250'] as const).forEach(id => playbook(id, async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + await vscode.commands.executeCommand('editor.action.formatDocument'); + assert.strictEqual(document.getText(), data.expected); + await noErrors(document.uri); + const edits = await client().sendRequest('textDocument/formatting', { + textDocument: { uri: document.uri.toString() }, + options: { tabSize: 4, insertSpaces: true, insertFinalNewline: true } + }); + assert.deepStrictEqual(edits, []); + await vscode.commands.executeCommand('editor.action.formatDocument'); + assert.strictEqual(document.getText(), data.expected); + await vscode.commands.executeCommand('undo'); + assert.strictEqual(document.getText(), data.source); + await vscode.commands.executeCommand('redo'); + assert.strictEqual(document.getText(), data.expected); + await vscode.commands.executeCommand('undo'); + assert.strictEqual(document.getText(), data.source); + })); +} diff --git a/lang/vscode-extension/src/test/playbook/graph.ts b/lang/vscode-extension/src/test/playbook/graph.ts new file mode 100644 index 00000000000..db1fa92e1cb --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/graph.ts @@ -0,0 +1,237 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { LSPErrorCodes, Moniker, TextDocumentEdit, WorkspaceEdit } from 'vscode-languageclient/node'; +import { catalog, scenarioRegex, scenarioText } from './shared'; +import { client, diagnostics, eventually, fixture, hover, noErrors, playbook, position, symbols, targets, Workspace } from './support'; + +async function graph(workspace: Workspace): Promise { + const setup = catalog.common.graph; + for (const module of setup.sourceModules) { await workspace.write(module.uri); } + await workspace.configure(setup.sourceModules.map(module => ({ ...module, uri: workspace.uri(module.uri).toString() }))); + const document = await workspace.open(setup.root); + await noErrors(document.uri); + return document; +} + +async function rename(document: vscode.TextDocument, anchor: string, name: string): Promise { + // Creation/configuration notifications may legitimately retire a captured graph. Retry only + // explicit cancellation/content-modified responses, never an absent or incorrect semantic answer. + const result = await eventually(async () => { + try { + const value = await client().sendRequest('textDocument/rename', { + textDocument: { uri: document.uri.toString() }, position: position(document, anchor), newName: name + }); + return { value }; + } catch (error) { + if ([LSPErrorCodes.RequestCancelled, LSPErrorCodes.ContentModified].includes((error as { code: number }).code)) { return undefined; } + throw error; + } + }, value => value !== undefined, 'Graph rename completes for current inputs'); + return result!.value; +} + +async function references(document: vscode.TextDocument): Promise { + return await vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, position(document, catalog.common.graph.referenceAnchor)) ?? []; +} + +export function graphCases(): void { + playbook('X59', async (workspace, data) => { + const document = await graph(workspace); + const result = await references(document); + assert.strictEqual(result.length, data.referenceCount, 'Interface declaration and two interface-dispatch calls; concrete override is a different identity'); + assert.deepStrictEqual(result.map(item => item.uri.toString()).sort(), + data.modules.map(name => workspace.uri(scenarioText(data.moduleFile, name)).toString()).sort()); + assert.ok(!vscode.workspace.textDocuments.some(item => item.uri.toString() === workspace.uri(data.closedConsumer).toString()), 'Consumer remains unopened'); + }); + + playbook('X60', async (workspace, data) => { + const document = await graph(workspace); + const proposed = await rename(document, data.anchor, data.replaceWith); + assert.ok(proposed?.documentChanges?.length === data.moduleCount && !proposed.changes); + for (const change of proposed.documentChanges) { + assert.ok(TextDocumentEdit.is(change)); + assert.strictEqual(change.textDocument.version, change.textDocument.uri === document.uri.toString() ? document.version : null); + } + const edit = await vscode.commands.executeCommand('vscode.executeDocumentRenameProvider', document.uri, position(document, data.anchor), data.replaceWith); + assert.ok(edit?.size === data.moduleCount && await vscode.workspace.applyEdit(edit)); + for (const name of data.modules) { + const changed = await workspace.open(scenarioText(data.file, name)); + await symbols(changed); + await noErrors(changed.uri); + assert.strictEqual(changed.getText(), fixture(scenarioText(data.file, name)).replace(scenarioRegex(data.replaceFrom), data.replaceWith)); + assert.strictEqual(await fs.readFile(changed.uri.fsPath, 'utf8'), fixture(scenarioText(data.file, name)), 'Proof and editor edits do not save source files'); + } + }); + + playbook('X61', async (workspace, data) => { + const document = await graph(workspace); + assert.strictEqual(await rename(document, data.anchor, data.newName), null); + assert.strictEqual(document.getText(), fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X62', async (workspace, data) => { + await graph(workspace); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const signature = await workspace.signature(document, position(document, data.searchCall, data.searchCall.length)); + assert.ok(signature?.signatures.some(item => scenarioRegex(data.searchName).test(item.label) && scenarioRegex(data.searchArgumentType).test(item.label))); + assert.strictEqual(await rename(document, data.searchCall, data.searchRename), null); + assert.match(await hover(document, position(document, data.descriptionCall)), scenarioRegex(data.descriptionType)); + assert.strictEqual(await rename(document, data.descriptionCall, data.descriptionRename), null); + }); + + playbook('X63', async (workspace, data) => { + const document = await graph(workspace); + const consumer = await workspace.open(data.file); + await workspace.replace(consumer, fixture(data.file).replace(data.replaceFrom, data.addedReference)); + assert.strictEqual((await references(document)).filter(item => item.uri.toString() === consumer.uri.toString()).length, data.consumerReferenceCount); + const edit = await rename(document, data.anchor, data.newName); + assert.ok(edit?.documentChanges); + const change = edit.documentChanges.find(item => TextDocumentEdit.is(item) && item.textDocument.uri === consumer.uri.toString()); + assert.ok(change && TextDocumentEdit.is(change)); + assert.strictEqual(change.textDocument.version, consumer.version); + await workspace.replace(consumer, data.replaceWith); + await diagnostics(consumer.uri, values => values.length > 0, 'Incomplete graph diagnostic'); + assert.deepStrictEqual(await references(document), []); + assert.strictEqual(await rename(document, data.anchor, data.newName), null); + await noErrors(document.uri); + await workspace.replace(consumer, fixture(data.file)); + await noErrors(consumer.uri); + assert.strictEqual((await references(document)).length, data.referenceCount); + }); +} + +export function indexingCases(): void { + playbook('X251', async (workspace, data) => { + const roots = Array.from({ length: data.roots }, (_, index) => ({ + name: scenarioText(data.rootName, index), uri: workspace.uri(scenarioText(data.rootFile, index)).toString(), dependencies: [] as string[] + })); + for (let index = 0; index < data.roots; index++) { + await workspace.write(scenarioText(data.rootFile, index), scenarioText(data.rootSource, index)); + } + await workspace.write(data.consumer.uri, data.consumer.source); + const graph = [...roots, { ...data.consumer, uri: workspace.uri(data.consumer.uri).toString() }]; + await workspace.configure(graph); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const refs = async () => await vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, position(document, data.anchor)) ?? []; + const search = async (query: string) => await vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider', query) ?? []; + assert.strictEqual((await refs()).length, data.referenceCount); + assert.ok(!vscode.workspace.textDocuments.some(item => item.uri.toString() === workspace.uri(data.consumer.uri).toString()), 'Consumer remains closed'); + assert.strictEqual((await search(data.symbol)).length, data.roots); + assert.strictEqual((await search(data.symbol)).length, data.roots); + await workspace.replace(document, document.getText().replace(data.replaceFrom, data.replacement)); + await noErrors(document.uri); + assert.strictEqual((await search(data.addedSymbol)).length, 1); + assert.strictEqual((await refs()).length, data.referenceCount); + const last = data.roots - 1; + const broken = await workspace.open(scenarioText(data.rootFile, last)); + await workspace.replace(broken, scenarioText(data.brokenSource, last)); + await diagnostics(broken.uri, items => items.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Broken independent root'); + assert.deepStrictEqual(await refs(), []); + assert.strictEqual((await search(data.addedSymbol)).length, 1); + await workspace.replace(broken, scenarioText(data.rootSource, last)); + await noErrors(broken.uri); + assert.strictEqual((await refs()).length, data.referenceCount); + await workspace.configure(graph.filter(item => item.name !== roots[last].name)); + await eventually(() => search(data.symbol), items => items.length === data.roots - 1, 'Removed root leaves the index'); + await workspace.configure(graph); + await eventually(() => search(data.symbol), items => items.length === data.roots, 'Restored root returns to the index'); + }); + +} + +export function monikerCases(): void { + const monikers = async (uri: vscode.Uri, text: string, anchor: string) => { + const offset = text.indexOf(anchor); + assert.ok(offset >= 0, anchor); + return await client().sendRequest('textDocument/moniker', { + textDocument: { uri: uri.toString() }, + position: { line: text.slice(0, offset).split('\n').length - 1, character: offset - text.lastIndexOf('\n', offset) - 1 } + }); + }; + playbook('X252', async (workspace, data) => { + await workspace.write(data.libraryFile, data.librarySource); + await workspace.write(data.consumerFile, data.consumerSource); + await workspace.configure(data.sourceModules.map(module => ({ ...module, uri: workspace.uri(module.uri).toString() }))); + const consumer = await workspace.open(data.consumerFile); + await noErrors(consumer.uri); + const imported = (await monikers(consumer.uri, consumer.getText(), data.reference))[0]; + const exported = (await monikers(workspace.uri(data.libraryFile), data.librarySource, data.declaration))[0]; + assert.ok(imported && exported); + assert.strictEqual(imported.scheme, data.scheme); + assert.strictEqual(imported.unique, data.unique); + assert.strictEqual(exported.kind, 'export'); + assert.deepStrictEqual(imported, { ...exported, kind: 'import' }); + const overload = (await monikers(consumer.uri, consumer.getText(), data.overload))[0]; + assert.ok(overload && overload.identifier !== imported.identifier); + const library = await workspace.open(data.libraryFile); + await noErrors(library.uri); + assert.deepStrictEqual(await monikers(library.uri, library.getText(), data.declaration), [exported]); + await workspace.replace(library, data.librarySource.replace(data.replaceFrom, data.replaceWith)); + await noErrors(consumer.uri); + const changed = (await monikers(library.uri, library.getText(), data.declaration))[0]; + assert.ok(changed && changed.identifier !== exported.identifier); + assert.deepStrictEqual(await monikers(consumer.uri, consumer.getText(), data.reference), [{ ...changed, kind: 'import' }]); + await workspace.replace(library, data.librarySource); + await noErrors(consumer.uri); + assert.deepStrictEqual(await monikers(consumer.uri, consumer.getText(), data.reference), [imported]); + }); + playbook('X253', async (workspace, data) => { + await workspace.write(data.file, data.source); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const own = (await monikers(document.uri, document.getText(), data.private))[0]; + const bundled = (await monikers(document.uri, document.getText(), data.library))[0]; + assert.ok(own && bundled); + assert.strictEqual(own.kind, 'local'); + assert.strictEqual(bundled.kind, 'import'); + assert.strictEqual(bundled.scheme, data.scheme); + assert.strictEqual(bundled.unique, data.unique); + assert.deepStrictEqual(await monikers(document.uri, document.getText(), data.local), []); + await workspace.replace(document, data.source.replace(data.replaceFrom, data.replaceWith)); + await diagnostics(document.uri, items => items.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Broken moniker source'); + assert.deepStrictEqual(await monikers(document.uri, document.getText(), data.private), []); + await workspace.replace(document, data.source); + await noErrors(document.uri); + assert.deepStrictEqual(await monikers(document.uri, document.getText(), data.private), [own]); + assert.deepStrictEqual(await monikers(document.uri, document.getText(), data.library), [bundled]); + }); +} + +export function libraryContentCases(): void { + playbook('X254', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + assert.ok(client().initializeResult?.capabilities.workspace?.textDocumentContent?.schemes.includes(data.scheme)); + for (const name of data.types) { + const at = position(document, name); + const imported = await client().sendRequest('textDocument/moniker', { + textDocument: { uri: document.uri.toString() }, position: at + }); + const [target] = await targets(document, 'Definition', at); + assert.strictEqual(target.uri.scheme, data.scheme); + const library = await vscode.workspace.openTextDocument(target.uri); + await vscode.window.showTextDocument(library); + const text = library.getText(); + assert.strictEqual(library.getText(target.range), name); + const supplied = await client().sendRequest<{ text: string }>('workspace/textDocumentContent', { uri: target.uri.toString() }); + assert.strictEqual(text.replace(/\r\n/g, '\n'), supplied.text.replace(/\r\n/g, '\n')); + const exported = await client().sendRequest('textDocument/moniker', { + textDocument: { uri: target.uri.toString() }, position: target.range.start + }); + assert.deepStrictEqual(exported, [{ ...imported[0], kind: 'export' }]); + assert.strictEqual(exported[0].scheme, data.monikerScheme); + // TextEditor.edit is an extension API and can bypass editor read-only input handling. + // Exercise the user's typing command, as the manual playbook requires. + await vscode.commands.executeCommand('default:type', { text: '// forbidden\n' }); + assert.strictEqual(library.getText(), text); + assert.strictEqual(library.isDirty, false); + const edits = await vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', target.uri, + { tabSize: 4, insertSpaces: true }); + assert.deepStrictEqual(edits ?? [], []); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/highlighting.ts b/lang/vscode-extension/src/test/playbook/highlighting.ts new file mode 100644 index 00000000000..e5fa0cbfcdf --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/highlighting.ts @@ -0,0 +1,103 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { SemanticTokens, SemanticTokensOptions } from 'vscode-languageclient/node'; +import { WorkbenchUi } from '../workbenchUi'; +import { captureColorTheme } from './colors'; +import { client, diagnostics, eventually, noErrors, playbook } from './support'; + +async function tokens(document: vscode.TextDocument) { + const legend = (client().initializeResult!.capabilities.semanticTokensProvider as SemanticTokensOptions).legend; + const result = await client().sendRequest('textDocument/semanticTokens/full', { textDocument: { uri: document.uri.toString() } }); + const data = result?.data ?? []; + let line = 0; + let column = 0; + return Array.from({ length: data.length / 5 }, (_, index) => { + const [lines, columns, length, type, mask] = data.slice(index * 5, index * 5 + 5); + line += lines; + column = (lines ? 0 : column) + columns; + assert.ok(line < document.lineCount && column + length <= document.lineAt(line).text.length, 'Current-source token span'); + const start = document.offsetAt(new vscode.Position(line, column)); + return { start, end: start + length, text: document.getText().slice(start, start + length), type: legend.tokenTypes[type], + modifiers: legend.tokenModifiers.filter((_, bit) => mask & (1 << bit)) }; + }); +} + +/** Read rendered attributes per character, independently of Monaco's span splits. */ +async function lexicalStyles(ui: WorkbenchUi, anchors: string[]): Promise { + return eventually(() => ui.page.locator('.editor-group-container.active .view-lines .view-line').evaluateAll((lines, wanted) => { + const rows = lines.map(line => { + const walker = line.ownerDocument.createTreeWalker(line, line.ownerDocument.defaultView!.NodeFilter.SHOW_TEXT); + let text = ''; + const styles: string[] = []; + while (walker.nextNode()) { + const node = walker.currentNode; + const value = (node.textContent ?? '').replace(/\u00a0/g, ' '); + const css = line.ownerDocument.defaultView!.getComputedStyle(node.parentElement!); + text += value; + styles.push(...Array.from(value, () => `${css.color}/${css.fontStyle}/${css.fontWeight}/${css.textDecorationLine}`)); + } + return { text, styles }; + }); + return wanted.map(anchor => { + const row = rows.find(item => item.text.includes(anchor)); + const start = row?.text.indexOf(anchor) ?? -1; + return row ? row.styles.slice(start, start + anchor.length) : []; + }); + }, anchors), values => values.every((styles, index) => styles.length === anchors[index].length), 'Lexical fragments are rendered'); +} + +export function highlightingCases(): void { + playbook('X276', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const actual = await tokens(document); + for (const expected of data.tokens) { + const start = data.source.indexOf(expected.anchor) + expected.offset; + const token = actual.find(item => item.start === start); + assert.ok(token, expected.anchor); + assert.strictEqual(token.text, expected.text); + assert.strictEqual(token.type, expected.type); + assert.strictEqual(token.modifiers.includes('defaultLibrary'), expected.library); + } + const ui = await WorkbenchUi.connect(); + try { await ui.screenshot('X276-current-theme'); } finally { await ui.close(); } + }); + + playbook('X277', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const before = await tokens(document); + for (const anchor of data.lexical) { + const start = data.source.indexOf(anchor); + assert.ok(!before.some(token => token.start < start + anchor.length && token.end > start), `No semantic overlay on ${anchor}`); + } + const ui = await WorkbenchUi.connect(); + try { + for (const pair of data.matchingStyles) { + const [control, expression] = await lexicalStyles(ui, [pair.left, pair.right]); + assert.deepStrictEqual(expression.slice(0, pair.length), control.slice(0, pair.length), + `Expression bodies preserve lexical styling: ${pair.right}`); + } + const styles = await lexicalStyles(ui, data.lexical); + await fs.writeFile(path.join(process.env.XTC_PLAYBOOK_REPORT_DIR!, 'X277-lexical-styles.json'), JSON.stringify({ + theme: vscode.workspace.getConfiguration('workbench').get('colorTheme'), + semanticHighlighting: vscode.workspace.getConfiguration('editor', { uri: document.uri, languageId: document.languageId }).get('semanticHighlighting.enabled'), + anchors: data.lexical, styles + }, null, 2) + '\n'); + await captureColorTheme(ui, 'X277'); + for (const edit of data.recovery) { + await workspace.replace(document, data.source.replace(edit.from, edit.to)); + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Damaged source reports an error'); + const current = await tokens(document); + for (const token of current.filter(item => item.type !== 'comment')) assert.match(token.text, /^[A-Za-z_][A-Za-z_0-9]*$/); + await workspace.replace(document, data.source); + await noErrors(document.uri); + assert.deepStrictEqual(await tokens(document), before, 'Repair restores exact classifications'); + } + } finally { + await ui.close(); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/index.ts b/lang/vscode-extension/src/test/playbook/index.ts new file mode 100644 index 00000000000..a8d8b30688b --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/index.ts @@ -0,0 +1,88 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import Mocha from 'mocha'; +import * as vscode from 'vscode'; +import { showTestProgress } from '../progress'; +import { stopOnUnsafeFailure } from '../wait'; +import { selectedScenarioIds, sharedScenarioHash, sharedScenarioIds, sharedScenarioPath } from './shared'; +import { cases } from './support'; + +async function hostResults() { + const root = path.resolve(__dirname, '../../../../lsp-server/build/test-results'); + return Promise.all(['test', 'compilerStdioTest'].map(async task => { + const directory = path.join(root, task); + const files = (await fs.readdir(directory).catch(() => [] as string[])).filter(file => /^TEST-.*Xdk.*\.xml$/.test(file)); + const suites = await Promise.all(files.map(async file => { + const xml = await fs.readFile(path.join(directory, file), 'utf8'); + const attributes = Object.fromEntries([...xml.slice(0, xml.indexOf('>', xml.indexOf(' [match[1], match[2]])); + return { file: path.join(directory, file), timestamp: attributes.timestamp, + tests: Number(attributes.tests), failures: Number(attributes.failures), errors: Number(attributes.errors), skipped: Number(attributes.skipped) }; + })); + return { task, status: !suites.length ? 'not-run' : suites.some(suite => suite.failures || suite.errors || suite.skipped) ? 'failed-or-skipped' : 'passed', suites }; + })); +} + +export async function run(): Promise { + const directory = process.env.XTC_PLAYBOOK_REPORT_DIR; + assert.ok(directory, 'Launch with npm run test:playbook or the Gradle playbook task'); + const selected = selectedScenarioIds(process.env.XTC_PLAYBOOK_CASES); + const mocha = new Mocha({ ui: 'tdd', color: true, timeout: 90_000 }); + const ids = selected.map(id => id.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|'); + mocha.grep(new RegExp(`(?:^| )(?:${ids}):`)); + mocha.addFile(path.join(__dirname, 'playbook.test.js')); + const results: { id: string; title: string; status: string; durationMs?: number; error?: string }[] = []; + const failures = await new Promise(resolve => { + const runner = mocha.run(resolve); + stopOnUnsafeFailure(runner); + showTestProgress(runner, 'Ecstasy playbook', test => test.title); + runner.on('pass', test => { + results.push({ id: test.title.split(':')[0], title: test.title, status: 'passed', durationMs: test.duration }); + }); + runner.on('fail', (test, error) => { + results.push({ id: test.title.split(':')[0], title: test.title, status: 'failed', error: error.stack }); + console.error(`${test.title}\n${error.stack}`); + }); + runner.on('pending', test => { + results.push({ id: test.title.split(':')[0], title: test.title, status: 'not-run' }); + }); + }); + const report = { + commit: process.env.XTC_PLAYBOOK_COMMIT, dirtyPaths: process.env.XTC_PLAYBOOK_DIRTY, + vscode: vscode.version, finished: new Date().toISOString(), failures, + progressCancellation: process.env.XTC_PLAYBOOK_CANCEL_UI === 'true' ? 'visible-control' : 'native-token', + sharedScenarios: { file: sharedScenarioPath, sha256: sharedScenarioHash, ids: sharedScenarioIds }, + selection: { mode: selected.length === sharedScenarioIds.length ? 'full' : 'focused', ids: selected }, + cases: [...cases].map(([id, description]) => ({ ...description, id, + ...(results.find(result => result.id === id) ?? { status: selected.some(value => value === id) ? 'not-run' : 'not-selected' }) })), + errors: results.filter(result => !cases.has(result.id)), + scope: 'VS Code extension-host/provider checks; X145 --cancel-ui and X260 activate visible Cancel controls, X259 inspects rendered inlay labels, and X273–X275 exercise native color swatches, picker controls and Undo/Redo. X154/X273/X276/X277 capture the startup theme without changing it; separate X277 --theme/--lexical-baseline launches compare dark/light/high-contrast styles; X276 checks resolved annotations and library ownership, and X277 checks lexical styles and damaged-source recovery. X278 applies the compiler documentation action with native Undo/Redo; X279 clicks rendered reference lenses, opens native reference peek and checks live settings. Other visual appearance and physical key/menu interaction remain manual.', + hostChecks: await hostResults(), + hostChecksNote: 'Existing Gradle XML evidence; inspect timestamps. Focused editor runs do not rerun host checks.', + supportingCoverage: { + '7a.1–7a.2': 'XdkStdioTest: packaged compiler with absent and invalid XDK_HOME', + '7a.3–7a.4': 'Case timings and XdkRetentionTest latency output; interactive cold/warm log interpretation remains manual', + '7a.5–7a.6': 'X29/X30/X51 and controlled XdkCursorServerTest cancellation; queue-depth/log presentation remains manual', + '7a.7': 'XdkRetentionTest bounded release checks; a prolonged interactive memory/GC soak remains manual', + '7a.10–7a.14': 'X3–X4, X6–X20 and X33–X42', + 'Dependency host API': 'XdkDependencyTest and XdkLanguageServerTest (Gradle test task)', + 'Automatic source recompilation host API': 'XdkProjectTest and XdkProjectServerTest (Gradle test task)', + 'Configured graph and binary contracts': 'XdkProjectQueryTest and X59–X63; includes bundled XDK member resolution and rename rejection', + 'Property/accessor implementations': 'XdkSemanticLookupTest, XdkDependencyTest, packaged stdio and X64–X67; field/default/mixin bodies, source ownership and unsupported controls', + 'Deterministic rename/cursor races': 'XdkProjectQueryLifecycleTest, XdkCursorServerTest and XdkRenameServerTest (Gradle test task)' + } + }; + await fs.writeFile(path.join(directory, 'results.json'), JSON.stringify(report, null, 2) + '\n'); + await fs.writeFile(path.join(directory, 'results.txt'), [ + `Compiler playbook at ${report.commit}; VS Code ${report.vscode}`, + `Selection: ${report.selection.mode} (${selected.join(', ')})`, + ...report.cases.map(item => `${item.id}: ${item.status} — ${item.title}${'error' in item && item.error ? `\n ${item.error}` : ''}${item.manual.length ? `\n Manual: ${item.manual.join('; ')}` : ''}`), + ...report.hostChecks.map(item => `Host ${item.task}: ${item.status} (${item.suites.reduce((sum, suite) => sum + suite.tests, 0)} XDK tests; XML timestamps in results.json)`), + report.hostChecksNote, + ...Object.entries(report.supportingCoverage).map(([id, coverage]) => `${id}: ${coverage}`), + ...report.errors.map(error => error.error ?? error.title), report.scope + ].join('\n') + '\n'); + assert.strictEqual(failures, 0, `Compiler playbook failed; see ${directory}/results.json`); + assert.strictEqual(report.cases.filter(item => selected.some(id => id === item.id) && item.status !== 'passed').length, + 0, 'No silently skipped selected playbook cases'); +} diff --git a/lang/vscode-extension/src/test/playbook/inlineCompletion.ts b/lang/vscode-extension/src/test/playbook/inlineCompletion.ts new file mode 100644 index 00000000000..cdaaff036c0 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/inlineCompletion.ts @@ -0,0 +1,81 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { InlineCompletionList, InlineCompletionTriggerKind } from 'vscode-languageclient/node'; +import { focusTestEditor } from '../native-focus'; +import { WorkbenchUi } from '../workbenchUi'; +import { client, eventually, noErrors, playbook, symbols } from './support'; + +export function inlineCompletionCases(): void { + for (const id of ['X255', 'X256', 'X257', 'X258'] as const) { + playbook(id, async (workspace, data) => { + const offset = data.source.indexOf('§'); + const source = data.source.replace('§', ''); + const document = await workspace.open(data.file, source); + const editor = await vscode.window.showTextDocument(document); + const configuration = vscode.workspace.getConfiguration('editor', document.uri); + const previous = configuration.inspect('inlineSuggest.enabled')?.workspaceValue; + await configuration.update('inlineSuggest.enabled', true, vscode.ConfigurationTarget.Workspace); + const ui = await WorkbenchUi.connect(); + try { + await focusTestEditor(document); + const at = document.positionAt(offset); + const query = (triggerKind: InlineCompletionTriggerKind, selectedCompletionInfo?: { range: vscode.Range; text: string }) => + client().sendRequest('textDocument/inlineCompletion', { + textDocument: { uri: document.uri.toString() }, position: at, + context: { + triggerKind, + selectedCompletionInfo: selectedCompletionInfo && { + ...selectedCompletionInfo, + range: client().code2ProtocolConverter.asRange(selectedCompletionInfo.range) + } + } + }); + assert.ok(client().initializeResult?.capabilities.inlineCompletionProvider); + if (data.mode === 'selection') { + assert.deepStrictEqual((await query(InlineCompletionTriggerKind.Automatic)).items, []); + const items = (await query(InlineCompletionTriggerKind.Invoked)).items; + assert.deepStrictEqual(items.map(item => item.insertText).sort(), ['another', 'answer']); + const range = new vscode.Range(at.translate(0, -2), at); + assert.deepStrictEqual((await query(InlineCompletionTriggerKind.Invoked, { range, text: 'ans' })).items.map(item => item.insertText), ['answer']); + assert.deepStrictEqual((await query(InlineCompletionTriggerKind.Invoked, { range, text: 'answer' })).items, []); + editor.selection = new vscode.Selection(at, at); + await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger'); + await vscode.commands.executeCommand('editor.action.inlineSuggest.showNext'); + const nextText = items[1].insertText; + assert.ok(typeof nextText === 'string'); + await eventually(() => ui.inlineText(), text => text.trim() === nextText.slice(2), 'Next inline alternative is rendered'); + await vscode.commands.executeCommand('editor.action.inlineSuggest.commit'); + await eventually(async () => document.getText(), text => text === source.slice(0, offset - 2) + nextText + source.slice(offset), 'Native next inline alternative is accepted'); + return; + } + assert.deepStrictEqual((await query(InlineCompletionTriggerKind.Automatic)).items.map(item => item.insertText), [data.expected]); + editor.selection = new vscode.Selection(at, at); + await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger'); + if (data.mode === 'typing') { + await vscode.commands.executeCommand('editor.action.inlineSuggest.hide'); + assert.strictEqual(document.getText(), source, 'Dismissing ghost text does not edit the document'); + await vscode.commands.executeCommand('default:type', { text: 'w' }); + await eventually(async () => document.getText(), text => text === source.slice(0, offset) + 'w' + source.slice(offset), 'Typed prefix reaches the document'); + await symbols(document); + await vscode.commands.executeCommand('editor.action.inlineSuggest.trigger'); + } + await eventually(() => ui.inlineText(), text => text.trim() === data.expected.slice(data.mode === 'typing' ? 4 : 3), 'Inline suggestion is rendered before acceptance'); + await vscode.commands.executeCommand('editor.action.inlineSuggest.commit'); + const expected = source.slice(0, offset - 3) + data.expected + source.slice(offset); + await eventually(async () => document.getText(), text => text === expected, 'Native inline acceptance preserves the source suffix'); + if (data.mode === 'accept') { + await noErrors(document.uri); + await vscode.commands.executeCommand('undo'); + await eventually(async () => document.getText(), text => text === source, 'One Undo restores the typed prefix'); + } else if (data.mode === 'incomplete') { + await workspace.replace(document, expected.replace('consume(answer\n', 'consume(answer);\n')); + await noErrors(document.uri); + } + } finally { + await ui.close(); + await vscode.commands.executeCommand('editor.action.inlineSuggest.hide'); + await configuration.update('inlineSuggest.enabled', previous, vscode.ConfigurationTarget.Workspace); + } + }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/librarySettings.ts b/lang/vscode-extension/src/test/playbook/librarySettings.ts new file mode 100644 index 00000000000..693e3d62dfc --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/librarySettings.ts @@ -0,0 +1,119 @@ +import * as assert from 'node:assert'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { normalizeLibraries } from '../../library-configuration'; +import { libraryOptions } from '../../library-settings'; +import { getClient, updateCompilerConfiguration } from '../../lsp-client'; +import { WorkbenchUi } from '../workbenchUi'; +import { catalog, sharedScenarioPath } from './shared'; +import { client, diagnostics, eventually, noErrors, playbook, targets } from './support'; + +export function librarySettingsCases(): void { + for (const id of ['X266', 'X267', 'X268'] as const) { + playbook(id, async workspace => { + const data = catalog.common.librarySettings; + const binaries = path.resolve(path.dirname(sharedScenarioPath), '../../lsp-server/build/generated/compiler-playbook/libraries'); + const one = vscode.Uri.file(path.join(binaries, 'one')).toString(); + const two = vscode.Uri.file(path.join(binaries, 'two')).toString(); + const sources = vscode.Uri.file(path.join(binaries, 'sources')).toString(); + const settings = vscode.workspace.getConfiguration('xtc.compiler'); + const previous = settings.inspect('libraries')?.workspaceValue; + const previousGraph = settings.inspect('sourceModules')?.workspaceValue; + const options = (modulePath: string[] | null, attached = false) => ({ modulePath, + sourceAttachments: attached ? [{ module: data.module, roots: [sources] }] : [] }); + const apply = async (paths: string[], attached = false) => { + await settings.update('libraries', options(paths, attached), vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + }; + const document = await workspace.open(data.consumerFile, data.consumer); + try { + if (id === 'X266') { + const ui = await WorkbenchUi.connect(); + const pick = async (label: string) => { + await ui.page.locator('.quick-input-widget:visible .monaco-list-row').filter({ hasText: label }).first().click({ timeout: 10_000 }); + }; + const enter = async (value: string) => { + const widget = ui.page.locator('.quick-input-widget:visible'); + // QuickPick and InputBox reuse the same input element. Wait for the prompt, + // then for the returned path row, before sending the next input. + await widget.getByText('File URI or path relative to the single workspace folder') + .waitFor({ state: 'visible', timeout: 10_000 }); + const input = widget.locator('input').first(); + await input.fill(value); + await input.press('Enter'); + await widget.locator('.monaco-list-row').filter({ hasText: value }).first() + .waitFor({ state: 'visible', timeout: 10_000 }); + }; + try { + const cancelled = vscode.commands.executeCommand('xtc.configureCompilerLibraries'); + await pick('Edit binary paths'); + await pick('Enter path…'); + await enter(one); + await ui.page.locator('.quick-input-widget:visible input').first().press('Escape'); + await cancelled; + assert.deepStrictEqual(settings.inspect('libraries')?.workspaceValue, previous); + const command = vscode.commands.executeCommand('xtc.configureCompilerLibraries'); + await pick('Edit binary paths'); + for (const value of [one, two]) { await pick('Enter path…'); await enter(value); } + await pick(`2. ${two}`); + await pick('Move up'); + await pick('Use these paths'); + await pick('Apply library settings'); + await command; + assert.deepStrictEqual(libraryOptions().modulePath, [two, one]); + await noErrors(document.uri); + const effective = await client().sendRequest<{ bundledXdk: { readOnly: boolean } }>('xtc/languageServiceStatus'); + assert.ok(effective.bundledXdk.readOnly); + } catch (error) { + await ui.screenshot(`${id}-picker-failure`); + console.error(`[${id}] Visible picker:`, await ui.page.locator('.quick-input-widget:visible').allTextContents()); + throw error; + } finally { await vscode.commands.executeCommand('workbench.action.closeQuickOpen'); await ui.close(); } + } else { + await apply([one], true); + await noErrors(document.uri); + const definition = async () => { + const locations = await targets(document, 'Definition', document.positionAt(data.consumer.indexOf(data.anchor))); + assert.strictEqual(locations.length, 1); + assert.strictEqual(locations[0].range.start.line, data.definitionLine); + const library = await vscode.workspace.openTextDocument(locations[0].uri); + assert.strictEqual(library.getText(), data.source); + assert.strictEqual(library.uri.scheme, 'ecstasy-library'); + await vscode.window.showTextDocument(library, { preview: true, preserveFocus: true }); + }; + await definition(); + if (id === 'X267') { + const previousClient = getClient(); + await vscode.commands.executeCommand('xtc.restartServer'); + await eventually(async () => getClient() !== previousClient && !!getClient()?.initializeResult, ready => ready, 'Library configuration reconnect'); + await noErrors(document.uri); + await definition(); + assert.deepStrictEqual(libraryOptions(), options([one], true)); + } else { + assert.throws(() => normalizeLibraries(options([one, one]), [], true), /Duplicate/); + assert.throws(() => normalizeLibraries(options([vscode.Uri.file(path.join(binaries, 'missing')).toString()]), [], true), /does not exist/); + await settings.update('libraries', options([vscode.Uri.file(path.join(binaries, 'missing')).toString()]), vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + await noErrors(document.uri); + await definition(); + await apply([one], true); + const resources = [two, one]; + await settings.update('sourceModules', [{ name: 'LibraryConsumer', uri: document.uri.toString(), resourceRoots: resources }], vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + const modules = await client().sendRequest<{ resourceRoots: string[] }[]>('xtc/compilerSourceModules'); + assert.deepStrictEqual(modules[0].resourceRoots.map(uri => path.resolve(vscode.Uri.parse(uri).fsPath)), resources.map(uri => path.resolve(vscode.Uri.parse(uri).fsPath))); + await noErrors(document.uri); + } + await apply([]); + await diagnostics(document.uri, values => values.length > 0, 'Removed binary library'); + await apply([one], true); + await noErrors(document.uri); + } + } finally { + await settings.update('sourceModules', previousGraph, vscode.ConfigurationTarget.Workspace); + await settings.update('libraries', previous, vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + } + }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/liveWorkspace.ts b/lang/vscode-extension/src/test/playbook/liveWorkspace.ts new file mode 100644 index 00000000000..cb55d6d4d93 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/liveWorkspace.ts @@ -0,0 +1,175 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { compilerSettingsLocation } from '../../rename-proposal'; +import { edges, hierarchy } from './modules'; +import { client, diagnostics, eventually, label, noErrors, playbook, position, targetNames, targets, Workspace } from './support'; + +export async function discovered(workspace: Workspace, body: () => Promise): Promise { + const original = vscode.workspace.workspaceFolders!.map(folder => ({ uri: folder.uri.toString(), name: folder.name })); + const folder = { uri: vscode.Uri.file(workspace.directory).toString(), name: 'Live discovery' }; + await client().sendNotification('workspace/didChangeWorkspaceFolders', { event: { added: [folder], removed: original } }); + await vscode.workspace.getConfiguration('xtc.compiler').update('sourceModules', null, vscode.ConfigurationTarget.Workspace); + try { return await body(); } + finally { + // These are isolated test settings. A failed history assertion must not be hidden by + // the configuration API refusing cleanup of a dirty settings document. + const settings = vscode.workspace.textDocuments.find(document => document.uri.toString() === compilerSettingsLocation()?.uri.toString()); + if (settings?.isDirty) await settings.save(); + await workspace.configure([]); + await client().sendNotification('workspace/didChangeWorkspaceFolders', { event: { added: original, removed: [folder] } }); + } +} + +function namedRename(id: 'X102' | 'X104'): void { + playbook(id, async (workspace, data) => { + await workspace.write(data.file, data.source); + await discovered(workspace, async () => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const edit = await vscode.commands.executeCommand( + 'vscode.executeDocumentRenameProvider', document.uri, position(document, data.anchor), data.replacement); + assert.ok(edit); + assert.strictEqual(edit.get(document.uri).length, data.edits); + assert.ok(await vscode.workspace.applyEdit(edit)); + await noErrors(document.uri); + assert.strictEqual(document.getText().split(data.replacement).length - 1, data.edits); + if ('preserved' in data) assert.ok(document.getText().includes(data.preserved)); + }); + }); +} + +export function liveWorkspaceCases(): void { + playbook('X99', async (workspace, data) => { + await workspace.write(data.file, data.original); + await workspace.write(data.library, data.libraryText); + await discovered(workspace, async () => { + const document = await workspace.open(data.file, data.original); + await workspace.replace(document, data.changed); + const found = await eventually(() => targets(document, 'Definition', position(document, data.anchor)), + values => values.length === 1, 'Unsaved import reaches discovered dependency'); + assert.deepStrictEqual(await targetNames(found), [data.target]); + const library = await workspace.open(data.library, data.libraryText); + await workspace.replace(library, data.brokenLibrary); + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Dependency edit reaches unchanged consumer'); + await workspace.discard(library); + await noErrors(document.uri); + assert.strictEqual(await fs.readFile(document.uri.fsPath, 'utf8'), data.original); + }); + }); + + playbook('X100', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.write(data.neighbor, data.neighborText); + await discovered(workspace, async () => { + const document = await workspace.open(data.file, data.source); + const neighbor = await workspace.open(data.neighbor, data.neighborText); + await workspace.replace(neighbor, data.brokenNeighbor); + await diagnostics(neighbor.uri, values => values.length > 0, 'Neighbor is broken'); + const items = await hierarchy(document, position(document, data.anchor)); + assert.strictEqual(items?.length, 1); + const children = await edges(items[0], 'subtypes'); + assert.deepStrictEqual(children?.map(item => item.name), [data.child]); + const references = await vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, position(document, data.anchor)); + assert.deepStrictEqual(references, [], 'Incomplete graphs do not claim complete reference results'); + await workspace.replace(neighbor, data.neighborText); + await noErrors(neighbor.uri); + }); + }); + + playbook('X101', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + for (const type of data.types) { + const found = await targets(document, 'Definition', position(document, type)); + assert.deepStrictEqual(await targetNames(found), [type]); + assert.strictEqual(found[0].uri.scheme, 'ecstasy-library', 'Bundled source uses the read-only content provider'); + const edits = await vscode.commands.executeCommand( + 'vscode.executeFormatDocumentProvider', found[0].uri, { tabSize: 4, insertSpaces: true }); + assert.deepStrictEqual(edits ?? [], []); + await assert.rejects(client().sendRequest('textDocument/prepareRename', { + textDocument: { uri: document.uri.toString() }, position: position(document, type) + }), { message: data.renameRejection }); + } + const types = await targets(document, 'TypeDefinition', position(document, data.variable)); + assert.deepStrictEqual(await targetNames(types), [data.variableType]); + }); + namedRename('X102'); + playbook('X103', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.write(data.member, data.memberSource); + await workspace.write(data.companion, data.companionSource); + await discovered(workspace, async () => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const provider = client().getFeature('textDocument/rename').getProvider(document); + assert.ok(provider); + // The generic execute-command round trip regroups resource operations before text edits. + // Call the registered provider used by Rename, preserving its ordered WorkspaceEdit. + const cancellation = new vscode.CancellationTokenSource(); + try { + // Fixture creation can deliver a late watcher notification that cancels the + // proof. Retry only the unapplied query; apply the resulting edit exactly once. + const edit = await eventually( + async () => provider.provideRenameEdits(document, position(document, data.anchor), data.replacement, cancellation.token), + edit => !!edit, 'Companion rename proof after fixture watcher notifications'); + assert.ok(edit); + assert.ok(await vscode.workspace.applyEdit(edit)); + } finally { cancellation.dispose(); } + await noErrors(document.uri); + const moved = await vscode.workspace.openTextDocument(workspace.uri(data.destination)); + assert.strictEqual(moved.getText(), data.memberSource.replace(data.anchor, data.replacement)); + assert.strictEqual(await fs.stat(workspace.uri(data.member).fsPath).catch(() => null), null); + assert.ok(document.getText().includes(data.replacement)); + assert.strictEqual(await fs.readFile(workspace.uri(data.companionDestination).fsPath, 'utf8'), data.companionSource); + assert.strictEqual(await fs.stat(workspace.uri(data.companion).fsPath).catch(() => null), null); + await vscode.window.showTextDocument(document); + await vscode.commands.executeCommand('undo'); + await eventually(async () => document.getText(), text => text === data.source, 'Undo restores the renamed type uses'); + assert.strictEqual(await fs.readFile(workspace.uri(data.companion).fsPath, 'utf8'), data.companionSource); + assert.strictEqual(await fs.stat(workspace.uri(data.destination).fsPath).catch(() => null), null); + await noErrors(document.uri); + }); + }); + + namedRename('X104'); + playbook('X105', async (workspace, data) => { + await workspace.write(data.library, data.libraryText); + await workspace.write(data.file, data.variants[0].source); + await discovered(workspace, async () => { + const document = await workspace.open(data.file, data.variants[0].source); + for (const variant of data.variants) { + await workspace.replace(document, variant.source); + await diagnostics(document.uri, values => values.length > 0, 'Unresolved imported type'); + const at = position(document, variant.anchor); + const action = await eventually(async () => { + try { + const actions = await vscode.commands.executeCommand( + 'vscode.executeCodeActionProvider', document.uri, new vscode.Range(at, at), vscode.CodeActionKind.QuickFix.value, 100); + return actions?.find(item => item.title === variant.title); + } catch (error) { + if (error instanceof Error && error.name === 'Canceled') return undefined; + throw error; + } + }, item => !!item?.edit, variant.title); + assert.ok(action?.edit, variant.title); + assert.ok(await vscode.workspace.applyEdit(action.edit)); + await noErrors(document.uri); + assert.ok(document.getText().includes(variant.importText)); + } + for (const variant of data.completions) { + await workspace.replace(document, variant.source.replace('§', '')); + const at = document.positionAt(variant.source.indexOf('§')); + const item = await eventually(() => workspace.completion(document, at).then(items => + items.find(value => label(value) === variant.label && value.additionalTextEdits?.length)), + value => !!value, `Completion imports ${variant.label}`); + assert.ok(item); + await workspace.accept(document, item); + assert.strictEqual(document.getText(), variant.expected); + assert.ok(document.getText().includes(variant.importText)); + await noErrors(document.uri); + } + }); + }); + +} diff --git a/lang/vscode-extension/src/test/playbook/memberActions.ts b/lang/vscode-extension/src/test/playbook/memberActions.ts new file mode 100644 index 00000000000..b6c31208f36 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/memberActions.ts @@ -0,0 +1,116 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { focusTestEditor, focusTestWindow } from '../native-focus'; +import { discovered } from './liveWorkspace'; +import { diagnostics, eventually, noErrors, playbook, position } from './support'; + +export function memberActionCases(): void { + playbook('X122', async (workspace, data) => { + await workspace.write(data.file, data.variants[0].source); + await discovered(workspace, async () => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + await workspace.replace(document, variant.source); + const originalDiagnostics = async () => { + if ('initiallyValid' in variant && !variant.initiallyValid) { + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), + 'Missing implementation is diagnosed before applying the action'); + } else await noErrors(document.uri); + }; + await originalDiagnostics(); + const at = position(document, data.anchor); + const actions = await eventually(async () => vscode.commands.executeCommand( + 'vscode.executeCodeActionProvider', document.uri, new vscode.Range(at, at), undefined, 100), + actions => Array.isArray(actions) && (!variant.title || actions.some(action => action.title === variant.title)), 'Member action reply'); + if (!variant.title) { + assert.ok(!actions?.some(action => /^(Implement|Override) /.test(action.title) && action.title.includes(` ${data.refusalMember}(`))); + assert.strictEqual(document.getText(), variant.source); + continue; + } + const action = actions?.find(action => action.title === variant.title); + assert.ok(action?.edit); + assert.ok(await vscode.workspace.applyEdit(action.edit)); + assert.strictEqual(document.getText(), variant.expected); + await noErrors(document.uri); + for (const [command, expected] of [['undo', variant.source], ['redo', variant.expected], ['undo', variant.source]]) { + await focusTestWindow(); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + await vscode.commands.executeCommand(command); + await eventually(async () => document.getText(), text => text === expected, `${command} member action`); + if (command === 'redo') await noErrors(document.uri); + else await originalDiagnostics(); + } + } + }); + }); +} + +export function localRefactoringCases(ids: readonly ('X148' | 'X156' | 'X157' | 'X177' | 'X178' | 'X179' | 'X180' | 'X181' | 'X182' | 'X183' | 'X184' | 'X185' | 'X186' | 'X187' | 'X188' | 'X189' | 'X190' | 'X191' | 'X192' | 'X193' | 'X194' | 'X195' | 'X196' | 'X197' | 'X198' | 'X199' | 'X200' | 'X201' | 'X202' | 'X203' | 'X204' | 'X205' | 'X206' | 'X207' | 'X208' | 'X209' | 'X210' | 'X211' | 'X212' | 'X213' | 'X214' | 'X215' | 'X221' | 'X222' | 'X223' | 'X224' | 'X225' | 'X226' | 'X227' | 'X228' | 'X229' | 'X230' | 'X231' | 'X232' | 'X233' | 'X234' | 'X235' | 'X236' | 'X237' | 'X238' | 'X239' | 'X240' | 'X241' | 'X242')[] = ['X148']): void { + for (const id of ids) playbook(id, async (workspace, data) => { + await workspace.write(data.file, data.source); + if ('destinationFile' in data) await workspace.write(data.destinationFile, data.destinationSource); + if ('files' in data) for (const file of data.files) await workspace.write(file.file, file.source); + await discovered(workspace, async () => { + if ('sourceModules' in data) await workspace.configure(data.sourceModules.map(module => ({ ...module, uri: workspace.uri(module.uri).toString() }))); + const document = await workspace.open(data.file); + const originalDiagnostics = async () => { + if ('initiallyValid' in data && !data.initiallyValid) { + await diagnostics(document.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), + 'Missing method is diagnosed before applying the action'); + } else await noErrors(document.uri); + }; + const verifyAdditionalFiles = async (applied: boolean) => { + if ('files' in data) for (const file of data.files) { + const document = await vscode.workspace.openTextDocument(workspace.uri(file.file)); + const expected = applied && 'expected' in file ? file.expected : file.source; + assert.strictEqual(document.getText(), expected); + } + }; + await originalDiagnostics(); + const kind = 'initiallyValid' in data ? vscode.CodeActionKind.QuickFix.value : vscode.CodeActionKind.Refactor.value; + const start = 'selectionOffset' in data ? document.positionAt(data.selectionOffset) : position(document, data.selected); + const range = new vscode.Range(start, document.positionAt(document.offsetAt(start) + data.selected.length)); + const readActions = async () => { + try { + return await vscode.commands.executeCommand( + 'vscode.executeCodeActionProvider', document.uri, range, kind, 100) ?? []; + } catch (error) { + // Host refresh can cancel a read-only lookup. Never retry edits or history. + if (error instanceof Error && error.name === 'Canceled') { + console.log(`[playbook] ${id}: provider lookup cancelled before any edit; retrying lookup`); + return undefined; + } + throw error; + } + }; + if ('refused' in data && data.refused) { + const actions = await eventually(readActions, items => items !== undefined, 'Completed code-action refusal query'); + assert.ok(!actions?.some(item => item.title === data.title)); + assert.strictEqual(document.getText(), data.source); + return; + } + const action = await eventually(async () => { + const actions = await readActions(); + return actions?.find(item => item.title === data.title); + }, item => !!item?.edit, data.title); + assert.ok(action?.edit); + assert.ok(await vscode.workspace.applyEdit(action.edit)); + const destination = 'destinationFile' in data ? await workspace.open(data.destinationFile) : document; + const original = 'destinationFile' in data ? data.destinationSource : data.source; + assert.strictEqual(destination.getText(), data.expected); + await verifyAdditionalFiles(true); + if (destination !== document) assert.strictEqual(document.getText(), data.source); + await noErrors(document.uri); + for (const [command, expected] of [['undo', original], ['redo', data.expected], ['undo', original]]) { + await focusTestEditor(destination); + await vscode.commands.executeCommand(command); + await eventually(async () => destination.getText(), text => text === expected, `${command} local refactoring`); + if (destination !== document) assert.strictEqual(document.getText(), data.source); + await verifyAdditionalFiles(command === 'redo'); + if (command === 'redo') await noErrors(document.uri); + else await originalDiagnostics(); + } + }); + }); + +} diff --git a/lang/vscode-extension/src/test/playbook/modules.ts b/lang/vscode-extension/src/test/playbook/modules.ts new file mode 100644 index 00000000000..c50a8ebf578 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/modules.ts @@ -0,0 +1,207 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { CompletionItem, CompletionList, TypeHierarchyItem } from 'vscode-languageclient/node'; +import { getClient } from '../../lsp-client'; +import { scenarioRegex, scenarioText } from './shared'; +import { client, diagnostics, diagnosticCode, eventually, fixture, label, noErrors, playbook, position, symbols, targets } from './support'; + +export async function hierarchy(document: vscode.TextDocument, at: vscode.Position): Promise { + return await client().sendRequest('textDocument/prepareTypeHierarchy', { + textDocument: { uri: document.uri.toString() }, position: at + }) ?? []; +} + +export async function edges(item: TypeHierarchyItem, direction: 'supertypes' | 'subtypes'): Promise { + assert.ok(item, 'Prepared type hierarchy item'); + return await client().sendRequest(`typeHierarchy/${direction}`, { item }) ?? []; +} + +export function moduleCases(): void { + playbook('X21', async (workspace, data) => { + const root = await workspace.project(); + const references = await vscode.commands.executeCommand('vscode.executeReferenceProvider', root.uri, position(root, data.anchor)); + assert.ok(references?.some(item => item.uri.toString() === workspace.uri(data.file).toString())); + const found = await vscode.commands.executeCommand('vscode.executeWorkspaceSymbolProvider', data.symbolName); + assert.ok(found?.some(item => item.location.uri.toString() === workspace.uri(data.file).toString())); + const child = await workspace.open(data.file); + for (const word of data.uses) { + const definitions = await targets(child, 'Definition', position(child, word)); + assert.strictEqual(definitions.length, data.targetCount); + assert.strictEqual(definitions[0].uri.toString(), root.uri.toString()); + } + }); + + playbook('X22', async (workspace, data) => { + const root = await workspace.project(); + const [base] = await hierarchy(root, position(root, data.baseDeclaration)); + assert.ok(base); + const children = await edges(base, 'subtypes'); + assert.strictEqual(children.length, data.childCount); + assert.strictEqual(children[0].name, data.childName); + assert.strictEqual(vscode.Uri.parse(children[0].uri).fsPath, workspace.uri(data.file).fsPath); + const parents = await edges(children[0], 'supertypes'); + assert.ok(parents.some(item => scenarioText(data.display, item.name, item.detail).includes(data.typeArgument)), JSON.stringify(parents)); + assert.ok((await edges(base, 'supertypes')).some(item => item.name === data.interfaceName)); + const [named] = await hierarchy(root, position(root, data.interfaceDeclaration, data.offset)); + assert.ok((await edges(named, 'subtypes')).some(item => item.name.startsWith(data.baseName))); + }); + + playbook('X23', async (workspace, data) => { + const root = await workspace.project(); + const child = await workspace.open(data.memberFile); + const version = child.version; + await workspace.replace(root, fixture(data.rootFile).replace(data.replaceFrom, data.replaceWith)); + const errors = await diagnostics(child.uri, values => values.length > 0, 'Sibling diagnostic'); + assert.ok(errors.every(item => diagnosticCode(item) !== data.diagnosticCode)); + assert.strictEqual(child.version, version); + assert.ok(!vscode.languages.getDiagnostics(root.uri).some(item => diagnosticCode(item) === data.diagnosticCode)); + await workspace.replace(root, fixture(data.rootFile)); + await noErrors(child.uri); + }); + + playbook('X24', async (workspace, data) => { + const root = await workspace.project(); + const text = fixture(data.memberFile).replace(data.method, data.incompleteMethod); + const { document, at } = await workspace.marked(data.memberFile, text); + for (const type of data.types) { + await workspace.replace(root, fixture(data.rootFile).replace(data.replaceFrom, scenarioText(data.replaceWith, type, type === data.integerType ? data.integerValue : data.stringValue))); + const item = (await workspace.completion(document, at)).find(value => label(value) === data.label); + assert.ok(item); + assert.match(item.detail ?? '', new RegExp(type)); + } + assert.strictEqual(await fs.readFile(root.uri.fsPath, 'utf8'), fixture(data.rootFile)); + }); + + playbook('X25', async (workspace, data) => { + const root = await workspace.project(); + const uri = workspace.uri(data.file); + try { + await client().sendNotification('textDocument/didOpen', { textDocument: { + uri: uri.toString(), languageId: 'xtc', version: data.version, text: data.text + } }); + await symbols(root); + // Fixture filesystem notifications can rebuild the module between these two + // requests. Reprepare each time: hierarchy items deliberately expire on rebuild. + await eventually(async () => { + const [base] = await hierarchy(root, position(root, data.anchor)); + return base ? edges(base, 'subtypes') : []; + }, children => children.some(item => item.name === data.name), 'Unsaved member in current hierarchy'); + assert.strictEqual(await fs.stat(uri.fsPath).catch(() => undefined), undefined); + await noErrors(uri); + } finally { + await client().sendNotification('textDocument/didClose', { textDocument: { uri: uri.toString() } }); + } + }); + + playbook('X26', async (workspace, data) => { + const root = await workspace.project(); + const child = await workspace.open(data.file); + await workspace.replace(child, fixture(data.file).replace(data.anchor, data.replaceWith)); + await diagnostics(child.uri, values => values.length > 0, 'Broken member'); + await workspace.discard(child); + await symbols(root); + await noErrors(child.uri); + const reopened = await workspace.open(data.file); + assert.strictEqual(reopened.getText(), fixture(data.file)); + assert.strictEqual((await targets(reopened, 'Definition', position(reopened, data.anchor))).length, data.expected); + }); + + playbook('X27', async (workspace, data) => { + await workspace.project(); + await workspace.write(data.file, data.text); + const uri = workspace.uri(data.file); + await diagnostics(uri, values => values.length > 0, 'Closed created member diagnostic'); + await fs.unlink(uri.fsPath); + await noErrors(uri); + }); + + playbook('X28', async (workspace, data) => { + await workspace.project(); + const child = await workspace.open(data.file); + const [old] = await hierarchy(child, position(child, data.anchor)); + assert.ok(old); + await workspace.replace(child, data.replaceWith); + await noErrors(child.uri); + assert.deepStrictEqual(await edges(old, 'supertypes'), []); + const [fresh] = await hierarchy(child, position(child, data.anchor)); + assert.ok(!(await edges(fresh, 'supertypes')).some(item => item.name.startsWith(data.contains))); + await workspace.replace(child, fixture(data.file)); + const [restored] = await hierarchy(child, position(child, data.anchor)); + assert.ok((await edges(restored, 'supertypes')).some(item => item.name.startsWith(data.contains))); + }); + + playbook('X29', async (workspace, data) => { + const { document, at } = await workspace.editing(data.incompleteBody); + const initial = document.getText(); + const pending = []; + for (let index = 0; index < 10; index++) { + await workspace.replace(document, initial.replace(data.integerCall, index % 2 ? data.integerCall : data.stringCall), false); + pending.push(workspace.signature(document, at)); + } + await Promise.all(pending); + const final = await workspace.editing(data.completedBody); + await noErrors(final.document.uri); + const signature = await workspace.signature(final.document, position(final.document, data.completedCall, data.offset)); + assert.match(signature?.signatures[0]?.label ?? '', scenarioRegex(data.pattern)); + const root = await workspace.project(); + const child = await workspace.open(data.memberFile); + for (let index = 0; index < 6; index++) { + await workspace.replace(root, fixture(data.rootFile).replace(data.replaceFrom, scenarioText(data.replaceWith, index)), false); + } + await symbols(child); + await noErrors(child.uri); + assert.strictEqual((await targets(child, 'Definition', position(child, data.anchor))).length, data.expected); + }); + + playbook('X30', async (workspace, data) => { + const { document, at } = await workspace.editing(data.body); + const token = new vscode.CancellationTokenSource(); + const pending = client().sendRequest('textDocument/completion', { textDocument: { uri: document.uri.toString() }, position: at }, token.token) + .then(() => undefined, error => { assert.ok((data.cancellationCodes as (number)[]).includes(error.code), String(error)); }); + token.cancel(); + await pending; + token.dispose(); + await workspace.discard(document); + const reopened = await workspace.open(data.file); + await noErrors(reopened.uri); + const previous = getClient(); + await vscode.commands.executeCommand('xtc.restartServer'); + await eventually(async () => getClient(), value => !!value?.initializeResult && value !== previous, 'Restarted language client'); + await symbols(reopened); + await noErrors(reopened.uri); + const current = await workspace.editing(data.body); + assert.ok((await workspace.completion(current.document, current.at)).some(item => label(item) === data.label)); + }); + + playbook('X31', async (workspace, data) => { + const document = await workspace.open(data.file); + const capabilities = client().initializeResult!.capabilities; + for (const capability of data.unsupportedCapabilities) { + assert.ok(!capabilities[capability as keyof typeof capabilities], capability); + } + assert.strictEqual(Boolean(capabilities[data.prototypeCapability as keyof typeof capabilities]), + process.env.XTC_LSP_COLOR_PROTOTYPE === 'true', data.prototypeCapability); + for (const capability of data.supportedCapabilities) { + assert.ok(capabilities[capability as keyof typeof capabilities], capability); + } + await workspace.replace(document, data.unformatted); + assert.ok((await vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', document.uri, + { tabSize: data.tabSize, insertSpaces: true }))?.length, 'Compiler formatting supplies edits'); + }); + + playbook('X32', async (workspace, data) => { + for (const body of data.bodies) { + const { document, at } = await workspace.editing(body); + const response = await client().sendRequest('textDocument/completion', { + textDocument: { uri: document.uri.toString() }, position: at + }); + const compilerItems = Array.isArray(response) ? response : response?.items ?? []; + assert.strictEqual(compilerItems.length, data.completionCount, body); + // VS Code adds the extension's static snippets at an empty slot even when the + // compiler correctly withholds semantic candidates. Inspect both boundaries. + const semanticItems = (await workspace.completion(document, at)).filter(item => item.kind !== vscode.CompletionItemKind.Snippet); + assert.strictEqual(semanticItems.length, data.completionCount, `${body}: ${semanticItems.map(label).join(', ')}`); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/navigation.ts b/lang/vscode-extension/src/test/playbook/navigation.ts new file mode 100644 index 00000000000..905512572fc --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/navigation.ts @@ -0,0 +1,97 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { editScenario, scenarioOffset, scenarioRegex, shared } from './shared'; +import { diagnostics, diagnosticCode, fixture, hover, nextProblem, noErrors, playbook, position, symbolNames, symbols, targetNames, targets } from './support'; + +export function navigationCases(): void { + playbook('X1', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + const names = symbolNames(await symbols(document)); + for (const name of data.symbolNames) { + assert.ok(names.some(value => value.includes(name)), `${name} in outline: ${names}`); + } + const folds = await vscode.commands.executeCommand('vscode.executeFoldingRangeProvider', document.uri); + assert.ok(folds && folds.length >= data.minimumFolds); + const at = position(document, data.anchor, data.offset); + const selections = await vscode.commands.executeCommand('vscode.executeSelectionRangeProvider', document.uri, [at]); + assert.ok(selections?.[0]?.parent); + assert.ok(selections[0].range.contains(at)); + assert.ok(selections[0].parent.range.contains(selections[0].range)); + }); + + playbook('X2', async (workspace, data) => { + const scenario = shared.diagnostics; + const document = await workspace.open(scenario.file); + for (const expected of scenario.errors) { + const text = editScenario(fixture(scenario.file), expected.edit); + await workspace.replace(document, text); + const errors = await diagnostics(document.uri, values => values.some(item => vscode.DiagnosticSeverity[item.severity].toUpperCase() === expected.severity), 'Expected compiler errors'); + assert.ok(errors.every(item => diagnosticCode(item).startsWith(expected.codePrefix))); + assert.ok(errors.some(item => item.message.toLowerCase().includes(expected.messageContains.toLowerCase()))); + const start = scenarioOffset(text, expected.rangeStart); + const end = scenarioOffset(text, expected.rangeEnd); + assert.ok(errors.some(item => document.offsetAt(item.range.start) >= start && document.offsetAt(item.range.start) <= end)); + assert.ok(errors.every(item => item.range.start.line > 0)); + assert.ok(errors.every(item => !item.range.isEmpty && item.source === 'xtc')); + await nextProblem(document, errors); + await workspace.replace(document, fixture(scenario.file)); + await noErrors(document.uri); + } + await workspace.replace(document, fixture(scenario.file) + scenario.cleanAppend); + await noErrors(document.uri); + }); + + playbook('X3', async (workspace, data) => { + const document = await workspace.open(data.file); + const narrowed = position(document, data.narrowUse, data.offset); + const original = position(document, data.wideUse, data.offset); + assert.match(await hover(document, narrowed), scenarioRegex(data.narrowedType)); + assert.match(await hover(document, original), scenarioRegex(data.declaredType)); + const a = await targets(document, 'Definition', narrowed); + const b = await targets(document, 'Definition', original); + assert.strictEqual(a.length, data.targetCount); + assert.deepStrictEqual(a, b); + assert.strictEqual(a[0].range.start.line, position(document, data.declaration, data.offset).line); + }); + + playbook('X4', async (workspace, data) => { + const scenario = shared.definitions; + const document = await workspace.open(scenario.file); + const [local, property] = scenario.locations.map(location => document.positionAt(scenarioOffset(document.getText(), location.cursor))); + const localDefinition = await targets(document, 'Definition', local); + const propertyDefinition = await targets(document, 'Definition', property); + assert.deepStrictEqual(await targets(document, 'Declaration', local), localDefinition); + assert.deepStrictEqual(await targets(document, 'Declaration', property), propertyDefinition); + assert.strictEqual(localDefinition.length, data.targetCount); + assert.strictEqual(propertyDefinition.length, data.targetCount); + assert.ok(!localDefinition[0].range.isEqual(propertyDefinition[0].range)); + for (const [index, definition] of [localDefinition[0], propertyDefinition[0]].entries()) { + const expected = scenario.locations[index]; + assert.strictEqual(definition.uri.toString(), workspace.uri(expected.targetFile).toString()); + assert.strictEqual(document.offsetAt(definition.range.start), scenarioOffset(fixture(expected.targetFile), expected.target)); + } + for (const [at, excluded] of [[local, property], [property, local]]) { + const references = await vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, at); + assert.ok(references?.some(item => item.range.contains(at))); + assert.ok(references.every(item => !item.range.contains(excluded))); + const highlights = await vscode.commands.executeCommand('vscode.executeDocumentHighlights', document.uri, at); + assert.ok(highlights?.some(item => item.range.contains(at))); + assert.ok(highlights.every(item => !item.range.contains(excluded))); + } + }); + + playbook('X5', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.deepStrictEqual(await targetNames(await targets(document, 'Definition', position(document, data.libraryType))), [data.libraryTarget]); + await workspace.replace(document, fixture(data.file).slice(0, fixture(data.file).lastIndexOf(data.missingCloser))); + await diagnostics(document.uri, values => values.some(item => diagnosticCode(item).startsWith(data.parserCodePrefix)), 'Parser recovery diagnostic'); + assert.ok(symbolNames(await symbols(document)).some(name => name.includes(data.retainedSymbol))); + assert.ok((await vscode.commands.executeCommand('vscode.executeFoldingRangeProvider', document.uri))?.length); + const at = position(document, data.anchor, data.offset); + assert.ok((await vscode.commands.executeCommand('vscode.executeSelectionRangeProvider', document.uri, [at]))?.length); + assert.deepStrictEqual(await targets(document, 'Definition', at), []); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/platform.ts b/lang/vscode-extension/src/test/playbook/platform.ts new file mode 100644 index 00000000000..eb091ce0685 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/platform.ts @@ -0,0 +1,534 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { ApplyWorkspaceEditParams, ApplyWorkspaceEditResponse, CodeAction, ProgressType } from 'vscode-languageclient/node'; +import { modelPath } from '../../build-model'; +import { getClient, updateCompilerConfiguration } from '../../lsp-client'; +import { cutAndPasteDirectories } from '../explorer-move'; +import { ExplorerMoveTrace } from '../explorer-move-trace'; +import { discovered } from './liveWorkspace'; +import { client, diagnostics, eventually, hover, label, noErrors, playbook, symbols, targets } from './support'; + +export function platformCases(): void { + playbook('X124', async (workspace, data) => { + const external = await fs.mkdtemp(path.join(os.tmpdir(), 'xtc-playbook-resources-')); + try { + await workspace.write(data.file, data.text); + const resourceRoot = path.join(external, data.resourceDirectory); + const modules = [{ name: data.module, uri: workspace.uri(data.file).toString(), resourceRoots: [vscode.Uri.file(resourceRoot).toString()] }]; + await workspace.configure(modules); + const document = await workspace.open(data.file); + await diagnostics(document.uri, values => values.length > 0, 'Missing external resource'); + await fs.mkdir(resourceRoot, { recursive: true }); + await fs.writeFile(path.join(resourceRoot, data.resource), data.contents); + await noErrors(document.uri); + await workspace.configure([{ ...modules[0], resourceRoots: [] }]); + await diagnostics(document.uri, values => values.length > 0, 'Explicitly disabled resources'); + await workspace.configure(modules); + await noErrors(document.uri); + assert.deepStrictEqual(vscode.workspace.getConfiguration('xtc.compiler').get('sourceModules'), modules); + await fs.unlink(path.join(resourceRoot, data.resource)); + await diagnostics(document.uri, values => values.length > 0, 'Deleted external resource'); + await fs.writeFile(path.join(resourceRoot, data.resource), data.contents); + await noErrors(document.uri); + const replacement = path.join(external, 'replacement'); + await workspace.configure([{ ...modules[0], resourceRoots: [vscode.Uri.file(replacement).toString()] }]); + await diagnostics(document.uri, values => values.length > 0, 'Replacement root is missing'); + await fs.mkdir(replacement); + await fs.writeFile(path.join(replacement, data.resource), data.contents); + await noErrors(document.uri); + } finally { await fs.rm(external, { recursive: true, force: true }); } + }); + + playbook('X125', async (workspace, data) => { + const document = await workspace.open(data.file, data.hoverSource); + await noErrors(document.uri); + const contents = await hover(document, document.positionAt(data.hoverSource.lastIndexOf(data.hoverAnchor))); + assert.ok(contents.includes(data.hoverExpected), contents); + for (const call of data.signatures) { + await workspace.replace(document, call.text.replace('§', '')); + const help = await workspace.signature(document, document.positionAt(call.text.indexOf('§'))); + assert.strictEqual(help?.signatures[0].parameters.length, 2); + assert.strictEqual(help?.activeParameter, call.active); + } + for (const text of data.completions) { + await workspace.replace(document, text.replace('§', '')); + const items = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', document.uri, document.positionAt(text.indexOf('§'))); + assert.ok(items?.items.some(item => label(item) === data.completion)); + } + await workspace.replace(document, data.narrowedSource); + await noErrors(document.uri); + const locations = await targets(document, 'TypeDefinition', document.positionAt(data.narrowedSource.lastIndexOf(data.narrowedAnchor))); + assert.ok(locations.some(location => location.uri.path.endsWith(data.targetSuffix))); + }); + playbook('X126', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + const id = { uri: document.uri.toString() }; + const full = await client().sendRequest<{ data: number[]; resultId: string }>('textDocument/semanticTokens/full', { textDocument: id }); + assert.ok(full.data.length && full.resultId); + await workspace.replace(document, '\n' + data.source); + const caps = client().initializeResult!.capabilities.semanticTokensProvider!; + if (typeof caps.full === 'object' && caps.full.delta) { + const delta = await client().sendRequest<{ edits: { start: number; deleteCount: number; data?: number[] }[] }>('textDocument/semanticTokens/full/delta', { textDocument: id, previousResultId: full.resultId }); + const patched = [...full.data]; + for (const edit of delta.edits.slice().sort((a, b) => b.start - a.start)) patched.splice(edit.start, edit.deleteCount, ...edit.data ?? []); + const current = await client().sendRequest<{ data: number[] }>('textDocument/semanticTokens/full', { textDocument: id }); + assert.deepStrictEqual(patched, current.data); + } else { + await assert.rejects(client().sendRequest('textDocument/semanticTokens/full/delta', { textDocument: id, previousResultId: full.resultId }), /not negotiated/); + } + if (caps.range) { + const range = await client().sendRequest<{ data: number[] }>('textDocument/semanticTokens/range', { textDocument: id, range: { start: { line: 0, character: 0 }, end: { line: 1, character: 0 } } }); + assert.deepStrictEqual(range.data, []); + } + await workspace.discard(document); + await workspace.open(data.file); + if (typeof caps.full === 'object' && caps.full.delta) { + const reopened = await client().sendRequest<{ data: number[] }>('textDocument/semanticTokens/full/delta', { textDocument: id, previousResultId: full.resultId }); + assert.ok(reopened.data.length); + } + }); + + playbook('X127', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: 'Resolve', uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file, data.source); + const actions = await client().sendRequest('textDocument/codeAction', { + textDocument: { uri: document.uri.toString() }, range: { start: { line: 0, character: 0 }, end: { line: 0, character: data.source.length } }, context: { diagnostics: [] } + }); + const matching = actions.filter(action => action.title === data.actionTitle); + assert.strictEqual(matching.length, 1); + const action = matching[0]; + const capabilities = client().initializeResult!.capabilities.codeActionProvider; + if (typeof capabilities === 'object' && capabilities.resolveProvider) { + assert.ok(action.data); + assert.strictEqual(action.edit, undefined); + const resolved = await client().sendRequest('codeAction/resolve', action); + assert.ok(resolved.edit?.documentChanges?.length); + assert.strictEqual(resolved.title, action.title); + await workspace.replace(document, '\n' + data.source); + await assert.rejects(client().sendRequest('codeAction/resolve', action), /expired or changed/); + } else assert.ok(action.edit); + }); + + playbook('X128', async (workspace, data) => { + for (const variant of data.variants) { + await workspace.write(variant.root, variant.source); + await workspace.write(variant.member, variant.memberSource); + await workspace.configure([{ name: 'App', uri: workspace.uri(variant.root).toString() }]); + const document = await workspace.open(variant.root); + await noErrors(document.uri); + const edit = new vscode.WorkspaceEdit(); + edit.renameFile(workspace.uri(variant.from), workspace.uri(variant.to), { overwrite: false }); + assert.ok(await vscode.workspace.applyEdit(edit)); + await eventually(async () => document.getText(), text => text === variant.expected, 'Native file rename updates references'); + const target = await workspace.open(variant.target); + assert.strictEqual(target.getText(), variant.targetSource); + await noErrors(document.uri); + } + }); + + playbook('X129', async (workspace, data) => { + const report = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, modelPath); + const model = JSON.parse(JSON.stringify(data.model).split('${workspace}').join(vscode.Uri.file(workspace.directory).toString())); + const settings = vscode.workspace.getConfiguration('xtc.compiler'); + const previous = settings.get('sourceModules'); + const writeModel = async () => { + await fs.mkdir(path.dirname(report.fsPath), { recursive: true }); + await fs.writeFile(report.fsPath, JSON.stringify(model)); + await updateCompilerConfiguration(); + }; + try { + await workspace.write(data.file, data.source); + await workspace.write(data.resource, data.contents); + await writeModel(); + await settings.update('sourceModules', null, vscode.ConfigurationTarget.Workspace); + const document = await workspace.open(data.file); + await noErrors(document.uri); + model.sourceSets[0].resourceRoots = []; + await writeModel(); + await diagnostics(document.uri, values => values.length > 0, 'Reimport disabled resources'); + model.sourceSets[0].resourceRoots = [workspace.uri('processed').toString()]; + await writeModel(); + await noErrors(document.uri); + await fs.writeFile(report.fsPath, '{'); + await updateCompilerConfiguration(); + await noErrors(document.uri); + const override = [{ name: 'GradleAssets', uri: document.uri.toString(), resourceRoots: [] }]; + await workspace.configure(override); + await diagnostics(document.uri, values => values.length > 0, 'Explicit override disables resources'); + await writeModel(); + assert.deepStrictEqual(vscode.workspace.getConfiguration('xtc.compiler').get('sourceModules'), override); + await diagnostics(document.uri, values => values.length > 0, 'Refresh preserves explicit override'); + await settings.update('sourceModules', null, vscode.ConfigurationTarget.Workspace); + await noErrors(document.uri); + await vscode.commands.executeCommand('xtc.showCompilerPaths'); + } finally { + await fs.rm(report.fsPath, { force: true }); + await settings.update('sourceModules', previous, vscode.ConfigurationTarget.Workspace); + await updateCompilerConfiguration(); + } + }); + + playbook('X130', async (workspace, data) => { + for (const file of data.files) await workspace.write(file.file, file.text); + await fs.mkdir(path.join(workspace.directory, data.destination)); + await discovered(workspace, async () => { + const document = await workspace.open(data.consumer); + await noErrors(document.uri); + const trace = new ExplorerMoveTrace(path.join(process.env.XTC_PLAYBOOK_REPORT_DIR!, 'X130-move-trace.json'), + vscode.Uri.file(workspace.directory), client()); + try { + // Use Explorer's actual batch operation and its Undo source. A pure file-only + // workspace.applyEdit is not on the unrelated Consumer editor's Undo stack. + assert.deepStrictEqual(data.sources, ['old', 'second']); + const paste = await cutAndPasteDirectories( + data.sources.map(source => workspace.uri(source)), workspace.uri(data.destination), trace); + const moved = async () => Promise.all(data.sources.map(async source => ({ + old: await fs.stat(path.join(workspace.directory, source)).then(() => true, () => false), + new: await fs.stat(path.join(workspace.directory, data.destination, source)).then(() => true, () => false) + }))); + await eventually(moved, values => values.every(value => !value.old && value.new), 'All selected directories moved'); + await noErrors(document.uri); + for (const [action, expected] of [['undo', false], ['redo', true]] as const) { + await trace.command('workbench.files.action.focusFilesExplorer'); + await trace.command(action); + await eventually(moved, values => values.every(value => value.old !== expected && value.new === expected), `${action} restores all paths`); + await noErrors(document.uri); + } + for (const file of data.files) { + const target = data.sources.some(source => file.file.startsWith(`${source}/`)) ? `${data.destination}/${file.file}` : file.file; + assert.strictEqual(await fs.readFile(path.join(workspace.directory, target), 'utf8'), file.text); + } + if (paste) throw new Error(`X130 FAILED: VS Code threw while clearing Cut highlighting. Move, Undo, Redo and resource assertions completed:\n${paste.stack}`); + } finally { await trace.save(); } + }); + }); + + playbook('X131', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const id = { textDocument: { uri: document.uri.toString() } }; + const lenses = await client().sendRequest('textDocument/codeLens', id); + const links = await client().sendRequest('textDocument/documentLink', id); + const hints = await client().sendRequest('textDocument/inlayHint', { ...id, range: { start: { line: 0, character: 0 }, end: { line: 6, character: 0 } } }); + const symbols = await client().sendRequest('workspace/symbol', { query: data.module }); + const lens = lenses.find(item => item.range.start.line === 0)!, link = links[0], hint = hints.find(item => item.data || item.tooltip)!, symbol = symbols[0]; + const resolve = async (method: string, item: T): Promise => item.data ? client().sendRequest(method, item) : item; + const resolvedLens = await resolve('codeLens/resolve', lens); + assert.deepStrictEqual(resolvedLens.range, lens.range); + assert.strictEqual(resolvedLens.command?.arguments?.length, 2); + assert.strictEqual((await resolve('documentLink/resolve', link)).target, data.link); + const resolvedHint = await resolve('inlayHint/resolve', hint); + assert.deepStrictEqual(resolvedHint.label, hint.label); + assert.ok(resolvedHint.tooltip); + assert.ok('range' in (await resolve('workspaceSymbol/resolve', symbol)).location); + await workspace.replace(document, '\n' + data.source); + for (const [method, item] of [['codeLens/resolve', lens], ['documentLink/resolve', link], ['inlayHint/resolve', hint], ['workspaceSymbol/resolve', symbol]] as const) { + if (item.data) await assert.rejects(client().sendRequest(method, item), /expired or changed/); + } + }); + + playbook('X132', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const params = { textDocument: { uri: document.uri.toString() }, options: { tabSize: 4, insertSpaces: true }, ranges: data.ranges }; + const edits = await client().sendRequest('textDocument/rangesFormatting', params); + assert.deepStrictEqual(edits.map(edit => edit.range.start.line), data.lines); + assert.ok(edits.every(edit => edit.newText === data.indent)); + const sync = client().initializeResult?.capabilities.textDocumentSync; + if (typeof sync === 'object') { + const save = { textDocument: params.textDocument, reason: 1 }; + if (sync.willSave) await client().sendNotification('textDocument/willSave', save); + if (sync.willSaveWaitUntil) assert.deepStrictEqual(await client().sendRequest('textDocument/willSaveWaitUntil', save), []); + } + assert.strictEqual(document.getText(), data.source); + }); + + playbook('X133', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const linked = async (anchor: string) => client().sendRequest('textDocument/linkedEditingRange', { + textDocument: { uri: document.uri.toString() }, position: document.positionAt(document.getText().indexOf(anchor)) + }); + const result = await linked(data.anchor); + assert.deepStrictEqual(result.ranges.map(range => document.offsetAt(new vscode.Position(range.start.line, range.start.character))), data.uses.map((anchor: string) => data.source.indexOf(anchor))); + for (const anchor of data.refused) assert.strictEqual((await linked(anchor)).ranges?.length ?? 0, 0); + await workspace.replace(document, data.broken); + await diagnostics(document.uri, values => values.length > 0, 'Broken source'); + assert.strictEqual((await linked(data.anchor)).ranges?.length ?? 0, 0); + await workspace.replace(document, data.source); + await noErrors(document.uri); + assert.strictEqual((await linked(data.anchor)).ranges.length, 2); + }); + + playbook('X134', async (workspace, data) => { + const external = await fs.mkdtemp(path.join(os.tmpdir(), 'xtc-playbook-source-')); + try { + const library = path.join(external, data.libraryFile); + await fs.writeFile(library, data.library); + await workspace.write(data.file, data.consumer); + await workspace.configure([ + { name: data.libraryModule, uri: vscode.Uri.file(library).toString() }, + { name: data.module, uri: workspace.uri(data.file).toString(), dependencies: [data.libraryModule] } + ]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + await fs.writeFile(library, data.brokenLibrary); + await diagnostics(document.uri, values => values.length > 0, 'External source changed'); + await fs.writeFile(library, data.library); + await noErrors(document.uri); + await fs.rm(library); + await diagnostics(document.uri, values => values.length > 0, 'External source deleted'); + await fs.writeFile(library, data.library); + await noErrors(document.uri); + } finally { await workspace.configure([]); await fs.rm(external, { recursive: true, force: true }); } + }); + + playbook('X135', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const visible = () => vscode.window.visibleTextEditors.some(editor => + /Ecstasy Language Server(?:\.\d+)?\.log$/.test(editor.document.uri.path)); + for (const command of ['xtc.showServerOutput', 'xtc.hideServerOutput', 'xtc.showServerOutput', 'xtc.hideServerOutput']) { + await vscode.commands.executeCommand(command); + await eventually(async () => visible(), value => value === (command === 'xtc.showServerOutput'), command); + } + assert.strictEqual(document.getText(), data.source); + }); + + playbook('X136', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const before = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + const config = vscode.workspace.getConfiguration('xtc'); + const original = config.inspect('inlayHints.enabled')?.workspaceValue; + const graph = config.get('compiler.sourceModules'); + try { + await config.update('inlayHints.enabled', false, vscode.ConfigurationTarget.Workspace); + const hints = await vscode.commands.executeCommand('vscode.executeInlayHintProvider', document.uri, new vscode.Range(0, 0, document.lineCount, 0)); + assert.deepStrictEqual(hints, []); + const report = await vscode.commands.executeCommand<{ configured: object; effective: { pid: number; compilerQueue: { queueSize: number; queuedJobs: string[] } } }>('xtc.showLanguageServiceStatus'); + assert.strictEqual(report?.effective.pid, before.pid); + assert.ok(Array.isArray(report.effective.compilerQueue.queuedJobs)); + assert.strictEqual(report.effective.compilerQueue.queueSize, report.effective.compilerQueue.queuedJobs.length); + assert.deepStrictEqual(config.get('compiler.sourceModules'), graph); + assert.strictEqual(document.getText(), data.source); + } finally { await config.update('inlayHints.enabled', original, vscode.ConfigurationTarget.Workspace); } + }); + + playbook('X137', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await workspace.replace(document, data.edited); + const config = vscode.workspace.getConfiguration('xtc.languageService'); + const original = config.inspect('textSynchronization')?.workspaceValue; + const status = async () => { + const current = getClient(); + if (!current?.isRunning()) return undefined; + try { return await current.sendRequest<{ pid: number; textSynchronization: string }>('xtc/languageServiceStatus'); } + catch { return undefined; } + }; + let previous = (await status())!.pid; + try { + for (const transport of ['incremental', 'full']) { + const started = Date.now(); + await config.update('textSynchronization', transport, vscode.ConfigurationTarget.Workspace); + const after = await eventually(status, value => value?.textSynchronization === transport && value.pid !== previous, `Restart into ${transport}`); + console.log(`[X137] ${transport} connection ready in ${Date.now() - started}ms; PID ${previous} -> ${after!.pid}`); + previous = after!.pid; + assert.strictEqual(document.getText(), data.edited); + assert.ok(document.isDirty, 'Restart must not save the buffer'); + await noErrors(document.uri); + await eventually( + () => symbols(document), + values => values.length > 0, 'Document symbols registered after restart'); + console.log(`[X137] ${transport} unsaved document ready in ${Date.now() - started}ms`); + } + } finally { + await config.update('textSynchronization', original, vscode.ConfigurationTarget.Workspace); + await eventually(status, value => value?.textSynchronization === config.get('textSynchronization'), 'Original transport restored'); + } + }); + + playbook('X138', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + const config = vscode.workspace.getConfiguration('xtc.formatting'); + const original = config.inspect('indentSize')?.workspaceValue; + const before = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + try { + await config.update('indentSize', data.indent, vscode.ConfigurationTarget.Workspace); + await eventually(async () => client().sendRequest<{ formatting: { indentSize: number } }>('xtc/languageServiceStatus'), value => value.formatting?.indentSize === data.indent, 'Live formatter configuration'); + const formatted = async () => { + const edits = await vscode.commands.executeCommand('vscode.executeFormatDocumentProvider', document.uri, { tabSize: 4, insertSpaces: true }); + assert.ok(edits, 'Formatting provider returns edits'); + return edits.sort((left, right) => document.offsetAt(right.range.start) - document.offsetAt(left.range.start)) + .reduce((text, edit) => text.slice(0, document.offsetAt(edit.range.start)) + edit.newText + text.slice(document.offsetAt(edit.range.end)), document.getText()); + }; + const expected = data.source.replace('\nInt', `\n${data.expectedIndent}Int`); + assert.strictEqual(await formatted(), expected); + await config.update('indentSize', 0, vscode.ConfigurationTarget.Workspace); + await client().sendNotification('workspace/didChangeConfiguration', { settings: {} }); + assert.strictEqual(await formatted(), expected); + const after = await client().sendRequest<{ pid: number; serverSaveFormatting: boolean }>('xtc/languageServiceStatus'); + assert.strictEqual(after.pid, before.pid); + assert.strictEqual(after.serverSaveFormatting, false); + assert.strictEqual(document.getText(), data.source); + } finally { await config.update('indentSize', original, vscode.ConfigurationTarget.Workspace); } + }); + + playbook('X139', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + const config = vscode.workspace.getConfiguration('xtc.languageService'); + const editor = vscode.workspace.getConfiguration('editor', { uri: document.uri, languageId: 'xtc' }); + const previousOwner = config.inspect('saveFormatting')?.workspaceValue; + const previousNative = editor.inspect('formatOnSave')?.workspaceValue; + const running = async () => { + const current = getClient(); + if (!current?.isRunning()) return undefined; + try { return await current.sendRequest<{ serverSaveFormatting: boolean }>('xtc/languageServiceStatus'); } + catch { return undefined; } + }; + try { + await editor.update('formatOnSave', false, vscode.ConfigurationTarget.Workspace); + await config.update('saveFormatting', 'server', vscode.ConfigurationTarget.Workspace); + await eventually(running, value => value?.serverSaveFormatting === true, 'Server save edits enabled'); + await workspace.replace(document, data.source + '\n'); + await document.save(); + assert.ok(document.getText().includes(' Int value = 1;')); + await editor.update('formatOnSave', true, vscode.ConfigurationTarget.Workspace); + const middleware = client().clientOptions.middleware!.willSaveWaitUntil!; + assert.deepStrictEqual(await middleware({ document, reason: vscode.TextDocumentSaveReason.Manual, waitUntil: () => {} }, () => { throw new Error('Native format-on-save must suppress the server hook'); }), []); + await workspace.replace(document, data.source); + await document.save(); + assert.strictEqual(document.getText(), data.formatted); + } finally { + await editor.update('formatOnSave', previousNative, vscode.ConfigurationTarget.Workspace); + await config.update('saveFormatting', previousOwner, vscode.ConfigurationTarget.Workspace); + await eventually(running, value => value?.serverSaveFormatting === false, 'Editor save ownership restored'); + } + }); + + playbook('X140', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + assert.strictEqual(client().initializeResult!.capabilities.positionEncoding, 'utf-16'); + const offset = data.source.lastIndexOf(data.anchor); + const at = document.positionAt(offset); + assert.ok((await hover(document, at)).includes(data.expected)); + const prepared = await client().sendRequest<{ range: { start: { character: number }; end: { character: number } } }>('textDocument/prepareRename', { + textDocument: { uri: document.uri.toString() }, position: at, + }); + assert.strictEqual(prepared.range.start.character, offset); + assert.strictEqual(prepared.range.end.character, offset + data.anchor.length); + }); + + playbook('X141', async (workspace, data) => { + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const messages: { message: string; verbose?: string }[] = []; + const registration = client().onNotification('$/logTrace', value => { messages.push(value); }); + try { + for (const level of data.levels) { + await client().sendNotification('$/setTrace', { value: level }); + const before = messages.length; + await client().sendRequest('textDocument/hover', { + textDocument: { uri: document.uri.toString() }, + position: document.positionAt(data.source.indexOf(data.anchor)), + }); + const trace = await eventually(async () => messages.slice(before).find(value => value.message.startsWith('textDocument/hover:')), + value => !!value, 'Server runtime trace'); + assert.strictEqual(typeof trace!.verbose === 'string', level === 'verbose'); + assert.ok(!JSON.stringify(trace).includes(data.source.trim())); + } + } finally { + registration.dispose(); + // Reconnect to restore the language client's own logTrace handler after this observer. + await vscode.commands.executeCommand('xtc.restartServer'); + } + }); + + playbook('X142', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const at = document.positionAt(data.source.indexOf(data.anchor)); + assert.ok((await hover(document, at)).includes(data.expected)); + const definitions = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', document.uri, at); + assert.ok(definitions?.some(value => value.range.start.character === data.source.indexOf(data.name))); + const references = await vscode.commands.executeCommand('vscode.executeReferenceProvider', document.uri, at); + assert.strictEqual(references?.length, data.references); + const edit = await vscode.commands.executeCommand('vscode.executeDocumentRenameProvider', document.uri, at, data.newName); + assert.ok(edit && await vscode.workspace.applyEdit(edit)); + assert.strictEqual(document.getText(), data.source.split(data.name).join(data.newName)); + await noErrors(document.uri); + await vscode.commands.executeCommand('undo'); + assert.strictEqual(document.getText(), data.source); + }); + + playbook('X143', async (workspace, data) => { + const source = `module ${data.module} {\n${Array.from({ length: data.declarations }, (_, i) => ` class ${data.prefix}${i} {}`).join('\n')}\n}\n`; + await workspace.write(data.file, source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + await symbols(document); + const batches: { name: string }[][] = []; + const observer = client().onProgress(new ProgressType<{ name: string }[]>(), data.token, values => { batches.push(values); }); + try { + const normal = await client().sendRequest<{ name: string }[]>('workspace/symbol', { query: data.prefix }); + const final = await client().sendRequest<{ name: string }[]>('workspace/symbol', { query: data.prefix, partialResultToken: data.token }); + assert.deepStrictEqual(final, []); + assert.ok(batches.length > 1 && batches.every(values => values.length <= data.batchSize)); + assert.strictEqual(normal.length, data.declarations); + assert.deepStrictEqual(batches.flat().map(value => value.name), normal.map(value => value.name)); + } finally { observer.dispose(); } + }); + + playbook('X144', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.write(data.otherFile, data.otherSource); + await workspace.configure([ + { name: data.module, uri: workspace.uri(data.file).toString() }, + { name: data.otherModule, uri: workspace.uri(data.otherFile).toString() } + ]); + const other = await workspace.open(data.otherFile); + const document = await workspace.open(data.file); + await symbols(document); + // Test-only SDK entry point: run the real incoming handler, including its version check. + // A renamed SDK method must fail this case, never silently fall back to workspace.applyEdit. + const apply = Reflect.get(client(), 'handleApplyWorkspaceEdit') as (params: ApplyWorkspaceEditParams) => Promise; + assert.strictEqual(typeof apply, 'function'); + const change = (target: vscode.TextDocument, version: number, newText: string) => ({ + textDocument: { uri: target.uri.toString(), version }, + edits: [{ range: { start: { line: 0, character: 0 }, end: target.positionAt(target.getText().length) }, newText }] + }); + const accepted = await apply.call(client(), { label: 'Ecstasy guarded edit', edit: { + documentChanges: [change(document, document.version, data.changed)] + } }); + assert.ok(accepted.applied); + assert.strictEqual(document.getText(), data.changed); + await vscode.commands.executeCommand('undo'); + assert.strictEqual(document.getText(), data.source); + const oldVersion = other.version; + await workspace.replace(other, data.otherChanged); + const refused = await apply.call(client(), { edit: { documentChanges: [ + change(document, document.version, data.changed), + change(other, oldVersion, data.otherSource) + ] } }); + assert.strictEqual(refused.applied, false); + assert.strictEqual(document.getText(), data.source); + assert.strictEqual(other.getText(), data.otherChanged); + await noErrors(document.uri); + await noErrors(other.uri); + }); + +} diff --git a/lang/vscode-extension/src/test/playbook/playbook.test.ts b/lang/vscode-extension/src/test/playbook/playbook.test.ts new file mode 100644 index 00000000000..8d8562f197c --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/playbook.test.ts @@ -0,0 +1,125 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { getClient } from '../../lsp-client'; +import { advancedCases, pullDiagnosticCases, typeHeaderCases } from './advanced'; +import { colorCases } from './colors'; +import { compilerImportCases } from './compilerImports'; +import { completionCases } from './completion'; +import { referenceLensCases } from './referenceLenses'; +import { documentationCases } from './documentation'; +import { editingClosureCases, formattingBreadthCases, linkedScopeCases, structuralRecoveryCases } from './editingClosure'; +import { configurationCases, dependencyCases } from './dependencies'; +import { graphCases, indexingCases, libraryContentCases, monikerCases } from './graph'; +import { highlightingCases } from './highlighting'; +import { inlineCompletionCases } from './inlineCompletion'; +import { librarySettingsCases } from './librarySettings'; +import { liveWorkspaceCases } from './liveWorkspace'; +import { localRefactoringCases, memberActionCases } from './memberActions'; +import { moduleCases } from './modules'; +import { navigationCases } from './navigation'; +import { platformCases } from './platform'; +import { progressCases } from './progress'; +import { refreshOverlapCases, reliabilityCases } from './reliability'; +import { propertyCases } from './properties'; +import { renameCases } from './rename'; +import { renameFamilyCases } from './renameFamilies'; +import { runtimeSettingsCases } from './runtimeSettings'; +import { editScenario, scenarioOffset, scenarioText, shared } from './shared'; +import { semanticCases, semanticClosureCases } from './semantics'; +import { syntaxCompletionCases } from './syntaxCompletion'; +import { typeMoveCases } from './typeMoves'; +import { client, diagnosticCode, diagnostics, eventually, fixture, loadFixtures, nextProblem, noErrors, playbook } from './support'; + +suite('XdkAdapter playbook', function () { + suiteSetup(async function () { + this.timeout(60_000); + await loadFixtures(); + const extension = vscode.extensions.all.find(item => item.packageJSON.name === 'xtc-language'); + assert.ok(extension, 'Development extension loaded'); + await extension.activate(); + await eventually(async () => !!getClient()?.initializeResult, Boolean, 'Language server initialization'); + const health = await client().sendRequest<{ healthy: boolean; adapter: string }>('xtc/healthCheck'); + assert.ok(health.healthy); + assert.strictEqual(health.adapter, 'XDK', 'Build this suite with -Plsp.adapter=compiler; another backend must not silently skip tests'); + }); + + navigationCases(); + completionCases(); + moduleCases(); + semanticCases(); + dependencyCases(); + renameCases(); + graphCases(); + propertyCases(); + advancedCases(); + liveWorkspaceCases(); + typeHeaderCases(['X106', 'X107', 'X108']); + renameFamilyCases(); + memberActionCases(); + pullDiagnosticCases(); + platformCases(); + progressCases(); + reliabilityCases(); + localRefactoringCases(); + syntaxCompletionCases(); + semanticClosureCases(); + renameFamilyCases(['X155']); + localRefactoringCases(['X156', 'X157']); + editingClosureCases(); + renameFamilyCases(['X159', 'X160']); + typeMoveCases(); + renameFamilyCases(['X164', 'X165', 'X166', 'X167', 'X168']); + typeMoveCases(['X169', 'X170', 'X171', 'X172', 'X173', 'X174', 'X175', 'X176']); + localRefactoringCases(['X177', 'X178', 'X179', 'X180', 'X181', 'X182', 'X183', 'X184', 'X185', 'X186', 'X187', 'X188', 'X189', 'X190', 'X191', 'X192', 'X193', 'X194', 'X195', 'X196', 'X197', 'X198', 'X199', 'X200', 'X201', 'X202', 'X203', 'X204', 'X205', 'X206', 'X207', 'X208', 'X209', 'X210', 'X211', 'X212', 'X213', 'X214', 'X215']); + typeMoveCases(['X216', 'X217', 'X218', 'X219']); + renameFamilyCases(['X220']); + localRefactoringCases(['X221', 'X222', 'X223', 'X224', 'X225', 'X226', 'X227', 'X228', 'X229', 'X230', 'X231', 'X232', 'X233', 'X234', 'X235', 'X236', 'X237', 'X238', 'X239', 'X240', 'X241', 'X242']); + editingClosureCases(['X243', 'X244']); + linkedScopeCases(); + structuralRecoveryCases(); + formattingBreadthCases(); + indexingCases(); + monikerCases(); + libraryContentCases(); + inlineCompletionCases(); + refreshOverlapCases(); + compilerImportCases(); + librarySettingsCases(); + runtimeSettingsCases(); + colorCases(); + highlightingCases(); + documentationCases(); + referenceLensCases(); + configurationCases(); + + playbook('7a.8', async (workspace, data) => { + const scenario = shared.warning; + const document = await workspace.open(scenario.file); + const result = await diagnostics(document.uri, values => values.some(item => diagnosticCode(item) === scenario.code), 'Duplicate annotation warning'); + assert.strictEqual(result.length, scenario.count); + assert.strictEqual(vscode.DiagnosticSeverity[result[0].severity].toUpperCase(), scenario.severity); + assert.ok(result[0].message.toLowerCase().includes(scenario.messageContains.toLowerCase())); + assert.strictEqual(result[0].source, 'xtc'); + const declaration = document.positionAt(scenarioOffset(document.getText(), scenario.declaration)); + assert.ok(result[0].range.isEqual(new vscode.Range(declaration, declaration.translate(0, data.warningCount)))); + await nextProblem(document, result); + const errorDocument = await workspace.open(data.errorFile); + await workspace.replace(errorDocument, fixture(data.errorFile).replace(data.replaceFrom, data.replaceWith)); + await diagnostics(errorDocument.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Error alongside warning'); + await diagnostics(document.uri, values => values.length === data.warningCount && values[0].severity === vscode.DiagnosticSeverity.Warning, 'Warning remains in other file'); + await workspace.replace(document, editScenario(fixture(scenario.file), scenario.edit)); + await noErrors(document.uri); + await diagnostics(errorDocument.uri, values => values.some(item => item.severity === vscode.DiagnosticSeverity.Error), 'Clearing warning preserves error'); + await workspace.replace(errorDocument, fixture(data.errorFile)); + await noErrors(errorDocument.uri); + await workspace.replace(document, fixture(scenario.file)); + await diagnostics(document.uri, values => values.length === scenario.count && diagnosticCode(values[0]) === scenario.code, 'Warning restored'); + }); + + playbook('7a.9', async (workspace, data) => { + const document = await workspace.open(data.file, data.moduleStart + Array.from({ length: data.declarationCount }, (_, index) => scenarioText(data.declaration, index, index)).join('\n') + data.moduleEnd); + const result = await diagnostics(document.uri, values => values.length > 0, 'Invalid source diagnostics'); + assert.ok(result.filter(item => item.severity === vscode.DiagnosticSeverity.Error).length <= data.maximumErrors, `${result.length} diagnostics`); + assert.ok(result.every(item => diagnosticCode(item) !== data.diagnosticCode)); + }); +}); diff --git a/lang/vscode-extension/src/test/playbook/progress.ts b/lang/vscode-extension/src/test/playbook/progress.ts new file mode 100644 index 00000000000..94031161a61 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/progress.ts @@ -0,0 +1,115 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { LSPErrorCodes, ProgressType, WorkDoneProgressBegin, WorkDoneProgressEnd, WorkDoneProgressReport } from 'vscode-languageclient/node'; +import { client, eventually, hover, noErrors, playbook } from './support'; +import { WorkbenchUi } from '../workbenchUi'; + +export function progressCases(): void { + playbook('X145', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + const cancelUi = process.env.XTC_PLAYBOOK_CANCEL_UI === 'true'; + const ui = cancelUi ? await WorkbenchUi.connect() : undefined; + const workload = (methods: number) => `module ${data.module} {\n static Int value = 1;\n${Array.from({ length: methods }, (_, i) => + ` Int read${i}() { return value; }`).join('\n')}\n}\n`; + const large = workload(data.methods); + const connection = client(); + const titles = new Map(); + const messages: string[] = []; + const original = connection.onProgress; + const subscribe = original.bind(connection); + // Observe registration without replacing the SDK handler or inventing progress events. + connection.onProgress =

(type: ProgressType

, token: string | number, handler: (value: P) => void) => subscribe(type, token, (value: P) => { + // The extension is bundled, so its ProgressType singleton has a different identity + // from the test runner's module. Observe the wire value, not that module singleton. + if (typeof value === 'object' && value !== null && 'kind' in value) { + const event = value as WorkDoneProgressBegin | WorkDoneProgressReport | WorkDoneProgressEnd; + if (event.kind === 'begin') { titles.set(token, event.title); } + if (titles.get(token) === 'Finding references' && event.message) { messages.push(event.message); } + if (event.kind === 'end') { titles.delete(token); } + } + handler(value); + }); + const features = Reflect.get(connection, '_features') as object[]; + const feature = features.find(item => Reflect.get(item, 'activeParts') instanceof Set); + assert.ok(feature, 'Installed SDK progress feature'); + // Test-only SDK observation: _progress and _cancellationToken are populated by the real + // workbench withProgress callback. Fail on SDK shape changes rather than mocking the UI. + const parts = Reflect.get(feature, 'activeParts') as Set; + assert.ok(parts instanceof Set); + function active(): vscode.CancellationToken[] { + return [...parts].filter(part => titles.get(Reflect.get(part, '_token')) === 'Finding references' + && Reflect.get(part, '_progress')).map(part => Reflect.get(part, '_cancellationToken')); + } + async function start(text: string) { + await workspace.replace(document, text, false); + let completed = false; + const deadline = Date.now() + 20_000; + async function references(): Promise { + assert.strictEqual(document.getText(), text, 'Progress workload was not externally edited'); + try { + return await client().sendRequest('textDocument/references', { + textDocument: { uri: document.uri.toString() }, position: document.positionAt(text.indexOf('value')), + context: { includeDeclaration: true } + }); + } catch (error) { + // A delayed fixture-create watch can retire this read. Re-request only; + // never replay the edit, cancel, restart or an already completed mutation. + if ((error as { code?: number }).code === LSPErrorCodes.ContentModified && Date.now() < deadline) { return references(); } + throw error; + } + } + const outcome = references().then(() => ({ error: undefined }), (error: unknown) => ({ error })) + .finally(() => { completed = true; }); + const tokens = await eventually(async () => { + assert.ok(!completed, 'Workload must remain pending until native progress is exercised'); + return active(); + }, values => values.length === 1, 'Real compiler reference progress'); + return { outcome, token: tokens[0], completed: () => completed }; + } + try { + await vscode.commands.executeCommand('notifications.clearAll'); + const canceled = await start(cancelUi ? workload(data.uiMethods) : large); + assert.ok(messages.some(message => message.includes(data.file.split('/').pop()!)), + 'Reference progress identifies the source being analyzed'); + if (cancelUi) { + // Only the visible Cancel button may cancel this query. A completed workload + // remains a failure; the UI driver has no direct cancellation fallback. + await vscode.commands.executeCommand('notifications.showList'); + await ui!.cancelReferences(); + } else { + // Ordinary unattended runs exercise the real workbench token callback. + const cancel = Reflect.get(canceled.token, 'cancel') as () => void; + assert.strictEqual(typeof cancel, 'function'); + cancel.call(canceled.token); + } + await eventually(async () => { + assert.ok(!canceled.completed() || canceled.token.isCancellationRequested, + 'Workload completed before Cancel was exercised'); + return canceled.token.isCancellationRequested; + }, value => value, 'Native progress Cancel action'); + const result = await canceled.outcome; + assert.strictEqual((result.error as { code?: number })?.code, LSPErrorCodes.RequestCancelled, 'Pending reference request is canceled'); + await eventually(async () => active().length, size => size === 0, 'Canceled progress disappears'); + assert.ok((await hover(document, document.positionAt(large.indexOf('value')))).includes('Int')); + await noErrors(document.uri); + const before = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus', {}); + const pending = await start(large + '// unsaved restart\n'); + await vscode.commands.executeCommand('xtc.restartServer'); + assert.ok((await pending.outcome).error, 'Restart retires the pending reader'); + await eventually(async () => { try { process.kill(before.pid, 0); return false; } catch { return true; } }, + value => value, 'Old server process exits'); + const after = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus', {}); + assert.notStrictEqual(after.pid, before.pid); + assert.strictEqual(document.getText(), large + '// unsaved restart\n'); + await workspace.replace(document, data.source); + await noErrors(document.uri); + await eventually(async () => active().length, size => size === 0, 'Disconnected progress disappears'); + } finally { + connection.onProgress = original; + if (cancelUi) { await vscode.commands.executeCommand('notifications.hideList'); } + await ui?.close(); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/properties.ts b/lang/vscode-extension/src/test/playbook/properties.ts new file mode 100644 index 00000000000..80d3afd610e --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/properties.ts @@ -0,0 +1,65 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { noErrors, playbook, position, targets } from './support'; + +function locations(document: vscode.TextDocument, anchors: { anchor: string; offset: number; length: number }[]): vscode.Location[] { + return anchors.map(({ anchor, offset, length }) => { + const at = position(document, anchor, offset); + return new vscode.Location(document.uri, new vscode.Range(at, at.translate(0, length))); + }); +} + +function assertLocations(actual: vscode.Location[], expected: vscode.Location[]): void { + const keys = (values: vscode.Location[]) => values.map(value => `${value.uri}:${JSON.stringify(value.range)}`).sort(); + assert.deepStrictEqual(keys(actual), keys(expected)); +} + +export function propertyCases(): void { + playbook('X64', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + const expected = locations(document, data.locations); + for (const at of [position(document, data.anchor2, data.offset2), position(document, data.anchor, data.offset)]) { + assertLocations(await targets(document, 'Implementation', at), expected); + } + }); + + playbook('X65', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { anchor, offset, expected } of data.variants) { + assertLocations(await targets(document, 'Implementation', position(document, anchor, offset)), locations(document, expected)); + } + }); + + playbook('X66', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { anchor, offset } of data.variants) { + assert.deepStrictEqual(await targets(document, 'Implementation', position(document, anchor, offset)), []); + } + }); + + playbook('X67', async (workspace, data) => { + const original = data.original; + await workspace.write(data.memberFile, original); + const document = await workspace.open(data.rootFile); + await noErrors(document.uri); + const lookup = () => targets(document, 'Implementation', position(document, data.anchor, data.lineShift)); + const initial = await lookup(); + assert.strictEqual(initial.length, data.targetCount); + const memberUri = workspace.uri(data.memberFile); + const closed = initial.find(item => item.uri.toString() === memberUri.toString()); + assert.ok(closed); + assert.ok(closed.range.isEqual(new vscode.Range(data.declarationLine, original.indexOf(data.getter), data.declarationLine, original.indexOf(data.getter) + data.targetCount))); + const member = await workspace.open(data.memberFile); + await workspace.replace(member, '\n\n' + original); + const moved = (await lookup()).find(item => item.uri.toString() === memberUri.toString()); + assert.ok(moved?.range.isEqual(new vscode.Range(closed.range.start.translate(data.lineShift), closed.range.end.translate(data.lineShift)))); + await workspace.replace(member, data.replaceWith); + assert.deepStrictEqual(await lookup(), []); + await workspace.replace(member, original); + await noErrors(member.uri); + assertLocations(await lookup(), initial); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/referenceLenses.ts b/lang/vscode-extension/src/test/playbook/referenceLenses.ts new file mode 100644 index 00000000000..b2cf91c0b91 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/referenceLenses.ts @@ -0,0 +1,52 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { WorkbenchUi } from '../workbenchUi'; +import { client, eventually, noErrors, playbook } from './support'; + +export function referenceLensCases(): void { + playbook('X279', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.write(data.consumerFile, data.consumer); + await workspace.configure([ + { name: data.module, uri: workspace.uri(data.file).toString() }, + { name: data.consumerModule, uri: workspace.uri(data.consumerFile).toString(), dependencies: [data.module] } + ]); + const document = await workspace.open(data.file); + const settings = vscode.workspace.getConfiguration('xtc.codeLens', document.uri); + const before = settings.inspect('references')?.workspaceValue; + const ui = await WorkbenchUi.connect(); + const token = new vscode.CancellationTokenSource(); + try { + await settings.update('references', true, vscode.ConfigurationTarget.Workspace); + await noErrors(document.uri); + const provider = client().getFeature('textDocument/codeLens').getProvider(document)!.provider!; + const lenses = async () => Promise.all((await provider.provideCodeLenses(document, token.token) ?? []) + .map(lens => lens.isResolved ? lens : provider.resolveCodeLens!(lens, token.token))); + await eventually(lenses, values => values.some(lens => lens?.command?.title === data.initialTitle), 'Closed-consumer reference count'); + const link = ui.page.locator('.editor-group-container.active .codelens-decoration a').filter({ hasText: data.initialTitle }); + await link.waitFor({ state: 'visible', timeout: 30_000 }); + await ui.screenshot('X279-reference-lens'); + await link.click({ timeout: 5_000 }); + const peek = ui.page.locator('.peekview-widget'); + await peek.waitFor({ state: 'visible', timeout: 15_000 }); + await eventually(() => peek.innerText(), text => text.includes(data.consumerFile) && text.includes(data.file), 'Native reference peek contains both files'); + await vscode.commands.executeCommand('closeReferenceSearch'); + const consumer = await workspace.open(data.consumerFile); + await workspace.replace(consumer, data.changedConsumer); + await noErrors(consumer.uri); + await workspace.open(data.file); + await eventually(lenses, values => values.some(lens => lens?.command?.title === data.changedTitle), 'Edited-consumer reference count'); + await settings.update('references', false, vscode.ConfigurationTarget.Workspace); + await eventually(lenses, values => values.length === 1 && values[0]?.command?.command === 'xtc.runModule', 'Reference toggle retains Run'); + await eventually(() => ui.page.locator('.editor-group-container.active .codelens-decoration').allTextContents(), + values => values.some(text => text.includes('Run')) && values.every(text => !text.includes('reference')), 'Native reference lenses disappear'); + await settings.update('references', true, vscode.ConfigurationTarget.Workspace); + await eventually(lenses, values => values.some(lens => lens?.command?.title === data.changedTitle), 'Reference toggle restores current count'); + assert.strictEqual(document.getText(), data.source); + } finally { + token.dispose(); + await settings.update('references', before, vscode.ConfigurationTarget.Workspace); + await ui.close(); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/reliability.ts b/lang/vscode-extension/src/test/playbook/reliability.ts new file mode 100644 index 00000000000..938de70a30d --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/reliability.ts @@ -0,0 +1,160 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { WorkbenchUi } from '../workbenchUi'; +import { CloseAction } from 'vscode-languageclient/node'; +import { client, eventually, noErrors, playbook } from './support'; + +export function reliabilityCases(): void { + playbook('X146', async (workspace, data) => { + await workspace.write(data.library, data.original); + await workspace.write(data.consumer, data.source); + await workspace.configure([ + { name: data.libraryModule, uri: workspace.uri(data.library).toString() }, + { name: data.consumerModule, uri: workspace.uri(data.consumer).toString(), dependencies: [data.libraryModule] } + ]); + const library = await workspace.open(data.library); + const consumer = await workspace.open(data.consumer); + const version = consumer.version; + const connection = client(); + const hints = connection.getFeature('textDocument/inlayHint').getProvider(consumer)!; + const tokens = connection.getFeature('textDocument/semanticTokens').getProvider(consumer)!; + const lenses = connection.getFeature('textDocument/codeLens').getProvider(consumer)!; + const folding = connection.getFeature('textDocument/foldingRange').getProvider(consumer)!; + const diagnostics = connection.getFeature('textDocument/diagnostic').getProvider(consumer)!; + const events: string[] = []; + const subscriptions = [hints.onDidChangeInlayHints.event(() => events.push('hints')), + tokens.onDidChangeSemanticTokensEmitter.event(() => events.push('tokens')), + lenses.onDidChangeCodeLensEmitter.event(() => events.push('lenses')), + folding.onDidChangeFoldingRange.event(() => events.push('folding')), + diagnostics.onDidChangeDiagnosticsEmitter.event(() => events.push('diagnostics'))]; + const cancellation = new vscode.CancellationTokenSource(); + async function verify(expected: string) { + await eventually(async () => { + const values = await hints.provider.provideInlayHints(consumer, new vscode.Range(0, 0, consumer.lineCount, 0), cancellation.token); + return values?.map(hint => typeof hint.label === 'string' ? hint.label : hint.label.map(part => part.value).join('')) ?? []; + }, labels => labels.some(label => label.includes(expected)), `Untouched consumer receives ${expected} inlay`); + assert.strictEqual(consumer.version, version); + assert.strictEqual(consumer.getText(), data.source); + } + try { + await verify(data.before); + for (const [source, expected] of [[data.changed, data.after], [data.original, data.before]]) { + events.length = 0; + await workspace.replace(library, source); + await eventually(async () => [...events], values => + ['hints', 'tokens', 'lenses', 'folding', 'diagnostics'].every(feature => values.includes(feature)), + 'All five negotiated native providers receive dependency refresh'); + await verify(expected); + assert.ok((await lenses.provider!.provideCodeLenses(consumer, cancellation.token))!.length > 0); + assert.ok((await folding.provider.provideFoldingRanges(consumer, {}, cancellation.token))!.length > 0); + await noErrors(consumer.uri); + } + } finally { subscriptions.forEach(subscription => subscription.dispose()); cancellation.dispose(); } + }); + + playbook('X147', async (workspace, data) => { + await workspace.write(data.file, data.source); + await workspace.configure([{ name: data.module, uri: workspace.uri(data.file).toString() }]); + const document = await workspace.open(data.file); + await noErrors(document.uri); + const config = vscode.workspace.getConfiguration('xtc'); + const originalSettings = config.inspect('inlayHints.enabled')?.workspaceValue; + const connection = client(); + const originalRequest = connection.sendRequest; + const releases = new Set<() => void>(); + let hold = true; + // Hold a real reply at the UI boundary, so the older command actually finishes last. + connection.sendRequest = ((...args: unknown[]) => { + const response = Reflect.apply(originalRequest, connection, args) as Promise; + return hold && args[0] === 'xtc/languageServiceStatus' + ? response.then(value => new Promise(resolve => { + const release = () => { releases.delete(release); resolve(value); }; + releases.add(release); + })) : response; + }) as typeof connection.sendRequest; + async function delayed() { + hold = true; + const pending = vscode.commands.executeCommand('xtc.showLanguageServiceStatus'); + await eventually(async () => releases.size, count => count === 1, 'Older status reply is held'); + hold = false; + return { pending, release: [...releases][0] }; + } + try { + const older = await delayed(); + assert.ok(await vscode.commands.executeCommand('xtc.showLanguageServiceStatus')); + older.release(); + assert.strictEqual(await older.pending, undefined, 'Older command cannot replace newer report'); + const settings = await delayed(); + await config.update('inlayHints.enabled', !config.get('inlayHints.enabled', true), vscode.ConfigurationTarget.Workspace); + settings.release(); + assert.strictEqual(await settings.pending, undefined, 'Old report cannot publish after settings change'); + const retired = await delayed(); + const before = await Reflect.apply(originalRequest, connection, ['xtc/languageServiceStatus']) as { pid: number }; + await vscode.commands.executeCommand('xtc.restartServer'); + retired.release(); + assert.strictEqual(await retired.pending, undefined, 'Retired connection cannot publish its old report'); + assert.strictEqual((await connection.clientOptions.errorHandler!.closed()).action, CloseAction.DoNotRestart); + const current = await vscode.commands.executeCommand<{ effective: { pid: number } }>('xtc.showLanguageServiceStatus'); + assert.notStrictEqual(current!.effective.pid, before.pid); + await eventually(async () => { try { process.kill(before.pid, 0); return false; } catch { return true; } }, value => value, 'Retired server exits'); + assert.strictEqual(document.getText(), data.source); + } finally { + connection.sendRequest = originalRequest; + releases.forEach(release => release()); + await config.update('inlayHints.enabled', originalSettings, vscode.ConfigurationTarget.Workspace); + } + }); +} + +export function refreshOverlapCases(): void { + playbook('X259', async (workspace, data) => { + await workspace.write(data.library, data.original); + await workspace.write(data.consumer, data.source); + await workspace.configure([ + { name: data.libraryModule, uri: workspace.uri(data.library).toString() }, + { name: data.consumerModule, uri: workspace.uri(data.consumer).toString(), dependencies: [data.libraryModule] } + ]); + const library = await workspace.open(data.library); + const consumer = await workspace.open(data.consumer); + const version = consumer.version; + const config = vscode.workspace.getConfiguration('xtc'); + const original = config.inspect('inlayHints.enabled')?.workspaceValue; + const ui = await WorkbenchUi.connect(); + const hints = (enabled: boolean) => config.update('inlayHints.enabled', enabled, vscode.ConfigurationTarget.Workspace); + async function visible(expected?: string) { + // Inspect the renderer's installed hint labels, without executing an inlay provider. + await eventually(() => ui.inlayLabels(), labels => expected === undefined ? labels.length === 0 : + labels.some(label => label.includes(expected)) && + !labels.some(label => label.includes(expected === data.before ? data.after : data.before)), + `Untouched consumer displays ${expected ?? 'no'} inlay hints`); + assert.strictEqual(consumer.getText(), data.source); + assert.strictEqual(consumer.version, version); + await ui.screenshot(`X259-${expected ?? 'disabled'}`); + } + try { + await hints(true); + await visible(data.before); + await hints(false); + await visible(); + const previous = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + await workspace.replace(library, data.changed, false); + await hints(true); + await vscode.commands.executeCommand('xtc.restartServer'); + await eventually(async () => { + try { process.kill(previous.pid, 0); return false; } + catch (error) { if ((error as NodeJS.ErrnoException).code === 'ESRCH') return true; throw error; } + }, Boolean, 'Retired refresh producer exits'); + assert.notStrictEqual((await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus')).pid, previous.pid); + await visible(data.after); + await noErrors(consumer.uri); + await hints(false); + await workspace.replace(library, data.original, false); + await hints(true); + await visible(data.before); + await noErrors(consumer.uri); + } finally { + await config.update('inlayHints.enabled', original, vscode.ConfigurationTarget.Workspace); + await ui.close(); + } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/rename.ts b/lang/vscode-extension/src/test/playbook/rename.ts new file mode 100644 index 00000000000..a24e5f4046d --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/rename.ts @@ -0,0 +1,93 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { TextDocumentEdit, WorkspaceEdit } from 'vscode-languageclient/node'; +import { scenarioRegex } from './shared'; +import { client, diagnostics, fixture, noErrors, playbook, position, symbols } from './support'; + +async function rename(document: vscode.TextDocument, at: vscode.Position, newName: string): Promise { + return client().sendRequest('textDocument/rename', { + textDocument: { uri: document.uri.toString() }, position: at, newName + }); +} + +async function applyRename(document: vscode.TextDocument, at: vscode.Position, newName: string): Promise { + const edit = await vscode.commands.executeCommand('vscode.executeDocumentRenameProvider', document.uri, at, newName); + assert.ok(edit?.size, 'Rename must produce a workspace edit'); + assert.ok(await vscode.workspace.applyEdit(edit)); + await symbols(document); + await noErrors(document.uri); +} + +export function renameCases(): void { + playbook('X53', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + await applyRename(document, position(document, data.anchor), data.replaceWith); + assert.strictEqual(document.getText(), fixture(data.file).replace(scenarioRegex(data.replaceFrom), data.replaceWith)); + }); + + playbook('X54', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const anchor of data.anchors) { + await workspace.replace(document, fixture(data.file)); + await applyRename(document, position(document, anchor, anchor === data.declaration ? data.callOffset : data.declarationOffset), data.replaceWith); + assert.strictEqual(document.getText(), fixture(data.file).replace(scenarioRegex(data.replaceFrom), data.replaceWith)); + assert.ok(document.getText().includes(data.namedArgument)); + } + }); + + playbook('X55', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.strictEqual(await rename(document, position(document, data.anchor), data.newName), null); + assert.strictEqual(document.getText(), fixture(data.file)); + await noErrors(document.uri); + }); + + playbook('X56', async (workspace, data) => { + const document = await workspace.open(data.file); + for (const anchor of data.variants) { + assert.strictEqual(await rename(document, position(document, anchor), data.newName), null, anchor); + } + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.replaceWith)); + assert.strictEqual(await rename(document, position(document, data.anchor, data.offset), data.newName), null); + }); + + playbook('X57', async (workspace, data) => { + const document = await workspace.open(data.file); + const edit = await rename(document, position(document, data.anchor), data.newName); + assert.ok(edit?.documentChanges?.length); + assert.ok(!edit.changes, 'Only versioned documentChanges are supported'); + const change = edit.documentChanges[0]; + assert.ok(TextDocumentEdit.is(change)); + assert.strictEqual(change.textDocument.version, document.version); + await workspace.replace(document, '\n' + fixture(data.file)); + assert.notStrictEqual(change.textDocument.version, document.version); + // The language client's rename provider checks this before returning an edit. + // Converting directly to vscode.WorkspaceEdit would discard the protocol version. + assert.strictEqual(client().validateWorkspaceEdit(edit), false, 'Client must reject an edit for the old document version'); + assert.strictEqual(document.getText(), '\n' + fixture(data.file)); + + const pending = rename(document, position(document, data.anchor), data.newName).catch(error => { + assert.ok((data.cancellationCodes as (number)[]).includes(error.code), String(error)); + return null; + }); + await workspace.discard(document); + await pending; + const reopened = await workspace.open(data.file); + assert.strictEqual(reopened.getText(), fixture(data.file)); + await applyRename(reopened, position(reopened, data.anchor), data.newName); + }); + + playbook('X58', async (workspace, data) => { + const document = await workspace.open(data.file); + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.replaceWith)); + await diagnostics(document.uri, values => values.length > 0, 'Broken rename source'); + assert.strictEqual(await rename(document, position(document, data.anchor), data.newName), null); + await workspace.replace(document, fixture(data.file)); + await noErrors(document.uri); + for (const name of data.invalidNames) { + assert.strictEqual(await rename(document, position(document, data.anchor), name), null, name); + } + await applyRename(document, position(document, data.anchor), data.newName); + }); +} diff --git a/lang/vscode-extension/src/test/playbook/renameFamilies.ts b/lang/vscode-extension/src/test/playbook/renameFamilies.ts new file mode 100644 index 00000000000..1b5a3bea5ac --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/renameFamilies.ts @@ -0,0 +1,177 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { compilerSettingsLocation, compilerSourceModules, renameWithConfiguration } from '../../rename-proposal'; +import { focusTestWindow } from '../native-focus'; +import { discovered } from './liveWorkspace'; +import { client, eventually, noErrors, playbook, position, symbols, Workspace } from './support'; + +/** Inspect edits to closed consumers without opening their editor tabs before Undo. */ +async function contents(workspace: Workspace, file: string): Promise { + const uri = workspace.uri(file); + return vscode.workspace.textDocuments.find(document => !document.isClosed && document.uri.toString() === uri.toString())?.getText() + ?? fs.readFile(uri.fsPath, 'utf8'); +} + +/** Exercise the installed client's real proposal, including the asynchronous conversion boundary. */ +async function configurationGuards(document: vscode.TextDocument, at: vscode.Position, replacement: string, + token: vscode.CancellationToken): Promise { + const languageClient = client(); + const otherFolder = vscode.workspace.workspaceFolders?.[1]; + if (otherFolder) { + const configuration = vscode.workspace.getConfiguration('xtc.compiler', otherFolder.uri); + // The extension declares window scope. VS Code itself prevents an effective folder override. + await assert.rejects(() => Promise.resolve(configuration.update('sourceModules', [], vscode.ConfigurationTarget.WorkspaceFolder)), + /does not support the folder resource scope/); + assert.strictEqual(configuration.inspect('sourceModules')?.workspaceFolderValue, undefined); + } + const settings = await vscode.workspace.openTextDocument(compilerSettingsLocation()!.uri); + const original = settings.getText(); + const converter = languageClient.protocol2CodeConverter; + // No fake compiler reply: delay the actual conversion only long enough for a real settings edit. + const delayedConverter = new Proxy(converter, { + get(target, property, receiver) { + if (property === 'asWorkspaceEdit') return async (...args: Parameters) => { + const converted = await converter.asWorkspaceEdit(...args); + const edit = new vscode.WorkspaceEdit(); + edit.insert(settings.uri, settings.positionAt(settings.getText().length), '\n// Settings edited during Rename\n'); + assert.ok(await vscode.workspace.applyEdit(edit)); + return converted; + }; + return Reflect.get(target, property, receiver); + } + }); + const delayedClient = new Proxy(languageClient, { + get(target, property) { + if (property === 'protocol2CodeConverter') return delayedConverter; + const value = Reflect.get(target, property, target); + return typeof value === 'function' ? value.bind(target) : value; + } + }); + try { + const outcome = await eventually(async () => { + try { + const edit = await renameWithConfiguration(delayedClient, document, at, replacement, token); + return edit ? { edit } : undefined; + } catch (error) { + return { error }; + } + }, value => value !== undefined, 'Rename proposal survives fixture watcher delivery'); + assert.ok(outcome && 'error' in outcome, 'Conversion must refuse the intervening settings edit'); + assert.match(String(outcome.error), /changed while converting the refactoring/); + } finally { + const restore = new vscode.WorkspaceEdit(); + restore.replace(settings.uri, new vscode.Range(settings.positionAt(0), settings.positionAt(settings.getText().length)), original); + assert.ok(await vscode.workspace.applyEdit(restore)); + assert.ok(await settings.save()); + } +} + +const renameCases = ['X109', 'X110', 'X111', 'X112', 'X113', 'X114', 'X115', 'X116', 'X117', 'X118', 'X119', 'X120', 'X121'] as const; + +export function renameFamilyCases(ids: readonly (typeof renameCases[number] | 'X155' | 'X159' | 'X160' | 'X164' | 'X165' | 'X166' | 'X167' | 'X168' | 'X220')[] = renameCases): void { + for (const id of ids) { + playbook(id, async (workspace, data) => { + for (const file of data.files) await workspace.write(file.file, file.source); + await discovered(workspace, async () => { + if ('sourceModules' in data) await workspace.configure(data.sourceModules.map(module => ({ + ...module, + uri: workspace.uri(module.uri).toString(), + resourceRoots: 'resourceRoots' in module ? module.resourceRoots?.map(root => workspace.uri(root).toString()) : undefined + }))); + if ('projectSettingsRoundTrip' in data && data.projectSettingsRoundTrip) { + assert.ok(vscode.workspace.getConfiguration('xtc.compiler').inspect('sourceModules')?.workspaceValue, + 'The explicit graph must be persisted in workspace settings'); + } + const document = await workspace.open(data.file); + await noErrors(document.uri); + if ('navigationAnchor' in data) { + const references = await vscode.commands.executeCommand( + 'vscode.executeReferenceProvider', document.uri, position(document, data.navigationAnchor)); + assert.deepStrictEqual(references?.map(reference => reference.uri.fsPath).sort(), + data.navigationFiles.map(file => workspace.uri(file).fsPath).sort()); + } + for (const file of data.files.filter(file => file.file !== data.file)) { + assert.ok(!vscode.workspace.textDocuments.some(document => !document.isClosed && document.uri.fsPath === workspace.uri(file.file).fsPath), + `Consumer must be closed before rename: ${file.file}`); + } + const provider = client().getFeature('textDocument/rename').getProvider(document); + assert.ok(provider); + const cancellation = new vscode.CancellationTokenSource(); + try { + if ('sourceModules' in data) { + await configurationGuards(document, position(document, data.anchor), data.replacement, cancellation.token); + for (const file of data.files) assert.strictEqual(await contents(workspace, file.file), file.source); + } + if (data.files.some(file => file.file !== file.destination && file.source !== file.expected)) { + const configuration = vscode.workspace.getConfiguration('files.refactoring'); + const previous = configuration.inspect('autoSave')?.workspaceValue; + try { + await configuration.update('autoSave', false, vscode.ConfigurationTarget.Workspace); + const refusal = await eventually(async () => { + try { + const edit = await provider.provideRenameEdits(document, position(document, data.anchor), data.replacement, cancellation.token); + assert.ok(!edit, 'An unsafe resource history must not be offered'); + return undefined; + } catch (error) { return error; } + }, Boolean, 'Resource rename refuses disabled refactoring auto-save'); + assert.match(String(refusal), /requires files\.refactoring\.autoSave/); + for (const file of data.files) assert.strictEqual(await contents(workspace, file.file), file.source); + } finally { await configuration.update('autoSave', previous, vscode.ConfigurationTarget.Workspace); } + } + const edit = await eventually( + async () => provider.provideRenameEdits(document, position(document, data.anchor), data.replacement, cancellation.token), + Boolean, `${id} rename proof after fixture discovery`); + assert.ok(edit); + assert.ok(await vscode.workspace.applyEdit(edit, { isRefactoring: true })); + } finally { cancellation.dispose(); } + for (const file of data.files) { + assert.strictEqual(await contents(workspace, file.destination), file.expected); + if (file.destination !== file.file) assert.strictEqual(await fs.stat(workspace.uri(file.file).fsPath).catch(() => null), null); + } + const destination = data.files.find(file => file.file === data.file)!.destination; + const renamed = await workspace.open(destination); + await symbols(renamed); + await noErrors(renamed.uri); + if ('sourceModules' in data) { + assert.ok(JSON.stringify(compilerSourceModules()).includes('Renamed.example.org')); + assert.ok(JSON.stringify(compilerSourceModules()).includes('assets') && JSON.stringify(compilerSourceModules()).includes('fallback')); + assert.ok(!JSON.stringify(compilerSourceModules()).includes('Library.example.org')); + assert.ok(await vscode.workspace.saveAll()); + } + await focusTestWindow(); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + await vscode.commands.executeCommand('undo'); + await eventually(() => contents(workspace, data.file).catch(() => ''), + text => text === data.files.find(file => file.file === data.file)!.source, `${id} undo restores the request document`); + for (const file of data.files) { + assert.strictEqual(await contents(workspace, file.file), file.source); + if (file.destination !== file.file) assert.strictEqual(await fs.stat(workspace.uri(file.destination).fsPath).catch(() => null), null); + } + const restored = await workspace.open(data.file); + await symbols(restored); + await noErrors(restored.uri); + if ('sourceModules' in data) { + assert.ok(JSON.stringify(compilerSourceModules()).includes('Library.example.org')); + assert.ok(JSON.stringify(compilerSourceModules()).includes('assets') && JSON.stringify(compilerSourceModules()).includes('fallback')); + assert.ok(!JSON.stringify(compilerSourceModules()).includes('Renamed.example.org')); + await focusTestWindow(); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + await vscode.commands.executeCommand('redo'); + await eventually(async () => compilerSourceModules(), + graph => JSON.stringify(graph).includes('Renamed.example.org'), 'Redo restores the replacement graph'); + await eventually(async () => Promise.all(data.files.map(file => contents(workspace, file.destination).catch(() => ''))), + texts => texts.every((text, index) => text === data.files[index].expected), 'Redo restores every source and resource'); + await workspace.open(destination); + await focusTestWindow(); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + await vscode.commands.executeCommand('undo'); + await eventually(async () => compilerSourceModules(), + graph => JSON.stringify(graph).includes('Library.example.org'), 'Second undo restores the original graph'); + for (const file of data.files) assert.strictEqual(await contents(workspace, file.file), file.source); + assert.ok(await vscode.workspace.saveAll()); + } + }); + }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/runtimeSettings.ts b/lang/vscode-extension/src/test/playbook/runtimeSettings.ts new file mode 100644 index 00000000000..aac9527e877 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/runtimeSettings.ts @@ -0,0 +1,118 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { stopLanguageClient } from '../../lsp-client'; +import { readArchive } from '../archive'; +import { PlaybookCleanupFailure } from '../wait'; +import { WorkbenchUi } from '../workbenchUi'; +import { catalog } from './shared'; +import { client, noErrors, playbook } from './support'; + +interface Status { pid: number; jvmOptions: string[]; logs: { directory: string; retention: Record } } +export function runtimeSettingsCases(): void { + for (const id of ['X269', 'X270'] as const) playbook(id, async workspace => { + const data = catalog.common.runtimeSettings; + const settings = vscode.workspace.getConfiguration('xtc'); + const previousVm = settings.inspect('java.vmOptions')?.globalValue; + const previousLogs = settings.inspect('server.logs')?.globalValue; + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const status = () => client().sendRequest('xtc/languageServiceStatus'); + const before = await status(); + const ui = await WorkbenchUi.connect(); + try { + await settings.update('java.vmOptions', data.vmOptions, vscode.ConfigurationTarget.Global); + if (id === 'X270') await settings.update('server.logs', data.logs, vscode.ConfigurationTarget.Global); + assert.strictEqual((await status()).pid, before.pid, 'Saving runtime settings must not restart a working server'); + await vscode.commands.executeCommand('xtc.restartServer'); + await noErrors(document.uri); + const applied = await status(); + assert.notStrictEqual(applied.pid, before.pid); + assert.ok(applied.jvmOptions.includes(data.vmOptions[0])); + if (id === 'X269') { + await settings.update('java.vmOptions', data.invalidOptions, vscode.ConfigurationTarget.Global); + await assert.rejects(async () => vscode.commands.executeCommand('xtc.restartServer'), /heap/); + await ui.page.locator('.notification-list-item').filter({ hasText: 'Invalid Ecstasy runtime/log settings' }) + .filter({ hasText: 'heap' }).waitFor({ state: 'visible', timeout: 5_000 }); + assert.strictEqual((await status()).pid, applied.pid); + await noErrors(document.uri); + } else { + assert.deepStrictEqual(applied.logs.retention, data.logs); + assert.notStrictEqual(applied.logs.directory, before.logs.directory); + const destination = vscode.Uri.file(path.join(workspace.directory, 'server-logs.zip')); + await vscode.commands.executeCommand('xtc.exportServerLogs', destination); + const archive = await fs.readFile(destination.fsPath); + assert.strictEqual(archive.readUInt32LE(0), 0x04034b50); + assert.ok(archive.includes(Buffer.from('manifest.json')) && archive.includes(Buffer.from('server.log'))); + assert.ok(archive.length < 6 * 1024 * 1024); + await settings.update('server.logs', { ...data.logs, totalSizeMb: 0 }, vscode.ConfigurationTarget.Global); + await assert.rejects(async () => vscode.commands.executeCommand('xtc.restartServer'), /retention/i); + await ui.page.locator('.notification-list-item').filter({ hasText: 'Invalid Ecstasy runtime/log settings' }) + .filter({ hasText: 'retention' }).waitFor({ state: 'visible', timeout: 5_000 }); + assert.strictEqual((await status()).pid, applied.pid); + } + assert.strictEqual(document.getText(), data.source); + } finally { + try { + await settings.update('java.vmOptions', previousVm, vscode.ConfigurationTarget.Global); + await settings.update('server.logs', previousLogs, vscode.ConfigurationTarget.Global); + await vscode.commands.executeCommand('xtc.restartServer'); + await vscode.commands.executeCommand('notifications.clearAll'); + } catch (error) { throw new PlaybookCleanupFailure([error]); } + finally { await ui.close(); } + } + }); + for (const id of ['X271', 'X272'] as const) playbook(id, async workspace => { + const data = catalog.common.runtimeSettings; + const settings = vscode.workspace.getConfiguration('xtc'); + const original = settings.inspect('java.vmOptions')?.globalValue; + const document = await workspace.open(data.file, data.source); + await noErrors(document.uri); + const previous = await client().sendRequest('xtc/languageServiceStatus'); + const ui = await WorkbenchUi.connect(); + const unhandled: unknown[] = []; + const rejected = (error: unknown) => { unhandled.push(error); }; + process.on('unhandledRejection', rejected); + try { + if (id === 'X271') await stopLanguageClient(); + else { + await settings.update('java.vmOptions', data.startupFailureOptions, vscode.ConfigurationTarget.Global); + await assert.rejects(async () => vscode.commands.executeCommand('xtc.restartServer'), + /initialization|connection|start/i); + await ui.page.locator('.notification-list-item').filter({ hasText: 'Ecstasy Language Server could not start' }) + .waitFor({ state: 'visible', timeout: 5_000 }); + const failures = (await ui.page.locator('.notification-list-item').allTextContents()) + .filter(text => /could not start|Server initialization failed|Pending response rejected|Connection to server got closed/.test(text)); + assert.strictEqual(failures.length, 1, 'One actionable notification per failed launch'); + } + const destination = vscode.Uri.file(path.join(workspace.directory, 'offline.zip')); + await vscode.commands.executeCommand('xtc.exportServerLogs', destination); + const entries = await readArchive(await fs.readFile(destination.fsPath)); + const manifest = JSON.parse(entries.get('manifest.json')!.toString()); + assert.strictEqual(manifest.offline, true); + assert.ok(entries.has('launcher.log')); + if (id === 'X271') { + assert.strictEqual(manifest.logs.directory, previous.logs.directory); + assert.ok([...entries.keys()].some(name => name.endsWith('server.log'))); + } else { + assert.ok(entries.get('launcher.log')!.includes(data.startupFailureOptions[0])); + assert.strictEqual(manifest.logs, undefined, 'Failed launch must not reuse an earlier successful session'); + } + } finally { + try { + await settings.update('java.vmOptions', original, vscode.ConfigurationTarget.Global); + await vscode.commands.executeCommand('xtc.restartServer'); + await noErrors(document.uri); + assert.strictEqual(document.getText(), data.source); + await vscode.commands.executeCommand('notifications.clearAll'); + } catch (error) { throw new PlaybookCleanupFailure([error]); } + finally { + process.off('unhandledRejection', rejected); + await ui.close(); + } + } + assert.deepStrictEqual(unhandled, [], 'Startup failures must reject the command without orphaned promise rejections'); + }); + +} diff --git a/lang/vscode-extension/src/test/playbook/semantics.ts b/lang/vscode-extension/src/test/playbook/semantics.ts new file mode 100644 index 00000000000..524f81e92ce --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/semantics.ts @@ -0,0 +1,221 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { CallHierarchyIncomingCall, CallHierarchyItem, CallHierarchyOutgoingCall, SemanticTokens, SemanticTokensOptions } from 'vscode-languageclient/node'; +import { WorkbenchUi } from '../workbenchUi'; +import { scenarioRegex } from './shared'; +import { client, fixture, noErrors, playbook, position, symbols, targetNames, targets } from './support'; + +async function calls(document: vscode.TextDocument, at: vscode.Position): Promise { + return await client().sendRequest('textDocument/prepareCallHierarchy', { + textDocument: { uri: document.uri.toString() }, position: at + }) ?? []; +} + +async function incoming(item: CallHierarchyItem): Promise { + assert.ok(item, 'Prepared call hierarchy item'); + return await client().sendRequest('callHierarchy/incomingCalls', { item }) ?? []; +} + +async function outgoing(item: CallHierarchyItem): Promise { + assert.ok(item, 'Prepared call hierarchy item'); + return await client().sendRequest('callHierarchy/outgoingCalls', { item }) ?? []; +} + +export function semanticCases(): void { + playbook('X33', async (workspace, data) => { + const document = await workspace.open(data.file); + await noErrors(document.uri); + for (const { anchor, expected } of data.variants) { + assert.deepStrictEqual(await targetNames(await targets(document, 'TypeDefinition', position(document, anchor))), [expected]); + } + }); + + playbook('X34', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.deepStrictEqual((await targetNames(await targets(document, 'TypeDefinition', position(document, data.narrowedUse, data.narrowedOffset, true)))), data.narrowedTargets); + assert.deepStrictEqual((await targetNames(await targets(document, 'TypeDefinition', position(document, data.unionUse, data.unionOffset)))).sort(), data.unionTargets); + }); + + playbook('X35', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.deepStrictEqual(await targetNames(await targets(document, 'TypeDefinition', position(document, data.formalUse, data.offset))), data.formalTargets); + assert.deepStrictEqual(await targetNames(await targets(document, 'TypeDefinition', position(document, data.binaryUse))), data.binaryTargets); + }); + + playbook('X36', async (workspace, data) => { + const document = await workspace.open(data.file); + assert.deepStrictEqual((await targetNames(await targets(document, 'Implementation', position(document, data.anchor)))).sort(), + data.implementations); + }); + + playbook('X37', async (workspace, data) => { + const document = await workspace.open(data.file); + const overrideLines = [...document.getText().matchAll(scenarioRegex(data.overrides))].map(match => document.positionAt(match.index! + data.declarationPrefix.length).line); + assert.strictEqual(overrideLines.length, data.declarationOffset); + for (const at of [position(document, data.declaration, data.declarationOffset), position(document, data.use, data.useOffset)]) { + const result = await targets(document, 'Implementation', at); + assert.deepStrictEqual(result.map(item => item.range.start.line).sort((a, b) => a - b), overrideLines); + } + const child = await targets(document, 'Implementation', new vscode.Position(overrideLines[1], document.lineAt(overrideLines[1]).text.indexOf(data.methodName))); + assert.strictEqual(child.length, data.targetCount); + assert.strictEqual(child[0].range.start.line, overrideLines[1]); + }); + + playbook('X38', async (workspace, data) => { + const root = await workspace.project(); + await workspace.replace(root, fixture(data.rootFile).replace(data.replaceFrom, data.rootWithFactory)); + const lookup = () => targets(root, 'TypeDefinition', position(root, data.factory)); + const initial = await lookup(); + assert.strictEqual(initial.length, data.targetCount); + assert.strictEqual(initial[0].uri.toString(), workspace.uri(data.memberFile).toString()); + const implementations = await targets(root, 'Implementation', position(root, data.method)); + assert.strictEqual(implementations.length, data.targetCount); + assert.strictEqual(implementations[0].uri.toString(), root.uri.toString()); + const child = await workspace.open(data.memberFile); + await workspace.replace(child, '\n\n' + fixture(data.memberFile)); + assert.strictEqual((await lookup())[0].range.start.line, initial[0].range.start.line + data.lineShift); + await workspace.replace(child, data.replaceWith); + assert.deepStrictEqual(await lookup(), []); + await workspace.replace(child, fixture(data.memberFile)); + assert.strictEqual((await lookup())[0].range.start.line, initial[0].range.start.line); + }); + + playbook('X39', async (workspace, data) => { + const document = await workspace.open(data.file); + const [leaf] = await calls(document, position(document, data.callee, data.offset)); + const result = await incoming(leaf); + assert.strictEqual(result.length, data.edgeCount); + assert.strictEqual(result.find(item => item.from.name.includes(data.callerName))?.fromRanges.length, data.edgeCount); + assert.strictEqual(result.find(item => item.from.name.includes(data.lambdaName))?.fromRanges.length, data.lambdaSites); + const [run] = await calls(document, position(document, data.caller, data.offset)); + const edges = await outgoing(run); + assert.strictEqual(edges.length, data.edgeCount); + assert.strictEqual(new Set(edges.map(item => item.to.selectionRange.start.line)).size, data.edgeCount); + assert.deepStrictEqual(edges.map(item => item.fromRanges.length).sort(), (data.sitesPerCallee as (number)[])); + }); + + playbook('X40', async (workspace, data) => { + const document = await workspace.open(data.file); + const [leaf] = await calls(document, position(document, data.callee, data.offset)); + const lambda = (await incoming(leaf)).find(item => item.from.name.includes(data.lambdaName)); + assert.ok(lambda); + const edges = await outgoing(lambda.from); + assert.strictEqual(edges.length, data.edgeCount); + assert.deepStrictEqual(edges[0].to.selectionRange, leaf.selectionRange); + assert.strictEqual(edges[0].fromRanges[0].start.line, position(document, data.lambdaCall).line); + assert.deepStrictEqual(await calls(document, position(document, data.dynamicCall)), []); + }); + + semanticAccessCases(['X41']); + + playbook('X42', async (workspace, data) => { + const document = await workspace.open(data.file); + async function verify(expected: typeof data.inferred): Promise { + await noErrors(document.uri); + const range = new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)); + const hints = await vscode.commands.executeCommand('vscode.executeInlayHintProvider', document.uri, range); + assert.ok(hints); + const labels = hints.map(hint => typeof hint.label === 'string' ? hint.label : hint.label.map(part => part.value).join('')); + for (const label of expected.hints) { assert.ok(labels.some(actual => actual.includes(label)), JSON.stringify(hints)); } + assert.ok(!hints.some(hint => hint.position.line === position(document, expected.anchor).line)); + assert.strictEqual(hints.filter(hint => hint.kind === vscode.InlayHintKind.Type).length, expected.typeHintCount); + assert.ok(!labels.some(label => label.includes(expected.excludedHint))); + } + await verify({ ...data, source: document.getText() }); + await workspace.replace(document, data.inferred.source); + await verify(data.inferred); + }); + + playbook('X43', async (workspace, data) => { + const document = await workspace.open(data.file); + const [old] = await calls(document, position(document, data.anchor, data.offset)); + await workspace.replace(document, fixture(data.file).replace(data.replaceFrom, data.shiftedDeclaration)); + assert.deepStrictEqual(await outgoing(old), []); + const [fresh] = await calls(document, position(document, data.anchor, data.offset)); + assert.strictEqual(fresh.selectionRange.start.line, old.selectionRange.start.line + data.lineShift); + assert.strictEqual((await outgoing(fresh)).length, data.edgeCount); + await workspace.replace(document, document.getText().replace(data.anchor, data.replaceWith)); + assert.deepStrictEqual(await outgoing(fresh), []); + await workspace.replace(document, fixture(data.file)); + assert.strictEqual((await outgoing((await calls(document, position(document, data.anchor, data.offset)))[0])).length, data.edgeCount); + await workspace.discard(document); + const reopened = await workspace.open(data.file); + assert.strictEqual((await outgoing((await calls(reopened, position(reopened, data.anchor, data.offset)))[0])).length, data.edgeCount); + }); + + playbook('X44', async (workspace, data) => { + const member = fixture(data.memberFile).replace(data.memberDeclaration, data.memberWithCall); + await workspace.write(data.memberFile, member); + const root = await workspace.open(data.rootFile, fixture(data.rootFile).replace(data.replaceFrom, data.replaceWith)); + await noErrors(root.uri); + const [target] = await calls(root, position(root, data.anchor, data.offset)); + const [edge] = await incoming(target); + assert.ok(edge); + assert.strictEqual(vscode.Uri.parse(edge.from.uri).fsPath, workspace.uri(data.memberFile).fsPath); + const child = await workspace.open(data.memberFile); + await workspace.replace(child, '\n\n' + member); + await symbols(root); + assert.deepStrictEqual(await incoming(target), []); + const [current] = await calls(root, position(root, data.anchor, data.offset)); + const [moved] = await incoming(current); + assert.strictEqual(moved.fromRanges[0].start.line, edge.fromRanges[0].start.line + data.lineShift); + }); +} + +export function semanticClosureCases(): void { + playbook('X153', async (workspace, data) => { + await workspace.write(data.file, data.variants[0].source); + const document = await workspace.open(data.file); + for (const variant of data.variants) { + await workspace.replace(document, variant.source); + await noErrors(document.uri); + const actual = await targets(document, 'Implementation', position(document, variant.anchor, variant.offset)); + assert.deepStrictEqual(actual.map(item => item.range.start).sort((a, b) => a.compareTo(b)), + variant.targets.map(anchor => position(document, anchor)).sort((a, b) => a.compareTo(b))); + assert.ok(actual.every(item => item.uri.toString() === document.uri.toString())); + } + }); + semanticAccessCases(['X154']); +} + +function semanticAccessCases(ids: readonly ('X41' | 'X154')[]): void { + for (const id of ids) { + playbook(id, async (workspace, data) => { + if ('source' in data) await workspace.write(data.file, data.source); + const document = await workspace.open(data.file); + const legend = (client().initializeResult!.capabilities.semanticTokensProvider as SemanticTokensOptions).legend; + const result = await client().sendRequest('textDocument/semanticTokens/full', { textDocument: { uri: document.uri.toString() } }); + let line = 0; + let character = 0; + const tokens: { line: number; character: number; text: string; type: string; modifiers: string[] }[] = []; + for (let offset = 0; offset < result.data.length; offset += 5) { + const [deltaLine, deltaCharacter, length, type, mask] = result.data.slice(offset, offset + 5); + line += deltaLine; + character = deltaLine ? deltaCharacter : character + deltaCharacter; + tokens.push({ line, character, text: document.getText(new vscode.Range(line, character, line, character + length)), + type: legend.tokenTypes[type], modifiers: legend.tokenModifiers.filter((_, bit) => mask & (1 << bit)) }); + } + for (const { name, type } of data.tokenKinds) { + assert.ok(tokens.some(token => token.text === name && token.type === type), JSON.stringify(tokens)); + } + assert.ok(tokens.some(token => token.text === data.methodName && token.modifiers.includes(data.staticModifier) && token.modifiers.includes(data.declarationModifier))); + if ('accesses' in data) for (const access of data.accesses) { + const at = position(document, access.anchor, access.offset); + const token = tokens.find(item => item.line === at.line && item.character === at.character); + assert.ok(token, access.anchor); + assert.strictEqual(token.modifiers.includes('modification'), access.write, access.anchor); + } + const write = position(document, data.anchor2); + assert.ok(tokens.some(token => token.line === write.line && token.text === data.variableName && token.modifiers.includes(data.writeModifier))); + const highlights = await vscode.commands.executeCommand('vscode.executeDocumentHighlights', document.uri, write); + assert.ok(highlights?.some(item => item.range.contains(write) && item.kind === vscode.DocumentHighlightKind.Write)); + const read = position(document, data.anchor, data.offset); + assert.ok(highlights?.some(item => item.range.contains(read) && item.kind === vscode.DocumentHighlightKind.Read)); + if (id === 'X154') { + const ui = await WorkbenchUi.connect(); + try { await ui.screenshot(`${id}-current-theme`); } finally { await ui.close(); } + } + }); + + } +} diff --git a/lang/vscode-extension/src/test/playbook/shared.ts b/lang/vscode-extension/src/test/playbook/shared.ts new file mode 100644 index 00000000000..8304d41034c --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/shared.ts @@ -0,0 +1,102 @@ +import * as assert from 'node:assert'; +import { createHash } from 'node:crypto'; +import { readFileSync } from 'node:fs'; +import * as path from 'node:path'; + +// Type-only import: the canonical JSON stays outside the extension package and is loaded below. +import type scenarioFile from '../../../../test-fixtures/compiler-playbook/scenarios.json'; + +type Catalog = typeof scenarioFile; +export type ScenarioId = keyof Catalog['cases']; +export type ScenarioValues = Catalog['cases'][K]['values']; +export const sharedScenarioPath = path.resolve(__dirname, '../../../../test-fixtures/compiler-playbook/scenarios.json'); +const contents = readFileSync(sharedScenarioPath, 'utf8'); +export const sharedScenarioHash = createHash('sha256').update(contents).digest('hex'); +export const catalog: Catalog = JSON.parse(contents); +assert.strictEqual(catalog.schemaVersion, 2, 'Unsupported shared playbook schema'); +export const sharedScenarioIds = Object.keys(catalog.cases) as ScenarioId[]; + +/** Exact IDs only: a typo or empty selection must fail before opening the editor. */ +export function selectedScenarioIds(selection: string | undefined): ScenarioId[] { + if (selection === undefined) return sharedScenarioIds; + const requested = selection.split(',').map(id => id.trim()); + assert.ok(requested.every(id => sharedScenarioIds.includes(id as ScenarioId)), `Unknown or empty playbook case in: ${selection}`); + assert.strictEqual(new Set(requested).size, requested.length, 'Duplicate playbook case IDs'); + return sharedScenarioIds.filter(id => requested.includes(id)); +} + +const expectedIds = [...Array.from({ length: 279 }, (_, index) => `X${index + 1}`), 'CFG1', 'CFG2', 'CFG3', '7a.8', '7a.9']; +assert.deepStrictEqual(sharedScenarioIds, expectedIds, 'The catalog must describe the complete playbook in order'); +for (const [id, scenario] of Object.entries(catalog.cases)) { + assert.ok(scenario.title && scenario.values && Array.isArray(scenario.manual), `Invalid scenario ${id}`); + assert.ok(['full', 'partial', 'not-implemented'].includes(scenario.intellij.coverage), `Missing IntelliJ coverage for ${id}`); + assert.ok(scenario.intellij.coverage === 'full' || scenario.intellij.limitations.length > 0, `Explain the IntelliJ gap for ${id}`); +} + +export function scenarioRegex(pattern: { source: string; flags: string }): RegExp { + return new RegExp(pattern.source, pattern.flags); +} + +/** ${0}, ${1}, ... insert literal data; catalog contents are never evaluated as code. */ +export function scenarioText(template: string, ...values: (string | number | undefined)[]): string { + const used = new Set(); + const result = template.replace(/\$\{(\d+)\}/g, (_, index) => { + const position = Number(index); + assert.ok(position < values.length, `Missing substitution ${position} in ${template}`); + used.add(position); + return String(values[position]); + }); + assert.strictEqual(used.size, values.length, `Unused substitutions in ${template}`); + return result; +} + +export const shared = { + sourceModules: catalog.common.sourceModules, + diagnostics: { id: 'X2', ...catalog.cases.X2.values }, + definitions: { id: 'X4', ...catalog.cases.X4.values }, + completion: { id: 'X7', ...catalog.cases.X7.values }, + dependencyNavigation: { id: 'X45', ...catalog.cases.X45.values }, + dependencyEdit: { id: 'X46', ...catalog.cases.X46.values }, + configuration: { id: 'CFG1', ...catalog.cases.CFG1.values }, + warning: { id: '7a.8', ...catalog.cases['7a.8'].values } +}; + +/** An edit/anchor must be unique: silently editing another example invalidates the test. */ +function unique(text: string, anchor: string): number { + assert.ok(anchor.length > 0, 'Empty shared playbook anchor'); + const offset = text.indexOf(anchor); + assert.ok(offset >= 0 && text.indexOf(anchor, offset + 1) < 0, `Expected one shared playbook anchor: ${JSON.stringify(anchor)}`); + return offset; +} + +export function editScenario(text: string, edit: { from: string; to: string }): string { + const offset = unique(text, edit.from); + return text.slice(0, offset) + edit.to + text.slice(offset + edit.from.length); +} + +export function scenarioOffset(text: string, marked: string): number { + const marker = unique(marked, '§'); + return unique(text, marked.replace('§', '')) + marker; +} + +export function validateSharedFixtures(fixture: (file: string) => string): void { + for (const error of shared.diagnostics.errors) { + const text = editScenario(fixture(shared.diagnostics.file), error.edit); + assert.ok(scenarioOffset(text, error.rangeStart) <= scenarioOffset(text, error.rangeEnd)); + } + for (const location of shared.definitions.locations) { + scenarioOffset(fixture(shared.definitions.file), location.cursor); + scenarioOffset(fixture(location.targetFile), location.target); + } + const incomplete = editScenario(fixture(shared.completion.file), shared.completion.edit); + scenarioOffset(incomplete, shared.completion.cursor); + editScenario(incomplete, shared.completion.accepted); + scenarioOffset(fixture(shared.dependencyNavigation.file), shared.dependencyNavigation.location.cursor); + scenarioOffset(fixture(shared.dependencyNavigation.location.targetFile), shared.dependencyNavigation.location.target); + editScenario(fixture(shared.dependencyEdit.file), shared.dependencyEdit.edit); + fixture(shared.dependencyEdit.consumer); + fixture(shared.configuration.consumer); + scenarioOffset(fixture(shared.warning.file), shared.warning.declaration); + editScenario(fixture(shared.warning.file), shared.warning.edit); + for (const module of shared.sourceModules) { fixture(module.uri); } +} diff --git a/lang/vscode-extension/src/test/playbook/support.ts b/lang/vscode-extension/src/test/playbook/support.ts new file mode 100644 index 00000000000..144c96afd09 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/support.ts @@ -0,0 +1,274 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { getClient } from '../../lsp-client'; +import { withTestWait } from '../progress'; +import { mustStopPlaybook, PlaybookCleanupFailure, waitFor } from '../wait'; +import { catalog, ScenarioId, ScenarioValues, shared, sharedScenarioIds, validateSharedFixtures } from './shared'; + +export const manualPath = path.resolve(__dirname, '../../../../doc/manual-test-plan.md'); +export const cases = new Map(); +let fixtures: Map; + +export function client() { + const result = getClient(); + assert.ok(result?.initializeResult && result.isRunning(), 'Compiler language client must be running'); + return result; +} + +export async function eventually(read: () => Promise, accept: (value: T) => boolean, message: string): Promise { + return withTestWait(message, () => waitFor(read, accept, message)); +} + +export async function loadFixtures(): Promise { + const manual = await fs.readFile(manualPath, 'utf8'); + const blocks = [...manual.matchAll(/```xtc\n([\s\S]*?)\n```/g)].map(match => match[1]); + fixtures = new Map(); + for (const { file, pattern } of catalog.common.fixtures) { + const matches = blocks.filter(text => new RegExp(pattern, 'm').test(text)); + assert.strictEqual(matches.length, 1, `One canonical ${file} fixture in playbook`); + fixtures.set(file, matches[0] + '\n'); + } + validateSharedFixtures(fixture); + assert.deepStrictEqual([...cases.keys()], sharedScenarioIds, 'Every shared case must have exactly one VS Code implementation'); + const ids = [...manual.matchAll(/^\| (X\d+) \|/gm)].map(match => match[1]); + assert.deepStrictEqual([...cases.keys()].filter(id => /^X\d+$/.test(id)), ids, + 'Every XdkAdapter playbook row must have exactly one registered case, in playbook order'); +} + +export function fixture(file: string): string { + const text = fixtures.get(file); + assert.ok(text, `Missing fixture ${file}`); + return text; +} + +export function position(document: vscode.TextDocument, text: string, offset = 0, last = false): vscode.Position { + const index = last ? document.getText().lastIndexOf(text) : document.getText().indexOf(text); + assert.ok(index >= 0, `Missing anchor ${JSON.stringify(text)} in ${document.uri.fsPath}`); + return document.positionAt(index + offset); +} + +export function label(item: vscode.CompletionItem): string { + return typeof item.label === 'string' ? item.label : item.label.label; +} + +export function symbolNames(symbols: vscode.DocumentSymbol[]): string[] { + return symbols.flatMap(symbol => [symbol.name, ...symbolNames(symbol.children ?? [])]); +} + +export async function symbols(document: vscode.TextDocument): Promise { + // This also acts as an analysis barrier after sibling edits or explicit close/reopen. + // The execute command caches results at an unchanged editor version, so ask the + // registered provider directly when the module snapshot may have changed. + const provider = client().getFeature('textDocument/documentSymbol').getProvider(document); + assert.ok(provider, 'Registered Ecstasy document-symbol provider'); + const token = new vscode.CancellationTokenSource(); + try { return await provider.provideDocumentSymbols(document, token.token) as vscode.DocumentSymbol[] ?? []; } + finally { token.dispose(); } +} + +export async function hover(document: vscode.TextDocument, at: vscode.Position): Promise { + const result = await vscode.commands.executeCommand('vscode.executeHoverProvider', document.uri, at); + return (result ?? []).flatMap(item => item.contents.map(content => typeof content === 'string' ? content : content.value)).join('\n'); +} + +export async function targets(document: vscode.TextDocument, kind: 'Definition' | 'Declaration' | 'TypeDefinition' | 'Implementation', at: vscode.Position) { + const result = await vscode.commands.executeCommand<(vscode.Location | vscode.LocationLink)[]>(`vscode.execute${kind}Provider`, document.uri, at); + return (result ?? []).map(item => 'targetUri' in item + ? new vscode.Location(item.targetUri, item.targetSelectionRange ?? item.targetRange) + : item); +} + +export async function targetNames(locations: vscode.Location[]): Promise { + return Promise.all(locations.map(async location => (await vscode.workspace.openTextDocument(location.uri)).getText(location.range))); +} + +export async function noErrors(uri: vscode.Uri): Promise { + await diagnostics(uri, values => values.length === 0, 'Expected no diagnostics'); +} + +export async function diagnostics(uri: vscode.Uri, accept: (values: vscode.Diagnostic[]) => boolean, message: string) { + return eventually(async () => vscode.languages.getDiagnostics(uri), accept, `${message} in ${uri.fsPath}`); +} + +export function diagnosticCode(item: vscode.Diagnostic): string { + return String(typeof item.code === 'object' ? item.code.value : item.code); +} + +export async function nextProblem(document: vscode.TextDocument, values: vscode.Diagnostic[]): Promise { + await vscode.commands.executeCommand('workbench.actions.view.problems'); + const editor = await vscode.window.showTextDocument(document, { preview: false }); + editor.selection = new vscode.Selection(0, 0, 0, 0); + await vscode.commands.executeCommand('editor.action.marker.nextInFiles'); + await eventually(async () => vscode.window.activeTextEditor, + current => current?.document.uri.toString() === document.uri.toString() && values.some(item => item.range.contains(current.selection.start)), + 'Next problem selects the diagnostic source location'); + await vscode.commands.executeCommand('closeMarkersNavigation'); +} + +export class Workspace { + private readonly detached = new Set(); + + constructor(readonly directory: string) {} + + uri(file: string): vscode.Uri { return vscode.Uri.file(path.join(this.directory, file)); } + + async write(file: string, text = fixture(file)): Promise { + const uri = this.uri(file); + await fs.mkdir(path.dirname(uri.fsPath), { recursive: true }); + await fs.writeFile(uri.fsPath, text); + } + + async open(file: string, text?: string): Promise { + if (text !== undefined) { await this.write(file, text); } + else if (!await fs.stat(this.uri(file).fsPath).catch(() => undefined)) { await this.write(file); } + let document = await vscode.workspace.openTextDocument(this.uri(file)); + if (document.languageId !== 'xtc') { document = await vscode.languages.setTextDocumentLanguage(document, 'xtc'); } + await vscode.window.showTextDocument(document, { preview: false }); + if (this.detached.delete(document.uri.toString())) { + const provider = client().getFeature('textDocument/didOpen').getProvider(document); + assert.ok(provider, 'Registered document-open synchronization'); + await provider.send(document); + } + await eventually(() => symbols(document), values => values.length > 0, 'Initial document structure'); + return document; + } + + async replace(document: vscode.TextDocument, text: string, settle = true): Promise { + const edit = new vscode.WorkspaceEdit(); + edit.replace(document.uri, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), text); + assert.ok(await vscode.workspace.applyEdit(edit)); + if (settle) { await symbols(document); } + } + + async marked(file: string, text: string): Promise<{ document: vscode.TextDocument; at: vscode.Position }> { + const index = text.indexOf('§'); + assert.ok(index >= 0, 'Missing cursor marker'); + const document = await this.open(file); + await this.replace(document, text.replace('§', '')); + return { document, at: document.positionAt(index) }; + } + + async editing(body: string, inspect = false, transform: (text: string) => string = text => text) { + const setup = catalog.common.editing; + const anchor = inspect ? setup.inspect : setup.run; + const text = transform(fixture(setup.file)).replace(anchor, anchor.replace('{}', `{ ${body} }`)); + return this.marked(setup.file, text); + } + + async completion(document: vscode.TextDocument, at: vscode.Position): Promise { + const result = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', document.uri, at); + return result?.items ?? []; + } + + async accept(document: vscode.TextDocument, item: vscode.CompletionItem): Promise { + assert.ok(item.range, `Completion ${label(item)} must provide its source replacement range`); + const range = item.range instanceof vscode.Range ? item.range : item.range.replacing; + const value = item.insertText ?? label(item); + if (value instanceof vscode.SnippetString) { + assert.strictEqual(item.additionalTextEdits?.length ?? 0, 0, 'Snippet fixtures must not discard additional edits'); + await vscode.window.showTextDocument(document); + // insertSnippet always adjusts indentation. The runner's snippet-edit API can retain + // the provider's keepWhitespace choice, as native completion acceptance does. + const snippet = Object.assign(vscode.SnippetTextEdit.replace(range, value), { keepWhitespace: item.keepWhitespace }); + const edit = new vscode.WorkspaceEdit(); + edit.set(document.uri, [snippet]); + assert.ok(await vscode.workspace.applyEdit(edit)); + await symbols(document); + return; + } + assert.strictEqual(typeof value, 'string', 'These fixtures expect plain completion text'); + const edit = new vscode.WorkspaceEdit(); + edit.replace(document.uri, range, value as string); + for (const additional of item.additionalTextEdits ?? []) { + edit.replace(document.uri, additional.range, additional.newText); + } + assert.ok(await vscode.workspace.applyEdit(edit)); + await symbols(document); + } + + async signature(document: vscode.TextDocument, at: vscode.Position): Promise { + return vscode.commands.executeCommand('vscode.executeSignatureHelpProvider', document.uri, at); + } + + async project(): Promise { + await this.write(catalog.common.project.member); + const document = await this.open(catalog.common.project.root); + await noErrors(document.uri); + return document; + } + + async configure(modules: { name: string; uri: string; dependencies?: string[]; resourceRoots?: string[] }[]): Promise { + await vscode.workspace.getConfiguration('xtc.compiler').update('sourceModules', modules, vscode.ConfigurationTarget.Workspace); + } + + async dependencies(): Promise { + for (const module of shared.sourceModules) { await this.write(module.uri); } + await this.configureSharedGraph(); + const document = await this.open(shared.dependencyNavigation.file); + await noErrors(document.uri); + return document; + } + + async configureSharedGraph(): Promise { + await this.configure(shared.sourceModules.map(module => ({ ...module, uri: this.uri(module.uri).toString() }))); + } + + async discard(document: vscode.TextDocument): Promise { + await vscode.window.showTextDocument(document, { preview: false }); + await vscode.commands.executeCommand('workbench.action.revertAndCloseActiveEditor'); + // Closing a tab can retain a hidden model. Use the client's public synchronization + // provider so the server and client's synced-document map both observe didClose. + if (!document.isClosed && !this.detached.has(document.uri.toString())) { + const provider = client().getFeature('textDocument/didClose').getProvider(document); + assert.ok(provider, 'Registered document-close synchronization'); + await provider.send(document); + this.detached.add(document.uri.toString()); + } + await client().sendRequest('xtc/healthCheck'); + } + + async dispose(): Promise { + // Every buffer and setting belongs to this test's isolated VS Code workspace. + await eventually(async () => getClient()?.isRunning(), running => running === true, 'Client ready for fixture cleanup'); + for (const document of vscode.workspace.textDocuments.filter(doc => doc.uri.fsPath.startsWith(this.directory + path.sep) && doc.languageId === 'xtc')) { + await this.discard(document); + } + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); + await this.configure([]); + } +} + +export function playbook(id: K, body: (workspace: Workspace, data: ScenarioValues) => Promise): void { + const scenario = catalog.cases[id]; + assert.ok(scenario, `Case ${id} is missing from the shared catalog`); + const { title, manual } = scenario; + assert.ok(!cases.has(id), `Duplicate playbook case ${id}`); + cases.set(id, { title, manual }); + test(`${id}: ${title}`, async function () { + this.timeout(90_000); + const root = vscode.workspace.workspaceFolders?.[0].uri.fsPath; + assert.ok(root); + const workspace = new Workspace(path.join(root, id)); + try { await body(workspace, scenario.values); } + catch (error) { + // A timed-out command can still be mutating the disposable editor. The runner aborts; + // do not start competing cleanup actions or proceed to another case in that window. + if (mustStopPlaybook(error)) throw error; + const report = process.env.XTC_PLAYBOOK_REPORT_DIR; + if (report) { + for (const document of vscode.workspace.textDocuments.filter(doc => doc.uri.fsPath.startsWith(workspace.directory + path.sep))) { + const file = path.join(report, 'failure-sources', id, path.relative(workspace.directory, document.uri.fsPath)); + await fs.mkdir(path.dirname(file), { recursive: true }); + await fs.writeFile(file, document.getText()); + } + } + try { await workspace.dispose(); } + catch (cleanupError) { throw new PlaybookCleanupFailure([error, cleanupError]); } + throw error; + } + try { await workspace.dispose(); } + catch (error) { throw new PlaybookCleanupFailure([error]); } + }); +} diff --git a/lang/vscode-extension/src/test/playbook/syntaxCompletion.ts b/lang/vscode-extension/src/test/playbook/syntaxCompletion.ts new file mode 100644 index 00000000000..39a89e93671 --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/syntaxCompletion.ts @@ -0,0 +1,48 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { focusTestWindow } from '../native-focus'; +import { discovered } from './liveWorkspace'; +import { eventually, label, noErrors, playbook } from './support'; + +export function syntaxCompletionCases(): void { + for (const id of ['X149', 'X150', 'X151', 'X152'] as const) { + playbook(id, async (workspace, data) => { + await workspace.write(data.file, data.variants[0].source); + await discovered(workspace, async () => { + const document = await workspace.open(data.file); + for (const variant of data.variants) { + await workspace.replace(document, variant.source); + const at = document.positionAt(variant.source.indexOf(variant.anchor) + + ('prefix' in variant ? (variant.prefix?.length ?? variant.anchor.length) : variant.anchor.length)); + const item = await eventually(async () => (await workspace.completion(document, at)) + .find(item => label(item) === variant.label), item => !!item, variant.label); + assert.ok(item); + assert.strictEqual(item.insertText instanceof vscode.SnippetString, id === 'X150'); + await workspace.accept(document, item); + assert.strictEqual(document.getText(), variant.expected); + if ('selected' in variant) { + const editor = vscode.window.activeTextEditor!; + assert.strictEqual(document.getText(editor.selection), variant.selected); + await vscode.commands.executeCommand('jumpToNextSnippetPlaceholder'); + assert.ok(editor.selection.isEmpty, 'Final snippet tab stop leaves no selected placeholder'); + assert.strictEqual(document.getText(), variant.expected); + const finalOffset = 'finalAnchor' in variant && variant.finalAnchor !== undefined + ? variant.expected.indexOf(variant.finalAnchor) + variant.finalAnchor.length + : (() => { + const body = variant.expected.split('\n').find(line => line.length > 0 && !line.trim())!; + return variant.expected.indexOf(`\n${body}\n`) + 1 + body.length; + })(); + assert.strictEqual(document.offsetAt(editor.selection.active), finalOffset); + } + await noErrors(document.uri); + await focusTestWindow(); + await vscode.window.showTextDocument(document, { preview: false, preserveFocus: false }); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + assert.strictEqual(vscode.window.activeTextEditor?.document, document, 'Undo targets the completed document'); + await vscode.commands.executeCommand('undo'); + await eventually(async () => document.getText(), text => text === variant.source, 'One undo restores the completion prefix'); + } + }); + }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/typeMoves.ts b/lang/vscode-extension/src/test/playbook/typeMoves.ts new file mode 100644 index 00000000000..21db04a39bf --- /dev/null +++ b/lang/vscode-extension/src/test/playbook/typeMoves.ts @@ -0,0 +1,77 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as vscode from 'vscode'; +import { compilerSettingsLocation, compilerSourceModules } from '../../rename-proposal'; +import { SourceModule, sourceGraphKey } from '../../source-graph-configuration'; +import { focusTestWindow } from '../native-focus'; +import { client, eventually, noErrors, playbook, symbols } from './support'; + +export function typeMoveCases(ids: readonly ('X161' | 'X162' | 'X163' | 'X169' | 'X170' | 'X171' | 'X172' | 'X173' | 'X174' | 'X175' | 'X176' | 'X216' | 'X217' | 'X218' | 'X219')[] = ['X161', 'X162', 'X163']): void { + ids.forEach(id => playbook(id, async (workspace, data) => { + for (const file of data.files) await workspace.write(file.file, file.source); + if ('directories' in data) { + for (const directory of data.directories) await vscode.workspace.fs.createDirectory(workspace.uri(directory)); + } + const graph = (modules: SourceModule[]) => modules.map(module => ({ + ...module, uri: workspace.uri(module.uri).toString(), + resourceRoots: module.resourceRoots?.map(root => workspace.uri(root).toString()) + })); + await workspace.configure(graph(data.modules)); + const document = await workspace.open(data.root); + await symbols(document); + await noErrors(document.uri); + if ('consumer' in data) { + assert.ok(!vscode.window.tabGroups.all.flatMap(group => group.tabs).some(tab => + tab.input instanceof vscode.TabInputText && tab.input.uri.toString() === workspace.uri(data.consumer).toString()), 'Consumer stays closed before Move'); + } + const moves = 'moves' in data ? data.moves : [{ from: data.from, to: data.to }]; + if ('refused' in data && data.refused) { + const beforeGraph = sourceGraphKey(compilerSourceModules()); + const proposal = await client().sendRequest('xtc/renameFiles', { files: moves.map(move => ({ + oldUri: workspace.uri(move.from).toString(), newUri: workspace.uri(move.to).toString() + })) }); + assert.strictEqual(proposal, null, 'A colliding member rejects the complete proposal'); + for (const file of data.files) assert.strictEqual(await fs.readFile(workspace.uri(file.file).fsPath, 'utf8'), file.source); + for (const move of moves) assert.strictEqual(await fs.stat(workspace.uri(move.to).fsPath).catch(() => null), null); + assert.strictEqual(sourceGraphKey(compilerSourceModules()), beforeGraph); + await noErrors(document.uri); + return; + } + const move = new vscode.WorkspaceEdit(); + for (const item of moves) move.renameFile(workspace.uri(item.from), workspace.uri(item.to), { overwrite: false }); + assert.ok(await vscode.workspace.applyEdit(move, { isRefactoring: true })); + + const verify = async (moved: boolean) => { + await eventually(async () => Promise.all(data.files.map(async file => { + const uri = workspace.uri(moved ? file.destination : file.file); + const buffer = vscode.workspace.textDocuments.find(value => !value.isClosed && value.uri.toString() === uri.toString()); + return buffer?.getText() ?? fs.readFile(uri.fsPath, 'utf8').catch(() => ''); + })), texts => texts.every((text, index) => text === (moved ? data.files[index].expected : data.files[index].source)), + `Move ${moved ? 'applied' : 'undone'}: all sources and resources agree`); + for (const file of data.files.filter(value => value.destination !== value.file)) { + assert.strictEqual(await fs.stat(workspace.uri(moved ? file.file : file.destination).fsPath).catch(() => null), null); + } + if ('afterModules' in data) { + const expected = sourceGraphKey(graph(moved ? data.afterModules : data.modules)); + await eventually(async () => sourceGraphKey(compilerSourceModules()), value => value === expected, 'Persisted graph follows Move/Undo/Redo'); + await noErrors((await workspace.open(moved ? data.afterRoot : data.root)).uri); + await noErrors((await workspace.open(data.consumer)).uri); + } else { + await symbols(document); + await noErrors(document.uri); + } + }; + await verify(true); + for (const [action, moved] of [['undo', false], ['redo', true]] as const) { + // Graph moves also edit settings. Use the settings editor + // as the undo context so the native transaction includes its file operations as well. + if ('afterModules' in data) { + await vscode.window.showTextDocument(await vscode.workspace.openTextDocument(compilerSettingsLocation()!.uri), { preview: false }); + } + await focusTestWindow(); + await vscode.commands.executeCommand('workbench.action.focusActiveEditorGroup'); + await vscode.commands.executeCommand(action); + await verify(moved); + } + })); +} diff --git a/lang/vscode-extension/src/test/progress.ts b/lang/vscode-extension/src/test/progress.ts new file mode 100644 index 00000000000..072850e3c50 --- /dev/null +++ b/lang/vscode-extension/src/test/progress.ts @@ -0,0 +1,62 @@ +import type Mocha from 'mocha'; +import * as vscode from 'vscode'; +import { PLAYBOOK_WAIT_MS } from './wait'; + +interface WaitStep { readonly token: symbol; readonly message?: string; readonly elapsed?: number } +const waits = new vscode.EventEmitter(); + +/** Describe bounded waits without hiding the case ID, counters or failure result. */ +export async function withTestWait(message: string, action: () => Promise): Promise { + const token = Symbol(message); + const started = performance.now(); + const report = () => waits.fire({ token, message, elapsed: Math.floor((performance.now() - started) / 1000) }); + report(); + const timer = setInterval(report, 1000); + try { return await action(); } + finally { clearInterval(timer); waits.fire({ token }); } +} + +/** Share native status-bar progress between smoke tests and selected or full playbook runs. */ +export function showTestProgress(runner: Mocha.Runner, title: string, label: (test: Mocha.Test) => string): void { + const progress = vscode.window.createStatusBarItem('xtc.tests.progress', vscode.StatusBarAlignment.Left, 10_000); + progress.name = `${title} progress`; + const show = (current: string, detail: string) => { + const completed = runner.stats?.tests ?? 0; + const failed = runner.stats?.failures ?? 0; + const skipped = runner.stats?.pending ?? 0; + const summary = `${title}: ${completed}/${runner.total} completed, ${runner.total - completed} left | ${current}` + + (failed ? ` | ${failed} failed` : '') + (skipped ? ` | ${skipped} skipped` : ''); + progress.text = `$(beaker) ${summary}`; + progress.tooltip = detail; + progress.accessibilityInformation = { label: `${summary}. ${detail}` }; + progress.show(); + }; + // Keep the ID and a useful description visible; the tooltip retains the complete title. + const caption = (test: Mocha.Test) => { + const text = label(test); + return text.length > 90 ? `${text.slice(0, 87).trimEnd()}…` : text; + }; + // Owned by this serial runner; nested waits restore their enclosing step on completion. + const activeWaits = new Map(); + let current: Mocha.Test | undefined; + const subscription = waits.event(step => { + if (step.message === undefined) activeWaits.delete(step.token); + else activeWaits.set(step.token, step); + if (!current) return; + const active = [...activeWaits.values()].pop(); + const message = active?.message; + if (message === undefined) { show(`${caption(current)} — running`, current.fullTitle()); return; } + const timing = `${active?.elapsed}s / ${PLAYBOOK_WAIT_MS / 1000}s`; + const detail = `${current.fullTitle()}\nWaiting: ${message} (${timing})`; + const short = message.length > 58 ? `${message.slice(0, 55)}…` : message; + show(`${current.title.split(':')[0]} — ${short} (${timing})`, detail); + }); + show('starting', `Starting ${title}`); + runner.on('test', test => { current = test; show(`${caption(test)} — running`, test.fullTitle()); }); + runner.on('test end', test => { + current = undefined; + activeWaits.clear(); + show(`${caption(test)} — ${test.state ?? 'skipped'}`, test.fullTitle()); + }); + runner.once('end', () => { subscription.dispose(); progress.dispose(); }); +} diff --git a/lang/vscode-extension/src/test/runLifecycle.ts b/lang/vscode-extension/src/test/runLifecycle.ts new file mode 100644 index 00000000000..362884eb01d --- /dev/null +++ b/lang/vscode-extension/src/test/runLifecycle.ts @@ -0,0 +1,154 @@ +import * as assert from 'node:assert'; +import { spawn } from 'node:child_process'; +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { catalog } from './playbook/shared'; +import { testVSCodeBuild } from './vscodeCache'; + +interface LifecycleReceipt { + pid: number; + applicationPid: number; + status?: string; + pending?: boolean; + unsaved?: boolean; +} + +/** Keep a second real window alive while another closes with pending work and reopens from backup. */ +export async function runLifecycle(extensionRoot: string, sharedProcess = false): Promise { + const { version, executable } = await testVSCodeBuild(extensionRoot); + const reports = path.join(extensionRoot, 'build/reports/project-lifecycle'); + await fs.mkdir(reports, { recursive: true }); + const directory = await fs.mkdtemp(path.join(reports, 'run-')); + const profiles = await fs.mkdtemp(path.join(os.tmpdir(), 'xtc-code-life-')); + const extensions = path.join(profiles, 'extensions'); + const driver = path.join(extensions, 'local-test.lifecycle-0.0.0'); + await fs.mkdir(driver, { recursive: true }); + await fs.symlink(extensionRoot, path.join(extensions, 'xtclang.xtc-language'), process.platform === 'win32' ? 'junction' : 'dir'); + await fs.writeFile(path.join(driver, 'package.json'), JSON.stringify({ + name: 'lifecycle', publisher: 'local-test', version: '0.0.0', + engines: { vscode: `^${version}` }, activationEvents: ['onStartupFinished'], main: './index.js' + }, null, 2) + '\n'); + await fs.writeFile(path.join(driver, 'index.js'), ` +const vscode = require('vscode'); +exports.activate = () => { + require(${JSON.stringify(path.join(__dirname, 'lifecycle/index.js'))}).run().then( + () => vscode.commands.executeCommand(process.env.XTC_LIFECYCLE_SHARED_PROCESS === 'true' && + vscode.workspace.workspaceFolders?.[0].name !== 'primary' ? 'workbench.action.closeWindow' : 'workbench.action.quit'), + error => { console.error(error); return vscode.commands.executeCommand('workbench.action.quit'); } + ); +}; +`); + const data = catalog.cases.X145.values; + for (const role of ['primary', 'secondary']) { + const workspace = path.join(directory, role); + await fs.mkdir(path.join(workspace, '.vscode'), { recursive: true }); + await fs.writeFile(path.join(workspace, data.file), data.source); + await fs.writeFile(path.join(workspace, '.vscode/settings.json'), JSON.stringify({ + 'files.autoSave': 'off', + 'xtc.compiler.sourceModules': [{ name: data.module, uri: data.file }] + }, null, 2) + '\n'); + } + // Hot exit and window restoration are application settings: workspace values are ignored. + for (const profile of sharedProcess ? ['shared'] : ['primary', 'secondary']) { + const user = path.join(profiles, profile, 'User'); + await fs.mkdir(user, { recursive: true }); + await fs.writeFile(path.join(user, 'settings.json'), JSON.stringify({ + 'files.hotExit': 'onExitAndWindowClose', 'window.restoreWindows': 'none' + }, null, 2) + '\n'); + } + console.log(`[project-lifecycle] Native window receipts: ${directory}`); + // Development windows deliberately have no persistent backup path in VS Code. Install only + // these two local extensions into an isolated normal profile so hot exit is exercised for real. + const launch = (role: string) => { + const child = spawn(executable, [path.join(directory, role === 'primary' ? 'primary' : 'secondary'), + `--user-data-dir=${path.join(profiles, sharedProcess ? 'shared' : role === 'primary' ? 'primary' : 'secondary')}`, + `--extensions-dir=${extensions}`, '--skip-welcome', '--skip-release-notes', '--new-window', + '--disable-workspace-trust', '--disable-updates', '--no-sandbox', '--disable-gpu-sandbox'], { + env: { ...process.env, XTC_LIFECYCLE_ROLE: role, XTC_LIFECYCLE_REPORT: directory, XTC_LIFECYCLE_SHARED_PROCESS: String(sharedProcess) }, stdio: 'inherit' + }); + assert.ok(child.pid); + const completion = new Promise((resolve, reject) => { + const deadline = setTimeout(() => { + reject(new Error(`${role} native window did not finish within 180 seconds`)); + child.kill(); // Failed-run cleanup only; never evidence of successful process retirement. + }, 180_000); + child.once('error', error => { clearTimeout(deadline); reject(error); }); + child.once('exit', (code, signal) => { + clearTimeout(deadline); + if (code === 0) resolve(); + else reject(new Error(`${role} native window exited with code ${code}, signal ${signal}`)); + }); + }); + return { pid: child.pid, completion }; + }; + const read = async (name: string): Promise => { + const deadline = Date.now() + 120_000; + while (Date.now() < deadline) { + const failures = (await fs.readdir(directory)).filter(file => file.endsWith('-failure.json')); + assert.deepStrictEqual(failures, [], `Window failed while waiting for ${name}`); + try { return JSON.parse(await fs.readFile(path.join(directory, `${name}.json`), 'utf8')); } + catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; } + await new Promise(resolve => setTimeout(resolve, 100)); + } + assert.fail(`Missing ${name} receipt`); + }; + const waitForExit = async (pid: number) => { + const deadline = Date.now() + 15_000; + while (Date.now() < deadline) { + try { process.kill(pid, 0); } + catch (error) { if ((error as NodeJS.ErrnoException).code === 'ESRCH') return; throw error; } + await new Promise(resolve => setTimeout(resolve, 100)); + } + assert.fail(`Native window left compiler process ${pid} alive`); + }; + const primaryProcess = launch('primary'); + const primary = primaryProcess.completion.then(() => undefined, error => error as Error); + try { + // Ensure the first process owns the profile before another CLI hands it a new window. + if (sharedProcess) await read('primary-ready'); + await launch('closing').completion; + const closed = await read('closing'); + assert.ok(!(await fs.readdir(directory)).some(file => file.endsWith('-failure.json'))); + assert.ok(closed.pending && closed.unsaved); + await waitForExit(closed.pid); + await launch('reopened').completion; + const reopened = await read('reopened'); + assert.strictEqual(reopened.status, 'passed'); + await waitForExit(reopened.pid); + assert.ifError(await primary); + const first = await read('primary'); + assert.strictEqual(first.status, 'passed'); + await waitForExit(first.pid); + if (sharedProcess) { + assert.strictEqual(first.applicationPid, primaryProcess.pid); + assert.strictEqual(closed.applicationPid, first.applicationPid, 'Closed window shared the live main process'); + assert.strictEqual(reopened.applicationPid, first.applicationPid, 'Reopening reused the same main process'); + } + await fs.writeFile(path.join(directory, 'results.json'), JSON.stringify({ + status: 'passed', vscodeWindows: 2, separateProfiles: !sharedProcess, sharedProcess, installedExtensions: true, + primary: first, closing: closed, reopened + }, null, 2) + '\n'); + await fs.writeFile(path.join(directory, '.completed'), 'Native lifecycle windows exited\n'); + } catch (error) { + const failure = JSON.stringify({ status: 'failed', error: String(error) }, null, 2) + '\n'; + await fs.writeFile(path.join(directory, 'launcher-failure.json'), failure); + await fs.writeFile(path.join(directory, 'results.json'), failure); + throw error; + } finally { + await primary; + // Retain the real host logs before removing only these disposable test profiles. + for (const role of sharedProcess ? ['shared'] : ['primary', 'secondary']) { + await fs.cp(path.join(profiles, role, 'logs'), path.join(directory, `${role}-logs`), { recursive: true }) + .catch(error => { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }); + } + // Retain native backup evidence on failure without changing the next run's profile. + if (await fs.stat(path.join(directory, 'launcher-failure.json')).then(() => true, () => false)) { + for (const profile of sharedProcess ? ['shared'] : ['primary', 'secondary']) { + await fs.cp(path.join(profiles, profile, 'Backups'), path.join(directory, `${profile}-backups`), { recursive: true }) + .catch(error => { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; }); + } + } + await fs.rm(profiles, { recursive: true, force: true }); + } +} diff --git a/lang/vscode-extension/src/test/runTest.ts b/lang/vscode-extension/src/test/runTest.ts index 7d56cf19ed1..f01020d6834 100644 --- a/lang/vscode-extension/src/test/runTest.ts +++ b/lang/vscode-extension/src/test/runTest.ts @@ -1,84 +1,138 @@ -// Headless integration-test launcher. Downloads VS Code builds into the shared -// per-user cache (see vscodeCache.ts), then runs the Mocha suite inside each of -// them with the extension loaded from this build tree: once on the oldest VS -// Code that package.json's engines.vscode supports and once on the latest -// stable, so an API or Node feature missing from the minimum fails here, not -// for users. +// Headless integration-test launcher. Downloads (and caches) a VS Code +// build via @vscode/test-electron, then runs the Mocha suite inside that +// VS Code instance with the extension loaded from this build tree. // // Invoked by `npm run test:vscode` and by the // `:lang:vscode-extension:testVscodeExtension` Gradle task. -import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { execFileSync } from 'node:child_process'; +import * as fs from 'node:fs/promises'; import * as os from 'node:os'; import * as path from 'node:path'; -import { downloadAndUnzipVSCode, runTests } from '@vscode/test-electron'; -import { - buildDirectory, - markUsedAndPrune, - removeCheckoutBuilds, - sharedCachePath, - withDownloadLock, -} from './vscodeCache'; +import { runTests } from '@vscode/test-electron'; +import { testVSCodeBuild } from './vscodeCache'; +import { workbenchPort } from './workbenchUi'; async function main(): Promise { // __dirname at runtime resolves to /out/test, so the extension // root and the fixtures directory are two levels up. - const extensionDevelopmentPath = path.resolve(__dirname, '..', '..'); - const extensionTestsPath = path.resolve(__dirname, 'suite', 'index'); - const fixturesPath = path.resolve(extensionDevelopmentPath, 'src', 'test', 'fixtures'); - const manifest = JSON.parse(readFileSync(path.join(extensionDevelopmentPath, 'package.json'), 'utf8')) as { - engines: { vscode: string }; - }; - const minimumVersion = manifest.engines.vscode.replace(/^\^/, ''); - const cachePath = sharedCachePath(); - - for (const build of removeCheckoutBuilds(path.join(extensionDevelopmentPath, '.vscode-test'))) { - console.log(`[vscode-test] Removed ${build}: builds now live in ${cachePath}`); + const extensionRoot = path.resolve(__dirname, '..', '..'); + const args = process.argv.slice(2); + if (args.includes('--project-lifecycle') && args.every(arg => ['--project-lifecycle', '--shared-process'].includes(arg))) { + await (await import('./runLifecycle.js')).runLifecycle(extensionRoot, args.includes('--shared-process')); + return; + } + if (args.some(argument => !['--settings-persistence', '--playbook', '--multi-root', '--cancel-ui', '--explorer-move-probe', '--refresh-during-move'].includes(argument) && !argument.startsWith('--cases=') && !argument.startsWith('--theme=') && argument !== '--lexical-baseline')) { + throw new Error('Expected --playbook with optional --cases=ID[,ID], --multi-root, --cancel-ui, --theme=NAME and --lexical-baseline, or --project-lifecycle with optional --shared-process, or --explorer-move-probe with optional --refresh-during-move, or --settings-persistence'); + } + const explorerProbe = args.includes('--explorer-move-probe'); + const refreshDuringMove = args.includes('--refresh-during-move'); + if (refreshDuringMove && !explorerProbe) throw new Error('--refresh-during-move requires --explorer-move-probe'); + if (explorerProbe && args.some(argument => !['--explorer-move-probe', '--refresh-during-move'].includes(argument))) { + throw new Error('Run --explorer-move-probe with only the optional --refresh-during-move flag'); + } + const persistence = args.includes('--settings-persistence'); + if (persistence && args.length !== 1) throw new Error('--settings-persistence runs independently'); + const playbook = args.includes('--playbook'); + const multiRoot = args.includes('--multi-root'); + const cancelUi = args.includes('--cancel-ui'); + const uiPort = await workbenchPort(); + if (multiRoot && !playbook) throw new Error('Use --multi-root with --playbook'); + if (cancelUi && !playbook) throw new Error('Use --cancel-ui with --playbook'); + const selections = args.filter(argument => argument.startsWith('--cases=')); + if (selections.length > 1 || (selections.length > 0 && !playbook)) { + throw new Error('Use --cases=ID[,ID] once, with --playbook'); + } + const selected = playbook + ? (await import('./playbook/shared.js')).selectedScenarioIds(selections[0]?.slice('--cases='.length)) + : []; + if (cancelUi && !selected.includes('X145')) throw new Error('--cancel-ui requires X145 in the selected cases'); + const themes = args.filter(argument => argument.startsWith('--theme=')); + const lexicalBaseline = args.includes('--lexical-baseline'); + if (themes.length > 1 || (themes.length > 0 && !playbook)) throw new Error('Use --theme=NAME once, with --playbook'); + const theme = themes[0]?.slice('--theme='.length) ?? 'Default Dark Modern'; + if (themes.length && !(await import('./playbook/shared.js')).catalog.common.colorPrototype.themes.vscode.includes(theme)) { + throw new Error(`Unsupported playbook theme: ${theme}`); + } + if (lexicalBaseline && (!playbook || selected.length !== 1 || selected[0] !== 'X277')) { + throw new Error('--lexical-baseline requires --playbook --cases=X277'); + } + const extensionTestsPath = path.resolve(__dirname, persistence ? 'settings-persistence' : explorerProbe ? 'explorer-probe' : playbook ? 'playbook' : 'suite', 'index'); + const { version, executable } = await testVSCodeBuild(extensionRoot); + console.log(`[vscode-test] Running on VS Code ${version} (${executable})`); + const reports = path.join(extensionRoot, 'build', 'reports', explorerProbe ? 'explorer-probe' : playbook ? 'compiler-playbook' : 'extension-tests'); + await fs.mkdir(reports, { recursive: true }); + const runDirectory = await fs.mkdtemp(path.join(reports, 'run-')); + const extensionDevelopmentPath = explorerProbe ? path.join(runDirectory, 'empty-extension') : extensionRoot; + if (explorerProbe) { + await fs.mkdir(extensionDevelopmentPath); + await fs.writeFile(path.join(extensionDevelopmentPath, 'package.json'), JSON.stringify({ + name: 'explorer-move-probe', publisher: 'local-test', version: '0.0.0', engines: { vscode: `^${version}` } + }, null, 2) + '\n'); + } + // Unix-domain sockets inside user-data-dir have a short OS path limit (103 on macOS). + // Smoke tests also mutate settings; never reuse a profile or the repository fixtures. + const profile = await fs.mkdtemp(path.join(os.tmpdir(), 'xtc-code-')); + // Seed the profile before Electron creates the first workbench: no live theme transitions. + await fs.mkdir(path.join(profile, 'User'), { recursive: true }); + await fs.writeFile(path.join(profile, 'User', 'settings.json'), JSON.stringify({ 'workbench.colorTheme': theme }, null, 2)); + const fixturesPath = path.join(runDirectory, 'workspace'); + if (!playbook && !explorerProbe) { + await fs.cp(path.join(extensionDevelopmentPath, 'src', 'test', 'fixtures'), fixturesPath, { + recursive: true, filter: source => path.basename(source) !== '.vscode' + }); + } + await fs.mkdir(path.join(fixturesPath, '.vscode'), { recursive: true }); + await fs.writeFile(path.join(fixturesPath, '.vscode', 'settings.json'), JSON.stringify({ + 'files.autoSave': 'off', 'editor.semanticHighlighting.enabled': true, + 'editor.inlayHints.enabled': 'on', + '[xtc]': { 'editor.semanticHighlighting.enabled': !lexicalBaseline } + }, null, 2)); + console.log(`[vscode-test] Reports and isolated workspace: ${runDirectory}`); + await fs.writeFile(path.join(reports, 'latest-run.txt'), runDirectory + '\n'); + // Folder retirement needs an existing workspace container. Converting a single-folder + // extension-test window into a new workspace would replace the running test host itself. + const workspaceFile = persistence || multiRoot || selected.includes('X265') ? path.join(runDirectory, 'compiler.code-workspace') : undefined; + if (workspaceFile) { + await fs.mkdir(path.join(runDirectory, 'external'), { recursive: true }); + await fs.writeFile(workspaceFile, JSON.stringify({ + folders: [{ path: 'workspace' }, ...(multiRoot ? [{ path: 'external' }] : [])], settings: {} + }, null, 2) + '\n'); } - const builds = await withDownloadLock(cachePath, async () => { - const downloaded: { version: string; executable: string }[] = []; - for (const version of [minimumVersion, 'stable']) { - downloaded.push({ version, executable: await downloadAndUnzipVSCode({ version, cachePath }) }); - } - const pruned = markUsedAndPrune( - cachePath, - downloaded.map(({ executable }) => buildDirectory(cachePath, executable)), - ); - for (const build of pruned) { - console.log(`[vscode-test] Removed ${build}: unused for 30 days`); - } - return downloaded; - }); - - for (const { version, executable } of builds) { - console.log(`[vscode-test] Running the suite on VS Code ${version} (${executable})`); - // A fresh profile for each build, in the system's temporary directory: - // VS Code's IPC socket lives in it, and macOS limits a socket's path to - // 103 characters. Its settings turn off VS Code's AI features, which - // otherwise look for a GitHub account the test instance does not have - // and log every miss ("[AgentHost] No signed-in session resolved ..."). - const profile = mkdtempSync(path.join(os.tmpdir(), 'xtc-code-')); - try { - mkdirSync(path.join(profile, 'User')); - writeFileSync( - path.join(profile, 'User', 'settings.json'), - JSON.stringify({ 'chat.disableAIFeatures': true }, null, 4), - ); - await runTests({ - vscodeExecutablePath: executable, - extensionDevelopmentPath, - extensionTestsPath, - // --disable-extensions stops third-party extensions (anything that - // claims `.x` — e.g. Logos parser-generator extensions) from - // preempting our language registration; that way the test asserts - // OUR behaviour, not the intersection of the user's installed - // extensions and ours. - launchArgs: [fixturesPath, '--disable-extensions', `--user-data-dir=${profile}`], - }); - } finally { - rmSync(profile, { recursive: true, force: true }); - } + try { + for (const phase of persistence ? ['write', 'read'] : ['single']) await runTests({ + vscodeExecutablePath: executable, + extensionDevelopmentPath, + extensionTestsPath, + // --disable-extensions stops third-party extensions (anything that + // claims `.x` — e.g. Logos parser-generator extensions) from + // preempting our language registration; that way the test asserts + // OUR behaviour, not the intersection of the user's installed + // extensions and ours. + launchArgs: [workspaceFile ?? fixturesPath, '--disable-extensions', + `--user-data-dir=${profile}`, '--skip-welcome', '--skip-release-notes', + `--remote-debugging-port=${uiPort}`, '--remote-debugging-address=127.0.0.1' + ], + extensionTestsEnv: playbook ? { + XTC_LSP_COLOR_PROTOTYPE: String((await import('./playbook/shared.js')).catalog.common.colorPrototype.cases.some(id => selected.includes(id as typeof selected[number]))), + XTC_PLAYBOOK_REPORT_DIR: runDirectory, + XTC_LSP_LOG_DIR: path.join(runDirectory, 'server-logs'), + XTC_PLAYBOOK_CASES: selected.join(','), + XTC_PLAYBOOK_CANCEL_UI: String(cancelUi), + XTC_PLAYBOOK_UI_PORT: String(uiPort), + XTC_PLAYBOOK_COMMIT: execFileSync('git', ['rev-parse', 'HEAD'], { cwd: extensionDevelopmentPath, encoding: 'utf8' }).trim(), + XTC_PLAYBOOK_DIRTY: execFileSync('git', ['status', '--porcelain'], { cwd: extensionDevelopmentPath, encoding: 'utf8' }).trim() + } : explorerProbe ? { XTC_EXPLORER_PROBE_REPORT: runDirectory, + XTC_EXPLORER_PROBE_REFRESH: String(refreshDuringMove) } : { XTC_LSP_LOG_DIR: path.join(runDirectory, 'server-logs'), XTC_SETTINGS_PHASE: phase }, + }); + } catch (error) { + await fs.writeFile(path.join(runDirectory, 'launcher-error.txt'), String(error) + '\n'); + throw error; + } finally { + await fs.cp(path.join(profile, 'logs'), path.join(runDirectory, 'logs'), { recursive: true }).catch(() => undefined); + await fs.rm(profile, { recursive: true, force: true }); + await fs.writeFile(path.join(runDirectory, '.completed'), 'VS Code exited\n'); } } diff --git a/lang/vscode-extension/src/test/settings-persistence/index.ts b/lang/vscode-extension/src/test/settings-persistence/index.ts new file mode 100644 index 00000000000..0466d96aeac --- /dev/null +++ b/lang/vscode-extension/src/test/settings-persistence/index.ts @@ -0,0 +1,46 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { getClient } from '../../lsp-client'; +import { catalog } from '../playbook/shared'; +import { eventually } from '../playbook/support'; + +/** Separate VS Code processes reuse this disposable profile and workspace, with no replay on read. */ +export async function run(): Promise { + const phase = process.env.XTC_SETTINGS_PHASE; + assert.ok(phase === 'write' || phase === 'read'); + const root = vscode.workspace.workspaceFolders![0].uri; + const data = catalog.common.runtimeSettings; + const file = vscode.Uri.joinPath(root, data.file); + const settings = vscode.workspace.getConfiguration('xtc'); + if (phase === 'write') { + await vscode.workspace.fs.writeFile(file, Buffer.from(data.source)); + await settings.update('java.vmOptions', data.vmOptions, vscode.ConfigurationTarget.Global); + await settings.update('server.logs', data.logs, vscode.ConfigurationTarget.Global); + await settings.update('languageService.textSynchronization', 'incremental', vscode.ConfigurationTarget.Global); + await settings.update('inlayHints.enabled', false, vscode.ConfigurationTarget.Global); + await settings.update('inlayHints.enabled', true, vscode.ConfigurationTarget.Workspace); + await settings.update('compiler.sourceModules', [{ name: 'RuntimeSettings', uri: file.toString(), resourceRoots: [] }], vscode.ConfigurationTarget.Workspace); + } + const extension = vscode.extensions.getExtension('xtclang.xtc-language')!; + await extension.activate(); + await vscode.window.showTextDocument(await vscode.workspace.openTextDocument(file)); + if (phase === 'write') await vscode.commands.executeCommand('xtc.restartServer'); + await eventually(async () => getClient()?.isRunning(), running => running === true, 'Persisted connection starts'); + const status = await getClient()!.sendRequest<{ adapter: string; pid: number; jvmOptions: string[]; textSynchronization: string; logs: { retention: object } }>('xtc/languageServiceStatus'); + assert.ok(['XDK', 'TreeSitter'].includes(status.adapter), 'Persistence must use a real packaged adapter'); + assert.strictEqual(status.textSynchronization, 'incremental'); + assert.ok(status.jvmOptions.includes(data.vmOptions[0])); + assert.deepStrictEqual(status.logs.retention, data.logs); + const saved = vscode.workspace.getConfiguration('xtc'); + assert.strictEqual(saved.inspect('inlayHints.enabled')!.globalValue, false); + assert.strictEqual(saved.inspect('inlayHints.enabled')!.workspaceValue, true); + const report = await vscode.commands.executeCommand<{ configured: Record }>('xtc.showLanguageServiceStatus'); + assert.strictEqual(report!.configured['inlayHints.enabled'].value, true); + assert.strictEqual(report!.configured['inlayHints.enabled'].origin, 'workspace'); + assert.deepStrictEqual(saved.get('compiler.sourceModules'), [{ name: 'RuntimeSettings', uri: file.toString(), resourceRoots: [] }]); + const previous = path.join(root.fsPath, '..', 'settings-write.json'); + if (phase === 'read') assert.notStrictEqual(JSON.parse(await fs.readFile(previous, 'utf8')).pid, status.pid); + await fs.writeFile(path.join(root.fsPath, '..', `settings-${phase}.json`), JSON.stringify({ status: 'passed', adapter: status.adapter, pid: status.pid, configured: report!.configured }, null, 2) + '\n'); +} diff --git a/lang/vscode-extension/src/test/suite/activation.test.ts b/lang/vscode-extension/src/test/suite/activation.test.ts index 7e116835bc1..1320eea4035 100644 --- a/lang/vscode-extension/src/test/suite/activation.test.ts +++ b/lang/vscode-extension/src/test/suite/activation.test.ts @@ -5,6 +5,7 @@ import * as assert from 'node:assert'; import * as vscode from 'vscode'; +import { configurationProperties } from '../../settings-report'; const PUBLISHER_AND_NAME = 'xtclang.xtc-language'; @@ -12,7 +13,11 @@ const EXPECTED_COMMANDS = [ 'xtc.createProject', 'xtc.runModule', 'xtc.restartServer', + 'xtc.selectLanguageAdapter', 'xtc.showServerOutput', + 'xtc.hideServerOutput', + 'xtc.openLanguageSettings', + 'xtc.showLanguageServiceStatus', ]; suite('Extension activation surfaces', () => { @@ -34,6 +39,18 @@ suite('Extension activation surfaces', () => { ); }); + test('connection preferences are window scoped and ineffective formatting fields are explicit', () => { + const properties = configurationProperties(vscode.extensions.getExtension(PUBLISHER_AND_NAME)!.packageJSON.contributes.configuration); + for (const key of ['adapter', 'textSynchronization', 'saveFormatting']) { + assert.strictEqual(properties[`xtc.languageService.${key}`].scope, 'window'); + } + assert.strictEqual(properties['xtc.languageService.textSynchronization'].default, 'full'); + assert.strictEqual(properties['xtc.languageService.saveFormatting'].default, 'editor'); + assert.ok(properties['xtc.formatting.tabSize'].deprecationMessage); + assert.strictEqual(properties['xtc.formatting.maxLineWidth'].deprecationMessage, undefined); + assert.strictEqual(properties['xtc.formatting.maxLineWidth'].default, 120); + }); + test('xtc.showServerOutput executes without throwing', async () => { // Executing this command instantiates the output channel if not already // created, which gives us a non-invasive smoke test that the channel diff --git a/lang/vscode-extension/src/test/suite/build-model.test.ts b/lang/vscode-extension/src/test/suite/build-model.test.ts new file mode 100644 index 00000000000..d17bc4d8004 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/build-model.test.ts @@ -0,0 +1,33 @@ +import * as assert from 'node:assert'; +import { BuildSourceSet, mergeBuildModels, parseBuildModel } from '../../build-model'; + +suite('Evaluated compiler build model', () => { + const entry: BuildSourceSet = { + projectId: 'file:///workspace/#:library', projectPath: ':library', + projectDirectory: 'file:///workspace/library/', buildFile: 'file:///workspace/library/build.gradle.kts', + sourceSet: 'main', sourceRoots: ['file:///outside/custom-source/'], + sourceFiles: ['file:///outside/custom-source/Library.x'], moduleRoots: ['file:///outside/custom-source/Library.x'], + resourceSourceRoots: ['file:///outside/assets/'], resourceRoots: ['file:///workspace/build/processed/', 'file:///fallback/'], + resourceTask: ':library:processXtcResources', projectDependencies: [], modulePath: ['file:///outside/Library.xtc'] + }; + const text = (sourceSets: unknown[] = [entry], schemaVersion = 1) => JSON.stringify({ schemaVersion, sourceSets }); + + test('preserves external paths, ordered processed resources and distinct main/test owners', () => { + const model = parseBuildModel(text([entry, { ...entry, sourceSet: 'test' }])); + assert.strictEqual(model.sourceSets.length, 2); + assert.deepStrictEqual(model.sourceSets[0], entry); + }); + + test('rejects malformed or ambiguous models before installing configuration', () => { + [text([entry], 2), text([entry, entry]), text([{ ...entry, sourceRoots: ['relative/path'] }]), + text([{ ...entry, projectId: 42 }]), text([{ ...entry, resourceRoots: undefined }]), + text([{ ...entry, projectDependencies: [1] }])].forEach(value => assert.throws(() => parseBuildModel(value))); + }); + + test('overlapping composite and workspace roots deduplicate only identical owners', () => { + const first = parseBuildModel(text()); + const second = parseBuildModel(text([{ ...entry, projectId: 'file:///other/#:library' }])); + assert.strictEqual(mergeBuildModels([first, structuredClone(first), second]).sourceSets.length, 2); + assert.throws(() => mergeBuildModels([first, parseBuildModel(text([{ ...entry, resourceRoots: [] }]))]), /Conflicting/); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/compiler-import.test.ts b/lang/vscode-extension/src/test/suite/compiler-import.test.ts new file mode 100644 index 00000000000..c22d2735c62 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/compiler-import.test.ts @@ -0,0 +1,94 @@ +import * as assert from 'node:assert'; +import { CompilerImport } from '../../compiler-import'; + +suite('Compiler import ownership', () => { + function fixture() { + let disk: string | undefined = 'original'; + const owner = new CompilerImport(() => disk, text => { if (text === 'invalid') throw new Error('Invalid model'); }); + return { owner, write: (text?: string) => { disk = text; } }; + } + + test('watchers retain accepted inputs until successful completion and reject overlapping imports', () => { + const { owner, write } = fixture(); + assert.strictEqual(owner.current(), 'original'); + const operation = owner.begin(false); + write('new'); + assert.strictEqual(owner.current(), 'original'); + assert.match(owner.description(), /Refreshing/); + assert.throws(() => owner.begin(true), /already running/); + assert.strictEqual(owner.finish(operation, 'succeeded').outcome, 'succeeded'); + assert.strictEqual(owner.current(), 'new'); + assert.match(owner.description(), /Last import:/); + }); + + test('cancelled and failed valid reports stay unaccepted until explicit retry', () => { + for (const outcome of ['cancelled', 'failed'] as const) { + const { owner, write } = fixture(); + owner.current(); + const operation = owner.begin(true); + write('unaccepted'); + assert.strictEqual(owner.finish(operation, outcome).outcome, outcome); + assert.strictEqual(owner.current(), 'original'); + assert.strictEqual(owner.current(), 'original'); + owner.finish(owner.begin(false), 'succeeded'); + assert.strictEqual(owner.current(), 'unaccepted'); + } + }); + + test('invalid or missing outputs cannot replace accepted inputs and later repairs remain observable', () => { + const { owner, write } = fixture(); + owner.current(); + for (const output of ['invalid', undefined]) { + const operation = owner.begin(false); + write(output); + assert.strictEqual(owner.finish(operation, 'succeeded').outcome, 'failed'); + assert.strictEqual(owner.current(), 'original'); + } + write('externally repaired'); + assert.strictEqual(owner.current(), 'externally repaired'); + write(undefined); + assert.strictEqual(owner.current(), undefined); + }); + + test('late completion cannot retire or publish over a newer import', () => { + const { owner, write } = fixture(); + owner.current(); + const first = owner.begin(false); + owner.finish(first, 'cancelled'); + const second = owner.begin(true); + write('new'); + assert.throws(() => owner.finish(first, 'succeeded'), /no longer owns/); + assert.strictEqual(owner.current(), 'original'); + owner.finish(second, 'succeeded'); + assert.strictEqual(owner.current(), 'new'); + }); + + test('removed workspace owners cannot publish late output or start another import', () => { + const { owner, write } = fixture(); + owner.current(); + const operation = owner.begin(false); + owner.retire(); + write('late report'); + assert.strictEqual(owner.finish(operation, 'succeeded').outcome, 'cancelled'); + assert.strictEqual(owner.current(), undefined); + assert.strictEqual(owner.retained(), undefined); + assert.throws(() => owner.begin(false), /retired/); + }); + + test('late daemon output stays rejected and cannot complete a different import', () => { + let disk = JSON.stringify({ value: 'initial' }); + const owner = new CompilerImport(() => disk, JSON.parse, text => JSON.parse(text).importId); + owner.current(); + const first = owner.begin(true); + owner.finish(first, 'cancelled'); + disk = JSON.stringify({ value: 'late', importId: first.id }); + assert.strictEqual(JSON.parse(owner.current()!).value, 'initial'); + const second = owner.begin(false); + assert.strictEqual(owner.finish(second, 'succeeded').outcome, 'failed'); + assert.strictEqual(JSON.parse(owner.current()!).value, 'initial'); + const retry = owner.begin(false); + disk = JSON.stringify({ value: 'accepted', importId: retry.id }); + assert.strictEqual(owner.finish(retry, 'succeeded').outcome, 'succeeded'); + assert.strictEqual(JSON.parse(owner.current()!).value, 'accepted'); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/compiler-paths.test.ts b/lang/vscode-extension/src/test/suite/compiler-paths.test.ts new file mode 100644 index 00000000000..1363d2f3138 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/compiler-paths.test.ts @@ -0,0 +1,41 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { CompilerPathDraft } from '../../compiler-paths'; + +suite('Compiler path draft ownership', () => { + test('model refresh and a newer dialog invalidate a pending edit', () => { + const changes = new vscode.EventEmitter(); + const old = new CompilerPathDraft(changes.event); + try { + old.assertCurrent(); + changes.fire(); + assert.throws(() => old.assertCurrent(), /Compiler paths changed/); + const current = new CompilerPathDraft(changes.event); + try { current.assertCurrent(); } finally { current.dispose(); } + } finally { old.dispose(); changes.dispose(); } + }); + + test('a settings update during dialogs is retained instead of overwritten', async () => { + const changes = new vscode.EventEmitter(); + const config = vscode.workspace.getConfiguration('xtc.compiler'); + const previous = config.inspect('sourceModules')?.workspaceValue; + const replacement = [{ name: 'DraftOwnership', uri: 'file:///draft-ownership.x' }]; + const draft = new CompilerPathDraft(changes.event); + try { + await config.update('sourceModules', replacement, vscode.ConfigurationTarget.Workspace); + assert.throws(() => draft.assertCurrent(), /Compiler paths changed/); + assert.deepStrictEqual(vscode.workspace.getConfiguration('xtc.compiler').get('sourceModules'), replacement); + } finally { + draft.dispose(); changes.dispose(); + await config.update('sourceModules', previous, vscode.ConfigurationTarget.Workspace); + } + }); + + test('disposed dialogs cannot publish their draft', () => { + const changes = new vscode.EventEmitter(); + const draft = new CompilerPathDraft(changes.event); + draft.dispose(); + assert.throws(() => draft.assertCurrent(), /Compiler paths changed/); + changes.dispose(); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/compiler-project.test.ts b/lang/vscode-extension/src/test/suite/compiler-project.test.ts new file mode 100644 index 00000000000..ed21eb2af02 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/compiler-project.test.ts @@ -0,0 +1,125 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as path from 'node:path'; +import * as vscode from 'vscode'; +import { getClient } from '../../lsp-client'; + +const LIBRARY = 'module Library { static Int value() = 1; class Item {} }'; +const CONSUMER = 'module Consumer { package lib import Library; Int value = 10; ' + + 'private Int pick(Int input) = input; Int run() { Int local = pick(input = lib.value()); return local + value; } }'; + +async function eventually(check: () => Promise | boolean, description: string, + details: () => unknown = () => undefined): Promise { + const end = Date.now() + 30_000; + while (Date.now() < end) { + if (await check()) { return; } + await new Promise(resolve => setTimeout(resolve, 100)); + } + assert.fail(`Timed out: ${description}\n${JSON.stringify(details(), null, 2)}`); +} + +async function replace(document: vscode.TextDocument, text: string): Promise { + const edit = new vscode.WorkspaceEdit(); + edit.replace(document.uri, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), text); + assert.ok(await vscode.workspace.applyEdit(edit)); +} + +suite('Compiler editor acceptance', function () { + this.timeout(180_000); + + suiteSetup(async function () { + await vscode.extensions.getExtension('xtclang.xtc-language')!.activate(); + await eventually(() => !!getClient()?.initializeResult, 'language server initialization'); + // These cases require the default compiler build; Tree-sitter runs the common smoke tests. + if (!getClient()!.initializeResult!.capabilities.typeHierarchyProvider) { this.skip(); } + }); + + test('settings, unsaved dependency rebuilding, semantic rename and reopen work through VS Code', async () => { + const workspace = vscode.workspace.workspaceFolders![0].uri.fsPath; + const directory = await fs.mkdtemp(path.join(workspace, 'compiler-acceptance-')); + const library = vscode.Uri.file(path.join(directory, 'Library.x')); + const consumer = vscode.Uri.file(path.join(directory, 'Consumer.x')); + const config = vscode.workspace.getConfiguration('xtc.compiler'); + const previous = config.inspect('sourceModules')?.workspaceValue; + try { + await fs.writeFile(library.fsPath, LIBRARY); + await fs.writeFile(consumer.fsPath, CONSUMER); + await config.update('sourceModules', [], vscode.ConfigurationTarget.Workspace); + const document = await vscode.workspace.openTextDocument(consumer); + await vscode.window.showTextDocument(document); + await eventually(() => vscode.languages.getDiagnostics(consumer).length > 0, 'missing dependency diagnostics'); + await config.update('sourceModules', [ + { name: 'Library', uri: library.toString() }, + { name: 'Consumer', uri: consumer.toString(), dependencies: ['Library'] } + ], vscode.ConfigurationTarget.Workspace); + await eventually(() => vscode.languages.getDiagnostics(consumer).length === 0, 'configuration enables source compilation'); + assert.strictEqual(document.getText(), CONSUMER, 'consumer fixture must remain unchanged while configuring compilation'); + const originalVersion = document.version; + const dependency = await vscode.workspace.openTextDocument(library); + await vscode.window.showTextDocument(dependency); + await replace(dependency, LIBRARY.replace('Int value() = 1', 'String value() = "changed"')); + await eventually(() => vscode.languages.getDiagnostics(consumer).length > 0, 'unchanged consumer reports new dependency type'); + assert.strictEqual(document.version, originalVersion); + await replace(dependency, LIBRARY); + await eventually(() => { + assert.strictEqual(document.getText(), CONSUMER, 'consumer fixture changed outside the dependency test'); + assert.strictEqual(dependency.getText(), LIBRARY, 'dependency fixture changed after correction'); + return vscode.languages.getDiagnostics(consumer).length === 0; + }, 'dependency correction clears consumer errors', () => vscode.languages.getDiagnostics(consumer)); + await vscode.window.showTextDocument(document); + + const definition = await vscode.commands.executeCommand('vscode.executeDefinitionProvider', consumer, + document.positionAt(document.getText().indexOf('lib.value') + 4)); + assert.ok(definition?.some(location => location.uri.toString() === library.toString()), 'definition reaches dependency source'); + const signature = await vscode.commands.executeCommand('vscode.executeSignatureHelpProvider', consumer, + document.positionAt(document.getText().lastIndexOf('pick(') + 5)); + assert.ok(signature?.signatures.some(item => item.label.includes('pick')), 'selected signature reaches the editor'); + const incomplete = 'module Consumer { void run(String text) { text.si } }'; + await replace(document, incomplete); + const completion = await vscode.commands.executeCommand('vscode.executeCompletionItemProvider', consumer, + document.positionAt(incomplete.indexOf('text.si') + 'text.si'.length)); + assert.ok(completion?.items.some(item => (typeof item.label === 'string' ? item.label : item.label.label) === 'size'), + 'compiler completion works at an incomplete member prefix'); + await replace(document, CONSUMER); + const rename = await vscode.commands.executeCommand('vscode.executeDocumentRenameProvider', consumer, + document.positionAt(document.getText().lastIndexOf('input')), 'number'); + assert.ok(rename && await vscode.workspace.applyEdit(rename), 'versioned rename applies through the editor'); + assert.ok(document.getText().includes('Int number) = number')); + assert.ok(document.getText().includes('pick(number = lib.value())')); + const rejected = await getClient()!.sendRequest('textDocument/rename', { + textDocument: { uri: consumer.toString() }, position: document.positionAt(document.getText().indexOf('local')), newName: 'value' + }); + assert.strictEqual(rejected, null, 'silent property capture must be rejected'); + + const prefix = document.getText() + '\n'; + const timings: number[] = []; + // Repeated editor notifications and queries, including real invalid/valid transitions. + for (let cycle = 0; cycle < 40; cycle++) { + const start = Date.now(); + await replace(document, prefix.replace('Int local =', 'MissingType local =') + `// ${cycle}`); + await eventually(() => vscode.languages.getDiagnostics(consumer).length > 0, `error cycle ${cycle}`); + await replace(document, prefix + `// ${cycle}`); + await eventually(() => vscode.languages.getDiagnostics(consumer).length === 0, `recovery cycle ${cycle}`); + const hovers = await vscode.commands.executeCommand('vscode.executeHoverProvider', consumer, + document.positionAt(document.getText().indexOf('local'))); + assert.ok(hovers?.length, 'hover survives repeated recompilation'); + timings.push(Date.now() - start); + } + timings.sort((a, b) => a - b); + console.log(`Compiler editor workload: 40 error/recovery cycles, p50=${timings[20]}ms p95=${timings[38]}ms`); + await replace(document, CONSUMER); + await document.save(); + await dependency.save(); + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); + const reopened = await vscode.workspace.openTextDocument(consumer); + await vscode.window.showTextDocument(reopened); + await eventually(async () => !!(await vscode.commands.executeCommand('vscode.executeHoverProvider', consumer, + reopened.positionAt(CONSUMER.indexOf('local'))))?.length, 'hover after reopen'); + assert.strictEqual(vscode.languages.getDiagnostics(consumer).length, 0); + } finally { + await config.update('sourceModules', previous, vscode.ConfigurationTarget.Workspace); + await vscode.commands.executeCommand('workbench.action.closeAllEditors'); + await fs.rm(directory, { recursive: true, force: true }); + } + }); +}); diff --git a/lang/vscode-extension/src/test/suite/compiler-task.test.ts b/lang/vscode-extension/src/test/suite/compiler-task.test.ts new file mode 100644 index 00000000000..319da84e190 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/compiler-task.test.ts @@ -0,0 +1,108 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { runCompilerTask } from '../../compiler-task'; + +suite('Compiler task lifetime', () => { + function fixture() { + const processes = new vscode.EventEmitter(); + const tasks = new vscode.EventEmitter(); + const cancel = new vscode.CancellationTokenSource(); + const task = new vscode.Task({ type: 'xtc-model', operation: 'owned' }, vscode.TaskScope.Workspace, 'Import', 'Ecstasy'); + const terminations: string[] = []; + const execution = { task, terminate: () => { terminations.push('owned'); } }; + const host = { onDidEndTaskProcess: processes.event, onDidEndTask: tasks.event, + executeTask: async () => execution }; + return { task, execution, host, cancel, terminations, tasks, processes, + dispose: () => { cancel.dispose(); processes.dispose(); tasks.dispose(); } }; + } + + test('immediately exiting task is observed even before executeTask resolves', async () => { + const f = fixture(); + try { + f.host.executeTask = async () => { f.processes.fire({ execution: f.execution, exitCode: 0 }); return f.execution; }; + assert.strictEqual((await runCompilerTask(f.task, f.cancel.token, () => {}, f.host)).outcome, 'succeeded'); + f.cancel.cancel(); + await Promise.resolve(); + assert.deepStrictEqual(f.terminations, []); + } finally { f.dispose(); } + }); + + test('cancellation before dispatch never starts the task', async () => { + const f = fixture(); + try { + f.host.executeTask = async () => { throw new Error('Must not launch'); }; + const result = runCompilerTask(f.task, f.cancel.token, () => {}, f.host); + f.cancel.cancel(); + assert.strictEqual((await result).outcome, 'cancelled'); + } finally { f.dispose(); } + }); + + test('cancellation while the execution handle is pending terminates only the owned task', async () => { + const f = fixture(); + try { + const reports: string[] = []; + f.host.executeTask = async () => { + f.cancel.cancel(); + return { ...f.execution, terminate: () => { + f.terminations.push('owned'); + f.processes.fire({ execution: f.execution, exitCode: undefined }); + f.tasks.fire({ execution: f.execution }); + } }; + }; + const result = await runCompilerTask(f.task, f.cancel.token, message => reports.push(message), f.host); + assert.strictEqual(result.outcome, 'cancelled'); + assert.deepStrictEqual(f.terminations, ['owned']); + assert.deepStrictEqual(reports, ['Cancelling Gradle import…']); + } finally { f.dispose(); } + }); + + test('unrelated task exits do not complete this import and missing process results fail', async () => { + const f = fixture(); + try { + const other = new vscode.Task({ type: 'xtc-model', operation: 'other' }, vscode.TaskScope.Workspace, 'Other', 'Ecstasy'); + f.host.executeTask = async () => { + f.processes.fire({ execution: { ...f.execution, task: other }, exitCode: 0 }); + f.tasks.fire({ execution: f.execution }); + return f.execution; + }; + assert.strictEqual((await runCompilerTask(f.task, f.cancel.token, () => {}, f.host)).outcome, 'failed'); + } finally { f.dispose(); } + }); + + test('startup failure retires cancellation listeners', async () => { + const f = fixture(); + try { + f.host.executeTask = async () => { throw new Error('Startup refused'); }; + await assert.rejects(runCompilerTask(f.task, f.cancel.token, () => {}, f.host), /Startup refused/); + f.cancel.cancel(); + await Promise.resolve(); + assert.deepStrictEqual(f.terminations, []); + } finally { f.dispose(); } + }); + + test('native process task success is observed through the actual VS Code event order', async () => { + const cancel = new vscode.CancellationTokenSource(); + const task = nativeTask('success', "console.log('Ecstasy compiler import task test');"); + try { + assert.strictEqual((await runCompilerTask(task, cancel.token, () => {})).outcome, 'succeeded'); + } finally { cancel.dispose(); } + }); + + test('native task cancellation terminates the process and completes as cancellation', async () => { + const cancel = new vscode.CancellationTokenSource(); + const task = nativeTask('cancel', 'setInterval(() => {}, 1000);'); + const started = vscode.tasks.onDidStartTaskProcess(event => { + if (event.execution.task.definition.operation === task.definition.operation) cancel.cancel(); + }); + try { + assert.strictEqual((await runCompilerTask(task, cancel.token, () => {})).outcome, 'cancelled'); + } finally { started.dispose(); cancel.dispose(); } + }); + + function nativeTask(operation: string, script: string): vscode.Task { + const owner = vscode.workspace.workspaceFolders?.[0]; + assert.ok(owner, 'Native import regression needs the test workspace'); + return new vscode.Task({ type: 'xtc-model', operation: `test-import-${operation}` }, owner, `Import ${operation}`, 'Ecstasy', + new vscode.ProcessExecution(process.execPath, ['-e', script], { env: { ELECTRON_RUN_AS_NODE: '1' } })); + } +}); diff --git a/lang/vscode-extension/src/test/suite/extension.test.ts b/lang/vscode-extension/src/test/suite/extension.test.ts index 21f3cb5304c..7caec897732 100644 --- a/lang/vscode-extension/src/test/suite/extension.test.ts +++ b/lang/vscode-extension/src/test/suite/extension.test.ts @@ -2,7 +2,6 @@ // Runs inside a real VS Code instance via @vscode/test-electron. import * as assert from 'node:assert'; -import * as path from 'node:path'; import * as vscode from 'vscode'; const PUBLISHER_AND_NAME = 'xtclang.xtc-language'; @@ -28,10 +27,8 @@ suite('Ecstasy file association', () => { }); test('opens .x files with languageId "xtc"', async () => { - // The runTest launcher opens VS Code with src/test/fixtures as the - // workspace folder, so this absolute path resolves cleanly. - const fixture = path.resolve(__dirname, '..', '..', '..', 'src', 'test', 'fixtures', 'hello.x'); - const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(fixture)); + const fixture = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, 'hello.x'); + const doc = await vscode.workspace.openTextDocument(fixture); const langId = await waitForLanguageId(doc, 'xtc'); assert.strictEqual( langId, diff --git a/lang/vscode-extension/src/test/suite/index.ts b/lang/vscode-extension/src/test/suite/index.ts index 2e8c1ae6519..feb7924cbde 100644 --- a/lang/vscode-extension/src/test/suite/index.ts +++ b/lang/vscode-extension/src/test/suite/index.ts @@ -5,6 +5,8 @@ import * as path from 'node:path'; import { glob } from 'glob'; import Mocha from 'mocha'; +import { showTestProgress } from '../progress'; +import { stopOnUnsafeFailure } from '../wait'; export async function run(): Promise { const mocha = new Mocha({ ui: 'tdd', color: true, timeout: 10000 }); @@ -14,12 +16,15 @@ export async function run(): Promise { mocha.addFile(path.resolve(testsRoot, file)); } await new Promise((resolve, reject) => { - mocha.run(failures => { + const runner = mocha.run(failures => { if (failures > 0) { reject(new Error(`${failures} test(s) failed.`)); } else { resolve(); } }); + stopOnUnsafeFailure(runner); + showTestProgress(runner, 'Ecstasy tests', test => + test.title.length <= 48 ? test.title : test.title.slice(0, 45) + '…'); }); } diff --git a/lang/vscode-extension/src/test/suite/library-configuration.test.ts b/lang/vscode-extension/src/test/suite/library-configuration.test.ts new file mode 100644 index 00000000000..0c044973cf6 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/library-configuration.test.ts @@ -0,0 +1,41 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { pathToFileURL } from 'node:url'; +import { inheritedLibraries, normalizeLibraries } from '../../library-configuration'; + +suite('Compiler library configuration', () => { + test('inheritance is distinct from explicitly empty libraries', () => { + assert.deepStrictEqual(normalizeLibraries(undefined, []), inheritedLibraries); + assert.deepStrictEqual(normalizeLibraries({ modulePath: [] }, []), { modulePath: [], sourceAttachments: [] }); + }); + + test('order and attachment association survive normalization', () => { + const base = 'file:///workspace/'; + assert.deepStrictEqual(normalizeLibraries({ modulePath: ['second', 'first'], + sourceAttachments: [{ module: 'Library', roots: ['sources', 'fallback'] }] }, [base]), { + modulePath: ['file:///workspace/second', 'file:///workspace/first'], + sourceAttachments: [{ module: 'Library', roots: ['file:///workspace/sources', 'file:///workspace/fallback'] }] + }); + }); + + test('ambiguous roots aliases remote schemes and malformed attachments are rejected', () => { + assert.throws(() => normalizeLibraries({ modulePath: ['relative'] }, ['file:///one/', 'file:///two/']), /exactly one/); + assert.throws(() => normalizeLibraries({ modulePath: ['sources', './sources'] }, ['file:///workspace/']), /Duplicate/); + assert.throws(() => normalizeLibraries({ modulePath: ['https://example.org/library.xtc'] }, []), /local file/); + assert.throws(() => normalizeLibraries({ sourceAttachments: [{ module: 'Library', roots: [] }] }, []), /ordered roots/); + assert.throws(() => normalizeLibraries({ sourceAttachments: [{ module: '', roots: [] }] }, []), /module name/); + assert.throws(() => normalizeLibraries({ typo: [] }, []), /Unknown/); + }); + + test('settings application validates existing files before persistence', () => { + const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'xtc-libraries-test-')); + const uri = pathToFileURL(directory + path.sep).toString(); + try { + fs.mkdirSync(path.join(directory, 'sources')); + assert.throws(() => normalizeLibraries({ modulePath: ['missing.xtc'] }, [uri], true), /does not exist/); + assert.ok(normalizeLibraries({ modulePath: ['sources'] }, [uri], true).modulePath?.length); + } finally { fs.rmSync(directory, { recursive: true, force: true }); } + }); +}); diff --git a/lang/vscode-extension/src/test/suite/lsp-startup.test.ts b/lang/vscode-extension/src/test/suite/lsp-startup.test.ts index 93b1ebe33c9..c85f84726a8 100644 --- a/lang/vscode-extension/src/test/suite/lsp-startup.test.ts +++ b/lang/vscode-extension/src/test/suite/lsp-startup.test.ts @@ -5,16 +5,15 @@ // adapter selection broken, etc.) — none of which the per-surface tests // above would notice, because they don't exercise actual LSP traffic. // -// Slow by design: starting the LSP server JVM + tree-sitter native lib -// load takes a few seconds even on a warm machine; we poll up to 30s. +// Starting the server JVM and selected adapter takes a few seconds; reads have a 30s deadline. import * as assert from 'node:assert'; -import * as path from 'node:path'; import * as vscode from 'vscode'; +import { connectionSettingsChanged, getClient } from '../../lsp-client'; +import { waitFor } from '../wait'; const PUBLISHER_AND_NAME = 'xtclang.xtc-language'; const STARTUP_TIMEOUT_MS = 30_000; -const POLL_INTERVAL_MS = 250; async function waitForHover(uri: vscode.Uri, position: vscode.Position): Promise { // The built-in `vscode.executeHoverProvider` command returns hovers from @@ -24,20 +23,9 @@ async function waitForHover(uri: vscode.Uri, position: vscode.Position): Promise // hover entry. Polling on this signal is more reliable than scraping the // output channel for "Backend: TreeSitter" because it actually verifies // end-to-end LSP RPC, not just startup logging. - const deadline = Date.now() + STARTUP_TIMEOUT_MS; - let lastResult: vscode.Hover[] = []; - while (Date.now() < deadline) { - lastResult = (await vscode.commands.executeCommand( - 'vscode.executeHoverProvider', - uri, - position, - )) ?? []; - if (lastResult.length > 0) { - return lastResult; - } - await new Promise(resolve => setTimeout(resolve, POLL_INTERVAL_MS)); - } - return lastResult; + return waitFor(async () => (await vscode.commands.executeCommand( + 'vscode.executeHoverProvider', uri, position)) ?? [], + hovers => hovers.length > 0, 'Language server responds to hover', STARTUP_TIMEOUT_MS); } suite('LSP startup', function () { @@ -52,8 +40,8 @@ suite('LSP startup', function () { }); test('LSP server responds to a hover request on hello.x', async () => { - const fixture = path.resolve(__dirname, '..', '..', '..', 'src', 'test', 'fixtures', 'hello.x'); - const doc = await vscode.workspace.openTextDocument(vscode.Uri.file(fixture)); + const fixture = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, 'hello.x'); + const doc = await vscode.workspace.openTextDocument(fixture); await vscode.window.showTextDocument(doc); // Aim the hover at the `console` identifier in `console.print(...)`. @@ -71,7 +59,86 @@ suite('LSP startup', function () { `LSP server did not respond to hover within ${STARTUP_TIMEOUT_MS} ms. ` + 'This usually means the LSP server JVM failed to start ' + '(missing JAR, wrong Java version, tree-sitter native lib not loaded). ' + - 'Check the "XTC Language Server" output channel from a manual run.', + 'Check the "Ecstasy Language Server" output channel from a manual run.', ); }); + + test('an unexpected server exit restarts the existing connection and preserves unsaved text', async () => { + const fixture = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, 'hello.x'); + const document = await vscode.workspace.openTextDocument(fixture); + await vscode.window.showTextDocument(document); + const original = document.getText(); + const offset = original.indexOf('console.print'); + assert.ok(offset > 0, 'hello.x contains console.print'); + const at = document.positionAt(offset); + await waitForHover(fixture, at); + const connection = getClient(); + assert.ok(connection); + assert.ok(connection.isRunning()); + const initialized = connection.initializeResult; + const before = await connection.sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + assert.ok(Number.isSafeInteger(before.pid) && before.pid > 0, 'Only terminate this isolated server'); + try { + const edit = new vscode.WorkspaceEdit(); + edit.insert(fixture, document.positionAt(original.length), '\n// unsaved restart control\n'); + assert.ok(await vscode.workspace.applyEdit(edit)); + const unsaved = document.getText(); + process.kill(before.pid, 'SIGTERM'); + await waitFor(async () => connection.isRunning() && connection.initializeResult !== initialized, + Boolean, 'Automatic restart completes initialization'); + assert.strictEqual(getClient(), connection, 'The installed client owns its automatic restart'); + const after = await connection.sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + assert.notStrictEqual(after.pid, before.pid); + assert.strictEqual(document.getText(), unsaved); + assert.ok((await waitForHover(fixture, at)).length > 0, 'The restarted server analyzes the reopened buffer'); + } finally { + const restore = new vscode.WorkspaceEdit(); + restore.replace(fixture, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), original); + assert.ok(await vscode.workspace.applyEdit(restore)); + } + }); + + test('switching adapters restarts the server and reopens unsaved buffers in both backends', async function () { + this.timeout(150_000); + const fixture = vscode.Uri.joinPath(vscode.workspace.workspaceFolders![0].uri, 'hello.x'); + const document = await vscode.workspace.openTextDocument(fixture); + await vscode.window.showTextDocument(document); + const original = document.getText(); + const settings = vscode.workspace.getConfiguration('xtc'); + const saved = settings.inspect('languageService.adapter')?.workspaceValue; + await waitFor(async () => getClient()?.isRunning() ?? false, Boolean, 'Initial server running'); + const insertion = original.indexOf('@Inject'); + assert.ok(insertion >= 0, 'hello.x contains an injected property'); + const edit = new vscode.WorkspaceEdit(); + edit.insert(fixture, document.positionAt(insertion), 'Int adapterSwitchProof = 42;\n '); + assert.ok(await vscode.workspace.applyEdit(edit)); + const unsaved = document.getText(); + const names = (symbols: vscode.DocumentSymbol[]): string[] => symbols.flatMap(symbol => [symbol.name, ...names(symbol.children ?? [])]); + try { + for (const [choice, expected] of [['treesitter', 'TreeSitter'], ['compiler', 'XDK']]) { + const previous = getClient(); + const before = await previous!.sendRequest<{ pid: number }>('xtc/languageServiceStatus'); + await vscode.commands.executeCommand('xtc.selectLanguageAdapter', choice); + const current = await waitFor(async () => getClient(), value => !!value && value !== previous && value.isRunning(), `Restart into ${choice}`); + const status = await current!.sendRequest<{ adapter: string; pid: number }>('xtc/languageServiceStatus'); + assert.strictEqual(status.adapter, expected); + assert.notStrictEqual(status.pid, before.pid); + assert.strictEqual(document.getText(), unsaved); + const symbols = await waitFor(async () => (await vscode.commands.executeCommand('vscode.executeDocumentSymbolProvider', fixture)) ?? [], + value => names(value).includes('adapterSwitchProof'), `Unsaved symbol available in ${choice}`); + assert.ok(names(symbols).includes('adapterSwitchProof')); + await vscode.commands.executeCommand('xtc.selectLanguageAdapter', choice); + assert.strictEqual(getClient(), current, 'Selecting the saved adapter again leaves the connection intact'); + } + const current = getClient(); + await assert.rejects(async () => vscode.commands.executeCommand('xtc.selectLanguageAdapter', 'invalid'), /adapter/); + assert.strictEqual(getClient(), current, 'An invalid selection leaves the working connection intact'); + } finally { + const restore = new vscode.WorkspaceEdit(); + restore.replace(fixture, new vscode.Range(document.positionAt(0), document.positionAt(document.getText().length)), original); + assert.ok(await vscode.workspace.applyEdit(restore)); + await settings.update('languageService.adapter', saved, vscode.ConfigurationTarget.Workspace); + await waitFor(async () => !connectionSettingsChanged() && (getClient()?.isRunning() ?? false), Boolean, 'Restore original adapter'); + } + }); }); diff --git a/lang/vscode-extension/src/test/suite/playbook-wait.test.ts b/lang/vscode-extension/src/test/suite/playbook-wait.test.ts new file mode 100644 index 00000000000..7ad57193727 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/playbook-wait.test.ts @@ -0,0 +1,37 @@ +import * as assert from 'node:assert'; +import Mocha from 'mocha'; +import { PlaybookCleanupFailure, PlaybookTimeout, stopOnUnsafeFailure, waitFor } from '../wait'; + +suite('Playbook timeout isolation', () => { + test('condition timeout fails with its last observation', async () => { + await assert.rejects(waitFor(async () => 'still visible', value => value === 'gone', 'Hide hints', 10), + error => error instanceof PlaybookTimeout && error.message.includes('still visible')); + }); + + test('a read that never replies is bounded too', async () => { + await assert.rejects(waitFor(() => new Promise(() => {}), Boolean, 'Provider reply', 10), PlaybookTimeout); + }); + + test('an explicit refusal stays distinct from success and timeout', async () => { + const refusal = new Error('Unsupported operation'); + await assert.rejects(waitFor(async () => { throw refusal; }, Boolean, 'Refusal', 100), error => error === refusal); + assert.strictEqual(await waitFor(async () => 'ready', value => value === 'ready', 'Success', 100), 'ready'); + }); + + test('Mocha cannot start another case after a timeout or cleanup failure', async () => { + for (const operation of [ + () => new Promise(() => {}), + async () => { throw new PlaybookTimeout('Unfinished editor command'); }, + async () => { throw Object.assign(new Error('Renderer never replied'), { name: 'TimeoutError' }); }, + async () => { throw new PlaybookCleanupFailure([new Error('Could not restore settings')]); } + ]) { + const mocha = new Mocha({ reporter: class extends Mocha.reporters.Base {}, timeout: 10 }); + let followingRan = false; + mocha.suite.addTest(new Mocha.Test('unsafe case', operation)); + mocha.suite.addTest(new Mocha.Test('following case', () => { followingRan = true; })); + const failures = await new Promise(resolve => stopOnUnsafeFailure(mocha.run(resolve))); + assert.strictEqual(failures, 1); + assert.strictEqual(followingRan, false); + } + }); +}); diff --git a/lang/vscode-extension/src/test/suite/runtime-settings.test.ts b/lang/vscode-extension/src/test/suite/runtime-settings.test.ts new file mode 100644 index 00000000000..57c9eb23327 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/runtime-settings.test.ts @@ -0,0 +1,16 @@ +import * as assert from 'node:assert'; +import { validateJvmOptions, validateLogRetention } from '../../runtime-settings'; + +suite('Machine-local JVM options', () => { + test('supported tuning remains separate arguments', () => { + const options = ['-Xms256M', '-Xmx2G', '-Xss1M', '-XX:ActiveProcessorCount=4', '-XX:+UseG1GC']; + assert.deepStrictEqual(validateJvmOptions(options), options); + }); + test('launch overrides conflicting collectors and impossible heaps are rejected', () => { + for (const options of [['-jar'], ['@options'], ['-Dxtc.adapter=mock'], ['-javaagent:agent.jar'], ['-Xmx1G -Xss1M'], ['-Xmx2G', '-Xmx1G'], ['-Xms2G', '-Xmx1G'], ['-XX:+UseG1GC', '-XX:+UseZGC'], ['-Xmx999999999999999999999G']]) assert.throws(() => validateJvmOptions(options)); + }); + test('retention rejects malformed values and impossible caps', () => { + assert.strictEqual(validateLogRetention({ historyDays: 2 }).historyDays, 2); + for (const value of [null, [], { historyDays: 0 }, { historyDays: 1.5 }, { totalSizeMb: 1 }, { retainedSessions: 21 }, { typo: 2 }]) assert.throws(() => validateLogRetention(value)); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/service-settings.test.ts b/lang/vscode-extension/src/test/suite/service-settings.test.ts new file mode 100644 index 00000000000..62f006cb93e --- /dev/null +++ b/lang/vscode-extension/src/test/suite/service-settings.test.ts @@ -0,0 +1,20 @@ +import * as assert from 'node:assert'; +import { parseServiceSettings, synchronizationOptions } from '../../service-settings'; + +suite('Language service settings contract', () => { + test('defaults remain Full synchronization and editor-owned saving', () => { + const settings = parseServiceSettings({}); + assert.deepStrictEqual(settings, { adapter: 'default', textSynchronization: 'full', saveFormatting: 'editor', inlayHints: true }); + assert.ok(Object.isFrozen(settings)); + }); + test('invalid values never become a connection configuration', () => { + for (const raw of [{ adapter: 'mock' }, { adapter: null }, { adapter: 'typo' }, { textSynchronization: 'patch' }, { saveFormatting: true }, { inlayHints: 'false' }, { textSynchronization: null }]) { + assert.throws(() => parseServiceSettings(raw)); + } + }); + test('native save formatting takes precedence over the server hook', () => { + const settings = parseServiceSettings({ textSynchronization: 'incremental', saveFormatting: 'server' }); + assert.deepStrictEqual(synchronizationOptions(settings, false), { incremental: true, formatOnSave: true }); + assert.deepStrictEqual(synchronizationOptions(settings, true), { incremental: true, formatOnSave: false }); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/settings-report.test.ts b/lang/vscode-extension/src/test/suite/settings-report.test.ts new file mode 100644 index 00000000000..cfc37793ca5 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/settings-report.test.ts @@ -0,0 +1,42 @@ +import * as assert from 'node:assert'; +import * as vscode from 'vscode'; +import { formattingSettings } from '../../editor-settings'; +import { configuredSettings, configurationProperties } from '../../settings-report'; + +suite('Settings consumers and reporting', () => { + test('every settings group is included in the report', () => { + const groups = vscode.extensions.getExtension('xtclang.xtc-language')!.packageJSON.contributes.configuration; + assert.strictEqual(groups.length, 4); + assert.ok(configurationProperties(groups)['xtc.java.vmOptions']); + assert.ok(configurationProperties(groups)['xtc.compiler.libraries']); + assert.deepStrictEqual(configurationProperties({ properties: { legacy: {} } }), { legacy: {} }); + }); + + test('the formatter receives the configured wrapping margin and rejects invalid margins', async () => { + const settings = vscode.workspace.getConfiguration('xtc'); + const previous = settings.inspect('formatting.maxLineWidth')?.globalValue; + try { + await settings.update('formatting.maxLineWidth', 73, vscode.ConfigurationTarget.Global); + assert.strictEqual((formattingSettings() as { maxLineWidth: number }).maxLineWidth, 73); + await settings.update('formatting.maxLineWidth', 0, vscode.ConfigurationTarget.Global); + assert.throws(formattingSettings, /maxLineWidth/); + } finally { + await settings.update('formatting.maxLineWidth', previous, vscode.ConfigurationTarget.Global); + } + }); + + test('machine reports use the same normalized values as the launcher', async () => { + const settings = vscode.workspace.getConfiguration('xtc'); + const previous = settings.inspect('server.logs')?.globalValue; + try { + await settings.update('server.logs', { historyDays: 2 }, vscode.ConfigurationTarget.Global); + const report = configuredSettings({ 'xtc.server.logs': { scope: 'machine' } }); + assert.deepStrictEqual(report, { 'server.logs': { + value: { historyDays: 2, maxFileMb: 10, totalSizeMb: 50, retainedSessions: 5 }, + origin: 'user', scope: 'machine' + } }); + } finally { + await settings.update('server.logs', previous, vscode.ConfigurationTarget.Global); + } + }); +}); diff --git a/lang/vscode-extension/src/test/suite/source-graph-configuration.test.ts b/lang/vscode-extension/src/test/suite/source-graph-configuration.test.ts new file mode 100644 index 00000000000..a5fc34bd4b7 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/source-graph-configuration.test.ts @@ -0,0 +1,68 @@ +import * as assert from 'node:assert'; +import { applyEdits } from 'jsonc-parser'; +import { SourceModule, sourceGraphEdits, sourceGraphKey, sourceModulesIn } from '../../source-graph-configuration'; + +suite('Rename settings boundaries', () => { + const before: SourceModule[] = [{ name: 'Library', uri: 'file:///libraries/Library.x' }, + { name: 'Consumer', uri: 'file:///application/Consumer.x', dependencies: ['Library'] }]; + const after: SourceModule[] = [{ name: 'Renamed', uri: 'file:///libraries/Renamed.x' }, + { name: 'Consumer', uri: 'file:///application/Consumer.x', dependencies: ['Renamed'] }]; + const path = ['settings', 'xtc.compiler.sourceModules']; + const text = `{ + // Preserve the workspace, not just its compiler graph. + "folders": [{"path": "../libraries"}, {"path": "../application"}], + "settings": {"editor.tabSize": 8, "xtc.compiler.sourceModules": ${JSON.stringify(before)},}, + }`; + + test('resource roots preserve precedence and are guarded through rename history', () => { + const original = [{ ...before[0], resourceRoots: ['file:///custom/', 'file:///fallback/'] }]; + const renamed = [{ ...after[0], resourceRoots: original[0].resourceRoots }]; + const settings = JSON.stringify({ settings: { 'xtc.compiler.sourceModules': original } }); + const edited = applyEdits(settings, sourceGraphEdits(settings, path, original, renamed)); + assert.deepStrictEqual(sourceModulesIn(edited, path), renamed); + assert.notStrictEqual(sourceGraphKey(original), sourceGraphKey([{ ...original[0], resourceRoots: [...original[0].resourceRoots].reverse() }])); + assert.notStrictEqual(sourceGraphKey([{ ...before[0], resourceRoots: [] }]), sourceGraphKey([before[0]])); + assert.strictEqual(sourceGraphKey([{ ...before[0], resourceRoots: null }]), sourceGraphKey([before[0]])); + assert.throws(() => sourceGraphEdits(settings, path, [{ ...before[0], resourceRoots: [] }], renamed), /graph changed/); + assert.throws(() => sourceGraphKey([{ ...before[0], resourceRoots: ['file:///custom/', 'file:///custom/'] }]), /Duplicate/); + }); + + test('saved multi-root settings preserve folders, comments and unrelated preferences through history', () => { + const changed = applyEdits(text, sourceGraphEdits(text, path, before, after)); + assert.deepStrictEqual(sourceModulesIn(changed, path), after); + assert.ok(changed.includes('// Preserve the workspace')); + assert.ok(changed.includes('"folders": [{"path": "../libraries"}, {"path": "../application"}]')); + const independentlyEdited = changed.replace('"editor.tabSize": 8', '"editor.tabSize": 2'); + const undone = applyEdits(independentlyEdited, sourceGraphEdits(independentlyEdited, path, after, before)); + assert.deepStrictEqual(sourceModulesIn(undone, path), before); + assert.ok(undone.includes('"editor.tabSize": 2')); + assert.deepStrictEqual(sourceModulesIn(applyEdits(undone, sourceGraphEdits(undone, path, before, after)), path), after); + }); + + test('late proposals and history cannot overwrite independently changed dependency edges', () => { + const changed = text.replace('"dependencies":["Library"]', '"dependencies":[]'); + assert.throws(() => sourceGraphEdits(changed, path, before, after), /graph changed/); + assert.throws(() => sourceGraphEdits(text, path, after, before), /graph changed/); + assert.deepStrictEqual(sourceModulesIn(text, path), before); + }); + + test('global-only or folder-only graphs cannot be materialized into workspace settings by rename', () => { + assert.throws(() => sourceGraphEdits('{"settings":{}}', path, before, after), /explicit source graph/); + assert.throws(() => sourceGraphEdits('{"xtc.compiler.sourceModules":null}', ['xtc.compiler.sourceModules'], before, after), /explicit source graph/); + }); + + test('relative roots require a single unambiguous workspace base', () => { + const relative = [{ name: 'Library', uri: 'nested/../Library.x' }]; + assert.strictEqual(sourceGraphKey(relative, 'file:///libraries/'), sourceGraphKey([before[0]])); + assert.throws(() => sourceGraphKey(relative)); + }); + + test('malformed graphs, duplicate identities and unsupported URI schemes are refused', () => { + [null, {}, [null], [{ name: 1, uri: 'file:///Library.x' }], + [{ name: 'Library', uri: 'https://example.org/Library.x' }], + [{ ...before[0], dependencies: [1] }], [before[0], before[0]], + [before[0], { ...before[0], name: 'Different' }]].forEach(graph => assert.throws(() => sourceGraphKey(graph))); + assert.throws(() => sourceGraphEdits('{', path, before, after), /settings JSON/); + assert.throws(() => sourceGraphEdits(text, path, before, [after[0], after[0]]), /Duplicate/); + }); +}); diff --git a/lang/vscode-extension/src/test/suite/support-logs.test.ts b/lang/vscode-extension/src/test/suite/support-logs.test.ts new file mode 100644 index 00000000000..f7025e5eff0 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/support-logs.test.ts @@ -0,0 +1,53 @@ +import * as assert from 'node:assert'; +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import type { Memento } from 'vscode'; +import { offlineArchive, SupportLogs } from '../../support-logs'; +import { readArchive } from '../archive'; + +suite('Offline project support logs', () => { + test('retired callbacks cannot overwrite a later launch and recorded failures survive reopening', async () => { + const values = new Map(); + const storage: Memento = { + keys: () => [...values.keys()], + get: (key: string, fallback?: T) => values.get(key) as T ?? fallback as T, + update: async (key, value) => { values.set(key, value); } + }; + const logs = new SupportLogs(storage); + const retired = logs.begin('OLD'); + const current = logs.begin('NEW'); + logs.finish(retired, 'STALE FAILURE'); + logs.finish(current, ' CURRENT FAILURE'); + const archive = await readArchive(await new SupportLogs(storage).export()); + assert.strictEqual(archive.get('launcher.log')?.toString(), 'NEW CURRENT FAILURE'); + assert.strictEqual(archive.size, 2); + }); + + test('export is bounded excludes source files symlinks and other projects and tolerates pruning', async () => { + const directory = await fs.mkdtemp(path.join(os.tmpdir(), 'ecstasy-support-')); + try { + const own = path.join(directory, 'server-123-456'); + const other = path.join(directory, 'server-789-456'); + const traces = path.join(directory, 'traces'); + await Promise.all([own, other, traces].map(root => fs.mkdir(root))); + await fs.writeFile(path.join(own, 'server.log'), 'x'.repeat(700_000) + 'TAIL'); + await fs.writeFile(path.join(other, 'server.log'), 'OTHER PROJECT'); + await fs.writeFile(path.join(own, 'Main.x'), 'SOURCE CONTENT'); + await fs.symlink(path.join(other, 'server.log'), path.join(own, 'server.1.log')); + await fs.writeFile(path.join(traces, 'lsp-trace-123-456.jsonl'), 'OWN TRACE'); + await fs.writeFile(path.join(traces, 'lsp-trace-789-456.jsonl'), 'OTHER TRACE'); + const session = { id: 'owned', started: '', launcher: 'FAILED LAUNCH', logs: { directory: own, traceDirectory: traces } }; + const entries = await readArchive(await offlineArchive(session)); + assert.strictEqual(entries.size, 4); + const log = [...entries].find(([name]) => name.endsWith('server.log'))![1].toString(); + assert.strictEqual(log.length, 512 * 1024); + assert.ok(log.endsWith('TAIL')); + assert.doesNotMatch([...entries.values()].join(''), /OTHER PROJECT|OTHER TRACE|SOURCE CONTENT/); + assert.ok(JSON.parse(entries.get('manifest.json')!.toString()).files.some((file: { truncated: boolean }) => file.truncated)); + await fs.rm(own, { recursive: true }); + const pruned = await readArchive(await offlineArchive({ ...session, logs: { directory: own, traceDirectory: own } })); + assert.strictEqual(pruned.size, 2); + } finally { await fs.rm(directory, { recursive: true, force: true }); } + }); +}); diff --git a/lang/vscode-extension/src/test/vscodeCache.ts b/lang/vscode-extension/src/test/vscodeCache.ts index 9b0f8d9e0db..7b47182a537 100644 --- a/lang/vscode-extension/src/test/vscodeCache.ts +++ b/lang/vscode-extension/src/test/vscodeCache.ts @@ -4,11 +4,12 @@ // so every checkout and worktree kept its own copy of every version it ever tested, and each new // stable release added another ~500 MB that nothing removed. The builds now live in one per-user // cache, one copy per version, and builds no test run has used for PRUNE_AFTER_DAYS are removed. -// The throwaway test profile (user data, extensions) stays in the checkout's .vscode-test/. +// Test profiles are separate temporary directories; only immutable IDE builds are reused. -import { mkdirSync, readdirSync, rmSync, statSync, utimesSync } from 'node:fs'; +import { mkdirSync, readFileSync, readdirSync, rmSync, statSync, utimesSync } from 'node:fs'; import * as os from 'node:os'; import * as path from 'node:path'; +import { downloadAndUnzipVSCode } from '@vscode/test-electron'; const PRUNE_AFTER_DAYS = 30; const DAY_MS = 24 * 60 * 60 * 1000; @@ -16,6 +17,26 @@ const DAY_MS = 24 * 60 * 60 * 1000; const STALE_LOCK_MS = 15 * 60 * 1000; const LOCK_POLL_MS = 1000; +/** Smoke, playbook and lifecycle tests all run once on the declared minimum supported IDE. */ +export async function testVSCodeBuild(extensionRoot: string): Promise<{ version: string; executable: string }> { + const manifest = JSON.parse(readFileSync(path.join(extensionRoot, 'package.json'), 'utf8')) as { + engines: { vscode: string }; + }; + const version = manifest.engines.vscode.replace(/^\^/, ''); + const cachePath = sharedCachePath(); + for (const build of removeCheckoutBuilds(path.join(extensionRoot, '.vscode-test'))) { + console.log(`[vscode-test] Removed ${build}: builds now live in ${cachePath}`); + } + const executable = await withDownloadLock(cachePath, async () => { + const executable = await downloadAndUnzipVSCode({ version, cachePath }); + for (const build of markUsedAndPrune(cachePath, [buildDirectory(cachePath, executable)])) { + console.log(`[vscode-test] Removed ${build}: unused for 30 days`); + } + return executable; + }); + return { version, executable }; +} + /** The per-user VS Code build cache, or XTC_VSCODE_TEST_CACHE when set. */ export function sharedCachePath(): string { const override = process.env.XTC_VSCODE_TEST_CACHE; diff --git a/lang/vscode-extension/src/test/wait.ts b/lang/vscode-extension/src/test/wait.ts new file mode 100644 index 00000000000..16f17c99aee --- /dev/null +++ b/lang/vscode-extension/src/test/wait.ts @@ -0,0 +1,36 @@ +import { setTimeout as delay } from 'node:timers/promises'; +import type Mocha from 'mocha'; + +export const PLAYBOOK_WAIT_MS = 30_000; + +/** A timeout means the editor operation may still be running; never start another case. */ +export class PlaybookTimeout extends Error {} +export class PlaybookCleanupFailure extends Error { + constructor(readonly failures: readonly unknown[]) { super(`Playbook cleanup failed: ${failures.map(String).join('; ')}`); } +} + +export function mustStopPlaybook(error: unknown): boolean { + return error instanceof PlaybookTimeout || error instanceof PlaybookCleanupFailure || + (error instanceof Error && error.name === 'TimeoutError') || + (error as { code?: string } | undefined)?.code === 'ERR_MOCHA_TIMEOUT'; +} + +export function stopOnUnsafeFailure(runner: Mocha.Runner): void { + runner.on('fail', (_test, error) => { if (mustStopPlaybook(error)) runner.abort(); }); +} + +/** Bounds both polling and a single read that never settles. The caller must stop on timeout. */ +export async function waitFor(read: () => Promise, accept: (value: T) => boolean, message: string, timeoutMs = PLAYBOOK_WAIT_MS): Promise { + let last: T | undefined; + const timer = new AbortController(); + const deadline = delay(timeoutMs, undefined, { signal: timer.signal }).then(() => { + throw new PlaybookTimeout(`${message} timed out after ${timeoutMs} ms; last value: ${JSON.stringify(last)}`); + }); + try { + for (;;) { + last = await Promise.race([Promise.resolve().then(read), deadline]); + if (accept(last)) return last; + await Promise.race([delay(75), deadline]); + } + } finally { timer.abort(); } +} diff --git a/lang/vscode-extension/src/test/workbenchUi.ts b/lang/vscode-extension/src/test/workbenchUi.ts new file mode 100644 index 00000000000..c1fb2340d6d --- /dev/null +++ b/lang/vscode-extension/src/test/workbenchUi.ts @@ -0,0 +1,62 @@ +import * as assert from 'node:assert'; +import * as path from 'node:path'; +import { createServer } from 'node:net'; +import { chromium, Browser, Page } from 'playwright-core'; + +/** Test-only attachment to the isolated Electron renderer; never controls the desktop pointer. */ +export class WorkbenchUi { + private constructor(private readonly browser: Browser, readonly page: Page) {} + + static async connect(): Promise { + const port = Number(process.env.XTC_PLAYBOOK_UI_PORT); + assert.ok(Number.isInteger(port) && port > 0, 'UI assertions require the isolated launcher endpoint'); + const browser = await chromium.connectOverCDP(`http://127.0.0.1:${port}`, { timeout: 15_000 }); + try { + const pages = browser.contexts().flatMap(context => context.pages()) + .filter(page => page.url().includes('/workbench/')); + assert.strictEqual(pages.length, 1, 'Exactly one workbench belongs to this isolated test instance'); + return new WorkbenchUi(browser, pages[0]); + } catch (error) { await browser.close(); throw error; } + } + + async screenshot(name: string): Promise { + await this.page.screenshot({ path: path.join(process.env.XTC_PLAYBOOK_REPORT_DIR!, `${name}.png`) }); + } + + async cancelReferences(): Promise { + const notification = this.page.locator('.notification-list-item').filter({ hasText: 'Finding references' }); + const button = notification.getByRole('button', { name: 'Cancel', exact: true }); + await button.waitFor({ state: 'visible', timeout: 10_000 }); + await this.screenshot('X145-before-visible-cancel'); + // Trusted renderer input activates the actual workbench control, once. No token.cancel fallback. + await button.click({ timeout: 5_000 }); + } + + async inlayLabels(): Promise { + return this.page.locator('.monaco-editor:visible .view-lines [class*="dyn-rule-"]').allTextContents(); + } + + async focusEditor(): Promise { + // Focus the actual text input rather than an Output panel left open by a preceding case. + // DOM focus does not move the desktop pointer and never repeats an edit/history command. + const input = this.page.locator('.editor-group-container.active .monaco-editor:visible :is(textarea.inputarea, .native-edit-context)'); + await input.focus({ timeout: 5_000 }); + assert.ok(await input.evaluate(element => element === element.ownerDocument.activeElement), 'Source editor owns keyboard focus'); + } + + async inlineText(): Promise { + return (await this.page.locator('.editor-group-container.active .monaco-editor:visible .ghost-text-decoration').allTextContents()).join(''); + } + + async close(): Promise { await this.browser.close(); } +} + +/** Let the OS allocate a loopback port; fail if another process wins the short launch race. */ +export async function workbenchPort(): Promise { + const server = createServer(); + await new Promise((resolve, reject) => { server.once('error', reject); server.listen(0, '127.0.0.1', resolve); }); + const address = server.address(); + assert.ok(address && typeof address !== 'string'); + await new Promise((resolve, reject) => server.close(error => error ? reject(error) : resolve())); + return address.port; +} diff --git a/lang/vscode-extension/tsconfig.json b/lang/vscode-extension/tsconfig.json index e72f036c21f..ab390790bbd 100644 --- a/lang/vscode-extension/tsconfig.json +++ b/lang/vscode-extension/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { - "module": "Node16", - "moduleResolution": "Node16", + "module": "NodeNext", + "moduleResolution": "NodeNext", "target": "ES2020", "lib": ["ES2020"], "types": ["node", "mocha", "vscode"], @@ -10,6 +10,7 @@ "sourceMap": true, "strict": true, "esModuleInterop": true, + "resolveJsonModule": true, "skipLibCheck": true, "forceConsistentCasingInFileNames": true }, diff --git a/xdk/build.gradle.kts b/xdk/build.gradle.kts index a5b86c2db55..e72fa0be47b 100644 --- a/xdk/build.gradle.kts +++ b/xdk/build.gradle.kts @@ -123,28 +123,7 @@ dependencies { testImplementation(libs.javatools.utils) testEcstasyModule(libs.xdk.ecstasy) xdkJavaToolsJitBridge(libs.javatools.jitbridge) - xtcModule(libs.xdk.ecstasy) - xtcModule(libs.xdk.aggregate) - xtcModule(libs.xdk.cli) - xtcModule(libs.xdk.collections) - xtcModule(libs.xdk.convert) - xtcModule(libs.xdk.crypto) - xtcModule(libs.xdk.json) - xtcModule(libs.xdk.jsondb) - xtcModule(libs.xdk.metrics) - xtcModule(libs.xdk.net) - xtcModule(libs.xdk.oodb) - xtcModule(libs.xdk.runner) - xtcModule(libs.xdk.sec) - xtcModule(libs.xdk.web) - xtcModule(libs.xdk.webauth) - xtcModule(libs.xdk.webcli) - xtcModule(libs.xdk.xenia) - xtcModule(libs.xdk.xml) - xtcModule(libs.xdk.xunit) - xtcModule(libs.xdk.xunit.db) - xtcModule(libs.xdk.xunit.engine) - xtcModule(libs.javatools.bridge) + xtcModule(libs.bundles.xdk.libraries) xtcLauncherBinaries(project(path = ":javatools-launcher", configuration = "xtcLauncherBinaries")) } From 1e4c0a869569993d77da823bc3ee7cd4106a7f4c Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:54:56 +0100 Subject: [PATCH 009/324] Report repository read attempts without inferring dependency failures - Report repository read attempts without inferring dependency failures. - Keep repository read observations separate from compiler diagnostics. - Align repository candidate constructor assignments. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../java/org/xvm/api/EmbeddingSupport.java | 18 --- .../main/java/org/xvm/api/ModuleCompiler.java | 27 ---- .../main/java/org/xvm/asm/DirRepository.java | 89 +++++++----- .../main/java/org/xvm/asm/FileRepository.java | 66 ++++++--- .../java/org/xvm/asm/LinkedRepository.java | 12 -- .../java/org/xvm/asm/ModuleRepository.java | 20 --- .../org/xvm/asm/DirRepositoryFailureTest.java | 85 ++++++----- .../xvm/asm/FileRepositoryFailureTest.java | 65 ++++++--- .../lsp/adapter/EmbeddingDiagnosticsTest.kt | 17 ++- .../lsp/adapter/RepositoryReportingTest.kt | 136 ++++++++++++++++++ 10 files changed, 344 insertions(+), 191 deletions(-) create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index db90fe60b8d..0b9247afcc3 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -1042,24 +1042,6 @@ protected int compile(List compilers, ModuleRepository repoLib) { throw new IllegalStateException("This method must not be called"); } - @Override - protected void prelinkSystemLibraries(ModuleRepository repository) { - try { - super.prelinkSystemLibraries(repository); - } catch (LauncherException e) { - ModuleCompiler.reportRepositoryReadFailures(repository, m_errors); - throw e; - } - } - - @Override - public void log(ErrorInfo error) { - if (Compiler.MODULE_MISSING.equals(error.getCode())) { - ModuleCompiler.reportRepositoryReadFailures(ensureLibraryRepo(), m_errors); - } - super.log(error); - } - @Override protected ModuleRepository configureLibraryRepo(List ignore) { BuildRepository build = new BuildRepository(); diff --git a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java index 99fa748e2c9..c5e4d559394 100644 --- a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java +++ b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java @@ -146,15 +146,6 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu return true; } - /** - * Forward retained read issues only after the compiler has established a dependency failure. - * Successful fallback and speculative repository misses must not become source errors. - */ - static void reportRepositoryReadFailures(ModuleRepository repository, ErrorListener errs) { - repository.getReadFailures().forEach((file, cause) -> errs.error(ERR_INTERNAL, NOWHERE, - cause, "Dependency search skipped unreadable file " + file)); - } - /** * Adapter that supplies the source and repositories to the standard compiler pipeline and * captures its single compiled module instead of writing it to disk. @@ -240,24 +231,6 @@ private int flushAndCheckErrors(ErrorList errsModule, String context) { return checkErrors(context); } - @Override - protected void prelinkSystemLibraries(ModuleRepository repository) { - try { - super.prelinkSystemLibraries(repository); - } catch (LauncherException e) { - reportRepositoryReadFailures(repository, m_errors); - throw e; - } - } - - @Override - public void log(ErrorInfo error) { - if (Compiler.MODULE_MISSING.equals(error.getCode())) { - reportRepositoryReadFailures(ensureLibraryRepo(), m_errors); - } - super.log(error); - } - /** * The build repository comes first: it receives the module being compiled and, as the * launcher's does, a copy of each module read from the others. The caller's modules are diff --git a/javatools/src/main/java/org/xvm/asm/DirRepository.java b/javatools/src/main/java/org/xvm/asm/DirRepository.java index aea8d3b2d59..0112788e980 100644 --- a/javatools/src/main/java/org/xvm/asm/DirRepository.java +++ b/javatools/src/main/java/org/xvm/asm/DirRepository.java @@ -16,10 +16,13 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeMap; +import java.util.function.BiConsumer; + /** * A simple ModuleRepository that manages its contents in a directory. */ @@ -50,10 +53,29 @@ public class DirRepository * @param fReadOnly true to make the repository "read-only" */ public DirRepository(File dir, boolean fReadOnly) { + this(dir, fReadOnly, DirRepository::reportReadFailure); + } + + /** + * Construct a repository with an observer for unsuccessful candidate reads. + * + *

The observer receives the candidate file and original I/O exception synchronously for + * each failed read attempt, including an unsupported binary version. It replaces the default + * console report; scanning continues and other files or repositories may supply a module. + * Cached absence produces no report. A candidate can be unrelated to the requested module, + * and a report does not identify a failed write, corruption or the file's module identity. + * Write failures continue to be thrown by {@link #storeModule(ModuleStructure)}. + * + * @param dir the directory that contains the repository contents + * @param fReadOnly true to make the repository read-only + * @param onReadFailure the non-null observer; it must not reenter this repository + */ + public DirRepository(File dir, boolean fReadOnly, BiConsumer onReadFailure) { assert dir != null && dir.isDirectory(); - m_dir = dir; - m_fRO = fReadOnly; + m_dir = dir; + m_fRO = fReadOnly; + m_onReadFailure = Objects.requireNonNull(onReadFailure); } // ----- accessors ----------------------------------------------------------------------------- @@ -80,17 +102,6 @@ public synchronized Set getModuleNames() { return Collections.unmodifiableSet(modulesByName.keySet()); } - @Override - public synchronized Map getReadFailures() { - Map failures = new HashMap<>(); - for (ModuleInfo info : modulesByFile.values()) { - if (info.readFailure != null) { - failures.put(info.file, info.readFailure); - } - } - return Map.copyOf(failures); - } - @Override public synchronized ModuleStructure loadModule(String sModule) { ensureCache(); @@ -220,7 +231,7 @@ private void clearCache() { * @return the module information */ protected ModuleInfo createModuleInfo(File file) { - return new ModuleInfo(file); + return new ModuleInfo(file, m_onReadFailure); } /** @@ -262,7 +273,7 @@ private Map readCache() { String name = err ? null : in.readUTF(); if (modulesByFile.put(file, - new ModuleInfo(file, name, timestamp, size, err)) != null) { + new ModuleInfo(file, name, timestamp, size, err, m_onReadFailure)) != null) { return null; } } @@ -356,7 +367,6 @@ private File getCacheFile() { */ private boolean isCacheValid() { // Failed candidates are retried once per second, or immediately when metadata changes. - // Retaining their cause avoids treating the intervening cached result as unexplained absence. boolean recent = System.currentTimeMillis() < lastScan + 1000; boolean failed = modulesByFile.values().stream().anyMatch(ModuleInfo::hasReadFailure); if (recent && !failed) { @@ -382,9 +392,14 @@ private boolean isCacheValid() { protected static class ModuleInfo { public ModuleInfo(File file) { - this.file = file; - this.timestamp = file.lastModified(); - this.size = file.length(); + this(file, DirRepository::reportReadFailure); + } + + private ModuleInfo(File file, BiConsumer onReadFailure) { + this.onReadFailure = onReadFailure; + this.file = file; + this.timestamp = file.lastModified(); + this.size = file.length(); ModuleStructure module = tryLoad(); if (module == null) { @@ -398,29 +413,32 @@ public ModuleInfo(File file) { } } - private ModuleInfo(File file, String name, long timestamp, long size, boolean err) { - this.file = file; - this.name = name; - this.versions = null; - this.timestamp = timestamp; - this.size = size; - this.err = err; + private ModuleInfo(File file, String name, long timestamp, long size, boolean err, + BiConsumer onReadFailure) { + this.onReadFailure = onReadFailure; + this.file = file; + this.name = name; + this.versions = null; + this.timestamp = timestamp; + this.size = size; + this.err = err; } ModuleStructure tryLoad() { try { FileStructure struct = new FileStructure(file); - readFailure = null; + readFailed = false; return struct.getModule(); } catch (IOException e) { - readFailure = e; + readFailed = true; + onReadFailure.accept(file, e); return null; } } boolean hasReadFailure() { - // A failed entry read from the disk cache must be retried to recover its cause. - return err || readFailure != null; + // A failed entry read from the disk cache must be retried, even with unchanged metadata. + return err || readFailed; } ModuleStructure ensureModule() { @@ -448,8 +466,14 @@ ModuleStructure ensureModule() { */ private transient ModuleStructure module; - /** The original cause, retained separately from available modules. */ - private IOException readFailure; + /** Whether a payload read failed after the header was cached. */ + private boolean readFailed; + + private final BiConsumer onReadFailure; + } + + private static void reportReadFailure(File file, IOException cause) { + System.out.println("Error reading module file " + file + ": " + cause); } // ----- constants ----------------------------------------------------------------------------- @@ -469,6 +493,7 @@ ModuleStructure ensureModule() { private final File m_dir; private final boolean m_fRO; + private final BiConsumer m_onReadFailure; private Map modulesByFile = new HashMap<>(); private final Map modulesByName = new TreeMap<>(); diff --git a/javatools/src/main/java/org/xvm/asm/FileRepository.java b/javatools/src/main/java/org/xvm/asm/FileRepository.java index 88d46d88da2..3e1e5b16da4 100644 --- a/javatools/src/main/java/org/xvm/asm/FileRepository.java +++ b/javatools/src/main/java/org/xvm/asm/FileRepository.java @@ -5,9 +5,12 @@ import java.util.HashMap; import java.util.Map; +import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.function.BiConsumer; + import org.xvm.asm.FileStructure.FileInfo; import org.xvm.asm.constants.ModuleConstant; @@ -33,10 +36,29 @@ public class FileRepository * @param readOnly true to make the repository "read-only" */ public FileRepository(File file, boolean readOnly) { + this(file, readOnly, (candidate, cause) -> + System.out.println("Error reading module file " + candidate + ": " + cause)); + } + + /** + * Construct a repository with an observer for unsuccessful file reads. + * + *

The observer receives the candidate file and original I/O exception synchronously for + * each failed read attempt, including an unsupported binary version. It replaces the default + * console report; it does not change lookup or fallback. Cached absence produces no report. + * A report establishes neither the file's module identity nor why its contents became unreadable. + * Write failures continue to be thrown by {@link #storeModule(ModuleStructure)}. + * + * @param file the file that contains the module(s) + * @param readOnly true to make the repository read-only + * @param onReadFailure the non-null observer; it must not reenter this repository + */ + public FileRepository(File file, boolean readOnly, BiConsumer onReadFailure) { assert file != null && !file.isDirectory(); - this.file = moduleFile(file); - this.readOnly = readOnly; + this.file = moduleFile(file); + this.readOnly = readOnly; + this.onReadFailure = Objects.requireNonNull(onReadFailure); } // ----- accessors ----------------------------------------------------------------------------- @@ -62,11 +84,6 @@ public synchronized Set getModuleNames() { return validateCache() ? cachedVersionsByName.keySet() : Set.of(); } - @Override - public synchronized Map getReadFailures() { - return readFailure == null ? Map.of() : Map.of(file, readFailure); - } - @Override public synchronized VersionTree getAvailableVersions(String sModule) { return validateCache() ? cachedVersionsByName.get(sModule) : null; @@ -172,9 +189,10 @@ public synchronized void storeModule(ModuleStructure module) fileStructure.writeTo(workFile); file.delete(); if (workFile.renameTo(file)) { - cachedVersionsByName = fileStructure.buildFileInfo().modules(); - cachedFileStructure = fileStructure; - cacheOk = true; + cachedVersionsByName = fileStructure.buildFileInfo().modules(); + cachedFileStructure = fileStructure; + cacheOk = true; + readFailed = false; } } catch (IOException e) { throw new IOException("Error writing module to file: " + file, e); @@ -233,16 +251,16 @@ private static File moduleFile(File file) { * Make sure that the cache is up to date. */ private boolean validateCache() { - // Successful reads keep the usual burst cache. Failed reads retain their cause and are + // Successful reads keep the usual burst cache. Failed reads are // retried after the burst, or immediately if the file metadata changes. long now = System.currentTimeMillis(); boolean unchanged = timestamp == file.lastModified() && size == file.length(); boolean recent = now < lastScan + 60/*ms*/; - if (readFailure == null ? recent || unchanged : recent && unchanged) { + if (!readFailed ? recent || unchanged : recent && unchanged) { return cacheOk; } - boolean retryFailedRead = readFailure != null; + boolean retryFailedRead = readFailed; cachedVersionsByName = Map.of(); cachedFileStructure = null; cachedModuleStructures = null; @@ -262,7 +280,7 @@ private boolean validateCache() { cacheOk = true; } } else { - readFailure = null; + readFailed = false; } timestamp = file.lastModified(); size = file.length(); @@ -301,29 +319,31 @@ private boolean ensureFileStructure() { } /** - * @return the FileInfo freshly read from the file system, or null with a retained read failure + * @return the FileInfo freshly read from the file system, or null after reporting an unsuccessful read */ private FileInfo readFileInfo() { try { FileInfo info = FileStructure.readFileInfo(file); - readFailure = null; + readFailed = false; return info; } catch (IOException e) { - readFailure = e; + readFailed = true; + onReadFailure.accept(file, e); return null; } } /** - * @return the FileStructure freshly read from the file system, or null with a retained read failure + * @return the FileStructure freshly read from the file system, or null after reporting an unsuccessful read */ private FileStructure readFileStructure() { try { FileStructure structure = new FileStructure(file); - readFailure = null; + readFailed = false; return structure; } catch (IOException e) { - readFailure = e; + readFailed = true; + onReadFailure.accept(file, e); return null; } } @@ -371,8 +391,10 @@ private Map ensureModuleCache() { private long lastScan; private boolean cacheOk; - /** The most recent unsuccessful read; cleared by a successful retry or removal. */ - private IOException readFailure; + /** Whether the last read failed and must be retried after the cache interval. */ + private boolean readFailed; + + private final BiConsumer onReadFailure; /** * Cached file contents: module names and versions thereof. diff --git a/javatools/src/main/java/org/xvm/asm/LinkedRepository.java b/javatools/src/main/java/org/xvm/asm/LinkedRepository.java index 9ab32deedd8..397b4b93549 100644 --- a/javatools/src/main/java/org/xvm/asm/LinkedRepository.java +++ b/javatools/src/main/java/org/xvm/asm/LinkedRepository.java @@ -1,12 +1,9 @@ package org.xvm.asm; -import java.io.File; import java.io.IOException; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.Objects; import java.util.Set; import java.util.TreeSet; @@ -85,15 +82,6 @@ public Set getModuleNames() { return names; } - @Override - public Map getReadFailures() { - Map failures = new HashMap<>(); - for (ModuleRepository repo : repos) { - failures.putAll(repo.getReadFailures()); - } - return Map.copyOf(failures); - } - @Override public VersionTree getAvailableVersions(String sModule) { VersionTree vers = new VersionTree<>(); diff --git a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java index ef42aaa2c93..284fa3ef99b 100644 --- a/javatools/src/main/java/org/xvm/asm/ModuleRepository.java +++ b/javatools/src/main/java/org/xvm/asm/ModuleRepository.java @@ -1,9 +1,7 @@ package org.xvm.asm; -import java.io.File; import java.io.IOException; -import java.util.Map; import java.util.Set; import java.util.TreeSet; @@ -53,24 +51,6 @@ default Set getModuleNames(String sDomain) { */ Set getModuleNames(); - /** - * Obtain the file read failures retained by this repository's most recent accesses. - * - *

An unreadable candidate does not stop a search: other files or repositories may still - * supply the requested module. Hosts can inspect these failures when explaining an unsuccessful - * lookup. Their presence alone does not mean that a lookup or compilation failed, and a file's - * module identity may be unknown if its header could not be read. - * - *

This method does not initiate I/O. File repositories replace or clear retained failures - * when subsequent accesses retry the file or observe its removal. Implementations that do not - * retain file read failures return an empty map. - * - * @return an immutable snapshot mapping unreadable files to their original I/O exceptions - */ - default Map getReadFailures() { - return Map.of(); - } - /** * Determine the set of available versions of the specified module. * diff --git a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java index 30ff7436769..6de7a207594 100644 --- a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java @@ -1,5 +1,6 @@ package org.xvm.asm; +import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; @@ -8,6 +9,8 @@ import java.time.Duration; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Test; @@ -19,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -27,24 +31,24 @@ class DirRepositoryFailureTest { @TempDir Path directory; + private final Map reports = new HashMap<>(); + @Test - void corruptModuleRetainsItsFailureAndCanBeRepaired() throws IOException { + void unreadableCandidateIsReportedAndCanBeRepaired() throws IOException { Path file = directory.resolve("broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new DirRepository(directory.toFile(), true); + var repository = new DirRepository(directory.toFile(), true, reports::put); - assertTrue(repository.getReadFailures().isEmpty(), "inspection must not initiate a scan"); + assertTrue(reports.isEmpty(), "construction must not initiate a scan"); assertTrue(repository.getModuleNames().isEmpty()); assertNull(repository.loadModule("Broken")); - var failures = repository.getReadFailures(); - assertEquals(Set.of(file.toFile()), failures.keySet()); - assertNotNull(failures.get(file.toFile())); - assertThrows(UnsupportedOperationException.class, failures::clear); + assertEquals(Set.of(file.toFile()), reports.keySet()); + assertNotNull(reports.get(file.toFile())); + reports.clear(); new FileStructure("Broken").writeTo(file.toFile()); assertNotNull(repository.loadModule("Broken")); - assertTrue(repository.getReadFailures().isEmpty()); - assertEquals(1, failures.size(), "an earlier snapshot must not change"); + assertTrue(reports.isEmpty()); } @Test @@ -52,12 +56,12 @@ void incompatibleCandidateDoesNotHideAUsableModuleInTheSameDirectory() throws IO Path old = directory.resolve("Old.xtc"); writeUnsupportedModule(old, "Old"); new FileStructure("Good").writeTo(directory.resolve("Good.xtc").toFile()); - var repository = new DirRepository(directory.toFile(), true); + var repository = new DirRepository(directory.toFile(), true, reports::put); assertNotNull(repository.loadModule("Good")); assertEquals(Set.of("Good"), repository.getModuleNames()); assertNull(repository.loadModule("Unknown")); - assertTrue(repository.getReadFailures().get(old.toFile()).getMessage() + assertTrue(reports.get(old.toFile()).getMessage() .contains("Unsupported .xtc version")); } @@ -73,47 +77,47 @@ void incompatibleCandidateDoesNotBlockALaterRepository(boolean singleFile) throw good.getModule().setVersion(version); good.writeTo(second.resolve("Good.xtc").toFile()); ModuleRepository candidate = singleFile - ? new FileRepository(old.toFile(), true) - : new DirRepository(first.toFile(), true); - var repository = new LinkedRepository(candidate, new DirRepository(second.toFile(), true)); + ? new FileRepository(old.toFile(), true, reports::put) + : new DirRepository(first.toFile(), true, reports::put); + var repository = new LinkedRepository(candidate, new DirRepository(second.toFile(), true, reports::put)); assertNotNull(repository.loadModule("Good")); assertEquals(Set.of("Good"), repository.getModuleNames()); assertTrue(repository.getAvailableVersions("Good").contains(version)); assertNotNull(repository.loadModule("Good", version, true)); - var failures = repository.getReadFailures(); - assertEquals(Set.of(old.toFile()), failures.keySet()); - assertTrue(failures.get(old.toFile()).getMessage().contains("Unsupported .xtc version")); - assertThrows(UnsupportedOperationException.class, failures::clear); + assertEquals(Set.of(old.toFile()), reports.keySet()); + assertTrue(reports.get(old.toFile()).getMessage().contains("Unsupported .xtc version")); } @Test - void unsuccessfulLinkedSearchRetainsFailuresFromAllCandidates() throws IOException { + void linkedSearchReportsCandidateReadsWithoutAttributingTheMissingModule() throws IOException { Path first = Files.createDirectory(directory.resolve("first")); Path second = Files.createDirectory(directory.resolve("second")); Path old = first.resolve("Old.xtc"); Path broken = second.resolve("Broken.xtc"); writeUnsupportedModule(old, "Old"); Files.writeString(broken, "not a compiled module"); - var repository = new LinkedRepository(new DirRepository(first.toFile(), true), - new DirRepository(second.toFile(), true)); + var repository = new LinkedRepository(new DirRepository(first.toFile(), true, reports::put), + new DirRepository(second.toFile(), true, reports::put)); assertNull(repository.loadModule("Missing")); - assertEquals(Set.of(old.toFile(), broken.toFile()), repository.getReadFailures().keySet()); + assertEquals(Set.of(old.toFile(), broken.toFile()), reports.keySet()); } @Test - void cachedFailureIsRereadByANewRepositoryInstance() throws IOException { + void cachedFailureIsRetriedAndReportedByANewRepositoryInstance() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - assertTrue(new DirRepository(directory.toFile(), true).getModuleNames().isEmpty()); - var repository = new DirRepository(directory.toFile(), true); + assertTrue(new DirRepository(directory.toFile(), true, reports::put).getModuleNames().isEmpty()); + reports.clear(); + var repository = new DirRepository(directory.toFile(), true, reports::put); assertNull(repository.loadModule("Broken")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); new FileStructure("Broken").writeTo(file.toFile()); assertNotNull(repository.loadModule("Broken")); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); } @Test @@ -123,10 +127,11 @@ void failedCandidateIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOExcep byte[] valid = Files.readAllBytes(file); writeUnsupportedModule(file, "Good"); var timestamp = Files.getLastModifiedTime(file); - var repository = new DirRepository(directory.toFile(), true); + var repository = new DirRepository(directory.toFile(), true, reports::put); assertNull(repository.loadModule("Good")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); Files.write(file, valid); Files.setLastModifiedTime(file, timestamp); assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { @@ -134,20 +139,32 @@ void failedCandidateIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOExcep Thread.sleep(10); } }); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); } @Test - void removingAnUnreadableFileClearsItsFailure() throws IOException { + void removedCandidatesProduceNoFurtherReports() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new DirRepository(directory.toFile(), true); + var repository = new DirRepository(directory.toFile(), true, reports::put); assertNull(repository.loadModule("Broken")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); Files.delete(file); assertTrue(repository.getModuleNames().isEmpty()); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); + } + + @Test + void observerFailuresAreNotSwallowedAsUnavailableModules() throws IOException { + Files.writeString(directory.resolve("Broken.xtc"), "unreadable candidate"); + var failure = new IllegalStateException("observer failed"); + var repository = new DirRepository(directory.toFile(), true, (file, cause) -> { + throw failure; + }); + + assertSame(failure, assertThrows(IllegalStateException.class, () -> repository.loadModule("Missing"))); } private static void writeUnsupportedModule(Path file, String name) throws IOException { diff --git a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java index ea721767797..0896a705e6d 100644 --- a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java @@ -1,6 +1,7 @@ package org.xvm.asm; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -9,11 +10,14 @@ import java.time.Duration; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; @@ -25,45 +29,47 @@ class FileRepositoryFailureTest { @TempDir Path directory; + private final Map reports = new HashMap<>(); + @Test - void invalidHeaderRetainsItsCauseAndCanBeRepaired() throws IOException { + void invalidHeaderIsReportedAndCanBeRepaired() throws IOException { Path file = directory.resolve("broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new FileRepository(file.toFile(), true); + var repository = new FileRepository(file.toFile(), true, reports::put); - assertTrue(repository.getReadFailures().isEmpty(), "inspection must not initiate a read"); + assertTrue(reports.isEmpty(), "construction must not initiate a read"); assertTrue(repository.getModuleNames().isEmpty()); assertNull(repository.loadModule("Broken")); - var failures = repository.getReadFailures(); - assertEquals(Set.of(file.toFile()), failures.keySet()); - assertNotNull(failures.get(file.toFile())); - assertThrows(UnsupportedOperationException.class, failures::clear); + assertEquals(Set.of(file.toFile()), reports.keySet()); + assertNotNull(reports.get(file.toFile())); + reports.clear(); new FileStructure("Broken").writeTo(file.toFile()); assertNotNull(repository.loadModule("Broken")); - assertTrue(repository.getReadFailures().isEmpty()); - assertEquals(1, failures.size(), "an earlier snapshot must not change"); + assertTrue(reports.isEmpty()); } @Test - void truncatedModuleRetainsItsCauseAfterItsHeaderWasRead() throws IOException { + void truncatedPayloadIsReportedAfterItsHeaderWasRead() throws IOException { Path file = directory.resolve("broken.xtc"); + reports.clear(); new FileStructure("Broken").writeTo(file.toFile()); byte[] contents = Files.readAllBytes(file); var input = new ByteArrayInputStream(contents); FileStructure.readFileInfo(input); Files.write(file, Arrays.copyOf(contents, contents.length - input.available())); - var repository = new FileRepository(file.toFile(), true); + var repository = new FileRepository(file.toFile(), true, reports::put); assertTrue(repository.getModuleNames().contains("Broken")); assertNull(repository.loadModule("Broken")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); assertNull(repository.loadModule("Broken")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); Files.write(file, contents); assertNotNull(repository.loadModule("Broken")); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); } @Test @@ -75,10 +81,11 @@ void failedReadIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException broken[0] = 0; // Invalidate the magic value without changing the file size. Files.write(file, broken); var timestamp = Files.getLastModifiedTime(file); - var repository = new FileRepository(file.toFile(), true); + var repository = new FileRepository(file.toFile(), true, reports::put); assertNull(repository.loadModule("Good")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); Files.write(file, valid); Files.setLastModifiedTime(file, timestamp); assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { @@ -86,19 +93,35 @@ void failedReadIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException Thread.sleep(10); } }); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); } @Test - void removingAnUnreadableFileClearsItsFailure() throws IOException { + void removedCandidatesProduceNoFurtherReports() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new FileRepository(file.toFile(), true); + var repository = new FileRepository(file.toFile(), true, reports::put); assertNull(repository.loadModule("Broken")); - assertNotNull(repository.getReadFailures().get(file.toFile())); + assertNotNull(reports.get(file.toFile())); + reports.clear(); Files.delete(file); assertTrue(repository.getModuleNames().isEmpty()); - assertTrue(repository.getReadFailures().isEmpty()); + assertTrue(reports.isEmpty()); + } + @Test + void failedWritesAreThrownSeparatelyFromReadReports() throws IOException { + Path file = directory.resolve("Old.xtc"); + Files.writeString(file, "unreadable candidate"); + byte[] original = Files.readAllBytes(file); + var repository = new FileRepository(file.toFile(), true, reports::put); + + assertNull(repository.loadModule("Missing")); + assertEquals(Set.of(file.toFile()), reports.keySet()); + reports.clear(); + assertThrows(IOException.class, () -> repository.storeModule(new FileStructure("Good").getModule())); + assertTrue(reports.isEmpty(), "a write failure is not a read event"); + assertArrayEquals(original, Files.readAllBytes(file)); } + } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt index 6ee045d7edf..d1e26633e6c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt @@ -14,6 +14,8 @@ import org.xvm.asm.FileStructure import org.xvm.asm.ModuleRepository import org.xvm.compiler.Source import org.xvm.tool.ModuleInfo +import java.io.File +import java.io.IOException import java.nio.ByteBuffer import java.nio.file.Files import java.nio.file.Path @@ -75,15 +77,20 @@ class EmbeddingDiagnosticsTest { val bytes = file.readBytes() ByteBuffer.wrap(bytes).putInt(2 * Int.SIZE_BYTES, FileStructure.getToolMinorVersion() - 1) Files.write(file.toPath(), bytes) + val reports = mutableMapOf() val repository: ModuleRepository = - if (singleFile) FileRepository(file, true) else DirRepository(directory.toFile(), true) + if (singleFile) { + FileRepository(file, true, reports::put) + } else { + DirRepository(directory.toFile(), true, reports::put) + } val support = EmbeddingSupport.instance() for (text in listOf("module Test {}", "module Test { void broken( { }")) { val errors = ErrorList() val result = support.compileModule(Source(text), repository, errors) assertThat(result.succeeded()).isEqualTo(text == "module Test {}") - assertThat(repository.readFailures).containsKey(file) + assertThat(reports).containsKey(file) assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") } @@ -95,9 +102,9 @@ class EmbeddingDiagnosticsTest { errors, ) assertThat(result.succeeded()).isFalse() - assertThat(errors.errors.map { it.code }).contains("COMPILER-24", "EMB-5") - assertThat(errors.errors.filter { it.code == "EMB-5" }.map { it.message }) - .anyMatch { it.contains(file.path) && it.contains("Unsupported .xtc version") } + assertThat(errors.errors.map { it.code }).contains("COMPILER-24").doesNotContain("EMB-5") + assertThat(errors.errors).noneMatch { it.message.contains(file.path) } + assertThat(reports.getValue(file)).hasMessageContaining("Unsupported .xtc version") } @Test diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt new file mode 100644 index 00000000000..1651b9ea244 --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt @@ -0,0 +1,136 @@ +package org.xvm.lsp.adapter + +import org.assertj.core.api.Assertions.assertThat +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.Timeout +import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource +import org.xvm.api.EmbeddingSupport +import org.xvm.api.InterpreterConnector +import org.xvm.asm.ConstantPool +import org.xvm.asm.DirRepository +import org.xvm.asm.ErrorList +import org.xvm.asm.FileRepository +import org.xvm.asm.FileStructure +import org.xvm.asm.LinkedRepository +import org.xvm.asm.ModuleRepository +import org.xvm.asm.ModuleStructure +import org.xvm.compiler.InstantRepository +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream +import java.io.File +import java.io.IOException +import java.nio.ByteBuffer +import java.nio.file.Files +import java.nio.file.Path + +/** Exercises repository reporting as a host and through the native Ecstasy runtime. */ +@Timeout(60) +class RepositoryReportingTest { + @TempDir lateinit var directory: Path + + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `candidate reports remain separate from compilation outcomes`(singleFile: Boolean) { + CompilerTestSupport.configure() + val file = directory.resolve("Old.xtc").toFile() + unsupported(file, "Old") + val reports = mutableMapOf() + val repository: ModuleRepository = + if (singleFile) FileRepository(file, true, reports::put) else DirRepository(directory.toFile(), true, reports::put) + val support = EmbeddingSupport.instance() + val validErrors = ErrorList(100) + assertThat(support.compile("module Good {}", repository, validErrors)).isNotNull() + assertThat(validErrors.errors).isEmpty() + assertThat(reports[file]?.message).contains("Unsupported .xtc version") + + val missingErrors = ErrorList(100) + assertThat(support.compile("module Test { package lib import Missing; }", repository, missingErrors)).isNull() + assertThat(missingErrors.hasSeriousErrors()).isTrue() + assertThat(missingErrors.errors).noneMatch { it.message.contains(file.path) } + } + + @Test + fun `Ecstasy repositories preserve platform fallback and retry with optional reporting`() { + CompilerTestSupport.configure() + val first = Files.createDirectory(directory.resolve("first")) + val second = Files.createDirectory(directory.resolve("second")) + unsupported(first.resolve("Old.xtc").toFile(), "Old") + Files.writeString(first.resolve("Good.xtc"), "unreadable candidate") + compile("module Good.example.org {}").fileStructure.writeTo(second.resolve("Good.xtc").toFile()) + val source = + """ + module RepositoryLookup { + import ecstasy.mgmt.DirRepository; + import ecstasy.mgmt.LinkedRepository; + import ecstasy.mgmt.ModuleRepository; + import ecstasy.reflect.ModuleTemplate; + + service Reports { + Int count = 0; + void record(File file, Exception cause) { + assert file.name == "Old.xtc" || file.name == "Good.xtc"; + assert cause.cause != Null; + ++count; + } + } + + Int run() { + @Inject Directory rootDir; + Directory first = rootDir.dirFor("${first.toString().removePrefix("/")}"); + Directory second = rootDir.dirFor("${second.toString().removePrefix("/")}"); + Reports reports = new Reports(); + DirRepository primary = new DirRepository(first, (file, cause) -> reports.record(file, cause)); + ModuleRepository fallback = new DirRepository(second); + ModuleRepository linked = new LinkedRepository([primary, fallback].toArray(Constant, True)); + assert ModuleTemplate good := linked.getModule("Good.example.org"); + assert good.qualifiedName == "Good.example.org"; + assert reports.count >= 2; + assert !(linked.getModule("Missing")); + + first.fileFor("Good.xtc").contents = second.fileFor("Good.xtc").contents; + assert primary.getModule("Good.example.org"); + assert !(primary.getModule("ForceRefresh")); + assert primary.getModule("Good.example.org"); + assert primary.moduleNames.contains("Good.example.org"); + + first.fileFor("Old.xtc").delete(); + first.fileFor("Good.xtc").delete(); + assert !(primary.getModule("ForceRefresh")); + assert primary.moduleNames.empty; + assert linked.getModule("Good.example.org"); + return 42; + } + } + """.trimIndent() + val compiled = compile(source) + val bytes = ByteArrayOutputStream().also { compiled.fileStructure.writeTo(it) }.toByteArray() + val module = FileStructure(ByteArrayInputStream(bytes)).module + val connector = + InterpreterConnector(LinkedRepository(InstantRepository(module), EmbeddingSupport.instance().configuredRepository)) + connector.loadModule(module.name) + connector.start(null) + ConstantPool.withPool(connector.constantPool).use { + connector.invoke0(connector.findMethods("run").single()) + } + assertThat(connector.join()).isEqualTo(42) + } + + private fun compile(source: String): ModuleStructure { + val errors = ErrorList(100) + val module = EmbeddingSupport.instance().compile(source, null, errors) + assertThat(errors.errors).isEmpty() + return requireNotNull(module) + } + + private fun unsupported( + file: File, + name: String, + ) { + FileStructure(name).writeTo(file) + val bytes = file.readBytes() + ByteBuffer.wrap(bytes).putInt(2 * Int.SIZE_BYTES, FileStructure.getToolMinorVersion() - 1) + file.writeBytes(bytes) + } +} From f136e0b2546ba9843b8c2092fd582f0c82ae6cab Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:09 +0100 Subject: [PATCH 010/324] Clarify tooling contracts and isolate partial AST helpers - Emit runtime type values for bound generic arguments. - Clarify tooling contracts and isolate partial AST helpers. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../src/main/kotlin/IntellijRunIdeSupport.kt | 11 + .../src/main/kotlin/PruneTestReportsTask.kt | 5 + .../src/test/kotlin/ServerLogTailsTest.kt | 5 + .../test/kotlin/TestReportRetentionTest.kt | 5 + gradle/libs.versions.toml | 26 +- javatools/README.md | 110 +++++- .../java/org/xvm/api/EmbeddingSupport.java | 340 ++++++++++------- .../src/main/java/org/xvm/asm/Annotation.java | 3 +- .../main/java/org/xvm/asm/ConstantPool.java | 6 +- .../org/xvm/asm/constants/MethodInfo.java | 4 + .../main/java/org/xvm/compiler/Compiler.java | 22 +- .../java/org/xvm/compiler/CursorBinding.java | 361 +++++++++++++++++- .../org/xvm/compiler/InitializerBinding.java | 28 +- .../org/xvm/compiler/InvocationBinding.java | 106 ++++- .../main/java/org/xvm/compiler/Parser.java | 15 + .../java/org/xvm/compiler/ast/AstNode.java | 16 +- .../xvm/compiler/ast/LambdaExpression.java | 9 +- .../org/xvm/compiler/ast/NewExpression.java | 2 + .../java/org/xvm/compiler/ast/PartialAst.java | 282 ++++++++++++++ .../java/org/xvm/compiler/ast/StageMgr.java | 53 ++- .../org/xvm/compiler/ast/StatementBlock.java | 35 +- .../ast/{ => partial}/CursorScope.java | 96 ++--- .../IncompleteDeclarationStatement.java | 32 +- .../ast/partial/IncompleteExpression.java | 11 +- .../partial/IncompleteLocalDeclaration.java | 12 + .../ast/partial/IncompleteStatement.java | 187 +++++++-- .../IncompleteTypeCompositionStatement.java | 35 +- .../ast/{ => partial}/PartialArgument.java | 17 +- .../{ => partial}/PartialCallResolver.java | 111 +++--- .../PartialConstructionResolver.java | 41 +- .../ast/{ => partial}/PartialQueries.java | 53 ++- .../compiler/ast/partial/PartialSyntax.java | 46 ++- .../ast/partial/ProposedLiteralToken.java | 8 + .../main/java/org/xvm/tool/ModuleInfo.java | 42 +- .../xvm/api/BindingIdentitySnapshotTest.java | 3 + .../org/xvm/api/EmbeddingFootprintTest.java | 4 + .../org/xvm/asm/DirRepositoryFailureTest.java | 4 + .../xvm/asm/FileRepositoryFailureTest.java | 4 + .../org/xvm/compiler/EvalCompilerTest.java | 4 + .../compiler/ParserDeclarationNameTest.java | 1 + .../ast/DeclarationProvenanceTest.java | 1 + .../ast/NameResolverReportingTest.java | 1 + .../ast/{ => partial}/PartialSyntaxTest.java | 21 +- .../partial/RegisteredChildFieldsTest.java | 2 + lang/.editorconfig | 3 + lang/.gitignore | 3 + lang/README.md | 35 +- lang/doc/README.md | 13 +- lang/doc/manual-test-plan.md | 157 ++++---- lang/intellij-plugin/README.md | 10 +- lang/intellij-plugin/TESTING.md | 7 +- .../idea/playbook/ClientDiagnostics.kt | 14 + .../xtclang/idea/playbook/ClientProtocol.kt | 13 + .../org/xtclang/idea/playbook/ClientTrace.kt | 3 + .../xtclang/idea/playbook/CompilerPlaybook.kt | 2 + .../idea/playbook/CompilerSettingsPage.kt | 1 + .../idea/playbook/CompletionActions.kt | 2 + .../org/xtclang/idea/playbook/EditorFocus.kt | 4 + .../org/xtclang/idea/playbook/IdeServices.kt | 15 + .../idea/playbook/LanguageServicePage.kt | 1 + .../xtclang/idea/playbook/LargeFileEditing.kt | 1 + .../idea/playbook/NativeDiagnostics.kt | 2 + .../idea/playbook/NativeSemanticViews.kt | 16 + .../idea/playbook/NavigationChooserTest.kt | 4 + .../org/xtclang/idea/playbook/ParityColors.kt | 1 + .../idea/playbook/ParityCompilerImports.kt | 1 + .../idea/playbook/ParityHighlighting.kt | 2 + .../idea/playbook/ParityInlineCompletion.kt | 1 + .../idea/playbook/ParityLibrarySettings.kt | 1 + .../xtclang/idea/playbook/ParityPlatform.kt | 3 + .../xtclang/idea/playbook/ParityProgress.kt | 1 + .../idea/playbook/ParityReferenceLenses.kt | 1 + .../idea/playbook/ParityReliability.kt | 2 + .../org/xtclang/idea/playbook/ParityRename.kt | 4 + .../idea/playbook/ParityRuntimeSettings.kt | 1 + .../xtclang/idea/playbook/ParitySemantics.kt | 1 + .../xtclang/idea/playbook/ParityTypeMoves.kt | 1 + .../xtclang/idea/playbook/ParityWorkspace.kt | 4 + .../xtclang/idea/playbook/PlaybookFailure.kt | 1 + .../idea/playbook/PlaybookFailureTest.kt | 4 + .../xtclang/idea/playbook/PlaybookProgress.kt | 1 + .../xtclang/idea/playbook/ProjectLifecycle.kt | 3 + .../idea/playbook/RefactoringActions.kt | 3 + .../xtclang/idea/playbook/ReferenceActions.kt | 9 + .../idea/playbook/SettingsPersistence.kt | 1 + .../xtclang/idea/playbook/SharedScenarios.kt | 16 + .../xtclang/idea/playbook/StartupEditing.kt | 1 + .../xtclang/idea/playbook/StructureViews.kt | 7 + .../org/xtclang/idea/playbook/UiWaitsTest.kt | 4 + .../idea/playbook/UpstreamReplacementGates.kt | 1 + .../xtclang/idea/playbook/WorkspaceFolders.kt | 5 + .../idea/playbook/probe/PlaybookProgress.kt | 1 + .../idea/lsp/ClosedRefactoringDocuments.kt | 6 + .../xtclang/idea/lsp/CodeActionMessages.kt | 5 + .../xtclang/idea/lsp/CompilerBuildModel.kt | 8 +- .../org/xtclang/idea/lsp/CompilerImport.kt | 13 +- .../xtclang/idea/lsp/CompilerImportService.kt | 4 + .../idea/lsp/CompilerLibrariesPanel.kt | 4 + .../xtclang/idea/lsp/CompilerRootWatches.kt | 4 + .../xtclang/idea/lsp/DiagnosticQuickFixes.kt | 6 + .../idea/lsp/DiagnosticResultMessages.kt | 3 + .../idea/lsp/DirectoryDocumentMoves.kt | 5 + .../idea/lsp/DocumentStartupMessages.kt | 32 +- .../org/xtclang/idea/lsp/EditorRefresh.kt | 2 + .../idea/lsp/LanguageServiceConfigurable.kt | 2 + .../xtclang/idea/lsp/LibraryConfiguration.kt | 2 + .../xtclang/idea/lsp/PreflightedRenames.kt | 1 + .../org/xtclang/idea/lsp/ServerJvmOptions.kt | 8 +- .../xtclang/idea/lsp/ServerRuntimeSettings.kt | 1 + .../org/xtclang/idea/lsp/ServerSupportLogs.kt | 1 + .../xtclang/idea/lsp/ServerWorkspaceEdit.kt | 2 + .../idea/lsp/SourceGraphConfiguration.kt | 1 + .../org/xtclang/idea/lsp/SourceGraphEdit.kt | 1 + .../xtclang/idea/lsp/XtcFileMoveHandler.kt | 1 + .../org/xtclang/idea/lsp/XtcFileOperations.kt | 19 +- .../xtclang/idea/lsp/XtcFileRenameHandler.kt | 1 + .../xtclang/idea/lsp/XtcFormattingService.kt | 8 +- .../org/xtclang/idea/lsp/XtcLanguageServer.kt | 3 + .../org/xtclang/idea/lsp/XtcRenameEdit.kt | 3 + .../org/xtclang/idea/lsp/XtcRenameHandler.kt | 14 +- .../idea/lsp/ClientCapabilitiesTest.kt | 4 + .../idea/lsp/CodeActionMessagesTest.kt | 4 + .../idea/lsp/CompilerBuildModelTest.kt | 4 + .../idea/lsp/CompilerBuildUpdatesTest.kt | 4 + .../idea/lsp/CompilerImportServiceTest.kt | 4 + .../xtclang/idea/lsp/CompilerImportTest.kt | 4 + .../idea/lsp/CompilerRootWatchesTest.kt | 4 + .../xtclang/idea/lsp/ConfigurationJsonTest.kt | 4 + .../idea/lsp/DiagnosticQuickFixesTest.kt | 4 + .../idea/lsp/DiagnosticReportJsonTest.kt | 4 + .../idea/lsp/DiagnosticResultMessagesTest.kt | 4 + .../idea/lsp/DirectoryDocumentMovesTest.kt | 5 + .../idea/lsp/DocumentStartupMessagesTest.kt | 4 + .../xtclang/idea/lsp/FileMoveTargetsTest.kt | 4 + .../lsp/LanguageServiceConfigurationTest.kt | 4 + .../idea/lsp/LibraryConfigurationTest.kt | 4 + .../idea/lsp/PreflightedRenamesTest.kt | 4 + .../xtclang/idea/lsp/ServerJvmOptionsTest.kt | 5 + .../idea/lsp/ServerSettingsPersistenceTest.kt | 4 + .../xtclang/idea/lsp/ServerSupportLogsTest.kt | 5 + .../idea/lsp/ServerWorkspaceEditTest.kt | 4 + .../idea/lsp/SourceGraphConfigurationTest.kt | 4 + lang/lsp-server/README.md | 30 +- .../org/xvm/lsp/adapter/AdapterFormatter.kt | 18 +- .../org/xvm/lsp/adapter/AdapterModels.kt | 1 + .../org/xvm/lsp/adapter/DocumentColor.kt | 4 + .../adapter/treesitter/TreeSitterAdapter.kt | 30 +- .../adapter/xdk/CompilerAnnotationIdentity.kt | 48 +-- .../xvm/lsp/adapter/xdk/CompilerDispatch.kt | 17 +- .../adapter/xdk/CompilerImplementations.kt | 17 +- .../lsp/adapter/xdk/CompilerImportAliases.kt | 14 +- .../lsp/adapter/xdk/CompilerMemberAction.kt | 4 +- .../adapter/xdk/CompilerMemberSignature.kt | 92 ++--- .../adapter/xdk/CompilerMethodRelations.kt | 4 + .../lsp/adapter/xdk/CompilerMissingMethod.kt | 13 +- .../adapter/xdk/CompilerPropertyRelations.kt | 1 + .../xvm/lsp/adapter/xdk/CompilerQueueTrace.kt | 19 +- .../adapter/xdk/CompilerReceiverIdentity.kt | 24 +- .../lsp/adapter/xdk/CompilerRenameFacts.kt | 38 +- .../lsp/adapter/xdk/CompilerResourceValues.kt | 24 +- .../lsp/adapter/xdk/CompilerSourceLinks.kt | 4 +- .../lsp/adapter/xdk/CompilerSourceUsage.kt | 12 +- .../xvm/lsp/adapter/xdk/CompilerTypeNames.kt | 10 +- .../xvm/lsp/adapter/xdk/LibraryDeclaration.kt | 1 + .../lsp/adapter/xdk/PartialSemanticModel.kt | 5 + .../org/xvm/lsp/adapter/xdk/SemanticModel.kt | 13 + .../lsp/adapter/xdk/SemanticModelBuilder.kt | 152 ++------ .../org/xvm/lsp/adapter/xdk/XdkAdapter.kt | 28 +- .../xvm/lsp/adapter/xdk/XdkAttachedSources.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkAutoImports.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkBuildModel.kt | 16 +- .../xvm/lsp/adapter/xdk/XdkColorPrototype.kt | 3 + .../xvm/lsp/adapter/xdk/XdkCursorQueries.kt | 5 +- .../org/xvm/lsp/adapter/xdk/XdkDependency.kt | 10 + .../xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt | 3 + .../org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt | 12 +- .../org/xvm/lsp/adapter/xdk/XdkImports.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkLexical.kt | 2 + .../org/xvm/lsp/adapter/xdk/XdkLibraries.kt | 2 + .../xvm/lsp/adapter/xdk/XdkLibrarySources.kt | 2 + .../lsp/adapter/xdk/XdkLocalDeclarations.kt | 1 + .../xvm/lsp/adapter/xdk/XdkLocalExtraction.kt | 6 +- .../org/xvm/lsp/adapter/xdk/XdkLocalInline.kt | 6 +- .../xvm/lsp/adapter/xdk/XdkLocalRemoval.kt | 1 + .../xvm/lsp/adapter/xdk/XdkMemberActions.kt | 4 + .../xvm/lsp/adapter/xdk/XdkMemberInline.kt | 5 +- .../lsp/adapter/xdk/XdkMethodExtraction.kt | 22 +- .../lsp/adapter/xdk/XdkMissingDeclarations.kt | 8 +- .../xvm/lsp/adapter/xdk/XdkMissingMethods.kt | 15 +- .../org/xvm/lsp/adapter/xdk/XdkMonikers.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkMoveImports.kt | 1 + .../xvm/lsp/adapter/xdk/XdkNavigationIndex.kt | 8 +- .../xvm/lsp/adapter/xdk/XdkPresentation.kt | 32 +- .../xvm/lsp/adapter/xdk/XdkProjectQueries.kt | 41 +- .../xvm/lsp/adapter/xdk/XdkQualifiedName.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkRename.kt | 80 ++-- .../xvm/lsp/adapter/xdk/XdkRenameProposal.kt | 1 + .../xvm/lsp/adapter/xdk/XdkResourceMoves.kt | 2 + .../org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt | 5 +- .../xvm/lsp/adapter/xdk/XdkSourceModule.kt | 1 + .../org/xvm/lsp/adapter/xdk/XdkSources.kt | 2 + .../org/xvm/lsp/adapter/xdk/XdkSymbols.kt | 16 +- .../lsp/adapter/xdk/XdkSyntaxCompletions.kt | 31 +- .../org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt | 11 +- .../lsp/adapter/xdk/XdkWorkspaceDiscovery.kt | 5 + .../org/xvm/lsp/index/WorkspaceIndexer.kt | 6 +- .../org/xvm/lsp/server/ClientRefresh.kt | 5 + .../kotlin/org/xvm/lsp/server/ClientTrace.kt | 1 + .../org/xvm/lsp/server/CodeLensSettings.kt | 1 + .../org/xvm/lsp/server/ConnectionProgress.kt | 6 +- .../org/xvm/lsp/server/DiagnosticReports.kt | 1 + .../xvm/lsp/server/EditorFormattingState.kt | 1 + .../org/xvm/lsp/server/LspJsonOptions.kt | 40 +- .../org/xvm/lsp/server/PartialResults.kt | 1 + .../org/xvm/lsp/server/ProtocolLifecycle.kt | 6 +- .../org/xvm/lsp/server/ProtocolTrace.kt | 12 +- .../org/xvm/lsp/server/ReadOnlyDocuments.kt | 1 + .../org/xvm/lsp/server/RenameProposal.kt | 4 + .../org/xvm/lsp/server/ResolveReports.kt | 10 +- .../xvm/lsp/server/SemanticTokenReports.kt | 1 + .../org/xvm/lsp/server/XtcLanguageServer.kt | 4 + .../lsp/server/XtcLanguageServerLauncher.kt | 16 +- .../xvm/lsp/server/XtcTextDocumentService.kt | 5 +- .../lsp/treesitter/SemanticTokenEncoder.kt | 4 +- .../org/xvm/lsp/treesitter/XtcQueryEngine.kt | 8 +- .../kotlin/org/xvm/lsp/util/ExecutionTrace.kt | 2 + .../kotlin/org/xvm/lsp/util/ProgressLabels.kt | 8 +- .../kotlin/org/xvm/lsp/util/ServerLogs.kt | 20 +- .../kotlin/org/xvm/lsp/util/TraceProcessId.kt | 5 +- .../org/xvm/lsp/adapter/AdapterFuturesTest.kt | 4 + .../CompilerBoundaryRequirementsTest.kt | 5 + .../xvm/lsp/adapter/CompilerCallSiteTest.kt | 5 + .../CompilerDeclarationAnalysisTest.kt | 5 + .../adapter/CompilerExtractMethodFactsTest.kt | 4 + .../CompilerMethodExtractionProofTest.kt | 4 + .../adapter/CompilerMissingMethodProofTest.kt | 5 + .../lsp/adapter/EmbeddingDiagnosticsTest.kt | 5 + .../lsp/adapter/XdkAdapterLifecycleTest.kt | 8 + .../adapter/XdkAnonymousConstructorTest.kt | 4 + .../lsp/adapter/XdkArgumentCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkArgumentContextTest.kt | 4 + .../xvm/lsp/adapter/XdkArrayDimensionTest.kt | 5 + .../org/xvm/lsp/adapter/XdkBuildModelTest.kt | 4 + .../xvm/lsp/adapter/XdkCallHierarchyTest.kt | 4 + .../org/xvm/lsp/adapter/XdkColorValueTest.kt | 5 + .../lsp/adapter/XdkCompletionSignatureTest.kt | 4 + .../xvm/lsp/adapter/XdkCompoundHeaderTest.kt | 4 + .../XdkConfigurationReplacementTest.kt | 4 + .../XdkCrossModuleMissingMethodsTest.kt | 4 + .../XdkCrossOwnerMissingMethodsTest.kt | 4 + .../lsp/adapter/XdkCursorPresentationTest.kt | 4 + .../xvm/lsp/adapter/XdkCursorRequestTest.kt | 5 + .../xvm/lsp/adapter/XdkCyclicRenameTest.kt | 4 + .../lsp/adapter/XdkDeclarationHeaderTest.kt | 4 + .../org/xvm/lsp/adapter/XdkDeclarationTest.kt | 4 + .../xvm/lsp/adapter/XdkDelegatedRenameTest.kt | 4 + .../lsp/adapter/XdkDelimiterRecoveryTest.kt | 4 + .../org/xvm/lsp/adapter/XdkDependencyTest.kt | 4 + .../xvm/lsp/adapter/XdkDiagnosticIndexTest.kt | 4 + .../org/xvm/lsp/adapter/XdkDiagnosticTest.kt | 5 + .../xvm/lsp/adapter/XdkDocumentationTest.kt | 4 + .../org/xvm/lsp/adapter/XdkEditingTest.kt | 4 + .../XdkEnclosingValueCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkExternalRenameTest.kt | 4 + .../org/xvm/lsp/adapter/XdkFileChangesTest.kt | 4 + .../xvm/lsp/adapter/XdkFileOperationsTest.kt | 4 + .../org/xvm/lsp/adapter/XdkFoldingTest.kt | 4 + .../lsp/adapter/XdkFormattingBreadthTest.kt | 4 + .../xvm/lsp/adapter/XdkGenericHeaderTest.kt | 4 + .../org/xvm/lsp/adapter/XdkGraphMoveTest.kt | 5 + .../org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt | 4 + .../XdkImportCompletionLifecycleTest.kt | 4 + .../lsp/adapter/XdkImportCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkIncompleteCallTest.kt | 4 + .../adapter/XdkIncompleteConstructorTest.kt | 4 + .../lsp/adapter/XdkIncompleteFunctionTest.kt | 4 + .../xvm/lsp/adapter/XdkIndexLifecycleTest.kt | 4 + .../adapter/XdkInferredPresentationTest.kt | 4 + .../org/xvm/lsp/adapter/XdkInitializerTest.kt | 4 + .../lsp/adapter/XdkInlineCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkLambdaRenameTest.kt | 4 + .../org/xvm/lsp/adapter/XdkLexicalTest.kt | 4 + .../org/xvm/lsp/adapter/XdkLibrariesTest.kt | 4 + .../lsp/adapter/XdkLibraryDocumentsTest.kt | 4 + .../xvm/lsp/adapter/XdkLinkedEditingTest.kt | 4 + .../lsp/adapter/XdkLiteralCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkLiteralRecoveryTest.kt | 4 + .../xvm/lsp/adapter/XdkLiveWorkspaceTest.kt | 4 + .../xvm/lsp/adapter/XdkLocalExtractionTest.kt | 4 + .../adapter/XdkLocalInitializerActionsTest.kt | 4 + .../org/xvm/lsp/adapter/XdkLocalInlineTest.kt | 4 + .../xvm/lsp/adapter/XdkLocalRemovalTest.kt | 5 + .../adapter/XdkManualCompositionRenameTest.kt | 4 + .../lsp/adapter/XdkManualCompositionTest.kt | 4 + .../xvm/lsp/adapter/XdkMemberActionsTest.kt | 4 + .../xvm/lsp/adapter/XdkMemberInlineTest.kt | 4 + .../lsp/adapter/XdkMethodExtractionTest.kt | 4 + .../adapter/XdkMissingDeclarationNameTest.kt | 4 + .../lsp/adapter/XdkMissingDeclarationsTest.kt | 5 + .../xvm/lsp/adapter/XdkMissingMethodsTest.kt | 4 + .../xvm/lsp/adapter/XdkModuleSessionTest.kt | 5 + .../org/xvm/lsp/adapter/XdkMonikerTest.kt | 5 + .../org/xvm/lsp/adapter/XdkMoveInputsTest.kt | 4 + .../xvm/lsp/adapter/XdkNavigationIndexTest.kt | 4 + .../org/xvm/lsp/adapter/XdkNavigationTest.kt | 4 + .../lsp/adapter/XdkOperandCompletionTest.kt | 5 + .../xvm/lsp/adapter/XdkParameterRenameTest.kt | 4 + .../lsp/adapter/XdkPresentationScaleTest.kt | 4 + .../xvm/lsp/adapter/XdkPresentationTest.kt | 4 + .../adapter/XdkPrimaryParameterRenameTest.kt | 4 + .../xvm/lsp/adapter/XdkProjectGraphTest.kt | 4 + .../lsp/adapter/XdkProjectProofMemoryTest.kt | 1 + .../adapter/XdkProjectQueryLifecycleTest.kt | 7 + .../xvm/lsp/adapter/XdkProjectQueryTest.kt | 4 + .../org/xvm/lsp/adapter/XdkProjectTest.kt | 4 + .../XdkPropertyArgumentCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkQualifiedHeaderTest.kt | 4 + .../org/xvm/lsp/adapter/XdkRecoveryTest.kt | 4 + .../xvm/lsp/adapter/XdkReferenceLensTest.kt | 4 + .../org/xvm/lsp/adapter/XdkRenameTest.kt | 4 + .../xvm/lsp/adapter/XdkResourceInputsTest.kt | 4 + .../xvm/lsp/adapter/XdkResourceRenameTest.kt | 4 + .../org/xvm/lsp/adapter/XdkSafeDeleteTest.kt | 4 + .../xvm/lsp/adapter/XdkScopeCompletionTest.kt | 4 + .../xvm/lsp/adapter/XdkSemanticColorTest.kt | 5 + .../xvm/lsp/adapter/XdkSemanticLookupTest.kt | 4 + .../org/xvm/lsp/adapter/XdkSourceLinksTest.kt | 4 + .../adapter/XdkSpecializedConstructorTest.kt | 4 + .../org/xvm/lsp/adapter/XdkStructuralTest.kt | 4 + .../lsp/adapter/XdkSyntaxCompletionTest.kt | 4 + .../org/xvm/lsp/adapter/XdkTypeHeaderTest.kt | 4 + .../org/xvm/lsp/adapter/XdkTypeMoveTest.kt | 4 + .../adapter/XdkUnionDelegationRenameTest.kt | 4 + .../org/xvm/lsp/adapter/XdkUnionRenameTest.kt | 4 + .../xvm/lsp/adapter/XdkValueTemplateTest.kt | 4 + .../lsp/adapter/XdkWorkspaceDiscoveryTest.kt | 4 + .../lsp/adapter/XdkWorkspaceNavigationTest.kt | 4 + .../adapter/XdkWorkspaceRefactoringTest.kt | 4 + .../xvm/lsp/adapter/XdkWrittenFormalTest.kt | 4 + .../org/xvm/lsp/server/AdapterBackendTest.kt | 4 + .../lsp/server/CapabilityNegotiationTest.kt | 4 + .../xvm/lsp/server/ClientNotificationsTest.kt | 4 + .../xvm/lsp/server/ClientPresentationTest.kt | 4 + .../xvm/lsp/server/CodeLensSettingsTest.kt | 4 + .../xvm/lsp/server/ColorValueProtocolTest.kt | 4 + .../lsp/server/CompilerConfigurationTest.kt | 6 + .../xvm/lsp/server/CompilerLibrariesTest.kt | 4 + .../xvm/lsp/server/ConnectionProgressTest.kt | 5 + .../lsp/server/DiagnosticPresentationTest.kt | 4 + .../lsp/server/DocumentSynchronizationTest.kt | 4 + .../lsp/server/EditorFormattingStateTest.kt | 4 + .../org/xvm/lsp/server/ExecutionTraceTest.kt | 5 + .../xvm/lsp/server/FileChangeSnapshotsTest.kt | 4 + .../xvm/lsp/server/IndexingProgressTest.kt | 4 + .../server/InlineCompletionProtocolTest.kt | 4 + .../lsp/server/LibraryContentProtocolTest.kt | 4 + .../org/xvm/lsp/server/MonikerProtocolTest.kt | 4 + .../org/xvm/lsp/server/PartialResultsTest.kt | 4 + .../org/xvm/lsp/server/ProgressLabelsTest.kt | 4 + .../xvm/lsp/server/ProtocolLifecycleTest.kt | 4 + .../xvm/lsp/server/RequestOwnershipTest.kt | 4 + .../org/xvm/lsp/server/ResolveReportsTest.kt | 4 + .../lsp/server/ResourceFileWatchersTest.kt | 4 + .../lsp/server/SemanticTokenReportsTest.kt | 4 + .../xvm/lsp/server/StartupRegistrationTest.kt | 4 + .../xvm/lsp/server/XdkCallbackServerTest.kt | 5 + .../org/xvm/lsp/server/XdkCursorServerTest.kt | 8 + .../org/xvm/lsp/server/XdkCursorWatchTest.kt | 4 + .../lsp/server/XdkDeclarationServerTest.kt | 4 + .../server/XdkFeatureResolveProtocolTest.kt | 4 + .../xvm/lsp/server/XdkLanguageServerTest.kt | 7 + .../org/xvm/lsp/server/XdkModuleServerTest.kt | 5 + .../xvm/lsp/server/XdkProjectServerTest.kt | 6 + .../xvm/lsp/server/XdkPullDiagnosticsTest.kt | 5 + .../org/xvm/lsp/server/XdkRenameServerTest.kt | 4 + .../xvm/lsp/server/XdkResolveProtocolTest.kt | 4 + .../server/XdkSemanticTokenProtocolTest.kt | 4 + .../kotlin/org/xvm/lsp/server/XdkStdioTest.kt | 5 + .../server/XdkSyntaxCompletionProtocolTest.kt | 4 + .../kotlin/org/xvm/lsp/util/ServerLogsTest.kt | 4 + .../compiler-import/build.gradle.kts | 6 +- lang/vscode-extension/README.md | 8 +- 382 files changed, 3383 insertions(+), 1245 deletions(-) create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/PartialAst.java rename javatools/src/main/java/org/xvm/compiler/ast/{ => partial}/CursorScope.java (87%) rename javatools/src/main/java/org/xvm/compiler/ast/{ => partial}/PartialArgument.java (80%) rename javatools/src/main/java/org/xvm/compiler/ast/{ => partial}/PartialCallResolver.java (85%) rename javatools/src/main/java/org/xvm/compiler/ast/{ => partial}/PartialConstructionResolver.java (82%) rename javatools/src/main/java/org/xvm/compiler/ast/{ => partial}/PartialQueries.java (55%) rename javatools/src/test/java/org/xvm/compiler/ast/{ => partial}/PartialSyntaxTest.java (94%) create mode 100644 lang/.editorconfig diff --git a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt index 3887818a2a6..041af74149d 100644 --- a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt +++ b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt @@ -17,6 +17,10 @@ import java.nio.file.StandardOpenOption.READ import java.nio.file.attribute.BasicFileAttributes import kotlin.concurrent.thread +/** + * Reports the versions, caches and log paths used by the development IDE. Reads live sandbox state at + * execution time so the report also explains what a reused sandbox contains. + */ @UntrackedTask(because = "Runtime reporting task reads live sandbox state and should never be state-tracked") abstract class RunIdeEnvironmentReportTask : DefaultTask() { @get:Input @@ -88,6 +92,10 @@ abstract class RunIdeEnvironmentReportTask : DefaultTask() { } } +/** + * Forwards new language-server log lines to the Gradle console while the development IDE runs. A named daemon + * thread follows session creation and log rollover; restarting this task replaces the previous tailer. + */ @UntrackedTask(because = "Runtime log tail task manages background thread state and should never be state-tracked") abstract class StartLogTailTask : DefaultTask() { // The directory may not exist until the first server starts; its live contents are not inputs. @@ -126,6 +134,7 @@ abstract class StartLogTailTask : DefaultTask() { } } +/** Stops the named development-IDE log tailer when the IDE run finishes. */ @UntrackedTask(because = "Runtime log tail stop task manages background thread state and should never be state-tracked") abstract class StopLogTailTask : DefaultTask() { @get:Input @@ -141,8 +150,10 @@ abstract class StopLogTailTask : DefaultTask() { internal object ServerLogTails { private val sessionName = Regex("server-[0-9]+-[0-9]+") + /** File identity and byte offset used to distinguish appended text from truncation or rollover. */ data class Position(val key: Any?, val offset: Long) + /** New log lines and the detached file cursors to use on the next poll. */ data class Batch(val positions: Map, val lines: List) fun poll(root: Path, previous: Map, skipExisting: Boolean = false): Batch { diff --git a/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt b/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt index 377ea48e770..233ae91206d 100644 --- a/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt +++ b/build-logic/common-plugins/src/main/kotlin/PruneTestReportsTask.kt @@ -33,6 +33,11 @@ abstract class PruneTestReportsTask : DefaultTask() { } } +/** + * Prunes disposable IDE payloads from completed playbook runs while retaining their reports. Cleanup is + * serialized by a file lock, honors explicit artifact retention, and refuses to traverse symlinks or delete a + * checkout. + */ internal object TestReportRetention { fun prune(root: Path, keep: Int = 5): Int { require(keep >= 1) { "Keep at least one complete run" } diff --git a/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt b/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt index 5cf21dae235..e496c590ab1 100644 --- a/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt +++ b/build-logic/common-plugins/src/test/kotlin/ServerLogTailsTest.kt @@ -5,6 +5,11 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.StandardOpenOption.APPEND +/** + * Protects the development IDE log tailer from losing or replaying output as servers start and logs roll + * over. Uses real temporary files to check partial UTF-8 lines, bounded reads and exclusion of unrelated + * files or symlinks without launching an IDE. + */ class ServerLogTailsTest { @TempDir lateinit var root: Path diff --git a/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt b/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt index fe0c69921f7..7ff0cc3419f 100644 --- a/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt +++ b/build-logic/common-plugins/src/test/kotlin/TestReportRetentionTest.kt @@ -6,6 +6,11 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.attribute.FileTime +/** + * Verifies that playbook cleanup reclaims disposable IDE caches without deleting reports, pinned artifacts, + * unfinished runs or Git checkouts. Real directory and symlink fixtures protect the destructive cleanup + * boundary. + */ class TestReportRetentionTest { @TempDir lateinit var root: Path diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index c5948345490..18ab2a09dff 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -253,9 +253,27 @@ lang-jackson-bom = { module = "com.fasterxml.jackson:jackson-bom", version.ref = [bundles] # One library set for the distribution and embedded compiler hosts. xdk-libraries = [ - "xdk-aggregate", "xdk-cli", "xdk-collections", "xdk-convert", "xdk-crypto", - "xdk-ecstasy", "xdk-json", "xdk-jsondb", "xdk-metrics", "xdk-net", "xdk-oodb", - "xdk-runner", "xdk-sec", "xdk-web", "xdk-webauth", "xdk-webcli", "xdk-xenia", - "xdk-xml", "xdk-xunit", "xdk-xunit-db", "xdk-xunit-engine", "javatools-bridge", + "xdk-aggregate", + "xdk-cli", + "xdk-collections", + "xdk-convert", + "xdk-crypto", + "xdk-ecstasy", + "xdk-json", + "xdk-jsondb", + "xdk-metrics", + "xdk-net", + "xdk-oodb", + "xdk-runner", + "xdk-sec", + "xdk-web", + "xdk-webauth", + "xdk-webcli", + "xdk-xenia", + "xdk-xml", + "xdk-xunit", + "xdk-xunit-db", + "xdk-xunit-engine", + "javatools-bridge", ] unicode = ["jakarta-activation-api", "jakarta-xml-bind-api", "jaxb-runtime"] diff --git a/javatools/README.md b/javatools/README.md index fde2df41867..a381fed591f 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -7,8 +7,9 @@ compiler, IR, runtime, etc. This project uses the library produced by the "javatools_utils" project, and the `implicit.x` resource file from the "ecstasy" project. -Long term, this project will be used to help support IDE -plug-ins for common Java-based IDEs, such as IntelliJ IDEA. +The compiler also supports the compiler-backed language server. Start with +[Compiler architecture for language tooling](#compiler-architecture-for-language-tooling) for the +design rationale, API boundaries and compiler changes required by editor consumers. (Note: The test portion of this project may have dependencies on test frameworks.) @@ -31,6 +32,95 @@ Status: Suitable for use * AST nodes are multi-pass compiled (with optional re-queuing) via `org.xvm.compiler.ast.StageManager` +## Compiler architecture for language tooling + +The language server needs the compiler's answers while a user is editing: which declaration a +name binds to, which overload an argument fits, what type an expression has, and which diagnostics +belong to the current document revision. Running the command-line compiler and parsing its output +cannot supply those answers. Successful compilation also discards or rewrites some of the source +information that navigation needs; an unfinished buffer may never reach emission at all. + +This section and the detailed diagnostic guide below explain the design rationale and changes to +`javatools` that make this possible. The compiler remains independent of LSP, IntelliJ and VS Code. +It exposes compilation results and source facts; the adapter in `lang/lsp-server` converts those +facts into immutable editor models, applies document-version checks and implements the protocol. + +| Compiler mechanism | Why an editor needs it | Code and contract | +| --- | --- | --- | +| Owned diagnostics and explicit stopping | Associate every report with an attempt, retain speculative failures appropriately, and cancel obsolete work without manufacturing source errors. | `ErrorListener`, `ErrorList`, `Reporting`; [diagnostic design](#compiler-diagnostics-and-error-listeners). | +| Snapshot inputs and compilation outcomes | Compile unsaved module/member files together and distinguish successful output from ordinary partial progress after a failure. | `EmbeddingSupport` and the `ModuleInfo` source hooks; [host snapshots and outcomes](#compile-a-host-snapshot-and-inspect-its-outcome). | +| Selected semantic facts | Navigate exact overloads and argument slots without reproducing compiler resolution in the editor. Preserve references lost to constant folding. | `InvocationBinding`, `InitializerBinding` and `EmbeddingSupport.Compilation`. | +| Declaration and incomplete-source analysis | Answer bounded scope, type and argument queries while the buffer is unfinished, without presenting invalid source as executable code. | `EmbeddingSupport.analyzeDeclarations`, `analyzeIncomplete`, `CursorBinding` and `compiler.ast.partial`. | +| Source-tree copying | Probe proposed syntax on an independently parented tree without changing the source tree being inspected. | `AstNode.copyTree` and its covariant specializations. | +| Repository failure observations | Explain a required dependency that cannot be read while preserving repositories' existing best-effort search behavior. | `ModuleRepository`, `DirRepository`, `FileRepository` and the embedding dependency search. | +| Scoped compiler state and cache reporting | Release operation-owned listeners and temporary state, and replay cached diagnostics to the current attempt. | `Reporting`, `TransientThreadLocal` and `TypeInfo` diagnostic replay. | + +### Preserve selected semantic facts + +`InvocationBinding` records selected methods, function signatures, constructors and argument-to- +parameter mappings at the point where the compiler establishes them. The maps use AST identity; +they do not guess a target by spelling or make a dynamic function call look like a statically +selected method. `InitializerBinding` preserves validated source facts before a constant +initializer's temporary method is discarded. Without that step, a name can compile correctly but +disappear from references, rename and highlighting after constant folding. + +These facts belong to a compilation attempt. An immutable map does not make the contained AST, +type constants or constant pool safe to share across workers. The language adapter copies the +facts it needs into detached models before releasing the attempt. Keeping this boundary explicit +is essential for a long-running editor that repeatedly replaces analyses of unsaved text. + +### Analyze declarations and unfinished source explicitly + +`analyzeDeclarations` resolves declaration structure without promising a fully validated or +emitted module. `analyzeIncomplete` retains supported incomplete syntax and reports cursor facts +from its real lexical context. Its cursor overloads support a broader set of editing positions +than the deliberately narrow trailing-expression overload; their Javadoc defines each boundary. +Unsupported damage remains a diagnostic rather than a guessed semantic result. + +Incomplete nodes and their analysis helpers live in `org.xvm.compiler.ast.partial`: +`PartialQueries` coordinates inspection, `PartialCallResolver` and `PartialConstructionResolver` +fit candidates, `PartialArgument` builds disposable argument proposals, and `CursorScope` captures +visible names and resolves unfinished type headers. These helpers are package private. + +Java subpackages do not share package access. One internal bridge, `ast.PartialAst`, lets these +helpers reuse ordinary parenting, validation, argument fitting, construction preparation and +scope lookup. Existing AST members retain their protected or package-private visibility. The +bridge delegates compiler operations; the partial package decides which incomplete syntax and +candidate facts to retain. It neither introduces another type checker nor exposes compiler +contexts as a host API. Embedding hosts continue to consume `EmbeddingSupport` results. + +Candidate fitting uses ordinary compiler rules on trial syntax. A possible completion is not a +selected invocation, and partial analysis never supplies an executable module. This distinction +lets signature help and completion use authentic compiler information without weakening the +normal validation or emission contract. + +### Copy source trees and preserve ordinary compiler behavior + +The old pervasive `clone()` calls obscure an important distinction: `Object.clone()` starts with +a shallow field copy, while AST overrides selectively copy children and repair ownership. The +name alone does not tell a caller which bindings, transient state or parents remain shared, and +adding a field can silently change what a clone retains. A clone of a validated node is therefore +not equivalent to freshly parsed syntax. This is especially confusing for speculative editor +queries, which need to know exactly what a trial may mutate without affecting the original. + +`copyTree()` names the source-tree copy contract and gives expression/statement callers covariant +return types. Its contract covers child ownership, source metadata and the existing stage-aware +copy behavior; it is not a general deep copy of an entire compiler or constant pool. Specialized +incomplete nodes copy and adopt their children explicitly. The existing `clone()` entry point +continues to use the same copying machinery so the two APIs cannot drift independently. + +The compiler's callable typing and fitting corrections serve both ordinary compilation and +cursor probes. Boolean operand corrections preserve evaluation and side effects even when the +result is constant. These are compiler correctness rules, not alternate semantics enabled by an +LSP flag. The separate removal of the abandoned binary AST does not remove the source AST used +by tooling; legacy `.xtc` slots are still read or skipped as required for format compatibility. + +Repository observation is similarly additive to search behavior: an unreadable candidate can +still be skipped while another repository supplies the module. A host receives a useful cause +when a required dependency ultimately cannot be found. The diagnostic guide below explains the +intentional Java API migrations, the behavior retained for ordinary CLI use and the regression +evidence; it does not claim universal source or binary compatibility for old Java embedding code. + ## Compiler diagnostics and error listeners ### Why the old reporting model had to change @@ -366,12 +456,11 @@ Check failures and skips in `javatools/build/test-results/test/TEST-*.xml`; a gr not establish that optional tests executed. The real compiler-consumer integration test additionally exercises the standard build path with compiled modules supplied by its Gradle dependencies. -### Combined embedding review scope +### Embedding API boundaries -This combined change includes the listener contract and explicit-listener migration, scoped -parser/validation reporting, compilation outcomes, cached TypeInfo diagnostic replay, cancellation -and host-supplied module source snapshots. The shared behavior can be reviewed with its actual -host consumer tests rather than inferred across separate API migrations. +The embedding API combines the listener contract, scoped parser/validation reporting, compilation +outcomes, cached TypeInfo diagnostic replay, cancellation and host-supplied source snapshots. +Consumer tests exercise these mechanisms together through the public entry points. The independent [repository read-failure fix](https://github.com/xtclang/xvm/pull/683) supplies retained I/O causes; this API forwards them only after a required dependency search fails. @@ -379,9 +468,10 @@ retained I/O causes; this API forwards them only after a required dependency sea default-budget and lexical-rollback behavior preserved here. Empty transient-local retention is an independent utility fix. None of these APIs depends on an LSP implementation or editor. -Selected-call facts, declaration-only analysis, incomplete-source recovery, cursor completion, -AST copying and the production language server remain later review boundaries. The result here -exposes ordinary compilation progress, not recovered syntax or executable output from invalid code. +The ordinary compilation result exposes partial progress without treating invalid code as +executable output. Selected-call facts and the separate declaration/incomplete-analysis APIs +extend that model as described in the [architecture overview](#compiler-architecture-for-language-tooling). +The language server remains a consumer outside `javatools`. ### Scoped parser and validation reporting diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index 0b9247afcc3..f73d8386fae 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -54,7 +54,6 @@ import org.xvm.compiler.ast.InvocationExpression; import org.xvm.compiler.ast.NewExpression; import org.xvm.compiler.ast.PropertyDeclarationStatement; -import org.xvm.compiler.ast.Statement; import org.xvm.compiler.ast.StatementBlock; import org.xvm.compiler.ast.TypeCompositionStatement; import org.xvm.compiler.ast.partial.IncompleteStatement; @@ -147,7 +146,7 @@ public String err(Object value) { * @return the repository, or null if none of the directories exist */ private static ModuleRepository repoOver(File... dirs) { - List list = Arrays.stream(dirs) + var list = Arrays.stream(dirs) .filter(File::isDirectory) .map(dir -> new DirRepository(dir, true)) .toList(); @@ -198,11 +197,10 @@ public Footprint footprint() { * @return the snapshot; pool counts are zero if no file structure was built */ public Footprint footprint(Compilation compilation) { - ConstantPool pool = compilation == null ? null : compilation.pool(); - Runtime runtime = Runtime.getRuntime(); + var pool = compilation == null ? null : compilation.pool(); + var runtime = Runtime.getRuntime(); return new Footprint(cfgRepo == null ? 0 : cfgRepo.getModuleNames().size(), pool == null ? 0 : pool.size(), - pool == null ? 0 : pool.getInvalidationCount(), - runtime.totalMemory() - runtime.freeMemory()); + pool == null ? 0 : pool.getInvalidationCount(), runtime.totalMemory() - runtime.freeMemory()); } /** @@ -219,8 +217,8 @@ private boolean verifyConfigured() { // Configuring only lib/ produced a repository that could never compile anything: // every compile failed in prelinkSystemLibraries with "Unable to load module: // mack.xtclang.org", reported as an internal error with no location - File dirHome = new File(home); - ModuleRepository repo = repoOver(new File(dirHome, "lib"), new File(dirHome, "javatools")); + var dirHome = new File(home); + var repo = repoOver(new File(dirHome, "lib"), new File(dirHome, "javatools")); if (repo != null) { configure(repo, null); } @@ -278,13 +276,16 @@ public static EmbeddingSupport instance() { } /** - * Provide configuration necessary for the underlying Ecstasy tools and libraries. Must be - * called exactly one time before any other method. + * Provide configuration for the underlying Ecstasy tools and libraries before compiling or + * running a module. Repeating the same configuration is allowed; a conflicting configuration + * is rejected. Passive configuration and footprint queries do not require initialization. * * @param coreRepo the ModuleRepository to load the core libraries from * @param customInjector (optional) "module:class" name of a custom injector implementation to * use to provide injectable resources in lieu of the default injector * for this implementation + * + * @return this configured host support instance */ public EmbeddingSupport configure(ModuleRepository coreRepo, String customInjector) { synchronized (LOCK) { @@ -431,37 +432,73 @@ public record Compilation(ModuleStructure module, FileStructure file, StatementB initializerBindings = identitySnapshot(initializerBindings); } - /** Retain hosts that supply constructor bindings without folded initializer facts. */ + /** + * Retain hosts that supply constructor bindings without folded initializer facts. + * + * @param module the compiled module, or null when compilation did not succeed + * @param file the file structure produced by the attempt, or null if none was built + * @param ast the assembled source root, or null if loading or parsing failed + * @param sourceTrees the available per-source syntax trees, including recovered trees + * @param callBindings validated method-call facts keyed by invocation identity + * @param functionBindings validated function-call facts keyed by invocation identity + * @param constructorBindings validated constructor-call facts keyed by construction identity + */ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, - List sourceTrees, - Map callBindings, + List sourceTrees, Map callBindings, Map functionBindings, Map constructorBindings) { this(module, file, ast, sourceTrees, callBindings, functionBindings, constructorBindings, Map.of()); } - /** Retain hosts that supply method and function bindings. */ + /** + * Retain hosts that supply method and function bindings. + * + * @param module the compiled module, or null when compilation did not succeed + * @param file the file structure produced by the attempt, or null if none was built + * @param ast the assembled source root, or null if loading or parsing failed + * @param sourceTrees the available per-source syntax trees, including recovered trees + * @param callBindings validated method-call facts keyed by invocation identity + * @param functionBindings validated function-call facts keyed by invocation identity + */ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, - List sourceTrees, - Map callBindings, + List sourceTrees, Map callBindings, Map functionBindings) { this(module, file, ast, sourceTrees, callBindings, functionBindings, Map.of()); } - /** Retain hosts that supply only statically selected method calls. */ + /** + * Retain hosts that supply only statically selected method calls. + * + * @param module the compiled module, or null when compilation did not succeed + * @param file the file structure produced by the attempt, or null if none was built + * @param ast the assembled source root, or null if loading or parsing failed + * @param sourceTrees the available per-source syntax trees, including recovered trees + * @param callBindings validated method-call facts keyed by invocation identity + */ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, - List sourceTrees, - Map callBindings) { + List sourceTrees, Map callBindings) { this(module, file, ast, sourceTrees, callBindings, Map.of()); } - /** Retain the construction API for hosts supplying structural source trees. */ - public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, - List sourceTrees) { + /** + * Retain the construction API for hosts supplying structural source trees. + * + * @param module the compiled module, or null when compilation did not succeed + * @param file the file structure produced by the attempt, or null if none was built + * @param ast the assembled source root, or null if loading or parsing failed + * @param sourceTrees the available per-source syntax trees, including recovered trees + */ + public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast, List sourceTrees) { this(module, file, ast, sourceTrees, Map.of()); } - /** Retain the original construction API for hosts supplying one assembled tree. */ + /** + * Retain the original construction API for hosts supplying one assembled tree. + * + * @param module the compiled module, or null when compilation did not succeed + * @param file the file structure produced by the attempt, or null if none was built + * @param ast the assembled source root, or null if loading or parsing failed + */ public Compilation(ModuleStructure module, FileStructure file, StatementBlock ast) { this(module, file, ast, ast == null ? List.of() : List.of(ast)); } @@ -517,10 +554,7 @@ public StatementBlock parsed() { */ public Compilation compileModule(Source source, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(source, "source"); - return compileModule(listener -> { - StatementBlock tree = new Parser(source, listener).parseSource(); - return new ParsedSources(listener.hasSeriousErrors() ? null : tree, List.of(tree)); - }, input, errs); + return compileModule(listener -> ParsedSources.parse(source, listener), input, errs); } /** @@ -538,26 +572,30 @@ public Compilation compileModule(Source source, ModuleRepository input, @NotNull */ public Compilation compileModule(ModuleInfo sources, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(sources, "sources"); - return compileModule(listener -> { - Node root = sources.getSourceTree(listener); - return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), - sources.getParsedSources()); - }, input, errs); + return compileModule(listener -> ParsedSources.parse(sources, listener), input, errs); } /** * A successfully resolved declaration graph, before expression validation and code generation. * It contains no executable artifact. The calling worker exclusively owns these structures and * may inspect TypeInfo with its listener; ordinary compilation still validates every body. + * + * @param file the file structure containing the resolved declarations + * @param ast the assembled source tree + * @param sourceTrees the available per-source trees, retained in source order */ - public record DeclarationAnalysis(FileStructure file, StatementBlock ast, - List sourceTrees) { + public record DeclarationAnalysis(FileStructure file, StatementBlock ast, List sourceTrees) { public DeclarationAnalysis { requireNonNull(file, "file"); requireNonNull(ast, "ast"); sourceTrees = List.copyOf(sourceTrees); } + /** + * Obtain the pool that owns these resolved declarations. + * + * @return the constant pool owned by this declaration analysis + */ public ConstantPool pool() { return file.getConstantPool(); } @@ -572,13 +610,9 @@ public ConstantPool pool() { * @param errs the host listener * @return resolved declarations, never a compiled module */ - public Optional analyzeDeclarations(Source source, ModuleRepository input, - @NotNull ErrorListener errs) { + public Optional analyzeDeclarations(Source source, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(source, "source"); - return analyzeDeclarations(listener -> { - StatementBlock tree = new Parser(source, listener).parseSource(); - return new ParsedSources(listener.hasSeriousErrors() ? null : tree, List.of(tree)); - }, input, errs); + return analyzeDeclarations(listener -> ParsedSources.parse(source, listener), input, errs); } /** @@ -590,21 +624,15 @@ public Optional analyzeDeclarations(Source source, ModuleRe * @param errs the host listener * @return resolved declarations, empty after any declaration failure or cancellation */ - public Optional analyzeDeclarations(ModuleInfo sources, ModuleRepository input, - @NotNull ErrorListener errs) { + public Optional analyzeDeclarations(ModuleInfo sources, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(sources, "sources"); - return analyzeDeclarations(listener -> { - Node root = sources.getSourceTree(listener); - return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), - sources.getParsedSources()); - }, input, errs); + return analyzeDeclarations(listener -> ParsedSources.parse(sources, listener), input, errs); } private Optional analyzeDeclarations(Function parse, - ModuleRepository input, ErrorListener errs) { - CompilerAttempt attempt = runCompiler(parse, input, errs, CursorBinding.Collector.NONE, - CompilationGoal.DECLARATIONS); - EmbeddingCompiler compiler = attempt.compiler(); + ModuleRepository input, ErrorListener errs) { + var attempt = runCompiler(parse, input, errs, CursorBinding.Collector.NONE, CompilationGoal.DECLARATIONS); + var compiler = attempt.compiler(); return attempt.succeeded() ? Optional.of(new DeclarationAnalysis(compiler.file, compiler.ast, compiler.sourceTrees)) : Optional.empty(); @@ -642,7 +670,15 @@ public record PartialAnalysis(List sourceTrees, List sourceTrees, List sites, @Nullable ConstantPool pool, Map callBindings, @@ -650,15 +686,27 @@ public PartialAnalysis(List sourceTrees, List sourceTrees, List sites, @Nullable ConstantPool pool, Map callBindings) { this(sourceTrees, sites, pool, callBindings, Map.of()); } - /** Create partial facts with syntax and receiver information only. */ - public PartialAnalysis(List sourceTrees, List sites, - @Nullable ConstantPool pool) { + /** + * Create partial facts with syntax and receiver information only. + * + * @param sourceTrees the available per-source syntax trees, including recovered trees + * @param sites the retained incomplete source sites + * @param pool the attempt's pool, or null if semantic analysis did not start + */ + public PartialAnalysis(List sourceTrees, List sites, @Nullable ConstantPool pool) { this(sourceTrees, sites, pool, Map.of()); } } @@ -682,57 +730,68 @@ public PartialAnalysis(List sourceTrees, List { - StatementBlock tree = Parser.forPartialAnalysis(source, listener).parseSource(); + var tree = Parser.forPartialAnalysis(source, listener).parseSource(); return new ParsedSources(tree, List.of(tree)); }, Parser.UNEXPECTED_EOF, input, errs); } /** - * Analyze an incomplete member access/call at an explicit source position, including before - * existing closing braces or a semicolon. Source text is unchanged and later declarations - * retain their positions. The position must come from {@link Source#getPosition()} for this text. - * Supports standalone statements, simple assignment/initializer values, single return values, - * and final nested call arguments, including named arguments and existing closing parentheses. - * A cursor at the end of a written member token retains that token as a completion prefix. - * A cursor before a call's closing parenthesis inspects its receiver and written arguments, - * even when the selected syntax is complete. The selected member/call itself is not validated. - * Bare-name prefixes and empty statement boundaries expose visible scope; a final named argument - * awaiting its value retains its label. Candidate signatures can infer types from written arguments. - * Compound/conditional values and arguments following the cursor retain their real contexts. - * Constructors reuse normal receiver/type preparation, including virtual/inner/annotated types, - * required-type and argument-derived class inference, and parenthesized array initializers. - * Missing call/group/tuple parentheses and index/list/map brackets are retained at statement or - * outer-delimiter boundaries. Declaration values retain missing terminators; parameter defaults - * may retain a missing closing parenthesis before a body. An explicit cursor at EOF can also - * retain missing block braces. This does not invent operands, declaration names/types or literal - * contents, or repair unrelated errors. Anonymous construction prepares the retained declaration - * and fits its own or superclass constructors; it does not emit forwarding constructors or run - * capture analysis. Single-dimensional array brackets fit the size parameter of the fixed-size - * constructor, including an empty/final-name slot and a missing closing bracket. A following - * supplier is parsed for recovery but is outside this prefix proof. Multidimensional - * construction remains unsupported. Member/return and method-parameter type prefixes query - * types through the enclosing compiler scope. Flat qualified names resolve their qualifier - * and visible nested types, replacing only the final written identifier. Unqualified empty - * parameter slots are also supported. An unfinished header retains its written declaration - * name and range but registers no method/property component or parameters. Written leaf - * names inside parameterized and compound types also participate, including bounded missing - * angle/group closers. These are visible-type suggestions, not proof of generic constraints. - * Empty type/qualified slots, selected qualifier tokens, parameterized qualifiers, generic - * base names and function/sequence types are supported. Generic-method, multiple-return and - * type-composition headers query their enclosing scope; unregistered formals hide outer names - * without acquiring invented identities. A root module header registers only its written - * namespace/core import; its incomplete body and compositions cannot emit or contribute facts. - * Other syntax errors prevent semantic analysis; cursors outside supported boundaries yield no site. + * Analyze an incomplete member access or call at an explicit source position, including before + * existing closing braces or a semicolon. Source text is unchanged and later declarations retain + * their positions. The cursor must come from {@link Source#getPosition()} for this exact text. + * + *

Expression queries support standalone statements, assignment and initializer values, single + * return values, and nested call arguments, including named arguments and existing closing + * parentheses. Member tokens retain their completion prefix. A cursor before a call's closing + * parenthesis inspects its receiver and written arguments even when the selected syntax is + * complete. The selected member or call itself is not validated. Bare-name prefixes and empty + * statement boundaries expose visible scope; a named argument awaiting its value retains its + * label. Candidate signatures infer types from written arguments. Compound and conditional + * values, and arguments following the cursor, retain their real contexts. + * + *

Recovery retains missing call/group/tuple parentheses and index/list/map brackets at + * statement or outer-delimiter boundaries. Declaration values can retain missing terminators, + * parameter defaults can retain a missing closing parenthesis before a body, and a cursor at + * EOF can retain missing block braces. Recovery does not invent operands, declaration names or + * types, or literal contents, and does not repair unrelated errors. + * + *

Construction queries reuse normal receiver/type preparation, including virtual, inner and + * annotated types, required-type and argument-derived class inference, and parenthesized array + * initializers. Anonymous construction prepares the retained declaration and fits its own or + * superclass constructors without emitting forwarding constructors or running capture analysis. + * Single-dimensional array brackets fit the fixed-size constructor's size parameter, including + * an empty/final-name slot and a missing closing bracket. A following supplier is parsed for + * recovery but is outside this prefix proof. Multidimensional construction remains unsupported. + * + *

Declaration queries resolve member, return and method-parameter type prefixes through the + * enclosing compiler scope. Flat qualified names resolve their qualifier and visible nested + * types, replacing only the final written identifier. Unqualified empty parameter slots are + * supported. An unfinished header retains its written name and range but registers no method, + * property component or parameters. Written leaf names inside parameterized and compound types + * participate, including bounded missing angle/group closers; these are visible-type suggestions, + * not proof of generic constraints. Empty type/qualified slots, selected qualifier tokens, + * parameterized qualifiers, generic base names and function/sequence types are supported. + * + *

Generic-method, multiple-return and type-composition headers query their enclosing scope. + * Unregistered formals hide outer names without acquiring invented identities. A root module + * header registers only its written namespace/core import; its incomplete body and compositions + * cannot emit or contribute facts. Other syntax errors prevent semantic analysis, and cursors + * outside supported boundaries yield no site. Partial facts never promise executable output. + * + * @param source a fresh named source whose text remains unchanged during this attempt + * @param cursor the encoded source position at which to inspect incomplete syntax + * @param input compiled dependencies, or null to use only the configured core repository + * @param errs the host's diagnostic listener and abort policy + * + * @return retained source trees and available cursor/binding facts, without an executable module */ - public PartialAnalysis analyzeIncomplete(Source source, long cursor, ModuleRepository input, - @NotNull ErrorListener errs) { + public PartialAnalysis analyzeIncomplete(Source source, long cursor, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(source, "source"); return analyzeIncomplete(listener -> { - StatementBlock tree = Parser.forPartialAnalysis(source, cursor, listener).parseSource(); + var tree = Parser.forPartialAnalysis(source, cursor, listener).parseSource(); return new ParsedSources(tree, List.of(tree)); }, Parser.INCOMPLETE_EXPRESSION, input, errs); } @@ -743,15 +802,22 @@ public PartialAnalysis analyzeIncomplete(Source source, long cursor, ModuleRepos * The cursor refers to the selected file's snapshot, which may contain unsaved text. Neither * this ModuleInfo nor its parsed trees may be reused for another attempt or normal compilation. * Only the recognized cursor diagnostic is deferred during assembly; other source errors abort it. + * + * @param sources a fresh module source snapshot for this attempt + * @param sourceFile the file in that snapshot containing the cursor + * @param cursor the selected position in the owning Source snapshot + * @param input the optional repository of compiled dependencies + * @param errs the diagnostic listener and abort policy for this operation + * + * @return retained syntax and any facts proved at the cursor; never a compiled module */ public PartialAnalysis analyzeIncomplete(ModuleInfo sources, File sourceFile, long cursor, ModuleRepository input, @NotNull ErrorListener errs) { requireNonNull(sources, "sources"); - String name = resolveFile(requireNonNull(sourceFile, "sourceFile")).getPath(); + var name = resolveFile(requireNonNull(sourceFile, "sourceFile")).getPath(); return analyzeIncomplete(listener -> { - ErrorListener assembly = ErrorListener.cancellable( - ErrorListener.collecting(listener::log), listener::isAbortDesired); - Node root = sources.getSourceTree(assembly, (source, nodeErrors) -> { + var assembly = ErrorListener.cancellable(ErrorListener.collecting(listener::log), listener::isAbortDesired); + var root = sources.getSourceTree(assembly, (source, nodeErrors) -> { if (!name.equals(source.getFileName())) { return new Parser(source, nodeErrors).parseSource(); } @@ -764,23 +830,21 @@ public PartialAnalysis analyzeIncomplete(ModuleInfo sources, File sourceFile, lo }), listener::isAbortDesired); return Parser.forPartialAnalysis(source, cursor, partial).parseSource(); }); - return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), - sources.getParsedSources()); + return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), sources.getParsedSources()); }, Parser.INCOMPLETE_EXPRESSION, input, errs); } - private PartialAnalysis analyzeIncomplete(Function parse, - String boundaryCode, ModuleRepository input, - ErrorListener errs) { + private PartialAnalysis analyzeIncomplete(Function parse, String boundaryCode, + ModuleRepository input, ErrorListener errs) { verifyConfigured(); requireNonNull(errs, "errs"); Set delivered = new HashSet<>(); - ErrorListener host = ErrorListener.cancellable(ErrorListener.collecting(error -> { + var host = ErrorListener.cancellable(ErrorListener.collecting(error -> { if (delivered.add(error.genUID())) { errs.log(error); } }), errs::isAbortDesired); - ErrorList syntaxErrors = new ErrorList(ErrorList.UNLIMITED); + var syntaxErrors = new ErrorList(ErrorList.UNLIMITED); ParsedSources parsed; try { if (host.isAbortDesired()) { @@ -804,7 +868,7 @@ private PartialAnalysis analyzeIncomplete(Function } var cursors = new CursorBinding.Collector(); - Compilation attempt = compileModule(listener -> parsed, input, host, cursors); + var attempt = compileModule(listener -> parsed, input, host, cursors); // Anonymous construction can replace its deferred body with an owned class/validation // clone. Publish only surviving syntax, never the pre-validation cursor identity. // Primary-constructor defaults can expose the same surviving node through both the @@ -816,8 +880,7 @@ private PartialAnalysis analyzeIncomplete(Function /** Parsed children exist before parent links; expose the innermost unfinished operations. */ private static Stream incompleteSites(AstNode node) { - var nested = PartialSyntax.children(node) - .flatMap(EmbeddingSupport::incompleteSites); + var nested = PartialSyntax.children(node).flatMap(EmbeddingSupport::incompleteSites); if (node instanceof IncompleteStatement site) { var descendants = nested.toList(); return descendants.isEmpty() ? Stream.of(site) : descendants.stream(); @@ -825,36 +888,49 @@ private static Stream incompleteSites(AstNode node) { return nested; } - /** An assembled tree is available only when parsing/loading succeeded. */ /** Preserve node identity, reject null entries and detach from the collector. */ private static Map identitySnapshot(Map source) { - Map copy = new IdentityHashMap<>(); + var copy = new IdentityHashMap(); source.forEach((key, value) -> copy.put(requireNonNull(key), requireNonNull(value))); return Collections.unmodifiableMap(copy); } - private record ParsedSources(StatementBlock root, List sources) {} + /** Available source trees, with an assembled root only when parsing and loading succeeded. */ + private record ParsedSources(StatementBlock root, List sources) { + /** Keep recovered syntax for diagnostics even when errors prevent compiling the root. */ + static ParsedSources parse(Source source, ErrorListener errs) { + var tree = new Parser(source, errs).parseSource(); + return new ParsedSources(errs.hasSeriousErrors() ? null : tree, List.of(tree)); + } - private Compilation compileModule(Function parse, - ModuleRepository input, ErrorListener errs) { + /** Assemble the module before reading the per-source trees populated during parsing. */ + static ParsedSources parse(ModuleInfo sources, ErrorListener errs) { + var root = sources.getSourceTree(errs); + return new ParsedSources(root == null ? null : (StatementBlock) root.ast(), sources.getParsedSources()); + } + } + + private Compilation compileModule(Function parse, ModuleRepository input, ErrorListener errs) { return compileModule(parse, input, errs, CursorBinding.Collector.NONE); } private Compilation compileModule(Function parse, - ModuleRepository input, ErrorListener errs, CursorBinding.Collector cursors) { + ModuleRepository input, ErrorListener errs, CursorBinding.Collector cursors) { return runCompiler(parse, input, errs, cursors, CompilationGoal.MODULE).compiler().result(); } + /** Selects declaration resolution alone or the full validation and emission pipeline. */ private enum CompilationGoal { DECLARATIONS, MODULE } + /** Keeps partial compiler state alongside whether the requested goal completed successfully. */ private record CompilerAttempt(EmbeddingCompiler compiler, boolean succeeded) {} private CompilerAttempt runCompiler(Function parse, - ModuleRepository input, ErrorListener errs, - CursorBinding.Collector cursors, CompilationGoal goal) { + ModuleRepository input, ErrorListener errs, + CursorBinding.Collector cursors, CompilationGoal goal) { verifyConfigured(); requireNonNull(errs, "errs"); - EmbeddingCompiler compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs, cursors, goal); + var compiler = new EmbeddingCompiler(parse, input, cfgRepo, errs, cursors, goal); try { return new CompilerAttempt(compiler, !errs.isAbortDesired() && compiler.process() == 0 && !errs.hasSeriousErrors() && !errs.isAbortDesired()); @@ -973,7 +1049,7 @@ protected EmbeddingCompiler(Function parse, @Override protected int process() { - ModuleRepository repoLib = ensureLibraryRepo(); + var repoLib = ensureLibraryRepo(); checkErrors("repository setup"); prelinkSystemLibraries(repoLib); @@ -983,9 +1059,8 @@ protected int process() { try { // The launcher stops the next compilation stage on any error. Parsing can still // recover within its own stage, subject to the host's budget and cancellation. - ErrorListener errs = ErrorListener.cancellable( - ErrorListener.collecting(this::log), m_errors::isAbortDesired); - ParsedSources parsed = parse.apply(errs); + var errs = ErrorListener.cancellable(ErrorListener.collecting(this::log), m_errors::isAbortDesired); + var parsed = parse.apply(errs); block = this.ast = parsed.root(); this.sourceTrees = parsed.sources(); } catch (CompilerException e) { @@ -1004,15 +1079,14 @@ protected int process() { block.log(this, ERROR, ERR_MODULE_SOURCE); return checkErrors("source parsing"); } - Statement stmt = block.getStatements().getLast(); - if (!(stmt instanceof TypeCompositionStatement stmtModule) || - stmtModule.getCategory().getId() != Id.MODULE) { + var stmt = block.getStatements().getLast(); + if (!(stmt instanceof TypeCompositionStatement stmtModule) || stmtModule.getCategory().getId() != Id.MODULE) { stmt.log(this, ERROR, ERR_MODULE_SOURCE); return checkErrors("source parsing"); } - Compiler compiler = new Compiler(stmtModule, this, bindings, cursors); - FileStructure struct = compiler.generateInitialFileStructure(); + var compiler = new Compiler(stmtModule, this, bindings, cursors); + var struct = compiler.generateInitialFileStructure(); this.file = struct; if (struct == null || checkErrors("module creation") != 0) { return 1; @@ -1044,7 +1118,7 @@ protected int compile(List compilers, ModuleRepository repoLib) { @Override protected ModuleRepository configureLibraryRepo(List ignore) { - BuildRepository build = new BuildRepository(); + var build = new BuildRepository(); return inRepo == null || inRepo == coreRepo ? new LinkedRepository(true, build, coreRepo) : new LinkedRepository(true, build, inRepo, coreRepo); @@ -1277,8 +1351,8 @@ public Control runMethod( verifyConfigured(); requireNonNull(errs, "errs"); - ModuleRepository repository = new LinkedRepository(input, cfgRepo); - ModuleStructure module = version == null + var repository = new LinkedRepository(input, cfgRepo); + var module = version == null ? repository.loadModule(moduleName) : repository.loadModule(moduleName, version, true); if (module == null) { @@ -1291,7 +1365,7 @@ public Control runMethod( } try { - Connector connector = ensureConnector(); + var connector = ensureConnector(); return useJit() ? JitControl.create(connector, module, repository, console, rootDir, errs) : InterpreterControl.create(connector, module, repository, entry, console, diff --git a/javatools/src/main/java/org/xvm/asm/Annotation.java b/javatools/src/main/java/org/xvm/asm/Annotation.java index 1666c42782d..b14f8dc0b96 100644 --- a/javatools/src/main/java/org/xvm/asm/Annotation.java +++ b/javatools/src/main/java/org/xvm/asm/Annotation.java @@ -321,8 +321,7 @@ public boolean validate(ErrorListener errs) { // it must be an annotation type if (getAnnotationType().getExplicitClassFormat() != Component.Format.ANNOTATION) { - log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, - getAnnotationClass().getValueString()); + log(errs, Severity.ERROR, VE_CLASS_NOT_ANNOTATION, getAnnotationClass().getValueString()); fHalt |= errs.isAbortDesired(); } diff --git a/javatools/src/main/java/org/xvm/asm/ConstantPool.java b/javatools/src/main/java/org/xvm/asm/ConstantPool.java index 69d3ad8fdc4..8f18fa229a6 100755 --- a/javatools/src/main/java/org/xvm/asm/ConstantPool.java +++ b/javatools/src/main/java/org/xvm/asm/ConstantPool.java @@ -1277,7 +1277,11 @@ public static String getImplicitImportName(String sPath) { return IMPLICITS.byPath().get(sPath); } - /** The language's implicit import names, without resolving or loading their components. */ + /** + * The language's implicit import names, without resolving or loading their components. + * + * @return the names available through implicit imports + */ public static Set getImplicitImportNames() { return IMPLICITS.byName().keySet(); } diff --git a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java index 4ef470bc56d..3213373d95f 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java +++ b/javatools/src/main/java/org/xvm/asm/constants/MethodInfo.java @@ -1353,6 +1353,10 @@ public boolean hasSuper(TypeInfo infoType) { * The written method body selected by super dispatch, when there is one. Field, native, * delegation and into-constraint bodies have no source method target. Inspection uses the * same chain selection as getSuper(), without generating or optimizing forwarding code. + * + * @param infoType the enclosing type information used to locate the inherited method + * + * @return the written super-dispatch target, or empty when no source method represents it */ public Optional getSuperMethod(TypeInfo infoType) { MethodStructure method = getTopmostMethodStructure(infoType); diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index 2daf2df16ee..d6ca8204136 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -32,14 +32,26 @@ public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs) { this(stmtModule, errs, InvocationBinding.Collector.NONE); } - /** Construct a compiler that records call facts in the supplied attempt-owned collector. */ - public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, - InvocationBinding.Collector bindings) { + /** + * Construct a compiler that records call facts in the supplied attempt-owned collector. + * + * @param stmtModule the assembled module declaration to compile + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + */ + public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, InvocationBinding.Collector bindings) { this(stmtModule, errs, bindings, CursorBinding.Collector.NONE); } - public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, - InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { + /** + * Construct a module compiler using the supplied attempt-owned fact collectors. + * + * @param stmtModule the assembled module declaration to compile + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection + */ + public Compiler(TypeCompositionStatement stmtModule, ErrorListener errs, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { if (stmtModule == null) { throw new IllegalArgumentException("AST node for module required"); } diff --git a/javatools/src/main/java/org/xvm/compiler/CursorBinding.java b/javatools/src/main/java/org/xvm/compiler/CursorBinding.java index 611f6ec89a0..1b83cdf24f5 100644 --- a/javatools/src/main/java/org/xvm/compiler/CursorBinding.java +++ b/javatools/src/main/java/org/xvm/compiler/CursorBinding.java @@ -16,7 +16,25 @@ import org.xvm.compiler.ast.AstNode; import org.xvm.compiler.ast.partial.IncompleteStatement; -/** Facts captured at an explicit cursor while its real validation context is alive. */ + +/** + * Facts captured at an explicit cursor while its real validation context is alive. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param formals written formal names and constraints, without invented declaration identities + * @param argumentLiterals literal spellings validated as candidate argument insertions + * @param argumentExpressions enclosing-instance expressions validated as candidate argument insertions + * @param enclosingExpressions enclosing-instance expressions available in the current value context + * @param argumentTemplates compiler-fitted source templates for the argument slot + */ public record CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -37,7 +55,23 @@ public record CursorBinding(List variables, TypeConstant thisType, boo argumentTemplates = List.copyOf(argumentTemplates); } - /** Retain ordinary-expression consumers predating compiler-fitted value templates. */ + /** + * Retain ordinary-expression consumers predating compiler-fitted value templates. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param formals written formal names and constraints, without invented declaration identities + * @param argumentLiterals literal spellings validated as candidate argument insertions + * @param argumentExpressions enclosing-instance expressions validated as candidate argument insertions + * @param enclosingExpressions enclosing-instance expressions available in the current value context + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -47,13 +81,35 @@ public CursorBinding(List variables, TypeConstant thisType, boolean in argumentProperties, formals, argumentLiterals, argumentExpressions, enclosingExpressions, List.of()); } + /** + * Replace argument templates while preserving scope and other candidate facts. + * + * @param templates replacement compiler-fitted argument templates + * + * @return a new binding containing the replacement templates + */ public CursorBinding withArgumentTemplates(List templates) { return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, enclosingExpressions, templates); } - /** Preserve the previous constructor while adding ordinary-expression proposals. */ + /** + * Preserve the previous constructor while adding ordinary-expression proposals. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param formals written formal names and constraints, without invented declaration identities + * @param argumentLiterals literal spellings validated as candidate argument insertions + * @param argumentExpressions enclosing-instance expressions validated as candidate argument insertions + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -63,12 +119,33 @@ public CursorBinding(List variables, TypeConstant thisType, boolean in argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, List.of()); } + /** + * Replace enclosing-instance value proposals while preserving call facts. + * + * @param expressions replacement compiler-validated enclosing-instance expressions + * + * @return a new binding containing the replacement enclosing-instance expressions + */ public CursorBinding withEnclosingExpressions(List expressions) { return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, expressions, argumentTemplates); } - /** Retain callers predating enclosing-instance insertion proposals. */ + /** + * Retain callers predating enclosing-instance insertion proposals. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param formals written formal names and constraints, without invented declaration identities + * @param argumentLiterals literal spellings validated as candidate argument insertions + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -77,7 +154,20 @@ public CursorBinding(List variables, TypeConstant thisType, boolean in argumentValues, argumentProperties, formals, argumentLiterals, List.of()); } - /** Retain callers predating literal insertion proposals. */ + /** + * Retain callers predating literal insertion proposals. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param formals written formal names and constraints, without invented declaration identities + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -86,7 +176,19 @@ public CursorBinding(List variables, TypeConstant thisType, boolean in argumentValues, argumentProperties, formals, List.of()); } - /** Retain the original complete constructor for existing embedding consumers. */ + /** + * Retain the original complete constructor for existing embedding consumers. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues, @@ -98,8 +200,19 @@ public CursorBinding(List variables, TypeConstant thisType, boolean in /** * A written formal, not an invented declaration identity. A recursive written constraint has * no resolved type until its declaration exists; only that case supplies text instead. + * + * @param name the written formal name token + * @param constraint the resolved constraint, or null when only its recursive source spelling is + * available + * @param writtenConstraint the recursive source spelling, or null when the constraint is resolved */ public record Formal(Token name, TypeConstant constraint, String writtenConstraint) { + /** + * Capture a written formal with a resolved constraint. + * + * @param name the written formal name token + * @param constraint the resolved formal constraint + */ public Formal(Token name, TypeConstant constraint) { this(name, constraint, null); } @@ -111,43 +224,99 @@ public Formal(Token name, TypeConstant constraint) { } } + /** + * Replace written formals while preserving scope and call facts. + * + * @param formals written formal names and constraints, without invented declaration identities + * + * @return a new binding containing the replacement written formals + */ public CursorBinding withFormals(List formals) { return new CursorBinding(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, argumentProperties, formals, argumentLiterals, argumentExpressions, enclosingExpressions, argumentTemplates); } - /** Retain variable-completion callers. Record patterns must also include argument properties. */ + /** + * Retain variable-completion callers. Record patterns must also include argument properties. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions, List argumentValues) { this(variables, thisType, instance, types, candidates, callsInspected, functions, argumentValues, List.of()); } - /** Retain signature-only callers. */ + /** + * Retain signature-only callers. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected, List functions) { this(variables, thisType, instance, types, candidates, callsInspected, functions, List.of()); } - /** Retain callers that only consume method candidates. */ + /** + * Retain callers that only consume method candidates. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param candidates methods whose signatures fit the written arguments + * @param callsInspected whether call fitting ran, distinguishing rejection from an uninspected call + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, List candidates, boolean callsInspected) { this(variables, thisType, instance, types, candidates, callsInspected, List.of()); } + /** + * Capture visible variables and receiver context without inspecting calls. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance) { this(variables, thisType, instance, List.of(), List.of(), false); } - /** Group call-specific facts while retaining the existing constructors and record components. */ + /** + * Group call-specific facts while retaining the existing constructors and record components. + * + * @param variables visible variables with their narrowed types and read permissions + * @param thisType the enclosing receiver type, or null when no receiver is available + * @param instance whether the current scope permits instance access + * @param types compiler-resolved type names visible at the cursor + * @param calls the inspected call candidates and insertion facts + */ public CursorBinding(List variables, TypeConstant thisType, boolean instance, List types, CallFacts calls) { this(variables, thisType, instance, types, calls.candidates(), calls.inspected(), calls.functions(), calls.argumentValues(), calls.argumentProperties(), List.of(), calls.argumentLiterals(), calls.argumentExpressions()); } - /** A grouped immutable view; no context, mutable operation or AST child is introduced. */ + /** + * A grouped immutable view; no context, mutable operation or AST child is introduced. + * + * @return an immutable grouped view of the current call candidates and insertion facts + */ public CallFacts callFacts() { return new CallFacts(candidates, callsInspected, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); } @@ -156,34 +325,79 @@ private CursorBinding withCallFacts(CallFacts calls) { return new CursorBinding(variables, thisType, instance, types, calls).withFormals(formals).withEnclosingExpressions(enclosingExpressions).withArgumentTemplates(argumentTemplates); } + /** + * Replace method candidates and mark the call as inspected. + * + * @param candidates methods whose signatures fit the written arguments + * + * @return a new binding with the replacement methods and call inspection recorded + */ public CursorBinding withCandidates(List candidates) { return withCallFacts(callFacts().withCandidates(candidates)); } + /** + * Replace function candidates and mark the call as inspected. + * + * @param functions function signatures whose written arguments fit + * + * @return a new binding with the replacement functions and call inspection recorded + */ public CursorBinding withFunctions(List functions) { return withCallFacts(callFacts().withFunctions(functions)); } + /** + * Replace visible type names while preserving scope and call facts. + * + * @param types compiler-resolved type names visible at the cursor + * + * @return a new binding containing the replacement visible type names + */ public CursorBinding withTypes(List types) { return new CursorBinding(variables, thisType, instance, types, callFacts()).withFormals(formals).withEnclosingExpressions(enclosingExpressions).withArgumentTemplates(argumentTemplates); } - /** Readable source variables whose proposed insertion fits at least one incomplete-call candidate. */ + /** + * Readable source variables whose proposed insertion fits at least one incomplete-call candidate. + * + * @param values replacement readable variable insertions that fit a call candidate + * + * @return a new binding containing the replacement variable insertions + */ public CursorBinding withArgumentValues(List values) { return withCallFacts(callFacts().withArgumentValues(values)); } - /** Implicit property/constant reads whose insertion fits at least one incomplete-call candidate. */ + /** + * Implicit property/constant reads whose insertion fits at least one incomplete-call candidate. + * + * @param properties replacement property insertions that fit a call candidate + * + * @return a new binding containing the replacement property insertions + */ public CursorBinding withArgumentProperties(List properties) { return withCallFacts(callFacts().withArgumentProperties(properties)); } - /** Literal source spellings whose insertion fits and validates with the other written arguments. */ + /** + * Literal source spellings whose insertion fits and validates with the other written arguments. + * + * @param literals replacement validated literal source spellings + * + * @return a new binding containing the replacement literal insertions + */ public CursorBinding withArgumentLiterals(List literals) { return withCallFacts(new CallFacts(candidates, callsInspected, functions, argumentValues, argumentProperties, literals, argumentExpressions)); } - /** Compiler-validated enclosing-instance source expressions, distinct from literals. */ + /** + * Compiler-validated enclosing-instance source expressions, distinct from literals. + * + * @param expressions replacement compiler-validated enclosing-instance expressions + * + * @return a new binding containing the replacement expression insertions + */ public CursorBinding withArgumentExpressions(List expressions) { return withCallFacts(new CallFacts(candidates, callsInspected, functions, argumentValues, argumentProperties, argumentLiterals, expressions)); @@ -194,6 +408,14 @@ public CursorBinding withArgumentExpressions(List expressions) { * An inspected empty candidate list means rejection, not absence of inspection. Updating * argument values, properties or literals preserves that distinction. Previous constructors * remain available; record patterns must include the literal-proposal component. + * + * @param candidates methods whose signatures fit the written arguments + * @param inspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param argumentLiterals literal spellings validated as candidate argument insertions + * @param argumentExpressions enclosing-instance expressions validated as candidate argument insertions */ public record CallFacts(List candidates, boolean inspected, List functions, List argumentValues, @@ -207,30 +429,77 @@ public record CallFacts(List candidates, boolean inspected, argumentExpressions = List.copyOf(argumentExpressions); } + /** + * Capture call candidates and insertion facts; omitted proposal kinds remain empty. + * + * @param candidates methods whose signatures fit the written arguments + * @param inspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + * @param argumentLiterals literal spellings validated as candidate argument insertions + */ public CallFacts(List candidates, boolean inspected, List functions, List argumentValues, List argumentProperties, List argumentLiterals) { this(candidates, inspected, functions, argumentValues, argumentProperties, argumentLiterals, List.of()); } + /** + * Capture call candidates and insertion facts; omitted proposal kinds remain empty. + * + * @param candidates methods whose signatures fit the written arguments + * @param inspected whether call fitting ran, distinguishing rejection from an uninspected call + * @param functions function signatures whose written arguments fit + * @param argumentValues readable variables whose insertion fits a call candidate + * @param argumentProperties property reads whose insertion fits a call candidate + */ public CallFacts(List candidates, boolean inspected, List functions, List argumentValues, List argumentProperties) { this(candidates, inspected, functions, argumentValues, argumentProperties, List.of()); } + /** + * Replace method candidates and mark the call as inspected. + * + * @param candidates methods whose signatures fit the written arguments + * + * @return new call facts with the replacement methods and inspection recorded + */ public CallFacts withCandidates(List candidates) { return new CallFacts(candidates, true, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); } + /** + * Replace function candidates and mark the call as inspected. + * + * @param functions function signatures whose written arguments fit + * + * @return new call facts with the replacement functions and inspection recorded + */ public CallFacts withFunctions(List functions) { return new CallFacts(candidates, true, functions, argumentValues, argumentProperties, argumentLiterals, argumentExpressions); } + /** + * Replace variable insertions without changing whether the call was inspected. + * + * @param values replacement readable variable insertions that fit a call candidate + * + * @return new call facts containing the replacement variable insertions + */ public CallFacts withArgumentValues(List values) { return new CallFacts(candidates, inspected, functions, values, argumentProperties, argumentLiterals, argumentExpressions); } + /** + * Replace property insertions without changing whether the call was inspected. + * + * @param properties replacement property insertions that fit a call candidate + * + * @return new call facts containing the replacement property insertions + */ public CallFacts withArgumentProperties(List properties) { return new CallFacts(candidates, inspected, functions, argumentValues, properties, argumentLiterals, argumentExpressions); } @@ -238,29 +507,61 @@ public CallFacts withArgumentProperties(List properties) { /** A compiler-resolved type name; type preserves parameterized qualifier substitution. */ public record NamedType(String name, IdentityConstant identity, TypeConstant type) { + /** + * Capture a type name using its declaration's own type without qualifier substitution. + * + * @param name the source-visible type name + * @param identity the compiler-resolved declaration identity + */ public NamedType(String name, IdentityConstant identity) { this(name, identity, identity.getType()); } } - /** Fits the written arguments; missing arguments cannot establish a selected overload. */ + /** + * Fits the written arguments; missing arguments cannot establish a selected overload. + * + * @param method the compiler-selected method identity + * @param signature the instantiated signature fitted at this source site + * @param arguments written argument spans associated with visible parameter indices + * @param converting whether fitting requires argument conversion + * @param receiverArgument whether the explicit receiver occupies an argument slot + */ public record Candidate(MethodConstant method, SignatureConstant signature, List arguments, boolean converting, boolean receiverArgument) { public Candidate { arguments = List.copyOf(arguments); } + /** + * Capture a fitted method candidate without a receiver argument. + * + * @param method the compiler-selected method identity + * @param signature the instantiated signature fitted at this source site + * @param arguments source-to-parameter associations for the written arguments + * @param converting whether fitting requires argument conversion + */ public Candidate(MethodConstant method, SignatureConstant signature, List arguments, boolean converting) { this(method, signature, arguments, converting, false); } + /** + * Mark the receiver as occupying a call argument slot. + * + * @return a new candidate whose receiver occupies an argument slot + */ public Candidate withReceiverArgument() { return new Candidate(method, signature, arguments, converting, true); } } - /** A function type whose written arguments fit; there is no selected method or parameter names. */ + /** + * A function type whose written arguments fit; there is no selected method or parameter names. + * + * @param type the compiler-resolved value or function type + * @param arguments written argument spans associated with visible parameter indices + */ public record FunctionCandidate(TypeConstant type, List arguments) { public FunctionCandidate { arguments = List.copyOf(arguments); @@ -275,6 +576,7 @@ public record Property(String name, PropertyConstant identity, TypeConstant type /** Attempt-owned scratch space; no context or collector is stored on a syntax node. */ public static final class Collector { + /** Create an enabled cursor-fact collector for one compilation attempt. */ public Collector() { this(true); } @@ -283,24 +585,45 @@ private Collector(boolean enabled) { f_enabled = enabled; } + /** + * Determine whether this attempt requests cursor facts. + * + * @return whether this collector records cursor facts + */ public boolean isEnabled() { return f_enabled; } - /** A validation retry must not retain facts from its earlier attempt. */ + /** + * A validation retry must not retain facts from its earlier attempt. + * + * @param site the retained incomplete source site + */ public void begin(IncompleteStatement site) { if (f_enabled) { f_bindings.remove(site); } } + /** + * Retain the facts captured for this incomplete source site. + * + * @param site the retained incomplete source site + * @param binding the successfully established facts for this source node + */ public void record(IncompleteStatement site, CursorBinding binding) { if (f_enabled) { f_bindings.put(site, binding); } } - /** Discard trial clones and release scratch entries when publishing surviving syntax. */ + /** + * Discard trial clones and release scratch entries when publishing surviving syntax. + * + * @param roots the surviving source roots to publish facts for + * + * @return immutable facts keyed by surviving site identity; scratch entries are cleared + */ public Map finish(List roots) { Map result = new IdentityHashMap<>(); roots.stream().flatMap(Collector::nodes) diff --git a/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java b/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java index 9bcef7267de..c1b0c313138 100644 --- a/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java +++ b/javatools/src/main/java/org/xvm/compiler/InitializerBinding.java @@ -26,19 +26,36 @@ public record InitializerBinding(List references, List writtenNodes(AstNode root) { List nodes = new ArrayList<>(List.of(root)); for (int i = 0; i < nodes.size(); i++) { @@ -47,7 +64,14 @@ public static List writtenNodes(AstNode root) { return List.copyOf(nodes); } - /** Called only after successful validation/emission and the decision to discard the method. */ + /** + * Called only after successful validation/emission and the decision to discard the method. + * + * @param written the original initializer nodes captured before validation rewrites + * @param facts the validated call and construction facts from this initializer attempt + * + * @return detached source associations retained after discarding the synthetic initializer method + */ public static InitializerBinding capture(List written, InvocationBinding.Facts facts) { List references = new ArrayList<>(); List expressions = new ArrayList<>(); diff --git a/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java b/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java index 1e3ddc71ecd..d560222d9d3 100644 --- a/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java +++ b/javatools/src/main/java/org/xvm/compiler/InvocationBinding.java @@ -23,6 +23,10 @@ /** * Immutable source provenance for a selected, instantiated method call. Created during real * validation, not overload probing. No Context or mutable argument expressions are retained. + * + * @param method the compiler-selected method identity + * @param signature the instantiated signature fitted at this source site + * @param arguments written argument spans associated with visible parameter indices */ public record InvocationBinding(MethodConstant method, SignatureConstant signature, List arguments) { @@ -30,7 +34,12 @@ public record InvocationBinding(MethodConstant method, SignatureConstant signatu arguments = List.copyOf(arguments); } - /** A validated function-valued invocation has a signature, but no statically selected method. */ + /** + * A validated function-valued invocation has a signature, but no statically selected method. + * + * @param type the compiler-resolved value or function type + * @param arguments written argument spans associated with visible parameter indices + */ public record FunctionCall(TypeConstant type, List arguments) { public FunctionCall { arguments = List.copyOf(arguments); @@ -40,6 +49,11 @@ public record FunctionCall(TypeConstant type, List arguments) { /** * The two kinds of call facts remain separate so a function never claims a method target. * Maps are immutable identity snapshots: keys and values compare by reference. + * + * @param methods validated method calls keyed by invocation identity + * @param functions validated function calls keyed by invocation identity + * @param constructors validated constructor calls keyed by construction identity + * @param initializers folded initializer facts keyed by source property identity */ public record Facts(Map methods, Map functions, @@ -52,12 +66,25 @@ public record Facts(Map methods, initializers = identitySnapshot(initializers); } + /** + * Capture call facts, leaving omitted constructor or initializer facts empty. + * + * @param methods validated method calls keyed by invocation identity + * @param functions validated function calls keyed by invocation identity + * @param constructors validated constructor calls keyed by construction identity + */ public Facts(Map methods, Map functions, Map constructors) { this(methods, functions, constructors, Map.of()); } + /** + * Capture call facts, leaving omitted constructor or initializer facts empty. + * + * @param methods validated method calls keyed by invocation identity + * @param functions validated function calls keyed by invocation identity + */ public Facts(Map methods, Map functions) { this(methods, functions, Map.of()); @@ -77,12 +104,25 @@ public record Label(String name, long startPosition, long endPosition) {} /** A written argument's source range and visible parameter index; positional/legacy facts have no label. */ public record Argument(long startPosition, long endPosition, int parameterIndex, boolean named, Label label) { - /** Retain callers that recorded named status without the label's source span. */ + /** + * Retain callers that recorded named status without the label's source span. + * + * @param startPosition the written source range's inclusive start + * @param endPosition the written source range's exclusive end + * @param parameterIndex the zero-based visible parameter index + * @param named whether the argument was written with a parameter label + */ public Argument(long startPosition, long endPosition, int parameterIndex, boolean named) { this(startPosition, endPosition, parameterIndex, named, null); } - /** Retain the original positional-argument construction API. */ + /** + * Retain the original positional-argument construction API. + * + * @param startPosition the written source range's inclusive start + * @param endPosition the written source range's exclusive end + * @param parameterIndex the zero-based visible parameter index + */ public Argument(long startPosition, long endPosition, int parameterIndex) { this(startPosition, endPosition, parameterIndex, false); } @@ -91,6 +131,11 @@ public Argument(long startPosition, long endPosition, int parameterIndex) { /** * Match the compiler's parameter-ordered expressions to their written argument spans before * validation inserts conversions. Synthetic defaults have no matching written expression. + * + * @param written the arguments in source order, before validation rewrites + * @param ordered the same arguments arranged in parameter order, including synthetic defaults + * + * @return source-to-parameter associations, or empty if any written argument cannot be matched */ public static Optional> arguments(List written, List ordered) { List bindings = new ArrayList<>(); @@ -130,6 +175,7 @@ public static Optional> arguments(List written, List< * a compiler context survives with the returned tree. The result is detached from the collector. */ public static final class Collector { + /** Create an enabled call-fact collector for one compilation attempt. */ public Collector() { this(true); } @@ -143,7 +189,11 @@ public boolean isEnabled() { return f_enabled; } - /** Invalidate any earlier result before retrying validation of this invocation. */ + /** + * Invalidate any earlier result before retrying validation of this invocation. + * + * @param invocation the source invocation being validated + */ public void begin(InvocationExpression invocation) { if (f_enabled) { f_bindings.remove(invocation); @@ -151,45 +201,83 @@ public void begin(InvocationExpression invocation) { } } - /** Record a completed validation; node identity keeps speculative clones separate. */ + /** + * Record a completed validation; node identity keeps speculative clones separate. + * + * @param invocation the source invocation being validated + * @param binding the successfully established facts for this source node + */ public void record(InvocationExpression invocation, InvocationBinding binding) { if (f_enabled) { f_bindings.put(invocation, binding); } } + /** + * Retain a successfully validated call's source associations. + * + * @param invocation the source invocation being validated + * @param binding the successfully established facts for this source node + */ public void record(InvocationExpression invocation, FunctionCall binding) { if (f_enabled) { f_functions.put(invocation, binding); } } - /** Constructor arguments undergo the same named/default argument rewriting as methods. */ + /** + * Constructor arguments undergo the same named/default argument rewriting as methods. + * + * @param construction the source construction being validated + */ public void begin(NewExpression construction) { if (f_enabled) { f_constructors.remove(construction); } } + /** + * Retain a successfully validated call's source associations. + * + * @param construction the source construction being validated + * @param binding the successfully established facts for this source node + */ public void record(NewExpression construction, InvocationBinding binding) { if (f_enabled) { f_constructors.put(construction, binding); } } - /** A discarded synthetic initializer keeps detached facts anchored to its source property. */ + /** + * A discarded synthetic initializer keeps detached facts anchored to its source property. + * + * @param property the surviving property declaration that owns the initializer + * @param binding the successfully established facts for this source node + */ public void record(PropertyDeclarationStatement property, InitializerBinding binding) { if (f_enabled) { f_initializers.put(property, binding); } } - /** Finish the attempt, returning immutable facts for the surviving source trees. */ + /** + * Finish the attempt, returning immutable facts for the surviving source trees. + * + * @param roots the surviving source roots to publish facts for + * + * @return immutable method-call facts keyed by surviving invocation identity; scratch entries are cleared + */ public Map finish(List roots) { return finishFacts(roots).methods(); } - /** Finish both kinds together, releasing discarded or speculative nodes in either map. */ + /** + * Finish both kinds together, releasing discarded or speculative nodes in either map. + * + * @param roots the surviving source roots to publish facts for + * + * @return immutable call and initializer facts for surviving syntax; scratch entries are cleared + */ public Facts finishFacts(List roots) { Map result = new IdentityHashMap<>(); Map functions = new IdentityHashMap<>(); diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index c447a6a70ed..25bff57e936 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -63,6 +63,11 @@ public Parser(Source source, ErrorListener listener) { /** * Create a parser that can retain a trailing incomplete expression statement for explicit * partial analysis. Errors are still reported; this does not make the source compilable. + * + * @param source the source snapshot to parse + * @param listener the diagnostic listener and abort policy for this parse + * + * @return a parser that retains only the explicitly supported incomplete syntax boundaries */ public static Parser forPartialAnalysis(Source source, ErrorListener listener) { return new Parser(source, listener, null, true, NO_CURSOR); @@ -76,6 +81,12 @@ public static Parser forPartialAnalysis(Source source, ErrorListener listener) { * need not be malformed; it is inspected as a prefix, without validating the selected operation. * Missing call/group/index closers around that operation retain their enclosing syntax at a * statement or outer-delimiter boundary; a cursor at EOF also retains unfinished blocks. + * + * @param source the source snapshot to parse + * @param cursor the selected position in the owning Source snapshot + * @param listener the diagnostic listener and abort policy for this parse + * + * @return a parser that retains only the explicitly supported incomplete syntax boundaries */ public static Parser forPartialAnalysis(Source source, long cursor, ErrorListener listener) { if (cursor == NO_CURSOR) { @@ -5373,6 +5384,7 @@ private IncompleteHeader incompleteHeader(Token prefix) { return new IncompleteHeader(new IncompleteStatement(prefix, f_cursor, INCOMPLETE_EXPRESSION)); } + /** Unwinds header parsing while carrying the incomplete site, written type and formal parameters. */ private static class IncompleteHeader extends CompilerException { private IncompleteHeader(IncompleteStatement site) { this(site, site.getTarget(), List.of()); @@ -6526,6 +6538,8 @@ private Reporting.Scope reportingTo(ErrorListener errs) { * a kept attempt is the real parse and nothing re-reads those tokens. An attempt is a * {@link ErrorListener#branch} instead, so keeping it merges what it had to say and dropping * it drops the lot, which is the behaviour the rest of the compiler already gets. + * + * @return a scope that restores tokens and discards diagnostics unless the attempt is kept */ public Attempt attempt() { return new Attempt(); @@ -6536,6 +6550,7 @@ public Attempt attempt() { */ public class Attempt implements AutoCloseable, ErrorListener { + /** Save the token position and install a diagnostic branch until this attempt is closed. */ public Attempt() { // read the destination before moving it: the branch buffers into whatever this parser // was reporting to when the attempt began diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index 2ef0e32f983..c987d9342d8 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -277,21 +277,19 @@ public AstNode copyTree() { if (oVal != null) { if (oVal instanceof AstNode node) { - AstNode nodeNew = node.copyTree(); + var nodeNew = node.copyTree(); that.adopt(nodeNew); oVal = nodeNew; - } else if (oVal instanceof List list) { - ArrayList listNew = new ArrayList<>(); - for (AstNode node : (List) list) { - listNew.add(node.copyTree()); - } - + } else if (oVal instanceof List list) { + var listNew = list.stream() + .map(AstNode.class::cast) + .map(AstNode::copyTree) + .collect(Collectors.toCollection(ArrayList::new)); that.adopt(listNew); oVal = listNew; } else { - throw new IllegalStateException( - "unsupported container type: " + oVal.getClass().getSimpleName()); + throw new IllegalStateException("unsupported container type: " + oVal.getClass().getSimpleName()); } try { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java index 7b5a27b77ea..2d39762eef1 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/LambdaExpression.java @@ -114,7 +114,11 @@ public MethodStructure getLambda() { return m_lambda; } - /** The written arrow token, for source-position consumers. */ + /** + * The written arrow token, for source-position consumers. + * + * @return the lambda's written arrow token + */ public Token getOperator() { return operator; } @@ -124,8 +128,7 @@ public Token getOperator() { * its generated method has been validated */ public LambdaBindings getSourceBindings() { - return m_ctxLambda == null || !m_ctxLambda.f_bindings.isFor(m_lambda) - ? null : m_ctxLambda.f_bindings; + return m_ctxLambda == null || !m_ctxLambda.f_bindings.isFor(m_lambda) ? null : m_ctxLambda.f_bindings; } /** Record a generated parameter through the context that owns its source associations. */ diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java index 6063c1115e4..4229eeb8278 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java @@ -126,6 +126,8 @@ public List getArguments() { * Written anonymous body before an anonymous class owns it. It is deliberately not a compiler * child yet: registering/validating it requires the construction's inferred type and context. * Syntax readers may inspect this borrowed node; they must not adopt or validate it themselves. + * + * @return the retained anonymous source body, or empty when none remains unregistered */ public Optional getUnregisteredBody() { return anon == null ? Optional.ofNullable(body) : Optional.empty(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialAst.java b/javatools/src/main/java/org/xvm/compiler/ast/PartialAst.java new file mode 100644 index 00000000000..48e25e1e562 --- /dev/null +++ b/javatools/src/main/java/org/xvm/compiler/ast/PartialAst.java @@ -0,0 +1,282 @@ +package org.xvm.compiler.ast; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; +import java.util.function.Consumer; +import java.util.stream.Stream; + +import org.xvm.asm.ClassStructure; +import org.xvm.asm.ConstantPool; +import org.xvm.asm.ErrorListener; +import org.xvm.asm.MethodStructure; +import org.xvm.asm.constants.IdentityConstant; +import org.xvm.asm.constants.MethodConstant; +import org.xvm.asm.constants.SignatureConstant; +import org.xvm.asm.constants.TypeConstant; +import org.xvm.asm.constants.TypeInfo; + +import org.xvm.compiler.Token; + +/** + * Internal bridge from {@code ast.partial} to ordinary compiler operations. Java subpackages do + * not share package access, so this class keeps the existing AST members protected or package + * private while partial-analysis policy lives in its own package. + * + *

All nodes, contexts and results belong to the current compiler worker and attempt. This is + * not an embedding API: hosts consume the results exposed by {@code EmbeddingSupport}. Methods + * delegate to ordinary validation and fitting; they neither select incomplete calls for emission + * nor install proposed expressions into the source tree. + */ +public final class PartialAst { + private PartialAst() {} + + /** + * Obtain the constant pool visible from a source node. + * + * @param node the node in the current compilation attempt + * @return its compilation's pool + */ + public static ConstantPool pool(AstNode node) { + return node.pool(); + } + + /** + * Give disposable syntax a lexical parent without changing its internal child parentage. + * + * @param the concrete node type, preserved for the caller + * @param node the disposable node; never an attached child being moved between live trees + * @param scope the lexical parent used for resolution + * @return the same node + */ + public static T inScope(T node, AstNode scope) { + node.setParent(scope); + return node; + } + + /** + * Parent a newly built trial tree, including its descendants, without attaching it to a source child field. + * + * @param the concrete node type, preserved for the caller + * @param node the trial tree + * @param scope its lexical parent, or null for a trial root + * @return the same tree with descendant parent links installed + */ + public static T parentTree(T node, AstNode scope) { + inScope(node, scope).introduceParentage(); + return node; + } + + /** + * Validate an intact expression through the ordinary compiler rules. The caller controls + * whether it is retained source or a disposable proposal and owns any replacement it returns. + * + * @param expression the expression to validate + * @param ctx its live compiler context + * @param required the required type, or null when unconstrained + * @param errs the listener and abort policy + * @return the validated replacement, or null on failure + */ + public static Expression validate(Expression expression, Context ctx, TypeConstant required, ErrorListener errs) { + return expression.validate(ctx, required, errs); + } + + /** + * Query type information with the ordinary expression's access rules. + * + * @param expression the expression supplying lexical ownership + * @param ctx its live compiler context + * @param type the receiver type to inspect + * @param errs the listener and abort policy + * @return the type information used by ordinary validation + */ + public static TypeInfo typeInfo(Expression expression, Context ctx, TypeConstant type, ErrorListener errs) { + return expression.getTypeInfo(ctx, type, errs); + } + + /** + * Initialize lazy parameter registers before enumerating visible variable names. + * + * @param ctx the cursor's live validation context + * @return the names, without making any claim about readability or narrowed types + */ + public static Set variableNames(Context ctx) { + Stream.iterate(ctx, Objects::nonNull, Context::getOuterContext).forEach(Context::getNameMap); + Set names = new HashSet<>(); + ctx.collectVariables(names); + return names; + } + + /** + * Read already established explicit and wildcard imports without retaining the block's maps. + * + * @param block the enclosing source block + * @return candidate import names; later ordinary lookup still checks visibility + */ + public static Set importNames(StatementBlock block) { + Set names = new HashSet<>(); + if (block.imports != null) { + names.addAll(block.imports.keySet()); + } + if (block.importsWild != null) { + block.importsWild.forEach(statement -> { + if (statement.getNameResolver().getConstant() instanceof IdentityConstant identity + && identity.getComponent() != null) { + names.addAll(identity.getComponent().getChildByNameMap().keySet()); + } + }); + } + return names; + } + + /** + * Inspect a written type qualifier without adopting it into another tree. + * + * @param type the written named type + * @return its borrowed qualifier, or null when absent + */ + public static TypeExpression qualifier(NamedTypeExpression type) { + return type.left; + } + + /** + * Retain the positions of all written name segments, not just their string values. + * + * @param type the written named type + * @return a read-only snapshot containing the borrowed source tokens + */ + public static List nameTokens(NamedTypeExpression type) { + return List.copyOf(type.names); + } + + /** + * Build a trial reference to the same name and access spelling without type arguments. + * + * @param type the unqualified written type whose name is being resolved + * @return a fresh, unparented type reference + */ + public static NamedTypeExpression typeReference(NamedTypeExpression type) { + return new NamedTypeExpression(null, type.names, type.access, null, null, type.getEndPosition()); + } + + /** + * Inspect the source expression retained by a failed type parse. + * + * @param type the failed type wrapper + * @return its borrowed written expression + */ + public static Expression writtenType(BadTypeExpression type) { + return type.nonType; + } + + /** + * Test for an anonymous source body, including after preparation registers its class shell. + * + * @param creation the source construction + * @return whether it retains an anonymous body + */ + public static boolean hasAnonymousBody(NewExpression creation) { + return creation.body != null; + } + + /** + * Prepare the ordinary construction receiver and type without synthesizing a forwarding + * constructor or emitting the unfinished operation. + * + * @param creation the owned source construction or its disposable trial copy + * @param ctx the trial validation context + * @param required the contextual type, or null when unconstrained + * @param errs the listener and abort policy + * @return prepared types, or null if preparation failed + */ + public static Construction prepareConstruction(NewExpression creation, Context ctx, TypeConstant required, ErrorListener errs) { + var prepared = creation.prepareConstruction(ctx, required, errs, true); + return prepared == null ? null : new Construction(prepared.result(), prepared.target(), + prepared.superType(), prepared.requiresNewable()); + } + + /** + * Prepared types for this construction query; retains no private construction plan or context. + * + * @param result the inferred result type + * @param target the type whose constructors are inspected + * @param superType the prepared superclass type for anonymous construction, otherwise null + * @param requiresNewable whether ordinary construction requires a newable target + */ + public record Construction(TypeConstant result, TypeConstant target, TypeConstant superType, boolean requiresNewable) {} + + /** + * Identify the real fixed-size constructor selected by array-dimension syntax. + * + * @param creation the prepared construction + * @return the supply constructor, or null for other type syntax + */ + public static MethodConstant arrayConstructor(NewExpression creation) { + return creation.type instanceof ArrayTypeExpression array ? array.getSupplyConstructor() : null; + } + + /** + * Arrange named arguments using the ordinary compiler's parameter-order rules. + * + * @param site the source operation supplying diagnostic positions + * @param method the candidate declaration + * @param arguments the owned trial arguments, already in parameter order when unnamed + * @param errs the listener and abort policy + * @return the parameter-ordered arguments, or null if labels cannot be arranged + */ + public static List orderArguments(AstNode site, MethodStructure method, List arguments, ErrorListener errs) { + return site.containsNamedArgs(arguments) ? site.rearrangeNamedArgs(method, arguments, errs) : arguments; + } + + /** + * Apply ordinary post-argument class inference to a prepared construction. + * + * @param creation the prepared source construction or trial copy + * @param ctx the trial validation context + * @param owner the class declaring the candidate constructor + * @param method the candidate constructor + * @param arguments its validated arguments in parameter order + * @return the inferred class type, or null when no refinement was obtained + */ + public static TypeConstant inferConstruction(NewExpression creation, Context ctx, ClassStructure owner, + MethodStructure method, List arguments) { + return creation.inferTypeFromConstructor(ctx, owner, method, arguments); + } + + /** + * Fit one method using ordinary named-argument, conversion and inference rules. The callback + * runs during fitting and must copy any facts it retains; it does not select a final overload. + * + * @param site the source operation supplying pool and diagnostic positions + * @param ctx the enclosing context; fitting uses a child context + * @param target the receiver type + * @param info its type information + * @param method the single candidate to fit + * @param arguments disposable written arguments + * @param errs the listener and abort policy + * @param matched receives each fitted signature and ordered argument list synchronously + */ + public static void matchMethod(AstNode site, Context ctx, TypeConstant target, TypeInfo info, MethodConstant method, + List arguments, ErrorListener errs, Consumer matched) { + var named = site.collectNamedArgs(arguments, errs); + if (named == null || errs.isAbortDesired()) { + return; + } + Set direct = new HashSet<>(); + Set converting = new HashSet<>(); + site.collectMatchingMethods(ctx.enter(), target, info, Set.of(method), arguments, false, named, + null, direct, converting, new HashMap<>(), errs, (signature, ordered) -> + matched.accept(new MethodMatch(signature, ordered, converting.contains(method)))); + } + + /** + * Borrowed values delivered synchronously by the ordinary argument fitter. + * + * @param signature the fitted signature before partial-query presentation adjustments + * @param arguments the argument expressions in parameter order; valid during the callback + * @param converting whether this candidate requires conversion + */ + public record MethodMatch(SignatureConstant signature, List arguments, boolean converting) {} +} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java index b3508d468c9..a67349de57b 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StageMgr.java @@ -41,14 +41,28 @@ public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs) { this(node, stageTarget, errs, InvocationBinding.Collector.NONE); } - /** Progress a node while preserving the compilation attempt's call-fact collector. */ - public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, - InvocationBinding.Collector bindings) { + /** + * Progress a node while preserving the compilation attempt's call-fact collector. + * + * @param node the node whose subtree is processed + * @param stageTarget the target compiler stage + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + */ + public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings) { this(node, stageTarget, errs, bindings, CursorBinding.Collector.NONE); } - public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, - InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { + /** + * Progress source nodes using the enclosing attempt's call and cursor collectors. + * + * @param node the node whose subtree is processed + * @param stageTarget the target compiler stage + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection + */ + public StageMgr(AstNode node, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { assert node != null; assert stageTarget != null && stageTarget.isTargetable(); @@ -71,14 +85,28 @@ public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener er this(list, stageTarget, errs, InvocationBinding.Collector.NONE); } - /** Progress child nodes using the same collector as their enclosing compilation. */ - public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, - InvocationBinding.Collector bindings) { + /** + * Progress child nodes using the same collector as their enclosing compilation. + * + * @param list the nonempty list of nodes whose subtrees are processed + * @param stageTarget the target compiler stage + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + */ + public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings) { this(list, stageTarget, errs, bindings, CursorBinding.Collector.NONE); } - public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, - InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { + /** + * Progress source nodes using the enclosing attempt's call and cursor collectors. + * + * @param list the nonempty list of nodes whose subtrees are processed + * @param stageTarget the target compiler stage + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection + */ + public StageMgr(List list, Stage stageTarget, @NotNull ErrorListener errs, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { assert list != null && !list.isEmpty(); assert stageTarget != null && stageTarget.isTargetable(); @@ -200,6 +228,11 @@ public CursorBinding.Collector getCursorBindings() { return f_cursors; } + /** + * Obtain the collector shared by this stage manager and its child stages. + * + * @return the attempt-owned collector, possibly the disabled NONE instance + */ public InvocationBinding.Collector getInvocationBindings() { return f_bindings; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java index e6e3e5cc1a3..945ee5bcfff 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/StatementBlock.java @@ -304,11 +304,29 @@ public boolean compileMethod(Code code, ErrorListener errs) { return compileMethod(code, errs, InvocationBinding.Collector.NONE); } - /** Compile a method with the collector owned by its enclosing compilation attempt. */ + /** + * Compile a method with the collector owned by its enclosing compilation attempt. + * + * @param code the method code object receiving generated instructions + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * + * @return whether compilation completed without a condition that must stop further progress + */ public boolean compileMethod(Code code, ErrorListener errs, InvocationBinding.Collector bindings) { return compileMethod(code, errs, bindings, CursorBinding.Collector.NONE); } + /** + * Compile a method while preserving both of the enclosing attempt's fact collectors. + * + * @param code the method code object receiving generated instructions + * @param errs the diagnostic listener and abort policy for this operation + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection + * + * @return whether compilation completed without a condition that must stop further progress + */ public boolean compileMethod(Code code, ErrorListener errs, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { return compileMethod(new RootContext(this, code.getMethodStructure(), bindings, cursors), code, errs); @@ -565,11 +583,26 @@ public RootContext(StatementBlock stmt, MethodStructure method) { this(stmt, method, InvocationBinding.Collector.NONE); } + /** + * Create a method-body context that shares the enclosing attempt's fact collectors. + * + * @param stmt the method body whose context is being created + * @param method the method whose body is being compiled + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + */ public RootContext(StatementBlock stmt, MethodStructure method, InvocationBinding.Collector bindings) { this(stmt, method, bindings, CursorBinding.Collector.NONE); } + /** + * Create a method-body context that shares the enclosing attempt's fact collectors. + * + * @param stmt the method body whose context is being created + * @param method the method whose body is being compiled + * @param bindings the attempt-owned call-fact collector, or its NONE instance to disable collection + * @param cursors the attempt-owned cursor-fact collector, or its NONE instance to disable collection + */ public RootContext(StatementBlock stmt, MethodStructure method, InvocationBinding.Collector bindings, CursorBinding.Collector cursors) { super(null, false); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/CursorScope.java similarity index 87% rename from javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java rename to javatools/src/main/java/org/xvm/compiler/ast/partial/CursorScope.java index 64c13bf87db..90a0c987ff1 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/CursorScope.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/CursorScope.java @@ -1,4 +1,4 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.util.Arrays; import java.util.Comparator; @@ -30,7 +30,21 @@ import org.xvm.compiler.CursorBinding; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; -import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.BadTypeExpression; +import org.xvm.compiler.ast.BiTypeExpression; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.DecoratedTypeExpression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NameResolver; +import org.xvm.compiler.ast.NamedTypeExpression; +import org.xvm.compiler.ast.NullableTypeExpression; +import org.xvm.compiler.ast.Parameter; +import org.xvm.compiler.ast.PartialAst; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.StatementBlock; +import org.xvm.compiler.ast.TypeCompositionStatement; +import org.xvm.compiler.ast.TypeExpression; import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; @@ -39,10 +53,7 @@ final class CursorScope { /** Copy scope facts now; callers must not retain this validation context. */ static CursorBinding capture(Context ctx) { - // Parameter registers are initialized lazily, including their assignment state. - Stream.iterate(ctx, Objects::nonNull, Context::getOuterContext).forEach(Context::getNameMap); - Set names = new HashSet<>(); - ctx.collectVariables(names); + var names = PartialAst.variableNames(ctx); var variables = names.stream().sorted() .filter(name -> !ctx.isReservedName(name)) .map(name -> ctx.getVar(name) instanceof Register register @@ -70,7 +81,7 @@ static List declarationTypes(IncompleteStatement site, List writtenFormals, ErrorListener errs) { if (site.getTarget() instanceof NamedTypeExpression type) { if (writtenFormals.stream().anyMatch(formal -> formal.getName().equals(firstName(type)))) { - if (type.left != null || type.getNames().length < 2) { + if (PartialAst.qualifier(type) != null || type.getNames().length < 2) { return List.of(); } var parameters = writtenFormals.stream() @@ -81,21 +92,22 @@ static List declarationTypes(IncompleteStatement site, } // NameResolver's FORMAL_TYPE mode permits virtual child classes, not typedefs // or static nested types. An upper bound must not erase that distinction. + var names = PartialAst.nameTokens(type); var candidates = parameterizedTypes(new NamedTypeExpression( - qualifier, type.names.subList(1, type.names.size()), type.paramTypes, type.getEndPosition()), + qualifier, names.subList(1, names.size()), type.getParamTypes(), type.getEndPosition()), scope, site.getCompletionPrefix(), errs); - if (type.names.size() == 2) { + if (names.size() == 2) { return candidates.stream().filter(candidate -> candidate.identity().getComponent() instanceof ClassStructure child && child.isVirtualChild()).toList(); } var bound = formalBound(parameters.get(firstName(type)), scope, parameters, Set.of(), errs); var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); var first = bound == null ? null : bound.ensureTypeInfo(scope.getComponent().getIdentityConstant(), probe) - .getChildInfosByName().get(type.names.get(1).getValueText()); + .getChildInfosByName().get(names.get(1).getValueText()); return first != null && first.getIdentity().getComponent() instanceof ClassStructure child && child.isVirtualChild() && !probe.hasSeriousErrors() && !probe.isAbortDesired() ? candidates : List.of(); } - if (type.left != null) { + if (PartialAst.qualifier(type) != null) { return parameterizedTypes(type, scope, site.getCompletionPrefix(), errs); } if (type.getNames().length > 1) { @@ -111,14 +123,14 @@ static List declarationTypes(IncompleteStatement site, } private static String firstName(NamedTypeExpression type) { - return type.left instanceof NamedTypeExpression left ? firstName(left) : type.getNames()[0]; + return PartialAst.qualifier(type) instanceof NamedTypeExpression left ? firstName(left) : type.getNames()[0]; } /** Resolve bounds on disposable syntax only; no unfinished class/method or formal is registered. */ static List declarationFormals(IncompleteStatement site, AstNode scope, List writtenFormals, ErrorListener errs) { return site.getTarget() instanceof NamedTypeExpression named - && (named.left != null || named.getNames().length > 1) ? List.of() : resolveFormals(site, scope, writtenFormals, errs); + && (PartialAst.qualifier(named) != null || named.getNames().length > 1) ? List.of() : resolveFormals(site, scope, writtenFormals, errs); } private static List resolveFormals(IncompleteStatement site, AstNode scope, List parameters, @@ -126,7 +138,7 @@ private static List resolveFormals(IncompleteStatement sit var byName = parameters.stream().collect(Collectors.toMap(Parameter::getName, Function.identity(), (first, second) -> first)); return parameters.stream().takeWhile(parameter -> !errs.isAbortDesired()).map(parameter -> { - if (parameter.getType() instanceof BadTypeExpression bad && bad.nonType instanceof TypeExpression written + if (parameter.getType() instanceof BadTypeExpression bad && PartialAst.writtenType(bad) instanceof TypeExpression written && site.getTarget() instanceof NamedTypeExpression selected && parameter.getName().startsWith(site.getCompletionPrefix())) { var proposal = completeBound(written, selected.getNameToken(), parameter.getNameToken()); @@ -147,7 +159,7 @@ private static List resolveFormals(IncompleteStatement sit /** Replace just the selected leaf on disposable syntax, retaining every other bound operand. */ private static TypeExpression completeBound(TypeExpression written, Token selected, Token name) { - if (written instanceof NamedTypeExpression named && named.left == null && named.paramTypes == null + if (written instanceof NamedTypeExpression named && PartialAst.qualifier(named) == null && named.getParamTypes() == null && named.getNames().length == 1 && named.getNameToken().getStartPosition() == selected.getStartPosition()) { return new NamedTypeExpression(null, List.of(name), null, null, null, named.getEndPosition()); @@ -179,12 +191,12 @@ private static BoundSyntax writtenBound(TypeExpression type, AstNode scope, .map(child -> writtenBound(child, scope, parameters, resolving, depth, errs)) .reduce(BoundSyntax.COMPLETE, BoundSyntax::combine); } - if (!(type instanceof NamedTypeExpression named) || named.left != null) { + if (!(type instanceof NamedTypeExpression named) || PartialAst.qualifier(named) != null) { return BoundSyntax.INVALID; } String name = named.getNames()[0]; if (parameters.containsKey(name)) { - if (named.getNames().length != 1 || named.paramTypes != null) { + if (named.getNames().length != 1 || named.getParamTypes() != null) { return BoundSyntax.INVALID; } if (resolving.containsKey(name)) { @@ -199,8 +211,8 @@ private static BoundSyntax writtenBound(TypeExpression type, AstNode scope, return writtenBound(parameter.getType(), scope, parameters, active, depth, errs); } var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); - var reference = new NamedTypeExpression(null, named.names, named.access, null, null, named.getEndPosition()); - reference.setParent(scope); + var reference = PartialAst.typeReference(named); + PartialAst.inScope(reference, scope); var target = new NameResolver(reference, Arrays.asList(named.getNames()).iterator()).forceResolve(probe); if (!(target instanceof IdentityConstant identity) || !isTypeIdentity(identity) || !visible(identity, scope.getComponent().getIdentityConstant()) @@ -208,11 +220,12 @@ private static BoundSyntax writtenBound(TypeExpression type, AstNode scope, return BoundSyntax.INVALID; } int nested = depth + (identity.getComponent() instanceof ClassStructure ? 1 : 0); - return named.paramTypes == null ? BoundSyntax.COMPLETE : named.paramTypes.stream() + return named.getParamTypes() == null ? BoundSyntax.COMPLETE : named.getParamTypes().stream() .map(argument -> writtenBound(argument, scope, parameters, resolving, nested, errs)) .reduce(BoundSyntax.COMPLETE, BoundSyntax::combine); } + /** Classifies a written type bound without inventing a formal type for recursive syntax. */ private enum BoundSyntax { COMPLETE, RECURSIVE, INVALID; @@ -231,7 +244,7 @@ private static TypeConstant formalBound(Parameter parameter, AstNode scope, Map< if (copy == null) { return null; } - copy.setParent(scope); + PartialAst.inScope(copy, scope); var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); TypeConstant bound = resolveType(copy, probe); return bound != null && visibleNames(copy, scope.getComponent().getIdentityConstant()) ? bound : null; @@ -244,7 +257,7 @@ private static TypeExpression formalConstraint(Parameter parameter, AstNode scop return null; } if (parameter.getType() == null) { - return new NamedTypeExpression(new NameExpression(parameter.getNameToken()), scope.pool().typeObject()); + return new NamedTypeExpression(new NameExpression(parameter.getNameToken()), PartialAst.pool(scope).typeObject()); } var active = Stream.concat(resolving.stream(), Stream.of(parameter.getName())) .collect(Collectors.toUnmodifiableSet()); @@ -257,15 +270,15 @@ private static TypeExpression substituteFormals(TypeExpression type, AstNode sco if (type instanceof BadTypeExpression) { return null; } - if (type instanceof NamedTypeExpression named && named.left == null + if (type instanceof NamedTypeExpression named && PartialAst.qualifier(named) == null && parameters.containsKey(named.getNames()[0])) { TypeExpression replacement = formalConstraint(parameters.get(named.getNames()[0]), scope, parameters, resolving, errs); - if (replacement == null || named.paramTypes != null) { + if (replacement == null || named.getParamTypes() != null) { return null; } - return named.getNames().length == 1 ? replacement - : new NamedTypeExpression(replacement, named.names.subList(1, named.names.size()), - null, named.getEndPosition()); + var names = PartialAst.nameTokens(named); + return names.size() == 1 ? replacement + : new NamedTypeExpression(replacement, names.subList(1, names.size()), null, named.getEndPosition()); } var children = StreamSupport.stream(type.children().spliterator(), false).toList(); for (AstNode child : children) { @@ -315,11 +328,12 @@ private static List qualifiedTypes(NamedTypeExpression /** Resolve only a disposable copy of the complete written qualifier, including its arguments. */ private static List parameterizedTypes(NamedTypeExpression type, AstNode scope, String prefix, ErrorListener errs) { - var left = (TypeExpression) type.left.clone(); - TypeExpression qualifier = type.names.size() == 1 ? left - : new NamedTypeExpression(left, type.names.subList(0, type.names.size() - 1), null, - type.names.get(type.names.size() - 2).getEndPosition()); - qualifier.setParent(scope); + var left = (TypeExpression) PartialAst.qualifier(type).clone(); + var names = PartialAst.nameTokens(type); + TypeExpression qualifier = names.size() == 1 ? left + : new NamedTypeExpression(left, names.subList(0, names.size() - 1), null, + names.get(names.size() - 2).getEndPosition()); + PartialAst.inScope(qualifier, scope); var lookup = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); var owner = scope.getComponent().getIdentityConstant(); TypeConstant resolved = resolveType(qualifier, lookup); @@ -341,7 +355,7 @@ private static List parameterizedTypes(NamedTypeExpress var name = new Token(written.getStartPosition(), written.getEndPosition(), Id.IDENTIFIER, child.getName()); var candidate = new NamedTypeExpression((TypeExpression) qualifier.clone(), List.of(name), null, written.getEndPosition()); - candidate.setParent(scope); + PartialAst.inScope(candidate, scope); var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); TypeConstant result = resolveType(candidate, probe); var target = candidate.getNameBindings().getLast().target(); @@ -364,7 +378,7 @@ private static boolean visibleNames(AstNode node, IdentityConstant owner) { return (!(node instanceof NamedTypeExpression named) || named.getNameBindings().stream().allMatch(binding -> binding.target() instanceof IdentityConstant identity && visible(identity, owner) - || binding.target() instanceof TypeConstant && named.alreadyReached(Stage.Validated))) + || binding.target() instanceof TypeConstant && named.getStage().compareTo(Stage.Validated) >= 0)) && StreamSupport.stream(node.children().spliterator(), false).allMatch(child -> visibleNames(child, owner)); } @@ -415,7 +429,7 @@ static List enclosingValues(IncompleteStatement site, Context ctx, TypeC var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); var trial = ctx.enter(); try { - var value = PartialCallResolver.proposedInstance(site, text).validate(trial, required, probe); + var value = PartialAst.validate(PartialCallResolver.proposedInstance(site, text), trial, required, probe); return value != null && value.getTypeFit().isFit() && !probe.hasSeriousErrors() && !probe.isAbortDesired(); } finally { @@ -447,17 +461,7 @@ private static Set names(IncompleteStatement site, List writt } } if (node instanceof StatementBlock block) { - if (block.imports != null) { - names.addAll(block.imports.keySet()); - } - if (block.importsWild != null) { - block.importsWild.forEach(statement -> { - if (statement.getNameResolver().getConstant() instanceof IdentityConstant identity - && identity.getComponent() != null) { - names.addAll(identity.getComponent().getChildByNameMap().keySet()); - } - }); - } + names.addAll(PartialAst.importNames(block)); } }); writtenFormals.stream().map(Parameter::getName).forEach(names::remove); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java index c9524be23ac..18dd66d73e9 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java @@ -12,7 +12,6 @@ import org.xvm.compiler.Token; import org.xvm.compiler.ast.Context; import org.xvm.compiler.ast.Parameter; -import org.xvm.compiler.ast.PartialQueries; import org.xvm.compiler.ast.StageMgr; import org.xvm.compiler.ast.Statement; @@ -25,12 +24,32 @@ * validation contexts. */ public final class IncompleteDeclarationStatement extends Statement { + /** Distinguishes the declaration headers retained when parsing stops at incomplete syntax. */ public enum Kind { METHOD, PROPERTY } + /** + * Retain an unfinished declaration header without registering a declaration component. + * + * @param kind the written declaration category + * @param name the written declaration name token, or null if absent + * @param start the declaration's inclusive source start + * @param end the declaration's exclusive source end + * @param cursors the incomplete type or name query sites retained in the header + */ public IncompleteDeclarationStatement(Kind kind, Token name, long start, long end, List cursors) { this(kind, name, start, end, cursors, List.of()); } + /** + * Retain an unfinished declaration header without registering a declaration component. + * + * @param kind the written declaration category + * @param name the written declaration name token, or null if absent + * @param start the declaration's inclusive source start + * @param end the declaration's exclusive source end + * @param cursors the incomplete type or name query sites retained in the header + * @param formals written formal names and constraints, without invented declaration identities + */ public IncompleteDeclarationStatement(Kind kind, Token name, long start, long end, List cursors, List formals) { this.kind = kind; this.name = name; @@ -40,11 +59,20 @@ public IncompleteDeclarationStatement(Kind kind, Token name, long start, long en this.formals = List.copyOf(formals); } + /** + * Identify the written declaration category retained for structural queries. + * + * @return the retained method or property declaration category + */ public Kind getKind() { return kind; } - /** A written name only; an absent declaration name does not acquire a placeholder. */ + /** + * A written name only; an absent declaration name does not acquire a placeholder. + * + * @return the written name token, or null if no declaration name was written + */ public Optional getNameToken() { return Optional.ofNullable(name); } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java index d9bcbfc77a0..9b41ce07669 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java @@ -19,11 +19,20 @@ * receiver/argument ownership without a second semantic cache or clone-reset protocol. */ public final class IncompleteExpression extends Expression { + /** + * Retain an incomplete site in a value-expression position without fabricating a value. + * + * @param site the retained incomplete source site + */ public IncompleteExpression(IncompleteStatement site) { this.site = site; } - /** The retained source operation; exposes syntax ownership without a validation result. */ + /** + * The retained source operation; exposes syntax ownership without a validation result. + * + * @return the incomplete site retained in this expression position + */ public IncompleteStatement getSite() { return site; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java index 77f4f95e4ce..e1b5ab3f73e 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java @@ -14,12 +14,24 @@ /** A missing local name and its written initializer; no register or inferred type is invented. */ public final class IncompleteLocalDeclaration extends Statement { + /** + * Retain an unfinished local declaration and its written initializer as syntax. + * + * @param cursor the retained incomplete declaration-name site + * @param initializer the written initializer expression, or null if absent + * @param end the declaration's exclusive source end + */ public IncompleteLocalDeclaration(IncompleteStatement cursor, Expression initializer, long end) { this.cursor = cursor; this.initializer = initializer; this.end = end; } + /** + * Obtain the retained initializer without validating the unfinished declaration. + * + * @return the written initializer, or null when absent + */ public Expression getInitializer() { return initializer; } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java index 5a0c7b80d0a..de4386ea188 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java @@ -18,7 +18,6 @@ import org.xvm.compiler.ast.NameExpression; import org.xvm.compiler.ast.NamedTypeExpression; import org.xvm.compiler.ast.NewExpression; -import org.xvm.compiler.ast.PartialQueries; import org.xvm.compiler.ast.Statement; import org.xvm.compiler.ast.TypeExpression; @@ -37,42 +36,101 @@ * succeeded (isValidated and a fitting TypeFit), never a failed child's placeholder type. */ public final class IncompleteStatement extends Statement { + /** + * Retain a bounded unfinished operation and its written children for explicit partial analysis. + * + * @param target the written receiver, callee or declaration type + * @param operator the written operator or opening delimiter selecting the incomplete operation + * @param arguments the complete written arguments preceding the cursor slot + * @param separators the top-level comma tokens between written arguments + * @param endPosition the written source range's exclusive end + */ public IncompleteStatement(Expression target, Token operator, List arguments, List separators, long endPosition) { this(target, operator, arguments, separators, endPosition, Parser.UNEXPECTED_EOF); } + /** + * Retain a bounded unfinished operation and its written children for explicit partial analysis. + * + * @param target the written receiver, callee or declaration type + * @param operator the written operator or opening delimiter selecting the incomplete operation + * @param arguments the complete written arguments preceding the cursor slot + * @param separators the top-level comma tokens between written arguments + * @param endPosition the written source range's exclusive end + * @param diagnosticCode the boundary diagnostic that prevents this incomplete operation from being emitted + */ public IncompleteStatement(Expression target, Token operator, List arguments, List separators, long endPosition, String diagnosticCode) { this(target, operator, arguments, separators, endPosition, diagnosticCode, null); } - /** A written member token selected at its end by an explicit cursor probe. */ + /** + * A written member token selected at its end by an explicit cursor probe. + * + * @param receiver the written member-access receiver + * @param dot the member-access dot token + * @param memberName the written member token selected by the cursor + * @param cursor the selected position in the owning Source snapshot + * @param diagnosticCode the boundary diagnostic that prevents this incomplete operation from being emitted + */ public IncompleteStatement(Expression receiver, Token dot, Token memberName, long cursor, String diagnosticCode) { this(receiver, dot, List.of(), List.of(), cursor, diagnosticCode, memberName); } - /** An unqualified name prefix at an explicit cursor. */ + /** + * An unqualified name prefix at an explicit cursor. + * + * @param name the written unqualified name prefix + * @param cursor the selected position in the owning Source snapshot + * @param diagnosticCode the boundary diagnostic that prevents this incomplete operation from being emitted + */ public IncompleteStatement(Token name, long cursor, String diagnosticCode) { this(new NameExpression(name), name, List.of(), List.of(), cursor, diagnosticCode, name); } - /** A declaration type prefix; retain its qualifiers as syntax and replace only the final token. */ + /** + * A declaration type prefix; retain its qualifiers as syntax and replace only the final token. + * + * @param type the written type expression retained at the cursor + * @param cursor the selected position in the owning Source snapshot + * + * @return a site selecting the final written type token while retaining its qualifiers + */ public static IncompleteStatement forDeclarationType(NamedTypeExpression type, long cursor) { Token name = type.getNameToken(); return new IncompleteStatement(type, name, List.of(), List.of(), cursor, Parser.INCOMPLETE_EXPRESSION, name); } - /** A missing declaration name after a complete written type; never fabricate a name token. */ + /** + * A missing declaration name after a complete written type; never fabricate a name token. + * + * @param type the written type expression retained at the cursor + * @param cursor the selected position in the owning Source snapshot + * + * @return a site retaining the complete type at the empty declaration-name slot + */ @SuppressWarnings("unused") public static IncompleteStatement forDeclarationName(NamedTypeExpression type, long cursor) { return forDeclarationName(type, type.getNameToken(), cursor); } - /** Retain a complete wrapped or compound type and its last written token, without a new name. */ + /** + * Retain a complete wrapped or compound type and its last written token, without a new name. + * + * @param type the written type expression retained at the cursor + * @param last the last written token of the complete declaration type + * @param cursor the selected position in the owning Source snapshot + * + * @return a site retaining the complete type at the empty declaration-name slot + */ public static IncompleteStatement forDeclarationName(TypeExpression type, Token last, long cursor) { return new IncompleteStatement(type, last, List.of(), List.of(), cursor, Parser.INCOMPLETE_EXPRESSION, null); } - /** Complete written type at an empty declaration-name slot; syntax only, never a binding. */ + /** + * Complete written type at an empty declaration-name slot; syntax only, never a binding. + * + * @return the complete written type at an empty declaration-name slot, or empty otherwise + */ public Optional getDeclarationType() { return target instanceof TypeExpression type && cursorName == null && !isCall() && operator.getId() != Id.DOT @@ -80,19 +138,46 @@ public Optional getDeclarationType() { ? Optional.of(type) : Optional.empty(); } - /** The written type's final name, for syntax suggestions only, not a resolved type or binding. */ + /** + * The written type's final name, for syntax suggestions only, not a resolved type or binding. + * + * @return the final written simple-type name, or empty for other syntax + */ public Optional getDeclarationNameType() { return getDeclarationType().filter(NamedTypeExpression.class::isInstance) .map(NamedTypeExpression.class::cast).map(NamedTypeExpression::getNameToken); } - /** A call whose last written name is a named argument awaiting its value. */ + /** + * A call whose last written name is a named argument awaiting its value. + * + * @param callee the written call target + * @param open the written opening call delimiter + * @param arguments the complete written arguments preceding the cursor slot + * @param separators the top-level comma tokens between written arguments + * @param cursor the selected position in the owning Source snapshot + * @param name the written named-argument label awaiting its value + * + * @return an incomplete call retaining the named-argument label awaiting a value + */ public static IncompleteStatement forNamedArgument(Expression callee, Token open, List arguments, List separators, long cursor, Token name) { return new IncompleteStatement(callee, open, arguments, separators, cursor, Parser.INCOMPLETE_EXPRESSION, name); } - /** A direct argument name prefix; label is null for a positional argument. */ + /** + * A direct argument name prefix; label is null for a positional argument. + * + * @param callee the written call target + * @param open the written opening call delimiter + * @param arguments the complete written arguments preceding the cursor slot + * @param separators the top-level comma tokens between written arguments + * @param cursor the selected position in the owning Source snapshot + * @param label the named-argument label, or null for a positional argument + * @param prefix the written argument-name prefix to replace + * + * @return an incomplete call retaining the argument prefix and optional label + */ public static IncompleteStatement forArgumentPrefix(Expression callee, Token open, List arguments, List separators, long cursor, Token label, Token prefix) { return new IncompleteStatement(callee, open, arguments, separators, cursor, @@ -118,22 +203,38 @@ private IncompleteStatement(Expression target, Token operator, List this.argumentPrefix = argumentPrefix; } - /** The written receiver, callee or declaration type; a call is never overload-resolved. */ + /** + * The written receiver, callee or declaration type; a call is never overload-resolved. + * + * @return the written receiver, callee or declaration type + */ public Expression getTarget() { return target; } - /** The dot or opening call/dimension delimiter, at its original source position. */ + /** + * The dot or opening call/dimension delimiter, at its original source position. + * + * @return the written operator or delimiter token selecting this site + */ public Token getOperator() { return operator; } - /** The original typed member token, if present; its text and range are syntax, not a binding. */ + /** + * The original typed member token, if present; its text and range are syntax, not a binding. + * + * @return the original member token, or empty when this site does not select one + */ public Optional getMemberName() { return isCall() ? Optional.empty() : Optional.ofNullable(cursorName); } - /** Typed text before the cursor; the original token still supplies the whole replacement range. */ + /** + * Typed text before the cursor; the original token still supplies the whole replacement range. + * + * @return the text before the cursor within the selected token, or an empty string + */ public String getCompletionPrefix() { return getArgumentPrefix().or(this::getMemberName).map(name -> endPosition < name.getEndPosition() @@ -141,53 +242,95 @@ public String getCompletionPrefix() { : name.getValueText()).orElse(""); } - /** The named argument at the cursor, whose value is absent or retained only as a name prefix. */ + /** + * The named argument at the cursor, whose value is absent or retained only as a name prefix. + * + * @return the pending named-argument label, or empty for an unnamed slot + */ public Optional getPendingArgumentName() { return isCall() ? Optional.ofNullable(cursorName) : Optional.empty(); } - /** Original token to replace when completing an argument; it is never validated as a value. */ + /** + * Original token to replace when completing an argument; it is never validated as a value. + * + * @return the original argument prefix token, or empty when absent + */ public Optional getArgumentPrefix() { return Optional.ofNullable(argumentPrefix); } - /** The real containing call for a direct, labeled or parenthesized argument cursor. */ + /** + * The real containing call for a direct, labeled or parenthesized argument cursor. + * + * @return the containing incomplete call, or empty outside a supported argument context + */ public Optional getArgumentCall() { return PartialSyntax.argumentCall(this); } - /** Complete written arguments; excludes the missing value or cursor-selected argument prefix. */ + /** + * Complete written arguments; excludes the missing value or cursor-selected argument prefix. + * + * @return a snapshot of the complete written arguments, excluding the pending cursor slot + */ public List getArguments() { return List.copyOf(arguments); } - /** Written array dimensions preceding this call's opening parenthesis. */ + /** + * Written array dimensions preceding this call's opening parenthesis. + * + * @return the written construction dimensions, or an empty list for other targets + */ public List getLeadingArguments() { return target instanceof NewExpression creation ? creation.getArguments() : List.of(); } - /** Top-level commas only; nested calls and strings do not contribute separators. */ + /** + * Top-level commas only; nested calls and strings do not contribute separators. + * + * @return the immutable top-level comma tokens + */ public List getSeparators() { return separators; } + /** + * Determine whether the retained operation is a call or a supported construction dimension. + * + * @return whether this site selects a call or a supported construction dimension + */ public boolean isCall() { return operator.getId() == Id.L_PAREN || operator.getId() == Id.ASYNC_PAREN || operator.getId() == Id.L_SQUARE && target instanceof NewExpression; } + /** + * Determine whether the cursor selects an unqualified name prefix. + * + * @return whether this site selects an unqualified name rather than an empty declaration-name slot + */ public boolean isNameCompletion() { return operator.getId() == Id.IDENTIFIER && getDeclarationType().isEmpty(); } - /** A type query in an unfinished declaration, with no value or parameter-name completion. */ + /** + * A type query in an unfinished declaration, with no value or parameter-name completion. + * + * @return whether this site queries a type in an unfinished declaration header + */ public boolean isTypeCompletion() { return getDeclarationType().isEmpty() && (getParent() instanceof IncompleteDeclarationStatement || getParent() instanceof IncompleteTypeCompositionStatement); } - /** Explicit receiver only; an unqualified call does not invent an implicit receiver. */ + /** + * Explicit receiver only; an unqualified call does not invent an implicit receiver. + * + * @return the explicit written receiver, or empty for an unqualified call or name query + */ public Optional getReceiver() { if (isNameCompletion() || getDeclarationType().isPresent()) { return Optional.empty(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java index 0d8614b543d..264ff197473 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java @@ -13,7 +13,6 @@ import org.xvm.compiler.Token.Id; import org.xvm.compiler.ast.Context; import org.xvm.compiler.ast.Parameter; -import org.xvm.compiler.ast.PartialQueries; import org.xvm.compiler.ast.StageMgr; import org.xvm.compiler.ast.Statement; import org.xvm.compiler.ast.StatementBlock; @@ -29,10 +28,33 @@ */ public final class IncompleteTypeCompositionStatement extends TypeCompositionStatement { + /** + * Retain a written type header and its query sites without making the declaration emittable. + * + * @param source the source that owns the retained declaration + * @param category the declaration's written category token + * @param name the written declaration name token, or null if absent + * @param start the declaration's inclusive source start + * @param end the declaration's exclusive source end + * @param cursors the incomplete type or name query sites retained in the header + */ public IncompleteTypeCompositionStatement(Source source, Token category, Token name, long start, long end, List cursors) { this(source, category, name, null, null, start, end, cursors, List.of()); } + /** + * Retain a written type header and its query sites without making the declaration emittable. + * + * @param source the source that owns the retained declaration + * @param category the declaration's written category token + * @param name the written declaration name token, or null if absent + * @param qualified the written qualified name tokens, or null for an unqualified declaration + * @param body the retained source body, or null if absent + * @param start the declaration's inclusive source start + * @param end the declaration's exclusive source end + * @param cursors the incomplete type or name query sites retained in the header + * @param formals written formal names and constraints, without invented declaration identities + */ public IncompleteTypeCompositionStatement(Source source, Token category, Token name, List qualified, StatementBlock body, long start, long end, List cursors, List formals) { super(source, category, name, start, end); this.cursors = List.copyOf(cursors); @@ -41,6 +63,17 @@ public IncompleteTypeCompositionStatement(Source source, Token category, Token n this.body = body; } + /** + * Retain a written type header and its query sites without making the declaration emittable. + * + * @param source the source that owns the retained declaration + * @param category the declaration's written category token + * @param name the written declaration name token, or null if absent + * @param body the retained source body, or null if absent + * @param start the declaration's inclusive source start + * @param end the declaration's exclusive source end + * @param cursors the incomplete type or name query sites retained in the header + */ public IncompleteTypeCompositionStatement(Source source, Token category, Token name, StatementBlock body, long start, long end, List cursors) { this(source, category, name, start, end, cursors); this.body = body; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialArgument.java similarity index 80% rename from javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java rename to javatools/src/main/java/org/xvm/compiler/ast/partial/PartialArgument.java index aa18d5e1d04..a9f2d0bb16c 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PartialArgument.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialArgument.java @@ -1,15 +1,19 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.util.List; import java.util.stream.IntStream; import java.util.stream.StreamSupport; -import org.xvm.compiler.ast.partial.IncompleteExpression; -import org.xvm.compiler.ast.partial.IncompleteStatement; -import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.LabeledExpression; +import org.xvm.compiler.ast.NonBindingExpression; +import org.xvm.compiler.ast.PartialAst; import org.xvm.compiler.ast.partial.PartialSyntax.ArgumentCursor; -/** Trial argument construction; lexical parenting stays inside the ordinary compiler package. */ +/** + * Builds disposable argument lists for incomplete-call candidate fitting. The ordinary compiler + * bridge supplies lexical parentage without installing proposals into the retained source tree. + */ final class PartialArgument { private PartialArgument() {} @@ -28,8 +32,7 @@ static List proposed(IncompleteStatement call, ArgumentCursor argume } private static List replace(IncompleteStatement call, int index, Expression value) { - value.setParent(call); - value.introduceParentage(); + PartialAst.parentTree(value, call); var arguments = call.getArguments(); return IntStream.range(0, arguments.size()) .mapToObj(i -> i == index ? value : arguments.get(i)).toList(); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialCallResolver.java similarity index 85% rename from javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java rename to javatools/src/main/java/org/xvm/compiler/ast/partial/PartialCallResolver.java index 117f0edd17a..e2e36c67fb4 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PartialCallResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialCallResolver.java @@ -1,14 +1,11 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.math.BigDecimal; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; -import java.util.HashSet; import java.util.List; import java.util.Objects; -import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -35,18 +32,38 @@ import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.LabeledExpression; +import org.xvm.compiler.ast.LambdaExpression; +import org.xvm.compiler.ast.ListExpression; +import org.xvm.compiler.ast.LiteralExpression; +import org.xvm.compiler.ast.MapExpression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NamedTypeExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.NonBindingExpression; +import org.xvm.compiler.ast.PartialAst; +import org.xvm.compiler.ast.ReturnStatement; +import org.xvm.compiler.ast.StageMgr; +import org.xvm.compiler.ast.StatementBlock; import org.xvm.compiler.ast.StatementBlock.TargetInfo; -import org.xvm.compiler.ast.partial.IncompleteStatement; -import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.ThrowExpression; +import org.xvm.compiler.ast.TupleExpression; import org.xvm.compiler.ast.partial.PartialSyntax.ArgumentCursor; -import org.xvm.compiler.ast.partial.ProposedLiteralToken; import org.xvm.util.PackedInteger; import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; -/** Explicit incomplete-call inspection on the compiler worker, while the lexical context exists. */ +/** + * Inspects incomplete calls on the compiler worker while their lexical contexts still exist. + * Reuses ordinary member lookup and argument fitting through {@link PartialAst}; the bridge + * keeps compiler internals out of the embedding API. + * Candidates remain possibilities; inspection does not select an emittable call. + */ final class PartialCallResolver { static CursorBinding inspect(IncompleteStatement site, Context ctx, TypeConstant required, ErrorListener errs) { if (site.getTarget() instanceof NewExpression creation) { @@ -121,32 +138,27 @@ private static CursorBinding methodScope(IncompleteStatement site, Context ctx, static List probeCallCandidate(AstNode site, Context ctx, TypeConstant target, TypeInfo info, MethodConstant method, List arguments, ErrorListener errs) { var written = arguments.stream().map(argument -> (Expression) argument.clone()).toList(); - var named = site.collectNamedArgs(written, errs); - if (named == null || errs.isAbortDesired()) { - return List.of(); - } - Set direct = new HashSet<>(); - Set converting = new HashSet<>(); List result = new ArrayList<>(); - site.collectMatchingMethods(ctx.enter(), target, info, Set.of(method), written, false, named, - null, direct, converting, new HashMap<>(), errs, (signature, ordered) -> { - var resolved = signature.resolveGenericTypes(site.pool(), target); - var original = info.getMethodById(method).getSignature(); - // A pending method formal is not a concrete expected type. Retain its written - // formal instead of leaking PendingTypeConstant or substituting Object. - var params = IntStream.range(0, resolved.getParamCount()) - .mapToObj(i -> resolved.getRawParams()[i].containsUnresolved() - ? original.getRawParams()[i] : resolved.getRawParams()[i]) - .toArray(TypeConstant[]::new); - var returns = IntStream.range(0, resolved.getReturnCount()) - .mapToObj(i -> resolved.getRawReturns()[i].containsUnresolved() - ? original.getRawReturns()[i] : resolved.getRawReturns()[i]) - .toArray(TypeConstant[]::new); - var copied = site.pool().ensureSignatureConstant(resolved.getName(), params, returns); - var declaration = info.getMethodById(method).getTopmostMethodStructure(info).getIdentityConstant(); - InvocationBinding.arguments(written, ordered).ifPresent(mapping -> - result.add(new CursorBinding.Candidate(declaration, copied, mapping, converting.contains(method)))); - }); + PartialAst.matchMethod(site, ctx, target, info, method, written, errs, match -> { + var signature = match.signature(); + var ordered = match.arguments(); + var resolved = signature.resolveGenericTypes(PartialAst.pool(site), target); + var original = info.getMethodById(method).getSignature(); + // A pending method formal is not a concrete expected type. Retain its written + // formal instead of leaking PendingTypeConstant or substituting Object. + var params = IntStream.range(0, resolved.getParamCount()) + .mapToObj(i -> resolved.getRawParams()[i].containsUnresolved() + ? original.getRawParams()[i] : resolved.getRawParams()[i]) + .toArray(TypeConstant[]::new); + var returns = IntStream.range(0, resolved.getReturnCount()) + .mapToObj(i -> resolved.getRawReturns()[i].containsUnresolved() + ? original.getRawReturns()[i] : resolved.getRawReturns()[i]) + .toArray(TypeConstant[]::new); + var copied = PartialAst.pool(site).ensureSignatureConstant(resolved.getName(), params, returns); + var declaration = info.getMethodById(method).getTopmostMethodStructure(info).getIdentityConstant(); + InvocationBinding.arguments(written, ordered).ifPresent(mapping -> + result.add(new CursorBinding.Candidate(declaration, copied, mapping, match.converting()))); + }); return List.copyOf(result); } @@ -165,7 +177,7 @@ static boolean validateArguments(Context ctx, CursorBinding.Candidate candidate, } var expected = candidate.signature().getRawParams()[formals + mapping.orElseThrow().parameterIndex()]; Expression value = written instanceof LabeledExpression label ? label.getUnderlyingExpression() : written; - Expression validated = ((Expression) value.clone()).validate(trial, expected, probe); + Expression validated = PartialAst.validate((Expression) value.clone(), trial, expected, probe); return validated != null && validated.getTypeFit().isFit() && !probe.hasSeriousErrors() && !probe.isAbortDesired(); }); @@ -202,8 +214,7 @@ static CursorBinding argumentValues(IncompleteStatement site, Context ctx, } Expression argument = site.getPendingArgumentName() .map(label -> new LabeledExpression(label, value)).orElse(value); - argument.setParent(site); - argument.introduceParentage(); + PartialAst.parentTree(argument, site); var arguments = new ArrayList<>(written); arguments.add(argument); return fits.test(arguments); @@ -225,10 +236,10 @@ static CursorBinding argumentValues(IncompleteStatement site, Context ctx, .filter(text -> fitsValue.test(proposedInstance(cursor, text))).toList(); var templates = qualified || !prefix.isEmpty() ? List.of() : Stream.concat(scope.candidates().stream().flatMap(candidate -> Arrays.stream(candidate.signature().getRawParams())), - scope.functions().stream().flatMap(function -> Arrays.stream(site.pool().extractFunctionParams(function.type())))) + scope.functions().stream().flatMap(function -> Arrays.stream(PartialAst.pool(site).extractFunctionParams(function.type())))) .map(TypeConstant::resolveTypedefs).map(TypeConstant::removeNullable) .filter(TypeConstant::isFunction) - .map(site.pool()::extractFunctionParams).filter(Objects::nonNull) + .map(PartialAst.pool(site)::extractFunctionParams).filter(Objects::nonNull) .map(parameters -> parameters.length).distinct().sorted() .takeWhile(arity -> !errs.isAbortDesired()) .filter(arity -> fitsValue.test(proposedLambda(cursor, arity))) @@ -241,8 +252,7 @@ static CursorBinding argumentValues(IncompleteStatement site, Context ctx, return result; } var lookup = ErrorListener.cancellable(ErrorListener.collecting(errs::log), errs::isAbortDesired); - var receiver = qualified ? ((Expression) cursor.getReceiver().orElseThrow().clone()) - .validate(ctx.enter(), null, lookup) : null; + var receiver = qualified ? PartialAst.validate((Expression) cursor.getReceiver().orElseThrow().clone(), ctx.enter(), null, lookup) : null; if (qualified && (receiver == null || !receiver.getTypeFit().isFit())) { return result; } @@ -278,8 +288,7 @@ private static Expression proposedLambda(IncompleteStatement site, int arity) { var returns = new ReturnStatement(new Token(cursor, cursor, Id.RETURN), body); var lambda = new LambdaExpression(names, new Token(cursor, cursor, Id.LAMBDA), new StatementBlock(new ArrayList<>(List.of(returns)), cursor, cursor), cursor); - lambda.setParent(site); - lambda.introduceParentage(); + PartialAst.parentTree(lambda, site); return lambda; } @@ -296,8 +305,7 @@ private static Expression proposedLiteral(IncompleteStatement site, String text, var map = new MapExpression(new NamedTypeExpression(null, List.of(new Token(cursor, cursor, Id.IDENTIFIER, "Map")), null, null, null, cursor), new ArrayList<>(), new ArrayList<>(), cursor); - map.setParent(site); - map.introduceParentage(); + PartialAst.parentTree(map, site); var probe = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); yield new StageMgr(map, Stage.Resolved, probe).fastForward(20) && !probe.hasSeriousErrors() && !probe.isAbortDesired() ? map : null; @@ -325,8 +333,7 @@ static Expression proposedInstance(IncompleteStatement site, String text) { long cursor = site.getEndPosition(); var expression = new NameExpression(new NameExpression(new Token(cursor, cursor, Id.IDENTIFIER, "this")), null, new Token(cursor, cursor, Id.IDENTIFIER, text.substring("this.".length())), null, cursor); - expression.setParent(site); - expression.introduceParentage(); + PartialAst.parentTree(expression, site); return expression; } @@ -335,7 +342,7 @@ private static CursorBinding.Property propertyValue(IncompleteStatement site, Co String name, ErrorListener errs) { var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); var expression = proposedName(site, name); - var value = expression.validate(ctx.enter(), null, validation); + var value = PartialAst.validate(expression, ctx.enter(), null, validation); return value != null && value.getTypeFit().isFit() && !validation.hasSeriousErrors() && !validation.isAbortDesired() && expression.getResolvedTarget() instanceof PropertyConstant identity @@ -348,8 +355,7 @@ private static NameExpression proposedName(IncompleteStatement site, String name var expression = !site.isCall() && site.getReceiver().isPresent() ? new NameExpression((Expression) site.getReceiver().orElseThrow().clone(), null, token, null, cursor) : new NameExpression(token); - expression.setParent(site); - expression.introduceParentage(); + PartialAst.parentTree(expression, site); return expression; } @@ -375,7 +381,7 @@ private static List function( } var validation = ErrorListener.cancellable(ErrorListener.collecting(silent(PROBE)::log), errs::isAbortDesired); var trial = ctx.enter(); - var callee = ((Expression) site.getTarget().clone()).validate(trial, null, validation); + var callee = PartialAst.validate((Expression) site.getTarget().clone(), trial, null, validation); if (callee == null || !callee.getTypeFit().isFit() || validation.hasSeriousErrors() || validation.isAbortDesired()) { return List.of(); } @@ -383,7 +389,7 @@ private static List function( if (!type.isFunction()) { return List.of(); } - var parameters = site.pool().extractFunctionParams(type); + var parameters = PartialAst.pool(site).extractFunctionParams(type); if (parameters == null || parameters.length < written.size()) { return List.of(); } @@ -394,7 +400,7 @@ private static List function( if (argument instanceof NonBindingExpression && PartialSyntax.argument(site).isPresent()) { return true; } - var value = argument.validate(trial, parameters[index], validation); + var value = PartialAst.validate(argument, trial, parameters[index], validation); return value != null && value.isSingle() && value.getTypeFit().isFit() && !validation.hasSeriousErrors() && !validation.isAbortDesired(); }); @@ -444,6 +450,7 @@ private static Target target(NameExpression callee, Context ctx, ErrorListener e return new Target(receiver.getType(), MethodKind.Method); } + /** Receiver type and lookup mode, including whether the receiver becomes the first argument. */ private record Target(TypeConstant type, MethodKind kind, boolean receiverArgument) { private Target(TypeConstant type, MethodKind kind) { this(type, kind, false); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialConstructionResolver.java similarity index 82% rename from javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java rename to javatools/src/main/java/org/xvm/compiler/ast/partial/PartialConstructionResolver.java index a40a3da035f..ba1b3e31839 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PartialConstructionResolver.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialConstructionResolver.java @@ -1,4 +1,4 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.util.ArrayList; import java.util.List; @@ -18,12 +18,21 @@ import org.xvm.compiler.CursorBinding; import org.xvm.compiler.Token.Id; -import org.xvm.compiler.ast.partial.IncompleteStatement; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.NonBindingExpression; +import org.xvm.compiler.ast.PartialAst; import static org.xvm.asm.ErrorListener.Silence.PROBE; import static org.xvm.asm.ErrorListener.silent; -/** Constructor cursor queries reuse normal type preparation and argument fitting. */ +/** + * Inspects incomplete construction using normal type preparation and argument fitting through + * {@link PartialAst}, without exposing {@link NewExpression}'s internal preparation methods. + * Stops before synthesis and emission, retaining candidate signatures for the cursor query + * instead of selecting a final constructor. + */ final class PartialConstructionResolver { static CursorBinding inspect(IncompleteStatement site, NewExpression creation, Context ctx, TypeConstant required, ErrorListener errs) { @@ -35,15 +44,15 @@ static CursorBinding inspect(IncompleteStatement site, NewExpression creation, C // An anonymous declaration owns its class shell in this analysis attempt. Preparing a // detached clone would attach orphan components to the enclosing source method instead. // Stop before forwarding-constructor synthesis, capture validation or emission. - boolean anonymous = creation.body != null; + boolean anonymous = PartialAst.hasAnonymousBody(creation); var trial = anonymous ? creation : (NewExpression) creation.clone(); - var construction = trial.prepareConstruction(ctx.enter(), required, validation, true); + var construction = PartialAst.prepareConstruction(trial, ctx.enter(), required, validation); if (construction == null || validation.hasSeriousErrors() || validation.isAbortDesired()) { return scope; } var lookup = ErrorListener.cancellable(ErrorListener.collecting(errs::log), errs::isAbortDesired); var info = anonymous ? construction.target().ensureTypeInfo(lookup) - : trial.getTypeInfo(ctx, construction.target(), lookup); + : PartialAst.typeInfo(trial, ctx, construction.target(), lookup); if (lookup.hasSeriousErrors() || lookup.isAbortDesired() || construction.requiresNewable() && !info.isNewable(false, validation)) { return scope; @@ -60,9 +69,8 @@ static CursorBinding inspect(IncompleteStatement site, NewExpression creation, C // Brackets select the size slot of the real fixed-size array constructor. // Other Array overloads (copy, mutability, capacity) are not dimension syntax. .filter(method -> site.getOperator().getId() != Id.L_SQUARE - || trial.type instanceof ArrayTypeExpression array - && target.getMethodById(method).getTopmostMethodStructure(target) - .getIdentityConstant().equals(array.getSupplyConstructor())) + || target.getMethodById(method).getTopmostMethodStructure(target) + .getIdentityConstant().equals(PartialAst.arrayConstructor(trial))) .filter(method -> anonymous || target.getType().getAccess() == Access.PRIVATE || target.getMethodById(method).isVisible(ctx.getThisClassId())) // A class shell's generated default is only a placeholder for superclass @@ -94,6 +102,7 @@ static CursorBinding inspect(IncompleteStatement site, NewExpression creation, C .anyMatch(candidate -> PartialCallResolver.validateArguments(ctx, candidate, arguments(site, values), errs))); } + /** Candidate constructor paired with the type information that supplies its signature. */ private record Constructor(TypeInfo info, MethodConstant method) {} private static List arguments(IncompleteStatement site, List values) { @@ -112,16 +121,14 @@ private static CursorBinding.Candidate infer(NewExpression creation, Context ctx var trial = ctx.enter(); List ordered = written.stream().map(value -> (Expression) value.clone()) .collect(Collectors.toCollection(ArrayList::new)); - if (creation.containsNamedArgs(ordered)) { - ordered = creation.rearrangeNamedArgs(method, ordered, validation); - if (ordered == null) { - return null; - } + ordered = PartialAst.orderArguments(creation, method, ordered, validation); + if (ordered == null) { + return null; } for (int index = 0; index < ordered.size() && !validation.isAbortDesired(); ++index) { var value = ordered.get(index); if (!(value instanceof NonBindingExpression)) { - var validated = value.validate(trial, candidate.signature().getRawParams()[index], validation); + var validated = PartialAst.validate(value, trial, candidate.signature().getRawParams()[index], validation); if (validated == null || !validated.getTypeFit().isFit()) { return null; } @@ -131,9 +138,9 @@ private static CursorBinding.Candidate infer(NewExpression creation, Context ctx if (validation.hasSeriousErrors() || validation.isAbortDesired()) { return null; } - var inferred = creation.inferTypeFromConstructor(trial, owner, method, ordered); + var inferred = PartialAst.inferConstruction(creation, trial, owner, method, ordered); return inferred == null ? candidate : new CursorBinding.Candidate(candidate.method(), - method.getIdentityConstant().getSignature().resolveGenericTypes(creation.pool(), inferred), + method.getIdentityConstant().getSignature().resolveGenericTypes(PartialAst.pool(creation), inferred), candidate.arguments(), candidate.converting()); } } diff --git a/javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialQueries.java similarity index 55% rename from javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java rename to javatools/src/main/java/org/xvm/compiler/ast/partial/PartialQueries.java index dc64ec8be9b..0eb425f6028 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/PartialQueries.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialQueries.java @@ -1,4 +1,4 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.util.List; import java.util.Optional; @@ -8,21 +8,33 @@ import org.xvm.asm.constants.TypeConstant; import org.xvm.compiler.CursorBinding; -import org.xvm.compiler.ast.partial.IncompleteStatement; -import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.AstNode; +import org.xvm.compiler.ast.Context; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.LabeledExpression; +import org.xvm.compiler.ast.LambdaExpression; +import org.xvm.compiler.ast.NonBindingExpression; +import org.xvm.compiler.ast.Parameter; +import org.xvm.compiler.ast.PartialAst; /** - * Compiler-internal semantic operations for retained incomplete syntax. Public only to connect - * the partial syntax package to ordinary validation and inference; embedding hosts query their - * compilation results instead. Contexts and bindings belong to the current compilation attempt. - * This service retains no state and never turns an incomplete operation into an emittable value. + * Semantic entry points used by retained incomplete nodes. Contexts and bindings belong to the + * current compilation attempt; embedding hosts consume its published results instead of calling + * these operations. The ordinary compiler bridge supplies validation without widening AST member + * visibility. No incomplete operation becomes an emittable value. */ -public final class PartialQueries { +final class PartialQueries { private PartialQueries() {} - /** Inspect intact children in their real context, without selecting the incomplete operation. */ - public static void inspect(IncompleteStatement site, Context ctx, TypeConstant required, - ErrorListener errs) { + /** + * Inspect intact children in their real context, without selecting the incomplete operation. + * + * @param site the retained incomplete source site + * @param ctx the live compiler context that owns the query + * @param required the contextual required type, or null when unconstrained + * @param errs the diagnostic listener and abort policy for this operation + */ + static void inspect(IncompleteStatement site, Context ctx, TypeConstant required, ErrorListener errs) { var bindings = ctx.getCursorBindings(); bindings.begin(site); if (bindings.isEnabled() && !errs.isAbortDesired()) { @@ -32,7 +44,7 @@ public static void inspect(IncompleteStatement site, Context ctx, TypeConstant r ? scope.withTypes(CursorScope.types(site, ctx, errs)) : scope); } site.getReceiver().ifPresent(receiver -> { - Expression validated = receiver.validate(ctx, null, errs); + Expression validated = PartialAst.validate(receiver, ctx, null, errs); if (validated != null) { AstNode owner = site.isCall() ? site.getTarget() : site; owner.replaceChild(receiver, validated); @@ -42,7 +54,7 @@ public static void inspect(IncompleteStatement site, Context ctx, TypeConstant r if (partialTarget && !errs.isAbortDesired()) { // A member prefix before existing arguments owns the original receiver. Inspect it // in place; a trial callee clone cannot supply bindings for the retained source site. - site.getTarget().validate(ctx, null, errs); + PartialAst.validate(site.getTarget(), ctx, null, errs); } CursorBinding call = bindings.isEnabled() && site.isCall() && !partialTarget && !errs.isAbortDesired() ? PartialCallResolver.inspect(site, ctx, required, errs) : null; @@ -54,7 +66,7 @@ public static void inspect(IncompleteStatement site, Context ctx, TypeConstant r Expression value = argument instanceof LabeledExpression labeled ? labeled.getUnderlyingExpression() : argument; if (!(value instanceof NonBindingExpression || value instanceof LambdaExpression)) { - Expression validated = argument.validate(ctx, null, errs); + Expression validated = PartialAst.validate(argument, ctx, null, errs); if (validated != null) { site.replaceChild(argument, validated); } @@ -65,8 +77,17 @@ public static void inspect(IncompleteStatement site, Context ctx, TypeConstant r } } - /** Resolve an unfinished header in its explicit lexical scope, without registering formals. */ - public static Optional declarationBinding(IncompleteStatement site, AstNode scope, + /** + * Resolve an unfinished header in its explicit lexical scope, without registering formals. + * + * @param site the retained incomplete source site + * @param scope the enclosing lexical scope used to resolve the unfinished header + * @param writtenFormals written formal parameters that shadow outer names without registering identities + * @param errs the diagnostic listener and abort policy for this operation + * + * @return visible type and formal facts, or empty when no enclosing class scope is available + */ + static Optional declarationBinding(IncompleteStatement site, AstNode scope, List writtenFormals, ErrorListener errs) { return scope.getComponent() instanceof ClassStructure owner ? Optional.of(new CursorBinding(List.of(), owner.getFormalType(), false) diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java index 94edc80090b..1e67cb5e407 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java @@ -23,7 +23,13 @@ private PartialSyntax() { // empty } - /** Include deferred anonymous source bodies without changing compiler child ownership. */ + /** + * Include deferred anonymous source bodies without changing compiler child ownership. + * + * @param node the syntax node to inspect, or null for the containment queries + * + * @return the direct children, including a retained anonymous source body when present + */ public static Stream children(AstNode node) { var children = StreamSupport.stream(node.childNodes().spliterator(), false); return node instanceof NewExpression creation ? Stream.concat(children, creation.getUnregisteredBody().stream()) : children; @@ -32,12 +38,25 @@ public static Stream children(AstNode node) { /** A value cursor and its written argument slot; no inferred type or candidate is retained. */ public record ArgumentCursor(IncompleteStatement cursor, int index) {} - /** Whether this node or any descendant is retained incomplete syntax; null has no syntax. */ + /** + * Whether this node or any descendant is retained incomplete syntax; null has no syntax. + * + * @param node the syntax node to inspect, or null for the containment queries + * + * @return whether this node or a descendant retains an incomplete site; false for null + */ public static boolean contains(AstNode node) { return contains(node, site -> true); } - /** Whether a retained site ends at exactly this source cursor, including beneath nested calls. */ + /** + * Whether a retained site ends at exactly this source cursor, including beneath nested calls. + * + * @param node the syntax node to inspect, or null for the containment queries + * @param cursor the selected position in the owning Source snapshot + * + * @return whether a retained site ends at exactly the supplied source position + */ public static boolean containsAt(AstNode node, long cursor) { return contains(node, site -> site.getEndPosition() == cursor); } @@ -47,7 +66,13 @@ private static boolean contains(AstNode node, Predicate mat || children(node).anyMatch(child -> contains(child, matches))); } - /** Find a value cursor in a written argument, excluding array-dimension slots. */ + /** + * Find a value cursor in a written argument, excluding array-dimension slots. + * + * @param call the retained incomplete call whose arguments are inspected + * + * @return the first supported written argument containing a value cursor, or empty if none exists + */ public static Optional argument(IncompleteStatement call) { // Multiple array dimensions are not positional arguments to an ordinary constructor. if (call.getOperator().getId() == Id.L_SQUARE) { @@ -63,6 +88,11 @@ public static Optional argument(IncompleteStatement call) { /** * Find the single value cursor inside labels, groups or compound expressions. A nested call, * construction or lambda owns its own arguments/scope and is never searched through here. + * + * @param expression the written value expression to inspect + * + * @return the unique value cursor in the expression, or empty when absent, ambiguous or behind a call/scope + * boundary */ public static Optional valueCursor(Expression expression) { if (expression instanceof IncompleteExpression hole) { @@ -77,7 +107,13 @@ public static Optional valueCursor(Expression expression) { return sites.size() == 1 ? Optional.of(sites.getFirst()) : Optional.empty(); } - /** Find the real call containing a direct, labeled, grouped or compound argument cursor. */ + /** + * Find the real call containing a direct, labeled, grouped or compound argument cursor. + * + * @param site the retained incomplete source site + * + * @return the real containing incomplete call, or empty outside a supported argument context + */ public static Optional argumentCall(IncompleteStatement site) { AstNode parent = site.getParent(); while (parent instanceof Expression expression && !isArgumentBoundary(expression)) { diff --git a/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java b/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java index d6eb8c6a227..5f1b5031cb5 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java @@ -11,6 +11,14 @@ public final class ProposedLiteralToken extends Token { private final String spelling; + /** + * Create a zero-width trial literal with an explicit source spelling. + * + * @param cursor the selected position in the owning Source snapshot + * @param id the token kind for the proposed literal + * @param value the proposed literal's parsed value + * @param spelling the source spelling to use for literal conversion + */ public ProposedLiteralToken(long cursor, Id id, Object value, String spelling) { super(cursor, cursor, id, value); this.spelling = spelling; diff --git a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java index 334ce828e09..553d4e2edc4 100644 --- a/javatools/src/main/java/org/xvm/tool/ModuleInfo.java +++ b/javatools/src/main/java/org/xvm/tool/ModuleInfo.java @@ -74,22 +74,22 @@ public class ModuleInfo { * @param moduleName the module identity obtained from the host's source snapshot */ protected ModuleInfo(File sourceFile, String moduleName) { - this.fileSpec = Objects.requireNonNull(sourceFile, "sourceFile").getAbsoluteFile(); - this.moduleName = Objects.requireNonNull(moduleName, "moduleName"); - if (moduleName.isBlank() || !isExplicitSourceFile(fileSpec.getName())) { + this.fileSpec = Objects.requireNonNull(sourceFile, "sourceFile").getAbsoluteFile(); + this.moduleName = Objects.requireNonNull(moduleName, "moduleName"); + if (moduleName.isBlank() || !isExplicitSourceFile(this.fileSpec.getName())) { throw new IllegalArgumentException("A module name and .x source root are required"); } - this.sourceFile = fileSpec; - deduce = false; - fileName = removeExtension(fileSpec.getName()); - sourceDir = fileSpec.getParentFile(); - projectDir = sourceDir; - sourceStatus = Status.Exists; - sourceContent = Content.Module; - resourceDir = NoResources; - binaryDir = sourceDir; - binaryStatus = Status.NotExists; - binaryContent = Content.Invalid; + this.sourceFile = this.fileSpec; + this.deduce = false; + this.fileName = removeExtension(this.fileSpec.getName()); + this.sourceDir = this.fileSpec.getParentFile(); + this.projectDir = this.sourceDir; + this.sourceStatus = Status.Exists; + this.sourceContent = Content.Module; + this.resourceDir = NoResources; + this.binaryDir = this.sourceDir; + this.binaryStatus = Status.NotExists; + this.binaryContent = Content.Invalid; } /** @@ -717,6 +717,11 @@ public Node getSourceTree(ErrorListener errs) { * partial analysis while retaining the same source snapshot, resources and package assembly. * The parser must report other syntax errors to its supplied listener; incomplete syntax * must never be passed to ordinary compilation. Use a fresh ModuleInfo for each attempt. + * + * @param errs the diagnostic listener and abort policy for this operation + * @param parse the parser factory applied to each discovered source with its diagnostic listener + * + * @return the assembled source root, or null if loading or parsing prevented assembly */ public Node getSourceTree(ErrorListener errs, BiFunction parse) { @@ -884,6 +889,8 @@ protected String resourcePathPart() { /** * Parse while observing the host's abort request. The no-argument entry point is retained * for CLI consumers that drain the node's diagnostics themselves. + * + * @param errs the diagnostic listener and abort policy for this operation */ public void parse(ErrorListener errs) { if (!errs.isAbortDesired()) { @@ -891,7 +898,12 @@ public void parse(ErrorListener errs) { } } - /** Parse with an attempt-owned strategy; existing custom nodes retain their own parser. */ + /** + * Parse with an attempt-owned strategy; existing custom nodes retain their own parser. + * + * @param errs the diagnostic listener and abort policy for this operation + * @param parse the parser factory applied to each discovered source with its diagnostic listener + */ public void parse(ErrorListener errs, BiFunction parse) { parse(errs); } diff --git a/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java index e1e4ddcd3ac..0f0c7015ee0 100644 --- a/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java +++ b/javatools/src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java @@ -111,6 +111,7 @@ private static Token name() { return new Token(0, 4, Id.IDENTIFIER, "call"); } + /** Gives distinct call nodes equal values so the test detects accidental equality-based binding maps. */ private static class EqualInvocation extends InvocationExpression { EqualInvocation() { super(new NameExpression(name()), false, List.of(), 4); @@ -127,6 +128,7 @@ public int hashCode() { } } + /** Gives distinct construction nodes equal values so their compiler facts must still be keyed by identity. */ private static class EqualConstruction extends NewExpression { EqualConstruction() { super(null, new Token(0, 3, Id.NEW, null), null, List.of(), -1, null, 4); @@ -143,6 +145,7 @@ public int hashCode() { } } + /** Builds minimal compilation and partial-analysis results around the same source tree and pool. */ private record HostInputs(FileStructure file, StatementBlock tree) { HostInputs() { this(new FileStructure("IdentityFacts"), new StatementBlock(List.of())); diff --git a/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java b/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java index ed7cb87a575..613204e6a38 100644 --- a/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java +++ b/javatools/src/test/java/org/xvm/api/EmbeddingFootprintTest.java @@ -8,6 +8,10 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; +/** + * Ensures compiler memory observations inspect the supplied attempt without accidentally bootstrapping the + * runtime. Keeps lightweight language-server status queries separate from interpreter initialization. + */ public class EmbeddingFootprintTest { @Test public void snapshotDoesNotBootstrapTheRuntime() { diff --git a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java index 6de7a207594..917ecbfaf82 100644 --- a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java @@ -27,6 +27,10 @@ import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; +/** + * Exercises directory repository reads with corrupt, incompatible and repaired candidates. Protects both + * best-effort fallback to usable modules and delivery of the original read failure to an observing host. + */ class DirRepositoryFailureTest { @TempDir Path directory; diff --git a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java index 0896a705e6d..cc0ead928e8 100644 --- a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java @@ -25,6 +25,10 @@ import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; +/** + * Exercises failed and repaired module-file reads, including repairs that preserve file metadata. Ensures + * failure observations do not turn write errors into read reports or leave failed reads permanently cached. + */ class FileRepositoryFailureTest { @TempDir Path directory; diff --git a/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java b/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java index ed1cbe20c9b..2b29a4c5558 100644 --- a/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java +++ b/javatools/src/test/java/org/xvm/compiler/EvalCompilerTest.java @@ -4,6 +4,10 @@ import static org.junit.jupiter.api.Assertions.assertTrue; +/** + * Checks that an evaluation compiler owns a usable diagnostic buffer before compilation begins. Callers must + * not depend on a later pipeline phase to initialize reporting. + */ class EvalCompilerTest { @Test void diagnosticsAreAvailableBeforeCompilation() { diff --git a/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java b/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java index fc2be4d1507..e3b1b34bd66 100644 --- a/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java @@ -168,5 +168,6 @@ private static Stream slots(AstNode root) { .filter(site -> site.getDeclarationType().isPresent()); } + /** Retains the recovered tree, cursor and diagnostics together for declaration-name assertions. */ private record Parsed(StatementBlock tree, Source source, long cursor, ErrorList errors) {} } diff --git a/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java b/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java index a12b7f0f219..a798c771ef0 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java @@ -113,6 +113,7 @@ void parameterClonesRetainKnownTargetsButRebindingDoesNotRewriteTheOriginalSlot( } } + /** Controls when a nested class scope becomes resolvable so retries can be checked for source provenance. */ private static class ResolvingScope extends AstNode { private final FileStructure file = new FileStructure("Provenance"); private final Source source = new Source("Outer.Inner.Missing"); diff --git a/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java index 9324df80465..16b664e3d6c 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java @@ -86,6 +86,7 @@ void nestedResolutionRestoresTheOuterCallbacksListener() { assertEquals(1, inner.getErrors().size()); } + /** Routes name resolution through a supplied component resolver to observe diagnostic ownership. */ private static class ResolvingNode extends AstNode { ResolvingNode(ComponentResolver resolver) { f_resolver = resolver; diff --git a/javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java b/javatools/src/test/java/org/xvm/compiler/ast/partial/PartialSyntaxTest.java similarity index 94% rename from javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java rename to javatools/src/test/java/org/xvm/compiler/ast/partial/PartialSyntaxTest.java index a5290627530..ecf84ded5d9 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/PartialSyntaxTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/partial/PartialSyntaxTest.java @@ -1,4 +1,4 @@ -package org.xvm.compiler.ast; +package org.xvm.compiler.ast.partial; import java.util.List; import java.util.function.UnaryOperator; @@ -8,9 +8,18 @@ import org.xvm.compiler.Parser; import org.xvm.compiler.Token; import org.xvm.compiler.Token.Id; -import org.xvm.compiler.ast.partial.IncompleteExpression; -import org.xvm.compiler.ast.partial.IncompleteStatement; -import org.xvm.compiler.ast.partial.PartialSyntax; +import org.xvm.compiler.ast.Expression; +import org.xvm.compiler.ast.ExpressionStatement; +import org.xvm.compiler.ast.InvocationExpression; +import org.xvm.compiler.ast.LabeledExpression; +import org.xvm.compiler.ast.LambdaExpression; +import org.xvm.compiler.ast.NameExpression; +import org.xvm.compiler.ast.NewExpression; +import org.xvm.compiler.ast.NonBindingExpression; +import org.xvm.compiler.ast.ParenthesizedExpression; +import org.xvm.compiler.ast.PartialAst; +import org.xvm.compiler.ast.RelOpExpression; +import org.xvm.compiler.ast.StatementBlock; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -134,7 +143,7 @@ public void dimensionsAreNotOrdinaryArgumentSlots() { var creation = new NewExpression(null, token(Id.NEW), null, List.of(), 0, null, 40); var dimensions = new IncompleteStatement(creation, token(Id.L_SQUARE), List.of(value), List.of(), 40); - dimensions.introduceParentage(); + PartialAst.parentTree(dimensions, null); assertTrue(PartialSyntax.containsAt(dimensions, 20)); assertTrue(PartialSyntax.argument(dimensions).isEmpty()); @@ -210,7 +219,7 @@ private static IncompleteStatement call(Expression argument) { var call = new IncompleteStatement(name("work"), token(Id.L_PAREN), List.of(name("first"), argument, name("last")), List.of(token(Id.COMMA), token(Id.COMMA)), 100); - call.introduceParentage(); + PartialAst.parentTree(call, null); return call; } diff --git a/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java b/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java index c7a614086bc..abe38b1dfb2 100644 --- a/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java +++ b/javatools/src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java @@ -90,6 +90,7 @@ private static NameExpression name(String name) { return new NameExpression(new Token(0, name.length(), Id.IDENTIFIER, name)); } + /** Supplies an inherited child field that must participate in traversal and copying. */ private abstract static class Parent extends Statement { Parent(Expression receiver) { this.receiver = adopt(receiver); @@ -123,6 +124,7 @@ public String toString() { private Expression receiver; } + /** Combines inherited and local registered children with a deliberately unregistered expression. */ private static final class Child extends Parent { Child(Expression receiver, List arguments) { super(receiver); diff --git a/lang/.editorconfig b/lang/.editorconfig new file mode 100644 index 00000000000..8932985d6dd --- /dev/null +++ b/lang/.editorconfig @@ -0,0 +1,3 @@ +[*.{kt,kts}] +# Keep single-expression when entries concise even when another entry needs a block. +ktlint_standard_when-entry-bracing = disabled diff --git a/lang/.gitignore b/lang/.gitignore index 97d72543dce..e702e6d4c1c 100644 --- a/lang/.gitignore +++ b/lang/.gitignore @@ -17,3 +17,6 @@ # lang/dsl). package-lock.json IS tracked at the subpackage level for # reproducible installs; node_modules/ is the install output. node_modules/ + +# Shared Kotlin formatting rules are part of this build, unlike the personal root config. +!.editorconfig diff --git a/lang/README.md b/lang/README.md index c8436fdd0e2..b37b6eb3bfe 100644 --- a/lang/README.md +++ b/lang/README.md @@ -102,9 +102,10 @@ The test outputs parse timing sorted by slowest files, helping identify grammar ## Kotlin Formatting -Spotless runs ktlint's default rules over Kotlin sources, tests, IntelliJ integration tests +Spotless runs ktlint over Kotlin sources, tests, IntelliJ integration tests and the `lang` Gradle scripts, using the same formatter versions as master. Versions are pinned in -the shared version catalog. Generated build outputs are excluded. From the repository root: +the shared version catalog. The local `.editorconfig` permits concise single-expression `when` +entries alongside entries that need blocks. Generated build outputs are excluded. From the repository root: ```bash # Format all lang Kotlin sources and Gradle scripts @@ -291,24 +292,18 @@ Generated files: ``` Compiler mode is the default. Its bounded semantic features and dependency host API are described in the -[LSP server README](./lsp-server/README.md); the capability matrix below records supported requests -and remaining project/workspace limits. No external XDK installation is required. +[LSP server README](./lsp-server/README.md), including supported requests and remaining +project/workspace limits. No external XDK installation is required. -## Canonical Status Documents +## Feature and Architecture Guides -To avoid duplicating stale implementation matrices across multiple READMEs, the canonical -status documents are: - -- [`doc/plans/plan-ide-integration.md`](./doc/plans/plan-ide-integration.md) - - current implementation matrix across adapters and IDE integration status -- [`doc/plans/plan-tree-sitter.md`](./doc/plans/plan-tree-sitter.md) - - tree-sitter architecture and implementation status -- [`doc/plans/existing-tree-sitter-functionality-missing.md`](./doc/plans/existing-tree-sitter-functionality-missing.md) - - current tree-sitter implemented vs remaining functionality -- [`lsp-server/README.md`](./lsp-server/README.md) - - LSP-server-specific build, runtime, and configuration details -- [`intellij-plugin/README.md`](./intellij-plugin/README.md) - - IntelliJ-plugin-specific build/testing/runtime details +- [LSP server](./lsp-server/README.md): adapter capabilities, runtime and configuration. +- [Manual playbook](./doc/manual-test-plan.md): runnable examples, native IDE tests and feature limits. +- [Compiler architecture](../javatools/README.md#compiler-architecture-for-language-tooling): + why tooling needs compiler facts, diagnostic ownership and bounded partial analysis. +- [IntelliJ plugin](./intellij-plugin/README.md) and [VS Code extension](./vscode-extension/README.md): + installation, settings and publication. +- [Tree-sitter](./tree-sitter/README.md): grammar and native library builds. ## Dependency Versions & Compatibility @@ -368,11 +363,9 @@ All versions are defined in `/gradle/libs.versions.toml`. ## Documentation -- [Implementation Plans](./doc/plans/README.md) - Current plans, status docs, and retained design notes for language tooling +- [Historical design notes](./doc/plans/README.md) - Background proposals; use the feature guides above for current behavior - [LSP Implementation Survey](./doc/lsp-implementations-survey.md) - Survey of how other languages implement language server support -Architecture analysis and research documentation: *Internal documentation* - ## TODOs - [ ] **Language naming consistency**: The formal language name is "Ecstasy", but the codebase currently uses "xtc" for technical identifiers (scope names, language IDs, file names). A future PR should audit and unify the naming to use "Ecstasy" where appropriate for user-facing elements while keeping "xtc" for technical identifiers where consistency is important (e.g., `source.xtc` scope name, `xtc.tmLanguage.json` file names). diff --git a/lang/doc/README.md b/lang/doc/README.md index 8b57bd1e407..702a4ff6cb8 100644 --- a/lang/doc/README.md +++ b/lang/doc/README.md @@ -1,9 +1,14 @@ # Ecstasy Language Tooling Documentation -## Implementation Plans +## Build, use and test the language tools -See [plans/](./plans/) for implementation plans and roadmaps. +- [Language server](../lsp-server/README.md): compiler and Tree-sitter adapters, supported features and configuration. +- [IntelliJ plugin](../intellij-plugin/README.md) and [testing](../intellij-plugin/TESTING.md): native actions, settings and test harnesses. +- [VS Code extension](../vscode-extension/README.md): installation, commands and native tests. +- [Manual playbook](manual-test-plan.md): copyable examples, expected behavior, automated selections and client limitations. +- [Compiler architecture](../../javatools/README.md#compiler-architecture-for-language-tooling): why the embedding and source-analysis APIs are needed. -## Architecture Research +## Background design notes -Comprehensive architecture analysis and design documentation: *Internal documentation* +The [retained design notes](plans/) describe earlier design work. Use the feature and testing guides +above for the behavior of the current implementation. diff --git a/lang/doc/manual-test-plan.md b/lang/doc/manual-test-plan.md index 4f7e25ce70e..92500f3a593 100644 --- a/lang/doc/manual-test-plan.md +++ b/lang/doc/manual-test-plan.md @@ -31,9 +31,9 @@ notifications are failures; do not replay a timed-out mutation to manufacture a ## Client compatibility limits - VS Code's native batch-move and overlapping-move Undo paths have recorded host failures - (X130/UP16 and X218/UP23). The cases remain assertions, not silently skipped successes. -- IntelliJ uses the supported read-only file fallback for library content (X254/UP25). - X257/UP26 covers inline-completion constraints partly through the installed connection because + (X130 and X218). The cases remain assertions, not silently skipped successes. +- IntelliJ uses the supported read-only file fallback for library content (X254). + X257 covers inline-completion constraints partly through the installed connection because released LSP4IJ does not expose all required native invocation metadata. Both report partial coverage. - The optional color-value prototype is disabled by default. X273–X275 explicitly enable it; its native swatch refresh has a recorded VS Code failure. Theme-based source highlighting is @@ -59,7 +59,7 @@ The native project-lifecycle tests exercise ownership and process exit. Check th separately from the feature catalog; old processes from another run are not evidence of a leak in the current run. -## IntelliJ startup editing acceptance (L56) +## IntelliJ startup editing acceptance Run in a fresh compiler-mode project before waiting for the Language Servers panel to report ready. The unit tests cover notification permutations; these steps exercise the actual client/editor path. @@ -83,7 +83,7 @@ The unit tests cover notification permutations; these steps exercise the actual The report includes `startup-editing.json` (PID/version/text stamps and phase results) and `server-trace/` (one execution trace per child process). X103's forward/reverse file rename also passes after removing the transport read lock that caused a real EDT deadlock. This regression -was unrelated to focus loss. X42/X97/X108 contain the shared L57–L59 semantic variants. +was unrelated to focus loss. X42/X97/X108 contain the shared inlay, completion and declaration-context variants. ## Current development batch: workspace queries and editing @@ -140,7 +140,7 @@ or Undo against an already modified fixture. Focused automated gate after compilation/unit checks: CFG2, X57, X101, X105, X118, X122–X134 in each host, plus the existing native lens/link/inlay/workspace-navigation checks when their display path changed. Run both newly added and modified tests. A full catalog checkpoint -and longer idle/restart/memory runs remain the submission gate under L82; selected cases alone +and longer idle/restart/memory runs remain part of responsiveness and lifecycle acceptance; selected cases alone cannot establish full playbook completion. Backend command for the save/sync/range checks (no editor window): @@ -909,7 +909,7 @@ Workspace folders take precedence; extra roots are merged in. Non-existent paths ## Adapter Comparison Summary -> See [plan-ide-integration.md](plans/plan-ide-integration.md#adapter-capability-matrix) for the canonical adapter comparison matrix. +> See the [backend comparison](../lsp-server/README.md#backend-comparison) for compiler and Tree-sitter feature boundaries. --- @@ -1194,7 +1194,7 @@ colors, and a theme can give multiple categories the same appearance. #### Compiler semantic-color prototype This is the requested source-highlighting feature. It uses the IDE theme and requires no new -Ecstasy library or application-color API. The separate L77 picker experiment below is opt-in. +Ecstasy library or application-color API. The separate color-value picker experiment below is opt-in. Open [`semantic-colors.code-workspace`](../test-fixtures/semantic-highlighting/semantic-colors.code-workspace) in VS Code with the compiler extension, then open `SemanticColors.x`. The workspace only adds @@ -1208,7 +1208,7 @@ Inspect lowercase `counter` as `class`, uppercase `Count` as `parameter`, `count `next` as `variable`, `Reader` and imported `List` as `interface`, `Mode` as `enum`, and both uses of `Quiet` as `enumMember`. `create` is `function.static` at its declaration and call; `read` is `method`. The `count += 1` target has `modification`; its reads do not. This is source-code -highlighting, separate from L77 color-value swatches and pickers. +highlighting, separate from color-value swatches and pickers. `XdkSemanticColorTest` compiles this fixture and checks classifications. Shared X41/X154 check functions, enum values, imported interfaces and writes in both editor drivers. Both cases pass in @@ -1219,7 +1219,7 @@ names used as parameters/properties/locals and incomplete-member edits followed --- -### L77 color-value prototype +### Experimental color-value picker This opt-in experiment is separate from syntax/semantic highlighting. The ordinary fixture [`ColorPrototype.x`](../test-fixtures/color-values/ColorPrototype.x) defines a four-byte `Rgba` @@ -1748,12 +1748,11 @@ module Consumers { | X43 | Keep hierarchy items open, insert a blank line before `run`, then reopen hierarchy. Break the module with an unfinished declaration, correct it and close/reopen the file. | Fresh results use current ranges. Old hierarchy items do not resolve against the edited snapshot. A parse failure clears semantic answers; correction restores them. | | X44 | In the section D two-file fixture, add `static Int target(Int n) = n;` to Project and `Int callTarget() = target(1);` to Child, then close Child and show incoming calls on `target`. | `callTarget` and its call site point to the closed Child source. An unsaved member edit moves the result; stale positions are not reused. | -Also unavailable: separate Go to Declaration, document links and linked editing. The bounded -rename checks below exercise recompilation plus binding comparison. The interactive fixtures -do not configure dependency artifacts; source navigation through the host API is tested separately -below. Workspace-wide reference/implementation searches and external or conditional-mixin hierarchy -remain open. No Tree-sitter fallback runs in compiler mode. Check the -[capability matrix](plans/plan-ide-integration.md#adapter-capability-matrix) when those limits change. +The bounded rename checks below exercise recompilation plus binding comparison. Declaration +navigation, links, linked editing, cross-module queries and dependency navigation have their own +scenarios later in this playbook. Passing these initial fixtures does not establish coverage of +those features. Compiler mode does not run a Tree-sitter fallback; consult the +[backend comparison](../lsp-server/README.md#backend-comparison) for its feature boundaries. Record the commit, editor/version, confirmed backend, case ID, source/unsaved edits, expected and actual result, and relevant server-log lines for failures. Mark unsupported client actions and @@ -1903,7 +1902,7 @@ accepted edits before continuing. These are bounded semantic edits, not general | X57 | Start a rename and edit another file in the same module, close/reopen the target, or change its version before applying. | Pending work is canceled or rejected as changed; an edit for an old open-buffer version is not applied. Fast machines may need the controlled server regression below to exercise this race. | | X58 | Introduce a syntax error, try rename, fix it and retry. Try an invalid identifier or an existing local name. | Broken/unsupported/conflicting requests give no edits or temporary diagnostics. A valid rename works again after correction. | -#### Additional L62 rename checks +#### Additional compiler-validated rename checks Shared cases **X109–X117** now carry these sources, expected edits and file destinations for both editor drivers. Each starts with other consumers closed and checks a single Undo against @@ -2031,7 +2030,7 @@ module Dormant { | # | Action | Expected result | |---|--------|-----------------| | X59 | Find References on the interface's `map` in Contracts.x, with Uses.x and Dormant.x unopened. | The declaration and the two calls through `api.Mapper` appear. The concrete override and `impl.map` have their own identity and are excluded from this exact reference query. | -| X60 | Rename that `map` to `convert`; inspect the preview, apply, then undo. | Five edits across all three files: the contract, concrete override, and all three calls. No diagnostics after recompilation. Closed files have null edit versions; open buffers carry their current versions. IntelliJ persists edited closed consumers through the UP21 bridge before opening them; the original open buffer stays unsaved. | +| X60 | Rename that `map` to `convert`; inspect the preview, apply, then undo. | Five edits across all three files: the contract, concrete override, and all three calls. No diagnostics after recompilation. Closed files have null edit versions; open buffers carry their current versions. IntelliJ persists edited closed consumers through the closed-document persistence bridge before opening them; the original open buffer stays unsaved. | | X61 | Rename Contracts.Base's `pick` to `choose`. | No edit. The unchanged `box.choose(1)` would select a different overload even though the edited graph compiles. | | X62 | Open Uses.x, invoke signature help inside `indexOf('a')`, then try renaming `indexOf`. Separately try renaming Named's `toString` override. | The bundled XDK supplies the Char overload's signature; binary `String.indexOf` cannot be renamed. The source override also cannot be renamed because its contract belongs to the bundled XDK. These are resolved binary targets, not missing dependencies. | | X63 | Add a second `mapper.map("d")` call to Dormant's return expression without saving, query references/rename, then temporarily replace its body with `MissingType broken;`. Restore the fixture. | Queries include the unsaved call and rename uses its current buffer version. An incomplete configured graph gives no reference list or rename edit; proof compilations add no diagnostics of their own. Restoring it restores results. | @@ -2225,7 +2224,7 @@ module Advanced { | X94 | In temporary `Editing.x`, complete `Str` in `List`, `Map>`, `Map`, `List<(Int \| Str)>`, `Object + Str` and `Object - Str` parameter headers. Also try `List` property/return types and `List`. Use the shared X94 variants. Finally remove both `>` from the nested `Map` header, accept `String`, then restore `>>`. | Only the selected leaf token changes; generic arguments, compound operators and qualifiers stay intact. Values such as `StringValue` are excluded and no call signature appears. Complete accepted headers clear Problems. Missing `>` still reports an error after acceptance; adding the closers clears it. Type suggestions establish visibility, not generic-constraint compatibility. Both drivers consume the same nine variants. | | X95 | In temporary `Editing.x`, use the shared X95 replacement program. Complete the marked type in class `extends`, interface `extends`, `implements`, `delegates`, `incorporates` and mixin `into` headers. Also try `Owner.Nes`, `List` and the missing-`>` variant. Accept the selected entry, then restore the repaired declaration. | Completion replaces only the final token and offers visible types rather than values. Complete accepted headers clear Problems; accepting `String` with a missing `>` leaves an error until the closer is restored. No signature appears in a type slot. Both drivers consume the same nine variants; native IntelliJ assertions pass. | | X96 | In temporary `Editing.x`, use the shared X96 replacement program. Complete registered `Element` in a type prefix and empty generic slot, `String` before a later generic argument, and `Item`/`Alias` after `Owner`. Put the cursor inside `String`, `StringBuffer` and `Item`, then accept the selected entry. | All eight variants preserve surrounding syntax and clear Problems after acceptance. Empty slots insert at the cursor; mid-token edits replace the whole identifier without duplicating its suffix. Parameterized aliases retain their substituted compiler type. No signature appears. Both editor drivers consume the same data; native IntelliJ assertions pass. | -| X97 | In temporary `Editing.x`, run the shared argument-context variants: later/named arguments, groups, qualified properties, function values, construction, operators and missing operands. Accept the specified variable or literal in each row. | Fitting rejects incompatible values, preserves surrounding syntax and exact edits, retains signature help and clears diagnostics after acceptance. Literal rows verify Value kind and sort metadata; method rows verify documentation and active parameters. The 30 shared variants include character, decimal, binary, array, map and tuple insertions; see the latest L64 receipt for validation. | +| X97 | In temporary `Editing.x`, run the shared argument-context variants: later/named arguments, groups, qualified properties, function values, construction, operators and missing operands. Accept the specified variable or literal in each row. | Fitting rejects incompatible values, preserves surrounding syntax and exact edits, retains signature help and clears diagnostics after acceptance. Literal rows verify Value kind and sort metadata; method rows verify documentation and active parameters. The 30 shared variants include character, decimal, binary, array, map and tuple insertions; record the selected completion cases in the acceptance report. | | X98 | Complete `Li|st` and `Li|`, including a nested `Map` argument. | Only the base identifier is replaced; `` and nested delimiters survive. The accepted source compiles. VS Code passes in `run-KoAP6K`; the IntelliJ consumer compiles and awaits its native checkpoint. | | X99 | In an isolated discovered workspace, create `LiveLibrary.x` with `module LiveLibrary { static Int value() = 1; }` and `LiveConsumer.x` with `module LiveConsumer {}`. Add `package lib import LiveLibrary; Int run() = lib.value();` to the consumer without saving. Change the library result to `String`, then discard it. | Definition reaches `value`; consumer Problems updates for the incompatible unsaved dependency and clears on discard. Neither buffer is saved by the server. | | X100 | Create a healthy module with `class Base {}` and `class Child extends Base {}`, plus an independent module. Break the neighbor with `Missing broken;`, then request Base's subtypes. | Child remains navigable. Exact references return no complete answer while the graph is broken. Repairing the neighbor restores full graph queries. | @@ -2236,7 +2235,7 @@ module Advanced { | X105 | Use unresolved `Document` in a type header; apply the import quick fix for `xml.xtclang.org`. Then try unresolved `Widget` declared in a separate discovered source module. | Only imports whose complete graph compiles are offered; applying clears diagnostics. Source imports also add the discovered dependency edge. Explicit graphs do not silently gain dependencies. | | X106 | In Editing.x, complete `Str` inside `function Str(Int)`, `function void(Str)`, `function (Int, Str)(Int)` and `Function<, >` parameter types. Repeat with a missing function/sequence closer. | Only the written leaf token is replaced. Complete forms compile; missing delimiters remain diagnostics until repaired. No callable signature is invented for a declaration header. The same eight variants are implemented in both drivers. | | X107 | Complete a trailing `ecstasy.text.` type, edit `te` inside `ecstasy.text.StringBuffer`, and fill empty union/intersection/difference operands. | Insert only into an empty slot or replace only the selected qualifier token; preserve the remaining suffix and compile the accepted source. | -| X108 | Complete type names in multiple-return lists, generic method/class headers, package compositions and written `Element`/`Other` formals, including `Chain` recursion. | Preserve exact ranges and restore compilation. Recursive constraints are explicitly labelled written syntax with no invented formal identity. The 17 shared variants include compound/nullable/immutable recursion and a cursor inside its own bound; see the latest L64 receipt for validation. | +| X108 | Complete type names in multiple-return lists, generic method/class headers, package compositions and written `Element`/`Other` formals, including `Chain` recursion. | Preserve exact ranges and restore compilation. Recursive constraints are explicitly labelled written syntax with no invented formal identity. The 17 shared variants include compound/nullable/immutable recursion and a cursor inside its own bound; record the selected completion cases in the acceptance report. | | X109 | In the shared `X109` fixture, public parameter rename updates a closed caller and preserves another overload. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | | X110 | In the shared `X110` fixture, override parameter slots join differently named declarations and callers. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | | X111 | In the shared `X111` fixture, constructor parameter rename starts at a named argument. Keep every other fixture file closed before Rename. | Compare all files with the shared expected contents; preserve unrelated overloads/aliases; verify moved paths, then Undo once and verify every original path and source. | @@ -2276,12 +2275,12 @@ module Advanced { | X142 | Open configured `FoldedInitializer.x`; hover and navigate from `Int copy = value`, find references, rename `value` to `number`, then Undo. | The declaration, folded initializer and method body share one semantic identity; all three rename together, compile cleanly and restore on Undo. | | X143 | Configure `PartialSymbols.x` with 130 classes. Compare ordinary workspace symbols with a request carrying a partial-result token. | Ordered progress batches contain at most 64 symbols each; their combined names match the ordinary result exactly and the final response is empty. | | X144 | Apply a current versioned text edit through the installed client, Undo it, then send a two-document edit with one stale version. | Current edit and native Undo succeed. A stale target refuses the whole batch and preserves both current buffers. Drivers invoke the installed application handler; packaged tests separately exercise server-to-client transport. | -| X145 | Replace ProgressWork with the shared workload and request references. Verify the progress source/activity text, cancel, then check hover; repeat and restart while pending. | Cancellation terminates only the request; progress disappears. Restart preserves unsaved text, retires the pending reader and exits the old PID. Visible VS Code Cancel-control checks use 20,000 methods; automatic IntelliJ Cancel, the ordinary VS Code callback and restart use 5,000. A request finishing too early fails as unexercised. Historical model-cancellation receipts and current visible-control acceptance are distinguished in the L81 section below. | +| X145 | Replace ProgressWork with the shared workload and request references. Verify the progress source/activity text, cancel, then check hover; repeat and restart while pending. | Cancellation terminates only the request; progress disappears. Restart preserves unsaved text, retires the pending reader and exits the old PID. Visible VS Code Cancel-control checks use 20,000 methods; automatic IntelliJ Cancel, the ordinary VS Code callback and restart use 5,000. A request finishing too early fails as unexercised. Historical model-cancellation receipts and current visible-control acceptance are distinguished in the progress and cancellation section below. | | X146 | Open RefreshConsumer with inferred `var value = lib.make()`. Change only RefreshLibrary from returning Int to String, then restore it. In IntelliJ, also keep at least 16 additional documents connected and issue a burst of lens/hint/token refreshes before the dependency changes. | Both clients receive provider refreshes and show the changed inferred-type hint. Consumer text/version stays unchanged. IntelliJ reads the native cached inlay result and must report no read-action overload; its driver restores the temporary tab limit. VS Code observes the registered provider's refresh events and result. | | X147 | Hold an older settings report while requesting a newer one, changing settings, restarting or closing the settings page. Complete the old reply last. | Older reports cannot overwrite current or disposed UI state. VS Code delays a real server reply; IntelliJ drives the real settings component with controlled asynchronous report data and an EDT completion barrier. Restart retires the old PID and preserves source. | | X148 | Open the shared Extract.x fixture, select exactly `42` in the return statement, and choose Extract literal to local variable. Undo, Redo, then Undo. Repeat with a string/character literal; try selecting a call or only part of a literal. | The action inserts `val extractedValue = 42;` immediately before the return, which uses that local. Existing identifiers force a fresh suffix. Exact text, indentation and diagnostics survive Undo/Redo. Unsupported selections offer no extraction. Selected X148 acceptance passes in both editors. | | X149 | Open the shared Naming.x variants. Complete `str` in the middle of `strange` to `string`; repeat in a parameter, then complete `st` with another `string` already present. Undo each acceptance. | The whole declaration token is replaced. The collision variant proposes `string1`. No reference or rename is claimed. Both drivers pass exact source, diagnostics and one Undo (see the per-case acceptance reports). | -| X150 | Open the shared Templates.x variants. Complete `cl` with `class declaration`, then `if`, `for`, `do` and `try` with their block templates, and `interface` with its declaration template. Inspect the selected name/condition, press Tab to reach the body, and Undo. Also try `mo` in an empty file and completion inside a string, comment, member access and argument. | Templates appear only in the corresponding file/member/statement context. Placeholder navigation leaves exact indentation and source unchanged; Undo restores the prefix. IntelliJ may first restore the placeholder selection without changing text, then undo the insertion. Comments, strings and value/type positions receive no syntax templates. Clients without snippet support receive plain text without placeholder syntax. Both editor cases pass; syntax exclusions and minimal-client behavior have backend/protocol coverage. IntelliJ uses the documented UP18 indentation constraint. | +| X150 | Open the shared Templates.x variants. Complete `cl` with `class declaration`, then `if`, `for`, `do` and `try` with their block templates, and `interface` with its declaration template. Inspect the selected name/condition, press Tab to reach the body, and Undo. Also try `mo` in an empty file and completion inside a string, comment, member access and argument. | Templates appear only in the corresponding file/member/statement context. Placeholder navigation leaves exact indentation and source unchanged; Undo restores the prefix. IntelliJ may first restore the placeholder selection without changing text, then undo the insertion. Comments, strings and value/type positions receive no syntax templates. Clients without snippet support receive plain text without placeholder syntax. Both editor cases pass; syntax exclusions and minimal-client behavior have backend/protocol coverage. IntelliJ uses the documented LSP4IJ snippet-indentation constraint. | | X151 | Open the shared MissingNames.x variants. Invoke completion after the space following the property or parameter type, before the initializer or closing parenthesis. Accept `string1`, `string`, `list`, `stringArray`, `fn` and `value` across named, nullable, array, function and compound types, then Undo each insertion. Also try an empty name at EOF, an already written name, a comment and a bare local expression. | Completion inserts only the suggested name at the empty cursor range, preserving the written type, whitespace and remaining source. Collision checks include existing document identifiers. Accepted variants compile; one Undo restores the incomplete source. Existing names, comments and ambiguous expressions acquire no empty declaration-name slot. X151 passes in both editors; EOF, UTF-16/CRLF and refusal controls pass in parser/backend/protocol tests (see the per-case acceptance reports). | | X152 | Open the shared SemanticValues.x variants. Complete `thi` to `this.Owner`, then `this.Ow` to `this.Owner` inside a nested class call. Complete an empty indexed callback argument with `0`. Undo each acceptance. | Only an enclosing instance that fits the actual call is offered. Qualified insertion replaces only the selected member. Indexed functions retain their function parameter type. Every accepted document compiles and Undo restores the exact incomplete source. Static-boundary and incompatible-type refusals have backend coverage. | @@ -2542,7 +2541,7 @@ sections instead of silently counting provider responses as complete UI acceptan | Diagnostics and Problems | X1/X2/X23/X27/X46–X52/X123, 7a.8/7a.9 | Inspect error/warning presentation, source code and related-location navigation in Problems; verify closed/deleted URIs, duplicate-free warnings, configuration failures without modal loops, and refresh without changing buffer versions. Long editing/idle/restart runs are distinct from one successful correction. | | Completion and signature UI | X6–X20/X70–X98/X106–X108/X125 | Keyboard and mouse acceptance, Escape without edits, overload selection/active parameter after retrigger, IME/non-ASCII identifiers and end-of-file/no-final-newline boundaries. Existing UTF-16/CRLF and syntax-recovery variants remain mandatory; untested forms are not implied by a fitting result. | | Navigation, symbols and hierarchy | X3–X5/X21–X22/X28/X33–X44/X59/X63–X69/X99–X101/X131 | Native workspace-symbol search/navigation for closed roots, multiple declaration targets, back navigation, ambiguous/missing source attachments, inherited generic/compound edges and stale closed-file handles. Do not infer a complete conditional-mixin hierarchy or dynamic call graph. | -| Highlights, folds and selection | X1/X4/X41/X92 and L56 startup test | Exact nested selection expansion/shrink, empty/recovered document fallback, folding boundaries after closing/reopening or deleting text, and theme/read/write visual distinctions. Startup race assertions are a separate harness test, not the normal readiness wait. | +| Highlights, folds and selection | X1/X4/X41/X92 and the startup-editing test | Exact nested selection expansion/shrink, empty/recovered document fallback, folding boundaries after closing/reopening or deleting text, and theme/read/write visual distinctions. Startup race assertions are a separate harness test, not the normal readiness wait. | | Inlay hints | X42/X131 | Enable/disable through native controls, inspect tooltip rendering for locals/parameters/lambdas, verify no misleading names for unnamed function calls, named arguments or omitted defaults. A protocol tooltip does not prove the UI renders it. | | Formatting | X31/X132; backend token-preservation tests; sections 13–13c | Apply full/range/on-type edits through native actions; verify literal and multiline-string/comment bytes, CRLF, tabs, final-newline policy, untouched lines and idempotence. X138 verifies live indent settings; X249/X250 verify continuation/comment margins and bounded width wrapping with native history. Backend regressions cover custom indentation and tabs. Not every variant is a shared-driver scenario. | | Document links | X131 plus lexical unit tests; section 15 | Click HTTP(S) URLs in comments/strings; verify exact ranges and absence of invented import/file/unsupported-scheme links. Malformed/partial literals must not create arbitrary targets. External browser launching is a manual action, separate from resolving a URI. | @@ -2551,7 +2550,7 @@ sections instead of silently counting provider responses as complete UI acceptan | Rename, actions and resource edits | X53–X63/X102–X105/X109–X122/X127–X130 | Collision/capture refusals, cancellation, stale documents, graph persistence, closed consumers, one Undo/Redo and source text plus filesystem bytes; see the batch matrix. Explorer drag/drop and arbitrary third-party edit application are separate host paths. | | Discovery, paths and build settings | CFG1–CFG3/X99/X100/X124/X129/X134 | Multiple roots, Unicode/space-containing paths, symlinks, project close/reopen and external shared watch leases. Gradle-import failure, missing generated resources and no-Gradle projects must retain usable last-valid inputs and show actionable status. | | Generic editor integration | Separate sections 1, 16, 17 and VS Code V-cases | File association, TextMate fallback, comment/uncomment, templates/snippets, keybindings, plugin reload and Community-only startup. Compiler shared scenarios do not replace these checks or an explicit Tree-sitter smoke run. | -| Reliability and configuration | X29/X30/X51/X57 plus process/retention suites | Cross-platform/remote filesystems, large-workspace latency/heap bounds, prolonged run/stop/crash cycles, cancellation under queue pressure, and UI1–UI7 settings acceptance remain explicit L82 gates. | +| Reliability and configuration | X29/X30/X51/X57 plus process/retention suites | Cross-platform/remote filesystems, large-workspace latency/heap bounds, prolonged run/stop/crash cycles, cancellation under queue pressure, and UI1–UI7 settings acceptance remain separate responsiveness and lifecycle acceptance requirements. | A catalog entry's `coverage: full` means the driver implements that **case's stated assertions**; it does not mean the entire feature family is exhaustive. Manual checks should record pass/fail/ @@ -2602,13 +2601,13 @@ Still to come: - Binary source attachment, conditional-mixin hierarchy and broader implementation targets - Wider member/workspace rename: unsupported composition routes and automatic external-consumer enumeration. X119–X121 add primary-constructor properties, lambda parameters and method-value - escapes. The L62 extension also covers public/explicit-constructor parameters, supported + escapes. The rename checks also cover public/explicit-constructor parameters, supported composition families, qualified modules and implicit package directories. Both clients implement graph persistence through native Rename. Shared X109–X118 pass in selected runs in both editors. VS Code edited-file moves with refactoring auto-save disabled remain refused. Consumers outside the graph remain unknown - Diagnostic-driven quick fixes and refactorings -L61 declaration acceptance: X4 now also requests Go to Declaration for the shadowed local and +Declaration-navigation acceptance: X4 now also requests Go to Declaration for the shadowed local and property and checks that their distinct declaration targets match definition. VS Code uses its declaration provider; IntelliJ checks the request through the installed client transport beside the existing native navigation actions. Backend/protocol cases cover multiple inherited contracts, @@ -2682,9 +2681,9 @@ preferences. Check invalid JSON retains a running service, native Reset/Cancel n and restart never saves a dirty source. Remote filesystems, alternate keymaps and restricted workspace build execution remain explicit boundaries, not assertions covered by these local runs. -Protocol hardening acceptance (L80/L81): X136/X137/X140/X141 pass in VS Code `run-5eCFZV` and +Protocol and lifecycle acceptance: X136/X137/X140/X141 pass in VS Code `run-5eCFZV` and IntelliJ `run-15914309414363009017` (also START, zero IDE errors). X140 checks a method-body reference -after an emoji; the subsequent L83 implementation is exercised separately by X142 below. +after an emoji; constant-folded initializer navigation is exercised separately by X142 below. For a large configured project, request workspace diagnostics or references and observe the work-done progress notification. Cancel the request: its progress must end, while another reader and document analysis remain usable. Check no stuck progress after restart/close. @@ -2696,7 +2695,7 @@ features exercised by changing the modern IDE's capability declaration. ### Protocol and lifecycle coverage map -This map covers the L80/L81 protocol batch and the accompanying state-ownership fixes. A shared +This map covers protocol negotiation, lifecycle and state ownership. A shared scenario is automated in both editors only where named below. Controlled backend tests exercise interleavings that normal editor actions cannot reliably force. Manual rows remain acceptance work; they are not passing automated receipts. The current catalog has 152 cases. The earlier @@ -2755,7 +2754,7 @@ Pending manual acceptance in both editors: The disk-index/open-buffer, long-lived path-picker and stalled watcher-registration fixes and their deterministic regressions are explicit in the [acceptance reports](#acceptance-reports). -Partial-result streaming and L83 initializer facts are implemented; their acceptance and remaining +Partial-result streaming and constant-initializer facts are implemented; their acceptance and remaining limits are recorded below. Generic IntelliJ server-initiated text-edit checking is implemented; X144 passes both editors. The catalog now contains 152 cases; the receipts below retain their original scope. @@ -2863,7 +2862,7 @@ X130 alone passes in `run-QLBWYV`; it is not evidence of a host fix. The [acceptance reports](#acceptance-reports) records the failed focus attempt, correction, upstream source and remaining repair task. -### Final L80 protocol checks (2026-10-01) +### Protocol capability checks The current response/provider capability audit is complete; see the [acceptance reports](#acceptance-reports). @@ -2880,11 +2879,11 @@ identity when related information is suppressed. The tests include supported and For the next normal editor acceptance run, retain the existing signature-help cases, X123 diagnostics and X131 resolve checks in both drivers. These exercise the installed clients' negotiated behavior; -they cannot substitute for the reduced-client protocol tests. L81 manual checks and the X130 host +they cannot substitute for the reduced-client protocol tests. Manual progress and cancellation checks and the X130 host failure remain open. Exact test receipts are in the [acceptance reports](#acceptance-reports). -### L81 detailed progress and upstream acceptance (2026-10-01) +### Progress, cancellation and client compatibility X145 now checks that reference progress names its source. The editor supplies the Ecstasy service/source name once; the operation title is simply `Finding references`, without another @@ -2925,11 +2924,11 @@ retains failed attempts separately from passing refresh/restart cases. IntelliJ VS Code `run-VeotM0` passes its automated visible-control check in 17.6 seconds, plus X146/X147/X259. All upstream compatibility issues are collected in [client compatibility limits](#client-compatibility-limits). -Source `TODO LSP4IJ:` / `TODO VSCODE:` markers carry matching UP IDs and removal conditions. -UP15's malformed-parameter classification and UP16's X130 host repaint remain defects; a passing +Source `TODO LSP4IJ:` / `TODO VSCODE:` markers carry the affected behavior and removal conditions. +Malformed-parameter classification and the X130 host repaint remain defects; a passing reader-recovery or Move/Undo/Redo check is not evidence that those defects are repaired. -### Bounded L82 regression and memory checkpoint +### Bounded regression and memory workload Run the combined compiler/LSP/packaged-server/plugin checks before the full editor catalogs. Confirm XML counts and record existing disabled/opt-in cases separately; a cached task or missing @@ -2942,7 +2941,7 @@ For a repeatable real-project memory run, with the compiler server already built python3 lang/scripts/compiler-workload.py \ --workspace ../platform \ --jar lang/lsp-server/build/libs/lsp-server-0.4.4-SNAPSHOT-all.jar \ - --output lang/lsp-server/build/reports/platform-workload/l82-checkpoint \ + --output lang/lsp-server/build/reports/platform-workload/memory-checkpoint \ --cycles 400 --restarts 2 --sample-rss --gc-every 100 --heap-histograms ``` @@ -2991,7 +2990,7 @@ restart while a report is pending, extract with Undo/Redo, and refresh an untouc dependency edits. Keep the usual deadlines. The generated workload supplies the many-buffer scale evidence; a three-case native selection does not reproduce the full catalog's retained workload. -### Large-file IntelliJ diagnostic (L82 / UP17) +### Large-file IntelliJ responsiveness diagnostic The optional `CompilerPlaybookTest.largeFileEditing` probe reproduces the recorded UI freeze. It is disabled by default, including in ordinary full playbook runs. The @@ -3005,7 +3004,7 @@ For a manual check, open its generated `parity/LARGE_FILE/LargeFile.x` and repla semantic colors before replacing: the compiler replying does not establish that the IDE applied its highlights. Check typing, repaint and navigation responsiveness, then Undo and verify the source and highlighting recover. Record UI and server times separately. A freeze or missing -decoration fails this acceptance; a passing bounded X145 does not override it. UP17 remains open. +decoration fails this acceptance; a passing bounded X145 does not override it. The large-file responsiveness issue remains open. Startup notification presentation: the bold service title appears once. Beneath it, Version, Adapter and PID share one compact line with dimmed, theme-aware labels and normal value text. @@ -3021,7 +3020,7 @@ one-line versus whole-file hints, tokens, hover, queue/API phases and transport project-query compilation distinct from warmed responses. This does not replace the decorated IntelliJ probe, native tooltip/theme checks or a prolonged editing/retention workload. -### Native project lifetime checks (L81) +### Native project lifetime checks These optional checks open disposable native windows and exercise real close/reopen behavior: @@ -3073,27 +3072,27 @@ The whole identifier must be replaced once and Both drivers use the same completion variants and exact expected resulting documents; their earlier X105 passes establish only quick fixes. -L65 manual-module check (backend regression coverage passes; these manual actions remain available): in +Implementation-navigation manual check (backend regression coverage passes; these manual actions remain available): in `manualTests/src/main/x/jit/jit_tests/basic/mixinTests.x`, use Go to Implementation on `t5.Root.self`; verify the written `Base.self` body and no generated redirect location. In `delegationTests.x`, `ReportableAsString.showText` reaches `ReportableString.showText`; the interface-valued delegate field does not invent another executable source body. -L62 manual-module check (backend regression coverage passes): rename `t5.Root.self` to `copySelf` +Compiler-validated rename manual check (backend regression coverage passes): rename `t5.Root.self` to `copySelf` in mixinTests; Root, Base, Mix and the test5 call change together, while t6 stays unchanged. Undo restores exact text. In condMixinTests rename the first `MixS.size` to `width`; only its corresponding test1 call changes, not the independent t2 composition. A rename colliding with another default method must be refused. Go to Definition on Element in a conditional-incorporation clause reaches the corresponding mixin formal. -L64 closure additions (execution pending): X150 now includes a function argument lambda. Accept +Lambda and inferred-name completion checks: X150 now includes a function argument lambda. Accept `(arg1) -> TODO()`, verify that TODO() is selected, Tab reaches the end of the expression and Undo restores the empty argument. This is a scaffold; replace TODO() before executing the program. X151 adds unnamed val/var locals with string, integer and constructor initializers; accept the syntax-derived name, verify the following source and Undo. X152 adds ordinary return expressions with `thi` and `this.Ow`; verify the real enclosing instance, exact replacement and Undo. -L65 closure additions (execution pending): +Dispatch navigation and semantic highlighting checks: | ID | Manual actions | Expected result | | --- | --- | --- | @@ -3115,7 +3114,7 @@ L65 closure additions (execution pending): | X162 | Configure App at `old/App.x` with resource roots `old/assets`, then `fallback`; keep Consumer closed and its resources disabled (`[]`). Move `old` into `target` through native Move. Inspect compiler settings, reopen both modules, Undo once and Redo once. Then add an unresolved name in App.x without saving and repair it. | Source and resource paths move together; resource precedence, dependency names and `[]` are retained. One Undo/Redo restores/reapplies files and settings, with clean diagnostics. Subsequent typing produces and clears diagnostics at the current path without duplicate document connections. | | X163 | Configure App at `old/App.x` without resource overrides and keep Consumer closed. Move just `App.x` into `target`. Inspect its companion directory, `old/data.txt` and compiler settings, then Undo/Redo. Add and repair an unresolved name without saving. | The companion follows the source. Default resources left behind become an explicit `old` root; the resource bytes remain available. Undo restores the absent override; Redo restores the pinned root. Continued editing reaches the moved source and diagnostics clear after repair. | -L67 index replacement check: with an unopened consumer, navigate a member from a host-indexed +Dependency-index replacement check: with an unopened consumer, navigate a member from a host-indexed binary. Replace its source index with another source location, remove the index while retaining the binary, then configure editable source for that module. Navigation must follow the replacement, be absent without an index, and prefer the configured source. Previously prepared hierarchy @@ -3141,7 +3140,7 @@ refused participation reply can veto a host move. X166–X168 have shared fixtures and implementations in both drivers. All three pass alongside X164/X165 in the latest selected runs recorded at the top of this file. Exact receipts, the shared -catalog hash, proof boundaries and the failed/repaired IntelliJ UP20 evidence are in the +catalog hash, proof boundaries and the failed and repaired IntelliJ directory-move synchronization evidence are in the [acceptance reports](#acceptance-reports). For manual focus coverage, switch away while Rename is opening, return to its dialog and submit once; inspect the exact edits and Undo once. The native harness now observes focus while the @@ -3163,7 +3162,7 @@ a pinned resource root is conservatively refused. Undo restores the stored, prov its original resource configuration. Before applying refactorings, save any pre-existing unsaved closed IntelliJ document; the host refuses buffers that were not available to the compiler proof. -### L62 empty destinations and qualified-name trivia +### Empty move destinations and qualified-name trivia Shared X173–X176 pass in both drivers in the selected runs recorded at the top of this file. The setup creates the listed empty directories explicitly, without a Marker.x source. @@ -3175,7 +3174,7 @@ The setup creates the listed empty directories explicitly, without a Marker.x so | X175 | Request the shared batch into empty util with an inline Taken collision. | No proposal, edits, file changes or settings changes. This is a protocol refusal; it cannot veto arbitrary VS Code Explorer moves. | | X176 | Move First.x and Second.x together into empty util. Undo and Redo. | Mutual references and the closed consumer update as one transaction; diagnostics remain clean. | -### L63 local initializer and unused-local actions +### Local initializer and unused-local actions Shared X177–X180 pass in both drivers in the selected runs recorded at the top of this file. Use the exact source in `lang/test-fixtures/compiler-playbook/scenarios.json` for each row. @@ -3191,7 +3190,7 @@ Inferred locals, changed expected types, partial/deferred expressions, interveni Ref/Var annotations and runtime initializers remain refusal controls in backend tests. General extract-method, missing-declaration generation and global safe delete remain separate work. -### L63 private helper extraction +### Private helper extraction X181–X184 pass in both drivers in the selected runs recorded at the top of this file. @@ -3208,7 +3207,7 @@ broken neighbors. Swapped helper arguments and changed moved-call targets must f when both programs compile. Multi-statement extraction and mutable/by-reference captures remain outside this slice. -### Missing-method quick fixes (L63, X185–X188) +### Missing-method quick fixes (X185–X188) Use compiler mode. The call must initially report an error. Place the caret on `missing` and open Quick Fix / intentions (Alt+Enter in IntelliJ; Quick Fix in VS Code). Generated methods are @@ -3233,9 +3232,9 @@ No diagnostic text or numeric error code is used to invent a signature. Run **X181 followed by X185** together when validating IntelliJ: an unchanged error after a source-graph refresh must still offer a working quick fix. Standalone X185 passing did not catch -the UP07 stale-annotation issue. Final acceptance passes all six selected cases in both editors. +the stale-diagnostic-annotation issue. Final acceptance passes all six selected cases in both editors. -### Local arguments and typed initializer quick fixes (L63, X189–X192) +### Local arguments and typed initializer quick fixes (X189–X192) Use the exact fixtures in the shared catalog. Place a caret on `missing`, invoke Quick Fix (IntelliJ Option/Alt+Enter; VS Code Quick Fix), and select **Create private method 'missing'**. @@ -3249,11 +3248,11 @@ Use the exact fixtures in the shared catalog. Place a caret on `missing`, invoke For X189–X191, compare the complete expected source, require diagnostics to clear, Undo to restore the error/source, Redo to clear them and Undo once more. X192 checks refusal through the installed -connection in both editors. The selected continuation also runs X181 → X185, retaining the UP07 +connection in both editors. The selected continuation also runs X181 → X185, retaining the stale-annotation quick-fix-refresh regression. These cases use the existing shared action drivers; they add no editor-specific production bridge. -### Qualified missing-method quick fixes (L63, X193–X196) +### Qualified missing-method quick fixes (X193–X196) Use the exact shared sources in compiler mode. Place the caret on the leaf `missing` token, then invoke Quick Fix (IntelliJ Option/Alt+Enter; VS Code Quick Fix). @@ -3270,7 +3269,7 @@ restore source/error, Redo to clear them and Undo again. Refusals use the instal in both editors. The backend additionally rejects a repair that still compiles after changing `peer` to `this`, and verifies that selecting only the receiver does not offer member creation. -### Class-qualified missing-method quick fixes (L63, X197–X200) +### Class-qualified missing-method quick fixes (X197–X200) Use the exact shared sources in compiler mode, with the caret on the leaf `missing` token. Open Quick Fix / intentions and select **Create private method 'missing'** where offered. @@ -3290,7 +3289,7 @@ other class owners, existing static overloads, explicit generic/singleton qualif The parameter type in X200 is qualified deliberately: `Box Box` shadows its own type annotation and reports a separate dynamic-type diagnostic before the missing call can be analyzed. -### Cross-owner missing-method quick fixes (L63, X201–X204) +### Cross-owner missing-method quick fixes (X201–X204) Use the exact shared sources in compiler mode. Open only the caller initially, put the caret on `missing` and inspect Quick Fix / intentions. The public action explicitly names the destination; @@ -3310,7 +3309,7 @@ binary destinations, another configured module, generic/const classes, existing neighbors, supported signature type shapes, and destination document versions (null when closed, its own version when open). The next continuation adds bounded local arguments and typed initializers. -### Cross-owner local types and initializer contexts (L63, X205–X208) +### Cross-owner local types and initializer contexts (X205–X208) Use the shared source for each case in compiler mode. The public action states both visibility and destination; `TODO()` still needs an implementation. These cases reuse the same native @@ -3327,7 +3326,7 @@ Backend controls additionally cover typed/var/val locals, separately assigned lo class qualifiers, zero/mixed arguments, source and compound types, invalid/out-of-scope locals, and compilable signature substitutions or argument rebinding that the proof must reject. -### Cross-module missing-method destinations (L63, X209–X212) +### Cross-module missing-method destinations (X209–X212) Use compiler mode and the complete shared fixture set for each case. Keep the destination closed until applying the Quick Fix on `missing`. Successful actions must edit only the named destination; @@ -3345,7 +3344,7 @@ Backend controls cover explicit dependency graphs, unrelated roots, binary artif source indexes, read-only destinations, unsupported owners, existing members, closed broken consumers, missing destination imports, exact signature identity and destination-specific document versions. -### Missing-method destination imports (L63, X213–X215) +### Missing-method destination imports (X213–X215) Load every shared fixture and use the explicit `sourceModules` graph recorded for each case. The source import alone does not establish the destination's dependency permission. Keep the @@ -3363,9 +3362,9 @@ Backend controls include repeated/compound types, multiple imports with collidin existing alias reuse, unused dependency exclusion, CRLF preservation, open/closed document versions, and rejection of compilable signature substitutions after import insertions shift the method. -### L62 ownership and relocation closure checks +### Ownership and relocation checks -X216–X220 are implemented in both drivers; selected acceptance and the UP23 exception are below. +X216–X220 are implemented in both drivers; selected acceptance and the VS Code overlapping-resource Undo exception are below. Use the shared fixtures for exact source, destination and graph settings. | Case | Manual action | Required result | @@ -3382,7 +3381,7 @@ file operations. Parent-first application can partially fail; child-first applic also fails because VS Code retains the inverse move order. X218 remains a failing VS Code acceptance case, while IntelliJ passes. See [client compatibility limits](#client-compatibility-limits). -### L63 generic repairs and broader refactorings (X221–X242) +### Generic repairs and broader refactorings (X221–X242) These shared scenarios have passing selected-run receipts in both drivers. The [acceptance reports](#acceptance-reports) @@ -3424,14 +3423,14 @@ The backend repeats that import/type-spelling check with a compiled binary depen Boundaries: runtime Class/Type values without a concrete source owner, invented generic inverse substitutions, static-property initializers, mutable capture/control-flow extraction, argument substitution for method inline, instance-property getters and public API deletion are not inferred. -Private member inline keeps the declaration; safe delete is a separate action. UP23 remains the -recorded VS Code overlapping-resource Undo defect; this text-edit batch does not repair it. +Private member inline keeps the declaration; safe delete is a separate action. The recorded +VS Code overlapping-resource Undo defect remains; these text-edit checks do not repair it. -### L66 structural and editing continuation +### Structural and editing checks X243–X250 have passing selected receipts in both editors, together with X132/X138/X139/X158. The [acceptance reports](#acceptance-reports) -retains the failed attempts and corrections, including UP24 (IntelliJ formatting Redo). The shared +retains the failed attempts and corrections, including the IntelliJ formatting Redo issue. The shared formatting fixtures preserve their existing final newline; newline insertion remains an independent editor setting. Four/eight spaces and a 120-column margin are the default settings. Change IntelliJ Ecstasy Code Style or VS Code `xtc.formatting.*` to verify custom indentation and width; use @@ -3448,7 +3447,7 @@ Ecstasy Code Style or VS Code `xtc.formatting.*` to verify custom indentation an | X249 | Format continuation expressions and standalone block comments, repeat, then Undo/Redo/Undo. | Shared indentation is exact and stable; literal contents and relative comment layout remain intact. | | X250 | Format the shared long concatenation, repeat, then Undo/Redo/Undo. | Wrap at token boundaries at the configured 120-column margin; preserve every literal and native history. | -### L67 graph-index acceptance (2026-10-05) +### Graph-index acceptance | Case | Actions | Expected result | | --- | --- | --- | @@ -3463,7 +3462,7 @@ Selected acceptance passes in VS Code `run-8dtGVL` and IntelliJ `run-33434998256 X45/X59/X63/X143/X251, with zero scenario failures; IntelliJ also passes startup. The catalog now has 256 cases. This does not claim a full-catalog rerun. -### Portable symbol identities — L74 +### Portable symbol identities | Case | Action | Expected result | |------|--------|-----------------| @@ -3475,11 +3474,11 @@ trace for `scheme`, `identifier`, `unique` and `kind`; neither IDE currently pro X31 now requires the compiler moniker provider. Binary-only/source-indexed parity and checkout relocation are backend tests, not claims about native editor widgets. -### Read-only library documents — L75 +### Read-only library documents | Case | Action | Expected result | |------|--------|-----------------| -| X254 | Open the shared LibraryViews fixture and navigate to String and xml.Document. Open each matching source, compare its declaration moniker with the consumer, and request formatting. Attempt an edit in the VS Code virtual editor. | VS Code negotiates revision-owned virtual content and refuses edits; IntelliJ uses protected file views (UP25). Declaration/export and consumer/import share artifact identity. Formatting leaves the source unchanged. | +| X254 | Open the shared LibraryViews fixture and navigate to String and xml.Document. Open each matching source, compare its declaration moniker with the consumer, and request formatting. Attempt an edit in the VS Code virtual editor. | VS Code negotiates revision-owned virtual content and refuses edits; IntelliJ uses protected file views. Declaration/export and consumer/import share artifact identity. Formatting leaves the source unchanged. | Select **X31,X101,X158,X252,X253,X254** to cover existing bundled navigation, portable identities and the new library-view path. X254 runs the same String/xml.Document source fixture in both editors. @@ -3488,7 +3487,7 @@ new library-view path. X254 runs the same String/xml.Document source fixture in 2. In VS Code, confirm the target uses `ecstasy-library:` and displays the matching declaration. Typing must leave the document unchanged and clean. Formatting must return no edits. 3. In IntelliJ, confirm the matching source file opens read-only and formatting leaves it unchanged. - LSP4IJ 0.21.0 lacks virtual content/refresh (UP25); this explicitly tests the supported file fallback. + LSP4IJ 0.21.0 lacks virtual content/refresh; this explicitly tests the supported file fallback. 4. The driver requests `textDocument/moniker` at the library declaration. Its scheme/identifier must match the consumer import; the declaration kind is `export`. Neither IDE has a moniker browser. 5. For supporting clients, `workspace/textDocumentContent` returns exact matching bundled text. @@ -3500,20 +3499,20 @@ opening a library view does not add it to the writable source graph. Externally indexes remain file views because they do not carry a verified source-text snapshot. This does not claim complete semantic editing/navigation inside library bodies. -### Compiler inline completion (L76) +### Compiler inline completion | Case | Action | Expected | |---|---|---| | X255 | Accept and undo a compiler-backed inline name. Use the shared `X255/InlineValues.x` source. | Native ghost text accepts answer; one Undo restores ans. | | X256 | Dismiss ghost text and keep suggestions current while typing. Use the shared `X256/InlineValues.x` source. | Dismissal leaves source unchanged; further typing produces a current suggestion. | -| X257 | Respect ambiguity and the selected completion item. Use the shared `X257/InlineValues.x` source. | Automatic ambiguity yields nothing; explicit alternatives and selection constraints match the source. IntelliJ native trigger/selection limits are UP26. | +| X257 | Respect ambiguity and the selected completion item. Use the shared `X257/InlineValues.x` source. | Automatic ambiguity yields nothing; explicit alternatives and selection constraints match the source. IntelliJ cannot distinguish an explicit inline-completion trigger or transmit the selected popup item. | | X258 | Complete a type-compatible value in an unfinished call. Use the shared `X258/InlineValues.x` source. | Only the Int-compatible answer is suggested; missing delimiters remain untouched. | | X259 | Keep RefreshConsumer open, including its bundled recursive json.Doc method signature. Disable inlay hints, change RefreshLibrary.make from Int to String, re-enable hints and restart. Restore the dependency while toggling hints again. | Visible consumer hints disappear when disabled, then show String and Int in order. Recursive signatures do not crash workspace analysis. The consumer text/version stays unchanged and the old server exits. X146 separately verifies all five refresh families; X147 covers late report publication. | Ghost suggestions are plain compiler names/values. No imports or placeholder bodies are inserted. VS Code uses its Inline Suggest commands (Tab to accept, Escape to dismiss); IntelliJ uses Insert Inline Completion and Escape. Explicit ambiguous suggestions are a VS Code native check; -LSP4IJ currently sends Automatic even for a direct invocation (UP26). +LSP4IJ currently sends Automatic even for a direct invocation. ### Compiler build import lifecycle (UI5–UI7) @@ -3578,7 +3577,7 @@ Selected acceptance (2026-10-06): VS Code `run-uZLiO0` passes X129/X254/X260/X26 `run-Fpp8xO` passes X267; corrected `run-HTMdex` passes X266/X268. The initial runs exposed URI hostname normalization and picker/URI-spelling assumptions, all fixed and retained as failed receipts. IntelliJ `run-5914234873745700022` passes START/X129/X260/X263/X266/X267 and keeps -X254 partial for UP25; `run-5018889098298757849` passes START/X268 after creating its source-row +X254 partial because IntelliJ uses file-backed library views; `run-5018889098298757849` passes START/X268 after creating its source-row fixture explicitly. Both IntelliJ runs record zero IDE errors. The backend selection passes 33 tests, IntelliJ units 114, and VS Code extension tests 41, without failures or skips. @@ -3698,10 +3697,10 @@ visual comparisons; normal native runs leave it alone. All four focused cases pass in both editors; final X277 grammar/recovery checks and the six VS Code startup comparisons are recorded in the [acceptance reports](#acceptance-reports). -### Full catalog and UP12 follow-up (2026-10-07) +### Recorded full-catalog run and diagnostic recovery (2026-10-07) -VS Code's full attempt and continuations cover the same 284 cases: 281 pass, X130/UP16 and -X218/UP23 retain their host failures, and experimental X273 fails to display native color swatches. +VS Code's full attempt and continuations cover the same 284 cases: 281 pass, X130 and +X218 retain their host failures, and experimental X273 fails to display native color swatches. Its first native color request receives `ContentModified`; a later explicit provider query returns the expected colors, but the 30-second rendered check still fails. A fresh selected launch reproduces that failure. X274/X275, theme-based highlighting X276/X277, documentation X278 and reference lenses @@ -3710,5 +3709,5 @@ toggles rendering merely to obtain a pass. The color prototype remains off by de See the [acceptance reports](#acceptance-reports) for every receipt, the X31/X127 harness corrections and the outstanding prototype refresh investigation. -This is combined VS Code coverage, not an uninterrupted green run. UP17's opt-in large-file test, +This is combined VS Code coverage, not an uninterrupted green run. The opt-in large-file responsiveness test, prolonged workloads and cross-platform/remote-host acceptance remain separate. diff --git a/lang/intellij-plugin/README.md b/lang/intellij-plugin/README.md index c0c1740aa9a..8b0bdf87d41 100644 --- a/lang/intellij-plugin/README.md +++ b/lang/intellij-plugin/README.md @@ -670,14 +670,14 @@ Apache License 2.0 - See [LICENSE](../../LICENSE) for details. The native file-tree Move action now requests compiler proof before moving selected source files or module containers. Shared X130 passes native batch Move and project Undo/Redo. Labels use Ecstasy; implementation names retain Xtc. Upstream compatibility bridges carry searchable -`// TODO LSP4IJ:` comments with their removal conditions and stable UP identifiers in the +`// TODO LSP4IJ:` comments with their removal conditions and their affected behavior in the [client compatibility limits](../doc/manual-test-plan.md#client-compatibility-limits). Lazy action selection now uses a registered client command to resolve and apply the edit in a version-checked undo command; selected native X105/X122/X127 acceptance passes. X131 adds protocol assertions for code-lens/link/inlay/symbol resolution through the installed client connection. -L72 adds shared X132 for multiple-range formatting and negotiated save hooks; selected native +Shared X132 covers for multiple-range formatting and negotiated save hooks; selected native acceptance and the backend/packaged transport regressions pass. The server defaults to full synchronization and no save-time edits; custom LSP hosts can set `initializationOptions.xtcDocumentSync` with independent `incremental` and `formatOnSave` booleans. @@ -718,7 +718,7 @@ applied libraries** rereads saved paths without a restart. The source-module tab **Order resource directories…** with native path selection and automatic-resource inheritance. Attachments supply navigation-only snapshots; they do not add compiler source modules. IntelliJ -opens protected fallback files until LSP4IJ supports our virtual library editor (UP25). Use matching +opens protected fallback files until LSP4IJ supports our virtual library editor. Use matching sources; available debug text is checked but is not a complete binary/source equivalence proof. Project settings survive server restart; invalid replacements retain accepted compiler inputs. Shared X266–X268 and their [acceptance reports](../doc/manual-test-plan.md#acceptance-reports) @@ -755,11 +755,11 @@ stale replies cannot replace a newer report. ### Experimental color-value picker -The opt-in L77 prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared +The opt-in color-value prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared fixture. It supplies native color swatches and picker edits separately from normal semantic highlighting. It is disabled for ordinary launches and does not introduce a public XDK color API. Shared X273–X275 exercise picker edits, dismissal, native Undo/Redo and diagnostic recovery. -See the [prototype recipe and acceptance](../doc/manual-test-plan.md#l77-color-value-prototype). +See the [prototype recipe and acceptance](../doc/manual-test-plan.md#experimental-color-value-picker). ### Compiler documentation and reference counts diff --git a/lang/intellij-plugin/TESTING.md b/lang/intellij-plugin/TESTING.md index 7238d37c2a8..9d9486cd473 100644 --- a/lang/intellij-plugin/TESTING.md +++ b/lang/intellij-plugin/TESTING.md @@ -748,8 +748,7 @@ released dependency. Standalone formatting Undo/Redo and reconnection of closed are separate requirements. See the [client limits](../doc/manual-test-plan.md#client-compatibility-limits). Readiness polling accepts a successful observation once, using a monotonic deadline. Pinned -Driver `waitFor` rechecks success and can falsely report a timeout during connection replacement -(UP29). `UiWaitsTest` covers this race, actual expiry, pending reads, cancellation and null values; +Driver `waitFor` rechecks success and can falsely report a timeout during connection replacement. `UiWaitsTest` covers this race, actual expiry, pending reads, cancellation and null values; `PlaybookFailureTest` retains fatal timeout/cancellation behavior. These tests run without opening an IDE through `testCompilerPlaybook --tests '*UiWaitsTest' --tests '*PlaybookFailureTest'`. The wrapper only retries observations; edit/refactoring actions remain outside polling loops. @@ -860,7 +859,7 @@ All testing and development tasks for the IntelliJ plugin, runnable from the pro - [Integration Tests: API Interaction (Mar 2025)](https://blog.jetbrains.com/platform/2025/03/integration-tests-for-plugin-developers-api-interaction/) - [IntelliJ Platform SDK — IDE Development Instance](https://plugins.jetbrains.com/docs/intellij/ide-development-instance.html) -### L82 refresh and VFS regression selection +### Refresh and VFS regression selection X146 additionally keeps 16 extra documents connected and schedules 64 lens, hint and semantic-token refreshes each while a write action delays their read passes. The test temporarily raises the sandbox @@ -888,7 +887,7 @@ neither editor bundles test binaries in source control. Use `-PintellijPlaybookCases=X266,X267,X268` for this selection. The new URI round-trip regression keeps case-sensitive module names in the virtual URI path because hosts normalize URI authorities. See the [acceptance reports](../doc/manual-test-plan.md#acceptance-reports) -for native results; a successful fallback-file check does not close UP25's virtual-editor gap. +for native results; a successful fallback-file check does not close LSP4IJ's virtual-library-editor gap. ### Machine-local JVM tuning and log support diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt index 578d56b1871..5d264a71b1e 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientDiagnostics.kt @@ -30,6 +30,7 @@ fun Driver.receivedDiagnostics(editor: JEditorUiComponent): List, ) +/** Exposes the project's started LSP4IJ servers through the IDE driver. */ @Remote("com.redhat.devtools.lsp4ij.LanguageServiceAccessor", plugin = "com.redhat.devtools.lsp4ij") interface LanguageClients { // Driver transports collection results as lists even when the IDE method returns a Set. fun getStartedServers(): Collection } +/** Gives the driver access to a running client's documents, lifecycle and protocol trace. */ @Remote("com.redhat.devtools.lsp4ij.LanguageServerWrapper", plugin = "com.redhat.devtools.lsp4ij") interface StartedLanguageServer { fun getOpenedDocuments(): Collection @@ -67,11 +70,13 @@ interface StartedLanguageServer { fun getServerTrace(): ClientTraceLevel } +/** Reads the client's lifecycle state without loading its enum into the test process. */ @Remote("com.redhat.devtools.lsp4ij.ServerStatus", plugin = "com.redhat.devtools.lsp4ij") interface ClientServerStatus { fun name(): String } +/** Exposes one recorded protocol message from the IDE-side LSP trace. */ @Remote( "com.redhat.devtools.lsp4ij.LanguageServerWrapper\$LSPTrace", plugin = "com.redhat.devtools.lsp4ij", @@ -80,6 +85,7 @@ interface ClientTraceEntry { fun message(): ClientValue } +/** Reads the file, diagnostics and synchronizer already owned by the IDE client. */ @Remote("com.redhat.devtools.lsp4ij.OpenedDocument", plugin = "com.redhat.devtools.lsp4ij") interface ClientDocument { fun getFile(): VirtualFile @@ -89,6 +95,7 @@ interface ClientDocument { fun getSynchronizer(): DocumentSynchronizer? } +/** Lets the driver observe document-open completion and flush pending native text changes. */ @Remote( "com.redhat.devtools.lsp4ij.DocumentContentSynchronizer", plugin = "com.redhat.devtools.lsp4ij", @@ -101,6 +108,7 @@ interface DocumentSynchronizer { fun getDocument(): ParityDocument } +/** Polls native document synchronization without blocking the driver on a remote future. */ @Remote("java.util.concurrent.CompletableFuture") interface DocumentOpenFuture { fun isDone(): Boolean @@ -108,6 +116,7 @@ interface DocumentOpenFuture { fun isCompletedExceptionally(): Boolean } +/** Reads the diagnostic delivered to the native client rather than issuing a separate request. */ @Remote("org.eclipse.lsp4j.Diagnostic", plugin = "com.redhat.devtools.lsp4ij") interface ClientDiagnostic { fun getCode(): DiagnosticCode? @@ -119,6 +128,7 @@ interface ClientDiagnostic { fun getRange(): SourceRange } +/** Exposes both protocol representations of a diagnostic code across the driver boundary. */ @Remote("org.eclipse.lsp4j.jsonrpc.messages.Either", plugin = "com.redhat.devtools.lsp4ij") interface DiagnosticCode { fun getLeft(): String? @@ -126,11 +136,13 @@ interface DiagnosticCode { fun getRight(): Int? } +/** Reads the native diagnostic severity by enum name. */ @Remote("org.eclipse.lsp4j.DiagnosticSeverity", plugin = "com.redhat.devtools.lsp4ij") interface DiagnosticSeverity { fun name(): String } +/** Exposes a protocol range from the IDE process for exact diagnostic assertions. */ @Remote("org.eclipse.lsp4j.Range", plugin = "com.redhat.devtools.lsp4ij") interface SourceRange { fun getStart(): SourcePosition @@ -138,6 +150,7 @@ interface SourceRange { fun getEnd(): SourcePosition } +/** Reads zero-based protocol line and character coordinates through the driver. */ @Remote("org.eclipse.lsp4j.Position", plugin = "com.redhat.devtools.lsp4ij") interface SourcePosition { fun getLine(): Int @@ -145,6 +158,7 @@ interface SourcePosition { fun getCharacter(): Int } +/** Exposes the IDE document modification stamp used to await a fresh diagnostic snapshot. */ @Remote("com.intellij.openapi.editor.Document") interface DocumentVersion { fun getModificationStamp(): Long diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt index d31c5293ec2..46444033f5c 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientProtocol.kt @@ -135,6 +135,7 @@ class ClientProtocol( } } +/** Sends protocol requests through the existing IDE client connection. */ @Remote("org.eclipse.lsp4j.jsonrpc.Endpoint", plugin = "com.redhat.devtools.lsp4ij") interface ClientEndpoint { fun request( @@ -148,6 +149,7 @@ interface ClientEndpoint { ) } +/** Polls or cancels an IDE-side asynchronous operation from the test driver. */ @Remote("java.util.concurrent.CompletableFuture") interface ClientFuture { fun isDone(): Boolean @@ -161,12 +163,14 @@ interface ClientFuture { fun exceptionNow(): ClientFailure } +/** Preserves the failed request's method, protocol code and message in the test process. */ class ClientRequestFailure( method: String, val code: Int?, message: String?, ) : RuntimeException("$method failed ($code): $message") +/** Reads a remote failure message without importing its implementation class. */ @Remote("java.lang.Throwable") interface ClientFailure { fun getMessage(): String? @@ -174,42 +178,51 @@ interface ClientFailure { fun getCause(): ClientFailure? } +/** Unwraps a native JSON-RPC response exception into its protocol error. */ @Remote("org.eclipse.lsp4j.jsonrpc.ResponseErrorException", plugin = "com.redhat.devtools.lsp4ij") interface ClientResponseFailure { fun getResponseError(): ClientResponseError } +/** Reads the server's error code through the driver boundary. */ @Remote("org.eclipse.lsp4j.jsonrpc.messages.ResponseError", plugin = "com.redhat.devtools.lsp4ij") interface ClientResponseError { fun getCode(): Int } +/** Reads a completed protocol result that remains in the IDE process. */ @Remote("java.util.concurrent.CompletableFuture") interface ClientObjectFuture : ClientFuture { fun get(): ClientValue? } +/** Reads a completed collection of native protocol values through the driver. */ @Remote("java.util.concurrent.CompletableFuture") interface ClientListFuture : ClientFuture { fun get(): List? } +/** Holds an opaque IDE-side value for subsequent native conversion or protocol operations. */ @Remote("java.lang.Object") interface ClientValue +/** Carries request JSON in the IDE process for the native protocol endpoint. */ @Remote("com.google.gson.JsonElement", plugin = "com.redhat.devtools.lsp4ij") interface ClientJson +/** Parses request JSON in the IDE process so the native endpoint receives compatible value types. */ @Remote("com.google.gson.JsonParser", plugin = "com.redhat.devtools.lsp4ij") interface ClientJsonParser { fun parseString(json: String): ClientJson } +/** Obtains LSP4IJ's configured Gson instance for protocol-compatible serialization. */ @Remote("com.redhat.devtools.lsp4ij.JSONUtils", plugin = "com.redhat.devtools.lsp4ij") interface ClientJsonTools { fun getLsp4jGson(): ClientGson } +/** Serializes native protocol objects before transferring their contents to the test process. */ @Remote("com.google.gson.Gson", plugin = "com.redhat.devtools.lsp4ij") interface ClientGson { fun toJson(value: ClientValue): String diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt index d95ea3226f6..d7622344fbc 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ClientTrace.kt @@ -175,6 +175,7 @@ class ClientTrace( ): Boolean = URI(left) == URI(right) } +/** Selects a native LSP4IJ trace level without duplicating its enum in the test classloader. */ @Remote("com.redhat.devtools.lsp4ij.settings.ServerTrace", plugin = "com.redhat.devtools.lsp4ij") interface ClientTraceLevel { fun valueOf(name: String): ClientTraceLevel @@ -182,6 +183,7 @@ interface ClientTraceLevel { fun name(): String } +/** Locates IDE editors so tests can inspect the native protocol trace output. */ @Remote("com.intellij.openapi.editor.EditorFactory") interface TraceEditors { fun getInstance(): TraceEditors @@ -189,6 +191,7 @@ interface TraceEditors { fun getAllEditors(): List } +/** Exposes an editor's document for trace-content assertions. */ @Remote("com.intellij.openapi.editor.Editor") interface TraceEditor { fun getDocument(): ParityDocument diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt index ac48347409b..65d2fd308fb 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerPlaybook.kt @@ -29,6 +29,7 @@ import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds import kotlin.time.TimeSource +/** Selects the harness workload, including feature checks and focused lifecycle or responsiveness runs. */ enum class PlaybookMode { FEATURES, STARTUP, @@ -47,6 +48,7 @@ class CompilerPlaybook( private val mode: PlaybookMode = PlaybookMode.FEATURES, private val onResult: (Result) -> Unit = {}, ) { + /** Records one scenario's identity, outcome and elapsed time for the playbook report. */ data class Result( val id: String, val description: String, diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt index f283683033e..42cce07e7ef 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompilerSettingsPage.kt @@ -3,6 +3,7 @@ package org.xtclang.idea.playbook import com.intellij.driver.client.Remote import com.intellij.driver.sdk.Project +/** Drives the plugin's compiler settings through its IDE-side test probe. */ @Remote( "org.xtclang.idea.playbook.probe.CompilerSettingsPage", plugin = "org.xtclang.playbook.probe", diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt index 890a610d72f..e43cf652a01 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/CompletionActions.kt @@ -164,11 +164,13 @@ fun Driver.hasCompletionMetadata( (expected["sorted"]?.asBoolean != true || value["sortText"]?.asString?.isNotBlank() == true) } +/** Exposes the native lookup element underlying a visible completion choice. */ @Remote("com.intellij.codeInsight.lookup.LookupElement") interface NativeCompletionElement { fun getObject(): NativeCompletionProposal } +/** Reads the protocol completion proposal retained by LSP4IJ's lookup element. */ @Remote( "com.redhat.devtools.lsp4ij.client.features.LSPCompletionProposal", plugin = "com.redhat.devtools.lsp4ij", diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt index 25dbd2a1f12..57df5b0c990 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/EditorFocus.kt @@ -18,6 +18,7 @@ fun Driver.dismissPopups() { } } +/** Locates IntelliJ's popup manager so cleanup can dismiss native popups deterministically. */ @Remote("com.intellij.ide.IdeEventQueue") interface NativeEventQueue { fun getInstance(): NativeEventQueue @@ -25,6 +26,7 @@ interface NativeEventQueue { fun getPopupManager(): NativePopupManager } +/** Dismisses native popups before the next scenario changes editor focus. */ @Remote("com.intellij.ide.IdePopupManager") interface NativePopupManager { fun closeAllPopups(): Boolean @@ -137,6 +139,7 @@ internal fun JEditorUiComponent.scrollToCaretNow() { } } +/** Uses the IDE-side focus probe to activate the intended editor. */ @Remote("org.xtclang.idea.playbook.probe.EditorUi", plugin = "org.xtclang.playbook.probe") internal interface NativeEditorUi { fun focusEditor(editor: Editor) @@ -163,6 +166,7 @@ internal interface NativeEditorUi { fun scrollToCaret(editor: Editor) } +/** Requests native application focus before keyboard-driven editor actions. */ @Remote("com.intellij.ui.AppIcon") interface NativeAppFocus { fun getInstance(): NativeAppFocus diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt index 44e0e6c6d62..a52eed37b72 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/IdeServices.kt @@ -19,6 +19,7 @@ interface LspSettings { ) } +/** Applies project LSP client settings through the IDE driver. */ @Remote( "com.redhat.devtools.lsp4ij.settings.ProjectLanguageServerSettings", plugin = "com.redhat.devtools.lsp4ij", @@ -30,6 +31,7 @@ interface ProjectLspSettings { ) } +/** Exposes the selected server definition's native client settings. */ @Remote( "com.redhat.devtools.lsp4ij.settings.LanguageServerSettings\$LanguageServerDefinitionSettings", plugin = "com.redhat.devtools.lsp4ij", @@ -42,6 +44,7 @@ interface LspServerSettings { fun setServerTrace(trace: ClientTraceLevel): LspServerSettings } +/** Finds the active completion popup in the IDE process. */ @Remote("com.intellij.codeInsight.lookup.LookupManager") interface EditorLookupManager { fun getInstance(project: Project): EditorLookupManager @@ -49,6 +52,7 @@ interface EditorLookupManager { fun getActiveLookup(): EditorLookup? } +/** Inspects and selects entries in the native completion popup. */ @Remote( value = "com.intellij.codeInsight.lookup.impl.LookupImpl", serviceInterface = "com.intellij.codeInsight.lookup.Lookup", @@ -63,6 +67,7 @@ interface EditorLookup { fun setCurrentItem(item: CompletionItem) } +/** Reads the native lookup label and renders its presentation for popup assertions. */ @Remote("com.intellij.codeInsight.lookup.LookupElement") interface CompletionItem { fun getLookupString(): String @@ -70,6 +75,7 @@ interface CompletionItem { fun renderElement(presentation: LookupElementPresentation) } +/** Locates LSP4IJ feature support attached to a real IDE file. */ @Remote("com.redhat.devtools.lsp4ij.LSPFileSupport", plugin = "com.redhat.devtools.lsp4ij") interface LspFileSupport { fun getSupport(file: PsiFile): LspFileSupport @@ -81,6 +87,7 @@ interface LspFileSupport { fun getSelectionRangeSupport(): SelectionRangeSupport } +/** Requests native signature help using the editor client's feature support. */ @Remote( "com.redhat.devtools.lsp4ij.features.signatureHelp.LSPSignatureHelpSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -89,6 +96,7 @@ interface SignatureSupport { fun getValidLSPFuture(): SignatureFuture? } +/** Polls a native signature-help request without blocking the UI thread. */ @Remote("java.util.concurrent.CompletableFuture") interface SignatureFuture { fun isDone(): Boolean @@ -98,6 +106,7 @@ interface SignatureFuture { fun get(): SignatureHelp? } +/** Exposes the signatures and active selection returned to the native client. */ @Remote("org.eclipse.lsp4j.SignatureHelp", plugin = "com.redhat.devtools.lsp4ij") interface SignatureHelp { fun getSignatures(): List @@ -107,6 +116,7 @@ interface SignatureHelp { fun getActiveParameter(): Int? } +/** Reads one displayed signature and its parameter metadata. */ @Remote("org.eclipse.lsp4j.SignatureInformation", plugin = "com.redhat.devtools.lsp4ij") interface SignatureInformation { fun getLabel(): String @@ -118,11 +128,13 @@ interface SignatureInformation { fun getParameters(): List } +/** Exposes a signature parameter's label through the IDE driver. */ @Remote("org.eclipse.lsp4j.ParameterInformation", plugin = "com.redhat.devtools.lsp4ij") interface ParameterInformation { fun getLabel(): ParameterLabel } +/** Reads either the string label or its offset-pair protocol representation. */ @Remote("org.eclipse.lsp4j.jsonrpc.messages.Either", plugin = "com.redhat.devtools.lsp4ij") interface ParameterLabel { fun getLeft(): String? @@ -130,6 +142,7 @@ interface ParameterLabel { fun getRight(): ParameterRange } +/** Exposes the start and end offsets of a signature parameter label. */ @Remote("org.eclipse.lsp4j.jsonrpc.messages.Tuple\$Two", plugin = "com.redhat.devtools.lsp4ij") interface ParameterRange { fun getFirst(): Int @@ -137,11 +150,13 @@ interface ParameterRange { fun getSecond(): Int } +/** Reads rendered parameter-hint text to verify what the user actually sees. */ @Remote("com.intellij.ui.components.JBHtmlPane") interface ParameterHintText { fun getText(): String } +/** Dismisses native editor hints between scenarios. */ @Remote("com.intellij.codeInsight.hint.HintManager") interface EditorHints { fun hideAllHints() diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt index 1a4f7ad8c25..53e74414093 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LanguageServicePage.kt @@ -3,6 +3,7 @@ package org.xtclang.idea.playbook import com.intellij.driver.client.Remote import com.intellij.driver.sdk.Project +/** Drives adapter selection and language-service settings through the IDE-side probe. */ @Remote( "org.xtclang.idea.playbook.probe.LanguageServicePage", plugin = "org.xtclang.playbook.probe", diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt index 444b26e502f..bb1be2f2c9b 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/LargeFileEditing.kt @@ -75,6 +75,7 @@ fun Driver.largeFileEditing( println("IntelliJ large-file editing receipt: $report") } +/** Measures native editor work with and without language decorations. */ @Remote("org.xtclang.idea.playbook.probe.LargeFileProbe", plugin = "org.xtclang.playbook.probe") interface LargeFileProbe { fun plain( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt index 29d06131485..944fe508483 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeDiagnostics.kt @@ -9,6 +9,7 @@ import com.intellij.driver.sdk.Project import com.intellij.driver.sdk.singleProject import com.intellij.driver.sdk.ui.components.common.JEditorUiComponent +/** Captures one painted editor diagnostic for severity, message and range assertions. */ data class Diagnostic( val severity: String, val description: String, @@ -24,6 +25,7 @@ fun JEditorUiComponent.installedDiagnostics(): List = Gson().fromJson(json, Array::class.java).toList() } +/** Reads diagnostics actually painted in the IDE rather than querying the server independently. */ @Remote("org.xtclang.idea.playbook.probe.EditorDiagnostics", plugin = "org.xtclang.playbook.probe") interface EditorDiagnostics { fun read( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt index f7e2ad2ecef..6bcc74151e1 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NativeSemanticViews.kt @@ -193,6 +193,7 @@ internal fun targetNames( ?: error("Target does not select a declaration: $location") } +/** Locates native location, hover, token and inlay support for an editor file. */ @Remote("com.redhat.devtools.lsp4ij.LSPFileSupport", plugin = "com.redhat.devtools.lsp4ij") interface NativeSemanticSupport { fun getDefinitionSupport(): NativeLocationsSupport @@ -208,6 +209,7 @@ interface NativeSemanticSupport { fun getInlayHintsSupport(): NativeInlaysSupport } +/** Uses the installed client's location feature for navigation assertions. */ @Remote( "com.redhat.devtools.lsp4ij.features.AbstractLSPDocumentFeatureSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -216,16 +218,19 @@ interface NativeLocationsSupport { fun getValidLSPFuture(): NativeLocationsFuture? } +/** Polls a native navigation request and exposes its completed locations. */ @Remote("java.util.concurrent.CompletableFuture") interface NativeLocationsFuture : ClientFuture { fun get(): List } +/** Reads a native navigation target's URI and source range. */ @Remote("com.redhat.devtools.lsp4ij.usages.LocationData", plugin = "com.redhat.devtools.lsp4ij") interface NativeLocation { fun location(): ClientValue } +/** Requests hover through the installed client's feature support. */ @Remote( "com.redhat.devtools.lsp4ij.features.documentation.LSPHoverSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -234,11 +239,13 @@ interface NativeHoverSupport { fun getValidLSPFuture(): NativeHoverFuture? } +/** Polls the native hover result without blocking the UI thread. */ @Remote("java.util.concurrent.CompletableFuture") interface NativeHoverFuture : ClientFuture { fun get(): List } +/** Exposes the hover contents received by the IDE client. */ @Remote( "com.redhat.devtools.lsp4ij.features.documentation.HoverData", plugin = "com.redhat.devtools.lsp4ij", @@ -247,6 +254,7 @@ interface NativeHover { fun hover(): ClientValue } +/** Requests semantic tokens through the installed client. */ @Remote( "com.redhat.devtools.lsp4ij.features.semanticTokens.LSPSemanticTokensSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -255,11 +263,13 @@ interface NativeTokensSupport { fun getValidLSPFuture(): NativeTokensFuture? } +/** Polls the native semantic-token result. */ @Remote("java.util.concurrent.CompletableFuture") interface NativeTokensFuture : ClientFuture { fun get(): NativeTokens? } +/** Exposes the encoded semantic-token data received by the client. */ @Remote( "com.redhat.devtools.lsp4ij.features.semanticTokens.SemanticTokensData", plugin = "com.redhat.devtools.lsp4ij", @@ -268,6 +278,7 @@ interface NativeTokens { fun getSemanticTokens(): ClientValue } +/** Requests inlay hints through the installed client. */ @Remote( "com.redhat.devtools.lsp4ij.features.inlayhint.LSPInlayHintsSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -276,11 +287,13 @@ interface NativeInlaysSupport { fun getValidLSPFuture(): NativeInlaysFuture? } +/** Polls the native inlay-hint result. */ @Remote("java.util.concurrent.CompletableFuture") interface NativeInlaysFuture : ClientFuture { fun get(): List } +/** Reads an inlay hint retained by the client for rendering. */ @Remote( "com.redhat.devtools.lsp4ij.features.inlayhint.InlayHintData", plugin = "com.redhat.devtools.lsp4ij", @@ -289,11 +302,13 @@ interface NativeInlayData { fun inlayHint(): ClientValue } +/** Locates the actual editor inlay model for painted-hint assertions. */ @Remote("com.intellij.openapi.editor.Editor") interface NativeInlayEditor { fun getInlayModel(): NativeInlayModel } +/** Enumerates rendered inlays in the editor's source range. */ @Remote("com.intellij.openapi.editor.InlayModel") interface NativeInlayModel { fun getInlineElementsInRange( @@ -302,6 +317,7 @@ interface NativeInlayModel { ): List } +/** Exposes a rendered inlay's offset for comparison with its protocol position. */ @Remote("com.intellij.openapi.editor.Inlay") interface NativeInlay { fun getOffset(): Int diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt index 47f896de9b1..da05aa8a720 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/NavigationChooserTest.kt @@ -13,6 +13,10 @@ import javax.swing.JTable import javax.swing.KeyStroke import javax.swing.SwingUtilities +/** + * Reproduces native navigation-popup selection without relying on global keyboard focus. The selected row + * must reach the popup action exactly once, preventing a stalled playbook or duplicate navigation. + */ class NavigationChooserTest { @Test fun `the chosen row reaches the popup action once without keyboard focus`() = diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt index 085a4f3fba0..9a6956546b3 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityColors.kt @@ -133,6 +133,7 @@ internal fun ParityWorkspace.captureColorScheme(document: ParityWorkspace.Docume } } +/** Exercises the IDE's native color decoration and picker through a test probe. */ @Remote("org.xtclang.idea.playbook.probe.ColorUi", plugin = "org.xtclang.playbook.probe") internal interface ColorProbe { fun offsets(editor: Editor): List diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt index 5e6a0c94ae2..64731c3339b 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityCompilerImports.kt @@ -291,6 +291,7 @@ internal fun ParityScenarios.compilerImportCases() { } } +/** Drives Gradle compiler-model import through the plugin's native settings page. */ @Remote("org.xtclang.idea.playbook.probe.CompilerImportPage", plugin = "org.xtclang.playbook.probe") internal interface CompilerImportPage { fun open(project: Project): CompilerImportPage diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt index 79f28286b18..513031c795b 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityHighlighting.kt @@ -5,6 +5,7 @@ import com.intellij.driver.model.OnDispatcher import com.intellij.driver.sdk.Editor import kotlin.time.Duration.Companion.seconds +/** Captures painted token text and attributes for lexical and semantic highlighting comparisons. */ private data class HighlightToken( val start: Int, val end: Int, @@ -133,6 +134,7 @@ internal fun ParityScenarios.highlightingCases() { } } +/** Reads editor highlighters to verify the final theme-based colors shown to the user. */ @Remote("org.xtclang.idea.playbook.probe.HighlightingUi", plugin = "org.xtclang.playbook.probe") internal interface HighlightingProbe { fun lexical( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt index 2bdc665d557..fd62bca06d3 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityInlineCompletion.kt @@ -85,6 +85,7 @@ internal fun ParityScenarios.inlineCompletionCases() { } } +/** Drives native inline completion and observes the suggested text. */ @Remote("org.xtclang.idea.playbook.probe.InlineCompletionUi", plugin = "org.xtclang.playbook.probe") internal interface InlineCompletionProbe { fun type( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt index c702872d25b..685945553eb 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityLibrarySettings.kt @@ -114,6 +114,7 @@ internal fun ParityScenarios.librarySettingsCases() { } } +/** Edits explicit compiler libraries through the native settings probe. */ @Remote("org.xtclang.idea.playbook.probe.CompilerLibrariesPage", plugin = "org.xtclang.playbook.probe") interface CompilerLibrariesPage { fun content(project: Project): String? diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt index 4a1d8183f48..ae8cdd0d66e 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityPlatform.kt @@ -1183,6 +1183,7 @@ internal fun ParityScenarios.platformCases() { } } +/** Observes streamed protocol batches through the IDE-side test probe. */ @Remote("org.xtclang.idea.playbook.probe.PartialResults", plugin = "org.xtclang.playbook.probe") interface PartialResults { fun listen( @@ -1195,6 +1196,7 @@ interface PartialResults { fun dispose() } +/** Applies protocol workspace edits through the installed IDE client. */ @Remote("org.xtclang.idea.playbook.probe.WorkspaceEdits", plugin = "org.xtclang.playbook.probe") interface WorkspaceEdits { fun apply( @@ -1203,6 +1205,7 @@ interface WorkspaceEdits { ): ClientFuture } +/** Drives native file-tree operations and undo history for filesystem-edit scenarios. */ @Remote("org.xtclang.idea.playbook.probe.FileTreeOperations", plugin = "org.xtclang.playbook.probe") interface FileTreeOperations { fun globalHistoryAvailable( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt index e78754d1d44..1a534bb40a8 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt @@ -103,6 +103,7 @@ internal fun ParityScenarios.progressCases() { } } +/** Observes and cancels the IDE's displayed work-progress indicators. */ @Remote("org.xtclang.idea.playbook.probe.ProgressUi", plugin = "org.xtclang.playbook.probe") internal interface ProgressUi { fun visible( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt index 0f4bd28008b..8d98540c644 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReferenceLenses.kt @@ -66,6 +66,7 @@ internal fun ParityScenarios.referenceLensCases() { } } +/** Reads and activates native code lenses, including reference counts and Run. */ @Remote("org.xtclang.idea.playbook.probe.CodeLensUi", plugin = "org.xtclang.playbook.probe") interface CodeLensProbe { fun titles(editor: Editor): List diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt index ff60c3d7fda..e138b587f3a 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityReliability.kt @@ -161,6 +161,7 @@ internal fun ParityScenarios.reliabilityCases() { } } +/** Observes server refresh requests through the installed client connection. */ @Remote("org.xtclang.idea.playbook.probe.RefreshRequests", plugin = "org.xtclang.playbook.probe") interface RefreshRequests { fun listen(project: Project): RefreshRequests @@ -178,6 +179,7 @@ interface RefreshRequests { fun dispose() } +/** Reads the plugin's effective compiler configuration report. */ @Remote("org.xtclang.idea.playbook.probe.CompilerReportPage", plugin = "org.xtclang.playbook.probe") interface CompilerReportPage { fun open(project: Project): CompilerReportPage diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt index 1ba606300eb..b82df901cc5 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRename.kt @@ -189,6 +189,7 @@ internal fun ParityWorkspace.awaitRetired( } } +/** Obtains rename proposals through the plugin's native rename integration. */ @Remote("org.xtclang.idea.lsp.XtcRenameEdit", plugin = "org.xtclang.idea") interface NativeRenameRequests { fun request( @@ -199,9 +200,11 @@ interface NativeRenameRequests { ): ClientFuture } +/** Keeps a workspace edit in the IDE process until the native transaction applies it. */ @Remote("org.eclipse.lsp4j.WorkspaceEdit", plugin = "com.redhat.devtools.lsp4ij") interface NativeWorkspaceEdit +/** Applies a prepared rename using the plugin's native edit transaction. */ @Remote("org.xtclang.idea.lsp.XtcRenameEdit", plugin = "org.xtclang.idea") interface NativeRenameTransaction { fun getEdit(): NativeWorkspaceEdit @@ -209,6 +212,7 @@ interface NativeRenameTransaction { fun apply(): Boolean } +/** Polls a prepared native rename operation. */ @Remote("java.util.concurrent.CompletableFuture") interface NativeRenameFuture : ClientFuture { fun get(): NativeRenameTransaction? diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt index 6f384cc675f..b7004db7106 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityRuntimeSettings.kt @@ -162,6 +162,7 @@ internal fun ParityScenarios.runtimeSettingsCases() { } } +/** Edits runtime launch and logging options through the native settings probe. */ @Remote("org.xtclang.idea.playbook.probe.ServerRuntimePage", plugin = "org.xtclang.playbook.probe") interface ServerRuntimePage { fun content(): String diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt index c4bf5bb9f5b..2d3f6a16d6c 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParitySemantics.kt @@ -116,6 +116,7 @@ internal fun ParityScenarios.semanticCases() { ) }!! + /** Decodes semantic-token positions for comparison with native editor observations. */ data class Token( val line: Int, val character: Int, diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt index 26ff3d712d3..c1bffbdb166 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityTypeMoves.kt @@ -199,6 +199,7 @@ internal fun ParityScenarios.typeMoveCases() { } } +/** Enters the target name in the native move dialog. */ @Remote("javax.swing.JTextField") private interface MoveNameField { fun setText(text: String) diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt index 57431ad7870..8dd47bde8f3 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityWorkspace.kt @@ -43,6 +43,7 @@ class ParityWorkspace( private val projectRoot = with(driver) { Path.of(singleProject().getBasePath()) } val directory: Path = projectRoot.resolve("parity/$id") + /** Couples a fixture's path and text with its playbook workspace identity. */ inner class Document( val file: String, private val source: VirtualFile, @@ -488,6 +489,7 @@ internal fun JsonObject.pattern(name: String): Regex = ) } +/** Finds fixture files through IntelliJ's virtual filesystem. */ @Remote("com.intellij.openapi.vfs.LocalFileSystem") interface ParityFiles { fun getInstance(): ParityFiles @@ -495,6 +497,7 @@ interface ParityFiles { fun refreshAndFindFileByPath(path: String): VirtualFile? } +/** Obtains native documents for fixture files through the IDE document manager. */ @Remote("com.intellij.openapi.fileEditor.FileDocumentManager") interface ParityDocuments { fun getDocument(file: VirtualFile): ParityDocument? @@ -508,6 +511,7 @@ interface ParityDocuments { fun saveDocument(document: ParityDocument) } +/** Reads the fixture's actual IDE buffer through the driver. */ @Remote("com.intellij.openapi.editor.Document") interface ParityDocument { fun getText(): String diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt index 283e476e8ac..ac7b06ce83f 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailure.kt @@ -4,6 +4,7 @@ import com.intellij.driver.sdk.WaitForException import java.util.concurrent.CancellationException import java.util.concurrent.TimeoutException +/** Stops the playbook when cleanup cannot restore a safe state for subsequent scenarios. */ internal class PlaybookCleanupFailure( cause: Throwable, ) : IllegalStateException("Could not restore the playbook workspace", cause) diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt index cd51c127b03..6b7835b5014 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookFailureTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.api.Test import java.util.concurrent.TimeoutException import kotlin.time.Duration.Companion.seconds +/** + * Checks which harness failures must stop an editor playbook. Wrapped timeouts, cancellation and failed + * cleanup cannot be treated as an ordinary failed case followed by unsafe further edits. + */ class PlaybookFailureTest { @Test fun `timeouts stop the run including wrapped driver failures`() { assertTrue(WaitForException(1.seconds, "Editor did not change").mustStopPlaybook()) diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt index e5d473b6f8c..f353a9a6984 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/PlaybookProgress.kt @@ -3,6 +3,7 @@ package org.xtclang.idea.playbook import com.intellij.driver.client.Remote import com.intellij.driver.sdk.Project +/** Updates the IDE-side playbook progress display from the driver process. */ @Remote("org.xtclang.idea.playbook.probe.PlaybookProgress", plugin = "org.xtclang.playbook.probe") internal interface PlaybookProgress { fun install(project: Project) diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt index 614cf3c327e..bbb4451d158 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ProjectLifecycle.kt @@ -175,6 +175,7 @@ internal fun Driver.projectLifecycle(shared: SharedScenarios) { ) } +/** Opens and closes native projects through the lifecycle test probe. */ @Remote("org.xtclang.idea.playbook.probe.ProjectLifecycle", plugin = "org.xtclang.playbook.probe") internal interface ProjectLifecycle { fun open(path: String): OpenProjectFuture @@ -197,11 +198,13 @@ internal interface ProjectLifecycle { fun visible(project: Project): Boolean } +/** Polls native project opening before subsequent playbook actions use the project. */ @Remote("java.util.concurrent.CompletableFuture") internal interface OpenProjectFuture : ClientFuture { fun get(): Project? } +/** Polls native project disposal before lifecycle assertions continue. */ @Remote("java.util.concurrent.CompletableFuture") internal interface CloseProjectFuture : ClientFuture { fun get(): Boolean diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt index 4591b701be5..e72329521ed 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/RefactoringActions.kt @@ -239,6 +239,7 @@ fun Driver.partialGraphHierarchy( } } +/** Invokes a native dialog button through the driver instead of relying on keyboard focus. */ @Remote("javax.swing.JButton") interface NativeButton { fun isEnabled(): Boolean @@ -246,11 +247,13 @@ interface NativeButton { fun doClick() } +/** Selects a native list row before invoking the associated dialog action. */ @Remote("javax.swing.JList") interface NativeListSelection { fun setSelectedIndex(index: Int) } +/** Selects and activates a native navigation-popup target deterministically. */ @Remote("org.xtclang.idea.playbook.probe.NavigationChooser", plugin = "org.xtclang.playbook.probe") internal interface NativeNavigationChooser { fun choose( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt index d771b6a9304..ffdd1b3002c 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ReferenceActions.kt @@ -78,11 +78,13 @@ fun NativeUsageView.rangesIn(path: String): List = .mapNotNull { it.getNavigationRange() } .map { it.getStartOffset() until it.getEndOffset() } +/** Locates the native Find Usages result view. */ @Remote("com.intellij.usages.UsageViewManager") interface NativeUsageViews { fun getSelectedUsageView(): NativeUsageView? } +/** Exposes the usages displayed by IntelliJ for reference-result assertions. */ @Remote("com.intellij.usages.UsageView") interface NativeUsageView { fun isSearchInProgress(): Boolean @@ -90,11 +92,13 @@ interface NativeUsageView { fun getUsages(): Collection } +/** Reads one native usage entry's underlying source information. */ @Remote("com.intellij.usages.UsageInfo2UsageAdapter") interface NativeUsage { fun getMergedInfos(): Array } +/** Exposes a usage's file and source range through the driver. */ @Remote("com.intellij.usageView.UsageInfo") interface NativeUsageInfo { fun getVirtualFile(): VirtualFile? @@ -102,6 +106,7 @@ interface NativeUsageInfo { fun getNavigationRange(): NativeSourceRange? } +/** Reads offsets from a native usage result without converting to protocol coordinates. */ @Remote("com.intellij.openapi.util.Segment") interface NativeSourceRange { fun getStartOffset(): Int @@ -109,6 +114,7 @@ interface NativeSourceRange { fun getEndOffset(): Int } +/** Requests document highlights through the installed native client. */ @Remote( "com.redhat.devtools.lsp4ij.features.highlight.LSPHighlightSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -117,6 +123,7 @@ interface HighlightSupport { fun getValidLSPFuture(): HighlightFuture? } +/** Polls the client's document-highlight result. */ @Remote("java.util.concurrent.CompletableFuture") interface HighlightFuture { fun isDone(): Boolean @@ -126,6 +133,7 @@ interface HighlightFuture { fun get(): List } +/** Reads a native document highlight's range and access kind. */ @Remote("org.eclipse.lsp4j.DocumentHighlight", plugin = "com.redhat.devtools.lsp4ij") interface ClientHighlight { fun getRange(): SourceRange @@ -133,6 +141,7 @@ interface ClientHighlight { fun getKind(): ClientHighlightKind? } +/** Exposes the protocol highlight kind by enum name. */ @Remote("org.eclipse.lsp4j.DocumentHighlightKind", plugin = "com.redhat.devtools.lsp4ij") interface ClientHighlightKind { fun name(): String diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt index 8c95f2d1132..7ed3704755c 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SettingsPersistence.kt @@ -77,6 +77,7 @@ internal fun Driver.settingsPersistence( } } +/** Drives persisted settings and reloads through the native test probe. */ @Remote("org.xtclang.idea.playbook.probe.SettingsPersistencePage", plugin = "org.xtclang.playbook.probe") internal interface SettingsPersistencePage { fun sources( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt index 4c8c04f34fc..2826adea8d1 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/SharedScenarios.kt @@ -14,6 +14,7 @@ class SharedScenarios( val common: Common, val scenarios: Map, ) { + /** Holds fixture and configuration data shared by all editor scenarios. */ data class Common( val sourceModules: List, val fixtures: List, @@ -22,22 +23,26 @@ class SharedScenarios( val colorPrototype: JsonObject, ) + /** Identifies a fixture file and the source pattern used to populate it. */ data class Fixture( val file: String, val pattern: String, ) + /** Identifies the shared edit, run and inspect targets. */ data class Editing( val file: String, val run: String, val inspect: String, ) + /** Records native-client coverage and explicit limitations for one scenario. */ data class Coverage( val coverage: String, val limitations: List, ) + /** Retains a scenario's instructions, expectations and IntelliJ coverage from the shared catalog. */ data class Scenario( val title: String, val manual: List, @@ -65,6 +70,7 @@ class SharedScenarios( val configuration: Configuration = core("CFG1") val warning: Warning = core("7a.8") + /** Describes a fixture module's sources, dependencies and optional resource roots. */ data class SourceModule( val name: String, val uri: String, @@ -72,6 +78,7 @@ class SharedScenarios( val resourceRoots: List? = null, ) + /** Replaces a unique source fragment so both editor drivers make the same change. */ data class Edit( val from: String, val to: String, @@ -82,12 +89,14 @@ class SharedScenarios( } } + /** Pairs a source cursor marker with the expected navigation target. */ data class Location( val cursor: String, val targetFile: String, val target: String, ) + /** Couples an intentional source error with its expected diagnostic classification and range. */ data class ExpectedError( val edit: Edit, val severity: String, @@ -97,6 +106,7 @@ class SharedScenarios( val rangeEnd: String, ) + /** Describes the shared diagnostic introduction and repair scenario. */ data class Diagnostics( val id: String, val file: String, @@ -104,12 +114,14 @@ class SharedScenarios( val cleanAppend: String, ) + /** Lists the source-to-definition assertions shared by both editors. */ data class Definitions( val id: String, val file: String, val locations: List, ) + /** Describes the incomplete source, chosen completion and expected accepted edit. */ data class Completion( val id: String, val file: String, @@ -119,12 +131,14 @@ class SharedScenarios( val accepted: Edit, ) + /** Describes one navigation assertion in its fixture file. */ data class Navigation( val id: String, val file: String, val location: Location, ) + /** Describes a dependency edit whose effects must reach its consumer. */ data class DependencyEdit( val id: String, val file: String, @@ -132,11 +146,13 @@ class SharedScenarios( val edit: Edit, ) + /** Identifies the consumer used to verify compiler configuration changes. */ data class Configuration( val id: String, val consumer: String, ) + /** Describes warning counts, source ownership and the edit that changes them. */ data class Warning( val id: String, val file: String, diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt index a7511cb56eb..4bf56c6077c 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StartupEditing.kt @@ -126,6 +126,7 @@ fun Driver.startupEditing( println("IntelliJ startup editing receipt: $report") } +/** Drives edits while the IDE language service is still starting. */ @Remote("org.xtclang.idea.playbook.probe.StartupEdits", plugin = "org.xtclang.playbook.probe") interface StartupEdits { fun openAndEdit( diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt index 73c758f275d..465d3e88315 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/StructureViews.kt @@ -124,16 +124,19 @@ fun Driver.selectionParents( withContext(OnDispatcher.EDT) { editor.editor.getSelectionModel().removeSelection() } } +/** Exposes an editor's native folding model through the driver. */ @Remote("com.intellij.openapi.editor.Editor") interface FoldingEditor { fun getFoldingModel(): EditorFolding } +/** Enumerates the fold regions installed in the native editor. */ @Remote("com.intellij.openapi.editor.FoldingModel") interface EditorFolding { fun getAllFoldRegions(): Array } +/** Reads native fold offsets and validity for folding assertions. */ @Remote("com.intellij.openapi.editor.FoldRegion") interface EditorFoldRegion { fun isValid(): Boolean @@ -143,6 +146,7 @@ interface EditorFoldRegion { fun getEndOffset(): Int } +/** Reads the actual editor selection for expand-selection assertions. */ @Remote("com.intellij.openapi.editor.SelectionModel") interface SelectionOffsets { fun getSelectionStart(): Int @@ -150,6 +154,7 @@ interface SelectionOffsets { fun getSelectionEnd(): Int } +/** Requests selection ranges through the installed client's feature support. */ @Remote( "com.redhat.devtools.lsp4ij.features.selectionRange.LSPSelectionRangeSupport", plugin = "com.redhat.devtools.lsp4ij", @@ -158,6 +163,7 @@ interface SelectionRangeSupport { fun getValidLSPFuture(): SelectionRangeFuture? } +/** Polls native selection-range results. */ @Remote("java.util.concurrent.CompletableFuture") interface SelectionRangeFuture { fun isDone(): Boolean @@ -167,6 +173,7 @@ interface SelectionRangeFuture { fun get(): List } +/** Exposes a protocol selection range and its enclosing parent. */ @Remote("org.eclipse.lsp4j.SelectionRange", plugin = "com.redhat.devtools.lsp4ij") interface ClientSelectionRange { fun getRange(): SourceRange diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt index bc39a8f6feb..a48a499fd47 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UiWaitsTest.kt @@ -10,6 +10,10 @@ import java.util.concurrent.CancellationException import java.util.concurrent.atomic.AtomicInteger import kotlin.time.Duration +/** + * Protects readiness polling from checking a successful observation twice during connection replacement. Uses + * controlled observations to cover expiry, pending reads and cancellation without launching an IDE. + */ class UiWaitsTest { @Test fun `a successful readiness observation is not checked again`() { diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt index 7a4e7f1f17d..fbf1eba8a83 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/UpstreamReplacementGates.kt @@ -182,6 +182,7 @@ internal fun ParityWorkspace.nativeInlineAlternatives( check(document.text == source) } +/** Checks that virtual library documents remain valid and read-only in the IDE. */ @Remote("com.intellij.openapi.vfs.VirtualFile") internal interface NativeLibraryFile { fun isValid(): Boolean diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt index d828567808b..8f9db93ff8f 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/WorkspaceFolders.kt @@ -30,21 +30,26 @@ fun Driver.changeWorkspaceFolders( .didChangeWorkspaceFolders(params) } +/** Exposes the workspace service of the IDE's existing language-server connection. */ @Remote("org.eclipse.lsp4j.services.LanguageServer", plugin = "com.redhat.devtools.lsp4ij") interface RemoteLanguageServer { fun getWorkspaceService(): RemoteWorkspaceService } +/** Delivers workspace-folder changes through the installed client connection. */ @Remote("org.eclipse.lsp4j.services.WorkspaceService", plugin = "com.redhat.devtools.lsp4ij") interface RemoteWorkspaceService { fun didChangeWorkspaceFolders(params: RemoteWorkspaceFoldersParams) } +/** Constructs a protocol workspace folder in the IDE process. */ @Remote("org.eclipse.lsp4j.WorkspaceFolder", plugin = "com.redhat.devtools.lsp4ij") interface RemoteWorkspaceFolder +/** Carries added and removed native workspace-folder values. */ @Remote("org.eclipse.lsp4j.WorkspaceFoldersChangeEvent", plugin = "com.redhat.devtools.lsp4ij") interface RemoteWorkspaceFoldersEvent +/** Wraps a workspace-folder change for the native protocol notification. */ @Remote("org.eclipse.lsp4j.DidChangeWorkspaceFoldersParams", plugin = "com.redhat.devtools.lsp4ij") interface RemoteWorkspaceFoldersParams diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt index f0d1f061580..f0724038c77 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/probe/PlaybookProgress.kt @@ -47,6 +47,7 @@ object PlaybookProgress { @JvmStatic fun text(project: Project): String = (WindowManager.getInstance().getStatusBar(project)?.getWidget(ID) as Widget).label.text + /** Renders the running scenario and completion count in the IDE status bar. */ private class Widget( private val frame: JFrame?, ) : CustomStatusBarWidget { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt index 584b54fcb58..ce826a16f75 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ClosedRefactoringDocuments.kt @@ -11,6 +11,12 @@ import org.eclipse.lsp4j.WorkspaceEdit // TODO LSP4IJ: UP21 — WorkspaceEdit leaves closed documents unsaved and disconnected from the // server. Persist only this transaction's closed documents, including after native Undo/Redo. + +/** + * Keeps closed documents changed by a refactoring synchronized with disk. Undo actions bracket the edit so + * saving happens after text and paths are restored on both Undo and Redo; pre-existing unsaved buffers are + * excluded during capture. + */ internal class ClosedRefactoringDocuments private constructor( private val project: Project, private val documents: List, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt index 525dce22f75..f06f1dab350 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CodeActionMessages.kt @@ -10,6 +10,11 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage // TODO LSP4IJ: UP11 — resolved action edits need a version-checked write command, not an undo-transparent // action. Route selection through the supported client-command extension until upstream preserves // import/member-generation Undo/Redo (X105/X122). Listing or resolving alone never applies edits. + +/** + * Adapts unresolved server code actions to a client command that resolves them on invocation. Preserves the + * original action data so expensive edits can remain deferred with the supported LSP4IJ version. + */ internal object CodeActionMessages { const val COMMAND = "xtc.resolveCodeAction" diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt index ae8cf19ed8b..3741f003333 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerBuildModel.kt @@ -261,9 +261,7 @@ object CompilerBuildModel { } val output = outputs.firstOrNull { it.isCancelled || it.isTimeout || it.exitCode != 0 } when { - output?.isCancelled == true || indicator.isCanceled -> { - model.finish(operation, CompilerImport.Outcome.CANCELLED) - } + output?.isCancelled == true || indicator.isCanceled -> model.finish(operation, CompilerImport.Outcome.CANCELLED) output != null -> { model.finish( @@ -274,9 +272,7 @@ object CompilerBuildModel { ) } - else -> { - model.finish(operation, CompilerImport.Outcome.SUCCEEDED, cancelled = { indicator.isCanceled }) - } + else -> model.finish(operation, CompilerImport.Outcome.SUCCEEDED, cancelled = { indicator.isCanceled }) } } catch (cancelled: ProcessCanceledException) { model.finish(operation, CompilerImport.Outcome.CANCELLED) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt index dc30c9ab3a5..046aa68f60a 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImport.kt @@ -10,24 +10,29 @@ internal class CompilerImport( private val importId: (String) -> String? = { null }, private val validate: (String) -> Unit, ) { + /** Terminal outcome displayed for a compiler-model import operation. */ enum class Outcome { SUCCEEDED, CANCELLED, FAILED } + /** Identity of one preparation or import attempt, used to reject superseded completions. */ class Operation( val prepare: Boolean, ) { val id: String = UUID.randomUUID().toString() } + /** Last completed import outcome and its user-facing explanation. */ data class Result( val outcome: Outcome, val message: String, val finished: Instant = Instant.now(), ) + /** An observed model-file state; a null text records that the file was absent. */ private data class Observed( val text: String?, ) + /** Atomic import state tying the accepted model to the observation and operation that produced it. */ private data class State( val accepted: String? = null, val observed: Observed? = null, @@ -105,9 +110,7 @@ internal class CompilerImport( ) } - outcome == Outcome.FAILED -> { - Result(outcome, detail ?: "Gradle import failed; previous compiler configuration retained.") - } + outcome == Outcome.FAILED -> Result(outcome, detail ?: "Gradle import failed; previous compiler configuration retained.") validation.isFailure -> { Result( @@ -116,9 +119,7 @@ internal class CompilerImport( ) } - else -> { - Result(Outcome.SUCCEEDED, "Ecstasy compiler inputs imported.") - } + else -> Result(Outcome.SUCCEEDED, "Ecstasy compiler inputs imported.") } val after = before.copy( diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt index cb909ecb560..86d20bc85f0 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerImportService.kt @@ -8,6 +8,10 @@ import com.intellij.openapi.project.Project import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicReference +/** + * Owns compiler-model import for one IDE project. Coordinates model discovery and validation with the import + * state machine, and retires pending work when the project is disposed. + */ @Service(Service.Level.PROJECT) internal class CompilerImportService( private val project: Project, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt index 7dcb5427a84..789625e5f60 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerLibrariesPanel.kt @@ -14,6 +14,10 @@ import javax.swing.JButton import javax.swing.JPanel import javax.swing.table.DefaultTableModel +/** + * Edits ordered binary module paths and their attached source roots. Keeps the inherit-from-Gradle choice + * distinct from an explicitly configured library list. + */ internal class CompilerLibrariesPanel( private val project: Project, ) : JPanel(BorderLayout(0, 8)) { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt index da806f3d226..e9ab349ff82 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/CompilerRootWatches.kt @@ -23,6 +23,10 @@ internal class CompilerRootWatches( private val watch: (Set) -> AutoCloseable, private val refresh: (Set) -> CompletableFuture, ) : AutoCloseable { + /** + * Watch registrations and their shared lease, replaced together when roots change or the connection + * closes. + */ private data class State( val registrations: Map> = emptyMap(), val lease: AutoCloseable = AutoCloseable {}, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt index feb922c0f28..992f304a4ef 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixes.kt @@ -14,7 +14,13 @@ import java.util.UUID // suppresses annotation refresh. Give each delivered full report a client-only data revision so // annotations acquire the new fixes. Restore the server's exact data before a code-action request. // Remove when upstream retains equivalent lazy fixes or refreshes their owning annotations. + +/** + * Invalidates diagnostic quick-fix presentations when the server refreshes their actions, even if the visible + * diagnostic text is unchanged. + */ internal object DiagnosticQuickFixes { + /** Original diagnostic data plus a revision that makes refreshed quick fixes a new presentation. */ private data class Presentation( val original: Any?, val revision: UUID, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt index c18c6fe1c61..41495717edc 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessages.kt @@ -21,16 +21,19 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage internal class DiagnosticResultMessages( private val snapshot: (String) -> DocumentStartupMessages.Snapshot?, ) { + /** Diagnostic result namespace for one document and optional provider identifier. */ private data class Key( val uri: String, val identifier: String?, ) + /** Document snapshot captured when a pull-diagnostic request is sent. */ private data class Pending( val key: Key, val snapshot: DocumentStartupMessages.Snapshot, ) + /** Cached result ID tied to the exact document snapshot that produced it. */ private data class Result( val snapshot: DocumentStartupMessages.Snapshot, val id: String, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt index b278d310a16..c58780a1802 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMoves.kt @@ -20,6 +20,11 @@ import java.util.concurrent.CompletableFuture // TODO LSP4IJ: UP19 — reconnect open descendants when their directory moves or is renamed. // Upstream onFileRenameAfter handles only the event's exact file. Remove this bridge once X162 // passes Move/Undo/Redo and subsequent unsaved edits without stale document connections. + +/** + * Keeps open LSP documents attached to the correct URIs during directory moves. Closes them under their old + * URIs before the VFS change, then lets synchronization reopen them at their new paths. + */ internal class DirectoryDocumentMoves( private val wrapper: LanguageServerWrapper, private val disposed: () -> Boolean, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt index 22149559144..40368746912 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/DocumentStartupMessages.kt @@ -27,17 +27,20 @@ import java.net.URI internal class DocumentStartupMessages( private val snapshot: (String) -> Snapshot?, ) { + /** Owner, document stamp and text used to check whether startup synchronization is still current. */ data class Snapshot( val owner: Any, val stamp: Long, val text: String, ) + /** Document owner and deferred change notification while its open message is being coordinated. */ private data class Opening( val owner: Any, val change: NotificationMessage, ) + /** Optional startup snapshot to incorporate into one document notification. */ private data class Fold( val uri: String, val snapshot: Snapshot?, @@ -45,6 +48,11 @@ internal class DocumentStartupMessages( // The actual transmitted version, never a second counter. A closed entry retains only the // version ceiling: LSP4IJ can reuse versions on reopen, making old server edits ambiguous. + + /** + * Last transmitted document state. The retired version ceiling prevents a replacement synchronizer from + * reusing an older LSP version. + */ private data class Sent( val owner: Any?, val version: Int, @@ -84,17 +92,11 @@ internal class DocumentStartupMessages( val params = (message as? NotificationMessage)?.params val uri = when (params) { - is DidOpenTextDocumentParams -> { - params.textDocument.uri - } + is DidOpenTextDocumentParams -> params.textDocument.uri - is DidChangeTextDocumentParams -> { - params.textDocument.uri - } + is DidChangeTextDocumentParams -> params.textDocument.uri - is DidCloseTextDocumentParams -> { - params.textDocument.uri - } + is DidCloseTextDocumentParams -> params.textDocument.uri else -> { ((message as? RequestMessage)?.params as? FoldingRangeRequestParams) @@ -108,17 +110,11 @@ internal class DocumentStartupMessages( val current = uri?.let(snapshot) synchronized(lock) { when (params) { - is DidOpenTextDocumentParams -> { - open(message, params, current, next) - } + is DidOpenTextDocumentParams -> open(message, params, current, next) - is DidChangeTextDocumentParams -> { - change(message, params, current, next) - } + is DidChangeTextDocumentParams -> change(message, params, current, next) - is DidCloseTextDocumentParams -> { - close(params.textDocument.uri, current, next) - } + is DidCloseTextDocumentParams -> close(params.textDocument.uri, current, next) else -> { if (message is RequestMessage && message.params is FoldingRangeRequestParams) { diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt index 5e73950ad4e..db443223371 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/EditorRefresh.kt @@ -25,6 +25,7 @@ internal class EditorRefresh( private val owner: Disposable, private val isDisposed: () -> Boolean, ) { + /** Editor presentation whose refresh requests can be coalesced independently. */ enum class Feature { LENSES, HINTS, @@ -38,6 +39,7 @@ internal class EditorRefresh( Feature.TOKENS to SemanticTokensEditorFeature(), ) + /** Editor and PSI file paired with the presentation updates to run for that editor. */ private data class Presentation( val file: PsiFile, val editor: Editor, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt index e48006d120b..0e536f9565a 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurable.kt @@ -19,8 +19,10 @@ import javax.swing.JComponent import javax.swing.JPanel import javax.swing.JTextArea +/** Edits application defaults for new projects and projects that inherit language-service settings. */ class LanguageServiceApplicationConfigurable : LanguageServiceConfigurable(null) +/** Edits project language-service overrides, with an explicit option to inherit application defaults. */ class LanguageServiceProjectConfigurable( project: Project, ) : LanguageServiceConfigurable(project) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt index b69c8bb09d3..02e7b154e28 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/LibraryConfiguration.kt @@ -8,11 +8,13 @@ import java.net.URI import java.nio.file.Files import java.nio.file.Path +/** Binary and source-library settings; a null module path inherits the Gradle model. */ internal data class LibraryOptions( val modulePath: List? = null, val sourceAttachments: List = emptyList(), ) +/** Ordered source roots attached to a binary module for source navigation. */ internal data class LibraryAttachment( val module: String, val roots: List, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt index 99e956c994f..b8502804f28 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/PreflightedRenames.kt @@ -9,6 +9,7 @@ import java.util.concurrent.CompletableFuture /** Connection-local, lexically scoped proof for the synchronous VFS portion of a guarded edit. */ internal class PreflightedRenames { + /** Source and destination identifying a move already validated by the refactoring transaction. */ private data class Move( val from: Path, val to: Path, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt index 71d756a80aa..5a4271a00b7 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerJvmOptions.kt @@ -10,13 +10,9 @@ internal object ServerJvmOptions { options.map { option -> val size = memory.matchEntire(option) when { - size != null -> { - size.groupValues[1] - } + size != null -> size.groupValues[1] - collector.matches(option) -> { - "collector" - } + collector.matches(option) -> "collector" Regex("-XX:ActiveProcessorCount=[1-9][0-9]*").matches(option) -> { require(option.substringAfter('=').toIntOrNull() in 1..1024) { "ActiveProcessorCount must be between 1 and 1024" } diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt index 96beaa11602..743f16b3914 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerRuntimeSettings.kt @@ -12,6 +12,7 @@ import com.intellij.util.xmlb.annotations.OptionTag @Service(Service.Level.APP) @State(name = "EcstasyServerRuntime", storages = [Storage(value = "ecstasy-server-runtime.xml", roamingType = RoamingType.DISABLED)]) internal class ServerRuntimeSettings : SerializablePersistentStateComponent(Options()) { + /** Persisted JVM and log-retention settings used when launching the language server. */ data class Options( // Explicit bindings preserve immutable fields in IntelliJ's XML serializer. @field:OptionTag @JvmField val vmOptions: String = "", diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt index 61e050051a3..98fd241160d 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerSupportLogs.kt @@ -21,6 +21,7 @@ import java.util.zip.ZipOutputStream @Service(Service.Level.PROJECT) @State(name = "EcstasySupportLogs", storages = [Storage(StoragePathMacros.WORKSPACE_FILE)]) internal class ServerSupportLogs : SerializablePersistentStateComponent(Session()) { + /** Persisted identity and log locations of a server launch, including sessions from earlier IDE runs. */ data class Session( @field:OptionTag @JvmField val id: String = "", @field:OptionTag @JvmField val started: String = "", diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt index 8c63928f209..4dade939d8e 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEdit.kt @@ -20,12 +20,14 @@ internal class ServerWorkspaceEdit private val label: String, private val targets: List, ) { + /** Text replacement expressed in offsets of the validated document snapshot. */ private data class Replacement( val start: Int, val end: Int, val text: String, ) + /** Document, file and synchronization snapshot checked again before applying the server edit. */ private data class Target( val uri: String, val version: Int?, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt index 2be8d31dde2..d6328e544af 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphConfiguration.kt @@ -138,6 +138,7 @@ internal object SourceGraphConfiguration { values.asJsonArray.map(::stringValue) } + /** Normalized module configuration used to compare source graphs independently of input ordering. */ private data class CanonicalModule( val name: String, val uri: URI, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt index 796fd5286a4..66b919aa53e 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/SourceGraphEdit.kt @@ -57,6 +57,7 @@ internal class SourceGraphEdit throw UnexpectedUndoException(failure.message) } + /** Captured project, server and settings content that must still match when a graph edit is applied. */ class Snapshot( val project: Project, val serverId: String, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt index 57d7bd960f0..221430d7ca6 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileMoveHandler.kt @@ -61,6 +61,7 @@ class XtcFileMoveHandler : MoveHandlerDelegate() { else -> null } + /** Collects the destination and optional new name before requesting a compiler-validated move. */ private class MoveDialog( project: Project, private val files: List, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt index 02ba56b21c1..2a890682a38 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileOperations.kt @@ -51,21 +51,10 @@ internal object XtcFileOperations { if (project.isDisposed || future.isCancelled) return@invokeLater val failure = when { - error != null -> { - "$title failed: ${error.cause?.message ?: error.message}" - } - - edit == null -> { - "The compiler cannot safely apply this operation. No changes were made." - } - - !edit.apply() -> { - "Sources or paths changed; invoke the operation again." - } - - else -> { - null - } + error != null -> "$title failed: ${error.cause?.message ?: error.message}" + edit == null -> "The compiler cannot safely apply this operation. No changes were made." + !edit.apply() -> "Sources or paths changed; invoke the operation again." + else -> null } if (failure == null) { completed() diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt index dbc735b3d3f..a08f5d2cd57 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFileRenameHandler.kt @@ -59,6 +59,7 @@ class XtcFileRenameHandler : } } + /** Collects a source-file name before requesting and applying its compiler-validated rename. */ private class RenameDialog( project: Project, private val file: VirtualFile, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt index be94f2ae8dd..cb9b36758bc 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcFormattingService.kt @@ -84,13 +84,9 @@ class XtcFormattingService : LSPFormattingAndRangeBothService() { val snapshot = EditorFactory.getInstance().createDocument(original) val formatted = LSPIJUtils.applyEdits(snapshot, edits ?: emptyList()) when { - formatted == original -> { - request.onTextReady(null) - } + formatted == original -> request.onTextReady(null) - synchronous -> { - request.onTextReady(formatted) - } + synchronous -> request.onTextReady(formatted) else -> { // TODO LSP4IJ: UP24 — IDEA's asynchronous formatting application is diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt index 865615d8983..81c7ea192ae 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcLanguageServer.kt @@ -25,16 +25,19 @@ interface XtcLanguageServer : LanguageServer { fun renameFilesProposal(params: RenameFilesParams): CompletableFuture } +/** Server rename edit paired with any source-graph change that must be applied in the same transaction. */ data class RenameProposal( val edit: WorkspaceEdit, val graph: SourceGraphReplacement? = null, ) +/** Expected and proposed source graphs for a guarded configuration replacement. */ data class SourceGraphReplacement( val before: List, val after: List, ) +/** Wire representation of a source module, excluding server-only compiler and filesystem state. */ data class SourceModuleConfiguration( val name: String, val uri: String, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt index ce896b4a7cc..3aa02bd3b48 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameEdit.kt @@ -110,6 +110,7 @@ class XtcRenameEdit } } + /** Open buffer and synchronizer identity captured before requesting a rename. */ private data class Buffer( val opened: OpenedDocument, val synchronizer: DocumentContentSynchronizer, @@ -123,6 +124,7 @@ class XtcRenameEdit synchronizer.document.modificationStamp == stamp } + /** Source file, original modification stamp and proposed destination for a rename. */ private data class FileMove( val file: VirtualFile, val url: String, @@ -136,6 +138,7 @@ class XtcRenameEdit FileMoveTargets.valid(mapOf(Path.of(file.path) to target)) } + /** Connection, buffers and file moves that must remain current while the server proves the rename. */ private data class Snapshot( val wrapper: LanguageServerWrapper, val server: LanguageServer, diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt index e588f1e6b18..3a8d10bf862 100644 --- a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameHandler.kt @@ -94,9 +94,7 @@ class XtcRenameHandler : RenameHandler { .showErrorHint(editor, "This symbol cannot be renamed.") } - else -> { - RenameDialog(file, editor, offset, prepared).show() - } + else -> RenameDialog(file, editor, offset, prepared).show() } } } @@ -112,6 +110,7 @@ class XtcRenameHandler : RenameHandler { invoke(project, editor, file, dataContext) } + /** Server and original name returned by the prepare-rename step. */ private data class Prepared( val server: LanguageServerItem, val name: String, @@ -152,9 +151,7 @@ class XtcRenameHandler : RenameHandler { ReadAction.computeBlocking { val name = when { - result.isSecond -> { - result.second.placeholder - } + result.isSecond -> result.second.placeholder result.isFirst -> { LSPIJUtils @@ -171,15 +168,14 @@ class XtcRenameHandler : RenameHandler { )?.let(editor.document::getText) } - else -> { - null - } + else -> null } name?.let { Prepared(server, it) } } } } + /** Collects a symbol name while retaining the document stamp needed to reject a stale proposal. */ private class RenameDialog( private val file: PsiFile, private val editor: Editor, diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt index a74b4dc12e2..fe09f564311 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ClientCapabilitiesTest.kt @@ -14,6 +14,10 @@ import org.eclipse.lsp4j.SynchronizationCapabilities import org.eclipse.lsp4j.TextDocumentClientCapabilities import org.junit.jupiter.api.Test +/** + * Checks that the IntelliJ client advertises only capabilities its native integration can honor. Guards + * snippet indentation, save-hook limitations and deferred resolution against misleading negotiation. + */ class ClientCapabilitiesTest { @Test fun `advertise the indentation mode implemented by native snippet expansion`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt index 2e2158e72ec..76dc51b268b 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CodeActionMessagesTest.kt @@ -9,6 +9,10 @@ import org.eclipse.lsp4j.jsonrpc.messages.Either import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage import org.junit.jupiter.api.Test +/** + * Checks the compatibility bridge for deferred code actions. Listing an action must retain its opaque server + * data without resolving or applying its edit before the user selects it. + */ class CodeActionMessagesTest { @Test fun `lazy selection carries original action without resolving or applying while listing`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt index 82be4394ae1..a949e6da917 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildModelTest.kt @@ -5,6 +5,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Validates imported Gradle compiler inputs, including resource precedence and composite-build ownership. + * Malformed or ambiguous models must be refused before accepted project configuration changes. + */ class CompilerBuildModelTest { private val entry = mapOf( diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt index 131261ddcce..37cbe08eb66 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerBuildUpdatesTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import java.nio.file.Path +/** + * Verifies that compiler-model watching recognizes atomic report replacement and deletion while ignoring + * unrelated paths. Protects automatic refresh from missed updates and unnecessary reimports. + */ class CompilerBuildUpdatesTest { @Test fun `atomic report replacement and removal affect inputs but unrelated paths do not`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt index ca12d5a0679..e4a590b802c 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportServiceTest.kt @@ -13,6 +13,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Exercises project and build-root ownership of compiler imports. Disposed or unlinked owners must cancel + * their producers and reject late work rather than resurrecting stale inputs. + */ class CompilerImportServiceTest { /** Cancellation ownership is tested without installing an application-wide ProgressManager. */ private class Indicator : EmptyProgressIndicatorBase(ModalityState.nonModal()) { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt index 18e69b8e35b..dfcde6f8359 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerImportTest.kt @@ -9,6 +9,10 @@ import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicReference +/** + * Exercises the import state machine independently of Gradle timing. Cancellation, failed builds and + * superseded completions must preserve the last accepted compiler model. + */ class CompilerImportTest { private val disk = AtomicReference("original") private val imports = CompilerImport(disk::get) { require(it != "invalid") { "Invalid model" } } diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt index 82572662ecd..9411a7fc238 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/CompilerRootWatchesTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.api.Test import java.nio.file.Path import java.util.concurrent.CompletableFuture +/** + * Checks shared watch leases, wire registration decoding and exact cleanup. Prevents root replacement or + * disconnect from leaking watches or subscribing to unintended global paths. + */ class CompilerRootWatchesTest { @Test fun `watch leases share roots replace settings and close exactly once`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt index f33b738a12c..fa94ac778a2 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ConfigurationJsonTest.kt @@ -9,6 +9,10 @@ import org.eclipse.lsp4j.jsonrpc.json.MessageJsonHandler import org.eclipse.lsp4j.jsonrpc.messages.NotificationMessage import org.junit.jupiter.api.Test +/** + * Checks wire serialization of configuration resets. Explicit discovery reset must survive JSON encoding + * without changing normal omission of unrelated optional protocol fields. + */ class ConfigurationJsonTest { @Test fun `configuration notification preserves explicit discovery reset on the wire`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt index b369cdc5f7f..4ba42152d71 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticQuickFixesTest.kt @@ -19,6 +19,10 @@ import org.eclipse.lsp4j.jsonrpc.messages.RequestMessage import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage import org.junit.jupiter.api.Test +/** + * Protects diagnostic quick-fix refresh when visible error text stays unchanged. Checks that presentation + * revisions refresh lazy actions while original opaque server data survives the request round trip. + */ class DiagnosticQuickFixesTest { @Test fun `presentation preserves decoded full and unchanged companion reports`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt index 1e205f4380b..9d3bb4eda60 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticReportJsonTest.kt @@ -12,6 +12,10 @@ import org.eclipse.lsp4j.jsonrpc.services.ServiceEndpoints import org.eclipse.lsp4j.services.TextDocumentService import org.junit.jupiter.api.Test +/** + * Exercises full, unchanged and related pull-diagnostic report decoding with the client JSON configuration. A + * valid report shape must not terminate the message reader or lose related-document updates. + */ class DiagnosticReportJsonTest { @Test fun `related full and unchanged reports decode without killing the message reader`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt index aad9d2ce3a9..bc46121f3e4 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DiagnosticResultMessagesTest.kt @@ -17,6 +17,10 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage import org.junit.jupiter.api.Test import java.util.concurrent.atomic.AtomicReference +/** + * Forces diagnostic reply, edit and close races around snapshot lookup. Ensures a retired result ID cannot + * re-enter the cache or authorize reuse against a newer document revision. + */ class DiagnosticResultMessagesTest { @Test fun `close or cancellation during reply snapshot lookup cannot reinsert a retired result`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt index 015937d4f69..877355bb0c3 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DirectoryDocumentMovesTest.kt @@ -7,6 +7,10 @@ import com.intellij.testFramework.LightVirtualFile import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +/** + * Checks the LSP4IJ directory-move bridge and its pinned client API. Open descendants must reconnect once at + * their new URIs while unrelated documents and ordinary file operations retain their normal lifecycle. + */ class DirectoryDocumentMovesTest { @Test fun `pinned LSP4IJ exposes the owned disconnect operation required by the directory move bridge`() { @@ -54,6 +58,7 @@ class DirectoryDocumentMovesTest { assertThat(DirectoryDocumentMoves.descendants(events) { error("Handled by LSP4IJ") }).isEmpty() } + /** Supplies an in-memory file's parent and directory status without creating a real IDE filesystem. */ private class File( name: String, private val folder: VirtualFile? = null, diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt index d6658a2c84f..6686b03464d 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/DocumentStartupMessagesTest.kt @@ -21,6 +21,10 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage import org.junit.jupiter.api.Test import java.net.URI +/** + * Exercises open/change ordering and transmitted document versions across typing, restart and reopen. + * Protects server-edit validation from stale ownership, reused versions and incorrect UTF-16 patching. + */ class DocumentStartupMessagesTest { private val uri = "file:///Startup.x" private val buffers = diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt index 658ac29e789..1ce4a68ebf0 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/FileMoveTargetsTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import java.nio.file.Files import java.nio.file.Path +/** + * Validates move destinations before any filesystem writes and again after asynchronous compiler proof. + * Covers collisions, missing parents and ambiguous batches so a stale proposal cannot overwrite new files. + */ class FileMoveTargetsTest { @TempDir lateinit var root: Path diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt index a1527398185..445a3dc9a5e 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LanguageServiceConfigurationTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Checks inheritance, validation and concurrent updates of language-service settings. Applying a stale + * settings page must not replace a newer service preference or a source graph changed by refactoring. + */ class LanguageServiceConfigurationTest { @Test fun `adapter selection preserves inherited preferences and compiler paths`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt index f314ad7934c..97d513c6bb9 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/LibraryConfigurationTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.api.io.TempDir import java.nio.file.Files import java.nio.file.Path +/** + * Checks persistence of ordered module paths and attached sources without replacing unrelated settings. + * Invalid, aliased or unsupported paths must fail before publication. + */ class LibraryConfigurationTest { @TempDir lateinit var directory: Path diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt index a762347c21b..cffd4bb4f5f 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/PreflightedRenamesTest.kt @@ -14,6 +14,10 @@ import java.nio.file.Path import java.util.concurrent.CompletableFuture import java.util.concurrent.Executors +/** + * Exercises the real LSP proxy boundary used during an already-proven native rename. Verifies scoped + * ownership survives synchronous dispatch and no-op follow-up requests do not block on the compiler again. + */ class PreflightedRenamesTest { private val guard = PreflightedRenames() private val pending = CompletableFuture() diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt index 6da90463917..368d88e269a 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerJvmOptionsTest.kt @@ -4,6 +4,11 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Validates server launch options and retention limits before publishing machine settings. Rejects launch + * overrides, contradictory arguments and impossible heap policies while preserving each accepted option as + * one argument. + */ class ServerJvmOptionsTest { @Test fun `supported options remain separate arguments`() { val options = listOf("-Xms256M", "-Xmx2G", "-Xss1M", "-XX:ActiveProcessorCount=4", "-XX:+UseG1GC") diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt index 34ea57d6041..351f8892e09 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSettingsPersistenceTest.kt @@ -4,6 +4,10 @@ import com.intellij.util.xmlb.XmlSerializer import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +/** + * Round-trips runtime options and support-session metadata through IntelliJ XML serialization. Guards against + * settings that work in memory but disappear or change after an IDE restart. + */ class ServerSettingsPersistenceTest { @Test fun `runtime options survive IntelliJ XML serialization`() { val options = ServerRuntimeSettings.Options("-Xmx768M", 2, 1, 3, 2) diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt index 16eb8583fff..e715a7c5a82 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerSupportLogsTest.kt @@ -7,6 +7,11 @@ import java.nio.file.Files import java.nio.file.Path import java.util.zip.ZipFile +/** + * Checks ownership and bounds of offline support archives, including failed launches. Late callbacks and + * foreign sessions must not replace the current project's recorded server, and symlinks must not expand the + * archive scope. + */ class ServerSupportLogsTest { @TempDir lateinit var temporary: Path diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt index bb722c3b411..3b5ba50a06d 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerWorkspaceEditTest.kt @@ -8,6 +8,10 @@ import org.eclipse.lsp4j.TextEdit import org.eclipse.lsp4j.jsonrpc.messages.Either import org.junit.jupiter.api.Test +/** + * Compares supported platform and LSP4IJ edit representations. Malformed or unsupported alternatives must be + * refused as a whole rather than becoming a partial refactoring. + */ class ServerWorkspaceEditTest { @Test fun `platform and LSP4IJ edit representations preserve the same edits`() { diff --git a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt index 6911c5ddba9..dfbca58ee0e 100644 --- a/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt +++ b/lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/SourceGraphConfigurationTest.kt @@ -6,6 +6,10 @@ import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test import java.net.URI +/** + * Exercises source-graph validation, guarded replacement and Undo. Preserves resource order, explicit + * empty/discovered states and unrelated settings when asynchronous proposals meet newer configuration edits. + */ class SourceGraphConfigurationTest { @Test fun `resource roots preserve order empty automatic and rename ownership`() { diff --git a/lang/lsp-server/README.md b/lang/lsp-server/README.md index 51febced050..b01286043d8 100644 --- a/lang/lsp-server/README.md +++ b/lang/lsp-server/README.md @@ -21,9 +21,9 @@ This project provides the LSP server that powers IDE features like: The server is used by both the [IntelliJ plugin](../intellij-plugin/) and [VS Code extension](../vscode-extension/). -For the canonical cross-adapter feature matrix and current implementation status, see -[`../doc/plans/plan-ide-integration.md`](../doc/plans/plan-ide-integration.md). This -README focuses on LSP-server-specific build/runtime/configuration details. +The [backend comparison](#backend-comparison) and [supported features](#supported-lsp-features) +describe the adapter boundaries. For the architectural changes required in the compiler, start with +[the javatools guide](../../javatools/README.md#compiler-architecture-for-language-tooling). > **Note:** All `./gradlew :lang:*` commands below assume `-PincludeBuildLang=true -PincludeBuildAttachLang=true` are passed when running from the project root. See [Composite Build Properties](../../CLAUDE.md) in the project CLAUDE.md for details. @@ -122,7 +122,7 @@ Compiler mode has implementations for all 27 capabilities in this project's adap plus push/pull diagnostics and document/workspace synchronization. Several implementations remain bounded; the interface does not cover every LSP feature. Debug inline values, notebooks and broader refactorings remain incomplete. Color values have an opt-in, fixture-only constructor prototype; -a public Ecstasy color-library contract remains deferred. See the [explicit absent-feature inventory](../doc/plans/plan-ide-integration.md#compiler-completeness-snapshot) +a public Ecstasy color-library contract remains deferred. See the [feature boundaries below](#supported-lsp-features) and [acceptance reports](../doc/manual-test-plan.md#acceptance-reports). Capability coverage, semantic completeness and native test coverage are tracked separately. Compiler code actions include expression/local extraction, private helper extraction for contiguous @@ -204,7 +204,7 @@ explicit import aliases and member-file type/package moves also use compiler pro include existing companion directories and simple discovery-owned module roots, updating closed imports while retaining local package aliases. Versioned text edits precede file/directory moves. They require client resource-operation support and reject collisions and symlinks. Qualified modules, -implicit package directories and bounded cross-module ownership moves use the L62 graph proof. +implicit package directories and bounded cross-module ownership moves use compiler proof across the affected source graph. Explicit source-graph replacement is available through guarded native `xtc/rename` and `xtc/renameFiles` proposals with configuration persistence and Undo; generic LSP clients cannot apply that configuration change through a standard rename. The `construct` keyword stays unchanged. @@ -230,9 +230,8 @@ single-source inputs. Source roots use the normal file/same-name-directory modul ## Supported LSP Features -The canonical feature matrix lives in -[`../doc/plans/plan-ide-integration.md`](../doc/plans/plan-ide-integration.md#adapter-capability-matrix). -At a high level, the explicit Tree-sitter backend provides: +The [manual playbook](../doc/manual-test-plan.md) supplies examples and acceptance checks for these +features. At a high level, the explicit Tree-sitter backend provides: - document symbols, same-file navigation, workspace-symbol search, and best-effort cross-file navigation - context-aware completion @@ -307,7 +306,8 @@ enclosing compiler scope, including flat qualified names, without registering in Explicit cursor queries also recover missing operands inside compound call arguments and validate the complete proposed expression. Declaration/parameter names, missing map entries and unterminated literal contents remain outside this recovery. The -[capability matrix](../doc/plans/plan-ide-integration.md) records the remaining syntax/callable limits. +[manual completion checks](../doc/manual-test-plan.md#xdkadapter-playbook) exercise the supported +syntax and callable boundaries. Declaration-header recovery retains malformed method names and source extents for outline and folding. Compiler folds keep the heading line and a real closing brace visible; precise end columns @@ -738,8 +738,8 @@ where users can configure indent size, continuation indent, tab usage, and right settings are forwarded to the LSP server via `workspace/configuration` at startup and whenever the configuration changes. The server uses them as a fallback when no `xtc-format.toml` is present. -See [formatting-plan.md](../doc/plans/formatting-plan.md) for the full implementation plan -including the `xtc-format.toml` config file schema and the configuration architecture. +See the [formatting design reference](../doc/plans/formatting-plan.md#configuration-architecture) +for the `xtc-format.toml` schema and configuration precedence. ## Known Issues: IntelliJ Platform and LSP4IJ @@ -795,8 +795,8 @@ in slow operation reports: ## Documentation - [Tree-sitter Feature Matrix](../tree-sitter/doc/functionality.md) - What Tree-sitter can/cannot do -- [Tree-sitter Integration Plan](../doc/plans/plan-tree-sitter.md) - Full implementation details -- [Formatting Plan](../doc/plans/formatting-plan.md) - On-type formatting design, configuration architecture, industry survey +- [Compiler architecture](../../javatools/README.md#compiler-architecture-for-language-tooling) - Embedding, diagnostics and source-analysis contracts +- [Manual playbook](../doc/manual-test-plan.md) - Feature examples, native tests and client limitations The server uses UTF-16 positions and adapts hover, outline, symbol @@ -831,7 +831,7 @@ URIs include the binary and source-archive revisions; registered content comes f bundle, never from an arbitrary path or an editor overlay. Compiler input replacement refreshes fetched views; immutable revision URIs keep their original text. Closing the server retires its content registry. Line-ending normalization preserves declaration positions; other client text -rewrites withhold monikers until matching presentation is restored. Clients without this capability retain protected file views (LSP4IJ UP25). +rewrites withhold monikers until matching presentation is restored. Clients without this capability retain protected file views (LSP4IJ). External host source indexes remain host-owned file locations: they supply declaration ranges, not verified source text, and are not silently promoted to virtual library snapshots. Selected X31/X252/X253 pass in both editors; full-catalog/release acceptance remains separate. @@ -846,7 +846,7 @@ placeholders, mid-token edits and read-only libraries are excluded. Cancellation versions retire pending suggestions. No new embedding/AST API or generative service is involved. Both editor clients have native providers. IntelliJ's direct invocation currently sends Automatic -and omits popup selection (UP26); shared X255–X258 distinguish native from protocol coverage. +and omits popup selection; shared X255–X258 distinguish native from protocol coverage. See the [acceptance report format](../doc/manual-test-plan.md#acceptance-reports) for how native and protocol coverage are recorded. diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt index 6d33da1f476..8891623c4d6 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterFormatter.kt @@ -360,13 +360,8 @@ class AdapterFormatter { if (prevTrimmed.endsWith("{}")) { val bodyIndent = when { - isContinuationLine(prevTrimmed) -> { - findDeclarationIndent(tree, prevLineIndex) + config.indentSize - } - - else -> { - prevIndent + config.indentSize - } + isContinuationLine(prevTrimmed) -> findDeclarationIndent(tree, prevLineIndex) + config.indentSize + else -> prevIndent + config.indentSize } val closingIndent = when { @@ -403,13 +398,8 @@ class AdapterFormatter { val currentIndent = currentLine.takeWhile { it == ' ' }.length val bodyIndent = when { - isContinuationLine(prevTrimmed) -> { - findDeclarationIndent(tree, prevLineIndex) + config.indentSize - } - - else -> { - prevIndent + config.indentSize - } + isContinuationLine(prevTrimmed) -> findDeclarationIndent(tree, prevLineIndex) + config.indentSize + else -> prevIndent + config.indentSize } val closingIndent = when { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt index a567b7021be..db9283574cb 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/AdapterModels.kt @@ -21,6 +21,7 @@ data class SymbolMoniker( val identifier: String, val kind: Kind, ) { + /** Relationship between a moniker and its provider: imported, exported or local to this document. */ enum class Kind { IMPORT, EXPORT, LOCAL } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt index 035c9b792d6..8bc4f2049a5 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/DocumentColor.kt @@ -12,6 +12,10 @@ data class ColorValue( } } +/** + * An actual color value and its source range, used for editor swatches and color presentations. Source-code + * highlighting uses semantic tokens instead. + */ data class DocumentColor( val range: Range, val color: ColorValue, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt index c5534db1668..733276db41b 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/treesitter/TreeSitterAdapter.kt @@ -624,9 +624,7 @@ class TreeSitterAdapter : AbstractAdapter() { "type_expression", "type_name", "generic_type", - -> { - return CompletionContext.TYPE - } + -> return CompletionContext.TYPE "annotation" -> { return CompletionContext.ANNOTATION @@ -1000,15 +998,11 @@ class TreeSitterAdapter : AbstractAdapter() { return when (parent.type) { "assignment_statement", "assignment_expression", - -> { - parent.childByFieldName("left")?.let { isOrContains(it, node) } == true - } + -> parent.childByFieldName("left")?.let { isOrContains(it, node) } == true "variable_declaration", "parameter", - -> { - parent.childByFieldName("name")?.let { isSameNode(it, node) } == true - } + -> parent.childByFieldName("name")?.let { isSameNode(it, node) } == true else -> { false @@ -1131,9 +1125,7 @@ class TreeSitterAdapter : AbstractAdapter() { "line_comment", "block_comment", "doc_comment", - -> { - FoldingRange.FoldingKind.COMMENT - } + -> FoldingRange.FoldingKind.COMMENT "import_list" -> { FoldingRange.FoldingKind.IMPORTS @@ -1677,17 +1669,9 @@ class TreeSitterAdapter : AbstractAdapter() { buildList { val message = when { - node.isError -> { - "Syntax error: unexpected '${node.text.take(20)}${if (node.text.length > 20) "..." else ""}'" - } - - node.isMissing -> { - "Syntax error: missing ${node.type}" - } - - else -> { - null - } + node.isError -> "Syntax error: unexpected '${node.text.take(20)}${if (node.text.length > 20) "..." else ""}'" + node.isMissing -> "Syntax error: missing ${node.type}" + else -> null } if (message != null) { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt index 032c81467dc..d3aa1146ee6 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerAnnotationIdentity.kt @@ -35,49 +35,27 @@ internal fun annotationIdentity( fun bytes(value: ByteArray) = scalar(HexFormat.of().formatHex(value)) return when (value) { - is TypeConstant -> { - receiverIdentity(value, identity, depth + 1) - } + is TypeConstant -> receiverIdentity(value, identity, depth + 1) - is SingletonConstant -> { - identity(value.classConstant) - } + is SingletonConstant -> identity(value.classConstant) - is ByteConstant, is CharConstant, is IntConstant, is StringConstant, is LiteralConstant -> { - scalar(value.valueString) - } + is ByteConstant, is CharConstant, is IntConstant, is StringConstant, is LiteralConstant -> scalar(value.valueString) - is FloatConstant -> { - scalar(value.value.toRawBits().toString()) - } + is FloatConstant -> scalar(value.value.toRawBits().toString()) - is Float64Constant -> { - scalar(value.value.toRawBits().toString()) - } + is Float64Constant -> scalar(value.value.toRawBits().toString()) - is Float128Constant -> { - bytes(value.value) - } + is Float128Constant -> bytes(value.value) - is FPNConstant -> { - bytes(value.value) - } + is FPNConstant -> bytes(value.value) - is DecimalConstant -> { - bytes(value.value.toByteArray()) - } + is DecimalConstant -> bytes(value.value.toByteArray()) - is DecimalAutoConstant -> { - bytes(value.value.toByteArray()) - } + is DecimalAutoConstant -> bytes(value.value.toByteArray()) - is UInt8ArrayConstant -> { - bytes(value.value) - } + is UInt8ArrayConstant -> bytes(value.value) - is RegExConstant -> { - scalar("${value.flags}:${value.value}") - } + is RegExConstant -> scalar("${value.flags}:${value.value}") is ArrayConstant -> { ProofIdentity.TypeShape( @@ -108,8 +86,6 @@ internal fun annotationIdentity( } // Runtime handles, deferred computations and filesystem objects need their own contracts. - else -> { - null - } + else -> null } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt index bbd9027ac59..7a39970e074 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerDispatch.kt @@ -24,6 +24,7 @@ internal data class CompilerDispatch( val branches: List, ) + /** A possible receiver type paired with its compiler-selected dispatch chain. */ data class Branch( val receiver: TypeConstant, val dispatch: CompilerDispatch, @@ -139,9 +140,7 @@ internal fun TypeInfo.dispatch( CompilerDispatch(listOf(body.identity), supported = false) } - else -> { - CompilerDispatch(listOf(body.identity), supported = false) - } + else -> CompilerDispatch(listOf(body.identity), supported = false) } } return CompilerDispatch( @@ -159,9 +158,7 @@ private fun MethodInfo.closingContracts(depth: Int = 0): List? { return chain .flatMap { body -> when (body.implementation) { - Implementation.Delegating -> { - emptyList() - } + Implementation.Delegating -> emptyList() Implementation.Union -> { val left = body.unionLeft.closingContracts(depth + 1) ?: return null @@ -171,13 +168,9 @@ private fun MethodInfo.closingContracts(depth: Int = 0): List? { Implementation.Declared, Implementation.Abstract, Implementation.Default, Implementation.Explicit, Implementation.SansCode, - -> { - listOf(body.methodStructure?.takeUnless { it.isSynthetic }?.identityConstant ?: return null) - } + -> listOf(body.methodStructure?.takeUnless { it.isSynthetic }?.identityConstant ?: return null) - else -> { - return null - } + else -> return null } }.distinct() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt index ebf2c0cbc93..3606ab6c736 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImplementations.kt @@ -132,9 +132,7 @@ internal fun TypeInfo.methodImplementation( return when (body.implementation) { Implementation.Explicit, Implementation.Default, - -> { - body.methodStructure?.takeUnless { it.isSynthetic || it.isNative || it.isAbstract }?.identityConstant - } + -> body.methodStructure?.takeUnless { it.isSynthetic || it.isNative || it.isAbstract }?.identityConstant Implementation.Delegating -> { val delegate = delegateType(body.propertyConstant ?: return null, errors) ?: return null @@ -142,17 +140,11 @@ internal fun TypeInfo.methodImplementation( delegate.methodImplementation(selected, errors, visited + key) } - Implementation.FromInto -> { - body.intoMethodInfo?.let { methodImplementation(it, errors, visited + key) } - } + Implementation.FromInto -> body.intoMethodInfo?.let { methodImplementation(it, errors, visited + key) } - Implementation.Capped -> { - getNarrowingMethod(method)?.let { methodImplementation(it, errors, visited + key) } - } + Implementation.Capped -> getNarrowingMethod(method)?.let { methodImplementation(it, errors, visited + key) } - else -> { - null - } + else -> null } } @@ -210,6 +202,7 @@ private fun TypeInfo.accessorImplementation( ?.identity ?: property.sourceField() } +/** Written property accessor and the implementation that contributed it to dispatch. */ private data class WrittenAccessor( val implementation: Implementation, val identity: IdentityConstant, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt index 4f10ef1002b..ba8e2239db1 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerImportAliases.kt @@ -49,17 +49,9 @@ internal fun compilerImportAliases( .mapNotNull { node -> val first = when (node) { - is NameExpression -> { - node.nameToken.takeIf { node.leftExpression == null } - } - - is NamedTypeExpression -> { - node.nameBindings.firstOrNull()?.name() - } - - else -> { - null - } + is NameExpression -> node.nameToken.takeIf { node.leftExpression == null } + is NamedTypeExpression -> node.nameBindings.firstOrNull()?.name() + else -> null } ?: return@mapNotNull null if (first.valueText != alias.aliasName) return@mapNotNull null val at = range(first.startPosition, first.endPosition) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt index 746049f8732..a6a7871af20 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt @@ -72,9 +72,7 @@ internal fun compilerMemberActions( } } - else -> { - null - } + else -> null } ?: return@mapNotNull null parameter.name to if (value is UnaryMinusExpression) "-$token" else token.toString() diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt index 6c32b311a14..67df2421044 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberSignature.kt @@ -58,41 +58,28 @@ internal fun memberSignature( val rendered = render(type) ?: return null val default = if (parameter.hasDefaultValue()) { - // Validated primitive constants are independent of their original - // expression/scope. - // Fresh declaration repair accepts only parser-owned literal tokens; the - // complete - // proposed compilation must validate both the original and generated defaults. + // Validated primitive constants are independent of their original expression/scope. + // Fresh declaration repair accepts only parser-owned literal tokens; the proposed + // compilation must validate both the original and generated defaults. val value = when (val constant = parameter.defaultValue) { - is StringConstant -> { - constant.valueString - } + is StringConstant -> constant.valueString - is CharConstant -> { - constant.valueString - } + is CharConstant -> constant.valueString - is IntConstant -> { - constant.valueString - } + is IntConstant -> constant.valueString - is SingletonConstant -> { + is SingletonConstant -> when (constant) { constant.constantPool.valTrue() -> "ecstasy.Boolean.True" constant.constantPool.valFalse() -> "ecstasy.Boolean.False" constant.constantPool.valNull() -> "ecstasy.Nullable.Null" else -> return null } - } - null -> { - literalDefaults[name] ?: return null - } + null -> literalDefaults[name] ?: return null - else -> { - return null - } + else -> return null } " = $value" } else { @@ -126,9 +113,7 @@ internal fun TypeConstant.memberSourceType( ): String? { return when (this) { // A recursive alias is a TerminalTypeConstant without a single defining constant. - is RecursiveTypeConstant -> { - null - } + is RecursiveTypeConstant -> null is ParameterizedTypeConstant -> { val base = underlyingType.memberSourceType(owner, formals, modules) ?: return null @@ -139,9 +124,7 @@ internal fun TypeConstant.memberSourceType( arguments.joinToString(", ", "$base<", ">") } - is ImmutableTypeConstant -> { - underlyingType.memberSourceType(owner, formals, modules)?.let { "immutable $it" } - } + is ImmutableTypeConstant -> underlyingType.memberSourceType(owner, formals, modules)?.let { "immutable $it" } is RelationalTypeConstant -> { val operator = @@ -158,9 +141,7 @@ internal fun TypeConstant.memberSourceType( is TerminalTypeConstant -> { when (val identity = definingConstant) { - is TypeParameterConstant -> { - formals[identity] - } + is TypeParameterConstant -> formals[identity] is PropertyConstant -> { // Class formals are properties, not method type parameters. Only the exact @@ -174,13 +155,9 @@ internal fun TypeConstant.memberSourceType( is ClassConstant -> { when { - constantPool.getImplicitlyImportedIdentity(identity.name) == identity -> { - identity.name - } + constantPool.getImplicitlyImportedIdentity(identity.name) == identity -> identity.name - identity.moduleConstant == owner.moduleConstant -> { - identity.pathString - } + identity.moduleConstant == owner.moduleConstant -> identity.pathString XdkAutoImports.target(identity) != null -> { modules[identity.moduleConstant.name]?.let { @@ -188,48 +165,25 @@ internal fun TypeConstant.memberSourceType( } } - else -> { - null - } + else -> null } } - else -> { - null - } + else -> null } } - else -> { - null - } + else -> null } } /** Inspect only type structures whose source spelling the renderer understands. */ internal fun TypeConstant.memberClasses(): List = when (this) { - is RecursiveTypeConstant -> { - emptyList() - } - - is ParameterizedTypeConstant -> { - underlyingType.memberClasses() + paramTypes.flatMap { it.memberClasses() } - } - - is RelationalTypeConstant -> { - underlyingType.memberClasses() + underlyingType2.memberClasses() - } - - is ImmutableTypeConstant -> { - underlyingType.memberClasses() - } - - is TerminalTypeConstant -> { - listOfNotNull(definingConstant as? ClassConstant) - } - - else -> { - emptyList() - } + is RecursiveTypeConstant -> emptyList() + is ParameterizedTypeConstant -> underlyingType.memberClasses() + paramTypes.flatMap { it.memberClasses() } + is RelationalTypeConstant -> underlyingType.memberClasses() + underlyingType2.memberClasses() + is ImmutableTypeConstant -> underlyingType.memberClasses() + is TerminalTypeConstant -> listOfNotNull(definingConstant as? ClassConstant) + else -> emptyList() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt index 0841f756cdc..dbc9064d3b7 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt @@ -13,6 +13,10 @@ internal class CompilerMethodRelations( val declarations: Set, val chains: List, ) { + /** + * Ordered methods contributed to an owner, including cycle and alternative-dispatch evidence needed for + * refactoring checks. + */ data class Chain( val owner: IdentityConstant, val methods: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt index 462dcee58d8..8055740eac6 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt @@ -45,10 +45,12 @@ internal data class CompilerMissingMethod( ) { /** Fresh declaration types and already detached body types never share compiler pools. */ sealed interface Type { + /** Attempt-owned compiler type used while deriving a missing method signature. */ data class Resolved( val value: TypeConstant, ) : Type + /** Type proof that remains usable after the compilation attempt is released. */ data class Detached( val value: ProofIdentity, ) : Type @@ -61,6 +63,7 @@ internal data class CompilerMissingInputs( val receivers: Map = emptyMap(), val expressions: Map = emptyMap(), ) { + /** Compiler type and source context needed to spell it in each possible destination. */ data class LocalType( val source: String?, val type: TypeConstant, @@ -68,12 +71,14 @@ internal data class CompilerMissingInputs( ) } +/** Source spelling, detached type proof and imports required for a generated type reference. */ private data class MissingType( val source: String, val proof: CompilerMissingMethod.Type, val imports: List = emptyList(), ) +/** Proposed parameter spelling and type, with its argument binding and required imports. */ private data class MissingParameter( val source: String, val type: CompilerMissingMethod.Type, @@ -204,9 +209,7 @@ internal fun compilerMissingMethods( } ?: local.takeIf { it.hasExplicitMethodType() }?.type?.let(::render) val returns = when (val statement = call.parent) { - is ExpressionStatement -> { - if (statement.expression === call) emptyList() else return@mapNotNull null - } + is ExpressionStatement -> if (statement.expression === call) emptyList() else return@mapNotNull null is ReturnStatement -> { if (statement.expressions?.singleOrNull() !== call) return@mapNotNull null @@ -221,9 +224,7 @@ internal fun compilerMissingMethods( listOf(localType(local) ?: return@mapNotNull null) } - else -> { - return@mapNotNull null - } + else -> return@mapNotNull null } val conditional = declaration.isConditionalReturn && call.parent is ReturnStatement diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt index af0043f4b61..fbd87c64401 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt @@ -16,6 +16,7 @@ internal class CompilerPropertyRelations( val declarations: Set = emptySet(), val chains: List = emptyList(), ) { + /** Ordered property contributions for an owner, retaining whether their equivalence can be proved. */ data class Chain( val owner: IdentityConstant, val properties: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt index 699e99e29df..5353fe2ca88 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerQueueTrace.kt @@ -8,12 +8,14 @@ import java.util.concurrent.atomic.AtomicLong /** Observes the existing worker; no compiler work or future completion runs under this lock. */ internal class CompilerQueueTrace { + /** Scheduling phase reported for a compiler request before and during execution. */ private enum class Phase { DEBOUNCING, QUEUED, RUNNING, } + /** Trace span, submission time and current scheduling phase for one request. */ private data class Job( val span: ExecutionTrace.Span, val submission: Long, @@ -35,6 +37,7 @@ internal class CompilerQueueTrace { action: () -> Unit, ): Work = Work(kind, uri, result, action) + /** Runs a queued operation inside its trace span and completes its associated result future. */ inner class Work internal constructor( kind: String, @@ -161,21 +164,13 @@ internal class CompilerQueueTrace { fun Job.label(): String { val operation = when (span.operation) { - "compile" -> { - "compiling" - } + "compile" -> "compiling" - "project-REFERENCES" -> { - "finding references in" - } + "project-REFERENCES" -> "finding references in" - "project-DIAGNOSTICS" -> { - "checking diagnostics in" - } + "project-DIAGNOSTICS" -> "checking diagnostics in" - "rename-proof", "project-RENAME", "project-RENAME_PROPOSAL", "project-FILE_RENAME" -> { - "checking rename in" - } + "rename-proof", "project-RENAME", "project-RENAME_PROPOSAL", "project-FILE_RENAME" -> "checking rename in" else -> { val kind = diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt index de4053f370e..c1ace33b778 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerReceiverIdentity.kt @@ -27,9 +27,7 @@ internal fun receiverIdentity( fun nested(type: TypeConstant) = receiverIdentity(type, identity, depth + 1) return when (type) { // Recursive types extend TerminalTypeConstant but have no single defining constant. - is RecursiveTypeConstant -> { - identity(type.typedef) - } + is RecursiveTypeConstant -> identity(type.typedef) is TerminalTypeConstant -> { when (val declaration = type.definingConstant) { @@ -53,9 +51,11 @@ internal fun receiverIdentity( ProofIdentity.TypeShape(type.format, listOf(base, annotation) + arguments) } - is ImmutableTypeConstant, is ServiceTypeConstant -> { - ProofIdentity.TypeShape(type.format, listOf(nested(type.underlyingType) ?: return null)) - } + is ImmutableTypeConstant, is ServiceTypeConstant -> + ProofIdentity.TypeShape( + type.format, + listOf(nested(type.underlyingType) ?: return null), + ) is VirtualChildTypeConstant -> { val parent = nested(type.parentType) ?: return null @@ -63,9 +63,11 @@ internal fun receiverIdentity( ProofIdentity.TypeShape(type.format, listOf(parent, identity(type.definingConstant)) + listOfNotNull(origin)) } - is InnerChildTypeConstant -> { - ProofIdentity.TypeShape(type.format, listOf(nested(type.parentType) ?: return null, identity(type.definingConstant))) - } + is InnerChildTypeConstant -> + ProofIdentity.TypeShape( + type.format, + listOf(nested(type.parentType) ?: return null, identity(type.definingConstant)), + ) is RelationalTypeConstant -> { val operands = listOf(nested(type.underlyingType) ?: return null, nested(type.underlyingType2) ?: return null) @@ -78,8 +80,6 @@ internal fun receiverIdentity( ProofIdentity.TypeShape(type.format, components) } - else -> { - null - } + else -> null } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt index 51caf6d3354..dd4fbcb8f00 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerRenameFacts.kt @@ -17,12 +17,14 @@ import java.util.UUID /** Compiler-proven identities copied before an attempt's pool and AST can be released. */ internal sealed interface ProofIdentity { + /** Declaration identity detached from an AST and constant pool, anchored to its source location. */ data class Source( val location: SemanticModel.SourceLocation, val format: Constant.Format, val name: String, ) : ProofIdentity + /** Declaration identity anchored to a particular immutable dependency artifact revision. */ data class Binary( val key: XdkDependency.SymbolKey, ) : ProofIdentity @@ -33,6 +35,7 @@ internal sealed interface ProofIdentity { val signature: XdkDependency.SymbolKey, ) : ProofIdentity + /** Value parameter identified by its owning method and parameter slot. */ data class Parameter( val method: ProofIdentity, val index: Int, @@ -97,15 +100,21 @@ internal sealed interface ProofIdentity { ) : ProofIdentity } +/** Attempt-owned receiver identity and compiler register used to establish dispatch. */ internal data class CompilerReceiver( val owner: IdentityConstant, val register: Int, ) +/** + * Detached declarations and contribution chains used to compare bindings across compilation attempts. + * Contains no compiler constants, so retaining proof data does not retain an attempt's constant pool. + */ internal class ProofRelations( val declarations: Set = emptySet(), val chains: List = emptyList(), ) { + /** Owner and ordered member proofs, including unsupported, cyclic and alternative contributions. */ data class Chain( val owner: ProofIdentity, val members: List, @@ -242,15 +251,16 @@ internal fun captureRenameFacts( (host in declarations || dependencies?.declarations?.containsKey(host) == true) val packageParent = (constant as? PackageConstant)?.let { identity(it.parentConstant) } when { - constant is TypeParameterConstant -> { - ProofIdentity.MethodFormal(identity(constant.method), constant.register) - } + constant is TypeParameterConstant -> ProofIdentity.MethodFormal(identity(constant.method), constant.register) // Bundled source navigation must use the same artifact identity as binary-only // views. - location != null && module !in XdkLibraries.moduleNames -> { - ProofIdentity.Source(location, constant.format, requireNotNull(value).name) - } + location != null && module !in XdkLibraries.moduleNames -> + ProofIdentity.Source( + location, + constant.format, + requireNotNull(value).name, + ) constant is PackageConstant && module !in XdkLibraries.moduleNames && @@ -266,9 +276,7 @@ internal fun captureRenameFacts( } } - is ProofIdentity.Directory -> { - File(packageParent.path) - } + is ProofIdentity.Directory -> File(packageParent.path) } directory?.let { ProofIdentity.Directory(File(it, constant.name).path) } ?: ProofIdentity.Unproven() @@ -278,9 +286,7 @@ internal fun captureRenameFacts( sourceHost && (constant.component as? MethodStructure)?.let { it.isSynthetic && it.isShorthandConstructor - } == true -> { - ProofIdentity.PrimaryConstructor(identity(host)) - } + } == true -> ProofIdentity.PrimaryConstructor(identity(host)) constant is MethodConstant && module !in XdkLibraries.moduleNames && @@ -348,9 +354,7 @@ internal fun captureRenameFacts( } } - else -> { - ProofIdentity.Unproven() - } + else -> ProofIdentity.Unproven() } } @@ -374,9 +378,7 @@ internal fun captureRenameFacts( } } - else -> { - ProofIdentity.Unproven() - } + else -> ProofIdentity.Unproven() } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt index 0f5525bcbe7..3c5bd91a6c7 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerResourceValues.kt @@ -47,13 +47,9 @@ private fun resourceValue(value: Constant?): String? { fun append(value: Constant?): Boolean { text(value?.format?.name ?: return false) when (value) { - is StringConstant -> { - text(value.value) - } + is StringConstant -> text(value.value) - is UInt8ArrayConstant -> { - bytes(value.value) - } + is UInt8ArrayConstant -> bytes(value.value) is FileStoreConstant -> { text(value.path) @@ -65,9 +61,7 @@ private fun resourceValue(value: Constant?): String? { // Filesystem timestamps change during ordinary Move/Undo. Content and names // must remain equivalent; incidental timestamp metadata is not a binding. when (value.format) { - Constant.Format.FSFile -> { - bytes(value.fileBytes) - } + Constant.Format.FSFile -> bytes(value.fileBytes) Constant.Format.FSDir -> { val children = value.directoryContents.sortedBy { it.name } @@ -75,19 +69,13 @@ private fun resourceValue(value: Constant?): String? { return children.all(::append) } - Constant.Format.FSLink -> { - return append(value.linkTarget) - } + Constant.Format.FSLink -> return append(value.linkTarget) - else -> { - return false - } + else -> return false } } - else -> { - return false - } + else -> return false } return true } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt index de0a7f028f0..478a9fb1b0c 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceLinks.kt @@ -34,9 +34,7 @@ internal fun compilerSourceLinks( Triple(owner?.importedModule?.identityConstant, node.startPosition, node.endPosition) } - else -> { - return@mapNotNull null - } + else -> return@mapNotNull null } val target = imported.first?.let(declaration)?.takeIf { it.sourceName != null } ?: return@mapNotNull null diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt index c0e958320cb..1268e152303 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerSourceUsage.kt @@ -34,17 +34,13 @@ internal fun compilerWrites(nodes: List): List { - emptyList() - } + else -> emptyList() } } private fun writeTargets(node: AstNode): List = when (node) { - is NameExpression -> { - listOf(node) - } + is NameExpression -> listOf(node) is MultipleLValueStatement, is TupleExpression, @@ -57,7 +53,5 @@ private fun writeTargets(node: AstNode): List = .toList() } - else -> { - emptyList() - } + else -> emptyList() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt index dbb4245dbea..10f04542f3f 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerTypeNames.kt @@ -20,6 +20,7 @@ internal data class CompilerTypeName( val imported: Boolean, ) +/** Written type reference and its proven identity, import route and qualified source path. */ internal data class TypeName( val location: SemanticModel.SourceLocation, val terminal: SemanticModel.Range, @@ -29,6 +30,7 @@ internal data class TypeName( val imported: Boolean, ) +/** Proven declaration identity and its module-relative path for type moves. */ internal data class TypePath( val target: ProofIdentity, val module: String, @@ -59,9 +61,7 @@ internal fun compilerTypeNames(nodes: List): List { if (target !is ClassConstant && target !is TypedefConstant) return@mapNotNull null val tokens = when (node) { - is NamedTypeExpression -> { - node.nameBindings.map { it.name() } - } + is NamedTypeExpression -> node.nameBindings.map { it.name() } is NameExpression -> { val chain = generateSequence(node) { it.leftExpression as? NameExpression }.toList().asReversed() @@ -85,9 +85,7 @@ internal fun compilerTypeNames(nodes: List): List { written } - else -> { - return@mapNotNull null - } + else -> return@mapNotNull null } if (tokens.isEmpty()) return@mapNotNull null diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt index 62f09b77d73..005b73ff3e4 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/LibraryDeclaration.kt @@ -11,6 +11,7 @@ import org.xvm.compiler.ast.PropertyDeclarationStatement import org.xvm.compiler.ast.TypeCompositionStatement import org.xvm.lsp.util.ExecutionTrace +/** Declaration located in attached library source, with the source range used for navigation. */ internal data class LibraryDeclaration( val path: List, val range: SemanticModel.Range, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt index b33d9613f88..f35a897e022 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt @@ -18,6 +18,7 @@ class PartialSemanticModel sites: List, syntaxCompletions: List = emptyList(), ) { + /** Incomplete source operation for which the compiler retained cursor information. */ enum class Kind { NAME, DECLARATION_NAME, @@ -39,6 +40,7 @@ class PartialSemanticModel val writtenConstraint: String? = null, ) + /** Detached member candidate visible at an incomplete source position. */ @ConsistentCopyVisibility data class Member internal constructor( @@ -49,6 +51,7 @@ class PartialSemanticModel val signature: Signature?, ) + /** Written argument range, optional label and any type established before parsing or fitting stopped. */ data class Argument( val range: Range, val label: String?, @@ -61,6 +64,7 @@ class PartialSemanticModel val range: Range, ) + /** Possible method or constructor binding; incomplete analysis has not selected it as the final call. */ @ConsistentCopyVisibility data class CallCandidate internal constructor( @@ -70,6 +74,7 @@ class PartialSemanticModel val constructor: Boolean = false, ) + /** Possible function signature and its argument mapping at an incomplete call. */ @ConsistentCopyVisibility data class FunctionCandidate internal constructor( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt index bed7694c141..97750a5ed8c 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt @@ -49,12 +49,14 @@ class SemanticModel values, ) + /** How much semantic information this compilation snapshot could establish. */ enum class Status { UNAVAILABLE, PARTIAL, COMPLETE, } + /** Language declaration category exposed independently of compiler implementation classes. */ enum class SymbolKind { MODULE, PACKAGE, @@ -66,17 +68,20 @@ class SemanticModel TYPE_PARAMETER, } + /** Whether a source occurrence declares a symbol or refers to an existing declaration. */ enum class Role { DECLARATION, REFERENCE, } + /** Read/write role of a bound source occurrence. */ enum class Usage { READ, WRITE, READ_WRITE, } + /** Declaration properties used by editor presentation and semantic highlighting. */ enum class Modifier { READONLY, STATIC, @@ -94,6 +99,7 @@ class SemanticModel ENUM_VALUE, } + /** Structural type form retained without exposing a compiler TypeConstant. */ enum class TypeForm { NAMED, FORMAL, @@ -133,11 +139,13 @@ class SemanticModel operator fun contains(position: Position): Boolean = position in start.. { - declare(node.nameToken, identity(node), SymbolKind.PROPERTY, node.source) - } + is PropertyDeclarationStatement -> declare(node.nameToken, identity(node), SymbolKind.PROPERTY, node.source) is TypeCompositionStatement -> { // Anonymous classes borrow the constructed type's span for their generated @@ -614,9 +612,7 @@ private class SemanticModelBuilder( } } - is TypedefStatement -> { - declare(node.nameToken, identity(node), SymbolKind.TYPE, node.source) - } + is TypedefStatement -> declare(node.nameToken, identity(node), SymbolKind.TYPE, node.source) } if (node is InvocationExpression) { val callee = node.invokedExpression @@ -627,9 +623,7 @@ private class SemanticModelBuilder( } nodes.forEach { node -> when (node) { - is MethodDeclarationStatement -> { - identity(node)?.let(constants::get)?.let { callable(node, it) } - } + is MethodDeclarationStatement -> identity(node)?.let(constants::get)?.let { callable(node, it) } is LambdaExpression -> { val method = node.lambda ?: return@forEach @@ -1234,17 +1228,12 @@ private class SemanticModelBuilder( ) ?: return@mapNotNull null val name = when { - !method.isConstructor -> { - method.name - } + !method.isConstructor -> method.name - method.containingClass.isAnonInnerClass -> { + method.containingClass.isAnonInnerClass -> "new ${operation.target.childNodes().filterIsInstance().single()}" - } - else -> { - "new ${method.containingClass.name}" - } + else -> "new ${method.containingClass.name}" } PartialSemanticModel.CallCandidate( member = @@ -1325,6 +1314,7 @@ private class SemanticModelBuilder( } } + /** Member lookup context used to distinguish instance, static and implicit receivers. */ private enum class Lookup { INSTANCE, STATIC, @@ -1675,25 +1665,17 @@ private class SemanticModelBuilder( private fun kind(target: Argument?): SymbolKind = when (target) { - is Register -> { - SymbolKind.VARIABLE - } + is Register -> SymbolKind.VARIABLE is Constant -> { when (target.format) { - Constant.Format.Module -> { - SymbolKind.MODULE - } + Constant.Format.Module -> SymbolKind.MODULE - Constant.Format.Package -> { - SymbolKind.PACKAGE - } + Constant.Format.Package -> SymbolKind.PACKAGE Constant.Format.Method, Constant.Format.MultiMethod, - -> { - SymbolKind.METHOD - } + -> SymbolKind.METHOD Constant.Format.Property -> { if ((target as PropertyConstant).isFormalType) { @@ -1706,19 +1688,13 @@ private class SemanticModelBuilder( Constant.Format.TypeParameter, Constant.Format.FormalTypeChild, Constant.Format.DynamicFormal, - -> { - SymbolKind.TYPE_PARAMETER - } + -> SymbolKind.TYPE_PARAMETER - else -> { - SymbolKind.TYPE - } + else -> SymbolKind.TYPE } } - else -> { - SymbolKind.VARIABLE - } + else -> SymbolKind.VARIABLE } private fun signature(target: Argument): Signature? { @@ -1770,63 +1746,23 @@ private class SemanticModelBuilder( } val underlying = when { - constant.isRelationalType -> { - listOf(copyType(constant.underlyingType), copyType(constant.underlyingType2)) - } - - constant.isModifyingType -> { - listOf(copyType(constant.underlyingType)) - } - - else -> { - emptyList() - } + constant.isRelationalType -> listOf(copyType(constant.underlyingType), copyType(constant.underlyingType2)) + constant.isModifyingType -> listOf(copyType(constant.underlyingType)) + else -> emptyList() } val form = when (constant.format) { - Constant.Format.TerminalType -> { - if (constant.isFormalType) TypeForm.FORMAL else TypeForm.NAMED - } - - Constant.Format.ParameterizedType -> { - TypeForm.PARAMETERIZED - } - - Constant.Format.ImmutableType -> { - TypeForm.IMMUTABLE - } - - Constant.Format.ServiceType -> { - TypeForm.SERVICE - } - - Constant.Format.AccessType -> { - TypeForm.ACCESS - } - - Constant.Format.AnnotatedType -> { - TypeForm.ANNOTATED - } - - Constant.Format.UnionType -> { - TypeForm.UNION - } - - Constant.Format.IntersectionType -> { - TypeForm.INTERSECTION - } - - Constant.Format.DifferenceType -> { - TypeForm.DIFFERENCE - } - - Constant.Format.RecursiveType -> { - TypeForm.RECURSIVE - } - - else -> { - TypeForm.OTHER - } + Constant.Format.TerminalType -> if (constant.isFormalType) TypeForm.FORMAL else TypeForm.NAMED + Constant.Format.ParameterizedType -> TypeForm.PARAMETERIZED + Constant.Format.ImmutableType -> TypeForm.IMMUTABLE + Constant.Format.ServiceType -> TypeForm.SERVICE + Constant.Format.AccessType -> TypeForm.ACCESS + Constant.Format.AnnotatedType -> TypeForm.ANNOTATED + Constant.Format.UnionType -> TypeForm.UNION + Constant.Format.IntersectionType -> TypeForm.INTERSECTION + Constant.Format.DifferenceType -> TypeForm.DIFFERENCE + Constant.Format.RecursiveType -> TypeForm.RECURSIVE + else -> TypeForm.OTHER } types[id] = Type( @@ -1870,13 +1806,9 @@ private class SemanticModelBuilder( copyableType(constant.underlyingType2, visited) } - constant.isModifyingType -> { - copyableType(constant.underlyingType, visited) - } + constant.isModifyingType -> copyableType(constant.underlyingType, visited) - else -> { - true - } + else -> true } } @@ -1902,26 +1834,18 @@ private class SemanticModelBuilder( ( formal.method.isNascent || formal.method.rawParams[formal.register].containsUnresolved() - ) -> { - false - } + ) -> false - formal != null -> { - acyclicConstraint(formal.constraintType, visited) - } + formal != null -> acyclicConstraint(formal.constraintType, visited) constant.isRelationalType -> { acyclicConstraint(constant.underlyingType, visited) && acyclicConstraint(constant.underlyingType2, visited) } - constant.isModifyingType -> { - acyclicConstraint(constant.underlyingType, visited) - } + constant.isModifyingType -> acyclicConstraint(constant.underlyingType, visited) - else -> { - true - } + else -> true } } @@ -1936,9 +1860,7 @@ private class SemanticModelBuilder( val visited = seen + constant val resolved = constant.resolveTypedefs() return when { - resolved != constant -> { - typeDefinitions(resolved, visited) - } + resolved != constant -> typeDefinitions(resolved, visited) constant.isNullable -> { // T? navigates to T, not to the Nullable marker that makes the union nullable. @@ -1955,9 +1877,7 @@ private class SemanticModelBuilder( operands.flatMap { typeDefinitions(it, visited) } } - constant.isModifyingType -> { - typeDefinitions(constant.underlyingType, visited) - } + constant.isModifyingType -> typeDefinitions(constant.underlyingType, visited) else -> { val target = normalized(constant) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt index a308a610801..0dbb99d7c26 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt @@ -173,6 +173,7 @@ class XdkAdapter AdapterCapability.LINKED_EDITING, ) + if (colorPrototype) setOf(AdapterCapability.DOCUMENT_COLOR) else emptySet() + /** Workspace discovery inputs and the catalog built from them, including any configuration problem. */ private data class Discovery( val folders: List = emptyList(), val explicit: Boolean = false, @@ -574,6 +575,7 @@ class XdkAdapter return request.result } + /** Cursor query category used when coalescing work for the same document. */ private enum class CursorKind { PROBE, COMPLETION, @@ -581,20 +583,24 @@ class XdkAdapter SIGNATURE, } + /** Document and query category whose pending cursor request can supersede an older request. */ private data class CursorKey( val uri: String, val kind: CursorKind, ) + /** Cancelable result and queued compiler work owned by a dependent query. */ private sealed interface QueryWork { val result: CompletableFuture<*> val task: CompilerQueueTrace.Work } + /** Query work tied to the compilation request whose snapshot it consumes. */ private sealed interface QueryRequest : QueryWork { val compilation: Request } + /** Project-wide query categories with separate scheduling and cancellation identities. */ private enum class ProjectQueryKind { REFERENCES, CODE_LENSES, @@ -609,6 +615,7 @@ class XdkAdapter DIAGNOSTICS, } + /** Identity used to coalesce equivalent project queries without conflating distinct operations. */ private data class ProjectQueryKey( val uri: String, val kind: ProjectQueryKind, @@ -616,6 +623,10 @@ class XdkAdapter val requestId: UUID? = null, ) + /** + * Project query with captured dependencies and overlays, executed against those inputs on the + * compiler worker. + */ private class ProjectRequest( val key: ProjectQueryKey, val project: XdkProject, @@ -628,6 +639,7 @@ class XdkAdapter override val task = trace.task("project-${key.kind}", key.uri, result) { work(this) } } + /** Cursor query and source position bound to one compilation request. */ private class CursorRequest( override val compilation: Request, val key: CursorKey, @@ -642,6 +654,7 @@ class XdkAdapter override val task = trace.task("cursor-${key.kind}", uri, result) { work(this) } } + /** Rename query bound to the compilation, module and position on which its proof depends. */ private class RenameRequest( override val compilation: Request, val module: ModuleAnalysis, @@ -831,6 +844,10 @@ class XdkAdapter override fun getCachedResult(uri: String): CompilationResult? = analysis(uri)?.let { CompilationResult.withDiagnostics(uri, it.diagnostics, it.symbols) } + /** + * Captured compilation inputs and scheduling state for one module analysis. Later workspace changes + * create a new request rather than mutating these inputs. + */ private class Request( val scope: String, val uri: String, @@ -845,6 +862,7 @@ class XdkAdapter val task = trace.task("compile", scope, result) { work(this) } } + /** New compilation request together with the superseded work that must be retired. */ private data class Submission( val request: Request, val retired: List, @@ -885,6 +903,10 @@ class XdkAdapter ?: XdkLibrarySources.sourceUri(name) } + /** + * Compiler output assembled for publication; AST and semantic data may be absent after a failed + * attempt. + */ private data class Analysis( val diagnostics: List = emptyList(), val symbols: List = emptyList(), @@ -1145,11 +1167,13 @@ class XdkAdapter XdkSources.capture(root, request.overlays, resources, cancelled) } + /** Exact source inputs and dependency revisions that determine whether a compiled module can be reused. */ private data class BuildKey( val sources: XdkSources.Inputs, val dependencies: Map, ) + /** Reusable build output and diagnostics indexed by the exact inputs that produced them. */ private data class CachedBuild( val key: BuildKey, val artifact: XdkDependency?, @@ -1250,9 +1274,7 @@ class XdkAdapter ) } - else -> { - compileOne(request, uri, sources, inputs, module.name) - } + else -> compileOne(request, uri, sources, inputs, module.name) } synchronized(lifecycle) { if (isStale(request)) throw CancellationException() diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt index 272637942cb..dd50018e092 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAttachedSources.kt @@ -16,6 +16,7 @@ import java.util.HexFormat /** Navigation-only snapshots: attachments never become writable project modules. */ internal object XdkAttachedSources { + /** Mapping from dependency symbols to attached source locations and read-only source documents. */ data class Index( val declarations: Map, val documents: Map, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt index b78336879ca..1e8237bf035 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAutoImports.kt @@ -18,6 +18,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Detached import candidates; only a successful whole-graph repair proof can publish an edit. */ internal object XdkAutoImports { + /** Importable declaration name, module and qualified path established by the compiler. */ data class Target( val name: String, val module: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt index f1e09a42a65..01b31aacd0a 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkBuildModel.kt @@ -8,6 +8,7 @@ import java.io.IOException internal class XdkBuildModel private constructor( private val sourceSets: List, ) { + /** Sources, resources and dependency paths exported for one Gradle source set. */ data class SourceSet( val project: String, val name: String, @@ -18,6 +19,7 @@ internal class XdkBuildModel private constructor( val modulePath: List, ) + /** Source modules and binary dependencies assembled from an accepted Gradle build model. */ data class Inputs( val modules: List, val binaries: List, @@ -84,17 +86,9 @@ internal class XdkBuildModel private constructor( .distinct() .flatMap { root -> when { - root.isDirectory -> { - root.listFiles().orEmpty().filter { it.isFile && it.extension == "xtc" } - } - - root.isFile && root.extension == "xtc" -> { - listOf(root) - } - - else -> { - emptyList() - } + root.isDirectory -> root.listFiles().orEmpty().filter { it.isFile && it.extension == "xtc" } + root.isFile && root.extension == "xtc" -> listOf(root) + else -> emptyList() } }.distinct() .map { XdkDependency.fromBinary(it.readBytes()) } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt index 4d611147c8a..c53915fc306 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkColorPrototype.kt @@ -47,6 +47,7 @@ internal object XdkColorPrototype { ): String = text.substring(offset(start), offset(end)) } + /** Compiler-bound constructor call inspected for supported literal color channels. */ private data class Construction( val source: Source, val start: Long, @@ -54,6 +55,7 @@ internal object XdkColorPrototype { val binding: InvocationBinding, ) + /** Literal channel value and offsets to replace when applying an editor color selection. */ internal data class Channel( val parameter: Int, val start: Int, @@ -61,6 +63,7 @@ internal object XdkColorPrototype { val value: Int, ) + /** Original constructor text and channel locations retained to preserve source spelling during color edits. */ internal class Site( val sourceName: String?, val range: Range, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt index bc12cb3b478..d37a4c678ef 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt @@ -25,9 +25,7 @@ internal object XdkCursorQueries { ) } - else -> { - return model.syntaxCompletions - } + else -> return model.syntaxCompletions } val range = Range( @@ -283,6 +281,7 @@ internal object XdkCursorQueries { ) } + /** Call-site signature, argument mapping and documentation detached for signature-help responses. */ private data class SignatureSite( val range: SemanticModel.Range, val callee: SemanticModel.Range, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt index 239b52a79c5..32ea05f3717 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt @@ -57,6 +57,7 @@ class XdkDependency }.toByteArray() .let { HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(it)) } + /** Constant-table symbol identity qualified by its module and immutable artifact revision. */ data class SymbolKey( val module: String, val revision: String, @@ -123,6 +124,11 @@ internal data class DependencyDeclaration( val location: SemanticModel.SourceLocation, ) +/** + * Immutable set of external binary modules for a compilation. Rejects duplicate or bundled-module + * replacements and opens fresh compiler repositories on the worker so mutable pools are not shared between + * attempts. + */ internal class XdkDependencies( dependencies: List, ) { @@ -161,6 +167,10 @@ internal class XdkDependencies( return Open(repository, declarations, modules.mapValues { it.value.revision }, modules) } + /** + * Attempt-owned repository and declaration identities, paired with the artifact revisions that produced + * them. + */ class Open( val repository: ModuleRepository, val declarations: Map, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt index b744d7c1d0b..ea8c5421c4c 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnosticIndex.kt @@ -5,18 +5,21 @@ import java.util.concurrent.atomic.AtomicReference /** Bounded to the current graph. Only detached diagnostics, source inputs and artifacts survive. */ internal class XdkDiagnosticIndex { + /** Module source and dependency inputs that determine reusable diagnostics. */ data class Key( val name: String, val sources: XdkSources.Inputs, val dependencies: Map, ) + /** Diagnostics and optional emitted artifact from one set of module inputs. */ data class Build( val key: Key, val result: CompilationResult, val artifact: XdkDependency?, ) + /** Published diagnostic results and reusable builds for one project revision. */ data class Snapshot( val revision: String? = null, val results: List = emptyList(), diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt index 46a9f3bf2b3..64485181816 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDiagnostics.kt @@ -31,18 +31,12 @@ internal fun ErrorListener.ErrorInfo.toDiagnostic( return Diagnostic( location = when (where) { - is ErrorListener.Site.In -> { - sourceUri?.let { spanOf(it, where) } ?: wholeDocument(uri) - } + is ErrorListener.Site.In -> sourceUri?.let { spanOf(it, where) } ?: wholeDocument(uri) - is ErrorListener.Site.At -> { - declarations[where.xs()] ?: wholeDocument(uri) - } + is ErrorListener.Site.At -> declarations[where.xs()] ?: wholeDocument(uri) // a whole-compilation failure belongs to the document, not to a line in it - else -> { - wholeDocument(uri) - } + else -> wholeDocument(uri) }, severity = severity.toLspSeverity(), message = diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt index 35185abf594..aa92746b7cd 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkImports.kt @@ -16,6 +16,7 @@ import org.xvm.lsp.util.ExecutionTrace * publication. */ internal object XdkImports { + /** Proposed import edit awaiting the compiler checks that establish its safety. */ data class Candidate( val title: String, val kind: CodeActionKind, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt index 9e56d1ac132..d9f9c1896e8 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLexical.kt @@ -16,6 +16,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Java-lexer editing helpers. A failed lex never authorizes a source rewrite. */ internal object XdkLexical { + /** Compiler token with source offsets and editor range for syntax-aware edits. */ private data class Span( val id: Token.Id, val range: Range, @@ -46,6 +47,7 @@ internal object XdkLexical { ) } ?: true + /** Brace, parenthesis and bracket depths used while scanning a token sequence. */ private data class Nesting( val braces: Int = 0, val parentheses: Int = 0, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt index 969745ff184..e5109e27c6b 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt @@ -11,11 +11,13 @@ import java.util.Set.copyOf as immutableSet /** The compiler and its matching libraries travel together in the language server. */ internal object XdkLibraries { + /** Bundled compiler repository and the immutable artifacts used to reopen its modules. */ private data class Bundle( val repository: BuildRepository, val artifacts: Map, ) + /** Library artifact revision with lazily materialized detached symbols. */ private data class Artifact( val revision: String, val symbols: Lazy, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt index bf16c25b8f2..b3f472683a7 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibrarySources.kt @@ -21,11 +21,13 @@ import java.util.zip.ZipInputStream * not. */ internal object XdkLibrarySources { + /** Attached source text and the revision used to detect a changed source file. */ private data class Entry( val text: String, val revision: String, ) + /** Read-only library source document and its declarations, identified by module and relative path. */ private data class SourceFile( val module: String, val path: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt index e1ddcec28b5..3b25694c61d 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalDeclarations.kt @@ -22,6 +22,7 @@ internal object XdkLocalDeclarations { ?.takeIf { it.kind == SemanticModel.SymbolKind.VARIABLE && it.declarationSource == source } } + /** Local declaration and its assignment, retaining the written type for source edits. */ data class Initializer( val statement: AssignmentStatement, val local: VariableDeclarationStatement, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt index 0dbe6305d77..9e4374353be 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalExtraction.kt @@ -22,12 +22,14 @@ import org.xvm.lsp.util.ExecutionTrace * preserve relocated references and calls as well as every unaffected binding and dispatch chain. */ internal object XdkLocalExtraction { + /** Proposed local extraction and the relocation needed to compare its bindings after recompilation. */ data class Candidate( val title: String, val edits: List, val relocation: XdkRename.Relocation, ) + /** Statement and expression selected for extraction, with any known type. */ private data class Selected( val statement: AstNode, val expression: Expression, @@ -71,9 +73,7 @@ internal object XdkLocalExtraction { } } - else -> { - null - } + else -> null } } else { null diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt index f0fdcf9f1e2..02d3b89fa20 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalInline.kt @@ -18,6 +18,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Inline single-use locals with either adjacent evaluation or compiler-proven constant values. */ internal object XdkLocalInline { + /** Inline-local edits and the removed or relocated source sites needed for compiler proof. */ data class Candidate( val title: String, val edits: List, @@ -26,6 +27,7 @@ internal object XdkLocalInline { val expression: SemanticModel.SourceLocation? = null, ) + /** Adjacent declaration and use eligible for inlining without changing evaluation order. */ private data class AdjacentUse( val assignment: AssignmentStatement, val statement: AstNode, @@ -85,9 +87,7 @@ internal object XdkLocalInline { } } - else -> { - null - } + else -> null } } } else { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt index fb6f2bf6745..78bb6e8a91f 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLocalRemoval.kt @@ -11,6 +11,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Delete an unused plain local only after the compiler proves its initializer needs no evaluation. */ internal object XdkLocalRemoval { + /** Proposed local removal and the source locations whose disappearance is intentional. */ data class Candidate( val edit: XdkRename.Edit, val removed: Set, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt index 7ba10edcaf9..cf3fdea77b9 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberActions.kt @@ -4,11 +4,13 @@ import org.xvm.lsp.adapter.Range /** Immutable contracts and atomic class-member edits. */ internal object XdkMemberActions { + /** Import declaration and its insertion position in the destination source. */ data class Import( val position: SemanticModel.Position, val declaration: String, ) + /** Generated member insertion and any imports needed by its signature. */ data class Edits( val member: XdkRename.Edit, val imports: List, @@ -17,6 +19,7 @@ internal object XdkMemberActions { get() = imports + member } + /** Compiler-derived member contract and proposed implementation at a specific owner. */ data class Candidate( val owner: SemanticModel.SourceLocation, val contract: ProofIdentity, @@ -38,6 +41,7 @@ internal object XdkMemberActions { owner.range.end >= SemanticModel.Position(range.start.line, range.start.column) } + /** Group of member implementations offered together as one code action. */ data class Action( val members: List, ) { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt index de00d34dee5..476c4834c44 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMemberInline.kt @@ -22,6 +22,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Copy one private member expression at its use; declaration deletion is a separate proof. */ internal object XdkMemberInline { + /** Proposed member inlining with the use, replacement and intentionally removed binding sites. */ data class Candidate( val title: String, val source: String, @@ -92,9 +93,7 @@ internal object XdkMemberInline { Triple(property, read, value) } - else -> { - return null - } + else -> return null } if (owner(member) !== owner(use)) return null // A local callable or closure can have a different receiver or capture environment. diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt index 6f29cbe7988..b92e856db89 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMethodExtraction.kt @@ -24,6 +24,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Same-owner expression extraction. Syntax proposes the helper; compiler facts prove the edit. */ internal object XdkMethodExtraction { + /** Value captured by an extracted method, with declaration identity, type and argument/parameter offsets. */ data class Capture( val declaration: SemanticModel.SourceLocation, val kind: SemanticModel.SymbolKind, @@ -33,6 +34,7 @@ internal object XdkMethodExtraction { val argumentOffset: Int, ) + /** Proposed helper method, replacement and captured values to verify in the trial compilation. */ data class Candidate( val name: String, val expression: SemanticModel.SourceLocation, @@ -134,12 +136,14 @@ internal object XdkMethodExtraction { } is AssignmentStatement -> { - if (statement.rValue === expression) XdkLocalDeclarations.initializer(text, statement)?.type else null + if (statement.rValue === expression) { + XdkLocalDeclarations.initializer(text, statement)?.type + } else { + null + } } - else -> { - null - } + else -> null } ?: facts.sourceTypes[selectedLocation] ?: return null } val selectedNodes = roots.flatMap { XdkRefactoringSyntax.tree(it).map { entry -> entry.first }.toList() } @@ -194,13 +198,11 @@ internal object XdkMethodExtraction { written.type } - is VariableDeclarationStatement -> { - written.childNodes().filterIsInstance().singleOrNull() ?: return null - } + is VariableDeclarationStatement -> + written.childNodes().filterIsInstance().singleOrNull() + ?: return null - else -> { - return null - } + else -> return null } if (type is VariableTypeExpression || type is AnnotatedTypeExpression) return null Capture(declaration, symbol.kind, symbol.name, spelling(type) ?: return null, 0, 0) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt index 5f31938b8ad..af231cfa733 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingDeclarations.kt @@ -14,6 +14,10 @@ import org.xvm.lsp.adapter.Range /** Syntax chooses a writable declaration; a complete compiler repair must prove its use. */ internal object XdkMissingDeclarations { + /** + * Proposed declaration and its unresolved use, retained so recompilation can prove that the use binds to + * the inserted declaration. + */ data class Candidate( val name: String, val kind: SemanticModel.SymbolKind, @@ -153,9 +157,7 @@ internal object XdkMissingDeclarations { Candidate(node.name, SemanticModel.SymbolKind.PROPERTY, location(node), edit, expected) } - else -> { - null - } + else -> null } }.distinct() } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt index 49fd0f3b555..a3feb45524c 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMissingMethods.kt @@ -4,11 +4,13 @@ import org.xvm.lsp.adapter.Range /** Detached signatures from fresh declarations; only a complete repair proof can publish them. */ internal object XdkMissingMethods { + /** Whether the generated method must be an instance member or a static function. */ enum class Dispatch { INSTANCE, STATIC, } + /** Destination type and dispatch mode established from the unresolved call receiver. */ data class Receiver( val destination: SemanticModel.SourceLocation, val dispatch: Dispatch, @@ -23,11 +25,13 @@ internal object XdkMissingMethods { val importSource: String? = null, ) + /** Type spelling and imports required in the generated method's destination. */ data class TypeSource( val source: String, val imports: List = emptyList(), ) + /** Detached parameter, return and generic constraints used to prove a generated method signature. */ data class Signature( val parameters: List, val returns: List, @@ -43,17 +47,20 @@ internal object XdkMissingMethods { val expressions: Map = emptyMap(), ) + /** Detached local type identity paired with source context for destination rendering. */ data class LocalType( val source: String?, val identity: ProofIdentity, val destinationSources: Map = emptyMap(), ) + /** Argument use and the declaration it must continue to reference after the edit. */ data class ArgumentBinding( val use: SemanticModel.SourceLocation, val declaration: SemanticModel.SourceLocation, ) + /** Missing-method proposal with its destination, argument bindings and visibility requirements. */ data class Candidate( val callee: SemanticModel.SourceLocation, val name: String, @@ -142,9 +149,7 @@ internal object XdkMissingMethods { new: ProofIdentity, ): Boolean = when (old) { - in binders -> { - binders[old] == new - } + in binders -> binders[old] == new is ProofIdentity.TypeShape if new is ProofIdentity.TypeShape -> { old.format == new.format && old.components.size == new.components.size && @@ -156,9 +161,7 @@ internal object XdkMissingMethods { old.targets.all { first -> new.targets.any { sameType(first, it) } } } - else -> { - XdkRename.sameType(old, new, plan) - } + else -> XdkRename.sameType(old, new, plan) } fun equivalent( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt index 388f249a306..ec98c795f3e 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMonikers.kt @@ -27,6 +27,7 @@ internal class XdkArtifactSymbols private constructor( ) { private val entries = immutableMap(entries) + /** Dependency constant index and whether the corresponding moniker is exported. */ private data class Entry( val index: Int, val exported: Boolean, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt index d2242e78011..d919d081999 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkMoveImports.kt @@ -10,6 +10,7 @@ import org.xvm.compiler.ast.TypeCompositionStatement /** Fresh, capture-free module aliases for a proposed ownership change. No compiler state escapes. */ internal object XdkMoveImports { + /** Resolved import aliases and the edit needed to preserve them after moving a source. */ data class Imports( val aliases: Map, val edit: XdkRename.Edit, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt index 8b925d190b6..c7cfe59908f 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkNavigationIndex.kt @@ -7,24 +7,30 @@ import java.util.concurrent.atomic.AtomicReference * no compiler, AST, constant pool or refactoring authorization survives here. */ internal class XdkNavigationIndex { + /** Module source inputs and dependency revisions determining reusable navigation facts. */ data class Key( val name: String, val sources: XdkSources.Inputs, val dependencies: Map, ) + /** Detached semantic models and declaration proofs used by cross-module navigation. */ data class Facts( val models: List, val constants: Map, ) + /** Navigation facts and optional dependency artifact retained for a particular input key. */ data class Build( val key: Key, val facts: Facts?, val artifact: XdkDependency?, ) - // Identity, rather than structural equality, fences publication after retirement or close. + /** + * Reusable module builds and the navigation models published together from them. Identity, rather + * than structural equality, fences publication after retirement or close. + */ class Snapshot( val builds: Map = emptyMap(), val navigation: Map = emptyMap(), diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt index 2dc789d1dc4..2d78b4693c8 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkPresentation.kt @@ -35,18 +35,14 @@ internal object XdkPresentation { ): String? { val label = when { - symbol != null && signature != null -> { - XdkCursorQueries.signature(model, symbol.name, signature).label - } + symbol != null && signature != null -> XdkCursorQueries.signature(model, symbol.name, signature).label symbol != null -> { listOfNotNull(type ?: symbol.kind.name.lowercase(), symbol.name) .joinToString(" ") } - else -> { - type - } + else -> type } ?: return null return "```xtc\n$label\n```" + symbol?.documentation?.let { "\n\n$it" }.orEmpty() } @@ -67,9 +63,7 @@ internal object XdkPresentation { when (symbol.kind) { SymbolKind.MODULE, SymbolKind.PACKAGE, - -> { - "namespace" - } + -> "namespace" SymbolKind.TYPE -> { if (occurrence.annotation) { @@ -95,25 +89,15 @@ internal object XdkPresentation { } } - SymbolKind.TYPE_PARAMETER -> { - "typeParameter" - } + SymbolKind.TYPE_PARAMETER -> "typeParameter" - SymbolKind.METHOD -> { - if (symbol.isFunction) "function" else "method" - } + SymbolKind.METHOD -> if (symbol.isFunction) "function" else "method" - SymbolKind.PROPERTY -> { - "property" - } + SymbolKind.PROPERTY -> "property" - SymbolKind.VARIABLE -> { - "variable" - } + SymbolKind.VARIABLE -> "variable" - SymbolKind.PARAMETER -> { - "parameter" - } + SymbolKind.PARAMETER -> "parameter" } val modifiers = buildList { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt index b4d8dd8bb7d..d7d7e7a43df 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkProjectQueries.kt @@ -395,37 +395,25 @@ internal class XdkProjectQueries( } val targets = when { - target is ProofIdentity.Parameter -> { - parameterFamily(before, target) ?: return null - } + target is ProofIdentity.Parameter -> parameterFamily(before, target) ?: return null symbol.kind in setOf( SemanticModel.SymbolKind.TYPE, SemanticModel.SymbolKind.PACKAGE, SemanticModel.SymbolKind.MODULE, - ) -> { - setOf(target) - } + ) -> setOf(target) symbol.kind in setOf(SemanticModel.SymbolKind.METHOD, SemanticModel.SymbolKind.PROPERTY) && SemanticModel.Modifier.STATIC in symbol.modifiers && - symbol.name != "construct" -> { - setOf(target) - } + symbol.name != "construct" -> setOf(target) - symbol.kind == SemanticModel.SymbolKind.PROPERTY -> { - propertyFamily(before, target) ?: return null - } + symbol.kind == SemanticModel.SymbolKind.PROPERTY -> propertyFamily(before, target) ?: return null - symbol.kind == SemanticModel.SymbolKind.METHOD -> { - methodFamily(before, target) ?: return null - } + symbol.kind == SemanticModel.SymbolKind.METHOD -> methodFamily(before, target) ?: return null - else -> { - return null - } + else -> return null } val ids = before.constants.filterValues { members(it).all(targets::contains) }.keys val base = XdkRename.plan(before, texts, source, line, column, name, ids) ?: return null @@ -614,9 +602,7 @@ internal class XdkProjectQueries( } } - else -> { - null - } + else -> null } } }.firstOrNull() ?: return@map null @@ -1260,13 +1246,9 @@ internal class XdkProjectQueries( private fun members(identity: ProofIdentity): Set = when (identity) { - is ProofIdentity.Alternatives -> { - identity.targets.flatMapTo(linkedSetOf(), ::members) - } + is ProofIdentity.Alternatives -> identity.targets.flatMapTo(linkedSetOf(), ::members) - is ProofIdentity.Composed -> { - identity.members.flatMapTo(linkedSetOf(), ::members) - } + is ProofIdentity.Composed -> identity.members.flatMapTo(linkedSetOf(), ::members) is ProofIdentity.Parameter -> { members(identity.method).mapTo(linkedSetOf()) { @@ -1274,9 +1256,7 @@ internal class XdkProjectQueries( } } - else -> { - setOf(identity) - } + else -> setOf(identity) } /** Parameter names belong to callable slots, including differently named overrides. */ @@ -1311,6 +1291,7 @@ internal class XdkProjectQueries( return parameters.takeIf { declarations.keys.containsAll(it) } } + /** Whether a proposed edit must preserve a complete compilation or repair an already incomplete one. */ private enum class Proof { COMPLETE, REPAIR, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt index f374c8c87be..a98064252db 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkQualifiedName.kt @@ -37,6 +37,7 @@ internal class XdkQualifiedName private constructor( return XdkRename.Edit(0, end, prefix + extra) } + /** Token and offsets forming one component of a written qualified name. */ private data class Part( val id: Token.Id, val start: Int, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt index 2963dde0974..7a1a82f0810 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRename.kt @@ -12,6 +12,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Worker-only edit planning and binding comparison. Compiler constants never escape this proof. */ internal object XdkRename { + /** Replacement text and offsets in the original source, before any edits are applied. */ data class Edit( val start: Int, val end: Int, @@ -26,6 +27,10 @@ internal object XdkRename { val contentOffset: Int, ) + /** + * Source snapshots, text edits and file moves for a trial refactoring. Also translates source locations + * so bindings can be compared across the original and proposed compilations. + */ class Plan( val original: Map, val edits: Map>, @@ -643,13 +648,9 @@ internal object XdkRename { is ProofIdentity.Binary, is ProofIdentity.Method, - -> { - target == Target.External(contract) - } + -> target == Target.External(contract) - else -> { - false - } + else -> false } val changed = newDispatch - oldDispatch val additions = @@ -713,21 +714,10 @@ internal object XdkRename { chains: Set, ): Site? = when (target) { - is Target.Declaration -> { - target.site - } - - is Target.SourceProof -> { - target.site - } - - is Target.Composed -> { - (memberBridge(target, chains)?.firstOrNull() as? Target.Declaration)?.site - } - - else -> { - null - } + is Target.Declaration -> target.site + is Target.SourceProof -> target.site + is Target.Composed -> (memberBridge(target, chains)?.firstOrNull() as? Target.Declaration)?.site + else -> null } fun selectedRebinding( @@ -784,9 +774,7 @@ internal object XdkRename { it } - else -> { - return null - } + else -> return null } } return written.takeIf { @@ -839,6 +827,7 @@ internal object XdkRename { Target.Declaration(Site(source, start, end), symbol.kind) to slot }.toMap() + /** Renamed owner and contribution chain used to detect unintended override or dispatch changes. */ private data class Dispatch( val owner: Target, val members: List, @@ -915,6 +904,7 @@ internal object XdkRename { return Site(moved(source), start, end) } + /** Source occurrence translated through a refactoring, distinguishing calls from other bindings. */ private data class Site( val source: String, val start: Int, @@ -922,48 +912,59 @@ internal object XdkRename { val call: Boolean = false, ) + /** Binding identity normalized through a proposed edit for comparison across compilation attempts. */ private sealed interface Target { + /** Written declaration identified by its translated location and symbol kind. */ data class Declaration( val site: Site, val kind: SemanticModel.SymbolKind, ) : Target + /** Detached dependency identity unaffected by source-location translation. */ data class External( val constant: ProofIdentity, ) : Target + /** Translated directory identity for an implicit package. */ data class Directory( val path: String, ) : Target + /** Value parameter identified by the translated method and parameter slot. */ data class Parameter( val method: Target, val index: Int, ) : Target + /** Generic method parameter identified separately from visible value parameters. */ data class MethodFormal( val method: Target, val index: Int, ) : Target + /** Compiler-generated primary constructor identified by its translated owner. */ data class PrimaryConstructor( val owner: Target, ) : Target + /** Super receiver interpreted relative to its translated method. */ data class Super( val method: Target, ) : Target + /** Predefined receiver identified by its translated owner and compiler register. */ data class Receiver( val owner: Target, val register: Int, ) : Target + /** Translated source declaration and constant kind used in a detached proof. */ data class SourceProof( val site: Site, val format: Constant.Format, ) : Target + /** Translated dispatch composition, preserving member order, delegates, cycles and alternatives. */ data class Composed( val owner: Target, val members: List, @@ -972,10 +973,12 @@ internal object XdkRename { val alternatives: List, ) : Target + /** Unordered possible bindings whose translated identities must remain equivalent. */ data class Alternatives( val targets: Set, ) : Target + /** Translated structural type identity with its component bindings. */ data class TypeShape( val format: Constant.Format, val components: List, @@ -1000,9 +1003,14 @@ internal object XdkRename { translate: (String, Int) -> Int?, ): Target? { return when (identity) { - is ProofIdentity.TypeShape -> { - Target.TypeShape(identity.format, identity.components.map { composedTarget(it, texts, moved, translate) ?: return null }) - } + is ProofIdentity.TypeShape -> + Target.TypeShape( + identity.format, + identity.components.map { + composedTarget(it, texts, moved, translate) + ?: return null + }, + ) is ProofIdentity.Alternatives -> { Target.Alternatives( @@ -1029,13 +1037,15 @@ internal object XdkRename { ?.let(Target::PrimaryConstructor) } - is ProofIdentity.Super -> { - composedTarget(identity.method, texts, moved, translate)?.let(Target::Super) - } + is ProofIdentity.Super -> composedTarget(identity.method, texts, moved, translate)?.let(Target::Super) - is ProofIdentity.Receiver -> { - composedTarget(identity.owner, texts, moved, translate)?.let { Target.Receiver(it, identity.register) } - } + is ProofIdentity.Receiver -> + composedTarget( + identity.owner, + texts, + moved, + translate, + )?.let { Target.Receiver(it, identity.register) } is ProofIdentity.Composed -> { val owner = composedTarget(identity.owner, texts, moved, translate) ?: return null @@ -1064,9 +1074,7 @@ internal object XdkRename { Target.SourceProof(Site(moved(source), start, end), identity.format) } - else -> { - Target.External(identity) - } + else -> Target.External(identity) } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt index 5d678700a5e..42a55335172 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkRenameProposal.kt @@ -34,6 +34,7 @@ class XdkRenameScope sourceUris: List, val revision: String, ) { + /** Whether the proposal depends on an explicitly configured source graph or a discovered one. */ enum class Boundary { CONFIGURED_GRAPH, DISCOVERED_GRAPH, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt index 3b59b38ac38..f3f99586012 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkResourceMoves.kt @@ -9,6 +9,7 @@ internal class XdkResourceMoves( private val changedSources: Set, private val incoming: Map = emptyMap(), ) { + /** Stops a trial move when the resource value cannot be established from the captured inputs. */ class UnprovenResource( reason: String, ) : RuntimeException(reason) @@ -43,6 +44,7 @@ internal class XdkResourceMoves( return Directory(null, "", resources.roots.map(::path), entries) } + /** Resource-directory view translating proposed paths back to the captured source roots. */ private inner class Directory( parent: ResourceDir?, name: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt index 522673ac49d..90e93e04242 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSafeDelete.kt @@ -15,6 +15,7 @@ import org.xvm.lsp.util.ExecutionTrace /** Private source members have bounded consumers; public APIs and initialization effects do not. */ internal object XdkSafeDelete { + /** Proposed declaration removal and the binding sites intentionally removed with it. */ data class Candidate( val name: String, val identity: ProofIdentity, @@ -56,9 +57,7 @@ internal object XdkSafeDelete { } } - else -> { - null - } + else -> null } ?: return null if (parents[parents[member]] !is TypeCompositionStatement) return null if (XdkRefactoringSyntax.tree(member).any { (node, _) -> node is AnnotationExpression }) return null diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt index 3d74a9da3f9..c92079a6d15 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSourceModule.kt @@ -50,6 +50,7 @@ internal class XdkProject( ) { val modules: Map = immutableMap(modules.associateBy { it.name }) + /** Canonical source-module inputs used to compare project configurations. */ private data class Configuration( val name: String, val uri: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt index 6f1f8a7cd12..a4a1c185e81 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt @@ -32,6 +32,7 @@ internal class XdkSources /** Cache identity contains only immutable input values, never a parsed ModuleInfo tree. */ val inputs = Inputs(this.text, immutableSet(directories), immutableMap(aliases), resources) + /** Captured source text, directories, aliases and resources that identify one compilation input set. */ data class Inputs( val text: Map, val directories: Set, @@ -86,6 +87,7 @@ internal class XdkSources text[file]?.toCharArray() ?: throw IOException("Source is absent from this compilation snapshot: $file") + /** Unsaved editor text paired with its document URI for source snapshot construction. */ private data class Overlay( val uri: String, val text: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt index 2bc21643db1..75ce43f97f2 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSymbols.kt @@ -95,17 +95,11 @@ internal object XdkSymbols { node: AstNode, ): SymbolInfo? = when (node) { - is TypeCompositionStatement -> { - node.name?.let { SymbolInfo.of(it, kindOf(node), rangeOf(uri, node)) } - } + is TypeCompositionStatement -> node.name?.let { SymbolInfo.of(it, kindOf(node), rangeOf(uri, node)) } - is MethodDeclarationStatement -> { - SymbolInfo.of(node.name, SymbolKind.METHOD, rangeOf(uri, node)) - } + is MethodDeclarationStatement -> SymbolInfo.of(node.name, SymbolKind.METHOD, rangeOf(uri, node)) - is PropertyDeclarationStatement -> { - SymbolInfo.of(node.name, SymbolKind.PROPERTY, rangeOf(uri, node)) - } + is PropertyDeclarationStatement -> SymbolInfo.of(node.name, SymbolKind.PROPERTY, rangeOf(uri, node)) is TypedefStatement -> { // LSP has no alias kind; represent the written type declaration as a type. @@ -124,9 +118,7 @@ internal object XdkSymbols { } } - else -> { - null - } + else -> null } /** diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt index d1ab94d2d49..da36eb94917 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSyntaxCompletions.kt @@ -167,13 +167,9 @@ internal object XdkSyntaxCompletions { val newline = newlines.find(text)?.value ?: "\n" val words = when (context) { - Context.FILE -> { - fileKeywords - } + Context.FILE -> fileKeywords - Context.TYPE -> { - typeKeywords - } + Context.TYPE -> typeKeywords Context.STATEMENT -> { val enclosing = @@ -222,6 +218,7 @@ internal object XdkSyntaxCompletions { } } + /** Syntax node and its ancestors used to determine visible declarations and completion context. */ private data class Entry( val node: AstNode, val ancestors: List, @@ -262,17 +259,11 @@ internal object XdkSyntaxCompletions { ): DeclarationName? { fun base(node: TypeExpression): String? = when (node) { - is NamedTypeExpression -> { - node.nameToken?.valueText - } + is NamedTypeExpression -> node.nameToken?.valueText - is FunctionTypeExpression -> { - "fn" - } + is FunctionTypeExpression -> "fn" - is BiTypeExpression -> { - "value" - } + is BiTypeExpression -> "value" is ArrayTypeExpression -> { node @@ -291,9 +282,7 @@ internal object XdkSyntaxCompletions { ?.let(::base) } - else -> { - null - } + else -> null } if (type is VariableTypeExpression) { val suggestion = @@ -322,9 +311,7 @@ internal object XdkSyntaxCompletions { } } - else -> { - null - } + else -> null } return suggestion?.let { DeclarationName(it, "written $type initializer") } } @@ -364,6 +351,7 @@ internal object XdkSyntaxCompletions { ) } + /** Syntactic location that determines which keyword templates can be offered. */ private enum class Context( val description: String, ) { @@ -372,6 +360,7 @@ internal object XdkSyntaxCompletions { STATEMENT("statement"), } + /** Keyword completion with plain-text and snippet forms for one syntactic context. */ private data class Template( val context: Context, val keyword: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt index 03f4e5b6f6c..d9254a6e8f9 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkTypeMoves.kt @@ -5,6 +5,7 @@ import java.io.File /** Plan qualification changes from resolved type identities, never from matching source text. */ internal object XdkTypeMoves { + /** Type-move edits, resource moves and dependency changes awaiting a complete compiler proof. */ data class Proposal( val edits: Map>, val resources: Map, @@ -24,6 +25,7 @@ internal object XdkTypeMoves { } } + /** Expected module and declaration path after moving a source type. */ data class Destination( val source: String, val module: String, @@ -199,6 +201,7 @@ internal object XdkTypeMoves { return resolved.second + implicit.map { it.name } } + /** Original type identity paired with the proposed owner path, file moves and rename plan. */ private data class Move( val owner: TypePath, val module: String, @@ -209,9 +212,7 @@ internal object XdkTypeMoves { private fun directory(identity: ProofIdentity): File? = when (identity) { - is ProofIdentity.Directory -> { - File(identity.path) - } + is ProofIdentity.Directory -> File(identity.path) is ProofIdentity.Source -> { identity.location.sourceName @@ -222,8 +223,6 @@ internal object XdkTypeMoves { }?.let { File(it.parentFile, it.nameWithoutExtension) } } - else -> { - null - } + else -> null } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt index 3ce8ee50765..bc1d35a5938 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkWorkspaceDiscovery.kt @@ -18,6 +18,7 @@ internal object XdkWorkspaceDiscovery { private val excluded = setOf(".git", ".gradle", ".idea", ".vscode-test", "build", "out", "node_modules") + /** Source header and imports discovered without requiring a successful module compilation. */ data class Header( val text: String, val module: String?, @@ -25,6 +26,10 @@ internal object XdkWorkspaceDiscovery { val incomplete: Boolean, ) + /** + * Immutable inventory of discovered source headers. Source changes produce a new catalog so a queued + * query can retain a consistent view. + */ class Catalog( private val files: Map = emptyMap(), ) { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt index b56c0fdae35..5a6a430fc56 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/index/WorkspaceIndexer.kt @@ -57,8 +57,10 @@ class WorkspaceIndexer( private val parseLock = Any() private val closed = AtomicBoolean() - // Owned by parseLock. Identity distinguishes reads across edits, closes and disk events; - // a later disk read must also supersede an earlier read of the same closed file. + /** + * Identity token for one open-buffer or disk-read revision, owned by the parse lock. Even two reads of + * the same closed file must have distinct tokens so late results cannot replace newer ones. + */ private class Revision( val open: Boolean, ) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt index 2692dd9c4e9..39364316ae7 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientRefresh.kt @@ -11,6 +11,7 @@ import java.util.concurrent.atomic.AtomicReference internal class ClientRefresh( private val client: () -> LanguageClient?, ) : AutoCloseable { + /** Client presentation with its own negotiated refresh capability and request slot. */ enum class Feature { DIAGNOSTICS, TOKENS, @@ -37,6 +38,10 @@ internal class ClientRefresh( features.filter { it in enabled.get() }.forEach { slots.getValue(it).request() } } + /** + * Coalesces refresh requests for one feature while allowing a change during an in-flight refresh to + * schedule another. + */ private inner class Slot( private val feature: Feature, ) { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt index 0223678622d..f4039b41e57 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ClientTrace.kt @@ -13,6 +13,7 @@ import java.util.concurrent.atomic.AtomicReference internal class ClientTrace( private val client: () -> LanguageClient?, ) : AutoCloseable { + /** Protocol trace verbosity negotiated with the client through initialize and setTrace. */ private enum class Level( val value: String, ) { diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt index 75ea8efffc7..40f2546f974 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/CodeLensSettings.kt @@ -2,6 +2,7 @@ package org.xvm.lsp.server import com.google.gson.GsonBuilder +/** Validates reference-CodeLens configuration, retaining the enabled default when the setting is absent. */ internal object CodeLensSettings { fun references(raw: Any?): Boolean { val value = GsonBuilder().serializeNulls().create().toJsonTree(raw) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt index 78df4458cbc..4c929ab5fd0 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ConnectionProgress.kt @@ -34,6 +34,10 @@ internal class ConnectionProgress( CompletableFuture().completeOnTimeout(null, 10, SECONDS) }, ) : AutoCloseable { + /** + * Progress title and cancelable request future, with optional detail text evaluated when reporting + * progress. + */ private data class Entry( val title: String, val result: CompletableFuture<*>, @@ -215,7 +219,7 @@ internal class ConnectionProgress( try { dispatcher.execute(block) } catch (_: RejectedExecutionException) { - // Connection already closed. + // A late completion or cancellation callback raced with dispatcher shutdown. } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt index 0e29dfee3f0..b3f185108f2 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/DiagnosticReports.kt @@ -19,6 +19,7 @@ import java.util.UUID /** Called under the document lifecycle lock. IDs belong to this server and one document's items. */ internal class DiagnosticReports { + /** Diagnostic items and their result ID, retained for unchanged pull-diagnostic responses. */ private data class Entry( val id: String, val diagnostics: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt index cdde8a03799..a29808dc174 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/EditorFormattingState.kt @@ -6,6 +6,7 @@ import java.util.concurrent.atomic.AtomicReference /** A late configuration reply cannot undo a newer preference or restore a reset value. */ internal class EditorFormattingState : AutoCloseable { + /** Formatting preference and revision updated atomically, including the terminal closed state. */ private data class Snapshot( val revision: Long = 0, val config: FormattingConfig? = null, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt index 287f4f65660..de0c1c6f967 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/LspJsonOptions.kt @@ -26,13 +26,9 @@ internal object LspJsonOptions { ): List { val value = lookup(raw, key) ?: return emptyList() return when (value) { - is List<*> -> { - value.mapNotNull { it?.toString()?.trim()?.takeIf(String::isNotEmpty) } - } + is List<*> -> value.mapNotNull { it?.toString()?.trim()?.takeIf(String::isNotEmpty) } - is JsonArray -> { - value.mapNotNull { stringOrNull(it) } - } + is JsonArray -> value.mapNotNull { stringOrNull(it) } is JsonElement -> { if (value.isJsonArray) { @@ -42,9 +38,7 @@ internal object LspJsonOptions { } } - else -> { - emptyList() - } + else -> emptyList() } } @@ -70,17 +64,9 @@ internal object LspJsonOptions { .takeUnless { it.isJsonNull } ?.let { when { - it.isJsonPrimitive && it.asJsonPrimitive.isNumber -> { - it.asInt - } - - it.isJsonPrimitive && it.asJsonPrimitive.isString -> { - it.asString.toIntOrNull() - } - - else -> { - null - } + it.isJsonPrimitive && it.asJsonPrimitive.isNumber -> it.asInt + it.isJsonPrimitive && it.asJsonPrimitive.isString -> it.asString.toIntOrNull() + else -> null } } } @@ -109,17 +95,9 @@ internal object LspJsonOptions { .takeUnless { it.isJsonNull } ?.let { when { - it.isJsonPrimitive && it.asJsonPrimitive.isBoolean -> { - it.asBoolean - } - - it.isJsonPrimitive && it.asJsonPrimitive.isString -> { - it.asString.toBooleanStrictOrNull() - } - - else -> { - null - } + it.isJsonPrimitive && it.asJsonPrimitive.isBoolean -> it.asBoolean + it.isJsonPrimitive && it.asJsonPrimitive.isString -> it.asString.toBooleanStrictOrNull() + else -> null } } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt index 90d80dd17d5..64cc0efb4ff 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/PartialResults.kt @@ -16,6 +16,7 @@ internal class PartialResults( private val dispatcher: ExecutorService = Executors.newSingleThreadExecutor(Thread.ofVirtual().name("lsp-partial-results").factory()), ) : AutoCloseable { + /** Batches to publish as partial results and the final response remaining after those batches. */ data class Plan( val batches: List, val remainder: T, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt index 3fffc4e406e..86a6806559d 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolLifecycle.kt @@ -11,6 +11,7 @@ import java.util.concurrent.atomic.AtomicReference /** Enforces the wire lifecycle without restricting direct embedding/test use of server services. */ internal class ProtocolLifecycle { + /** Connection phase used to validate the ordering of wire requests and notifications. */ private enum class Phase { NEW, INITIALIZING, @@ -19,6 +20,7 @@ internal class ProtocolLifecycle { SHUTDOWN, } + /** Current wire phase and the initialize request ID needed to recognize its response. */ private data class State( val phase: Phase, val initialization: Either? = null, @@ -65,9 +67,7 @@ internal class ProtocolLifecycle { (message.method != "shutdown" || state.compareAndSet(previous, State(Phase.SHUTDOWN))) } - else -> { - false - } + else -> false } if (permitted) { next.consume(message) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt index b973f8d8b11..e7715a89c8d 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ProtocolTrace.kt @@ -110,9 +110,7 @@ internal class ProtocolTrace( ) } - is DidCloseTextDocumentParams -> { - mapOf("uri" to params.textDocument.uri) - } + is DidCloseTextDocumentParams -> mapOf("uri" to params.textDocument.uri) is PublishDiagnosticsParams -> { mapOf( @@ -122,9 +120,7 @@ internal class ProtocolTrace( ) } - else -> { - emptyMap() - } + else -> emptyMap() } val span = ExecutionTrace.span( @@ -142,9 +138,7 @@ internal class ProtocolTrace( } } - else -> { - next.consume(message) - } + else -> next.consume(message) } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt index 40e5c840d62..c337d5209cc 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ReadOnlyDocuments.kt @@ -24,6 +24,7 @@ internal class ReadOnlyDocuments( private val adapter: Adapter, private val client: () -> LanguageClient?, ) : AutoCloseable { + /** Connection readiness and published library documents, including which documents the client has fetched. */ private data class State( val enabled: Boolean = false, val ready: Boolean = false, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt index ab91e00201e..4f684b74e5a 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/RenameProposal.kt @@ -10,6 +10,10 @@ data class RenameProposal( val scope: RenameScope? = null, ) +/** + * Expected and proposed source graphs; clients apply the replacement only if the expected graph is still + * current. + */ data class SourceGraphReplacement( val before: List, val after: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt index 9bdbf39023c..3982746e316 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/ResolveReports.kt @@ -6,11 +6,19 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseError import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode import java.util.UUID -/** Bounded detached results, owned by one connection and guarded by the document lifecycle lock. */ +/** + * Retains deferred response data behind opaque handles for completion, code-action and other resolve + * requests. Entry and character budgets bound retention; oversized values must be returned eagerly. + * + * One connection owns the cache, and callers hold the document lifecycle lock. Resolution checks the + * document revision and item label so an expired or stale handle cannot apply data from another request. + * Stored values must be detached from compiler attempts to avoid retaining ASTs and constant pools. + */ internal class ResolveReports( private val limit: Int = 512, private val characterLimit: Int = 2_000_000, ) { + /** Deferred response data tied to a document revision, with its size tracked against the cache budget. */ private data class Entry( val revision: Long, val label: String, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt index 97f35cf5d76..a3fad68962e 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/SemanticTokenReports.kt @@ -14,6 +14,7 @@ internal class SemanticTokenReports( private val maximumReports: Int = 128, private val maximumIntegers: Int = 1_000_000, ) { + /** Previously returned token data retained for a later delta request on the same document. */ private data class Report( val uri: String, val data: List, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt index 79a932b3cf0..a7eac9c9bab 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServer.kt @@ -126,6 +126,7 @@ class XtcLanguageServer( override fun setTrace(params: SetTraceParams): Unit = clientTrace.configure(params.value) + /** Negotiated support for versioned edits, resource renames and file-watch registrations. */ private data class EditCapabilities( val versioned: Boolean = false, val renameFiles: Boolean = false, @@ -193,6 +194,7 @@ class XtcLanguageServer( internal val supportsFileRenames: Boolean get() = editCapabilities.get().renameFiles + /** Negotiated diagnostic transport and refresh features for this client connection. */ private data class DiagnosticCapabilities( val pull: Boolean = false, val related: Boolean = false, @@ -225,6 +227,7 @@ class XtcLanguageServer( val closed: Boolean = false, ) + /** Response fields the client permits the server to populate lazily through resolve requests. */ private data class ResolveCapabilities( val completionDocumentation: Boolean = false, val actionEdit: Boolean = false, @@ -275,6 +278,7 @@ class XtcLanguageServer( internal fun resolveWorkspaceSymbol(symbol: WorkspaceSymbol): CompletableFuture = textDocumentService.resolveWorkspaceSymbol(symbol) + /** Negotiated semantic-token range, delta and refresh support. */ private data class TokenCapabilities( val range: Boolean = false, val delta: Boolean = false, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt index af597d88d5f..78be6e935e3 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcLanguageServerLauncher.kt @@ -74,23 +74,17 @@ internal enum class AdapterBackend( fun fromSetting(setting: String? = null): AdapterBackend = when (setting?.lowercase()) { - "mock" -> { - MOCK - } + "mock" -> MOCK "treesitter", "tree-sitter", - -> { - TREE_SITTER - } + -> TREE_SITTER null, "compiler", "xtc", "full", - -> { - COMPILER - } + -> COMPILER else -> { throw IllegalArgumentException( @@ -135,9 +129,7 @@ private fun createAdapter(requested: AdapterBackend): Pair { - MockAdapter() to AdapterBackend.MOCK - } + AdapterBackend.MOCK -> MockAdapter() to AdapterBackend.MOCK } fun main( diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt index f3860a0ef24..d7abf087205 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt @@ -161,6 +161,7 @@ class XtcTextDocumentService( private val logger = LoggerFactory.getLogger(XtcTextDocumentService::class.java) } + /** Open-buffer revision and the analysis future associated with that exact content and module scope. */ private class Document( val content: String, val version: Int, @@ -319,9 +320,7 @@ class XtcTextDocumentService( synchronized(lifecycle) { if (!result.isDone) { when { - stale() -> { - result.completeExceptionally(contentModified()) - } + stale() -> result.completeExceptionally(contentModified()) failure != null -> { val cause = diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt index 65e93f73149..46344afc341 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/SemanticTokenEncoder.kt @@ -299,9 +299,7 @@ class SemanticTokenEncoder { child.text == "inject" || child.text == "delegate" || child.text == "readonly" || - child.text == "immutable" -> { - emitToken(child, "modifier", 0) - } + child.text == "immutable" -> emitToken(child, "modifier", 0) } } } diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt index 246247523e4..2efdf849715 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/treesitter/XtcQueryEngine.kt @@ -165,9 +165,7 @@ class XtcQueryEngine( "method_declaration", "function_declaration", "constructor_declaration", - -> { - findParameterInMethod(current, name) - } + -> findParameterInMethod(current, name) "class_body", "module_body", @@ -243,9 +241,7 @@ class XtcQueryEngine( "method_declaration", "function_declaration", "constructor_declaration", - -> { - enumerateParameters(current, uri) - } + -> enumerateParameters(current, uri) "class_body", "module_body", diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt index afac344518e..026afef2674 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ExecutionTrace.kt @@ -9,6 +9,7 @@ import java.util.concurrent.atomic.AtomicLong /** Timing metadata only: never logs source buffers, protocol payloads or compiler objects. */ internal object ExecutionTrace { + /** Operation identity, parent and monotonic start time propagated across asynchronous work. */ data class Span( val id: Long, val parent: Long?, @@ -18,6 +19,7 @@ internal object ExecutionTrace { val created: Long = System.nanoTime(), ) + /** Active trace span and API nesting depth for the current scoped execution. */ private data class Context( val span: Span, val apiDepth: Int, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt index 6430e8fdd5c..67474812486 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ProgressLabels.kt @@ -13,13 +13,9 @@ internal object ProgressLabels { val root = roots.mapNotNull(::path).filter { source?.startsWith("$it/") == true }.maxByOrNull { it.length } val label = when { - source.isNullOrEmpty() -> { - uri.substringBefore('?').substringBefore('#') - } + source.isNullOrEmpty() -> uri.substringBefore('?').substringBefore('#') - root != null -> { - source.removePrefix("$root/") - } + root != null -> source.removePrefix("$root/") else -> { source diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt index 25eb3dcd9af..b5d0f22e090 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/ServerLogs.kt @@ -18,6 +18,11 @@ import java.util.zip.ZipEntry import java.util.zip.ZipOutputStream import kotlin.jvm.optionals.getOrNull +/** + * Validated limits for server log rolling and closed-session cleanup. File and total-size limits configure + * each rolling appender; age also limits its history. Age and session count govern closed-session pruning, + * which preserves live sessions even when those retention targets are exceeded. + */ internal data class LogRetention( val historyDays: Int = 7, val maxFileMb: Int = 10, @@ -41,7 +46,14 @@ internal data class LogRetention( } } -/** Owned session files only. Never traverses source roots, other applications' logs, or symlinks. */ +/** + * Locates, prunes and exports this server's logs and execution traces. Each process gets its own + * session directory; concurrent servers share only the root and the lock used for pruning. + * + * Cleanup removes closed sessions only when every entry is a recognized regular log file. Unknown + * files, symlinks and live processes keep a directory intact. Export reads bounded tails from the + * current session and configured trace directory, so a support bundle cannot grow with the log files. + */ internal object ServerLogs { val process = TraceProcessId().propertyValue val root: Path = @@ -68,7 +80,10 @@ internal object ServerLogs { "exportFileLimitBytes" to FILE_LIMIT, ) - /** Active sessions are protected, including a PID with an unknown start time. */ + /** + * Removes expired or excess closed sessions and returns the number deleted. A held retention lock + * makes this call a no-op; active processes, including a PID with an unknown start time, are kept. + */ fun prune( root: Path = this.root, policy: LogRetention = retention, @@ -134,6 +149,7 @@ internal object ServerLogs { return actualStart == null || actualStart.toEpochMilli() == started } + /** Packages recent log tails and the supplied service status as a bounded, base64-encoded ZIP. */ fun export(status: Map): Map { val files = listOf(directory, traceDirectory) diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt index fdfa86b0bfe..b09e5c09469 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/util/TraceProcessId.kt @@ -3,7 +3,10 @@ package org.xvm.lsp.util import ch.qos.logback.core.PropertyDefinerBase import java.time.Instant -/** Keep concurrent IDE servers and test JVMs in separate trace files, including PID reuse. */ +/** + * Supplies Logback with a process identity made from the PID and process start timestamp. Session and trace + * paths use it to separate concurrent IDE servers and test JVMs, and to distinguish later PID reuse. + */ class TraceProcessId : PropertyDefinerBase() { override fun getPropertyValue(): String = ProcessHandle.current().let { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt index 14bb4ea783f..a8930833a8e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/AdapterFuturesTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import java.util.concurrent.CompletableFuture +/** + * Verifies cancellation propagation through analysis and dependent proof futures. A canceled request must not + * start later proof work, and cancellation during future construction must reach the newly created work. + */ class AdapterFuturesTest { @Test fun `cancellation before analysis completes does not start proof`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt index 9eea3e372c5..8ddfef235c7 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBoundaryRequirementsTest.kt @@ -26,6 +26,11 @@ import java.util.IdentityHashMap import java.util.UUID import java.util.concurrent.Executors +/** + * Exercises the compiler contracts needed by a detached editor model using emitted and reloaded modules. + * Checks executable generic bindings, dependency replacement and identity separation across modules and + * overloads. + */ class CompilerBoundaryRequirementsTest { @ParameterizedTest @ValueSource(strings = ["id", "&id", "pick", "&pick"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt index aaeb4c8a4dc..66d056be9a1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerCallSiteTest.kt @@ -19,6 +19,11 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.semanticSnapshot import java.util.concurrent.Executors +/** + * Checks facts recorded at actual compiler call selection, including generic inference and named arguments. + * Dynamic function values retain signatures without invented method targets, and invalid argument fitting + * cannot publish a selected call. + */ class CompilerCallSiteTest { @Test fun `function valued calls expose typed signatures without inventing method targets`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt index 1ad207b5818..db8d3763613 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt @@ -10,6 +10,11 @@ import org.xvm.compiler.Source import org.xvm.tool.ModuleInfo import java.nio.file.Path +/** + * Exercises declaration-only embedding independently of full compilation. Verifies cancellation and source + * identity, and prevents syntax/declaration failures from exposing a supposedly queryable graph or executable + * result. + */ class CompilerDeclarationAnalysisTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt index 3820a3d7288..f37d30b11ef 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerExtractMethodFactsTest.kt @@ -8,6 +8,10 @@ import org.xvm.compiler.Source import org.xvm.lsp.adapter.xdk.XdkDependencies import org.xvm.lsp.adapter.xdk.projectRenameFacts +/** + * Checks the compiler evidence used to capture inputs for extracted methods. Distinguishes stable values from + * writes and reference storage while keeping ordinary rename analysis free of extraction-only data. + */ class CompilerExtractMethodFactsTest { @Test fun `capture evidence distinguishes stable values from writes and reference storage`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt index ef37976eb33..12f208dfa66 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMethodExtractionProofTest.kt @@ -11,6 +11,10 @@ import org.xvm.lsp.adapter.xdk.XdkMethodExtraction import org.xvm.lsp.adapter.xdk.XdkRename import org.xvm.lsp.adapter.xdk.projectRenameFacts +/** + * Challenges method-extraction proposals that still compile but rebind a capture or call. Ensures successful + * compilation alone is insufficient to authorize the transformation. + */ class CompilerMethodExtractionProofTest { @Test fun `compiling helper with swapped arguments fails capture proof`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt index 812c92d00aa..2883fff5443 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt @@ -15,6 +15,11 @@ import org.xvm.lsp.adapter.xdk.projectRenameFacts import org.xvm.lsp.adapter.xdk.renameFacts import org.xvm.lsp.adapter.xdk.toDependency +/** + * Challenges generated method signatures and argument bindings after imports, relocation and cross-module + * insertion. Compatible but different types or rebound arguments must fail proof even when the proposed + * source compiles. + */ class CompilerMissingMethodProofTest { @Test fun `imports shift the inserted method but cannot weaken its source signature proof`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt index d1e26633e6c..7a682b2bef2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt @@ -20,6 +20,11 @@ import java.nio.ByteBuffer import java.nio.file.Files import java.nio.file.Path +/** + * Exercises public embedding diagnostics with invalid roots, dependency failures and fallback repositories. + * Protects source errors from being mislabeled as internal failures and preserves useful repository causes + * without poisoning successful fallback. + */ class EmbeddingDiagnosticsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt index 35acd8d924b..2345dd08476 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAdapterLifecycleTest.kt @@ -11,7 +11,15 @@ import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS +/** + * Controls compiler execution to force edit, close and reopen races. Obsolete attempts may finish, but must + * neither publish stale document views nor prevent queued edits from coalescing. + */ class XdkAdapterLifecycleTest { + /** + * Holds the first compilation until the test releases it, deliberately ignoring cancellation to expose + * stale-result races. + */ private class PausedCompiler { val started = CountDownLatch(1) val release = CountDownLatch(1) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt index 3860673e3ee..d162feccb15 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkAnonymousConstructorTest.kt @@ -18,6 +18,10 @@ import org.xvm.compiler.ast.TypeCompositionStatement import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.toDependency +/** + * Exercises cursor fitting for anonymous construction without emitting forwarding constructors or capturing + * the body. Checks that only accessible, real constructor candidates are presented. + */ class XdkAnonymousConstructorTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt index a5718355265..1317e4ee9b0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentCompletionTest.kt @@ -16,6 +16,10 @@ import org.xvm.lsp.adapter.xdk.semanticSnapshot import java.nio.file.Path import java.util.concurrent.Executors +/** + * Verifies value completion against compiler argument fitting for positional, named and overloaded calls. + * Suggestions must preserve the exact edited token and must not choose an overload prematurely. + */ class XdkArgumentCompletionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt index 835c6326059..6dba0d40dc6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArgumentContextTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks completion against the entire written call, including arguments after the cursor. A locally + * plausible suggestion must be rejected when the surrounding call cannot fit. + */ class XdkArgumentContextTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt index 25acf518def..873d469d2c6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkArrayDimensionTest.kt @@ -12,6 +12,11 @@ import org.xvm.compiler.Source import org.xvm.compiler.ast.NewExpression import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks array-size completion against the fixed-size constructor's actual size slot. Copy, capacity and + * mutability overloads must not appear as dimension candidates, and unsupported dimensions must retain honest + * lexical behavior. + */ class XdkArrayDimensionTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt index 232a4f8b38e..edb1e26e5b2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkBuildModelTest.kt @@ -18,6 +18,10 @@ import java.net.URI import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Compiles evaluated build-model inputs across source sets and projects. Checks dependency visibility, + * processed resources, member imports and replacement of imported binary paths. + */ class XdkBuildModelTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt index d5e0f52157c..df1e04bf682 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCallHierarchyTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Verifies hierarchy edges from compiler-selected calls with exact source ownership. Covers overloads, + * recursion, lambdas and closed members while refusing to invent runtime targets for dynamic calls. + */ class XdkCallHierarchyTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt index e06019f87a1..06c710ac4b9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkColorValueTest.kt @@ -13,6 +13,11 @@ import org.xvm.lsp.adapter.xdk.toDependency import java.nio.file.Path import kotlin.io.path.readText +/** + * Exercises the opt-in literal color-constructor experiment and source-preserving picker edits. Unrelated + * types, dynamic expressions and stale documents must not acquire swatches or rewriteable color + * presentations. + */ class XdkColorValueTest { @Test fun `fixture exposes initializers and reordered named channels only when enabled`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt index 9a478f24344..cf55f6be8f9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompletionSignatureTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks member completion and signature help against resolved receiver types and incomplete calls. Preserves + * overload alternatives, accessibility and argument positions without inventing a selected invocation. + */ class XdkCompletionSignatureTest { @Test fun `member completion preserves overloads substitutes receiver types and excludes private members`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt index 4449fd3267e..9172e1598a1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCompoundHeaderTest.kt @@ -13,6 +13,10 @@ import org.xvm.compiler.ast.NamedTypeExpression import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises type completion inside damaged compound declaration headers. Ensures bounded recovery preserves + * written syntax and restores compilation after the exact suggested edit. + */ class XdkCompoundHeaderTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt index 52349cfa27c..be62208be7e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkConfigurationReplacementTest.kt @@ -14,6 +14,10 @@ import java.nio.file.Path import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks reuse and retirement of module analyses when source graphs, build inputs or binaries change. + * Independent successful buffers remain usable while changed dependencies invalidate their consumers. + */ class XdkConfigurationReplacementTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt index fa2c058ab21..92fffef1921 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossModuleMissingMethodsTest.kt @@ -18,6 +18,10 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.attribute.PosixFilePermission +/** + * Exercises method generation into configured dependency modules, including closed companions and imported + * signature types. Refuses edits that would invent ownership or introduce an invalid reverse dependency. + */ class XdkCrossModuleMissingMethodsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt index b3861851b67..8c6e7cf4d2d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCrossOwnerMissingMethodsTest.kt @@ -12,6 +12,10 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.attribute.PosixFilePermission +/** + * Checks that unresolved receiver calls create methods in the compiler-selected source owner. Signature + * identities, visibility and instance/static dispatch must survive editing a closed destination. + */ class XdkCrossOwnerMissingMethodsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt index 8e8cc3cbda4..5a8ce28bcda 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorPresentationTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Verifies documentation, ordering and active-parameter metadata on compiler cursor results. Repeated + * analyses must preserve source documentation and named/default argument mappings. + */ class XdkCursorPresentationTest { @Test fun `completion carries source documentation and stable ordering across fresh attempts`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt index 53b949273d1..4c07c51b01c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCursorRequestTest.kt @@ -19,9 +19,14 @@ import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS +/** + * Controls partial analysis to force request supersession and cancellation. Cursor work may not publish after + * an edit, and retiring it must leave ordinary compilation diagnostics intact. + */ class XdkCursorRequestTest { @TempDir lateinit var directory: Path + /** Holds one cursor analysis beyond cancellation so late results can be checked against request ownership. */ private class PausedCursor { val started = CountDownLatch(1) val release = CountDownLatch(1) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt index 997183d10c3..a017f141973 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkCyclicRenameTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks rename through recursive and dynamic delegation. Written contracts must move together without + * inventing executable implementation locations for unresolved runtime dispatch. + */ class XdkCyclicRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt index 05cff24e5db..9ffa2fc15e2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt @@ -14,6 +14,10 @@ import org.xvm.compiler.ast.partial.IncompleteDeclarationStatement import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Exercises malformed declaration headers while retaining their written names, ranges and following + * declarations. Recovery must not register incomplete method or property components as valid declarations. + */ class XdkDeclarationHeaderTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt index d7aed47ffbd..d2b42066fab 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationTest.kt @@ -8,6 +8,10 @@ import org.xvm.lsp.model.Location import java.net.URI import java.nio.file.Path +/** + * Distinguishes Go to Declaration from implementation selection. Checks written contracts, lexical locals, + * import aliases and closed consumers, and prevents failed analyses from reusing old targets. + */ class XdkDeclarationTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt index 155e5d9ccf9..534187020a0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelegatedRenameTest.kt @@ -8,6 +8,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Path +/** + * Checks rename across nested generic delegation and module boundaries. Written contracts and implementations + * must remain one proven family after serialization and source edits. + */ class XdkDelegatedRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt index ae8287dd4bf..77eb885781a 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDelimiterRecoveryTest.kt @@ -16,6 +16,10 @@ import org.xvm.lsp.adapter.xdk.semanticSnapshot import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises completion and signature help with missing enclosing delimiters. Recovery must preserve the + * innermost real context without inventing separators or altering surrounding source. + */ class XdkDelimiterRecoveryTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt index 24612480acf..e49485e5f6a 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDependencyTest.kt @@ -21,6 +21,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Checks navigation and identity across emitted dependency artifacts and source attachments. Recompilation + * and replacement must preserve only matching declarations and retire obsolete accessor or overload targets. + */ class XdkDependencyTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt index 3adf2319b23..c73868a5cd1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticIndexTest.kt @@ -13,6 +13,10 @@ import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicInteger import kotlin.time.TimeSource +/** + * Checks reusable diagnostics for closed project roots and independent pull consumers. Dependency edits + * invalidate only affected builds, while cancellation belongs to one reader rather than the shared analysis. + */ class XdkDiagnosticIndexTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt index f2709ae493d..5be7e54a929 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDiagnosticTest.kt @@ -11,6 +11,11 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.model.Location import java.util.concurrent.TimeUnit.SECONDS +/** + * Verifies diagnostic source ownership and UTF-16 positions for local, foreign and unpositioned reports. + * Unexpected compiler failures must remain internal failures rather than masquerading as configuration + * problems. + */ class XdkDiagnosticTest { @Test fun `binary only structure diagnostics keep the document fallback`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt index 1e53ff58f7d..bf9ceed9dd6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDocumentationTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks generated documentation comments against real declarations and compiler parameter names. Preserves + * tabs, line endings and existing comments while rejecting bodies, locals and ambiguous same-line sites. + */ class XdkDocumentationTest { @ParameterizedTest @ValueSource(strings = ["\n", "\r\n", "\r"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt index 3e06ec0b49c..3d56b07a12e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEditingTest.kt @@ -6,6 +6,10 @@ import org.xvm.lsp.adapter.xdk.SemanticModel import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename +/** + * Exercises formatting, links, linked editing and run lenses through the compiler adapter. Checks live + * settings and exact source preservation, including CRLF, literal contents and UTF-16 link ranges. + */ class XdkEditingTest { @Test fun `live editor settings reach every formatter and reset to request options`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt index 378fa3b2565..70fbb5c5fe0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkEnclosingValueCompletionTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks completion of enclosing instances against actual compiler scope and argument fitting. Accepted edits + * must compile and must not expose an unrelated or inaccessible receiver. + */ class XdkEnclosingValueCompletionTest { @Test fun `shared enclosing fixtures accept their exact edits and compile`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt index b4b66fa7b41..780acbb6462 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkExternalRenameTest.kt @@ -14,6 +14,10 @@ import java.io.File import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises rename proofs spanning explicit external roots and unsaved consumers. Missing or changed captured + * inputs must invalidate proof instead of silently narrowing the claimed rename scope. + */ class XdkExternalRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt index 3329f5dc9f0..d8c4fc0f191 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileChangesTest.kt @@ -12,6 +12,10 @@ import org.xvm.lsp.adapter.xdk.XdkSources import java.nio.file.Files import java.nio.file.Path +/** + * Checks whether late filesystem notifications still describe the inputs already compiled. Identical events + * can retain current work; real source, membership or resource changes must retire stale results. + */ class XdkFileChangesTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt index 2034609f4e5..a1eb55beefd 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFileOperationsTest.kt @@ -10,6 +10,10 @@ import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises compiler-proven file, package and module moves as combined graph edits. Checks reference/resource + * preservation and distinguishes edits supplied by the server from physical operations owned by the host. + */ class XdkFileOperationsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt index eba0f1944bb..024bc6c48b8 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFoldingTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Protects method folding boundaries around closing braces and adjacent declarations. Damaged syntax must not + * make a fold consume the following declaration. + */ class XdkFoldingTest { @ParameterizedTest @ValueSource(strings = ["\n", "\r\n"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt index 2da2aec3917..1d5257c3b7e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkFormattingBreadthTest.kt @@ -11,6 +11,10 @@ import org.xvm.lsp.adapter.xdk.XdkLexical import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Exercises compiler formatting across syntax and range boundaries. Checks idempotence, configured width, + * line endings and unchanged literal contents, including malformed lexemes and on-type edits. + */ class XdkFormattingBreadthTest { @ParameterizedTest @ValueSource(strings = ["\n", "\r\n", "\r"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt index 8b96af25968..7ecd33f3059 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGenericHeaderTest.kt @@ -11,6 +11,10 @@ import org.xvm.compiler.Source import org.xvm.compiler.ast.NamedTypeExpression import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises completion within incomplete generic type headers. Accepting a base name must preserve written + * arguments and delimiters rather than replacing the surrounding type expression. + */ class XdkGenericHeaderTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt index fcadab9da59..5901340839d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkGraphMoveTest.kt @@ -12,6 +12,11 @@ import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks source-graph and resource ownership during container relocation. A proposal must preserve + * dependencies and resource values, including disabled or missing roots, rather than succeeding against the + * old filesystem layout. + */ class XdkGraphMoveTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt index 09f80e93662..84fdeb2203c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkHeaderSlotsTest.kt @@ -11,6 +11,10 @@ import org.xvm.compiler.Source import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises cursor positions in declaration type slots. Checks visible compiler scope and exact replacement + * boundaries without publishing incomplete declaration identities. + */ class XdkHeaderSlotsTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt index 97d1f1baae5..2d58eb1da06 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionLifecycleTest.kt @@ -12,6 +12,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Forces retirement while import-completion proof is pending. Neither the import edit nor ordinary completion + * items may escape from the obsolete request. + */ class XdkImportCompletionLifecycleTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt index 9588740d46d..eaa73a07235 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkImportCompletionTest.kt @@ -7,6 +7,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Path +/** + * Checks atomic completion/import edits against actual dependency visibility. Whole-token replacement must + * compile without inventing dependencies, conflating same-named types or hiding unrelated errors. + */ class XdkImportCompletionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt index 746671232b5..5cbd686631d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteCallTest.kt @@ -7,6 +7,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks overload candidates for unfinished calls against all written arguments. Retains inferred expected + * types and named mappings while refusing invalid or duplicate bindings. + */ class XdkIncompleteCallTest { @Test fun `written argument types filter overloads without selecting an unfinished call`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt index bf980a6d40d..7470a7d04c1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteConstructorTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks constructor candidates in partial source using normal accessibility and type inference. Incomplete + * input must not select an overload or offer constructors unavailable to ordinary compilation. + */ class XdkIncompleteConstructorTest { @Test fun `constructor signatures fit written arguments without selecting an overload`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt index 93c9f687455..970e65447ac 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIncompleteFunctionTest.kt @@ -9,6 +9,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises signature and argument queries for function-valued parameters, properties and narrowed locals. + * Uses the current callable type without inventing a method declaration target. + */ class XdkIncompleteFunctionTest { @ParameterizedTest @ValueSource(strings = ["fn(", "fn(1", "fn(1, "]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt index b242c969d87..9bbe0b82a67 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkIndexLifecycleTest.kt @@ -14,6 +14,10 @@ import org.xvm.lsp.adapter.xdk.semanticSnapshot import org.xvm.lsp.adapter.xdk.toDependency import java.nio.file.Path +/** + * Checks replacement of dependency and source indices without losing detached semantic facts. Old hierarchy + * handles must expire when the underlying graph or source authority changes. + */ class XdkIndexLifecycleTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt index f20fa1e73a2..2e2cc375bc0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInferredPresentationTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks inferred lambda and destructuring hints from compiler types. Explicit annotations suppress redundant + * hints, and captures must not be mistaken for lambda parameters. + */ class XdkInferredPresentationTest { @Test fun `lambda parameter and return hints use the function signature without captures`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt index 0323f1e6c7f..fa1dc449aea 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInitializerTest.kt @@ -9,6 +9,10 @@ import org.xvm.lsp.treesitter.SemanticTokenLegend import java.nio.file.Files import java.nio.file.Path +/** + * Protects source facts erased by constant folding. A folded initializer must still participate in hover, + * references, tokens and complete compiler-validated rename. + */ class XdkInitializerTest { @Test fun `folded initializer participates in hover navigation references tokens and complete rename`( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt index 3bf82c6b43e..3d32ab35d8b 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkInlineCompletionTest.kt @@ -8,6 +8,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkInlineCompletions import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises compiler-backed inline suggestions for names, members and literals. Checks fitting and explicit- + * trigger behavior in complete and incomplete source without relying on text-only guesses. + */ class XdkInlineCompletionTest { @ParameterizedTest @ValueSource(strings = ["consume(ans§);", "consume(ans§", "consume(value = ans§);", "Int result = ans§;"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt index 4a913f6cafb..c2b84c9b403 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLambdaRenameTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks lambda parameter rename through captures, nesting and argument/return contexts. Refuses name capture + * even when the resulting program would still compile. + */ class XdkLambdaRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt index 5f9856ba78d..aeb00b3da32 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLexicalTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkLexical import org.xvm.lsp.treesitter.SemanticTokenLegend +/** + * Checks lexical token projection used alongside semantic highlighting. Preserves documentation modifiers and + * UTF-16 ranges, including large inputs without repeated whole-source scans. + */ class XdkLexicalTest { @ParameterizedTest @ValueSource(strings = ["\n", "\r\n", "\r"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt index cdc627e12f1..7cb581cae6e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibrariesTest.kt @@ -14,6 +14,10 @@ import java.nio.file.Files import java.nio.file.Path import java.nio.file.attribute.PosixFilePermission +/** + * Exercises the production bundled XDK library set. Confirms modules resolve without an external installation + * and bundled source remains navigable, read-only and outside rename/replacement authority. + */ class XdkLibrariesTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt index fa3ffebc8c9..7cf07e11039 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLibraryDocumentsTest.kt @@ -7,6 +7,10 @@ import java.net.URI import java.nio.file.Files import java.nio.file.Path +/** + * Checks revision-owned library source documents and monikers in virtual and file-backed views. Navigation + * must find declarations independently of which symbols a consumer previously queried. + */ class XdkLibraryDocumentsTest { @Test fun `matching bundled sources have revision owned content and monikers in file and virtual views`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt index b839b88f3af..7616d3fc7cf 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLinkedEditingTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename +/** + * Checks linked ranges for lexical lambda parameters and explicit aliases. Callable parameters and member + * contracts must use graph rename rather than unsafe local linked editing. + */ class XdkLinkedEditingTest { @ParameterizedTest @ValueSource(strings = ["(Int input) -> input", "input -> input"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt index 006e042e367..a3e3862fdef 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralCompletionTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks proposed literals with compiler fitting and compilation of the accepted edit. Covers positional and + * named slots so syntactically plausible but incompatible values are not offered. + */ class XdkLiteralCompletionTest { @ParameterizedTest @ValueSource(strings = ["Int", "String", "Boolean", "String?"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt index 30f58462de6..fa9a8845242 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiteralRecoveryTest.kt @@ -13,6 +13,10 @@ import org.xvm.compiler.Source import org.xvm.compiler.ast.MethodDeclarationStatement import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises completion around unfinished literal containers and declaration values. Preserves subsequent + * declarations and the inner argument context without fabricating literal contents. + */ class XdkLiteralRecoveryTest { @ParameterizedTest @ValueSource( diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt index 77021524bb2..742284da49a 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLiveWorkspaceTest.kt @@ -12,6 +12,10 @@ import org.xvm.lsp.adapter.xdk.toDependency import java.nio.file.Path import java.util.concurrent.atomic.AtomicInteger +/** + * Exercises automatic workspace discovery as buffers, folders and closed files change. Unsaved source takes + * precedence, explicit graphs retain authority and failed graphs cannot leave stale semantic answers visible. + */ class XdkLiveWorkspaceTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt index 9fdd18ca551..bff7037772d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalExtractionTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks extraction of expressions into locals with exact edits and compiler binding comparison. Preserves + * contextual types, evaluation and captured references rather than accepting compilation success alone. + */ class XdkLocalExtractionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt index 311fad7201a..4dc27f8ca99 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInitializerActionsTest.kt @@ -10,6 +10,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Exercises local extraction in initializer contexts. Preserves contextual types, comments and evaluation + * order, and refuses partial or ambiguous selections that lack a safe insertion contract. + */ class XdkLocalInitializerActionsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt index fc443fef41d..a59650c3a40 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalInlineTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks local inlining against evaluation count, contextual type and binding preservation. Unsafe reads, + * writes, intervening effects or broken graphs must prevent publication. + */ class XdkLocalInlineTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt index 84fd9c4b537..f99918734d2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkLocalRemovalTest.kt @@ -10,6 +10,11 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks removal of unused compiler constants while preserving comments and surviving bindings. Runtime + * initializers, storage annotations, reads and writes must prevent an apparently unused declaration from + * being deleted. + */ class XdkLocalRemovalTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt index a09fc3c5f5f..416e44d0a27 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionRenameTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Runs rename against real manual-test mixin compositions. Protects covariant and conditional contract + * families from touching unrelated compositions or accepting colliding defaults. + */ class XdkManualCompositionRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt index 7cc748e0db4..bf173c8d34f 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkManualCompositionTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Exercises implementation navigation with real manual-test mixin and delegation sources. Navigation must + * reach written bodies without inventing a runtime receiver target. + */ class XdkManualCompositionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt index 5788630fe4e..a26f81825a2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt @@ -13,6 +13,10 @@ import org.xvm.lsp.adapter.xdk.XdkDependency import org.xvm.lsp.adapter.xdk.toDependency import java.nio.file.Path +/** + * Checks generated implementations and overrides against compiler-selected member contracts. Unsupported + * recursive signatures must not hide independent actions or weaken proof for the generated member. + */ class XdkMemberActionsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt index 19f740eb1b9..97843bd35da 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberInlineTest.kt @@ -10,6 +10,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks bounded private-member inlining while retaining the declaration. Preserves evaluation and bindings, + * and refuses capture, recursive bodies and unsupported runtime initializer behavior. + */ class XdkMemberInlineTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt index 38c9765ce05..ab799678b61 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMethodExtractionTest.kt @@ -15,6 +15,10 @@ import org.xvm.lsp.adapter.xdk.XdkSourceModule import org.xvm.lsp.adapter.xdk.toDependency import java.nio.file.Path +/** + * Checks generated helper methods, signatures and captured inputs through trial compilation. Preserves + * contextual types, overload selection, receiver ownership and exact source trivia. + */ class XdkMethodExtractionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt index 43244711a83..669b68e8171 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationNameTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.PartialSemanticModel import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises suggestions for unwritten declaration names using the written type. Missing names must not become + * registered compiler identities before the user accepts an edit. + */ class XdkMissingDeclarationNameTest { @Test fun `missing property and parameter names use written types without registering bindings`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt index 3b3485921e8..0a49aa759aa 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingDeclarationsTest.kt @@ -10,6 +10,11 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks bounded class and property creation from unresolved source uses. Rejects unknown ownership, + * unsupported constructor requirements and unrelated errors instead of guessing a declaration that merely + * compiles. + */ class XdkMissingDeclarationsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt index 7e9d6d607b4..5566bdeb83e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMissingMethodsTest.kt @@ -11,6 +11,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Exercises method generation from unresolved calls using compiler-derived signature types. Refuses types + * without a proven destination spelling and verifies instance/static and value/void contracts. + */ class XdkMissingMethodsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt index 72feba196ed..21aefc2f297 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkModuleSessionTest.kt @@ -15,6 +15,11 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Exercises whole-module snapshots with unsaved members, implicit packages and resources. Diagnostics and URI + * aliases must remain attached to their real source while any member edit invalidates the shared module + * analysis. + */ class XdkModuleSessionTest { @Test fun `structure warnings belong to a closed member declaration and follow its overlay`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt index 9581d9e32b6..0d8050f50e0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMonikerTest.kt @@ -16,6 +16,11 @@ import org.xvm.lsp.adapter.xdk.compiledSemantics import java.nio.file.Files import java.nio.file.Path +/** + * Checks portable symbol identities across recompilation, checkout relocation and dependency replacement. + * Overloads and versions stay distinct; private or unresolved declarations cannot acquire invented exported + * artifact identities. + */ class XdkMonikerTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt index 011ba9c5017..e7221edbfcc 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMoveInputsTest.kt @@ -7,6 +7,10 @@ import org.xvm.lsp.adapter.xdk.XdkMoveInputs import java.nio.file.Files import java.nio.file.Path +/** + * Checks captured resource inputs used to authorize moves. Changed bytes, newly introduced members and + * symbolic links must invalidate ownership even when ordinary metadata appears unchanged. + */ class XdkMoveInputsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt index 8ae33462167..6f168f176be 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationIndexTest.kt @@ -19,6 +19,10 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.atomic.AtomicInteger import kotlin.time.TimeSource +/** + * Verifies reuse of detached navigation facts by exact source and dependency inputs. Rebuilds only affected + * roots and detects resource-content changes even when timestamps match. + */ class XdkNavigationIndexTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt index 30d36efcdd9..820cf54e639 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkNavigationTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.model.Location +/** + * Checks source navigation, references and highlights by compiler identity rather than name. Covers + * shadowing, reused register slots and narrowed views of the same declaration. + */ class XdkNavigationTest { @Test fun `a local definition points to its declaration name and excludes it from ordinary references`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt index 81f2efa85f2..39d2dcbfdfa 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkOperandCompletionTest.kt @@ -5,6 +5,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises completion of missing operands within larger expressions. Suggestions must fit the original + * argument and surrounding expression, preserving exact source edits and rejecting incompatible candidates. + */ class XdkOperandCompletionTest { @ParameterizedTest @ValueSource( @@ -108,6 +112,7 @@ class XdkOperandCompletionTest { } } +/** Supplies matching method, constructor and function argument contexts for operand-completion tests. */ private object XdkArgumentContextFixture { const val HEADER = "module Operands { class Pair(Int number, String text) {} " + diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt index 7c9ad72abe0..c09a503ca53 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkParameterRenameTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks method and constructor parameter rename across named callers, overloads and overrides. Uses the + * compiler's visible parameter slots and refuses capture or unrelated same-named parameters. + */ class XdkParameterRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt index 61abc31c718..747bb2a3f50 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationScaleTest.kt @@ -7,6 +7,10 @@ import org.xvm.lsp.adapter.xdk.XdkPresentation import org.xvm.lsp.treesitter.SemanticTokenLegend import java.util.UUID +/** + * Checks detached semantic presentation on large models. Protects lexical gaps, non-overlapping token ranges + * and half-open hint boundaries without requiring an editor process. + */ class XdkPresentationScaleTest { @Test fun `semantic coverage preserves adjacent lexical tokens and excludes nested overlaps`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt index 4df4cf15eba..a64d8ceb417 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPresentationTest.kt @@ -7,6 +7,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.treesitter.SemanticTokenLegend +/** + * Verifies compiler-derived hover, token and inlay presentation with source-accurate ranges. Covers Unicode, + * writes and recovery so semantic overlays do not corrupt lexical highlighting. + */ class XdkPresentationTest { @ParameterizedTest @ValueSource(strings = ["plain", "😀"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt index 6a2451a7686..f87a3a037e7 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPrimaryParameterRenameTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks the relationship between shorthand constructor parameters, named arguments and generated properties. + * All proven source uses rename together while collisions and local capture remain refused. + */ class XdkPrimaryParameterRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt index 98425dc7c3c..7361fa5de82 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectGraphTest.kt @@ -8,6 +8,10 @@ import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Files import java.nio.file.Path +/** + * Checks that dependency watch roots include unopened source modules and missing resource directories. + * Ensures later file creation can invalidate the same graph that compilation uses. + */ class XdkProjectGraphTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt index f8c2735ee73..abdf49fce50 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectProofMemoryTest.kt @@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicBoolean class XdkProjectProofMemoryTest { @TempDir lateinit var directory: Path + /** Enumerates proof-query exit paths that must all release temporary compiler state. */ enum class Outcome { SUCCESS, REJECTED, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt index 88eeafa1dd6..a7f6785459c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryLifecycleTest.kt @@ -17,15 +17,21 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Forces changes and cancellation during project queries. Open overlays, closed disk inputs and graph + * ownership must stay current until publication; trial results must not become normal editor analyses. + */ class XdkProjectQueryLifecycleTest { @TempDir lateinit var directory: Path + /** Selects the project query whose ownership and stale-result behavior is exercised. */ enum class Feature { REFERENCES, CODE_LENSES, RENAME, } + /** Enumerates changes that must retire an outstanding project query. */ private enum class Change { EDIT, CLOSE, @@ -218,6 +224,7 @@ class XdkProjectQueryLifecycleTest { } } + /** Owns a real adapter and a two-module fixture with controllable compilation delays. */ private inner class Session : AutoCloseable { val library = directory diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt index 3656c484d05..7a104da35bd 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectQueryTest.kt @@ -16,6 +16,10 @@ import org.xvm.lsp.adapter.xdk.toDependency import java.io.File import java.nio.file.Path +/** + * Checks cross-module queries against declared dependency visibility and serialized identities. Closed + * consumers contribute facts without allowing unrelated roots to supply otherwise undeclared imports. + */ class XdkProjectQueryTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt index b8132d056be..50ec741af23 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkProjectTest.kt @@ -18,6 +18,10 @@ import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean import java.util.concurrent.atomic.AtomicInteger +/** + * Exercises dependency-ordered module compilation and caching under rapid edits. Reuse requires exact + * captured inputs; obsolete or uncooperative compilations cannot publish stale dependency artifacts. + */ class XdkProjectTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt index d442e077867..4dbaf8f57c9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPropertyArgumentCompletionTest.kt @@ -16,6 +16,10 @@ import org.xvm.lsp.adapter.xdk.semanticSnapshot import java.nio.file.Path import java.util.concurrent.Executors +/** + * Checks property-value suggestions using compiler readability, narrowing and argument fitting. Accepted + * edits must preserve the original token boundaries and ordinary validation rules. + */ class XdkPropertyArgumentCompletionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt index 0d07aaae325..25ec4031a10 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkQualifiedHeaderTest.kt @@ -14,6 +14,10 @@ import org.xvm.compiler.ast.NamedTypeExpression import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks qualified type-prefix completion in damaged headers. Only accessible nested types are offered, and + * replacement is limited to the final identifier rather than the qualifier. + */ class XdkQualifiedHeaderTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt index 34de5fea11e..87ee4a56847 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRecoveryTest.kt @@ -12,6 +12,10 @@ import org.xvm.lsp.adapter.xdk.semanticSnapshot import org.xvm.tool.ModuleInfo import java.nio.file.Path +/** + * Checks recovered source structure separately from successful compilation. Outlines, folding and selection + * must reflect the current damaged buffer without reusing stale semantic answers. + */ class XdkRecoveryTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt index fce44da8826..80803e60bc7 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkReferenceLensTest.kt @@ -9,6 +9,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Path +/** + * Checks reference lenses across closed consumers and overloads, excluding declarations from counts. Also + * verifies documentation actions in configured projects without changing disk inputs during analysis. + */ class XdkReferenceLensTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt index fae4805bdae..f5a1f61af62 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRenameTest.kt @@ -10,6 +10,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicInteger +/** + * Challenges renames with capture, overload and named-argument cases. Successful recompilation is necessary + * but insufficient: every surviving binding must retain its intended identity. + */ class XdkRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt index 3acc41af521..15485c598c9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceInputsTest.kt @@ -10,6 +10,10 @@ import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises conventional and explicit resource roots, including absent directories and same-timestamp edits. + * Only affected source consumers should recompile, and repaired resources must recover diagnostics. + */ class XdkResourceInputsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt index 12034120e9a..23e5d5ec22d 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkResourceRenameTest.kt @@ -11,6 +11,10 @@ import java.net.URI import java.nio.file.Files import java.nio.file.Path +/** + * Checks source and companion-resource relocation during file and module rename. Forward and reverse + * operations must preserve imports, resource ownership and exact source contents. + */ class XdkResourceRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt index 11038fdc3ff..cd297defdd0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSafeDeleteTest.kt @@ -10,6 +10,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks safe deletion of bounded private declarations. Public consumers, surviving references, + * initialization effects and comment loss must prevent removal despite an apparently unused local view. + */ class XdkSafeDeleteTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt index 93582b35fd9..681a5403775 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkScopeCompletionTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks visible names at empty and incomplete statement positions. Uses actual compiler scope and assignment + * state while leaving the original source unchanged during inspection. + */ class XdkScopeCompletionTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt index f91b204d004..15f02406ac4 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticColorTest.kt @@ -15,6 +15,10 @@ import org.xvm.lsp.treesitter.SemanticTokenLegend import java.nio.file.Path import kotlin.io.path.readText +/** + * Protects semantic highlighting improvements over the lexical grammar. Resolved identity controls annotation + * and library classification, and damage/repair must preserve lexical gaps without invented semantics. + */ class XdkSemanticColorTest { @Test fun `shared highlighting improvements preserve lexical gaps through damage and repair`() { @@ -278,6 +282,7 @@ class XdkSemanticColorTest { } } + /** Decodes token text, classification and modifiers for exact semantic-highlighting assertions. */ private data class Token( val line: Int, val column: Int, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt index 069e98c2fd4..623972672f3 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSemanticLookupTest.kt @@ -15,6 +15,10 @@ import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS import java.net.URI as SourceUri +/** + * Checks navigation from inferred and specialized types to their real declarations. Generic returns, + * covariant overrides and unrelated same-named types must not collapse into a textual lookup. + */ class XdkSemanticLookupTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt index a1903d1123c..cb9e0b38af2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSourceLinksTest.kt @@ -9,6 +9,10 @@ import org.xvm.lsp.adapter.xdk.XdkLibrarySources import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks import links and explicit alias ranges from resolved compiler identities. Wildcards resolve their + * actual container while unsupported syntax produces diagnostics rather than guessed paths. + */ class XdkSourceLinksTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt index ae5552d5eec..4d5e2a1779c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSpecializedConstructorTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Exercises partial queries for specialized constructors using ordinary compiler contexts. Preserves expected + * types, implicit parents and source edits instead of substituting a simpler constructor model. + */ class XdkSpecializedConstructorTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt index 3bfe4a1b079..c84ace9ab4e 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkStructuralTest.kt @@ -8,6 +8,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkRename import java.nio.file.Path +/** + * Checks current outlines, folds and strictly nested selection ranges in shared damaged-source fixtures. + * Structure must recover without stale nodes or duplicate selection spans. + */ class XdkStructuralTest { @ParameterizedTest @ValueSource(strings = ["\n", "\r\n", "\r"]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt index 63270ae6dc2..47d9601d5b6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkSyntaxCompletionTest.kt @@ -7,6 +7,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSyntaxCompletions import java.util.concurrent.CancellationException +/** + * Exercises syntax-derived names and templates at valid declaration or statement positions. Checks exact + * replacement ranges and excludes contexts where a template would corrupt existing source. + */ class XdkSyntaxCompletionTest { @Test fun `declaration names use written types and replace the entire identifier`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt index 0699d96f363..637ed1cb0b9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeHeaderTest.kt @@ -14,6 +14,10 @@ import org.xvm.compiler.ast.partial.IncompleteTypeCompositionStatement import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks visible type suggestions in unfinished composition headers without registering a partial class. + * Accepting a suggestion must edit only the prefix and restore ordinary compilation diagnostics. + */ class XdkTypeHeaderTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt index b05591f1c85..8daa12d6b2f 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkTypeMoveTest.kt @@ -12,6 +12,10 @@ import java.nio.file.Files import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks type relocation between packages with closed consumers and companion resources. Qualification and + * imports must preserve resolved identities rather than rewriting every matching name. + */ class XdkTypeMoveTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt index cd47d127c93..0b2024dbf12 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionDelegationRenameTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks rename through unions nested in generic and recursive delegation. Every possible written contract + * and receiver remains part of the proof, including alternatives hidden behind generated dispatch. + */ class XdkUnionDelegationRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt index d2eb592cc17..2fdbdb45ccf 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkUnionRenameTest.kt @@ -10,6 +10,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Path +/** + * Checks rename across all possible union-call targets and closed consumers. Reordered union legs or shared + * spellings must not hide a changed binding. + */ class XdkUnionRenameTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt index 5a0d254d997..529d6e47176 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkValueTemplateTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Checks lambda templates against expected callable arity in ordinary, named and constructor arguments. + * Generated scaffolds must preserve contextual typing and exact insertion syntax. + */ class XdkValueTemplateTest { @Test fun `lambda templates fit expected arity and compile in named ordinary and constructor arguments`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt index fead0dd3ff5..bc00b7aefc0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceDiscoveryTest.kt @@ -10,6 +10,10 @@ import org.xvm.lsp.adapter.xdk.XdkWorkspaceDiscovery import java.nio.file.Path import java.util.concurrent.CancellationException +/** + * Exercises source-module discovery, import graphs and open-buffer precedence. Healthy modules remain + * queryable beside damaged ones, generated sources are excluded and explicit configuration retains authority. + */ class XdkWorkspaceDiscoveryTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt index 141469595f8..6f9bcbaf695 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceNavigationTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.api.io.TempDir import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path +/** + * Checks implementations, subtype relationships and call hierarchy across unopened consumers. Closed-file + * changes must expire old hierarchy handles even without timely watcher delivery. + */ class XdkWorkspaceNavigationTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt index a3b7344eb60..20bc775ce87 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWorkspaceRefactoringTest.kt @@ -9,6 +9,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import org.xvm.lsp.adapter.xdk.XdkSourceModule import java.nio.file.Path +/** + * Checks graph-wide edits through unopened consumers with full compiler proof. Binary-only targets, broken + * graphs and unsafe import cleanup cannot yield source refactoring edits. + */ class XdkWorkspaceRefactoringTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt index 2bf68dda42c..fed4c297e66 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkWrittenFormalTest.kt @@ -6,6 +6,10 @@ import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.adapter.xdk.XdkAdapter +/** + * Exercises incomplete written generic bounds without inventing formal identities. Recursive suggestions + * require a real guarding type structure and must not expose invalid sibling or direct cycles. + */ class XdkWrittenFormalTest { private val uri = "untitled:Formals.x" diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt index 286391d2b30..b6882e7d6a5 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/AdapterBackendTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatIllegalArgumentException import org.junit.jupiter.api.Test +/** + * Checks bundled defaults, startup overrides and supported adapter aliases. Unknown values must fail + * explicitly rather than silently selecting a mock or different backend. + */ class AdapterBackendTest { @Test fun `startup selection overrides the bundled default and rejects invalid overrides`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt index 2e6e9537e42..551be2a57d1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CapabilityNegotiationTest.kt @@ -49,6 +49,10 @@ import org.xvm.lsp.adapter.SignatureHelp as AdapterSignatureHelp import org.xvm.lsp.adapter.TextEdit as AdapterEdit import org.xvm.lsp.adapter.WorkspaceEdit as AdapterWorkspaceEdit +/** + * Exercises optional protocol fields against minimal and fully capable clients. Tooltip and signature + * metadata must honor negotiation in both eager responses and later resolution. + */ class CapabilityNegotiationTest { @Test fun `link tooltips honor missing false and true support on eager and resolved results`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt index 7f6adc9711a..a00e29a7ffe 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientNotificationsTest.kt @@ -17,6 +17,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises negotiated refresh and runtime trace behavior under refusal and disconnect. Coalescing must + * retain a later change, and tracing must not expose request payloads or continue after closure. + */ internal class ClientNotificationsTest { @ParameterizedTest @EnumSource(ClientRefresh.Feature::class) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt index 93c4e26c6df..eb8ca9818e8 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ClientPresentationTest.kt @@ -33,6 +33,10 @@ import org.xvm.lsp.model.CompilationResult import org.xvm.lsp.model.Location import org.xvm.lsp.model.SymbolInfo +/** + * Checks protocol presentation for clients with reduced capabilities. Avoids unnegotiated requests and + * metadata while preserving UTF-16 positions, plain hover and legacy symbol forms. + */ class ClientPresentationTest { @Test fun `minimal client receives no unnegotiated configuration or refresh requests`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt index 4e166389d1e..45df8e09665 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CodeLensSettingsTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Protects the enabled reference-lens default and strict boolean configuration. Missing preferences remain + * valid while malformed overrides fail instead of silently changing behavior. + */ class CodeLensSettingsTest { @Test fun `references default on and accept only an explicit boolean override`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt index d1104a45795..ac9ee2c7957 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ColorValueProtocolTest.kt @@ -27,6 +27,10 @@ import org.xvm.lsp.adapter.CompilerTestSupport import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises opt-in color requests at the protocol boundary. Checks current document ranges and channel + * validation, and verifies disabled providers reject requests without disturbing analysis. + */ class ColorValueProtocolTest { @Test fun `color provider requires opt in and disabled requests report method not found`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt index c3a55c9ca54..72517acf695 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerConfigurationTest.kt @@ -32,6 +32,11 @@ import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises compiler configuration replacement and presentation-only updates through the server. Preserves + * accepted inputs and cached work when settings do not change compilation, and keeps explicit graph ownership + * distinct from discovery. + */ class CompilerConfigurationTest { @Test fun `unchanged library settings preserve discovered resource inputs and cached compilation`() { @@ -336,6 +341,7 @@ class CompilerConfigurationTest { } } + /** Owns a compiler server and captures configuration requests and published diagnostics for one test. */ private inner class Session( pull: Boolean = false, ) : AutoCloseable { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt index cd9224be61b..bdedff78a49 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/CompilerLibrariesTest.kt @@ -17,6 +17,10 @@ import java.net.URI import java.nio.file.Files import java.nio.file.Path +/** + * Checks ordered binary libraries and attached navigation sources through server configuration. Replacement + * and removal retire old facts while inheritance, empty paths and bundled-module ownership remain distinct. + */ class CompilerLibrariesTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt index 60cb0a4b92e..3ec8bc17cae 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ConnectionProgressTest.kt @@ -18,7 +18,12 @@ import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicReference +/** + * Controls progress creation acknowledgements, completion, cancellation and close races. Each token must own + * only its request, report live details and finish once without canceling shared compiler work. + */ class ConnectionProgressTest { + /** Controls client acknowledgements, reporting and expiry independently of real-time scheduling. */ private class Session : AutoCloseable { val dispatcher = Executors.newSingleThreadExecutor() val client = mock(LanguageClient::class.java) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt index 257a44b3a58..67b361549c5 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DiagnosticPresentationTest.kt @@ -11,6 +11,10 @@ import org.junit.jupiter.params.provider.ValueSource import org.xvm.lsp.model.Diagnostic import org.xvm.lsp.model.Location +/** + * Checks pull-diagnostic capability negotiation independently of push diagnostics. Related information and + * related-document results must obey the client features that apply to their own response path. + */ class DiagnosticPresentationTest { @ParameterizedTest @ValueSource(booleans = [false, true]) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt index 68f00d2317b..a948a6ca368 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/DocumentSynchronizationTest.kt @@ -35,6 +35,10 @@ import org.xvm.lsp.adapter.Position as AdapterPosition import org.xvm.lsp.adapter.Range as AdapterRange import org.xvm.lsp.adapter.TextEdit as AdapterTextEdit +/** + * Exercises sequential UTF-16 patches and save edits against exact document snapshots. Invalid ranges, stale + * versions and conflicting edits must fail without mutating the server buffer or waiting for compilation. + */ class DocumentSynchronizationTest { private val uri = "file:///Sync.x" private val id = TextDocumentIdentifier(uri) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt index 68fe1388517..0da1d6eb4fc 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/EditorFormattingStateTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Forces out-of-order configuration replies around reset and disconnect. Late or malformed settings must not + * replace the latest valid editor formatting snapshot. + */ class EditorFormattingStateTest { @Test fun `disconnect rejects delayed configuration and requests created after close`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt index 578bd044fd5..5a4f6216b2c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ExecutionTraceTest.kt @@ -20,6 +20,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks trace ownership across queued, nested and asynchronous operations. Cancellation must not pretend + * work stopped, and protocol spans must distinguish request direction and end at actual reply delivery. + */ class ExecutionTraceTest { @Test fun `queue lists preserve order and cancellation does not pretend running work stopped`() { @@ -191,6 +195,7 @@ class ExecutionTraceTest { } } + /** Attaches a temporary trace appender and detaches it on close to isolate captured JSON events. */ private class Recording : AutoCloseable { val entries = CopyOnWriteArrayList() private val logger = LoggerFactory.getLogger("org.xvm.lsp.trace") as Logger diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt index 70d36b1bf68..7feb1c408b4 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/FileChangeSnapshotsTest.kt @@ -8,6 +8,10 @@ import org.junit.jupiter.api.io.TempDir import java.nio.file.Files import java.nio.file.Path +/** + * Checks deduplication of file-operation and watcher events using content snapshots. Equal timestamps cannot + * hide changed bytes, and folder notifications must not trigger tree scans on the notification thread. + */ class FileChangeSnapshotsTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt index 2057812241e..a5b65d8cbcf 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/IndexingProgressTest.kt @@ -20,6 +20,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks progress labels and ownership for real background indexing lifetimes. Cancellation must belong to + * the announced scan or query rather than an unrelated request. + */ class IndexingProgressTest { @Test fun `query progress identifies the requested source or workspace`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt index 02817503640..5691a453b53 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/InlineCompletionProtocolTest.kt @@ -31,6 +31,10 @@ import org.xvm.lsp.adapter.InlineCompletionContext as AdapterContext import org.xvm.lsp.adapter.Position as AdapterPosition import org.xvm.lsp.adapter.Range as AdapterRange +/** + * Checks independently negotiated inline completion, including trigger and selected-item context. Edits and + * cancellation must retire a query before any stale suggestion is published. + */ class InlineCompletionProtocolTest { @Test fun `inline completion negotiates independently and round trips selection trigger and plain edits`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt index 3fbaa3a488d..9294365ed60 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt @@ -31,6 +31,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises connection-owned virtual library content and refresh. Only registered current artifact URIs may + * be read; client-provided paths or presentations cannot replace authoritative source content. + */ class LibraryContentProtocolTest { @Test fun `library content is negotiated per connection and cannot read unregistered or retired URIs`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt index d247e30a605..555161990de 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/MonikerProtocolTest.kt @@ -26,6 +26,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks portable symbol IDs and optional partial-result streaming on the wire. Edited or canceled requests + * must retire their work without publishing stale or partial identities. + */ class MonikerProtocolTest { @Test fun `monikers use protocol kinds scheme uniqueness and optional partial results`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt index e02babc7aec..00e8e9a92d8 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/PartialResultsTest.kt @@ -20,6 +20,10 @@ import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Checks bounded streaming before the final protocol response. Every item is delivered once, while + * cancellation, stale ownership, disconnect and backend failure prevent later batches from escaping. + */ class PartialResultsTest { @Test fun `partial batches precede an empty final response and preserve every item once`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt index e18ff727c47..2f0a9978353 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProgressLabelsTest.kt @@ -4,6 +4,10 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.xvm.lsp.util.ProgressLabels +/** + * Checks that progress text identifies the useful source or workspace path. Nested roots must select the most + * specific owner so users can identify the work they are canceling. + */ class ProgressLabelsTest { @Test fun `source labels retain useful paths and select the most specific workspace root`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt index 144b7677eaa..8daa7c17a39 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ProtocolLifecycleTest.kt @@ -10,6 +10,10 @@ import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode import org.eclipse.lsp4j.jsonrpc.messages.ResponseMessage import org.junit.jupiter.api.Test +/** + * Exercises initialize, initialized and shutdown ordering at the message boundary. Failed initialization may + * retry, but duplicate or premature requests must not bypass lifecycle rules. + */ class ProtocolLifecycleTest { @Test fun `failed initialization can retry and late client replies survive shutdown`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt index d14cefd0d14..6ff580f7f3c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/RequestOwnershipTest.kt @@ -14,6 +14,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicInteger +/** + * Separates reader cancellation from shared analysis ownership. Closing or canceling a request must retire it + * immediately even if the underlying compilation ignores cancellation and another reader still needs it. + */ class RequestOwnershipTest { @Test fun `canceling a request awaiting analysis does not cancel another reader or shared analysis`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt index 9dc055c6db8..a4776969d50 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResolveReportsTest.kt @@ -5,6 +5,10 @@ import org.assertj.core.api.Assertions.assertThat import org.assertj.core.api.Assertions.assertThatThrownBy import org.junit.jupiter.api.Test +/** + * Checks opaque handles for bounded deferred response data. Foreign, stale, tampered or evicted handles must + * fail rather than resolving an item from another document revision. + */ class ResolveReportsTest { @Test fun `handles preserve detached values and reject foreign stale tampered and evicted requests`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt index 530098b0f39..2c9a414a4a1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/ResourceFileWatchersTest.kt @@ -17,6 +17,10 @@ import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Forces stalled and failed watcher registration/removal replies. The queue must recover, IDs must not be + * reused while retirement is pending, and disconnect must release all outstanding work. + */ class ResourceFileWatchersTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt index 40b12683aa1..36ad313e6e1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/SemanticTokenReportsTest.kt @@ -5,6 +5,10 @@ import org.eclipse.lsp4j.Position import org.eclipse.lsp4j.Range import org.junit.jupiter.api.Test +/** + * Checks full/range/delta token responses and bounded history. Reconstructed deltas must match exact token + * arrays; foreign or expired result IDs fall back to full responses. + */ class SemanticTokenReportsTest { @Test fun `delta reconstructs insertions removals integer edits and unchanged results`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt index d5e9d7d6ad3..7195ba27cda 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/StartupRegistrationTest.kt @@ -18,6 +18,10 @@ import org.mockito.Mockito.`when` import org.xvm.lsp.adapter.mock.MockAdapter import java.util.concurrent.CompletableFuture +/** + * Verifies that dynamic watcher registration waits for initialization and negotiated client support. Prevents + * startup traffic that a minimal or not-yet-ready client cannot handle. + */ class StartupRegistrationTest { @Test fun `watcher requests wait for initialized and require negotiated dynamic registration`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt index af0b30d959d..837a07c0749 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCallbackServerTest.kt @@ -23,7 +23,12 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.Executors import java.util.concurrent.TimeUnit.SECONDS +/** + * Forces compiler completion and transport cancellation around document locks. Callbacks must release the + * compiler worker before publication so cancellation cannot deadlock behind a reader. + */ class XdkCallbackServerTest { + /** Selects analysis or query completion when reproducing compiler callback races. */ enum class Callback { ANALYSIS, QUERY, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt index 85cd8ccf1be..a0840f1e4f3 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorServerTest.kt @@ -42,7 +42,12 @@ import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicInteger +/** + * Controls asynchronous cursor queries at the server boundary. Ensures typing, cancellation and document + * lifecycle changes retire pending requests without borrowing facts from a different revision. + */ class XdkCursorServerTest { + /** Selects the asynchronous request whose lifecycle is under test. */ enum class Feature { COMPLETION, SIGNATURE, @@ -50,11 +55,13 @@ class XdkCursorServerTest { REFERENCES, } + /** Couples a queued result future with the action that completes it on demand. */ private class Pending( val future: CompletableFuture<*>, val finish: () -> Unit, ) + /** Controls analysis readiness and query completion, including deliberately ignored cancellation. */ private class Backend( val gate: CompletableFuture = CompletableFuture.completedFuture(Unit), val ignoreCancellation: Boolean = false, @@ -139,6 +146,7 @@ class XdkCursorServerTest { ): CompletableFuture> = query(listOf(Location(uri, line, column, line, column + 1))) } + /** Owns the server connection and document lifecycle around the controllable backend. */ private class Session( val backend: Backend, ) : AutoCloseable { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt index 9c9d263fedc..f8bc663f5f1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkCursorWatchTest.kt @@ -26,6 +26,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.ExecutionException import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks watcher events against incomplete-source snapshot inputs. Late directory creation can preserve a + * still-valid cursor result only when the effective inputs are unchanged. + */ class XdkCursorWatchTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt index 84f0ea90be4..e66b9abfb7a 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkDeclarationServerTest.kt @@ -14,6 +14,10 @@ import org.xvm.lsp.adapter.mock.MockAdapter import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks declaration-provider negotiation and conversion of multiple targets to wire locations. Unsupported + * adapters must not advertise or serve guessed declaration results. + */ class XdkDeclarationServerTest { @Test fun `declaration is advertised only by the implementing adapter and preserves multiple wire targets`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt index d3c1e5d642c..e7172fc21aa 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkFeatureResolveProtocolTest.kt @@ -40,6 +40,10 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicReference +/** + * Checks lazy lenses, links, inlays and workspace symbols against document identity and negotiated fields. + * Eager clients get complete results, stale handles expire, and disabling reference lenses preserves Run. + */ class XdkFeatureResolveProtocolTest { @TempDir lateinit var root: Path private val source = diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt index d3c8f0ed227..06a38f38da7 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkLanguageServerTest.kt @@ -39,6 +39,11 @@ import java.util.concurrent.CompletableFuture import java.util.concurrent.LinkedBlockingQueue import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises compiler-backed server behavior across dependency replacement, diagnostics and shutdown. + * Unchanged consumer versions must still refresh after dependency edits, and foreign reports retain their + * actual source ownership. + */ class XdkLanguageServerTest { @Test fun `host dependency replacement republishes diagnostics for unchanged consumer versions`() { @@ -134,6 +139,7 @@ class XdkLanguageServerTest { } } + /** Captures pushed diagnostics and closes the initialized server after each lifecycle test. */ private class Session( adapter: Adapter, ) : AutoCloseable { @@ -184,6 +190,7 @@ class XdkLanguageServerTest { private class PendingAdapter( private val delegate: MockAdapter = MockAdapter(), ) : Adapter by delegate { + /** Retains one compilation's exact input and future until the test chooses its completion order. */ private data class Pending( val uri: String, val content: String, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt index 61f715359f8..8776b590db0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkModuleServerTest.kt @@ -37,6 +37,10 @@ import java.nio.file.Path import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks whole-module analysis through document versions and filesystem notifications. Member diagnostics and + * hierarchy handles must follow their own sources and expire when the shared module snapshot changes. + */ class XdkModuleServerTest { @TempDir lateinit var directory: Path @@ -192,6 +196,7 @@ class XdkModuleServerTest { return root to member } + /** Owns a real compiler server and records diagnostics across whole-module document changes. */ private class Session : AutoCloseable { val server = XtcLanguageServer(XdkAdapter()) val documents = server.textDocumentService diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt index 2c7a2fd401e..eb1991edfba 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkProjectServerTest.kt @@ -37,6 +37,11 @@ import java.nio.file.Path import java.util.concurrent.CopyOnWriteArrayList import java.util.concurrent.TimeUnit.SECONDS +/** + * Exercises dependency/resource edits through server notifications and open-buffer overlays. Failed or + * deleted inputs must retire stale consumer facts; repair and closing an overlay restore the appropriate + * analysis. + */ class XdkProjectServerTest { @TempDir lateinit var directory: Path @@ -345,6 +350,7 @@ class XdkProjectServerTest { return directory.resolve("$name.x").toFile().also { it.writeText(text) } } + /** Configures a real compiler server with dependency fixtures and records consumer diagnostics. */ private class Session( library: File, consumer: File, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt index 3a245fddc15..8698747febe 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkPullDiagnosticsTest.kt @@ -43,6 +43,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicInteger +/** + * Checks negotiated document/workspace pull diagnostics alongside the push fallback. Closed roots can be + * diagnosed without becoming editor buffers, and result IDs must reflect repaired or changed analyses. + */ class XdkPullDiagnosticsTest { @TempDir lateinit var directory: Path @@ -347,6 +351,7 @@ class XdkPullDiagnosticsTest { } } + /** Negotiates pull capabilities and records push reports and refreshes for comparison. */ private class Session( pull: Boolean = true, related: Boolean = true, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt index 13f0ed8a481..40da9123ac2 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt @@ -43,6 +43,10 @@ import java.util.concurrent.CountDownLatch import java.util.concurrent.TimeUnit.SECONDS import java.util.concurrent.atomic.AtomicBoolean +/** + * Checks versioned rename and move proposals at the server boundary. Captured graph and resource inputs + * authorize a proposal but do not apply it, and later changes must invalidate stale proof. + */ class XdkRenameServerTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt index 9d6653b7143..836517d68fb 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkResolveProtocolTest.kt @@ -36,6 +36,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import java.nio.file.Path import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks deferred completion documentation and code-action edits with eager fallbacks. Resolution preserves + * insertion identity and refuses changed inputs instead of applying a stale edit. + */ class XdkResolveProtocolTest { @TempDir lateinit var directory: Path diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt index 39908d8f1ff..08b0ab8a0d4 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSemanticTokenProtocolTest.kt @@ -34,6 +34,10 @@ import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.CompletableFuture import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks negotiated semantic-token delta, refresh and reopen behavior through the compiler server. + * Unsupported request forms must fail without corrupting the full-token response path. + */ class XdkSemanticTokenProtocolTest { @Test fun `compiler token delta survives edits and resets on reopen with negotiated refresh`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt index 0e3bdf505bf..6f8fddcd9b1 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt @@ -112,6 +112,7 @@ import java.util.jar.JarOutputStream */ @Tag("compiler-stdio") class XdkStdioTest { + /** Adds the language-service status request to the packaged server's test-side JSON-RPC proxy. */ interface ServiceStatusServer : LanguageServer { @JsonRequest("xtc/languageServiceStatus") fun languageServiceStatus(): CompletableFuture> @@ -1767,6 +1768,10 @@ class XdkStdioTest { } } + /** + * Owns the packaged server process, protocol connection and captured events, closing them together after + * each test. + */ private class Session( jar: Path, directory: Path, diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt index e6e94118dc6..85f64be4921 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkSyntaxCompletionProtocolTest.kt @@ -20,6 +20,10 @@ import org.mockito.Mockito.mock import org.xvm.lsp.adapter.xdk.XdkAdapter import java.util.concurrent.TimeUnit.SECONDS +/** + * Checks template and name-completion wire edits against snippet and indentation capabilities. Minimal + * clients receive plain text without placeholder leakage, with exact Unicode insertion positions. + */ class XdkSyntaxCompletionProtocolTest { @Test fun `missing declaration names use a plain empty edit at the original Unicode position`() { diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt index 141ea39d396..681eeefffc9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/util/ServerLogsTest.kt @@ -13,6 +13,10 @@ import java.time.Instant import java.util.Base64 import java.util.zip.ZipInputStream +/** + * Exercises session pruning and bounded support archives using real temporary files. Live processes, foreign + * entries and symlinks must remain untouched, and invalid retention limits must fail before cleanup. + */ class ServerLogsTest { @TempDir lateinit var root: Path diff --git a/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts b/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts index 5a1eacbf20c..5c95d1a2e62 100644 --- a/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts +++ b/lang/test-fixtures/compiler-playbook/compiler-import/build.gradle.kts @@ -1,8 +1,10 @@ import java.util.Properties import java.util.concurrent.TimeUnit -// A real Gradle producer with deterministic publication/exit gates. The XTC plugin's evaluated -// model is tested by TestKit; this fixture isolates the two IDEs' import and cancellation lifecycle. +/** + * A real Gradle producer with deterministic publication/exit gates. The XTC plugin's evaluated + * model is tested by TestKit; this fixture isolates the two IDEs' import and cancellation lifecycle. + */ abstract class ExportPlaybookModel : DefaultTask() { @get:Internal abstract val controlDirectory: DirectoryProperty diff --git a/lang/vscode-extension/README.md b/lang/vscode-extension/README.md index 768cbd81620..5247931b23b 100644 --- a/lang/vscode-extension/README.md +++ b/lang/vscode-extension/README.md @@ -441,14 +441,14 @@ X142 verifies constant-initializer navigation and rename/Undo. X143 compares par workspace-symbol batches with the ordinary response. These checks complement the protocol and lifecycle cases; see the [manual playbook](../doc/manual-test-plan.md). -For the L81 visible-control check, select X145 with `-PcompilerPlaybookCancelUi=true`. +For the visible progress-cancellation check, select X145 with `-PcompilerPlaybookCancelUi=true`. The test clicks the actual Cancel button through the isolated Electron renderer using `playwright-core`; it does not move the desktop pointer or download a browser. Shared X259 checks rendered consumer inlays across settings changes and restart. After building, run `node scripts/run-vscode-tests.cjs --project-lifecycle --shared-process` from this directory to check two windows in one Electron process, independent compiler ownership and real hot-exit restoration. Omit `--shared-process` for separate application instances. See the -[manual playbook](../doc/manual-test-plan.md#native-project-lifetime-checks-l81) for the full checks. +[manual playbook](../doc/manual-test-plan.md#native-project-lifetime-checks) for the full checks. Compiler workspace synchronization now has shared X263–X265 cases for nested composite input aggregation, automatic Gradle refresh/export watching, and project/folder closure during import. @@ -517,11 +517,11 @@ Untrusted and virtual workspaces are explicitly unsupported; remote hosts remain ### Experimental color-value picker -The opt-in L77 prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared +The opt-in color-value prototype recognizes literal `ColorPrototype.Rgba` constructors from the shared fixture. It supplies native color swatches and picker edits separately from normal semantic highlighting. It is disabled for ordinary launches and does not introduce a public XDK color API. Shared X273–X275 exercise picker edits, dismissal, native Undo/Redo and diagnostic recovery. -See the [prototype recipe and acceptance](../doc/manual-test-plan.md#l77-color-value-prototype). +See the [prototype recipe and acceptance](../doc/manual-test-plan.md#experimental-color-value-picker). The playbook keeps its startup theme. For explicit theme comparisons, the Node/npm launcher accepts `--playbook --cases=X277 --theme="Default Light Modern"` and the optional `--lexical-baseline`. From cddcc8eba3e7f84f896e5ff9765ef6282b51b209 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:10 +0100 Subject: [PATCH 011/324] Keep unchanged repository read failures cached until file metadata changes - Keep unchanged repository read failures cached until file metadata changes. - Keep repository cache fixes without adding a reporting API. - Verify native repository recovery through existing APIs. - Clarify repository caching and explicit LSP dependency validation. - Keep embedding diagnostics tests independent of repository observers. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 17 ++- .../main/java/org/xvm/asm/DirRepository.java | 88 +++--------- .../main/java/org/xvm/asm/FileRepository.java | 43 ++---- .../org/xvm/asm/DirRepositoryFailureTest.java | 132 +++++++----------- .../xvm/asm/FileRepositoryFailureTest.java | 111 +++++++-------- .../xvm/asm/RepositoryFailureTestSupport.java | 42 ++++++ .../lsp/adapter/EmbeddingDiagnosticsTest.kt | 13 +- .../lsp/adapter/RepositoryReportingTest.kt | 41 +++--- 8 files changed, 215 insertions(+), 272 deletions(-) create mode 100644 javatools/src/test/java/org/xvm/asm/RepositoryFailureTestSupport.java diff --git a/javatools/README.md b/javatools/README.md index a381fed591f..06c4d953bf0 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -115,11 +115,12 @@ result is constant. These are compiler correctness rules, not alternate semantic LSP flag. The separate removal of the abandoned binary AST does not remove the source AST used by tooling; legacy `.xtc` slots are still read or skipped as required for format compatibility. -Repository observation is similarly additive to search behavior: an unreadable candidate can -still be skipped while another repository supplies the module. A host receives a useful cause -when a required dependency ultimately cannot be found. The diagnostic guide below explains the -intentional Java API migrations, the behavior retained for ordinary CLI use and the regression -evidence; it does not claim universal source or binary compatibility for old Java embedding code. +Repository lookup remains tolerant: an unreadable candidate can be skipped while another file +or repository supplies the module. An unrelated failed candidate does not establish why an import +is missing. The LSP validates its explicitly configured dependency bytes before accepting immutable +snapshots; it does not infer dependency errors from repository scans. The diagnostic guide below +explains the intentional Java API migrations, retained CLI behavior and regression evidence; +it does not claim universal source or binary compatibility for old Java embedding code. ## Compiler diagnostics and error listeners @@ -462,8 +463,10 @@ The embedding API combines the listener contract, scoped parser/validation repor outcomes, cached TypeInfo diagnostic replay, cancellation and host-supplied source snapshots. Consumer tests exercise these mechanisms together through the public entry points. -The independent [repository read-failure fix](https://github.com/xtclang/xvm/pull/683) supplies -retained I/O causes; this API forwards them only after a required dependency search fails. +The independent [repository cache correction](https://github.com/xtclang/xvm/pull/683) preserves +tolerant lookup while reusing unchanged successful and failed candidates. It adds no reporting API +and does not attribute a missing dependency to an unrelated unreadable file. Hosts that require +validation of specific input files can read those files before constructing their repository. [The parser/branch correction](https://github.com/xtclang/xvm/pull/684) supplies the parent-abort, default-budget and lexical-rollback behavior preserved here. Empty transient-local retention is an independent utility fix. None of these APIs depends on an LSP implementation or editor. diff --git a/javatools/src/main/java/org/xvm/asm/DirRepository.java b/javatools/src/main/java/org/xvm/asm/DirRepository.java index 0112788e980..77828efc49e 100644 --- a/javatools/src/main/java/org/xvm/asm/DirRepository.java +++ b/javatools/src/main/java/org/xvm/asm/DirRepository.java @@ -16,13 +16,10 @@ import java.util.Collections; import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; import java.util.TreeMap; -import java.util.function.BiConsumer; - /** * A simple ModuleRepository that manages its contents in a directory. */ @@ -53,29 +50,10 @@ public class DirRepository * @param fReadOnly true to make the repository "read-only" */ public DirRepository(File dir, boolean fReadOnly) { - this(dir, fReadOnly, DirRepository::reportReadFailure); - } - - /** - * Construct a repository with an observer for unsuccessful candidate reads. - * - *

The observer receives the candidate file and original I/O exception synchronously for - * each failed read attempt, including an unsupported binary version. It replaces the default - * console report; scanning continues and other files or repositories may supply a module. - * Cached absence produces no report. A candidate can be unrelated to the requested module, - * and a report does not identify a failed write, corruption or the file's module identity. - * Write failures continue to be thrown by {@link #storeModule(ModuleStructure)}. - * - * @param dir the directory that contains the repository contents - * @param fReadOnly true to make the repository read-only - * @param onReadFailure the non-null observer; it must not reenter this repository - */ - public DirRepository(File dir, boolean fReadOnly, BiConsumer onReadFailure) { assert dir != null && dir.isDirectory(); - m_dir = dir; - m_fRO = fReadOnly; - m_onReadFailure = Objects.requireNonNull(onReadFailure); + m_dir = dir; + m_fRO = fReadOnly; } // ----- accessors ----------------------------------------------------------------------------- @@ -198,8 +176,7 @@ private void rebuildCache(File[] files) { fWriteCache |= files.length != oldModulesByFile.size(); for (File file : files) { ModuleInfo info = oldModulesByFile.get(file); - if (info == null || info.hasReadFailure() - || info.timestamp != file.lastModified() || info.size != file.length()) { + if (info == null || info.timestamp != file.lastModified() || info.size != file.length()) { // build a new one to cache info = createModuleInfo(file); fWriteCache = true; @@ -231,7 +208,7 @@ private void clearCache() { * @return the module information */ protected ModuleInfo createModuleInfo(File file) { - return new ModuleInfo(file, m_onReadFailure); + return new ModuleInfo(file); } /** @@ -273,7 +250,7 @@ private Map readCache() { String name = err ? null : in.readUTF(); if (modulesByFile.put(file, - new ModuleInfo(file, name, timestamp, size, err, m_onReadFailure)) != null) { + new ModuleInfo(file, name, timestamp, size, err)) != null) { return null; } } @@ -366,10 +343,8 @@ private File getCacheFile() { * @return true if the cache is still good, or false if it needs to be rebuilt */ private boolean isCacheValid() { - // Failed candidates are retried once per second, or immediately when metadata changes. - boolean recent = System.currentTimeMillis() < lastScan + 1000; - boolean failed = modulesByFile.values().stream().anyMatch(ModuleInfo::hasReadFailure); - if (recent && !failed) { + // Check metadata at the usual interval; a failed read alone does not invalidate the cache. + if (System.currentTimeMillis() < lastScan + 1000) { return true; } @@ -385,21 +360,16 @@ private boolean isCacheValid() { } } - return !failed || recent; + return true; } // ----- inner class: ModuleInfo --------------------------------------------------------------- protected static class ModuleInfo { public ModuleInfo(File file) { - this(file, DirRepository::reportReadFailure); - } - - private ModuleInfo(File file, BiConsumer onReadFailure) { - this.onReadFailure = onReadFailure; - this.file = file; - this.timestamp = file.lastModified(); - this.size = file.length(); + this.file = file; + this.timestamp = file.lastModified(); + this.size = file.length(); ModuleStructure module = tryLoad(); if (module == null) { @@ -413,15 +383,13 @@ private ModuleInfo(File file, BiConsumer onReadFailure) { } } - private ModuleInfo(File file, String name, long timestamp, long size, boolean err, - BiConsumer onReadFailure) { - this.onReadFailure = onReadFailure; - this.file = file; - this.name = name; - this.versions = null; - this.timestamp = timestamp; - this.size = size; - this.err = err; + private ModuleInfo(File file, String name, long timestamp, long size, boolean err) { + this.file = file; + this.name = name; + this.versions = null; + this.timestamp = timestamp; + this.size = size; + this.err = err; } ModuleStructure tryLoad() { @@ -429,20 +397,15 @@ ModuleStructure tryLoad() { FileStructure struct = new FileStructure(file); readFailed = false; return struct.getModule(); - } catch (IOException e) { + } catch (Exception e) { readFailed = true; - onReadFailure.accept(file, e); + System.out.println("Error loading module from file: " + file + "; " + e.getMessage()); return null; } } - boolean hasReadFailure() { - // A failed entry read from the disk cache must be retried, even with unchanged metadata. - return err || readFailed; - } - ModuleStructure ensureModule() { - if (err) { + if (err || readFailed) { return null; } @@ -466,14 +429,8 @@ ModuleStructure ensureModule() { */ private transient ModuleStructure module; - /** Whether a payload read failed after the header was cached. */ + /** A failed lazy read remains unavailable until changed metadata replaces this entry. */ private boolean readFailed; - - private final BiConsumer onReadFailure; - } - - private static void reportReadFailure(File file, IOException cause) { - System.out.println("Error reading module file " + file + ": " + cause); } // ----- constants ----------------------------------------------------------------------------- @@ -493,7 +450,6 @@ private static void reportReadFailure(File file, IOException cause) { private final File m_dir; private final boolean m_fRO; - private final BiConsumer m_onReadFailure; private Map modulesByFile = new HashMap<>(); private final Map modulesByName = new TreeMap<>(); diff --git a/javatools/src/main/java/org/xvm/asm/FileRepository.java b/javatools/src/main/java/org/xvm/asm/FileRepository.java index 3e1e5b16da4..ade9a83f4f7 100644 --- a/javatools/src/main/java/org/xvm/asm/FileRepository.java +++ b/javatools/src/main/java/org/xvm/asm/FileRepository.java @@ -5,12 +5,9 @@ import java.util.HashMap; import java.util.Map; -import java.util.Objects; import java.util.Optional; import java.util.Set; -import java.util.function.BiConsumer; - import org.xvm.asm.FileStructure.FileInfo; import org.xvm.asm.constants.ModuleConstant; @@ -36,29 +33,10 @@ public class FileRepository * @param readOnly true to make the repository "read-only" */ public FileRepository(File file, boolean readOnly) { - this(file, readOnly, (candidate, cause) -> - System.out.println("Error reading module file " + candidate + ": " + cause)); - } - - /** - * Construct a repository with an observer for unsuccessful file reads. - * - *

The observer receives the candidate file and original I/O exception synchronously for - * each failed read attempt, including an unsupported binary version. It replaces the default - * console report; it does not change lookup or fallback. Cached absence produces no report. - * A report establishes neither the file's module identity nor why its contents became unreadable. - * Write failures continue to be thrown by {@link #storeModule(ModuleStructure)}. - * - * @param file the file that contains the module(s) - * @param readOnly true to make the repository read-only - * @param onReadFailure the non-null observer; it must not reenter this repository - */ - public FileRepository(File file, boolean readOnly, BiConsumer onReadFailure) { assert file != null && !file.isDirectory(); - this.file = moduleFile(file); - this.readOnly = readOnly; - this.onReadFailure = Objects.requireNonNull(onReadFailure); + this.file = moduleFile(file); + this.readOnly = readOnly; } // ----- accessors ----------------------------------------------------------------------------- @@ -251,12 +229,11 @@ private static File moduleFile(File file) { * Make sure that the cache is up to date. */ private boolean validateCache() { - // Successful reads keep the usual burst cache. Failed reads are - // retried after the burst, or immediately if the file metadata changes. + // Both successful and failed reads use the usual metadata-based burst cache. long now = System.currentTimeMillis(); boolean unchanged = timestamp == file.lastModified() && size == file.length(); boolean recent = now < lastScan + 60/*ms*/; - if (!readFailed ? recent || unchanged : recent && unchanged) { + if (recent || unchanged) { return cacheOk; } @@ -326,9 +303,9 @@ private FileInfo readFileInfo() { FileInfo info = FileStructure.readFileInfo(file); readFailed = false; return info; - } catch (IOException e) { + } catch (Exception e) { readFailed = true; - onReadFailure.accept(file, e); + System.out.println("Error loading FileInfo from file: " + file + "; " + e.getMessage()); return null; } } @@ -341,9 +318,9 @@ private FileStructure readFileStructure() { FileStructure structure = new FileStructure(file); readFailed = false; return structure; - } catch (IOException e) { + } catch (Exception e) { readFailed = true; - onReadFailure.accept(file, e); + System.out.println("Error loading FileStructure from file: " + file + "; " + e.getMessage()); return null; } } @@ -391,11 +368,9 @@ private Map ensureModuleCache() { private long lastScan; private boolean cacheOk; - /** Whether the last read failed and must be retried after the cache interval. */ + /** A replacement must pass a complete read after a prior failure, not just a header read. */ private boolean readFailed; - private final BiConsumer onReadFailure; - /** * Cached file contents: module names and versions thereof. */ diff --git a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java index 917ecbfaf82..83d0acab050 100644 --- a/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/DirRepositoryFailureTest.java @@ -1,6 +1,5 @@ package org.xvm.asm; -import java.io.File; import java.io.IOException; import java.nio.ByteBuffer; @@ -9,8 +8,6 @@ import java.time.Duration; -import java.util.HashMap; -import java.util.Map; import java.util.Set; import org.junit.jupiter.api.Test; @@ -22,37 +19,32 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; /** * Exercises directory repository reads with corrupt, incompatible and repaired candidates. Protects both - * best-effort fallback to usable modules and delivery of the original read failure to an observing host. + * best-effort fallback to usable modules and metadata-based reuse of unavailable candidates. */ -class DirRepositoryFailureTest { +class DirRepositoryFailureTest extends RepositoryFailureTestSupport { @TempDir Path directory; - private final Map reports = new HashMap<>(); - @Test void unreadableCandidateIsReportedAndCanBeRepaired() throws IOException { Path file = directory.resolve("broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new DirRepository(directory.toFile(), true, reports::put); + var repository = new DirRepository(directory.toFile(), true); - assertTrue(reports.isEmpty(), "construction must not initiate a scan"); + assertTrue(output().isEmpty(), "construction must not initiate a scan"); assertTrue(repository.getModuleNames().isEmpty()); assertNull(repository.loadModule("Broken")); - assertEquals(Set.of(file.toFile()), reports.keySet()); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().contains(file.toString())); - reports.clear(); + clearOutput(); new FileStructure("Broken").writeTo(file.toFile()); - assertNotNull(repository.loadModule("Broken")); - assertTrue(reports.isEmpty()); + awaitModule(repository, "Broken"); + assertTrue(output().isEmpty()); } @Test @@ -60,13 +52,12 @@ void incompatibleCandidateDoesNotHideAUsableModuleInTheSameDirectory() throws IO Path old = directory.resolve("Old.xtc"); writeUnsupportedModule(old, "Old"); new FileStructure("Good").writeTo(directory.resolve("Good.xtc").toFile()); - var repository = new DirRepository(directory.toFile(), true, reports::put); + var repository = new DirRepository(directory.toFile(), true); assertNotNull(repository.loadModule("Good")); assertEquals(Set.of("Good"), repository.getModuleNames()); assertNull(repository.loadModule("Unknown")); - assertTrue(reports.get(old.toFile()).getMessage() - .contains("Unsupported .xtc version")); + assertTrue(output().contains("Unsupported .xtc version")); } @ParameterizedTest @@ -81,94 +72,77 @@ void incompatibleCandidateDoesNotBlockALaterRepository(boolean singleFile) throw good.getModule().setVersion(version); good.writeTo(second.resolve("Good.xtc").toFile()); ModuleRepository candidate = singleFile - ? new FileRepository(old.toFile(), true, reports::put) - : new DirRepository(first.toFile(), true, reports::put); - var repository = new LinkedRepository(candidate, new DirRepository(second.toFile(), true, reports::put)); + ? new FileRepository(old.toFile(), true) + : new DirRepository(first.toFile(), true); + var repository = new LinkedRepository(candidate, new DirRepository(second.toFile(), true)); assertNotNull(repository.loadModule("Good")); assertEquals(Set.of("Good"), repository.getModuleNames()); assertTrue(repository.getAvailableVersions("Good").contains(version)); assertNotNull(repository.loadModule("Good", version, true)); - assertEquals(Set.of(old.toFile()), reports.keySet()); - assertTrue(reports.get(old.toFile()).getMessage().contains("Unsupported .xtc version")); + assertTrue(output().contains(old.toString())); + assertTrue(output().contains("Unsupported .xtc version")); } @Test - void linkedSearchReportsCandidateReadsWithoutAttributingTheMissingModule() throws IOException { - Path first = Files.createDirectory(directory.resolve("first")); - Path second = Files.createDirectory(directory.resolve("second")); - Path old = first.resolve("Old.xtc"); - Path broken = second.resolve("Broken.xtc"); - writeUnsupportedModule(old, "Old"); - Files.writeString(broken, "not a compiled module"); - var repository = new LinkedRepository(new DirRepository(first.toFile(), true, reports::put), - new DirRepository(second.toFile(), true, reports::put)); - - assertNull(repository.loadModule("Missing")); - assertEquals(Set.of(old.toFile(), broken.toFile()), reports.keySet()); - } - - @Test - void cachedFailureIsRetriedAndReportedByANewRepositoryInstance() throws IOException { + void cachedFailureIsReusedByANewRepositoryInstanceUntilFileChanges() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - assertTrue(new DirRepository(directory.toFile(), true, reports::put).getModuleNames().isEmpty()); - reports.clear(); - var repository = new DirRepository(directory.toFile(), true, reports::put); + assertTrue(new DirRepository(directory.toFile(), true).getModuleNames().isEmpty()); + clearOutput(); + var repository = new DirRepository(directory.toFile(), true); assertNull(repository.loadModule("Broken")); - assertNotNull(reports.get(file.toFile())); - reports.clear(); + assertTrue(output().isEmpty(), "cached absence is not a new read attempt"); new FileStructure("Broken").writeTo(file.toFile()); - assertNotNull(repository.loadModule("Broken")); - assertTrue(reports.isEmpty()); + awaitModule(repository, "Broken"); + assertTrue(output().isEmpty()); } @Test - void failedCandidateIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException { - Path file = directory.resolve("Good.xtc"); - new FileStructure("Good").writeTo(file.toFile()); - byte[] valid = Files.readAllBytes(file); - writeUnsupportedModule(file, "Good"); - var timestamp = Files.getLastModifiedTime(file); - var repository = new DirRepository(directory.toFile(), true, reports::put); - assertNull(repository.loadModule("Good")); - assertNotNull(reports.get(file.toFile())); - - reports.clear(); - Files.write(file, valid); - Files.setLastModifiedTime(file, timestamp); - assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { - while (repository.loadModule("Good") == null) { - Thread.sleep(10); - } - }); - assertTrue(reports.isEmpty()); + void unchangedFailureIsNotReadAgainWhenTheScanIntervalExpires() throws Exception { + Path file = directory.resolve("Old.xtc"); + writeUnsupportedModule(file, "Old"); + var repository = new DirRepository(directory.toFile(), true); + assertNull(repository.loadModule("Old")); + assertTrue(output().contains(file.toString())); + clearOutput(); + + Thread.sleep(1100); // Advance past the production metadata-check interval. + for (int i = 0; i < 3; ++i) { + assertTrue(repository.getModuleNames().isEmpty()); + assertNull(repository.loadModule("Missing")); + } + assertTrue(output().isEmpty(), "unchanged candidates must not be read or reported repeatedly"); + + new FileStructure("Good").writeTo(directory.resolve("Good.xtc").toFile()); + awaitModule(repository, "Good"); + assertTrue(output().isEmpty(), "a rebuild for another file must retain the failed entry"); + new FileStructure("Repaired").writeTo(file.toFile()); + awaitModule(repository, "Repaired"); + assertTrue(output().isEmpty()); } @Test void removedCandidatesProduceNoFurtherReports() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new DirRepository(directory.toFile(), true, reports::put); + var repository = new DirRepository(directory.toFile(), true); assertNull(repository.loadModule("Broken")); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().contains(file.toString())); - reports.clear(); + clearOutput(); Files.delete(file); - assertTrue(repository.getModuleNames().isEmpty()); - assertTrue(reports.isEmpty()); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (!repository.getModuleNames().isEmpty()) Thread.sleep(10); + }); + assertTrue(output().isEmpty()); } - @Test - void observerFailuresAreNotSwallowedAsUnavailableModules() throws IOException { - Files.writeString(directory.resolve("Broken.xtc"), "unreadable candidate"); - var failure = new IllegalStateException("observer failed"); - var repository = new DirRepository(directory.toFile(), true, (file, cause) -> { - throw failure; + private static void awaitModule(ModuleRepository repository, String name) { + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (repository.loadModule(name) == null) Thread.sleep(10); }); - - assertSame(failure, assertThrows(IllegalStateException.class, () -> repository.loadModule("Missing"))); } private static void writeUnsupportedModule(Path file, String name) throws IOException { diff --git a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java index cc0ead928e8..9d065af683c 100644 --- a/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java +++ b/javatools/src/test/java/org/xvm/asm/FileRepositoryFailureTest.java @@ -1,7 +1,6 @@ package org.xvm.asm; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.nio.file.Files; @@ -10,122 +9,120 @@ import java.time.Duration; import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; /** - * Exercises failed and repaired module-file reads, including repairs that preserve file metadata. Ensures - * failure observations do not turn write errors into read reports or leave failed reads permanently cached. + * Exercises failed module reads and recovery after normal file replacements. Ensures unchanged failures + * stay cached and quiet, while write errors still reach the writer. */ -class FileRepositoryFailureTest { +class FileRepositoryFailureTest extends RepositoryFailureTestSupport { @TempDir Path directory; - private final Map reports = new HashMap<>(); - @Test void invalidHeaderIsReportedAndCanBeRepaired() throws IOException { Path file = directory.resolve("broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new FileRepository(file.toFile(), true, reports::put); + var repository = new FileRepository(file.toFile(), true); - assertTrue(reports.isEmpty(), "construction must not initiate a read"); + assertTrue(output().isEmpty(), "construction must not initiate a read"); assertTrue(repository.getModuleNames().isEmpty()); assertNull(repository.loadModule("Broken")); - assertEquals(Set.of(file.toFile()), reports.keySet()); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().contains(file.toString())); - reports.clear(); + clearOutput(); new FileStructure("Broken").writeTo(file.toFile()); - assertNotNull(repository.loadModule("Broken")); - assertTrue(reports.isEmpty()); + awaitModule(repository, "Broken"); + assertTrue(output().isEmpty()); } @Test - void truncatedPayloadIsReportedAfterItsHeaderWasRead() throws IOException { + void truncatedPayloadIsReportedAfterItsHeaderWasRead() throws Exception { Path file = directory.resolve("broken.xtc"); - reports.clear(); + clearOutput(); new FileStructure("Broken").writeTo(file.toFile()); byte[] contents = Files.readAllBytes(file); var input = new ByteArrayInputStream(contents); FileStructure.readFileInfo(input); Files.write(file, Arrays.copyOf(contents, contents.length - input.available())); - var repository = new FileRepository(file.toFile(), true, reports::put); + var repository = new FileRepository(file.toFile(), true); assertTrue(repository.getModuleNames().contains("Broken")); assertNull(repository.loadModule("Broken")); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().contains(file.toString())); + clearOutput(); + Thread.sleep(80); // Past the burst interval; the unchanged bad payload remains unavailable. assertNull(repository.loadModule("Broken")); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().isEmpty()); - reports.clear(); Files.write(file, contents); - assertNotNull(repository.loadModule("Broken")); - assertTrue(reports.isEmpty()); + awaitModule(repository, "Broken"); + assertTrue(output().isEmpty()); } @Test - void failedReadIsRetriedEvenWhenRepairPreservesItsMetadata() throws IOException { - Path file = directory.resolve("Good.xtc"); - new FileStructure("Good").writeTo(file.toFile()); - byte[] valid = Files.readAllBytes(file); - byte[] broken = valid.clone(); - broken[0] = 0; // Invalidate the magic value without changing the file size. - Files.write(file, broken); - var timestamp = Files.getLastModifiedTime(file); - var repository = new FileRepository(file.toFile(), true, reports::put); - assertNull(repository.loadModule("Good")); - assertNotNull(reports.get(file.toFile())); - - reports.clear(); - Files.write(file, valid); - Files.setLastModifiedTime(file, timestamp); - assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { - while (repository.loadModule("Good") == null) { - Thread.sleep(10); - } - }); - assertTrue(reports.isEmpty()); + void unchangedFailureIsNotReadAgainWhenTheBurstIntervalExpires() throws Exception { + Path file = directory.resolve("Broken.xtc"); + Files.writeString(file, "not a compiled module"); + var repository = new FileRepository(file.toFile(), true); + assertNull(repository.loadModule("Broken")); + assertTrue(output().contains(file.toString())); + clearOutput(); + + Thread.sleep(80); // Advance past the production burst-cache interval. + for (int i = 0; i < 3; ++i) { + assertTrue(repository.getModuleNames().isEmpty()); + assertNull(repository.loadModule("Broken")); + } + assertTrue(output().isEmpty(), "unchanged failures must not cause repeated reads or reports"); + + new FileStructure("Repaired").writeTo(file.toFile()); + awaitModule(repository, "Repaired"); + assertTrue(output().isEmpty()); } @Test void removedCandidatesProduceNoFurtherReports() throws IOException { Path file = directory.resolve("Broken.xtc"); Files.writeString(file, "not a compiled module"); - var repository = new FileRepository(file.toFile(), true, reports::put); + var repository = new FileRepository(file.toFile(), true); assertNull(repository.loadModule("Broken")); - assertNotNull(reports.get(file.toFile())); + assertTrue(output().contains(file.toString())); - reports.clear(); + clearOutput(); Files.delete(file); - assertTrue(repository.getModuleNames().isEmpty()); - assertTrue(reports.isEmpty()); + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (!repository.getModuleNames().isEmpty()) Thread.sleep(10); + }); + assertTrue(output().isEmpty()); } + @Test - void failedWritesAreThrownSeparatelyFromReadReports() throws IOException { + void failedWritesStillReachTheWriter() throws IOException { Path file = directory.resolve("Old.xtc"); Files.writeString(file, "unreadable candidate"); byte[] original = Files.readAllBytes(file); - var repository = new FileRepository(file.toFile(), true, reports::put); + var repository = new FileRepository(file.toFile(), true); assertNull(repository.loadModule("Missing")); - assertEquals(Set.of(file.toFile()), reports.keySet()); - reports.clear(); + assertTrue(output().contains(file.toString())); + clearOutput(); assertThrows(IOException.class, () -> repository.storeModule(new FileStructure("Good").getModule())); - assertTrue(reports.isEmpty(), "a write failure is not a read event"); + assertTrue(output().isEmpty(), "write failures must be thrown, not printed"); assertArrayEquals(original, Files.readAllBytes(file)); } + private static void awaitModule(ModuleRepository repository, String name) { + assertTimeoutPreemptively(Duration.ofSeconds(5), () -> { + while (repository.loadModule(name) == null) Thread.sleep(10); + }); + } } diff --git a/javatools/src/test/java/org/xvm/asm/RepositoryFailureTestSupport.java b/javatools/src/test/java/org/xvm/asm/RepositoryFailureTestSupport.java new file mode 100644 index 00000000000..000e7244e8c --- /dev/null +++ b/javatools/src/test/java/org/xvm/asm/RepositoryFailureTestSupport.java @@ -0,0 +1,42 @@ +package org.xvm.asm; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import java.nio.charset.StandardCharsets; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.parallel.Isolated; + +/** + * Captures the repositories' existing console reports without adding a reporting API for tests. + * Isolation keeps temporary process-wide output redirection separate from other test classes. + */ +@Isolated +abstract class RepositoryFailureTestSupport { + @BeforeEach + void captureOutput() { + original = System.out; + captured = new PrintStream(bytes, true, StandardCharsets.UTF_8); + System.setOut(captured); + } + + @AfterEach + void restoreOutput() { + System.setOut(original); + captured.close(); + } + + String output() { + return bytes.toString(StandardCharsets.UTF_8); + } + + void clearOutput() { + bytes.reset(); + } + + private final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + private PrintStream original; + private PrintStream captured; +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt index 7a682b2bef2..c6b21a7b79c 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt @@ -14,16 +14,14 @@ import org.xvm.asm.FileStructure import org.xvm.asm.ModuleRepository import org.xvm.compiler.Source import org.xvm.tool.ModuleInfo -import java.io.File -import java.io.IOException import java.nio.ByteBuffer import java.nio.file.Files import java.nio.file.Path /** * Exercises public embedding diagnostics with invalid roots, dependency failures and fallback repositories. - * Protects source errors from being mislabeled as internal failures and preserves useful repository causes - * without poisoning successful fallback. + * Protects source errors from being mislabeled as internal failures and keeps unavailable candidates + * from poisoning successful fallback or becoming unrelated missing-import diagnostics. */ class EmbeddingDiagnosticsTest { @TempDir lateinit var directory: Path @@ -82,12 +80,11 @@ class EmbeddingDiagnosticsTest { val bytes = file.readBytes() ByteBuffer.wrap(bytes).putInt(2 * Int.SIZE_BYTES, FileStructure.getToolMinorVersion() - 1) Files.write(file.toPath(), bytes) - val reports = mutableMapOf() val repository: ModuleRepository = if (singleFile) { - FileRepository(file, true, reports::put) + FileRepository(file, true) } else { - DirRepository(directory.toFile(), true, reports::put) + DirRepository(directory.toFile(), true) } val support = EmbeddingSupport.instance() @@ -95,7 +92,6 @@ class EmbeddingDiagnosticsTest { val errors = ErrorList() val result = support.compileModule(Source(text), repository, errors) assertThat(result.succeeded()).isEqualTo(text == "module Test {}") - assertThat(reports).containsKey(file) assertThat(errors.errors.map { it.code }).doesNotContain("EMB-5") } @@ -109,7 +105,6 @@ class EmbeddingDiagnosticsTest { assertThat(result.succeeded()).isFalse() assertThat(errors.errors.map { it.code }).contains("COMPILER-24").doesNotContain("EMB-5") assertThat(errors.errors).noneMatch { it.message.contains(file.path) } - assertThat(reports.getValue(file)).hasMessageContaining("Unsupported .xtc version") } @Test diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt index 1651b9ea244..e4aab0b5f62 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/RepositoryReportingTest.kt @@ -4,6 +4,7 @@ import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test import org.junit.jupiter.api.Timeout import org.junit.jupiter.api.io.TempDir +import org.junit.jupiter.api.parallel.Isolated import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource import org.xvm.api.EmbeddingSupport @@ -17,33 +18,32 @@ import org.xvm.asm.LinkedRepository import org.xvm.asm.ModuleRepository import org.xvm.asm.ModuleStructure import org.xvm.compiler.InstantRepository +import org.xvm.runtime.template._native.io.xTerminalConsole import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File -import java.io.IOException import java.nio.ByteBuffer import java.nio.file.Files import java.nio.file.Path -/** Exercises repository reporting as a host and through the native Ecstasy runtime. */ +/** Exercises tolerant repository lookup and cache recovery through compiler and runtime APIs. */ +@Isolated @Timeout(60) class RepositoryReportingTest { @TempDir lateinit var directory: Path @ParameterizedTest @ValueSource(booleans = [false, true]) - fun `candidate reports remain separate from compilation outcomes`(singleFile: Boolean) { + fun `unavailable unrelated candidates do not change compilation outcomes`(singleFile: Boolean) { CompilerTestSupport.configure() val file = directory.resolve("Old.xtc").toFile() unsupported(file, "Old") - val reports = mutableMapOf() val repository: ModuleRepository = - if (singleFile) FileRepository(file, true, reports::put) else DirRepository(directory.toFile(), true, reports::put) + if (singleFile) FileRepository(file, true) else DirRepository(directory.toFile(), true) val support = EmbeddingSupport.instance() val validErrors = ErrorList(100) assertThat(support.compile("module Good {}", repository, validErrors)).isNotNull() assertThat(validErrors.errors).isEmpty() - assertThat(reports[file]?.message).contains("Unsupported .xtc version") val missingErrors = ErrorList(100) assertThat(support.compile("module Test { package lib import Missing; }", repository, missingErrors)).isNull() @@ -52,7 +52,7 @@ class RepositoryReportingTest { } @Test - fun `Ecstasy repositories preserve platform fallback and retry with optional reporting`() { + fun `Ecstasy repositories cache unchanged failures and recover after normal replacement`() { CompilerTestSupport.configure() val first = Files.createDirectory(directory.resolve("first")) val second = Files.createDirectory(directory.resolve("second")) @@ -67,27 +67,21 @@ class RepositoryReportingTest { import ecstasy.mgmt.ModuleRepository; import ecstasy.reflect.ModuleTemplate; - service Reports { - Int count = 0; - void record(File file, Exception cause) { - assert file.name == "Old.xtc" || file.name == "Good.xtc"; - assert cause.cause != Null; - ++count; - } - } - Int run() { @Inject Directory rootDir; Directory first = rootDir.dirFor("${first.toString().removePrefix("/")}"); Directory second = rootDir.dirFor("${second.toString().removePrefix("/")}"); - Reports reports = new Reports(); - DirRepository primary = new DirRepository(first, (file, cause) -> reports.record(file, cause)); + DirRepository primary = new DirRepository(first); ModuleRepository fallback = new DirRepository(second); ModuleRepository linked = new LinkedRepository([primary, fallback].toArray(Constant, True)); assert ModuleTemplate good := linked.getModule("Good.example.org"); assert good.qualifiedName == "Good.example.org"; - assert reports.count >= 2; - assert !(linked.getModule("Missing")); + for (Int i = 0; i < 3; ++i) { + // Missing lookups force a metadata check even within the scan interval. + assert !(linked.getModule("Missing")); + assert linked.getModule("Good.example.org"); + assert primary.moduleNames.empty; + } first.fileFor("Good.xtc").contents = second.fileFor("Good.xtc").contents; assert primary.getModule("Good.example.org"); @@ -115,6 +109,13 @@ class RepositoryReportingTest { connector.invoke0(connector.findMethods("run").single()) } assertThat(connector.join()).isEqualTo(42) + // The native console records its output. Only the first reads may report these unique files; + // missing lookups and rebuilding after another file changes must reuse unavailable entries. + val console = xTerminalConsole.CONSOLE_LOG + val reports = (0 until console.size()).map { console.get(it) }.filter { it.contains(first.toString()) } + assertThat(reports).hasSize(2) + assertThat(reports.count { it.contains("Old.xtc") }).isEqualTo(1) + assertThat(reports.count { it.contains("Good.xtc") }).isEqualTo(1) } private fun compile(source: String): ModuleStructure { From cf7df0a2a6e53c8541e836914b8c014ba2960040 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:12 +0100 Subject: [PATCH 012/324] Verify compiler output without binary AST dependencies - Verify compiler output without binary AST dependencies. - Document both compiler output regression suites. - Check local Java whitespace in the binary AST removal scope. - Describe the final binary AST removal format boundary. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/README.md | 3 ++- .../org/xvm/compiler/ast/ArrayAccessExpression.java | 2 +- .../main/java/org/xvm/compiler/ast/NewExpression.java | 4 ++-- lang/doc/manual-test-plan.md | 10 +++++----- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/javatools/README.md b/javatools/README.md index 06c4d953bf0..b4165f2f718 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -113,7 +113,8 @@ The compiler's callable typing and fitting corrections serve both ordinary compi cursor probes. Boolean operand corrections preserve evaluation and side effects even when the result is constant. These are compiler correctness rules, not alternate semantics enabled by an LSP flag. The separate removal of the abandoned binary AST does not remove the source AST used -by tooling; legacy `.xtc` slots are still read or skipped as required for format compatibility. +by tooling. The serialized format contains no BAST metadata slot; older `.xtc` files must be +recompiled with the current compiler. Repository lookup remains tolerant: an unreadable candidate can be skipped while another file or repository supplies the module. An unrelated failed candidate does not establish why an import diff --git a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java index 7f1e77c4586..faf549c4dc3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java @@ -411,7 +411,7 @@ protected Expression validate(Context ctx, TypeConstant typeRequired, ErrorListe Expression exprRow = aexprIndexes[1]; // matrix[index,index] - TypeConstant typeElement = null; + TypeConstant typeElement = null; if (exprCol.testFit(ctx, pool.typeInt64(), false, probe).isFit() && exprRow.testFit(ctx, pool.typeInt64(), false, probe).isFit()) { typeElement = typeRequired; diff --git a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java index 4229eeb8278..66ffd6338a3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/NewExpression.java @@ -1505,8 +1505,8 @@ protected boolean isCaptureFinal(String sCaptureName) { protected boolean isImplicitDeref(String sCaptureName) { assert m_mapCapture.containsKey(sCaptureName); - Register reg = m_captureBindings.registers().get(sCaptureName); - Boolean FVar = m_mapCapture .get(sCaptureName); + Register reg = m_captureBindings.registers().get(sCaptureName); + Boolean FVar = m_mapCapture .get(sCaptureName); return FVar || !reg.isEffectivelyFinal(); } diff --git a/lang/doc/manual-test-plan.md b/lang/doc/manual-test-plan.md index 92500f3a593..66cb33110ba 100644 --- a/lang/doc/manual-test-plan.md +++ b/lang/doc/manual-test-plan.md @@ -1324,14 +1324,14 @@ After assembling with the same compiler flag, `npm run test:playbook` from `lang reruns just the editor suite. It reports existing host-test evidence without rebuilding/rerunning it. See the [extension testing notes](../vscode-extension/README.md#testing) for display/`xvfb` requirements. -The host suite also includes `CompilerEmissionAuditTest`: it compiles atomic operations through -qualified, singleton, enclosing-instance and generic receivers, deserializes their binary ASTs, -checks source/dependency diagnostic delivery, and inspects dense-switch conversion metadata. These -are compiler-output checks that the editor UI cannot establish. To run them without launching VS Code: +The host suite also includes `CompilerAtomicEmissionTest` and `CompilerEmissionAuditTest`: they +execute serialized atomic operations through qualified, implicit, enclosing-instance and generic +receivers, check source/dependency diagnostic delivery, and inspect dense-switch bytecode conversions. +These are compiler-output checks that the editor UI cannot establish. To run them without launching VS Code: ```bash ./gradlew :lang:lsp-server:test --tests org.xvm.lsp.adapter.CompilerEmissionAuditTest \ - --rerun --no-build-cache \ + --tests org.xvm.lsp.adapter.CompilerAtomicEmissionTest --rerun-tasks --no-build-cache \ -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler ``` From 9dad3df5d26be3598ddf7aaf1288e23a476342f4 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:13 +0100 Subject: [PATCH 013/324] Isolate parallel LSP test workers and keep CI and lint checks clean - List each compiler test suite on its own line. - Fix VS Code lint findings and preserve playbook cleanup failures. - Avoid duplicate LSP CI runs and isolate parallel test workers. - Format LSP test settings for the CI Kotlin Gradle check. - Fix order-dependent packaged-server trace assertion. - Default LSP test workers to half the available cores. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .github/workflows/commit.yml | 46 +++++++++++++++---- lang/lsp-server/README.md | 25 ++++++++-- lang/lsp-server/build.gradle.kts | 20 +++++++- .../org/xvm/lsp/adapter/XdkRetentionTest.kt | 20 ++++++-- .../kotlin/org/xvm/lsp/server/XdkStdioTest.kt | 8 +++- lang/vscode-extension/src/status-bar.ts | 3 +- .../src/test/explorer-move.ts | 2 +- .../src/test/playbook/completion.ts | 2 +- .../src/test/playbook/dependencies.ts | 6 +-- .../src/test/playbook/navigation.ts | 2 +- .../src/test/playbook/runtimeSettings.ts | 20 ++++---- .../src/test/suite/playbook-wait.test.ts | 32 ++++++++++++- lang/vscode-extension/src/test/wait.ts | 10 ++++ 13 files changed, 159 insertions(+), 37 deletions(-) diff --git a/.github/workflows/commit.yml b/.github/workflows/commit.yml index a502586d2ea..e33a686c765 100644 --- a/.github/workflows/commit.yml +++ b/.github/workflows/commit.yml @@ -77,6 +77,11 @@ on: type: boolean required: false default: false + lsp-heavy-tests: + description: 'Include the extended LSP retention workload (also enable include-lang)' + type: boolean + required: false + default: false include-lang: description: 'Include lang projects in build and run tree-sitter validation' type: boolean @@ -90,6 +95,10 @@ env: ORG_GRADLE_PROJECT_includeBuildAttachManualTests: ${{ github.event_name != 'workflow_dispatch' || !(github.event.inputs.skip-tests == true || github.event.inputs.skip-tests == 'true') }} ORG_GRADLE_PROJECT_xtcPluginOverrideVerboseLogging: true RUN_INTEGRATION_TESTS: true + # Keep compiler regressions and a complete retention smoke test in every applicable PR run. + # The longer repetition workload also runs locally by default and can run before merge via dispatch. + ORG_GRADLE_PROJECT_lspTestHeavy: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && (github.event.inputs.lsp-heavy-tests == true || github.event.inputs.lsp-heavy-tests == 'true')) }} + ORG_GRADLE_PROJECT_lspTestForks: 2 # Lang build inclusion - overrides gradle.properties default (true) to exclude lang # from push/PR CI builds. Only included via workflow_dispatch with include-lang=true. @@ -696,7 +705,7 @@ jobs: # Gradle stops at the first failing task and names it in the output. # - core: tree-sitter grammar compiles (tree-sitter generate), parser handles the # XDK corpus, native libraries build for all platforms (cached by content - # hash); LSP adapter tests with the tree-sitter adapter; DSL tests, which also + # hash); both LSP adapters against one compiler-default artifact; DSL tests, which also # read the XDK corpus # - VS Code extension: headless integration tests, then bundle and package the VSIX, and, # only when the repository variable VSCODE_MARKETPLACE_PUBLISH is true, the @@ -729,14 +738,17 @@ jobs: echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" echo "🌳 LANG VALIDATION: core $VALIDATE_CORE, VS Code extension $VALIDATE_VSCODE" echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" - ./gradlew $GRADLE_OPTIONS $LANG_OPTS -Plsp.adapter=treesitter $LANG_TASKS + # Keep the same packaged default as the main build and stdio checks. Switching + # defaults rewrites build-info and the JAR, invalidating every test's classpath and + # rerunning the entire suite. Tests select Tree-sitter explicitly where needed. + ./gradlew $GRADLE_OPTIONS $LANG_OPTS -Plsp.adapter=compiler $LANG_TASKS echo "" if [ "$VALIDATE_CORE" = "true" ]; then echo "✅ Grammar: VALID" echo "✅ Corpus parsing: PASSED" echo "✅ Native libraries: BUILT" - echo "✅ Tree-sitter adapter tests: PASSED" + echo "✅ Compiler and Tree-sitter adapter tests: PASSED" echo "✅ DSL tests: PASSED" fi if [ "$VALIDATE_VSCODE" = "true" ]; then @@ -774,13 +786,22 @@ jobs: results = Path("lang/lsp-server/build/test-results/test") suites = [ "adapter.CompilerConsumerTest", - "adapter.XdkAdapterTest", "adapter.XdkNavigationTest", "adapter.XdkDiagnosticTest", - "adapter.XdkAdapterLifecycleTest", "adapter.SemanticModelTest", "adapter.CompilerProjectTest", - "adapter.TypeInfoDiagnosticsTest", "adapter.TypeInfoFinalCompositionTest", "adapter.EmbeddingDiagnosticsTest", - "adapter.XdkModuleSessionTest", "adapter.XdkRecoveryTest", "adapter.XdkPartialAnalysisTest", + "adapter.XdkAdapterTest", + "adapter.XdkNavigationTest", + "adapter.XdkDiagnosticTest", + "adapter.XdkAdapterLifecycleTest", + "adapter.SemanticModelTest", + "adapter.CompilerProjectTest", + "adapter.TypeInfoDiagnosticsTest", + "adapter.TypeInfoFinalCompositionTest", + "adapter.EmbeddingDiagnosticsTest", + "adapter.XdkModuleSessionTest", + "adapter.XdkRecoveryTest", + "adapter.XdkPartialAnalysisTest", "adapter.CompilerCallSiteTest", "server.XdkModuleServerTest", - "server.XdkLanguageServerTest", "server.AdapterBackendTest", + "server.XdkLanguageServerTest", + "server.AdapterBackendTest", ] for suite in suites: result = ElementTree.parse(results / f"TEST-org.xvm.lsp.{suite}.xml").getroot() @@ -793,6 +814,15 @@ jobs: print("server.XdkStdioTest", counts) PYTEST + - name: Upload LSP test results and timings + if: always() && matrix.os == 'ubuntu-latest' + uses: actions/upload-artifact@v7 + with: + name: lsp-test-results-${{ env.ARTIFACT_SHA }} + path: lang/lsp-server/build/test-results/**/*.xml + if-no-files-found: ignore + retention-days: 7 + # The checks that lang validation ran passed; later runs of this pull request on the # same inputs skip them. - name: Record lang core checks pass diff --git a/lang/lsp-server/README.md b/lang/lsp-server/README.md index b01286043d8..9839fabeace 100644 --- a/lang/lsp-server/README.md +++ b/lang/lsp-server/README.md @@ -515,15 +515,32 @@ precise supported contexts and exclusions. ```bash # Build the project (with lang enabled) -./gradlew :lang:lsp-server:build +./gradlew :lang:lsp-server:build -PincludeBuildLang=true -PincludeBuildAttachLang=true -# Run tests -./gradlew :lang:lsp-server:test +# Run tests, including the extended retention workload +./gradlew :lang:lsp-server:test -PincludeBuildLang=true -PincludeBuildAttachLang=true # Create fat JAR with all dependencies -./gradlew :lang:lsp-server:fatJar +./gradlew :lang:lsp-server:fatJar -PincludeBuildLang=true -PincludeBuildAttachLang=true ``` +Tests use two separate JVM workers, each reusing its own bundled XDK repository. Test methods +remain sequential within each JVM because some tests change compiler globals. Use +`-PlspTestForks=1` to run with one worker on memory-constrained machines. + +PR CI runs the compiler and Tree-sitter regressions, packaged-server stdio tests, and a 36-cycle +retention test covering every expression/header variant. It passes `-PlspTestHeavy=false` to +exclude only JUnit tests tagged `heavy`, currently the extended 120-cycle retention workload. +Local runs include that workload by default; `-Plsp.retentionCycles=N` controls its length. +For a fresh run, add `--rerun-tasks --no-build-cache` and inspect the JUnit XML under +`build/test-results/test` and `build/test-results/compilerStdioTest`. + +To run the extended workload before merging, dispatch **Verify Commit** on the branch with +both **include-lang** and **lsp-heavy-tests** enabled. Master push builds include it as well. +CI keeps one compiler-default server artifact throughout the job; tests select the Tree-sitter +backend explicitly, avoiding a second full suite run caused by rebuilding the JAR with a different +default adapter. + ## Tree-sitter Native Library The tree-sitter adapter requires native libraries (`libtree-sitter-xtc`). These are built diff --git a/lang/lsp-server/build.gradle.kts b/lang/lsp-server/build.gradle.kts index 8a4d2a7053b..50dea2b4fe2 100644 --- a/lang/lsp-server/build.gradle.kts +++ b/lang/lsp-server/build.gradle.kts @@ -257,6 +257,21 @@ tasks.withType().configureEach { } tasks.test { + // Separate JVMs isolate compiler globals while each worker reuses its bundled XDK repository. + // Do not enable JUnit method concurrency: several regressions deliberately alter compiler state. + // Default to half the cores, capped because each worker holds its own XDK; -PlspTestForks overrides. + maxParallelForks = + providers + .gradleProperty("lspTestForks") + .map(String::toInt) + .orElse(providers.provider { (Runtime.getRuntime().availableProcessors() / 2).coerceIn(1, 8) }) + .get() + val includeHeavy = + providers + .gradleProperty("lspTestHeavy") + .orElse("true") + .get() + .toBooleanStrict() systemProperty( "xtc.lsp.retentionCycles", providers.gradleProperty("lsp.retentionCycles").orElse("120").get(), @@ -264,7 +279,10 @@ tasks.test { // Prefer the real logger over javatools' shaded no-op provider, without setting a provider // property on Gradle's separate bootstrap classloader (which cannot see test dependencies). classpath = configurations.testRuntimeClasspath.get().filter { it.name.startsWith("logback-classic-") } + classpath - useJUnitPlatform { excludeTags("compiler-stdio") } + useJUnitPlatform { + excludeTags("compiler-stdio") + if (!includeHeavy) excludeTags("heavy") + } testLogging { events("failed") } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt index 7ad49b4648b..319f23d8077 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkRetentionTest.kt @@ -2,6 +2,7 @@ package org.xvm.lsp.adapter import org.assertj.core.api.Assertions.assertThat import org.awaitility.Awaitility.await +import org.junit.jupiter.api.Tag import org.junit.jupiter.api.Test import org.junit.jupiter.api.io.TempDir import org.xvm.api.EmbeddingSupport @@ -20,13 +21,26 @@ import java.util.concurrent.TimeUnit.NANOSECONDS import java.util.concurrent.TimeUnit.SECONDS /** - * A bounded repeatable retention workload; it is not a multi-hour editor soak or a performance SLA. + * Verifies that dependency replacement, editing, renaming and cursor queries release compiler graphs. + * The regular test covers every cursor/header variant; the heavy test repeats the workload longer + * to expose accumulation across many requests. Neither test defines a performance SLA. */ class XdkRetentionTest { @TempDir lateinit var directory: Path @Test fun `source graphs replacements rename and cursor proofs release attempts after close and shutdown`() { + // Four headers, each selected once every nine cycles, plus all eight expression variants. + verifyRetention(36) + } + + @Test + @Tag("heavy") + fun `extended editing workload does not accumulate compiler graphs`() { + verifyRetention(CYCLES) + } + + private fun verifyRetention(cycles: Int) { CompilerTestSupport.configure() directory = directory.toRealPath() val support = EmbeddingSupport.instance() @@ -102,7 +116,7 @@ class XdkRetentionTest { "new Object(te|) { construct(String value) {} Int read() = box.number; };" to "textValue", ) - repeat(CYCLES) { cycle -> + repeat(cycles) { cycle -> adapter.replaceDependencies(listOf(artifacts[cycle % artifacts.size])) val started = System.nanoTime() val storm = @@ -167,7 +181,7 @@ class XdkRetentionTest { assertReleased(observed) val millis = timings.map(NANOSECONDS::toMillis).sorted() println( - "Retention workload: cycles=$CYCLES, edit requests=${CYCLES * 8}, weak references=${observed.size}, retained=0, " + + "Retention workload: cycles=$cycles, edit requests=${cycles * 8}, weak references=${observed.size}, retained=0, " + "rebuild p50=${millis[millis.size / 2]}ms, p95=${millis[(millis.size * 0.95).toInt()]}ms (includes debounce)", ) } diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt index 6f8fddcd9b1..06870f0d3f0 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt @@ -589,6 +589,8 @@ class XdkStdioTest { DocumentSymbolParams(TextDocumentIdentifier(URI)), ), ) + // Let the server write its final trace records before reading the log files. + session.shutdownAndExit() session.traceDirectory } val entries = @@ -611,10 +613,12 @@ class XdkStdioTest { assertThat(it["event"].asString).isEqualTo("end") assertThat(it["elapsedMs"].asDouble).isPositive() } - assertThat(entries).anySatisfy { + // Query completion and protocol replies share an operation name, but only replies have a boundary. + val requests = entries.filter { it["kind"].asString == "lsp-request" } + assertThat(requests).anySatisfy { assertThat(it["operation"]?.asString).isEqualTo("textDocument/documentSymbol") assertThat(it["event"].asString).isEqualTo("end") - assertThat(it["boundary"].asString).isEqualTo("server-reply-written") + assertThat(it["boundary"]?.asString).isEqualTo("server-reply-written") } assertThat(entries.toString()).doesNotContain("Int answer() = 42") } diff --git a/lang/vscode-extension/src/status-bar.ts b/lang/vscode-extension/src/status-bar.ts index 24aa2074c2f..df74e0a009e 100644 --- a/lang/vscode-extension/src/status-bar.ts +++ b/lang/vscode-extension/src/status-bar.ts @@ -19,13 +19,14 @@ export function updateStatusBar(state: 'starting' | 'ready' | 'stopped' | 'error statusBarItem.backgroundColor = undefined; statusBarItem.command = 'xtc.showServerOutput'; break; - case 'ready': + case 'ready': { const label = adapter === 'XDK' ? 'Compiler' : adapter === 'TreeSitter' ? 'Tree-sitter' : adapter; statusBarItem.text = `$(check) Ecstasy${label ? ` · ${label}` : ''}`; statusBarItem.tooltip = `Ecstasy Language Server: Ready${label ? ` (${label})` : ''} — click to switch adapter`; statusBarItem.backgroundColor = undefined; statusBarItem.command = 'xtc.selectLanguageAdapter'; break; + } case 'stopped': statusBarItem.text = '$(error) Ecstasy'; statusBarItem.tooltip = 'Ecstasy Language Server: Stopped - click to restart'; diff --git a/lang/vscode-extension/src/test/explorer-move.ts b/lang/vscode-extension/src/test/explorer-move.ts index 59ba2c3edec..fce64648e31 100644 --- a/lang/vscode-extension/src/test/explorer-move.ts +++ b/lang/vscode-extension/src/test/explorer-move.ts @@ -13,7 +13,7 @@ export async function cutAndPasteDirectories(sources: readonly vscode.Uri[], des await trace.command('workbench.files.action.focusFilesExplorer'); await trace.command('workbench.files.action.collapseExplorerFolders'); await trace.command('revealInExplorer', sources[0]); - for (const _ of sources.slice(1)) await trace.command('list.expandSelectionDown'); + for (let index = 1; index < sources.length; index++) await trace.command('list.expandSelectionDown'); await trace.command('copyFilePath'); const selected = (await vscode.env.clipboard.readText()).split(/\r?\n/).sort(); if (JSON.stringify(selected) === JSON.stringify(expected)) { diff --git a/lang/vscode-extension/src/test/playbook/completion.ts b/lang/vscode-extension/src/test/playbook/completion.ts index d1bf8ce031b..9a7b4b6f524 100644 --- a/lang/vscode-extension/src/test/playbook/completion.ts +++ b/lang/vscode-extension/src/test/playbook/completion.ts @@ -19,7 +19,7 @@ export function completionCases(): void { } }); - playbook('X7', async (workspace, data) => { + playbook('X7', async workspace => { const scenario = shared.completion; const document = await workspace.open(scenario.file); const incomplete = editScenario(fixture(scenario.file), scenario.edit); diff --git a/lang/vscode-extension/src/test/playbook/dependencies.ts b/lang/vscode-extension/src/test/playbook/dependencies.ts index 1171acf61a7..2c1bec2d673 100644 --- a/lang/vscode-extension/src/test/playbook/dependencies.ts +++ b/lang/vscode-extension/src/test/playbook/dependencies.ts @@ -20,7 +20,7 @@ async function linked(document: vscode.TextDocument, workspace: Workspace): Prom } export function dependencyCases(): void { - playbook('X45', async (workspace, data) => { + playbook('X45', async workspace => { const consumer = await workspace.dependencies(); const location = shared.dependencyNavigation.location; assert.ok(!vscode.window.visibleTextEditors.some(editor => editor.document.uri.toString() === workspace.uri(location.targetFile).toString())); @@ -139,7 +139,7 @@ export function dependencyCases(): void { } export function configurationCases(): void { - playbook('CFG1', async (workspace, data) => { + playbook('CFG1', async workspace => { const consumer = await workspace.dependencies(); assert.strictEqual(consumer.uri.toString(), workspace.uri(shared.configuration.consumer).toString()); await workspace.configure([]); @@ -163,7 +163,7 @@ export function configurationCases(): void { } finally { registration.dispose(); } }); - playbook('CFG3', async (workspace, data) => { + playbook('CFG3', async workspace => { const consumer = await workspace.dependencies(); const version = consumer.version; await client().sendNotification('workspace/didChangeConfiguration', { settings: null }); diff --git a/lang/vscode-extension/src/test/playbook/navigation.ts b/lang/vscode-extension/src/test/playbook/navigation.ts index 905512572fc..c6b427d492c 100644 --- a/lang/vscode-extension/src/test/playbook/navigation.ts +++ b/lang/vscode-extension/src/test/playbook/navigation.ts @@ -20,7 +20,7 @@ export function navigationCases(): void { assert.ok(selections[0].parent.range.contains(selections[0].range)); }); - playbook('X2', async (workspace, data) => { + playbook('X2', async workspace => { const scenario = shared.diagnostics; const document = await workspace.open(scenario.file); for (const expected of scenario.errors) { diff --git a/lang/vscode-extension/src/test/playbook/runtimeSettings.ts b/lang/vscode-extension/src/test/playbook/runtimeSettings.ts index aac9527e877..86ce0524459 100644 --- a/lang/vscode-extension/src/test/playbook/runtimeSettings.ts +++ b/lang/vscode-extension/src/test/playbook/runtimeSettings.ts @@ -4,7 +4,7 @@ import * as path from 'node:path'; import * as vscode from 'vscode'; import { stopLanguageClient } from '../../lsp-client'; import { readArchive } from '../archive'; -import { PlaybookCleanupFailure } from '../wait'; +import { withPlaybookCleanup } from '../wait'; import { WorkbenchUi } from '../workbenchUi'; import { catalog } from './shared'; import { client, noErrors, playbook } from './support'; @@ -21,7 +21,7 @@ export function runtimeSettingsCases(): void { const status = () => client().sendRequest('xtc/languageServiceStatus'); const before = await status(); const ui = await WorkbenchUi.connect(); - try { + await withPlaybookCleanup(async () => { await settings.update('java.vmOptions', data.vmOptions, vscode.ConfigurationTarget.Global); if (id === 'X270') await settings.update('server.logs', data.logs, vscode.ConfigurationTarget.Global); assert.strictEqual((await status()).pid, before.pid, 'Saving runtime settings must not restart a working server'); @@ -53,15 +53,14 @@ export function runtimeSettingsCases(): void { assert.strictEqual((await status()).pid, applied.pid); } assert.strictEqual(document.getText(), data.source); - } finally { + }, async () => { try { await settings.update('java.vmOptions', previousVm, vscode.ConfigurationTarget.Global); await settings.update('server.logs', previousLogs, vscode.ConfigurationTarget.Global); await vscode.commands.executeCommand('xtc.restartServer'); await vscode.commands.executeCommand('notifications.clearAll'); - } catch (error) { throw new PlaybookCleanupFailure([error]); } - finally { await ui.close(); } - } + } finally { await ui.close(); } + }); }); for (const id of ['X271', 'X272'] as const) playbook(id, async workspace => { const data = catalog.common.runtimeSettings; @@ -74,7 +73,7 @@ export function runtimeSettingsCases(): void { const unhandled: unknown[] = []; const rejected = (error: unknown) => { unhandled.push(error); }; process.on('unhandledRejection', rejected); - try { + await withPlaybookCleanup(async () => { if (id === 'X271') await stopLanguageClient(); else { await settings.update('java.vmOptions', data.startupFailureOptions, vscode.ConfigurationTarget.Global); @@ -99,19 +98,18 @@ export function runtimeSettingsCases(): void { assert.ok(entries.get('launcher.log')!.includes(data.startupFailureOptions[0])); assert.strictEqual(manifest.logs, undefined, 'Failed launch must not reuse an earlier successful session'); } - } finally { + }, async () => { try { await settings.update('java.vmOptions', original, vscode.ConfigurationTarget.Global); await vscode.commands.executeCommand('xtc.restartServer'); await noErrors(document.uri); assert.strictEqual(document.getText(), data.source); await vscode.commands.executeCommand('notifications.clearAll'); - } catch (error) { throw new PlaybookCleanupFailure([error]); } - finally { + } finally { process.off('unhandledRejection', rejected); await ui.close(); } - } + }); assert.deepStrictEqual(unhandled, [], 'Startup failures must reject the command without orphaned promise rejections'); }); diff --git a/lang/vscode-extension/src/test/suite/playbook-wait.test.ts b/lang/vscode-extension/src/test/suite/playbook-wait.test.ts index 7ad57193727..7578b4257bf 100644 --- a/lang/vscode-extension/src/test/suite/playbook-wait.test.ts +++ b/lang/vscode-extension/src/test/suite/playbook-wait.test.ts @@ -1,6 +1,6 @@ import * as assert from 'node:assert'; import Mocha from 'mocha'; -import { PlaybookCleanupFailure, PlaybookTimeout, stopOnUnsafeFailure, waitFor } from '../wait'; +import { mustStopPlaybook, PlaybookCleanupFailure, PlaybookTimeout, stopOnUnsafeFailure, waitFor, withPlaybookCleanup } from '../wait'; suite('Playbook timeout isolation', () => { test('condition timeout fails with its last observation', async () => { @@ -34,4 +34,34 @@ suite('Playbook timeout isolation', () => { assert.strictEqual(followingRan, false); } }); + + test('cleanup runs after success or failure and preserves both failures when restoration fails', async () => { + const assertion = new Error('Assertion failed'); + const restoration = new Error('Could not restore settings'); + for (const bodyFails of [false, true]) { + for (const cleanupFails of [false, true]) { + const calls: string[] = []; + const operation = withPlaybookCleanup(async () => { + calls.push('body'); + if (bodyFails) throw assertion; + }, async () => { + calls.push('cleanup'); + if (cleanupFails) throw restoration; + }); + if (cleanupFails) { + await assert.rejects(operation, error => { + assert.ok(error instanceof PlaybookCleanupFailure); + assert.deepStrictEqual(error.failures, bodyFails ? [assertion, restoration] : [restoration]); + assert.ok(mustStopPlaybook(error)); + return true; + }); + } else if (bodyFails) { + await assert.rejects(operation, error => error === assertion); + } else { + await operation; + } + assert.deepStrictEqual(calls, ['body', 'cleanup']); + } + } + }); }); diff --git a/lang/vscode-extension/src/test/wait.ts b/lang/vscode-extension/src/test/wait.ts index 16f17c99aee..f9570db746f 100644 --- a/lang/vscode-extension/src/test/wait.ts +++ b/lang/vscode-extension/src/test/wait.ts @@ -9,6 +9,16 @@ export class PlaybookCleanupFailure extends Error { constructor(readonly failures: readonly unknown[]) { super(`Playbook cleanup failed: ${failures.map(String).join('; ')}`); } } +/** Always restore test settings; retain the original failure if cleanup also fails and abort the run. */ +export async function withPlaybookCleanup(body: () => Promise, cleanup: () => Promise): Promise { + const failures: unknown[] = []; + try { await body(); } + catch (error) { failures.push(error); } + try { await cleanup(); } + catch (error) { throw new PlaybookCleanupFailure([...failures, error]); } + if (failures.length) throw failures[0]; +} + export function mustStopPlaybook(error: unknown): boolean { return error instanceof PlaybookTimeout || error instanceof PlaybookCleanupFailure || (error instanceof Error && error.name === 'TimeoutError') || From 6da9a5812ba448a657b2062230c02ae2ef77d5d5 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:14 +0100 Subject: [PATCH 014/324] Verify the LSP4IJ APIs the plugin uses, and prepare the runIde sandbox - Verify known LSP4IJ integration APIs explicitly. - Prepare the actual IntelliJ runIde sandbox. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../src/main/kotlin/IntellijRunIdeSupport.kt | 12 +- .../main/kotlin/VerifyPluginInternalApis.kt | 82 +++++++++++++ .../src/test/kotlin/InternalApiPolicyTest.kt | 112 ++++++++++++++++++ lang/intellij-plugin/README.md | 18 ++- lang/intellij-plugin/build.gradle.kts | 73 +++++++----- lang/intellij-plugin/verifier-lsp4ij-apis.txt | 60 ++++++++++ 6 files changed, 321 insertions(+), 36 deletions(-) create mode 100644 build-logic/common-plugins/src/main/kotlin/VerifyPluginInternalApis.kt create mode 100644 build-logic/common-plugins/src/test/kotlin/InternalApiPolicyTest.kt create mode 100644 lang/intellij-plugin/verifier-lsp4ij-apis.txt diff --git a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt index 041af74149d..ae6fc8cf0d5 100644 --- a/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt +++ b/build-logic/common-plugins/src/main/kotlin/IntellijRunIdeSupport.kt @@ -42,6 +42,14 @@ abstract class RunIdeEnvironmentReportTask : DefaultTask() { @get:PathSensitive(PathSensitivity.RELATIVE) abstract val sandboxDir: DirectoryProperty + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val sandboxLogDir: DirectoryProperty + + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val sandboxSystemDir: DirectoryProperty + @get:InputDirectory @get:PathSensitive(PathSensitivity.RELATIVE) abstract val mavenLocalRoot: DirectoryProperty @@ -55,8 +63,8 @@ abstract class RunIdeEnvironmentReportTask : DefaultTask() { @TaskAction fun report() { val sandbox = sandboxDir.get().asFile - val ideaLog = sandbox.resolve("log/idea.log") - val systemDir = sandbox.resolve("system") + val ideaLog = sandboxLogDir.get().file("idea.log").asFile + val systemDir = sandboxSystemDir.get().asFile val sandboxIsReused = systemDir.exists() && systemDir.listFiles().orEmpty().isNotEmpty() val xtcArtifacts = mavenLocalRoot.get().asFile.resolve("org/xtclang") diff --git a/build-logic/common-plugins/src/main/kotlin/VerifyPluginInternalApis.kt b/build-logic/common-plugins/src/main/kotlin/VerifyPluginInternalApis.kt new file mode 100644 index 00000000000..337262f297a --- /dev/null +++ b/build-logic/common-plugins/src/main/kotlin/VerifyPluginInternalApis.kt @@ -0,0 +1,82 @@ +import org.gradle.api.DefaultTask +import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.Property +import org.gradle.api.tasks.Input +import org.gradle.api.tasks.InputDirectory +import org.gradle.api.tasks.InputFile +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity +import org.gradle.api.tasks.TaskAction +import org.gradle.work.DisableCachingByDefault +import java.nio.file.Path + +/** + * Checks the verifier's internal-API reports against the exact LSP4IJ integration dependencies. + * Binary compatibility remains the verifier's responsibility. Unknown APIs, dependency upgrades and + * incomplete reports fail here, so accepting existing workarounds cannot hide new platform internals. + */ +@DisableCachingByDefault(because = "Verifies external tool reports without producing an artifact") +abstract class VerifyPluginInternalApis : DefaultTask() { + @get:InputDirectory + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val reportsDirectory: DirectoryProperty + + @get:InputFile + @get:PathSensitive(PathSensitivity.RELATIVE) + abstract val acceptedApis: RegularFileProperty + + @get:Input + abstract val lsp4ijVersion: Property + + @TaskAction + fun verify() { + val count = InternalApiPolicy.verify( + reportsDirectory.get().asFile.toPath(), acceptedApis.get().asFile.readLines(), lsp4ijVersion.get(), + ) + logger.lifecycle("Plugin internal API check: {} known LSP4IJ usages; no unreviewed internal APIs", count) + } +} + +/** Matches full API signatures, never package wildcards; unfamiliar report lines fail closed. */ +internal object InternalApiPolicy { + fun verify(reports: Path, policy: List, version: String): Int { + val entries = policy.map(String::trim).filter { it.isNotEmpty() && !it.startsWith('#') } + require(entries.firstOrNull() == "lsp4ij=$version") { + "Review the internal API policy for LSP4IJ $version before changing the dependency version" + } + val accepted = entries.drop(1).toSet() + require(accepted.isNotEmpty() && accepted.all { API.matches(it) }) { + "Internal API exceptions must name exact LSP4IJ classes or member signatures" + } + val verdicts = reports.toFile().walkTopDown().filter { it.name == "verification-verdict.txt" }.toList() + check(verdicts.isNotEmpty()) { "No Plugin Verifier verdicts found in $reports" } + return verdicts.sumOf { verdict -> + val text = verdict.readText() + val count = COUNT.find(text)?.groupValues?.get(1)?.toInt() ?: 0 + val report = verdict.resolveSibling("internal-api-usages.txt") + val usages = if (report.isFile) report.readLines().filter(String::isNotBlank) else emptyList() + check(usages.size == count) { "Incomplete internal API report beside $verdict: expected $count, found ${usages.size}" } + val dependencies = verdict.resolveSibling("dependencies.txt") + check(dependencies.isFile && DEPENDENCY.findAll(dependencies.readText()).any { it.groupValues[1] == version }) { + "Verifier dependency report does not contain the reviewed LSP4IJ $version beside $verdict" + } + val unknown = usages.filter { signature(it.substringBefore(" is ")) !in accepted } + check(unknown.isEmpty()) { "Unreviewed internal APIs in $report:\n${unknown.joinToString("\n")}" } + usages.size + } + } + + /** Debug information changes parameter names between reports; only their types form the API. */ + private fun signature(api: String): String = PARAMETERS.replace(api) { parameters -> + parameters.groupValues[1].split(',').joinToString(", ", "(", ")") { parameter -> + PARAMETER_NAME.replace(parameter.trim(), "$1") + } + } + + private val API = Regex("Internal (?:class|interface|method|constructor) com\\.redhat\\.devtools\\.lsp4ij\\.[^*]+") + private val PARAMETERS = Regex("\\(([^()]*)\\)") + private val PARAMETER_NAME = Regex("^([\\w.$]+(?:\\[\\])*) [\\w$]+$") + private val COUNT = Regex("(\\d+) usages? of internal API") + private val DEPENDENCY = Regex("com\\.redhat\\.devtools\\.lsp4ij:([^\\s]+)") +} diff --git a/build-logic/common-plugins/src/test/kotlin/InternalApiPolicyTest.kt b/build-logic/common-plugins/src/test/kotlin/InternalApiPolicyTest.kt new file mode 100644 index 00000000000..415feba257f --- /dev/null +++ b/build-logic/common-plugins/src/test/kotlin/InternalApiPolicyTest.kt @@ -0,0 +1,112 @@ +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertThrows +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.io.TempDir +import java.nio.file.Files +import java.nio.file.Path + +/** + * Protects the verifier gate against broad exceptions, dependency drift and incomplete reports. + * Uses report-format fixtures so new platform internals cannot silently inherit LSP4IJ exceptions. + */ +class InternalApiPolicyTest { + @TempDir lateinit var reports: Path + + private val api = "Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.getStartedServers() : java.util.Set" + private val policy get() = listOf("# Exact reviewed dependency", "lsp4ij=0.21.0", api) + + @Test + fun `accepts repeated uses of an exact signature across IDE reports`() { + report("IDE-A", listOf(usage(api), usage(api))) + report("IDE-B", listOf(usage(api))) + assertEquals(3, InternalApiPolicy.verify(reports, policy, "0.21.0")) + } + + @Test + fun `rejects new LSP4IJ members and IntelliJ internal APIs`() { + val unknown = "Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.newMethod() : void" + val platform = "Internal method com.intellij.ide.plugins.PluginManager.findEnabledPlugin() : void" + report("IDE", listOf(usage(api), usage(unknown), usage(platform))) + val error = assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + assertTrue(error.message!!.contains(unknown)) + assertTrue(error.message!!.contains(platform)) + } + + @Test + fun `a changed member signature requires review`() { + report("IDE", listOf(usage(api.replace("java.util.Set", "java.util.List")))) + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + } + + @Test + fun `debug parameter names do not change the API signature`() { + val member = "Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.hasAny" + val types = "(com.intellij.psi.PsiFile, java.util.function.Predicate) : boolean" + report("IDE-A", listOf(usage("$member(com.intellij.psi.PsiFile arg0, java.util.function.Predicate arg1) : boolean"))) + report("IDE-B", listOf(usage("$member(com.intellij.psi.PsiFile file, java.util.function.Predicate filter) : boolean"))) + assertEquals(2, InternalApiPolicy.verify(reports, listOf("lsp4ij=0.21.0", member + types), "0.21.0")) + } + + @Test + fun `parameter normalization preserves types order and arity`() { + val member = "Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.hasAny" + val policy = listOf("lsp4ij=0.21.0", "$member(com.intellij.psi.PsiFile, java.util.function.Predicate) : boolean") + for (parameters in listOf( + "com.intellij.psi.PsiElement file, java.util.function.Predicate filter", + "java.util.function.Predicate filter, com.intellij.psi.PsiFile file", + "com.intellij.psi.PsiFile file", + )) { + report("IDE", listOf(usage("$member($parameters) : boolean"))) + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + } + } + + @Test + fun `rejects both configured and verifier-resolved dependency upgrades`() { + report("IDE", listOf(usage(api)), dependency = "0.22.0") + assertThrows(IllegalArgumentException::class.java) { InternalApiPolicy.verify(reports, policy, "0.22.0") } + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + } + + @Test + fun `missing or truncated reports cannot pass`() { + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + val directory = report("IDE", listOf(usage(api)), count = 2) + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + Files.delete(directory.resolve("internal-api-usages.txt")) + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + } + + @Test + fun `a clean verdict does not require an empty usages file`() { + val directory = report("IDE", emptyList()) + Files.delete(directory.resolve("internal-api-usages.txt")) + assertEquals(0, InternalApiPolicy.verify(reports, policy, "0.21.0")) + } + + @Test + fun `unrecognized report text fails rather than being skipped`() { + report("IDE", listOf("Unrecognized internal API finding")) + assertThrows(IllegalStateException::class.java) { InternalApiPolicy.verify(reports, policy, "0.21.0") } + } + + @Test + fun `exception file cannot include platform APIs or package wildcards`() { + for (entry in listOf("Internal class com.intellij.InternalClass", "Internal class com.redhat.devtools.lsp4ij.*")) { + assertThrows(IllegalArgumentException::class.java) { + InternalApiPolicy.verify(reports, listOf("lsp4ij=0.21.0", entry), "0.21.0") + } + } + } + + private fun usage(symbol: String): String = "$symbol is invoked in org.xtclang.idea.lsp.Adapter.run() : void. This method is marked internal." + + private fun report(ide: String, usages: List, count: Int = usages.size, dependency: String = "0.21.0"): Path { + val directory = Files.createDirectories(reports.resolve("$ide/plugins/org.xtclang.idea/0.4.4-SNAPSHOT")) + Files.writeString(directory.resolve("verification-verdict.txt"), if (count == 0) "Compatible." else "Compatible. $count usages of internal API") + Files.writeString(directory.resolve("dependencies.txt"), "+--- com.redhat.devtools.lsp4ij:$dependency\n") + Files.writeString(directory.resolve("internal-api-usages.txt"), usages.joinToString("\n")) + return directory + } +} diff --git a/lang/intellij-plugin/README.md b/lang/intellij-plugin/README.md index 8b0bdf87d41..1f4062e8f0c 100644 --- a/lang/intellij-plugin/README.md +++ b/lang/intellij-plugin/README.md @@ -113,6 +113,16 @@ The plugin invokes `xtc init` to scaffold the project, then imports it as a Grad ### Build Artifacts +`verifyPlugin` also runs `verifyPluginInternalApis`. Binary incompatibilities, invalid plugins +and missing dependencies fail verification. Internal API usages are checked against +[`verifier-lsp4ij-apis.txt`](verifier-lsp4ij-apis.txt), which records the exact LSP4IJ 0.21.0 +signatures used for live-connection ownership, semantic-cache invalidation and coalesced editor +refresh. These remain unsupported upstream APIs; the list documents a compatibility risk, not a +public-API guarantee. Other internal APIs, changed signatures and LSP4IJ version changes fail the +check. Complete verifier reports are retained under `build/reports/pluginVerifier` and uploaded +by CI. Revisit the exceptions when upgrading LSP4IJ, and remove them as public replacements are +adopted. Publishing requires both checks. + | Task | Output | Use Case | |------|--------|----------| | `buildPlugin` | `build/distributions/*.zip` | Install in any IntelliJ instance | @@ -331,8 +341,8 @@ stale sandbox state, or missing artifacts: [runIde] Path: .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3 [runIde] Status: reused (existing sandbox with IDE caches/indices) [runIde] Plugins: [intellij-plugin, lsp4ij] -[runIde] IDE log: .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log -[runIde] tail -f .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log +[runIde] IDE log: .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log_runIde/idea.log +[runIde] tail -f .../lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log_runIde/idea.log [runIde] ─── mavenLocal XTC Artifacts ─── [runIde] ~/.m2/repository/org/xtclang [runIde] xdk: 0.4.4-SNAPSHOT @@ -380,9 +390,9 @@ These appear with a `[lsp-server]` prefix whenever the LSP server is active. noisy (indexing, VFS, GC, etc.). To view them in a separate terminal: ```bash -tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log +tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log_runIde/idea.log # Or filter to XTC-related entries: -tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log/idea.log | grep -i "xtc\|lsp" +tail -f lang/.intellijPlatform/sandbox/intellij-plugin/IU-2026.2.3/log_runIde/idea.log | grep -i "xtc\|lsp" ``` **LSP server file log** (always available, even outside `runIde`): diff --git a/lang/intellij-plugin/build.gradle.kts b/lang/intellij-plugin/build.gradle.kts index a52f1d9b831..ca8668f2b4a 100644 --- a/lang/intellij-plugin/build.gradle.kts +++ b/lang/intellij-plugin/build.gradle.kts @@ -9,6 +9,7 @@ import org.gradle.api.tasks.TaskAction import org.jetbrains.intellij.platform.gradle.IntelliJPlatformType import org.jetbrains.intellij.platform.gradle.TestFrameworkType import org.jetbrains.intellij.platform.gradle.models.ProductRelease +import org.jetbrains.intellij.platform.gradle.tasks.PrepareSandboxTask import org.jetbrains.intellij.platform.gradle.tasks.VerifyPluginTask import java.io.File @@ -487,13 +488,10 @@ intellijPlatform { // cost ~1 GB and a verification per IDE. failureLevel is explicit so the build fails // on real breaks but not on every deprecated/experimental API touch. // - // INTERNAL_API_USAGES is included so the build fails if the plugin calls an - // IntelliJ API marked @ApiStatus.Internal. Such APIs are not meant for client - // plugins and can change without notice; gating on them here is what catches a - // regression like a switch to PluginManager.findEnabledPlugin (internal) over - // PluginManagerCore.getPlugin (public). Previously these were only logged as - // "Compatible. N usages of internal API" and silently passed CI while still - // generating a JetBrains verifier email on every Marketplace upload. + // Internal API reports are checked by verifyPluginInternalApis below. LSP4IJ marks + // several required integration points internal; a version-bound list names those + // exact signatures. Other internal APIs (including IntelliJ Platform internals) + // still fail, and the complete usage report remains available for review. pluginVerification { ides { latest { @@ -507,7 +505,6 @@ intellijPlatform { VerifyPluginTask.FailureLevel.COMPATIBILITY_PROBLEMS, VerifyPluginTask.FailureLevel.INVALID_PLUGIN, VerifyPluginTask.FailureLevel.MISSING_DEPENDENCIES, - VerifyPluginTask.FailureLevel.INTERNAL_API_USAGES, ) } } @@ -529,7 +526,18 @@ val summarizeSearchableOptions = manifestFile.set(layout.buildDirectory.file("tmp/buildSearchableOptions/content.json")) } -val verifyPlugin = tasks.named("verifyPlugin") +val verifyPlugin = tasks.named("verifyPlugin") +val verifyPluginInternalApis = + tasks.register("verifyPluginInternalApis") { + group = "verification" + description = "Reject unreviewed internal APIs in the IntelliJ Plugin Verifier reports." + reportsDirectory.set(verifyPlugin.flatMap { it.verificationReportsDirectory }) + acceptedApis.set(layout.projectDirectory.file("verifier-lsp4ij-apis.txt")) + lsp4ijVersion.set(libs.versions.lang.intellij.lsp4ij) + } + +// A direct verifyPlugin invocation must enforce the same policy as publishing. +verifyPlugin.configure { finalizedBy(verifyPluginInternalApis) } val publishPlugin = tasks.named("publishPlugin") { @@ -538,7 +546,7 @@ val publishPlugin = // Enforce binary-compat verification before publishing, regardless of who // invokes publishPlugin (CI, local, snapshot pipeline). Catches removed/ // changed APIs and missing dependencies that would otherwise reach users. - dependsOn(verifyPlugin) + dependsOn(verifyPluginInternalApis) } // Searchable options allow IntelliJ's Settings search (Cmd+Shift+A / Ctrl+Shift+A) to index @@ -671,9 +679,20 @@ val prepareJarSearchableOptions = // Some bundled plugins have split frontend/backend architectures that fail to // load cleanly in the sandbox. Disable them to avoid spurious errors. -// Derive sandbox config dir from prepareSandbox (handles versioned sandbox directories like IU-2025.3.1.1) -// prepareSandbox.destinationDirectory is the plugins/ directory, config/ is a sibling -val sandboxConfigDir: Provider = prepareSandbox.flatMap { it.destinationDirectory.dir("../config") } +// The platform plugin gives runIde its own sandbox. Add off-classpath assets to that producer +// so its Sync task owns the complete plugin installation, including the server and grammar. +val prepareRunIdeSandbox = + tasks.named("prepareSandbox_runIde") { + from(textMateGrammar) { + into("intellij-plugin/lib/textmate") + } + from(lspServerJar) { + into("intellij-plugin/bin") + rename { "xtc-lsp-server.jar" } + } + } + +val sandboxConfigDir: Provider = prepareRunIdeSandbox.flatMap { it.sandboxConfigDirectory } // Plugins to disable in sandbox (Ultimate-only or problematic split-architecture plugins) val disabledSandboxPlugins = @@ -688,9 +707,7 @@ val configureDisabledPlugins = group = "intellij platform" description = "Disable Ultimate-only plugins in the sandbox IDE" - // Must run after prepareSandbox creates the directory structure - dependsOn(prepareSandbox) - mustRunAfter(prepareSandbox) + dependsOn(prepareRunIdeSandbox) val configDir = sandboxConfigDir // Capture for configuration cache val pluginsList = disabledSandboxPlugins @@ -712,8 +729,7 @@ val configureSandboxLogging = group = "intellij platform" description = "Configure INFO-level logging for XTC plugin in the sandbox IDE" - dependsOn(prepareSandbox) - mustRunAfter(prepareSandbox) + dependsOn(prepareRunIdeSandbox) val configDir = sandboxConfigDir outputs.file(configDir.map { it.file("options/log-categories.xml") }) @@ -738,8 +754,7 @@ val configureSandboxAppearance = group = "intellij platform" description = "Remove broken user-derived color-scheme overrides from the sandbox IDE config" - dependsOn(prepareSandbox) - mustRunAfter(prepareSandbox) + dependsOn(prepareRunIdeSandbox) val configDir = sandboxConfigDir doNotTrackState("Removes invalid overrides from user-managed sandbox settings on each IDE launch") @@ -790,8 +805,7 @@ val runIdeLspLogDirectory = val runIdeInfo = tasks.register("runIdeInfo") { dependsOn( - copyTextMateToSandbox, - copyLspServerToSandbox, + prepareRunIdeSandbox, configureDisabledPlugins, configureSandboxLogging, configureSandboxAppearance, @@ -801,16 +815,17 @@ val runIdeInfo = lsp4ijVersion.set(runIdeCapturedLsp4ijVersion) pluginVersion.set(runIdeCapturedPluginVersion) semanticTokensEnabled.set(runIdeCapturedSemanticTokens) - sandboxDir.set(sandboxConfigDir.map { it.dir("..") }) + sandboxDir.set(prepareRunIdeSandbox.flatMap { it.sandboxDirectory }) + sandboxLogDir.set(prepareRunIdeSandbox.flatMap { it.sandboxLogDirectory }) + sandboxSystemDir.set(prepareRunIdeSandbox.flatMap { it.sandboxSystemDirectory }) val mavenLocalRoot = providers .systemProperty("maven.repo.local") .orElse(providers.systemProperty("user.home").map { "$it/.m2/repository" }) this.mavenLocalRoot.set(layout.dir(mavenLocalRoot.map(::File))) pluginNames.set( - sandboxConfigDir.map { configDir -> - configDir - .dir("../plugins") + prepareRunIdeSandbox.flatMap { it.sandboxPluginsDirectory }.map { pluginsDir -> + pluginsDir .asFile .listFiles() ?.map { it.name } @@ -833,14 +848,12 @@ val stopLspLogTail = threadName.set("lsp-log-tailer") } -// Ensure TextMate files, LSP server JAR, and mavenLocal artifacts are ready before IDE starts -// NOTE: finalizedBy doesn't guarantee completion, so we need explicit dependsOn +// Prepare the interactive sandbox and publish its Gradle dependencies before starting the IDE. val runIde = tasks.named("runIde") { parentPublishLocal.forEach { dependsOn(it) } dependsOn( - copyTextMateToSandbox, - copyLspServerToSandbox, + prepareRunIdeSandbox, configureDisabledPlugins, configureSandboxLogging, configureSandboxAppearance, diff --git a/lang/intellij-plugin/verifier-lsp4ij-apis.txt b/lang/intellij-plugin/verifier-lsp4ij-apis.txt new file mode 100644 index 00000000000..9ef7e00f4c0 --- /dev/null +++ b/lang/intellij-plugin/verifier-lsp4ij-apis.txt @@ -0,0 +1,60 @@ +# Exact internal API dependencies of the compiler-backed LSP4IJ integration. +# These are risk exceptions, not public API guarantees. Keep the verifier's complete reports. +# Revisit on every LSP4IJ upgrade; do not add package wildcards or IntelliJ Platform APIs. +# Parameter names are omitted because they depend on available debug information. +# Missing classes/methods and other binary incompatibilities still fail verifyPlugin. +# +# LanguageServiceAccessor / server wrappers: identify our live connection without starting +# unrelated servers; preserve lifecycle ownership for custom requests and refactorings. +# LSPFileSupport: invalidate stale semantic caches after compiler/project changes; restarting +# the entire server would lose the document-version and request ownership guarantees. +# EditorFeature renderers: refresh hints, lenses and tokens in one coalesced read/UI pass, +# avoiding upstream's per-document scheduling storm while retaining native rendering. +# Public LanguageServerManager start/stop APIs cannot replace these connection/cache hooks. +# Remove entries when upstream exposes equivalent public APIs and the adapter migrates. +lsp4ij=0.21.0 + +Internal class com.redhat.devtools.lsp4ij.LSPFileSupport +Internal class com.redhat.devtools.lsp4ij.LanguageServiceAccessor +Internal class com.redhat.devtools.lsp4ij.internal.editor.CodeVisionEditorFeature +Internal class com.redhat.devtools.lsp4ij.internal.editor.DeclarativeInlayHintsEditorFeature +Internal class com.redhat.devtools.lsp4ij.internal.editor.SemanticTokensEditorFeature +Internal constructor com.redhat.devtools.lsp4ij.internal.editor.CodeVisionEditorFeature.() +Internal constructor com.redhat.devtools.lsp4ij.internal.editor.DeclarativeInlayHintsEditorFeature.() +Internal constructor com.redhat.devtools.lsp4ij.internal.editor.SemanticTokensEditorFeature.() +Internal interface com.redhat.devtools.lsp4ij.internal.editor.EditorFeature +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getCallHierarchyIncomingCallsSupport() : com.redhat.devtools.lsp4ij.features.callHierarchy.LSPCallHierarchyIncomingCallsSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getCallHierarchyOutgoingCallsSupport() : com.redhat.devtools.lsp4ij.features.callHierarchy.LSPCallHierarchyOutgoingCallsSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getCodeLensSupport() : com.redhat.devtools.lsp4ij.features.codeLens.LSPCodeLensSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getCompletionSupport() : com.redhat.devtools.lsp4ij.features.completion.LSPCompletionSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getDefinitionSupport() : com.redhat.devtools.lsp4ij.features.navigation.LSPDefinitionSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getDocumentSymbolSupport() : com.redhat.devtools.lsp4ij.features.documentSymbol.LSPDocumentSymbolSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getHighlightSupport() : com.redhat.devtools.lsp4ij.features.highlight.LSPHighlightSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getHoverSupport() : com.redhat.devtools.lsp4ij.features.documentation.LSPHoverSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getImplementationSupport() : com.redhat.devtools.lsp4ij.features.implementation.LSPImplementationSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getInlayHintsSupport() : com.redhat.devtools.lsp4ij.features.inlayhint.LSPInlayHintsSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getIntentionCodeActionSupport() : com.redhat.devtools.lsp4ij.features.codeAction.intention.LSPIntentionCodeActionSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getPrepareCallHierarchySupport() : com.redhat.devtools.lsp4ij.features.callHierarchy.LSPPrepareCallHierarchySupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getPrepareRenameSupport() : com.redhat.devtools.lsp4ij.features.rename.LSPPrepareRenameSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getPrepareTypeHierarchySupport() : com.redhat.devtools.lsp4ij.features.typeHierarchy.LSPPrepareTypeHierarchySupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getReferenceSupport() : com.redhat.devtools.lsp4ij.features.references.LSPReferenceSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getRenameSupport() : com.redhat.devtools.lsp4ij.features.rename.LSPRenameSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getSemanticTokensSupport() : com.redhat.devtools.lsp4ij.features.semanticTokens.LSPSemanticTokensSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getSignatureHelpSupport() : com.redhat.devtools.lsp4ij.features.signatureHelp.LSPSignatureHelpSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getSupport(com.intellij.psi.PsiFile) : com.redhat.devtools.lsp4ij.LSPFileSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getTypeDefinitionSupport() : com.redhat.devtools.lsp4ij.features.typeDefinition.LSPTypeDefinitionSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getTypeHierarchySubtypesSupport() : com.redhat.devtools.lsp4ij.features.typeHierarchy.LSPTypeHierarchySubtypesSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.getTypeHierarchySupertypesSupport() : com.redhat.devtools.lsp4ij.features.typeHierarchy.LSPTypeHierarchySupertypesSupport +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.hasSupport(com.intellij.psi.PsiFile) : boolean +Internal method com.redhat.devtools.lsp4ij.LSPFileSupport.restartDaemonCodeAnalyzerWithDebounce(org.eclipse.lsp4j.jsonrpc.CancelChecker) : void +Internal method com.redhat.devtools.lsp4ij.LanguageServerItem.getServerWrapper() : com.redhat.devtools.lsp4ij.LanguageServerWrapper +Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.getInstance(com.intellij.openapi.project.Project) : com.redhat.devtools.lsp4ij.LanguageServiceAccessor +Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.getLanguageServers(com.intellij.psi.PsiFile, java.util.function.Predicate, java.util.function.Predicate) : java.util.concurrent.CompletableFuture +Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.getStartedServers() : java.util.Set +Internal method com.redhat.devtools.lsp4ij.LanguageServiceAccessor.hasAny(com.intellij.psi.PsiFile, java.util.function.Predicate) : boolean +Internal method com.redhat.devtools.lsp4ij.client.LanguageClientImpl.setServerWrapper(com.redhat.devtools.lsp4ij.LanguageServerWrapper) : void +Internal method com.redhat.devtools.lsp4ij.client.features.LSPClientFeatures.getServerWrapper() : com.redhat.devtools.lsp4ij.LanguageServerWrapper +Internal method com.redhat.devtools.lsp4ij.internal.editor.EditorFeature.clearEditorCache(com.intellij.openapi.editor.Editor, com.intellij.openapi.project.Project) : void +Internal method com.redhat.devtools.lsp4ij.internal.editor.EditorFeature.clearLSPCache(com.intellij.psi.PsiFile) : void +Internal method com.redhat.devtools.lsp4ij.internal.editor.EditorFeature.collectUiRunnable(com.intellij.openapi.editor.Editor, com.intellij.psi.PsiFile, java.util.List) : void +Internal method com.redhat.devtools.lsp4ij.internal.editor.EditorFeature.shouldProcess(com.intellij.psi.PsiFile) : boolean From a68eb6a77d2d072353d96bd11b78ccdd6c4ec5a8 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:15 +0100 Subject: [PATCH 015/324] Document compiler support for the language server - Explain native playbook runs and manual LSP verification. - Document compiler support for the language server. - Refer to visible compiler diagram labels in the guide. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/LSP-COMPILER.md | 1238 ++++++++++++++++++++++++++++++++++ javatools/README.md | 486 +------------ lang/doc/manual-test-plan.md | 101 ++- 3 files changed, 1345 insertions(+), 480 deletions(-) create mode 100644 javatools/LSP-COMPILER.md diff --git a/javatools/LSP-COMPILER.md b/javatools/LSP-COMPILER.md new file mode 100644 index 00000000000..211e99ea86f --- /dev/null +++ b/javatools/LSP-COMPILER.md @@ -0,0 +1,1238 @@ +# Compiler support for the language server + +The compiler-backed language server uses the Ecstasy compiler to answer questions about source +code: which declaration a name refers to, which overload a call selects, what types are visible +at a cursor, and whether an edit preserves those relationships. This document explains the +changes to `javatools` that make those answers available, why each change is needed, and where +the language server consumes it. + +The central change is to make compilation an operation that a host can own and inspect. A host +supplies a consistent source snapshot, dependencies, a diagnostic destination and a cancellation +policy. The compiler returns the structures and facts that the operation actually established. +An unsuccessful compilation can expose useful syntax and partial progress without claiming that +it produced an executable module. + +These mechanisms are general Java embedding APIs. `javatools` does not depend on LSP4J, LSP4IJ, +IntelliJ, VS Code, TextMate or tree-sitter. Protocol messages, document versions, scheduling, +editor presentation and refactoring edits remain in `lang/lsp-server`. + +This guide describes the combined implementation, including supporting changes that can be +reviewed or merged independently. The [diagnostic design and migration guide](#diagnostics-cancellation-and-reporting-scopes) +below includes the error-listener rationale and API examples. Java source links are relative to +this directory and can be followed from a rendered Markdown document. + +## Contents + +- [Why the previous compiler interface was insufficient](#why-the-previous-compiler-interface-was-insufficient) +- [One editor request through the compiler](#one-editor-request-through-the-compiler) +- [Source snapshots and compilation outcomes](#source-snapshots-and-compilation-outcomes) +- [Diagnostics, cancellation and reporting scopes](#diagnostics-cancellation-and-reporting-scopes) +- [Why explicit probes and a final propagated listener](#why-explicit-probes-and-a-final-propagated-listener) +- [Preserving semantic facts from successful validation](#preserving-semantic-facts-from-successful-validation) +- [Declaration analysis and unfinished source](#declaration-analysis-and-unfinished-source) +- [AST traversal, copying and the partial-analysis package](#ast-traversal-copying-and-the-partial-analysis-package) +- [Constant pools, caches and dependency inputs](#constant-pools-caches-and-dependency-inputs) +- [Why ordinary AST and runtime files also changed](#why-ordinary-ast-and-runtime-files-also-changed) +- [Compatibility and limits](#compatibility-and-limits) +- [Tests and reproduction](#tests-and-reproduction) +- [Appendix: file-by-file compiler changes](#appendix-file-by-file-compiler-changes) + +## Why the previous compiler interface was insufficient + +A batch compiler can report errors, abandon its intermediate state and return failure. An editor +must continue helping while that failure is the normal state of the buffer. It must also avoid +publishing an answer from an older version after the user has typed something else. + +| Previous limitation | Concrete editor failure | Compiler mechanism added or corrected | +| --- | --- | --- | +| A failed embedding compilation returned no module and lost access to its intermediate state. | An erroneous method body could leave the host with no way to inspect the declarations or source tree that had already been built. | `Compilation` retains available syntax, file structure, pool and validated binding facts independently of executable success. | +| Disk discovery and source reading were coupled to the CLI's module loading. | A saved module root and an unsaved member file could be analyzed as different revisions; virtual or newly created members were difficult to represent. | `ModuleInfo` exposes source-text and membership hooks while preserving normal package/resource assembly. | +| Listener substitution, null-to-discard conventions and implicit stop behavior obscured who owned diagnostics. | Reports could disappear during a probe or cached lookup; a host could be stopped by a budget it had not chosen. | Explicit reporting ownership, branches, suppression reasons, stateful collectors and abort queries. | +| Useful bindings were temporary validation decisions, or were rewritten away. | Name matching could select the wrong overload; constant folding could make a reference disappear from rename. | Attempt-owned call collectors, initializer provenance and source-binding accessors. | +| A valid parse and completed validation were prerequisites for many semantic queries. | Completion after a member-access dot or inside an unfinished argument could not use the real lexical scope. | Explicit cursor analysis, bounded recovery nodes and compiler-fitted candidate facts. | +| Compiler structures and working context were mutable and sometimes retained operation state. | Repeated editor requests could retain old listeners or accidentally inspect constants under the wrong working pool. | Scoped reporting/validation state, explicit pool scopes and detached adapter snapshots. | + +Parsing command-line output cannot recover selected overloads, local-register identity or facts +discarded by a rewrite. A separate editor type checker would have to reproduce Ecstasy's access, +generic inference, composition and overload rules. Instead, the compiler exports decisions where +it already makes them, and partial queries reuse its existing validation and fitting operations. + +## One editor request through the compiler + +```mermaid +flowchart TD + A[Editor document and workspace revisions] --> B[LSP adapter captures source and dependency inputs] + B --> C[Serialized compiler worker] + C --> D[EmbeddingSupport and the ordinary compiler pipeline] + D --> E[Attempt-owned diagnostics, syntax, structures and bindings] + E --> F[SemanticModelBuilder copies facts into detached models] + F --> G[Adapter checks that input revisions are still current] + G --> H[LSP response or diagnostic publication] +``` + +The ownership transition happens when **SemanticModelBuilder copies facts into detached models**. +An immutable collection of AST references is still a collection of compiler-owned objects. The host +must finish inspecting and copying them while it exclusively owns the attempt; it cannot hand the +AST or constant pool to arbitrary request threads. + +The production path is visible in these files: + +| Responsibility | Implementation | +| --- | --- | +| Capture module text, membership, URI aliases and resources; create fresh compilation inputs. | [XdkSources.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkSources.kt) | +| Schedule and serialize work, cancel obsolete attempts, and gate publication against current inputs. | [XdkAdapter.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAdapter.kt) | +| Call and time the embedding APIs for ordinary, dependency and refactoring compilations. | [CompilerCalls.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerCalls.kt) | +| Drive parsing, linking, resolution, validation and emission, or stop at the requested analysis goal. | [EmbeddingSupport.java](src/main/java/org/xvm/api/EmbeddingSupport.java), [tool/Compiler.java](src/main/java/org/xvm/tool/Compiler.java), [compiler/Compiler.java](src/main/java/org/xvm/compiler/Compiler.java) | +| Convert compiler objects and source positions to independently owned semantic data. | [SemanticModelBuilder.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModelBuilder.kt), [SemanticModel.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/SemanticModel.kt), [PartialSemanticModel.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/PartialSemanticModel.kt) | + +The compiler does not know a document's LSP version or decide whether a response is stale. The +adapter supplies cancellation and rejects superseded results. Compiler cancellation checkpoints +make that policy effective during compilation rather than only after it finishes. + +## Source snapshots and compilation outcomes + +### Configure compiler libraries without starting an interpreter + +`EmbeddingSupport.configure` accepts the core repository used by compilation. The `XDK_HOME` +fallback includes both `lib/` and `javatools/`: the compiler needs the bootstrap turtle module +(`mack.xtclang.org`) and native bridge (`_native.xtclang.org`), not just the ordinary libraries. +Missing a bootstrap module prevents compilation before the user's source can be analyzed. + +The language server instead supplies its bundled compiler libraries through +[XdkLibraries.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkLibraries.kt). Library +setup, compiler attempts and runtime/interpreter initialization are separate responsibilities; +loading the compiler's required modules does not mean executing the user's program. + +### Reuse normal module assembly with host-provided inputs + +[ModuleInfo.java](src/main/java/org/xvm/tool/ModuleInfo.java) still implements module discovery, +member/package assembly and resource association. Its host hooks let the adapter replace the +inputs to that machinery: + +- `readSource(File)` supplies the captured characters for a source file. +- `sourceEntries(File)` supplies a directory's captured source membership, including added or + removed members, instead of rediscovering membership during the attempt. +- The protected `(File sourceFile, String moduleName)` constructor establishes a source identity + without requiring the source root to exist on disk. Subclasses provide the corresponding source + tree and resource behavior. +- `getSourceTree(errors, parser)` allows a selected member to use cursor-aware parsing while the + other members use ordinary parsing and the same assembly path. +- `getParsedSources()` exposes the available per-source trees, including syntax retained when an + error prevents construction of a usable assembled root. + +The LSP implementation overrides these hooks in `XdkSources`. Open buffers take precedence over +disk text. Source URIs, source membership and captured resource inputs belong to the same analysis +request. Refactoring proofs can replay captured inputs with proposed text or path changes without +first writing those changes to the user's files. + +**Use a fresh `ModuleInfo` for each attempt.** It caches assembled compiler trees. The adapter +therefore retains immutable input values and creates a fresh `XdkSources` when replaying them, +rather than treating a previously parsed `ModuleInfo` as a reusable immutable snapshot. + +### Return progress without calling failure success + +The `compileModule(Source, ...)` and `compileModule(ModuleInfo, ...)` overloads return +`EmbeddingSupport.Compilation`. Its fields have deliberately different meanings: + +| Result member | What the host can rely on | +| --- | --- | +| `module()` / `succeeded()` | An executable module was produced successfully. A failed attempt has no successful module. | +| `file()` / `pool()` | Registration got far enough to construct a file structure and its constant pool. Their existence alone does not establish successful validation. | +| `ast()` / `parsed()` | The assembled source root is available after loading/parsing succeeded. Semantic validation may still have failed. | +| `sourceTrees()` | Available per-source syntax, including recovered trees when parsing failed. Positions belong to each tree's own `Source`; recovered trees may not yet have parent links. | +| Binding maps | Facts established by the relevant successful validations and retained by the collector. Their presence does not turn the whole compilation into a successful one. | + +`EmbeddingSupport` creates a fresh embedded compiler for the attempt and drives the normal tool +compiler pipeline. It retains state as each phase reaches it, instead of converting every failure +into a single null. Expected compiler aborts remain distinct from unexpected exceptions and +assertions, which are reported as internal failures even if an earlier source diagnostic exists. + +The existing convenience APIs in [ModuleCompiler.java](src/main/java/org/xvm/api/ModuleCompiler.java) +and `EmbeddingSupport` keep their module/null or boolean result shapes. Hosts that need intermediate +state use `compileModule` explicitly. + +For example, with `embedding` already configured and `dependencies` supplied by the host: + +```java +var reports = new ErrorList(); +var obsolete = new AtomicBoolean(); +var errors = ErrorListener.cancellable(reports, obsolete::get); +var result = embedding.compileModule(new Source(text, documentName), dependencies, errors); + +if (result.succeeded()) { + ModuleStructure module = result.module(); + // The executable result is available. +} +if (result.pool() != null) { + try (var scope = ConstantPool.withPool(result.pool())) { + // Inspect available compiler facts and copy the values needed by the host. + // This is still the compiler worker, not an arbitrary LSP request thread. + } +} +// The host still checks its input revision before publishing reports or copied facts. +``` + +The host can set `obsolete` from its request lifecycle. Cancellation need not produce a source +error, so the absence of diagnostics is not a substitute for checking whether an attempt is current. + +## Diagnostics, cancellation and reporting scopes + +### Why the old reporting model had to change + +The old error-listener API did not provide a reliable contract for embedding the compiler. +Accepting a listener at the entry point was not enough: deeper calls could substitute `BLACKHOLE`, +accept null as an implicit request to discard reports, or find a different listener through a +shared structure. The caller could receive only part of an attempt's diagnostics without knowing +anything had been lost. Suppression, ownership and stopping policy were spread across unrelated +call sites, so adding a callback at the outer API could not repair what happened underneath it. + +This caused observable failures. The default branch for a host listener imposed a one-error +budget the host had never requested: the lexer stopped at the first recoverable error and never +reported the next one. A previously created branch could also ignore its parent's later abort +request. Two regression tests reproduce those failures on the old compiler with exactly the same +test source that passes on the repaired implementation. + +Reporting also had inconsistent control-flow effects. `log` returned an abort decision, but the +fallback `RuntimeErrorListener` instead threw an exception for ERROR and FATAL. Selecting a +diagnostic destination could therefore change how the operation failed. An editor must be able to +collect an ordinary source error as data, distinguish it from an internal compiler failure, and +let the compiler stop at an appropriate checkpoint. The destination receiving a report should not +introduce a different failure path merely because that report has a particular severity. + +### Changes and enhancements + +* **Separate reporting from stopping.** `log(ErrorInfo)` delivers a diagnostic; + `isAbortDesired()` answers whether work should stop. The lexer and parser retain explicit stop + checkpoints, and forwarding listeners preserve the owner's policy. +* **Make host callbacks usable by the compiler.** `collecting` forwards structured diagnostics + while remembering severity and error codes. A bare callback receives reports but does not + implement those state queries automatically. +* **Make branching consistent.** A host's default branch buffers without an arbitrary one-error + limit. `ErrorList` branches retain their configured budget, and both observe their parent's + current abort request. +* **Make suppression explicit.** Named reasons distinguish probes, cascade suppression and + intentional discards. A derived silence discards messages while retaining the owner's stop + policy. +* **Require a reporting destination at active compilation boundaries.** An omitted listener is + rejected before compilation starts; intentional silence is supplied as an explicit listener. +* **Keep ownership stable.** Constructor-owned listeners remain attached to their operation. + Temporary suppression derives a local listener instead of overwriting the original destination. +* **Preserve diagnostic structure.** `Site` describes source spans, structures or unpositioned + reports. Severity helpers take message arguments as trailing varargs; `tee` forwards reports to + two destinations while combining their state. + +### Removed behavior and its replacement + +* **The boolean return from `log`** is replaced by a reporting call followed by an explicit policy + query where the caller needs to stop. There is one continuation query for wrappers to preserve. +* **`BLACKHOLE` and `BlackholeErrorListener`** are replaced by named suppression factories. + Removing the old symbols makes unmigrated call sites fail to compile, requiring each caller to + identify its reason for silence and whether a parent's abort policy must survive. +* **Null-to-discard fallback at compilation boundaries** is replaced by a required listener. + Passing `silent(DISCARD)` still supports a caller that deliberately wants no reports; accidentally + omitting the destination no longer looks like a successful choice of silence. +* **The silent default in `ResolutionCollector.getErrorListener()`** is removed. Implementations + must provide their destination, and `SimpleCollector` retains the listener it was given. +* **Reassigning a TypeInfo build's listener to `BLACKHOLE`** is replaced by local cascade + suppression. The original listener continues to identify the operation and its stop policy. +* **Severity-triggered exceptions in the fallback runtime listener** are replaced by recorded + abort state. The compiler or runtime caller decides how to end its operation. This does not + remove the command-line launcher's deliberate fatal-error and exit handling. +* **The listener parameter on `Expression.testFitAsType`** is removed because that operation is + a probe. Its staging and fit checks use explicit probe silence; selected validation can still + retain and merge its diagnostics. + +The legacy positional reporting overloads remain available after recompilation, but are +deprecated in favor of `Site` and trailing arguments. Their source re-anchoring behavior is tested. + +### Why explicit probes and a final propagated listener + +A probe asks a question whose answer can legitimately be “no.” For example, overload resolution +can test whether a written argument fits an `Int` parameter, reject that candidate and then +select a `String` overload. Reporting the rejected fit as a source error would diagnose a valid +program. Completion performs similar trials on proposed arguments. Silent probing is therefore +necessary; the redesign makes its policy explicit instead of removing it. + +The old representations combined several different decisions: + +| Previous pattern | What a reviewer or embedding host could not determine reliably | Explicit replacement | +| --- | --- | --- | +| Pass `null` and let a deeper helper choose a sink. | Was silence deliberate, was the caller missing a listener, and which helpers would discard, substitute or dereference it? | Require a listener at the active boundary; pass an explicit silence when that is the intended policy. | +| Pass `BLACKHOLE` for every kind of suppression. | Was this an alternative fit, a cascade after an existing error, or a deliberately unused destination? Should the current attempt's abort request still apply? | Name the suppression reason and choose standalone silence versus a wrapper derived from the current owner. | +| Reassign the operation's listener to a discard sink. | Which later validations or callbacks still reach the original owner, and can they still see cancellation or its error budget? | Keep the owner stable and derive a local listener for only the affected calls. | +| Store a listener on a shared structure and find it implicitly. | Which request owns a later cached lookup, particularly after the structure outlives the request? | Pass the current attempt's listener through compiler operations; retain diagnostics as replayable data rather than retaining their previous destination. | + +The operation-owned references in `Compiler`, `StageMgr`, `Launcher` and resolution collectors +are final where their lifetime has one owner. This prevents accidental reassignment to a sink or +another request's destination. The compiler passes that owner, or a deliberately derived child, +through the calls that perform the work. It is one **logical reporting owner**, not a requirement +that every nested operation receive the exact same listener object. + +A branch, tee, cancellation wrapper or derived silence can legitimately be a different object. +What matters is preserving the required relationship to the owner. Diagnostic retention and +error counters can change while the reference remains final. Final references do not make the +listener, parser or compiler thread-safe, nor do they make an AST immutable. + +In particular, suppressing a message and suppressing a stop request are different decisions: + +| Operation | Appropriate listener | Reports and stopping | +| --- | --- | --- | +| Validate the selected source construct. | The operation's listener. | Real diagnostics reach their owner; its state and stop policy remain available. | +| Ask an alternative-fit question within an active attempt. | `errors.silence(PROBE)`. | Trial messages are discarded, but the current parent abort/cancellation request remains visible. | +| Perform a deliberately independent silent query with no parent policy. | `silent(PROBE)`. | No reports, retained errors or abort request; this is the standalone behavior formerly supplied by a blackhole. | +| Try work whose diagnostics may be needed if it is accepted. | `errors.branch(node)`. | Reports are buffered; merge accepted reports once. The branch also observes parent abort and its own configured policy. | +| Suppress follow-on errors from already incomplete information. | `errors.silence(CASCADE)`. | Avoids flooding the owner with consequences while retaining its stop request. | + +This distinction is why replacing every old `BLACKHOLE` with the real reporting listener would +also be wrong: rejected alternatives would become user errors. Conversely, replacing everything +with standalone `silent(PROBE)` would not preserve host cancellation. Existing standalone probe +sites remain standalone where that is their deliberate contract; they do not acquire cancellation +propagation merely because the factory has a clearer name. Work that must observe the active +attempt's policy needs a derived listener and a checkpoint that queries it. + +For instance, related trials within one operation can share a derived probe: + +```java +final ErrorListener probe = errors.silence(ErrorListener.Silence.PROBE); +var firstFit = first.testFit(context, candidateType, false, probe); +var secondFit = second.testFit(context, candidateType, false, probe); +// The fit results answer the trial questions; selected validation still receives errors. +``` + +The wrapper reads the parent's current abort state; it does not snapshot that state when created. +Creating it before two branches therefore does not lose a later cancellation request. It must +still belong to that operation, not be cached globally or reused for a different compilation. +The standalone `silent(PROBE)` factory already returns a shared stateless sink, so repeated calls +do not allocate a fresh listener. A local variable is useful when it makes a group of trials +clearer, not as a required allocation optimization. Buffers and branches are different: they +retain diagnostics and must not be shared between unrelated trials just to shorten the code. + +`PROBE`, `CASCADE` and `DISCARD` are explanatory reasons, not levels or compiler modes. Changing +the enum value does not change how messages are discarded. The substantive improvement is explicit +ownership, appropriate retention and stop propagation. Keeping a deprecated `BLACKHOLE` alias was +technically possible; removing it forces old call sites to be reviewed instead of silently keeping +an unexplained policy. A caller that deliberately wants the old independent discard behavior can +still choose it explicitly. + +Non-null active listeners also eliminate two meanings for the same value: an intentionally silent +operation and an accidentally omitted destination. Boundary checks reject the latter early; +annotations describe that contract to Java/Kotlin callers and IDEs. This does not forbid meaningful +absence elsewhere, such as an inactive resolver having no current reporting scope. + +Some temporary destination changes remain necessary for parser speculation and parameterless +resolution callbacks. They are enclosed in `Reporting.Scope`, with restoration on nested, normal +and exceptional exits, rather than arbitrary reassignment of the operation's owner. The scoped +mechanism below is the deliberate exception to permanent ownership, not a return to ambient +listener swapping. + +### Why this is a prerequisite for the compiler-backed LSP + +**Reliable diagnostic ownership and abort propagation are required for the compiler-backed LSP.** +The adapter must associate reports with the requested compilation, reject unusable semantic +results, and stop obsolete work at compiler checkpoints. It cannot reconstruct diagnostics that +the compiler discarded or obtain errors from work that an unintended one-error budget prevented. +Leaving those failures in place blocks a reliable compiler-backed adapter, regardless of how much +code is added around the public entry point. These changes establish the reporting foundation; +the result, source-snapshot and cached-diagnostic APIs described in this guide complete the host-facing path. +The exact helper names and the decision to remove a legacy alias are API design choices. The +delivery, state and stopping guarantees they implement are the essential requirements. + +### Compatibility with ordinary compilation + +Normal command-line compilation continues to use the existing compiler pipeline, console and +exit policy. Users do not need new flags or changes to their Ecstasy source. The CLI supplies an +explicit discard listener only for its external delegate; its console still reports diagnostics. +`ErrorList` retains its configured serious-error budget, fatal reports still request an abort, +and the lexer/parser still stop when the applicable policy requires it. The changes repair +diagnostic delivery and listener ownership without introducing an IDE dependency into the compiler. + +There is strong regression evidence for that continuity: the full Java suite for this listener +migration executed 418 tests successfully, and all 24 rebuilt XDK modules matched a freshly built +master baseline after normalizing only module creation timestamps. The [focused checks](#historical-listener-regression-evidence) add +direct coverage of reporting, aborts, boundary validation and launcher behavior. These historical results +support confidence in ordinary compilation; they are evidence for the tested paths, not an +exhaustive guarantee for every source program or external integration. + +**The Java embedding API deliberately changes at the planned 0.5.0 boundary.** Existing Java +callers and listener implementations must migrate and recompile. Changed return types, removed +symbols, rejected nulls and the runtime listener's new exception behavior prevent a claim of +100% source or binary compatibility, even for a non-LSP Java consumer. The migration steps below +identify those changes explicitly so ordinary consumers can preserve their intended behavior. + +### Code tour and practical usage + +#### 1. Give each compilation its own diagnostic owner + +Start at [ModuleCompiler](src/main/java/org/xvm/api/ModuleCompiler.java) or +[EmbeddingSupport](src/main/java/org/xvm/api/EmbeddingSupport.java). Supply the attempt's listener +at the entry point and keep it available until the result and diagnostics have been consumed. +For example, with a configured core repository and source text: + +```java +final var errors = new ErrorList(100); +var compiler = new ModuleCompiler(coreRepository); +var module = compiler.compile(sourceText, null, errors); +var diagnostics = List.copyOf(errors.getErrors()); + +if (module == null || errors.hasSeriousErrors()) { + // Publish the diagnostics and do not treat this as a successful compilation. + return; +} +// The caller can now use the successfully compiled module. +``` + +The null argument here is the optional dependency repository, not the listener. The required +listener also reaches launcher construction and dispatch, the compiler, lexer/parser, +[StageMgr](src/main/java/org/xvm/compiler/ast/StageMgr.java), anonymous-class construction and +[Component.SimpleCollector](src/main/java/org/xvm/asm/Component.java). +[ResolutionCollector](src/main/java/org/xvm/asm/ComponentResolver.java) implementations must +return their selected destination. AST reporting and validation helpers no longer silently +accept a missing listener. + +Use a fresh collector for an independent attempt. An `ErrorList` is mutable; even `clear()` keeps +its previously seen diagnostic UIDs. A new operation should not inherit that deduplication history. +Hosts must serialize compiler access and listener callbacks. Final references and non-null +parameters do not make compiler objects or their accumulated state thread-safe. + +#### 2. Report data, then make the control-flow decision + +[ErrorListener](src/main/java/org/xvm/asm/ErrorListener.java) remains the central interface. +Its nested `ErrorInfo` carries severity, code, message arguments and location. `Site.In`, `Site.At` +and `Site.None` describe the location independently of the message: + +```java +errors.error(code, ErrorListener.in(source, start, end), arguments); +if (errors.isAbortDesired()) { + return; +} +``` + +Use `in(source, start, end)` for source text, `at(structure)` for an assembled structure, and +`NOWHERE` for a report without either location. Source positions are the encoded values returned +by `Source.getPosition()`, not raw byte offsets or LSP positions. The `error`, `warn`, `info` and +`fatal` helpers select severity and delegate to `log`; they do not transfer control themselves. + +`isAbortDesired()` and `hasSeriousErrors()` answer different questions. An operation can have +errors while remaining within its budget, and a host may request a stop without reporting an +error. Callers use the first query at stop checkpoints and the second when deciding whether a +result is usable. `hasError(code)` checks whether a particular diagnostic was recorded. + +`RuntimeErrorListener` now prints diagnostics and records an abort request for ERROR/FATAL +instead of deliberately throwing from `log`. Its abort flag remains set for that instance's +lifetime; the shared `RUNTIME` sink is not a fresh per-attempt collector. A reporting callback +can still throw its own exception, which forwarding helpers propagate. The redesign removes the +fallback sink's severity-driven exception policy, not Java exception handling in general. + +#### 3. Choose retention, callback delivery or both + +[ErrorList](src/main/java/org/xvm/asm/ErrorList.java) retains the first report for each diagnostic +UID in insertion order. ERROR/FATAL reports spend its count budget; warnings do not. Its +no-argument constructor uses the shared `ErrorListener.DEFAULT_MAX_ERRORS` (512), `FIRST_ERROR` +selects one, and `UNLIMITED` disables the count limit. A retained FATAL requests an abort with +any budget. Pass an explicit limit to `ErrorList(int)` when an operation needs a different policy. + +When a host wants every report delivered to a callback, use: + +```java +ErrorListener errors = ErrorListener.collecting(hostDiagnostics::add); +``` + +`collecting` forwards every report, including duplicates, and tracks severity and codes before +calling the consumer. It has no count budget and requests an abort on FATAL. A bare lambda +implements only `log` and inherits state queries that return false, so it is not a substitute +when the compiler relies on those queries. + +`tee(first, second)` delivers to both destinations in order and observes either destination's +abort, serious-error and code state. This supports reporting to a host while recording diagnostics +for later replay. `ErrorList.logTo(destination)` replays retained reports without clearing them; +deduplication belongs to the destination. If the first destination throws, the second is not called. + +#### 4. Buffer speculative diagnostics until a result is accepted + +`branch(node)` creates an `ErrorList.BranchedErrorListener`. Pass it to speculative work and merge +it once if that work's diagnostics should be retained: + +```java +var trial = errors.branch(node); +// Run the candidate validation with trial as its listener. +if (keepDiagnostics) { + trial.merge(); +} +``` + +Unmerged diagnostics remain local to the branch. A default host branch has an unlimited count +budget, while an `ErrorList` branch copies its parent's configured limit. Both observe the +parent's current abort request, and a local FATAL still stops the branch. A node-associated +branch re-anchors structure reports to that node's source span; explicit source spans remain intact. + +Merging an unbranched listener returns that listener. Merging a branch forwards its reports and +returns the parent without clearing the buffer. Do not repeatedly merge an accepted branch: +an `ErrorList` parent can deduplicate replay, but an arbitrary callback need not. + +#### 5. Suppress reports without losing the operation's owner + +`Silence` and `SilentErrorListener`, nested in `ErrorListener`, make suppression explicit. +`PROBE`, `CASCADE` and `DISCARD` are **reasons, not severity levels**. They have identical discard +behavior; compiler algorithms must not depend on which name is selected. + +| Reason | Use it when | +|---|---| +| `PROBE` | Testing an alternative, such as whether an expression fits a candidate type. Failure is the answer to that query, not necessarily a problem in the selected program. | +| `CASCADE` | Examining already incomplete information whose follow-on errors would obscure the original failure. | +| `DISCARD` | Deliberately ignoring this destination, such as the CLI's external delegate when its console already reports diagnostics. | + +The important behavioral distinction is between a standalone sink and a derived silence: + +```java +// No parent listener or stop policy: an independent, deliberately silent probe. +ErrorListener independent = ErrorListener.silent(ErrorListener.Silence.PROBE); + +// Suppress follow-on reports but continue to honor this operation's abort request. +ErrorListener quiet = errors.silence(ErrorListener.Silence.CASCADE); +``` + +`independent` never requests an abort. `quiet.isAbortDesired()` observes `errors`, even when +no diagnostic caused that stop request. Neither retains discarded messages; use a branch when +diagnostics might later be needed. `suppressed()` exposes a derived silence's parent, not a buffer +of hidden diagnostics. Re-silencing an existing silence returns the same instance and reason. + +For a separately constructed standalone sink, `new SilentErrorListener(reason)` needs no +explicit null parent. The two-argument constructor retains an optional parent for callers that +need its stop policy. Required factory arguments and results use `@NotNull`; intentional absence +uses `@Nullable`, including `suppressed()` on a standalone sink. Public construction boundaries +still reject invalid null arguments immediately; annotations also expose the contract to IDEs +and Kotlin callers. + +The names make intent searchable and reviewable. `BLACKHOLE` did not distinguish those purposes, +and substituting it for an active listener also removed access to that listener's stop policy. +Removing the old constant and class makes missed migrations visible to the Java compiler. A +deprecated alias was possible, but would allow unexplained suppression to survive the migration. +Callers that want exactly the old standalone discard behavior can use `silent(DISCARD)`. + +Stable ownership is equally important. In +[TypeConstant](src/main/java/org/xvm/asm/constants/TypeConstant.java), `cascade(incomplete, errors)` +derives suppression at affected calls instead of assigning a sink to the `errors` parameter. +The original destination remains available throughout the build. Constructor-owned references, +such as those in `StageMgr`, `SimpleCollector` and `Launcher`, are final so they cannot drift to +another destination during the operation. Their listeners' counters can still change. + +Non-null means that an active reporting boundary has an explicit owner or an explicit discard +policy. It does not mean that every listener field everywhere must be permanently non-null or +final: nested parser/resolver work needs temporary destinations and scoped restoration. That +ownership work is described separately below. + +#### 6. Migrate Java callers and implementations + +The planned release boundary is **0.5.0**. Recompile Java clients and listener implementations +after applying these changes; Java cannot retain compatibility overloads that differ only by +the return type of `log`. + +| Previous use | Replacement and consequence | +|---|---| +| `if (errors.log(...))` | Report first, then query `errors.isAbortDesired()` and perform the same return/throw at that checkpoint. Update custom implementations to `void log(ErrorInfo)`. | +| `BLACKHOLE` or `BlackholeErrorListener` | Choose a named standalone silence or derive one from the current owner when its abort policy must survive. | +| Null at a compilation/launcher boundary | Pass the operation's listener or explicit `silent(DISCARD)`. Missing listeners now fail at the boundary. | +| A `ResolutionCollector` relying on the default sink | Implement `getErrorListener()` and retain the intended destination. | +| `testFitAsType(..., errors)` | Remove the listener argument; the operation owns its probe policy. | +| Depending on `RUNTIME.log` to throw on severity | Check `isAbortDesired()` and explicitly choose the operation's failure handling. | +| Legacy positional reporting overloads | They remain callable after recompilation but are deprecated; migrate to `Site` and trailing message arguments. | + +### Give temporary reporting state a bounded lifetime + +`Parser.attempt()` replaces `Parser.SafeLookAhead`: use `Attempt.keep()` in place of +`keepResults()`. A kept attempt merges its buffered diagnostics, including warnings; a discarded +attempt restores its tokens and recovery state. Nested attempts observe their parent's abort +request. This removes a public nested parser class and requires source migration/recompilation at +the same breaking release boundary described above. + +Parser and resolver destinations use `Reporting` scopes, closed in reverse order with +try-with-resources. `NameResolver.getErrorListener()` is null outside `resolve`; callbacks borrow +the caller's non-null listener only during that call. These scopes restore destinations on every +exit. They do not make a parser, resolver, AST or listener safe for concurrent use. Lexical +reports also follow the active attempt, preserving lexical rollback from the parser correction. +Module-name-only scans can supply an explicit discard listener when reports are unwanted. + +Loop and try/finally label variables retain their context/listener as one immutable +`ValidationScope` value. Each statement restores its previous value on normal, early and +exceptional exits, and `Statement.validate` likewise restores its common validation context. +The temporary references stay on the AST because label-variable and jump callbacks need the +currently validating statement. No shared mutable holder is added to cloned AST nodes. +`EvalCompiler` and `ModuleInfo.Node` own final diagnostic buffers from construction onward. + +The unit regressions cover nested reporting, failed host/resolver callbacks, token rollback, +abort propagation and exceptional statement exits without requiring installed modules. Compile +and run the existing `manualTests/src/main/x/loop.x` and `exceptions.x` exercises with the built +XDK to check lazy label variables and try/finally behavior through code generation and execution. +The separate Gradle compiler-consumer test supplies its compiled modules through dependencies. + +### Replay diagnostics from cached type information + +Structures no longer discover the current listener through a mutable listener attached to a file +or constant pool. Explicit inspection supplies it, for example through +`TypeConstant.ensureTypeInfo(errors)` and `XvmStructure.log(errors, ...)`. + +[TypeConstant.java](src/main/java/org/xvm/asm/constants/TypeConstant.java) retains diagnostics from +a successful TypeInfo build and replays them on later explicit lookups. This fixes the case where +a silent probe populated a cache before the selected source use: the real consumer must still +receive the warning. An `ErrorList` deduplicates repeated reports; a raw callback can observe each +replay. Invalidation replaces the cached facts and their retained reports. + +The no-argument TypeInfo lookup remains an explicitly silent metadata query. Diagnostic-sensitive +code must choose the listener-taking operation. Some inspections build TypeInfo and can report new +diagnostics; they are compiler work, not harmless getters safe to invoke on any thread. + +### Cancel obsolete work without manufacturing errors + +`ErrorListener.cancellable` decorates the operation listener, preserving reporting, state, +branching and merging while polling the host predicate at abort checkpoints. A valid token stream +also polls; cancellation cannot depend on finding a source error. Module-member loading and phase +boundaries retain the same policy. This is cooperative cancellation, not arbitrary interruption: +the host must keep its predicate true once an attempt becomes obsolete. Other attempts have +separate policies, and the adapter still checks input revisions before publishing their results. + +## Preserving semantic facts from successful validation + +### Names, declarations and source identity + +The source AST carries locations that assembled components alone cannot provide. Tooling accessors +expose written declaration-name tokens, documentation, local registers, resolved name targets, +import aliases and qualified-name components. Relevant changes are in: + +- [NameExpression](src/main/java/org/xvm/compiler/ast/NameExpression.java) and + [NamedTypeExpression](src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java): resolved + targets and per-token type-name bindings. +- [ImportStatement](src/main/java/org/xvm/compiler/ast/ImportStatement.java): written alias and + qualified-name tokens, plus the imported identity. +- [Parameter](src/main/java/org/xvm/compiler/ast/Parameter.java), + [VariableDeclarationStatement](src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java), + [MethodDeclarationStatement](src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java), + [PropertyDeclarationStatement](src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java) + and [TypeCompositionStatement](src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java): + written names and the compiler identities associated with declarations. + +The adapter can consequently distinguish two same-spelled names in different scopes, a reference +to an import alias from a reference to its target, and a written declaration from a generated one. +These identities support navigation, references, rename and theme-based semantic highlighting. +They do not define colors in the compiler or introduce a public graphics/color library. + +### Capture selected calls before rewrites obscure them + +[InvocationBinding.java](src/main/java/org/xvm/compiler/InvocationBinding.java) records a selected +method identity, its instantiated signature and the association between written argument spans +and visible parameter indices. Named-argument labels retain their written ranges. Synthetic +default arguments are not presented as arguments the user wrote. If a rewrite prevents a reliable +association, the collector omits that tooling fact rather than guessing or changing compilation. + +Method calls, function-valued calls and constructor calls remain distinct. A function-valued +invocation can have a validated signature without having a statically selected runtime method; +`FunctionCall` records the former without inventing the latter. Constructors are keyed by their +`NewExpression` nodes and carry the same kind of argument provenance. + +The collector belongs to the compilation attempt. It is passed through +[Compiler](src/main/java/org/xvm/compiler/Compiler.java), +[StageMgr](src/main/java/org/xvm/compiler/ast/StageMgr.java), method/root contexts, deferred bodies +and nested compilation. [InvocationExpression](src/main/java/org/xvm/compiler/ast/InvocationExpression.java) +and [NewExpression](src/main/java/org/xvm/compiler/ast/NewExpression.java) record facts when real +validation succeeds. Retried validation invalidates earlier entries. Publication retains only +eligible nodes surviving in the final tree, excluding discarded speculative clones, and clears +the collector's scratch state. + +Maps use AST identity, not structural equality. Their collection wrappers are immutable, but the +AST nodes and constants remain owned by the compiler worker. Ordinary compiler constructors use +`Collector.NONE`; normal compilation does not need to retain optional tooling call maps. + +This gives signature help and parameter hints the compiler's actual argument mapping. It also +lets rename and call hierarchy distinguish overloads, instead of treating all calls with the same +spelling as references to one declaration. + +### Keep references that constant folding would discard + +Successful constant-initializer compilation can discard the temporary method used to validate an +initializer. [InitializerBinding.java](src/main/java/org/xvm/compiler/InitializerBinding.java) +captures source references, expression types and selected calls before that method disappears, +and anchors them to the surviving source property. + +For example, if an initializer refers to another constant, replacing the initializer with its +computed value must not erase the original occurrence from references or rename. The retained +record stores source spans and compiler constants, not the temporary clone nodes, contexts, +registers or mutable tokens. The adapter then copies the needed identities into its detached model. + +### Relate generated implementation details to written source + +[LambdaBindings.java](src/main/java/org/xvm/compiler/ast/LambdaBindings.java) distinguishes written +lambda parameters from synthetic capture parameters and maps capture registers back to their +source registers. [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java) +provides the corresponding origin information for anonymous-class captures. A captured local +must still rename as the same source variable even when generated methods or properties access it. + +For implementation and super navigation, +[MethodInfo.getSuperMethod](src/main/java/org/xvm/asm/constants/MethodInfo.java) exposes a written +super-dispatch target when one exists, without generating forwarding code. +[MethodBody.getIntoMethodInfo](src/main/java/org/xvm/asm/constants/MethodBody.java) exposes an +existing mixin constraint chain. Field, native and synthetic implementation details are not +misrepresented as source methods. The adapter's +[CompilerMethodRelations.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMethodRelations.kt) +and [CompilerPropertyRelations.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerPropertyRelations.kt) +turn that metadata into navigation facts. + +## Declaration analysis and unfinished source + +These are separate operations with narrower guarantees than successful compilation: + +| Operation | Work performed | Result guarantee | +| --- | --- | --- | +| `compileModule` | Ordinary parse, registration, linking/resolution, validation and emission. | Successful module only when compilation succeeds; available partial progress otherwise. | +| `analyzeDeclarations` | Ordinary parsing and declaration resolution, stopping before body validation/emission. | `Optional` containing a resolved declaration graph; no executable artifact. Syntax, linkage, declaration failure or cancellation prevents a successful result. | +| `analyzeIncomplete` | Explicitly selected partial parsing plus ordinary compiler inspection of supported intact syntax. | `PartialAnalysis` with available syntax and proved cursor facts; never a promise of an executable module or a selected incomplete call. | + +### Recover syntax without validating damaged code as if it were complete + +[Parser.java](src/main/java/org/xvm/compiler/Parser.java) can retain useful syntax after errors by +recovering at statement/declaration boundaries. The host still sees those errors. An ordinary +`compileModule` attempt does not proceed to semantic compilation merely because a recovered tree +exists. This preserves structural information for diagnostics and outlines without making an +invalid program valid. + +Cursor-aware parsing is enabled explicitly with `Parser.forPartialAnalysis`, through +`EmbeddingSupport.analyzeIncomplete`. It retains the original source text and positions, including +declarations after the cursor. It does not truncate the document and rebase later locations, nor +does it replace every syntax error with invented valid code. + +The overload without an explicit cursor is deliberately narrow: a trailing incomplete expression +statement at EOF. The explicit-cursor overloads support a broader set of editing contexts, including +one selected member file in a module snapshot. They must receive a position produced by +`Source.getPosition()` for the exact source being analyzed. + +### Retain a bounded incomplete site and inspect its real context + +The explicit-cursor path supports the following groups; the +[API Javadoc](src/main/java/org/xvm/api/EmbeddingSupport.java) defines the detailed boundaries: + +| Editing context | Retained information | +| --- | --- | +| A member-access suffix, bare-name prefix or empty statement boundary. | Written prefix, receiver where available, visible variables/types and enclosing-instance access. | +| An unfinished call, including named and nested arguments. | Written argument positions and labels, candidate signatures, argument mappings and candidate value insertions. Assignment, initializer, return and compound-expression contexts remain attached to their real enclosing syntax. | +| Construction, including inner/virtual/annotated types. | Ordinary type preparation and constructor fitting, including available generic inference. Anonymous construction prepares the declaration and fits constructors without emitting forwarding constructors or running capture analysis. | +| A single-dimensional array size slot. | Fitting against the fixed-size constructor's size parameter. A following supplier is parsed for recovery but is not proved by this prefix analysis; multidimensional construction is unsupported. | +| Missing delimiters or a terminator around a supported site. | Bounded missing parentheses, brackets, angle/group closers and, at an EOF cursor, block braces. No invented operand, declaration name/type or literal contents. | +| Unfinished member/return/parameter types and declaration headers. | Visible types, qualified-name components and written formals with their shadowing constraints. Unfinished methods/properties do not register completed declarations or invented parameters. A root module header registers only its written namespace/core import. | + +The embedding implementation requires one selected innermost incomplete site and rejects semantic +analysis if other serious syntax errors remain outside its recognized boundary. Recognized boundary +diagnostics still reach the host. Repeated delivery of the same boundary report is deduplicated; +other errors and unexpected failures remain visible. + +The incomplete node inspects intact children in the actual compiler context and prevents the +incomplete operation from becoming an emitted method. A non-null pool only means semantic work +started. Child types are usable only when the child validated successfully with a fitting result; +placeholder types left by failed validation are not semantic proof. + +### Export scope and candidate facts while the context exists + +[CursorBinding.java](src/main/java/org/xvm/compiler/CursorBinding.java) captures narrowed local +types and readability, the enclosing receiver, visible type names, method/function candidates, +written argument mappings and compiler-fitted insertion proposals. Written formals can hide outer +names without being assigned fictitious completed declaration identities. + +`callsInspected` distinguishes **no fitting candidates** from **the call was not inspected**. +That prevents an empty list from being interpreted as evidence that no overload is legal when +fitting never ran. A candidate whose written arguments fit is still a candidate: missing arguments +cannot establish that the complete operation will validate or select that method. + +Candidate fitting uses ordinary compiler operations on disposable syntax. Variable/property reads, +literals, enclosing-instance expressions and supported source templates are checked in context; +they are not accepted solely by comparing type names in the LSP server. The adapter consumes the +copied results through [XdkCursorQueries.kt](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkCursorQueries.kt) +and its completion/signature-help code. + +## AST traversal, copying and the partial-analysis package + +### Give a source-tree copy a precise contract + +[AstNode.copyTree](src/main/java/org/xvm/compiler/ast/AstNode.java) names the operation that the +older `clone()` API left implicit. It preserves the concrete node type, source positions and +compilation stage, recursively copies registered syntax children, and parents copied children +to their copied owners. Copying does not reparent the original tree. The copy's root initially +retains its original parent until the caller adopts it elsewhere. + +This is **not a fresh parse or a deep copy of the compiler**. The default implementation still +starts with shallow cloning and reflectively copies registered child fields. Non-child bindings +and transient fields can remain shared unless an override handles them. A validated clone must +not be treated as an independent unvalidated program. + +`Expression`, `Statement`, `TypeExpression` and specialized nodes use covariant `copyTree` return +types, so callers do not need to cast a copied expression back from `AstNode`. Constructor-based +partial-node copies adopt their children explicitly and preserve root metadata with +`copyTreeMetadataTo`. Legacy `clone()` delegates to the same implementation, avoiding two competing +copy contracts. + +The related `childNodes()` API starts a fresh traversal for each iterator. `children()` retains +its existing one-shot mutable traversal semantics for compiler passes. Neither is a synchronized +snapshot; callers must own the tree and copy a list if they need a stable collection. + +### Keep partial-analysis policy out of ordinary node implementations + +The incomplete syntax and fitting helpers live under +[`org.xvm.compiler.ast.partial`](src/main/java/org/xvm/compiler/ast/partial): + +| Classes | Responsibility | +| --- | --- | +| `IncompleteStatement`, `IncompleteExpression` | Retain a selected unfinished operation and its intact children in statement/expression contexts. | +| `IncompleteLocalDeclaration`, `IncompleteDeclarationStatement`, `IncompleteTypeCompositionStatement` | Preserve written declaration/header syntax without inventing successfully registered declarations. | +| `PartialSyntax` | Inspect retained sites and deferred anonymous source bodies without changing child ownership. | +| `PartialQueries`, `CursorScope` | Coordinate cursor inspection and capture the visible lexical scope. | +| `PartialCallResolver`, `PartialConstructionResolver` | Fit call/constructor candidates with the ordinary compiler's rules. | +| `PartialArgument`, `ProposedLiteralToken` | Build disposable proposed arguments and retain appropriate source anchoring for trials. | + +Java subpackages do not share package-private access. The single documented +[PartialAst.java](src/main/java/org/xvm/compiler/ast/PartialAst.java) bridge exposes the ordinary +parenting, validation, type inspection, scope and fitting operations needed by these helpers. +Existing AST operations keep their protected/package-private visibility. The five coordinating +helpers (`PartialQueries`, `CursorScope`, `PartialCallResolver`, `PartialConstructionResolver` and +`PartialArgument`) remain package private. + +The bridge is public for Java package access, **not a supported embedding facade**. Its arguments +are live compiler nodes and contexts. Hosts use `EmbeddingSupport` results; the bridge neither +installs proposals in the source tree nor implements an alternative type checker. Parser hooks +and a few ordinary-node hooks remain necessary because only those components know the syntactic +or validation context in which the incomplete site occurs. + +## Constant pools, caches and dependency inputs + +### Make the working context explicit + +[ConstantPool.withPool](src/main/java/org/xvm/asm/ConstantPool.java) scopes the working pool and +restores its predecessor on close. A constant's owning pool is not automatically the working pool: +cross-pool resolution can deliberately register or resolve into another pool. Inspection after +compilation therefore establishes the appropriate scope explicitly. Scopes are opened and closed +on the same thread; asynchronous work must establish its own context. + +The adapter serializes access to compiler state and shared repositories. Its detached models do +not contain compiler objects; snapshot-local symbol/type identities are separate from live +registers and constants. Some worker-only projections deliberately build TypeInfo using the +attempt's listener, while ordinary snapshot extraction avoids resuming validation. Those two +operations have different cost and reporting contracts. + +`EmbeddingSupport.footprint(compilation)` inspects the supplied attempt's pool without starting an +interpreter. `ensureRuntimePool()` explicitly initializes runtime state when needed; the deprecated +`getConstantPool()` retains that runtime-initializing behavior. Neither should be confused with +the pool belonging to an unsuccessful compilation. The footprint's heap count is JVM-wide used +heap, not a measurement of memory owned exclusively by that compilation. + +The supporting [TransientThreadLocal](../javatools_utils/src/main/java/org/xvm/util/TransientThreadLocal.java) +correction treats null as absence instead of storing a null-valued entry that still retains its +key in the per-thread map. Setting or computing null removes the entry. This matters for repeated +compiler use, including deferred TypeInfo state, but is a utility lifecycle fix rather than an +editor feature or a new claim of compiler thread safety. + +### Preserve tolerant repositories and validate explicit dependency inputs + +[FileRepository](src/main/java/org/xvm/asm/FileRepository.java) and +[DirRepository](src/main/java/org/xvm/asm/DirRepository.java) retain their best-effort behavior: +an unreadable candidate is unavailable, and another candidate or repository may still satisfy +the request. A damaged unrelated file does not prove why a requested module is absent. + +The corrections keep unchanged failed reads in the existing metadata-based cache policy. A +changed file becomes eligible for retry; after a prior payload failure, a valid header alone is +not enough to establish a successful replacement. Directory cache rebuilding also avoids marking +an interrupted rebuild as current. [LinkedRepository](src/main/java/org/xvm/asm/LinkedRepository.java) +handles a repository with no available version tree instead of dereferencing null. + +**This does not add a repository error callback or progress API.** For explicit dependency inputs, +the LSP validates owned bytes through `FileStructure` before accepting them into +[XdkDependency](../lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkDependency.kt). It can then +retain immutable bytes and reopen compiler-owned structures for an attempt. This keeps useful +dependency diagnostics separate from tolerant repository scans and avoids sharing a mutable +`FileStructure` across requests. Compiler queue/progress presentation belongs to the adapter. + +## Why ordinary AST and runtime files also changed + +The number of touched AST files is larger than the number of new concepts. Several contracts must +be carried through existing compiler operations rather than implemented only at an entry point: + +| Change group | Why existing files participate | +| --- | --- | +| Listener migration across expressions, constants, structures and tools. | Every reporting/probing call must select an owner or an explicit silence and preserve the appropriate abort policy. Most such edits do not change an expression's language semantics. | +| Collector propagation through stages, root contexts, method bodies and nested construction. | The same attempt must see successful bindings established inside deferred or nested validation. An outer wrapper cannot reconstruct these decisions afterward. | +| Name/source accessors and capture provenance. | The relevant written token or resolved register is owned by the node that parsed or validated it. Exposing that fact avoids re-resolving the name in the adapter. | +| Copy and child-traversal overrides. | Each node must honor syntax-child ownership; specialized state cannot be made safe by changing only the base class. | +| Partial-syntax hooks in expressions and declarations. | The parser and enclosing node must retain the unfinished site in its real context while preventing normal emission of the unfinished operation. | +| Tool/runtime embedding call sites. | `Launcher`, tool drivers, `InterpreterControl`, `EvalCompiler`, `xRTCompiler` and `xRTType` must use the revised reporting/API contracts too. They do not gain an LSP dependency. | + +Compiler correctness fixes discovered while exercising the new consumers apply to ordinary +compilation as well: + +- Bound callable references must retain their callable type, including generic method references. + Function fitting must not return a usable type after an argument-count or argument-type failure + merely because the required return types match. The + [function-typing regressions](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerFunctionTypingTest.kt) + check the fitting contract, rejected calls, and successful execution after serialization. +- A constant Boolean result does not imply that evaluating its operands has no effects. + `probe() && False` must still evaluate `probe()` once, as must `probe() || True`. The + [conditional-effects regression](../manualTests/src/main/x/conditionalEffects.x) checks assignment, + conditional, negated and argument contexts as well as the corresponding nonconstant cases. + +Emitted programs and tooling probes must use one consistent set of rules. These are not alternate +semantics enabled by an editor flag; some supporting corrections are already present independently +of the remaining tooling changes. + +The source AST discussed here is distinct from the removed binary AST implementation. Tooling +does not restore that implementation or serialize its binding/cursor records into `.xtc` files. +The separate binary-format change requires older affected `.xtc` artifacts to be rebuilt; it +should not be mistaken for an LSP wire-format requirement. + +## Compatibility and limits + +- **Ordinary Ecstasy compilation uses the same pipeline and validation rules.** Optional binding + collectors default to `NONE` in ordinary compiler construction. Cursor analysis is explicitly + requested; recovered syntax does not authorize emission of an invalid program. +- **The Java embedding API is intentionally not universally source/binary compatible.** The + planned breaking release boundary covers changed listener signatures, removed `BLACKHOLE` and + `SafeLookAhead` APIs, required listeners and changed fallback runtime-listener behavior. Existing + Java consumers must migrate and recompile; see the [migration guide](#6-migrate-java-callers-and-implementations). +- **Partial facts have narrower guarantees than complete results.** An available syntax tree, + resolved declaration graph, fitted candidate and emitted module answer different questions. + Unsupported source damage yields diagnostics or absent facts, not invented semantic certainty. +- **Compiler objects remain mutable and confined to their worker.** Immutable binding-map wrappers, + final listeners and `copyTree()` do not make a compilation safe for concurrent access. +- **Cancellation is cooperative.** Polling and phase checks stop obsolete work where supported; + the adapter must still check revisions before publication. +- **This is not a fully incremental compiler API.** The adapter captures/reuses input and detached + result data, schedules work and avoids unnecessary requests. Fresh attempts still run compiler + phases for their module snapshot rather than incrementally mutating an AST shared with the IDE. +- **Editor policy stays outside the compiler.** LSP message handling, themes, text edits, URI/version + management, client workarounds and UI progress remain in `lang`. Syntax-only fallback behavior + and compiler-derived semantic answers remain distinct sources of information. + +### Java result records and nullable progress + +Retained convenience constructors do not preserve a record's old deconstruction shape. +`CursorBinding` has fourteen components; Java clients using record patterns must adapt to the +current shape even where an older constructor descriptor remains available. Prefer accessors +when the consumer does not need deconstruction. + +`PartialAnalysis` accepts and returns a nullable `ConstantPool`, like `Compilation`: null means +semantic analysis did not start. Its retained three- through six-argument constructors take that +pool directly, not an `Optional` parameter. Consumers of an earlier development API must recompile, +pass the pool or null and replace `pool().orElse(...)` with a null check. Optional return values +elsewhere remain appropriate where their API declares them. + +Collection snapshots retain compiler-owned values. Constructor compatibility and immutable +collection wrappers do not extend those values' lifetime or permit concurrent AST/pool access. + +## Tests and reproduction + +The useful evidence is at three levels: contracts tested directly in `javatools`, Java embedding +consumers exercised with compiled XDK modules, and packaged-server/editor acceptance tests. A +passing parser unit test alone does not demonstrate that the distributed LSP preserves a fact +through its transport and editor integration. + +| Contract | Representative tests | +| --- | --- | +| Callback delivery, budgets, branches, suppression and cancellation. | [ErrorListenerHostRegressionTest](src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java), [ErrorListenerBranchTest](src/test/java/org/xvm/asm/ErrorListenerBranchTest.java), [ErrorListenerSilenceTest](src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java), [ErrorListenerCancellationTest](src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java), [CompilerCancellationTest](src/test/java/org/xvm/compiler/CompilerCancellationTest.java). | +| Reporting restoration and parser speculation. | [ReportingTest](src/test/java/org/xvm/asm/ReportingTest.java), [ParserAttemptTest](src/test/java/org/xvm/compiler/ParserAttemptTest.java), [NameResolverReportingTest](src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java), [ValidationScopeTest](src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java). | +| Tree copying, traversal and source provenance. | [AstTreeCopyTest](src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java), [OrdinaryAstTreeCopyTest](src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java), [AstChildTraversalTest](src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java), [DeclarationProvenanceTest](src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java), [RegisteredChildFieldsTest](src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java). | +| Snapshot inputs, outcomes and cached diagnostics through the embedding boundary. | [EmbeddingSourceSnapshotTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingSourceSnapshotTest.kt), [EmbeddingDiagnosticsTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/EmbeddingDiagnosticsTest.kt), [TypeInfoDiagnosticsTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoDiagnosticsTest.kt), [TypeInfoFinalCompositionTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/TypeInfoFinalCompositionTest.kt). | +| Selected calls, functions, constructors and folded initializers. | [CompilerBindingFactsTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerBindingFactsTest.kt), [BindingIdentitySnapshotTest](src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java). | +| Declaration analysis, cursor candidates and recovery. | [CompilerDeclarationAnalysisTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerDeclarationAnalysisTest.kt), [CompilerPartialFactsTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerPartialFactsTest.kt), [ParserRecoveryTest](src/test/java/org/xvm/compiler/ParserRecoveryTest.java), [XdkPartialAnalysisTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkPartialAnalysisTest.kt), [XdkDeclarationHeaderTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkDeclarationHeaderTest.kt). | +| Tolerant lookup, failed-read caching and repaired inputs. | [DirRepositoryFailureTest](src/test/java/org/xvm/asm/DirRepositoryFailureTest.java), [FileRepositoryFailureTest](src/test/java/org/xvm/asm/FileRepositoryFailureTest.java). | +| Packaged compiler server and editor behavior. | [XdkStdioTest](../lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkStdioTest.kt) and the [manual/automated editor playbook](../lang/doc/manual-test-plan.md). | + +### Historical listener regression evidence + +The strongest evidence is the pair of +[host regression tests](src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java). +Their source uses API shapes available on both sides of the reporting change. On the previous +implementation at `1d9352b6f`, both compile and fail for the expected behavioral reason. On the +repaired implementation, the same source passes both cases: + +* `hostBranchLetsLexerReportBothErrors` sends two bad string escapes through the real lexer. + Previously it threw after the first error because the default branch had a one-error budget. + It now completes the scan and publishes both reports when the branch is merged. +* `parentAbortReachesTheLexerThroughAnExistingBranch` aborts the parent after creating its branch. + Previously the lexer continued because the branch considered only its local state. It now + honors the parent at its reporting checkpoint. + +[ErrorListenerMigrationTest](src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java) also proves +that derived silence preserves a host stop request without publishing an error. Its parser/lexer +budget tests and source-reanchoring test guard existing behavior. The branch, site, silence and +abort suites cover collection, forwarding, replay and suppression. +[ErrorListenerBoundaryTest](src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java) checks required +listeners and destination retention; these assertions enforce the chosen API contract rather than +claiming that every API change reproduces an old bug. + +The 2026-10-09 focused run on this implementation passed **60 tests with zero failures, errors +or skips**. Earlier full-suite verification of the same production listener migration passed +418 executed Java tests, with 42 skips recorded separately. A forced XDK rebuild produced +24 modules matching the master baseline after normalizing creation timestamps. The separate +integration checkout combining the reporting changes with the embedding foundations also passed +the real compiler-consumer test with no skips. These checks cover different scopes; the 60 focused +tests are not presented as a substitute for compilation and integration validation. + +Run the focused tests from the repository root: + +```bash +./gradlew :javatools:test \ + --tests 'org.xvm.asm.ErrorListener*Test' \ + --tests org.xvm.api.ErrorListenerBoundaryTest \ + --tests org.xvm.tool.LauncherErrorHandlingTest \ + --rerun-tasks --no-build-cache +``` + +These focused cases do not require installed XDK modules. The numerical receipts above +describe the listener migration at that time, not a fresh run of the complete implementation. +The broader commands below cover compiled-module consumers and packaged-server integration; +neither historical counts nor a green task substitute for checking current failures and skips. + +### Run the compiler and embedding consumers + +Run commands from the repository root. Build the XDK outputs first when exercising tests that +require installed modules: + +```bash +./gradlew :xdk:installDist +./gradlew :javatools:test --rerun-tasks --no-build-cache +``` + +A focused embedding-consumer run exercises the new API boundaries without launching an IDE: + +```bash +./gradlew :lang:lsp-server:test \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + --tests 'org.xvm.lsp.adapter.Embedding*Test' \ + --tests org.xvm.lsp.adapter.TypeInfoDiagnosticsTest \ + --tests org.xvm.lsp.adapter.TypeInfoFinalCompositionTest \ + --tests org.xvm.lsp.adapter.CompilerBindingFactsTest \ + --tests org.xvm.lsp.adapter.CompilerDeclarationAnalysisTest \ + --tests org.xvm.lsp.adapter.CompilerPartialFactsTest \ + --rerun-tasks --no-build-cache +``` + +Exercise the packaged artifact separately: + +```bash +./gradlew :lang:lsp-server:compilerStdioTest \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + --rerun-tasks --no-build-cache +``` + +Check the JUnit XML under `javatools/build/test-results/test` and +`lang/lsp-server/build/test-results/{test,compilerStdioTest}` for failures **and skips**. Some Java +tests conditionally skip when compiled modules are absent; a green task alone does not establish +that they ran. The test links and commands here describe coverage and reproduction, not a claim +that every suite has just been rerun. Current CI results and their artifacts provide that receipt. + +## Appendix: file-by-file compiler changes + +This inventory records the `javatools` portion of the full implementation review when this guide +was added: **147 files (97 modified and 50 added)**, including this document. It +describes what changed in each file and why, rather than restating everything that file does. +Supporting changes already in the review's base and changes outside `javatools` are discussed +in the main guide but are not counted again here. + +The files are grouped by responsibility. **Modified** means an existing file changed; **Added** +means a new file in this review. A small listener migration is not a new language feature, and +a test file is not an additional production mechanism. Every file link opens the corresponding +source or document in this checkout. + +### Documentation and embedding entry points + +| File | Change | What changed and why | +| --- | --- | --- | +| [README.md](README.md) | Modified | Keeps a short architecture overview and links to this guide for the consolidated diagnostic design, listener migration and compiler contracts. | +| [LSP-COMPILER.md](LSP-COMPILER.md) | Added | Explains the compiler-to-LSP data flow, API guarantees, partial-analysis limits, compatibility implications and test coverage; this appendix maps those mechanisms to their files. | +| [src/main/java/org/xvm/api/EmbeddingSupport.java](src/main/java/org/xvm/api/EmbeddingSupport.java) | Modified | Returns compilation outcomes, retained source trees and identity-based semantic facts; adds declaration/cursor analysis over source snapshots, explicit bootstrap configuration and footprint inspection without starting the runtime. | +| [src/main/java/org/xvm/api/ModuleCompiler.java](src/main/java/org/xvm/api/ModuleCompiler.java) | Modified | Requires an explicit listener and reports compilation, input and output failures through it. Retains the simpler module/boolean result APIs and documents repository sharing constraints. | +| [src/main/java/org/xvm/api/InterpreterControl.java](src/main/java/org/xvm/api/InterpreterControl.java) | Modified | Routes an unhandled interpreted exception through the guaranteed listener with the module as its diagnostic site, removing the nullable-listener discard path. | + +### Diagnostics, structures and repositories + +| File | Change | What changed and why | +| --- | --- | --- | +| [Annotation.java](src/main/java/org/xvm/asm/Annotation.java) | Modified | Separates diagnostic emission from the abort query when rejecting a non-annotation type; logging no longer returns the stop decision. | +| [ClassStructure.java](src/main/java/org/xvm/asm/ClassStructure.java) | Modified | Uses an explicitly silent probe when looking up a Service member, replacing the global blackhole sentinel. | +| [Component.java](src/main/java/org/xvm/asm/Component.java) | Modified | Requires a destination for resolution collectors and reports cyclical contributions through the typed fatal/site API. | +| [ComponentResolver.java](src/main/java/org/xvm/asm/ComponentResolver.java) | Modified | Removes the default discard listener from the resolution-collector interface. Implementations must choose where resolution diagnostics go. | +| [ConstantPool.java](src/main/java/org/xvm/asm/ConstantPool.java) | Modified | Exposes implicit-import names without loading or resolving their components, allowing cursor scope queries to enumerate names using the language's own import table. | +| [DirRepository.java](src/main/java/org/xvm/asm/DirRepository.java) | Modified | Preserves tolerant lookup while caching unchanged failed payload reads, retrying changed candidates and invalidating a directory scan before rebuilding its cache. Adds no reporting callback API. | +| [ErrorList.java](src/main/java/org/xvm/asm/ErrorList.java) | Modified | Makes retained diagnostics, deduplication, shared default budgets, parent-aware branches and replay explicit. Branch delivery and abort policy remain distinct operations. | +| [ErrorListener.java](src/main/java/org/xvm/asm/ErrorListener.java) | Modified | Defines diagnostic sites, severity helpers, collection/forwarding/cancellation wrappers, branches and named silence reasons. Separates reporting from abort queries and replaces the blackhole/null conventions. | +| [FileRepository.java](src/main/java/org/xvm/asm/FileRepository.java) | Modified | Caches failed reads under the existing metadata policy, retries changed files and requires a full payload read after a prior failure. Lookup stays best-effort; write failures still propagate. | +| [FileStructure.java](src/main/java/org/xvm/asm/FileStructure.java) | Modified | Removes storage and inheritance of an ambient error listener, so a reused module structure cannot carry one host's diagnostic destination into another operation. | +| [LinkedRepository.java](src/main/java/org/xvm/asm/LinkedRepository.java) | Modified | Handles an underlying repository with no available version tree instead of dereferencing null during combined version lookup. | +| [MethodStructure.java](src/main/java/org/xvm/asm/MethodStructure.java) | Modified | Uses a shared probe listener for speculative common-type inference and the explicit diagnostic-site API for unsupported dynamic type parameters. | +| [Reporting.java](src/main/java/org/xvm/asm/Reporting.java) | Added | Adds a scoped diagnostic destination for callbacks that cannot take a listener parameter. Closing a scope restores the previous destination, including after nested calls or exceptions. | +| [XvmStructure.java](src/main/java/org/xvm/asm/XvmStructure.java) | Modified | Makes structural reporting use the listener supplied by the caller; removes ambient lookup and the boolean return from the logging helper. | + +### Type information and inherited method metadata + +| File | Change | What changed and why | +| --- | --- | --- | +| [ImmutableTypeConstant.java](src/main/java/org/xvm/asm/constants/ImmutableTypeConstant.java) | Modified | Reports redundant immutability and queries abort separately, preserving the validation decision under the revised listener contract. | +| [MethodBody.java](src/main/java/org/xvm/asm/constants/MethodBody.java) | Modified | Exposes the existing FromInto mixin constraint metadata for inspection without generating a forwarding implementation. | +| [MethodInfo.java](src/main/java/org/xvm/asm/constants/MethodInfo.java) | Modified | Adds a source-method query for super dispatch using the compiler's existing chain selection; synthetic, native, field and other non-source targets remain absent. | +| [PropertyInfo.java](src/main/java/org/xvm/asm/constants/PropertyInfo.java) | Modified | Anchors ignored duplicate/super annotation warnings to the contributing property's identity, so the reported structure matches the annotation being ignored. | +| [TypeCollector.java](src/main/java/org/xvm/asm/constants/TypeCollector.java) | Modified | Replaces blackhole use in speculative common-type inference with explicit probes, sharing one within a local sequence where appropriate. | +| [TypeConstant.java](src/main/java/org/xvm/asm/constants/TypeConstant.java) | Modified | Uses explicit reporting for TypeInfo construction, retains and replays cached diagnostics to later callers, and distinguishes speculative/cascade suppression from real validation. Cache invalidation also clears retained diagnostic state. | +| [TypeInfoReal.java](src/main/java/org/xvm/asm/constants/TypeInfoReal.java) | Modified | Marks the parent-newability check as a silent probe while retaining the caller's listener for the surrounding TypeInfo computation. | +| [UnresolvedTypeConstant.java](src/main/java/org/xvm/asm/constants/UnresolvedTypeConstant.java) | Modified | Reports an unresolved type through the error/site API, retaining the constant as the diagnostic location. | + +### Compiler phases, parsing and exported facts + +| File | Change | What changed and why | +| --- | --- | --- | +| [Compiler.java](src/main/java/org/xvm/compiler/Compiler.java) | Modified | Owns a final listener and optional invocation/cursor collectors and passes them through compiler stages. Registration recognizes host cancellation without inventing an error or retaining a listener on the file structure. | +| [CursorBinding.java](src/main/java/org/xvm/compiler/CursorBinding.java) | Added | Defines immutable collections of scope, type, candidate-call and argument-proposal facts for an explicit cursor query. Its collector publishes inspected source sites without presenting candidates as selected executable calls. | +| [EvalCompiler.java](src/main/java/org/xvm/compiler/EvalCompiler.java) | Modified | Creates its diagnostic buffer at construction so hosts can inspect errors before lambda compilation, with an explicit first-error policy. | +| [InitializerBinding.java](src/main/java/org/xvm/compiler/InitializerBinding.java) | Added | Captures source references, types and call facts before successful constant folding discards an initializer's temporary method; excludes validation contexts and disposable AST identities from the published provenance. | +| [InvocationBinding.java](src/main/java/org/xvm/compiler/InvocationBinding.java) | Added | Records selected methods, function types, constructors and source-to-parameter argument associations. Attempt-owned collectors discard stale/trial facts and publish maps keyed by source-node identity. | +| [Lexer.java](src/main/java/org/xvm/compiler/Lexer.java) | Modified | Uses explicit sites/listeners and checks cancellation even while reading valid tokens. Unterminated string handling stops at EOF instead of repeatedly reporting when no error budget stops it. | +| [Parser.java](src/main/java/org/xvm/compiler/Parser.java) | Modified | Replaces implicit lookahead listener swapping with scoped attempts; adds bounded syntax recovery and opt-in cursor/header parsing while preserving written positions and later declarations. Unrelated parse errors still block semantic compilation. | +| [Token.java](src/main/java/org/xvm/compiler/Token.java) | Modified | Migrates token diagnostics to explicit source-span or no-location sites with trailing message arguments; the helper no longer returns an abort decision. | + +### AST ownership, contexts and semantic provenance + +| File | Change | What changed and why | +| --- | --- | --- | +| [AnonInnerClass.java](src/main/java/org/xvm/compiler/ast/AnonInnerClass.java) | Modified | Enforces the non-null listener contract at construction even when assertions are disabled; this particular edit does not add a new anonymous-class algorithm. | +| [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java) | Added | Associates anonymous-class captures and generated properties with their enclosing source registers, so navigation and rename can follow the original declaration. | +| [AstNode.java](src/main/java/org/xvm/compiler/ast/AstNode.java) | Modified | Adds repeatable child traversal and the documented copyTree contract, preserves the legacy clone entry point, supports inherited registered child fields and carries fact collectors into child staging. Reporting requires an explicit destination. | +| [CompositionNode.java](src/main/java/org/xvm/compiler/ast/CompositionNode.java) | Modified | Makes source lookup safe for unparented parsed compositions and implicit compositions with no type child, supporting inspection before normal tree adoption. | +| [Context.java](src/main/java/org/xvm/compiler/ast/Context.java) | Modified | Propagates the attempt's call/cursor collectors through lexical contexts and makes diagnostic/probe ownership explicit during name and this resolution. | +| [Expression.java](src/main/java/org/xvm/compiler/ast/Expression.java) | Modified | Adds the covariant copyTree result and uses it for exhaustive fit trials. Makes type-fitting probes explicitly silent instead of depending on whether a caller happened to pass null or a reporting listener. | +| [ForEachStatement.java](src/main/java/org/xvm/compiler/ast/ForEachStatement.java) | Modified | Holds the context/listener pair for lazy loop variables in ValidationScope and restores it on every validation exit; speculative fitting uses explicit probes. | +| [ForStatement.java](src/main/java/org/xvm/compiler/ast/ForStatement.java) | Modified | Replaces separate nullable callback fields with ValidationScope and restores the previous scope after normal, early and exceptional validation exits. | +| [ImportStatement.java](src/main/java/org/xvm/compiler/ast/ImportStatement.java) | Modified | Exposes the written alias, qualified-name tokens and already-resolved import identity without restarting name resolution, giving tooling exact import provenance. | +| [InvocationExpression.java](src/main/java/org/xvm/compiler/ast/InvocationExpression.java) | Modified | Publishes validated method/function/super call facts and written argument mappings, exposes the invoked expression/selected method and uses explicit probes. Keeps failed argument fitting from being overwritten by successful return-type matching. | +| [LambdaBindings.java](src/main/java/org/xvm/compiler/ast/LambdaBindings.java) | Added | Maps written lambda parameters and generated capture parameters back to their source registers, distinguishing source declarations from synthetic compiler parameters. | +| [LambdaExpression.java](src/main/java/org/xvm/compiler/ast/LambdaExpression.java) | Modified | Exposes the arrow token and retained source bindings, carries collectors into generated-method validation and supplies a typed tree copy; diagnostic probes and sites use the explicit listener API. | +| [MethodDeclarationStatement.java](src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java) | Modified | Exposes declaration names and documentation, retains formal-parameter identities and carries collectors through annotation/default/body validation and generated method compilation. | +| [NameExpression.java](src/main/java/org/xvm/compiler/ast/NameExpression.java) | Modified | Exposes the resolved constant or register and supplies a typed tree copy. Also preserves the callable type and hidden generic bindings of bound method references; speculative resolution selects an explicit probe. | +| [NameResolver.java](src/main/java/org/xvm/compiler/ast/NameResolver.java) | Modified | Scopes the caller's listener across collector callbacks and releases it on all exits. Retains successfully resolved qualified-name segments without restarting lookup when tooling inspects them. | +| [NamedTypeExpression.java](src/main/java/org/xvm/compiler/ast/NamedTypeExpression.java) | Modified | Exposes tokens and resolved identities for individual qualified-name segments. Migrates specialized copying, including the non-child dynamic expression, to copyTree and uses an explicit default-type probe. | +| [NewExpression.java](src/main/java/org/xvm/compiler/ast/NewExpression.java) | Modified | Shares construction preparation with partial queries, records selected constructors and capture origins, exposes written arguments/deferred bodies and propagates collectors. Cursor-containing bodies avoid publishing disposable capture-analysis clones or emitting an unfinished construction. | +| [Parameter.java](src/main/java/org/xvm/compiler/ast/Parameter.java) | Modified | Retains a parameter's resolved register, constructor-property identity or formal type constant for source consumers, with a typed copy that keeps the binding slot independent when a copy is rebound. | +| [PartialAst.java](src/main/java/org/xvm/compiler/ast/PartialAst.java) | Added | Provides one documented internal bridge from ast.partial to ordinary parenting, validation, scope, call-fitting and construction operations. Keeps those operations out of the embedding API and avoids widening existing AST members individually. | +| [PropertyDeclarationStatement.java](src/main/java/org/xvm/compiler/ast/PropertyDeclarationStatement.java) | Modified | Exposes the property name token, records initializer provenance before folding and carries collectors into real initializer methods. A cursor initializer is inspected through source-owned nodes rather than disposable folding clones. | +| [StageMgr.java](src/main/java/org/xvm/compiler/ast/StageMgr.java) | Modified | Retains a final non-null listener and the attempt's call/cursor collectors across stage processing. Existing constructors select disabled collectors for ordinary compilation. | +| [Statement.java](src/main/java/org/xvm/compiler/ast/Statement.java) | Modified | Adds the covariant copyTree result and restores the validation context in finally, including when partial validation supplies an explicit value-context callback. | +| [StatementBlock.java](src/main/java/org/xvm/compiler/ast/StatementBlock.java) | Modified | Threads collectors through method/root contexts, records parameter and capture associations, and adds a typed tree copy. TypeInfo queries during contextual lookup use the current listener. | +| [TryStatement.java](src/main/java/org/xvm/compiler/ast/TryStatement.java) | Modified | Keeps the finally-validation context and listener together and restores them after nested validation or failure, avoiding stale callback state on a retained tree. | +| [TypeCompositionStatement.java](src/main/java/org/xvm/compiler/ast/TypeCompositionStatement.java) | Modified | Exposes declaration/formal/constructor-parameter provenance and supports syntax-only incomplete subclasses. Rejects conflicting unconditional type declarations before an invalid composite cast, respects deferred children and propagates collectors into constructors. | +| [TypeExpression.java](src/main/java/org/xvm/compiler/ast/TypeExpression.java) | Modified | Adds a covariant copyTree result and replaces null-listener type queries with explicit probes while keeping reporting queries tied to their supplied listener. | +| [TypedefStatement.java](src/main/java/org/xvm/compiler/ast/TypedefStatement.java) | Modified | Exposes the written alias token, allowing navigation and rename to use its exact declaration range. | +| [ValidationScope.java](src/main/java/org/xvm/compiler/ast/ValidationScope.java) | Added | Represents a live context/listener pair for lazy validation callbacks. Null at the owning statement means no active scope; an active scope cannot contain a missing context or listener. | +| [VariableDeclarationStatement.java](src/main/java/org/xvm/compiler/ast/VariableDeclarationStatement.java) | Modified | Exposes the existing declaration register and documents matching narrowed uses through original-register identity, rather than matching local names by spelling. | +| [WhileStatement.java](src/main/java/org/xvm/compiler/ast/WhileStatement.java) | Modified | Uses ValidationScope for lazy loop variables and restores the previous scope after validation, including exceptions, rather than leaving context/listener fields attached to the statement. | + +### Expression call-site migration + +| File | Change | What changed and why | +| --- | --- | --- | +| [AnnotationExpression.java](src/main/java/org/xvm/compiler/ast/AnnotationExpression.java) | Modified | Uses a named probe when resolving an annotation argument as a method reference; real annotation validation keeps its reporting listener. | +| [ArrayAccessExpression.java](src/main/java/org/xvm/compiler/ast/ArrayAccessExpression.java) | Modified | Replaces null/blackhole listeners in index, slice, tuple, matrix and operator-fit queries with explicit probes, sharing a local probe across related candidate checks. | +| [AsExpression.java](src/main/java/org/xvm/compiler/ast/AsExpression.java) | Modified | Uses explicit probes for implicit cast-type lookup and operand-fit checks. | +| [AssignmentStatement.java](src/main/java/org/xvm/compiler/ast/AssignmentStatement.java) | Modified | Uses one local probe for speculative right-hand-side, tuple-unpacking and future-value fitting before the chosen assignment is validated. | +| [CaseManager.java](src/main/java/org/xvm/compiler/ast/CaseManager.java) | Modified | Uses a shared probe for case-value versus range fitting, leaving selected case validation responsible for diagnostics. | +| [CmpChainExpression.java](src/main/java/org/xvm/compiler/ast/CmpChainExpression.java) | Modified | Supplies an explicit probe when testing operands against a common comparison type. | +| [CmpExpression.java](src/main/java/org/xvm/compiler/ast/CmpExpression.java) | Modified | Uses an explicit probe when selecting a common operand type. | +| [ElseExpression.java](src/main/java/org/xvm/compiler/ast/ElseExpression.java) | Modified | Marks the alternative expression's multi-value fitting as a probe instead of passing a null listener. | +| [ElvisExpression.java](src/main/java/org/xvm/compiler/ast/ElvisExpression.java) | Modified | Uses explicit probes for alternative/conditional fitting and always branches the supplied listener where diagnostics must be retained for a chosen interpretation. | +| [ListExpression.java](src/main/java/org/xvm/compiler/ast/ListExpression.java) | Modified | Uses a probe for implicit lookup of the list's explicitly written type. | +| [MapExpression.java](src/main/java/org/xvm/compiler/ast/MapExpression.java) | Modified | Uses a probe for implicit lookup of the map's explicitly written type. | +| [NonBindingExpression.java](src/main/java/org/xvm/compiler/ast/NonBindingExpression.java) | Modified | Uses a probe to inspect the type of an unbound argument placeholder. | +| [NotNullExpression.java](src/main/java/org/xvm/compiler/ast/NotNullExpression.java) | Modified | Uses probes for type/value interpretations and conditional multi-value fitting, keeping speculative failures out of ordinary diagnostics. | +| [ParenthesizedExpression.java](src/main/java/org/xvm/compiler/ast/ParenthesizedExpression.java) | Modified | Shares an explicit probe between tuple and single-value interpretations before selecting the validation path. | +| [RelOpExpression.java](src/main/java/org/xvm/compiler/ast/RelOpExpression.java) | Modified | Uses explicit probes for operator and operand fitting and calls the revised type-fitting helper, whose silence policy is internal to that helper. | +| [ReturnStatement.java](src/main/java/org/xvm/compiler/ast/ReturnStatement.java) | Modified | Uses one probe for competing multi-value, conditional, future and tuple interpretations before validating the chosen return form. | +| [SequentialAssignExpression.java](src/main/java/org/xvm/compiler/ast/SequentialAssignExpression.java) | Modified | Adds the probe static imports alongside the expression migration. There is no executable behavior change in this file's diff. | +| [StatementExpression.java](src/main/java/org/xvm/compiler/ast/StatementExpression.java) | Modified | Uses probes when inspecting temporary statement bodies and an explicit source site when reporting a missing return. | +| [TemplateExpression.java](src/main/java/org/xvm/compiler/ast/TemplateExpression.java) | Modified | Shares a probe while choosing String versus Object handling for interpolated values. | +| [TernaryExpression.java](src/main/java/org/xvm/compiler/ast/TernaryExpression.java) | Modified | Uses local probes for the competing arm types and conditional False forms, replacing nullable listeners without changing which chosen arms are subsequently validated. | +| [TraceExpression.java](src/main/java/org/xvm/compiler/ast/TraceExpression.java) | Modified | Explicitly suppresses reports when adopting the already-validated wrapped expression's types and constants. | +| [TupleExpression.java](src/main/java/org/xvm/compiler/ast/TupleExpression.java) | Modified | Uses an explicit probe to obtain a written tuple type during implicit-type inspection. | +| [UnaryComplementExpression.java](src/main/java/org/xvm/compiler/ast/UnaryComplementExpression.java) | Modified | Marks required-type fitting as a probe before selecting the complement operator. | +| [UnaryMinusExpression.java](src/main/java/org/xvm/compiler/ast/UnaryMinusExpression.java) | Modified | Marks required-type fitting as a probe before selecting the negation operator. | + +### Partial syntax and compiler-backed cursor queries + +| File | Change | What changed and why | +| --- | --- | --- | +| [CursorScope.java](src/main/java/org/xvm/compiler/ast/partial/CursorScope.java) | Added | Captures visible variables, enclosing-instance information and type-name candidates through ordinary contextual lookup, retaining real accessibility and narrowing rules. | +| [IncompleteDeclarationStatement.java](src/main/java/org/xvm/compiler/ast/partial/IncompleteDeclarationStatement.java) | Added | Retains an unfinished declaration header and selected type prefix as syntax without registering an invented method/property component, parameter list or body context. | +| [IncompleteExpression.java](src/main/java/org/xvm/compiler/ast/partial/IncompleteExpression.java) | Added | Keeps an incomplete operation inside its real assignment, return or other value context. It inspects the intact prefix but never supplies a valid value or permits emission. | +| [IncompleteLocalDeclaration.java](src/main/java/org/xvm/compiler/ast/partial/IncompleteLocalDeclaration.java) | Added | Retains the written type/initializer when a local name is missing, without inventing a local register or inferred declaration identity. | +| [IncompleteStatement.java](src/main/java/org/xvm/compiler/ast/partial/IncompleteStatement.java) | Added | Owns the selected bare name, member or call prefix, including arguments and cursor metadata. Validation publishes prefix facts and then fails deliberately so the unfinished operation cannot emit code. | +| [IncompleteTypeCompositionStatement.java](src/main/java/org/xvm/compiler/ast/partial/IncompleteTypeCompositionStatement.java) | Added | Retains unfinished type headers and bodies without contributing inheritance/member facts. A root module can register its written namespace/core import solely to establish the query scope. | +| [PartialArgument.java](src/main/java/org/xvm/compiler/ast/partial/PartialArgument.java) | Added | Builds disposable argument trials with the correct lexical parentage, preserving labels and other written arguments without replacing retained source syntax. | +| [PartialCallResolver.java](src/main/java/org/xvm/compiler/ast/partial/PartialCallResolver.java) | Added | Uses normal lookup and argument fitting to inspect possible method/function calls at the cursor. Candidate signatures remain possibilities rather than a selected executable call. | +| [PartialConstructionResolver.java](src/main/java/org/xvm/compiler/ast/partial/PartialConstructionResolver.java) | Added | Reuses ordinary construction preparation and fitting for incomplete constructors, stopping before synthesis and emission instead of maintaining a second constructor type checker. | +| [PartialQueries.java](src/main/java/org/xvm/compiler/ast/partial/PartialQueries.java) | Added | Coordinates semantic inspection for incomplete nodes through PartialAst, collecting facts while their real compiler context is live and keeping compiler internals out of the host API. | +| [PartialSyntax.java](src/main/java/org/xvm/compiler/ast/partial/PartialSyntax.java) | Added | Provides read-only containment, cursor and argument-slot queries, including deferred anonymous bodies. Respects boundaries where a nested call or lambda owns a different argument context. | +| [ProposedLiteralToken.java](src/main/java/org/xvm/compiler/ast/partial/ProposedLiteralToken.java) | Added | Carries the spelling of a disposable zero-width literal proposal so normal literal conversion can inspect it; it is never installed in the source tree or published as a source binding. | + +### Tool drivers, runtime consumers and diagnostic text + +| File | Change | What changed and why | +| --- | --- | --- | +| [java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java](src/main/java/org/xvm/runtime/template/_native/lang/src/xRTCompiler.java) | Modified | Adapts the runtime compiler's diagnostics to explicit no-location sites and trailing varargs, preserving its existing result completion path. | +| [java/org/xvm/runtime/template/_native/reflect/xRTType.java](src/main/java/org/xvm/runtime/template/_native/reflect/xRTType.java) | Modified | Uses named probes for reflective newability queries instead of the removed blackhole sentinel; no editor dependency is introduced. | +| [java/org/xvm/tool/Bundler.java](src/main/java/org/xvm/tool/Bundler.java) | Modified | Aligns its constructor naming/documentation with the required listener passed to Launcher; bundling behavior is unchanged by this edit. | +| [java/org/xvm/tool/Compiler.java](src/main/java/org/xvm/tool/Compiler.java) | Modified | Extracts the ordinary declaration phases so embedding can stop before body validation/emission, while full compilation still runs them. Uses explicit diagnostics and abort queries without sentinel comparisons. | +| [java/org/xvm/tool/Disassembler.java](src/main/java/org/xvm/tool/Disassembler.java) | Modified | Aligns its constructor with Launcher's explicit listener contract; this is integration and documentation rather than a disassembly redesign. | +| [java/org/xvm/tool/Initializer.java](src/main/java/org/xvm/tool/Initializer.java) | Modified | Documents and forwards the explicit listener required by Launcher, retaining the initializer's existing work. | +| [java/org/xvm/tool/Launcher.java](src/main/java/org/xvm/tool/Launcher.java) | Modified | Requires a final diagnostic destination, chooses explicit discard for CLI-only dispatch and forwards a fatal diagnostic to the host before console handling can abort. | +| [java/org/xvm/tool/ModuleInfo.java](src/main/java/org/xvm/tool/ModuleInfo.java) | Modified | Adds snapshot-backed source identity, text and directory-entry hooks; accepts a parser strategy and exposes retained per-file syntax after loading failure. Node buffers are final and parsing observes host cancellation. | +| [java/org/xvm/tool/Runner.java](src/main/java/org/xvm/tool/Runner.java) | Modified | Aligns constructor naming/documentation with the explicit Launcher listener, retaining the execution path. | +| [java/org/xvm/tool/TestRunner.java](src/main/java/org/xvm/tool/TestRunner.java) | Modified | Forwards the listener through the revised Runner constructor and aligns its parameter naming; it adds no LSP-specific test-running behavior. | +| [resources/errors.properties](src/main/resources/errors.properties) | Modified | Adds the incomplete-expression diagnostic used by retained partial syntax, keeping the diagnostic code/message available through normal compiler reporting. | + +### Embedding and diagnostic contract tests + +| File | Change | What changed and why | +| --- | --- | --- | +| [api/BindingIdentitySnapshotTest.java](src/test/java/org/xvm/api/BindingIdentitySnapshotTest.java) | Added | Checks identity-based method/function/constructor maps at publication boundaries, detached immutable cursor maps and rejection of null entries. | +| [api/EmbeddingFootprintTest.java](src/test/java/org/xvm/api/EmbeddingFootprintTest.java) | Added | Checks that footprint inspection measures the supplied attempt and does not bootstrap the runtime. | +| [api/ErrorListenerBoundaryTest.java](src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java) | Added | Checks rejection of missing listeners before compilation/file access and verifies that collectors, stage managers and AST reporting retain an explicitly chosen destination. | +| [asm/DirRepositoryFailureTest.java](src/test/java/org/xvm/asm/DirRepositoryFailureTest.java) | Added | Exercises corrupt/incompatible candidates, fallback to usable modules, shared failed-read caching, changed-file repair and candidate removal. | +| [asm/ErrorListenerAbortTest.java](src/test/java/org/xvm/asm/ErrorListenerAbortTest.java) | Added | Checks the shared default budget, unlimited mode, fatal abort policy and the fallback listener's reporting behavior. | +| [asm/ErrorListenerBranchTest.java](src/test/java/org/xvm/asm/ErrorListenerBranchTest.java) | Added | Checks host and ErrorList branches, full merge delivery, harmless merge on an unbranched listener and the distinction between a bare callback and a stateful collector. | +| [asm/ErrorListenerCancellationTest.java](src/test/java/org/xvm/asm/ErrorListenerCancellationTest.java) | Added | Checks cancellation through pre-existing branches and merges without replacing the wrapped budget or affecting a separate attempt. | +| [asm/ErrorListenerHostRegressionTest.java](src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java) | Added | Exercises real lexer failures through a host branch, proving that two errors survive and that a later parent abort reaches an already-created branch. | +| [asm/ErrorListenerMigrationTest.java](src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java) | Added | Checks duplicate delivery versus buffering, parent budget/abort propagation, retained branch source sites and parser/lexer stopping after the configured budget is spent. | +| [asm/ErrorListenerSilenceTest.java](src/test/java/org/xvm/asm/ErrorListenerSilenceTest.java) | Added | Checks silence reasons, standalone versus parent-derived silence, idempotent suppression and observation of later parent abort requests. | +| [asm/ErrorListenerSiteTest.java](src/test/java/org/xvm/asm/ErrorListenerSiteTest.java) | Added | Checks trailing varargs, severity helpers, exhaustive diagnostic sites, tee delivery/abort behavior and replay of retained diagnostics. | +| [asm/FileRepositoryFailureTest.java](src/test/java/org/xvm/asm/FileRepositoryFailureTest.java) | Added | Exercises invalid headers, truncated payloads, unchanged failed-read caching, repair/removal and propagation of write failures. | +| [asm/FileStructureErrorListenerTest.java](src/test/java/org/xvm/asm/FileStructureErrorListenerTest.java) | Modified | Replaces ambient-listener expectations with explicit delivery and verifies that reusing a structure does not reuse the previous request's listener. | +| [asm/ReportingTest.java](src/test/java/org/xvm/asm/ReportingTest.java) | Added | Checks nested destination restoration after exceptions, restoration of an inactive state and rejection of a null active destination. | +| [asm/RepositoryFailureTestSupport.java](src/test/java/org/xvm/asm/RepositoryFailureTestSupport.java) | Added | Captures and restores process-wide console output under isolated test execution, allowing repository tests to inspect existing reports without adding a production reporting API. | + +### Parser, AST and partial-analysis tests + +| File | Change | What changed and why | +| --- | --- | --- | +| [compiler/CompilerCancellationTest.java](src/test/java/org/xvm/compiler/CompilerCancellationTest.java) | Added | Checks that cancelled structure registration stops without throwing or fabricating a source error. | +| [compiler/CursorBindingTest.java](src/test/java/org/xvm/compiler/CursorBindingTest.java) | Added | Checks grouped cursor-fact construction, immutable snapshots and independent updates that preserve unrelated call, argument and enclosing-instance facts. | +| [compiler/EvalCompilerTest.java](src/test/java/org/xvm/compiler/EvalCompilerTest.java) | Added | Checks that diagnostics can be queried before compilation starts. | +| [compiler/ParserAttemptTest.java](src/test/java/org/xvm/compiler/ParserAttemptTest.java) | Added | Checks kept/discarded and nested attempts, token rewind, recovery restoration, lexer reports, callback exceptions, caller abort and module-name scanning. | +| [compiler/ParserDeclarationNameTest.java](src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java) | Added | Checks unfinished declaration-name slots, retained written types/initializers, independent children and following declarations; rejects slots outside supported contexts and honors stopping policies. | +| [compiler/ParserRecoveryTest.java](src/test/java/org/xvm/compiler/ParserRecoveryTest.java) | Added | Exercises ordinary recovery and explicit cursor parsing across expressions, calls, delimiters, declaration/type headers and EOF. Checks positions, following declarations, ownership, speculation and cancellation/budgets. | +| [compiler/ParserTest.java](src/test/java/org/xvm/compiler/ParserTest.java) | Modified | Migrates the existing discarded-lookahead regression to Parser.Attempt and updates the explanation of speculative diagnostic suppression. | +| [compiler/ast/AstChildTraversalTest.java](src/test/java/org/xvm/compiler/ast/AstChildTraversalTest.java) | Added | Checks repeated/interleaved traversal, live views versus snapshots, exhaustion and preservation of the legacy mutable replacement/removal cursor. | +| [compiler/ast/AstTreeCopyTest.java](src/test/java/org/xvm/compiler/ast/AstTreeCopyTest.java) | Added | Checks recovery-node metadata, independent owned children, retained cursor tokens and immutable header lists when partial syntax is copied inside ordinary trees. | +| [compiler/ast/CompositionSourceTest.java](src/test/java/org/xvm/compiler/ast/CompositionSourceTest.java) | Added | Checks source lookup for a default composition before adoption, when no parent or explicit type supplies a source. | +| [compiler/ast/DeclarationProvenanceTest.java](src/test/java/org/xvm/compiler/ast/DeclarationProvenanceTest.java) | Added | Checks qualified-name prefixes across deferred/failed resolution and retries, resolver ownership after copying and independent rebinding of copied parameters. | +| [compiler/ast/NameResolverReportingTest.java](src/test/java/org/xvm/compiler/ast/NameResolverReportingTest.java) | Added | Checks that direct resolution and callbacks use the same caller, release the listener afterward and restore outer reporting through nested, deferred and exceptional calls. | +| [compiler/ast/OrdinaryAstTreeCopyTest.java](src/test/java/org/xvm/compiler/ast/OrdinaryAstTreeCopyTest.java) | Added | Checks typed and legacy copies of ordinary nodes for metadata preservation and independent child ownership. | +| [compiler/ast/ValidationScopeTest.java](src/test/java/org/xvm/compiler/ast/ValidationScopeTest.java) | Added | Exercises callback behavior after nested failures, finally-scope restoration and early loop exits; also checks that active scopes cannot contain missing state. | +| [compiler/ast/partial/PartialSyntaxTest.java](src/test/java/org/xvm/compiler/ast/partial/PartialSyntaxTest.java) | Added | Checks deferred anonymous syntax, exact cursor containment, argument ownership across labels/groups/nested calls, copying and disposable substitutions without changing written syntax. | +| [compiler/ast/partial/RegisteredChildFieldsTest.java](src/test/java/org/xvm/compiler/ast/partial/RegisteredChildFieldsTest.java) | Added | Checks inherited and declared child fields in traversal, dumping, copying, replacement and removal, including independent mutable child lists. | + +### Existing tool test integration + +| File | Change | What changed and why | +| --- | --- | --- | +| [BundlerTest.java](src/test/java/org/xvm/tool/BundlerTest.java) | Modified | Passes explicit discard listeners through existing launcher/bundler tests, preserving their CLI and artifact assertions under the new contract. | +| [LauncherErrorHandlingTest.java](src/test/java/org/xvm/tool/LauncherErrorHandlingTest.java) | Modified | Migrates callbacks and explicit listener choices and adds a regression proving that the host receives a fatal diagnostic before console handling throws. | +| [LauncherVersionTest.java](src/test/java/org/xvm/tool/LauncherVersionTest.java) | Modified | Uses explicit discard listeners for version commands, preserving the existing output-format checks. | +| [ModuleInfoTest.java](src/test/java/org/xvm/tool/ModuleInfoTest.java) | Modified | Adds a snapshot-source case that parses before its directory exists and checks one owned diagnostic buffer from node construction; migrates existing diagnostic assertions to the new API. | diff --git a/javatools/README.md b/javatools/README.md index b4165f2f718..e844e0f086f 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -8,8 +8,9 @@ This project uses the library produced by the "javatools_utils" project, and the `implicit.x` resource file from the "ecstasy" project. The compiler also supports the compiler-backed language server. Start with -[Compiler architecture for language tooling](#compiler-architecture-for-language-tooling) for the -design rationale, API boundaries and compiler changes required by editor consumers. +[Compiler support for the language server](LSP-COMPILER.md) for a complete walkthrough of the +compiler changes, their rationale, the LSP consumers and the regression tests. The +[architecture overview](#compiler-architecture-for-language-tooling) below provides a shorter introduction. (Note: The test portion of this project may have dependencies on test frameworks.) @@ -40,19 +41,19 @@ belong to the current document revision. Running the command-line compiler and p cannot supply those answers. Successful compilation also discards or rewrites some of the source information that navigation needs; an unfinished buffer may never reach emission at all. -This section and the detailed diagnostic guide below explain the design rationale and changes to -`javatools` that make this possible. The compiler remains independent of LSP, IntelliJ and VS Code. +The [compiler guide](LSP-COMPILER.md) explains the design rationale and changes to `javatools` +that make this possible. The compiler remains independent of LSP, IntelliJ and VS Code. It exposes compilation results and source facts; the adapter in `lang/lsp-server` converts those facts into immutable editor models, applies document-version checks and implements the protocol. | Compiler mechanism | Why an editor needs it | Code and contract | | --- | --- | --- | -| Owned diagnostics and explicit stopping | Associate every report with an attempt, retain speculative failures appropriately, and cancel obsolete work without manufacturing source errors. | `ErrorListener`, `ErrorList`, `Reporting`; [diagnostic design](#compiler-diagnostics-and-error-listeners). | -| Snapshot inputs and compilation outcomes | Compile unsaved module/member files together and distinguish successful output from ordinary partial progress after a failure. | `EmbeddingSupport` and the `ModuleInfo` source hooks; [host snapshots and outcomes](#compile-a-host-snapshot-and-inspect-its-outcome). | +| Owned diagnostics and explicit stopping | Associate every report with an attempt, retain speculative failures appropriately, and cancel obsolete work without manufacturing source errors. | `ErrorListener`, `ErrorList`, `Reporting`; [diagnostic design](LSP-COMPILER.md#diagnostics-cancellation-and-reporting-scopes). | +| Snapshot inputs and compilation outcomes | Compile unsaved module/member files together and distinguish successful output from ordinary partial progress after a failure. | `EmbeddingSupport` and the `ModuleInfo` source hooks; [host snapshots and outcomes](LSP-COMPILER.md#source-snapshots-and-compilation-outcomes). | | Selected semantic facts | Navigate exact overloads and argument slots without reproducing compiler resolution in the editor. Preserve references lost to constant folding. | `InvocationBinding`, `InitializerBinding` and `EmbeddingSupport.Compilation`. | | Declaration and incomplete-source analysis | Answer bounded scope, type and argument queries while the buffer is unfinished, without presenting invalid source as executable code. | `EmbeddingSupport.analyzeDeclarations`, `analyzeIncomplete`, `CursorBinding` and `compiler.ast.partial`. | | Source-tree copying | Probe proposed syntax on an independently parented tree without changing the source tree being inspected. | `AstNode.copyTree` and its covariant specializations. | -| Repository failure observations | Explain a required dependency that cannot be read while preserving repositories' existing best-effort search behavior. | `ModuleRepository`, `DirRepository`, `FileRepository` and the embedding dependency search. | +| Tolerant repositories and explicit dependency validation | Preserve best-effort repository lookup while letting the adapter validate the specific dependency bytes it accepts. | `DirRepository`, `FileRepository`, `LinkedRepository` and the adapter's `XdkDependency`; no new repository reporting API. | | Scoped compiler state and cache reporting | Release operation-owned listeners and temporary state, and replay cached diagnostics to the current attempt. | `Reporting`, `TransientThreadLocal` and `TypeInfo` diagnostic replay. | ### Preserve selected semantic facts @@ -119,474 +120,17 @@ recompiled with the current compiler. Repository lookup remains tolerant: an unreadable candidate can be skipped while another file or repository supplies the module. An unrelated failed candidate does not establish why an import is missing. The LSP validates its explicitly configured dependency bytes before accepting immutable -snapshots; it does not infer dependency errors from repository scans. The diagnostic guide below -explains the intentional Java API migrations, retained CLI behavior and regression evidence; +snapshots; it does not infer dependency errors from repository scans. The +[diagnostic guide](LSP-COMPILER.md#diagnostics-cancellation-and-reporting-scopes) explains the +intentional Java API migrations, retained CLI behavior and regression evidence; it does not claim universal source or binary compatibility for old Java embedding code. ## Compiler diagnostics and error listeners -### Why the old reporting model had to change - -The old error-listener API did not provide a reliable contract for embedding the compiler. -Accepting a listener at the entry point was not enough: deeper calls could substitute `BLACKHOLE`, -accept null as an implicit request to discard reports, or find a different listener through a -shared structure. The caller could receive only part of an attempt's diagnostics without knowing -anything had been lost. Suppression, ownership and stopping policy were spread across unrelated -call sites, so adding a callback at the outer API could not repair what happened underneath it. - -This caused observable failures. The default branch for a host listener imposed a one-error -budget the host had never requested: the lexer stopped at the first recoverable error and never -reported the next one. A previously created branch could also ignore its parent's later abort -request. Two regression tests reproduce those failures on the old compiler with exactly the same -test source that passes on the repaired implementation. - -Reporting also had inconsistent control-flow effects. `log` returned an abort decision, but the -fallback `RuntimeErrorListener` instead threw an exception for ERROR and FATAL. Selecting a -diagnostic destination could therefore change how the operation failed. An editor must be able to -collect an ordinary source error as data, distinguish it from an internal compiler failure, and -let the compiler stop at an appropriate checkpoint. The destination receiving a report should not -introduce a different failure path merely because that report has a particular severity. - -### Changes and enhancements - -* **Separate reporting from stopping.** `log(ErrorInfo)` delivers a diagnostic; - `isAbortDesired()` answers whether work should stop. The lexer and parser retain explicit stop - checkpoints, and forwarding listeners preserve the owner's policy. -* **Make host callbacks usable by the compiler.** `collecting` forwards structured diagnostics - while remembering severity and error codes. A bare callback receives reports but does not - implement those state queries automatically. -* **Make branching consistent.** A host's default branch buffers without an arbitrary one-error - limit. `ErrorList` branches retain their configured budget, and both observe their parent's - current abort request. -* **Make suppression explicit.** Named reasons distinguish probes, cascade suppression and - intentional discards. A derived silence discards messages while retaining the owner's stop - policy. -* **Require a reporting destination at active compilation boundaries.** An omitted listener is - rejected before compilation starts; intentional silence is supplied as an explicit listener. -* **Keep ownership stable.** Constructor-owned listeners remain attached to their operation. - Temporary suppression derives a local listener instead of overwriting the original destination. -* **Preserve diagnostic structure.** `Site` describes source spans, structures or unpositioned - reports. Severity helpers take message arguments as trailing varargs; `tee` forwards reports to - two destinations while combining their state. - -### Removed behavior and its replacement - -* **The boolean return from `log`** is replaced by a reporting call followed by an explicit policy - query where the caller needs to stop. There is one continuation query for wrappers to preserve. -* **`BLACKHOLE` and `BlackholeErrorListener`** are replaced by named suppression factories. - Removing the old symbols makes unmigrated call sites fail to compile, requiring each caller to - identify its reason for silence and whether a parent's abort policy must survive. -* **Null-to-discard fallback at compilation boundaries** is replaced by a required listener. - Passing `silent(DISCARD)` still supports a caller that deliberately wants no reports; accidentally - omitting the destination no longer looks like a successful choice of silence. -* **The silent default in `ResolutionCollector.getErrorListener()`** is removed. Implementations - must provide their destination, and `SimpleCollector` retains the listener it was given. -* **Reassigning a TypeInfo build's listener to `BLACKHOLE`** is replaced by local cascade - suppression. The original listener continues to identify the operation and its stop policy. -* **Severity-triggered exceptions in the fallback runtime listener** are replaced by recorded - abort state. The compiler or runtime caller decides how to end its operation. This does not - remove the command-line launcher's deliberate fatal-error and exit handling. -* **The listener parameter on `Expression.testFitAsType`** is removed because that operation is - a probe. Its staging and fit checks use explicit probe silence; selected validation can still - retain and merge its diagnostics. - -The legacy positional reporting overloads remain available after recompilation, but are -deprecated in favor of `Site` and trailing arguments. Their source re-anchoring behavior is tested. - -### Why this is a prerequisite for the compiler-backed LSP - -**Reliable diagnostic ownership and abort propagation are required for the compiler-backed LSP.** -The adapter must associate reports with the requested compilation, reject unusable semantic -results, and stop obsolete work at compiler checkpoints. It cannot reconstruct diagnostics that -the compiler discarded or obtain errors from work that an unintended one-error budget prevented. -Leaving those failures in place blocks a reliable compiler-backed adapter, regardless of how much -code is added around the public entry point. These changes establish the reporting foundation; -the result, source-snapshot and cached-diagnostic APIs below complete the host-facing path. -The exact helper names and the decision to remove a legacy alias are API design choices. The -delivery, state and stopping guarantees they implement are the essential requirements. - -### Compatibility with ordinary compilation - -Normal command-line compilation continues to use the existing compiler pipeline, console and -exit policy. Users do not need new flags or changes to their Ecstasy source. The CLI supplies an -explicit discard listener only for its external delegate; its console still reports diagnostics. -`ErrorList` retains its configured serious-error budget, fatal reports still request an abort, -and the lexer/parser still stop when the applicable policy requires it. The changes repair -diagnostic delivery and listener ownership without introducing an IDE dependency into the compiler. - -There is strong regression evidence for that continuity: the full Java suite for this listener -migration executed 418 tests successfully, and all 24 rebuilt XDK modules matched a freshly built -master baseline after normalizing only module creation timestamps. The focused checks below add -direct coverage of reporting, aborts, boundary validation and launcher behavior. These results -support confidence in ordinary compilation; they are evidence for the tested paths, not an -exhaustive guarantee for every source program or external integration. - -**The Java embedding API deliberately changes at the planned 0.5.0 boundary.** Existing Java -callers and listener implementations must migrate and recompile. Changed return types, removed -symbols, rejected nulls and the runtime listener's new exception behavior prevent a claim of -100% source or binary compatibility, even for a non-LSP Java consumer. The migration steps below -identify those changes explicitly so ordinary consumers can preserve their intended behavior. - -### Code tour and practical usage - -#### 1. Give each compilation its own diagnostic owner - -Start at [ModuleCompiler](src/main/java/org/xvm/api/ModuleCompiler.java) or -[EmbeddingSupport](src/main/java/org/xvm/api/EmbeddingSupport.java). Supply the attempt's listener -at the entry point and keep it available until the result and diagnostics have been consumed. -For example, with a configured core repository and source text: - -```java -final var errors = new ErrorList(100); -var compiler = new ModuleCompiler(coreRepository); -var module = compiler.compile(sourceText, null, errors); -var diagnostics = List.copyOf(errors.getErrors()); - -if (module == null || errors.hasSeriousErrors()) { - // Publish the diagnostics and do not treat this as a successful compilation. - return; -} -// The caller can now use the successfully compiled module. -``` - -The null argument here is the optional dependency repository, not the listener. The required -listener also reaches launcher construction and dispatch, the compiler, lexer/parser, -[StageMgr](src/main/java/org/xvm/compiler/ast/StageMgr.java), anonymous-class construction and -[Component.SimpleCollector](src/main/java/org/xvm/asm/Component.java). -[ResolutionCollector](src/main/java/org/xvm/asm/ComponentResolver.java) implementations must -return their selected destination. AST reporting and validation helpers no longer silently -accept a missing listener. - -Use a fresh collector for an independent attempt. An `ErrorList` is mutable; even `clear()` keeps -its previously seen diagnostic UIDs. A new operation should not inherit that deduplication history. -Hosts must serialize compiler access and listener callbacks. Final references and non-null -parameters do not make compiler objects or their accumulated state thread-safe. - -#### 2. Report data, then make the control-flow decision - -[ErrorListener](src/main/java/org/xvm/asm/ErrorListener.java) remains the central interface. -Its nested `ErrorInfo` carries severity, code, message arguments and location. `Site.In`, `Site.At` -and `Site.None` describe the location independently of the message: - -```java -errors.error(code, ErrorListener.in(source, start, end), arguments); -if (errors.isAbortDesired()) { - return; -} -``` - -Use `in(source, start, end)` for source text, `at(structure)` for an assembled structure, and -`NOWHERE` for a report without either location. Source positions are the encoded values returned -by `Source.getPosition()`, not raw byte offsets or LSP positions. The `error`, `warn`, `info` and -`fatal` helpers select severity and delegate to `log`; they do not transfer control themselves. - -`isAbortDesired()` and `hasSeriousErrors()` answer different questions. An operation can have -errors while remaining within its budget, and a host may request a stop without reporting an -error. Callers use the first query at stop checkpoints and the second when deciding whether a -result is usable. `hasError(code)` checks whether a particular diagnostic was recorded. - -`RuntimeErrorListener` now prints diagnostics and records an abort request for ERROR/FATAL -instead of deliberately throwing from `log`. Its abort flag remains set for that instance's -lifetime; the shared `RUNTIME` sink is not a fresh per-attempt collector. A reporting callback -can still throw its own exception, which forwarding helpers propagate. The redesign removes the -fallback sink's severity-driven exception policy, not Java exception handling in general. - -#### 3. Choose retention, callback delivery or both - -[ErrorList](src/main/java/org/xvm/asm/ErrorList.java) retains the first report for each diagnostic -UID in insertion order. ERROR/FATAL reports spend its count budget; warnings do not. Its -no-argument constructor uses the shared `ErrorListener.DEFAULT_MAX_ERRORS` (512), `FIRST_ERROR` -selects one, and `UNLIMITED` disables the count limit. A retained FATAL requests an abort with -any budget. Pass an explicit limit to `ErrorList(int)` when an operation needs a different policy. - -When a host wants every report delivered to a callback, use: - -```java -ErrorListener errors = ErrorListener.collecting(hostDiagnostics::add); -``` - -`collecting` forwards every report, including duplicates, and tracks severity and codes before -calling the consumer. It has no count budget and requests an abort on FATAL. A bare lambda -implements only `log` and inherits state queries that return false, so it is not a substitute -when the compiler relies on those queries. - -`tee(first, second)` delivers to both destinations in order and observes either destination's -abort, serious-error and code state. This supports reporting to a host while recording diagnostics -for later replay. `ErrorList.logTo(destination)` replays retained reports without clearing them; -deduplication belongs to the destination. If the first destination throws, the second is not called. - -#### 4. Buffer speculative diagnostics until a result is accepted - -`branch(node)` creates an `ErrorList.BranchedErrorListener`. Pass it to speculative work and merge -it once if that work's diagnostics should be retained: - -```java -var trial = errors.branch(node); -// Run the candidate validation with trial as its listener. -if (keepDiagnostics) { - trial.merge(); -} -``` - -Unmerged diagnostics remain local to the branch. A default host branch has an unlimited count -budget, while an `ErrorList` branch copies its parent's configured limit. Both observe the -parent's current abort request, and a local FATAL still stops the branch. A node-associated -branch re-anchors structure reports to that node's source span; explicit source spans remain intact. - -Merging an unbranched listener returns that listener. Merging a branch forwards its reports and -returns the parent without clearing the buffer. Do not repeatedly merge an accepted branch: -an `ErrorList` parent can deduplicate replay, but an arbitrary callback need not. - -#### 5. Suppress reports without losing the operation's owner - -`Silence` and `SilentErrorListener`, nested in `ErrorListener`, make suppression explicit. -`PROBE`, `CASCADE` and `DISCARD` are **reasons, not severity levels**. They have identical discard -behavior; compiler algorithms must not depend on which name is selected. - -| Reason | Use it when | -|---|---| -| `PROBE` | Testing an alternative, such as whether an expression fits a candidate type. Failure is the answer to that query, not necessarily a problem in the selected program. | -| `CASCADE` | Examining already incomplete information whose follow-on errors would obscure the original failure. | -| `DISCARD` | Deliberately ignoring this destination, such as the CLI's external delegate when its console already reports diagnostics. | - -The important behavioral distinction is between a standalone sink and a derived silence: - -```java -// No parent listener or stop policy: an independent, deliberately silent probe. -ErrorListener independent = ErrorListener.silent(ErrorListener.Silence.PROBE); - -// Suppress follow-on reports but continue to honor this operation's abort request. -ErrorListener quiet = errors.silence(ErrorListener.Silence.CASCADE); -``` - -`independent` never requests an abort. `quiet.isAbortDesired()` observes `errors`, even when -no diagnostic caused that stop request. Neither retains discarded messages; use a branch when -diagnostics might later be needed. `suppressed()` exposes a derived silence's parent, not a buffer -of hidden diagnostics. Re-silencing an existing silence returns the same instance and reason. - -For a separately constructed standalone sink, `new SilentErrorListener(reason)` needs no -explicit null parent. The two-argument constructor retains an optional parent for callers that -need its stop policy. Required factory arguments and results use `@NotNull`; intentional absence -uses `@Nullable`, including `suppressed()` on a standalone sink. Public construction boundaries -still reject invalid null arguments immediately; annotations also expose the contract to IDEs -and Kotlin callers. - -The names make intent searchable and reviewable. `BLACKHOLE` did not distinguish those purposes, -and substituting it for an active listener also removed access to that listener's stop policy. -Removing the old constant and class makes missed migrations visible to the Java compiler. A -deprecated alias was possible, but would allow unexplained suppression to survive the migration. -Callers that want exactly the old standalone discard behavior can use `silent(DISCARD)`. - -Stable ownership is equally important. In -[TypeConstant](src/main/java/org/xvm/asm/constants/TypeConstant.java), `cascade(incomplete, errors)` -derives suppression at affected calls instead of assigning a sink to the `errors` parameter. -The original destination remains available throughout the build. Constructor-owned references, -such as those in `StageMgr`, `SimpleCollector` and `Launcher`, are final so they cannot drift to -another destination during the operation. Their listeners' counters can still change. - -Non-null means that an active reporting boundary has an explicit owner or an explicit discard -policy. It does not mean that every listener field everywhere must be permanently non-null or -final: nested parser/resolver work needs temporary destinations and scoped restoration. That -ownership work is described separately below. - -#### 6. Migrate Java callers and implementations - -The planned release boundary is **0.5.0**. Recompile Java clients and listener implementations -after applying these changes; Java cannot retain compatibility overloads that differ only by -the return type of `log`. - -| Previous use | Replacement and consequence | -|---|---| -| `if (errors.log(...))` | Report first, then query `errors.isAbortDesired()` and perform the same return/throw at that checkpoint. Update custom implementations to `void log(ErrorInfo)`. | -| `BLACKHOLE` or `BlackholeErrorListener` | Choose a named standalone silence or derive one from the current owner when its abort policy must survive. | -| Null at a compilation/launcher boundary | Pass the operation's listener or explicit `silent(DISCARD)`. Missing listeners now fail at the boundary. | -| A `ResolutionCollector` relying on the default sink | Implement `getErrorListener()` and retain the intended destination. | -| `testFitAsType(..., errors)` | Remove the listener argument; the operation owns its probe policy. | -| Depending on `RUNTIME.log` to throw on severity | Check `isAbortDesired()` and explicitly choose the operation's failure handling. | -| Legacy positional reporting overloads | They remain callable after recompilation but are deprecated; migrate to `Site` and trailing message arguments. | - -### Regression evidence and how to reproduce it - -The strongest evidence is the pair of -[host regression tests](src/test/java/org/xvm/asm/ErrorListenerHostRegressionTest.java). -Their source uses API shapes available on both sides of the reporting change. On the previous -implementation at `1d9352b6f`, both compile and fail for the expected behavioral reason. On the -repaired implementation, the same source passes both cases: - -* `hostBranchLetsLexerReportBothErrors` sends two bad string escapes through the real lexer. - Previously it threw after the first error because the default branch had a one-error budget. - It now completes the scan and publishes both reports when the branch is merged. -* `parentAbortReachesTheLexerThroughAnExistingBranch` aborts the parent after creating its branch. - Previously the lexer continued because the branch considered only its local state. It now - honors the parent at its reporting checkpoint. - -[ErrorListenerMigrationTest](src/test/java/org/xvm/asm/ErrorListenerMigrationTest.java) also proves -that derived silence preserves a host stop request without publishing an error. Its parser/lexer -budget tests and source-reanchoring test guard existing behavior. The branch, site, silence and -abort suites cover collection, forwarding, replay and suppression. -[ErrorListenerBoundaryTest](src/test/java/org/xvm/api/ErrorListenerBoundaryTest.java) checks required -listeners and destination retention; these assertions enforce the chosen API contract rather than -claiming that every API change reproduces an old bug. - -The 2026-10-09 focused run on this implementation passed **60 tests with zero failures, errors -or skips**. Earlier full-suite verification of the same production listener migration passed -418 executed Java tests, with 42 skips recorded separately. A forced XDK rebuild produced -24 modules matching the master baseline after normalizing creation timestamps. The separate -integration checkout combining the reporting changes with the embedding foundations also passed -the real compiler-consumer test with no skips. These checks cover different scopes; the 60 focused -tests are not presented as a substitute for compilation and integration validation. - -Run the focused tests from the repository root: - -```bash -./gradlew :javatools:test \ - --tests 'org.xvm.asm.ErrorListener*Test' \ - --tests org.xvm.api.ErrorListenerBoundaryTest \ - --tests org.xvm.tool.LauncherErrorHandlingTest \ - --rerun-tasks --no-build-cache -``` - -These focused cases do not require installed XDK modules. For broader compiler verification, -build the XDK first, then force the Java test run: - -```bash -./gradlew :xdk:installDist -./gradlew :javatools:test --rerun-tasks --no-build-cache -``` - -Check failures and skips in `javatools/build/test-results/test/TEST-*.xml`; a green task alone does -not establish that optional tests executed. The real compiler-consumer integration test additionally -exercises the standard build path with compiled modules supplied by its Gradle dependencies. - -### Embedding API boundaries - -The embedding API combines the listener contract, scoped parser/validation reporting, compilation -outcomes, cached TypeInfo diagnostic replay, cancellation and host-supplied source snapshots. -Consumer tests exercise these mechanisms together through the public entry points. - -The independent [repository cache correction](https://github.com/xtclang/xvm/pull/683) preserves -tolerant lookup while reusing unchanged successful and failed candidates. It adds no reporting API -and does not attribute a missing dependency to an unrelated unreadable file. Hosts that require -validation of specific input files can read those files before constructing their repository. -[The parser/branch correction](https://github.com/xtclang/xvm/pull/684) supplies the parent-abort, -default-budget and lexical-rollback behavior preserved here. Empty transient-local retention is -an independent utility fix. None of these APIs depends on an LSP implementation or editor. - -The ordinary compilation result exposes partial progress without treating invalid code as -executable output. Selected-call facts and the separate declaration/incomplete-analysis APIs -extend that model as described in the [architecture overview](#compiler-architecture-for-language-tooling). -The language server remains a consumer outside `javatools`. - -### Scoped parser and validation reporting - -`Parser.attempt()` replaces `Parser.SafeLookAhead`: use `Attempt.keep()` in place of -`keepResults()`. A kept attempt merges its buffered diagnostics, including warnings; a discarded -attempt restores its tokens and recovery state. Nested attempts observe their parent's abort -request. This removes a public nested parser class and requires source migration/recompilation at -the same breaking release boundary described above. - -Parser and resolver destinations use `Reporting` scopes, closed in reverse order with -try-with-resources. `NameResolver.getErrorListener()` is null outside `resolve`; callbacks borrow -the caller's non-null listener only during that call. These scopes restore destinations on every -exit. They do not make a parser, resolver, AST or listener safe for concurrent use. Lexical -reports also follow the active attempt, preserving lexical rollback from the parser correction. -Module-name-only scans can supply an explicit discard listener when reports are unwanted. - -Loop and try/finally label variables retain their context/listener as one immutable -`ValidationScope` value. Each statement restores its previous value on normal, early and -exceptional exits, and `Statement.validate` likewise restores its common validation context. -The temporary references stay on the AST because label-variable and jump callbacks need the -currently validating statement. No shared mutable holder is added to cloned AST nodes. -`EvalCompiler` and `ModuleInfo.Node` own final diagnostic buffers from construction onward. - -The unit regressions cover nested reporting, failed host/resolver callbacks, token rollback, -abort propagation and exceptional statement exits without requiring installed modules. Compile -and run the existing `manualTests/src/main/x/loop.x` and `exceptions.x` exercises with the built -XDK to check lazy label variables and try/finally behavior through code generation and execution. -The separate Gradle compiler-consumer test supplies its compiled modules through dependencies. - -### Compile a host snapshot and inspect its outcome - -```java -ErrorList reports = new ErrorList(); -ErrorListener operation = ErrorListener.cancellable(reports, cancelled::get); -EmbeddingSupport.Compilation result = embedding.compileModule( - new Source(text, documentName), dependencies, operation); - -if (result.succeeded()) { - ModuleStructure executable = result.module(); -} -if (result.parsed() != null) { - // Inspect ordinary parsed syntax, even if semantic validation subsequently failed. -} -if (result.pool() != null) { - // Inspect structures built before a semantic failure, on the compiling worker. -} -``` - -`Compilation` separates a successful executable module from the file structure and assembled AST -that an attempt reached. Loading/parsing failures can leave no AST; semantic failures can leave a -file and pool without an executable module. Returning these objects does not make them immutable -or safe to share between workers. The host owns their lifetime and must detach any facts it shares. -The existing String/File convenience methods delegate to this path and retain their module/null -or boolean result shapes. A compilation creates a fresh compiler and build repository; hosts must -serialize attempts sharing mutable repositories. - -`compileModule(ModuleInfo, ...)` runs the same source discovery, package assembly and resource -association used by the CLI. A host can override `readSource` for unsaved text, `sourceEntries` -for added/deleted members, and the protected source-identity constructor for a wholly virtual -module. Membership, text and resource identity must describe one consistent snapshot. Construct a -fresh `ModuleInfo` per attempt: it caches its assembled tree. Ordinary file constructors retain -disk discovery and reading. - -Errors stay structured. A non-module/empty source gets a source diagnostic. Failed dependency -resolution can include the original repository path and I/O cause. Successful fallback past an -unreadable file does not fail a valid compilation. Unexpected exceptions and assertions are reported -as internal failures even when an earlier source error was already collected. FATAL reports reach -the host before the CLI console's deliberate exception can stop execution. - -`footprint(result)` reads the supplied compilation pool and repository counts without starting an -interpreter. `ensureRuntimePool()` explicitly initializes the runtime if needed; the deprecated -`getConstantPool()` retains that behavior. A runtime pool is not the pool of a failed compilation. - -### Report diagnostics from cached type information - -Structures no longer store, inherit or discover a listener through an ambient pool. Pass the -operation's listener to `XvmStructure.log` and `TypeConstant.ensureTypeInfo(errors)`. The no-argument -TypeInfo lookup is an explicit silent metadata query. A successful TypeInfo build retains an -immutable diagnostic list; a later explicit lookup replays it to the new listener, including when -it returns the same cached TypeInfo instance. Receiving `ErrorList` instances deduplicate repeated -reports; a raw callback deliberately receives each replay. Cache invalidation causes a fresh build -and replaces the retained reports. - -This matters when speculative work builds a generic type before the selected source use: the -later caller must still hear an ignored-annotation warning. Tests cover source and serialized -library types, silent-first/cache-hit ordering, distinct reporting owners, final selected uses -and partial structures after semantic failure. - -### Cancel obsolete work without manufacturing errors - -`ErrorListener.cancellable` decorates the operation listener, preserving its reporting, state, -branching and merging while polling the host predicate at abort checkpoints. A valid token stream -also polls; cancellation cannot depend on finding a source error. Module-member loading and phase -boundaries retain the same policy. This is cooperative cancellation, not interruption: a host must -keep its predicate true once an attempt becomes obsolete. Other attempts have separate policies. - -The direct consumer suites `EmbeddingDiagnosticsTest`, `EmbeddingSourceSnapshotTest` and -`TypeInfoDiagnosticsTest` use compiled modules supplied by Gradle, with no production adapter. -Run them from the repository root: - -```bash -./gradlew :lang:lsp-server:test \ - -PincludeBuildLang=true -PincludeBuildAttachLang=true \ - --tests 'org.xvm.lsp.adapter.Embedding*Test' \ - --tests org.xvm.lsp.adapter.TypeInfoDiagnosticsTest \ - --rerun-tasks --no-build-cache -``` - -The earlier numerical receipts above describe the original listener migration. Validation of the -combined branch must additionally include these consumer suites and a fresh XDK build; those -historical receipts alone do not validate the enlarged scope. +The detailed design and migration guide now lives in +[Compiler support for the language server](LSP-COMPILER.md#diagnostics-cancellation-and-reporting-scopes). +It explains [why probes and final propagated listeners replace implicit suppression](LSP-COMPILER.md#why-explicit-probes-and-a-final-propagated-listener), +diagnostic ownership, scoped reporting, cancellation, compatibility and regression evidence. ## Assembler diff --git a/lang/doc/manual-test-plan.md b/lang/doc/manual-test-plan.md index 66cb33110ba..d3024586ba1 100644 --- a/lang/doc/manual-test-plan.md +++ b/lang/doc/manual-test-plan.md @@ -5,18 +5,100 @@ The shared catalog contains **284 scenarios**: X1–X279, CFG1–CFG3 and 7a.8/7 The compiler adapter is enabled by default; Tree-sitter remains a selectable syntax-only adapter. Scenario definitions, source fixtures and native assertions are retained below. +## Try the complete implementation + +Use this guide from the same checkout as the plugin you build. For the complete reference in +[draft #693](https://github.com/xtclang/xvm/pull/693), check out +`errs/full-testable-lsp-20261009`. All commands below run from the repository root. +Use JDK 25 or newer to start Gradle. The build prepares the bundled server/compiler and editor +dependencies; the first run may download the IDE, Node and native parser tools. +IntelliJ's native harness needs a graphical desktop. VS Code also needs a display, or `xvfb-run` +on headless Linux (see the [extension testing notes](../vscode-extension/README.md#testing)). + +Choose either automated acceptance or manual exploration. The automated harness owns its isolated +editor window and closes it afterward; launch a separate development session for manual work. + +### Manually verify the demo programs + +Launch one editor: + +```bash +# IntelliJ sandbox with the plugin and compiler server +./gradlew :lang:intellij-plugin:runIde \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler + +# Or VS Code Extension Development Host +./gradlew :lang:vscode-extension:runCode \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler +``` + +1. Open a fresh scratch folder/project in the launched editor. Confirm the compiler backend using + [Launch and confirm the backend](#launch-and-confirm-the-backend). No separate XDK installation + is required; matching libraries are bundled. +2. Pick a feature in the [XdkAdapter playbook](#xdkadapter-playbook). Copy its fenced Ecstasy demo + into the exact filenames and directory layout specified there. For cases that refer to shared + variants, open [scenarios.json](../test-fixtures/compiler-playbook/scenarios.json), find the + case ID, and use its `values` sources and `manual` notes. Preserve the stated source-module + configuration for multi-module cases. JSON strings must be decoded when copied into `.x` files. +3. Perform the row's action and compare the visible result and source edits with its expected + result. `|` in a row and `§` in a shared anchor mark cursor positions; do not insert them into + the program. Undo temporary edits before trying another row. Deliberately incomplete programs + can have diagnostics while completion and signature help remain available. +4. To compare adapters, invoke **Tools → Switch Ecstasy Language Adapter** in IntelliJ or + **Ecstasy: Switch Language Adapter** in VS Code. The server restarts and restores open buffers. + Compiler-specific semantic expectations apply to Compiler mode; switch back before continuing. +5. For a finding, record the commit, editor/version, adapter, case ID, exact source and actions, + expected/actual result, and server/editor logs. Use the coverage and compatibility notes below + to distinguish a known client limitation from a regression. + +Useful starting points: + +| Feature | Demo and cases | +| --- | --- | +| Diagnostics, hover, definitions, references, outline and folding | [Navigation.x, X1–X5](#a-diagnostics-navigation-and-structure) | +| Typed completion and overload/signature help | [Editing.x, X6–X20](#b-typed-and-scope-aware-completion) | +| Multiple source files and type hierarchy | [Project.x and Project/Child.x](#d-module-files-overlays-and-type-hierarchy) | +| Implementations, call hierarchy, highlighting and inlays | [Lookups.x and Consumers.x](#f-type-definition-and-implementation-lookup) | +| Rename, dependencies and checked edits | [Compiler-validated rename](#h-compiler-validated-rename), then later scenario tables | +| Themes, CodeLens, settings, imports and refactoring variants | Find the feature name or X-number in this document and the [shared catalog](../test-fixtures/compiler-playbook/scenarios.json) | + ## Acceptance reports -Run both native harnesses from the repository root: +Run the complete native playbooks sequentially from the repository root: ```bash ./gradlew :lang:intellij-plugin:testCompilerPlaybook \ - -PincludeBuildLang=true -PincludeBuildAttachLang=true + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + --rerun-tasks --no-build-cache ./gradlew :lang:vscode-extension:testCompilerPlaybook \ - -PincludeBuildLang=true -PincludeBuildAttachLang=true + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + --rerun-tasks --no-build-cache +``` + +These commands force fresh execution, including prerequisite host tests where scheduled. VS Code's +unfiltered task also runs the LSP unit and packaged-stdio suites. IntelliJ's task includes feature, +startup, focus, settings and project-lifecycle checks; the large-file probe remains opt-in. +The catalog currently has 284 scenarios; a partial-coverage result is not a full pass. + +For a short feature selection, use exact IDs (this example checks structure, diagnostics, hover +and typed completion): + +```bash +./gradlew :lang:intellij-plugin:testCompilerPlaybook \ + --tests '*CompilerPlaybookTest.compilerPlaybook' \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + -PintellijPlaybookCases=X1,X2,X3,X6 --rerun --no-build-cache +./gradlew :lang:vscode-extension:testCompilerPlaybook \ + -PincludeBuildLang=true -PincludeBuildAttachLang=true -Plsp.adapter=compiler \ + -PcompilerPlaybookCases=X1,X2,X3,X6 ``` -Each editor writes per-case results under its `build/reports/compiler-playbook/` directory. +The IntelliJ method filter limits this selection to the feature session; remove it for the other +lifecycle sessions. VS Code's selected-case task does not schedule the host regression suites. +Selected runs report excluded cases separately and cannot establish whole-catalog acceptance. + +Each editor writes per-case results under `lang//build/reports/compiler-playbook/run-*/` +(`intellij-plugin` or `vscode-extension` for ``). VS Code also writes `latest-run.txt`. IntelliJ also records IDE failures, screenshots and server traces; VS Code records extension errors and the isolated workspace. Judge acceptance from those reports, including their catalog hash, selected IDs, skipped/not-selected cases and errors. Historical selected runs do not establish @@ -1291,7 +1373,7 @@ Run the compiler playbook from the repository root: This builds the extension and its bundled compiler, runs the server and packaged-JAR regression suites, then launches a real VS Code extension host. It reads the fixtures below directly, creates -a separate workspace/profile, and runs one case for every X1–X265 row plus the configuration and +a separate workspace/profile, and runs one case for every X1–X279 row plus the configuration and compiler-diagnostic checks. Missing case IDs, a wrong backend, failures and skipped editor cases fail the run. The editor cases run on every invocation; Gradle may reuse unchanged host-test results. The test window's status bar shows completed/selected cases, remaining cases and the current case, @@ -1439,14 +1521,15 @@ case to execute. Native runs remain occasional checkpoints. ### Shared editor scenarios Both drivers read [the shared scenario data](../test-fixtures/compiler-playbook/scenarios.json) -for all 148 scenarios: X1–X143, CFG1–CFG3 and 7a.8–7a.9. The catalog owns titles, source-module +for all 284 scenarios: X1–X279, CFG1–CFG3 and 7a.8–7a.9. The catalog owns titles, source-module configuration, fixture selectors, edits, cursor/definition anchors, variants, expectations and manual-check notes. Base programs remain the canonical fixtures below; bounded replacement programs also live in the shared scenario values. A `§` marks an offset; `${0}` templates substitute literal values without evaluating code. Native TypeScript and Kotlin code still performs editor actions and assertions. VS Code executes -all 148 cases. IntelliJ now has assertions for the same 148, plus a separate startup check. +all 284 cases. IntelliJ has coverage entries for the same catalog, including the explicitly +documented partial cases, plus separate startup and lifecycle checks. Current selected native pass receipts are recorded above; implementation is not validation. A missing driver implementation must be called `not-implemented`, not an unsupported IDE feature. `not-run` means an implemented case was unselected or prevented from running, such as after an earlier failure. Partial coverage never appears @@ -1462,8 +1545,8 @@ entry. Keep client-specific protocol and UI mechanics in the drivers. ### Launch and confirm the backend -From the repository root, choose one command. Keep `-Plsp.adapter=compiler` on the editor launch -task too: a later build without it can restore the default backend. +From the repository root, choose one command. Compiler mode is the repository default; +`-Plsp.adapter=compiler` makes the intended backend explicit if local properties override it. ```bash # IntelliJ sandbox From b0c5e0daaa6b592cf624dfcb14ab18aba0c489e1 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:16 +0100 Subject: [PATCH 016/324] Guard code actions and renames at synthetic bodies and locked library sources - Guard code-action insertions against synthetic declaration bodies. - Identify locked Ecstasy sources and explain library rename restrictions. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- .../xtclang/idea/XtcEditorTabTitleProvider.kt | 13 ++++ .../src/main/resources/META-INF/plugin.xml | 1 + .../lsp/adapter/xdk/CompilerMemberAction.kt | 4 +- .../lsp/adapter/xdk/CompilerMissingMethod.kt | 9 +-- .../kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt | 22 ++++++ .../xvm/lsp/server/XtcTextDocumentService.kt | 16 +++- .../adapter/CompilerMissingMethodProofTest.kt | 18 +++++ .../xvm/lsp/adapter/XdkMemberActionsTest.kt | 13 ++++ .../lsp/server/LibraryContentProtocolTest.kt | 50 +++++++++++++ .../xvm/lsp/server/QueryFailureLoggingTest.kt | 75 +++++++++++++++++++ .../org/xvm/lsp/server/XdkRenameServerTest.kt | 43 +++++++++++ 11 files changed, 254 insertions(+), 10 deletions(-) create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcEditorTabTitleProvider.kt create mode 100644 lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/QueryFailureLoggingTest.kt diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcEditorTabTitleProvider.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcEditorTabTitleProvider.kt new file mode 100644 index 00000000000..41cd8d55635 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/XtcEditorTabTitleProvider.kt @@ -0,0 +1,13 @@ +package org.xtclang.idea + +import com.intellij.openapi.fileEditor.impl.EditorTabTitleProvider +import com.intellij.openapi.project.Project +import com.intellij.openapi.vfs.VirtualFile + +/** Makes read-only Ecstasy sources recognizable before users attempt an edit or refactoring. */ +class XtcEditorTabTitleProvider : EditorTabTitleProvider { + override fun getEditorTabTitle( + project: Project, + file: VirtualFile, + ): String? = if (file.extension == "x" && !file.isWritable) "${file.name} (locked)" else null +} diff --git a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml index ab04a3d19b9..19c7de497b1 100644 --- a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml +++ b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml @@ -99,6 +99,7 @@ + /liveTemplates/XTC diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt index a6a7871af20..6ddaafbd1f7 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMemberAction.kt @@ -83,14 +83,12 @@ internal fun compilerMemberActions( if (structure.format != Format.CLASS || structure.isSynthetic || errors.isAbortDesired) { return@flatMap emptyList() } + val insertion = XdkAst.memberInsertion(node) ?: return@flatMap emptyList() val info = ExecutionTrace.api("TypeConstant.ensureTypeInfo(member-actions)") { structure.formalType.ensureAccess(Access.PRIVATE).ensureTypeInfo(errors) } if (errors.hasSeriousErrors() || errors.isAbortDesired) return@flatMap emptyList() - val end = node.ensureBody().endPosition - val insertion = - SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end) - 1) val methods = info.methods.values.filter { it.identity.isTopLevel && it.isVirtual && !it.isCtorOrValidator diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt index 8055740eac6..5d862c1d427 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/CompilerMissingMethod.kt @@ -127,6 +127,7 @@ internal fun compilerMissingMethods( owner } val external = destinations.entries.singleOrNull { it.value.location == receiver?.destination } + val insertion = external?.value?.insertion ?: destination?.let(XdkAst::memberInsertion) ?: return@mapNotNull null val structure = (destination?.component ?: external?.key?.component) as? ClassStructure ?: return@mapNotNull null val crossOwner = destination !== owner // Preserve concrete call types; never infer an owner's formal from equal actual types. @@ -319,14 +320,12 @@ internal fun compilerMissingMethods( MissingParameter("${rendered.source} $argumentName", rendered.proof, imports = rendered.imports) } - fun position(value: Long) = SemanticModel.Position(Source.calculateLine(value), Source.calculateOffset(value)) val signature = parameters.joinToString(", ") { it.source } CompilerMissingMethod( XdkMissingMethods.Candidate( name.calleeLocation(), name.name, - external?.value?.insertion - ?: position(requireNotNull(destination).ensureBody().endPosition).let { it.copy(column = it.column - 1) }, + insertion, external?.value?.location ?: requireNotNull(destination).location(), "${if (crossOwner) "public" else "private"} ${if (dispatch == Dispatch.STATIC) "static " else ""}" + "$generic${if (conditional) "conditional " else ""}$result ${name.name}($signature)", @@ -555,12 +554,12 @@ internal fun EmbeddingSupport.Compilation.missingMethodDestinations( aliases.filterKeys { it !in existing }.mapValues { (module, alias) -> XdkMemberActions.Import(importAt, " package $alias import $module;") } - val end = node.ensureBody().endPosition + val insertion = XdkAst.memberInsertion(node) ?: return@mapNotNull null val declaration = node.location(node.nameToken.startPosition, node.nameToken.endPosition) declaration to XdkMissingMethods.Destination( node.location(), - SemanticModel.Position(Source.calculateLine(end), Source.calculateOffset(end) - 1), + insertion, aliases, imports, module.source.fileName, diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt index 3375e575dc0..55df4a090c5 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/adapter/xdk/XdkAst.kt @@ -22,6 +22,28 @@ import java.util.IdentityHashMap * compiler-owned snapshot cached by [XdkAdapter]. */ internal object XdkAst { + /** + * Locate a written closing brace for a member insertion. Semicolon-only declarations can have + * compiler-generated bodies with zero positions; those bodies are not source edit destinations. + * Inspect existing children rather than creating a body with `ensureBody()`. + */ + fun memberInsertion(node: TypeCompositionStatement): SemanticModel.Position? { + val body = node.childNodes().filterIsInstance().singleOrNull() ?: return null + if (body.endPosition <= body.startPosition || + body.startPosition < node.startPosition || body.endPosition > node.endPosition + ) { + return null + } + val source = node.source.clone() + source.position = body.startPosition + if (!source.hasNext() || source.next() != '{') return null + source.position = body.endPosition + source.rewind() + val insertion = source.position + if (source.next() != '}') return null + return SemanticModel.Position(Source.calculateLine(insertion), Source.calculateOffset(insertion)) + } + /** * Attempt-local structure ownership; binary structures cannot acquire a guessed source span. */ diff --git a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt index d7abf087205..6b48968bb27 100644 --- a/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt +++ b/lang/lsp-server/src/main/kotlin/org/xvm/lsp/server/XtcTextDocumentService.kt @@ -279,7 +279,15 @@ class XtcTextDocumentService( // Release that thread before entering either the document or compiler lifecycle. result.whenCompleteAsync { _, failure -> synchronized(lifecycle) { pendingQueries.remove(result) } - val outcome = if (failure == null) "completed" else "canceled or failed" + val cause = generateSequence(failure) { (it as? CompletionException)?.cause }.lastOrNull() + val outcome = + when { + cause == null -> "completed" + cause is CancellationException -> "canceled" + cause is ResponseErrorException && cause.responseError.code != ResponseErrorCode.InternalError.value -> "rejected" + else -> "failed" + } + if (outcome == "failed") logger.error("$method: request failed for $uri", cause) ExecutionTrace.event(trace, "end", mapOf("outcome" to outcome)) logger.info( "{}: {} in {}", @@ -1232,7 +1240,11 @@ class XtcTextDocumentService( ?: throw ResponseErrorException( ResponseError( ResponseErrorCode.InvalidParams, - "Rename not allowed at this position", + if (adapter.readOnlyDocument(params.textDocument.uri) != null) { + "Cannot rename a symbol in read-only library source. Open the library's source project to refactor it." + } else { + "Rename not allowed at this position" + }, null, ), ) diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt index 2883fff5443..836771ff338 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/CompilerMissingMethodProofTest.kt @@ -2,6 +2,8 @@ package org.xvm.lsp.adapter import org.assertj.core.api.Assertions.assertThat import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource import org.xvm.api.EmbeddingSupport import org.xvm.asm.ErrorList import org.xvm.compiler.Source @@ -21,6 +23,22 @@ import org.xvm.lsp.adapter.xdk.toDependency * source compiles. */ class CompilerMissingMethodProofTest { + @ParameterizedTest + @ValueSource(strings = ["const Key(String name);", "class Empty;", "service Idle;"]) + fun `semicolon-only types do not break missing method destinations for neighboring classes`(declaration: String) { + CompilerTestSupport.configure() + val text = "module Library { $declaration class Other {} }" + val errors = ErrorList() + val compilation = EmbeddingSupport.instance().compileModule(Source(text, SOURCE), null, errors) + assertThat(compilation.succeeded()).describedAs(errors.errors.toString()).isTrue() + + val destinations = compilation.missingMethodDestinations() + + assertThat(destinations).hasSize(1) + val destination = destinations.values.single() + assertThat(XdkRename.offset(text, destination.insertion)).isEqualTo(text.indexOf("{}") + 1) + } + @Test fun `imports shift the inserted method but cannot weaken its source signature proof`() { CompilerTestSupport.configure() diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt index a26f81825a2..9cbb24da5b9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/adapter/XdkMemberActionsTest.kt @@ -20,6 +20,19 @@ import java.nio.file.Path class XdkMemberActionsTest { @TempDir lateinit var directory: Path + @Test + fun `semicolon-only declarations preserve code actions for a neighboring class`() { + val text = + "module App { const Key(String name); class Empty; " + + "class Base { Int count() = 1; } class Box extends Base {} }" + workspace(text) { adapter, uri -> + val action = actions(adapter, uri, text).single { it.title == "Override Int64 count()" } + val changed = apply(text, requireNotNull(action.edit).changes.getValue(uri)) + assertThat(changed).contains("const Key(String name);", "class Empty;") + assertThat(adapter.compile(uri, changed).diagnostics).isEmpty() + } + } + @Test fun `recursive inherited signatures do not suppress unrelated member actions`() { val text = diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt index 9294365ed60..47bc323adb9 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/LibraryContentProtocolTest.kt @@ -9,6 +9,8 @@ import org.eclipse.lsp4j.InitializedParams import org.eclipse.lsp4j.Location import org.eclipse.lsp4j.MonikerParams import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.PrepareRenameParams +import org.eclipse.lsp4j.RenameParams import org.eclipse.lsp4j.TextDocumentContentCapabilities import org.eclipse.lsp4j.TextDocumentContentChangeEvent import org.eclipse.lsp4j.TextDocumentContentParams @@ -19,6 +21,8 @@ import org.eclipse.lsp4j.VersionedTextDocumentIdentifier import org.eclipse.lsp4j.jsonrpc.ResponseErrorException import org.eclipse.lsp4j.services.LanguageClient import org.junit.jupiter.api.Test +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.ValueSource import org.mockito.ArgumentMatchers.any import org.mockito.Mockito.doAnswer import org.mockito.Mockito.mock @@ -36,6 +40,52 @@ import java.util.concurrent.TimeUnit.SECONDS * be read; client-provided paths or presentations cannot replace authoritative source content. */ class LibraryContentProtocolTest { + @ParameterizedTest + @ValueSource(booleans = [false, true]) + fun `rename in a library method explains that navigation sources are read only`(virtual: Boolean) { + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + server + .initialize( + editorInitializeParams().apply { + if (virtual) capabilities.workspace.textDocumentContent = TextDocumentContentCapabilities() + }, + ).join() + val uri = "file:///App.x" + val text = + "module App { package webcli import webcli.xtclang.org; " + + "void run() { webcli.Gateway.getPassword(); } }" + val documents = server.textDocumentService + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text))) + val target = + documents + .definition( + DefinitionParams(TextDocumentIdentifier(uri), Position(0, text.indexOf("getPassword") + 5)), + ).get(30, SECONDS) + .left + .single() + val content = + if (virtual) { + server.workspaceService + .textDocumentContent(TextDocumentContentParams(target.uri)) + .join() + .text + } else { + Files.readString(Path.of(URI(target.uri))) + } + assertThat(content.lines()[target.range.start.line]).contains("getPassword(") + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(target.uri, "xtc", 1, content))) + val middle = Position(target.range.start.line, target.range.start.character + 5) + val document = TextDocumentIdentifier(target.uri) + assertThatThrownBy { documents.prepareRename(PrepareRenameParams(document, middle)).get(30, SECONDS) } + .hasCauseInstanceOf(ResponseErrorException::class.java) + .hasRootCauseMessage( + "Cannot rename a symbol in read-only library source. Open the library's source project to refactor it.", + ) + assertThat(documents.rename(RenameParams(document, middle, "readPassword")).get(30, SECONDS)).isNull() + } + } + @Test fun `library content is negotiated per connection and cannot read unregistered or retired URIs`() { listOf(false, true).forEach { supported -> diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/QueryFailureLoggingTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/QueryFailureLoggingTest.kt new file mode 100644 index 00000000000..1b63738375b --- /dev/null +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/QueryFailureLoggingTest.kt @@ -0,0 +1,75 @@ +package org.xvm.lsp.server + +import ch.qos.logback.classic.Level +import ch.qos.logback.classic.Logger +import ch.qos.logback.classic.spi.ILoggingEvent +import ch.qos.logback.core.AppenderBase +import org.assertj.core.api.Assertions.assertThat +import org.assertj.core.api.Assertions.assertThatThrownBy +import org.eclipse.lsp4j.WorkspaceSymbolParams +import org.eclipse.lsp4j.jsonrpc.ResponseErrorException +import org.eclipse.lsp4j.jsonrpc.messages.ResponseError +import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import org.junit.jupiter.api.Test +import org.slf4j.LoggerFactory +import org.xvm.lsp.adapter.Adapter +import org.xvm.lsp.adapter.mock.MockAdapter +import java.util.concurrent.CancellationException +import java.util.concurrent.CompletionException +import java.util.concurrent.CopyOnWriteArrayList +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit.SECONDS + +/** Internal query failures need a local stack trace; ordinary cancellation and protocol rejections do not. */ +class QueryFailureLoggingTest { + @Test + fun `unexpected backend failures retain their original exception in the server log`() { + val failure = IllegalArgumentException("Negative source position") + val errors = queryErrors(CompletionException(failure)) + assertThat(errors).hasSize(1) + val event = errors.single() + assertThat(event.formattedMessage).contains("workspace/symbol", "request failed") + assertThat(event.throwableProxy.className).isEqualTo(failure.javaClass.name) + assertThat(event.throwableProxy.message).isEqualTo(failure.message) + assertThat(event.throwableProxy.stackTraceElementProxyArray).isNotEmpty() + } + + @Test + fun `cancellation and stale content produce no internal error log`() { + assertThat(queryErrors(CancellationException("Canceled by editor"))).isEmpty() + assertThat( + queryErrors(ResponseErrorException(ResponseError(ResponseErrorCode.ContentModified, "Document changed", null))), + ).isEmpty() + } + + private fun queryErrors(failure: RuntimeException): List { + val events = CopyOnWriteArrayList() + val finished = CountDownLatch(1) + val logger = LoggerFactory.getLogger(XtcTextDocumentService::class.java) as Logger + val appender = + object : AppenderBase() { + override fun append(event: ILoggingEvent) { + events += event + if (event.formattedMessage.startsWith("workspace/symbol:") && " in " in event.formattedMessage) { + finished.countDown() + } + } + }.apply { start() } + val adapter = + object : Adapter by MockAdapter() { + override fun findWorkspaceSymbols(query: String): Nothing = throw failure + } + logger.addAppender(appender) + try { + XtcLanguageServer(adapter).use { server -> + assertThatThrownBy { server.workspaceService.symbol(WorkspaceSymbolParams("Value")).get(5, SECONDS) } + .isInstanceOf(Exception::class.java) + assertThat(finished.await(5, SECONDS)).isTrue() + } + return events.filter { it.level == Level.ERROR } + } finally { + logger.detachAppender(appender) + appender.stop() + } + } +} diff --git a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt index 40da9123ac2..ca7dfe4b9d6 100644 --- a/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt +++ b/lang/lsp-server/src/test/kotlin/org/xvm/lsp/server/XdkRenameServerTest.kt @@ -14,6 +14,7 @@ import org.eclipse.lsp4j.FileOperationsWorkspaceCapabilities import org.eclipse.lsp4j.FileRename import org.eclipse.lsp4j.InitializeParams import org.eclipse.lsp4j.Position +import org.eclipse.lsp4j.PrepareRenameParams import org.eclipse.lsp4j.Range import org.eclipse.lsp4j.ReferenceContext import org.eclipse.lsp4j.ReferenceParams @@ -50,6 +51,48 @@ import java.util.concurrent.atomic.AtomicBoolean class XdkRenameServerTest { @TempDir lateinit var directory: Path + @Test + fun `every position inside a project method name prepares the same complete rename`() { + val text = + "module App { (String, String) getPassword(String? realm = Null, Boolean force = False) { " + + "return \"name\", \"password\"; } void run() { getPassword(); } }" + val root = directory.resolve("App.x").toFile().apply { writeText(text) } + val uri = root.toURI().toString() + XtcLanguageServer(XdkAdapter()).use { server -> + server.connect(mock(LanguageClient::class.java)) + server.initialize(parameters()).get(20, SECONDS) + server.replaceCompilerSourceModules(listOf(XdkSourceModule("App", uri))) + val documents = server.textDocumentService + val document = TextDocumentIdentifier(uri) + documents.didOpen(DidOpenTextDocumentParams(TextDocumentItem(uri, "xtc", 1, text))) + val name = "getPassword" + val starts = listOf(text.indexOf(name), text.lastIndexOf(name)) + starts.forEach { start -> + name.indices.forEach { offset -> + val prepared = + documents + .prepareRename( + PrepareRenameParams(document, Position(0, start + offset)), + ).get(30, SECONDS) + .second + assertThat(prepared.placeholder).isEqualTo(name) + assertThat(prepared.range).isEqualTo(Range(Position(0, start), Position(0, start + name.length))) + } + } + val edit = + requireNotNull( + documents.rename(RenameParams(document, Position(0, starts.first() + 5), "readPassword")).get(30, SECONDS), + ) + val changes = edit.documentChanges.single().left + assertThat(changes.textDocument.uri).isEqualTo(uri) + assertThat(changes.edits.map { it.left.newText }).containsExactly("readPassword", "readPassword") + assertThat(changes.edits.map { it.left.range }).containsExactlyInAnyOrderElementsOf( + starts.map { Range(Position(0, it), Position(0, it + name.length)) }, + ) + assertThat(root.readText()).isEqualTo(text) + } + } + @ParameterizedTest @CsvSource("false,false", "false,true", "true,false", "true,true") fun `late watched creation preserves a rename proof only while its inputs are unchanged`( From d1af461316879a8dd7abf22130a450b2dc0203cc Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:17 +0100 Subject: [PATCH 017/324] Restart editor-owned language servers from a project-scoped CLI Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- lang/doc/manual-test-plan.md | 2 +- lang/intellij-plugin/README.md | 19 +++ lang/intellij-plugin/build.gradle.kts | 4 + .../xtclang/idea/playbook/ParityProgress.kt | 21 +++- .../xtclang/idea/lsp/ServerControlEndpoint.kt | 115 ++++++++++++++++++ .../xtclang/idea/lsp/ServerControlStartup.kt | 35 ++++++ .../src/main/resources/META-INF/plugin.xml | 1 + .../idea/lsp/ServerControlEndpointTest.kt | 88 ++++++++++++++ lang/scripts/xtc-lsp | 112 +++++++++++++++++ lang/vscode-extension/README.md | 20 +++ lang/vscode-extension/build.gradle.kts | 1 + lang/vscode-extension/src/extension.ts | 20 +++ lang/vscode-extension/src/server-control.ts | 84 +++++++++++++ .../src/test/playbook/progress.ts | 7 +- .../src/test/suite/server-control.test.ts | 81 ++++++++++++ 15 files changed, 607 insertions(+), 3 deletions(-) create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlEndpoint.kt create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlStartup.kt create mode 100644 lang/intellij-plugin/src/test/kotlin/org/xtclang/idea/lsp/ServerControlEndpointTest.kt create mode 100755 lang/scripts/xtc-lsp create mode 100644 lang/vscode-extension/src/server-control.ts create mode 100644 lang/vscode-extension/src/test/suite/server-control.test.ts diff --git a/lang/doc/manual-test-plan.md b/lang/doc/manual-test-plan.md index d3024586ba1..64e3fc1c0b9 100644 --- a/lang/doc/manual-test-plan.md +++ b/lang/doc/manual-test-plan.md @@ -2358,7 +2358,7 @@ module Advanced { | X142 | Open configured `FoldedInitializer.x`; hover and navigate from `Int copy = value`, find references, rename `value` to `number`, then Undo. | The declaration, folded initializer and method body share one semantic identity; all three rename together, compile cleanly and restore on Undo. | | X143 | Configure `PartialSymbols.x` with 130 classes. Compare ordinary workspace symbols with a request carrying a partial-result token. | Ordered progress batches contain at most 64 symbols each; their combined names match the ordinary result exactly and the final response is empty. | | X144 | Apply a current versioned text edit through the installed client, Undo it, then send a two-document edit with one stale version. | Current edit and native Undo succeed. A stale target refuses the whole batch and preserves both current buffers. Drivers invoke the installed application handler; packaged tests separately exercise server-to-client transport. | -| X145 | Replace ProgressWork with the shared workload and request references. Verify the progress source/activity text, cancel, then check hover; repeat and restart while pending. | Cancellation terminates only the request; progress disappears. Restart preserves unsaved text, retires the pending reader and exits the old PID. Visible VS Code Cancel-control checks use 20,000 methods; automatic IntelliJ Cancel, the ordinary VS Code callback and restart use 5,000. A request finishing too early fails as unexercised. Historical model-cancellation receipts and current visible-control acceptance are distinguished in the progress and cancellation section below. | +| X145 | Replace ProgressWork with the shared workload and request references. Verify the progress source/activity text, cancel, then check hover; repeat and run `python3 lang/scripts/xtc-lsp restart --project --ide intellij` (or `vscode`) while pending. | Cancellation terminates only the request; progress disappears. The CLI asks the owning editor to restart, preserves unsaved text, retires the pending reader and exits the old PID. Visible VS Code Cancel-control checks use 20,000 methods; automatic IntelliJ Cancel, the ordinary VS Code callback and restart use 5,000. A request finishing too early fails as unexercised. Historical model-cancellation receipts and current visible-control acceptance are distinguished in the progress and cancellation section below. | | X146 | Open RefreshConsumer with inferred `var value = lib.make()`. Change only RefreshLibrary from returning Int to String, then restore it. In IntelliJ, also keep at least 16 additional documents connected and issue a burst of lens/hint/token refreshes before the dependency changes. | Both clients receive provider refreshes and show the changed inferred-type hint. Consumer text/version stays unchanged. IntelliJ reads the native cached inlay result and must report no read-action overload; its driver restores the temporary tab limit. VS Code observes the registered provider's refresh events and result. | | X147 | Hold an older settings report while requesting a newer one, changing settings, restarting or closing the settings page. Complete the old reply last. | Older reports cannot overwrite current or disposed UI state. VS Code delays a real server reply; IntelliJ drives the real settings component with controlled asynchronous report data and an EDT completion barrier. Restart retires the old PID and preserves source. | | X148 | Open the shared Extract.x fixture, select exactly `42` in the return statement, and choose Extract literal to local variable. Undo, Redo, then Undo. Repeat with a string/character literal; try selecting a call or only part of a literal. | The action inserts `val extractedValue = 42;` immediately before the return, which uses that local. Existing identifiers force a fresh suffix. Exact text, indentation and diagnostics survive Undo/Redo. Unsupported selections offer no extraction. Selected X148 acceptance passes in both editors. | diff --git a/lang/intellij-plugin/README.md b/lang/intellij-plugin/README.md index 1f4062e8f0c..e310e1dc6c0 100644 --- a/lang/intellij-plugin/README.md +++ b/lang/intellij-plugin/README.md @@ -307,6 +307,25 @@ Compiler provides semantic analysis; Tree-sitter provides syntax-based features type checking. Switching restarts the server and reopens unsaved buffers. Both adapters ship in the plugin, so switching does not require rebuilding or changing LSP4IJ. +To restart from a terminal, use the bundled `bin/xtc-lsp` script (Python 3 required), or run it +from this repository: + +```bash +python3 lang/scripts/xtc-lsp list +python3 lang/scripts/xtc-lsp restart --project ~/src/platform --ide intellij +``` + +The project must already be open in the updated plugin, with an Ecstasy file opened at least once. +The CLI asks IntelliJ to restart its server and resynchronize unsaved buffers; it does not save files +or send process signals. Omit `--ide` when only one editor has that exact project root open. If multiple +windows match, use `--session` with an ID from `list`. A failed startup is reported to the CLI; the +editor control endpoint remains available for another attempt. A timeout does not automatically retry. + +Discovery records live in `~/.xtc/editor-control`. The endpoint binds only to IPv4 loopback and requires +a per-session token. Unix records are owner-only; Windows records inherit the user-profile directory's +permissions. Records are removed on project close; stale records left by an IDE crash are ignored. +For remote IDE sessions, run the CLI on the machine hosting the IDE backend. + The default setting honors the server build's choice, normally Compiler. Development builds can still change that default. See [LSP Server README](../lsp-server/README.md) for details. diff --git a/lang/intellij-plugin/build.gradle.kts b/lang/intellij-plugin/build.gradle.kts index ca8668f2b4a..e8f0fd37542 100644 --- a/lang/intellij-plugin/build.gradle.kts +++ b/lang/intellij-plugin/build.gradle.kts @@ -608,6 +608,9 @@ val buildPlugin = // Get typed reference to IntelliJ Platform tasks val prepareSandbox = tasks.named("prepareSandbox") +prepareSandbox.configure { + from(rootProject.layout.projectDirectory.file("scripts/xtc-lsp")) { into("intellij-plugin/bin") } +} // Derive TextMate destination from prepareSandbox's output (works with any IDE version) // prepareSandbox.destinationDirectory is the plugins/ directory, we need plugins//lib/textmate @@ -683,6 +686,7 @@ val prepareJarSearchableOptions = // so its Sync task owns the complete plugin installation, including the server and grammar. val prepareRunIdeSandbox = tasks.named("prepareSandbox_runIde") { + from(rootProject.layout.projectDirectory.file("scripts/xtc-lsp")) { into("intellij-plugin/bin") } from(textMateGrammar) { into("intellij-plugin/lib/textmate") } diff --git a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt index 1a534bb40a8..215af0dacfd 100644 --- a/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt +++ b/lang/intellij-plugin/src/integrationTest/kotlin/org/xtclang/idea/playbook/ParityProgress.kt @@ -5,7 +5,9 @@ import com.intellij.driver.model.OnDispatcher import com.intellij.driver.sdk.Project import com.intellij.driver.sdk.singleProject import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode +import java.nio.file.Path import java.util.concurrent.CancellationException +import java.util.concurrent.TimeUnit.SECONDS import kotlin.time.Duration.Companion.seconds internal fun ParityScenarios.progressCases() { @@ -84,7 +86,24 @@ internal fun ParityScenarios.progressCases() { val wrapper = protocol.server() val previous = requireNotNull(wrapper.getCurrentProcessId()) val pending = start(large + "// unsaved restart\n") - wrapper.restart() + // The CLI must reach the editor, then reuse its normal restart and unsaved-buffer replay. + val script = + Path + .of(System.getProperty("xtc.playbook.scenarios")) + .parent.parent.parent + .resolve("scripts/xtc-lsp") + val project = with(driver) { singleProject().getBasePath() } + val restart = + ProcessBuilder("python3", script.toString(), "restart", "--project", project, "--ide", "intellij") + .redirectErrorStream(true) + .start() + try { + check(restart.waitFor(70, SECONDS)) { "CLI restart timed out" } + val output = restart.inputStream.bufferedReader().readText() + check(restart.exitValue() == 0) { output } + } finally { + if (restart.isAlive) restart.destroyForcibly() + } with(driver) { awaitUi("old process exits and pending request retires", 30.seconds) { pending.isDone() && !utility(ProgressUi::class).alive(previous) diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlEndpoint.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlEndpoint.kt new file mode 100644 index 00000000000..f94b0bf9195 --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlEndpoint.kt @@ -0,0 +1,115 @@ +package org.xtclang.idea.lsp + +import com.google.gson.Gson +import com.sun.net.httpserver.HttpExchange +import com.sun.net.httpserver.HttpServer +import java.net.InetSocketAddress +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.attribute.PosixFilePermissions +import java.security.MessageDigest +import java.util.UUID +import java.util.concurrent.CompletableFuture +import java.util.concurrent.Executors +import java.util.concurrent.TimeUnit.SECONDS +import java.util.concurrent.atomic.AtomicBoolean + +/** + * Editor-owned, authenticated loopback control for the CLI. The endpoint outlives server restarts, + * accepts only status/restart operations, and removes its discovery file when the project closes. + * Project paths are queried live; the token never appears in responses or logs. + */ +internal class ServerControlEndpoint( + directory: Path, + private val projects: () -> List, + private val restart: () -> CompletableFuture<*>, +) : AutoCloseable { + private val id = UUID.randomUUID().toString() + private val token = UUID.randomUUID().toString() + UUID.randomUUID().toString() + private val busy = AtomicBoolean() + private val closed = AtomicBoolean() + private val gson = Gson() + private val executor = Executors.newFixedThreadPool(2) { Thread(it, "xtc-editor-control").apply { isDaemon = true } } + private val server = HttpServer.create(InetSocketAddress("127.0.0.1", 0), 8) + private val descriptor = directory.resolve("$id.json") + + init { + try { + Files.createDirectories(directory) + require(!Files.isSymbolicLink(directory)) { "Control directory must not be a symbolic link" } + val posix = Files.getFileStore(directory).supportsFileAttributeView("posix") + if (posix) Files.setPosixFilePermissions(directory, PosixFilePermissions.fromString("rwx------")) + server.executor = executor + server.createContext("/") { exchange -> exchange.use { handle(it) } } + server.start() + val attributes = + if (posix) { + arrayOf( + PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rw-------")), + ) + } else { + emptyArray() + } + Files.createFile(descriptor, *attributes) + Files.writeString(descriptor, gson.toJson(status() + mapOf("port" to server.address.port, "token" to token))) + } catch (failure: Exception) { + close() + throw failure + } + } + + private fun status(): Map = + mapOf("version" to 1, "id" to id, "ide" to "intellij", "pid" to ProcessHandle.current().pid(), "projects" to projects()) + + private fun handle(exchange: HttpExchange) { + val authorization = exchange.requestHeaders.getFirst("Authorization").orEmpty() + if (exchange.requestHeaders.containsKey("Origin") || + !MessageDigest.isEqual(authorization.toByteArray(), "Bearer $token".toByteArray()) + ) { + reply(exchange, 403, mapOf("error" to "Unauthorized")) + return + } + when (exchange.requestMethod to exchange.requestURI.toString()) { + "GET" to "/status" -> reply(exchange, 200, status()) + + "POST" to "/restart" -> { + if (!busy.compareAndSet(false, true)) { + reply(exchange, 409, mapOf("error" to "Restart already in progress")) + return + } + var pending: CompletableFuture<*>? = null + try { + check(!closed.get()) { "Project is closing" } + pending = restart() + pending.whenComplete { _, _ -> busy.set(false) } + pending.get(60, SECONDS) + reply(exchange, 200, status() + ("message" to "Server restarted; the editor is resynchronizing open buffers.")) + } catch (failure: Exception) { + reply(exchange, 500, mapOf("error" to (failure.cause?.message ?: failure.message ?: "Restart failed"))) + } finally { + if (pending == null) busy.set(false) + } + } + + else -> reply(exchange, 404, mapOf("error" to "Unknown operation")) + } + } + + private fun reply( + exchange: HttpExchange, + code: Int, + value: Map, + ) { + val bytes = gson.toJson(value).toByteArray(Charsets.UTF_8) + exchange.responseHeaders.set("Content-Type", "application/json") + exchange.sendResponseHeaders(code, bytes.size.toLong()) + exchange.responseBody.write(bytes) + } + + override fun close() { + if (!closed.compareAndSet(false, true)) return + server.stop(0) + executor.shutdownNow() + Files.deleteIfExists(descriptor) + } +} diff --git a/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlStartup.kt b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlStartup.kt new file mode 100644 index 00000000000..3746597b48a --- /dev/null +++ b/lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/ServerControlStartup.kt @@ -0,0 +1,35 @@ +package org.xtclang.idea.lsp + +import com.intellij.openapi.diagnostic.logger +import com.intellij.openapi.project.Project +import com.intellij.openapi.startup.ProjectActivity +import com.intellij.openapi.util.Disposer +import com.redhat.devtools.lsp4ij.LanguageServiceAccessor +import java.nio.file.Path +import java.util.concurrent.CompletableFuture + +/** Keep CLI control with the project, so a failed language-server process can still be restarted. */ +class ServerControlStartup : ProjectActivity { + override suspend fun execute(project: Project) { + if (project.isDefault || project.isDisposed || project.basePath == null) return + try { + val control = + ServerControlEndpoint( + Path.of(System.getProperty("user.home"), ".xtc", "editor-control"), + { listOf(Path.of(requireNotNull(project.basePath)).toRealPath().toString()) }, + ) { + check(!project.isDisposed) { "Project is closing" } + val servers = + LanguageServiceAccessor.getInstance(project).startedServers.filter { + it.serverDefinition.id == CompilerSettings.SERVER_ID + } + check(servers.isNotEmpty()) { "Open an Ecstasy file in this project before restarting its server" } + servers.forEach { it.restart() } + CompletableFuture.allOf(*servers.map { it.initializedServer }.toTypedArray()) + } + Disposer.register(project) { control.close() } + } catch (failure: Exception) { + logger().warn("Ecstasy CLI control unavailable", failure) + } + } +} diff --git a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml index 19c7de497b1..0139d659f3e 100644 --- a/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml +++ b/lang/intellij-plugin/src/main/resources/META-INF/plugin.xml @@ -55,6 +55,7 @@ + () + ServerControlEndpoint(directory, { listOf("/project") }) { + calls.incrementAndGet() + entered.countDown() + pending + }.use { + val file = Files.list(directory).use { it.findFirst().orElseThrow() } + val descriptor = JsonParser.parseString(Files.readString(file)).asJsonObject + val client = HttpClient.newHttpClient() + val base = "http://127.0.0.1:${descriptor["port"].asInt}" + val token = descriptor["token"].asString + + fun request( + route: String, + method: String = "GET", + credential: String = token, + ): HttpRequest = + HttpRequest + .newBuilder(URI.create(base + route)) + .header("Authorization", "Bearer $credential") + .method(method, HttpRequest.BodyPublishers.noBody()) + .build() + assertThat(client.send(request("/restart", "POST", "wrong"), BodyHandlers.ofString()).statusCode()).isEqualTo(403) + assertThat(client.send(request("/restart"), BodyHandlers.ofString()).statusCode()).isEqualTo(404) + val status = client.send(request("/status"), BodyHandlers.ofString()) + assertThat(status.statusCode()).isEqualTo(200) + assertThat(status.body()).contains("/project", "intellij").doesNotContain(token) + assertThat(calls.get()).isZero() + val restart = client.sendAsync(request("/restart", "POST"), BodyHandlers.ofString()) + assertThat(entered.await(5, SECONDS)).isTrue() + assertThat(restart).isNotDone() + assertThat(client.send(request("/restart", "POST"), BodyHandlers.ofString()).statusCode()).isEqualTo(409) + pending.complete(null) + assertThat(restart.get(5, SECONDS).statusCode()).isEqualTo(200) + assertThat(calls.get()).isEqualTo(1) + } + assertThat(Files.list(directory).use { it.count() }).isZero() + } + + @Test + fun `failed restart reports failure and does not stop the control endpoint`() { + ServerControlEndpoint(directory, { listOf("/project") }) { + CompletableFuture.failedFuture(IllegalStateException("Launch failed")) + }.use { + val descriptor = + Files + .list( + directory, + ).use { JsonParser.parseString(Files.readString(it.findFirst().orElseThrow())).asJsonObject } + val client = HttpClient.newHttpClient() + val builder = + HttpRequest + .newBuilder(URI.create("http://127.0.0.1:${descriptor["port"].asInt}/restart")) + .header("Authorization", "Bearer ${descriptor["token"].asString}") + .POST(HttpRequest.BodyPublishers.noBody()) + repeat(2) { + val response = client.send(builder.build(), BodyHandlers.ofString()) + assertThat(response.statusCode()).isEqualTo(500) + assertThat(response.body()).contains("Launch failed") + } + } + } +} diff --git a/lang/scripts/xtc-lsp b/lang/scripts/xtc-lsp new file mode 100755 index 00000000000..f3364b63ca4 --- /dev/null +++ b/lang/scripts/xtc-lsp @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""Restart an editor-owned Ecstasy language server without discarding unsaved buffers.""" + +import argparse +import http.client +import json +import os +from pathlib import Path +import stat +import sys + + +def request(session, operation, timeout): + """Contact only authenticated IPv4 loopback endpoints, without proxy or redirect handling.""" + port = session.get("port") + if type(port) is not int or not 1 <= port <= 65535: + raise ValueError("Invalid control port") + token = session.get("token") + if not isinstance(token, str) or not token or len(token) > 256: + raise ValueError("Invalid control token") + connection = http.client.HTTPConnection("127.0.0.1", port, timeout=timeout) + try: + connection.request("POST" if operation == "restart" else "GET", f"/{operation}", + headers={"Authorization": f"Bearer {token}"}) + response = connection.getresponse() + body = response.read(65537) + if len(body) > 65536: + raise ValueError("Oversized control response") + result = json.loads(body) + if response.status != 200: + raise ValueError(result.get("error", f"Control request failed: HTTP {response.status}")) + if result.get("version") != 1 or result.get("id") != session.get("id"): + raise ValueError("Control session changed; list sessions again") + return result + finally: + connection.close() + + +def sessions(directory): + """Ignore stale discovery records; a live authenticated response establishes session identity.""" + if not directory.exists(): + return [] + if directory.is_symlink(): + raise ValueError("Control directory must not be a symbolic link") + if os.name == "posix": + info = directory.stat() + if info.st_uid != os.getuid() or info.st_mode & 0o077: + raise ValueError("Control directory must be owned by you and accessible only to you") + live = [] + for file in sorted(directory.glob("*.json")): + try: + info = file.lstat() + if not stat.S_ISREG(info.st_mode) or info.st_size > 65536: + continue + if os.name == "posix" and (info.st_uid != os.getuid() or info.st_mode & 0o077): + continue + descriptor = json.loads(file.read_text(encoding="utf-8")) + if not isinstance(descriptor, dict) or descriptor.get("version") != 1: + continue + current = request(descriptor, "status", 1) + projects = current.get("projects") + if current.get("ide") not in ("intellij", "vscode") or not isinstance(projects, list): + continue + if not all(isinstance(project, str) for project in projects): + continue + live.append({**descriptor, **current}) + except (OSError, ValueError, TypeError, AttributeError, http.client.HTTPException): + continue + return live + + +def describe(session): + return f"{session['id']} {session['ide']} " + ", ".join(session["projects"]) + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + commands = parser.add_subparsers(dest="command", required=True) + commands.add_parser("list", help="List live editor sessions (tokens are never printed)") + restart = commands.add_parser("restart", help="Restart through the IDE and restore its open buffers") + restart.add_argument("--project", type=Path, default=Path.cwd(), help="Exact workspace/project root (default: current directory)") + restart.add_argument("--ide", choices=("intellij", "vscode"), help="Select an editor when both have the project open") + restart.add_argument("--session", help="Select an exact session ID from 'list'") + restart.add_argument("--timeout", type=float, default=65, help="Seconds to wait for restart (default: 65)") + args = parser.parse_args() + try: + directory = Path(os.environ.get("XTC_EDITOR_CONTROL_DIR", Path.home() / ".xtc/editor-control")) + live = sessions(directory) + if args.command == "list": + print("\n".join(map(describe, live)) if live else "No live Ecstasy editor sessions.") + return 0 + if not 0 < args.timeout <= 600: + raise ValueError("--timeout must be greater than zero and at most 600 seconds") + project = os.path.normcase(str(args.project.resolve())) + matches = [session for session in live + if project in [os.path.normcase(os.path.realpath(root)) for root in session["projects"]] + and (args.ide is None or args.ide == session["ide"]) + and (args.session is None or args.session == session["id"])] + if not matches: + raise ValueError(f"No live Ecstasy editor session matches {args.project.resolve()}. Open the project and run 'xtc-lsp list'.") + if len(matches) != 1: + raise ValueError("Multiple editors match; select --ide or --session:\n" + "\n".join(map(describe, matches))) + result = request(matches[0], "restart", args.timeout) + print(f"{matches[0]['ide']}: {result['message']}") + return 0 + except (OSError, ValueError, KeyError, http.client.HTTPException) as error: + print(f"xtc-lsp: {error}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/lang/vscode-extension/README.md b/lang/vscode-extension/README.md index 5247931b23b..7d633b95147 100644 --- a/lang/vscode-extension/README.md +++ b/lang/vscode-extension/README.md @@ -396,6 +396,26 @@ semantic analysis or **Tree-sitter** for syntax-based features without compiler The choice is stored in the window-scoped `xtc.languageService.adapter` setting; changing it restarts the server and reopens unsaved buffers. The status bar shows the running adapter. +The extension also bundles `bin/xtc-lsp`, a Python 3 CLI for restarting through the owning editor. +From this repository, run: + +```bash +python3 lang/scripts/xtc-lsp list +python3 lang/scripts/xtc-lsp restart --project ~/src/platform --ide vscode +``` + +The updated extension must be active in that workspace. The CLI preserves unsaved buffers and uses +the existing client restart lifecycle. `--project` matches an exact workspace-folder root; in a +multi-root window, any root selects that window's shared server. Omit `--ide` when the match is unique, +or select `--session` using an ID from `list` when several windows have the same project open. +Failures return a nonzero exit code; timeouts never trigger an automatic retry. + +Control remains available after a language-server crash. Each editor session publishes a private token +under `~/.xtc/editor-control` and accepts authenticated requests only on IPv4 loopback. Unix records +are owner-only; Windows records inherit the user-profile directory's permissions. Closing the window +removes its record; stale records from crashed editors are ignored. For SSH, containers or WSL, run +the CLI where the extension host runs. + **Bundled default** (the default setting) honors the server build's choice, normally Compiler. Both real adapters are included, so switching does not require rebuilding. Development builds can still change the bundled default: diff --git a/lang/vscode-extension/build.gradle.kts b/lang/vscode-extension/build.gradle.kts index 7c8238d8fdd..058ab4836ad 100644 --- a/lang/vscode-extension/build.gradle.kts +++ b/lang/vscode-extension/build.gradle.kts @@ -183,6 +183,7 @@ val stagePackage = from(listOf(copyLspServer, copyDapServer)) { into("server") } from(generateIcons) { into("icons") } from(npmBundle) { into("out") } + from(rootProject.layout.projectDirectory.file("scripts/xtc-lsp")) { into("bin") } into(packageDirectory) val stagedManifest = packageDirectory.map { it.file("package.json") } diff --git a/lang/vscode-extension/src/extension.ts b/lang/vscode-extension/src/extension.ts index bf18d7bce90..b54ceb15811 100644 --- a/lang/vscode-extension/src/extension.ts +++ b/lang/vscode-extension/src/extension.ts @@ -24,6 +24,7 @@ import { runtimeJvmOptions, runtimeLogArguments } from './runtime-settings'; import { configuredSettings, configurationProperties } from './settings-report'; import { supportLogs } from './support-logs'; import { serviceFailure } from './service-notifications'; +import { ServerControlEndpoint } from './server-control'; function ensureXtcLanguageAssociation(document: vscode.TextDocument): void { if (document.fileName.endsWith('.x') && document.languageId !== 'xtc') { @@ -120,6 +121,25 @@ export function activate(context: vscode.ExtensionContext): void { const serverJar = context.asAbsolutePath(path.join('server', 'lsp-server.jar')); const serverExists = fs.existsSync(serverJar); + // Keep control in the extension host, including when the language-server process has failed. + let controlClosed = false; + let control: ServerControlEndpoint | undefined; + context.subscriptions.push({ dispose: () => { + controlClosed = true; + void control?.close().catch(error => outputChannel.error(`CLI control cleanup failed: ${error}`)); + } }); + void ServerControlEndpoint.start(path.join(os.homedir(), '.xtc', 'editor-control'), + () => (vscode.workspace.workspaceFolders ?? []).filter(folder => folder.uri.scheme === 'file') + .map(folder => fs.realpathSync(folder.uri.fsPath)), + async () => { + if (controlClosed) throw new Error('Workspace is closing'); + if (!serverExists) throw new Error('Ecstasy Language Server JAR not found. Build the extension first.'); + await restartLanguageClient(context, serverJar, outputChannel); + }).then(async endpoint => { + control = endpoint; + if (controlClosed) await endpoint.close(); + }).catch(error => outputChannel.error(`CLI control unavailable: ${error}`)); + // Register LSP-related commands const effectiveOutput = vscode.window.createOutputChannel('Ecstasy Effective Configuration'); // Owned by this UI command on the extension event loop, never by compiler callbacks. diff --git a/lang/vscode-extension/src/server-control.ts b/lang/vscode-extension/src/server-control.ts new file mode 100644 index 00000000000..0428d0f50bc --- /dev/null +++ b/lang/vscode-extension/src/server-control.ts @@ -0,0 +1,84 @@ +import * as fs from 'node:fs/promises'; +import * as http from 'node:http'; +import * as path from 'node:path'; +import { randomUUID, timingSafeEqual } from 'node:crypto'; + +/** Editor-owned CLI endpoint: survives server crashes and delegates restart to the existing client lifecycle. */ +export class ServerControlEndpoint { + private readonly id = randomUUID(); + private readonly token = randomUUID() + randomUUID(); + private readonly server: http.Server; + private readonly descriptor: string; + private busy = false; + private closed = false; + + private constructor(directory: string, private readonly projects: () => string[], private readonly restart: () => Promise) { + this.descriptor = path.join(directory, `${this.id}.json`); + this.server = http.createServer((request, response) => { + void this.handle(request, response).catch(() => response.destroy()); + }); + this.server.requestTimeout = 5000; + this.server.headersTimeout = 5000; + } + + static async start(directory: string, projects: () => string[], restart: () => Promise): Promise { + await fs.mkdir(directory, { recursive: true, mode: 0o700 }); + if ((await fs.lstat(directory)).isSymbolicLink()) throw new Error('Control directory must not be a symbolic link'); + if (process.platform !== 'win32') await fs.chmod(directory, 0o700); + const control = new ServerControlEndpoint(directory, projects, restart); + try { + await new Promise((resolve, reject) => { + control.server.once('error', reject); + control.server.listen(0, '127.0.0.1', resolve); + }); + const address = control.server.address(); + if (!address || typeof address === 'string') throw new Error('Control server has no TCP port'); + await fs.writeFile(control.descriptor, JSON.stringify({ ...control.status(), port: address.port, token: control.token }), + { flag: 'wx', mode: 0o600 }); + return control; + } catch (error) { + await control.close(); + throw error; + } + } + + private status(): object { + return { version: 1, id: this.id, ide: 'vscode', pid: process.pid, projects: this.projects() }; + } + + private async handle(request: http.IncomingMessage, response: http.ServerResponse): Promise { + const actual = Buffer.from(request.headers.authorization ?? ''); + const expected = Buffer.from(`Bearer ${this.token}`); + const reply = (code: number, value: object) => { + response.writeHead(code, { 'Content-Type': 'application/json' }); + response.end(JSON.stringify(value)); + }; + if (request.headers.origin !== undefined || actual.length !== expected.length || !timingSafeEqual(actual, expected)) { + reply(403, { error: 'Unauthorized' }); + return; + } + if (request.method === 'GET' && request.url === '/status') { + reply(200, this.status()); + } else if (request.method === 'POST' && request.url === '/restart') { + if (this.busy) { reply(409, { error: 'Restart already in progress' }); return; } + this.busy = true; + try { + if (this.closed) throw new Error('Workspace is closing'); + await this.restart(); + reply(200, { ...this.status(), message: 'Server restarted; the editor is resynchronizing open buffers.' }); + } catch (error) { + reply(500, { error: error instanceof Error ? error.message : String(error) }); + } finally { this.busy = false; } + } else { + reply(404, { error: 'Unknown operation' }); + } + } + + async close(): Promise { + if (this.closed) return; + this.closed = true; + this.server.closeAllConnections(); + await new Promise(resolve => this.server.close(() => resolve())); + await fs.rm(this.descriptor, { force: true }); + } +} diff --git a/lang/vscode-extension/src/test/playbook/progress.ts b/lang/vscode-extension/src/test/playbook/progress.ts index 94031161a61..d8460d3e860 100644 --- a/lang/vscode-extension/src/test/playbook/progress.ts +++ b/lang/vscode-extension/src/test/playbook/progress.ts @@ -1,4 +1,7 @@ import * as assert from 'node:assert'; +import { execFile } from 'node:child_process'; +import * as path from 'node:path'; +import { promisify } from 'node:util'; import * as vscode from 'vscode'; import { LSPErrorCodes, ProgressType, WorkDoneProgressBegin, WorkDoneProgressEnd, WorkDoneProgressReport } from 'vscode-languageclient/node'; import { client, eventually, hover, noErrors, playbook } from './support'; @@ -96,7 +99,9 @@ export function progressCases(): void { await noErrors(document.uri); const before = await client().sendRequest<{ pid: number }>('xtc/languageServiceStatus', {}); const pending = await start(large + '// unsaved restart\n'); - await vscode.commands.executeCommand('xtc.restartServer'); + const script = path.resolve(__dirname, '../../../../scripts/xtc-lsp'); + await promisify(execFile)('python3', [script, 'restart', '--project', vscode.workspace.workspaceFolders![0].uri.fsPath, + '--ide', 'vscode'], { timeout: 70000 }); assert.ok((await pending.outcome).error, 'Restart retires the pending reader'); await eventually(async () => { try { process.kill(before.pid, 0); return false; } catch { return true; } }, value => value, 'Old server process exits'); diff --git a/lang/vscode-extension/src/test/suite/server-control.test.ts b/lang/vscode-extension/src/test/suite/server-control.test.ts new file mode 100644 index 00000000000..ed3f79f9c15 --- /dev/null +++ b/lang/vscode-extension/src/test/suite/server-control.test.ts @@ -0,0 +1,81 @@ +import * as assert from 'node:assert'; +import { execFile } from 'node:child_process'; +import * as fs from 'node:fs/promises'; +import * as os from 'node:os'; +import * as path from 'node:path'; +import { promisify } from 'node:util'; +import { ServerControlEndpoint } from '../../server-control'; + +/** Exercise the actual CLI against real endpoints, including ambiguous windows and restart failures. */ +suite('Editor-owned server control', () => { + const execute = promisify(execFile); + const script = path.resolve(__dirname, '../../../../scripts/xtc-lsp'); + let directory: string; + const controls: ServerControlEndpoint[] = []; + setup(async () => { directory = await fs.mkdtemp(path.join(os.tmpdir(), 'xtc-control-')); }); + teardown(async () => { + await Promise.all(controls.splice(0).map(control => control.close())); + await fs.rm(directory, { recursive: true, force: true }); + }); + const cli = (...args: string[]) => execute('python3', [script, ...args], { + env: { ...process.env, XTC_EDITOR_CONTROL_DIR: directory }, timeout: 10000 + }); + + test('exact project routing refuses ambiguity and ignores stale records', async () => { + let first = 0, second = 0; + const project = path.join(directory, 'project'); + controls.push(await ServerControlEndpoint.start(directory, () => [project], async () => { first++; })); + await fs.writeFile(path.join(directory, 'stale.json'), '{broken'); + await cli('restart', '--project', project); + assert.strictEqual(first, 1); + controls.push(await ServerControlEndpoint.start(directory, () => [project], async () => { second++; })); + await assert.rejects(cli('restart', '--project', project), /Multiple editors match/); + await assert.rejects(cli('restart', '--project', path.join(directory, 'other')), /No live Ecstasy editor session/); + assert.strictEqual(first, 1); + assert.strictEqual(second, 0); + const listing = (await cli('list')).stdout; + const id = listing.trim().split(/\s+/)[0]; + await cli('restart', '--project', project, '--session', id); + assert.strictEqual(first + second, 2); + for (const file of (await fs.readdir(directory)).filter(file => file !== 'stale.json')) { + const descriptor = JSON.parse(await fs.readFile(path.join(directory, file), 'utf8')); + assert.ok(!listing.includes(descriptor.token)); + if (process.platform !== 'win32') assert.strictEqual((await fs.stat(path.join(directory, file))).mode & 0o077, 0); + } + }); + + test('authentication, operation and busy checks precede restart, and disposal removes discovery', async () => { + let calls = 0; + let complete!: () => void; + const pending = new Promise(resolve => { complete = resolve; }); + const control = await ServerControlEndpoint.start(directory, () => ['/project'], async () => { calls++; await pending; }); + controls.push(control); + const file = path.join(directory, (await fs.readdir(directory))[0]); + const descriptor = JSON.parse(await fs.readFile(file, 'utf8')); + const url = `http://127.0.0.1:${descriptor.port}`; + const headers = { Authorization: `Bearer ${descriptor.token}` }; + assert.strictEqual((await fetch(`${url}/restart`, { method: 'POST' })).status, 403); + assert.strictEqual((await fetch(`${url}/restart`, { headers })).status, 404); + assert.strictEqual(calls, 0); + const restart = fetch(`${url}/restart`, { method: 'POST', headers }); + const deadline = Date.now() + 5000; + while (!calls && Date.now() < deadline) await new Promise(resolve => setTimeout(resolve, 5)); + assert.strictEqual(calls, 1); + assert.strictEqual((await fetch(`${url}/restart`, { method: 'POST', headers })).status, 409); + complete(); + assert.strictEqual((await restart).status, 200); + assert.strictEqual(calls, 1); + await control.close(); + assert.deepStrictEqual(await fs.readdir(directory), []); + }); + + test('restart failures reach the CLI and the endpoint remains usable', async () => { + let fail = true; + controls.push(await ServerControlEndpoint.start(directory, () => ['/project'], async () => { + if (fail) throw new Error('Launch failed'); + })); + await assert.rejects(cli('restart', '--project', '/project'), /Launch failed/); + fail = false; + await cli('restart', '--project', '/project'); + }); +}); From e0f5f8d80bfcfc24f6182cbbec2918bcab5e65c4 Mon Sep 17 00:00:00 2001 From: Marcus Lagergren <1062473+lagergren@users.noreply.github.com> Date: Wed, 28 Oct 2026 15:55:20 +0100 Subject: [PATCH 018/324] Group source bindings, clean up the compiler AST and share duplicated code - Group compiler source bindings in their own package. - Update parameter description for TestRunner constructor. - Correct comment formatting in ParserTest. - Apply the Java whitespace rules to lines this branch changes. - Clean up AstNode type and nullability warnings. - Keep partial argument replacement on one line. - Clarify AST traversal and resolution comments. - Clean up compiler AST validation and emission. - dedup: Remove AST regressions duplicated by master. - dedup: Remove duplicated blocks from WhileStatement. - dedup: Render statement condition lists with one shared idiom. - dedup: Share code that the LSP branch duplicated. - dedup: Share loop label variables and continue handling. Validation: the touched builds compile. The suites and the playbooks ran on the last commit of this series. --- javatools/LSP-COMPILER.md | 58 +++- javatools/README.md | 5 + .../java/org/xvm/api/EmbeddingSupport.java | 22 +- .../main/java/org/xvm/api/ModuleCompiler.java | 14 +- .../main/java/org/xvm/asm/FileRepository.java | 8 +- .../org/xvm/asm/constants/TypeConstant.java | 2 +- .../main/java/org/xvm/compiler/Compiler.java | 2 + .../main/java/org/xvm/compiler/Parser.java | 38 +-- .../compiler/ast/AnnotationExpression.java | 6 + .../compiler/ast/AnonymousClassBindings.java | 54 ---- .../org/xvm/compiler/ast/AssertStatement.java | 7 +- .../java/org/xvm/compiler/ast/AstNode.java | 112 ++++---- .../java/org/xvm/compiler/ast/BindingAst.java | 36 +++ .../org/xvm/compiler/ast/CaseManager.java | 112 +++----- .../org/xvm/compiler/ast/CaseStatement.java | 2 +- .../xvm/compiler/ast/CmpChainExpression.java | 155 ++++------ .../org/xvm/compiler/ast/CmpExpression.java | 23 +- .../xvm/compiler/ast/ComponentStatement.java | 25 ++ .../org/xvm/compiler/ast/CompositionNode.java | 15 +- .../xvm/compiler/ast/CondOpExpression.java | 1 + .../java/org/xvm/compiler/ast/Context.java | 219 +++++--------- .../java/org/xvm/compiler/ast/Expression.java | 134 ++++----- .../org/xvm/compiler/ast/FileExpression.java | 16 +- .../xvm/compiler/ast/ForEachStatement.java | 80 ++---- .../org/xvm/compiler/ast/ForStatement.java | 186 ++---------- .../compiler/ast/FunctionTypeExpression.java | 2 +- .../org/xvm/compiler/ast/GotoStatement.java | 1 + .../org/xvm/compiler/ast/IfStatement.java | 70 +---- .../org/xvm/compiler/ast/ImportStatement.java | 8 +- .../compiler/ast/InvocationExpression.java | 14 +- .../org/xvm/compiler/ast/IsExpression.java | 47 +-- .../org/xvm/compiler/ast/LambdaBindings.java | 76 ----- .../xvm/compiler/ast/LambdaExpression.java | 48 ++-- .../org/xvm/compiler/ast/ListExpression.java | 24 +- .../xvm/compiler/ast/LiteralExpression.java | 19 +- .../org/xvm/compiler/ast/LoopStatement.java | 124 ++++++++ .../org/xvm/compiler/ast/MapExpression.java | 26 +- .../ast/MethodDeclarationStatement.java | 54 +--- .../compiler/ast/ModuleTypeExpression.java | 2 +- .../compiler/ast/MultipleLValueStatement.java | 2 +- .../org/xvm/compiler/ast/NameExpression.java | 23 +- .../org/xvm/compiler/ast/NameResolver.java | 24 +- .../xvm/compiler/ast/NamedTypeExpression.java | 8 +- .../org/xvm/compiler/ast/NewExpression.java | 270 ++++++++---------- .../xvm/compiler/ast/NotNullExpression.java | 27 +- .../org/xvm/compiler/ast/PackExpression.java | 1 + .../java/org/xvm/compiler/ast/PartialAst.java | 1 + .../xvm/compiler/ast/PrefixExpression.java | 15 + .../ast/PropertyDeclarationStatement.java | 41 +-- .../org/xvm/compiler/ast/RelOpExpression.java | 38 ++- .../org/xvm/compiler/ast/ReturnStatement.java | 22 +- .../ast/SequentialAssignExpression.java | 3 - .../java/org/xvm/compiler/ast/StageMgr.java | 4 +- .../java/org/xvm/compiler/ast/Statement.java | 63 +++- .../org/xvm/compiler/ast/StatementBlock.java | 34 +-- .../xvm/compiler/ast/StatementExpression.java | 6 +- .../xvm/compiler/ast/SwitchExpression.java | 8 +- .../org/xvm/compiler/ast/SwitchStatement.java | 28 +- .../org/xvm/compiler/ast/ThrowExpression.java | 29 +- .../org/xvm/compiler/ast/TryStatement.java | 9 +- .../ast/TypeCompositionStatement.java | 112 ++++---- .../ast/UnaryComplementExpression.java | 13 +- .../compiler/ast/UnaryMinusExpression.java | 13 +- .../ast/VariableDeclarationStatement.java | 23 +- .../org/xvm/compiler/ast/VersionOverride.java | 2 +- .../org/xvm/compiler/ast/WhileStatement.java | 244 +++++----------- .../ast/bindings/AnonymousClassBindings.java | 93 ++++++ .../{ => ast/bindings}/CursorBinding.java | 3 +- .../bindings}/InitializerBinding.java | 3 +- .../{ => ast/bindings}/InvocationBinding.java | 2 +- .../compiler/ast/bindings/LambdaBindings.java | 99 +++++++ .../xvm/compiler/ast/partial/CursorScope.java | 5 +- .../compiler/ast/partial/PartialArgument.java | 3 +- .../ast/partial/PartialCallResolver.java | 4 +- .../partial/PartialConstructionResolver.java | 2 +- .../compiler/ast/partial/PartialQueries.java | 2 +- .../main/java/org/xvm/tool/ModuleInfo.java | 73 +++-- .../main/java/org/xvm/tool/TestRunner.java | 2 +- .../xvm/api/BindingIdentitySnapshotTest.java | 4 +- .../java/org/xvm/compiler/ParserTest.java | 2 +- .../ast/CompilerCounterAtomicTest.java | 27 ++ .../{ => ast/bindings}/CursorBindingTest.java | 2 +- .../xtclang/idea/lsp/XtcFileRenameHandler.kt | 19 +- .../org/xtclang/idea/lsp/XtcRenameDialog.kt | 37 +++ .../org/xtclang/idea/lsp/XtcRenameEdit.kt | 155 +++++----- .../org/xtclang/idea/lsp/XtcRenameHandler.kt | 18 +- .../lsp/adapter/xdk/SemanticModelBuilder.kt | 4 +- .../org/xvm/lsp/adapter/xdk/XdkAdapter.kt | 107 +++---- .../xvm/lsp/adapter/xdk/XdkColorPrototype.kt | 2 +- .../xvm/lsp/adapter/XdkPartialAnalysisTest.kt | 2 +- manualTests/build.gradle.kts | 1 + manualTests/src/main/x/astExpressions.x | 92 ++++++ 92 files changed, 1704 insertions(+), 1969 deletions(-) delete mode 100644 javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/BindingAst.java delete mode 100644 javatools/src/main/java/org/xvm/compiler/ast/LambdaBindings.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/LoopStatement.java create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/bindings/AnonymousClassBindings.java rename javatools/src/main/java/org/xvm/compiler/{ => ast/bindings}/CursorBinding.java (99%) rename javatools/src/main/java/org/xvm/compiler/{ => ast/bindings}/InitializerBinding.java (98%) rename javatools/src/main/java/org/xvm/compiler/{ => ast/bindings}/InvocationBinding.java (99%) create mode 100644 javatools/src/main/java/org/xvm/compiler/ast/bindings/LambdaBindings.java rename javatools/src/test/java/org/xvm/compiler/{ => ast/bindings}/CursorBindingTest.java (99%) create mode 100644 lang/intellij-plugin/src/main/kotlin/org/xtclang/idea/lsp/XtcRenameDialog.kt create mode 100644 manualTests/src/main/x/astExpressions.x diff --git a/javatools/LSP-COMPILER.md b/javatools/LSP-COMPILER.md index 211e99ea86f..f2b60fadb52 100644 --- a/javatools/LSP-COMPILER.md +++ b/javatools/LSP-COMPILER.md @@ -592,6 +592,27 @@ separate policies, and the adapter still checks input revisions before publishin ## Preserving semantic facts from successful validation +### Keep binding facts and their writers together + +`org.xvm.compiler.ast.bindings` groups `InvocationBinding`, `InitializerBinding`, `CursorBinding`, +`LambdaBindings` and `AnonymousClassBindings` alongside the `ast.partial` analysis package. These +classes describe established source associations or inspected cursor candidates; the ordinary AST +still owns validation and register allocation. The LSP consumes the resulting facts through the +embedding results and source-node accessors. + +Lambda and anonymous-class bindings expose read-only accessors that return immutable collection +snapshots. Their nested `Collector` classes provide the compiler-facing recording operations and +retain the method/construction identity used to reject facts requested through a copied node. +Each writer stays with its existing validation context or construction. It does not introduce a +global cache, a second capture pass or a new lifecycle for the AST. + +The [BindingAst](src/main/java/org/xvm/compiler/ast/BindingAst.java) bridge has one operation: +associate an existing lambda parameter register with its written declaration and return the name +token. That operation needs the AST's protected parameter lists and package-private binding setter. +Keeping it in `ast` preserves their visibility. The other binding classes use public source +accessors and require no additional AST bridge. The collector and bridge APIs are public for Java +cross-package access, but are internal compiler operations; embedding hosts read published facts. + ### Names, declarations and source identity The source AST carries locations that assembled components alone cannot provide. Tooling accessors @@ -617,7 +638,7 @@ They do not define colors in the compiler or introduce a public graphics/color l ### Capture selected calls before rewrites obscure them -[InvocationBinding.java](src/main/java/org/xvm/compiler/InvocationBinding.java) records a selected +[InvocationBinding.java](src/main/java/org/xvm/compiler/ast/bindings/InvocationBinding.java) records a selected method identity, its instantiated signature and the association between written argument spans and visible parameter indices. Named-argument labels retain their written ranges. Synthetic default arguments are not presented as arguments the user wrote. If a rewrite prevents a reliable @@ -648,7 +669,7 @@ spelling as references to one declaration. ### Keep references that constant folding would discard Successful constant-initializer compilation can discard the temporary method used to validate an -initializer. [InitializerBinding.java](src/main/java/org/xvm/compiler/InitializerBinding.java) +initializer. [InitializerBinding.java](src/main/java/org/xvm/compiler/ast/bindings/InitializerBinding.java) captures source references, expression types and selected calls before that method disappears, and anchors them to the surviving source property. @@ -659,9 +680,9 @@ registers or mutable tokens. The adapter then copies the needed identities into ### Relate generated implementation details to written source -[LambdaBindings.java](src/main/java/org/xvm/compiler/ast/LambdaBindings.java) distinguishes written +[LambdaBindings.java](src/main/java/org/xvm/compiler/ast/bindings/LambdaBindings.java) distinguishes written lambda parameters from synthetic capture parameters and maps capture registers back to their -source registers. [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java) +source registers. [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/bindings/AnonymousClassBindings.java) provides the corresponding origin information for anonymous-class captures. A captured local must still rename as the same source variable even when generated methods or properties access it. @@ -729,7 +750,7 @@ placeholder types left by failed validation are not semantic proof. ### Export scope and candidate facts while the context exists -[CursorBinding.java](src/main/java/org/xvm/compiler/CursorBinding.java) captures narrowed local +[CursorBinding.java](src/main/java/org/xvm/compiler/ast/bindings/CursorBinding.java) captures narrowed local types and readability, the enclosing receiver, visible type names, method/function candidates, written argument mappings and compiler-fitted insertion proposals. Written formals can hide outer names without being assigned fictitious completed declaration identities. @@ -906,6 +927,10 @@ should not be mistaken for an LSP wire-format requirement. ### Java result records and nullable progress +The five source-binding classes now live in `org.xvm.compiler.ast.bindings`. Java/Kotlin consumers +must update imports and recompile; moving a public class changes its binary name. This is part of +the planned embedding API migration, not a change to emitted Ecstasy code or the `.xtc` format. + Retained convenience constructors do not preserve a record's old deconstruction shape. `CursorBinding` has fourteen components; Java clients using record patterns must adapt to the current shape even where an older constructor descriptor remains available. Prefer accessors @@ -1025,8 +1050,9 @@ that every suite has just been rerun. Current CI results and their artifacts pro ## Appendix: file-by-file compiler changes This inventory records the `javatools` portion of the full implementation review when this guide -was added: **147 files (97 modified and 50 added)**, including this document. It -describes what changed in each file and why, rather than restating everything that file does. +was added and updated for binding-package organization: **148 files (97 modified and 51 added)**, +including this document. It describes what changed in each file and why, rather than restating +everything that file does. Supporting changes already in the review's base and changes outside `javatools` are discussed in the main guide but are not counted again here. @@ -1082,21 +1108,28 @@ source or document in this checkout. | File | Change | What changed and why | | --- | --- | --- | | [Compiler.java](src/main/java/org/xvm/compiler/Compiler.java) | Modified | Owns a final listener and optional invocation/cursor collectors and passes them through compiler stages. Registration recognizes host cancellation without inventing an error or retaining a listener on the file structure. | -| [CursorBinding.java](src/main/java/org/xvm/compiler/CursorBinding.java) | Added | Defines immutable collections of scope, type, candidate-call and argument-proposal facts for an explicit cursor query. Its collector publishes inspected source sites without presenting candidates as selected executable calls. | | [EvalCompiler.java](src/main/java/org/xvm/compiler/EvalCompiler.java) | Modified | Creates its diagnostic buffer at construction so hosts can inspect errors before lambda compilation, with an explicit first-error policy. | -| [InitializerBinding.java](src/main/java/org/xvm/compiler/InitializerBinding.java) | Added | Captures source references, types and call facts before successful constant folding discards an initializer's temporary method; excludes validation contexts and disposable AST identities from the published provenance. | -| [InvocationBinding.java](src/main/java/org/xvm/compiler/InvocationBinding.java) | Added | Records selected methods, function types, constructors and source-to-parameter argument associations. Attempt-owned collectors discard stale/trial facts and publish maps keyed by source-node identity. | | [Lexer.java](src/main/java/org/xvm/compiler/Lexer.java) | Modified | Uses explicit sites/listeners and checks cancellation even while reading valid tokens. Unterminated string handling stops at EOF instead of repeatedly reporting when no error budget stops it. | | [Parser.java](src/main/java/org/xvm/compiler/Parser.java) | Modified | Replaces implicit lookahead listener swapping with scoped attempts; adds bounded syntax recovery and opt-in cursor/header parsing while preserving written positions and later declarations. Unrelated parse errors still block semantic compilation. | | [Token.java](src/main/java/org/xvm/compiler/Token.java) | Modified | Migrates token diagnostics to explicit source-span or no-location sites with trailing message arguments; the helper no longer returns an abort decision. | +### Binding facts and collectors + +| File | Change | What changed and why | +| --- | --- | --- | +| [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/bindings/AnonymousClassBindings.java) | Added | Associates anonymous-class captures and generated properties with their enclosing source registers, so navigation and rename can follow the original declaration. | +| [CursorBinding.java](src/main/java/org/xvm/compiler/ast/bindings/CursorBinding.java) | Added | Defines immutable collections of scope, type, candidate-call and argument-proposal facts for an explicit cursor query. Its collector publishes inspected source sites without presenting candidates as selected executable calls. | +| [InitializerBinding.java](src/main/java/org/xvm/compiler/ast/bindings/InitializerBinding.java) | Added | Captures source references, types and call facts before successful constant folding discards an initializer's temporary method; excludes validation contexts and disposable AST identities from the published provenance. | +| [InvocationBinding.java](src/main/java/org/xvm/compiler/ast/bindings/InvocationBinding.java) | Added | Records selected methods, function types, constructors and source-to-parameter argument associations. Attempt-owned collectors discard stale/trial facts and publish maps keyed by source-node identity. | +| [LambdaBindings.java](src/main/java/org/xvm/compiler/ast/bindings/LambdaBindings.java) | Added | Maps written lambda parameters and generated capture parameters back to their source registers, distinguishing source declarations from synthetic compiler parameters. | + ### AST ownership, contexts and semantic provenance | File | Change | What changed and why | | --- | --- | --- | | [AnonInnerClass.java](src/main/java/org/xvm/compiler/ast/AnonInnerClass.java) | Modified | Enforces the non-null listener contract at construction even when assertions are disabled; this particular edit does not add a new anonymous-class algorithm. | -| [AnonymousClassBindings.java](src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java) | Added | Associates anonymous-class captures and generated properties with their enclosing source registers, so navigation and rename can follow the original declaration. | | [AstNode.java](src/main/java/org/xvm/compiler/ast/AstNode.java) | Modified | Adds repeatable child traversal and the documented copyTree contract, preserves the legacy clone entry point, supports inherited registered child fields and carries fact collectors into child staging. Reporting requires an explicit destination. | +| [BindingAst.java](src/main/java/org/xvm/compiler/ast/BindingAst.java) | Added | Supplies the single cross-package operation that associates a lambda parameter with its existing register, keeping AST field/setter visibility unchanged while binding collection lives in ast.bindings. | | [CompositionNode.java](src/main/java/org/xvm/compiler/ast/CompositionNode.java) | Modified | Makes source lookup safe for unparented parsed compositions and implicit compositions with no type child, supporting inspection before normal tree adoption. | | [Context.java](src/main/java/org/xvm/compiler/ast/Context.java) | Modified | Propagates the attempt's call/cursor collectors through lexical contexts and makes diagnostic/probe ownership explicit during name and this resolution. | | [Expression.java](src/main/java/org/xvm/compiler/ast/Expression.java) | Modified | Adds the covariant copyTree result and uses it for exhaustive fit trials. Makes type-fitting probes explicitly silent instead of depending on whether a caller happened to pass null or a reporting listener. | @@ -1104,7 +1137,6 @@ source or document in this checkout. | [ForStatement.java](src/main/java/org/xvm/compiler/ast/ForStatement.java) | Modified | Replaces separate nullable callback fields with ValidationScope and restores the previous scope after normal, early and exceptional validation exits. | | [ImportStatement.java](src/main/java/org/xvm/compiler/ast/ImportStatement.java) | Modified | Exposes the written alias, qualified-name tokens and already-resolved import identity without restarting name resolution, giving tooling exact import provenance. | | [InvocationExpression.java](src/main/java/org/xvm/compiler/ast/InvocationExpression.java) | Modified | Publishes validated method/function/super call facts and written argument mappings, exposes the invoked expression/selected method and uses explicit probes. Keeps failed argument fitting from being overwritten by successful return-type matching. | -| [LambdaBindings.java](src/main/java/org/xvm/compiler/ast/LambdaBindings.java) | Added | Maps written lambda parameters and generated capture parameters back to their source registers, distinguishing source declarations from synthetic compiler parameters. | | [LambdaExpression.java](src/main/java/org/xvm/compiler/ast/LambdaExpression.java) | Modified | Exposes the arrow token and retained source bindings, carries collectors into generated-method validation and supplies a typed tree copy; diagnostic probes and sites use the explicit listener API. | | [MethodDeclarationStatement.java](src/main/java/org/xvm/compiler/ast/MethodDeclarationStatement.java) | Modified | Exposes declaration names and documentation, retains formal-parameter identities and carries collectors through annotation/default/body validation and generated method compilation. | | [NameExpression.java](src/main/java/org/xvm/compiler/ast/NameExpression.java) | Modified | Exposes the resolved constant or register and supplies a typed tree copy. Also preserves the callable type and hidden generic bindings of bound method references; speculative resolution selects an explicit probe. | @@ -1212,7 +1244,7 @@ source or document in this checkout. | File | Change | What changed and why | | --- | --- | --- | | [compiler/CompilerCancellationTest.java](src/test/java/org/xvm/compiler/CompilerCancellationTest.java) | Added | Checks that cancelled structure registration stops without throwing or fabricating a source error. | -| [compiler/CursorBindingTest.java](src/test/java/org/xvm/compiler/CursorBindingTest.java) | Added | Checks grouped cursor-fact construction, immutable snapshots and independent updates that preserve unrelated call, argument and enclosing-instance facts. | +| [compiler/ast/bindings/CursorBindingTest.java](src/test/java/org/xvm/compiler/ast/bindings/CursorBindingTest.java) | Added | Checks grouped cursor-fact construction, immutable snapshots and independent updates that preserve unrelated call, argument and enclosing-instance facts. | | [compiler/EvalCompilerTest.java](src/test/java/org/xvm/compiler/EvalCompilerTest.java) | Added | Checks that diagnostics can be queried before compilation starts. | | [compiler/ParserAttemptTest.java](src/test/java/org/xvm/compiler/ParserAttemptTest.java) | Added | Checks kept/discarded and nested attempts, token rewind, recovery restoration, lexer reports, callback exceptions, caller abort and module-name scanning. | | [compiler/ParserDeclarationNameTest.java](src/test/java/org/xvm/compiler/ParserDeclarationNameTest.java) | Added | Checks unfinished declaration-name slots, retained written types/initializers, independent children and following declarations; rejects slots outside supported contexts and honors stopping policies. | diff --git a/javatools/README.md b/javatools/README.md index e844e0f086f..ec83005ee30 100644 --- a/javatools/README.md +++ b/javatools/README.md @@ -70,6 +70,11 @@ type constants or constant pool safe to share across workers. The language adapt facts it needs into detached models before releasing the attempt. Keeping this boundary explicit is essential for a long-running editor that repeatedly replaces analyses of unsaved text. +The five source-binding classes live in `org.xvm.compiler.ast.bindings`, alongside `ast.partial`. +Compiler-owned collectors write the facts; a one-operation `ast.BindingAst` bridge associates +lambda parameter registers with written declarations without widening existing AST members. +See [binding ownership and package boundaries](LSP-COMPILER.md#keep-binding-facts-and-their-writers-together). + ### Analyze declarations and unfinished source explicitly `analyzeDeclarations` resolves declaration structure without promising a fully validated or diff --git a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java index f73d8386fae..9baa6da97d9 100644 --- a/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java +++ b/javatools/src/main/java/org/xvm/api/EmbeddingSupport.java @@ -42,10 +42,7 @@ import org.xvm.compiler.BuildRepository; import org.xvm.compiler.Compiler; import org.xvm.compiler.CompilerException; -import org.xvm.compiler.CursorBinding; import org.xvm.compiler.InstantRepository; -import org.xvm.compiler.InitializerBinding; -import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Parser; import org.xvm.compiler.Source; @@ -56,6 +53,9 @@ import org.xvm.compiler.ast.PropertyDeclarationStatement; import org.xvm.compiler.ast.StatementBlock; import org.xvm.compiler.ast.TypeCompositionStatement; +import org.xvm.compiler.ast.bindings.CursorBinding; +import org.xvm.compiler.ast.bindings.InitializerBinding; +import org.xvm.compiler.ast.bindings.InvocationBinding; import org.xvm.compiler.ast.partial.IncompleteStatement; import org.xvm.compiler.ast.partial.PartialSyntax; @@ -969,21 +969,7 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to locate module " + file); return false; } - - if (module == null) { - assert errs.hasSeriousErrors() || errs.isAbortDesired(); - return false; - } - - if (output != null) { - try { - output.storeModule(module); - } catch (IOException e) { - errs.error(ERR_INTERNAL, at(module), e, "Unable to store module " + module.getName()); - return false; - } - } - return true; + return ModuleCompiler.storeCompiled(module, output, errs); } /** diff --git a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java index c5e4d559394..1ecd5624321 100644 --- a/javatools/src/main/java/org/xvm/api/ModuleCompiler.java +++ b/javatools/src/main/java/org/xvm/api/ModuleCompiler.java @@ -129,9 +129,21 @@ public boolean compile(File file, ModuleRepository input, ModuleRepository outpu errs.error(ERR_INTERNAL, NOWHERE, e, "Unable to read module " + file); return false; } + return storeCompiled(module, output, errs); + } + /** + * Finish a file compilation by placing its module into the optional output repository. + * + * @param module the compiled module, or null if the compilation failed or was abandoned + * @param output (optional) the module repository to write the compiled module to + * @param errs the ErrorListener that received the compilation's messages + * + * @return true if there is a module and it was stored, or no output was requested + */ + static boolean storeCompiled(ModuleStructure module, ModuleRepository output, ErrorListener errs) { if (module == null) { - assert errs.hasSeriousErrors(); + assert errs.hasSeriousErrors() || errs.isAbortDesired(); return false; } diff --git a/javatools/src/main/java/org/xvm/asm/FileRepository.java b/javatools/src/main/java/org/xvm/asm/FileRepository.java index ade9a83f4f7..bab290d6ef8 100644 --- a/javatools/src/main/java/org/xvm/asm/FileRepository.java +++ b/javatools/src/main/java/org/xvm/asm/FileRepository.java @@ -167,10 +167,10 @@ public synchronized void storeModule(ModuleStructure module) fileStructure.writeTo(workFile); file.delete(); if (workFile.renameTo(file)) { - cachedVersionsByName = fileStructure.buildFileInfo().modules(); - cachedFileStructure = fileStructure; - cacheOk = true; - readFailed = false; + cachedVersionsByName = fileStructure.buildFileInfo().modules(); + cachedFileStructure = fileStructure; + cacheOk = true; + readFailed = false; } } catch (IOException e) { throw new IOException("Error writing module to file: " + file, e); diff --git a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java index 8d3bf777886..b63074d2443 100644 --- a/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java +++ b/javatools/src/main/java/org/xvm/asm/constants/TypeConstant.java @@ -4126,7 +4126,7 @@ protected boolean explodeProperty( if (infoSet != null) { infoSet = infoSet.layerOn(new MethodInfo(new MethodBody(idSet, - idSet.getSignature(), Implementation.Implicit), nRank+1), false, cascade(!fComplete, errs)); + idSet.getSignature(), Implementation.Implicit), nRank + 1), false, cascade(!fComplete, errs)); } } diff --git a/javatools/src/main/java/org/xvm/compiler/Compiler.java b/javatools/src/main/java/org/xvm/compiler/Compiler.java index d6ca8204136..6aebf5b71e8 100644 --- a/javatools/src/main/java/org/xvm/compiler/Compiler.java +++ b/javatools/src/main/java/org/xvm/compiler/Compiler.java @@ -11,6 +11,8 @@ import org.xvm.compiler.ast.StageMgr; import org.xvm.compiler.ast.TypeCompositionStatement; +import org.xvm.compiler.ast.bindings.CursorBinding; +import org.xvm.compiler.ast.bindings.InvocationBinding; /** * A module compiler for Ecstasy code. diff --git a/javatools/src/main/java/org/xvm/compiler/Parser.java b/javatools/src/main/java/org/xvm/compiler/Parser.java index 25bff57e936..f296845f298 100644 --- a/javatools/src/main/java/org/xvm/compiler/Parser.java +++ b/javatools/src/main/java/org/xvm/compiler/Parser.java @@ -112,8 +112,8 @@ private Parser(Source source, ErrorListener errs, Lexer lexer, boolean fPartialA throw new IllegalArgumentException("Source required"); } - m_source = source; - f_errs = new Reporting(requireNonNull(errs, "errs")); + m_source = source; + f_errs = new Reporting(requireNonNull(errs, "errs")); // Lexical reports and cancellation follow the active speculative attempt. ErrorListener lexicalReports = new ErrorListener() { @Override @@ -161,7 +161,7 @@ public StatementBlock parseSource() { // during parsing m_fDone = true; - long lStart = peek().getStartPosition(); + long lStart = peek().getStartPosition(); List stmts = parseTypeCompositionComponents(null, new ArrayList<>(), true); if (stmts.isEmpty() && !f_errs.get().hasSeriousErrors()) { log(Severity.ERROR, NO_TYPE_FOUND, lStart, m_source.getPosition()); @@ -1243,8 +1243,8 @@ Statement parseMethodDeclarationAfterName(long lStartPos, Expression exprConditi return retainDeclaration(header, lStartPos, name, IncompleteDeclarationStatement.Kind.METHOD, List.of(), e); } - long lEndPos = prev().getEndPosition(); - StatementBlock body; + long lEndPos = prev().getEndPosition(); + StatementBlock body; switch (peek().getId()) { case SEMICOLON: body = null; @@ -3675,7 +3675,7 @@ Expression parsePrimaryExpression(boolean fExtended) { break; } - left = new NameExpression(left, nameNDR, name, null, lEndPos); + left = new NameExpression(left, nameNDR, name, null, lEndPos); if (fNormal && nameNext.getStartPosition() < f_cursor && f_cursor <= nameNext.getEndPosition() && canRetainIncompleteAt(nameNext.getEndPosition(), true)) { var hole = incomplete(left, dot, nameNext); @@ -5180,7 +5180,7 @@ private List parseTypeExpressionList(boolean fAllowTypeSequence, if (fAllowTypeSequence && peek(Id.COMP_LT)) { Token tokStart = peek(); List listSeq = parseTypeParameterTypeList(true, false, fHeader); - Token tokEnd = prev(); + Token tokEnd = prev(); types.add(new TupleTypeExpression(listSeq, tokStart.getStartPosition(), tokEnd.getEndPosition())); } else { types.add(parseIntersectingTypeExpression(true, fHeader)); @@ -5283,14 +5283,14 @@ List parseParameterList(boolean required) { } if (match(Id.R_PAREN) == null) { while (true) { - TypeExpression type = parseDeclarationType(); + TypeExpression type = parseDeclarationType(); var missingName = declarationNameSlot(type); if (missingName.isPresent()) { log(Severity.ERROR, INCOMPLETE_EXPRESSION, f_cursor, f_cursor); throw new IncompleteHeader(missingName.orElseThrow()); } - Token name = expect(Id.IDENTIFIER); - Expression value = null; + Token name = expect(Id.IDENTIFIER); + Expression value = null; if (match(Id.ASN) != null) { value = parseExpression(); } @@ -6284,11 +6284,11 @@ protected void putBack(Token token) { } protected static class Mark { - long pos; - Token token; - Token putBack; - Token lastMatch; - Token doc; + long pos; + Token token; + Token putBack; + Token lastMatch; + Token doc; } protected Mark mark() { @@ -6305,10 +6305,10 @@ protected void restore(Mark mark) { // A nesting-limit or listener abort ends this parse, including after speculative rewind. // Token backtracking must not re-enable recovery from an exhausted parser budget. m_lexer.restore(mark.pos); - m_token = mark.token; - m_tokenPutBack = mark.putBack; - m_tokenPrev = mark.lastMatch; - m_doc = mark.doc; + m_token = mark.token; + m_tokenPutBack = mark.putBack; + m_tokenPrev = mark.lastMatch; + m_doc = mark.doc; } /** diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java index 82fafd16aa9..a1af49acbf3 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AnnotationExpression.java @@ -64,6 +64,12 @@ public AnnotationExpression(Annotation anno, AstNode node) { this.lEndPos = node.getEndPosition(); } + /** Return an independent syntax tree with the same node type. */ + @Override + public AnnotationExpression copyTree() { + return (AnnotationExpression) super.copyTree(); + } + // ----- accessors ----------------------------------------------------------------------------- @Override diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java b/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java deleted file mode 100644 index ef2e7005311..00000000000 --- a/javatools/src/main/java/org/xvm/compiler/ast/AnonymousClassBindings.java +++ /dev/null @@ -1,54 +0,0 @@ -package org.xvm.compiler.ast; - -import java.util.Collections; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.Map; - -import org.xvm.asm.Component; -import org.xvm.asm.PropertyStructure; -import org.xvm.asm.Register; -import org.xvm.asm.constants.PropertyConstant; - -/** Captured variables and the source origins of registers used to read them. */ -public final class AnonymousClassBindings { - AnonymousClassBindings(NewExpression owner, Map registers) { - f_owner = owner; - f_registers = Map.copyOf(registers); - } - - /** - * @return an immutable snapshot, preserving register identity rather than register equality - */ - public Map getCaptureOrigins() { - return Collections.unmodifiableMap(new IdentityHashMap<>(f_origins)); - } - - /** Copy generated capture properties to their enclosing source registers. */ - Map propertyOrigins(Component component) { - var origins = new HashMap(); - f_registers.forEach((name, register) -> { - if (component.getChild(name) instanceof PropertyStructure property && - property.isSynthetic()) { - origins.put(property.getIdentityConstant(), register.getOriginalRegister()); - } - }); - return Map.copyOf(origins); - } - - boolean isFor(NewExpression owner) { - return f_owner == owner; - } - - Map registers() { - return f_registers; - } - - void bind(String name, Register captured) { - f_origins.put(captured.getOriginalRegister(), f_registers.get(name).getOriginalRegister()); - } - - private final NewExpression f_owner; - private final Map f_registers; - private final Map f_origins = new IdentityHashMap<>(); -} diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java b/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java index 0d46b4e1c4b..e3256469227 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AssertStatement.java @@ -6,6 +6,7 @@ import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.xvm.asm.Argument; import org.xvm.asm.Assignment; @@ -591,11 +592,7 @@ public String toString() { if (!conds.isEmpty()) { sb.append(' ') - .append(conds.get(0)); - for (int i = 1, c = conds.size(); i < c; ++i) { - sb.append(", ") - .append(conds.get(i)); - } + .append(conds.stream().map(Object::toString).collect(Collectors.joining(", "))); } sb.append(';'); diff --git a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java index c987d9342d8..88756897bcf 100644 --- a/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java +++ b/javatools/src/main/java/org/xvm/compiler/ast/AstNode.java @@ -22,6 +22,8 @@ import java.util.stream.Collectors; +import org.jetbrains.annotations.NotNull; + import org.xvm.asm.Argument; import org.xvm.asm.Component; import org.xvm.asm.ComponentResolver; @@ -48,13 +50,13 @@ import org.xvm.compiler.Compiler; import org.xvm.compiler.Compiler.Stage; -import org.xvm.compiler.CursorBinding; -import org.xvm.compiler.InvocationBinding; import org.xvm.compiler.Source; import org.xvm.compiler.Token; import org.xvm.compiler.ast.Expression.TypeFit; import org.xvm.compiler.ast.NameExpression.Meaning; +import org.xvm.compiler.ast.bindings.CursorBinding; +import org.xvm.compiler.ast.bindings.InvocationBinding; import org.xvm.util.ListMap; import org.xvm.util.Severity; @@ -439,12 +441,15 @@ protected AstNode getCodeContainer() { /** * @return a unique counter in the context of the code container + * + * @throws IllegalStateException if this node has no enclosing code container */ protected int getCodeContainerCounter() { AstNode parent = getParent(); - return parent == null - ? null - : parent.getCodeContainerCounter(); + if (parent == null) { + throw new IllegalStateException("No enclosing code container for " + getClass().getSimpleName()); + } + return parent.getCodeContainerCounter(); } /** @@ -724,9 +729,9 @@ public void log(ErrorListener errs, Severity severity, String sCode, Object... a * checking for errors that are obvious from "this point down" (no lateral evaluation of * structures, because we can't count on them even existing yet.) * - *

  • The general idea is that this method recurses through the structure, allowing each node - * to introduce itself as the parent of each node under it, and that the nodes which will be - * structures in the resulting FileStructure will register themselves.
  • + *
  • This method recurses through the tree, establishing each node as the parent of its + * children. Nodes that represent structures in the resulting FileStructure also register + * themselves.
  • * *
  • Type parameters for the types must also be registered, because they are also types, and * they will be required to already be present when the second pass begins.
  • @@ -747,9 +752,8 @@ protected void registerStructures(StageMgr mgr, ErrorListener errs) { * compilation is not only unpredictable, but the potential exists for dependencies in either * direction (first to last and/or vice versa). * - *

    As a result, some questions may come to an AstNode to resolve that it is not yet prepared to - * resolve, in which case the caller (another AstNode) has to add itself to the list of nodes - * that require another pass. + *

    A node may be asked to resolve a name before its dependencies are ready. In that case, + * the calling node must defer its own resolution to another pass. * *