add new getCursor and getLines methods to editor (#201)

This commit is contained in:
Markus Ylisiurunen 2025-12-16 21:13:43 +02:00 committed by GitHub
parent deee1c2952
commit 4b04c87b3d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 0 deletions

View file

@ -595,6 +595,14 @@ export class Editor implements Component {
return this.state.lines.join("\n");
}
getLines(): string[] {
return [...this.state.lines];
}
getCursor(): { line: number; col: number } {
return { line: this.state.cursorLine, col: this.state.cursorCol };
}
setText(text: string): void {
this.historyIndex = -1; // Exit history browsing mode
this.setTextInternal(text);

View file

@ -247,6 +247,34 @@ describe("Editor component", () => {
});
});
describe("public state accessors", () => {
it("returns cursor position", () => {
const editor = new Editor(defaultEditorTheme);
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 0 });
editor.handleInput("a");
editor.handleInput("b");
editor.handleInput("c");
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 3 });
editor.handleInput("\x1b[D"); // Left
assert.deepStrictEqual(editor.getCursor(), { line: 0, col: 2 });
});
it("returns lines as a defensive copy", () => {
const editor = new Editor(defaultEditorTheme);
editor.setText("a\nb");
const lines = editor.getLines();
assert.deepStrictEqual(lines, ["a", "b"]);
lines[0] = "mutated";
assert.deepStrictEqual(editor.getLines(), ["a", "b"]);
});
});
describe("Unicode text editing behavior", () => {
it("inserts mixed ASCII, umlauts, and emojis as literal text", () => {
const editor = new Editor(defaultEditorTheme);