bazaareditor.cpp 4.43 KB
Newer Older
cerf's avatar
cerf committed
1 2
/**************************************************************************
**
Eike Ziller's avatar
Eike Ziller committed
3 4
** Copyright (C) 2015 Hugues Delorme
** Contact: http://www.qt.io/licensing
cerf's avatar
cerf committed
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
cerf's avatar
cerf committed
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.
cerf's avatar
cerf committed
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
cerf's avatar
cerf committed
27 28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
cerf's avatar
cerf committed
30 31 32 33
#include "bazaareditor.h"
#include "annotationhighlighter.h"
#include "constants.h"

34
#include <utils/qtcassert.h>
cerf's avatar
cerf committed
35

36 37 38
#include <QRegExp>
#include <QString>
#include <QTextCursor>
cerf's avatar
cerf committed
39

40
#define BZR_CHANGE_PATTERN "[0-9]+"
41

cerf's avatar
cerf committed
42 43 44
using namespace Bazaar::Internal;
using namespace Bazaar;

45
BazaarEditorWidget::BazaarEditorWidget() :
46
      m_changesetId(QLatin1String(Constants::CHANGESET_ID)),
47
      m_exactChangesetId(QLatin1String(Constants::CHANGESET_ID_EXACT))
cerf's avatar
cerf committed
48
{
49 50
    setAnnotateRevisionTextFormat(tr("&Annotate %1"));
    setAnnotatePreviousRevisionTextFormat(tr("Annotate &parent revision %1"));
51 52 53
    // Diff format:
    // === <change> <file|dir> 'mainwindow.cpp'
    setDiffFilePattern(QRegExp(QLatin1String("^=== [a-z]+ [a-z]+ '(.+)'\\s*")));
54
    setLogEntryPattern(QRegExp(QLatin1String("^revno: (\\d+)")));
cerf's avatar
cerf committed
55 56
}

57
QSet<QString> BazaarEditorWidget::annotationChanges() const
cerf's avatar
cerf committed
58 59
{
    QSet<QString> changes;
60 61
    const QString txt = toPlainText();
    if (txt.isEmpty())
cerf's avatar
cerf committed
62 63
        return changes;

hjk's avatar
hjk committed
64
    QRegExp changeNumRx(QLatin1String("^(" BZR_CHANGE_PATTERN ") "));
65 66 67
    QTC_ASSERT(changeNumRx.isValid(), return changes);
    if (changeNumRx.indexIn(txt) != -1) {
        changes.insert(changeNumRx.cap(1));
hjk's avatar
hjk committed
68
        changeNumRx.setPattern(QLatin1String("\n(" BZR_CHANGE_PATTERN ") "));
69 70 71 72 73 74
        QTC_ASSERT(changeNumRx.isValid(), return changes);
        int pos = 0;
        while ((pos = changeNumRx.indexIn(txt, pos)) != -1) {
            pos += changeNumRx.matchedLength();
            changes.insert(changeNumRx.cap(1));
        }
cerf's avatar
cerf committed
75 76 77 78
    }
    return changes;
}

79
QString BazaarEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const
cerf's avatar
cerf committed
80
{
81 82 83 84 85 86
    // The test is done in two steps: first we check if the line contains a
    // changesetId. Then we check if the cursor is over the changesetId itself
    // and not over "revno" or another part of the line.
    // The two steps are necessary because matching only for the changesetId
    // leads to many false-positives (a regex like "[0-9]+" matches a lot of text).
    const int cursorCol = cursorIn.columnNumber();
cerf's avatar
cerf committed
87
    QTextCursor cursor = cursorIn;
88
    cursor.select(QTextCursor::LineUnderCursor);
cerf's avatar
cerf committed
89
    if (cursor.hasSelection()) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
        const QString line = cursor.selectedText();
        const int start = m_changesetId.indexIn(line);
        if (start > -1) {
            const QString match = m_changesetId.cap(0);
            const int stop = start + match.length();
            if (start <= cursorCol && cursorCol <= stop) {
                cursor = cursorIn;
                cursor.select(QTextCursor::WordUnderCursor);
                if (cursor.hasSelection()) {
                    const QString change = cursor.selectedText();
                    if (m_exactChangesetId.exactMatch(change))
                        return change;
                }
            }
        }
cerf's avatar
cerf committed
105 106 107 108
    }
    return QString();
}

109
VcsBase::BaseAnnotationHighlighter *BazaarEditorWidget::createAnnotationHighlighter(const QSet<QString> &changes) const
cerf's avatar
cerf committed
110
{
111
    return new BazaarAnnotationHighlighter(changes);
cerf's avatar
cerf committed
112
}