feat: add shell completions

This commit is contained in:
Amaan Qureshi 2024-09-24 12:37:31 -04:00
parent d3c262a104
commit 1f0feb5254
4 changed files with 34 additions and 2 deletions

10
Cargo.lock generated
View file

@ -231,6 +231,15 @@ dependencies = [
"strsim",
]
[[package]]
name = "clap_complete"
version = "4.5.29"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8937760c3f4c60871870b8c3ee5f9b30771f792a7045c48bcbba999d7d6b3b8e"
dependencies = [
"clap",
]
[[package]]
name = "clap_derive"
version = "4.5.18"
@ -1468,6 +1477,7 @@ dependencies = [
"anyhow",
"bstr",
"clap",
"clap_complete",
"ctor",
"ctrlc",
"dirs",

View file

@ -50,6 +50,7 @@ clap = { version = "4.5.18", features = [
"help",
"unstable-styles",
] }
clap_complete = "4.5.29"
ctor = "0.2.8"
ctrlc = { version = "3.4.5", features = ["termination"] }
dirs = "5.0.1"

View file

@ -29,6 +29,7 @@ anstyle.workspace = true
anyhow.workspace = true
bstr.workspace = true
clap.workspace = true
clap_complete.workspace = true
ctor.workspace = true
ctrlc.workspace = true
dirs.workspace = true

View file

@ -7,6 +7,7 @@ use std::{
use anstyle::{AnsiColor, Color, Style};
use anyhow::{anyhow, Context, Result};
use clap::{crate_authors, Args, Command, FromArgMatches as _, Subcommand};
use clap_complete::{generate, Shell};
use glob::glob;
use regex::Regex;
use tree_sitter::{ffi, Parser, Point};
@ -45,6 +46,7 @@ enum Commands {
Tags(Tags),
Playground(Playground),
DumpLanguages(DumpLanguages),
Complete(Complete),
}
#[derive(Args)]
@ -388,6 +390,18 @@ struct DumpLanguages {
pub config_path: Option<PathBuf>,
}
#[derive(Args)]
#[command(about = "Generate shell completions", alias = "comp")]
struct Complete {
#[arg(
long,
short,
value_enum,
help = "The shell to generate completions for"
)]
pub shell: Shell,
}
fn main() {
let result = run();
if let Err(err) = &result {
@ -426,9 +440,9 @@ fn run() -> Result<()> {
.arg_required_else_help(true)
.disable_help_subcommand(true)
.disable_colored_help(false);
let cli = Commands::augment_subcommands(cli);
let mut cli = Commands::augment_subcommands(cli);
let command = Commands::from_arg_matches(&cli.get_matches())?;
let command = Commands::from_arg_matches(&cli.clone().get_matches())?;
let current_dir = env::current_dir().unwrap();
let mut loader = loader::Loader::new()?;
@ -1000,6 +1014,12 @@ fn run() -> Result<()> {
);
}
}
Commands::Complete(complete_options) => {
let name = cli.get_name().to_string();
let stdout = &mut std::io::stdout();
generate(complete_options.shell, &mut cli, name, stdout);
}
}
Ok(())