The function below is written in Lua and returns the list of buffers from the current neovim session, it also allows the option to specify a optional table (object) as a parameter, with a listed
property to filter the type of buffer returned by the function (only listed buffers, or every single one).
local function get_buffers(options)
local buffers = {}
for buffer = 1, vim.fn.bufnr('$') do
local is_listed = vim.fn.buflisted(buffer) == 1
if options.listed and is_listed then
table.insert(buffers, buffer)
else
table.insert(buffers, buffer)
end
end
return buffers
end
return get_buffers
The if
..else
part seems a little bit off for me, I'm not sure if it can be improved, but something tells me that there's some way to make this less repetitive