Repository navigation
v0.9.0
What's Changed
Features
Format
- Add format setup with optional format on save by @nmfirdausw in #10
Highlights
- Formatting that follows
gq. A newformatoption formats with Neovim's own settings: yourformatprgor a customformatexprwhen set, otherwise the language server. - Format on save, off by default. Turn it on with
format = { on_save = true }, or toggle it while you work. - Per filetype, from an ftplugin. Set
vim.b.format_on_save = true(orfalse) in a filetype's ftplugin to opt that buffer in or out. - A failing formatter is safe. If
formatprgfails, such as a missingstylua, your text is kept and the formatter's error is shown. Before, the buffer was replaced with the error output.
Usage
require("batu").setup({
format = {
on_save = true, -- format before every write (default: false)
timeout_ms = 1000, -- how long a language server may take
},
-- format = false, -- leave formatting alone
})An external formatter for one filetype, in ftplugin/lua.lua:
vim.opt_local.formatprg = "stylua --indent-type Spaces --indent-width 2 --quote-style AutoPreferDouble -"
vim.b.format_on_save = true -- format this filetype on save, whatever `on_save` is| Option | Default | Effect |
|---|---|---|
format |
true |
false leaves formatting alone, a table sets the options below |
on_save |
false |
true formats before every write |
timeout_ms |
1000 |
How long a language server may take to format |
Keys
| Key | Mode | Action |
|---|---|---|
<leader>cf |
normal, visual | Format the buffer, or the selection |
<leader>tf |
normal | Toggle format on save for the current buffer |
<leader>tF |
normal | Toggle format on save for every buffer |
gq gqq |
normal, visual | Format, keeping the text when the formatter fails |
A buffer's own setting (vim.b.format_on_save, or <leader>tf) wins over the global one.
How it works
If formatprg or a custom formatexpr is set, that formatter runs over the buffer as gggqG would. A formatprg wins over the server's default formatexpr, which is attached after your ftplugin runs, so one set there works. Otherwise the language server formats, with timeout_ms, and only when a server can format the buffer. gq would otherwise fall back to wrapping lines at textwidth. Special buffers and URL-style ones such as oil:// are skipped on save.
Upgrading
format is on by default, but nothing formats on save until you set on_save = true. gq, gqq and visual gq are now Batu mappings. To keep Neovim's own, set them to false in the keymaps table. If you use another formatter plugin, pass format = false.
Full Changelog: v0.8.0...v0.9.0