fix(editor): keep in-file Find responsive in large files (#4698)

CodeMirror compares a plain-text query one character at a time; a pass over a 50 MB log took ~850 ms and each find-bar keystroke runs several, freezing the editor for seconds. Plain-text queries now run as an escaped regular expression (~35 ms per pass) with a literal replacement. Find Previous no longer uses CodeMirror's quadratic backward regex search; one forward pass finds the previous match.
This commit is contained in:
l0ng-ai
2026-11-20 13:19:51 +08:00
committed by GitHub
parent 2b75b79ea0
commit 815bebf4ec
2 changed files with 65 additions and 5 deletions
+34
View File
@@ -171,6 +171,40 @@ describe("find in selection", () => {
});
});
describe("plain-text queries", () => {
it("match regex syntax and line breaks literally, case-insensitively", () => {
const v = make("a.b(c)[d]$ x A.B(C)[D]$ axb\nend\n1 + 1 = 2", { search: "a.b(c)[d]$" });
expect(countMatches(v.state).total).toBe(2);
expect(countMatches(make("one\ntwo\none two", { search: "one\ntwo" }).state).total).toBe(1);
expect(countMatches(make("1+1 11", { search: "1+1" }).state).total).toBe(1);
expect(countMatches(make("a/b a\\b", { search: "a\\b" }).state).total).toBe(1);
});
it("replace with `$` and backslashes as written", () => {
const v = make("cost: x; cost: y", { search: "cost", replace: "$& $1 $$ \\n" });
replaceAll(v);
expect(v.state.doc.toString()).toBe("$& $1 $$ \\n: x; $& $1 $$ \\n: y");
const w = make("cost", { search: "cost", replace: "$" });
replaceNext(w, true);
replaceNext(w, true);
expect(w.state.doc.toString()).toBe("$");
});
it("previous wraps from the start to the last match, skipping the selected one", () => {
const v = make("ab ab ab", { search: "ab" });
v.dispatch({ selection: { anchor: 0, head: 2 } });
findPrevious(v);
expect(sel(v)).toEqual([6, 8]);
findPrevious(v);
expect(sel(v)).toEqual([3, 5]);
const only = make("x ab", { search: "ab" });
only.dispatch({ selection: { anchor: 2, head: 4 } });
expect(findPrevious(only)).toBe(false);
expect(sel(only)).toEqual([2, 4]);
expect(findPrevious(make("x", { search: "ab" }))).toBe(false);
});
});
describe("search context, exclude and preserve case", () => {
const lang = StreamLanguage.define<null>({
token(stream) {
+31 -5
View File
@@ -17,12 +17,12 @@ import {
type ChangeDesc,
type EditorState,
type Extension,
type SelectionRange,
} from "@codemirror/state";
import { Decoration, EditorView, ViewPlugin, type DecorationSet, type ViewUpdate } from "@codemirror/view";
import {
SearchQuery,
findNext as cmFindNext,
findPrevious as cmFindPrevious,
getSearchQuery,
replaceAll as cmReplaceAll,
replaceNext as cmReplaceNext,
@@ -240,15 +240,21 @@ export interface FindQueryOptions extends FindOptions {
inChanges?: boolean;
}
/** The characters a regular expression treats as syntax (`-` only inside a class, which never opens here). */
const REGEX_SYNTAX = /[\\^$.*+?()[\]{}|/]/g;
export function makeQuery(o: FindQueryOptions) {
// CodeMirror compares a plain-text query one character at a time; the same text as an escaped
// regular expression runs on the engine's matcher, over 20× faster in a large file, so every
// keystroke in the bar stays responsive. Its replacement escapes `$` to stay literal too.
return new SearchQuery({
search: o.search,
search: o.regexp ? o.search : o.search.replace(REGEX_SYNTAX, "\\$&"),
// Plain-text search and replace are literal (`\n` is not a newline), as in IDEA.
literal: !o.regexp,
caseSensitive: o.caseSensitive,
wholeWord: o.wholeWord,
regexp: o.regexp,
replace: o.replace,
regexp: true,
replace: o.regexp ? o.replace : o.replace.replaceAll("$", "$$$$"),
test: testFor(o.context ?? "any", !!o.inChanges),
});
}
@@ -345,8 +351,28 @@ export function findNext(view: EditorView): boolean {
return view.state.field(findScope, false) ? step(view, true) : cmFindNext(view);
}
/** The last match in `from`–`to` other than `skip`. */
function lastMatch(state: EditorState, from: number, to: number, skip?: SelectionRange): Match | null {
const cursor = getSearchQuery(state).getCursor(state, from, to);
let last: Match | null = null;
for (let r = cursor.next(); !r.done; r = cursor.next())
if (!skip || r.value.from !== skip.from || r.value.to !== skip.to) last = r.value as Match;
return last;
}
/**
* The match before the selection, wrapping to the end. CodeMirror finds it by scanning ever larger
* chunks backwards, quadratic in the distance to the match; one forward pass is linear.
*/
export function findPrevious(view: EditorView): boolean {
return view.state.field(findScope, false) ? step(view, false) : cmFindPrevious(view);
const { state } = view;
if (state.field(findScope, false)) return step(view, false);
if (!getSearchQuery(state).valid) return false;
const main = state.selection.main;
const m = lastMatch(state, 0, main.from) ?? lastMatch(state, main.from, state.doc.length, main);
if (!m) return false;
selectMatch(view, m);
return true;
}
/** The text replacing `m`, keeping its case when `preserve` is set. */