]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/linter/linter.lua
9c68b97d8c379954cfe0803e3f6f0d1f1e938a9a
[micro.git] / runtime / plugins / linter / linter.lua
1 if GetOption("linter") == nil then
2     AddOption("linter", true)
3 end
4
5 MakeCommand("lint", "linter.lintCommand", 0)
6
7 function lintCommand()
8     CurView():Save(false)
9     runLinter()
10 end
11
12 function runLinter()
13     local ft = CurView().Buf:FileType()
14     local file = CurView().Buf.Path
15     local devnull = "/dev/null"
16     local temp = os.getenv("TMPDIR")
17     if OS == "windows" then
18         devnull = "NUL"
19         temp = os.getenv("TEMP")
20     end
21     if ft == "go" then
22         lint("gobuild", "go", {"build", "-o", devnull}, "%f:%l: %m")
23         lint("golint", "golint", {CurView().Buf.Path}, "%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")
30     elseif ft == "c" then
31         lint("gcc", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
32     elseif ft == "swift" then
33         lint("switfc", "xcrun", {"swiftc", file}, "%f:%l:%d+:.+: %m")
34     elseif ft == "Objective-C" then
35         lint("clang", "xcrun", {"clang", "-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
36     elseif ft == "d" then
37         lint("dmd", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", file}, "%f%(%l%):.+: %m")
38     elseif ft == "java" then
39         lint("javac", "javac", {"-d", temp, file}, "%f:%l: error: %m")
40     elseif ft == "javascript" then
41         lint("jshint", "jshint", {file}, "%f: line %l,.+, %m")
42     elseif ft == "nim" then
43         lint("nim", "nim", {"check", "--listFullPaths", "--stdout", "--hints:off", file}, "%f\\(%l, %d\\) .+: %m")
44     end
45 end
46
47 function onSave(view)
48     if GetOption("linter") then
49         runLinter()
50     else
51         CurView():ClearAllGutterMessages()
52     end
53 end
54
55 function lint(linter, cmd, args, errorformat)
56     CurView():ClearGutterMessages(linter)
57
58     JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat)
59 end
60
61 function onExit(output, linter, errorformat)
62     local lines = split(output, "\n")
63
64     local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
65     for _,line in ipairs(lines) do
66         -- Trim whitespace
67         line = line:match("^%s*(.+)%s*$")
68         if string.find(line, regex) then
69             print(regex)
70             local file, line, msg = string.match(line, regex)
71             if basename(CurView().Buf.Path) == basename(file) then
72                 CurView():GutterMessage(linter, tonumber(line), msg, 2)
73             end
74         end
75     end
76 end
77
78 function split(str, sep)
79     local result = {}
80     local regex = ("([^%s]+)"):format(sep)
81     for each in str:gmatch(regex) do
82         table.insert(result, each)
83     end
84     return result
85 end
86
87 function basename(file)
88     local name = string.gsub(file, "(.*/)(.*)", "%2")
89     return name
90 end