fix(editor): use editor snapshot for SQL execution

This commit is contained in:
t8y2 committed 2026-06-17 15:30:31 +08:00
1 parent 4dbd1c7cc8
commit 6e7035823a
5 files changed
+43 -18

No files matched your search

@@ -8,7 +8,7 @@ import { search as cmSearch } from "@codemirror/search";
import EditorSearchPanel from "./EditorSearchPanel.vue";
import CustomContextMenu, { type ContextMenuItem } from "@/components/ui/CustomContextMenu.vue";
import { copyToClipboard } from "@/lib/clipboard";
import { resolveExecutableSql } from "@/lib/sqlExecutionTarget";
import { resolveExecutableSql, type SqlExecutionSnapshot } from "@/lib/sqlExecutionTarget";
import { formatSqlText, type SqlFormatDialect } from "@/lib/sqlFormatter";
import { useConnectionStore } from "@/stores/connectionStore";
import { useSettingsStore } from "@/stores/settingsStore";
@@ -64,7 +64,7 @@ const emit = defineEmits<{
selectionChange: [value: string];
cursorChange: [pos: number];
formatError: [message: string];
execute: [sql: string];
execute: [snapshot: SqlExecutionSnapshot];
save: [];
clickTable: [tableName: string];
viewTableData: [tableName: string];
@@ -288,7 +288,7 @@ function handleTab(view: EditorViewType): boolean {
}
function executeCurrentSql() {
if (view.value) emit("execute", executableSqlFromView(view.value));
if (view.value) emit("execute", sqlExecutionSnapshotFromView(view.value));
return true;
}
@@ -461,6 +461,14 @@ function executableSqlFromView(currentView: EditorViewType): string {
return resolveExecutableSql(currentView.state.doc.toString(), selectedSqlFromView(currentView));
}
function sqlExecutionSnapshotFromView(currentView: EditorViewType): SqlExecutionSnapshot {
return {
fullSql: currentView.state.doc.toString(),
selectedSql: selectedSqlFromView(currentView),
cursorPos: currentView.state.selection.main.head,
};
}
function identifierRangeAt(sql: string, pos: number): { from: number; to: number; text: string } | null {
const isIdentifierChar = (ch: string | undefined) => !!ch && /[\w$.]/.test(ch);
if (!isIdentifierChar(sql[pos]) && !isIdentifierChar(sql[pos - 1])) return null;