diff options
Diffstat (limited to 'src/plugins/help/helpindexfilter.cpp')
-rw-r--r-- | src/plugins/help/helpindexfilter.cpp | 123 |
1 files changed, 57 insertions, 66 deletions
diff --git a/src/plugins/help/helpindexfilter.cpp b/src/plugins/help/helpindexfilter.cpp index fe1c34a48e0..9da19214801 100644 --- a/src/plugins/help/helpindexfilter.cpp +++ b/src/plugins/help/helpindexfilter.cpp @@ -3,30 +3,34 @@ #include "helpindexfilter.h" +#include "localhelpmanager.h" #include "helpmanager.h" +#include "helpplugin.h" #include "helptr.h" -#include "localhelpmanager.h" -#include <coreplugin/icore.h> #include <coreplugin/helpmanager.h> +#include <coreplugin/icore.h> #include <extensionsystem/pluginmanager.h> +#include <utils/async.h> #include <utils/utilsicons.h> #include <QHelpEngine> #include <QHelpFilterEngine> #include <QHelpLink> -#include <QIcon> using namespace Core; using namespace Help; using namespace Help::Internal; +using namespace Tasking; +using namespace Utils; HelpIndexFilter::HelpIndexFilter() { setId("HelpIndexFilter"); setDisplayName(Tr::tr("Help Index")); - setDefaultIncludedByDefault(false); + setDescription(Tr::tr("Locates help topics, for example in the Qt documentation.")); setDefaultShortcutString("?"); + setRefreshRecipe(Sync([this] { invalidateCache(); })); m_icon = Utils::Icons::BOOKMARK.icon(); connect(Core::HelpManager::Signals::instance(), &Core::HelpManager::Signals::setupFinished, @@ -39,82 +43,69 @@ HelpIndexFilter::HelpIndexFilter() this, &HelpIndexFilter::invalidateCache); } -HelpIndexFilter::~HelpIndexFilter() = default; - -bool HelpIndexFilter::updateCache(QFutureInterface<LocatorFilterEntry> &future, - const QStringList &cache, const QString &entry) +static void matches(QPromise<QStringList> &promise, const LocatorStorage &storage, + const QStringList &cache, const QIcon &icon) { - const Qt::CaseSensitivity cs = caseSensitivity(entry); + 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 (future.isCanceled()) - return false; - if (keyword.startsWith(entry, cs)) + if (promise.isCanceled()) + return; + if (keyword.startsWith(input, cs)) bestKeywords.append(keyword); - else if (keyword.contains(entry, cs)) + else if (keyword.contains(input, cs)) worseKeywords.append(keyword); } - bestKeywords << worseKeywords; - m_lastIndicesCache = bestKeywords; - 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); - LocatorFilterEntry filterEntry(this, keyword, {}, m_icon); - filterEntry.highlightInfo = {index, int(entry.length())}; + 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); } - - return entries; -} - -void HelpIndexFilter::accept(const LocatorFilterEntry &selection, - QString *newText, int *selectionStart, int *selectionLength) const -{ - Q_UNUSED(newText) - Q_UNUSED(selectionStart) - Q_UNUSED(selectionLength) - const QString &key = selection.displayName; - const QMultiMap<QString, QUrl> links = LocalHelpManager::linksForKeyword(key); - emit linksActivated(links, key); -} - -void HelpIndexFilter::refresh(QFutureInterface<void> &future) -{ - Q_UNUSED(future) - invalidateCache(); + storage.reportOutput(entries); + promise.addResult(lastIndicesCache); } -QStringList HelpIndexFilter::allIndices() const +LocatorMatcherTasks HelpIndexFilter::matchers() { - LocalHelpManager::setupGuiHelpEngine(); - return LocalHelpManager::filterEngine()->indices(QString()); + TreeStorage<LocatorStorage> storage; + + const auto onSetup = [this, storage](Async<QStringList> &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(ExtensionSystem::PluginManager::futureSynchronizer()); + async.setConcurrentCallData(matches, *storage, cache, m_icon); + }; + const auto onDone = [this, storage](const Async<QStringList> &async) { + if (async.isResultAvailable()) { + m_lastIndicesCache = async.result(); + m_lastEntry = storage->input(); + } + }; + + return {{AsyncTask<QStringList>(onSetup, onDone), storage}}; } void HelpIndexFilter::invalidateCache() |