Skip to content

Commit

Permalink
optimizations for large file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stefankueng committed Oct 20, 2024
1 parent e9105f1 commit 205b8d1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions src/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3425,6 +3425,8 @@ void CMainWindow::AddHotSpots() const
auto lineCount = m_editor.Scintilla().LineCount();
auto endPos = m_editor.Scintilla().PositionFromLine(m_editor.Scintilla().DocLineFromVisible(firstVisibleLine + min(linesOnScreen, lineCount)));

if (startPos < 0 || endPos < 0)
return;
// to speed up the search, first search for "://" without using the regex engine
auto fStartPos = startPos;
auto fEndPos = endPos;
Expand Down
31 changes: 26 additions & 5 deletions src/ScintillaWnd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ CScintillaWnd::CScintillaWnd(HINSTANCE hInst)
, m_bCursorShown(true)
, m_bScratch(false)
, m_eraseBkgnd(true)
, m_hugeLevelReached(false)
, m_cursorTimeout(-1)
, m_bInFolderMargin(false)
, m_hasConsolas(false)
Expand Down Expand Up @@ -1023,7 +1024,7 @@ void CScintillaWnd::RestoreCurrentPos(const CPosData& pos)
UpdateLineNumberWidth();
}

void CScintillaWnd::SetupLexerForLang(const std::string& lang) const
void CScintillaWnd::SetupLexerForLang(const std::string& lang)
{
const auto& lexerData = CLexStyles::Instance().GetLexerDataForLang(lang);
const auto& keywords = CLexStyles::Instance().GetKeywordsForLang(lang);
Expand Down Expand Up @@ -1103,6 +1104,14 @@ void CScintillaWnd::SetupLexerForLang(const std::string& lang) const
{
m_scintilla.SetProperty(propName.c_str(), propValue.c_str());
}
auto length = m_scintilla.Length();
m_hugeLevelReached = false;
if (length > 500 * 1024 * 1024)
{
// turn off folding
m_scintilla.SetProperty("fold", "0");
m_hugeLevelReached = true;
}
for (const auto& [styleId, styleData] : lexerData.styles)
{
m_scintilla.StyleSetFore(styleId, theme.GetThemeColor(styleData.foregroundColor, true));
Expand Down Expand Up @@ -1476,16 +1485,17 @@ void CScintillaWnd::MarkSelectedWord(bool clear, bool edit)
m_scintilla.SetIndicatorCurrent(INDIC_SELECTION_MARK);
m_scintilla.IndicatorClearRange(startStylePos, len);

auto sSelText = m_scintilla.GetSelText();
auto selTextLen = sSelText.size();
if ((selTextLen == 0) || (clear))
auto selSpan = m_scintilla.SelectionSpan();
if (clear || selSpan.Length() == 0 || selSpan.Length() + 10000)
{
lastSelText.clear();
m_docScroll.Clear(DOCSCROLLTYPE_SELTEXT);
m_selTextMarkerCount = 0;
SendMessage(*this, WM_NCPAINT, static_cast<WPARAM>(1), 0);
return;
}
auto sSelText = m_scintilla.GetSelText();
auto selTextLen = sSelText.size();
auto origSelStart = m_scintilla.SelectionStart();
auto origSelEnd = m_scintilla.SelectionEnd();
auto selStartLine = m_scintilla.LineFromPosition(origSelStart);
Expand Down Expand Up @@ -1692,7 +1702,8 @@ void CScintillaWnd::MatchBraces(BraceMatch what)
SetTimer(*this, TIM_BRACEHIGHLIGHTTEXT, 1000, nullptr);
}

if ((what == BraceMatch::Highlight) && (m_scintilla.IndentationGuides() != Scintilla::IndentView::None))
if ((braceAtCaret >= 0) && (braceOpposite >= 0) && (what == BraceMatch::Highlight) &&
(m_scintilla.IndentationGuides() != Scintilla::IndentView::None))
{
auto columnAtCaret = m_scintilla.Column(braceAtCaret);
auto columnOpposite = m_scintilla.Column(braceOpposite);
Expand Down Expand Up @@ -2548,6 +2559,16 @@ void CScintillaWnd::ReflectEvents(SCNotification* pScn)
case SCN_SAVEPOINTREACHED:
EnableChangeHistory();
break;
case SCN_MODIFIED:
if (pScn->modificationType & SC_MOD_INSERTTEXT)
{
if (!m_hugeLevelReached && m_scintilla.Length() > 500 * 1024 * 1024)
{
// turn off folding
m_scintilla.SetProperty("fold", "0");
m_hugeLevelReached = true;
}
}
default:
break;
}
Expand Down
5 changes: 3 additions & 2 deletions src/ScintillaWnd.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// This file is part of BowPad.
//
// Copyright (C) 2013-2022 - Stefan Kueng
// Copyright (C) 2013-2022, 2024 - Stefan Kueng
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -99,7 +99,7 @@ class CScintillaWnd : public CWindow
void UpdateLineNumberWidth() const;
void SaveCurrentPos(CPosData& pos);
void RestoreCurrentPos(const CPosData& pos);
void SetupLexerForLang(const std::string& lang) const;
void SetupLexerForLang(const std::string& lang);
void MarginClick(SCNotification* pNotification);
void SelectionUpdated() const;
void MarkSelectedWord(bool clear, bool edit);
Expand Down Expand Up @@ -162,6 +162,7 @@ class CScintillaWnd : public CWindow
bool m_bCursorShown;
bool m_bScratch;
bool m_eraseBkgnd;
bool m_hugeLevelReached;
int m_cursorTimeout;
bool m_bInFolderMargin;
bool m_hasConsolas;
Expand Down

0 comments on commit 205b8d1

Please sign in to comment.