OSDN Git Service

b6e2c484110bb3508cf5066cb50e49e3aa0750b8
[qt-creator-jp/qt-creator-jp.git] / src / plugins / bazaar / bazaareditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2010 Hugues Delorme
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #include "bazaareditor.h"
35 #include "annotationhighlighter.h"
36 #include "constants.h"
37 #include "bazaarplugin.h"
38 #include "bazaarclient.h"
39
40 #include <coreplugin/editormanager/editormanager.h>
41 #include <utils/qtcassert.h>
42 #include <vcsbase/diffhighlighter.h>
43
44 #include <QtCore/QRegExp>
45 #include <QtCore/QString>
46 #include <QtGui/QTextCursor>
47 #include <QtGui/QTextBlock>
48 #include <QtCore/QDir>
49 #include <QtCore/QFileInfo>
50 #include <QtCore/QDebug>
51
52 # define BZR_CHANGE_PATTERN "[0-9]+"
53
54 using namespace Bazaar::Internal;
55 using namespace Bazaar;
56
57 BazaarEditor::BazaarEditor(const VCSBase::VCSBaseEditorParameters *type, QWidget *parent)
58     : VCSBase::VCSBaseEditorWidget(type, parent),
59       m_exactChangesetId(QLatin1String(Constants::CHANGESET_ID_EXACT)),
60       m_diffFileId(QLatin1String("^(=== modified file '.*'$)"))
61 {
62     setAnnotateRevisionTextFormat(tr("Annotate %1"));
63     setAnnotatePreviousRevisionTextFormat(tr("Annotate parent revision %1"));
64 }
65
66 QSet<QString> BazaarEditor::annotationChanges() const
67 {
68     QSet<QString> changes;
69     const QString txt = toPlainText();
70     if (txt.isEmpty())
71         return changes;
72
73     QRegExp changeNumRx(QLatin1String("^("BZR_CHANGE_PATTERN") "));
74     QTC_ASSERT(changeNumRx.isValid(), return changes);
75     if (changeNumRx.indexIn(txt) != -1) {
76         changes.insert(changeNumRx.cap(1));
77         changeNumRx.setPattern(QLatin1String("\n("BZR_CHANGE_PATTERN") "));
78         QTC_ASSERT(changeNumRx.isValid(), return changes);
79         int pos = 0;
80         while ((pos = changeNumRx.indexIn(txt, pos)) != -1) {
81             pos += changeNumRx.matchedLength();
82             changes.insert(changeNumRx.cap(1));
83         }
84     }
85     return changes;
86 }
87
88 QString BazaarEditor::changeUnderCursor(const QTextCursor &cursorIn) const
89 {
90     QTextCursor cursor = cursorIn;
91     cursor.select(QTextCursor::WordUnderCursor);
92     if (cursor.hasSelection()) {
93         const QString change = cursor.selectedText();
94         if (m_exactChangesetId.exactMatch(change))
95             return change;
96     }
97     return QString();
98 }
99
100 VCSBase::DiffHighlighter *BazaarEditor::createDiffHighlighter() const
101 {
102     return new VCSBase::DiffHighlighter(m_diffFileId);
103 }
104
105 VCSBase::BaseAnnotationHighlighter *BazaarEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
106 {
107     return new BazaarAnnotationHighlighter(changes);
108 }
109
110 QString BazaarEditor::fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const
111 {
112     const QString filechangeId(QLatin1String("+++ b/"));
113     QTextBlock::iterator iterator;
114     for (iterator = diffFileSpec.begin(); !(iterator.atEnd()); iterator++) {
115         QTextFragment fragment = iterator.fragment();
116         if(fragment.isValid()) {
117             if (fragment.text().startsWith(filechangeId)) {
118                 const QString filename = fragment.text().remove(0, filechangeId.size());
119                 return findDiffFile(filename, BazaarPlugin::instance()->versionControl());
120             }
121         }
122     }
123     return QString();
124 }