|
1 |
| -VERSION = "1.0.1" |
| 1 | +local micro = import("micro") |
| 2 | +local config = import("micro/config") |
| 3 | +local shell = import("micro/shell") |
| 4 | +local buffer = import("micro/buffer") |
2 | 5 |
|
3 |
| -if GetOption("goimports") == nil then |
4 |
| - AddOption("goimports", false) |
5 |
| -end |
6 |
| -if GetOption("gofmt") == nil then |
7 |
| - AddOption("gofmt", true) |
8 |
| -end |
| 6 | +function init() |
| 7 | + config.RegisterCommonOption("goimports", false) |
| 8 | + config.RegisterCommonOption("gofmt", true) |
9 | 9 |
|
10 |
| -MakeCommand("goimports", "go.goimports", 0) |
11 |
| -MakeCommand("gofmt", "go.gofmt", 0) |
12 |
| -MakeCommand("gorename", "go.gorename", 0) |
| 10 | + config.MakeCommand("goimports", "go.goimports", config.NoComplete) |
| 11 | + config.MakeCommand("gofmt", "go.gofmt", config.NoComplete) |
| 12 | + config.MakeCommand("gorename", "go.gorename", config.NoComplete) |
13 | 13 |
|
14 |
| -function onViewOpen(view) |
15 |
| - if view.Buf:FileType() == "go" then |
16 |
| - SetLocalOption("tabstospaces", "off", view) |
17 |
| - end |
| 14 | + config.AddRuntimeFile("go-plugin", config.RTHelp, "help/go-plugin.md") |
| 15 | + config.TryBindKey("F6", "command-edit:gorename ", false) |
| 16 | + config.MakeCommand("gorename", "go.gorenameCmd", config.NoComplete) |
18 | 17 | end
|
19 | 18 |
|
20 |
| -function onSave(view) |
21 |
| - if CurView().Buf:FileType() == "go" then |
22 |
| - if GetOption("goimports") then |
23 |
| - goimports() |
24 |
| - elseif GetOption("gofmt") then |
25 |
| - gofmt() |
| 19 | +function onSave(bp) |
| 20 | + if bp.Buf:FileType() == "go" then |
| 21 | + if bp.Buf.Settings["goimports"] then |
| 22 | + goimports(bp) |
| 23 | + elseif bp.Buf.Settings["gofmt"] then |
| 24 | + gofmt(bp) |
26 | 25 | end
|
27 | 26 | end
|
| 27 | + return false |
28 | 28 | end
|
29 | 29 |
|
30 |
| -function gofmt() |
31 |
| - CurView():Save(false) |
32 |
| - local handle = io.popen("gofmt -w " .. CurView().Buf.Path) |
33 |
| - local result = handle:read("*a") |
34 |
| - handle:close() |
35 |
| - |
36 |
| - CurView():ReOpen() |
37 |
| -end |
38 |
| - |
39 |
| -function gorename() |
40 |
| - local res, canceled = messenger:Prompt("Rename to:", "", 0) |
41 |
| - if not canceled then |
42 |
| - gorenameCmd(res) |
43 |
| - CurView():Save(false) |
| 30 | +function gofmt(bp) |
| 31 | + bp:Save() |
| 32 | + local _, err = shell.RunCommand("gofmt -w " .. bp.Buf.Path) |
| 33 | + if err ~= nil then |
| 34 | + micro.InfoBar():Error(err) |
| 35 | + return |
44 | 36 | end
|
| 37 | + |
| 38 | + bp.Buf:ReOpen() |
45 | 39 | end
|
46 | 40 |
|
47 |
| -function gorenameCmd(res) |
48 |
| - CurView():Save(false) |
49 |
| - local v = CurView() |
50 |
| - local c = v.Cursor |
51 |
| - local buf = v.Buf |
52 |
| - local loc = Loc(c.X, c.Y) |
53 |
| - local offset = ByteOffset(loc, buf) |
54 |
| - if #res > 0 then |
55 |
| - local cmd = "gorename --offset " .. CurView().Buf.Path .. ":#" .. tostring(offset) .. " --to " .. res |
56 |
| - JobStart(cmd, "", "go.renameStderr", "go.renameExit") |
57 |
| - messenger:Message("Renaming...") |
| 41 | +function gorenameCmd(bp, args) |
| 42 | + micro.Log(args) |
| 43 | + if #args == 0 then |
| 44 | + micro.InfoBar():Message("Not enough arguments") |
| 45 | + else |
| 46 | + bp:Save() |
| 47 | + local buf = bp.Buf |
| 48 | + if #args == 1 then |
| 49 | + local c = bp.Cursor |
| 50 | + local loc = buffer.Loc(c.X, c.Y) |
| 51 | + local offset = buffer.ByteOffset(loc, buf) |
| 52 | + local cmdargs = {"--offset", buf.Path .. ":#" .. tostring(offset), "--to", args[1]} |
| 53 | + shell.JobSpawn("gorename", cmdargs, "", "go.renameStderr", "go.renameExit", bp) |
| 54 | + else |
| 55 | + local cmdargs = {"--from", args[1], "--to", args[2]} |
| 56 | + shell.JobSpawn("gorename", cmdargs, "", "go.renameStderr", "go.renameExit", bp) |
| 57 | + end |
| 58 | + micro.InfoBar():Message("Renaming...") |
58 | 59 | end
|
59 | 60 | end
|
60 | 61 |
|
61 | 62 | function renameStderr(err)
|
62 |
| - messenger:Error(err) |
63 |
| -end |
64 |
| - |
65 |
| -function renameExit() |
66 |
| - CurView():ReOpen() |
67 |
| - messenger:Message("Done") |
| 63 | + micro.Log(err) |
| 64 | + micro.InfoBar():Message(err) |
68 | 65 | end
|
69 | 66 |
|
70 |
| -function goimports() |
71 |
| - CurView():Save(false) |
72 |
| - local handle = io.popen("goimports -w " .. CurView().Buf.Path) |
73 |
| - local result = split(handle:read("*a"), ":") |
74 |
| - handle:close() |
75 |
| - |
76 |
| - CurView():ReOpen() |
| 67 | +function renameExit(output, args) |
| 68 | + local bp = args[1] |
| 69 | + bp.Buf:ReOpen() |
77 | 70 | end
|
78 | 71 |
|
79 |
| -function split(str, sep) |
80 |
| - local result = {} |
81 |
| - local regex = ("([^%s]+)"):format(sep) |
82 |
| - for each in str:gmatch(regex) do |
83 |
| - table.insert(result, each) |
| 72 | +function goimports(bp) |
| 73 | + bp:Save() |
| 74 | + local _, err = shell.RunCommand("goimports -w " .. bp.Buf.Path) |
| 75 | + if err ~= nil then |
| 76 | + micro.InfoBar():Error(err) |
| 77 | + return |
84 | 78 | end
|
85 |
| - return result |
86 |
| -end |
87 | 79 |
|
88 |
| -AddRuntimeFile("go", "help", "help/go-plugin.md") |
89 |
| -BindKey("F6", "go.gorename") |
90 |
| -MakeCommand("gorename", "go.gorenameCmd", 0) |
| 80 | + bp.Buf:ReOpen() |
| 81 | +end |
0 commit comments