feat(editor): add explain plan shortcut

This commit is contained in:
Eve authored and GitHub committed 2026-09-10 15:57:04 +08:00
1 parent 981978dcf2
commit bd828076ab
6 files changed
+53 -2

No files matched your search

@@ -39,6 +39,24 @@ describe("QueryEditor execution shortcuts", () => {
view.destroy();
});
it("checks shortcut eligibility at dispatch time without rebuilding the keymap", () => {
let enabled = false;
const explain = vi.fn(() => true);
const view = createView([createQueryEditorExecutionShortcutBindings("Mod+E", explain, isComposing, () => enabled)]);
expect(runScopeHandlers(view, executeEvent("e"), "editor")).toBe(true);
expect(explain).not.toHaveBeenCalled();
enabled = true;
expect(runScopeHandlers(view, executeEvent("e"), "editor")).toBe(true);
expect(explain).toHaveBeenCalledOnce();
enabled = false;
expect(runScopeHandlers(view, executeEvent("e"), "editor")).toBe(true);
expect(explain).toHaveBeenCalledOnce();
view.destroy();
});
it("consumes the SQL shortcut without executing during IME composition", () => {
const execute = vi.fn(() => true);
const view = createView([createQueryEditorExecutionShortcutBindings("Mod+Enter", execute, isComposing)]);
@@ -152,6 +152,14 @@ describe("shortcutRegistry editor actions", () => {
expect(findShortcutConflict("expandSelectStar", DEFAULT_SHORTCUT_SETTINGS.expandSelectStar, DEFAULT_SHORTCUT_SETTINGS)).toBeNull();
});
it("registers a configurable editor shortcut for the explain plan", () => {
const definition = SHORTCUT_DEFINITIONS.find((item) => item.id === "explainSql");
expect(definition).toMatchObject({ labelKey: "toolbar.explainPlan", scope: "editor", defaultShortcut: "Mod+E" });
expect(shortcutToCodeMirrorKey(DEFAULT_SHORTCUT_SETTINGS.explainSql)).toBe("Mod-e");
expect(findShortcutConflict("explainSql", "Mod+E", DEFAULT_SHORTCUT_SETTINGS)).toBeNull();
});
it("keeps current-view search and editor find contextual on Mod+F", () => {
const focusSearch = SHORTCUT_DEFINITIONS.find((item) => item.id === "focusSearch");
const find = SHORTCUT_DEFINITIONS.find((item) => item.id === "find");
@@ -59,14 +59,14 @@ export function createQueryEditorPostCompositionKeyGuard(options: { navigator?:
* document, so the app-level shortcut fallback also checks the editor state.
* This guard covers the keymap path when CodeMirror still dispatches it.
*/
export function createQueryEditorExecutionShortcutBindings(shortcut: string, run: Command, isComposing: (view: EditorView) => boolean): KeyBinding[] {
export function createQueryEditorExecutionShortcutBindings(shortcut: string, run: Command, isComposing: (view: EditorView) => boolean, isEnabled: () => boolean = () => true): KeyBinding[] {
if (!shortcut) return [];
return [
{
key: shortcutToCodeMirrorKey(shortcut),
preventDefault: true,
run(view) {
if (isComposing(view)) return true;
if (isComposing(view) || !isEnabled()) return true;
return run(view);
},
},
@@ -3,6 +3,7 @@ import { isMacShortcutPlatform, parseShortcutStrokes, shortcutDisplayParts } fro
export type ShortcutActionId =
| "executeSql"
| "executeSqlInNewResultTab"
| "explainSql"
| "formatSql"
| "expandSelectStar"
| "toggleLineComment"
@@ -151,6 +152,12 @@ export const SHORTCUT_DEFINITIONS: ShortcutDefinition[] = [
scope: "editor",
defaultShortcut: "Mod+\\",
},
{
id: "explainSql",
labelKey: "toolbar.explainPlan",
scope: "editor",
defaultShortcut: "Mod+E",
},
{
id: "formatSql",
labelKey: "settings.shortcutFormatSql",