aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/range.lua
diff options
context:
space:
mode:
authorYi Ming <ofseed@foxmail.com>2026-05-17 16:39:25 +0800
committerYi Ming <ofseed@foxmail.com>2026-05-18 22:19:07 +0800
commit856bc6d284f6c9f31f21d01e69b44457502a773f (patch)
tree0d5a547aa799d7322eb1ebc8df78759f09f5f0af /runtime/lua/vim/range.lua
parentf2b031136ff607866a9ab7933408aa86eb8b99a7 (diff)
downloadrneovim-856bc6d284f6c9f31f21d01e69b44457502a773f.tar.gz
rneovim-856bc6d284f6c9f31f21d01e69b44457502a773f.tar.bz2
rneovim-856bc6d284f6c9f31f21d01e69b44457502a773f.zip
fix(range): handle inclusive/exclusive positions on multibyte characters
Diffstat (limited to 'runtime/lua/vim/range.lua')
-rw-r--r--runtime/lua/vim/range.lua14
1 files changed, 8 insertions, 6 deletions
diff --git a/runtime/lua/vim/range.lua b/runtime/lua/vim/range.lua
index dd22d1151e..e069df4a3a 100644
--- a/runtime/lua/vim/range.lua
+++ b/runtime/lua/vim/range.lua
@@ -150,11 +150,12 @@ end
---@param buf integer
---@return integer, integer
local function to_inclusive_pos(buf, row, col)
+ local line = get_line(buf, row)
if col > 0 then
- col = col - 1
+ col = col + vim.str_utf_start(line, col) - 1
elseif col == 0 and row > 0 then
row = row - 1
- col = #get_line(buf, row)
+ col = #line > 0 and #line + vim.str_utf_start(line, #line) - 1 or 0
end
return row, col
@@ -165,11 +166,12 @@ end
---@param buf integer
---@return integer, integer
local function to_exclusive_pos(buf, row, col)
- if col >= #get_line(buf, row) then
+ local line = get_line(buf, row)
+ if col >= #line then
row = row + 1
col = 0
else
- col = col + 1
+ col = col + vim.str_utf_end(line, col + 1) + 1
end
return row, col
@@ -179,7 +181,7 @@ end
---@param r1 vim.Range
---@param r2 vim.Range
function M.__lt(r1, r2)
- if r1:is_empty() then
+ if r1:is_empty() or r2:is_empty() then
return cmp_pos(r1[3], r1[4], r2[1], r2[2]) ~= 1
end
@@ -191,7 +193,7 @@ end
---@param r1 vim.Range
---@param r2 vim.Range
function M.__le(r1, r2)
- if r1:is_empty() then
+ if r1:is_empty() or r2:is_empty() then
return cmp_pos(r1[3], r1[4], r2[1], r2[2]) ~= 1
end