Skip to content

Build your personal knowledge base with Trilium Notes

fix(code): ask for completions again after a paste moves the list

Elian Doranb110f17ca05e
diff --git a/apps/client/src/widgets/type_widgets/code/code_completions.spec.tsx b/apps/client/src/widgets/type_widgets/code/code_completions.spec.tsx
index ad0d4fe5a7..5156260587 100644
--- a/apps/client/src/widgets/type_widgets/code/code_completions.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/code/code_completions.spec.tsx
@@ -54,6 +54,21 @@ describe("codeCompletions", () => {
await vi.waitFor(() => expect(view.getText()).toBe("api.getNotes"));
});
+ it("asks the language service again for a member at the same column of another expression", async () => {
+ const view = build([]);
+ vi.spyOn(view, "hasTypeCompletions").mockReturnValue(true);
+ const lookUp = vi.spyOn(view, "getTypeCompletions")
+ .mockResolvedValueOnce([ { label: "log" } ])
+ .mockResolvedValueOnce([ { label: "size" } ]);
+
+ type("api.");
+ await vi.waitFor(() => expect(rows()).toEqual([ "log" ]));
+
+ type("log(\"x\");\nobj.");
+ await vi.waitFor(() => expect(rows()).toEqual([ "size" ]));
+ expect(lookUp).toHaveBeenCalledTimes(2);
+ });
+
it("lists the snippets after a /, and puts the content of the one picked in place of the command", async () => {
const view = build([ { noteId: "s1", title: "Header", description: "Page header", content: "// header\n" } ]);
diff --git a/apps/client/src/widgets/type_widgets/code/code_completions.tsx b/apps/client/src/widgets/type_widgets/code/code_completions.tsx
index d0eb4aab0c..0ccfab8ac8 100644
--- a/apps/client/src/widgets/type_widgets/code/code_completions.tsx
+++ b/apps/client/src/widgets/type_widgets/code/code_completions.tsx
@@ -100,7 +100,10 @@ function createCodeCompletionList(editor: VanillaCodeMirror, getSnippets: () =>
const list = createHostedList<HostedCompletionState<CodeCompletionMatch>>((state, props) => {
const { match } = state;
- const key = `${match.kind}:${match.from}`;
+ // `match.from` counts from the start of the line, so the key also holds the line's place and
+ // its text before the match: a paste that ends at the same column elsewhere asks again.
+ const line = editor.state.doc.lineAt(editor.state.selection.main.head);
+ const key = `${match.kind}:${line.from + match.from}:${line.text.slice(0, match.from)}`;
if (cached?.key !== key) {
cached = { key, entries: entriesFor(match) };
}