DiffEditor: Use AsyncTask

Change-Id: Id5b21b10c7a8805b560eb4fb36fd955b7af34afa
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Jarek Kobus
2022-10-28 14:53:17 +02:00
parent 30eac65e09
commit b414268427
5 changed files with 40 additions and 51 deletions

View File

@@ -3,18 +3,11 @@
#pragma once
#include "diffeditor_global.h"
#include <coreplugin/diffservice.h>
#include <extensionsystem/iplugin.h>
namespace Utils { class FutureSynchronizer; }
QT_BEGIN_NAMESPACE
template <typename T>
class QFuture;
QT_END_NAMESPACE
namespace DiffEditor {
namespace Internal {

View File

@@ -25,8 +25,7 @@
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditorsettings.h>
#include <utils/futuresynchronizer.h>
#include <utils/runextensions.h>
#include <utils/asynctask.h>
#include <utils/tooltip/tooltip.h>
using namespace Core;
@@ -781,13 +780,7 @@ SideBySideDiffEditorWidget::SideBySideDiffEditorWidget(QWidget *parent)
setFocusProxy(m_editor[LeftSide]);
}
SideBySideDiffEditorWidget::~SideBySideDiffEditorWidget()
{
if (m_watcher) {
m_watcher->cancel();
DiffEditorPlugin::futureSynchronizer()->addFuture(m_watcher->future());
}
}
SideBySideDiffEditorWidget::~SideBySideDiffEditorWidget() = default;
TextEditorWidget *SideBySideDiffEditorWidget::sideEditorWidget(DiffSide side) const
{
@@ -815,10 +808,8 @@ void SideBySideDiffEditorWidget::clear(const QString &message)
setDiff({});
for (SideDiffEditorWidget *editor : m_editor)
editor->clearAll(message);
if (m_watcher) {
m_watcher->cancel();
DiffEditorPlugin::futureSynchronizer()->addFuture(m_watcher->future());
m_watcher.reset();
if (m_asyncTask) {
m_asyncTask.reset();
m_controller.setBusyShowing(false);
}
}
@@ -878,15 +869,16 @@ void SideBySideDiffEditorWidget::restoreState()
void SideBySideDiffEditorWidget::showDiff()
{
m_watcher.reset(new QFutureWatcher<ShowResults>());
m_asyncTask.reset(new AsyncTask<ShowResults>());
m_asyncTask->setFutureSynchronizer(DiffEditorPlugin::futureSynchronizer());
m_controller.setBusyShowing(true);
connect(m_watcher.get(), &QFutureWatcherBase::finished, this, [this] {
if (m_watcher->isCanceled()) {
connect(m_asyncTask.get(), &AsyncTaskBase::done, this, [this] {
if (m_asyncTask->isCanceled()) {
for (SideDiffEditorWidget *editor : m_editor)
editor->clearAll(tr("Retrieving data failed."));
} else {
const ShowResults results = m_watcher->result();
const ShowResults results = m_asyncTask->result();
m_editor[LeftSide]->setDiffData(results[LeftSide].diffData);
m_editor[RightSide]->setDiffData(results[RightSide].diffData);
TextDocumentPtr leftDoc(results[LeftSide].textDocument);
@@ -916,7 +908,7 @@ void SideBySideDiffEditorWidget::showDiff()
m_editor[RightSide]->setSelections(results[RightSide].selections);
setCurrentDiffFileIndex(m_controller.currentDiffFileIndex());
}
m_watcher.release()->deleteLater();
m_asyncTask.release()->deleteLater();
m_controller.setBusyShowing(false);
});
@@ -985,8 +977,9 @@ void SideBySideDiffEditorWidget::showDiff()
futureInterface.reportResult(result);
};
m_watcher->setFuture(runAsync(getDocument));
ProgressManager::addTask(m_watcher->future(), tr("Rendering diff"), "DiffEditor");
m_asyncTask->setAsyncCallData(getDocument);
m_asyncTask->start();
ProgressManager::addTask(m_asyncTask->future(), tr("Rendering diff"), "DiffEditor");
}
void SideBySideDiffEditorWidget::setFontSettings(const FontSettings &fontSettings)

View File

@@ -7,7 +7,7 @@
#include "diffeditorwidgetcontroller.h"
#include "selectabletexteditorwidget.h" // TODO: we need DiffSelections here only
#include <QFutureWatcher>
#include <QFutureInterface>
#include <QWidget>
namespace Core { class IContext; }
@@ -17,6 +17,11 @@ class FontSettings;
class TextEditorWidget;
}
namespace Utils {
template <typename R>
class AsyncTask;
}
QT_BEGIN_NAMESPACE
class QMenu;
class QSplitter;
@@ -138,7 +143,7 @@ private:
};
using ShowResults = std::array<ShowResult, SideCount>;
std::unique_ptr<QFutureWatcher<ShowResults>> m_watcher;
std::unique_ptr<Utils::AsyncTask<ShowResults>> m_asyncTask;
};
} // namespace Internal

