editor_help_search.cpp 25 KB

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