diff options
author | rxi <rxi@users.noreply.github.com> | 2020-05-05 20:28:58 +0100 |
---|---|---|
committer | rxi <rxi@users.noreply.github.com> | 2020-05-05 20:29:24 +0100 |
commit | 9f21c411a3ddc5afddcf3dd05b85f6cc92a48858 (patch) | |
tree | 471f4bef12eac7c4bb32c77a9f95fad15e9447b7 /plugins | |
parent | e6e350b74dae1257e68e7069e9b82935bc31d6e4 (diff) | |
download | lite-xl-plugins-9f21c411a3ddc5afddcf3dd05b85f6cc92a48858.tar.gz lite-xl-plugins-9f21c411a3ddc5afddcf3dd05b85f6cc92a48858.zip |
Added plugins `gitstatus` and `lfautoinsert`
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/gitstatus.lua | 61 | ||||
-rw-r--r-- | plugins/lfautoinsert.lua | 54 |
2 files changed, 115 insertions, 0 deletions
diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua new file mode 100644 index 0000000..4fe043f --- /dev/null +++ b/plugins/gitstatus.lua @@ -0,0 +1,61 @@ +local core = require "core" +local config = require "core.config" +local style = require "core.style" +local StatusView = require "core.statusview" + + +local git = { + branch = nil, + inserts = 0, + deletes = 0, +} + +core.add_thread(function() + while true do + if system.get_file_info(".git") then + -- get branch name + local fp = io.popen("git rev-parse --abbrev-ref HEAD") + git.branch = fp:read("*l") + fp:close() + + -- get diff + local fp = io.popen("git diff --stat") + local last_line = "" + for line in fp:lines() do last_line = line end + fp:close() + git.inserts = tonumber(last_line:match("(%d+) ins")) or 0 + git.deletes = tonumber(last_line:match("(%d+) del")) or 0 + + else + git.branch = nil + end + + coroutine.yield(config.project_scan_rate) + end +end) + + +local get_items = StatusView.get_items + +function StatusView:get_items() + if not git.branch then + return get_items(self) + end + local left, right = get_items(self) + + local t = { + style.dim, self.separator, + (git.inserts ~= 0 or git.deletes ~= 0) and style.accent or style.text, + git.branch, + style.dim, " ", + git.inserts ~= 0 and style.accent or style.text, "+", git.inserts, + style.dim, " / ", + git.deletes ~= 0 and style.accent or style.text, "-", git.deletes, + } + for _, item in ipairs(t) do + table.insert(right, item) + end + + return left, right +end + diff --git a/plugins/lfautoinsert.lua b/plugins/lfautoinsert.lua new file mode 100644 index 0000000..64006b9 --- /dev/null +++ b/plugins/lfautoinsert.lua @@ -0,0 +1,54 @@ +local core = require "core" +local command = require "core.command" +local config = require "core.config" +local keymap = require "core.keymap" + +config.autoinsert_map = { + ["{\n"] = "}", + ["%(\n"] = ")", + ["%f[[]%[\n"] = "]", + ["%[%[\n"] = "]]", + ["=\n"] = false, + [":\n"] = false, + ["^#if"] = "#endif", + ["^#else"] = "#endif", + ["%f[%w]do\n"] = "end", + ["%f[%w]then\n"] = "end", + ["%f[%w]else\n"] = "end", + ["%f[%w]repeat\n"] = "until", + ["%f[%w]function.*%)\n"] = "end", +} + + +local function indent_size(doc, line) + local text = doc.lines[line] or "" + local s, e = text:find("^[\t ]*") + return e - s +end + +command.add("core.docview", { + ["autoinsert:newline"] = function() + command.perform("doc:newline") + + local doc = core.active_view.doc + local line = doc:get_selection() + local text = doc.lines[line - 1] + + for ptn, close in pairs(config.autoinsert_map) do + if text:find(ptn) then + if close + and indent_size(doc, line + 1) <= indent_size(doc, line - 1) + then + command.perform("doc:newline") + core.active_view:on_text_input(close) + command.perform("doc:move-to-previous-line") + end + command.perform("doc:indent") + end + end + end +}) + +keymap.add { + ["return"] = { "command:submit", "autoinsert:newline" } +} |