Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions src/foldermodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::Entry> FolderModel::search(const QString &root, const QString &query, bool showHidden,
const QString &sort, const std::function<bool()> &stop,
int limit, const QLocale &locale, const QStringList &filter,
Expand Down Expand Up @@ -628,6 +651,7 @@ void FolderModel::reload() {
return;
}
m_shownQuery.clear();
m_complete = false;
const auto latest = m_latest;
auto *watcher = new QFutureWatcher<std::tuple<QVector<Entry>, QString, QStringList, QHash<QString, QVector<Entry>>, QHash<QString, QString>>>(this);
connect(watcher, &QFutureWatcherBase::finished, this, [this, watcher, generation] {
Expand Down Expand Up @@ -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<Entry> kept;
for (const Entry &e : std::as_const(m_entries))
if (wanted.matches(QFileInfo(e.path).fileName(), e.isDir, e.extension))

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Unicode extensions match differently. This passes e.extension, which entryFor() has already uppercased, into a check that lowercases it again. That round trip changes some Unicode letters: a file named notes.ß stores SS, so narrowing notes to notes ext:ß incorrectly removes it, while a fresh search finds it. Use the original suffix from QFileInfo(e.path) rather than the display extension.

kept.push_back(e);
m_completeQuery = query;
setError({});
setEntries(kept);
QSet<QString> 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;
Comment on lines +898 to +900

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Folder counts stop updating. Typing another character after a search finishes can cancel folder counts that are still running. reload() changes m_generation, so their results are discarded, but this early return never starts countFolders(generation) again. On large or slow folders, the remaining results can show no item counts until another full reload. Restart the unfinished counts before returning.

}
m_complete = false;
if (fresh)
clearEntries();
setError({});
Expand All @@ -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;
Expand All @@ -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<QVector<Entry>> &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<Entry> &found) { promise.addResult(found); });
}));
}
Expand Down
11 changes: 11 additions & 0 deletions src/foldermodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,11 @@ class FolderModel : public QAbstractListModel {
const QLocale &locale = QLocale(), const QStringList &filter = {},
const std::function<void(const QVector<Entry> &)> &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.
Comment on lines +200 to +202

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Narrowing contract is backward. The comment says every from match must also match to, but the function requires the reverse: every to match must already match from. For example, beacon matches beac, but not beach, even though this pair correctly returns true. Reverse this description and the matching comment above SearchFilter::narrowedBy so future filters follow the right rule.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

static bool narrows(const QString &from, const QString &to);

signals:
void pathChanged();
void countChanged();
Expand All @@ -222,6 +227,12 @@ class FolderModel : public QAbstractListModel {
void insertFound(const QVector<Entry> &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<Entry> &entries);
void clearEntries();
QString cacheKey() const;
Expand Down
59 changes: 59 additions & 0 deletions tests/folio_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading