Merge pull request #904 from jgmdev/PR/fix-syntax-optimization

syntax: fix conflicts introduced with #896
This commit is contained in:
Jefferson González 2022-03-29 15:44:19 -04:00 committed by GitHub
commit fac54d2ff4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 41 additions and 3 deletions

View File

@ -7,11 +7,49 @@ local plain_text_syntax = { name = "Plain Text", patterns = {}, symbols = {} }
function syntax.add(t)
-- this rule gives us a performance gain for the tokenizer in lines with
-- long amounts of consecutive spaces without affecting other patterns
-- the rule %s+ gives us a performance gain for the tokenizer in lines with
-- long amounts of consecutive spaces, to not affect other patterns we
-- insert it after any rule that starts with spaces to prevent conflicts
if t.patterns then
table.insert(t.patterns, 1, { pattern = "%s+", type = "normal" })
local temp_patterns = {}
::pattern_remove_loop::
for pos, pattern in ipairs(t.patterns) do
local pattern_str = ""
local ptype = pattern.pattern
and "pattern" or (pattern.regex and "regex" or nil)
if ptype then
if type(pattern[ptype]) == "table" then
pattern_str = pattern[ptype][1]
else
pattern_str = pattern[ptype]
end
if (ptype == "pattern" and(
pattern_str:find("^%^?%%s")
or
pattern_str:find("^%^?%s")
))
or
(ptype == "regex" and (
pattern_str:find("^%^?\\s")
or
pattern_str:find("^%^?%s")
))
then
table.insert(temp_patterns, table.remove(t.patterns, pos))
-- since we are removing from iterated table we need to start
-- from the beginning again to prevent any issues
goto pattern_remove_loop
end
end
end
for pos, pattern in ipairs(temp_patterns) do
table.insert(t.patterns, pos, pattern)
end
local pos = 1
if #temp_patterns > 0 then pos = #temp_patterns+1 end
table.insert(t.patterns, pos, { pattern = "%s+", type = "normal" })
end
table.insert(syntax.items, t)
end