feat(editor): add explain plan and format to context menu
This commit is contained in:
1 parent
69687c1a84
commit
eeeea648c1
3 files changed
+68
-2
No files matched your search
@@ -969,6 +969,12 @@ function executeInNewResultTabFromContextMenu() {
|
|||||||
focusEditor();
|
focusEditor();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function explainFromContextMenu() {
|
||||||
|
if (!canExecuteContextSql.value) return;
|
||||||
|
emit("explain");
|
||||||
|
focusEditor();
|
||||||
|
}
|
||||||
|
|
||||||
function exportQueryFromContextMenu(format: "csv" | "xlsx" | "txt") {
|
function exportQueryFromContextMenu(format: "csv" | "xlsx" | "txt") {
|
||||||
const sql = executableSql.value;
|
const sql = executableSql.value;
|
||||||
if (!sql.trim()) return;
|
if (!sql.trim()) return;
|
||||||
@@ -1194,6 +1200,7 @@ function selectSqlLineFromGutter(currentView: EditorViewType, line: { from: numb
|
|||||||
const contextMenuActions: QueryEditorContextMenuActions = {
|
const contextMenuActions: QueryEditorContextMenuActions = {
|
||||||
executeFromContextMenu,
|
executeFromContextMenu,
|
||||||
executeInNewResultTabFromContextMenu,
|
executeInNewResultTabFromContextMenu,
|
||||||
|
explainFromContextMenu,
|
||||||
requestPreviewChanges,
|
requestPreviewChanges,
|
||||||
exportQueryFromContextMenu,
|
exportQueryFromContextMenu,
|
||||||
toggleCommentFromContextMenu,
|
toggleCommentFromContextMenu,
|
||||||
@@ -1241,6 +1248,8 @@ function getContextMenuState(): QueryEditorContextMenuState {
|
|||||||
contextObjectTarget: contextObjectTarget.value,
|
contextObjectTarget: contextObjectTarget.value,
|
||||||
shortcuts: settingsStore.editorSettings.shortcuts,
|
shortcuts: settingsStore.editorSettings.shortcuts,
|
||||||
expandSelectStar: target ? () => void expandSelectStar(target) : undefined,
|
expandSelectStar: target ? () => void expandSelectStar(target) : undefined,
|
||||||
|
canExplain: props.canExplain ?? (canExecuteContextSql.value && !props.readOnly && !props.hideExecutionControls),
|
||||||
|
hasContent: (view.value?.state.doc.length ?? 0) > 0,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import {
|
|||||||
Eye,
|
Eye,
|
||||||
FileCode,
|
FileCode,
|
||||||
FoldVertical,
|
FoldVertical,
|
||||||
|
GitBranch,
|
||||||
Highlighter,
|
Highlighter,
|
||||||
MessageSquareText,
|
MessageSquareText,
|
||||||
Minimize2,
|
Minimize2,
|
||||||
@@ -50,11 +51,14 @@ export interface QueryEditorContextMenuState {
|
|||||||
contextObjectTarget: SqlObjectNavigationTarget | null;
|
contextObjectTarget: SqlObjectNavigationTarget | null;
|
||||||
shortcuts: ShortcutSettings;
|
shortcuts: ShortcutSettings;
|
||||||
expandSelectStar: (() => void) | undefined;
|
expandSelectStar: (() => void) | undefined;
|
||||||
|
canExplain?: boolean;
|
||||||
|
hasContent?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface QueryEditorContextMenuActions {
|
export interface QueryEditorContextMenuActions {
|
||||||
executeFromContextMenu: () => void;
|
executeFromContextMenu: () => void;
|
||||||
executeInNewResultTabFromContextMenu: () => void;
|
executeInNewResultTabFromContextMenu: () => void;
|
||||||
|
explainFromContextMenu?: () => void;
|
||||||
requestPreviewChanges: (sql?: string) => void;
|
requestPreviewChanges: (sql?: string) => void;
|
||||||
exportQueryFromContextMenu: (format: "csv" | "xlsx" | "txt") => void;
|
exportQueryFromContextMenu: (format: "csv" | "xlsx" | "txt") => void;
|
||||||
toggleCommentFromContextMenu: () => void;
|
toggleCommentFromContextMenu: () => void;
|
||||||
@@ -163,6 +167,13 @@ const contextMenuItems = computed<ContextMenuItem[]>(() => {
|
|||||||
icon: Play,
|
icon: Play,
|
||||||
shortcut: shortcuts.executeSqlInNewResultTab,
|
shortcut: shortcuts.executeSqlInNewResultTab,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
label: t("toolbar.explainPlan"),
|
||||||
|
action: actions.explainFromContextMenu,
|
||||||
|
disabled: state.canExplain === false || !canExecuteContextSql,
|
||||||
|
icon: GitBranch,
|
||||||
|
shortcut: shortcuts.explainSql,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
label: t("editor.previewChanges"),
|
label: t("editor.previewChanges"),
|
||||||
action: () => void actions.requestPreviewChanges(props.getState().previewContextSql),
|
action: () => void actions.requestPreviewChanges(props.getState().previewContextSql),
|
||||||
@@ -204,9 +215,9 @@ const contextMenuItems = computed<ContextMenuItem[]>(() => {
|
|||||||
shortcut: shortcuts.toggleBlockComment,
|
shortcut: shortcuts.toggleBlockComment,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: t("editor.contextMenu.formatSelectionSql"),
|
label: canCopySelectedSql ? t("editor.contextMenu.formatSelectionSql") : t("toolbar.formatSql"),
|
||||||
action: () => void actions.formatCurrentSql(),
|
action: () => void actions.formatCurrentSql(),
|
||||||
disabled: state.readOnly || !canCopySelectedSql || !canFormatSqlForDatabaseType(state.databaseType),
|
disabled: state.readOnly || (!canCopySelectedSql && !canExecuteContextSql && !state.hasContent) || !canFormatSqlForDatabaseType(state.databaseType),
|
||||||
icon: AlignLeft,
|
icon: AlignLeft,
|
||||||
shortcut: shortcuts.formatSql,
|
shortcut: shortcuts.formatSql,
|
||||||
},
|
},
|
||||||
|
|||||||
+46
@@ -17,6 +17,7 @@ function mountMenu() {
|
|||||||
const actions = {
|
const actions = {
|
||||||
executeFromContextMenu: vi.fn(),
|
executeFromContextMenu: vi.fn(),
|
||||||
executeInNewResultTabFromContextMenu: vi.fn(),
|
executeInNewResultTabFromContextMenu: vi.fn(),
|
||||||
|
explainFromContextMenu: vi.fn(),
|
||||||
requestPreviewChanges: vi.fn(),
|
requestPreviewChanges: vi.fn(),
|
||||||
exportQueryFromContextMenu: vi.fn(),
|
exportQueryFromContextMenu: vi.fn(),
|
||||||
toggleCommentFromContextMenu: vi.fn(),
|
toggleCommentFromContextMenu: vi.fn(),
|
||||||
@@ -172,6 +173,7 @@ describe("QueryEditor extracted context menu", () => {
|
|||||||
it.each([
|
it.each([
|
||||||
["editor.contextMenu.executeCurrent", "executeFromContextMenu"],
|
["editor.contextMenu.executeCurrent", "executeFromContextMenu"],
|
||||||
["settings.shortcutExecuteSqlInNewResultTab", "executeInNewResultTabFromContextMenu"],
|
["settings.shortcutExecuteSqlInNewResultTab", "executeInNewResultTabFromContextMenu"],
|
||||||
|
["toolbar.explainPlan", "explainFromContextMenu"],
|
||||||
["editor.contextMenu.screenshotSelection", "openCodeSnapshot"],
|
["editor.contextMenu.screenshotSelection", "openCodeSnapshot"],
|
||||||
["editor.contextMenu.delimitedList", "openDelimitedListDialog"],
|
["editor.contextMenu.delimitedList", "openDelimitedListDialog"],
|
||||||
["editor.contextMenu.sendToAi", "sendSelectionToAi"],
|
["editor.contextMenu.sendToAi", "sendSelectionToAi"],
|
||||||
@@ -186,6 +188,50 @@ describe("QueryEditor extracted context menu", () => {
|
|||||||
expect(actions[action]).toHaveBeenCalledTimes(1);
|
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([
|
it.each([
|
||||||
["table", "contextMenu.editStructure", "edit-table-structure"],
|
["table", "contextMenu.editStructure", "edit-table-structure"],
|
||||||
["view", "contextMenu.editView", "edit-view"],
|
["view", "contextMenu.editView", "edit-view"],
|
||||||
|
|||||||
Reference in new issue
Block a user