+VERSION = "1.0.0"
+
local micro = import("micro")
local runtime = import("runtime")
local filepath = import("path/filepath")
-- 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
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
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)
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
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)