From c5004b71b1c608ddd144861b343f439effe3070e Mon Sep 17 00:00:00 2001 From: Nathan Sobo Date: Tue, 3 Apr 2012 11:27:49 -0600 Subject: [PATCH] Selections are only cleared when cursor is explicitly moved This allows multiple selections to be modified without changes to an earlier selection clearing later selections. It means we can remove the `modifySelections` method from `CompositeSelection` and just call methods on selections in a loop. --- spec/atom/editor-spec.coffee | 2 +- src/atom/composite-cursor.coffee | 1 - src/atom/composite-selection.coffee | 21 ++++++++------------- src/atom/cursor.coffee | 6 ++++++ src/atom/selection.coffee | 7 ++----- 5 files changed, 17 insertions(+), 20 deletions(-) diff --git a/spec/atom/editor-spec.coffee b/spec/atom/editor-spec.coffee index e54f270eb..190eb234d 100644 --- a/spec/atom/editor-spec.coffee +++ b/spec/atom/editor-spec.coffee @@ -1893,4 +1893,4 @@ describe "Editor", -> eventHandler.reset() editor.buffer.setPath("new.txt") - expect(eventHandler).toHaveBeenCalled() \ No newline at end of file + expect(eventHandler).toHaveBeenCalled() diff --git a/src/atom/composite-cursor.coffee b/src/atom/composite-cursor.coffee index ac852e926..ac3166557 100644 --- a/src/atom/composite-cursor.coffee +++ b/src/atom/composite-cursor.coffee @@ -21,7 +21,6 @@ class CompositeCursor cursor = new Cursor(@editor) @cursors.push(cursor) @editor.lines.append(cursor) - @editor.addSelectionForCursor(cursor) cursor addCursorAtScreenPosition: (screenPosition) -> diff --git a/src/atom/composite-selection.coffee b/src/atom/composite-selection.coffee index d85e03d48..05ba22dfe 100644 --- a/src/atom/composite-selection.coffee +++ b/src/atom/composite-selection.coffee @@ -35,6 +35,7 @@ class CompositeSeleciton selection = new Selection({@editor, cursor}) @selections.push(selection) @editor.lines.append(selection) + selection addSelectionForBufferRange: (bufferRange, options) -> cursor = @editor.compositeCursor.addCursor() @@ -66,26 +67,20 @@ class CompositeSeleciton fn(selection) for selection in @getSelections() @mergeIntersectingSelections(reverse: true) - modifySelectedText: (fn) -> - selection.retainSelection = true for selection in @getSelections() - for selection in @getSelections() - selection.retainSelection = false - fn(selection) - insertText: (text) -> - @modifySelectedText (selection) -> selection.insertText(text) + selection.insertText(text) for selection in @getSelections() backspace: -> - @modifySelectedText (selection) -> selection.backspace() + selection.backspace() for selection in @getSelections() backspaceToBeginningOfWord: -> - @modifySelectedText (selection) -> selection.backspaceToBeginningOfWord() + selection.backspaceToBeginningOfWord() for selection in @getSelections() delete: -> - @modifySelectedText (selection) -> selection.delete() + selection.delete() for selection in @getSelections() deleteToEndOfWord: -> - @modifySelectedText (selection) -> selection.deleteToEndOfWord() + selection.deleteToEndOfWord() for selection in @getSelections() selectToScreenPosition: (position) -> @getLastSelection().selectToScreenPosition(position) @@ -122,7 +117,7 @@ class CompositeSeleciton cut: -> maintainPasteboard = false - @modifySelectedText (selection) -> + for selection in @getSelections() selection.cut(maintainPasteboard) maintainPasteboard = true @@ -140,4 +135,4 @@ class CompositeSeleciton if selection.intersectsWith(otherSelection) selection.merge(otherSelection, options) @mergeIntersectingSelections(options) - return \ No newline at end of file + return diff --git a/src/atom/cursor.coffee b/src/atom/cursor.coffee index 1085c74be..93a305fd4 100644 --- a/src/atom/cursor.coffee +++ b/src/atom/cursor.coffee @@ -14,6 +14,7 @@ class Cursor extends View initialize: (@editor) -> @anchor = new Anchor(@editor) + @selection = @editor.compositeSelection.addSelectionForCursor(this) @one 'attach', => @updateAppearance() handleBufferChange: (e) -> @@ -31,6 +32,7 @@ class Cursor extends View setBufferPosition: (bufferPosition) -> @anchor.setBufferPosition(bufferPosition) @refreshScreenPosition() + @clearSelection() getScreenPosition: -> @anchor.getScreenPosition() @@ -38,6 +40,7 @@ class Cursor extends View setScreenPosition: (position, options={}) -> @anchor.setScreenPosition(position, options) @refreshScreenPosition(position, options) + @clearSelection() refreshScreenPosition: -> @goalColumn = null @@ -48,6 +51,9 @@ class Cursor extends View window.clearTimeout(@idleTimeout) if @idleTimeout @idleTimeout = window.setTimeout (=> @addClass 'idle'), 200 + clearSelection: -> + @selection.clearSelection() unless @selection.retainSelection + getCurrentBufferLine: -> @editor.lineForBufferRow(@getBufferPosition().row) diff --git a/src/atom/selection.coffee b/src/atom/selection.coffee index 274468e7d..6d82852c1 100644 --- a/src/atom/selection.coffee +++ b/src/atom/selection.coffee @@ -16,11 +16,7 @@ class Selection extends View initialize: ({@editor, @cursor}) -> @regions = [] - @cursor.on 'cursor:position-changed', => - if @retainSelection - @updateAppearance() - else - @clearSelection() + @cursor.on 'cursor:position-changed', => @updateAppearance() handleBufferChange: (e) -> return unless @anchor @@ -106,6 +102,7 @@ class Selection extends View { text, shouldOutdent } = @autoIndentText(text) @editor.buffer.change(@getBufferRange(), text) @autoOutdentText() if shouldOutdent + @clearSelection() autoIndentText: (text) -> if @editor.autoIndent