fix(editor): keep selection highlights aligned during dragging

This commit is contained in:
C.J.S authored and GitHub committed 2026-09-18 17:34:14 +08:00
1 parent 985d55ba85
commit 06bbf99e3d
2 files changed
+171 -7

No files matched your search

@@ -1,5 +1,6 @@
// @vitest-environment happy-dom
import { indentMore } from "@codemirror/commands";
import { EditorSelection, EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
@@ -67,6 +68,70 @@ describe("selectionMatchOccurrences", () => {
});
});
describe("deferred selection match highlights", () => {
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
vi.useRealTimers();
document.body.replaceChildren();
});
it("rebuilds highlights only after pointer selection updates settle", async () => {
const view = createView("foo one\nfoo two\nfoo three");
view.dispatch({ selection: { anchor: 0, head: 3 }, userEvent: "select.pointer" });
expect(view.dom.querySelectorAll(".cm-selectionMatch")).toHaveLength(0);
// Later pointer updates must not rebuild the marks mid-drag.
view.dispatch({ selection: { anchor: 0, head: 4 }, userEvent: "select.pointer" });
await vi.advanceTimersByTimeAsync(SELECTION_MATCH_UPDATE_DELAY_MS - 1);
expect(view.dom.querySelectorAll(".cm-selectionMatch")).toHaveLength(0);
await vi.advanceTimersByTimeAsync(1);
// The selected occurrence itself keeps the editor's selection background.
expect(view.dom.querySelectorAll(".cm-selectionMatch")).toHaveLength(2);
view.destroy();
});
it("clears highlights as soon as the selection collapses", async () => {
const view = createView("foo one\nfoo two\nfoo three");
view.dispatch({ selection: { anchor: 0, head: 3 }, userEvent: "select.pointer" });
await vi.advanceTimersByTimeAsync(SELECTION_MATCH_UPDATE_DELAY_MS);
expect(view.dom.querySelectorAll(".cm-selectionMatch")).toHaveLength(2);
view.dispatch({ selection: { anchor: 3 } });
expect(view.dom.querySelectorAll(".cm-selectionMatch")).toHaveLength(0);
view.destroy();
});
it("keeps existing highlights aligned while a document rescan is pending", async () => {
const view = createView("foo one\nfoo two\nfoo three", EditorSelection.range(0, 3));
await vi.advanceTimersByTimeAsync(SELECTION_MATCH_UPDATE_DELAY_MS);
expect(Array.from(view.dom.querySelectorAll(".cm-selectionMatch"), (node) => node.textContent)).toEqual(["foo", "foo"]);
expect(indentMore(view)).toBe(true);
expect(Array.from(view.dom.querySelectorAll(".cm-selectionMatch"), (node) => node.textContent)).toEqual(["foo", "foo"]);
await vi.advanceTimersByTimeAsync(SELECTION_MATCH_UPDATE_DELAY_MS);
expect(Array.from(view.dom.querySelectorAll(".cm-selectionMatch"), (node) => node.textContent)).toEqual(["foo", "foo"]);
view.destroy();
});
it("drops pending work when the view is destroyed", async () => {
const view = createView("foo one\nfoo two\nfoo three");
view.dispatch({ selection: { anchor: 0, head: 3 }, userEvent: "select.pointer" });
view.destroy();
await vi.advanceTimersByTimeAsync(SELECTION_MATCH_UPDATE_DELAY_MS + 40);
expect(document.querySelectorAll(".cm-selectionMatch")).toHaveLength(0);
});
});
describe("SelectionMatchMarkerScan", () => {
it("finds a match that crosses a scan chunk boundary exactly once", () => {
const prefix = "x".repeat(SELECTION_MATCH_SCAN_CHUNK_LENGTH - 2);