Improve folding/unfolding performance for large files

- Use the Scintilla dedicated command SCI_FOLDALL to replace the inefficient loop for "Fold All" command.
- Fix "Fold level" & switching back to large files folding performance.
- Refactoring the hide/show lines functions.

Note that there might be a regression of URL link:
https://github.com/notepad-plus-plus/notepad-plus-plus/pull/16200/files#r1961937279

The commit is based on code of #16064.

Fix #16064, close #16200
This commit is contained in:
Don Ho 2025-02-17 13:37:43 +01:00
parent a52738c1dd
commit 83755ca155
7 changed files with 242 additions and 210 deletions

View File

@ -2227,7 +2227,7 @@ void Notepad_plus::command(int id)
case IDM_VIEW_FOLD_7:
case IDM_VIEW_FOLD_8:
_isFolding = true; // So we can ignore events while folding is taking place
_pEditView->collapse(id - IDM_VIEW_FOLD - 1, fold_collapse);
_pEditView->foldLevel(id - IDM_VIEW_FOLD - 1, fold_collapse);
_isFolding = false;
break;
@ -2240,7 +2240,7 @@ void Notepad_plus::command(int id)
case IDM_VIEW_UNFOLD_7:
case IDM_VIEW_UNFOLD_8:
_isFolding = true; // So we can ignore events while folding is taking place
_pEditView->collapse(id - IDM_VIEW_UNFOLD - 1, fold_expand);
_pEditView->foldLevel(id - IDM_VIEW_UNFOLD - 1, fold_expand);
_isFolding = false;
break;

View File

@ -147,7 +147,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
}
else if ((notification->margin == ScintillaEditView::_SC_MARGE_SYMBOL) && !notification->modifiers)
{
if (!_pEditView->markerMarginClick(lineClick))
if (!_pEditView->hidelineMarkerClicked(lineClick))
bookmarkToggle(lineClick);
}
break;
@ -180,28 +180,6 @@ BOOL Notepad_plus::notify(SCNotification *notification)
break;
}
case SCN_FOLDINGSTATECHANGED:
{
if ((notification->nmhdr.hwndFrom == _mainEditView.getHSelf()) || (notification->nmhdr.hwndFrom == _subEditView.getHSelf()))
{
size_t lineClicked = notification->line;
if (!_isFolding)
{
int urlAction = (NppParameters::getInstance()).getNppGUI()._styleURL;
Buffer* currentBuf = _pEditView->getCurrentBuffer();
if (urlAction != urlDisable && currentBuf->allowClickableLink())
{
addHotSpot();
}
}
if (_pDocMap)
_pDocMap->fold(lineClicked, _pEditView->isFolded(lineClicked));
}
return TRUE;
}
case SCN_CHARADDED:
{
if (!notifyView) return FALSE;
@ -582,6 +560,30 @@ BOOL Notepad_plus::notify(SCNotification *notification)
// ======= End of SCN_*
//
case SCN_FOLDINGSTATECHANGED: // Notification not part of Scintilla, but Notepad++ added
{
if ((notification->nmhdr.hwndFrom == _mainEditView.getHSelf()) || (notification->nmhdr.hwndFrom == _subEditView.getHSelf()))
{
size_t lineClicked = notification->line;
/*
if (!_isFolding)
{
int urlAction = (NppParameters::getInstance()).getNppGUI()._styleURL;
Buffer* currentBuf = _pEditView->getCurrentBuffer();
if (urlAction != urlDisable && currentBuf->allowClickableLink())
{
addHotSpot();
}
}
*/
if (_pDocMap)
_pDocMap->fold(lineClicked, _pEditView->isFolded(lineClicked));
}
return TRUE;
}
case TCN_MOUSEHOVERING:
case TCN_MOUSEHOVERSWITCHING:

View File

@ -649,13 +649,13 @@ void Buffer::setHideLineChanged(bool isHide, size_t location)
{
//First run through all docs without removing markers
for (int i = 0; i < _references; ++i)
_referees.at(i)->notifyMarkers(this, isHide, location, false); // (i == _references-1));
_referees.at(i)->notifyHidelineMarkers(this, isHide, location, false); // (i == _references-1));
if (!isHide) // no deleting if hiding lines
{
//Then all docs to remove markers.
for (int i = 0; i < _references; ++i)
_referees.at(i)->notifyMarkers(this, isHide, location, true);
_referees.at(i)->notifyHidelineMarkers(this, isHide, location, true);
}
}

View File

@ -5581,7 +5581,7 @@ void Finder::beginNewFilesSearch()
_nbFoundFiles = 0;
// fold all old searches (1st level only)
_scintView.collapse(searchHeaderLevel - SC_FOLDLEVELBASE, fold_collapse);
_scintView.foldLevel(searchHeaderLevel - SC_FOLDLEVELBASE, fold_collapse);
}
void Finder::finishFilesSearch(int count, int searchedCount, bool searchedEntireNotSelection, const FindOption* pFindOpt)

