bug: postfix snippet pattern match does not work
I want to create a snippet that will match a string of digits (\d+) as an Unix Epoch time and convert it into a date in ISO format
local ls = require('luasnip')
local f = ls.function_node
local postfix = require('luasnip.extras.postfix').postfix
local ISO_FORMAT = '%Y-%m-%dT%H:%M:%S'
...
postfix({
trig = '.toiso',
name = 'epoch to ISO',
match_pattern = '[%d]+$', -- or match_pattern = '%d+$'
docTrig = '0',
}, {
f(function(_, parent)
print(parent.snippet.env.POSTFIX_MATCH)
if #parent.snippet.env.POSTFIX_MATCH < 9 then
return os.date(ISO_FORMAT) -- Is there a way to cancel the snippet at this point?
end
return os.date(ISO_FORMAT, tonumber(parent.snippet.env.POSTFIX_MATCH))
end, {}),
}),
With the above configuration, the snippet always trigger for any string and parent.snippet.env.POSTFIX_MATCH always evaluates to $POSTFIX_MATCH.
I have followed the examples in the docs and also looked at the code but I cannot tell what is wrong with it.
Hey, I don't get this behaviour you're experiencing (at least on master, what version/commit are you on?). Are you using an (non-master version) of cmp_luasnip for triggering the snippet? There may be issues with that
https://github.com/L3MON4D3/LuaSnip/assets/41961280/1b126d39-f67e-46ba-b06e-8f3d9f90c9b4
Re. the comment with >= 9 characters for the timestamp: you can use resolveExpandParams for this, but you'd have to more-or-less replicate what postfix itself is doing (see https://github.com/L3MON4D3/LuaSnip/blob/a57babc55ec135efc188d5f332317cad99681137/lua/luasnip/extras/postfix.lua#L13-L45), or take the KISS-route and just manually put "%d%d%d%d%d%%d%d%d%d+$" to restrict the number of.. numbers :D
BTW, nice snippet! Fun application of postfix :)
hi, thanks for the quick reply!
I realized that I was seeing the match value as $POSTFIX_MATCH because it was not yet expanded. Once accepting the snippet the value was correct.
So now the only problem is that the trigger is not respected. Not sure what could be wrong.
I removed and reinstalled luasnip with packer but the behaviour continues
https://github.com/L3MON4D3/LuaSnip/assets/43855513/aa054f9a-692c-4bee-b1bd-a4efac57bf30
nvim --version
NVIM v0.9.4
Build type: Release
LuaJIT 2.1.1700008891