Module: Homebrew::Completions Private

Extended by:
Utils::Output::Mixin
Defined in:
completions.rb

Overview

This module is part of a private API. This module may only be used in the Homebrew/brew repository. Third parties should avoid using this module if possible, as it may be removed or changed without warning.

Helper functions for generating shell completions.

Defined Under Namespace

Classes: Variables

Constant Summary collapse

COMPLETIONS_DIR =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let((HOMEBREW_REPOSITORY/"completions").freeze, Pathname)
TEMPLATE_DIR =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let((HOMEBREW_LIBRARY_PATH/"completions").freeze, Pathname)
SHELLS =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[bash fish zsh].freeze
COMPLETIONS_EXCLUSION_LIST =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

%w[
  instal
  uninstal
  update-report
].freeze
BASH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({
  formula:           "__brew_complete_formulae",
  installed_formula: "__brew_complete_installed_formulae",
  outdated_formula:  "__brew_complete_outdated_formulae",
  cask:              "__brew_complete_casks",
  installed_cask:    "__brew_complete_installed_casks",
  outdated_cask:     "__brew_complete_outdated_casks",
  tap:               "__brew_complete_tapped",
  installed_tap:     "__brew_complete_tapped",
  command:           "__brew_complete_commands",
  diagnostic_check:  '__brewcomp "${__HOMEBREW_DOCTOR_CHECKS=$(brew doctor --list-checks)}"',
  file:              "__brew_complete_files",
  service:           "__brew_complete_services",
}.freeze, T::Hash[Symbol, String])
ZSH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({
  formula:           "__brew_formulae",
  installed_formula: "__brew_installed_formulae",
  outdated_formula:  "__brew_outdated_formulae",
  cask:              "__brew_casks",
  installed_cask:    "__brew_installed_casks",
  outdated_cask:     "__brew_outdated_casks",
  tap:               "__brew_any_tap",
  installed_tap:     "__brew_installed_taps",
  command:           "__brew_commands",
  diagnostic_check:  "__brew_diagnostic_checks",
  file:              "__brew_formulae_or_ruby_files",
  service:           "__brew_services",
}.freeze, T::Hash[Symbol, String])
FISH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.let({
  formula:           "__fish_brew_suggest_formulae_all",
  installed_formula: "__fish_brew_suggest_formulae_installed",
  outdated_formula:  "__fish_brew_suggest_formulae_outdated",
  cask:              "__fish_brew_suggest_casks_all",
  installed_cask:    "__fish_brew_suggest_casks_installed",
  outdated_cask:     "__fish_brew_suggest_casks_outdated",
  tap:               "__fish_brew_suggest_taps_installed",
  installed_tap:     "__fish_brew_suggest_taps_installed",
  command:           "__fish_brew_suggest_commands",
  diagnostic_check:  "__fish_brew_suggest_diagnostic_checks",
  service:           "__fish_brew_suggest_services",
}.freeze, T::Hash[Symbol, String])

Class Method Summary collapse

Methods included from Utils::Output::Mixin

issue_reporting_message, odebug, odeprecated, odie, odisabled, ofail, oh1, oh1_title, ohai, ohai_title, onoe, opoo, opoo_outside_github_actions, opoo_without_github_actions_annotation, pretty_cannot_install, pretty_deprecated, pretty_disabled, pretty_duration, pretty_install_status, pretty_installed, pretty_uninstalled, pretty_unmarked, pretty_upgradable, pretty_warning

Class Method Details

.command_gets_completions?(command) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


145
146
147
# File 'completions.rb', line 145

def self.command_gets_completions?(command)
  command_options(command).any? || Commands.command_subcommands(command).any?
end

.command_hidden_from_manpage?(command) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Boolean)


150
151
152
153
154
# File 'completions.rb', line 150

def self.command_hidden_from_manpage?(command)
  return false unless (cmd_path = Commands.path(command))

  Homebrew::CLI::Parser.from_cmd_path(cmd_path)&.hide_from_man_page == true
end

.command_options(command, subcommand: nil) ⇒ Hash{String => String}

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • command (String)
  • subcommand (String, nil) (defaults to: nil)

Returns:



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'completions.rb', line 180

def self.command_options(command, subcommand: nil)
  options = {}
  Commands.command_options(command, subcommand:)&.each do |option|
    next if option.blank?

    name = option.first
    desc = option.second
    if name.start_with? "--[no-]"
      options[name.gsub("[no-]", "")] = desc
      options[name.sub("[no-]", "no-")] = desc
    else
      options[name] = desc
    end
  end
  options
