mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 19:11:58 +03:00
⚡ [perf: avoid expensive doc stringification for autocomplete] (#102)
* perf: avoid expensive doc stringification for autocomplete
Refactored the autocomplete variable matching logic in `src/ui/editor.ts` to iterate through document lines using CodeMirror's `iterLines()` instead of allocating the entire document string in memory with `toString()`. Also pulled the regex up and introduced a quick `.includes("=")` bailout to skip the regex engine completely for most lines.
Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com>
* drop stray benchmark script
---------
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
parent
d528aa9bbb
commit
289ae8aa3f
1 changed files with 10 additions and 3 deletions
|
|
@ -170,6 +170,7 @@ export class SumEditor {
|
||||||
this.resultsEl = resultsEl;
|
this.resultsEl = resultsEl;
|
||||||
|
|
||||||
let engineOptions: { label: string; type: string; detail?: string }[] | null = null;
|
let engineOptions: { label: string; type: string; detail?: string }[] | null = null;
|
||||||
|
const varPattern = /^\s*([\p{L}_][\p{L}\d_]*)\s*=/u;
|
||||||
const completionSource = (ctx: CompletionContext): CompletionResult | null => {
|
const completionSource = (ctx: CompletionContext): CompletionResult | null => {
|
||||||
const word = ctx.matchBefore(/[\p{L}_]+$/u);
|
const word = ctx.matchBefore(/[\p{L}_]+$/u);
|
||||||
if (!word || (word.from === word.to && !ctx.explicit)) return null;
|
if (!word || (word.from === word.to && !ctx.explicit)) return null;
|
||||||
|
|
@ -182,9 +183,15 @@ export class SumEditor {
|
||||||
}));
|
}));
|
||||||
const options = [...engineOptions];
|
const options = [...engineOptions];
|
||||||
// document variables
|
// document variables
|
||||||
const text = ctx.state.doc.toString();
|
const iter = ctx.state.doc.iterLines();
|
||||||
for (const m of text.matchAll(/^\s*([\p{L}_][\p{L}\d_]*)\s*=/gmu)) {
|
for (let next = iter.next(); !next.done; next = iter.next()) {
|
||||||
options.push({ label: m[1], type: "variable" });
|
const line = next.value;
|
||||||
|
if (line.includes("=")) {
|
||||||
|
const m = varPattern.exec(line);
|
||||||
|
if (m) {
|
||||||
|
options.push({ label: m[1], type: "variable" });
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return { from: word.from, options, validFor: /^[\p{L}_]*$/u };
|
return { from: word.from, options, validFor: /^[\p{L}_]*$/u };
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue