import { assertEquals } from "@std/assert"; import { EditorView } from "@codemirror/view"; import { until } from "@berry/util"; import { withDom } from "@berry/util/dom.ts"; import { highlighting } from "./highlight.ts"; Deno.test("highlight replies decorate the current text across lines without moving selection or applying stale tokens", () => withDom(async () => { const replies: { source: string; resolve(lines: Node[][]): void }[] = []; const parent = document.createElement("div"); document.body.append(parent); const view = new EditorView({ parent, doc: "old\nname", extensions: [highlighting("a.ts", (source) => new Promise((resolve) => { replies.push({ source, resolve }); }))], }); const lit = (a: string, b: string) => { const keyword = document.createElement("a-k"); keyword.textContent = a; const name = document.createElement("a-vm"); name.textContent = b; return [[keyword], [name]]; }; try { await until(() => replies.length === 1); view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: "new\nother" }, selection: { anchor: 6 }, }); await until(() => replies.length === 2); replies[1].resolve(lit("new", "other")); await until(() => !!parent.querySelector("a-k")); replies[0].resolve(lit("old", "name")); await Promise.resolve(); assertEquals(parent.querySelector("a-k")!.textContent, "new"); assertEquals(parent.querySelector("a-vm")!.textContent, "other"); assertEquals(view.state.doc.toString(), "new\nother"); assertEquals(view.state.selection.main.anchor, 6); } finally { view.destroy(); } }));