class Pry::Command::ShowSource

Public Instance Methods

content_for(code_object) click to toggle source

The source for code_object prepared for display.

# File lib/pry/commands/show_source.rb, line 48
def content_for(code_object)
  content = ''
  if opts.present?(:d)
    code = Code.new(
      render_doc_markup_for(code_object), start_line_for(code_object), :text
    )
    content += code.with_line_numbers(use_line_numbers?).to_s
    content += "\n"
  end

  code = Code.new(
    code_object.source || [], start_line_for(code_object)
  )
  content += code.with_line_numbers(use_line_numbers?).highlighted
  content
end
docs_for(code_object) click to toggle source

Return docs for the code_object, adjusting for whether the code_object has yard docs available, in which case it returns those. (note we only have to check yard docs for modules since they can have multiple docs, but methods can only be doc'd once so we dont need to check them)

# File lib/pry/commands/show_source.rb, line 86
def docs_for(code_object)
  if code_object.module_with_yard_docs?
    # yard docs
    code_object.yard_doc
  else
    # normal docs (i.e comments above method/module/command)
    code_object.doc
  end
end
header_options() click to toggle source

Which sections to include in the 'header', can toggle: :owner, :signature and visibility.

Calls superclass method Pry::Command::ShowInfo#header_options
# File lib/pry/commands/show_source.rb, line 98
def header_options
  super.merge signature: true
end
options(opt) click to toggle source
Calls superclass method Pry::Command::ShowInfo#options
# File lib/pry/commands/show_source.rb, line 31
def options(opt)
  opt.on :e, :eval, "evaluate the command's argument as a ruby " \
                    "expression and show the class its return value"
  opt.on :d, :doc, 'include documentation in the output'
  super(opt)
end
process() click to toggle source
Calls superclass method Pry::Command::ShowInfo#process
# File lib/pry/commands/show_source.rb, line 38
def process
  if opts.present?(:e)
    obj = target.eval(args.first)
    self.args = Array.new(1) { obj.is_a?(Module) ? obj.name : obj.class.name }
  end

  super
end
render_doc_markup_for(code_object) click to toggle source

process the markup (if necessary) and apply colors

# File lib/pry/commands/show_source.rb, line 66
def render_doc_markup_for(code_object)
  docs = docs_for(code_object)

  if code_object.command?
    # command '--help' shouldn't use markup highlighting
    docs
  else
    if docs.empty?
      raise CommandError, "No docs found for: #{obj_name || 'current context'}"
    end

    process_comment_markup(docs)
  end
end
start_line_for(code_object) click to toggle source

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 @return [Fixnum] start line of docs

# File lib/pry/commands/show_source.rb, line 105
def start_line_for(code_object)
  return 1 if code_object.command? || opts.present?(:'base-one')
  return 1 unless code_object.source_line

  code_object.source_line - code_object.doc.lines.count
end