This commit is contained in:
hrsh7th
2021-08-16 14:02:09 +09:00
parent 141187561c
commit e4deb0142a
5 changed files with 41 additions and 22 deletions

View File

@@ -112,4 +112,28 @@ misc.copy = function(tbl)
return copy
end
---Safe version of vim.str_utfindex
---@param text string
---@param vimindex number
---@return number
misc.to_utfindex = function(text, vimindex)
return vim.str_utfindex(text, math.max(0, math.min(vimindex - 1, #text)))
end
---Safe version of vim.str_byteindex
---@param text string
---@param utfindex number
---@return number
misc.to_vimindex = function(text, utfindex)
for i = utfindex, 1, -1 do
local s, v = pcall(function()
return vim.str_byteindex(text, i) + 1
end)
if s then
return v
end
end
return utfindex + 1
end
return misc