summaryrefslogtreecommitdiff
path: root/scripts/deploy/completions/zsh.zsh
blob: 99ecd09eaa6c296ea60b76622e0bfae7f411d06e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/usr/bin/env zsh
_jvn_completion() {
    local -a args
    local suggestions

    local buffer="$BUFFER"
    local cursor="$CURSOR"
    local current_word="${words[$CURRENT]}"
    local previous_word=""
    local command_name="${words[1]}"
    local word_index="$CURRENT"

    if [[ $CURRENT -gt 1 ]]; then
        previous_word="${words[$((CURRENT-1))]}"
    fi

    args=(
        -f "${buffer//-/^}"
        -C "$cursor"
        -w "${current_word//-/^}"
        -p "${previous_word//-/^}"
        -c "$command_name"
        -i "$word_index"
        -a "${(@)words//-/^}"
        -F "zsh"
    )

    suggestions=$(jvn_comp "${args[@]}" 2>/dev/null)

    if [[ $? -eq 0 ]] && [[ -n "$suggestions" ]]; then
        local -a completions
        completions=(${(f)suggestions})

        if [[ "${completions[1]}" == "_file_" ]]; then
            shift completions
            _files
        else
            local -a parsed_completions
            for item in "${completions[@]}"; do
                if [[ "$item" =~ '^([^$]+)\$\((.+)\)$' ]]; then
                    parsed_completions+=("${match[1]}:${match[2]}")
                else
                    parsed_completions+=("$item")
                fi
            done

            if (( $+functions[_describe] )); then
                _describe 'jvn commands' parsed_completions
            else
                local -a simple_completions
                for item in "${parsed_completions[@]}"; do
                    if [[ "$item" =~ '^([^:]+):(.+)$' ]]; then
                        simple_completions+=("${match[1]}")
                    else
                        simple_completions+=("$item")
                    fi
                done
                compadd -a simple_completions
            fi
        fi
    fi
}

compdef _jvn_completion jvn

if [[ $? -ne 0 ]]; then
    compctl -K _jvn_completion jvn
fi