From a106b0d109dbe400f602b7395cf69e35966b4788 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Thu, 6 Jun 2024 00:58:56 -0300 Subject: [PATCH] Some optimizations. --- include/eepp/ui/doc/foldrangeservice.hpp | 2 ++ include/eepp/ui/doc/textdocument.hpp | 4 ++++ src/eepp/ui/doc/documentview.cpp | 8 +++++--- src/eepp/ui/doc/foldrangeservice.cpp | 20 +++++++++++-------- src/eepp/ui/doc/textdocument.cpp | 16 +++++++++++++-- .../autocomplete/autocompleteplugin.cpp | 4 ++-- src/tools/ecode/projectsearch.cpp | 2 +- 7 files changed, 40 insertions(+), 16 deletions(-) diff --git a/include/eepp/ui/doc/foldrangeservice.hpp b/include/eepp/ui/doc/foldrangeservice.hpp index 8253591b9..f8d7962fb 100644 --- a/include/eepp/ui/doc/foldrangeservice.hpp +++ b/include/eepp/ui/doc/foldrangeservice.hpp @@ -42,6 +42,8 @@ class EE_API FoldRangeServive { const FoldRangeProvider& getProvider() const; + bool hasProvider() const; + void setProvider( const FoldRangeProvider& provider ); bool isEnabled() const; diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index 987eb4156..1d99b2c1b 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -627,6 +627,10 @@ class EE_API TextDocument { FoldRangeServive& getFoldRangeService(); + std::vector getLines() const; + + void setLines( std::vector&& lines ); + protected: friend class TextUndoStack; friend class FoldRangeServive; diff --git a/src/eepp/ui/doc/documentview.cpp b/src/eepp/ui/doc/documentview.cpp index 3ae824da2..c992259a7 100644 --- a/src/eepp/ui/doc/documentview.cpp +++ b/src/eepp/ui/doc/documentview.cpp @@ -164,7 +164,8 @@ bool DocumentView::isWrapEnabled() const { void DocumentView::setMaxWidth( Float maxWidth, bool forceReconstructBreaks ) { if ( maxWidth != mMaxWidth ) { mMaxWidth = maxWidth; - invalidateCache(); + if ( !isOneToOne() ) + invalidateCache(); } else if ( forceReconstructBreaks || mPendingReconstruction ) { invalidateCache(); } @@ -195,7 +196,8 @@ void DocumentView::setLineWrapMode( LineWrapMode mode ) { } TextPosition DocumentView::getVisibleIndexPosition( VisibleIndex visibleIndex ) const { - if ( isOneToOne() ) + eeASSERT( mConfig.mode == LineWrapMode::NoWrap || !mVisibleLines.empty() ); + if ( isOneToOne() || mVisibleLines.empty() ) return { static_cast( visibleIndex ), 0 }; return mVisibleLines[eeclamp( static_cast( visibleIndex ), 0ll, eemax( static_cast( mVisibleLines.size() ) - 1, 0ll ) )]; @@ -509,7 +511,7 @@ void DocumentView::foldRegion( Int64 foldDocIdx ) { auto foldRegion = mDoc->getFoldRangeService().find( foldDocIdx ); if ( !foldRegion ) return; - if ( isOneToOne() ) + if ( isOneToOne() && mDocLineToVisibleIndex.empty() ) invalidateCache(); Int64 toDocIdx = foldRegion->end().line(); changeVisibility( foldDocIdx + 1, toDocIdx, false ); diff --git a/src/eepp/ui/doc/foldrangeservice.cpp b/src/eepp/ui/doc/foldrangeservice.cpp index b60348f9f..a49069a84 100644 --- a/src/eepp/ui/doc/foldrangeservice.cpp +++ b/src/eepp/ui/doc/foldrangeservice.cpp @@ -9,14 +9,13 @@ namespace EE { namespace UI { namespace Doc { static std::vector findFoldingRangesBraces( TextDocument* doc ) { Clock c; - std::stack braceStack; std::vector regions; - const auto& braces = doc->getSyntaxDefinition().getFoldBraces(); - size_t linesCount = doc->linesCount(); - if ( linesCount <= 2 ) + if ( doc->linesCount() <= 2 ) return regions; + const auto& braces = doc->getSyntaxDefinition().getFoldBraces(); + std::stack braceStack; auto highlighter = doc->getHighlighter(); - for ( size_t lineIdx = 0; lineIdx < linesCount; lineIdx++ ) { + for ( size_t lineIdx = 0; lineIdx < doc->linesCount(); lineIdx++ ) { const auto& line = doc->line( lineIdx ).getText(); size_t lineLength = line.length(); for ( size_t colIdx = 0; colIdx < lineLength; colIdx++ ) { @@ -64,11 +63,12 @@ static std::vector findFoldingRangesIndentation( TextDocument* doc ) Clock c; std::stack indentStack; std::vector regions; + if ( doc->linesCount() <= 2 ) + return regions; const auto& braces = doc->getSyntaxDefinition().getFoldBraces(); - size_t linesCount = doc->linesCount(); int currentIndent = 0; - for ( size_t lineIdx = 0; lineIdx < linesCount; lineIdx++ ) { + for ( size_t lineIdx = 0; lineIdx < doc->linesCount(); lineIdx++ ) { const auto& line = doc->line( lineIdx ).getText(); int newIndent = countLeadingSpaces( line ); if ( newIndent > currentIndent ) { @@ -91,7 +91,7 @@ static std::vector findFoldingRangesIndentation( TextDocument* doc ) auto top = indentStack.top(); indentStack.pop(); regions.emplace_back( TextPosition( top.line() + 1, 0 ), - TextPosition( static_cast( linesCount ) - 1, 0 ) ); + TextPosition( static_cast( doc->linesCount() ) - 1, 0 ) ); } Log::debug( "findFoldingRangesIndentation for \"%s\" took %s", doc->getFilePath(), @@ -194,6 +194,10 @@ const FoldRangeServive::FoldRangeProvider& FoldRangeServive::getProvider() const return mProvider; } +bool FoldRangeServive::hasProvider() const { + return mProvider != nullptr; +} + void FoldRangeServive::setProvider( const FoldRangeProvider& provider ) { mProvider = provider; if ( provider == nullptr ) { diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index 482a809d6..2fa33e68f 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -996,11 +996,15 @@ const TextRange& TextDocument::getSelection() const { } TextDocumentLine& TextDocument::line( const size_t& index ) { - return mLines[index]; + static TextDocumentLine safeLine = TextDocumentLine( "" ); + eeASSERT( index < mLines.size() ); + return index >= mLines.size() ? safeLine : mLines[index]; } const TextDocumentLine& TextDocument::line( const size_t& index ) const { - return mLines[index]; + static TextDocumentLine safeLine = TextDocumentLine( "" ); + eeASSERT( index < mLines.size() ); + return index >= mLines.size() ? safeLine : mLines[index]; } size_t TextDocument::linesCount() const { @@ -2988,6 +2992,14 @@ FoldRangeServive& TextDocument::getFoldRangeService() { return mFoldRangeService; } +std::vector TextDocument::getLines() const { + return mLines; +} + +void TextDocument::setLines( std::vector&& lines ) { + mLines = std::move( lines ); +} + static inline void changeDepth( SyntaxHighlighter* highlighter, int& depth, const TextPosition& pos, int dir ) { if ( highlighter ) { diff --git a/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp b/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp index d740ffd7c..1e4ca761f 100644 --- a/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp +++ b/src/tools/ecode/plugins/autocomplete/autocompleteplugin.cpp @@ -1226,8 +1226,8 @@ AutoCompletePlugin::SymbolsList AutoCompletePlugin::getDocumentSymbols( TextDocu return symbols; std::string current( getPartialSymbol( doc ) ); TextPosition end = doc->getSelection().end(); - for ( Int64 i = 0; i < lc; i++ ) { - const auto& string = doc->line( i ).getText().toUtf8(); + for ( Int64 i = 0; i < static_cast( doc->linesCount() ); i++ ) { + const auto& string = doc->line( i ).toUtf8(); for ( auto& match : pattern.gmatch( string ) ) { std::string matchStr( match[0] ); // Ignore the symbol if is actually the current symbol being written diff --git a/src/tools/ecode/projectsearch.cpp b/src/tools/ecode/projectsearch.cpp index 1e39263cc..52862e594 100644 --- a/src/tools/ecode/projectsearch.cpp +++ b/src/tools/ecode/projectsearch.cpp @@ -284,7 +284,7 @@ void ProjectSearch::find( const std::vector files, std::string stri f.position = r.result; const auto& line = doc->line( r.result.start().line() ); if ( line.size() > EE_1KB ) - f.line = line.getText().substr( 0, EE_1KB ); + f.line = line.substr( 0, EE_1KB ); else f.line = line.getTextWithoutNewLine(); f.start = r.result.start().column();