1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "subversioneditor.h"
27 #include "subversionplugin.h"
28 
29 #include "annotationhighlighter.h"
30 #include "subversionconstants.h"
31 
32 #include <utils/qtcassert.h>
33 #include <vcsbase/diffandloghighlighter.h>
34 
35 #include <QDebug>
36 #include <QFileInfo>
37 #include <QTextCursor>
38 #include <QTextBlock>
39 
40 using namespace Subversion;
41 using namespace Subversion::Internal;
42 
SubversionEditorWidget()43 SubversionEditorWidget::SubversionEditorWidget() :
44     m_changeNumberPattern("^\\s*(?<area>(?<rev>\\d+))\\s+.*$"),
45     m_revisionNumberPattern("\\b(?<area>(r|[rR]evision )(?<rev>\\d+))\\b")
46 {
47     QTC_ASSERT(m_changeNumberPattern.isValid(), return);
48     QTC_ASSERT(m_revisionNumberPattern.isValid(), return);
49     /* Diff pattern:
50     \code
51         Index: main.cpp
52     ===================================================================
53     --- main.cpp<tab>(revision 2)
54     +++ main.cpp<tab>(working copy)
55     @@ -6,6 +6,5 @@
56     \endcode
57     */
58     setDiffFilePattern("^[-+]{3} ([^\\t]+)|^Index: .*|^=+$");
59     setLogEntryPattern("^(r\\d+) \\|");
60     setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
61     setAnnotationEntryPattern("^(\\d+):");
62 }
63 
changeUnderCursor(const QTextCursor & c) const64 QString SubversionEditorWidget::changeUnderCursor(const QTextCursor &c) const
65 {
66     QTextCursor cursor = c;
67     // Any number is regarded as change number.
68     cursor.select(QTextCursor::LineUnderCursor);
69     if (!cursor.hasSelection())
70         return QString();
71     QString change = cursor.selectedText();
72     const int pos = c.position() - cursor.selectionStart() + 1;
73     // Annotation output has number, log output has revision numbers,
74     // both at the start of the line.
75     auto matchIter = m_changeNumberPattern.globalMatch(change);
76     if (!matchIter.hasNext())
77         matchIter = m_revisionNumberPattern.globalMatch(change);
78 
79     // We may have several matches of our regexp and we way have
80     // several () in the regexp
81     const QString areaName = QLatin1String("area");
82     while (matchIter.hasNext()) {
83         auto match = matchIter.next();
84         const QString rev = match.captured(QLatin1String("rev"));
85         if (rev.isEmpty())
86             continue;
87 
88         const QString area = match.captured(areaName);
89         QTC_ASSERT(area.contains(rev), continue);
90 
91         const int start = match.capturedStart(areaName);
92         const int end = match.capturedEnd(areaName);
93         if (pos > start && pos <= end)
94             return rev;
95     }
96     return QString();
97 }
98 
createAnnotationHighlighter(const QSet<QString> & changes) const99 VcsBase::BaseAnnotationHighlighter *SubversionEditorWidget::createAnnotationHighlighter(const QSet<QString> &changes) const
100 {
101     return new SubversionAnnotationHighlighter(changes);
102 }
103 
annotationPreviousVersions(const QString & v) const104 QStringList SubversionEditorWidget::annotationPreviousVersions(const QString &v) const
105 {
106     bool ok;
107     const int revision = v.toInt(&ok);
108     if (!ok || revision < 2)
109         return QStringList();
110     return QStringList(QString::number(revision - 1));
111 }
112