completion: extract completing ctags symbol names into helper function

The previous commit doubled the number of __git_match_ctag()'s positional parameters, and, to keep the position of existing parameters for the sake of backwards compatibility, the prefix, current word and suffix parameters ended up in different order than in other functions accepting the same parameters. Then there is a condition checking the existence of the tag file before invoking this function. We could still live with this if there were only a single callsite, but the next commit will add a few more, so it's worth providing a cleaner interface. Add the wrapper function __git_complete_symbol(), which encompasses the condition for checking the presence of the tag file and filling COMPREPLY, and accepts '--opt=val'-style options with default values that keep callsites simpler. Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

SZEDER Gábor committed Mar 23, 2017 at 16:38 UTC 2f779f91764cb79303d8166b0df7049045e232ac
1 file changed +29 -4
contrib/completion/git-completion.bash
+29 -4
@@ -1529,6 +1529,34 @@ __git_match_ctag () {
1529 " "$2"
1530 }
1531
1532 +# Complete symbol names from a tag file.
1533 +# Usage: __git_complete_symbol [<option>]...
1534 +# --tags=<file>: The tag file to list symbol names from instead of the
1535 +# default "tags".
1536 +# --pfx=<prefix>: A prefix to be added to each symbol name.
1537 +# --cur=<word>: The current symbol name to be completed. Defaults to
1538 +# the current word to be completed.
1539 +# --sfx=<suffix>: A suffix to be appended to each symbol name instead
1540 +# of the default space.
1541 +__git_complete_symbol () {
1542 + local tags=tags pfx="" cur_="${cur-}" sfx=" "
1543 +
1544 + while test $# != 0; do
1545 + case "$1" in
1546 + --tags=*) tags="${1##--tags=}" ;;
1547 + --pfx=*) pfx="${1##--pfx=}" ;;
1548 + --cur=*) cur_="${1##--cur=}" ;;
1549 + --sfx=*) sfx="${1##--sfx=}" ;;
1550 + *) return 1 ;;
1551 + esac
1552 + shift
1553 + done
1554 +
1555 + if test -r "$tags"; then
1556 + __gitcomp_direct "$(__git_match_ctag "$cur_" "$tags" "$pfx" "$sfx")"
1557 + fi
1558 +}
1559 +
1560 _git_grep ()
1561 {
1562 __git_has_doubledash && return
@@ -1554,10 +1582,7 @@ _git_grep ()
1582
1583 case "$cword,$prev" in
1584 2,*|*,-*)
1557 - if test -r tags; then
1558 - __gitcomp_direct "$(__git_match_ctag "$cur" tags "" " ")"
1559 - return
1560 - fi
1585 + __git_complete_symbol && return
1586 ;;
1587 esac
1588