feat(snip): precise visual snippet configuration

This commit is contained in:
arne314
2025-06-27 23:41:35 +02:00
parent 50634a190d
commit bfc9bff1a5
2 changed files with 31 additions and 7 deletions

View File

@@ -27,6 +27,9 @@ local default_config = {
'visual', 'visual',
}, },
exclude = {}, -- list of triggers to exclude exclude = {}, -- list of triggers to exclude
visual_disable = {}, -- visual.lua: list of triggers to exclude from visual selection mode
visual_disable_normal = {}, -- visual.lua: list of triggers to exclude from normal snippet mode
visual_disable_postfix = {}, -- visual.lua: list of triggers to exclude from postfix snippet mode
}, },
} }

View File

@@ -7,10 +7,17 @@ local t = ls.text_node
local helper = require('typstar.autosnippets') local helper = require('typstar.autosnippets')
local utils = require('typstar.utils') local utils = require('typstar.utils')
local cfg = require('typstar.config').config.snippets
local math = helper.in_math local math = helper.in_math
local snip = helper.snip local snip = helper.snip
local snippets = {} local snippets = {}
local visual_disable = {}
local visual_disable_normal = {}
local visual_disable_postfix = {}
utils.generate_bool_set(cfg.visual_disable, visual_disable)
utils.generate_bool_set(cfg.visual_disable_normal, visual_disable_normal)
utils.generate_bool_set(cfg.visual_disable_postfix, visual_disable_postfix)
local operations = { -- first boolean: existing brackets should be kept; second boolean: brackets should be added local operations = { -- first boolean: existing brackets should be kept; second boolean: brackets should be added
{ 'vi', '1/', '', true, false }, { 'vi', '1/', '', true, false },
@@ -68,17 +75,31 @@ local smart_wrap = function(args, parent, old_state, expand)
local cursor = utils.get_cursor_pos() local cursor = utils.get_cursor_pos()
local root = utils.get_treesitter_root(bufnr) local root = utils.get_treesitter_root(bufnr)
if process_ts_query(bufnr, cursor, ts_wrapnobrackets_query, root, expand[2], expand[3], expand[4] and 0 or 1) then local trigger = expand[1]
return s(nil, t())
end
local expand1 = expand[5] and expand[2] .. '(' or expand[2] local expand1 = expand[5] and expand[2] .. '(' or expand[2]
local expand2 = expand[5] and expand[3] .. ')' or expand[3] local expand2 = expand[5] and expand[3] .. ')' or expand[3]
if process_ts_query(bufnr, cursor, ts_wrap_query, root, expand1, expand2) then return s(nil, t()) end
if #parent.env.LS_SELECT_RAW > 0 then -- visual selection
if not visual_disable[trigger] and #parent.env.LS_SELECT_RAW > 0 then
return s(nil, t(expand1 .. table.concat(parent.env.LS_SELECT_RAW) .. expand2)) return s(nil, t(expand1 .. table.concat(parent.env.LS_SELECT_RAW) .. expand2))
end end
-- postfix
if not visual_disable_postfix[trigger] then
if
process_ts_query(bufnr, cursor, ts_wrapnobrackets_query, root, expand[2], expand[3], expand[4] and 0 or 1)
or process_ts_query(bufnr, cursor, ts_wrap_query, root, expand1, expand2)
then
return s(nil, t())
end
end
-- normal snippet
if not visual_disable_normal[trigger] then
return s(nil, { t(expand1), i(1, '1+1'), t(expand2) }) return s(nil, { t(expand1), i(1, '1+1'), t(expand2) })
else
return s(nil, t(trigger))
end
end end
for _, val in pairs(operations) do for _, val in pairs(operations) do