Skip to content

Commit

Permalink
Text-based editors: now highlight all occurrences of a word throughou…
Browse files Browse the repository at this point in the history
…t a whole file (#1414).
  • Loading branch information
agarny committed May 17, 2018
1 parent 395e0fb commit 06a2693
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 18 deletions.
84 changes: 66 additions & 18 deletions src/plugins/widget/EditorWidget/src/editorwidget.cpp
Expand Up @@ -86,6 +86,10 @@ EditorWidget::EditorWidget(const QString &pContents, bool pReadOnly,

mEditor->setFocusPolicy(Qt::NoFocus);

// Define an indicator for our highlighting

mHighlightIndicatorNumber = mEditor->indicatorDefine(QsciScintilla::RoundBoxIndicator);

// Forward some signals that are emitted by our editor
// Note: we cannot use the new connect() syntax since the signal is located
// in our QScintilla plugin and that we don't know anything about
Expand Down Expand Up @@ -137,6 +141,9 @@ EditorWidget::EditorWidget(const QString &pContents, bool pReadOnly,
connect(mFindReplace, &EditorWidgetFindReplaceWidget::replaceAllRequested,
this, &EditorWidget::replaceAll);

connect(mFindReplace, &EditorWidgetFindReplaceWidget::searchOptionsChanged,
this, &EditorWidget::highlightAll);

// Add our editor and find/replace widgets to our layout

layout->addWidget(mEditor);
Expand Down Expand Up @@ -559,7 +566,8 @@ bool EditorWidget::findReplaceIsVisible() const

void EditorWidget::setFindReplaceVisible(bool pVisible)
{
// Set our find text, if we are to show our find/replace widget
// Set our find text and highlight all its occurrences, if we are to show
// our find/replace widget
// Note: if we are over a word, then we want it to become our find text, but
// if we make it so then a call to findText() will be triggered if the
// find text is different. Clearly, we don't want this to happen,
Expand All @@ -574,6 +582,8 @@ void EditorWidget::setFindReplaceVisible(bool pVisible)

mFindReplace->setFindText(crtWord);
mFindReplace->setActive(true);

highlightAll();
} else {
mFindReplace->selectFindText();
}
Expand Down Expand Up @@ -692,15 +702,24 @@ void EditorWidget::replaceAndFind()

//==============================================================================

void EditorWidget::replaceAll()
void EditorWidget::doHighlightAllOrReplaceAll(bool pHighlightAll)
{
// Replace all the occurences of the text
// Highlight/replace all the occurences of the text
// Note: the original plan was to call mEditor->findFirst() the first time
// round and with wrapping disabled, and then mEditor->findNext()
// until we have found all occurrences, but for some reasons this can
// result in some occurrences being missed, hence the way we do it
// below...

// Clear any previous hihghlighting, if needed

if (pHighlightAll) {
int lastLine, lastIndex;

mEditor->lineIndexFromPosition(mEditor->text().length(), &lastLine, &lastIndex);
mEditor->clearIndicatorRange(0, 0, lastLine, lastIndex, mHighlightIndicatorNumber);
}

// Keep track of the first visible line and of our position

int origFirstVisibleLine = mEditor->firstVisibleLine();
Expand All @@ -713,33 +732,39 @@ void EditorWidget::replaceAll()

mEditor->QsciScintilla::setCursorPosition(0, 0);

// Replace all occurrences
// Hihghlight / replace all occurrences

int oldLine = 0;
int oldColumn = 0;
int newLine;
int newColumn;
int firstLine = -1;
int firstColumn = -1;
int line;
int column;

while (findNext()) {
// Retrieve our new position

mEditor->getCursorPosition(&newLine, &newColumn);
mEditor->getCursorPosition(&line, &column);

// Make sure that our new position is not 'before' our old one
// Check whether we are back to our first line/column

if ( (newLine < oldLine)
|| ((newLine == oldLine) && (newColumn < oldColumn))) {
if ((line == firstLine) && (column == firstColumn))
break;
}

// Our new position is fine, so replace the occurrence
// Our new position is fine, so highlight/replace the occurrence

mEditor->replace(mFindReplace->replaceText());
if (pHighlightAll) {
mEditor->fillIndicatorRange(line, column-mFindReplace->findText().length(),
line, column,
mHighlightIndicatorNumber);
} else {
mEditor->replace(mFindReplace->replaceText());
}

// Get ready for the next occurrence
// Initialise our first line/column, if needed

oldLine = newLine;
oldColumn = newColumn;
if ((firstLine == -1) && (firstColumn == -1)) {
firstLine = line;
firstColumn = column;
}
}

// Reset the first visible line and go to our original position, after
Expand All @@ -755,6 +780,24 @@ void EditorWidget::replaceAll()

//==============================================================================

void EditorWidget::replaceAll()
{
// Replace all the occurences of the text

doHighlightAllOrReplaceAll(false);
}

//==============================================================================

void EditorWidget::highlightAll()
{
// Highlight all the occurences of the text

doHighlightAllOrReplaceAll(true);
}

//==============================================================================

void EditorWidget::keepTrackOfCursorPosition(int pLine, int pColumn)
{
// Keep track of our new position within our editor
Expand Down Expand Up @@ -852,6 +895,11 @@ void EditorWidget::findTextChanged(const QString &pText)
mEditor->setSelection(mCurrentLine, mCurrentColumn,
mCurrentLine, mCurrentColumn);
} else {
// Look for the first occurrence of the given text in our editor, but
// first highlight all the occurrences of the given text

highlightAll();

findText(pText, true);
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/plugins/widget/EditorWidget/src/editorwidget.h
Expand Up @@ -146,6 +146,8 @@ class EDITORWIDGET_EXPORT EditorWidget : public Core::Widget
QFrame *mSeparator;
EditorWidgetFindReplaceWidget *mFindReplace;

int mHighlightIndicatorNumber;

int mCurrentLine;
int mCurrentColumn;

Expand All @@ -157,6 +159,8 @@ class EDITORWIDGET_EXPORT EditorWidget : public Core::Widget

bool findText(const QString &pText, bool pForward);

void doHighlightAllOrReplaceAll(bool pHighlightAll);

signals:
void cursorPositionChanged(int pRow, int pColumn);

Expand All @@ -183,6 +187,8 @@ private slots:
void replace();
void replaceAndFind();
void replaceAll();

void highlightAll();
};

//==============================================================================
Expand Down
Expand Up @@ -565,6 +565,10 @@ void EditorWidgetFindReplaceWidget::searchOptionChanged()
}

mDropDownAction->setIcon(dropDownPixmap);

// Let people know that our search options have changed

emit searchOptionsChanged();
}

//==============================================================================
Expand Down
Expand Up @@ -119,6 +119,8 @@ class EditorWidgetFindReplaceWidget : public Core::Widget
void replaceAndFindRequested();
void replaceAllRequested();

void searchOptionsChanged();

private slots:
void findPreviousButtonClicked();
void findNextButtonClicked();
Expand Down

0 comments on commit 06a2693

Please sign in to comment.