]> git.lizzy.rs Git - micro.git/blobdiff - runtime/plugins/linter/linter.lua
Adding additional Python and Objective-C linting (#524)
[micro.git] / runtime / plugins / linter / linter.lua
index 9767da5d80c816562678b15617c21f02870bb8fb..88892797b5a48f1611cc973c96c0f7c1742d13f5 100644 (file)
@@ -2,56 +2,75 @@ if GetOption("linter") == nil then
     AddOption("linter", true)
 end
 
-function linter_onSave()
+MakeCommand("lint", "linter.lintCommand", 0)
+
+function lintCommand()
+    CurView():Save(false)
+    runLinter()
+end
+
+function runLinter()
+    local ft = CurView().Buf:FileType()
+    local file = CurView().Buf.Path
+    local devnull = "/dev/null"
+    local temp = os.getenv("TMPDIR")
+    if OS == "windows" then
+        devnull = "NUL"
+        temp = os.getenv("TEMP")
+    end
+    if ft == "go" then
+        lint("gobuild", "go", {"build", "-o", devnull}, "%f:%l: %m")
+        lint("golint", "golint", {CurView().Buf.Path}, "%f:%l:%d+: %m")
+    elseif ft == "lua" then
+        lint("luacheck", "luacheck", {"--no-color", file}, "%f:%l:%d+: %m")
+    elseif ft == "python" then
+        lint("pyflakes", "pyflakes", {file}, "%f:%l:.-:? %m")
+        lint("mypy", "mypy", {file}, "%f:%l: %m")
+        lint("pylint", "pylint", {"--output-format=parseable", "--reports=no", file}, "%f:%l: %m")
+    elseif ft == "c" then
+        lint("gcc", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
+    elseif ft == "Objective-C" then
+        lint("clang", "xcrun", {"clang", "-fsyntax-only", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m")
+    elseif ft == "d" then
+        lint("dmd", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", file}, "%f%(%l%):.+: %m")
+    elseif ft == "java" then
+        lint("javac", "javac", {"-d", temp, file}, "%f:%l: error: %m")
+    elseif ft == "javascript" then
+        lint("jshint", "jshint", {file}, "%f: line %l,.+, %m")
+    end
+end
+
+function onSave(view)
     if GetOption("linter") then
-        local ft = views[mainView+1].Buf.FileType
-        local file = views[mainView+1].Buf.Path
-        local devnull = "/dev/null"
-        if OS == "windows" then
-            devnull = "NUL"
-        end
-        if ft == "Go" then
-            linter_lint("gobuild", "go build -o " .. devnull, "%f:%l: %m")
-            linter_lint("golint", "golint " .. views[mainView+1].Buf.Path, "%f:%l:%d+: %m")
-        elseif ft == "Lua" then
-            linter_lint("luacheck", "luacheck --no-color " .. file, "%f:%l:%d+: %m")
-        elseif ft == "Python" then
-            linter_lint("pyflakes", "pyflakes " .. file, "%f:%l: %m")
-        elseif ft == "C" then
-            linter_lint("gcc", "gcc -fsyntax-only -Wall -Wextra " .. file, "%f:%l:%d+:.+: %m")
-        elseif ft == "D" then
-            linter_lint("dmd", "dmd -color=off -o- -w -wi -c " .. file, "%f%(%l%):.+: %m")
-        elseif ft == "Java" then
-            linter_lint("javac", "javac " .. file, "%f:%l: error: %m")
-        elseif ft == "JavaScript" then
-            linter_lint("jshint", "jshint " .. file, "%f: line %l,.+, %m")
-        end
+        runLinter()
     else
-        views[mainView+1]:ClearAllGutterMessages()
+        CurView():ClearAllGutterMessages()
     end
 end
 
-function linter_lint(linter, cmd, errorformat)
-    views[mainView+1]:ClearGutterMessages(linter)
+function lint(linter, cmd, args, errorformat)
+    CurView():ClearGutterMessages(linter)
+
+    JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat)
+end
 
-    local handle = io.popen("(" .. cmd .. ")" .. " 2>&1")
-    local lines = linter_split(handle:read("*a"), "\n")
-    handle:close()
+function onExit(output, linter, errorformat)
+    local lines = split(output, "\n")
 
-    local regex = errorformat:gsub("%%f", "(.+)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
+    local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%m", "(.+)")
     for _,line in ipairs(lines) do
         -- Trim whitespace
         line = line:match("^%s*(.+)%s*$")
         if string.find(line, regex) then
             local file, line, msg = string.match(line, regex)
-            if linter_basename(views[mainView+1].Buf.Path) == linter_basename(file) then
-                views[mainView+1]:GutterMessage(linter, tonumber(line), msg, 2)
+            if basename(CurView().Buf.Path) == basename(file) then
+                CurView():GutterMessage(linter, tonumber(line), msg, 2)
             end
         end
     end
 end
 
-function linter_split(str, sep)
+function split(str, sep)
     local result = {}
     local regex = ("([^%s]+)"):format(sep)
     for each in str:gmatch(regex) do
@@ -60,7 +79,7 @@ function linter_split(str, sep)
     return result
 end
 
-function linter_basename(file)
+function basename(file)
     local name = string.gsub(file, "(.*/)(.*)", "%2")
     return name
 end