editor_help_search.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /**************************************************************************/
  2. /* editor_help_search.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef EDITOR_HELP_SEARCH_H
  31. #define EDITOR_HELP_SEARCH_H
  32. #include "core/templates/rb_map.h"
  33. #include "editor/code_editor.h"
  34. #include "editor/editor_help.h"
  35. #include "editor/plugins/editor_plugin.h"
  36. #include "scene/gui/option_button.h"
  37. #include "scene/gui/tree.h"
  38. class EditorHelpSearch : public ConfirmationDialog {
  39. GDCLASS(EditorHelpSearch, ConfirmationDialog);
  40. enum SearchFlags {
  41. SEARCH_CLASSES = 1 << 0,
  42. SEARCH_CONSTRUCTORS = 1 << 1,
  43. SEARCH_METHODS = 1 << 2,
  44. SEARCH_OPERATORS = 1 << 3,
  45. SEARCH_SIGNALS = 1 << 4,
  46. SEARCH_CONSTANTS = 1 << 5,
  47. SEARCH_PROPERTIES = 1 << 6,
  48. SEARCH_THEME_ITEMS = 1 << 7,
  49. SEARCH_ANNOTATIONS = 1 << 8,
  50. SEARCH_ALL = SEARCH_CLASSES | SEARCH_CONSTRUCTORS | SEARCH_METHODS | SEARCH_OPERATORS | SEARCH_SIGNALS | SEARCH_CONSTANTS | SEARCH_PROPERTIES | SEARCH_THEME_ITEMS | SEARCH_ANNOTATIONS,
  51. SEARCH_CASE_SENSITIVE = 1 << 29,
  52. SEARCH_SHOW_HIERARCHY = 1 << 30
  53. };
  54. LineEdit *search_box = nullptr;
  55. Button *case_sensitive_button = nullptr;
  56. Button *hierarchy_button = nullptr;
  57. OptionButton *filter_combo = nullptr;
  58. Tree *results_tree = nullptr;
  59. bool old_search = false;
  60. String old_term;
  61. int old_search_flags = 0;
  62. class Runner;
  63. Ref<Runner> search;
  64. struct TreeCache {
  65. HashMap<String, TreeItem *> item_cache;
  66. void clear();
  67. ~TreeCache() {
  68. clear();
  69. }
  70. } tree_cache;
  71. void _update_results();
  72. void _search_box_gui_input(const Ref<InputEvent> &p_event);
  73. void _search_box_text_changed(const String &p_text);
  74. void _filter_combo_item_selected(int p_option);
  75. void _confirmed();
  76. bool _all_terms_in_name(const Vector<String> &p_terms, const String &p_name) const;
  77. void _match_method_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::MethodDoc> &p_methods, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const;
  78. void _match_const_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::ConstantDoc> &p_constants, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const;
  79. void _match_property_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::PropertyDoc> &p_properties, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const;
  80. void _match_theme_property_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::ThemeItemDoc> &p_properties, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const;
  81. Dictionary _native_search_cb(const String &p_search_string, int p_result_limit);
  82. void _native_action_cb(const String &p_item_string);
  83. protected:
  84. void _notification(int p_what);
  85. static void _bind_methods();
  86. public:
  87. void popup_dialog();
  88. void popup_dialog(const String &p_term);
  89. EditorHelpSearch();
  90. };
  91. class EditorHelpSearch::Runner : public RefCounted {
  92. enum Phase {
  93. PHASE_MATCH_CLASSES_INIT,
  94. PHASE_MATCH_CLASSES,
  95. PHASE_CLASS_ITEMS_INIT,
  96. PHASE_CLASS_ITEMS,
  97. PHASE_MEMBER_ITEMS_INIT,
  98. PHASE_MEMBER_ITEMS,
  99. PHASE_SELECT_MATCH,
  100. PHASE_MAX
  101. };
  102. int phase = 0;
  103. template <typename T>
  104. struct MemberMatch {
  105. const T *doc = nullptr;
  106. bool name = false;
  107. String keyword;
  108. MemberMatch() {}
  109. MemberMatch(const T *p_doc) :
  110. doc(p_doc) {}
  111. };
  112. struct ClassMatch {
  113. const DocData::ClassDoc *doc = nullptr;
  114. bool name = false;
  115. String keyword;
  116. LocalVector<MemberMatch<DocData::MethodDoc>> constructors;
  117. LocalVector<MemberMatch<DocData::MethodDoc>> methods;
  118. LocalVector<MemberMatch<DocData::MethodDoc>> operators;
  119. LocalVector<MemberMatch<DocData::MethodDoc>> signals;
  120. LocalVector<MemberMatch<DocData::ConstantDoc>> constants;
  121. LocalVector<MemberMatch<DocData::PropertyDoc>> properties;
  122. LocalVector<MemberMatch<DocData::ThemeItemDoc>> theme_properties;
  123. LocalVector<MemberMatch<DocData::MethodDoc>> annotations;
  124. bool required() {
  125. return name || !keyword.is_empty() || !constructors.is_empty() || !methods.is_empty() || !operators.is_empty() || !signals.is_empty() || !constants.is_empty() || !properties.is_empty() || !theme_properties.is_empty() || !annotations.is_empty();
  126. }
  127. };
  128. Control *ui_service = nullptr;
  129. Tree *results_tree = nullptr;
  130. TreeCache *tree_cache = nullptr;
  131. String term;
  132. Vector<String> terms;
  133. int search_flags;
  134. Color disabled_color;
  135. HashMap<String, DocData::ClassDoc>::Iterator iterator_doc;
  136. LocalVector<RBSet<String, NaturalNoCaseComparator>::Element *> iterator_stack;
  137. HashMap<String, ClassMatch> matches;
  138. HashMap<String, ClassMatch>::Iterator iterator_match;
  139. LocalVector<Pair<DocData::ClassDoc *, String>> matched_classes;
  140. TreeItem *root_item = nullptr;
  141. HashMap<String, TreeItem *> class_items;
  142. TreeItem *matched_item = nullptr;
  143. float match_highest_score = 0;
  144. bool _is_class_disabled_by_feature_profile(const StringName &p_class);
  145. void _populate_cache();
  146. bool _find_or_create_item(TreeItem *p_parent, const String &p_item_meta, TreeItem *&r_item);
  147. bool _fill();
  148. bool _phase_fill_classes_init();
  149. bool _phase_fill_classes();
  150. bool _phase_fill_member_items_init();
  151. bool _phase_fill_member_items();
  152. bool _slice();
  153. bool _phase_match_classes_init();
  154. bool _phase_match_classes();
  155. bool _phase_class_items_init();
  156. bool _phase_class_items();
  157. bool _phase_member_items_init();
  158. bool _phase_member_items();
  159. bool _phase_select_match();
  160. String _build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const;
  161. String _build_keywords_tooltip(const String &p_keywords) const;
  162. void _match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, LocalVector<MemberMatch<DocData::MethodDoc>> *r_match_methods);
  163. bool _all_terms_in_name(const String &p_name) const;
  164. String _match_keywords_in_all_terms(const String &p_keywords) const;
  165. bool _match_string(const String &p_term, const String &p_string) const;
  166. String _match_keywords(const String &p_term, const String &p_keywords) const;
  167. void _match_item(TreeItem *p_item, const String &p_text, bool p_is_keywords = false);
  168. TreeItem *_create_class_hierarchy(const ClassMatch &p_match);
  169. TreeItem *_create_class_hierarchy(const DocData::ClassDoc *p_class_doc, const String &p_matching_keyword, bool p_gray);
  170. TreeItem *_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray, const String &p_matching_keyword);
  171. TreeItem *_create_category_item(TreeItem *p_parent, const String &p_class, const StringName &p_icon, const String &p_text, const String &p_metatype);
  172. TreeItem *_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  173. TreeItem *_create_constructor_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  174. TreeItem *_create_operator_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  175. TreeItem *_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  176. TreeItem *_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  177. TreeItem *_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ConstantDoc> &p_match);
  178. TreeItem *_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::PropertyDoc> &p_match);
  179. TreeItem *_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ThemeItemDoc> &p_match);
  180. TreeItem *_create_member_item(TreeItem *p_parent, const String &p_class_name, const StringName &p_icon, const String &p_name, const String &p_text, const String &p_type, const String &p_metatype, const String &p_tooltip, const String &p_keywords, bool p_is_deprecated, bool p_is_experimental, const String &p_matching_keyword);
  181. public:
  182. bool work(uint64_t slot = 100000);
  183. Runner(Control *p_icon_service, Tree *p_results_tree, TreeCache *p_tree_cache, const String &p_term, int p_search_flags);
  184. };
  185. #endif // EDITOR_HELP_SEARCH_H