2012-06-22 11:33:12 -04:00
|
|
|
require 'pry/helpers/documentation_helpers'
|
|
|
|
require 'forwardable'
|
|
|
|
|
|
|
|
class Pry
|
|
|
|
class WrappedModule
|
|
|
|
|
|
|
|
# This class represents a single candidate for a module/class definition.
|
|
|
|
# It provides access to the source, documentation, line and file
|
2012-06-23 04:14:10 -04:00
|
|
|
# for a monkeypatch (reopening) of a class/module.
|
2012-06-22 11:33:12 -04:00
|
|
|
class Candidate
|
2012-12-21 10:36:17 -05:00
|
|
|
include Pry::Helpers::DocumentationHelpers
|
2012-12-25 07:47:33 -05:00
|
|
|
include Pry::CodeObject::Helpers
|
2012-06-22 11:33:12 -04:00
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
# @return [String] The file where the module definition is located.
|
|
|
|
attr_reader :file
|
2012-12-20 14:13:19 -05:00
|
|
|
alias_method :source_file, :file
|
2012-06-22 11:33:12 -04:00
|
|
|
|
|
|
|
# @return [Fixnum] The line where the module definition is located.
|
|
|
|
attr_reader :line
|
2012-12-20 14:13:19 -05:00
|
|
|
alias_method :source_line, :line
|
2012-06-22 11:33:12 -04:00
|
|
|
|
2013-02-07 09:36:50 -05:00
|
|
|
# Methods to delegate to associated `Pry::WrappedModule
|
|
|
|
# instance`.
|
|
|
|
private_delegates = [:lines_for_file, :method_candidates,
|
2013-02-10 12:46:55 -05:00
|
|
|
:yard_docs?]
|
2012-06-22 11:33:12 -04:00
|
|
|
|
2013-02-10 12:46:55 -05:00
|
|
|
public_delegates = [:wrapped, :module?, :class?, :name, :nonblank_name,
|
|
|
|
:number_of_candidates]
|
2013-02-07 09:36:50 -05:00
|
|
|
|
|
|
|
def_delegators :@wrapper, *(private_delegates + public_delegates)
|
2013-03-02 19:01:55 -05:00
|
|
|
private(*private_delegates)
|
|
|
|
public(*public_delegates)
|
2012-06-22 11:33:12 -04:00
|
|
|
|
2012-06-23 04:14:10 -04:00
|
|
|
# @raise [Pry::CommandError] If `rank` is out of bounds.
|
2012-06-22 11:33:12 -04:00
|
|
|
# @param [Pry::WrappedModule] wrapper The associated
|
|
|
|
# `Pry::WrappedModule` instance that owns the candidates.
|
|
|
|
# @param [Fixnum] rank The rank of the candidate to
|
|
|
|
# retrieve. Passing 0 returns 'primary candidate' (the candidate with largest
|
|
|
|
# number of methods), passing 1 retrieves candidate with
|
|
|
|
# second largest number of methods, and so on, up to
|
|
|
|
# `Pry::WrappedModule#number_of_candidates() - 1`
|
|
|
|
def initialize(wrapper, rank)
|
|
|
|
@wrapper = wrapper
|
|
|
|
|
2012-06-23 04:14:10 -04:00
|
|
|
if number_of_candidates <= 0
|
|
|
|
raise CommandError, "Cannot find a definition for #{name} module!"
|
|
|
|
elsif rank > (number_of_candidates - 1)
|
2012-06-22 11:33:12 -04:00
|
|
|
raise CommandError, "No such module candidate. Allowed candidates range is from 0 to #{number_of_candidates - 1}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@rank = rank
|
|
|
|
@file, @line = source_location
|
|
|
|
end
|
|
|
|
|
|
|
|
# @raise [Pry::CommandError] If source code cannot be found.
|
|
|
|
# @return [String] The source for the candidate, i.e the
|
|
|
|
# complete module/class definition.
|
|
|
|
def source
|
2013-01-28 19:05:50 -05:00
|
|
|
return nil if file.nil?
|
2012-06-22 11:33:12 -04:00
|
|
|
return @source if @source
|
|
|
|
|
2012-07-01 11:33:13 -04:00
|
|
|
@source = strip_leading_whitespace(Pry::Code.from_file(file).expression_at(line, number_of_lines_in_first_chunk))
|
2012-06-22 11:33:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @raise [Pry::CommandError] If documentation cannot be found.
|
|
|
|
# @return [String] The documentation for the candidate.
|
2012-07-01 11:33:13 -04:00
|
|
|
def doc
|
2013-01-28 19:05:50 -05:00
|
|
|
return nil if file.nil?
|
2012-06-22 11:33:12 -04:00
|
|
|
return @doc if @doc
|
|
|
|
|
2013-02-01 04:24:01 -05:00
|
|
|
@doc = get_comment_content(Pry::Code.from_file(file).comment_describing(line))
|
2012-06-22 11:33:12 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Array, nil] A `[String, Fixnum]` pair representing the
|
|
|
|
# source location (file and line) for the candidate or `nil`
|
|
|
|
# if no source location found.
|
|
|
|
def source_location
|
|
|
|
return @source_location if @source_location
|
|
|
|
|
2012-07-01 11:33:13 -04:00
|
|
|
file, line = first_method_source_location
|
2012-06-22 11:33:12 -04:00
|
|
|
return nil if !file.is_a?(String)
|
|
|
|
|
2013-01-02 19:47:22 -05:00
|
|
|
@source_location = [file, first_line_of_module_definition(file, line)]
|
2012-06-22 11:33:12 -04:00
|
|
|
rescue Pry::RescuableException
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-02 19:47:22 -05:00
|
|
|
# Locate the first line of the module definition.
|
|
|
|
# @param [String] file The file that contains the module
|
|
|
|
# definition (somewhere).
|
|
|
|
# @param [Fixnum] line The module definition should appear
|
|
|
|
# before this line (if it exists).
|
|
|
|
# @return [Fixnum] The line where the module is defined. This
|
|
|
|
# line number is one-indexed.
|
|
|
|
def first_line_of_module_definition(file, line)
|
|
|
|
searchable_lines = lines_for_file(file)[0..(line - 2)]
|
|
|
|
searchable_lines.rindex { |v| class_regexes.any? { |r| r =~ v } } + 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def class_regexes
|
|
|
|
mod_type_string = wrapped.class.to_s.downcase
|
|
|
|
[/^\s*#{mod_type_string}\s+(?:(?:\w*)::)*?#{wrapped.name.split(/::/).last}/,
|
|
|
|
/^\s*(::)?#{wrapped.name.split(/::/).last}\s*?=\s*?#{wrapped.class}/,
|
|
|
|
/^\s*(::)?#{wrapped.name.split(/::/).last}\.(class|instance)_eval/]
|
|
|
|
end
|
|
|
|
|
2012-06-22 11:33:12 -04:00
|
|
|
# This method is used by `Candidate#source_location` as a
|
|
|
|
# starting point for the search for the candidate's definition.
|
|
|
|
# @return [Array] The source location of the base method used to
|
|
|
|
# calculate the source location of the candidate.
|
2012-07-01 11:33:13 -04:00
|
|
|
def first_method_source_location
|
2013-05-10 03:42:55 -04:00
|
|
|
@first_method_source_location ||= method_candidates[@rank].first.source_location
|
2012-07-01 11:33:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Array] The source location of the last method in this
|
|
|
|
# candidate's module definition.
|
|
|
|
def last_method_source_location
|
2013-05-10 03:42:55 -04:00
|
|
|
@end_method_source_location ||= method_candidates[@rank].last.source_location
|
2012-06-28 13:24:18 -04:00
|
|
|
end
|
|
|
|
|
2012-07-01 11:33:13 -04:00
|
|
|
# Return the number of lines between the start of the class definition
|
|
|
|
# and the start of the last method. We use this value so we can
|
|
|
|
# quickly grab these lines from the file (without having to
|
|
|
|
# check each intervening line for validity, which is expensive) speeding up source extraction.
|
|
|
|
# @return [Fixum] Number of lines.
|
|
|
|
def number_of_lines_in_first_chunk
|
|
|
|
end_method_line = last_method_source_location.last
|
|
|
|
|
|
|
|
end_method_line - line
|
2012-06-28 13:24:18 -04:00
|
|
|
end
|
2012-06-22 11:33:12 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|