Skip to content

Commit a682790

Browse files
committed
Update for 2.0
1 parent 039a4a7 commit a682790

File tree

5 files changed

+86
-86
lines changed

5 files changed

+86
-86
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Go Plugin for Micro
22

3-
This repository holds the Go plugin for micro.
3+
This repository holds the Go plugin for micro. This plugin
4+
has been updated for use with micro 2.0.
45

5-
Install with `> plugin install go`. This plugin will let you
6-
automatically run `gofmt` and `goimports` from within micro.
6+
Install by cloning this into your plug directory:
77

8-
Use `> gorename newName` or F6 to use gorename.
8+
```
9+
git clone https://github.com/micro-editor/go-plugin ~/.config/micro/plug
10+
```

go.lua

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,81 @@
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")
25

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)
99

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)
1313

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)
1817
end
1918

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)
2625
end
2726
end
27+
return false
2828
end
2929

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
4436
end
37+
38+
bp.Buf:ReOpen()
4539
end
4640

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...")
5859
end
5960
end
6061

6162
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)
6865
end
6966

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()
7770
end
7871

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
8478
end
85-
return result
86-
end
8779

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

help/go-plugin.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,14 @@ to rename and enter the command `> gorename newName`.
2929

3030
You also press F6 (the default binding) to open a prompt to rename. You
3131
can rebind this in your `bindings.json` file with the action `go.gorename`.
32+
33+
The go plugin also provides an interface for using `gorename` if you have
34+
it installed. The gorename command behaves in two ways:
35+
36+
* With one argument: `> gorename newname` will rename the identifier
37+
under the cursor with the new name `newname`.
38+
39+
* With two arguments: `> gorename oldname newname` will rename the old
40+
identifier with the new one. This uses gorename's syntax for specifying
41+
the old identifier, so please see `gorename --help` for the details
42+
for how to specify the variable.

info.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name": "go",
3+
"description": "Go plugin",
4+
"website": "",
5+
"install": "",
6+
"version": "1.0.0",
7+
"require": [
8+
"micro >= 2.0.0"
9+
]
10+
}

repo.json

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)