Skip to content

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

feat(sql-editor): highlight the SQL code blocks (#887)

Yunwei Xiao362cbc7008d5
diff --git a/frontend/src/components/MonacoEditor/MonacoEditor.vue b/frontend/src/components/MonacoEditor/MonacoEditor.vue
index d3f081df37..8cd2d76fbd 100644
--- a/frontend/src/components/MonacoEditor/MonacoEditor.vue
+++ b/frontend/src/components/MonacoEditor/MonacoEditor.vue
@@ -15,9 +15,15 @@ import {
} from "vue";
import { useStore } from "vuex";
import { useI18n } from "vue-i18n";
-import type { editor as Editor } from "monaco-editor";
+import { editor as Editor } from "monaco-editor";
import { useMonaco } from "./useMonaco";
+import {
+ useNamespacedActions,
+ useNamespacedGetters,
+ useNamespacedState,
+} from "vuex-composition-helpers";
+
import {
TabGetters,
SqlDialect,
@@ -25,11 +31,7 @@ import {
SqlEditorState,
SheetGetters,
} from "../../types";
-import {
- useNamespacedActions,
- useNamespacedGetters,
- useNamespacedState,
-} from "vuex-composition-helpers";
+import { useLineDecorations } from "./lineDecorations";
const props = defineProps({
value: {
@@ -175,15 +177,24 @@ const init = async () => {
// when editor change selection, emit change-selection event with selected text
editorInstance.onDidChangeCursorSelection((e) => {
- const selectedText = editorInstance.getModel()?.getValueInRange({
- startLineNumber: e.selection.startLineNumber,
- startColumn: e.selection.startColumn,
- endLineNumber: e.selection.endLineNumber,
- endColumn: e.selection.endColumn,
- }) as string;
+ const selectedText = editorInstance
+ .getModel()
+ ?.getValueInRange(e.selection) as string;
emit("change-selection", selectedText);
});
+ editorInstance.onDidChangeCursorPosition((e) => {
+ const { defineLineDecorations, disposeLineDecorations } =
+ useLineDecorations(editorInstance, e.position);
+ // clear the old decorations
+ disposeLineDecorations();
+
+ // define the new decorations
+ nextTick(() => {
+ defineLineDecorations();
+ });
+ });
+
editorInstance.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.KeyS, () => {
const value = editorInstance.getValue();
emit("save", value);
@@ -227,3 +238,11 @@ watch(
}
);
</script>
+
+<style>
+.monaco-editor .cldr.sql-fragment {
+ @apply bg-indigo-600;
+ width: 4px !important;
+ margin-left: 2px;
+}
+</style>
diff --git a/frontend/src/components/MonacoEditor/lineDecorations.ts b/frontend/src/components/MonacoEditor/lineDecorations.ts
new file mode 100644
index 0000000000..dc2042ddf4
--- /dev/null
+++ b/frontend/src/components/MonacoEditor/lineDecorations.ts
@@ -0,0 +1,115 @@
+import * as monaco from "monaco-editor";
+import { ref } from "vue";
+
+const DELIMITER = ";";
+
+type FragmentRange = {
+ startLineNumber: number;
+ startColumn: number;
+ endLineNumber: number;
+ endColumn: number;
+};
+
+/**
+ * Get the ranges of SQL fragments split by the delimiter
+ * @param editor monaco.editor.IStandaloneCodeEditor
+ * @returns fragmentRangeList FragmentRange[]
+ */
+const getSQLFragmentRangeList = (
+ editor: monaco.editor.IStandaloneCodeEditor
+): FragmentRange[] => {
+ const model = editor.getModel() as monaco.editor.ITextModel;
+ const linesContent = model.getLinesContent();
+ const sqlFragmentRangeList: FragmentRange[] = [];
+ let startLineNumber = 0;
+
+ for (let i = 0; i < linesContent.length; i++) {
+ const content = linesContent[i];
+ const range: FragmentRange = new monaco.Range(0, 0, 0, 0);
+
+ const idx = content.indexOf(DELIMITER);
+ // whether the content of line is a empty content
+ const isEmptyContent = monaco.Range.isEmpty(
+ new monaco.Range(i, 0, i, content.length)
+ );
+
+ // first non-empty line
+ if (!isEmptyContent && startLineNumber === 0) {
+ startLineNumber = i + 1;
+ }
+
+ // which contains the delimiter
+ if (idx !== -1) {
+ range.startLineNumber = startLineNumber;
+ // startColumn is always first character of line
+ range.startColumn = 1;
+ // endLineNumber is the line number contains the delimiter
+ range.endLineNumber = i + 1;
+ // keep the last character of the line in the range
+ range.endColumn = idx + 2;
+
+ // jump to the next line of end line
+ startLineNumber = range.endLineNumber + 1;
+
+ sqlFragmentRangeList.push(
+ new monaco.Range(
+ range.startLineNumber,
+ range.startColumn,
+ range.endLineNumber,
+ range.endColumn
+ )
+ );
+ }
+
+ // continue count the empty content
+ if (isEmptyContent && startLineNumber > 0) {
+ startLineNumber += 1;
+ }
+ }
+ return sqlFragmentRangeList;
+};
+
+const lineDecorations = ref<string[]>([]);
+
+const useLineDecorations = (
+ editor: monaco.editor.IStandaloneCodeEditor,
+ position: monaco.Position
+) => {
+ const defineLineDecorations = () => {
+ const newDecorations: monaco.editor.IModelDeltaDecoration[] = [];
+
+ const sqlFragmentRangeList = getSQLFragmentRangeList(editor);
+
+ sqlFragmentRangeList.forEach((range) => {
+ // if the current position in the range, then highlight the range
+ if (monaco.Range.containsPosition(range, position)) {
+ newDecorations.push({
+ range: range,
+ options: {
+ isWholeLine: true,
+ linesDecorationsClassName: "sql-fragment",
+ },
+ });
+ }
+ });
+
+ lineDecorations.value = editor.deltaDecorations([], newDecorations);
+
+ return lineDecorations;
+ };
+
+ const disposeLineDecorations = () => {
+ if (lineDecorations.value && lineDecorations.value.length > 0) {
+ editor.deltaDecorations(lineDecorations.value, []);
+ lineDecorations.value = [];
+ }
+ };
+
+ return {
+ lineDecorations,
+ defineLineDecorations,
+ disposeLineDecorations,
+ };
+};
+
+export { useLineDecorations, getSQLFragmentRangeList };