Files
zyedidia.micro/runtime/plugins/go/go.lua
2016-05-05 12:53:27 -04:00

43 lines
1.0 KiB
Lua

if GetOption("goimports") == nil then
AddOption("goimports", false)
end
if GetOption("gofmt") == nil then
AddOption("gofmt", true)
end
function go_onSave()
if view.Buf.Filetype == "Go" then
if GetOption("goimports") then
go_goimports()
elseif GetOption("gofmt") then
go_gofmt()
end
linter_lint("go build", "go build -o /dev/null 2>&1", "%f:%l: %m")
linter_lint("go lint", "golint " .. view.Buf.Path, "%f:%l:%d+: %m")
view:ReOpen()
end
end
function go_gofmt()
local handle = io.popen("gofmt -w " .. view.Buf.Path)
local result = handle:read("*a")
handle:close()
end
function go_goimports()
local handle = io.popen("goimports -w " .. view.Buf.Path)
local result = go_split(handle:read("*a"), ":")
handle:close()
end
function go_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