forked from qt-creator/qt-creator
Make it possible to save diff editor contents
Task-number: QTCREATORBUG-12548 Task-number: QTCREATORBUG-12549 Change-Id: Ia27080cc689da48fc5401010e2277edaf0a01f4d Reviewed-by: Eike Ziller <eike.ziller@digia.com>
This commit is contained in:
@@ -51,6 +51,7 @@
|
|||||||
#include <QToolBar>
|
#include <QToolBar>
|
||||||
#include <QComboBox>
|
#include <QComboBox>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
|
#include <QDir>
|
||||||
#include <QTextCodec>
|
#include <QTextCodec>
|
||||||
#include <QTextBlock>
|
#include <QTextBlock>
|
||||||
|
|
||||||
@@ -316,13 +317,9 @@ bool DiffEditor::open(QString *errorString,
|
|||||||
if (!m_controller)
|
if (!m_controller)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
QFile file(fileName);
|
QString patch;
|
||||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (m_document->read(fileName, &patch, errorString) != Utils::TextFileFormat::ReadSuccess)
|
||||||
*errorString = tr("Could not open patch file \"%1\".").arg(fileName);
|
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
const QString patch = Core::EditorManager::defaultTextCodec()->toUnicode(file.readAll());
|
|
||||||
|
|
||||||
bool ok = false;
|
bool ok = false;
|
||||||
QList<FileData> fileDataList
|
QList<FileData> fileDataList
|
||||||
@@ -336,7 +333,9 @@ bool DiffEditor::open(QString *errorString,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_controller->setDiffFiles(fileDataList, QFileInfo(fileName).absolutePath());
|
const QFileInfo fi(fileName);
|
||||||
|
m_document->setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
|
||||||
|
m_controller->setDiffFiles(fileDataList, fi.absolutePath());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,17 +30,22 @@
|
|||||||
#include "diffeditordocument.h"
|
#include "diffeditordocument.h"
|
||||||
#include "diffeditorconstants.h"
|
#include "diffeditorconstants.h"
|
||||||
#include "diffeditorcontroller.h"
|
#include "diffeditorcontroller.h"
|
||||||
|
#include "diffutils.h"
|
||||||
|
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QFile>
|
||||||
|
#include <QDir>
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
namespace DiffEditor {
|
namespace DiffEditor {
|
||||||
|
|
||||||
DiffEditorDocument::DiffEditorDocument() :
|
DiffEditorDocument::DiffEditorDocument() :
|
||||||
Core::IDocument(),
|
Core::TextDocument(),
|
||||||
m_diffEditorController(new DiffEditorController(this))
|
m_diffEditorController(new DiffEditorController(this))
|
||||||
{
|
{
|
||||||
setId(Constants::DIFF_EDITOR_ID);
|
setId(Constants::DIFF_EDITOR_ID);
|
||||||
setDisplayName(QCoreApplication::translate("DiffEditor", Constants::DIFF_EDITOR_DISPLAY_NAME));
|
|
||||||
setTemporary(true);
|
setTemporary(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,12 +64,33 @@ bool DiffEditorDocument::setContents(const QByteArray &contents)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DiffEditorDocument::defaultPath() const
|
||||||
|
{
|
||||||
|
if (!m_diffEditorController)
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
return m_diffEditorController->workingDirectory();
|
||||||
|
}
|
||||||
|
|
||||||
bool DiffEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave)
|
bool DiffEditorDocument::save(QString *errorString, const QString &fileName, bool autoSave)
|
||||||
{
|
{
|
||||||
Q_UNUSED(errorString)
|
Q_UNUSED(errorString)
|
||||||
Q_UNUSED(fileName)
|
|
||||||
Q_UNUSED(autoSave)
|
Q_UNUSED(autoSave)
|
||||||
|
|
||||||
|
if (!m_diffEditorController)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
|
const QString contents = DiffUtils::makePatch(m_diffEditorController->diffFiles());
|
||||||
|
|
||||||
|
const bool ok = write(fileName, format(), contents, errorString);
|
||||||
|
|
||||||
|
if (!ok)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
const QFileInfo fi(fileName);
|
||||||
|
setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
|
||||||
|
setDisplayName(QString());
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
Core::IDocument::ReloadBehavior DiffEditorDocument::reloadBehavior(ChangeTrigger state, ChangeType type) const
|
Core::IDocument::ReloadBehavior DiffEditorDocument::reloadBehavior(ChangeTrigger state, ChangeType type) const
|
||||||
|
|||||||
@@ -32,13 +32,13 @@
|
|||||||
|
|
||||||
#include "diffeditor_global.h"
|
#include "diffeditor_global.h"
|
||||||
|
|
||||||
#include <coreplugin/idocument.h>
|
#include <coreplugin/textdocument.h>
|
||||||
|
|
||||||
namespace DiffEditor {
|
namespace DiffEditor {
|
||||||
|
|
||||||
class DiffEditorController;
|
class DiffEditorController;
|
||||||
|
|
||||||
class DIFFEDITOR_EXPORT DiffEditorDocument : public Core::IDocument
|
class DIFFEDITOR_EXPORT DiffEditorDocument : public Core::TextDocument
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -48,11 +48,11 @@ public:
|
|||||||
DiffEditorController *controller() const;
|
DiffEditorController *controller() const;
|
||||||
|
|
||||||
bool setContents(const QByteArray &contents);
|
bool setContents(const QByteArray &contents);
|
||||||
QString defaultPath() const { return QString(); }
|
QString defaultPath() const;
|
||||||
QString suggestedFileName() const { return QString(); }
|
QString suggestedFileName() const { return QString(); }
|
||||||
|
|
||||||
bool isModified() const { return false; }
|
bool isModified() const { return false; }
|
||||||
bool isSaveAsAllowed() const { return false; }
|
bool isSaveAsAllowed() const { return true; }
|
||||||
bool save(QString *errorString, const QString &fileName, bool autoSave);
|
bool save(QString *errorString, const QString &fileName, bool autoSave);
|
||||||
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
|
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
|
||||||
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
|
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
|
||||||
|
|||||||
@@ -61,8 +61,6 @@ protected:
|
|||||||
void reload();
|
void reload();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString getFileContents(const QString &fileName) const;
|
|
||||||
|
|
||||||
QString m_leftFileName;
|
QString m_leftFileName;
|
||||||
QString m_rightFileName;
|
QString m_rightFileName;
|
||||||
};
|
};
|
||||||
@@ -78,8 +76,27 @@ SimpleDiffEditorReloader::SimpleDiffEditorReloader(QObject *parent,
|
|||||||
|
|
||||||
void SimpleDiffEditorReloader::reload()
|
void SimpleDiffEditorReloader::reload()
|
||||||
{
|
{
|
||||||
const QString leftText = getFileContents(m_leftFileName);
|
QString errorString;
|
||||||
const QString rightText = getFileContents(m_rightFileName);
|
Utils::TextFileFormat format;
|
||||||
|
format.codec = Core::EditorManager::defaultTextCodec();
|
||||||
|
|
||||||
|
QString leftText;
|
||||||
|
if (Utils::TextFileFormat::readFile(m_leftFileName,
|
||||||
|
format.codec,
|
||||||
|
&leftText, &format, &errorString)
|
||||||
|
!= Utils::TextFileFormat::ReadSuccess) {
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString rightText;
|
||||||
|
if (Utils::TextFileFormat::readFile(m_rightFileName,
|
||||||
|
format.codec,
|
||||||
|
&rightText, &format, &errorString)
|
||||||
|
!= Utils::TextFileFormat::ReadSuccess) {
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
Differ differ;
|
Differ differ;
|
||||||
QList<Diff> diffList = differ.cleanupSemantics(
|
QList<Diff> diffList = differ.cleanupSemantics(
|
||||||
@@ -120,14 +137,6 @@ void SimpleDiffEditorReloader::reload()
|
|||||||
reloadFinished();
|
reloadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
QString SimpleDiffEditorReloader::getFileContents(const QString &fileName) const
|
|
||||||
{
|
|
||||||
QFile file(fileName);
|
|
||||||
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
|
|
||||||
return Core::EditorManager::defaultTextCodec()->toUnicode(file.readAll());
|
|
||||||
return QString();
|
|
||||||
}
|
|
||||||
|
|
||||||
/////////////////
|
/////////////////
|
||||||
|
|
||||||
DiffEditorPlugin::DiffEditorPlugin()
|
DiffEditorPlugin::DiffEditorPlugin()
|
||||||
|
|||||||
@@ -346,8 +346,6 @@ QString DiffUtils::makePatchLine(const QChar &startLineCharacter,
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString DiffUtils::makePatch(const ChunkData &chunkData,
|
QString DiffUtils::makePatch(const ChunkData &chunkData,
|
||||||
const QString &leftFileName,
|
|
||||||
const QString &rightFileName,
|
|
||||||
bool lastChunk)
|
bool lastChunk)
|
||||||
{
|
{
|
||||||
QString diffText;
|
QString diffText;
|
||||||
@@ -425,6 +423,16 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
|
|||||||
|
|
||||||
diffText.prepend(chunkLine);
|
diffText.prepend(chunkLine);
|
||||||
|
|
||||||
|
return diffText;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString DiffUtils::makePatch(const ChunkData &chunkData,
|
||||||
|
const QString &leftFileName,
|
||||||
|
const QString &rightFileName,
|
||||||
|
bool lastChunk)
|
||||||
|
{
|
||||||
|
QString diffText = makePatch(chunkData, lastChunk);
|
||||||
|
|
||||||
const QString rightFileInfo = QLatin1String("+++ ") + rightFileName + QLatin1Char('\n');
|
const QString rightFileInfo = QLatin1String("+++ ") + rightFileName + QLatin1Char('\n');
|
||||||
const QString leftFileInfo = QLatin1String("--- ") + leftFileName + QLatin1Char('\n');
|
const QString leftFileInfo = QLatin1String("--- ") + leftFileName + QLatin1Char('\n');
|
||||||
|
|
||||||
@@ -434,6 +442,39 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
|
|||||||
return diffText;
|
return diffText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString DiffUtils::makePatch(const QList<FileData> &fileDataList)
|
||||||
|
{
|
||||||
|
QString diffText;
|
||||||
|
|
||||||
|
for (int i = 0; i < fileDataList.count(); i++) {
|
||||||
|
const FileData &fileData = fileDataList.at(i);
|
||||||
|
|
||||||
|
if (fileData.binaryFiles) {
|
||||||
|
const QString binaryLine = QLatin1String("Binary files ")
|
||||||
|
+ fileData.leftFileInfo.fileName
|
||||||
|
+ QLatin1String(" and ")
|
||||||
|
+ fileData.rightFileInfo.fileName
|
||||||
|
+ QLatin1String(" differ\n");
|
||||||
|
diffText += binaryLine;
|
||||||
|
} else {
|
||||||
|
const QString leftFileInfo = QLatin1String("--- ")
|
||||||
|
+ fileData.leftFileInfo.fileName + QLatin1Char('\n');
|
||||||
|
const QString rightFileInfo = QLatin1String("+++ ")
|
||||||
|
+ fileData.rightFileInfo.fileName + QLatin1Char('\n');
|
||||||
|
|
||||||
|
diffText += leftFileInfo;
|
||||||
|
diffText += rightFileInfo;
|
||||||
|
|
||||||
|
for (int j = 0; j < fileData.chunks.count(); j++) {
|
||||||
|
diffText += makePatch(fileData.chunks.at(j),
|
||||||
|
(j == fileData.chunks.count() - 1)
|
||||||
|
&& fileData.lastChunkAtTheEndOfFile);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return diffText;
|
||||||
|
}
|
||||||
|
|
||||||
static QList<RowData> readLines(const QString &patch,
|
static QList<RowData> readLines(const QString &patch,
|
||||||
bool ignoreWhitespace,
|
bool ignoreWhitespace,
|
||||||
bool lastChunk,
|
bool lastChunk,
|
||||||
|
|||||||
@@ -128,10 +128,13 @@ public:
|
|||||||
const QString &textLine,
|
const QString &textLine,
|
||||||
bool lastChunk,
|
bool lastChunk,
|
||||||
bool lastLine);
|
bool lastLine);
|
||||||
|
static QString makePatch(const ChunkData &chunkData,
|
||||||
|
bool lastChunk = false);
|
||||||
static QString makePatch(const ChunkData &chunkData,
|
static QString makePatch(const ChunkData &chunkData,
|
||||||
const QString &leftFileName,
|
const QString &leftFileName,
|
||||||
const QString &rightFileName,
|
const QString &rightFileName,
|
||||||
bool lastChunk = false);
|
bool lastChunk = false);
|
||||||
|
static QString makePatch(const QList<FileData> &fileDataList);
|
||||||
static QList<FileData> readPatch(const QString &patch,
|
static QList<FileData> readPatch(const QString &patch,
|
||||||
bool ignoreWhitespace,
|
bool ignoreWhitespace,
|
||||||
bool *ok = 0);
|
bool *ok = 0);
|
||||||
|
|||||||
Reference in New Issue
Block a user