fix(editor): preserve user-adjusted SQL editor position during execution

This commit is contained in:
zipg authored and GitHub committed 2026-08-24 13:30:50 +08:00
1 parent 4d7d4ab74e
commit 8cc924270c
4 files changed
+86 -17

No files matched your search

@@ -226,7 +226,7 @@ let viewportEmitFrame: number | null = null;
let viewportRestoreFrame: number | null = null; let viewportRestoreFrame: number | null = null;
let latestViewport: { scrollTop: number; scrollLeft: number } | undefined = props.initialViewport; let latestViewport: { scrollTop: number; scrollLeft: number } | undefined = props.initialViewport;
let lastEmittedViewport: { scrollTop: number; scrollLeft: number } | undefined = props.initialViewport; let lastEmittedViewport: { scrollTop: number; scrollLeft: number } | undefined = props.initialViewport;
const gutterExecutionViewport = createQueryEditorExecutionViewportOwnership(); const executionViewportOwnership = createQueryEditorExecutionViewportOwnership();
let latestSelection: { anchor: number; head: number } | undefined = props.initialSelection; let latestSelection: { anchor: number; head: number } | undefined = props.initialSelection;
const connectionStore = useConnectionStore(); const connectionStore = useConnectionStore();
const settingsStore = useSettingsStore(); const settingsStore = useSettingsStore();
@@ -830,7 +830,7 @@ interface RequestExecuteOptions {
function emitExecutionRequest(source: SqlExecutionOverride, openInNewResultTab = false) { function emitExecutionRequest(source: SqlExecutionOverride, openInNewResultTab = false) {
if (typeof source === "string" || source.editorViewportRequestId === undefined) { if (typeof source === "string" || source.editorViewportRequestId === undefined) {
gutterExecutionViewport.cancelPendingRequest(); executionViewportOwnership.cancelPendingRequest();
} }
if (openInNewResultTab) { if (openInNewResultTab) {
emit("executeInNewResultTab", source); emit("executeInNewResultTab", source);
@@ -840,7 +840,7 @@ function emitExecutionRequest(source: SqlExecutionOverride, openInNewResultTab =
} }
function requestExecute(options: RequestExecuteOptions = {}) { function requestExecute(options: RequestExecuteOptions = {}) {
gutterExecutionViewport.cancelPendingRequest(); executionViewportOwnership.cancelPendingRequest();
const currentView = view.value; const currentView = view.value;
if (!currentView) return false; if (!currentView) return false;
currentView.focus(); currentView.focus();
@@ -1689,7 +1689,7 @@ function executeSqlStatementFromGutter(currentView: EditorViewType, line: { from
event.stopPropagation(); event.stopPropagation();
// Gutter play is always scoped to the statement/command for that line, even // Gutter play is always scoped to the statement/command for that line, even
// when the main editor execute action would run the full document. // when the main editor execute action would run the full document.
const editorViewportRequestId = gutterExecutionViewport.beginRequest(); const editorViewportRequestId = executionViewportOwnership.beginRequest();
emitExecutionRequest({ ...sqlExecutionSnapshotForRange(currentView, statementRange), editorViewportRequestId }); emitExecutionRequest({ ...sqlExecutionSnapshotForRange(currentView, statementRange), editorViewportRequestId });
// 不主动聚焦编辑器,否则 CodeMirror 会把屏幕滚回之前的光标位置。 // 不主动聚焦编辑器,否则 CodeMirror 会把屏幕滚回之前的光标位置。
// currentView.focus(); // currentView.focus();
@@ -5847,7 +5847,7 @@ function pauseQueryEditorBackgroundWork() {
flushEditorSelection(); flushEditorSelection();
clearTableNavigationHover(); clearTableNavigationHover();
clearPendingCompletionTab(); clearPendingCompletionTab();
gutterExecutionViewport.reset(); executionViewportOwnership.reset();
editorIsActive = false; editorIsActive = false;
clearScheduledSemanticDiagnostics(); clearScheduledSemanticDiagnostics();
completionEpoch++; completionEpoch++;
@@ -6006,7 +6006,7 @@ function openReplace(): boolean {
} }
function scrollCursorIntoView() { function scrollCursorIntoView() {
const preserveViewport = gutterExecutionViewport.consumeAcceptedRequest(); const preserveViewport = executionViewportOwnership.consumeCompletionPreservation();
if (!view.value || !editorViewModule || !editorIsActive || preserveViewport) return; if (!view.value || !editorViewModule || !editorIsActive || preserveViewport) return;
const pos = view.value.state.selection.main.head; const pos = view.value.state.selection.main.head;
// Use "center" rather than "nearest": by the time this runs, the results pane has already // Use "center" rather than "nearest": by the time this runs, the results pane has already
@@ -6020,19 +6020,28 @@ function scrollCursorIntoView() {
}); });
} }
function beginExecutionViewportTracking() {
executionViewportOwnership.beginExecution();
}
function recordExecutionViewportInteraction() {
executionViewportOwnership.recordUserInteraction();
}
function dismissHoverTooltip() { function dismissHoverTooltip() {
if (!view.value || !hoverCloseEffect) return; if (!view.value || !hoverCloseEffect) return;
view.value.dispatch({ effects: hoverCloseEffect }); view.value.dispatch({ effects: hoverCloseEffect });
} }
function acceptGutterExecutionViewport(requestId: number) { function acceptGutterExecutionViewport(requestId: number) {
return gutterExecutionViewport.acceptRequest(requestId); return executionViewportOwnership.acceptRequest(requestId);
} }
defineExpose({ defineExpose({
openSearch, openSearch,
openReplace, openReplace,
scrollCursorIntoView, scrollCursorIntoView,
beginExecutionViewportTracking,
acceptGutterExecutionViewport, acceptGutterExecutionViewport,
requestExecute, requestExecute,
requestExecuteInNewResultTab, requestExecuteInNewResultTab,
@@ -6044,7 +6053,7 @@ defineExpose({
</script> </script>
<template> <template>
<div class="h-full w-full overflow-hidden relative" @gesturestart="onEditorGestureStart" @gesturechange="onEditorGestureChange" @gestureend="onEditorGestureEnd"> <div class="h-full w-full overflow-hidden relative" @wheel="recordExecutionViewportInteraction" @pointerdown="recordExecutionViewportInteraction" @gesturestart="onEditorGestureStart" @gesturechange="onEditorGestureChange" @gestureend="onEditorGestureEnd">
<CustomContextMenu :items="currentContextMenuItems" @close="contextMenuOpen = false" v-slot="{ onContextMenu }"> <CustomContextMenu :items="currentContextMenuItems" @close="contextMenuOpen = false" v-slot="{ onContextMenu }">
<div <div
ref="editorRef" ref="editorRef"
@@ -751,6 +751,9 @@ watch(
watch( watch(
() => props.activeTab.isExecuting, () => props.activeTab.isExecuting,
(isExecuting, wasExecuting) => { (isExecuting, wasExecuting) => {
if (isExecuting && !wasExecuting) {
queryEditorRef.value?.beginExecutionViewportTracking();
}
if (!isExecuting && wasExecuting) { if (!isExecuting && wasExecuting) {
nextTick(() => { nextTick(() => {
requestAnimationFrame(() => { requestAnimationFrame(() => {
@@ -50,7 +50,7 @@ describe("QueryEditor execution routing", () => {
}); });
it("preserves the source range when executing from the statement gutter", () => { it("preserves the source range when executing from the statement gutter", () => {
expect(queryEditorSource).toContain("const editorViewportRequestId = gutterExecutionViewport.beginRequest()"); expect(queryEditorSource).toContain("const editorViewportRequestId = executionViewportOwnership.beginRequest()");
expect(queryEditorSource).toContain("emitExecutionRequest({ ...sqlExecutionSnapshotForRange(currentView, statementRange), editorViewportRequestId })"); expect(queryEditorSource).toContain("emitExecutionRequest({ ...sqlExecutionSnapshotForRange(currentView, statementRange), editorViewportRequestId })");
expect(queryEditorSource).not.toContain('emit("execute", statementRange.sql)'); expect(queryEditorSource).not.toContain('emit("execute", statementRange.sql)');
}); });
@@ -62,6 +62,13 @@ describe("QueryEditor execution routing", () => {
expect(queryStoreSource.indexOf("tab.isExecuting = true")).toBeLessThan(queryStoreSource.indexOf("options?.onExecutionStarted?.()")); expect(queryStoreSource.indexOf("tab.isExecuting = true")).toBeLessThan(queryStoreSource.indexOf("options?.onExecutionStarted?.()"));
}); });
it("tracks editor interaction while a query is executing", () => {
expect(contentAreaSource).toContain("queryEditorRef.value?.beginExecutionViewportTracking()");
expect(queryEditorSource).toContain('@wheel="recordExecutionViewportInteraction"');
expect(queryEditorSource).toContain('@pointerdown="recordExecutionViewportInteraction"');
expect(queryEditorSource).toContain("executionViewportOwnership.recordUserInteraction()");
});
it("lets the shortcut skip the picker without affecting other execution entry points", () => { it("lets the shortcut skip the picker without affecting other execution entry points", () => {
// The picker guard must also honor the shortcut's bypass flag, otherwise Ctrl+Enter would keep popping the dialog. // The picker guard must also honor the shortcut's bypass flag, otherwise Ctrl+Enter would keep popping the dialog.
expect(queryEditorSource).toContain("if (options.bypassPicker || !settingsStore.editorSettings.showExecutionTargetPicker"); expect(queryEditorSource).toContain("if (options.bypassPicker || !settingsStore.editorSettings.showExecutionTargetPicker");
@@ -75,7 +82,33 @@ describe("QueryEditor execution routing", () => {
}); });
}); });
describe("QueryEditor gutter execution viewport ownership", () => { describe("QueryEditor execution viewport ownership", () => {
it("keeps automatic cursor centering when the user does not interact during execution", () => {
const ownership = createQueryEditorExecutionViewportOwnership();
ownership.beginExecution();
expect(ownership.consumeCompletionPreservation()).toBe(false);
});
it("preserves the viewport once after user interaction during execution", () => {
const ownership = createQueryEditorExecutionViewportOwnership();
ownership.beginExecution();
ownership.recordUserInteraction();
expect(ownership.consumeCompletionPreservation()).toBe(true);
expect(ownership.consumeCompletionPreservation()).toBe(false);
});
it("ignores editor interaction outside an active execution", () => {
const ownership = createQueryEditorExecutionViewportOwnership();
ownership.recordUserInteraction();
expect(ownership.consumeCompletionPreservation()).toBe(false);
});
it("does not let a cancelled or early-returned gutter request affect the next ordinary execution", () => { it("does not let a cancelled or early-returned gutter request affect the next ordinary execution", () => {
const ownership = createQueryEditorExecutionViewportOwnership(); const ownership = createQueryEditorExecutionViewportOwnership();
const cancelledRequestId = ownership.beginRequest(); const cancelledRequestId = ownership.beginRequest();
@@ -83,7 +116,7 @@ describe("QueryEditor gutter execution viewport ownership", () => {
ownership.cancelPendingRequest(); ownership.cancelPendingRequest();
expect(ownership.acceptRequest(cancelledRequestId)).toBe(false); expect(ownership.acceptRequest(cancelledRequestId)).toBe(false);
expect(ownership.consumeAcceptedRequest()).toBe(false); expect(ownership.consumeCompletionPreservation()).toBe(false);
}); });
it("preserves the viewport once for the matching accepted execution", () => { it("preserves the viewport once for the matching accepted execution", () => {
@@ -91,8 +124,9 @@ describe("QueryEditor gutter execution viewport ownership", () => {
const requestId = ownership.beginRequest(); const requestId = ownership.beginRequest();
expect(ownership.acceptRequest(requestId)).toBe(true); expect(ownership.acceptRequest(requestId)).toBe(true);
expect(ownership.consumeAcceptedRequest()).toBe(true); ownership.beginExecution();
expect(ownership.consumeAcceptedRequest()).toBe(false); expect(ownership.consumeCompletionPreservation()).toBe(true);
expect(ownership.consumeCompletionPreservation()).toBe(false);
}); });
it("clears pending and accepted ownership when the editor becomes inactive", () => { it("clears pending and accepted ownership when the editor becomes inactive", () => {
@@ -106,7 +140,17 @@ describe("QueryEditor gutter execution viewport ownership", () => {
expect(ownership.acceptRequest(acceptedRequestId)).toBe(true); expect(ownership.acceptRequest(acceptedRequestId)).toBe(true);
ownership.reset(); ownership.reset();
expect(ownership.consumeAcceptedRequest()).toBe(false); expect(ownership.consumeCompletionPreservation()).toBe(false);
});
it("clears execution interaction when the editor becomes inactive", () => {
const ownership = createQueryEditorExecutionViewportOwnership();
ownership.beginExecution();
ownership.recordUserInteraction();
ownership.reset();
expect(ownership.consumeCompletionPreservation()).toBe(false);
}); });
}); });
@@ -2,6 +2,8 @@ export function createQueryEditorExecutionViewportOwnership() {
let nextRequestId = 0; let nextRequestId = 0;
let pendingRequestId: number | undefined; let pendingRequestId: number | undefined;
let acceptedRequestId: number | undefined; let acceptedRequestId: number | undefined;
let executionActive = false;
let userInteractedDuringExecution = false;
return { return {
beginRequest(): number { beginRequest(): number {
@@ -18,14 +20,25 @@ export function createQueryEditorExecutionViewportOwnership() {
acceptedRequestId = requestId; acceptedRequestId = requestId;
return true; return true;
}, },
consumeAcceptedRequest(): boolean { beginExecution() {
if (acceptedRequestId === undefined) return false; executionActive = true;
userInteractedDuringExecution = false;
},
recordUserInteraction() {
if (executionActive) userInteractedDuringExecution = true;
},
consumeCompletionPreservation(): boolean {
const preserveViewport = acceptedRequestId !== undefined || userInteractedDuringExecution;
acceptedRequestId = undefined; acceptedRequestId = undefined;
return true; executionActive = false;
userInteractedDuringExecution = false;
return preserveViewport;
}, },
reset() { reset() {
pendingRequestId = undefined; pendingRequestId = undefined;
acceptedRequestId = undefined; acceptedRequestId = undefined;
executionActive = false;
userInteractedDuringExecution = false;
}, },
}; };
} }