Bug 1953978 - mach commands-completion compatibility with Bash 3 r=ahal
Differential Revision: https://phabricator.services.mozilla.com/D241525
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
# Is bash completion too slow to your taste?
|
||||||
|
# Consider generating fast completion scripts with:
|
||||||
|
# ./mach mach-completion bash -f path/to/mach.bash-completion
|
||||||
|
# See https://firefox-source-docs.mozilla.org/mach/usage.html#tab-completion
|
||||||
|
|
||||||
function _mach()
|
function _mach()
|
||||||
{
|
{
|
||||||
local cur targets
|
local cur targets
|
||||||
|
|||||||
@@ -230,7 +230,12 @@ def _zsh_describe(value, description=None):
|
|||||||
help="File path to save completion script.",
|
help="File path to save completion script.",
|
||||||
)
|
)
|
||||||
def completion_bash(command_context, outfile):
|
def completion_bash(command_context, outfile):
|
||||||
commands_subcommands = []
|
# Note: Make sure to generate a Bash 3-compatible script, because macOS
|
||||||
|
# ships with Bash 3 by default. One of the consequences is that Bash 4
|
||||||
|
# features such as associative arrays ("declare -A") cannot be used.
|
||||||
|
# For example, while completion_zsh uses the associative array syntax for
|
||||||
|
# commands_subcommands, here we use case/esac instead.
|
||||||
|
case_commands_subcommands = []
|
||||||
case_options = []
|
case_options = []
|
||||||
case_subcommands = []
|
case_subcommands = []
|
||||||
for i, cmd in enumerate(commands_info(command_context)):
|
for i, cmd in enumerate(commands_info(command_context)):
|
||||||
@@ -274,9 +279,15 @@ def completion_bash(command_context, outfile):
|
|||||||
# Build case statement for subcommands.
|
# Build case statement for subcommands.
|
||||||
subcommands = [_zsh_describe(s.subcommand, None) for s in cmd.subcommands]
|
subcommands = [_zsh_describe(s.subcommand, None) for s in cmd.subcommands]
|
||||||
if subcommands:
|
if subcommands:
|
||||||
commands_subcommands.append(
|
comsubs = " ".join([h.subcommand for h in cmd.subcommands])
|
||||||
'[{}]=" {} "'.format(
|
case_commands_subcommands.append(
|
||||||
cmd.name, " ".join([h.subcommand for h in cmd.subcommands])
|
"\n".join(
|
||||||
|
[
|
||||||
|
" ({})".format(cmd.name),
|
||||||
|
' comsubs=" {} "'.format(comsubs),
|
||||||
|
" ;;",
|
||||||
|
"",
|
||||||
|
]
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -295,10 +306,10 @@ def completion_bash(command_context, outfile):
|
|||||||
opt for opt_strs in global_options(command_context) for opt in opt_strs
|
opt for opt_strs in global_options(command_context) for opt in opt_strs
|
||||||
]
|
]
|
||||||
context = {
|
context = {
|
||||||
|
"case_commands_subcommands": "\n".join(sorted(case_commands_subcommands)),
|
||||||
"case_options": "\n".join(case_options),
|
"case_options": "\n".join(case_options),
|
||||||
"case_subcommands": "\n".join(case_subcommands),
|
"case_subcommands": "\n".join(case_subcommands),
|
||||||
"commands": " ".join(commands(command_context)),
|
"commands": " ".join(commands(command_context)),
|
||||||
"commands_subcommands": " ".join(sorted(commands_subcommands)),
|
|
||||||
"globalopts": " ".join(sorted(globalopts)),
|
"globalopts": " ".join(sorted(globalopts)),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ _mach_complete()
|
|||||||
{
|
{
|
||||||
local com coms comsubs cur opts script sub subs
|
local com coms comsubs cur opts script sub subs
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
declare -A comsubs=( %(commands_subcommands)s )
|
|
||||||
|
|
||||||
_get_comp_words_by_ref -n : cur words
|
_get_comp_words_by_ref -n : cur words
|
||||||
# for an alias, get the real script behind it
|
# for an alias, get the real script behind it
|
||||||
@@ -19,7 +18,10 @@ _mach_complete()
|
|||||||
|
|
||||||
if [[ -z $com ]]; then
|
if [[ -z $com ]]; then
|
||||||
com=$word
|
com=$word
|
||||||
elif [[ "${comsubs[$com]}" == *" $word "* ]]; then
|
case $com in
|
||||||
|
%(case_commands_subcommands)s
|
||||||
|
esac
|
||||||
|
elif [[ $comsubs == *" $word "* ]]; then
|
||||||
sub=$word
|
sub=$word
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user