aada4d62bff3fc2e1720da1da4c54d47b5f86fa3

Author
Israel Rodrigo Faria <israel.faria@gmail.com>
Committer
GitHub <noreply@github.com>
Date

Message

Add plugin for bazel completion (#6434)

Diff

This diff is truncated to protect this page.

  1diff --git a/plugins/bazel/README.md b/plugins/bazel/README.md
  2new file mode 100644
  3index 0000000000000000000000000000000000000000..e5ffe6ea10a46a313c69cfe701a1bd75f4acdbda
  4--- /dev/null
  5+++ b/plugins/bazel/README.md
  6@@ -0,0 +1,5 @@
  7+## Bazel autocomplete plugin
  8+
  9+A copy of the completion script from the
 10+[bazelbuild/bazel](https://github.com/bazelbuild/bazel/master/scripts/zsh_completion/_bazel)
 11+git repo.
 12diff --git a/plugins/bazel/_bazel b/plugins/bazel/_bazel
 13new file mode 100644
 14index 0000000000000000000000000000000000000000..827ce545c0db1e69e90ee699fd89ab036349b76a
 15--- /dev/null
 16+++ b/plugins/bazel/_bazel
 17@@ -0,0 +1,341 @@
 18+#compdef bazel
 19+
 20+# Copyright 2015 The Bazel Authors. All rights reserved.
 21+#
 22+# Licensed under the Apache License, Version 2.0 (the "License");
 23+# you may not use this file except in compliance with the License.
 24+# You may obtain a copy of the License at
 25+#
 26+#    http://www.apache.org/licenses/LICENSE-2.0
 27+#
 28+# Unless required by applicable law or agreed to in writing, software
 29+# distributed under the License is distributed on an "AS IS" BASIS,
 30+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 31+# See the License for the specific language governing permissions and
 32+# limitations under the License.
 33+
 34+# Installation
 35+# ------------
 36+#
 37+# 1. Add this script to a directory on your $fpath:
 38+#     fpath[1,0]=~/.zsh/completion/
 39+#     mkdir -p ~/.zsh/completion/
 40+#     cp scripts/zsh_completion/_bazel ~/.zsh/completion
 41+#
 42+# 2. Optionally, add the following to your .zshrc.
 43+#     zstyle ':completion:*' use-cache on
 44+#     zstyle ':completion:*' cache-path ~/.zsh/cache
 45+#
 46+#   This way, the completion script does not have to parse Bazel's options
 47+#   repeatedly.  The directory in cache-path must be created manually.
 48+#
 49+# 3. Restart the shell
 50+#
 51+# Options
 52+# -------
 53+#  completion:init:bazel:* cache-lifetime
 54+#    Lifetime for the completion cache (if turned on, default: 1 week)
 55+
 56+local curcontext="$curcontext" state line
 57+
 58+: ${BAZEL_COMPLETION_PACKAGE_PATH:=%workspace%}
 59+: ${BAZEL:=bazel}
 60+_bazel_b() { ${BAZEL} --noblock_for_lock "$@" 2>/dev/null; }
 61+
 62+# Default cache lifetime is 1 week
 63+zstyle -s ":completion:${curcontext}:" cache-lifetime lifetime
 64+if [[ -z "${lifetime}" ]]; then
 65+  lifetime=$((60*60*24*7))
 66+fi
 67+
 68+_bazel_cache_policy() {
 69+  local -a oldp
 70+  oldp=( "$1"(Nms+${lifetime}) )
 71+  (( $#oldp ))
 72+}
 73+
 74+_set_cache_policy() {
 75+  zstyle -s ":completion:*:$curcontext*" cache-policy update_policy
 76+
 77+  if [[ -z "$update_policy" ]]; then
 78+    zstyle ":completion:$curcontext*" cache-policy _bazel_cache_policy
 79+  fi
 80+}
 81+
 82+# Skips over all global arguments.  After invocation, OFFSET contains the
 83+# position of the bazel command in $words.
 84+_adapt_subcommand_offset() {
 85+  OFFSET=2
 86+  for w in ${words[2,-1]}; do
 87+    if [[ $w == (#b)-* ]]; then
 88+      (( OFFSET++ ))
 89+    else
 90+      return
 91+    fi
 92+  done
 93+}
 94+
 95+# Retrieve the cache but also check that the value is not empty.
 96+_bazel_safe_retrieve_cache() {
 97+  _retrieve_cache $1 && [[ ${(P)#2} -gt 0 ]]
 98+}
 99+
100+# Puts the name of the variable that contains the options for the bazel
101+# subcommand handed in as the first argument into the global variable
102+# _bazel_cmd_options.
103+_bazel_get_options() {
104+  local lcmd=$1
105+  _bazel_cmd_options=_bazel_${lcmd}_options
106+  _bazel_cmd_args=_bazel_${lcmd}_args
107+  if [[ ${(P)#_bazel_cmd_options} != 0 ]]; then
108+    return
109+  fi
110+  if _cache_invalid BAZEL_${lcmd}_options || _cache_invalid BAZEL_${lcmd}_args \
111+    || ! _bazel_safe_retrieve_cache BAZEL_${lcmd}_options ${_bazel_cmd_options} \
112+    || ! _retrieve_cache BAZEL_${lcmd}_args ${_bazel_cmd_args}; then
113+    if ! eval "$(_bazel_b help completion)"; then
114+      return
115+    fi
116+    local opts_var