end

.completions_to_link?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
112
113
# File 'completions.rb', line 103

def self.completions_to_link?
  Tap.installed.each do |tap|
    next if tap.official_git_checkout?

    SHELLS.each do |shell|
      return true if (tap.path/"completions/#{shell}").exist?
    end
  end

  false
end

.format_description(description, fish: false) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • description (String)
  • fish (Boolean) (defaults to: false)

Returns:



170
171
172
173
174
175
176
177
# File 'completions.rb', line 170

def self.format_description(description, fish: false)
  description = if fish
    description.gsub("'", "\\\\'")
  else
    description.gsub("'", "'\\\\''")
  end
  description.gsub(/[<>]/, "").tr("\n", " ").chomp(".")
end

.format_zsh_argument(opt) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



330
331
332
333
334
335
336
# File 'completions.rb', line 330

def self.format_zsh_argument(opt)
  if opt.start_with?("- ")
    opt
  else
    "'#{opt}'"
  end
end

.generate_bash_completion_file(commands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'completions.rb', line 308

def self.generate_bash_completion_file(commands)
  commands -= Commands.internal_commands_aliases

  variables = Variables.new(
    aliases:              Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_cmd, command|
      "#{alias_cmd}) echo \"#{command}\" ;;"
    end,
    completion_functions: commands.filter_map do |command|
      generate_bash_subcommand_completion command
    end,
    maintainer_commands:  commands.select { |command| command_hidden_from_manpage?(command) },
    function_mappings:    commands.filter_map do |command|
      next unless command_gets_completions? command

      "#{command}) _brew_#{Commands.method_name command} ;;"
    end,
  )

  ERB.new((TEMPLATE_DIR/"bash.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end

.generate_bash_named_args_completion(types) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'completions.rb', line 198

def self.generate_bash_named_args_completion(types)
  named_completion_string = ""
  return named_completion_string if types.blank?

  named_args_strings, named_args_types = types.partition { |type| type.is_a? String }

  T.cast(named_args_types, T::Array[Symbol]).each do |type|
    next unless BASH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING.key? type

    named_completion_string += "\n  #{BASH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING[type]}"
  end

  named_completion_string += "\n  __brewcomp \"#{named_args_strings.join(" ")}\"" if named_args_strings.any?
  named_completion_string
end

.generate_bash_nested_subcommand_completion(command, subcommands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'completions.rb', line 215

def self.generate_bash_nested_subcommand_completion(command, subcommands)
  top_level_options = command_options(command).keys.sort.join("\n          ")
  subcommand_names = subcommand_completion_names(subcommands).join(" ")
  subcommand_cases = subcommands.map do |subcommand|
    "      #{([subcommand.name] + subcommand.aliases).join("|")}) subcommand=\"#{subcommand.name}\"; break ;;"
  end.join("\n")
  option_cases = subcommands.map do |subcommand|
    options = command_options(command, subcommand: subcommand.name).keys.sort.join("\n        ")
    <<~EOS
      #{subcommand.name})
              __brewcomp "
              #{options}
              "
              return
              ;;
    EOS
  end.join
  named_arg_cases = subcommands.filter_map do |subcommand|
    named_completion_string = generate_bash_named_args_completion(
      Commands.named_args_type(command, subcommand: subcommand.name),
    )
    next if named_completion_string.blank?

    <<~EOS
      #{subcommand.name})#{named_completion_string}
              ;;
    EOS
  end.join

  <<~COMPLETION
    _brew_#{Commands.method_name command}() {
      local cur="${COMP_WORDS[COMP_CWORD]}"
      local subcommand=""
      local i
      for (( i = 2; i < COMP_CWORD; i++ ))
      do
        case "${COMP_WORDS[i]}" in
    #{subcommand_cases}
          *) ;;
        esac
      done
      case "${cur}" in
        -*)
          case "${subcommand}" in
            "")
              __brewcomp "
              #{top_level_options}
              "
              return
              ;;
    #{option_cases.chomp}
            *) ;;
          esac
          ;;
        *) ;;
      esac
      case "${subcommand}" in
        "")
          __brewcomp "#{subcommand_names}"
          ;;
    #{named_arg_cases.chomp}
        *) ;;
      esac
    }
  COMPLETION
end

.generate_bash_subcommand_completion(command) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'completions.rb', line 283

