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

Multilanguage Snippets file not being loaded #1234

Open
ccolorado opened this issue Sep 19, 2024 · 2 comments
Open

Multilanguage Snippets file not being loaded #1234

ccolorado opened this issue Sep 19, 2024 · 2 comments

Comments

@ccolorado
Copy link

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
Copy link
Owner

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
Copy link
Author

Thanks for the explanation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants