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. { 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] })),