- fix: can't correctly undo/redo unindent

This commit is contained in:
Roy Qu 2021-11-30 21:39:13 +08:00
parent 9b01d9e85f
commit ce242fc2ab
2 changed files with 47 additions and 70 deletions

View File

@ -1,6 +1,8 @@
Version 0.10.4 For Dev-C++ 7 Beta Version 0.10.4 For Dev-C++ 7 Beta
- fix: indent can't be correctly undo - fix: can't correctly undo/redo indent
- fix: can't correctly undo/redo unindent
- change: press tab when there are selections will do indent - change: press tab when there are selections will do indent
- change: press shift+tab when there are selections will do unindent
Version 0.10.3 For Dev-C++ 7 Beta Version 0.10.3 For Dev-C++ 7 Beta
- enhancement: treat files ended with ".C" or ".CPP" as C++ files - enhancement: treat files ended with ".C" or ".CPP" as C++ files

View File

@ -2585,16 +2585,11 @@ void SynEdit::doBlockIndent()
QString strToInsert; QString strToInsert;
int e,x,i; int e,x,i;
QString spaces; QString spaces;
SynSelectionMode oldSelectionMode;
BufferCoord insertionPos; BufferCoord insertionPos;
oldSelectionMode = mActiveSelectionMode;
oldCaretPos = caretXY(); oldCaretPos = caretXY();
strToInsert = nullptr; strToInsert = nullptr;
auto action = finally([&,this]{
});
// keep current selection detail // keep current selection detail
if (selAvail()) { if (selAvail()) {
BB = blockBegin(); BB = blockBegin();
@ -2629,10 +2624,8 @@ void SynEdit::doBlockIndent()
auto action2=finally([this]{ auto action2=finally([this]{
mUndoList->EndBlock(); mUndoList->EndBlock();
}); });
mUndoList->AddChange(SynChangeReason::crCaret, oldCaretPos, oldCaretPos,"", oldSelectionMode); mUndoList->AddChange(SynChangeReason::crCaret, oldCaretPos, oldCaretPos,"", activeSelectionMode());
//We need to save the position of the end block for redo mUndoList->AddChange(SynChangeReason::crSelection,mBlockBegin,mBlockEnd,"", activeSelectionMode());
mUndoList->AddChange(SynChangeReason::crSelection,mBlockBegin,mBlockEnd,"", oldSelectionMode);
//adjust the x position of orgcaretpos appropriately
insertionPos.Line = BB.Line; insertionPos.Line = BB.Line;
if (mActiveSelectionMode == SynSelectionMode::smColumn) if (mActiveSelectionMode == SynSelectionMode::smColumn)
insertionPos.Char = std::min(BB.Char, BE.Char); insertionPos.Char = std::min(BB.Char, BE.Char);
@ -2645,11 +2638,10 @@ void SynEdit::doBlockIndent()
BB.Char += spaces.length(); BB.Char += spaces.length();
if (BE.Char > 1) if (BE.Char > 1)
BE.Char+=spaces.length(); BE.Char+=spaces.length();
mUndoList->AddChange(SynChangeReason::crSelection,BB,BE,"", oldSelectionMode); mUndoList->AddChange(SynChangeReason::crSelection,BB,BE,"", activeSelectionMode());
mUndoList->AddChange(SynChangeReason::crCaret,oldCaretPos,oldCaretPos,"",oldSelectionMode); mUndoList->AddChange(SynChangeReason::crCaret,oldCaretPos,oldCaretPos,"",activeSelectionMode());
setCaretAndSelection(oldCaretPos, setCaretAndSelection(oldCaretPos,
BB, BE); BB, BE);
setActiveSelectionMode(oldSelectionMode);
} }
} }
@ -2669,18 +2661,23 @@ void SynEdit::doBlockUnindent()
} }
BufferCoord oldCaretPos = caretXY(); BufferCoord oldCaretPos = caretXY();
int x = 0; int x = 0;
mUndoList->BeginBlock();
auto action1=finally([this]{
mUndoList->EndBlock();
});
mUndoList->AddChange(SynChangeReason::crCaret, oldCaretPos, oldCaretPos,"", activeSelectionMode());
mUndoList->AddChange(SynChangeReason::crSelection,mBlockBegin,mBlockEnd,"", activeSelectionMode());
int e = BE.Line; int e = BE.Line;
// convert selection to complete lines // convert selection to complete lines
if (BE.Char == 1) if (BE.Char == 1)
e = BE.Line - 1; e = BE.Line - 1;
// build string to delete // build string to delete
QString fullStrToDelete; QString strToDelete;
for (int i = BB.Line; i<= e;i++) { for (int i = BB.Line; i<= e;i++) {
QString line = mLines->getString(i - 1); QString line = mLines->getString(i - 1);
if (!fullStrToDelete.isEmpty()) if (!strToDelete.isEmpty())
fullStrToDelete+=lineBreak(); strToDelete+=lineBreak();
fullStrToDelete += line;
if (line.isEmpty()) if (line.isEmpty())
continue; continue;
if (line[0]!=' ' && line[0]!='\t') if (line[0]!=' ' && line[0]!='\t')
@ -2700,15 +2697,18 @@ void SynEdit::doBlockUnindent()
x = charsToDelete; x = charsToDelete;
QString tempString = line.mid(charsToDelete); QString tempString = line.mid(charsToDelete);
mLines->putString(i-1,tempString); mLines->putString(i-1,tempString);
strToDelete += line.mid(0,charsToDelete);
} }
mUndoList->AddChange( mUndoList->AddChange(
SynChangeReason::crUnindent, BB, BE, fullStrToDelete, mActiveSelectionMode); SynChangeReason::crUnindent, BB, BE, strToDelete, mActiveSelectionMode);
// restore selection // restore selection
//adjust the x position of orgcaretpos appropriately //adjust the x position of orgcaretpos appropriately
oldCaretPos.Char -= x; oldCaretPos.Char -= x;
BB.Char -= firstIndent; BB.Char -= firstIndent;
BE.Char -= lastIndent; BE.Char -= lastIndent;
mUndoList->AddChange(SynChangeReason::crSelection,BB,BE,"", activeSelectionMode());
mUndoList->AddChange(SynChangeReason::crCaret,oldCaretPos,oldCaretPos,"",activeSelectionMode());
setCaretAndSelection(oldCaretPos, BB, BE); setCaretAndSelection(oldCaretPos, BB, BE);
} }
@ -3039,9 +3039,11 @@ void SynEdit::doOnStatusChange(SynStatusChanges)
void SynEdit::insertBlock(const BufferCoord &BB, const BufferCoord &BE, const QString &ChangeStr, bool AddToUndoList) void SynEdit::insertBlock(const BufferCoord &BB, const BufferCoord &BE, const QString &ChangeStr, bool AddToUndoList)
{ {
SynSelectionMode oldSelMode = mActiveSelectionMode;
setCaretAndSelection(BB, BB, BE); setCaretAndSelection(BB, BB, BE);
setActiveSelectionMode(SynSelectionMode::smColumn); setActiveSelectionMode(SynSelectionMode::smColumn);
setSelTextPrimitiveEx(SynSelectionMode::smColumn, ChangeStr, AddToUndoList); setSelTextPrimitiveEx(SynSelectionMode::smColumn, ChangeStr, AddToUndoList);
setActiveSelectionMode(oldSelMode);
setStatusChanged(SynStatusChange::scSelection); setStatusChanged(SynStatusChange::scSelection);
} }
@ -4219,20 +4221,14 @@ void SynEdit::doUndoItem()
break; break;
case SynChangeReason::crUnindent: case SynChangeReason::crUnindent:
// reinsert the string // reinsert the string
if (Item->changeSelMode()!= SynSelectionMode::smColumn) {
insertBlock(BufferCoord{1, Item->changeStartPos().Line}, BufferCoord insertPos;
BufferCoord{1, Item->changeEndPos().Line}, insertPos.Line = Item->changeStartPos().Line;
Item->changeStr(), insertPos.Char = 1;
false); insertBlock(insertPos,
else { insertPos,
insertBlock(BufferCoord{Item->changeStartPos().Char, Item->changeStartPos().Line},
BufferCoord{Item->changeEndPos().Char, Item->changeEndPos().Line},
Item->changeStr(), false); Item->changeStr(), false);
} }
setCaretAndSelection(
Item->changeStartPos(),
Item->changeStartPos(),
Item->changeEndPos());
mRedoList->AddChange( mRedoList->AddChange(
Item->changeReason(), Item->changeReason(),
Item->changeStartPos(), Item->changeStartPos(),
@ -4414,47 +4410,26 @@ void SynEdit::doRedoItem()
Item->changeEndPos(), Item->changeStr(), Item->changeSelMode()); Item->changeEndPos(), Item->changeStr(), Item->changeSelMode());
break; break;
case SynChangeReason::crUnindent: { case SynChangeReason::crUnindent: {
// Delete string int e = Item->changeEndPos().Line;
QString StrToDelete = Item->changeStr(); // convert selection to complete lines
internalSetCaretY(Item->changeStartPos().Line); if (Item->changeEndPos().Char == 1)
int BeginX = 1; e = Item->changeEndPos().Line - 1;
if (Item->changeSelMode() == SynSelectionMode::smColumn)
BeginX = std::min(Item->changeStartPos().Char, Item->changeEndPos().Char);
int Run = 0;
int Len;
QString TempString; QString TempString;
do { for (int i = Item->changeStartPos().Line; i<= e;i++) {
Run = GetEOL(StrToDelete,Run); QString line = mLines->getString(i - 1);
if (Run != 0) { if (line.isEmpty())
Len = Run; continue;
if (Len > 0) { if (line[0]!=' ' && line[0]!='\t')
TempString = mLines->getString(mCaretY - 1); continue;
TempString.remove(BeginX-1,Len); int charsToDelete = 0;
mLines->putString(mCaretY-1,TempString); while (charsToDelete < mTabWidth &&
} charsToDelete < line.length() &&
} else line[charsToDelete] == ' ')
Len = 0; charsToDelete++;
if (StrToDelete[Run] == '\r') { if (charsToDelete == 0)
Run++; charsToDelete = 1;
if (StrToDelete[Run] == '\n') QString tempString = line.mid(charsToDelete);
Run++; mLines->putString(i-1,tempString);
mCaretY++;
}
} while (Run<StrToDelete.length());
if (Item->changeSelMode() == SynSelectionMode::smColumn) {
setCaretAndSelection(Item->changeStartPos(), Item->changeStartPos(),
Item->changeEndPos());
} else {
// restore selection
BufferCoord CaretPt;
if (mOptions.testFlag(eoTabsToSpaces))
CaretPt.Char = Item->changeStartPos().Char - mTabWidth;
else
CaretPt.Char = Item->changeStartPos().Char - 1;
CaretPt.Line = Item->changeStartPos().Line;
setCaretAndSelection(CaretPt, CaretPt,
BufferCoord{Item->changeEndPos().Char - Len,
Item->changeEndPos().Line});
} }
mUndoList->AddChange(Item->changeReason(), Item->changeStartPos(), mUndoList->AddChange(Item->changeReason(), Item->changeStartPos(),
Item->changeEndPos(), Item->changeStr(), Item->changeSelMode()); Item->changeEndPos(), Item->changeStr(), Item->changeSelMode());