ruby/lib/irb/cmd/show_source.rb
Stan Lo 5d78ec8a94 [ruby/irb] Decouple edit and show_source commands
(https://github.com/ruby/irb/pull/658)

* Decouple `edit` command from `show_source`

2 commands should not depend on each other. If `edit` command also needs
to find a source, the source finding logic should be extracted into a
separate class.

* Return nil if is not an actual file path

* Refactor SourceFinder

9790517a0c
2023-07-31 19:57:36 +00:00

56 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require_relative "nop"
require_relative "../source_finder"
require_relative "../color"
module IRB
module ExtendCommand
class ShowSource < Nop
category "Context"
description "Show the source code of a given method or constant."
class << self
def transform_args(args)
# Return a string literal as is for backward compatibility
if args.empty? || string_literal?(args)
args
else # Otherwise, consider the input as a String for convenience
args.strip.dump
end
end
end
def execute(str = nil)
unless str.is_a?(String)
puts "Error: Expected a string but got #{str.inspect}"
return
end
source = SourceFinder.new(@irb_context).find_source(str)
if source
show_source(source)
else
puts "Error: Couldn't locate a definition for #{str}"
end
nil
end
private
def show_source(source)
puts
puts "#{bold("From")}: #{source.file}:#{source.first_line}"
puts
code = IRB::Color.colorize_code(File.read(source.file))
puts code.lines[(source.first_line - 1)...source.last_line].join
puts
end
def bold(str)
Color.colorize(str, [:BOLD])
end
end
end
end