Skip to content

Commit

Permalink
Some performance optimizations + fix page up and page down with line-…
Browse files Browse the repository at this point in the history
…wrap enabled.
  • Loading branch information
SpartanJ committed Sep 30, 2024
1 parent e789c15 commit 3b1ad77
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 65 deletions.
16 changes: 11 additions & 5 deletions include/eepp/ui/doc/textdocument.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,14 +226,20 @@ class EE_API TextDocument {

TextPosition previousChar( TextPosition position ) const;

TextPosition previousWordBoundary( TextPosition position,
bool ignoreFirstNonWord = true ) const;
TextPosition previousWordBoundary( TextPosition position, bool ignoreFirstNonWord = true,
std::size_t maxSeekChars = 1024,
bool returnInvalidOnMaxSeek = false ) const;

TextPosition nextWordBoundary( TextPosition position, bool ignoreFirstNonWord = true ) const;
TextPosition nextWordBoundary( TextPosition position, bool ignoreFirstNonWord = true,
std::size_t maxSeekChars = 1024,
bool returnInvalidOnMaxSeek = false ) const;

TextPosition previousSpaceBoundaryInLine( TextPosition position ) const;
TextPosition previousSpaceBoundaryInLine( TextPosition position,
std::size_t maxSeekChars = 1024,
bool returnInvalidOnMaxSeek = false ) const;

TextPosition nextSpaceBoundaryInLine( TextPosition position ) const;
TextPosition nextSpaceBoundaryInLine( TextPosition position, std::size_t maxSeekChars = 1024,
bool returnInvalidOnMaxSeek = false ) const;

TextPosition startOfWord( TextPosition position ) const;

Expand Down
28 changes: 18 additions & 10 deletions include/eepp/ui/uicodeeditor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,10 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {

void moveToNextLine();

void moveToPreviousPage();

void moveToNextPage();

void moveToStartOfLine();

void moveToEndOfLine();
Expand Down Expand Up @@ -503,10 +507,11 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {

virtual Int64 getColFromXOffset( VisibleIndex visibleIndex, const Float& x ) const;

std::vector<Rectf>
getTextRangeRectangles( const TextRange& range, const Vector2f& startScroll,
std::optional<const DocumentLineRange> lineRange = {},
std::optional<Float> lineHeight = {} );
std::vector<Rectf> getTextRangeRectangles(
const TextRange& range, const Vector2f& startScroll,
std::optional<const DocumentLineRange> lineRange = {}, std::optional<Float> lineHeight = {},
std::optional<DocumentViewLineRange> visibleLineRange =
{} /* if passed it will clip rectangles against the visual line range */ );

virtual Float getLineWidth( const Int64& docLine );

Expand Down Expand Up @@ -960,18 +965,19 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {

virtual void drawLineText( const Int64& line, Vector2f position, const Float& fontSize,
const Float& lineHeight,
const DocumentViewLineRange& visualLineRange );
const DocumentViewLineRange& visibleLineRange );

virtual void drawSelectionMatch( const DocumentLineRange& lineRange,
const Vector2f& startScroll, const Float& lineHeight );

virtual void drawWordMatch( const String& text, const DocumentLineRange& lineRange,
const Vector2f& startScroll, const Float& lineHeight,
bool ignoreSelectionMatch = false );
bool ignoreSelectionMatch = false,
const DocumentViewLineRange& visibleLineRange = {} );

virtual void drawWhitespaces( const DocumentLineRange& lineRange, const Vector2f& startScroll,
const Float& lineHeight,
const DocumentViewLineRange& visualLineRange );
const DocumentViewLineRange& visibleLineRange );

virtual void drawIndentationGuides( const DocumentLineRange& lineRange,
const Vector2f& startScroll, const Float& lineHeight );
Expand All @@ -981,14 +987,16 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {

virtual void drawTextRange( const TextRange& range, const DocumentLineRange& lineRange,
const Vector2f& startScroll, const Float& lineHeight,
const Color& backgroundColor );
const Color& backgroundColor,
const DocumentViewLineRange& visibleLineRange );

virtual void drawLineNumbers( const DocumentLineRange& lineRange, const Vector2f& startScroll,
const Vector2f& screenStart, const Float& lineHeight,
const Float& lineNumberWidth, const int& lineNumberDigits,
const Float& fontSize );

virtual void drawColorPreview( const Vector2f& startScroll, const Float& lineHeight );
virtual void drawColorPreview( const Vector2f& startScroll, const Float& lineHeight,
const DocumentViewLineRange& visibleLineRange );

virtual void onFontChanged();

Expand Down Expand Up @@ -1046,7 +1054,7 @@ class EE_API UICodeEditor : public UIWidget, public TextDocument::Client {

void drawWordRanges( const TextRanges& ranges, const DocumentLineRange& lineRange,
const Vector2f& startScroll, const Float& lineHeight,
bool ignoreSelectionMatch );
bool ignoreSelectionMatch, const DocumentViewLineRange& visibleLineRange );

void updateHighlightWordCache();

Expand Down
1 change: 1 addition & 0 deletions src/eepp/ui/doc/documentview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ DocumentView::VisibleLineRange DocumentView::getVisibleLineRange( const TextPosi
}
Int64 fromIdx = static_cast<Int64>( toVisibleIndex( pos.line() ) );
Int64 toIdx = static_cast<Int64>( toVisibleIndex( pos.line(), true ) );
// TODO: Implement binary search
DocumentView::VisibleLineRange info;
for ( Int64 i = fromIdx; i < toIdx; i++ ) {
Int64 fromCol = mVisibleLines[i].column();
Expand Down
45 changes: 29 additions & 16 deletions src/eepp/ui/doc/textdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1380,46 +1380,55 @@ TextPosition TextDocument::previousChar( TextPosition position ) const {
return positionOffset( position, TextPosition( 0, -1 ) );
}

TextPosition TextDocument::previousWordBoundary( TextPosition position,
bool ignoreFirstNonWord ) const {
TextPosition TextDocument::previousWordBoundary( TextPosition position, bool ignoreFirstNonWord,
std::size_t maxSeekChars,
bool returnInvalidOnMaxSeek ) const {
auto ch = getChar( positionOffset( position, -1 ) );
bool inWord = !isNonWord( ch );
if ( !ignoreFirstNonWord && !inWord )
return position;
String::StringBaseType nextChar = 0;
Int64 seekedChars = static_cast<Int64>( maxSeekChars );
do {
TextPosition curPos = position;
position = positionOffset( position, -1 );
if ( curPos == position ) {
break;
}
nextChar = getChar( positionOffset( position, -1 ) );
} while ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) );
return position;
} while ( ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) ) &&
--seekedChars );
return returnInvalidOnMaxSeek && seekedChars == 0 ? TextPosition() : position;
}

