Async formatting and linting utility for neovim.
- Blazingly fast
- Async using coroutine and luv spawn
- Builtin support for popular formatters and linters
- Easy configuration for custom tools
- Light-weight
Use any plugin manager you like. Guard is configured as follows:
local ft = require('guard.filetype')
ft('lang'):fmt('format-tool-1')
:append('format-tool-2')
:lint('lint-tool-1')
:append('lint-tool-2')
-- Call setup() LAST!
require('guard').setup({
-- the only options for the setup function
fmt_on_save = true,
-- Use lsp if no formatter was defined for this filetype
lsp_as_default_formatter = false,
})
- Use
GuardFmt
to manually call format, when there is a visual selection only the selection is formatted. GuardDisable
disables auto format for the current buffer, you can alsoGuardDisable 16
(the buffer number)- Use
GuardEnable
to re-enable auto format, usage is the same asGuardDisable
Format c files with clang-format and lint with clang-tidy:
ft('c'):fmt('clang-format')
:lint('clang-tidy')
Or use lsp to format go files first, then format with golines, then lint with golangci:
ft('go'):fmt('lsp')
:append('golines')
:lint('golangci')
Register multiple filetypes to a single linter or formatter:
ft('typescript,javascript,typescriptreact'):fmt('prettier')
Easily configure your custom formatter if not in the defaults:
{
cmd -- string: tool command
args -- table: command arugments
fname -- string: insert filename to args tail
stdin -- boolean: pass buffer contents into stdin
timeout -- integer
ignore_pattern -- table: ignore run format when pattern match
ignore_error -- boolean: when has lsp error ignore format
find -- string: format if the file is found in the lsp root dir
env -- table: environment variables passed to cmd (key value pair)
--special
fn -- function: if fn is set other field will not take effect
}
For example, format your assembly with asmfmt:
ft('asm'):fmt({
cmd = 'asmfmt',
stdin = true
})
Consult the builtin tools if needed.
lsp
usevim.lsp.buf.format
- black
- cbfmt
- clang-format
- djhtml
- fish_indent
- fnlfmt
- gofmt
- golines
- google-java-format
- isort
- mixformat
- latexindent
- pg_format
- prettier
- prettierd
- rubocop
- rustfmt
- shfmt
- stylua
- swiftformat
- swift-format
- sql-formatter
If guard does not auto format on save, run checkhealth
first.