From b19b4482c133ad6a376da62bad257211267a9a63 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Tue, 18 Apr 2023 00:25:35 +0200 Subject: [PATCH] HelpIndexFilter: Reimplement matchers() Change-Id: I47bf8eee25b91ac5ae7d8fd2c06a34370d5d433f Reviewed-by: Eike Ziller Reviewed-by: --- src/plugins/help/helpindexfilter.cpp | 70 +++++++++++++++++++++++++++- src/plugins/help/helpindexfilter.h | 1 + src/plugins/help/helpplugin.cpp | 14 ++++-- src/plugins/help/helpplugin.h | 3 ++ 4 files changed, 84 insertions(+), 4 deletions(-) diff --git a/src/plugins/help/helpindexfilter.cpp b/src/plugins/help/helpindexfilter.cpp index fc6e5b8e24f..2d005079973 100644 --- a/src/plugins/help/helpindexfilter.cpp +++ b/src/plugins/help/helpindexfilter.cpp @@ -11,7 +11,7 @@ #include #include #include -#include +#include #include #include @@ -21,6 +21,7 @@ using namespace Core; using namespace Help; using namespace Help::Internal; +using namespace Utils; HelpIndexFilter::HelpIndexFilter() { @@ -42,6 +43,73 @@ HelpIndexFilter::HelpIndexFilter() this, &HelpIndexFilter::invalidateCache); } +static void matches(QPromise &promise, const LocatorStorage &storage, + const QStringList &cache, const QIcon &icon) +{ + const QString input = storage.input(); + const Qt::CaseSensitivity cs = ILocatorFilter::caseSensitivity(input); + QStringList bestKeywords; + QStringList worseKeywords; + bestKeywords.reserve(cache.size()); + worseKeywords.reserve(cache.size()); + for (const QString &keyword : cache) { + if (promise.isCanceled()) + return; + if (keyword.startsWith(input, cs)) + bestKeywords.append(keyword); + else if (keyword.contains(input, cs)) + worseKeywords.append(keyword); + } + const QStringList lastIndicesCache = bestKeywords + worseKeywords; + + LocatorFilterEntries entries; + for (const QString &key : lastIndicesCache) { + if (promise.isCanceled()) + return; + const int index = key.indexOf(input, 0, cs); + LocatorFilterEntry filterEntry; + filterEntry.displayName = key; + filterEntry.acceptor = [key] { + HelpPlugin::showLinksInCurrentViewer(LocalHelpManager::linksForKeyword(key), key); + return AcceptResult(); + }; + filterEntry.displayIcon = icon; + filterEntry.highlightInfo = {index, int(input.length())}; + entries.append(filterEntry); + } + storage.reportOutput(entries); + promise.addResult(lastIndicesCache); +} + +LocatorMatcherTasks HelpIndexFilter::matchers() +{ + using namespace Tasking; + + TreeStorage storage; + + const auto onSetup = [this, storage](AsyncTask &async) { + if (m_needsUpdate) { + m_needsUpdate = false; + LocalHelpManager::setupGuiHelpEngine(); + m_allIndicesCache = LocalHelpManager::filterEngine()->indices({}); + m_lastIndicesCache.clear(); + m_lastEntry.clear(); + } + const QStringList cache = m_lastEntry.isEmpty() || !storage->input().contains(m_lastEntry) + ? m_allIndicesCache : m_lastIndicesCache; + async.setFutureSynchronizer(HelpPlugin::futureSynchronizer()); + async.setConcurrentCallData(matches, *storage, cache, m_icon); + }; + const auto onDone = [this, storage](const AsyncTask &async) { + if (async.isResultAvailable()) { + m_lastIndicesCache = async.result(); + m_lastEntry = storage->input(); + } + }; + + return {{Async(onSetup, onDone), storage}}; +} + void HelpIndexFilter::prepareSearch(const QString &entry) { Q_UNUSED(entry) diff --git a/src/plugins/help/helpindexfilter.h b/src/plugins/help/helpindexfilter.h index 653d6ff9b7c..489f7078c18 100644 --- a/src/plugins/help/helpindexfilter.h +++ b/src/plugins/help/helpindexfilter.h @@ -19,6 +19,7 @@ public: QList matchesFor(QFutureInterface &future, const QString &entry) override; private: + Core::LocatorMatcherTasks matchers() final; void invalidateCache(); QStringList m_allIndicesCache; diff --git a/src/plugins/help/helpplugin.cpp b/src/plugins/help/helpplugin.cpp index c3a743043c9..045035d093f 100644 --- a/src/plugins/help/helpplugin.cpp +++ b/src/plugins/help/helpplugin.cpp @@ -43,6 +43,7 @@ #include #include +#include #include #include #include @@ -97,7 +98,7 @@ class HelpPluginPrivate : public QObject public: HelpPluginPrivate(); - void modeChanged(Utils::Id mode, Utils::Id old); + void modeChanged(Id mode, Id old); void requestContextHelp(); void showContextHelp(const HelpItem &contextHelp); @@ -142,6 +143,7 @@ public: LocalHelpManager m_localHelpManager; HelpIndexFilter helpIndexFilter; + FutureSynchronizer m_futureSynchronizer; }; static HelpPluginPrivate *dd = nullptr; @@ -395,6 +397,12 @@ HelpWidget *HelpPlugin::modeHelpWidget() return dd->m_centralWidget; } +FutureSynchronizer *HelpPlugin::futureSynchronizer() +{ + QTC_ASSERT(dd, return nullptr); + return &dd->m_futureSynchronizer; +} + void HelpPluginPrivate::showLinksInCurrentViewer(const QMultiMap &links, const QString &key) { if (links.size() < 1) @@ -403,7 +411,7 @@ void HelpPluginPrivate::showLinksInCurrentViewer(const QMultiMap widget->showLinks(links, key); } -void HelpPluginPrivate::modeChanged(Utils::Id mode, Utils::Id old) +void HelpPluginPrivate::modeChanged(Id mode, Id old) { Q_UNUSED(old) if (mode == m_mode.id()) { @@ -503,7 +511,7 @@ HelpViewer *HelpPluginPrivate::viewerForContextHelp() void HelpPluginPrivate::requestContextHelp() { // Find out what to show - const QVariant tipHelpValue = Utils::ToolTip::contextHelp(); + const QVariant tipHelpValue = ToolTip::contextHelp(); const HelpItem tipHelp = tipHelpValue.canConvert() ? tipHelpValue.value() : HelpItem(tipHelpValue.toString()); diff --git a/src/plugins/help/helpplugin.h b/src/plugins/help/helpplugin.h index 96d14a152b8..bd483ae3da8 100644 --- a/src/plugins/help/helpplugin.h +++ b/src/plugins/help/helpplugin.h @@ -10,6 +10,8 @@ QT_BEGIN_NAMESPACE class QUrl; QT_END_NAMESPACE +namespace Utils { class FutureSynchronizer; } + namespace Help { namespace Internal { @@ -30,6 +32,7 @@ public: const QString &key); static HelpViewer *createHelpViewer(); static HelpWidget *modeHelpWidget(); + static Utils::FutureSynchronizer *futureSynchronizer(); private: void initialize() final;