Skip to content

Commit

Permalink
fix: Cannot use strwidth to get string length (#1635)
Browse files Browse the repository at this point in the history
* test: add test

* fix: Cannot use strwidth to get string length
  • Loading branch information
xiyaowong committed Nov 19, 2023
1 parent 0bf6660 commit 9e0cff5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion runtime/lua/vscode-neovim/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ do
local is_linewise = motion == "line" or mode == "V"
if is_linewise then
start_pos = { start_pos[1], 0 }
end_pos = { end_pos[1], api.nvim_strwidth(fn.getline(end_pos[1])) - 1 }
end_pos = { end_pos[1], #fn.getline(end_pos[1]) }
end

local range = vim.lsp.util.make_given_range_params(start_pos, end_pos, 0, "utf-16").range
Expand Down
10 changes: 4 additions & 6 deletions runtime/lua/vscode-neovim/cursor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,15 @@ local function start_multi_cursor(right, skip_empty)
local end_pos = api.nvim_buf_get_mark(0, ">") ---@type number[]
for row = start_pos[1], end_pos[1] do
local line = vim.fn.getline(row)
local width = api.nvim_strwidth(line)
if width == 0 and (skip_empty or is_block) then
if #line == 0 and (skip_empty or is_block) then
else
local max_col = math.max(0, width - 1)
-- (row, col) is (1, 0)-indexed
local s_col, e_col
if is_line then
s_col = api.nvim_strwidth(line:match("^%s*") or "")
e_col = max_col
s_col = #(line:match("^%s*") or "")
e_col = #line
else
e_col = math.min(max_col, end_pos[2])
e_col = math.min(#line, end_pos[2])
s_col = math.min(e_col, start_pos[2])
end
local range = vim.lsp.util.make_given_range_params({ row, s_col }, { row, e_col }, 0, "utf-16").range
Expand Down
22 changes: 22 additions & 0 deletions src/test/suite/visual-modes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,28 @@ describe("Visual modes test", () => {
);
});

it("Visual line mode - multi cursor editing when emoji exists", async () => {
await openTextDocument({ content: "🦄😊😂🤣" });
await sendVSCodeKeys("V");
await sendInsertKey("ma");
await assertContent(
{
mode: "i",
vsCodeSelections: [new vscode.Selection(0, 8, 0, 8)],
},
client,
);
await sendVSCodeKeys("test");
await sendEscapeKey();
await assertContent(
{
mode: "n",
content: ["🦄😊😂🤣test"],
},
client,
);
});

it("Visual block mode - multi cursor editing", async () => {
await openTextDocument({ content: ["blah1 abc", "blah2 abc", "blah3 abc"].join("\n") });

Expand Down

0 comments on commit 9e0cff5

Please sign in to comment.