Skip to content

Commit

Permalink
Reflect PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bcgraham committed Dec 1, 2021
1 parent 871a88e commit e6fcd2f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 17 deletions.
15 changes: 3 additions & 12 deletions lib/pry/indent.rb
Expand Up @@ -160,12 +160,11 @@ def indent(input)
before.times { prefix.sub! SPACES, '' }
new_prefix = prefix + SPACES * after

if !end_with_newline?(output) && already_indented?(output, prefix)
prefix = "\n" + prefix
unless previously_in_string
line = line.lstrip
line = prefix + line unless line.empty?
end

line = prefix + line.lstrip unless previously_in_string

output += line

prefix = new_prefix
Expand Down Expand Up @@ -280,14 +279,6 @@ def in_string?
!open_delimiters.empty?
end

def already_indented?(output, prefix)
!output.lines.to_a[-1].nil? && output.lines.to_a[-1].to_s.start_with?(prefix)
end

def end_with_newline?(output)
output.lines.to_a[-1].to_s.end_with?("\n")
end

# Given a string of Ruby code, use CodeRay to export the tokens.
#
# @param [String] string The Ruby to lex
Expand Down
7 changes: 2 additions & 5 deletions spec/indent_spec.rb
Expand Up @@ -102,7 +102,7 @@ def number
expect(@indent.indent(input)).to eq output
end

it 'should properly indent nested code with consecutive linebreaks' do
it 'strips empty lines' do
input = <<TXT.strip
class C
def foo
Expand All @@ -115,21 +115,18 @@ def bar
end
TXT

# rubocop:disable Layout/TrailingWhitespace
output = <<TXT.strip
class C
def foo
:foo
end
def bar
:bar
end
end
TXT
# rubocop:enable Layout/TrailingWhitespace

expect(@indent.indent(input).inspect).to eq output.inspect
expect(@indent.indent(input)).to eq output
end

it 'should indent statements such as if, else, etc' do
Expand Down

0 comments on commit e6fcd2f

Please sign in to comment.