diff options
author | rxi <rxi@users.noreply.github.com> | 2020-11-29 09:45:11 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-29 09:45:11 +0000 |
commit | a5257204d5be911f94ef8b50d8032e36832ccf8d (patch) | |
tree | 32c9e96dabbb67637f77ea1a01862b42cf1b4641 /plugins | |
parent | 70482a13de18ee37d677bdddf4df7a93a25d2384 (diff) | |
parent | bf115ce9e48b37122e51cf1df146621ea5143572 (diff) | |
download | lite-xl-plugins-a5257204d5be911f94ef8b50d8032e36832ccf8d.tar.gz lite-xl-plugins-a5257204d5be911f94ef8b50d8032e36832ccf8d.zip |
Merge pull request #103 from jeremypenner/master
Add rainbowparen plugin
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/rainbowparen.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/rainbowparen.lua b/plugins/rainbowparen.lua new file mode 100644 index 0000000..b2689f0 --- /dev/null +++ b/plugins/rainbowparen.lua @@ -0,0 +1,58 @@ +local tokenizer = require "core.tokenizer" +local style = require "core.style" +local common = require "core.common" + +local tokenize = tokenizer.tokenize +local closers = { + ["("] = ")", + ["["] = "]", + ["{"] = "}" +} +local function parenstyle(parenstack) + return "paren" .. ((#parenstack % 5) + 1) +end +function tokenizer.tokenize(syntax, text, state) + state = state or {} + local res, istate = tokenize(syntax, text, state.istate) + local parenstack = state.parenstack or "" + local newres = {} + -- split parens out + -- the stock tokenizer can't do this because it merges identical adjacent tokens + for i, type, text in tokenizer.each_token(res) do + if type == "normal" or type == "symbol" then + for normtext1, paren, normtext2 in text:gmatch("([^%(%[{}%]%)]*)([%(%[{}%]%)]?)([^%(%[{}%]%)]*)") do + if #normtext1 > 0 then + table.insert(newres, type) + table.insert(newres, normtext1) + end + if #paren > 0 then + if paren == parenstack:sub(-1) then -- expected closer + parenstack = parenstack:sub(1, -2) + table.insert(newres, parenstyle(parenstack)) + elseif closers[paren] then -- opener + table.insert(newres, parenstyle(parenstack)) + parenstack = parenstack .. closers[paren] + else -- unexpected closer + table.insert(newres, "paren_unbalanced") + end + table.insert(newres, paren) + end + if #normtext2 > 0 then + table.insert(newres, type) + table.insert(newres, normtext2) + end + end + else + table.insert(newres, type) + table.insert(newres, text) + end + end + return newres, { parenstack = parenstack, istate = istate } +end + +style.syntax.paren_unbalanced = style.syntax.paren_unbalanced or { common.color "#DC0408" } +style.syntax.paren1 = style.syntax.paren1 or { common.color "#FC6F71"} +style.syntax.paren2 = style.syntax.paren2 or { common.color "#fcb053"} +style.syntax.paren3 = style.syntax.paren3 or { common.color "#fcd476"} +style.syntax.paren4 = style.syntax.paren4 or { common.color "#52dab2"} +style.syntax.paren5 = style.syntax.paren5 or { common.color "#5a98cf"} |