micro/runtime/plugins/go/go.lua

47 lines
1,011 B
Lua
Raw Normal View History

2016-04-30 16:13:21 +00:00
if GetOption("goimports") == nil then
AddOption("goimports", false)
end
if GetOption("gofmt") == nil then
AddOption("gofmt", true)
end
MakeCommand("goimports", "go.goimports")
MakeCommand("gofmt", "go.gofmt")
function onSave()
if CurView().Buf.FileType == "Go" then
2016-04-30 16:13:21 +00:00
if GetOption("goimports") then
goimports()
2016-04-30 16:13:21 +00:00
elseif GetOption("gofmt") then
gofmt()
end
end
end
function gofmt()
CurView():Save()
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
local result = handle:read("*a")
handle:close()
CurView():ReOpen()
end
function goimports()
CurView():Save()
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
local result = split(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)
end
return result
end