From e62e062f9b9dd4af61b78ba991a2f40b90fe2470 Mon Sep 17 00:00:00 2001 From: Corey Johnson & Nathan Sobo Date: Tue, 27 Mar 2012 11:26:36 -0700 Subject: [PATCH] Maintain selection directionality when merging selections with keyboard --- spec/atom/editor-spec.coffee | 4 ++++ spec/atom/selection-spec.coffee | 9 +++++++++ src/atom/composite-selection.coffee | 10 +++++----- src/atom/selection.coffee | 7 +++++-- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index c84f20dac..97902cec8 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -1077,6 +1077,7 @@ describe "Editor", -> editor.selectDown() expect(editor.compositeSelection.getSelections()).toEqual [selection1] expect(selection1.getScreenRange()).toEqual([[0, 9], [4, 25]]) + expect(selection1.isReversed()).toBeFalsy() expect(selection2.parent()).not.toExist() expect(selection3.parent()).not.toExist() @@ -1088,6 +1089,7 @@ describe "Editor", -> editor.selectUp() expect(editor.compositeSelection.getSelections()).toEqual [selection1] expect(selection1.getScreenRange()).toEqual([[0, 0], [1, 20]]) + expect(selection1.isReversed()).toBeTruthy() expect(selection2.parent()).not.toExist() it "merges selections when they intersect when moving left", -> @@ -1098,6 +1100,7 @@ describe "Editor", -> editor.selectLeft() expect(editor.compositeSelection.getSelections()).toEqual [selection1] expect(selection1.getScreenRange()).toEqual([[0, 8], [1, 20]]) + expect(selection1.isReversed()).toBeTruthy() expect(selection2.parent()).not.toExist() it "merges selections when they intersect when moving right", -> @@ -1108,6 +1111,7 @@ describe "Editor", -> editor.selectRight() expect(editor.compositeSelection.getSelections()).toEqual [selection1] expect(selection1.getScreenRange()).toEqual([[0, 9], [1, 21]]) + expect(selection1.isReversed()).toBeFalsy() expect(selection2.parent()).not.toExist() describe "cursor merging", -> diff --git a/spec/atom/selection-spec.coffee b/spec/atom/selection-spec.coffee index a56facbcd..158770d6f 100644 --- a/spec/atom/selection-spec.coffee +++ b/spec/atom/selection-spec.coffee @@ -205,3 +205,12 @@ describe "Selection", -> editor.setCursorScreenPosition [0,2] selection.selectLine(1) expect(selection.getText()).toBe " var sort = function(items) {" + + describe ".isReversed()", -> + it "returns true if the cursor precedes the anchor", -> + selection.cursor.setScreenPosition([0, 20]) + selection.selectToScreenPosition([0, 10]) + expect(selection.isReversed()).toBeTruthy() + + selection.selectToScreenPosition([0, 25]) + expect(selection.isReversed()).toBeFalsy() diff --git a/src/atom/composite-selection.coffee b/src/atom/composite-selection.coffee index 15ce96c5b..7d0297739 100644 --- a/src/atom/composite-selection.coffee +++ b/src/atom/composite-selection.coffee @@ -49,11 +49,11 @@ class CompositeSeleciton selectLeft: -> selection.selectLeft() for selection in @getSelections() - @mergeIntersectingSelections() + @mergeIntersectingSelections reverse: true selectUp: -> selection.selectUp() for selection in @getSelections() - @mergeIntersectingSelections() + @mergeIntersectingSelections reverse: true selectDown: -> selection.selectDown() for selection in @getSelections() @@ -71,14 +71,14 @@ class CompositeSeleciton lastSelection: -> _.last(@selections) - mergeIntersectingSelections: -> + mergeIntersectingSelections: (options) -> for selection in @getSelections() otherSelections = @getSelections() _.remove(otherSelections, selection) for otherSelection in otherSelections if selection.intersectsWith(otherSelection) - selection.merge(otherSelection) - @mergeIntersectingSelections() + selection.merge(otherSelection, options) + @mergeIntersectingSelections(options) return modifySelectedText: (fn) -> diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index fac11fa19..984bccd83 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -139,11 +139,14 @@ class Selection extends View isEmpty: -> @getBufferRange().isEmpty() + isReversed: -> + @cursor.getBufferPosition().isLessThan(@anchorBufferPosition) + intersectsWith: (otherSelection) -> @getScreenRange().intersectsWith(otherSelection.getScreenRange()) - merge: (otherSelection) -> - @setScreenRange(@getScreenRange().union(otherSelection.getScreenRange())) + merge: (otherSelection, options) -> + @setScreenRange(@getScreenRange().union(otherSelection.getScreenRange()), options) otherSelection.remove() remove: ->