forked from qt-creator/qt-creator
Project tree: Make code for diff against current file reusable
The same functionality should be available in the file system view. Change-Id: Ib31cb84924e767ebe06f595bf638a5e674d4f8b5 Reviewed-by: André Hartmann <aha_1980@gmx.de> Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
@@ -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>())
|
||||
diffService->diffFiles(leftFileName, rightFileName);
|
||||
}
|
||||
|
||||
void ProjectExplorerPlugin::renameFile(Node *node, const QString &newFilePath)
|
||||
{
|
||||
const QString oldFilePath = node->filePath().toFileInfo().absoluteFilePath();
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -31,6 +31,8 @@
|
||||
|
||||
#include <functional>
|
||||
|
||||
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);
|
||||
|
||||
@@ -36,12 +36,15 @@
|
||||
#include "texteditorconstants.h"
|
||||
#include "typingsettings.h"
|
||||
#include <texteditor/generichighlighter/highlighter.h>
|
||||
#include <coreplugin/diffservice.h>
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <coreplugin/editormanager/documentmodel.h>
|
||||
#include <extensionsystem/pluginmanager.h>
|
||||
#include <utils/textutils.h>
|
||||
#include <utils/guard.h>
|
||||
#include <utils/mimetypes/mimedatabase.h>
|
||||
|
||||
#include <QAction>
|
||||
#include <QApplication>
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
@@ -357,6 +360,22 @@ void TextDocument::setFontSettings(const FontSettings &fontSettings)
|
||||
emit fontSettingsChanged();
|
||||
}
|
||||
|
||||
QAction *TextDocument::createDiffAgainstCurrentFileAction(
|
||||
QObject *parent, const std::function<Utils::FileName()> &filePath)
|
||||
{
|
||||
const auto diffAgainstCurrentFile = [filePath]() {
|
||||
auto diffService = ExtensionSystem::PluginManager::getObject<DiffService>();
|
||||
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)
|
||||
|
||||
@@ -35,6 +35,7 @@
|
||||
#include <QSharedPointer>
|
||||
|
||||
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<Utils::FileName()> &filePath);
|
||||
|
||||
signals:
|
||||
void aboutToOpen(const QString &fileName, const QString &realFileName);
|
||||
void openFinishedSuccessfully();
|
||||
|
||||
Reference in New Issue
Block a user