Skip to content

Commit

Permalink
feat: Shell completion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Mar 8, 2024
1 parent ae06ddf commit 39bc6eb
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 1 deletion.
10 changes: 10 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ glob = "0.3.1"
const_format = "0.2.32"
tree-sitter-go = "0.20.0"
tree-sitter-rust = "0.20.4"
clap_complete = "4.5.1"

[features]
all = ["german", "symbols"]
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,13 @@ cargo add srgn

See [here](#rust-library) for more.

### Shell completions

[Various
shells](https://docs.rs/clap_complete/4.5.1/clap_complete/shells/enum.Shell.html#variants)
are supported for shell completion scripts. For example, append `eval "$(srgn
--completions zsh)"` to `~/.zshrc` for completions in ZSH.

## Walkthrough

The tool is designed around **scopes** and **actions**. Scopes narrow down the parts of
Expand Down
27 changes: 26 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,14 @@ fn main() -> Result<()> {
.format_timestamp_micros() // High precision is nice for benchmarks
.init();

if let Some(shell) = args.shell {
debug!("Generating completions file for {shell:?}.");
cli::print_completions(shell, &mut cli::Cli::command());
debug!("Done generating completions file, exiting.");

return Ok(());
}

info!("Launching app with args: {:?}", args);

debug!("Assembling scopers.");
Expand Down Expand Up @@ -421,7 +429,8 @@ fn level_filter_from_env_and_verbosity(additional_verbosity: u8) -> LevelFilter
}

mod cli {
use clap::{builder::ArgPredicate, ArgAction, Parser};
use clap::{builder::ArgPredicate, ArgAction, Command, CommandFactory, Parser};
use clap_complete::{generate, Generator, Shell};
use srgn::{
scoping::langs::{
csharp::{CustomCSharpQuery, PremadeCSharpQuery},
Expand Down Expand Up @@ -458,6 +467,13 @@ mod cli {
)]
pub scope: String,

/// Print shell completions for the given shell
// This thing needs to live up here to show up within `Options` next to `--help`
// and `--version`. Further down, it'd show up in the wrong section because we
// alter `next_help_heading`.
#[arg(long = "completions", value_enum, verbatim_doc_comment)]
pub shell: Option<Shell>,

#[command(flatten)]
pub composable_actions: ComposableActions,

Expand All @@ -475,6 +491,11 @@ mod cli {
pub german_options: GermanOptions,
}

/// https://github.com/clap-rs/clap/blob/f65d421607ba16c3175ffe76a20820f123b6c4cb/clap_complete/examples/completion-derive.rs#L69
pub(super) fn print_completions<G: Generator>(gen: G, cmd: &mut Command) {
generate(gen, cmd, cmd.get_name().to_string(), &mut std::io::stdout());
}

#[derive(Parser, Debug)]
#[group(required = false, multiple = true)]
#[command(next_help_heading = "Options (global)")]
Expand Down Expand Up @@ -722,6 +743,10 @@ mod cli {
pub(super) fn init() -> Self {
Self::parse()
}

pub(super) fn command() -> clap::Command {
<Self as CommandFactory>::command()
}
}
}

Expand Down

0 comments on commit 39bc6eb

Please sign in to comment.