Maintain selection directionality when merging selections with keyboard

This commit is contained in:
Corey Johnson & Nathan Sobo
2012-03-27 11:26:36 -07:00
parent 81869ebf59
commit e62e062f9b
4 changed files with 23 additions and 7 deletions

View File

@@ -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", ->

View File

@@ -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()

View File

@@ -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) ->

View File

@@ -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: ->