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.



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

def initialize source, pins, locals
  # HACK: Keep the library from changing this
  @source = source.dup
  @pins = pins
  @locals = locals
end

Instance Attribute Details

#localsArray<Pin::Base> (readonly)

Returns:



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

def locals
  @locals
end

#pinsArray<Pin::Base> (readonly)

Returns:



13
14
15
# File 'lib/solargraph/source_map.rb', line 13

def pins
  @pins
end

#sourceSource (readonly)

Returns:



10
11
12
# File 'lib/solargraph/source_map.rb', line 10

def source
  @source
end

Class Method Details

.load(filename) ⇒ SourceMap

Returns:



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

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

.load_string(code, filename = nil) ⇒ SourceMap

Returns:



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

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

.map(source) ⇒ SourceMap

Parameters:

Returns:



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

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

Instance Method Details

#codeObject



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

def code
  source.code
end

#comment_at?(position) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

def comment_at? position
  @source.comment_at?(position)
end

#cursor_at(position) ⇒ Solargraph::SourceMap::Fragment

Parameters:

Returns:

  • (Solargraph::SourceMap::Fragment)


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

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

#document_symbolsObject



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

def document_symbols
  @document_symbols ||= pins.select { |pin|
    [Pin::ATTRIBUTE, Pin::CONSTANT, Pin::METHOD, Pin::NAMESPACE].include?(pin.kind) and !pin.path.empty?
  }
end

#filenameObject



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

def filename
  source.filename
end

#first_pin(path) ⇒ Object



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

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

#locate_block_pin(line, character) ⇒ Object



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

def locate_block_pin line, character
  _locate_pin line, character, Pin::NAMESPACE, Pin::METHOD, Pin::BLOCK
end

#locate_named_path_pin(line, character) ⇒ Object



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

def locate_named_path_pin line, character
  _locate_pin line, character, Pin::NAMESPACE, Pin::METHOD
end

#locate_pin(location) ⇒ Solargraph::Pin::Base

Parameters:

Returns:



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

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

#query_symbols(query) ⇒ Object



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

def query_symbols query
  document_symbols.select{|pin| pin.path.include?(query)}
end

#references(name) ⇒ Array<Location>

Parameters:

  • name (String)

Returns:



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

def references name
  source.references name
end

#requiresObject



33
34
35
# File 'lib/solargraph/source_map.rb', line 33

def requires
  @requires ||= pins.select{|p| p.kind == Pin::REQUIRE_REFERENCE}
end

#string_at?(position) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


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

def string_at? position
  @source.string_at?(position)
end

#try_merge!(other_map) ⇒ Object

Parameters:



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/solargraph/source_map.rb', line 85

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