refactor(snip): improve line prepend implementation

This commit is contained in:
arne314
2025-05-17 23:21:11 +02:00
parent 0fdcfcc9c0
commit 73191eb32c
2 changed files with 25 additions and 43 deletions

View File

@@ -45,58 +45,39 @@ function M.start_snip(trigger, expand, insert, condition, priority, options)
return M.snip('^(\\s*)' .. trigger, '<>' .. expand, { M.cap(1), unpack(insert) }, condition, priority, options) return M.snip('^(\\s*)' .. trigger, '<>' .. expand, { M.cap(1), unpack(insert) }, condition, priority, options)
end end
-- Allows to pass expand string and insert table to either indent each line -- transform the snippet expand by inserting indentation and/or a prefix after each newline
-- dynamically of the captured group indent, and or prepend to each line after the indent function M.blocktransform(expand, insert, prepend, indent_capture_idx)
-- For example prepend = '-- ' in lua. local indent = indent_capture_idx ~= nil
-- indent: boolean to turn off indenting (option can't be set off right now) if not indent and not prepend then return expand, insert end
-- prepend: prepend string
function M.blocktransform(expand, insert, prepend, indent)
-- if idiomatic, skip
if indent ~= nil and not indent and not prepend then return expand, insert end
-- defaults / setup
if indent == nil then indent = true end
prepend = prepend or '' prepend = prepend or ''
local last_newl_index = 0
local modified_expand = expand local modified_expand = expand
function shallowClone(original) local modified_insert = {}
local copy = {} for i, v in pairs(insert) do
for key, value in pairs(original) do modified_insert[i] = v
copy[key] = value
end end
return copy
end
local modified_insert = shallowClone(insert)
local newl_count = 0
local offset = 0 local offset = 0
local last_pos = 0
-- logic
while true do while true do
-- break if no \n anymore local newline_pos = string.find(expand, '\n', last_pos + 1)
local new_newl_index = string.find(expand, '\n', last_newl_index + 1) if not newline_pos then break end
if not new_newl_index then break end
newl_count = newl_count + 1
-- insert the prepend and newl at the correct position -- prepend string
local insert_pos = new_newl_index + offset + 1 local insert_pos = newline_pos + offset + 1
local prefix = (indent and '<>' or '') .. prepend
modified_expand = string.sub(modified_expand, 1, insert_pos - 1) modified_expand = string.sub(modified_expand, 1, insert_pos - 1)
.. (indent and '<>' or '') .. prefix
.. prepend
.. string.sub(modified_expand, insert_pos) .. string.sub(modified_expand, insert_pos)
offset = offset + (indent and 2 or 0) + #prepend offset = offset + #prefix
-- indent of course needs to be added as a dynamic function -- indent node
if indent then if indent then
local substring = string.sub(modified_expand, 1, insert_pos + 1) local expand_before = string.sub(modified_expand, 1, insert_pos + 1)
local count = 0 local indent_pos = select(2, string.gsub(expand_before, '<>', ''))
table.insert(modified_insert, indent_pos, M.leading_white_spaces(indent_capture_idx))
local _, occurrences = string.gsub(substring, '<>', '')
count = occurrences
table.insert(modified_insert, count, M.leading_white_spaces(1))
end end
last_pos = newline_pos
last_newl_index = new_newl_index
end end
return modified_expand, modified_insert return modified_expand, modified_insert
end end
@@ -119,7 +100,7 @@ function M.bulletpoint_snip(trigger, expand, insert, condition, priority, option
{ M.cap(1), unpack(insert) }, { M.cap(1), unpack(insert) },
condition, condition,
priority, priority,
options vim.tbl_deep_extend('keep', { indentCaptureIdx = 1 }, options or {})
) )
end end
return M return M

View File

@@ -35,9 +35,10 @@ function M.snip(trigger, expand, insert, condition, priority, options)
wordTrig = true, wordTrig = true,
blacklist = {}, blacklist = {},
prepend = nil, prepend = nil,
indentCaptureIdx = nil,
}, options or {}) }, options or {})
if options.prepend ~= nil then if options.prepend ~= nil or options.indentCaptureIdx ~= nil then
expand, insert = M.blocktransform(expand, insert, options.prepend, true) expand, insert = M.blocktransform(expand, insert, options.prepend, options.indentCaptureIdx)
end end
return luasnip.snippet( return luasnip.snippet(
{ {