Last active
July 27, 2025 16:26
-
-
Save VonHeikemen/4e7322577fbbc4e260d420e25763a414 to your computer and use it in GitHub Desktop.
enhanced native completion to expand snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- based on u/YungDaVinci work: | |
-- https://www.reddit.com/r/neovim/comments/ydlgzi/expand_lsp_snippets_with_luasnip_while_using/ | |
vim.api.nvim_create_augroup('user-snippet-expand', {}) | |
vim.api.nvim_create_autocmd('CompleteDone', { | |
group = 'user-snippet-expand', | |
desc = 'Expand LSP snippet', | |
pattern = '*', | |
callback = function(opts) | |
local comp = vim.v.completed_item | |
local item = vim.tbl_get(comp, 'user_data', 'nvim', 'lsp', 'completion_item') | |
-- check that we were given a snippet | |
if ( | |
not item | |
or not item.insertTextFormat | |
or item.insertTextFormat == 1 | |
) then | |
return | |
end | |
-- remove the inserted text | |
local cursor = vim.api.nvim_win_get_cursor(0) | |
local line = vim.api.nvim_get_current_line() | |
local lnum = cursor[1] - 1 | |
local start_char = cursor[2] - #comp.word | |
vim.api.nvim_buf_set_text(opts.buf, lnum, start_char, lnum, #line, {''}) | |
-- insert snippet | |
local snip_text = vim.tbl_get(item, 'textEdit', 'newText') or item.insertText | |
assert(snip_text, "Language server indicated it had a snippet, but no snippet text could be found!") | |
require('luasnip').lsp_expand(snip_text) | |
end | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment