cvseditor.cpp 5.04 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8 9 10 11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12 13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15 16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18 19 20 21 22 23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25 26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30 31

#include "cvseditor.h"
32
#include "cvsutils.h"
33 34 35 36 37

#include "annotationhighlighter.h"
#include "cvsconstants.h"

#include <utils/qtcassert.h>
38
#include <vcsbase/diffandloghighlighter.h>
39

40 41 42
#include <QDebug>
#include <QTextCursor>
#include <QTextBlock>
43

hjk's avatar
hjk committed
44
namespace Cvs {
45 46 47 48
namespace Internal {

// Match a CVS revision ("1.1.1.1")
#define CVS_REVISION_PATTERN "[\\d\\.]+"
49
#define CVS_REVISION_AT_START_PATTERN "^(" CVS_REVISION_PATTERN ") "
50

51
CvsEditorWidget::CvsEditorWidget() :
52
    m_revisionAnnotationPattern(QLatin1String(CVS_REVISION_AT_START_PATTERN ".*$")),
53
    m_revisionLogPattern(QLatin1String("^revision  *(" CVS_REVISION_PATTERN ")$"))
54
{
55 56
    QTC_ASSERT(m_revisionAnnotationPattern.isValid(), return);
    QTC_ASSERT(m_revisionLogPattern.isValid(), return);
57 58 59 60 61 62 63 64 65
    /* Diff format:
    \code
    cvs diff -d -u -r1.1 -r1.2:
    --- mainwindow.cpp<\t>13 Jul 2009 13:50:15 -0000<tab>1.1
    +++ mainwindow.cpp<\t>14 Jul 2009 07:09:24 -0000<tab>1.2
    @@ -6,6 +6,5 @@
    \endcode
    */
    setDiffFilePattern(QRegExp(QLatin1String("^[-+]{3} ([^\\t]+)")));
66
    setLogEntryPattern(QRegExp(QLatin1String("^revision (.+)$")));
67
    setAnnotateRevisionTextFormat(tr("Annotate revision \"%1\""));
68 69
}

70
QSet<QString> CvsEditorWidget::annotationChanges() const
71 72 73 74 75 76 77 78 79 80
{
    QSet<QString> changes;
    const QString txt = toPlainText();
    if (txt.isEmpty())
        return changes;
    // Hunt for first change number in annotation: "1.1 (author)"
    QRegExp r(QLatin1String(CVS_REVISION_AT_START_PATTERN));
    QTC_ASSERT(r.isValid(), return changes);
    if (r.indexIn(txt) != -1) {
        changes.insert(r.cap(1));
81
        r.setPattern(QLatin1String("\n(" CVS_REVISION_PATTERN ") "));
82 83 84 85 86 87 88
        QTC_ASSERT(r.isValid(), return changes);
        int pos = 0;
        while ((pos = r.indexIn(txt, pos)) != -1) {
            pos += r.matchedLength();
            changes.insert(r.cap(1));
        }
    }
89
    if (Constants::debug)
90 91 92 93
        qDebug() << "CVSEditor::annotationChanges() returns #" << changes.size();
    return changes;
}

94
QString CvsEditorWidget::changeUnderCursor(const QTextCursor &c) const
95
{
96 97 98 99 100 101 102
    // Try to match "1.1" strictly:
    // 1) Annotation: Check for a revision number at the beginning of the line.
    //    Note that "cursor.select(QTextCursor::WordUnderCursor)" will
    //    only select the part up until the dot.
    //    Check if we are at the beginning of a line within a reasonable offset.
    // 2) Log: check for lines like "revision 1.1", cursor past "revision"
    switch (contentType()) {
103
    case VcsBase::OtherContent:
hjk's avatar
hjk committed
104
    case VcsBase::DiffOutput:
105
        break;
hjk's avatar
hjk committed
106
    case VcsBase::AnnotateOutput: {
107 108 109 110 111 112 113 114
            const QTextBlock block = c.block();
            if (c.atBlockStart() || (c.position() - block.position() < 3)) {
                const QString line = block.text();
                if (m_revisionAnnotationPattern.exactMatch(line))
                    return m_revisionAnnotationPattern.cap(1);
            }
        }
        break;
hjk's avatar
hjk committed
115
    case VcsBase::LogOutput: {
116 117 118 119 120
            const QTextBlock block = c.block();
            if (c.position() - block.position() > 8 && m_revisionLogPattern.exactMatch(block.text()))
                return m_revisionLogPattern.cap(1);
        }
        break;
121 122 123 124
    }
    return QString();
}

125
VcsBase::BaseAnnotationHighlighter *CvsEditorWidget::createAnnotationHighlighter(const QSet<QString> &changes) const
126
{
127
    return new CvsAnnotationHighlighter(changes);
128 129
}

130
QStringList CvsEditorWidget::annotationPreviousVersions(const QString &revision) const
131 132 133 134 135 136
{
    if (isFirstRevision(revision))
        return QStringList();
    return QStringList(previousRevision(revision));
}

137 138
}
}