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>
45 #include <QtGui/QTextBlock>
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") "
54 CVSEditor::CVSEditor(const VCSBase::VCSBaseEditorParameters *type,
56 VCSBase::VCSBaseEditorWidget(type, parent),
57 m_revisionAnnotationPattern(QLatin1String(CVS_REVISION_AT_START_PATTERN".*$")),
58 m_revisionLogPattern(QLatin1String("^revision *("CVS_REVISION_PATTERN")$"))
60 QTC_ASSERT(m_revisionAnnotationPattern.isValid(), return);
61 QTC_ASSERT(m_revisionLogPattern.isValid(), return);
62 setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
65 QSet<QString> CVSEditor::annotationChanges() const
67 QSet<QString> changes;
68 const QString txt = toPlainText();
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);
79 while ((pos = r.indexIn(txt, pos)) != -1) {
80 pos += r.matchedLength();
81 changes.insert(r.cap(1));
84 if (CVS::Constants::debug)
85 qDebug() << "CVSEditor::annotationChanges() returns #" << changes.size();
89 QString CVSEditor::changeUnderCursor(const QTextCursor &c) const
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:
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);
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);
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
128 VCSBase::DiffHighlighter *CVSEditor::createDiffHighlighter() const
130 const QRegExp filePattern(QLatin1String("^[-+][-+][-+] .*1\\.[\\d\\.]+$"));
131 QTC_ASSERT(filePattern.isValid(), /**/);
132 return new VCSBase::DiffHighlighter(filePattern);
135 VCSBase::BaseAnnotationHighlighter *CVSEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
137 return new CVSAnnotationHighlighter(changes);
140 QString CVSEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
142 // "+++ mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000 1.1"
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'));
151 diffFileName.truncate(tabIndex);
152 return findDiffFile(diffFileName);
158 QStringList CVSEditor::annotationPreviousVersions(const QString &revision) const
160 if (isFirstRevision(revision))
161 return QStringList();
162 return QStringList(previousRevision(revision));