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
change/set/select_choice: update current node before modifying choice.
If we don't do this, the content of a choiceNode may not be restored
correctly.
(or, it will be restored correctly, but it won't be what the user saw
before they called change/set/select_choice, which seems suboptimal).
  • Loading branch information
L3MON4D3 committed Nov 3, 2025
commit 26d21ae21bfb8fa5ab6f56fdd6e745ed1df83108
31 changes: 22 additions & 9 deletions lua/luasnip/extras/select_choice.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local session = require("luasnip.session")
local ls = require("luasnip")
local node_util = require("luasnip.nodes.util")
local feedkeys = require("luasnip.util.feedkeys")

-- in this procedure, make sure that api_leave is called before
-- set_choice_callback exits.
Expand All @@ -10,9 +11,8 @@ local function set_choice_callback(data)
ls._api_leave()
return
end
-- feed+immediately execute i to enter INSERT after vim.ui.input closes.
-- vim.api.nvim_feedkeys("i", "x", false)
ls._set_choice(indx, {cursor_restore_data = data})
-- set_choice restores cursor from before.
ls._set_choice(indx, {cursor_restore_data = data, skip_update = true})
ls._api_leave()
end
end
Expand All @@ -25,12 +25,25 @@ local function select_choice()
local active = session.current_nodes[vim.api.nvim_get_current_buf()]

ls._api_enter()
local restore_data = node_util.store_cursor_node_relative(active, {place_cursor_mark = false})
vim.ui.select(
ls.get_current_choices(),
{ kind = "luasnip" },
set_choice_callback(restore_data)
)

ls._active_update_dependents()

if not session.active_choice_nodes[vim.api.nvim_get_current_buf()] then
print("Active choice was removed while updating a dynamicNode.")
return
end

local restore_data = node_util.store_cursor_node_relative(active, {place_cursor_mark = true})

-- make sure all movements are done, otherwise the movements may be put into
-- the select-dialog.
feedkeys.enqueue_action(function()
vim.ui.select(
ls.get_current_choices(),
{ kind = "luasnip" },
set_choice_callback(restore_data)
)
end)
end

return select_choice
59 changes: 42 additions & 17 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ end

local function node_update_dependents_preserve_position(node, current, opts)
-- set luasnip_changedtick so that static_text is preserved when possible.
local restore_data = node_util.store_cursor_node_relative(current, {place_cursor_mark = true})
local restore_data = opts.cursor_restore_data or node_util.store_cursor_node_relative(current, {place_cursor_mark = true})

-- update all nodes that depend on this one.
local ok, res =
Expand Down Expand Up @@ -318,15 +318,15 @@ local function node_update_dependents_preserve_position(node, current, opts)
end
end

local function update_dependents(node)
local function update_dependents(node, opts)
local active = session.current_nodes[vim.api.nvim_get_current_buf()]
-- don't update if a jump/change_choice is in progress, or if we don't have
-- an active node.
if active ~= nil then
local upd_res = node_update_dependents_preserve_position(
node,
active,
{ no_move = false, restore_position = true }
{ no_move = false, restore_position = true, cursor_restore_data = opts and opts.cursor_restore_data }
)
if upd_res.new_current then
upd_res.new_current:focus()
Expand All @@ -335,8 +335,8 @@ local function update_dependents(node)
end
end

local function _active_update_dependents()
update_dependents(session.current_nodes[vim.api.nvim_get_current_buf()])
local function _active_update_dependents(opts)
update_dependents(session.current_nodes[vim.api.nvim_get_current_buf()], opts)
end

-- return next active node.
Expand Down Expand Up @@ -804,39 +804,61 @@ end
local function _change_choice(val, opts)
local active_choice =
session.active_choice_nodes[vim.api.nvim_get_current_buf()]
assert(active_choice, "No active choiceNode")

-- make sure we update completely, there may have been changes to the
-- buffer since the last update.
if not opts.skip_update then
assert(active_choice, "No active choiceNode")

_active_update_dependents({ cursor_restore_data = opts.cursor_restore_data })

active_choice =
session.active_choice_nodes[vim.api.nvim_get_current_buf()]
if not active_choice then
print("Active choice was removed while updating a dynamicNode.")
return
end
end

-- if the active choice exists current_node still does.
local current_node = session.current_nodes[vim.api.nvim_get_current_buf()]

local restore_data = opts and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node, {place_cursor_mark = false})

local new_active = safe_choice_action(
active_choice.parent.snippet,
active_choice.change_choice,
active_choice,
val,
session.current_nodes[vim.api.nvim_get_current_buf()],
restore_data
opts.skip_update and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node, {place_cursor_mark = false})
)
session.current_nodes[vim.api.nvim_get_current_buf()] = new_active
_active_update_dependents()
end

--- Change the currently active choice.
---@param val 1|-1 Move one choice forward or backward.
function API.change_choice(val, opts)
api_do(_change_choice, val, opts)
function API.change_choice(val)
api_do(_change_choice, val, {})
end

local function _set_choice(choice_indx, opts)
local active_choice =
session.active_choice_nodes[vim.api.nvim_get_current_buf()]
assert(active_choice, "No active choiceNode")

local current_node = session.current_nodes[vim.api.nvim_get_current_buf()]
if not opts.skip_update then
assert(active_choice, "No active choiceNode")

_active_update_dependents({ cursor_restore_data = opts.cursor_restore_data })

active_choice =
session.active_choice_nodes[vim.api.nvim_get_current_buf()]
if not active_choice then
print("Active choice was removed while updating a dynamicNode.")
return
end
end

local restore_data = opts and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node, {place_cursor_mark = false})
local current_node = session.current_nodes[vim.api.nvim_get_current_buf()]

local choice = active_choice.choices[choice_indx]
assert(choice, "Invalid Choice")
Expand All @@ -847,16 +869,18 @@ local function _set_choice(choice_indx, opts)
active_choice,
choice,
current_node,
restore_data
-- if the update was skipped, we have to use the cursor_restore_data
-- here.
opts.skip_update and opts.cursor_restore_data or node_util.store_cursor_node_relative(current_node, {place_cursor_mark = false})
)
session.current_nodes[vim.api.nvim_get_current_buf()] = new_active
_active_update_dependents()
end

--- Set the currently active choice.
---@param choice_indx integer Index of the choice to switch to.
function API.set_choice(choice_indx, opts)
api_do(_set_choice, choice_indx, opts)
function API.set_choice(choice_indx)
api_do(_set_choice, choice_indx, {})
end

--- Get a string-representation of all the current choiceNode's choices.
Expand Down Expand Up @@ -1378,6 +1402,7 @@ API.log = require("luasnip.util.log")
ls = lazy_table(API, ls_lazy)

-- undocumented, internally-used, exported functions.
ls._active_update_dependents = _active_update_dependents
ls._api_do = api_do
ls._api_enter = api_enter
ls._api_leave = api_leave
Expand Down