Debian Codegraph @

editor/doc/editor_help.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* editor_help.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.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/core_constants.h"
35 #include "core/extension/gdextension.h"
36 #include "core/input/input.h"
37 #include "core/io/json.h"
38 #include "core/object/script_language.h"
39 #include "core/os/keyboard.h"
40 #include "core/string/string_builder.h"
41 #include "core/version.h"
42 #include "editor/doc/doc_data_compressed.gen.h"
43 #include "editor/docks/filesystem_dock.h"
44 #include "editor/editor_main_screen.h"
45 #include "editor/editor_node.h"
46 #include "editor/editor_string_names.h"
47 #include "editor/file_system/editor_file_system.h"
48 #include "editor/file_system/editor_paths.h"
49 #include "editor/gui/editor_toaster.h"
50 #include "editor/inspector/editor_property_name_processor.h"
51 #include "editor/script/script_editor_plugin.h"
52 #include "editor/settings/editor_settings.h"
53 #include "editor/themes/editor_scale.h"
54 #include "scene/gui/line_edit.h"
55
56 #include "modules/modules_enabled.gen.h" // For gdscript, mono.
57
58 // For syntax highlighting.
59 #ifdef MODULE_GDSCRIPT_ENABLED
60 #include "modules/gdscript/editor/gdscript_highlighter.h"
61 #include "modules/gdscript/gdscript.h"
62 #endif
63
64 // For syntax highlighting.
65 #ifdef MODULE_MONO_ENABLED
66 #include "modules/mono/csharp_script.h"
67 #endif
68
69 #define CONTRIBUTE_URL "https://contributing.godotengine.org/en/latest/documentation/class_reference.html"
70
71 #ifdef MODULE_MONO_ENABLED
72 // Sync with the types mentioned in https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_differences.html
73 const Vector<String> classes_with_csharp_differences = {
74 "@GlobalScope",
75 "String",
76 "NodePath",
77 "Signal",
78 "Callable",
79 "RID",
80 "Basis",
81 "Transform2D",
82 "Transform3D",
83 "Rect2",
84 "Rect2i",
85 "AABB",
86 "Quaternion",
87 "Projection",
88 "Color",
89 "Array",
90 "Dictionary",
91 "PackedByteArray",
92 "PackedColorArray",
93 "PackedFloat32Array",
94 "PackedFloat64Array",
95 "PackedInt32Array",
96 "PackedInt64Array",
97 "PackedStringArray",
98 "PackedVector2Array",
99 "PackedVector3Array",
100 "PackedVector4Array",
101 "Variant",
102 };
103 #endif
104
105 static const char32_t nbsp_chr = 160;
106 static const String nbsp = String::chr(nbsp_chr);
107 static const String nbsp_equal_nbsp = nbsp + "=" + nbsp;
108 static const String colon_nbsp = ":" + nbsp;
109
110 const Vector<String> packed_array_types = {
111 "PackedByteArray",
112 "PackedColorArray",
113 "PackedFloat32Array",
114 "PackedFloat64Array",
115 "PackedInt32Array",
116 "PackedInt64Array",
117 "PackedStringArray",
118 "PackedVector2Array",
119 "PackedVector3Array",
120 "PackedVector4Array",
121 };
122
123 static String _replace_nbsp_with_space(const String &p_string) {
124 return p_string.replace_char(nbsp_chr, ' ');
125 }
126
127 static String _fix_constant(const String &p_constant) {
128 if (p_constant.strip_edges() == "4294967295") {
129 return "0xFFFFFFFF";
130 }
131
132 if (p_constant.strip_edges() == "2147483647") {
133 return "0x7FFFFFFF";
134 }
135
136 if (p_constant.strip_edges() == "1048575") {
137 return "0xFFFFF";
138 }
139
140 return p_constant;
141 }
142
143 static void _add_qualifiers_to_rt(const String &p_qualifiers, RichTextLabel *p_rt) {
144 for (const String &qualifier : p_qualifiers.split_spaces()) {
145 String hint;
146 if (qualifier == "vararg") {
147 hint = TTR("This method supports a variable number of arguments.");
148 } else if (qualifier == "virtual") {
149 hint = TTR("This method is called by the engine.\nIt can be overridden to customize built-in behavior.");
150 } else if (qualifier == "required") {
151 hint = TTR("This method is required to be overridden when extending its base class.");
152 } else if (qualifier == "const") {
153 hint = TTR("This method has no side effects.\nIt does not modify the object in any way.");
154 } else if (qualifier == "static") {
155 hint = TTR("This method does not need an instance to be called.\nIt can be called directly using the class name.");
156 } else if (qualifier == "abstract") {
157 hint = TTR("This method must be implemented to complete the abstract class.");
158 }
159
160 p_rt->add_text(" ");
161 if (hint.is_empty()) {
162 p_rt->add_text(qualifier);
163 } else {
164 p_rt->push_hint(hint);
165 p_rt->add_text(qualifier);
166 p_rt->pop(); // hint
167 }
168 }
169 }
170
171 // Removes unnecessary prefix from `p_class_specifier` when within the `p_edited_class` context.
172 static String _contextualize_class_specifier(const String &p_class_specifier, const String &p_edited_class) {
173 // If this is a completely different context than the current class, then keep full path.
174 if (!p_class_specifier.begins_with(p_edited_class)) {
175 return p_class_specifier;
176 }
177
178 // Here equal `length()` and `begins_with()` from above implies `p_class_specifier == p_edited_class`.
179 if (p_class_specifier.length() == p_edited_class.length()) {
180 int rfind = p_class_specifier.rfind_char('.');
181 if (rfind == -1) { // Single identifier.
182 return p_class_specifier;
183 }
184 // Multiple specifiers: keep last one only.
185 return p_class_specifier.substr(rfind + 1);
186 }
187
188 // They share a _name_ prefix but not a _class specifier_ prefix, e.g. `Tree` and `TreeItem`.
189 // `begins_with()` and `length()`s being different implies `p_class_specifier.length() > p_edited_class.length()` so this is safe.
190 if (p_class_specifier[p_edited_class.length()] != '.') {
191 return p_class_specifier;
192 }
193
194 // Remove class specifier prefix.
195 return p_class_specifier.substr(p_edited_class.length() + 1);
196 }
197
198 /// EditorHelp ///
199
200 void EditorHelp::_update_theme_item_cache() {
201 VBoxContainer::_update_theme_item_cache();
202
203 theme_cache.text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
204 theme_cache.title_color = get_theme_color(SNAME("title_color"), SNAME("EditorHelp"));
205 theme_cache.headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
206 theme_cache.comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
207 theme_cache.symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
208 theme_cache.value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
209 theme_cache.qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
210 theme_cache.type_color = get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
211 theme_cache.override_color = get_theme_color(SNAME("override_color"), SNAME("EditorHelp"));
212
213 theme_cache.doc_font = get_theme_font(SNAME("doc"), EditorStringName(EditorFonts));
214 theme_cache.doc_bold_font = get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
215 theme_cache.doc_italic_font = get_theme_font(SNAME("doc_italic"), EditorStringName(EditorFonts));
216 theme_cache.doc_title_font = get_theme_font(SNAME("doc_title"), EditorStringName(EditorFonts));
217 theme_cache.doc_code_font = get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
218 theme_cache.doc_kbd_font = get_theme_font(SNAME("doc_keyboard"), EditorStringName(EditorFonts));
219
220 theme_cache.doc_font_size = get_theme_font_size(SNAME("doc_size"), EditorStringName(EditorFonts));
221 theme_cache.doc_title_font_size = get_theme_font_size(SNAME("doc_title_size"), EditorStringName(EditorFonts));
222 theme_cache.doc_code_font_size = get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
223 theme_cache.doc_kbd_font_size = get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
224
225 theme_cache.background_style = get_theme_stylebox(SNAME("background"), SNAME("EditorHelp"));
226
227 class_desc->begin_bulk_theme_override();
228 class_desc->add_theme_font_override("normal_font", theme_cache.doc_font);
229 class_desc->add_theme_font_size_override("normal_font_size", theme_cache.doc_font_size);
230
231 class_desc->add_theme_constant_override(SceneStringName(line_separation), get_theme_constant(SceneStringName(line_separation), SNAME("EditorHelp")));
232 class_desc->add_theme_constant_override("table_h_separation", get_theme_constant(SNAME("table_h_separation"), SNAME("EditorHelp")));
233 class_desc->add_theme_constant_override("table_v_separation", get_theme_constant(SNAME("table_v_separation"), SNAME("EditorHelp")));
234 class_desc->add_theme_constant_override("text_highlight_h_padding", get_theme_constant(SNAME("text_highlight_h_padding"), SNAME("EditorHelp")));
235 class_desc->add_theme_constant_override("text_highlight_v_padding", get_theme_constant(SNAME("text_highlight_v_padding"), SNAME("EditorHelp")));
236 class_desc->end_bulk_theme_override();
237 }
238
239 void EditorHelp::_search(bool p_search_previous) {
240 if (p_search_previous) {
241 find_bar->search_prev();
242 } else {
243 find_bar->search_next();
244 }
245 }
246
247 void EditorHelp::_class_desc_finished() {
248 if (scroll_to >= 0) {
249 class_desc->connect(SceneStringName(draw), callable_mp(class_desc, &RichTextLabel::scroll_to_paragraph).bind(scroll_to), CONNECT_ONE_SHOT | CONNECT_DEFERRED);
250 }
251 scroll_to = -1;
252 }
253
254 void EditorHelp::_class_list_select(const String &p_select) {
255 _goto_desc(p_select);
256 }
257
258 void EditorHelp::_class_desc_select(const String &p_select) {
259 if (p_select.begins_with("$")) { // Enum.
260 const String link = p_select.substr(1);
261
262 String enum_class_name;
263 String enum_name;
264 if (CoreConstants::is_global_enum(link)) {
265 enum_class_name = "@GlobalScope";
266 enum_name = link;
267 } else {
268 const int dot_pos = link.rfind_char('.');
269 if (dot_pos >= 0) {
270 enum_class_name = link.left(dot_pos);
271 enum_name = link.substr(dot_pos + 1);
272 } else {
273 enum_class_name = edited_class;
274 enum_name = link;
275 }
276 }
277
278 emit_signal(SNAME("go_to_help"), "class_enum:" + enum_class_name + ":" + enum_name);
279 } else if (p_select.begins_with("#")) { // Class.
280 emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1));
281 } else if (p_select.begins_with("@")) { // Member.
282 const int tag_end = p_select.find_char(' ');
283 const String tag = p_select.substr(1, tag_end - 1);
284 const String link = p_select.substr(tag_end + 1).lstrip(" ");
285
286 String topic;
287 const HashMap<String, int> *table = nullptr;
288
289 if (tag == "method") {
290 topic = "class_method";
291 table = &method_line;
292 } else if (tag == "constructor") {
293 topic = "class_method";
294 table = &method_line;
295 } else if (tag == "operator") {
296 topic = "class_method";
297 table = &method_line;
298 } else if (tag == "member") {
299 topic = "class_property";
300 table = &property_line;
301 } else if (tag == "enum") {
302 topic = "class_enum";
303 table = &enum_line;
304 } else if (tag == "signal") {
305 topic = "class_signal";
306 table = &signal_line;
307 } else if (tag == "constant") {
308 topic = "class_constant";
309 table = &constant_line;
310 } else if (tag == "annotation") {
311 topic = "class_annotation";
312 table = &annotation_line;
313 } else if (tag == "theme_item") {
314 topic = "class_theme_item";
315 table = &theme_property_line;
316 } else {
317 return;
318 }
319
320 // Case order is important here to correctly handle edge cases like `Variant.Type` in `@GlobalScope`.
321 if (table->has(link)) {
322 // Found in the current page.
323 if (class_desc->is_finished()) {
324 emit_signal(SNAME("request_save_history"));
325 class_desc->scroll_to_paragraph((*table)[link]);
326 } else {
327 scroll_to = (*table)[link];
328 }
329 } else {
330 // Look for link in `@GlobalScope`.
331 if (topic == "class_enum") {
332 const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
333 const String enum_link = link.trim_prefix("@GlobalScope.");
334
335 for (const DocData::ConstantDoc &constant : cd.constants) {
336 if (constant.enumeration == enum_link) {
337 // Found in `@GlobalScope`.
338 emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + enum_link);
339 return;
340 }
341 }
342 } else if (topic == "class_constant") {
343 const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
344
345 for (const DocData::ConstantDoc &constant : cd.constants) {
346 if (constant.name == link) {
347 // Found in `@GlobalScope`.
348 emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
349 return;
350 }
351 }
352 }
353
354 if (link.contains_char('.')) {
355 const int class_end = link.rfind_char('.');
356 emit_signal(SNAME("go_to_help"), topic + ":" + link.left(class_end) + ":" + link.substr(class_end + 1));
357 }
358 }
359 } else if (p_select.begins_with("http:") || p_select.begins_with("https:")) {
360 OS::get_singleton()->shell_open(p_select);
361 } else if (p_select.begins_with("^")) { // Copy button.
362 DisplayServer::get_singleton()->clipboard_set(p_select.substr(1));
363 EditorToaster::get_singleton()->popup_str(TTR("Code snippet copied to clipboard."), EditorToaster::SEVERITY_INFO);
364 }
365 }
366
367 void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
368 }
369
370 void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
371 // Add extra horizontal margins for better readability.
372 // The margins increase as the width of the editor help container increases.
373 real_t char_width = theme_cache.doc_code_font->get_char_size('x', theme_cache.doc_code_font_size).width;
374 const int new_display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
375 if (display_margin != new_display_margin || p_force_update_theme) {
376 display_margin = new_display_margin;
377
378 Ref<StyleBox> class_desc_stylebox = theme_cache.background_style->duplicate();
379 class_desc_stylebox->set_content_margin(SIDE_LEFT, display_margin);
380 class_desc_stylebox->set_content_margin(SIDE_RIGHT, display_margin);
381 class_desc->add_theme_style_override(CoreStringName(normal), class_desc_stylebox);
382 class_desc->add_theme_style_override("focused", class_desc_stylebox);
383 }
384 }
385
386 static void _add_type_to_rt(const String &p_type, const String &p_enum, bool p_is_bitfield, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class) {
387 const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
388
389 if (p_type.is_empty() || p_type == "void") {
390 p_rt->push_color(Color(type_color, 0.5));
391 p_rt->push_hint(TTR("No return value."));
392 p_rt->add_text("void");
393 p_rt->pop(); // hint
394 p_rt->pop(); // color
395 return;
396 }
397
398 bool is_enum_type = !p_enum.is_empty();
399 bool is_bitfield = p_is_bitfield && is_enum_type;
400 bool can_ref = !p_type.contains_char('*') || is_enum_type;
401
402 String link_t = p_type; // For links in metadata
403 String display_t; // For display purposes.
404 if (is_enum_type) {
405 link_t = p_enum; // The link for enums is always the full enum description
406 display_t = _contextualize_class_specifier(p_enum, p_class);
407 } else {
408 display_t = _contextualize_class_specifier(p_type, p_class);
409 }
410
411 p_rt->push_color(type_color);
412 bool add_typed_container = false;
413 if (can_ref) {
414 if (link_t.ends_with("[]")) {
415 add_typed_container = true;
416 link_t = link_t.trim_suffix("[]");
417 display_t = display_t.trim_suffix("[]");
418
419 p_rt->push_meta("#Array", RichTextLabel::META_UNDERLINE_ON_HOVER); // class
420 p_rt->add_text("Array");
421 p_rt->pop(); // meta
422 p_rt->add_text("[");
423 } else if (link_t.begins_with("Dictionary[")) {
424 add_typed_container = true;
425 link_t = link_t.trim_prefix("Dictionary[").trim_suffix("]");
426 display_t = display_t.trim_prefix("Dictionary[").trim_suffix("]");
427
428 p_rt->push_meta("#Dictionary", RichTextLabel::META_UNDERLINE_ON_HOVER); // class
429 p_rt->add_text("Dictionary");
430 p_rt->pop(); // meta
431 p_rt->add_text("[");
432 p_rt->push_meta("#" + link_t.get_slice(", ", 0), RichTextLabel::META_UNDERLINE_ON_HOVER); // class
433 p_rt->add_text(_contextualize_class_specifier(display_t.get_slice(", ", 0), p_class));
434 p_rt->pop(); // meta
435 p_rt->add_text(", ");
436
437 link_t = link_t.get_slice(", ", 1);
438 display_t = _contextualize_class_specifier(display_t.get_slice(", ", 1), p_class);
439 } else if (is_bitfield) {
440 p_rt->push_color(Color(type_color, 0.5));
441 p_rt->push_hint(TTR("This value is an integer composed as a bitmask of the following flags."));
442 p_rt->add_text("BitField");
443 p_rt->pop(); // hint
444 p_rt->add_text("[");
445 p_rt->pop(); // color
446 }
447
448 if (is_enum_type) {
449 p_rt->push_meta("$" + link_t, RichTextLabel::META_UNDERLINE_ON_HOVER); // enum
450 } else {
451 p_rt->push_meta("#" + link_t, RichTextLabel::META_UNDERLINE_ON_HOVER); // class
452 }
453 }
454 p_rt->add_text(display_t);
455 if (can_ref) {
456 p_rt->pop(); // meta
457 if (add_typed_container) {
458 p_rt->add_text("]");
459 } else if (is_bitfield) {
460 p_rt->push_color(Color(type_color, 0.5));
461 p_rt->add_text("]");
462 p_rt->pop(); // color
463 }
464 }
465 p_rt->pop(); // color
466 }
467
468 void EditorHelp::_add_type(const String &p_type, const String &p_enum, bool p_is_bitfield) {
469 _add_type_to_rt(p_type, p_enum, p_is_bitfield, class_desc, this, edited_class);
470 }
471
472 void EditorHelp::_add_type_icon(const String &p_type, int p_size, const String &p_fallback) {
473 Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(p_type, p_fallback);
474 if (icon.is_null()) {
475 icon = EditorNode::get_singleton()->get_class_icon("Object");
476 ERR_FAIL_COND(icon.is_null());
477 }
478 Vector2i size = Vector2i(icon->get_width(), icon->get_height());
479 if (p_size > 0) {
480 // Ensures icon scales proportionally on both axes, based on icon height.
481 float ratio = p_size / float(size.height);
482 size.width *= ratio;
483 size.height *= ratio;
484 }
485
486 class_desc->add_image(icon, size.width, size.height);
487 }
488
489 // Macros for assigning the deprecated/experimental marks to class members in overview.
490
491 #define DEPRECATED_DOC_TAG \
492 class_desc->push_font(theme_cache.doc_bold_font); \
493 class_desc->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor))); \
494 Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError")); \
495 class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
496 class_desc->add_text(String::chr(160) + TTR("Deprecated")); \
497 class_desc->pop(); \
498 class_desc->pop();
499
500 #define EXPERIMENTAL_DOC_TAG \
501 class_desc->push_font(theme_cache.doc_bold_font); \
502 class_desc->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); \
503 Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning")); \
504 class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
505 class_desc->add_text(String::chr(160) + TTR("Experimental")); \
506 class_desc->pop(); \
507 class_desc->pop();
508
509 // Macros for displaying the deprecated/experimental info in class member descriptions.
510
511 #define DEPRECATED_DOC_MSG(m_message, m_default_message) \
512 Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError")); \
513 class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
514 class_desc->add_text(nbsp); \
515 class_desc->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor))); \
516 class_desc->push_font(theme_cache.doc_bold_font); \
517 class_desc->add_text(TTR("Deprecated:")); \
518 class_desc->pop(); \
519 class_desc->pop(); \
520 class_desc->add_text(" "); \
521 if ((m_message).is_empty()) { \
522 class_desc->add_text(m_default_message); \
523 } else { \
524 _add_text(m_message); \
525 }
526
527 #define EXPERIMENTAL_DOC_MSG(m_message, m_default_message) \
528 Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning")); \
529 class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
530 class_desc->add_text(nbsp); \
531 class_desc->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); \
532 class_desc->push_font(theme_cache.doc_bold_font); \
533 class_desc->add_text(TTR("Experimental:")); \
534 class_desc->pop(); \
535 class_desc->pop(); \
536 class_desc->add_text(" "); \
537 if ((m_message).is_empty()) { \
538 class_desc->add_text(m_default_message); \
539 } else { \
540 _add_text(m_message); \
541 }
542
543 void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview, bool p_override) {
544 if (p_override) {
545 method_line[p_method.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
546 }
547
548 const bool is_vararg = p_method.qualifiers.contains("vararg");
549
550 if (p_overview) {
551 class_desc->push_cell();
552 class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
553 } else {
554 _add_bulletpoint();
555 }
556
557 _add_type(p_method.return_type, p_method.return_enum, p_method.return_is_bitfield);
558
559 if (p_overview) {
560 class_desc->pop(); // paragraph
561 class_desc->pop(); // cell
562 class_desc->push_cell();
563 } else {
564 class_desc->add_text(" ");
565 }
566
567 const bool is_documented = p_method.is_deprecated || p_method.is_experimental || !p_method.description.strip_edges().is_empty();
568
569 if (p_overview && is_documented) {
570 class_desc->push_meta("@method " + p_method.name, RichTextLabel::META_UNDERLINE_ON_HOVER);
571 }
572
573 class_desc->push_color(theme_cache.headline_color);
574 class_desc->add_text(p_method.name);
575 class_desc->pop(); // color
576
577 if (p_overview && is_documented) {
578 class_desc->pop(); // meta
579 }
580
581 class_desc->push_color(theme_cache.symbol_color);
582 class_desc->add_text("(");
583 class_desc->pop(); // color
584
585 for (int j = 0; j < p_method.arguments.size(); j++) {
586 const DocData::ArgumentDoc &argument = p_method.arguments[j];
587
588 if (j > 0) {
589 class_desc->push_color(theme_cache.symbol_color);
590 class_desc->add_text(", ");
591 class_desc->pop(); // color
592 }
593
594 class_desc->push_color(theme_cache.text_color);
595 class_desc->add_text(argument.name);
596 class_desc->pop(); // color
597
598 class_desc->push_color(theme_cache.symbol_color);
599 class_desc->add_text(colon_nbsp);
600 class_desc->pop(); // color
601
602 _add_type(argument.type, argument.enumeration, argument.is_bitfield);
603
604 if (!argument.default_value.is_empty()) {
605 class_desc->push_color(theme_cache.symbol_color);
606 class_desc->add_text(nbsp_equal_nbsp);
607 class_desc->pop(); // color
608
609 class_desc->push_color(theme_cache.value_color);
610 class_desc->add_text(_fix_constant(argument.default_value));
611 class_desc->pop(); // color
612 }
613 }
614
615 if (is_vararg) {
616 if (!p_method.arguments.is_empty()) {
617 class_desc->push_color(theme_cache.symbol_color);
618 class_desc->add_text(", ");
619 class_desc->pop(); // color
620 }
621
622 class_desc->push_color(theme_cache.symbol_color);
623 class_desc->add_text("...");
624 class_desc->pop(); // color
625
626 const DocData::ArgumentDoc &rest_argument = p_method.rest_argument;
627
628 class_desc->push_color(theme_cache.text_color);
629 class_desc->add_text(rest_argument.name.is_empty() ? "args" : rest_argument.name);
630 class_desc->pop(); // color
631
632 class_desc->push_color(theme_cache.symbol_color);
633 class_desc->add_text(colon_nbsp);
634 class_desc->pop(); // color
635
636 if (rest_argument.type.is_empty()) {
637 _add_type("Array");
638 } else {
639 _add_type(rest_argument.type, rest_argument.enumeration, rest_argument.is_bitfield);
640 }
641 }
642
643 class_desc->push_color(theme_cache.symbol_color);
644 class_desc->add_text(")");
645 class_desc->pop(); // color
646
647 if (!p_method.qualifiers.is_empty()) {
648 class_desc->push_color(theme_cache.qualifier_color);
649 _add_qualifiers_to_rt(p_method.qualifiers, class_desc);
650 class_desc->pop(); // color
651 }
652
653 if (p_overview) {
654 if (p_method.is_deprecated) {
655 class_desc->add_text(" ");
656 DEPRECATED_DOC_TAG;
657 }
658
659 if (p_method.is_experimental) {
660 class_desc->add_text(" ");
661 EXPERIMENTAL_DOC_TAG;
662 }
663
664 class_desc->pop(); // cell
665 }
666 }
667
668 void EditorHelp::_add_bulletpoint() {
669 static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
670 class_desc->add_text(String(prefix));
671 }
672
673 void EditorHelp::_push_normal_font() {
674 class_desc->push_font(theme_cache.doc_font);
675 class_desc->push_font_size(theme_cache.doc_font_size);
676 }
677
678 void EditorHelp::_pop_normal_font() {
679 class_desc->pop(); // font_size
680 class_desc->pop(); // font
681 }
682
683 void EditorHelp::_push_title_font() {
684 class_desc->push_font(theme_cache.doc_title_font);
685 class_desc->push_font_size(theme_cache.doc_title_font_size);
686 class_desc->push_color(theme_cache.title_color);
687 }
688
689 void EditorHelp::_pop_title_font() {
690 class_desc->pop(); // color
691 class_desc->pop(); // font_size
692 class_desc->pop(); // font
693 }
694
695 void EditorHelp::_push_code_font() {
696 class_desc->push_font(theme_cache.doc_code_font);
697 class_desc->push_font_size(theme_cache.doc_code_font_size);
698 }
699
700 void EditorHelp::_pop_code_font() {
701 class_desc->pop(); // font_size
702 class_desc->pop(); // font
703 }
704
705 Error EditorHelp::_goto_desc(const String &p_class) {
706 if (!doc->class_list.has(p_class)) {
707 return ERR_DOES_NOT_EXIST;
708 }
709
710 select_locked = true;
711
712 class_desc->show();
713
714 description_line = 0;
715
716 if (p_class == edited_class) {
717 return OK; // Already there.
718 }
719
720 edited_class = p_class;
721 _update_doc();
722 return OK;
723 }
724
725 void EditorHelp::_update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods) {
726 class_desc->add_newline();
727 class_desc->add_newline();
728
729 static const char *titles_by_type[METHOD_TYPE_MAX] = {
730 TTRC("Methods"),
731 TTRC("Constructors"),
732 TTRC("Operators"),
733 };
734 const String title = TTRGET(titles_by_type[p_method_type]);
735
736 section_line.push_back(Pair<String, int>(title, class_desc->get_paragraph_count() - 2));
737 _push_title_font();
738 class_desc->add_text(title);
739 _pop_title_font();
740
741 class_desc->add_newline();
742 class_desc->add_newline();
743
744 class_desc->push_indent(1);
745 _push_code_font();
746 class_desc->push_table(2);
747 class_desc->set_table_column_expand(1, true);
748
749 bool any_previous = false;
750 for (int pass = 0; pass < 2; pass++) {
751 Vector<DocData::MethodDoc> m;
752
753 for (const DocData::MethodDoc &method : p_methods) {
754 const String &q = method.qualifiers;
755 if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
756 m.push_back(method);
757 }
758 }
759
760 if (any_previous && !m.is_empty()) {
761 class_desc->push_cell();
762 class_desc->pop(); // cell
763 class_desc->push_cell();
764 class_desc->pop(); // cell
765 }
766
767 String group_prefix;
768 for (int i = 0; i < m.size(); i++) {
769 const String new_prefix = m[i].name.left(3);
770 bool is_new_group = false;
771
772 if (i < m.size() - 1 && new_prefix == m[i + 1].name.left(3) && new_prefix != group_prefix) {
773 is_new_group = i > 0;
774 group_prefix = new_prefix;
775 } else if (!group_prefix.is_empty() && new_prefix != group_prefix) {
776 is_new_group = true;
777 group_prefix = "";
778 }
779
780 if (is_new_group && pass == 1) {
781 class_desc->push_cell();
782 class_desc->pop(); // cell
783 class_desc->push_cell();
784 class_desc->pop(); // cell
785 }
786
787 // For constructors always point to the first one.
788 _add_method(m[i], true, (p_method_type != METHOD_TYPE_CONSTRUCTOR || i == 0));
789 }
790
791 any_previous = !m.is_empty();
792 }
793
794 class_desc->pop(); // table
795 _pop_code_font();
796 class_desc->pop(); // indent
797 }
798
799 void EditorHelp::_update_method_descriptions(const DocData::ClassDoc &p_classdoc, MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods) {
800 #define HANDLE_DOC(m_string) ((p_classdoc.is_script_doc ? (m_string) : DTR(m_string)).strip_edges())
801
802 class_desc->add_newline();
803 class_desc->add_newline();
804 class_desc->add_newline();
805
806 static const char *titles_by_type[METHOD_TYPE_MAX] = {
807 TTRC("Method Descriptions"),
808 TTRC("Constructor Descriptions"),
809 TTRC("Operator Descriptions"),
810 };
811 const String title = TTRGET(titles_by_type[p_method_type]);
812
813 section_line.push_back(Pair<String, int>(title, class_desc->get_paragraph_count() - 2));
814 _push_title_font();
815 class_desc->add_text(title);
816 _pop_title_font();
817
818 String link_color_text = theme_cache.title_color.to_html(false);
819
820 for (int pass = 0; pass < 2; pass++) {
821 Vector<DocData::MethodDoc> methods_filtered;
822
823 for (int i = 0; i < p_methods.size(); i++) {
824 const String &q = p_methods[i].qualifiers;
825 if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
826 methods_filtered.push_back(p_methods[i]);
827 }
828 }
829
830 for (int i = 0; i < methods_filtered.size(); i++) {
831 const DocData::MethodDoc &method = methods_filtered[i];
832
833 class_desc->add_newline();
834 class_desc->add_newline();
835 class_desc->add_newline();
836
837 _push_code_font();
838 // For constructors always point to the first one.
839 _add_method(method, false, (p_method_type != METHOD_TYPE_CONSTRUCTOR || i == 0));
840 _pop_code_font();
841
842 class_desc->add_newline();
843 class_desc->add_newline();
844
845 class_desc->push_indent(1);
846 _push_normal_font();
847 class_desc->push_color(theme_cache.text_color);
848
849 bool has_prev_text = false;
850
851 if (method.is_deprecated) {
852 has_prev_text = true;
853
854 static const char *messages_by_type[METHOD_TYPE_MAX] = {
855 TTRC("This method may be changed or removed in future versions."),
856 TTRC("This constructor may be changed or removed in future versions."),
857 TTRC("This operator may be changed or removed in future versions."),
858 };
859 DEPRECATED_DOC_MSG(HANDLE_DOC(method.deprecated_message), TTRGET(messages_by_type[p_method_type]));
860 }
861
862 if (method.is_experimental) {
863 if (has_prev_text) {
864 class_desc->add_newline();
865 class_desc->add_newline();
866 }
867 has_prev_text = true;
868
869 static const char *messages_by_type[METHOD_TYPE_MAX] = {
870 TTRC("This method may be changed or removed in future versions."),
871 TTRC("This constructor may be changed or removed in future versions."),
872 TTRC("This operator may be changed or removed in future versions."),
873 };
874 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(method.experimental_message), TTRGET(messages_by_type[p_method_type]));
875 }
876
877 if (!method.errors_returned.is_empty()) {
878 if (has_prev_text) {
879 class_desc->add_newline();
880 class_desc->add_newline();
881 }
882 has_prev_text = true;
883
884 class_desc->add_text(TTR("Error codes returned:"));
885 class_desc->add_newline();
886 class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
887 for (int j = 0; j < method.errors_returned.size(); j++) {
888 if (j > 0) {
889 class_desc->add_newline();
890 }
891
892 int val = method.errors_returned[j];
893 String text = itos(val);
894 for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
895 if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
896 text = CoreConstants::get_global_constant_name(k);
897 break;
898 }
899 }
900
901 class_desc->push_font(theme_cache.doc_bold_font);
902 class_desc->add_text(text);
903 class_desc->pop(); // font
904 }
905 class_desc->pop(); // list
906 }
907
908 const String descr = HANDLE_DOC(method.description);
909 const bool is_documented = method.is_deprecated || method.is_experimental || !descr.is_empty();
910 if (!descr.is_empty()) {
911 if (has_prev_text) {
912 class_desc->add_newline();
913 class_desc->add_newline();
914 }
915 has_prev_text = true;
916
917 _add_text(descr);
918 } else if (!is_documented) {
919 if (has_prev_text) {
920 class_desc->add_newline();
921 class_desc->add_newline();
922 }
923 has_prev_text = true;
924
925 String message;
926 if (p_classdoc.is_script_doc) {
927 static const char *messages_by_type[METHOD_TYPE_MAX] = {
928 TTRC("There is currently no description for this method."),
929 TTRC("There is currently no description for this constructor."),
930 TTRC("There is currently no description for this operator."),
931 };
932 message = TTRGET(messages_by_type[p_method_type]);
933 } else {
934 static const char *messages_by_type[METHOD_TYPE_MAX] = {
935 TTRC("There is currently no description for this method. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
936 TTRC("There is currently no description for this constructor. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
937 TTRC("There is currently no description for this operator. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
938 };
939 message = TTRGET(messages_by_type[p_method_type]).replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text);
940 }
941
942 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
943 class_desc->add_text(" ");
944 class_desc->push_color(theme_cache.comment_color);
945 class_desc->append_text(message);
946 class_desc->pop(); // color
947 }
948
949 class_desc->pop(); // color
950 _pop_normal_font();
951 class_desc->pop(); // indent
952 }
953 }
954
955 #undef HANDLE_DOC
956 }
957
958 void EditorHelp::_update_doc() {
959 if (!doc->class_list.has(edited_class)) {
960 return;
961 }
962
963 scroll_locked = true;
964
965 class_desc->clear();
966 method_line.clear();
967 section_line.clear();
968 section_line.push_back(Pair<String, int>(TTR("Top"), 0));
969
970 String link_color_text = theme_cache.title_color.to_html(false);
971
972 DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
973
974 #define HANDLE_DOC(m_string) ((cd.is_script_doc ? (m_string) : DTR(m_string)).strip_edges())
975
976 // Class name
977
978 _push_title_font();
979
980 class_desc->add_text(TTR("Class:") + " ");
981 _add_type_icon(edited_class, theme_cache.doc_title_font_size, "");
982 class_desc->add_text(nbsp);
983
984 class_desc->push_color(theme_cache.headline_color);
985 class_desc->add_text(edited_class);
986 class_desc->pop(); // color
987
988 _pop_title_font();
989
990 if (cd.is_deprecated) {
991 class_desc->add_newline();
992 DEPRECATED_DOC_MSG(HANDLE_DOC(cd.deprecated_message), TTR("This class may be changed or removed in future versions."));
993 }
994
995 if (cd.is_experimental) {
996 class_desc->add_newline();
997 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(cd.experimental_message), TTR("This class may be changed or removed in future versions."));
998 }
999
1000 // Inheritance tree
1001
1002 // Ascendents
1003 if (!cd.inherits.is_empty()) {
1004 class_desc->add_newline();
1005
1006 _push_normal_font();
1007 class_desc->push_color(theme_cache.title_color);
1008
1009 class_desc->add_text(TTR("Inherits:") + " ");
1010
1011 String inherits = cd.inherits;
1012 while (!inherits.is_empty()) {
1013 _add_type_icon(inherits, theme_cache.doc_font_size, "ArrowRight");
1014 class_desc->add_text(nbsp); // Otherwise icon borrows hyperlink from `_add_type()`.
1015 _add_type(inherits);
1016
1017 const DocData::ClassDoc *base_class_doc = doc->class_list.getptr(inherits);
1018 inherits = base_class_doc ? base_class_doc->inherits : String();
1019
1020 if (!inherits.is_empty()) {
1021 class_desc->add_text(" < ");
1022 }
1023 }
1024
1025 class_desc->pop(); // color
1026 _pop_normal_font();
1027 }
1028
1029 // Descendants
1030 if ((cd.is_script_doc || ClassDB::class_exists(cd.name)) && doc->inheriting.has(cd.name)) {
1031 class_desc->add_newline();
1032
1033 _push_normal_font();
1034 class_desc->push_color(theme_cache.title_color);
1035 class_desc->add_text(TTR("Inherited by:") + " ");
1036
1037 for (RBSet<String, NaturalNoCaseComparator>::Element *itr = doc->inheriting[cd.name].front(); itr; itr = itr->next()) {
1038 if (itr->prev()) {
1039 class_desc->add_text(" , ");
1040 }
1041
1042 _add_type_icon(itr->get(), theme_cache.doc_font_size, "ArrowRight");
1043 class_desc->add_text(nbsp); // Otherwise icon borrows hyperlink from `_add_type()`.
1044 _add_type(itr->get());
1045 }
1046
1047 class_desc->pop(); // color
1048 _pop_normal_font();
1049 }
1050
1051 bool has_description = false;
1052
1053 // Brief description
1054 const String brief_class_descr = HANDLE_DOC(cd.brief_description);
1055 if (!brief_class_descr.is_empty()) {
1056 has_description = true;
1057
1058 class_desc->add_newline();
1059 class_desc->add_newline();
1060
1061 class_desc->push_indent(1);
1062 class_desc->push_font(theme_cache.doc_bold_font);
1063 class_desc->push_color(theme_cache.text_color);
1064
1065 _add_text(brief_class_descr);
1066
1067 class_desc->pop(); // color
1068 class_desc->pop(); // font
1069 class_desc->pop(); // indent
1070 }
1071
1072 // Class description
1073 const String class_descr = HANDLE_DOC(cd.description);
1074 if (!class_descr.is_empty()) {
1075 has_description = true;
1076
1077 class_desc->add_newline();
1078 class_desc->add_newline();
1079
1080 section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_paragraph_count() - 2));
1081 description_line = class_desc->get_paragraph_count() - 2;
1082 _push_title_font();
1083 class_desc->add_text(TTR("Description"));
1084 _pop_title_font();
1085
1086 class_desc->add_newline();
1087 class_desc->add_newline();
1088
1089 class_desc->push_indent(1);
1090 _push_normal_font();
1091 class_desc->push_color(theme_cache.text_color);
1092
1093 _add_text(class_descr);
1094
1095 class_desc->pop(); // color
1096 _pop_normal_font();
1097 class_desc->pop(); // indent
1098 }
1099
1100 if (!has_description) {
1101 class_desc->add_newline();
1102 class_desc->add_newline();
1103
1104 class_desc->push_indent(1);
1105 _push_normal_font();
1106
1107 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
1108 class_desc->add_text(" ");
1109
1110 class_desc->push_color(theme_cache.comment_color);
1111 if (cd.is_script_doc) {
1112 class_desc->add_text(TTR("There is currently no description for this class."));
1113 } else {
1114 class_desc->append_text(TTR("There is currently no description for this class. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
1115 }
1116 class_desc->pop(); // color
1117
1118 _pop_normal_font();
1119 class_desc->pop(); // indent
1120 }
1121
1122 #ifdef MODULE_MONO_ENABLED
1123 if (classes_with_csharp_differences.has(cd.name)) {
1124 class_desc->add_newline();
1125 class_desc->add_newline();
1126
1127 const String &csharp_differences_url = vformat("%s/tutorials/scripting/c_sharp/c_sharp_differences.html", GODOT_VERSION_DOCS_URL);
1128
1129 class_desc->push_indent(1);
1130 _push_normal_font();
1131 class_desc->push_color(theme_cache.text_color);
1132
1133 class_desc->append_text("[b]" + TTR("Note:") + "[/b] " + vformat(TTR("There are notable differences when using this API with C#. See [url=%s]C# API differences to GDScript[/url] for more information."), csharp_differences_url));
1134
1135 class_desc->pop(); // color
1136 _pop_normal_font();
1137 class_desc->pop(); // indent
1138 }
1139 #endif
1140
1141 // Online tutorials
1142 if (!cd.tutorials.is_empty()) {
1143 class_desc->add_newline();
1144 class_desc->add_newline();
1145
1146 _push_title_font();
1147 class_desc->add_text(TTR("Online Tutorials"));
1148 _pop_title_font();
1149
1150 class_desc->add_newline();
1151
1152 class_desc->push_indent(1);
1153 _push_code_font();
1154 class_desc->push_color(theme_cache.symbol_color);
1155
1156 for (const DocData::TutorialDoc &tutorial : cd.tutorials) {
1157 const String link = HANDLE_DOC(tutorial.link);
1158
1159 String link_text = HANDLE_DOC(tutorial.title);
1160 if (link_text.is_empty()) {
1161 const int sep_pos = link.find("//");
1162 if (sep_pos >= 0) {
1163 link_text = link.substr(sep_pos + 2);
1164 } else {
1165 link_text = link;
1166 }
1167 }
1168
1169 class_desc->add_newline();
1170 _add_bulletpoint();
1171 class_desc->append_text("[url=" + link + "]" + link_text + "[/url]");
1172 }
1173
1174 class_desc->pop(); // color
1175 _pop_code_font();
1176 class_desc->pop(); // indent
1177 }
1178
1179 // Properties overview
1180 HashSet<String> skip_methods;
1181
1182 bool has_properties = false;
1183 bool has_property_descriptions = false;
1184 for (const DocData::PropertyDoc &prop : cd.properties) {
1185 const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
1186 if (!is_documented && prop.name.begins_with("_")) {
1187 continue;
1188 }
1189 has_properties = true;
1190 if (!prop.overridden) {
1191 has_property_descriptions = true;
1192 break;
1193 }
1194 }
1195
1196 if (has_properties) {
1197 class_desc->add_newline();
1198 class_desc->add_newline();
1199
1200 section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_paragraph_count() - 2));
1201 _push_title_font();
1202 class_desc->add_text(TTR("Properties"));
1203 _pop_title_font();
1204
1205 class_desc->add_newline();
1206 class_desc->add_newline();
1207
1208 class_desc->push_indent(1);
1209 _push_code_font();
1210 class_desc->push_table(4);
1211 class_desc->set_table_column_expand(1, true);
1212
1213 cd.properties.sort_custom<PropertyCompare>();
1214
1215 bool is_generating_overridden_properties = true; // Set to false as soon as we encounter a non-overridden property.
1216 bool overridden_property_exists = false;
1217
1218 for (const DocData::PropertyDoc &prop : cd.properties) {
1219 // Ignore undocumented private.
1220 const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
1221 if (!is_documented && prop.name.begins_with("_")) {
1222 continue;
1223 }
1224
1225 if (is_generating_overridden_properties && !prop.overridden) {
1226 is_generating_overridden_properties = false;
1227 // No need for the extra spacing when there's no overridden property.
1228 if (overridden_property_exists) {
1229 class_desc->push_cell();
1230 class_desc->pop(); // cell
1231 class_desc->push_cell();
1232 class_desc->pop(); // cell
1233 class_desc->push_cell();
1234 class_desc->pop(); // cell
1235 class_desc->push_cell();
1236 class_desc->pop(); // cell
1237 }
1238 }
1239
1240 property_line[prop.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
1241
1242 // Property type.
1243 class_desc->push_cell();
1244 class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
1245 _add_type(prop.type, prop.enumeration, prop.is_bitfield);
1246 class_desc->pop(); // paragraph
1247 class_desc->pop(); // cell
1248
1249 bool describe = false;
1250
1251 if (!prop.setter.is_empty()) {
1252 skip_methods.insert(prop.setter);
1253 describe = true;
1254 }
1255 if (!prop.getter.is_empty()) {
1256 skip_methods.insert(prop.getter);
1257 describe = true;
1258 }
1259
1260 if (is_documented) {
1261 describe = true;
1262 }
1263
1264 if (prop.overridden) {
1265 describe = false;
1266 }
1267
1268 // Property name.
1269 class_desc->push_cell();
1270 class_desc->push_color(theme_cache.headline_color);
1271
1272 if (describe) {
1273 class_desc->push_meta("@member " + prop.name, RichTextLabel::META_UNDERLINE_ON_HOVER);
1274 }
1275
1276 class_desc->add_text(prop.name);
1277
1278 if (describe) {
1279 class_desc->pop(); // meta
1280 }
1281
1282 class_desc->pop(); // color
1283 class_desc->pop(); // cell
1284
1285 // Property value.
1286 class_desc->push_cell();
1287
1288 if (!prop.default_value.is_empty()) {
1289 if (prop.overridden) {
1290 class_desc->push_color(theme_cache.override_color);
1291 class_desc->add_text("[");
1292 const String link = vformat("[url=@member %s.%s]%s[/url]", prop.overrides, prop.name, prop.overrides);
1293 class_desc->append_text(vformat(TTR("overrides %s:"), link));
1294 class_desc->add_text(" " + _fix_constant(prop.default_value) + "]");
1295 class_desc->pop(); // color
1296 overridden_property_exists = true;
1297 } else {
1298 class_desc->push_color(theme_cache.symbol_color);
1299 class_desc->add_text("[" + TTR("default:") + " ");
1300 class_desc->pop(); // color
1301
1302 class_desc->push_color(theme_cache.value_color);
1303 class_desc->add_text(_fix_constant(prop.default_value));
1304 class_desc->pop(); // color
1305
1306 class_desc->push_color(theme_cache.symbol_color);
1307 class_desc->add_text("]");
1308 class_desc->pop(); // color
1309 }
1310 }
1311
1312 class_desc->pop(); // cell
1313
1314 // Property setter/getter and deprecated/experimental marks.
1315 class_desc->push_cell();
1316
1317 bool has_prev_text = false;
1318
1319 if (cd.is_script_doc && (!prop.setter.is_empty() || !prop.getter.is_empty())) {
1320 has_prev_text = true;
1321
1322 class_desc->push_color(theme_cache.symbol_color);
1323 class_desc->add_text("[" + TTR("property:") + " ");
1324 class_desc->pop(); // color
1325
1326 if (!prop.setter.is_empty()) {
1327 class_desc->push_color(theme_cache.value_color);
1328 class_desc->add_text("setter");
1329 class_desc->pop(); // color
1330 }
1331 if (!prop.getter.is_empty()) {
1332 if (!prop.setter.is_empty()) {
1333 class_desc->push_color(theme_cache.symbol_color);
1334 class_desc->add_text(", ");
1335 class_desc->pop(); // color
1336 }
1337 class_desc->push_color(theme_cache.value_color);
1338 class_desc->add_text("getter");
1339 class_desc->pop(); // color
1340 }
1341
1342 class_desc->push_color(theme_cache.symbol_color);
1343 class_desc->add_text("]");
1344 class_desc->pop(); // color
1345 }
1346
1347 if (prop.is_deprecated) {
1348 if (has_prev_text) {
1349 class_desc->add_text(" ");
1350 }
1351 has_prev_text = true;
1352 DEPRECATED_DOC_TAG;
1353 }
1354
1355 if (prop.is_experimental) {
1356 if (has_prev_text) {
1357 class_desc->add_text(" ");
1358 }
1359 has_prev_text = true;
1360 EXPERIMENTAL_DOC_TAG;
1361 }
1362
1363 class_desc->pop(); // cell
1364 }
1365
1366 class_desc->pop(); // table
1367 _pop_code_font();
1368 class_desc->pop(); // indent
1369 }
1370
1371 // Methods overview
1372 bool sort_methods = EDITOR_GET("text_editor/help/sort_functions_alphabetically");
1373
1374 Vector<DocData::MethodDoc> methods;
1375
1376 for (const DocData::MethodDoc &method : cd.methods) {
1377 if (skip_methods.has(method.name)) {
1378 if (method.arguments.is_empty() /* getter */ || (method.arguments.size() == 1 && method.return_type == "void" /* setter */)) {
1379 continue;
1380 }
1381 }
1382 // Ignore undocumented non virtual private.
1383 const bool is_documented = method.is_deprecated || method.is_experimental || !method.description.strip_edges().is_empty();
1384 if (!is_documented && method.name.begins_with("_") && !method.qualifiers.contains("virtual")) {
1385 continue;
1386 }
1387 methods.push_back(method);
1388 }
1389
1390 if (!cd.constructors.is_empty()) {
1391 if (sort_methods) {
1392 cd.constructors.sort();
1393 }
1394 _update_method_list(METHOD_TYPE_CONSTRUCTOR, cd.constructors);
1395 }
1396
1397 if (!methods.is_empty()) {
1398 if (sort_methods) {
1399 methods.sort();
1400 }
1401 _update_method_list(METHOD_TYPE_METHOD, methods);
1402 }
1403
1404 if (!cd.operators.is_empty()) {
1405 if (sort_methods) {
1406 cd.operators.sort();
1407 }
1408 _update_method_list(METHOD_TYPE_OPERATOR, cd.operators);
1409 }
1410
1411 // Theme properties
1412 if (!cd.theme_properties.is_empty()) {
1413 class_desc->add_newline();
1414 class_desc->add_newline();
1415
1416 section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_paragraph_count() - 2));
1417 _push_title_font();
1418 class_desc->add_text(TTR("Theme Properties"));
1419 _pop_title_font();
1420
1421 String theme_data_type;
1422 HashMap<String, String> data_type_names;
1423 data_type_names["color"] = TTR("Colors");
1424 data_type_names["constant"] = TTR("Constants");
1425 data_type_names["font"] = TTR("Fonts");
1426 data_type_names["font_size"] = TTR("Font Sizes");
1427 data_type_names["icon"] = TTR("Icons");
1428 data_type_names["style"] = TTR("Styles");
1429
1430 for (const DocData::ThemeItemDoc &theme_item : cd.theme_properties) {
1431 if (theme_data_type != theme_item.data_type) {
1432 theme_data_type = theme_item.data_type;
1433
1434 class_desc->add_newline();
1435 class_desc->add_newline();
1436
1437 class_desc->push_indent(1);
1438 _push_title_font();
1439
1440 if (data_type_names.has(theme_data_type)) {
1441 class_desc->add_text(data_type_names[theme_data_type]);
1442 } else {
1443 class_desc->add_text(theme_data_type);
1444 }
1445
1446 _pop_title_font();
1447 class_desc->pop(); // indent
1448 }
1449
1450 class_desc->add_newline();
1451 class_desc->add_newline();
1452
1453 theme_property_line[theme_item.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
1454
1455 class_desc->push_indent(1);
1456
1457 // Theme item header.
1458 _push_code_font();
1459 _add_bulletpoint();
1460
1461 // Theme item object type.
1462 _add_type(theme_item.type);
1463
1464 // Theme item name.
1465 class_desc->push_color(theme_cache.headline_color);
1466 class_desc->add_text(" ");
1467 class_desc->add_text(theme_item.name);
1468 class_desc->pop(); // color
1469
1470 // Theme item default value.
1471 if (!theme_item.default_value.is_empty()) {
1472 class_desc->push_color(theme_cache.symbol_color);
1473 class_desc->add_text(" [" + TTR("default:") + " ");
1474 class_desc->pop(); // color
1475
1476 class_desc->push_color(theme_cache.value_color);
1477 class_desc->add_text(_fix_constant(theme_item.default_value));
1478 class_desc->pop(); // color
1479
1480 class_desc->push_color(theme_cache.symbol_color);
1481 class_desc->add_text("]");
1482 class_desc->pop(); // color
1483 }
1484
1485 _pop_code_font();
1486
1487 // Theme item description.
1488 class_desc->push_indent(1);
1489 _push_normal_font();
1490 class_desc->push_color(theme_cache.comment_color);
1491
1492 bool has_prev_text = false;
1493
1494 if (theme_item.is_deprecated) {
1495 has_prev_text = true;
1496 DEPRECATED_DOC_MSG(HANDLE_DOC(theme_item.deprecated_message), TTR("This theme property may be changed or removed in future versions."));
1497 }
1498
1499 if (theme_item.is_experimental) {
1500 if (has_prev_text) {
1501 class_desc->add_newline();
1502 class_desc->add_newline();
1503 }
1504 has_prev_text = true;
1505 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(theme_item.experimental_message), TTR("This theme property may be changed or removed in future versions."));
1506 }
1507
1508 const String descr = HANDLE_DOC(theme_item.description);
1509 if (!descr.is_empty()) {
1510 if (has_prev_text) {
1511 class_desc->add_newline();
1512 class_desc->add_newline();
1513 }
1514 has_prev_text = true;
1515 _add_text(descr);
1516 } else if (!has_prev_text) {
1517 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
1518 class_desc->add_text(" ");
1519 class_desc->push_color(theme_cache.comment_color);
1520 if (cd.is_script_doc) {
1521 class_desc->add_text(TTR("There is currently no description for this theme property."));
1522 } else {
1523 class_desc->append_text(TTR("There is currently no description for this theme property. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
1524 }
1525 class_desc->pop(); // color
1526 }
1527
1528 class_desc->pop(); // color
1529 _pop_normal_font();
1530 class_desc->pop(); // indent
1531
1532 class_desc->pop(); // indent
1533 }
1534 }
1535
1536 // Signals
1537 if (!cd.signals.is_empty()) {
1538 if (sort_methods) {
1539 cd.signals.sort();
1540 }
1541
1542 bool header_added = false;
1543
1544 for (const DocData::MethodDoc &signal : cd.signals) {
1545 // Ignore undocumented private.
1546 const bool is_documented = signal.is_deprecated || signal.is_experimental || !signal.description.strip_edges().is_empty();
1547 if (!is_documented && signal.name.begins_with("_")) {
1548 continue;
1549 }
1550
1551 if (!header_added) {
1552 header_added = true;
1553
1554 class_desc->add_newline();
1555 class_desc->add_newline();
1556
1557 section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_paragraph_count() - 2));
1558 _push_title_font();
1559 class_desc->add_text(TTR("Signals"));
1560 _pop_title_font();
1561 }
1562
1563 class_desc->add_newline();
1564 class_desc->add_newline();
1565
1566 signal_line[signal.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
1567
1568 class_desc->push_indent(1);
1569
1570 // Signal header.
1571 _push_code_font();
1572 _add_bulletpoint();
1573 class_desc->push_color(theme_cache.headline_color);
1574 class_desc->add_text(signal.name);
1575 class_desc->pop(); // color
1576
1577 class_desc->push_color(theme_cache.symbol_color);
1578 class_desc->add_text("(");
1579 class_desc->pop(); // color
1580
1581 for (int j = 0; j < signal.arguments.size(); j++) {
1582 const DocData::ArgumentDoc &argument = signal.arguments[j];
1583
1584 if (j > 0) {
1585 class_desc->push_color(theme_cache.symbol_color);
1586 class_desc->add_text(", ");
1587 class_desc->pop(); // color
1588 }
1589
1590 class_desc->push_color(theme_cache.text_color);
1591 class_desc->add_text(argument.name);
1592 class_desc->pop(); // color
1593
1594 class_desc->push_color(theme_cache.symbol_color);
1595 class_desc->add_text(colon_nbsp);
1596 class_desc->pop(); // color
1597
1598 _add_type(argument.type, argument.enumeration, argument.is_bitfield);
1599
1600 // Signals currently do not support default argument values, neither the core nor GDScript.
1601 // This code is just for completeness.
1602 if (!argument.default_value.is_empty()) {
1603 class_desc->push_color(theme_cache.symbol_color);
1604 class_desc->add_text(nbsp_equal_nbsp);
1605 class_desc->pop(); // color
1606
1607 class_desc->push_color(theme_cache.value_color);
1608 class_desc->add_text(_fix_constant(argument.default_value));
1609 class_desc->pop(); // color
1610 }
1611 }
1612
1613 class_desc->push_color(theme_cache.symbol_color);
1614 class_desc->add_text(")");
1615 class_desc->pop(); // color
1616
1617 _pop_code_font();
1618
1619 class_desc->add_newline();
1620
1621 // Signal description.
1622 class_desc->push_indent(1);
1623 _push_normal_font();
1624 class_desc->push_color(theme_cache.comment_color);
1625
1626 const String descr = HANDLE_DOC(signal.description);
1627 const bool is_multiline = descr.find_char('\n') > 0;
1628 bool has_prev_text = false;
1629
1630 if (signal.is_deprecated) {
1631 has_prev_text = true;
1632 DEPRECATED_DOC_MSG(HANDLE_DOC(signal.deprecated_message), TTR("This signal may be changed or removed in future versions."));
1633 }
1634
1635 if (signal.is_experimental) {
1636 if (has_prev_text) {
1637 class_desc->add_newline();
1638 if (is_multiline) {
1639 class_desc->add_newline();
1640 }
1641 }
1642 has_prev_text = true;
1643 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(signal.experimental_message), TTR("This signal may be changed or removed in future versions."));
1644 }
1645
1646 if (!descr.is_empty()) {
1647 if (has_prev_text) {
1648 class_desc->add_newline();
1649 if (is_multiline) {
1650 class_desc->add_newline();
1651 }
1652 }
1653 has_prev_text = true;
1654 _add_text(descr);
1655 } else if (!has_prev_text) {
1656 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
1657 class_desc->add_text(" ");
1658 class_desc->push_color(theme_cache.comment_color);
1659 if (cd.is_script_doc) {
1660 class_desc->add_text(TTR("There is currently no description for this signal."));
1661 } else {
1662 class_desc->append_text(TTR("There is currently no description for this signal. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
1663 }
1664 class_desc->pop(); // color
1665 }
1666
1667 class_desc->pop(); // color
1668 _pop_normal_font();
1669 class_desc->pop(); // indent
1670
1671 class_desc->pop(); // indent
1672 }
1673 }
1674
1675 // Constants and enums
1676 if (!cd.constants.is_empty()) {
1677 HashMap<String, Vector<DocData::ConstantDoc>> enums;
1678 Vector<DocData::ConstantDoc> constants;
1679
1680 for (const DocData::ConstantDoc &constant : cd.constants) {
1681 if (!constant.enumeration.is_empty()) {
1682 if (!enums.has(constant.enumeration)) {
1683 enums[constant.enumeration] = Vector<DocData::ConstantDoc>();
1684 }
1685
1686 enums[constant.enumeration].push_back(constant);
1687 } else {
1688 // Ignore undocumented private.
1689 const bool is_documented = constant.is_deprecated || constant.is_experimental || !constant.description.strip_edges().is_empty();
1690 if (!is_documented && constant.name.begins_with("_")) {
1691 continue;
1692 }
1693 constants.push_back(constant);
1694 }
1695 }
1696
1697 // Enums
1698 bool has_enums = enums.size() && !cd.is_script_doc;
1699 if (enums.size() && !has_enums) {
1700 for (KeyValue<String, DocData::EnumDoc> &E : cd.enums) {
1701 const bool is_documented = E.value.is_deprecated || E.value.is_experimental || !E.value.description.strip_edges().is_empty();
1702 if (!is_documented && E.key.begins_with("_")) {
1703 continue;
1704 }
1705 has_enums = true;
1706 break;
1707 }
1708 }
1709 if (has_enums) {
1710 class_desc->add_newline();
1711 class_desc->add_newline();
1712
1713 section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_paragraph_count() - 2));
1714 _push_title_font();
1715 class_desc->add_text(TTR("Enumerations"));
1716 _pop_title_font();
1717
1718 for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
1719 String key = E.key;
1720 if ((key.get_slice_count(".") > 1) && (key.get_slicec('.', 0) == edited_class)) {
1721 key = key.get_slicec('.', 1);
1722 }
1723 if (cd.enums.has(key)) {
1724 const bool is_documented = cd.enums[key].is_deprecated || cd.enums[key].is_experimental || !cd.enums[key].description.strip_edges().is_empty();
1725 if (!is_documented && cd.is_script_doc && E.key.begins_with("_")) {
1726 continue;
1727 }
1728 }
1729
1730 class_desc->add_newline();
1731 class_desc->add_newline();
1732
1733 // Enum header.
1734 _push_code_font();
1735
1736 enum_line[E.key] = class_desc->get_paragraph_count() - 2;
1737 class_desc->push_color(theme_cache.title_color);
1738 if (E.value.size() && E.value[0].is_bitfield) {
1739 class_desc->add_text("flags ");
1740 } else {
1741 class_desc->add_text("enum ");
1742 }
1743 class_desc->pop(); // color
1744
1745 class_desc->push_color(theme_cache.headline_color);
1746 class_desc->add_text(key);
1747 class_desc->pop(); // color
1748
1749 class_desc->push_color(theme_cache.symbol_color);
1750 class_desc->add_text(":");
1751 class_desc->pop(); // color
1752
1753 _pop_code_font();
1754
1755 // Enum description.
1756 if (key != "@unnamed_enums" && cd.enums.has(key)) {
1757 const String descr = HANDLE_DOC(cd.enums[key].description);
1758 const bool is_multiline = descr.find_char('\n') > 0;
1759 if (cd.enums[key].is_deprecated || cd.enums[key].is_experimental || !descr.is_empty()) {
1760 class_desc->add_newline();
1761
1762 class_desc->push_indent(1);
1763 _push_normal_font();
1764 class_desc->push_color(theme_cache.text_color);
1765
1766 bool has_prev_text = false;
1767
1768 if (cd.enums[key].is_deprecated) {
1769 has_prev_text = true;
1770 DEPRECATED_DOC_MSG(HANDLE_DOC(cd.enums[key].deprecated_message), TTR("This enumeration may be changed or removed in future versions."));
1771 }
1772
1773 if (cd.enums[key].is_experimental) {
1774 if (has_prev_text) {
1775 class_desc->add_newline();
1776 if (is_multiline) {
1777 class_desc->add_newline();
1778 }
1779 }
1780 has_prev_text = true;
1781 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(cd.enums[key].experimental_message), TTR("This enumeration may be changed or removed in future versions."));
1782 }
1783
1784 if (!descr.is_empty()) {
1785 if (has_prev_text) {
1786 class_desc->add_newline();
1787 if (is_multiline) {
1788 class_desc->add_newline();
1789 }
1790 }
1791 has_prev_text = true;
1792 _add_text(descr);
1793 }
1794
1795 class_desc->pop(); // color
1796 _pop_normal_font();
1797 class_desc->pop(); // indent
1798 }
1799 }
1800
1801 HashMap<String, int> enum_values;
1802 const int enum_start_line = enum_line[E.key];
1803
1804 bool prev_is_multiline = true; // Use a large margin for the first item.
1805 for (const DocData::ConstantDoc &enum_value : E.value) {
1806 const String descr = HANDLE_DOC(enum_value.description);
1807 const bool is_multiline = descr.find_char('\n') > 0;
1808
1809 class_desc->add_newline();
1810 if (prev_is_multiline || is_multiline) {
1811 class_desc->add_newline();
1812 }
1813 prev_is_multiline = is_multiline;
1814
1815 if (cd.name == "@GlobalScope") {
1816 enum_values[enum_value.name] = enum_start_line;
1817 }
1818
1819 // Add the enum constant line to the constant_line map so we can locate it as a constant.
1820 constant_line[enum_value.name] = class_desc->get_paragraph_count() - 2;
1821
1822 class_desc->push_indent(1);
1823
1824 // Enum value header.
1825 _push_code_font();
1826 _add_bulletpoint();
1827
1828 class_desc->push_color(theme_cache.headline_color);
1829 class_desc->add_text(enum_value.name);
1830 class_desc->pop(); // color
1831
1832 class_desc->push_color(theme_cache.symbol_color);
1833 class_desc->add_text(nbsp_equal_nbsp);
1834 class_desc->pop(); // color
1835
1836 class_desc->push_color(theme_cache.value_color);
1837 class_desc->add_text(_fix_constant(enum_value.value));
1838 class_desc->pop(); // color
1839
1840 _pop_code_font();
1841
1842 // Enum value description.
1843 if (enum_value.is_deprecated || enum_value.is_experimental || !descr.is_empty()) {
1844 class_desc->add_newline();
1845
1846 class_desc->push_indent(1);
1847 _push_normal_font();
1848 class_desc->push_color(theme_cache.comment_color);
1849
1850 bool has_prev_text = false;
1851
1852 if (enum_value.is_deprecated) {
1853 has_prev_text = true;
1854 DEPRECATED_DOC_MSG(HANDLE_DOC(enum_value.deprecated_message), TTR("This constant may be changed or removed in future versions."));
1855 }
1856
1857 if (enum_value.is_experimental) {
1858 if (has_prev_text) {
1859 class_desc->add_newline();
1860 if (is_multiline) {
1861 class_desc->add_newline();
1862 }
1863 }
1864 has_prev_text = true;
1865 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(enum_value.experimental_message), TTR("This constant may be changed or removed in future versions."));
1866 }
1867
1868 if (!descr.is_empty()) {
1869 if (has_prev_text) {
1870 class_desc->add_newline();
1871 if (is_multiline) {
1872 class_desc->add_newline();
1873 }
1874 }
1875 has_prev_text = true;
1876 _add_text(descr);
1877 }
1878
1879 class_desc->pop(); // color
1880 _pop_normal_font();
1881 class_desc->pop(); // indent
1882 }
1883
1884 class_desc->pop(); // indent
1885 }
1886
1887 if (cd.name == "@GlobalScope") {
1888 enum_values_line[E.key] = enum_values;
1889 }
1890 }
1891 }
1892
1893 // Constants
1894 if (!constants.is_empty()) {
1895 class_desc->add_newline();
1896 class_desc->add_newline();
1897
1898 section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_paragraph_count() - 2));
1899 _push_title_font();
1900 class_desc->add_text(TTR("Constants"));
1901 _pop_title_font();
1902
1903 bool prev_is_multiline = true; // Use a large margin for the first item.
1904 for (const DocData::ConstantDoc &constant : constants) {
1905 const String descr = HANDLE_DOC(constant.description);
1906 const bool is_multiline = descr.find_char('\n') > 0;
1907
1908 class_desc->add_newline();
1909 if (prev_is_multiline || is_multiline) {
1910 class_desc->add_newline();
1911 }
1912 prev_is_multiline = is_multiline;
1913
1914 constant_line[constant.name] = class_desc->get_paragraph_count() - 2;
1915
1916 class_desc->push_indent(1);
1917
1918 // Constant header.
1919 _push_code_font();
1920
1921 if (constant.value.begins_with("Color(") && constant.value.ends_with(")")) {
1922 String stripped = constant.value.remove_char(' ').replace("Color(", "").remove_char(')');
1923 PackedFloat64Array color = stripped.split_floats(",");
1924 if (color.size() >= 3) {
1925 class_desc->push_color(Color(color[0], color[1], color[2]));
1926 _add_bulletpoint();
1927 class_desc->pop(); // color
1928 }
1929 } else {
1930 _add_bulletpoint();
1931 }
1932
1933 class_desc->push_color(theme_cache.headline_color);
1934 class_desc->add_text(constant.name);
1935 class_desc->pop(); // color
1936
1937 class_desc->push_color(theme_cache.symbol_color);
1938 class_desc->add_text(nbsp_equal_nbsp);
1939 class_desc->pop(); // color
1940
1941 class_desc->push_color(theme_cache.value_color);
1942 class_desc->add_text(_fix_constant(constant.value));
1943 class_desc->pop(); // color
1944
1945 _pop_code_font();
1946
1947 // Constant description.
1948 if (constant.is_deprecated || constant.is_experimental || !descr.is_empty()) {
1949 class_desc->add_newline();
1950
1951 class_desc->push_indent(1);
1952 _push_normal_font();
1953 class_desc->push_color(theme_cache.comment_color);
1954
1955 bool has_prev_text = false;
1956
1957 if (constant.is_deprecated) {
1958 has_prev_text = true;
1959 DEPRECATED_DOC_MSG(HANDLE_DOC(constant.deprecated_message), TTR("This constant may be changed or removed in future versions."));
1960 }
1961
1962 if (constant.is_experimental) {
1963 if (has_prev_text) {
1964 class_desc->add_newline();
1965 if (is_multiline) {
1966 class_desc->add_newline();
1967 }
1968 }
1969 has_prev_text = true;
1970 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(constant.experimental_message), TTR("This constant may be changed or removed in future versions."));
1971 }
1972
1973 if (!descr.is_empty()) {
1974 if (has_prev_text) {
1975 class_desc->add_newline();
1976 if (is_multiline) {
1977 class_desc->add_newline();
1978 }
1979 }
1980 has_prev_text = true;
1981 _add_text(descr);
1982 }
1983
1984 class_desc->pop(); // color
1985 _pop_normal_font();
1986 class_desc->pop(); // indent
1987 }
1988
1989 class_desc->pop(); // indent
1990 }
1991 }
1992 }
1993
1994 // Annotations
1995 if (!cd.annotations.is_empty()) {
1996 if (sort_methods) {
1997 cd.annotations.sort();
1998 }
1999
2000 class_desc->add_newline();
2001 class_desc->add_newline();
2002
2003 section_line.push_back(Pair<String, int>(TTR("Annotations"), class_desc->get_paragraph_count() - 2));
2004 _push_title_font();
2005 class_desc->add_text(TTR("Annotations"));
2006 _pop_title_font();
2007
2008 for (const DocData::MethodDoc &annotation : cd.annotations) {
2009 class_desc->add_newline();
2010 class_desc->add_newline();
2011
2012 annotation_line[annotation.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
2013
2014 class_desc->push_indent(1);
2015
2016 // Annotation header.
2017 _push_code_font();
2018 _add_bulletpoint();
2019
2020 class_desc->push_color(theme_cache.headline_color);
2021 class_desc->add_text(annotation.name);
2022 class_desc->pop(); // color
2023
2024 if (!annotation.arguments.is_empty()) {
2025 class_desc->push_color(theme_cache.symbol_color);
2026 class_desc->add_text("(");
2027 class_desc->pop(); // color
2028
2029 for (int j = 0; j < annotation.arguments.size(); j++) {
2030 const DocData::ArgumentDoc &argument = annotation.arguments[j];
2031
2032 if (j > 0) {
2033 class_desc->push_color(theme_cache.symbol_color);
2034 class_desc->add_text(", ");
2035 class_desc->pop(); // color
2036 }
2037
2038 class_desc->push_color(theme_cache.text_color);
2039 class_desc->add_text(argument.name);
2040 class_desc->pop(); // color
2041
2042 class_desc->push_color(theme_cache.symbol_color);
2043 class_desc->add_text(colon_nbsp);
2044 class_desc->pop(); // color
2045
2046 _add_type(argument.type, argument.enumeration, argument.is_bitfield);
2047
2048 if (!argument.default_value.is_empty()) {
2049 class_desc->push_color(theme_cache.symbol_color);
2050 class_desc->add_text(nbsp_equal_nbsp);
2051 class_desc->pop(); // color
2052
2053 class_desc->push_color(theme_cache.value_color);
2054 class_desc->add_text(_fix_constant(argument.default_value));
2055 class_desc->pop(); // color
2056 }
2057 }
2058
2059 if (annotation.qualifiers.contains("vararg")) {
2060 if (!annotation.arguments.is_empty()) {
2061 class_desc->push_color(theme_cache.symbol_color);
2062 class_desc->add_text(", ");
2063 class_desc->pop(); // color
2064 }
2065
2066 class_desc->push_color(theme_cache.symbol_color);
2067 class_desc->add_text("...");
2068 class_desc->pop(); // color
2069
2070 const DocData::ArgumentDoc &rest_argument = annotation.rest_argument;
2071
2072 class_desc->push_color(theme_cache.text_color);
2073 class_desc->add_text(rest_argument.name.is_empty() ? "args" : rest_argument.name);
2074 class_desc->pop(); // color
2075
2076 class_desc->push_color(theme_cache.symbol_color);
2077 class_desc->add_text(colon_nbsp);
2078 class_desc->pop(); // color
2079
2080 if (rest_argument.type.is_empty()) {
2081 _add_type("Array");
2082 } else {
2083 _add_type(rest_argument.type, rest_argument.enumeration, rest_argument.is_bitfield);
2084 }
2085 }
2086
2087 class_desc->push_color(theme_cache.symbol_color);
2088 class_desc->add_text(")");
2089 class_desc->pop(); // color
2090 }
2091
2092 if (!annotation.qualifiers.is_empty()) {
2093 class_desc->push_color(theme_cache.qualifier_color);
2094 _add_qualifiers_to_rt(annotation.qualifiers, class_desc);
2095 class_desc->pop(); // color
2096 }
2097
2098 _pop_code_font();
2099
2100 class_desc->add_newline();
2101
2102 // Annotation description.
2103 class_desc->push_indent(1);
2104 _push_normal_font();
2105 class_desc->push_color(theme_cache.comment_color);
2106
2107 const String descr = HANDLE_DOC(annotation.description);
2108 if (!descr.is_empty()) {
2109 _add_text(descr);
2110 } else {
2111 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
2112 class_desc->add_text(" ");
2113 class_desc->push_color(theme_cache.comment_color);
2114 if (cd.is_script_doc) {
2115 class_desc->add_text(TTR("There is currently no description for this annotation."));
2116 } else {
2117 class_desc->append_text(TTR("There is currently no description for this annotation. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
2118 }
2119 class_desc->pop(); // color
2120 }
2121
2122 class_desc->pop(); // color
2123 _pop_normal_font();
2124 class_desc->pop(); // indent
2125
2126 class_desc->pop(); // indent
2127 }
2128 }
2129
2130 // Property descriptions
2131 if (has_property_descriptions) {
2132 class_desc->add_newline();
2133 class_desc->add_newline();
2134 class_desc->add_newline();
2135
2136 section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_paragraph_count() - 2));
2137 _push_title_font();
2138 class_desc->add_text(TTR("Property Descriptions"));
2139 _pop_title_font();
2140
2141 for (const DocData::PropertyDoc &prop : cd.properties) {
2142 if (prop.overridden) {
2143 continue;
2144 }
2145 // Ignore undocumented private.
2146 const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
2147 if (!is_documented && prop.name.begins_with("_")) {
2148 continue;
2149 }
2150
2151 class_desc->add_newline();
2152 class_desc->add_newline();
2153 class_desc->add_newline();
2154
2155 property_line[prop.name] = class_desc->get_paragraph_count() - 2;
2156
2157 class_desc->push_table(2);
2158 class_desc->set_table_column_expand(1, true);
2159
2160 class_desc->push_cell();
2161 _push_code_font();
2162 _add_bulletpoint();
2163 _add_type(prop.type, prop.enumeration, prop.is_bitfield);
2164 _pop_code_font();
2165 class_desc->pop(); // cell
2166
2167 class_desc->push_cell();
2168 _push_code_font();
2169
2170 class_desc->push_color(theme_cache.headline_color);
2171 class_desc->add_text(prop.name);
2172 class_desc->pop(); // color
2173
2174 if (!prop.default_value.is_empty()) {
2175 class_desc->push_color(theme_cache.symbol_color);
2176 class_desc->add_text(" [" + TTR("default:") + " ");
2177 class_desc->pop(); // color
2178
2179 class_desc->push_color(theme_cache.value_color);
2180 class_desc->add_text(_fix_constant(prop.default_value));
2181 class_desc->pop(); // color
2182
2183 class_desc->push_color(theme_cache.symbol_color);
2184 class_desc->add_text("]");
2185 class_desc->pop(); // color
2186 }
2187
2188 if (cd.is_script_doc && (!prop.setter.is_empty() || !prop.getter.is_empty())) {
2189 class_desc->push_color(theme_cache.symbol_color);
2190 class_desc->add_text(" [" + TTR("property:") + " ");
2191 class_desc->pop(); // color
2192
2193 if (!prop.setter.is_empty()) {
2194 class_desc->push_color(theme_cache.value_color);
2195 class_desc->add_text("setter");
2196 class_desc->pop(); // color
2197 }
2198 if (!prop.getter.is_empty()) {
2199 if (!prop.setter.is_empty()) {
2200 class_desc->push_color(theme_cache.symbol_color);
2201 class_desc->add_text(", ");
2202 class_desc->pop(); // color
2203 }
2204 class_desc->push_color(theme_cache.value_color);
2205 class_desc->add_text("getter");
2206 class_desc->pop(); // color
2207 }
2208
2209 class_desc->push_color(theme_cache.symbol_color);
2210 class_desc->add_text("]");
2211 class_desc->pop(); // color
2212 }
2213
2214 _pop_code_font();
2215 class_desc->pop(); // cell
2216
2217 // Script doc doesn't have setter, getter.
2218 if (!cd.is_script_doc) {
2219 HashMap<String, DocData::MethodDoc> method_map;
2220 for (int j = 0; j < methods.size(); j++) {
2221 method_map[methods[j].name] = methods[j];
2222 }
2223
2224 if (!prop.setter.is_empty()) {
2225 class_desc->push_cell();
2226 class_desc->pop(); // cell
2227
2228 class_desc->push_cell();
2229 _push_code_font();
2230 class_desc->push_color(theme_cache.text_color);
2231
2232 if (method_map[prop.setter].arguments.size() > 1) {
2233 // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
2234 class_desc->push_meta("@method " + prop.setter);
2235 class_desc->add_text(prop.setter + TTR("(value)"));
2236 class_desc->pop(); // meta
2237 } else {
2238 class_desc->add_text(prop.setter + TTR("(value)"));
2239 }
2240
2241 class_desc->pop(); // color
2242 class_desc->push_color(theme_cache.comment_color);
2243 class_desc->add_text(" setter");
2244 class_desc->pop(); // color
2245 _pop_code_font();
2246 class_desc->pop(); // cell
2247
2248 method_line[prop.setter] = property_line[prop.name];
2249 }
2250
2251 if (!prop.getter.is_empty()) {
2252 class_desc->push_cell();
2253 class_desc->pop(); // cell
2254
2255 class_desc->push_cell();
2256 _push_code_font();
2257 class_desc->push_color(theme_cache.text_color);
2258
2259 if (!method_map[prop.getter].arguments.is_empty()) {
2260 // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
2261 class_desc->push_meta("@method " + prop.getter);
2262 class_desc->add_text(prop.getter + "()");
2263 class_desc->pop(); // meta
2264 } else {
2265 class_desc->add_text(prop.getter + "()");
2266 }
2267
2268 class_desc->pop(); // color
2269 class_desc->push_color(theme_cache.comment_color);
2270 class_desc->add_text(" getter");
2271 class_desc->pop(); // color
2272 _pop_code_font();
2273 class_desc->pop(); // cell
2274
2275 method_line[prop.getter] = property_line[prop.name];
2276 }
2277 }
2278
2279 class_desc->pop(); // table
2280
2281 class_desc->add_newline();
2282 class_desc->add_newline();
2283
2284 class_desc->push_indent(1);
2285 _push_normal_font();
2286 class_desc->push_color(theme_cache.text_color);
2287
2288 bool has_prev_text = false;
2289
2290 if (prop.is_deprecated) {
2291 has_prev_text = true;
2292 DEPRECATED_DOC_MSG(HANDLE_DOC(prop.deprecated_message), TTR("This property may be changed or removed in future versions."));
2293 }
2294
2295 if (prop.is_experimental) {
2296 if (has_prev_text) {
2297 class_desc->add_newline();
2298 class_desc->add_newline();
2299 }
2300 has_prev_text = true;
2301 EXPERIMENTAL_DOC_MSG(HANDLE_DOC(prop.experimental_message), TTR("This property may be changed or removed in future versions."));
2302 }
2303
2304 const String descr = HANDLE_DOC(prop.description);
2305 if (!descr.is_empty()) {
2306 if (has_prev_text) {
2307 class_desc->add_newline();
2308 class_desc->add_newline();
2309 }
2310 has_prev_text = true;
2311 _add_text(descr);
2312 } else if (!has_prev_text) {
2313 class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
2314 class_desc->add_text(" ");
2315 class_desc->push_color(theme_cache.comment_color);
2316 if (cd.is_script_doc) {
2317 class_desc->add_text(TTR("There is currently no description for this property."));
2318 } else {
2319 class_desc->append_text(TTR("There is currently no description for this property. Please help us by [color=$color][url=$url]contributing one[/url][/color]!").replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text));
2320 }
2321 class_desc->pop(); // color
2322 }
2323
2324 // Add copy note to built-in properties returning `Packed*Array`.
2325 if (!cd.is_script_doc && packed_array_types.has(prop.type)) {
2326 class_desc->add_newline();
2327 class_desc->add_newline();
2328 // See also `EditorHelpBit::parse_symbol()` and `doc/tools/make_rst.py`.
2329 _add_text(vformat(TTR("[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [%s] for more details."), prop.type));
2330 }
2331
2332 class_desc->pop(); // color
2333 _pop_normal_font();
2334 class_desc->pop(); // indent
2335 }
2336 }
2337
2338 // Constructor descriptions
2339 if (!cd.constructors.is_empty()) {
2340 _update_method_descriptions(cd, METHOD_TYPE_CONSTRUCTOR, cd.constructors);
2341 }
2342
2343 // Method descriptions
2344 if (!methods.is_empty()) {
2345 _update_method_descriptions(cd, METHOD_TYPE_METHOD, methods);
2346 }
2347
2348 // Operator descriptions
2349 if (!cd.operators.is_empty()) {
2350 _update_method_descriptions(cd, METHOD_TYPE_OPERATOR, cd.operators);
2351 }
2352
2353 // Allow the document to be scrolled slightly below the end.
2354 class_desc->add_newline();
2355 class_desc->add_newline();
2356
2357 // Free the scroll.
2358 scroll_locked = false;
2359
2360 #undef HANDLE_DOC
2361 }
2362
2363 void EditorHelp::_request_help(const String &p_string) {
2364 Error err = _goto_desc(p_string);
2365 if (err == OK) {
2366 EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
2367 }
2368 }
2369
2370 void EditorHelp::_help_callback(const String &p_topic) {
2371 Vector<String> parts;
2372 {
2373 int from = 0;
2374 int buffer_start = 0;
2375 while (true) {
2376 const int pos = p_topic.find_char(':', from);
2377 if (pos < 0) {
2378 parts.push_back(p_topic.substr(buffer_start));
2379 break;
2380 }
2381
2382 if (pos + 1 < p_topic.length() && p_topic[pos + 1] == ':') {
2383 // `::` used in built-in scripts.
2384 from = pos + 2;
2385 } else {
2386 parts.push_back(p_topic.substr(buffer_start, pos - buffer_start));
2387 from = pos + 1;
2388 buffer_start = from;
2389 }
2390 }
2391 }
2392
2393 const String what = parts[0]; // `parts` is always non-empty.
2394 const String clss = (parts.size() > 1) ? parts[1] : String();
2395 const String name = (parts.size() > 2) ? parts[2] : String();
2396
2397 _request_help(clss); // First go to class.
2398
2399 int line = 0;
2400
2401 if (what == "class_desc") {
2402 line = description_line;
2403 } else if (what == "class_signal") {
2404 if (signal_line.has(name)) {
2405 line = signal_line[name];
2406 }
2407 } else if (what == "class_method" || what == "class_method_desc") {
2408 if (method_line.has(name)) {
2409 line = method_line[name];
2410 }
2411 } else if (what == "class_property") {
2412 if (property_line.has(name)) {
2413 line = property_line[name];
2414 }
2415 } else if (what == "class_enum") {
2416 if (enum_line.has(name)) {
2417 line = enum_line[name];
2418 }
2419 } else if (what == "class_theme_item") {
2420 if (theme_property_line.has(name)) {
2421 line = theme_property_line[name];
2422 }
2423 } else if (what == "class_constant") {
2424 if (constant_line.has(name)) {
2425 line = constant_line[name];
2426 }
2427 } else if (what == "class_annotation") {
2428 if (annotation_line.has(name)) {
2429 line = annotation_line[name];
2430 }
2431 } else if (what == "class_global") { // Deprecated.
2432 if (constant_line.has(name)) {
2433 line = constant_line[name];
2434 } else if (method_line.has(name)) {
2435 line = method_line[name];
2436 } else {
2437 HashMap<String, HashMap<String, int>>::Iterator iter = enum_values_line.begin();
2438 while (true) {
2439 if (iter->value.has(name)) {
2440 line = iter->value[name];
2441 break;
2442 } else if (iter == enum_values_line.last()) {
2443 break;
2444 } else {
2445 ++iter;
2446 }
2447 }
2448 }
2449 }
2450
2451 if (class_desc->is_finished()) {
2452 class_desc->scroll_to_paragraph(line);
2453 } else {
2454 scroll_to = line;
2455 }
2456 }
2457
2458 static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class) {
2459 bool is_native = false;
2460 {
2461 const DocData::ClassDoc *E = EditorHelp::get_doc(p_class);
2462 if (E && !E->is_script_doc) {
2463 is_native = true;
2464 }
2465 }
2466
2467 const bool using_space_indent = int(EDITOR_GET("text_editor/behavior/indent/type")) == 1;
2468 const int indent_size = MAX(1, int(EDITOR_GET("text_editor/behavior/indent/size")));
2469
2470 const Ref<Font> doc_font = p_owner_node->get_theme_font(SNAME("doc"), EditorStringName(EditorFonts));
2471 const Ref<Font> doc_bold_font = p_owner_node->get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
2472 const Ref<Font> doc_italic_font = p_owner_node->get_theme_font(SNAME("doc_italic"), EditorStringName(EditorFonts));
2473 const Ref<Font> doc_code_font = p_owner_node->get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
2474 const Ref<Font> doc_kbd_font = p_owner_node->get_theme_font(SNAME("doc_keyboard"), EditorStringName(EditorFonts));
2475
2476 const int doc_font_size = p_owner_node->get_theme_font_size(SNAME("doc_size"), EditorStringName(EditorFonts));
2477 const int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
2478 const int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
2479
2480 const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
2481 const Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
2482 const Color kbd_color = p_owner_node->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));
2483 const Color code_dark_color = Color(code_color, 0.8);
2484
2485 const Color link_color = p_owner_node->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));
2486 const Color link_method_color = p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
2487 const Color link_property_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)), 0.25);
2488 const Color link_annotation_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)), 0.5);
2489
2490 const Color code_bg_color = p_owner_node->get_theme_color(SNAME("code_bg_color"), SNAME("EditorHelp"));
2491 const Color kbd_bg_color = p_owner_node->get_theme_color(SNAME("kbd_bg_color"), SNAME("EditorHelp"));
2492 const Color param_bg_color = p_owner_node->get_theme_color(SNAME("param_bg_color"), SNAME("EditorHelp"));
2493
2494 String bbcode = p_bbcode.dedent().remove_chars("\r").strip_edges();
2495
2496 // Select the correct code examples.
2497 switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
2498 case 0: // GDScript
2499 bbcode = bbcode.replace("[gdscript", "[codeblock lang=gdscript"); // Tag can have extra arguments.
2500 bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
2501
2502 for (int pos = bbcode.find("[csharp"); pos != -1; pos = bbcode.find("[csharp")) {
2503 int end_pos = bbcode.find("[/csharp]");
2504 if (end_pos == -1) {
2505 WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
2506 break;
2507 }
2508
2509 bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 9); // 9 is length of "[/csharp]".
2510 while (bbcode[pos] == '\n') {
2511 bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
2512 }
2513 }
2514 break;
2515 case 1: // C#
2516 bbcode = bbcode.replace("[csharp", "[codeblock lang=csharp"); // Tag can have extra arguments.
2517 bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
2518
2519 for (int pos = bbcode.find("[gdscript"); pos != -1; pos = bbcode.find("[gdscript")) {
2520 int end_pos = bbcode.find("[/gdscript]");
2521 if (end_pos == -1) {
2522 WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
2523 break;
2524 }
2525
2526 bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 11); // 11 is length of "[/gdscript]".
2527 while (bbcode[pos] == '\n') {
2528 bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
2529 }
2530 }
2531 break;
2532 case 2: // GDScript and C#
2533 bbcode = bbcode.replace("[csharp", "[b]C#:[/b]\n[codeblock lang=csharp"); // Tag can have extra arguments.
2534 bbcode = bbcode.replace("[gdscript", "[b]GDScript:[/b]\n[codeblock lang=gdscript"); // Tag can have extra arguments.
2535
2536 bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
2537 bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
2538 break;
2539 }
2540
2541 // Remove codeblocks (they would be printed otherwise).
2542 bbcode = bbcode.replace("[codeblocks]\n", "");
2543 bbcode = bbcode.replace("\n[/codeblocks]", "");
2544 bbcode = bbcode.replace("[codeblocks]", "");
2545 bbcode = bbcode.replace("[/codeblocks]", "");
2546
2547 // Remove `\n` here because `\n` is replaced by `\n\n` later.
2548 // Will be compensated when parsing `[/codeblock]`.
2549 bbcode = bbcode.replace("[/codeblock]\n", "[/codeblock]");
2550
2551 List<String> tag_stack;
2552
2553 int pos = 0;
2554 while (pos < bbcode.length()) {
2555 int brk_pos = bbcode.find_char('[', pos);
2556
2557 if (brk_pos < 0) {
2558 brk_pos = bbcode.length();
2559 }
2560
2561 if (brk_pos > pos) {
2562 p_rt->add_text(bbcode.substr(pos, brk_pos - pos).replace("\n", "\n\n"));
2563 }
2564
2565 if (brk_pos == bbcode.length()) {
2566 break; // Nothing else to add.
2567 }
2568
2569 int brk_end = bbcode.find_char(']', brk_pos + 1);
2570
2571 if (brk_end == -1) {
2572 p_rt->add_text(bbcode.substr(brk_pos).replace("\n", "\n\n"));
2573 break;
2574 }
2575
2576 const String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
2577
2578 if (tag.begins_with("/")) {
2579 bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1);
2580
2581 if (!tag_ok) {
2582 p_rt->add_text("[");
2583 pos = brk_pos + 1;
2584 continue;
2585 }
2586
2587 tag_stack.pop_front();
2588 pos = brk_end + 1;
2589 if (tag == "/img") {
2590 // Nothing to do.
2591 } else if (tag == "/url") {
2592 p_rt->pop(); // meta
2593 p_rt->pop(); // color
2594 p_rt->add_text(nbsp);
2595 p_rt->add_image(p_owner_node->get_editor_theme_icon(SNAME("ExternalLink")), 0, doc_font_size, link_color);
2596 } else {
2597 p_rt->pop();
2598 }
2599 } else if (tag.begins_with("method ") || tag.begins_with("constructor ") || tag.begins_with("operator ") || tag.begins_with("member ") || tag.begins_with("signal ") || tag.begins_with("enum ") || tag.begins_with("constant ") || tag.begins_with("annotation ") || tag.begins_with("theme_item ")) {
2600 const int tag_end = tag.find_char(' ');
2601 const String link_tag = tag.left(tag_end);
2602 const String link_target = tag.substr(tag_end + 1).lstrip(" ");
2603
2604 Color target_color = link_color;
2605 RichTextLabel::MetaUnderline underline_mode = RichTextLabel::META_UNDERLINE_ON_HOVER;
2606 if (link_tag == "method" || link_tag == "constructor" || link_tag == "operator") {
2607 target_color = link_method_color;
2608 } else if (link_tag == "member" || link_tag == "signal" || link_tag == "theme_item") {
2609 target_color = link_property_color;
2610 } else if (link_tag == "annotation") {
2611 target_color = link_annotation_color;
2612 } else {
2613 // Better visibility for constants, enums, etc.
2614 underline_mode = RichTextLabel::META_UNDERLINE_ALWAYS;
2615 }
2616
2617 // Use monospace font to make clickable references
2618 // easier to distinguish from inline code and other text.
2619 p_rt->push_font(doc_code_font);
2620 p_rt->push_font_size(doc_code_font_size);
2621 p_rt->push_color(target_color);
2622 p_rt->push_meta("@" + link_tag + " " + link_target, underline_mode);
2623
2624 if (link_tag == "member" &&
2625 ((!link_target.contains_char('.') && (p_class == "ProjectSettings" || p_class == "EditorSettings")) ||
2626 link_target.begins_with("ProjectSettings.") || link_target.begins_with("EditorSettings."))) {
2627 // Special formatting for both ProjectSettings and EditorSettings.
2628 String prefix;
2629 if (link_target.begins_with("EditorSettings.")) {
2630 prefix = "(" + TTR("Editor") + ") ";
2631 }
2632
2633 const String setting_name = link_target.trim_prefix("ProjectSettings.").trim_prefix("EditorSettings.");
2634 PackedStringArray setting_sections;
2635 for (const String &section : setting_name.split("/", false)) {
2636 setting_sections.append(EditorPropertyNameProcessor::get_singleton()->process_name(section, EditorPropertyNameProcessor::get_settings_style()));
2637 }
2638
2639 p_rt->push_bold();
2640 p_rt->add_text(prefix + String(" > ").join(setting_sections));
2641 p_rt->pop(); // bold
2642 } else {
2643 p_rt->add_text(link_target + (link_tag == "method" ? "()" : ""));
2644 }
2645
2646 p_rt->pop(); // meta
2647 p_rt->pop(); // color
2648 p_rt->pop(); // font_size
2649 p_rt->pop(); // font
2650
2651 pos = brk_end + 1;
2652 } else if (tag.begins_with("param ")) {
2653 const int tag_end = tag.find_char(' ');
2654 const String param_name = tag.substr(tag_end + 1).lstrip(" ");
2655
2656 // Use monospace font with translucent background color to make code easier to distinguish from other text.
2657 p_rt->push_font(doc_code_font);
2658 p_rt->push_font_size(doc_code_font_size);
2659 p_rt->push_bgcolor(param_bg_color);
2660 p_rt->push_color(code_color);
2661
2662 p_rt->add_text(param_name);
2663
2664 p_rt->pop(); // color
2665 p_rt->pop(); // bgcolor
2666 p_rt->pop(); // font_size
2667 p_rt->pop(); // font
2668
2669 pos = brk_end + 1;
2670 } else if (tag == p_class) {
2671 // Use a bold font when class reference tags are in their own page.
2672 p_rt->push_font(doc_bold_font);
2673 p_rt->add_text(tag);
2674 p_rt->pop(); // font
2675
2676 pos = brk_end + 1;
2677 } else if (EditorHelp::has_doc(tag)) {
2678 // Use a monospace font for class reference tags such as [Node2D] or [SceneTree].
2679
2680 p_rt->push_font(doc_code_font);
2681 p_rt->push_font_size(doc_code_font_size);
2682 p_rt->push_color(type_color);
2683 p_rt->push_meta("#" + tag, RichTextLabel::META_UNDERLINE_ON_HOVER);
2684
2685 p_rt->add_text(tag);
2686
2687 p_rt->pop(); // meta
2688 p_rt->pop(); // color
2689 p_rt->pop(); // font_size
2690 p_rt->pop(); // font
2691
2692 pos = brk_end + 1;
2693 } else if (tag == "b") {
2694 // Use bold font.
2695 p_rt->push_font(doc_bold_font);
2696
2697 pos = brk_end + 1;
2698 tag_stack.push_front(tag);
2699 } else if (tag == "i") {
2700 // Use italics font.
2701 p_rt->push_font(doc_italic_font);
2702
2703 pos = brk_end + 1;
2704 tag_stack.push_front(tag);
2705 } else if (tag == "code" || tag.begins_with("code ")) {
2706 int end_pos = bbcode.find("[/code]", brk_end + 1);
2707 if (end_pos < 0) {
2708 end_pos = bbcode.length();
2709 }
2710
2711 // Use monospace font with darkened background color to make code easier to distinguish from other text.
2712 p_rt->push_font(doc_code_font);
2713 p_rt->push_font_size(doc_code_font_size);
2714 p_rt->push_bgcolor(code_bg_color);
2715 p_rt->push_color(code_color.lerp(p_owner_node->get_theme_color(SNAME("error_color"), EditorStringName(Editor)), 0.6));
2716
2717 p_rt->add_text(bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)));
2718
2719 p_rt->pop(); // color
2720 p_rt->pop(); // bgcolor
2721 p_rt->pop(); // font_size
2722 p_rt->pop(); // font
2723
2724 pos = end_pos + 7; // `len("[/code]")`.
2725 } else if (tag == "codeblock" || tag.begins_with("codeblock ")) {
2726 int end_pos = bbcode.find("[/codeblock]", brk_end + 1);
2727 if (end_pos < 0) {
2728 end_pos = bbcode.length();
2729 }
2730
2731 const String codeblock_text = bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)).strip_edges();
2732
2733 String codeblock_copy_text = codeblock_text;
2734 if (using_space_indent) {
2735 // Replace the code block's tab indentation with spaces.
2736 StringBuilder builder;
2737 PackedStringArray text_lines = codeblock_copy_text.split("\n");
2738 for (const String &line : text_lines) {
2739 const String stripped_line = line.dedent();
2740 const int tab_count = line.length() - stripped_line.length();
2741
2742 if (builder.num_strings_appended() > 0) {
2743 builder.append("\n");
2744 }
2745 if (tab_count > 0) {
2746 builder.append(String(" ").repeat(tab_count * indent_size) + stripped_line);
2747 } else {
2748 builder.append(line);
2749 }
2750 }
2751 codeblock_copy_text = builder.as_string();
2752 }
2753
2754 String lang;
2755 const PackedStringArray args = tag.trim_prefix("codeblock").split(" ", false);
2756 for (int i = args.size() - 1; i >= 0; i--) {
2757 if (args[i].begins_with("lang=")) {
2758 lang = args[i].trim_prefix("lang=");
2759 break;
2760 }
2761 }
2762
2763 // Use monospace font with darkened background color to make code easier to distinguish from other text.
2764 // Use a single-column table with cell row background color instead of `[bgcolor]`.
2765 // This makes the background color highlight cover the entire block, rather than individual lines.
2766 p_rt->push_font(doc_code_font);
2767 p_rt->push_font_size(doc_code_font_size);
2768 p_rt->push_table(2);
2769
2770 p_rt->push_cell();
2771 p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
2772 p_rt->set_cell_padding(Rect2(10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE));
2773 p_rt->push_color(code_dark_color);
2774
2775 bool codeblock_printed = false;
2776
2777 #ifdef MODULE_GDSCRIPT_ENABLED
2778 if (!codeblock_printed && (lang.is_empty() || lang == "gdscript")) {
2779 EditorHelpHighlighter::get_singleton()->highlight(p_rt, EditorHelpHighlighter::LANGUAGE_GDSCRIPT, codeblock_text, is_native);
2780 codeblock_printed = true;
2781 }
2782 #endif
2783
2784 #ifdef MODULE_MONO_ENABLED
2785 if (!codeblock_printed && lang == "csharp") {
2786 EditorHelpHighlighter::get_singleton()->highlight(p_rt, EditorHelpHighlighter::LANGUAGE_CSHARP, codeblock_text, is_native);
2787 codeblock_printed = true;
2788 }
2789 #endif
2790
2791 if (!codeblock_printed) {
2792 p_rt->add_text(codeblock_text);
2793 codeblock_printed = true;
2794 }
2795
2796 p_rt->pop(); // color
2797 p_rt->pop(); // cell
2798
2799 // Copy codeblock button.
2800 p_rt->push_cell();
2801 p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
2802 p_rt->set_cell_padding(Rect2(0, 10 * EDSCALE, 0, 10 * EDSCALE));
2803 p_rt->set_cell_size_override(Vector2(1, 1), Vector2(10, 10) * EDSCALE);
2804 p_rt->push_meta("^" + codeblock_copy_text, RichTextLabel::META_UNDERLINE_ON_HOVER);
2805 p_rt->add_image(p_owner_node->get_editor_theme_icon(SNAME("ActionCopy")), 24 * EDSCALE, 24 * EDSCALE, Color(link_property_color, 0.3), INLINE_ALIGNMENT_BOTTOM_TO, Rect2(), Variant(), false, TTR("Click to copy."));
2806 p_rt->pop(); // meta
2807 p_rt->pop(); // cell
2808
2809 p_rt->pop(); // table
2810 p_rt->pop(); // font_size
2811 p_rt->pop(); // font
2812
2813 pos = end_pos + 12; // `len("[/codeblock]")`.
2814
2815 // Compensate for `\n` removed before the loop.
2816 if (pos < bbcode.length()) {
2817 p_rt->add_newline();
2818 }
2819 } else if (tag == "kbd") {
2820 int end_pos = bbcode.find("[/kbd]", brk_end + 1);
2821 if (end_pos < 0) {
2822 end_pos = bbcode.length();
2823 }
2824
2825 // Use keyboard font with custom color and background color.
2826 p_rt->push_font(doc_kbd_font);
2827 p_rt->push_font_size(doc_kbd_font_size);
2828 p_rt->push_bgcolor(kbd_bg_color);
2829 p_rt->push_color(kbd_color);
2830
2831 p_rt->add_text(bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)));
2832
2833 p_rt->pop(); // color
2834 p_rt->pop(); // bgcolor
2835 p_rt->pop(); // font_size
2836 p_rt->pop(); // font
2837
2838 pos = end_pos + 6; // `len("[/kbd]")`.
2839 } else if (tag == "center") {
2840 // Align to center.
2841 p_rt->push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, Control::TEXT_DIRECTION_AUTO, "");
2842 pos = brk_end + 1;
2843 tag_stack.push_front(tag);
2844 } else if (tag == "br") {
2845 // Force a line break.
2846 p_rt->add_newline();
2847 pos = brk_end + 1;
2848 } else if (tag == "u") {
2849 // Use underline.
2850 p_rt->push_underline();
2851 pos = brk_end + 1;
2852 tag_stack.push_front(tag);
2853 } else if (tag == "s") {
2854 // Use strikethrough.
2855 p_rt->push_strikethrough();
2856 pos = brk_end + 1;
2857 tag_stack.push_front(tag);
2858 } else if (tag == "lb") {
2859 p_rt->add_text("[");
2860 pos = brk_end + 1;
2861 } else if (tag == "rb") {
2862 p_rt->add_text("]");
2863 pos = brk_end + 1;
2864 } else if (tag == "url" || tag.begins_with("url=")) {
2865 String url;
2866 if (tag.begins_with("url=")) {
2867 url = tag.substr(4);
2868 } else {
2869 int end = bbcode.find_char('[', brk_end);
2870 if (end == -1) {
2871 end = bbcode.length();
2872 }
2873 url = bbcode.substr(brk_end + 1, end - brk_end - 1);
2874 }
2875
2876 p_rt->push_color(link_color);
2877 p_rt->push_meta(url, RichTextLabel::META_UNDERLINE_ON_HOVER, url + "\n\n" + TTR("Click to open in browser."));
2878
2879 pos = brk_end + 1;
2880 tag_stack.push_front("url");
2881 } else if (tag.begins_with("img")) {
2882 int width = 0;
2883 int height = 0;
2884 bool size_in_percent = false;
2885 if (tag.length() > 4) {
2886 Vector<String> subtags = tag.substr(4).split(" ");
2887 HashMap<String, String> bbcode_options;
2888 for (int i = 0; i < subtags.size(); i++) {
2889 const String &expr = subtags[i];
2890 int value_pos = expr.find_char('=');
2891 if (value_pos > -1) {
2892 bbcode_options[expr.left(value_pos)] = expr.substr(value_pos + 1).unquote();
2893 }
2894 }
2895 HashMap<String, String>::Iterator width_option = bbcode_options.find("width");
2896 if (width_option) {
2897 width = width_option->value.to_int();
2898 if (width_option->value.ends_with("%")) {
2899 size_in_percent = true;
2900 }
2901 }
2902
2903 HashMap<String, String>::Iterator height_option = bbcode_options.find("height");
2904 if (height_option) {
2905 height = height_option->value.to_int();
2906 if (height_option->value.ends_with("%")) {
2907 size_in_percent = true;
2908 }
2909 }
2910 }
2911 int end = bbcode.find_char('[', brk_end);
2912 if (end == -1) {
2913 end = bbcode.length();
2914 }
2915
2916 String image_path = bbcode.substr(brk_end + 1, end - brk_end - 1);
2917 p_rt->add_image(ResourceLoader::load(image_path, "Texture2D"), width, height, Color(1, 1, 1), INLINE_ALIGNMENT_CENTER, Rect2(), Variant(), false, String(), size_in_percent);
2918
2919 pos = end;
2920 tag_stack.push_front("img");
2921 } else if (tag.begins_with("color=")) {
2922 String col = tag.substr(6);
2923 Color color = Color::from_string(col, Color());
2924 p_rt->push_color(color);
2925
2926 pos = brk_end + 1;
2927 tag_stack.push_front("color");
2928 } else if (tag.begins_with("font=")) {
2929 String font_path = tag.substr(5);
2930 Ref<Font> font = ResourceLoader::load(font_path, "Font");
2931 if (font.is_valid()) {
2932 p_rt->push_font(font);
2933 } else {
2934 p_rt->push_font(doc_font);
2935 }
2936
2937 pos = brk_end + 1;
2938 tag_stack.push_front("font");
2939 } else {
2940 p_rt->add_text("["); // Ignore.
2941 pos = brk_pos + 1;
2942 }
2943 }
2944
2945 // Close unclosed tags.
2946 for (const String &tag : tag_stack) {
2947 if (tag != "img") {
2948 p_rt->pop();
2949 }
2950 }
2951 }
2952
2953 void EditorHelp::_add_text(const String &p_bbcode) {
2954 _add_text_to_rt(p_bbcode, class_desc, this, edited_class);
2955 }
2956
2957 void EditorHelp::_wait_for_thread(Thread &p_thread) {
2958 if (p_thread.is_started()) {
2959 p_thread.wait_to_finish();
2960 }
2961 }
2962
2963 void EditorHelp::_compute_doc_version_hash() {
2964 uint32_t version_hash = Engine::get_singleton()->get_version_info().hash();
2965 doc_version_hash = vformat("%d/%d/%d/%s", version_hash, ClassDB::get_api_hash(ClassDB::API_CORE), ClassDB::get_api_hash(ClassDB::API_EDITOR), _doc_data_hash);
2966 }
2967
2968 String EditorHelp::get_cache_full_path() {
2969 return EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("editor_doc_cache-%d.%d.res", GODOT_VERSION_MAJOR, GODOT_VERSION_MINOR));
2970 }
2971
2972 String EditorHelp::get_script_doc_cache_full_path() {
2973 return EditorPaths::get_singleton()->get_project_settings_dir().path_join("editor_script_doc_cache.res");
2974 }
2975
2976 DocTools *EditorHelp::get_doc_data() {
2977 _wait_for_thread();
2978 return doc;
2979 }
2980
2981 bool EditorHelp::has_doc(const String &p_class_name) {
2982 return get_doc(p_class_name) != nullptr;
2983 }
2984
2985 DocData::ClassDoc *EditorHelp::get_doc(const String &p_class_name) {
2986 return get_doc_data()->class_list.getptr(p_class_name);
2987 }
2988
2989 void EditorHelp::add_doc(const DocData::ClassDoc &p_class_doc) {
2990 if (!_script_docs_loaded.is_set()) {
2991 _docs_to_add.push_back(p_class_doc);
2992 return;
2993 }
2994
2995 get_doc_data()->add_doc(p_class_doc);
2996 }
2997
2998 void EditorHelp::remove_doc(const String &p_class_name) {
2999 if (!_script_docs_loaded.is_set()) {
3000 _docs_to_remove.push_back(p_class_name);
3001 return;
3002 }
3003
3004 DocTools *dt = get_doc_data();
3005 if (dt->has_doc(p_class_name)) {
3006 dt->remove_doc(p_class_name);
3007 }
3008 }
3009
3010 void EditorHelp::remove_script_doc_by_path(const String &p_path) {
3011 if (!_script_docs_loaded.is_set()) {
3012 _docs_to_remove_by_path.push_back(p_path);
3013 return;
3014 }
3015 get_doc_data()->remove_script_doc_by_path(p_path);
3016 }
3017
3018 void EditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) {
3019 if (!ext_doc) {
3020 ext_doc = memnew(DocTools);
3021 }
3022
3023 ext_doc->load_xml(p_buffer, p_size);
3024
3025 if (doc) {
3026 doc->load_xml(p_buffer, p_size);
3027 }
3028 }
3029
3030 void EditorHelp::remove_class(const String &p_class) {
3031 if (ext_doc && ext_doc->has_doc(p_class)) {
3032 ext_doc->remove_doc(p_class);
3033 }
3034
3035 if (doc && doc->has_doc(p_class)) {
3036 remove_doc(p_class);
3037 }
3038 }
3039
3040 void EditorHelp::_load_doc_thread(void *p_udata) {
3041 bool use_script_cache = (bool)p_udata;
3042 Ref<Resource> cache_res = ResourceLoader::load(get_cache_full_path());
3043 if (cache_res.is_valid() && cache_res->get_meta("version_hash", "") == doc_version_hash) {
3044 Array classes = cache_res->get_meta("classes", Array());
3045 for (int i = 0; i < classes.size(); i++) {
3046 doc->add_doc(DocData::ClassDoc::from_dict(classes[i]));
3047 }
3048 if (use_script_cache) {
3049 callable_mp_static(&EditorHelp::load_script_doc_cache).call_deferred();
3050 }
3051 // Extensions' docs are not cached. Generate them now (on the main thread).
3052 callable_mp_static(&EditorHelp::_gen_extensions_docs).call_deferred();
3053 } else {
3054 // We have to go back to the main thread to start from scratch, bypassing any possibly existing cache.
3055 callable_mp_static(&EditorHelp::generate_doc).call_deferred(false, use_script_cache);
3056 }
3057
3058 OS::get_singleton()->benchmark_end_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
3059 }
3060
3061 void EditorHelp::_gen_doc_thread(void *p_udata) {
3062 DocTools compdoc;
3063 compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
3064 doc->merge_from(compdoc); // Ensure all is up to date.
3065
3066 Ref<Resource> cache_res;
3067 cache_res.instantiate();
3068 cache_res->set_meta("version_hash", doc_version_hash);
3069 Array classes;
3070 for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
3071 if (ClassDB::class_exists(E.value.name)) {
3072 ClassDB::APIType api = ClassDB::get_api_type(E.value.name);
3073 if (api == ClassDB::API_EXTENSION || api == ClassDB::API_EDITOR_EXTENSION) {
3074 continue;
3075 }
3076 }
3077 classes.push_back(DocData::ClassDoc::to_dict(E.value));
3078 }
3079 cache_res->set_meta("classes", classes);
3080 Error err = ResourceSaver::save(cache_res, get_cache_full_path(), ResourceSaver::FLAG_COMPRESS);
3081 if (err) {
3082 ERR_PRINT("Cannot save editor help cache (" + get_cache_full_path() + ").");
3083 }
3084
3085 // Load script docs after native ones are cached so native cache doesn't contain script docs.
3086 bool use_script_cache = (bool)p_udata;
3087 if (use_script_cache) {
3088 callable_mp_static(&EditorHelp::load_script_doc_cache).call_deferred();
3089 }
3090
3091 OS::get_singleton()->benchmark_end_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
3092 }
3093
3094 void EditorHelp::_gen_extensions_docs() {
3095 doc->generate((DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES | DocTools::GENERATE_FLAG_EXTENSION_CLASSES_ONLY));
3096
3097 // Append extra doc data, as it gets overridden by the generation step.
3098 if (ext_doc) {
3099 doc->merge_from(*ext_doc);
3100 }
3101 }
3102 static void _load_script_doc_cache(bool p_changes) {
3103 EditorHelp::load_script_doc_cache();
3104 }
3105
3106 void EditorHelp::load_script_doc_cache() {
3107 if (!ProjectSettings::get_singleton()->is_project_loaded()) {
3108 print_verbose("Skipping loading script doc cache since no project is open.");
3109 return;
3110 }
3111
3112 if (EditorNode::is_cmdline_mode()) {
3113 return;
3114 }
3115
3116 _wait_for_thread();
3117
3118 if (!ResourceLoader::exists(get_script_doc_cache_full_path())) {
3119 print_verbose("Script documentation cache not found. Regenerating it may take a while for projects with many scripts.");
3120 regenerate_script_doc_cache();
3121 return;
3122 }
3123
3124 if (EditorFileSystem::get_singleton()->is_scanning()) {
3125 // This is assuming EditorFileSystem is performing first scan. We must wait until it is done.
3126 EditorFileSystem::get_singleton()->connect(SNAME("sources_changed"), callable_mp_static(_load_script_doc_cache), CONNECT_ONE_SHOT);
3127 return;
3128 }
3129
3130 worker_thread.start(_load_script_doc_cache_thread, nullptr);
3131 }
3132
3133 void EditorHelp::_process_postponed_docs() {
3134 for (const String &class_name : _docs_to_remove) {
3135 doc->remove_doc(class_name);
3136 }
3137 for (const String &path : _docs_to_remove_by_path) {
3138 doc->remove_script_doc_by_path(path);
3139 }
3140 for (const DocData::ClassDoc &cd : _docs_to_add) {
3141 doc->add_doc(cd);
3142 }
3143 _docs_to_add.clear();
3144 _docs_to_remove.clear();
3145 _docs_to_remove_by_path.clear();
3146 }
3147
3148 void EditorHelp::_load_script_doc_cache_thread(void *p_udata) {
3149 ERR_FAIL_COND_MSG(!ProjectSettings::get_singleton()->is_project_loaded(), "Error: cannot load script doc cache without a project.");
3150 ERR_FAIL_COND_MSG(!ResourceLoader::exists(get_script_doc_cache_full_path()), "Error: cannot load script doc cache from inexistent file.");
3151
3152 Ref<Resource> script_doc_cache_res = ResourceLoader::load(get_script_doc_cache_full_path(), "", ResourceFormatLoader::CACHE_MODE_IGNORE);
3153 if (script_doc_cache_res.is_null()) {
3154 print_verbose("Script doc cache is corrupted. Regenerating it instead.");
3155 _delete_script_doc_cache();
3156 callable_mp_static(EditorHelp::regenerate_script_doc_cache).call_deferred();
3157 return;
3158 }
3159
3160 Array classes = script_doc_cache_res->get_meta("classes", Array());
3161 for (const Dictionary dict : classes) {
3162 doc->add_doc(DocData::ClassDoc::from_dict(dict));
3163 }
3164
3165 // Protect from race condition in other threads reading / this thread writing to _docs_to_add/remove/etc.
3166 _script_docs_loaded.set();
3167
3168 // Deal with docs likely added from EditorFileSystem's scans while the cache was loading in EditorHelp::worker_thread.
3169 _process_postponed_docs();
3170
3171 // Always delete the doc cache after successful load since most uses of editor will change a script, invalidating cache.
3172 _delete_script_doc_cache();
3173 }
3174
3175 // Helper method to deal with "sources_changed" signal having a parameter.
3176 static void _regenerate_script_doc_cache(bool p_changes) {
3177 EditorHelp::regenerate_script_doc_cache();
3178 }
3179
3180 void EditorHelp::regenerate_script_doc_cache() {
3181 if (EditorFileSystem::get_singleton()->is_scanning()) {
3182 // Wait until EditorFileSystem scanning is complete to use updated filesystem structure.
3183 EditorFileSystem::get_singleton()->connect(SNAME("sources_changed"), callable_mp_static(_regenerate_script_doc_cache), CONNECT_ONE_SHOT);
3184 return;
3185 }
3186
3187 _wait_for_thread(worker_thread);
3188 _wait_for_thread(loader_thread);
3189 loader_thread.start(_regen_script_doc_thread, EditorFileSystem::get_singleton()->get_filesystem());
3190 }
3191
3192 // Runs on worker_thread since it writes to DocData.
3193 void EditorHelp::_finish_regen_script_doc_thread(void *p_udata) {
3194 loader_thread.wait_to_finish();
3195 _process_postponed_docs();
3196 _script_docs_loaded.set();
3197
3198 OS::get_singleton()->benchmark_end_measure("EditorHelp", "Generate Script Documentation");
3199 }
3200
3201 // Runs on loader_thread since _reload_scripts_documentation calls ResourceLoader::load().
3202 // Avoids deadlocks of worker_thread needing main thread for load task dispatching, but main thread waiting on worker_thread.
3203 void EditorHelp::_regen_script_doc_thread(void *p_udata) {
3204 OS::get_singleton()->benchmark_begin_measure("EditorHelp", "Generate Script Documentation");
3205
3206 EditorFileSystemDirectory *dir = static_cast<EditorFileSystemDirectory *>(p_udata);
3207 _script_docs_loaded.set_to(false);
3208
3209 // Ignore changes from filesystem scan since script docs will be now.
3210 _docs_to_add.clear();
3211 _docs_to_remove.clear();
3212 _docs_to_remove_by_path.clear();
3213
3214 _reload_scripts_documentation(dir);
3215
3216 // All ResourceLoader::load() calls are done, so we can no longer deadlock with main thread.
3217 // Switch to back to worker_thread from loader_thread to resynchronize access to DocData.
3218 worker_thread.start(_finish_regen_script_doc_thread, nullptr);
3219 }
3220
3221 void EditorHelp::_reload_scripts_documentation(EditorFileSystemDirectory *p_dir) {
3222 // Recursively force compile all scripts, which should generate their documentation.
3223 for (int i = 0; i < p_dir->get_subdir_count(); i++) {
3224 _reload_scripts_documentation(p_dir->get_subdir(i));
3225 }
3226
3227 for (int i = 0; i < p_dir->get_file_count(); i++) {
3228 if (ClassDB::is_parent_class(p_dir->get_file_type(i), SNAME("Script"))) {
3229 Ref<Script> scr = ResourceLoader::load(p_dir->get_file_path(i));
3230 if (scr.is_valid()) {
3231 for (const DocData::ClassDoc &cd : scr->get_documentation()) {
3232 _docs_to_add.push_back(cd);
3233 }
3234 }
3235 }
3236 }
3237 }
3238
3239 void EditorHelp::_delete_script_doc_cache() {
3240 if (FileAccess::exists(get_script_doc_cache_full_path())) {
3241 DirAccess::remove_file_or_error(ProjectSettings::get_singleton()->globalize_path(get_script_doc_cache_full_path()));
3242 }
3243 }
3244
3245 void EditorHelp::save_script_doc_cache() {
3246 if (!_script_docs_loaded.is_set()) {
3247 print_verbose("Script docs haven't been properly loaded or regenerated, so don't save them to disk.");
3248 return;
3249 }
3250
3251 Ref<Resource> cache_res;
3252 cache_res.instantiate();
3253 Array classes;
3254 for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
3255 if (E.value.is_script_doc) {
3256 classes.push_back(DocData::ClassDoc::to_dict(E.value));
3257 }
3258 }
3259
3260 cache_res->set_meta("classes", classes);
3261 Error err = ResourceSaver::save(cache_res, get_script_doc_cache_full_path(), ResourceSaver::FLAG_COMPRESS);
3262 ERR_FAIL_COND_MSG(err != OK, vformat("Cannot save script documentation cache in %s.", get_script_doc_cache_full_path()));
3263 }
3264
3265 void EditorHelp::generate_doc(bool p_use_cache, bool p_use_script_cache) {
3266 doc_generation_count++;
3267 OS::get_singleton()->benchmark_begin_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
3268
3269 // In case not the first attempt.
3270 _wait_for_thread();
3271
3272 if (!doc) {
3273 doc = memnew(DocTools);
3274 }
3275
3276 if (doc_version_hash.is_empty()) {
3277 _compute_doc_version_hash();
3278 }
3279
3280 if (p_use_cache && FileAccess::exists(get_cache_full_path())) {
3281 worker_thread.start(_load_doc_thread, (void *)p_use_script_cache);
3282 } else {
3283 print_verbose("Regenerating editor help cache");
3284 doc->generate();
3285 worker_thread.start(_gen_doc_thread, (void *)p_use_script_cache);
3286 }
3287 }
3288
3289 void EditorHelp::_toggle_files_pressed() {
3290 ScriptEditor::get_singleton()->toggle_files_panel();
3291 update_toggle_files_button();
3292 }
3293
3294 void EditorHelp::_notification(int p_what) {
3295 switch (p_what) {
3296 case NOTIFICATION_POSTINITIALIZE: {
3297 // Requires theme to be up to date.
3298 _class_desc_resized(false);
3299 } break;
3300
3301 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
3302 bool need_update = false;
3303 if (EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/help")) {
3304 need_update = true;
3305 }
3306 #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
3307 if (!need_update && EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/theme/highlighting")) {
3308 need_update = true;
3309 }
3310 #endif
3311 if (!need_update) {
3312 break;
3313 }
3314 [[fallthrough]];
3315 }
3316 case NOTIFICATION_READY: {
3317 _wait_for_thread();
3318 _update_doc();
3319 } break;
3320
3321 case NOTIFICATION_THEME_CHANGED: {
3322 if (is_inside_tree()) {
3323 if (is_visible_in_tree()) {
3324 _update_doc();
3325 } else {
3326 update_pending = true;
3327 }
3328
3329 _class_desc_resized(true);
3330 }
3331 update_toggle_files_button();
3332 } break;
3333
3334 case NOTIFICATION_VISIBILITY_CHANGED: {
3335 if (update_pending && is_visible_in_tree()) {
3336 _update_doc();
3337 }
3338 update_toggle_files_button();
3339 } break;
3340
3341 case NOTIFICATION_TRANSLATION_CHANGED: {
3342 if (!is_ready()) {
3343 break;
3344 }
3345
3346 if (is_visible_in_tree()) {
3347 _update_doc();
3348 } else {
3349 update_pending = true;
3350 }
3351 [[fallthrough]];
3352 }
3353 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED: {
3354 update_toggle_files_button();
3355 } break;
3356 }
3357 }
3358
3359 void EditorHelp::go_to_help(const String &p_help) {
3360 _wait_for_thread();
3361 _help_callback(p_help);
3362 }
3363
3364 void EditorHelp::go_to_class(const String &p_class) {
3365 _wait_for_thread();
3366 _goto_desc(p_class);
3367 }
3368
3369 void EditorHelp::update_doc() {
3370 _wait_for_thread();
3371 ERR_FAIL_COND(!doc->class_list.has(edited_class));
3372 ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
3373 _update_doc();
3374 }
3375
3376 void EditorHelp::cleanup_doc() {
3377 _wait_for_thread();
3378 memdelete(doc);
3379 doc = nullptr;
3380 }
3381
3382 Vector<Pair<String, int>> EditorHelp::get_sections() {
3383 _wait_for_thread();
3384 Vector<Pair<String, int>> sections;
3385
3386 for (int i = 0; i < section_line.size(); i++) {
3387 sections.push_back(Pair<String, int>(section_line[i].first, i));
3388 }
3389 return sections;
3390 }
3391
3392 void EditorHelp::scroll_to_section(int p_section_index) {
3393 _wait_for_thread();
3394 int line = section_line[p_section_index].second;
3395 if (class_desc->is_finished()) {
3396 class_desc->scroll_to_paragraph(line);
3397 } else {
3398 scroll_to = line;
3399 }
3400 }
3401
3402 void EditorHelp::popup_search() {
3403 _wait_for_thread();
3404 find_bar->popup_search();
3405 }
3406
3407 String EditorHelp::get_class() {
3408 return edited_class;
3409 }
3410
3411 void EditorHelp::search_again(bool p_search_previous) {
3412 _search(p_search_previous);
3413 }
3414
3415 int EditorHelp::get_scroll() const {
3416 return class_desc->get_v_scroll_bar()->get_value();
3417 }
3418
3419 void EditorHelp::set_scroll(int p_scroll) {
3420 class_desc->get_v_scroll_bar()->set_value(p_scroll);
3421 }
3422
3423 void EditorHelp::update_toggle_files_button() {
3424 if (is_layout_rtl()) {
3425 toggle_files_button->set_button_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_files_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
3426 } else {
3427 toggle_files_button->set_button_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_files_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
3428 }
3429 toggle_files_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Files Panel"), ED_GET_SHORTCUT("script_editor/toggle_files_panel")->get_as_text()));
3430 }
3431
3432 void EditorHelp::_bind_methods() {
3433 ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
3434 ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
3435 ClassDB::bind_method("_search", &EditorHelp::_search);
3436 ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
3437
3438 ADD_SIGNAL(MethodInfo("go_to_help"));
3439 ADD_SIGNAL(MethodInfo("request_save_history"));
3440 }
3441
3442 void EditorHelp::init_gdext_pointers() {
3443 GDExtensionEditorHelp::editor_help_load_xml_buffer = &EditorHelp::load_xml_buffer;
3444 GDExtensionEditorHelp::editor_help_remove_class = &EditorHelp::remove_class;
3445 }
3446
3447 EditorHelp::EditorHelp() {
3448 set_custom_minimum_size(Size2(150 * EDSCALE, 0));
3449
3450 class_desc = memnew(RichTextLabel);
3451 class_desc->set_tab_size(8);
3452 add_child(class_desc);
3453 class_desc->set_threaded(true);
3454 class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
3455
3456 class_desc->connect(SceneStringName(finished), callable_mp(this, &EditorHelp::_class_desc_finished));
3457 class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
3458 class_desc->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelp::_class_desc_input));
3459 class_desc->connect(SceneStringName(resized), callable_mp(this, &EditorHelp::_class_desc_resized).bind(false));
3460
3461 // Added second so it opens at the bottom so it won't offset the entire widget.
3462 find_bar = memnew(FindBar);
3463 add_child(find_bar);
3464 find_bar->hide();
3465 find_bar->set_rich_text_label(class_desc);
3466
3467 status_bar = memnew(HBoxContainer);
3468 add_child(status_bar);
3469 status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
3470 status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
3471
3472 toggle_files_button = memnew(Button);
3473 toggle_files_button->set_theme_type_variation(SceneStringName(FlatButton));
3474 toggle_files_button->set_accessibility_name(TTRC("Scripts"));
3475 toggle_files_button->set_tooltip_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
3476 toggle_files_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelp::_toggle_files_pressed));
3477 status_bar->add_child(toggle_files_button);
3478
3479 class_desc->set_selection_enabled(true);
3480 class_desc->set_context_menu_enabled(true);
3481 class_desc->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
3482
3483 class_desc->hide();
3484 }
3485
3486 /// EditorHelpBit ///
3487
3488 #define HANDLE_DOC(m_string) ((is_native ? DTR(m_string) : (m_string)).strip_edges())
3489
3490 EditorHelpBit::HelpData EditorHelpBit::_get_class_help_data(const StringName &p_class_name) {
3491 if (doc_class_cache.has(p_class_name)) {
3492 return doc_class_cache[p_class_name];
3493 }
3494
3495 HelpData result;
3496
3497 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3498 if (class_doc) {
3499 // Non-native class shouldn't be cached, nor translated.
3500 const bool is_native = !class_doc->is_script_doc;
3501
3502 const String brief_description = HANDLE_DOC(class_doc->brief_description);
3503 const String long_description = HANDLE_DOC(class_doc->description);
3504
3505 if (!brief_description.is_empty()) {
3506 result.description += "[b]" + brief_description + "[/b]";
3507 }
3508 if (!long_description.is_empty()) {
3509 if (!result.description.is_empty()) {
3510 result.description += '\n';
3511 }
3512 result.description += long_description;
3513 }
3514 if (class_doc->is_deprecated) {
3515 if (class_doc->deprecated_message.is_empty()) {
3516 result.deprecated_message = TTR("This class may be changed or removed in future versions.");
3517 } else {
3518 result.deprecated_message = HANDLE_DOC(class_doc->deprecated_message);
3519 }
3520 }
3521 if (class_doc->is_experimental) {
3522 if (class_doc->experimental_message.is_empty()) {
3523 result.experimental_message = TTR("This class may be changed or removed in future versions.");
3524 } else {
3525 result.experimental_message = HANDLE_DOC(class_doc->experimental_message);
3526 }
3527 }
3528
3529 if (is_native) {
3530 doc_class_cache[p_class_name] = result;
3531 }
3532 }
3533
3534 return result;
3535 }
3536
3537 EditorHelpBit::HelpData EditorHelpBit::_get_enum_help_data(const StringName &p_class_name, const StringName &p_enum_name) {
3538 if (doc_enum_cache.has(p_class_name) && doc_enum_cache[p_class_name].has(p_enum_name)) {
3539 return doc_enum_cache[p_class_name][p_enum_name];
3540 }
3541
3542 HelpData result;
3543
3544 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3545 if (class_doc) {
3546 // Non-native enums shouldn't be cached, nor translated.
3547 const bool is_native = !class_doc->is_script_doc;
3548
3549 for (const KeyValue<String, DocData::EnumDoc> &kv : class_doc->enums) {
3550 const StringName enum_name = kv.key;
3551 const DocData::EnumDoc &enum_doc = kv.value;
3552
3553 HelpData current;
3554 current.description = HANDLE_DOC(enum_doc.description);
3555 if (enum_doc.is_deprecated) {
3556 if (enum_doc.deprecated_message.is_empty()) {
3557 current.deprecated_message = TTR("This enumeration may be changed or removed in future versions.");
3558 } else {
3559 current.deprecated_message = HANDLE_DOC(enum_doc.deprecated_message);
3560 }
3561 }
3562 if (enum_doc.is_experimental) {
3563 if (enum_doc.experimental_message.is_empty()) {
3564 current.experimental_message = TTR("This enumeration may be changed or removed in future versions.");
3565 } else {
3566 current.experimental_message = HANDLE_DOC(enum_doc.experimental_message);
3567 }
3568 }
3569
3570 if (enum_name == p_enum_name) {
3571 result = current;
3572
3573 if (!is_native) {
3574 break;
3575 }
3576 }
3577
3578 if (is_native) {
3579 doc_enum_cache[p_class_name][enum_name] = current;
3580 }
3581 }
3582 }
3583
3584 return result;
3585 }
3586
3587 EditorHelpBit::HelpData EditorHelpBit::_get_constant_help_data(const StringName &p_class_name, const StringName &p_constant_name) {
3588 if (doc_constant_cache.has(p_class_name) && doc_constant_cache[p_class_name].has(p_constant_name)) {
3589 return doc_constant_cache[p_class_name][p_constant_name];
3590 }
3591
3592 HelpData result;
3593
3594 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3595 if (class_doc) {
3596 // Non-native constants shouldn't be cached, nor translated.
3597 const bool is_native = !class_doc->is_script_doc;
3598
3599 for (const DocData::ConstantDoc &constant : class_doc->constants) {
3600 HelpData current;
3601 current.description = HANDLE_DOC(constant.description);
3602 if (constant.is_deprecated) {
3603 if (constant.deprecated_message.is_empty()) {
3604 current.deprecated_message = TTR("This constant may be changed or removed in future versions.");
3605 } else {
3606 current.deprecated_message = HANDLE_DOC(constant.deprecated_message);
3607 }
3608 }
3609 if (constant.is_experimental) {
3610 if (constant.experimental_message.is_empty()) {
3611 current.experimental_message = TTR("This constant may be changed or removed in future versions.");
3612 } else {
3613 current.experimental_message = HANDLE_DOC(constant.experimental_message);
3614 }
3615 }
3616 current.doc_type = { constant.type, constant.enumeration, constant.is_bitfield };
3617 if (constant.is_value_valid) {
3618 current.value = constant.value;
3619 }
3620
3621 if (constant.name == p_constant_name) {
3622 result = current;
3623
3624 if (!is_native) {
3625 break;
3626 }
3627 }
3628
3629 if (is_native) {
3630 doc_constant_cache[p_class_name][constant.name] = current;
3631 }
3632 }
3633 }
3634
3635 return result;
3636 }
3637
3638 EditorHelpBit::HelpData EditorHelpBit::_get_property_help_data(const StringName &p_class_name, const StringName &p_property_name) {
3639 if (doc_property_cache.has(p_class_name) && doc_property_cache[p_class_name].has(p_property_name)) {
3640 return doc_property_cache[p_class_name][p_property_name];
3641 }
3642
3643 HelpData result;
3644
3645 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3646 if (class_doc) {
3647 // Non-native properties shouldn't be cached, nor translated.
3648 const bool is_native = !class_doc->is_script_doc;
3649
3650 for (const DocData::PropertyDoc &property : class_doc->properties) {
3651 HelpData current;
3652 current.description = HANDLE_DOC(property.description);
3653 if (property.is_deprecated) {
3654 if (property.deprecated_message.is_empty()) {
3655 current.deprecated_message = TTR("This property may be changed or removed in future versions.");
3656 } else {
3657 current.deprecated_message = HANDLE_DOC(property.deprecated_message);
3658 }
3659 }
3660 if (property.is_experimental) {
3661 if (property.experimental_message.is_empty()) {
3662 current.experimental_message = TTR("This property may be changed or removed in future versions.");
3663 } else {
3664 current.experimental_message = HANDLE_DOC(property.experimental_message);
3665 }
3666 }
3667 current.doc_type = { property.type, property.enumeration, property.is_bitfield };
3668 current.value = property.default_value;
3669
3670 String enum_class_name;
3671 String enum_name;
3672 if (CoreConstants::is_global_enum(property.enumeration)) {
3673 enum_class_name = "@GlobalScope";
3674 enum_name = property.enumeration;
3675 } else {
3676 const int dot_pos = property.enumeration.rfind_char('.');
3677 if (dot_pos >= 0) {
3678 enum_class_name = property.enumeration.left(dot_pos);
3679 enum_name = property.enumeration.substr(dot_pos + 1);
3680 }
3681 }
3682
3683 if (!enum_class_name.is_empty() && !enum_name.is_empty()) {
3684 // Classes can use enums from other classes, so check from which it came.
3685 const DocData::ClassDoc *enum_class = EditorHelp::get_doc(enum_class_name);
3686 if (enum_class) {
3687 const String enum_prefix = EditorPropertyNameProcessor::get_singleton()->process_name(enum_name, EditorPropertyNameProcessor::STYLE_CAPITALIZED) + " ";
3688 for (DocData::ConstantDoc constant : enum_class->constants) {
3689 // Don't display `_MAX` enum value descriptions, as these are never exposed in the inspector.
3690 if (constant.enumeration == enum_name && !constant.name.ends_with("_MAX")) {
3691 // Prettify the enum value display, so that "<ENUM_NAME>_<ITEM>" becomes "Item".
3692 const String item_name = EditorPropertyNameProcessor::get_singleton()->process_name(constant.name, EditorPropertyNameProcessor::STYLE_CAPITALIZED).trim_prefix(enum_prefix);
3693 String item_descr = HANDLE_DOC(constant.description);
3694 if (item_descr.is_empty()) {
3695 item_descr = "[color=<EditorHelpBitCommentColor>][i]" + TTR("No description available.") + "[/i][/color]";
3696 }
3697 current.description += vformat("\n[b]%s:[/b] %s", item_name, item_descr);
3698 }
3699 }
3700 current.description = current.description.lstrip("\n");
3701 }
3702 }
3703
3704 if (property.name == p_property_name) {
3705 result = current;
3706
3707 if (!is_native) {
3708 break;
3709 }
3710 }
3711
3712 if (is_native) {
3713 doc_property_cache[p_class_name][property.name] = current;
3714 }
3715 }
3716 }
3717
3718 return result;
3719 }
3720
3721 EditorHelpBit::HelpData EditorHelpBit::_get_theme_item_help_data(const StringName &p_class_name, const StringName &p_theme_item_name) {
3722 if (doc_theme_item_cache.has(p_class_name) && doc_theme_item_cache[p_class_name].has(p_theme_item_name)) {
3723 return doc_theme_item_cache[p_class_name][p_theme_item_name];
3724 }
3725
3726 HelpData result;
3727
3728 bool found = false;
3729 DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3730 while (class_doc) {
3731 // Non-native theme items shouldn't be cached, nor translated.
3732 const bool is_native = !class_doc->is_script_doc;
3733
3734 for (const DocData::ThemeItemDoc &theme_item : class_doc->theme_properties) {
3735 HelpData current;
3736 current.description = HANDLE_DOC(theme_item.description);
3737 if (theme_item.is_deprecated) {
3738 if (theme_item.deprecated_message.is_empty()) {
3739 current.deprecated_message = TTR("This theme property may be changed or removed in future versions.");
3740 } else {
3741 current.deprecated_message = HANDLE_DOC(theme_item.deprecated_message);
3742 }
3743 }
3744 if (theme_item.is_experimental) {
3745 if (theme_item.experimental_message.is_empty()) {
3746 current.experimental_message = TTR("This theme property may be changed or removed in future versions.");
3747 } else {
3748 current.experimental_message = HANDLE_DOC(theme_item.experimental_message);
3749 }
3750 }
3751 current.doc_type = { theme_item.type, String(), false };
3752 current.value = theme_item.default_value;
3753
3754 if (theme_item.name == p_theme_item_name) {
3755 result = current;
3756 found = true;
3757
3758 if (!is_native) {
3759 break;
3760 }
3761 }
3762
3763 if (is_native) {
3764 doc_theme_item_cache[p_class_name][theme_item.name] = current;
3765 }
3766 }
3767
3768 if (found || class_doc->inherits.is_empty()) {
3769 break;
3770 }
3771
3772 // Check for inherited theme items.
3773 class_doc = EditorHelp::get_doc(class_doc->inherits);
3774 }
3775
3776 return result;
3777 }
3778
3779 EditorHelpBit::HelpData EditorHelpBit::_get_method_help_data(const StringName &p_class_name, const StringName &p_method_name) {
3780 if (doc_method_cache.has(p_class_name) && doc_method_cache[p_class_name].has(p_method_name)) {
3781 return doc_method_cache[p_class_name][p_method_name];
3782 }
3783
3784 HelpData result;
3785
3786 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3787 if (class_doc) {
3788 // Non-native methods shouldn't be cached, nor translated.
3789 const bool is_native = !class_doc->is_script_doc;
3790
3791 for (const DocData::MethodDoc &method : class_doc->methods) {
3792 HelpData current;
3793 current.description = HANDLE_DOC(method.description);
3794 if (method.is_deprecated) {
3795 if (method.deprecated_message.is_empty()) {
3796 current.deprecated_message = TTR("This method may be changed or removed in future versions.");
3797 } else {
3798 current.deprecated_message = HANDLE_DOC(method.deprecated_message);
3799 }
3800 }
3801 if (method.is_experimental) {
3802 if (method.experimental_message.is_empty()) {
3803 current.experimental_message = TTR("This method may be changed or removed in future versions.");
3804 } else {
3805 current.experimental_message = HANDLE_DOC(method.experimental_message);
3806 }
3807 }
3808 current.doc_type = { method.return_type, method.return_enum, method.return_is_bitfield };
3809 for (const DocData::ArgumentDoc &argument : method.arguments) {
3810 const DocType argument_doc_type = { argument.type, argument.enumeration, argument.is_bitfield };
3811 current.arguments.push_back({ argument.name, argument_doc_type, argument.default_value });
3812 }
3813 current.qualifiers = method.qualifiers;
3814 const DocData::ArgumentDoc &rest_argument = method.rest_argument;
3815 const DocType rest_argument_doc_type = { rest_argument.type, rest_argument.enumeration, rest_argument.is_bitfield };
3816 current.rest_argument = { rest_argument.name, rest_argument_doc_type, rest_argument.default_value };
3817
3818 if (method.name == p_method_name) {
3819 result = current;
3820
3821 if (!is_native) {
3822 break;
3823 }
3824 }
3825
3826 if (is_native) {
3827 doc_method_cache[p_class_name][method.name] = current;
3828 }
3829 }
3830 }
3831
3832 return result;
3833 }
3834
3835 EditorHelpBit::HelpData EditorHelpBit::_get_signal_help_data(const StringName &p_class_name, const StringName &p_signal_name) {
3836 if (doc_signal_cache.has(p_class_name) && doc_signal_cache[p_class_name].has(p_signal_name)) {
3837 return doc_signal_cache[p_class_name][p_signal_name];
3838 }
3839
3840 HelpData result;
3841
3842 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3843 if (class_doc) {
3844 // Non-native signals shouldn't be cached, nor translated.
3845 const bool is_native = !class_doc->is_script_doc;
3846
3847 for (const DocData::MethodDoc &signal : class_doc->signals) {
3848 HelpData current;
3849 current.description = HANDLE_DOC(signal.description);
3850 if (signal.is_deprecated) {
3851 if (signal.deprecated_message.is_empty()) {
3852 current.deprecated_message = TTR("This signal may be changed or removed in future versions.");
3853 } else {
3854 current.deprecated_message = HANDLE_DOC(signal.deprecated_message);
3855 }
3856 }
3857 if (signal.is_experimental) {
3858 if (signal.experimental_message.is_empty()) {
3859 current.experimental_message = TTR("This signal may be changed or removed in future versions.");
3860 } else {
3861 current.experimental_message = HANDLE_DOC(signal.experimental_message);
3862 }
3863 }
3864 for (const DocData::ArgumentDoc &argument : signal.arguments) {
3865 const DocType argument_type = { argument.type, argument.enumeration, argument.is_bitfield };
3866 current.arguments.push_back({ argument.name, argument_type, argument.default_value });
3867 }
3868 current.qualifiers = signal.qualifiers;
3869
3870 if (signal.name == p_signal_name) {
3871 result = current;
3872
3873 if (!is_native) {
3874 break;
3875 }
3876 }
3877
3878 if (is_native) {
3879 doc_signal_cache[p_class_name][signal.name] = current;
3880 }
3881 }
3882 }
3883
3884 return result;
3885 }
3886
3887 EditorHelpBit::HelpData EditorHelpBit::_get_annotation_help_data(const StringName &p_class_name, const StringName &p_annotation_name) {
3888 if (doc_annotation_cache.has(p_class_name) && doc_annotation_cache[p_class_name].has(p_annotation_name)) {
3889 return doc_annotation_cache[p_class_name][p_annotation_name];
3890 }
3891
3892 HelpData result;
3893
3894 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(p_class_name);
3895 if (class_doc) {
3896 // Non-native annotations shouldn't be cached, nor translated.
3897 const bool is_native = !class_doc->is_script_doc;
3898
3899 for (const DocData::MethodDoc &annotation : class_doc->annotations) {
3900 HelpData current;
3901 current.description = HANDLE_DOC(annotation.description);
3902 if (annotation.is_deprecated) {
3903 if (annotation.deprecated_message.is_empty()) {
3904 current.deprecated_message = TTR("This annotation may be changed or removed in future versions.");
3905 } else {
3906 current.deprecated_message = HANDLE_DOC(annotation.deprecated_message);
3907 }
3908 }
3909 if (annotation.is_experimental) {
3910 if (annotation.experimental_message.is_empty()) {
3911 current.experimental_message = TTR("This annotation may be changed or removed in future versions.");
3912 } else {
3913 current.experimental_message = HANDLE_DOC(annotation.experimental_message);
3914 }
3915 }
3916 for (const DocData::ArgumentDoc &argument : annotation.arguments) {
3917 const DocType argument_type = { argument.type, argument.enumeration, argument.is_bitfield };
3918 current.arguments.push_back({ argument.name, argument_type, argument.default_value });
3919 }
3920 current.qualifiers = annotation.qualifiers;
3921
3922 if (annotation.name == p_annotation_name) {
3923 result = current;
3924
3925 if (!is_native) {
3926 break;
3927 }
3928 }
3929
3930 if (is_native) {
3931 doc_annotation_cache[p_class_name][annotation.name] = current;
3932 }
3933 }
3934 }
3935
3936 return result;
3937 }
3938
3939 #undef HANDLE_DOC
3940
3941 void EditorHelpBit::_add_type_to_title(const DocType &p_doc_type) {
3942 _add_type_to_rt(p_doc_type.type, p_doc_type.enumeration, p_doc_type.is_bitfield, title, this, symbol_class_name);
3943 }
3944
3945 void EditorHelpBit::_update_labels() {
3946 const Ref<Font> doc_bold_font = get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
3947
3948 if (!symbol_type.is_empty() || !symbol_name.is_empty()) {
3949 title->clear();
3950
3951 title->push_font(doc_bold_font);
3952
3953 if (!symbol_type.is_empty()) {
3954 title->push_color(get_theme_color(SNAME("title_color"), SNAME("EditorHelp")));
3955 title->add_text(symbol_type);
3956 title->pop(); // color
3957 }
3958
3959 if (!symbol_type.is_empty() && !symbol_name.is_empty()) {
3960 title->add_text(" ");
3961 }
3962
3963 if (!symbol_name.is_empty()) {
3964 if (!symbol_doc_link.is_empty()) {
3965 title->push_meta(symbol_doc_link, RichTextLabel::META_UNDERLINE_ON_HOVER);
3966 }
3967 if (use_class_prefix && !symbol_class_name.is_empty() && symbol_hint != SYMBOL_HINT_INHERITANCE) {
3968 title->add_text(symbol_class_name + ".");
3969 }
3970 title->add_text(symbol_name);
3971 if (!symbol_doc_link.is_empty()) {
3972 title->pop(); // meta
3973 }
3974 }
3975
3976 title->pop(); // font
3977
3978 const Color text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
3979 const Color symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
3980 const Color value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
3981 const Color qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
3982 const Ref<Font> doc_source = get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
3983 const int doc_source_size = get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
3984
3985 switch (symbol_hint) {
3986 case SYMBOL_HINT_NONE: {
3987 // Nothing to do.
3988 } break;
3989 case SYMBOL_HINT_INHERITANCE: {
3990 const DocData::ClassDoc *class_doc = EditorHelp::get_doc(symbol_class_name);
3991 String inherits = class_doc ? class_doc->inherits : String();
3992
3993 if (!inherits.is_empty()) {
3994 title->push_font(doc_source);
3995 title->push_font_size(doc_source_size * 0.9);
3996
3997 while (!inherits.is_empty()) {
3998 title->push_color(symbol_color);
3999 title->add_text(" <" + nbsp);
4000 title->pop(); // color
4001
4002 _add_type_to_title({ inherits, String(), false });
4003
4004 const DocData::ClassDoc *base_class_doc = EditorHelp::get_doc(inherits);
4005 inherits = base_class_doc ? base_class_doc->inherits : String();
4006 }
4007
4008 title->pop(); // font_size
4009 title->pop(); // font
4010 }
4011 } break;
4012 case SYMBOL_HINT_ASSIGNABLE: {
4013 const bool has_type = !help_data.doc_type.type.is_empty();
4014 const bool has_value = !help_data.value.is_empty();
4015
4016 if (has_type || has_value) {
4017 title->push_font(doc_source);
4018 title->push_font_size(doc_source_size * 0.9);
4019
4020 if (has_type) {
4021 title->push_color(symbol_color);
4022 title->add_text(colon_nbsp);
4023 title->pop(); // color
4024
4025 _add_type_to_title(help_data.doc_type);
4026 }
4027
4028 if (has_value) {
4029 title->push_color(symbol_color);
4030 title->add_text(nbsp_equal_nbsp);
4031 title->pop(); // color
4032
4033 title->push_color(value_color);
4034 title->add_text(_fix_constant(help_data.value));
4035 title->pop(); // color
4036 }
4037
4038 title->pop(); // font_size
4039 title->pop(); // font
4040 }
4041 } break;
4042 case SYMBOL_HINT_SIGNATURE: {
4043 title->push_font(doc_source);
4044 title->push_font_size(doc_source_size * 0.9);
4045
4046 title->push_color(symbol_color);
4047 title->add_text("(");
4048 title->pop(); // color
4049
4050 for (int i = 0; i < help_data.arguments.size(); i++) {
4051 const ArgumentData &argument = help_data.arguments[i];
4052
4053 if (i > 0) {
4054 title->push_color(symbol_color);
4055 title->add_text(", ");
4056 title->pop(); // color
4057 }
4058
4059 title->push_color(text_color);
4060 title->add_text(argument.name);
4061 title->pop(); // color
4062
4063 title->push_color(symbol_color);
4064 title->add_text(colon_nbsp);
4065 title->pop(); // color
4066
4067 _add_type_to_title(argument.doc_type);
4068
4069 if (!argument.default_value.is_empty()) {
4070 title->push_color(symbol_color);
4071 title->add_text(nbsp_equal_nbsp);
4072 title->pop(); // color
4073
4074 title->push_color(value_color);
4075 title->add_text(_fix_constant(argument.default_value));
4076 title->pop(); // color
4077 }
4078 }
4079
4080 if (help_data.qualifiers.contains("vararg")) {
4081 if (!help_data.arguments.is_empty()) {
4082 title->push_color(symbol_color);
4083 title->add_text(", ");
4084 title->pop(); // color
4085 }
4086
4087 title->push_color(symbol_color);
4088 title->add_text("...");
4089 title->pop(); // color
4090
4091 const ArgumentData &rest_argument = help_data.rest_argument;
4092
4093 title->push_color(text_color);
4094 title->add_text(rest_argument.name.is_empty() ? "args" : rest_argument.name);
4095 title->pop(); // color
4096
4097 title->push_color(symbol_color);
4098 title->add_text(colon_nbsp);
4099 title->pop(); // color
4100
4101 if (rest_argument.doc_type.type.is_empty()) {
4102 _add_type_to_title({ "Array", "", false });
4103 } else {
4104 _add_type_to_title(rest_argument.doc_type);
4105 }
4106 }
4107
4108 title->push_color(symbol_color);
4109 title->add_text(")");
4110 title->pop(); // color
4111
4112 if (!help_data.doc_type.type.is_empty()) {
4113 title->push_color(symbol_color);
4114 title->add_text(" ->" + nbsp);
4115 title->pop(); // color
4116
4117 _add_type_to_title(help_data.doc_type);
4118 }
4119
4120 if (!help_data.qualifiers.is_empty()) {
4121 title->push_color(qualifier_color);
4122 _add_qualifiers_to_rt(help_data.qualifiers, title);
4123 title->pop(); // color
4124 }
4125
4126 title->pop(); // font_size
4127 title->pop(); // font
4128 } break;
4129 }
4130
4131 title->show();
4132 } else {
4133 title->hide();
4134 }
4135
4136 content->clear();
4137
4138 bool has_prev_text = false;
4139
4140 if (!help_data.deprecated_message.is_empty()) {
4141 has_prev_text = true;
4142
4143 Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError"));
4144 content->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
4145 content->add_text(nbsp);
4146 content->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
4147 content->push_font(doc_bold_font);
4148 content->add_text(TTR("Deprecated:"));
4149 content->pop(); // font
4150 content->pop(); // color
4151 content->add_text(" ");
4152 _add_text_to_rt(help_data.deprecated_message, content, this, symbol_class_name);
4153 }
4154
4155 if (!help_data.experimental_message.is_empty()) {
4156 if (has_prev_text) {
4157 content->add_newline();
4158 content->add_newline();
4159 }
4160 has_prev_text = true;
4161
4162 Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning"));
4163 content->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
4164 content->add_text(nbsp);
4165 content->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
4166 content->push_font(doc_bold_font);
4167 content->add_text(TTR("Experimental:"));
4168 content->pop(); // font
4169 content->pop(); // color
4170 content->add_text(" ");
4171 _add_text_to_rt(help_data.experimental_message, content, this, symbol_class_name);
4172 }
4173
4174 if (!help_data.description.is_empty()) {
4175 if (has_prev_text) {
4176 content->add_newline();
4177 content->add_newline();
4178 }
4179 has_prev_text = true;
4180
4181 const Color comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
4182 _add_text_to_rt(help_data.description.replace("<EditorHelpBitCommentColor>", comment_color.to_html()), content, this, symbol_class_name);
4183 }
4184
4185 if (!help_data.resource_path.is_empty()) {
4186 if (has_prev_text) {
4187 content->add_newline();
4188 content->add_newline();
4189 }
4190 has_prev_text = true;
4191
4192 const String ext = help_data.resource_path.get_extension();
4193 const bool is_dir = ext.is_empty();
4194 const bool is_valid = is_dir || EditorFileSystem::get_singleton()->get_valid_extensions().has(ext);
4195 if (!is_dir && is_valid) {
4196 content->push_meta("open-res:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
4197 content->add_image(get_editor_theme_icon(SNAME("Load")));
4198 content->add_text(nbsp + TTR("Open"));
4199 content->pop(); // meta
4200 content->add_newline();
4201 }
4202
4203 if (is_valid) {
4204 content->push_meta("show:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
4205 content->add_image(get_editor_theme_icon(SNAME("Filesystem")));
4206 content->add_text(nbsp + TTR("Show in FileSystem"));
4207 content->pop(); // meta
4208 } else {
4209 content->push_meta("open-file:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
4210 content->add_image(get_editor_theme_icon(SNAME("Filesystem")));
4211 content->add_text(nbsp + TTR("Open in File Manager"));
4212 content->pop(); // meta
4213 }
4214 }
4215
4216 if (is_inside_tree()) {
4217 update_content_height();
4218 }
4219 }
4220
4221 void EditorHelpBit::_go_to_url(const String &p_what) {
4222 Vector<String> parts;
4223 {
4224 int from = 0;
4225 int buffer_start = 0;
4226 while (true) {
4227 const int pos = p_what.find_char(':', from);
4228 if (pos < 0) {
4229 parts.push_back(p_what.substr(buffer_start));
4230 break;
4231 }
4232
4233 if (pos + 1 < p_what.length() && p_what[pos + 1] == ':') {
4234 // `::` used in built-in scripts.
4235 from = pos + 2;
4236 } else {
4237 parts.push_back(p_what.substr(buffer_start, pos - buffer_start));
4238 from = pos + 1;
4239 buffer_start = from;
4240 }
4241 }
4242 }
4243
4244 const String what = parts[0]; // `parts` is always non-empty.
4245 const String clss = (parts.size() > 1) ? parts[1].to_lower() : String();
4246 const String name = (parts.size() > 2) ? parts[2].to_lower().replace_chars("/_", '-') : String();
4247
4248 String section = "";
4249 if (what == "class_desc") {
4250 section = "#description";
4251 } else if (what == "class_signal") {
4252 section = vformat("#class-%s-signal-%s", clss, name);
4253 } else if (what == "class_method" || what == "class_method_desc") {
4254 section = vformat("#class-%s-method-%s", clss, name);
4255 } else if (what == "class_property") {
4256 section = vformat("#class-%s-property-%s", clss, name);
4257 } else if (what == "class_enum") {
4258 section = vformat("#enum-%s-%s", clss, name);
4259 } else if (what == "class_theme_item") {
4260 section = vformat("#class-%s-theme-%s", clss, name);
4261 } else if (what == "class_constant") {
4262 section = vformat("#class-%s-constant-%s", clss, name);
4263 } else if (what == "class_annotation") {
4264 section = vformat("#%s", clss);
4265 }
4266
4267 String doc_url = clss.is_empty() ? String(GODOT_VERSION_DOCS_URL "/") : vformat(GODOT_VERSION_DOCS_URL "/classes/class_%s.html%s", clss, section);
4268 OS::get_singleton()->shell_open(doc_url);
4269 }
4270
4271 void EditorHelpBit::_go_to_help(const String &p_what) {
4272 if (ScriptEditor::get_singleton()) {
4273 EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
4274 ScriptEditor::get_singleton()->goto_help(p_what);
4275 } else {
4276 _go_to_url(p_what);
4277 }
4278 emit_signal(SNAME("request_hide"));
4279 }
4280
4281 void EditorHelpBit::_meta_clicked(const String &p_select) {
4282 if (p_select.begins_with("$")) { // Enum.
4283 const String link = p_select.substr(1);
4284
4285 String enum_class_name;
4286 String enum_name;
4287 if (CoreConstants::is_global_enum(link)) {
4288 enum_class_name = "@GlobalScope";
4289 enum_name = link;
4290 } else {
4291 const int dot_pos = link.rfind_char('.');
4292 if (dot_pos >= 0) {
4293 enum_class_name = link.left(dot_pos);
4294 enum_name = link.substr(dot_pos + 1);
4295 } else {
4296 enum_class_name = symbol_class_name;
4297 enum_name = link;
4298 }
4299 }
4300
4301 _go_to_help("class_enum:" + enum_class_name + ":" + enum_name);
4302 } else if (p_select.begins_with("#")) { // Class.
4303 _go_to_help("class_name:" + p_select.substr(1));
4304 } else if (p_select.begins_with("@")) { // Member.
4305 const int tag_end = p_select.find_char(' ');
4306 const String tag = p_select.substr(1, tag_end - 1);
4307 const String link = p_select.substr(tag_end + 1).lstrip(" ");
4308
4309 String topic;
4310 if (tag == "method") {
4311 topic = "class_method";
4312 } else if (tag == "constructor") {
4313 topic = "class_method";
4314 } else if (tag == "operator") {
4315 topic = "class_method";
4316 } else if (tag == "member") {
4317 topic = "class_property";
4318 } else if (tag == "enum") {
4319 topic = "class_enum";
4320 } else if (tag == "signal") {
4321 topic = "class_signal";
4322 } else if (tag == "constant") {
4323 topic = "class_constant";
4324 } else if (tag == "annotation") {
4325 topic = "class_annotation";
4326 } else if (tag == "theme_item") {
4327 topic = "class_theme_item";
4328 } else {
4329 return;
4330 }
4331
4332 if (topic == "class_enum") {
4333 const String enum_link = link.trim_prefix("@GlobalScope.");
4334 if (CoreConstants::is_global_enum(enum_link)) {
4335 _go_to_help(topic + ":@GlobalScope:" + enum_link);
4336 return;
4337 }
4338 } else if (topic == "class_constant") {
4339 if (CoreConstants::is_global_constant(link)) {
4340 _go_to_help(topic + ":@GlobalScope:" + link);
4341 return;
4342 }
4343 }
4344
4345 if (link.contains_char('.')) {
4346 const int class_end = link.rfind_char('.');
4347 _go_to_help(topic + ":" + link.left(class_end) + ":" + link.substr(class_end + 1));
4348 } else {
4349 _go_to_help(topic + ":" + symbol_class_name + ":" + link);
4350 }
4351 } else if (p_select.begins_with("open-file:")) {
4352 String path = ProjectSettings::get_singleton()->globalize_path(p_select.trim_prefix("open-file:"));
4353 OS::get_singleton()->shell_show_in_file_manager(path, true);
4354 } else if (p_select.begins_with("open-res:")) {
4355 EditorNode::get_singleton()->load_scene_or_resource(p_select.trim_prefix("open-res:"));
4356 } else if (p_select.begins_with("show:")) {
4357 FileSystemDock::get_singleton()->navigate_to_path(p_select.trim_prefix("show:"));
4358 } else if (p_select.begins_with("http:") || p_select.begins_with("https:")) {
4359 OS::get_singleton()->shell_open(p_select);
4360 } else if (p_select.begins_with("^")) { // Copy button.
4361 DisplayServer::get_singleton()->clipboard_set(p_select.substr(1));
4362 }
4363 }
4364
4365 void EditorHelpBit::_bind_methods() {
4366 ADD_SIGNAL(MethodInfo("request_hide"));
4367 }
4368
4369 void EditorHelpBit::_notification(int p_what) {
4370 switch (p_what) {
4371 case NOTIFICATION_THEME_CHANGED:
4372 _update_labels();
4373 break;
4374 }
4375 }
4376
4377 void EditorHelpBit::parse_symbol(const String &p_symbol, const String &p_prologue) {
4378 const PackedStringArray slices = p_symbol.split("|", true, 3);
4379 ERR_FAIL_COND_MSG(slices.size() < 3, R"(Invalid doc id: The expected format is "item_type|class_name|item_name[|item_data]".)");
4380
4381 const String &item_type = slices[0];
4382 const String &class_name = slices[1];
4383 const String &item_name = slices[2];
4384
4385 Dictionary item_data;
4386 if (slices.size() > 3) {
4387 item_data = JSON::parse_string(slices[3]);
4388 }
4389
4390 symbol_doc_link = String();
4391 symbol_class_name = class_name;
4392 symbol_type = String();
4393 symbol_name = item_name;
4394 symbol_hint = SYMBOL_HINT_NONE;
4395 help_data = HelpData();
4396
4397 if (item_type == "class") {
4398 symbol_doc_link = vformat("#%s", class_name);
4399 symbol_type = TTR("Class");
4400 symbol_name = class_name;
4401 symbol_hint = SYMBOL_HINT_INHERITANCE;
4402 help_data = _get_class_help_data(class_name);
4403 } else if (item_type == "enum") {
4404 symbol_doc_link = vformat("$%s.%s", class_name, item_name);
4405 symbol_type = TTR("Enumeration");
4406 help_data = _get_enum_help_data(class_name, item_name);
4407 } else if (item_type == "constant") {
4408 symbol_doc_link = vformat("@constant %s.%s", class_name, item_name);
4409 symbol_type = TTR("Constant");
4410 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4411 help_data = _get_constant_help_data(class_name, item_name);
4412 } else if (item_type == "property") {
4413 if (item_name.begins_with("metadata/")) {
4414 symbol_type = TTR("Metadata");
4415 symbol_name = item_name.trim_prefix("metadata/");
4416 } else if (class_name == "ProjectSettings" || class_name == "EditorSettings") {
4417 symbol_doc_link = vformat("@member %s.%s", class_name, item_name);
4418 symbol_type = TTR("Setting");
4419 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4420 } else {
4421 symbol_doc_link = vformat("@member %s.%s", class_name, item_name);
4422 symbol_type = TTR("Property");
4423 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4424 }
4425 help_data = _get_property_help_data(class_name, item_name);
4426
4427 // Add copy note to built-in properties returning `Packed*Array`.
4428 const DocData::ClassDoc *cd = EditorHelp::get_doc(class_name);
4429 if (cd && !cd->is_script_doc && packed_array_types.has(help_data.doc_type.type)) {
4430 if (!help_data.description.is_empty()) {
4431 help_data.description += "\n";
4432 }
4433 // See also `EditorHelp::_update_doc()` and `doc/tools/make_rst.py`.
4434 help_data.description += vformat(TTR("[b]Note:[/b] The returned array is [i]copied[/i] and any changes to it will not update the original property value. See [%s] for more details."), help_data.doc_type.type);
4435 }
4436 } else if (item_type == "internal_property") {
4437 symbol_type = TTR("Internal Property");
4438 help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This property can only be set in the Inspector.") + "[/i][/color]";
4439 } else if (item_type == "theme_item") {
4440 symbol_doc_link = vformat("@theme_item %s.%s", class_name, item_name);
4441 symbol_type = TTR("Theme Property");
4442 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4443 help_data = _get_theme_item_help_data(class_name, item_name);
4444 } else if (item_type == "method") {
4445 symbol_doc_link = vformat("@method %s.%s", class_name, item_name);
4446 symbol_type = TTR("Method");
4447 symbol_hint = SYMBOL_HINT_SIGNATURE;
4448 help_data = _get_method_help_data(class_name, item_name);
4449 } else if (item_type == "signal") {
4450 symbol_doc_link = vformat("@signal %s.%s", class_name, item_name);
4451 symbol_type = TTR("Signal");
4452 symbol_hint = SYMBOL_HINT_SIGNATURE;
4453 help_data = _get_signal_help_data(class_name, item_name);
4454 } else if (item_type == "annotation") {
4455 symbol_doc_link = vformat("@annotation %s.%s", class_name, item_name);
4456 symbol_type = TTR("Annotation");
4457 symbol_hint = SYMBOL_HINT_SIGNATURE;
4458 help_data = _get_annotation_help_data(class_name, item_name);
4459 } else if (item_type == "local_constant" || item_type == "local_variable") {
4460 symbol_type = (item_type == "local_constant") ? TTR("Local Constant") : TTR("Local Variable");
4461 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4462 help_data.description = item_data.get("description", "").operator String().strip_edges();
4463 if (item_data.get("is_deprecated", false)) {
4464 const String deprecated_message = item_data.get("deprecated_message", "").operator String().strip_edges();
4465 if (deprecated_message.is_empty()) {
4466 if (item_type == "local_constant") {
4467 help_data.deprecated_message = TTR("This constant may be changed or removed in future versions.");
4468 } else {
4469 help_data.deprecated_message = TTR("This variable may be changed or removed in future versions.");
4470 }
4471 } else {
4472 help_data.deprecated_message = deprecated_message;
4473 }
4474 }
4475 if (item_data.get("is_experimental", false)) {
4476 const String experimental_message = item_data.get("experimental_message", "").operator String().strip_edges();
4477 if (experimental_message.is_empty()) {
4478 if (item_type == "local_constant") {
4479 help_data.experimental_message = TTR("This constant may be changed or removed in future versions.");
4480 } else {
4481 help_data.experimental_message = TTR("This variable may be changed or removed in future versions.");
4482 }
4483 } else {
4484 help_data.experimental_message = experimental_message;
4485 }
4486 }
4487 help_data.doc_type.type = item_data.get("doc_type", "");
4488 help_data.doc_type.enumeration = item_data.get("enumeration", "");
4489 help_data.doc_type.is_bitfield = item_data.get("is_bitfield", false);
4490 help_data.value = item_data.get("value", "");
4491 } else if (item_type == "resource") {
4492 String path = item_name.simplify_path();
4493 const bool is_uid = path.begins_with("uid://");
4494 if (is_uid) {
4495 if (ResourceUID::get_singleton()->has_id(ResourceUID::get_singleton()->text_to_id(path))) {
4496 path = ResourceUID::uid_to_path(path);
4497 } else {
4498 path = "";
4499 }
4500 }
4501 help_data.resource_path = path;
4502
4503 Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
4504 if (da->file_exists(path)) {
4505 help_data.doc_type.type = ResourceLoader::get_resource_type(path);
4506 if (help_data.doc_type.type.is_empty()) {
4507 const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
4508 symbol_type = textfile_ext.has(path.get_extension()) ? TTR("Text File") : TTR("File");
4509 } else {
4510 symbol_type = TTR("Resource");
4511 symbol_hint = SYMBOL_HINT_ASSIGNABLE;
4512 if (is_uid) {
4513 help_data.description = vformat("%s: [color=<EditorHelpBitCommentColor>]%s[/color]", TTR("Path"), path);
4514 }
4515 }
4516 symbol_name = path.get_file();
4517 } else if (!is_uid && da->dir_exists(path)) {
4518 symbol_type = TTR("Directory");
4519 symbol_name = path;
4520 } else {
4521 help_data.resource_path = "";
4522 symbol_name = "";
4523 if (is_uid) {
4524 symbol_type = TTR("Invalid UID");
4525 help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This UID does not point to any valid Resource.") + "[/i][/color]";
4526 } else {
4527 symbol_type = TTR("Invalid path");
4528 help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This path does not exist.") + "[/i][/color]";
4529 }
4530 }
4531 } else {
4532 ERR_FAIL_MSG("Invalid doc id: Unknown item type " + item_type.quote() + ".");
4533 }
4534
4535 // Do not add links for custom or undocumented symbols.
4536 if (symbol_class_name.is_empty() || (help_data.description.is_empty() && help_data.deprecated_message.is_empty() && help_data.experimental_message.is_empty())) {
4537 symbol_doc_link = String();
4538 }
4539
4540 if (!p_prologue.is_empty()) {
4541 if (help_data.description.is_empty()) {
4542 help_data.description = p_prologue;
4543 } else {
4544 help_data.description = p_prologue + "\n" + help_data.description;
4545 }
4546 }
4547
4548 if (help_data.description.is_empty() && item_type != "resource") {
4549 help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("No description available.") + "[/i][/color]";
4550 }
4551
4552 if (is_inside_tree()) {
4553 _update_labels();
4554 }
4555 }
4556
4557 void EditorHelpBit::set_custom_text(const String &p_type, const String &p_name, const String &p_description) {
4558 symbol_doc_link = String();
4559 symbol_class_name = String();
4560 symbol_type = p_type;
4561 symbol_name = p_name;
4562 symbol_hint = SYMBOL_HINT_NONE;
4563
4564 help_data = HelpData();
4565 help_data.description = p_description;
4566
4567 if (is_inside_tree()) {
4568 _update_labels();
4569 }
4570 }
4571
4572 void EditorHelpBit::set_content_height_limits(float p_min, float p_max) {
4573 ERR_FAIL_COND(p_min > p_max);
4574 content_min_height = p_min;
4575 content_max_height = p_max;
4576
4577 if (is_inside_tree()) {
4578 update_content_height();
4579 }
4580 }
4581
4582 void EditorHelpBit::update_content_height() {
4583 float content_height = content->get_content_height();
4584 const Ref<StyleBox> style = content->get_theme_stylebox(CoreStringName(normal));
4585 if (style.is_valid()) {
4586 content_height += style->get_content_margin(SIDE_TOP) + style->get_content_margin(SIDE_BOTTOM);
4587 }
4588 content->set_custom_minimum_size(Size2(content->get_custom_minimum_size().x, CLAMP(content_height, content_min_height, content_max_height)));
4589 }
4590
4591 EditorHelpBit::EditorHelpBit(const String &p_symbol, const String &p_prologue, bool p_use_class_prefix, bool p_allow_selection, bool p_in_tooltip) {
4592 add_theme_constant_override("separation", 0);
4593
4594 title = memnew(RichTextLabel);
4595 title->set_theme_type_variation(p_in_tooltip ? "EditorHelpBitTooltipTitle" : "EditorHelpBitTitle");
4596 title->set_custom_minimum_size(Size2(640 * EDSCALE, 0)); // GH-93031. Set the minimum width even if `fit_content` is true.
4597 title->set_fit_content(true);
4598 title->set_selection_enabled(p_allow_selection);
4599 title->set_context_menu_enabled(p_allow_selection);
4600 title->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
4601 title->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
4602 title->hide();
4603 add_child(title);
4604
4605 content_min_height = 48 * EDSCALE;
4606 content_max_height = 360 * EDSCALE;
4607
4608 content = memnew(RichTextLabel);
4609 content->set_theme_type_variation(p_in_tooltip ? "EditorHelpBitTooltipContent" : "EditorHelpBitContent");
4610 content->set_custom_minimum_size(Size2(640 * EDSCALE, content_min_height));
4611 content->set_v_size_flags(Control::SIZE_EXPAND_FILL);
4612 content->set_selection_enabled(p_allow_selection);
4613 content->set_context_menu_enabled(p_allow_selection);
4614 content->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
4615 content->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
4616 add_child(content);
4617
4618 use_class_prefix = p_use_class_prefix;
4619
4620 if (!p_symbol.is_empty()) {
4621 parse_symbol(p_symbol, p_prologue);
4622 } else if (!p_prologue.is_empty()) {
4623 set_custom_text(String(), String(), p_prologue);
4624 }
4625 }
4626
4627 /// EditorHelpBitTooltip ///
4628
4629 bool EditorHelpBitTooltip::_is_tooltip_visible = false;
4630
4631 Control *EditorHelpBitTooltip::_make_invisible_control() {
4632 Control *control = memnew(Control);
4633 control->set_visible(false);
4634 return control;
4635 }
4636
4637 void EditorHelpBitTooltip::_start_timer() {
4638 if (timer->is_inside_tree() && timer->is_stopped()) {
4639 timer->start();
4640 }
4641 }
4642
4643 void EditorHelpBitTooltip::_target_gui_input(const Ref<InputEvent> &p_event) {
4644 // Only scrolling is not checked in `NOTIFICATION_INTERNAL_PROCESS`.
4645 const Ref<InputEventMouseButton> mb = p_event;
4646 if (mb.is_valid()) {
4647 switch (mb->get_button_index()) {
4648 case MouseButton::WHEEL_UP:
4649 case MouseButton::WHEEL_DOWN:
4650 case MouseButton::WHEEL_LEFT:
4651 case MouseButton::WHEEL_RIGHT:
4652 queue_free();
4653 break;
4654 default:
4655 break;
4656 }
4657 }
4658 }
4659
4660 void EditorHelpBitTooltip::_notification(int p_what) {
4661 switch (p_what) {
4662 case NOTIFICATION_ENTER_TREE:
4663 _is_tooltip_visible = true;
4664 _enter_tree_time = OS::get_singleton()->get_ticks_msec();
4665 break;
4666 case NOTIFICATION_EXIT_TREE:
4667 _is_tooltip_visible = false;
4668 break;
4669 case NOTIFICATION_WM_MOUSE_ENTER:
4670 _is_mouse_inside_tooltip = true;
4671 timer->stop();
4672 break;
4673 case NOTIFICATION_WM_MOUSE_EXIT:
4674 _is_mouse_inside_tooltip = false;
4675 _start_timer();
4676 break;
4677 case NOTIFICATION_INTERNAL_PROCESS:
4678 // A workaround to hide the tooltip since the window does not receive keyboard events
4679 // with `FLAG_POPUP` and `FLAG_NO_FOCUS` flags, so we can't use `_input_from_window()`.
4680 if (is_inside_tree()) {
4681 if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"), true)) {
4682 queue_free();
4683 get_parent_viewport()->set_input_as_handled();
4684 } else if (Input::get_singleton()->is_anything_pressed_except_mouse()) {
4685 queue_free();
4686 } else if (!Input::get_singleton()->get_mouse_button_mask().is_empty()) {
4687 if (!_is_mouse_inside_tooltip) {
4688 queue_free();
4689 }
4690 } else if (!Input::get_singleton()->get_last_mouse_velocity().is_zero_approx()) {
4691 if (!_is_mouse_inside_tooltip && OS::get_singleton()->get_ticks_msec() - _enter_tree_time > 350) {
4692 _start_timer();
4693 }
4694 }
4695 }
4696 break;
4697 }
4698 }
4699
4700 Control *EditorHelpBitTooltip::make_tooltip(Control *p_target, const String &p_symbol, const String &p_prologue, bool p_use_class_prefix) {
4701 ERR_FAIL_NULL_V(p_target, _make_invisible_control());
4702
4703 // Show the custom tooltip only if it is not already visible.
4704 // The viewport will retrigger `make_custom_tooltip()` every few seconds
4705 // because the return control is not visible even if the custom tooltip is displayed.
4706 if (_is_tooltip_visible || Input::get_singleton()->is_anything_pressed()) {
4707 return _make_invisible_control();
4708 }
4709
4710 EditorHelpBit *help_bit = memnew(EditorHelpBit(p_symbol, p_prologue, p_use_class_prefix, false, true));
4711
4712 EditorHelpBitTooltip *tooltip = memnew(EditorHelpBitTooltip(p_target));
4713 help_bit->connect("request_hide", callable_mp(static_cast<Node *>(tooltip), &Node::queue_free));
4714 tooltip->add_child(help_bit);
4715 p_target->add_child(tooltip);
4716
4717 help_bit->update_content_height();
4718 tooltip->popup_under_cursor();
4719
4720 return _make_invisible_control();
4721 }
4722
4723 // Copy-paste from `Viewport::_gui_show_tooltip()`.
4724 void EditorHelpBitTooltip::popup_under_cursor() {
4725 Point2 mouse_pos = get_mouse_position();
4726 Point2 tooltip_offset = GLOBAL_GET_CACHED(Point2, "display/mouse_cursor/tooltip_position_offset");
4727 Rect2 r(mouse_pos + tooltip_offset, get_contents_minimum_size());
4728 r.size = r.size.min(get_max_size());
4729
4730 Window *window = get_parent_visible_window();
4731 Rect2i vr;
4732 if (is_embedded()) {
4733 vr = get_embedder()->get_visible_rect();
4734 } else {
4735 vr = window->get_usable_parent_rect();
4736 }
4737
4738 if (!DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_SELF_FITTING_WINDOWS) || is_embedded()) {
4739 if (r.size.x + r.position.x > vr.size.x + vr.position.x) {
4740 // Place it in the opposite direction. If it fails, just hug the border.
4741 r.position.x = mouse_pos.x - r.size.x - tooltip_offset.x;
4742
4743 if (r.position.x < vr.position.x) {
4744 r.position.x = vr.position.x + vr.size.x - r.size.x;
4745 }
4746 } else if (r.position.x < vr.position.x) {
4747 r.position.x = vr.position.x;
4748 }
4749
4750 if (r.size.y + r.position.y > vr.size.y + vr.position.y) {
4751 // Same as above.
4752 r.position.y = mouse_pos.y - r.size.y - tooltip_offset.y;
4753
4754 if (r.position.y < vr.position.y) {
4755 r.position.y = vr.position.y + vr.size.y - r.size.y;
4756 }
4757 } else if (r.position.y < vr.position.y) {
4758 r.position.y = vr.position.y;
4759 }
4760 }
4761
4762 // When `FLAG_POPUP` is false, it prevents the editor from losing focus when displaying the tooltip.
4763 // This way, clicks and double-clicks are still available outside the tooltip.
4764 set_flag(Window::FLAG_POPUP, false);
4765 set_flag(Window::FLAG_NO_FOCUS, true);
4766 popup(r);
4767 }
4768
4769 EditorHelpBitTooltip::EditorHelpBitTooltip(Control *p_target) {
4770 ERR_FAIL_NULL(p_target);
4771
4772 set_theme_type_variation("TooltipPanel");
4773
4774 timer = memnew(Timer);
4775 timer->set_wait_time(0.25);
4776 timer->connect("timeout", callable_mp(static_cast<Node *>(this), &Node::queue_free));
4777 add_child(timer);
4778
4779 p_target->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorHelpBitTooltip::_start_timer));
4780 p_target->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelpBitTooltip::_target_gui_input));
4781
4782 set_process_internal(true);
4783 }
4784
4785 /// EditorHelpHighlighter ///
4786
4787 EditorHelpHighlighter *EditorHelpHighlighter::singleton = nullptr;
4788
4789 void EditorHelpHighlighter::create_singleton() {
4790 ERR_FAIL_COND(singleton != nullptr);
4791 singleton = memnew(EditorHelpHighlighter);
4792 }
4793
4794 void EditorHelpHighlighter::free_singleton() {
4795 ERR_FAIL_NULL(singleton);
4796 memdelete(singleton);
4797 singleton = nullptr;
4798 }
4799
4800 EditorHelpHighlighter *EditorHelpHighlighter::get_singleton() {
4801 return singleton;
4802 }
4803
4804 EditorHelpHighlighter::HighlightData EditorHelpHighlighter::_get_highlight_data(Language p_language, const String &p_source, bool p_use_cache) {
4805 switch (p_language) {
4806 case LANGUAGE_GDSCRIPT:
4807 #ifndef MODULE_GDSCRIPT_ENABLED
4808 ERR_FAIL_V_MSG(HighlightData(), "GDScript module is disabled.");
4809 #endif
4810 break;
4811 case LANGUAGE_CSHARP:
4812 #ifndef MODULE_MONO_ENABLED
4813 ERR_FAIL_V_MSG(HighlightData(), "Mono module is disabled.");
4814 #endif
4815 break;
4816 default:
4817 ERR_FAIL_V_MSG(HighlightData(), "Invalid parameter \"p_language\".");
4818 }
4819
4820 if (p_use_cache) {
4821 const HashMap<String, HighlightData>::ConstIterator E = highlight_data_caches[p_language].find(p_source);
4822 if (E) {
4823 return E->value;
4824 }
4825 }
4826
4827 text_edits[p_language]->set_text(p_source);
4828 if (scripts[p_language].is_valid()) { // See GH-89610.
4829 scripts[p_language]->set_source_code(p_source);
4830 }
4831 highlighters[p_language]->_update_cache();
4832
4833 HighlightData result;
4834
4835 int source_offset = 0;
4836 int result_index = 0;
4837 for (int i = 0; i < text_edits[p_language]->get_line_count(); i++) {
4838 const Dictionary dict = highlighters[p_language]->_get_line_syntax_highlighting_impl(i);
4839
4840 result.resize(result.size() + dict.size());
4841
4842 const Variant *key = nullptr;
4843 int prev_column = -1;
4844 while ((key = dict.next(key)) != nullptr) {
4845 const int column = *key;
4846 ERR_FAIL_COND_V(column <= prev_column, HighlightData());
4847 prev_column = column;
4848
4849 const Color color = dict[*key].operator Dictionary().get("color", Color());
4850
4851 result.write[result_index] = { source_offset + column, color };
4852 result_index++;
4853 }
4854
4855 source_offset += text_edits[p_language]->get_line(i).length() + 1; // Plus newline.
4856 }
4857
4858 if (p_use_cache) {
4859 highlight_data_caches[p_language][p_source] = result;
4860 }
4861
4862 return result;
4863 }
4864
4865 void EditorHelpHighlighter::highlight(RichTextLabel *p_rich_text_label, Language p_language, const String &p_source, bool p_use_cache) {
4866 ERR_FAIL_NULL(p_rich_text_label);
4867
4868 const HighlightData highlight_data = _get_highlight_data(p_language, p_source, p_use_cache);
4869
4870 if (!highlight_data.is_empty()) {
4871 for (int i = 1; i < highlight_data.size(); i++) {
4872 const Pair<int, Color> &prev = highlight_data[i - 1];
4873 const Pair<int, Color> &curr = highlight_data[i];
4874 p_rich_text_label->push_color(prev.second);
4875 p_rich_text_label->add_text(p_source.substr(prev.first, curr.first - prev.first));
4876 p_rich_text_label->pop(); // color
4877 }
4878
4879 const Pair<int, Color> &last = highlight_data[highlight_data.size() - 1];
4880 p_rich_text_label->push_color(last.second);
4881 p_rich_text_label->add_text(p_source.substr(last.first));
4882 p_rich_text_label->pop(); // color
4883 }
4884 }
4885
4886 void EditorHelpHighlighter::reset_cache() {
4887 const Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");
4888
4889 #ifdef MODULE_GDSCRIPT_ENABLED
4890 highlight_data_caches[LANGUAGE_GDSCRIPT].clear();
4891 text_edits[LANGUAGE_GDSCRIPT]->add_theme_color_override(SceneStringName(font_color), text_color);
4892 #endif
4893
4894 #ifdef MODULE_MONO_ENABLED
4895 highlight_data_caches[LANGUAGE_CSHARP].clear();
4896 text_edits[LANGUAGE_CSHARP]->add_theme_color_override(SceneStringName(font_color), text_color);
4897 #endif
4898 }
4899
4900 EditorHelpHighlighter::EditorHelpHighlighter() {
4901 const Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");
4902
4903 #ifdef MODULE_GDSCRIPT_ENABLED
4904 TextEdit *gdscript_text_edit = memnew(TextEdit);
4905 gdscript_text_edit->add_theme_color_override(SceneStringName(font_color), text_color);
4906
4907 Ref<GDScript> gdscript;
4908 gdscript.instantiate();
4909
4910 Ref<GDScriptSyntaxHighlighter> gdscript_highlighter;
4911 gdscript_highlighter.instantiate();
4912 gdscript_highlighter->set_text_edit(gdscript_text_edit);
4913 gdscript_highlighter->_set_edited_resource(gdscript);
4914
4915 text_edits[LANGUAGE_GDSCRIPT] = gdscript_text_edit;
4916 scripts[LANGUAGE_GDSCRIPT] = gdscript;
4917 highlighters[LANGUAGE_GDSCRIPT] = gdscript_highlighter;
4918 #endif
4919
4920 #ifdef MODULE_MONO_ENABLED
4921 TextEdit *csharp_text_edit = memnew(TextEdit);
4922 csharp_text_edit->add_theme_color_override(SceneStringName(font_color), text_color);
4923
4924 // See GH-89610.
4925 //Ref<CSharpScript> csharp;
4926 //csharp.instantiate();
4927
4928 Ref<EditorStandardSyntaxHighlighter> csharp_highlighter;
4929 csharp_highlighter.instantiate();
4930 csharp_highlighter->set_text_edit(csharp_text_edit);
4931 //csharp_highlighter->_set_edited_resource(csharp);
4932 csharp_highlighter->_set_script_language(CSharpLanguage::get_singleton());
4933
4934 text_edits[LANGUAGE_CSHARP] = csharp_text_edit;
4935 //scripts[LANGUAGE_CSHARP] = csharp;
4936 highlighters[LANGUAGE_CSHARP] = csharp_highlighter;
4937 #endif
4938 }
4939
4940 EditorHelpHighlighter::~EditorHelpHighlighter() {
4941 #ifdef MODULE_GDSCRIPT_ENABLED
4942 memdelete(text_edits[LANGUAGE_GDSCRIPT]);
4943 #endif
4944
4945 #ifdef MODULE_MONO_ENABLED
4946 memdelete(text_edits[LANGUAGE_CSHARP]);
4947 #endif
4948 }
4949
4950 /// FindBar ///
4951
4952 FindBar::FindBar() {
4953 search_text = memnew(LineEdit);
4954 search_text->set_keep_editing_on_text_submit(true);
4955 add_child(search_text);
4956 search_text->set_placeholder(TTR("Search"));
4957 search_text->set_tooltip_text(TTR("Search"));
4958 search_text->set_accessibility_name(TTRC("Search Documentation"));
4959 search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
4960 search_text->set_h_size_flags(SIZE_EXPAND_FILL);
4961 search_text->connect(SceneStringName(text_changed), callable_mp(this, &FindBar::_search_text_changed));
4962 search_text->connect(SceneStringName(text_submitted), callable_mp(this, &FindBar::_search_text_submitted));
4963
4964 matches_label = memnew(Label);
4965 add_child(matches_label);
4966 matches_label->set_focus_mode(FOCUS_ACCESSIBILITY);
4967 matches_label->hide();
4968
4969 find_prev = memnew(Button);
4970 find_prev->set_theme_type_variation(SceneStringName(FlatButton));
4971 find_prev->set_disabled(results_count < 1);
4972 find_prev->set_tooltip_text(TTR("Previous Match"));
4973 add_child(find_prev);
4974 find_prev->set_focus_mode(FOCUS_ACCESSIBILITY);
4975 find_prev->connect(SceneStringName(pressed), callable_mp(this, &FindBar::search_prev));
4976
4977 find_next = memnew(Button);
4978 find_next->set_theme_type_variation(SceneStringName(FlatButton));
4979 find_next->set_disabled(results_count < 1);
4980 find_next->set_tooltip_text(TTR("Next Match"));
4981 add_child(find_next);
4982 find_next->set_focus_mode(FOCUS_ACCESSIBILITY);
4983 find_next->connect(SceneStringName(pressed), callable_mp(this, &FindBar::search_next));
4984
4985 hide_button = memnew(Button);
4986 hide_button->set_theme_type_variation(SceneStringName(FlatButton));
4987 hide_button->set_tooltip_text(TTR("Hide"));
4988 hide_button->set_focus_mode(FOCUS_ACCESSIBILITY);
4989 hide_button->connect(SceneStringName(pressed), callable_mp(this, &FindBar::_hide_bar));
4990 hide_button->set_v_size_flags(SIZE_EXPAND_FILL);
4991 add_child(hide_button);
4992 }
4993
4994 void FindBar::popup_search() {
4995 show();
4996 bool grabbed_focus = false;
4997 if (!search_text->has_focus()) {
4998 search_text->grab_focus();
4999 grabbed_focus = true;
5000 }
5001
5002 if (!search_text->get_text().is_empty()) {
5003 search_text->select_all();
5004 search_text->set_caret_column(search_text->get_text().length());
5005 if (grabbed_focus) {
5006 rich_text_label->deselect();
5007 results_count_to_current = 0;
5008 _search();
5009 }
5010 }
5011 }
5012
5013 void FindBar::_notification(int p_what) {
5014 switch (p_what) {
5015 case NOTIFICATION_THEME_CHANGED: {
5016 find_prev->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
5017 find_next->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
5018 hide_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
5019 matches_label->add_theme_color_override(SceneStringName(font_color), results_count > 0 ? get_theme_color(SceneStringName(font_color), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
5020 } break;
5021
5022 case NOTIFICATION_VISIBILITY_CHANGED: {
5023 set_process_input(is_visible_in_tree());
5024 } break;
5025 }
5026 }
5027
5028 void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
5029 rich_text_label = p_rich_text_label;
5030 }
5031
5032 bool FindBar::search_next() {
5033 return _search();
5034 }
5035
5036 bool FindBar::search_prev() {
5037 return _search(true);
5038 }
5039
5040 bool FindBar::_search(bool p_search_previous) {
5041 String stext = search_text->get_text();
5042 bool keep = prev_search == stext;
5043 bool ret = rich_text_label->search(stext, keep, p_search_previous);
5044
5045 prev_search = stext;
5046 if (!keep) {
5047 results_count_to_current = 0;
5048 }
5049
5050 if (ret) {
5051 _update_results_count(p_search_previous);
5052 } else {
5053 results_count = 0;
5054 results_count_to_current = 0;
5055 }
5056
5057 if (results_count == 1) {
5058 rich_text_label->scroll_to_selection();
5059 }
5060
5061 _update_matches_label();
5062
5063 return ret;
5064 }
5065
5066 void FindBar::_update_results_count(bool p_search_previous) {
5067 results_count = 0;
5068
5069 String searched = search_text->get_text();
5070 if (searched.is_empty()) {
5071 return;
5072 }
5073
5074 String full_text = rich_text_label->get_parsed_text();
5075
5076 int from_pos = 0;
5077
5078 while (true) {
5079 int pos = full_text.findn(searched, from_pos);
5080 if (pos == -1) {
5081 break;
5082 }
5083
5084 results_count++;
5085 from_pos = pos + searched.length();
5086 }
5087
5088 results_count_to_current += (p_search_previous) ? -1 : 1;
5089 if (results_count_to_current > results_count) {
5090 results_count_to_current = results_count_to_current - results_count;
5091 } else if (results_count_to_current <= 0) {
5092 results_count_to_current = results_count;
5093 }
5094 }
5095
5096 void FindBar::_update_matches_label() {
5097 if (search_text->get_text().is_empty() || results_count == -1) {
5098 matches_label->hide();
5099 } else {
5100 matches_label->show();
5101
5102 matches_label->add_theme_color_override(SceneStringName(font_color), results_count > 0 ? get_theme_color(SceneStringName(font_color), SNAME("Label")) : get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
5103 if (results_count == 0) {
5104 matches_label->set_text(TTR("No match"));
5105 } else if (results_count_to_current == 0) {
5106 matches_label->set_text(vformat(TTRN("%d match", "%d matches", results_count), results_count));
5107 } else {
5108 matches_label->set_text(vformat(TTRN("%d of %d match", "%d of %d matches", results_count), results_count_to_current, results_count));
5109 }
5110 }
5111 find_prev->set_disabled(results_count < 1);
5112 find_next->set_disabled(results_count < 1);
5113 }
5114
5115 void FindBar::_hide_bar() {
5116 if (search_text->has_focus()) {
5117 rich_text_label->grab_focus();
5118 }
5119
5120 hide();
5121 }
5122
5123 // Implemented in input(..) as the LineEdit consumes the Escape pressed key.
5124 void FindBar::input(const Ref<InputEvent> &p_event) {
5125 ERR_FAIL_COND(p_event.is_null());
5126
5127 Ref<InputEventKey> k = p_event;
5128 if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
5129 Control *focus_owner = get_viewport()->gui_get_focus_owner();
5130
5131 if (rich_text_label->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
5132 _hide_bar();
5133 accept_event();
5134 }
5135 }
5136 }
5137
5138 void FindBar::_search_text_changed(const String &p_text) {
5139 search_next();
5140 }
5141
5142 void FindBar::_search_text_submitted(const String &p_text) {
5143 if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
5144 search_prev();
5145 } else {
5146 search_next();
5147 }
5148 }