mirror of
https://github.com/pulsar-edit/pulsar.git
synced 2024-11-11 04:48:44 +03:00
Maintain cursor position when buffer is reloaded
This commit is contained in:
parent
d479b03056
commit
80b71582e0
@ -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
|
||||
|
@ -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]
|
||||
|
@ -73,8 +73,10 @@ class Buffer
|
||||
@trigger "path-change", this
|
||||
|
||||
reload: ->
|
||||
@trigger 'before-reload'
|
||||
@updateCachedDiskContents()
|
||||
@setText(@cachedDiskContents)
|
||||
@trigger 'after-reload'
|
||||
|
||||
updateCachedDiskContents: ->
|
||||
@cachedDiskContents = @file.read()
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user