]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/go/go.lua
Merge pull request #315 from boombuler/pascal
[micro.git] / runtime / plugins / go / go.lua
1 if GetOption("goimports") == nil then
2     AddOption("goimports", false)
3 end
4 if GetOption("gofmt") == nil then
5     AddOption("gofmt", true)
6 end
7
8 MakeCommand("goimports", "go.goimports", 0)
9 MakeCommand("gofmt", "go.gofmt", 0)
10
11 function onViewOpen(view)
12     if view.Buf:FileType() == "go" then
13         SetLocalOption("tabstospaces", "off", view)
14     end
15 end
16
17 function onSave(view)
18     if CurView().Buf:FileType() == "go" then
19         if GetOption("goimports") then
20             goimports()
21         elseif GetOption("gofmt") then
22             gofmt()
23         end
24     end
25 end
26
27 function gofmt()
28     CurView():Save(false)
29     local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
30     local result = handle:read("*a")
31     handle:close()
32
33     CurView():ReOpen()
34 end
35
36 function goimports()
37     CurView():Save(false)
38     local handle = io.popen("goimports -w " .. CurView().Buf.Path)
39     local result = split(handle:read("*a"), ":")
40     handle:close()
41
42     CurView():ReOpen()
43 end
44
45 function split(str, sep)
46     local result = {}
47     local regex = ("([^%s]+)"):format(sep)
48     for each in str:gmatch(regex) do
49         table.insert(result, each)
50     end
51     return result
52 end