Skip to content
Browse files

show-doc for core classes now shows file name, closes #536

  • Loading branch information...
1 parent 765f017 commit d8d80419ee7ed93d1800b7461412b1ddd196a7f7 @banister banister committed
Showing with 37 additions and 9 deletions.
  1. +15 −3 lib/pry/default_commands/introspection.rb
  2. +22 −6 lib/pry/wrapped_module.rb
View
18 lib/pry/default_commands/introspection.rb
@@ -54,17 +54,29 @@ def process_module(name)
klass = target.eval(name)
mod = Pry::WrappedModule(klass)
- file_name, line = mod.source_location
+
+ # source_file reveals the underlying .c file in case of core
+ # classes on MRI.
+ # This is different to source_location, which
+ # will return nil.
+ if mod.yard_docs?
+ file_name, line = mod.source_file, nil
+ else
+ file_name, line = mod.source_location
+ end
+
doc = mod.doc
if doc.empty?
output.puts "No documentation found."
else
- set_file_and_dir_locals(file_name)
+
+ # source_location is nil in case of core classes on MRI
+ set_file_and_dir_locals(file_name) if !mod.yard_docs?
output.puts "\n#{Pry::Helpers::Text.bold('From:')} #{file_name} @ line #{line}:\n\n"
if opts.present?(:b) || opts.present?(:l)
- start_line = file_name.nil? ? 1 : line - doc.lines.count
+ start_line = mod.source_location.nil? ? 1 : line - doc.lines.count
doc = Code.new(doc, start_line, :text).
with_line_numbers(true)
end
View
28 lib/pry/wrapped_module.rb
@@ -101,17 +101,20 @@ def respond_to?(method_name)
super || wrapped.respond_to?(method_name)
end
+ def yard_docs?
+ !!(defined?(YARD) && YARD::Registry.at(name))
+ end
+
def doc
return @doc if @doc
file_name, line = source_location
- if file_name.nil?
- if defined?(YARD) && from_yard = YARD::Registry.at(name)
- @doc = from_yard.docstring
- else
- raise CommandError, "Can't find module's source location"
- end
+ if yard_docs?
+ from_yard = YARD::Registry.at(name)
+ @doc = from_yard.docstring
+ elsif source_location.nil?
+ raise CommandError, "Can't find module's source location"
else
@doc = extract_doc
end
@@ -132,6 +135,19 @@ def source
end
+ def source_file
+ if yard_docs?
+ from_yard = YARD::Registry.at(name)
+ from_yard.file
+ else
+ Array(source_location).first
+ end
+ end
+
+ def source_line
+ source_location.nil? ? nil : source_location.last
+ end
+
# Retrieve the source location of a module. Return value is in same
# format as Method#source_location. If the source location
# cannot be found this method returns `nil`.

0 comments on commit d8d8041

Please sign in to comment.
Something went wrong with that request. Please try again.