diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index fb3ba09252..be186d33ac 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -969,6 +969,12 @@ function executeInNewResultTabFromContextMenu() { focusEditor(); } +function explainFromContextMenu() { + if (!canExecuteContextSql.value) return; + emit("explain"); + focusEditor(); +} + function exportQueryFromContextMenu(format: "csv" | "xlsx" | "txt") { const sql = executableSql.value; if (!sql.trim()) return; @@ -1194,6 +1200,7 @@ function selectSqlLineFromGutter(currentView: EditorViewType, line: { from: numb const contextMenuActions: QueryEditorContextMenuActions = { executeFromContextMenu, executeInNewResultTabFromContextMenu, + explainFromContextMenu, requestPreviewChanges, exportQueryFromContextMenu, toggleCommentFromContextMenu, @@ -1241,6 +1248,8 @@ function getContextMenuState(): QueryEditorContextMenuState { contextObjectTarget: contextObjectTarget.value, shortcuts: settingsStore.editorSettings.shortcuts, expandSelectStar: target ? () => void expandSelectStar(target) : undefined, + canExplain: props.canExplain ?? (canExecuteContextSql.value && !props.readOnly && !props.hideExecutionControls), + hasContent: (view.value?.state.doc.length ?? 0) > 0, }; } diff --git a/apps/desktop/src/components/editor/QueryEditorContextMenu.vue b/apps/desktop/src/components/editor/QueryEditorContextMenu.vue index e1ed67c619..277d0f8a2f 100644 --- a/apps/desktop/src/components/editor/QueryEditorContextMenu.vue +++ b/apps/desktop/src/components/editor/QueryEditorContextMenu.vue @@ -15,6 +15,7 @@ import { Eye, FileCode, FoldVertical, + GitBranch, Highlighter, MessageSquareText, Minimize2, @@ -50,11 +51,14 @@ export interface QueryEditorContextMenuState { contextObjectTarget: SqlObjectNavigationTarget | null; shortcuts: ShortcutSettings; expandSelectStar: (() => void) | undefined; + canExplain?: boolean; + hasContent?: boolean; } export interface QueryEditorContextMenuActions { executeFromContextMenu: () => void; executeInNewResultTabFromContextMenu: () => void; + explainFromContextMenu?: () => void; requestPreviewChanges: (sql?: string) => void; exportQueryFromContextMenu: (format: "csv" | "xlsx" | "txt") => void; toggleCommentFromContextMenu: () => void; @@ -163,6 +167,13 @@ const contextMenuItems = computed(() => { icon: Play, shortcut: shortcuts.executeSqlInNewResultTab, }, + { + label: t("toolbar.explainPlan"), + action: actions.explainFromContextMenu, + disabled: state.canExplain === false || !canExecuteContextSql, + icon: GitBranch, + shortcut: shortcuts.explainSql, + }, { label: t("editor.previewChanges"), action: () => void actions.requestPreviewChanges(props.getState().previewContextSql), @@ -204,9 +215,9 @@ const contextMenuItems = computed(() => { shortcut: shortcuts.toggleBlockComment, }, { - label: t("editor.contextMenu.formatSelectionSql"), + label: canCopySelectedSql ? t("editor.contextMenu.formatSelectionSql") : t("toolbar.formatSql"), action: () => void actions.formatCurrentSql(), - disabled: state.readOnly || !canCopySelectedSql || !canFormatSqlForDatabaseType(state.databaseType), + disabled: state.readOnly || (!canCopySelectedSql && !canExecuteContextSql && !state.hasContent) || !canFormatSqlForDatabaseType(state.databaseType), icon: AlignLeft, shortcut: shortcuts.formatSql, }, diff --git a/apps/desktop/src/components/editor/__tests__/QueryEditorContextMenu.integration.spec.ts b/apps/desktop/src/components/editor/__tests__/QueryEditorContextMenu.integration.spec.ts index b095aa998f..c0f12c5769 100644 --- a/apps/desktop/src/components/editor/__tests__/QueryEditorContextMenu.integration.spec.ts +++ b/apps/desktop/src/components/editor/__tests__/QueryEditorContextMenu.integration.spec.ts @@ -17,6 +17,7 @@ function mountMenu() { const actions = { executeFromContextMenu: vi.fn(), executeInNewResultTabFromContextMenu: vi.fn(), + explainFromContextMenu: vi.fn(), requestPreviewChanges: vi.fn(), exportQueryFromContextMenu: vi.fn(), toggleCommentFromContextMenu: vi.fn(), @@ -172,6 +173,7 @@ describe("QueryEditor extracted context menu", () => { it.each([ ["editor.contextMenu.executeCurrent", "executeFromContextMenu"], ["settings.shortcutExecuteSqlInNewResultTab", "executeInNewResultTabFromContextMenu"], + ["toolbar.explainPlan", "explainFromContextMenu"], ["editor.contextMenu.screenshotSelection", "openCodeSnapshot"], ["editor.contextMenu.delimitedList", "openDelimitedListDialog"], ["editor.contextMenu.sendToAi", "sendSelectionToAi"], @@ -186,6 +188,50 @@ describe("QueryEditor extracted context menu", () => { expect(actions[action]).toHaveBeenCalledTimes(1); }); + it("enables explain plan only when executable SQL exists and explain is permitted", async () => { + const { state, actions, open } = mountMenu(); + await open(); + expect(button("toolbar.explainPlan").disabled).toBe(true); + + await open(() => { + state.executableSql = "SELECT * FROM users"; + }); + expect(button("toolbar.explainPlan").disabled).toBe(false); + button("toolbar.explainPlan").click(); + expect(actions.explainFromContextMenu).toHaveBeenCalledTimes(1); + + await open(() => { + state.canExplain = false; + }); + expect(button("toolbar.explainPlan").disabled).toBe(true); + }); + + it("dynamically switches format SQL label and enablement between selection and whole document", async () => { + const { state, actions, open } = mountMenu(); + await open(); + expect(button("toolbar.formatSql").disabled).toBe(true); + + await open(() => { + state.hasContent = true; + state.executableSql = "SELECT 1"; + }); + expect(button("toolbar.formatSql").disabled).toBe(false); + button("toolbar.formatSql").click(); + expect(actions.formatCurrentSql).toHaveBeenCalledTimes(1); + + await open(() => { + state.selectedSql = "SELECT 1"; + }); + expect(button("editor.contextMenu.formatSelectionSql").disabled).toBe(false); + button("editor.contextMenu.formatSelectionSql").click(); + expect(actions.formatCurrentSql).toHaveBeenCalledTimes(2); + + await open(() => { + state.readOnly = true; + }); + expect(button("editor.contextMenu.formatSelectionSql").disabled).toBe(true); + }); + it.each([ ["table", "contextMenu.editStructure", "edit-table-structure"], ["view", "contextMenu.editView", "edit-view"],