Commit 20b836f5 authored by Orgad Shaneh's avatar Orgad Shaneh Committed by Orgad Shaneh

DiffEditor: Simplify makePatch

Store indices in controller and avoid passing them around.

Change-Id: I49c80cb6cf6734a18f80ad5c7c441973d246708f
Reviewed-by: default avatarJarek Kobus <jaroslaw.kobus@digia.com>
parent b01a6a46
......@@ -43,6 +43,8 @@ namespace DiffEditor {
DiffEditorController::DiffEditorController(QObject *parent)
: QObject(parent),
m_diffFileIndex(-1),
m_chunkIndex(-1),
m_descriptionEnabled(false),
m_contextLinesNumber(3),
m_ignoreWhitespace(true),
......@@ -99,30 +101,33 @@ bool DiffEditorController::isIgnoreWhitespace() const
return m_ignoreWhitespace;
}
QString DiffEditorController::makePatch(int diffFileIndex,
int chunkIndex,
bool revert) const
QString DiffEditorController::makePatch(bool revert, bool addPrefix) const
{
if (diffFileIndex < 0 || chunkIndex < 0)
if (m_diffFileIndex < 0 || m_chunkIndex < 0)
return QString();
if (diffFileIndex >= m_diffFiles.count())
if (m_diffFileIndex >= m_diffFiles.count())
return QString();
const FileData fileData = m_diffFiles.at(diffFileIndex);
if (chunkIndex >= fileData.chunks.count())
const FileData fileData = m_diffFiles.at(m_diffFileIndex);
if (m_chunkIndex >= fileData.chunks.count())
return QString();
const ChunkData chunkData = fileData.chunks.at(chunkIndex);
const bool lastChunk = (chunkIndex == fileData.chunks.count() - 1);
const ChunkData chunkData = fileData.chunks.at(m_chunkIndex);
const bool lastChunk = (m_chunkIndex == fileData.chunks.count() - 1);
const QString fileName = revert
? fileData.rightFileInfo.fileName
: fileData.leftFileInfo.fileName;
QString leftPrefix, rightPrefix;
if (addPrefix) {
leftPrefix = QLatin1String("a/");
rightPrefix = QLatin1String("b/");
}
return DiffUtils::makePatch(chunkData,
fileName,
fileName,
leftPrefix + fileName,
rightPrefix + fileName,
lastChunk && fileData.lastChunkAtTheEndOfFile);
}
......@@ -269,7 +274,9 @@ void DiffEditorController::requestChunkActions(QMenu *menu,
int diffFileIndex,
int chunkIndex)
{
emit chunkActionsRequested(menu, diffFileIndex, chunkIndex);
m_diffFileIndex = diffFileIndex;
m_chunkIndex = chunkIndex;
emit chunkActionsRequested(menu, diffFileIndex >= 0 && chunkIndex >= 0);
}
void DiffEditorController::requestSaveState()
......
......@@ -55,7 +55,7 @@ public:
int contextLinesNumber() const;
bool isIgnoreWhitespace() const;
QString makePatch(int diffFileIndex, int chunkIndex, bool revert) const;
QString makePatch(bool revert, bool addPrefix = false) const;
DiffEditorReloader *reloader() const;
void setReloader(DiffEditorReloader *reloader);
......@@ -86,9 +86,7 @@ signals:
void descriptionEnablementChanged(bool on);
void contextLinesNumberChanged(int lines);
void ignoreWhitespaceChanged(bool ignore);
void chunkActionsRequested(QMenu *menu,
int diffFileIndex,
int chunkIndex);
void chunkActionsRequested(QMenu *menu, bool isValid);
void saveStateRequested();
void restoreStateRequested();
void expandBranchesRequested(const QString &revision);
......@@ -99,6 +97,8 @@ private:
QString m_clearMessage;
QList<FileData> m_diffFiles;
int m_diffFileIndex;
int m_chunkIndex;
QString m_workingDirectory;
QString m_description;
bool m_descriptionEnabled;
......
......@@ -1319,9 +1319,7 @@ void SideBySideDiffEditorWidget::slotSendChunkToCodePaster()
if (m_contextMenuChunkIndex >= fileData.chunks.count())
return;
const QString patch = m_controller->makePatch(m_contextMenuFileIndex,
m_contextMenuChunkIndex,
false);
const QString patch = m_controller->makePatch(false);
if (patch.isEmpty())
return;
......@@ -1341,27 +1339,27 @@ void SideBySideDiffEditorWidget::slotSendChunkToCodePaster()
void SideBySideDiffEditorWidget::slotApplyChunk()
{
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, false);
patch(false);
}
void SideBySideDiffEditorWidget::slotRevertChunk()
{
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, true);
patch(true);
}
void SideBySideDiffEditorWidget::patch(int diffFileIndex, int chunkIndex, bool revert)
void SideBySideDiffEditorWidget::patch(bool revert)
{
if (!m_controller)
return;
if (diffFileIndex < 0 || chunkIndex < 0)
if (m_contextMenuFileIndex < 0 || m_contextMenuChunkIndex < 0)
return;
if (diffFileIndex >= m_contextFileData.count())
if (m_contextMenuFileIndex >= m_contextFileData.count())
return;
const FileData fileData = m_contextFileData.at(diffFileIndex);
if (chunkIndex >= fileData.chunks.count())
const FileData fileData = m_contextFileData.at(m_contextMenuFileIndex);
if (m_contextMenuChunkIndex >= fileData.chunks.count())
return;
const QString title = revert ? tr("Revert Chunk") : tr("Apply Chunk");
......@@ -1385,7 +1383,7 @@ void SideBySideDiffEditorWidget::patch(int diffFileIndex, int chunkIndex, bool r
? QFileInfo(fileName).absolutePath()
: m_controller->workingDirectory();
const QString patch = m_controller->makePatch(diffFileIndex, chunkIndex, revert);
const QString patch = m_controller->makePatch(revert);
if (patch.isEmpty())
return;
......
......@@ -94,7 +94,7 @@ private:
// void synchronizeFoldings(SideDiffEditorWidget *source, SideDiffEditorWidget *destination);
void jumpToOriginalFile(const QString &fileName,
int lineNumber, int columnNumber);
void patch(int diffFileIndex, int chunkIndex, bool revert);
void patch(bool revert);
DiffEditorGuiController *m_guiController;
DiffEditorController *m_controller;
......
......@@ -298,9 +298,7 @@ void UnifiedDiffEditorWidget::slotSendChunkToCodePaster()
if (m_contextMenuChunkIndex >= fileData.chunks.count())
return;
const QString patch = m_controller->makePatch(m_contextMenuFileIndex,
m_contextMenuChunkIndex,
false);
const QString patch = m_controller->makePatch(false);
if (patch.isEmpty())
return;
......@@ -321,27 +319,27 @@ void UnifiedDiffEditorWidget::slotSendChunkToCodePaster()
void UnifiedDiffEditorWidget::slotApplyChunk()
{
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, false);
patch(false);
}
void UnifiedDiffEditorWidget::slotRevertChunk()
{
patch(m_contextMenuFileIndex, m_contextMenuChunkIndex, true);
patch(true);
}
void UnifiedDiffEditorWidget::patch(int diffFileIndex, int chunkIndex, bool revert)
void UnifiedDiffEditorWidget::patch(bool revert)
{
if (!m_controller)
return;
if (diffFileIndex < 0 || chunkIndex < 0)
if (m_contextMenuFileIndex < 0 || m_contextMenuChunkIndex < 0)
return;
if (diffFileIndex >= m_contextFileData.count())
if (m_contextMenuFileIndex >= m_contextFileData.count())
return;
const FileData fileData = m_contextFileData.at(diffFileIndex);
if (chunkIndex >= fileData.chunks.count())
const FileData fileData = m_contextFileData.at(m_contextMenuFileIndex);
if (m_contextMenuChunkIndex >= fileData.chunks.count())
return;
const QString title = revert ? tr("Revert Chunk") : tr("Apply Chunk");
......@@ -364,9 +362,7 @@ void UnifiedDiffEditorWidget::patch(int diffFileIndex, int chunkIndex, bool reve
? QFileInfo(fileName).absolutePath()
: m_controller->workingDirectory();
const QString patch = m_controller->makePatch(diffFileIndex,
chunkIndex,
revert);
const QString patch = m_controller->makePatch(revert);
if (patch.isEmpty())
return;
......
......@@ -112,7 +112,7 @@ private:
void addContextMenuActions(QMenu *menu,
int diffFileIndex,
int chunkIndex);
void patch(int diffFileIndex, int chunkIndex, bool revert);
void patch(bool revert);
DiffEditorGuiController *m_guiController;
DiffEditorController *m_controller;
......
......@@ -731,9 +731,7 @@ GitClient::GitClient(GitSettings *settings) :
m_cachedGitVersion(0),
m_msgWait(tr("Waiting for data...")),
m_settings(settings),
m_disableEditor(false),
m_contextDiffFileIndex(-1),
m_contextChunkIndex(-1)
m_disableEditor(false)
{
QTC_CHECK(settings);
connect(ICore::instance(), SIGNAL(saveSettingsRequested()), this, SLOT(saveSettings()));
......@@ -823,8 +821,8 @@ GitDiffEditorReloader *GitClient::findOrCreateDiffEditor(const QString &document
QTC_ASSERT(diffEditorDocument, return 0);
controller = diffEditorDocument->controller();
connect(controller, SIGNAL(chunkActionsRequested(QMenu*,int,int)),
this, SLOT(slotChunkActionsRequested(QMenu*,int,int)), Qt::DirectConnection);
connect(controller, SIGNAL(chunkActionsRequested(QMenu*,bool)),
this, SLOT(slotChunkActionsRequested(QMenu*,bool)), Qt::DirectConnection);
connect(controller, SIGNAL(expandBranchesRequested(QString)),
this, SLOT(branchesForCommit(QString)));
......@@ -839,7 +837,7 @@ GitDiffEditorReloader *GitClient::findOrCreateDiffEditor(const QString &document
return reloader;
}
void GitClient::slotChunkActionsRequested(QMenu *menu, int diffFileIndex, int chunkIndex)
void GitClient::slotChunkActionsRequested(QMenu *menu, bool isValid)
{
menu->addSeparator();
QAction *stageChunkAction = menu->addAction(tr("Stage Chunk"));
......@@ -847,53 +845,20 @@ void GitClient::slotChunkActionsRequested(QMenu *menu, int diffFileIndex, int ch
QAction *unstageChunkAction = menu->addAction(tr("Unstage Chunk"));
connect(unstageChunkAction, SIGNAL(triggered()), this, SLOT(slotUnstageChunk()));
m_contextDiffFileIndex = diffFileIndex;
m_contextChunkIndex = chunkIndex;
m_contextController = qobject_cast<DiffEditor::DiffEditorController *>(sender());
if (m_contextDiffFileIndex < 0 || m_contextChunkIndex < 0 || !m_contextController) {
if (!isValid || !m_contextController) {
stageChunkAction->setEnabled(false);
unstageChunkAction->setEnabled(false);
}
}
QString GitClient::makePatch(int diffFileIndex, int chunkIndex, bool revert) const
{
if (m_contextController.isNull())
return QString();
if (diffFileIndex < 0 || chunkIndex < 0)
return QString();
QList<DiffEditor::FileData> fileDataList = m_contextController->diffFiles();
if (diffFileIndex >= fileDataList.count())
return QString();
const DiffEditor::FileData fileData = fileDataList.at(diffFileIndex);
if (chunkIndex >= fileData.chunks.count())
return QString();
const DiffEditor::ChunkData chunkData = fileData.chunks.at(chunkIndex);
const bool lastChunk = (chunkIndex == fileData.chunks.count() - 1);
const QString fileName = revert
? fileData.rightFileInfo.fileName
: fileData.leftFileInfo.fileName;
return DiffEditor::DiffUtils::makePatch(chunkData,
QLatin1String("a/") + fileName,
QLatin1String("b/") + fileName,
lastChunk && fileData.lastChunkAtTheEndOfFile);
}
void GitClient::slotStageChunk()
{
if (m_contextController.isNull())
return;
const QString patch = makePatch(m_contextDiffFileIndex,
m_contextChunkIndex, false);
const QString patch = m_contextController->makePatch(false, true);
if (patch.isEmpty())
return;
......@@ -905,8 +870,7 @@ void GitClient::slotUnstageChunk()
if (m_contextController.isNull())
return;
const QString patch = makePatch(m_contextDiffFileIndex,
m_contextChunkIndex, true);
const QString patch = m_contextController->makePatch(true, true);
if (patch.isEmpty())
return;
......
......@@ -353,13 +353,12 @@ private slots:
QString change, int lineNumber);
void finishSubmoduleUpdate();
void fetchFinished(const QVariant &cookie);
void slotChunkActionsRequested(QMenu *menu, int diffFileIndex, int chunkIndex);
void slotChunkActionsRequested(QMenu *menu, bool isValid);
void slotStageChunk();
void slotUnstageChunk();
void branchesForCommit(const QString &revision);
private:
QString makePatch(int diffFileIndex, int chunkIndex, bool revert) const;
void stage(const QString &patch, bool revert);
QByteArray readConfigBytes(const QString &workingDirectory, const QString &configVar) const;
QTextCodec *getSourceCodec(const QString &file) const;
......@@ -442,8 +441,6 @@ private:
QMap<QString, StashInfo> m_stashInfo;
QStringList m_updatedSubmodules;
bool m_disableEditor;
int m_contextDiffFileIndex;
int m_contextChunkIndex;
QPointer<DiffEditor::DiffEditorController> m_contextController;
QFutureSynchronizer<void> m_synchronizer; // for commit updates
};
......
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