Serialize TextBuffer inside EditSession serialize

This commit is contained in:
Mutwin Kraus 2013-03-26 14:46:45 +01:00 committed by Nathan Sobo
parent 693d8258ad
commit cc87595e4e
5 changed files with 69 additions and 19 deletions

View File

@ -2060,7 +2060,9 @@ describe "EditSession", ->
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.destroy()
editSession = project.buildEditSession(null, autoIndent: false)
editSession.buffer.setText('test')
editSession = EditSession.deserialize(editSession.serialize())
expect(editSession.buffer.getText()).toBe('test')

View File

@ -1229,3 +1229,48 @@ describe 'Buffer', ->
expect(buffer.clipPosition([1, 0])).toEqual [0,9]
expect(buffer.clipPosition([0,10])).toEqual [0,9]
expect(buffer.clipPosition([10,Infinity])).toEqual [0,9]
describe "when the buffer is serialized", ->
describe "when the contents of the buffer are saved on disk", ->
it "stores the file path", ->
data = buffer.serialize()
expect(data.path).toBe(buffer.getPath())
expect(data.text).toBeFalsy()
describe "when the buffer has unsaved changes", ->
it "stores the file path and the changed buffer text", ->
buffer.setText("abc")
data = buffer.serialize()
expect(data.path).toBe(buffer.getPath())
expect(data.text).toBe("abc")
describe "when the buffer has never been saved", ->
it "stores the changed buffer text", ->
buffer.release()
buffer = new Buffer()
buffer.setText("abc")
data = buffer.serialize()
expect(data.path).toBeFalsy()
expect(data.text).toBe("abc")
describe "when a buffer is deserialized", ->
reloadBuffer = () ->
serialized = buffer.serialize()
buffer.release()
buffer = Buffer.deserialize(serialized)
it "loads the contents of the file saved on disk when there are no unsaved changes", ->
path = buffer.getPath()
reloadBuffer()
expect(buffer.getPath()).toBe(path)
it "loads the stored changes if the file was modified", ->
path = buffer.getPath()
buffer.setText("abc")
reloadBuffer()
expect(buffer.getPath()).toBe(path)
expect(buffer.getText()).toBe("abc")
it "loads the stored changes if the file was never saved", ->
buffer.release()
buffer = new Buffer()
buffer.setText("abc")
reloadBuffer()
expect(buffer.getPath()).toBeFalsy()
expect(buffer.getText()).toBe("abc")

View File

@ -15,11 +15,8 @@ class EditSession
registerDeserializer(this)
@deserialize: (state) ->
if fs.exists(state.buffer)
session = project.buildEditSession(state.buffer)
else if state.unsavedState
session = project.buildEditSessionFromState(state.unsavedState)
else
session = project.buildEditSessionForBuffer(Buffer.deserialize(state.buffer))
if !session?
console.warn "Could not build edit session for path '#{state.buffer}' because that file no longer exists" if state.buffer
session = project.buildEditSession(null)
session.setScrollTop(state.scrollTop)
@ -90,11 +87,10 @@ class EditSession
serialize: ->
deserializer: 'EditSession'
buffer: @buffer.getPath()
buffer: @buffer.serialize()
scrollTop: @getScrollTop()
scrollLeft: @getScrollLeft()
cursorScreenPosition: @getCursorScreenPosition().serialize()
unsavedState: @buffer.getText() if !@buffer.fileExists()
copy: ->
EditSession.deserialize(@serialize(), @project)

View File

@ -87,9 +87,6 @@ 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
@ -141,11 +138,6 @@ 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

@ -11,6 +11,7 @@ BufferMarker = require 'buffer-marker'
module.exports =
class Buffer
@idCounter = 1
registerDeserializer(this)
stoppedChangingDelay: 300
stoppedChangingTimeout: null
undoManager: null
@ -24,7 +25,13 @@ class Buffer
invalidMarkers: null
refcount: 0
constructor: (path, @project) ->
@deserialize: (state) ->
if state && (state.path? || state.text?)
new Buffer(state.path, project, state.text)
else
new Buffer(null, project)
constructor: (path, @project, initialText) ->
@id = @constructor.idCounter++
@nextMarkerId = 1
@validMarkers = {}
@ -35,9 +42,12 @@ class Buffer
if path
throw "Path '#{path}' does not exist" unless fs.exists(path)
@setPath(path)
@reload()
if initialText?
@setText(initialText)
else
@reload()
else
@setText('')
@setText(initialText ? '')
@undoManager = new UndoManager(this)
@ -56,6 +66,11 @@ class Buffer
@destroy() if @refcount <= 0
this
serialize: ->
deserializer: 'TextBuffer'
path: @getPath()
text: @getText() if @isModified()
hasEditors: -> @refcount > 1
subscribeToFile: ->