saadparwaiz1/cmp_luasnip

Snippets are not reloaded after adding a new snippet

smjonas opened this issue · 8 comments

I noticed this autocommand in plugin/cmp_luasnip.lua:

autocmd User LuasnipSnippetsAdded lua require'cmp_luasnip'.refresh()

So I assumed that this causes snippets to be automatically reloaded once I add a new snippet. However, that doesn't seem to be the case.

The help docs for luasnip show this:

After snippets were lazy-loaded, the User LuasnipSnippetsAdded-event will be triggered.

There is no mention of this event being called when the snippet definitions change.

So I don't think this autocommand works as intended.

I am not even sure how to achieve this since Lua caches all modules so maybe luasnip would need to be reloaded somehow.

Have you tried doing doautocmd User LuasnipSnippetsAdded after adding your snippets? It should happen automatically for snippets loaded via "luasnip.loaders.from_{vscode,snipmate}".load, but adding snippets to the table manually doesn't trigger the event.

Thanks for the suggestion, I tried that but then require("luasnip.session").latest_load_ft seems to be nil and causes this error:

Error detected while processing User Autocommands for "LuasnipSnippetsAdded":
E5108: Error executing lua ...ite/pack/packer/opt/cmp_luasnip/lua/cmp_luasnip/init.lua:29: table index is
nil
stack traceback:
        ...ite/pack/packer/opt/cmp_luasnip/lua/cmp_luasnip/init.lua:29: in function 'refresh'
        [string ":lua"]:1: in main chunk

Ahh right, forgot about that. I added a function, "luasnip".refresh_notify(filetype) to trigger that autocmd correctly.

Thanks for your fast response! Unfortunately, I still get the same error even after upgrading Luasnip when running :doautocmd User LuasnipSnippetsAdded.

Are you calling doautocmd or the refresh_notify? :D

Ohh at first I was using doautocmd but :lua require("luasnip").refresh_notify("lua") does nothing. Are you sure it is possible with that command since I don't think Lua will automagically reload my module with the snippet definitions :D? My snippets are in a plugins/luasnip.lua module so I tried :lua package.loadedp["plugins.loaded"] = nil, then reloading it with packer but this also didn't work.

Ohh, no, that we can't do. The refresh_notify only tells cmp_luasnip (or other plugins) that snippets were added.
You could register an autocommand that :luafiles plugins/luasnip.lua to reload your snippets, but that working also depends on what you do in there (won't work if you also load() snippets).
I recommend checking out this, it's a pretty nice setup that also has reload-capability.

Alright, thanks for these tips, I will check out the link! I guess, I was just used to it from UltiSnips, but there it's easier to reload snippets on file save since a special filetype is used.