OSDN Git Service

Update license.
[qt-creator-jp/qt-creator-jp.git] / src / plugins / git / giteditor.cpp
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
8 **
9 **
10 ** GNU Lesser General Public License Usage
11 **
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 **
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 **
23 ** Other Usage
24 **
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **************************************************************************/
32
33 #include "giteditor.h"
34
35 #include "annotationhighlighter.h"
36 #include "gitconstants.h"
37 #include "gitplugin.h"
38 #include "gitclient.h"
39 #include "gitsettings.h"
40 #include <QtCore/QTextCodec>
41
42 #include <coreplugin/editormanager/editormanager.h>
43 #include <utils/qtcassert.h>
44 #include <vcsbase/diffhighlighter.h>
45 #include <vcsbase/vcsbaseoutputwindow.h>
46 #include <QtCore/QDebug>
47 #include <QtCore/QDir>
48 #include <QtCore/QFileInfo>
49 #include <QtCore/QRegExp>
50 #include <QtCore/QSet>
51 #include <QtCore/QTextStream>
52
53 #include <QtGui/QTextCursor>
54 #include <QtGui/QTextEdit>
55 #include <QtGui/QTextBlock>
56
57 #define CHANGE_PATTERN_8C "[a-f0-9]{7,8}"
58 #define CHANGE_PATTERN_40C "[a-f0-9]{40,40}"
59
60 namespace Git {
61 namespace Internal {
62
63 // ------------ GitEditor
64 GitEditor::GitEditor(const VCSBase::VCSBaseEditorParameters *type,
65                      QWidget *parent)  :
66     VCSBase::VCSBaseEditorWidget(type, parent),
67     m_changeNumberPattern8(QLatin1String(CHANGE_PATTERN_8C)),
68     m_changeNumberPattern40(QLatin1String(CHANGE_PATTERN_40C))
69 {
70     QTC_ASSERT(m_changeNumberPattern8.isValid(), return);
71     QTC_ASSERT(m_changeNumberPattern40.isValid(), return);
72     setAnnotateRevisionTextFormat(tr("Blame %1"));
73     setAnnotatePreviousRevisionTextFormat(tr("Blame parent revision %1"));
74     if (Git::Constants::debug)
75         qDebug() << "GitEditor::GitEditor" << type->type << type->id;
76 }
77
78 QSet<QString> GitEditor::annotationChanges() const
79 {
80     QSet<QString> changes;
81     const QString txt = toPlainText();
82     if (txt.isEmpty())
83         return changes;
84     // Hunt for first change number in annotation: "<change>:"
85     QRegExp r(QLatin1String("^("CHANGE_PATTERN_8C") "));
86     QTC_ASSERT(r.isValid(), return changes);
87     if (r.indexIn(txt) != -1) {
88         changes.insert(r.cap(1));
89         r.setPattern(QLatin1String("\n("CHANGE_PATTERN_8C") "));
90         QTC_ASSERT(r.isValid(), return changes);
91         int pos = 0;
92         while ((pos = r.indexIn(txt, pos)) != -1) {
93             pos += r.matchedLength();
94             changes.insert(r.cap(1));
95         }
96     }
97     if (Git::Constants::debug)
98         qDebug() << "GitEditor::annotationChanges() returns #" << changes.size();
99     return changes;
100 }
101
102 QString GitEditor::changeUnderCursor(const QTextCursor &c) const
103 {
104     QTextCursor cursor = c;
105     // Any number is regarded as change number.
106     cursor.select(QTextCursor::WordUnderCursor);
107     if (!cursor.hasSelection())
108         return QString();
109     const QString change = cursor.selectedText();
110     if (Git::Constants::debug > 1)
111         qDebug() << "GitEditor:::changeUnderCursor:" << change;
112     if (m_changeNumberPattern8.exactMatch(change))
113         return change;
114     if (m_changeNumberPattern40.exactMatch(change))
115         return change;
116     return QString();
117 }
118
119 VCSBase::DiffHighlighter *GitEditor::createDiffHighlighter() const
120 {
121     const QRegExp filePattern(QLatin1String("^(diff --git a/|index |[+-][+-][+-] [ab]).*$"));
122     return new VCSBase::DiffHighlighter(filePattern);
123 }
124
125 VCSBase::BaseAnnotationHighlighter *GitEditor::createAnnotationHighlighter(const QSet<QString> &changes) const
126 {
127     return new GitAnnotationHighlighter(changes);
128 }
129
130 QString GitEditor::fileNameFromDiffSpecification(const QTextBlock &inBlock) const
131 {
132         // Check for "+++ b/src/plugins/git/giteditor.cpp" (blame and diff)
133     // Go back chunks.
134     const QString newFileIndicator = QLatin1String("+++ b/");
135     for (QTextBlock  block = inBlock; block.isValid(); block = block.previous()) {
136         QString diffFileName = block.text();
137         if (diffFileName.startsWith(newFileIndicator)) {
138             diffFileName.remove(0, newFileIndicator.size());
139             return findDiffFile(diffFileName, GitPlugin::instance()->versionControl());
140         }
141     }
142     return QString();
143 }
144
145 /* Remove the date specification from annotation, which is tabular:
146 \code
147 8ca887aa (author               YYYY-MM-DD HH:MM:SS <offset>  <line>)<content>
148 \endcode */
149
150 static void removeAnnotationDate(QString *s)
151 {
152     if (s->isEmpty())
153         return;
154     // Get position of date (including blank) and the ')'
155     const QRegExp isoDatePattern(QLatin1String(" \\d{4}-\\d{2}-\\d{2}"));
156     Q_ASSERT(isoDatePattern.isValid());
157     const int datePos = s->indexOf(isoDatePattern);
158     const int parenPos = datePos == -1 ? -1 : s->indexOf(QLatin1Char(')'));
159     if (parenPos == -1)
160         return;
161     // In all lines, remove the bit from datePos .. parenPos;
162     const int dateLength = parenPos - datePos;
163     const QChar newLine = QLatin1Char('\n');
164     for (int pos = 0; pos < s->size(); ) {
165         if (pos + parenPos >s->size()) // Should not happen
166             break;
167         s->remove(pos + datePos, dateLength);
168         const int nextLinePos = s->indexOf(newLine, pos + datePos);
169         pos = nextLinePos == -1 ? s->size() : nextLinePos  + 1;
170     }
171 }
172
173 void GitEditor::setPlainTextDataFiltered(const QByteArray &a)
174 {
175     // If desired, filter out the date from annotation
176     const bool omitAnnotationDate = contentType() == VCSBase::AnnotateOutput
177                                     && GitPlugin::instance()->settings().omitAnnotationDate;
178     if (omitAnnotationDate) {
179         QString text = codec()->toUnicode(a);
180         removeAnnotationDate(&text);
181         setPlainText(text);
182     } else {
183         setPlainTextData(a);
184     }
185 }
186
187 void GitEditor::commandFinishedGotoLine(bool ok, int /* exitCode */, const QVariant &v)
188 {
189     if (ok && v.type() == QVariant::Int) {
190         const int line = v.toInt();
191         if (line >= 0)
192             gotoLine(line);
193     }
194 }
195
196 QStringList GitEditor::annotationPreviousVersions(const QString &revision) const
197 {
198     QStringList revisions;
199     QString errorMessage;
200     GitClient *client = GitPlugin::instance()->gitClient();
201     const QFileInfo fi(source());
202     const QString workingDirectory = fi.absolutePath();
203     // Get the SHA1's of the file.
204     if (!client->synchronousParentRevisions(workingDirectory, QStringList(fi.fileName()),
205                                             revision, &revisions, &errorMessage)) {
206         VCSBase::VCSBaseOutputWindow::instance()->appendSilently(errorMessage);
207         return QStringList();
208     }
209     // Format verbose, SHA1 being first token
210     QStringList descriptions;
211     if (!client->synchronousShortDescriptions(workingDirectory, revisions, &descriptions, &errorMessage)) {
212         VCSBase::VCSBaseOutputWindow::instance()->appendSilently(errorMessage);
213         return QStringList();
214     }
215     return descriptions;
216 }
217
218 } // namespace Internal
219 } // namespace Git
220