From 859e91ada25116908afdcd62a4f5a7859f1da597 Mon Sep 17 00:00:00 2001 From: Youwen Wu Date: Tue, 17 Sep 2024 15:51:01 -0700 Subject: [PATCH] feat: add conform formatting keymaps --- config/keymaps.nix | 64 ++++++++++++++++++++++++++++++----- config/plugins/formatters.nix | 43 ++++++++++++++++++++--- 2 files changed, 94 insertions(+), 13 deletions(-) diff --git a/config/keymaps.nix b/config/keymaps.nix index 1cef5f8..ca77659 100644 --- a/config/keymaps.nix +++ b/config/keymaps.nix @@ -489,14 +489,60 @@ silent = true; }; } - # { - # action = "lua require'conform'.format({ bufnr = args.bf })"; - # key = "cf"; - # options = { - # silent = true; - # noremap = true; - # desc = "Format buffer"; - # }; - # } + { + action = { + __raw = '' + function() require('conform').format() end + ''; + }; + key = "cf"; + options = { + silent = true; + noremap = true; + desc = "Format buffer"; + }; + } + { + action = { + __raw = '' + function() + -- we do this instead of `not vim.g.disable_autoformat` so that we + -- can handle when `vim.g.disable_autoformat` has not yet been defined + if (vim.g.disable_autoformat) then + vim.g.disable_autoformat = false + else + vim.g.disable_autoformat = true + end + end + ''; + }; + key = "ctf"; + options = { + silent = true; + noremap = true; + desc = "Toggle autoformat globally"; + }; + } + { + action = { + __raw = '' + -- we do this instead of `not vim.g.disable_autoformat` so that we + -- can handle when `vim.g.disable_autoformat` has not yet been defined + function() + if (vim.b[0].disable_autoformat) then + vim.b[0].disable_autoformat = false + else + vim.b[0].disable_autoformat = true + end + end + ''; + }; + key = "cbf"; + options = { + silent = true; + noremap = true; + desc = "Toggle autoformat in buffer"; + }; + } ]; } diff --git a/config/plugins/formatters.nix b/config/plugins/formatters.nix index 9cfde77..74fb924 100644 --- a/config/plugins/formatters.nix +++ b/config/plugins/formatters.nix @@ -39,10 +39,45 @@ }); # package = pkgs.vimPlugins.conform-nvim; settings = { - format_on_save = { - timeoutMs = 500; - lspFallback = true; - }; + # format_on_save = { + # timeoutMs = 500; + # lspFallback = true; + # }; + format_on_save = # Lua + '' + function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + + -- if slow_format_filetypes[vim.bo[bufnr].filetype] then + -- return + -- end + -- + -- local function on_format(err) + -- if err and err:match("timeout$") then + -- slow_format_filetypes[vim.bo[bufnr].filetype] = true + -- end + -- end + -- + -- return { timeout_ms = 500, lsp_fallback = true }, on_format + return { timeout_ms = 500, lsp_fallback = true } + end + ''; + format_after_save = # Lua + '' + function(bufnr) + if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then + return + end + + -- if not slow_format_filetypes[vim.bo[bufnr].filetype] then + -- return + -- end + + return { lsp_fallback = true } + end + ''; formatters_by_ft = { lua = [ "stylua" ]; python = [ "black" ];