Enhance large files with syntax highlighting performance

Fix #15952, close #15981
This commit is contained in:
Don Ho 2024-12-24 04:35:49 +01:00
parent 8a8f8b2a89
commit de9ffd2ea8
10 changed files with 111 additions and 13 deletions

View File

@ -1331,6 +1331,7 @@ bool Notepad_plus::replaceInOpenedFiles()
}
_invisibleEditView.execute(SCI_SETDOCPOINTER, 0, oldDoc);
_invisibleEditView.setCurrentBuffer(oldBuf);
_pEditView = pOldView;
@ -3875,7 +3876,9 @@ void Notepad_plus::setLanguage(LangType langType)
reset = true;
_subEditView.saveCurrentPos();
prev = _subEditView.execute(SCI_GETDOCPOINTER);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, 0);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
}
}
@ -3890,7 +3893,9 @@ void Notepad_plus::setLanguage(LangType langType)
if (reset)
{
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, prev);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
_subEditView.restoreCurrentPosPreStep();
}
}
@ -4662,6 +4667,10 @@ void Notepad_plus::loadBufferIntoView(BufferID id, int whichOne, bool dontClose)
{
idToClose = BUFFER_INVALID;
}
else
{
buf->setLastLangType(-1); // When replacing the "new" tab with an opened file, the last used language should be reset to its initial value so that the language can be reloaded later in the activateBuffer() function.
}
}
MainFileManager.addBufferReference(id, viewToOpen);

View File

@ -3713,10 +3713,13 @@ void Notepad_plus::command(int id)
case IDM_LANG_TOML:
case IDM_LANG_USER :
{
setLanguage(menuID2LangType(id));
LangType lang = menuID2LangType(id);
setLanguage(lang);
// Manually set language, don't change language even file extension changes.
Buffer *buffer = _pEditView->getCurrentBuffer();
buffer->langHasBeenSetFromMenu();
buffer->setLastLangType(static_cast<int>(lang));
if (_pDocMap)
{

View File

@ -574,13 +574,17 @@ bool Notepad_plus::doReload(BufferID id, bool alert)
if (mainVisisble)
{
_mainEditView.saveCurrentPos();
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_mainEditView.execute(SCI_SETDOCPOINTER, 0, 0);
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
}
if (subVisisble)
{
_subEditView.saveCurrentPos();
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, 0);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
}
if (!mainVisisble && !subVisisble)
@ -592,13 +596,17 @@ bool Notepad_plus::doReload(BufferID id, bool alert)
Buffer * pBuf = MainFileManager.getBufferByID(id);
if (mainVisisble)
{
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_mainEditView.execute(SCI_SETDOCPOINTER, 0, pBuf->getDocument());
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
_mainEditView.restoreCurrentPosPreStep();
}
if (subVisisble)
{
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, pBuf->getDocument());
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
_subEditView.restoreCurrentPosPreStep();
}
@ -2473,12 +2481,16 @@ bool Notepad_plus::loadSession(Session & session, bool isSnapshotMode, const wch
//Force in the document so we can add the markers
//Don't use default methods because of performance
Document prevDoc = _mainEditView.execute(SCI_GETDOCPOINTER);
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_mainEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
for (size_t j = 0, len = session._mainViewFiles[i]._marks.size(); j < len ; ++j)
{
_mainEditView.execute(SCI_MARKERADD, session._mainViewFiles[i]._marks[j], MARK_BOOKMARK);
}
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_mainEditView.execute(SCI_SETDOCPOINTER, 0, prevDoc);
_mainEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
++i;
}
else
@ -2604,12 +2616,16 @@ bool Notepad_plus::loadSession(Session & session, bool isSnapshotMode, const wch
//Force in the document so we can add the markers
//Don't use default methods because of performance
Document prevDoc = _subEditView.execute(SCI_GETDOCPOINTER);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, buf->getDocument());
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
for (size_t j = 0, len = session._subViewFiles[k]._marks.size(); j < len ; ++j)
{
_subEditView.execute(SCI_MARKERADD, session._subViewFiles[k]._marks[j], MARK_BOOKMARK);
}
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_subEditView.execute(SCI_SETDOCPOINTER, 0, prevDoc);
_subEditView.execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
++k;
}

