From 8fad9a5ecddc88d57a531e4b0084544984f23d25 Mon Sep 17 00:00:00 2001 From: Jacob McDonnell Date: Sat, 16 Jul 2022 18:13:16 -0400 Subject: Added profile and other missing configs --- .config/micro/plug/go/go.lua | 84 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 .config/micro/plug/go/go.lua (limited to '.config/micro/plug/go/go.lua') 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 -- cgit v1.2.3