Let me preface this by saying that everything worked just fine until the most recent lspconfig
update.
After that, I have noticed that vim.api.nvim_list_bufs()
in the snippet below ends up adding duplicated values to the bufs_fn_len
.
Well, until the bug (?) is fixed, let’s add a check and insert values until if those aren’t already present in the table. Turns out, this is not as straightforward as it sounds. Check the example below:
local bufs_fn_len = {}
for _, buf_nr in ipairs(vim.api.nvim_list_bufs()) do
if vim.api.nvim_buf_is_valid(buf_nr) then
local buf = vim.api.nvim_buf_get_name(buf_nr)
local buf_nm = vim.fn.expand(buf)
if buf_nm ~= '' then
buf_nm = vim.fn.substitute(buf_nm, vim.fn.getcwd(), '', 'g')
buf_nm = vim.fn.substitute(buf_nm, vim.fn.expand('$HOME'), '~', 'g')
buf_nm = vim.fs.basename(buf_nm)
buf_nm = string.len(buf_nm)
if (not bufs_fn_len[buf_nm]) then
table.insert(bufs_fn_len, buf_nm)
vim.print('Adding buf ' .. buf_nm)
vim.print('Content: '.. table.concat(bufs_fn_len, '; '))
end
end
end
On my end this results in:
Adding buf 11
Content: 11
Adding buf 13
Content: 11; 13
Adding buf 12
Content: 11; 13; 12
Adding buf 9
Content: 11; 13; 12; 9
Adding buf 8
Content: 11; 13; 12; 9; 8
Adding buf 13
Content: 11; 13; 12; 9; 8; 13
Adding buf 8
Content: 11; 13; 12; 9; 8; 13; 8
Adding buf 10
Content: 11; 13; 12; 9; 8; 13; 8; 10
Adding buf 11
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11
Adding buf 11
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11; 11
Adding buf 14
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11; 11; 14
Adding buf 17
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11; 11; 14; 17
Adding buf 16
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11; 11; 14; 17; 16
Adding buf 15
Content: 11; 13; 12; 9; 8; 13; 8; 10; 11; 11; 14; 17; 16; 15
Double-checking that the issue is coming directly from nvim_list_bufs()
:lua vim.print(vim.api.nvim_list_bufs())
returns:
{ 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22 }
Two questions
- Why
vim.api.nvim_list_bufs()
has duplicated values in the first place? - Why the
if (not bufs_fn_len[buf_nm]) then
conditional has no effect?