]> git.lizzy.rs Git - micro.git/blobdiff - runtime/plugins/linter/linter.lua
Use actual lua functions for callbacks instead of strings
[micro.git] / runtime / plugins / linter / linter.lua
index b4f759432c2f436e076a90dd942f175c4ed88a24..546f5779b68d4a249bd612e84ac78ada3079af19 100644 (file)
@@ -1,3 +1,5 @@
+VERSION = "1.0.0"
+
 local micro = import("micro")
 local runtime = import("runtime")
 local filepath = import("path/filepath")
@@ -32,7 +34,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 +49,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
 
@@ -76,12 +82,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)
@@ -118,7 +124,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 +134,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)