View File

@ -2399,10 +2399,7 @@ void ScintillaEditView::activateBuffer(BufferID buffer, bool force)
syncFoldStateWith(lineStateVectorNew);
restoreCurrentPosPreStep();
//runMarkers(true, 0, true, false);
restoreHiddenLines();
setCRLF();
NppParameters& nppParam = NppParameters::getInstance();
@ -2448,10 +2445,24 @@ void ScintillaEditView::getCurrentFoldStates(std::vector<size_t> & lineStateVect
void ScintillaEditView::syncFoldStateWith(const std::vector<size_t> & lineStateVectorNew)
{
size_t nbLineState = lineStateVectorNew.size();
for (size_t i = 0 ; i < nbLineState ; ++i)
if (nbLineState > 0)
{
if (nbLineState > MAX_FOLD_LINES_MORE_THAN) // Block WM_SETREDRAW messages
::SendMessage(_hSelf, WM_SETREDRAW, FALSE, 0);
for (size_t i = 0; i < nbLineState; ++i)
{
auto line = lineStateVectorNew.at(i);
fold(line, false);
fold(line, fold_collapse, false);
}
if (nbLineState > MAX_FOLD_LINES_MORE_THAN)
{
::SendMessage(_hSelf, WM_SETREDRAW, TRUE, 0);
execute(SCI_SCROLLCARET);
::InvalidateRect(_hSelf, nullptr, TRUE);
}
}
}
@ -2537,16 +2548,19 @@ struct FoldLevelStack
}
void ScintillaEditView::collapseFoldIndentationBased(int level2Collapse, bool mode)
void ScintillaEditView::foldIndentationBasedLevel(int level2Collapse, bool mode)
{
execute(SCI_COLOURISE, 0, -1);
FoldLevelStack levelStack;
++level2Collapse; // 1-based level number
const intptr_t maxLine = execute(SCI_GETLINECOUNT);
intptr_t line = 0;
if (maxLine > MAX_FOLD_LINES_MORE_THAN)
{
::SendMessage(_hSelf, WM_SETREDRAW, FALSE, 0);
}
while (line < maxLine)
{
intptr_t level = execute(SCI_GETFOLDLEVEL, line);
@ -2568,21 +2582,34 @@ void ScintillaEditView::collapseFoldIndentationBased(int level2Collapse, bool mo
++line;
}
runMarkers(true, 0, true, false);
if (maxLine > MAX_FOLD_LINES_MORE_THAN)
{
::SendMessage(_hSelf, WM_SETREDRAW, TRUE, 0);
execute(SCI_SCROLLCARET);
::InvalidateRect(_hSelf, nullptr, TRUE);
}
if (mode == fold_expand)
hideMarkedLines(0, true);
}
void ScintillaEditView::collapse(int level2Collapse, bool mode)
void ScintillaEditView::foldLevel(int level2Collapse, bool mode)
{
if (isFoldIndentationBased())
{
collapseFoldIndentationBased(level2Collapse, mode);
foldIndentationBasedLevel(level2Collapse, mode);
return;
}
execute(SCI_COLOURISE, 0, -1);
intptr_t maxLine = execute(SCI_GETLINECOUNT);
if (maxLine > MAX_FOLD_LINES_MORE_THAN)
{
::SendMessage(_hSelf, WM_SETREDRAW, FALSE, 0);
}
for (int line = 0; line < maxLine; ++line)
{
intptr_t level = execute(SCI_GETFOLDLEVEL, line);
@ -2597,7 +2624,15 @@ void ScintillaEditView::collapse(int level2Collapse, bool mode)
}
}
runMarkers(true, 0, true, false);
if (maxLine > MAX_FOLD_LINES_MORE_THAN)
{
::SendMessage(_hSelf, WM_SETREDRAW, TRUE, 0);
execute(SCI_SCROLLCARET);
::InvalidateRect(_hSelf, nullptr, TRUE);
}
if (mode == fold_expand)
hideMarkedLines(0, true);
}
void ScintillaEditView::foldCurrentPos(bool mode)
@ -2626,14 +2661,8 @@ bool ScintillaEditView::isCurrentLineFolded() const
return !isExpanded;
}
void ScintillaEditView::fold(size_t line, bool mode)
void ScintillaEditView::fold(size_t line, bool mode, bool shouldBeNotified/* = true*/)
{
auto endStyled = execute(SCI_GETENDSTYLED);
auto len = execute(SCI_GETTEXTLENGTH);
if (endStyled < len)
execute(SCI_COLOURISE, 0, -1);
intptr_t headerLine;
auto level = execute(SCI_GETFOLDLEVEL, line);
@ -2650,27 +2679,27 @@ void ScintillaEditView::fold(size_t line, bool mode)
{
execute(SCI_TOGGLEFOLD, headerLine);
if (shouldBeNotified)
{
SCNotification scnN{};
scnN.nmhdr.code = SCN_FOLDINGSTATECHANGED;
scnN.nmhdr.hwndFrom = _hSelf;
scnN.nmhdr.idFrom = 0;
scnN.line = headerLine;
scnN.foldLevelNow = isFolded(headerLine)?1:0; //folded:1, unfolded:0
scnN.foldLevelNow = isFolded(headerLine) ? 1 : 0; //folded:1, unfolded:0
::SendMessage(_hParent, WM_NOTIFY, 0, reinterpret_cast<LPARAM>(&scnN));
}
}
}
void ScintillaEditView::foldAll(bool mode)
{
auto maxLine = execute(SCI_GETLINECOUNT);
execute(SCI_FOLDALL, (mode == fold_expand ? SC_FOLDACTION_EXPAND : SC_FOLDACTION_CONTRACT) | SC_FOLDACTION_CONTRACT_EVERY_LEVEL, 0);
for (int line = 0; line < maxLine; ++line)
if (mode == fold_expand)
{
auto level = execute(SCI_GETFOLDLEVEL, line);
if (level & SC_FOLDLEVELHEADERFLAG)
if (isFolded(line) != mode)
fold(line, mode);
hideMarkedLines(0, true);
execute(SCI_SCROLLCARET);
}
}
@ -3015,7 +3044,9 @@ void ScintillaEditView::marginClick(Sci_Position position, int modifiers)
// Toggle this line
bool mode = isFolded(lineClick);
fold(lineClick, !mode);
runMarkers(true, lineClick, true, false);
if (!mode == fold_expand) // after toggling
hideMarkedLines(lineClick, true);
}
}
}
@ -3067,7 +3098,8 @@ void ScintillaEditView::expand(size_t& line, bool doExpand, bool force, intptr_t
++line;
}
runMarkers(true, 0, true, false);
if (doExpand)
hideMarkedLines(0, true);
}
@ -4015,19 +4047,21 @@ void ScintillaEditView::scrollPosToCenter(size_t pos)
void ScintillaEditView::hideLines()
{
//Folding can screw up hide lines badly if it unfolds a hidden section.
//Adding runMarkers(hide, foldstart) directly (folding on single document) can help
// Unfolding can screw up hide lines badly if it unfolds a hidden section.
// Using hideMarkedLines() after unfolding can help
//Special func on buffer. If markers are added, create notification with location of start, and hide bool set to true
size_t startLine = execute(SCI_LINEFROMPOSITION, execute(SCI_GETSELECTIONSTART));
size_t endLine = execute(SCI_LINEFROMPOSITION, execute(SCI_GETSELECTIONEND));
//perform range check: cannot hide very first and very last lines
//Offset them one off the edges, and then check if they are within the reasonable
// perform range check: cannot hide very first and very last lines
// Offset them one off the edges, and then check if they are within the reasonable
size_t nbLines = execute(SCI_GETLINECOUNT);
if (nbLines < 3)
return; //cannot possibly hide anything
if (!startLine)
++startLine;
if (endLine == (nbLines-1))
--endLine;
@ -4064,8 +4098,10 @@ void ScintillaEditView::hideLines()
// Previous markers must be removed in the selected region:
removeMarker(startMarker, 1 << MARK_HIDELINESBEGIN);
for (size_t i = startLine; i <= endLine; ++i)
removeMarker(i, (1 << MARK_HIDELINESBEGIN) | (1 << MARK_HIDELINESEND));
removeMarker(endMarker, 1 << MARK_HIDELINESEND);
// When hiding lines just below/above other hidden lines,
@ -4100,7 +4136,7 @@ void ScintillaEditView::hideLines()
_currentBuffer->setHideLineChanged(true, startMarker);
}
bool ScintillaEditView::markerMarginClick(intptr_t lineNumber)
bool ScintillaEditView::hidelineMarkerClicked(intptr_t lineNumber)
{
auto state = execute(SCI_MARKERGET, lineNumber);
bool openPresent = (state & (1 << MARK_HIDELINESBEGIN)) != 0;
@ -4139,21 +4175,24 @@ bool ScintillaEditView::markerMarginClick(intptr_t lineNumber)
return true;
}
void ScintillaEditView::notifyMarkers(Buffer * buf, bool isHide, size_t location, bool del)
void ScintillaEditView::notifyHidelineMarkers(Buffer * buf, bool isHide, size_t location, bool del)
{
if (buf != _currentBuffer) //if not visible buffer dont do a thing
return;
runMarkers(isHide, location, false, del);
if (isHide)
hideMarkedLines(location, false);
else
showHiddenLines(location, false, del);
}
//Run through full document. When switching in or opening folding
//hide is false only when user click on margin
void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDoc, bool doDelete)
{
//Removes markers if opening
/*
AllLines = (start,ENDOFDOCUMENT)
Hide:
//Removes markers if opening
/*
AllLines = (start,ENDOFDOCUMENT)
Hide:
Run through all lines.
Find open hiding marker:
set hiding start
@ -4165,7 +4204,7 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
else
search for other hidden sections
Show:
Show:
Run through all lines
Find open hiding marker
set last start
@ -4175,12 +4214,15 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
Show from last start to folding header
Skip to LASTCHILD
Set last start to lastchild
*/
*/
void ScintillaEditView::hideMarkedLines(size_t searchStart, bool toEndOfDoc)
{
size_t maxLines = execute(SCI_GETLINECOUNT);
if (doHide)
{
auto startHiding = searchStart;
bool isInSection = false;
for (auto i = searchStart; i < maxLines; ++i)
{
auto state = execute(SCI_MARKERGET, i);
@ -4189,13 +4231,14 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
if (isInSection)
{
execute(SCI_HIDELINES, startHiding, i-1);
if (!endOfDoc)
if (!toEndOfDoc)
{
return; //done, only single section requested
} //otherwise keep going
}
isInSection = false;
}
if ((state & (1 << MARK_HIDELINESBEGIN)) != 0)
{
isInSection = true;
@ -4203,9 +4246,12 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
}
}
}
else
{
}
void ScintillaEditView::showHiddenLines(size_t searchStart, bool toEndOfDoc, bool doDelete)
{
size_t maxLines = execute(SCI_GETLINECOUNT);
auto startShowing = searchStart;
bool isInSection = false;
for (auto i = searchStart; i < maxLines; ++i)
@ -4228,7 +4274,7 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
if (doDelete)
{
execute(SCI_MARKERDELETE, i, MARK_HIDELINESEND);
if (!endOfDoc)
if (!toEndOfDoc)
{
return; //done, only single section requested
} //otherwise keep going
@ -4238,7 +4284,7 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
{
if (startShowing >= i)
{ //because of fold skipping, we passed the close tag. In that case we cant do anything
if (!endOfDoc)
if (!toEndOfDoc)
{
return;
}
@ -4248,8 +4294,10 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
continue;
}
}
execute(SCI_SHOWLINES, startShowing, i-1);
if (!endOfDoc)
if (!toEndOfDoc)
{
return; //done, only single section requested
} //otherwise keep going
@ -4266,9 +4314,9 @@ void ScintillaEditView::runMarkers(bool doHide, size_t searchStart, bool endOfDo
}
}
}
}
}
void ScintillaEditView::restoreHiddenLines()
{
int line = 0;
@ -4285,7 +4333,6 @@ void ScintillaEditView::restoreHiddenLines()
if (line != -1)
{
execute(SCI_HIDELINES, startHiding, line - 1);
}
}
}

