aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/git/gitgrep.cpp
diff options
context:
space:
mode:
authorOrgad Shaneh <orgad.shaneh@audiocodes.com>2018-10-08 17:23:29 +0300
committerOrgad Shaneh <orgads@gmail.com>2018-10-10 08:29:55 +0000
commitc879a78c032d42a4fac93832c8ec7e0d603fe75d (patch)
tree9b276027790d9bb5afcd27d8125374e50f7208b4 /src/plugins/git/gitgrep.cpp
parent879a7082f8a64da6038f6c6b074f6b3c5a541de1 (diff)
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>
Diffstat (limited to 'src/plugins/git/gitgrep.cpp')
-rw-r--r--src/plugins/git/gitgrep.cpp43
1 files changed, 29 insertions, 14 deletions
diff --git a/src/plugins/git/gitgrep.cpp b/src/plugins/git/gitgrep.cpp
index 063deb98a25..d4c850bcdf5 100644
--- a/src/plugins/git/gitgrep.cpp
+++ b/src/plugins/git/gitgrep.cpp
@@ -82,6 +82,17 @@ public:
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
{
if (line.isEmpty())
@@ -97,7 +108,15 @@ public:
const int textSeparator = line.indexOf(QChar::Null, lineSeparator + 1);
single.lineNumber = line.mid(lineSeparator + 1, textSeparator - lineSeparator - 1).toInt();
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 (;;) {
const int matchStart = text.indexOf(boldRed);
if (matchStart == -1)
@@ -106,23 +125,19 @@ public:
const int matchEnd = text.indexOf(resetColor, matchTextStart);
QTC_ASSERT(matchEnd != -1, break);
const int matchLength = matchEnd - matchTextStart;
- matches.append(qMakePair(matchStart, matchLength));
- text = text.left(matchStart) + text.mid(matchTextStart, matchLength)
- + text.mid(matchEnd + resetColor.size());
+ Match match(matchStart, matchLength);
+ const QStringRef matchText = text.midRef(matchTextStart, matchLength);
+ 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;
- 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)) {
- single.matchStart = match.first;
- single.matchLength = match.second;
+ single.matchStart = match.matchStart;
+ single.matchLength = match.matchLength;
+ single.regexpCapturedTexts = match.regexpCapturedTexts;
resultList->append(single);
}
}