editor_help_search.cpp 50 KB

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