From f66a18d88ddc9d8c793c2a1ed9e29ff64989b047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E5=B3=B0?= <67090734+tianyifeng-druid@users.noreply.github.com> Date: Mon, 10 Aug 2026 00:07:12 +0800 Subject: [PATCH] feat(editor): add SQL intention actions (Alt+Enter style) --- .../src/components/editor/QueryEditor.vue | 217 ++++- .../src/components/ui/CustomContextMenu.vue | 19 +- .../ui/customContextMenuRegistry.ts | 17 + apps/desktop/src/i18n/locales/en.ts | 5 + apps/desktop/src/i18n/locales/es.ts | 5 + apps/desktop/src/i18n/locales/it.ts | 5 + apps/desktop/src/i18n/locales/ja.ts | 5 + apps/desktop/src/i18n/locales/ko.ts | 5 + apps/desktop/src/i18n/locales/pt-BR.ts | 5 + apps/desktop/src/i18n/locales/zh-CN.ts | 5 + apps/desktop/src/i18n/locales/zh-TW.ts | 5 + .../__tests__/sql/sqlIntentionActions.spec.ts | 278 ++++++ .../src/lib/editor/shortcutRegistry.ts | 9 +- .../src/lib/editor/sqlIntentionActions.ts | 820 ++++++++++++++++++ .../lib/sidebar/sidebarTreeMenuDescriptors.ts | 2 +- .../src/lib/sidebar/sidebarTreeRuntime.ts | 2 +- 16 files changed, 1384 insertions(+), 20 deletions(-) create mode 100644 apps/desktop/src/lib/__tests__/sql/sqlIntentionActions.spec.ts create mode 100644 apps/desktop/src/lib/editor/sqlIntentionActions.ts diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index 8d4513dd05..c4a083a354 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -114,6 +114,8 @@ import type { StatementExecutionMarker } from "@/lib/tabs/tabPresentation"; import { isSchemaAware, isSingleDatabase, supportsDatabaseNameCompletion, supportsDatabaseSchemaQualifier, supportsSqlInListPaste } from "@/lib/database/databaseFeatureSupport"; import { metadataSchemaForConnection, sqlSnippetDatabaseTypeForConnection } from "@/lib/database/jdbcDialect"; import { usesLocalOnlyEditorCompletionMetadata, usesOnDemandOnlyEditorColumnMetadata } from "@/lib/metadata/completionMetadataPolicy"; +import { loadTableMetadata } from "@/lib/metadata/tableMetadataCache"; +import { analyzeIntentionActions, prepareExpandWildcardContext, buildExpandWildcardReplacement, type IntentionAction } from "@/lib/editor/sqlIntentionActions"; import { loadObjectDdl } from "@/lib/metadata/objectDdlCache"; import { loadObjectMetadataFacet } from "@/lib/metadata/objectMetadataCache"; import { queryContextObjectActions, queryContextObjectRoute, queryTableCandidateAtSqlPosition, queryTableNavigationTargetAtSqlPosition, resolveQueryContextCandidateDatabase, resolveQueryContextObjectTarget, type QueryContextObjectAction } from "@/lib/sql/queryCursorTableTarget"; @@ -366,6 +368,121 @@ function applyDelimitedListResult(result: string) { focusEditor(); } +// ==================== Intention Popup ==================== + +interface IntentionPopupState { + visible: boolean; + // 直接复用 IntentionAction 类型,避免手动重声明导致 replacements 等字段丢失(TS2551) + actions: IntentionAction[]; + position: { x: number; y: number }; + selectedIndex: number; +} + +const intentionPopup = ref(null); + +function getIntentionActionLabel(kind: string): string { + switch (kind) { + case "expand_wildcard": + return t("intentionExpandWildcard"); + case "qualify_identifier": + return t("intentionQualifyIdentifier"); + case "unqualify_identifier": + return t("intentionUnqualifyIdentifier"); + case "batch_qualify_identifiers": + return t("intentionBatchQualifyIdentifiers"); + default: + return kind; + } +} + +function closeIntentionPopup() { + document.removeEventListener("keydown", onIntentionPopupKey); + intentionPopup.value = null; + focusEditor(); +} + +function onIntentionPopupKey(e: KeyboardEvent) { + if (!intentionPopup.value?.visible) return; + switch (e.key) { + case "Escape": + e.preventDefault(); + closeIntentionPopup(); + break; + case "ArrowDown": + e.preventDefault(); + intentionPopup.value.selectedIndex = Math.min(intentionPopup.value.selectedIndex + 1, intentionPopup.value.actions.length - 1); + break; + case "ArrowUp": + e.preventDefault(); + intentionPopup.value.selectedIndex = Math.max(intentionPopup.value.selectedIndex - 1, 0); + break; + case "Enter": + e.preventDefault(); + executeIntentionAction(intentionPopup.value.actions[intentionPopup.value.selectedIndex]); + break; + } +} + +function executeIntentionAction(action: IntentionPopupState["actions"][number]) { + if (!intentionPopup.value) return; + closeIntentionPopup(); + + const currentView = view.value; + if (!currentView) return; + + switch (action.kind) { + case "expand_wildcard": { + const sql = currentView.state.doc.toString(); + const cursor = currentView.state.selection.main.head; + void (async () => { + try { + const ctx = prepareExpandWildcardContext(sql, cursor, props.databaseType, sqlBehaviorDialect()); + if (!ctx) return; + + const replacement = await buildExpandWildcardReplacement(props.databaseType, ctx.rowSources, async (source) => { + const schema = source.metadataTarget?.schema; + const tableName = source.metadataTarget?.table ?? source.name; + if (!tableName) return []; + const result = await loadTableMetadata({ + connectionId: props.connectionId ?? "", + database: props.database ?? "", + schema, + tableName, + databaseType: props.databaseType ?? "mysql", + force: false, + }); + return result.metadata.columns.map((c) => c.name); + }); + const v = view.value; + if (!v || v.state.doc.toString() !== sql) return; + v.dispatch({ changes: { from: ctx.starSpan.start, to: ctx.starSpan.end, insert: replacement } }); + } catch { + // metadata load failed + } + })(); + break; + } + case "qualify_identifier": + case "unqualify_identifier": + currentView.dispatch({ + changes: { from: action.span.start, to: action.span.end, insert: action.replacement }, + }); + break; + + case "batch_qualify_identifiers": { + // 按从后往前的顺序逐一替换,避免 offset 漂移 + const reps = action.replacements ?? []; + for (let i = reps.length - 1; i >= 0; i--) { + const r = reps[i]; + currentView.dispatch({ + changes: { from: r.span.start, to: r.span.end, insert: r.replacement }, + }); + } + break; + } + } +} + const executeContextMenuLabel = computed(() => t(hasSelectedSql.value ? "editor.contextMenu.executeSelection" : "editor.contextMenu.executeCurrent")); interface EditorGestureEvent extends Event { @@ -1525,6 +1642,41 @@ const contextMenuItems = computed(() => { ]; }); +function handleSqlIntentionActions(currentView: EditorViewType): boolean { + if (props.readOnly) return false; + try { + const sql = currentView.state.doc.toString(); + const sel = currentView.state.selection.main; + + const actions = analyzeIntentionActions({ + sql, + cursor: sel.head, + databaseType: props.databaseType, + dialect: sqlBehaviorDialect(), + selection: sel.from !== sel.to ? { from: sel.from, to: sel.to } : undefined, + }); + + if (actions.length === 0) return false; + + // 计算光标视口坐标,用于定位弹出菜单 + const coords = currentView.coordsAtPos(sel.head); + if (!coords) return false; + + // 显示弹出菜单(参考 DataGrip Alt+Enter 意图操作弹出菜单) + intentionPopup.value = { + visible: true, + actions, + position: { x: coords.right + 8, y: coords.bottom + 4 }, + selectedIndex: 0, + }; + document.addEventListener("keydown", onIntentionPopupKey); + return true; + } catch (err) { + console.error("[SQL Intention] error:", err); + return false; + } +} + function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view"))["keymap"]) { const shortcuts = normalizeShortcutSettings(settingsStore.editorSettings.shortcuts); const Prec = codeMirrorPrec; @@ -1585,6 +1737,7 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view") if (sql.trim()) emit("sendSelectionToAi", sql); return true; }), + ...binding(shortcuts.sqlIntentionActions, handleSqlIntentionActions), ...createQueryEditorSearchKeymap({ openSearch, openReplace, @@ -1606,7 +1759,7 @@ function extendQueryEditorSelectionForView(currentView: EditorViewType): boolean const language = databaseType === "redis" || databaseType === "mongodb" || databaseType === "elasticsearch" || databaseType === "victoriametrics" ? "text" : "sql"; return extendQueryEditorSelection(currentView, { databaseType, - dialect: props.syntaxDialect ?? props.dialect, + dialect: sqlBehaviorDialect(), language, }); } @@ -5269,6 +5422,17 @@ defineExpose({ + + +
+
+
+ 💡 + {{ action.label || getIntentionActionLabel(action.kind) }} +
+
+
+
@@ -5482,3 +5646,54 @@ defineExpose({ height: 16px; } + + diff --git a/apps/desktop/src/components/ui/CustomContextMenu.vue b/apps/desktop/src/components/ui/CustomContextMenu.vue index 790ec7ca19..3bf9c6a475 100644 --- a/apps/desktop/src/components/ui/CustomContextMenu.vue +++ b/apps/desktop/src/components/ui/CustomContextMenu.vue @@ -1,23 +1,10 @@