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.
This commit is contained in:
Zachary Yedidia
2016-06-21 17:49:57 -04:00
parent c1dadbd0c7
commit 77b6c2c486
5 changed files with 48 additions and 33 deletions

View File

@@ -5,20 +5,20 @@ if GetOption("gofmt") == nil then
AddOption("gofmt", true)
end
MakeCommand("goimports", "go_goimports")
MakeCommand("gofmt", "go_gofmt")
MakeCommand("goimports", "go.goimports")
MakeCommand("gofmt", "go.gofmt")
function go_onSave()
function onSave()
if CurView().Buf.FileType == "Go" then
if GetOption("goimports") then
go_goimports()
goimports()
elseif GetOption("gofmt") then
go_gofmt()
gofmt()
end
end
end
function go_gofmt()
function gofmt()
CurView():Save()
local handle = io.popen("gofmt -w " .. CurView().Buf.Path)
local result = handle:read("*a")
@@ -27,16 +27,16 @@ function go_gofmt()
CurView():ReOpen()
end
function go_goimports()
function goimports()
CurView():Save()
local handle = io.popen("goimports -w " .. CurView().Buf.Path)
local result = go_split(handle:read("*a"), ":")
local result = split(handle:read("*a"), ":")
handle:close()
CurView():ReOpen()
end
function go_split(str, sep)
function split(str, sep)
local result = {}
local regex = ("([^%s]+)"):format(sep)
for each in str:gmatch(regex) do