diff --git a/src/foldermodel.cpp b/src/foldermodel.cpp index 465ea17b..7799e8ee 100644 --- a/src/foldermodel.cpp +++ b/src/foldermodel.cpp @@ -416,13 +416,36 @@ struct SearchFilter { } bool matches(const QFileInfo &info) const { - const QString name = info.fileName(); - if (!extensions.isEmpty() && (info.isDir() || !extensions.contains(info.suffix().toLower()))) + return matches(info.fileName(), info.isDir(), info.suffix()); + } + + bool matches(const QString &name, bool isDir, const QString &suffix) const { + if (!extensions.isEmpty() && (isDir || !extensions.contains(suffix.toLower()))) return false; return name.contains(text, Qt::CaseInsensitive); } + + // Whether what this matches, `other` matches too: its text holds ours, + // and it takes no extension we don't (or we take any). + bool narrowedBy(const SearchFilter &other) const { + if (!other.text.contains(text, Qt::CaseInsensitive)) + return false; + if (extensions.isEmpty()) + return true; + if (other.extensions.isEmpty()) + return false; + return std::all_of(other.extensions.begin(), other.extensions.end(), + [&](const QString &ext) { return extensions.contains(ext); }); + } }; +// How many matches a search of a folder shows before it stops looking. +static constexpr int searchLimit = 1000; + +bool FolderModel::narrows(const QString &from, const QString &to) { + return SearchFilter(from).narrowedBy(SearchFilter(to)); +} + QVector FolderModel::search(const QString &root, const QString &query, bool showHidden, const QString &sort, const std::function &stop, int limit, const QLocale &locale, const QStringList &filter, @@ -628,6 +651,7 @@ void FolderModel::reload() { return; } m_shownQuery.clear(); + m_complete = false; const auto latest = m_latest; auto *watcher = new QFutureWatcher, QString, QStringList, QHash>, QHash>>(this); connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] { @@ -854,6 +878,28 @@ void FolderModel::searchHere(quint64 generation) { const bool fresh = query != m_shownQuery; m_shownQuery = query; m_staleClear.stop(); + // What's shown is all the previous search found, and this one only asks + // for less of it: keep what still matches, with no walk. + const QString key = path + QLatin1Char('\n') + (showHidden ? "h" : "") + QLatin1Char('\n') + filter.join(QLatin1Char('\n')); + if (fresh && m_complete && key == m_completeKey && narrows(m_completeQuery, query)) { + const SearchFilter wanted(query); + QVector kept; + for (const Entry &e : std::as_const(m_entries)) + if (wanted.matches(QFileInfo(e.path).fileName(), e.isDir, e.extension)) + kept.push_back(e); + m_completeQuery = query; + setError({}); + setEntries(kept); + QSet still; + for (const Entry &e : std::as_const(m_entries)) + if (m_selected.contains(e.path)) + still.insert(e.path); + setSelected(still); + setLoading(false); + emit loaded(); + return; + } + m_complete = false; if (fresh) clearEntries(); setError({}); @@ -870,7 +916,7 @@ void FolderModel::searchHere(quint64 generation) { emit loaded(); } }); - connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, fresh, announced] { + connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation, fresh, announced, query, key] { watcher->deleteLater(); if (generation != m_generation) return; @@ -888,12 +934,17 @@ void FolderModel::searchHere(quint64 generation) { still.insert(e.path); setSelected(still); } + // Unless it was cut short by the limit, this is all there is to + // narrow by a longer query. + m_complete = m_entries.size() < searchLimit; + m_completeQuery = query; + m_completeKey = key; setLoading(false); emit loaded(); countFolders(generation); }); watcher->setFuture(QtConcurrent::run(&FolderModel::listingPool(), [path, query, showHidden, sort, filter, locale, latest, generation](QPromise> &promise) { - search(path, query, showHidden, sort, [&] { return *latest != generation; }, 1000, locale, filter, + search(path, query, showHidden, sort, [&] { return *latest != generation; }, searchLimit, locale, filter, [&](const QVector &found) { promise.addResult(found); }); })); } diff --git a/src/foldermodel.h b/src/foldermodel.h index 020f1ba2..9e0d1c84 100644 --- a/src/foldermodel.h +++ b/src/foldermodel.h @@ -197,6 +197,11 @@ class FolderModel : public QAbstractListModel { const QLocale &locale = QLocale(), const QStringList &filter = {}, const std::function &)> &found = {}); + // Whether every match of the search `from` is also a match of `to`, so + // typing on ("beac" to "beach", or adding an ext:) can narrow what's + // already found instead of looking again. + static bool narrows(const QString &from, const QString &to); + signals: void pathChanged(); void countChanged(); @@ -222,6 +227,12 @@ class FolderModel : public QAbstractListModel { void insertFound(const QVector &found); // The search whose matches are shown (or being found). QString m_shownQuery; + // Set, m_entries holds every match of m_completeQuery (not cut short by + // the limit) in the folder that m_completeKey names, so a search that + // narrows it is answered from them. + bool m_complete = false; + QString m_completeQuery; + QString m_completeKey; void setEntries(const QVector &entries); void clearEntries(); QString cacheKey() const; diff --git a/tests/folio_tests.cpp b/tests/folio_tests.cpp index 1de00c72..736f1ba9 100644 --- a/tests/folio_tests.cpp +++ b/tests/folio_tests.cpp @@ -1096,6 +1096,65 @@ private slots: QCOMPARE(names("my notes"), QString("my notes.txt")); } + void searchNarrowsWhatsAlreadyFound() + { + QVERIFY(FolderModel::narrows("beac", "beach")); + QVERIFY(FolderModel::narrows("BEAC", "beach")); + QVERIFY(FolderModel::narrows("beach", "beach")); + QVERIFY(!FolderModel::narrows("beach", "beac")); + QVERIFY(!FolderModel::narrows("beach", "sea")); + // An extension narrows; dropping it or taking another widens. + QVERIFY(FolderModel::narrows("beach", "beach ext:jpg")); + QVERIFY(FolderModel::narrows("ext:jpg,png", "ext:jpg")); + QVERIFY(FolderModel::narrows("ext:jpg", "img ext:jpg")); + QVERIFY(!FolderModel::narrows("ext:jpg", "ext:jpg,png")); + QVERIFY(!FolderModel::narrows("ext:jpg", "ext:png")); + QVERIFY(!FolderModel::narrows("ext:jpg", "beach")); + // Plain text is matched as typed, spaces and all. + QVERIFY(!FolderModel::narrows("my notes", "my notes")); + QVERIFY(FolderModel::narrows("my", "my notes")); + + // The model answers a longer query from what it found, and the + // same as a fresh search would. + QTemporaryDir dir; + QDir(dir.path()).mkpath("sub"); + write(dir.filePath("beach.jpg"), "x"); + write(dir.filePath("beach.png"), "x"); + write(dir.filePath("sub/beach-2.jpg"), "x"); + write(dir.filePath("sub/beam.txt"), "x"); + write(dir.filePath("sub/beach.d"), "x"); + QDir(dir.filePath("sub")).mkpath("beach-folder"); + FolderModel model; + QSignalSpy loaded(&model, &FolderModel::loaded); + model.setPath(dir.path()); + QVERIFY(loaded.wait(3000)); + auto names = [&] { + QStringList out; + for (int row = 0; row < model.count(); ++row) + out << model.get(row).value("name").toString(); + return out; + }; + model.setQuery("bea"); + QTRY_VERIFY(!model.loading()); + QCOMPARE(model.count(), 6); + // Written after the search, in a folder the model doesn't watch: + // only a new walk would find it. + write(dir.filePath("sub/beach-new.jpg"), "x"); + model.setQuery("beach"); + QVERIFY(!model.loading()); + QCOMPARE(model.count(), 5); + QVERIFY(!names().contains("sub/beach-new.jpg")); + model.selectOnly(model.indexOfPath(dir.filePath("beach.png"))); + model.setQuery("beach ext:jpg"); + QVERIFY(!model.loading()); + QCOMPARE(names().join(", "), QString("beach.jpg, sub/beach-2.jpg")); + QCOMPARE(model.selectedCount(), 0); + // Widening looks again, and finds what was written meanwhile. + model.setQuery("beach"); + QTRY_VERIFY(!model.loading()); + QVERIFY(names().contains("sub/beach-new.jpg")); + } + void folderSearchSkipsSymlinkedFolders() { // A link back up the tree would otherwise be walked round and round.