]> git.ipfire.org Git - thirdparty/git.git/commitdiff
completion: put matching ctags symbol names directly into COMPREPLY
authorSZEDER Gábor <szeder.dev@gmail.com>
Thu, 23 Mar 2017 15:38:37 +0000 (16:38 +0100)
committerJunio C Hamano <gitster@pobox.com>
Thu, 23 Mar 2017 20:31:24 +0000 (13:31 -0700)
The one-liner awk script in __git_match_ctag() listing ctags symbol
names for 'git grep <TAB>' is already smart enough to list only symbol
names matching the current word to be completed.

Extend this helper function to accept prefix and suffix parameters to
be prepended and appended, respectively, to each listed symbol name in
the awk script, so its output won't require any additional processing
or filtering in the completion script before being handed over to
Bash.  Use the faster __gitcomp_direct() helper instead of
__gitcomp_nl() to fill the fully processed matching symbol names into
Bash's COMPREPLY array.

Right after 'git grep <TAB>' in current git.git with 14k+ symbol names
in the tag file, best of five:

  Before:

    $ time __gitcomp_nl "$(__git_match_ctag "" tags)"

    real    0m0.178s
    user    0m0.176s
    sys     0m0.000s

  After:

    $ time __gitcomp_direct "$(__git_match_ctag "" tags "" " ")"

    real    0m0.058s
    user    0m0.048s
    sys     0m0.008s

Signed-off-by: SZEDER Gábor <szeder.dev@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
contrib/completion/git-completion.bash

index bd07d9a7461021be6ab58b5083c34169e455f965..5ac609a79ca077bc67339146696039884b32d5c1 100644 (file)
@@ -1518,8 +1518,15 @@ _git_gitk ()
        _gitk
 }
 
-__git_match_ctag() {
-       awk "/^${1//\//\\/}/ { print \$1 }" "$2"
+# Lists matching symbol names from a tag (as in ctags) file.
+# 1: List symbol names matching this word.
+# 2: The tag file to list symbol names from.
+# 3: A prefix to be added to each listed symbol name (optional).
+# 4: A suffix to be appended to each listed symbol name (optional).
+__git_match_ctag () {
+       awk -v pfx="${3-}" -v sfx="${4-}" "
+               /^${1//\//\\/}/ { print pfx \$1 sfx }
+               " "$2"
 }
 
 _git_grep ()
@@ -1548,7 +1555,7 @@ _git_grep ()
        case "$cword,$prev" in
        2,*|*,-*)
                if test -r tags; then
-                       __gitcomp_nl "$(__git_match_ctag "$cur" tags)"
+                       __gitcomp_direct "$(__git_match_ctag "$cur" tags "" " ")"
                        return
                fi
                ;;