Class: Clamp::Completion::FishGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/clamp/completion/fish_generator.rb

Overview

Generates fish shell completion scripts.

Instance Method Summary collapse

Constructor Details

#initialize(command_class, executable_name) ⇒ FishGenerator

Returns a new instance of FishGenerator.



9
10
11
12
# File 'lib/clamp/completion/fish_generator.rb', line 9

def initialize(command_class, executable_name)
  @command_class = command_class
  @executable_name = executable_name
end

Instance Method Details

#generateObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/clamp/completion/fish_generator.rb', line 14

def generate
  lines = []
  lines << "# Fish completions for #{@executable_name}"
  lines << "# Generated by Clamp"
  lines << ""
  helpers = [subcmd_args_function]

  # Track visible option switches at each depth, so we can identify
  # which options are new at each level. Depth-first walk order means
  # the entry at depth N-1 is always the current node's parent.
  switches_at_depth = []

  Completion.walk_command_tree(@command_class) do |cmd, path, has_children|
    generate_option_completions(lines, cmd, path, switches_at_depth)

    next unless has_children

    # Subcommand names need an exclusive condition (only at this level).
    child_names = cmd.recognised_subcommands.flat_map(&:names)
    exclusive_cond = condition_for(path, child_names)
    subcmd_cond = subcommand_condition(cmd, path, exclusive_cond, helpers)
    generate_subcommand_completions(lines, cmd, subcmd_cond)
    lines << ""
  end
  "#{helpers.join("\n\n")}\n\n#{lines.join("\n")}\n"
end