Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Don't destroy buffer when underlying file is deleted. #178

Merged
merged 6 commits into from
Jan 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions spec/text-buffer-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -1330,13 +1330,10 @@ describe "TextBuffer", ->
done()
fs.removeSync(filePath)

it "retains its path and reports the buffer as not modified", ->
# FIXME: This doesn't pass on Linux
return if process.platform is 'linux'

expect(bufferToDelete.getPath()).toBe filePath
expect(bufferToDelete.isModified()).toBeFalsy()

it "unsets its path and reports the buffer as modified", ->
expect(bufferToDelete.getPath()).toBe undefined
expect(bufferToDelete.file).toBe null
expect(bufferToDelete.isModified()).toBeTruthy()

describe "when the file is deleted", ->
it "notifies all onDidDelete listeners ", (done) ->
Expand Down
16 changes: 15 additions & 1 deletion src/text-buffer.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ class TextBuffer
# * `load` A {Boolean}, `true` to asynchronously load the buffer from disk
# after initialization.
# * `text` The initial {String} text of the buffer.
# * `shouldDestroyOnFileDelete` A {Function} that returns a {Boolean}
# indicating whether the buffer should be destroyed if its file is
# deleted.
constructor: (params) ->
text = params if typeof params is 'string'

Expand Down Expand Up @@ -79,6 +82,8 @@ class TextBuffer
@transactCallDepth = 0
@digestWhenLastPersisted = params?.digestWhenLastPersisted ? false

@shouldDestroyOnFileDelete = params?.shouldDestroyOnFileDelete ? -> false

@setPath(params.filePath) if params?.filePath
@load() if params?.load

Expand Down Expand Up @@ -1472,14 +1477,23 @@ class TextBuffer
if modified
@updateCachedDiskContents()
else
@destroy()
if @shouldDestroyOnFileDelete()
@destroy()
else
@unsubscribeFromFile()

@fileSubscriptions.add @file.onDidRename =>
@emitter.emit 'did-change-path', @getPath()

@fileSubscriptions.add @file.onWillThrowWatchError (errorObject) =>
@emitter.emit 'will-throw-watch-error', errorObject

unsubscribeFromFile: ->
@fileSubscriptions?.dispose()
@setPath(null)
@conflict = false
@cachedDiskContents = null

createMarkerSnapshot: ->
snapshot = {}
for markerLayerId, markerLayer of @markerLayers
Expand Down