chore: improved buffer.len function

This commit is contained in:
əlemi 2025-01-18 14:18:03 +01:00
parent c69cd8dd5a
commit b26510e836
Signed by: alemi
GPG key ID: A4895B84D311642C

View file

@ -90,6 +90,17 @@ local function cursor_position()
end
end
---return lenght for given buffer
---@param unit string either 'bytes' for byte count or 'chars' for characters count, default to 'chars'
---@return integer
local function buffer_len(buf, unit)
local count = -1
if unit == nil then unit = 'chars' end
vim.api.nvim_buf_call(buf, function()
count = vim.fn.wordcount()[unit]
end)
return count
end
---@param buf integer?
---@return string
local function buffer_get_content(buf)
@ -138,12 +149,6 @@ local function buffer_set_content(buf, content, first, last)
end
local function buffer_len(buf)
local count = 0
vim.api.nvim_buf_call(buf, function()
count = vim.fn.wordcount().chars
end)
return count
end
---@return string