Skip to content

feat: add shell completions - #33

Merged
akriaueno merged 1 commit into
developfrom
feature/shell-completions
May 30, 2026
Merged

akriaueno merged 1 commit into
developfrom
feature/shell-completions

Conversation

@akriaueno

@akriaueno akriaueno commented May 30, 2026

Copy link
Copy Markdown
Owner

Summary

  • add why --completion <bash|zsh|fish> without introducing completion subcommands
  • delegate why <TAB> command-name completion to each shell's native command completion path
  • install generated completions from Nix and Homebrew packages
  • add Home Manager-backed E2E coverage for zsh/fish/bash completion installation and behavior

Testing

  • cargo +stable fmt --check
  • cargo +stable test --locked — 20 passed
  • cargo +stable clippy --all-targets -- -D warnings
  • nix build .#checks.x86_64-linux.home-manager-completions
  • nix flake check — all checks passed

Notes

  • nix flake check still warns about the existing homeManagerModules output name, but the check succeeds.

Summary by CodeRabbit

  • New Features

    • Added shell completion support for bash, zsh, and fish shells via --completion flag
    • Shell completions are automatically installed in Homebrew and Nix packages
  • Documentation

    • Added Shell Completion section to README with usage instructions and examples
  • Tests

    • Added end-to-end testing for shell completions with home-manager integration
    • Enhanced CI pipeline with additional Nix consistency checks

Review Change Stack

@coderabbitai

coderabbitai Bot commented May 30, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@akriaueno, we couldn't start this review because you've reached your PR review rate limit.

More reviews will be available in 43 minutes and 40 seconds. Learn how PR review limits work.

Your organization has run out of usage credits. Purchase more in the billing tab.

⌛ How to resolve this issue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available.

Please see our Fair Usage Limits Policy for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6eb61c10-b305-4b55-9801-17d32b08388e

📥 Commits

Reviewing files that changed from the base of the PR and between fa931d1 and 517e520.

