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
23 lint("gcc", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
24 elseif ft == "c++" then
25 lint("gcc", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
27 lint("dmd", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", file}, "%f%(%l%):.+: %m")
28 elseif ft == "go" then
29 lint("gobuild", "go", {"build", "-o", devnull}, "%f:%l: %m")
30 lint("golint", "golint", {file}, "%f:%l:%d+: %m")
31 elseif ft == "java" then
32 lint("javac", "javac", {"-d", dir, file}, "%f:%l: error: %m")
33 elseif ft == "javascript" then
34 lint("jshint", "jshint", {file}, "%f: line %l,.+, %m")
35 elseif string.match(ft, "literate") then
36 lint("literate", "lit", {"-c", file}, "%f:%l:%m")
37 elseif ft == "lua" then
38 lint("luacheck", "luacheck", {"--no-color", file}, "%f:%l:%d+: %m")
39 elseif ft == "nim" then
40 lint("nim", "nim", {"check", "--listFullPaths", "--stdout", "--hints:off", file}, "%f.%l, %d+. %m")
41 elseif ft == "Objective-C" then
42 lint("clang", "xcrun", {"clang", "-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
43 elseif ft == "python" then
44 lint("pyflakes", "pyflakes", {file}, "%f:%l:.-:? %m")
45 lint("mypy", "mypy", {file}, "%f:%l: %m")
46 lint("pylint", "pylint", {"--output-format=parseable", "--reports=no", file}, "%f:%l: %m")
47 elseif ft == "shell" then
48 lint("shfmt", "shfmt", {file}, "%f:%l:%d+: %m")
49 elseif ft == "swift" and OS == "darwin" then
50 lint("switfc", "xcrun", {"swiftc", file}, "%f:%l:%d+:.+: %m")
51 elseif ft == "swift" and OS == "linux" then
52 lint("switfc", "swiftc", {file}, "%f:%l:%d+:.+: %m")
53 elseif ft == "yaml" then
54 lint("yaml", "yamllint", {"--format", "parsable", file}, "%f:%l:%d+:.+ %m")
59 if GetOption("linter") then
62 CurView():ClearAllGutterMessages()
66 function lint(linter, cmd, args, errorformat)
67 CurView():ClearGutterMessages(linter)
69 JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat)
72 function onExit(output, linter, errorformat)
73 local lines = split(output, "\n")
75 local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
76 for _,line in ipairs(lines) do
78 line = line:match("^%s*(.+)%s*$")
79 if string.find(line, regex) then
80 local file, line, msg = string.match(line, regex)
81 if basename(CurView().Buf.Path) == basename(file) then
82 CurView():GutterMessage(linter, tonumber(line), msg, 2)
88 function split(str, sep)
90 local regex = ("([^%s]+)"):format(sep)
91 for each in str:gmatch(regex) do
92 table.insert(result, each)
97 function basename(file)
99 if OS == "windows" then
102 local name = string.gsub(file, "(.*" .. sep .. ")(.*)", "%2")