nvim: Intelligent format on save
This commit is contained in:
@@ -6,7 +6,35 @@ return {
|
|||||||
|
|
||||||
conform.setup(opts)
|
conform.setup(opts)
|
||||||
|
|
||||||
vim.keymap.set({ "n", "v" }, "<leader>cp", function()
|
vim.api.nvim_create_user_command("FormatDisable", function(args)
|
||||||
|
if args.bang then
|
||||||
|
vim.b.disable_autoformat = true
|
||||||
|
else
|
||||||
|
vim.g.disable_autoformat = true
|
||||||
|
end
|
||||||
|
end, {
|
||||||
|
desc = "Disable autoformat-on-save (! = current buffer only)",
|
||||||
|
bang = true,
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.api.nvim_create_user_command("FormatEnable", function()
|
||||||
|
vim.b.disable_autoformat = false
|
||||||
|
vim.g.disable_autoformat = false
|
||||||
|
end, { desc = "Re-enable autoformat-on-save" })
|
||||||
|
|
||||||
|
vim.keymap.set("n", "<leader>cf", "<cmd>FormatDisable!<cr>", {
|
||||||
|
desc = "Format: disable autoformat for this buffer",
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.keymap.set("n", "<leader>cF", "<cmd>FormatDisable<cr>", {
|
||||||
|
desc = "Format: disable autoformat globally (session)",
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.keymap.set("n", "<leader>ce", "<cmd>FormatEnable<cr>", {
|
||||||
|
desc = "Format: re-enable autoformat",
|
||||||
|
})
|
||||||
|
|
||||||
|
vim.keymap.set("n", "<leader>cw", function()
|
||||||
conform.format({
|
conform.format({
|
||||||
lsp_fallback = true,
|
lsp_fallback = true,
|
||||||
async = false,
|
async = false,
|
||||||
@@ -32,5 +60,21 @@ return {
|
|||||||
typescript = { "prettier" },
|
typescript = { "prettier" },
|
||||||
yaml = { "prettier" },
|
yaml = { "prettier" },
|
||||||
},
|
},
|
||||||
|
format_on_save = function(bufnr)
|
||||||
|
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local fname = vim.api.nvim_buf_get_name(bufnr)
|
||||||
|
if fname ~= "" then
|
||||||
|
local marker = vim.fs.find(".no-autoformat", {
|
||||||
|
upward = true,
|
||||||
|
path = vim.fs.dirname(fname),
|
||||||
|
})[1]
|
||||||
|
if marker then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return { timeout_ms = 2000, lsp_fallback = true }
|
||||||
|
end,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user