Skip to content

Commit 4c65dbd

Browse files
gh-liuSaghen
andauthored
feat: support get_bufnrs for the buffer source (#411)
* feat: support specifies the bufnrs for the buffer source as [cmp-buffer](https://github.com/hrsh7th/cmp-buffer?tab=readme-ov-file#get_bufnrs-type-fun-number) * feat: deduplicate get_bufnrs, default to normal visible buffers * style: get_buf_text return type --------- Co-authored-by: Liam Dyer <[email protected]>
1 parent 160b687 commit 4c65dbd

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ MiniDeps.add({
598598
name = 'Buffer',
599599
module = 'blink.cmp.sources.buffer',
600600
fallback_for = { 'lsp' },
601+
opts = {
602+
-- default to all visible buffers
603+
get_bufnrs = function()
604+
return vim
605+
.iter(vim.api.nvim_list_wins())
606+
:map(function(win) return vim.api.nvim_win_get_buf(win) end)
607+
:filter(function(buf) return vim.bo[buf].buftype ~= 'nofile' end)
608+
:totable()
609+
end,
610+
}
601611
},
602612
},
603613
},

lua/blink/cmp/lib/utils.lua

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ function utils.union_keys(t1, t2)
2828
return vim.tbl_keys(t3)
2929
end
3030

31+
--- Returns a list of unique values from the input array
32+
--- @generic T
33+
--- @param arr T[]
34+
--- @return T[]
35+
function utils.deduplicate(arr)
36+
local hash = {}
37+
for _, v in ipairs(arr) do
38+
hash[v] = true
39+
end
40+
return vim.tbl_keys(hash)
41+
end
42+
3143
--- Determines whether the current buffer is a "special" buffer or if the filetype is in the list of ignored filetypes
3244
--- @return boolean
3345
function utils.is_blocked_buffer()

lua/blink/cmp/sources/buffer.lua

+25-6
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
local uv = vim.uv
66

7-
---@return string
8-
local function get_buf_text()
9-
local bufnr = vim.api.nvim_get_current_buf()
7+
--- @param bufnr integer
8+
--- @return string
9+
local function get_buf_text(bufnr)
1010
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
1111

1212
-- exclude word under the cursor
@@ -62,19 +62,38 @@ local function run_async(buf_text, callback)
6262
worker:queue(buf_text)
6363
end
6464

65+
--- @class blink.cmp.BufferOpts
66+
--- @field get_bufnrs fun(): integer[]
67+
6568
--- Public API
6669

67-
--- @class blink.cmp.Source
6870
local buffer = {}
6971

70-
function buffer.new() return setmetatable({}, { __index = buffer }) end
72+
function buffer.new(opts)
73+
opts = opts or {} ---@type blink.cmp.BufferOpts
74+
local self = setmetatable({}, { __index = buffer })
75+
self.get_bufnrs = opts.get_bufnrs
76+
or function()
77+
return vim
78+
.iter(vim.api.nvim_list_wins())
79+
:map(function(win) return vim.api.nvim_win_get_buf(win) end)
80+
:filter(function(buf) return vim.bo[buf].buftype ~= 'nofile' end)
81+
:totable()
82+
end
83+
return self
84+
end
7185

7286
function buffer:get_completions(_, callback)
7387
local transformed_callback = function(items)
7488
callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = items })
7589
end
7690

77-
local buf_text = get_buf_text()
91+
local bufnrs = require('blink.cmp.lib.utils').deduplicate(self.get_bufnrs())
92+
local buf_texts = {}
93+
for _, buf in ipairs(bufnrs) do
94+
table.insert(buf_texts, get_buf_text(buf))
95+
end
96+
local buf_text = table.concat(buf_texts, '\n')
7897
-- should take less than 2ms
7998
if #buf_text < 20000 then
8099
run_sync(buf_text, transformed_callback)

0 commit comments

Comments
 (0)