Skip to content

Conversation

@SamuelMiksits
Copy link
Collaborator

Yay I created shell completion! This adds the following:

  • forsyde-devtools-exe and forsyde-lsp-exe to PATH. This makes it so you can run forsyde-devtools-exe directly without the usage of cabal exec forsyde-devtools-exe -- and having to manually type that out because cabal exec does not tab match.
  • Shell completion for bash/fish/zsh for options inside those 2 executables.

How it works

The file /.completions/generate_completions.sh is a bash script that generates completion scripts for bash/fish/zsh automatically. I created this since we might want to have this as a pre-commit hook or an automatic build task in the future so that we don't manually have to rerun it.

Then, in the nix flake I use the following

# Add completion script
source .completions/forsyde-devtools-exe/$0
source .completions/forsyde-lsp-exe/$0

where $0 retrieves the name of the current shell. This is where I am not entirely sure. If I run this in a "normal terminal" I get for instance:

$ echo $0
/bin/bash 

but if I run this inside nix develop I get:

$ echo $0
bash 

But if this can only be ran inside nix this should probably be fine? Then it should only be bash or zsh or fish without the /bin/ part in front of it.

Also, sorry, I couldn't help myself. I changed chmod +x .git/hooks/* to chmod u+x .git/hooks/* and didn't undo it before I commited, I can edit it back if you want.

Test

Start with

$ nix develop

from the root of forsyde-devtools. This should add forsyde-devtools-exe and forsyde-lsp-exe to PATH.
Then, try to enter forsyde-devtools-exe by typing

forsyde-d<TAB>

which should automatically shell complete to forsyde-devtools-exe.
Try that this also works with:

forsyde-l<TAB>

which should automatically shell complete to forsyde-lsp-exe.
Then, try to complete some options in the executable such as --output-forsyde-ir with

forsyde-d<TAB> --output-f<TAB>

which should complete to forsyde-devtools-exe --output-forsyde-ir.

Also, try that it works for the LSP with:

forsyde-l<TAB> --p<TAB>

which should complete to forsyde-lsp-exe --port.

@SamuelMiksits
Copy link
Collaborator Author

@sthaeron You use zsh right? So would be good if you could test it for that

@SamuelMiksits SamuelMiksits marked this pull request as draft November 20, 2025 12:39
@SamuelMiksits
Copy link
Collaborator Author

SamuelMiksits commented Nov 20, 2025

Converted to draft since I was having some issues. When I try to pass a file it can't find it which is kind of a problem
(EDIT: Okay seems like it works, but the issue is that bash completion for arguments doesn't work, since it is not enabled by default. This can be fixed by adding action:

-- Handle file input, always need to define a file
inputFile :: Parser Input
inputFile =
  InputFile
    <$> strArgument
      ( metavar "INPUT"
          <> help "Input filename"
          <> action "file"
      )

When I run this in my normal bash it works fine, but when I run in Nix I get

bash: line 1: compgen: command not found

this is typically a problem when you are in a non-interactive shell so I am stuck troubleshooting that right now)

CMDLINE=(${CMDLINE[@]} --bash-completion-word $arg)
done

COMPREPLY=( $(/home/sam/Documents/IL2232/forsyde-devtools/dist-newstyle/build/x86_64-linux/ghc-9.10.2/forsyde-devtools-0.0.0.1/x/forsyde-devtools-exe/build/forsyde-devtools-exe/forsyde-devtools-exe "${CMDLINE[@]}") )
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hmm, I guess it uses my "PWD" so it puts literally my path in there

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ah, yes. After replacing that with my own path it worked for me.

Copy link
Collaborator Author

@SamuelMiksits SamuelMiksits Nov 21, 2025

Choose a reason for hiding this comment

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

I guess the intention is that you should re-generate this script by running

$ ./.completions/generate_completions.sh

inside NixOS from the root forsyde-devtools/. My general problem is that the wiki for optparse-applicative says the following:

For a quick test, you can run something like (for a program called foo on the PATH):

$ source <(foo --bash-completion-script `which foo`)

Normally, the output of --bash-completion-script should be shipped with the program and copied to the appropriate directory (usually /etc/bash_completion.d/) during installation.

and I don't understand how I can ship this script with our project unless the binary is in like /usr/bin or something (since which resolves the path to something on my home path).

I can't really figure out how to get a PATH to our binary inside the bash completions that can calculate itself, that we can actually ship. Like do we literally have to put $(pwd) inside the completion scripts ourselves? I can put a command that runs the .completions/generate_completions.sh inside the Nix shell hook, but the issue is that we don't know if the binary exists yet when you do that (on first time clone of the repo I don't expect it to be built), so the which command would probably error out.

@ramadhanafif
Copy link
Collaborator

This is cool

@SamuelMiksits
Copy link
Collaborator Author

@sthaeron Now I have updated it so that you can autocomplete file paths for arguments, for both the compiler and LSP server. So this means that now you can do:

forsyde-devtools-exe --o<TAB>-f<TAB> ./e<TAB>m<TAB>S<TAB>03<TAB> --s<TAB>

(I mean you can technically also autocomplete the forsyde-devtools-exe binary)
to get to

forsyde-devtools-exe --output-forsyde-ir ./examples/model/SDF_example_003.hs --stdout

where it was previously not possible to autocomplete the path to the model. So we can leave this for now and revisit it once we have a real shippable binary.

@SamuelMiksits SamuelMiksits linked an issue Nov 27, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Shell completion for options and arguments for the binaries

3 participants