From 63e3be8630dba57efe7e75fc0956af5fe96eabd7 Mon Sep 17 00:00:00 2001 From: probablycorey Date: Mon, 9 Dec 2013 11:06:29 -0800 Subject: [PATCH] Only mark a deleted file as modified if it was previously modified. Fixes #693 --- spec/text-buffer-spec.coffee | 55 ++++++++++++++++++++---------------- src/text-buffer.coffee | 6 ++-- 2 files changed, 33 insertions(+), 28 deletions(-) diff --git a/spec/text-buffer-spec.coffee b/spec/text-buffer-spec.coffee index 31066cd5b..de468183a 100644 --- a/spec/text-buffer-spec.coffee +++ b/spec/text-buffer-spec.coffee @@ -34,11 +34,11 @@ describe 'TextBuffer', -> expect(buffer.getText()).toBe fs.readFileSync(filePath, 'utf8') describe "when no file exists for the path", -> - it "is modified and is initially empty", -> + it "is not modified and is initially empty", -> filePath = "does-not-exist.txt" expect(fs.existsSync(filePath)).toBeFalsy() buffer = atom.project.bufferForPathSync(filePath) - expect(buffer.isModified()).toBeTruthy() + expect(buffer.isModified()).not.toBeTruthy() expect(buffer.getText()).toBe '' describe "when no path is given", -> @@ -160,20 +160,38 @@ describe 'TextBuffer', -> filePath = bufferToDelete.getPath() # symlinks may have been converted expect(bufferToDelete.getPath()).toBe filePath - expect(bufferToDelete.isModified()).toBeFalsy() - - removeHandler = jasmine.createSpy('removeHandler') - bufferToDelete.file.on 'removed', removeHandler - fs.removeSync(filePath) - waitsFor "file to be removed", -> - removeHandler.callCount > 0 afterEach -> bufferToDelete.destroy() - it "retains its path and reports the buffer as modified", -> - expect(bufferToDelete.getPath()).toBe filePath - expect(bufferToDelete.isModified()).toBeTruthy() + describe "when the file is modified", -> + beforeEach -> + bufferToDelete.setText("I WAS MODIFIED") + expect(bufferToDelete.isModified()).toBeTruthy() + + removeHandler = jasmine.createSpy('removeHandler') + bufferToDelete.file.on 'removed', removeHandler + fs.removeSync(filePath) + waitsFor "file to be removed", -> + removeHandler.callCount > 0 + + it "retains its path and reports the buffer as modified", -> + expect(bufferToDelete.getPath()).toBe filePath + expect(bufferToDelete.isModified()).toBeTruthy() + + describe "when the file is not modified", -> + beforeEach -> + expect(bufferToDelete.isModified()).toBeFalsy() + + removeHandler = jasmine.createSpy('removeHandler') + bufferToDelete.file.on 'removed', removeHandler + fs.removeSync(filePath) + waitsFor "file to be removed", -> + removeHandler.callCount > 0 + + it "retains its path and reports the buffer as not modified", -> + expect(bufferToDelete.getPath()).toBe filePath + expect(bufferToDelete.isModified()).toBeFalsy() it "resumes watching of the file when it is re-saved", -> bufferToDelete.save() @@ -210,19 +228,6 @@ describe 'TextBuffer', -> advanceClock(buffer.stoppedChangingDelay) expect(modifiedHandler).toHaveBeenCalledWith(false) - it "reports the modified status changing to true after the underlying file is deleted", -> - buffer.release() - filePath = path.join(temp.dir, 'atom-tmp-file') - fs.writeFileSync(filePath, 'delete me') - buffer = atom.project.bufferForPathSync(filePath) - modifiedHandler = jasmine.createSpy("modifiedHandler") - buffer.on 'modified-status-changed', modifiedHandler - - fs.removeSync(filePath) - - waitsFor "modified status to change", -> modifiedHandler.callCount - runs -> expect(buffer.isModified()).toBe true - it "reports the modified status changing to false after a modified buffer is saved", -> filePath = path.join(temp.dir, 'atom-tmp-file') fs.writeFileSync(filePath, '') diff --git a/src/text-buffer.coffee b/src/text-buffer.coffee index a32913679..ed1a7f086 100644 --- a/src/text-buffer.coffee +++ b/src/text-buffer.coffee @@ -117,8 +117,8 @@ class TextBuffer extends telepath.Model @reload() @file.on "removed", => - @updateCachedDiskContents().done => - @emitModifiedStatusChanged(@isModified()) + @wasModifiedBeforeRemove = @getText() != @cachedDiskContents + @updateCachedDiskContents() @file.on "moved", => @emit "path-changed", this @@ -403,7 +403,7 @@ class TextBuffer extends telepath.Model if @file.exists() @getText() != @cachedDiskContents else - true + @wasModifiedBeforeRemove ? not @isEmpty() else not @isEmpty()