|
@@ -277,8 +277,26 @@ local format_on_save = require("format-on-save")
|
|
|
local formatters = require("format-on-save.formatters")
|
|
|
local vim_notify = require("format-on-save.error-notifiers.vim-notify")
|
|
|
|
|
|
+-- only format with LSP if there are clients attached
|
|
|
+local lsp_formatter = formatters.custom({
|
|
|
+ format = function()
|
|
|
+ local bufnr = vim.api.nvim_get_current_buf()
|
|
|
+ local clients = vim.lsp.get_active_clients({ bufnr = bufnr })
|
|
|
+ if #clients > 0 then
|
|
|
+ vim.lsp.buf.format({ timeout_ms = 4000, bufnr = bufnr })
|
|
|
+ end
|
|
|
+ end
|
|
|
+})
|
|
|
+
|
|
|
+-- the built-in formatter was not playing well with folds
|
|
|
+local whitespace_formatter = formatters.custom({
|
|
|
+ format = function()
|
|
|
+ vim.cmd('%s/\\s\\+$//e')
|
|
|
+ end
|
|
|
+})
|
|
|
+
|
|
|
local js = {
|
|
|
- formatters.lsp,
|
|
|
+ lsp_formatter,
|
|
|
formatters.if_file_exists({
|
|
|
pattern = {
|
|
|
"dprint.json",
|
|
@@ -286,7 +304,7 @@ local js = {
|
|
|
".dprint.json",
|
|
|
".dprint.jsonc",
|
|
|
},
|
|
|
- formatter = formatters.shell({ cmd = { "dprint", "fmt", "--stdin", "%" }}),
|
|
|
+ formatter = formatters.shell({ cmd = { "dprint", "fmt", "--stdin", "%:p" }}),
|
|
|
}),
|
|
|
}
|
|
|
|
|
@@ -301,11 +319,14 @@ format_on_save.setup({
|
|
|
javascript = js,
|
|
|
typescript = js,
|
|
|
typescriptreact = js,
|
|
|
+
|
|
|
+ -- don't format diffs
|
|
|
+ diff = {},
|
|
|
},
|
|
|
|
|
|
fallback_formatter = {
|
|
|
- formatters.remove_trailing_whitespace,
|
|
|
- formatters.lsp,
|
|
|
+ whitespace_formatter,
|
|
|
+ lsp_formatter,
|
|
|
},
|
|
|
})
|
|
|
|