editor_help_search.cpp 26 KB

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