From 422d422debaa27b52a6108fd7b45a38c4c748bd3 Mon Sep 17 00:00:00 2001 From: Jarek Kobus Date: Wed, 12 Apr 2023 18:09:04 +0200 Subject: [PATCH] LocatorMatcherTask: Get rid of Input/OutputData typedefs Introduce LocatorFilterEntries instead. Change-Id: I082e0405441aa4b70a5e8fb796aa5c4fa3879265 Reviewed-by: Eike Ziller Reviewed-by: --- .../coreplugin/locator/ilocatorfilter.cpp | 58 +++++++++---------- .../coreplugin/locator/ilocatorfilter.h | 19 +++--- src/plugins/cppeditor/cpplocatorfilter.cpp | 20 +++---- .../cppeditor/cpplocatorfilter_test.cpp | 4 +- src/plugins/languageclient/locatorfilter.cpp | 8 +-- .../jsonwizard/jsonfieldpage.cpp | 2 +- 6 files changed, 55 insertions(+), 56 deletions(-) diff --git a/src/plugins/coreplugin/locator/ilocatorfilter.cpp b/src/plugins/coreplugin/locator/ilocatorfilter.cpp index a2d8d847f52..fc8b7126788 100644 --- a/src/plugins/coreplugin/locator/ilocatorfilter.cpp +++ b/src/plugins/coreplugin/locator/ilocatorfilter.cpp @@ -5,7 +5,6 @@ #include "../coreplugin.h" #include "../coreplugintr.h" -#include "../editormanager/editormanager.h" #include #include @@ -22,6 +21,7 @@ #include #include #include +#include #include @@ -61,29 +61,29 @@ class OutputDataProvider struct WorkingData { WorkingData() = default; - WorkingData(const LocatorMatcherTask::OutputData &entries, std::atomic &state) { + WorkingData(const LocatorFilterEntries &entries, std::atomic &state) { mergeWith(entries, state); } - LocatorMatcherTask::OutputData mergeWith(const LocatorMatcherTask::OutputData &entries, - std::atomic &state) { - LocatorMatcherTask::OutputData results; + LocatorFilterEntries mergeWith(const LocatorFilterEntries &entries, + std::atomic &state) { + LocatorFilterEntries results; results.reserve(entries.size()); for (const LocatorFilterEntry &entry : entries) { if (state == State::Canceled) - return LocatorMatcherTask::OutputData(); + return {}; const auto &link = entry.linkForEditor; if (!link || m_cache.emplace(*link).second) results.append(entry); } if (state == State::Canceled) - return LocatorMatcherTask::OutputData(); + return {}; m_data += results; return results; } - LocatorMatcherTask::OutputData entries() const { return m_data; } + LocatorFilterEntries entries() const { return m_data; } private: - LocatorMatcherTask::OutputData m_data; + LocatorFilterEntries m_data; std::unordered_set m_cache; }; @@ -93,7 +93,7 @@ public: , m_outputData(filterCount, {}) {} - void addOutputData(int index, const LocatorMatcherTask::OutputData &outputData) + void addOutputData(int index, const LocatorFilterEntries &outputData) { QTC_ASSERT(index >= 0, return); @@ -115,9 +115,9 @@ public: } // Called from separate thread (OutputFilter's thread) - void run(QPromise &promise) + void run(QPromise &promise) { - QList> data; + QList> data; QList> workingList(m_filterCount, {}); while (waitForData(&data)) { // Emit new results only when new data is reachable from the beginning (i.e. no gaps) @@ -137,13 +137,13 @@ public: const auto &workingData = workingList.at(currentIndex); if (!workingData.has_value()) { const bool mergeToCurrent = currentIndex == mergeToIndex; - const LocatorMatcherTask::OutputData dataForIndex = mergeToCurrent - ? *outputData : LocatorMatcherTask::OutputData(); + const LocatorFilterEntries dataForIndex = mergeToCurrent ? *outputData + : LocatorFilterEntries(); workingList[currentIndex] = std::make_optional(WorkingData(dataForIndex, m_state)); if (m_state == State::Canceled) return; - const LocatorMatcherTask::OutputData newData = mergeToCurrent + const LocatorFilterEntries newData = mergeToCurrent ? workingList[currentIndex]->entries() : workingList[mergeToIndex]->mergeWith(*outputData, m_state); if (m_state == State::Canceled) @@ -151,7 +151,7 @@ public: if (!hasGap && !newData.isEmpty()) promise.addResult(newData); } else if (currentIndex != mergeToIndex) { - const LocatorMatcherTask::OutputData newData + const LocatorFilterEntries newData = workingList[mergeToIndex]->mergeWith(workingData->entries(), m_state); workingList[currentIndex] = std::make_optional({}); if (m_state == State::Canceled) @@ -168,7 +168,7 @@ public: } private: - bool waitForData(QList> *data) + bool waitForData(QList> *data) { QMutexLocker locker(&m_mutex); if (m_state == State::Canceled) @@ -191,7 +191,7 @@ private: QWaitCondition m_waitCondition; const int m_filterCount = 0; std::atomic m_state = State::Awaiting; - QList> m_outputData; + QList> m_outputData; }; class OutputFilter : public QObject @@ -202,18 +202,18 @@ public: ~OutputFilter(); void setFilterCount(int count); // When last index is added it ends automatically (asynchronously) - void addOutputData(int index, const LocatorMatcherTask::OutputData &outputData); + void addOutputData(int index, const LocatorFilterEntries &outputData); void start(); bool isRunning() const { return m_watcher.get(); } signals: - void serialOutputDataReady(const LocatorMatcherTask::OutputData &serialOutputData); + void serialOutputDataReady(const LocatorFilterEntries &serialOutputData); void done(); private: int m_filterCount = 0; - std::unique_ptr> m_watcher; + std::unique_ptr> m_watcher; std::shared_ptr m_dataProvider; }; @@ -234,7 +234,7 @@ void OutputFilter::setFilterCount(int count) m_filterCount = count; } -void OutputFilter::addOutputData(int index, const LocatorMatcherTask::OutputData &outputData) +void OutputFilter::addOutputData(int index, const LocatorFilterEntries &outputData) { QTC_ASSERT(isRunning(), return); @@ -251,7 +251,7 @@ void OutputFilter::start() } m_dataProvider.reset(new OutputDataProvider(m_filterCount)); - m_watcher.reset(new QFutureWatcher); + m_watcher.reset(new QFutureWatcher); connect(m_watcher.get(), &QFutureWatcherBase::resultReadyAt, this, [this](int index) { emit serialOutputDataReady(m_watcher->resultAt(index)); }); @@ -262,7 +262,7 @@ void OutputFilter::start() }); // TODO: When filterCount == 1, deliver results directly and finish? - auto filter = [](QPromise &promise, + auto filter = [](QPromise &promise, const std::shared_ptr &dataProvider) { dataProvider->run(promise); }; @@ -303,7 +303,7 @@ void LocatorMatcher::setTasks(const QList &tasks) d->m_tasks = tasks; } -void LocatorMatcher::setInputData(const LocatorMatcherTask::InputData &inputData) +void LocatorMatcher::setInputData(const QString &inputData) { d->m_storage.input = inputData; } @@ -331,7 +331,7 @@ void LocatorMatcher::start() filterStorage->m_filter = &filter; filter.setFilterCount(filterCount); connect(&filter, &OutputFilter::serialOutputDataReady, - this, [this](const LocatorMatcherTask::OutputData &serialOutputData) { + this, [this](const LocatorFilterEntries &serialOutputData) { d->m_storage.output += serialOutputData; emit serialOutputDataReady(serialOutputData); }); @@ -406,13 +406,13 @@ bool LocatorMatcher::isRunning() const return d->m_taskTree.get() && d->m_taskTree->isRunning(); } -LocatorMatcherTask::OutputData LocatorMatcher::outputData() const +LocatorFilterEntries LocatorMatcher::outputData() const { return d->m_storage.output; } -LocatorMatcherTask::OutputData LocatorMatcher::runBlocking(const QList &tasks, - const LocatorMatcherTask::InputData &input, int parallelLimit) +LocatorFilterEntries LocatorMatcher::runBlocking(const QList &tasks, + const QString &input, int parallelLimit) { LocatorMatcher tree; tree.setTasks(tasks); diff --git a/src/plugins/coreplugin/locator/ilocatorfilter.h b/src/plugins/coreplugin/locator/ilocatorfilter.h index 77dc59b2d08..e8428e5eb36 100644 --- a/src/plugins/coreplugin/locator/ilocatorfilter.h +++ b/src/plugins/coreplugin/locator/ilocatorfilter.h @@ -112,16 +112,16 @@ public: } }; +using LocatorFilterEntries = QList; + class CORE_EXPORT LocatorMatcherTask final { public: - using InputData = QString; - using OutputData = QList; class Storage { public: - InputData input; - OutputData output; + QString input; + LocatorFilterEntries output; }; // The main task. Initial data taken from storage.input field. // Results reporting is done through the storage.output field. @@ -150,25 +150,24 @@ public: LocatorMatcher(); ~LocatorMatcher(); void setTasks(const QList &tasks); - void setInputData(const LocatorMatcherTask::InputData &inputData); + void setInputData(const QString &inputData); void setParallelLimit(int limit); // by default 0 = parallel void start(); void stop(); bool isRunning() const; // Total data collected so far, even when running. - LocatorMatcherTask::OutputData outputData() const; + LocatorFilterEntries outputData() const; // Note: Starts internal event loop. - static LocatorMatcherTask::OutputData runBlocking(const QList &tasks, - const LocatorMatcherTask::InputData &input, - int parallelLimit = 0); + static LocatorFilterEntries runBlocking(const QList &tasks, + const QString &input, int parallelLimit = 0); static void addMatcherCreator(MatcherType type, const LocatorMatcherTaskCreator &creator); static QList matchers(MatcherType type); signals: - void serialOutputDataReady(const LocatorMatcherTask::OutputData &serialOutputData); + void serialOutputDataReady(const LocatorFilterEntries &serialOutputData); void done(bool success); private: diff --git a/src/plugins/cppeditor/cpplocatorfilter.cpp b/src/plugins/cppeditor/cpplocatorfilter.cpp index 7eaee38b84f..13320c2ae62 100644 --- a/src/plugins/cppeditor/cpplocatorfilter.cpp +++ b/src/plugins/cppeditor/cpplocatorfilter.cpp @@ -26,10 +26,10 @@ namespace CppEditor { using EntryFromIndex = std::function; -void matchesFor(QPromise &promise, const QString &entry, +void matchesFor(QPromise &promise, const QString &entry, IndexItem::ItemType wantedType, const EntryFromIndex &converter) { - QList entries[int(ILocatorFilter::MatchLevel::Count)]; + LocatorFilterEntries entries[int(ILocatorFilter::MatchLevel::Count)]; const Qt::CaseSensitivity caseSensitivityForPrefix = ILocatorFilter::caseSensitivity(entry); const QRegularExpression regexp = ILocatorFilter::createRegExp(entry); if (!regexp.isValid()) @@ -96,7 +96,7 @@ void matchesFor(QPromise &promise, const QString } promise.addResult(std::accumulate(std::begin(entries), std::end(entries), - QList())); + LocatorFilterEntries())); } LocatorMatcherTask locatorMatcher(IndexItem::ItemType type, const EntryFromIndex &converter) @@ -105,15 +105,15 @@ LocatorMatcherTask locatorMatcher(IndexItem::ItemType type, const EntryFromIndex TreeStorage storage; - const auto onSetup = [=](AsyncTask &async) { + const auto onSetup = [=](AsyncTask &async) { async.setFutureSynchronizer(Internal::CppEditorPlugin::futureSynchronizer()); async.setConcurrentCallData(matchesFor, storage->input, type, converter); }; - const auto onDone = [storage](const AsyncTask &async) { + const auto onDone = [storage](const AsyncTask &async) { if (async.isResultAvailable()) storage->output = async.result(); }; - return {Async(onSetup, onDone, onDone), storage}; + return {Async(onSetup, onDone, onDone), storage}; } LocatorMatcherTask cppAllSymbolsMatcher() @@ -200,7 +200,7 @@ LocatorFilterEntry::HighlightInfo highlightInfo(const QRegularExpressionMatch &m return LocatorFilterEntry::HighlightInfo(positions.starts, positions.lengths, dataType); } -void matchesForCurrentDocument(QPromise &promise, +void matchesForCurrentDocument(QPromise &promise, const QString &entry, const FilePath ¤tFileName) { const QRegularExpression regexp = FuzzyMatcher::createRegExp(entry, Qt::CaseInsensitive, false); @@ -304,15 +304,15 @@ LocatorMatcherTask cppCurrentDocumentMatcher() TreeStorage storage; - const auto onSetup = [=](AsyncTask &async) { + const auto onSetup = [=](AsyncTask &async) { async.setFutureSynchronizer(Internal::CppEditorPlugin::futureSynchronizer()); async.setConcurrentCallData(matchesForCurrentDocument, storage->input, currentFileName()); }; - const auto onDone = [storage](const AsyncTask &async) { + const auto onDone = [storage](const AsyncTask &async) { if (async.isResultAvailable()) storage->output = async.result(); }; - return {Async(onSetup, onDone, onDone), storage}; + return {Async(onSetup, onDone, onDone), storage}; } CppLocatorFilter::CppLocatorFilter() diff --git a/src/plugins/cppeditor/cpplocatorfilter_test.cpp b/src/plugins/cppeditor/cpplocatorfilter_test.cpp index f81ab6c40f6..7edb0c9d50e 100644 --- a/src/plugins/cppeditor/cpplocatorfilter_test.cpp +++ b/src/plugins/cppeditor/cpplocatorfilter_test.cpp @@ -53,7 +53,7 @@ public: return result; }; - const QList entries = filter ? matchesFor(searchText) : runMatcher(); + const LocatorFilterEntries entries = filter ? matchesFor(searchText) : runMatcher(); const ResultDataList results = ResultData::fromFilterEntryList(entries); if (debug) { ResultData::printFilterEntries(expectedResults, "Expected:"); @@ -93,7 +93,7 @@ public: return result; }; - const QList entries = matchers.isEmpty() ? matchesFor(searchText) + const LocatorFilterEntries entries = matchers.isEmpty() ? matchesFor(searchText) : runMatcher(); ResultDataList results = ResultData::fromFilterEntryList(entries); if (debug) { diff --git a/src/plugins/languageclient/locatorfilter.cpp b/src/plugins/languageclient/locatorfilter.cpp index 83c13002d21..a0229b11869 100644 --- a/src/plugins/languageclient/locatorfilter.cpp +++ b/src/plugins/languageclient/locatorfilter.cpp @@ -28,7 +28,7 @@ using namespace Utils; namespace LanguageClient { -void filterResults(QPromise &promise, Client *client, +void filterResults(QPromise &promise, Client *client, const QList &results, const QList &filter) { const auto doFilter = [&](const SymbolInformation &info) { @@ -71,7 +71,7 @@ LocatorMatcherTask locatorMatcher(Client *client, int maxResultCount, *resultStorage = result->toList(); }; - const auto onFilterSetup = [=](AsyncTask &async) { + const auto onFilterSetup = [=](AsyncTask &async) { const QList results = *resultStorage; if (results.isEmpty()) return TaskAction::StopWithDone; @@ -79,7 +79,7 @@ LocatorMatcherTask locatorMatcher(Client *client, int maxResultCount, async.setConcurrentCallData(filterResults, client, results, filter); return TaskAction::Continue; }; - const auto onFilterDone = [storage](const AsyncTask &async) { + const auto onFilterDone = [storage](const AsyncTask &async) { if (async.isResultAvailable()) storage->output = async.result(); }; @@ -87,7 +87,7 @@ LocatorMatcherTask locatorMatcher(Client *client, int maxResultCount, const Group root { Storage(resultStorage), SymbolRequest(onQuerySetup, onQueryDone), - Async(onFilterSetup, onFilterDone) + Async(onFilterSetup, onFilterDone) }; return {root, storage}; } diff --git a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp index d57c0bacc5f..2837fe844c8 100644 --- a/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp +++ b/src/plugins/projectexplorer/jsonwizard/jsonfieldpage.cpp @@ -599,7 +599,7 @@ void LineEditField::setupCompletion(FancyLineEdit *lineEdit) using namespace Utils; if (m_completion == Completion::None) return; - const auto handleResults = [this, lineEdit](const QList &entries) { + const auto handleResults = [this, lineEdit](const LocatorFilterEntries &entries) { QSet namespaces; QStringList classes; Project * const project = ProjectTree::currentProject();