fix(project): mark symlinks with a link arrow in the tree (#3547)

Directory and file symlinks used the same icons as real folders and files. The Project view now overlays IDEA's link arrow (expui nodes/symlink) on every entry the listing reports as a symlink; the model already carried the flag, so no extra stat calls.

Closes #3527

Co-authored-by: Inf1mum <8135225+Inf1mum@users.noreply.github.com>
This commit is contained in:
Vadim Lazuk
2026-11-15 23:06:39 +08:00
committed by GitHub
co-authored by Inf1mum
parent d1290c76ea
commit b7adbbb17c
10 changed files with 150 additions and 26 deletions
@@ -163,7 +163,7 @@
width: 16px;
height: 16px;
}
.runnable {
.overlay {
position: absolute;
inset: 0;
pointer-events: none;
@@ -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();
});
+3 -1
View File
@@ -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 && <Icon name="symlink-mark" size={16} className={styles.overlay} label="Symbolic link" />}
{!!file?.runnable && icon !== "spring-boot" && icon !== "jvm-test" && (
<Icon name="runnable-mark" size={16} className={styles.runnable} />
<Icon name="runnable-mark" size={16} className={styles.overlay} />
)}
</span>
{isEditing && editing ? (
+65 -23
View File
@@ -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<MemNode, { kind: "symlink" }>;
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<string, MemNode>();
@@ -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<string, string | null> = {}, root = "/") {
constructor(files: Record<string, string | null> = {}, root = "/", links: Record<string, string> = {}) {
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<FileEntry[]> {
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<FileEntry[]> {
// 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[] }>) {
+42 -1
View File
@@ -12,7 +12,7 @@ import {
type TreeOptions,
} from "./model";
const entry = (path: string, kind: "file" | "dir" = "file", extra: Partial<FileEntry> = {}): FileEntry => {
const entry = (path: string, kind: FileEntry["kind"] = "file", extra: Partial<FileEntry> = {}): 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", () => {
+1
View File
@@ -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",
+1
View File
@@ -1,4 +1,5 @@
{
"Symbolic link": "符号链接",
"Copy as TSV": "复制为 TSV",
"Copy as CSV": "复制为 CSV",
"Copy as One-Row": "复制为单行",
+4
View File
@@ -0,0 +1,4 @@
<!-- Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M11 0.5C11 0.223858 11.2239 0 11.5 0H15.5C15.7761 0 16 0.223858 16 0.5V4.5C16 4.77614 15.7761 5 15.5 5C15.2239 5 15 4.77614 15 4.5V1.70711L10.8536 5.85355C10.6583 6.04882 10.3417 6.04882 10.1464 5.85355C9.95118 5.65829 9.95118 5.34171 10.1464 5.14645L14.2929 1H11.5C11.2239 1 11 0.776142 11 0.5Z" fill="#6C707E"/>
</svg>

After

Width:  |  Height:  |  Size: 593 B

+4
View File
@@ -0,0 +1,4 @@
<!-- Copyright 2000-2024 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license. -->
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M11 0.5C11 0.223858 11.2239 0 11.5 0H15.5C15.7761 0 16 0.223858 16 0.5V4.5C16 4.77614 15.7761 5 15.5 5C15.2239 5 15 4.77614 15 4.5V1.70711L10.8536 5.85355C10.6583 6.04882 10.3417 6.04882 10.1464 5.85355C9.95118 5.65829 9.95118 5.34171 10.1464 5.14645L14.2929 1H11.5C11.2239 1 11 0.776142 11 0.5Z" fill="#CED0D6"/>
</svg>

After

Width:  |  Height:  |  Size: 593 B

+1
View File
@@ -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",