diff --git a/src/editor/lsp/completion.test.ts b/src/editor/lsp/completion.test.ts index 8b8a8543d9..544c1a73e4 100644 --- a/src/editor/lsp/completion.test.ts +++ b/src/editor/lsp/completion.test.ts @@ -1107,6 +1107,21 @@ describe("explicit completion", () => { }, ); + it("keeps Tab for an explicit lookup that is still inside the interaction delay", async () => { + const view = editor(["println", "print"]); + const now = vi.spyOn(Date, "now").mockReturnValue(Date.now()); + try { + explicitCompletion(view); + await vi.waitFor(() => expect(currentCompletions(view.state)).toHaveLength(2)); + expect(runScopeHandlers(view, new KeyboardEvent("keydown", { key: "Tab" }), "editor")).toBe(false); + expect(completionStatus(view.state)).toBe("active"); + expect(view.state.doc.toString()).toBe("pri"); + } finally { + now.mockRestore(); + view.destroy(); + } + }); + it("keeps a sole candidate in the list when auto-insert is off", async () => { useProject.setState({ settings: { editor: { completion: { autoInsertSingle: false } } } as unknown as Settings }); const view = editor(["println"]); diff --git a/src/editor/lsp/completion.ts b/src/editor/lsp/completion.ts index 91f42177a7..b9d8d1c3c4 100644 --- a/src/editor/lsp/completion.ts +++ b/src/editor/lsp/completion.ts @@ -1086,6 +1086,7 @@ export function smartTypeCompletion(view: EditorView): boolean { /** Show suggestions as you type: sources only answer explicit requests when it is off. */ function gated(source: CompletionSource): CompletionSource { return (ctx) => { + if (ctx.explicit && ctx.view) explicitLookups.add(ctx.view); if (ctx.state.field(smartLookup, false) && source !== claimingLspSource) return null; const only = exclusiveSource(ctx); if (only) return source === contributedSource ? only(ctx) : null; @@ -1115,6 +1116,8 @@ export const COMMIT_CHARS = [".", ",", ";", "=", " ", ":", "("]; const focusedLookups = new WeakSet(); /** Explicit invocations waiting for their first result (single candidate → insert it). */ const explicitPending = new WeakSet(); +/** Lookups requested explicitly by any entry point (⌃Space, ⌘J, a snippet field), not opened by typing. */ +const explicitLookups = new WeakSet(); const NAVIGATION_KEYS = new Set(["ArrowUp", "ArrowDown", "PageUp", "PageDown"]); @@ -1180,6 +1183,7 @@ const lookupTracker = ViewPlugin.fromClass( const view = this.view; if (status === null) { focusedLookups.delete(view); + explicitLookups.delete(view); resetLookupDocs(); if (explicitPending.has(view)) { explicitPending.delete(view); @@ -1404,10 +1408,16 @@ export function completionExtension() { if (completionStatus(view.state) !== "active") return false; replaceMode = true; try { - return acceptCompletion(view); + if (acceptCompletion(view)) return true; } finally { replaceMode = false; } + // A lookup that typing opened, refused only by CodeMirror's interactionDelay: close it so Tab + // does what it does without one (expand a template, move to a field) instead of inserting + // a tab. A lookup the user requested or navigated keeps Tab. + if (selectedCompletion(view.state) && !explicitLookups.has(view) && !focusedLookups.has(view)) + closeCompletion(view); + return false; }, }, ]), diff --git a/src/features/templates/__tests__/editor.test.ts b/src/features/templates/__tests__/editor.test.ts index 45c975ab34..46faf39129 100644 --- a/src/features/templates/__tests__/editor.test.ts +++ b/src/features/templates/__tests__/editor.test.ts @@ -11,6 +11,7 @@ import { CompletionContext, autocompletion, completionStatus, + currentCompletions, hasNextSnippetField, snippet, } from "@codemirror/autocomplete"; @@ -25,6 +26,8 @@ import { templateEditorExtension, } from "../editor"; import { closeChooser, useChooser } from "../popup/store"; +import { completionExtension } from "@/editor/lsp/completion"; +import { tabKeymap } from "@/editor/core/cm/smartKeys"; import { liveTemplates, removeTemplate, saveTemplate } from "../live/store"; const MODES: Record = { @@ -86,6 +89,39 @@ describe("Tab expansion", () => { expect(v.state.selection.main.head).toBe("function f() {\n console.log(".length); }); + it("expands an abbreviation when Tab comes right after the lookup opens", async () => { + // CodeMirror refuses to accept a completion for 75 ms after the lookup opens; a quick Tab + // used to fall through to a literal tab character instead of expanding. + const v = await editor("typescript", "function f() {\n for", undefined, [completionExtension(), tabKeymap]); + const now = vi.spyOn(Date, "now").mockReturnValue(Date.now()); + try { + const end = v.state.doc.length; + v.dispatch({ changes: { from: end, insert: "i" }, selection: { anchor: end + 1 }, userEvent: "input.type" }); + await vi.waitFor(() => expect(currentCompletions(v.state).length).toBeGreaterThan(0)); + expect(tab(v)).toBe(true); + } finally { + now.mockRestore(); + } + expect(completionStatus(v.state)).toBeNull(); + expect(v.state.doc.toString()).toContain("for (let i = 0; i < "); + const field = v.state.selection.main.from; + expect(tab(v)).toBe(true); + expect(v.state.selection.main.from).toBeGreaterThan(field); + }); + + it("leaves a lookup opened with Insert Live Template to Tab inside the interaction delay", async () => { + const v = await editor("typescript", "items.for", undefined, [completionExtension(), tabKeymap]); + const now = vi.spyOn(Date, "now").mockReturnValue(Date.now()); + try { + insertLiveTemplate(v); + await vi.waitFor(() => expect(currentCompletions(v.state).length).toBeGreaterThan(0)); + tab(v); + } finally { + now.mockRestore(); + } + expect(v.state.doc.toString()).toMatch(/^items\.for/); + }); + it("expands a PHP template in a class body", async () => { // The PHP grammar's document node is named `Template`: it is code, not a template string. const v = await editor("php", "