Browse Source

Merge pull request #65315 from Mickeon/editor-help-search-multi-term

Allow multiple words in Docs Help Search filter
Rémi Verschelde 3 years ago
parent
commit
a140efbbeb
2 changed files with 37 additions and 32 deletions
  1. 34 32
      editor/editor_help_search.cpp
  2. 3 0
      editor/editor_help_search.h

+ 34 - 32
editor/editor_help_search.cpp

@@ -320,6 +320,11 @@ bool EditorHelpSearch::Runner::_phase_match_classes_init() {
 	matched_item = nullptr;
 	matched_item = nullptr;
 	match_highest_score = 0;
 	match_highest_score = 0;
 
 
+	terms = term.split_spaces();
+	if (terms.is_empty()) {
+		terms.append(term);
+	}
+
 	return true;
 	return true;
 }
 }
 
 
@@ -350,62 +355,38 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
 		// Make an exception for annotations, since there are not that many of them.
 		// Make an exception for annotations, since there are not that many of them.
 		if (term.length() > 1 || term == "@") {
 		if (term.length() > 1 || term == "@") {
 			if (search_flags & SEARCH_CONSTRUCTORS) {
 			if (search_flags & SEARCH_CONSTRUCTORS) {
-				for (int i = 0; i < class_doc.constructors.size(); i++) {
-					String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
-					if (method_name.find(term) > -1 ||
-							(term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
-							(term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
-							(term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
-						match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
-					}
-				}
+				_match_method_name_and_push_back(class_doc.constructors, &match.constructors);
 			}
 			}
 			if (search_flags & SEARCH_METHODS) {
 			if (search_flags & SEARCH_METHODS) {
-				for (int i = 0; i < class_doc.methods.size(); i++) {
-					String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
-					if (method_name.find(term) > -1 ||
-							(term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
-							(term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
-							(term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
-						match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
-					}
-				}
+				_match_method_name_and_push_back(class_doc.methods, &match.methods);
 			}
 			}
 			if (search_flags & SEARCH_OPERATORS) {
 			if (search_flags & SEARCH_OPERATORS) {
-				for (int i = 0; i < class_doc.operators.size(); i++) {
-					String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
-					if (method_name.find(term) > -1 ||
-							(term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
-							(term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
-							(term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
-						match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
-					}
-				}
+				_match_method_name_and_push_back(class_doc.operators, &match.operators);
 			}
 			}
 			if (search_flags & SEARCH_SIGNALS) {
 			if (search_flags & SEARCH_SIGNALS) {
 				for (int i = 0; i < class_doc.signals.size(); i++) {
 				for (int i = 0; i < class_doc.signals.size(); i++) {
-					if (_match_string(term, class_doc.signals[i].name)) {
+					if (_all_terms_in_name(class_doc.signals[i].name)) {
 						match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
 						match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
 					}
 					}
 				}
 				}
 			}
 			}
 			if (search_flags & SEARCH_CONSTANTS) {
 			if (search_flags & SEARCH_CONSTANTS) {
 				for (int i = 0; i < class_doc.constants.size(); i++) {
 				for (int i = 0; i < class_doc.constants.size(); i++) {
-					if (_match_string(term, class_doc.constants[i].name)) {
+					if (_all_terms_in_name(class_doc.constants[i].name)) {
 						match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
 						match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
 					}
 					}
 				}
 				}
 			}
 			}
 			if (search_flags & SEARCH_PROPERTIES) {
 			if (search_flags & SEARCH_PROPERTIES) {
 				for (int i = 0; i < class_doc.properties.size(); i++) {
 				for (int i = 0; i < class_doc.properties.size(); i++) {
-					if (_match_string(term, class_doc.properties[i].name) || _match_string(term, class_doc.properties[i].getter) || _match_string(term, class_doc.properties[i].setter)) {
+					if (_all_terms_in_name(class_doc.properties[i].name)) {
 						match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
 						match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
 					}
 					}
 				}
 				}
 			}
 			}
 			if (search_flags & SEARCH_THEME_ITEMS) {
 			if (search_flags & SEARCH_THEME_ITEMS) {
 				for (int i = 0; i < class_doc.theme_properties.size(); i++) {
 				for (int i = 0; i < class_doc.theme_properties.size(); i++) {
-					if (_match_string(term, class_doc.theme_properties[i].name)) {
+					if (_all_terms_in_name(class_doc.theme_properties[i].name)) {
 						match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
 						match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
 					}
 					}
 				}
 				}
@@ -417,7 +398,6 @@ bool EditorHelpSearch::Runner::_phase_match_classes() {
 					}
 					}
 				}
 				}
 			}
 			}
-			matches[class_doc.name] = match;
 		}
 		}
 		matches[class_doc.name] = match;
 		matches[class_doc.name] = match;
 	}
 	}
@@ -514,6 +494,28 @@ bool EditorHelpSearch::Runner::_phase_select_match() {
 	return true;
 	return true;
 }
 }
 
 
+void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, Vector<DocData::MethodDoc *> *r_match_methods) {
+	// Constructors, Methods, Operators...
+	for (int i = 0; i < p_methods.size(); i++) {
+		String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].name : p_methods[i].name.to_lower();
+		if (_all_terms_in_name(method_name) ||
+				(term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
+				(term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
+				(term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
+			r_match_methods->push_back(const_cast<DocData::MethodDoc *>(&p_methods[i]));
+		}
+	}
+}
+
+bool EditorHelpSearch::Runner::_all_terms_in_name(String name) {
+	for (int i = 0; i < terms.size(); i++) {
+		if (!_match_string(terms[i], name)) {
+			return false;
+		}
+	}
+	return true;
+}
+
 bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
 bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
 	if (search_flags & SEARCH_CASE_SENSITIVE) {
 	if (search_flags & SEARCH_CASE_SENSITIVE) {
 		return p_string.find(p_term) > -1;
 		return p_string.find(p_term) > -1;

+ 3 - 0
editor/editor_help_search.h

@@ -119,6 +119,7 @@ class EditorHelpSearch::Runner : public RefCounted {
 	Control *ui_service = nullptr;
 	Control *ui_service = nullptr;
 	Tree *results_tree = nullptr;
 	Tree *results_tree = nullptr;
 	String term;
 	String term;
+	Vector<String> terms;
 	int search_flags;
 	int search_flags;
 
 
 	Ref<Texture2D> empty_icon;
 	Ref<Texture2D> empty_icon;
@@ -145,6 +146,8 @@ class EditorHelpSearch::Runner : public RefCounted {
 
 
 	String _build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const;
 	String _build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const;
 
 
+	void _match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, Vector<DocData::MethodDoc *> *r_match_methods);
+	bool _all_terms_in_name(String name);
 	bool _match_string(const String &p_term, const String &p_string) const;
 	bool _match_string(const String &p_term, const String &p_string) const;
 	void _match_item(TreeItem *p_item, const String &p_text);
 	void _match_item(TreeItem *p_item, const String &p_text);
 	TreeItem *_create_class_hierarchy(const ClassMatch &p_match);
 	TreeItem *_create_class_hierarchy(const ClassMatch &p_match);