Better handling of multiple buffers in UndoGroupHelper

It may happen that the buffers are visited in order buffer1,
buffer2, buffer1. In this case, we want to have only one undo group
in buffer1. The solution is to replace buffer_ with a set<Buffer*>.

A use case among others is InsetLabel::updateReferences.
This commit is contained in:
Jean-Marc Lasgouttes 2017-11-29 14:10:33 +01:00
parent 6df9cbef95
commit cc7364dfc5
2 changed files with 28 additions and 23 deletions

View File

@ -40,6 +40,7 @@
#include <algorithm>
#include <deque>
#include <set>
using namespace std;
using namespace lyx::support;
@ -649,22 +650,31 @@ void Undo::recordUndoFullBuffer(CursorData const & cur)
/// UndoGroupHelper class stuff
/** FIXME: handle restarted groups
* It may happen that the buffers are visited in order buffer1,
* buffer2, buffer1. In this case, we want to have only one undo group
* in buffer1. One solution is to replace buffer_ with a set<Buffer*>,
* but I am not sure yet how to do it. A use case is
* InsetLabel::updateReferences.
*/
class UndoGroupHelper::Impl {
friend class UndoGroupHelper;
set<Buffer *> buffers_;
};
UndoGroupHelper::UndoGroupHelper(Buffer * buf) : d(new UndoGroupHelper::Impl)
{
resetBuffer(buf);
}
UndoGroupHelper::~UndoGroupHelper()
{
for (Buffer * buf : d->buffers_)
buf->undo().endUndoGroup();
delete d;
}
void UndoGroupHelper::resetBuffer(Buffer * buf)
{
if (buf == buffer_)
return;
if (buffer_)
buffer_->undo().endUndoGroup();
buffer_ = buf;
if (buffer_)
buffer_->undo().beginUndoGroup();
if (buf && d->buffers_.count(buf) == 0) {
d->buffers_.insert(buf);
buf->undo().beginUndoGroup();
}
}

View File

@ -135,15 +135,9 @@ private:
*/
class UndoGroupHelper {
public:
UndoGroupHelper(Buffer * buf = 0) : buffer_(0)
{
resetBuffer(buf);
}
UndoGroupHelper(Buffer * buf = 0);
~UndoGroupHelper()
{
resetBuffer(0);
}
~UndoGroupHelper();
/** Close the current undo group if necessary and create a new one
* for buffer \c buf.
@ -151,7 +145,8 @@ public:
void resetBuffer(Buffer * buf);
private:
Buffer * buffer_;
class Impl;
Impl * const d;
};