LuaSnip icon indicating copy to clipboard operation
LuaSnip copied to clipboard

Feature Request: Allowing fmt() to automatically transform unassigned {placeholders} nodes to empty Insert Nodes

Open ziontee113 opened this issue 3 years ago • 4 comments

Please pardon me if this feature already exists (for Lua parser). At the moment I only create snippets using fmt(), and it would be very time saving not having to type i(1, "") over and over again when creating snippets.

For example: from

fmt( [[
local {} = {}
]], { i(1,""), i(2,"") })

To:

fmt( [[
local {} = {}
]] )

I know other parsers exists, which offers easier syntax, but lacks the freedom & extensibility like Lua Snippets. Extended example:

fmt( [[
local {} = {}
{content}
]], { content = i(3, "") } )

I assume that we don't have this feature yet because of placeholder jump order complications?

ziontee113 avatar Aug 11 '22 00:08 ziontee113

Uhh, I actually have something like this in my config :sweat_smile:

ls.setup_snip_env()


local function ins_generate(nodes)
	return setmetatable(nodes or {}, {
        -- generate missing keys as corresponding insertNode.
	__index = function(table, key)
		local indx = tonumber(key)
		if indx then
			local val = ls.i(indx)
			rawset(table, key, val)
			return val
		end
	end})
end

local function fmt(body, nodes)
	return require("luasnip.extras.fmt").fmt(body, ins_generate(nodes))
end

ls.snip_expand(s("a", fmt("{} = {}")))

I adapted it a bit to make it more like your suggestion. Maybe we should provide some version of it in extras?

L3MON4D3 avatar Aug 11 '22 07:08 L3MON4D3

Maybe we should provide some version of it in extras?

I think so yeah :+1: Thank you very much for sharing :) I think this would make Lua Snippets feel "less annoying" (less constrained). Having a red error raised every 2 seconds is not a very fun experience :joy:

ziontee113 avatar Aug 11 '22 10:08 ziontee113

\begin{sarcasm} How will you feel if the error were cyan instead?
\end{sarcasm}

leiserfg avatar Aug 11 '22 12:08 leiserfg

Having a red error raised every 2 seconds is not a very fun experience

Ouh, I think it might also prevent further reloading of the file, at least I experienced that once and don't think I got to fixing that yet xD We should wrap the call that actually loads the file in a pcall and log (:grimacing:) errors, maybe print a small warning with a shortened error message.

L3MON4D3 avatar Aug 11 '22 18:08 L3MON4D3