mirror of
https://github.com/Ascyii/typstar.git
synced 2026-01-01 13:34:24 -05:00
perf(snip): consider fixed trigger lengths
ignore the first part of a line if the trigger length is known
This commit is contained in:
@@ -46,13 +46,13 @@ end
|
|||||||
|
|
||||||
function M.snip(trigger, expand, insert, condition, priority, wordTrig)
|
function M.snip(trigger, expand, insert, condition, priority, wordTrig)
|
||||||
priority = priority or 1000
|
priority = priority or 1000
|
||||||
if wordTrig == nil then wordTrig = #trigger <= 2 end
|
if wordTrig == nil then wordTrig = true end
|
||||||
return luasnip.snippet(
|
return luasnip.snippet(
|
||||||
{
|
{
|
||||||
trig = trigger,
|
trig = trigger,
|
||||||
trigEngine = M.engine,
|
trigEngine = M.engine,
|
||||||
trigEngineOpts = { condition = condition },
|
trigEngineOpts = { condition = condition, wordTrig = wordTrig },
|
||||||
wordTrig = wordTrig,
|
wordTrig = false,
|
||||||
priority = priority,
|
priority = priority,
|
||||||
snippetType = 'autosnippet',
|
snippetType = 'autosnippet',
|
||||||
},
|
},
|
||||||
@@ -69,6 +69,17 @@ end
|
|||||||
|
|
||||||
function M.engine(trigger, opts)
|
function M.engine(trigger, opts)
|
||||||
local base_engine = lsengines.ecma(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 condition = function()
|
||||||
local cached = lexical_result_cache[opts.condition]
|
local cached = lexical_result_cache[opts.condition]
|
||||||
if cached ~= nil and cached[1] == last_keystroke_time then return cached[2] end
|
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 }
|
lexical_result_cache[opts.condition] = { last_keystroke_time, result }
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- matching
|
||||||
return function(line, trig)
|
return function(line, trig)
|
||||||
if not M.snippets_toggle or not condition() then return nil end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -46,6 +46,11 @@ function M.run_shell_command(cmd, show_output)
|
|||||||
end
|
end
|
||||||
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.char_to_hex(c) return string.format('%%%02X', string.byte(c)) end
|
||||||
|
|
||||||
function M.urlencode(url)
|
function M.urlencode(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user