Merge pull request #47175 from YeldhamDev/help_search_early_match

Select non-perfect matches if necessary in the Search Help dialog
This commit is contained in:
Rémi Verschelde 2021-04-11 22:27:29 +02:00 committed by GitHub
commit 712bb8cbb6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 11 deletions

View file

@ -123,7 +123,7 @@ void EditorHelpSearch::_notification(int p_what) {
if (search->work()) { if (search->work()) {
// Search done. // Search done.
// Only point to the perfect match if it's a new search, and not just reopening a old one. // Only point to the match if it's a new search, and not just reopening a old one.
if (!old_search) { if (!old_search) {
results_tree->ensure_cursor_is_visible(); results_tree->ensure_cursor_is_visible();
} else { } else {
@ -310,6 +310,7 @@ bool EditorHelpSearch::Runner::_phase_match_classes_init() {
iterator_doc = EditorHelp::get_doc_data()->class_list.front(); iterator_doc = EditorHelp::get_doc_data()->class_list.front();
matches.clear(); matches.clear();
matched_item = nullptr; matched_item = nullptr;
match_highest_score = 0;
return true; return true;
} }
@ -460,16 +461,20 @@ bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String
} }
void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text) { void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text) {
if (!matched_item) { float inverse_length = 1.f / float(p_text.length());
if (search_flags & SEARCH_CASE_SENSITIVE) {
if (p_text.casecmp_to(term) == 0) { // Favor types where search term is a substring close to the start of the type.
matched_item = p_item; float w = 0.5f;
} int pos = p_text.findn(term);
} else { float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
if (p_text.nocasecmp_to(term) == 0) {
matched_item = p_item; // Favor shorter items: they resemble the search term more.
} w = 0.1f;
} score *= (1 - w) + w * (term.length() * inverse_length);
if (match_highest_score == 0 || score > match_highest_score) {
matched_item = p_item;
match_highest_score = score;
} }
} }

View file

@ -124,6 +124,7 @@ class EditorHelpSearch::Runner : public Reference {
TreeItem *root_item = nullptr; TreeItem *root_item = nullptr;
Map<String, TreeItem *> class_items; Map<String, TreeItem *> class_items;
TreeItem *matched_item = nullptr; TreeItem *matched_item = nullptr;
float match_highest_score = 0;
bool _is_class_disabled_by_feature_profile(const StringName &p_class); bool _is_class_disabled_by_feature_profile(const StringName &p_class);