Class: Decode::Index
- Inherits:
-
Object
- Object
- Decode::Index
- Defined in:
- lib/decode/index.rb
Overview
A list of definitions organised for quick lookup and lexical enumeration.
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
All definitions which have been parsed.
-
#languages ⇒ Object
All supported languages for this index.
-
#sources ⇒ Object
readonly
All source files that have been parsed.
-
#trie ⇒ Object
readonly
A (prefix) trie of lexically scoped definitions.
Instance Method Summary collapse
-
#initialize(languages = Languages.all) ⇒ Index
constructor
Initialize an empty index.
- #inspect ⇒ Object (also: #to_s)
-
#lookup(reference, relative_to: nil) ⇒ Object
Lookup the specified reference and return matching definitions.
-
#update(paths) ⇒ Object
Updates the index by parsing the specified files.
Constructor Details
Instance Attribute Details
#definitions ⇒ Object (readonly)
All definitions which have been parsed.
41 42 43 |
# File 'lib/decode/index.rb', line 41 def definitions @definitions end |
#languages ⇒ Object
All supported languages for this index.
33 34 35 |
# File 'lib/decode/index.rb', line 33 def languages @languages end |
#sources ⇒ Object (readonly)
All source files that have been parsed.
37 38 39 |
# File 'lib/decode/index.rb', line 37 def sources @sources end |
#trie ⇒ Object (readonly)
A (prefix) trie of lexically scoped definitions.
45 46 47 |
# File 'lib/decode/index.rb', line 45 def trie @trie end |
Instance Method Details
#inspect ⇒ Object Also known as: to_s
25 26 27 |
# File 'lib/decode/index.rb', line 25 def inspect "#<#{self.class} #{@definitions.size} definition(s)>" end |
#lookup(reference, relative_to: nil) ⇒ Object
Lookup the specified reference and return matching definitions.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/decode/index.rb', line 70 def lookup(reference, relative_to: nil) if reference.absolute? || relative_to.nil? lexical_path = [] else lexical_path = relative_to.path.dup end path = reference.path while true node = @trie.lookup(lexical_path) if node.children[path.first] if target = node.lookup(path) return reference.best(target.values) else return nil end end break if lexical_path.empty? lexical_path.pop end end |
#update(paths) ⇒ Object
Updates the index by parsing the specified files. All extracted definitions are merged into the existing index.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/decode/index.rb', line 51 def update(paths) paths.each do |path| if source = @languages.source_for(path) @sources[path] = source source.definitions do |symbol| # $stderr.puts "Adding #{symbol.qualified_name} to #{symbol.lexical_path.join(' -> ')}" @definitions[symbol.qualified_name] = symbol @trie.insert(symbol.path, symbol) end end end end |