Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 38 additions & 52 deletions completions/bun.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

_file_arguments() {
local extensions="${1}"
local reset=$(shopt -p globstar)
shopt -s globstar
local reset="$(shopt -p extglob 2>/dev/null)"
shopt -s extglob 2>/dev/null

if [[ -z "${cur_word}" ]]; then
COMPREPLY=( $(compgen -fG -X "${extensions}" -- "${cur_word}") );
else
COMPREPLY=( $(compgen -f -X "${extensions}" -- "${cur_word}") );
fi
COMPREPLY+=( $(compgen -d -- "${cur_word}") )
COMPREPLY+=( $(compgen -f -X "${extensions}" -- "${cur_word}") )

$reset
eval "${reset}" 2>/dev/null
}

_long_short_completion() {
Expand Down Expand Up @@ -69,17 +66,6 @@ _read_scripts_in_package_json() {
[[ -n "${keep}" ]] && filtered_reply+=( "${reply_word}" );
done
COMPREPLY=( "${filtered_reply[@]}" );
replaced_script="${prev}";
}
}


_subcommand_comp_reply() {
local cur_word="${1}"
local sub_commands="${2}"
local regexp_subcommand="^[dbcriauh]";
[[ "${prev}" =~ ${regexp_subcommand} ]] && {
COMPREPLY+=( $(compgen -W "${sub_commands}" -- "${cur_word}") );
}
}

Expand All @@ -90,9 +76,10 @@ _bun_completions() {
declare -A PM_OPTIONS;

local SUBCOMMANDS="dev bun create run install add remove upgrade completions discord help init pm x test repl update audit dedupe prune outdated link unlink build";
local RUN_EXTS="!(*.@(js|mjs|cjs|ts|mts|cts|jsx|tsx|wasm|html)?($|))";

GLOBAL_OPTIONS[LONG_OPTIONS]="--use --cwd --bunfile --server-bunfile --config --disable-react-fast-refresh --disable-hmr --env-file --extension-order --jsx-factory --jsx-fragment --extension-order --jsx-factory --jsx-fragment --jsx-import-source --jsx-production --jsx-runtime --main-fields --no-summary --version --platform --public-dir --tsconfig-override --define --external --help --inject --loader --origin --port --dump-environment-variables --dump-limits --disable-bun-js";
GLOBAL_OPTIONS[SHORT_OPTIONS]="-c -v -d -e -h -i -l -u -p";
GLOBAL_OPTIONS[LONG_OPTIONS]="--use --cwd --bunfile --server-bunfile --config --disable-react-fast-refresh --disable-hmr --env-file --extension-order --jsx-factory --jsx-fragment --extension-order --jsx-factory --jsx-fragment --jsx-import-source --jsx-production --jsx-runtime --main-fields --no-summary --version --platform --public-dir --tsconfig-override --define --external --help --inject --loader --origin --port --dump-environment-variables --dump-limits --disable-bun-js --hot --watch --no-clear-screen --smol --bun --silent --preload --require --import --inspect --inspect-brk --inspect-wait";
GLOBAL_OPTIONS[SHORT_OPTIONS]="-c -v -d -e -h -i -l -u -p -b -r";

PACKAGE_OPTIONS[ADD_OPTIONS_LONG]="--development --optional --peer --catalog --filter";
PACKAGE_OPTIONS[ADD_OPTIONS_SHORT]="-d -F";
Expand All @@ -116,13 +103,26 @@ _bun_completions() {
local cur_word="${COMP_WORDS[${COMP_CWORD}]}";
local prev="${COMP_WORDS[$(( COMP_CWORD - 1 ))]}";

# Locate the first non-option word so that runtime flags before the
# subcommand (e.g. `bun --hot run …`, `bun --watch …`) don't break dispatch.
local first_word="" i;
for (( i=1; i < COMP_CWORD; i++ )); do
case "${COMP_WORDS[i]}" in
=|:) ((i++));;
--conditions|--config|--console-depth|--cwd|--define|--dns-result-order|--elide-lines|--env-file|--eval|--extension-order|--fetch-preconnect|--filter|--import|--install|--jsx-factory|--jsx-fragment|--jsx-import-source|--jsx-runtime|--loader|--main-fields|--max-http-header-size|--origin|--port|--preload|--print|--public-dir|--require|--shell|--target|--title|--tsconfig-override|--unhandled-rejections|-F|-c|-d|-e|-l|-p|-r|-u)
Comment thread
robobun marked this conversation as resolved.
((i++)); [[ "${COMP_WORDS[i]}" == "=" || "${COMP_WORDS[i]}" == ":" ]] && ((i++));;
-*) ;;
*) first_word="${COMP_WORDS[i]}"; break;;
esac
done
Comment thread
robobun marked this conversation as resolved.

