forked from qt-creator/qt-creator
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:
@@ -29,34 +29,14 @@ namespace CppEditor {
|
||||
class SemanticInfoUpdaterPrivate
|
||||
{
|
||||
public:
|
||||
explicit SemanticInfoUpdaterPrivate(SemanticInfoUpdater *q);
|
||||
~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();
|
||||
public:
|
||||
SemanticInfoUpdater *q;
|
||||
|
||||
SemanticInfo m_semanticInfo;
|
||||
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()
|
||||
{
|
||||
if (!m_watcher)
|
||||
@@ -97,11 +77,9 @@ static void doUpdate(QPromise<SemanticInfo> &promise, const SemanticInfo::Source
|
||||
promise.addResult(newSemanticInfo);
|
||||
}
|
||||
|
||||
bool SemanticInfoUpdaterPrivate::reuseCurrentSemanticInfo(const SemanticInfo::Source &source,
|
||||
bool emitSignalWhenFinished)
|
||||
static std::optional<SemanticInfo> canReuseSemanticInfo(
|
||||
const SemanticInfo ¤tSemanticInfo, const SemanticInfo::Source &source)
|
||||
{
|
||||
const SemanticInfo currentSemanticInfo = semanticInfo();
|
||||
|
||||
if (!source.force
|
||||
&& currentSemanticInfo.complete
|
||||
&& currentSemanticInfo.revision == source.revision
|
||||
@@ -114,18 +92,15 @@ bool SemanticInfoUpdaterPrivate::reuseCurrentSemanticInfo(const SemanticInfo::So
|
||||
newSemanticInfo.revision = source.revision;
|
||||
newSemanticInfo.snapshot = source.snapshot;
|
||||
newSemanticInfo.doc = currentSemanticInfo.doc;
|
||||
setSemanticInfo(newSemanticInfo, emitSignalWhenFinished);
|
||||
qCDebug(log) << "re-using current semantic info, source revision:" << source.revision;
|
||||
return true;
|
||||
return newSemanticInfo;
|
||||
}
|
||||
|
||||
return false;
|
||||
return {};
|
||||
}
|
||||
|
||||
SemanticInfoUpdater::SemanticInfoUpdater()
|
||||
: d(new SemanticInfoUpdaterPrivate(this))
|
||||
{
|
||||
}
|
||||
: d(new SemanticInfoUpdaterPrivate)
|
||||
{}
|
||||
|
||||
SemanticInfoUpdater::~SemanticInfoUpdater() = default;
|
||||
|
||||
@@ -134,15 +109,17 @@ SemanticInfo SemanticInfoUpdater::update(const SemanticInfo::Source &source)
|
||||
qCDebug(log) << "update() - synchronous";
|
||||
d->cancelFuture();
|
||||
|
||||
const bool emitSignalWhenFinished = false;
|
||||
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished))
|
||||
return semanticInfo();
|
||||
const auto info = canReuseSemanticInfo(d->m_semanticInfo, source);
|
||||
if (info) {
|
||||
d->m_semanticInfo = *info;
|
||||
return d->m_semanticInfo;
|
||||
}
|
||||
|
||||
QPromise<SemanticInfo> dummy;
|
||||
dummy.start();
|
||||
doUpdate(dummy, source);
|
||||
const SemanticInfo result = dummy.future().result();
|
||||
d->setSemanticInfo(result, emitSignalWhenFinished);
|
||||
d->m_semanticInfo = result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -151,13 +128,17 @@ void SemanticInfoUpdater::updateDetached(const SemanticInfo::Source &source)
|
||||
qCDebug(log) << "updateDetached() - asynchronous";
|
||||
d->cancelFuture();
|
||||
|
||||
const bool emitSignalWhenFinished = true;
|
||||
if (d->reuseCurrentSemanticInfo(source, emitSignalWhenFinished))
|
||||
const auto info = canReuseSemanticInfo(d->m_semanticInfo, source);
|
||||
if (info) {
|
||||
d->m_semanticInfo = *info;
|
||||
emit updated(d->m_semanticInfo);
|
||||
return;
|
||||
}
|
||||
|
||||
d->m_watcher.reset(new QFutureWatcher<SemanticInfo>);
|
||||
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();
|
||||
});
|
||||
const auto future = Utils::asyncRun(CppModelManager::sharedThreadPool(), doUpdate, source);
|
||||
@@ -167,7 +148,7 @@ void SemanticInfoUpdater::updateDetached(const SemanticInfo::Source &source)
|
||||
|
||||
SemanticInfo SemanticInfoUpdater::semanticInfo() const
|
||||
{
|
||||
return d->semanticInfo();
|
||||
return d->m_semanticInfo;
|
||||
}
|
||||
|
||||
} // namespace CppEditor
|
||||
|
||||
Reference in New Issue
Block a user