Prompt to save dirty buffers from destroyEditSessionIndex

Previously only the active editor session was prompting to
save dirty buffers before closing.  This caused the confirm
dialog to not display when closing from a tab since the
Editor.destroyEditSessionIndex is used there.
This commit is contained in:
Kevin Sawicki 2013-01-07 16:24:51 -08:00
parent 5ded74b3e6
commit 0adae3c634
2 changed files with 35 additions and 21 deletions

View File

@ -2043,6 +2043,17 @@ describe "Editor", ->
editor.underlayer.trigger event
expect(editor.getSelection().getScreenRange()).toEqual [[0,0], [12,2]]
describe ".destroyEditSessionIndex(index)", ->
it "prompts to save dirty buffers before closing", ->
editor.setText("I'm dirty")
rootView.open('sample.txt')
expect(editor.getEditSessions().length).toBe 2
spyOn(atom, "confirm")
editor.destroyEditSessionIndex(0)
expect(atom.confirm).toHaveBeenCalled
expect(editor.getEditSessions().length).toBe 2
expect(editor.getEditSessions()[0].buffer.isModified()).toBeTruthy()
describe ".destroyInactiveEditSessions()", ->
it "destroys all non-active, non-modified edit sessions", ->
editor.setText("I'm dirty")

View File

@ -148,7 +148,7 @@ class Editor extends View
'core:select-down': @selectDown
'core:select-to-top': @selectToTop
'core:select-to-bottom': @selectToBottom
'core:close': @close
'core:close': @destroyActiveEditSession
'editor:save': @save
'editor:newline-below': @insertNewlineBelow
'editor:toggle-soft-tabs': @toggleSoftTabs
@ -448,15 +448,21 @@ class Editor extends View
@destroyEditSessionIndex(@getActiveEditSessionIndex())
destroyEditSessionIndex: (index) ->
if @editSessions.length == 1
@remove()
else
editSession = @editSessions[index]
return if @mini
editSession = @editSessions[index]
destroySession = =>
if index is @getActiveEditSessionIndex()
@loadPreviousEditSession()
_.remove(@editSessions, editSession)
editSession.destroy()
@trigger 'editor:edit-session-removed', [editSession, index]
@remove() if @editSessions.length is 0
if editSession.buffer.isModified()
@promptToSaveDirtySession(editSession, destroySession)
else
destroySession(editSession)
destroyInactiveEditSessions: ->
index = 0
@ -634,14 +640,14 @@ class Editor extends View
@removeClass 'soft-wrap'
$(window).off 'resize', @_setSoftWrapColumn
save: (onSuccess) ->
save: (session=@activeEditSession, onSuccess) ->
if @getPath()
@activeEditSession.save()
session.save()
onSuccess?()
else
atom.showSaveDialog (path) =>
if path
@activeEditSession.saveAs(path)
session.saveAs(path)
onSuccess?()
autosave: ->
@ -679,19 +685,16 @@ class Editor extends View
rootView: ->
@parents('#root-view').view()
close: ->
return if @mini
if @getBuffer().isModified()
filename = if @getPath() then fs.base(@getPath()) else "untitled buffer"
atom.confirm(
"'#{filename}' has changes, do you want to save them?"
"Your changes will be lost if you don't save them"
"Save", (=> @save(=> @destroyActiveEditSession())),
"Cancel", null
"Don't save", (=> @destroyActiveEditSession())
)
else
@destroyActiveEditSession()
promptToSaveDirtySession: (session, callback) ->
path = session.getPath()
filename = if path then fs.base(path) else "untitled buffer"
atom.confirm(
"'#{filename}' has changes, do you want to save them?"
"Your changes will be lost if you don't save them"
"Save", => @save(session, callback),
"Cancel", null
"Don't save", callback
)
remove: (selector, keepData) ->
return super if keepData