Deserialize also untitled buffers

This commit is contained in:
Antonio Scandurra
2015-12-07 14:47:23 +01:00
parent d377e725e0
commit f7a4ef4a84
3 changed files with 41 additions and 1 deletions

View File

@@ -24,6 +24,34 @@ describe "TokenizedBuffer", ->
advanceClock() while tokenizedBuffer.firstInvalidRow()?
changeHandler?.reset()
describe "serialization", ->
describe "when the underlying buffer has a path", ->
it "deserializes it searching for its path in the current project", ->
buffer = atom.project.bufferForPathSync('sample.js')
tokenizedBufferA = new TokenizedBuffer({
buffer, config: atom.config, grammarRegistry: atom.grammars, packageManager: atom.packages, assert: atom.assert
})
tokenizedBufferB = TokenizedBuffer.deserialize(
JSON.parse(JSON.stringify(tokenizedBufferA.serialize())),
atom
)
expect(tokenizedBufferB.buffer).toBe(tokenizedBufferA.buffer)
describe "when the underlying buffer has no path", ->
it "deserializes it searching for its id in the current project", ->
buffer = atom.project.bufferForPathSync(null)
tokenizedBufferA = new TokenizedBuffer({
buffer, config: atom.config, grammarRegistry: atom.grammars, packageManager: atom.packages, assert: atom.assert
})
tokenizedBufferB = TokenizedBuffer.deserialize(
JSON.parse(JSON.stringify(tokenizedBufferA.serialize())),
atom
)
expect(tokenizedBufferB.buffer).toBe(tokenizedBufferA.buffer)
describe "when the buffer is destroyed", ->
beforeEach ->
buffer = atom.project.bufferForPathSync('sample.js')

View File

@@ -308,12 +308,20 @@ class Project extends Model
findBufferForPath: (filePath) ->
_.find @buffers, (buffer) -> buffer.getPath() is filePath
findBufferForId: (id) ->
_.find @buffers, (buffer) -> buffer.getId() is id
# Only to be used in specs
bufferForPathSync: (filePath) ->
absoluteFilePath = @resolvePath(filePath)
existingBuffer = @findBufferForPath(absoluteFilePath) if filePath
existingBuffer ? @buildBufferSync(absoluteFilePath)
# Only to be used when deserializing
bufferForIdSync: (id) ->
existingBuffer = @findBufferForId(id) if id
existingBuffer ? @buildBufferSync(absoluteFilePath)
# Given a file path, this retrieves or creates a new {TextBuffer}.
#
# If the `filePath` already has a `buffer`, that value is used instead. Otherwise,

View File

@@ -22,7 +22,10 @@ class TokenizedBuffer extends Model
changeCount: 0
@deserialize: (state, atomEnvironment) ->
state.buffer = atomEnvironment.project.bufferForPathSync(state.bufferPath)
if state.bufferPath
state.buffer = atomEnvironment.project.bufferForPathSync(state.bufferPath)
else
state.buffer = atomEnvironment.project.bufferForIdSync(state.bufferId)
state.config = atomEnvironment.config
state.grammarRegistry = atomEnvironment.grammars
state.packageManager = atomEnvironment.packages
@@ -53,6 +56,7 @@ class TokenizedBuffer extends Model
serialize: ->
deserializer: 'TokenizedBuffer'
bufferPath: @buffer.getPath()
bufferId: @buffer.getId()
tabLength: @tabLength
ignoreInvisibles: @ignoreInvisibles
largeFileMode: @largeFileMode