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/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.

Parameters:



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

def initialize source, pins, locals
  # HACK: Keep the library from changing this
  @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

#localsArray<Pin::Base> (readonly)

Returns:



23
24
25
# File 'lib/solargraph/source_map.rb', line 23

def locals
  @locals
end

#pinsArray<Pin::Base> (readonly)

Returns:



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

def pins
  @pins
end

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



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

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:



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

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

.map(source) ⇒ SourceMap

Parameters:

Returns:



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

def map source
  result = SourceMap::Mapper.map(source)
  new(source, *result)
end

Instance Method Details

#codeString

Returns:

  • (String)


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

def code
  source.code
end

#cursor_at(position) ⇒ Source::Cursor

Parameters:

Returns:



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

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

#document_symbolsArray<Pin::Base>

Returns:



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

def document_symbols
  @document_symbols ||= pins.select { |pin|
    pin.path && !pin.path.empty?
  }
end

#environEnviron

Returns:



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

def environ
  @environ ||= Environ.new
end

#filenameString

Returns:

  • (String)


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

def filename
  source.filename
end

#first_pin(path) ⇒ Pin::Base

Parameters:

  • path (String)

Returns:



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

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

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

Parameters:

Returns:



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

def locals_at(location)
  return [] if location.filename != filename
  locals.select { |pin| pin.visible_at?(location) }
end

#locate_block_pin(line, character) ⇒ Object



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

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

#locate_named_path_pin(line, character) ⇒ Object



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

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:



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

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

#pins_by_class(klass) ⇒ Object



38
39
40
# File 'lib/solargraph/source_map.rb', line 38

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:



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

def query_symbols query
  return document_symbols if query && query.empty?
  document_symbols.select{ |pin| fuzzy_string_match(pin.path, query) || fuzzy_string_match(pin.name, query) }
end

#rebindable_method_namesObject



42
43
44
45
46
47
# File 'lib/solargraph/source_map.rb', line 42

def rebindable_method_names
  @rebindable_method_names ||= pins_by_class(Pin::Method)
    .select { |pin| pin.comments && pin.comments.include?('@yieldself') }
    .map(&:name)
    .to_set
end

#references(name) ⇒ Array<Location>

Parameters:

  • name (String)

Returns:



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

def references name
  source.references name
end

#requiresArray<Pin::Reference::Require>



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

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

#try_merge!(other_map) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/solargraph/source_map.rb', line 112

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