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

View File

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

View File

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