Postfix snippets from Luasnip are not expanded correctly
Make sure you have done the following
- [X] I have updated to the latest version of
blink.cmp - [X] I have read the README
Bug Description
When selecting postfix snippet it appears after TS "context".
Relevant configuration
snippets = {
expand = function(snippet)
require('luasnip').lsp_expand(snippet)
end,
active = function(filter)
if filter and filter.direction then
return require('luasnip').jumpable(filter.direction)
end
return require('luasnip').in_snippet()
end,
jump = function(direction) require('luasnip').jump(direction) end,
},
neovim version
NVIM v0.10.2 Build type: Release LuaJIT 2.1.1727870382 Run "nvim -V1 -v" for more info
blink.cmp version: branch, tag, or commit
v0.7.6
I've found the solution for postfix snippets defined with Luasnip:
keymap = {
["<C-f>"] = {
function(cmp)
vim.schedule(function()
local ls = require "luasnip"
if ls.expandable() then
ls.expand()
else
cmp.select_and_accept()
end
end)
end
}
<C-f> is used to expand a snippet. Wrap the entire thing in vim.schedule and postfix snippets are expanded correctly.
I would like to select elements without any additional keymaps. I did similar workaround already, but it is annoying that I need to remember it.
I'm not sure what you mean. Do you mind giving an example?
Sorry I though you suggested new keymap just for it. I tried your solution and it seems working partially. Still after selection of entry and pressing keymap behaviour is the same. Snipped is not being correctly expanded.
Can you please dump your entire blink.cmp config?
return {
'saghen/blink.cmp',
lazy = false, -- lazy loading handled internally
version = 'v0.*',
opts = {
keymap = {
preset = 'super-tab',
['<CR>'] = { 'accept', 'fallback' },
-- fix attempt
['<C-g>'] = { function(cmp)
vim.schedule(function()
local ls = require "luasnip"
if ls.expandable() then
ls.expand()
else
cmp.select_and_accept()
end
end)
end },
['<S-Tab>'] = { 'select_prev', 'fallback' },
['<Tab>'] = { 'select_next', 'fallback' },
['<C-n>'] = {
function(cmp)
if cmp.snippet_active() then
return cmp.accept()
else
return cmp.select_and_accept()
end
end,
'snippet_forward',
'fallback'
},
['<C-p>'] = { 'snippet_backward', 'fallback' },
},
completion = {
trigger = {
show_in_snippet = false
},
list = {
selection = "auto_insert"
}
},
documentation = {
auto_show = false,
},
appearance = {
use_nvim_cmp_as_default = true,
nerd_font_variant = 'mono'
},
sources = {
completion = {
enabled_providers = function(ctx)
-- providers func body
end
},
providers = {
-- non related providers
},
snippets = {
expand = function(snippet)
require('luasnip').lsp_expand(snippet)
end,
active = function(filter)
if filter and filter.direction then
return require('luasnip').jumpable(filter.direction)
end
return require('luasnip').in_snippet()
end,
jump = function(direction) require('luasnip').jump(direction) end,
},
},
opts_extend = { "sources.completion.enabled_providers" }
}
I would recommend updating to the latest version first. Here's the updated config:
--- @type LazySpec
return {
"saghen/blink.cmp",
dependencies = "L3MON4D3/LuaSnip",
lazy = false, -- lazy loading handled internally
-- version = 'v0.*',
---@module 'blink.cmp'
---@type blink.cmp.Config
opts = {
keymap = {
preset = "super-tab",
["<CR>"] = { "accept", "fallback" },
-- fix attempt
["<C-g>"] = {
function(cmp)
vim.schedule(function()
local ls = require "luasnip"
if ls.expandable() then
ls.expand()
else
cmp.select_and_accept()
end
end)
end,
},
["<S-Tab>"] = { "select_prev", "fallback" },
["<Tab>"] = { "select_next", "fallback" },
["<C-n>"] = {
function(cmp)
if cmp.snippet_active() then
return cmp.accept()
else
return cmp.select_and_accept()
end
end,
"snippet_forward",
"fallback",
},
["<C-p>"] = { "snippet_backward", "fallback" },
},
completion = {
trigger = {
show_in_snippet = false,
},
list = {
selection = "auto_insert",
},
},
documentation = {
auto_show = false,
},
appearance = {
use_nvim_cmp_as_default = true,
nerd_font_variant = "mono",
},
sources = {
completion = {
default = { "lsp", "buffer", "path", "luasnip" },
},
providers = {
-- non related providers
},
snippets = {
expand = function(snippet)
require("luasnip").lsp_expand(snippet)
end,
active = function(filter)
if filter and filter.direction then
return require("luasnip").jumpable(filter.direction)
end
return require("luasnip").in_snippet()
end,
jump = function(direction)
require("luasnip").jump(direction)
end,
},
},
opts_extend = { "sources.default" },
},
}
I made blink.cmp depend on luasnip and commented out the explicitly specified version plus added the new sources.default key in place of enabled_providers.
After updating the config, I would recommend that you delete blink through Lazy by running :Lazy, selecting blink and pressing x. Then blink will appear at the top under 'Not installed'. Hover your cursor over blink.cmp then press i to install and then it should work. If it doesn't, I would recommend installing from source by adding build = "cargo build --release" to instruct Lazy to build blink.cmp from source. Let me know how it goes!
Hey, I followed your steps, adjusted the configuration and dived deep into testing. I found out couple of things:
- Your work around works partially. It can not be assign to
<CR>andfallbacklogic is not working since it is wrapped invim.schedule - When
completion.list.selectionis set toauto_selectthen additionaldotis being added before snippet while picking option. It might be the reason why it is not working correctly in general. - When on
manualorpreselectmode the normalacceptworks partially, leaving thedotat the end of capture.
For <CR> I have the following defined, which expands snippets as expected on my setup:
["<cr>"] = {
function(cmp)
if cmp.is_visible() then return cmp.accept() end
end,
"fallback",
},
When completion.list.selection is set to auto_select then additional dot is being added before snippet while picking option. It might be the reason why it is not working correctly in general.
When on manual or preselect mode the normal accept works partially, leaving the dot at the end of capture.
I'm not experiencing those issues at all, so unfortunately I'm unable to provide hints there.
Postfix snippets seem to be working as expected now in my testing. Let me know if that isn't the case and I'll re-open