2013-01-05 07:32:12 -05:00
|
|
|
require 'pry/code/loc'
|
|
|
|
require 'pry/code/code_range'
|
2013-11-23 16:01:40 -05:00
|
|
|
require 'pry/code/code_file'
|
2013-01-05 07:32:12 -05:00
|
|
|
|
2011-11-26 00:05:10 -05:00
|
|
|
class Pry
|
2012-01-08 01:01:15 -05:00
|
|
|
class << self
|
2012-01-14 23:07:16 -05:00
|
|
|
# Convert the given object into an instance of `Pry::Code`, if it isn't
|
|
|
|
# already one.
|
|
|
|
#
|
|
|
|
# @param [Code, Method, UnboundMethod, Proc, Pry::Method, String, Array,
|
|
|
|
# IO] obj
|
2012-01-08 01:01:15 -05:00
|
|
|
def Code(obj)
|
|
|
|
case obj
|
|
|
|
when Code
|
|
|
|
obj
|
2012-01-08 02:56:42 -05:00
|
|
|
when ::Method, UnboundMethod, Proc, Pry::Method
|
2012-01-08 01:01:15 -05:00
|
|
|
Code.from_method(obj)
|
|
|
|
else
|
|
|
|
Code.new(obj)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# `Pry::Code` is a class that encapsulates lines of source code and their
|
|
|
|
# line numbers and formats them for terminal output. It can read from a file
|
|
|
|
# or method definition or be instantiated with a `String` or an `Array`.
|
|
|
|
#
|
|
|
|
# In general, the formatting methods in `Code` return a new `Code` object
|
|
|
|
# which will format the text as specified when `#to_s` is called. This allows
|
|
|
|
# arbitrary chaining of formatting methods without mutating the original
|
|
|
|
# object.
|
2011-11-26 00:05:10 -05:00
|
|
|
class Code
|
|
|
|
class << self
|
2012-06-03 04:55:07 -04:00
|
|
|
include MethodSource::CodeHelpers
|
2012-04-10 09:39:29 -04:00
|
|
|
|
2011-11-26 00:05:10 -05:00
|
|
|
# Instantiate a `Code` object containing code loaded from a file or
|
|
|
|
# Pry's line buffer.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-02 18:23:28 -05:00
|
|
|
# @param [String] filename The name of a file, or "(pry)".
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [Symbol] code_type The type of code the file contains.
|
2011-11-26 00:05:10 -05:00
|
|
|
# @return [Code]
|
2013-11-23 16:01:40 -05:00
|
|
|
def from_file(filename, code_type = nil)
|
|
|
|
code_file = CodeFile.new(filename, code_type)
|
|
|
|
new(code_file.code, 1, code_file.code_type)
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
2011-12-04 15:43:44 -05:00
|
|
|
|
|
|
|
# Instantiate a `Code` object containing code extracted from a
|
|
|
|
# `::Method`, `UnboundMethod`, `Proc`, or `Pry::Method` object.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
|
|
|
# @param [::Method, UnboundMethod, Proc, Pry::Method] meth The method
|
|
|
|
# object.
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer, nil] start_line The line number to start on, or nil to
|
2012-06-27 01:30:00 -04:00
|
|
|
# use the method's original line numbers.
|
2011-12-04 15:43:44 -05:00
|
|
|
# @return [Code]
|
2013-01-06 21:17:18 -05:00
|
|
|
def from_method(meth, start_line = nil)
|
2011-12-04 15:43:44 -05:00
|
|
|
meth = Pry::Method(meth)
|
|
|
|
start_line ||= meth.source_line || 1
|
|
|
|
new(meth.source, start_line, meth.source_type)
|
|
|
|
end
|
2012-01-15 01:06:24 -05:00
|
|
|
|
2012-04-10 09:41:38 -04:00
|
|
|
# Attempt to extract the source code for module (or class) `mod`.
|
|
|
|
#
|
|
|
|
# @param [Module, Class] mod The module (or class) of interest.
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer] candidate_rank The module candidate (by rank)
|
2012-06-23 04:14:10 -04:00
|
|
|
# to use (see `Pry::WrappedModule::Candidate` for more information).
|
2013-02-02 11:56:21 -05:00
|
|
|
# @param [Integer, nil] start_line The line number to start on, or nil to
|
|
|
|
# use the method's original line numbers.
|
2012-04-10 09:41:38 -04:00
|
|
|
# @return [Code]
|
2013-02-02 11:56:21 -05:00
|
|
|
def from_module(mod, candidate_rank = 0, start_line=nil)
|
2012-06-22 11:33:12 -04:00
|
|
|
candidate = Pry::WrappedModule(mod).candidate(candidate_rank)
|
|
|
|
start_line ||= candidate.line
|
|
|
|
new(candidate.source, start_line, :ruby)
|
2012-04-10 09:41:38 -04:00
|
|
|
end
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
|
2012-06-27 01:30:00 -04:00
|
|
|
# @return [Symbol] The type of code stored in this wrapper.
|
2012-01-08 01:01:15 -05:00
|
|
|
attr_accessor :code_type
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Instantiate a `Code` object containing code from the given `Array`,
|
|
|
|
# `String`, or `IO`. The first line will be line 1 unless specified
|
|
|
|
# otherwise. If you need non-contiguous line numbers, you can create an
|
|
|
|
# empty `Code` object and then use `#push` to insert the lines.
|
|
|
|
#
|
2012-01-08 02:56:42 -05:00
|
|
|
# @param [Array<String>, String, IO] lines
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer?] start_line
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [Symbol?] code_type
|
2013-01-04 20:27:17 -05:00
|
|
|
def initialize(lines = [], start_line = 1, code_type = :ruby)
|
2011-12-04 15:23:07 -05:00
|
|
|
if lines.is_a? String
|
|
|
|
lines = lines.lines
|
|
|
|
end
|
2013-01-04 20:27:17 -05:00
|
|
|
@lines = lines.each_with_index.map { |line, lineno|
|
|
|
|
LOC.new(line, lineno + start_line.to_i) }
|
2011-11-26 00:05:10 -05:00
|
|
|
@code_type = code_type
|
|
|
|
end
|
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
# Append the given line. +lineno+ is one more than the last existing
|
2012-01-14 23:07:16 -05:00
|
|
|
# line, unless specified otherwise.
|
|
|
|
#
|
2011-11-26 00:05:10 -05:00
|
|
|
# @param [String] line
|
2013-01-04 20:27:17 -05:00
|
|
|
# @param [Integer?] lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [String] The inserted line.
|
2013-01-04 20:27:17 -05:00
|
|
|
def push(line, lineno = nil)
|
|
|
|
if lineno.nil?
|
|
|
|
lineno = @lines.last.lineno + 1
|
|
|
|
end
|
|
|
|
@lines.push(LOC.new(line, lineno))
|
2012-01-14 23:07:16 -05:00
|
|
|
line
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
alias << push
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Filter the lines using the given block.
|
|
|
|
#
|
2013-01-04 21:24:42 -05:00
|
|
|
# @yield [LOC]
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-06 21:17:18 -05:00
|
|
|
def select(&block)
|
2012-01-14 23:07:16 -05:00
|
|
|
alter do
|
2013-01-06 21:17:18 -05:00
|
|
|
@lines = @lines.select(&block)
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Remove all lines that aren't in the given range, expressed either as a
|
2012-01-15 17:16:18 -05:00
|
|
|
# `Range` object or a first and last line number (inclusive). Negative
|
|
|
|
# indices count from the end of the array of lines.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Range, Integer] start_line
|
|
|
|
# @param [Integer?] end_line
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 21:24:42 -05:00
|
|
|
def between(start_line, end_line = nil)
|
2012-01-08 03:11:30 -05:00
|
|
|
return self unless start_line
|
2012-01-08 02:56:42 -05:00
|
|
|
|
2013-01-05 00:18:00 -05:00
|
|
|
code_range = CodeRange.new(start_line, end_line)
|
2012-01-08 03:11:30 -05:00
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
alter do
|
2013-01-05 00:18:00 -05:00
|
|
|
@lines = @lines[code_range.indices_range(@lines)] || []
|
2011-12-04 15:43:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 19:52:20 -05:00
|
|
|
# Take `num_lines` from `start_line`, forward or backwards.
|
2012-02-10 16:53:12 -05:00
|
|
|
#
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer] start_line
|
|
|
|
# @param [Integer] num_lines
|
2012-02-10 16:53:12 -05:00
|
|
|
# @return [Code]
|
|
|
|
def take_lines(start_line, num_lines)
|
2013-01-04 20:27:17 -05:00
|
|
|
start_idx =
|
|
|
|
if start_line >= 0
|
|
|
|
@lines.index { |loc| loc.lineno >= start_line } || @lines.length
|
|
|
|
else
|
2013-03-25 00:22:43 -04:00
|
|
|
[@lines.length + start_line, 0].max
|
2013-01-04 20:27:17 -05:00
|
|
|
end
|
2012-02-10 16:53:12 -05:00
|
|
|
|
|
|
|
alter do
|
|
|
|
@lines = @lines.slice(start_idx, num_lines)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
# Remove all lines except for the +lines+ up to and excluding +lineno+.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-04 20:27:17 -05:00
|
|
|
# @param [Integer] lineno
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer] lines
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 20:27:17 -05:00
|
|
|
def before(lineno, lines = 1)
|
|
|
|
return self unless lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
select do |loc|
|
|
|
|
loc.lineno >= lineno - lines && loc.lineno < lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
# Remove all lines except for the +lines+ on either side of and including
|
|
|
|
# +lineno+.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-04 20:27:17 -05:00
|
|
|
# @param [Integer] lineno
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer] lines
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 20:27:17 -05:00
|
|
|
def around(lineno, lines = 1)
|
|
|
|
return self unless lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
select do |loc|
|
|
|
|
loc.lineno >= lineno - lines && loc.lineno <= lineno + lines
|
2012-01-14 23:07:16 -05:00
|
|
|
end
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
# Remove all lines except for the +lines+ after and excluding +lineno+.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-04 20:27:17 -05:00
|
|
|
# @param [Integer] lineno
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer] lines
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 20:27:17 -05:00
|
|
|
def after(lineno, lines = 1)
|
|
|
|
return self unless lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
select do |loc|
|
|
|
|
loc.lineno > lineno && loc.lineno <= lineno + lines
|
2012-01-14 23:07:16 -05:00
|
|
|
end
|
2012-01-08 02:56:42 -05:00
|
|
|
end
|
2012-01-08 01:01:15 -05:00
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Remove all lines that don't match the given `pattern`.
|
|
|
|
#
|
|
|
|
# @param [Regexp] pattern
|
|
|
|
# @return [Code]
|
2012-01-08 02:56:42 -05:00
|
|
|
def grep(pattern)
|
|
|
|
return self unless pattern
|
2012-01-15 02:03:41 -05:00
|
|
|
pattern = Regexp.new(pattern)
|
2012-01-14 23:07:16 -05:00
|
|
|
|
2013-01-04 20:27:17 -05:00
|
|
|
select do |loc|
|
|
|
|
loc.line =~ pattern
|
2012-01-14 23:07:16 -05:00
|
|
|
end
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Format output with line numbers next to it, unless `y_n` is falsy.
|
|
|
|
#
|
2012-06-27 01:30:00 -04:00
|
|
|
# @param [Boolean?] y_n
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 21:24:42 -05:00
|
|
|
def with_line_numbers(y_n = true)
|
2012-01-14 23:07:16 -05:00
|
|
|
alter do
|
2011-12-04 15:23:07 -05:00
|
|
|
@with_line_numbers = y_n
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-06 19:52:20 -05:00
|
|
|
# Format output with a marker next to the given +lineno+, unless +lineno+ is
|
|
|
|
# falsy.
|
2012-01-14 23:07:16 -05:00
|
|
|
#
|
2013-01-04 20:27:17 -05:00
|
|
|
# @param [Integer?] lineno
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 20:27:17 -05:00
|
|
|
def with_marker(lineno = 1)
|
2012-01-14 23:07:16 -05:00
|
|
|
alter do
|
2013-01-04 20:27:17 -05:00
|
|
|
@with_marker = !!lineno
|
|
|
|
@marker_lineno = lineno
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Format output with the specified number of spaces in front of every line,
|
|
|
|
# unless `spaces` is falsy.
|
|
|
|
#
|
2013-01-03 07:29:04 -05:00
|
|
|
# @param [Integer?] spaces
|
2012-01-14 23:07:16 -05:00
|
|
|
# @return [Code]
|
2013-01-04 21:24:42 -05:00
|
|
|
def with_indentation(spaces = 0)
|
2012-01-14 23:07:16 -05:00
|
|
|
alter do
|
2012-01-08 01:01:15 -05:00
|
|
|
@with_indentation = !!spaces
|
|
|
|
@indentation_num = spaces
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-26 00:05:10 -05:00
|
|
|
# @return [String]
|
|
|
|
def inspect
|
|
|
|
Object.instance_method(:to_s).bind(self).call
|
|
|
|
end
|
|
|
|
|
2013-01-06 16:46:45 -05:00
|
|
|
# @return [Integer] the number of digits in the last line.
|
|
|
|
def max_lineno_width
|
|
|
|
@lines.length > 0 ? @lines.last.lineno.to_s.length : 0
|
|
|
|
end
|
|
|
|
|
2013-01-02 20:18:38 -05:00
|
|
|
# @return [String] a formatted representation (based on the configuration of
|
|
|
|
# the object).
|
2011-11-26 00:05:10 -05:00
|
|
|
def to_s
|
2013-01-06 21:26:51 -05:00
|
|
|
@lines.map { |loc|
|
|
|
|
loc = loc.dup
|
|
|
|
loc.colorize(@code_type) if Pry.color
|
2013-01-06 16:46:45 -05:00
|
|
|
loc.add_line_number(max_lineno_width) if @with_line_numbers
|
2013-01-06 21:26:51 -05:00
|
|
|
loc.add_marker(@marker_lineno) if @with_marker
|
|
|
|
loc.indent(@indentation_num) if @with_indentation
|
|
|
|
loc.line
|
|
|
|
}.join("\n") + "\n"
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
2011-12-04 15:23:07 -05:00
|
|
|
|
2012-06-03 04:55:07 -04:00
|
|
|
# Get the comment that describes the expression on the given line number.
|
|
|
|
#
|
2013-01-06 21:04:46 -05:00
|
|
|
# @param [Integer] line_number (1-based)
|
|
|
|
# @return [String] the code.
|
2012-06-03 04:55:07 -04:00
|
|
|
def comment_describing(line_number)
|
|
|
|
self.class.comment_describing(raw, line_number)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get the multiline expression that starts on the given line number.
|
|
|
|
#
|
2013-01-06 21:04:46 -05:00
|
|
|
# @param [Integer] line_number (1-based)
|
|
|
|
# @return [String] the code.
|
2013-01-04 21:24:42 -05:00
|
|
|
def expression_at(line_number, consume = 0)
|
2012-06-28 13:24:18 -04:00
|
|
|
self.class.expression_at(raw, line_number, :consume => consume)
|
2012-06-03 04:55:07 -04:00
|
|
|
end
|
|
|
|
|
2012-07-28 04:41:25 -04:00
|
|
|
# Get the (approximate) Module.nesting at the give line number.
|
|
|
|
#
|
2013-01-06 21:04:46 -05:00
|
|
|
# @param [Integer] line_number line number starting from 1
|
|
|
|
# @param [Module] top_module the module in which this code exists
|
|
|
|
# @return [Array<Module>] a list of open modules.
|
2013-01-04 21:24:42 -05:00
|
|
|
def nesting_at(line_number, top_module = Object)
|
2012-07-28 04:41:25 -04:00
|
|
|
Pry::Indent.nesting_at(raw, line_number)
|
|
|
|
end
|
|
|
|
|
2012-01-21 00:55:05 -05:00
|
|
|
# Return an unformatted String of the code.
|
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
def raw
|
2013-01-04 20:27:17 -05:00
|
|
|
@lines.map(&:line).join("\n") + "\n"
|
2012-01-21 00:55:05 -05:00
|
|
|
end
|
|
|
|
|
2012-01-15 00:19:30 -05:00
|
|
|
# Return the number of lines stored.
|
|
|
|
#
|
2013-01-03 07:29:04 -05:00
|
|
|
# @return [Integer]
|
2012-01-15 00:19:30 -05:00
|
|
|
def length
|
|
|
|
@lines ? @lines.length : 0
|
|
|
|
end
|
|
|
|
|
|
|
|
# Two `Code` objects are equal if they contain the same lines with the same
|
2012-01-15 03:55:40 -05:00
|
|
|
# numbers. Otherwise, call `to_s` and `chomp` and compare as Strings.
|
2012-01-15 00:19:30 -05:00
|
|
|
#
|
2012-01-15 03:55:40 -05:00
|
|
|
# @param [Code, Object] other
|
2012-01-15 00:19:30 -05:00
|
|
|
# @return [Boolean]
|
|
|
|
def ==(other)
|
2012-01-15 03:55:40 -05:00
|
|
|
if other.is_a?(Code)
|
2013-01-06 18:44:49 -05:00
|
|
|
other_lines = other.instance_variable_get(:@lines)
|
|
|
|
@lines.each_with_index.all? { |loc, i| loc == other_lines[i] }
|
2012-01-15 03:55:40 -05:00
|
|
|
else
|
|
|
|
to_s.chomp == other.to_s.chomp
|
2012-01-15 00:19:30 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-14 23:07:16 -05:00
|
|
|
# Forward any missing methods to the output of `#to_s`.
|
2013-01-06 21:17:18 -05:00
|
|
|
def method_missing(name, *args, &block)
|
|
|
|
to_s.send(name, *args, &block)
|
2011-12-04 15:23:07 -05:00
|
|
|
end
|
2012-01-15 00:19:30 -05:00
|
|
|
undef =~
|
2012-01-14 23:07:16 -05:00
|
|
|
|
|
|
|
protected
|
2013-01-02 18:39:45 -05:00
|
|
|
|
|
|
|
# An abstraction of the `dup.instance_eval` pattern used throughout this
|
|
|
|
# class.
|
2013-01-06 21:17:18 -05:00
|
|
|
def alter(&block)
|
|
|
|
dup.tap { |o| o.instance_eval(&block) }
|
2013-01-02 18:39:45 -05:00
|
|
|
end
|
2011-11-26 00:05:10 -05:00
|
|
|
end
|
|
|
|
end
|