mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
fix line/index confusion in Code#between
This commit is contained in:
parent
b177aa9aa1
commit
8879dfc6de
2 changed files with 21 additions and 5 deletions
|
@ -138,7 +138,8 @@ class Pry
|
||||||
end
|
end
|
||||||
|
|
||||||
# Remove all lines that aren't in the given range, expressed either as a
|
# Remove all lines that aren't in the given range, expressed either as a
|
||||||
# `Range` object or a first and last line number (inclusive).
|
# `Range` object or a first and last line number (inclusive). Negative
|
||||||
|
# indices count from the end of the array of lines.
|
||||||
#
|
#
|
||||||
# @param [Range, Fixnum] start_line
|
# @param [Range, Fixnum] start_line
|
||||||
# @param [Fixnum?] end_line
|
# @param [Fixnum?] end_line
|
||||||
|
@ -155,12 +156,20 @@ class Pry
|
||||||
end_line ||= start_line
|
end_line ||= start_line
|
||||||
end
|
end
|
||||||
|
|
||||||
start_line -= 1 unless start_line < 1
|
if start_line > 0
|
||||||
end_line -= 1 unless end_line < 1
|
start_idx = @lines.index { |l| l.last >= start_line } || @lines.length
|
||||||
range = start_line..end_line
|
else
|
||||||
|
start_idx = start_line
|
||||||
|
end
|
||||||
|
|
||||||
|
if end_line > 0
|
||||||
|
end_idx = (@lines.index { |l| l.last > end_line } || 0) - 1
|
||||||
|
else
|
||||||
|
end_idx = end_line
|
||||||
|
end
|
||||||
|
|
||||||
alter do
|
alter do
|
||||||
@lines = @lines[range] || []
|
@lines = @lines[start_idx..end_idx] || []
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -92,6 +92,13 @@ describe Pry::Code do
|
||||||
@code.should =~ /\A def self/
|
@code.should =~ /\A def self/
|
||||||
@code.should =~ /world!'\Z/
|
@code.should =~ /world!'\Z/
|
||||||
end
|
end
|
||||||
|
|
||||||
|
should 'use real line numbers for positive indices' do
|
||||||
|
@code = @code.after(3, 3)
|
||||||
|
@code = @code.between(4, 4)
|
||||||
|
@code.length.should == 1
|
||||||
|
@code.should =~ /\A end\Z/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe '#before' do
|
describe '#before' do
|
||||||
|
|
Loading…
Add table
Reference in a new issue