diff --git a/Userland/Applications/Assistant/Providers.cpp b/Userland/Applications/Assistant/Providers.cpp index 72413b4988..aed186af00 100644 --- a/Userland/Applications/Assistant/Providers.cpp +++ b/Userland/Applications/Assistant/Providers.cpp @@ -5,6 +5,7 @@ */ #include "Providers.h" +#include #include #include #include @@ -132,7 +133,7 @@ void FileProvider::query(DeprecatedString const& query, Function>>>::construct( [this, query](auto& task) -> Optional>> { - Vector> results; + BinaryHeap sorted_results; for (auto& path : m_full_path_cache) { if (task.is_canceled()) @@ -144,7 +145,20 @@ void FileProvider::query(DeprecatedString const& query, Function sorted_results.peek_min_key()) { + if (sorted_results.size() == MAX_SEARCH_RESULTS) + sorted_results.pop_min(); + + sorted_results.insert(match_result.score, path); + } + } + + Vector> results; + results.ensure_capacity(sorted_results.size()); + while (!sorted_results.is_empty()) { + auto score = sorted_results.peek_min_key(); + auto path = sorted_results.pop_min(); + results.append(make_ref_counted(path, score)); } return results; }, diff --git a/Userland/Applications/Assistant/Providers.h b/Userland/Applications/Assistant/Providers.h index 3d1daba6c2..a802a1ec29 100644 --- a/Userland/Applications/Assistant/Providers.h +++ b/Userland/Applications/Assistant/Providers.h @@ -18,6 +18,8 @@ namespace Assistant { +static constexpr size_t MAX_SEARCH_RESULTS = 6; + class Result : public RefCounted { public: virtual ~Result() = default; diff --git a/Userland/Applications/Assistant/main.cpp b/Userland/Applications/Assistant/main.cpp index 7bda7ebc0f..35f316c291 100644 --- a/Userland/Applications/Assistant/main.cpp +++ b/Userland/Applications/Assistant/main.cpp @@ -144,8 +144,6 @@ private: } -static constexpr size_t MAX_SEARCH_RESULTS = 6; - ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio recvfd sendfd rpath cpath unix proc exec thread")); @@ -270,7 +268,7 @@ ErrorOr serenity_main(Main::Arguments arguments) else app_state.selected_index = 0; app_state.results = results; - app_state.visible_result_count = min(results.size(), MAX_SEARCH_RESULTS); + app_state.visible_result_count = min(results.size(), Assistant::MAX_SEARCH_RESULTS); update_ui_timer->restart(); };