Merge pull request #10845 from nandahkrishna/fix-completions-zsh

completions: fix zsh autocompletion issues
This commit is contained in:
Nanda H Krishna 2021-03-17 10:55:19 +05:30 committed by GitHub
commit 53f34af1c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 290 additions and 162 deletions

View File

@ -213,29 +213,51 @@ module Homebrew
def generate_zsh_subcommand_completion(command)
return unless command_gets_completions? command
options = command_options(command).sort.map do |opt, desc|
next opt if desc.blank?
options = command_options(command)
conflicts = generate_zsh_option_exclusions(command, opt)
"#{conflicts}#{opt}[#{format_description desc}]"
end
args_options = []
if (types = Commands.named_args_type(command))
named_args_strings, named_args_types = types.partition { |type| type.is_a? String }
named_args_types.each do |type|
next unless ZSH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING.key? type
options << "::#{type}:#{ZSH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING[type]}"
args_options << "- #{type}"
opt = "--#{type.to_s.gsub(/(installed|outdated)_/, "")}"
if options.key?(opt)
desc = options[opt]
if desc.blank?
args_options << opt
else
conflicts = generate_zsh_option_exclusions(command, opt)
args_options << "#{conflicts}#{opt}[#{format_description desc}]"
end
options.delete(opt)
end
args_options << "*::#{type}:#{ZSH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING[type]}"
end
options << "::subcommand:(#{named_args_strings.join(" ")})" if named_args_strings.any?
if named_args_strings.any?
args_options << "- subcommand"
args_options << "*::subcommand:(#{named_args_strings.join(" ")})"
end
end
options = options.sort.map do |opt, desc|
next opt if desc.blank?
conflicts = generate_zsh_option_exclusions(command, opt)
"#{conflicts}#{opt}[#{format_description desc}]"
end
options += args_options
<<~COMPLETION
# brew #{command}
_brew_#{Commands.method_name command}() {
_arguments \\
#{options.map! { |opt| "'#{opt}'" }.join(" \\\n ")}
#{options.map! { |opt| opt.start_with?("- ") ? opt : "'#{opt}'" }.join(" \\\n ")}
}
COMPLETION
end

View File

@ -321,7 +321,8 @@ describe Homebrew::Completions do
'--hide[Act as if none of the specified hidden are installed. hidden should be a comma-separated list of formulae]' \\
'--quiet[Make some output more quiet]' \\
'--verbose[Make some output more verbose]' \\
'::formula:__brew_formulae'
- formula \\
'*::formula:__brew_formulae'
}
COMPLETION
end
@ -343,9 +344,12 @@ describe Homebrew::Completions do
end
it "returns appropriate completion for a command with multiple named arg types" do
completion = described_class.generate_zsh_subcommand_completion("upgrade")
completion = described_class.generate_zsh_subcommand_completion("livecheck")
expect(completion).to match(
/'::outdated_formula:__brew_outdated_formulae' \\\n '::outdated_cask:__brew_outdated_casks'\n}$/,
/'*::formula:__brew_formulae'/,
)
expect(completion).to match(
/'*::cask:__brew_casks'\n}$/,
)
end
end

File diff suppressed because it is too large Load Diff