SemanticInfoUpdater: Simplify internal implementation

Change-Id: If9913ccbc66d35608d18286ea3ca12d751ad2115
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
This commit is contained in:
Jarek Kobus
2023-10-06 10:00:53 +02:00
parent 716c194846
commit d021f9158b

View File

@@ -29,34 +29,14 @@ namespace CppEditor {
class SemanticInfoUpdaterPrivate class SemanticInfoUpdaterPrivate
{ {
public: public:
explicit SemanticInfoUpdaterPrivate(SemanticInfoUpdater *q);
~SemanticInfoUpdaterPrivate() { cancelFuture(); } ~SemanticInfoUpdaterPrivate() { cancelFuture(); }
SemanticInfo semanticInfo() const { return m_semanticInfo; }
void setSemanticInfo(const SemanticInfo &semanticInfo, bool emitSignal);
bool reuseCurrentSemanticInfo(const SemanticInfo::Source &source, bool emitSignalWhenFinished);
void cancelFuture(); void cancelFuture();
public:
SemanticInfoUpdater *q;
SemanticInfo m_semanticInfo; SemanticInfo m_semanticInfo;
std::unique_ptr<QFutureWatcher<SemanticInfo>> m_watcher; std::unique_ptr<QFutureWatcher<SemanticInfo>> m_watcher;
}; };
SemanticInfoUpdaterPrivate::SemanticInfoUpdaterPrivate(SemanticInfoUpdater *q)
: q(q)
{}
void SemanticInfoUpdaterPrivate::setSemanticInfo(const SemanticInfo &semanticInfo, bool emitSignal)
{
m_semanticInfo = semanticInfo;
if (emitSignal) {
qCDebug(log) << "emiting new info";
emit q->updated(semanticInfo);
}
}
void SemanticInfoUpdaterPrivate::cancelFuture() void SemanticInfoUpdaterPrivate::cancelFuture()
{ {
if (!m_watcher) if (!m_watcher)
@@ -97,11 +77,9 @@ static void doUpdate(QPromise<SemanticInfo> &promise, const SemanticInfo::Source
promise.addResult(newSemanticInfo); promise.addResult(newSemanticInfo);
} }
bool SemanticInfoUpdaterPrivate::reuseCurrentSemanticInfo(const SemanticInfo::Source &source, static std::optional<SemanticInfo> canReuseSemanticInfo(
bool emitSignalWhenFinished) const SemanticInfo &currentSemanticInfo, const SemanticInfo::Source &source)
{ {
const SemanticInfo currentSemanticInfo = semanticInfo();
if (!source.force if (!source.force
&& currentSemanticInfo.complete && currentSemanticInfo.complete
&& currentSemanticInfo.revision == source.revision && currentSemanticInfo.revision == source.revision
@@ -114,18 +92,15 @@ bool SemanticInfoUpdaterPrivate::reuseCurrentSemanticInfo(const SemanticInfo::So
newSemanticInfo.revision = source.revision; newSemanticInfo.revision = source.revision;
newSemanticInfo.snapshot = source.snapshot; newSemanticInfo.snapshot = source.snapshot;
newSemanticInfo.doc = currentSemanticInfo.doc; newSemanticInfo.doc = currentSemanticInfo.doc;
setSemanticInfo(newSemanticInfo, emitSignalWhenFinished);
qCDebug(log) << "re-using current semantic info, source revision:" << source.revision; qCDebug(log) << "re-using current semantic info, source revision:" << source.revision;
return true; return newSemanticInfo;
} }
return {};
return false;
} }
SemanticInfoUpdater::SemanticInfoUpdater() SemanticInfoUpdater::SemanticInfoUpdater()
: d(new SemanticInfoUpdaterPrivate(this)) : d(new SemanticInfoUpdaterPrivate)
{ {}
}
SemanticInfoUpdater::~SemanticInfoUpdater() = default; SemanticInfoUpdater::~SemanticInfoUpdater() = default;
@@ -134,15 +109,17 @@ SemanticInfo SemanticInfoUpdater::update(const SemanticInfo::Source &source)
qCDebug(log) << "update() - synchronous"; qCDebug(log) << "update() - synchronous";
d->cancelFuture(); d->cancelFuture();
const bool emitSignalWhenFinished = false; const auto info = canReuseSemanticInfo(d->m_semanticInfo, source);
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished)) if (info) {
return semanticInfo(); d->m_semanticInfo = *info;
return d->m_semanticInfo;
}
QPromise<SemanticInfo> dummy; QPromise<SemanticInfo> dummy;
dummy.start(); dummy.start();
doUpdate(dummy, source); doUpdate(dummy, source);
const SemanticInfo result = dummy.future().result(); const SemanticInfo result = dummy.future().result();
d->setSemanticInfo(result, emitSignalWhenFinished); d->m_semanticInfo = result;
return result; return result;
} }
@@ -151,13 +128,17 @@ void SemanticInfoUpdater::updateDetached(const SemanticInfo::Source &source)
qCDebug(log) << "updateDetached() - asynchronous"; qCDebug(log) << "updateDetached() - asynchronous";
d->cancelFuture(); d->cancelFuture();
const bool emitSignalWhenFinished = true; const auto info = canReuseSemanticInfo(d->m_semanticInfo, source);
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished)) if (info) {
d->m_semanticInfo = *info;
emit updated(d->m_semanticInfo);
return; return;
}
d->m_watcher.reset(new QFutureWatcher<SemanticInfo>); d->m_watcher.reset(new QFutureWatcher<SemanticInfo>);
connect(d->m_watcher.get(), &QFutureWatcherBase::finished, this, [this] { connect(d->m_watcher.get(), &QFutureWatcherBase::finished, this, [this] {
d->setSemanticInfo(d->m_watcher->result(), true); d->m_semanticInfo = d->m_watcher->result();
emit updated(d->m_semanticInfo);
d->m_watcher.release()->deleteLater(); d->m_watcher.release()->deleteLater();
}); });
const auto future = Utils::asyncRun(CppModelManager::sharedThreadPool(), doUpdate, source); const auto future = Utils::asyncRun(CppModelManager::sharedThreadPool(), doUpdate, source);
@@ -167,7 +148,7 @@ void SemanticInfoUpdater::updateDetached(const SemanticInfo::Source &source)
SemanticInfo SemanticInfoUpdater::semanticInfo() const SemanticInfo SemanticInfoUpdater::semanticInfo() const
{ {
return d->semanticInfo(); return d->m_semanticInfo;
} }
} // namespace CppEditor } // namespace CppEditor