diff --git a/src/features/search/__tests__/dialogs.test.tsx b/src/features/search/__tests__/dialogs.test.tsx index a24b22d45a..99f15d3d4d 100644 --- a/src/features/search/__tests__/dialogs.test.tsx +++ b/src/features/search/__tests__/dialogs.test.tsx @@ -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(); act(() => showSearchEverywhere("all")); diff --git a/src/features/search/everywhere/store.ts b/src/features/search/everywhere/store.ts index 65be305b2e..4d54ee5543 100644 --- a/src/features/search/everywhere/store.ts +++ b/src/features/search/everywhere/store.ts @@ -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(HISTORY_KEY, []); + const v = loadJson(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)); } diff --git a/src/platform/search.test.ts b/src/platform/search.test.ts index c46228f099..495b3d4eda 100644 --- a/src/platform/search.test.ts +++ b/src/platform/search.test.ts @@ -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([ diff --git a/src/platform/search.ts b/src/platform/search.ts index 172345198e..188654a27f 100644 --- a/src/platform/search.ts +++ b/src/platform/search.ts @@ -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)); } /**