editor_help_search.cpp 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317
  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 "core/os/keyboard.h"
  32. #include "editor/editor_feature_profile.h"
  33. #include "editor/editor_main_screen.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. #include "editor/themes/editor_scale.h"
  38. #include "editor/themes/editor_theme_manager.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_custom_minimum_size(Size2(200, 0) * EDSCALE);
  285. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  286. search_box->set_clear_button_enabled(true);
  287. search_box->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  288. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  289. register_text_enter(search_box);
  290. hbox->add_child(search_box);
  291. case_sensitive_button = memnew(Button);
  292. case_sensitive_button->set_theme_type_variation(SceneStringName(FlatButton));
  293. case_sensitive_button->set_tooltip_text(TTR("Case Sensitive"));
  294. case_sensitive_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  295. case_sensitive_button->set_toggle_mode(true);
  296. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  297. hbox->add_child(case_sensitive_button);
  298. hierarchy_button = memnew(Button);
  299. hierarchy_button->set_theme_type_variation(SceneStringName(FlatButton));
  300. hierarchy_button->set_tooltip_text(TTR("Show Hierarchy"));
  301. hierarchy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  302. hierarchy_button->set_toggle_mode(true);
  303. hierarchy_button->set_pressed(true);
  304. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  305. hbox->add_child(hierarchy_button);
  306. filter_combo = memnew(OptionButton);
  307. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  308. filter_combo->set_stretch_ratio(0); // Fixed width.
  309. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  310. filter_combo->add_separator();
  311. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  312. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  313. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  314. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  315. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  316. filter_combo->add_item(TTR("Annotations Only"), SEARCH_ANNOTATIONS);
  317. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  318. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  319. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  320. filter_combo->connect(SceneStringName(item_selected), callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  321. hbox->add_child(filter_combo);
  322. // Create the results tree.
  323. results_tree = memnew(Tree);
  324. results_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  325. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  326. results_tree->set_columns(2);
  327. results_tree->set_column_title(0, TTR("Name"));
  328. results_tree->set_column_clip_content(0, true);
  329. results_tree->set_column_title(1, TTR("Member Type"));
  330. results_tree->set_column_expand(1, false);
  331. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  332. results_tree->set_column_clip_content(1, true);
  333. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  334. results_tree->set_hide_root(true);
  335. results_tree->set_select_mode(Tree::SELECT_ROW);
  336. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  337. results_tree->connect(SceneStringName(item_selected), callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  338. vbox->add_child(results_tree, true);
  339. }
  340. void EditorHelpSearch::TreeCache::clear() {
  341. for (const KeyValue<String, TreeItem *> &E : item_cache) {
  342. memdelete(E.value);
  343. }
  344. item_cache.clear();
  345. }
  346. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  347. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  348. if (profile.is_null()) {
  349. return false;
  350. }
  351. StringName class_name = p_class;
  352. while (class_name != StringName()) {
  353. if (!ClassDB::class_exists(class_name)) {
  354. return false;
  355. }
  356. if (profile->is_class_disabled(class_name)) {
  357. return true;
  358. }
  359. class_name = ClassDB::get_parent_class(class_name);
  360. }
  361. return false;
  362. }
  363. bool EditorHelpSearch::Runner::_fill() {
  364. bool phase_done = false;
  365. switch (phase) {
  366. case PHASE_MATCH_CLASSES_INIT:
  367. phase_done = _phase_fill_classes_init();
  368. break;
  369. case PHASE_MATCH_CLASSES:
  370. phase_done = _phase_fill_classes();
  371. break;
  372. case PHASE_CLASS_ITEMS_INIT:
  373. case PHASE_CLASS_ITEMS:
  374. phase_done = true;
  375. break;
  376. case PHASE_MEMBER_ITEMS_INIT:
  377. phase_done = _phase_fill_member_items_init();
  378. break;
  379. case PHASE_MEMBER_ITEMS:
  380. phase_done = _phase_fill_member_items();
  381. break;
  382. case PHASE_SELECT_MATCH:
  383. phase_done = _phase_select_match();
  384. break;
  385. case PHASE_MAX:
  386. return true;
  387. default:
  388. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  389. return true;
  390. }
  391. if (phase_done) {
  392. phase++;
  393. }
  394. return false;
  395. }
  396. bool EditorHelpSearch::Runner::_phase_fill_classes_init() {
  397. // Initialize fill.
  398. iterator_stack.clear();
  399. matched_classes.clear();
  400. matched_item = nullptr;
  401. match_highest_score = 0;
  402. // Initialize stack of iterators to fill, in reverse.
  403. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].back());
  404. return true;
  405. }
  406. bool EditorHelpSearch::Runner::_phase_fill_classes() {
  407. if (iterator_stack.is_empty()) {
  408. return true;
  409. }
  410. if (iterator_stack[iterator_stack.size() - 1]) {
  411. DocData::ClassDoc *class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  412. // Decrement stack.
  413. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->prev();
  414. // Drop last element of stack if empty.
  415. if (!iterator_stack[iterator_stack.size() - 1]) {
  416. iterator_stack.resize(iterator_stack.size() - 1);
  417. }
  418. if (!class_doc || class_doc->name.is_empty()) {
  419. return false;
  420. }
  421. // If class matches the flags, add it to the matched stack.
  422. const bool class_matched =
  423. (search_flags & SEARCH_CLASSES) ||
  424. ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) ||
  425. ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) ||
  426. ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) ||
  427. ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) ||
  428. ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) ||
  429. ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) ||
  430. ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) ||
  431. ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty());
  432. if (class_matched) {
  433. if (term.is_empty() || class_doc->name.containsn(term)) {
  434. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, String()));
  435. } else if (String keyword = _match_keywords(term, class_doc->keywords); !keyword.is_empty()) {
  436. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, keyword));
  437. }
  438. }
  439. // Add inheriting classes, in reverse.
  440. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  441. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].back());
  442. }
  443. return false;
  444. }
  445. // Drop last element of stack if empty.
  446. if (!iterator_stack[iterator_stack.size() - 1]) {
  447. iterator_stack.resize(iterator_stack.size() - 1);
  448. }
  449. return iterator_stack.is_empty();
  450. }
  451. bool EditorHelpSearch::Runner::_phase_fill_member_items_init() {
  452. // Prepare tree.
  453. class_items.clear();
  454. _populate_cache();
  455. return true;
  456. }
  457. 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) {
  458. const String item_meta = "class_" + p_metatype + ":" + p_class;
  459. TreeItem *item = nullptr;
  460. if (_find_or_create_item(p_parent, item_meta, item)) {
  461. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  462. item->set_text(0, p_text);
  463. item->set_metadata(0, item_meta);
  464. }
  465. item->set_collapsed(true);
  466. return item;
  467. }
  468. bool EditorHelpSearch::Runner::_phase_fill_member_items() {
  469. if (matched_classes.is_empty()) {
  470. return true;
  471. }
  472. // Pop working item from stack.
  473. Pair<DocData::ClassDoc *, String> match = matched_classes[matched_classes.size() - 1];
  474. DocData::ClassDoc *class_doc = match.first;
  475. const String &keyword = match.second;
  476. matched_classes.resize(matched_classes.size() - 1);
  477. if (class_doc) {
  478. TreeItem *item = _create_class_hierarchy(class_doc, keyword, !(search_flags & SEARCH_CLASSES));
  479. // If the class has no inheriting classes, fold its item.
  480. item->set_collapsed(!item->get_first_child());
  481. if (search_flags & SEARCH_CLASSES) {
  482. item->clear_custom_color(0);
  483. item->clear_custom_color(1);
  484. } else {
  485. item->set_custom_color(0, disabled_color);
  486. item->set_custom_color(1, disabled_color);
  487. }
  488. // Create common header if required.
  489. const bool search_all = (search_flags & SEARCH_ALL) == SEARCH_ALL;
  490. if ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) {
  491. TreeItem *parent_item = item;
  492. if (search_all) {
  493. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstructor"), TTRC("Constructors"), "constructors");
  494. }
  495. for (const DocData::MethodDoc &constructor_doc : class_doc->constructors) {
  496. _create_constructor_item(parent_item, class_doc, &constructor_doc);
  497. }
  498. }
  499. if ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) {
  500. TreeItem *parent_item = item;
  501. if (search_all) {
  502. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberMethod"), TTRC("Methods"), "methods");
  503. }
  504. for (const DocData::MethodDoc &method_doc : class_doc->methods) {
  505. _create_method_item(parent_item, class_doc, &method_doc);
  506. }
  507. }
  508. if ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) {
  509. TreeItem *parent_item = item;
  510. if (search_all) {
  511. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberOperator"), TTRC("Operators"), "operators");
  512. }
  513. for (const DocData::MethodDoc &operator_doc : class_doc->operators) {
  514. _create_operator_item(parent_item, class_doc, &operator_doc);
  515. }
  516. }
  517. if ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) {
  518. TreeItem *parent_item = item;
  519. if (search_all) {
  520. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberSignal"), TTRC("Signals"), "signals");
  521. }
  522. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  523. _create_signal_item(parent_item, class_doc, &signal_doc);
  524. }
  525. }
  526. if ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) {
  527. TreeItem *parent_item = item;
  528. if (search_all) {
  529. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstant"), TTRC("Constants"), "constants");
  530. }
  531. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  532. _create_constant_item(parent_item, class_doc, &constant_doc);
  533. }
  534. }
  535. if ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) {
  536. TreeItem *parent_item = item;
  537. if (search_all) {
  538. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberProperty"), TTRC("Properties"), "properties");
  539. }
  540. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  541. _create_property_item(parent_item, class_doc, &property_doc);
  542. }
  543. }
  544. if ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) {
  545. TreeItem *parent_item = item;
  546. if (search_all) {
  547. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberTheme"), TTRC("Theme Properties"), "theme_items");
  548. }
  549. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  550. _create_theme_property_item(parent_item, class_doc, &theme_property_doc);
  551. }
  552. }
  553. if ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty()) {
  554. TreeItem *parent_item = item;
  555. if (search_all) {
  556. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberAnnotation"), TTRC("Annotations"), "annotations");
  557. }
  558. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  559. _create_annotation_item(parent_item, class_doc, &annotation_doc);
  560. }
  561. }
  562. }
  563. return matched_classes.is_empty();
  564. }
  565. bool EditorHelpSearch::Runner::_slice() {
  566. bool phase_done = false;
  567. switch (phase) {
  568. case PHASE_MATCH_CLASSES_INIT:
  569. phase_done = _phase_match_classes_init();
  570. break;
  571. case PHASE_MATCH_CLASSES:
  572. phase_done = _phase_match_classes();
  573. break;
  574. case PHASE_CLASS_ITEMS_INIT:
  575. phase_done = _phase_class_items_init();
  576. break;
  577. case PHASE_CLASS_ITEMS:
  578. phase_done = _phase_class_items();
  579. break;
  580. case PHASE_MEMBER_ITEMS_INIT:
  581. phase_done = _phase_member_items_init();
  582. break;
  583. case PHASE_MEMBER_ITEMS:
  584. phase_done = _phase_member_items();
  585. break;
  586. case PHASE_SELECT_MATCH:
  587. phase_done = _phase_select_match();
  588. break;
  589. case PHASE_MAX:
  590. return true;
  591. default:
  592. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  593. return true;
  594. }
  595. if (phase_done) {
  596. phase++;
  597. }
  598. return false;
  599. }
  600. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  601. iterator_doc = nullptr;
  602. iterator_stack.clear();
  603. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  604. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].front());
  605. } else {
  606. iterator_doc = EditorHelp::get_doc_data()->class_list.begin();
  607. }
  608. matches.clear();
  609. matched_item = nullptr;
  610. match_highest_score = 0;
  611. if (!term.is_empty()) {
  612. terms = term.split_spaces();
  613. if (terms.is_empty()) {
  614. terms.append(term);
  615. }
  616. }
  617. return true;
  618. }
  619. bool EditorHelpSearch::Runner::_phase_match_classes() {
  620. if (!iterator_doc && iterator_stack.is_empty()) {
  621. return true;
  622. }
  623. DocData::ClassDoc *class_doc = nullptr;
  624. if (iterator_doc) {
  625. class_doc = &iterator_doc->value;
  626. } else if (!iterator_stack.is_empty() && iterator_stack[iterator_stack.size() - 1]) {
  627. class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  628. }
  629. if (class_doc && class_doc->name.is_empty()) {
  630. class_doc = nullptr;
  631. }
  632. if (class_doc && !_is_class_disabled_by_feature_profile(class_doc->name)) {
  633. ClassMatch match;
  634. match.doc = class_doc;
  635. // Match class name.
  636. if (search_flags & SEARCH_CLASSES) {
  637. match.name = _match_string(term, class_doc->name);
  638. match.keyword = _match_keywords(term, class_doc->keywords);
  639. }
  640. if (search_flags & SEARCH_CONSTRUCTORS) {
  641. _match_method_name_and_push_back(class_doc->constructors, &match.constructors);
  642. }
  643. if (search_flags & SEARCH_METHODS) {
  644. _match_method_name_and_push_back(class_doc->methods, &match.methods);
  645. }
  646. if (search_flags & SEARCH_OPERATORS) {
  647. _match_method_name_and_push_back(class_doc->operators, &match.operators);
  648. }
  649. if (search_flags & SEARCH_SIGNALS) {
  650. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  651. MemberMatch<DocData::MethodDoc> signal;
  652. signal.name = _all_terms_in_name(signal_doc.name);
  653. signal.keyword = _match_keywords_in_all_terms(signal_doc.keywords);
  654. if (signal.name || !signal.keyword.is_empty()) {
  655. signal.doc = &signal_doc;
  656. match.signals.push_back(signal);
  657. }
  658. }
  659. }
  660. if (search_flags & SEARCH_CONSTANTS) {
  661. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  662. MemberMatch<DocData::ConstantDoc> constant;
  663. constant.name = _all_terms_in_name(constant_doc.name);
  664. constant.keyword = _match_keywords_in_all_terms(constant_doc.keywords);
  665. if (constant.name || !constant.keyword.is_empty()) {
  666. constant.doc = &constant_doc;
  667. match.constants.push_back(constant);
  668. }
  669. }
  670. }
  671. if (search_flags & SEARCH_PROPERTIES) {
  672. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  673. MemberMatch<DocData::PropertyDoc> property;
  674. property.name = _all_terms_in_name(property_doc.name);
  675. property.keyword = _match_keywords_in_all_terms(property_doc.keywords);
  676. if (property.name || !property.keyword.is_empty()) {
  677. property.doc = &property_doc;
  678. match.properties.push_back(property);
  679. }
  680. }
  681. }
  682. if (search_flags & SEARCH_THEME_ITEMS) {
  683. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  684. MemberMatch<DocData::ThemeItemDoc> theme_property;
  685. theme_property.name = _all_terms_in_name(theme_property_doc.name);
  686. theme_property.keyword = _match_keywords_in_all_terms(theme_property_doc.keywords);
  687. if (theme_property.name || !theme_property.keyword.is_empty()) {
  688. theme_property.doc = &theme_property_doc;
  689. match.theme_properties.push_back(theme_property);
  690. }
  691. }
  692. }
  693. if (search_flags & SEARCH_ANNOTATIONS) {
  694. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  695. MemberMatch<DocData::MethodDoc> annotation;
  696. annotation.name = _all_terms_in_name(annotation_doc.name);
  697. annotation.keyword = _match_keywords_in_all_terms(annotation_doc.keywords);
  698. if (annotation.name || !annotation.keyword.is_empty()) {
  699. annotation.doc = &annotation_doc;
  700. match.annotations.push_back(annotation);
  701. }
  702. }
  703. }
  704. matches[class_doc->name] = match;
  705. }
  706. if (iterator_doc) {
  707. ++iterator_doc;
  708. return !iterator_doc;
  709. }
  710. if (!iterator_stack.is_empty()) {
  711. // Iterate on stack.
  712. if (iterator_stack[iterator_stack.size() - 1]) {
  713. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->next();
  714. }
  715. // Drop last element of stack.
  716. if (!iterator_stack[iterator_stack.size() - 1]) {
  717. iterator_stack.resize(iterator_stack.size() - 1);
  718. }
  719. }
  720. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  721. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].front());
  722. }
  723. return iterator_stack.is_empty();
  724. }
  725. void EditorHelpSearch::Runner::_populate_cache() {
  726. // Deselect to prevent re-selection issues.
  727. results_tree->deselect_all();
  728. root_item = results_tree->get_root();
  729. if (root_item) {
  730. LocalVector<TreeItem *> stack;
  731. // Add children of root item to stack.
  732. for (TreeItem *child = root_item->get_first_child(); child; child = child->get_next()) {
  733. stack.push_back(child);
  734. }
  735. // Traverse stack and cache items.
  736. while (!stack.is_empty()) {
  737. TreeItem *cur_item = stack[stack.size() - 1];
  738. stack.resize(stack.size() - 1);
  739. // Add to the cache.
  740. tree_cache->item_cache.insert(cur_item->get_metadata(0).operator String(), cur_item);
  741. // Add any children to the stack.
  742. for (TreeItem *child = cur_item->get_first_child(); child; child = child->get_next()) {
  743. stack.push_back(child);
  744. }
  745. // Remove from parent.
  746. cur_item->get_parent()->remove_child(cur_item);
  747. }
  748. } else {
  749. root_item = results_tree->create_item();
  750. }
  751. }
  752. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  753. iterator_match = matches.begin();
  754. _populate_cache();
  755. class_items.clear();
  756. return true;
  757. }
  758. bool EditorHelpSearch::Runner::_phase_class_items() {
  759. if (!iterator_match) {
  760. return true;
  761. }
  762. ClassMatch &match = iterator_match->value;
  763. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  764. if (match.required()) {
  765. _create_class_hierarchy(match);
  766. }
  767. } else {
  768. if (match.name || !match.keyword.is_empty()) {
  769. _create_class_item(root_item, match.doc, false, match.name ? String() : match.keyword);
  770. }
  771. }
  772. ++iterator_match;
  773. return !iterator_match;
  774. }
  775. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  776. iterator_match = matches.begin();
  777. return true;
  778. }
  779. bool EditorHelpSearch::Runner::_phase_member_items() {
  780. if (!iterator_match) {
  781. return true;
  782. }
  783. ClassMatch &match = iterator_match->value;
  784. if (!match.doc || match.doc->name.is_empty()) {
  785. ++iterator_match;
  786. return false;
  787. }
  788. // Pick appropriate parent item if showing hierarchy, otherwise pick root.
  789. TreeItem *parent_item = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  790. for (const MemberMatch<DocData::MethodDoc> &constructor_item : match.constructors) {
  791. _create_constructor_item(parent_item, match.doc, constructor_item);
  792. }
  793. for (const MemberMatch<DocData::MethodDoc> &method_item : match.methods) {
  794. _create_method_item(parent_item, match.doc, method_item);
  795. }
  796. for (const MemberMatch<DocData::MethodDoc> &operator_item : match.operators) {
  797. _create_operator_item(parent_item, match.doc, operator_item);
  798. }
  799. for (const MemberMatch<DocData::MethodDoc> &signal_item : match.signals) {
  800. _create_signal_item(parent_item, match.doc, signal_item);
  801. }
  802. for (const MemberMatch<DocData::ConstantDoc> &constant_item : match.constants) {
  803. _create_constant_item(parent_item, match.doc, constant_item);
  804. }
  805. for (const MemberMatch<DocData::PropertyDoc> &property_item : match.properties) {
  806. _create_property_item(parent_item, match.doc, property_item);
  807. }
  808. for (const MemberMatch<DocData::ThemeItemDoc> &theme_property_item : match.theme_properties) {
  809. _create_theme_property_item(parent_item, match.doc, theme_property_item);
  810. }
  811. for (const MemberMatch<DocData::MethodDoc> &annotation_item : match.annotations) {
  812. _create_annotation_item(parent_item, match.doc, annotation_item);
  813. }
  814. ++iterator_match;
  815. return !iterator_match;
  816. }
  817. bool EditorHelpSearch::Runner::_phase_select_match() {
  818. if (matched_item) {
  819. matched_item->select(0);
  820. }
  821. return true;
  822. }
  823. void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, LocalVector<MemberMatch<DocData::MethodDoc>> *r_match_methods) {
  824. // Constructors, Methods, Operators...
  825. for (int i = 0; i < p_methods.size(); i++) {
  826. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].name : p_methods[i].name.to_lower();
  827. String keywords = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].keywords : p_methods[i].keywords.to_lower();
  828. MemberMatch<DocData::MethodDoc> method;
  829. method.name = _all_terms_in_name(method_name);
  830. method.keyword = _match_keywords_in_all_terms(keywords);
  831. if (method.name || !method.keyword.is_empty() ||
  832. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  833. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  834. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  835. method.doc = &p_methods[i];
  836. r_match_methods->push_back(method);
  837. }
  838. }
  839. }
  840. bool EditorHelpSearch::Runner::_all_terms_in_name(const String &p_name) const {
  841. for (int i = 0; i < terms.size(); i++) {
  842. if (!_match_string(terms[i], p_name)) {
  843. return false;
  844. }
  845. }
  846. return true;
  847. }
  848. String EditorHelpSearch::Runner::_match_keywords_in_all_terms(const String &p_keywords) const {
  849. String matching_keyword;
  850. for (int i = 0; i < terms.size(); i++) {
  851. matching_keyword = _match_keywords(terms[i], p_keywords);
  852. if (matching_keyword.is_empty()) {
  853. return String();
  854. }
  855. }
  856. return matching_keyword;
  857. }
  858. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  859. if (search_flags & SEARCH_CASE_SENSITIVE) {
  860. return p_string.contains(p_term);
  861. } else {
  862. return p_string.containsn(p_term);
  863. }
  864. }
  865. String EditorHelpSearch::Runner::_match_keywords(const String &p_term, const String &p_keywords) const {
  866. for (const String &k : p_keywords.split(",")) {
  867. const String keyword = k.strip_edges();
  868. if (_match_string(p_term, keyword)) {
  869. return keyword;
  870. }
  871. }
  872. return String();
  873. }
  874. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text, bool p_is_keywords) {
  875. if (p_text.is_empty()) {
  876. return;
  877. }
  878. float inverse_length = 1.0f / float(p_text.length());
  879. // Favor types where search term is a substring close to the start of the type.
  880. float w = 0.5f;
  881. int pos = p_text.findn(term);
  882. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.0f, 0.9f - w);
  883. // Favor shorter items: they resemble the search term more.
  884. w = 0.1f;
  885. score *= (1 - w) + w * (term.length() * inverse_length);
  886. // Reduce the score of keywords, since they are an indirect match.
  887. if (p_is_keywords) {
  888. score *= 0.9f;
  889. }
  890. // Replace current match if term is short as we are searching in reverse.
  891. if (match_highest_score == 0 || score > match_highest_score || (score == match_highest_score && term.length() == 1)) {
  892. matched_item = p_item;
  893. match_highest_score = score;
  894. }
  895. }
  896. String EditorHelpSearch::Runner::_build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const {
  897. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  898. for (int i = 0; i < p_doc->arguments.size(); i++) {
  899. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  900. tooltip += arg.type + " " + arg.name;
  901. if (!arg.default_value.is_empty()) {
  902. tooltip += " = " + arg.default_value;
  903. }
  904. if (i < p_doc->arguments.size() - 1) {
  905. tooltip += ", ";
  906. }
  907. }
  908. tooltip += ")";
  909. tooltip += _build_keywords_tooltip(p_doc->keywords);
  910. return tooltip;
  911. }
  912. String EditorHelpSearch::Runner::_build_keywords_tooltip(const String &p_keywords) const {
  913. String tooltip;
  914. if (p_keywords.is_empty()) {
  915. return tooltip;
  916. }
  917. tooltip = "\n\n" + TTR("Keywords") + ": ";
  918. for (const String &keyword : p_keywords.split(",")) {
  919. tooltip += keyword.strip_edges().quote() + ", ";
  920. }
  921. // Remove trailing comma and space.
  922. return tooltip.left(-2);
  923. }
  924. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const DocData::ClassDoc *p_class_doc, const String &p_matching_keyword, bool p_gray) {
  925. if (p_class_doc->name.is_empty()) {
  926. return nullptr;
  927. }
  928. if (TreeItem **found = class_items.getptr(p_class_doc->name)) {
  929. return *found;
  930. }
  931. // Ensure parent nodes are created first.
  932. TreeItem *parent_item = root_item;
  933. if (!p_class_doc->inherits.is_empty()) {
  934. if (class_items.has(p_class_doc->inherits)) {
  935. parent_item = class_items[p_class_doc->inherits];
  936. } else if (const DocData::ClassDoc *found = EditorHelp::get_doc_data()->class_list.getptr(p_class_doc->inherits)) {
  937. parent_item = _create_class_hierarchy(found, String(), true);
  938. }
  939. }
  940. TreeItem *class_item = _create_class_item(parent_item, p_class_doc, p_gray, p_matching_keyword);
  941. class_items[p_class_doc->name] = class_item;
  942. return class_item;
  943. }
  944. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  945. if (p_match.doc->name.is_empty()) {
  946. return nullptr;
  947. }
  948. if (class_items.has(p_match.doc->name)) {
  949. return class_items[p_match.doc->name];
  950. }
  951. // Ensure parent nodes are created first.
  952. TreeItem *parent_item = root_item;
  953. if (!p_match.doc->inherits.is_empty()) {
  954. if (class_items.has(p_match.doc->inherits)) {
  955. parent_item = class_items[p_match.doc->inherits];
  956. } else {
  957. ClassMatch &base_match = matches[p_match.doc->inherits];
  958. if (base_match.doc) {
  959. parent_item = _create_class_hierarchy(base_match);
  960. }
  961. }
  962. }
  963. 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);
  964. class_items[p_match.doc->name] = class_item;
  965. return class_item;
  966. }
  967. bool EditorHelpSearch::Runner::_find_or_create_item(TreeItem *p_parent, const String &p_item_meta, TreeItem *&r_item) {
  968. // Attempt to find in cache.
  969. if (tree_cache->item_cache.has(p_item_meta)) {
  970. r_item = tree_cache->item_cache[p_item_meta];
  971. // Remove from cache.
  972. tree_cache->item_cache.erase(p_item_meta);
  973. // Add to tree.
  974. p_parent->add_child(r_item);
  975. return false;
  976. } else {
  977. // Otherwise create item.
  978. r_item = results_tree->create_item(p_parent);
  979. return true;
  980. }
  981. }
  982. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray, const String &p_matching_keyword) {
  983. String tooltip = DTR(p_doc->brief_description.strip_edges());
  984. tooltip += _build_keywords_tooltip(p_doc->keywords);
  985. const String item_meta = "class_name:" + p_doc->name;
  986. TreeItem *item = nullptr;
  987. if (_find_or_create_item(p_parent, item_meta, item)) {
  988. item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_doc->name));
  989. item->set_text(1, TTR("Class"));
  990. item->set_tooltip_text(0, tooltip);
  991. item->set_tooltip_text(1, tooltip);
  992. item->set_metadata(0, item_meta);
  993. if (p_doc->is_deprecated) {
  994. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  995. item->add_button(0, error_icon, 0, false, TTR("This class is marked as deprecated."));
  996. } else if (p_doc->is_experimental) {
  997. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  998. item->add_button(0, warning_icon, 0, false, TTR("This class is marked as experimental."));
  999. }
  1000. }
  1001. // Cached item might be collapsed.
  1002. item->set_collapsed(false);
  1003. if (p_gray) {
  1004. item->set_custom_color(0, disabled_color);
  1005. item->set_custom_color(1, disabled_color);
  1006. } else {
  1007. item->clear_custom_color(0);
  1008. item->clear_custom_color(1);
  1009. }
  1010. if (p_matching_keyword.is_empty()) {
  1011. item->set_text(0, p_doc->name);
  1012. } else {
  1013. item->set_text(0, p_doc->name + " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword));
  1014. }
  1015. if (!term.is_empty()) {
  1016. _match_item(item, p_doc->name);
  1017. }
  1018. for (const String &keyword : p_doc->keywords.split(",")) {
  1019. _match_item(item, keyword.strip_edges(), true);
  1020. }
  1021. return item;
  1022. }
  1023. TreeItem *EditorHelpSearch::Runner::_create_constructor_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1024. String tooltip = p_class_doc->name + "(";
  1025. String text = p_class_doc->name + "(";
  1026. for (int i = 0; i < p_match.doc->arguments.size(); i++) {
  1027. const DocData::ArgumentDoc &arg = p_match.doc->arguments[i];
  1028. tooltip += arg.type + " " + arg.name;
  1029. text += arg.type;
  1030. if (!arg.default_value.is_empty()) {
  1031. tooltip += " = " + arg.default_value;
  1032. }
  1033. if (i < p_match.doc->arguments.size() - 1) {
  1034. tooltip += ", ";
  1035. text += ", ";
  1036. }
  1037. }
  1038. tooltip += ")";
  1039. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1040. text += ")";
  1041. 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);
  1042. }
  1043. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1044. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1045. 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);
  1046. }
  1047. TreeItem *EditorHelpSearch::Runner::_create_operator_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1048. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1049. String text = p_match.doc->name;
  1050. if (!p_match.doc->arguments.is_empty()) {
  1051. text += "(" + p_match.doc->arguments[0].type + ")";
  1052. }
  1053. 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);
  1054. }
  1055. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1056. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1057. 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);
  1058. }
  1059. TreeItem *EditorHelpSearch::Runner::_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1060. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1061. // Hide the redundant leading @ symbol.
  1062. String text = p_match.doc->name.substr(1);
  1063. 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);
  1064. }
  1065. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ConstantDoc> &p_match) {
  1066. String tooltip = p_class_doc->name + "." + p_match.doc->name;
  1067. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1068. 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);
  1069. }
  1070. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::PropertyDoc> &p_match) {
  1071. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1072. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->setter + "(value) setter";
  1073. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->getter + "() getter";
  1074. 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);
  1075. }
  1076. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ThemeItemDoc> &p_match) {
  1077. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1078. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1079. 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);
  1080. }
  1081. 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) {
  1082. const String item_meta = "class_" + p_metatype + ":" + p_class_name + ":" + p_name;
  1083. TreeItem *item = nullptr;
  1084. if (_find_or_create_item(p_parent, item_meta, item)) {
  1085. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  1086. item->set_text(1, TTRGET(p_type));
  1087. item->set_tooltip_text(0, p_tooltip);
  1088. item->set_tooltip_text(1, p_tooltip);
  1089. item->set_metadata(0, item_meta);
  1090. if (p_is_deprecated) {
  1091. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  1092. item->add_button(0, error_icon, 0, false, TTR("This member is marked as deprecated."));
  1093. } else if (p_is_experimental) {
  1094. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  1095. item->add_button(0, warning_icon, 0, false, TTR("This member is marked as experimental."));
  1096. }
  1097. }
  1098. String text;
  1099. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1100. text = p_text;
  1101. } else {
  1102. text = p_class_name + "." + p_text;
  1103. }
  1104. if (!p_matching_keyword.is_empty()) {
  1105. text += " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword);
  1106. }
  1107. item->set_text(0, text);
  1108. // Don't match member items for short searches.
  1109. if (term.length() > 1 || term == "@") {
  1110. _match_item(item, p_name);
  1111. }
  1112. for (const String &keyword : p_keywords.split(",")) {
  1113. _match_item(item, keyword.strip_edges(), true);
  1114. }
  1115. return item;
  1116. }
  1117. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  1118. // Return true when the search has been completed, otherwise false.
  1119. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1120. if (term.length() > 1 || term == "@") {
  1121. while (!_slice()) {
  1122. if (OS::get_singleton()->get_ticks_usec() > until) {
  1123. return false;
  1124. }
  1125. }
  1126. } else {
  1127. while (!_fill()) {
  1128. if (OS::get_singleton()->get_ticks_usec() > until) {
  1129. return false;
  1130. }
  1131. }
  1132. }
  1133. return true;
  1134. }
  1135. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, TreeCache *p_tree_cache, const String &p_term, int p_search_flags) :
  1136. ui_service(p_icon_service),
  1137. results_tree(p_results_tree),
  1138. tree_cache(p_tree_cache),
  1139. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.to_lower() : p_term),
  1140. search_flags(p_search_flags),
  1141. disabled_color(ui_service->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor))) {
  1142. }