Class: Toys::Completion::Context

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

Overview

The context in which to determine completion candidates.

The #fragment is the entire word being completed, and each candidate returned for it must be a replacement for that entire word. If the shell replaces less than the whole word, because it breaks words at certain characters, the completion engine trims the candidates to match; that is not the concern of a completion.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loader:, previous_words: [], fragment: "", **params) ⇒ Context

Create a completion context.

Extra params are optional and can be used to affect the behavior in specific cases. Currently these are:

  • shell: :bash causes the FileSystem completion to include glob expansions, since bash doesn't handle those directly
  • disable_flags: true causes tool completion to omit flag completions which is used by the toys do built-in tool

Parameters:

  • loader (Toys::Loader)

    The loader providing the tools being completed. Required.

  • previous_words (Array<String>) (defaults to: [])

    Array of complete strings that appeared prior to the fragment to complete.

  • fragment (String) (defaults to: "")

    The string fragment to complete.

  • params (Hash)

    Miscellaneous context data



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/toys/completion.rb', line 43

def initialize(loader:, previous_words: [], fragment: "", **params)
  @loader = loader
  @previous_words = previous_words
  @fragment = fragment
  extra_params = {
    loader: loader, previous_words: previous_words, fragment: fragment
  }
  @params = params.merge(extra_params)
  @tool = nil
  @args = nil
  @arg_parser = nil
end

Instance Attribute Details

#fragmentString (readonly)

The current string fragment to complete. This is the entire word under the cursor, and candidates must replace all of it.

Returns:

  • (String)


83
84
85
# File 'lib/toys/completion.rb', line 83

def fragment
  @fragment
end

#loaderToys::Loader (readonly)

The loader providing the tools being completed.

Returns:



70
71
72
# File 'lib/toys/completion.rb', line 70

def loader
  @loader
end

#previous_wordsArray<String> (readonly)

All previous words.

Returns:

  • (Array<String>)


76
77
78
# File 'lib/toys/completion.rb', line 76

def previous_words
  @previous_words
end

Instance Method Details

#[](key) ⇒ Object Also known as: get

Get data for arbitrary key.

Parameters:

  • key (Symbol)

Returns:

  • (Object)


90
91
92
# File 'lib/toys/completion.rb', line 90

def [](key)
  @params[key]
end

#arg_parserToys::ArgParser

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

Returns:



120
121
122
123
# File 'lib/toys/completion.rb', line 120

def arg_parser
  lookup_tool
  @arg_parser ||= ArgParser.new(@tool, @loader).parse(@args)
end

#argsArray<String>

An array of complete arguments passed to the tool, prior to the fragment to complete.

Returns:

  • (Array<String>)


109
110
111
112
# File 'lib/toys/completion.rb', line 109

def args
  lookup_tool
  @args
end

#toolToys::ToolDefinition

The tool being invoked, which should control the completion.



99
100
101
102
# File 'lib/toys/completion.rb', line 99

def tool
  lookup_tool
  @tool
end

#with(**delta_params) ⇒ Toys::Completion::Context

Create a new completion context with the given modifications.

Parameters:

  • delta_params (Hash)

    Replace context data.

Returns:



62
63
64
# File 'lib/toys/completion.rb', line 62

def with(**delta_params)
  Context.new(**@params, **delta_params)
end