editor_help_search.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705
  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_node.h"
  34. #include "editor/editor_scale.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_string_names.h"
  37. void EditorHelpSearch::_update_icons() {
  38. search_box->set_right_icon(results_tree->get_editor_theme_icon(SNAME("Search")));
  39. search_box->set_clear_button_enabled(true);
  40. search_box->add_theme_icon_override("right_icon", results_tree->get_editor_theme_icon(SNAME("Search")));
  41. case_sensitive_button->set_icon(results_tree->get_editor_theme_icon(SNAME("MatchCase")));
  42. hierarchy_button->set_icon(results_tree->get_editor_theme_icon(SNAME("ClassList")));
  43. if (is_visible()) {
  44. _update_results();
  45. }
  46. }
  47. void EditorHelpSearch::_update_results() {
  48. String term = search_box->get_text();
  49. int search_flags = filter_combo->get_selected_id();
  50. if (case_sensitive_button->is_pressed()) {
  51. search_flags |= SEARCH_CASE_SENSITIVE;
  52. }
  53. if (hierarchy_button->is_pressed()) {
  54. search_flags |= SEARCH_SHOW_HIERARCHY;
  55. }
  56. search = Ref<Runner>(memnew(Runner(results_tree, results_tree, term, search_flags)));
  57. set_process(true);
  58. }
  59. void EditorHelpSearch::_search_box_gui_input(const Ref<InputEvent> &p_event) {
  60. // Redirect up and down navigational key events to the results list.
  61. Ref<InputEventKey> key = p_event;
  62. if (key.is_valid()) {
  63. switch (key->get_keycode()) {
  64. case Key::UP:
  65. case Key::DOWN:
  66. case Key::PAGEUP:
  67. case Key::PAGEDOWN: {
  68. results_tree->gui_input(key);
  69. search_box->accept_event();
  70. } break;
  71. default:
  72. break;
  73. }
  74. }
  75. }
  76. void EditorHelpSearch::_search_box_text_changed(const String &p_text) {
  77. _update_results();
  78. }
  79. void EditorHelpSearch::_filter_combo_item_selected(int p_option) {
  80. _update_results();
  81. }
  82. void EditorHelpSearch::_confirmed() {
  83. TreeItem *item = results_tree->get_selected();
  84. if (!item) {
  85. return;
  86. }
  87. // Activate the script editor and emit the signal with the documentation link to display.
  88. EditorNode::get_singleton()->set_visible_editor(EditorNode::EDITOR_SCRIPT);
  89. emit_signal(SNAME("go_to_help"), item->get_metadata(0));
  90. hide();
  91. }
  92. void EditorHelpSearch::_notification(int p_what) {
  93. switch (p_what) {
  94. case NOTIFICATION_VISIBILITY_CHANGED: {
  95. if (!is_visible()) {
  96. results_tree->call_deferred(SNAME("clear")); // Wait for the Tree's mouse event propagation.
  97. get_ok_button()->set_disabled(true);
  98. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "search_help", Rect2(get_position(), get_size()));
  99. }
  100. } break;
  101. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  102. _update_icons();
  103. } break;
  104. case NOTIFICATION_READY: {
  105. connect("confirmed", callable_mp(this, &EditorHelpSearch::_confirmed));
  106. } break;
  107. case NOTIFICATION_THEME_CHANGED: {
  108. _update_icons();
  109. } break;
  110. case NOTIFICATION_PROCESS: {
  111. // Update background search.
  112. if (search.is_valid()) {
  113. if (search->work()) {
  114. // Search done.
  115. // Only point to the match if it's a new search, and not just reopening a old one.
  116. if (!old_search) {
  117. results_tree->ensure_cursor_is_visible();
  118. } else {
  119. old_search = false;
  120. }
  121. get_ok_button()->set_disabled(!results_tree->get_selected());
  122. search = Ref<Runner>();
  123. set_process(false);
  124. }
  125. } else {
  126. set_process(false);
  127. }
  128. } break;
  129. }
  130. }
  131. void EditorHelpSearch::_bind_methods() {
  132. ADD_SIGNAL(MethodInfo("go_to_help"));
  133. }
  134. void EditorHelpSearch::popup_dialog() {
  135. popup_dialog(search_box->get_text());
  136. }
  137. void EditorHelpSearch::popup_dialog(const String &p_term) {
  138. // Restore valid window bounds or pop up at default size.
  139. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "search_help", Rect2());
  140. if (saved_size != Rect2()) {
  141. popup(saved_size);
  142. } else {
  143. popup_centered_ratio(0.5F);
  144. }
  145. if (p_term.is_empty()) {
  146. search_box->clear();
  147. } else {
  148. if (old_term == p_term) {
  149. old_search = true;
  150. } else {
  151. old_term = p_term;
  152. }
  153. search_box->set_text(p_term);
  154. search_box->select_all();
  155. }
  156. search_box->grab_focus();
  157. _update_results();
  158. }
  159. EditorHelpSearch::EditorHelpSearch() {
  160. set_hide_on_ok(false);
  161. set_clamp_to_embedder(true);
  162. set_title(TTR("Search Help"));
  163. get_ok_button()->set_disabled(true);
  164. set_ok_button_text(TTR("Open"));
  165. // Split search and results area.
  166. VBoxContainer *vbox = memnew(VBoxContainer);
  167. add_child(vbox);
  168. // Create the search box and filter controls (at the top).
  169. HBoxContainer *hbox = memnew(HBoxContainer);
  170. vbox->add_child(hbox);
  171. search_box = memnew(LineEdit);
  172. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  173. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  174. search_box->connect("gui_input", callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  175. search_box->connect("text_changed", callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  176. register_text_enter(search_box);
  177. hbox->add_child(search_box);
  178. case_sensitive_button = memnew(Button);
  179. case_sensitive_button->set_theme_type_variation("FlatButton");
  180. case_sensitive_button->set_tooltip_text(TTR("Case Sensitive"));
  181. case_sensitive_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  182. case_sensitive_button->set_toggle_mode(true);
  183. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  184. hbox->add_child(case_sensitive_button);
  185. hierarchy_button = memnew(Button);
  186. hierarchy_button->set_theme_type_variation("FlatButton");
  187. hierarchy_button->set_tooltip_text(TTR("Show Hierarchy"));
  188. hierarchy_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  189. hierarchy_button->set_toggle_mode(true);
  190. hierarchy_button->set_pressed(true);
  191. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  192. hbox->add_child(hierarchy_button);
  193. filter_combo = memnew(OptionButton);
  194. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  195. filter_combo->set_stretch_ratio(0); // Fixed width.
  196. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  197. filter_combo->add_separator();
  198. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  199. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  200. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  201. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  202. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  203. filter_combo->add_item(TTR("Annotations Only"), SEARCH_ANNOTATIONS);
  204. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  205. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  206. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  207. filter_combo->connect("item_selected", callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  208. hbox->add_child(filter_combo);
  209. // Create the results tree.
  210. results_tree = memnew(Tree);
  211. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  212. results_tree->set_columns(2);
  213. results_tree->set_column_title(0, TTR("Name"));
  214. results_tree->set_column_clip_content(0, true);
  215. results_tree->set_column_title(1, TTR("Member Type"));
  216. results_tree->set_column_expand(1, false);
  217. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  218. results_tree->set_column_clip_content(1, true);
  219. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  220. results_tree->set_hide_root(true);
  221. results_tree->set_select_mode(Tree::SELECT_ROW);
  222. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  223. results_tree->connect("item_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  224. vbox->add_child(results_tree, true);
  225. }
  226. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  227. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  228. if (profile.is_null()) {
  229. return false;
  230. }
  231. StringName class_name = p_class;
  232. while (class_name != StringName()) {
  233. if (!ClassDB::class_exists(class_name)) {
  234. return false;
  235. }
  236. if (profile->is_class_disabled(class_name)) {
  237. return true;
  238. }
  239. class_name = ClassDB::get_parent_class(class_name);
  240. }
  241. return false;
  242. }
  243. bool EditorHelpSearch::Runner::_slice() {
  244. bool phase_done = false;
  245. switch (phase) {
  246. case PHASE_MATCH_CLASSES_INIT:
  247. phase_done = _phase_match_classes_init();
  248. break;
  249. case PHASE_MATCH_CLASSES:
  250. phase_done = _phase_match_classes();
  251. break;
  252. case PHASE_CLASS_ITEMS_INIT:
  253. phase_done = _phase_class_items_init();
  254. break;
  255. case PHASE_CLASS_ITEMS:
  256. phase_done = _phase_class_items();
  257. break;
  258. case PHASE_MEMBER_ITEMS_INIT:
  259. phase_done = _phase_member_items_init();
  260. break;
  261. case PHASE_MEMBER_ITEMS:
  262. phase_done = _phase_member_items();
  263. break;
  264. case PHASE_SELECT_MATCH:
  265. phase_done = _phase_select_match();
  266. break;
  267. case PHASE_MAX:
  268. return true;
  269. default:
  270. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  271. return true;
  272. };
  273. if (phase_done) {
  274. phase++;
  275. }
  276. return false;
  277. }
  278. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  279. iterator_doc = EditorHelp::get_doc_data()->class_list.begin();
  280. matches.clear();
  281. matched_item = nullptr;
  282. match_highest_score = 0;
  283. terms = term.split_spaces();
  284. if (terms.is_empty()) {
  285. terms.append(term);
  286. }
  287. return true;
  288. }
  289. bool EditorHelpSearch::Runner::_phase_match_classes() {
  290. if (!iterator_doc) {
  291. return true;
  292. }
  293. DocData::ClassDoc &class_doc = iterator_doc->value;
  294. if (class_doc.name.is_empty()) {
  295. ++iterator_doc;
  296. return false;
  297. }
  298. if (!_is_class_disabled_by_feature_profile(class_doc.name)) {
  299. ClassMatch match;
  300. match.doc = &class_doc;
  301. // Match class name.
  302. if (search_flags & SEARCH_CLASSES) {
  303. // If the search term is empty, add any classes which are not script docs or which don't start with
  304. // a double-quotation. This will ensure that only C++ classes and explicitly named classes will
  305. // be added.
  306. match.name = (term.is_empty() && (!class_doc.is_script_doc || class_doc.name[0] != '\"')) || _match_string(term, class_doc.name);
  307. }
  308. // Match members only if the term is long enough, to avoid slow performance from building a large tree.
  309. // Make an exception for annotations, since there are not that many of them.
  310. if (term.length() > 1 || term == "@") {
  311. if (search_flags & SEARCH_CONSTRUCTORS) {
  312. _match_method_name_and_push_back(class_doc.constructors, &match.constructors);
  313. }
  314. if (search_flags & SEARCH_METHODS) {
  315. _match_method_name_and_push_back(class_doc.methods, &match.methods);
  316. }
  317. if (search_flags & SEARCH_OPERATORS) {
  318. _match_method_name_and_push_back(class_doc.operators, &match.operators);
  319. }
  320. if (search_flags & SEARCH_SIGNALS) {
  321. for (int i = 0; i < class_doc.signals.size(); i++) {
  322. if (_all_terms_in_name(class_doc.signals[i].name)) {
  323. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  324. }
  325. }
  326. }
  327. if (search_flags & SEARCH_CONSTANTS) {
  328. for (int i = 0; i < class_doc.constants.size(); i++) {
  329. if (_all_terms_in_name(class_doc.constants[i].name)) {
  330. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  331. }
  332. }
  333. }
  334. if (search_flags & SEARCH_PROPERTIES) {
  335. for (int i = 0; i < class_doc.properties.size(); i++) {
  336. if (_all_terms_in_name(class_doc.properties[i].name)) {
  337. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  338. }
  339. }
  340. }
  341. if (search_flags & SEARCH_THEME_ITEMS) {
  342. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  343. if (_all_terms_in_name(class_doc.theme_properties[i].name)) {
  344. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  345. }
  346. }
  347. }
  348. if (search_flags & SEARCH_ANNOTATIONS) {
  349. for (int i = 0; i < class_doc.annotations.size(); i++) {
  350. if (_match_string(term, class_doc.annotations[i].name)) {
  351. match.annotations.push_back(const_cast<DocData::MethodDoc *>(&class_doc.annotations[i]));
  352. }
  353. }
  354. }
  355. }
  356. matches[class_doc.name] = match;
  357. }
  358. ++iterator_doc;
  359. return !iterator_doc;
  360. }
  361. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  362. iterator_match = matches.begin();
  363. results_tree->clear();
  364. root_item = results_tree->create_item();
  365. class_items.clear();
  366. return true;
  367. }
  368. bool EditorHelpSearch::Runner::_phase_class_items() {
  369. if (!iterator_match) {
  370. return true;
  371. }
  372. ClassMatch &match = iterator_match->value;
  373. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  374. if (match.required()) {
  375. _create_class_hierarchy(match);
  376. }
  377. } else {
  378. if (match.name) {
  379. _create_class_item(root_item, match.doc, false);
  380. }
  381. }
  382. ++iterator_match;
  383. return !iterator_match;
  384. }
  385. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  386. iterator_match = matches.begin();
  387. return true;
  388. }
  389. bool EditorHelpSearch::Runner::_phase_member_items() {
  390. if (!iterator_match) {
  391. return true;
  392. }
  393. ClassMatch &match = iterator_match->value;
  394. if (!match.doc || match.doc->name.is_empty()) {
  395. ++iterator_match;
  396. return false;
  397. }
  398. TreeItem *parent_item = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  399. bool constructor_created = false;
  400. for (int i = 0; i < match.methods.size(); i++) {
  401. String text = match.methods[i]->name;
  402. if (!constructor_created) {
  403. if (match.doc->name == match.methods[i]->name) {
  404. text += " " + TTR("(constructors)");
  405. constructor_created = true;
  406. }
  407. } else {
  408. if (match.doc->name == match.methods[i]->name) {
  409. continue;
  410. }
  411. }
  412. _create_method_item(parent_item, match.doc, text, match.methods[i]);
  413. }
  414. for (int i = 0; i < match.signals.size(); i++) {
  415. _create_signal_item(parent_item, match.doc, match.signals[i]);
  416. }
  417. for (int i = 0; i < match.constants.size(); i++) {
  418. _create_constant_item(parent_item, match.doc, match.constants[i]);
  419. }
  420. for (int i = 0; i < match.properties.size(); i++) {
  421. _create_property_item(parent_item, match.doc, match.properties[i]);
  422. }
  423. for (int i = 0; i < match.theme_properties.size(); i++) {
  424. _create_theme_property_item(parent_item, match.doc, match.theme_properties[i]);
  425. }
  426. for (int i = 0; i < match.annotations.size(); i++) {
  427. // Hide the redundant leading @ symbol.
  428. _create_annotation_item(parent_item, match.doc, match.annotations[i]->name.substr(1), match.annotations[i]);
  429. }
  430. ++iterator_match;
  431. return !iterator_match;
  432. }
  433. bool EditorHelpSearch::Runner::_phase_select_match() {
  434. if (matched_item) {
  435. matched_item->select(0);
  436. }
  437. return true;
  438. }
  439. void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, Vector<DocData::MethodDoc *> *r_match_methods) {
  440. // Constructors, Methods, Operators...
  441. for (int i = 0; i < p_methods.size(); i++) {
  442. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].name : p_methods[i].name.to_lower();
  443. if (_all_terms_in_name(method_name) ||
  444. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  445. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  446. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  447. r_match_methods->push_back(const_cast<DocData::MethodDoc *>(&p_methods[i]));
  448. }
  449. }
  450. }
  451. bool EditorHelpSearch::Runner::_all_terms_in_name(String name) {
  452. for (int i = 0; i < terms.size(); i++) {
  453. if (!_match_string(terms[i], name)) {
  454. return false;
  455. }
  456. }
  457. return true;
  458. }
  459. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  460. if (search_flags & SEARCH_CASE_SENSITIVE) {
  461. return p_string.find(p_term) > -1;
  462. } else {
  463. return p_string.findn(p_term) > -1;
  464. }
  465. }
  466. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text) {
  467. float inverse_length = 1.f / float(p_text.length());
  468. // Favor types where search term is a substring close to the start of the type.
  469. float w = 0.5f;
  470. int pos = p_text.findn(term);
  471. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  472. // Favor shorter items: they resemble the search term more.
  473. w = 0.1f;
  474. score *= (1 - w) + w * (term.length() * inverse_length);
  475. if (match_highest_score == 0 || score > match_highest_score) {
  476. matched_item = p_item;
  477. match_highest_score = score;
  478. }
  479. }
  480. String EditorHelpSearch::Runner::_build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const {
  481. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  482. for (int i = 0; i < p_doc->arguments.size(); i++) {
  483. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  484. tooltip += arg.type + " " + arg.name;
  485. if (!arg.default_value.is_empty()) {
  486. tooltip += " = " + arg.default_value;
  487. }
  488. if (i < p_doc->arguments.size() - 1) {
  489. tooltip += ", ";
  490. }
  491. }
  492. tooltip += ")";
  493. return tooltip;
  494. }
  495. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  496. if (p_match.doc->name.is_empty()) {
  497. return nullptr;
  498. }
  499. if (class_items.has(p_match.doc->name)) {
  500. return class_items[p_match.doc->name];
  501. }
  502. // Ensure parent nodes are created first.
  503. TreeItem *parent_item = root_item;
  504. if (!p_match.doc->inherits.is_empty()) {
  505. if (class_items.has(p_match.doc->inherits)) {
  506. parent_item = class_items[p_match.doc->inherits];
  507. } else {
  508. ClassMatch &base_match = matches[p_match.doc->inherits];
  509. if (base_match.doc) {
  510. parent_item = _create_class_hierarchy(base_match);
  511. }
  512. }
  513. }
  514. TreeItem *class_item = _create_class_item(parent_item, p_match.doc, !p_match.name);
  515. class_items[p_match.doc->name] = class_item;
  516. return class_item;
  517. }
  518. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  519. String tooltip = DTR(p_doc->brief_description.strip_edges());
  520. TreeItem *item = results_tree->create_item(p_parent);
  521. item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_doc->name));
  522. item->set_text(0, p_doc->name);
  523. item->set_text(1, TTR("Class"));
  524. item->set_tooltip_text(0, tooltip);
  525. item->set_tooltip_text(1, tooltip);
  526. item->set_metadata(0, "class_name:" + p_doc->name);
  527. if (p_gray) {
  528. item->set_custom_color(0, disabled_color);
  529. item->set_custom_color(1, disabled_color);
  530. }
  531. if (p_doc->is_deprecated) {
  532. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon("StatusError");
  533. item->add_button(0, error_icon, 0, false, TTR("This class is marked as deprecated."));
  534. } else if (p_doc->is_experimental) {
  535. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon("NodeWarning");
  536. item->add_button(0, warning_icon, 0, false, TTR("This class is marked as experimental."));
  537. }
  538. _match_item(item, p_doc->name);
  539. return item;
  540. }
  541. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  542. String tooltip = _build_method_tooltip(p_class_doc, p_doc);
  543. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip, p_doc->is_deprecated, p_doc->is_experimental);
  544. }
  545. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  546. String tooltip = _build_method_tooltip(p_class_doc, p_doc);
  547. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip, p_doc->is_deprecated, p_doc->is_experimental);
  548. }
  549. TreeItem *EditorHelpSearch::Runner::_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  550. String tooltip = _build_method_tooltip(p_class_doc, p_doc);
  551. return _create_member_item(p_parent, p_class_doc->name, "MemberAnnotation", p_doc->name, p_text, TTRC("Annotation"), "annotation", tooltip, p_doc->is_deprecated, p_doc->is_experimental);
  552. }
  553. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  554. String tooltip = p_class_doc->name + "." + p_doc->name;
  555. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip, p_doc->is_deprecated, p_doc->is_experimental);
  556. }
  557. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  558. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  559. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  560. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  561. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip, p_doc->is_deprecated, p_doc->is_experimental);
  562. }
  563. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  564. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  565. return _create_member_item(p_parent, p_class_doc->name, "MemberTheme", p_doc->name, p_doc->name, TTRC("Theme Property"), "theme_item", tooltip, false, false);
  566. }
  567. TreeItem *EditorHelpSearch::Runner::_create_member_item(TreeItem *p_parent, const String &p_class_name, const String &p_icon, const String &p_name, const String &p_text, const String &p_type, const String &p_metatype, const String &p_tooltip, bool is_deprecated, bool is_experimental) {
  568. Ref<Texture2D> icon;
  569. String text;
  570. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  571. icon = ui_service->get_editor_theme_icon(p_icon);
  572. text = p_text;
  573. } else {
  574. icon = ui_service->get_editor_theme_icon(p_icon);
  575. text = p_class_name + "." + p_text;
  576. }
  577. TreeItem *item = results_tree->create_item(p_parent);
  578. item->set_icon(0, icon);
  579. item->set_text(0, text);
  580. item->set_text(1, TTRGET(p_type));
  581. item->set_tooltip_text(0, p_tooltip);
  582. item->set_tooltip_text(1, p_tooltip);
  583. item->set_metadata(0, "class_" + p_metatype + ":" + p_class_name + ":" + p_name);
  584. if (is_deprecated) {
  585. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon("StatusError");
  586. item->add_button(0, error_icon, 0, false, TTR("This member is marked as deprecated."));
  587. } else if (is_experimental) {
  588. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon("NodeWarning");
  589. item->add_button(0, warning_icon, 0, false, TTR("This member is marked as experimental."));
  590. }
  591. _match_item(item, p_name);
  592. return item;
  593. }
  594. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  595. // Return true when the search has been completed, otherwise false.
  596. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  597. while (!_slice()) {
  598. if (OS::get_singleton()->get_ticks_usec() > until) {
  599. return false;
  600. }
  601. }
  602. return true;
  603. }
  604. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, const String &p_term, int p_search_flags) :
  605. ui_service(p_icon_service),
  606. results_tree(p_results_tree),
  607. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.strip_edges().to_lower() : p_term.strip_edges()),
  608. search_flags(p_search_flags),
  609. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), EditorStringName(Editor))) {
  610. }