Class: Toys::ToolDefinition::DefaultCompletion

Inherits:
Completion::Base show all
Defined in:
lib/toys/tool_definition.rb

Overview

A Completion that implements the default algorithm for a tool.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(complete_subtools: true, include_hidden_subtools: false, complete_args: true, complete_flags: true, complete_flag_values: true, delegation_target: nil) ⇒ DefaultCompletion

Create a completion given configuration options.

Parameters:

  • complete_subtools (Boolean) (defaults to: true)

    Whether to complete subtool names

  • include_hidden_subtools (Boolean) (defaults to: false)

    Whether to include hidden subtools (i.e. those beginning with an underscore)

  • complete_args (Boolean) (defaults to: true)

    Whether to complete positional args

  • complete_flags (Boolean) (defaults to: true)

    Whether to complete flag names

  • complete_flag_values (Boolean) (defaults to: true)

    Whether to complete flag values

  • delegation_target (Array<String>, nil) (defaults to: nil)

    Delegation target, or nil if none.



1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
# File 'lib/toys/tool_definition.rb', line 1162

def initialize(complete_subtools: true, include_hidden_subtools: false,
               complete_args: true, complete_flags: true, complete_flag_values: true,
               delegation_target: nil)
  super()
  @complete_subtools = complete_subtools
  @include_hidden_subtools = include_hidden_subtools
  @complete_flags = complete_flags
  @complete_args = complete_args
  @complete_flag_values = complete_flag_values
  @delegation_target = delegation_target
end

Instance Attribute Details

#delegation_targetArray<String>?

Delegation target, or nil for none.

Returns:

  • (Array<String>)

    if there is a delegation target

  • (nil)

    if there is no delegation target



1219
1220
1221
# File 'lib/toys/tool_definition.rb', line 1219

def delegation_target
  @delegation_target
end

Instance Method Details

#call(context) ⇒ Array<Toys::Completion::Candidate>

Returns candidates for the current completion.

Parameters:

Returns:



1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
# File 'lib/toys/tool_definition.rb', line 1228

def call(context)
  candidates = valued_flag_candidates(context)
  return candidates if candidates
  candidates = subtool_or_arg_candidates(context)
  candidates += plain_flag_candidates(context)
  candidates += flag_value_candidates(context)
  if delegation_target
    delegate_tool = context.cli.loader.lookup_specific(delegation_target)
    if delegate_tool
      context = context.with(previous_words: delegation_target)
      candidates += delegate_tool.completion.call(context)
    end
  end
  candidates
end

#complete_args?Boolean

Whether to complete positional args

Returns:

  • (Boolean)


1202
1203
1204
# File 'lib/toys/tool_definition.rb', line 1202

def complete_args?
  @complete_args
end

#complete_flag_values?Boolean

Whether to complete flag values

Returns:

  • (Boolean)


1210
1211
1212
# File 'lib/toys/tool_definition.rb', line 1210

def complete_flag_values?
  @complete_flag_values
end

#complete_flags?Boolean

Whether to complete flags

Returns:

  • (Boolean)


1194
1195
1196
# File 'lib/toys/tool_definition.rb', line 1194

def complete_flags?
  @complete_flags
end

#complete_subtools?Boolean

Whether to complete subtool names

Returns:

  • (Boolean)


1178
1179
1180
# File 'lib/toys/tool_definition.rb', line 1178

def complete_subtools?
  @complete_subtools
end

#include_hidden_subtools?Boolean

Whether to include hidden subtools

Returns:

  • (Boolean)


1186
1187
1188
# File 'lib/toys/tool_definition.rb', line 1186

def include_hidden_subtools?
  @include_hidden_subtools
end