Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

fileEditorVirtualization.test.ts

737 lines23.7 KB

1import {
2 getSharedHighlighter,
3 VirtualizedFile,
4 Virtualizer,
5 type FileContents,
6} from "@pierre/diffs";
7import { Editor, TextDocument } from "@pierre/diffs/edit";
8import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from "vite-plus/test";
9​
10const renderingManagerUrl = new URL(
11 "./managers/UniversalRenderingManager.js",
12 import.meta.resolve("@pierre/diffs"),
13);
14const { clearRenderQueue } = (await import(/* @vite-ignore */ renderingManagerUrl.href)) as {
15 clearRenderQueue(): void;
16};
17​
18class MeasuredElement {
19 static geometryReads = 0;
20 children: MeasuredElement[] = [];
21 dataset: Record<string, string> = {};
22 nextElementSibling: MeasuredElement | null = null;
23 shadowRoot: MeasuredElement | null = null;
24 width = 283;
25​
26 constructor(readonly height = 0) {}
27​
28 attachShadow() {
29 this.shadowRoot ??= new MeasuredElement();
30 return this.shadowRoot;
31 }
32​
33 appendChild(child: MeasuredElement) {
34 this.children.push(child);
35 return child;
36 }
37​
38 getBoundingClientRect() {
39 MeasuredElement.geometryReads += 1;
40 return { top: 0, height: this.height, width: this.width };
41 }
43​
44class MeasuredCodeElement extends MeasuredElement {
45 readonly tagName = "CODE";
46​
47 get firstElementChild() {
48 return this.children[0] ?? null;
49 }
51​
52const observers: RecordedResizeObserver[] = [];
53const animationFrames = new Map<number, FrameRequestCallback>();
54let nextFrameId = 0;
55​
56class RecordedResizeObserver {
57 readonly targets = new Set<Element>();
58​
59 constructor(readonly callback: ResizeObserverCallback) {
60 observers.push(this);
61 }
62​
63 observe(target: Element) {
64 this.targets.add(target);
65 }
66​
67 unobserve(target: Element) {
68 this.targets.delete(target);
69 }
70​
71 disconnect() {
72 this.targets.clear();
73 }
74​
75 deliver(target: HTMLElement) {
76 const rect = target.getBoundingClientRect();
77 const size = { inlineSize: rect.width, blockSize: rect.height };
78 this.callback(
79 [
80 {
81 target,
82 contentRect: rect,
83 contentBoxSize: [size],
84 borderBoxSize: [size],
85 devicePixelContentBoxSize: [size],
86 },
87 ],
88 this as unknown as ResizeObserver,
89 );
90 }
92​
93function drainRenderFrames() {
94 const errors = vi.spyOn(console, "error");
95 try {
96 for (let frame = 0; animationFrames.size > 0; frame += 1) {
97 if (frame === 10) throw new Error("The production render queue did not settle");
98 const callbacks = [...animationFrames.values()];
99 animationFrames.clear();
100 for (const callback of callbacks) callback(frame);
101 }
102 expect(errors).not.toHaveBeenCalled();
103 } finally {
104 errors.mockRestore();
105 }
107​
108function measuredElement(element: MeasuredElement): HTMLElement {
109 return element as unknown as HTMLElement;
111​
112class LayoutVirtualizer extends Virtualizer {
113 override getOffsetInScrollContainer(_element: HTMLElement) {
114 return 0;
115 }
117​
118class MeasuredFile extends VirtualizedFile {
119 override top = 0;
120 override onRender = () => false;
121​
122 override __attachEditor(editor: Parameters<VirtualizedFile["__attachEditor"]>[0]) {
123 this.editor = editor;
124 return () => {
125 this.editor = undefined;
126 };
127 }
128​
129 async initialize(file: FileContents) {
130 this.updateCodeViewLayout(file, 0);
131 // Document changes require the session an attached editor installs.
132 (this as unknown as { installEditSession(file: FileContents): void }).installEditSession(file);
133 await this.fileRenderer.initializeHighlighter();
134 expect(
135 this.fileRenderer.renderFile(file, {
136 startingLine: 5950,
137 totalLines: 51,
138 bufferBefore: 0,
139 bufferAfter: 0,
140 }),
141 ).toBeDefined();
142 this.fileContainer = measuredElement(new MeasuredElement());
143 }
144​
145 measure(
146 rows: ReadonlyArray<readonly [lineIndex: number, height: number]>,
147 contentWidth = 226.25,
148 ) {
149 const content = new MeasuredElement();
150 content.width = contentWidth;
151 content.children = rows.map(([lineIndex, height]) => {
152 const row = new MeasuredElement(height);
153 row.dataset.lineIndex = String(lineIndex);
154 return row;
155 });
156 const code = new MeasuredCodeElement();
157 code.width = contentWidth + 56.75;
158 code.children = [new MeasuredElement(), content];
159 this.code = measuredElement(code);
160 this.reconcileHeights();
161 }
162​
163 resizeContent(contentWidth: number, codeWidth = contentWidth + 56.75) {
164 const code = this.code;
165 const content = code?.children[1];
166 if (!(code instanceof MeasuredElement) || !(content instanceof MeasuredElement)) {
167 throw new Error("Expected measured code and content");
168 }
169 code.width = codeWidth;
170 content.width = contentWidth;
171 }
172​
173 observeLayout() {
174 const pre = new MeasuredElement();
175 if (!(this.code instanceof MeasuredElement)) throw new Error("Expected measured code");
176 pre.children = [this.code];
177 this.resizeManager.setup(measuredElement(pre) as HTMLPreElement, {
178 disableAnnotations: true,
179 columnVariables: "measure",
180 });
181 const code = this.code;
182 const observer = observers.find((candidate) => candidate.targets.has(code));
183 if (observer === undefined) throw new Error("The real resize manager did not observe code");
184 return () => observer.deliver(code);
185 }
186​
187 dispose() {
188 this.fileContainer = undefined;
189 this.code = undefined;
190 this.cleanUp();
191 }
193​
194const instances: MeasuredFile[] = [];
195const editors: Editor<"file", undefined, undefined>[] = [];
196​
197beforeAll(async () => {
198 await getSharedHighlighter({
199 themes: ["pierre-dark"],
200 langs: ["text"],
201 preferredHighlighter: "shiki-wasm",
202 });
203});
204​
205beforeEach(() => {
206 observers.length = 0;
207 animationFrames.clear();
208 MeasuredElement.geometryReads = 0;
209 vi.stubGlobal("HTMLElement", MeasuredElement);
210 vi.stubGlobal("Document", MeasuredElement);
211 vi.stubGlobal("ResizeObserver", RecordedResizeObserver);
212 vi.stubGlobal("requestAnimationFrame", (callback: FrameRequestCallback) => {
213 const id = ++nextFrameId;
214 animationFrames.set(id, callback);
215 return id;
216 });
217 vi.stubGlobal("cancelAnimationFrame", (id: number) => animationFrames.delete(id));
218});
219​
220afterEach(() => {
221 for (const editor of editors.splice(0)) editor.cleanUp();
222 for (const instance of instances.splice(0)) instance.dispose();
223 clearRenderQueue();
224 animationFrames.clear();
225 vi.unstubAllGlobals();
226});
227​
228async function makeFixture(
229 overflow: "wrap" | "scroll" = "wrap",
230 lineCount = 6001,
231 contentWidth = 226.25,
232) {
233 const contents = Array.from({ length: lineCount }, (_, index) => `line ${index}`).join("\n");
234 const file: FileContents = {
235 name: "wrapped.txt",
236 contents,
237 cacheKey: `wrapped:${overflow}`,
238 lang: "text",
239 };
240 const document = new TextDocument<"file", undefined>(file.name, contents, "text");
241 const instance = new MeasuredFile(
242 {
243 overflow,
244 disableFileHeader: true,
245 theme: "pierre-dark",
246 preferredHighlighter: "shiki-wasm",
247 useTokenTransformer: true,
248 controlledSelection: true,
249 },
250 new LayoutVirtualizer(),
251 );
252 instances.push(instance);
253 await instance.initialize(file);
254 instance.measure(
255 [
256 [0, 80],
257 [120, 60],
258 [4999, 100],
259 [5000, 80],
260 [5999, 100],
261 [6000, 60],
262 ],
263 contentWidth,
264 );
265 const apply = (change: { startLine: number } | undefined, passStartLine = true) => {
266 if (change === undefined) throw new Error("Expected a document change");
267 file.contents = document.getText();
268 instance.applyDocumentChange(
269 document,
270 undefined,
271 false,
272 passStartLine ? change.startLine : undefined,
273 );
274 };
275 const append = () => {
276 const position = document.positionAt(document.getText().length);
277 apply(document.applyEdits([{ range: { start: position, end: position }, newText: "\n" }]));
278 };
279 return { instance, document, file, apply, append };
281​
282describe("wrapped editor document changes", () => {
283 it("preserves the position above an EOF insertion across layout checkpoints", async () => {
284 const { instance, document, append } = await makeFixture();
285 const previousLastLine = document.lineCount;
286 const before = instance.getLinePosition(previousLastLine);
287 expect(before).toEqual({ top: 120328, height: 60 });
288 const viewport = { top: before!.top - 100, bottom: before!.top + 80 };
289 expect(instance.getAdvancedStickySpecs(viewport)).toEqual({ topOffset: 118240, height: 2096 });
290​
291 append();
292​
293 expect(document.lineCount).toBe(previousLastLine + 1);
294 expect(instance.getLinePosition(previousLastLine)).toEqual({ top: before!.top, height: 20 });
295 expect(instance.getLinePosition(document.lineCount)).toEqual({ top: 120348, height: 20 });
296 expect(instance.getVirtualizedHeight()).toBe(120376);
297 expect(instance.getAdvancedStickySpecs(viewport)).toEqual({ topOffset: 118240, height: 2096 });
298 });
299​
300 it("invalidates changed and shifted rows after an insertion in the middle", async () => {
301 const { instance, document, apply } = await makeFixture();
302 const before = instance.getLinePosition(5001);
303 const position = { line: 5000, character: 2 };
304 apply(document.applyEdits([{ range: { start: position, end: position }, newText: "\n" }]));
305​
306 expect(instance.getLinePosition(5001)).toEqual({ top: before!.top, height: 20 });
307 expect(instance.getLineHeight(4999)).toBe(100);
308 expect(instance.getLineHeight(5000)).toBe(20);
309 expect(instance.getLineHeight(5999)).toBe(20);
310 expect(instance.getLinePosition(document.lineCount)).toEqual({ top: 120208, height: 20 });
311 });
312​
313 it("keeps preceding measurements when a deletion crosses a checkpoint", async () => {
314 const { instance, document, apply } = await makeFixture();
315 const before = instance.getLinePosition(5000);
316 apply(
317 document.applyEdits([
318 {
319 range: { start: { line: 4999, character: 2 }, end: { line: 5001, character: 2 } },
320 newText: "",
321 },
322 ]),
323 );
324​
325 expect(document.lineCount).toBe(5999);
326 expect(instance.getLinePosition(5000)).toEqual({ top: before!.top, height: 20 });
327 expect(instance.getLineHeight(120)).toBe(60);
328 expect(instance.getLineHeight(4999)).toBe(20);
329 expect(instance.getLineHeight(6000)).toBe(20);
330 expect(instance.getLinePosition(document.lineCount)).toEqual({ top: 120068, height: 20 });
331 });
332​
333 it("uses the earliest changed line for edits at multiple selections", async () => {
334 const { instance, document, apply } = await makeFixture();
335 const before = instance.getLinePosition(121);
336 apply(
337 document.applyEdits(
338 [120, 5000].map((line) => ({
339 range: { start: { line, character: 2 }, end: { line, character: 2 } },
340 newText: "\n",
341 })),
342 ),
343 );
344​
345 expect(document.lineCount).toBe(6003);
346 expect(instance.getLinePosition(121)).toEqual({ top: before!.top, height: 20 });
347 expect(instance.getLineHeight(0)).toBe(80);
348 expect(instance.getLineHeight(120)).toBe(20);
349 expect(instance.getLineHeight(4999)).toBe(20);
350 });
351​
352 it("retains the unchanged prefix through repeated Enter, undo and redo", async () => {
353 const { instance, document, apply, append } = await makeFixture();
354 const before = instance.getLinePosition(6001);
355 for (let count = 0; count < 60; count += 1) append();
356 expect(document.lineCount).toBe(6061);
357 expect(instance.getLinePosition(6001)?.top).toBe(before!.top);
358 apply(document.undo()?.[0]);
359 expect(instance.getLinePosition(6001)?.top).toBe(before!.top);
360 apply(document.redo()?.[0]);
361 expect(document.lineCount).toBe(6061);
362 expect(instance.getLinePosition(6001)?.top).toBe(before!.top);
363 });
364​
365 it("keeps unwrapped positions unchanged", async () => {
366 const { instance, append } = await makeFixture("scroll");
367 const before = instance.getLinePosition(6001);
368 append();
369 expect(instance.getLinePosition(6001)).toEqual(before);
370 expect(instance.getLinePosition(6002)).toEqual({ top: 120028, height: 20 });
371 });
372​
373 it("fully invalidates measurements when the first changed line is unknown", async () => {
374 const { instance, document, apply } = await makeFixture();
375 const position = document.positionAt(document.getText().length);
376 apply(
377 document.applyEdits([{ range: { start: position, end: position }, newText: "\n" }]),
378 false,
379 );
380 expect(instance.getLinePosition(6001)).toEqual({ top: 120008, height: 20 });
381 expect(instance.getLineHeight(0)).toBe(20);
382 });
383​
384 it("still discards all measured rows after a metric change", async () => {
385 const { instance, file, append } = await makeFixture();
386 append();
387 instance.setMetrics({ hunkLineCount: 50, lineHeight: 24, diffHeaderHeight: 44, spacing: 8 });
388 instance.updateCodeViewLayout(file, 0);
389 expect(instance.getLinePosition(6001)).toEqual({ top: 144008, height: 24 });
390 });
391​
392 it("still discards all measured rows when annotations change", async () => {
393 const { instance, file, append } = await makeFixture();
394 append();
395 instance.setLineAnnotations([{ lineNumber: 10, metadata: undefined }]);
396 instance.updateCodeViewLayout(file, 0);
397 expect(instance.getLinePosition(6001)).toEqual({ top: 120008, height: 20 });
398 });
399});
400​
401describe("wrapped measurement widths", () => {
402 it.each([
403 [226.25, 482.25],
404 [482.25, 226.25],
405 ])(
406 "drops offscreen measurements when content changes from %spx to %spx",
407 async (before, after) => {
408 const { instance } = await makeFixture("wrap", 6001, before);
409 instance.measure([[6000, 60]], after);
410 expect(instance.getLineHeight(0)).toBe(20);
411 expect(instance.getLineHeight(120)).toBe(20);
412 expect(instance.getLineHeight(6000)).toBe(60);
413 expect(instance.getVirtualizedHeight()).toBe(120076);
414 },
415 );
416​
417 it.each([
418 [226.25, 482.25],
419 [482.25, 226.25],
420 ])(
421 "does not retain old-width prefix heights after a %spx to %spx resize and edit",
422 async (before, after) => {
423 const { instance, append } = await makeFixture("wrap", 6001, before);
424 instance.measure([[6000, 60]], after);
425 append();
426 expect(instance.getLineHeight(0)).toBe(20);
427 expect(instance.getLineHeight(120)).toBe(20);
428 expect(instance.getLinePosition(6001)).toEqual({ top: 120008, height: 20 });
429 },
430 );
431​
432 it("repairs an edit before resize delivery when the real resize and render queues drain", async () => {
433 const { instance, append } = await makeFixture();
434 instance.measure([[6000, 60]]);
435 const deliverResize = instance.observeLayout();
436 instance.resizeContent(482.25);
437 const readsBeforeEdit = MeasuredElement.geometryReads;
438 append();
439 expect(MeasuredElement.geometryReads).toBe(readsBeforeEdit);
440 expect(instance.getLineHeight(0)).toBe(80);
441 deliverResize();
442 drainRenderFrames();
443 expect(instance.getLineHeight(0)).toBe(20);
444 expect(instance.getLinePosition(6001)).toEqual({ top: 120008, height: 60 });
445 });
446​
447 it("keeps measured prefixes through same-width reconciliation and editing", async () => {
448 const { instance, append } = await makeFixture();
449 instance.measure([[6000, 60]]);
450 append();
451 expect(instance.getLineHeight(0)).toBe(80);
452 expect(instance.getLineHeight(120)).toBe(60);
453 expect(instance.getLinePosition(6001)).toEqual({ top: 120328, height: 20 });
454 });
455​
456 it("preserves measurements on first and repeated same-width resize deliveries", async () => {
457 const { instance } = await makeFixture();
458 const before = instance.getVirtualizedHeight();
459 const deliverResize = instance.observeLayout();
460 deliverResize();
461 deliverResize();
462 drainRenderFrames();
463 expect(instance.getLineHeight(0)).toBe(80);
464 expect(instance.getVirtualizedHeight()).toBe(before);
465 });
466​
467 it.each([
468 [226.25, 482.25],
469 [482.25, 226.25],
470 ])("handles a %spx to %spx resize before first observer delivery", async (before, after) => {
471 const { instance } = await makeFixture("wrap", 6001, before);
472 instance.measure([[6000, 20]], before);
473 const deliverResize = instance.observeLayout();
474 instance.resizeContent(after);
475 deliverResize();
476 drainRenderFrames();
477 expect(instance.getLineHeight(0)).toBe(20);
478 expect(instance.getVirtualizedHeight()).toBe(120036);
479 });
480​
481 it("keeps width validity when a new editor attaches to the same file", async () => {
482 const { instance } = await makeFixture();
483 instance.measure([[6000, 20]]);
484 const deliverResize = instance.observeLayout();
485 vi.stubGlobal("SVGSVGElement", EditorElement);
486 vi.stubGlobal(
487 "document",
488 Object.assign(new EditorElement(), { createElement: () => new EditorElement() }),
489 );
490 const first = new Editor("file");
491 editors.push(first);
492 first.edit(instance);
493 first.cleanUp();
494 const second = new Editor("file");
495 editors.push(second);
496 second.edit(instance);
497 instance.resizeContent(482.25);
498 deliverResize();
499 drainRenderFrames();
500 expect(instance.getLineHeight(0)).toBe(20);
501 expect(instance.getVirtualizedHeight()).toBe(120036);
502 });
503​
504 it("ignores stale resize deliveries after cleanup", async () => {
505 const { instance } = await makeFixture();
506 const deliverResize = instance.observeLayout();
507 instance.dispose();
508 clearRenderQueue();
509 animationFrames.clear();
510 deliverResize();
511 expect(animationFrames.size).toBe(0);
512 });
513​
514 it("does not discard a stable code width for gutter subpixel rounding", async () => {
515 const { instance } = await makeFixture("wrap", 6001, 482.25);
516 const deliverResize = instance.observeLayout();
517 instance.resizeContent(482.234375, 539);
518 deliverResize();
519 drainRenderFrames();
520 expect(instance.getLineHeight(0)).toBe(80);
521 });
522​
523 it("waits for a visible width instead of caching measurements while hidden", async () => {
524 const { instance } = await makeFixture();
525 instance.measure([[6000, 20]]);
526 const deliverResize = instance.observeLayout();
527 instance.resizeContent(0, 0);
528 deliverResize();
529 drainRenderFrames();
530 expect(instance.getLineHeight(0)).toBe(80);
531 instance.resizeContent(482.25);
532 deliverResize();
533 drainRenderFrames();
534 expect(instance.getLineHeight(0)).toBe(20);
535 expect(instance.getVirtualizedHeight()).toBe(120036);
536 });
537});
538​
539class EditorElement extends MeasuredElement {
540 style: Record<string, string> = {};
541 parentElement: EditorElement | null = null;
542​
543 override appendChild(child: EditorElement) {
544 child.parentElement = this;
545 this.children.push(child);
546 return child;
547 }
548​
549 append(child: EditorElement) {
550 this.appendChild(child);
551 }
552​
553 prepend(child: EditorElement) {
554 child.parentElement = this;
555 this.children.unshift(child);
556 }
557​
558 replaceChildren(...children: (EditorElement | string)[]) {
559 this.children = [];
560 for (const child of children) if (typeof child !== "string") this.appendChild(child);
561 }
562​
563 setAttribute() {}
564 removeAttribute() {}
565 addEventListener() {}
566 removeEventListener() {}
567 after() {}
568​
569 remove() {
570 if (this.parentElement) {
571 this.parentElement.children = this.parentElement.children.filter((child) => child !== this);
572 }
573 }
574​
575 set innerHTML(value: string) {
576 expect(value.startsWith("<svg")).toBe(true);
577 this.replaceChildren(new EditorElement());
578 }
579​
580 get firstElementChild() {
581 return this.children[0] ?? null;
582 }
583​
584 querySelectorAll(selector: string) {
585 expect(selector).toBe("[data-code]");
586 return this.children.filter((child) => "code" in child.dataset);
587 }
588​
589 querySelector(selector: string) {
590 expect(selector).toBe("[data-deletions]");
591 return null;
592 }
593​
594 getContext() {
595 return { measureText: (text: string) => ({ width: text.length * 8 }) };
596 }
598​
599async function makeEditorFixture(lineCount: number) {
600 const { instance, file } = await makeFixture("wrap", lineCount);
601 vi.stubGlobal("SVGSVGElement", EditorElement);
602 vi.stubGlobal("Document", EditorElement);
603 vi.stubGlobal(
604 "document",
605 Object.assign(new EditorElement(), { createElement: () => new EditorElement() }),
606 );
607 vi.stubGlobal("window", { matchMedia: () => ({ matches: true }) });
608 vi.stubGlobal("requestAnimationFrame", () => 1);
609 vi.stubGlobal("cancelAnimationFrame", () => {});
610 vi.stubGlobal("getComputedStyle", () => ({
611 paddingTop: "0px",
612 fontSize: "13px",
613 fontFamily: "monospace",
614 tabSize: "2",
615 lineHeight: "20px",
616 }));
617 vi.stubGlobal(
618 "ResizeObserver",
619 class {
620 observe() {}
621 disconnect() {}
622 },
623 );
624 instance.setOptions({
625 ...instance.options,
626 useTokenTransformer: true,
627 controlledSelection: true,
628 themeType: "dark",
629 });
630 const content = new EditorElement();
631 content.dataset.content = "";
632 const gutter = new EditorElement();
633 gutter.dataset.gutter = "";
634 const code = new EditorElement();
635 code.dataset.code = "";
636 code.appendChild(gutter);
637 code.appendChild(content);
638 const shadow = new EditorElement();
639 shadow.appendChild(code);
640 const host = Object.assign(new EditorElement(), { shadowRoot: shadow });
641 const highlighter = await getSharedHighlighter({
642 themes: ["pierre-dark"],
643 langs: ["text"],
644 preferredHighlighter: "shiki-wasm",
645 });
646 const editor = new Editor("file");
647 editors.push(editor);
648 editor.edit(instance);
649 editor.__syncRenderView({
650 highlighter,
651 fileContainer: measuredElement(host),
652 file,
653 lineAnnotations: undefined,
654 renderRange: { startingLine: 0, totalLines: 1, bufferBefore: 0, bufferAfter: 0 },
655 });
656 const append = (count: number) => {
657 const lines = editor.getText().split("\n");
658 const end = { line: lines.length - 1, character: lines.at(-1)!.length };
659 editor.applyEdits([{ range: { start: end, end }, newText: "\n".repeat(count) }]);
660 };
661 const remove = (count: number) => {
662 const lines = editor.getText().split("\n");
663 const startLine = lines.length - count - 1;
664 editor.applyEdits([
665 {
666 range: {
667 start: { line: startLine, character: lines[startLine]!.length },
668 end: { line: lines.length - 1, character: lines.at(-1)!.length },
669 },
670 newText: "",
671 },
672 ]);
673 };
674 return { instance, editor, append, remove };
676​
677describe("editor gutter-width changes", () => {
678 it.each([
679 [9999, 1],
680 [9998, 3],
681 ])(
682 "clears prefix measurements when %i lines grow by %i across a digit boundary",
683 async (lines, count) => {
684 const { instance, editor, append } = await makeEditorFixture(lines);
685 expect(instance.getLineHeight(0)).toBe(80);
686 append(count);
687 expect(editor.getText().split("\n")).toHaveLength(lines + count);
688 expect(instance.getLineHeight(0)).toBe(20);
689 expect(instance.getLineHeight(5000)).toBe(20);
690 },
691 );
692​
693 it.each([
694 [10000, 1],
695 [10002, 4],
696 ])(
697 "clears prefix measurements when %i lines shrink by %i across a digit boundary",
698 async (lines, count) => {
699 const { instance, editor, remove } = await makeEditorFixture(lines);
700 remove(count);
701 expect(editor.getText().split("\n")).toHaveLength(lines - count);
702 expect(instance.getLineHeight(0)).toBe(20);
703 expect(instance.getLineHeight(5000)).toBe(20);
704 },
705 );
706​
707 it("clears newly measured prefixes on undo and redo across a digit boundary", async () => {
708 const { instance, editor, append } = await makeEditorFixture(9999);
709 append(1);
710 instance.measure([[0, 100]]);
711 editor.undo();
712 expect(editor.getText().split("\n")).toHaveLength(9999);
713 expect(instance.getLineHeight(0)).toBe(20);
714 instance.measure([[0, 80]]);
715 editor.redo();
716 expect(editor.getText().split("\n")).toHaveLength(10000);
717 expect(instance.getLineHeight(0)).toBe(20);
718 });
719​
720 it.each([
721 [9998, 1],
722 [10000, 2],
723 ])(
724 "retains prefix measurements when %i lines grow by %i without changing digit width",
725 async (lines, count) => {
726 const { instance, editor, append } = await makeEditorFixture(lines);
727 append(count);
728 expect(editor.getText().split("\n")).toHaveLength(lines + count);
729 expect(instance.getLineHeight(0)).toBe(80);
730 expect(instance.getLineHeight(5000)).toBe(80);
731 editor.undo();
732 expect(instance.getLineHeight(0)).toBe(80);
733 editor.redo();
734 expect(instance.getLineHeight(0)).toBe(80);
735 },
736 );
737});