fix(tui): refactor the multi-line insertion handling

This commit is contained in:
Sviatoslav Abakumov 2026-01-25 16:29:20 +04:00
parent 7d2d170c1b
commit 609095b0b6
No known key found for this signature in database

View file

@ -849,30 +849,23 @@ export class Editor implements Component, Focusable {
this.state.cursorCol += normalized.length; this.state.cursorCol += normalized.length;
} else { } else {
// Multi-line insertion // Multi-line insertion
const newLines: string[] = []; this.state.lines = [
// All lines before current line
...this.state.lines.slice(0, this.state.cursorLine),
// Add all lines before current line // The first inserted line merged with text before cursor
for (let i = 0; i < this.state.cursorLine; i++) { beforeCursor + insertedLines[0],
newLines.push(this.state.lines[i] || "");
}
// Add the first inserted line merged with text before cursor // All middle inserted lines
newLines.push(beforeCursor + (insertedLines[0] || "")); ...insertedLines.slice(1, -1),
// Add all middle inserted lines // The last inserted line with text after cursor
for (let i = 1; i < insertedLines.length - 1; i++) { insertedLines[insertedLines.length - 1] + afterCursor,
newLines.push(insertedLines[i] || "");
}
// Add the last inserted line with text after cursor // All lines after current line
newLines.push((insertedLines[insertedLines.length - 1] || "") + afterCursor); ...this.state.lines.slice(this.state.cursorLine + 1),
];
// Add all lines after current line
for (let i = this.state.cursorLine + 1; i < this.state.lines.length; i++) {
newLines.push(this.state.lines[i] || "");
}
this.state.lines = newLines;
this.state.cursorLine += insertedLines.length - 1; this.state.cursorLine += insertedLines.length - 1;
this.state.cursorCol = (insertedLines[insertedLines.length - 1] || "").length; this.state.cursorCol = (insertedLines[insertedLines.length - 1] || "").length;
} }