work save

This commit is contained in:
Roy Qu 2022-07-02 06:44:01 +08:00
parent 097108a2c9
commit cd135b19a2
2 changed files with 91 additions and 151 deletions

View File

@ -2377,8 +2377,6 @@ void SynEdit::insertLine(bool moveCaret)
BufferCoord iUndoBegin = mBlockBegin; BufferCoord iUndoBegin = mBlockBegin;
BufferCoord iUndoEnd = mBlockEnd; BufferCoord iUndoEnd = mBlockEnd;
setSelTextPrimitive(""); setSelTextPrimitive("");
mUndoList->AddChange(SynChangeReason::crDelete, iUndoBegin, iUndoEnd, helper,
mActiveSelectionMode);
} }
QString Temp = lineText(); QString Temp = lineText();
@ -2488,11 +2486,11 @@ void SynEdit::doTabKey()
mUndoList->EndBlock(); mUndoList->EndBlock();
}); });
if (selAvail()) { if (selAvail()) {
mUndoList->AddChange(SynChangeReason::crDelete, // mUndoList->AddChange(SynChangeReason::crDelete,
mBlockBegin, // mBlockBegin,
mBlockEnd, // mBlockEnd,
selText(), // selText(),
mActiveSelectionMode); // mActiveSelectionMode);
setSelTextPrimitive(""); setSelTextPrimitive("");
} }
BufferCoord StartOfBlock = caretXY(); BufferCoord StartOfBlock = caretXY();
@ -2566,9 +2564,9 @@ void SynEdit::doShiftTabKey()
QString OldSelText = selText(); QString OldSelText = selText();
setSelTextPrimitive(""); setSelTextPrimitive("");
mUndoList->AddChange( // mUndoList->AddChange(
SynChangeReason::crSilentDelete, BufferCoord{NewX, mCaretY}, // SynChangeReason::crSilentDelete, BufferCoord{NewX, mCaretY},
OldCaretXY, OldSelText, SynSelectionMode::smNormal); // OldCaretXY, OldSelText, SynSelectionMode::smNormal);
internalSetCaretX(NewX); internalSetCaretX(NewX);
} }
} }
@ -3105,14 +3103,14 @@ void SynEdit::doPasteFromClipboard()
"", "",
SynSelectionMode::smNormal); SynSelectionMode::smNormal);
AddPasteEndMarker = true; AddPasteEndMarker = true;
if (selAvail()) { // if (selAvail()) {
mUndoList->AddChange( // mUndoList->AddChange(
SynChangeReason::crDelete, // SynChangeReason::crDelete,
mBlockBegin, // mBlockBegin,
mBlockEnd, // mBlockEnd,
selText(), // selText(),
mActiveSelectionMode); // mActiveSelectionMode);
} // }
// } else if (!colSelAvail()) // } else if (!colSelAvail())
// setActiveSelectionMode(selectionMode()); // setActiveSelectionMode(selectionMode());
BufferCoord vStartOfBlock = blockBegin(); BufferCoord vStartOfBlock = blockBegin();
@ -4385,9 +4383,7 @@ void SynEdit::doUndo()
FKeepGoing = true; FKeepGoing = true;
else { else {
FKeepGoing = (mOptions.testFlag(eoGroupUndo) && FKeepGoing = (mOptions.testFlag(eoGroupUndo) &&
(FLastChange == Item->changeReason()) && (FLastChange == Item->changeReason()) );
! (FLastChange == SynChangeReason::crIndent
|| FLastChange == SynChangeReason::crUnindent));
} }
FLastChange = Item->changeReason(); FLastChange = Item->changeReason();
} }
@ -4450,9 +4446,7 @@ void SynEdit::doUndoItem()
Item->changeSelMode()); Item->changeSelMode());
setCaretAndSelection(caretXY(), Item->changeStartPos(), Item->changeEndPos()); setCaretAndSelection(caretXY(), Item->changeStartPos(), Item->changeEndPos());
break; break;
case SynChangeReason::crInsert: case SynChangeReason::crInsert: {
case SynChangeReason::crPaste:
case SynChangeReason::crDragDropInsert: {
setCaretAndSelection( setCaretAndSelection(
Item->changeStartPos(), Item->changeStartPos(),
Item->changeStartPos(), Item->changeStartPos(),
@ -4493,11 +4487,7 @@ void SynEdit::doUndoItem()
Item->changeStr(), Item->changeStr(),
Item->changeSelMode()); Item->changeSelMode());
break; break;
case SynChangeReason::crDeleteAfterCursor: case SynChangeReason::crDelete: {
case SynChangeReason::crDelete:
case SynChangeReason::crSilentDelete:
case SynChangeReason::crSilentDeleteAfterCursor:
case SynChangeReason::crDeleteAll: {
// If there's no selection, we have to set // If there's no selection, we have to set
// the Caret's position manualy. // the Caret's position manualy.
BufferCoord TmpPos; BufferCoord TmpPos;
@ -4570,42 +4560,6 @@ void SynEdit::doUndoItem()
"", "",
Item->changeSelMode()); Item->changeSelMode());
break; break;
case SynChangeReason::crIndent:
// restore the selection
setCaretAndSelection(
Item->changeEndPos(),
Item->changeStartPos(),
Item->changeEndPos());
mRedoList->AddChange(
Item->changeReason(),
Item->changeStartPos(),
Item->changeEndPos(),
Item->changeStr(),
Item->changeSelMode());
break;
case SynChangeReason::crUnindent:
// reinsert the string
{
BufferCoord insertPos;
insertPos.Line = Item->changeStartPos().Line;
insertPos.Char = 1;
insertBlock(insertPos,
insertPos,
Item->changeStr(), false);
}
mRedoList->AddChange(
Item->changeReason(),
Item->changeStartPos(),
Item->changeEndPos(),
Item->changeStr(),
Item->changeSelMode());
break;
case SynChangeReason::crNothing:
mRedoList->AddChange(SynChangeReason::crNothing,
BufferCoord{0, 0},
BufferCoord{0, 0},
"", SynSelectionMode::smNormal);
break;
default: default:
break; break;
} }
@ -4763,8 +4717,6 @@ void SynEdit::doRedoItem()
Item->changeSelMode()); Item->changeSelMode());
break; break;
case SynChangeReason::crInsert: case SynChangeReason::crInsert:
case SynChangeReason::crPaste:
case SynChangeReason::crDragDropInsert:
setCaretAndSelection( setCaretAndSelection(
Item->changeStartPos(), Item->changeStartPos(),
Item->changeStartPos(), Item->changeStartPos(),
@ -4782,20 +4734,7 @@ void SynEdit::doRedoItem()
Item->changeEndPos()); Item->changeEndPos());
} }
break; break;
case SynChangeReason::crDeleteAfterCursor: case SynChangeReason::crDelete: {
case SynChangeReason::crSilentDeleteAfterCursor: {
setCaretAndSelection(Item->changeStartPos(), Item->changeStartPos(),
Item->changeEndPos());
QString TempString = selText();
setSelTextPrimitiveEx(Item->changeSelMode(),
Item->changeStr(),false);
mUndoList->AddChange(Item->changeReason(), Item->changeStartPos(),
Item->changeEndPos(), TempString, Item->changeSelMode());
internalSetCaretXY(Item->changeEndPos());
break;
}
case SynChangeReason::crDelete:
case SynChangeReason::crSilentDelete: {
setCaretAndSelection(Item->changeStartPos(), Item->changeStartPos(), setCaretAndSelection(Item->changeStartPos(), Item->changeStartPos(),
Item->changeEndPos()); Item->changeEndPos());
QString TempString = selText(); QString TempString = selText();
@ -4812,40 +4751,6 @@ void SynEdit::doRedoItem()
commandProcessor(SynEditorCommand::ecLineBreak); commandProcessor(SynEditorCommand::ecLineBreak);
break; break;
} }
case SynChangeReason::crIndent:
setCaretAndSelection(
Item->changeEndPos(),
Item->changeStartPos(),
Item->changeEndPos());
mUndoList->AddChange(Item->changeReason(), Item->changeStartPos(),
Item->changeEndPos(), Item->changeStr(), Item->changeSelMode());
break;
case SynChangeReason::crUnindent: {
int e = Item->changeEndPos().Line;
// convert selection to complete lines
if (Item->changeEndPos().Char == 1)
e = Item->changeEndPos().Line - 1;
QString TempString;
for (int i = Item->changeStartPos().Line; i<= e;i++) {
QString line = mDocument->getString(i - 1);
if (line.isEmpty())
continue;
if (line[0]!=' ' && line[0]!='\t')
continue;
int charsToDelete = 0;
while (charsToDelete < tabWidth() &&
charsToDelete < line.length() &&
line[charsToDelete] == ' ')
charsToDelete++;
if (charsToDelete == 0)
charsToDelete = 1;
QString tempString = line.mid(charsToDelete);
mDocument->putString(i-1,tempString);
}
mUndoList->AddChange(Item->changeReason(), Item->changeStartPos(),
Item->changeEndPos(), Item->changeStr(), Item->changeSelMode());
break;
}
default: default:
break; break;
} }
@ -5205,14 +5110,14 @@ void SynEdit::setSelectedTextEmpty()
BufferCoord vUndoEnd = mBlockEnd; BufferCoord vUndoEnd = mBlockEnd;
QString vSelText = selText(); QString vSelText = selText();
setSelTextPrimitive(""); setSelTextPrimitive("");
if ((vUndoBegin.Line < vUndoEnd.Line) || ( // if ((vUndoBegin.Line < vUndoEnd.Line) || (
(vUndoBegin.Line == vUndoEnd.Line) && (vUndoBegin.Char < vUndoEnd.Char))) { // (vUndoBegin.Line == vUndoEnd.Line) && (vUndoBegin.Char < vUndoEnd.Char))) {
mUndoList->AddChange(SynChangeReason::crDelete, vUndoBegin, vUndoEnd, vSelText, // mUndoList->AddChange(SynChangeReason::crDelete, vUndoBegin, vUndoEnd, vSelText,
mActiveSelectionMode); // mActiveSelectionMode);
} else { // } else {
mUndoList->AddChange(SynChangeReason::crDeleteAfterCursor, vUndoBegin, vUndoEnd, vSelText, // mUndoList->AddChange(SynChangeReason::crDeleteAfterCursor, vUndoBegin, vUndoEnd, vSelText,
mActiveSelectionMode); // mActiveSelectionMode);
} // }
} }
void SynEdit::setSelTextPrimitive(const QString &aValue) void SynEdit::setSelTextPrimitive(const QString &aValue)
@ -5234,14 +5139,45 @@ void SynEdit::setSelTextPrimitiveEx(SynSelectionMode PasteMode, const QString &V
PSynEditFoldRange foldRange = foldStartAtLine(BE.Line); PSynEditFoldRange foldRange = foldStartAtLine(BE.Line);
QString s = mDocument->getString(BE.Line-1); QString s = mDocument->getString(BE.Line-1);
if ((foldRange) && foldRange->collapsed && BE.Char>s.length()) { if ((foldRange) && foldRange->collapsed && BE.Char>s.length()) {
s=s+highlighter()->foldString(); QString newS=s+highlighter()->foldString();
if (BE.Char>s.length()) { if (selAvail()) {
if ((BB.Char<=s.length() || BB.Line<BE.Line)
&& BE.Char>newS.length() ) {
//selection has whole block
BE.Line = foldRange->toLine; BE.Line = foldRange->toLine;
BE.Char = mDocument->getString(BE.Line-1).length()+1; BE.Char = mDocument->getString(BE.Line-1).length()+1;
} else {
uncollapse(foldRange);
BE.Char = s.length()+1;
if (BB.Char>s.length())
BB.Char=s.length()+1;
} }
} else {
if (BE.Char>newS.length()) {
//caret at the end of the block
uncollapse(foldRange);
BE.Line = foldRange->toLine;
BE.Char = mDocument->getString(BE.Line-1).length()+1;
BB=BE;
} else {
//caret in the block { }
uncollapse(foldRange);
BE.Char = mDocument->getString(BE.Line-1).length()+1;
BB=BE;
}
}
mBlockBegin=BB;
mBlockEnd=BE;
mCaretX=BE.Char;
mCaretY=BE.Line;
} }
} }
if (selAvail()) { if (selAvail()) {
mUndoList->AddChange(SynChangeReason::crDelete,
BB,
BE,
selText(),
mActiveSelectionMode);
deleteSelection(BB,BE); deleteSelection(BB,BE);
if (mActiveSelectionMode == SynSelectionMode::smColumn) { if (mActiveSelectionMode == SynSelectionMode::smColumn) {
int colBegin = charToColumn(BB.Line,BB.Char); int colBegin = charToColumn(BB.Line,BB.Char);
@ -5270,9 +5206,9 @@ void SynEdit::doSetSelText(const QString &Value)
if (selAvail()) { if (selAvail()) {
mUndoList->BeginBlock(); mUndoList->BeginBlock();
blockBeginned = true; blockBeginned = true;
mUndoList->AddChange( // mUndoList->AddChange(
SynChangeReason::crDelete, mBlockBegin, mBlockEnd, // SynChangeReason::crDelete, mBlockBegin, mBlockEnd,
selText(), mActiveSelectionMode); // selText(), mActiveSelectionMode);
} }
// } else if (!colSelAvail()) // } else if (!colSelAvail())
// setActiveSelectionMode(selectionMode()); // setActiveSelectionMode(selectionMode());
@ -5836,16 +5772,15 @@ void SynEdit::deleteFromTo(const BufferCoord &start, const BufferCoord &end)
doOnPaintTransient(SynTransientType::ttBefore); doOnPaintTransient(SynTransientType::ttBefore);
if ((start.Char != end.Char) || (start.Line != end.Line)) { if ((start.Char != end.Char) || (start.Line != end.Line)) {
BufferCoord oldCaret = caretXY(); BufferCoord oldCaret = caretXY();
mUndoList->BeginBlock();
mUndoList->AddChange(SynChangeReason::crCaret, oldCaret, start,
"", activeSelectionMode());
mUndoList->AddChange(SynChangeReason::crSelection,mBlockBegin,mBlockEnd,"",activeSelectionMode());
setBlockBegin(start); setBlockBegin(start);
setBlockEnd(end); setBlockEnd(end);
setActiveSelectionMode(SynSelectionMode::smNormal); setActiveSelectionMode(SynSelectionMode::smNormal);
QString helper = selText(); QString helper = selText();
setSelTextPrimitive(""); setSelTextPrimitive("");
mUndoList->BeginBlock();
mUndoList->AddChange(SynChangeReason::crCaret, oldCaret, start,
"", SynSelectionMode::smNormal);
mUndoList->AddChange(SynChangeReason::crSilentDeleteAfterCursor, start, end,
helper, SynSelectionMode::smNormal);
mUndoList->EndBlock(); mUndoList->EndBlock();
internalSetCaretXY(start); internalSetCaretXY(start);
} }

View File

@ -161,24 +161,29 @@ private:
int calculateLineColumns(int Index); int calculateLineColumns(int Index);
}; };
enum class SynChangeReason {crInsert, crPaste, crDragDropInsert, enum class SynChangeReason {
//several undo entries can be chained together via the ChangeNumber crInsert,
//see also TCustomSynEdit.[Begin|End]UndoBlock methods crDelete,
crDeleteAfterCursor, crDelete,
crLineBreak, crIndent, crUnindent,
crSilentDelete, crSilentDeleteAfterCursor,
crAutoCompleteBegin, crAutoCompleteEnd,
crPasteBegin, crPasteEnd, //for pasting, since it might do a lot of operations
crSpecial1Begin, crSpecial1End,
crSpecial2Begin, crSpecial2End,
crLeftTop,
crCaret, //just restore the Caret, allowing better Undo behavior crCaret, //just restore the Caret, allowing better Undo behavior
crSelection, //restore Selection crSelection, //restore Selection
crNothing,
crGroupBreak, crGroupBreak,
crDeleteAll, crLeftTop,
crLineBreak,
crMoveSelectionUp, crMoveSelectionUp,
crMoveSelectionDown crMoveSelectionDown
//several undo entries can be chained together via the ChangeNumber
//see also TCustomSynEdit.[Begin|End]UndoBlock methods
// crDeleteAfterCursor,
// crLineBreak, crIndent, crUnindent,
// crSilentDelete, crSilentDeleteAfterCursor,
// crAutoCompleteBegin, crAutoCompleteEnd,
// crPasteBegin, crPasteEnd, //for pasting, since it might do a lot of operations
// crSpecial1Begin, crSpecial1End,
// crSpecial2Begin, crSpecial2End,
// crNothing,
// crDeleteAll,
}; };
class SynEditUndoItem { class SynEditUndoItem {
private: private: