Skip to content

Commit

Permalink
Fix syntax highlight error in sub-syntaxes due to an incorrect line c…
Browse files Browse the repository at this point in the history
…ache movement on added lines.

Add a new event to track the number of visible lines changes in UICodeEditor.
  • Loading branch information
SpartanJ committed Mar 3, 2025
1 parent 276ed6b commit e15bb23
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/eepp/scene/event.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class EE_API Event {
OnScrollChange,
OnModelChanged,
OnWindowToFront,
OnVisibleLinesCountChange,
NoEvent = eeINDEX_NOT_FOUND
};

Expand Down
5 changes: 5 additions & 0 deletions include/eepp/ui/doc/documentview.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ class EE_API DocumentView {

void onFoldRegionsUpdated();

void setOnVisibleLineCountChange( std::function<void()> onVisibleLinesCountChangeCb );

protected:
std::shared_ptr<TextDocument> mDoc;
FontStyleConfig mFontStyle;
Expand All @@ -168,6 +170,7 @@ class EE_API DocumentView {
bool mPendingReconstruction{ false };
bool mUnderConstruction{ false };
bool mUpdatingFoldRegions{ false };
std::function<void()> mOnVisibleLineCountChange;

void changeVisibility( Int64 fromDocIdx, Int64 toDocIdx, bool visible,
bool recomputeOffset = true, bool recomputeLineToVisibleIndex = true );
Expand All @@ -184,6 +187,8 @@ class EE_API DocumentView {
bool recomputeLineToVisibleIndex = true );

void moveCursorToVisibleArea();

void onVisibleLinesCountChange();
};

}}} // namespace EE::UI::Doc
Expand Down
22 changes: 22 additions & 0 deletions src/eepp/ui/doc/documentview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,8 @@ void DocumentView::invalidateCache() {

mPendingReconstruction = false;

onVisibleLinesCountChange();

Log::debug( "DocumentView for \"%s\" generated in %s", mDoc->getFilePath(),
clock.getElapsedTime().toString() );
}
Expand Down Expand Up @@ -430,9 +432,12 @@ void DocumentView::setPendingReconstruction( bool pendingReconstruction ) {
}

void DocumentView::clearCache() {
auto visibleLines = mVisibleLines.size();
mVisibleLines.clear();
mDocLineToVisibleIndex.clear();
mVisibleLinesOffset.clear();
if ( mDoc && visibleLines != mDoc->linesCount() )
onVisibleLinesCountChange();
}

void DocumentView::clear() {
Expand Down Expand Up @@ -487,6 +492,8 @@ void DocumentView::updateCache( Int64 fromLine, Int64 toLine, Int64 numLines ) {
Int64 oldIdxFrom = static_cast<Int64>( toVisibleIndex( fromLine, false ) );
Int64 oldIdxTo = static_cast<Int64>( toVisibleIndex( toLine, true ) );

auto visibleLinesCount = mVisibleLines.size();

// Remove old visible lines
mVisibleLines.erase( mVisibleLines.begin() + oldIdxFrom, mVisibleLines.begin() + oldIdxTo + 1 );

Expand Down Expand Up @@ -535,6 +542,9 @@ void DocumentView::updateCache( Int64 fromLine, Int64 toLine, Int64 numLines ) {
recomputeDocLineToVisibleIndex( oldIdxFrom );

verifyStructuralConsistency();

if ( visibleLinesCount != mVisibleLines.size() )
onVisibleLinesCountChange();
}

void DocumentView::recomputeDocLineToVisibleIndex( Int64 fromVisibleIndex, bool ensureDocSize ) {
Expand Down Expand Up @@ -696,6 +706,8 @@ void DocumentView::changeVisibility( Int64 fromDocIdx, Int64 toDocIdx, bool visi

if ( recomputeLineToVisibleIndex )
eeASSERT( mDocLineToVisibleIndex.size() == mDoc->linesCount() );

onVisibleLinesCountChange();
}

bool DocumentView::isFolded( Int64 docIdx, bool andNotFirstLine ) const {
Expand Down Expand Up @@ -792,4 +804,14 @@ void DocumentView::verifyStructuralConsistency() {
#endif
}

void DocumentView::onVisibleLinesCountChange() {
if ( mOnVisibleLineCountChange )
mOnVisibleLineCountChange();
}

void DocumentView::setOnVisibleLineCountChange(
std::function<void()> onVisibleLinesCountChangeCb ) {
mOnVisibleLineCountChange = std::move( onVisibleLinesCountChangeCb );
}

}}} // namespace EE::UI::Doc
3 changes: 2 additions & 1 deletion src/eepp/ui/doc/syntaxhighlighter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ void SyntaxHighlighter::moveHighlight( const Int64& fromLine, const Int64& /*toL
return;
Int64 linesCount = mDoc->linesCount();
if ( numLines > 0 ) {
for ( Int64 i = linesCount - 1; i >= fromLine; --i ) {
Int64 toLine = fromLine + numLines;
for ( Int64 i = linesCount - 1; i >= toLine; --i ) {
auto lineIt = mLines.find( i - numLines );
if ( lineIt != mLines.end() ) {
const auto& line = lineIt->second;
Expand Down
9 changes: 7 additions & 2 deletions src/eepp/ui/uicodeeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ UICodeEditor::UICodeEditor( const std::string& elementTag, const bool& autoRegis
setTextSelection( true );
setColorScheme( SyntaxColorScheme::getDefault() );
refreshTag();
mDocView.setOnVisibleLineCountChange(
[this] { sendCommonEvent( Event::OnVisibleLinesCountChange ); } );
mVScrollBar = UIScrollBar::NewVertical();
mVScrollBar->setParent( this );
mVScrollBar->addEventListener( Event::OnSizeChange,
Expand Down Expand Up @@ -2126,8 +2128,6 @@ void UICodeEditor::updateEditor() {

void UICodeEditor::onDocumentTextChanged( const DocumentContentChange& change ) {
invalidateDraw();
checkMatchingBrackets();
sendCommonEvent( Event::OnTextChanged );
mDocView.updateCache( change.range.start().line(), change.range.start().line(), 0 );

if ( !change.text.empty() && !mDocView.isWrapEnabled() ) {
Expand All @@ -2141,6 +2141,8 @@ void UICodeEditor::onDocumentTextChanged( const DocumentContentChange& change )
}

findRegionsDelayed();
checkMatchingBrackets();
sendCommonEvent( Event::OnTextChanged );
}

void UICodeEditor::onDocumentCursorChange( const Doc::TextPosition& ) {
Expand Down Expand Up @@ -2168,6 +2170,9 @@ void UICodeEditor::onDocumentLineCountChange( const size_t& lastCount, const siz

if ( Math::countDigits( (Int64)lastCount ) != Math::countDigits( (Int64)newCount ) )
invalidateLineWrapMaxWidth( false );

if ( !mDocView.isWrapEnabled() )
sendCommonEvent( Event::OnVisibleLinesCountChange );
}

void UICodeEditor::onDocumentLineChanged( const Int64& lineNumber ) {
Expand Down

0 comments on commit e15bb23

Please sign in to comment.