Closed JxJxxJxJ closed 1 year ago
You need to drop the first argument of the function (the list of inputs).
require('luasnip').function_node(function( _ , snip)
You need to drop the first argument of the function (the list of inputs).
require('luasnip').function_node(function( _ , snip)
Sorry I did not understand, what should I do with that?
The first argument is the list of inputs the function will use, not the parent snippet.
@leiserfg I'm trying again to solve this. Currently I made the modifications you indicated.
I have:
return {
s({ trig = 'bk', wordTrig = true, snippetType = "autosnippet" },
{
require('luasnip').function_node(function(_, snip)
local selected_text = snip.env.TM_SELECTED_TEXT or ""
return "\\textbf{" .. selected_text .. "}"
end, {})
},
{ condition = in_text }
),
}
But when I select text + press TAB
+ bk
the following error appear
E5108: Error executing lua [string "require("luasnip").setup_snip_env() ---@diagn..."]:7: attempt to conca
tenate local 'selected_text' (a table value)
stack traceback:
[string "require("luasnip").setup_snip_env() ---@diagn..."]:7: in function 'fn'
.../packer/start/LuaSnip/lua/luasnip/nodes/functionNode.lua:47: in function 'update'
.../pack/packer/start/LuaSnip/lua/luasnip/nodes/snippet.lua:813: in function 'update'
.../pack/packer/start/LuaSnip/lua/luasnip/nodes/snippet.lua:504: in function 'trigger_expand'
...rvim/site/pack/packer/start/LuaSnip/lua/luasnip/init.lua:227: in function 'snip_expand'
...rvim/site/pack/packer/start/LuaSnip/lua/luasnip/init.lua:310: in function 'expand_auto'
[string ":lua"]:1: in main chunk
The problem I encountered is that selected_text
is a table value, but I am trying to concatenate it as a string. This could be because TM_SELECTED_TEXT
returns a table instead of a string in some cases.
To fix this issue, I can convert the selected_text
table into a string before concatenating it in the function node. Here's a way to do that using table.concat
:
return {
s({ trig = 'bk', wordTrig = true, snippetType = "autosnippet" },
{
require('luasnip').function_node(function(_, snip)
local selected_text = snip.env.TM_SELECTED_TEXT or ""
if type(selected_text) == 'table' then
selected_text = table.concat(selected_text, '\n')
end
return "\\textbf{" .. selected_text .. "}"
end, {})
},
{ condition = in_text }
),
}
This solution checks if selected_text
is a table and, if so, converts it into a string by concatenating its elements with newline characters as separators.
Is there any more elegant solution?
TM_SELECTED_TEXT is always a table.
Thanks!
s({ trig = 'vbf', wordTrig = true, snippetType = "autosnippet" },
{
require('luasnip').function_node(function(_, snip)
local selected_text = snip.env.TM_SELECTED_TEXT or {}
local text = table.concat(selected_text, '\n')
return "\\textbf{" .. text .. "}"
end, {})
},
{ condition = in_text }
),
Works perfectly :) Thank you very much
I'm trying something like this but does not seem to work.
After I press
<Tab>
as instore_selection_keys = "<Tab>",
in myrequire("luasnip").config.set_config
and thenbk
to trigger the snippet the following error appears