Skip to content

Commit

Permalink
perf: keep the grammar state of the last editing line
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Oct 8, 2024
1 parent ab7981f commit c00f6fc
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ export function createPlainShiki(shiki: HighlighterCore) {
for (let i = start; i < length; i++) {
patch([], loadLines[i]?.loads ?? []);
}
loadLines.length = end;
loadLines.fill(null!, start, end);
loadLines.push(...chunk);
const lastGrammarState = loadLines.at(-1)?.lastGrammarState;
loadLines.length = end - 1;
loadLines.fill(null!, start, end - 1);
loadLines.push(createLoadLine({ lastGrammarState }), ...chunk);

let offset = textLines.slice(0, start).reduce((res, text) => res + text.length + 1, 0);
const findNodeAndOffset = createFindNodeAndOffset(innerText, textNodes, offset);
Expand All @@ -156,9 +157,9 @@ export function createPlainShiki(shiki: HighlighterCore) {
loads.push({ token, range });
}

const loadLine = loadLines[i] ??= {} as LoadLine;
const loadLine = loadLines[i] ??= createLoadLine();

patch(loads, loadLine.loads ?? []);
patch(loads, loadLine.loads);
loadLine.loads = loads;
loadLine.text = text;

Expand Down Expand Up @@ -215,6 +216,15 @@ function collectTextNodes(el: HTMLElement) {
return textNodes;
}

function createLoadLine(options: Partial<LoadLine> = {}) {
return {
text: "",
lastGrammarState: void 0,
loads: [],
...options
} as LoadLine;
}

function createFindNodeAndOffset(innerText: string, textNodes: Text[], initialOffset: number) {
let i = 0;
let offset = 0;
Expand Down

0 comments on commit c00f6fc

Please sign in to comment.