OSDN Git Service

3c5a94152f80342f95c673a82117bad5889936b2
[qt-creator-jp/qt-creator-jp.git] / src / plugins / mercurial / mercurialeditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2009 Brian McGillion
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 "mercurialeditor.h"
35 #include "annotationhighlighter.h"
36 #include "constants.h"
37 #include "mercurialplugin.h"
38 #include "mercurialclient.h"
39
40 #include <coreplugin/editormanager/editormanager.h>
41 #include <vcsbase/diffhighlighter.h>
42
43 #include <QtCore/QString>
44 #include <QtGui/QTextCursor>
45 #include <QtGui/QTextBlock>
46 #include <QtCore/QDir>
47 #include <QtCore/QFileInfo>
48 #include <QtCore/QDebug>
49
50 using namespace Mercurial::Internal;
51 using namespace Mercurial;
52
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))
60 {
61     setAnnotateRevisionTextFormat(tr("Annotate %1"));
62     setAnnotatePreviousRevisionTextFormat(tr("Annotate parent revision %1"));
63 }
64
65 QSet<QString> MercurialEditor::annotationChanges() const
66 {
67     QSet<QString> changes;
68     const QString data = toPlainText();
69     if (data.isEmpty())
70         return changes;
71
72     int position = 0;
73     while ((position = changesetIdentifier12.indexIn(data, position)) != -1) {
74         changes.insert(changesetIdentifier12.cap(1));
75         position += changesetIdentifier12.matchedLength();
76     }
77
78     return changes;
79 }
80
81 QString MercurialEditor::changeUnderCursor(const QTextCursor &cursorIn) const
82 {
83     QTextCursor cursor = cursorIn;
84     cursor.select(QTextCursor::WordUnderCursor);
85     if (cursor.hasSelection()) {
86         const QString change = cursor.selectedText();
87         if (exactIdentifier12.exactMatch(change))
88             return change;
89         if (exactIdentifier40.exactMatch(change))
90             return change;
91     }
92     return QString();
93 }
94
95 VCSBase::DiffHighlighter *MercurialEditor::createDiffHighlighter() const
96 {
97     return new VCSBase::DiffHighlighter(diffIdentifier);
98 }
99
100 VCSBase::BaseAnnotationHighlighter *MercurialEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
101 {
102     return new MercurialAnnotationHighlighter(changes);
103 }
104
105 QString MercurialEditor::fileNameFromDiffSpecification(const QTextBlock &diffFileSpec) const
106 {
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());
115             }
116         }
117     }
118     return QString();
119 }
120
121 QStringList MercurialEditor::annotationPreviousVersions(const QString &revision) const
122 {
123     MercurialClient *client = MercurialPlugin::instance()->client();
124     QStringList parents;
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();
135     return descriptions;
136 }
137