case "${prev}" in
help|--help|-h|-v|--version) return;;
-c|--config) _file_arguments "!*.toml" && return;;
--bunfile) _file_arguments "!*.bun" && return;;
--server-bunfile) _file_arguments "!*.server.bun" && return;;
--backend)
case "${COMP_WORDS[1]}" in
case "${first_word}" in
a|add|remove|rm|install|i|dedupe|update|up)
COMPREPLY=( $(compgen -W "clonefile copyfile hardlink clonefile_each_dir symlink" -- "${cur_word}") );
;;
Expand All @@ -132,7 +132,7 @@ _bun_completions() {
COMPREPLY=( $(compgen -W "dev optional peer" -- "${cur_word}") );
return;;
-F|--filter)
case "${COMP_WORDS[1]}" in
case "${first_word}" in
a|add|remove|rm|i|install|prune|update|up) return;;
esac
;;
Expand All @@ -156,8 +156,8 @@ _bun_completions() {
return;;
esac

case "${COMP_WORDS[1]}" in
help|completions|--help|-h|-v|--version) return;;
case "${first_word}" in
help|completions) return;;
add|a)
_long_short_completion \
"${PACKAGE_OPTIONS[ADD_OPTIONS_LONG]} ${PACKAGE_OPTIONS[ADD_OPTIONS_SHORT]} ${PACKAGE_OPTIONS[SHARED_OPTIONS_LONG]} ${PACKAGE_OPTIONS[SHARED_OPTIONS_SHORT]}" \
Expand Down Expand Up @@ -201,45 +201,31 @@ _bun_completions() {
COMPREPLY=( $(compgen -W "--help -h --eval -e --print -p --preload -r --smol --config -c --cwd --env-file --no-env-file" -- "${cur_word}") );
return;;
run)
_file_arguments "!(*.@(js|ts|jsx|tsx|mjs|cjs)?($|))";
COMPREPLY+=( $(compgen -W "--version --cwd --help --silent -v -h" -- "${cur_word}" ) );
_read_scripts_in_package_json;
COMPREPLY+=( $(compgen -W "--version --cwd --help --silent -v -h" -- "${cur_word}" ) );
_file_arguments "${RUN_EXTS}";
return;;
pm)
_long_short_completion \
"${PM_OPTIONS[LONG_OPTIONS]} ${PM_OPTIONS[SHORT_OPTIONS]}";
COMPREPLY+=( $(compgen -W "bin ls licenses cache hash hash-print hash-string" -- "${cur_word}") );
return;;
*)
local replaced_script;
"")
_long_short_completion \
"${GLOBAL_OPTIONS[*]}" \
"${GLOBAL_OPTIONS[SHORT_OPTIONS]}"

_read_scripts_in_package_json;
_subcommand_comp_reply "${cur_word}" "${SUBCOMMANDS}";

# determine if completion should be continued
# when the current word is an empty string
# the previous word is not part of the allowed completion
# the previous word is not an argument to the last two option
[[ -z "${cur_word}" ]] && {
local prev_in_reply="";
local reply_word;
for reply_word in "${COMPREPLY[@]}"; do
[[ "${reply_word}" == "${prev}" ]] && { prev_in_reply=1; break; };
done
[[ -z "${prev_in_reply}" ]] && {
local re_prev_prev="(^| )${COMP_WORDS[(( COMP_CWORD - 2 ))]}($| )";
local global_option_with_extra_args="--bunfile --server-bunfile --config --port --cwd --public-dir --jsx-runtime --platform --loader";
[[
( -n "${replaced_script}" && "${replaced_script}" == "${prev}" ) || \
( "${global_option_with_extra_args}" =~ ${re_prev_prev} )
]] && return;
unset COMPREPLY;
}
[[ "${cur_word}" != -* ]] && {
_read_scripts_in_package_json;
COMPREPLY+=( $(compgen -W "${SUBCOMMANDS}" -- "${cur_word}") );
_file_arguments "${RUN_EXTS}";
Comment thread
robobun marked this conversation as resolved.
}
return;;
*)
_long_short_completion \
"${GLOBAL_OPTIONS[*]}" \
"${GLOBAL_OPTIONS[SHORT_OPTIONS]}"
[[ "${cur_word}" != -* ]] && COMPREPLY+=( $(compgen -f -- "${cur_word}") );
return;;
Comment thread
robobun marked this conversation as resolved.
esac

}
Expand Down
Loading