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