Preserve buffer contents for unsaved files when reloading

This commit is contained in:
Mutwin Kraus
2013-03-25 20:16:22 +01:00
committed by Nathan Sobo
parent 9a8fd062c4
commit 693d8258ad
4 changed files with 16 additions and 1 deletions

View File

@@ -2059,6 +2059,10 @@ describe "EditSession", ->
editSession.setCursorScreenPosition([0, 1])
editSession.buffer.reload()
expect(editSession.getCursorScreenPosition()).toEqual [0,1]
it "preserves the current state if the file was not saved yet", ->
editSession = project.buildEditSessionFromState('test', autoIndent: false)
editSession = EditSession.deserialize(editSession.serialize())
expect(editSession.buffer.getText()).toBe('test')
describe "when the 'grammars-loaded' event is triggered on the syntax global", ->
it "reloads the edit session's grammar and re-tokenizes the buffer if it changes", ->

View File

@@ -17,6 +17,8 @@ class EditSession
@deserialize: (state) ->
if fs.exists(state.buffer)
session = project.buildEditSession(state.buffer)
else if state.unsavedState
session = project.buildEditSessionFromState(state.unsavedState)
else
console.warn "Could not build edit session for path '#{state.buffer}' because that file no longer exists" if state.buffer
session = project.buildEditSession(null)
@@ -92,6 +94,7 @@ class EditSession
scrollTop: @getScrollTop()
scrollLeft: @getScrollLeft()
cursorScreenPosition: @getCursorScreenPosition().serialize()
unsavedState: @buffer.getText() if !@buffer.fileExists()
copy: ->
EditSession.deserialize(@serialize(), @project)

View File

@@ -87,6 +87,9 @@ class Project
buildEditSession: (filePath, editSessionOptions={}) ->
@buildEditSessionForBuffer(@bufferForPath(filePath), editSessionOptions)
buildEditSessionFromState: (state, editSessionOptions={}) ->
@buildEditSessionForBuffer(@bufferForState(state), editSessionOptions)
buildEditSessionForBuffer: (buffer, editSessionOptions) ->
options = _.extend(@defaultEditSessionOptions(), editSessionOptions)
options.project = this
@@ -138,6 +141,11 @@ class Project
else
@buildBuffer()
bufferForState: (state) ->
buffer = @buildBuffer()
buffer.setText(state) if state
buffer
buildBuffer: (filePath) ->
buffer = new Buffer(filePath, this)
@buffers.push buffer

View File

@@ -445,7 +445,7 @@ class Buffer
@trigger 'modified-status-changed', modifiedStatus
fileExists: ->
@file.exists()
@file? && @file.exists()
logLines: (start=0, end=@getLastRow())->
for row in [start..end]