Move merging of intersecting selections into EditSession and the Selection model

This commit is contained in:
Nathan Sobo
2012-06-08 13:26:54 -06:00
parent 7eb01272ed
commit 31bd38dfcc
5 changed files with 35 additions and 26 deletions

View File

@@ -51,11 +51,8 @@ class CompositeSeleciton
cursor = @editor.activeEditSession.addCursor()
@selectionForCursor(cursor).setBufferRange(bufferRange, options)
removeSelectionViewForCursor: (cursor) ->
if view = @selectionViewForCursor(cursor)
view.cursor = null
view.remove()
_.remove(@selections, view)
removeSelectionView: (selectionView) ->
_.remove(@selections, selectionView)
selectionForCursor: (cursor) ->
_.find @selections, (selection) -> selection.cursor == cursor
@@ -175,11 +172,4 @@ class CompositeSeleciton
maintainPasteboard = true
mergeIntersectingSelections: (options) ->
for selection in @getSelections()
otherSelections = @getSelections()
_.remove(otherSelections, selection)
for otherSelection in otherSelections
if selection.intersectsWith(otherSelection)
selection.merge(otherSelection, options)
@mergeIntersectingSelections(options)
return
@editor.activeEditSession.mergeIntersectingSelections(options)

View File

@@ -30,7 +30,6 @@ class CursorView extends View
remove: ->
@editor.compositeCursor.removeCursor(this)
@editor.compositeSelection.removeSelectionViewForCursor(@cursor)
@cursor.off()
super

View File

@@ -208,4 +208,14 @@ class EditSession
else
positions.push(position)
mergeIntersectingSelections: (options) ->
for selection in @getSelections()
otherSelections = @getSelections()
_.remove(otherSelections, selection)
for otherSelection in otherSelections
if selection.intersectsWith(otherSelection)
selection.merge(otherSelection, options)
@mergeIntersectingSelections(options)
return
_.extend(EditSession.prototype, EventEmitter)

View File

@@ -20,6 +20,10 @@ class SelectionView extends View
@selection.on 'change-screen-range', =>
@updateAppearance()
@selection.on 'destroy', =>
@selection = null
@remove()
handleBufferChange: (e) ->
return unless @anchor
@anchor.handleBufferChange(e)
@@ -36,11 +40,8 @@ class SelectionView extends View
isReversed: ->
@selection.isReversed()
intersectsWith: (otherSelection) ->
@getScreenRange().intersectsWith(otherSelection.getScreenRange())
clearSelection: ->
@selection.clear()
@selection?.clear()
updateAppearance: ->
return unless @cursor
@@ -136,12 +137,9 @@ class SelectionView extends View
@editor.buffer.delete(range) unless range.isEmpty()
@clearSelection()
merge: (otherSelection, options) ->
@setScreenRange(@getScreenRange().union(otherSelection.getScreenRange()), options)
otherSelection.remove()
remove: ->
@cursor?.destroy()
@editor.compositeSelection.removeSelectionView(this)
@selection?.destroy()
super
modifySelection: (fn) ->

View File

@@ -9,14 +9,19 @@ class Selection
anchor: null
constructor: ({@cursor, @editSession}) ->
@cursor.on 'change-screen-position', (e) =>
@cursor.on 'change-screen-position.selection', (e) =>
@trigger 'change-screen-range', @getScreenRange() unless e.bufferChanged
@cursor.on 'destroy', => @destroy()
@cursor.on 'destroy.selection', =>
@cursor = null
@destroy()
destroy: ->
@cursor.off()
if @cursor
@cursor.off('.selection')
@cursor.destroy()
@editSession.removeSelection(this)
@trigger 'destroy'
getScreenRange: ->
if @anchor
@@ -103,4 +108,11 @@ class Selection
@anchor = new Anchor(@editSession)
@anchor.setScreenPosition(@cursor.getScreenPosition())
intersectsWith: (otherSelection) ->
@getScreenRange().intersectsWith(otherSelection.getScreenRange())
merge: (otherSelection, options) ->
@setScreenRange(@getScreenRange().union(otherSelection.getScreenRange()), options)
otherSelection.destroy()
_.extend Selection.prototype, EventEmitter