Class: Decode::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(paths) ⇒ Index

Returns a new instance of Index.



26
27
28
29
30
31
32
33
# File 'lib/decode/index.rb', line 26

def initialize(paths)
  @paths = paths
  @sources = {}
  @symbols = {}
  
  # This is essentially a prefix tree:
  @trie = Trie.new
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



35
36
37
# File 'lib/decode/index.rb', line 35

def paths
  @paths
end

#sourcesObject (readonly)

Returns the value of attribute sources.



36
37
38
# File 'lib/decode/index.rb', line 36

def sources
  @sources
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



37
38
39
# File 'lib/decode/index.rb', line 37

def symbols
  @symbols
end

#trieObject (readonly)

Returns the value of attribute trie.



39
40
41
# File 'lib/decode/index.rb', line 39

def trie
  @trie
end

Instance Method Details

#lookup(reference, relative_to: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/decode/index.rb', line 54

def lookup(reference, relative_to: nil)
  if reference.absolute? || relative_to.nil?
    lexical_path = []
  else
    lexical_path = relative_to.lexical_path
  end
  
  path = reference.path
  
  while true
    node = @trie.match(lexical_path)
    
    if node.children[path.first]
      if target = node.lookup(path)
        if reference.kind
          return target.values.select{|symbol| symbol.kind == reference.kind}
        else
          return target.values
        end
      else
        return nil
      end
    end
    
    break if lexical_path.empty?
    lexical_path.pop
  end
end

#update!Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/decode/index.rb', line 41

def update!
  @paths.each do |path|
    source = Source.new(path)
    @sources[path.relative_path] = Source.new(path)
    
    source.parse do |definition|
      @symbols[definition.qualified_name] = definition
      
      @trie.insert(definition.lexical_path, definition)
    end
  end
end