Skip to content

Bug: sources.providers.buffer.opts.get_bufnrs cannot deal with multiple buffers #433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
2 tasks done
chrisgrieser opened this issue Dec 3, 2024 · 0 comments
Closed
2 tasks done
Labels
bug Something isn't working

Comments

@chrisgrieser
Copy link
Contributor

chrisgrieser commented Dec 3, 2024

Make sure you have done the following

  • I have updated to the latest version of blink.cmp
  • I have read the README

Bug Description

When setting sources.providers.buffer.opts.get_bufnrs to something that returns multiple buffer numbers, you regularly get this error:

failed to get completions with error: ...are/nvim/lazy/blink.cmp/lua/blink/cmp/sources/buffer.lua:18: attempt to index local 'line' (a nil value)

The issue seems to be caused by this:

local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
-- exclude word under the cursor
local line_number = vim.api.nvim_win_get_cursor(0)[1]
local column = vim.api.nvim_win_get_cursor(0)[2]
local line = lines[line_number]
local start_col = column
while start_col > 1 do
local char = line:sub(start_col, start_col)

I think the issue arises from line_number being the current line number of the current window where the cursor is (line 13), but then being used to index lines, which are the lines of any buffer passed (line 10). Thus, the error occurs when the cursor is currently on line n, while there is a different buffer passed by get_bufnrs that has fewer than n lines. In this case line[line_number] is nil and consequently errors on line 18, explaining the error message.

Relevant configuration

sources = {
	providers = {
		buffer = {
			opts = {
				get_bufnrs = function()
					local allOpenBuffers = vim.fn.getbufinfo { buflisted = 1, bufloaded = 1 }
					local allBufs = vim.iter(allOpenBuffers)
						:filter(function(buf) return vim.bo[buf.bufnr].buftype == "" end)
						:map(function(buf) return buf.bufnr end)
						:totable()
					return allBufs
				end,
			},
		},
	},
},

neovim version

NVIM v0.10.2

blink.cmp version: branch, tag, or commit

0.7.1

@chrisgrieser chrisgrieser added the bug Something isn't working label Dec 3, 2024
@Saghen Saghen closed this as completed in 2cbb02d Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant