fix(editor): anchor caret back to cursor after diagnostics relayout
This commit is contained in:
3 files changed
+141
-7
No files matched your search
+60
@@ -0,0 +1,60 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { needsDiagnosticCaretReanchor, type DiagnosticCaretAnchorState } from "@/lib/editor/queryEditorDiagnosticCaretAnchor";
|
||||
|
||||
const textNode = { nodeType: 3, name: "text" } as unknown as Node;
|
||||
const lineNode = { nodeType: 1, name: "line" } as unknown as Node;
|
||||
|
||||
function state(overrides: Partial<DiagnosticCaretAnchorState> = {}): DiagnosticCaretAnchorState {
|
||||
return {
|
||||
hasFocus: true,
|
||||
composing: false,
|
||||
domRangeCount: 1,
|
||||
currentAnchorNode: lineNode,
|
||||
currentAnchorOffset: 2,
|
||||
targetNode: textNode,
|
||||
targetOffset: 3,
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
describe("needsDiagnosticCaretReanchor", () => {
|
||||
it("re-anchors when the browser caret collapsed onto the surrounding line element", () => {
|
||||
// The reported case: `document.getSelection()` reports (line, childCount)
|
||||
// while the caret really sits inside the text node.
|
||||
expect(needsDiagnosticCaretReanchor(state())).toBe(true);
|
||||
});
|
||||
|
||||
it("re-anchors when the browser caret sits on the wrong offset of the same node", () => {
|
||||
expect(needsDiagnosticCaretReanchor(state({ currentAnchorNode: textNode, currentAnchorOffset: 0 }))).toBe(true);
|
||||
});
|
||||
|
||||
it("keeps a caret that already matches the precise DOM position", () => {
|
||||
expect(needsDiagnosticCaretReanchor(state({ currentAnchorNode: textNode, currentAnchorOffset: 3 }))).toBe(false);
|
||||
});
|
||||
|
||||
it("does not touch the caret while an IME composition is running", () => {
|
||||
expect(needsDiagnosticCaretReanchor(state({ composing: true }))).toBe(false);
|
||||
});
|
||||
|
||||
it("does not touch the caret when the editor is not focused", () => {
|
||||
expect(needsDiagnosticCaretReanchor(state({ hasFocus: false }))).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps non-empty selections and multiple cursors untouched", () => {
|
||||
expect(needsDiagnosticCaretReanchor(state({ targetNode: null }))).toBe(false);
|
||||
expect(needsDiagnosticCaretReanchor(state({ domRangeCount: 2 }))).toBe(false);
|
||||
expect(needsDiagnosticCaretReanchor(state({ domRangeCount: 0, currentAnchorNode: null }))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("QueryEditor diagnostic caret re-anchor wiring", () => {
|
||||
const queryEditorSource = readFileSync(new URL("../../../components/editor/QueryEditor.vue", import.meta.url), "utf8");
|
||||
|
||||
it("re-anchors the browser caret after applying diagnostics, outside a transaction", () => {
|
||||
expect(queryEditorSource).toMatch(/function reconfigureDiagnostics\(\)[\s\S]*?reanchorCaretAfterDiagnostics\(currentView\)/);
|
||||
// The re-anchor must not dispatch a selection transaction: that closes the
|
||||
// completion popup the user is looking at (#9480).
|
||||
expect(queryEditorSource).toMatch(/function reanchorCaretAfterDiagnostics\(currentView: EditorViewType\)[\s\S]*?domSelection\.collapse\(target\.node, target\.offset\)/);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Chromium keeps using its own native insertion point for the next typed
|
||||
* character. When a decoration-only update (SQL / Redis syntax diagnostics)
|
||||
* wraps or unwraps the text nodes next to the caret, the browser collapses
|
||||
* that insertion point onto the surrounding line element instead of a text
|
||||
* node. `document.getSelection()` still reports a position that CodeMirror
|
||||
* considers equivalent to the caret — the same document position, only
|
||||
* expressed as (lineElement, childCount) instead of (textNode, offset) — so
|
||||
* CodeMirror leaves it alone, while the browser inserts the next character at
|
||||
* the start of the re-parented text node.
|
||||
*
|
||||
* Reported as #9480: after a short pause inside a Redis command argument
|
||||
* ("HGET us|"), the typed characters land at the start of the argument
|
||||
* ("HGET er:1us").
|
||||
*
|
||||
* Collapsing the DOM caret onto the precise DOM position of the editor
|
||||
* selection restores the insertion point. This has to happen outside a
|
||||
* CodeMirror transaction: a selection transaction would reset the completion
|
||||
* popup the user is currently looking at, and it is not needed because the
|
||||
* editor state already sits on that position.
|
||||
*/
|
||||
export interface DiagnosticCaretAnchorState {
|
||||
/** Whether the editor owns the browser focus. */
|
||||
hasFocus: boolean;
|
||||
/** Whether an IME composition is in progress — the browser owns the caret then. */
|
||||
composing: boolean;
|
||||
/** Number of ranges in the DOM selection of the editor root. */
|
||||
domRangeCount: number;
|
||||
/** Anchor node the browser currently uses for the caret. */
|
||||
currentAnchorNode: Node | null;
|
||||
currentAnchorOffset: number;
|
||||
/** Precise DOM position of the editor caret, or null when there is no single empty caret. */
|
||||
targetNode: Node | null;
|
||||
targetOffset: number;
|
||||
}
|
||||
|
||||
export function needsDiagnosticCaretReanchor(state: DiagnosticCaretAnchorState): boolean {
|
||||
if (!state.hasFocus || state.composing) return false;
|
||||
if (state.domRangeCount !== 1) return false;
|
||||
if (!state.currentAnchorNode || !state.targetNode) return false;
|
||||
return state.currentAnchorNode !== state.targetNode || state.currentAnchorOffset !== state.targetOffset;
|
||||
}
|
||||
Reference in new issue
Block a user