L3MON4D3 / LuaSnip

Snippet Engine for Neovim written in Lua.
Apache License 2.0
3.5k stars 245 forks source link

Multilanguage Snippets file not being loaded #1234

Open ccolorado opened 2 months ago

ccolorado commented 2 months ago

I created two versions of console.log snippets, since they are compatible across multiple languages I tried combining them into a single file. However doesn't seem to get loaded.

Snippet only seems to work when the snippet is included on a file named after the language e.g. "custom/snippets/javascript.lua"

I couldn't find on the docs a reference to the filenames with the filetypes so perhaps it is a bug ?

local ls = require 'luasnip'

ls.add_snippets({ 'typescript', 'javascript' }, {
  -- Console log variable
  ls.snippet('lv', {
    ls.text_node { 'console.log(' }, ...

Loading: https://github.com/ccolorado/kickstart.nvim/blob/536cb9c312b18013c439d8f435b9e0b5fdfcc4c4/init.lua#L724

Snippets directory: https://github.com/ccolorado/kickstart.nvim/tree/multilang_snippets/lua/custom/snippets

L3MON4D3 commented 2 months ago

Hi :)

Snippet only seems to work when the snippet is included on a file named after the language e.g. "custom/snippets/javascript.lua"

Jup, that is as it should be, the lua-loader associates the snippets loaded from some file only with the filetype given by the filename.

One way to use some collection of snippets in the same filetypes is ls.filetype_extend, you could for example do ls.filetype_extend("javascript", {"jslikes"}) and ls.filetype_extend("typescript", {"jslikes"}) and then write the snippets into a file custom/snippets/jslikes.lua

Another idea would be the multisnippet, where you could use the filetype-key to add one snippet to all these filetypes: ms({{filetype="javascript"}, {filetype="typescript"}, common={trig="lv"}}, {ls.text_node ...}) and return this snippet in custom/snippets/all.lua so that it is loaded for all possible filetypes you may use (this comes with the tiny disadvantage/uncleanness that the snippet is processed even if you don't edit a type/javascript file)

Hope that helped, feel free to ask more about either option, it's possible I made some mistake in the code I gave, I didn't test it :)

ccolorado commented 2 months ago

Thanks for the explanation.