View File

@ -443,10 +443,7 @@ BOOL Notepad_plus::notify(SCNotification *notification)
LPNMMOUSE lpnm = (LPNMMOUSE)notification;
if (lpnm->dwItemSpec == DWORD(STATUSBAR_CUR_POS))
{
bool isFirstTime = !_goToLineDlg.isCreated();
_goToLineDlg.doDialog(_nativeLangSpeaker.isRTL());
if (isFirstTime)
_nativeLangSpeaker.changeDlgLang(_goToLineDlg.getHSelf(), "GoToLine");
command(IDM_SEARCH_GOTOLINE);
}
else if (lpnm->dwItemSpec == DWORD(STATUSBAR_DOC_SIZE))
{

View File

@ -1391,7 +1391,9 @@ SavingStatus FileManager::saveBuffer(BufferID id, const wchar_t* filename, bool
if (isCopy) // "Save a Copy As..." command
{
_pscratchTilla->execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
_pscratchTilla->execute(SCI_SETDOCPOINTER, 0, _scratchDocDefault);
_pscratchTilla->execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
return SavingStatus::SaveOK; //all done - we don't change the current buffer's path to "fullpath", since it's "Save a Copy As..." action.
}

View File

@ -216,6 +216,10 @@ public:
void setLangType(LangType lang, const wchar_t * userLangName = L"");
int getLastLangType() const { return _lastLangType; }
void setLastLangType(int val) { _lastLangType = val; }
UniMode getUnicodeMode() const { return _unicodeMode; }
void setUnicodeMode(UniMode mode);
@ -388,6 +392,7 @@ private:
//document properties
Document _doc; //invariable
LangType _lang = L_TEXT;
int _lastLangType = -1;
std::wstring _userLangExt; // it's useful if only (_lang == L_USER)
bool _isDirty = false;
EolType _eolFormat = EolType::osdefault;

View File

@ -87,6 +87,7 @@ intptr_t CALLBACK GoToLineDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM l
case IDOK :
{
(*_ppEditView)->execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
long long line = getLine();
if (line != -1)
{
@ -111,6 +112,7 @@ intptr_t CALLBACK GoToLineDlg::run_dlgProc(UINT message, WPARAM wParam, LPARAM l
(*_ppEditView)->execute(SCI_GOTOPOS, posToGoto);
}
}
(*_ppEditView)->execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
SCNotification notification{};
notification.nmhdr.code = SCN_PAINTED;

View File

@ -230,6 +230,8 @@ void ScintillaEditView::init(HINSTANCE hInst, HWND hPere)
throw std::runtime_error("ScintillaEditView::init : SCI_GETDIRECTPOINTER message failed");
}
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
execute(SCI_SETIDLESTYLING, SC_IDLESTYLING_ALL, 0);
execute(SCI_SETMARGINMASKN, _SC_MARGE_FOLDER, SC_MASK_FOLDERS);
showMargin(_SC_MARGE_FOLDER, true);
@ -345,6 +347,7 @@ void ScintillaEditView::init(HINSTANCE hInst, HWND hPere)
delete[] defaultCharList;
}
}
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
//Get the startup document and make a buffer for it so it can be accessed like a file
attachDefaultDoc();
}
@ -2109,6 +2112,19 @@ void ScintillaEditView::defineDocType(LangType typeDoc)
if (currentIndentMode != docIndentMode)
execute(SCI_SETINDENTATIONGUIDES, docIndentMode);
}
execute(SCI_SETLAYOUTCACHE, SC_CACHE_DOCUMENT, 0);
execute(SCI_STARTSTYLING, 0, 0);
}
Document ScintillaEditView::getBlankDocument()
{
if(_blankDocument==0)
{
_blankDocument=static_cast<Document>(execute(SCI_CREATEDOCUMENT,0,SC_DOCUMENTOPTION_TEXT_LARGE));
execute(SCI_ADDREFDOCUMENT,0,_blankDocument);
}
return _blankDocument;
}
BufferID ScintillaEditView::attachDefaultDoc()
@ -2278,16 +2294,51 @@ void ScintillaEditView::activateBuffer(BufferID buffer, bool force)
// put the state into the future ex buffer
_currentBuffer->setHeaderLineState(lineStateVector, this);
_prevBuffer = _currentBuffer;
_currentBufferID = buffer; //the magical switch happens here
_currentBuffer = newBuf;
const bool isSameLangType = _prevBuffer != nullptr && ((_prevBuffer == _currentBuffer) || (_prevBuffer->getLangType() == _currentBuffer->getLangType()));
const int currentLangInt = static_cast<int>(_currentBuffer->getLangType());
const bool isFirstActiveBuffer = (_currentBuffer->getLastLangType() != currentLangInt);
if (isFirstActiveBuffer) // Entering the tab for the 1st time
{
// change the doc, this operation will decrease
// the ref count of old current doc and increase the one of the new doc. FileManager should manage the rest
// Note that the actual reference in the Buffer itself is NOT decreased, Notepad_plus does that if neccessary
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
execute(SCI_SETDOCPOINTER, 0, _currentBuffer->getDocument());
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
// Due to execute(SCI_CLEARDOCUMENTSTYLE); in defineDocType() function
// defineDocType() function should be called here, but not be after the fold info loop
defineDocType(_currentBuffer->getLangType());
}
else if (isSameLangType) // After the 2nd entering with the same language type
{
// No need to call defineDocType() since it's the same language type
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
execute(SCI_SETDOCPOINTER, 0, _currentBuffer->getDocument());
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
}
else // Entering the tab for the 2nd or more times, with the different language type
{
// In order to improve the performance of switch-in on the 2nd or more times for the large files,
// a blank document is used for accelerate defineDocType() call.
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
execute(SCI_SETDOCPOINTER, 0, getBlankDocument());
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
defineDocType(_currentBuffer->getLangType());
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_OFF);
execute(SCI_SETDOCPOINTER, 0, _currentBuffer->getDocument());
execute(SCI_SETMODEVENTMASK, MODEVENTMASK_ON);
}
_currentBuffer->setLastLangType(currentLangInt);
setWordChars();

