1 if GetOption("linter") == nil then
2 AddOption("linter", true)
5 MakeCommand("lint", "linter.lintCommand", 0)
13 local ft = CurView().Buf:FileType()
14 local file = CurView().Buf.Path
15 local dir = DirectoryName(file)
16 if OS == "windows" then
22 lint("gobuild", "go", {"build", "-o", devnull}, "%f:%l: %m")
23 lint("golint", "golint", {file}, "%f:%l:%d+: %m")
24 elseif ft == "lua" then
25 lint("luacheck", "luacheck", {"--no-color", file}, "%f:%l:%d+: %m")
26 elseif ft == "python" then
27 lint("pyflakes", "pyflakes", {file}, "%f:%l:.-:? %m")
28 lint("mypy", "mypy", {file}, "%f:%l: %m")
29 lint("pylint", "pylint", {"--output-format=parseable", "--reports=no", file}, "%f:%l: %m")
31 lint("gcc", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
32 elseif ft == "c++" then
33 lint("gcc", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
34 elseif ft == "swift" then
35 lint("switfc", "xcrun", {"swiftc", file}, "%f:%l:%d+:.+: %m")
36 elseif ft == "Objective-C" then
37 lint("clang", "xcrun", {"clang", "-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
39 lint("dmd", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", file}, "%f%(%l%):.+: %m")
40 elseif ft == "java" then
41 lint("javac", "javac", {"-d", dir, file}, "%f:%l: error: %m")
42 elseif ft == "javascript" then
43 lint("jshint", "jshint", {file}, "%f: line %l,.+, %m")
44 elseif ft == "nim" then
45 lint("nim", "nim", {"check", "--listFullPaths", "--stdout", "--hints:off", file}, "%f.%l, %d+. %m")
46 elseif string.match(ft, "literate") then
47 lint("literate", "lit", {"-c", file}, "%f:%l:%m")
52 if GetOption("linter") then
55 CurView():ClearAllGutterMessages()
59 function lint(linter, cmd, args, errorformat)
60 CurView():ClearGutterMessages(linter)
62 JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat)
65 function onExit(output, linter, errorformat)
66 local lines = split(output, "\n")
68 local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
69 for _,line in ipairs(lines) do
71 line = line:match("^%s*(.+)%s*$")
72 if string.find(line, regex) then
73 local file, line, msg = string.match(line, regex)
74 if basename(CurView().Buf.Path) == basename(file) then
75 CurView():GutterMessage(linter, tonumber(line), msg, 2)
81 function split(str, sep)
83 local regex = ("([^%s]+)"):format(sep)
84 for each in str:gmatch(regex) do
85 table.insert(result, each)
90 function basename(file)
91 local name = string.gsub(file, "(.*/)(.*)", "%2")