-
Notifications
You must be signed in to change notification settings - Fork 6
/
go.lua
84 lines (72 loc) · 2.23 KB
/
go.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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