VCS: Refactor annotationChanges() in VcsBaseEditor

Devirtualize the function, and use QRegularExpression with globalMatch.

Change-Id: I18c92cb37b535c616f03f45dff8b18249c961d5d
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Orgad Shaneh
2020-02-19 23:23:36 +02:00
committed by Orgad Shaneh
parent 3cdbd8683d
commit 3c0b89f697
17 changed files with 54 additions and 160 deletions

View File

@@ -48,28 +48,7 @@ BazaarEditorWidget::BazaarEditorWidget() :
// === <change> <file|dir> 'mainwindow.cpp' // === <change> <file|dir> 'mainwindow.cpp'
setDiffFilePattern(QRegExp(QLatin1String("^=== [a-z]+ [a-z]+ '(.+)'\\s*"))); setDiffFilePattern(QRegExp(QLatin1String("^=== [a-z]+ [a-z]+ '(.+)'\\s*")));
setLogEntryPattern(QRegExp(QLatin1String("^revno: (\\d+)"))); setLogEntryPattern(QRegExp(QLatin1String("^revno: (\\d+)")));
} setAnnotationEntryPattern("^(" BZR_CHANGE_PATTERN ") ");
QSet<QString> BazaarEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString txt = toPlainText();
if (txt.isEmpty())
return changes;
QRegExp changeNumRx(QLatin1String("^(" BZR_CHANGE_PATTERN ") "));
QTC_ASSERT(changeNumRx.isValid(), return changes);
if (changeNumRx.indexIn(txt) != -1) {
changes.insert(changeNumRx.cap(1));
changeNumRx.setPattern(QLatin1String("\n(" BZR_CHANGE_PATTERN ") "));
QTC_ASSERT(changeNumRx.isValid(), return changes);
int pos = 0;
while ((pos = changeNumRx.indexIn(txt, pos)) != -1) {
pos += changeNumRx.matchedLength();
changes.insert(changeNumRx.cap(1));
}
}
return changes;
} }
QString BazaarEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const QString BazaarEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const

View File

@@ -40,7 +40,6 @@ public:
BazaarEditorWidget(); BazaarEditorWidget();
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &cursor) const override; QString changeUnderCursor(const QTextCursor &cursor) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;

View File

@@ -52,24 +52,8 @@ ClearCaseEditorWidget::ClearCaseEditorWidget() :
setDiffFilePattern(diffFilePattern); setDiffFilePattern(diffFilePattern);
setLogEntryPattern(QRegExp(QLatin1String("version \"([^\"]+)\""))); setLogEntryPattern(QRegExp(QLatin1String("version \"([^\"]+)\"")));
setAnnotateRevisionTextFormat(tr("Annotate version \"%1\"")); setAnnotateRevisionTextFormat(tr("Annotate version \"%1\""));
} setAnnotationEntryPattern("([^|]*)\\|[^\\n]*\\n");
setAnnotationSeparatorPattern("\\n-{30}");
QSet<QString> ClearCaseEditorWidget::annotationChanges() const
{
QSet<QString> changes;
QString txt = toPlainText();
if (txt.isEmpty())
return changes;
// search until header
int separator = txt.indexOf(QRegExp(QLatin1String("\n-{30}")));
QRegExp r(QLatin1String("([^|]*)\\|[^\n]*\n"));
QTC_ASSERT(r.isValid(), return changes);
int pos = r.indexIn(txt, 0);
while (pos != -1 && pos < separator) {
changes.insert(r.cap(1));
pos = r.indexIn(txt, pos + r.matchedLength());
}
return changes;
} }
QString ClearCaseEditorWidget::changeUnderCursor(const QTextCursor &c) const QString ClearCaseEditorWidget::changeUnderCursor(const QTextCursor &c) const

View File

@@ -41,7 +41,6 @@ public:
ClearCaseEditorWidget(); ClearCaseEditorWidget();
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;

View File

@@ -59,28 +59,7 @@ CvsEditorWidget::CvsEditorWidget() :
setDiffFilePattern(QRegExp(QLatin1String("^[-+]{3} ([^\\t]+)"))); setDiffFilePattern(QRegExp(QLatin1String("^[-+]{3} ([^\\t]+)")));
setLogEntryPattern(QRegExp(QLatin1String("^revision (.+)$"))); setLogEntryPattern(QRegExp(QLatin1String("^revision (.+)$")));
setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\"")); setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
} setAnnotationEntryPattern("^(" CVS_REVISION_PATTERN ") ");
QSet<QString> CvsEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString txt = toPlainText();
if (txt.isEmpty())
return changes;
// Hunt for first change number in annotation: "1.1 (author)"
QRegExp r(QLatin1String(CVS_REVISION_AT_START_PATTERN));
QTC_ASSERT(r.isValid(), return changes);
if (r.indexIn(txt) != -1) {
changes.insert(r.cap(1));
r.setPattern(QLatin1String("\n(" CVS_REVISION_PATTERN ") "));
QTC_ASSERT(r.isValid(), return changes);
int pos = 0;
while ((pos = r.indexIn(txt, pos)) != -1) {
pos += r.matchedLength();
changes.insert(r.cap(1));
}
}
return changes;
} }
QString CvsEditorWidget::changeUnderCursor(const QTextCursor &c) const QString CvsEditorWidget::changeUnderCursor(const QTextCursor &c) const

View File

@@ -40,7 +40,6 @@ public:
CvsEditorWidget(); CvsEditorWidget();
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;

View File

@@ -75,28 +75,7 @@ GitEditorWidget::GitEditorWidget() :
setLogEntryPattern(QRegExp("^commit ([0-9a-f]{8})[0-9a-f]{32}")); setLogEntryPattern(QRegExp("^commit ([0-9a-f]{8})[0-9a-f]{32}"));
setAnnotateRevisionTextFormat(tr("&Blame %1")); setAnnotateRevisionTextFormat(tr("&Blame %1"));
setAnnotatePreviousRevisionTextFormat(tr("Blame &Parent Revision %1")); setAnnotatePreviousRevisionTextFormat(tr("Blame &Parent Revision %1"));
} setAnnotationEntryPattern("^(" CHANGE_PATTERN ") ");
QSet<QString> GitEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString txt = toPlainText();
if (txt.isEmpty())
return changes;
// Hunt for first change number in annotation: "<change>:"
QRegExp r("^(" CHANGE_PATTERN ") ");
QTC_ASSERT(r.isValid(), return changes);
if (r.indexIn(txt) != -1) {
changes.insert(r.cap(1));
r.setPattern("\n(" CHANGE_PATTERN ") ");
QTC_ASSERT(r.isValid(), return changes);
int pos = 0;
while ((pos = r.indexIn(txt, pos)) != -1) {
pos += r.matchedLength();
changes.insert(r.cap(1));
}
}
return changes;
} }
QString GitEditorWidget::changeUnderCursor(const QTextCursor &c) const QString GitEditorWidget::changeUnderCursor(const QTextCursor &c) const

View File

@@ -54,7 +54,6 @@ private:
void resetChange(const QByteArray &resetType); void resetChange(const QByteArray &resetType);
void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override; void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override;
void aboutToOpen(const QString &fileName, const QString &realFileName) override; void aboutToOpen(const QString &fileName, const QString &realFileName) override;
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const override; VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(const QSet<QString> &changes) const override;
QString decorateVersion(const QString &revision) const override; QString decorateVersion(const QString &revision) const override;

View File

