From e4d6f50a0b39273df1d079eeabdac8ec64ce7052 Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Mon, 23 Nov 2026 09:12:56 +0800 Subject: [PATCH] fix(search): make Replace All details easier to review (#5236) Replace All puts the affected count above separate, scrollable values for the search text and replacement. Both values stay available in full, and an empty replacement is labeled explicitly. The Replace in Files popup and the Find tool window use the same confirmation. --- e2e/replaceAllConfirmationLayout.spec.ts | 75 +++++++++++++++++++ .../search/__tests__/replaceFlow.test.ts | 6 +- src/features/search/find/FindInFiles.tsx | 25 ++----- src/features/search/find/FindToolWindow.tsx | 25 ++----- .../find/ReplaceAllConfirmation.module.css | 51 +++++++++++++ .../search/find/ReplaceAllConfirmation.tsx | 59 +++++++++++++++ src/features/search/find/replaceFlow.ts | 13 +--- src/platform/locales/text/en.json | 10 ++- src/platform/locales/text/tr.json | 3 +- src/platform/locales/text/zh-TW.json | 3 +- src/platform/locales/text/zh.json | 7 +- 11 files changed, 223 insertions(+), 54 deletions(-) create mode 100644 e2e/replaceAllConfirmationLayout.spec.ts create mode 100644 src/features/search/find/ReplaceAllConfirmation.module.css create mode 100644 src/features/search/find/ReplaceAllConfirmation.tsx diff --git a/e2e/replaceAllConfirmationLayout.spec.ts b/e2e/replaceAllConfirmationLayout.spec.ts new file mode 100644 index 000000000..c5b5c0224 --- /dev/null +++ b/e2e/replaceAllConfirmationLayout.spec.ts @@ -0,0 +1,75 @@ +import type { SearchQuery } from "../src/bindings"; +import { expect, ipcCalls, stubDesktop, test } from "./fixtures"; + +test("Replace All keeps both values readable and the actions visible", async ({ workbench: page }) => { + const pattern = `LEGACY_${"MONTH_END_RECONCILIATION_INPUT_".repeat(15)}END`; + const replacement = `ACTIVE_${"MONTH_END_RECONCILIATION_OUTPUT_".repeat(18)}END`; + await page.emulateMedia({ reducedMotion: "reduce" }); + await page.setViewportSize({ width: 1040, height: 700 }); + await stubDesktop(page); + await page.evaluate((pattern) => { + window.__ipc.handlers.search_validate = () => null; + window.__ipc.handlers.search_start = ({ query, onEvent }) => { + if ((query as SearchQuery).pattern !== pattern) return null; + setTimeout(() => { + for (const relPath of ["invoice.txt", "receipt.txt"]) { + window.__ipc.send(onEvent, { + type: "file", + file: { + path: `/dev/acme-api/${relPath}`, + relPath, + matches: [{ line: 0, text: pattern, ranges: [[0, pattern.length]], textOffset: 0 }], + }, + }); + } + window.__ipc.send(onEvent, { + type: "done", + summary: { files: 2, matches: 2, limitHit: false, cancelled: false, elapsedMs: 1 }, + }); + }, 20); + return null; + }; + }, pattern); + await page.keyboard.press("Meta+Shift+r"); + const popup = page.getByRole("dialog", { name: "Replace in Files", exact: true }); + await popup.getByRole("textbox", { name: "Text to find", exact: true }).fill(pattern); + const replacementInput = popup.getByRole("textbox", { name: "Replace with", exact: true }); + await replacementInput.fill(replacement); + await expect(popup.getByRole("listbox", { name: "Matches" }).getByRole("option")).toHaveCount(2); + await page.setViewportSize({ width: 440, height: 520 }); + await popup.getByRole("button", { name: "Replace All", exact: true }).click(); + + const dialog = page.getByRole("dialog", { name: "Replace All", exact: true }); + const findText = dialog.getByRole("region", { name: "Text to find", exact: true }); + const replaceText = dialog.getByRole("region", { name: "Replace with", exact: true }); + const replace = dialog.getByRole("button", { name: "Replace", exact: true }); + const cancel = dialog.getByRole("button", { name: "Cancel", exact: true }); + await expect(findText).toHaveText(pattern); + await expect(replaceText).toHaveText(replacement); + await expect(dialog.getByText("Replace 2 occurrences in 2 files?", { exact: true })).toBeVisible(); + const bottom = (await replace.boundingBox())!.y; + for (const item of [findText, replaceText]) { + await expect(item).toHaveCSS("font-size", "13px"); + expect(await item.evaluate((el) => el.scrollHeight > el.clientHeight && el.scrollWidth === el.clientWidth)).toBe( + true, + ); + await item.focus(); + await page.keyboard.press("End"); + await expect.poll(() => item.evaluate((el) => el.scrollTop)).toBeGreaterThan(0); + } + expect((await replace.boundingBox())!.y).toBeCloseTo(bottom, 0); + await expect(replace).toBeInViewport({ ratio: 1 }); + await expect(cancel).toBeInViewport({ ratio: 1 }); + await cancel.click(); + await expect(dialog).toBeHidden(); + + await page.setViewportSize({ width: 1040, height: 700 }); + await replacementInput.fill(""); + await popup.getByRole("button", { name: "Open in Find Window", exact: true }).click(); + const findWindow = page.getByRole("region", { name: "Find", exact: true }); + await findWindow.getByRole("button", { name: "Replace All", exact: true }).click(); + await expect(findText).toHaveText(pattern); + await expect(replaceText).toHaveText("Empty text"); + await cancel.click(); + expect(await ipcCalls(page, "replace_in_files")).toHaveLength(0); +}); diff --git a/src/features/search/__tests__/replaceFlow.test.ts b/src/features/search/__tests__/replaceFlow.test.ts index 2a91c2b7d..a20d7fe06 100644 --- a/src/features/search/__tests__/replaceFlow.test.ts +++ b/src/features/search/__tests__/replaceFlow.test.ts @@ -103,9 +103,9 @@ describe("replace flow model", () => { }); it("confirmation and summary texts carry the counts", () => { - expect(confirmText({ usages: 3, files: 1 }, "foo", "bar")).toBe( - "Replace 3 occurrences of 'foo' with 'bar' in 1 file?", - ); + expect(confirmText({ usages: 1, files: 1 })).toBe("Replace 1 occurrence in 1 file?"); + expect(confirmText({ usages: 3, files: 1 })).toBe("Replace 3 occurrences in 1 file?"); + expect(confirmText({ usages: 3, files: 2 })).toBe("Replace 3 occurrences in 2 files?"); expect(summaryText({ files: 2, replacements: 1, failed: 0, cancelled: false, elapsedMs: 3 })).toBe( "Replaced 1 occurrence in 2 files", ); diff --git a/src/features/search/find/FindInFiles.tsx b/src/features/search/find/FindInFiles.tsx index f0db413ab..a525b510b 100644 --- a/src/features/search/find/FindInFiles.tsx +++ b/src/features/search/find/FindInFiles.tsx @@ -39,14 +39,12 @@ import { } from "@/ui"; import { useRowHeight } from "@/ui/density"; import { maybeEditorService, notifications } from "@/workbench/contributions"; -import { confirm } from "@/workbench/messages"; import { basename, dirname, openLocation } from "../common/open"; import { CodePreview, MatchText, ReplaceDiffPreview } from "./parts"; import { clearPreviewCache } from "./preview"; +import { confirmReplacement } from "./ReplaceAllConfirmation"; import { NO_MARKS, - confirmAllText, - confirmText, countUsages, groupState, loadReplacePreview, @@ -346,22 +344,11 @@ function FindInFilesDialog() { void replaceAll(usages); return; } - const ok = await confirm({ - title: "Replace All", - ok: "Replace", - message: ( - <> - {allInScope - ? confirmAllText(s.pattern, s.replacement || "") - : confirmText(counts, s.pattern, s.replacement || "")} - {marks.excluded.size > 0 && ( - <> - {" "} - Excluded usages are skipped. - - )} - - ), + const ok = await confirmReplacement({ + counts: allInScope ? null : counts, + pattern: s.pattern, + replacement: s.replacement || "", + hasExclusions: marks.excluded.size > 0, }); if (ok) void replaceAll(usages, allInScope); }; diff --git a/src/features/search/find/FindToolWindow.tsx b/src/features/search/find/FindToolWindow.tsx index b0409f901..b3a8aa128 100644 --- a/src/features/search/find/FindToolWindow.tsx +++ b/src/features/search/find/FindToolWindow.tsx @@ -23,14 +23,12 @@ import { T, } from "@/ui"; import { notifications, useOccurrenceNavigator } from "@/workbench/contributions"; -import { confirm } from "@/workbench/messages"; import { basename, openLocation, relDir } from "../common/open"; import { countFileMatches } from "./session"; import { CodePreview, MatchText, type PreviewHighlight } from "./parts"; +import { confirmReplacement } from "./ReplaceAllConfirmation"; import { NO_MARKS, - confirmAllText, - confirmText, countUsages, groupState, loadReplacePreview, @@ -369,22 +367,11 @@ function FindTabView({ tab }: { tab: FindTab }) { // Past the search's limit, every match in the scope is replaced, listed or not (as in IDEA). const allInScope = !!st.summary?.limitHit; const confirmReplaceAll = async () => { - const ok = await confirm({ - title: "Replace All", - ok: "Replace", - message: ( - <> - {allInScope - ? confirmAllText(tab.query.pattern, tab.replacement ?? "") - : confirmText(countUsages(allPending), tab.query.pattern, tab.replacement ?? "")} - {marks.excluded.size > 0 && ( - <> - {" "} - Excluded usages are skipped. - - )} - - ), + const ok = await confirmReplacement({ + counts: allInScope ? null : countUsages(allPending), + pattern: tab.query.pattern, + replacement: tab.replacement ?? "", + hasExclusions: marks.excluded.size > 0, }); if (ok) void replace(allPending, allInScope); }; diff --git a/src/features/search/find/ReplaceAllConfirmation.module.css b/src/features/search/find/ReplaceAllConfirmation.module.css new file mode 100644 index 000000000..b4dc71540 --- /dev/null +++ b/src/features/search/find/ReplaceAllConfirmation.module.css @@ -0,0 +1,51 @@ +.content { + display: grid; + gap: var(--space-2); + font-size: var(--fs-ui); +} + +.content p, +.values, +.values dd, +.values pre { + margin: 0; +} + +.values { + display: grid; + grid-template-columns: auto minmax(0, 1fr); + align-items: start; + gap: var(--space-2); +} + +.values dt { + padding-block: var(--space-1); + color: var(--fg-secondary); +} + +.values pre { + max-height: 5em; + padding: var(--space-1) var(--space-2); + overflow: auto; + border: 1px solid var(--border); + border-radius: var(--radius-control); + background: var(--bg-field); + font-family: var(--font-mono); + font-size: inherit; + white-space: pre-wrap; + overflow-wrap: anywhere; +} + +.values pre:focus-visible { + outline: 1px solid var(--focus-ring); +} + +.empty { + color: var(--fg-muted); + font-family: var(--font-ui); +} + +.note { + color: var(--fg-muted); + font-size: var(--fs-small); +} diff --git a/src/features/search/find/ReplaceAllConfirmation.tsx b/src/features/search/find/ReplaceAllConfirmation.tsx new file mode 100644 index 000000000..8aa52e622 --- /dev/null +++ b/src/features/search/find/ReplaceAllConfirmation.tsx @@ -0,0 +1,59 @@ +import { tx } from "@/platform/i18n"; +import { T } from "@/ui"; +import { confirm } from "@/workbench/messages"; +import { confirmText, type ReplaceCounts } from "./replaceFlow"; +import styles from "./ReplaceAllConfirmation.module.css"; + +export function confirmReplacement({ + counts, + pattern, + replacement, + hasExclusions, +}: { + counts: ReplaceCounts | null; + pattern: string; + replacement: string; + hasExclusions: boolean; +}): Promise { + return confirm({ + title: "Replace All", + ok: "Replace", + message: ( +
+

{counts ? confirmText(counts) : Replace every occurrence in the scope?}

+
+
+ Text to find +
+
+
+              {pattern}
+            
+
+
+ Replace with +
+
+
+              {replacement || (
+                
+                  Empty text
+                
+              )}
+            
+
+
+ {!counts && ( +

+ The search hit its limit, so it runs again to find any additional matches. +

+ )} + {hasExclusions && ( +

+ Excluded usages are skipped. +

+ )} +
+ ), + }); +} diff --git a/src/features/search/find/replaceFlow.ts b/src/features/search/find/replaceFlow.ts index 32b2edd4b..dc0377308 100644 --- a/src/features/search/find/replaceFlow.ts +++ b/src/features/search/find/replaceFlow.ts @@ -91,15 +91,10 @@ export function countUsages(usages: readonly Usage[]): ReplaceCounts { } /** "Replace 3 occurrences in 2 files" for the confirmation dialog. */ -export function confirmText(c: ReplaceCounts, pattern: string, replacement: string): string { - const occ = `${c.usages} occurrence${c.usages === 1 ? "" : "s"}`; - const files = `${c.files} file${c.files === 1 ? "" : "s"}`; - return `Replace ${occ} of '${pattern}' with '${replacement}' in ${files}?`; -} - -/** Confirmation for Replace All once the search hit its limit (the count shown is not the total). */ -export function confirmAllText(pattern: string, replacement: string): string { - return tx`Replace every occurrence of '${pattern}' with '${replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.`; +export function confirmText({ usages, files }: ReplaceCounts): string { + if (usages === 1) return tx("Replace 1 occurrence in 1 file?"); + if (files === 1) return tx`Replace ${usages} occurrences in 1 file?`; + return tx`Replace ${usages} occurrences in ${files} files?`; } /** Toggles exclusion of every usage in `groups` (all excluded → include them again). */ diff --git a/src/platform/locales/text/en.json b/src/platform/locales/text/en.json index b2609c7e5..25252ee4d 100644 --- a/src/platform/locales/text/en.json +++ b/src/platform/locales/text/en.json @@ -3185,7 +3185,15 @@ "Stacked": "Stacked", "Resize plugin panes": "Resize plugin panes", "Run a Python script or module": "Run a Python script or module", - "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.", + "Replace 1 occurrence in 1 file?": "Replace 1 occurrence in 1 file?", + "Replace {usages} occurrences in 1 file?": "Replace {usages} occurrences in 1 file?", + "Replace {usages} occurrences in {files} files?": "Replace {usages} occurrences in {files} files?", + "Replace every occurrence in the scope?": "Replace every occurrence in the scope?", + "The search hit its limit, so it runs again to find any additional matches.": "The search hit its limit, so it runs again to find any additional matches.", + "Text to find": "Text to find", + "Replace with": "Replace with", + "Empty text": "Empty text", + "Excluded usages are skipped.": "Excluded usages are skipped.", "Server default": "Server default", "New Database": "New Database", "Owner:": "Owner:", diff --git a/src/platform/locales/text/tr.json b/src/platform/locales/text/tr.json index 9d2bf18d7..9c6bb55e4 100644 --- a/src/platform/locales/text/tr.json +++ b/src/platform/locales/text/tr.json @@ -2588,7 +2588,8 @@ "Resize Markdown preview": "Markdown önizlemesini yeniden boyutlandır", "Resize plugin panes": "Eklenti bölmelerini yeniden boyutlandır", "Run a Python script or module": "Python betiği veya modülü çalıştır", - "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "Kapsamdaki tüm '{pattern}' eşleşmeleri '{replacement}' ile değiştirilsin mi? Arama sınıra ulaştığı için listelenmeyen eşleşmeleri bulmak üzere yeniden çalıştırılacak.", + "Replace every occurrence in the scope?": "Kapsamdaki tüm eşleşmeler değiştirilsin mi?", + "The search hit its limit, so it runs again to find any additional matches.": "Arama sınıra ulaştığı için kalan eşleşmeleri bulmak üzere yeniden çalıştırılacak.", "Server default": "Sunucu varsayılanı", "New Database": "Yeni veritabanı", "Owner:": "Sahip:", diff --git a/src/platform/locales/text/zh-TW.json b/src/platform/locales/text/zh-TW.json index af0376ef5..b7ce58108 100644 --- a/src/platform/locales/text/zh-TW.json +++ b/src/platform/locales/text/zh-TW.json @@ -3391,7 +3391,8 @@ "Group By": "分組依據", "Show on Double-Click": "雙擊時顯示", "Follow the Interface": "跟隨介面", - "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "要在整個範圍內把所有「{pattern}」取代為「{replacement}」嗎?搜尋已達上限,將重新搜尋以找出未列出的相符項目。", + "Replace every occurrence in the scope?": "要取代整個範圍內的所有相符項目嗎?", + "The search hit its limit, so it runs again to find any additional matches.": "搜尋已達上限,將重新搜尋以找出其餘相符項目。", "Review": "審查", "Commit and Push…": "提交併推送…", "Language server": "語言伺服器", diff --git a/src/platform/locales/text/zh.json b/src/platform/locales/text/zh.json index ab58d02e4..8bf8b62a6 100644 --- a/src/platform/locales/text/zh.json +++ b/src/platform/locales/text/zh.json @@ -3820,7 +3820,12 @@ "Group By": "分组依据", "Show on Double-Click": "双击时显示", "Follow the Interface": "跟随界面", - "Replace every occurrence of '{pattern}' with '{replacement}' in the scope? The search hit its limit, so it runs again to find the occurrences not listed.": "要在整个范围内把所有「{pattern}」替换为「{replacement}」吗?搜索已达上限,将重新搜索以找出未列出的匹配项。", + "Replace 1 occurrence in 1 file?": "替换 1 个文件中的 1 处匹配项吗?", + "Replace {usages} occurrences in 1 file?": "替换 1 个文件中的 {usages} 处匹配项吗?", + "Replace {usages} occurrences in {files} files?": "替换 {files} 个文件中的 {usages} 处匹配项吗?", + "Replace every occurrence in the scope?": "替换整个范围内的所有匹配项吗?", + "The search hit its limit, so it runs again to find any additional matches.": "搜索已达上限,将重新搜索以找出其余匹配项。", + "Empty text": "空文本", "Review": "审查", "Commit and Push…": "提交并推送…", "Language server": "语言服务器",