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
|
|
|
|
|
|
2016-08-17 18:16:27 +00:00
|
|
|
MakeCommand("goimports", "go.goimports", 0)
|
|
|
|
|
MakeCommand("gofmt", "go.gofmt", 0)
|
2016-05-30 17:38:50 +00:00
|
|
|
|
2016-08-25 20:01:42 +00:00
|
|
|
function onViewOpen(view)
|
|
|
|
|
if view.Buf:FileType() == "go" then
|
|
|
|
|
SetLocalOption("tabstospaces", "off", view)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-08-25 20:35:03 +00:00
|
|
|
function onSave(view)
|
2016-08-25 20:01:42 +00:00
|
|
|
if CurView().Buf:FileType() == "go" then
|
2016-04-30 16:13:21 +00:00
|
|
|
if GetOption("goimports") then
|
2016-06-21 21:49:57 +00:00
|
|
|
goimports()
|
2016-04-30 16:13:21 +00:00
|
|
|
elseif GetOption("gofmt") then
|
2016-06-21 21:49:57 +00:00
|
|
|
gofmt()
|
2016-04-27 18:12:32 +00:00
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-06-21 21:49:57 +00:00
|
|
|
function gofmt()
|
2016-08-17 18:16:27 +00:00
|
|
|
CurView():Save(false)
|
2016-06-08 17:29:24 +00:00
|
|
|
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
|
2016-04-27 18:12:32 +00:00
|
|
|
local result = handle:read("*a")
|
|
|
|
|
handle:close()
|
2016-05-30 17:38:50 +00:00
|
|
|
|
2016-06-08 17:29:24 +00:00
|
|
|
CurView():ReOpen()
|
2016-04-27 18:12:32 +00:00
|
|
|
end
|
|
|
|
|
|
2016-06-21 21:49:57 +00:00
|
|
|
function goimports()
|
2016-08-17 18:16:27 +00:00
|
|
|
CurView():Save(false)
|
2016-06-08 17:29:24 +00:00
|
|
|
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
|
2016-06-21 21:49:57 +00:00
|
|
|
local result = split(handle:read("*a"), ":")
|
2016-04-27 18:12:32 +00:00
|
|
|
handle:close()
|
2016-05-30 17:38:50 +00:00
|
|
|
|
2016-06-08 17:29:24 +00:00
|
|
|
CurView():ReOpen()
|
2016-04-27 18:12:32 +00:00
|
|
|
end
|
|
|
|
|
|
2016-06-21 21:49:57 +00:00
|
|
|
function split(str, sep)
|
2016-04-29 00:43:45 +00:00
|
|
|
local result = {}
|
|
|
|
|
local regex = ("([^%s]+)"):format(sep)
|
|
|
|
|
for each in str:gmatch(regex) do
|
|
|
|
|
table.insert(result, each)
|
|
|
|
|
end
|
|
|
|
|
return result
|
2016-04-27 18:12:32 +00:00
|
|
|
end
|