Skip to content

Commit

Permalink
feat: use getcharpos
Browse files Browse the repository at this point in the history
  • Loading branch information
higashi000 committed Dec 12, 2022
1 parent 29f5063 commit c3c79da
Showing 1 changed file with 6 additions and 24 deletions.
30 changes: 6 additions & 24 deletions denops/kakkonan/mod/surround.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ export async function surroundBrackets(
await execute(vim, "normal `<");

let startColNo: number;
let finishColNo: number;

if (await vim.call("has", "nvim")) {
const line = await vim.call("getline", ".") as string;
const pos = await vim.call("getpos", ".") as number[];
Expand All @@ -35,25 +33,9 @@ export async function surroundBrackets(
const finishLineNo = await vim.call("line", ".") as number;
let isEnd = false;

if (await vim.call("has", "nvim")) {
const line = await vim.call("getline", ".") as string;
const pos = await vim.call("getpos", ".") as number[];
const col = await vim.call("charidx", line, pos[2]);
const byteCol = await vim.call("byteidx", line, col);
if (col === -1) {
isEnd = true;
}
const pos = await vim.call("getcharpos", ".");

if (byteCol === pos[2] || byteCol === -1) {
finishColNo = col;
} else {
finishColNo = col + 1;
}
} else {
const pos = await vim.call("getcharpos", ".");

finishColNo = pos[2] + 1;
}
const finishColNo = pos[2];

const line = await vim.call("getline", ".") as string;

Expand All @@ -74,14 +56,14 @@ export async function surroundBrackets(
return;
}

let surroundText = '';
let surroundText = "";
if (!isEnd) {
surroundText = line.slice(0, startColNo - 1) + inputBracket +
line.slice(startColNo - 1, finishColNo) +
brackets[inputBracket] + line.slice(finishColNo, line.length);
line.slice(startColNo - 1, finishColNo) +
brackets[inputBracket] + line.slice(finishColNo, line.length);
} else {
surroundText = line.slice(0, startColNo - 1) + inputBracket +
line.slice(startColNo - 1, line.length) + brackets[inputBracket];
line.slice(startColNo - 1, line.length) + brackets[inputBracket];
}
await vim.call("setline", startLineNo, surroundText);

Expand Down

0 comments on commit c3c79da

Please sign in to comment.