Skip to content

Commit

Permalink
Update to check cmp.windows.autocomplete for if the menu is open or c…
Browse files Browse the repository at this point in the history
…losed.
  • Loading branch information
dsully committed Oct 9, 2024
1 parent 7fb946a commit aba9921
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
-- set to 'mono' for 'Nerd Font Mono' or 'normal' for 'Nerd Font'
-- adjusts spacing to ensure icons are aligned
nerd_font_variant = 'normal',

-- experimental auto-brackets support
-- accept = { auto_brackets = { enabled = true } },

-- accept = { auto_brackets = { enabled = true } }
-- experimental signature help support
-- trigger = { signature_help = { enabled = true } },
-- trigger = { signature_help = { enabled = true } }
}
}
```
Expand Down Expand Up @@ -192,18 +192,18 @@ For LazyVim/distro users, you can disable nvim-cmp via:
},
-- FOR REF: full example
providers = {
{
{
-- all of these properties work on every source
{
'blink.cmp.sources.lsp',
keyword_length = 0,
score_offset = 0,
trigger_characters = { 'f', 'o', 'o' },
opts = {},
},
},
-- the follow two sources have additional options
{
'blink.cmp.sources.path',
{
'blink.cmp.sources.path',
opts = {
trailing_slash = false,
label_trailing_slash = true,
Expand Down
18 changes: 17 additions & 1 deletion lua/blink/cmp/fuzzy/init.lua
Original file line number Diff line number Diff line change
@@ -1,29 +1,42 @@
local config = require('blink.cmp.config').fuzzy

local fuzzy = {
---@type blink.cmp.Context?
last_context = nil,
---@type blink.cmp.CompletionItem[]?
last_items = nil,
rust = require('blink.cmp.fuzzy.ffi'),
}

---@param db_path string
function fuzzy.init_db(db_path)
fuzzy.rust.init_db(db_path)

vim.api.nvim_create_autocmd('VimLeavePre', {
callback = function() fuzzy.rust.destroy_db() end,
callback = fuzzy.rust.destroy_db,
})

return fuzzy
end

---@param item blink.cmp.CompletionItem
function fuzzy.access(item) fuzzy.rust.access(item) end

---@param lines string
function fuzzy.get_words(lines) return fuzzy.rust.get_words(lines) end

---@param needle string
---@param items blink.cmp.CompletionItem[]?
---@return blink.cmp.CompletionItem[]
function fuzzy.filter_items(needle, items)
-- convert to table of strings
local haystack_labels = {}
local haystack_score_offsets = {}
local haystack_kinds = {}
local haystack_sources = {}

items = items or {}

for _, item in ipairs(items) do
table.insert(haystack_labels, item.label)
table.insert(haystack_score_offsets, item.score_offset or 0)
Expand Down Expand Up @@ -60,6 +73,9 @@ function fuzzy.filter_items(needle, items)
return filtered_items
end

---@param needle string
---@param context blink.cmp.Context
---@param items blink.cmp.CompletionItem[]?
function fuzzy.filter_items_with_cache(needle, context, items)
if items == nil then
if fuzzy.last_context == nil or fuzzy.last_context.id ~= context.id then return {} end
Expand Down
4 changes: 2 additions & 2 deletions lua/blink/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ cmp.hide = function()
end

--- @param callback fun(context: blink.cmp.Context)
cmp.on_show = function(callback) cmp.trigger.listen_on_show(callback) end
cmp.on_open = function(callback) cmp.windows.autocomplete.listen_on_open(callback) end

--- @param callback fun()
cmp.on_hide = function(callback) cmp.trigger.listen_on_hide(callback) end
cmp.on_close = function(callback) cmp.windows.autocomplete.listen_on_close(callback) end

cmp.accept = function()
local item = cmp.windows.autocomplete.get_selected_item()
Expand Down
3 changes: 3 additions & 0 deletions lua/blink/cmp/sources/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

local uv = vim.uv

---@return string
local function get_buf_text()
local bufnr = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
Expand Down Expand Up @@ -45,6 +46,8 @@ local function words_to_items(words)
return items
end

--- @param buf_text string
--- @param callback fun(items: blink.cmp.CompletionItem[])
local function run_sync(buf_text, callback) callback(words_to_items(require('blink.cmp.fuzzy').get_words(buf_text))) end

local function run_async(buf_text, callback)
Expand Down
29 changes: 25 additions & 4 deletions lua/blink/cmp/windows/autocomplete.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,18 @@ local config = require('blink.cmp.config')
local renderer = require('blink.cmp.windows.lib.render')
local autocmp_config = config.windows.autocomplete
local autocomplete = {
---@type blink.cmp.CompletionItem[]
items = {},
---@type blink.cmp.Context?
context = nil,
event_targets = {
on_position_update = {},
--- @type fun(item: blink.cmp.CompletionItem?, context: blink.cmp.Context)
on_select = function() end,
on_close = function() end,
--- @type table<fun()>
on_close = {},
--- @type table<fun()>
on_open = {},
},
}

Expand Down Expand Up @@ -55,11 +61,15 @@ end

---------- Visibility ----------

--- @param context blink.cmp.Context
--- @param items blink.cmp.CompletionItem[]
function autocomplete.open_with_items(context, items)
autocomplete.context = context
autocomplete.items = items
autocomplete.draw()

vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)

autocomplete.win:open()

autocomplete.context = context
Expand All @@ -72,15 +82,25 @@ end

function autocomplete.open()
if autocomplete.win:is_open() then return end
vim.iter(autocomplete.event_targets.on_open):each(function(callback) callback() end)
autocomplete.win:open()
end

function autocomplete.close()
if not autocomplete.win:is_open() then return end
autocomplete.win:close()
autocomplete.event_targets.on_close()
vim.iter(autocomplete.event_targets.on_close):each(function(callback) callback() end)
end
function autocomplete.listen_on_close(callback) autocomplete.event_targets.on_close = callback end

--- Add a listener for when the autocomplete window closes
--- @param callback fun()
function autocomplete.listen_on_close(callback) table.insert(autocomplete.event_targets.on_close, callback) end

--- Add a listener for when the autocomplete window opens
--- This is useful for hiding GitHub Copilot ghost text and similar functionality.
---
--- @param callback fun()
function autocomplete.listen_on_open(callback) table.insert(autocomplete.event_targets.on_open, callback) end

--- @param context blink.cmp.Context
--- TODO: Don't switch directions if the context is the same
Expand Down Expand Up @@ -194,7 +214,7 @@ function autocomplete.draw()
kind = kind,
kind_icon = kind_icon,
icon_gap = icon_gap,
deprecated = item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)),
deprecated = item.deprecated or (item.tags and vim.tbl_contains(item.tags, 1)) or false,
})
)
end
Expand Down Expand Up @@ -245,6 +265,7 @@ function autocomplete.render_item_reversed(ctx)
}
end

---@return blink.cmp.CompletionItem?
function autocomplete.get_selected_item()
if not autocomplete.win:is_open() then return end
local current_line = vim.api.nvim_win_get_cursor(autocomplete.win:get_win())[1]
Expand Down

0 comments on commit aba9921

Please sign in to comment.