aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/autoformat_doc.lua
diff options
context:
space:
mode:
authorSteven Arcangeli <506791+stevearc@users.noreply.github.com>2023-08-31 08:54:11 -0700
committerGitHub <noreply@github.com>2023-08-31 08:54:11 -0700
commit3f34f2de48e393b2ee289f2c8fa613c7eabae6d8 (patch)
treee4f6e725d345cdbd07cd3f39f69ee7243592378e /scripts/autoformat_doc.lua
parent860bd36663b9b02b42a80c6b7f19642add0551ab (diff)
feat: format() takes an optional callback (#21)
* refactor: replicate lsp.buf.format call * feat: format() takes an optional callback * fix: improper logging * fix: callback returns error if buffer is no longer valid * fix: provide more detailed error message to callback * fix: properly detect task interruption * cleanup: remove unnecessary error code translation * fix: lsp formatting for Neovim 0.9 * doc: add example of async formatting on save * fix: async LSP formatter discards changes if buffer was modified * fix: error code comparison * fix: use the same LSP client filtering logic everywhere * fix: add buffer validity guard checks * fix: add buffer validity guard to LSP formatter * refactor: change the default log level to WARN
Diffstat (limited to 'scripts/autoformat_doc.lua')
-rw-r--r--scripts/autoformat_doc.lua35
1 files changed, 35 insertions, 0 deletions
diff --git a/scripts/autoformat_doc.lua b/scripts/autoformat_doc.lua
new file mode 100644
index 0000000..1ea8ac4
--- /dev/null
+++ b/scripts/autoformat_doc.lua
@@ -0,0 +1,35 @@
+-- Format synchronously on save
+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,
+})
+
+-- Format asynchronously on save
+vim.api.nvim_create_autocmd("BufWritePost", {
+ pattern = "*",
+ callback = function(args)
+ require("conform").format({ async = true, lsp_fallback = true, buf = args.buf }, function(err)
+ if not err then
+ vim.api.nvim_buf_call(args.buf, function()
+ vim.cmd.update()
+ end)
+ end
+ end)
+ end,
+})