From 7bbe4d1ff88fba41dec8750843f31bb542cd0d5e Mon Sep 17 00:00:00 2001 From: Don Ho Date: Wed, 27 Sep 2023 15:38:13 +0200 Subject: [PATCH] Fix "Single Line Uncomment" uncomments an extra line issue Fix from: https://github.com/notepad-plus-plus/notepad-plus-plus/issues/12829#issuecomment-1737330242 Fix #12829 --- PowerEditor/src/Notepad_plus.cpp | 74 ++++++++++++++++---------------- 1 file changed, 36 insertions(+), 38 deletions(-) diff --git a/PowerEditor/src/Notepad_plus.cpp b/PowerEditor/src/Notepad_plus.cpp index 24ca5e0e5..f4aa71ff2 100644 --- a/PowerEditor/src/Notepad_plus.cpp +++ b/PowerEditor/src/Notepad_plus.cpp @@ -8459,26 +8459,26 @@ DWORD WINAPI Notepad_plus::backupDocument(void * /*param*/) //-- undoStreamComment: New function to undo stream comment around or within selection end-points. bool Notepad_plus::undoStreamComment(bool tryBlockComment) { - const TCHAR *commentStart; - const TCHAR *commentEnd; - const TCHAR *commentLineSymbol; + const TCHAR* commentStart; + const TCHAR* commentEnd; + const TCHAR* commentLineSymbol; generic_string symbolStart; generic_string symbolEnd; generic_string symbol; const int charbufLen = 10; - TCHAR charbuf[charbufLen]; + TCHAR charbuf[charbufLen]{}; bool retVal = false; - Buffer * buf = _pEditView->getCurrentBuffer(); + Buffer* buf = _pEditView->getCurrentBuffer(); //-- Avoid side-effects (e.g. cursor moves number of comment-characters) when file is read-only. if (buf->isReadOnly()) return false; if (buf->getLangType() == L_USER) { - UserLangContainer * userLangContainer = NppParameters::getInstance().getULCFromName(buf->getUserDefineLangName()); + UserLangContainer* userLangContainer = NppParameters::getInstance().getULCFromName(buf->getUserDefineLangName()); if (!userLangContainer) return false; @@ -8528,7 +8528,7 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) //-- Note: The caretPosition is either at selectionEnd or at selectionStart!! selectionStart is always before (smaller) than selectionEnd!! //-- First, search all start_comment and end_comment before and after the selectionStart and selectionEnd position. - const int iSelStart=0, iSelEnd=1; + const int iSelStart = 0, iSelEnd = 1; const size_t N_CMNT = 2; intptr_t posStartCommentBefore[N_CMNT], posEndCommentBefore[N_CMNT], posStartCommentAfter[N_CMNT], posEndCommentAfter[N_CMNT]; bool blnStartCommentBefore[N_CMNT], blnEndCommentBefore[N_CMNT], blnStartCommentAfter[N_CMNT], blnEndCommentAfter[N_CMNT]; @@ -8562,8 +8562,8 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) && (!blnEndCommentBefore[iSelStart] || (posStartCommentBefore[iSelStart] >= posEndCommentBefore[iSelStart])) && (!blnStartCommentAfter[iSelStart] || (posEndCommentAfter[iSelStart] <= posStartCommentAfter[iSelStart]))) { - posStartComment = posStartCommentBefore[iSelStart]; - posEndComment = posEndCommentAfter[iSelStart]; + posStartComment = posStartCommentBefore[iSelStart]; + posEndComment = posEndCommentAfter[iSelStart]; } else //-- Second, check if there is a stream-comment around the selectionEnd position: { @@ -8583,16 +8583,16 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) && (!blnEndCommentBefore[iSelEnd] || (posStartCommentBefore[iSelEnd] >= posEndCommentBefore[iSelEnd])) && (!blnStartCommentAfter[iSelEnd] || (posEndCommentAfter[iSelEnd] <= posStartCommentAfter[iSelEnd]))) { - posStartComment = posStartCommentBefore[iSelEnd]; - posEndComment = posEndCommentAfter[iSelEnd]; + posStartComment = posStartCommentBefore[iSelEnd]; + posEndComment = posEndCommentAfter[iSelEnd]; } //-- Third, check if there is a stream-comment within the selected area: - else if ( (blnStartCommentAfter[iSelStart] && (posStartCommentAfter[iSelStart] < selectionEnd)) - && (blnEndCommentBefore[iSelEnd] && (posEndCommentBefore[iSelEnd] > selectionStart))) + else if ((blnStartCommentAfter[iSelStart] && (posStartCommentAfter[iSelStart] < selectionEnd)) + && (blnEndCommentBefore[iSelEnd] && (posEndCommentBefore[iSelEnd] > selectionStart))) { - //-- If there are more than one stream-comment within the selection, take the first one after selectionStart!! - posStartComment = posStartCommentAfter[iSelStart]; - posEndComment = posEndCommentAfter[iSelStart]; + //-- If there are more than one stream-comment within the selection, take the first one after selectionStart!! + posStartComment = posStartCommentAfter[iSelStart]; + posEndComment = posEndCommentAfter[iSelStart]; } //-- Finally, if there is no stream-comment, return else @@ -8605,16 +8605,16 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) //-- Ok, there are valid start-comment and valid end-comment around the caret-position. // Now, un-comment stream-comment: retVal = true; - intptr_t startCommentLength = start_comment_length; - intptr_t endCommentLength = end_comment_length; + intptr_t startCommentLength = static_cast(start_comment_length); + intptr_t endCommentLength = static_cast(end_comment_length); //-- First delete end-comment, so that posStartCommentBefore does not change! //-- Get character before end-comment to decide, if there is a white character before the end-comment, which will be removed too! - _pEditView->getGenericText(charbuf, charbufLen, posEndComment-1, posEndComment); - if (wcsncmp(charbuf, white_space.c_str(), white_space.length()) == 0) + _pEditView->getGenericText(charbuf, charbufLen, posEndComment - 1, posEndComment); + if (wcsnicmp(charbuf, white_space.c_str(), white_space.length()) == 0) { - endCommentLength +=1; - posEndComment-=1; + endCommentLength += 1; + posEndComment -= 1; } //-- Delete end stream-comment string --------- _pEditView->execute(SCI_BEGINUNDOACTION); @@ -8622,9 +8622,9 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) _pEditView->execute(SCI_REPLACESEL, 0, reinterpret_cast("")); //-- Get character after start-comment to decide, if there is a white character after the start-comment, which will be removed too! - _pEditView->getGenericText(charbuf, charbufLen, posStartComment+startCommentLength, posStartComment+startCommentLength+1); - if (wcsncmp(charbuf, white_space.c_str(), white_space.length()) == 0) - startCommentLength +=1; + _pEditView->getGenericText(charbuf, charbufLen, posStartComment + startCommentLength, posStartComment + startCommentLength + 1); + if (wcsnicmp(charbuf, white_space.c_str(), white_space.length()) == 0) + startCommentLength += 1; //-- Delete starting stream-comment string --------- _pEditView->execute(SCI_SETSEL, posStartComment, posStartComment + startCommentLength); @@ -8636,36 +8636,34 @@ bool Notepad_plus::undoStreamComment(bool tryBlockComment) // selectionStart if (selectionStart > posStartComment) { - if (selectionStart >= posStartComment+startCommentLength) - selectionStartMove = -startCommentLength; + if (selectionStart >= posStartComment + startCommentLength) + selectionStartMove = -static_cast(startCommentLength); else - selectionStartMove = -selectionStart - posStartComment; + selectionStartMove = -static_cast(selectionStart - posStartComment); } else selectionStartMove = 0; // selectionEnd - if (selectionEnd >= posEndComment+endCommentLength) - selectionEndMove = -startCommentLength+endCommentLength; + if (selectionEnd >= posEndComment + endCommentLength) + selectionEndMove = -static_cast(startCommentLength + endCommentLength); else if (selectionEnd <= posEndComment) - selectionEndMove = -startCommentLength; + selectionEndMove = -static_cast(startCommentLength); else - selectionEndMove = -startCommentLength + (selectionEnd - posEndComment); + selectionEndMove = -static_cast(startCommentLength + (selectionEnd - posEndComment)); //-- Reset selection of text without deleted stream-comment-string if (move_caret) { // moving caret to the beginning of selected block - _pEditView->execute(SCI_GOTOPOS, selectionEnd+selectionEndMove); - _pEditView->execute(SCI_SETCURRENTPOS, selectionStart+selectionStartMove); + _pEditView->execute(SCI_GOTOPOS, selectionEnd + selectionEndMove); + _pEditView->execute(SCI_SETCURRENTPOS, selectionStart + selectionStartMove); } else { - _pEditView->execute(SCI_SETSEL, selectionStart+selectionStartMove, selectionEnd+selectionEndMove); + _pEditView->execute(SCI_SETSEL, selectionStart + selectionStartMove, selectionEnd + selectionEndMove); } - } - while (1); //do as long as stream-comments are within selection - + } while (1); //do as long as stream-comments are within selection } void Notepad_plus::monitoringStartOrStopAndUpdateUI(Buffer* pBuf, bool isStarting)