View File

@@ -21,9 +21,8 @@
#include <texteditor/textdocumentlayout.h>
#include <texteditor/texteditorsettings.h>
#include <utils/futuresynchronizer.h>
#include <utils/asynctask.h>
#include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/tooltip/tooltip.h>
using namespace Core;
@@ -54,13 +53,7 @@ UnifiedDiffEditorWidget::UnifiedDiffEditorWidget(QWidget *parent)
Core::ICore::addContextObject(context);
}
UnifiedDiffEditorWidget::~UnifiedDiffEditorWidget()
{
if (m_watcher) {
m_watcher->cancel();
DiffEditorPlugin::futureSynchronizer()->addFuture(m_watcher->future());
}
}
UnifiedDiffEditorWidget::~UnifiedDiffEditorWidget() = default;
void UnifiedDiffEditorWidget::setDocument(DiffEditorDocument *document)
{
@@ -203,10 +196,8 @@ void UnifiedDiffEditorWidget::clear(const QString &message)
{
m_data = {};
setSelections({});
if (m_watcher) {
m_watcher->cancel();
DiffEditorPlugin::futureSynchronizer()->addFuture(m_watcher->future());
m_watcher.reset();
if (m_asyncTask) {
m_asyncTask.reset();
m_controller.setBusyShowing(false);
}
@@ -462,13 +453,14 @@ void UnifiedDiffEditorWidget::showDiff()
return;
}
m_watcher.reset(new QFutureWatcher<ShowResult>());
m_asyncTask.reset(new AsyncTask<ShowResult>());
m_asyncTask->setFutureSynchronizer(DiffEditorPlugin::futureSynchronizer());
m_controller.setBusyShowing(true);
connect(m_watcher.get(), &QFutureWatcherBase::finished, this, [this] {
if (m_watcher->isCanceled()) {
connect(m_asyncTask.get(), &AsyncTaskBase::done, this, [this] {
if (m_asyncTask->isCanceled()) {
setPlainText(tr("Retrieving data failed."));
} else {
const ShowResult result = m_watcher->result();
const ShowResult result = m_asyncTask->result();
m_data = result.diffData;
TextDocumentPtr doc(result.textDocument);
{
@@ -482,7 +474,7 @@ void UnifiedDiffEditorWidget::showDiff()
setSelections(result.selections);
setCurrentDiffFileIndex(m_controller.currentDiffFileIndex());
}
m_watcher.release()->deleteLater();
m_asyncTask.release()->deleteLater();
m_controller.setBusyShowing(false);
});
@@ -535,8 +527,9 @@ void UnifiedDiffEditorWidget::showDiff()
futureInterface.reportResult(result);
};
m_watcher->setFuture(runAsync(getDocument));
ProgressManager::addTask(m_watcher->future(), tr("Rendering diff"), "DiffEditor");
m_asyncTask->setAsyncCallData(getDocument);
m_asyncTask->start();
ProgressManager::addTask(m_asyncTask->future(), tr("Rendering diff"), "DiffEditor");
}
void UnifiedDiffEditorWidget::jumpToOriginalFile(const QTextCursor &cursor)

View File

@@ -6,12 +6,17 @@
#include "diffeditorwidgetcontroller.h"
#include "selectabletexteditorwidget.h"
#include <QFutureWatcher>
#include <QFutureInterface>
namespace Core { class IContext; }
namespace TextEditor { class FontSettings; }
namespace Utils {
template <typename R>
class AsyncTask;
}
namespace DiffEditor {
class ChunkData;
@@ -107,7 +112,7 @@ private:
DiffSelections selections;
};
std::unique_ptr<QFutureWatcher<ShowResult>> m_watcher;
std::unique_ptr<Utils::AsyncTask<ShowResult>> m_asyncTask;
};
} // namespace Internal