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/region.rb,
lib/solargraph/source_map/completion.rb,
lib/solargraph/source_map/node_processor.rb,
lib/solargraph/source_map/node_processor/base.rb,
lib/solargraph/source_map/node_processor/def_node.rb,
lib/solargraph/source_map/node_processor/sym_node.rb,
lib/solargraph/source_map/node_processor/args_node.rb,
lib/solargraph/source_map/node_processor/defs_node.rb,
lib/solargraph/source_map/node_processor/send_node.rb,
lib/solargraph/source_map/node_processor/alias_node.rb,
lib/solargraph/source_map/node_processor/begin_node.rb,
lib/solargraph/source_map/node_processor/block_node.rb,
lib/solargraph/source_map/node_processor/casgn_node.rb,
lib/solargraph/source_map/node_processor/cvasgn_node.rb,
lib/solargraph/source_map/node_processor/gvasgn_node.rb,
lib/solargraph/source_map/node_processor/ivasgn_node.rb,
lib/solargraph/source_map/node_processor/lvasgn_node.rb,
lib/solargraph/source_map/node_processor/orasgn_node.rb,
lib/solargraph/source_map/node_processor/sclass_node.rb,
lib/solargraph/source_map/node_processor/resbody_node.rb,
lib/solargraph/source_map/node_processor/namespace_node.rb

Overview

An index of pins and other ApiMap-related data for a Source.

Defined Under Namespace

Modules: NodeProcessor Classes: Clip, Completion, Mapper, Region

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:


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

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:


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

def locals
  @locals
end

#pinsArray<Pin::Base> (readonly)

Returns:


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

def pins
  @pins
end

#sourceSource (readonly)

Returns:


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

def source
  @source
end

Class Method Details

.load(filename) ⇒ SourceMap

Parameters:

  • filename (String)

Returns:


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

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:


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

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

.map(source) ⇒ SourceMap

Parameters:

Returns:


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

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

Instance Method Details

#codeString

Returns:

  • (String)

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

def code
  source.code
end

#cursor_at(position) ⇒ Source::Cursor

Parameters:

Returns:


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

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

#document_symbolsArray<Pin::Base>

Returns:


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

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

#filenameString

Returns:

  • (String)

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

def filename
  source.filename
end

#first_pin(path) ⇒ Pin::Base

Parameters:

  • path (String)

Returns:


68
69
70
# File 'lib/solargraph/source_map.rb', line 68

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

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

Parameters:

Returns:


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

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

#locate_block_pin(line, character) ⇒ Object


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

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

#locate_named_path_pin(line, character) ⇒ Object


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

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:


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

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

#query_symbols(query) ⇒ Array<Pin::Base>

Parameters:

  • query (String)

Returns:


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

def query_symbols query
  document_symbols.select{ |pin| fuzzy_string_match(pin.path, query) || fuzzy_string_match(pin.name, query) }
end

#references(name) ⇒ Array<Location>

Parameters:

  • name (String)

Returns:


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

def references name
  source.references name
end

#requiresArray<Pin::Reference::Require>

Returns:


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

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

#try_merge!(other_map) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

89
90
91
92
93
94
95
96
97
98
99
# File 'lib/solargraph/source_map.rb', line 89

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