Skip to content

Commit bc7fd66

Browse files
author
Oliver Weiler
authored
Unify Bash's and zsh's programmable completion (#1002)
* Unify Bash's and zsh's programmable completion * Perform autocompletion for short options * Use SDKMAN_CANDIDATES for resolving candidates * Handle `read` edge case
1 parent 4bf5b70 commit bc7fd66

File tree

4 files changed

+21
-87
lines changed

4 files changed

+21
-87
lines changed

contrib/completion/bash/sdk

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#!/usr/bin/bash
22

33
_sdk() {
4-
local -r previous_word="${COMP_WORDS[COMP_CWORD - 1]}"
5-
local -r current_word="${COMP_WORDS[COMP_CWORD]}"
4+
local -r previous_word=${COMP_WORDS[COMP_CWORD - 1]}
5+
local -r current_word=${COMP_WORDS[COMP_CWORD]}
66

77
if ((COMP_CWORD == 3)); then
8-
local -r before_previous_word="${COMP_WORDS[COMP_CWORD - 2]}"
8+
local -r before_previous_word=${COMP_WORDS[COMP_CWORD - 2]}
99

1010
__sdkman_complete_candidate_version "$before_previous_word" "$previous_word" "$current_word"
1111

@@ -25,19 +25,18 @@ __sdkman_complete_command() {
2525
sdk)
2626
candidates=("install" "uninstall" "list" "use" "config" "default" "home" "env" "current" "upgrade" "version" "broadcast" "help" "offline" "selfupdate" "update" "flush")
2727
;;
28-
current|default|home|uninstall|upgrade|use)
28+
current|c|default|d|home|h|uninstall|rm|upgrade|ug|use|u)
2929
local -r candidate_paths=("${SDKMAN_CANDIDATES_DIR}"/*)
3030

3131
for candidate_path in "${candidate_paths[@]}"; do
32-
candidates+=(${candidate_path##*/})
32+
candidates+=("${candidate_path##*/}")
3333
done
3434
;;
35-
install|list)
36-
local -r all_candidates=$(curl --silent "${SDKMAN_CANDIDATES_API}/candidates/all")
37-
IFS=',' read -r -a candidates <<< "$all_candidates"
35+
install|i|list|ls)
36+
candidates=${SDKMAN_CANDIDATES[@]}
3837
;;
39-
env)
40-
candidates=("init install clear")
38+
env|e)
39+
candidates=("init" "install" "clear")
4140
;;
4241
offline)
4342
candidates=("enable" "disable")
@@ -61,18 +60,19 @@ __sdkman_complete_candidate_version() {
6160
local -a candidates
6261

6362
case $command in
64-
use|default|home|uninstall)
63+
default|d|home|h|uninstall|rm|use|u)
6564
local -r version_paths=("${SDKMAN_CANDIDATES_DIR}/${candidate}"/*)
6665

6766
for version_path in "${version_paths[@]}"; do
6867
[[ $version_path = *current ]] && continue
6968

70-
candidates+=(${version_path##*/})
69+
candidates+=("${version_path##*/}")
7170
done
7271
;;
73-
install)
74-
local -r all_candidate_versions=$(curl --silent "${SDKMAN_CANDIDATES_API}/candidates/$candidate/${SDKMAN_PLATFORM}/versions/all")
75-
IFS=',' read -r -a candidates <<< "$all_candidate_versions"
72+
install|i)
73+
while IFS= read -r -d, version || [[ -n "$version" ]]; do
74+
candidates+=("$version")
75+
done <<< "$(curl --silent "${SDKMAN_CANDIDATES_API}/candidates/$candidate/${SDKMAN_PLATFORM}/versions/all")"
7676
;;
7777
esac
7878

contrib/completion/zsh/sdk

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/main/bash/sdkman-init.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,9 @@ if [[ "$sdkman_auto_complete" == 'true' ]]; then
190190
compinit
191191
fi
192192
fi
193-
source "${SDKMAN_DIR}/contrib/completion/zsh/sdk"
193+
autoload -U bashcompinit
194+
bashcompinit
195+
source "${SDKMAN_DIR}/contrib/completion/bash/sdk"
194196
__sdkman_echo_debug "ZSH completion script loaded..."
195197
elif [[ "$bash_shell" == 'true' ]]; then
196198
source "${SDKMAN_DIR}/contrib/completion/bash/sdk"

src/test/groovy/sdkman/specs/CompletionSpec.groovy

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ import sdkman.support.SdkmanEnvSpecification
44

55
class CompletionSpec extends SdkmanEnvSpecification {
66
static final String CANDIDATES_API = "http://localhost:8080/2"
7-
87
static final String BROADCAST_API_LATEST_ID_ENDPOINT = "$CANDIDATES_API/broadcast/latest/id"
9-
static final String CANDIDATES_ALL_ENDPOINT = "$CANDIDATES_API/candidates/all"
108

119
def "should complete the list of commands"() {
1210
given:
1311
bash = sdkmanBashEnvBuilder
14-
.withVersionCache("x.y.z")
1512
.withConfiguration("sdkman_auto_complete", "true")
1613
.build()
1714

@@ -28,11 +25,8 @@ class CompletionSpec extends SdkmanEnvSpecification {
2825

2926
def "should complete the list of candidates"() {
3027
given:
31-
curlStub.primeWith(BROADCAST_API_LATEST_ID_ENDPOINT, "echo dbfb025be9f97fda2052b5febcca0155")
32-
.primeWith(CANDIDATES_ALL_ENDPOINT, "echo java,groovy")
33-
3428
bash = sdkmanBashEnvBuilder
35-
.withVersionCache("x.y.z")
29+
.withCandidates(["java", "groovy"])
3630
.withConfiguration("sdkman_auto_complete", "true")
3731
.build()
3832

@@ -41,7 +35,7 @@ class CompletionSpec extends SdkmanEnvSpecification {
4135

4236
when:
4337
bash.execute("COMP_CWORD=2; COMP_WORDS=(sdk install); _sdk")
44-
bash.execute("echo \${COMPREPLY[@]}")
38+
bash.execute('echo "\${COMPREPLY[@]}"')
4539

4640
then:
4741
bash.output.contains("java groovy")
@@ -55,7 +49,6 @@ class CompletionSpec extends SdkmanEnvSpecification {
5549
unameStub.forKernel("Darwin").forMachine("x86_64")
5650

5751
bash = sdkmanBashEnvBuilder
58-
.withVersionCache("x.y.z")
5952
.withConfiguration("sdkman_auto_complete", "true")
6053
.withUnameStub(unameStub)
6154
.build()
@@ -65,7 +58,7 @@ class CompletionSpec extends SdkmanEnvSpecification {
6558

6659
when:
6760
bash.execute("COMP_CWORD=3; COMP_WORDS=(sdk install java); _sdk")
68-
bash.execute("echo \${COMPREPLY[@]}")
61+
bash.execute('echo "\${COMPREPLY[@]}"')
6962

7063
then:
7164
bash.output.contains("16.0.1.hs-adpt 17.0.0-tem")

0 commit comments

Comments
 (0)