Commit 656c3343 authored by jkobus's avatar jkobus Committed by Jarek Kobus

Make it possible to save diff editor contents

Task-number: QTCREATORBUG-12548
Task-number: QTCREATORBUG-12549
Change-Id: Ia27080cc689da48fc5401010e2277edaf0a01f4d
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent 337c1b3b
......@@ -51,6 +51,7 @@
#include <QToolBar>
#include <QComboBox>
#include <QFileInfo>
#include <QDir>
#include <QTextCodec>
#include <QTextBlock>
......@@ -316,13 +317,9 @@ bool DiffEditor::open(QString *errorString,
if (!m_controller)
return false;
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
*errorString = tr("Could not open patch file \"%1\".").arg(fileName);
QString patch;
if (m_document->read(fileName, &patch, errorString) != Utils::TextFileFormat::ReadSuccess)
return false;
}
const QString patch = Core::EditorManager::defaultTextCodec()->toUnicode(file.readAll());
bool ok = false;
QList<FileData> fileDataList
......@@ -336,7 +333,9 @@ bool DiffEditor::open(QString *errorString,
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;
}
......
......@@ -30,17 +30,22 @@
#include "diffeditordocument.h"
#include "diffeditorconstants.h"
#include "diffeditorcontroller.h"
#include "diffutils.h"
#include <coreplugin/editormanager/editormanager.h>
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QTextCodec>
namespace DiffEditor {
DiffEditorDocument::DiffEditorDocument() :
Core::IDocument(),
Core::TextDocument(),
m_diffEditorController(new DiffEditorController(this))
{
setId(Constants::DIFF_EDITOR_ID);
setDisplayName(QCoreApplication::translate("DiffEditor", Constants::DIFF_EDITOR_DISPLAY_NAME));
setTemporary(true);
}
......@@ -59,12 +64,33 @@ bool DiffEditorDocument::setContents(const QByteArray &contents)
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)
{
Q_UNUSED(errorString)
Q_UNUSED(fileName)
Q_UNUSED(autoSave)
return false;
if (!m_diffEditorController)
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
......
......@@ -32,13 +32,13 @@
#include "diffeditor_global.h"
#include <coreplugin/idocument.h>
#include <coreplugin/textdocument.h>
namespace DiffEditor {
class DiffEditorController;
class DIFFEDITOR_EXPORT DiffEditorDocument : public Core::IDocument
class DIFFEDITOR_EXPORT DiffEditorDocument : public Core::TextDocument
{
Q_OBJECT
public:
......@@ -48,11 +48,11 @@ public:
DiffEditorController *controller() const;
bool setContents(const QByteArray &contents);
QString defaultPath() const { return QString(); }
QString defaultPath() const;
QString suggestedFileName() const { return QString(); }
bool isModified() const { return false; }
bool isSaveAsAllowed() const { return false; }
bool isSaveAsAllowed() const { return true; }
bool save(QString *errorString, const QString &fileName, bool autoSave);
ReloadBehavior reloadBehavior(ChangeTrigger state, ChangeType type) const;
bool reload(QString *errorString, ReloadFlag flag, ChangeType type);
......
......@@ -61,8 +61,6 @@ protected:
void reload();
private:
QString getFileContents(const QString &fileName) const;
QString m_leftFileName;
QString m_rightFileName;
};
......@@ -78,8 +76,27 @@ SimpleDiffEditorReloader::SimpleDiffEditorReloader(QObject *parent,
void SimpleDiffEditorReloader::reload()
{
const QString leftText = getFileContents(m_leftFileName);
const QString rightText = getFileContents(m_rightFileName);
QString errorString;
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;
QList<Diff> diffList = differ.cleanupSemantics(
......@@ -120,14 +137,6 @@ void SimpleDiffEditorReloader::reload()
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()
......
......@@ -346,9 +346,7 @@ QString DiffUtils::makePatchLine(const QChar &startLineCharacter,
}
QString DiffUtils::makePatch(const ChunkData &chunkData,
const QString &leftFileName,
const QString &rightFileName,
bool lastChunk)
bool lastChunk)
{
QString diffText;
int leftLineCount = 0;
......@@ -425,6 +423,16 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
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 leftFileInfo = QLatin1String("--- ") + leftFileName + QLatin1Char('\n');
......@@ -434,6 +442,39 @@ QString DiffUtils::makePatch(const ChunkData &chunkData,
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,
bool ignoreWhitespace,
bool lastChunk,
......
......@@ -128,10 +128,13 @@ public:
const QString &textLine,
bool lastChunk,
bool lastLine);
static QString makePatch(const ChunkData &chunkData,
bool lastChunk = false);
static QString makePatch(const ChunkData &chunkData,
const QString &leftFileName,
const QString &rightFileName,
bool lastChunk = false);
static QString makePatch(const QList<FileData> &fileDataList);
static QList<FileData> readPatch(const QString &patch,
bool ignoreWhitespace,
bool *ok = 0);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment