Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(codemirror): give up a waiting commit whose text an edit reaches
Elian Doranf2fd53041dc6
diff --git a/packages/codemirror/src/extensions/hosted_completion.spec.ts b/packages/codemirror/src/extensions/hosted_completion.spec.tsindex 0ca40c8c12..7679b7c13c 100644--- a/packages/codemirror/src/extensions/hosted_completion.spec.ts+++ b/packages/codemirror/src/extensions/hosted_completion.spec.ts@@ -1,4 +1,5 @@import { EditorSelection, type EditorState } from "@codemirror/state";+import type { EditorView } from "@codemirror/view";import { afterEach, describe, expect, it, vi } from "vitest";import { createFieldEditor, type FieldEditor, type FieldEditorConfig } from "../field_editor.js";@@ -148,6 +149,31 @@ describe("hostedCompletion", () => {expect(editor?.state.doc.toString()).toBe("I see [[new]] @x @z");editor?.dispatch({ changes: { from: 16, to: 19 }, selection: EditorSelection.cursor(16) });+ // A promise can settle on a function, which runs on the typed range then.+ type(" @fn");+ await vi.waitFor(() => expect(shown()?.match.query).toBe("fn"));+ shown()?.commit(Promise.resolve((view: EditorView, from: number, to: number) => {+ view.dispatch({ changes: { from, to, insert: "<fn>" } });+ }));+ await vi.waitFor(() => expect(editor?.state.doc.toString()).toBe("I see [[new]] @x <fn>"));++ // Text replaced or edited where the commit waits gives it up, rather than landing elsewhere.+ type(" @a");+ await vi.waitFor(() => expect(shown()?.match.query).toBe("a"));+ shown()?.commit(new Promise<string | undefined>((resolve) => { resolveLink = resolve; }));+ editor?.dispatch({ changes: { from: 0, to: editor.state.doc.length, insert: "Another note" } });+ resolveLink("[[late]]");+ await settle();+ expect(editor?.state.doc.toString()).toBe("Another note");+ type(" @b");+ await vi.waitFor(() => expect(shown()?.match.query).toBe("b"));+ shown()?.commit(new Promise<string | undefined>((resolve) => { resolveLink = resolve; }));+ editor?.dispatch({ changes: { from: 15, insert: "c" } });+ resolveLink("[[late]]");+ await settle();+ expect(editor?.state.doc.toString()).toBe("Another note @bc");+ editor?.dispatch({ changes: { from: 12, to: 16 }, selection: EditorSelection.cursor(12) });+// Settling past the editor's end does nothing.type(" @y");await vi.waitFor(() => expect(shown()?.match.query).toBe("y"));@@ -157,7 +183,7 @@ describe("hostedCompletion", () => {view?.destroy();resolveLink("[[late]]");await new Promise((resolve) => setTimeout(resolve, 0));- expect(view?.state.doc.toString()).toBe("I see [[new]] @x @y");+ expect(view?.state.doc.toString()).toBe("Another note @y");});it("forwards the keys to the open list, which takes them from the field", async () => {diff --git a/packages/codemirror/src/extensions/hosted_completion.ts b/packages/codemirror/src/extensions/hosted_completion.tsindex e6312cba84..44442720a7 100644--- a/packages/codemirror/src/extensions/hosted_completion.ts+++ b/packages/codemirror/src/extensions/hosted_completion.ts@@ -13,6 +13,9 @@ export interface HostedCompletionMatch {*/export type HostedCompletionApply = (view: EditorView, from: number, to: number) => void;+/** What a pick puts in place of the match: text, or what an {@link HostedCompletionApply} does. */+export type HostedCompletionInsert = string | HostedCompletionApply;+/** What a {@link HostedCompletionList} is shown for, each time the text before the caret changes. */export interface HostedCompletionState<M extends HostedCompletionMatch> {match: M;@@ -23,9 +26,11 @@ export interface HostedCompletionState<M extends HostedCompletionMatch> {/*** Replaces what stands between the match's `from` and the caret with `insert`, as a pick does,* or runs `insert` on that range. For a promise, the list closes and the text stays until it- * settles, past the editor losing the focus, and stays for good where it settles on `undefined`.+ * settles on either, past the editor losing the focus, and stays for good where it settles on+ * `undefined`. An edit that reaches the text meanwhile, such as the editor taking another note's+ * content, gives the commit up.*/- commit(insert: string | HostedCompletionApply | Promise<string | undefined>): void;+ commit(insert: HostedCompletionInsert | Promise<HostedCompletionInsert | undefined>): void;/*** Points the editor at the list's highlighted entry through `aria-activedescendant`, by the id* of the entry's element, or at none for `null`. Once the list closes, it points at none.@@ -94,10 +99,13 @@ export function hostedCompletion<M extends HostedCompletionMatch>({ match, listthis.dismissedAt = update.changes.mapPos(this.dismissedAt, 1);}if (update.docChanged) {- // Text typed right before or after a waiting commit's range stays outside it.for (const range of this.pending) {- range.from = update.changes.mapPos(range.from, 1);- range.to = update.changes.mapPos(range.to, -1);+ if (update.changes.touchesRange(range.from, range.to)) {+ this.pending.delete(range);+ } else {+ range.from = update.changes.mapPos(range.from);+ range.to = update.changes.mapPos(range.to);+ }}}@@ -189,7 +197,7 @@ export function hostedCompletion<M extends HostedCompletionMatch>({ match, list});}- private commit(from: number, insert: string | HostedCompletionApply | Promise<string | undefined>) {+ private commit(from: number, insert: HostedCompletionInsert | Promise<HostedCompletionInsert | undefined>) {if (this.from !== from) {return;}@@ -213,17 +221,23 @@ export function hostedCompletion<M extends HostedCompletionMatch>({ match, list}/** Closes the list, which stays closed on the match, and replaces the match once `insert` settles. */- private async commitWhenSettled(from: number, insert: Promise<string | undefined>) {+ private async commitWhenSettled(from: number, insert: Promise<HostedCompletionInsert | undefined>) {const range = { from, to: this.view.state.selection.main.head };this.pending.add(range);this.close();- const text = await insert.catch(() => undefined);- this.pending.delete(range);- if (text === undefined || this.destroyed) {+ const settled = await insert.catch(() => undefined);+ // `update()` drops the range of a commit an edit gave up meanwhile.+ const stillWaiting = this.pending.delete(range);+ if (settled === undefined || !stillWaiting || this.destroyed) {+ return;+ }+ if (typeof settled === "function") {+ settled(this.view, range.from, range.to);return;}+ const text = settled;this.view.dispatch({changes: { from: range.from, to: range.to, insert: text },selection: { anchor: range.from + text.length },