Add Editor::onDidMoveCursor

This commit is contained in:
Nathan Sobo
2014-09-04 09:29:11 -06:00
committed by Ben Ogle
parent 05ea381c0a
commit 2a81687d38
3 changed files with 11 additions and 6 deletions

View File

@@ -793,19 +793,20 @@ describe "Editor", ->
editor.setCursorBufferPosition([3, 1])
expect(editor.getCurrentParagraphBufferRange()).toBeUndefined()
describe "cursor-moved events", ->
describe "::onDidMoveCursor", ->
cursorMovedHandler = null
beforeEach ->
editor.foldBufferRow(4)
editor.setSelectedBufferRange([[8, 1], [9, 0]])
cursorMovedHandler = jasmine.createSpy("cursorMovedHandler")
editor.on 'cursor-moved', cursorMovedHandler
editor.onDidMoveCursor(cursorMovedHandler)
describe "when the position of the cursor changes", ->
it "emits a cursor-moved event", ->
it "notifies observers", ->
buffer.insert([9, 0], '...')
expect(cursorMovedHandler).toHaveBeenCalledWith(
cursor: editor.getLastCursor()
oldBufferPosition: [9, 0]
oldScreenPosition: [6, 0]
newBufferPosition: [9, 3]
@@ -814,7 +815,7 @@ describe "Editor", ->
)
describe "when the position of the associated selection's tail changes, but not the cursor's position", ->
it "does not emit a cursor-moved event", ->
it "does not notify observers", ->
buffer.insert([8, 0], '...')
expect(cursorMovedHandler).not.toHaveBeenCalled()

View File

@@ -65,7 +65,7 @@ class Cursor extends Model
textChanged: textChanged
@emit 'moved', movedEvent
@editor.cursorMoved(movedEvent)
@editor.cursorMoved(this, movedEvent)
@marker.on 'destroyed', =>
@destroyed = true
@editor.removeCursor(this)

View File

@@ -318,6 +318,9 @@ class Editor extends Model
onDidInsertText: (callback) ->
@emitter.on 'did-insert-text', callback
onDidMoveCursor: (callback) ->
@emitter.on 'did-move-cursor', callback
# Retrieves the current {TextBuffer}.
getBuffer: -> @buffer
@@ -1853,8 +1856,9 @@ class Editor extends Model
@movingCursors = false
@emit 'cursors-moved'
cursorMoved: (event) ->
cursorMoved: (cursor, event) ->
@emit 'cursor-moved', event
@emitter.emit 'did-move-cursor', _.extend({cursor}, event)
@emit 'cursors-moved' unless @movingCursors
# Merge cursors that have the same screen position