clean up undolist

This commit is contained in:
Roy Qu 2022-07-05 22:25:28 +08:00
parent 2ff9dee9c0
commit fc6b582402
3 changed files with 33 additions and 82 deletions

View File

@ -3425,7 +3425,7 @@ QString SynEdit::expandAtWideGlyphs(const QString &S)
void SynEdit::updateModifiedStatus() void SynEdit::updateModifiedStatus()
{ {
setModified(!mUndoList->initialState()); setModified(!mUndoList->isEmpty());
} }
int SynEdit::scanFrom(int Index, int canStopIndex) int SynEdit::scanFrom(int Index, int canStopIndex)
@ -6689,8 +6689,6 @@ void SynEdit::onUndoAdded()
if (! mUndoList->insideRedo() && if (! mUndoList->insideRedo() &&
mUndoList->peekItem() && (mUndoList->peekItem()->changeReason()!=SynChangeReason::GroupBreak)) mUndoList->peekItem() && (mUndoList->peekItem()->changeReason()!=SynChangeReason::GroupBreak))
mRedoList->clear(); mRedoList->clear();
if (mUndoList->blockCount() == 0 )
onChanged();
} }
SynSelectionMode SynEdit::activeSelectionMode() const SynSelectionMode SynEdit::activeSelectionMode() const
@ -6901,9 +6899,6 @@ void SynEdit::setTopLine(int Value)
void SynEdit::onRedoAdded() void SynEdit::onRedoAdded()
{ {
updateModifiedStatus(); updateModifiedStatus();
if (mRedoList->blockCount() == 0 )
onChanged();
} }
void SynEdit::onGutterChanged() void SynEdit::onGutterChanged()

View File

