Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
176420f
implement optional argnodes.
L3MON4D3 Dec 28, 2023
bd09791
implement subtree_do, invokes callbacks on tree of nodes (in a snippet).
L3MON4D3 Mar 4, 2024
5fdf9d8
make resolve_position work for static snippets.
L3MON4D3 Mar 4, 2024
aa1b233
fix: make root_path work for snippets.
L3MON4D3 Mar 4, 2024
b5d2a34
implement subtree_leave_entered, for leaving only entered nodes.
L3MON4D3 May 5, 2025
b1f3492
overhaul snippet-updates.
L3MON4D3 Nov 3, 2025
b419d9a
make sure visible is set on -1-node.
L3MON4D3 Mar 10, 2024
83c7649
propery remove child-snippets when `:exit`ing.
L3MON4D3 Apr 23, 2024
61bf611
exitNode: use same update_dependents as all other nodes.
L3MON4D3 Apr 23, 2024
c295b3f
update after snip_expand.
L3MON4D3 Oct 14, 2025
719d55c
Format with stylua
L3MON4D3 Oct 14, 2025
09217d6
Make insertNode correctly handle static_text if it's a snippetString.
L3MON4D3 Oct 28, 2024
3be6031
allow using snippet_string as dynamicNode-args.
L3MON4D3 Oct 28, 2024
a35adb8
restoreNode,insertNode: propagate store.
L3MON4D3 Oct 29, 2024
d4abde8
dynamicNode.update: store snippet before evaluating fn.
L3MON4D3 Oct 29, 2024
4ca232e
dynamicNode.update: copy extmarks after focusing.
L3MON4D3 Oct 29, 2024
3225dd1
dynamicNode.update: do update_restore instead of update.
L3MON4D3 Oct 29, 2024
45df41e
restoreNode: don't store on exit, store should have been called before.
L3MON4D3 Oct 29, 2024
888ac5e
add some tests for new restoreNode-behaviour.
L3MON4D3 Oct 29, 2024
722251d
choiceNode: correctly refocus when current_node is in another snippet.
L3MON4D3 Oct 29, 2024
7d8140c
we don't want to go into adjacent snippetNodes, but land between them.
L3MON4D3 Oct 29, 2024
5441143
snippet: correctly propagate exit to child_snippets (and clear them).
L3MON4D3 Oct 29, 2024
b63fb1f
add another test for the new restoreNode.
L3MON4D3 Oct 29, 2024
d4b8110
store content of nested snippets before capturing argnode.
L3MON4D3 Oct 29, 2024
508cc3f
make sure marks are invalidated even for nested snippets.
L3MON4D3 Oct 29, 2024
d40fc8a
get_args: `store` only when calling in static mode.
L3MON4D3 Oct 29, 2024
5192bf9
snippetstring: store strings as \n-separated string.
L3MON4D3 Oct 14, 2025
87ad057
small refactor.
L3MON4D3 Oct 30, 2024
fc7e586
implement a few simple string-operations on snippetString.
L3MON4D3 Oct 30, 2024
c5cfc00
fix flakiness in test.
L3MON4D3 Oct 30, 2024
3f7ce76
update: try to find new active node in child-snippet.
L3MON4D3 Oct 30, 2024
befdc53
allow replacing parts of a snippetString with other text.
L3MON4D3 Nov 2, 2024
6ee05ef
implement gsub on snippetString.
L3MON4D3 Nov 2, 2024
de371f5
snippetstring.replace: fix substitution in textNode.
L3MON4D3 Nov 2, 2024
7c34402
fix switchup.
L3MON4D3 Nov 2, 2024
b9313e2
make in-place modifying functions private.
L3MON4D3 Nov 2, 2024
80be0c4
add :sub to snippetString.
L3MON4D3 Nov 2, 2024
bea1bb7
add `opt` for the optional argument.
L3MON4D3 Nov 3, 2024
5f476e4
correctly store+restore visual selection during update.
L3MON4D3 Nov 3, 2024
24edd78
fNode: always store result in static_text.
L3MON4D3 Nov 4, 2024
4d25da0
update_dependents: get cursor-position after queried movements.
L3MON4D3 Nov 4, 2024
25ef218
move the jump_active-check into the autocommand.
L3MON4D3 Oct 14, 2025
79b336c
optionally update a node differnt from the current node.
L3MON4D3 Oct 14, 2025
e0ee698
update_dependents: use update_restore by default.
L3MON4D3 Nov 4, 2024
79722c5
choiceNode: call update_dependents after routine is done completely.
L3MON4D3 Nov 4, 2024
67b99dc
move no_region_wrap back into main-module.
L3MON4D3 Oct 14, 2025
fcfb058
dynamicNode/restoreNode: don't destroy snip on exit.
L3MON4D3 Nov 4, 2024
5123fde
handle selection on first line and column of buffer with `before`.
L3MON4D3 Nov 4, 2024
ad85601
document imperfect behaviour asserted by test.
L3MON4D3 Nov 4, 2024
482f0bf
export optional_arg as opt for tests.
L3MON4D3 Nov 4, 2024
d96028f
set jump_active=false ASAP.
L3MON4D3 Nov 4, 2024
28c1300
choiceNode: explicitly set parent and pos for choices.
L3MON4D3 Nov 6, 2024
a8e340c
fix(dynamicNode): don't access .snip in update_static.
L3MON4D3 Nov 6, 2024
98ed158
dynamicNode: optionally use .snip to generate docstring.
L3MON4D3 Nov 6, 2024
acec69f
enqueue cursor-movement due to update in typeahead.
L3MON4D3 Nov 6, 2024
2c5300f
get_args: do (static_)visible-check in get_args, not get_static_text.
L3MON4D3 Nov 6, 2024
d69df12
test docstring-generation with self-dependent dynamicNode.
L3MON4D3 Nov 6, 2024
24b925e
properly restore cursor-position in set_choice.
L3MON4D3 Nov 7, 2024
d3a7709
change_choice: use cursor-restore system from update_dependents.
L3MON4D3 Oct 14, 2025
37b963a
snippet_string: add metadata and marks.
L3MON4D3 Nov 13, 2024
eed721c
store cursor-position in snippetString to more accurately restore it.
L3MON4D3 Oct 14, 2025
6bef3b3
api_enter: only log an error when called recursively.
L3MON4D3 Nov 14, 2024
77c5de4
feedkeys: ignore errors on asynchronous nvim_win_set_cursor.
L3MON4D3 Nov 14, 2024
4be52be
correctly restore self-dependent dynamicNode.
L3MON4D3 Nov 14, 2024
26d21ae
change/set/select_choice: update current node before modifying choice.
L3MON4D3 Oct 14, 2025
29c1ee2
add a few tests for previous changes.
L3MON4D3 Nov 14, 2024
a4dd915
Format with stylua
L3MON4D3 Oct 14, 2025
dcdf2ae
fix: pass correct arguments to str_byteindex.
L3MON4D3 May 5, 2025
02fd641
fix: handle unicode->snippetstring->unicode conversion correctly.
L3MON4D3 May 5, 2025
38c55fe
add a few annotations.
L3MON4D3 Oct 14, 2025
9e1d258
feedkeys: only clear action after its confirm is called.
L3MON4D3 May 16, 2025
b525bd3
store: check that child-snip has valid extmarks before storing.
L3MON4D3 Jun 3, 2025
ab81681
improve logging of dynamicNode.
L3MON4D3 Oct 19, 2025
5c2225e
dynamicNode: limit number of nested updates.
L3MON4D3 Oct 19, 2025
42fdd36
rip out update_depth again.
L3MON4D3 Oct 19, 2025
f5bd74d
add brief documentation for new features.
L3MON4D3 Nov 2, 2025
31c9e2b
Format with stylua
L3MON4D3 Nov 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add brief documentation for new features.
  • Loading branch information
L3MON4D3 committed Nov 3, 2025
commit f5bd74dcd79190cff5fe3ca1d4ffa2031d33fb8c
112 changes: 109 additions & 3 deletions DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local opt = require("luasnip.nodes.optional_arg")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
Expand Down Expand Up @@ -937,8 +938,10 @@ user input.
the dynamicNode's place.
`args`, `parent` and `user_args` are also explained in
[FunctionNode](#functionnode)
- `args`: `table of text` (`{{"node1line1", "node1line2"}, {"node2line1"}}`)
from nodes the `dynamicNode` depends on.
- `args`: `table of text` (`snippetstring_args == false`) or `snippetString`
(otherwise). Represents content of the nodes this dynamicNode depends on.
Their index in the `node_references`-list determines the position in
this list.
- `parent`: the immediate parent of the `dynamicNode`.
- `old_state`: a user-defined table. This table may contain anything; its
intended usage is to preserve information from the previously generated
Expand All @@ -957,8 +960,10 @@ user input.
inserted at the `dynamicNode`s place.
(`dynamicNode` behaves exactly the same as `functionNode` in this regard).

- `opts`: In addition to the common [Node](#node)-keys, there is, again,
- `opts`: In addition to the common [Node](#node)-keys, there is:
- `user_args`, which is described in [FunctionNode](#functionnode).
- `snippetstring_args`, boolean: If set, `function` receives a list of
[Snippetstring](#snippetstring) instead of `string[]`. `false` by default.

**Examples**:

Expand Down Expand Up @@ -1024,6 +1029,70 @@ ls.add_snippets("all",
As with `functionNode`, `user_args` can be used to reuse similar `dynamicNode`-
functions.

## Self-dependent DynamicNode

While the previous examples only showed dynamicNodes that depend on nodes
outside of itself, it's possible to update a dynamicNode in response to a change
to a node within it:

```lua
ls.snip_expand(s("trig", {
d(1, function(args)
if not args[1] then
-- the arg does not exist -> provide a default.
return sn(nil, {i(1, "asdf", {key = "ins"})})
else
-- This branch is only take after the dynamicNode was updated once.
-- Now we can perform the actual "task" of this dynamicNode:
-- replacing all occurences of "a" with "e".
return sn(nil, {i(1, args[1]:gsub("a", "e"), {key = "ins"})})
end
end, {opt(k("ins"))}, { snippetstring_args = true })
}))
```

The example above shows a dynamicNode that will substitute all occurrences of
"a" within it with an "e". Important for this to work are:
* Use an [Optional Noderef](#optional-noderef) `opt(k("ins"))` to reference the
generated insertNode that will eventually update the dynamicNode on changes:
A dynamicNode does not update when one of its argnodes is missing, so in
order to get anything out of the node, we need `opt`.
* Set `snippetstring_args`: This ensures that snippets expanded inside the
dynamicNode are preserved during the update! While not completely necessary,
this is a good idea because it can be annoying to lose jump-points
only because the snippet they belong is inside such a self-dependent
dynamicNode.

When using these self-dependent dynamicNodes it is a really good idea to give
nodes that can cause an update some unique key, or make sure that they are
inside of a restoreNode. If this is not done, LuaSnip may not find a node that
is equivalent to the one that contained the cursor before the update, and thus
may have to jump into the dynamicNode anew, which may be unexpected.

Another danger is in accidentally constructing an infinite loop of updates.
Right now, LuaSnip is very indiscriminate in updating dynamicNodes, and if given
a snippet like

```lua
s("srep", {
d(1, function(args)
if not args[1] then
return sn(nil, {i(1, "sdf", {key = "ins"})})
else
return sn(nil, {i(1, args[1]:gsub("a", "aa"), {key = "ins"})})
end
end, {opt(k("ins"))}, {snippetstring_args = true})
}),
```

any "a" is replaced with "aa", ad infinitum (or until the process is killed,
which is likely soon).

These infinite loops may also be caused by nested self-dependent dynamicNodes,
think one that replaces "a" with "e", and another that changes "e" back to "a",
so it's best to build in some kind of safeguard into the generating function.
One could for example skip the update once the argnode exceeds a certain length.

# RestoreNode

This node can store and restore a snippetNode as is. This includes changed
Expand Down Expand Up @@ -1121,6 +1190,20 @@ that really bothers you feel free to open an issue.

<!-- panvimdoc-ignore-end -->

# Snippetstring

Snippetstrings can store the content of an insertNode as-is. This includes both
the regular text, and expanded snippets. The primary purpose of snippetstrings
is to facilitate easy modifications of text in dynamicNode, while preserving
snippets and cursor-positions.

A snippetstring supports the string-functions `lower`, `upper`, `gsub` and
`sub`, and the `..`-metamethod. While `lower`, `upper`, and `..` will always
preserve snippets inside the snippetstring, `sub` will replace partially
contained snippets with their text, while `gsub` will do so if a replacement
crosses a node-boundary.


# Key Indexer

A very flexible way of referencing nodes ([Node Reference](#node-reference)).
Expand Down Expand Up @@ -1178,6 +1261,29 @@ s("trig", {
<!-- panvimdoc-ignore-end -->


# Optional Noderef

`opt` wraps another node-reference and makes the wrapped node not strictly
required for performing an update. This means, amongs other things, that a
`dynamicNode`s can be updated by nodes within it:

```lua
ls.snip_expand(s("trig", {
d(1, function(args)
if not args[1] then
-- the arg does not exist -> provide a default.
return sn(nil, {i(1, "asdf", {key = "ins"})})
else
-- This branch is only take after the dynamicNode was updated once.
-- Now we can perform the actual "task" of this dynamicNode:
-- replacing all occurences of "a" with "e".
return sn(nil, {i(1, args[1][1]:gsub("a", "e"), {key = "ins"})})
end
end, {opt(k("ins"))})
}))
```


# Absolute Indexer

`absolute_indexer` allows accessing nodes by their unique jump-index path from
Expand Down
112 changes: 109 additions & 3 deletions data/DOC-template.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ local d = ls.dynamic_node
local r = ls.restore_node
local events = require("luasnip.util.events")
local ai = require("luasnip.nodes.absolute_indexer")
local opt = require("luasnip.nodes.optional_arg")
local extras = require("luasnip.extras")
local l = extras.lambda
local rep = extras.rep
Expand Down Expand Up @@ -902,8 +903,10 @@ user input.
the dynamicNode's place.
`args`, `parent` and `user_args` are also explained in
[FunctionNode](#functionnode)
- `args`: `table of text` (`{{"node1line1", "node1line2"}, {"node2line1"}}`)
from nodes the `dynamicNode` depends on.
- `args`: `table of text` (`snippetstring_args == false`) or `snippetString`
(otherwise). Represents content of the nodes this dynamicNode depends on.
Their index in the `node_references`-list determines the position in
this list.
- `parent`: the immediate parent of the `dynamicNode`.
- `old_state`: a user-defined table. This table may contain anything; its
intended usage is to preserve information from the previously generated
Expand All @@ -922,8 +925,10 @@ user input.
inserted at the `dynamicNode`s place.
(`dynamicNode` behaves exactly the same as `functionNode` in this regard).

- `opts`: In addition to the common [Node](#node)-keys, there is, again,
- `opts`: In addition to the common [Node](#node)-keys, there is:
- `user_args`, which is described in [FunctionNode](#functionnode).
- `snippetstring_args`, boolean: If set, `function` receives a list of
[Snippetstring](#snippetstring) instead of `string[]`. `false` by default.

**Examples**:

Expand Down Expand Up @@ -989,6 +994,70 @@ ls.add_snippets("all",
As with `functionNode`, `user_args` can be used to reuse similar `dynamicNode`-
functions.

## Self-dependent DynamicNode

While the previous examples only showed dynamicNodes that depend on nodes
outside of itself, it's possible to update a dynamicNode in response to a change
to a node within it:

```lua
ls.snip_expand(s("trig", {
d(1, function(args)
if not args[1] then
-- the arg does not exist -> provide a default.
return sn(nil, {i(1, "asdf", {key = "ins"})})
else
-- This branch is only take after the dynamicNode was updated once.
-- Now we can perform the actual "task" of this dynamicNode:
-- replacing all occurences of "a" with "e".
return sn(nil, {i(1, args[1]:gsub("a", "e"), {key = "ins"})})
end
end, {opt(k("ins"))}, { snippetstring_args = true })
}))
```

The example above shows a dynamicNode that will substitute all occurrences of
"a" within it with an "e". Important for this to work are:
* Use an [Optional Noderef](#optional-noderef) `opt(k("ins"))` to reference the
generated insertNode that will eventually update the dynamicNode on changes:
A dynamicNode does not update when one of its argnodes is missing, so in
order to get anything out of the node, we need `opt`.
* Set `snippetstring_args`: This ensures that snippets expanded inside the
dynamicNode are preserved during the update! While not completely necessary,
this is a good idea because it can be annoying to lose jump-points
only because the snippet they belong is inside such a self-dependent
dynamicNode.

When using these self-dependent dynamicNodes it is a really good idea to give
nodes that can cause an update some unique key, or make sure that they are
inside of a restoreNode. If this is not done, LuaSnip may not find a node that
is equivalent to the one that contained the cursor before the update, and thus
may have to jump into the dynamicNode anew, which may be unexpected.

Another danger is in accidentally constructing an infinite loop of updates.
Right now, LuaSnip is very indiscriminate in updating dynamicNodes, and if given
a snippet like

```lua
s("srep", {
d(1, function(args)
if not args[1] then
return sn(nil, {i(1, "sdf", {key = "ins"})})
else
return sn(nil, {i(1, args[1]:gsub("a", "aa"), {key = "ins"})})
end
end, {opt(k("ins"))}, {snippetstring_args = true})
}),
```

any "a" is replaced with "aa", ad infinitum (or until the process is killed,
which is likely soon).

These infinite loops may also be caused by nested self-dependent dynamicNodes,
think one that replaces "a" with "e", and another that changes "e" back to "a",
so it's best to build in some kind of safeguard into the generating function.
One could for example skip the update once the argnode exceeds a certain length.

# RestoreNode

This node can store and restore a snippetNode as is. This includes changed
Expand Down Expand Up @@ -1086,6 +1155,20 @@ that really bothers you feel free to open an issue.

<!-- panvimdoc-ignore-end -->

# Snippetstring

Snippetstrings can store the content of an insertNode as-is. This includes both
the regular text, and expanded snippets. The primary purpose of snippetstrings
is to facilitate easy modifications of text in dynamicNode, while preserving
snippets and cursor-positions.

A snippetstring supports the string-functions `lower`, `upper`, `gsub` and
`sub`, and the `..`-metamethod. While `lower`, `upper`, and `..` will always
preserve snippets inside the snippetstring, `sub` will replace partially
contained snippets with their text, while `gsub` will do so if a replacement
crosses a node-boundary.


# Key Indexer

A very flexible way of referencing nodes ([Node Reference](#node-reference)).
Expand Down Expand Up @@ -1143,6 +1226,29 @@ s("trig", {
<!-- panvimdoc-ignore-end -->


# Optional Noderef

`opt` wraps another node-reference and makes the wrapped node not strictly
required for performing an update. This means, amongs other things, that a
`dynamicNode`s can be updated by nodes within it:

```lua
ls.snip_expand(s("trig", {
d(1, function(args)
if not args[1] then
-- the arg does not exist -> provide a default.
return sn(nil, {i(1, "asdf", {key = "ins"})})
else
-- This branch is only take after the dynamicNode was updated once.
-- Now we can perform the actual "task" of this dynamicNode:
-- replacing all occurences of "a" with "e".
return sn(nil, {i(1, args[1][1]:gsub("a", "e"), {key = "ins"})})
end
end, {opt(k("ins"))})
}))
```


# Absolute Indexer

`absolute_indexer` allows accessing nodes by their unique jump-index path from
Expand Down
Loading
Loading