Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): sync a script into the language service when the editor gets it

Elian Doranf15380f877f6
diff --git a/packages/codemirror/src/type_completion/codemirror-ts/environment.ts b/packages/codemirror/src/type_completion/codemirror-ts/environment.ts
index 84c4ed497f..ba833cd6e0 100644
--- a/packages/codemirror/src/type_completion/codemirror-ts/environment.ts
+++ b/packages/codemirror/src/type_completion/codemirror-ts/environment.ts
@@ -1,5 +1,5 @@
-import { combineConfig, Facet } from "@codemirror/state";
-import { EditorView } from "@codemirror/view";
+import { combineConfig, type EditorState, Facet } from "@codemirror/state";
+import { type EditorView, ViewPlugin, type ViewUpdate } from "@codemirror/view";
import type ts from "@typescript/vfs";
import { type VirtualTypeScriptEnvironment } from "@typescript/vfs";
@@ -32,22 +32,25 @@ export const tsFacet = Facet.define<
* do get deleted or renamed.
*/
export function tsSync() {
- // TODO: this is a weak solution to the cold start problem.
- // If you boot up a CodeMirror instance, we want the initial
- // value to get loaded into CodeMirror. We do get a change event,
- // but it surprisingly doesn't have `docChanged: true` on it,
- // so this is a rough heuristic to just accept the first event
- // regardless of whether it looks significant.
- let first = true;
- return EditorView.updateListener.of((update) => {
- const config = update.view.state.facet(tsFacet);
- if (!config) return;
- if (!update.docChanged && !first) return;
- first = false;
- createOrUpdateFile(config.env, config.path, update.state.doc.toString() || ' ');
+ // The plugin is constructed whenever it joins an editor, at creation or through a
+ // reconfiguration, so the document the editor already holds is synced then.
+ return ViewPlugin.fromClass(class {
+ constructor(view: EditorView) {
+ syncDocument(view.state);
+ }
+
+ update(update: ViewUpdate) {
+ if (update.docChanged) syncDocument(update.state);
+ }
});
}
+function syncDocument(state: EditorState) {
+ const config = state.facet(tsFacet);
+ if (!config) return;
+ createOrUpdateFile(config.env, config.path, state.doc.toString() || ' ');
+}
+
/**
* In TypeScript, updates are not like PUTs, you
* need to create a file before updating it.
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
index be04097aa1..0139991cd6 100644
--- a/packages/codemirror/src/type_completion/index.spec.ts
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -1,4 +1,5 @@
-import { EditorState } from "@codemirror/state";
+import { Compartment, EditorState } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
import { describe, expect, it } from "vitest";
import { createOrUpdateFile, tsFacet } from "./codemirror-ts/environment.js";
@@ -532,6 +533,31 @@ describe("buildTypeCompletion", () => {
}, TIMEOUT);
});
+describe("tsSync", () => {
+ /** The text the language service holds for the editor's script. */
+ function synced(view: EditorView) {
+ const config = view.state.facet(tsFacet);
+ return config?.env.getSourceFile(config.path)?.text;
+ }
+
+ it("syncs the document from the moment the editor has the extensions, then every change", async () => {
+ const { extensions } = await buildTypeCompletion(SCRIPT_MIME_BACKEND);
+
+ const created = new EditorView({ state: EditorState.create({ doc: "const a = 1;", extensions }) });
+ expect(synced(created)).toBe("const a = 1;");
+ created.dispatch({ changes: { from: 0, insert: "// note\n" } });
+ expect(synced(created)).toBe("// note\nconst a = 1;");
+ created.destroy();
+
+ // As the editor adds them: to a document it already holds, through a compartment.
+ const compartment = new Compartment();
+ const added = new EditorView({ state: EditorState.create({ doc: "let b;", extensions: compartment.of([]) }) });
+ added.dispatch({ effects: compartment.reconfigure(extensions) });
+ expect(synced(added)).toBe("let b;");
+ added.destroy();
+ }, TIMEOUT);
+});
+
describe("renderHoverTooltip", () => {
it("renders the signature, the JSDoc body and each tag", () => {
const { dom } = renderHoverTooltip({