Skip to content

Commit

Permalink
fix(complete): make git diff git <TAB> Bash-complete for git diff
Browse files Browse the repository at this point in the history
Early in the Bash-completion script, we build up a string that
identifies the command or subcommand. When we see the top-level
command's name (e.g. `git`) we set the command so far to that
value. We do that regardless of where in the argument list it
appears. For example, if the argument list is `git diff git`, we set
the current command to `git` when run into it the second time. We
therefore suggest arguments to the top-level command afterwards, which
is not correct.

This patch fixes that by also considering the string that identifies
the command so far, so we only set the overall command to `git` if the
command so far is the empty string.

This is actually just a step on the way to getting completion to work
for aliases of subcommands.

Closes clap-rs#4273
  • Loading branch information
martinvonz committed Sep 29, 2022
1 parent 2ae1f09 commit 273115e
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 37 deletions.
6 changes: 3 additions & 3 deletions clap_complete/src/shells/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ impl Generator for Bash {
for i in ${{COMP_WORDS[@]}}
do
case \"${{i}}\" in
\"$1\")
case \"${{cmd}},${{i}}\" in
\",$1\")
cmd=\"{cmd}\"
;;{subcmds}
*)
Expand Down Expand Up @@ -86,7 +86,7 @@ fn all_subcommands(cmd: &Command) -> String {

subcmds.extend(scs.iter().map(|sc| {
format!(
"{name})
"*,{name})
cmd+=\"__{fn_name}\"
;;",
name = sc,
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
*)
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/tests/snapshots/basic.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
help)
*,help)
cmd+="__help"
;;
test)
*,test)
cmd+="__test"
;;
*)
Expand Down
8 changes: 4 additions & 4 deletions clap_complete/tests/snapshots/feature_sample.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
help)
*,help)
cmd+="__help"
;;
test)
*,test)
cmd+="__test"
;;
*)
Expand Down
18 changes: 9 additions & 9 deletions clap_complete/tests/snapshots/quoting.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
cmd-backslash)
*,cmd-backslash)
cmd+="__cmd__backslash"
;;
cmd-backticks)
*,cmd-backticks)
cmd+="__cmd__backticks"
;;
cmd-brackets)
*,cmd-brackets)
cmd+="__cmd__brackets"
;;
cmd-double-quotes)
*,cmd-double-quotes)
cmd+="__cmd__double__quotes"
;;
cmd-expansions)
*,cmd-expansions)
cmd+="__cmd__expansions"
;;
cmd-single-quotes)
*,cmd-single-quotes)
cmd+="__cmd__single__quotes"
;;
help)
*,help)
cmd+="__help"
;;
*)
Expand Down
14 changes: 7 additions & 7 deletions clap_complete/tests/snapshots/special_commands.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
help)
*,help)
cmd+="__help"
;;
some-cmd-with-hyphens)
*,some-cmd-with-hyphens)
cmd+="__some__cmd__with__hyphens"
;;
some-hidden-cmd)
*,some-hidden-cmd)
cmd+="__some__hidden__cmd"
;;
some_cmd)
*,some_cmd)
cmd+="__some_cmd"
;;
test)
*,test)
cmd+="__test"
;;
*)
Expand Down
12 changes: 6 additions & 6 deletions clap_complete/tests/snapshots/sub_subcommands.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
help)
*,help)
cmd+="__help"
;;
some_cmd)
*,some_cmd)
cmd+="__some_cmd"
;;
sub_cmd)
*,sub_cmd)
cmd+="__sub_cmd"
;;
test)
*,test)
cmd+="__test"
;;
*)
Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/value_hint.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ _my-app() {

for i in ${COMP_WORDS[@]}
do
case "${i}" in
"$1")
case "${cmd},${i}" in
",$1")
cmd="my__app"
;;
*)
Expand Down

0 comments on commit 273115e

Please sign in to comment.