Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: bash completion script #17147

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 2 additions & 18 deletions completions/bun.bash
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,11 @@ _read_scripts_in_package_json() {
[[ "${COMP_WORDS[${line}]}" == "--cwd" ]] && working_dir="${COMP_WORDS[$((line + 1))]}";
done

[[ -f "${working_dir}/package.json" ]] && package_json=$(<"${working_dir}/package.json");

[[ "${package_json}" =~ "\"scripts\""[[:space:]]*":"[[:space:]]*\{(.*)\} ]] && {
local package_json_compreply;
local matched="${BASH_REMATCH[@]:1}";
local scripts="${matched%%\}*}";
scripts="${scripts//@(\"|\')/}";
readarray -td, scripts <<<"${scripts}";
for completion in "${scripts[@]}"; do
package_json_compreply+=( "${completion%:*}" );
done
COMPREPLY+=( $(compgen -W "${package_json_compreply[*]}" -- "${cur_word}") );
}
COMPREPLY+=( $(compgen -W "$(bun getcompletes s)" -- "${cur_word}") );

# when a script is passed as an option, do not show other scripts as part of the completion anymore
local re_prev_script="(^| )${prev}($| )";
[[
( "${COMPREPLY[*]}" =~ ${re_prev_script} && -n "${COMP_WORDS[2]}" ) || \
( "${COMPREPLY[*]}" =~ ${re_comp_word_script} )
]] && {
[[ "${COMPREPLY[*]}" =~ ${re_prev_script} && -n "${COMP_WORDS[2]}" ]] && {
local re_script=$(echo ${package_json_compreply[@]} | sed 's/[^ ]*/(&)/g');
local new_reply=$(echo "${COMPREPLY[@]}" | sed -E "s/$re_script//");
COMPREPLY=( $(compgen -W "${new_reply}" -- "${cur_word}") );
Expand Down Expand Up @@ -164,7 +149,6 @@ _bun_completions() {
"${GLOBAL_OPTIONS[*]}" \
"${GLOBAL_OPTIONS[SHORT_OPTIONS]}"

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

# determine if completion should be continued
Expand Down