1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2010 Hugues Delorme
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 "bazaareditor.h"
35 #include "annotationhighlighter.h"
36 #include "constants.h"
37 #include "bazaarplugin.h"
38 #include "bazaarclient.h"
40 #include <coreplugin/editormanager/editormanager.h>
41 #include <utils/qtcassert.h>
42 #include <vcsbase/diffhighlighter.h>
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>
52 # define BZR_CHANGE_PATTERN "[0-9]+"
54 using namespace Bazaar::Internal;
55 using namespace Bazaar;
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 '.*'$)"))
62 setAnnotateRevisionTextFormat(tr("Annotate %1"));
63 setAnnotatePreviousRevisionTextFormat(tr("Annotate parent revision %1"));
66 QSet<QString> BazaarEditor::annotationChanges() const
68 QSet<QString> changes;
69 const QString txt = toPlainText();
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);
80 while ((pos = changeNumRx.indexIn(txt, pos)) != -1) {
81 pos += changeNumRx.matchedLength();
82 changes.insert(changeNumRx.cap(1));
88 QString BazaarEditor::changeUnderCursor(const QTextCursor &cursorIn) const
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))
100 VCSBase::DiffHighlighter *BazaarEditor::createDiffHighlighter() const
102 return new VCSBase::DiffHighlighter(m_diffFileId);
105 VCSBase::BaseAnnotationHighlighter *BazaarEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
107 return new BazaarAnnotationHighlighter(changes);
110 QString BazaarEditor::fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const
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());