1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 Hugues Delorme
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of Qt Creator.
7 **
8 ** Commercial License Usage
9 ** Licensees holding valid commercial Qt licenses may use this file in
10 ** accordance with the commercial license agreement provided with the
11 ** Software or, alternatively, in accordance with the terms contained in
12 ** a written agreement between you and The Qt Company. For licensing terms
13 ** and conditions see https://www.qt.io/terms-conditions. For further
14 ** information use the contact form at https://www.qt.io/contact-us.
15 **
16 ** GNU General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU
18 ** General Public License version 3 as published by the Free Software
19 ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
20 ** included in the packaging of this file. Please review the following
21 ** information to ensure the GNU General Public License requirements will
22 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
23 **
24 ****************************************************************************/
25 
26 #include "bazaareditor.h"
27 #include "annotationhighlighter.h"
28 #include "constants.h"
29 
30 #include <utils/qtcassert.h>
31 
32 #include <QString>
33 #include <QTextCursor>
34 
35 #define BZR_CHANGE_PATTERN "[0-9]+"
36 
37 using namespace Bazaar::Internal;
38 using namespace Bazaar;
39 
BazaarEditorWidget()40 BazaarEditorWidget::BazaarEditorWidget() :
41     m_changesetId(QLatin1String(Constants::CHANGESET_ID)),
42     m_exactChangesetId(QLatin1String(Constants::CHANGESET_ID_EXACT))
43 {
44     setAnnotateRevisionTextFormat(tr("&Annotate %1"));
45     setAnnotatePreviousRevisionTextFormat(tr("Annotate &parent revision %1"));
46     // Diff format:
47     // === <change> <file|dir> 'mainwindow.cpp'
48     setDiffFilePattern("^=== [a-z]+ [a-z]+ '(.+)'\\s*");
49     setLogEntryPattern("^revno: (\\d+)");
50     setAnnotationEntryPattern("^(" BZR_CHANGE_PATTERN ") ");
51 }
52 
changeUnderCursor(const QTextCursor & cursorIn) const53 QString BazaarEditorWidget::changeUnderCursor(const QTextCursor &cursorIn) const
54 {
55     // The test is done in two steps: first we check if the line contains a
56     // changesetId. Then we check if the cursor is over the changesetId itself
57     // and not over "revno" or another part of the line.
58     // The two steps are necessary because matching only for the changesetId
59     // leads to many false-positives (a regex like "[0-9]+" matches a lot of text).
60     const int cursorCol = cursorIn.columnNumber();
61     QTextCursor cursor = cursorIn;
62     cursor.select(QTextCursor::LineUnderCursor);
63     if (cursor.hasSelection()) {
64         const QString line = cursor.selectedText();
65         const QRegularExpressionMatch match = m_changesetId.match(line);
66         if (match.hasMatch()) {
67             const int start = match.capturedStart();
68             const int stop = match.capturedEnd();
69             if (start <= cursorCol && cursorCol <= stop) {
70                 cursor = cursorIn;
71                 cursor.select(QTextCursor::WordUnderCursor);
72                 if (cursor.hasSelection()) {
73                     const QString change = cursor.selectedText();
74                     if (m_exactChangesetId.match(change).hasMatch())
75                         return change;
76                 }
77             }
78         }
79     }
80     return QString();
81 }
82 
createAnnotationHighlighter(const QSet<QString> & changes) const83 VcsBase::BaseAnnotationHighlighter *BazaarEditorWidget::createAnnotationHighlighter(const QSet<QString> &changes) const
84 {
85     return new BazaarAnnotationHighlighter(changes);
86 }
87