diff options
Diffstat (limited to 'plugins/rainbowparen.lua')
-rw-r--r-- | plugins/rainbowparen.lua | 61 |
1 files changed, 52 insertions, 9 deletions
diff --git a/plugins/rainbowparen.lua b/plugins/rainbowparen.lua index 52e9d50..6ca4cb4 100644 --- a/plugins/rainbowparen.lua +++ b/plugins/rainbowparen.lua @@ -1,7 +1,23 @@ --- mod-version:2 -- lite-xl 2.0 -local tokenizer = require "core.tokenizer" +-- mod-version:3 +local core = require "core" local style = require "core.style" +local config = require "core.config" local common = require "core.common" +local command = require "core.command" +local tokenizer = require "core.tokenizer" +local Highlighter = require "core.doc.highlighter" + +config.plugins.rainbowparen = common.merge({ + enabled = true, + parens = 5 +}, config.plugins.rainbowparen) + +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"} local tokenize = tokenizer.tokenize local closers = { @@ -9,10 +25,15 @@ local closers = { ["["] = "]", ["{"] = "}" } + local function parenstyle(parenstack) - return "paren" .. ((#parenstack % 5) + 1) + return "paren" .. ((#parenstack % config.plugins.rainbowparen.parens) + 1) end + function tokenizer.tokenize(syntax, text, state) + if not config.plugins.rainbowparen.enabled then + return tokenize(syntax, text, state) + end state = state or {} local res, istate = tokenize(syntax, text, state.istate) local parenstack = state.parenstack or "" @@ -51,9 +72,31 @@ function tokenizer.tokenize(syntax, text, state) 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"} +local function toggle_rainbowparen(enabled) + config.plugins.rainbowparen.enabled = enabled + for _, doc in ipairs(core.docs) do + doc.highlighter = Highlighter(doc) + doc:reset_syntax() + end +end + +-- The config specification used by the settings gui +config.plugins.rainbowparen.config_spec = { + name = "Rainbow Parentheses", + { + label = "Enable", + description = "Activates rainbow parenthesis coloring by default.", + path = "enabled", + type = "toggle", + default = true, + on_apply = function(enabled) + toggle_rainbowparen(enabled) + end + } +} + +command.add(nil, { + ["rainbow-parentheses:toggle"] = function() + toggle_rainbowparen(not config.plugins.rainbowparen.enabled) + end +}) |