Class: Solargraph::SourceMap::Mapper

Inherits:
Object
  • Object
show all
Includes:
Solargraph::Source::NodeMethods
Defined in:
lib/solargraph/source_map/mapper.rb

Overview

The Mapper generates pins and other data for SourceMaps.

This class is used internally by the SourceMap class. Users should not normally need to call it directly.

Constant Summary collapse

MACRO_REGEXP =
/(@\!method|@\!attribute|@\!domain|@\!macro|@\!parse)/.freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Solargraph::Source::NodeMethods

const_from, drill_signature, get_node_end_position, get_node_start_position, infer_literal_node_type, pack_name, resolve_node_signature, returns_from, unpack_name

Class Method Details

.map(source) ⇒ Array


46
47
48
49
# File 'lib/solargraph/source_map/mapper.rb', line 46

def map source
  return new.unmap(source.filename, source.code) unless source.parsed?
  new.map source
end

Instance Method Details

#closure_at(position) ⇒ Object


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

def closure_at(position)
  @pins.select{|pin| pin.is_a?(Pin::Closure) and pin.location.range.contain?(position)}.last
end

#map(source) ⇒ Array

Generate the data.


19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/solargraph/source_map/mapper.rb', line 19

def map source
  @source = source
  @filename = source.filename
  @code = source.code
  @comments = source.comments
  @pins, @locals = NodeProcessor.process(source.node, Region.new(source: source))
  process_comment_directives
  [@pins, @locals]
rescue Exception => e
  Solargraph.logger.warn "Error mapping #{source.filename}: [#{e.class}] #{e.message}"
  Solargraph.logger.warn e.backtrace.join("\n")
  [[], []]
end

#pinsArray<Solargraph::Pin::Base>


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

def pins
  @pins ||= []
end

#process_comment(source_position, comment_position, comment) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/solargraph/source_map/mapper.rb', line 61

def process_comment source_position, comment_position, comment
  return unless comment =~ MACRO_REGEXP
  cmnt = remove_inline_comment_hashes(comment)
  parse = Solargraph::Source.parse_docstring(cmnt)
  last_line = 0
  # @param d [YARD::Tags::Directive]
  parse.directives.each do |d|
    line_num = cmnt.lines[last_line..-1].find_index { |l| l.include?("@!#{d.tag.tag_name}") }
    line_num += last_line
    pos = Solargraph::Position.new(comment_position.line + line_num, comment_position.column)
    process_directive(source_position, pos, d)
    last_line = line_num + 1
  end
end

#process_comment_directivesvoid


157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/solargraph/source_map/mapper.rb', line 157

def process_comment_directives
  return unless @code =~ MACRO_REGEXP
  used = []
  @source.associated_comments.each do |line, comments|
    used.concat comments
    src_pos = Position.new(line, @code.lines[line].chomp.length)
    com_pos = Position.new(comments.first.loc.line, comments.first.loc.column)
    txt = comments.map(&:text).join("\n")
    process_comment(src_pos, com_pos, txt)
  end
  left = @comments - used
  return if left.empty?
  txt = left.map(&:text).join("\n")
  com_pos = Position.new(left.first.loc.line, left.first.loc.column)
  process_comment(com_pos, com_pos, txt)
end

#process_directive(source_position, comment_position, directive) ⇒ Object


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/solargraph/source_map/mapper.rb', line 78

def process_directive source_position, comment_position, directive
  docstring = Solargraph::Source.parse_docstring(directive.tag.text).to_docstring
  location = Location.new(@filename, Range.new(comment_position, comment_position))
  case directive.tag.tag_name
  when 'method'
    namespace = closure_at(source_position)
    if namespace.location.range.start.line < comment_position.line
      namespace = closure_at(comment_position)
    end
    region = Region.new(source: @source, closure: namespace)
    src_node = Solargraph::Source.parse("def #{directive.tag.name};end", @filename, location.range.start.line)
    gen_pin = Solargraph::SourceMap::NodeProcessor.process(src_node, region).first.last
    return if gen_pin.nil?
    # @todo: Smelly instance variable access
    gen_pin.instance_variable_set(:@comments, docstring.all)
    @pins.push gen_pin
  when 'attribute'
    return if directive.tag.name.nil?
    namespace = closure_at(source_position)
    t = (directive.tag.types.nil? || directive.tag.types.empty?) ? nil : directive.tag.types.flatten.join('')
    if t.nil? || t.include?('r')
      pins.push Solargraph::Pin::Attribute.new(
        location: location,
        closure: namespace,
        name: directive.tag.name,
        comments: docstring.all,
        access: :reader,
        scope: namespace.is_a?(Pin::Singleton) ? :class : :instance,
        visibility: :public
      )
    end
    if t.nil? || t.include?('w')
      pins.push Solargraph::Pin::Attribute.new(
        location: location,
        closure: namespace,
        name: "#{directive.tag.name}=",
        comments: docstring.all,
        access: :writer,
        scope: namespace.is_a?(Pin::Singleton) ? :class : :instance,
        visibility: :public
      )
    end
  when 'parse'
    ns = closure_at(source_position)
    region = Region.new(source: @source, closure: ns)
    begin
      node = Solargraph::Source.parse(directive.tag.text, @filename, comment_position.line)
      NodeProcessor.process(node, region, @pins)
    rescue Parser::SyntaxError => e
      # @todo Handle parser errors in !parse directives
    end
  when 'domain'
    namespace = closure_at(source_position)
    namespace.domains.concat directive.tag.types unless directive.tag.types.nil?
  when 'override'
    pins.push Pin::Reference::Override.new(location, directive.tag.name, docstring.tags)
  end
end

#remove_inline_comment_hashes(comment) ⇒ Object


137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/solargraph/source_map/mapper.rb', line 137

def remove_inline_comment_hashes comment
  ctxt = ''
  num = nil
  started = false
  comment.lines.each { |l|
    # Trim the comment and minimum leading whitespace
    p = l.gsub(/^#/, '')
    if num.nil? && !p.strip.empty?
      num = p.index(/[^ ]/)
      started = true
    elsif started && !p.strip.empty?
      cur = p.index(/[^ ]/)
      num = cur if cur < num
    end
    ctxt += "#{p[num..-1]}\n" if started
  }
  ctxt
end

#unmap(filename, code) ⇒ Array


36
37
38
39
40
41
# File 'lib/solargraph/source_map/mapper.rb', line 36

def unmap filename, code
  s = Position.new(0, 0)
  e = Position.from_offset(code, code.length)
  location = Location.new(filename, Range.new(s, e))
  [[Pin::Namespace.new(location: location, name: '')], []]
end