DiffEditor: Stage and unstage selected lines for Git

Fixes: QTCREATORBUG-19071
Change-Id: I560ba208e68e477ea865e499847d819cfdfeb6f3
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Andre Hartmann
2017-11-29 21:36:30 +01:00
committed by Jarek Kobus
parent 2758682723
commit 1766832918
15 changed files with 422 additions and 57 deletions

View File

@@ -852,38 +852,58 @@ QTextCodec *GitClient::codecFor(GitClient::CodecType codecType, const QString &s
return nullptr;
}
void GitClient::chunkActionsRequested(QMenu *menu, int fileIndex, int chunkIndex)
void GitClient::chunkActionsRequested(QMenu *menu, int fileIndex, int chunkIndex,
const DiffEditor::ChunkSelection &selection)
{
QPointer<DiffEditor::DiffEditorController> diffController
= qobject_cast<DiffEditorController *>(sender());
auto stageChunk = [this](QPointer<DiffEditor::DiffEditorController> diffController,
int fileIndex, int chunkIndex, bool revert) {
int fileIndex, int chunkIndex, DiffEditorController::PatchOptions options,
const DiffEditor::ChunkSelection &selection) {
if (diffController.isNull())
return;
DiffEditorController::PatchOptions options = DiffEditorController::AddPrefix;
if (revert)
options |= DiffEditorController::Revert;
const QString patch = diffController->makePatch(fileIndex, chunkIndex, options);
stage(diffController, patch, revert);
options |= DiffEditorController::AddPrefix;
const QString patch = diffController->makePatch(fileIndex, chunkIndex, selection, options);
stage(diffController, patch, options & Revert);
};
menu->addSeparator();
QAction *stageChunkAction = menu->addAction(tr("Stage Chunk"));
connect(stageChunkAction, &QAction::triggered, this,
[stageChunk, diffController, fileIndex, chunkIndex]() {
stageChunk(diffController, fileIndex, chunkIndex, false);
stageChunk(diffController, fileIndex, chunkIndex,
DiffEditorController::NoOption, DiffEditor::ChunkSelection());
});
QAction *stageLinesAction = menu->addAction(tr("Stage %n Line(s)", "", selection.selectedRowsCount));
connect(stageLinesAction, &QAction::triggered, this,
[stageChunk, diffController, fileIndex, chunkIndex, selection]() {
stageChunk(diffController, fileIndex, chunkIndex,
DiffEditorController::NoOption, selection);
});
QAction *unstageChunkAction = menu->addAction(tr("Unstage Chunk"));
connect(unstageChunkAction, &QAction::triggered, this,
[stageChunk, diffController, fileIndex, chunkIndex]() {
stageChunk(diffController, fileIndex, chunkIndex, true);
stageChunk(diffController, fileIndex, chunkIndex,
DiffEditorController::Revert, DiffEditor::ChunkSelection());
});
QAction *unstageLinesAction = menu->addAction(tr("Unstage %n Line(s)", "", 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);
}
}