Optimize multiple cursors selection (issue SpartanJ/ecode#356).

This commit is contained in:
Martín Lucas Golini
2024-11-08 02:40:46 -03:00
parent 6d91ac11d8
commit 0027e0b22b
3 changed files with 29 additions and 20 deletions
+21 -17
View File
@@ -185,18 +185,20 @@ class EE_API TextRanges : public std::vector<TextRange> {
}
bool exists( const TextRange& range ) const {
for ( const auto& r : *this )
if ( range == r )
return true;
return false;
if ( !mIsSorted )
return std::find( begin(), end(), range ) != end();
return std::binary_search( begin(), end(), range );
}
size_t findIndex( const TextRange& range ) const {
for ( size_t i = 0; i < size(); ++i ) {
if ( ( *this )[i] == range )
return i;
if ( !mIsSorted ) {
auto it = std::find( begin(), end(), range );
return it != end() ? std::distance( begin(), it ) : static_cast<size_t>( -1 );
} else {
auto it = std::lower_bound( begin(), end(), range );
return ( it != end() && *it == range ) ? std::distance( begin(), it )
: static_cast<size_t>( -1 );
}
return 0;
}
bool hasSelection() const {
@@ -216,15 +218,17 @@ class EE_API TextRanges : public std::vector<TextRange> {
bool merge() {
if ( size() <= 1 )
return false;
size_t oldSize = size();
TextRanges newRanges;
newRanges.emplace_back( ( *this )[0] );
for ( size_t i = 1; i < size(); ++i )
if ( !newRanges.exists( ( *this )[i] ) )
newRanges.emplace_back( ( *this )[i] );
*this = newRanges;
sort();
return oldSize != size();
if ( !mIsSorted )
sort();
auto it = std::unique( begin(), end(),
[]( const TextRange& a, const TextRange& b ) { return a == b; } );
bool merged = it != end();
erase( it, end() );
return merged;
}
std::string toString() const {
+7 -2
View File
@@ -1411,8 +1411,13 @@ Uint32 UICodeEditor::onMouseDown( const Vector2i& position, const Uint32& flags
TextRange range( mDoc->getSelection().start(), textScreenPos );
range.normalize();
range = mDoc->sanitizeRange( range );
for ( Int64 i = range.start().line(); i < range.end().line(); ++i )
mDoc->addSelection( { i, range.start().column() } );
TextRanges ranges;
ranges.reserve( range.end().line() - range.start().line() + 1 );
for ( Int64 i = range.start().line(); i < range.end().line(); ++i ) {
TextPosition pos{ i, range.start().column() };
ranges.push_back( TextRange{ pos, pos } );
}
mDoc->addSelections( std::move( ranges ) );
} else if ( input->isModState( KEYMOD_SHIFT ) ) {
mDoc->selectTo( textScreenPos );
} else if ( input->isModState( KEYMOD_CTRL ) &&
+1 -1
View File
@@ -8,7 +8,7 @@ using namespace EE;
#define ECODE_MAJOR_VERSION 0
#define ECODE_MINOR_VERSION 6
#define ECODE_PATCH_LEVEL 3
#define ECODE_PATCH_LEVEL 4
/* ECODE_COMMIT_NUMBER 9999 is used for official releases, nightly builds (pre-releases) will
* contain the number of commits after the last official release
*/