]> git.lizzy.rs Git - micro.git/blob - runtime/plugins/linter/linter.lua
Merge pull request #883 from onodera-punpun/lint
[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 dir = DirectoryName(file)
16     if OS == "windows" then
17         devnull = "NUL"
18     else
19         devnull = "/dev/null"
20     end
21
22     if ft == "c" 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")
26     elseif ft == "d" then
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")
55     end
56 end
57
58 function onSave(view)
59     if GetOption("linter") then
60         runLinter()
61     else
62         CurView():ClearAllGutterMessages()
63     end
64 end
65
66 function lint(linter, cmd, args, errorformat)
67     CurView():ClearGutterMessages(linter)
68
69     JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat)
70 end
71
72 function onExit(output, linter, errorformat)
73     local lines = split(output, "\n")
74
75     local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
76     for _,line in ipairs(lines) do
77         -- Trim whitespace
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)
83             end
84         end
85     end
86 end
87
88 function split(str, sep)
89     local result = {}
90     local regex = ("([^%s]+)"):format(sep)
91     for each in str:gmatch(regex) do
92         table.insert(result, each)
93     end
94     return result
95 end
96
97 function basename(file)
98     local sep = "/"
99     if OS == "windows" then
100         sep = "\\"
101     end
102     local name = string.gsub(file, "(.*" .. sep .. ")(.*)", "%2")
103     return name
104 end