@@ -37,10 +37,10 @@ const char MERCURIALDEFAULT[] = "hg";
const char MERCURIAL_CONTEXT[] = "Mercurial Context"; const char MERCURIAL_CONTEXT[] = "Mercurial Context";
// Changeset identifiers // Changeset identifiers
const char CHANGESETID12[] = " ([a-f0-9]{12,12}) "; //match 12 hex chars and capture const char CHANGESETID12[] = " ([a-f0-9]{12}) "; //match 12 hex chars and capture
const char CHANGESETID40[] = " ([a-f0-9]{40,40}) "; const char CHANGESETID40[] = " ([a-f0-9]{40}) ";
const char CHANGEIDEXACT12[] = "[a-f0-9]{12,12}"; //match 12 hex chars a const char CHANGEIDEXACT12[] = "[a-f0-9]{12}"; //match 12 hex chars
const char CHANGEIDEXACT40[] = "[a-f0-9]{40,40}"; const char CHANGEIDEXACT40[] = "[a-f0-9]{40}";
// match diff header. e.g. +++ b/filename // match diff header. e.g. +++ b/filename
const char DIFFIDENTIFIER[] = "^(?:diff --git a/|[+-]{3} (?:/dev/null|[ab]/(.+$)))"; const char DIFFIDENTIFIER[] = "^(?:diff --git a/|[+-]{3} (?:/dev/null|[ab]/(.+$)))";

View File

@@ -45,7 +45,6 @@ namespace Internal {
MercurialEditorWidget::MercurialEditorWidget(MercurialClient *client) : MercurialEditorWidget::MercurialEditorWidget(MercurialClient *client) :
exactIdentifier12(QLatin1String(Constants::CHANGEIDEXACT12)), exactIdentifier12(QLatin1String(Constants::CHANGEIDEXACT12)),
exactIdentifier40(QLatin1String(Constants::CHANGEIDEXACT40)), exactIdentifier40(QLatin1String(Constants::CHANGEIDEXACT40)),
changesetIdentifier12(QLatin1String(Constants::CHANGESETID12)),
changesetIdentifier40(QLatin1String(Constants::CHANGESETID40)), changesetIdentifier40(QLatin1String(Constants::CHANGESETID40)),
m_client(client) m_client(client)
{ {
@@ -53,22 +52,7 @@ MercurialEditorWidget::MercurialEditorWidget(MercurialClient *client) :
setLogEntryPattern(QRegExp(QLatin1String("^changeset:\\s+(\\S+)$"))); setLogEntryPattern(QRegExp(QLatin1String("^changeset:\\s+(\\S+)$")));
setAnnotateRevisionTextFormat(tr("&Annotate %1")); setAnnotateRevisionTextFormat(tr("&Annotate %1"));
setAnnotatePreviousRevisionTextFormat(tr("Annotate &parent revision %1")); setAnnotatePreviousRevisionTextFormat(tr("Annotate &parent revision %1"));
} setAnnotationEntryPattern(Constants::CHANGESETID12);
QSet<QString> MercurialEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString data = toPlainText();
if (data.isEmpty())
return changes;
int position = 0;
while ((position = changesetIdentifier12.indexIn(data, position)) != -1) {
changes.insert(changesetIdentifier12.cap(1));
position += changesetIdentifier12.matchedLength();
}
return changes;
} }
QString MercurialEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const QString MercurialEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const

View File

@@ -41,7 +41,6 @@ public:
explicit MercurialEditorWidget(MercurialClient *client); explicit MercurialEditorWidget(MercurialClient *client);
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &cursor) const override; QString changeUnderCursor(const QTextCursor &cursor) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;
@@ -50,7 +49,6 @@ private:
mutable QRegExp exactIdentifier12; mutable QRegExp exactIdentifier12;
mutable QRegExp exactIdentifier40; mutable QRegExp exactIdentifier40;
mutable QRegExp changesetIdentifier12;
const QRegExp changesetIdentifier40; const QRegExp changesetIdentifier40;
MercurialClient *m_client; MercurialClient *m_client;

View File

