From 31bd38dfcc40048ff933b2945cd1a26b56bb80ec Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Fri, 8 Jun 2012 13:26:54 -0600 Subject: [PATCH] Move merging of intersecting selections into EditSession and the Selection model --- src/app/composite-selection.coffee | 16 +++------------- src/app/cursor-view.coffee | 1 - src/app/edit-session.coffee | 10 ++++++++++ src/app/selection-view.coffee | 16 +++++++--------- src/app/selection.coffee | 18 +++++++++++++++--- 5 files changed, 35 insertions(+), 26 deletions(-) diff --git a/src/app/composite-selection.coffee b/src/app/composite-selection.coffee index 02f609310..247145127 100644 --- a/src/app/composite-selection.coffee +++ b/src/app/composite-selection.coffee @@ -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) diff --git a/src/app/cursor-view.coffee b/src/app/cursor-view.coffee index c47c03eec..81f8aec1a 100644 --- a/src/app/cursor-view.coffee +++ b/src/app/cursor-view.coffee @@ -30,7 +30,6 @@ class CursorView extends View remove: -> @editor.compositeCursor.removeCursor(this) - @editor.compositeSelection.removeSelectionViewForCursor(@cursor) @cursor.off() super diff --git a/src/app/edit-session.coffee b/src/app/edit-session.coffee index 064304a67..e1a1f7031 100644 --- a/src/app/edit-session.coffee +++ b/src/app/edit-session.coffee @@ -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) diff --git a/src/app/selection-view.coffee b/src/app/selection-view.coffee index c6efbcdcb..204078ddd 100644 --- a/src/app/selection-view.coffee +++ b/src/app/selection-view.coffee @@ -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) -> diff --git a/src/app/selection.coffee b/src/app/selection.coffee index 839c02aad..52694e221 100644 --- a/src/app/selection.coffee +++ b/src/app/selection.coffee @@ -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