treefmt icon indicating copy to clipboard operation
treefmt copied to clipboard

Dont format in place, just dump changes ?

Open teto opened this issue 2 years ago • 2 comments

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 option settings.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.

teto avatar Jan 31 '23 13:01 teto

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.

zimbatm avatar Jan 31 '23 14:01 zimbatm

(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.

nifoc avatar Jul 21 '23 18:07 nifoc

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.

brianmcgee avatar Aug 19 '24 15:08 brianmcgee