forked from qt-creator/qt-creator
Git: Fix regexp substitution with multiple occurrences in line
If the line has multiple occurrences of the search pattern, all occurrences got the same capture groups. For example, text: Foo(1); Foo(2); Search for Foo\((.)\) and replace with \1, the result was: 1; 1 Change-Id: Idd4dc21397d5331b1e31a2860eca39d9bc407437 Reviewed-by: Christian Stenger <christian.stenger@qt.io> Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
committed by
Orgad Shaneh
parent
879a7082f8
commit
c879a78c03
@@ -82,6 +82,17 @@ public:
|
|||||||
m_directory = parameters.additionalParameters.toString();
|
m_directory = parameters.additionalParameters.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Match
|
||||||
|
{
|
||||||
|
Match() = default;
|
||||||
|
Match(int start, int length) :
|
||||||
|
matchStart(start), matchLength(length) {}
|
||||||
|
|
||||||
|
int matchStart = 0;
|
||||||
|
int matchLength = 0;
|
||||||
|
QStringList regexpCapturedTexts;
|
||||||
|
};
|
||||||
|
|
||||||
void processLine(const QString &line, FileSearchResultList *resultList) const
|
void processLine(const QString &line, FileSearchResultList *resultList) const
|
||||||
{
|
{
|
||||||
if (line.isEmpty())
|
if (line.isEmpty())
|
||||||
@@ -97,7 +108,15 @@ public:
|
|||||||
const int textSeparator = line.indexOf(QChar::Null, lineSeparator + 1);
|
const int textSeparator = line.indexOf(QChar::Null, lineSeparator + 1);
|
||||||
single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt();
|
single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt();
|
||||||
QString text = line.mid(textSeparator + 1);
|
QString text = line.mid(textSeparator + 1);
|
||||||
QVector<QPair<int, int>> matches;
|
QRegularExpression regexp;
|
||||||
|
QVector<Match> matches;
|
||||||
|
if (m_parameters.flags & FindRegularExpression) {
|
||||||
|
const QRegularExpression::PatternOptions patternOptions =
|
||||||
|
(m_parameters.flags & QTextDocument::FindCaseSensitively)
|
||||||
|
? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption;
|
||||||
|
regexp.setPattern(m_parameters.text);
|
||||||
|
regexp.setPatternOptions(patternOptions);
|
||||||
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
const int matchStart = text.indexOf(boldRed);
|
const int matchStart = text.indexOf(boldRed);
|
||||||
if (matchStart == -1)
|
if (matchStart == -1)
|
||||||
@@ -106,23 +125,19 @@ public:
|
|||||||
const int matchEnd = text.indexOf(resetColor, matchTextStart);
|
const int matchEnd = text.indexOf(resetColor, matchTextStart);
|
||||||
QTC_ASSERT(matchEnd != -1, break);
|
QTC_ASSERT(matchEnd != -1, break);
|
||||||
const int matchLength = matchEnd - matchTextStart;
|
const int matchLength = matchEnd - matchTextStart;
|
||||||
matches.append(qMakePair(matchStart, matchLength));
|
Match match(matchStart, matchLength);
|
||||||
text = text.left(matchStart) + text.mid(matchTextStart, matchLength)
|
const QStringRef matchText = text.midRef(matchTextStart, matchLength);
|
||||||
+ text.mid(matchEnd + resetColor.size());
|
if (m_parameters.flags & FindRegularExpression)
|
||||||
|
match.regexpCapturedTexts = regexp.match(matchText).capturedTexts();
|
||||||
|
matches.append(match);
|
||||||
|
text = text.leftRef(matchStart) + matchText + text.midRef(matchEnd + resetColor.size());
|
||||||
}
|
}
|
||||||
single.matchingLine = text;
|
single.matchingLine = text;
|
||||||
|
|
||||||
if (m_parameters.flags & FindRegularExpression) {
|
|
||||||
const QRegularExpression::PatternOptions patternOptions =
|
|
||||||
(m_parameters.flags & QTextDocument::FindCaseSensitively)
|
|
||||||
? QRegularExpression::NoPatternOption : QRegularExpression::CaseInsensitiveOption;
|
|
||||||
QRegularExpression regexp(m_parameters.text, patternOptions);
|
|
||||||
QRegularExpressionMatch regexpMatch = regexp.match(line);
|
|
||||||
single.regexpCapturedTexts = regexpMatch.capturedTexts();
|
|
||||||
}
|
|
||||||
for (auto match : qAsConst(matches)) {
|
for (auto match : qAsConst(matches)) {
|
||||||
single.matchStart = match.first;
|
single.matchStart = match.matchStart;
|
||||||
single.matchLength = match.second;
|
single.matchLength = match.matchLength;
|
||||||
|
single.regexpCapturedTexts = match.regexpCapturedTexts;
|
||||||
resultList->append(single);
|
resultList->append(single);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user