дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
fix(editor): keep column 0 on paste after clearing auto-indent in YAML (#7415)
Fixes #7388. pasteReindentFilter fell back to the previous line's indent whenever the caret was at column 0 and the language had no indent opinion, so a line whose auto-indent the user had just deleted was treated like a clicked blank line (#4474). A state field now records a blank line whose indentation the user removed (kept across Enter from it, cleared when the caret leaves or the line gets text), and the paste keeps column 0 there. In YAML, an explicit indent is also kept when the language computes 0 because a root key follows. Co-authored-by: OnurrUgur <[email protected]>
Цей коміт міститься в:
1 джерело
befb4792ce
коміт
44ccaaef26
2 files changed
+190
-53
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { Compartment, EditorSelection, EditorState, type Extension } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { deleteCharBackward, indentLess, insertNewlineAndIndent } from "@codemirror/commands";
|
||||
import { codeFolding, foldEffect, foldService, foldedRanges, indentService, indentUnit } from "@codemirror/language";
|
||||
import { languages } from "@codemirror/language-data";
|
||||
import {
|
||||
@@ -319,6 +320,61 @@ describe("paste reformatting", () => {
|
||||
paste(v, "a: 1\n b: 2");
|
||||
expect(v.state.doc.toString()).toBe("outer:\n key: 1\n\n a: 1\n b: 2\ntrailing: 2");
|
||||
});
|
||||
|
||||
it.each(["indentEachLine", "indentBlock"] as const)(
|
||||
"keeps column 0 in YAML (%s) when the user deletes auto-inserted indentation before pasting",
|
||||
(mode) => {
|
||||
const block = "test:\n test2:\n test3: xxx";
|
||||
const v = make(block + "|", [yaml, pasteReindentFilter(mode)]);
|
||||
insertNewlineAndIndent(v);
|
||||
while (v.state.doc.lineAt(v.state.selection.main.head).text.length > 0) deleteCharBackward(v);
|
||||
paste(v, block);
|
||||
expect(v.state.doc.toString()).toBe(`${block}\n${block}`);
|
||||
},
|
||||
);
|
||||
|
||||
it("keeps column 0 in YAML across an extra Enter after clearing indentation", () => {
|
||||
const block = "test:\n test2:\n test3: xxx";
|
||||
const v = make(block + "|", [yaml, pasteReindentFilter("indentEachLine")]);
|
||||
insertNewlineAndIndent(v);
|
||||
indentLess(v);
|
||||
indentLess(v);
|
||||
insertNewlineAndIndent(v);
|
||||
paste(v, block);
|
||||
expect(v.state.doc.toString()).toBe(`${block}\n\n${block}`);
|
||||
});
|
||||
|
||||
it("clears the explicit column-0 override once the caret moves away and returns", () => {
|
||||
const block = "test:\n test2:\n test3: xxx";
|
||||
const v = make(block + "|", [yaml, pasteReindentFilter("indentEachLine")]);
|
||||
insertNewlineAndIndent(v);
|
||||
while (v.state.doc.lineAt(v.state.selection.main.head).text.length > 0) deleteCharBackward(v);
|
||||
const blankPos = v.state.selection.main.head;
|
||||
v.dispatch({ selection: { anchor: 0 } });
|
||||
v.dispatch({ selection: { anchor: blankPos } });
|
||||
paste(v, "sibling: yyy");
|
||||
expect(v.state.doc.toString()).toBe(`${block}\n sibling: yyy`);
|
||||
});
|
||||
|
||||
it("keeps an explicit YAML indent when a root key follows below", () => {
|
||||
const doc = "test:\n test2:\n test3: xxx\n |\ntrailing: 1";
|
||||
const v = make(doc, [yaml, pasteReindentFilter("indentEachLine")]);
|
||||
paste(v, "sibling:\n leaf: 2");
|
||||
expect(v.state.doc.toString()).toBe("test:\n test2:\n test3: xxx\n sibling:\n leaf: 2\ntrailing: 1");
|
||||
});
|
||||
|
||||
it("still indents onto the kept auto-indent in YAML for multi-line and single-line pastes", () => {
|
||||
const block = "test:\n test2:\n test3: xxx";
|
||||
const multi = make(block + "|", [yaml, pasteReindentFilter("indentEachLine")]);
|
||||
insertNewlineAndIndent(multi);
|
||||
paste(multi, block);
|
||||
expect(multi.state.doc.toString()).toBe(`${block}\n test:\n test2:\n test3: xxx`);
|
||||
|
||||
const single = make(block + "|", [yaml, pasteReindentFilter("indentEachLine")]);
|
||||
insertNewlineAndIndent(single);
|
||||
paste(single, "test4: yyy");
|
||||
expect(single.state.doc.toString()).toBe(`${block}\n test4: yyy`);
|
||||
});
|
||||
});
|
||||
|
||||
describe("closing quotes", () => {
|
||||
|
||||
@@ -26,7 +26,15 @@ import {
|
||||
type ViewUpdate,
|
||||
} from "@codemirror/view";
|
||||
import { highlightTree } from "@lezer/highlight";
|
||||
import { getIndentUnit, getIndentation, indentRange, indentString, indentUnit, syntaxTree } from "@codemirror/language";
|
||||
import {
|
||||
getIndentUnit,
|
||||
getIndentation,
|
||||
indentRange,
|
||||
indentString,
|
||||
indentUnit,
|
||||
language,
|
||||
syntaxTree,
|
||||
} from "@codemirror/language";
|
||||
import {
|
||||
cursorLineStart,
|
||||
deleteCharBackwardStrict,
|
||||
@@ -255,6 +263,71 @@ function previousLineIndent(state: EditorState, line: number): number | null {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1-based line number whose column-0 position the user explicitly chose by removing
|
||||
* leading indentation (or by pressing Enter from such a line). Cleared as soon as the
|
||||
* caret leaves the line or the line stops being empty, so clicking onto a pre-existing
|
||||
* blank line still inherits the previous line's indent (#4474) while clearing auto-indent
|
||||
* before pasting keeps column 0 (#7388).
|
||||
*/
|
||||
const explicitZeroIndentLine = StateField.define<number | null>({
|
||||
create: () => null,
|
||||
update(value, tr) {
|
||||
const sel = tr.state.selection;
|
||||
if (sel.ranges.length !== 1 || !sel.main.empty) return null;
|
||||
const line = tr.state.doc.lineAt(sel.main.head);
|
||||
if (!tr.docChanged) {
|
||||
return value !== null && line.number === value && line.text.length === 0 ? value : null;
|
||||
}
|
||||
if (line.text.length !== 0) return null;
|
||||
|
||||
if (tr.isUserEvent("delete")) {
|
||||
let clearedIndent = false;
|
||||
let valid = true;
|
||||
tr.changes.iterChanges((fromA, toA, fromB, toB, inserted) => {
|
||||
if (inserted.length > 0) {
|
||||
valid = false;
|
||||
return;
|
||||
}
|
||||
const oldLine = tr.startState.doc.lineAt(fromA);
|
||||
if (
|
||||
toA <= oldLine.to &&
|
||||
oldLine.text.length > 0 &&
|
||||
/^[ \t]+$/.test(oldLine.text) &&
|
||||
fromB === line.from &&
|
||||
toB === line.from
|
||||
) {
|
||||
clearedIndent = true;
|
||||
} else {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
return valid && clearedIndent ? line.number : null;
|
||||
}
|
||||
|
||||
if (value !== null && tr.isUserEvent("input") && !tr.isUserEvent("input.paste")) {
|
||||
const oldSel = tr.startState.selection;
|
||||
if (oldSel.ranges.length === 1 && oldSel.main.empty) {
|
||||
const oldLine = tr.startState.doc.lineAt(oldSel.main.head);
|
||||
if (oldLine.number === value && oldLine.text.length === 0) {
|
||||
let onlyNewline = false;
|
||||
let valid = true;
|
||||
tr.changes.iterChanges((fromA, toA, _fromB, _toB, inserted) => {
|
||||
if (fromA === oldLine.from && toA === oldLine.to && inserted.toString() === "\n") {
|
||||
onlyNewline = true;
|
||||
} else {
|
||||
valid = false;
|
||||
}
|
||||
});
|
||||
if (valid && onlyNewline) return line.number;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
/** Reformat on paste › Indent Block: re-indents a pasted block to the caret's indentation. */
|
||||
export function indentPastedText(
|
||||
text: string,
|
||||
@@ -291,59 +364,67 @@ export function indentPastedText(
|
||||
* language's indentation, where the language can compute one. A paste
|
||||
* after nothing but whitespace starts at the line's proper indentation.
|
||||
*/
|
||||
export function pasteReindentFilter(mode: "indentBlock" | "indentEachLine") {
|
||||
return EditorState.transactionFilter.of((tr: Transaction) => {
|
||||
if (!tr.isUserEvent("input.paste") || !tr.docChanged) return tr;
|
||||
const start = tr.startState;
|
||||
const changes: Array<{ from: number; to: number; text: string }> = [];
|
||||
tr.changes.iterChanges((from, to, _fb, _tb, inserted) => changes.push({ from, to, text: inserted.toString() }));
|
||||
if (changes.length !== 1) return tr;
|
||||
let { from } = changes[0]!;
|
||||
const { to, text } = changes[0]!;
|
||||
if (!text.trim()) return tr;
|
||||
const line = start.doc.lineAt(from);
|
||||
const before = start.doc.sliceString(line.from, from);
|
||||
const atIndent = /^\s*$/.test(before);
|
||||
const tabSize = start.tabSize;
|
||||
const unitCols = getIndentUnit(start) || tabSize;
|
||||
const useTabs = start.facet(indentUnit)[0] === "\t";
|
||||
let targetCol = leadingColumns(atIndent ? before : line.text, tabSize);
|
||||
let prefix = "";
|
||||
if (atIndent) {
|
||||
let proper = getIndentation(start, line.from);
|
||||
if (!targetCol && !proper) {
|
||||
const above = previousLineIndent(start, line.number);
|
||||
if (above) proper = above;
|
||||
export function pasteReindentFilter(mode: "indentBlock" | "indentEachLine"): Extension {
|
||||
return [
|
||||
explicitZeroIndentLine,
|
||||
EditorState.transactionFilter.of((tr: Transaction) => {
|
||||
if (!tr.isUserEvent("input.paste") || !tr.docChanged) return tr;
|
||||
const start = tr.startState;
|
||||
const changes: Array<{ from: number; to: number; text: string }> = [];
|
||||
tr.changes.iterChanges((from, to, _fb, _tb, inserted) => changes.push({ from, to, text: inserted.toString() }));
|
||||
if (changes.length !== 1) return tr;
|
||||
let { from } = changes[0]!;
|
||||
const { to, text } = changes[0]!;
|
||||
if (!text.trim()) return tr;
|
||||
const line = start.doc.lineAt(from);
|
||||
const before = start.doc.sliceString(line.from, from);
|
||||
const atIndent = /^\s*$/.test(before);
|
||||
const tabSize = start.tabSize;
|
||||
const unitCols = getIndentUnit(start) || tabSize;
|
||||
const useTabs = start.facet(indentUnit)[0] === "\t";
|
||||
let targetCol = leadingColumns(atIndent ? before : line.text, tabSize);
|
||||
let prefix = "";
|
||||
if (atIndent) {
|
||||
let proper = getIndentation(start, line.from);
|
||||
const clearedZero = start.field(explicitZeroIndentLine, false) === line.number;
|
||||
if (!targetCol && !proper && !clearedZero) {
|
||||
const above = previousLineIndent(start, line.number);
|
||||
if (above) proper = above;
|
||||
}
|
||||
if (
|
||||
proper !== null &&
|
||||
proper !== targetCol &&
|
||||
!(targetCol > 0 && proper === 0 && start.facet(language)?.name === "yaml")
|
||||
) {
|
||||
targetCol = proper;
|
||||
prefix = indentString(start, proper);
|
||||
from = line.from;
|
||||
}
|
||||
}
|
||||
if (proper !== null && proper !== targetCol) {
|
||||
targetCol = proper;
|
||||
prefix = indentString(start, proper);
|
||||
from = line.from;
|
||||
}
|
||||
}
|
||||
const block = indentPastedText(text, targetCol, useTabs ? "\t" : " ", tabSize, unitCols);
|
||||
const out = prefix + (prefix ? block.trimStart() : block);
|
||||
const formatted = start.changes({ from, to, insert: out });
|
||||
const first: TransactionSpec = {
|
||||
changes: formatted,
|
||||
// Keep line-wise paste carets on their original text after reformatting.
|
||||
selection: tr.newSelection.map(tr.changes.invertedDesc, 1).map(formatted, 1),
|
||||
userEvent: "input.paste",
|
||||
scrollIntoView: true,
|
||||
};
|
||||
if (mode !== "indentEachLine") return out === text && from === changes[0]!.from ? tr : first;
|
||||
// Lines whose start lies in the pasted text, plus the paste line when it was pasted at its indentation.
|
||||
const pasted = start.update({ ...first, filter: false }).state;
|
||||
const end = from + out.length;
|
||||
const firstLine = pasted.doc.lineAt(from);
|
||||
const lineStart = atIndent ? firstLine.from : firstLine.to + 1;
|
||||
const lastLine = pasted.doc.lineAt(end);
|
||||
const lineEnd = lastLine.from === end && end > from ? end - 1 : end;
|
||||
if (lineStart > lineEnd) return first;
|
||||
const reindent = indentRange(pasted, lineStart, lineEnd);
|
||||
if (reindent.empty) return first;
|
||||
return [first, { changes: reindent, selection: pasted.selection.map(reindent, 1), sequential: true }];
|
||||
});
|
||||
const block = indentPastedText(text, targetCol, useTabs ? "\t" : " ", tabSize, unitCols);
|
||||
const out = prefix + (prefix ? block.trimStart() : block);
|
||||
const formatted = start.changes({ from, to, insert: out });
|
||||
const first: TransactionSpec = {
|
||||
changes: formatted,
|
||||
// Keep line-wise paste carets on their original text after reformatting.
|
||||
selection: tr.newSelection.map(tr.changes.invertedDesc, 1).map(formatted, 1),
|
||||
userEvent: "input.paste",
|
||||
scrollIntoView: true,
|
||||
};
|
||||
if (mode !== "indentEachLine") return out === text && from === changes[0]!.from ? tr : first;
|
||||
// Lines whose start lies in the pasted text, plus the paste line when it was pasted at its indentation.
|
||||
const pasted = start.update({ ...first, filter: false }).state;
|
||||
const end = from + out.length;
|
||||
const firstLine = pasted.doc.lineAt(from);
|
||||
const lineStart = atIndent ? firstLine.from : firstLine.to + 1;
|
||||
const lastLine = pasted.doc.lineAt(end);
|
||||
const lineEnd = lastLine.from === end && end > from ? end - 1 : end;
|
||||
if (lineStart > lineEnd) return first;
|
||||
const reindent = indentRange(pasted, lineStart, lineEnd);
|
||||
if (reindent.empty) return first;
|
||||
return [first, { changes: reindent, selection: pasted.selection.map(reindent, 1), sequential: true }];
|
||||
}),
|
||||
];
|
||||
}
|
||||
|
||||
// --- rich-text copy -----------------------------------------------------------
|
||||
|
||||
Reference in new issue
Заблокувати користувача