refactor(editor): extract gutter cursor helper with behavioral tests
This commit is contained in:
1 parent
09c47b1851
commit
658a7a405d
3 files changed
+137
-21
No files matched your search
@@ -74,7 +74,7 @@ import { detectAndFormatStructured } from "@/lib/sql/autoFormat";
|
|||||||
import { restoreSqlFromSourcePaste } from "@/lib/sql/sqlSourcePaste";
|
import { restoreSqlFromSourcePaste } from "@/lib/sql/sqlSourcePaste";
|
||||||
import { enabledSqlParameterSyntaxes, resolveSqlVariableSyntaxToggles } from "@/lib/sql/sqlVariableSyntax";
|
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 { mapQueryEditorFormatSelection } from "@/lib/editor/queryEditorFormatSelection";
|
||||||
import { joinQueryEditorLines } from "@/lib/editor/queryEditorJoinLines";
|
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 overlapping that statement is more specific, so preserve it; a
|
||||||
// selection elsewhere in the document must not hijack the click.
|
// selection elsewhere in the document must not hijack the click.
|
||||||
const editorViewportRequestId = executionViewportOwnership.beginRequest();
|
const editorViewportRequestId = executionViewportOwnership.beginRequest();
|
||||||
const selection = currentView.state.selection.main;
|
const { selectionOverlapsStatement } = locateCursorForGutterExecution(currentView, statementRange, settingsStore.editorSettings.locateCursorOnGutterExecute);
|
||||||
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 executionSnapshot = selectionOverlapsStatement ? sqlExecutionSnapshotFromView(currentView) : sqlExecutionSnapshotForRange(currentView, statementRange);
|
const executionSnapshot = selectionOverlapsStatement ? sqlExecutionSnapshotFromView(currentView) : sqlExecutionSnapshotForRange(currentView, statementRange);
|
||||||
emitExecutionRequest({ ...executionSnapshot, editorViewportRequestId });
|
emitExecutionRequest({ ...executionSnapshot, editorViewportRequestId });
|
||||||
if (settingsStore.editorSettings.locateCursorOnGutterExecute) {
|
|
||||||
currentView.focus();
|
|
||||||
}
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,15 @@
|
|||||||
import { readFileSync } from "node:fs";
|
// @vitest-environment happy-dom
|
||||||
import { describe, expect, it } from "vitest";
|
|
||||||
import { createQueryEditorExecutionViewportOwnership, isQueryEditorPositionVisible } from "../../editor/queryEditorExecutionViewport";
|
|
||||||
|
|
||||||
const queryEditorSource = ["QueryEditor.vue", "useQueryEditorExecution.ts"].map((file) => readFileSync(new URL(`../../../components/editor/${file}`, import.meta.url), "utf8")).join("\n");
|
import { readFileSync } from "node:fs";
|
||||||
|
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 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", () => {
|
describe("QueryEditor execution routing", () => {
|
||||||
it("routes the execution shortcut through the shared execution-mode contract while bypassing the picker", () => {
|
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", () => {
|
it("snapshots pre-execution cursor visibility when execution viewport tracking starts", () => {
|
||||||
expect(queryEditorSource).toContain("beginExecution(cursorVisible)");
|
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", () => {
|
it("positions cursor at statement start and focuses editor on gutter execution when enabled", () => {
|
||||||
expect(queryEditorSource).toContain("if (settingsStore.editorSettings.locateCursorOnGutterExecute && !selectionOverlapsStatement)");
|
const doc = "select * from users;\nselect * from orders;";
|
||||||
expect(queryEditorSource).toContain("selection: { anchor: statementRange.from, head: statementRange.from }");
|
const statementRange = { from: 21, to: 42 };
|
||||||
expect(queryEditorSource).toContain("if (settingsStore.editorSettings.locateCursorOnGutterExecute) {\n currentView.focus();\n }");
|
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();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
import type { EditorView } from "@codemirror/view";
|
||||||
|
|
||||||
export interface QueryEditorViewportRange {
|
export interface QueryEditorViewportRange {
|
||||||
from: number;
|
from: number;
|
||||||
to: 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 };
|
||||||
|
}
|
||||||
Reference in new issue
Block a user