perf(snip): consider fixed trigger lengths

ignore the first part of a line if the trigger length is known
This commit is contained in:
arne314
2025-01-16 00:28:59 +01:00
parent 1b4cac06ae
commit 1cbc6086de
2 changed files with 31 additions and 4 deletions

View File

@@ -46,13 +46,13 @@ end
function M.snip(trigger, expand, insert, condition, priority, wordTrig)
priority = priority or 1000
if wordTrig == nil then wordTrig = #trigger <= 2 end
if wordTrig == nil then wordTrig = true end
return luasnip.snippet(
{
trig = trigger,
trigEngine = M.engine,
trigEngineOpts = { condition = condition },
wordTrig = wordTrig,
trigEngineOpts = { condition = condition, wordTrig = wordTrig },
wordTrig = false,
priority = priority,
snippetType = 'autosnippet',
},
@@ -69,6 +69,17 @@ end
function M.engine(trigger, opts)
local base_engine = lsengines.ecma(trigger, opts)
-- determine possibly fixed length of trigger
local fixed_length
if not trigger:match('[%+%*%?%]%[|]') then
fixed_length = #trigger
- utils.count_string(trigger, '\\')
- utils.count_string(trigger, '%(')
- utils.count_string(trigger, '%)')
end
-- cache preanalysis results
local condition = function()
local cached = lexical_result_cache[opts.condition]
if cached ~= nil and cached[1] == last_keystroke_time then return cached[2] end
@@ -76,9 +87,20 @@ function M.engine(trigger, opts)
lexical_result_cache[opts.condition] = { last_keystroke_time, result }
return result
end
-- matching
return function(line, trig)
if not M.snippets_toggle or not condition() then return nil end
return base_engine(line, trig)
if fixed_length ~= nil then line = line:sub(#line - fixed_length) end
local whole, captures = base_engine(line, trig)
if whole == nil then return nil end
-- custom word trig
local from = #line - #whole + 1
if opts.wordTrig and from ~= 1 and string.match(string.sub(line, from - 1, from - 1), '[%w.]') ~= nil then
return nil
end
return whole, captures
end
end

View File

@@ -46,6 +46,11 @@ function M.run_shell_command(cmd, show_output)
end
end
function M.count_string(str, tocount)
local _, count = str:gsub(tocount, '')
return count
end
function M.char_to_hex(c) return string.format('%%%02X', string.byte(c)) end
function M.urlencode(url)