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
0 commit comments