-
-
Notifications
You must be signed in to change notification settings - Fork 245
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
[Question] dynamic nodes - create nodes at later position/use nesting #328
Comments
First of all, good job with the images, they nicely illustrate what you mean 👍 The issue with the snippet in its' current form is just that the initial choice for c(2,{
sn(nil,{
i(1), t("):")
}),
-- or just t("):"), a single textNode is actually detected and stopped at, but this doesn't work for snippetNodes.
d(nil,py_init)
}), (This exact problem has come up a few times already, I'll mention it in the Doc).
Yes! I think this is a really nice application for the DynamicNode with user Input, in this case you'd control not the number of rows in a table, but the number of arguments for the function.
Thank you :D |
Not quite sure why I even used a So this is what I came up with up to now: local function py_init()
return
sn(nil, {
t(", "),
i(1,"arg"),
c(2,{
t({"):"}),
d(nil,py_init)
}),
t({"", ""}),
t{"\tself."},
d(3, function(args,snip,uarg) return i(nil, args[1][1]) end, {1}),
t{" = "},
rep(1),
})
end s("test", fmt(
[[def __init__(self{}]],
{
c(1, {t({"):", "\tpass", ""}), d(nil, py_init, {})})
}
)),
Still I think your hint about DynamicNode with user Input looks promising and I'll check it out (but it's quite some code so I guess this will take time to wrap my head around). [I'll keep this issue open for that time if this is ok with you]
So thanks for the (fast) help |
Nice, that looks pretty good already 👍
Yup, please do, I don't mind relevant issues being open longer
Hah, thank you :D |
Few notes
And maybe a small note that
One possibility maybe would be using a big input node holding the whole argument list. This wouldn't require external updating as far as I thought it through (except if one wants restore nodes I guess). The issue I have with this is that this way if jumping back to the input node with the argument list, the whole argument list is marked (in select mode) and I don't know how to only edit a portion of it (if e.g. only the name of one argument should be changed or one argument should be removed). (This is why I aimed at having different input nodes for the different arguments in the first place) |
Thank you, good points👍
I don't really get what you mean with the infinite argument list, I hope I'm not misunderstanding something :D
Thats how the Javadoc-snippet works, and true, it's not really optimal. |
Oh, nice, that looks pretty much perfect 👍 Just for the record, here's what I had in mind with local pyinit = function(_, parent)
-- this could also be outside the dynamicNode.
local nodes = {t"def __init__(self"}
-- snip.argc is controlled via c-t/c-g
local argc = parent.argc
-- snip.argc is not set on the first call.
if not argc then
parent.argc = 1
argc = 1
end
-- store jump_indx separately and increase for each insertNode.
local jump_indx = 1
-- generate args
for _ = 1, argc do
vim.list_extend(nodes, {t", ", i(jump_indx, "arg"..jump_indx)})
jump_indx = jump_indx + 1
end
nodes[#nodes + 1] = t{")", ""}
-- generate assignments
for j = 1, argc do
vim.list_extend(nodes, {
t"\t self.",
i(jump_indx, "arg"..j),
t" = ",
-- repeat argj
rep(j),
t{"", ""}})
jump_indx = jump_indx + 1
end
-- remove last linebreak.
nodes[#nodes] = nil
return sn(nil, nodes)
end
...
s("pyinit", d(1, pyinit, {},
function(parent) parent.argc = parent.argc + 1 end,
-- Don't drop below one arg. If 0 args should be accepted too, pyinit needs
-- to be extended so it has at least one insertNode from which the
-- dynamicNode may be updated via c-t/c-g.
function(parent) parent.argc = math.max(parent.argc - 1, 1) end )) 1646898950.mp4(Do note that this code will break the next time luasnip is updated, I'll push some breaking changes concerning the dynamicNode-user_args soon. |
One more idea: using s("pyinit", d(1, pyinit, {},
function(parent)
vim.ui.input(
{prompt = "Number of args: "},
function(argc)
parent.argc = math.max(argc, 1)
end)
end
)) |
What do you mean with scrolling? |
Ah, that was not very precise 😅 |
Ah yes right. So now we've got four solutions
Before doing the list I thought that 2. would be nice, but now after thinking about it again maybe 4. is the nicest one ^^ |
Ah, the way it's set up now you'd have to use one key for 4, the function that queries for s({trig="pyinit(%d+)", regTrig=true}, d(1, pyinit, {}))
...
-- in pyinit:
local argc = tonumber(parent.captures[1])
... and then trigger it via |
Ah right, I missed the point that the ui function is the argument to the |
So I think for me this is solved (I was only looking for one solution (and then understanding the latex table snippet), now I've got four xD). Thanks a lot for explaining and suggesting various ways to solve this 👍 |
😆
The solutions definitely shouldn't be left buried in some issue :D |
Yes, of course. Good idea (EDIT: I'll do that ^^) |
Nice, thank you 😄 |
Done. If there is something with the section (too bad documented or something else) just let me know. And again thanks for helping 👍 |
No problem, thanks for extending the wiki :D |
Hi,
I just got into reading the docs/manual and while porting some of my snippets, I stumbled over this.
Is is possible to use dynamic nodes for infinite new nodes but for each node, a new node at another position is created. Sounds probably confusing, so let me illustrate this with my current snippet as an example.
So at first, the snippet expanded to
now by toggling the choice node, it inserts a node for inserting the first argument
after that the user can decide (probably by choice node) there should be an additional argument thus
is created.
After finishing the argument list, there should be nodes to change the variable names (
self.arg
) to which the arguments are assigned to.I already played around a bit with
and
which should give a tokenization like this
(wrong order of the arguments, but I'd be able to live with that). But I'm stuck at the
choice node
in mypy_init
function, after inserting the first argument I can't switch the choice (otherwise I get back to the 0 argument version) andtab
jumps out of the whole snippet (at the end of the snippet and of course switching choice there doesn't work).Note: the
init
template is missing theself
parameter for valid python and theself.arg
text is no insert node yet.Now I see this issue is quite similar to the latex
itemize
and thejdocsnip
functionality provided in the examples. But I don't get the issue with my snippet (the inability to switch choice).And I'm interested in if this approach can be improved as having to insert the delimiting text (
):\n
) after the last argument is unpleasant since it commands how the recursive calls should be nested, and thus the ordering of the arguments).[The separation of where the nodes are created (here: in the argument list) and where additional new nodes are inserted (here: the function body) without the need to pass the content between these two places on to the last recursive call was the basic idea/question of this issue]
I know this is quite a huge question so take your time and remember you're doing this voluntarily so don't feel in the need to help (even though of course I'd appreciate it)
The text was updated successfully, but these errors were encountered: