Module: Solargraph::YardMap::Directives::MethodDirective

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

Class Method Summary collapse

Class Method Details

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

Parameters:

Returns:



45
46
47
# File 'lib/solargraph/yard_map/directives/method_directive.rb', line 45

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

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
# File 'lib/solargraph/yard_map/directives/method_directive.rb', line 15

def process_directive source, pins, source_position, comment_position, directive
  namespace = closure_at(pins, source_position) || pins.first

  namespace = closure_at(pins, comment_position) if namespace.location&.range&.start&.line&.< comment_position.line # rubocop:disable Style/SafeNavigationChainLength
  begin
    src = Solargraph::Source.load_string("def #{directive.tag.name};end", source.filename)
    region = Parser::Region.new(source: src, closure: namespace)
    method_gen_pins = Parser.process_node(src.node, region).first.select { |pin| pin.is_a?(Pin::Method) }
    gen_pin = method_gen_pins.last
    return [] if gen_pin.nil?
    # Move the location to the end of the line so it gets recognized
    # as originating from a comment
    shifted = Solargraph::Position.new(comment_position.line,
                                       source.code.lines[comment_position.line].to_s.chomp.length)
    comments = Solargraph::Source.parse_docstring(directive.tag.text.to_s).to_docstring.all.to_s
    # @todo: Smelly instance variable access
    gen_pin.instance_variable_set(:@comments, comments)
    gen_pin.instance_variable_set(:@location,
                                  Solargraph::Location.new(source.filename, Range.new(shifted, shifted)))
    gen_pin.instance_variable_set(:@explicit, false)
    [gen_pin]
  rescue Parser::SyntaxError
    # @todo Handle error in directive
    []
  end
end