Add EditSession.consolidateSelections()

This commit is contained in:
Nathan Sobo
2013-04-05 12:33:27 -06:00
parent bd58834e7d
commit 2efed9f42c
2 changed files with 23 additions and 6 deletions

View File

@@ -792,6 +792,20 @@ describe "EditSession", ->
[[10, 0], [10, 0]]
]
describe ".consolidateSelections()", ->
it "destroys all selections but the most recent, returning true if any selections were destroyed", ->
editSession.setSelectedBufferRange([[3, 16], [3, 21]])
selection1 = editSession.getSelection()
selection2 = editSession.addSelectionForBufferRange([[3, 25], [3, 34]])
selection3 = editSession.addSelectionForBufferRange([[8, 4], [8, 10]])
expect(editSession.getSelections()).toEqual [selection1, selection2, selection3]
expect(editSession.consolidateSelections()).toBeTruthy()
expect(editSession.getSelections()).toEqual [selection3]
expect(selection3.isEmpty()).toBeFalsy()
expect(editSession.consolidateSelections()).toBeFalsy()
expect(editSession.getSelections()).toEqual [selection3]
describe "when the cursor is moved while there is a selection", ->
makeSelection = -> selection.setBufferRange [[1, 2], [1, 5]]

View File

@@ -626,13 +626,16 @@ class EditSession
_.remove(@selections, selection)
clearSelections: ->
lastSelection = @getLastSelection()
for selection in @getSelections() when selection != lastSelection
selection.destroy()
lastSelection.clear()
@consolidateSelections()
@getSelection().clear()
clearAllSelections: ->
selection.destroy() for selection in @getSelections()
consolidateSelections: ->
selections = @getSelections()
if selections.length > 1
selection.destroy() for selection in selections[0...-1]
true
else
false
getSelections: -> new Array(@selections...)