VcsOutputFormatter: Fix multiple regexp matches

Fixes: QTCREATORBUG-23614
Change-Id: I86e548a1f727113782afbc6b934d6dddc92c92ea
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Andre Hartmann
2020-02-18 01:35:26 +01:00
committed by André Hartmann
parent 5bb6c7f0cf
commit 937e2b56e7
2 changed files with 16 additions and 24 deletions

View File

@@ -31,40 +31,33 @@
namespace VcsBase { namespace VcsBase {
VcsOutputFormatter::VcsOutputFormatter() : VcsOutputFormatter::VcsOutputFormatter() :
m_urlRegexp("https?://\\S*"), m_regexp(
m_referenceRegexp("(v[0-9]+\\.[0-9]+\\.[0-9]+[\\-A-Za-z0-9]*)" // v0.1.2-beta3 "(https?://\\S*)" // https://codereview.org/c/1234
"|([0-9a-f]{6,}(?:\\.\\.[0-9a-f]{6,})?)") // 789acf or 123abc..456cde "|(v[0-9]+\\.[0-9]+\\.[0-9]+[\\-A-Za-z0-9]*)" // v0.1.2-beta3
"|([0-9a-f]{6,}(?:\\.\\.[0-9a-f]{6,})?)") // 789acf or 123abc..456cde
{ {
} }
void VcsOutputFormatter::appendMessage(const QString &text, Utils::OutputFormat format) void VcsOutputFormatter::appendMessage(const QString &text, Utils::OutputFormat format)
{ {
const QRegularExpressionMatch urlMatch = m_urlRegexp.match(text); QRegularExpressionMatchIterator it = m_regexp.globalMatch(text);
const QRegularExpressionMatch referenceMatch = m_referenceRegexp.match(text); int begin = 0;
while (it.hasNext()) {
auto append = [this](const QRegularExpressionMatch &match, const QRegularExpressionMatch match = it.next();
QString text, Utils::OutputFormat format) {
const QTextCharFormat normalFormat = charFormat(format); const QTextCharFormat normalFormat = charFormat(format);
OutputFormatter::appendMessage(text.left(match.capturedStart()), format); OutputFormatter::appendMessage(text.mid(begin, match.capturedStart() - begin), format);
QTextCursor tc = plainTextEdit()->textCursor(); QTextCursor tc = plainTextEdit()->textCursor();
QStringView url = match.capturedView(); QStringView url = match.capturedView();
int end = match.capturedEnd(); begin = match.capturedEnd();
while (url.rbegin()->isPunct()) { while (url.rbegin()->isPunct()) {
url.chop(1); url.chop(1);
--end; --begin;
} }
tc.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); tc.movePosition(QTextCursor::End);
tc.insertText(url.toString(), linkFormat(normalFormat, url.toString())); tc.insertText(url.toString(), linkFormat(normalFormat, url.toString()));
tc.movePosition(QTextCursor::End, QTextCursor::MoveAnchor); tc.movePosition(QTextCursor::End);
OutputFormatter::appendMessage(text.mid(end), format); }
}; OutputFormatter::appendMessage(text.mid(begin), format);
if (urlMatch.hasMatch())
append(urlMatch, text, format);
else if (referenceMatch.hasMatch())
append(referenceMatch, text, format);
else
OutputFormatter::appendMessage(text, format);
} }
void VcsOutputFormatter::handleLink(const QString &href) void VcsOutputFormatter::handleLink(const QString &href)

View File

@@ -42,8 +42,7 @@ signals:
void referenceClicked(const QString &reference); void referenceClicked(const QString &reference);
private: private:
const QRegularExpression m_urlRegexp; const QRegularExpression m_regexp;
const QRegularExpression m_referenceRegexp;
}; };
} }