OSDN Git Service

74bca3afc22385ee4c860704f17c114eac185804
[qt-creator-jp/qt-creator-jp.git] / src / plugins / cvs / cvseditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "cvseditor.h"
35 #include "cvsutils.h"
36
37 #include "annotationhighlighter.h"
38 #include "cvsconstants.h"
39
40 #include <utils/qtcassert.h>
41 #include <vcsbase/diffhighlighter.h>
42
43 #include <QtCore/QDebug>
44 #include <QtGui/QTextCursor>
45 #include <QtGui/QTextBlock>
46
47 namespace CVS {
48 namespace Internal {
49
50 // Match a CVS revision ("1.1.1.1")
51 #define CVS_REVISION_PATTERN "[\\d\\.]+"
52 #define CVS_REVISION_AT_START_PATTERN "^("CVS_REVISION_PATTERN") "
53
54 CVSEditor::CVSEditor(const VCSBase::VCSBaseEditorParameters *type,
55                                    QWidget *parent) :
56     VCSBase::VCSBaseEditorWidget(type, parent),
57     m_revisionAnnotationPattern(QLatin1String(CVS_REVISION_AT_START_PATTERN".*$")),
58     m_revisionLogPattern(QLatin1String("^revision  *("CVS_REVISION_PATTERN")$"))
59 {
60     QTC_ASSERT(m_revisionAnnotationPattern.isValid(), return);
61     QTC_ASSERT(m_revisionLogPattern.isValid(), return);
62     setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
63 }
64
65 QSet<QString> CVSEditor::annotationChanges() const
66 {
67     QSet<QString> changes;
68     const QString txt = toPlainText();
69     if (txt.isEmpty())
70         return changes;
71     // Hunt for first change number in annotation: "1.1 (author)"
72     QRegExp r(QLatin1String(CVS_REVISION_AT_START_PATTERN));
73     QTC_ASSERT(r.isValid(), return changes);
74     if (r.indexIn(txt) != -1) {
75         changes.insert(r.cap(1));
76         r.setPattern(QLatin1String("\n("CVS_REVISION_PATTERN") "));
77         QTC_ASSERT(r.isValid(), return changes);
78         int pos = 0;
79         while ((pos = r.indexIn(txt, pos)) != -1) {
80             pos += r.matchedLength();
81             changes.insert(r.cap(1));
82         }
83     }
84     if (CVS::Constants::debug)
85         qDebug() << "CVSEditor::annotationChanges() returns #" << changes.size();
86     return changes;
87 }
88
89 QString CVSEditor::changeUnderCursor(const QTextCursor &c) const
90 {
91     // Try to match "1.1" strictly:
92     // 1) Annotation: Check for a revision number at the beginning of the line.
93     //    Note that "cursor.select(QTextCursor::WordUnderCursor)" will
94     //    only select the part up until the dot.
95     //    Check if we are at the beginning of a line within a reasonable offset.
96     // 2) Log: check for lines like "revision 1.1", cursor past "revision"
97     switch (contentType()) {
98     case VCSBase::RegularCommandOutput:
99     case VCSBase::DiffOutput:
100         break;
101     case VCSBase::AnnotateOutput: {
102             const QTextBlock block = c.block();
103             if (c.atBlockStart() || (c.position() - block.position() < 3)) {
104                 const QString line = block.text();
105                 if (m_revisionAnnotationPattern.exactMatch(line))
106                     return m_revisionAnnotationPattern.cap(1);
107             }
108         }
109         break;
110     case VCSBase::LogOutput: {
111             const QTextBlock block = c.block();
112             if (c.position() - block.position() > 8 && m_revisionLogPattern.exactMatch(block.text()))
113                 return m_revisionLogPattern.cap(1);
114         }
115         break;
116     }
117     return QString();
118 }
119
120 /* \code
121 cvs diff -d -u -r1.1 -r1.2:
122 --- mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000 <\t>1.1
123 +++ mainwindow.cpp<\t>14 Jul 2009 07:09:24 -0000<\t>1.2
124 @@ -6,6 +6,5 @@
125 \endcode
126 */
127
128 VCSBase::DiffHighlighter *CVSEditor::createDiffHighlighter() const
129 {
130     const QRegExp filePattern(QLatin1String("^[-+][-+][-+] .*1\\.[\\d\\.]+$"));
131     QTC_ASSERT(filePattern.isValid(), /**/);
132     return new VCSBase::DiffHighlighter(filePattern);
133 }
134
135 VCSBase::BaseAnnotationHighlighter *CVSEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
136 {
137     return new CVSAnnotationHighlighter(changes);
138 }
139
140 QString CVSEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
141 {
142     // "+++ mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000      1.1"
143     // Go back chunks
144     const QString diffIndicator = QLatin1String("+++ ");
145     for (QTextBlock  block = inBlock; block.isValid() ; block = block.previous()) {
146         QString diffFileName = block.text();
147         if (diffFileName.startsWith(diffIndicator)) {
148             diffFileName.remove(0, diffIndicator.size());
149             const int tabIndex = diffFileName.indexOf(QLatin1Char('\t'));
150             if (tabIndex != -1)
151                 diffFileName.truncate(tabIndex);
152             return findDiffFile(diffFileName);
153         }
154     }
155     return QString();
156 }
157
158 QStringList CVSEditor::annotationPreviousVersions(const QString &revision) const
159 {
160     if (isFirstRevision(revision))
161         return QStringList();
162     return QStringList(previousRevision(revision));
163 }
164
165 }
166 }