Skip to content

Commit

Permalink
update after snip_expand.
Browse files Browse the repository at this point in the history
Has to happen because we modified text.
I don't like using vim.schedule here, but it seems like this is the
only way of getting the desired behaviour into all possible ways of
expanding snippets (direct snip_expand is used by cmp_luasnip, so can't
just handle `expand` and its variants in init.lua (or, we could do so
and submit a PR to cmp_luasnip, but let's wait with that until this
actually becomes problematic, which I don't think it will)).
  • Loading branch information
L3MON4D3 committed Apr 24, 2024
1 parent 05d5aef commit f513f60
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions lua/luasnip/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ local function node_update_dependents_preserve_position(node, opts)
end
end

local function active_update_dependents()
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 not session.jump_active and active ~= nil then
local upd_res = node_update_dependents_preserve_position(active, { no_move = false, restore_position = true })
upd_res.new_node:focus()
session.current_nodes[vim.api.nvim_get_current_buf()] = upd_res.new_node
end
end

-- return next active node.
local function safe_jump_current(dir, no_move, dry_run)
local node = session.current_nodes[vim.api.nvim_get_current_buf()]
Expand Down Expand Up @@ -424,6 +435,11 @@ local function snip_expand(snippet, opts)
-- -1 to disable count.
vim.cmd([[silent! call repeat#set("\<Plug>luasnip-expand-repeat", -1)]])

-- schedule update of active node.
-- Not really happy with this, but for some reason I don't have time to
-- investigate, nvim_buf_get_text does not return the updated text :/
vim.schedule(active_update_dependents)

return snip
end

Expand Down Expand Up @@ -594,17 +610,6 @@ local function get_current_choices()
return choice_lines
end

local function active_update_dependents()
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 not session.jump_active and active ~= nil then
local upd_res = node_update_dependents_preserve_position(active, { no_move = false, restore_position = true })
upd_res.new_node:focus()
session.current_nodes[vim.api.nvim_get_current_buf()] = upd_res.new_node
end
end

local function store_snippet_docstrings(snippet_table)
-- ensure the directory exists.
-- 493 = 0755
Expand Down

0 comments on commit f513f60

Please sign in to comment.