1695b41eeSopenharmony_ci# Copyright 2011 Google Inc. All Rights Reserved.
2695b41eeSopenharmony_ci#
3695b41eeSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
4695b41eeSopenharmony_ci# you may not use this file except in compliance with the License.
5695b41eeSopenharmony_ci# You may obtain a copy of the License at
6695b41eeSopenharmony_ci#
7695b41eeSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
8695b41eeSopenharmony_ci#
9695b41eeSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
10695b41eeSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
11695b41eeSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12695b41eeSopenharmony_ci# See the License for the specific language governing permissions and
13695b41eeSopenharmony_ci# limitations under the License.
14695b41eeSopenharmony_ci
15695b41eeSopenharmony_ci# Add the following to your .bashrc to tab-complete ninja targets
16695b41eeSopenharmony_ci#   . path/to/ninja/misc/bash-completion
17695b41eeSopenharmony_ci
18695b41eeSopenharmony_ci_ninja_target() {
19695b41eeSopenharmony_ci    local cur prev targets dir line targets_command OPTIND
20695b41eeSopenharmony_ci
21695b41eeSopenharmony_ci    # When available, use bash_completion to:
22695b41eeSopenharmony_ci    #   1) Complete words when the cursor is in the middle of the word
23695b41eeSopenharmony_ci    #   2) Complete paths with files or directories, as appropriate
24695b41eeSopenharmony_ci    if _get_comp_words_by_ref cur prev &>/dev/null ; then
25695b41eeSopenharmony_ci        case $prev in
26695b41eeSopenharmony_ci            -f)
27695b41eeSopenharmony_ci                _filedir
28695b41eeSopenharmony_ci                return 0
29695b41eeSopenharmony_ci                ;;
30695b41eeSopenharmony_ci            -C)
31695b41eeSopenharmony_ci                _filedir -d
32695b41eeSopenharmony_ci                return 0
33695b41eeSopenharmony_ci                ;;
34695b41eeSopenharmony_ci        esac
35695b41eeSopenharmony_ci    else
36695b41eeSopenharmony_ci        cur="${COMP_WORDS[COMP_CWORD]}"
37695b41eeSopenharmony_ci    fi
38695b41eeSopenharmony_ci
39695b41eeSopenharmony_ci    if [[ "$cur" == "--"* ]]; then
40695b41eeSopenharmony_ci        # there is currently only one argument that takes --
41695b41eeSopenharmony_ci	COMPREPLY=($(compgen -P '--' -W 'version' -- "${cur:2}"))
42695b41eeSopenharmony_ci    else
43695b41eeSopenharmony_ci	dir="."
44695b41eeSopenharmony_ci	line=$(echo ${COMP_LINE} | cut -d" " -f 2-)
45695b41eeSopenharmony_ci        # filter out all non relevant arguments but keep C for dirs
46695b41eeSopenharmony_ci	while getopts :C:f:j:l:k:nvd:t: opt $line; do
47695b41eeSopenharmony_ci	    case $opt in
48695b41eeSopenharmony_ci                # eval for tilde expansion
49695b41eeSopenharmony_ci		C) eval dir="$OPTARG" ;;
50695b41eeSopenharmony_ci	    esac
51695b41eeSopenharmony_ci	done;
52695b41eeSopenharmony_ci	targets_command="eval ninja -C \"${dir}\" -t targets all 2>/dev/null | cut -d: -f1"
53695b41eeSopenharmony_ci	COMPREPLY=($(compgen -W '`${targets_command}`' -- "$cur"))
54695b41eeSopenharmony_ci    fi
55695b41eeSopenharmony_ci    return
56695b41eeSopenharmony_ci}
57695b41eeSopenharmony_cicomplete -F _ninja_target ninja
58