diff --git a/src/plugins/projectexplorer/projectexplorer.cpp b/src/plugins/projectexplorer/projectexplorer.cpp index a1557ef272d..b15cad5cb83 100644 --- a/src/plugins/projectexplorer/projectexplorer.cpp +++ b/src/plugins/projectexplorer/projectexplorer.cpp @@ -315,7 +315,6 @@ public: void duplicateFile(); void deleteFile(); void handleRenameFile(); - void handleDiffFile(); void handleSetStartupProject(); void setStartupProject(ProjectExplorer::Project *project); @@ -1118,7 +1117,8 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er mfileContextMenu->addAction(cmd, Constants::G_FILE_OTHER); // diff file action - dd->m_diffFileAction = new QAction(tr("Diff Against Current File"), this); + dd->m_diffFileAction = TextEditor::TextDocument::createDiffAgainstCurrentFileAction( + this, &ProjectTree::currentFilePath); cmd = ActionManager::registerAction(dd->m_diffFileAction, Constants::DIFFFILE, projecTreeContext); mfileContextMenu->addAction(cmd, Constants::G_FILE_OTHER); @@ -1362,8 +1362,6 @@ bool ProjectExplorerPlugin::initialize(const QStringList &arguments, QString *er dd, &ProjectExplorerPluginPrivate::deleteFile); connect(dd->m_renameFileAction, &QAction::triggered, dd, &ProjectExplorerPluginPrivate::handleRenameFile); - connect(dd->m_diffFileAction, &QAction::triggered, - dd, &ProjectExplorerPluginPrivate::handleDiffFile); connect(dd->m_setStartupProjectAction, &QAction::triggered, dd, &ProjectExplorerPluginPrivate::handleSetStartupProject); connect(dd->m_projectTreeCollapseAllAction, &QAction::triggered, @@ -3379,39 +3377,6 @@ void ProjectExplorerPluginPrivate::handleRenameFile() } } -void ProjectExplorerPluginPrivate::handleDiffFile() -{ - // current editor's file - auto textDocument = TextEditor::TextDocument::currentTextDocument(); - - if (!textDocument) - return; - - const QString leftFileName = textDocument->filePath().toString(); - - if (leftFileName.isEmpty()) - return; - - // current item's file - Node *currentNode = ProjectTree::findCurrentNode(); - QTC_ASSERT(currentNode && currentNode->nodeType() == NodeType::File, return); - - FileNode *fileNode = currentNode->asFileNode(); - - if (!fileNode) - return; - - const QString rightFileName = currentNode->filePath().toString(); - if (rightFileName.isEmpty()) - return; - - if (!isTextFile(rightFileName)) - return; - - if (auto diffService = ExtensionSystem::PluginManager::getObject()) - diffService->diffFiles(leftFileName, rightFileName); -} - void ProjectExplorerPlugin::renameFile(Node *node, const QString &newFilePath) { const QString oldFilePath = node->filePath().toFileInfo().absoluteFilePath(); diff --git a/src/plugins/projectexplorer/projecttree.cpp b/src/plugins/projectexplorer/projecttree.cpp index 69bbeb8a075..b8db32c3974 100644 --- a/src/plugins/projectexplorer/projecttree.cpp +++ b/src/plugins/projectexplorer/projecttree.cpp @@ -110,6 +110,12 @@ Node *ProjectTree::findCurrentNode() return s_instance->m_currentNode; } +FileName ProjectTree::currentFilePath() +{ + Node *currentNode = findCurrentNode(); + return currentNode ? currentNode->filePath() : FileName(); +} + void ProjectTree::registerWidget(ProjectTreeWidget *widget) { s_instance->m_projectTreeWidgets.append(widget); diff --git a/src/plugins/projectexplorer/projecttree.h b/src/plugins/projectexplorer/projecttree.h index 684d4b731a8..f127b155f74 100644 --- a/src/plugins/projectexplorer/projecttree.h +++ b/src/plugins/projectexplorer/projecttree.h @@ -31,6 +31,8 @@ #include +namespace Utils { class FileName; } + namespace ProjectExplorer { class FileNode; class FolderNode; @@ -52,6 +54,7 @@ public: static Project *currentProject(); static Node *findCurrentNode(); + static Utils::FileName currentFilePath(); // Integration with ProjectTreeWidget static void registerWidget(Internal::ProjectTreeWidget *widget); diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index 9e8befb9391..404007382b4 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -36,12 +36,15 @@ #include "texteditorconstants.h" #include "typingsettings.h" #include +#include #include #include +#include #include #include #include +#include #include #include #include @@ -357,6 +360,22 @@ void TextDocument::setFontSettings(const FontSettings &fontSettings) emit fontSettingsChanged(); } +QAction *TextDocument::createDiffAgainstCurrentFileAction( + QObject *parent, const std::function &filePath) +{ + const auto diffAgainstCurrentFile = [filePath]() { + auto diffService = ExtensionSystem::PluginManager::getObject(); + auto textDocument = TextEditor::TextDocument::currentTextDocument(); + const QString leftFilePath = textDocument ? textDocument->filePath().toString() : QString(); + const QString rightFilePath = filePath().toString(); + if (diffService && !leftFilePath.isEmpty() && !rightFilePath.isEmpty()) + diffService->diffFiles(leftFilePath, rightFilePath); + }; + auto diffAction = new QAction(tr("Diff Against Current File"), parent); + QObject::connect(diffAction, &QAction::triggered, parent, diffAgainstCurrentFile); + return diffAction; +} + void TextDocument::triggerPendingUpdates() { if (d->m_fontSettingsNeedsApply) diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index eecaa838233..65caa2b2e6f 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -35,6 +35,7 @@ #include QT_BEGIN_NAMESPACE +class QAction; class QTextCursor; class QTextDocument; QT_END_NAMESPACE @@ -138,6 +139,9 @@ public: void setTabSettings(const TextEditor::TabSettings &tabSettings); void setFontSettings(const TextEditor::FontSettings &fontSettings); + static QAction *createDiffAgainstCurrentFileAction(QObject *parent, + const std::function &filePath); + signals: void aboutToOpen(const QString &fileName, const QString &realFileName); void openFinishedSuccessfully();