editor_help_search.cpp 50 KB

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