Class: Samovar::Completion::Context

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

Overview

The context provided to dynamic completion callbacks.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, arguments, current, row = nil, environment: ENV) ⇒ Context

Initialize a new completion context.



34
35
36
37
38
39
40
# File 'lib/samovar/completion/context.rb', line 34

def initialize(table, arguments, current, row = nil, environment: ENV)
	@table = table
	@arguments = arguments
	@current = current
	@row = row
	@environment = environment
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



46
47
48
# File 'lib/samovar/completion/context.rb', line 46

def arguments
  @arguments
end

#currentObject (readonly)

Returns the value of attribute current.



49
50
51
# File 'lib/samovar/completion/context.rb', line 49

def current
  @current
end

#environmentObject (readonly)

Returns the value of attribute environment.



55
56
57
# File 'lib/samovar/completion/context.rb', line 55

def environment
  @environment
end

#rowObject (readonly)

Returns the value of attribute row.



52
53
54
# File 'lib/samovar/completion/context.rb', line 52

def row
  @row
end

#tableObject (readonly)

Returns the value of attribute table.



43
44
45
# File 'lib/samovar/completion/context.rb', line 43

def table
  @table
end

#The token being completed.(tokenbeingcompleted.) ⇒ Object (readonly)



49
# File 'lib/samovar/completion/context.rb', line 49

attr :current

#The truncated command-line arguments.(truncatedcommand-linearguments.) ⇒ Object (readonly)



46
# File 'lib/samovar/completion/context.rb', line 46

attr :arguments

Class Method Details

.for(command_class, arguments, environment: ENV) ⇒ Object

Build a context for a command class and argument list.



18
19
20
21
22
23
24
25
# File 'lib/samovar/completion/context.rb', line 18

def self.for(command_class, arguments, environment: ENV)
	return self.new(
		command_class.table.merged,
		arguments,
		arguments.last || "",
		environment: environment,
	)
end

Instance Method Details

#completeObject

Complete the current command class.



81
82
83
# File 'lib/samovar/completion/context.rb', line 81

def complete
	complete_rows(@table, words)
end

#complete_command(command_class, words = []) ⇒ Object

Complete the given command class with completed words.



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

def complete_command(command_class, words = [])
	complete_rows(command_class.table.merged, words)
end

#complete_rows(table, input) ⇒ Object

Complete the rows in a command table. The input array is mutable and may be consumed by parser rows.



100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/samovar/completion/context.rb', line 100

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

#The command table to complete.=(commandtabletocomplete. = (value)) ⇒ Object



43
# File 'lib/samovar/completion/context.rb', line 43

attr :table

#The environment for completion callbacks.=(environment) ⇒ Object



55
# File 'lib/samovar/completion/context.rb', line 55

attr :environment

#The parser row whose value is being completed.=(parserrowwhosevalueisbeingcompleted. = (value)) ⇒ Object



52
# File 'lib/samovar/completion/context.rb', line 52

attr :row

#with_row(row) ⇒ Object

Create a context for completing the given parser row.



61
62
63
64
65
66
67
68
69
# File 'lib/samovar/completion/context.rb', line 61

def with_row(row)
	return self.class.new(
		@table,
		@arguments,
		@current,
		row,
		environment: @environment,
	)
end

#wordsObject

The completed words before the current token.



74
75
76
# File 'lib/samovar/completion/context.rb', line 74

def words
	@arguments.take(@arguments.size - 1)
end