Files
zyedidia.micro/runtime/plugins/linter/linter.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

69 lines
2.2 KiB
Lua

if GetOption("linter") == nil then
AddOption("linter", true)
end
function onSave()
if GetOption("linter") then
local ft = CurView().Buf.FileType
local file = CurView().Buf.Path
local devnull = "/dev/null"
if OS == "windows" then
devnull = "NUL"
end
if ft == "Go" then
lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
lint("golint", "golint " .. CurView().Buf.Path, "%f:%l:%d+: %m")
elseif ft == "Lua" then
lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
elseif ft == "Python" then
lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
elseif ft == "C" then
lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
elseif ft == "D" then
lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
elseif ft == "Java" then
lint("javac", "javac " .. file, "%f:%l: error: %m")
elseif ft == "JavaScript" then
lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
end
else
CurView():ClearAllGutterMessages()
end
end
function lint(linter, cmd, errorformat)
CurView():ClearGutterMessages(linter)
JobStart(cmd, "", "", "linter.onExit", linter, errorformat)
end
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
-- Trim whitespace
line = line:match("^%s*(.+)%s*$")
if string.find(line, regex) then
local file, line, msg = string.match(line, regex)
if basename(CurView().Buf.Path) == basename(file) then
CurView():GutterMessage(linter, tonumber(line), msg, 2)
end
end
end
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
function basename(file)
local name = string.gsub(file, "(.*/)(.*)", "%2")
return name
end