Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cmp-nvim-lsp triggers completion when it should not #7

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lua/cmp_mini_snippets/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,25 @@ end

-- Remove the word inserted by nvim-cmp and insert snippet
-- It's safe to assume that mode is insert during completion
local function insert_snippet(snip, word)
local insert_snippet = vim.schedule_wrap(function(snip, word)
local cursor = vim.api.nvim_win_get_cursor(0)
cursor[1] = cursor[1] - 1 -- nvim_buf_set_text: line is zero based
local start_col = cursor[2] - #word
vim.api.nvim_buf_set_text(0, cursor[1], start_col, cursor[1], cursor[2], {})

local insert = MiniSnippets.config.expand.insert or MiniSnippets.default_insert
insert({ body = snip.body }) -- insert at cursor
end
end)

---Executed after the item was selected.
---@param completion_item lsp.CompletionItem
---@param callback fun(completion_item: lsp.CompletionItem|nil)
function source:execute(completion_item, callback)
insert_snippet(completion_item.data.snip, completion_item.word)
callback(completion_item)

-- After callback, scheduled
-- Sometimes cmp-nvim-lsp kicks in when it should not(#6)
insert_snippet(completion_item.data.snip, completion_item.word)
end

return source