diff --git a/src/plugins/coreplugin/patchtool.cpp b/src/plugins/coreplugin/patchtool.cpp index 6c954af9e82..fa528f83107 100644 --- a/src/plugins/coreplugin/patchtool.cpp +++ b/src/plugins/coreplugin/patchtool.cpp @@ -1,14 +1,15 @@ // Copyright (C) 2016 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 -#include "patchtool.h" -#include "messagemanager.h" +#include "coreplugintr.h" #include "icore.h" +#include "messagemanager.h" +#include "patchtool.h" #include #include -#include +#include using namespace Utils; @@ -37,22 +38,30 @@ void PatchTool::setPatchCommand(const FilePath &newCommand) s->endGroup(); } +bool PatchTool::confirmPatching(QWidget *parent, PatchAction patchAction) +{ + const QString title = patchAction == PatchAction::Apply ? Tr::tr("Apply Chunk") + : Tr::tr("Revert Chunk"); + const QString question = patchAction == PatchAction::Apply + ? Tr::tr("Would you like to apply the chunk?") + : Tr::tr("Would you like to revert the chunk?"); + return QMessageBox::question(parent, title, question, QMessageBox::Yes | QMessageBox::No) + == QMessageBox::Yes; +} + static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirectory, - int strip, bool reverse, bool withCrlf) + int strip, PatchAction patchAction, bool withCrlf) { const FilePath patch = PatchTool::patchCommand(); if (patch.isEmpty()) { - MessageManager::writeDisrupting(QApplication::translate( - "Core::PatchTool", - "There is no patch-command configured in the general \"Environment\" settings.")); + MessageManager::writeDisrupting(Tr::tr("There is no patch-command configured in " + "the general \"Environment\" settings.")); return false; } if (!patch.exists() && !patch.searchInPath().exists()) { - MessageManager::writeDisrupting( - QApplication::translate("Core::PatchTool", - "The patch-command configured in the general \"Environment\" " - "settings does not exist.")); + MessageManager::writeDisrupting(Tr::tr("The patch-command configured in the general " + "\"Environment\" settings does not exist.")); return false; } @@ -70,20 +79,18 @@ static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirec if (strip >= 0) args << ("-p" + QString::number(strip)); - if (reverse) + if (patchAction == PatchAction::Revert) args << "-R"; if (withCrlf) args << "--binary"; - MessageManager::writeDisrupting( - QApplication::translate("Core::PatchTool", "Running in %1: %2 %3") + MessageManager::writeDisrupting(Tr::tr("Running in %1: %2 %3") .arg(workingDirectory.toUserOutput(), patch.toUserOutput(), args.join(' '))); patchProcess.setCommand({patch, args}); patchProcess.setWriteData(input); patchProcess.start(); if (!patchProcess.waitForStarted()) { - MessageManager::writeFlashing( - QApplication::translate("Core::PatchTool", "Unable to launch \"%1\": %2") - .arg(patch.toUserOutput(), patchProcess.errorString())); + MessageManager::writeFlashing(Tr::tr("Unable to launch \"%1\": %2") + .arg(patch.toUserOutput(), patchProcess.errorString())); return false; } @@ -92,9 +99,8 @@ static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirec if (!patchProcess.readDataFromProcess(&stdOut, &stdErr)) { patchProcess.stop(); patchProcess.waitForFinished(); - MessageManager::writeFlashing( - QApplication::translate("Core::PatchTool", "A timeout occurred running \"%1\"") - .arg(patch.toUserOutput())); + MessageManager::writeFlashing(Tr::tr("A timeout occurred running \"%1\"") + .arg(patch.toUserOutput())); return false; } @@ -102,7 +108,7 @@ static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirec if (stdOut.contains("(different line endings)") && !withCrlf) { QByteArray crlfInput = input; crlfInput.replace('\n', "\r\n"); - return runPatchHelper(crlfInput, workingDirectory, strip, reverse, true); + return runPatchHelper(crlfInput, workingDirectory, strip, patchAction, true); } else { MessageManager::writeFlashing(QString::fromLocal8Bit(stdOut)); } @@ -111,24 +117,21 @@ static bool runPatchHelper(const QByteArray &input, const FilePath &workingDirec MessageManager::writeFlashing(QString::fromLocal8Bit(stdErr)); if (patchProcess.exitStatus() != QProcess::NormalExit) { - MessageManager::writeFlashing( - QApplication::translate("Core::PatchTool", "\"%1\" crashed.").arg(patch.toUserOutput())); + MessageManager::writeFlashing(Tr::tr("\"%1\" crashed.").arg(patch.toUserOutput())); return false; } if (patchProcess.exitCode() != 0) { - MessageManager::writeFlashing( - QApplication::translate("Core::PatchTool", "\"%1\" failed (exit code %2).") - .arg(patch.toUserOutput()) - .arg(patchProcess.exitCode())); + MessageManager::writeFlashing(Tr::tr("\"%1\" failed (exit code %2).") + .arg(patch.toUserOutput()).arg(patchProcess.exitCode())); return false; } return true; } bool PatchTool::runPatch(const QByteArray &input, const FilePath &workingDirectory, - int strip, bool reverse) + int strip, PatchAction patchAction) { - return runPatchHelper(input, workingDirectory, strip, reverse, false); + return runPatchHelper(input, workingDirectory, strip, patchAction, false); } } // namespace Core diff --git a/src/plugins/coreplugin/patchtool.h b/src/plugins/coreplugin/patchtool.h index 8f2f198f071..46c82f51345 100644 --- a/src/plugins/coreplugin/patchtool.h +++ b/src/plugins/coreplugin/patchtool.h @@ -1,4 +1,4 @@ -// Copyright (C) 2016 The Qt Company Ltd. +// Copyright (C) 2022 The Qt Company Ltd. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0 #pragma once @@ -9,15 +9,22 @@ namespace Core { +enum class PatchAction { + Apply, + Revert +}; + class CORE_EXPORT PatchTool { public: static Utils::FilePath patchCommand(); static void setPatchCommand(const Utils::FilePath &newCommand); + static bool confirmPatching(QWidget *parent, PatchAction patchAction); + // Utility to run the 'patch' command static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {}, - int strip = 0, bool reverse = false); + int strip = 0, PatchAction patchAction = PatchAction::Apply); }; } // namespace Core diff --git a/src/plugins/diffeditor/diffeditorcontroller.cpp b/src/plugins/diffeditor/diffeditorcontroller.cpp index c3432bc2c6b..ad9e0a9c2d1 100644 --- a/src/plugins/diffeditor/diffeditorcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorcontroller.cpp @@ -13,6 +13,7 @@ #include +using namespace Core; using namespace Utils; namespace DiffEditor { @@ -55,7 +56,8 @@ QString DiffEditorController::makePatch(int fileIndex, int chunkIndex, PatchOptions options) const { return m_document->makePatch(fileIndex, chunkIndex, selection, - options & Revert, options & AddPrefix); + (options & Revert) ? PatchAction::Revert : PatchAction::Apply, + options & AddPrefix); } Core::IDocument *DiffEditorController::findOrCreateDocument(const QString &vcsId, diff --git a/src/plugins/diffeditor/diffeditordocument.cpp b/src/plugins/diffeditor/diffeditordocument.cpp index f69f19573dc..3bad3e3ad3d 100644 --- a/src/plugins/diffeditor/diffeditordocument.cpp +++ b/src/plugins/diffeditor/diffeditordocument.cpp @@ -19,6 +19,7 @@ #include #include +using namespace Core; using namespace Utils; namespace DiffEditor { @@ -63,8 +64,8 @@ static void appendRow(ChunkData *chunk, const RowData &row) chunk->rows.append(row); } -ChunkData DiffEditorDocument::filterChunk(const ChunkData &data, - const ChunkSelection &selection, bool revert) +ChunkData DiffEditorDocument::filterChunk(const ChunkData &data, const ChunkSelection &selection, + PatchAction patchAction) { if (selection.isNull()) return data; @@ -85,13 +86,13 @@ ChunkData DiffEditorDocument::filterChunk(const ChunkData &data, row.line[RightSide] = TextLineData(TextLineData::Separator); appendRow(&chunk, row); - if (revert) { + if (patchAction == PatchAction::Revert) { newRow.line[LeftSide] = newRow.line[RightSide]; newRow.equal = true; appendRow(&chunk, newRow); } } else { // isRightSelected - if (!revert) { + if (patchAction == PatchAction::Apply) { RowData newRow = row; newRow.line[RightSide] = newRow.line[LeftSide]; newRow.equal = true; @@ -102,10 +103,10 @@ ChunkData DiffEditorDocument::filterChunk(const ChunkData &data, appendRow(&chunk, row); } } else { - if (revert) - row.line[LeftSide] = row.line[RightSide]; - else + if (patchAction == PatchAction::Apply) row.line[RightSide] = row.line[LeftSide]; + else + row.line[LeftSide] = row.line[RightSide]; row.equal = true; appendRow(&chunk, row); } @@ -115,9 +116,8 @@ ChunkData DiffEditorDocument::filterChunk(const ChunkData &data, } QString DiffEditorDocument::makePatch(int fileIndex, int chunkIndex, - const ChunkSelection &selection, - bool revert, bool addPrefix, - const QString &overriddenFileName) const + const ChunkSelection &selection, PatchAction patchAction, + bool addPrefix, const QString &overriddenFileName) const { if (fileIndex < 0 || chunkIndex < 0 || fileIndex >= m_diffFiles.count()) return {}; @@ -126,22 +126,16 @@ QString DiffEditorDocument::makePatch(int fileIndex, int chunkIndex, if (chunkIndex >= fileData.chunks.count()) return {}; - const ChunkData chunkData = filterChunk(fileData.chunks.at(chunkIndex), selection, revert); + const ChunkData chunkData = filterChunk(fileData.chunks.at(chunkIndex), selection, patchAction); const bool lastChunk = (chunkIndex == fileData.chunks.count() - 1); const QString fileName = !overriddenFileName.isEmpty() - ? overriddenFileName : revert - ? fileData.fileInfo[RightSide].fileName - : fileData.fileInfo[LeftSide].fileName; + ? overriddenFileName : patchAction == PatchAction::Apply + ? fileData.fileInfo[LeftSide].fileName : fileData.fileInfo[RightSide].fileName; - QString leftPrefix, rightPrefix; - if (addPrefix) { - leftPrefix = "a/"; - rightPrefix = "b/"; - } - return DiffUtils::makePatch(chunkData, - leftPrefix + fileName, - rightPrefix + fileName, + const QString leftFileName = addPrefix ? QString("a/") + fileName : fileName; + const QString rightFileName = addPrefix ? QString("b/") + fileName : fileName; + return DiffUtils::makePatch(chunkData, leftFileName, rightFileName, lastChunk && fileData.lastChunkAtTheEndOfFile); } diff --git a/src/plugins/diffeditor/diffeditordocument.h b/src/plugins/diffeditor/diffeditordocument.h index 8a22801092d..5238c361c3f 100644 --- a/src/plugins/diffeditor/diffeditordocument.h +++ b/src/plugins/diffeditor/diffeditordocument.h @@ -5,6 +5,7 @@ #include "diffutils.h" +#include #include QT_FORWARD_DECLARE_CLASS(QMenu) @@ -31,10 +32,10 @@ public: LoadFailed }; - static ChunkData filterChunk(const ChunkData &data, - const ChunkSelection &selection, bool revert); + static ChunkData filterChunk(const ChunkData &data, const ChunkSelection &selection, + Core::PatchAction patchAction); QString makePatch(int fileIndex, int chunkIndex, const ChunkSelection &selection, - bool revert, bool addPrefix = false, + Core::PatchAction patchAction, bool addPrefix = false, const QString &overriddenFileName = {}) const; void setDiffFiles(const QList &data, const Utils::FilePath &directory, diff --git a/src/plugins/diffeditor/diffeditorplugin.cpp b/src/plugins/diffeditor/diffeditorplugin.cpp index bb9d3259e38..fafc85fb175 100644 --- a/src/plugins/diffeditor/diffeditorplugin.cpp +++ b/src/plugins/diffeditor/diffeditorplugin.cpp @@ -1421,7 +1421,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() QTest::addColumn("chunk"); QTest::addColumn("rows"); QTest::addColumn("selection"); - QTest::addColumn("revert"); + QTest::addColumn("patchAction"); auto createChunk = [] { ChunkData chunk; @@ -1451,7 +1451,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"", "B"}, {"C", "C"} }; - QTest::newRow("one added") << chunk << rows << ChunkSelection() << false; + QTest::newRow("one added") << chunk << rows << ChunkSelection() << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1462,7 +1462,7 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", ""}, {"C", "C"} }; - QTest::newRow("one removed") << chunk << rows << ChunkSelection() << false; + QTest::newRow("one removed") << chunk << rows << ChunkSelection() << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1477,7 +1477,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"", "D"}, {"F", "F"} }; - QTest::newRow("stage selected added") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) << false; + QTest::newRow("stage selected added") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1490,7 +1491,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"C", "C"}, {"E", "E"} }; - QTest::newRow("stage selected added keep changed") << chunk << rows << ChunkSelection({1}, {1}) << false; + QTest::newRow("stage selected added keep changed") << chunk << rows << ChunkSelection({1}, {1}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1507,7 +1509,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"E", "E"}, {"F", "F"} }; - QTest::newRow("stage selected removed") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) << false; + QTest::newRow("stage selected removed") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1523,7 +1526,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"", "D"}, {"F", "F"} }; - QTest::newRow("stage selected added/removed") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) << false; + QTest::newRow("stage selected added/removed") << chunk << rows << ChunkSelection({2, 3}, {2, 3}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1534,7 +1538,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", "C"}, {"D", "D"} }; - QTest::newRow("stage modified row") << chunk << rows << ChunkSelection({1}, {1}) << false; + QTest::newRow("stage modified row") << chunk << rows << ChunkSelection({1}, {1}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1545,7 +1550,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", "C"}, {"D", "D"} }; - QTest::newRow("stage modified and unmodified rows") << chunk << rows << ChunkSelection({0, 1, 2}, {0, 1, 2}) << false; + QTest::newRow("stage modified and unmodified rows") << chunk << rows + << ChunkSelection({0, 1, 2}, {0, 1, 2}) << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1556,7 +1562,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", "C"}, {"D", "D"} }; - QTest::newRow("stage unmodified left rows") << chunk << rows << ChunkSelection({0, 1, 2}, {1}) << false; + QTest::newRow("stage unmodified left rows") << chunk << rows << ChunkSelection({0, 1, 2}, {1}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1567,7 +1574,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", "C"}, {"D", "D"} }; - QTest::newRow("stage unmodified right rows") << chunk << rows << ChunkSelection({1}, {0, 1, 2}) << false; + QTest::newRow("stage unmodified right rows") << chunk << rows << ChunkSelection({1}, {0, 1, 2}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1578,7 +1586,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", ""}, {"D", "D"} }; - QTest::newRow("stage left only") << chunk << rows << ChunkSelection({1}, {}) << false; + QTest::newRow("stage left only") << chunk << rows << ChunkSelection({1}, {}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1590,7 +1599,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"", "C"}, {"D", "D"} }; - QTest::newRow("stage right only") << chunk << rows << ChunkSelection({}, {1}) << false; + QTest::newRow("stage right only") << chunk << rows << ChunkSelection({}, {1}) + << PatchAction::Apply; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1601,7 +1611,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"B", "C"}, {"D", "D"} }; - QTest::newRow("stage modified row and revert") << chunk << rows << ChunkSelection({1}, {1}) << true; + QTest::newRow("stage modified row and revert") << chunk << rows << ChunkSelection({1}, {1}) + << PatchAction::Revert; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1614,7 +1625,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"D", "D"} }; // symmetric to: "stage right only" - QTest::newRow("stage left only and revert") << chunk << rows << ChunkSelection({1}, {}) << true; + QTest::newRow("stage left only and revert") << chunk << rows << ChunkSelection({1}, {}) + << PatchAction::Revert; chunk = createChunk(); appendRow(&chunk, "A", "A"); // 50 @@ -1626,8 +1638,8 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch_data() {"D", "D"} }; // symmetric to: "stage left only" - QTest::newRow("stage right only and revert") << chunk << rows << ChunkSelection({}, {1}) << true; - + QTest::newRow("stage right only and revert") << chunk << rows << ChunkSelection({}, {1}) + << PatchAction::Revert; } void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch() @@ -1635,9 +1647,9 @@ void DiffEditor::Internal::DiffEditorPlugin::testFilterPatch() QFETCH(ChunkData, chunk); QFETCH(ListOfStringPairs, rows); QFETCH(ChunkSelection, selection); - QFETCH(bool, revert); + QFETCH(PatchAction, patchAction); - const ChunkData result = DiffEditorDocument::filterChunk(chunk, selection, revert); + const ChunkData result = DiffEditorDocument::filterChunk(chunk, selection, patchAction); QCOMPARE(result.rows.size(), rows.size()); for (int i = 0; i < rows.size(); ++i) { QCOMPARE(result.rows.at(i).line[LeftSide].text, rows.at(i).first); diff --git a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp index 17733162d90..4d8bee2e4c5 100644 --- a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp @@ -22,7 +22,6 @@ #include #include -#include #include using namespace Core; @@ -127,7 +126,7 @@ void DiffEditorWidgetController::onDocumentReloadFinished() hideProgress(); } -void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkIndex) +void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, int chunkIndex) { if (!m_document) return; @@ -135,24 +134,16 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde if (!chunkExists(fileIndex, chunkIndex)) return; - const QString title = revert ? tr("Revert Chunk") : tr("Apply Chunk"); - const QString question = revert - ? tr("Would you like to revert the chunk?") - : tr("Would you like to apply the chunk?"); - if (QMessageBox::No == QMessageBox::question(m_diffEditorWidget, title, - question, - QMessageBox::Yes - | QMessageBox::No)) { + if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction)) return; - } const FileData fileData = m_contextFileData.at(fileIndex); - const QString fileName = revert - ? fileData.fileInfo[RightSide].fileName - : fileData.fileInfo[LeftSide].fileName; - const DiffFileInfo::PatchBehaviour patchBehaviour = revert - ? fileData.fileInfo[RightSide].patchBehaviour - : fileData.fileInfo[LeftSide].patchBehaviour; + const QString fileName = patchAction == PatchAction::Apply + ? fileData.fileInfo[LeftSide].fileName + : fileData.fileInfo[RightSide].fileName; + const DiffFileInfo::PatchBehaviour patchBehaviour = patchAction == PatchAction::Apply + ? fileData.fileInfo[LeftSide].patchBehaviour + : fileData.fileInfo[RightSide].patchBehaviour; const FilePath workingDirectory = m_document->baseDirectory().isEmpty() ? FilePath::fromString(fileName).absolutePath() @@ -162,14 +153,14 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde if (patchBehaviour == DiffFileInfo::PatchFile) { const int strip = m_document->baseDirectory().isEmpty() ? -1 : 0; - const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, revert); + const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction); if (patch.isEmpty()) return; FileChangeBlocker fileChangeBlocker(absFilePath); if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), - workingDirectory, strip, revert)) + workingDirectory, strip, patchAction)) m_document->reload(); } else { // PatchEditor auto textDocument = qobject_cast( @@ -187,15 +178,14 @@ void DiffEditorWidgetController::patch(bool revert, int fileIndex, int chunkInde const QString contentsCopyFileName = contentsCopy.fileName(); const QString contentsCopyDir = QFileInfo(contentsCopyFileName).absolutePath(); - const QString patch = m_document->makePatch(fileIndex, chunkIndex, - {}, revert, false, + const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction, false, QFileInfo(contentsCopyFileName).fileName()); if (patch.isEmpty()) return; if (PatchTool::runPatch(EditorManager::defaultTextCodec()->fromUnicode(patch), - FilePath::fromString(contentsCopyDir), 0, revert)) { + FilePath::fromString(contentsCopyDir), 0, patchAction)) { QString errorString; if (textDocument->reload(&errorString, FilePath::fromString(contentsCopyFileName))) m_document->reload(); @@ -266,15 +256,17 @@ bool DiffEditorWidgetController::fileNamesAreDifferent(int fileIndex) const return fileData.fileInfo[LeftSide].fileName != fileData.fileInfo[RightSide].fileName; } -void DiffEditorWidgetController::addApplyRevertAction(QMenu *menu, int fileIndex, int chunkIndex, DiffSide side) +void DiffEditorWidgetController::addPatchAction(QMenu *menu, int fileIndex, int chunkIndex, + PatchAction patchAction) { - const QString actionName = side == LeftSide ? tr("Apply Chunk...") : tr("Revert Chunk..."); + const QString actionName = patchAction == PatchAction::Apply ? tr("Apply Chunk...") + : tr("Revert Chunk..."); QAction *action = menu->addAction(actionName); - connect(action, &QAction::triggered, this, [this, fileIndex, chunkIndex, side] { - patch(side == RightSide, fileIndex, chunkIndex); + connect(action, &QAction::triggered, this, [this, fileIndex, chunkIndex, patchAction] { + patch(patchAction, fileIndex, chunkIndex); }); const bool enabled = chunkExists(fileIndex, chunkIndex) - && (side == RightSide || fileNamesAreDifferent(fileIndex)); + && (patchAction == PatchAction::Revert || fileNamesAreDifferent(fileIndex)); action->setEnabled(enabled); } @@ -315,7 +307,7 @@ void DiffEditorWidgetController::sendChunkToCodePaster(int fileIndex, int chunkI auto pasteService = ExtensionSystem::PluginManager::getObject(); QTC_ASSERT(pasteService, return); - const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, false); + const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, PatchAction::Apply); if (patch.isEmpty()) return; diff --git a/src/plugins/diffeditor/diffeditorwidgetcontroller.h b/src/plugins/diffeditor/diffeditorwidgetcontroller.h index 19cc5a30f24..e5d1d49333d 100644 --- a/src/plugins/diffeditor/diffeditorwidgetcontroller.h +++ b/src/plugins/diffeditor/diffeditorwidgetcontroller.h @@ -5,6 +5,7 @@ #include "diffutils.h" +#include #include #include @@ -38,7 +39,7 @@ public: int columnNumber); void setFontSettings(const TextEditor::FontSettings &fontSettings); void addCodePasterAction(QMenu *menu, int fileIndex, int chunkIndex); - void addApplyRevertAction(QMenu *menu, int fileIndex, int chunkIndex, DiffSide side); + void addPatchAction(QMenu *menu, int fileIndex, int chunkIndex, Core::PatchAction patchAction); void addExtraActions(QMenu *menu, int fileIndex, int chunkIndex, const ChunkSelection &selection); void updateCannotDecodeInfo(); void setBusyShowing(bool busy); @@ -60,7 +61,7 @@ private: bool isInProgress() const; void toggleProgress(bool wasInProgress); - void patch(bool revert, int fileIndex, int chunkIndex); + void patch(Core::PatchAction patchAction, int fileIndex, int chunkIndex); void sendChunkToCodePaster(int fileIndex, int chunkIndex); bool chunkExists(int fileIndex, int chunkIndex) const; bool fileNamesAreDifferent(int fileIndex) const; diff --git a/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp b/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp index 165bfa5b4da..d7ab7d6709f 100644 --- a/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp +++ b/src/plugins/diffeditor/sidebysidediffeditorwidget.cpp @@ -1107,8 +1107,9 @@ void SideBySideDiffEditorWidget::contextMenuRequested(DiffSide side, QMenu *menu { menu->addSeparator(); + const PatchAction patchAction = side == LeftSide ? PatchAction::Apply : PatchAction::Revert; m_controller.addCodePasterAction(menu, fileIndex, chunkIndex); - m_controller.addApplyRevertAction(menu, fileIndex, chunkIndex, side); + m_controller.addPatchAction(menu, fileIndex, chunkIndex, patchAction); m_controller.addExtraActions(menu, fileIndex, chunkIndex, selection); } diff --git a/src/plugins/diffeditor/unifieddiffeditorwidget.cpp b/src/plugins/diffeditor/unifieddiffeditorwidget.cpp index 3e38edb9d83..030e74a8b77 100644 --- a/src/plugins/diffeditor/unifieddiffeditorwidget.cpp +++ b/src/plugins/diffeditor/unifieddiffeditorwidget.cpp @@ -218,8 +218,8 @@ void UnifiedDiffEditorWidget::addContextMenuActions(QMenu *menu, int fileIndex, menu->addSeparator(); m_controller.addCodePasterAction(menu, fileIndex, chunkIndex); - m_controller.addApplyRevertAction(menu, fileIndex, chunkIndex, LeftSide); - m_controller.addApplyRevertAction(menu, fileIndex, chunkIndex, RightSide); + m_controller.addPatchAction(menu, fileIndex, chunkIndex, PatchAction::Apply); + m_controller.addPatchAction(menu, fileIndex, chunkIndex, PatchAction::Revert); m_controller.addExtraActions(menu, fileIndex, chunkIndex, selection); } diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp index d3f5264acef..5c5254a8ae5 100644 --- a/src/plugins/git/giteditor.cpp +++ b/src/plugins/git/giteditor.cpp @@ -4,10 +4,8 @@ #include "giteditor.h" #include "annotationhighlighter.h" -#include "branchadddialog.h" #include "gitclient.h" #include "gitsettings.h" -#include "gitsubmiteditorwidget.h" #include "gitconstants.h" #include "githighlighters.h" @@ -32,6 +30,7 @@ #define CHANGE_PATTERN "[a-f0-9]{7,40}" +using namespace Core; using namespace Utils; using namespace VcsBase; @@ -214,7 +213,7 @@ void GitEditorWidget::setPlainText(const QString &text) textDocument()->setPlainText(modText); } -void GitEditorWidget::applyDiffChunk(const DiffChunk& chunk, bool revert) +void GitEditorWidget::applyDiffChunk(const DiffChunk& chunk, PatchAction patchAction) { Utils::TemporaryFile patchFile("git-apply-chunk"); if (!patchFile.open()) @@ -226,7 +225,7 @@ void GitEditorWidget::applyDiffChunk(const DiffChunk& chunk, bool revert) patchFile.close(); QStringList args = {"--cached"}; - if (revert) + if (patchAction == PatchAction::Revert) args << "--reverse"; QString errorMessage; if (GitClient::instance()->synchronousApplyPatch(baseDir, patchFile.fileName(), &errorMessage, args)) { @@ -234,7 +233,7 @@ void GitEditorWidget::applyDiffChunk(const DiffChunk& chunk, bool revert) VcsOutputWindow::append(tr("Chunk successfully staged")); else VcsOutputWindow::append(errorMessage); - if (revert) + if (patchAction == PatchAction::Revert) emit diffChunkReverted(chunk); else emit diffChunkApplied(chunk); @@ -264,12 +263,12 @@ void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk) QAction *stageAction = menu->addAction(tr("Stage Chunk...")); connect(stageAction, &QAction::triggered, this, [this, chunk] { - applyDiffChunk(chunk, false); + applyDiffChunk(chunk, PatchAction::Apply); }); QAction *unstageAction = menu->addAction(tr("Unstage Chunk...")); connect(unstageAction, &QAction::triggered, this, [this, chunk] { - applyDiffChunk(chunk, true); + applyDiffChunk(chunk, PatchAction::Revert); }); } diff --git a/src/plugins/git/giteditor.h b/src/plugins/git/giteditor.h index b56e2398641..8c92a6da659 100644 --- a/src/plugins/git/giteditor.h +++ b/src/plugins/git/giteditor.h @@ -37,7 +37,7 @@ signals: void toggleFilters(bool value); private: - void applyDiffChunk(const VcsBase::DiffChunk& chunk, bool revert); + void applyDiffChunk(const VcsBase::DiffChunk& chunk, Core::PatchAction patchAction); void init() override; void addDiffActions(QMenu *menu, const VcsBase::DiffChunk &chunk) override; diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index aa45edc2710..1d396eb4532 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -41,7 +41,6 @@ #include #include #include -#include #include #include #include @@ -979,12 +978,12 @@ void VcsBaseEditorWidget::contextMenuEvent(QContextMenuEvent *e) // fileNameFromDiffSpecification() works. QAction *applyAction = menu->addAction(tr("Apply Chunk...")); connect(applyAction, &QAction::triggered, this, [this, chunk] { - slotApplyDiffChunk(chunk, false); + slotApplyDiffChunk(chunk, PatchAction::Apply); }); // Revert a chunk from a VCS diff, which might be linked to reloading the diff. QAction *revertAction = menu->addAction(tr("Revert Chunk...")); connect(revertAction, &QAction::triggered, this, [this, chunk] { - slotApplyDiffChunk(chunk, true); + slotApplyDiffChunk(chunk, PatchAction::Revert); }); // Custom diff actions addDiffActions(menu, chunk); @@ -1506,10 +1505,10 @@ bool VcsBaseEditorWidget::canApplyDiffChunk(const DiffChunk &dc) const // Default implementation of revert: Apply a chunk by piping it into patch, // (passing '-R' for revert), assuming we got absolute paths from the VCS plugins. -bool VcsBaseEditorWidget::applyDiffChunk(const DiffChunk &dc, bool revert) const +bool VcsBaseEditorWidget::applyDiffChunk(const DiffChunk &dc, PatchAction patchAction) const { return Core::PatchTool::runPatch(dc.asPatch(d->m_workingDirectory), - FilePath::fromString(d->m_workingDirectory), 0, revert); + FilePath::fromString(d->m_workingDirectory), 0, patchAction); } QString VcsBaseEditorWidget::fileNameFromDiffSpecification(const QTextBlock &inBlock, QString *header) const @@ -1586,16 +1585,13 @@ bool VcsBaseEditorWidget::hasDiff() const } } -void VcsBaseEditorWidget::slotApplyDiffChunk(const DiffChunk &chunk, bool revert) +void VcsBaseEditorWidget::slotApplyDiffChunk(const DiffChunk &chunk, PatchAction patchAction) { - const QString title = revert ? tr("Revert Chunk") : tr("Apply Chunk"); - const QString question = revert ? tr("Would you like to revert the chunk?") - : tr("Would you like to apply the chunk?"); - if (QMessageBox::No == QMessageBox::question(this, title, question, QMessageBox::Yes|QMessageBox::No)) + if (!PatchTool::confirmPatching(this, patchAction)) return; - if (applyDiffChunk(chunk, revert)) { - if (revert) + if (applyDiffChunk(chunk, patchAction)) { + if (patchAction == PatchAction::Revert) // TODO: make just one signal emit diffChunkReverted(chunk); else emit diffChunkApplied(chunk); diff --git a/src/plugins/vcsbase/vcsbaseeditor.h b/src/plugins/vcsbase/vcsbaseeditor.h index 2bb4109ed80..cbda2b9b2e2 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.h +++ b/src/plugins/vcsbase/vcsbaseeditor.h @@ -5,6 +5,7 @@ #include "vcsbase_global.h" +#include #include #include @@ -245,14 +246,14 @@ private: void slotJumpToEntry(int); void slotCursorPositionChanged() override; void slotAnnotateRevision(const QString &change); - void slotApplyDiffChunk(const DiffChunk &chunk, bool revert); + void slotApplyDiffChunk(const DiffChunk &chunk, Core::PatchAction patchAction); void slotPaste(); void showProgressIndicator(); void hideProgressIndicator(); bool canApplyDiffChunk(const DiffChunk &dc) const; // Revert a patch chunk. Default implementation uses patch.exe - bool applyDiffChunk(const DiffChunk &dc, bool revert = false) const; + bool applyDiffChunk(const DiffChunk &dc, Core::PatchAction patchAction) const; // Indicates if the editor has diff contents. If true, an appropriate // highlighter is used and double-click inside a diff chunk jumps to