Debian Codegraph @

editor/doc/editor_help_search.cpp

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