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:



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/solargraph/source_map.rb', line 35

def initialize source
  @source = source
  # @type [Array<Pin::Base>, nil]
  @convention_pins = nil

  conventions_environ.merge Convention.for_local(self) unless filename.nil?
  # FIXME: unmemoizing the document_symbols in case it was called and memoized from any of conventions above
  # this is to ensure that the convention_pins from all conventions are used in the document_symbols.
  # solargraph-rails is known to use this method to get the document symbols. It should probably be removed.
  @document_symbols = nil
  self.convention_pins = conventions_environ.pins
  # @type [Hash{Class<Pin::Base> => Array<Pin::Base>}]
  @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:



158
159
160
161
# File 'lib/solargraph/source_map.rb', line 158

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:



166
167
168
169
# File 'lib/solargraph/source_map.rb', line 166

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

.map(source) ⇒ SourceMap

Deprecated.

Parameters:

Returns:



174
175
176
# File 'lib/solargraph/source_map.rb', line 174

def map source
  new(source)
end

Instance Method Details

#all_pinsArray<Pin::Base>

Returns:



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

def all_pins
  pins + convention_pins
end

#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)


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

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

#codeString

Returns:

  • (String)


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

def code
  source.code
end

#conventions_environEnviron

Returns:



86
87
88
# File 'lib/solargraph/source_map.rb', line 86

def conventions_environ
  @conventions_environ ||= Environ.new
end

#cursor_at(position) ⇒ Source::Cursor

Parameters:

  • position (Position, Array(Integer, Integer))

Returns:



107
108
109
# File 'lib/solargraph/source_map.rb', line 107

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

#document_symbolsArray<Pin::Base>

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

Returns:



93
94
95
96
97
# File 'lib/solargraph/source_map.rb', line 93

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

#filenameString?

Returns:

  • (String, nil)


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

def filename
  source.filename
end

#first_pin(path) ⇒ Pin::Base

Parameters:

  • path (String)

Returns:



113
114
115
# File 'lib/solargraph/source_map.rb', line 113

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

#localsArray<Pin::LocalVariable>

Returns:



30
31
32
# File 'lib/solargraph/source_map.rb', line 30

def locals
  data.locals
end

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

Parameters:

Returns:



149
150
151
152
153
# File 'lib/solargraph/source_map.rb', line 149

def locals_at location
  return [] if location.filename != filename
  closure = locate_closure_pin(location.range.start.line, location.range.start.character)
  locals.select { |pin| pin.visible_at?(closure, location) }
end

#locate_closure_pin(line, character) ⇒ Pin::Closure Also known as: locate_block_pin

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



134
135
136
# File 'lib/solargraph/source_map.rb', line 134

def locate_closure_pin line, character
  _locate_pin line, character, Pin::Closure
end

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

Parameters:

  • line (Integer)
  • character (Integer)

Returns:



127
128
129
# File 'lib/solargraph/source_map.rb', line 127

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:



119
120
121
122
# File 'lib/solargraph/source_map.rb', line 119

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<generic<T>>

Parameters:

  • klass (Class<generic<T>>)

Returns:

  • (Array<generic<T>>)


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

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:



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

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

#references(name) ⇒ Array<Location>

Parameters:

  • name (String)

Returns:



143
144
145
# File 'lib/solargraph/source_map.rb', line 143

def references name
  source.references name
end

#requiresArray<Pin::Reference::Require>

Returns:



81
82
83
# File 'lib/solargraph/source_map.rb', line 81

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