mirror of
https://github.com/atom/atom.git
synced 2026-02-04 19:54:59 -05:00
Buffers retain path when file is deleted and can be re-saved
Path watching resumes once the file is saved again. This commit allows files to be created for as-yet nonexistent paths. We won't call `$native.watchPath` until we have at least 1 subscription to the file in JS and the file exists on disk. Also, we moved execution of the path watcher callbacks until after the callbacks data structure is updated in order to avoid confusing behavior in specs.
This commit is contained in:
@@ -94,7 +94,7 @@ class Buffer
|
||||
|
||||
@file?.off()
|
||||
@file = new File(path)
|
||||
@subscribeToFile()
|
||||
@subscribeToFile() if @file.exists()
|
||||
|
||||
@trigger "path-change", this
|
||||
|
||||
@@ -236,14 +236,18 @@ class Buffer
|
||||
@saveAs(@getPath())
|
||||
|
||||
saveAs: (path) ->
|
||||
if not path then throw new Error("Can't save buffer with no file path")
|
||||
unless path then throw new Error("Can't save buffer with no file path")
|
||||
|
||||
@trigger 'before-save'
|
||||
@cachedDiskContents = @getText()
|
||||
fs.write path, @cachedDiskContents
|
||||
|
||||
@file?.updateMd5()
|
||||
@setPath(path)
|
||||
|
||||
text = @getText()
|
||||
|
||||
@cachedDiskContents = text
|
||||
@file.write(text)
|
||||
@subscribeToFile()
|
||||
@trigger 'after-save'
|
||||
|
||||
isModified: ->
|
||||
@@ -389,4 +393,7 @@ class Buffer
|
||||
@trigger 'stopped-changing'
|
||||
@stoppedChangingTimeout = setTimeout(stoppedChangingCallback, @stoppedChangingDelay)
|
||||
|
||||
fileExists: ->
|
||||
@file.exists()
|
||||
|
||||
_.extend(Buffer.prototype, EventEmitter)
|
||||
|
||||
@@ -9,8 +9,10 @@ class File
|
||||
md5: null
|
||||
|
||||
constructor: (@path) ->
|
||||
throw "Creating file with path that is not a file: #{@path}" unless fs.isFile(@path)
|
||||
@updateMd5()
|
||||
if @exists() and not fs.isFile(@path)
|
||||
throw new Error(@path + " is a directory")
|
||||
|
||||
@updateMd5() if @exists()
|
||||
|
||||
setPath: (@path) ->
|
||||
|
||||
@@ -19,6 +21,12 @@ class File
|
||||
getBaseName: ->
|
||||
fs.base(@path)
|
||||
|
||||
write: (text) ->
|
||||
previouslyExisted = @exists()
|
||||
fs.write(@getPath(), text)
|
||||
@updateMd5()
|
||||
@subscribeToNativeChangeEvents() if not previouslyExisted and @subscriptionCount() > 0
|
||||
|
||||
exists: ->
|
||||
fs.exists(@getPath())
|
||||
|
||||
@@ -26,7 +34,7 @@ class File
|
||||
@md5 = fs.md5ForPath(@path)
|
||||
|
||||
afterSubscribe: ->
|
||||
@subscribeToNativeChangeEvents() if @subscriptionCount() == 1
|
||||
@subscribeToNativeChangeEvents() if @exists() and @subscriptionCount() == 1
|
||||
|
||||
afterUnsubscribe: ->
|
||||
@unsubscribeFromNativeChangeEvents() if @subscriptionCount() == 0
|
||||
@@ -52,8 +60,8 @@ class File
|
||||
@subscribeToNativeChangeEvents()
|
||||
@handleNativeChangeEvent("contents-change", @getPath())
|
||||
else
|
||||
@unsubscribeFromNativeChangeEvents()
|
||||
@trigger "remove"
|
||||
@off()
|
||||
|
||||
subscribeToNativeChangeEvents: ->
|
||||
@watchId = $native.watchPath @path, (eventType, path) =>
|
||||
|
||||
Reference in New Issue
Block a user