mirror of
https://github.com/atom/atom.git
synced 2026-01-23 22:08:08 -05:00
Compare against EOF position when clipping
Previously the column could potentially be set to zero since a Math.min comparison was used for the length of an already adjusted row.
This commit is contained in:
@@ -889,3 +889,17 @@ describe 'Buffer', ->
|
||||
buffer.setText("\ninitialtext")
|
||||
buffer.append("hello\n1\r\n2\n")
|
||||
expect(buffer.getText()).toBe "\ninitialtexthello\n1\n2\n"
|
||||
|
||||
describe ".clipPosition(position)", ->
|
||||
describe "when the position is before the start of the buffer", ->
|
||||
it "returns the first position in the buffer", ->
|
||||
expect(buffer.clipPosition([-1,0])).toEqual [0,0]
|
||||
expect(buffer.clipPosition([0,-1])).toEqual [0,0]
|
||||
expect(buffer.clipPosition([-1,-1])).toEqual [0,0]
|
||||
|
||||
describe "when the position is after the end of the buffer", ->
|
||||
it "returns the last position in the buffer", ->
|
||||
buffer.setText('some text')
|
||||
expect(buffer.clipPosition([1, 0])).toEqual [0,9]
|
||||
expect(buffer.clipPosition([0,10])).toEqual [0,9]
|
||||
expect(buffer.clipPosition([10,Infinity])).toEqual [0,9]
|
||||
|
||||
@@ -212,13 +212,15 @@ class Buffer
|
||||
range
|
||||
|
||||
clipPosition: (position) ->
|
||||
{ row, column } = Point.fromObject(position)
|
||||
row = 0 if row < 0
|
||||
column = 0 if column < 0
|
||||
row = Math.min(@getLastRow(), row)
|
||||
column = Math.min(@lineLengthForRow(row), column)
|
||||
|
||||
new Point(row, column)
|
||||
position = Point.fromObject(position)
|
||||
eofPosition = @getEofPosition()
|
||||
if position.isGreaterThan(eofPosition)
|
||||
eofPosition
|
||||
else
|
||||
row = Math.max(position.row, 0)
|
||||
column = Math.max(position.column, 0)
|
||||
column = Math.min(@lineLengthForRow(row), column)
|
||||
new Point(row, column)
|
||||
|
||||
clipRange: (range) ->
|
||||
range = Range.fromObject(range)
|
||||
|
||||
Reference in New Issue
Block a user