-
I'm trying to follow https://github.com/L3MON4D3/LuaSnip/wiki/Misc#dynamicnode-with-user-input to create an expansible snippet. Here is what I have local ls = require("luasnip")
local c = ls.choice_node
local d = ls.dynamic_node
local f = ls.function_node
local i = ls.insert_node
local r = ls.restore_node
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
local fmt = function(format, args, opts)
opts = vim.tbl_extend("force", { repeat_duplicates = true }, opts or {})
return require("luasnip.extras.fmt").fmt(format, args, opts)
end
local lambda = require("luasnip.extras").l
---@diagnostic disable-next-line: unused-local
local rp = require("luasnip.extras").rep
local postfix = require("luasnip.extras.postfix").postfix
vim.keymap.set(
{ "i", "s" },
"<C-e>",
function()
local current_node = ls.session.current_nodes[vim.api.nvim_get_current_buf()]
local dynamic_node = current_node
while not dynamic_node.dynamicNode do
dynamic_node = dynamic_node.parent
end
dynamic_node = dynamic_node.dynamicNode
local func = dynamic_node.user_args.callback
if func then func() end
dynamic_node.last_args = nil
dynamic_node:update()
end
)
ls.add_snippets("text", {
s(
"recurse",
fmt(
[[
command({})
]],
{
d(1, function(_, _, old_state, _)
local nodes = old_state and old_state.nodes or {}
local pos = #nodes + 1
table.insert(nodes, i(pos, pos == 1 and "here" or ", here"))
local node = sn(nil, nodes)
node.old_state = { nodes = nodes }
return node
end, nil, { user_args = { callback = function() end } }),
}
)
),
}) I stripped the out the part that's responsible for restoring mode and cursor position since I haven't gotten to it. I would like to make the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 10 replies
-
Oh, I think adding a node to multiple snippets, like that function is doing via Better use |
Beta Was this translation helpful? Give feedback.
awesome, so the minial snippet to have expand dynamic node with user input is