LocatorMatcherTask: Get rid of Input/OutputData typedefs

Introduce LocatorFilterEntries instead.

Change-Id: I082e0405441aa4b70a5e8fb796aa5c4fa3879265
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Jarek Kobus
2023-04-12 18:09:04 +02:00
parent 44aaad4c2f
commit 422d422deb
6 changed files with 55 additions and 56 deletions

View File

@@ -5,7 +5,6 @@
#include "../coreplugin.h"
#include "../coreplugintr.h"
#include "../editormanager/editormanager.h"
#include <utils/asynctask.h>
#include <utils/fuzzymatcher.h>
@@ -22,6 +21,7 @@
#include <QLabel>
#include <QLineEdit>
#include <QRegularExpression>
#include <QWaitCondition>
#include <unordered_set>
@@ -61,29 +61,29 @@ class OutputDataProvider
struct WorkingData {
WorkingData() = default;
WorkingData(const LocatorMatcherTask::OutputData &entries, std::atomic<State> &state) {
WorkingData(const LocatorFilterEntries &entries, std::atomic<State> &state) {
mergeWith(entries, state);
}
LocatorMatcherTask::OutputData mergeWith(const LocatorMatcherTask::OutputData &entries,
LocatorFilterEntries mergeWith(const LocatorFilterEntries &entries,
std::atomic<State> &state) {
LocatorMatcherTask::OutputData results;
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<Utils::Link> 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<LocatorMatcherTask::OutputData> &promise)
void run(QPromise<LocatorFilterEntries> &promise)
{
QList<std::optional<LocatorMatcherTask::OutputData>> data;
QList<std::optional<LocatorFilterEntries>> data;
QList<std::optional<WorkingData>> 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<WorkingData>({});
if (m_state == State::Canceled)
@@ -168,7 +168,7 @@ public:
}
private:
bool waitForData(QList<std::optional<LocatorMatcherTask::OutputData>> *data)
bool waitForData(QList<std::optional<LocatorFilterEntries>> *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<State> m_state = State::Awaiting;
QList<std::optional<LocatorMatcherTask::OutputData>> m_outputData;
QList<std::optional<LocatorFilterEntries>> 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<QFutureWatcher<LocatorMatcherTask::OutputData>> m_watcher;
std::unique_ptr<QFutureWatcher<LocatorFilterEntries>> m_watcher;
std::shared_ptr<OutputDataProvider> 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<LocatorMatcherTask::OutputData>);
m_watcher.reset(new QFutureWatcher<LocatorFilterEntries>);
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<LocatorMatcherTask::OutputData> &promise,
auto filter = [](QPromise<LocatorFilterEntries> &promise,
const std::shared_ptr<OutputDataProvider> &dataProvider) {
dataProvider->run(promise);
};
@@ -303,7 +303,7 @@ void LocatorMatcher::setTasks(const QList<LocatorMatcherTask> &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<LocatorMatcherTask> &tasks,
const LocatorMatcherTask::InputData &input, int parallelLimit)
LocatorFilterEntries LocatorMatcher::runBlocking(const QList<LocatorMatcherTask> &tasks,
const QString &input, int parallelLimit)
{
LocatorMatcher tree;
tree.setTasks(tasks);

View File

@@ -112,16 +112,16 @@ public:
}
};
using LocatorFilterEntries = QList<LocatorFilterEntry>;
class CORE_EXPORT LocatorMatcherTask final
{
public:
using InputData = QString;
using OutputData = QList<LocatorFilterEntry>;
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<LocatorMatcherTask> &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<LocatorMatcherTask> &tasks,
const LocatorMatcherTask::InputData &input,
int parallelLimit = 0);
static LocatorFilterEntries runBlocking(const QList<LocatorMatcherTask> &tasks,
const QString &input, int parallelLimit = 0);
static void addMatcherCreator(MatcherType type, const LocatorMatcherTaskCreator &creator);
static QList<LocatorMatcherTask> matchers(MatcherType type);
signals:
void serialOutputDataReady(const LocatorMatcherTask::OutputData &serialOutputData);
void serialOutputDataReady(const LocatorFilterEntries &serialOutputData);
void done(bool success);
private:

View File

@@ -26,10 +26,10 @@ namespace CppEditor {
using EntryFromIndex = std::function<LocatorFilterEntry(const IndexItem::Ptr &)>;
void matchesFor(QPromise<LocatorMatcherTask::OutputData> &promise, const QString &entry,
void matchesFor(QPromise<LocatorFilterEntries> &promise, const QString &entry,
IndexItem::ItemType wantedType, const EntryFromIndex &converter)
{
QList<LocatorFilterEntry> 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<LocatorMatcherTask::OutputData> &promise, const QString
}
promise.addResult(std::accumulate(std::begin(entries), std::end(entries),
QList<LocatorFilterEntry>()));
LocatorFilterEntries()));
}
LocatorMatcherTask locatorMatcher(IndexItem::ItemType type, const EntryFromIndex &converter)
@@ -105,15 +105,15 @@ LocatorMatcherTask locatorMatcher(IndexItem::ItemType type, const EntryFromIndex
TreeStorage<LocatorMatcherTask::Storage> storage;
const auto onSetup = [=](AsyncTask<LocatorMatcherTask::OutputData> &async) {
const auto onSetup = [=](AsyncTask<LocatorFilterEntries> &async) {
async.setFutureSynchronizer(Internal::CppEditorPlugin::futureSynchronizer());
async.setConcurrentCallData(matchesFor, storage->input, type, converter);
};
const auto onDone = [storage](const AsyncTask<LocatorMatcherTask::OutputData> &async) {
const auto onDone = [storage](const AsyncTask<LocatorFilterEntries> &async) {
if (async.isResultAvailable())
storage->output = async.result();
};
return {Async<LocatorMatcherTask::OutputData>(onSetup, onDone, onDone), storage};
return {Async<LocatorFilterEntries>(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<LocatorMatcherTask::OutputData> &promise,
void matchesForCurrentDocument(QPromise<LocatorFilterEntries> &promise,
const QString &entry, const FilePath &currentFileName)
{
const QRegularExpression regexp = FuzzyMatcher::createRegExp(entry, Qt::CaseInsensitive, false);
@@ -304,15 +304,15 @@ LocatorMatcherTask cppCurrentDocumentMatcher()
TreeStorage<LocatorMatcherTask::Storage> storage;
const auto onSetup = [=](AsyncTask<LocatorMatcherTask::OutputData> &async) {
const auto onSetup = [=](AsyncTask<LocatorFilterEntries> &async) {
async.setFutureSynchronizer(Internal::CppEditorPlugin::futureSynchronizer());
async.setConcurrentCallData(matchesForCurrentDocument, storage->input, currentFileName());
};
const auto onDone = [storage](const AsyncTask<LocatorMatcherTask::OutputData> &async) {
const auto onDone = [storage](const AsyncTask<LocatorFilterEntries> &async) {
if (async.isResultAvailable())
storage->output = async.result();
};
return {Async<LocatorMatcherTask::OutputData>(onSetup, onDone, onDone), storage};
return {Async<LocatorFilterEntries>(onSetup, onDone, onDone), storage};
}
CppLocatorFilter::CppLocatorFilter()

View File

@@ -53,7 +53,7 @@ public:
return result;
};
const QList<LocatorFilterEntry> 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<LocatorFilterEntry> entries = matchers.isEmpty() ? matchesFor(searchText)
const LocatorFilterEntries entries = matchers.isEmpty() ? matchesFor(searchText)
: runMatcher();
ResultDataList results = ResultData::fromFilterEntryList(entries);
if (debug) {

View File

@@ -28,7 +28,7 @@ using namespace Utils;
namespace LanguageClient {
void filterResults(QPromise<LocatorMatcherTask::OutputData> &promise, Client *client,
void filterResults(QPromise<LocatorFilterEntries> &promise, Client *client,
const QList<SymbolInformation> &results, const QList<SymbolKind> &filter)
{
const auto doFilter = [&](const SymbolInformation &info) {
@@ -71,7 +71,7 @@ LocatorMatcherTask locatorMatcher(Client *client, int maxResultCount,
*resultStorage = result->toList();
};
const auto onFilterSetup = [=](AsyncTask<LocatorMatcherTask::OutputData> &async) {
const auto onFilterSetup = [=](AsyncTask<LocatorFilterEntries> &async) {
const QList<SymbolInformation> 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<LocatorMatcherTask::OutputData> &async) {
const auto onFilterDone = [storage](const AsyncTask<LocatorFilterEntries> &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<LocatorMatcherTask::OutputData>(onFilterSetup, onFilterDone)
Async<LocatorFilterEntries>(onFilterSetup, onFilterDone)
};
return {root, storage};
}

View File

@@ -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<LocatorFilterEntry> &entries) {
const auto handleResults = [this, lineEdit](const LocatorFilterEntries &entries) {
QSet<QString> namespaces;
QStringList classes;
Project * const project = ProjectTree::currentProject();