Files
zyedidia.micro/runtime/plugins/go/go.lua
Zachary Yedidia 77b6c2c486 Auto prefix for plugin functions
YOu no longer have to prefix all functions in a plugin with the plugin
name (linter_onSave). This will now be done automatically using lua's
setfenv. When passing a function as a callback to a editor function,
make sure to prefix the plugin name (linter.onExit, or go.goimports) so
that micro knows which plugin to call the function from.
2016-06-21 17:49:57 -04:00

47 lines
1011 B
Lua

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
if GetOption("goimports") then
goimports()
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