diff --git a/lua/typstar/autosnippets.lua b/lua/typstar/autosnippets.lua index 6eba3db..5c81d28 100644 --- a/lua/typstar/autosnippets.lua +++ b/lua/typstar/autosnippets.lua @@ -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 diff --git a/lua/typstar/utils.lua b/lua/typstar/utils.lua index 4118d7c..6aca0ae 100644 --- a/lua/typstar/utils.lua +++ b/lua/typstar/utils.lua @@ -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)