LocatorWidget: Dismantle the old matchesFor implementation

Stop using it internally.
Make ILocatorFilter::matchesFor() temporarily implemented
by default so that the reimplementations in subclasses may be
removed without leaving them abstract.

Change-Id: I0c5d4ff70145a37d29385ee58a051c9c5ddfab8e
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2023-04-24 18:21:14 +02:00
parent 2628e0f82f
commit 24664515f1
10 changed files with 9 additions and 226 deletions

View File

@@ -111,7 +111,6 @@ add_qtc_plugin(Core
locator/locatorconstants.h locator/locatorconstants.h
locator/locatorfiltersfilter.cpp locator/locatorfiltersfilter.h locator/locatorfiltersfilter.cpp locator/locatorfiltersfilter.h
locator/locatormanager.cpp locator/locatormanager.h locator/locatormanager.cpp locator/locatormanager.h
locator/locatorsearchutils.cpp locator/locatorsearchutils.h
locator/locatorsettingspage.cpp locator/locatorsettingspage.h locator/locatorsettingspage.cpp locator/locatorsettingspage.h
locator/locatorwidget.cpp locator/locatorwidget.h locator/locatorwidget.cpp locator/locatorwidget.h
locator/opendocumentsfilter.cpp locator/opendocumentsfilter.h locator/opendocumentsfilter.cpp locator/opendocumentsfilter.h

View File

@@ -6,7 +6,6 @@
#include "designmode.h" #include "designmode.h"
#include "editmode.h" #include "editmode.h"
#include "foldernavigationwidget.h" #include "foldernavigationwidget.h"
#include "helpmanager.h"
#include "icore.h" #include "icore.h"
#include "idocument.h" #include "idocument.h"
#include "iwizardfactory.h" #include "iwizardfactory.h"
@@ -472,8 +471,7 @@ QString CorePlugin::msgCrashpadInformation()
ExtensionSystem::IPlugin::ShutdownFlag CorePlugin::aboutToShutdown() ExtensionSystem::IPlugin::ShutdownFlag CorePlugin::aboutToShutdown()
{ {
Find::aboutToShutdown(); Find::aboutToShutdown();
ExtensionSystem::IPlugin::ShutdownFlag shutdownFlag = m_locator->aboutToShutdown( m_locator->aboutToShutdown();
[this] { emit asynchronousShutdownFinished(); });
m_mainWindow->aboutToShutdown(); m_mainWindow->aboutToShutdown();
return shutdownFlag; return SynchronousShutdown;
} }

View File

@@ -350,8 +350,6 @@ Project {
"locatormanager.h", "locatormanager.h",
"locator.cpp", "locator.cpp",
"locator.h", "locator.h",
"locatorsearchutils.cpp",
"locatorsearchutils.h",
"locatorsettingspage.cpp", "locatorsettingspage.cpp",
"locatorsettingspage.h", "locatorsettingspage.h",
"locatorwidget.cpp", "locatorwidget.cpp",

View File

@@ -229,7 +229,8 @@ public:
virtual void prepareSearch(const QString &entry); virtual void prepareSearch(const QString &entry);
virtual QList<LocatorFilterEntry> matchesFor(QFutureInterface<LocatorFilterEntry> &future, const QString &entry) = 0; virtual QList<LocatorFilterEntry> matchesFor(QFutureInterface<LocatorFilterEntry> &,
const QString &) { return {}; };
virtual QByteArray saveState() const; virtual QByteArray saveState() const;
virtual void restoreState(const QByteArray &state); virtual void restoreState(const QByteArray &state);

View File

@@ -145,13 +145,11 @@ bool Locator::delayedInitialize()
return true; return true;
} }
ExtensionSystem::IPlugin::ShutdownFlag Locator::aboutToShutdown( void Locator::aboutToShutdown()
const std::function<void()> &emitAsynchronousShutdownFinished)
{ {
m_shuttingDown = true; m_shuttingDown = true;
m_refreshTimer.stop(); m_refreshTimer.stop();
m_taskTree.reset(); m_taskTree.reset();
return LocatorWidget::aboutToShutdown(emitAsynchronousShutdownFinished);
} }
void Locator::loadSettings() void Locator::loadSettings()

View File

@@ -30,8 +30,7 @@ public:
~Locator() override; ~Locator() override;
static Locator *instance(); static Locator *instance();
ExtensionSystem::IPlugin::ShutdownFlag aboutToShutdown( void aboutToShutdown();
const std::function<void()> &emitAsynchronousShutdownFinished);
void initialize(); void initialize();
void extensionsInitialized(); void extensionsInitialized();

