Skip to content

Commit

Permalink
Avoid nested undo group when call EditReplaceAllInSelection().
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Dec 10, 2024
1 parent dd9bbb5 commit fa85fce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
26 changes: 15 additions & 11 deletions src/Edit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3613,7 +3613,7 @@ void EditStripTrailingBlanks(HWND hwnd, bool bIgnoreSelection) noexcept {
efrTrim->hwnd = hwnd;
efrTrim->fuFlags = SCFIND_REGEXP;
memcpy(efrTrim->szFind, "[ \t]+$", CSTRLEN("[ \t]+$"));
EditReplaceAllInSelection(hwnd, efrTrim);
EditReplaceAllInSelection(hwnd, efrTrim, EditReplaceAllFlag_UndoGroup);
NP2HeapFree(efrTrim);
return;
}
Expand Down Expand Up @@ -3653,7 +3653,7 @@ void EditStripLeadingBlanks(HWND hwnd, bool bIgnoreSelection) noexcept {
efrTrim->hwnd = hwnd;
efrTrim->fuFlags = SCFIND_REGEXP;
memcpy(efrTrim->szFind, "^[ \t]+", CSTRLEN("^[ \t]+"));
EditReplaceAllInSelection(hwnd, efrTrim);
EditReplaceAllInSelection(hwnd, efrTrim, EditReplaceAllFlag_UndoGroup);
NP2HeapFree(efrTrim);
return;
}
Expand Down Expand Up @@ -4369,7 +4369,7 @@ void EditEnsureConsistentLineEndings() noexcept {
return;
}
const size_t actions = SciCall_GetUndoActions();
if (lineCount + actions >= static_cast<size_t>(INT_MAX)) {
if (lineCount + actions >= MAX_NON_UTF8_SIZE) {
// Scintilla undo stack is indexed with int
return;
}
Expand All @@ -4382,7 +4382,7 @@ void EditEnsureConsistentLineEndings() noexcept {
#endif
{
const size_t dwLength = SciCall_GetLength() + lineCount;
if (dwLength >= static_cast<size_t>(INT_MAX)) {
if (dwLength >= MAX_NON_UTF8_SIZE) {
return;
}
}
Expand Down Expand Up @@ -5053,7 +5053,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar

case IDC_REPLACEINSEL:
bReplaceInitialized = true;
EditReplaceAllInSelection(lpefr->hwnd, lpefr, true);
EditReplaceAllInSelection(lpefr->hwnd, lpefr, EditReplaceAllFlag_Default);
break;
}
}
Expand Down Expand Up @@ -5796,7 +5796,8 @@ void EditReplaceAll(HWND hwnd, const EDITFINDREPLACE *lpefr) noexcept {
Sci_TextToFindFull ttf = { { 0, SciCall_GetLength() }, szFind2, { 0, 0 } };
Sci_Position iCount = 0;
while (SciCall_FindTextFull(searchFlags, &ttf) >= 0) {
if (++iCount == 1) {
++iCount;
if (iCount == 1) {
SciCall_BeginUndoAction();
}

Expand Down Expand Up @@ -5850,7 +5851,7 @@ void EditReplaceAll(HWND hwnd, const EDITFINDREPLACE *lpefr) noexcept {
//
// EditReplaceAllInSelection()
//
void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, bool bShowInfo) noexcept {
void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, EditReplaceAllFlag flag) noexcept {
if (SciCall_IsRectangleSelection()) {
NotifyRectangleSelection();
return;
Expand All @@ -5873,7 +5874,8 @@ void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, bool bSh
Sci_Position iCount = 0;
while (SciCall_FindTextFull(searchFlags, &ttf) >= 0) {
if (ttf.chrgText.cpMax <= SciCall_GetSelectionEnd()) {
if (++iCount == 1) {
++iCount;
if (iCount == 1 && (flag & EditReplaceAllFlag_UndoGroup) != 0) {
SciCall_BeginUndoAction();
}

Expand Down Expand Up @@ -5911,6 +5913,9 @@ void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, bool bSh

SendMessage(hwnd, WM_SETREDRAW, TRUE, 0);
if (iCount) {
if ((flag & EditReplaceAllFlag_UndoGroup) != 0) {
SciCall_EndUndoAction();
}
const Sci_Position iPos = SciCall_GetTargetEnd();
if (SciCall_GetSelectionEnd() < iPos) {
Sci_Position iAnchorPos = SciCall_GetAnchor();
Expand All @@ -5925,14 +5930,13 @@ void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, bool bSh
EditSelectEx(iAnchorPos, iCurrentPos);
}

SciCall_EndUndoAction();
InvalidateRect(hwnd, nullptr, TRUE);
}

// Remove wait cursor
EndWaitCursor();
LocalFree(pszReplace2);
if (bShowInfo) {
if ((flag & EditReplaceAllFlag_ShowInfo) != 0) {
ShwowReplaceCount(iCount);
}
}
Expand Down Expand Up @@ -6621,7 +6625,7 @@ void EditUpdateTimestampMatchTemplate(HWND hwnd) noexcept {
WideCharToMultiByte(cpEdit, 0, wchReplace, -1, efrTS->szReplace, COUNTOF(efrTS->szReplace), nullptr, nullptr);

if (!SciCall_IsSelectionEmpty()) {
EditReplaceAllInSelection(hwnd, efrTS, true);
EditReplaceAllInSelection(hwnd, efrTS, EditReplaceAllFlag_Default);
} else {
EditReplaceAll(hwnd, efrTS);
}
Expand Down
8 changes: 7 additions & 1 deletion src/Edit.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,14 @@ void EditFindNext(const EDITFINDREPLACE *lpefr, bool fExtendSelection) noexcept;
void EditFindPrev(const EDITFINDREPLACE *lpefr, bool fExtendSelection) noexcept;
void EditFindAll(const EDITFINDREPLACE *lpefr, bool selectAll) noexcept;
void EditReplace(HWND hwnd, const EDITFINDREPLACE *lpefr) noexcept;
enum EditReplaceAllFlag {
EditReplaceAllFlag_None,
EditReplaceAllFlag_UndoGroup,
EditReplaceAllFlag_ShowInfo,
EditReplaceAllFlag_Default,
};
void EditReplaceAll(HWND hwnd, const EDITFINDREPLACE *lpefr) noexcept;
void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, bool bShowInfo = false) noexcept;
void EditReplaceAllInSelection(HWND hwnd, const EDITFINDREPLACE *lpefr, EditReplaceAllFlag flag = EditReplaceAllFlag_None) noexcept;
bool EditLineNumDlg(HWND hwnd) noexcept;
void EditModifyLinesDlg(HWND hwnd) noexcept;
void EditEncloseSelectionDlg(HWND hwnd) noexcept;
Expand Down

0 comments on commit fa85fce

Please sign in to comment.