forked from qt-creator/qt-creator
Fix highlighting of regexp search results in editor
The highlighting in the editor was still done with QRegExp, so if you used Perl regular expression features, highlighting in the editor was incorrect. Fixes: QTCREATORBUG-21940 Change-Id: I785f0b2413a291d9f06de5877b18067a30d58588 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
This commit is contained in:
@@ -692,7 +692,8 @@ public:
|
|||||||
QTextCursor m_pendingLinkUpdate;
|
QTextCursor m_pendingLinkUpdate;
|
||||||
QTextCursor m_lastLinkUpdate;
|
QTextCursor m_lastLinkUpdate;
|
||||||
|
|
||||||
QRegExp m_searchExpr;
|
QRegularExpression m_searchExpr;
|
||||||
|
QString m_findText;
|
||||||
FindFlags m_findFlags;
|
FindFlags m_findFlags;
|
||||||
void highlightSearchResults(const QTextBlock &block, const PaintEventData &data) const;
|
void highlightSearchResults(const QTextBlock &block, const PaintEventData &data) const;
|
||||||
QTimer m_delayedUpdateTimer;
|
QTimer m_delayedUpdateTimer;
|
||||||
@@ -3663,7 +3664,7 @@ QTextBlock TextEditorWidgetPrivate::foldedBlockAt(const QPoint &pos, QRect *box)
|
|||||||
|
|
||||||
void TextEditorWidgetPrivate::highlightSearchResults(const QTextBlock &block, const PaintEventData &data) const
|
void TextEditorWidgetPrivate::highlightSearchResults(const QTextBlock &block, const PaintEventData &data) const
|
||||||
{
|
{
|
||||||
if (m_searchExpr.isEmpty())
|
if (m_searchExpr.pattern().isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int blockPosition = block.position();
|
int blockPosition = block.position();
|
||||||
@@ -3682,10 +3683,11 @@ void TextEditorWidgetPrivate::highlightSearchResults(const QTextBlock &block, co
|
|||||||
.toTextCharFormat(C_SEARCH_RESULT).background().color().darker(120);
|
.toTextCharFormat(C_SEARCH_RESULT).background().color().darker(120);
|
||||||
|
|
||||||
while (idx < text.length()) {
|
while (idx < text.length()) {
|
||||||
idx = m_searchExpr.indexIn(text, idx + l);
|
const QRegularExpressionMatch match = m_searchExpr.match(text, idx + 1);
|
||||||
if (idx < 0)
|
if (!match.hasMatch())
|
||||||
break;
|
break;
|
||||||
l = m_searchExpr.matchedLength();
|
idx = match.capturedStart();
|
||||||
|
l = match.capturedLength();
|
||||||
if (l == 0)
|
if (l == 0)
|
||||||
break;
|
break;
|
||||||
if ((m_findFlags & FindWholeWords)
|
if ((m_findFlags & FindWholeWords)
|
||||||
@@ -4280,7 +4282,7 @@ void TextEditorWidgetPrivate::paintSearchResultOverlay(const PaintEventData &dat
|
|||||||
QPainter &painter) const
|
QPainter &painter) const
|
||||||
{
|
{
|
||||||
m_searchResultOverlay->clear();
|
m_searchResultOverlay->clear();
|
||||||
if (m_searchExpr.isEmpty())
|
if (m_searchExpr.pattern().isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
const int margin = 5;
|
const int margin = 5;
|
||||||
@@ -5283,7 +5285,7 @@ void TextEditorWidgetPrivate::slotUpdateRequest(const QRect &r, int dy)
|
|||||||
m_extraArea->scroll(0, dy);
|
m_extraArea->scroll(0, dy);
|
||||||
} else if (r.width() > 4) { // wider than cursor width, not just cursor blinking
|
} else if (r.width() > 4) { // wider than cursor width, not just cursor blinking
|
||||||
m_extraArea->update(0, r.y(), m_extraArea->width(), r.height());
|
m_extraArea->update(0, r.y(), m_extraArea->width(), r.height());
|
||||||
if (!m_searchExpr.isEmpty()) {
|
if (!m_searchExpr.pattern().isEmpty()) {
|
||||||
const int m = m_searchResultOverlay->dropShadowWidth();
|
const int m = m_searchResultOverlay->dropShadowWidth();
|
||||||
q->viewport()->update(r.adjusted(-m, -m, m, m));
|
q->viewport()->update(r.adjusted(-m, -m, m, m));
|
||||||
}
|
}
|
||||||
@@ -6303,13 +6305,16 @@ void TextEditorWidgetPrivate::clearLink()
|
|||||||
|
|
||||||
void TextEditorWidgetPrivate::highlightSearchResultsSlot(const QString &txt, FindFlags findFlags)
|
void TextEditorWidgetPrivate::highlightSearchResultsSlot(const QString &txt, FindFlags findFlags)
|
||||||
{
|
{
|
||||||
if (m_searchExpr.pattern() == txt)
|
const QString pattern = (findFlags & FindRegularExpression) ? txt
|
||||||
|
: QRegularExpression::escape(txt);
|
||||||
|
const QRegularExpression::PatternOptions options
|
||||||
|
= (findFlags & FindCaseSensitively) ? QRegularExpression::NoPatternOption
|
||||||
|
: QRegularExpression::CaseInsensitiveOption;
|
||||||
|
if (m_searchExpr.pattern() == pattern && m_searchExpr.patternOptions() == options)
|
||||||
return;
|
return;
|
||||||
m_searchExpr.setPattern(txt);
|
m_searchExpr.setPattern(pattern);
|
||||||
m_searchExpr.setPatternSyntax((findFlags & FindRegularExpression) ?
|
m_searchExpr.setPatternOptions(options);
|
||||||
QRegExp::RegExp : QRegExp::FixedString);
|
m_findText = txt;
|
||||||
m_searchExpr.setCaseSensitivity((findFlags & FindCaseSensitively) ?
|
|
||||||
Qt::CaseSensitive : Qt::CaseInsensitive);
|
|
||||||
m_findFlags = findFlags;
|
m_findFlags = findFlags;
|
||||||
|
|
||||||
m_delayedUpdateTimer.start(50);
|
m_delayedUpdateTimer.start(50);
|
||||||
@@ -6367,7 +6372,7 @@ void TextEditorWidgetPrivate::highlightSearchResultsInScrollBar()
|
|||||||
m_searchWatcher = nullptr;
|
m_searchWatcher = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
const QString &txt = m_searchExpr.pattern();
|
const QString &txt = m_findText;
|
||||||
if (txt.isEmpty())
|
if (txt.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user