diff options
Diffstat (limited to 'src/plugins/coreplugin/locator/opendocumentsfilter.cpp')
-rw-r--r-- | src/plugins/coreplugin/locator/opendocumentsfilter.cpp | 129 |
1 files changed, 44 insertions, 85 deletions
diff --git a/src/plugins/coreplugin/locator/opendocumentsfilter.cpp b/src/plugins/coreplugin/locator/opendocumentsfilter.cpp index f8febc69f30..95e076ec2bd 100644 --- a/src/plugins/coreplugin/locator/opendocumentsfilter.cpp +++ b/src/plugins/coreplugin/locator/opendocumentsfilter.cpp @@ -3,102 +3,63 @@ #include "opendocumentsfilter.h" -#include "basefilefilter.h" #include "../coreplugintr.h" -#include <utils/filepath.h> +#include <coreplugin/editormanager/documentmodel.h> + +#include <extensionsystem/pluginmanager.h> + +#include <utils/algorithm.h> +#include <utils/async.h> #include <utils/link.h> -#include <utils/linecolumn.h> -#include <QAbstractItemModel> -#include <QMutexLocker> #include <QRegularExpression> +using namespace Tasking; using namespace Utils; namespace Core::Internal { +class Entry +{ +public: + Utils::FilePath fileName; + QString displayName; +}; + OpenDocumentsFilter::OpenDocumentsFilter() { setId("Open documents"); setDisplayName(Tr::tr("Open Documents")); + setDescription(Tr::tr("Switches to an open document.")); setDefaultShortcutString("o"); setPriority(High); setDefaultIncludedByDefault(true); - - connect(DocumentModel::model(), &QAbstractItemModel::dataChanged, - this, &OpenDocumentsFilter::slotDataChanged); - connect(DocumentModel::model(), &QAbstractItemModel::rowsInserted, - this, &OpenDocumentsFilter::slotRowsInserted); - connect(DocumentModel::model(), &QAbstractItemModel::rowsRemoved, - this, &OpenDocumentsFilter::slotRowsRemoved); -} - -void OpenDocumentsFilter::slotDataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, - const QVector<int> &roles) -{ - Q_UNUSED(roles) - - const int topIndex = std::max(0, topLeft.row() - 1 /*<no document>*/); - const int bottomIndex = bottomRight.row() - 1 /*<no document>*/; - - QMutexLocker lock(&m_mutex); - - const QList<DocumentModel::Entry *> documentEntries = DocumentModel::entries(); - for (int i = topIndex; i <= bottomIndex; ++i) { - QTC_ASSERT(i < m_editors.size(), break); - DocumentModel::Entry *e = documentEntries.at(i); - m_editors[i] = {e->filePath(), e->displayName()}; - } -} - -void OpenDocumentsFilter::slotRowsInserted(const QModelIndex &, int first, int last) -{ - const int firstIndex = std::max(0, first - 1 /*<no document>*/); - - QMutexLocker lock(&m_mutex); - - const QList<DocumentModel::Entry *> documentEntries = DocumentModel::entries(); - for (int i = firstIndex; i < last; ++i) { - DocumentModel::Entry *e = documentEntries.at(i); - m_editors.insert(i, {e->filePath(), e->displayName()}); - } } -void OpenDocumentsFilter::slotRowsRemoved(const QModelIndex &, int first, int last) +static void matchEditors(QPromise<void> &promise, const LocatorStorage &storage, + const QList<Entry> &editorsData) { - QMutexLocker lock(&m_mutex); - - const int firstIndex = std::max(0, first - 1 /*<no document>*/); - for (int i = firstIndex; i < last; ++i) - m_editors.removeAt(i); -} + const Link link = Link::fromString(storage.input(), true); + const QRegularExpression regexp = ILocatorFilter::createRegExp(link.targetFilePath.toString()); + if (!regexp.isValid()) + return; -QList<LocatorFilterEntry> OpenDocumentsFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future, - const QString &entry) -{ - QList<LocatorFilterEntry> goodEntries; - QList<LocatorFilterEntry> betterEntries; - const Link link = Link::fromString(entry, true); + LocatorFilterEntries goodEntries; + LocatorFilterEntries betterEntries; - const QRegularExpression regexp = createRegExp(link.targetFilePath.toString()); - if (!regexp.isValid()) - return goodEntries; - - const QList<Entry> editorEntries = editors(); - for (const Entry &editorEntry : editorEntries) { - if (future.isCanceled()) - break; - QString fileName = editorEntry.fileName.toString(); - if (fileName.isEmpty()) + for (const Entry &editorData : editorsData) { + if (promise.isCanceled()) + return; + if (editorData.fileName.isEmpty()) continue; - QString displayName = editorEntry.displayName; - const QRegularExpressionMatch match = regexp.match(displayName); + const QRegularExpressionMatch match = regexp.match(editorData.displayName); if (match.hasMatch()) { - LocatorFilterEntry filterEntry(this, displayName); - filterEntry.filePath = FilePath::fromString(fileName); + LocatorFilterEntry filterEntry; + filterEntry.displayName = editorData.displayName; + filterEntry.filePath = editorData.fileName; filterEntry.extraInfo = filterEntry.filePath.shortNativePath(); - filterEntry.highlightInfo = highlightInfo(match); + filterEntry.highlightInfo = ILocatorFilter::highlightInfo(match); filterEntry.linkForEditor = Link(filterEntry.filePath, link.targetLine, link.targetColumn); if (match.capturedStart() == 0) @@ -107,23 +68,21 @@ QList<LocatorFilterEntry> OpenDocumentsFilter::matchesFor(QFutureInterface<Locat goodEntries.append(filterEntry); } } - betterEntries.append(goodEntries); - return betterEntries; + storage.reportOutput(betterEntries + goodEntries); } -QList<OpenDocumentsFilter::Entry> OpenDocumentsFilter::editors() const +LocatorMatcherTasks OpenDocumentsFilter::matchers() { - QMutexLocker lock(&m_mutex); - return m_editors; -} + TreeStorage<LocatorStorage> storage; -void OpenDocumentsFilter::accept(const LocatorFilterEntry &selection, - QString *newText, int *selectionStart, int *selectionLength) const -{ - Q_UNUSED(newText) - Q_UNUSED(selectionStart) - Q_UNUSED(selectionLength) - BaseFileFilter::openEditorAt(selection); + const auto onSetup = [storage](Async<void> &async) { + const QList<Entry> editorsData = Utils::transform(DocumentModel::entries(), + [](const DocumentModel::Entry *e) { return Entry{e->filePath(), e->displayName()}; }); + async.setFutureSynchronizer(ExtensionSystem::PluginManager::futureSynchronizer()); + async.setConcurrentCallData(matchEditors, *storage, editorsData); + }; + + return {{AsyncTask<void>(onSetup), storage}}; } -} // Core::Internal +} // namespace Core::Internal |