Skip to content

Commit

Permalink
Highlight matching results in code editors while searching
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 8, 2024
1 parent 041fc76 commit 5b71c5e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
42 changes: 40 additions & 2 deletions src/gui/codeeditors/qgscodeeditorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ QgsCodeEditorWidget::QgsCodeEditorWidget( QgsCodeEditor *editor, QWidget *parent
findShortcut->setContext( Qt::ShortcutContext::WidgetWithChildrenShortcut );
connect( findShortcut, &QShortcut::activated, this, [this]
{
showSearchBar();
clearSearchHighlights();
mLineEditFind->setFocus();
if ( mEditor->hasSelectedText() )
{
Expand All @@ -87,6 +87,7 @@ QgsCodeEditorWidget::QgsCodeEditorWidget( QgsCodeEditor *editor, QWidget *parent
mBlockSearching--;
}
mLineEditFind->selectAll();
showSearchBar();
} );

QShortcut *findNextShortcut = new QShortcut( QKeySequence::StandardKey::FindNext, this );
Expand Down Expand Up @@ -134,11 +135,13 @@ QgsCodeEditorWidget::QgsCodeEditorWidget( QgsCodeEditor *editor, QWidget *parent

void QgsCodeEditorWidget::showSearchBar()
{
addSearchHighlights();
mFindWidget->show();
}

void QgsCodeEditorWidget::hideSearchBar()
{
clearSearchHighlights();
mFindWidget->hide();
}

Expand Down Expand Up @@ -170,6 +173,7 @@ void QgsCodeEditorWidget::textSearchChanged( const QString &text )
}
else
{
clearSearchHighlights();
mLineEditFind->setStyleSheet( QString() );
mFindNextButton->setEnabled( false );
mFindPrevButton->setEnabled( false );
Expand All @@ -181,11 +185,45 @@ void QgsCodeEditorWidget::updateSearch()
if ( mBlockSearching )
return;

clearSearchHighlights();
addSearchHighlights();

findText( true, true, true );
}

void QgsCodeEditorWidget::addSearchHighlights()
{
const QString searchString = mLineEditFind->text();
if ( searchString.isEmpty() )
return;

findText( true, true, true );
const long originalStartPos = mEditor->SendScintilla( QsciScintilla::SCI_GETTARGETSTART );
const long originalEndPos = mEditor->SendScintilla( QsciScintilla::SCI_GETTARGETEND );
long startPos = 0;
long docEnd = mEditor->length();

while ( true )
{
mEditor->SendScintilla( QsciScintilla::SCI_SETTARGETRANGE, startPos, docEnd );
const int fstart = mEditor->SendScintilla( QsciScintilla::SCI_SEARCHINTARGET, searchString.length(), searchString.toLocal8Bit().constData() );
if ( fstart < 0 )
break;

startPos = fstart + searchString.length();

mEditor->SendScintilla( QsciScintilla::SCI_SETINDICATORCURRENT, QgsCodeEditor::SEARCH_RESULT_INDICATOR );
mEditor->SendScintilla( QsciScintilla::SCI_INDICATORFILLRANGE, fstart, searchString.length() );
}

mEditor->SendScintilla( QsciScintilla::SCI_SETTARGETRANGE, originalStartPos, originalEndPos );
}

void QgsCodeEditorWidget::clearSearchHighlights()
{
long docStart = 0;
long docEnd = mEditor->length();
mEditor->SendScintilla( QsciScintilla::SCI_SETINDICATORCURRENT, QgsCodeEditor::SEARCH_RESULT_INDICATOR );
mEditor->SendScintilla( QsciScintilla::SCI_INDICATORCLEARRANGE, docStart, docEnd - docStart );
}

void QgsCodeEditorWidget::findText( bool forward, bool findFirst, bool showNotFoundWarning )
Expand Down
2 changes: 2 additions & 0 deletions src/gui/codeeditors/qgscodeeditorwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ class GUI_EXPORT QgsCodeEditorWidget : public QgsPanelWidget

private:

void clearSearchHighlights();
void addSearchHighlights();
void findText( bool forward, bool findFirst, bool showNotFoundWarning = false );

QgsCodeEditor *mEditor = nullptr;
Expand Down

0 comments on commit 5b71c5e

Please sign in to comment.