diff --git a/include/eepp/ui/doc/textdocument.hpp b/include/eepp/ui/doc/textdocument.hpp index 8d4c89b9a..c00dbe246 100644 --- a/include/eepp/ui/doc/textdocument.hpp +++ b/include/eepp/ui/doc/textdocument.hpp @@ -287,6 +287,8 @@ class EE_API TextDocument { std::size_t getLineLength( Int64 ) const; + void safeLineOp( Int64 line, std::function op ); + void getLineTextToBuffer( Int64 line, String& buffer ) const; std::string getLineTextUtf8( Int64 line ) const; diff --git a/src/eepp/ui/doc/textdocument.cpp b/src/eepp/ui/doc/textdocument.cpp index 00f3adfb7..52b7fd370 100644 --- a/src/eepp/ui/doc/textdocument.cpp +++ b/src/eepp/ui/doc/textdocument.cpp @@ -1727,6 +1727,16 @@ std::size_t TextDocument::getLineLength( Int64 line ) const { return line >= (Int64)mLines.size() ? 0 : mLines[line].size(); } +void TextDocument::safeLineOp( Int64 line, std::function op ) { + Lock l( mLinesMutex ); + static TextDocumentLine safeLine = TextDocumentLine( "", nullptr ); + if ( line >= 0 && line < mLines.size() ) { + op( mLines[line] ); + } else { + op( safeLine ); + } +} + String TextDocument::getLineText( Int64 line ) const { // eeASSERT( line < (Int64)linesCount() ); Lock l( mLinesMutex ); diff --git a/src/eepp/ui/uicodeeditor.cpp b/src/eepp/ui/uicodeeditor.cpp index b4200ad48..7770cbb16 100644 --- a/src/eepp/ui/uicodeeditor.cpp +++ b/src/eepp/ui/uicodeeditor.cpp @@ -714,8 +714,15 @@ void UICodeEditor::onFoldRegionsUpdated( size_t oldCount, size_t newCount ) { } Uint32 UICodeEditor::onMessage( const NodeMessage* msg ) { - if ( msg->getMsg() == NodeMessage::MouseDown ) + if ( msg->getMsg() == NodeMessage::MouseDown ) { return 1; + } else if ( msg->getMsg() == NodeMessage::Focus ) { + if ( msg->getSender() == mVScrollBar || msg->getSender() == mHScrollBar || + mVScrollBar->isParentOf( msg->getSender() ) || + mHScrollBar->isParentOf( msg->getSender() ) ) { + setFocus(); + } + } return UIWidget::onMessage( msg ); } @@ -5194,11 +5201,15 @@ bool UICodeEditor::checkAutoCloseXMLTag( const String& text ) { } Int64 UICodeEditor::getCurrentColumnCount() const { - Int64 curLine = mDoc->getSelection().start().line(); - Int64 sel = mDoc->getSelection().start().column(); Int64 count = 0; - for ( Int64 i = 0; i < sel; i++ ) - count += mDoc->line( curLine ).getText()[i] == '\t' ? mTabWidth : 1; + mDoc->safeLineOp( + mDoc->getSelection().start().line(), [this, &count]( TextDocumentLine& line ) { + const String& lineText = mDoc->line( mDoc->getSelection().start().line() ).getText(); + Int64 lineLen = lineText.size(); + Int64 sel = eemin( mDoc->getSelection().start().column(), lineLen ); + for ( Int64 i = 0; i < sel; i++ ) + count += lineText[i] == '\t' ? mTabWidth : 1; + } ); return count; }