Skip to content

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

chore: highlight the active range (#15333)

ecmadao194ab8277d33
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index 500754ad08..9dd61bdefe 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -3,7 +3,9 @@
ref="textModelEditorRef"
class="bb-monaco-editor"
:model="model"
- @update:content="handleChange"
+ :line-highlights="lineHighlights"
+ @update:content="handleContentChange"
+ @update:selection="handleSelectionChange"
>
<template v-if="$slots['corner-prefix']" #corner-prefix>
<slot name="corner-prefix" />
@@ -15,13 +17,16 @@
</template>
<script setup lang="ts">
+import { editor, type IRange } from "monaco-editor";
import { v4 as uuidv4 } from "uuid";
-import { computed, toRef } from "vue";
+import { computed, toRef, watchEffect } from "vue";
import { ref } from "vue";
import type { Language } from "@/types";
+import { isDev } from "@/utils";
import MonacoTextModelEditor from "./MonacoTextModelEditor.vue";
import { useSQLParser } from "./composables";
import { useMonacoTextModel, getUriByFilename } from "./text-model";
+import type { LineHighlightOption, Selection } from "./types";
import { extensionNameOfLanguage } from "./utils";
const textModelEditorRef = ref<InstanceType<typeof MonacoTextModelEditor>>();
@@ -39,6 +44,7 @@ const props = withDefaults(
);
const emit = defineEmits<{
(event: "update:content", content: string): void;
+ (event: "update:selection", selection: Selection | null): void;
}>();
const content = computed({
@@ -51,6 +57,120 @@ const content = computed({
});
const { getActiveStatementRange } = useSQLParser();
+const activeLineNumber = ref<number | undefined>();
+const activeSelection = ref<Selection | null>();
+
+const handleSelectionChange = (selection: Selection | null) => {
+ activeSelection.value = selection;
+ emit("update:selection", selection);
+};
+
+const handleContentChange = (value: string) => {
+ content.value = value;
+};
+
+watchEffect(() => {
+ textModelEditorRef.value?.codeEditor?.onDidChangeCursorPosition(
+ (e: editor.ICursorPositionChangedEvent) => {
+ activeLineNumber.value = e.position.lineNumber;
+ }
+ );
+});
+
+const activeRangeByCursor = computed((): IRange | undefined => {
+ if (activeLineNumber.value === undefined) {
+ return;
+ }
+ if (!textModelEditorRef.value?.codeEditor) {
+ return;
+ }
+ const model = textModelEditorRef.value.codeEditor.getModel();
+ if (!model) {
+ return;
+ }
+ const range = getActiveStatementRange(
+ getUriByFilename(filename.value).toString(),
+ activeLineNumber.value
+ );
+ if (!range) {
+ return;
+ }
+
+ // Check if the last line is empty
+ const lastLineStatement = model.getValueInRange({
+ startLineNumber: range.endLineNumber,
+ startColumn: 1,
+ endLineNumber: range.endLineNumber,
+ endColumn: range.endColumn,
+ });
+ if (!lastLineStatement && range.endLineNumber > range.startLineNumber) {
+ const newRange = {
+ startLineNumber: range.startLineNumber,
+ startColumn: range.startColumn,
+ endLineNumber: range.endLineNumber - 1,
+ endColumn: Infinity,
+ };
+ if (activeLineNumber.value > newRange.endLineNumber) {
+ return;
+ }
+ return newRange;
+ }
+
+ return range;
+});
+
+const activeRange = computed((): IRange | undefined => {
+ const rangeByCursor = activeRangeByCursor.value;
+ const rangeBySelection = activeSelection.value;
+
+ if (!rangeBySelection) {
+ return rangeByCursor;
+ }
+ if (
+ rangeBySelection.startLineNumber === rangeBySelection.endLineNumber &&
+ rangeBySelection.startColumn === rangeBySelection.endColumn
+ ) {
+ // Means no selection, just active cursor.
+ return rangeByCursor;
+ }
+
+ if (!rangeByCursor) {
+ return rangeBySelection;
+ }
+
+ // Calculate the maximum range
+ return {
+ startLineNumber: Math.min(
+ rangeByCursor.startLineNumber,
+ rangeBySelection.startLineNumber
+ ),
+ startColumn: Math.min(
+ rangeByCursor.startColumn,
+ rangeBySelection.startColumn
+ ),
+ endLineNumber: Math.max(
+ rangeByCursor.endLineNumber,
+ rangeBySelection.endLineNumber
+ ),
+ endColumn: Math.max(rangeByCursor.endColumn, rangeBySelection.endColumn),
+ };
+});
+
+const lineHighlights = computed((): LineHighlightOption[] => {
+ const range = activeRange.value;
+ if (!range || !isDev()) {
+ return [];
+ }
+ return [
+ {
+ startLineNumber: range.startLineNumber,
+ endLineNumber: range.endLineNumber,
+ options: {
+ blockClassName: "border border-accent",
+ },
+ },
+ ];
+});
const filename = computed(() => {
if (props.filename) return props.filename;
@@ -60,36 +180,17 @@ const filename = computed(() => {
const model = useMonacoTextModel(filename, content, toRef(props, "language"));
-const handleChange = (value: string) => {
- emit("update:content", value);
-};
-
defineExpose({
get editor() {
return textModelEditorRef.value;
},
- getActiveStatementByCursor: () => {
- if (!textModelEditorRef.value || !textModelEditorRef.value.codeEditor) {
- return "";
- }
- const model = textModelEditorRef.value.codeEditor.getModel();
- if (!model) {
- return "";
- }
-
- const position = textModelEditorRef.value.codeEditor.getPosition();
- if (!position) {
- return "";
- }
- const range = getActiveStatementRange(
- getUriByFilename(filename.value).toString(),
- position.lineNumber
- );
- if (!range) {
+ getActiveStatement: () => {
+ if (!activeRange.value) {
return "";
}
- textModelEditorRef.value.codeEditor.setSelection(range);
- return model.getValueInRange(range);
+ const model = textModelEditorRef.value?.codeEditor?.getModel();
+ textModelEditorRef.value?.codeEditor?.setSelection(activeRange.value);
+ return model?.getValueInRange(activeRange.value);
},
});
</script>
diff --git a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
index 5f39f9e8cf..e1f0f34446 100644
--- a/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoTextModelEditor.vue
@@ -199,8 +199,8 @@ onMounted(async () => {
toRef(props, "formatContentOptions")
);
const content = useContent(monaco, editor);
- const selectedContent = useSelectedContent(editor);
- const selection = useSelection(monaco, editor);
+ const selection = useSelection(editor);
+ const selectedContent = useSelectedContent(editor, selection);
useAdvices(monaco, editor, toRef(props, "advices"));
useLineHighlights(monaco, editor, toRef(props, "lineHighlights"));
useAutoHeight(monaco, editor, containerRef, toRef(props, "autoHeight"));
diff --git a/frontend/src/components/MonacoEditor/composables/useLineHighlights.ts b/frontend/src/components/MonacoEditor/composables/useLineHighlights.ts
index e0b9b8058d..a4cbf34c04 100644
--- a/frontend/src/components/MonacoEditor/composables/useLineHighlights.ts
+++ b/frontend/src/components/MonacoEditor/composables/useLineHighlights.ts
@@ -8,13 +8,6 @@ export const useLineHighlights = (
editor: monaco.editor.IStandaloneCodeEditor,
options: MaybeRef<LineHighlightOption[]>
) => {
- const OverviewRulerPositionMap = {
- LEFT: monaco.editor.OverviewRulerLane.Left,
- RIGHT: monaco.editor.OverviewRulerLane.Right,
- CENTER: monaco.editor.OverviewRulerLane.Center,
- FULL: monaco.editor.OverviewRulerLane.Full,
- };
-
watchEffect((onCleanup) => {
const opts = unref(options);
requestAnimationFrame(() => {
@@ -22,24 +15,17 @@ export const useLineHighlights = (
opts.map((opt) => {
return {
range: new monaco.Range(
- opt.lineNumber,
+ opt.startLineNumber,
1,
- opt.lineNumber,
+ opt.endLineNumber,
Infinity
),
options: {
- isWholeLine: true,
- inlineClassName: opt.className,
- overviewRuler: opt.overviewRuler
- ? {
- color: opt.overviewRuler.color,
- position:
- OverviewRulerPositionMap[opt.overviewRuler.position],
- }
- : undefined,
+ ...opt.options,
+ blockPadding: [3, 3, 3, 3],
stickiness:
monaco.editor.TrackedRangeStickiness
- .NeverGrowsWhenTypingAtEdges,
+ .AlwaysGrowsWhenTypingAtEdges,
},
};
})
diff --git a/frontend/src/components/MonacoEditor/composables/useSQLParser.ts b/frontend/src/components/MonacoEditor/composables/useSQLParser.ts
index b9b3deff3f..82ea931f8f 100644
--- a/frontend/src/components/MonacoEditor/composables/useSQLParser.ts
+++ b/frontend/src/components/MonacoEditor/composables/useSQLParser.ts
@@ -1,6 +1,8 @@
import { useLocalStorage } from "@vueuse/core";
+import { orderBy } from "lodash-es";
import type monaco from "monaco-editor";
import { watchEffect } from "vue";
+import { MonacoEditorReady } from "../editor";
interface WebSocketMessage {
method: string;
@@ -30,37 +32,40 @@ export const useSQLParser = () => {
);
watchEffect(() => {
- import("../lsp-client").then(({ connectionWebSocket }) => {
- connectionWebSocket.value?.then((ws) => {
- ws.addEventListener("message", (msg) => {
- try {
- if (!msg || !msg.data) {
- return;
+ MonacoEditorReady.then(() => {
+ import("../lsp-client").then(({ connectionWebSocket }) => {
+ connectionWebSocket.value?.then((ws) => {
+ ws.addEventListener("message", (msg) => {
+ try {
+ if (!msg || !msg.data) {
+ return;
+ }
+ const data = JSON.parse(msg.data) as WebSocketMessage;
+ if (data.method !== "$/textDocument/statementRanges") {
+ return;
+ }
+ const rangeMessage = data.params as StatementRangeMessage;
+ if (!rangeMessage.uri || !Array.isArray(rangeMessage.ranges)) {
+ return;
+ }
+ statementRangeByUri.value.set(
+ rangeMessage.uri,
+ orderBy(rangeMessage.ranges, (range) => range.start, "asc").map(
+ (range) => {
+ // The position starts from 1 in the editor.
+ return {
+ startLineNumber: range.start.line + 1,
+ endLineNumber: range.end.line + 1,
+ startColumn: range.start.character + 1,
+ endColumn: range.end.character + 1,
+ };
+ }
+ )
+ );
+ } catch {
+ // nothing
}
-
- const data = JSON.parse(msg.data) as WebSocketMessage;
- if (data.method !== "$/textDocument/statementRanges") {
- return;
- }
- const rangeMessage = data.params as StatementRangeMessage;
- if (!rangeMessage.uri || !Array.isArray(rangeMessage.ranges)) {
- return;
- }
- statementRangeByUri.value.set(
- rangeMessage.uri,
- rangeMessage.ranges.map((range) => {
- // The position starts from 1 in the editor.
- return {
- startLineNumber: range.start.line + 1,
- endLineNumber: range.end.line + 1,
- startColumn: range.start.character + 1,
- endColumn: range.end.character + 1,
- };
- })
- );
- } catch {
- // nothing
- }
+ });
});
});
});
@@ -70,11 +75,12 @@ export const useSQLParser = () => {
uri: string,
line: number
): monaco.IRange | undefined => {
- if (!statementRangeByUri.value.has(uri)) {
+ const ranges = statementRangeByUri.value.get(uri);
+ if (!ranges || !Array.isArray(ranges)) {
return undefined;
}
- for (const range of statementRangeByUri.value.get(uri)!) {
+ for (const range of ranges) {
if (range.startLineNumber <= line && range.endLineNumber >= line) {
return range;
}
diff --git a/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts b/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
index f6aac49663..8bdfa5c524 100644
--- a/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
+++ b/frontend/src/components/MonacoEditor/composables/useSelectedContent.ts
@@ -1,45 +1,18 @@
import type monaco from "monaco-editor";
-import { ref } from "vue";
+import { ref, type ShallowRef, watchEffect } from "vue";
export const useSelectedContent = (
- editor: monaco.editor.IStandaloneCodeEditor
+ editor: monaco.editor.IStandaloneCodeEditor,
+ selection: ShallowRef<monaco.Selection | null>
) => {
- const selectedContent = ref(getSelectedContent(editor));
- const update = () => {
- selectedContent.value = getSelectedContent(editor);
- };
+ const selectedContent = ref<string>("");
- editor.onDidChangeCursorSelection(update);
- editor.onDidChangeModel(update);
- editor.onDidChangeModelContent(update);
+ watchEffect(() => {
+ const model = editor.getModel();
+ if (selection.value && model) {
+ selectedContent.value = model.getValueInRange(selection.value);
+ }
+ });
return selectedContent;
};
-
-const getSelectedContent = (editor: monaco.editor.IStandaloneCodeEditor) => {
- const model = editor.getModel();
- if (!model) return "";
- const selection = editor.getSelection();
- if (!selection) return "";
-
- 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: 1,
- endLineNumber: position.lineNumber + 1,
- endColumn: 0,
- });
-};
diff --git a/frontend/src/components/MonacoEditor/composables/useSelection.ts b/frontend/src/components/MonacoEditor/composables/useSelection.ts
index a7dce15724..170ac0bf66 100644
--- a/frontend/src/components/MonacoEditor/composables/useSelection.ts
+++ b/frontend/src/components/MonacoEditor/composables/useSelection.ts
@@ -1,11 +1,7 @@
import type monaco from "monaco-editor";
import { shallowRef } from "vue";
-import type { MonacoModule } from "../types";
-export const useSelection = (
- monaco: MonacoModule,
- editor: monaco.editor.IStandaloneCodeEditor
-) => {
+export const useSelection = (editor: monaco.editor.IStandaloneCodeEditor) => {
const selection = shallowRef<monaco.Selection | null>(getSelection(editor));
const update = () => {
selection.value = getSelection(editor);
diff --git a/frontend/src/components/MonacoEditor/types.ts b/frontend/src/components/MonacoEditor/types.ts
index 81ec2d1032..d027672dc9 100644
--- a/frontend/src/components/MonacoEditor/types.ts
+++ b/frontend/src/components/MonacoEditor/types.ts
@@ -21,19 +21,10 @@ export type AdviceOption = {
endColumn: number; // starts from 1
};
-export type LineHighlightOverviewRulerPosition =
- | "FULL"
- | "CENTER"
- | "LEFT"
- | "RIGHT";
-export type LineHighlightOverviewRulerOption = {
- color: string;
- position: LineHighlightOverviewRulerPosition;
-};
export type LineHighlightOption = {
- className: string;
- lineNumber: number; // starts from 1
- overviewRuler?: LineHighlightOverviewRulerOption;
+ startLineNumber: number; // starts from 1
+ endLineNumber: number; // starts from 1
+ options: monaco.editor.IModelDecorationOptions;
};
export const SupportedLanguages: monaco.languages.ILanguageExtensionPoint[] = [
diff --git a/frontend/src/plugins/ai/components/editor-actions.ts b/frontend/src/plugins/ai/components/editor-actions.ts
index 8536cd1425..cd74bb365e 100644
--- a/frontend/src/plugins/ai/components/editor-actions.ts
+++ b/frontend/src/plugins/ai/components/editor-actions.ts
@@ -2,7 +2,10 @@ import type monaco from "monaco-editor";
import type { MaybeRef } from "vue";
import { computed, shallowRef, unref, watchEffect } from "vue";
import type { MonacoModule } from "@/components/MonacoEditor";
-import { useSelectedContent } from "@/components/MonacoEditor/composables";
+import {
+ useSelectedContent,
+ useSelection,
+} from "@/components/MonacoEditor/composables";
import { useTextModelLanguage } from "@/components/MonacoEditor/composables/common";
import { useEditorContextKey } from "@/components/MonacoEditor/utils";
import type { AIContext, ChatAction } from "../types";
@@ -38,7 +41,8 @@ export const useAIActions = async (
contentEmpty.set(checkContentEmpty());
});
- const selectedContent = useSelectedContent(editor);
+ const selection = useSelection(editor);
+ const selectedContent = useSelectedContent(editor, selection);
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 5c0957ec10..b11b1fb772 100644
--- a/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue
+++ b/frontend/src/views/sql-editor/EditorPanel/StandardPanel/SQLEditor.vue
@@ -158,12 +158,7 @@ const getActiveStatement = () => {
if (!tab) {
return "";
}
- return (
- tab.selectedStatement ||
- monacoEditorRef.value?.getActiveStatementByCursor() ||
- tab.statement ||
- ""
- );
+ return monacoEditorRef.value?.getActiveStatement() || tab.statement || "";
};
const runQueryAction = ({