fix(search): keep search history per project (#5829)

Search Everywhere history and the find / replace history shared by the
editor's find bar and Find in Files were kept in one store for every
project, so one project's class names and search strings showed up in
another project's history dropdowns.

Each project now keeps its own history, keyed by its root (IntelliJ IDEA
keeps it in the project's workspace); a window without a project keeps the
shared one. File masks stay one list for all projects, as in IDEA. Earlier
history isn't carried over, since it can't be told apart by project.

Fixes #5758
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-25 16:23:06 +08:00
1 джерело b748a4cfbc
коміт 9ca7a297e1
4 files changed
+47 -6

No files matched your search

+18 -1
Переглянути файл
@@ -2,6 +2,8 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { act, fireEvent, render, screen, within } from "@testing-library/react";
import { registerAction } from "@/platform/actions";
import { clearContext, getContext, setContext, type ContextKeys } from "@/platform/context";
import { useProject } from "@/platform/project";
import { projectKey } from "@/platform/search";
import type { SeDeps } from "../everywhere/controller";
const defaultFindFiles: SeDeps["findFiles"] = async (q) =>
@@ -168,7 +170,7 @@ describe("Search Everywhere dialog", () => {
});
it("⌥↑ / ⌥↓ browse the tab's search history and restore the draft", async () => {
localStorage.removeItem("openidea.search.everywhereHistory.v1");
localStorage.removeItem(projectKey("openidea.search.everywhereHistory.v1"));
pushSearchHistory("all", "older");
pushSearchHistory("all", "newer");
pushSearchHistory("files", "other tab");
@@ -188,6 +190,21 @@ describe("Search Everywhere dialog", () => {
expect(searchHistory("all")).toEqual(["newer", "older"]);
});
it("keeps each project's Search Everywhere history apart (#5758)", () => {
const before = useProject.getState().project;
const open = (root: string) => useProject.setState({ project: { root, name: "p", isGit: false } as never });
try {
open("/work/a");
pushSearchHistory("all", "FooService");
open("/work/b");
expect(searchHistory("all")).toEqual([]);
open("/work/a");
expect(searchHistory("all")).toEqual(["FooService"]);
} finally {
useProject.setState({ project: before });
}
});
it("⇥ cycles scopes and repeating the scope action toggles non-project items", async () => {
render(<SearchEverywhereOverlay />);
act(() => showSearchEverywhere("all"));
+4 -2
Переглянути файл
@@ -1,5 +1,6 @@
import { create } from "zustand";
import { getContext, type ContextKeys } from "@/platform/context";
import { projectKey } from "@/platform/search";
import { loadJson, saveJson } from "../common/open";
import type { ProjectFileSet } from "@/platform/ipc";
import type { SeTab } from "./model";
@@ -135,8 +136,9 @@ interface HistoryEntry {
query: string;
}
/** The open project's history: it names the project's own symbols and files. */
function loadHistory(): HistoryEntry[] {
const v = loadJson<unknown>(HISTORY_KEY, []);
const v = loadJson<unknown>(projectKey(HISTORY_KEY), []);
return Array.isArray(v)
? v.filter((x): x is HistoryEntry => typeof x?.tab === "string" && typeof x?.query === "string")
: [];
@@ -153,5 +155,5 @@ export function pushSearchHistory(tab: SeTab, query: string) {
const q = query.trim();
if (!q) return;
const rest = loadHistory().filter((e) => e.tab !== tab || e.query !== q);
saveJson(HISTORY_KEY, [{ tab, query: q }, ...rest].slice(0, MAX_HISTORY));
saveJson(projectKey(HISTORY_KEY), [{ tab, query: q }, ...rest].slice(0, MAX_HISTORY));
}
+13
Переглянути файл
@@ -39,6 +39,19 @@ describe("shared search pieces", () => {
expect(searchHistory("find")).toEqual(["a", "b\nc"]);
});
it("keeps each project's history apart (#5758)", () => {
openProject("/work/a");
rememberSearch("find", "FooService");
rememberSearch("replace", "BarService");
openProject("/work/b");
expect(searchHistory("find")).toEqual([]);
rememberSearch("find", "parse_args");
expect(searchHistory("find")).toEqual(["parse_args"]);
openProject("/work/a");
expect(searchHistory("find")).toEqual(["FooService"]);
expect(searchHistory("replace")).toEqual(["BarService"]);
});
it("custom scopes are saved per project", () => {
openProject("/p1");
saveCustomScopes([
+12 -3
Переглянути файл
@@ -68,18 +68,27 @@ function save(key: string, value: unknown) {
}
}
/**
* `key` for the open project: search history names the project's identifiers, so each project
* keeps its own (IDEA stores it in the project's workspace). Without a project, `key` itself.
*/
export function projectKey(key: string): string {
const root = useProject.getState().project?.root;
return root ? `${key}:${root}` : key;
}
const HISTORY_KEYS = { find: "openidea.search.findHistory.v1", replace: "openidea.search.replaceHistory.v1" };
const MAX_HISTORY = 20;
/** Recent search (or replace) strings, newest first; shared by the find bar and Find in Files. */
/** Recent search (or replace) strings of the open project, newest first; shared by the find bar and Find in Files. */
export function searchHistory(kind: "find" | "replace"): string[] {
const v = load(HISTORY_KEYS[kind]);
const v = load(projectKey(HISTORY_KEYS[kind]));
return Array.isArray(v) ? v.filter((x): x is string => typeof x === "string") : [];
}
export function rememberSearch(kind: "find" | "replace", text: string) {
if (!text.trim()) return;
save(HISTORY_KEYS[kind], [text, ...searchHistory(kind).filter((x) => x !== text)].slice(0, MAX_HISTORY));
save(projectKey(HISTORY_KEYS[kind]), [text, ...searchHistory(kind).filter((x) => x !== text)].slice(0, MAX_HISTORY));
}
/**