Module: Solargraph::YardMap::Directives::ParseDirective

Defined in:
lib/solargraph/yard_map/directives/parse_directive.rb

Class Method Summary collapse

Class Method Details

.closure_at(pins, position) ⇒ Pin::Closure

Parameters:

Returns:



47
48
49
# File 'lib/solargraph/yard_map/directives/parse_directive.rb', line 47

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

.process_directive(source, pins, source_position, comment_position, directive) ⇒ Array<Solargraph::Pin::Base>

Parameters:

Returns:



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/solargraph/yard_map/directives/parse_directive.rb', line 15

def process_directive source, pins, source_position, comment_position, directive
  ns = closure_at(pins, source_position)
  pins_copy = pins.dup
  src = Solargraph::Source.load_string(directive.tag.text.to_s, source.filename)
  region = Parser::Region.new(source: src, closure: ns)
  # @todo These pins may need to be marked not explicit
  old_pins_index = pins.length
  loff = if source.code.lines[comment_position.line].strip.end_with?('@!parse')
           comment_position.line + 1
         else
           comment_position.line
         end
  Parser.process_node(src.node, region, pins_copy)
  new_pins = pins_copy[old_pins_index..] || []
  new_pins.each do |p|
    # @todo Smelly instance variable access
    next if p.location.nil?
    # @sg-ignore Unresolved call to range on Solargraph::Location, nil - does not account for next clause above.
    p.location.range.start.instance_variable_set(:@line, p.location.range.start.line + loff)
    # @sg-ignore Unresolved call to range on Solargraph::Location, nil
    p.location.range.ending.instance_variable_set(:@line, p.location.range.ending.line + loff)
  end

  new_pins
rescue Parser::SyntaxError
  # @todo Handle parser errors in !parse directives
  []
end