From b864646c2d7b8d57badfcac5259f9430b2dc4ba4 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Mon, 23 Nov 2026 00:30:46 +0800 Subject: [PATCH] fix(search): keep recent find and replace values readable --- e2e/findHistoryLayout.spec.ts | 78 ++++++++++++++++++++++++ src/editor/core/view/FindBar.tsx | 24 ++------ src/features/search/find/FindInFiles.tsx | 23 ++----- src/platform/locales/text/es.json | 1 - src/platform/locales/text/fr.json | 1 - src/platform/locales/text/ko.json | 1 - src/platform/locales/text/pl.json | 1 - src/platform/locales/text/pt.json | 1 - src/platform/locales/text/tr.json | 1 - src/platform/locales/text/zh-TW.json | 1 - src/platform/locales/text/zh.json | 1 - src/ui/Menu.module.css | 2 +- src/ui/Menu.tsx | 9 ++- src/ui/Popup.tsx | 19 ++++-- src/ui/SearchHistoryMenu.tsx | 37 +++++++++++ 15 files changed, 146 insertions(+), 54 deletions(-) create mode 100644 e2e/findHistoryLayout.spec.ts create mode 100644 src/ui/SearchHistoryMenu.tsx diff --git a/e2e/findHistoryLayout.spec.ts b/e2e/findHistoryLayout.spec.ts new file mode 100644 index 000000000..745c1e496 --- /dev/null +++ b/e2e/findHistoryLayout.spec.ts @@ -0,0 +1,78 @@ +import type { Locator, Page } from "@playwright/test"; +import { expect, openFromTree, test } from "./fixtures"; + +const latest = "regional-order-reconciliation-endpoints-for-sandbox-environments/".repeat(3) + "checkout\nText to find"; + +async function seedHistory(page: Page, kind: "find" | "replace") { + await page.evaluate( + async ({ kind, latest }) => { + const searchModule = "/src/platform/search.ts"; + const { rememberSearch } = await import(/* @vite-ignore */ searchModule); + for (let i = 0; i < 19; i++) rememberSearch(kind, `Order rule ${i}`); + rememberSearch(kind, latest); + }, + { kind, latest }, + ); +} + +async function openHistory(field: Locator, menu: Locator) { + await field.focus(); + await field.press("Alt+ArrowDown"); + await expect(menu).toBeVisible(); +} + +test("editor find history grows with the field and restores the original query", async ({ workbench: page }) => { + await page.setViewportSize({ width: 1040, height: 700 }); + await openFromTree(page, "build.gradle.kts"); + await page.keyboard.press("Meta+f"); + await seedHistory(page, "find"); + + const field = page.getByRole("textbox", { name: "Find", exact: true }); + const menu = page.getByRole("menu", { name: "Recent Searches", exact: true }); + await openHistory(field, menu); + + const newest = menu.getByRole("menuitem").first(); + await expect(newest).toHaveText(latest.replace("\n", " ⏎ ")); + await expect(newest).toHaveAttribute("title", latest); + await expect.poll(async () => (await menu.boundingBox())!.width).toBeGreaterThan(420); + await newest.press("Enter"); + await expect(menu).toBeHidden(); + await expect(field).toHaveValue(latest); + await expect(field).toBeFocused(); +}); + +test("project replacement history stays inside a narrow window and keeps its heading visible", async ({ + workbench: page, +}) => { + await page.setViewportSize({ width: 440, height: 520 }); + await page.keyboard.press("Meta+Shift+r"); + await seedHistory(page, "replace"); + + const field = page.getByRole("textbox", { name: "Replace with" }); + const menu = page.getByRole("menu", { name: "Recent Replacements" }); + await openHistory(field, menu); + + const popup = menu.locator(".."); + const heading = popup.getByText("Recent Replacements", { exact: true }); + const newest = menu.getByRole("menuitem").first(); + await expect(newest).toHaveText(latest.replace("\n", " ⏎ ")); + await expect + .poll(async () => { + const { x, width } = (await popup.boundingBox())!; + return x >= 0 && x + width <= 440; + }) + .toBe(true); + + await newest.press("End"); + const oldest = menu.getByRole("menuitem").last(); + await expect(oldest).toBeFocused(); + await expect.poll(async () => popup.evaluate((node) => node.scrollTop)).toBeGreaterThan(0); + await expect(heading).toBeInViewport(); + + await oldest.press("Home"); + await expect(newest).toBeFocused(); + await newest.press("Enter"); + await expect(menu).toBeHidden(); + await expect(field).toHaveValue(latest); + await expect(field).toBeFocused(); +}); diff --git a/src/editor/core/view/FindBar.tsx b/src/editor/core/view/FindBar.tsx index ca1efc7bc..9717ca317 100644 --- a/src/editor/core/view/FindBar.tsx +++ b/src/editor/core/view/FindBar.tsx @@ -8,12 +8,13 @@ * Exclude with Preserve Case, Esc closes. `FindPanel` is the bar itself, for * any CodeMirror view (the diff viewer's sides); `FindBar` is it on an editor pane. */ +import { SearchHistoryMenu } from "@/ui/SearchHistoryMenu"; import { useEffect, useLayoutEffect, useRef, useState, type KeyboardEvent } from "react"; import { create } from "zustand"; import clsx from "clsx"; import type { EditorView } from "@codemirror/view"; import { Button, IconButton, Menu, SearchField, T, type MenuItem } from "@/ui"; -import { SEARCH_CONTEXTS, rememberSearch, searchHistory } from "@/platform/search"; +import { SEARCH_CONTEXTS, rememberSearch } from "@/platform/search"; import { editorController, useFindBars, useTabs } from "../controller"; import { countMatches, @@ -557,19 +558,6 @@ export function FindPanel({ target, version, replace, seq, onReplaceShown, onClo ? "0 results" : `${count.current || "?"}/${count.total}${count.capped ? "+" : ""}`; - const historyItems = (kind: "find" | "replace"): MenuItem[] => { - const h = searchHistory(kind); - return h.length - ? [ - { kind: "header", label: kind === "find" ? "Recent Searches" : "Recent Replacements" }, - ...h.map((q) => ({ - label: q.replace(/\n/g, "⏎"), - verbatim: true, - run: () => setOpts(kind === "find" ? { search: q } : { replace: q }), - })), - ] - : [{ label: kind === "find" ? "No recent searches" : "No recent replacements", disabled: true }]; - }; const filterItems: MenuItem[] = [ { label: "Search In Selection", @@ -765,12 +753,10 @@ export function FindPanel({ target, version, replace, seq, onReplaceShown, onClo )} - setOpts(historyMenu === "replace" ? { replace: value } : { search: value })} onClose={() => setHistoryMenu(null)} /> set({ mask: m, maskEnabled: true }), })); - const historyItems = (kind: "find" | "replace"): MenuItem[] => { - const h = findHistory(kind); - return h.length - ? [ - { kind: "header", label: kind === "find" ? "Recent Searches" : "Recent Replacements" }, - ...h.map((q) => ({ - label: q, - verbatim: true, - run: () => set(kind === "find" ? { pattern: q } : { replacement: q }), - })), - ] - : [{ label: kind === "find" ? "No recent searches" : "No recent replacements", disabled: true }]; - }; const contextItems: MenuItem[] = SEARCH_CONTEXTS.map((c) => ({ label: c.label, checked: c.value === s.context ? true : undefined, @@ -974,12 +961,10 @@ function FindInFilesDialog() { placement="bottom-end" onClose={() => setMaskMenu(false)} /> - set(historyMenu === "replace" ? { replacement: value } : { pattern: value })} onClose={() => setHistoryMenu(null)} /> {heading &&
{heading}
} void) { +function watchPopupSize(reference: Element | VirtualElement, floating: HTMLElement, update: () => void) { // Positioning writes the popup's size limits. Defer those writes outside the resize observer's delivery. let frame = 0; let placed = false; @@ -149,6 +152,7 @@ function PopupInner({ closeOnOutsideClick = true, closeOnDeactivate = true, matchWidth = false, + maxWidthOverAnchor, unstyled = false, resizable, minWidth = 200, @@ -168,19 +172,22 @@ function PopupInner({ shift({ padding: 6 }), sizeMw({ padding: 6, - apply({ rects, elements, availableHeight, availableWidth }) { + apply({ rects, elements, availableHeight, availableWidth, middlewareData }) { const floatingStyle = elements.floating.style; const maxHeight = Math.max(120, availableHeight); if (!hasInlineMaxHeight && (rects.floating.height > maxHeight || floatingStyle.maxHeight)) { floatingStyle.maxHeight = `${maxHeight}px`; + const scrollTop = middlewareData.measureNaturalHeight?.scrollTop; + if (scrollTop) elements.floating.scrollTop = scrollTop; } - const width = Math.max(0, availableWidth); + const anchorLimit = maxWidthOverAnchor === undefined ? Infinity : rects.reference.width + maxWidthOverAnchor; + const width = Math.max(0, Math.min(availableWidth, anchorLimit)); floatingStyle.maxWidth = `${width}px`; if (matchWidth) floatingStyle.minWidth = `${Math.min(rects.reference.width, width)}px`; }, }), ], - [hasInlineMaxHeight, offset, matchWidth], + [hasInlineMaxHeight, offset, matchWidth, maxWidthOverAnchor], ); const { refs, floatingStyles, isPositioned } = useFloating({ placement, @@ -188,7 +195,7 @@ function PopupInner({ transform: false, middleware, elements: { reference }, - whileElementsMounted: resizable ? watchResizablePopup : autoUpdate, + whileElementsMounted: resizable || maxWidthOverAnchor !== undefined ? watchPopupSize : autoUpdate, }); const elRef = useRef(null); const onCloseRef = useRef(onClose); diff --git a/src/ui/SearchHistoryMenu.tsx b/src/ui/SearchHistoryMenu.tsx new file mode 100644 index 000000000..cbd0b0c8a --- /dev/null +++ b/src/ui/SearchHistoryMenu.tsx @@ -0,0 +1,37 @@ +import { Menu, type MenuItem } from "./Menu"; +import type { PopupAnchor } from "./Popup"; +import { searchHistory } from "@/platform/search"; + +interface SearchHistoryMenuProps { + kind: "find" | "replace" | null; + anchor: PopupAnchor; + onSelect: (value: string) => void; + onClose: () => void; +} + +export function SearchHistoryMenu({ kind, anchor, onSelect, onClose }: SearchHistoryMenuProps) { + if (kind === null) return null; + + const replace = kind === "replace"; + const entries = searchHistory(kind); + const items: MenuItem[] = entries.length + ? entries.map((value) => ({ + label: value.replace(/\r\n?|\n/g, " ⏎ "), + hint: value, + verbatim: true, + run: () => onSelect(value), + })) + : [{ label: replace ? "No recent replacements" : "No recent searches", disabled: true }]; + + return ( + + ); +}