diff --git a/lib/pry/code/loc.rb b/lib/pry/code/loc.rb index 905c7040..a65a7718 100644 --- a/lib/pry/code/loc.rb +++ b/lib/pry/code/loc.rb @@ -1,10 +1,12 @@ class Pry class Code - # Represents a line of code. A line of code is a tuple, which consists of a - # line and a line number. A `LOC` object's state (namely, the line - # parameter) can be changed via instance methods. `Pry::Code` heavily uses - # this class. + # Represents a line of code (which may, in fact, contain multiple lines if the + # entirety was eval'd as a single unit following the `edit` command). + # + # A line of code is a tuple, which consists of a line and a line number. A + # `LOC` object's state (namely, the line parameter) can be changed via + # instance methods. `Pry::Code` heavily uses this class. # # @api private # @example @@ -62,7 +64,8 @@ class Pry def add_line_number(max_width = 0, color = false) padded = lineno.to_s.rjust(max_width) colorized_lineno = color ? Pry::Helpers::BaseHelpers.colorize_code(padded) : padded - tuple[0] = "#{ colorized_lineno }: #{ line }" + properly_padded_line = handle_multiline_entries_from_edit_command(line, max_width) + tuple[0] = "#{ colorized_lineno }: #{ properly_padded_line }" end # Prepends a marker "=>" or an empty marker to the +line+. @@ -86,6 +89,12 @@ class Pry def indent(distance) tuple[0] = "#{ ' ' * distance }#{ line }" end + + def handle_multiline_entries_from_edit_command(line, max_width) + line.split("\n").map.with_index do |inner_line, i| + i.zero? ? inner_line : "#{' '* (max_width + 2)}#{inner_line}" + end.join("\n") + end end end diff --git a/spec/code_spec.rb b/spec/code_spec.rb index 2a6d548b..7b78157f 100644 --- a/spec/code_spec.rb +++ b/spec/code_spec.rb @@ -195,6 +195,13 @@ describe Pry::Code do expect(@code).to match(/1:/) end + specify 'pad multiline units created with edit command' do + multiline_unit = "def am_i_pretty?\n 'yes i am'\n end" + code = Pry::Code.new(multiline_unit).with_line_numbers + middle_line = code.split("\n")[1] + expect(middle_line).to match(/2: 'yes i am'/) + end + specify 'disable line numbers when falsy' do @code = @code.with_line_numbers @code = @code.with_line_numbers(false)