From 131beebadbbb069d3feaaefe392a949a226a7f02 Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 18 Nov 2026 16:20:00 +0100 Subject: [PATCH 1/2] fix(example): keep the find count right when the replacement still matches Replacing a case-insensitive "import" with "IMPORT" showed "2 of 3" while 4 matches were painted, since the bar subtracted one from the count after each replace. Replace all showed "no results" over the same painted matches. The bar now reads the selected match and the total from the editor after a replace, and the count again after a replace all. --- example/src/__tests__/useFind.test.ts | 39 +++++++++++++++++++++++++++ example/src/chrome/useFind.ts | 26 +++++++++++------- 2 files changed, 56 insertions(+), 9 deletions(-) diff --git a/example/src/__tests__/useFind.test.ts b/example/src/__tests__/useFind.test.ts index 68df03a4..43f789a9 100644 --- a/example/src/__tests__/useFind.test.ts +++ b/example/src/__tests__/useFind.test.ts @@ -43,6 +43,45 @@ test('an invalid regex reports and keeps the query', async () => { expect(result.current.match).toEqual({ index: 0, count: 0 }); }); +// Case-insensitive import replaced by IMPORT: all 4 matches stay, so the count stays 4. +const stillMatching = () => { + const api = { + ...editor(), + setSearch: jest.fn(async () => ({ count: 4 })), + findNext: jest.fn(async () => ({ index: 0, total: 4 })), + replaceAll: jest.fn(async () => 4), + getSelection: jest.fn(() => ({ anchor: 20, head: 26 })), + setSelection: jest.fn(async () => {}), + }; + return api; +}; + +test('replacing one match with text that still matches keeps the count and moves to the next', async () => { + const api = stillMatching(); + const ref = { current: api as never }; + const { result } = await renderHook(() => useFind(ref, jest.fn())); + await act(() => { + result.current.setQuery('import'); + }); + await waitFor(() => expect(result.current.match).toEqual({ index: 1, count: 4 })); + api.findNext.mockResolvedValueOnce({ index: 1, total: 4 }); + await act(() => result.current.replaceOne()); + expect(result.current.match).toEqual({ index: 2, count: 4 }); + expect(api.setSelection).toHaveBeenCalledWith({ anchor: 20, head: 20 }); +}); + +test('replace all keeps the count of matches the new text still has', async () => { + const api = stillMatching(); + const ref = { current: api as never }; + const { result } = await renderHook(() => useFind(ref, jest.fn())); + await act(() => { + result.current.setQuery('import'); + }); + await waitFor(() => expect(result.current.match).toEqual({ index: 1, count: 4 })); + await act(() => result.current.replaceAll()); + expect(result.current.match).toEqual({ index: 0, count: 4 }); +}); + // A file switch remounts the editor under an open bar; Done has to clear the instance mounted last. test('unmounting clears the painted matches on the editor mounted last', async () => { const first = editor(); diff --git a/example/src/chrome/useFind.ts b/example/src/chrome/useFind.ts index f58be480..91767bd6 100644 --- a/example/src/chrome/useFind.ts +++ b/example/src/chrome/useFind.ts @@ -62,21 +62,29 @@ export function useFind(editor: RefObject, onStatus: (text [editor], ); - // replaceCurrent selects the next match, which takes over the replaced index unless it was the last one. + // replaceCurrent selects the next match. Finding again from its start reads its index and the + // fresh total, which a replacement that still matches keeps. const replaceOne = useCallback(async () => { - const replaced = await editor.current?.replaceCurrent(replacement); - if (replaced) { - const left = Math.max(0, match.count - 1); - setMatch({ index: left === 0 ? 0 : match.index > left ? 1 : match.index, count: left }); + const ed = editor.current; + const replaced = await ed?.replaceCurrent(replacement); + if (ed && replaced) { + const { anchor, head } = ed.getSelection(); + const start = Math.min(anchor, head); + await ed.setSelection({ anchor: start, head: start }); + const hit = await ed.findNext(); + setMatch(hit ? { index: hit.index + 1, count: hit.total } : NO_MATCH); } status.current(replaced ? 'replaced one' : 'select a match first'); - }, [editor, replacement, match]); + }, [editor, replacement]); + // The search stays on after a replace all, so the count is read again for the new text. const replaceAll = useCallback(async () => { - const n = (await editor.current?.replaceAll(replacement)) ?? 0; - setMatch(NO_MATCH); + const ed = editor.current; + const n = (await ed?.replaceAll(replacement)) ?? 0; + const fresh = query ? await ed?.setSearch({ text: query, ...options }).catch(() => null) : null; + setMatch(fresh?.count ? { index: 0, count: fresh.count } : NO_MATCH); status.current(`replaced ${n}`); - }, [editor, replacement]); + }, [editor, replacement, query, options]); const toggle = useCallback( (option: FindOption) => setOptions((o) => ({ ...o, [option]: !o[option] })), From 5012830b71be31041d16e5d990a9b1ca54d477cb Mon Sep 17 00:00:00 2001 From: Rayen Sbai Date: Wed, 18 Nov 2026 16:20:06 +0100 Subject: [PATCH 2/2] fix(example): reset the find bar when the editor opens another file --- example/src/EditorApp.tsx | 2 ++ 1 file changed, 2 insertions(+) diff --git a/example/src/EditorApp.tsx b/example/src/EditorApp.tsx index 9775e233..43da8cbd 100644 --- a/example/src/EditorApp.tsx +++ b/example/src/EditorApp.tsx @@ -208,7 +208,9 @@ export function EditorApp({ themeId, onTheme, onNested }: Props) { onTheme={onTheme} /> {findOpen ? ( + // Keyed like the editor, so opening another file remounts the bar with no stale query or count.