diff --git a/examples/formatter-cabal-fmt.toml b/examples/formatter-cabal-fmt.toml index de69588..9975aa4 100644 --- a/examples/formatter-cabal-fmt.toml +++ b/examples/formatter-cabal-fmt.toml @@ -1,6 +1,19 @@ # Example generated by ../examples.sh [formatter.cabal-fmt] -command = "cabal-fmt" +command = "cabal-fmt-wrapper" excludes = [] -includes = ["*.cabal"] -options = ["--inplace"] +includes = [ + "*.cabal", + "*.chs", + "*.cpphs", + "*.gc", + "*.hs", + "*.hsc", + "*.hsig", + "*.lhs", + "*.lhsig", + "*.ly", + "*.x", + "*.y", +] +options = [] diff --git a/examples/formatter-cabal-gild.toml b/examples/formatter-cabal-gild.toml index 382cdc0..bd6f1a3 100644 --- a/examples/formatter-cabal-gild.toml +++ b/examples/formatter-cabal-gild.toml @@ -2,5 +2,20 @@ [formatter.cabal-gild] command = "cabal-gild-wrapper" excludes = [] -includes = ["*.cabal", "cabal.project", "cabal.project.local"] +includes = [ + "*.cabal", + "cabal.project", + "cabal.project.local", + "*.chs", + "*.cpphs", + "*.gc", + "*.hs", + "*.hsc", + "*.hsig", + "*.lhs", + "*.lhsig", + "*.ly", + "*.x", + "*.y", +] options = [] diff --git a/programs/cabal-fmt.nix b/programs/cabal-fmt.nix index f72895d..979fc86 100644 --- a/programs/cabal-fmt.nix +++ b/programs/cabal-fmt.nix @@ -1,4 +1,13 @@ -{ mkFormatterModule, ... }: +{ + lib, + config, + pkgs, + mkFormatterModule, + ... +}: +let + cfg = config.programs.cabal-fmt; +in { meta.maintainers = [ ]; @@ -9,8 +18,38 @@ "haskellPackages" "cabal-fmt" ]; - args = [ "--inplace" ]; - includes = [ "*.cabal" ]; + includes = [ + "*.cabal" + # Include Haskell source files to detect changes + # for cabal-fmt's module discovery feature + "*.chs" + "*.cpphs" + "*.gc" + "*.hs" + "*.hsc" + "*.hsig" + "*.lhs" + "*.lhsig" + "*.ly" + "*.x" + "*.y" + ]; }) ]; + + config = lib.mkIf cfg.enable { + settings.formatter.cabal-fmt = { + # Override command to filter out non-cabal files + command = pkgs.writeShellApplication { + name = "cabal-fmt-wrapper"; + # The cabal file needs to be formatted by a formatter along with other Haskell source code. + # For example, module completion by `cabal-fmt: discover`. + # It is difficult to determine this strictly. + # Since formatting with cabal-fmt doesn't take much time, we execute it speculatively. + text = ''${pkgs.git}/bin/git ls-files -z "*.cabal"|${pkgs.parallel}/bin/parallel --null "${lib.getExe cfg.package} --inplace {}"''; + }; + # Clear args since we're handling them in the wrapper + options = lib.mkForce [ ]; + }; + }; } diff --git a/programs/cabal-gild.nix b/programs/cabal-gild.nix index 8bf94ac..81583b6 100644 --- a/programs/cabal-gild.nix +++ b/programs/cabal-gild.nix @@ -1,8 +1,8 @@ { - mkFormatterModule, lib, config, pkgs, + mkFormatterModule, ... }: @@ -23,6 +23,19 @@ in "*.cabal" "cabal.project" "cabal.project.local" + # Include Haskell source files to detect changes + # for cabal-gild's module discovery feature + "*.chs" + "*.cpphs" + "*.gc" + "*.hs" + "*.hsc" + "*.hsig" + "*.lhs" + "*.lhsig" + "*.ly" + "*.x" + "*.y" ]; }) ]; @@ -33,11 +46,11 @@ in # https://github.com/tfausak/cabal-gild/issues/35 command = pkgs.writeShellApplication { name = "cabal-gild-wrapper"; - text = '' - for file in "$@"; do - ${lib.getExe cfg.package} --io="$file" - done - ''; + # The cabal file needs to be formatted by a formatter along with other Haskell source code. + # For example, module completion by `cabal-gild: discover`. + # It is difficult to determine this strictly. + # Since formatting with cabal-gild doesn't take much time, we execute it speculatively. + text = ''${pkgs.git}/bin/git ls-files -z "*.cabal"|${pkgs.parallel}/bin/parallel --null "${lib.getExe cfg.package} --io {}"''; }; }; };