Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lib/irb/source_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ def file_content

def colorized_content
if !binary_file? && file_exist?
end_line = find_end
# To correctly colorize, we need to colorize full content and extract the relevant lines.
colored = IRB::Color.colorize_code(file_content)
colored.lines[@line - 1...end_line].join
colored_lines = IRB::Color.colorize_code(file_content).lines

# Handle wrong line number case: line_no passed to eval is wrong, file is edited after load, etc
return if colored_lines.size < @line

colored_lines[@line - 1...find_end].join
elsif @ast_source
IRB::Color.colorize_code(@ast_source)
end
Expand Down
19 changes: 18 additions & 1 deletion test/irb/command/test_show_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def test_show_source_shows_binary_source
assert_match(%r[Defined in binary file:.+io/console], out)
end

def test_show_source_method_overrided
def test_show_source_method_overridden
write_ruby <<~RUBY
class Request
def method; 'POST'; end
Expand All @@ -393,6 +393,23 @@ def path; '/'; end
assert_match(%r[#{@ruby_file.to_path}:3\s+def path; '/'; end], out)
end

def test_show_source_with_wrong_line
write_ruby <<~RUBY.chomp
eval 'def foo; end', binding, __FILE__, 4 # Line 4 doesn't exist
binding.irb if
def bar; end # Line 3
RUBY

out = run_ruby_file do
type "show_source foo"
type "show_source bar"
type "exit"
end

assert_match(%r[#{@ruby_file.to_path}:4\s+Source not available], out)
assert_match(%r[#{@ruby_file.to_path}:3\s+def bar; end], out)
end

def test_show_source_with_constant_lookup
write_ruby <<~RUBY
X = 1
Expand Down