1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Pad multiline units of the edit command consistently with numbering

This commit is contained in:
Jack Kinsella 2018-03-25 17:44:38 +02:00
parent f53f7940db
commit aa79e81669
2 changed files with 21 additions and 5 deletions

View file

@ -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

View file

@ -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)