editor_help_search.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /**************************************************************************/
  2. /* editor_help_search.cpp */
  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. #include "editor_help_search.h"
  31. #include "editor/editor_feature_profile.h"
  32. #include "editor/editor_main_screen.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "editor/themes/editor_theme_manager.h"
  38. #include "scene/gui/line_edit.h"
  39. bool EditorHelpSearch::_all_terms_in_name(const Vector<String> &p_terms, const String &p_name) const {
  40. for (int i = 0; i < p_terms.size(); i++) {
  41. if (!p_name.containsn(p_terms[i])) {
  42. return false;
  43. }
  44. }
  45. return true;
  46. }
  47. void EditorHelpSearch::_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 {
  48. // Constructors, Methods, Operators...
  49. for (int i = 0; i < p_methods.size(); i++) {
  50. String method_name = p_methods[i].name.to_lower();
  51. if (_all_terms_in_name(p_terms, method_name) ||
  52. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  53. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  54. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  55. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_methods[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_methods[i].name);
  56. }
  57. }
  58. }
  59. void EditorHelpSearch::_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 {
  60. for (int i = 0; i < p_constants.size(); i++) {
  61. String method_name = p_constants[i].name.to_lower();
  62. if (_all_terms_in_name(p_terms, method_name) ||
  63. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  64. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  65. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  66. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_constants[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_constants[i].name);
  67. }
  68. }
  69. }
  70. void EditorHelpSearch::_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 {
  71. for (int i = 0; i < p_properties.size(); i++) {
  72. String method_name = p_properties[i].name.to_lower();
  73. if (_all_terms_in_name(p_terms, method_name) ||
  74. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  75. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  76. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  77. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_properties[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_properties[i].name);
  78. }
  79. }
  80. }
  81. void EditorHelpSearch::_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 {
  82. for (int i = 0; i < p_properties.size(); i++) {
  83. String method_name = p_properties[i].name.to_lower();
  84. if (_all_terms_in_name(p_terms, method_name) ||
  85. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  86. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  87. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  88. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_properties[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_properties[i].name);
  89. }
  90. }
  91. }
  92. Dictionary EditorHelpSearch::_native_search_cb(const String &p_search_string, int p_result_limit) {
  93. Dictionary ret;
  94. const String &term = p_search_string.strip_edges().to_lower();
  95. Vector<String> terms = term.split_spaces();
  96. if (terms.is_empty()) {
  97. terms.append(term);
  98. }
  99. for (HashMap<String, DocData::ClassDoc>::Iterator iterator_doc = EditorHelp::get_doc_data()->class_list.begin(); iterator_doc; ++iterator_doc) {
  100. DocData::ClassDoc &class_doc = iterator_doc->value;
  101. if (class_doc.name.is_empty()) {
  102. continue;
  103. }
  104. if (class_doc.name.containsn(term)) {
  105. ret[vformat("class_name:%s", class_doc.name)] = class_doc.name;
  106. }
  107. if (term.length() > 1 || term == "@") {
  108. _match_method_name_and_push_back(term, terms, class_doc.constructors, TTRC("Constructor"), "method", class_doc.name, ret);
  109. _match_method_name_and_push_back(term, terms, class_doc.methods, TTRC("Method"), "method", class_doc.name, ret);
  110. _match_method_name_and_push_back(term, terms, class_doc.operators, TTRC("Operator"), "method", class_doc.name, ret);
  111. _match_method_name_and_push_back(term, terms, class_doc.signals, TTRC("Signal"), "signal", class_doc.name, ret);
  112. _match_const_name_and_push_back(term, terms, class_doc.constants, TTRC("Constant"), "constant", class_doc.name, ret);
  113. _match_property_name_and_push_back(term, terms, class_doc.properties, TTRC("Property"), "property", class_doc.name, ret);
  114. _match_theme_property_name_and_push_back(term, terms, class_doc.theme_properties, TTRC("Theme Property"), "theme_item", class_doc.name, ret);
  115. _match_method_name_and_push_back(term, terms, class_doc.annotations, TTRC("Annotation"), "annotation", class_doc.name, ret);
  116. }
  117. if (ret.size() > p_result_limit) {
  118. break;
  119. }
  120. }
  121. return ret;
  122. }
  123. void EditorHelpSearch::_native_action_cb(const String &p_item_string) {
  124. emit_signal(SNAME("go_to_help"), p_item_string);
  125. }
  126. void EditorHelpSearch::_update_results() {
  127. const String term = search_box->get_text().strip_edges();
  128. int search_flags = filter_combo->get_selected_id();
  129. // Process separately if term is not short, or is "@" for annotations.
  130. if (term.length() > 1 || term == "@") {
  131. case_sensitive_button->set_disabled(false);
  132. hierarchy_button->set_disabled(false);
  133. if (case_sensitive_button->is_pressed()) {
  134. search_flags |= SEARCH_CASE_SENSITIVE;
  135. }
  136. if (hierarchy_button->is_pressed()) {
  137. search_flags |= SEARCH_SHOW_HIERARCHY;
  138. }
  139. search.instantiate(results_tree, results_tree, &tree_cache, term, search_flags);
  140. // Clear old search flags to force rebuild on short term.
  141. old_search_flags = 0;
  142. set_process(true);
  143. } else {
  144. // Disable hierarchy and case sensitive options, not used for short searches.
  145. case_sensitive_button->set_disabled(true);
  146. hierarchy_button->set_disabled(true);
  147. // Always show hierarchy for short searches.
  148. search.instantiate(results_tree, results_tree, &tree_cache, term, search_flags | SEARCH_SHOW_HIERARCHY);
  149. old_search_flags = search_flags;
  150. set_process(true);
  151. }
  152. }
  153. void EditorHelpSearch::_search_box_gui_input(const Ref<InputEvent> &p_event) {
  154. // Redirect navigational key events to the tree.
  155. Ref<InputEventKey> key = p_event;
  156. if (key.is_valid()) {
  157. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  158. results_tree->gui_input(key);
  159. search_box->accept_event();
  160. }
  161. }
  162. }
  163. void EditorHelpSearch::_search_box_text_changed(const String &p_text) {
  164. _update_results();
  165. }
  166. void EditorHelpSearch::_filter_combo_item_selected(int p_option) {
  167. _update_results();
  168. }
  169. void EditorHelpSearch::_confirmed() {
  170. TreeItem *item = results_tree->get_selected();
  171. if (!item) {
  172. return;
  173. }
  174. // Activate the script editor and emit the signal with the documentation link to display.
  175. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  176. emit_signal(SNAME("go_to_help"), item->get_metadata(0));
  177. hide();
  178. }
  179. void EditorHelpSearch::_notification(int p_what) {
  180. switch (p_what) {
  181. case NOTIFICATION_ENTER_TREE: {
  182. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_HELP)) {
  183. DisplayServer::get_singleton()->help_set_search_callbacks(callable_mp(this, &EditorHelpSearch::_native_search_cb), callable_mp(this, &EditorHelpSearch::_native_action_cb));
  184. }
  185. } break;
  186. case NOTIFICATION_EXIT_TREE: {
  187. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_HELP)) {
  188. DisplayServer::get_singleton()->help_set_search_callbacks();
  189. }
  190. } break;
  191. case NOTIFICATION_VISIBILITY_CHANGED: {
  192. if (!is_visible()) {
  193. tree_cache.clear();
  194. results_tree->get_vscroll_bar()->set_value(0);
  195. search = Ref<Runner>();
  196. callable_mp(results_tree, &Tree::clear).call_deferred(); // Wait for the Tree's mouse event propagation.
  197. get_ok_button()->set_disabled(true);
  198. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "search_help", Rect2(get_position(), get_size()));
  199. }
  200. } break;
  201. case NOTIFICATION_READY: {
  202. connect(SceneStringName(confirmed), callable_mp(this, &EditorHelpSearch::_confirmed));
  203. } break;
  204. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  205. if (!EditorThemeManager::is_generated_theme_outdated()) {
  206. break;
  207. }
  208. [[fallthrough]];
  209. }
  210. case NOTIFICATION_THEME_CHANGED: {
  211. const int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  212. results_tree->add_theme_constant_override("icon_max_width", icon_width);
  213. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  214. search_box->add_theme_icon_override("right_icon", get_editor_theme_icon(SNAME("Search")));
  215. case_sensitive_button->set_button_icon(get_editor_theme_icon(SNAME("MatchCase")));
  216. hierarchy_button->set_button_icon(get_editor_theme_icon(SNAME("ClassList")));
  217. if (is_visible()) {
  218. _update_results();
  219. }
  220. } break;
  221. case NOTIFICATION_PROCESS: {
  222. // Update background search.
  223. if (search.is_valid()) {
  224. if (search->work()) {
  225. // Search done.
  226. // Only point to the match if it's a new search, and not just reopening a old one.
  227. if (!old_search) {
  228. results_tree->ensure_cursor_is_visible();
  229. } else {
  230. old_search = false;
  231. }
  232. get_ok_button()->set_disabled(!results_tree->get_selected());
  233. search = Ref<Runner>();
  234. set_process(false);
  235. }
  236. } else {
  237. set_process(false);
  238. }
  239. } break;
  240. }
  241. }
  242. void EditorHelpSearch::_bind_methods() {
  243. ADD_SIGNAL(MethodInfo("go_to_help"));
  244. }
  245. void EditorHelpSearch::popup_dialog() {
  246. popup_dialog(search_box->get_text());
  247. }
  248. void EditorHelpSearch::popup_dialog(const String &p_term) {
  249. // Restore valid window bounds or pop up at default size.
  250. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "search_help", Rect2());
  251. if (saved_size != Rect2()) {
  252. popup(saved_size);
  253. } else {
  254. popup_centered_ratio(0.5F);
  255. }
  256. old_search_flags = 0;
  257. if (p_term.is_empty()) {
  258. search_box->clear();
  259. } else {
  260. if (old_term == p_term) {
  261. old_search = true;
  262. } else {
  263. old_term = p_term;
  264. }
  265. search_box->set_text(p_term);
  266. search_box->select_all();
  267. }
  268. search_box->grab_focus();
  269. _update_results();
  270. }
  271. EditorHelpSearch::EditorHelpSearch() {
  272. set_hide_on_ok(false);
  273. set_clamp_to_embedder(true);
  274. set_title(TTR("Search Help"));
  275. get_ok_button()->set_disabled(true);
  276. set_ok_button_text(TTR("Open"));
  277. // Split search and results area.
  278. VBoxContainer *vbox = memnew(VBoxContainer);
  279. add_child(vbox);
  280. // Create the search box and filter controls (at the top).
  281. HBoxContainer *hbox = memnew(HBoxContainer);
  282. vbox->add_child(hbox);
  283. search_box = memnew(LineEdit);
  284. search_box->set_accessibility_name(TTRC("Search"));
  285. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  286. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  287. search_box->set_clear_button_enabled(true);
  288. search_box->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  289. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  290. register_text_enter(search_box);
  291. hbox->add_child(search_box);
  292. case_sensitive_button = memnew(Button);
  293. case_sensitive_button->set_theme_type_variation(SceneStringName(FlatButton));
  294. case_sensitive_button->set_tooltip_text(TTR("Case Sensitive"));
  295. case_sensitive_button->set_accessibility_name(TTRC("Case Sensitive"));
  296. case_sensitive_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  297. case_sensitive_button->set_toggle_mode(true);
  298. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  299. hbox->add_child(case_sensitive_button);
  300. hierarchy_button = memnew(Button);
  301. hierarchy_button->set_theme_type_variation(SceneStringName(FlatButton));
  302. hierarchy_button->set_tooltip_text(TTR("Show Hierarchy"));
  303. hierarchy_button->set_accessibility_name(TTRC("Show Hierarchy"));
  304. hierarchy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  305. hierarchy_button->set_toggle_mode(true);
  306. hierarchy_button->set_pressed(true);
  307. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  308. hbox->add_child(hierarchy_button);
  309. filter_combo = memnew(OptionButton);
  310. filter_combo->set_accessibility_name(TTRC("Filter"));
  311. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  312. filter_combo->set_stretch_ratio(0); // Fixed width.
  313. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  314. filter_combo->add_separator();
  315. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  316. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  317. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  318. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  319. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  320. filter_combo->add_item(TTR("Annotations Only"), SEARCH_ANNOTATIONS);
  321. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  322. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  323. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  324. filter_combo->connect(SceneStringName(item_selected), callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  325. hbox->add_child(filter_combo);
  326. // Create the results tree.
  327. results_tree = memnew(Tree);
  328. results_tree->set_accessibility_name(TTRC("Search Results"));
  329. results_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  330. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  331. results_tree->set_columns(2);
  332. results_tree->set_column_title(0, TTR("Name"));
  333. results_tree->set_column_clip_content(0, true);
  334. results_tree->set_column_title(1, TTR("Member Type"));
  335. results_tree->set_column_expand(1, false);
  336. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  337. results_tree->set_column_clip_content(1, true);
  338. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  339. results_tree->set_hide_root(true);
  340. results_tree->set_select_mode(Tree::SELECT_ROW);
  341. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  342. results_tree->connect(SceneStringName(item_selected), callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  343. vbox->add_child(results_tree, true);
  344. }
  345. void EditorHelpSearch::TreeCache::clear() {
  346. for (const KeyValue<String, TreeItem *> &E : item_cache) {
  347. memdelete(E.value);
  348. }
  349. item_cache.clear();
  350. }
  351. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  352. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  353. if (profile.is_null()) {
  354. return false;
  355. }
  356. StringName class_name = p_class;
  357. while (class_name != StringName()) {
  358. if (!ClassDB::class_exists(class_name)) {
  359. return false;
  360. }
  361. if (profile->is_class_disabled(class_name)) {
  362. return true;
  363. }
  364. class_name = ClassDB::get_parent_class(class_name);
  365. }
  366. return false;
  367. }
  368. bool EditorHelpSearch::Runner::_fill() {
  369. bool phase_done = false;
  370. switch (phase) {
  371. case PHASE_MATCH_CLASSES_INIT:
  372. phase_done = _phase_fill_classes_init();
  373. break;
  374. case PHASE_MATCH_CLASSES:
  375. phase_done = _phase_fill_classes();
  376. break;
  377. case PHASE_CLASS_ITEMS_INIT:
  378. case PHASE_CLASS_ITEMS:
  379. phase_done = true;
  380. break;
  381. case PHASE_MEMBER_ITEMS_INIT:
  382. phase_done = _phase_fill_member_items_init();
  383. break;
  384. case PHASE_MEMBER_ITEMS:
  385. phase_done = _phase_fill_member_items();
  386. break;
  387. case PHASE_SELECT_MATCH:
  388. phase_done = _phase_select_match();
  389. break;
  390. case PHASE_MAX:
  391. return true;
  392. default:
  393. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  394. return true;
  395. }
  396. if (phase_done) {
  397. phase++;
  398. }
  399. return false;
  400. }
  401. bool EditorHelpSearch::Runner::_phase_fill_classes_init() {
  402. // Initialize fill.
  403. iterator_stack.clear();
  404. matched_classes.clear();
  405. matched_item = nullptr;
  406. match_highest_score = 0;
  407. // Initialize stack of iterators to fill, in reverse.
  408. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].back());
  409. return true;
  410. }
  411. bool EditorHelpSearch::Runner::_phase_fill_classes() {
  412. if (iterator_stack.is_empty()) {
  413. return true;
  414. }
  415. if (iterator_stack[iterator_stack.size() - 1]) {
  416. DocData::ClassDoc *class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  417. // Decrement stack.
  418. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->prev();
  419. // Drop last element of stack if empty.
  420. if (!iterator_stack[iterator_stack.size() - 1]) {
  421. iterator_stack.resize(iterator_stack.size() - 1);
  422. }
  423. if (!class_doc || class_doc->name.is_empty()) {
  424. return false;
  425. }
  426. // If class matches the flags, add it to the matched stack.
  427. const bool class_matched =
  428. (search_flags & SEARCH_CLASSES) ||
  429. ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) ||
  430. ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) ||
  431. ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) ||
  432. ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) ||
  433. ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) ||
  434. ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) ||
  435. ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) ||
  436. ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty());
  437. if (class_matched) {
  438. if (term.is_empty() || class_doc->name.containsn(term)) {
  439. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, String()));
  440. } else if (String keyword = _match_keywords(term, class_doc->keywords); !keyword.is_empty()) {
  441. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, keyword));
  442. }
  443. }
  444. // Add inheriting classes, in reverse.
  445. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  446. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].back());
  447. }
  448. return false;
  449. }
  450. // Drop last element of stack if empty.
  451. if (!iterator_stack[iterator_stack.size() - 1]) {
  452. iterator_stack.resize(iterator_stack.size() - 1);
  453. }
  454. return iterator_stack.is_empty();
  455. }
  456. bool EditorHelpSearch::Runner::_phase_fill_member_items_init() {
  457. // Prepare tree.
  458. class_items.clear();
  459. _populate_cache();
  460. return true;
  461. }
  462. TreeItem *EditorHelpSearch::Runner::_create_category_item(TreeItem *p_parent, const String &p_class, const StringName &p_icon, const String &p_text, const String &p_metatype) {
  463. const String item_meta = "class_" + p_metatype + ":" + p_class;
  464. TreeItem *item = nullptr;
  465. if (_find_or_create_item(p_parent, item_meta, item)) {
  466. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  467. item->set_auto_translate_mode(0, AUTO_TRANSLATE_MODE_ALWAYS);
  468. item->set_text(0, p_text);
  469. item->set_metadata(0, item_meta);
  470. }
  471. item->set_collapsed(true);
  472. return item;
  473. }
  474. bool EditorHelpSearch::Runner::_phase_fill_member_items() {
  475. if (matched_classes.is_empty()) {
  476. return true;
  477. }
  478. // Pop working item from stack.
  479. Pair<DocData::ClassDoc *, String> match = matched_classes[matched_classes.size() - 1];
  480. DocData::ClassDoc *class_doc = match.first;
  481. const String &keyword = match.second;
  482. matched_classes.resize(matched_classes.size() - 1);
  483. if (class_doc) {
  484. TreeItem *item = _create_class_hierarchy(class_doc, keyword, !(search_flags & SEARCH_CLASSES));
  485. // If the class has no inheriting classes, fold its item.
  486. item->set_collapsed(!item->get_first_child());
  487. if (search_flags & SEARCH_CLASSES) {
  488. item->clear_custom_color(0);
  489. item->clear_custom_color(1);
  490. } else {
  491. item->set_custom_color(0, disabled_color);
  492. item->set_custom_color(1, disabled_color);
  493. }
  494. // Create common header if required.
  495. const bool search_all = (search_flags & SEARCH_ALL) == SEARCH_ALL;
  496. if ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) {
  497. TreeItem *parent_item = item;
  498. if (search_all) {
  499. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstructor"), TTRC("Constructors"), "constructors");
  500. }
  501. for (const DocData::MethodDoc &constructor_doc : class_doc->constructors) {
  502. _create_constructor_item(parent_item, class_doc, &constructor_doc);
  503. }
  504. }
  505. if ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) {
  506. TreeItem *parent_item = item;
  507. if (search_all) {
  508. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberMethod"), TTRC("Methods"), "methods");
  509. }
  510. for (const DocData::MethodDoc &method_doc : class_doc->methods) {
  511. _create_method_item(parent_item, class_doc, &method_doc);
  512. }
  513. }
  514. if ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) {
  515. TreeItem *parent_item = item;
  516. if (search_all) {
  517. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberOperator"), TTRC("Operators"), "operators");
  518. }
  519. for (const DocData::MethodDoc &operator_doc : class_doc->operators) {
  520. _create_operator_item(parent_item, class_doc, &operator_doc);
  521. }
  522. }
  523. if ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) {
  524. TreeItem *parent_item = item;
  525. if (search_all) {
  526. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberSignal"), TTRC("Signals"), "signals");
  527. }
  528. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  529. _create_signal_item(parent_item, class_doc, &signal_doc);
  530. }
  531. }
  532. if ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) {
  533. TreeItem *parent_item = item;
  534. if (search_all) {
  535. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstant"), TTRC("Constants"), "constants");
  536. }
  537. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  538. _create_constant_item(parent_item, class_doc, &constant_doc);
  539. }
  540. }
  541. if ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) {
  542. TreeItem *parent_item = item;
  543. if (search_all) {
  544. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberProperty"), TTRC("Properties"), "properties");
  545. }
  546. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  547. _create_property_item(parent_item, class_doc, &property_doc);
  548. }
  549. }
  550. if ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) {
  551. TreeItem *parent_item = item;
  552. if (search_all) {
  553. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberTheme"), TTRC("Theme Properties"), "theme_items");
  554. }
  555. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  556. _create_theme_property_item(parent_item, class_doc, &theme_property_doc);
  557. }
  558. }
  559. if ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty()) {
  560. TreeItem *parent_item = item;
  561. if (search_all) {
  562. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberAnnotation"), TTRC("Annotations"), "annotations");
  563. }
  564. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  565. _create_annotation_item(parent_item, class_doc, &annotation_doc);
  566. }
  567. }
  568. }
  569. return matched_classes.is_empty();
  570. }
  571. bool EditorHelpSearch::Runner::_slice() {
  572. bool phase_done = false;
  573. switch (phase) {
  574. case PHASE_MATCH_CLASSES_INIT:
  575. phase_done = _phase_match_classes_init();
  576. break;
  577. case PHASE_MATCH_CLASSES:
  578. phase_done = _phase_match_classes();
  579. break;
  580. case PHASE_CLASS_ITEMS_INIT:
  581. phase_done = _phase_class_items_init();
  582. break;
  583. case PHASE_CLASS_ITEMS:
  584. phase_done = _phase_class_items();
  585. break;
  586. case PHASE_MEMBER_ITEMS_INIT:
  587. phase_done = _phase_member_items_init();
  588. break;
  589. case PHASE_MEMBER_ITEMS:
  590. phase_done = _phase_member_items();
  591. break;
  592. case PHASE_SELECT_MATCH:
  593. phase_done = _phase_select_match();
  594. break;
  595. case PHASE_MAX:
  596. return true;
  597. default:
  598. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  599. return true;
  600. }
  601. if (phase_done) {
  602. phase++;
  603. }
  604. return false;
  605. }
  606. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  607. iterator_doc = nullptr;
  608. iterator_stack.clear();
  609. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  610. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].front());
  611. } else {
  612. iterator_doc = EditorHelp::get_doc_data()->class_list.begin();
  613. }
  614. matches.clear();
  615. matched_item = nullptr;
  616. match_highest_score = 0;
  617. if (!term.is_empty()) {
  618. terms = term.split_spaces();
  619. if (terms.is_empty()) {
  620. terms.append(term);
  621. }
  622. }
  623. return true;
  624. }
  625. bool EditorHelpSearch::Runner::_phase_match_classes() {
  626. if (!iterator_doc && iterator_stack.is_empty()) {
  627. return true;
  628. }
  629. DocData::ClassDoc *class_doc = nullptr;
  630. if (iterator_doc) {
  631. class_doc = &iterator_doc->value;
  632. } else if (!iterator_stack.is_empty() && iterator_stack[iterator_stack.size() - 1]) {
  633. class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  634. }
  635. if (class_doc && class_doc->name.is_empty()) {
  636. class_doc = nullptr;
  637. }
  638. if (class_doc && !_is_class_disabled_by_feature_profile(class_doc->name)) {
  639. ClassMatch match;
  640. match.doc = class_doc;
  641. // Match class name.
  642. if (search_flags & SEARCH_CLASSES) {
  643. match.name = _match_string(term, class_doc->name);
  644. match.keyword = _match_keywords(term, class_doc->keywords);
  645. }
  646. if (search_flags & SEARCH_CONSTRUCTORS) {
  647. _match_method_name_and_push_back(class_doc->constructors, &match.constructors);
  648. }
  649. if (search_flags & SEARCH_METHODS) {
  650. _match_method_name_and_push_back(class_doc->methods, &match.methods);
  651. }
  652. if (search_flags & SEARCH_OPERATORS) {
  653. _match_method_name_and_push_back(class_doc->operators, &match.operators);
  654. }
  655. if (search_flags & SEARCH_SIGNALS) {
  656. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  657. MemberMatch<DocData::MethodDoc> signal;
  658. signal.name = _all_terms_in_name(signal_doc.name);
  659. signal.keyword = _match_keywords_in_all_terms(signal_doc.keywords);
  660. if (signal.name || !signal.keyword.is_empty()) {
  661. signal.doc = &signal_doc;
  662. match.signals.push_back(signal);
  663. }
  664. }
  665. }
  666. if (search_flags & SEARCH_CONSTANTS) {
  667. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  668. MemberMatch<DocData::ConstantDoc> constant;
  669. constant.name = _all_terms_in_name(constant_doc.name);
  670. constant.keyword = _match_keywords_in_all_terms(constant_doc.keywords);
  671. if (constant.name || !constant.keyword.is_empty()) {
  672. constant.doc = &constant_doc;
  673. match.constants.push_back(constant);
  674. }
  675. }
  676. }
  677. if (search_flags & SEARCH_PROPERTIES) {
  678. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  679. MemberMatch<DocData::PropertyDoc> property;
  680. property.name = _all_terms_in_name(property_doc.name);
  681. property.keyword = _match_keywords_in_all_terms(property_doc.keywords);
  682. if (property.name || !property.keyword.is_empty()) {
  683. property.doc = &property_doc;
  684. match.properties.push_back(property);
  685. }
  686. }
  687. }
  688. if (search_flags & SEARCH_THEME_ITEMS) {
  689. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  690. MemberMatch<DocData::ThemeItemDoc> theme_property;
  691. theme_property.name = _all_terms_in_name(theme_property_doc.name);
  692. theme_property.keyword = _match_keywords_in_all_terms(theme_property_doc.keywords);
  693. if (theme_property.name || !theme_property.keyword.is_empty()) {
  694. theme_property.doc = &theme_property_doc;
  695. match.theme_properties.push_back(theme_property);
  696. }
  697. }
  698. }
  699. if (search_flags & SEARCH_ANNOTATIONS) {
  700. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  701. MemberMatch<DocData::MethodDoc> annotation;
  702. annotation.name = _all_terms_in_name(annotation_doc.name);
  703. annotation.keyword = _match_keywords_in_all_terms(annotation_doc.keywords);
  704. if (annotation.name || !annotation.keyword.is_empty()) {
  705. annotation.doc = &annotation_doc;
  706. match.annotations.push_back(annotation);
  707. }
  708. }
  709. }
  710. matches[class_doc->name] = match;
  711. }
  712. if (iterator_doc) {
  713. ++iterator_doc;
  714. return !iterator_doc;
  715. }
  716. if (!iterator_stack.is_empty()) {
  717. // Iterate on stack.
  718. if (iterator_stack[iterator_stack.size() - 1]) {
  719. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->next();
  720. }
  721. // Drop last element of stack.
  722. if (!iterator_stack[iterator_stack.size() - 1]) {
  723. iterator_stack.resize(iterator_stack.size() - 1);
  724. }
  725. }
  726. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  727. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].front());
  728. }
  729. return iterator_stack.is_empty();
  730. }
  731. void EditorHelpSearch::Runner::_populate_cache() {
  732. // Deselect to prevent re-selection issues.
  733. results_tree->deselect_all();
  734. root_item = results_tree->get_root();
  735. if (root_item) {
  736. LocalVector<TreeItem *> stack;
  737. // Add children of root item to stack.
  738. for (TreeItem *child = root_item->get_first_child(); child; child = child->get_next()) {
  739. stack.push_back(child);
  740. }
  741. // Traverse stack and cache items.
  742. while (!stack.is_empty()) {
  743. TreeItem *cur_item = stack[stack.size() - 1];
  744. stack.resize(stack.size() - 1);
  745. // Add to the cache.
  746. tree_cache->item_cache.insert(cur_item->get_metadata(0).operator String(), cur_item);
  747. // Add any children to the stack.
  748. for (TreeItem *child = cur_item->get_first_child(); child; child = child->get_next()) {
  749. stack.push_back(child);
  750. }
  751. // Remove from parent.
  752. cur_item->get_parent()->remove_child(cur_item);
  753. }
  754. } else {
  755. root_item = results_tree->create_item();
  756. }
  757. }
  758. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  759. iterator_match = matches.begin();
  760. _populate_cache();
  761. class_items.clear();
  762. return true;
  763. }
  764. bool EditorHelpSearch::Runner::_phase_class_items() {
  765. if (!iterator_match) {
  766. return true;
  767. }
  768. ClassMatch &match = iterator_match->value;
  769. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  770. if (match.required()) {
  771. _create_class_hierarchy(match);
  772. }
  773. } else {
  774. if (match.name || !match.keyword.is_empty()) {
  775. _create_class_item(root_item, match.doc, false, match.name ? String() : match.keyword);
  776. }
  777. }
  778. ++iterator_match;
  779. return !iterator_match;
  780. }
  781. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  782. iterator_match = matches.begin();
  783. return true;
  784. }
  785. bool EditorHelpSearch::Runner::_phase_member_items() {
  786. if (!iterator_match) {
  787. return true;
  788. }
  789. ClassMatch &match = iterator_match->value;
  790. if (!match.doc || match.doc->name.is_empty()) {
  791. ++iterator_match;
  792. return false;
  793. }
  794. // Pick appropriate parent item if showing hierarchy, otherwise pick root.
  795. TreeItem *parent_item = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  796. for (const MemberMatch<DocData::MethodDoc> &constructor_item : match.constructors) {
  797. _create_constructor_item(parent_item, match.doc, constructor_item);
  798. }
  799. for (const MemberMatch<DocData::MethodDoc> &method_item : match.methods) {
  800. _create_method_item(parent_item, match.doc, method_item);
  801. }
  802. for (const MemberMatch<DocData::MethodDoc> &operator_item : match.operators) {
  803. _create_operator_item(parent_item, match.doc, operator_item);
  804. }
  805. for (const MemberMatch<DocData::MethodDoc> &signal_item : match.signals) {
  806. _create_signal_item(parent_item, match.doc, signal_item);
  807. }
  808. for (const MemberMatch<DocData::ConstantDoc> &constant_item : match.constants) {
  809. _create_constant_item(parent_item, match.doc, constant_item);
  810. }
  811. for (const MemberMatch<DocData::PropertyDoc> &property_item : match.properties) {
  812. _create_property_item(parent_item, match.doc, property_item);
  813. }
  814. for (const MemberMatch<DocData::ThemeItemDoc> &theme_property_item : match.theme_properties) {
  815. _create_theme_property_item(parent_item, match.doc, theme_property_item);
  816. }
  817. for (const MemberMatch<DocData::MethodDoc> &annotation_item : match.annotations) {
  818. _create_annotation_item(parent_item, match.doc, annotation_item);
  819. }
  820. ++iterator_match;
  821. return !iterator_match;
  822. }
  823. bool EditorHelpSearch::Runner::_phase_select_match() {
  824. if (matched_item) {
  825. matched_item->select(0);
  826. }
  827. return true;
  828. }
  829. void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, LocalVector<MemberMatch<DocData::MethodDoc>> *r_match_methods) {
  830. // Constructors, Methods, Operators...
  831. for (int i = 0; i < p_methods.size(); i++) {
  832. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].name : p_methods[i].name.to_lower();
  833. String keywords = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].keywords : p_methods[i].keywords.to_lower();
  834. MemberMatch<DocData::MethodDoc> method;
  835. method.name = _all_terms_in_name(method_name);
  836. method.keyword = _match_keywords_in_all_terms(keywords);
  837. if (method.name || !method.keyword.is_empty() ||
  838. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  839. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  840. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  841. method.doc = &p_methods[i];
  842. r_match_methods->push_back(method);
  843. }
  844. }
  845. }
  846. bool EditorHelpSearch::Runner::_all_terms_in_name(const String &p_name) const {
  847. for (int i = 0; i < terms.size(); i++) {
  848. if (!_match_string(terms[i], p_name)) {
  849. return false;
  850. }
  851. }
  852. return true;
  853. }
  854. String EditorHelpSearch::Runner::_match_keywords_in_all_terms(const String &p_keywords) const {
  855. String matching_keyword;
  856. for (int i = 0; i < terms.size(); i++) {
  857. matching_keyword = _match_keywords(terms[i], p_keywords);
  858. if (matching_keyword.is_empty()) {
  859. return String();
  860. }
  861. }
  862. return matching_keyword;
  863. }
  864. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  865. if (search_flags & SEARCH_CASE_SENSITIVE) {
  866. return p_string.contains(p_term);
  867. } else {
  868. return p_string.containsn(p_term);
  869. }
  870. }
  871. String EditorHelpSearch::Runner::_match_keywords(const String &p_term, const String &p_keywords) const {
  872. for (const String &k : p_keywords.split(",")) {
  873. const String keyword = k.strip_edges();
  874. if (_match_string(p_term, keyword)) {
  875. return keyword;
  876. }
  877. }
  878. return String();
  879. }
  880. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text, bool p_is_keywords) {
  881. if (p_text.is_empty()) {
  882. return;
  883. }
  884. float inverse_length = 1.0f / float(p_text.length());
  885. // Favor types where search term is a substring close to the start of the type.
  886. float w = 0.5f;
  887. int pos = p_text.findn(term);
  888. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.0f, 0.9f - w);
  889. // Favor shorter items: they resemble the search term more.
  890. w = 0.1f;
  891. score *= (1 - w) + w * (term.length() * inverse_length);
  892. // Reduce the score of keywords, since they are an indirect match.
  893. if (p_is_keywords) {
  894. score *= 0.9f;
  895. }
  896. // Replace current match if term is short as we are searching in reverse.
  897. if (match_highest_score == 0 || score > match_highest_score || (score == match_highest_score && term.length() == 1)) {
  898. matched_item = p_item;
  899. match_highest_score = score;
  900. }
  901. }
  902. String EditorHelpSearch::Runner::_build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const {
  903. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  904. for (int i = 0; i < p_doc->arguments.size(); i++) {
  905. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  906. tooltip += arg.type + " " + arg.name;
  907. if (!arg.default_value.is_empty()) {
  908. tooltip += " = " + arg.default_value;
  909. }
  910. if (i < p_doc->arguments.size() - 1) {
  911. tooltip += ", ";
  912. }
  913. }
  914. tooltip += ")";
  915. tooltip += _build_keywords_tooltip(p_doc->keywords);
  916. return tooltip;
  917. }
  918. String EditorHelpSearch::Runner::_build_keywords_tooltip(const String &p_keywords) const {
  919. String tooltip;
  920. if (p_keywords.is_empty()) {
  921. return tooltip;
  922. }
  923. tooltip = "\n\n" + TTR("Keywords") + ": ";
  924. for (const String &keyword : p_keywords.split(",")) {
  925. tooltip += keyword.strip_edges().quote() + ", ";
  926. }
  927. // Remove trailing comma and space.
  928. return tooltip.left(-2);
  929. }
  930. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const DocData::ClassDoc *p_class_doc, const String &p_matching_keyword, bool p_gray) {
  931. if (p_class_doc->name.is_empty()) {
  932. return nullptr;
  933. }
  934. if (TreeItem **found = class_items.getptr(p_class_doc->name)) {
  935. return *found;
  936. }
  937. // Ensure parent nodes are created first.
  938. TreeItem *parent_item = root_item;
  939. if (!p_class_doc->inherits.is_empty()) {
  940. if (class_items.has(p_class_doc->inherits)) {
  941. parent_item = class_items[p_class_doc->inherits];
  942. } else if (const DocData::ClassDoc *found = EditorHelp::get_doc_data()->class_list.getptr(p_class_doc->inherits)) {
  943. parent_item = _create_class_hierarchy(found, String(), true);
  944. }
  945. }
  946. TreeItem *class_item = _create_class_item(parent_item, p_class_doc, p_gray, p_matching_keyword);
  947. class_items[p_class_doc->name] = class_item;
  948. return class_item;
  949. }
  950. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  951. if (p_match.doc->name.is_empty()) {
  952. return nullptr;
  953. }
  954. if (class_items.has(p_match.doc->name)) {
  955. return class_items[p_match.doc->name];
  956. }
  957. // Ensure parent nodes are created first.
  958. TreeItem *parent_item = root_item;
  959. if (!p_match.doc->inherits.is_empty()) {
  960. if (class_items.has(p_match.doc->inherits)) {
  961. parent_item = class_items[p_match.doc->inherits];
  962. } else {
  963. ClassMatch &base_match = matches[p_match.doc->inherits];
  964. if (base_match.doc) {
  965. parent_item = _create_class_hierarchy(base_match);
  966. }
  967. }
  968. }
  969. TreeItem *class_item = _create_class_item(parent_item, p_match.doc, !p_match.name && p_match.keyword.is_empty(), p_match.name ? String() : p_match.keyword);
  970. class_items[p_match.doc->name] = class_item;
  971. return class_item;
  972. }
  973. bool EditorHelpSearch::Runner::_find_or_create_item(TreeItem *p_parent, const String &p_item_meta, TreeItem *&r_item) {
  974. // Attempt to find in cache.
  975. if (tree_cache->item_cache.has(p_item_meta)) {
  976. r_item = tree_cache->item_cache[p_item_meta];
  977. // Remove from cache.
  978. tree_cache->item_cache.erase(p_item_meta);
  979. // Add to tree.
  980. p_parent->add_child(r_item);
  981. return false;
  982. } else {
  983. // Otherwise create item.
  984. r_item = results_tree->create_item(p_parent);
  985. return true;
  986. }
  987. }
  988. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray, const String &p_matching_keyword) {
  989. String tooltip = DTR(p_doc->brief_description.strip_edges());
  990. tooltip += _build_keywords_tooltip(p_doc->keywords);
  991. const String item_meta = "class_name:" + p_doc->name;
  992. TreeItem *item = nullptr;
  993. if (_find_or_create_item(p_parent, item_meta, item)) {
  994. item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_doc->name));
  995. item->set_text(1, TTR("Class"));
  996. item->set_tooltip_text(0, tooltip);
  997. item->set_tooltip_text(1, tooltip);
  998. item->set_metadata(0, item_meta);
  999. if (p_doc->is_deprecated) {
  1000. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  1001. item->add_button(0, error_icon, 0, false, TTR("This class is marked as deprecated."));
  1002. } else if (p_doc->is_experimental) {
  1003. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  1004. item->add_button(0, warning_icon, 0, false, TTR("This class is marked as experimental."));
  1005. }
  1006. }
  1007. // Cached item might be collapsed.
  1008. item->set_collapsed(false);
  1009. if (p_gray) {
  1010. item->set_custom_color(0, disabled_color);
  1011. item->set_custom_color(1, disabled_color);
  1012. } else {
  1013. item->clear_custom_color(0);
  1014. item->clear_custom_color(1);
  1015. }
  1016. if (p_matching_keyword.is_empty()) {
  1017. item->set_text(0, p_doc->name);
  1018. } else {
  1019. item->set_text(0, p_doc->name + " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword));
  1020. }
  1021. if (!term.is_empty()) {
  1022. _match_item(item, p_doc->name);
  1023. }
  1024. for (const String &keyword : p_doc->keywords.split(",")) {
  1025. _match_item(item, keyword.strip_edges(), true);
  1026. }
  1027. return item;
  1028. }
  1029. TreeItem *EditorHelpSearch::Runner::_create_constructor_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1030. String tooltip = p_class_doc->name + "(";
  1031. String text = p_class_doc->name + "(";
  1032. for (int i = 0; i < p_match.doc->arguments.size(); i++) {
  1033. const DocData::ArgumentDoc &arg = p_match.doc->arguments[i];
  1034. tooltip += arg.type + " " + arg.name;
  1035. text += arg.type;
  1036. if (!arg.default_value.is_empty()) {
  1037. tooltip += " = " + arg.default_value;
  1038. }
  1039. if (i < p_match.doc->arguments.size() - 1) {
  1040. tooltip += ", ";
  1041. text += ", ";
  1042. }
  1043. }
  1044. tooltip += ")";
  1045. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1046. text += ")";
  1047. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberConstructor"), p_match.doc->name, text, TTRC("Constructor"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1048. }
  1049. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1050. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1051. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberMethod"), p_match.doc->name, p_match.doc->name, TTRC("Method"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1052. }
  1053. TreeItem *EditorHelpSearch::Runner::_create_operator_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1054. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1055. String text = p_match.doc->name;
  1056. if (!p_match.doc->arguments.is_empty()) {
  1057. text += "(" + p_match.doc->arguments[0].type + ")";
  1058. }
  1059. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberOperator"), p_match.doc->name, text, TTRC("Operator"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1060. }
  1061. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1062. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1063. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberSignal"), p_match.doc->name, p_match.doc->name, TTRC("Signal"), "signal", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1064. }
  1065. TreeItem *EditorHelpSearch::Runner::_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1066. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1067. // Hide the redundant leading @ symbol.
  1068. String text = p_match.doc->name.substr(1);
  1069. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberAnnotation"), p_match.doc->name, text, TTRC("Annotation"), "annotation", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1070. }
  1071. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ConstantDoc> &p_match) {
  1072. String tooltip = p_class_doc->name + "." + p_match.doc->name;
  1073. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1074. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberConstant"), p_match.doc->name, p_match.doc->name, TTRC("Constant"), "constant", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1075. }
  1076. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::PropertyDoc> &p_match) {
  1077. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1078. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->setter + "(value) setter";
  1079. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->getter + "() getter";
  1080. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberProperty"), p_match.doc->name, p_match.doc->name, TTRC("Property"), "property", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1081. }
  1082. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ThemeItemDoc> &p_match) {
  1083. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1084. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1085. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberTheme"), p_match.doc->name, p_match.doc->name, TTRC("Theme Property"), "theme_item", p_match.doc->keywords, tooltip, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1086. }
  1087. TreeItem *EditorHelpSearch::Runner::_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) {
  1088. const String item_meta = "class_" + p_metatype + ":" + p_class_name + ":" + p_name;
  1089. TreeItem *item = nullptr;
  1090. if (_find_or_create_item(p_parent, item_meta, item)) {
  1091. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  1092. item->set_text(1, TTRGET(p_type));
  1093. item->set_tooltip_text(0, p_tooltip);
  1094. item->set_tooltip_text(1, p_tooltip);
  1095. item->set_metadata(0, item_meta);
  1096. if (p_is_deprecated) {
  1097. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  1098. item->add_button(0, error_icon, 0, false, TTR("This member is marked as deprecated."));
  1099. } else if (p_is_experimental) {
  1100. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  1101. item->add_button(0, warning_icon, 0, false, TTR("This member is marked as experimental."));
  1102. }
  1103. }
  1104. String text;
  1105. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1106. text = p_text;
  1107. } else {
  1108. text = p_class_name + "." + p_text;
  1109. }
  1110. if (!p_matching_keyword.is_empty()) {
  1111. text += " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword);
  1112. }
  1113. item->set_text(0, text);
  1114. // Don't match member items for short searches.
  1115. if (term.length() > 1 || term == "@") {
  1116. _match_item(item, p_name);
  1117. }
  1118. for (const String &keyword : p_keywords.split(",")) {
  1119. _match_item(item, keyword.strip_edges(), true);
  1120. }
  1121. return item;
  1122. }
  1123. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  1124. // Return true when the search has been completed, otherwise false.
  1125. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1126. if (term.length() > 1 || term == "@") {
  1127. while (!_slice()) {
  1128. if (OS::get_singleton()->get_ticks_usec() > until) {
  1129. return false;
  1130. }
  1131. }
  1132. } else {
  1133. while (!_fill()) {
  1134. if (OS::get_singleton()->get_ticks_usec() > until) {
  1135. return false;
  1136. }
  1137. }
  1138. }
  1139. return true;
  1140. }
  1141. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, TreeCache *p_tree_cache, const String &p_term, int p_search_flags) :
  1142. ui_service(p_icon_service),
  1143. results_tree(p_results_tree),
  1144. tree_cache(p_tree_cache),
  1145. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.to_lower() : p_term),
  1146. search_flags(p_search_flags),
  1147. disabled_color(ui_service->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor))) {
  1148. }