mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 14:05:11 +00:00
add new getCursor and getLines methods to editor (#201)
This commit is contained in:
parent
deee1c2952
commit
4b04c87b3d
2 changed files with 36 additions and 0 deletions
|
|
@ -595,6 +595,14 @@ export class Editor implements Component {
|
||||||
return this.state.lines.join("\n");
|
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 {
|
setText(text: string): void {
|
||||||
this.historyIndex = -1; // Exit history browsing mode
|
this.historyIndex = -1; // Exit history browsing mode
|
||||||
this.setTextInternal(text);
|
this.setTextInternal(text);
|
||||||
|
|
|
||||||
|
|
@ -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", () => {
|
describe("Unicode text editing behavior", () => {
|
||||||
it("inserts mixed ASCII, umlauts, and emojis as literal text", () => {
|
it("inserts mixed ASCII, umlauts, and emojis as literal text", () => {
|
||||||
const editor = new Editor(defaultEditorTheme);
|
const editor = new Editor(defaultEditorTheme);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue