diff options
author | Steven Arcangeli <stevearc@stevearc.com> | 2023-08-28 18:27:46 -0700 |
---|---|---|
committer | Steven Arcangeli <stevearc@stevearc.com> | 2023-08-28 18:27:46 -0700 |
commit | d508ae8f46b5b41e2806b412311719a941167c1a (patch) | |
tree | e4d9244fd20cbe0dcda9ce3a4e524fca5ba3d6fc /README.md | |
parent | e09ef2a6a3d0f987963bfef771b5af060183f849 (diff) |
refactor!: remove ability for formatter list to disable autoformat
I realized that there are so, so many possible features people would
want when configuring the autoformatter, but it's better to just code it
up yourself rather than try to create a config language that can
describe all possible logic. Also adding new docs to provide examples of
more advanced autoformat logic.
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 37 |
1 files changed, 34 insertions, 3 deletions
@@ -1,6 +1,6 @@ # conform.nvim -Formatter plugin for Neovim +Lightweight yet powerful formatter plugin for Neovim <!-- TOC --> @@ -9,6 +9,7 @@ Formatter plugin for Neovim - [Setup](#setup) - [Formatters](#formatters) - [Options](#options) +- [Autoformat on save](#autoformat-on-save) - [API](#api) - [format(opts)](#formatopts) - [list_formatters(bufnr)](#list_formattersbufnr) @@ -114,8 +115,6 @@ require("conform").setup({ formatters = { "isort", "black" }, -- Run formatters one after another instead of stopping at the first success run_all_formatters = true, - -- Don't run these formatters as part of the format_on_save autocmd (see below) - format_on_save = false }, }, }) @@ -266,6 +265,38 @@ require("conform").formatters.my_formatter = { <!-- /OPTIONS --> +## Autoformat on save + +If you want more complex logic than the `format_on_save` option allows, you can write it yourself +using your own autocmd. For example: + +<!-- AUTOFORMAT --> + +```lua +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*", + callback = function(args) + -- Disable autoformat on certain filetypes + local ignore_filetypes = { "sql", "java" } + if vim.tbl_contains(ignore_filetypes, vim.bo[args.buf].filetype) then + return + end + -- Disable with a global or buffer-local variable + if vim.g.disable_autoformat or vim.b[args.buf].disable_autoformat then + return + end + -- Disable autoformat for files in a certain path + local bufname = vim.api.nvim_buf_get_name(args.buf) + if bufname:match("/node_modules/") then + return + end + require("conform").format({ timeout_ms = 500, lsp_fallback = true, buf = args.buf }) + end, +}) +``` + +<!-- /AUTOFORMAT --> + ## API <!-- API --> |