Skip to content

Commit 95ea525

Browse files
committed
simplify dynamic completion check
1 parent 4455eb9 commit 95ea525

15 files changed

Lines changed: 201 additions & 304 deletions

lib/completely/pattern.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
module Completely
22
class Pattern
3-
DYNAMIC_WORD_PREFIX = '__completely_dynamic__'
4-
53
attr_reader :text, :completions, :function_name
64

75
def initialize(text, completions, function_name)
@@ -65,9 +63,7 @@ def serialized_words
6563
end
6664

6765
def serialize_word(word)
68-
if dynamic_word?(word)
69-
return %("#{DYNAMIC_WORD_PREFIX}#{escape_for_double_quotes word}")
70-
end
66+
return word if dynamic_word?(word)
7167

7268
%("#{escape_for_double_quotes word}")
7369
end

lib/completely/templates/template.erb

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111
local want_options=0
12-
local dynamic_prefix="<%= Completely::Pattern::DYNAMIC_WORD_PREFIX %>"
1312

1413
# words the user already typed (excluding the command itself)
1514
local used=()
@@ -21,29 +20,19 @@
2120
# Completing a non-option: drop options and already-used words.
2221
[[ "${cur:0:1}" == "-" ]] && want_options=1
2322
for word in "${words[@]}"; do
24-
local candidates=("$word")
25-
if [[ "$word" == "$dynamic_prefix"* ]]; then
26-
word="${word#"$dynamic_prefix"}"
27-
word="${word//$'\r'/ }"
28-
word="${word//$'\n'/ }"
29-
read -r -a candidates <<<"$word"
30-
fi
31-
32-
for candidate in "${candidates[@]}"; do
33-
if ((!want_options)); then
34-
[[ "${candidate:0:1}" == "-" ]] && continue
23+
if ((!want_options)); then
24+
[[ "${word:0:1}" == "-" ]] && continue
3525

36-
for u in "${used[@]}"; do
37-
if [[ "$u" == "$candidate" ]]; then
38-
continue 2
39-
fi
40-
done
41-
fi
26+
for u in "${used[@]}"; do
27+
if [[ "$u" == "$word" ]]; then
28+
continue 2
29+
fi
30+
done
31+
fi
4232

43-
# compgen -W expects shell-escaped words in one space-delimited string.
44-
printf -v candidate '%q' "$candidate"
45-
result+=("$candidate")
46-
done
33+
# compgen -W expects shell-escaped words in one space-delimited string.
34+
printf -v word '%q' "$word"
35+
result+=("$word")
4736
done
4837

4938
echo "${result[*]}"
@@ -70,6 +59,7 @@
7059
% patterns.each do |pattern|
7160
% next if pattern.empty?
7261
<%= pattern.case_string %>)
62+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
7363
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen <%= pattern.compgen %> -- "$cur")
7464
;;
7565

lib/completely/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Completely
2-
VERSION = '0.7.4'
2+
VERSION = '0.8.0.rc1'
33
end

spec/approvals/cli/generated-script

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ _mygit_completions_filter() {
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111
local want_options=0
12-
local dynamic_prefix="__completely_dynamic__"
1312

1413
# words the user already typed (excluding the command itself)
1514
local used=()
@@ -21,29 +20,19 @@ _mygit_completions_filter() {
2120
# Completing a non-option: drop options and already-used words.
2221
[[ "${cur:0:1}" == "-" ]] && want_options=1
2322
for word in "${words[@]}"; do
24-
local candidates=("$word")
25-
if [[ "$word" == "$dynamic_prefix"* ]]; then
26-
word="${word#"$dynamic_prefix"}"
27-
word="${word//$'\r'/ }"
28-
word="${word//$'\n'/ }"
29-
read -r -a candidates <<<"$word"
23+
if ((!want_options)); then
24+
[[ "${word:0:1}" == "-" ]] && continue
25+
26+
for u in "${used[@]}"; do
27+
if [[ "$u" == "$word" ]]; then
28+
continue 2
29+
fi
30+
done
3031
fi
3132

32-
for candidate in "${candidates[@]}"; do
33-
if ((!want_options)); then
34-
[[ "${candidate:0:1}" == "-" ]] && continue
35-
36-
for u in "${used[@]}"; do
37-
if [[ "$u" == "$candidate" ]]; then
38-
continue 2
39-
fi
40-
done
41-
fi
42-
43-
# compgen -W expects shell-escaped words in one space-delimited string.
44-
printf -v candidate '%q' "$candidate"
45-
result+=("$candidate")
46-
done
33+
# compgen -W expects shell-escaped words in one space-delimited string.
34+
printf -v word '%q' "$word"
35+
result+=("$word")
4736
done
4837

4938
echo "${result[*]}"
@@ -61,22 +50,27 @@ _mygit_completions() {
6150

6251
case "$compline" in
6352
'status'*'--branch')
64-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
53+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
54+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
6555
;;
6656

6757
'status'*'-b')
68-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
58+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
59+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
6960
;;
7061

7162
'status'*)
63+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
7264
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help" "--verbose" "--branch" "-b")" -- "$cur")
7365
;;
7466

