Class: Muby::Completer

Inherits:
Object
  • Object
show all
Defined in:
lib/muby/completer.rb

Constant Summary collapse

@@instance =
Muby::Completer.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCompleter

Returns a new instance of Completer.



7
8
9
# File 'lib/muby/completer.rb', line 7

def initialize
  @completions = {}
end

Instance Attribute Details

#completionsObject (readonly)

Returns the value of attribute completions.



5
6
7
# File 'lib/muby/completer.rb', line 5

def completions
  @completions
end

Class Method Details

.get_instanceObject



13
14
15
# File 'lib/muby/completer.rb', line 13

def self.get_instance
  @@instance
end

Instance Method Details

#complete(s) ⇒ Object



17
18
19
20
# File 'lib/muby/completer.rb', line 17

def complete(s)
  subhash = find_subhash(s, @completions) || {}
  find_endings(s, subhash)
end

#find_endings(s, hash) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/muby/completer.rb', line 28

def find_endings(s, hash)
  return_value = []
  hash.each do |char, value|
    if char == :end
      return_value << s
    else
      return_value |= find_endings(s + char, value)
    end
  end
  return_value
end

#find_subhash(s, hash) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/muby/completer.rb', line 40

def find_subhash(s, hash)
  return {} if hash.nil?
  if s.size == 0
    hash
  else
	rval = find_subhash(s[1..-1], hash[s[0..0]])
	rval.merge(find_subhash(s[1..-1], hash[s[0..0].swapcase]))
  end
end

#store(s) ⇒ Object



22
23
24
25
26
# File 'lib/muby/completer.rb', line 22

def store(s)
  s.split(/\W+/).each do |part|
    store_in_hash(part, @completions) unless part.empty?
  end
end

#store_in_hash(s, hash) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/muby/completer.rb', line 50

def store_in_hash(s, hash)
  if s.size == 1
    hash[s] ||= {}
    hash[s][:end] = true
  else
    hash[s[0..0]] ||= {}
    store_in_hash(s[1..-1], hash[s[0..0]])
  end
end