Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions examples/formatter-cabal-fmt.toml
Original file line number Diff line number Diff line change
@@ -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 = []
17 changes: 16 additions & 1 deletion examples/formatter-cabal-gild.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
45 changes: 42 additions & 3 deletions programs/cabal-fmt.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
{ mkFormatterModule, ... }:
{
lib,
config,
pkgs,
mkFormatterModule,
...
}:
let
cfg = config.programs.cabal-fmt;
in
{
meta.maintainers = [ ];

Expand All @@ -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 {}"'';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how many formatters we have that format other files besides the one we passed it. It looks like the formatter specification does allow this:

It SHOULD processes only the specified files. Files that are not passed SHOULD never be formatted.

Not your ptoblem, but I'm just realizing this might be an issue for the nascent Stdin Spec: numtide/treefmt#586

Actual problem: this assumes we're in a git repo, but we expect formatters to work in plain source archives as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure how many formatters we have that format other files besides the one we passed it. It looks like the formatter specification does allow this:

I miss to check of https://github.com/numtide/treefmt/blob/c24e1f1b4fc3087f4cbfef4a0ad076bd86b129f0/docs/site/reference/formatter-spec.md?plain=1#L46.

However, cabal need format cause other module change.
I think that should not must, I want to allow exception.
Is it no good?
I want to auto fix cabal module by claude code hook.
I have no idea smart solution.

Actual problem: this assumes we're in a git repo, but we expect formatters to work in plain source archives as well.

I think to depend git that ok because nix depend git already.
I use git ls-files because I want to respect git ignore of real rule.
If prefer plain repo more than respect gitignore, I can use find.

};
# Clear args since we're handling them in the wrapper
options = lib.mkForce [ ];
};
};
}
25 changes: 19 additions & 6 deletions programs/cabal-gild.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
mkFormatterModule,
lib,
config,
pkgs,
mkFormatterModule,
...
}:

Expand All @@ -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"
];
})
];
Expand All @@ -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 {}"'';
};
};
};
Expand Down