Maintain cursor position when buffer is reloaded

This commit is contained in:
Kevin Sawicki 2012-12-17 16:50:23 -08:00
parent d479b03056
commit 80b71582e0
4 changed files with 26 additions and 0 deletions

View File

@ -359,6 +359,14 @@ describe 'Buffer', ->
saveBuffer.save()
expect(events).toEqual ['beforeSave1', 'beforeSave2', 'fs.write', 'afterSave1', 'afterSave2']
it "fires before-reload and after-reload events when reloaded", ->
events = []
saveBuffer.on 'before-reload', -> events.push 'before-reload'
saveBuffer.on 'after-reload', -> events.push 'after-reload'
saveBuffer.reload()
expect(events).toEqual ['before-reload', 'after-reload']
describe "when the buffer has no path", ->
it "throws an exception", ->
saveBuffer = new Buffer

View File

@ -1822,3 +1822,9 @@ describe "EditSession", ->
expect(editSession.indentLevelForLine(" \thello")).toBe(2)
expect(editSession.indentLevelForLine(" \t hello")).toBe(2.5)
expect(editSession.indentLevelForLine(" \t \thello")).toBe(3.5)
describe "when the buffer is reloaded", ->
it "preserves the current cursor position", ->
editSession.setCursorScreenPosition([0, 1])
editSession.buffer.reload()
expect(editSession.getCursorScreenPosition()).toEqual [0,1]

View File

@ -73,8 +73,10 @@ class Buffer
@trigger "path-change", this
reload: ->
@trigger 'before-reload'
@updateCachedDiskContents()
@setText(@cachedDiskContents)
@trigger 'after-reload'
updateCachedDiskContents: ->
@cachedDiskContents = @file.read()

View File

@ -59,6 +59,8 @@ class EditSession
@buffer.on "update-anchors-after-change.edit-session-#{@id}", =>
@mergeCursors()
@preserveCursorPositionOnBufferReload()
@displayBuffer.on "change.edit-session-#{@id}", (e) =>
@refreshAnchorScreenPositions() unless e.bufferDelta
@trigger 'screen-lines-change', e
@ -597,4 +599,12 @@ class EditSession
inspect: ->
JSON.stringify @serialize()
preserveCursorPositionOnBufferReload: ->
cursorPosition = null
@buffer.on "before-reload.edit-session-#{@id}", =>
cursorPosition = @getCursorBufferPosition()
@buffer.on "after-reload.edit-session-#{@id}", =>
@setCursorBufferPosition(cursorPosition) if cursorPosition
cursorPosition = null
_.extend(EditSession.prototype, EventEmitter)