fix(search): list scratch files in Recent Files (#4337)

Recent Files only recorded paths inside the project, so scratch files never appeared in ⌘E. Files outside the project are now recorded and persisted by absolute path; the popup and Switcher show files under the Scratches root as Scratches/<path>.
This commit is contained in:
l0ng-ai
2026-11-18 22:30:54 +08:00
committed by GitHub
parent a5093c0cd1
commit e4ece2bd8d
7 changed files with 99 additions and 23 deletions
@@ -132,6 +132,23 @@ describe("recent files switcher", () => {
).toEqual(["README.md"]);
});
it("shows a scratch file under its root's label", () => {
const labelled = [{ path: "/data/scratches", label: "Scratches" }];
const items = filterRecentFiles(
["/data/scratches/api/generated-requests.http", "/p/src/Main.kt", "/elsewhere/a.txt"],
"",
{
root: "/p",
labelled,
},
);
expect(items.map((f) => [f.rel, f.dir])).toEqual([
["Scratches/api/generated-requests.http", "Scratches/api"],
["src/Main.kt", "src"],
["/elsewhere/a.txt", "/elsewhere"],
]);
});
it("preselects the previous file when the current one is first", () => {
const items = filterRecentFiles(paths, "", { root: "/p" });
expect(initialFileIndex(items, "/p/src/Main.kt", "")).toBe(1);
+12 -3
View File
@@ -25,6 +25,7 @@ import {
maybeEditorService,
toolWindowService,
useContributions,
useProjectViewRoots,
useFileDecoration,
type OpenEditorTab,
type ToolWindowContribution,
@@ -91,6 +92,14 @@ function RecentFilesDialog() {
const step = useRecentPopup((s) => s.step);
const edited = useTracking((s) => s.edited);
const root = useProject((s) => s.project?.root ?? null);
const viewRoots = useProjectViewRoots((s) => s.roots);
const labelled = useMemo(
() =>
[...viewRoots.values()].flatMap((r) =>
r.scope === "scratches" && r.path ? [{ path: r.path, label: r.label }] : [],
),
[viewRoots],
);
const toolWindows = useContributions((s) => s.toolWindows);
const [paths, setPaths] = useState<string[] | null>(null);
// Switcher: the open editor tabs (files and others), when the editor service lists them.
@@ -135,13 +144,13 @@ function RecentFilesDialog() {
const files = useMemo(
() =>
tabs
? switcherItems(paths ?? [], tabs, query, root).filter((f) => !removed.has(f.path))
? switcherItems(paths ?? [], tabs, query, root, labelled).filter((f) => !removed.has(f.path))
: filterRecentFiles(
(paths ?? []).filter((p) => !removed.has(p)),
query,
{ root, edited, editedOnly },
{ root, labelled, edited, editedOnly },
),
[paths, tabs, removed, query, root, edited, editedOnly],
[paths, tabs, removed, query, root, labelled, edited, editedOnly],
);
const fileRows = useMemo(() => files.map((f) => ({ ...f, key: f.path })), [files]);
const tools = useMemo(() => switcherToolWindows(toolWindows.values(), !!root, query), [toolWindows, root, query]);
+21 -3
View File
@@ -18,10 +18,27 @@ export interface RecentFileItem {
editor?: { input: EditorInput; icon: string };
}
/** A folder outside the project shown by its label (the Scratches root): `Scratches/requests.http`. */
export interface LabelledRoot {
path: string;
label: string;
}
function displayPath(path: string, root: string | null, labelled: readonly LabelledRoot[] = []): string {
if (root && path.startsWith(root + "/")) return path.slice(root.length + 1);
const outside = labelled.find((r) => path.startsWith(r.path + "/"));
return outside ? `${outside.label}/${path.slice(outside.path.length + 1)}` : path;
}
export function filterRecentFiles(
paths: readonly string[],
query: string,
opts: { root: string | null; edited?: ReadonlySet<string>; editedOnly?: boolean },
opts: {
root: string | null;
labelled?: readonly LabelledRoot[];
edited?: ReadonlySet<string>;
editedOnly?: boolean;
},
): RecentFileItem[] {
const out: RecentFileItem[] = [];
const seen = new Set<string>();
@@ -29,7 +46,7 @@ export function filterRecentFiles(
if (seen.has(path)) continue;
seen.add(path);
if (opts.editedOnly && !opts.edited?.has(path)) continue;
const rel = opts.root && path.startsWith(opts.root + "/") ? path.slice(opts.root.length + 1) : path;
const rel = displayPath(path, opts.root, opts.labelled);
const name = basename(rel);
let indices: number[] = [];
if (query.trim()) {
@@ -70,6 +87,7 @@ export function switcherItems(
tabs: readonly OpenEditorTab[],
query: string,
root: string | null,
labelled: readonly LabelledRoot[] = [],
): RecentFileItem[] {
const fileTabs = new Map(tabs.filter(isFileTab).map((t) => [t.input.path!, t]));
const ordered = [
@@ -83,7 +101,7 @@ export function switcherItems(
const out: RecentFileItem[] = [];
for (const t of ordered) {
if (isFileTab(t)) {
out.push(...filterRecentFiles([t.input.path!], query, { root }));
out.push(...filterRecentFiles([t.input.path!], query, { root, labelled }));
continue;
}
let indices: number[] = [];