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.



37
38
39
40
41
42
43
# File 'lib/samovar/completion/context.rb', line 37

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.



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

def arguments
  @arguments
end

#currentObject (readonly)

Returns the value of attribute current.



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

def current
  @current
end

#environmentObject (readonly)

Returns the value of attribute environment.



58
59
60
# File 'lib/samovar/completion/context.rb', line 58

def environment
  @environment
end

#rowObject (readonly)

Returns the value of attribute row.



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

def row
  @row
end

#tableObject (readonly)

Returns the value of attribute table.



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

def table
  @table
end

#The completed words before the current token.(completedwordsbeforethecurrenttoken.) ⇒ Object (readonly)



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

attr :arguments

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



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

attr :current

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
26
27
28
# File 'lib/samovar/completion/context.rb', line 18

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

Instance Method Details

#completeObject

Complete the current command class.



77
78
79
# File 'lib/samovar/completion/context.rb', line 77

def complete
	complete_rows(@table, @arguments.dup)
end

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

Complete the given command class with completed words.



86
87
88
# File 'lib/samovar/completion/context.rb', line 86

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.



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/samovar/completion/context.rb', line 96

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



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

attr :table

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



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

attr :environment

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



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

attr :row

#with_row(row) ⇒ Object

Create a context for completing the given parser row.



64
65
66
67
68
69
70
71
72
# File 'lib/samovar/completion/context.rb', line 64

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