From 5083c18c843d84b0fae93ad1be27e6bb28a44dac Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 15:37:41 -0700 Subject: [PATCH 1/7] Add cursor to onDidChangeCursorPosition event object --- spec/editor-spec.coffee | 14 ++++++++++++++ src/cursor.coffee | 1 + 2 files changed, 15 insertions(+) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index f196bec78..1c2dfbd13 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -174,6 +174,20 @@ describe "Editor", -> editor.moveDown() expect(editor.getCursorBufferPosition()).toEqual [1, 1] + it "emits an event with the old position, new position, and the cursor that moved", -> + editor.onDidChangeCursorPosition positionChangedHandler = jasmine.createSpy() + + editor.setCursorBufferPosition([2, 4]) + + expect(positionChangedHandler).toHaveBeenCalled() + eventObject = positionChangedHandler.mostRecentCall.args[0] + + expect(eventObject.oldBufferPosition).toEqual [0, 0] + expect(eventObject.oldScreenPosition).toEqual [0, 0] + expect(eventObject.newBufferPosition).toEqual [2, 4] + expect(eventObject.newScreenPosition).toEqual [2, 4] + expect(eventObject.cursor).toBe editor.getLastCursor() + describe ".setCursorScreenPosition(screenPosition)", -> it "clears a goal column established by vertical movement", -> # set a goal column by moving down diff --git a/src/cursor.coffee b/src/cursor.coffee index 112065ff1..488271799 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -42,6 +42,7 @@ class Cursor extends Model newBufferPosition: newHeadBufferPosition newScreenPosition: newHeadScreenPosition textChanged: textChanged + cursor: this @emit 'moved', movedEvent @emitter.emit 'did-change-position' From e5c03e139a39a515ef8933178fb16a448cddf49f Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 15:50:40 -0700 Subject: [PATCH 2/7] Only pass event to the editor --- src/cursor.coffee | 2 +- src/editor.coffee | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cursor.coffee b/src/cursor.coffee index 488271799..db377d38f 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -46,7 +46,7 @@ class Cursor extends Model @emit 'moved', movedEvent @emitter.emit 'did-change-position' - @editor.cursorMoved(this, movedEvent) + @editor.cursorMoved(movedEvent) @marker.onDidDestroy => @destroyed = true @editor.removeCursor(this) diff --git a/src/editor.coffee b/src/editor.coffee index a4226afaa..7ca0bf6ee 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -1604,7 +1604,7 @@ class Editor extends Model fn(cursor) for cursor in @getCursors() @mergeCursors() - cursorMoved: (cursor, event) -> + cursorMoved: (event) -> @emit 'cursor-moved', event @emitter.emit 'did-change-cursor-position', event From 3134364362e01401714a80c34422f8b5d7566f10 Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 15:50:47 -0700 Subject: [PATCH 3/7] Add cursor to the docs --- src/editor.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/editor.coffee b/src/editor.coffee index 7ca0bf6ee..f2bc749bd 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -226,6 +226,7 @@ class Editor extends Model # * `newBufferPosition` {Point} # * `newScreenPosition` {Point} # * `textChanged` {Boolean} + # * `cursor` {Cursor} that triggered the event # # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidChangeCursorPosition: (callback) -> From c62fb26001bce4a516bf587f7a76d62e0b8d6a3c Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 16:08:12 -0700 Subject: [PATCH 4/7] onDidChangeSelectionRange emits object with ranges + selection --- spec/editor-spec.coffee | 16 ++++++++++++++++ src/editor.coffee | 6 +++--- src/selection.coffee | 18 ++++++++++++++---- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/spec/editor-spec.coffee b/spec/editor-spec.coffee index 1c2dfbd13..67ded117f 100644 --- a/spec/editor-spec.coffee +++ b/spec/editor-spec.coffee @@ -901,6 +901,22 @@ describe "Editor", -> beforeEach -> selection = editor.getLastSelection() + describe "when the selection range changes", -> + it "emits an event with the old range, new range, and the selection that moved", -> + editor.setSelectedBufferRange([[3, 0], [4, 5]]) + + editor.onDidChangeSelectionRange rangeChangedHandler = jasmine.createSpy() + editor.selectToBufferPosition([6, 2]) + + expect(rangeChangedHandler).toHaveBeenCalled() + eventObject = rangeChangedHandler.mostRecentCall.args[0] + + expect(eventObject.oldBufferRange).toEqual [[3, 0], [4, 5]] + expect(eventObject.oldScreenRange).toEqual [[3, 0], [4, 5]] + expect(eventObject.newBufferRange).toEqual [[3, 0], [6, 2]] + expect(eventObject.newScreenRange).toEqual [[3, 0], [6, 2]] + expect(eventObject.selection).toBe selection + describe ".selectUp/Down/Left/Right()", -> it "expands each selection to its cursor's new location", -> editor.setSelectedBufferRanges([[[0,9], [0,13]], [[3,16], [3,21]]]) diff --git a/src/editor.coffee b/src/editor.coffee index f2bc749bd..a54b6e3eb 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -2088,9 +2088,9 @@ class Editor extends Model false # Called by the selection - selectionRangeChanged: (selection) -> - @emit 'selection-screen-range-changed', selection - @emitter.emit 'did-change-selection-range', selection + selectionRangeChanged: (event) -> + @emit 'selection-screen-range-changed', event + @emitter.emit 'did-change-selection-range', event ### Section: Searching and Replacing diff --git a/src/selection.coffee b/src/selection.coffee index 38c69b401..60e1d1da7 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -21,7 +21,7 @@ class Selection extends Model @cursor.selection = this @decoration = @editor.decorateMarker(@marker, type: 'highlight', class: 'selection') - @marker.onDidChange => @screenRangeChanged() + @marker.onDidChange (e) => @screenRangeChanged(e) @marker.onDidDestroy => unless @editor.isDestroyed() @destroyed = true @@ -686,10 +686,20 @@ class Selection extends Model Section: Private Utilities ### - screenRangeChanged: -> - @emit 'screen-range-changed', @getScreenRange() + screenRangeChanged: (e) -> + {oldHeadBufferPosition, oldTailBufferPosition} = e + {oldHeadScreenPosition, oldTailScreenPosition} = e + + eventObject = + oldBufferRange: new Range(oldHeadBufferPosition, oldTailBufferPosition) + oldScreenRange: new Range(oldHeadScreenPosition, oldTailScreenPosition) + newBufferRange: @getBufferRange() + newScreenRange: @getScreenRange() + selection: this + + @emit 'screen-range-changed', @getScreenRange() # old event @emitter.emit 'did-change-range' - @editor.selectionRangeChanged(this) + @editor.selectionRangeChanged(eventObject) finalize: -> @initialScreenRange = null unless @initialScreenRange?.isEqual(@getScreenRange()) From 91a443e7cb0749464848a04b719d1692cd0bccca Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 16:09:32 -0700 Subject: [PATCH 5/7] Update onDidChangeSelectionRange doc string --- src/editor.coffee | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/editor.coffee b/src/editor.coffee index a54b6e3eb..70cd5c0e2 100644 --- a/src/editor.coffee +++ b/src/editor.coffee @@ -235,7 +235,12 @@ class Editor extends Model # Essential: Calls your `callback` when a selection's screen range changes. # # * `callback` {Function} - # * `selection` {Selection} that moved + # * `event` {Object} + # * `oldBufferRange` {Range} + # * `oldScreenRange` {Range} + # * `newBufferRange` {Range} + # * `newScreenRange` {Range} + # * `selection` {Selection} that triggered the event # # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidChangeSelectionRange: (callback) -> From 688b20900043a135bec1dc3c95644e9fccdad7ad Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 16:12:19 -0700 Subject: [PATCH 6/7] Update doc string in Selection::onDidChangeRange --- src/selection.coffee | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/selection.coffee b/src/selection.coffee index 60e1d1da7..f17455cb9 100644 --- a/src/selection.coffee +++ b/src/selection.coffee @@ -40,7 +40,12 @@ class Selection extends Model # Extended: Calls your `callback` when the selection was moved. # # * `callback` {Function} - # * `screenRange` {Range} indicating the new screenrange + # * `event` {Object} + # * `oldBufferRange` {Range} + # * `oldScreenRange` {Range} + # * `newBufferRange` {Range} + # * `newScreenRange` {Range} + # * `selection` {Selection} that triggered the event # # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidChangeRange: (callback) -> From 021208d933cb98009f1e70a09c29a8cde83235eb Mon Sep 17 00:00:00 2001 From: Ben Ogle Date: Mon, 22 Sep 2014 16:12:51 -0700 Subject: [PATCH 7/7] Update Cursor::onDidChangePosition doc string --- src/cursor.coffee | 1 + 1 file changed, 1 insertion(+) diff --git a/src/cursor.coffee b/src/cursor.coffee index db377d38f..8947a3a78 100644 --- a/src/cursor.coffee +++ b/src/cursor.coffee @@ -71,6 +71,7 @@ class Cursor extends Model # * `newBufferPosition` {Point} # * `newScreenPosition` {Point} # * `textChanged` {Boolean} + # * `Cursor` {Cursor} that triggered the event # # Returns a {Disposable} on which `.dispose()` can be called to unsubscribe. onDidChangePosition: (callback) ->