Skip to content

Commit 8074c26

Browse files
committed
Initial commit
0 parents  commit 8074c26

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Go Plugin for Micro
2+
3+
This repository holds the Go plugin for micro.
4+
5+
Install with `> plugin install go`. This plugin will let you
6+
automatically run `gofmt` and `goimports` from within micro.

go.lua

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
VERSION = "1.0.0"
2+
3+
if GetOption("goimports") == nil then
4+
AddOption("goimports", false)
5+
end
6+
if GetOption("gofmt") == nil then
7+
AddOption("gofmt", true)
8+
end
9+
10+
MakeCommand("goimports", "go.goimports", 0)
11+
MakeCommand("gofmt", "go.gofmt", 0)
12+
13+
function onViewOpen(view)
14+
if view.Buf:FileType() == "go" then
15+
SetLocalOption("tabstospaces", "off", view)
16+
end
17+
end
18+
19+
function onSave(view)
20+
if CurView().Buf:FileType() == "go" then
21+
if GetOption("goimports") then
22+
goimports()
23+
elseif GetOption("gofmt") then
24+
gofmt()
25+
end
26+
end
27+
end
28+
29+
function gofmt()
30+
CurView():Save(false)
31+
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
32+
local result = handle:read("*a")
33+
handle:close()
34+
35+
CurView():ReOpen()
36+
end
37+
38+
function goimports()
39+
CurView():Save(false)
40+
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
41+
local result = split(handle:read("*a"), ":")
42+
handle:close()
43+
44+
CurView():ReOpen()
45+
end
46+
47+
function split(str, sep)
48+
local result = {}
49+
local regex = ("([^%s]+)"):format(sep)
50+
for each in str:gmatch(regex) do
51+
table.insert(result, each)
52+
end
53+
return result
54+
end
55+
56+
AddRuntimeFile("go", "help", "help/go-plugin.md")

help/go-plugin.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Go Plugin
2+
3+
The go plugin provides some extra niceties for using micro with
4+
the Go programming language. The main thing this plugin does is
5+
run `gofmt` and `goimports` for you automatically. If you would also
6+
like automatically error linting, check out the `linter` plugin.
7+
8+
You can run
9+
10+
```
11+
> gofmt
12+
```
13+
14+
or
15+
16+
```
17+
> goimports
18+
```
19+
20+
To automatically run these when you save the file, use the following
21+
options:
22+
23+
* `gofmt`: run gofmt on file saved. Default value: `on`
24+
* `goimports`: run goimports on file saved. Default value: `off`

0 commit comments

Comments
 (0)