Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add godef and goreturns #3

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This repository holds the Go plugin for micro.

Install with `> plugin install go`. This plugin will let you
automatically run `gofmt` and `goimports` from within micro.
automatically run `gofmt`, `goimports`, `gorename`, and `goreturns`
from within micro.

Use `> gorename newName` or F6 to use gorename.
Use `> gorename newName` or F6 to use gorename. Use `> godef` or F8 to use godef.
78 changes: 65 additions & 13 deletions go.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
VERSION = "1.0.1"
VERSION = "1.1.1"

go_os = import("os")
go_filepath = import("filepath")
go_time = import("time")
go_strings = import("strings")
go_ioutil = import("io/ioutil")

if GetOption("goimports") == nil then
AddOption("goimports", false)
end
if GetOption("gofmt") == nil then
AddOption("gofmt", true)
end
if GetOption("goreturns") == nil then
AddOption("goreturns", false)
end

MakeCommand("goimports", "go.goimports", 0)
MakeCommand("gofmt", "go.gofmt", 0)
MakeCommand("gorename", "go.gorename", 0)
MakeCommand("godef", "go.godef", 0)
MakeCommand("goreturns", "go.goreturns", 0)
MakeCommand("gorename", "go.gorenameCmd", 0)

function onViewOpen(view)
if view.Buf:FileType() == "go" then
Expand All @@ -19,7 +31,9 @@ end

function onSave(view)
if CurView().Buf:FileType() == "go" then
if GetOption("goimports") then
if GetOption("goreturns") then
goreturns()
elseif GetOption("goimports") then
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this else chain could possibly work, but it does seem to call all three things if all three things are turned on.

goimports()
elseif GetOption("gofmt") then
gofmt()
Expand All @@ -30,9 +44,8 @@ end
function gofmt()
CurView():Save(false)
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
local result = handle:read("*a")
messenger:Message(handle:read("*a"))
handle:close()

CurView():ReOpen()
end

Expand Down Expand Up @@ -70,21 +83,60 @@ end
function goimports()
CurView():Save(false)
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
local result = split(handle:read("*a"), ":")
messenger:Message(handle:read("*a"))
handle:close()

CurView():ReOpen()
end

function split(str, sep)
local result = {}
local regex = ("([^%s]+)"):format(sep)
for each in str:gmatch(regex) do
table.insert(result, each)
function tmpfile()
local dir = go_os.TempDir()
-- todo: would be better if micro exposed ioutil.TempFile or
-- even crypto/rand or something
local name = "godef-" .. go_time.Now():UnixNano()
local joined = go_filepath.Join(dir, name)
return joined
end

function godef()
local file = tmpfile()
local v = CurView()
go_ioutil.WriteFile(file, v.Buf:SaveString(false), tonumber("600", 8))
local c = v.Cursor
local offset = ByteOffset(Loc(c.X, c.Y), v.Buf)
local handle = io.popen("godef -f " .. file .. " -o " .. offset, "r")
local resp = handle:read("*a")
handle:close()
go_os.Remove(file)
local parts = go_strings.Split(resp, ":")
if #parts < 3 then
messenger:Message(resp)
return
end
local dest = parts[1]
for i = 2, #parts-2, 1 do
dest = dest .. parts[i]
end
local pos = Loc(tonumber(parts[#parts])-1, tonumber(parts[#parts-1])-1)
if dest == file then
c:GotoLoc(pos)
v:Relocate()
return
end
return result
HandleCommand("tab")
v = CurView()
v:Open(dest)
v.Cursor:GotoLoc(pos)
v:Relocate()
end

function goreturns()
CurView():Save(false)
local handle = io.popen("goreturns -w " .. CurView().Buf.Path)
messenger:Message(handle:read("*a"))
handle:close()
CurView():ReOpen()
end

AddRuntimeFile("go", "help", "help/go-plugin.md")
BindKey("F6", "go.gorename")
MakeCommand("gorename", "go.gorenameCmd", 0)
BindKey("F8", "go.godef")
17 changes: 14 additions & 3 deletions help/go-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The go plugin provides some extra niceties for using micro with
the Go programming language. The main thing this plugin does is
run `gofmt` and `goimports` for you automatically. If you would also
like automatically error linting, check out the `linter` plugin.
The plugin also provides `gorename` functionality.
run `gofmt`, `goimports`, and `goreturns` for you automatically.
If you would also like automatically error linting, check out the `linter`
plugin. The plugin also provides `gorename` and `godef` functionality.

You can run

Expand All @@ -18,14 +18,25 @@ or
> goimports
```

or

```
> goreturns
```

To automatically run these when you save the file, use the following
options:

* `gofmt`: run gofmt on file saved. Default value: `on`
* `goimports`: run goimports on file saved. Default value: `off`
* `goreturns`: run goreturns on file saved. Default value: `off`

To use `gorename`, place your cursor over the variable you would like
to rename and enter the command `> gorename newName`.

You also press F6 (the default binding) to open a prompt to rename. You
can rebind this in your `bindings.json` file with the action `go.gorename`.

To use `godef`, place your cursor over the variable you would like
to jump to its definition for and enter `> godef`. You can also press F8.