Fix "Close all but this" behaviour if multiple views are present and some files are dirty.

This PR contains three types of changes:

1. Actual bug fix. This bug appeared, because prior to closing files, "Close all but this" attempts to save modified files, and for that it needs to switch views. Those views were not restored after that, however - so further actions could end up being executed on a wrong view. Those are changed commented with // We may have to restore previous view after saving new files.

2. Another potential bug fix. Closing files in both views could result in views being switched (it may or may not, I did not manage to unambiguously find that out from the code). To prevent any possible issues, I stored view IDs in viewNo. If those could never switch, then the code changes nothing - but if they could, it fixes a potential bug.

3. Code clarity fix in Notepad_plus::switchEditViewTo. std::swap makes it more obvious this code is there only to swap values around!

Fixes #4911, Close #4920
This commit is contained in:
Silent 2018-10-13 20:16:47 +02:00 committed by Don HO
parent 94cf9d53ca
commit acb30b9e61
2 changed files with 12 additions and 10 deletions

View File

@ -3512,12 +3512,8 @@ int Notepad_plus::switchEditViewTo(int gid)
_activeView = newView;
//Good old switcheroo
DocTabView * tempTab = _pDocTab;
_pDocTab = _pNonDocTab;
_pNonDocTab = tempTab;
ScintillaEditView * tempView = _pEditView;
_pEditView = _pNonEditView;
_pNonEditView = tempView;
std::swap(_pDocTab, _pNonDocTab);
std::swap(_pEditView, _pNonEditView);
_pEditView->beSwitched();
_pEditView->getFocus(); //set the focus

View File

@ -1099,6 +1099,7 @@ bool Notepad_plus::fileCloseAllButCurrent()
{
BufferID current = _pEditView->getCurrentBufferID();
int active = _pDocTab->getCurrentTabIndex();
const int activeViewID = currentView();
//closes all documents, makes the current view the only one visible
//first check if we need to save any file
@ -1158,28 +1159,33 @@ bool Notepad_plus::fileCloseAllButCurrent()
}
}
// We may have to restore previous view after saving new files
switchEditViewTo(activeViewID);
bool isSnapshotMode = NppParameters::getInstance()->getNppGUI().isSnapshotMode();
//Then start closing, inactive view first so the active is left open
if (bothActive())
{
//first close all docs in non-current view, which gets closed automatically
//Set active tab to the last one closed.
activateBuffer(_pNonDocTab->getBufferByIndex(0), otherView());
const int viewNo = otherView();
activateBuffer(_pNonDocTab->getBufferByIndex(0), viewNo);
for (int32_t i = static_cast<int32_t>(_pNonDocTab->nbItem()) - 1; i >= 0; i--) //close all from right to left
{
doClose(_pNonDocTab->getBufferByIndex(i), otherView(), isSnapshotMode);
doClose(_pNonDocTab->getBufferByIndex(i), viewNo, isSnapshotMode);
}
}
activateBuffer(_pDocTab->getBufferByIndex(0), currentView());
const int viewNo = currentView();
activateBuffer(_pDocTab->getBufferByIndex(0), viewNo);
for (int32_t i = static_cast<int32_t>(_pDocTab->nbItem()) - 1; i >= 0; i--) //close all from right to left
{
if (i == active) //dont close active index
{
continue;
}
doClose(_pDocTab->getBufferByIndex(i), currentView(), isSnapshotMode);
doClose(_pDocTab->getBufferByIndex(i), viewNo, isSnapshotMode);
}
return true;
}