summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJarek Kobus <jaroslaw.kobus@qt.io>2023-01-12 14:28:44 +0100
committerJarek Kobus <jaroslaw.kobus@qt.io>2023-01-12 13:44:51 +0000
commita661dc5db4e3e613f24add66b9f1dda452d94661 (patch)
tree19a8deaa61967f5493d12d8f680b65f15c3d8ea7
parent8900004b84f8204170ea12ef5735133de029a7bc (diff)
downloadqt-creator-a661dc5db4e3e613f24add66b9f1dda452d94661.tar.gz
DiffEditorPlugin: Avoid code repetition
Change-Id: Ie78bb992ff145a970e1cc03f59f228851fd43b08 Reviewed-by: Orgad Shaneh <orgads@gmail.com>
-rw-r--r--src/plugins/diffeditor/diffeditorplugin.cpp70
1 files changed, 19 insertions, 51 deletions
diff --git a/src/plugins/diffeditor/diffeditorplugin.cpp b/src/plugins/diffeditor/diffeditorplugin.cpp
index f6de25d441..830db67151 100644
--- a/src/plugins/diffeditor/diffeditorplugin.cpp
+++ b/src/plugins/diffeditor/diffeditorplugin.cpp
@@ -364,36 +364,32 @@ static TextDocument *currentTextDocument()
DiffEditorServiceImpl::DiffEditorServiceImpl() = default;
-void DiffEditorServiceImpl::diffFiles(const QString &leftFileName, const QString &rightFileName)
+template <typename Controller, typename... Args>
+void reload(const QString &vcsId, const QString &displayName, Args &&...args)
{
- const QString documentId = Constants::DIFF_EDITOR_PLUGIN
- + QLatin1String(".DiffFiles.") + leftFileName + QLatin1Char('.') + rightFileName;
- const QString title = tr("Diff Files");
auto const document = qobject_cast<DiffEditorDocument *>(
- DiffEditorController::findOrCreateDocument(documentId, title));
+ DiffEditorController::findOrCreateDocument(vcsId, displayName));
if (!document)
return;
-
if (!DiffEditorController::controller(document))
- new DiffExternalFilesController(document, leftFileName, rightFileName);
+ new Controller(document, std::forward<Args>(args)...);
EditorManager::activateEditorForDocument(document);
document->reload();
}
-void DiffEditorServiceImpl::diffModifiedFiles(const QStringList &fileNames)
+void DiffEditorServiceImpl::diffFiles(const QString &leftFileName, const QString &rightFileName)
{
const QString documentId = Constants::DIFF_EDITOR_PLUGIN
- + QLatin1String(".DiffModifiedFiles");
- const QString title = tr("Diff Modified Files");
- auto const document = qobject_cast<DiffEditorDocument *>(
- DiffEditorController::findOrCreateDocument(documentId, title));
- if (!document)
- return;
+ + QLatin1String(".DiffFiles.") + leftFileName + QLatin1Char('.') + rightFileName;
+ const QString title = tr("Diff Files");
+ reload<DiffExternalFilesController>(documentId, title, leftFileName, rightFileName);
+}
- if (!DiffEditorController::controller(document))
- new DiffModifiedFilesController(document, fileNames);
- EditorManager::activateEditorForDocument(document);
- document->reload();
+void DiffEditorServiceImpl::diffModifiedFiles(const QStringList &fileNames)
+{
+ const QString documentId = Constants::DIFF_EDITOR_PLUGIN + QLatin1String(".DiffModifiedFiles");
+ const QString title = tr("Diff Modified Files");
+ reload<DiffModifiedFilesController>(documentId, title, fileNames);
}
class DiffEditorPluginPrivate : public QObject
@@ -482,38 +478,19 @@ void DiffEditorPluginPrivate::diffCurrentFile()
return;
const QString fileName = textDocument->filePath().toString();
-
if (fileName.isEmpty())
return;
- const QString documentId = Constants::DIFF_EDITOR_PLUGIN
- + QLatin1String(".Diff.") + fileName;
+ const QString documentId = Constants::DIFF_EDITOR_PLUGIN + QLatin1String(".Diff.") + fileName;
const QString title = tr("Diff \"%1\"").arg(fileName);
- auto const document = qobject_cast<DiffEditorDocument *>(
- DiffEditorController::findOrCreateDocument(documentId, title));
- if (!document)
- return;
-
- if (!DiffEditorController::controller(document))
- new DiffCurrentFileController(document, fileName);
- EditorManager::activateEditorForDocument(document);
- document->reload();
+ reload<DiffCurrentFileController>(documentId, title, fileName);
}
void DiffEditorPluginPrivate::diffOpenFiles()
{
- const QString documentId = Constants::DIFF_EDITOR_PLUGIN
- + QLatin1String(".DiffOpenFiles");
+ const QString documentId = Constants::DIFF_EDITOR_PLUGIN + QLatin1String(".DiffOpenFiles");
const QString title = tr("Diff Open Files");
- auto const document = qobject_cast<DiffEditorDocument *>(
- DiffEditorController::findOrCreateDocument(documentId, title));
- if (!document)
- return;
-
- if (!DiffEditorController::controller(document))
- new DiffOpenFilesController(document);
- EditorManager::activateEditorForDocument(document);
- document->reload();
+ reload<DiffOpenFilesController>(documentId, title);
}
void DiffEditorPluginPrivate::diffExternalFiles()
@@ -523,7 +500,6 @@ void DiffEditorPluginPrivate::diffExternalFiles()
return;
if (EditorManager::skipOpeningBigTextFile(filePath1))
return;
-
const FilePath filePath2 = FileUtils::getOpenFilePath(nullptr, tr("Select Second File for Diff"));
if (filePath2.isEmpty())
return;
@@ -533,15 +509,7 @@ void DiffEditorPluginPrivate::diffExternalFiles()
const QString documentId = QLatin1String(Constants::DIFF_EDITOR_PLUGIN)
+ ".DiffExternalFiles." + filePath1.toString() + '.' + filePath2.toString();
const QString title = tr("Diff \"%1\", \"%2\"").arg(filePath1.toString(), filePath2.toString());
- auto const document = qobject_cast<DiffEditorDocument *>(
- DiffEditorController::findOrCreateDocument(documentId, title));
- if (!document)
- return;
-
- if (!DiffEditorController::controller(document))
- new DiffExternalFilesController(document, filePath1.toString(), filePath2.toString());
- EditorManager::activateEditorForDocument(document);
- document->reload();
+ reload<DiffExternalFilesController>(documentId, title, filePath1.toString(), filePath2.toString());
}
static DiffEditorPlugin *s_instance = nullptr;