View File

@@ -1,35 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#include "locatorsearchutils.h"
#include <utils/link.h>
#include <unordered_set>
void Core::Internal::runSearch(QFutureInterface<Core::LocatorFilterEntry> &future,
const QList<ILocatorFilter *> &filters, const QString &searchText)
{
std::unordered_set<Utils::Link> addedCache;
const bool checkDuplicates = (filters.size() > 1);
const auto duplicatesRemoved = [&](const QList<LocatorFilterEntry> &entries) {
if (!checkDuplicates)
return entries;
QList<LocatorFilterEntry> results;
results.reserve(entries.size());
for (const LocatorFilterEntry &entry : entries) {
const auto &link = entry.linkForEditor;
if (!link || addedCache.emplace(*link).second)
results.append(entry);
}
return results;
};
for (ILocatorFilter *filter : filters) {
if (future.isCanceled())
break;
const auto results = duplicatesRemoved(filter->matchesFor(future, searchText));
if (!results.isEmpty())
future.reportResults(results);
}
}

View File

@@ -1,16 +0,0 @@
// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
#pragma once
#include "ilocatorfilter.h"
namespace Core {
namespace Internal {
void CORE_EXPORT runSearch(QFutureInterface<LocatorFilterEntry> &future,
const QList<ILocatorFilter *> &filters,
const QString &searchText);
} // namespace Internal
} // namespace Core

View File