View File

@ -93,6 +93,9 @@ const bool fold_uncollapse = true;
const bool fold_collapse = false;
#define MAX_FOLD_COLLAPSE_LEVEL 8
#define MODEVENTMASK_OFF 0
#define MODEVENTMASK_ON SC_MOD_DELETETEXT | SC_MOD_INSERTTEXT | SC_PERFORMED_UNDO | SC_PERFORMED_REDO | SC_MOD_CHANGEINDICATOR
enum TextCase : UCHAR
{
UPPERCASE,
@ -428,6 +431,11 @@ public:
virtual void destroy()
{
if (_blankDocument != 0)
{
execute(SCI_RELEASEDOCUMENT, 0, _blankDocument);
_blankDocument = 0;
}
::DestroyWindow(_hSelf);
_hSelf = NULL;
_pScintillaFunc = NULL;
@ -871,6 +879,8 @@ public:
bool pasteToMultiSelection() const;
void setElementColour(int element, COLORREF color) const { execute(SCI_SETELEMENTCOLOUR, element, color | 0xFF000000); };
Document getBlankDocument();
protected:
static bool _SciInit;
@ -894,6 +904,9 @@ protected:
BufferID _currentBufferID = nullptr;
Buffer * _currentBuffer = nullptr;
Buffer* _prevBuffer = nullptr;
Document _blankDocument = 0;
int _codepage = CP_ACP;
bool _wrapRestoreNeeded = false;
bool _positionRestoreNeeded = false;

View File

@ -582,7 +582,7 @@
#define IDM_FORUM (IDM_ABOUT + 4)
//#define IDM_PLUGINSHOME (IDM_ABOUT + 5)
#define IDM_UPDATE_NPP (IDM_ABOUT + 6)
#define IDM_WIKIFAQ (IDM_ABOUT + 7)
//#define IDM_WIKIFAQ (IDM_ABOUT + 7)
//#define IDM_HELP (IDM_ABOUT + 8)
#define IDM_CONFUPDATERPROXY (IDM_ABOUT + 9)
#define IDM_CMDLINEARGUMENTS (IDM_ABOUT + 10)