Skip to content

Commit

Permalink
[completion] A harness to test _init_completion from bash-completion.
Browse files Browse the repository at this point in the history
This is the API that completion plugins use.  It fills in 4 or 5
variables, and then the plugins set COMPREPLY.

_init_completion has a nasty shell implementation, which we can replace
in OSH with simple Python code.
  • Loading branch information
Andy Chu committed Dec 21, 2018
1 parent cee90bd commit 725dfbf
Show file tree
Hide file tree
Showing 5 changed files with 2,233 additions and 1 deletion.
61 changes: 61 additions & 0 deletions gold/init_completion.sh
@@ -0,0 +1,61 @@
#!/bin/bash
#
# Test our _init_completion builtin against the bash_completion implementaiton.
#
# Usage:
# ./init_completion.sh <function name>

tab-complete() {
local code=$1
local sh=bash
#local sh=bin/osh

local pat='^FLAGS|^COMP_WORDS|^WORDS|^VAR'
{ cat gold/init_completion_lib.sh; echo "$code"; } |
$sh --rcfile /dev/null -i 2>&1 |
egrep "$pat" || echo "ERROR: output didn't match $pat"
}

# _init_completion flags used:
# -s
# -n :
#
# Do NOT need to implement:
#
# -o '@(diff|patch)' is used once. But it's for redirect args, which we parse
# in OSH itself.
#
# NOTE: I see _init_completion -s -n : , but I believe that's identical to
# _init_completion -s.
# Also I see '-n =+!' once, but that may be a mistake. The most common cases
# are : and =.

test-init() {
tab-complete $'echo foo:bar --color=auto\t'

# readline includes quotes, and _init_completion doesn't do anything about this.
# I think that is a mistake and I will get rid of it?
#
# ls "--ver<TAB> or '--ver<TAB>' does NOT complete.
# But echo 'fro<TAB> DOES! So that is a mistake.

tab-complete $'echo "foo:bar|" --color=auto\t'

# scrape tab completion
echo
tab-complete $'noflags foo:bar --color=auto\t'
tab-complete $'noflags "foo:bar|" --color=auto\t'
tab-complete $'noflags "foo:bar|\t'

echo
tab-complete $'s foo:bar --color=auto\t'
tab-complete $'s foo:bar --color auto\t'

echo
tab-complete $'n foo:bar --color=auto\t'

echo
tab-complete $'n2 foo:bar --color=auto\t'
}

"$@"
50 changes: 50 additions & 0 deletions gold/init_completion_lib.sh
@@ -0,0 +1,50 @@
# Common functions fed to 'sh -i' by init_completion.sh.

# OSH should have this built in
if ! type -t _init_completion; then
source testdata/completion/bash_completion
fi

argv() {
python -c 'import sys; print(sys.argv[1:])' "$@"
}
comp_echo() {
echo
echo -n 'COMP_WORDS '; argv "${COMP_WORDS[@]}"
}
complete -F comp_echo echo

showvars() {
echo -n 'WORDS '; argv "${words[@]}"
echo -n 'VAR '; argv cur "$cur"
echo -n 'VAR '; argv prev "$prev"
echo -n 'VAR '; argv cword "$cword"
echo -n 'VAR '; argv split "$split"
}

_comp_init_completion() {
local cur prev words cword split

echo
echo FLAGS "$@"
_init_completion "$@"

echo
showvars
}

noflags() { echo; }
comp_noflags() { _comp_init_completion; }
complete -F comp_noflags noflags

s() { echo; }
comp_s() { _comp_init_completion -s ; }
complete -F comp_s s

n() { echo; }
comp_n() { _comp_init_completion -n = ; }
complete -F comp_n n

n2() { echo; }
comp_n2() { _comp_init_completion -n := ; }
complete -F comp_n2 n2
4 changes: 3 additions & 1 deletion misc/complete.sh
Expand Up @@ -58,7 +58,7 @@ audit-git() {

# EVERY plugin seems ot use the _init_completion function. We can do this
# ourselves!
audit-plugins() {
audit-plugin-init() {
ls ../bash-completion/completions/* | wc -l
#grep -c _init_completion ../bash-completion/completions/*

Expand All @@ -75,7 +75,9 @@ audit-plugins() {
grep --no-filename _init_completion \
$BASH_COMP ../bash-completion/completions/* |
sort | uniq -c | sort -n
}

audit-plugin-x() {
echo
echo '-X usage'
echo
Expand Down

0 comments on commit 725dfbf

Please sign in to comment.