1 if GetOption("linter") == nil then
2 AddOption("linter", true)
5 function linter_onSave()
6 if GetOption("linter") then
7 local ft = CurView().Buf.FileType
8 local file = CurView().Buf.Path
9 local devnull = "/dev/null"
10 if OS == "windows" then
14 linter_lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
15 linter_lint("golint", "golint " .. CurView().Buf.Path, "%f:%l:%d+: %m")
16 elseif ft == "Lua" then
17 linter_lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
18 elseif ft == "Python" then
19 linter_lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
21 linter_lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
23 linter_lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
24 elseif ft == "Java" then
25 linter_lint("javac", "javac " .. file, "%f:%l: error: %m")
26 elseif ft == "JavaScript" then
27 linter_lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
30 CurView():ClearAllGutterMessages()
34 function linter_lint(linter, cmd, errorformat)
35 CurView():ClearGutterMessages(linter)
37 JobStart(cmd, "", "", "linter_onExit", linter, errorformat)
40 function linter_onExit(output, linter, errorformat)
41 local lines = linter_split(output, "\n")
43 local regex = errorformat:gsub("%%f", "(.+)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
44 for _,line in ipairs(lines) do
46 line = line:match("^%s*(.+)%s*$")
47 if string.find(line, regex) then
48 local file, line, msg = string.match(line, regex)
49 if linter_basename(CurView().Buf.Path) == linter_basename(file) then
50 CurView():GutterMessage(linter, tonumber(line), msg, 2)
56 function linter_split(str, sep)
58 local regex = ("([^%s]+)"):format(sep)
59 for each in str:gmatch(regex) do
60 table.insert(result, each)
65 function linter_basename(file)
66 local name = string.gsub(file, "(.*/)(.*)", "%2")