@ -840,9 +840,8 @@ SynEditUndoList::SynEditUndoList():QObject()
mInsideRedo = false; mInsideRedo = false;
mBlockChangeNumber=0; mBlockChangeNumber=0;
mBlockCount=0; mBlockLockCount=0;
mFullUndoImposible=false; mFullUndoImposible=false;
mLockCount = 0;
mInitialChangeNumber = 0; mInitialChangeNumber = 0;
} }
@ -850,24 +849,19 @@ void SynEditUndoList::addChange(SynChangeReason AReason, const BufferCoord &ASta
const BufferCoord &AEnd, const QStringList& ChangeText, const BufferCoord &AEnd, const QStringList& ChangeText,
SynSelectionMode SelMode) SynSelectionMode SelMode)
{ {
if (mLockCount != 0)
return;
int changeNumber; int changeNumber;
if (mBlockChangeNumber != 0) { if (inBlock()) {
changeNumber = mBlockChangeNumber; changeNumber = mBlockChangeNumber;
} else { } else {
changeNumber = mNextChangeNumber; changeNumber = getNextChangeNumber();
if (mBlockCount == 0) {
mNextChangeNumber++;
if (mNextChangeNumber == 0) {
mNextChangeNumber++;
}
}
} }
PSynEditUndoItem NewItem = std::make_shared<SynEditUndoItem>(AReason, PSynEditUndoItem NewItem = std::make_shared<SynEditUndoItem>(AReason,
SelMode,AStart,AEnd,ChangeText, SelMode,AStart,AEnd,ChangeText,
changeNumber); changeNumber);
pushItem(NewItem); pushItem(NewItem);
if (mBlockLockCount == 0) {
emit addedUndo();
}
} }
void SynEditUndoList::addGroupBreak() void SynEditUndoList::addGroupBreak()
@ -882,8 +876,8 @@ void SynEditUndoList::addGroupBreak()
void SynEditUndoList::beginBlock() void SynEditUndoList::beginBlock()
{ {
mBlockCount++; mBlockLockCount++;
mBlockChangeNumber = mNextChangeNumber; mBlockChangeNumber = getNextChangeNumber();
} }
void SynEditUndoList::clear() void SynEditUndoList::clear()
@ -902,20 +896,27 @@ void SynEditUndoList::deleteItem(int index)
void SynEditUndoList::endBlock() void SynEditUndoList::endBlock()
{ {
if (mBlockCount > 0) { if (mBlockLockCount > 0) {
mBlockCount--; mBlockLockCount--;
if (mBlockCount == 0) { if (mBlockLockCount == 0) {
int iBlockID = mBlockChangeNumber; int iBlockID = mBlockChangeNumber;
mBlockChangeNumber = 0; mBlockChangeNumber = 0;
mNextChangeNumber++;
if (mNextChangeNumber == 0)
mNextChangeNumber++;
if (mItems.count() > 0 && peekItem()->changeNumber() == iBlockID) if (mItems.count() > 0 && peekItem()->changeNumber() == iBlockID)
emit addedUndo(); emit addedUndo();
} }
} }
} }
bool SynEditUndoList::inBlock()
{
return mBlockLockCount>0;
}
unsigned int SynEditUndoList::getNextChangeNumber()
{
return mNextChangeNumber++;
}
SynChangeReason SynEditUndoList::lastChangeReason() SynChangeReason SynEditUndoList::lastChangeReason()
{ {
if (mItems.count() == 0) if (mItems.count() == 0)
@ -929,11 +930,6 @@ bool SynEditUndoList::isEmpty()
return mItems.count()==0; return mItems.count()==0;
} }
void SynEditUndoList::lock()
{
mLockCount++;
}
PSynEditUndoItem SynEditUndoList::peekItem() PSynEditUndoItem SynEditUndoList::peekItem()
{ {
if (mItems.count() == 0) if (mItems.count() == 0)
@ -959,14 +955,6 @@ void SynEditUndoList::pushItem(PSynEditUndoItem Item)
return; return;
mItems.append(Item); mItems.append(Item);
ensureMaxEntries(); ensureMaxEntries();
if (Item->changeReason()!= SynChangeReason::GroupBreak)
emit addedUndo();
}
void SynEditUndoList::unlock()
{
if (mLockCount > 0)
mLockCount--;
} }
bool SynEditUndoList::canUndo() bool SynEditUndoList::canUndo()
@ -992,15 +980,6 @@ void SynEditUndoList::setMaxUndoActions(int maxUndoActions)
} }
} }
bool SynEditUndoList::initialState()
{
if (itemCount() == 0) {
return mInitialChangeNumber == 0;
} else {
return peekItem()->changeNumber() == mInitialChangeNumber;
}
}
PSynEditUndoItem SynEditUndoList::item(int index) PSynEditUndoItem SynEditUndoList::item(int index)
{ {
if (index <0 || index>=mItems.count()) { if (index <0 || index>=mItems.count()) {
@ -1009,22 +988,6 @@ PSynEditUndoItem SynEditUndoList::item(int index)
return mItems[index]; return mItems[index];
} }
void SynEditUndoList::setInitialState(const bool Value)
{
if (Value) {
if (itemCount() == 0)
mInitialChangeNumber = 0;
else
mInitialChangeNumber = peekItem()->changeNumber();
} else if (itemCount() == 0) {
if (mInitialChangeNumber == 0) {
mInitialChangeNumber = -1;
}
} else if (peekItem()->changeNumber() == mInitialChangeNumber) {
mInitialChangeNumber = -1;
}
}
void SynEditUndoList::setItem(int index, PSynEditUndoItem Value) void SynEditUndoList::setItem(int index, PSynEditUndoItem Value)
{ {
if (index <0 || index>=mItems.count()) { if (index <0 || index>=mItems.count()) {
@ -1043,11 +1006,6 @@ void SynEditUndoList::setBlockChangeNumber(int blockChangeNumber)
mBlockChangeNumber = blockChangeNumber; mBlockChangeNumber = blockChangeNumber;
} }
int SynEditUndoList::blockCount() const
{
return mBlockCount;
}
bool SynEditUndoList::insideRedo() const bool SynEditUndoList::insideRedo() const
{ {
return mInsideRedo; return mInsideRedo;

View File

@ -172,6 +172,7 @@ enum class SynChangeReason {
MoveSelectionDown, MoveSelectionDown,
Nothing // undo list empty Nothing // undo list empty
}; };
class SynEditUndoItem { class SynEditUndoItem {
private: private:
SynChangeReason mChangeReason; SynChangeReason mChangeReason;
@ -179,7 +180,7 @@ private:
BufferCoord mChangeStartPos; BufferCoord mChangeStartPos;
BufferCoord mChangeEndPos; BufferCoord mChangeEndPos;
QStringList mChangeText; QStringList mChangeText;
int mChangeNumber; size_t mChangeNumber;
public: public:
SynEditUndoItem(SynChangeReason reason, SynEditUndoItem(SynChangeReason reason,
SynSelectionMode selMode, SynSelectionMode selMode,
@ -207,32 +208,29 @@ public:
void addGroupBreak(); void addGroupBreak();
void beginBlock(); void beginBlock();
void endBlock();
void clear(); void clear();
void deleteItem(int index); void deleteItem(int index);
void endBlock();
SynChangeReason lastChangeReason(); SynChangeReason lastChangeReason();
bool isEmpty(); bool isEmpty();
void lock();
PSynEditUndoItem peekItem(); PSynEditUndoItem peekItem();
PSynEditUndoItem popItem(); PSynEditUndoItem popItem();
void pushItem(PSynEditUndoItem Item); void pushItem(PSynEditUndoItem Item);
void unlock();
bool canUndo(); bool canUndo();
int itemCount(); int itemCount();
int maxUndoActions() const; int maxUndoActions() const;
void setMaxUndoActions(int maxUndoActions); void setMaxUndoActions(int maxUndoActions);
bool initialState();
PSynEditUndoItem item(int index); PSynEditUndoItem item(int index);
void setInitialState(const bool Value);
void setItem(int index, PSynEditUndoItem Value); void setItem(int index, PSynEditUndoItem Value);
int blockChangeNumber() const; int blockChangeNumber() const;
void setBlockChangeNumber(int blockChangeNumber); void setBlockChangeNumber(int blockChangeNumber);
int blockCount() const;
bool insideRedo() const; bool insideRedo() const;
void setInsideRedo(bool insideRedo); void setInsideRedo(bool insideRedo);
@ -242,15 +240,15 @@ signals:
void addedUndo(); void addedUndo();
protected: protected:
void ensureMaxEntries(); void ensureMaxEntries();
bool inBlock();
unsigned int getNextChangeNumber();
protected: protected:
int mBlockChangeNumber; size_t mBlockChangeNumber;
int mBlockCount; int mBlockLockCount;
bool mFullUndoImposible; bool mFullUndoImposible;
QVector<PSynEditUndoItem> mItems; QVector<PSynEditUndoItem> mItems;
int mLockCount;
int mMaxUndoActions; int mMaxUndoActions;
int mNextChangeNumber; size_t mNextChangeNumber;
int mInitialChangeNumber;
bool mInsideRedo; bool mInsideRedo;
}; };