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 #pragma once
#include "diffeditor_global.h"
#include <coreplugin/diffservice.h> #include <coreplugin/diffservice.h>
#include <extensionsystem/iplugin.h> #include <extensionsystem/iplugin.h>
namespace Utils { class FutureSynchronizer; } namespace Utils { class FutureSynchronizer; }
QT_BEGIN_NAMESPACE
template <typename T>
class QFuture;
QT_END_NAMESPACE
namespace DiffEditor { namespace DiffEditor {
namespace Internal { namespace Internal {

View File

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

View File

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

View File

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

View File

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