25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
refactor(editor): extract gutter cursor helper with behavioral tests
lxk955658a7a405d70
diff --git a/apps/desktop/src/components/editor/QueryEditor.vue b/apps/desktop/src/components/editor/QueryEditor.vueindex baa06bbea0..852d12b494 100644--- a/apps/desktop/src/components/editor/QueryEditor.vue+++ b/apps/desktop/src/components/editor/QueryEditor.vue@@ -74,7 +74,7 @@ import { detectAndFormatStructured } from "@/lib/sql/autoFormat";import { restoreSqlFromSourcePaste } from "@/lib/sql/sqlSourcePaste";import { enabledSqlParameterSyntaxes, resolveSqlVariableSyntaxToggles } from "@/lib/sql/sqlVariableSyntax";-import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisible } from "@/lib/editor/queryEditorExecutionViewport";+import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisible, locateCursorForGutterExecution } from "@/lib/editor/queryEditorExecutionViewport";import { mapQueryEditorFormatSelection } from "@/lib/editor/queryEditorFormatSelection";import { joinQueryEditorLines } from "@/lib/editor/queryEditorJoinLines";@@ -1177,21 +1177,9 @@ function executeSqlStatementFromGutter(currentView: EditorViewType, line: { from// selection overlapping that statement is more specific, so preserve it; a// selection elsewhere in the document must not hijack the click.const editorViewportRequestId = executionViewportOwnership.beginRequest();- const selection = currentView.state.selection.main;- const hasSelectedSql = !selection.empty && currentView.state.sliceDoc(selection.from, selection.to).trim().length > 0;- const selectionOverlapsStatement = hasSelectedSql && selection.from < statementRange.to && statementRange.from < selection.to;- if (settingsStore.editorSettings.locateCursorOnGutterExecute && !selectionOverlapsStatement) {- currentView.dispatch({- selection: { anchor: statementRange.from, head: statementRange.from },- scrollIntoView: false,- userEvent: "select.pointer",- });- }+ const { selectionOverlapsStatement } = locateCursorForGutterExecution(currentView, statementRange, settingsStore.editorSettings.locateCursorOnGutterExecute);const executionSnapshot = selectionOverlapsStatement ? sqlExecutionSnapshotFromView(currentView) : sqlExecutionSnapshotForRange(currentView, statementRange);emitExecutionRequest({ ...executionSnapshot, editorViewportRequestId });- if (settingsStore.editorSettings.locateCursorOnGutterExecute) {- currentView.focus();- }return true;}diff --git a/apps/desktop/src/lib/__tests__/editor/queryEditorExecution.spec.ts b/apps/desktop/src/lib/__tests__/editor/queryEditorExecution.spec.tsindex 5ab3191fac..243a9a8c71 100644--- a/apps/desktop/src/lib/__tests__/editor/queryEditorExecution.spec.ts+++ b/apps/desktop/src/lib/__tests__/editor/queryEditorExecution.spec.ts@@ -1,8 +1,15 @@+// @vitest-environment happy-dom+import { readFileSync } from "node:fs";-import { describe, expect, it } from "vitest";-import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisible } from "../../editor/queryEditorExecutionViewport";+import path from "node:path";+import { fileURLToPath } from "node:url";+import { EditorSelection, EditorState } from "@codemirror/state";+import { EditorView } from "@codemirror/view";+import { describe, expect, it, vi } from "vitest";+import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisible, locateCursorForGutterExecution } from "../../editor/queryEditorExecutionViewport";-const queryEditorSource = ["QueryEditor.vue", "useQueryEditorExecution.ts"].map((file) => readFileSync(new URL(`../../../components/editor/${file}`, import.meta.url), "utf8")).join("\n");+const specDir = path.dirname(fileURLToPath(import.meta.url));+const queryEditorSource = ["QueryEditor.vue", "useQueryEditorExecution.ts"].map((file) => readFileSync(path.resolve(specDir, "../../../components/editor", file), "utf8")).join("\n");describe("QueryEditor execution routing", () => {it("routes the execution shortcut through the shared execution-mode contract while bypassing the picker", () => {@@ -13,11 +20,100 @@ describe("QueryEditor execution routing", () => {it("snapshots pre-execution cursor visibility when execution viewport tracking starts", () => {expect(queryEditorSource).toContain("beginExecution(cursorVisible)");});+});++describe("QueryEditor gutter execution cursor positioning", () => {+ function createEditor(doc: string, selection?: { anchor: number; head?: number }): EditorView {+ return new EditorView({+ parent: document.createElement("div"),+ state: EditorState.create({+ doc,+ selection: selection ? EditorSelection.single(selection.anchor, selection.head ?? selection.anchor) : undefined,+ }),+ });+ }it("positions cursor at statement start and focuses editor on gutter execution when enabled", () => {- expect(queryEditorSource).toContain("if (settingsStore.editorSettings.locateCursorOnGutterExecute && !selectionOverlapsStatement)");- expect(queryEditorSource).toContain("selection: { anchor: statementRange.from, head: statementRange.from }");- expect(queryEditorSource).toContain("if (settingsStore.editorSettings.locateCursorOnGutterExecute) {\n currentView.focus();\n }");+ const doc = "select * from users;\nselect * from orders;";+ const statementRange = { from: 21, to: 42 };+ const view = createEditor(doc, { anchor: 0 });+ const focusSpy = vi.spyOn(view, "focus");++ const result = locateCursorForGutterExecution(view, statementRange, true);++ expect(result.selectionOverlapsStatement).toBe(false);+ expect(result.cursorRelocated).toBe(true);+ expect(view.state.selection.main.from).toBe(statementRange.from);+ expect(view.state.selection.main.to).toBe(statementRange.from);+ expect(view.state.selection.main.empty).toBe(true);+ expect(focusSpy).toHaveBeenCalledOnce();+ view.destroy();+ });++ it("preserves active overlapping selection within statement while focusing editor", () => {+ const doc = "select * from users;\nselect * from orders;";+ const statementRange = { from: 21, to: 42 };+ const view = createEditor(doc, { anchor: 35, head: 41 });+ const focusSpy = vi.spyOn(view, "focus");++ const result = locateCursorForGutterExecution(view, statementRange, true);++ expect(result.selectionOverlapsStatement).toBe(true);+ expect(result.cursorRelocated).toBe(false);+ expect(view.state.selection.main.anchor).toBe(35);+ expect(view.state.selection.main.head).toBe(41);+ expect(focusSpy).toHaveBeenCalledOnce();+ view.destroy();+ });++ it("relocates cursor when active selection is elsewhere in the document and does not overlap statement", () => {+ const doc = "select * from users;\nselect * from orders;";+ const statementRange = { from: 21, to: 42 };+ const view = createEditor(doc, { anchor: 14, head: 19 });+ const focusSpy = vi.spyOn(view, "focus");++ const result = locateCursorForGutterExecution(view, statementRange, true);++ expect(result.selectionOverlapsStatement).toBe(false);+ expect(result.cursorRelocated).toBe(true);+ expect(view.state.selection.main.from).toBe(statementRange.from);+ expect(view.state.selection.main.to).toBe(statementRange.from);+ expect(view.state.selection.main.empty).toBe(true);+ expect(focusSpy).toHaveBeenCalledOnce();+ view.destroy();+ });++ it("does not alter cursor position or focus editor when disabled", () => {+ const doc = "select * from users;\nselect * from orders;";+ const statementRange = { from: 21, to: 42 };+ const view = createEditor(doc, { anchor: 5 });+ const focusSpy = vi.spyOn(view, "focus");++ const result = locateCursorForGutterExecution(view, statementRange, false);++ expect(result.selectionOverlapsStatement).toBe(false);+ expect(result.cursorRelocated).toBe(false);+ expect(view.state.selection.main.from).toBe(5);+ expect(view.state.selection.main.to).toBe(5);+ expect(focusSpy).not.toHaveBeenCalled();+ view.destroy();+ });++ it("treats whitespace-only selection within statement as non-overlapping and relocates cursor", () => {+ const doc = "select * from users;\nselect * from orders;";+ const statementRange = { from: 21, to: 44 };+ const view = createEditor(doc, { anchor: 27, head: 30 });+ const focusSpy = vi.spyOn(view, "focus");++ const result = locateCursorForGutterExecution(view, statementRange, true);++ expect(result.selectionOverlapsStatement).toBe(false);+ expect(result.cursorRelocated).toBe(true);+ expect(view.state.selection.main.from).toBe(statementRange.from);+ expect(view.state.selection.main.to).toBe(statementRange.from);+ expect(view.state.selection.main.empty).toBe(true);+ expect(focusSpy).toHaveBeenCalledOnce();+ view.destroy();});});diff --git a/apps/desktop/src/lib/editor/queryEditorExecutionViewport.ts b/apps/desktop/src/lib/editor/queryEditorExecutionViewport.tsindex da061cdda8..423d8ac44e 100644--- a/apps/desktop/src/lib/editor/queryEditorExecutionViewport.ts+++ b/apps/desktop/src/lib/editor/queryEditorExecutionViewport.ts@@ -1,3 +1,5 @@+import type { EditorView } from "@codemirror/view";+export interface QueryEditorViewportRange {from: number;to: number;@@ -63,3 +65,33 @@ export function createQueryEditorExecutionViewportOwnership() {},};}++export interface GutterExecutionCursorResult {+ selectionOverlapsStatement: boolean;+ cursorRelocated: boolean;+}++export function locateCursorForGutterExecution(view: EditorView, statementRange: { from: number; to: number }, locateCursorOnGutterExecute: boolean): GutterExecutionCursorResult {+ const selection = view.state.selection.main;+ const hasSelectedSql = !selection.empty && view.state.sliceDoc(selection.from, selection.to).trim().length > 0;+ const selectionOverlapsStatement = hasSelectedSql && selection.from < statementRange.to && statementRange.from < selection.to;++ // When an overlapping selection already exists within the statement, preserve the+ // explicit selection rather than collapsing the cursor to statement start (the editor+ // is still focused below).+ let cursorRelocated = false;+ if (locateCursorOnGutterExecute && !selectionOverlapsStatement) {+ view.dispatch({+ selection: { anchor: statementRange.from, head: statementRange.from },+ scrollIntoView: false,+ userEvent: "select.pointer",+ });+ cursorRelocated = true;+ }++ if (locateCursorOnGutterExecute) {+ view.focus();+ }++ return { selectionOverlapsStatement, cursorRelocated };+}