Skip to content

Commit

Permalink
get_args: do (static_)visible-check in get_args, not get_static_text.
Browse files Browse the repository at this point in the history
Much more appropriate, also get_current_choices will work even if some
insertNode is not visible.
  • Loading branch information
L3MON4D3 committed Nov 6, 2024
1 parent 211254b commit ae8d95c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 33 deletions.
6 changes: 0 additions & 6 deletions lua/luasnip/nodes/insertNode.lua
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,6 @@ function InsertNode:get_snippetstring()
return snippetstring
end
function InsertNode:get_static_snippetstring()
if not self.visible and not self.static_visible then
return nil
end
return self.static_text
end

Expand Down Expand Up @@ -412,9 +409,6 @@ function InsertNode:put_initial(pos)
end

function InsertNode:get_static_text()
if not self.visible and not self.static_visible then
return nil
end
return vim.split(self.static_text:str(), "\n")
end

Expand Down
34 changes: 7 additions & 27 deletions lua/luasnip/nodes/node.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,28 +32,6 @@ function Node:new(o, opts)
end

function Node:get_static_text()
-- return nil if not visible.
-- This will prevent updates if not all nodes are visible during
-- docstring/static_text-generation. (One example that would otherwise fail
-- is the following snippet:
--
-- s("trig", {
-- i(1, "cccc"),
-- t" ",
-- c(2, {
-- t"aaaa",
-- i(nil, "bbbb")
-- }),
-- f(function(args) return args[1][1]..args[2][1] end, {ai[2][2], 1} )
-- })
--
-- )
-- By also allowing visible, and not only static_visible, the docstrings
-- generated during `get_current_choices` (ie. without having the whole
-- snippet `static_init`ed) get better.
if not self.visible and not self.static_visible then
return nil
end
return self.static_text
end

Expand Down Expand Up @@ -160,9 +138,6 @@ function Node:get_snippetstring()
end

function Node:get_static_snippetstring()
if not self.visible and not self.static_visible then
return nil
end
-- if this is not overridden, get_static_text() is a multiline string.
return snippet_string.new(self:get_static_text())
end
Expand Down Expand Up @@ -275,8 +250,13 @@ local function get_args(node, get_text_func_name, static)
dict_key[#dict_key] = nil
end
-- maybe the node is part of a dynamicNode and not yet generated.
if argnode then
if not static and argnode.visible then
-- also handle the argnode as not-present if
-- * we are doing a regular update and it is not visible, or
-- * we are doing a static update and it is not static_visible or
-- visible (this second condition is to allow the docstring-generation
-- to be improved by data provided after the expansion)
if argnode and ((static and (argnode.static_visible or argnode.visible)) or (not static and argnode.visible)) then
if not static then
-- Don't store (aka call get_snippetstring) if this is a static
-- update (there will be no associated buffer-region!) and
-- don't store if the node is not visible. (Then there's
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/choice_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -309,4 +309,17 @@ describe("ChoiceNode", function()
{2:-- INSERT --} |]],
})
end)

it("correctly gives current content of choices.", function()
assert.are.same({"${1:asdf}", "qwer"}, exec_lua[[
ls.snip_expand(s("trig", {
c(1, {
i(1, "asdf"),
t"qwer"
})
}))
ls.change_choice()
return ls.get_current_choices()
]])
end)
end)

0 comments on commit ae8d95c

Please sign in to comment.