@@ -7,7 +7,6 @@
#include "locator.h" #include "locator.h"
#include "locatorconstants.h" #include "locatorconstants.h"
#include "locatormanager.h" #include "locatormanager.h"
#include "locatorsearchutils.h"
#include "../actionmanager/actionmanager.h" #include "../actionmanager/actionmanager.h"
#include "../coreplugintr.h" #include "../coreplugintr.h"
#include "../editormanager/editormanager.h" #include "../editormanager/editormanager.h"
@@ -25,7 +24,6 @@
#include <utils/itemviews.h> #include <utils/itemviews.h>
#include <utils/progressindicator.h> #include <utils/progressindicator.h>
#include <utils/qtcassert.h> #include <utils/qtcassert.h>
#include <utils/runextensions.h>
#include <utils/stylehelper.h> #include <utils/stylehelper.h>
#include <utils/tooltip/tooltip.h> #include <utils/tooltip/tooltip.h>
#include <utils/utilsicons.h> #include <utils/utilsicons.h>
@@ -54,16 +52,6 @@ const int LocatorEntryRole = int(HighlightingItemRole::User);
namespace Core { namespace Core {
namespace Internal { namespace Internal {
static bool isUsingLocatorMatcher()
{
const QString value = qtcEnvironmentVariable("QTC_USE_MATCHER", "").toLower();
return !(value == "false" || value == "off");
}
bool LocatorWidget::m_shuttingDown = false;
QFuture<void> LocatorWidget::m_sharedFuture;
LocatorWidget *LocatorWidget::m_sharedFutureOrigin = nullptr;
/* A model to represent the Locator results. */ /* A model to represent the Locator results. */
class LocatorModel : public QAbstractListModel class LocatorModel : public QAbstractListModel
{ {
@@ -631,12 +619,6 @@ LocatorWidget::LocatorWidget(Locator *locator)
connect(m_fileLineEdit, &QLineEdit::textChanged, connect(m_fileLineEdit, &QLineEdit::textChanged,
this, &LocatorWidget::showPopupDelayed); this, &LocatorWidget::showPopupDelayed);
m_entriesWatcher = new QFutureWatcher<LocatorFilterEntry>(this);
connect(m_entriesWatcher, &QFutureWatcher<LocatorFilterEntry>::resultsReadyAt,
this, &LocatorWidget::addSearchResults);
connect(m_entriesWatcher, &QFutureWatcher<LocatorFilterEntry>::finished,
this, &LocatorWidget::handleSearchFinished);
m_showPopupTimer.setInterval(100); m_showPopupTimer.setInterval(100);
m_showPopupTimer.setSingleShot(true); m_showPopupTimer.setSingleShot(true);
connect(&m_showPopupTimer, &QTimer::timeout, this, &LocatorWidget::showPopupNow); connect(&m_showPopupTimer, &QTimer::timeout, this, &LocatorWidget::showPopupNow);
@@ -664,12 +646,7 @@ LocatorWidget::LocatorWidget(Locator *locator)
updateFilterList(); updateFilterList();
} }
LocatorWidget::~LocatorWidget() LocatorWidget::~LocatorWidget() = default;
{
// no need to completely finish a running search, cancel it
if (m_entriesWatcher->future().isRunning())
m_entriesWatcher->future().cancel();
}
void LocatorWidget::updatePlaceholderText(Command *command) void LocatorWidget::updatePlaceholderText(Command *command)
{ {
@@ -878,7 +855,7 @@ void LocatorWidget::showPopupDelayed()
void LocatorWidget::showPopupNow() void LocatorWidget::showPopupNow()
{ {
m_showPopupTimer.stop(); m_showPopupTimer.stop();
updateCompletionList(m_fileLineEdit->text()); runMatcher(m_fileLineEdit->text());
emit showPopup(); emit showPopup();
} }
@@ -973,91 +950,6 @@ void LocatorWidget::runMatcher(const QString &text)
m_locatorMatcher->start(); m_locatorMatcher->start();
} }
// TODO: Remove when switched the default into new implementation.
static void printMatcherInfo()
{
static bool printed = false;
if (printed)
return;
printed = true;
if (isUsingLocatorMatcher()) {
qDebug() << "Using the new LocatorMatcher implementation (default). In order to switch "
"back to the old implementation, set QTC_USE_MATCHER=FALSE env var.";
return;
}
qDebug() << "QTC_USE_MATCHER env var set to FALSE, using the old matchesFor implementation.";
}
void LocatorWidget::updateCompletionList(const QString &text)
{
if (m_shuttingDown)
return;
printMatcherInfo();
if (isUsingLocatorMatcher()) {
runMatcher(text);
return;
}
m_updateRequested = true;
if (m_sharedFuture.isRunning()) {
// Cancel the old future. We may not just block the UI thread to wait for the search to
// actually cancel.
m_requestedCompletionText = text;
if (m_sharedFutureOrigin == this) {
// This locator widget is currently running. Make handleSearchFinished trigger another
// update.
m_rerunAfterFinished = true;
} else {
// Another locator widget is running. Trigger another update when that is finished.
Utils::onFinished(m_sharedFuture, this, [this](const QFuture<void> &) {
const QString text = m_requestedCompletionText;
m_requestedCompletionText.clear();
updateCompletionList(text);
});
}
m_sharedFuture.cancel();
return;
}
m_showProgressTimer.start();
m_needsClearResult = true;
QString searchText;
const QList<ILocatorFilter *> filters = filtersFor(text, searchText);
for (ILocatorFilter *filter : filters)
filter->prepareSearch(searchText);
QFuture<LocatorFilterEntry> future = Utils::runAsync(&runSearch, filters, searchText);
m_sharedFuture = QFuture<void>(future);
m_sharedFutureOrigin = this;
m_entriesWatcher->setFuture(future);
}
void LocatorWidget::handleSearchFinished()
{
m_showProgressTimer.stop();
setProgressIndicatorVisible(false);
m_updateRequested = false;
if (m_rowRequestedForAccept) {
acceptEntry(m_rowRequestedForAccept.value());
m_rowRequestedForAccept.reset();
return;
}
if (m_rerunAfterFinished) {
m_rerunAfterFinished = false;
const QString text = m_requestedCompletionText;
m_requestedCompletionText.clear();
updateCompletionList(text);
return;
}
if (m_needsClearResult) {
m_locatorModel->clear();
m_needsClearResult = false;
}
}
void LocatorWidget::scheduleAcceptEntry(const QModelIndex &index) void LocatorWidget::scheduleAcceptEntry(const QModelIndex &index)
{ {
if (m_updateRequested) { if (m_updateRequested) {
@@ -1065,31 +957,12 @@ void LocatorWidget::scheduleAcceptEntry(const QModelIndex &index)
// accept will be called after the update finished // accept will be called after the update finished
m_rowRequestedForAccept = index.row(); m_rowRequestedForAccept = index.row();
// do not wait for the rest of the search to finish // do not wait for the rest of the search to finish
if (isUsingLocatorMatcher()) m_locatorMatcher.reset();
m_locatorMatcher.reset();
else
m_entriesWatcher->future().cancel();
} else { } else {
acceptEntry(index.row()); acceptEntry(index.row());
} }
} }
ExtensionSystem::IPlugin::ShutdownFlag LocatorWidget::aboutToShutdown(
const std::function<void()> &emitAsynchronousShutdownFinished)
{
m_shuttingDown = true;
if (m_sharedFuture.isRunning()) {
Utils::onFinished(m_sharedFuture,
Locator::instance(),
[emitAsynchronousShutdownFinished](const QFuture<void> &) {
emitAsynchronousShutdownFinished();
});
m_sharedFuture.cancel();
return ExtensionSystem::IPlugin::AsynchronousShutdown;
}
return ExtensionSystem::IPlugin::SynchronousShutdown;
}
void LocatorWidget::acceptEntry(int row) void LocatorWidget::acceptEntry(int row)
{ {
if (row < 0 || row >= m_locatorModel->rowCount()) if (row < 0 || row >= m_locatorModel->rowCount())
@@ -1150,24 +1023,6 @@ void LocatorWidget::showConfigureDialog()
ICore::showOptionsDialog(Constants::FILTER_OPTIONS_PAGE); ICore::showOptionsDialog(Constants::FILTER_OPTIONS_PAGE);
} }
void LocatorWidget::addSearchResults(int firstIndex, int endIndex)
{
if (m_needsClearResult) {
m_locatorModel->clear();
m_needsClearResult = false;
}
const bool selectFirst = m_locatorModel->rowCount() == 0;
QList<LocatorFilterEntry> entries;
for (int i = firstIndex; i < endIndex; ++i)
entries.append(m_entriesWatcher->resultAt(i));
m_locatorModel->addEntries(entries);
if (selectFirst) {
emit selectRow(0);
if (m_rowRequestedForAccept)
m_rowRequestedForAccept = 0;
}
}
LocatorWidget *createStaticLocatorWidget(Locator *locator) LocatorWidget *createStaticLocatorWidget(Locator *locator)
{ {
auto widget = new LocatorWidget(locator); auto widget = new LocatorWidget(locator);

View File

@@ -44,9 +44,6 @@ public:
void scheduleAcceptEntry(const QModelIndex &index); void scheduleAcceptEntry(const QModelIndex &index);
static ExtensionSystem::IPlugin::ShutdownFlag aboutToShutdown(
const std::function<void()> &emitAsynchronousShutdownFinished);
signals: signals:
void showCurrentItemToolTip(); void showCurrentItemToolTip();
void lostFocus(); void lostFocus();
@@ -61,8 +58,6 @@ private:
void showPopupNow(); void showPopupNow();
void acceptEntry(int row); void acceptEntry(int row);
static void showConfigureDialog(); static void showConfigureDialog();
void addSearchResults(int firstIndex, int endIndex);
void handleSearchFinished();
void updateFilterList(); void updateFilterList();
bool isInMainWindow() const; bool isInMainWindow() const;
@@ -70,27 +65,18 @@ private:
bool eventFilter(QObject *obj, QEvent *event) override; bool eventFilter(QObject *obj, QEvent *event) override;
void runMatcher(const QString &text); void runMatcher(const QString &text);
void updateCompletionList(const QString &text);
static QList<ILocatorFilter*> filtersFor(const QString &text, QString &searchText); static QList<ILocatorFilter*> filtersFor(const QString &text, QString &searchText);
void setProgressIndicatorVisible(bool visible); void setProgressIndicatorVisible(bool visible);
LocatorModel *m_locatorModel = nullptr; LocatorModel *m_locatorModel = nullptr;
static bool m_shuttingDown;
static QFuture<void> m_sharedFuture;
static LocatorWidget *m_sharedFutureOrigin;
QMenu *m_filterMenu = nullptr; QMenu *m_filterMenu = nullptr;
QAction *m_centeredPopupAction = nullptr; QAction *m_centeredPopupAction = nullptr;
QAction *m_refreshAction = nullptr; QAction *m_refreshAction = nullptr;
QAction *m_configureAction = nullptr; QAction *m_configureAction = nullptr;
Utils::FancyLineEdit *m_fileLineEdit = nullptr; Utils::FancyLineEdit *m_fileLineEdit = nullptr;
QTimer m_showPopupTimer; QTimer m_showPopupTimer;
QFutureWatcher<LocatorFilterEntry> *m_entriesWatcher = nullptr;
QString m_requestedCompletionText;
bool m_needsClearResult = true; bool m_needsClearResult = true;
bool m_updateRequested = false; bool m_updateRequested = false;
bool m_rerunAfterFinished = false;
bool m_possibleToolTipRequest = false; bool m_possibleToolTipRequest = false;
QWidget *m_progressIndicator = nullptr; QWidget *m_progressIndicator = nullptr;
QTimer m_showProgressTimer; QTimer m_showProgressTimer;