From 0d36e54b80dc30c38b804c377153ba4014813ca5 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Thu, 1 Mar 2018 16:34:12 -0700 Subject: [PATCH 1/4] add godef and goreturns --- README.md | 3 +- go.lua | 78 +++++++++++++++++++++++++++++++++++++++-------- help/go-plugin.md | 17 +++++++++-- 3 files changed, 81 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 867574b..38f723e 100644 --- a/README.md +++ b/README.md @@ -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`, `goreturns`, and `godef` +from within micro. Use `> gorename newName` or F6 to use gorename. diff --git a/go.lua b/go.lua index c9531ce..a1413e4 100644 --- a/go.lua +++ b/go.lua @@ -1,4 +1,10 @@ -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) @@ -6,10 +12,16 @@ 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 @@ -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 goimports() elseif GetOption("gofmt") then gofmt() @@ -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 @@ -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") diff --git a/help/go-plugin.md b/help/go-plugin.md index affa4bd..95900ca 100644 --- a/help/go-plugin.md +++ b/help/go-plugin.md @@ -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 @@ -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 goimports 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. + From 29c363ecf944b87b1eaada6745e64b95e3df54a1 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Thu, 1 Mar 2018 17:10:11 -0700 Subject: [PATCH 2/4] typo fix --- help/go-plugin.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/help/go-plugin.md b/help/go-plugin.md index 95900ca..c4f2ddd 100644 --- a/help/go-plugin.md +++ b/help/go-plugin.md @@ -29,7 +29,7 @@ options: * `gofmt`: run gofmt on file saved. Default value: `on` * `goimports`: run goimports on file saved. Default value: `off` -* `goreturns`: 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`. From a0a739486f1a3ecf4c88b62d5db882a84f763fe3 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Thu, 1 Mar 2018 17:23:52 -0700 Subject: [PATCH 3/4] whitespace fix --- go.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.lua b/go.lua index a1413e4..31068aa 100644 --- a/go.lua +++ b/go.lua @@ -126,7 +126,7 @@ function godef() v = CurView() v:Open(dest) v.Cursor:GotoLoc(pos) - v:Relocate() + v:Relocate() end function goreturns() From dbec2398cf2ee8440db09d574d2fe10dbede5f38 Mon Sep 17 00:00:00 2001 From: JT Olds Date: Mon, 5 Mar 2018 12:05:49 -0700 Subject: [PATCH 4/4] update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 38f723e..1ecc112 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This repository holds the Go plugin for micro. Install with `> plugin install go`. This plugin will let you -automatically run `gofmt`, `goimports`, `gorename`, `goreturns`, and `godef` +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.