7567
'init'*)
68+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
7669
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")
7770
;;
7871

7972
*)
73+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
8074
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h" "-v" "--help" "--version" "init" "status")" -- "$cur")
8175
;;
8276

spec/approvals/cli/generated-script-alt

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ _mycomps_filter() {
99
local cur=${COMP_WORDS[COMP_CWORD]}
1010
local result=()
1111
local want_options=0
12-
local dynamic_prefix="__completely_dynamic__"
1312

1413
# words the user already typed (excluding the command itself)
1514
local used=()
@@ -21,29 +20,19 @@ _mycomps_filter() {
2120
# Completing a non-option: drop options and already-used words.
2221
[[ "${cur:0:1}" == "-" ]] && want_options=1
2322
for word in "${words[@]}"; do
24-
local candidates=("$word")
25-
if [[ "$word" == "$dynamic_prefix"* ]]; then
26-
word="${word#"$dynamic_prefix"}"
27-
word="${word//$'\r'/ }"
28-
word="${word//$'\n'/ }"
29-
read -r -a candidates <<<"$word"
23+
if ((!want_options)); then
24+
[[ "${word:0:1}" == "-" ]] && continue
25+
26+
for u in "${used[@]}"; do
27+
if [[ "$u" == "$word" ]]; then
28+
continue 2
29+
fi
30+
done
3031
fi
3132

32-
for candidate in "${candidates[@]}"; do
33-
if ((!want_options)); then
34-
[[ "${candidate:0:1}" == "-" ]] && continue
35-
36-
for u in "${used[@]}"; do
37-
if [[ "$u" == "$candidate" ]]; then
38-
continue 2
39-
fi
40-
done
41-
fi
42-
43-
# compgen -W expects shell-escaped words in one space-delimited string.
44-
printf -v candidate '%q' "$candidate"
45-
result+=("$candidate")
46-
done
33+
# compgen -W expects shell-escaped words in one space-delimited string.
34+
printf -v word '%q' "$word"
35+
result+=("$word")
4736
done
4837

4938
echo "${result[*]}"
@@ -61,22 +50,27 @@ _mycomps() {
6150

6251
case "$compline" in
6352
'status'*'--branch')
64-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
53+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
54+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
6555
;;
6656

6757
'status'*'-b')
68-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
58+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
59+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
6960
;;
7061

7162
'status'*)
63+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
7264
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "--help" "--verbose" "--branch" "-b")" -- "$cur")
7365
;;
7466

7567
'init'*)
68+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
7669
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mycomps_filter "--bare")" -- "$cur")
7770
;;
7871

7972
*)
73+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
8074
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mycomps_filter "-h" "-v" "--help" "--version" "init" "status")" -- "$cur")
8175
;;
8276

