mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-17 06:17:12 +09:00
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:
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@ if GetOption("linter") == nil then
|
||||
AddOption("linter", true)
|
||||
end
|
||||
|
||||
function linter_onSave()
|
||||
function onSave()
|
||||
if GetOption("linter") then
|
||||
local ft = CurView().Buf.FileType
|
||||
local file = CurView().Buf.Path
|
||||
@@ -11,34 +11,34 @@ function linter_onSave()
|
||||
devnull = "NUL"
|
||||
end
|
||||
if ft == "Go" then
|
||||
linter_lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
|
||||
linter_lint("golint", "golint " .. CurView().Buf.Path, "%f:%l:%d+: %m")
|
||||
lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
|
||||
lint("golint", "golint " .. CurView().Buf.Path, "%f:%l:%d+: %m")
|
||||
elseif ft == "Lua" then
|
||||
linter_lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
|
||||
lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
|
||||
elseif ft == "Python" then
|
||||
linter_lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
|
||||
lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
|
||||
elseif ft == "C" then
|
||||
linter_lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
|
||||
lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
|
||||
elseif ft == "D" then
|
||||
linter_lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
|
||||
lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
|
||||
elseif ft == "Java" then
|
||||
linter_lint("javac", "javac " .. file, "%f:%l: error: %m")
|
||||
lint("javac", "javac " .. file, "%f:%l: error: %m")
|
||||
elseif ft == "JavaScript" then
|
||||
linter_lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
|
||||
lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
|
||||
end
|
||||
else
|
||||
CurView():ClearAllGutterMessages()
|
||||
end
|
||||
end
|
||||
|
||||
function linter_lint(linter, cmd, errorformat)
|
||||
function lint(linter, cmd, errorformat)
|
||||
CurView():ClearGutterMessages(linter)
|
||||
|
||||
JobStart(cmd, "", "", "linter_onExit", linter, errorformat)
|
||||
JobStart(cmd, "", "", "linter.onExit", linter, errorformat)
|
||||
end
|
||||
|
||||
function linter_onExit(output, linter, errorformat)
|
||||
local lines = linter_split(output, "\n")
|
||||
function onExit(output, linter, errorformat)
|
||||
local lines = split(output, "\n")
|
||||
|
||||
local regex = errorformat:gsub("%%f", "(.+)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
|
||||
for _,line in ipairs(lines) do
|
||||
@@ -46,14 +46,14 @@ function linter_onExit(output, linter, errorformat)
|
||||
line = line:match("^%s*(.+)%s*$")
|
||||
if string.find(line, regex) then
|
||||
local file, line, msg = string.match(line, regex)
|
||||
if linter_basename(CurView().Buf.Path) == linter_basename(file) then
|
||||
if basename(CurView().Buf.Path) == basename(file) then
|
||||
CurView():GutterMessage(linter, tonumber(line), msg, 2)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function linter_split(str, sep)
|
||||
function split(str, sep)
|
||||
local result = {}
|
||||
local regex = ("([^%s]+)"):format(sep)
|
||||
for each in str:gmatch(regex) do
|
||||
@@ -62,7 +62,7 @@ function linter_split(str, sep)
|
||||
return result
|
||||
end
|
||||
|
||||
function linter_basename(file)
|
||||
function basename(file)
|
||||
local name = string.gsub(file, "(.*/)(.*)", "%2")
|
||||
return name
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user