From 71c1e15ce2f58516577e1df5d005f6e4235f1045 Mon Sep 17 00:00:00 2001 From: arne314 <73391160+arne314@users.noreply.github.com> Date: Wed, 30 Apr 2025 00:39:29 +0200 Subject: [PATCH] feat(snip): trigger blacklist option --- lua/typstar/autosnippets.lua | 15 +++++++++++---- lua/typstar/snippets/math.lua | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/typstar/autosnippets.lua b/lua/typstar/autosnippets.lua index 1789f79..4a23a99 100644 --- a/lua/typstar/autosnippets.lua +++ b/lua/typstar/autosnippets.lua @@ -73,6 +73,7 @@ function M.snip(trigger, expand, insert, condition, priority, trigOptions) trigOptions = vim.tbl_deep_extend('force', { maxTrigLength = nil, wordTrig = true, + blacklist = {}, }, trigOptions or {}) return luasnip.snippet( { @@ -151,10 +152,11 @@ function M.engine(trigger, opts) end -- matching - return function(line, trig) + return function(line_full, trig) if not M.snippets_toggle or not condition() then return nil end + local first_idx = 1 if max_length ~= nil then - local first_idx = #line - max_length -- include additional char for wordtrig + first_idx = #line_full - max_length -- include additional char for wordtrig if first_idx < 0 then if is_fixed_length then return nil @@ -163,10 +165,10 @@ function M.engine(trigger, opts) end end if first_idx > 0 then - if string.byte(line, first_idx) > 127 then return nil end + if string.byte(line_full, first_idx) > 127 then return nil end end - line = line:sub(first_idx) end + local line = line_full:sub(first_idx) local whole, captures = base_engine(line, trig) if whole == nil then return nil end @@ -175,6 +177,11 @@ function M.engine(trigger, opts) if opts.wordTrig and from ~= 1 and string.match(string.sub(line, from - 1, from - 1), '[%w.]') ~= nil then return nil end + + -- blacklist + for _, w in ipairs(opts.blacklist) do + if line_full:sub(-#w) == w then return nil end + end return whole, captures end end diff --git a/lua/typstar/snippets/math.lua b/lua/typstar/snippets/math.lua index bc18b1a..ebaa2bc 100644 --- a/lua/typstar/snippets/math.lua +++ b/lua/typstar/snippets/math.lua @@ -45,7 +45,7 @@ return { snip('ff', '(<>) / (<>) <>', { i(1, 'a'), i(2, 'b'), i(3) }, math), -- exponents - snip('iv', '^(-1) ', {}, math, 500, { wordTrig = false }), + snip('iv', '^(-1) ', {}, math, 500, { wordTrig = false, blacklist = { 'equiv' } }), snip('sr', '^2 ', {}, math, 500, { wordTrig = false }), snip('cb', '^3 ', {}, math, 500, { wordTrig = false }), snip('jj', '_(<>) ', { i(1, 'n') }, math, 500, { wordTrig = false }),