treefmt icon indicating copy to clipboard operation
treefmt copied to clipboard

Add static as well as dynamic shell completion scripts

Open a-kenji opened this issue 10 months ago • 7 comments

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

When using treefmt I am always trying to autocomplete flags.

Especially when setting treefmt --formatters [FORMATTER..].

Describe the solution you'd like

I would like to have static completions for all the flags that treefmt provides.

I would also really like dynamic completions, especially for the --formatters flags, since I don't always want to look into the configuration how the formatter is actually named right now. I expect this could help save some time and frustration for people.

a-kenji avatar Mar 03 '25 18:03 a-kenji

It would appear cobra already added this for us; it was just hidden and poorly documented (I can't find this from the main site):

❯ result/bin/treefmt completion
Generate the autocompletion script for treefmt for the specified shell.
See each sub-command's help for details on how to use the generated script.

Usage:
  treefmt completion [command]

Available Commands:
  bash        Generate the autocompletion script for bash
  fish        Generate the autocompletion script for fish
  powershell  Generate the autocompletion script for powershell
  zsh         Generate the autocompletion script for zsh

Flags:
  -h, --help   help for completion

Use "treefmt completion [command] --help" for more information about a command.

@a-kenji give it a go and let me know how you get on.

I think this isn't showing up in the help because of how we take over the root command. I've been wanting to switch to a sub command approach for a while, with format being the default sub command. This would be the first breaking change in the cli.

In the meantime, if this works well, we can update the docs at least to make it clearer.

brianmcgee avatar Mar 04 '25 12:03 brianmcgee

It currently doesn't work because it conflicts with the treefmt root command. It thinks that the user wants to format the "./completion" sub-folder.

diff --git i/pkgs/by-name/tr/treefmt2/package.nix w/pkgs/by-name/tr/treefmt2/package.nix
index 0aca91e20c5c..f9a4b98d59d7 100644
--- i/pkgs/by-name/tr/treefmt2/package.nix
+++ w/pkgs/by-name/tr/treefmt2/package.nix
@@ -2,6 +2,7 @@
   lib,
   buildGoModule,
   fetchFromGitHub,
+  installShellFiles,
 }:
 buildGoModule rec {
   pname = "treefmt";
@@ -20,6 +21,8 @@ buildGoModule rec {
 
   env.CGO_ENABLED = 1;
 
+  nativeBuildInputs = [ installShellFiles ];
+
   ldflags = [
     "-s"
     "-w"
@@ -27,6 +30,17 @@ buildGoModule rec {
     "-X github.com/numtide/treefmt/v2/build.Version=v${version}"
   ];
 
+  postInstall = ''
+    # FIXME: treefmt complains if the file is not found
+    touch treefmt.toml
+    export HOME=$PWD
+
+    installShellCompletion --cmd treefmt \
+      --bash <($out/bin/treefmt completion bash) \
+      --fish <($out/bin/treefmt completion fish) \
+      --zsh <($out/bin/treefmt completion zsh)
+  '';
+
   meta = {
     description = "one CLI to format the code tree";
     homepage = "https://github.com/numtide/treefmt";

zimbatm avatar Mar 04 '25 13:03 zimbatm

Ah, makes sense.

I like that the default command doesn't require you having to write treefmt format ... or something similar, but at the same time we need to be able to start expanding the list of subcommands 🤔

brianmcgee avatar Mar 04 '25 15:03 brianmcgee

I think I can re-work how we're using cobra to be support a default command for format and permit other kinds of sub commands.

brianmcgee avatar Mar 04 '25 16:03 brianmcgee

I did a quick experiment https://github.com/brianmcgee/cobra-experiments

It should be possible to re-work how we use cobra to setup format as a default subcommand.

We will have to be careful to extract some common flags such as changing work directory etc. We can add these as part of a PersistentPreRunE on the root command to ensure certain actions are performed before any command.

This should also make the default completions work.

I'll hopefully have some time later in the week to attempt this.

brianmcgee avatar Mar 04 '25 16:03 brianmcgee

@a-kenji give it a go and let me know how you get on.

Thanks, I will do that as soon I get a little downtime.

a-kenji avatar Mar 04 '25 17:03 a-kenji

Re-working to allow subcommands is fraught with issues. For example, suppose you have a directory or file called init and you want to run the init subcommand. In that case, there's a clash if we want the default behaviour to be formatting without having to specify the format subcommand.

I'm going to look at a simple flag approach instead, something like --completions to generate the completions. This will avoid having to re-work our use of cobra and the issue mentioned above.

brianmcgee avatar Mar 11 '25 16:03 brianmcgee