1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
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
16 ** GNU Lesser General Public License Usage
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.
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.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #include "cvseditor.h"
37 #include "annotationhighlighter.h"
38 #include "cvsconstants.h"
40 #include <utils/qtcassert.h>
41 #include <vcsbase/diffhighlighter.h>
43 #include <QtCore/QDebug>
44 #include <QtGui/QTextCursor>
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") "
53 CVSEditor::CVSEditor(const VCSBase::VCSBaseEditorParameters *type,
55 VCSBase::VCSBaseEditor(type, parent),
56 m_revisionAnnotationPattern(QLatin1String(CVS_REVISION_AT_START_PATTERN".*$")),
57 m_revisionLogPattern(QLatin1String("^revision *("CVS_REVISION_PATTERN")$"))
59 QTC_ASSERT(m_revisionAnnotationPattern.isValid(), return);
60 QTC_ASSERT(m_revisionLogPattern.isValid(), return);
61 setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
64 QSet<QString> CVSEditor::annotationChanges() const
66 QSet<QString> changes;
67 const QString txt = toPlainText();
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);
78 while ((pos = r.indexIn(txt, pos)) != -1) {
79 pos += r.matchedLength();
80 changes.insert(r.cap(1));
83 if (CVS::Constants::debug)
84 qDebug() << "CVSEditor::annotationChanges() returns #" << changes.size();
88 QString CVSEditor::changeUnderCursor(const QTextCursor &c) const
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:
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);
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);
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
127 VCSBase::DiffHighlighter *CVSEditor::createDiffHighlighter() const
129 const QRegExp filePattern(QLatin1String("^[-+][-+][-+] .*1\\.[\\d\\.]+$"));
130 QTC_ASSERT(filePattern.isValid(), /**/);
131 return new VCSBase::DiffHighlighter(filePattern);
134 VCSBase::BaseAnnotationHighlighter *CVSEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
136 return new CVSAnnotationHighlighter(changes);
139 QString CVSEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
141 // "+++ mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000 1.1"
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'));
150 diffFileName.truncate(tabIndex);
151 return findDiffFile(diffFileName);
157 QStringList CVSEditor::annotationPreviousVersions(const QString &revision) const
159 if (isFirstRevision(revision))
160 return QStringList();
161 return QStringList(previousRevision(revision));