2013-01-07 17:12:10 -05:00
|
|
|
class Pry
|
2013-01-08 19:39:02 -05:00
|
|
|
class Command::CodeCollector
|
2013-01-07 17:12:10 -05:00
|
|
|
include Helpers::CommandHelpers
|
|
|
|
|
2014-02-06 19:59:03 -05:00
|
|
|
attr_reader :args
|
|
|
|
attr_reader :opts
|
|
|
|
attr_reader :_pry_
|
2013-01-08 19:39:02 -05:00
|
|
|
|
2013-01-15 12:29:43 -05:00
|
|
|
# The name of the explicitly given file (if any).
|
|
|
|
attr_accessor :file
|
|
|
|
|
2013-01-12 16:10:04 -05:00
|
|
|
class << self
|
|
|
|
attr_accessor :input_expression_ranges
|
|
|
|
attr_accessor :output_result_ranges
|
|
|
|
end
|
|
|
|
|
|
|
|
@input_expression_ranges = []
|
|
|
|
@output_result_ranges = []
|
|
|
|
|
2013-01-08 19:39:02 -05:00
|
|
|
def initialize(args, opts, _pry_)
|
|
|
|
@args = args
|
|
|
|
@opts = opts
|
|
|
|
@_pry_ = _pry_
|
|
|
|
end
|
|
|
|
|
|
|
|
# Add the `--lines`, `-o`, `-i`, `-s`, `-d` options.
|
|
|
|
def self.inject_options(opt)
|
2013-01-12 16:10:04 -05:00
|
|
|
@input_expression_ranges = []
|
|
|
|
@output_result_ranges = []
|
|
|
|
|
2013-01-09 15:23:19 -05:00
|
|
|
opt.on :l, :lines, "Restrict to a subset of lines. Takes a line number or range",
|
2018-10-12 15:09:29 -04:00
|
|
|
optional_argument: true, as: Range, default: 1..-1
|
2013-01-09 15:23:19 -05:00
|
|
|
opt.on :o, :out, "Select lines from Pry's output result history. Takes an index or range",
|
2018-10-12 15:09:29 -04:00
|
|
|
optional_argument: true, as: Range, default: -5..-1 do |r|
|
2013-01-12 16:10:04 -05:00
|
|
|
output_result_ranges << (r || (-5..-1))
|
|
|
|
end
|
2013-01-09 15:23:19 -05:00
|
|
|
opt.on :i, :in, "Select lines from Pry's input expression history. Takes an index or range",
|
2018-10-12 15:09:29 -04:00
|
|
|
optional_argument: true, as: Range, default: -5..-1 do |r|
|
2013-01-12 16:10:04 -05:00
|
|
|
input_expression_ranges << (r || (-5..-1))
|
|
|
|
end
|
2013-01-09 15:23:19 -05:00
|
|
|
opt.on :s, :super, "Select the 'super' method. Can be repeated to traverse the ancestors",
|
2018-10-12 15:09:29 -04:00
|
|
|
as: :count
|
2013-01-09 15:23:19 -05:00
|
|
|
opt.on :d, :doc, "Select lines from the code object's documentation"
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-01-08 19:39:02 -05:00
|
|
|
# The content (i.e code/docs) for the selected object.
|
|
|
|
# If the user provided a bare code object, it returns the source.
|
|
|
|
# If the user provided the `-i` or `-o` switches, it returns the
|
|
|
|
# selected input/output lines joined as a string. If the user used
|
|
|
|
# `-d CODE_OBJECT` it returns the docs for that code object.
|
|
|
|
#
|
|
|
|
# @return [String]
|
2013-01-07 17:12:10 -05:00
|
|
|
def content
|
2015-01-23 04:30:41 -05:00
|
|
|
@content ||=
|
|
|
|
begin
|
|
|
|
raise CommandError, "Only one of --out, --in, --doc and CODE_OBJECT may be specified." if bad_option_combination?
|
2013-01-07 17:12:10 -05:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
content = case
|
|
|
|
when opts.present?(:o)
|
|
|
|
pry_output_content
|
|
|
|
when opts.present?(:i)
|
|
|
|
pry_input_content
|
|
|
|
when opts.present?(:d)
|
|
|
|
code_object_doc
|
|
|
|
else
|
|
|
|
code_object_source_or_file
|
|
|
|
end
|
2013-01-07 17:12:10 -05:00
|
|
|
|
2015-01-23 04:30:41 -05:00
|
|
|
restrict_to_lines(content, line_range)
|
|
|
|
end
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-01-08 19:39:02 -05:00
|
|
|
# The code object
|
|
|
|
#
|
|
|
|
# @return [Pry::WrappedModule, Pry::Method, Pry::Command]
|
|
|
|
def code_object
|
2018-10-12 15:09:29 -04:00
|
|
|
Pry::CodeObject.lookup(obj_name, _pry_, super: opts[:super])
|
2013-01-08 19:39:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# Given a string and a range, return the `range` lines of that
|
|
|
|
# string.
|
|
|
|
#
|
|
|
|
# @param [String] content
|
|
|
|
# @param [Range, Fixnum] range
|
|
|
|
# @return [String] The string restricted to the given range
|
|
|
|
def restrict_to_lines(content, range)
|
|
|
|
Array(content.lines.to_a[range]).join
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
|
2018-10-19 10:27:37 -04:00
|
|
|
# The selected `_pry_.output_ring` as a string, as specified by
|
2013-01-08 19:39:02 -05:00
|
|
|
# the `-o` switch.
|
|
|
|
#
|
|
|
|
# @return [String]
|
2013-01-07 17:12:10 -05:00
|
|
|
def pry_output_content
|
2018-10-19 10:27:37 -04:00
|
|
|
pry_array_content_as_string(_pry_.output_ring, self.class.output_result_ranges) do |v|
|
2014-02-06 19:59:03 -05:00
|
|
|
_pry_.config.gist.inspecter.call(v)
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-19 10:27:37 -04:00
|
|
|
# The selected `_pry_.input_ring` as a string, as specified by
|
2013-01-08 19:39:02 -05:00
|
|
|
# the `-i` switch.
|
|
|
|
#
|
|
|
|
# @return [String]
|
2013-01-07 17:12:10 -05:00
|
|
|
def pry_input_content
|
2018-10-19 10:27:37 -04:00
|
|
|
pry_array_content_as_string(_pry_.input_ring, self.class.input_expression_ranges) { |v| v }
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-01-15 15:48:16 -05:00
|
|
|
# The line range passed to `--lines`, converted to a 0-indexed range.
|
2013-01-08 19:39:02 -05:00
|
|
|
def line_range
|
|
|
|
opts.present?(:lines) ? one_index_range_or_number(opts[:lines]) : 0..-1
|
|
|
|
end
|
|
|
|
|
2013-01-12 16:10:04 -05:00
|
|
|
# Name of the object argument
|
|
|
|
def obj_name
|
2013-01-16 10:35:01 -05:00
|
|
|
@obj_name ||= args.empty? ? "" : args.join(" ")
|
2013-01-12 16:10:04 -05:00
|
|
|
end
|
|
|
|
|
2013-01-08 19:39:02 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def bad_option_combination?
|
|
|
|
[opts.present?(:in), opts.present?(:out),
|
|
|
|
!args.empty?].count(true) > 1
|
|
|
|
end
|
|
|
|
|
2013-01-12 16:10:04 -05:00
|
|
|
def pry_array_content_as_string(array, ranges, &block)
|
2013-01-08 15:37:37 -05:00
|
|
|
all = ''
|
2013-01-12 16:10:04 -05:00
|
|
|
ranges.each do |range|
|
|
|
|
raise CommandError, "Minimum value for range is 1, not 0." if convert_to_range(range).first == 0
|
|
|
|
|
|
|
|
ranged_array = Array(array[range]) || []
|
|
|
|
ranged_array.compact.each { |v| all << block.call(v) }
|
|
|
|
end
|
|
|
|
|
2013-01-08 15:37:37 -05:00
|
|
|
all
|
2013-01-07 17:12:10 -05:00
|
|
|
end
|
|
|
|
|
2013-01-12 16:10:04 -05:00
|
|
|
def code_object_doc
|
2018-11-17 11:22:56 -05:00
|
|
|
(code_object && code_object.doc) || could_not_locate(obj_name)
|
2013-01-12 16:10:04 -05:00
|
|
|
end
|
|
|
|
|
2013-01-07 17:12:10 -05:00
|
|
|
def code_object_source_or_file
|
|
|
|
(code_object && code_object.source) || file_content
|
|
|
|
end
|
|
|
|
|
|
|
|
def file_content
|
2015-01-23 04:30:41 -05:00
|
|
|
if File.exist?(obj_name)
|
2013-01-15 12:29:43 -05:00
|
|
|
# Set the file accessor.
|
|
|
|
self.file = obj_name
|
2013-01-07 17:12:10 -05:00
|
|
|
File.read(obj_name)
|
|
|
|
else
|
|
|
|
could_not_locate(obj_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def could_not_locate(name)
|
|
|
|
raise CommandError, "Cannot locate: #{name}!"
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_to_range(n)
|
|
|
|
if !n.is_a?(Range)
|
|
|
|
(n..n)
|
|
|
|
else
|
|
|
|
n
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|