This commit is contained in:
Christian Nieves
2022-07-28 17:35:07 +00:00
parent 106881fa6d
commit ea31968675
278 changed files with 48549 additions and 2 deletions

View File

@ -0,0 +1,66 @@
# Completion plugin for Piper / Google3
## Basics
This plugin shows "abbreviated" pathnames in Piper, as well as
supports tilde expansion and autocomplete to your workspaces in Piper.
To use it, copy the `google3` directory
(`/google/data/ro/users/mw/mweigel/oh-my-zsh/plugins/google3`) to your custom
plugin directory (by default this would be `~/.oh-my-zsh/custom/plugins`) and
add `google3` to the plugins array in your zshrc file:
```zsh
plugins=(... google3)
```
If you aren't using Oh-My-Zsh, copy the file `google3.plugin.zsh`
somewhere local on your workstation and source it from your zshrc
file. For example if you copied it to `~/.google3.plugin.zsh` then
you might add
```zsh
source ~/.google3.plugin.zsh
```
To your zshrc.
## Examples
Then, if you have a CitC client named "pager-setup" and you wanted to
go to the directory `/google/src/cloud/{{USERNAME}}/pager-setup`, you
could issue the following command:
```zsh
{{USERNAME}}:~$ cd ~[pager-setup:]
{{USERNAME}}:~[pager-setup:]$ pwd
/google/src/cloud/{{USERNAME}}/pager-setup
{{USERNAME}}:~[pager-setup:]$
```
You can also rely on tab-completion, e.g. hitting tab after typing the
following:
```zsh
{{USERNAME}}:~$ cd ~[pa
```
would - assuming you only have one Citc client that begins with "pa" -
expand to "pager-setup".
By default it will also smoosh "google3/java/com/google" in a path
down to "g3/jcg" and "google3/javatests/com/google" down to "g3/jtcg".
This can be disabled by setting the environment variable
GOOGLE3_PLUGIN_DISABLE_JCG to "true".
From [Zsh Hacks](http://go/eng-resources/zsh#java), this plugin also
incorporates the shell function `jt` for Java developers to quickly
switch back and forth between the current directory in the java
hierarchy and the javatests hierarchy.
## Changes
The code for this plugin resides in
[google3/experimental/users/mweigel/oh-my-zsh/plugins/google3/](http://google3/experimental/users/mweigel/oh-my-zsh/plugins/google3/). CLs
are gratefully reviewed, just add reviewer:
[mweigel](http://who/mweigel)

View File

@ -0,0 +1,114 @@
# show "abbreviated" pathnames in Piper, as well as support tilde expansion and
# autocomplete to your workspaces in Piper.
function jt() {
if [[ $PWD =~ '(.*)/javatests(.*)' ]]; then
cd "${match[1]}/java${match[2]}"
else
cd "${PWD/\/google3\/java//google3/javatests}"
fi
}
typeset -ga zsh_directory_name_functions
zsh_directory_name_functions+=("google3_directory_name")
expand_jcg() {
emulate -L zsh
setopt extendedglob
local -a match mbegin mend
if [[ $1 = (#b)(*)/g3/jcg(*) ]]; then
typeset -g expansion
expansion="$match[1]/google3/java/com/google$match[2]"
return 0;
elif [[ $1 = (#b)(*)/g3/jtcg(*) ]]; then
typeset -g expansion
expansion="$match[1]/google3/javatests/com/google$match[2]"
return 0;
fi
return 1
}
shrink_javacomgoogle() {
emulate -L zsh
setopt extendedglob
local -a match mbegin mend
if [[ $1 = (#b)(*)/google3/java/com/google(*) ]]; then
typeset -g expansion
expansion="$match[1]/g3/jcg$match[2]"
return 0;
elif [[ $1 = (#b)(*)/google3/javatests/com/google(*) ]]; then
typeset -g expansion
expansion="$match[1]/g3/jtcg$match[2]"
return 0;
fi
return 1
}
google3_directory_name() {
emulate -L zsh
setopt extendedglob
local -a match mbegin mend
if [[ $1 = d ]]; then
# turn the directory into a name
if [[ $2 = (#b)(/google/src/cloud/${USER}/)([^/]##)(*) ]]; then
# default case is one of my own workspaces
local my_dir
my_dir=$match[3]
if [[ "$GOOGLE3_PLUGIN_DISABLE_JCG" != "true" ]]; then
if shrink_javacomgoogle $my_dir; then
my_dir=$expansion
fi
fi
typeset -ga reply
reply=($match[2]:$my_dir $(( ${#match[1]} + ${#match[2]} + ${#match[3]} )) )
return 0
elif [[ $2 = (#b)(/google/src/cloud/)([a-z]##)/([^/]##)(*) ]]; then
# special case for other users' workspaces
# note that setting up completion of other users' workspaces would be
# prohibitive, and if I can't tab-complete someone else's workspace I
# don't think this code should expand them at all; so it's left out of the
# 'n' case
local my_dir
my_dir=$match[4]
if shrink_javacomgoogle $my_dir; then
my_dir=$expansion
fi
typeset -ga reply
reply=($match[2]:$match[3]:$my_dir $(( 1 + ${#match[1]} + ${#match[2]} + ${#match[3]} + ${#match[4]} )) )
return 0
else
return 1
fi
elif [[ $1 = n ]]; then
# turn the name into a directory
local dir
for dir in `/bin/ls /google/src/cloud/${USER}/`; do
if [[ $2 = ${dir}:(#b)(*) ]]; then
local my_dir
my_dir=$match[1]
if [[ "$GOOGLE3_PLUGIN_DISABLE_JCG" != "true" ]]; then
if expand_jcg $my_dir; then
my_dir=$expansion
fi
fi
typeset -ga reply
reply=(/google/src/cloud/${USER}/${dir}$my_dir)
return 0
fi
done
return 1
elif [[ $1 = c ]]; then
# complete names
local expl
local -a dirs
dirs=(/google/src/cloud/${USER}/*(/:t))
dirs=(${^dirs}:)
_wanted dynamic-dirs expl 'dynamic directory' compadd -S\] -a dirs
return
else
return 1
fi
return 0
}