Replies: 1 comment 1 reply
-
Hi :) I've tried your wrapper, and it seems to work as expected -- the following are utils used in the wrapper
function string.verbatim(str)
local regex = {"^","$","(",")","%",".","[","]","*","+","-","?",}
local percent = '%'
_G.my_regex_special = _G.my_regex_special
or ('([%s])'):format(
vim
.iter(regex)
:fold('', function(sum, current) return sum .. '%' .. current end)
)
local ret, _ = str
:gsub('%%', '%%%%')
:gsub(_G.my_regex_special, '%%%1')
:gsub(percent:rep(8), percent:rep(4))
return ret
end
---@generic TResult
---@return TResult[]
---@param iterator fun(): TResult
function Collect(iterator)
local ret = {}
for element in iterator do
table.insert(ret, element)
end
return ret
end
--- simple snippet definition
--- only for snippets have successive index
--- use <> as delimiters by default
---@param filetype string | string[]
---@param trigger string | { trig: string, name: string, desc: string }
---@param body string
---@param opts? { delimiters?: string }
local add = function(filetype, trigger, body, opts)
local ok, ls = pcall(require, 'luasnip')
if not ok then return end
local snip = ls.snippet --[[@as fun(trigger: string | { trig: string, name: string, desc: string }, node: any[] | any)]]
local insert = ls.i --[[@as fun(idx: integer, placeholder: string)]]
local fmt = require('luasnip.extras.fmt').fmt --[[@as fun(body: string, nodes: any[] | any, opts: table)]]
opts = (opts and opts.delimiters) and opts or { delimiters = '<>' }
local l, r = opts.delimiters:sub(1, 1), opts.delimiters:sub(2, 2)
local placeholders = Collect(body:gmatch(l:verbatim() .. '(.-)' .. r:verbatim()))
local body, count = body:gsub(l:verbatim() .. '.-' .. r:verbatim(), opts.delimiters)
local nodes = {}
for i = 1, count do
I(placeholders[i])
table.insert(nodes, insert(i, placeholders[i]))
end
I(body)
if type(filetype) == 'table' then
for _, ft in ipairs(filetype) do
ls.add_snippets(ft, {
snip(trigger, fmt(body, nodes, opts)),
})
end
else
ls.add_snippets(filetype, {
snip(trigger, fmt(body, nodes, opts)),
})
end
vim.notify(vim.inspect(placeholders))
end
require("luasnip").add_snippets("all", {
add(
{ 'cs', 'typescript', 'javascript', 'lua' },
'lambda',
'([param]) => [/* body */]',
{ delimiters = '[]' }
)
}, { key = "043fd491-ba06-4133-a415-b858f1bf795e" }) Could you also try my code? I think there may be something else interfering. I'm using the current master of luasnip, maybe that's a difference? |
Beta Was this translation helpful? Give feedback.
1 reply
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.
-
I was trying to make a wrapper for creating simple snippet by
fmt
.The wrapper is pretty long
The wrapper works fine for other uses, but error happens on following snippet:
I am using blink.cmp, it shows following error:
What's strange is that this error happens for filetype
cs
,javascript
,typescript
but works ok forlua
andjson
. I have no idea what I did wrong, any particular mistake would cause this error? Please help!Beta Was this translation helpful? Give feedback.
All reactions