Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): complete everything where nothing is typed yet

Elian Doran7f374ad13fa7
diff --git a/packages/codemirror/src/type_completion/codemirror-ts/autocomplete.ts b/packages/codemirror/src/type_completion/codemirror-ts/autocomplete.ts
index 5ed265fa9c..f56f2330a5 100644
--- a/packages/codemirror/src/type_completion/codemirror-ts/autocomplete.ts
+++ b/packages/codemirror/src/type_completion/codemirror-ts/autocomplete.ts
@@ -52,8 +52,7 @@ export async function getAutocompletion({
.filter(
(entry) =>
!TS_COMPLETE_BLOCKLIST.includes(entry.kind) &&
- (entry.sortText < "15" ||
- completionInfo.optionalReplacementSpan?.length),
+ !entry.kindModifiers?.split(",").includes(ts.ScriptElementKindModifier.deprecatedModifier),
)
.map((entry): Completion => {
const boost = -Number(entry.sortText) || 0;
diff --git a/packages/codemirror/src/type_completion/index.spec.ts b/packages/codemirror/src/type_completion/index.spec.ts
index 2f88b063af..0cf322b173 100644
--- a/packages/codemirror/src/type_completion/index.spec.ts
+++ b/packages/codemirror/src/type_completion/index.spec.ts
@@ -23,6 +23,21 @@ function completionsAtMarker(mime: string, source: string, context?: ScriptApiCo
return getScriptCompletions(mime, stripped, offset, context);
}
+/**
+ * The labels `buildTypeCompletion()`'s `complete()` offers at the `|` marker in `source`, as the
+ * editor's completion list asks for them.
+ */
+async function completeAtMarker(mime: string, source: string, explicit = false) {
+ const offset = source.indexOf("|");
+ const code = source.slice(0, offset) + source.slice(offset + 1);
+ const { extensions, complete } = await buildTypeCompletion(mime);
+ // No editor view syncs the document here, so the test writes it into the environment.
+ const config = EditorState.create({ extensions }).facet(tsFacet);
+ expect(config).toBeTruthy();
+ if (config) createOrUpdateFile(config.env, config.path, code);
+ return (await complete?.(offset, explicit))?.map((entry) => entry.label) ?? [];
+}
+
/** "Property 'x' does not exist on type 'y'." */
const TS_UNKNOWN_PROPERTY = 2339;
/** "Argument of type 'x' is not assignable to parameter of type 'y'." */
@@ -498,15 +513,23 @@ describe("buildTypeCompletion", () => {
}, TIMEOUT);
it("completes globals and keywords at the start of a word", async () => {
- const { extensions, complete } = await buildTypeCompletion(SCRIPT_MIME_FRONTEND);
- // No editor view syncs the document here, so the test writes it into the environment.
- const config = EditorState.create({ extensions }).facet(tsFacet);
- expect(config).toBeTruthy();
- if (config) createOrUpdateFile(config.env, config.path, "x");
-
- const labels = (await complete?.(1, false))?.map((entry) => entry.label);
+ const labels = await completeAtMarker(SCRIPT_MIME_FRONTEND, "x|");
expect(labels).toEqual(expect.arrayContaining([ "api", "console", "glob", "const", "return" ]));
}, TIMEOUT);
+
+ it("completes globals and keywords where nothing is typed yet", async () => {
+ // The new list caches the answer for the position it opens at, and filters it as the
+ // word is typed, so the answer with nothing typed has to hold them already.
+ const labels = await completeAtMarker(SCRIPT_MIME_FRONTEND, "function run() {\n |\n}", true);
+ expect(labels).toEqual(expect.arrayContaining([ "api", "console", "const", "return" ]));
+ }, TIMEOUT);
+
+ it("leaves deprecated names out", async () => {
+ const labels = await completeAtMarker(SCRIPT_MIME_FRONTEND, "e|");
+ expect(labels).toContain("encodeURI");
+ expect(labels).not.toContain("escape");
+ expect(labels).not.toContain("captureEvents");
+ }, TIMEOUT);
});
describe("renderHoverTooltip", () => {