Skip to content

Commit 3a07189

Browse files
committed
Fixed bun completions
1 parent afcab0d commit 3a07189

File tree

3 files changed

+142
-9
lines changed

3 files changed

+142
-9
lines changed

bash_completions/bun.bash

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#/usr/bin/env bash
2+
3+
_bun_completions() {
4+
local LOG_FILE="/tmp/bun_completion.log"
5+
echo "================ NEW COMPLETION REQUEST ================" >> "$LOG_FILE"
6+
7+
local cur="${COMP_WORDS[COMP_CWORD]}"
8+
local prev="${COMP_WORDS[COMP_CWORD-1]}"
9+
10+
echo "COMP_WORDS: ${COMP_WORDS[@]}" >> "$LOG_FILE"
11+
echo "COMP_CWORD: $COMP_CWORD" >> "$LOG_FILE"
12+
echo "cur: $cur" >> "$LOG_FILE"
13+
echo "prev: $prev" >> "$LOG_FILE"
14+
echo "COMP_LINE: $COMP_LINE" >> "$LOG_FILE"
15+
16+
# Get scripts from package.json
17+
local script_names=""
18+
if [[ -f package.json ]]; then
19+
script_names=$(jq -r '.scripts | keys[]' package.json 2>/dev/null)
20+
echo "Found scripts: $script_names" >> "$LOG_FILE"
21+
fi
22+
23+
# Check if we're trying to complete a partial subcommand after a namespace
24+
if [[ "$COMP_LINE" =~ ([[:alnum:]_-]+):([[:alnum:]_-]*) ]]; then
25+
local namespace="${BASH_REMATCH[1]}"
26+
local partial_subcommand="${BASH_REMATCH[2]}"
27+
echo "Detected namespace:partial pattern, namespace=$namespace, partial=$partial_subcommand" >> "$LOG_FILE"
28+
29+
# Find matching scripts and extract their suffixes
30+
local matching_suffixes=""
31+
for script in $script_names; do
32+
if [[ "$script" == "$namespace:"* ]]; then
33+
local suffix="${script#$namespace:}"
34+
# Only include if it starts with the partial subcommand
35+
if [[ -z "$partial_subcommand" || "$suffix" == "$partial_subcommand"* ]]; then
36+
matching_suffixes+=" $suffix"
37+
echo "Found matching suffix: $suffix" >> "$LOG_FILE"
38+
fi
39+
fi
40+
done
41+
42+
echo "All matching suffixes: $matching_suffixes" >> "$LOG_FILE"
43+
44+
if [[ -n "$matching_suffixes" ]]; then
45+
# If there's just one match and it's exactly the partial subcommand, don't duplicate it
46+
if [[ "$matching_suffixes" == " $partial_subcommand" ]]; then
47+
echo "Exact match found, not duplicating" >> "$LOG_FILE"
48+
return 0
49+
fi
50+
51+
# Set completions directly without compgen
52+
COMPREPLY=()
53+
for suffix in $matching_suffixes; do
54+
COMPREPLY+=("$suffix")
55+
done
56+
echo "Set COMPREPLY directly to: ${COMPREPLY[*]}" >> "$LOG_FILE"
57+
return 0
58+
fi
59+
fi
60+
61+
# Special case for namespace completion with empty suffix
62+
# If we're completing right after a colon
63+
if [[ "$cur" == ":" ]] || [[ "$cur" == "" && "$COMP_LINE" == *:* && "$COMP_LINE" != *:*[[:alnum:]]* ]]; then
64+
echo "Completing for empty suffix after a colon" >> "$LOG_FILE"
65+
66+
# Extract the namespace from COMP_LINE
67+
local namespace=""
68+
if [[ "$COMP_LINE" =~ ([[:alnum:]_-]+): ]]; then
69+
namespace="${BASH_REMATCH[1]}"
70+
echo "Extracted namespace: $namespace" >> "$LOG_FILE"
71+
72+
# Find scripts that match this namespace and transform them to just the suffix
73+
local matching_scripts=""
74+
for script in $script_names; do
75+
if [[ "$script" == "$namespace:"* ]]; then
76+
# Extract just the part after namespace:
77+
local suffix="${script#$namespace:}"
78+
matching_scripts+=" $suffix"
79+
echo "Found match: $script, adding suffix: $suffix" >> "$LOG_FILE"
80+
fi
81+
done
82+
83+
echo "Matching suffixes: $matching_scripts" >> "$LOG_FILE"
84+
85+
if [[ -n "$matching_scripts" ]]; then
86+
# Directly set COMPREPLY to avoid any further processing
87+
COMPREPLY=()
88+
for match in $matching_scripts; do
89+
COMPREPLY+=("$match")
90+
done
91+
echo "Set COMPREPLY directly to: ${COMPREPLY[*]}" >> "$LOG_FILE"
92+
return 0
93+
fi
94+
fi
95+
fi
96+
97+
# Regular completion for bun command
98+
if [[ $COMP_CWORD -eq 1 ]]; then
99+
local commands="add build completions create dev discord help i init install link pm r remove repl run test unlink update upgrade x"
100+
101+
# Add script names from package.json
102+
if [[ -n "$script_names" ]]; then
103+
commands+=" $script_names"
104+
fi
105+
106+
COMPREPLY=($(compgen -W "$commands" -- "$cur"))
107+
echo "First level completion: ${COMPREPLY[*]}" >> "$LOG_FILE"
108+
return 0
109+
fi
110+
111+
# Handle "bun run" command
112+
if [[ $COMP_CWORD -eq 2 && "$prev" == "run" ]]; then
113+
if [[ -n "$script_names" ]]; then
114+
COMPREPLY=($(compgen -W "$script_names" -- "$cur"))
115+
echo "bun run completion: ${COMPREPLY[*]}" >> "$LOG_FILE"
116+
fi
117+
return 0
118+
fi
119+
120+
echo "No matches, returning" >> "$LOG_FILE"
121+
return 0
122+
}
123+
124+
complete -o bashdefault -o default -o nospace -F _bun_completions bun

