diff options
author | JobinsJC <jobinsjjc1492@protonmail.com> | 2021-11-22 12:03:41 +0900 |
---|---|---|
committer | JobinsJC <jobinsjjc1492@protonmail.com> | 2021-11-22 12:03:41 +0900 |
commit | e8b11ef477ec6a201d274640785991dddaea14e1 (patch) | |
tree | 1f4188d13ce6d5fa5370a328525713cc4ba7a636 /plugins/formatter.lua | |
parent | bc0a289a79294445de9b0001bdaada93d801dadd (diff) | |
download | lite-xl-plugins-e8b11ef477ec6a201d274640785991dddaea14e1.tar.gz lite-xl-plugins-e8b11ef477ec6a201d274640785991dddaea14e1.zip |
Add support for Rust,C,J++,Javascript,JSON,Objective-C/Protobuf,C#
Diffstat (limited to 'plugins/formatter.lua')
-rw-r--r-- | plugins/formatter.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/plugins/formatter.lua b/plugins/formatter.lua new file mode 100644 index 0000000..4a4f215 --- /dev/null +++ b/plugins/formatter.lua @@ -0,0 +1,58 @@ +-- mod-version:2 -- lite-xl 2.0 +local core = require "core" +local command = require "core.command" +local keymap = require "core.keymap" + +local function exec(cmd) + local fp = io.popen(cmd, "r") + local res = fp:read("*a") + local success = fp:close() + return res:gsub("%\n$", ""), success +end + +local function get_cmd_text(cmd, doc) + local active_filename = doc and system.absolute_path(doc.filename or "") + return exec(string.format("%s %s", cmd, active_filename)) +end + +local function update_doc(cmd, doc) + local text, success = get_cmd_text(cmd, doc) + if success == nil then + local err_text = "Command '%s' not found in the system" + core.error(string.format(err_text, cmd)) + return + end + + local sel = { doc:get_selection() } + doc:remove(1, 1, math.huge, math.huge) + doc:insert(1, 1, text) + doc:set_selection(table.unpack(sel)) +end + +command.add("core.docview", { + ["format:gofmt"] = function() + update_doc("gofmt", core.active_view.doc) + end, + + ["format:gofmt:goimports"] = function() + update_doc("goimports", core.active_view.doc) + end, + + ["format:gofmt:goreturns"] = function() + update_doc("goreturns", core.active_view.doc) + end, + + ["format:rustfmt"] = function() + update_doc("rustfmt", core.active_view.doc) + end, + + ["format:clang-format"] = function() + update_doc("clang-format", core.active_view.doc) + end, +}) + +keymap.add { + ["ctrl+i"] = "format:gofmt", + ["ctrl+h"] = "format:gofmt:goimports", + ["ctrl+u"] = "format:gofmt:goreturns", +} |