Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: exec query based on cursor (#15060)

ecmadaoa12579516592
diff --git a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
index 0bd037a26b..5f39f9e8cf 100644
--- a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
@@ -199,7 +199,7 @@ onMounted(async () => {
toRef(props, "formatContentOptions")
);
const content = useContent(monaco, editor);
- const selectedContent = useSelectedContent(monaco, editor);
+ const selectedContent = useSelectedContent(editor);
const selection = useSelection(monaco, editor);
useAdvices(monaco, editor, toRef(props, "advices"));
useLineHighlights(monaco, editor, toRef(props, "lineHighlights"));
diff --git a/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts b/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
index ff5713e361..b35ff65d65 100644
--- a/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
+++ b/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
@@ -1,9 +1,7 @@
import type monaco from "monaco-editor";
import { ref } from "vue";
-import type { MonacoModule } from "../types";
export const useSelectedContent = (
- monaco: MonacoModule,
editor: monaco.editor.IStandaloneCodeEditor
) => {
const selectedContent = ref(getSelectedContent(editor));
@@ -26,3 +24,22 @@ const getSelectedContent = (editor: monaco.editor.IStandaloneCodeEditor) => {
return model.getValueInRange(selection);
};
+
+export const getActiveContentByCursor = (
+ editor: monaco.editor.IStandaloneCodeEditor
+) => {
+ const model = editor.getModel();
+ if (!model) {
+ return "";
+ }
+ const position = editor.getPosition();
+ if (!position) {
+ return "";
+ }
+ return model.getValueInRange({
+ startLineNumber: position.lineNumber,
+ startColumn: 0,
+ endLineNumber: position.lineNumber,
+ endColumn: editor.getBottomForLineNumber(position.lineNumber),
+ });
+};
diff --git a/frontend/src/plugins/ai/components/editor-actions.ts b/frontend/src/plugins/ai/components/editor-actions.ts
index 9345dd6177..8536cd1425 100644
--- a/frontend/src/plugins/ai/components/editor-actions.ts
+++ b/frontend/src/plugins/ai/components/editor-actions.ts
@@ -38,7 +38,7 @@ export const useAIActions = async (
contentEmpty.set(checkContentEmpty());
});
- const selectedContent = useSelectedContent(monaco, editor);
+ const selectedContent = useSelectedContent(editor);
useEditorContextKey(
editor,
"bb.ai.selectedContentEmpty",
diff --git a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue
index 3d4590ec7a..0aae0033bb 100644
--- a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue
@@ -39,6 +39,7 @@ import type {
} from "@/components/MonacoEditor";
import type { Selection as MonacoSelection } from "@/components/MonacoEditor";
import MonacoEditor from "@/components/MonacoEditor/MonacoEditor.vue";
+import { getActiveContentByCursor } from "@/components/MonacoEditor/composables";
import {
extensionNameOfLanguage,
formatEditorContent,
@@ -151,12 +152,24 @@ const handleSaveSheet = () => {
editorEvents.emit("save-sheet", { tab });
};
-const runQueryAction = (explain = false, newTab = false) => {
+const runQueryAction = ({
+ editor,
+ explain = false,
+ newTab = false,
+}: {
+ editor: IStandaloneCodeEditor;
+ explain: boolean;
+ newTab: boolean;
+}) => {
const tab = tabStore.currentTab;
if (!tab) {
return;
}
- const statement = tab.selectedStatement || tab.statement || "";
+ const statement =
+ tab.selectedStatement ||
+ getActiveContentByCursor(editor) ||
+ tab.statement ||
+ "";
const params: SQLEditorQueryParams = {
connection: { ...tab.connection },
statement,
@@ -188,7 +201,7 @@ const handleEditorReady = (
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter],
contextMenuGroupId: "operation",
contextMenuOrder: 0,
- run: () => runQueryAction(false),
+ run: () => runQueryAction({ editor, explain: false, newTab: false }),
});
editor.addAction({
id: "RunQueryInNewTab",
@@ -198,7 +211,7 @@ const handleEditorReady = (
],
contextMenuGroupId: "operation",
contextMenuOrder: 0,
- run: () => runQueryAction(false, /* newTab */ true),
+ run: () => runQueryAction({ editor, explain: false, newTab: true }),
});
editor.addAction({
id: "ExplainQuery",
@@ -206,7 +219,7 @@ const handleEditorReady = (
keybindings: [monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyE],
contextMenuGroupId: "operation",
contextMenuOrder: 0,
- run: () => runQueryAction(true),
+ run: () => runQueryAction({ editor, explain: true, newTab: false }),
});
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
handleSaveSheet();
@@ -220,7 +233,11 @@ const handleEditorReady = (
showAIPanel.value = true;
const tab = tabStore.currentTab;
- const statement = tab?.selectedStatement || tab?.statement;
+ const statement =
+ tab?.selectedStatement ||
+ getActiveContentByCursor(editor) ||
+ tab?.statement ||
+ "";
if (!statement) return;
await nextAnimationFrame();