diff --git a/src/features/project-tree/ProjectView.module.css b/src/features/project-tree/ProjectView.module.css index 1e71b0bed..28a6a0487 100644 --- a/src/features/project-tree/ProjectView.module.css +++ b/src/features/project-tree/ProjectView.module.css @@ -163,7 +163,7 @@ width: 16px; height: 16px; } -.runnable { +.overlay { position: absolute; inset: 0; pointer-events: none; diff --git a/src/features/project-tree/ProjectView.test.tsx b/src/features/project-tree/ProjectView.test.tsx index 6b1a82903..873cc1212 100644 --- a/src/features/project-tree/ProjectView.test.tsx +++ b/src/features/project-tree/ProjectView.test.tsx @@ -837,3 +837,31 @@ it("shows a decoration's suffix after the root name, with icons and struck-out s act(() => setFileDecorations("test-suffix", new Map())); await waitFor(() => expect(row("acme").textContent).not.toContain("main/")); }); + +it("marks file and folder symlinks with the link arrow, leaving real folders and files alone", async () => { + fs = new MemoryFs( + { + "/w/acme/dir1/keep.txt": "x", + "/w/acme/dir2/keep.txt": "x", + "/w/acme/note.txt": "x", + }, + ROOT, + // `ln -s dir1 link1` on macOS/Linux; the backend reports kind "symlink", size 0. + { "/w/acme/link1": "/w/acme/dir1", "/w/acme/note-link.txt": "/w/acme/note.txt" }, + ); + setProjectFsForTests(fs, ROOT); + initProjectTree(ROOT, "acme"); + const { row } = setup(); + const iconAt = (label: string) => row(label).querySelector("[data-project-path]")?.getAttribute("data-project-icon"); + await waitFor(() => expect(row("link1")).toBeTruthy()); + // The link to a folder carries the arrow (IDEA's symlink badge)… + expect(within(row("link1")).getByLabelText("Symbolic link").tagName).toBe("svg"); + expect(iconAt("link1")).toBe("folder"); + expect(iconAt("dir1")).toBe("folder"); + // …so does a link to a file, as in IDEA; real folders and files do not. + expect(within(row("note-link.txt")).getByLabelText("Symbolic link").tagName).toBe("svg"); + expect(iconAt("note-link.txt")).toBe("file-text"); + expect(within(row("dir1")).queryByLabelText("Symbolic link")).toBeNull(); + expect(within(row("dir2")).queryByLabelText("Symbolic link")).toBeNull(); + expect(within(row("note.txt")).queryByLabelText("Symbolic link")).toBeNull(); +}); diff --git a/src/features/project-tree/ProjectView.tsx b/src/features/project-tree/ProjectView.tsx index 4965877b6..06c7cb932 100644 --- a/src/features/project-tree/ProjectView.tsx +++ b/src/features/project-tree/ProjectView.tsx @@ -322,8 +322,10 @@ const Row = memo(function Row({ size={16} className={clsx(styles.icon, node.dir && styles.folderIcon, dimIcon && styles.faded)} /> + {/* IDEA's symlink arrow, on file and folder links (the entry kind comes with the listing). */} + {node.symlink && } {!!file?.runnable && icon !== "spring-boot" && icon !== "jvm-test" && ( - + )} {isEditing && editing ? ( diff --git a/src/features/project-tree/fs.ts b/src/features/project-tree/fs.ts index e60f3a0a5..98e0c038a 100644 --- a/src/features/project-tree/fs.ts +++ b/src/features/project-tree/fs.ts @@ -99,10 +99,16 @@ export function beginLocalImport(): LocalImport { }; } -type MemNode = { kind: "file"; size: number } | { kind: "dir" }; +type MemNode = { kind: "file"; size: number } | { kind: "dir" } | { kind: "symlink"; target: string }; + +/** What a link resolves to (the loop in `resolve` never stops on a link). */ +type ResolvedNode = Exclude; const err = (kind: "notFound" | "alreadyExists" | "invalidInput", message: string) => new IpcError({ kind, message }); +/** The tree's own notion (see `isDirEntry`): a folder, or a link to one. */ +const isFolder = (e: FileEntry) => e.kind === "dir" || (e.kind === "symlink" && e.size === 0); + /** Minimal in-memory file system with the backend's semantics (no overwrite, folders first). */ export class MemoryFs implements ProjectFs { readonly nodes = new Map(); @@ -110,7 +116,7 @@ export class MemoryFs implements ProjectFs { /** Called after every mutation with the changed paths (simulates the watcher). */ onChange?: (e: { created: string[]; modified: string[]; removed: string[] }) => void; - constructor(files: Record = {}, root = "/") { + constructor(files: Record = {}, root = "/", links: Record = {}) { this.nodes.set(root, { kind: "dir" }); for (const [path, content] of Object.entries(files)) { if (content === null) this.mkdirp(path); @@ -119,6 +125,10 @@ export class MemoryFs implements ProjectFs { this.nodes.set(path, { kind: "file", size: content.length }); } } + for (const [path, target] of Object.entries(links)) { + this.mkdirp(parentOf(path)); + this.nodes.set(path, { kind: "symlink", target }); + } } private mkdirp(path: string) { @@ -132,28 +142,60 @@ export class MemoryFs implements ProjectFs { return [...this.nodes.keys()].filter((p) => p !== dir && parentOf(p) === dir); } - async readDir(path: string): Promise { - const n = this.nodes.get(path); - if (!n) throw err("notFound", `${path} not found`); - if (n.kind !== "dir") throw err("invalidInput", `${path} is not a directory`); + /** Follows symlinks (bounded, so a link cycle errors instead of hanging). */ + private resolve(path: string): { path: string; node: ResolvedNode | undefined } { + let p = path; + let n = this.nodes.get(p); + for (let hops = 0; n?.kind === "symlink"; hops++) { + if (hops > 16) throw err("invalidInput", `${path} is a symlink loop`); + p = n.target; + n = this.nodes.get(p); + } + // The loop only exits on a non-link entry (or a missing one). + return { path: p, node: n as ResolvedNode | undefined }; + } + + async readDir(at: string): Promise { + // Like the backend, listing a symlink to a folder lists through the link. + const { path, node } = this.resolve(at); + if (!node) throw err("notFound", `${path} not found`); + if (node.kind !== "dir") throw err("invalidInput", `${at} is not a directory`); const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: "base" }); - return this.childrenOf(path) - .map((p) => { - const c = this.nodes.get(p)!; - const name = nameOf(p); - const excluded = [...this.ignored].some((i) => p === i || isInside(p, i)); - return { - name, - path: p, - kind: c.kind, - size: c.kind === "file" ? c.size : 0, - mtimeMs: 0, - ignored: excluded, - excluded, - hidden: name.startsWith("."), - } satisfies FileEntry; - }) - .sort((a, b) => Number(b.kind === "dir") - Number(a.kind === "dir") || collator.compare(a.name, b.name)); + return ( + this.childrenOf(path) + .map((p) => { + const c = this.nodes.get(p)!; + const name = nameOf(p); + const excluded = [...this.ignored].some((i) => p === i || isInside(p, i)); + if (c.kind === "symlink") { + const t = this.resolve(p); + return { + name, + path: p, + kind: "symlink", + // A link to a folder has size 0; a dangling one the link's own length (the backend's `symlink_metadata`). + size: t.node ? (t.node.kind === "dir" ? 0 : t.node.size) : c.target.length, + ...(t.node && { targetKind: t.node.kind === "dir" ? "dir" : "file" }), + mtimeMs: 0, + ignored: excluded, + excluded, + hidden: name.startsWith("."), + } satisfies FileEntry; + } + return { + name, + path: p, + kind: c.kind, + size: c.kind === "file" ? c.size : 0, + mtimeMs: 0, + ignored: excluded, + excluded, + hidden: name.startsWith("."), + } satisfies FileEntry; + }) + // Folders first, counting a link to a folder as a folder (the backend's ordering). + .sort((a, b) => Number(isFolder(b)) - Number(isFolder(a)) || collator.compare(a.name, b.name)) + ); } private emit(e: Partial<{ created: string[]; modified: string[]; removed: string[] }>) { diff --git a/src/features/project-tree/model.test.ts b/src/features/project-tree/model.test.ts index b13d3bc2e..7fdca10ef 100644 --- a/src/features/project-tree/model.test.ts +++ b/src/features/project-tree/model.test.ts @@ -12,7 +12,7 @@ import { type TreeOptions, } from "./model"; -const entry = (path: string, kind: "file" | "dir" = "file", extra: Partial = {}): FileEntry => { +const entry = (path: string, kind: FileEntry["kind"] = "file", extra: Partial = {}): FileEntry => { const name = path.slice(path.lastIndexOf("/") + 1); return { name, @@ -135,6 +135,47 @@ describe("buildModel", () => { expect(kids[0]).toMatchObject({ placeholder: "file", path: "/r" }); expect(kids).toHaveLength(5); }); + + describe("symlinks", () => { + // The backend reports links as `kind: "symlink"`, sized by their target: + // a folder link has size 0 (see `isDirEntry`). + const withLinks = listings({ + "/r": [ + entry("/r/dir1", "dir"), + entry("/r/link1", "symlink", { size: 0, targetKind: "dir" }), + entry("/r/file.txt"), + entry("/r/link.txt", "symlink", { size: 12, targetKind: "file" }), + ], + "/r/dir1": [entry("/r/dir1/a.txt")], + }); + + it("marks directory symlinks (dir + symlink) so the tree can overlay the link arrow", () => { + const m = buildModel(ROOT, withLinks, DEFAULT_TREE_OPTIONS); + expect(m.byKey.get("/r/link1")).toMatchObject({ dir: true, symlink: true }); + expect(m.byKey.get("/r/dir1")).toMatchObject({ dir: true, symlink: false }); + expect(m.byKey.get("/r/file.txt")).toMatchObject({ dir: false, symlink: false }); + }); + + it("marks file symlinks as files, not folders", () => { + const m = buildModel(ROOT, withLinks, DEFAULT_TREE_OPTIONS); + expect(m.byKey.get("/r/link.txt")).toMatchObject({ dir: false, symlink: true }); + }); + + it("keeps the link marker when a link heads a compacted chain", () => { + const l = listings({ + "/r": [entry("/r/link1", "symlink", { size: 0, targetKind: "dir" })], + "/r/link1": [entry("/r/link1/inner", "dir")], + "/r/link1/inner": [entry("/r/link1/inner/only.txt")], + }); + const m = buildModel(ROOT, l, DEFAULT_TREE_OPTIONS); + expect(m.byKey.get("/r/link1")).toMatchObject({ dir: true, symlink: true, path: "/r/link1/inner" }); + }); + + it("sorts folder links with the folders", () => { + const names = visibleEntries(withLinks.get("/r")!, DEFAULT_TREE_OPTIONS).map((e) => e.name); + expect(names).toEqual(["dir1", "link1", "file.txt", "link.txt"]); + }); + }); }); describe("visibleInfo", () => { diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index 03d42ee6f..cdf76c85b 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -1,4 +1,5 @@ { + "Symbolic link": "Symbolic link", "Copy as TSV": "Copy as TSV", "Copy as CSV": "Copy as CSV", "Copy as One-Row": "Copy as One-Row", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index 205ffb562..055a74f45 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -1,4 +1,5 @@ { + "Symbolic link": "符号链接", "Copy as TSV": "复制为 TSV", "Copy as CSV": "复制为 CSV", "Copy as One-Row": "复制为单行", diff --git a/src/ui/expui/nodes/symlink.svg b/src/ui/expui/nodes/symlink.svg new file mode 100644 index 000000000..4c2ccbb21 --- /dev/null +++ b/src/ui/expui/nodes/symlink.svg @@ -0,0 +1,4 @@ + + + + diff --git a/src/ui/expui/nodes/symlink_dark.svg b/src/ui/expui/nodes/symlink_dark.svg new file mode 100644 index 000000000..fcc2390a9 --- /dev/null +++ b/src/ui/expui/nodes/symlink_dark.svg @@ -0,0 +1,4 @@ + + + + diff --git a/src/ui/icons.tsx b/src/ui/icons.tsx index 0b8661e3b..4ff6ba5ec 100644 --- a/src/ui/icons.tsx +++ b/src/ui/icons.tsx @@ -89,6 +89,7 @@ const ICONS = { exception: "nodes/exception", "jvm-test": "nodes/test", "runnable-mark": "nodes/runnableMark", + "symlink-mark": "nodes/symlink", "kotlin-object": "kotlin/objectKotlin", "spring-config": "openidea/runConfigurations/springBoot", "gradle-project": "gradle/gradle",