X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=runtime%2Fplugins%2Flinter%2Flinter.lua;h=2a231af0e98a08aee95ee402c2e541c446e5d23b;hb=c1db99a5a5f241584978f9fec89ee097bf2f2b5d;hp=a8e2da8580a9e50a269b3da125216290b6260eae;hpb=1668e9131020fdda0125e14600cd4983c3a381a3;p=micro.git diff --git a/runtime/plugins/linter/linter.lua b/runtime/plugins/linter/linter.lua index a8e2da85..2a231af0 100644 --- a/runtime/plugins/linter/linter.lua +++ b/runtime/plugins/linter/linter.lua @@ -1,23 +1,82 @@ -function linter_lint(linter, cmd, errorformat) - view:ClearGutterMessages(linter) +if GetOption("linter") == nil then + AddOption("linter", true) +end + +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 == "c++" then + lint("gcc", "gcc", {"-fsyntax-only","-std=c++14", "-Wall", "-Wextra", file}, "%f:%l:%d+:.+: %m") + elseif ft == "swift" then + lint("switfc", "xcrun", {"swiftc", 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") + elseif ft == "nim" then + lint("nim", "nim", {"check", "--listFullPaths", "--stdout", "--hints:off", file}, "%f.%l, %d+. %m") + end +end + +function onSave(view) + if GetOption("linter") then + runLinter() + else + CurView():ClearAllGutterMessages() + end +end + +function lint(linter, cmd, args, errorformat) + CurView():ClearGutterMessages(linter) + + JobSpawn(cmd, args, "", "", "linter.onExit", linter, errorformat) +end - local handle = io.popen(cmd) - local lines = linter_split(handle:read("*a"), "\n") - handle:close() +function onExit(output, linter, errorformat) + local lines = split(output, "\n") - messenger:Message(view.Buf.Path) - 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(view.Buf.Path) == linter_basename(file) then - view: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 @@ -26,7 +85,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