@@ -63,28 +63,7 @@ PerforceEditorWidget::PerforceEditorWidget() :
setDiffFilePattern(QRegExp(QLatin1String("^(?:={4}|\\+{3}) (.+)(?:\\t|#\\d)"))); setDiffFilePattern(QRegExp(QLatin1String("^(?:={4}|\\+{3}) (.+)(?:\\t|#\\d)")));
setLogEntryPattern(QRegExp(QLatin1String("^... #\\d change (\\d+) "))); setLogEntryPattern(QRegExp(QLatin1String("^... #\\d change (\\d+) ")));
setAnnotateRevisionTextFormat(tr("Annotate change list \"%1\"")); setAnnotateRevisionTextFormat(tr("Annotate change list \"%1\""));
} setAnnotationEntryPattern("^(\\d+):");
QSet<QString> PerforceEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString txt = toPlainText();
if (txt.isEmpty())
return changes;
// Hunt for first change number in annotation: "<change>:"
QRegExp r(QLatin1String("^(\\d+):"));
QTC_ASSERT(r.isValid(), return changes);
if (r.indexIn(txt) != -1) {
changes.insert(r.cap(1));
r.setPattern(QLatin1String("\n(\\d+):"));
QTC_ASSERT(r.isValid(), return changes);
int pos = 0;
while ((pos = r.indexIn(txt, pos)) != -1) {
pos += r.matchedLength();
changes.insert(r.cap(1));
}
}
return changes;
} }
QString PerforceEditorWidget::changeUnderCursor(const QTextCursor &c) const QString PerforceEditorWidget::changeUnderCursor(const QTextCursor &c) const

View File

@@ -40,7 +40,6 @@ public:
PerforceEditorWidget(); PerforceEditorWidget();
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;

View File

@@ -59,30 +59,7 @@ SubversionEditorWidget::SubversionEditorWidget() :
setDiffFilePattern(QRegExp(QLatin1String("^[-+]{3} ([^\\t]+)|^Index: .*|^=+$"))); setDiffFilePattern(QRegExp(QLatin1String("^[-+]{3} ([^\\t]+)|^Index: .*|^=+$")));
setLogEntryPattern(QRegExp(QLatin1String("^(r\\d+) \\|"))); setLogEntryPattern(QRegExp(QLatin1String("^(r\\d+) \\|")));
setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\"")); setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
} setAnnotationEntryPattern("^(\\d+):");
QSet<QString> SubversionEditorWidget::annotationChanges() const
{
QSet<QString> changes;
const QString txt = toPlainText();
if (txt.isEmpty())
return changes;
// Hunt for first change number in annotation: "<change>:"
QRegExp r(QLatin1String("^(\\d+):"));
QTC_ASSERT(r.isValid(), return changes);
if (r.indexIn(txt) != -1) {
changes.insert(r.cap(1));
r.setPattern(QLatin1String("\n(\\d+):"));
QTC_ASSERT(r.isValid(), return changes);
int pos = 0;
while ((pos = r.indexIn(txt, pos)) != -1) {
pos += r.matchedLength();
changes.insert(r.cap(1));
}
}
if (Subversion::Constants::debug)
qDebug() << "SubversionEditor::annotationChanges() returns #" << changes.size();
return changes;
} }
QString SubversionEditorWidget::changeUnderCursor(const QTextCursor &c) const QString SubversionEditorWidget::changeUnderCursor(const QTextCursor &c) const

View File

@@ -40,7 +40,6 @@ public:
SubversionEditorWidget(); SubversionEditorWidget();
private: private:
QSet<QString> annotationChanges() const override;
QString changeUnderCursor(const QTextCursor &) const override; QString changeUnderCursor(const QTextCursor &) const override;
VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter( VcsBase::BaseAnnotationHighlighter *createAnnotationHighlighter(
const QSet<QString> &changes) const override; const QSet<QString> &changes) const override;

View File