View File

@ -68,8 +68,6 @@ typedef void * SCINTILLA_PTR;
#define WM_FINDINPROJECTS (SCINTILLA_USER + 15)
#define WM_REPLACEINPROJECTS (SCINTILLA_USER + 16)
const int NB_FOLDER_STATE = 7;
// Codepage
const int CP_CHINESE_TRADITIONAL = 950;
const int CP_CHINESE_SIMPLIFIED = 936;
@ -89,9 +87,13 @@ const int CP_GREEK = 1253;
#define LIST_7 128
#define LIST_8 256
const bool fold_expand = true;
const bool fold_collapse = false;
const int NB_FOLDER_STATE = 7;
#define MAX_FOLD_COLLAPSE_LEVEL 8
#define MAX_FOLD_LINES_MORE_THAN 99
#define MODEVENTMASK_OFF 0
@ -454,8 +456,7 @@ public:
void activateBuffer(BufferID buffer, bool force);
void getCurrentFoldStates(std::vector<size_t> & lineStateVector);
void syncFoldStateWith(const std::vector<size_t> & lineStateVectorNew);
void getText(char *dest, size_t start, size_t end) const;
void getGenericText(wchar_t *dest, size_t destlen, size_t start, size_t end) const;
@ -740,8 +741,6 @@ public:
void updateLineNumberWidth();
void performGlobalStyles();
void expand(size_t& line, bool doExpand, bool force = false, intptr_t visLevels = 0, intptr_t level = -1);
std::pair<size_t, size_t> getSelectionLinesRange(intptr_t selectionNumber = -1) const;
void currentLinesUp() const;
void currentLinesDown() const;
@ -775,14 +774,18 @@ public:
::MessageBox(_hSelf, L"This function needs a newer OS version.", L"Change Case Error", MB_OK | MB_ICONHAND);
};
void getCurrentFoldStates(std::vector<size_t> & lineStateVector);
void syncFoldStateWith(const std::vector<size_t> & lineStateVectorNew);
bool isFoldIndentationBased() const;
void collapseFoldIndentationBased(int level2Collapse, bool mode);
void collapse(int level2Collapse, bool mode);
void foldIndentationBasedLevel(int level, bool mode);
void foldLevel(int level, bool mode);
void foldAll(bool mode);
void fold(size_t line, bool mode);
void fold(size_t line, bool mode, bool shouldBeNotified = true);
bool isFolded(size_t line) const {
return (execute(SCI_GETFOLDEXPANDED, line) != 0);
};
void expand(size_t& line, bool doExpand, bool force = false, intptr_t visLevels = 0, intptr_t level = -1);
bool isCurrentLineFolded() const;
void foldCurrentPos(bool mode);
int getCodepage() const {return _codepage;};
@ -810,34 +813,14 @@ public:
void styleChange();
void hideLines();
bool markerMarginClick(intptr_t lineNumber); //true if it did something
void notifyMarkers(Buffer * buf, bool isHide, size_t location, bool del);
void runMarkers(bool doHide, size_t searchStart, bool endOfDoc, bool doDelete);
bool hidelineMarkerClicked(intptr_t lineNumber); //true if it did something
void notifyHidelineMarkers(Buffer * buf, bool isHide, size_t location, bool del);
void hideMarkedLines(size_t searchStart, bool endOfDoc);
void showHiddenLines(size_t searchStart, bool endOfDoc, bool doDelete);
void restoreHiddenLines();
bool hasSelection() const { return !execute(SCI_GETSELECTIONEMPTY); };
bool isSelecting() const {
static Sci_CharacterRangeFull previousSelRange = getSelection();
Sci_CharacterRangeFull currentSelRange = getSelection();
if (currentSelRange.cpMin == currentSelRange.cpMax)
{
previousSelRange = currentSelRange;
return false;
}
if ((previousSelRange.cpMin == currentSelRange.cpMin) || (previousSelRange.cpMax == currentSelRange.cpMax))
{
previousSelRange = currentSelRange;
return true;
}
previousSelRange = currentSelRange;
return false;
};
bool isPythonStyleIndentation(LangType typeDoc) const{
return (typeDoc == L_PYTHON || typeDoc == L_COFFEESCRIPT || typeDoc == L_HASKELL ||\
typeDoc == L_C || typeDoc == L_CPP || typeDoc == L_OBJC || typeDoc == L_CS || typeDoc == L_JAVA ||\

View File

@ -293,7 +293,7 @@ void DocumentMap::doMove()
void DocumentMap::fold(size_t line, bool foldOrNot)
{
_pMapView->fold(line, foldOrNot);
_pMapView->fold(line, foldOrNot, false);
}
void DocumentMap::foldAll(bool mode)