From 4d057a16d6d28cd17fccbf42ff0c5293e0e4e4a1 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Tue, 3 Oct 2017 10:01:53 -0700 Subject: [PATCH] Prompt to save when unloading if editor is in conflict --- spec/text-editor-spec.coffee | 31 ---------------------- spec/text-editor-spec.js | 50 ++++++++++++++++++++++++++++++++++++ src/text-editor.coffee | 2 +- 3 files changed, 51 insertions(+), 32 deletions(-) diff --git a/spec/text-editor-spec.coffee b/spec/text-editor-spec.coffee index efe3bf048..53011fdcc 100644 --- a/spec/text-editor-spec.coffee +++ b/spec/text-editor-spec.coffee @@ -5324,37 +5324,6 @@ describe "TextEditor", -> [[6, 3], [6, 4]], ]) - describe ".shouldPromptToSave()", -> - it "returns true when buffer changed", -> - jasmine.unspy(editor, 'shouldPromptToSave') - expect(editor.shouldPromptToSave()).toBeFalsy() - buffer.setText('changed') - expect(editor.shouldPromptToSave()).toBeTruthy() - - it "returns false when an edit session's buffer is in use by more than one session", -> - jasmine.unspy(editor, 'shouldPromptToSave') - buffer.setText('changed') - - editor2 = null - waitsForPromise -> - atom.workspace.getActivePane().splitRight() - atom.workspace.open('sample.js', autoIndent: false).then (o) -> editor2 = o - - runs -> - expect(editor.shouldPromptToSave()).toBeFalsy() - editor2.destroy() - expect(editor.shouldPromptToSave()).toBeTruthy() - - it "returns false when close of a window requested and edit session opened inside project", -> - jasmine.unspy(editor, 'shouldPromptToSave') - buffer.setText('changed') - expect(editor.shouldPromptToSave(windowCloseRequested: true, projectHasPaths: true)).toBeFalsy() - - it "returns true when close of a window requested and edit session opened without project", -> - jasmine.unspy(editor, 'shouldPromptToSave') - buffer.setText('changed') - expect(editor.shouldPromptToSave(windowCloseRequested: true, projectHasPaths: false)).toBeTruthy() - describe "when the editor contains surrogate pair characters", -> it "correctly backspaces over them", -> editor.setText('\uD835\uDF97\uD835\uDF97\uD835\uDF97') diff --git a/spec/text-editor-spec.js b/spec/text-editor-spec.js index 82ad3bc90..c81df8089 100644 --- a/spec/text-editor-spec.js +++ b/spec/text-editor-spec.js @@ -1,3 +1,5 @@ +const fs = require('fs') +const temp = require('temp').track() const {Point, Range} = require('text-buffer') const {it, fit, ffit, fffit, beforeEach, afterEach} = require('./async-spec-helpers') @@ -8,6 +10,54 @@ describe('TextEditor', () => { editor.destroy() }) + describe('.shouldPromptToSave()', () => { + beforeEach(async () => { + editor = await atom.workspace.open('sample.js') + jasmine.unspy(editor, 'shouldPromptToSave') + }) + + it('returns true when buffer has unsaved changes', () => { + expect(editor.shouldPromptToSave()).toBeFalsy() + editor.setText('changed') + expect(editor.shouldPromptToSave()).toBeTruthy() + }) + + it("returns false when an editor's buffer is in use by more than one buffer", async () => { + editor.setText('changed') + + atom.workspace.getActivePane().splitRight() + const editor2 = await atom.workspace.open('sample.js', {autoIndent: false}) + expect(editor.shouldPromptToSave()).toBeFalsy() + + editor2.destroy() + expect(editor.shouldPromptToSave()).toBeTruthy() + }) + + it('returns true when the window is closing if the file has changed on disk', async () => { + jasmine.useRealClock() + + editor.setText('initial stuff') + await editor.saveAs(temp.openSync('test-file').path) + + editor.setText('other stuff') + fs.writeFileSync(editor.getPath(), 'new stuff') + expect(editor.shouldPromptToSave({windowCloseRequested: true, projectHasPaths: true})).toBeFalsy() + + await new Promise(resolve => editor.onDidConflict(resolve)) + expect(editor.shouldPromptToSave({windowCloseRequested: true, projectHasPaths: true})).toBeTruthy() + }) + + it('returns false when the window is closing and the project has one or more directory paths', () => { + editor.setText('changed') + expect(editor.shouldPromptToSave({windowCloseRequested: true, projectHasPaths: true})).toBeFalsy() + }) + + it('returns false when the window is closing and the project has no directory paths', () => { + editor.setText('changed') + expect(editor.shouldPromptToSave({windowCloseRequested: true, projectHasPaths: false})).toBeTruthy() + }) + }) + describe('folding', () => { beforeEach(async () => { await atom.packages.activatePackage('language-javascript') diff --git a/src/text-editor.coffee b/src/text-editor.coffee index c9813e445..c00508f09 100644 --- a/src/text-editor.coffee +++ b/src/text-editor.coffee @@ -961,7 +961,7 @@ class TextEditor extends Model # this editor. shouldPromptToSave: ({windowCloseRequested, projectHasPaths}={}) -> if windowCloseRequested and projectHasPaths and atom.stateStore.isConnected() - false + @buffer.isInConflict() else @isModified() and not @buffer.hasMultipleEditors()