Skip to content

Commit

Permalink
feat: support get_bufnrs for the buffer source (#411)
Browse files Browse the repository at this point in the history
* 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 <liamcdyer@gmail.com>
  • Loading branch information
gh-liu and Saghen authored Dec 2, 2024
1 parent 160b687 commit 4c65dbd
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,16 @@ MiniDeps.add({
name = 'Buffer',
module = 'blink.cmp.sources.buffer',
fallback_for = { 'lsp' },
opts = {
-- default to all visible buffers
get_bufnrs = function()
return vim
.iter(vim.api.nvim_list_wins())
:map(function(win) return vim.api.nvim_win_get_buf(win) end)
:filter(function(buf) return vim.bo[buf].buftype ~= 'nofile' end)
:totable()
end,
}
},
},
},
Expand Down
12 changes: 12 additions & 0 deletions lua/blink/cmp/lib/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ function utils.union_keys(t1, t2)
return vim.tbl_keys(t3)
end

--- Returns a list of unique values from the input array
--- @generic T
--- @param arr T[]
--- @return T[]
function utils.deduplicate(arr)
local hash = {}
for _, v in ipairs(arr) do
hash[v] = true
end
return vim.tbl_keys(hash)
end

--- Determines whether the current buffer is a "special" buffer or if the filetype is in the list of ignored filetypes
--- @return boolean
function utils.is_blocked_buffer()
Expand Down
31 changes: 25 additions & 6 deletions lua/blink/cmp/sources/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

local uv = vim.uv

---@return string
local function get_buf_text()
local bufnr = vim.api.nvim_get_current_buf()
--- @param bufnr integer
--- @return string
local function get_buf_text(bufnr)
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)

-- exclude word under the cursor
Expand Down Expand Up @@ -62,19 +62,38 @@ local function run_async(buf_text, callback)
worker:queue(buf_text)
end

--- @class blink.cmp.BufferOpts
--- @field get_bufnrs fun(): integer[]

--- Public API

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

function buffer.new() return setmetatable({}, { __index = buffer }) end
function buffer.new(opts)
opts = opts or {} ---@type blink.cmp.BufferOpts
local self = setmetatable({}, { __index = buffer })
self.get_bufnrs = opts.get_bufnrs
or function()
return vim
.iter(vim.api.nvim_list_wins())
:map(function(win) return vim.api.nvim_win_get_buf(win) end)
:filter(function(buf) return vim.bo[buf].buftype ~= 'nofile' end)
:totable()
end
return self
end

function buffer:get_completions(_, callback)
local transformed_callback = function(items)
callback({ is_incomplete_forward = false, is_incomplete_backward = false, items = items })
end

local buf_text = get_buf_text()
local bufnrs = require('blink.cmp.lib.utils').deduplicate(self.get_bufnrs())
local buf_texts = {}
for _, buf in ipairs(bufnrs) do
table.insert(buf_texts, get_buf_text(buf))
end
local buf_text = table.concat(buf_texts, '\n')
-- should take less than 2ms
if #buf_text < 20000 then
run_sync(buf_text, transformed_callback)
Expand Down

0 comments on commit 4c65dbd

Please sign in to comment.