Replies: 1 comment
-
Hi! What you're observing is the mechanism luasnip uses to generate a string-representation of a snippet-object, it's explained here. So, the correct handling depends on what you'd like to see in the docstring, you could for example do s("co", {
d(1, function(_, snip)
-- check for fake_expand
if snip.snippet.env.____INVALIDVARNAME then
return sn(nil, {t"position[$x,$y]"})
end
local ls_select_raw = snip.env.LS_SELECT_RAW or {}
local selected_text = table.concat(ls_select_raw, "\n")
local register_data = vim.fn.getreg() .. ""
local selected_and_register_data = selected_text .. register_data
local first_match = string.match(selected_and_register_data, "-?%d+,%s*-?%d+")
if first_match then
return sn(nil, {
t("position([" .. first_match .. "])")
})
else
vim.notify("Register does not contain the pattern", vim.log.levels.INFO, { title = "LuaSnippet" })
return sn(nil, {})
end
end),
}) which would give the docstring |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When a snippet with a function node is expanded for the first time in a session, the function node appears to undergo a pre-evaluation phase using some placeholder variables. After this initial phase, it is reinvoked with the "correct"/expected variables. I am attempting to define a snippet as follows:
It seems that my snippet function is being called once during its initial setup. On this first invocation,
snip.env.LS_SELECTED_TEXT
is not of the expected type and instead holds the string "$LS_SELECTED_TEXT". I'm not entirely sure why this happens during initialization, so I handle it by returning an empty node. Is there a more appropriate way to handle this situation?I saw #647
where the author uses:
But that has the behavior of only containing the expected text the very first time the snippet is invoked, so I don't think that is what I want.
I have read
:h luasnip-selection
,:h luasnip-functionnode
,:h luasnip-dynamicnode
, I went through the examples, I watched some tutorials, and I even now know there is thisfake_env
function that is causing this behavior, but I do not know why it is this way and not mentioned.Beta Was this translation helpful? Give feedback.
All reactions