1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2009 Brian McGillion
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 "mercurialeditor.h"
35 #include "annotationhighlighter.h"
36 #include "constants.h"
37 #include "mercurialplugin.h"
38 #include "mercurialclient.h"
40 #include <coreplugin/editormanager/editormanager.h>
41 #include <vcsbase/diffhighlighter.h>
43 #include <QtCore/QString>
44 #include <QtGui/QTextCursor>
45 #include <QtGui/QTextBlock>
46 #include <QtCore/QDir>
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDebug>
50 using namespace Mercurial::Internal;
51 using namespace Mercurial;
53 MercurialEditor::MercurialEditor(const VCSBase::VCSBaseEditorParameters *type, QWidget *parent)
54 : VCSBase::VCSBaseEditorWidget(type, parent),
55 exactIdentifier12(QLatin1String(Constants::CHANGEIDEXACT12)),
56 exactIdentifier40(QLatin1String(Constants::CHANGEIDEXACT40)),
57 changesetIdentifier12(QLatin1String(Constants::CHANGESETID12)),
58 changesetIdentifier40(QLatin1String(Constants::CHANGESETID40)),
59 diffIdentifier(QLatin1String(Constants::DIFFIDENTIFIER))
61 setAnnotateRevisionTextFormat(tr("Annotate %1"));
62 setAnnotatePreviousRevisionTextFormat(tr("Annotate parent revision %1"));
65 QSet<QString> MercurialEditor::annotationChanges() const
67 QSet<QString> changes;
68 const QString data = toPlainText();
73 while ((position = changesetIdentifier12.indexIn(data, position)) != -1) {
74 changes.insert(changesetIdentifier12.cap(1));
75 position += changesetIdentifier12.matchedLength();
81 QString MercurialEditor::changeUnderCursor(const QTextCursor &cursorIn) const
83 QTextCursor cursor = cursorIn;
84 cursor.select(QTextCursor::WordUnderCursor);
85 if (cursor.hasSelection()) {
86 const QString change = cursor.selectedText();
87 if (exactIdentifier12.exactMatch(change))
89 if (exactIdentifier40.exactMatch(change))
95 VCSBase::DiffHighlighter *MercurialEditor::createDiffHighlighter() const
97 return new VCSBase::DiffHighlighter(diffIdentifier);
100 VCSBase::BaseAnnotationHighlighter *MercurialEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
102 return new MercurialAnnotationHighlighter(changes);
105 QString MercurialEditor::fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const
107 const QString filechangeId(QLatin1String("+++ b/"));
108 QTextBlock::iterator iterator;
109 for (iterator = diffFileSpec.begin(); !(iterator.atEnd()); iterator++) {
110 QTextFragment fragment = iterator.fragment();
111 if(fragment.isValid()) {
112 if (fragment.text().startsWith(filechangeId)) {
113 const QString filename = fragment.text().remove(0, filechangeId.size());
114 return findDiffFile(filename, MercurialPlugin::instance()->versionControl());
121 QStringList MercurialEditor::annotationPreviousVersions(const QString &revision) const
123 MercurialClient *client = MercurialPlugin::instance()->client();
125 const QFileInfo fi(source());
126 const QString workingDirectory = fi.absolutePath();
127 // Retrieve parent revisions
128 QStringList revisions;
129 if (!client->parentRevisionsSync(workingDirectory, fi.fileName(), revision, &revisions))
130 return QStringList();
131 // Format with short summary
132 QStringList descriptions;
133 if (!client->shortDescriptionsSync(workingDirectory, revisions, &descriptions))
134 return QStringList();