-
I'm looking for advice on a snippet I'm trying to write. I trying to get better at writing luasnip snippets so any advice is appreciated. I'm basically trying to write a small snippet for adding more plugins to my neovim config, which usually has something of the form: use { -- LuaSnip {{{
'L3MON4D3/LuaSnip',
setup = 'require("plugin_settings.luasnip").setup()',
config = 'require("plugin_settings.luasnip").config()',
} -- }}} so I started with the following: s('u', fmt([[
use {{ -- {name} {{{{{{
'{plugin}',
setup = 'require("plugin_settings.{module}").setup()',
config = 'require("plugin_settings.{module}").config()',
}} -- }}}}}}]], {
plugin = i(1),
name = d(2, function(plugin)
plugin = plugin[1][1]
local name = plugin:gsub('.*/', ''):gsub('nvim%-', '')
return sn(nil, t(name))
end, {1}),
module = f(function(name)
name = name[1][1]
return name:gsub([[%.lua]], '')
end, {2}),
})) which works pretty well. Then I thought I wanted to make the s('u', c(1, {
fmt([[
use {{ -- {name} {{{{{{
'{plugin}',
}} -- }}}}}}]], {
plugin = i(1),
name = d(2, function(plugin)
plugin = plugin[1][1]
local name = plugin:gsub('.*/', ''):gsub('nvim%-', '')
return sn(nil, t(name))
end, {1}),
}),
fmt([[
use {{ -- {name} {{{{{{
'{plugin}',
setup = 'require("plugin_settings.{module}").setup()',
config = 'require("plugin_settings.{module}").config()',
}} -- }}}}}}]], {
plugin = i(1),
name = d(2, function(plugin)
plugin = plugin[1][1]
local name = plugin:gsub('.*/', ''):gsub('nvim%-', '')
return sn(nil, t(name))
end, {1}),
module = f(function(name)
name = name[1][1]
return name:gsub([[%.lua]], '')
end, {2}),
}),
})) but this has two disadvantages:
I was thinking it there could be a way to have an internal choice node between nothing and the s('u', fmt([[
use {{ -- {name} {{{{{{
'{plugin}',
{settings}}} -- }}}}}}]], {
plugin = i(1),
name = d(2, function(plugin)
plugin = plugin[1][1]
local name = plugin:gsub('.*/', ''):gsub('nvim%-', '')
return sn(nil, t(name))
end, {1}),
settings = c(3, {
t(''),
fmt([[
setup = 'require("plugin_settings.{module}").setup()',
config = 'require("plugin_settings.{module}").config()',
]], {
module = f(function(name)
name = name[1][1]
return name:gsub([[%.lua]], '')
end, {2}),
}),
}),
})) But then one needs to jump to the choice node to change it and also seems to only be possible to change once. Let me know if these types of discussion points don't make sense here. I'm mostly looking for some advice from more experienced snippet writers :) |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
Ouh, here you could use I can't tell right away why changeChoice won't work the second time in either snippet, that's a bit weird. Another way: use this to generate the entire snippet in a function, then you could use the keybinds shown in the wiki to cycle between different configurations, with minimal duplication. |
Beta Was this translation helpful? Give feedback.
-
Oh, I think posts like this are quite useful, as long as they are also accessible to other users, might serve as inspiration for more complicated snippets :D |
Beta Was this translation helpful? Give feedback.
Ouh, here you could use
restoreNode
, it allows you to keep some part of the snippet across changing choice and re-executing adynamicNode
.I can't tell right away why changeChoice won't work the second time in either snippet, that's a bit weird.
Another way: use this to generate the entire snippet in a function, then you could use the keybinds shown in the wiki to cycle between different configurations, with minimal duplication.