Skip to content

Commit

Permalink
completion: introduce __gitcomp_nl_append ()
Browse files Browse the repository at this point in the history
There are situations where multiple classes of completions possible. For
example

  branch.<TAB>

should try to complete

  branch.master.
  branch.autosetupmerge
  branch.autosetuprebase

The first candidate has the suffix ".", and the second/ third candidates
have the suffix " ". To facilitate completions of this kind, create a
variation of __gitcomp_nl () that appends to the existing list of
completion candidates, COMPREPLY.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Ramkumar Ramachandra authored and Junio C Hamano committed Jan 6, 2014
1 parent d028b89 commit f33c2c0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
22 changes: 18 additions & 4 deletions contrib/completion/git-completion.bash
Original file line number Diff line number Diff line change
Expand Up @@ -178,16 +178,22 @@ _get_comp_words_by_ref ()
}
fi

__gitcompadd ()
__gitcompappend ()
{
local i=0
local i=${#COMPREPLY[@]}
for x in $1; do
if [[ "$x" == "$3"* ]]; then
COMPREPLY[i++]="$2$x$4"
fi
done
}

__gitcompadd ()
{
COMPREPLY=()
__gitcompappend "$@"
}

# Generates completion reply, appending a space to possible completion words,
# if necessary.
# It accepts 1 to 4 arguments:
Expand Down Expand Up @@ -218,6 +224,14 @@ __gitcomp ()
esac
}

# Variation of __gitcomp_nl () that appends to the existing list of
# completion candidates, COMPREPLY.
__gitcomp_nl_append ()
{
local IFS=$'\n'
__gitcompappend "$1" "${2-}" "${3-$cur}" "${4- }"
}

# Generates completion reply from newline-separated possible completion words
# by appending a space to all of them.
# It accepts 1 to 4 arguments:
Expand All @@ -229,8 +243,8 @@ __gitcomp ()
# appended.
__gitcomp_nl ()
{
local IFS=$'\n'
__gitcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
COMPREPLY=()
__gitcomp_nl_append "$@"
}

# Generates completion reply with compgen from newline-separated possible
Expand Down
8 changes: 8 additions & 0 deletions contrib/completion/git-completion.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ __gitcomp_nl ()
compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
}

__gitcomp_nl_append ()
{
emulate -L zsh

local IFS=$'\n'
compadd -Q -S "${4- }" -p "${2-}" -- ${=1} && _ret=0
}

__gitcomp_file ()
{
emulate -L zsh
Expand Down

0 comments on commit f33c2c0

Please sign in to comment.