feat(editor): add configurable completion shortcut
This commit is contained in:
1 parent
e2b8ce3408
commit
e6fa850a31
15 files changed
+158
No files matched your search
@@ -294,4 +294,25 @@ describe("QueryEditor completion Tab keymap", () => {
|
||||
expect(view.state.replaceSelection).toHaveBeenCalledWith(" ");
|
||||
expect(view.dispatch).toHaveBeenCalledOnce();
|
||||
});
|
||||
|
||||
it("triggers completion on Alt+/ through the manual shortcut and skips while composing", () => {
|
||||
const source = [extractDeclaration(/const COMPLETION_DEBOUNCE_DELAY_MS = \d+;/, "completion debounce delay"), "let imeCompositionActive = false;", extractFunction("isEditorComposing"), extractFunction("triggerSqlCompletion")].join("\n");
|
||||
const javascript = ts.transpileModule(source, {
|
||||
compilerOptions: { module: ts.ModuleKind.None, target: ts.ScriptTarget.ES2022 },
|
||||
}).outputText;
|
||||
const factory = new Function("codeMirrorStartCompletion", `${javascript}\nreturn { triggerSqlCompletion, isEditorComposing };`);
|
||||
const startCompletion = vi.fn(() => true);
|
||||
const { triggerSqlCompletion } = factory(startCompletion) as {
|
||||
triggerSqlCompletion: (view: MockView) => boolean;
|
||||
};
|
||||
const view = createView();
|
||||
|
||||
expect(triggerSqlCompletion(view)).toBe(true);
|
||||
expect(startCompletion).toHaveBeenCalledWith(view);
|
||||
|
||||
startCompletion.mockClear();
|
||||
(view as MockView & { composing: boolean }).composing = true;
|
||||
expect(triggerSqlCompletion(view)).toBe(false);
|
||||
expect(startCompletion).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,73 @@
|
||||
// @vitest-environment happy-dom
|
||||
|
||||
import { EditorState } from "@codemirror/state";
|
||||
import { EditorView, keymap, runScopeHandlers } from "@codemirror/view";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { createQueryEditorCompletionShortcutBindings } from "@/lib/editor/queryEditorCompletionShortcut";
|
||||
|
||||
function createView(shortcut: string, run: (view: EditorView) => boolean, platform: string): EditorView {
|
||||
return new EditorView({
|
||||
parent: document.createElement("div"),
|
||||
state: EditorState.create({
|
||||
extensions: [keymap.of(createQueryEditorCompletionShortcutBindings(shortcut, run, platform))],
|
||||
}),
|
||||
});
|
||||
}
|
||||
|
||||
describe("QueryEditor manual completion shortcut", () => {
|
||||
it("dispatches macOS Option+/ by the physical Slash key", () => {
|
||||
const run = vi.fn(() => true);
|
||||
const view = createView("Alt+/", run, "MacIntel");
|
||||
const event = new KeyboardEvent("keydown", {
|
||||
key: "÷",
|
||||
code: "Slash",
|
||||
altKey: true,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
|
||||
expect(runScopeHandlers(view, event, "editor")).toBe(true);
|
||||
expect(run).toHaveBeenCalledOnce();
|
||||
expect(run).toHaveBeenCalledWith(view);
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("leaves macOS Option+/ unhandled during IME composition", () => {
|
||||
const run = vi.fn(() => true);
|
||||
const view = createView("Alt+/", run, "MacIntel");
|
||||
const event = new KeyboardEvent("keydown", {
|
||||
key: "/",
|
||||
code: "Slash",
|
||||
altKey: true,
|
||||
isComposing: true,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
|
||||
expect(runScopeHandlers(view, event, "editor")).toBe(false);
|
||||
expect(event.defaultPrevented).toBe(false);
|
||||
expect(run).not.toHaveBeenCalled();
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("does not force-handle a configured shortcut when completion does not start", () => {
|
||||
const run = vi.fn(() => false);
|
||||
const view = createView("Alt+K", run, "MacIntel");
|
||||
const event = new KeyboardEvent("keydown", {
|
||||
key: "k",
|
||||
code: "KeyK",
|
||||
altKey: true,
|
||||
bubbles: true,
|
||||
cancelable: true,
|
||||
});
|
||||
|
||||
expect(runScopeHandlers(view, event, "editor")).toBe(false);
|
||||
expect(event.defaultPrevented).toBe(false);
|
||||
expect(run).toHaveBeenCalledOnce();
|
||||
view.destroy();
|
||||
});
|
||||
|
||||
it("keeps an empty configured shortcut disabled", () => {
|
||||
expect(createQueryEditorCompletionShortcutBindings("", () => true, "MacIntel")).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -154,6 +154,17 @@ describe("shortcutRegistry editor actions", () => {
|
||||
expect(DEFAULT_SHORTCUT_SETTINGS.extendSelection).toBe("Alt+W");
|
||||
});
|
||||
|
||||
it("registers an IDEA/DataGrip-style Alt+/ shortcut for manually triggering completion", () => {
|
||||
const definition = SHORTCUT_DEFINITIONS.find((item) => item.id === "triggerCompletion");
|
||||
|
||||
expect(definition).toMatchObject({ scope: "editor", defaultShortcut: "Alt+/" });
|
||||
expect(DEFAULT_SHORTCUT_SETTINGS.triggerCompletion).toBe("Alt+/");
|
||||
expect(formatShortcut(DEFAULT_SHORTCUT_SETTINGS.triggerCompletion, "Win32")).toBe("Alt+/");
|
||||
expect(formatShortcut(DEFAULT_SHORTCUT_SETTINGS.triggerCompletion, "MacIntel")).toBe("Alt+/");
|
||||
expect(shortcutToCodeMirrorKey(DEFAULT_SHORTCUT_SETTINGS.triggerCompletion)).toBe("Alt-/");
|
||||
expect(findShortcutConflict("triggerCompletion", DEFAULT_SHORTCUT_SETTINGS.triggerCompletion, DEFAULT_SHORTCUT_SETTINGS)).toBeNull();
|
||||
});
|
||||
|
||||
it("detects conflicts between formatter editor shortcuts and other editor shortcuts", () => {
|
||||
const shortcuts = normalizeShortcutSettings({ duplicateLine: "Mod+F" });
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { Command, KeyBinding } from "@codemirror/view";
|
||||
import { matchesShortcut, type ShortcutLikeEvent } from "@/lib/editor/keyboardShortcuts";
|
||||
import { isMacShortcutPlatform } from "@/lib/editor/shortcutDisplay";
|
||||
import { shortcutToCodeMirrorKey } from "@/lib/editor/shortcutRegistry";
|
||||
|
||||
function matchesMacOptionSlash(event: ShortcutLikeEvent, shortcut: string, platform: string): boolean {
|
||||
if (!isMacShortcutPlatform(platform) || shortcut !== "Alt+/" || event.isComposing) return false;
|
||||
if (!event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return false;
|
||||
return event.code === "Slash";
|
||||
}
|
||||
|
||||
export function createQueryEditorCompletionShortcutBindings(shortcut: string, run: Command, platform = globalThis.navigator?.platform || ""): KeyBinding[] {
|
||||
if (!shortcut) return [];
|
||||
if (!isMacShortcutPlatform(platform) || shortcut !== "Alt+/") {
|
||||
return [{ key: shortcutToCodeMirrorKey(shortcut), run }];
|
||||
}
|
||||
return [
|
||||
{
|
||||
any(view, event) {
|
||||
if (event.isComposing) return false;
|
||||
if (!matchesShortcut(event, shortcut, platform) && !matchesMacOptionSlash(event, shortcut, platform)) return false;
|
||||
return run(view);
|
||||
},
|
||||
},
|
||||
];
|
||||
}
|
||||
@@ -9,6 +9,7 @@ export type ShortcutActionId =
|
||||
| "toggleBlockComment"
|
||||
| "saveSql"
|
||||
| "acceptCompletion"
|
||||
| "triggerCompletion"
|
||||
| "indentMore"
|
||||
| "indentLess"
|
||||
| "insertLineBelow"
|
||||
@@ -138,6 +139,12 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
|
||||
scope: "editor",
|
||||
defaultShortcut: "Tab",
|
||||
},
|
||||
{
|
||||
id: "triggerCompletion",
|
||||
labelKey: "settings.shortcutTriggerCompletion",
|
||||
scope: "editor",
|
||||
defaultShortcut: "Alt+/",
|
||||
},
|
||||
{
|
||||
id: "indentMore",
|
||||
labelKey: "settings.shortcutIndentMore",
|
||||
|
||||
Reference in new issue
Block a user