From 0ffd8321e9fee37e3f191209921546a40523a269 Mon Sep 17 00:00:00 2001 From: Venkatesan Ravi Date: Mon, 31 Mar 2025 21:06:54 +0000 Subject: [PATCH] Make conform respect config.vim.lsp.formatOnSave and config.vim.lsp.mappings.toggleFormatOnSave --- .../formatter/conform-nvim/conform-nvim.nix | 70 ++++++++++++------- 1 file changed, 44 insertions(+), 26 deletions(-) diff --git a/modules/plugins/formatter/conform-nvim/conform-nvim.nix b/modules/plugins/formatter/conform-nvim/conform-nvim.nix index 727985a3..68c57c47 100644 --- a/modules/plugins/formatter/conform-nvim/conform-nvim.nix +++ b/modules/plugins/formatter/conform-nvim/conform-nvim.nix @@ -1,12 +1,9 @@ -{ - pkgs, - lib, - ... -}: let - inherit (lib.options) mkOption mkEnableOption literalExpression; - inherit (lib.types) attrs enum nullOr; - inherit (lib.nvim.types) mkPluginSetupOption; - inherit (lib.nvim.lua) mkLuaInline; +{lib, ...}: let + inherit (lib.generators) mkLuaInline; + inherit (lib.options) mkOption mkEnableOption; + 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]"; @@ -30,27 +27,48 @@ in { description = "Default values when calling `conform.format()`"; }; - format_on_save = mkOption { - type = nullOr attrs; - default = { + format_on_save = let + defaultFormatOnSaveOpts = { lsp_format = "fallback"; timeout_ms = 500; }; - description = '' - Table that will be passed to `conform.format()`. If this - is set, Conform will run the formatter on save. - ''; - }; + in + mkOption { + type = nullOr (either attrs luaInline); + default = mkLuaInline '' + 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 = {lsp_format = "fallback";}; - description = '' - Table that will be passed to `conform.format()`. If this - is set, Conform will run the formatter asynchronously after - save. - ''; - }; + format_after_save = let + defaultFormatAfterSaveOpts = {lsp_format = "fallback";}; + in + mkOption { + type = nullOr (either attrs luaInline); + default = mkLuaInline '' + 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. + ''; + }; }; }; }