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