Skip to content

Commit 8d25207

Browse files
committed
- Add support for middle wildcard for --flag args completions
1 parent e2791a6 commit 8d25207

File tree

9 files changed

+67
-28
lines changed

9 files changed

+67
-28
lines changed

lib/completely/pattern.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,18 @@ def prefix
3030
text.split(' ')[0]
3131
end
3232

33+
def case_string
34+
if text_without_prefix.empty?
35+
"*"
36+
elsif text_without_prefix.include? "*"
37+
%Q['#{text_without_prefix.gsub "*", "'*'"}']
38+
else
39+
%Q['#{text_without_prefix}'*]
40+
end
41+
end
42+
3343
def text_without_prefix
34-
text.split(' ')[1..-1].join ' '
44+
@text_without_prefix ||= text.split(' ')[1..-1].join ' '
3545
end
3646

3747
def compgen

lib/completely/template.erb

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,22 @@
55
# Modifying it manually is not recommended
66

77
<%= function_name %>() {
8-
local cur comp_line
8+
local cur compline
99
_init_completion -s || return
1010
cur=${COMP_WORDS[COMP_CWORD]}
11-
comp_line="${COMP_WORDS[@]:1}"
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1212

13-
case "$comp_line" in
13+
% if ENV['COMPLETELY_DEBUG']
14+
if [[ -n "$COMPLETELY_DEBUG" ]]; then
15+
echo "compline: '$compline'" > 'completely-debug.txt'
16+
echo "cur: '$cur'" >> 'completely-debug.txt'
17+
fi
18+
19+
%end
20+
case "$compline" in
1421
% patterns.each do |pattern|
1522
% next if pattern.empty?
16-
'<%= pattern.text_without_prefix %>'*) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;;
23+
<%= pattern.case_string %>) COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) ;;
1724
% end
1825
esac
1926
} &&

spec/approvals/cli/generated-script

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
# Modifying it manually is not recommended
66

77
_mygit_completions() {
8-
local cur comp_line
8+
local cur compline
99
_init_completion -s || return
1010
cur=${COMP_WORDS[COMP_CWORD]}
11-
comp_line="${COMP_WORDS[@]:1}"
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1212

13-
case "$comp_line" in
13+
case "$compline" in
1414
'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;
1515
'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;
1616
'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;
17-
''*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
17+
*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
1818
esac
1919
} &&
2020
complete -F _mygit_completions mygit

spec/approvals/cli/generated-script-alt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
# Modifying it manually is not recommended
66

77
_mycomps() {
8-
local cur comp_line
8+
local cur compline
99
_init_completion -s || return
1010
cur=${COMP_WORDS[COMP_CWORD]}
11-
comp_line="${COMP_WORDS[@]:1}"
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1212

13-
case "$comp_line" in
13+
case "$compline" in
1414
'status'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;
1515
'commit'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;
1616
'init'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;
17-
''*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
17+
*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;
1818
esac
1919
} &&
2020
complete -F _mycomps mygit

spec/approvals/cli/generated-wrapped-script

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ give_comps() {
66
echo $'# Modifying it manually is not recommended'
77
echo $''
88
echo $'_mygit_completions() {'
9-
echo $' local cur comp_line'
9+
echo $' local cur compline'
1010
echo $' _init_completion -s || return'
1111
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
12-
echo $' comp_line="${COMP_WORDS[@]:1}"'
12+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
1313
echo $''
14-
echo $' case "$comp_line" in'
14+
echo $' case "$compline" in'
1515
echo $' \'status\'*) COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) ;;'
1616
echo $' \'commit\'*) COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) ;;'
1717
echo $' \'init\'*) COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) ;;'
18-
echo $' \'\'*) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;'
18+
echo $' *) COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) ;;'
1919
echo $' esac'
2020
echo $'} &&'
2121
echo $'complete -F _mygit_completions mygit'

spec/approvals/completions/function

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ send_completions() {
66
echo $'# Modifying it manually is not recommended'
77
echo $''
88
echo $'_completely_completions() {'
9-
echo $' local cur comp_line'
9+
echo $' local cur compline'
1010
echo $' _init_completion -s || return'
1111
echo $' cur=${COMP_WORDS[COMP_CWORD]}'
12-
echo $' comp_line="${COMP_WORDS[@]:1}"'
12+
echo $' compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"'
1313
echo $''
14-
echo $' case "$comp_line" in'
14+
echo $' case "$compline" in'
1515
echo $' \'generate\'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;'
1616
echo $' \'init\'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;'
17-
echo $' \'\'*) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;'
17+
echo $' *) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;'
1818
echo $' esac'
1919
echo $'} &&'
2020
echo $'complete -F _completely_completions completely'

spec/approvals/completions/script

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
# Modifying it manually is not recommended
66

77
_completely_completions() {
8-
local cur comp_line
8+
local cur compline
99
_init_completion -s || return
1010
cur=${COMP_WORDS[COMP_CWORD]}
11-
comp_line="${COMP_WORDS[@]:1}"
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1212

13-
case "$comp_line" in
13+
case "$compline" in
1414
'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;
1515
'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;
16-
''*) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;
16+
*) COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) ;;
1717
esac
1818
} &&
1919
complete -F _completely_completions completely

spec/approvals/completions/script-only-spaces

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
# Modifying it manually is not recommended
66

77
_completely_completions() {
8-
local cur comp_line
8+
local cur compline
99
_init_completion -s || return
1010
cur=${COMP_WORDS[COMP_CWORD]}
11-
comp_line="${COMP_WORDS[@]:1}"
11+
compline="${COMP_WORDS[@]:1:$COMP_CWORD-1}"
1212

13-
case "$comp_line" in
13+
case "$compline" in
1414
'generate'*) COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) ;;
1515
'init'*) COMPREPLY=($(compgen -W "--help" -- "$cur")) ;;
1616
esac

spec/completely/pattern_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,28 @@
4343
end
4444
end
4545

46+
describe '#case_string', :focus do
47+
it "returns the quoted pattern (excluding command name) with a wildcard suffix" do
48+
expect(subject.case_string).to eq "'commit'*"
49+
end
50+
51+
context "when the pattern (excluding command name) is empty" do
52+
let(:text) { "git" }
53+
54+
it "returns '*'" do
55+
expect(subject.case_string).to eq "*"
56+
end
57+
end
58+
59+
context "when the pattern includes a wildcard" do
60+
let(:text) { "git checkout*--branch" }
61+
62+
it "returns the quoted pattern (excluding command name) with an unquoted wildcard and without a wildcard suffix" do
63+
expect(subject.case_string).to eq "'checkout'*'--branch'"
64+
end
65+
end
66+
end
67+
4668
describe '#text_without_prefix' do
4769
it "returns all but the first word from text" do
4870
expect(subject.text_without_prefix).to eq "commit"

0 commit comments

Comments
 (0)