def self.generate_bash_subcommand_completion(command)
  return unless command_gets_completions? command

  subcommands = Commands.command_subcommands(command)
  return generate_bash_nested_subcommand_completion(command, subcommands) if subcommands.present?

  named_completion_string = generate_bash_named_args_completion(Commands.named_args_type(command))

  <<~COMPLETION
    _brew_#{Commands.method_name command}() {
      local cur="${COMP_WORDS[COMP_CWORD]}"
      case "${cur}" in
        -*)
          __brewcomp "
          #{command_options(command).keys.sort.join("\n      ")}
          "
          return
          ;;
        *) ;;
      esac#{named_completion_string}
    }
  COMPLETION
end

.generate_fish_completion_file(commands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'completions.rb', line 655

def self.generate_fish_completion_file(commands)
  commands -= Commands.internal_commands_aliases

  variables = Variables.new(
    aliases:              Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.map do |alias_cmd, command|
      "        case '#{alias_cmd}'\n            echo '#{command}'"
    end,
    completion_functions: commands.filter_map do |command|
      generate_fish_subcommand_completion command
    end,
  )

  ERB.new((TEMPLATE_DIR/"fish.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end

.generate_fish_named_args(command, types, subcommand: nil) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
# File 'completions.rb', line 572

def self.generate_fish_named_args(command, types, subcommand: nil)
  named_args = []
  return named_args if types.blank?

  named_args_strings, named_args_types = types.partition { |type| type.is_a? String }

  T.cast(named_args_types, T::Array[Symbol]).each do |type|
    next unless FISH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING.key? type

    named_arg_function = FISH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING[type]
    if subcommand
      named_args << "__fish_brew_complete_sub_arg '#{command}' '#{subcommand}' -a '(#{named_arg_function})'"
      next
    end

    named_arg_prefix = "__fish_brew_complete_arg '#{command}; and not __fish_seen_argument"

    formula_option = command_options(command).key?("--formula")
    cask_option = command_options(command).key?("--cask")

    named_args << if formula_option && cask_option && type.to_s.end_with?("formula")
      "#{named_arg_prefix} -l cask -l casks' -a '(#{named_arg_function})'"
    elsif formula_option && cask_option && type.to_s.end_with?("cask")
      "#{named_arg_prefix} -l formula -l formulae' -a '(#{named_arg_function})'"
    else
      "__fish_brew_complete_arg '#{command}' -a '(#{named_arg_function})'"
    end
  end

  return named_args if subcommand

  named_args_strings.map do |named_arg_string|
    "__fish_brew_complete_sub_cmd '#{command}' '#{named_arg_string}'"
  end + named_args
end

.generate_fish_nested_subcommand_completion(command, subcommands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'completions.rb', line 609

def self.generate_fish_nested_subcommand_completion(command, subcommands)
  command_description = format_description Commands.command_description(command, short: true).to_s, fish: true
  lines = if COMPLETIONS_EXCLUSION_LIST.include?(command) ||
             Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key?(command)
    []
  else
    ["__fish_brew_complete_cmd '#{command}' '#{command_description}'"]
  end

  subcommands.each do |subcommand|
    description = subcommand.description
    ([subcommand.name] + subcommand.aliases).each do |subcommand_name|
      line = "__fish_brew_complete_sub_cmd '#{command}' '#{subcommand_name}'"
      line += " '#{format_description(description, fish: true)}'" if description.present?
      lines << line
    end
  end

  lines += command_options(command).sort.filter_map do |opt, desc|
    arg_line = "__fish_brew_complete_arg '#{command}; and [ (count (__fish_brew_args)) = 1 ]' " \
               "-l #{opt.sub(/^-+/, "")}"
    arg_line += " -d '#{format_description desc, fish: true}'" if desc.present?
    arg_line
  end

  subcommands.each do |subcommand|
    subcommand_names = ([subcommand.name] + subcommand.aliases).join(" ")
    lines += command_options(command, subcommand: subcommand.name).sort.filter_map do |opt, desc|
      arg_line = "__fish_brew_complete_sub_arg '#{command}' '#{subcommand_names}' " \
                 "-l #{opt.sub(/^-+/, "")}"
      arg_line += " -d '#{format_description desc, fish: true}'" if desc.present?
      arg_line
    end
    lines += generate_fish_named_args(
      command,
      Commands.named_args_type(command, subcommand: subcommand.name),
      subcommand: subcommand_names,
    )
  end

  <<~COMPLETION
    #{lines.join("\n").chomp}
  COMPLETION
end

.generate_fish_subcommand_completion(command) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
# File 'completions.rb', line 512

def self.generate_fish_subcommand_completion(command)
  command_description = format_description Commands.command_description(command, short: true).to_s, fish: true
  lines = if COMPLETIONS_EXCLUSION_LIST.include?(command) ||
             Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key?(command)
    []
  elsif command_hidden_from_manpage?(command)
    ["complete -f -c brew -n 'not __fish_brew_command; and set -q HOMEBREW_DEVELOPER' " \
     "-a '#{command}' -d '#{command_description}'"]
  else
    ["__fish_brew_complete_cmd '#{command}' '#{command_description}'"]
  end
  return unless command_gets_completions? command

  subcommands = Commands.command_subcommands(command)
  return generate_fish_nested_subcommand_completion(command, subcommands) if subcommands.present?

  options = command_options(command).sort.filter_map do |opt, desc|
    arg_line = "__fish_brew_complete_arg '#{command}' -l #{opt.sub(/^-+/, "")}"
    arg_line += " -d '#{format_description desc, fish: true}'" if desc.present?
    arg_line
  end

  subcommands = []
  named_args = []
  if (types = Commands.named_args_type(command))
    named_args_strings, named_args_types = types.partition { |type| type.is_a? String }

    T.cast(named_args_types, T::Array[Symbol]).each do |type|
      next unless FISH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING.key? type

      named_arg_function = FISH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING[type]
      named_arg_prefix = "__fish_brew_complete_arg '#{command}; and not __fish_seen_argument"

      formula_option = command_options(command).key?("--formula")
      cask_option = command_options(command).key?("--cask")

      named_args << if formula_option && cask_option && type.to_s.end_with?("formula")
        "#{named_arg_prefix} -l cask -l casks' -a '(#{named_arg_function})'"
      elsif formula_option && cask_option && type.to_s.end_with?("cask")
        "#{named_arg_prefix} -l formula -l formulae' -a '(#{named_arg_function})'"
      else
        "__fish_brew_complete_arg '#{command}' -a '(#{named_arg_function})'"
      end
    end

    named_args_strings.each do |subcommand|
      subcommands << "__fish_brew_complete_sub_cmd '#{command}' '#{subcommand}'"
    end
  end

  lines += subcommands + options + named_args
  <<~COMPLETION
    #{lines.join("\n").chomp}
  COMPLETION
end

.generate_zsh_arguments(command, options, types) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'completions.rb', line 364

def self.generate_zsh_arguments(command, options, types)
  options = options.dup

  args_options = []
  if types
    named_args_strings, named_args_types = types.partition { |type| type.is_a? String }

    T.cast(named_args_types, T::Array[Symbol]).each do |type|
      next unless ZSH_NAMED_ARGS_COMPLETION_FUNCTION_MAPPING.key? 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

    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

  options
end

.generate_zsh_completion_file(commands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'completions.rb', line 480

def self.generate_zsh_completion_file(commands)
  commands -= Commands.internal_commands_aliases

  variables = Variables.new(
    aliases:                      Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.filter_map do |alias_cmd, command|
      alias_cmd = "'#{alias_cmd}'" if alias_cmd.start_with? "-"
      command = "'#{command}'" if command.start_with? "-"
      "#{alias_cmd} #{command}"
    end,

    builtin_command_descriptions: commands.filter_map do |command|
      next if Commands::HOMEBREW_INTERNAL_COMMAND_ALIASES.key? command
      next if command_hidden_from_manpage?(command)

      zsh_command_description(command)
    end,

    maintainer_descriptions:      commands.filter_map do |command|
      next unless command_hidden_from_manpage?(command)

      zsh_command_description(command)
    end,

    completion_functions:         commands.filter_map do |command|
      generate_zsh_subcommand_completion command
    end,
  )

  ERB.new((TEMPLATE_DIR/"zsh.erb").read, trim_mode: ">").result(variables.instance_eval { binding })
end

.generate_zsh_nested_subcommand_completion(command, subcommands) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'completions.rb', line 409

def self.generate_zsh_nested_subcommand_completion(command, subcommands)
  top_level_arguments = generate_zsh_arguments(
    command,
    command_options(command),
    nil,
  ).map { |opt| format_zsh_argument(opt) } + [
    "'1:subcommand:->subcommand'",
    "'*::arg:->args'",
  ]
  subcommand_descriptions = subcommands.flat_map do |subcommand|
    description = subcommand.description
    ([subcommand.name] + subcommand.aliases).map do |subcommand_name|
      if description.present?
        "'#{subcommand_name}:#{format_description(description)}'"
      else
        "'#{subcommand_name}'"
      end
    end
  end.join("\n    ")

  subcommand_cases = subcommands.map do |subcommand|
    names = ([subcommand.name] + subcommand.aliases).join("|")
    options = generate_zsh_arguments(
      command,
      command_options(command, subcommand: subcommand.name),
      Commands.named_args_type(command, subcommand: subcommand.name),
    )
    <<~EOS
      #{names})
              _arguments \\
                #{options.map! { |opt| format_zsh_argument(opt) }.join(" \\\n          ")}
              ;;
    EOS
  end.join

  <<~COMPLETION
    # brew #{command}
    _brew_#{Commands.method_name command}() {
      local state
      local -a subcommands
      subcommands=(
        #{subcommand_descriptions}
      )

      _arguments -C \\
        #{top_level_arguments.join(" \\\n    ")}

      case "$state" in
        subcommand)
          _describe -t subcommands 'subcommand' subcommands
          ;;
        args)
          case "$words[1]" in
    #{subcommand_cases.chomp}
            *) ;;
          esac
          ;;
      esac
    }
  COMPLETION
end

.generate_zsh_option_exclusions(command, option) ⇒ String

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



472
473
474
475
476
477
# File 'completions.rb', line 472

def self.generate_zsh_option_exclusions(command, option)
  conflicts = Commands.option_conflicts(command, option.gsub(/^--?/, ""))
  return "" if conflicts.blank?

  "(#{conflicts.map { |conflict| "-#{"-" if conflict.size > 1}#{conflict}" }.join(" ")})"
end

.generate_zsh_subcommand_completion(command) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'completions.rb', line 339

def self.generate_zsh_subcommand_completion(command)
  return unless command_gets_completions? command

  subcommands = Commands.command_subcommands(command)
  return generate_zsh_nested_subcommand_completion(command, subcommands) if subcommands.present?

  options = command_options(command)
  options = generate_zsh_arguments(command, options, Commands.named_args_type(command))

  <<~COMPLETION
    # brew #{command}
    _brew_#{Commands.method_name command}() {
      _arguments \\
        #{options.map! { |opt| format_zsh_argument(opt) }.join(" \\\n    ")}
    }
  COMPLETION
