class Toys::Completion::Context

The context in which to determine completion candidates.

Attributes

cli[R]

The CLI being run. @return [Toys::CLI]

fragment[R]

The current string fragment to complete @return [String]

fragment_prefix[R]

A non-completed prefix for the current fragment. @return [String]

previous_words[R]

All previous words. @return [Array<String>]

Public Class Methods

new(cli:, previous_words: [], fragment_prefix: "", fragment: "", **params) click to toggle source

Create a completion context

@param cli [Toys::CLI] The CLI being run. Required. @param previous_words [Array<String>] Array of complete strings that

appeared prior to the fragment to complete.

@param fragment_prefix [String] A prefix in the fragment that does not

participate in completion. (e.g. "key=")

@param fragment [String] The string fragment to complete. @param params [Hash] Miscellaneous context data

# File lib/toys/completion.rb, line 30
def initialize(cli:, previous_words: [], fragment_prefix: "", fragment: "", **params)
  @cli = cli
  @previous_words = previous_words
  @fragment_prefix = fragment_prefix
  @fragment = fragment
  extra_params = {
    cli: cli, previous_words: previous_words, fragment_prefix: fragment_prefix,
    fragment: fragment
  }
  @params = params.merge(extra_params)
  @tool = nil
  @args = nil
  @arg_parser = nil
end

Public Instance Methods

[](key) click to toggle source

Get data for arbitrary key. @param [Symbol] key @return [Object]

# File lib/toys/completion.rb, line 84
def [](key)
  @params[key]
end
Also aliased as: get
arg_parser() click to toggle source

Current ArgParser indicating the status of argument parsing up to this point.

@return [Toys::ArgParser]

# File lib/toys/completion.rb, line 114
def arg_parser
  lookup_tool
  @arg_parser ||= ArgParser.new(@cli, @tool).parse(@args)
end
args() click to toggle source

An array of complete arguments passed to the tool, prior to the fragment to complete. @return [Array<String>]

# File lib/toys/completion.rb, line 103
def args
  lookup_tool
  @args
end
get(key)
Alias for: []
inspect() click to toggle source

@private

# File lib/toys/completion.rb, line 120
def inspect
  "<Toys::Completion::Context previous=#{previous_words.inspect}" \
    " prefix=#{fragment_prefix.inspect} fragment=#{fragment.inspect}>"
end
tool() click to toggle source

The tool being invoked, which should control the completion. @return [Toys::ToolDefinition]

# File lib/toys/completion.rb, line 93
def tool
  lookup_tool
  @tool
end
with(**delta_params) click to toggle source

Create a new completion context with the given modifications.

@param delta_params [Hash] Replace context data. @return [Toys::Completion::Context]

# File lib/toys/completion.rb, line 51
def with(**delta_params)
  Context.new(**@params.merge(delta_params))
end

Private Instance Methods

lookup_tool() click to toggle source
# File lib/toys/completion.rb, line 127
def lookup_tool
  @tool, @args = @cli.loader.lookup(@previous_words) unless @tool
end