Files
zyedidia.micro/runtime/plugins/go/go.lua
2016-08-19 18:47:37 -07:00

51 lines
1.1 KiB
Lua

if GetOption("goimports") == nil then
AddOption("goimports", false)
end
if GetOption("gofmt") == nil then
AddOption("gofmt", true)
end
if CurView().Buf.FileType == "Go" then
SetOption("tabstospaces", "off")
end
MakeCommand("goimports", "go.goimports", 0)
MakeCommand("gofmt", "go.gofmt", 0)
function onSave()
if CurView().Buf.FileType == "Go" then
if GetOption("goimports") then
goimports()
elseif GetOption("gofmt") then
gofmt()
end
end
end
function gofmt()
CurView():Save(false)
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
local result = handle:read("*a")
handle:close()
CurView():ReOpen()
end
function goimports()
CurView():Save(false)
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