дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-06 18:17:15 +02:00
fix(completion): expand a template on a Tab pressed right after the lookup opens (#8315)
For 75 ms after a lookup opens, CodeMirror's interactionDelay makes acceptCompletion refuse, and template Tab handlers step aside while a lookup is open, so a quick Tab after typing an abbreviation inserted a tab instead of expanding the live, postfix or Emmet template (the intermittent reactComponentTemplates e2e failure). When only that delay refuses a lookup that typing opened, the completion Tab now closes it and passes the key on; a lookup the user requested (Ctrl+Space, Insert Live Template) or navigated keeps Tab, and a Tab after the delay still accepts the selected item. Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
1 джерело
89a3f0951a
коміт
d7a7dde9f3
3 files changed
+62
-1
No files matched your search
@@ -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"]);
|
||||
|
||||
@@ -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<EditorView>();
|
||||
/** Explicit invocations waiting for their first result (single candidate → insert it). */
|
||||
const explicitPending = new WeakSet<EditorView>();
|
||||
/** Lookups requested explicitly by any entry point (⌃Space, ⌘J, a snippet field), not opened by typing. */
|
||||
const explicitLookups = new WeakSet<EditorView>();
|
||||
|
||||
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;
|
||||
},
|
||||
},
|
||||
]),
|
||||
|
||||
@@ -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<string, string> = {
|
||||
@@ -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", "<?php\n\nfinal class A\n{\n pubsf");
|
||||
|
||||
Reference in new issue
Заблокувати користувача