editor_help_search.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /*************************************************************************/
  2. /* editor_help_search.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_ENTER_TREE: {
  104. connect("confirmed", callable_mp(this, &EditorHelpSearch::_confirmed));
  105. _update_icons();
  106. } break;
  107. case NOTIFICATION_PROCESS: {
  108. // Update background search.
  109. if (search.is_valid()) {
  110. if (search->work()) {
  111. // Search done.
  112. // Only point to the match if it's a new search, and not just reopening a old one.
  113. if (!old_search) {
  114. results_tree->ensure_cursor_is_visible();
  115. } else {
  116. old_search = false;
  117. }
  118. get_ok_button()->set_disabled(!results_tree->get_selected());
  119. search = Ref<Runner>();
  120. set_process(false);
  121. }
  122. } else {
  123. set_process(false);
  124. }
  125. } break;
  126. }
  127. }
  128. void EditorHelpSearch::_bind_methods() {
  129. ADD_SIGNAL(MethodInfo("go_to_help"));
  130. }
  131. void EditorHelpSearch::popup_dialog() {
  132. popup_dialog(search_box->get_text());
  133. }
  134. void EditorHelpSearch::popup_dialog(const String &p_term) {
  135. // Restore valid window bounds or pop up at default size.
  136. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "search_help", Rect2());
  137. if (saved_size != Rect2()) {
  138. popup(saved_size);
  139. } else {
  140. popup_centered_ratio(0.5F);
  141. }
  142. if (p_term.is_empty()) {
  143. search_box->clear();
  144. } else {
  145. if (old_term == p_term) {
  146. old_search = true;
  147. } else {
  148. old_term = p_term;
  149. }
  150. search_box->set_text(p_term);
  151. search_box->select_all();
  152. }
  153. search_box->grab_focus();
  154. _update_results();
  155. }
  156. EditorHelpSearch::EditorHelpSearch() {
  157. set_hide_on_ok(false);
  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->connect("gui_input", callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  171. search_box->connect("text_changed", callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  172. register_text_enter(search_box);
  173. hbox->add_child(search_box);
  174. case_sensitive_button = memnew(Button);
  175. case_sensitive_button->set_flat(true);
  176. case_sensitive_button->set_tooltip_text(TTR("Case Sensitive"));
  177. case_sensitive_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  178. case_sensitive_button->set_toggle_mode(true);
  179. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  180. hbox->add_child(case_sensitive_button);
  181. hierarchy_button = memnew(Button);
  182. hierarchy_button->set_flat(true);
  183. hierarchy_button->set_tooltip_text(TTR("Show Hierarchy"));
  184. hierarchy_button->connect("pressed", callable_mp(this, &EditorHelpSearch::_update_results));
  185. hierarchy_button->set_toggle_mode(true);
  186. hierarchy_button->set_pressed(true);
  187. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  188. hbox->add_child(hierarchy_button);
  189. filter_combo = memnew(OptionButton);
  190. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  191. filter_combo->set_stretch_ratio(0); // Fixed width.
  192. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  193. filter_combo->add_separator();
  194. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  195. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  196. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  197. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  198. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  199. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  200. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  201. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  202. filter_combo->connect("item_selected", callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  203. hbox->add_child(filter_combo);
  204. // Create the results tree.
  205. results_tree = memnew(Tree);
  206. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  207. results_tree->set_columns(2);
  208. results_tree->set_column_title(0, TTR("Name"));
  209. results_tree->set_column_clip_content(0, true);
  210. results_tree->set_column_title(1, TTR("Member Type"));
  211. results_tree->set_column_expand(1, false);
  212. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  213. results_tree->set_column_clip_content(1, true);
  214. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  215. results_tree->set_hide_root(true);
  216. results_tree->set_select_mode(Tree::SELECT_ROW);
  217. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  218. results_tree->connect("item_selected", callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  219. vbox->add_child(results_tree, true);
  220. }
  221. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  222. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  223. if (profile.is_null()) {
  224. return false;
  225. }
  226. StringName class_name = p_class;
  227. while (class_name != StringName()) {
  228. if (!ClassDB::class_exists(class_name)) {
  229. return false;
  230. }
  231. if (profile->is_class_disabled(class_name)) {
  232. return true;
  233. }
  234. class_name = ClassDB::get_parent_class(class_name);
  235. }
  236. return false;
  237. }
  238. bool EditorHelpSearch::Runner::_slice() {
  239. bool phase_done = false;
  240. switch (phase) {
  241. case PHASE_MATCH_CLASSES_INIT:
  242. phase_done = _phase_match_classes_init();
  243. break;
  244. case PHASE_MATCH_CLASSES:
  245. phase_done = _phase_match_classes();
  246. break;
  247. case PHASE_CLASS_ITEMS_INIT:
  248. phase_done = _phase_class_items_init();
  249. break;
  250. case PHASE_CLASS_ITEMS:
  251. phase_done = _phase_class_items();
  252. break;
  253. case PHASE_MEMBER_ITEMS_INIT:
  254. phase_done = _phase_member_items_init();
  255. break;
  256. case PHASE_MEMBER_ITEMS:
  257. phase_done = _phase_member_items();
  258. break;
  259. case PHASE_SELECT_MATCH:
  260. phase_done = _phase_select_match();
  261. break;
  262. case PHASE_MAX:
  263. return true;
  264. default:
  265. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  266. return true;
  267. };
  268. if (phase_done) {
  269. phase++;
  270. }
  271. return false;
  272. }
  273. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  274. iterator_doc = EditorHelp::get_doc_data()->class_list.begin();
  275. matches.clear();
  276. matched_item = nullptr;
  277. match_highest_score = 0;
  278. return true;
  279. }
  280. bool EditorHelpSearch::Runner::_phase_match_classes() {
  281. DocData::ClassDoc &class_doc = iterator_doc->value;
  282. if (class_doc.name.is_empty()) {
  283. return false;
  284. }
  285. if (!_is_class_disabled_by_feature_profile(class_doc.name)) {
  286. ClassMatch match;
  287. match.doc = &class_doc;
  288. // Match class name.
  289. if (search_flags & SEARCH_CLASSES) {
  290. // If the search term is empty, add any classes which are not script docs or which don't start with
  291. // a double-quotation. This will ensure that only C++ classes and explicitly named classes will
  292. // be added.
  293. match.name = (term.is_empty() && (!class_doc.is_script_doc || class_doc.name[0] != '\"')) || _match_string(term, class_doc.name);
  294. }
  295. // Match members if the term is long enough.
  296. if (term.length() > 1) {
  297. if (search_flags & SEARCH_CONSTRUCTORS) {
  298. for (int i = 0; i < class_doc.constructors.size(); i++) {
  299. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.constructors[i].name : class_doc.constructors[i].name.to_lower();
  300. if (method_name.find(term) > -1 ||
  301. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  302. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  303. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  304. match.constructors.push_back(const_cast<DocData::MethodDoc *>(&class_doc.constructors[i]));
  305. }
  306. }
  307. }
  308. if (search_flags & SEARCH_METHODS) {
  309. for (int i = 0; i < class_doc.methods.size(); i++) {
  310. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.methods[i].name : class_doc.methods[i].name.to_lower();
  311. if (method_name.find(term) > -1 ||
  312. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  313. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  314. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  315. match.methods.push_back(const_cast<DocData::MethodDoc *>(&class_doc.methods[i]));
  316. }
  317. }
  318. }
  319. if (search_flags & SEARCH_OPERATORS) {
  320. for (int i = 0; i < class_doc.operators.size(); i++) {
  321. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? class_doc.operators[i].name : class_doc.operators[i].name.to_lower();
  322. if (method_name.find(term) > -1 ||
  323. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  324. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  325. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  326. match.operators.push_back(const_cast<DocData::MethodDoc *>(&class_doc.operators[i]));
  327. }
  328. }
  329. }
  330. if (search_flags & SEARCH_SIGNALS) {
  331. for (int i = 0; i < class_doc.signals.size(); i++) {
  332. if (_match_string(term, class_doc.signals[i].name)) {
  333. match.signals.push_back(const_cast<DocData::MethodDoc *>(&class_doc.signals[i]));
  334. }
  335. }
  336. }
  337. if (search_flags & SEARCH_CONSTANTS) {
  338. for (int i = 0; i < class_doc.constants.size(); i++) {
  339. if (_match_string(term, class_doc.constants[i].name)) {
  340. match.constants.push_back(const_cast<DocData::ConstantDoc *>(&class_doc.constants[i]));
  341. }
  342. }
  343. }
  344. if (search_flags & SEARCH_PROPERTIES) {
  345. for (int i = 0; i < class_doc.properties.size(); i++) {
  346. if (_match_string(term, class_doc.properties[i].name) || _match_string(term, class_doc.properties[i].getter) || _match_string(term, class_doc.properties[i].setter)) {
  347. match.properties.push_back(const_cast<DocData::PropertyDoc *>(&class_doc.properties[i]));
  348. }
  349. }
  350. }
  351. if (search_flags & SEARCH_THEME_ITEMS) {
  352. for (int i = 0; i < class_doc.theme_properties.size(); i++) {
  353. if (_match_string(term, class_doc.theme_properties[i].name)) {
  354. match.theme_properties.push_back(const_cast<DocData::ThemeItemDoc *>(&class_doc.theme_properties[i]));
  355. }
  356. }
  357. }
  358. matches[class_doc.name] = match;
  359. }
  360. matches[class_doc.name] = match;
  361. }
  362. ++iterator_doc;
  363. return !iterator_doc;
  364. }
  365. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  366. iterator_match = matches.begin();
  367. results_tree->clear();
  368. root_item = results_tree->create_item();
  369. class_items.clear();
  370. return true;
  371. }
  372. bool EditorHelpSearch::Runner::_phase_class_items() {
  373. if (!iterator_match) {
  374. return false;
  375. }
  376. ClassMatch &match = iterator_match->value;
  377. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  378. if (match.required()) {
  379. _create_class_hierarchy(match);
  380. }
  381. } else {
  382. if (match.name) {
  383. _create_class_item(root_item, match.doc, false);
  384. }
  385. }
  386. ++iterator_match;
  387. return !iterator_match;
  388. }
  389. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  390. iterator_match = matches.begin();
  391. return true;
  392. }
  393. bool EditorHelpSearch::Runner::_phase_member_items() {
  394. ClassMatch &match = iterator_match->value;
  395. if (!match.doc) {
  396. return false;
  397. }
  398. if (match.doc->name.is_empty()) {
  399. return false;
  400. }
  401. TreeItem *parent = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  402. bool constructor_created = false;
  403. for (int i = 0; i < match.methods.size(); i++) {
  404. String text = match.methods[i]->name;
  405. if (!constructor_created) {
  406. if (match.doc->name == match.methods[i]->name) {
  407. text += " " + TTR("(constructors)");
  408. constructor_created = true;
  409. }
  410. } else {
  411. if (match.doc->name == match.methods[i]->name) {
  412. continue;
  413. }
  414. }
  415. _create_method_item(parent, match.doc, text, match.methods[i]);
  416. }
  417. for (int i = 0; i < match.signals.size(); i++) {
  418. _create_signal_item(parent, match.doc, match.signals[i]);
  419. }
  420. for (int i = 0; i < match.constants.size(); i++) {
  421. _create_constant_item(parent, match.doc, match.constants[i]);
  422. }
  423. for (int i = 0; i < match.properties.size(); i++) {
  424. _create_property_item(parent, match.doc, match.properties[i]);
  425. }
  426. for (int i = 0; i < match.theme_properties.size(); i++) {
  427. _create_theme_property_item(parent, match.doc, match.theme_properties[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. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  439. if (search_flags & SEARCH_CASE_SENSITIVE) {
  440. return p_string.find(p_term) > -1;
  441. } else {
  442. return p_string.findn(p_term) > -1;
  443. }
  444. }
  445. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text) {
  446. float inverse_length = 1.f / float(p_text.length());
  447. // Favor types where search term is a substring close to the start of the type.
  448. float w = 0.5f;
  449. int pos = p_text.findn(term);
  450. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.f, .9f - w);
  451. // Favor shorter items: they resemble the search term more.
  452. w = 0.1f;
  453. score *= (1 - w) + w * (term.length() * inverse_length);
  454. if (match_highest_score == 0 || score > match_highest_score) {
  455. matched_item = p_item;
  456. match_highest_score = score;
  457. }
  458. }
  459. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  460. if (p_match.doc->name.is_empty()) {
  461. return nullptr;
  462. }
  463. if (class_items.has(p_match.doc->name)) {
  464. return class_items[p_match.doc->name];
  465. }
  466. // Ensure parent nodes are created first.
  467. TreeItem *parent = root_item;
  468. if (!p_match.doc->inherits.is_empty()) {
  469. if (class_items.has(p_match.doc->inherits)) {
  470. parent = class_items[p_match.doc->inherits];
  471. } else {
  472. ClassMatch &base_match = matches[p_match.doc->inherits];
  473. if (base_match.doc) {
  474. parent = _create_class_hierarchy(base_match);
  475. }
  476. }
  477. }
  478. TreeItem *class_item = _create_class_item(parent, p_match.doc, !p_match.name);
  479. class_items[p_match.doc->name] = class_item;
  480. return class_item;
  481. }
  482. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray) {
  483. Ref<Texture2D> icon = empty_icon;
  484. if (ui_service->has_theme_icon(p_doc->name, SNAME("EditorIcons"))) {
  485. icon = ui_service->get_theme_icon(p_doc->name, SNAME("EditorIcons"));
  486. } else if (ClassDB::class_exists(p_doc->name) && ClassDB::is_parent_class(p_doc->name, "Object")) {
  487. icon = ui_service->get_theme_icon(SNAME("Object"), SNAME("EditorIcons"));
  488. }
  489. String tooltip = DTR(p_doc->brief_description.strip_edges());
  490. TreeItem *item = results_tree->create_item(p_parent);
  491. item->set_icon(0, icon);
  492. item->set_text(0, p_doc->name);
  493. item->set_text(1, TTR("Class"));
  494. item->set_tooltip_text(0, tooltip);
  495. item->set_tooltip_text(1, tooltip);
  496. item->set_metadata(0, "class_name:" + p_doc->name);
  497. if (p_gray) {
  498. item->set_custom_color(0, disabled_color);
  499. item->set_custom_color(1, disabled_color);
  500. }
  501. _match_item(item, p_doc->name);
  502. return item;
  503. }
  504. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const String &p_text, const DocData::MethodDoc *p_doc) {
  505. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  506. for (int i = 0; i < p_doc->arguments.size(); i++) {
  507. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  508. tooltip += arg.type + " " + arg.name;
  509. if (!arg.default_value.is_empty()) {
  510. tooltip += " = " + arg.default_value;
  511. }
  512. if (i < p_doc->arguments.size() - 1) {
  513. tooltip += ", ";
  514. }
  515. }
  516. tooltip += ")";
  517. return _create_member_item(p_parent, p_class_doc->name, "MemberMethod", p_doc->name, p_text, TTRC("Method"), "method", tooltip);
  518. }
  519. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) {
  520. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  521. for (int i = 0; i < p_doc->arguments.size(); i++) {
  522. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  523. tooltip += arg.type + " " + arg.name;
  524. if (!arg.default_value.is_empty()) {
  525. tooltip += " = " + arg.default_value;
  526. }
  527. if (i < p_doc->arguments.size() - 1) {
  528. tooltip += ", ";
  529. }
  530. }
  531. tooltip += ")";
  532. return _create_member_item(p_parent, p_class_doc->name, "MemberSignal", p_doc->name, p_doc->name, TTRC("Signal"), "signal", tooltip);
  533. }
  534. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ConstantDoc *p_doc) {
  535. String tooltip = p_class_doc->name + "." + p_doc->name;
  536. return _create_member_item(p_parent, p_class_doc->name, "MemberConstant", p_doc->name, p_doc->name, TTRC("Constant"), "constant", tooltip);
  537. }
  538. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::PropertyDoc *p_doc) {
  539. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  540. tooltip += "\n " + p_class_doc->name + "." + p_doc->setter + "(value) setter";
  541. tooltip += "\n " + p_class_doc->name + "." + p_doc->getter + "() getter";
  542. return _create_member_item(p_parent, p_class_doc->name, "MemberProperty", p_doc->name, p_doc->name, TTRC("Property"), "property", tooltip);
  543. }
  544. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const DocData::ThemeItemDoc *p_doc) {
  545. String tooltip = p_doc->type + " " + p_class_doc->name + "." + p_doc->name;
  546. return _create_member_item(p_parent, p_class_doc->name, "MemberTheme", p_doc->name, p_doc->name, TTRC("Theme Property"), "theme_item", tooltip);
  547. }
  548. 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) {
  549. Ref<Texture2D> icon;
  550. String text;
  551. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  552. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  553. text = p_text;
  554. } else {
  555. icon = ui_service->get_theme_icon(p_icon, SNAME("EditorIcons"));
  556. text = p_class_name + "." + p_text;
  557. }
  558. TreeItem *item = results_tree->create_item(p_parent);
  559. item->set_icon(0, icon);
  560. item->set_text(0, text);
  561. item->set_text(1, TTRGET(p_type));
  562. item->set_tooltip_text(0, p_tooltip);
  563. item->set_tooltip_text(1, p_tooltip);
  564. item->set_metadata(0, "class_" + p_metatype + ":" + p_class_name + ":" + p_name);
  565. _match_item(item, p_name);
  566. return item;
  567. }
  568. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  569. // Return true when the search has been completed, otherwise false.
  570. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  571. while (!_slice()) {
  572. if (OS::get_singleton()->get_ticks_usec() > until) {
  573. return false;
  574. }
  575. }
  576. return true;
  577. }
  578. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, const String &p_term, int p_search_flags) :
  579. ui_service(p_icon_service),
  580. results_tree(p_results_tree),
  581. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.strip_edges().to_lower() : p_term.strip_edges()),
  582. search_flags(p_search_flags),
  583. empty_icon(ui_service->get_theme_icon(SNAME("ArrowRight"), SNAME("EditorIcons"))),
  584. disabled_color(ui_service->get_theme_color(SNAME("disabled_font_color"), SNAME("Editor"))) {
  585. }