Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[completion] A harness to test _init_completion from bash-completion.
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
Showing
5 changed files
with
2,233 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
} | ||
|
||
"$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.