Hide some diff editor controls when diff editor is bound to a file.

Remove unnecessary anymore "ignoreWhitespace" argument
from functions which read a patch file.

Transfer the ownership of reloader into controller.

Task-number: QTCREATORBUG-13250
Change-Id: I68183005b845d6ece9ea2be9888abc8597310426
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
jkobus
2014-10-24 14:11:12 +02:00
committed by Jarek Kobus
parent f406a38ca7
commit 2a9c9f2d70
12 changed files with 52 additions and 68 deletions

View File

@@ -322,12 +322,12 @@ QWidget *DiffEditor::toolBar()
whitespaceButton->setText(tr("Ignore Whitespace")); whitespaceButton->setText(tr("Ignore Whitespace"));
whitespaceButton->setCheckable(true); whitespaceButton->setCheckable(true);
whitespaceButton->setChecked(m_controller->isIgnoreWhitespace()); whitespaceButton->setChecked(m_controller->isIgnoreWhitespace());
m_toolBar->addWidget(whitespaceButton); m_whitespaceButtonAction = m_toolBar->addWidget(whitespaceButton);
QLabel *contextLabel = new QLabel(m_toolBar); QLabel *contextLabel = new QLabel(m_toolBar);
contextLabel->setText(tr("Context Lines:")); contextLabel->setText(tr("Context Lines:"));
contextLabel->setContentsMargins(6, 0, 6, 0); contextLabel->setContentsMargins(6, 0, 6, 0);
m_toolBar->addWidget(contextLabel); m_contextLabelAction = m_toolBar->addWidget(contextLabel);
QSpinBox *contextSpinBox = new QSpinBox(m_toolBar); QSpinBox *contextSpinBox = new QSpinBox(m_toolBar);
contextSpinBox->setRange(1, 100); contextSpinBox->setRange(1, 100);
@@ -335,7 +335,7 @@ QWidget *DiffEditor::toolBar()
contextSpinBox->setFrame(false); contextSpinBox->setFrame(false);
contextSpinBox->setSizePolicy(QSizePolicy::Minimum, contextSpinBox->setSizePolicy(QSizePolicy::Minimum,
QSizePolicy::Expanding); // Mac Qt5 QSizePolicy::Expanding); // Mac Qt5
m_toolBar->addWidget(contextSpinBox); m_contextSpinBoxAction = m_toolBar->addWidget(contextSpinBox);
QToolButton *toggleDescription = new QToolButton(m_toolBar); QToolButton *toggleDescription = new QToolButton(m_toolBar);
toggleDescription->setIcon( toggleDescription->setIcon(
@@ -497,6 +497,9 @@ void DiffEditor::slotDescriptionVisibilityChanged()
void DiffEditor::slotReloaderChanged(DiffEditorReloader *reloader) void DiffEditor::slotReloaderChanged(DiffEditorReloader *reloader)
{ {
m_whitespaceButtonAction->setVisible(reloader);
m_contextLabelAction->setVisible(reloader);
m_contextSpinBoxAction->setVisible(reloader);
m_reloadAction->setVisible(reloader); m_reloadAction->setVisible(reloader);
} }

View File

@@ -104,6 +104,9 @@ private:
DiffEditorGuiController *m_guiController; DiffEditorGuiController *m_guiController;
QToolBar *m_toolBar; QToolBar *m_toolBar;
QComboBox *m_entriesComboBox; QComboBox *m_entriesComboBox;
QAction *m_whitespaceButtonAction;
QAction *m_contextLabelAction;
QAction *m_contextSpinBoxAction;
QAction *m_toggleDescriptionAction; QAction *m_toggleDescriptionAction;
QAction *m_reloadAction; QAction *m_reloadAction;
QToolButton *m_diffEditorSwitcher; QToolButton *m_diffEditorSwitcher;

View File

@@ -64,7 +64,7 @@ DiffEditorController::DiffEditorController(QObject *parent)
DiffEditorController::~DiffEditorController() DiffEditorController::~DiffEditorController()
{ {
delete m_reloader;
} }
QString DiffEditorController::clearMessage() const QString DiffEditorController::clearMessage() const
@@ -137,20 +137,20 @@ DiffEditorReloader *DiffEditorController::reloader() const
return m_reloader; return m_reloader;
} }
// The ownership of reloader is passed to the controller
void DiffEditorController::setReloader(DiffEditorReloader *reloader) void DiffEditorController::setReloader(DiffEditorReloader *reloader)
{ {
if (m_reloader == reloader) if (m_reloader == reloader)
return; // nothing changes return; // nothing changes
if (m_reloader) delete m_reloader;
m_reloader->setController(0);
m_reloader = reloader; m_reloader = reloader;
if (m_reloader) if (m_reloader)
m_reloader->setController(this); m_reloader->setController(this);
reloaderChanged(m_reloader); emit reloaderChanged(m_reloader);
} }
void DiffEditorController::clear() void DiffEditorController::clear()

View File

@@ -31,6 +31,8 @@
#include "diffeditordocument.h" #include "diffeditordocument.h"
#include "diffeditorconstants.h" #include "diffeditorconstants.h"
#include "diffeditorcontroller.h" #include "diffeditorcontroller.h"
#include "diffeditormanager.h"
#include "diffeditorreloader.h"
#include "diffutils.h" #include "diffutils.h"
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
@@ -83,6 +85,10 @@ bool DiffEditorDocument::save(QString *errorString, const QString &fileName, boo
if (!ok) if (!ok)
return false; return false;
if (m_controller->reloader())
m_controller->setReloader(0);
DiffEditorManager::removeDocument(this);
const QFileInfo fi(fileName); const QFileInfo fi(fileName);
setTemporary(false); setTemporary(false);
setFilePath(QDir::cleanPath(fi.absoluteFilePath())); setFilePath(QDir::cleanPath(fi.absoluteFilePath()));
@@ -105,10 +111,7 @@ bool DiffEditorDocument::open(QString *errorString, const QString &fileName)
return false; return false;
bool ok = false; bool ok = false;
QList<FileData> fileDataList QList<FileData> fileDataList = DiffUtils::readPatch(patch, &ok);
= DiffUtils::readPatch(patch,
m_controller->isIgnoreWhitespace(),
&ok);
if (!ok) { if (!ok) {
*errorString = tr("Could not parse patch file \"%1\". " *errorString = tr("Could not parse patch file \"%1\". "
"The content is not of unified diff format.") "The content is not of unified diff format.")

View File

@@ -115,5 +115,14 @@ DiffEditorDocument *DiffEditorManager::findOrCreate(const QString &documentId, c
return document; return document;
} }
void DiffEditorManager::removeDocument(DiffEditorDocument *document)
{
if (!instance()->documentToId.contains(document))
return;
const QString documentId = instance()->documentToId.value(document);
instance()->documentToId.remove(document);
instance()->idToDocument.remove(documentId);
}
} // namespace DiffEditor } // namespace DiffEditor

View File

@@ -53,6 +53,7 @@ public:
static DiffEditorDocument *find(const QString &documentId); static DiffEditorDocument *find(const QString &documentId);
static DiffEditorDocument *findOrCreate(const QString &documentId, const QString &displayName); static DiffEditorDocument *findOrCreate(const QString &documentId, const QString &displayName);
static void removeDocument(DiffEditorDocument *document);
private slots: private slots:
void slotEditorsClosed(const QList<Core::IEditor *> &editors); void slotEditorsClosed(const QList<Core::IEditor *> &editors);

View File

@@ -54,8 +54,7 @@ class SimpleDiffEditorReloader : public DiffEditorReloader
{ {
Q_OBJECT Q_OBJECT
public: public:
SimpleDiffEditorReloader(QObject *parent, SimpleDiffEditorReloader(const QString &leftFileName,
const QString &leftFileName,
const QString &rightFileName); const QString &rightFileName);
protected: protected:
@@ -66,11 +65,9 @@ private:
QString m_rightFileName; QString m_rightFileName;
}; };
SimpleDiffEditorReloader::SimpleDiffEditorReloader(QObject *parent, SimpleDiffEditorReloader::SimpleDiffEditorReloader(const QString &leftFileName,
const QString &leftFileName,
const QString &rightFileName) const QString &rightFileName)
: DiffEditorReloader(parent), : m_leftFileName(leftFileName),
m_leftFileName(leftFileName),
m_rightFileName(rightFileName) m_rightFileName(rightFileName)
{ {
} }
@@ -206,7 +203,7 @@ void DiffEditorPlugin::diff()
DiffEditorController *controller = document->controller(); DiffEditorController *controller = document->controller();
if (!controller->reloader()) { if (!controller->reloader()) {
SimpleDiffEditorReloader *reloader = SimpleDiffEditorReloader *reloader =
new SimpleDiffEditorReloader(controller, fileName1, fileName2); new SimpleDiffEditorReloader(fileName1, fileName2);
controller->setReloader(reloader); controller->setReloader(reloader);
} }
@@ -475,7 +472,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testMakePatch()
QCOMPARE(result, patchText); QCOMPARE(result, patchText);
bool ok; bool ok;
QList<FileData> resultList = DiffUtils::readPatch(result, false, &ok); QList<FileData> resultList = DiffUtils::readPatch(result, &ok);
QVERIFY(ok); QVERIFY(ok);
QCOMPARE(resultList.count(), 1); QCOMPARE(resultList.count(), 1);
@@ -905,7 +902,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testReadPatch()
QFETCH(QList<FileData>, fileDataList); QFETCH(QList<FileData>, fileDataList);
bool ok; bool ok;
QList<FileData> result = DiffUtils::readPatch(sourcePatch, false, &ok); QList<FileData> result = DiffUtils::readPatch(sourcePatch, &ok);
QVERIFY(ok); QVERIFY(ok);
QCOMPARE(fileDataList.count(), result.count()); QCOMPARE(fileDataList.count(), result.count());

View File

@@ -33,9 +33,8 @@
namespace DiffEditor { namespace DiffEditor {
DiffEditorReloader::DiffEditorReloader(QObject *parent) DiffEditorReloader::DiffEditorReloader()
: QObject(parent), : m_controller(0),
m_controller(0),
m_reloading(false) m_reloading(false)
{ {
} }

View File

@@ -43,7 +43,7 @@ class DIFFEDITOR_EXPORT DiffEditorReloader : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
DiffEditorReloader(QObject *parent = 0); DiffEditorReloader();
~DiffEditorReloader(); ~DiffEditorReloader();
bool isReloading() const; bool isReloading() const;

View File

@@ -517,7 +517,6 @@ QString DiffUtils::makePatch(const QList<FileData> &fileDataList)
} }
static QList<RowData> readLines(const QString &patch, static QList<RowData> readLines(const QString &patch,
bool ignoreWhitespace,
bool lastChunk, bool lastChunk,
bool *lastChunkAtTheEndOfFile, bool *lastChunkAtTheEndOfFile,
bool *ok) bool *ok)
@@ -678,28 +677,16 @@ static QList<RowData> readLines(const QString &patch,
QList<Diff> outputLeftDiffList; QList<Diff> outputLeftDiffList;
QList<Diff> outputRightDiffList; QList<Diff> outputRightDiffList;
if (ignoreWhitespace) {
const QList<Diff> leftIntermediate =
Differ::moveWhitespaceIntoEqualities(leftDiffList);
const QList<Diff> rightIntermediate =
Differ::moveWhitespaceIntoEqualities(rightDiffList);
Differ::ignoreWhitespaceBetweenEqualities(leftIntermediate,
rightIntermediate,
&outputLeftDiffList,
&outputRightDiffList);
} else {
Differ::diffBetweenEqualities(leftDiffList, Differ::diffBetweenEqualities(leftDiffList,
rightDiffList, rightDiffList,
&outputLeftDiffList, &outputLeftDiffList,
&outputRightDiffList); &outputRightDiffList);
}
return DiffUtils::calculateOriginalData(outputLeftDiffList, return DiffUtils::calculateOriginalData(outputLeftDiffList,
outputRightDiffList).rows; outputRightDiffList).rows;
} }
static QList<ChunkData> readChunks(const QString &patch, static QList<ChunkData> readChunks(const QString &patch,
bool ignoreWhitespace,
bool *lastChunkAtTheEndOfFile, bool *lastChunkAtTheEndOfFile,
bool *ok) bool *ok)
{ {
@@ -728,7 +715,6 @@ static QList<ChunkData> readChunks(const QString &patch,
const QString lines = patch.mid(endOfLastChunk, const QString lines = patch.mid(endOfLastChunk,
pos - endOfLastChunk); pos - endOfLastChunk);
chunkDataList.last().rows = readLines(lines, chunkDataList.last().rows = readLines(lines,
ignoreWhitespace,
false, false,
lastChunkAtTheEndOfFile, lastChunkAtTheEndOfFile,
&readOk); &readOk);
@@ -747,7 +733,6 @@ static QList<ChunkData> readChunks(const QString &patch,
if (endOfLastChunk > 0) { if (endOfLastChunk > 0) {
const QString lines = patch.mid(endOfLastChunk); const QString lines = patch.mid(endOfLastChunk);
chunkDataList.last().rows = readLines(lines, chunkDataList.last().rows = readLines(lines,
ignoreWhitespace,
true, true,
lastChunkAtTheEndOfFile, lastChunkAtTheEndOfFile,
&readOk); &readOk);
@@ -761,7 +746,6 @@ static QList<ChunkData> readChunks(const QString &patch,
} }
static FileData readDiffHeaderAndChunks(const QString &headerAndChunks, static FileData readDiffHeaderAndChunks(const QString &headerAndChunks,
bool ignoreWhitespace,
bool *ok) bool *ok)
{ {
QString patch = headerAndChunks; QString patch = headerAndChunks;
@@ -789,7 +773,6 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks,
fileData.rightFileInfo.fileName = rightFileRegExp.cap(1); fileData.rightFileInfo.fileName = rightFileRegExp.cap(1);
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace,
&fileData.lastChunkAtTheEndOfFile, &fileData.lastChunkAtTheEndOfFile,
&readOk); &readOk);
} }
@@ -811,7 +794,6 @@ static FileData readDiffHeaderAndChunks(const QString &headerAndChunks,
} }
static QList<FileData> readDiffPatch(const QString &patch, static QList<FileData> readDiffPatch(const QString &patch,
bool ignoreWhitespace,
bool *ok) bool *ok)
{ {
const QRegExp diffRegExp(QLatin1String("(?:\\n|^)" // new line of the beginning of a patch const QRegExp diffRegExp(QLatin1String("(?:\\n|^)" // new line of the beginning of a patch
@@ -844,7 +826,6 @@ static QList<FileData> readDiffPatch(const QString &patch,
pos - lastPos); pos - lastPos);
const FileData fileData = readDiffHeaderAndChunks(headerAndChunks, const FileData fileData = readDiffHeaderAndChunks(headerAndChunks,
ignoreWhitespace,
&readOk); &readOk);
if (!readOk) if (!readOk)
@@ -861,7 +842,6 @@ static QList<FileData> readDiffPatch(const QString &patch,
patch.count() - lastPos - 1); patch.count() - lastPos - 1);
const FileData fileData = readDiffHeaderAndChunks(headerAndChunks, const FileData fileData = readDiffHeaderAndChunks(headerAndChunks,
ignoreWhitespace,
&readOk); &readOk);
if (readOk) if (readOk)
@@ -880,7 +860,6 @@ static QList<FileData> readDiffPatch(const QString &patch,
static FileData readGitHeaderAndChunks(const QString &headerAndChunks, static FileData readGitHeaderAndChunks(const QString &headerAndChunks,
const QString &fileName, const QString &fileName,
bool ignoreWhitespace,
bool *ok) bool *ok)
{ {
FileData fileData; FileData fileData;
@@ -944,7 +923,6 @@ static FileData readGitHeaderAndChunks(const QString &headerAndChunks,
patch.remove(0, rightFileRegExp.matchedLength()); patch.remove(0, rightFileRegExp.matchedLength());
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace,
&fileData.lastChunkAtTheEndOfFile, &fileData.lastChunkAtTheEndOfFile,
&readOk); &readOk);
} }
@@ -966,7 +944,6 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
FileData::FileOperation fileOperation, FileData::FileOperation fileOperation,
const QString &leftFileName, const QString &leftFileName,
const QString &rightFileName, const QString &rightFileName,
bool ignoreWhitespace,
bool *ok) bool *ok)
{ {
FileData fileData; FileData fileData;
@@ -1005,7 +982,6 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
patch.remove(0, rightFileRegExp.matchedLength()); patch.remove(0, rightFileRegExp.matchedLength());
fileData.chunks = readChunks(patch, fileData.chunks = readChunks(patch,
ignoreWhitespace,
&fileData.lastChunkAtTheEndOfFile, &fileData.lastChunkAtTheEndOfFile,
&readOk); &readOk);
} }
@@ -1024,7 +1000,7 @@ static FileData readCopyRenameChunks(const QString &copyRenameChunks,
return fileData; return fileData;
} }
static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace, bool *ok) static QList<FileData> readGitPatch(const QString &patch, bool *ok)
{ {
const QRegExp simpleGitRegExp(QLatin1String("(?:\\n|^)diff --git a/([^\\n]+) b/\\1\\n")); // diff --git a/cap1 b/cap1 const QRegExp simpleGitRegExp(QLatin1String("(?:\\n|^)diff --git a/([^\\n]+) b/\\1\\n")); // diff --git a/cap1 b/cap1
@@ -1074,14 +1050,12 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace,
if (lastOperation == FileData::ChangeFile) { if (lastOperation == FileData::ChangeFile) {
fileData = readGitHeaderAndChunks(headerAndChunks, fileData = readGitHeaderAndChunks(headerAndChunks,
lastLeftFileName, lastLeftFileName,
ignoreWhitespace,
&readOk); &readOk);
} else { } else {
fileData = readCopyRenameChunks(headerAndChunks, fileData = readCopyRenameChunks(headerAndChunks,
lastOperation, lastOperation,
lastLeftFileName, lastLeftFileName,
lastRightFileName, lastRightFileName,
ignoreWhitespace,
&readOk); &readOk);
} }
if (!readOk) if (!readOk)
@@ -1124,14 +1098,12 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace,
fileData = readGitHeaderAndChunks(headerAndChunks, fileData = readGitHeaderAndChunks(headerAndChunks,
lastLeftFileName, lastLeftFileName,
ignoreWhitespace,
&readOk); &readOk);
} else { } else {
fileData = readCopyRenameChunks(headerAndChunks, fileData = readCopyRenameChunks(headerAndChunks,
lastOperation, lastOperation,
lastLeftFileName, lastLeftFileName,
lastRightFileName, lastRightFileName,
ignoreWhitespace,
&readOk); &readOk);
} }
if (readOk) if (readOk)
@@ -1148,7 +1120,7 @@ static QList<FileData> readGitPatch(const QString &patch, bool ignoreWhitespace,
return fileDataList; return fileDataList;
} }
QList<FileData> DiffUtils::readPatch(const QString &patch, bool ignoreWhitespace, bool *ok) QList<FileData> DiffUtils::readPatch(const QString &patch, bool *ok)
{ {
bool readOk = false; bool readOk = false;
@@ -1161,9 +1133,9 @@ QList<FileData> DiffUtils::readPatch(const QString &patch, bool ignoreWhitespace
if (pos != -1) if (pos != -1)
croppedPatch = patch.left(pos + 1); // crop the ending for git format-patch croppedPatch = patch.left(pos + 1); // crop the ending for git format-patch
fileDataList = readGitPatch(croppedPatch, ignoreWhitespace, &readOk); fileDataList = readGitPatch(croppedPatch, &readOk);
if (!readOk) if (!readOk)
fileDataList = readDiffPatch(croppedPatch, ignoreWhitespace, &readOk); fileDataList = readDiffPatch(croppedPatch, &readOk);
if (ok) if (ok)
*ok = readOk; *ok = readOk;

View File

@@ -148,7 +148,6 @@ public:
bool lastChunk = false); bool lastChunk = false);
static QString makePatch(const QList<FileData> &fileDataList); static QString makePatch(const QList<FileData> &fileDataList);
static QList<FileData> readPatch(const QString &patch, static QList<FileData> readPatch(const QString &patch,
bool ignoreWhitespace,
bool *ok = 0); bool *ok = 0);
}; };

