Repository navigation
Narrow a search from what it already found #193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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, | ||
|
|
@@ -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] { | ||
|
|
@@ -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)) | ||
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
| 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<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); }); | ||
| })); | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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(); | ||
|
|
@@ -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; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
e.extension, whichentryFor()has already uppercased, into a check that lowercases it again. That round trip changes some Unicode letters: a file namednotes.ßstoresSS, so narrowingnotestonotes ext:ßincorrectly removes it, while a fresh search finds it. Use the original suffix fromQFileInfo(e.path)rather than the display extension.