OSDN Git Service

It's 2011 now.
[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
46 namespace CVS {
47 namespace Internal {
48
49 // Match a CVS revision ("1.1.1.1")
50 #define CVS_REVISION_PATTERN "[\\d\\.]+"
51 #define CVS_REVISION_AT_START_PATTERN "^("CVS_REVISION_PATTERN") "
52
53 CVSEditor::CVSEditor(const VCSBase::VCSBaseEditorParameters *type,
54                                    QWidget *parent) :
55     VCSBase::VCSBaseEditor(type, parent),
56     m_revisionAnnotationPattern(QLatin1String(CVS_REVISION_AT_START_PATTERN".*$")),
57     m_revisionLogPattern(QLatin1String("^revision  *("CVS_REVISION_PATTERN")$"))
58 {
59     QTC_ASSERT(m_revisionAnnotationPattern.isValid(), return);
60     QTC_ASSERT(m_revisionLogPattern.isValid(), return);
61     setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
62 }
63
64 QSet<QString> CVSEditor::annotationChanges() const
65 {
66     QSet<QString> changes;
67     const QString txt = toPlainText();
68     if (txt.isEmpty())
69         return changes;
70     // Hunt for first change number in annotation: "1.1 (author)"
71     QRegExp r(QLatin1String(CVS_REVISION_AT_START_PATTERN));
72     QTC_ASSERT(r.isValid(), return changes);
73     if (r.indexIn(txt) != -1) {
74         changes.insert(r.cap(1));
75         r.setPattern(QLatin1String("\n("CVS_REVISION_PATTERN") "));
76         QTC_ASSERT(r.isValid(), return changes);
77         int pos = 0;
78         while ((pos = r.indexIn(txt, pos)) != -1) {
79             pos += r.matchedLength();
80             changes.insert(r.cap(1));
81         }
82     }
83     if (CVS::Constants::debug)
84         qDebug() << "CVSEditor::annotationChanges() returns #" << changes.size();
85     return changes;
86 }
87
88 QString CVSEditor::changeUnderCursor(const QTextCursor &c) const
89 {
90     // Try to match "1.1" strictly:
91     // 1) Annotation: Check for a revision number at the beginning of the line.
92     //    Note that "cursor.select(QTextCursor::WordUnderCursor)" will
93     //    only select the part up until the dot.
94     //    Check if we are at the beginning of a line within a reasonable offset.
95     // 2) Log: check for lines like "revision 1.1", cursor past "revision"
96     switch (contentType()) {
97     case VCSBase::RegularCommandOutput:
98     case VCSBase::DiffOutput:
99         break;
100     case VCSBase::AnnotateOutput: {
101             const QTextBlock block = c.block();
102             if (c.atBlockStart() || (c.position() - block.position() < 3)) {
103                 const QString line = block.text();
104                 if (m_revisionAnnotationPattern.exactMatch(line))
105                     return m_revisionAnnotationPattern.cap(1);
106             }
107         }
108         break;
109     case VCSBase::LogOutput: {
110             const QTextBlock block = c.block();
111             if (c.position() - block.position() > 8 && m_revisionLogPattern.exactMatch(block.text()))
112                 return m_revisionLogPattern.cap(1);
113         }
114         break;
115     }
116     return QString();
117 }
118
119 /* \code
120 cvs diff -d -u -r1.1 -r1.2:
121 --- mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000 <\t>1.1
122 +++ mainwindow.cpp<\t>14 Jul 2009 07:09:24 -0000<\t>1.2
123 @@ -6,6 +6,5 @@
124 \endcode
125 */
126
127 VCSBase::DiffHighlighter *CVSEditor::createDiffHighlighter() const
128 {
129     const QRegExp filePattern(QLatin1String("^[-+][-+][-+] .*1\\.[\\d\\.]+$"));
130     QTC_ASSERT(filePattern.isValid(), /**/);
131     return new VCSBase::DiffHighlighter(filePattern);
132 }
133
134 VCSBase::BaseAnnotationHighlighter *CVSEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
135 {
136     return new CVSAnnotationHighlighter(changes);
137 }
138
139 QString CVSEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
140 {
141     // "+++ mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000      1.1"
142     // Go back chunks
143     const QString diffIndicator = QLatin1String("+++ ");
144     for (QTextBlock  block = inBlock; block.isValid() ; block = block.previous()) {
145         QString diffFileName = block.text();
146         if (diffFileName.startsWith(diffIndicator)) {
147             diffFileName.remove(0, diffIndicator.size());
148             const int tabIndex = diffFileName.indexOf(QLatin1Char('\t'));
149             if (tabIndex != -1)
150                 diffFileName.truncate(tabIndex);
151             return findDiffFile(diffFileName);
152         }
153     }
154     return QString();
155 }
156
157 QStringList CVSEditor::annotationPreviousVersions(const QString &revision) const
158 {
159     if (isFirstRevision(revision))
160         return QStringList();
161     return QStringList(previousRevision(revision));
162 }
163
164 }
165 }