git-bug

  1#compdef _git-bug git-bug
  2
  3# zsh completion for git-bug                              -*- shell-script -*-
  4
  5__git-bug_debug()
  6{
  7    local file="$BASH_COMP_DEBUG_FILE"
  8    if [[ -n ${file} ]]; then
  9        echo "$*" >> "${file}"
 10    fi
 11}
 12
 13_git-bug()
 14{
 15    local shellCompDirectiveError=1
 16    local shellCompDirectiveNoSpace=2
 17    local shellCompDirectiveNoFileComp=4
 18    local shellCompDirectiveFilterFileExt=8
 19    local shellCompDirectiveFilterDirs=16
 20
 21    local lastParam lastChar flagPrefix requestComp out directive compCount comp lastComp
 22    local -a completions
 23
 24    __git-bug_debug "\n========= starting completion logic =========="
 25    __git-bug_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
 26
 27    # The user could have moved the cursor backwards on the command-line.
 28    # We need to trigger completion from the $CURRENT location, so we need
 29    # to truncate the command-line ($words) up to the $CURRENT location.
 30    # (We cannot use $CURSOR as its value does not work when a command is an alias.)
 31    words=("${=words[1,CURRENT]}")
 32    __git-bug_debug "Truncated words[*]: ${words[*]},"
 33
 34    lastParam=${words[-1]}
 35    lastChar=${lastParam[-1]}
 36    __git-bug_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
 37
 38    # For zsh, when completing a flag with an = (e.g., git-bug -n=<TAB>)
 39    # completions must be prefixed with the flag
 40    setopt local_options BASH_REMATCH
 41    if [[ "${lastParam}" =~ '-.*=' ]]; then
 42        # We are dealing with a flag with an =
 43        flagPrefix="-P ${BASH_REMATCH}"
 44    fi
 45
 46    # Prepare the command to obtain completions
 47    requestComp="${words[1]} __complete ${words[2,-1]}"
 48    if [ "${lastChar}" = "" ]; then
 49        # If the last parameter is complete (there is a space following it)
 50        # We add an extra empty parameter so we can indicate this to the go completion code.
 51        __git-bug_debug "Adding extra empty parameter"
 52        requestComp="${requestComp} \"\""
 53    fi
 54
 55    __git-bug_debug "About to call: eval ${requestComp}"
 56
 57    # Use eval to handle any environment variables and such
 58    out=$(eval ${requestComp} 2>/dev/null)
 59    __git-bug_debug "completion output: ${out}"
 60
 61    # Extract the directive integer following a : from the last line
 62    local lastLine
 63    while IFS='\n' read -r line; do
 64        lastLine=${line}
 65    done < <(printf "%s\n" "${out[@]}")
 66    __git-bug_debug "last line: ${lastLine}"
 67
 68    if [ "${lastLine[1]}" = : ]; then
 69        directive=${lastLine[2,-1]}
 70        # Remove the directive including the : and the newline
 71        local suffix
 72        (( suffix=${#lastLine}+2))
 73        out=${out[1,-$suffix]}
 74    else
 75        # There is no directive specified.  Leave $out as is.
 76        __git-bug_debug "No directive found.  Setting do default"
 77        directive=0
 78    fi
 79
 80    __git-bug_debug "directive: ${directive}"
 81    __git-bug_debug "completions: ${out}"
 82    __git-bug_debug "flagPrefix: ${flagPrefix}"
 83
 84    if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
 85        __git-bug_debug "Completion received error. Ignoring completions."
 86        return
 87    fi
 88
 89    compCount=0
 90    while IFS='\n' read -r comp; do
 91        if [ -n "$comp" ]; then
 92            # If requested, completions are returned with a description.
 93            # The description is preceded by a TAB character.
 94            # For zsh's _describe, we need to use a : instead of a TAB.
 95            # We first need to escape any : as part of the completion itself.
 96            comp=${comp//:/\\:}
 97
 98            local tab=$(printf '\t')
 99            comp=${comp//$tab/:}
100
101            ((compCount++))
102            __git-bug_debug "Adding completion: ${comp}"
103            completions+=${comp}
104            lastComp=$comp
105        fi
106    done < <(printf "%s\n" "${out[@]}")
107
108    if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
109        # File extension filtering
110        local filteringCmd
111        filteringCmd='_files'
112        for filter in ${completions[@]}; do
113            if [ ${filter[1]} != '*' ]; then
114                # zsh requires a glob pattern to do file filtering
115                filter="\*.$filter"
116            fi
117            filteringCmd+=" -g $filter"
118        done
119        filteringCmd+=" ${flagPrefix}"
120
121        __git-bug_debug "File filtering command: $filteringCmd"
122        _arguments '*:filename:'"$filteringCmd"
123    elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
124        # File completion for directories only
125        local subDir
126        subdir="${completions[1]}"
127        if [ -n "$subdir" ]; then
128            __git-bug_debug "Listing directories in $subdir"
129            pushd "${subdir}" >/dev/null 2>&1
130        else
131            __git-bug_debug "Listing directories in ."
132        fi
133
134        _arguments '*:dirname:_files -/'" ${flagPrefix}"
135        if [ -n "$subdir" ]; then
136            popd >/dev/null 2>&1
137        fi
138    elif [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ] && [ ${compCount} -eq 1 ]; then
139        __git-bug_debug "Activating nospace."
140        # We can use compadd here as there is no description when
141        # there is only one completion.
142        compadd -S '' "${lastComp}"
143    elif [ ${compCount} -eq 0 ]; then
144        if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
145            __git-bug_debug "deactivating file completion"
146        else
147            # Perform file completion
148            __git-bug_debug "activating file completion"
149            _arguments '*:filename:_files'" ${flagPrefix}"
150        fi
151    else
152        _describe "completions" completions $(echo $flagPrefix)
153    fi
154}
155
156# don't run the completion function when being source-ed or eval-ed
157if [ "$funcstack[1]" = "_git-bug" ]; then
158	_git-bug
159fi