aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrxi <rxi@users.noreply.github.com>2020-04-12 11:31:29 +0100
committerGitHub <noreply@github.com>2020-04-12 11:31:29 +0100
commit84c40d0b607a30c20880fd156b9952d6292c5cb9 (patch)
treed0e7896044c8e2d68f3d33e31096b64dfdf4467b
parentaa5d8652937032880b4ffa63fb0e647b7cef23fd (diff)
parent1dae9a3b777426774578db91450df109d3e19194 (diff)
downloadlite-xl-plugins-84c40d0b607a30c20880fd156b9952d6292c5cb9.tar.gz
lite-xl-plugins-84c40d0b607a30c20880fd156b9952d6292c5cb9.zip
Merge pull request #7 from drmargarido/golang_tools
Added gofmt plugin with commands for gofmt, goimports and goreturns.
-rw-r--r--README.md1
-rw-r--r--gofmt.lua49
2 files changed, 50 insertions, 0 deletions
diff --git a/README.md b/README.md
index f1be5f0..2a38cdb 100644
--- a/README.md
+++ b/README.md
@@ -6,6 +6,7 @@ Plugins for the [lite text editor](https://github.com/rxi/lite)
Plugin | Description
-------|-----------------------------------------
+[`gofmt`](gofmt.lua?raw=1) | Auto-formats the current go file, adds the missing imports and the missing return cases
[`language_fe`](language_fe.lua?raw=1) | Syntax for the [fe](https://github.com/rxi/fe) programming language
[`language_go`](language_go.lua?raw=1) | Syntax for the [Go](https://golang.org/) programming language
[`language_jiyu`](language_jiyu.lua?raw=1) | Syntax for the [jiyu](https://github.com/machinamentum/jiyu) programming language
diff --git a/gofmt.lua b/gofmt.lua
new file mode 100644
index 0000000..11c90b9
--- /dev/null
+++ b/gofmt.lua
@@ -0,0 +1,49 @@
+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", {
+ ["gofmt:gofmt"] = function()
+ update_doc("gofmt", core.active_view.doc)
+ end,
+
+ ["gofmt:goimports"] = function()
+ update_doc("goimports", core.active_view.doc)
+ end,
+
+ ["gofmt:goreturns"] = function()
+ update_doc("goreturns", core.active_view.doc)
+ end,
+})
+
+keymap.add {
+ ["ctrl+i"] = "gofmt:gofmt",
+ ["ctrl+h"] = "gofmt:goimports",
+ ["ctrl+u"] = "gofmt:goreturns",
+}