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

@@ -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: ->