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