Skip to content

Commit

Permalink
feat: text editor edit() -> insert() and replace() implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
Cedric Halbronn committed Mar 21, 2024
1 parent b91758d commit 746a7be
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions packages/cursorless-neovim/src/ide/neovim/NeovimEdit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,40 @@ async function neovimDelete(range: Range): Promise<void> {
});
}

function neovimInsert(position: Position, text: string) {
async function neovimInsert(position: Position, text: string) {
// Uniform newlines so we can easily split
const newlines = text.replace(/(?:\r\n|\r|\n)/g, "\n").split("\n");
throw Error("neovimEdit(): Insert not implemented");
const newLines = text.replace(/(?:\r\n|\r|\n)/g, "\n").split("\n");

const client = neovimContext().client;
const buffer = await client.window.buffer;

// are we inserting at the beginning of a line?
if (position.character === 0) {
await buffer.insert(newLines, position.line);
return;
}

// we are inserting from the middle of a line
const firstLine = (
await buffer.getLines({
start: position.line,
end: position.line + 1,
strictIndexing: true,
})
)[0];
const newFirstLine =
firstLine.slice(0, position.character) +
newLines[0] +
firstLine.slice(position.character);

await buffer.setLines([newFirstLine, ...newLines.slice(1)], {
start: position.line,
end: position.line + 1,
strictIndexing: true,
});
}

function neovimReplace(range: Range, text: string) {
// Uniform newlines so we can easily split
const newlines = text.replace(/(?:\r\n|\r|\n)/g, "\n").split("\n");
throw Error("neovimEdit(): Replace not implemented");
async function neovimReplace(range: Range, text: string) {
await neovimDelete(range);
await neovimInsert(range.start, text);
}

0 comments on commit 746a7be

Please sign in to comment.