From 726043d14495842eab2600250d3c858c3b6deb64 Mon Sep 17 00:00:00 2001 From: Roy Qu Date: Thu, 4 Apr 2024 19:55:48 +0800 Subject: [PATCH] fix #370 : Can't correctly undo/redo "Delete current line". --- NEWS.md | 1 + libs/qsynedit/qsynedit/qsynedit.cpp | 68 +++++++++++++---------------- 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/NEWS.md b/NEWS.md index 1c01bb03..28ac5571 100644 --- a/NEWS.md +++ b/NEWS.md @@ -114,6 +114,7 @@ Red Panda C++ Version 2.27 - enhancement: Support "extern template" in code parser. - enhancement: Set shortcuts for tools menu item. - enhancement: Enhancement for custom tools. + - fix: Can't correctly undo/redo "Delete current line". Red Panda C++ Version 2.26 - enhancement: Code suggestion for embedded std::vectors. diff --git a/libs/qsynedit/qsynedit/qsynedit.cpp b/libs/qsynedit/qsynedit/qsynedit.cpp index a7921893..e4e564c7 100644 --- a/libs/qsynedit/qsynedit/qsynedit.cpp +++ b/libs/qsynedit/qsynedit/qsynedit.cpp @@ -1964,46 +1964,40 @@ void QSynEdit::doDeleteFromBOL() void QSynEdit::doDeleteLine() { - if (!mReadOnly && (mDocument->count() > 0)) { - PCodeFoldingRange foldRange=foldStartAtLine(mCaretY); - if (foldRange && foldRange->collapsed) - return; - beginEditing(); - addCaretToUndo(); - addSelectionToUndo(); - if (selAvail()) - setBlockBegin(caretXY()); - QStringList helper(lineText()); - if (mCaretY == mDocument->count()) { - if (mDocument->count()==1) { - mDocument->putLine(mCaretY - 1,""); - mUndoList->addChange(ChangeReason::Delete, - BufferCoord{1, mCaretY}, - BufferCoord{helper.length() + 1, mCaretY}, - helper, SelectionMode::Normal); - } else { - QString s = mDocument->getLine(mCaretY-2); - mDocument->deleteAt(mCaretY - 1); - helper.insert(0,""); - mUndoList->addChange(ChangeReason::Delete, - BufferCoord{s.length()+1, mCaretY-1}, - BufferCoord{helper.length() + 1, mCaretY}, - helper, SelectionMode::Normal); - doLinesDeleted(mCaretY, 1); - mCaretY--; - } + if (mReadOnly || (mDocument->count() <= 0)) + return; + PCodeFoldingRange foldRange=foldStartAtLine(mCaretY); + if (foldRange && foldRange->collapsed) + return; + beginEditing(); + addCaretToUndo(); + addSelectionToUndo(); + if (selAvail()) + setBlockBegin(caretXY()); + int oldCaretY = caretY(); + bool isLastLine = (oldCaretY >= mDocument->count()); + bool isFirstLine = (oldCaretY == 1); + BufferCoord startPos; + BufferCoord endPos; + if (isLastLine) { + if (isFirstLine) { + startPos.ch = 1; + startPos.line = oldCaretY; } else { - mDocument->deleteAt(mCaretY - 1); - helper.append(""); - mUndoList->addChange(ChangeReason::Delete, - BufferCoord{1, mCaretY}, - BufferCoord{helper.length() + 1, mCaretY}, - helper, SelectionMode::Normal); - doLinesDeleted(mCaretY, 1); + startPos.ch = mDocument->getLine(oldCaretY-2).length()+1; + startPos.line = oldCaretY-1; } - endEditing(); - internalSetCaretXY(BufferCoord{1, mCaretY}); // like seen in the Delphi editor + endPos.ch = lineText().length()+1; + endPos.line = oldCaretY; + } else { + startPos.ch = 1; + startPos.line = oldCaretY; + endPos.ch = 1; + endPos.line = oldCaretY+1; } + doDeleteText(startPos, endPos, SelectionMode::Normal); + endEditing(); + internalSetCaretXY(BufferCoord{1, oldCaretY}); } void QSynEdit::doSelectLine()