Class: Lightning::Completion

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text_typed, bolt_key) ⇒ Completion

Returns a new instance of Completion.



9
10
11
12
# File 'lib/lightning/completion.rb', line 9

def initialize(text_typed, bolt_key)
  @text_typed = text_typed
  @bolt_key = bolt_key
end

Class Method Details

.complete(text_to_complete, bolt_key) ⇒ Object



5
6
7
# File 'lib/lightning/completion.rb', line 5

def self.complete(text_to_complete, bolt_key)
  new(text_to_complete, bolt_key).matches
end

Instance Method Details

#blob_to_regex(string) ⇒ Object

just converts * to .* to make a glob-like regex



29
30
31
# File 'lib/lightning/completion.rb', line 29

def blob_to_regex(string)
  string.gsub(/^\*|([^\.])\*/) {|e| $1 ? $1 + ".*" : ".*" }
end

#matchesObject



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/lightning/completion.rb', line 14

def matches
  if Lightning.config[:complete_regex]
    begin 
      possible_completions.grep(/#{blob_to_regex(typed)}/)
    rescue RegexpError
      ['Error: Invalid regular expression']
    end
  else
    possible_completions.select do |e|
      e[0, typed.length] == typed
    end
  end
end

#possible_completionsObject



39
40
41
# File 'lib/lightning/completion.rb', line 39

def possible_completions
  Lightning.bolts[@bolt_key].completions
end

#typedObject



33
34
35
36
37
# File 'lib/lightning/completion.rb', line 33

def typed
  # @text_typed[/\s(.+?)$/, 1] || ''
  text = @text_typed[/^(\S+)\s+(#{Lightning::TEST_FLAG})?\s*(.+?)$/, 3] || ''
  text.strip
end