TextPosition TextDocument::nextWordBoundary( TextPosition position,
bool ignoreFirstNonWord ) const {
TextPosition TextDocument::nextWordBoundary( TextPosition position, bool ignoreFirstNonWord,
std::size_t maxSeekChars,
bool returnInvalidOnMaxSeek ) const {
auto ch = getChar( position );
bool inWord = !isNonWord( ch );
if ( !ignoreFirstNonWord && !inWord )
return position;
String::StringBaseType nextChar = 0;
Int64 seekedChars = static_cast<Int64>( maxSeekChars );
do {
TextPosition curPos = position;
position = positionOffset( position, 1 );
if ( curPos == position ) {
break;
}
nextChar = getChar( position );
} while ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) );
return position;
} while ( ( ( inWord && !isNonWord( nextChar ) ) || ( !inWord && nextChar == ch ) ) &&
--seekedChars );
return returnInvalidOnMaxSeek && seekedChars == 0 ? TextPosition() : position;
}

TextPosition TextDocument::previousSpaceBoundaryInLine( TextPosition position ) const {
TextPosition TextDocument::previousSpaceBoundaryInLine( TextPosition position,
std::size_t maxSeekChars,
bool returnInvalidOnMaxSeek ) const {
auto ch = getChar( positionOffset( position, -1 ) );
bool inWord = ch != ' ';
String::StringBaseType nextChar = 0;
Int64 seekedChars = static_cast<Int64>( maxSeekChars );
do {
TextPosition curPos = position;
position = positionOffset( position, -1 );
Expand All @@ -1430,14 +1439,16 @@ TextPosition TextDocument::previousSpaceBoundaryInLine( TextPosition position )
break;
}
nextChar = getChar( positionOffset( position, -1 ) );
} while ( ( inWord && nextChar != ' ' ) || ( !inWord && nextChar == ch ) );
return position;
} while ( ( ( inWord && nextChar != ' ' ) || ( !inWord && nextChar == ch ) ) && --seekedChars );
return returnInvalidOnMaxSeek && seekedChars == 0 ? TextPosition() : position;
}

TextPosition TextDocument::nextSpaceBoundaryInLine( TextPosition position ) const {
TextPosition TextDocument::nextSpaceBoundaryInLine( TextPosition position, std::size_t maxSeekChars,
bool returnInvalidOnMaxSeek ) const {
auto ch = getChar( position );
bool inWord = ch != ' ';
String::StringBaseType nextChar = 0;
Int64 seekedChars = static_cast<Int64>( maxSeekChars );
do {
TextPosition curPos = position;
position = positionOffset( position, 1 );
Expand All @@ -1448,8 +1459,8 @@ TextPosition TextDocument::nextSpaceBoundaryInLine( TextPosition position ) cons
break;
}
nextChar = getChar( position );
} while ( ( inWord && nextChar != ' ' ) || ( !inWord && nextChar == ch ) );
return position;
} while ( ( ( inWord && nextChar != ' ' ) || ( !inWord && nextChar == ch ) ) && --seekedChars );
return returnInvalidOnMaxSeek && seekedChars == 0 ? TextPosition() : position;
}

TextPosition TextDocument::startOfWord( TextPosition position ) const {
Expand Down Expand Up @@ -1987,8 +1998,10 @@ const Uint64& TextDocument::getModificationId() const {

void TextDocument::selectWord( bool withMulticursor ) {
if ( !hasSelection() ) {
setSelection( { nextWordBoundary( getSelection().start(), false ),
previousWordBoundary( getSelection().start(), false ) } );
setSelection( { nextWordBoundary( getSelection().start(), false,
std::numeric_limits<std::size_t>::max() ),
previousWordBoundary( getSelection().start(), false,
std::numeric_limits<std::size_t>::max() ) } );
} else if ( withMulticursor ) {
String text( getSelectedText() );
auto res( find( text, getBottomMostCursor().normalized().end() ) );
Expand Down
Loading

0 comments on commit 3b1ad77

Please sign in to comment.