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 <algorithm>
#include <deque> #include <deque>
#include <set>
using namespace std; using namespace std;
using namespace lyx::support; using namespace lyx::support;
@ -649,22 +650,31 @@ void Undo::recordUndoFullBuffer(CursorData const & cur)
/// UndoGroupHelper class stuff /// UndoGroupHelper class stuff
/** FIXME: handle restarted groups class UndoGroupHelper::Impl {
* It may happen that the buffers are visited in order buffer1, friend class UndoGroupHelper;
* buffer2, buffer1. In this case, we want to have only one undo group set<Buffer *> buffers_;
* 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.
*/ 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) void UndoGroupHelper::resetBuffer(Buffer * buf)
{ {
if (buf == buffer_) if (buf && d->buffers_.count(buf) == 0) {
return; d->buffers_.insert(buf);
if (buffer_) buf->undo().beginUndoGroup();
buffer_->undo().endUndoGroup(); }
buffer_ = buf;
if (buffer_)
buffer_->undo().beginUndoGroup();
} }

View File

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