Skip to content

Commit

Permalink
Do not break selection on Punctuation_Connector
Browse files Browse the repository at this point in the history
Selecting words by double click will not break on Unicode characters
from Punctuation_Connector class, this includes `_` (#310)
  • Loading branch information
variar committed May 19, 2021
1 parent cb3710f commit f2b1b99
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions src/ui/src/abstractlogview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1511,35 +1511,29 @@ void AbstractLogView::jumpToBottom()
// Select the word under the given position
void AbstractLogView::selectWordAtPosition( const QPoint& pos )
{
const int x = pos.x();
const auto lineNumber = LineNumber( static_cast<LineNumber::UnderlyingType>( pos.y() ) );
const QString line = logData_->getExpandedLineString( lineNumber );

if ( !line.isEmpty() && line[ x ].isLetterOrNumber() ) {
// Search backward for the first character in the word
int currentPos = x;
for ( ; currentPos > 0; currentPos-- )
if ( !line[ currentPos ].isLetterOrNumber() )
break;
// Exclude the first char of the line if needed
if ( !line[ currentPos ].isLetterOrNumber() )
currentPos++;
int start = currentPos;

// Now search for the end
currentPos = x;
for ( ; currentPos < line.length() - 1; currentPos++ )
if ( !line[ currentPos ].isLetterOrNumber() )
break;
// Exclude the last char of the line if needed
if ( !line[ currentPos ].isLetterOrNumber() )
currentPos--;
int end = currentPos;
const int clickPos = pos.x();

selection_.selectPortion( lineNumber, start, end );
updateGlobalSelection();
update();
const auto isWordSeparator = []( QChar c ) {
return !c.isLetterOrNumber() && c.category() != QChar::Punctuation_Connector;
};

if ( line.isEmpty() || isWordSeparator( line[ clickPos ] ) ) {
return;
}

const auto wordStart
= std::find_if( line.rbegin() + line.size() - clickPos, line.rend(), isWordSeparator );
const auto selectionStart = static_cast<int>( std::distance( line.begin(), wordStart.base() ) );

const auto wordEnd = std::find_if( line.begin() + clickPos, line.end(), isWordSeparator );
const auto selectionEnd = static_cast<int>( std::distance( line.begin(), wordEnd ) - 1 );

selection_.selectPortion( lineNumber, selectionStart, selectionEnd );
updateGlobalSelection();
update();
}

// Update the system global (middle click) selection (X11 only)
Expand Down

0 comments on commit f2b1b99

Please sign in to comment.