diff --git a/spec/app/point-spec.coffee b/spec/app/point-spec.coffee index 322bac5f2..d4d78715c 100644 --- a/spec/app/point-spec.coffee +++ b/spec/app/point-spec.coffee @@ -25,3 +25,9 @@ describe "Point", -> expect(new Point(5, 0).compare(new Point(6, 1))).toBe -1 expect(new Point(5, 5).compare(new Point(4, 1))).toBe 1 expect(new Point(5, 5).compare(new Point(5, 3))).toBe 1 + + describe ".translate(other)", -> + it "returns a translated point", -> + expect(new Point(1,2).translate([2,4])).toEqual [3,6] + expect(new Point(1,2).translate([-1])).toEqual [0,2] + expect(new Point(1,2).translate([0,-2])).toEqual [1,0] diff --git a/spec/app/range-spec.coffee b/spec/app/range-spec.coffee index 780962a5f..c73111ace 100644 --- a/spec/app/range-spec.coffee +++ b/spec/app/range-spec.coffee @@ -32,3 +32,8 @@ describe "Range", -> expect(new Range([2, 1], [3, 10]).union(new Range([1, 1], [2, 10]))).toEqual [[1, 1], [3, 10]] expect(new Range([2, 1], [3, 10]).union(new Range([2, 5], [3, 1]))).toEqual [[2, 1], [3, 10]] expect(new Range([2, 5], [3, 1]).union(new Range([2, 1], [3, 10]))).toEqual [[2, 1], [3, 10]] + + describe ".translate(startPoint, endPoint)", -> + it "returns a range translates by the specified start and end points", -> + expect(new Range([1, 1], [2, 10]).translate([1])).toEqual [[2, 1], [3, 10]] + expect(new Range([1, 1], [2, 10]).translate([1,2], [3,4])).toEqual [[2, 3], [5, 14]] diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index f0be412a5..c3eaa685a 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -369,9 +369,7 @@ class EditSession @foldBufferRow(foldedRow) for foldedRow in foldedRows - newStartPosition = [selection.start.row - 1, selection.start.column] - newEndPosition = [selection.end.row - 1, selection.end.column] - @setSelectedBufferRange([newStartPosition, newEndPosition], preserveFolds: true) + @setSelectedBufferRange(selection.translate([-1]), preserveFolds: true) moveLineDown: -> selection = @getSelectedBufferRange() @@ -408,9 +406,7 @@ class EditSession @foldBufferRow(foldedRow) for foldedRow in foldedRows - newStartPosition = [selection.start.row + 1, selection.start.column] - newEndPosition = [selection.end.row + 1, selection.end.column] - @setSelectedBufferRange([newStartPosition, newEndPosition], preserveFolds: true) + @setSelectedBufferRange(selection.translate([1]), preserveFolds: true) mutateSelectedText: (fn) -> diff --git a/src/app/point.coffee b/src/app/point.coffee index b740027f6..e471ad6c6 100644 --- a/src/app/point.coffee +++ b/src/app/point.coffee @@ -34,6 +34,10 @@ class Point new Point(row, column) + translate: (other) -> + other = Point.fromObject(other) + new Point(@row + other.row, @column + other.column) + splitAt: (column) -> if @row == 0 rightColumn = @column - column diff --git a/src/app/range.coffee b/src/app/range.coffee index 3464a3bce..136f52f15 100644 --- a/src/app/range.coffee +++ b/src/app/range.coffee @@ -48,6 +48,9 @@ class Range add: (point) -> new Range(@start.add(point), @end.add(point)) + translate: (startPoint, endPoint=startPoint) -> + new Range(@start.translate(startPoint), @end.translate(endPoint)) + intersectsWith: (otherRange) -> if @start.isLessThanOrEqual(otherRange.start) @end.isGreaterThanOrEqual(otherRange.start)