bashrc/bash_completions.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ if ! shopt -oq posix; then
1212
elif [ -f /opt/homebrew/etc/bash_completion ]; then
1313
source /opt/homebrew/etc/bash_completion
1414
fi
15+
fi
16+
17+
# Bun completion
18+
if [ -f "$DOTFILES_PATH/bash_completions/bun.bash" ]; then
19+
source "$DOTFILES_PATH/bash_completions/bun.bash"
1520
fi

bashrc/path.sh

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
11
#!/bin/bash
2+
3+
4+
# Use binaries from node_modules in the current dir.
5+
# But don't prioritize these because there's an NPM package that
6+
# also installs a 'which' binary, and this is annoying.
7+
# But they do need to come before mise shims
8+
export PATH="${PATH}:./node_modules/.bin"
9+
210
# Mise
3-
export PATH="$HOME/.local/share/mise/shims:${PATH}"
11+
export PATH="${PATH}:$HOME/.local/share/mise/shims"
12+
413
# Homebrew
514
export PATH="/opt/homebrew/bin:${PATH}"
615

@@ -31,11 +40,6 @@ export PATH="${PATH}:${DOTFILES_PATH}/bin"
3140
# System Python
3241
export PATH="${PATH}:${HOME}/Library/Python/2.7/bin/"
3342

34-
# Use binaries from node_modules in the current dir.
35-
# But don't prioritize these because there's an NPM package that
36-
# also installs a 'which' binary, and this is annoying.
37-
export PATH="${PATH}:./node_modules/.bin"
38-
3943
export PATH="${PATH}:/Users/ndbroadbent/anaconda/bin"
4044
export PATH="${PATH}:/opt/homebrew/opt/imagemagick/bin"
4145
export PATH="${PATH}:/opt/homebrew/opt/qt@5.5/bin"
@@ -60,8 +64,8 @@ export PATH="/opt/homebrew/opt/php@7.4/bin:$PATH"
6064

6165
export PATH="${KREW_ROOT:-$HOME/.krew}/bin:$PATH"
6266

63-
# Use local ./bin directory for executables
64-
export PATH="./bin:$PATH"
65-
6667
# Terraform wrapper to speed up convox commands
6768
export PATH="$HOME/code/convox_racks_terraform/scripts/terraform_wrapper:${PATH}"
69+
70+
# Use local ./bin directory for executables
71+
export PATH="./bin:$PATH"

0 commit comments

Comments
 (0)