diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vue index b0b75b501e..c87bc30737 100644 --- a/apps/desktop/src/components/editor/QueryEditor.vue +++ b/apps/desktop/src/components/editor/QueryEditor.vue @@ -151,6 +151,7 @@ let codeMirrorSnippetCompletion: typeof import("@codemirror/autocomplete").snipp let codeMirrorCompletionStatus: typeof import("@codemirror/autocomplete").completionStatus | null = null; let codeMirrorAcceptCompletion: typeof import("@codemirror/autocomplete").acceptCompletion | null = null; let codeMirrorIndentMore: typeof import("@codemirror/commands").indentMore | null = null; +let codeMirrorInsertNewlineKeepIndent: typeof import("@codemirror/commands").insertNewlineKeepIndent | null = null; let setSqlDiagnosticsEffect: import("@codemirror/state").StateEffectType | null = null; let semanticDiagnostics: SqlSemanticDiagnostic[] = []; let semanticDiagnosticTimer: ReturnType | null = null; @@ -269,12 +270,22 @@ function handleTab(view: EditorViewType): boolean { return true; } +function executeCurrentSql() { + if (view.value) emit("execute", executableSqlFromView(view.value)); + return true; +} + function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view"))["keymap"]) { const shortcuts = settingsStore.editorSettings.shortcuts; const Prec = codeMirrorPrec; return [ Prec?.high( codeMirrorKeymap.of([ + { + key: "Enter", + run: codeMirrorInsertNewlineKeepIndent ?? undefined, + shift: codeMirrorInsertNewlineKeepIndent ?? undefined, + }, { key: shortcutToCodeMirrorKey(shortcuts.find), preventDefault: true, @@ -285,6 +296,19 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view") preventDefault: true, run: openReplace, }, + { + key: shortcutToCodeMirrorKey(shortcuts.executeSql), + preventDefault: true, + run: executeCurrentSql, + }, + { + key: shortcutToCodeMirrorKey(shortcuts.saveSql), + preventDefault: true, + run: () => { + emit("save"); + return true; + }, + }, ]), ) ?? [], codeMirrorKeymap.of([ @@ -316,22 +340,6 @@ function runKeymapExtension(codeMirrorKeymap: (typeof import("@codemirror/view") return true; }, }, - { - key: shortcutToCodeMirrorKey(shortcuts.executeSql), - preventDefault: true, - run: () => { - if (view.value) emit("execute", executableSqlFromView(view.value)); - return true; - }, - }, - { - key: shortcutToCodeMirrorKey(shortcuts.saveSql), - preventDefault: true, - run: () => { - emit("save"); - return true; - }, - }, { key: shortcutToCodeMirrorKey(shortcuts.acceptCompletion), run: (view) => codeMirrorAcceptCompletion?.(view) ?? false, @@ -911,10 +919,26 @@ onMounted(async () => { if (!editorRef.value) return; const [ - { EditorView, keymap, rectangularSelection, hoverTooltip, showTooltip, Decoration, tooltips }, + { + EditorView, + keymap, + rectangularSelection, + hoverTooltip, + showTooltip, + Decoration, + tooltips, + lineNumbers, + highlightActiveLineGutter, + highlightSpecialChars, + drawSelection, + dropCursor, + crosshairCursor, + ViewPlugin, + layer, + RectangleMarker, + }, { EditorState, Compartment, Prec, StateEffect, StateField }, { sql, MSSQL, MySQL, PostgreSQL, SQLDialect }, - { basicSetup }, { autocompletion, startCompletion, @@ -923,17 +947,19 @@ onMounted(async () => { closeBracketsKeymap, snippetCompletion, completionStatus, + completionKeymap, }, - { indentMore }, - { bracketMatching }, + { indentMore, insertNewlineKeepIndent, history, defaultKeymap, historyKeymap }, + { bracketMatching, foldGutter, indentOnInput, syntaxHighlighting, defaultHighlightStyle, foldKeymap }, + { searchKeymap }, ] = await Promise.all([ import("@codemirror/view"), import("@codemirror/state"), import("@codemirror/lang-sql"), - import("codemirror"), import("@codemirror/autocomplete"), import("@codemirror/commands"), import("@codemirror/language"), + import("@codemirror/search"), ]); editorViewModule = { EditorView, keymap, rectangularSelection } as typeof import("@codemirror/view"); codeMirrorPrec = Prec; @@ -948,6 +974,7 @@ onMounted(async () => { codeMirrorCompletionStatus = completionStatus; codeMirrorAcceptCompletion = acceptCompletion; codeMirrorIndentMore = indentMore; + codeMirrorInsertNewlineKeepIndent = insertNewlineKeepIndent; const diagnosticTheme = EditorView.baseTheme({ ".cm-sql-error": { @@ -1015,6 +1042,118 @@ onMounted(async () => { const theme = await loadEditorTheme(ss.theme, editorThemeAppearance()); + const activeLineHighlighter = ViewPlugin.fromClass( + class { + decorations: import("@codemirror/view").DecorationSet; + constructor(view: import("@codemirror/view").EditorView) { + this.decorations = this.getDeco(view); + } + update(update: import("@codemirror/view").ViewUpdate) { + if (update.docChanged || update.selectionSet) this.decorations = this.getDeco(update.view); + } + getDeco(view: import("@codemirror/view").EditorView) { + if (!view.state.selection.main.empty) return Decoration.none; + let lastLineStart = -1; + const deco: any[] = []; + for (const r of view.state.selection.ranges) { + if (!r.empty) continue; + const line = view.lineBlockAt(r.head); + if (line.from > lastLineStart) { + deco.push(Decoration.line({ class: "cm-activeLine" }).range(line.from)); + lastLineStart = line.from; + } + } + return Decoration.set(deco); + } + }, + { decorations: (v) => v.decorations }, + ); + + function getLineElement(view: import("@codemirror/view").EditorView, pos: number): HTMLElement | null { + try { + const domAt = view.domAtPos(pos); + let node: Node | null = domAt.node; + while (node) { + if (node instanceof HTMLElement && node.classList.contains("cm-line")) return node; + node = node.parentElement; + } + } catch { + // domAtPos may throw for positions outside the viewport + } + return null; + } + + const vscodeSelectionLayer = layer({ + above: false, + class: "cm-vscodeSelectionLayer", + markers(view) { + const markers: InstanceType[] = []; + const contentRect = view.contentDOM.getBoundingClientRect(); + const baseRect = view.scrollDOM.getBoundingClientRect(); + const base = { + left: baseRect.left - view.scrollDOM.scrollLeft * view.scaleX, + top: baseRect.top - view.scrollDOM.scrollTop * view.scaleY, + }; + const lineElt = view.contentDOM.querySelector(".cm-line"); + const lineStyle = lineElt && window.getComputedStyle(lineElt); + const contentLeft = + contentRect.left + + (lineStyle ? parseInt(lineStyle.paddingLeft) + Math.min(0, parseInt(lineStyle.textIndent)) : 0); + // single character width for empty-line fallback + const sampleCoords = view.coordsAtPos(view.viewport.from); + const charWidth = sampleCoords ? sampleCoords.right - sampleCoords.left : 8; + for (const r of view.state.selection.ranges) { + if (r.empty) continue; + const fromLine = view.lineBlockAt(r.from); + const toLine = view.lineBlockAt(r.to); + for (let pos = fromLine.from; pos <= toLine.from; ) { + const line = view.lineBlockAt(pos); + const lineEl = getLineElement(view, line.from); + if (!lineEl) break; + const lineRect = lineEl.getBoundingClientRect(); + let left = contentLeft - base.left; + let right: number; + const isFirst = line.from === fromLine.from; + const isLast = line.from === toLine.from; + if (isFirst) { + const c = view.coordsAtPos(r.from); + if (c) left = c.left - base.left; + } + if (isLast) { + const c = view.coordsAtPos(r.to); + right = c ? c.left - base.left : contentLeft - base.left; + } else { + // not the last line: measure text content width from DOM + const range = document.createRange(); + range.selectNodeContents(lineEl); + const textRect = range.getBoundingClientRect(); + const textRight = textRect.right - base.left; + right = textRight > left ? textRight : left + charWidth; + } + const w = right! - left; + if (w > 0) { + markers.push( + new RectangleMarker( + "cm-vscodeSelection", + left, + lineRect.top - base.top, + w, + lineRect.bottom - lineRect.top, + ), + ); + } + if (isLast) break; + pos = line.to + 1; + if (pos > view.state.doc.length) break; + } + } + return markers; + }, + update(update, _dom) { + return update.docChanged || update.selectionSet || update.viewportChanged; + }, + }); + const state = EditorState.create({ doc: props.modelValue, extensions: [ @@ -1026,7 +1165,20 @@ onMounted(async () => { return { dom }; }, }), - basicSetup, + lineNumbers(), + highlightActiveLineGutter(), + highlightSpecialChars(), + history(), + foldGutter(), + drawSelection(), + vscodeSelectionLayer, + dropCursor(), + EditorState.allowMultipleSelections.of(true), + indentOnInput(), + syntaxHighlighting(defaultHighlightStyle, { fallback: true }), + crosshairCursor(), + activeLineHighlighter, + keymap.of([...defaultKeymap, ...searchKeymap, ...historyKeymap, ...foldKeymap, ...completionKeymap]), sql({ dialect }), tooltips({ parent: document.body }), autocompletion({