| 1 |
VERSION = "1.0.0" |
| 2 |
|
| 3 |
local micro = import("micro") |
| 4 |
local runtime = import("runtime") |
| 5 |
local filepath = import("path/filepath") |
| 6 |
local shell = import("micro/shell") |
| 7 |
local buffer = import("micro/buffer") |
| 8 |
local config = import("micro/config") |
| 9 |
local util = import("micro/util") |
| 10 |
local os = import("os") |
| 11 |
|
| 12 |
local linters = {} |
| 13 |
|
| 14 |
-- creates a linter entry, call from within an initialization function, not |
| 15 |
-- directly at initial load time |
| 16 |
-- |
| 17 |
-- name: name of the linter |
| 18 |
-- filetype: filetype to check for to use linter |
| 19 |
-- cmd: main linter process that is executed |
| 20 |
-- args: arguments to pass to the linter process |
| 21 |
-- use %f to refer to the current file name |
| 22 |
-- use %d to refer to the current directory name |
| 23 |
-- errorformat: how to parse the linter/compiler process output |
| 24 |
-- %f: file, %l: line number, %m: error/warning message |
| 25 |
-- os: list of OSs this linter is supported or unsupported on |
| 26 |
-- optional param, default: {} |
| 27 |
-- whitelist: should the OS list be a blacklist (do not run the linter for these OSs) |
| 28 |
-- or a whitelist (only run the linter for these OSs) |
| 29 |
-- optional param, default: false (should blacklist) |
| 30 |
-- domatch: should the filetype be interpreted as a lua pattern to match with |
| 31 |
-- the actual filetype, or should the linter only activate on an exact match |
| 32 |
-- optional param, default: false (require exact match) |
| 33 |
-- loffset: line offset will be added to the line number returned by the linter |
| 34 |
-- useful if the linter returns 0-indexed lines |
| 35 |
-- optional param, default: 0 |
| 36 |
-- coffset: column offset will be added to the col number returned by the linter |
| 37 |
-- useful if the linter returns 0-indexed columns |
| 38 |
-- optional param, default: 0 |
| 39 |
-- callback: function to call before executing the linter, if it returns |
| 40 |
-- false the lint is canceled. The callback is passed the buf. |
| 41 |
-- optional param, default: nil |
| 42 |
function makeLinter(name, filetype, cmd, args, errorformat, os, whitelist, domatch, loffset, coffset, callback) |
| 43 |
if linters[name] == nil then |
| 44 |
linters[name] = {} |
| 45 |
linters[name].filetype = filetype |
| 46 |
linters[name].cmd = cmd |
| 47 |
linters[name].args = args |
| 48 |
linters[name].errorformat = errorformat |
| 49 |
linters[name].os = os or {} |
| 50 |
linters[name].whitelist = whitelist or false |
| 51 |
linters[name].domatch = domatch or false |
| 52 |
linters[name].loffset = loffset or 0 |
| 53 |
linters[name].coffset = coffset or 0 |
| 54 |
linters[name].callback = callback or nil |
| 55 |
end |
| 56 |
end |
| 57 |
|
| 58 |
function removeLinter(name) |
| 59 |
linters[name] = nil |
| 60 |
end |
| 61 |
|
| 62 |
function preinit() |
| 63 |
local devnull = "/dev/null" |
| 64 |
if runtime.GOOS == "windows" then |
| 65 |
devnull = "NUL" |
| 66 |
end |
| 67 |
|
| 68 |
makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") |
| 69 |
makeLinter("g++", "c++", "g++", {"-fsyntax-only","-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") |
| 70 |
makeLinter("dmd", "d", "dmd", {"-color=off", "-o-", "-w", "-wi", "-c", "%f"}, "%f%(%l%):.+: %m") |
| 71 |
makeLinter("ldc2", "d", "ldc2", {"--o-", "--vcolumns", "-w", "-c", "%f"}, "%f%(%l,%c%):[^:]+: %m") |
| 72 |
makeLinter("gdc", "d", "gdc", {"-fsyntax-only","-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") |
| 73 |
makeLinter("eslint", "javascript", "eslint", {"-f","compact","%f"}, "%f: line %l, col %c, %m") |
| 74 |
makeLinter("gobuild", "go", "go", {"build", "-o", devnull, "%d"}, "%f:%l:%c:? %m") |
| 75 |
makeLinter("govet", "go", "go", {"vet"}, "%f:%l:%c: %m") |
| 76 |
makeLinter("clippy", "rust", "cargo", {"clippy", "--message-format", "short"}, "%f:%l:%c: %m") |
| 77 |
makeLinter("hlint", "haskell", "hlint", {"%f"}, "%f:%(?%l[,:]%c%)?.-: %m") |
| 78 |
makeLinter("javac", "java", "javac", {"-d", "%d", "%f"}, "%f:%l: error: %m") |
| 79 |
makeLinter("jshint", "javascript", "jshint", {"%f"}, "%f: line %l,.+, %m") |
| 80 |
makeLinter("literate", "literate", "lit", {"-c", "%f"}, "%f:%l:%m", {}, false, true) |
| 81 |
makeLinter("luacheck", "lua", "luacheck", {"--no-color", "%f"}, "%f:%l:%c: %m") |
| 82 |
makeLinter("nim", "nim", "nim", {"check", "--listFullPaths", "--stdout", "--hints:off", "%f"}, "%f.%l, %c. %m") |
| 83 |
makeLinter("clang", "objective-c", "xcrun", {"clang", "-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") |
| 84 |
makeLinter("pyflakes", "python", "pyflakes", {"%f"}, "%f:%l:.-:? %m") |
| 85 |
makeLinter("mypy", "python", "mypy", {"%f"}, "%f:%l: %m") |
| 86 |
makeLinter("pylint", "python", "pylint", {"--output-format=parseable", "--reports=no", "%f"}, "%f:%l: %m") |
| 87 |
makeLinter("ruff", "python", "ruff", {"check", "--output-format=concise", "%f"}, "%f:%l:%c: %m") |
| 88 |
makeLinter("flake8", "python", "flake8", {"%f"}, "%f:%l:%c: %m") |
| 89 |
makeLinter("shfmt", "shell", "shfmt", {"%f"}, "%f:%l:%c: %m") |
| 90 |
makeLinter("shellcheck", "shell", "shellcheck", {"-f", "gcc", "%f"}, "%f:%l:%c:.+: %m") |
| 91 |
makeLinter("swiftc", "swift", "xcrun", {"swiftc", "%f"}, "%f:%l:%c:.+: %m", {"darwin"}, true) |
| 92 |
makeLinter("swiftc-linux", "swift", "swiftc", {"%f"}, "%f:%l:%c:.+: %m", {"linux"}, true) |
| 93 |
makeLinter("yaml", "yaml", "yamllint", {"--format", "parsable", "%f"}, "%f:%l:%c:.+ %m") |
| 94 |
makeLinter("nix-linter", "nix", "nix-linter", {"%f"}, "%m at %f:%l:%c", {"linux"}, true) |
| 95 |
|
| 96 |
config.MakeCommand("lint", function(bp, args) |
| 97 |
bp:Save() |
| 98 |
runLinter(bp.Buf) |
| 99 |
end, config.NoComplete) |
| 100 |
|
| 101 |
config.AddRuntimeFile("linter", config.RTHelp, "help/linter.md") |
| 102 |
end |
| 103 |
|
| 104 |
function contains(list, element) |
| 105 |
for k, v in pairs(list) do |
| 106 |
if v == element then |
| 107 |
return true |
| 108 |
end |
| 109 |
end |
| 110 |
return false |
| 111 |
end |
| 112 |
|
| 113 |
function checkFtMatch(ft, v) |
| 114 |
local ftmatch = ft == v.filetype |
| 115 |
if v.domatch then |
| 116 |
ftmatch = string.match(ft, v.filetype) |
| 117 |
end |
| 118 |
|
| 119 |
local hasOS = contains(v.os, runtime.GOOS) |
| 120 |
if not hasOS and v.whitelist then |
| 121 |
ftmatch = false |
| 122 |
end |
| 123 |
if hasOS and not v.whitelist then |
| 124 |
ftmatch = false |
| 125 |
end |
| 126 |
return ftmatch |
| 127 |
end |
| 128 |
|
| 129 |
function runLinter(buf) |
| 130 |
local ft = buf:FileType() |
| 131 |
local file = buf.Path |
| 132 |
local dir = "." .. util.RuneStr(os.PathSeparator) .. filepath.Dir(file) |
| 133 |
|
| 134 |
for k, v in pairs(linters) do |
| 135 |
if checkFtMatch(ft, v) then |
| 136 |
local args = {} |
| 137 |
for k, arg in pairs(v.args) do |
| 138 |
args[k] = arg:gsub("%%f", file):gsub("%%d", dir) |
| 139 |
end |
| 140 |
lint(buf, k, v.cmd, args, v.errorformat, v.loffset, v.coffset, v.callback) |
| 141 |
end |
| 142 |
end |
| 143 |
end |
| 144 |
|
| 145 |
function onSave(bp) |
| 146 |
runLinter(bp.Buf) |
| 147 |
return true |
| 148 |
end |
| 149 |
|
| 150 |
function onBufferOptionChanged(buf, option, old, new) |
| 151 |
if option == "filetype" then |
| 152 |
if old ~= new then |
| 153 |
for k, v in pairs(linters) do |
| 154 |
if checkFtMatch(old, v) then |
| 155 |
buf:ClearMessages(k) |
| 156 |
end |
| 157 |
end |
| 158 |
end |
| 159 |
end |
| 160 |
return true |
| 161 |
end |
| 162 |
|
| 163 |
function lint(buf, linter, cmd, args, errorformat, loff, coff, callback) |
| 164 |
buf:ClearMessages(linter) |
| 165 |
|
| 166 |
if callback ~= nil then |
| 167 |
if not callback(buf) then |
| 168 |
return |
| 169 |
end |
| 170 |
end |
| 171 |
|
| 172 |
shell.JobSpawn(cmd, args, nil, nil, onExit, buf, linter, errorformat, loff, coff) |
| 173 |
end |
| 174 |
|
| 175 |
function onExit(output, args) |
| 176 |
local buf, linter, errorformat, loff, coff = args[1], args[2], args[3], args[4], args[5] |
| 177 |
local lines = split(output, "\n") |
| 178 |
|
| 179 |
local regex = errorformat:gsub("%%f", "(..-)"):gsub("%%l", "(%d+)"):gsub("%%c", "(%d+)"):gsub("%%m", "(.+)") |
| 180 |
for _,line in ipairs(lines) do |
| 181 |
-- Trim whitespace |
| 182 |
line = line:match("^%s*(.+)%s*$") |
| 183 |
if string.find(line, regex) then |
| 184 |
local file, line, col, msg = string.match(line, regex) |
| 185 |
local hascol = true |
| 186 |
if not string.find(errorformat, "%%c") then |
| 187 |
hascol = false |
| 188 |
msg = col |
| 189 |
elseif col == nil then |
| 190 |
hascol = false |
| 191 |
end |
| 192 |
if basename(buf.Path) == basename(file) then |
| 193 |
local bmsg = nil |
| 194 |
if hascol then |
| 195 |
local mstart = buffer.Loc(tonumber(col-1+coff), tonumber(line-1+loff)) |
| 196 |
local mend = buffer.Loc(tonumber(col+coff), tonumber(line-1+loff)) |
| 197 |
bmsg = buffer.NewMessage(linter, msg, mstart, mend, buffer.MTError) |
| 198 |
else |
| 199 |
bmsg = buffer.NewMessageAtLine(linter, msg, tonumber(line+loff), buffer.MTError) |
| 200 |
end |
| 201 |
buf:AddMessage(bmsg) |
| 202 |
end |
| 203 |
end |
| 204 |
end |
| 205 |
end |
| 206 |
|
| 207 |
function split(str, sep) |
| 208 |
local result = {} |
| 209 |
local regex = ("([^%s]+)"):format(sep) |
| 210 |
for each in str:gmatch(regex) do |
| 211 |
table.insert(result, each) |
| 212 |
end |
| 213 |
return result |
| 214 |
end |
| 215 |
|
| 216 |
function basename(file) |
| 217 |
local sep = "/" |
| 218 |
if runtime.GOOS == "windows" then |
| 219 |
sep = "\\" |
| 220 |
end |
| 221 |
local name = string.gsub(file, "(.*" .. sep .. ")(.*)", "%2") |
| 222 |
return name |
| 223 |
end |