]> git.lizzy.rs Git - micro.git/blobdiff - runtime/plugins/linter/linter.lua
Update linter to include eslint
[micro.git] / runtime / plugins / linter / linter.lua
index b4f759432c2f436e076a90dd942f175c4ed88a24..0bb312ec0579e9a2aa58ee4f9a6c7bc41e653e5a 100644 (file)
@@ -1,9 +1,13 @@
+VERSION = "1.0.0"
+
 local micro = import("micro")
 local runtime = import("runtime")
 local filepath = import("path/filepath")
 local shell = import("micro/shell")
 local buffer = import("micro/buffer")
 local config = import("micro/config")
+local util = import("micro/util")
+local os = import("os")
 
 local linters = {}
 
@@ -32,7 +36,10 @@ local linters = {}
 -- coffset: column offset will be added to the col number returned by the linter
 --          useful if the linter returns 0-indexed columns
 --     optional param, default: 0
-function makeLinter(name, filetype, cmd, args, errorformat, os, whitelist, domatch, loffset, coffset)
+-- callback: function to call before executing the linter, if it returns
+--           false the lint is canceled. The callback is passed the buf.
+--     optional param, default: nil
+function makeLinter(name, filetype, cmd, args, errorformat, os, whitelist, domatch, loffset, coffset, callback)
     if linters[name] == nil then
         linters[name] = {}
         linters[name].filetype = filetype
@@ -44,6 +51,7 @@ function makeLinter(name, filetype, cmd, args, errorformat, os, whitelist, domat
         linters[name].domatch = domatch or false
         linters[name].loffset = loffset or 0
         linters[name].coffset = coffset or 0
+        linters[name].callback = callback or nil
     end
 end
 
@@ -58,10 +66,12 @@ function init()
     end
 
     makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
-    makeLinter("gcc", "c++", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
+    makeLinter("g++", "c++", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m")
     makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m")
-    makeLinter("gobuild", "go", "go", {"build", "-o", devnull}, "%f:%l:%c:? %m")
+    makeLinter("eslint", "javascript", "eslint", {"-f","compact","%f"}, "%f: line %l, col %c, %m")
+    makeLinter("gobuild", "go", "go", {"build", "-o", devnull, "%d"}, "%f:%l:%c:? %m")
     -- makeLinter("golint", "go", "golint", {"%f"}, "%f:%l:%c: %m")
+    makeLinter("hlint", "haskell", "hlint", {"%f"}, "%f:%l:%c.-: %m")
     makeLinter("javac", "java", "javac", {"-d", "%d", "%f"}, "%f:%l: error: %m")
     makeLinter("jshint", "javascript", "jshint", {"%f"}, "%f: line %l,.+, %m")
     makeLinter("literate", "literate", "lit", {"-c", "%f"}, "%f:%l:%m", {}, false, true)
@@ -76,12 +86,12 @@ function init()
     makeLinter("swiftc", "swiftc", {"%f"}, "%f:%l:%c:.+: %m", {"linux"}, true)
     makeLinter("yaml", "yaml", "yamllint", {"--format", "parsable", "%f"}, "%f:%l:%c:.+ %m")
 
-    config.MakeCommand("lint", "linter.lintCmd", config.NoComplete)
-end
+    config.MakeCommand("lint", function(bp, args)
+        bp:Save()
+        runLinter(bp.Buf)
+    end, config.NoComplete)
 
-function lintCmd(bp, args)
-    bp:Save()
-    runLinter(bp.Buf)
+    config.AddRuntimeFile("linter", config.RTHelp, "help/linter.md")
 end
 
 function contains(list, element)
@@ -96,7 +106,7 @@ end
 function runLinter(buf)
     local ft = buf:FileType()
     local file = buf.Path
-    local dir = filepath.Dir(file)
+    local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file)
 
     for k, v in pairs(linters) do
         local ftmatch = ft == v.filetype
@@ -118,7 +128,7 @@ function runLinter(buf)
         end
 
         if ftmatch then
-            lint(buf, k, v.cmd, args, v.errorformat, v.loffset, v.coffset)
+            lint(buf, k, v.cmd, args, v.errorformat, v.loffset, v.coffset, v.callback)
         end
     end
 end
@@ -128,10 +138,16 @@ function onSave(bp)
     return true
 end
 
-function lint(buf, linter, cmd, args, errorformat, loff, coff)
+function lint(buf, linter, cmd, args, errorformat, loff, coff, callback)
     buf:ClearMessages(linter)
 
-    shell.JobSpawn(cmd, args, "", "", "linter.onExit", buf, linter, errorformat, loff, coff)
+    if callback ~= nil then
+        if not callback(buf) then
+            return
+        end
+    end
+
+    shell.JobSpawn(cmd, args, nil, nil, onExit, buf, linter, errorformat, loff, coff)
 end
 
 function onExit(output, args)