editor_help_search.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. class Runner;
  62. Ref<Runner> search;
  63. struct TreeCache {
  64. HashMap<String, TreeItem *> item_cache;
  65. void clear();
  66. ~TreeCache() {
  67. clear();
  68. }
  69. } tree_cache;
  70. void _update_results();
  71. void _search_box_gui_input(const Ref<InputEvent> &p_event);
  72. void _search_box_text_changed(const String &p_text);
  73. void _filter_combo_item_selected(int p_option);
  74. void _confirmed();
  75. bool _all_terms_in_name(const Vector<String> &p_terms, const String &p_name) const;
  76. 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;
  77. 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;
  78. 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;
  79. 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;
  80. Dictionary _native_search_cb(const String &p_search_string, int p_result_limit);
  81. void _native_action_cb(const String &p_item_string);
  82. protected:
  83. void _notification(int p_what);
  84. static void _bind_methods();
  85. public:
  86. void popup_dialog();
  87. void popup_dialog(const String &p_term);
  88. EditorHelpSearch();
  89. };
  90. class EditorHelpSearch::Runner : public RefCounted {
  91. enum Phase {
  92. PHASE_MATCH_CLASSES_INIT,
  93. PHASE_MATCH_CLASSES,
  94. PHASE_CLASS_ITEMS_INIT,
  95. PHASE_CLASS_ITEMS,
  96. PHASE_MEMBER_ITEMS_INIT,
  97. PHASE_MEMBER_ITEMS,
  98. PHASE_SELECT_MATCH,
  99. PHASE_MAX
  100. };
  101. int phase = 0;
  102. template <typename T>
  103. struct MemberMatch {
  104. T *doc = nullptr;
  105. bool name = false;
  106. String keyword;
  107. };
  108. struct ClassMatch {
  109. DocData::ClassDoc *doc = nullptr;
  110. bool name = false;
  111. String keyword;
  112. Vector<MemberMatch<DocData::MethodDoc>> constructors;
  113. Vector<MemberMatch<DocData::MethodDoc>> methods;
  114. Vector<MemberMatch<DocData::MethodDoc>> operators;
  115. Vector<MemberMatch<DocData::MethodDoc>> signals;
  116. Vector<MemberMatch<DocData::ConstantDoc>> constants;
  117. Vector<MemberMatch<DocData::PropertyDoc>> properties;
  118. Vector<MemberMatch<DocData::ThemeItemDoc>> theme_properties;
  119. Vector<MemberMatch<DocData::MethodDoc>> annotations;
  120. bool required() {
  121. return name || !keyword.is_empty() || methods.size() || signals.size() || constants.size() || properties.size() || theme_properties.size() || annotations.size();
  122. }
  123. };
  124. Control *ui_service = nullptr;
  125. Tree *results_tree = nullptr;
  126. TreeCache *tree_cache = nullptr;
  127. String term;
  128. Vector<String> terms;
  129. int search_flags;
  130. Color disabled_color;
  131. HashMap<String, DocData::ClassDoc>::Iterator iterator_doc;
  132. LocalVector<RBSet<String, NaturalNoCaseComparator>::Element *> iterator_stack;
  133. HashMap<String, ClassMatch> matches;
  134. HashMap<String, ClassMatch>::Iterator iterator_match;
  135. TreeItem *root_item = nullptr;
  136. HashMap<String, TreeItem *> class_items;
  137. TreeItem *matched_item = nullptr;
  138. float match_highest_score = 0;
  139. bool _is_class_disabled_by_feature_profile(const StringName &p_class);
  140. void _populate_cache();
  141. bool _find_or_create_item(TreeItem *p_parent, const String &p_item_meta, TreeItem *&r_item);
  142. bool _slice();
  143. bool _phase_match_classes_init();
  144. bool _phase_match_classes();
  145. bool _phase_class_items_init();
  146. bool _phase_class_items();
  147. bool _phase_member_items_init();
  148. bool _phase_member_items();
  149. bool _phase_select_match();
  150. String _build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const;
  151. String _build_keywords_tooltip(const String &p_keywords) const;
  152. void _match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, Vector<MemberMatch<DocData::MethodDoc>> *r_match_methods);
  153. bool _all_terms_in_name(const String &p_name) const;
  154. String _match_keywords_in_all_terms(const String &p_keywords) const;
  155. bool _match_string(const String &p_term, const String &p_string) const;
  156. String _match_keywords(const String &p_term, const String &p_keywords) const;
  157. void _match_item(TreeItem *p_item, const String &p_text, bool p_is_keywords = false);
  158. TreeItem *_create_class_hierarchy(const ClassMatch &p_match);
  159. TreeItem *_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray, const String &p_matching_keyword);
  160. TreeItem *_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const MemberMatch<DocData::MethodDoc> &p_match);
  161. TreeItem *_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  162. TreeItem *_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match);
  163. TreeItem *_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ConstantDoc> &p_match);
  164. TreeItem *_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::PropertyDoc> &p_match);
  165. TreeItem *_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ThemeItemDoc> &p_match);
  166. TreeItem *_create_member_item(TreeItem *p_parent, const String &p_class_name, const String &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);
  167. public:
  168. bool work(uint64_t slot = 100000);
  169. Runner(Control *p_icon_service, Tree *p_results_tree, TreeCache *p_tree_cache, const String &p_term, int p_search_flags);
  170. };
  171. #endif // EDITOR_HELP_SEARCH_H