summaryrefslogtreecommitdiff
path: root/.config/micro/plug/go/go.lua
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2022-07-16 18:13:16 -0400
committerJacob McDonnell <jacob@simplelittledream.com>2022-07-16 18:13:16 -0400
commit8fad9a5ecddc88d57a531e4b0084544984f23d25 (patch)
tree84954bc8219942aa56bc899330ccd0007bbe0ef0 /.config/micro/plug/go/go.lua
parent2887af7fcfb4d618dd13cf66ec2fbdbd84c7527c (diff)
Added profile and other missing configs
Diffstat (limited to '.config/micro/plug/go/go.lua')
-rw-r--r--.config/micro/plug/go/go.lua84
1 files changed, 84 insertions, 0 deletions
diff --git a/.config/micro/plug/go/go.lua b/.config/micro/plug/go/go.lua
new file mode 100644
index 0000000..d030316
--- /dev/null
+++ b/.config/micro/plug/go/go.lua
@@ -0,0 +1,84 @@
+VERSION = "2.0.2"
+
+local micro = import("micro")
+local config = import("micro/config")
+local shell = import("micro/shell")
+local buffer = import("micro/buffer")
+
+-- outside init because we want these options to take effect before
+-- buffers are initialized
+config.RegisterCommonOption("go", "goimports", false)
+config.RegisterCommonOption("go", "gofmt", true)
+
+function init()
+ config.MakeCommand("goimports", goimports, config.NoComplete)
+ config.MakeCommand("gofmt", gofmt, config.NoComplete)
+ config.MakeCommand("gorename", gorenameCmd, config.NoComplete)
+
+ config.AddRuntimeFile("go", config.RTHelp, "help/go-plugin.md")
+ config.TryBindKey("F6", "command-edit:gorename ", false)
+end
+
+function onSave(bp)
+ if bp.Buf:FileType() == "go" then
+ if bp.Buf.Settings["go.goimports"] then
+ goimports(bp)
+ elseif bp.Buf.Settings["go.gofmt"] then
+ gofmt(bp)
+ end
+ end
+ return true
+end
+
+function gofmt(bp)
+ bp:Save()
+ local _, err = shell.RunCommand("gofmt -w " .. bp.Buf.Path)
+ if err ~= nil then
+ micro.InfoBar():Error(err)
+ return
+ end
+
+ bp.Buf:ReOpen()
+end
+
+function gorenameCmd(bp, args)
+ micro.Log(args)
+ if #args == 0 then
+ micro.InfoBar():Message("Not enough arguments")
+ else
+ bp:Save()
+ local buf = bp.Buf
+ if #args == 1 then
+ local c = bp.Cursor
+ local loc = buffer.Loc(c.X, c.Y)
+ local offset = buffer.ByteOffset(loc, buf)
+ local cmdargs = {"--offset", buf.Path .. ":#" .. tostring(offset), "--to", args[1]}
+ shell.JobSpawn("gorename", cmdargs, nil, renameStderr, renameExit, bp)
+ else
+ local cmdargs = {"--from", args[1], "--to", args[2]}
+ shell.JobSpawn("gorename", cmdargs, nil, renameStderr, renameExit, bp)
+ end
+ micro.InfoBar():Message("Renaming...")
+ end
+end
+
+function renameStderr(err)
+ micro.Log(err)
+ micro.InfoBar():Message(err)
+end
+
+function renameExit(output, args)
+ local bp = args[1]
+ bp.Buf:ReOpen()
+end
+
+function goimports(bp)
+ bp:Save()
+ local _, err = shell.RunCommand("goimports -w " .. bp.Buf.Path)
+ if err ~= nil then
+ micro.InfoBar():Error(err)
+ return
+ end
+
+ bp.Buf:ReOpen()
+end