From a8361012d6009b3be6a2f6a06dbadc13b61b7619 Mon Sep 17 00:00:00 2001 From: Vikesh Raj Date: Tue, 6 Oct 2020 06:28:30 +0530 Subject: [PATCH] Complete recipes in bash completion script (#685) Modifies the bash completion script to complete both recipes and flags. `just ` will complete recipes, and `just -` will complete flags and options. --- completions/just.bash | 14 ++++++++++---- src/common.rs | 16 ++++++++-------- src/subcommand.rs | 42 +++++++++++++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 21 deletions(-) diff --git a/completions/just.bash b/completions/just.bash index eed0234..0146f85 100644 --- a/completions/just.bash +++ b/completions/just.bash @@ -21,10 +21,16 @@ _just() { case "${cmd}" in just) opts=" -q -u -v -e -l -h -V -f -d -s --dry-run --highlight --no-dotenv --no-highlight --quiet --clear-shell-args --unsorted --verbose --choose --dump --edit --evaluate --init --list --summary --variables --help --version --chooser --color --justfile --set --shell --shell-arg --working-directory --completions --show ... " - if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then - COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) - return 0 - fi + if [[ ${cur} == -* ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + elif [[ ${COMP_CWORD} -eq 1 ]]; then + local recipes=$(just --summary --color never 2> /dev/null) + if [[ $? -eq 0 ]]; then + COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") ) + return 0 + fi + fi case "${prev}" in --chooser) diff --git a/src/common.rs b/src/common.rs index c31f54b..fcd0d6a 100644 --- a/src/common.rs +++ b/src/common.rs @@ -18,14 +18,6 @@ pub(crate) use std::{ usize, vec, }; -// modules used in tests -#[cfg(test)] -pub(crate) use crate::testing; - -// structs and enums used in tests -#[cfg(test)] -pub(crate) use crate::{node::Node, tree::Tree}; - // dependencies pub(crate) use derivative::Derivative; pub(crate) use edit_distance::edit_distance; @@ -73,3 +65,11 @@ pub(crate) type CompilationResult<'a, T> = Result>; pub(crate) type ConfigResult = Result; pub(crate) type RunResult<'a, T> = Result>; pub(crate) type SearchResult = Result; + +// modules used in tests +#[cfg(test)] +pub(crate) use crate::testing; + +// structs and enums used in tests +#[cfg(test)] +pub(crate) use crate::{node::Node, tree::Tree}; diff --git a/src/subcommand.rs b/src/subcommand.rs index c2b090d..1f82f4f 100644 --- a/src/subcommand.rs +++ b/src/subcommand.rs @@ -113,8 +113,27 @@ _just "$@""#, ), ]; +const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[( + r#" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + fi"#, + r#" if [[ ${cur} == -* ]] ; then + COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) + return 0 + elif [[ ${COMP_CWORD} -eq 1 ]]; then + local recipes=$(just --summary --color never 2> /dev/null) + if [[ $? -eq 0 ]]; then + COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") ) + return 0 + fi + fi"#, +)]; + impl Subcommand { pub(crate) fn completions(shell: &str) -> Result<(), i32> { + use clap::Shell; + fn replace(haystack: &mut String, needle: &str, replacement: &str) -> Result<(), i32> { if let Some(index) = haystack.find(needle) { haystack.replace_range(index..index + needle.len(), replacement); @@ -129,7 +148,7 @@ impl Subcommand { } let shell = shell - .parse::() + .parse::() .expect("Invalid value for clap::Shell"); let buffer = Vec::new(); @@ -138,14 +157,19 @@ impl Subcommand { let buffer = cursor.into_inner(); let mut script = String::from_utf8(buffer).expect("Clap completion not UTF-8"); - if let clap::Shell::Zsh = shell { - for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS { - replace(&mut script, needle, replacement)?; - } - } - - if let clap::Shell::Fish = shell { - script.insert_str(0, FISH_RECIPE_COMPLETIONS); + match shell { + Shell::Bash => + for (needle, replacement) in BASH_COMPLETION_REPLACEMENTS { + replace(&mut script, needle, replacement)?; + }, + Shell::Fish => { + script.insert_str(0, FISH_RECIPE_COMPLETIONS); + }, + Shell::Zsh => + for (needle, replacement) in ZSH_COMPLETION_REPLACEMENTS { + replace(&mut script, needle, replacement)?; + }, + _ => {}, } println!("{}", script.trim());