Skip to content

Commit

Permalink
fix: Outdated completion item with mini.snippets (#2126)
Browse files Browse the repository at this point in the history
* fix: Outdated completion item with mini.snippets

* fix: Outdated completion item with mini.snippets. Prevent completion suggestions directly after snippet expand

* fix: Outdated completion item with mini.snippets. Undo changes

* fix: Outdated completion item with mini.snippets. The cmp.resubscribe solution

---------

Co-authored-by: abeldekat <[email protected]>
  • Loading branch information
abeldekat and abeldekat authored Jan 23, 2025
1 parent 8c82d0b commit 1250990
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ Plug 'hrsh7th/vim-vsnip'
" Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip'
" For mini.snippets users.
" Plug 'echasnovski/mini.snippets'
" Plug 'abeldekat/cmp-mini-snippets'
" For ultisnips users.
" Plug 'SirVer/ultisnips'
" Plug 'quangnguyen30192/cmp-nvim-ultisnips'
Expand All @@ -69,6 +73,12 @@ lua <<EOF
-- require('snippy').expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)
-- For `mini.snippets` users:
-- local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
-- insert({ body = args.body }) -- Insert at cursor
-- cmp.resubscribe({ "TextChangedI", "TextChangedP" })
-- require("cmp.config").set_onetime({ sources = {} })
end,
},
window = {
Expand Down
10 changes: 10 additions & 0 deletions doc/cmp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ A recommended configuration can be found below.
" Plug 'L3MON4D3/LuaSnip'
" Plug 'saadparwaiz1/cmp_luasnip'

" For mini.snippets users.
" Plug 'echasnovski/mini.snippets'
" Plug 'abeldekat/cmp-mini-snippets'

" For snippy users.
" Plug 'dcampos/nvim-snippy'
" Plug 'dcampos/cmp-snippy'
Expand All @@ -86,6 +90,12 @@ A recommended configuration can be found below.
-- require'snippy'.expand_snippet(args.body) -- For `snippy` users.
-- vim.fn["UltiSnips#Anon"](args.body) -- For `ultisnips` users.
-- vim.snippet.expand(args.body) -- For native neovim snippets (Neovim v0.10+)

-- For `mini.snippets` users:
-- local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
-- insert({ body = args.body }) -- Insert at cursor
-- cmp.resubscribe({ "TextChangedI", "TextChangedP" })
-- require("cmp.config").set_onetime({ sources = {} })
end,
},
window = {
Expand Down
6 changes: 6 additions & 0 deletions lua/cmp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ cmp.status = function()
end
end

---Ensures that cmp is the last receiver of the events specified.
---@param events string[]
cmp.resubscribe = function(events)
autocmd.resubscribe(events)
end

---@type cmp.Setup
cmp.setup = setmetatable({
global = function(c)
Expand Down
38 changes: 31 additions & 7 deletions lua/cmp/utils/autocmd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ autocmd.group = vim.api.nvim_create_augroup('___cmp___', { clear = true })

autocmd.events = {}

local function create_autocmd(event)
vim.api.nvim_create_autocmd(event, {
desc = ('nvim-cmp: autocmd: %s'):format(event),
group = autocmd.group,
callback = function()
autocmd.emit(event)
end,
})
end

---Subscribe autocmd
---@param events string|string[]
---@param callback function
Expand All @@ -16,13 +26,7 @@ autocmd.subscribe = function(events, callback)
for _, event in ipairs(events) do
if not autocmd.events[event] then
autocmd.events[event] = {}
vim.api.nvim_create_autocmd(event, {
desc = ('nvim-cmp: autocmd: %s'):format(event),
group = autocmd.group,
callback = function()
autocmd.emit(event)
end,
})
create_autocmd(event)
end
table.insert(autocmd.events[event], callback)
end
Expand Down Expand Up @@ -50,4 +54,24 @@ autocmd.emit = function(event)
end
end

---Resubscribe to events
---@param events string[]
autocmd.resubscribe = function(events)
-- Delete the autocommands if present
local found = vim.api.nvim_get_autocmds({
group = autocmd.group,
event = events,
})
for _, to_delete in ipairs(found) do
vim.api.nvim_del_autocmd(to_delete.id)
end

-- Recreate if event is known
for _, event in ipairs(events) do
if autocmd.events[event] then
create_autocmd(event)
end
end
end

return autocmd

0 comments on commit 1250990

Please sign in to comment.