123456789_123456789_123456789_123456789_123456789_

Class: Prism::NodeFind::SourceRangeFind

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Find
Instance Chain:
self, Find
Inherits: Prism::NodeFind::Find
Defined in: lib/prism/node_find.rb

Overview

Finds the AST node for a Method, UnboundMethod, Proc or Thread::Backtrace::Location using the #source_range

Instance Method Summary

Find - Inherited

#parse_file

Parse the given file path, returning a ::Prism::ParseResult or nil.

Instance Method Details

#find(callable)

Find the node for the given callable using the #source_range.

: (Method | UnboundMethod | Proc | Thread::Backtrace::Location callable) -> Node?

[ GitHub ]

  
# File 'lib/prism/node_find.rb', line 69

def find(callable)
  begin
    range = callable.source_range
  rescue ArgumentError # eval
    return
  end
  return unless range
  return unless (result = parse_file(range.absolute_path))

  start_offset = result.source.byte_offset(range.start_line, range.start_column)
  end_offset = result.source.byte_offset(range.end_line, range.end_column)
  result.value.tunnel(range.start_line, range.start_column).reverse_each do |node|
    if node.start_offset == start_offset && node.end_offset == end_offset
      return node
    end
  end
  nil
end