diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/autoformat_doc.lua | 32 | ||||
-rw-r--r-- | scripts/options_doc.lua | 5 |
2 files changed, 26 insertions, 11 deletions
diff --git a/scripts/autoformat_doc.lua b/scripts/autoformat_doc.lua index 31cbec7..4880310 100644 --- a/scripts/autoformat_doc.lua +++ b/scripts/autoformat_doc.lua @@ -20,16 +20,26 @@ vim.api.nvim_create_autocmd("BufWritePre", { end, }) --- Format asynchronously on save -vim.api.nvim_create_autocmd("BufWritePost", { - pattern = "*", - callback = function(args) - require("conform").format({ async = true, lsp_fallback = true, bufnr = args.buf }, function(err) - if not err then - vim.api.nvim_buf_call(args.buf, function() - vim.cmd.update() - end) - end - end) +-- To eliminate the boilerplate, you can pass a function to format_on_save +-- and it will be called during the BufWritePre callback. +require("conform").setup({ + format_on_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + -- ...additional logic... + return { timeout_ms = 500, lsp_fallback = true } + end, +}) + +-- There is a similar affordance for format_after_save, which uses BufWritePost. +-- This is good for formatters that are too slow to run synchronously. +require("conform").setup({ + format_after_save = function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + -- ...additional logic... + return { lsp_fallback = true } end, }) diff --git a/scripts/options_doc.lua b/scripts/options_doc.lua index 1a6921c..5dab627 100644 --- a/scripts/options_doc.lua +++ b/scripts/options_doc.lua @@ -18,6 +18,11 @@ require("conform").setup({ lsp_fallback = true, timeout_ms = 500, }, + -- If this is set, Conform will run the formatter asynchronously after save. + -- It will pass the table to conform.format(). + format_after_save = { + lsp_fallback = true, + }, -- Set the log level. Use `:ConformInfo` to see the location of the log file. log_level = vim.log.levels.ERROR, -- Conform will notify you when a formatter errors |