Editor: refine find in page

This commit is contained in:
Le Tan 2018-09-07 20:54:12 +08:00
parent 33c3a5a301
commit ca01e26333
2 changed files with 80 additions and 39 deletions

View File

@ -325,6 +325,25 @@ void VEditor::highlightTextAll(const QString &p_text,
highlightExtraSelections(); highlightExtraSelections();
} }
static QTextDocument::FindFlags findOptionsToFlags(uint p_options, bool p_forward)
{
QTextDocument::FindFlags findFlags;
if (p_options & FindOption::CaseSensitive) {
findFlags |= QTextDocument::FindCaseSensitively;
}
if (p_options & FindOption::WholeWordOnly) {
findFlags |= QTextDocument::FindWholeWords;
}
if (!p_forward) {
findFlags |= QTextDocument::FindBackward;
}
return findFlags;
}
QList<QTextCursor> VEditor::findTextAll(const QString &p_text, uint p_options) QList<QTextCursor> VEditor::findTextAll(const QString &p_text, uint p_options)
{ {
QList<QTextCursor> results; QList<QTextCursor> results;
@ -333,21 +352,13 @@ QList<QTextCursor> VEditor::findTextAll(const QString &p_text, uint p_options)
} }
// Options // Options
QTextDocument::FindFlags findFlags; bool caseSensitive = p_options & FindOption::CaseSensitive;
bool caseSensitive = false; QTextDocument::FindFlags findFlags = findOptionsToFlags(p_options, true);
if (p_options & FindOption::CaseSensitive) {
findFlags |= QTextDocument::FindCaseSensitively;
caseSensitive = true;
}
if (p_options & FindOption::WholeWordOnly) {
findFlags |= QTextDocument::FindWholeWords;
}
// Use regular expression // Use regular expression
bool useRegExp = false; bool useRegExp = p_options & FindOption::RegularExpression;
QRegExp exp; QRegExp exp;
if (p_options & FindOption::RegularExpression) { if (useRegExp) {
useRegExp = true; useRegExp = true;
exp = QRegExp(p_text, exp = QRegExp(p_text,
caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive); caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
@ -486,18 +497,27 @@ bool VEditor::peekText(const QString &p_text, uint p_options, bool p_forward)
bool wrapped = false; bool wrapped = false;
QTextCursor retCursor; QTextCursor retCursor;
bool found = findTextHelper(p_text, int start = textCursorW().position();
p_options, int skipPosition = start;
p_forward,
p_forward ? textCursorW().position() + 1 bool found = false;
: textCursorW().position(), while (true) {
wrapped, found = findTextHelper(p_text, p_options, p_forward, start, wrapped, retCursor);
retCursor);
if (found) { if (found) {
if (p_forward && retCursor.selectionStart() == skipPosition) {
// Skip the first match.
skipPosition = -1;
start = retCursor.selectionEnd();
continue;
}
makeBlockVisible(m_document->findBlock(retCursor.selectionStart())); makeBlockVisible(m_document->findBlock(retCursor.selectionStart()));
highlightIncrementalSearchedWord(retCursor); highlightIncrementalSearchedWord(retCursor);
} }
break;
}
return found; return found;
} }
@ -569,6 +589,37 @@ bool VEditor::findText(const QString &p_text,
return found; return found;
} }
bool VEditor::findTextOne(const QString &p_text, uint p_options, bool p_forward)
{
clearIncrementalSearchedWordHighlight();
if (p_text.isEmpty()) {
clearSearchedWordHighlight();
return false;
}
QTextCursor cursor = textCursorW();
bool wrapped = false;
QTextCursor retCursor;
int start = cursor.position();
bool found = findTextHelper(p_text, p_options, p_forward, start, wrapped, retCursor);
if (found) {
Q_ASSERT(!retCursor.isNull());
if (wrapped) {
showWrapLabel();
}
cursor.setPosition(retCursor.selectionStart(), QTextCursor::MoveAnchor);
setTextCursorW(cursor);
highlightSearchedWordUnderCursor(retCursor);
} else {
clearSearchedWordHighlight();
}
return found;
}
bool VEditor::findTextInRange(const QString &p_text, bool VEditor::findTextInRange(const QString &p_text,
uint p_options, uint p_options,
bool p_forward, bool p_forward,
@ -616,25 +667,13 @@ bool VEditor::findTextHelper(const QString &p_text,
bool found = false; bool found = false;
// Options // Options
QTextDocument::FindFlags findFlags; bool caseSensitive = p_options & FindOption::CaseSensitive;
bool caseSensitive = false; QTextDocument::FindFlags findFlags = findOptionsToFlags(p_options, p_forward);
if (p_options & FindOption::CaseSensitive) {
findFlags |= QTextDocument::FindCaseSensitively;
caseSensitive = true;
}
if (p_options & FindOption::WholeWordOnly) {
findFlags |= QTextDocument::FindWholeWords;
}
if (!p_forward) {
findFlags |= QTextDocument::FindBackward;
}
// Use regular expression // Use regular expression
bool useRegExp = false; bool useRegExp = p_options & FindOption::RegularExpression;
QRegExp exp; QRegExp exp;
if (p_options & FindOption::RegularExpression) { if (useRegExp) {
useRegExp = true; useRegExp = true;
// FIXME: hang bug in Qt's find(). // FIXME: hang bug in Qt's find().
QRegExp test("[$^]+"); QRegExp test("[$^]+");
@ -872,7 +911,7 @@ void VEditor::replaceText(const QString &p_text,
} }
if (p_findNext) { if (p_findNext) {
findText(p_text, p_options, true); findTextOne(p_text, p_options, true);
} }
} }
} }

View File

@ -320,6 +320,8 @@ private:
void cleanUp(); void cleanUp();
bool findTextOne(const QString &p_text, uint p_options, bool p_forward);
QLabel *m_wrapLabel; QLabel *m_wrapLabel;
QTimer *m_labelTimer; QTimer *m_labelTimer;