aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Zhang <wodesuck@gmail.com>2022-10-14 17:12:46 +0800
committerGitHub <noreply@github.com>2022-10-14 02:12:46 -0700
commit81986a7349da7b88abde459194078e9893e8ae8b (patch)
treeff0e1bf0d73426fbfa4d2474c3a00242715f4cee
parent9931db2e3fa43ed02e1b0bc6f167ed5398fa6369 (diff)
downloadrneovim-81986a7349da7b88abde459194078e9893e8ae8b.tar.gz
rneovim-81986a7349da7b88abde459194078e9893e8ae8b.tar.bz2
rneovim-81986a7349da7b88abde459194078e9893e8ae8b.zip
fix(lua): on_yank error with blockwise multibyte region #20162
Prevent out of range error when calling `str_byteindex`. Use `vim.str_byteindex(bufline, #bufline)` to cacluate utf length of `bufline`. fix #20161
-rw-r--r--runtime/lua/vim/_editor.lua9
-rw-r--r--test/functional/lua/vim_spec.lua20
2 files changed, 20 insertions, 9 deletions
diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua
index 28e5897aa9..0f312f19f5 100644
--- a/runtime/lua/vim/_editor.lua
+++ b/runtime/lua/vim/_editor.lua
@@ -416,11 +416,16 @@ function vim.region(bufnr, pos1, pos2, regtype, inclusive)
c2 = c1 + regtype:sub(2)
-- and adjust for non-ASCII characters
bufline = vim.api.nvim_buf_get_lines(bufnr, l, l + 1, true)[1]
- if c1 < #bufline then
+ local utflen = vim.str_utfindex(bufline, #bufline)
+ if c1 <= utflen then
c1 = vim.str_byteindex(bufline, c1)
+ else
+ c1 = #bufline + 1
end
- if c2 < #bufline then
+ if c2 <= utflen then
c2 = vim.str_byteindex(bufline, c2)
+ else
+ c2 = #bufline + 1
end
else
c1 = (l == pos1[1]) and pos1[2] or 0
diff --git a/test/functional/lua/vim_spec.lua b/test/functional/lua/vim_spec.lua
index 47a0004183..f250a3ec93 100644
--- a/test/functional/lua/vim_spec.lua
+++ b/test/functional/lua/vim_spec.lua
@@ -2227,13 +2227,19 @@ describe('lua stdlib', function()
eq(true, exec_lua[[return vim.g.test]])
end)
- it('vim.region', function()
- insert(helpers.dedent( [[
- text tααt tααt text
- text tαxt txtα tex
- text tαxt tαxt
- ]]))
- eq({5,15}, exec_lua[[ return vim.region(0,{1,5},{1,14},'v',true)[1] ]])
+ describe('vim.region', function()
+ it('charwise', function()
+ insert(helpers.dedent( [[
+ text tααt tααt text
+ text tαxt txtα tex
+ text tαxt tαxt
+ ]]))
+ eq({5,15}, exec_lua[[ return vim.region(0,{1,5},{1,14},'v',true)[1] ]])
+ end)
+ it('blockwise', function()
+ insert([[αα]])
+ eq({0,5}, exec_lua[[ return vim.region(0,{0,0},{0,4},'3',true)[0] ]])
+ end)
end)
describe('vim.on_key', function()