@@ -50,6 +50,7 @@
#include <QDebug> #include <QDebug>
#include <QFileInfo> #include <QFileInfo>
#include <QFile> #include <QFile>
#include <QRegularExpression>
#include <QRegExp> #include <QRegExp>
#include <QSet> #include <QSet>
#include <QTextCodec> #include <QTextCodec>
@@ -556,6 +557,8 @@ public:
QRegExp m_diffFilePattern; QRegExp m_diffFilePattern;
QRegExp m_logEntryPattern; QRegExp m_logEntryPattern;
QRegularExpression m_annotationEntryPattern;
QRegularExpression m_annotationSeparatorPattern;
QList<int> m_entrySections; // line number where this section starts QList<int> m_entrySections; // line number where this section starts
int m_cursorLine = -1; int m_cursorLine = -1;
int m_firstLineNumber = -1; int m_firstLineNumber = -1;
@@ -661,6 +664,20 @@ void VcsBaseEditorWidget::setLogEntryPattern(const QRegExp &pattern)
d->m_logEntryPattern = pattern; d->m_logEntryPattern = pattern;
} }
void VcsBaseEditorWidget::setAnnotationEntryPattern(const QString &pattern)
{
const QRegularExpression re(pattern, QRegularExpression::MultilineOption);
QTC_ASSERT(re.isValid() && re.captureCount() >= 1, return);
d->m_annotationEntryPattern = re;
}
void VcsBaseEditorWidget::setAnnotationSeparatorPattern(const QString &pattern)
{
const QRegularExpression re(pattern);
QTC_ASSERT(re.isValid() && re.captureCount() >= 1, return);
d->m_annotationSeparatorPattern = re;
}
bool VcsBaseEditorWidget::supportChangeLinks() const bool VcsBaseEditorWidget::supportChangeLinks() const
{ {
switch (d->m_parameters->type) { switch (d->m_parameters->type) {
@@ -1537,6 +1554,26 @@ void VcsBaseEditorWidget::addChangeActions(QMenu *, const QString &)
{ {
} }
QSet<QString> VcsBaseEditorWidget::annotationChanges() const
{
QSet<QString> changes;
QString text = toPlainText();
QStringRef txt(&text);
if (txt.isEmpty())
return changes;
if (d->m_annotationSeparatorPattern.isValid()) {
const QRegularExpressionMatch match = d->m_annotationSeparatorPattern.match(txt);
if (match.hasMatch())
txt.truncate(match.capturedStart());
}
QRegularExpressionMatchIterator i = d->m_annotationEntryPattern.globalMatch(txt);
while (i.hasNext()) {
const QRegularExpressionMatch match = i.next();
changes.insert(match.captured(1));
}
return changes;
}
QString VcsBaseEditorWidget::decorateVersion(const QString &revision) const QString VcsBaseEditorWidget::decorateVersion(const QString &revision) const
{ {
return revision; return revision;

View File

@@ -148,6 +148,10 @@ protected:
void setDiffFilePattern(const QRegExp &pattern); void setDiffFilePattern(const QRegExp &pattern);
// Pattern for log entry. hash/revision number must be in the first capture group // Pattern for log entry. hash/revision number must be in the first capture group
void setLogEntryPattern(const QRegExp &pattern); void setLogEntryPattern(const QRegExp &pattern);
// Pattern for annotation entry. hash/revision number must be in the first capture group
void setAnnotationEntryPattern(const QString &pattern);
// Pattern for annotation separator. Lookup will stop on match.
void setAnnotationSeparatorPattern(const QString &pattern);
virtual bool supportChangeLinks() const; virtual bool supportChangeLinks() const;
virtual QString fileNameForLine(int line) const; virtual QString fileNameForLine(int line) const;
@@ -246,7 +250,7 @@ protected:
// Implement to return a set of change identifiers in // Implement to return a set of change identifiers in
// annotation mode // annotation mode
virtual QSet<QString> annotationChanges() const = 0; QSet<QString> annotationChanges() const;
// Implement to identify a change number at the cursor position // Implement to identify a change number at the cursor position
virtual QString changeUnderCursor(const QTextCursor &) const = 0; virtual QString changeUnderCursor(const QTextCursor &) const = 0;
// Factory functions for highlighters // Factory functions for highlighters