From a3af6d6d97947d2c49962edf60751d2f3f6c2edf Mon Sep 17 00:00:00 2001 From: ache Date: Wed, 14 May 2025 18:19:36 +0200 Subject: Configure formatting --- lua/ache/plugins/formatting.lua | 86 ++++++++++++++++++++++++++++++++--------- 1 file changed, 67 insertions(+), 19 deletions(-) diff --git a/lua/ache/plugins/formatting.lua b/lua/ache/plugins/formatting.lua index 38910bf..8bad1ad 100644 --- a/lua/ache/plugins/formatting.lua +++ b/lua/ache/plugins/formatting.lua @@ -3,29 +3,75 @@ --] return { "stevearc/conform.nvim", - enable = false, + enabled = true, event = { "BufReadPre", "BufNewFile" }, - config = function() - local conform = require("conform") - - conform.setup({ - formatters_by_ft = { - javascript = { "prettier" }, - typescript = { "prettier" }, - svelte = { "prettier" }, - css = { "prettier" }, - html = { "prettier" }, - json = { "prettier" }, - yaml = { "prettier" }, - markdown = { "prettier" }, - lua = { "stylua" }, - python = { "isort", "black" }, - }, - format_on_save = { + opts = { + formatters_by_ft = { + javascript = { "prettier" }, + typescript = { "prettier" }, + svelte = { "prettier" }, + css = { "prettier" }, + html = { "prettier" }, + json = { "prettier" }, + yaml = { "prettier" }, + markdown = { "prettier" }, + lua = { "stylua" }, + fish = { "fish_indent" }, + sh = { "shfmt" }, + python = function(bufnr) + if require("conform").get_formatter_info("ruff_format", bufnr).available then + return { "ruff_format" } + else + return { "isort", "black" } + end + end, + }, + format_on_save = function(bufnr) + -- Disable with a global or buffer-local variable + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + return { lsp_fallback = true, async = false, tiemout_ms = 1000, - }, + } + end, + -- Conform will notify you when no formatters are available for the buffer + notify_on_error = true, + }, + config = function(_, opts) + local conform = require("conform") + + conform.setup(opts) + + vim.api.nvim_create_user_command("FormatDisable", function(args) + if args.bang then + -- FormatDisable! will disable formatting just for this buffer + vim.b.disable_autoformat = true + else + vim.g.disable_autoformat = true + end + end, { + desc = "Disable autoformat-on-save", + 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.api.nvim_create_user_command("FormatToggle", function() + if vim.b.disable_autoformat then + vim.b.disable_autoformat = false + vim.g.disable_autoformat = false + else + vim.b.disable_autoformat = true + vim.g.disable_autoformat = true + end + end, { + desc = "Toggle autoformat-on-save", }) vim.keymap.set({ "n", "v" }, "mp", function() @@ -35,5 +81,7 @@ return { tiemout_ms = 1000, }) end, { desc = "Format file or range (in visual mode)" }) + + vim.keymap.set({ "n", "v" }, "uf", "FormatToggle", { desc = "Toggle autoformat-on-save" }) end, } -- cgit v1.3-2-g11bf