Skip to content

Commit b1be3d1

Browse files
committed
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)
1 parent 1f6a8e2 commit b1be3d1

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,17 @@ MiniDeps.add({
598598
name = 'Buffer',
599599
module = 'blink.cmp.sources.buffer',
600600
fallback_for = { 'lsp' },
601+
-- opts = {
602+
-- -- Specifies the buffer numbers to complete
603+
-- -- Example usage for getting all visible buffers
604+
-- get_bufnrs = function()
605+
-- local bufs = {}
606+
-- for _, win in ipairs(vim.api.nvim_list_wins()) do
607+
-- bufs[vim.api.nvim_win_get_buf(win)] = true
608+
-- end
609+
-- return vim.tbl_keys(bufs)
610+
-- end,
611+
-- }
601612
},
602613
},
603614
},

lua/blink/cmp/sources/buffer.lua

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
local uv = vim.uv
66

7+
--- @param bufnr integer
78
---@return string
8-
local function get_buf_text()
9-
local bufnr = vim.api.nvim_get_current_buf()
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,30 @@ 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 or function() return { vim.api.nvim_get_current_buf() } end
76+
return self
77+
end
7178

7279
function buffer:get_completions(_, callback)
7380
local transformed_callback = function(items)
7481
callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = items })
7582
end
7683

77-
local buf_text = get_buf_text()
84+
local buf_texts = {}
85+
for _, buf in ipairs(self.get_bufnrs()) do
86+
table.insert(buf_texts, get_buf_text(buf))
87+
end
88+
local buf_text = table.concat(buf_texts, '\n')
7889
-- should take less than 2ms
7990
if #buf_text < 20000 then
8091
run_sync(buf_text, transformed_callback)
@@ -91,3 +102,4 @@ function buffer:get_completions(_, callback)
91102
end
92103

93104
return buffer
105+

0 commit comments

Comments
 (0)