forked from qt-creator/qt-creator
DiffEditor: Refactor DiffEditorController
Replace requestChunkActions() and chunkActionsRequested() with a virtual method addExtraActions(). Implement it in GitBaseDiffEditorController. Task-number: QTCREATORBUG-23242 Change-Id: I5da166b35d1146b9fd439e748803531d982ad2a8 Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -112,6 +112,38 @@ static QString branchesDisplay(const QString &prefix, QStringList *branches, boo
|
||||
|
||||
///////////////////////////////
|
||||
|
||||
static void stage(DiffEditorController *diffController, const QString &patch, bool revert)
|
||||
{
|
||||
TemporaryFile patchFile("git-patchfile");
|
||||
if (!patchFile.open())
|
||||
return;
|
||||
|
||||
const FilePath baseDir = diffController->workingDirectory();
|
||||
QTextCodec *codec = EditorManager::defaultTextCodec();
|
||||
const QByteArray patchData = codec ? codec->fromUnicode(patch) : patch.toLocal8Bit();
|
||||
patchFile.write(patchData);
|
||||
patchFile.close();
|
||||
|
||||
QStringList args = {"--cached"};
|
||||
if (revert)
|
||||
args << "--reverse";
|
||||
QString errorMessage;
|
||||
if (GitClient::instance()->synchronousApplyPatch(baseDir, patchFile.fileName(),
|
||||
&errorMessage, args)) {
|
||||
if (errorMessage.isEmpty()) {
|
||||
if (revert)
|
||||
VcsOutputWindow::appendSilently(Tr::tr("Chunk successfully unstaged"));
|
||||
else
|
||||
VcsOutputWindow::appendSilently(Tr::tr("Chunk successfully staged"));
|
||||
} else {
|
||||
VcsOutputWindow::appendError(errorMessage);
|
||||
}
|
||||
diffController->requestReload();
|
||||
} else {
|
||||
VcsOutputWindow::appendError(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
class GitBaseDiffEditorController : public VcsBaseDiffEditorController
|
||||
{
|
||||
Q_OBJECT
|
||||
@@ -120,6 +152,50 @@ protected:
|
||||
explicit GitBaseDiffEditorController(IDocument *document);
|
||||
|
||||
QStringList addConfigurationArguments(const QStringList &args) const;
|
||||
|
||||
private:
|
||||
void addExtraActions(QMenu *menu, int fileIndex, int chunkIndex,
|
||||
const ChunkSelection &selection) final
|
||||
{
|
||||
menu->addSeparator();
|
||||
|
||||
auto stageChunk = [this, fileIndex, chunkIndex](DiffEditorController::PatchOptions options,
|
||||
const DiffEditor::ChunkSelection &selection) {
|
||||
options |= DiffEditorController::AddPrefix;
|
||||
const QString patch = makePatch(fileIndex, chunkIndex, selection, options);
|
||||
stage(this, patch, options & Revert);
|
||||
};
|
||||
|
||||
QAction *stageChunkAction = menu->addAction(Tr::tr("Stage Chunk"));
|
||||
connect(stageChunkAction, &QAction::triggered, this, [stageChunk] {
|
||||
stageChunk(DiffEditorController::NoOption, {});
|
||||
});
|
||||
QAction *stageLinesAction = menu->addAction(Tr::tr("Stage Selection (%n Lines)", "",
|
||||
selection.selectedRowsCount()));
|
||||
connect(stageLinesAction, &QAction::triggered, this, [stageChunk, selection] {
|
||||
stageChunk(DiffEditorController::NoOption, selection);
|
||||
});
|
||||
QAction *unstageChunkAction = menu->addAction(Tr::tr("Unstage Chunk"));
|
||||
connect(unstageChunkAction, &QAction::triggered, this, [stageChunk] {
|
||||
stageChunk(DiffEditorController::Revert, {});
|
||||
});
|
||||
QAction *unstageLinesAction = menu->addAction(Tr::tr("Unstage Selection (%n Lines)", "",
|
||||
selection.selectedRowsCount()));
|
||||
connect(unstageLinesAction, &QAction::triggered, this, [stageChunk, selection] {
|
||||
stageChunk(DiffEditorController::Revert, selection);
|
||||
});
|
||||
|
||||
if (selection.isNull()) {
|
||||
stageLinesAction->setVisible(false);
|
||||
unstageLinesAction->setVisible(false);
|
||||
}
|
||||
if (!chunkExists(fileIndex, chunkIndex)) {
|
||||
stageChunkAction->setEnabled(false);
|
||||
stageLinesAction->setEnabled(false);
|
||||
unstageChunkAction->setEnabled(false);
|
||||
unstageLinesAction->setEnabled(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class GitDiffEditorController : public GitBaseDiffEditorController
|
||||
@@ -850,95 +926,6 @@ QTextCodec *GitClient::encoding(GitClient::EncodingType encodingType, const File
|
||||
}
|
||||
}
|
||||
|
||||
void GitClient::chunkActionsRequested(DiffEditor::DiffEditorController *controller,
|
||||
QMenu *menu, int fileIndex, int chunkIndex,
|
||||
const DiffEditor::ChunkSelection &selection) const
|
||||
{
|
||||
QPointer<DiffEditor::DiffEditorController> diffController(controller);
|
||||
|
||||
auto stageChunk = [this](QPointer<DiffEditor::DiffEditorController> diffController,
|
||||
int fileIndex, int chunkIndex, DiffEditorController::PatchOptions options,
|
||||
const DiffEditor::ChunkSelection &selection) {
|
||||
if (diffController.isNull())
|
||||
return;
|
||||
|
||||
options |= DiffEditorController::AddPrefix;
|
||||
const QString patch = diffController->makePatch(fileIndex, chunkIndex, selection, options);
|
||||
stage(diffController, patch, options & Revert);
|
||||
};
|
||||
|
||||
menu->addSeparator();
|
||||
QAction *stageChunkAction = menu->addAction(Tr::tr("Stage Chunk"));
|
||||
connect(stageChunkAction, &QAction::triggered, this,
|
||||
[stageChunk, diffController, fileIndex, chunkIndex] {
|
||||
stageChunk(diffController, fileIndex, chunkIndex,
|
||||
DiffEditorController::NoOption, DiffEditor::ChunkSelection());
|
||||
});
|
||||
QAction *stageLinesAction = menu->addAction(Tr::tr("Stage Selection (%n Lines)", "", selection.selectedRowsCount()));
|
||||
connect(stageLinesAction, &QAction::triggered, this,
|
||||
[stageChunk, diffController, fileIndex, chunkIndex, selection] {
|
||||
stageChunk(diffController, fileIndex, chunkIndex,
|
||||
DiffEditorController::NoOption, selection);
|
||||
});
|
||||
QAction *unstageChunkAction = menu->addAction(Tr::tr("Unstage Chunk"));
|
||||
connect(unstageChunkAction, &QAction::triggered, this,
|
||||
[stageChunk, diffController, fileIndex, chunkIndex] {
|
||||
stageChunk(diffController, fileIndex, chunkIndex,
|
||||
DiffEditorController::Revert, DiffEditor::ChunkSelection());
|
||||
});
|
||||
QAction *unstageLinesAction = menu->addAction(Tr::tr("Unstage Selection (%n Lines)", "", selection.selectedRowsCount()));
|
||||
connect(unstageLinesAction, &QAction::triggered, this,
|
||||
[stageChunk, diffController, fileIndex, chunkIndex, selection] {
|
||||
stageChunk(diffController, fileIndex, chunkIndex,
|
||||
DiffEditorController::Revert,
|
||||
selection);
|
||||
});
|
||||
if (selection.isNull()) {
|
||||
stageLinesAction->setVisible(false);
|
||||
unstageLinesAction->setVisible(false);
|
||||
}
|
||||
if (!diffController || !diffController->chunkExists(fileIndex, chunkIndex)) {
|
||||
stageChunkAction->setEnabled(false);
|
||||
stageLinesAction->setEnabled(false);
|
||||
unstageChunkAction->setEnabled(false);
|
||||
unstageLinesAction->setEnabled(false);
|
||||
}
|
||||
}
|
||||
|
||||
void GitClient::stage(DiffEditor::DiffEditorController *diffController,
|
||||
const QString &patch, bool revert) const
|
||||
{
|
||||
TemporaryFile patchFile("git-patchfile");
|
||||
if (!patchFile.open())
|
||||
return;
|
||||
|
||||
const FilePath baseDir = diffController->workingDirectory();
|
||||
QTextCodec *codec = EditorManager::defaultTextCodec();
|
||||
const QByteArray patchData = codec
|
||||
? codec->fromUnicode(patch) : patch.toLocal8Bit();
|
||||
patchFile.write(patchData);
|
||||
patchFile.close();
|
||||
|
||||
QStringList args = {"--cached"};
|
||||
if (revert)
|
||||
args << "--reverse";
|
||||
QString errorMessage;
|
||||
if (synchronousApplyPatch(baseDir, patchFile.fileName(),
|
||||
&errorMessage, args)) {
|
||||
if (errorMessage.isEmpty()) {
|
||||
if (revert)
|
||||
VcsOutputWindow::appendSilently(Tr::tr("Chunk successfully unstaged"));
|
||||
else
|
||||
VcsOutputWindow::appendSilently(Tr::tr("Chunk successfully staged"));
|
||||
} else {
|
||||
VcsOutputWindow::appendError(errorMessage);
|
||||
}
|
||||
diffController->requestReload();
|
||||
} else {
|
||||
VcsOutputWindow::appendError(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
void GitClient::requestReload(const QString &documentId, const FilePath &source,
|
||||
const QString &title, const FilePath &workingDirectory,
|
||||
std::function<GitBaseDiffEditorController *(IDocument *)> factory) const
|
||||
@@ -956,10 +943,6 @@ void GitClient::requestReload(const QString &documentId, const FilePath &source,
|
||||
|
||||
using namespace std::placeholders;
|
||||
|
||||
connect(controller, &DiffEditorController::chunkActionsRequested, this,
|
||||
std::bind(&GitClient::chunkActionsRequested, this, controller, _1, _2, _3, _4),
|
||||
Qt::DirectConnection);
|
||||
|
||||
VcsBase::setSource(document, sourceCopy);
|
||||
EditorManager::activateEditorForDocument(document);
|
||||
controller->requestReload();
|
||||
|
||||
Reference in New Issue
Block a user