diff --git a/src/plugins/coreplugin/patchtool.cpp b/src/plugins/coreplugin/patchtool.cpp index fa528f83107..f866647f9c5 100644 --- a/src/plugins/coreplugin/patchtool.cpp +++ b/src/plugins/coreplugin/patchtool.cpp @@ -38,13 +38,15 @@ void PatchTool::setPatchCommand(const FilePath &newCommand) s->endGroup(); } -bool PatchTool::confirmPatching(QWidget *parent, PatchAction patchAction) +bool PatchTool::confirmPatching(QWidget *parent, PatchAction patchAction, bool isModified) { const QString title = patchAction == PatchAction::Apply ? Tr::tr("Apply Chunk") : Tr::tr("Revert Chunk"); - const QString question = patchAction == PatchAction::Apply + QString question = patchAction == PatchAction::Apply ? Tr::tr("Would you like to apply the chunk?") : Tr::tr("Would you like to revert the chunk?"); + if (isModified) + question += "\n" + Tr::tr("Note: The file will be saved before this operation."); return QMessageBox::question(parent, title, question, QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes; } diff --git a/src/plugins/coreplugin/patchtool.h b/src/plugins/coreplugin/patchtool.h index 46c82f51345..c68689aae77 100644 --- a/src/plugins/coreplugin/patchtool.h +++ b/src/plugins/coreplugin/patchtool.h @@ -20,7 +20,7 @@ public: static Utils::FilePath patchCommand(); static void setPatchCommand(const Utils::FilePath &newCommand); - static bool confirmPatching(QWidget *parent, PatchAction patchAction); + static bool confirmPatching(QWidget *parent, PatchAction patchAction, bool isModified); // Utility to run the 'patch' command static bool runPatch(const QByteArray &input, const Utils::FilePath &workingDirectory = {}, diff --git a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp index 4d8bee2e4c5..5d71837b335 100644 --- a/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp +++ b/src/plugins/diffeditor/diffeditorwidgetcontroller.cpp @@ -134,9 +134,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i if (!chunkExists(fileIndex, chunkIndex)) return; - if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction)) - return; - const FileData fileData = m_contextFileData.at(fileIndex); const QString fileName = patchAction == PatchAction::Apply ? fileData.fileInfo[LeftSide].fileName @@ -150,7 +147,17 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i : m_document->baseDirectory(); const FilePath absFilePath = workingDirectory.resolvePath(fileName).absoluteFilePath(); + auto textDocument = qobject_cast( + DocumentModel::documentForFilePath(absFilePath)); + const bool isModified = patchBehaviour == DiffFileInfo::PatchFile && + textDocument && textDocument->isModified(); + + if (!PatchTool::confirmPatching(m_diffEditorWidget, patchAction, isModified)) + return; + if (patchBehaviour == DiffFileInfo::PatchFile) { + if (textDocument && !EditorManager::saveDocument(textDocument)) + return; const int strip = m_document->baseDirectory().isEmpty() ? -1 : 0; const QString patch = m_document->makePatch(fileIndex, chunkIndex, {}, patchAction); @@ -163,8 +170,6 @@ void DiffEditorWidgetController::patch(PatchAction patchAction, int fileIndex, i workingDirectory, strip, patchAction)) m_document->reload(); } else { // PatchEditor - auto textDocument = qobject_cast( - DocumentModel::documentForFilePath(absFilePath)); if (!textDocument) return; diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index a73b2f44149..4b451ef2f96 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -1578,7 +1578,14 @@ bool VcsBaseEditorWidget::hasDiff() const void VcsBaseEditorWidget::slotApplyDiffChunk(const DiffChunk &chunk, PatchAction patchAction) { - if (!PatchTool::confirmPatching(this, patchAction)) + auto textDocument = qobject_cast( + DocumentModel::documentForFilePath(chunk.fileName)); + const bool isModified = textDocument && textDocument->isModified(); + + if (!PatchTool::confirmPatching(this, patchAction, isModified)) + return; + + if (textDocument && !EditorManager::saveDocument(textDocument)) return; if (applyDiffChunk(chunk, patchAction) && patchAction == PatchAction::Revert)