Skip to content

Commit

Permalink
Support recipe paths containing :: in Bash completion script (#1863)
Browse files Browse the repository at this point in the history
  • Loading branch information
crdx authored Jan 27, 2024
1 parent daf4520 commit e92ee92
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 5 deletions.
24 changes: 19 additions & 5 deletions completions/just.bash
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
_just() {
local i cur prev opts cmds
local i cur prev words cword opts cmds
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"

# Modules use "::" as the separator, which is considered a wordbreak character in bash.
# The _get_comp_words_by_ref function is a hack to allow for exceptions to this rule without
# modifying the global COMP_WORDBREAKS environment variable.
if type _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref -n : cur prev words cword
else
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words=$COMP_WORDS
cword=$COMP_CWORD
fi

cmd=""
opts=""

for i in ${COMP_WORDS[@]}
for i in ${words[@]}
do
case "${i}" in
"$1")
Expand All @@ -24,7 +35,7 @@ _just() {
if [[ ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
elif [[ ${COMP_CWORD} -eq 1 ]]; then
elif [[ ${cword} -eq 1 ]]; then
local recipes=$(just --summary 2> /dev/null)

if echo "${cur}" | \grep -qF '/'; then
Expand All @@ -35,6 +46,9 @@ _just() {

if [[ $? -eq 0 ]]; then
COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") )
if type __ltrim_colon_completions &>/dev/null; then
__ltrim_colon_completions "$cur"
fi
return 0
fi
fi
Expand Down
33 changes: 33 additions & 0 deletions src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,37 @@ pub(crate) const BASH_COMPLETION_REPLACEMENTS: &[(&str, &str)] = &[
fi"#,
),
(r" just)", r#" "$1")"#),
(
r"local i cur prev opts cmds",
r"local i cur prev words cword opts cmds",
),
(
r#" cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}""#,
r#"
# Modules use "::" as the separator, which is considered a wordbreak character in bash.
# The _get_comp_words_by_ref function is a hack to allow for exceptions to this rule without
# modifying the global COMP_WORDBREAKS environment variable.
if type _get_comp_words_by_ref &>/dev/null; then
_get_comp_words_by_ref -n : cur prev words cword
else
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words=$COMP_WORDS
cword=$COMP_CWORD
fi
"#,
),
(r"for i in ${COMP_WORDS[@]}", r"for i in ${words[@]}"),
(
r"elif [[ ${COMP_CWORD} -eq 1 ]]; then",
r"elif [[ ${cword} -eq 1 ]]; then",
),
(
r#"COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") )"#,
r#"COMPREPLY=( $(compgen -W "${recipes}" -- "${cur}") )
if type __ltrim_colon_completions &>/dev/null; then
__ltrim_colon_completions "$cur"
fi"#,
),
];

0 comments on commit e92ee92

Please sign in to comment.