View File

@@ -331,8 +331,7 @@ void GitDiffHandler::slotTextualDiffOutputReceived(const QString &contents)
bool ok; bool ok;
QList<DiffEditor::FileData> fileDataList QList<DiffEditor::FileData> fileDataList
= DiffEditor::DiffUtils::readPatch( = DiffEditor::DiffUtils::readPatch(contents, &ok);
contents, m_controller->isIgnoreWhitespace(), &ok);
m_controller->setDiffFiles(fileDataList, m_workingDirectory); m_controller->setDiffFiles(fileDataList, m_workingDirectory);
m_controller->requestRestoreState(); m_controller->requestRestoreState();
deleteLater(); deleteLater();
@@ -368,7 +367,7 @@ public:
DiffShow DiffShow
}; };
GitDiffEditorReloader(QObject *parent); GitDiffEditorReloader();
void setWorkingDirectory(const QString &workingDir) { void setWorkingDirectory(const QString &workingDir) {
m_workingDirectory = workingDir; m_workingDirectory = workingDir;
} }
@@ -408,9 +407,8 @@ private:
QString m_displayName; QString m_displayName;
}; };
GitDiffEditorReloader::GitDiffEditorReloader(QObject *parent) GitDiffEditorReloader::GitDiffEditorReloader()
: DiffEditorReloader(parent), : m_gitClient(GitPlugin::instance()->gitClient())
m_gitClient(GitPlugin::instance()->gitClient())
{ {
} }
@@ -831,7 +829,7 @@ GitDiffEditorReloader *GitClient::findOrCreateDiffEditor(const QString &document
connect(controller, SIGNAL(expandBranchesRequested(QString)), connect(controller, SIGNAL(expandBranchesRequested(QString)),
this, SLOT(branchesForCommit(QString))); this, SLOT(branchesForCommit(QString)));
reloader = new GitDiffEditorReloader(controller); reloader = new GitDiffEditorReloader();
controller->setReloader(reloader); controller->setReloader(reloader);
reloader->setWorkingDirectory(workingDirectory); reloader->setWorkingDirectory(workingDirectory);