diff options
author | Jarek Kobus <jaroslaw.kobus@qt.io> | 2023-03-14 20:42:38 +0100 |
---|---|---|
committer | Jarek Kobus <jaroslaw.kobus@qt.io> | 2023-03-21 09:53:58 +0000 |
commit | 31d6990ab856e4f25166c635fd1e6ccc8041a521 (patch) | |
tree | 6cc2eb9708e680814be47e074e5a081676907173 /src/plugins/help | |
parent | 8e9b8933256c1483f1f72ade010ea879550d40d7 (diff) | |
download | qt-creator-31d6990ab856e4f25166c635fd1e6ccc8041a521.tar.gz |
HelpIndexFilter: Simplify internals
Implement properly prepareSearch() instead of scheduling
a blocked call to caller thread from matchesFor() thread.
Change-Id: Id417235b19da36675afb13cf9a6f35759fe9d66d
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Diffstat (limited to 'src/plugins/help')
-rw-r--r-- | src/plugins/help/helpindexfilter.cpp | 55 | ||||
-rw-r--r-- | src/plugins/help/helpindexfilter.h | 11 |
2 files changed, 21 insertions, 45 deletions
diff --git a/src/plugins/help/helpindexfilter.cpp b/src/plugins/help/helpindexfilter.cpp index de4e412ab9..63578ef944 100644 --- a/src/plugins/help/helpindexfilter.cpp +++ b/src/plugins/help/helpindexfilter.cpp @@ -16,7 +16,6 @@ #include <QHelpEngine> #include <QHelpFilterEngine> #include <QHelpLink> -#include <QIcon> using namespace Core; using namespace Help; @@ -41,11 +40,25 @@ HelpIndexFilter::HelpIndexFilter() this, &HelpIndexFilter::invalidateCache); } -HelpIndexFilter::~HelpIndexFilter() = default; +void HelpIndexFilter::prepareSearch(const QString &entry) +{ + Q_UNUSED(entry) + if (!m_needsUpdate) + return; -bool HelpIndexFilter::updateCache(QFutureInterface<LocatorFilterEntry> &future, - const QStringList &cache, const QString &entry) + m_needsUpdate = false; + LocalHelpManager::setupGuiHelpEngine(); + m_allIndicesCache = LocalHelpManager::filterEngine()->indices({}); + m_lastIndicesCache.clear(); + m_lastEntry.clear(); +} + +QList<LocatorFilterEntry> HelpIndexFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future, + const QString &entry) { + const QStringList cache = m_lastEntry.isEmpty() || !entry.contains(m_lastEntry) + ? m_allIndicesCache : m_lastIndicesCache; + const Qt::CaseSensitivity cs = caseSensitivity(entry); QStringList bestKeywords; QStringList worseKeywords; @@ -53,38 +66,15 @@ bool HelpIndexFilter::updateCache(QFutureInterface<LocatorFilterEntry> &future, worseKeywords.reserve(cache.size()); for (const QString &keyword : cache) { if (future.isCanceled()) - return false; + return {}; if (keyword.startsWith(entry, cs)) bestKeywords.append(keyword); else if (keyword.contains(entry, cs)) worseKeywords.append(keyword); } - bestKeywords << worseKeywords; - m_lastIndicesCache = bestKeywords; + m_lastIndicesCache = bestKeywords + worseKeywords; m_lastEntry = entry; - return true; -} - -QList<LocatorFilterEntry> HelpIndexFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future, const QString &entry) -{ - if (m_needsUpdate.exchange(false)) { - QStringList indices; - QMetaObject::invokeMethod(this, [this] { return allIndices(); }, - Qt::BlockingQueuedConnection, &indices); - m_allIndicesCache = indices; - // force updating the cache taking the m_allIndicesCache - m_lastIndicesCache = QStringList(); - m_lastEntry = QString(); - } - - const QStringList cacheBase = m_lastEntry.isEmpty() || !entry.contains(m_lastEntry) - ? m_allIndicesCache : m_lastIndicesCache; - - if (!updateCache(future, cacheBase, entry)) - return QList<LocatorFilterEntry>(); - - const Qt::CaseSensitivity cs = caseSensitivity(entry); QList<LocatorFilterEntry> entries; for (const QString &keyword : std::as_const(m_lastIndicesCache)) { const int index = keyword.indexOf(entry, 0, cs); @@ -92,7 +82,6 @@ QList<LocatorFilterEntry> HelpIndexFilter::matchesFor(QFutureInterface<LocatorFi filterEntry.highlightInfo = {index, int(entry.length())}; entries.append(filterEntry); } - return entries; } @@ -107,12 +96,6 @@ void HelpIndexFilter::accept(const LocatorFilterEntry &selection, emit linksActivated(links, key); } -QStringList HelpIndexFilter::allIndices() const -{ - LocalHelpManager::setupGuiHelpEngine(); - return LocalHelpManager::filterEngine()->indices(QString()); -} - void HelpIndexFilter::invalidateCache() { m_needsUpdate = true; diff --git a/src/plugins/help/helpindexfilter.h b/src/plugins/help/helpindexfilter.h index e74e071b12..f836941d48 100644 --- a/src/plugins/help/helpindexfilter.h +++ b/src/plugins/help/helpindexfilter.h @@ -9,8 +9,6 @@ #include <QMultiMap> #include <QUrl> -#include <atomic> - namespace Help { namespace Internal { @@ -20,28 +18,23 @@ class HelpIndexFilter final : public Core::ILocatorFilter public: HelpIndexFilter(); - ~HelpIndexFilter() final; - // ILocatorFilter + void prepareSearch(const QString &entry) override; QList<Core::LocatorFilterEntry> matchesFor(QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry) override; void accept(const Core::LocatorFilterEntry &selection, QString *newText, int *selectionStart, int *selectionLength) const override; - QStringList allIndices() const; - signals: void linksActivated(const QMultiMap<QString, QUrl> &links, const QString &key) const; private: - bool updateCache(QFutureInterface<Core::LocatorFilterEntry> &future, - const QStringList &cache, const QString &entry); void invalidateCache(); QStringList m_allIndicesCache; QStringList m_lastIndicesCache; QString m_lastEntry; - std::atomic_bool m_needsUpdate = true; + bool m_needsUpdate = true; QIcon m_icon; }; |