mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 05:24:24 -05:00
feat(snip): prepend to expanded lines (#15)
* changed add_to_newl behavior to auto-indent * commented debug messages * fixed dm inine according to into_newl change * oops. no visual doubling with inline dm * in theory indent and prepend block functionality... * parameter switch * comments * added prepend parameters to intonewl * refinement * necessary changes for dm snippet * backup in comment * style: format code * refactor: remove commented code --------- Co-authored-by: Arne <73391160+arne314@users.noreply.github.com>
This commit is contained in:
@@ -95,17 +95,100 @@ function M.start_snip(trigger, expand, insert, condition, priority, trigOptions)
|
||||
return M.snip('^(\\s*)' .. trigger, '<>' .. expand, { M.cap(1), unpack(insert) }, condition, priority, trigOptions)
|
||||
end
|
||||
|
||||
function M.start_snip_in_newl(trigger, expand, insert, condition, priority, trigOptions)
|
||||
-- Allows to pass expand string and insert table to either indent each line
|
||||
-- dynamically of the captured group indent, and or prepend to each line after the indent
|
||||
-- For example prepend = '-- ' in lua.
|
||||
-- indent: boolean to turn off indenting (option can't be set off right now)
|
||||
-- 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 ''
|
||||
local last_newl_index = 0
|
||||
local modified_expand = expand
|
||||
function shallowClone(original)
|
||||
local copy = {}
|
||||
for key, value in pairs(original) do
|
||||
copy[key] = value
|
||||
end
|
||||
return copy
|
||||
end
|
||||
|
||||
local modified_insert = shallowClone(insert)
|
||||
local newl_count = 0
|
||||
local offset = 0
|
||||
|
||||
-- logic
|
||||
while true do
|
||||
-- break if no \n anymore
|
||||
local new_newl_index = string.find(expand, '\n', last_newl_index + 1)
|
||||
if not new_newl_index then break end
|
||||
newl_count = newl_count + 1
|
||||
|
||||
-- insert the prepend and newl at the correct position
|
||||
local insert_pos = new_newl_index + offset + 1
|
||||
modified_expand = string.sub(modified_expand, 1, insert_pos - 1)
|
||||
.. (indent and '<>' or '')
|
||||
.. prepend
|
||||
.. string.sub(modified_expand, insert_pos)
|
||||
offset = offset + (indent and 2 or 0) + #prepend
|
||||
|
||||
-- indent of course needs to be added as a dynamic function
|
||||
if indent then
|
||||
local substring = string.sub(modified_expand, 1, insert_pos + 1)
|
||||
local count = 0
|
||||
|
||||
local _, occurrences = string.gsub(substring, '<>', '')
|
||||
count = occurrences
|
||||
table.insert(modified_insert, count, M.leading_white_spaces(1))
|
||||
end
|
||||
|
||||
last_newl_index = new_newl_index
|
||||
end
|
||||
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)
|
||||
return M.snip(
|
||||
'([^\\s]\\s+)' .. trigger,
|
||||
'<>\n<>' .. expand,
|
||||
{ M.cap(1), M.leading_white_spaces(1), unpack(insert) },
|
||||
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) },
|
||||
condition,
|
||||
priority,
|
||||
prependlines,
|
||||
trigOptions
|
||||
)
|
||||
end
|
||||
|
||||
function M.bulletpoint_snip(trigger, expand, insert, condition, priority, prepend, prependlines, trigOptions)
|
||||
prepend = prepend or ''
|
||||
return M.snip_after_transform(
|
||||
'(^\\s*\\-\\s+.*\\s*)' .. trigger,
|
||||
'<><>' .. expand,
|
||||
{ M.cap(1), prepend, unpack(insert) },
|
||||
condition,
|
||||
priority,
|
||||
prependlines,
|
||||
trigOptions
|
||||
)
|
||||
end
|
||||
|
||||
local alts_regex = '[\\[\\(](.*|.*)[\\)\\]]'
|
||||
|
||||
function M.engine(trigger, opts)
|
||||
|
||||
@@ -45,7 +45,8 @@ end
|
||||
|
||||
return {
|
||||
start('dm', '$\n<>\n<>$', { indent_visual(1), cap(1) }, markup),
|
||||
helper.start_snip_in_newl('dm', '$\n<>\n<>$', { indent_visual(1), helper.leading_white_spaces(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'),
|
||||
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),
|
||||
|
||||
Reference in New Issue
Block a user