editor_help_search.cpp 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 "editor/editor_feature_profile.h"
  32. #include "editor/editor_main_screen.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/editor_string_names.h"
  36. #include "editor/themes/editor_scale.h"
  37. #include "editor/themes/editor_theme_manager.h"
  38. bool EditorHelpSearch::_all_terms_in_name(const Vector<String> &p_terms, const String &p_name) const {
  39. for (int i = 0; i < p_terms.size(); i++) {
  40. if (!p_name.containsn(p_terms[i])) {
  41. return false;
  42. }
  43. }
  44. return true;
  45. }
  46. void EditorHelpSearch::_match_method_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::MethodDoc> &p_methods, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const {
  47. // Constructors, Methods, Operators...
  48. for (int i = 0; i < p_methods.size(); i++) {
  49. String method_name = p_methods[i].name.to_lower();
  50. if (_all_terms_in_name(p_terms, method_name) ||
  51. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  52. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  53. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  54. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_methods[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_methods[i].name);
  55. }
  56. }
  57. }
  58. void EditorHelpSearch::_match_const_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::ConstantDoc> &p_constants, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const {
  59. for (int i = 0; i < p_constants.size(); i++) {
  60. String method_name = p_constants[i].name.to_lower();
  61. if (_all_terms_in_name(p_terms, method_name) ||
  62. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  63. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  64. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  65. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_constants[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_constants[i].name);
  66. }
  67. }
  68. }
  69. void EditorHelpSearch::_match_property_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::PropertyDoc> &p_properties, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const {
  70. for (int i = 0; i < p_properties.size(); i++) {
  71. String method_name = p_properties[i].name.to_lower();
  72. if (_all_terms_in_name(p_terms, method_name) ||
  73. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  74. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  75. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  76. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_properties[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_properties[i].name);
  77. }
  78. }
  79. }
  80. void EditorHelpSearch::_match_theme_property_name_and_push_back(const String &p_term, const Vector<String> &p_terms, Vector<DocData::ThemeItemDoc> &p_properties, const String &p_type, const String &p_metatype, const String &p_class_name, Dictionary &r_result) const {
  81. for (int i = 0; i < p_properties.size(); i++) {
  82. String method_name = p_properties[i].name.to_lower();
  83. if (_all_terms_in_name(p_terms, method_name) ||
  84. (p_term.begins_with(".") && method_name.begins_with(p_term.substr(1))) ||
  85. (p_term.ends_with("(") && method_name.ends_with(p_term.left(p_term.length() - 1).strip_edges())) ||
  86. (p_term.begins_with(".") && p_term.ends_with("(") && method_name == p_term.substr(1, p_term.length() - 2).strip_edges())) {
  87. r_result[vformat("class_%s:%s:%s", p_metatype, p_class_name, p_properties[i].name)] = vformat("%s > %s: %s", p_class_name, p_type, p_properties[i].name);
  88. }
  89. }
  90. }
  91. Dictionary EditorHelpSearch::_native_search_cb(const String &p_search_string, int p_result_limit) {
  92. Dictionary ret;
  93. const String &term = p_search_string.strip_edges().to_lower();
  94. Vector<String> terms = term.split_spaces();
  95. if (terms.is_empty()) {
  96. terms.append(term);
  97. }
  98. for (HashMap<String, DocData::ClassDoc>::Iterator iterator_doc = EditorHelp::get_doc_data()->class_list.begin(); iterator_doc; ++iterator_doc) {
  99. DocData::ClassDoc &class_doc = iterator_doc->value;
  100. if (class_doc.name.is_empty()) {
  101. continue;
  102. }
  103. if (class_doc.name.containsn(term)) {
  104. ret[vformat("class_name:%s", class_doc.name)] = class_doc.name;
  105. }
  106. if (term.length() > 1 || term == "@") {
  107. _match_method_name_and_push_back(term, terms, class_doc.constructors, TTRC("Constructor"), "method", class_doc.name, ret);
  108. _match_method_name_and_push_back(term, terms, class_doc.methods, TTRC("Method"), "method", class_doc.name, ret);
  109. _match_method_name_and_push_back(term, terms, class_doc.operators, TTRC("Operator"), "method", class_doc.name, ret);
  110. _match_method_name_and_push_back(term, terms, class_doc.signals, TTRC("Signal"), "signal", class_doc.name, ret);
  111. _match_const_name_and_push_back(term, terms, class_doc.constants, TTRC("Constant"), "constant", class_doc.name, ret);
  112. _match_property_name_and_push_back(term, terms, class_doc.properties, TTRC("Property"), "property", class_doc.name, ret);
  113. _match_theme_property_name_and_push_back(term, terms, class_doc.theme_properties, TTRC("Theme Property"), "theme_item", class_doc.name, ret);
  114. _match_method_name_and_push_back(term, terms, class_doc.annotations, TTRC("Annotation"), "annotation", class_doc.name, ret);
  115. }
  116. if (ret.size() > p_result_limit) {
  117. break;
  118. }
  119. }
  120. return ret;
  121. }
  122. void EditorHelpSearch::_native_action_cb(const String &p_item_string) {
  123. emit_signal(SNAME("go_to_help"), p_item_string);
  124. }
  125. void EditorHelpSearch::_update_results() {
  126. const String term = search_box->get_text().strip_edges();
  127. int search_flags = filter_combo->get_selected_id();
  128. // Process separately if term is not short, or is "@" for annotations.
  129. if (term.length() > 1 || term == "@") {
  130. case_sensitive_button->set_disabled(false);
  131. hierarchy_button->set_disabled(false);
  132. if (case_sensitive_button->is_pressed()) {
  133. search_flags |= SEARCH_CASE_SENSITIVE;
  134. }
  135. if (hierarchy_button->is_pressed()) {
  136. search_flags |= SEARCH_SHOW_HIERARCHY;
  137. }
  138. search.instantiate(results_tree, results_tree, &tree_cache, term, search_flags);
  139. // Clear old search flags to force rebuild on short term.
  140. old_search_flags = 0;
  141. set_process(true);
  142. } else {
  143. // Disable hierarchy and case sensitive options, not used for short searches.
  144. case_sensitive_button->set_disabled(true);
  145. hierarchy_button->set_disabled(true);
  146. // Always show hierarchy for short searches.
  147. search.instantiate(results_tree, results_tree, &tree_cache, term, search_flags | SEARCH_SHOW_HIERARCHY);
  148. old_search_flags = search_flags;
  149. set_process(true);
  150. }
  151. }
  152. void EditorHelpSearch::_search_box_gui_input(const Ref<InputEvent> &p_event) {
  153. // Redirect navigational key events to the tree.
  154. Ref<InputEventKey> key = p_event;
  155. if (key.is_valid()) {
  156. if (key->is_action("ui_up", true) || key->is_action("ui_down", true) || key->is_action("ui_page_up") || key->is_action("ui_page_down")) {
  157. results_tree->gui_input(key);
  158. search_box->accept_event();
  159. }
  160. }
  161. }
  162. void EditorHelpSearch::_search_box_text_changed(const String &p_text) {
  163. _update_results();
  164. }
  165. void EditorHelpSearch::_filter_combo_item_selected(int p_option) {
  166. _update_results();
  167. }
  168. void EditorHelpSearch::_confirmed() {
  169. TreeItem *item = results_tree->get_selected();
  170. if (!item) {
  171. return;
  172. }
  173. // Activate the script editor and emit the signal with the documentation link to display.
  174. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  175. emit_signal(SNAME("go_to_help"), item->get_metadata(0));
  176. hide();
  177. }
  178. void EditorHelpSearch::_notification(int p_what) {
  179. switch (p_what) {
  180. case NOTIFICATION_ENTER_TREE: {
  181. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_HELP)) {
  182. DisplayServer::get_singleton()->help_set_search_callbacks(callable_mp(this, &EditorHelpSearch::_native_search_cb), callable_mp(this, &EditorHelpSearch::_native_action_cb));
  183. }
  184. } break;
  185. case NOTIFICATION_EXIT_TREE: {
  186. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_NATIVE_HELP)) {
  187. DisplayServer::get_singleton()->help_set_search_callbacks();
  188. }
  189. } break;
  190. case NOTIFICATION_VISIBILITY_CHANGED: {
  191. if (!is_visible()) {
  192. tree_cache.clear();
  193. results_tree->get_vscroll_bar()->set_value(0);
  194. search = Ref<Runner>();
  195. callable_mp(results_tree, &Tree::clear).call_deferred(); // Wait for the Tree's mouse event propagation.
  196. get_ok_button()->set_disabled(true);
  197. EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "search_help", Rect2(get_position(), get_size()));
  198. }
  199. } break;
  200. case NOTIFICATION_READY: {
  201. connect(SceneStringName(confirmed), callable_mp(this, &EditorHelpSearch::_confirmed));
  202. } break;
  203. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  204. if (!EditorThemeManager::is_generated_theme_outdated()) {
  205. break;
  206. }
  207. [[fallthrough]];
  208. }
  209. case NOTIFICATION_THEME_CHANGED: {
  210. const int icon_width = get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
  211. results_tree->add_theme_constant_override("icon_max_width", icon_width);
  212. search_box->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  213. search_box->add_theme_icon_override("right_icon", get_editor_theme_icon(SNAME("Search")));
  214. case_sensitive_button->set_button_icon(get_editor_theme_icon(SNAME("MatchCase")));
  215. hierarchy_button->set_button_icon(get_editor_theme_icon(SNAME("ClassList")));
  216. if (is_visible()) {
  217. _update_results();
  218. }
  219. } break;
  220. case NOTIFICATION_PROCESS: {
  221. // Update background search.
  222. if (search.is_valid()) {
  223. if (search->work()) {
  224. // Search done.
  225. // Only point to the match if it's a new search, and not just reopening a old one.
  226. if (!old_search) {
  227. results_tree->ensure_cursor_is_visible();
  228. } else {
  229. old_search = false;
  230. }
  231. get_ok_button()->set_disabled(!results_tree->get_selected());
  232. search = Ref<Runner>();
  233. set_process(false);
  234. }
  235. } else {
  236. set_process(false);
  237. }
  238. } break;
  239. }
  240. }
  241. void EditorHelpSearch::_bind_methods() {
  242. ADD_SIGNAL(MethodInfo("go_to_help"));
  243. }
  244. void EditorHelpSearch::popup_dialog() {
  245. popup_dialog(search_box->get_text());
  246. }
  247. void EditorHelpSearch::popup_dialog(const String &p_term) {
  248. // Restore valid window bounds or pop up at default size.
  249. Rect2 saved_size = EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "search_help", Rect2());
  250. if (saved_size != Rect2()) {
  251. popup(saved_size);
  252. } else {
  253. popup_centered_ratio(0.5F);
  254. }
  255. old_search_flags = 0;
  256. if (p_term.is_empty()) {
  257. search_box->clear();
  258. } else {
  259. if (old_term == p_term) {
  260. old_search = true;
  261. } else {
  262. old_term = p_term;
  263. }
  264. search_box->set_text(p_term);
  265. search_box->select_all();
  266. }
  267. search_box->grab_focus();
  268. _update_results();
  269. }
  270. EditorHelpSearch::EditorHelpSearch() {
  271. set_hide_on_ok(false);
  272. set_clamp_to_embedder(true);
  273. set_title(TTR("Search Help"));
  274. get_ok_button()->set_disabled(true);
  275. set_ok_button_text(TTR("Open"));
  276. // Split search and results area.
  277. VBoxContainer *vbox = memnew(VBoxContainer);
  278. add_child(vbox);
  279. // Create the search box and filter controls (at the top).
  280. HBoxContainer *hbox = memnew(HBoxContainer);
  281. vbox->add_child(hbox);
  282. search_box = memnew(LineEdit);
  283. search_box->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  284. search_box->set_h_size_flags(Control::SIZE_EXPAND_FILL);
  285. search_box->set_clear_button_enabled(true);
  286. search_box->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelpSearch::_search_box_gui_input));
  287. search_box->connect(SceneStringName(text_changed), callable_mp(this, &EditorHelpSearch::_search_box_text_changed));
  288. register_text_enter(search_box);
  289. hbox->add_child(search_box);
  290. case_sensitive_button = memnew(Button);
  291. case_sensitive_button->set_theme_type_variation(SceneStringName(FlatButton));
  292. case_sensitive_button->set_tooltip_text(TTR("Case Sensitive"));
  293. case_sensitive_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  294. case_sensitive_button->set_toggle_mode(true);
  295. case_sensitive_button->set_focus_mode(Control::FOCUS_NONE);
  296. hbox->add_child(case_sensitive_button);
  297. hierarchy_button = memnew(Button);
  298. hierarchy_button->set_theme_type_variation(SceneStringName(FlatButton));
  299. hierarchy_button->set_tooltip_text(TTR("Show Hierarchy"));
  300. hierarchy_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelpSearch::_update_results));
  301. hierarchy_button->set_toggle_mode(true);
  302. hierarchy_button->set_pressed(true);
  303. hierarchy_button->set_focus_mode(Control::FOCUS_NONE);
  304. hbox->add_child(hierarchy_button);
  305. filter_combo = memnew(OptionButton);
  306. filter_combo->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  307. filter_combo->set_stretch_ratio(0); // Fixed width.
  308. filter_combo->add_item(TTR("Display All"), SEARCH_ALL);
  309. filter_combo->add_separator();
  310. filter_combo->add_item(TTR("Classes Only"), SEARCH_CLASSES);
  311. filter_combo->add_item(TTR("Constructors Only"), SEARCH_CONSTRUCTORS);
  312. filter_combo->add_item(TTR("Methods Only"), SEARCH_METHODS);
  313. filter_combo->add_item(TTR("Operators Only"), SEARCH_OPERATORS);
  314. filter_combo->add_item(TTR("Signals Only"), SEARCH_SIGNALS);
  315. filter_combo->add_item(TTR("Annotations Only"), SEARCH_ANNOTATIONS);
  316. filter_combo->add_item(TTR("Constants Only"), SEARCH_CONSTANTS);
  317. filter_combo->add_item(TTR("Properties Only"), SEARCH_PROPERTIES);
  318. filter_combo->add_item(TTR("Theme Properties Only"), SEARCH_THEME_ITEMS);
  319. filter_combo->connect(SceneStringName(item_selected), callable_mp(this, &EditorHelpSearch::_filter_combo_item_selected));
  320. hbox->add_child(filter_combo);
  321. // Create the results tree.
  322. results_tree = memnew(Tree);
  323. results_tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  324. results_tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  325. results_tree->set_columns(2);
  326. results_tree->set_column_title(0, TTR("Name"));
  327. results_tree->set_column_clip_content(0, true);
  328. results_tree->set_column_title(1, TTR("Member Type"));
  329. results_tree->set_column_expand(1, false);
  330. results_tree->set_column_custom_minimum_width(1, 150 * EDSCALE);
  331. results_tree->set_column_clip_content(1, true);
  332. results_tree->set_custom_minimum_size(Size2(0, 100) * EDSCALE);
  333. results_tree->set_hide_root(true);
  334. results_tree->set_select_mode(Tree::SELECT_ROW);
  335. results_tree->connect("item_activated", callable_mp(this, &EditorHelpSearch::_confirmed));
  336. results_tree->connect(SceneStringName(item_selected), callable_mp((BaseButton *)get_ok_button(), &BaseButton::set_disabled).bind(false));
  337. vbox->add_child(results_tree, true);
  338. }
  339. void EditorHelpSearch::TreeCache::clear() {
  340. for (const KeyValue<String, TreeItem *> &E : item_cache) {
  341. memdelete(E.value);
  342. }
  343. item_cache.clear();
  344. }
  345. bool EditorHelpSearch::Runner::_is_class_disabled_by_feature_profile(const StringName &p_class) {
  346. Ref<EditorFeatureProfile> profile = EditorFeatureProfileManager::get_singleton()->get_current_profile();
  347. if (profile.is_null()) {
  348. return false;
  349. }
  350. StringName class_name = p_class;
  351. while (class_name != StringName()) {
  352. if (!ClassDB::class_exists(class_name)) {
  353. return false;
  354. }
  355. if (profile->is_class_disabled(class_name)) {
  356. return true;
  357. }
  358. class_name = ClassDB::get_parent_class(class_name);
  359. }
  360. return false;
  361. }
  362. bool EditorHelpSearch::Runner::_fill() {
  363. bool phase_done = false;
  364. switch (phase) {
  365. case PHASE_MATCH_CLASSES_INIT:
  366. phase_done = _phase_fill_classes_init();
  367. break;
  368. case PHASE_MATCH_CLASSES:
  369. phase_done = _phase_fill_classes();
  370. break;
  371. case PHASE_CLASS_ITEMS_INIT:
  372. case PHASE_CLASS_ITEMS:
  373. phase_done = true;
  374. break;
  375. case PHASE_MEMBER_ITEMS_INIT:
  376. phase_done = _phase_fill_member_items_init();
  377. break;
  378. case PHASE_MEMBER_ITEMS:
  379. phase_done = _phase_fill_member_items();
  380. break;
  381. case PHASE_SELECT_MATCH:
  382. phase_done = _phase_select_match();
  383. break;
  384. case PHASE_MAX:
  385. return true;
  386. default:
  387. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  388. return true;
  389. }
  390. if (phase_done) {
  391. phase++;
  392. }
  393. return false;
  394. }
  395. bool EditorHelpSearch::Runner::_phase_fill_classes_init() {
  396. // Initialize fill.
  397. iterator_stack.clear();
  398. matched_classes.clear();
  399. matched_item = nullptr;
  400. match_highest_score = 0;
  401. // Initialize stack of iterators to fill, in reverse.
  402. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].back());
  403. return true;
  404. }
  405. bool EditorHelpSearch::Runner::_phase_fill_classes() {
  406. if (iterator_stack.is_empty()) {
  407. return true;
  408. }
  409. if (iterator_stack[iterator_stack.size() - 1]) {
  410. DocData::ClassDoc *class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  411. // Decrement stack.
  412. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->prev();
  413. // Drop last element of stack if empty.
  414. if (!iterator_stack[iterator_stack.size() - 1]) {
  415. iterator_stack.resize(iterator_stack.size() - 1);
  416. }
  417. if (!class_doc || class_doc->name.is_empty()) {
  418. return false;
  419. }
  420. // If class matches the flags, add it to the matched stack.
  421. const bool class_matched =
  422. (search_flags & SEARCH_CLASSES) ||
  423. ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) ||
  424. ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) ||
  425. ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) ||
  426. ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) ||
  427. ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) ||
  428. ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) ||
  429. ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) ||
  430. ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty());
  431. if (class_matched) {
  432. if (term.is_empty() || class_doc->name.containsn(term)) {
  433. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, String()));
  434. } else if (String keyword = _match_keywords(term, class_doc->keywords); !keyword.is_empty()) {
  435. matched_classes.push_back(Pair<DocData::ClassDoc *, String>(class_doc, keyword));
  436. }
  437. }
  438. // Add inheriting classes, in reverse.
  439. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  440. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].back());
  441. }
  442. return false;
  443. }
  444. // Drop last element of stack if empty.
  445. if (!iterator_stack[iterator_stack.size() - 1]) {
  446. iterator_stack.resize(iterator_stack.size() - 1);
  447. }
  448. return iterator_stack.is_empty();
  449. }
  450. bool EditorHelpSearch::Runner::_phase_fill_member_items_init() {
  451. // Prepare tree.
  452. class_items.clear();
  453. _populate_cache();
  454. return true;
  455. }
  456. TreeItem *EditorHelpSearch::Runner::_create_category_item(TreeItem *p_parent, const String &p_class, const StringName &p_icon, const String &p_text, const String &p_metatype) {
  457. const String item_meta = "class_" + p_metatype + ":" + p_class;
  458. TreeItem *item = nullptr;
  459. if (_find_or_create_item(p_parent, item_meta, item)) {
  460. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  461. item->set_text(0, p_text);
  462. item->set_metadata(0, item_meta);
  463. }
  464. item->set_collapsed(true);
  465. return item;
  466. }
  467. bool EditorHelpSearch::Runner::_phase_fill_member_items() {
  468. if (matched_classes.is_empty()) {
  469. return true;
  470. }
  471. // Pop working item from stack.
  472. Pair<DocData::ClassDoc *, String> match = matched_classes[matched_classes.size() - 1];
  473. DocData::ClassDoc *class_doc = match.first;
  474. const String &keyword = match.second;
  475. matched_classes.resize(matched_classes.size() - 1);
  476. if (class_doc) {
  477. TreeItem *item = _create_class_hierarchy(class_doc, keyword, !(search_flags & SEARCH_CLASSES));
  478. // If the class has no inheriting classes, fold its item.
  479. item->set_collapsed(!item->get_first_child());
  480. if (search_flags & SEARCH_CLASSES) {
  481. item->clear_custom_color(0);
  482. item->clear_custom_color(1);
  483. } else {
  484. item->set_custom_color(0, disabled_color);
  485. item->set_custom_color(1, disabled_color);
  486. }
  487. // Create common header if required.
  488. const bool search_all = (search_flags & SEARCH_ALL) == SEARCH_ALL;
  489. if ((search_flags & SEARCH_CONSTRUCTORS) && !class_doc->constructors.is_empty()) {
  490. TreeItem *parent_item = item;
  491. if (search_all) {
  492. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstructor"), TTRC("Constructors"), "constructors");
  493. }
  494. for (const DocData::MethodDoc &constructor_doc : class_doc->constructors) {
  495. _create_constructor_item(parent_item, class_doc, &constructor_doc);
  496. }
  497. }
  498. if ((search_flags & SEARCH_METHODS) && !class_doc->methods.is_empty()) {
  499. TreeItem *parent_item = item;
  500. if (search_all) {
  501. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberMethod"), TTRC("Methods"), "methods");
  502. }
  503. for (const DocData::MethodDoc &method_doc : class_doc->methods) {
  504. _create_method_item(parent_item, class_doc, &method_doc);
  505. }
  506. }
  507. if ((search_flags & SEARCH_OPERATORS) && !class_doc->operators.is_empty()) {
  508. TreeItem *parent_item = item;
  509. if (search_all) {
  510. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberOperator"), TTRC("Operators"), "operators");
  511. }
  512. for (const DocData::MethodDoc &operator_doc : class_doc->operators) {
  513. _create_operator_item(parent_item, class_doc, &operator_doc);
  514. }
  515. }
  516. if ((search_flags & SEARCH_SIGNALS) && !class_doc->signals.is_empty()) {
  517. TreeItem *parent_item = item;
  518. if (search_all) {
  519. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberSignal"), TTRC("Signals"), "signals");
  520. }
  521. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  522. _create_signal_item(parent_item, class_doc, &signal_doc);
  523. }
  524. }
  525. if ((search_flags & SEARCH_CONSTANTS) && !class_doc->constants.is_empty()) {
  526. TreeItem *parent_item = item;
  527. if (search_all) {
  528. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberConstant"), TTRC("Constants"), "constants");
  529. }
  530. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  531. _create_constant_item(parent_item, class_doc, &constant_doc);
  532. }
  533. }
  534. if ((search_flags & SEARCH_PROPERTIES) && !class_doc->properties.is_empty()) {
  535. TreeItem *parent_item = item;
  536. if (search_all) {
  537. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberProperty"), TTRC("Properties"), "properties");
  538. }
  539. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  540. _create_property_item(parent_item, class_doc, &property_doc);
  541. }
  542. }
  543. if ((search_flags & SEARCH_THEME_ITEMS) && !class_doc->theme_properties.is_empty()) {
  544. TreeItem *parent_item = item;
  545. if (search_all) {
  546. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberTheme"), TTRC("Theme Properties"), "theme_items");
  547. }
  548. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  549. _create_theme_property_item(parent_item, class_doc, &theme_property_doc);
  550. }
  551. }
  552. if ((search_flags & SEARCH_ANNOTATIONS) && !class_doc->annotations.is_empty()) {
  553. TreeItem *parent_item = item;
  554. if (search_all) {
  555. parent_item = _create_category_item(parent_item, class_doc->name, SNAME("MemberAnnotation"), TTRC("Annotations"), "annotations");
  556. }
  557. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  558. _create_annotation_item(parent_item, class_doc, &annotation_doc);
  559. }
  560. }
  561. }
  562. return matched_classes.is_empty();
  563. }
  564. bool EditorHelpSearch::Runner::_slice() {
  565. bool phase_done = false;
  566. switch (phase) {
  567. case PHASE_MATCH_CLASSES_INIT:
  568. phase_done = _phase_match_classes_init();
  569. break;
  570. case PHASE_MATCH_CLASSES:
  571. phase_done = _phase_match_classes();
  572. break;
  573. case PHASE_CLASS_ITEMS_INIT:
  574. phase_done = _phase_class_items_init();
  575. break;
  576. case PHASE_CLASS_ITEMS:
  577. phase_done = _phase_class_items();
  578. break;
  579. case PHASE_MEMBER_ITEMS_INIT:
  580. phase_done = _phase_member_items_init();
  581. break;
  582. case PHASE_MEMBER_ITEMS:
  583. phase_done = _phase_member_items();
  584. break;
  585. case PHASE_SELECT_MATCH:
  586. phase_done = _phase_select_match();
  587. break;
  588. case PHASE_MAX:
  589. return true;
  590. default:
  591. WARN_PRINT("Invalid or unhandled phase in EditorHelpSearch::Runner, aborting search.");
  592. return true;
  593. }
  594. if (phase_done) {
  595. phase++;
  596. }
  597. return false;
  598. }
  599. bool EditorHelpSearch::Runner::_phase_match_classes_init() {
  600. iterator_doc = nullptr;
  601. iterator_stack.clear();
  602. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  603. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[""].front());
  604. } else {
  605. iterator_doc = EditorHelp::get_doc_data()->class_list.begin();
  606. }
  607. matches.clear();
  608. matched_item = nullptr;
  609. match_highest_score = 0;
  610. if (!term.is_empty()) {
  611. terms = term.split_spaces();
  612. if (terms.is_empty()) {
  613. terms.append(term);
  614. }
  615. }
  616. return true;
  617. }
  618. bool EditorHelpSearch::Runner::_phase_match_classes() {
  619. if (!iterator_doc && iterator_stack.is_empty()) {
  620. return true;
  621. }
  622. DocData::ClassDoc *class_doc = nullptr;
  623. if (iterator_doc) {
  624. class_doc = &iterator_doc->value;
  625. } else if (!iterator_stack.is_empty() && iterator_stack[iterator_stack.size() - 1]) {
  626. class_doc = EditorHelp::get_doc_data()->class_list.getptr(iterator_stack[iterator_stack.size() - 1]->get());
  627. }
  628. if (class_doc && class_doc->name.is_empty()) {
  629. class_doc = nullptr;
  630. }
  631. if (class_doc && !_is_class_disabled_by_feature_profile(class_doc->name)) {
  632. ClassMatch match;
  633. match.doc = class_doc;
  634. // Match class name.
  635. if (search_flags & SEARCH_CLASSES) {
  636. match.name = _match_string(term, class_doc->name);
  637. match.keyword = _match_keywords(term, class_doc->keywords);
  638. }
  639. if (search_flags & SEARCH_CONSTRUCTORS) {
  640. _match_method_name_and_push_back(class_doc->constructors, &match.constructors);
  641. }
  642. if (search_flags & SEARCH_METHODS) {
  643. _match_method_name_and_push_back(class_doc->methods, &match.methods);
  644. }
  645. if (search_flags & SEARCH_OPERATORS) {
  646. _match_method_name_and_push_back(class_doc->operators, &match.operators);
  647. }
  648. if (search_flags & SEARCH_SIGNALS) {
  649. for (const DocData::MethodDoc &signal_doc : class_doc->signals) {
  650. MemberMatch<DocData::MethodDoc> signal;
  651. signal.name = _all_terms_in_name(signal_doc.name);
  652. signal.keyword = _match_keywords_in_all_terms(signal_doc.keywords);
  653. if (signal.name || !signal.keyword.is_empty()) {
  654. signal.doc = &signal_doc;
  655. match.signals.push_back(signal);
  656. }
  657. }
  658. }
  659. if (search_flags & SEARCH_CONSTANTS) {
  660. for (const DocData::ConstantDoc &constant_doc : class_doc->constants) {
  661. MemberMatch<DocData::ConstantDoc> constant;
  662. constant.name = _all_terms_in_name(constant_doc.name);
  663. constant.keyword = _match_keywords_in_all_terms(constant_doc.keywords);
  664. if (constant.name || !constant.keyword.is_empty()) {
  665. constant.doc = &constant_doc;
  666. match.constants.push_back(constant);
  667. }
  668. }
  669. }
  670. if (search_flags & SEARCH_PROPERTIES) {
  671. for (const DocData::PropertyDoc &property_doc : class_doc->properties) {
  672. MemberMatch<DocData::PropertyDoc> property;
  673. property.name = _all_terms_in_name(property_doc.name);
  674. property.keyword = _match_keywords_in_all_terms(property_doc.keywords);
  675. if (property.name || !property.keyword.is_empty()) {
  676. property.doc = &property_doc;
  677. match.properties.push_back(property);
  678. }
  679. }
  680. }
  681. if (search_flags & SEARCH_THEME_ITEMS) {
  682. for (const DocData::ThemeItemDoc &theme_property_doc : class_doc->theme_properties) {
  683. MemberMatch<DocData::ThemeItemDoc> theme_property;
  684. theme_property.name = _all_terms_in_name(theme_property_doc.name);
  685. theme_property.keyword = _match_keywords_in_all_terms(theme_property_doc.keywords);
  686. if (theme_property.name || !theme_property.keyword.is_empty()) {
  687. theme_property.doc = &theme_property_doc;
  688. match.theme_properties.push_back(theme_property);
  689. }
  690. }
  691. }
  692. if (search_flags & SEARCH_ANNOTATIONS) {
  693. for (const DocData::MethodDoc &annotation_doc : class_doc->annotations) {
  694. MemberMatch<DocData::MethodDoc> annotation;
  695. annotation.name = _all_terms_in_name(annotation_doc.name);
  696. annotation.keyword = _match_keywords_in_all_terms(annotation_doc.keywords);
  697. if (annotation.name || !annotation.keyword.is_empty()) {
  698. annotation.doc = &annotation_doc;
  699. match.annotations.push_back(annotation);
  700. }
  701. }
  702. }
  703. matches[class_doc->name] = match;
  704. }
  705. if (iterator_doc) {
  706. ++iterator_doc;
  707. return !iterator_doc;
  708. }
  709. if (!iterator_stack.is_empty()) {
  710. // Iterate on stack.
  711. if (iterator_stack[iterator_stack.size() - 1]) {
  712. iterator_stack[iterator_stack.size() - 1] = iterator_stack[iterator_stack.size() - 1]->next();
  713. }
  714. // Drop last element of stack.
  715. if (!iterator_stack[iterator_stack.size() - 1]) {
  716. iterator_stack.resize(iterator_stack.size() - 1);
  717. }
  718. }
  719. if (class_doc && EditorHelp::get_doc_data()->inheriting.has(class_doc->name)) {
  720. iterator_stack.push_back(EditorHelp::get_doc_data()->inheriting[class_doc->name].front());
  721. }
  722. return iterator_stack.is_empty();
  723. }
  724. void EditorHelpSearch::Runner::_populate_cache() {
  725. // Deselect to prevent re-selection issues.
  726. results_tree->deselect_all();
  727. root_item = results_tree->get_root();
  728. if (root_item) {
  729. LocalVector<TreeItem *> stack;
  730. // Add children of root item to stack.
  731. for (TreeItem *child = root_item->get_first_child(); child; child = child->get_next()) {
  732. stack.push_back(child);
  733. }
  734. // Traverse stack and cache items.
  735. while (!stack.is_empty()) {
  736. TreeItem *cur_item = stack[stack.size() - 1];
  737. stack.resize(stack.size() - 1);
  738. // Add to the cache.
  739. tree_cache->item_cache.insert(cur_item->get_metadata(0).operator String(), cur_item);
  740. // Add any children to the stack.
  741. for (TreeItem *child = cur_item->get_first_child(); child; child = child->get_next()) {
  742. stack.push_back(child);
  743. }
  744. // Remove from parent.
  745. cur_item->get_parent()->remove_child(cur_item);
  746. }
  747. } else {
  748. root_item = results_tree->create_item();
  749. }
  750. }
  751. bool EditorHelpSearch::Runner::_phase_class_items_init() {
  752. iterator_match = matches.begin();
  753. _populate_cache();
  754. class_items.clear();
  755. return true;
  756. }
  757. bool EditorHelpSearch::Runner::_phase_class_items() {
  758. if (!iterator_match) {
  759. return true;
  760. }
  761. ClassMatch &match = iterator_match->value;
  762. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  763. if (match.required()) {
  764. _create_class_hierarchy(match);
  765. }
  766. } else {
  767. if (match.name || !match.keyword.is_empty()) {
  768. _create_class_item(root_item, match.doc, false, match.name ? String() : match.keyword);
  769. }
  770. }
  771. ++iterator_match;
  772. return !iterator_match;
  773. }
  774. bool EditorHelpSearch::Runner::_phase_member_items_init() {
  775. iterator_match = matches.begin();
  776. return true;
  777. }
  778. bool EditorHelpSearch::Runner::_phase_member_items() {
  779. if (!iterator_match) {
  780. return true;
  781. }
  782. ClassMatch &match = iterator_match->value;
  783. if (!match.doc || match.doc->name.is_empty()) {
  784. ++iterator_match;
  785. return false;
  786. }
  787. // Pick appropriate parent item if showing hierarchy, otherwise pick root.
  788. TreeItem *parent_item = (search_flags & SEARCH_SHOW_HIERARCHY) ? class_items[match.doc->name] : root_item;
  789. for (const MemberMatch<DocData::MethodDoc> &constructor_item : match.constructors) {
  790. _create_constructor_item(parent_item, match.doc, constructor_item);
  791. }
  792. for (const MemberMatch<DocData::MethodDoc> &method_item : match.methods) {
  793. _create_method_item(parent_item, match.doc, method_item);
  794. }
  795. for (const MemberMatch<DocData::MethodDoc> &operator_item : match.operators) {
  796. _create_operator_item(parent_item, match.doc, operator_item);
  797. }
  798. for (const MemberMatch<DocData::MethodDoc> &signal_item : match.signals) {
  799. _create_signal_item(parent_item, match.doc, signal_item);
  800. }
  801. for (const MemberMatch<DocData::ConstantDoc> &constant_item : match.constants) {
  802. _create_constant_item(parent_item, match.doc, constant_item);
  803. }
  804. for (const MemberMatch<DocData::PropertyDoc> &property_item : match.properties) {
  805. _create_property_item(parent_item, match.doc, property_item);
  806. }
  807. for (const MemberMatch<DocData::ThemeItemDoc> &theme_property_item : match.theme_properties) {
  808. _create_theme_property_item(parent_item, match.doc, theme_property_item);
  809. }
  810. for (const MemberMatch<DocData::MethodDoc> &annotation_item : match.annotations) {
  811. _create_annotation_item(parent_item, match.doc, annotation_item);
  812. }
  813. ++iterator_match;
  814. return !iterator_match;
  815. }
  816. bool EditorHelpSearch::Runner::_phase_select_match() {
  817. if (matched_item) {
  818. matched_item->select(0);
  819. }
  820. return true;
  821. }
  822. void EditorHelpSearch::Runner::_match_method_name_and_push_back(Vector<DocData::MethodDoc> &p_methods, LocalVector<MemberMatch<DocData::MethodDoc>> *r_match_methods) {
  823. // Constructors, Methods, Operators...
  824. for (int i = 0; i < p_methods.size(); i++) {
  825. String method_name = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].name : p_methods[i].name.to_lower();
  826. String keywords = (search_flags & SEARCH_CASE_SENSITIVE) ? p_methods[i].keywords : p_methods[i].keywords.to_lower();
  827. MemberMatch<DocData::MethodDoc> method;
  828. method.name = _all_terms_in_name(method_name);
  829. method.keyword = _match_keywords_in_all_terms(keywords);
  830. if (method.name || !method.keyword.is_empty() ||
  831. (term.begins_with(".") && method_name.begins_with(term.substr(1))) ||
  832. (term.ends_with("(") && method_name.ends_with(term.left(term.length() - 1).strip_edges())) ||
  833. (term.begins_with(".") && term.ends_with("(") && method_name == term.substr(1, term.length() - 2).strip_edges())) {
  834. method.doc = &p_methods[i];
  835. r_match_methods->push_back(method);
  836. }
  837. }
  838. }
  839. bool EditorHelpSearch::Runner::_all_terms_in_name(const String &p_name) const {
  840. for (int i = 0; i < terms.size(); i++) {
  841. if (!_match_string(terms[i], p_name)) {
  842. return false;
  843. }
  844. }
  845. return true;
  846. }
  847. String EditorHelpSearch::Runner::_match_keywords_in_all_terms(const String &p_keywords) const {
  848. String matching_keyword;
  849. for (int i = 0; i < terms.size(); i++) {
  850. matching_keyword = _match_keywords(terms[i], p_keywords);
  851. if (matching_keyword.is_empty()) {
  852. return String();
  853. }
  854. }
  855. return matching_keyword;
  856. }
  857. bool EditorHelpSearch::Runner::_match_string(const String &p_term, const String &p_string) const {
  858. if (search_flags & SEARCH_CASE_SENSITIVE) {
  859. return p_string.contains(p_term);
  860. } else {
  861. return p_string.containsn(p_term);
  862. }
  863. }
  864. String EditorHelpSearch::Runner::_match_keywords(const String &p_term, const String &p_keywords) const {
  865. for (const String &k : p_keywords.split(",")) {
  866. const String keyword = k.strip_edges();
  867. if (_match_string(p_term, keyword)) {
  868. return keyword;
  869. }
  870. }
  871. return String();
  872. }
  873. void EditorHelpSearch::Runner::_match_item(TreeItem *p_item, const String &p_text, bool p_is_keywords) {
  874. if (p_text.is_empty()) {
  875. return;
  876. }
  877. float inverse_length = 1.0f / float(p_text.length());
  878. // Favor types where search term is a substring close to the start of the type.
  879. float w = 0.5f;
  880. int pos = p_text.findn(term);
  881. float score = (pos > -1) ? 1.0f - w * MIN(1, 3 * pos * inverse_length) : MAX(0.0f, 0.9f - w);
  882. // Favor shorter items: they resemble the search term more.
  883. w = 0.1f;
  884. score *= (1 - w) + w * (term.length() * inverse_length);
  885. // Reduce the score of keywords, since they are an indirect match.
  886. if (p_is_keywords) {
  887. score *= 0.9f;
  888. }
  889. // Replace current match if term is short as we are searching in reverse.
  890. if (match_highest_score == 0 || score > match_highest_score || (score == match_highest_score && term.length() == 1)) {
  891. matched_item = p_item;
  892. match_highest_score = score;
  893. }
  894. }
  895. String EditorHelpSearch::Runner::_build_method_tooltip(const DocData::ClassDoc *p_class_doc, const DocData::MethodDoc *p_doc) const {
  896. String tooltip = p_doc->return_type + " " + p_class_doc->name + "." + p_doc->name + "(";
  897. for (int i = 0; i < p_doc->arguments.size(); i++) {
  898. const DocData::ArgumentDoc &arg = p_doc->arguments[i];
  899. tooltip += arg.type + " " + arg.name;
  900. if (!arg.default_value.is_empty()) {
  901. tooltip += " = " + arg.default_value;
  902. }
  903. if (i < p_doc->arguments.size() - 1) {
  904. tooltip += ", ";
  905. }
  906. }
  907. tooltip += ")";
  908. tooltip += _build_keywords_tooltip(p_doc->keywords);
  909. return tooltip;
  910. }
  911. String EditorHelpSearch::Runner::_build_keywords_tooltip(const String &p_keywords) const {
  912. String tooltip;
  913. if (p_keywords.is_empty()) {
  914. return tooltip;
  915. }
  916. tooltip = "\n\n" + TTR("Keywords") + ": ";
  917. for (const String &keyword : p_keywords.split(",")) {
  918. tooltip += keyword.strip_edges().quote() + ", ";
  919. }
  920. // Remove trailing comma and space.
  921. return tooltip.left(-2);
  922. }
  923. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const DocData::ClassDoc *p_class_doc, const String &p_matching_keyword, bool p_gray) {
  924. if (p_class_doc->name.is_empty()) {
  925. return nullptr;
  926. }
  927. if (TreeItem **found = class_items.getptr(p_class_doc->name)) {
  928. return *found;
  929. }
  930. // Ensure parent nodes are created first.
  931. TreeItem *parent_item = root_item;
  932. if (!p_class_doc->inherits.is_empty()) {
  933. if (class_items.has(p_class_doc->inherits)) {
  934. parent_item = class_items[p_class_doc->inherits];
  935. } else if (const DocData::ClassDoc *found = EditorHelp::get_doc_data()->class_list.getptr(p_class_doc->inherits)) {
  936. parent_item = _create_class_hierarchy(found, String(), true);
  937. }
  938. }
  939. TreeItem *class_item = _create_class_item(parent_item, p_class_doc, p_gray, p_matching_keyword);
  940. class_items[p_class_doc->name] = class_item;
  941. return class_item;
  942. }
  943. TreeItem *EditorHelpSearch::Runner::_create_class_hierarchy(const ClassMatch &p_match) {
  944. if (p_match.doc->name.is_empty()) {
  945. return nullptr;
  946. }
  947. if (class_items.has(p_match.doc->name)) {
  948. return class_items[p_match.doc->name];
  949. }
  950. // Ensure parent nodes are created first.
  951. TreeItem *parent_item = root_item;
  952. if (!p_match.doc->inherits.is_empty()) {
  953. if (class_items.has(p_match.doc->inherits)) {
  954. parent_item = class_items[p_match.doc->inherits];
  955. } else {
  956. ClassMatch &base_match = matches[p_match.doc->inherits];
  957. if (base_match.doc) {
  958. parent_item = _create_class_hierarchy(base_match);
  959. }
  960. }
  961. }
  962. TreeItem *class_item = _create_class_item(parent_item, p_match.doc, !p_match.name && p_match.keyword.is_empty(), p_match.name ? String() : p_match.keyword);
  963. class_items[p_match.doc->name] = class_item;
  964. return class_item;
  965. }
  966. bool EditorHelpSearch::Runner::_find_or_create_item(TreeItem *p_parent, const String &p_item_meta, TreeItem *&r_item) {
  967. // Attempt to find in cache.
  968. if (tree_cache->item_cache.has(p_item_meta)) {
  969. r_item = tree_cache->item_cache[p_item_meta];
  970. // Remove from cache.
  971. tree_cache->item_cache.erase(p_item_meta);
  972. // Add to tree.
  973. p_parent->add_child(r_item);
  974. return false;
  975. } else {
  976. // Otherwise create item.
  977. r_item = results_tree->create_item(p_parent);
  978. return true;
  979. }
  980. }
  981. TreeItem *EditorHelpSearch::Runner::_create_class_item(TreeItem *p_parent, const DocData::ClassDoc *p_doc, bool p_gray, const String &p_matching_keyword) {
  982. String tooltip = DTR(p_doc->brief_description.strip_edges());
  983. tooltip += _build_keywords_tooltip(p_doc->keywords);
  984. const String item_meta = "class_name:" + p_doc->name;
  985. TreeItem *item = nullptr;
  986. if (_find_or_create_item(p_parent, item_meta, item)) {
  987. item->set_icon(0, EditorNode::get_singleton()->get_class_icon(p_doc->name));
  988. item->set_text(1, TTR("Class"));
  989. item->set_tooltip_text(0, tooltip);
  990. item->set_tooltip_text(1, tooltip);
  991. item->set_metadata(0, item_meta);
  992. if (p_doc->is_deprecated) {
  993. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  994. item->add_button(0, error_icon, 0, false, TTR("This class is marked as deprecated."));
  995. } else if (p_doc->is_experimental) {
  996. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  997. item->add_button(0, warning_icon, 0, false, TTR("This class is marked as experimental."));
  998. }
  999. }
  1000. // Cached item might be collapsed.
  1001. item->set_collapsed(false);
  1002. if (p_gray) {
  1003. item->set_custom_color(0, disabled_color);
  1004. item->set_custom_color(1, disabled_color);
  1005. } else {
  1006. item->clear_custom_color(0);
  1007. item->clear_custom_color(1);
  1008. }
  1009. if (p_matching_keyword.is_empty()) {
  1010. item->set_text(0, p_doc->name);
  1011. } else {
  1012. item->set_text(0, p_doc->name + " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword));
  1013. }
  1014. if (!term.is_empty()) {
  1015. _match_item(item, p_doc->name);
  1016. }
  1017. for (const String &keyword : p_doc->keywords.split(",")) {
  1018. _match_item(item, keyword.strip_edges(), true);
  1019. }
  1020. return item;
  1021. }
  1022. TreeItem *EditorHelpSearch::Runner::_create_constructor_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1023. String tooltip = p_class_doc->name + "(";
  1024. String text = p_class_doc->name + "(";
  1025. for (int i = 0; i < p_match.doc->arguments.size(); i++) {
  1026. const DocData::ArgumentDoc &arg = p_match.doc->arguments[i];
  1027. tooltip += arg.type + " " + arg.name;
  1028. text += arg.type;
  1029. if (!arg.default_value.is_empty()) {
  1030. tooltip += " = " + arg.default_value;
  1031. }
  1032. if (i < p_match.doc->arguments.size() - 1) {
  1033. tooltip += ", ";
  1034. text += ", ";
  1035. }
  1036. }
  1037. tooltip += ")";
  1038. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1039. text += ")";
  1040. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberConstructor"), p_match.doc->name, text, TTRC("Constructor"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1041. }
  1042. TreeItem *EditorHelpSearch::Runner::_create_method_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1043. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1044. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberMethod"), p_match.doc->name, p_match.doc->name, TTRC("Method"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1045. }
  1046. TreeItem *EditorHelpSearch::Runner::_create_operator_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1047. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1048. String text = p_match.doc->name;
  1049. if (!p_match.doc->arguments.is_empty()) {
  1050. text += "(" + p_match.doc->arguments[0].type + ")";
  1051. }
  1052. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberOperator"), p_match.doc->name, text, TTRC("Operator"), "method", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1053. }
  1054. TreeItem *EditorHelpSearch::Runner::_create_signal_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1055. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1056. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberSignal"), p_match.doc->name, p_match.doc->name, TTRC("Signal"), "signal", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1057. }
  1058. TreeItem *EditorHelpSearch::Runner::_create_annotation_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::MethodDoc> &p_match) {
  1059. String tooltip = _build_method_tooltip(p_class_doc, p_match.doc);
  1060. // Hide the redundant leading @ symbol.
  1061. String text = p_match.doc->name.substr(1);
  1062. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberAnnotation"), p_match.doc->name, text, TTRC("Annotation"), "annotation", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1063. }
  1064. TreeItem *EditorHelpSearch::Runner::_create_constant_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ConstantDoc> &p_match) {
  1065. String tooltip = p_class_doc->name + "." + p_match.doc->name;
  1066. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1067. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberConstant"), p_match.doc->name, p_match.doc->name, TTRC("Constant"), "constant", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1068. }
  1069. TreeItem *EditorHelpSearch::Runner::_create_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::PropertyDoc> &p_match) {
  1070. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1071. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->setter + "(value) setter";
  1072. tooltip += "\n " + p_class_doc->name + "." + p_match.doc->getter + "() getter";
  1073. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberProperty"), p_match.doc->name, p_match.doc->name, TTRC("Property"), "property", tooltip, p_match.doc->keywords, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1074. }
  1075. TreeItem *EditorHelpSearch::Runner::_create_theme_property_item(TreeItem *p_parent, const DocData::ClassDoc *p_class_doc, const MemberMatch<DocData::ThemeItemDoc> &p_match) {
  1076. String tooltip = p_match.doc->type + " " + p_class_doc->name + "." + p_match.doc->name;
  1077. tooltip += _build_keywords_tooltip(p_match.doc->keywords);
  1078. return _create_member_item(p_parent, p_class_doc->name, SNAME("MemberTheme"), p_match.doc->name, p_match.doc->name, TTRC("Theme Property"), "theme_item", p_match.doc->keywords, tooltip, p_match.doc->is_deprecated, p_match.doc->is_experimental, p_match.name ? String() : p_match.keyword);
  1079. }
  1080. TreeItem *EditorHelpSearch::Runner::_create_member_item(TreeItem *p_parent, const String &p_class_name, const StringName &p_icon, const String &p_name, const String &p_text, const String &p_type, const String &p_metatype, const String &p_tooltip, const String &p_keywords, bool p_is_deprecated, bool p_is_experimental, const String &p_matching_keyword) {
  1081. const String item_meta = "class_" + p_metatype + ":" + p_class_name + ":" + p_name;
  1082. TreeItem *item = nullptr;
  1083. if (_find_or_create_item(p_parent, item_meta, item)) {
  1084. item->set_icon(0, ui_service->get_editor_theme_icon(p_icon));
  1085. item->set_text(1, TTRGET(p_type));
  1086. item->set_tooltip_text(0, p_tooltip);
  1087. item->set_tooltip_text(1, p_tooltip);
  1088. item->set_metadata(0, item_meta);
  1089. if (p_is_deprecated) {
  1090. Ref<Texture2D> error_icon = ui_service->get_editor_theme_icon(SNAME("StatusError"));
  1091. item->add_button(0, error_icon, 0, false, TTR("This member is marked as deprecated."));
  1092. } else if (p_is_experimental) {
  1093. Ref<Texture2D> warning_icon = ui_service->get_editor_theme_icon(SNAME("NodeWarning"));
  1094. item->add_button(0, warning_icon, 0, false, TTR("This member is marked as experimental."));
  1095. }
  1096. }
  1097. String text;
  1098. if (search_flags & SEARCH_SHOW_HIERARCHY) {
  1099. text = p_text;
  1100. } else {
  1101. text = p_class_name + "." + p_text;
  1102. }
  1103. if (!p_matching_keyword.is_empty()) {
  1104. text += " - " + vformat(TTR("Matches the \"%s\" keyword."), p_matching_keyword);
  1105. }
  1106. item->set_text(0, text);
  1107. // Don't match member items for short searches.
  1108. if (term.length() > 1 || term == "@") {
  1109. _match_item(item, p_name);
  1110. }
  1111. for (const String &keyword : p_keywords.split(",")) {
  1112. _match_item(item, keyword.strip_edges(), true);
  1113. }
  1114. return item;
  1115. }
  1116. bool EditorHelpSearch::Runner::work(uint64_t slot) {
  1117. // Return true when the search has been completed, otherwise false.
  1118. const uint64_t until = OS::get_singleton()->get_ticks_usec() + slot;
  1119. if (term.length() > 1 || term == "@") {
  1120. while (!_slice()) {
  1121. if (OS::get_singleton()->get_ticks_usec() > until) {
  1122. return false;
  1123. }
  1124. }
  1125. } else {
  1126. while (!_fill()) {
  1127. if (OS::get_singleton()->get_ticks_usec() > until) {
  1128. return false;
  1129. }
  1130. }
  1131. }
  1132. return true;
  1133. }
  1134. EditorHelpSearch::Runner::Runner(Control *p_icon_service, Tree *p_results_tree, TreeCache *p_tree_cache, const String &p_term, int p_search_flags) :
  1135. ui_service(p_icon_service),
  1136. results_tree(p_results_tree),
  1137. tree_cache(p_tree_cache),
  1138. term((p_search_flags & SEARCH_CASE_SENSITIVE) == 0 ? p_term.to_lower() : p_term),
  1139. search_flags(p_search_flags),
  1140. disabled_color(ui_service->get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor))) {
  1141. }