Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions example/src/EditorApp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<FindBar
key={`${fileId}:${openCount}`}
scheme={scheme}
editor={editor}
onStatus={onStatus}
Expand Down
39 changes: 39 additions & 0 deletions example/src/__tests__/useFind.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
26 changes: 17 additions & 9 deletions example/src/chrome/useFind.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,29 @@ export function useFind(editor: RefObject<CodeEditorRef | null>, 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] })),
Expand Down
Loading