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 5 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 @@ -79,6 +79,8 @@ class TextBuffer
@transactCallDepth = 0
@digestWhenLastPersisted = params?.digestWhenLastPersisted ? false

@shouldDestroyBufferOnFileDelete = -> false

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

Expand Down Expand Up @@ -131,6 +133,9 @@ class TextBuffer
preferredLineEnding: @preferredLineEnding
nextMarkerId: @nextMarkerId

setConfigCallbacks: (shouldDestroyBufferOnFileDelete) ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about using a constructor parameter named shouldDestroyOnFileDelete instead?

@shouldDestroyBufferOnFileDelete = shouldDestroyBufferOnFileDelete

###
Section: Event Subscription
###
Expand Down Expand Up @@ -1472,14 +1477,23 @@ class TextBuffer
if modified
@updateCachedDiskContents()
else
@destroy()
if @shouldDestroyBufferOnFileDelete()
@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