⛔ Files ignored due to path filters (1)
  • flake.lock is excluded by !**/*.lock
📒 Files selected for processing (10)
  • .github/scripts/generate_formula.sh
  • .github/workflows/ci.yml
  • README.md
  • completions/_why
  • completions/why.bash
  • completions/why.fish
  • flake.nix
  • nix/package.nix
  • src/main.rs
  • tests/e2e/home-manager/completions.nix
📝 Walkthrough

Walkthrough

The PR adds shell completion generation to why-cli. Users can run why --completion <shell> to output completion scripts for bash, zsh, or fish. The CLI wires completions through Nix and Homebrew builds, validates them with an end-to-end home-manager test, and documents usage in the README.

Changes

Shell Completion Implementation and Distribution

Layer / File(s) Summary
CLI completion feature and argument parsing
src/main.rs
clap imports extend to support ValueEnum; Args struct adds optional --completion with CompletionShell enum; command argument becomes optional when completion is requested; main() prints completion script and exits early when --completion is present; static completion constants provided for bash, zsh, fish; unit tests validate argument parsing and script content.
Nix package completion installation
nix/package.nix
Add installShellFiles parameter; include in nativeBuildInputs; postInstall hook calls installShellCompletion with dynamic output from the built why binary for all three shells.
Flake home-manager integration and E2E checks
flake.nix
Add home-manager flake input with nixpkgs following; extend outputs to include home-manager; define homeManagerCompletionConfig via home-manager.lib.homeManagerConfiguration with programs.why and shell modules enabled; add checks.home-manager-completions output that runs the E2E test.
End-to-end completion testing
tests/e2e/home-manager/completions.nix
runCommand derivation installs why into home-manager profile, verifies required binaries exist, sources completion scripts, triggers completion functions with test input, asserts ls appears in results.
Documentation and distribution configuration
.github/scripts/generate_formula.sh, README.md, .github/workflows/ci.yml
README adds "Shell Completion" section with usage examples and delegation behavior; Homebrew formula script calls generate_completions_from_executable; CI adds nix flake check step.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • akriaueno/why-cli#29: Introduces home-manager integration for why, which this PR extends by adding completion generation and validation through home-manager.

Poem

A rabbitt's tale of shells so fine,
With completions that align—
Bash and zsh and fish complete,
Tab completion, oh so neat! 🐰✨
From nix to brew, they all agree.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 66.67% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: add shell completions' directly summarizes the main change—adding shell completion functionality to the CLI tool across multiple package formats.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/shell-completions

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/main.rs (1)

170-235: ⚡ Quick win

Generate shell completions from the clap derive model (reduce drift)
Handwritten BASH_COMPLETION/ZSH_COMPLETION/FISH_COMPLETION in src/main.rs can desync from your derive Parser/Args as flags, help text, or value-enums change.

In clap 4.5.x, the recommended approach is to build a clap::Command from the derived parser (CommandFactory / Opt::command or Parser::command) and feed it to clap_complete::generate for bash/zsh/fish (as in the official clap_complete completion-derive.rs example). Because the scripts are generated from clap’s current option metadata, enum/value changes stay in sync automatically whenever you regenerate (e.g., at build time or when handling your --completion request).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/main.rs` around lines 170 - 235, The hardcoded
BASH_COMPLETION/ZSH_COMPLETION/FISH_COMPLETION constants and the
completion_script function will drift from your clap derive types; replace them
by constructing a clap::Command from your derived parser (use Parser::command or
CommandFactory::command on your Opt/Args type) and call clap_complete::generate
(or generate_to) to produce the bash/zsh/fish output at runtime (e.g., when
handling the --completion option) rather than returning the static strings;
update completion_script to invoke the generator with the produced Command and
the requested CompletionShell and return or print the generated script
accordingly.
tests/e2e/home-manager/completions.nix (1)

39-51: 🏗️ Heavy lift

Make the zsh path assert completion candidates, not function internals.

This only proves that _why autoloaded and still references _path_commands; it does not verify that why l actually offers ls. A zsh-specific regression in the generated matches would still pass here, so the E2E coverage is weaker than the bash/fish branches.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/e2e/home-manager/completions.nix` around lines 39 - 51, The current
test only checks that the autoloaded function `_why` references
`_path_commands`, which verifies internals not user-visible completions; change
the tail of the test so after `compinit` and autoloading `_why` it invokes zsh's
completion machinery for the command prefix (e.g., simulate completing `why l`)
and asserts that the candidate `ls` appears in the output instead of grepping
for `_path_commands`; update the commands that currently write to
"$TMPDIR/zsh.out" to capture the completion result for `why l` and check for
`ls` (keeping use of `compinit`, `autoload -Uz _why`, and any existing TMPDIR
output handling).
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nix/package.nix`:
- Around line 18-22: postInstall currently runs the built binary ($out/bin/why
--completion ...) which breaks cross-builds; instead generate or install
completions using a build-platform runnable tool: move the completion generation
into the buildInputs/buildPackages side (e.g., use stdenv.buildPackages or
buildPackages to call the why binary built for the build platform or use a qemu
wrapper), or ship pre-generated completion files and reference them in
postInstall; update the postInstall block that calls installShellCompletion to
read completions from those pre-generated files or from a build-side generator
rather than executing $out/bin/why at build time.

---

Nitpick comments:
In `@src/main.rs`:
- Around line 170-235: The hardcoded
BASH_COMPLETION/ZSH_COMPLETION/FISH_COMPLETION constants and the
completion_script function will drift from your clap derive types; replace them
by constructing a clap::Command from your derived parser (use Parser::command or
CommandFactory::command on your Opt/Args type) and call clap_complete::generate
(or generate_to) to produce the bash/zsh/fish output at runtime (e.g., when
handling the --completion option) rather than returning the static strings;
update completion_script to invoke the generator with the produced Command and
the requested CompletionShell and return or print the generated script
accordingly.

In `@tests/e2e/home-manager/completions.nix`:
- Around line 39-51: The current test only checks that the autoloaded function
`_why` references `_path_commands`, which verifies internals not user-visible
completions; change the tail of the test so after `compinit` and autoloading
`_why` it invokes zsh's completion machinery for the command prefix (e.g.,
simulate completing `why l`) and asserts that the candidate `ls` appears in the
output instead of grepping for `_path_commands`; update the commands that
currently write to "$TMPDIR/zsh.out" to capture the completion result for `why
l` and check for `ls` (keeping use of `compinit`, `autoload -Uz _why`, and any
existing TMPDIR output handling).
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ec7bc32a-4e64-4d6e-a92b-dc6cfab7a342

📥 Commits

Reviewing files that changed from the base of the PR and between 25284ce and fa931d1.

⛔ Files ignored due to path filters (1)
  • flake.lock is excluded by !**/*.lock
📒 Files selected for processing (7)
  • .github/scripts/generate_formula.sh
  • .github/workflows/ci.yml
  • README.md
  • flake.nix
  • nix/package.nix
  • src/main.rs
  • tests/e2e/home-manager/completions.nix

Comment thread nix/package.nix Outdated
@akriaueno
akriaueno force-pushed the feature/shell-completions branch from fa931d1 to 517e520 Compare May 30, 2026 13:13
@akriaueno
akriaueno merged commit 5d37f92 into develop May 30, 2026
8 checks passed
@akriaueno
akriaueno deleted the feature/shell-completions branch May 30, 2026 13:24
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.

1 participant