From 81986a7349da7b88abde459194078e9893e8ae8b Mon Sep 17 00:00:00 2001 From: Daniel Zhang Date: Fri, 14 Oct 2022 17:12:46 +0800 Subject: 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 --- runtime/lua/vim/_editor.lua | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'runtime/lua') 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 -- cgit