numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
602 stars 37 forks source link

Dont format in place, just dump changes ? #219

Closed teto closed 2 months ago

teto commented 1 year ago

Is your feature request related to a problem? Please describe.

Sometimes we dont want to format the code in place but have a look at what changes it proposes first. We've had bad experience with stylish-haskell which depending on the locales configuration would just delete half the code.

Describe the solution you'd like Not sure what I would like, maybe a different set of flags on a per formatter basis for inplace formatting and just checking. This would require a new command on treefmt too to be able to run "see-changes" and "inplace-format".

Describe alternatives you've considered

I've thought of overriding the default flags of the different formatters not to format in place like:

          # we override our default config with flags that disable inplace formatting
          treefmt = (flake-inputs.treefmt-nix.lib.mkWrapper pkgs (treefmtConfig // {
            settings.formatter.nixpkgs-fmt.options = ["--check"];
            settings.formatter.stylish-haskell.options = [ "-r" ];
            programs.hlint.enable = true;
          }));

but seems like it triggers other issues error: The optionsettings.formatter.nixpkgs-fmt.command' is used but not defined.`

That's probably the simplest for everyone. Guess I am just curious about the scope of treefmt.

zimbatm commented 1 year ago

This can be handled by treefmt direclty, without passing the options down to the tools.

Treefmt can already deal with buffers. Eg to get a sneak peek of a generated output:

$ treefmt --stdin src/eval_cache.rs < src/eval_cache.rs 

Potentially we could add a new --try option that takes a single file as a shortcut.

nifoc commented 1 year ago

(Sorry for posting on this older issue, but I think this might be related)

I use treefmt-nix with flake-parts, and running (something like) the above command does not work for me:

$ treefmt --stdin flake.nix < flake.nix
[ERR]: treefmt.toml could not be found in /Users/daniel/.config/nixpkgs and up. Use the --init option to create one.

treefmt is the wrapper script that is generated by treefmt-nix, so --config-file is specified in the wrapper script.

No combination of --config-file and --tree-root appears to work.

brianmcgee commented 2 months ago

I think this is supported with --stdin as @zimbatm described above.

I don't think the issues @nifoc mentioned either are an issue any more.