Class: Solargraph::SourceMap

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/source_map.rb,
lib/solargraph/source_map/clip.rb,
lib/solargraph/source_map/data.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 single Source that can be queried.

Defined Under Namespace

Classes: Clip, Completion, Data, Mapper

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ SourceMap

Returns a new instance of SourceMap.

Parameters:



30
31
32
33
34
35
36
# File 'lib/solargraph/source_map.rb', line 30

def initialize source
  @source = source

  environ.merge Convention.for_local(self) unless filename.nil?
  self.convention_pins = environ.pins
  @pin_select_cache = {}
end

Instance Attribute Details

#sourceSource (readonly)

Returns:



17
18
19
# File 'lib/solargraph/source_map.rb', line 17

def source
  @source
end

Class Method Details

.load(filename) ⇒ SourceMap

Parameters:

  • filename (String)

Returns:



155
156
157
158
# File 'lib/solargraph/source_map.rb', line 155

def load filename
  source = Solargraph::Source.load(filename)
  SourceMap.map(source)
end

.load_string(code, filename = nil) ⇒ SourceMap

Parameters:

  • code (String)
  • filename (String, nil) (defaults to: nil)

Returns:



163
164
165
166
# File 'lib/solargraph/source_map.rb', line 163

def load_string code, filename = nil
  source = Solargraph::Source.load_string(code, filename)
  SourceMap.map(source)
end

.map(source) ⇒ SourceMap

Deprecated.

Parameters:

Returns:



171
172
173
# File 'lib/solargraph/source_map.rb', line 171

def map source
  new(source)
end

Instance Method Details

#api_hashInteger

A hash representing the state of the source map’s API.

ApiMap#catalog uses this value to determine whether it needs to clear its cache.

Returns:

  • (Integer)


50
51
52
# File 'lib/solargraph/source_map.rb', line 50

def api_hash
  @api_hash ||= (pins_by_class(Pin::Constant) + pins_by_class(Pin::Namespace).select { |pin| pin.namespace.to_s > '' } + pins_by_class(Pin::Reference) + pins_by_class(Pin::Method).map(&:node) + locals).hash
end

#codeString

Returns:

  • (String)


60
61
62
# File 'lib/solargraph/source_map.rb', line 60

def code
  source.code
end

#cursor_at(position) ⇒ Source::Cursor

Parameters:

Returns:



90
91
92
# File 'lib/solargraph/source_map.rb', line 90

def cursor_at position
  Source::Cursor.new(source, position)
end

#document_symbolsArray<Pin::Base>

all pins except Solargraph::Pin::Reference::Reference

Returns:



76
77
78
79
80
# File 'lib/solargraph/source_map.rb', line 76

def document_symbols
  @document_symbols ||= (pins + convention_pins).select do |pin|
    pin.path && !pin.path.empty?
  end
end

#environEnviron

Returns:



70
71
72
# File 'lib/solargraph/source_map.rb', line 70

def environ
  @environ ||= Environ.new
end

#filenameString

Returns:

  • (String)


55
56
57
# File 'lib/solargraph/source_map.rb', line 55

def filename
  source.filename
end

#first_pin(path) ⇒ Pin::Base

Parameters:

  • path (String)

Returns:



96
97
98
# File 'lib/solargraph/source_map.rb', line 96

def first_pin path
  pins.select { |p| p.path == path }.first
end

#localsArray<Pin::LocalVariable>

Returns:



25
26
27
# File 'lib/solargraph/source_map.rb', line 25

def locals
  data.locals
end

#locals_at(location) ⇒ Array<Pin::LocalVariable>

Parameters:

Returns:



146
147
148
149
150
# File 'lib/solargraph/source_map.rb', line 146

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) ⇒ Pin::Namespace, ...

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



117
118
119
# File 'lib/solargraph/source_map.rb', line 117

def locate_block_pin line, character
  _locate_pin line, character, Pin::Namespace, Pin::Method, Pin::Block
end

#locate_named_path_pin(line, character) ⇒ Pin::Method, Pin::Namespace

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



110
111
112
# File 'lib/solargraph/source_map.rb', line 110

def locate_named_path_pin line, character
  _locate_pin line, character, Pin::Namespace, Pin::Method
end

#locate_pins(location) ⇒ Array<Solargraph::Pin::Base>

Parameters:

Returns:



102
103
104
105
# File 'lib/solargraph/source_map.rb', line 102

def locate_pins location
  # return nil unless location.start_with?("#{filename}:")
  (pins + locals).select { |pin| pin.location == location }
end

#pinsArray<Pin::Base>

Returns:



20
21
22
# File 'lib/solargraph/source_map.rb', line 20

def pins
  data.pins
end

#pins_by_class(klass) ⇒ Array<Pin::Base>

Parameters:

  • klass (Class)

Returns:



40
41
42
# File 'lib/solargraph/source_map.rb', line 40

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>

Parameters:

  • query (String)

Returns:



84
85
86
# File 'lib/solargraph/source_map.rb', line 84

def query_symbols query
  Pin::Search.new(document_symbols, query).results
end

#references(name) ⇒ Array<Location>

Parameters:

  • name (String)

Returns:



140
141
142
# File 'lib/solargraph/source_map.rb', line 140

def references name
  source.references name
end

#requiresArray<Pin::Reference::Require>

Returns:



65
66
67
# File 'lib/solargraph/source_map.rb', line 65

def requires
  pins_by_class(Pin::Reference::Require)
end

#try_merge!(other_map) ⇒ Boolean

TODO:

Candidate for deprecation

Parameters:

Returns:

  • (Boolean)


125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/solargraph/source_map.rb', line 125

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