X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=runtime%2Fplugins%2Flinter%2Flinter.lua;h=546f5779b68d4a249bd612e84ac78ada3079af19;hb=8a907956d1701aa9675a26bb587cd282562c988f;hp=b4f759432c2f436e076a90dd942f175c4ed88a24;hpb=cef32d4ac7f7bbc1f57fc67f8a1788edddfbe771;p=micro.git diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index b4f75943..546f5779 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -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)