| 1 |
# Linter |
| 2 |
|
| 3 |
The linter plugin runs a compiler or linter on your source code |
| 4 |
and parses the resulting output so that the messages and line numbers |
| 5 |
can be viewed from within micro. By default, the plugin supports the |
| 6 |
following filetypes and linters: |
| 7 |
|
| 8 |
* **c**: gcc |
| 9 |
* **c++**: g++ |
| 10 |
* **d**: dmd |
| 11 |
* **d**: ldc2 |
| 12 |
* **d**: gdc |
| 13 |
* **go**: go build |
| 14 |
* **go**: go vet |
| 15 |
* **haskell**: hlint |
| 16 |
* **java**: javac |
| 17 |
* **javascript**: jshint |
| 18 |
* **javascript**: eslint |
| 19 |
* **literate**: lit |
| 20 |
* **lua**: luacheck |
| 21 |
* **nim**: nim |
| 22 |
* **nix**: nix-linter |
| 23 |
* **objective-c**: clang |
| 24 |
* **python**: flake8 |
| 25 |
* **python**: mypy |
| 26 |
* **python**: pyflakes |
| 27 |
* **python**: pylint |
| 28 |
* **python**: ruff |
| 29 |
* **rust**: cargo clippy |
| 30 |
* **shell**: shfmt |
| 31 |
* **shell**: shellcheck |
| 32 |
* **swift**: swiftc (MacOS and Linux only) |
| 33 |
* **yaml**: yamllint |
| 34 |
|
| 35 |
If the linter plugin is enabled and the file corresponds to one of |
| 36 |
these filetypes, each time the buffer is saved, or when the `> lint` |
| 37 |
command is executed, micro will run the corresponding utility in the |
| 38 |
background and display the messages when it completes. |
| 39 |
|
| 40 |
The linter plugin also allows users to extend the supported filetypes. |
| 41 |
From inside another micro plugin, the function `linter.makeLinter` can |
| 42 |
be called to register a new filetype. Here is the spec for the `makeLinter` |
| 43 |
function: |
| 44 |
|
| 45 |
`linter.makeLinter(name, filetype, cmd, args, errorformat, os, whitelist, domatch, loffset, coffset, callback)` |
| 46 |
|
| 47 |
* **name**: name of the linter |
| 48 |
* **filetype**: filetype to check for to use linter |
| 49 |
* **cmd**: main linter process that is executed |
| 50 |
* **args**: arguments to pass to the linter process |
| 51 |
* use %f to refer to the current file name |
| 52 |
* use %d to refer to the current directory name |
| 53 |
* **errorformat**: how to parse the linter/compiler process output |
| 54 |
%f: file, %l: line number, %m: error/warning message |
| 55 |
* **os**: list of OSs this linter is supported or unsupported on |
| 56 |
optional param, default: {} |
| 57 |
* **whitelist**: should the OS list be a blacklist (do not run the linter for these OSs) |
| 58 |
or a whitelist (only run the linter for these OSs) |
| 59 |
optional param, default: false (should blacklist) |
| 60 |
* **domatch**: should the filetype be interpreted as a lua pattern to match with |
| 61 |
the actual filetype, or should the linter only activate on an exact match |
| 62 |
optional param, default: false (require exact match) |
| 63 |
* **loffset**: line offset will be added to the line number returned by the linter |
| 64 |
useful if the linter returns 0-indexed lines |
| 65 |
optional param, default: 0 |
| 66 |
* **coffset**: column offset will be added to the col number returned by the linter |
| 67 |
useful if the linter returns 0-indexed columns |
| 68 |
optional param, default: 0 |
| 69 |
* **callback**: function to call before executing the linter, if it returns |
| 70 |
false the lint is canceled. The callback is passed the buf. |
| 71 |
optional param, default: nil |
| 72 |
|
| 73 |
Below is an example for including a linter for any filetype using |
| 74 |
the `misspell` linter which checks for misspelled words in a file. |
| 75 |
|
| 76 |
```lua |
| 77 |
function init() |
| 78 |
-- uses the default linter plugin |
| 79 |
-- matches any filetype |
| 80 |
linter.makeLinter("misspell", "", "misspell", {"%f"}, "%f:%l:%c: %m", {}, false, true) |
| 81 |
end |
| 82 |
``` |
| 83 |
|
| 84 |
Here is an example for a more typical use-case, where the linter will only match one filetype (C in this case): |
| 85 |
|
| 86 |
```lua |
| 87 |
function init() |
| 88 |
linter.makeLinter("gcc", "c", "gcc", {"-fsyntax-only", "-Wall", "-Wextra", "%f"}, "%f:%l:%c:.+: %m") |
| 89 |
end |
| 90 |
``` |