spec/approvals/cli/generated-wrapped-script

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ give_comps() {
1010
echo $' local cur=${COMP_WORDS[COMP_CWORD]}'
1111
echo $' local result=()'
1212
echo $' local want_options=0'
13-
echo $' local dynamic_prefix="__completely_dynamic__"'
1413
echo $''
1514
echo $' # words the user already typed (excluding the command itself)'
1615
echo $' local used=()'
@@ -22,29 +21,19 @@ give_comps() {
2221
echo $' # Completing a non-option: drop options and already-used words.'
2322
echo $' [[ "${cur:0:1}" == "-" ]] && want_options=1'
2423
echo $' for word in "${words[@]}"; do'
25-
echo $' local candidates=("$word")'
26-
echo $' if [[ "$word" == "$dynamic_prefix"* ]]; then'
27-
echo $' word="${word#"$dynamic_prefix"}"'
28-
echo $' word="${word//$\'\r\'/ }"'
29-
echo $' word="${word//$\'\n\'/ }"'
30-
echo $' read -r -a candidates <<<"$word"'
24+
echo $' if ((!want_options)); then'
25+
echo $' [[ "${word:0:1}" == "-" ]] && continue'
26+
echo $''
27+
echo $' for u in "${used[@]}"; do'
28+
echo $' if [[ "$u" == "$word" ]]; then'
29+
echo $' continue 2'
30+
echo $' fi'
31+
echo $' done'
3132
echo $' fi'
3233
echo $''
33-
echo $' for candidate in "${candidates[@]}"; do'
34-
echo $' if ((!want_options)); then'
35-
echo $' [[ "${candidate:0:1}" == "-" ]] && continue'
36-
echo $''
37-
echo $' for u in "${used[@]}"; do'
38-
echo $' if [[ "$u" == "$candidate" ]]; then'
39-
echo $' continue 2'
40-
echo $' fi'
41-
echo $' done'
42-
echo $' fi'
43-
echo $''
44-
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
45-
echo $' printf -v candidate \'%q\' "$candidate"'
46-
echo $' result+=("$candidate")'
47-
echo $' done'
34+
echo $' # compgen -W expects shell-escaped words in one space-delimited string.'
35+
echo $' printf -v word \'%q\' "$word"'
36+
echo $' result+=("$word")'
4837
echo $' done'
4938
echo $''
5039
echo $' echo "${result[*]}"'
@@ -62,22 +51,27 @@ give_comps() {
6251
echo $''
6352
echo $' case "$compline" in'
6453
echo $' \'status\'*\'--branch\')'
65-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'
54+
echo $' # shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions'
55+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format=\'%(refname:short)\' 2>/dev/null))" -- "$cur")'
6656
echo $' ;;'
6757
echo $''
6858
echo $' \'status\'*\'-b\')'
69-
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format=\'%(refname:short)\' 2>/dev/null)")" -- "$cur")'
59+
echo $' # shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions'
60+
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format=\'%(refname:short)\' 2>/dev/null))" -- "$cur")'
7061
echo $' ;;'
7162
echo $''
7263
echo $' \'status\'*)'
64+
echo $' # shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions'
7365
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help" "--verbose" "--branch" "-b")" -- "$cur")'
7466
echo $' ;;'
7567
echo $''
7668
echo $' \'init\'*)'
69+
echo $' # shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions'
7770
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")'
7871
echo $' ;;'
7972
echo $''
8073
echo $' *)'
74+
echo $' # shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions'
8175
echo $' while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h" "-v" "--help" "--version" "init" "status")" -- "$cur")'
8276
echo $' ;;'
8377
echo $''

spec/approvals/cli/test/completely-tester-1.sh

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ _mygit_completions_filter() {
1717
local cur=${COMP_WORDS[COMP_CWORD]}
1818
local result=()
1919
local want_options=0
20-
local dynamic_prefix="__completely_dynamic__"
2120

2221
# words the user already typed (excluding the command itself)
2322
local used=()
@@ -29,29 +28,19 @@ _mygit_completions_filter() {
2928
# Completing a non-option: drop options and already-used words.
3029
[[ "${cur:0:1}" == "-" ]] && want_options=1
3130
for word in "${words[@]}"; do
32-
local candidates=("$word")
33-
if [[ "$word" == "$dynamic_prefix"* ]]; then
34-
word="${word#"$dynamic_prefix"}"
35-
word="${word//$'\r'/ }"
36-
word="${word//$'\n'/ }"
37-
read -r -a candidates <<<"$word"
31+
if ((!want_options)); then
32+
[[ "${word:0:1}" == "-" ]] && continue
33+
34+
for u in "${used[@]}"; do
35+
if [[ "$u" == "$word" ]]; then
36+
continue 2
37+
fi
38+
done
3839
fi
3940

40-
for candidate in "${candidates[@]}"; do
41-
if ((!want_options)); then
42-
[[ "${candidate:0:1}" == "-" ]] && continue
43-
44-
for u in "${used[@]}"; do
45-
if [[ "$u" == "$candidate" ]]; then
46-
continue 2
47-
fi
48-
done
49-
fi
50-
51-
# compgen -W expects shell-escaped words in one space-delimited string.
52-
printf -v candidate '%q' "$candidate"
53-
result+=("$candidate")
54-
done
41+
# compgen -W expects shell-escaped words in one space-delimited string.
42+
printf -v word '%q' "$word"
43+
result+=("$word")
5544
done
5645

5746
echo "${result[*]}"
@@ -69,22 +58,27 @@ _mygit_completions() {
6958

7059
case "$compline" in
7160
'status'*'--branch')
72-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
61+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
62+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
7363
;;
7464

7565
'status'*'-b')
76-
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "__completely_dynamic__$(git branch --format='%(refname:short)' 2>/dev/null)")" -- "$cur")
66+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
67+
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter $(git branch --format='%(refname:short)' 2>/dev/null))" -- "$cur")
7768
;;
7869

7970
'status'*)
71+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
8072
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "--help" "--verbose" "--branch" "-b")" -- "$cur")
8173
;;
8274

8375
'init'*)
76+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
8477
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -A directory -W "$(_mygit_completions_filter "--bare")" -- "$cur")
8578
;;
8679

8780
*)
81+
# shellcheck disable=SC2046 # intentional splitting for dynamic $(...) completions
8882
while read -r; do COMPREPLY+=("$REPLY"); done < <(compgen -W "$(_mygit_completions_filter "-h" "-v" "--help" "--version" "init" "status")" -- "$cur")
8983
;;
9084

0 commit comments

Comments
 (0)