Skip to content

Commit

Permalink
+fix: remove duplicate lines
Browse files Browse the repository at this point in the history
  • Loading branch information
RaiKoHoff committed Mar 6, 2024
1 parent 56d5d14 commit 1b0a170
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
76 changes: 45 additions & 31 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -4616,55 +4616,69 @@ void EditRemoveDuplicateLines(HWND hwnd, bool bRemoveEmptyLines)
DocPos const iSelEnd = SciCall_GetSelectionEnd();

DocLn iStartLine = 0;
DocLn iEndLine = 0;
DocLn iEndLine = Sci_GetLastDocLineNumber();

// correction for stream selection
if (iSelStart != iSelEnd) {
iStartLine = SciCall_LineFromPosition(iSelStart);
if (iSelStart > SciCall_PositionFromLine(iStartLine)) {
++iStartLine;
++iStartLine; // not beginning of line
}
iEndLine = SciCall_LineFromPosition(iSelEnd);
if (iSelEnd <= SciCall_PositionFromLine(iEndLine)) {
--iEndLine;
if (iSelEnd < SciCall_GetLineEndPosition(iEndLine)) {
--iEndLine;
}
} else {
iEndLine = Sci_GetLastDocLineNumber();
}

if ((iEndLine - iStartLine) <= 1) {
if (iStartLine == iEndLine) {
return;
}
assert(iStartLine < iEndLine);

DocPos const iMaxLineLen = Sci_GetRangeMaxLineLength(iStartLine, iEndLine) + 1;
char* const pCurrentLine = AllocMem(iMaxLineLen, HEAP_ZERO_MEMORY);
if (!pCurrentLine)
return;

UndoTransActionBegin();

for (DocLn iCurLine = iStartLine; iCurLine < iEndLine; ++iCurLine) {
DocPos const iCurLnLen = Sci_GetNetLineLength(iCurLine);
DocPos const iBegCurLine = SciCall_PositionFromLine(iCurLine);
const char* const pCurrentLine = SciCall_GetRangePointer(iBegCurLine, iCurLnLen + 1);

if (bRemoveEmptyLines || (iCurLnLen > 0)) {
DocLn iPrevLine = iCurLine;

for (DocLn iCompareLine = iCurLine + 1; iCompareLine <= iEndLine; ++iCompareLine) {
DocPos const iCmpLnLen = Sci_GetNetLineLength(iCompareLine);
if (bRemoveEmptyLines || (iCmpLnLen > 0)) {
DocPos const iBegCmpLine = SciCall_PositionFromLine(iCompareLine);
const char* const pCompareLine = SciCall_GetRangePointer(iBegCmpLine, iCmpLnLen + 1);
if (iCurLnLen == iCmpLnLen) {
if (IsSameCharSequence(pCurrentLine, pCompareLine, iCmpLnLen)) {
SciCall_SetTargetRange(SciCall_GetLineEndPosition(iPrevLine), SciCall_GetLineEndPosition(iCompareLine));
SciCall_ReplaceTarget(0, "");
--iCompareLine; // proactive preventing progress to avoid comparison line skip
--iEndLine;
}
}
} // empty
iPrevLine = iCompareLine;
DocLn iCurLine = iStartLine;
while (iCurLine < iEndLine) {

DocPos const iCurLnLen = Sci_GetNetLineLength(iCurLine);
DocPos const iBegCurLine = SciCall_PositionFromLine(iCurLine);
// range-pointer may move during line deletion, so copy current line for const comparison
strncpy_s(pCurrentLine, iMaxLineLen, SciCall_GetRangePointer(iBegCurLine, iCurLnLen + 1), iCurLnLen);
pCurrentLine[iCurLnLen] = '\0';

DocLn iPrevLine = iCurLine;
DocLn iCompareLine = iCurLine;
while (++iCompareLine <= iEndLine) {

DocPos const iCmpLnLen = Sci_GetNetLineLength(iCompareLine);
if (bRemoveEmptyLines || (iCmpLnLen > 0)) {

DocPos const iBegCmpLine = SciCall_PositionFromLine(iCompareLine);
const char* const pCompareLine = SciCall_GetRangePointer(iBegCmpLine, iCmpLnLen);

if ((iCurLnLen == iCmpLnLen) && IsSameCharSequence(pCurrentLine, pCompareLine, iCmpLnLen)) {
DocPos const posPrev = SciCall_GetLineEndPosition(iPrevLine);
DocPos const posComp = SciCall_GetLineEndPosition(iCompareLine);
assert(posPrev != posComp);
SciCall_SetTargetRange(posPrev, posComp);
SciCall_ReplaceTarget(0, "");
--iEndLine; // line inbetween removed
--iCompareLine; // don't proceed compare-line
}
}
} // empty
iPrevLine = iCompareLine;
}
++iCurLine;
}

EndUndoTransAction();

FreeMem(pCurrentLine);
}


Expand Down
6 changes: 3 additions & 3 deletions src/Notepad3.c
Original file line number Diff line number Diff line change
Expand Up @@ -9082,7 +9082,7 @@ static LRESULT _MsgNotifyFromEdit(HWND hwnd, const SCNotification* const scn)
//~SciCall_GotoLine(SciCall_LineFromPosition(scn->position));
// fallthrough
default:
return 0;
return FALSE; // not swallowed
}
}
break;
Expand Down Expand Up @@ -9120,9 +9120,9 @@ static LRESULT _MsgNotifyFromEdit(HWND hwnd, const SCNotification* const scn)
#endif

default:
return FALSE;
return FALSE; // not swallowed
}
return TRUE;
return TRUE; // swallowed
}


Expand Down

0 comments on commit 1b0a170

Please sign in to comment.