From c688311e37681065fea68b2e86e0d7598ebbfd25 Mon Sep 17 00:00:00 2001 From: Venkatesan Ravi Date: Mon, 31 Mar 2025 21:01:51 +0000 Subject: [PATCH] Add toggleFormatOnSave capability to conform --- .../formatter/conform-nvim/conform-nvim.nix | 99 ++++++++++++------- modules/plugins/lsp/config.nix | 6 +- 2 files changed, 67 insertions(+), 38 deletions(-) diff --git a/modules/plugins/formatter/conform-nvim/conform-nvim.nix b/modules/plugins/formatter/conform-nvim/conform-nvim.nix index 28fd2efe..a7e1e4af 100644 --- a/modules/plugins/formatter/conform-nvim/conform-nvim.nix +++ b/modules/plugins/formatter/conform-nvim/conform-nvim.nix @@ -1,11 +1,9 @@ -{ - config, - lib, - ... -}: let +{lib, ...}: let + inherit (lib.generators) mkLuaInline; inherit (lib.options) mkOption mkEnableOption; - inherit (lib.types) attrs nullOr; - inherit (lib.nvim.types) mkPluginSetupOption; + inherit (lib.types) attrs either nullOr; + inherit (lib.nvim.lua) toLuaObject; + inherit (lib.nvim.types) luaInline mkPluginSetupOption; in { options.vim.formatter.conform-nvim = { enable = mkEnableOption "lightweight yet powerful formatter plugin for Neovim [conform-nvim]"; @@ -29,33 +27,68 @@ in { description = "Default values when calling `conform.format()`"; }; - format_on_save = mkOption { - type = nullOr attrs; - default = - if config.vim.lsp.formatOnSave - then { - lsp_format = "fallback"; - timeout_ms = 500; - } - else null; - description = '' - Table that will be passed to `conform.format()`. If this - is set, Conform will run the formatter on save. - ''; - }; + format_on_save = let + defaultFormatOnSaveOpts = { + lsp_format = "fallback"; + timeout_ms = 500; + }; + in + mkOption { + type = nullOr (either attrs luaInline); + default = + mkLuaInline + # lua + '' + function() + if (not vim.g.formatsave) or (vim.b.disableFormatSave) then + return + else + return ${toLuaObject defaultFormatOnSaveOpts} + end + end + ''; + description = '' + Table or function(lualinline) that will be passed to `conform.format()`. If this + is set, Conform will run the formatter on save. - format_after_save = mkOption { - type = nullOr attrs; - default = - if config.vim.lsp.formatOnSave - then {lsp_format = "fallback";} - else null; - description = '' - Table that will be passed to `conform.format()`. If this - is set, Conform will run the formatter asynchronously after - save. - ''; - }; + Note: + - When config.vim.lsp.formatOnSave is set to true, internally + vim.g.formatsave is set to true. + - vim.b.disableFormatSave initally equals !config.vim.lsp.formatOnSave. + - vim.b.disableFormatSave is toggled using the + mapping from config.vim.lsp.mappings.toggleFormatOnSave. + ''; + }; + + format_after_save = let + defaultFormatAfterSaveOpts = {lsp_format = "fallback";}; + in + mkOption { + type = nullOr (either attrs luaInline); + default = + mkLuaInline + # lua + '' + function() + if (not vim.g.formatsave) or (vim.b.disableFormatSave) then + return + else + return ${toLuaObject defaultFormatAfterSaveOpts} + end + end + ''; + description = '' + Table or function(luainline) that will be passed to `conform.format()`. If this + is set, Conform will run the formatter asynchronously after save. + + Note: + - When config.vim.lsp.formatOnSave is set to true, internally + vim.g.formatsave is set to true. + - vim.b.disableFormatSave initally equals !config.vim.lsp.formatOnSave. + - vim.b.disableFormatSave is toggled using the + mapping from config.vim.lsp.mappings.toggleFormatOnSave. + ''; + }; }; }; } diff --git a/modules/plugins/lsp/config.nix b/modules/plugins/lsp/config.nix index 0fa16e47..63407138 100644 --- a/modules/plugins/lsp/config.nix +++ b/modules/plugins/lsp/config.nix @@ -93,11 +93,7 @@ in { }) end '' - else " - vim.lsp.buf.format({ - bufnr = bufnr, - }) - " + else "" } end, })