Skip to content

Commit 7fc3c9d

Browse files
committed
Improve shell suggestions.
1 parent 7d3c710 commit 7fc3c9d

3 files changed

Lines changed: 66 additions & 8 deletions

File tree

crates/rb-cli/src/commands/shell_integration.rs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,35 @@ _rb_completion() {{
7070
completions=$(rb __bash_complete "${{COMP_LINE}}" "${{COMP_POINT}}" 2>/dev/null)
7171
7272
if [ -n "$completions" ]; then
73-
# Only use nospace when actively navigating through a directory path
74-
# (i.e., when current word ends with /)
75-
if [[ "$cur" =~ /$ ]]; then
73+
# Check if all completions are directories (end with /)
74+
# If so, don't add space to allow continued navigation
75+
local all_dirs=true
76+
while IFS= read -r line; do
77+
if [[ ! "$line" =~ /$ ]]; then
78+
all_dirs=false
79+
break
80+
fi
81+
done <<< "$completions"
82+
83+
if [ "$all_dirs" = true ]; then
7684
compopt -o nospace
7785
fi
7886
7987
COMPREPLY=($(compgen -W "$completions" -- "$cur"))
8088
else
81-
# No rb completions, fall back to default bash completion (files/dirs)
82-
compopt -o default
83-
COMPREPLY=()
89+
# No rb completions - check if we're completing a directory-only flag
90+
# For -C, -R, -G: don't fall back to default (which would show files)
91+
case "$prev" in
92+
-C|--work-dir|-R|--rubies-dir|-G|--gem-home)
93+
# Return empty completions (no files for directory flags)
94+
COMPREPLY=()
95+
;;
96+
*)
97+
# Fall back to default bash completion for other cases
98+
compopt -o default
99+
COMPREPLY=()
100+
;;
101+
esac
84102
fi
85103
}}
86104
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
# ShellSpec tests for bash completion nospace integration
3+
# Tests that the bash completion function logic is correct
4+
5+
Describe "Ruby Butler Bash Completion Nospace Integration"
6+
Include spec/support/helpers.sh
7+
8+
Describe "compopt nospace behavior with directory completions"
9+
It "single directory completion does not add space (allows subdirectory navigation)"
10+
When run rb __bash_complete "rb -C sp" 9
11+
The status should equal 0
12+
The output should equal "spec/"
13+
The lines of output should equal 1
14+
# Even with count=1, if it's a directory, nospace is applied
15+
End
16+
17+
It "multiple directory completions do not add space"
18+
When run rb __bash_complete "rb -C spec/" 13
19+
The status should equal 0
20+
The line 1 of output should end with "/"
21+
The line 2 of output should end with "/"
22+
The line 3 of output should end with "/"
23+
# Multiple directories, nospace is applied
24+
End
25+
End
26+
27+
Describe "completion script logic validation"
28+
It "generated script contains all_dirs flag"
29+
When run rb shell-integration bash
30+
The output should include "local all_dirs=true"
31+
End
32+
33+
It "generated script applies nospace when all completions are directories"
34+
When run rb shell-integration bash
35+
# shellcheck disable=SC2016
36+
The output should include 'if [ "$all_dirs" = true ]; then'
37+
The output should include "compopt -o nospace"
38+
End
39+
End
40+
End

spec/behaviour/bash_completion_spec.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,11 @@ EOF
312312
The output should include "COMPREPLY=(\$(compgen -W \"\$completions\" -- \"\$cur\"))"
313313
End
314314

315-
It "includes fallback to default bash completion"
315+
It "includes fallback to default bash completion for non-directory flags"
316316
When run rb shell-integration bash
317317
The status should equal 0
318318
The output should include "compopt -o default"
319-
The output should include "# No rb completions, fall back to default bash completion"
319+
The output should include "# No rb completions - check if we're completing a directory-only flag"
320320
End
321321
End
322322
End

0 commit comments

Comments
 (0)