use subspace_product::{MCP_USAGE, ProductContext, UPGRADE_USAGE};
const COMPLETION_USAGE: &str = r#"cmd "completion" effect="read" help="Print a shell completion script for the current shell" {
arg "<shell>" help="Target shell" {
choices "bash" "zsh" "fish"
}
}
cmd "__complete-word" hide=#true effect="read" help="Internal: list completion candidates for a partial command line" {
arg "[words]..." help="Words after the shell completion separator"
}"#;
pub(crate) const COMPLETION: &str = "completion";
pub(crate) use subspace_product::COMPLETE_WORD_COMMAND as COMPLETE_WORD;
pub fn tree(context: &ProductContext) -> Result<usage::Spec, String> {
let upgrade = if context.product.update.is_some() {
UPGRADE_USAGE
} else {
""
};
format!(
"bin {:?}\nversion {:?}\n{COMPLETION_USAGE}\n{MCP_USAGE}\n{upgrade}\n",
context.product.binary_name.as_str(),
context.version,
)
.parse::<usage::Spec>()
.map_err(|error| error.to_string())
}
pub(crate) fn declares_framework_completion(spec: &usage::Spec) -> bool {
let framework = format!("bin {:?}\n{COMPLETION_USAGE}", spec.bin).parse::<usage::Spec>();
let declared = |spec: &usage::Spec| {
spec.cmd
.subcommands
.get(COMPLETION)
.and_then(|command| serde_json::to_value(command).ok())
};
framework.is_ok_and(|framework| {
declared(spec).is_some_and(|ours| Some(ours) == declared(&framework))
})
}
pub fn complete_word(spec: &usage::Spec, words: &[String]) -> Vec<String> {
let Some((prefix, preceding)) = words.split_last() else {
return Vec::new();
};
if !prefix.starts_with('-')
&& let Some((flag_word, before_flag)) = preceding.split_last()
&& flag_word.starts_with('-')
{
return flag_choices(spec, before_flag, flag_word, prefix);
}
let argv: Vec<String> = std::iter::once(spec.bin.clone())
.chain(preceding.iter().cloned())
.collect();
let Ok(output) = usage::parse::Parser::new(spec).explain(&argv) else {
return Vec::new();
};
let mut candidates: Vec<String> = if prefix.starts_with('-') {
output
.completion_flags()
.into_keys()
.filter(|key| key.starts_with(prefix.as_str()))
.collect()
} else {
let choices = output
.next_arg
.as_ref()
.and_then(|arg| arg.choices.as_ref())
.map(|choices| choices.choices.clone())
.unwrap_or_default();
output
.cmd
.subcommands
.iter()
.filter(|(_, command)| !command.hide)
.map(|(name, _)| name.clone())
.chain(choices)
.filter(|word| word.starts_with(prefix.as_str()))
.collect()
};
candidates.sort();
candidates.dedup();
candidates
}
fn flag_choices(
spec: &usage::Spec,
before_flag: &[String],
flag_word: &str,
prefix: &str,
) -> Vec<String> {
let argv: Vec<String> = std::iter::once(spec.bin.clone())
.chain(before_flag.iter().cloned())
.collect();
let Ok(output) = usage::parse::Parser::new(spec).explain(&argv) else {
return Vec::new();
};
let mut candidates: Vec<String> = output
.completion_flags()
.get(flag_word)
.and_then(|flag| flag.arg.as_ref())
.and_then(|arg| arg.choices.as_ref())
.map(|choices| {
choices
.choices
.iter()
.filter(|choice| choice.starts_with(prefix))
.cloned()
.collect()
})
.unwrap_or_default();
candidates.sort();
candidates.dedup();
candidates
}
pub(crate) fn init_script(bin: &str, shell: &str) -> Option<String> {
let bin_fn = bin.replace(['-', ' '], "_");
match shell {
"bash" => Some(format!(
r#"_{bin_fn}_complete() {{
local cur="${{COMP_WORDS[COMP_CWORD]}}"
local words=("${{COMP_WORDS[@]:1:COMP_CWORD}}")
COMPREPLY=($(compgen -W "$(command {bin} __complete-word -- "${{words[@]}}" 2>/dev/null)" -- "$cur"))
}}
complete -F _{bin_fn}_complete {bin}
"#
)),
"zsh" => Some(format!(
r#"#compdef {bin}
_{bin_fn}_complete() {{
local -a words=("${{(@)words[2,CURRENT]}}")
local -a candidates
candidates=("${{(@f)$(command {bin} __complete-word -- "${{words[@]}}" 2>/dev/null)}}")
compadd -a candidates
}}
compdef _{bin_fn}_complete {bin}
"#
)),
"fish" => Some(format!(
r#"function __{bin_fn}_complete_word
set -l tokens (commandline -opc)
set -l cur (commandline -ct)
command {bin} __complete-word -- $tokens[2..-1] $cur 2>/dev/null
end
complete -c {bin} -f -a '(__{bin_fn}_complete_word)'
"#
)),
_ => None,
}
}