refactor(snip): syntax of line prepended snippets

This commit is contained in:
arne314
2025-05-14 19:25:13 +02:00
parent 8295b2e4b6
commit abb355c505
2 changed files with 21 additions and 33 deletions

View File

@@ -68,18 +68,22 @@ function M.ri(insert_node_id)
return luasnip.function_node(function(args) return args[1][1] end, insert_node_id)
end
function M.snip(trigger, expand, insert, condition, priority, trigOptions)
function M.snip(trigger, expand, insert, condition, priority, options)
priority = priority or 1000
trigOptions = vim.tbl_deep_extend('force', {
options = vim.tbl_deep_extend('force', {
maxTrigLength = nil,
wordTrig = true,
blacklist = {},
}, trigOptions or {})
prepend = nil,
}, options or {})
if options.prepend ~= nil then
expand, insert = M.blocktransform(expand, insert, options.prepend, true)
end
return luasnip.snippet(
{
trig = trigger,
trigEngine = M.engine,
trigEngineOpts = vim.tbl_deep_extend('keep', { condition = condition }, trigOptions),
trigEngineOpts = vim.tbl_deep_extend('keep', { condition = condition }, options),
wordTrig = false,
priority = priority,
snippetType = 'autosnippet',
@@ -91,8 +95,8 @@ function M.snip(trigger, expand, insert, condition, priority, trigOptions)
)
end
function M.start_snip(trigger, expand, insert, condition, priority, trigOptions)
return M.snip('^(\\s*)' .. trigger, '<>' .. expand, { M.cap(1), unpack(insert) }, condition, priority, trigOptions)
function M.start_snip(trigger, expand, insert, condition, priority, options)
return M.snip('^(\\s*)' .. trigger, '<>' .. expand, { M.cap(1), unpack(insert) }, condition, priority, options)
end
-- Allows to pass expand string and insert table to either indent each line
@@ -151,41 +155,25 @@ function M.blocktransform(expand, insert, prepend, indent)
return modified_expand, modified_insert
end
function M.snip_after_transform(trigger, expand, insert, condition, priority, prependlines, trigOptions)
local expand, insert = M.blocktransform(expand, insert, prependlines, true)
function M.start_snip_in_newl(trigger, expand, insert, condition, priority, options)
return M.snip(
trigger,
expand,
insert,
condition,
priority,
vim.tbl_deep_extend('keep', { wordTrig = false }, trigOptions or {})
)
end
function M.start_snip_in_newl(trigger, expand, insert, condition, priority, prepend, prependlines, trigOptions)
prepend = prepend or ''
return M.snip_after_transform(
'([^\\s]\\s+)' .. trigger,
'<><>\n' .. expand,
{ M.cap(1), prepend, unpack(insert) },
'<>\n' .. expand,
{ M.cap(1), unpack(insert) },
condition,
priority,
prependlines,
trigOptions
options
)
end
function M.bulletpoint_snip(trigger, expand, insert, condition, priority, prepend, prependlines, trigOptions)
prepend = prepend or ''
return M.snip_after_transform(
function M.bulletpoint_snip(trigger, expand, insert, condition, priority, options)
return M.snip(
'(^\\s*\\-\\s+.*\\s*)' .. trigger,
'<><>' .. expand,
{ M.cap(1), prepend, unpack(insert) },
'<>' .. expand,
{ M.cap(1), unpack(insert) },
condition,
priority,
prependlines,
trigOptions
options
)
end

View File

@@ -45,8 +45,8 @@ end
return {
start('dm', '$\n<>\n<>$', { indent_visual(1), cap(1) }, markup),
helper.start_snip_in_newl('dm', '$\n\t<>\n$ <>', { helper.visual(1), i(2) }, markup, 999),
helper.bulletpoint_snip('dm', '\n$\n\t<>\n$ <>', { helper.visual(1), i(2) }, markup, 1001, '', '\t'),
helper.start_snip_in_newl('dm', '$\n\t<>\n$ <>', { helper.visual(1), i(2) }, markup, nil, { wordTrig = false }),
helper.bulletpoint_snip('dm', '\n$\n\t<>\n$ <>', { helper.visual(1), i(2) }, markup, 1100, { prepend = '\t' }),
start('fla', '#flashcard(0)[<>][\n<>\n<>]', { i(1, 'flashcard'), indent_visual(2), cap(1) }, markup),
start('flA', '#flashcard(0, "<>")[\n<>\n<>]', { i(1, 'flashcard'), indent_visual(2), cap(1) }, markup),
snip('IMP', '$==>>$ ', {}, markup),