forked from qt-creator/qt-creator
Merge remote-tracking branch 'origin/3.3'
Conflicts: src/plugins/coreplugin/coreplugin.cpp src/plugins/coreplugin/themesettingswidget.cpp src/plugins/qbsprojectmanager/qbsprojectmanager.cpp src/plugins/qbsprojectmanager/qbsprojectmanager.h src/plugins/qmlprofiler/qml/Overview.js src/shared/qbs Change-Id: Ibe92c166fc5bfbcb4d6964e50ca7298d8459d60e
This commit is contained in:
@@ -108,6 +108,44 @@ bool DiffEditorController::isIgnoreWhitespace() const
|
||||
return m_ignoreWhitespace;
|
||||
}
|
||||
|
||||
// ### fixme: git-specific handling should be done in the git plugin:
|
||||
// Remove unexpanded branches and follows-tag, clear indentation
|
||||
// and create E-mail
|
||||
static void formatGitDescription(QString *description)
|
||||
{
|
||||
QString result;
|
||||
result.reserve(description->size());
|
||||
foreach (QString line, description->split(QLatin1Char('\n'))) {
|
||||
if (line.startsWith(QLatin1String("commit "))
|
||||
|| line.startsWith(QLatin1String("Branches: <Expand>"))) {
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith(QLatin1String("Author: ")))
|
||||
line.replace(0, 8, QStringLiteral("From: "));
|
||||
else if (line.startsWith(QLatin1String(" ")))
|
||||
line.remove(0, 4);
|
||||
result.append(line);
|
||||
result.append(QLatin1Char('\n'));
|
||||
}
|
||||
*description = result;
|
||||
}
|
||||
|
||||
QString DiffEditorController::contents() const
|
||||
{
|
||||
QString result = m_description;
|
||||
const int formattingOptions = DiffUtils::GitFormat;
|
||||
if (formattingOptions & DiffUtils::GitFormat)
|
||||
formatGitDescription(&result);
|
||||
|
||||
const QString diff = DiffUtils::makePatch(diffFiles(), formattingOptions);
|
||||
if (!diff.isEmpty()) {
|
||||
if (!result.isEmpty())
|
||||
result += QLatin1Char('\n');
|
||||
result += diff;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QString DiffEditorController::makePatch(bool revert, bool addPrefix) const
|
||||
{
|
||||
if (m_diffFileIndex < 0 || m_chunkIndex < 0)
|
||||
|
||||
@@ -58,6 +58,7 @@ public:
|
||||
bool isIgnoreWhitespace() const;
|
||||
|
||||
QString makePatch(bool revert, bool addPrefix = false) const;
|
||||
QString contents() const;
|
||||
|
||||
DiffEditorReloader *reloader() const;
|
||||
void setReloader(DiffEditorReloader *reloader);
|
||||
|
||||
@@ -78,9 +78,7 @@ bool DiffEditorDocument::save(QString *errorString, const QString &fileName, boo
|
||||
Q_UNUSED(errorString)
|
||||
Q_UNUSED(autoSave)
|
||||
|
||||
const QString contents = DiffUtils::makePatch(m_controller->diffFiles());
|
||||
|
||||
const bool ok = write(fileName, format(), contents, errorString);
|
||||
const bool ok = write(fileName, format(), m_controller->contents(), errorString);
|
||||
|
||||
if (!ok)
|
||||
return false;
|
||||
@@ -127,4 +125,39 @@ bool DiffEditorDocument::open(QString *errorString, const QString &fileName)
|
||||
return true;
|
||||
}
|
||||
|
||||
QString DiffEditorDocument::suggestedFileName() const
|
||||
{
|
||||
enum { maxSubjectLength = 50 };
|
||||
QString result = QStringLiteral("0001");
|
||||
const QString description = m_controller->description();
|
||||
if (!description.isEmpty()) {
|
||||
// Derive "git format-patch-type" file name from subject.
|
||||
const int pos = description.indexOf(QLatin1String("\n\n "));
|
||||
const int endPos = pos >= 0 ? description.indexOf(QLatin1Char('\n'), pos + 6) : -1;
|
||||
if (endPos > pos) {
|
||||
const QChar space(QLatin1Char(' '));
|
||||
const QChar dash(QLatin1Char('-'));
|
||||
QString subject = description.mid(pos, endPos - pos);
|
||||
for (int i = 0; i < subject.size(); ++i) {
|
||||
if (!subject.at(i).isLetterOrNumber())
|
||||
subject[i] = space;
|
||||
}
|
||||
subject = subject.simplified();
|
||||
if (subject.size() > maxSubjectLength) {
|
||||
const int lastSpace = subject.lastIndexOf(space, maxSubjectLength);
|
||||
subject.truncate(lastSpace > 0 ? lastSpace : maxSubjectLength);
|
||||
}
|
||||
subject.replace(space, dash);
|
||||
result += dash;
|
||||
result += subject;
|
||||
}
|
||||
}
|
||||
return result + QStringLiteral(".patch");
|
||||
}
|
||||
|
||||
QString DiffEditorDocument::plainText() const
|
||||
{
|
||||
return m_controller->contents();
|
||||
}
|
||||
|
||||
} // namespace DiffEditor
|
||||
|
||||
@@ -42,6 +42,7 @@ class DiffEditorController;
|
||||
class DIFFEDITOR_EXPORT DiffEditorDocument : public Core::BaseTextDocument
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_PROPERTY(QString plainText READ plainText STORED false) // For access by code pasters
|
||||
public:
|
||||
explicit DiffEditorDocument();
|
||||
virtual ~DiffEditorDocument();
|
||||
@@ -50,7 +51,7 @@ public:
|
||||
|
||||
bool setContents(const QByteArray &contents);
|
||||
QString defaultPath() const;
|
||||
QString suggestedFileName() const { return QString(); }
|
||||
QString suggestedFileName() const Q_DECL_OVERRIDE;
|
||||
|
||||
bool isModified() const { return false; }
|
||||
bool isSaveAsAllowed() const { return true; }
|
||||
@@ -58,6 +59,8 @@ public:
|
||||
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
|
||||
bool open(QString *errorString, const QString &fileName);
|
||||
|
||||
QString plainText() const;
|
||||
|
||||
private:
|
||||
DiffEditorController *m_controller;
|
||||
};
|
||||
|
||||
@@ -194,9 +194,8 @@ void DiffEditorPlugin::diff()
|
||||
|
||||
const QString documentId = QLatin1String("Diff ") + fileName1
|
||||
+ QLatin1String(", ") + fileName2;
|
||||
DiffEditorDocument *document = DiffEditorManager::find(documentId);
|
||||
QString title = tr("Diff \"%1\", \"%2\"").arg(fileName1).arg(fileName2);
|
||||
document = DiffEditorManager::findOrCreate(documentId, title);
|
||||
DiffEditorDocument * const document = DiffEditorManager::findOrCreate(documentId, title);
|
||||
if (!document)
|
||||
return;
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
#include "diffutils.h"
|
||||
#include "differ.h"
|
||||
#include <QStringList>
|
||||
#include <QTextStream>
|
||||
#include "texteditor/fontsettings.h"
|
||||
|
||||
namespace DiffEditor {
|
||||
@@ -483,31 +484,35 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
|
||||
return diffText;
|
||||
}
|
||||
|
||||
QString DiffUtils::makePatch(const QList<FileData> &fileDataList)
|
||||
QString DiffUtils::makePatch(const QList<FileData> &fileDataList, unsigned formatFlags)
|
||||
{
|
||||
QString diffText;
|
||||
QTextStream str(&diffText);
|
||||
|
||||
for (int i = 0; i < fileDataList.count(); i++) {
|
||||
const FileData &fileData = fileDataList.at(i);
|
||||
|
||||
if (formatFlags & GitFormat) {
|
||||
str << "diff --git a/" << fileData.leftFileInfo.fileName
|
||||
<< " b/" << fileData.rightFileInfo.fileName << '\n';
|
||||
}
|
||||
if (fileData.binaryFiles) {
|
||||
const QString binaryLine = QLatin1String("Binary files ")
|
||||
+ fileData.leftFileInfo.fileName
|
||||
+ QLatin1String(" and ")
|
||||
+ fileData.rightFileInfo.fileName
|
||||
+ QLatin1String(" differ\n");
|
||||
diffText += binaryLine;
|
||||
str << "Binary files ";
|
||||
if (formatFlags & AddLevel)
|
||||
str << "a/";
|
||||
str << fileData.leftFileInfo.fileName << " and ";
|
||||
if (formatFlags & AddLevel)
|
||||
str << "b/";
|
||||
str << fileData.rightFileInfo.fileName << " differ\n";
|
||||
} else {
|
||||
const QString leftFileInfo = QLatin1String("--- ")
|
||||
+ fileData.leftFileInfo.fileName + QLatin1Char('\n');
|
||||
const QString rightFileInfo = QLatin1String("+++ ")
|
||||
+ fileData.rightFileInfo.fileName + QLatin1Char('\n');
|
||||
|
||||
diffText += leftFileInfo;
|
||||
diffText += rightFileInfo;
|
||||
|
||||
str << "--- ";
|
||||
if (formatFlags & AddLevel)
|
||||
str << "a/";
|
||||
str << fileData.leftFileInfo.fileName << "\n+++ ";
|
||||
if (formatFlags & AddLevel)
|
||||
str << "b/";
|
||||
str << fileData.rightFileInfo.fileName << '\n';
|
||||
for (int j = 0; j < fileData.chunks.count(); j++) {
|
||||
diffText += makePatch(fileData.chunks.at(j),
|
||||
str << makePatch(fileData.chunks.at(j),
|
||||
(j == fileData.chunks.count() - 1)
|
||||
&& fileData.lastChunkAtTheEndOfFile);
|
||||
}
|
||||
|
||||
@@ -130,6 +130,10 @@ public:
|
||||
|
||||
class DIFFEDITOR_EXPORT DiffUtils {
|
||||
public:
|
||||
enum PatchFormattingFlags {
|
||||
AddLevel = 0x1, // Add 'a/' , '/b' for git am
|
||||
GitFormat = AddLevel | 0x2, // Add line 'diff ..' as git does
|
||||
};
|
||||
|
||||
static ChunkData calculateOriginalData(const QList<Diff> &leftDiffList,
|
||||
const QList<Diff> &rightDiffList);
|
||||
@@ -146,7 +150,8 @@ public:
|
||||
const QString &leftFileName,
|
||||
const QString &rightFileName,
|
||||
bool lastChunk = false);
|
||||
static QString makePatch(const QList<FileData> &fileDataList);
|
||||
static QString makePatch(const QList<FileData> &fileDataList,
|
||||
unsigned formatFlags = 0);
|
||||
static QList<FileData> readPatch(const QString &patch,
|
||||
bool *ok = 0);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user