class Context
The context provided to dynamic completion callbacks.
Definitions
def self.for(command_class, arguments, environment: ENV)
Build a context for a command class and argument list.
Signature
-
parameter
command_classClass The command class to complete.
-
parameter
argumentsArray(String) The truncated command-line arguments.
-
parameter
environmentHash The environment for completion callbacks.
-
returns
Context The completion context.
Implementation
def self.for(command_class, arguments, environment: ENV)
arguments = arguments.dup
current = arguments.pop || ""
return self.new(
command_class.table.merged,
arguments,
current,
environment: environment,
)
end
def initialize(table, arguments, current, row = nil, environment: ENV)
Initialize a new completion context.
Signature
-
parameter
tableTable The command table to complete.
-
parameter
argumentsArray(String) The completed words before the current token.
-
parameter
currentString The token being completed.
-
parameter
rowObject | Nil The parser row whose value is being completed.
-
parameter
environmentHash The environment for completion callbacks.
Implementation
def initialize(table, arguments, current, row = nil, environment: ENV)
@table = table
@arguments = arguments
@current = current
@row = row
@environment = environment
end
attr :table
Signature
-
attribute
Table The command table to complete.
attr :arguments
Signature
-
attribute
Array(String) The completed words before the current token.
attr :current
Signature
-
attribute
String The token being completed.
attr :row
Signature
-
attribute
Object | Nil The parser row whose value is being completed.
attr :environment
Signature
-
attribute
Hash The environment for completion callbacks.
def with_row(row)
Create a context for completing the given parser row.
Signature
-
parameter
rowObject The parser row whose value is being completed.
-
returns
Context The specialized completion context.
Implementation
def with_row(row)
return self.class.new(
@table,
@arguments,
@current,
row,
environment: @environment,
)
end
def complete
Complete the current command class.
Signature
-
returns
Result The completion result.
Implementation
def complete
complete_rows(@table, @arguments.dup)
end
def complete_command(command_class, words = [])
Complete the given command class with completed words.
Signature
-
parameter
command_classClass The command class to complete.
-
parameter
wordsArray(String) The completed words before the current token.
-
returns
Result The completion result.
Implementation
def complete_command(command_class, words = [])
complete_rows(command_class.table.merged, words)
end
def complete_rows(table, input)
Complete the rows in a command table. The input array is mutable and may be consumed by parser rows.
Signature
-
parameter
tableTable The command table to complete.
-
parameter
inputArray(String) The mutable completed words to consume.
-
returns
Result The completion result.
Implementation
def complete_rows(table, input)
collected = []
table.each do |row|
if row.respond_to?(:complete)
if result = row.complete(input, self, collected)
return result
end
end
end
return Result.new(collected)
end