end

.link!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



80
81
82
83
84
85
# File 'completions.rb', line 80

def self.link!
  Settings.write :linkcompletions, true
  Tap.installed.each do |tap|
    Utils::Link.link_completions tap.path, "brew completions link"
  end
end

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


98
99
100
# File 'completions.rb', line 98

def self.link_completions?
  Settings.read(:linkcompletions) == "true"
end

.show_completions_message_if_neededvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'completions.rb', line 116

def self.show_completions_message_if_needed
  return if Settings.read(:completionsmessageshown) == "true"
  return unless completions_to_link?

  ohai "Homebrew completions for external commands are unlinked by default!"
  puts <<~EOS
    To opt-in to automatically linking external tap shell completion files, run:
      brew completions link
    Then, follow the directions at #{Formatter.url("https://docs.brew.sh/Shell-Completion")}
  EOS

  Settings.write :completionsmessageshown, true
end

.subcommand_completion_names(subcommands) ⇒ Array<String>

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



165
166
167
# File 'completions.rb', line 165

def self.subcommand_completion_names(subcommands)
  subcommands.flat_map { |subcommand| [subcommand.name, *subcommand.aliases] }
end

.unlink!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



88
89
90
91
92
93
94
95
# File 'completions.rb', line 88

def self.unlink!
  Settings.write :linkcompletions, false
  Tap.installed.each do |tap|
    next if tap.official_git_checkout?

    Utils::Link.unlink_completions tap.path
  end
end

.update_shell_completions!void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



131
132
133
134
135
136
137
138
139
140
141
142
# File 'completions.rb', line 131

def self.update_shell_completions!
  commands = (Commands.internal_commands_paths + Commands.internal_developer_commands_paths)
             .map { |path| Commands.basename_without_extension(path) }
             .uniq
             .sort

  puts "Writing completions to #{COMPLETIONS_DIR}"

  (COMPLETIONS_DIR/"bash/brew").atomic_write generate_bash_completion_file(commands)
  (COMPLETIONS_DIR/"zsh/_brew").atomic_write generate_zsh_completion_file(commands)
  (COMPLETIONS_DIR/"fish/brew.fish").atomic_write generate_fish_completion_file(commands)
end

.zsh_command_description(command) ⇒ String?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



157
158
159
160
161
162
# File 'completions.rb', line 157

def self.zsh_command_description(command)
  description = Commands.command_description(command, short: true)
  return if description.blank?

  "'#{command}:#{format_description(description)}'"
end