Class: Solargraph::SourceMap
- Inherits:
-
Object
- Object
- Solargraph::SourceMap
show all
- Defined in:
- lib/solargraph/source_map.rb,
lib/solargraph/source_map/clip.rb,
lib/solargraph/source_map/mapper.rb,
lib/solargraph/source_map/completion.rb
Overview
An index of pins and other ApiMap-related data for a Source.
Defined Under Namespace
Classes: Clip, Completion, Mapper
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(source, pins, locals) ⇒ SourceMap
Returns a new instance of SourceMap.
27
28
29
30
31
32
33
34
35
|
# File 'lib/solargraph/source_map.rb', line 27
def initialize source, pins, locals
@source = source.dup
@pins = pins
@locals = locals
environ.merge Convention.for_local(self) unless filename.nil?
@pin_class_hash = pins.to_set.classify(&:class).transform_values(&:to_a)
@pin_select_cache = {}
end
|
Instance Attribute Details
22
23
24
|
# File 'lib/solargraph/source_map.rb', line 22
def locals
@locals
end
|
19
20
21
|
# File 'lib/solargraph/source_map.rb', line 19
def pins
@pins
end
|
16
17
18
|
# File 'lib/solargraph/source_map.rb', line 16
def source
@source
end
|
Class Method Details
.load_string(code, filename = nil) ⇒ SourceMap
154
155
156
157
|
# File 'lib/solargraph/source_map.rb', line 154
def map source
result = SourceMap::Mapper.map(source)
new(source, *result)
end
|
Instance Method Details
#code ⇒ String
54
55
56
|
# File 'lib/solargraph/source_map.rb', line 54
def code
source.code
end
|
83
84
85
|
# File 'lib/solargraph/source_map.rb', line 83
def cursor_at position
Source::Cursor.new(source, position)
end
|
#document_symbols ⇒ Array<Pin::Base>
69
70
71
72
73
|
# File 'lib/solargraph/source_map.rb', line 69
def document_symbols
@document_symbols ||= pins.select { |pin|
pin.path && !pin.path.empty?
}
end
|
64
65
66
|
# File 'lib/solargraph/source_map.rb', line 64
def environ
@environ ||= Environ.new
end
|
#filename ⇒ String
49
50
51
|
# File 'lib/solargraph/source_map.rb', line 49
def filename
source.filename
end
|
#first_pin(path) ⇒ Pin::Base
89
90
91
|
# File 'lib/solargraph/source_map.rb', line 89
def first_pin path
pins.select { |p| p.path == path }.first
end
|
130
131
132
133
134
|
# File 'lib/solargraph/source_map.rb', line 130
def locals_at(location)
return [] if location.filename != filename
closure = locate_named_path_pin(location.range.start.line, location.range.start.character)
locals.select { |pin| pin.visible_at?(closure, location) }
end
|
#locate_block_pin(line, character) ⇒ Object
104
105
106
|
# File 'lib/solargraph/source_map.rb', line 104
def locate_block_pin line, character
_locate_pin line, character, Pin::Namespace, Pin::Method, Pin::Block
end
|
#locate_named_path_pin(line, character) ⇒ Object
100
101
102
|
# File 'lib/solargraph/source_map.rb', line 100
def locate_named_path_pin line, character
_locate_pin line, character, Pin::Namespace, Pin::Method
end
|
95
96
97
98
|
# File 'lib/solargraph/source_map.rb', line 95
def locate_pins location
(pins + locals).select { |pin| pin.location == location }
end
|
#pins_by_class(klass) ⇒ Object
37
38
39
|
# File 'lib/solargraph/source_map.rb', line 37
def pins_by_class klass
@pin_select_cache[klass] ||= @pin_class_hash.select { |key, _| key <= klass }.values.flatten
end
|
#query_symbols(query) ⇒ Array<Pin::Base>
77
78
79
|
# File 'lib/solargraph/source_map.rb', line 77
def query_symbols query
Pin::Search.new(document_symbols, query).results
end
|
#rebindable_method_names ⇒ Object
41
42
43
44
45
46
|
# File 'lib/solargraph/source_map.rb', line 41
def rebindable_method_names
@rebindable_method_names ||= pins_by_class(Pin::Method)
.select { |pin| pin. && pin..include?('@yieldself') }
.map(&:name)
.to_set
end
|
#references(name) ⇒ Array<Location>
124
125
126
|
# File 'lib/solargraph/source_map.rb', line 124
def references name
source.references name
end
|
59
60
61
|
# File 'lib/solargraph/source_map.rb', line 59
def requires
pins_by_class(Pin::Reference::Require)
end
|
#try_merge!(other_map) ⇒ Boolean
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/solargraph/source_map.rb', line 110
def try_merge! other_map
return false if pins.length != other_map.pins.length || locals.length != other_map.locals.length || requires.map(&:name).uniq.sort != other_map.requires.map(&:name).uniq.sort
pins.each_index do |i|
return false unless pins[i].try_merge!(other_map.pins[i])
end
locals.each_index do |i|
return false unless locals[i].try_merge!(other_map.locals[i])
end
@source = other_map.source
true
end
|