From ad3c18077cdb693f80a101436d785494e7ec71ea Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Tue, 5 Feb 2013 12:02:02 -0800 Subject: [PATCH] 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. --- spec/app/buffer-spec.coffee | 14 ++++++++++++++ src/app/buffer.coffee | 16 +++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/spec/app/buffer-spec.coffee b/spec/app/buffer-spec.coffee index b13c2b7c2..2a1eaa30c 100644 --- a/spec/app/buffer-spec.coffee +++ b/spec/app/buffer-spec.coffee @@ -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] diff --git a/src/app/buffer.coffee b/src/app/buffer.coffee index db7de2e75..62511d2f9 100644 --- a/src/app/buffer.coffee +++ b/src/app/buffer.coffee @@ -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)