The IDE you know, rebuilt in Rust. IntelliJ IDEA–style keymap, tool windows and search-everywhere in a fast native app: editor, code insight, debugger, Git, terminal, database and HTTP clients for 60+ languages.
TypeScript code-editorcross-platformdapdatabase-clientdebuggerdesktop-appdeveloper-toolsgithttp-clientideintellijlsprusttauriterminal
fix(project-tree): reveal library classes in registered archives (#2432)
l0ng-aie152cc4ad9cf
diff --git a/src/features/project-tree/extraRoots.test.ts b/src/features/project-tree/extraRoots.test.tsindex 9cde906a7..5fad18c75 100644--- a/src/features/project-tree/extraRoots.test.ts+++ b/src/features/project-tree/extraRoots.test.ts@@ -14,7 +14,7 @@ vi.mock("@/editor/refactor", () => ({}));const { MemoryFs, setProjectFsForTests } = await import("./fs");-const { DEFAULT_TREE_OPTIONS, buildModel, withExtraRoots } = await import("./model");+const { DEFAULT_TREE_OPTIONS, buildModel, visibleInfo, withExtraRoots } = await import("./model");const { currentModel, initProjectTree, loadDir, revealPath, useProjectTree } = await import("./store");const { beginRename, commitEditing, deleteSelection } = await import("./operations");@@ -149,3 +149,126 @@ describe("project view with External Libraries", () => {expect(revealPath("jar:///elsewhere/x.jar!/p/C.class")).toBe(false);});});++describe("library reveal root selection", () => {+ const GRADLE = "/g/caches/modules-2/files-2.1/org.example/a/1.0";+ let unregister = () => {};+ beforeEach(() => localStorage.clear());+ afterEach(() => {+ unregister();+ setProjectFsForTests(null);+ initProjectTree(null);+ });++ it.each([+ {+ name: "an archive entry inside the project",+ requested: "/p/lib/a.jar!/x/A.class",+ roots: ["/p/lib/a.jar!"],+ expected: "/p/lib/a.jar!/x/A.class",+ },+ {+ name: "a jdtls class inside the project",+ requested: "jdt://contents/a.jar/x/A.class?=p/%5C/p%5C/lib%5C/a.jar%3Cx%28A.class",+ roots: ["/p/lib/a.jar!"],+ expected: "/p/lib/a.jar!/x/A.class",+ },+ {+ name: "a jar URI inside a workspace member",+ requested: "jar:file:///member/lib/a.jar!/x/A.class",+ roots: ["/member/lib/a.jar!"],+ members: ["/member"],+ expected: "/member/lib/a.jar!/x/A.class",+ },+ {+ name: "JDK 8 sources after an unregistered rt.jar under the JDK home",+ requested:+ "jdt://contents/rt.jar/java.lang/String.class?=p/%5C/jdk8%5C/jre%5C/lib%5C/rt.jar%3Cjava.lang%28String.class",+ roots: ["/jdk8", "/jdk8/src.zip!"],+ expected: "/jdk8/src.zip!/java/lang/String.java",+ },+ {+ name: "the first registered archive candidate before JDK 8 sources",+ requested:+ "jdt://contents/rt.jar/java.lang/String.class?=p/%5C/jdk8%5C/jre%5C/lib%5C/rt.jar%3Cjava.lang%28String.class",+ roots: ["/jdk8", "/jdk8/src.zip!", "/jdk8/jre/lib/rt.jar!"],+ expected: "/jdk8/jre/lib/rt.jar!/java/lang/String.class",+ },+ {+ name: "a Gradle sources jar with a different binary hash directory",+ requested: `jar:file://${GRADLE}/sourceHash/a-1.0-sources.jar!/x/A.java`,+ roots: ["/g", `${GRADLE}/binaryHash/a-1.0.jar!`],+ expected: `${GRADLE}/binaryHash/a-1.0.jar!/x/A.class`,+ },+ {+ name: "the exact Gradle sources archive before its binary fallback",+ requested: `jar:file://${GRADLE}/sourceHash/a-1.0-sources.jar!/x/A.java`,+ roots: [`${GRADLE}/binaryHash/a-1.0.jar!`, `${GRADLE}/sourceHash/a-1.0-sources.jar!`],+ expected: `${GRADLE}/sourceHash/a-1.0-sources.jar!/x/A.java`,+ },+ {+ name: "an unregistered archive inside the project",+ requested: "jar:file:///p/lib/a.jar!/x/A.class",+ roots: [],+ expected: null,+ },+ {+ name: "an unregistered archive inside a workspace member",+ requested: "/member/lib/a.jar!/x/A.class",+ roots: [],+ members: ["/member"],+ expected: null,+ },+ {+ name: "an unregistered archive inside a broader library folder",+ requested: "jar:file:///jdk8/jre/lib/rt.jar!/java/lang/String.class",+ roots: ["/jdk8"],+ expected: null,+ },+ ])("reveals only registered archives: $name", async ({ requested, roots, members, expected }) => {+ setProjectFsForTests(new MemoryFs({ "/p/pom.xml": "x", ...(expected ? { [expected]: "x" } : {}) }, "/p"), "/p");+ initProjectTree("/p", "proj", members);+ unregister = registerProjectViewRoot({+ id: "externalLibraries",+ label: "External Libraries",+ icon: "library",+ readOnly: true,+ children: [+ {+ label: "Libraries",+ icon: "library",+ children: roots.map((path) => ({ label: path, icon: "library", path })),+ },+ ],+ });++ expect(revealPath(requested)).toBe(expected !== null);+ expect(useProjectTree.getState().revealRequest?.path ?? null).toBe(expected);+ if (!expected) return;+ const root = expected.slice(0, expected.indexOf("!/") + 1);+ const rootKey = currentModel().keyOfPath.get(root)!;+ expect(rootKey).not.toBe(root);+ expect(useProjectTree.getState().expanded.has(rootKey)).toBe(true);+ await vi.waitFor(() => {+ const model = currentModel();+ const key = model.keyOfPath.get(expected)!;+ expect(visibleInfo(model, useProjectTree.getState().expanded).visible.has(key)).toBe(true);+ });+ });++ it("matches a Windows archive root registered with backslashes", () => {+ setProjectFsForTests(new MemoryFs({ "C:/p/pom.xml": "x" }, "C:/p"), "C:/p");+ initProjectTree("C:/p", "proj");+ const root = "C:\\m2\\a.jar!";+ unregister = registerProjectViewRoot({+ id: "externalLibraries",+ label: "External Libraries",+ icon: "library",+ readOnly: true,+ children: [{ label: "Libraries", icon: "library", children: [{ label: "a.jar", icon: "library", path: root }] }],+ });++ expect(revealPath("jar:file:///C:/m2/a.jar!/x/A.class")).toBe(true);+ expect(useProjectTree.getState().revealRequest?.path).toBe("C:/m2/a.jar!/x/A.class");+ });+});diff --git a/src/features/project-tree/store.ts b/src/features/project-tree/store.tsindex 758edb38c..b438e7e93 100644--- a/src/features/project-tree/store.ts+++ b/src/features/project-tree/store.ts@@ -10,6 +10,7 @@ import { commands, type FileEntry, type OutlineItem } from "@/bindings";import { withDocument } from "@/editor/lsp/files";import { IpcError } from "@/platform/ipc";import { libraryEntryPaths } from "@/platform/virtualDocs";+import { ipcPath } from "@/platform/paths";import {DEFAULT_TREE_OPTIONS,GROUP_PREFIX,@@ -593,9 +594,20 @@ export function revealPath(requested: string, focus = false) {const { expanded } = get();const project = get().root;if (!project) return false;- const rootOf = (p: string) => memberRootOf(p) ?? (isSameOrInside(p, project) ? project : extraRootOf(p));+ // Registered archive roots by their `/`-separated form, which candidate paths use.+ const archiveRoots = new Map(+ extraFolders(projectViewRoots(useProjectViewRoots.getState().roots)).flatMap((r) =>+ r.path?.endsWith("!") ? [[ipcPath(r.path), r.path] as const] : [],+ ),+ );+ const archives = [...archiveRoots.keys()];+ const rootOf = (p: string) => {+ const archive = p.indexOf("!/");+ if (archive !== -1) return archiveRoots.get(ipcPath(p.slice(0, archive + 1))) ?? null;+ return memberRootOf(p) ?? (isSameOrInside(p, project) ? project : extraRootOf(p));+ };// Library documents (`jdt://`, `jar:` URIs) are shown as entries of their archive under External Libraries.- const path = [requested, ...libraryEntryPaths(requested)].find((p) => rootOf(p) !== null);+ const path = [requested, ...libraryEntryPaths(requested, archives)].find((p) => rootOf(p) !== null);if (!path) return false;const root = rootOf(path)!;// Library folders sit below group nodes (External Libraries › SDK), keyed in their own namespace.diff --git a/src/platform/virtualDocs.test.ts b/src/platform/virtualDocs.test.tsindex 09c39b7fa..b709339f0 100644--- a/src/platform/virtualDocs.test.ts+++ b/src/platform/virtualDocs.test.ts@@ -91,4 +91,36 @@ describe("libraryEntryPaths", () => {])("%s", (_, uri, expected) => {expect(libraryEntryPaths(uri)).toEqual(expected);});++ const GRADLE = "/home/me/.gradle/caches/modules-2/files-2.1/org.example/a/1.0";+ const SOURCES = `${GRADLE}/sourceHash/a-1.0-sources.jar`;+ const BINARY = `${GRADLE}/binaryHash/a-1.0.jar!`;++ it.each([+ `jar:file://${SOURCES}!/org/x/Y.java`,+ `jdt://contents/a-1.0-sources.jar/org.x/Y.class?=p/${encodeURIComponent(SOURCES)}%3Corg.x%28Y.class`,+ ])("finds a Gradle binary jar in a different hash directory for %s", (uri) => {+ expect(libraryEntryPaths(uri, [BINARY])).toEqual([+ ...(uri.startsWith("jar:") ? [`${SOURCES}!/org/x/Y.java`] : []),+ `${GRADLE}/sourceHash/a-1.0.jar!/org/x/Y.class`,+ `${BINARY}/org/x/Y.class`,+ ]);+ });++ it("keeps the direct Gradle binary match ahead of other hash directories", () => {+ expect(+ libraryEntryPaths(`jar:file://${SOURCES}!/org/x/Y.java`, [BINARY, `${GRADLE}/sourceHash/a-1.0.jar!`]),+ ).toEqual([`${SOURCES}!/org/x/Y.java`, `${GRADLE}/sourceHash/a-1.0.jar!/org/x/Y.class`]);+ });++ it("does not match Gradle jars from another version, artifact or file name", () => {+ expect(+ libraryEntryPaths(`jar:file://${SOURCES}!/org/x/Y.java`, [+ BINARY.replace("/1.0/", "/2.0/"),+ BINARY.replace("/a/", "/b/"),+ `${GRADLE}/binaryHash/b-1.0.jar!`,+ `${GRADLE}/binaryHash/a-1.0.jar`,+ ]),+ ).toEqual([`${SOURCES}!/org/x/Y.java`, `${GRADLE}/sourceHash/a-1.0.jar!/org/x/Y.class`]);+ });});diff --git a/src/platform/virtualDocs.ts b/src/platform/virtualDocs.tsindex dfb4c7463..f602785c0 100644--- a/src/platform/virtualDocs.ts+++ b/src/platform/virtualDocs.ts@@ -98,11 +98,20 @@ const uriFsPath = (p: string) => (/^\/[A-Za-z]:\//.test(p) ? p.slice(1) : p);const parentDir = (p: string, levels = 1) => p.split("/").slice(0, -levels).join("/");/** `(jar, entry without extension)` → the class in its jar (the binary jar for a `-sources.jar`). */-const jarCandidates = (jar: string, stem: string) => [- `${jar.replace(/-sources\.jar$/, ".jar")}!/${stem}.class`,+const jarCandidates = (jar: string, stem: string, archives: readonly string[]) => {+ const binary = jar.replace(/-sources\.jar$/, ".jar");+ const candidates = [`${binary}!/${stem}.class`];+ // Gradle caches binary and sources jars in separate hash directories of one version.+ const gradle = /^(.*\/files-2\.1\/[^/]+\/[^/]+\/[^/]+)\/[^/]+\/[^/]+$/.exec(jar);+ if (jar !== binary && gradle && !archives.includes(`${binary}!`)) {+ const name = binary.slice(binary.lastIndexOf("/") + 1);+ for (const root of archives)+ if (root.endsWith(`/${name}!`) && parentDir(root, 2) === gradle[1]) candidates.push(`${root}/${stem}.class`);+ }// JDK 8: `<home>/jre/lib/rt.jar` with its sources in `<home>/src.zip`.- ...(jar.endsWith("/rt.jar") ? [`${parentDir(jar, 3)}/src.zip!/${stem}.java`] : []),-];+ if (jar.endsWith("/rt.jar")) candidates.push(`${parentDir(jar, 3)}/src.zip!/${stem}.java`);+ return candidates;+};/*** Where a library document URI lies in the Project view's External Libraries,@@ -111,7 +120,7 @@ const jarCandidates = (jar: string, stem: string) => [* (classes and sources jars) and decompiled `file:` classes. Empty for anything* else; a missing entry still leads to its package folder.*/-export function libraryEntryPaths(uri: string): string[] {+export function libraryEntryPaths(uri: string, archives: readonly string[] = []): string[] {const base = uri.split(/[?#]/)[0]!;const scheme = base.slice(0, Math.max(base.indexOf(":"), 0)).toLowerCase();if (scheme === "file") return isClassFile(base) ? [uriFsPath(decode(base.replace(/^file:(\/\/)?/, "")))] : [];@@ -122,7 +131,7 @@ export function libraryEntryPaths(uri: string): string[] {const entry = decode(m[2]!);const exact = `${jar}!/${entry}`;const stem = entry.replace(/\.[^./]*$/, "");- return [exact, ...jarCandidates(jar, stem).filter((p) => p !== exact)];+ return [exact, ...jarCandidates(jar, stem, archives).filter((p) => p !== exact)];}if (scheme !== "jdt") return [];// jdt://contents/<jar or module>/<package>/<Class.class>?=<project>/<escaped jar path><package(Class.class@@ -139,5 +148,5 @@ export function libraryEntryPaths(uri: string): string[] {if (!jar) return [];// JDK 9+: `<home>/lib/jrt-fs.jar`, sources per module in `<home>/lib/src.zip`.if (jar.endsWith("/jrt-fs.jar")) return [`${parentDir(jar)}/src.zip!/${module}/${stem}.java`];- return jarCandidates(jar, stem);+ return jarCandidates(jar, stem, archives);}