mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
improve show-source/show-doc via Pry::CodeObject::Helpers mixin
Pry::CodeObject::Helpers mixin provides the following methods: command? module_with_yard_docs? real_method_object? c_method? which are then mixed into the code objects: Pry::Command, Pry::Method, Pry::WrappedModule, Pry::WrappedModule::Candidate
This commit is contained in:
parent
b84a889390
commit
407b20470a
8 changed files with 115 additions and 97 deletions
|
@ -230,10 +230,11 @@ require 'pry/version'
|
||||||
require 'pry/rbx_method'
|
require 'pry/rbx_method'
|
||||||
require 'pry/rbx_path'
|
require 'pry/rbx_path'
|
||||||
require 'pry/code'
|
require 'pry/code'
|
||||||
require 'pry/method'
|
|
||||||
require 'pry/wrapped_module'
|
|
||||||
require 'pry/history_array'
|
require 'pry/history_array'
|
||||||
require 'pry/helpers'
|
require 'pry/helpers'
|
||||||
|
require 'pry/code_object'
|
||||||
|
require 'pry/method'
|
||||||
|
require 'pry/wrapped_module'
|
||||||
require 'pry/history'
|
require 'pry/history'
|
||||||
require 'pry/command'
|
require 'pry/command'
|
||||||
require 'pry/command_set'
|
require 'pry/command_set'
|
||||||
|
@ -247,4 +248,3 @@ require 'pry/pry_instance'
|
||||||
require 'pry/cli'
|
require 'pry/cli'
|
||||||
require 'pry/pager'
|
require 'pry/pager'
|
||||||
require 'pry/terminal_info'
|
require 'pry/terminal_info'
|
||||||
require 'pry/code_object'
|
|
||||||
|
|
|
@ -1,6 +1,26 @@
|
||||||
class Pry
|
class Pry
|
||||||
class CodeObject
|
class CodeObject
|
||||||
include Helpers::CommandHelpers
|
module Helpers
|
||||||
|
# we need this helper as some Pry::Method objects can wrap Procs
|
||||||
|
# @return [Boolean]
|
||||||
|
def real_method_object?
|
||||||
|
is_a?(::Method) || is_a?(::UnboundMethod)
|
||||||
|
end
|
||||||
|
|
||||||
|
def c_method?
|
||||||
|
real_method_object? && source_type == :c
|
||||||
|
end
|
||||||
|
|
||||||
|
def module_with_yard_docs?
|
||||||
|
is_a?(WrappedModule) && yard_docs?
|
||||||
|
end
|
||||||
|
|
||||||
|
def command?
|
||||||
|
is_a?(Module) && self <= Pry::Command
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
include Pry::Helpers::CommandHelpers
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
def lookup(str, target, _pry_, options={})
|
def lookup(str, target, _pry_, options={})
|
||||||
|
|
|
@ -8,6 +8,7 @@ class Pry
|
||||||
# which creates a ClassCommand. Please don't use this class directly.
|
# which creates a ClassCommand. Please don't use this class directly.
|
||||||
class Command
|
class Command
|
||||||
extend Helpers::DocumentationHelpers
|
extend Helpers::DocumentationHelpers
|
||||||
|
extend CodeObject::Helpers
|
||||||
|
|
||||||
# represents a void return value for a command
|
# represents a void return value for a command
|
||||||
VOID_VALUE = Object.new
|
VOID_VALUE = Object.new
|
||||||
|
|
|
@ -30,32 +30,54 @@ class Pry
|
||||||
opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module/class"
|
opt.on :a, :all, "Show docs for all definitions and monkeypatches of the module/class"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def process
|
def process
|
||||||
code_object = Pry::CodeObject.lookup obj_name, target, _pry_, :super => opts[:super]
|
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
|
||||||
|
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
|
||||||
|
|
||||||
if !code_object
|
if show_docs_for_all_modules?(code_object)
|
||||||
raise Pry::CommandError, "Couldn't locate #{obj_name}!"
|
# show docs for all monkey patches for a module
|
||||||
end
|
result = docs_and_headers_for_all_module_candidates(code_object)
|
||||||
|
|
||||||
if code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
|
||||||
# show all monkey patches for a module
|
|
||||||
# docs_for_all_modules_candidates writes its own headers
|
|
||||||
result = docs_for_all_module_candidates(code_object)
|
|
||||||
else
|
else
|
||||||
# show the source for a specific code object
|
# show the source for a specific code object
|
||||||
result = header(code_object)
|
result = docs_and_header_for_code_object(code_object)
|
||||||
result << Code.new(render_doc_markup_for(code_object),
|
|
||||||
opts.present?(:b) ? 1 : start_line_for(code_object),
|
|
||||||
:text).
|
|
||||||
with_line_numbers(use_line_numbers?).to_s
|
|
||||||
set_file_and_dir_locals(code_object.source_file)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
set_file_and_dir_locals(code_object.source_file)
|
||||||
stagger_output result
|
stagger_output result
|
||||||
end
|
end
|
||||||
|
|
||||||
# simple utility function to piece together the name of the object
|
def docs_and_header_for_code_object(code_object)
|
||||||
|
result = header(code_object)
|
||||||
|
result << Code.new(render_doc_markup_for(code_object),
|
||||||
|
opts.present?(:b) ? 1 : start_line_for(code_object),
|
||||||
|
:text).
|
||||||
|
with_line_numbers(use_line_numbers?).to_s
|
||||||
|
end
|
||||||
|
|
||||||
|
def docs_and_headers_for_all_module_candidates(mod)
|
||||||
|
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
|
||||||
|
mod.number_of_candidates.times do |v|
|
||||||
|
candidate = mod.candidate(v)
|
||||||
|
begin
|
||||||
|
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
|
||||||
|
doc = Code.new(render_doc_markup_for(candidate),
|
||||||
|
opts.present?(:b) ? 1 : candidate.source_line,
|
||||||
|
:text).with_line_numbers(use_line_numbers?).to_s
|
||||||
|
result << "Number of lines: #{doc.lines.count}\n\n" << doc
|
||||||
|
rescue Pry::RescuableException
|
||||||
|
result << "\nNo code found.\n"
|
||||||
|
|
||||||
|
next
|
||||||
|
end
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
|
def show_docs_for_all_modules?(code_object)
|
||||||
|
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
||||||
|
end
|
||||||
|
|
||||||
|
# simple function to piece together the name of the object
|
||||||
# passed in from the arg list
|
# passed in from the arg list
|
||||||
def obj_name
|
def obj_name
|
||||||
@obj_name ||= args.empty? ? nil : args.join(" ")
|
@obj_name ||= args.empty? ? nil : args.join(" ")
|
||||||
|
@ -65,8 +87,8 @@ class Pry
|
||||||
def render_doc_markup_for(code_object)
|
def render_doc_markup_for(code_object)
|
||||||
docs = docs_for(code_object)
|
docs = docs_for(code_object)
|
||||||
|
|
||||||
if code_object_is_command?(code_object)
|
if code_object.command?
|
||||||
# command 'help' doesn't want markup highlighting
|
# command '--help' shouldn't use markup highlighting
|
||||||
docs
|
docs
|
||||||
else
|
else
|
||||||
process_comment_markup(docs)
|
process_comment_markup(docs)
|
||||||
|
@ -79,7 +101,7 @@ class Pry
|
||||||
# have multiple docs, but methods can only be doc'd once so we
|
# have multiple docs, but methods can only be doc'd once so we
|
||||||
# dont need to check them)
|
# dont need to check them)
|
||||||
def docs_for(code_object)
|
def docs_for(code_object)
|
||||||
if code_object.is_a?(WrappedModule) && code_object.yard_docs?
|
if code_object.module_with_yard_docs?
|
||||||
# yard docs
|
# yard docs
|
||||||
code_object.yard_doc
|
code_object.yard_doc
|
||||||
else
|
else
|
||||||
|
@ -88,17 +110,11 @@ class Pry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# we need this helper as some Pry::Method objects can wrap Procs
|
|
||||||
# @return [Boolean]
|
|
||||||
def real_method_object?(code_object)
|
|
||||||
code_object.is_a?(::Method) || code_object.is_a?(::UnboundMethod)
|
|
||||||
end
|
|
||||||
|
|
||||||
# takes into account possible yard docs, and returns yard_file / yard_line
|
# takes into account possible yard docs, and returns yard_file / yard_line
|
||||||
# Also adjusts for start line of comments (using start_line_for), which it has to infer
|
# Also adjusts for start line of comments (using start_line_for), which it has to infer
|
||||||
# by subtracting number of lines of comment from start line of code_object
|
# by subtracting number of lines of comment from start line of code_object
|
||||||
def file_and_line_for(code_object)
|
def file_and_line_for(code_object)
|
||||||
if code_object.is_a?(WrappedModule) && code_object.yard_docs?
|
if code_object.module_with_yard_docs?
|
||||||
[code_object.yard_file, code_object.yard_line]
|
[code_object.yard_file, code_object.yard_line]
|
||||||
else
|
else
|
||||||
[code_object.source_file, start_line_for(code_object)]
|
[code_object.source_file, start_line_for(code_object)]
|
||||||
|
@ -110,13 +126,13 @@ class Pry
|
||||||
def header(code_object)
|
def header(code_object)
|
||||||
file_name, line_num = file_and_line_for(code_object)
|
file_name, line_num = file_and_line_for(code_object)
|
||||||
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
||||||
if real_method_object?(code_object) && code_object.source_type == :c
|
if code_object.c_method?
|
||||||
h << "(C Method):"
|
h << "(C Method):"
|
||||||
else
|
else
|
||||||
h << "@ line #{line_num}:"
|
h << "@ line #{line_num}:"
|
||||||
end
|
end
|
||||||
|
|
||||||
if real_method_object?(code_object)
|
if code_object.real_method_object?
|
||||||
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
||||||
h << "#{text.bold("Visibility:")} #{code_object.visibility}\n"
|
h << "#{text.bold("Visibility:")} #{code_object.visibility}\n"
|
||||||
h << "#{text.bold("Signature:")} #{code_object.signature}"
|
h << "#{text.bold("Signature:")} #{code_object.signature}"
|
||||||
|
@ -125,33 +141,11 @@ class Pry
|
||||||
"#{docs_for(code_object).lines.count}\n\n"
|
"#{docs_for(code_object).lines.count}\n\n"
|
||||||
end
|
end
|
||||||
|
|
||||||
def docs_for_all_module_candidates(mod)
|
# figure out start line of docs by back-calculating based on
|
||||||
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
|
|
||||||
mod.number_of_candidates.times do |v|
|
|
||||||
candidate = mod.candidate(v)
|
|
||||||
begin
|
|
||||||
result << "\nCandidate #{v+1}/#{mod.number_of_candidates}: #{candidate.source_file} @ line #{candidate.source_line}:\n"
|
|
||||||
doc = Code.new(render_doc_markup_for(candidate),
|
|
||||||
opts.present?(:b) ? 1 : candidate.source_line,
|
|
||||||
:text).with_line_numbers(use_line_numbers?).to_s
|
|
||||||
result << "Number of lines: #{doc.lines.count}\n\n" << doc
|
|
||||||
rescue Pry::RescuableException
|
|
||||||
result << "\nNo code found.\n"
|
|
||||||
next
|
|
||||||
end
|
|
||||||
end
|
|
||||||
result
|
|
||||||
end
|
|
||||||
|
|
||||||
def code_object_is_command?(code_object)
|
|
||||||
code_object.is_a?(Module) && code_object <= Pry::Command
|
|
||||||
end
|
|
||||||
|
|
||||||
# figure out start line of docs by back calculating based on
|
|
||||||
# number of lines in the comment and the start line of the code_object
|
# number of lines in the comment and the start line of the code_object
|
||||||
# @return [Fixnum] start line of docs
|
# @return [Fixnum] start line of docs
|
||||||
def start_line_for(code_object)
|
def start_line_for(code_object)
|
||||||
if code_object_is_command?(code_object)
|
if code_object.command?
|
||||||
1
|
1
|
||||||
else
|
else
|
||||||
code_object.source_line.nil? ? 1 :
|
code_object.source_line.nil? ? 1 :
|
||||||
|
@ -179,5 +173,6 @@ class Pry
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
Pry::Commands.alias_command "?", "show-doc"
|
Pry::Commands.alias_command "?", "show-doc"
|
||||||
end
|
end
|
||||||
|
|
|
@ -37,56 +37,28 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
def process
|
def process
|
||||||
code_object = Pry::CodeObject.lookup obj_name, target, _pry_, :super => opts[:super]
|
code_object = Pry::CodeObject.lookup(obj_name, target, _pry_, :super => opts[:super])
|
||||||
|
raise Pry::CommandError, "Couldn't locate #{obj_name}!" if !code_object
|
||||||
|
|
||||||
if !code_object
|
if show_source_for_all_modules?(code_object)
|
||||||
raise Pry::CommandError, "Couldn't locate #{obj_name}!"
|
|
||||||
end
|
|
||||||
|
|
||||||
if code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
|
||||||
# show all monkey patches for a module
|
# show all monkey patches for a module
|
||||||
result = source_for_all_module_candidates(code_object)
|
result = source_and_headers_for_all_module_candidates(code_object)
|
||||||
else
|
else
|
||||||
# show the source for a specific code object
|
# show the source for a specific code object
|
||||||
result = header(code_object)
|
result = source_and_header_for_code_object(code_object)
|
||||||
result << Code.new(code_object.source, start_line_for(code_object)).
|
|
||||||
with_line_numbers(use_line_numbers?).to_s
|
|
||||||
|
|
||||||
set_file_and_dir_locals(code_object.source_file)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
set_file_and_dir_locals(code_object.source_file)
|
||||||
stagger_output result
|
stagger_output result
|
||||||
end
|
end
|
||||||
|
|
||||||
def obj_name
|
def source_and_header_for_code_object(code_object)
|
||||||
@obj_name ||= args.empty? ? nil : args.join(" ")
|
result = header(code_object)
|
||||||
|
result << Code.new(code_object.source, start_line_for(code_object)).
|
||||||
|
with_line_numbers(use_line_numbers?).to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
# we need this helper as some Pry::Method objects can wrap Procs
|
def source_and_headers_for_all_module_candidates(mod)
|
||||||
# @return [Boolean]
|
|
||||||
def real_method_object?(code_object)
|
|
||||||
code_object.is_a?(::Method) || code_object.is_a?(::UnboundMethod)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Generate a header (meta-data information) for all the code
|
|
||||||
# object types: methods, modules, commands, procs...
|
|
||||||
def header(code_object)
|
|
||||||
file_name, line_num = code_object.source_file, code_object.source_line
|
|
||||||
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
|
||||||
if real_method_object?(code_object) && code_object.source_type == :c
|
|
||||||
h << "(C Method):"
|
|
||||||
else
|
|
||||||
h << "@ line #{line_num}:"
|
|
||||||
end
|
|
||||||
|
|
||||||
if real_method_object?(code_object)
|
|
||||||
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
|
||||||
h << "#{text.bold("Visibility:")} #{code_object.visibility}"
|
|
||||||
end
|
|
||||||
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} #{code_object.source.lines.count}\n\n"
|
|
||||||
end
|
|
||||||
|
|
||||||
def source_for_all_module_candidates(mod)
|
|
||||||
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
|
result = "Found #{mod.number_of_candidates} candidates for `#{mod.name}` definition:\n"
|
||||||
mod.number_of_candidates.times do |v|
|
mod.number_of_candidates.times do |v|
|
||||||
candidate = mod.candidate(v)
|
candidate = mod.candidate(v)
|
||||||
|
@ -102,6 +74,33 @@ class Pry
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def show_source_for_all_modules?(code_object)
|
||||||
|
code_object.is_a?(Pry::WrappedModule) && opts.present?(:all)
|
||||||
|
end
|
||||||
|
|
||||||
|
def obj_name
|
||||||
|
@obj_name ||= args.empty? ? nil : args.join(" ")
|
||||||
|
end
|
||||||
|
|
||||||
|
# Generate a header (meta-data information) for all the code
|
||||||
|
# object types: methods, modules, commands, procs...
|
||||||
|
def header(code_object)
|
||||||
|
file_name, line_num = code_object.source_file, code_object.source_line
|
||||||
|
h = "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} "
|
||||||
|
if code_object.c_method?
|
||||||
|
h << "(C Method):"
|
||||||
|
else
|
||||||
|
h << "@ line #{line_num}:"
|
||||||
|
end
|
||||||
|
|
||||||
|
if code_object.real_method_object?
|
||||||
|
h << "\n#{text.bold("Owner:")} #{code_object.owner || "N/A"}\n"
|
||||||
|
h << "#{text.bold("Visibility:")} #{code_object.visibility}"
|
||||||
|
end
|
||||||
|
h << "\n#{Pry::Helpers::Text.bold('Number of lines:')} " <<
|
||||||
|
"#{code_object.source.lines.count}\n\n"
|
||||||
|
end
|
||||||
|
|
||||||
def start_line_for(code_object)
|
def start_line_for(code_object)
|
||||||
if opts.present?(:'base-one')
|
if opts.present?(:'base-one')
|
||||||
1
|
1
|
||||||
|
|
|
@ -21,6 +21,7 @@ class Pry
|
||||||
include Helpers::BaseHelpers
|
include Helpers::BaseHelpers
|
||||||
include RbxMethod if Helpers::BaseHelpers.rbx?
|
include RbxMethod if Helpers::BaseHelpers.rbx?
|
||||||
include Helpers::DocumentationHelpers
|
include Helpers::DocumentationHelpers
|
||||||
|
include CodeObject::Helpers
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
# Given a string representing a method name and optionally a binding to
|
# Given a string representing a method name and optionally a binding to
|
||||||
|
|
|
@ -9,6 +9,7 @@ class Pry
|
||||||
# for a monkeypatch (reopening) of a class/module.
|
# for a monkeypatch (reopening) of a class/module.
|
||||||
class Candidate
|
class Candidate
|
||||||
include Pry::Helpers::DocumentationHelpers
|
include Pry::Helpers::DocumentationHelpers
|
||||||
|
include Pry::CodeObject::Helpers
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
# @return [String] The file where the module definition is located.
|
# @return [String] The file where the module definition is located.
|
||||||
|
|
|
@ -14,7 +14,8 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
class WrappedModule
|
class WrappedModule
|
||||||
include Pry::Helpers::BaseHelpers
|
include Helpers::BaseHelpers
|
||||||
|
include CodeObject::Helpers
|
||||||
|
|
||||||
attr_reader :wrapped
|
attr_reader :wrapped
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue