editor_help.cpp 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753
  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. #include "editor_help.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/core_constants.h"
  33. #include "core/extension/gdextension.h"
  34. #include "core/input/input.h"
  35. #include "core/io/json.h"
  36. #include "core/object/script_language.h"
  37. #include "core/os/keyboard.h"
  38. #include "core/string/string_builder.h"
  39. #include "core/version_generated.gen.h"
  40. #include "editor/doc_data_compressed.gen.h"
  41. #include "editor/editor_main_screen.h"
  42. #include "editor/editor_node.h"
  43. #include "editor/editor_paths.h"
  44. #include "editor/editor_property_name_processor.h"
  45. #include "editor/editor_settings.h"
  46. #include "editor/editor_string_names.h"
  47. #include "editor/filesystem_dock.h"
  48. #include "editor/gui/editor_toaster.h"
  49. #include "editor/plugins/script_editor_plugin.h"
  50. #include "editor/themes/editor_scale.h"
  51. #include "scene/gui/line_edit.h"
  52. #include "modules/modules_enabled.gen.h" // For gdscript, mono.
  53. // For syntax highlighting.
  54. #ifdef MODULE_GDSCRIPT_ENABLED
  55. #include "modules/gdscript/editor/gdscript_highlighter.h"
  56. #include "modules/gdscript/gdscript.h"
  57. #endif
  58. // For syntax highlighting.
  59. #ifdef MODULE_MONO_ENABLED
  60. #include "editor/plugins/script_editor_plugin.h"
  61. #include "modules/mono/csharp_script.h"
  62. #endif
  63. #define CONTRIBUTE_URL vformat("%s/contributing/documentation/updating_the_class_reference.html", VERSION_DOCS_URL)
  64. #ifdef MODULE_MONO_ENABLED
  65. // Sync with the types mentioned in https://docs.godotengine.org/en/stable/tutorials/scripting/c_sharp/c_sharp_differences.html
  66. const Vector<String> classes_with_csharp_differences = {
  67. "@GlobalScope",
  68. "String",
  69. "NodePath",
  70. "Signal",
  71. "Callable",
  72. "RID",
  73. "Basis",
  74. "Transform2D",
  75. "Transform3D",
  76. "Rect2",
  77. "Rect2i",
  78. "AABB",
  79. "Quaternion",
  80. "Projection",
  81. "Color",
  82. "Array",
  83. "Dictionary",
  84. "PackedByteArray",
  85. "PackedColorArray",
  86. "PackedFloat32Array",
  87. "PackedFloat64Array",
  88. "PackedInt32Array",
  89. "PackedInt64Array",
  90. "PackedStringArray",
  91. "PackedVector2Array",
  92. "PackedVector3Array",
  93. "PackedVector4Array",
  94. "Variant",
  95. };
  96. #endif
  97. static const String nbsp = String::chr(160);
  98. static const String nbsp_equal_nbsp = nbsp + "=" + nbsp;
  99. static const String colon_nbsp = ":" + nbsp;
  100. const Vector<String> packed_array_types = {
  101. "PackedByteArray",
  102. "PackedColorArray",
  103. "PackedFloat32Array",
  104. "PackedFloat64Array",
  105. "PackedInt32Array",
  106. "PackedInt64Array",
  107. "PackedStringArray",
  108. "PackedVector2Array",
  109. "PackedVector3Array",
  110. "PackedVector4Array",
  111. };
  112. static String _replace_nbsp_with_space(const String &p_string) {
  113. return p_string.replace(nbsp, " ");
  114. }
  115. static String _fix_constant(const String &p_constant) {
  116. if (p_constant.strip_edges() == "4294967295") {
  117. return "0xFFFFFFFF";
  118. }
  119. if (p_constant.strip_edges() == "2147483647") {
  120. return "0x7FFFFFFF";
  121. }
  122. if (p_constant.strip_edges() == "1048575") {
  123. return "0xFFFFF";
  124. }
  125. return p_constant;
  126. }
  127. static void _add_qualifiers_to_rt(const String &p_qualifiers, RichTextLabel *p_rt) {
  128. for (const String &qualifier : p_qualifiers.split_spaces()) {
  129. String hint;
  130. if (qualifier == "vararg") {
  131. hint = TTR("This method supports a variable number of arguments.");
  132. } else if (qualifier == "virtual") {
  133. hint = TTR("This method is called by the engine.\nIt can be overridden to customize built-in behavior.");
  134. } else if (qualifier == "const") {
  135. hint = TTR("This method has no side effects.\nIt does not modify the object in any way.");
  136. } else if (qualifier == "static") {
  137. hint = TTR("This method does not need an instance to be called.\nIt can be called directly using the class name.");
  138. }
  139. p_rt->add_text(" ");
  140. if (hint.is_empty()) {
  141. p_rt->add_text(qualifier);
  142. } else {
  143. p_rt->push_hint(hint);
  144. p_rt->add_text(qualifier);
  145. p_rt->pop(); // hint
  146. }
  147. }
  148. }
  149. // Removes unnecessary prefix from `p_class_specifier` when within the `p_edited_class` context.
  150. static String _contextualize_class_specifier(const String &p_class_specifier, const String &p_edited_class) {
  151. // If this is a completely different context than the current class, then keep full path.
  152. if (!p_class_specifier.begins_with(p_edited_class)) {
  153. return p_class_specifier;
  154. }
  155. // Here equal `length()` and `begins_with()` from above implies `p_class_specifier == p_edited_class`.
  156. if (p_class_specifier.length() == p_edited_class.length()) {
  157. int rfind = p_class_specifier.rfind_char('.');
  158. if (rfind == -1) { // Single identifier.
  159. return p_class_specifier;
  160. }
  161. // Multiple specifiers: keep last one only.
  162. return p_class_specifier.substr(rfind + 1);
  163. }
  164. // They share a _name_ prefix but not a _class specifier_ prefix, e.g. `Tree` and `TreeItem`.
  165. // `begins_with()` and `length()`s being different implies `p_class_specifier.length() > p_edited_class.length()` so this is safe.
  166. if (p_class_specifier[p_edited_class.length()] != '.') {
  167. return p_class_specifier;
  168. }
  169. // Remove class specifier prefix.
  170. return p_class_specifier.substr(p_edited_class.length() + 1);
  171. }
  172. /// EditorHelp ///
  173. // TODO: This is sometimes used directly as `doc->something`, other times as `EditorHelp::get_doc_data()`, which is thread-safe.
  174. // Might this be a problem?
  175. DocTools *EditorHelp::doc = nullptr;
  176. DocTools *EditorHelp::ext_doc = nullptr;
  177. int EditorHelp::doc_generation_count = 0;
  178. String EditorHelp::doc_version_hash;
  179. Thread EditorHelp::worker_thread;
  180. static bool _attempt_doc_load(const String &p_class) {
  181. // Docgen always happens in the outer-most class: it also generates docs for inner classes.
  182. const String outer_class = p_class.get_slicec('.', 0);
  183. if (!ScriptServer::is_global_class(outer_class)) {
  184. return false;
  185. }
  186. // `ResourceLoader` is used in order to have a script-agnostic way to load scripts.
  187. // This forces GDScript to compile the code, which is unnecessary for docgen, but it's a good compromise right now.
  188. const Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(outer_class), outer_class);
  189. if (script.is_valid()) {
  190. const Vector<DocData::ClassDoc> docs = script->get_documentation();
  191. for (int j = 0; j < docs.size(); j++) {
  192. const DocData::ClassDoc &doc = docs.get(j);
  193. EditorHelp::get_doc_data()->add_doc(doc);
  194. }
  195. return true;
  196. }
  197. return false;
  198. }
  199. void EditorHelp::_update_theme_item_cache() {
  200. VBoxContainer::_update_theme_item_cache();
  201. theme_cache.text_color = get_theme_color(SNAME("text_color"), SNAME("EditorHelp"));
  202. theme_cache.title_color = get_theme_color(SNAME("title_color"), SNAME("EditorHelp"));
  203. theme_cache.headline_color = get_theme_color(SNAME("headline_color"), SNAME("EditorHelp"));
  204. theme_cache.comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
  205. theme_cache.symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
  206. theme_cache.value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
  207. theme_cache.qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
  208. theme_cache.type_color = get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  209. theme_cache.override_color = get_theme_color(SNAME("override_color"), SNAME("EditorHelp"));
  210. theme_cache.doc_font = get_theme_font(SNAME("doc"), EditorStringName(EditorFonts));
  211. theme_cache.doc_bold_font = get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
  212. theme_cache.doc_italic_font = get_theme_font(SNAME("doc_italic"), EditorStringName(EditorFonts));
  213. theme_cache.doc_title_font = get_theme_font(SNAME("doc_title"), EditorStringName(EditorFonts));
  214. theme_cache.doc_code_font = get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
  215. theme_cache.doc_kbd_font = get_theme_font(SNAME("doc_keyboard"), EditorStringName(EditorFonts));
  216. theme_cache.doc_font_size = get_theme_font_size(SNAME("doc_size"), EditorStringName(EditorFonts));
  217. theme_cache.doc_title_font_size = get_theme_font_size(SNAME("doc_title_size"), EditorStringName(EditorFonts));
  218. theme_cache.doc_code_font_size = get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
  219. theme_cache.doc_kbd_font_size = get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
  220. theme_cache.background_style = get_theme_stylebox(SNAME("background"), SNAME("EditorHelp"));
  221. class_desc->begin_bulk_theme_override();
  222. class_desc->add_theme_font_override("normal_font", theme_cache.doc_font);
  223. class_desc->add_theme_font_size_override("normal_font_size", theme_cache.doc_font_size);
  224. class_desc->add_theme_constant_override(SceneStringName(line_separation), get_theme_constant(SceneStringName(line_separation), SNAME("EditorHelp")));
  225. class_desc->add_theme_constant_override("table_h_separation", get_theme_constant(SNAME("table_h_separation"), SNAME("EditorHelp")));
  226. class_desc->add_theme_constant_override("table_v_separation", get_theme_constant(SNAME("table_v_separation"), SNAME("EditorHelp")));
  227. class_desc->add_theme_constant_override("text_highlight_h_padding", get_theme_constant(SNAME("text_highlight_h_padding"), SNAME("EditorHelp")));
  228. class_desc->add_theme_constant_override("text_highlight_v_padding", get_theme_constant(SNAME("text_highlight_v_padding"), SNAME("EditorHelp")));
  229. class_desc->end_bulk_theme_override();
  230. }
  231. void EditorHelp::_search(bool p_search_previous) {
  232. if (p_search_previous) {
  233. find_bar->search_prev();
  234. } else {
  235. find_bar->search_next();
  236. }
  237. }
  238. void EditorHelp::_class_desc_finished() {
  239. if (scroll_to >= 0) {
  240. class_desc->connect(SceneStringName(draw), callable_mp(class_desc, &RichTextLabel::scroll_to_paragraph).bind(scroll_to), CONNECT_ONE_SHOT | CONNECT_DEFERRED);
  241. }
  242. scroll_to = -1;
  243. }
  244. void EditorHelp::_class_list_select(const String &p_select) {
  245. _goto_desc(p_select);
  246. }
  247. void EditorHelp::_class_desc_select(const String &p_select) {
  248. if (p_select.begins_with("$")) { // Enum.
  249. const String link = p_select.substr(1);
  250. String enum_class_name;
  251. String enum_name;
  252. if (CoreConstants::is_global_enum(link)) {
  253. enum_class_name = "@GlobalScope";
  254. enum_name = link;
  255. } else {
  256. const int dot_pos = link.rfind_char('.');
  257. if (dot_pos >= 0) {
  258. enum_class_name = link.left(dot_pos);
  259. enum_name = link.substr(dot_pos + 1);
  260. } else {
  261. enum_class_name = edited_class;
  262. enum_name = link;
  263. }
  264. }
  265. emit_signal(SNAME("go_to_help"), "class_enum:" + enum_class_name + ":" + enum_name);
  266. } else if (p_select.begins_with("#")) { // Class.
  267. emit_signal(SNAME("go_to_help"), "class_name:" + p_select.substr(1));
  268. } else if (p_select.begins_with("@")) { // Member.
  269. const int tag_end = p_select.find_char(' ');
  270. const String tag = p_select.substr(1, tag_end - 1);
  271. const String link = p_select.substr(tag_end + 1).lstrip(" ");
  272. String topic;
  273. const HashMap<String, int> *table = nullptr;
  274. if (tag == "method") {
  275. topic = "class_method";
  276. table = &method_line;
  277. } else if (tag == "constructor") {
  278. topic = "class_method";
  279. table = &method_line;
  280. } else if (tag == "operator") {
  281. topic = "class_method";
  282. table = &method_line;
  283. } else if (tag == "member") {
  284. topic = "class_property";
  285. table = &property_line;
  286. } else if (tag == "enum") {
  287. topic = "class_enum";
  288. table = &enum_line;
  289. } else if (tag == "signal") {
  290. topic = "class_signal";
  291. table = &signal_line;
  292. } else if (tag == "constant") {
  293. topic = "class_constant";
  294. table = &constant_line;
  295. } else if (tag == "annotation") {
  296. topic = "class_annotation";
  297. table = &annotation_line;
  298. } else if (tag == "theme_item") {
  299. topic = "class_theme_item";
  300. table = &theme_property_line;
  301. } else {
  302. return;
  303. }
  304. // Case order is important here to correctly handle edge cases like `Variant.Type` in `@GlobalScope`.
  305. if (table->has(link)) {
  306. // Found in the current page.
  307. if (class_desc->is_finished()) {
  308. emit_signal(SNAME("request_save_history"));
  309. class_desc->scroll_to_paragraph((*table)[link]);
  310. } else {
  311. scroll_to = (*table)[link];
  312. }
  313. } else {
  314. // Look for link in `@GlobalScope`.
  315. if (topic == "class_enum") {
  316. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  317. const String enum_link = link.trim_prefix("@GlobalScope.");
  318. for (const DocData::ConstantDoc &constant : cd.constants) {
  319. if (constant.enumeration == enum_link) {
  320. // Found in `@GlobalScope`.
  321. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + enum_link);
  322. return;
  323. }
  324. }
  325. } else if (topic == "class_constant") {
  326. const DocData::ClassDoc &cd = doc->class_list["@GlobalScope"];
  327. for (const DocData::ConstantDoc &constant : cd.constants) {
  328. if (constant.name == link) {
  329. // Found in `@GlobalScope`.
  330. emit_signal(SNAME("go_to_help"), topic + ":@GlobalScope:" + link);
  331. return;
  332. }
  333. }
  334. }
  335. if (link.contains_char('.')) {
  336. const int class_end = link.rfind_char('.');
  337. emit_signal(SNAME("go_to_help"), topic + ":" + link.left(class_end) + ":" + link.substr(class_end + 1));
  338. }
  339. }
  340. } else if (p_select.begins_with("http:") || p_select.begins_with("https:")) {
  341. OS::get_singleton()->shell_open(p_select);
  342. } else if (p_select.begins_with("^")) { // Copy button.
  343. DisplayServer::get_singleton()->clipboard_set(p_select.substr(1));
  344. EditorToaster::get_singleton()->popup_str(TTR("Code snippet copied to clipboard."), EditorToaster::SEVERITY_INFO);
  345. }
  346. }
  347. void EditorHelp::_class_desc_input(const Ref<InputEvent> &p_input) {
  348. }
  349. void EditorHelp::_class_desc_resized(bool p_force_update_theme) {
  350. // Add extra horizontal margins for better readability.
  351. // The margins increase as the width of the editor help container increases.
  352. real_t char_width = theme_cache.doc_code_font->get_char_size('x', theme_cache.doc_code_font_size).width;
  353. const int new_display_margin = MAX(30 * EDSCALE, get_parent_anchorable_rect().size.width - char_width * 120 * EDSCALE) * 0.5;
  354. if (display_margin != new_display_margin || p_force_update_theme) {
  355. display_margin = new_display_margin;
  356. Ref<StyleBox> class_desc_stylebox = theme_cache.background_style->duplicate();
  357. class_desc_stylebox->set_content_margin(SIDE_LEFT, display_margin);
  358. class_desc_stylebox->set_content_margin(SIDE_RIGHT, display_margin);
  359. class_desc->add_theme_style_override(CoreStringName(normal), class_desc_stylebox);
  360. class_desc->add_theme_style_override("focused", class_desc_stylebox);
  361. }
  362. }
  363. 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) {
  364. const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  365. if (p_type.is_empty() || p_type == "void") {
  366. p_rt->push_color(Color(type_color, 0.5));
  367. p_rt->push_hint(TTR("No return value."));
  368. p_rt->add_text("void");
  369. p_rt->pop(); // hint
  370. p_rt->pop(); // color
  371. return;
  372. }
  373. bool is_enum_type = !p_enum.is_empty();
  374. bool is_bitfield = p_is_bitfield && is_enum_type;
  375. bool can_ref = !p_type.contains_char('*') || is_enum_type;
  376. String link_t = p_type; // For links in metadata
  377. String display_t; // For display purposes.
  378. if (is_enum_type) {
  379. link_t = p_enum; // The link for enums is always the full enum description
  380. display_t = _contextualize_class_specifier(p_enum, p_class);
  381. } else {
  382. display_t = _contextualize_class_specifier(p_type, p_class);
  383. }
  384. p_rt->push_color(type_color);
  385. bool add_typed_container = false;
  386. if (can_ref) {
  387. if (link_t.ends_with("[]")) {
  388. add_typed_container = true;
  389. link_t = link_t.trim_suffix("[]");
  390. display_t = display_t.trim_suffix("[]");
  391. p_rt->push_meta("#Array", RichTextLabel::META_UNDERLINE_ON_HOVER); // class
  392. p_rt->add_text("Array");
  393. p_rt->pop(); // meta
  394. p_rt->add_text("[");
  395. } else if (link_t.begins_with("Dictionary[")) {
  396. add_typed_container = true;
  397. link_t = link_t.trim_prefix("Dictionary[").trim_suffix("]");
  398. display_t = display_t.trim_prefix("Dictionary[").trim_suffix("]");
  399. p_rt->push_meta("#Dictionary", RichTextLabel::META_UNDERLINE_ON_HOVER); // class
  400. p_rt->add_text("Dictionary");
  401. p_rt->pop(); // meta
  402. p_rt->add_text("[");
  403. p_rt->push_meta("#" + link_t.get_slice(", ", 0), RichTextLabel::META_UNDERLINE_ON_HOVER); // class
  404. p_rt->add_text(_contextualize_class_specifier(display_t.get_slice(", ", 0), p_class));
  405. p_rt->pop(); // meta
  406. p_rt->add_text(", ");
  407. link_t = link_t.get_slice(", ", 1);
  408. display_t = _contextualize_class_specifier(display_t.get_slice(", ", 1), p_class);
  409. } else if (is_bitfield) {
  410. p_rt->push_color(Color(type_color, 0.5));
  411. p_rt->push_hint(TTR("This value is an integer composed as a bitmask of the following flags."));
  412. p_rt->add_text("BitField");
  413. p_rt->pop(); // hint
  414. p_rt->add_text("[");
  415. p_rt->pop(); // color
  416. }
  417. if (is_enum_type) {
  418. p_rt->push_meta("$" + link_t, RichTextLabel::META_UNDERLINE_ON_HOVER); // enum
  419. } else {
  420. p_rt->push_meta("#" + link_t, RichTextLabel::META_UNDERLINE_ON_HOVER); // class
  421. }
  422. }
  423. p_rt->add_text(display_t);
  424. if (can_ref) {
  425. p_rt->pop(); // meta
  426. if (add_typed_container) {
  427. p_rt->add_text("]");
  428. } else if (is_bitfield) {
  429. p_rt->push_color(Color(type_color, 0.5));
  430. p_rt->add_text("]");
  431. p_rt->pop(); // color
  432. }
  433. }
  434. p_rt->pop(); // color
  435. }
  436. void EditorHelp::_add_type(const String &p_type, const String &p_enum, bool p_is_bitfield) {
  437. _add_type_to_rt(p_type, p_enum, p_is_bitfield, class_desc, this, edited_class);
  438. }
  439. void EditorHelp::_add_type_icon(const String &p_type, int p_size, const String &p_fallback) {
  440. Ref<Texture2D> icon = EditorNode::get_singleton()->get_class_icon(p_type, p_fallback);
  441. Vector2i size = Vector2i(icon->get_width(), icon->get_height());
  442. if (p_size > 0) {
  443. // Ensures icon scales proportionally on both axes, based on icon height.
  444. float ratio = p_size / float(size.height);
  445. size.width *= ratio;
  446. size.height *= ratio;
  447. }
  448. class_desc->add_image(icon, size.width, size.height);
  449. }
  450. // Macros for assigning the deprecated/experimental marks to class members in overview.
  451. #define DEPRECATED_DOC_TAG \
  452. class_desc->push_font(theme_cache.doc_bold_font); \
  453. class_desc->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor))); \
  454. Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError")); \
  455. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
  456. class_desc->add_text(String::chr(160) + TTR("Deprecated")); \
  457. class_desc->pop(); \
  458. class_desc->pop();
  459. #define EXPERIMENTAL_DOC_TAG \
  460. class_desc->push_font(theme_cache.doc_bold_font); \
  461. class_desc->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); \
  462. Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning")); \
  463. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
  464. class_desc->add_text(String::chr(160) + TTR("Experimental")); \
  465. class_desc->pop(); \
  466. class_desc->pop();
  467. // Macros for displaying the deprecated/experimental info in class member descriptions.
  468. #define DEPRECATED_DOC_MSG(m_message, m_default_message) \
  469. Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError")); \
  470. class_desc->add_image(error_icon, error_icon->get_width(), error_icon->get_height()); \
  471. class_desc->add_text(nbsp); \
  472. class_desc->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor))); \
  473. class_desc->push_font(theme_cache.doc_bold_font); \
  474. class_desc->add_text(TTR("Deprecated:")); \
  475. class_desc->pop(); \
  476. class_desc->pop(); \
  477. class_desc->add_text(" "); \
  478. if ((m_message).is_empty()) { \
  479. class_desc->add_text(m_default_message); \
  480. } else { \
  481. _add_text(m_message); \
  482. }
  483. #define EXPERIMENTAL_DOC_MSG(m_message, m_default_message) \
  484. Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning")); \
  485. class_desc->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height()); \
  486. class_desc->add_text(nbsp); \
  487. class_desc->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor))); \
  488. class_desc->push_font(theme_cache.doc_bold_font); \
  489. class_desc->add_text(TTR("Experimental:")); \
  490. class_desc->pop(); \
  491. class_desc->pop(); \
  492. class_desc->add_text(" "); \
  493. if ((m_message).is_empty()) { \
  494. class_desc->add_text(m_default_message); \
  495. } else { \
  496. _add_text(m_message); \
  497. }
  498. void EditorHelp::_add_method(const DocData::MethodDoc &p_method, bool p_overview, bool p_override) {
  499. if (p_override) {
  500. method_line[p_method.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  501. }
  502. const bool is_vararg = p_method.qualifiers.contains("vararg");
  503. if (p_overview) {
  504. class_desc->push_cell();
  505. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  506. } else {
  507. _add_bulletpoint();
  508. }
  509. _add_type(p_method.return_type, p_method.return_enum, p_method.return_is_bitfield);
  510. if (p_overview) {
  511. class_desc->pop(); // paragraph
  512. class_desc->pop(); // cell
  513. class_desc->push_cell();
  514. } else {
  515. class_desc->add_text(" ");
  516. }
  517. const bool is_documented = p_method.is_deprecated || p_method.is_experimental || !p_method.description.strip_edges().is_empty();
  518. if (p_overview && is_documented) {
  519. class_desc->push_meta("@method " + p_method.name, RichTextLabel::META_UNDERLINE_ON_HOVER);
  520. }
  521. class_desc->push_color(theme_cache.headline_color);
  522. class_desc->add_text(p_method.name);
  523. class_desc->pop(); // color
  524. if (p_overview && is_documented) {
  525. class_desc->pop(); // meta
  526. }
  527. class_desc->push_color(theme_cache.symbol_color);
  528. class_desc->add_text("(");
  529. class_desc->pop(); // color
  530. for (int j = 0; j < p_method.arguments.size(); j++) {
  531. const DocData::ArgumentDoc &argument = p_method.arguments[j];
  532. class_desc->push_color(theme_cache.text_color);
  533. if (j > 0) {
  534. class_desc->add_text(", ");
  535. }
  536. class_desc->add_text(argument.name);
  537. class_desc->add_text(colon_nbsp);
  538. _add_type(argument.type, argument.enumeration, argument.is_bitfield);
  539. if (!argument.default_value.is_empty()) {
  540. class_desc->push_color(theme_cache.symbol_color);
  541. class_desc->add_text(nbsp_equal_nbsp);
  542. class_desc->pop(); // color
  543. class_desc->push_color(theme_cache.value_color);
  544. class_desc->add_text(_fix_constant(argument.default_value));
  545. class_desc->pop(); // color
  546. }
  547. class_desc->pop(); // color
  548. }
  549. if (is_vararg) {
  550. if (!p_method.arguments.is_empty()) {
  551. class_desc->push_color(theme_cache.text_color);
  552. class_desc->add_text(", ");
  553. class_desc->pop(); // color
  554. }
  555. class_desc->push_color(theme_cache.symbol_color);
  556. class_desc->add_text("...");
  557. class_desc->pop(); // color
  558. }
  559. class_desc->push_color(theme_cache.symbol_color);
  560. class_desc->add_text(")");
  561. class_desc->pop(); // color
  562. if (!p_method.qualifiers.is_empty()) {
  563. class_desc->push_color(theme_cache.qualifier_color);
  564. _add_qualifiers_to_rt(p_method.qualifiers, class_desc);
  565. class_desc->pop(); // color
  566. }
  567. if (p_overview) {
  568. if (p_method.is_deprecated) {
  569. class_desc->add_text(" ");
  570. DEPRECATED_DOC_TAG;
  571. }
  572. if (p_method.is_experimental) {
  573. class_desc->add_text(" ");
  574. EXPERIMENTAL_DOC_TAG;
  575. }
  576. class_desc->pop(); // cell
  577. }
  578. }
  579. void EditorHelp::_add_bulletpoint() {
  580. static const char32_t prefix[3] = { 0x25CF /* filled circle */, ' ', 0 };
  581. class_desc->add_text(String(prefix));
  582. }
  583. void EditorHelp::_push_normal_font() {
  584. class_desc->push_font(theme_cache.doc_font);
  585. class_desc->push_font_size(theme_cache.doc_font_size);
  586. }
  587. void EditorHelp::_pop_normal_font() {
  588. class_desc->pop(); // font_size
  589. class_desc->pop(); // font
  590. }
  591. void EditorHelp::_push_title_font() {
  592. class_desc->push_font(theme_cache.doc_title_font);
  593. class_desc->push_font_size(theme_cache.doc_title_font_size);
  594. class_desc->push_color(theme_cache.title_color);
  595. }
  596. void EditorHelp::_pop_title_font() {
  597. class_desc->pop(); // color
  598. class_desc->pop(); // font_size
  599. class_desc->pop(); // font
  600. }
  601. void EditorHelp::_push_code_font() {
  602. class_desc->push_font(theme_cache.doc_code_font);
  603. class_desc->push_font_size(theme_cache.doc_code_font_size);
  604. }
  605. void EditorHelp::_pop_code_font() {
  606. class_desc->pop(); // font_size
  607. class_desc->pop(); // font
  608. }
  609. Error EditorHelp::_goto_desc(const String &p_class) {
  610. // If class doesn't have docs listed, attempt on-demand docgen
  611. if (!doc->class_list.has(p_class) && !_attempt_doc_load(p_class)) {
  612. return ERR_DOES_NOT_EXIST;
  613. }
  614. select_locked = true;
  615. class_desc->show();
  616. description_line = 0;
  617. if (p_class == edited_class) {
  618. return OK; // Already there.
  619. }
  620. edited_class = p_class;
  621. _update_doc();
  622. return OK;
  623. }
  624. void EditorHelp::_update_method_list(MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods) {
  625. class_desc->add_newline();
  626. class_desc->add_newline();
  627. static const char *titles_by_type[METHOD_TYPE_MAX] = {
  628. TTRC("Methods"),
  629. TTRC("Constructors"),
  630. TTRC("Operators"),
  631. };
  632. const String title = TTRGET(titles_by_type[p_method_type]);
  633. section_line.push_back(Pair<String, int>(title, class_desc->get_paragraph_count() - 2));
  634. _push_title_font();
  635. class_desc->add_text(title);
  636. _pop_title_font();
  637. class_desc->add_newline();
  638. class_desc->add_newline();
  639. class_desc->push_indent(1);
  640. _push_code_font();
  641. class_desc->push_table(2);
  642. class_desc->set_table_column_expand(1, true);
  643. bool any_previous = false;
  644. for (int pass = 0; pass < 2; pass++) {
  645. Vector<DocData::MethodDoc> m;
  646. for (const DocData::MethodDoc &method : p_methods) {
  647. const String &q = method.qualifiers;
  648. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  649. m.push_back(method);
  650. }
  651. }
  652. if (any_previous && !m.is_empty()) {
  653. class_desc->push_cell();
  654. class_desc->pop(); // cell
  655. class_desc->push_cell();
  656. class_desc->pop(); // cell
  657. }
  658. String group_prefix;
  659. for (int i = 0; i < m.size(); i++) {
  660. const String new_prefix = m[i].name.left(3);
  661. bool is_new_group = false;
  662. if (i < m.size() - 1 && new_prefix == m[i + 1].name.left(3) && new_prefix != group_prefix) {
  663. is_new_group = i > 0;
  664. group_prefix = new_prefix;
  665. } else if (!group_prefix.is_empty() && new_prefix != group_prefix) {
  666. is_new_group = true;
  667. group_prefix = "";
  668. }
  669. if (is_new_group && pass == 1) {
  670. class_desc->push_cell();
  671. class_desc->pop(); // cell
  672. class_desc->push_cell();
  673. class_desc->pop(); // cell
  674. }
  675. // For constructors always point to the first one.
  676. _add_method(m[i], true, (p_method_type != METHOD_TYPE_CONSTRUCTOR || i == 0));
  677. }
  678. any_previous = !m.is_empty();
  679. }
  680. class_desc->pop(); // table
  681. _pop_code_font();
  682. class_desc->pop(); // indent
  683. }
  684. void EditorHelp::_update_method_descriptions(const DocData::ClassDoc &p_classdoc, MethodType p_method_type, const Vector<DocData::MethodDoc> &p_methods) {
  685. #define HANDLE_DOC(m_string) ((p_classdoc.is_script_doc ? (m_string) : DTR(m_string)).strip_edges())
  686. class_desc->add_newline();
  687. class_desc->add_newline();
  688. class_desc->add_newline();
  689. static const char *titles_by_type[METHOD_TYPE_MAX] = {
  690. TTRC("Method Descriptions"),
  691. TTRC("Constructor Descriptions"),
  692. TTRC("Operator Descriptions"),
  693. };
  694. const String title = TTRGET(titles_by_type[p_method_type]);
  695. section_line.push_back(Pair<String, int>(title, class_desc->get_paragraph_count() - 2));
  696. _push_title_font();
  697. class_desc->add_text(title);
  698. _pop_title_font();
  699. String link_color_text = theme_cache.title_color.to_html(false);
  700. for (int pass = 0; pass < 2; pass++) {
  701. Vector<DocData::MethodDoc> methods_filtered;
  702. for (int i = 0; i < p_methods.size(); i++) {
  703. const String &q = p_methods[i].qualifiers;
  704. if ((pass == 0 && q.contains("virtual")) || (pass == 1 && !q.contains("virtual"))) {
  705. methods_filtered.push_back(p_methods[i]);
  706. }
  707. }
  708. for (int i = 0; i < methods_filtered.size(); i++) {
  709. const DocData::MethodDoc &method = methods_filtered[i];
  710. class_desc->add_newline();
  711. class_desc->add_newline();
  712. class_desc->add_newline();
  713. _push_code_font();
  714. // For constructors always point to the first one.
  715. _add_method(method, false, (p_method_type != METHOD_TYPE_CONSTRUCTOR || i == 0));
  716. _pop_code_font();
  717. class_desc->add_newline();
  718. class_desc->add_newline();
  719. class_desc->push_indent(1);
  720. _push_normal_font();
  721. class_desc->push_color(theme_cache.text_color);
  722. bool has_prev_text = false;
  723. if (method.is_deprecated) {
  724. has_prev_text = true;
  725. static const char *messages_by_type[METHOD_TYPE_MAX] = {
  726. TTRC("This method may be changed or removed in future versions."),
  727. TTRC("This constructor may be changed or removed in future versions."),
  728. TTRC("This operator may be changed or removed in future versions."),
  729. };
  730. DEPRECATED_DOC_MSG(HANDLE_DOC(method.deprecated_message), TTRGET(messages_by_type[p_method_type]));
  731. }
  732. if (method.is_experimental) {
  733. if (has_prev_text) {
  734. class_desc->add_newline();
  735. class_desc->add_newline();
  736. }
  737. has_prev_text = true;
  738. static const char *messages_by_type[METHOD_TYPE_MAX] = {
  739. TTRC("This method may be changed or removed in future versions."),
  740. TTRC("This constructor may be changed or removed in future versions."),
  741. TTRC("This operator may be changed or removed in future versions."),
  742. };
  743. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(method.experimental_message), TTRGET(messages_by_type[p_method_type]));
  744. }
  745. if (!method.errors_returned.is_empty()) {
  746. if (has_prev_text) {
  747. class_desc->add_newline();
  748. class_desc->add_newline();
  749. }
  750. has_prev_text = true;
  751. class_desc->add_text(TTR("Error codes returned:"));
  752. class_desc->add_newline();
  753. class_desc->push_list(0, RichTextLabel::LIST_DOTS, false);
  754. for (int j = 0; j < method.errors_returned.size(); j++) {
  755. if (j > 0) {
  756. class_desc->add_newline();
  757. }
  758. int val = method.errors_returned[j];
  759. String text = itos(val);
  760. for (int k = 0; k < CoreConstants::get_global_constant_count(); k++) {
  761. if (CoreConstants::get_global_constant_value(k) == val && CoreConstants::get_global_constant_enum(k) == SNAME("Error")) {
  762. text = CoreConstants::get_global_constant_name(k);
  763. break;
  764. }
  765. }
  766. class_desc->push_font(theme_cache.doc_bold_font);
  767. class_desc->add_text(text);
  768. class_desc->pop(); // font
  769. }
  770. class_desc->pop(); // list
  771. }
  772. const String descr = HANDLE_DOC(method.description);
  773. const bool is_documented = method.is_deprecated || method.is_experimental || !descr.is_empty();
  774. if (!descr.is_empty()) {
  775. if (has_prev_text) {
  776. class_desc->add_newline();
  777. class_desc->add_newline();
  778. }
  779. has_prev_text = true;
  780. _add_text(descr);
  781. } else if (!is_documented) {
  782. if (has_prev_text) {
  783. class_desc->add_newline();
  784. class_desc->add_newline();
  785. }
  786. has_prev_text = true;
  787. String message;
  788. if (p_classdoc.is_script_doc) {
  789. static const char *messages_by_type[METHOD_TYPE_MAX] = {
  790. TTRC("There is currently no description for this method."),
  791. TTRC("There is currently no description for this constructor."),
  792. TTRC("There is currently no description for this operator."),
  793. };
  794. message = TTRGET(messages_by_type[p_method_type]);
  795. } else {
  796. static const char *messages_by_type[METHOD_TYPE_MAX] = {
  797. TTRC("There is currently no description for this method. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
  798. TTRC("There is currently no description for this constructor. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
  799. TTRC("There is currently no description for this operator. Please help us by [color=$color][url=$url]contributing one[/url][/color]!"),
  800. };
  801. message = TTRGET(messages_by_type[p_method_type]).replace("$url", CONTRIBUTE_URL).replace("$color", link_color_text);
  802. }
  803. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  804. class_desc->add_text(" ");
  805. class_desc->push_color(theme_cache.comment_color);
  806. class_desc->append_text(message);
  807. class_desc->pop(); // color
  808. }
  809. class_desc->pop(); // color
  810. _pop_normal_font();
  811. class_desc->pop(); // indent
  812. }
  813. }
  814. #undef HANDLE_DOC
  815. }
  816. void EditorHelp::_update_doc() {
  817. if (!doc->class_list.has(edited_class)) {
  818. return;
  819. }
  820. scroll_locked = true;
  821. class_desc->clear();
  822. method_line.clear();
  823. section_line.clear();
  824. section_line.push_back(Pair<String, int>(TTR("Top"), 0));
  825. String link_color_text = theme_cache.title_color.to_html(false);
  826. DocData::ClassDoc cd = doc->class_list[edited_class]; // Make a copy, so we can sort without worrying.
  827. #define HANDLE_DOC(m_string) ((cd.is_script_doc ? (m_string) : DTR(m_string)).strip_edges())
  828. // Class name
  829. _push_title_font();
  830. class_desc->add_text(TTR("Class:") + " ");
  831. _add_type_icon(edited_class, theme_cache.doc_title_font_size, "Object");
  832. class_desc->add_text(nbsp);
  833. class_desc->push_color(theme_cache.headline_color);
  834. class_desc->add_text(edited_class);
  835. class_desc->pop(); // color
  836. _pop_title_font();
  837. if (cd.is_deprecated) {
  838. class_desc->add_newline();
  839. DEPRECATED_DOC_MSG(HANDLE_DOC(cd.deprecated_message), TTR("This class may be changed or removed in future versions."));
  840. }
  841. if (cd.is_experimental) {
  842. class_desc->add_newline();
  843. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(cd.experimental_message), TTR("This class may be changed or removed in future versions."));
  844. }
  845. // Inheritance tree
  846. // Ascendents
  847. if (!cd.inherits.is_empty()) {
  848. class_desc->add_newline();
  849. _push_normal_font();
  850. class_desc->push_color(theme_cache.title_color);
  851. class_desc->add_text(TTR("Inherits:") + " ");
  852. String inherits = cd.inherits;
  853. while (!inherits.is_empty()) {
  854. _add_type_icon(inherits, theme_cache.doc_font_size, "ArrowRight");
  855. class_desc->add_text(nbsp); // Otherwise icon borrows hyperlink from `_add_type()`.
  856. _add_type(inherits);
  857. const DocData::ClassDoc *base_class_doc = doc->class_list.getptr(inherits);
  858. inherits = base_class_doc ? base_class_doc->inherits : String();
  859. if (!inherits.is_empty()) {
  860. class_desc->add_text(" < ");
  861. }
  862. }
  863. class_desc->pop(); // color
  864. _pop_normal_font();
  865. }
  866. // Descendants
  867. if ((cd.is_script_doc || ClassDB::class_exists(cd.name)) && doc->inheriting.has(cd.name)) {
  868. class_desc->add_newline();
  869. _push_normal_font();
  870. class_desc->push_color(theme_cache.title_color);
  871. class_desc->add_text(TTR("Inherited by:") + " ");
  872. for (RBSet<String, NaturalNoCaseComparator>::Element *itr = doc->inheriting[cd.name].front(); itr; itr = itr->next()) {
  873. if (itr->prev()) {
  874. class_desc->add_text(" , ");
  875. }
  876. _add_type_icon(itr->get(), theme_cache.doc_font_size, "ArrowRight");
  877. class_desc->add_text(nbsp); // Otherwise icon borrows hyperlink from `_add_type()`.
  878. _add_type(itr->get());
  879. }
  880. class_desc->pop(); // color
  881. _pop_normal_font();
  882. }
  883. bool has_description = false;
  884. // Brief description
  885. const String brief_class_descr = HANDLE_DOC(cd.brief_description);
  886. if (!brief_class_descr.is_empty()) {
  887. has_description = true;
  888. class_desc->add_newline();
  889. class_desc->add_newline();
  890. class_desc->push_indent(1);
  891. class_desc->push_font(theme_cache.doc_bold_font);
  892. class_desc->push_color(theme_cache.text_color);
  893. _add_text(brief_class_descr);
  894. class_desc->pop(); // color
  895. class_desc->pop(); // font
  896. class_desc->pop(); // indent
  897. }
  898. // Class description
  899. const String class_descr = HANDLE_DOC(cd.description);
  900. if (!class_descr.is_empty()) {
  901. has_description = true;
  902. class_desc->add_newline();
  903. class_desc->add_newline();
  904. section_line.push_back(Pair<String, int>(TTR("Description"), class_desc->get_paragraph_count() - 2));
  905. description_line = class_desc->get_paragraph_count() - 2;
  906. _push_title_font();
  907. class_desc->add_text(TTR("Description"));
  908. _pop_title_font();
  909. class_desc->add_newline();
  910. class_desc->add_newline();
  911. class_desc->push_indent(1);
  912. _push_normal_font();
  913. class_desc->push_color(theme_cache.text_color);
  914. _add_text(class_descr);
  915. class_desc->pop(); // color
  916. _pop_normal_font();
  917. class_desc->pop(); // indent
  918. }
  919. if (!has_description) {
  920. class_desc->add_newline();
  921. class_desc->add_newline();
  922. class_desc->push_indent(1);
  923. _push_normal_font();
  924. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  925. class_desc->add_text(" ");
  926. class_desc->push_color(theme_cache.comment_color);
  927. if (cd.is_script_doc) {
  928. class_desc->add_text(TTR("There is currently no description for this class."));
  929. } else {
  930. 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));
  931. }
  932. class_desc->pop(); // color
  933. _pop_normal_font();
  934. class_desc->pop(); // indent
  935. }
  936. #ifdef MODULE_MONO_ENABLED
  937. if (classes_with_csharp_differences.has(cd.name)) {
  938. class_desc->add_newline();
  939. class_desc->add_newline();
  940. const String &csharp_differences_url = vformat("%s/tutorials/scripting/c_sharp/c_sharp_differences.html", VERSION_DOCS_URL);
  941. class_desc->push_indent(1);
  942. _push_normal_font();
  943. class_desc->push_color(theme_cache.text_color);
  944. 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));
  945. class_desc->pop(); // color
  946. _pop_normal_font();
  947. class_desc->pop(); // indent
  948. }
  949. #endif
  950. // Online tutorials
  951. if (!cd.tutorials.is_empty()) {
  952. class_desc->add_newline();
  953. class_desc->add_newline();
  954. _push_title_font();
  955. class_desc->add_text(TTR("Online Tutorials"));
  956. _pop_title_font();
  957. class_desc->add_newline();
  958. class_desc->push_indent(1);
  959. _push_code_font();
  960. class_desc->push_color(theme_cache.symbol_color);
  961. for (const DocData::TutorialDoc &tutorial : cd.tutorials) {
  962. const String link = HANDLE_DOC(tutorial.link);
  963. String link_text = HANDLE_DOC(tutorial.title);
  964. if (link_text.is_empty()) {
  965. const int sep_pos = link.find("//");
  966. if (sep_pos >= 0) {
  967. link_text = link.substr(sep_pos + 2);
  968. } else {
  969. link_text = link;
  970. }
  971. }
  972. class_desc->add_newline();
  973. _add_bulletpoint();
  974. class_desc->append_text("[url=" + link + "]" + link_text + "[/url]");
  975. }
  976. class_desc->pop(); // color
  977. _pop_code_font();
  978. class_desc->pop(); // indent
  979. }
  980. // Properties overview
  981. HashSet<String> skip_methods;
  982. bool has_properties = false;
  983. bool has_property_descriptions = false;
  984. for (const DocData::PropertyDoc &prop : cd.properties) {
  985. const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
  986. if (!is_documented && prop.name.begins_with("_")) {
  987. continue;
  988. }
  989. has_properties = true;
  990. if (!prop.overridden) {
  991. has_property_descriptions = true;
  992. break;
  993. }
  994. }
  995. if (has_properties) {
  996. class_desc->add_newline();
  997. class_desc->add_newline();
  998. section_line.push_back(Pair<String, int>(TTR("Properties"), class_desc->get_paragraph_count() - 2));
  999. _push_title_font();
  1000. class_desc->add_text(TTR("Properties"));
  1001. _pop_title_font();
  1002. class_desc->add_newline();
  1003. class_desc->add_newline();
  1004. class_desc->push_indent(1);
  1005. _push_code_font();
  1006. class_desc->push_table(4);
  1007. class_desc->set_table_column_expand(1, true);
  1008. cd.properties.sort_custom<PropertyCompare>();
  1009. bool is_generating_overridden_properties = true; // Set to false as soon as we encounter a non-overridden property.
  1010. bool overridden_property_exists = false;
  1011. for (const DocData::PropertyDoc &prop : cd.properties) {
  1012. // Ignore undocumented private.
  1013. const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
  1014. if (!is_documented && prop.name.begins_with("_")) {
  1015. continue;
  1016. }
  1017. if (is_generating_overridden_properties && !prop.overridden) {
  1018. is_generating_overridden_properties = false;
  1019. // No need for the extra spacing when there's no overridden property.
  1020. if (overridden_property_exists) {
  1021. class_desc->push_cell();
  1022. class_desc->pop(); // cell
  1023. class_desc->push_cell();
  1024. class_desc->pop(); // cell
  1025. class_desc->push_cell();
  1026. class_desc->pop(); // cell
  1027. class_desc->push_cell();
  1028. class_desc->pop(); // cell
  1029. }
  1030. }
  1031. property_line[prop.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1032. // Property type.
  1033. class_desc->push_cell();
  1034. class_desc->push_paragraph(HORIZONTAL_ALIGNMENT_RIGHT, Control::TEXT_DIRECTION_AUTO, "");
  1035. _add_type(prop.type, prop.enumeration, prop.is_bitfield);
  1036. class_desc->pop(); // paragraph
  1037. class_desc->pop(); // cell
  1038. bool describe = false;
  1039. if (!prop.setter.is_empty()) {
  1040. skip_methods.insert(prop.setter);
  1041. describe = true;
  1042. }
  1043. if (!prop.getter.is_empty()) {
  1044. skip_methods.insert(prop.getter);
  1045. describe = true;
  1046. }
  1047. if (is_documented) {
  1048. describe = true;
  1049. }
  1050. if (prop.overridden) {
  1051. describe = false;
  1052. }
  1053. // Property name.
  1054. class_desc->push_cell();
  1055. class_desc->push_color(theme_cache.headline_color);
  1056. if (describe) {
  1057. class_desc->push_meta("@member " + prop.name, RichTextLabel::META_UNDERLINE_ON_HOVER);
  1058. }
  1059. class_desc->add_text(prop.name);
  1060. if (describe) {
  1061. class_desc->pop(); // meta
  1062. }
  1063. class_desc->pop(); // color
  1064. class_desc->pop(); // cell
  1065. // Property value.
  1066. class_desc->push_cell();
  1067. if (!prop.default_value.is_empty()) {
  1068. if (prop.overridden) {
  1069. class_desc->push_color(theme_cache.override_color);
  1070. class_desc->add_text("[");
  1071. const String link = vformat("[url=@member %s.%s]%s[/url]", prop.overrides, prop.name, prop.overrides);
  1072. class_desc->append_text(vformat(TTR("overrides %s:"), link));
  1073. class_desc->add_text(" " + _fix_constant(prop.default_value) + "]");
  1074. class_desc->pop(); // color
  1075. overridden_property_exists = true;
  1076. } else {
  1077. class_desc->push_color(theme_cache.symbol_color);
  1078. class_desc->add_text("[" + TTR("default:") + " ");
  1079. class_desc->pop(); // color
  1080. class_desc->push_color(theme_cache.value_color);
  1081. class_desc->add_text(_fix_constant(prop.default_value));
  1082. class_desc->pop(); // color
  1083. class_desc->push_color(theme_cache.symbol_color);
  1084. class_desc->add_text("]");
  1085. class_desc->pop(); // color
  1086. }
  1087. }
  1088. class_desc->pop(); // cell
  1089. // Property setter/getter and deprecated/experimental marks.
  1090. class_desc->push_cell();
  1091. bool has_prev_text = false;
  1092. if (cd.is_script_doc && (!prop.setter.is_empty() || !prop.getter.is_empty())) {
  1093. has_prev_text = true;
  1094. class_desc->push_color(theme_cache.symbol_color);
  1095. class_desc->add_text("[" + TTR("property:") + " ");
  1096. class_desc->pop(); // color
  1097. if (!prop.setter.is_empty()) {
  1098. class_desc->push_color(theme_cache.value_color);
  1099. class_desc->add_text("setter");
  1100. class_desc->pop(); // color
  1101. }
  1102. if (!prop.getter.is_empty()) {
  1103. if (!prop.setter.is_empty()) {
  1104. class_desc->push_color(theme_cache.symbol_color);
  1105. class_desc->add_text(", ");
  1106. class_desc->pop(); // color
  1107. }
  1108. class_desc->push_color(theme_cache.value_color);
  1109. class_desc->add_text("getter");
  1110. class_desc->pop(); // color
  1111. }
  1112. class_desc->push_color(theme_cache.symbol_color);
  1113. class_desc->add_text("]");
  1114. class_desc->pop(); // color
  1115. }
  1116. if (prop.is_deprecated) {
  1117. if (has_prev_text) {
  1118. class_desc->add_text(" ");
  1119. }
  1120. has_prev_text = true;
  1121. DEPRECATED_DOC_TAG;
  1122. }
  1123. if (prop.is_experimental) {
  1124. if (has_prev_text) {
  1125. class_desc->add_text(" ");
  1126. }
  1127. has_prev_text = true;
  1128. EXPERIMENTAL_DOC_TAG;
  1129. }
  1130. class_desc->pop(); // cell
  1131. }
  1132. class_desc->pop(); // table
  1133. _pop_code_font();
  1134. class_desc->pop(); // indent
  1135. }
  1136. // Methods overview
  1137. bool sort_methods = EDITOR_GET("text_editor/help/sort_functions_alphabetically");
  1138. Vector<DocData::MethodDoc> methods;
  1139. for (const DocData::MethodDoc &method : cd.methods) {
  1140. if (skip_methods.has(method.name)) {
  1141. if (method.arguments.is_empty() /* getter */ || (method.arguments.size() == 1 && method.return_type == "void" /* setter */)) {
  1142. continue;
  1143. }
  1144. }
  1145. // Ignore undocumented non virtual private.
  1146. const bool is_documented = method.is_deprecated || method.is_experimental || !method.description.strip_edges().is_empty();
  1147. if (!is_documented && method.name.begins_with("_") && !method.qualifiers.contains("virtual")) {
  1148. continue;
  1149. }
  1150. methods.push_back(method);
  1151. }
  1152. if (!cd.constructors.is_empty()) {
  1153. if (sort_methods) {
  1154. cd.constructors.sort();
  1155. }
  1156. _update_method_list(METHOD_TYPE_CONSTRUCTOR, cd.constructors);
  1157. }
  1158. if (!methods.is_empty()) {
  1159. if (sort_methods) {
  1160. methods.sort();
  1161. }
  1162. _update_method_list(METHOD_TYPE_METHOD, methods);
  1163. }
  1164. if (!cd.operators.is_empty()) {
  1165. if (sort_methods) {
  1166. cd.operators.sort();
  1167. }
  1168. _update_method_list(METHOD_TYPE_OPERATOR, cd.operators);
  1169. }
  1170. // Theme properties
  1171. if (!cd.theme_properties.is_empty()) {
  1172. class_desc->add_newline();
  1173. class_desc->add_newline();
  1174. section_line.push_back(Pair<String, int>(TTR("Theme Properties"), class_desc->get_paragraph_count() - 2));
  1175. _push_title_font();
  1176. class_desc->add_text(TTR("Theme Properties"));
  1177. _pop_title_font();
  1178. String theme_data_type;
  1179. HashMap<String, String> data_type_names;
  1180. data_type_names["color"] = TTR("Colors");
  1181. data_type_names["constant"] = TTR("Constants");
  1182. data_type_names["font"] = TTR("Fonts");
  1183. data_type_names["font_size"] = TTR("Font Sizes");
  1184. data_type_names["icon"] = TTR("Icons");
  1185. data_type_names["style"] = TTR("Styles");
  1186. for (const DocData::ThemeItemDoc &theme_item : cd.theme_properties) {
  1187. if (theme_data_type != theme_item.data_type) {
  1188. theme_data_type = theme_item.data_type;
  1189. class_desc->add_newline();
  1190. class_desc->add_newline();
  1191. class_desc->push_indent(1);
  1192. _push_title_font();
  1193. if (data_type_names.has(theme_data_type)) {
  1194. class_desc->add_text(data_type_names[theme_data_type]);
  1195. } else {
  1196. class_desc->add_text(theme_data_type);
  1197. }
  1198. _pop_title_font();
  1199. class_desc->pop(); // indent
  1200. }
  1201. class_desc->add_newline();
  1202. class_desc->add_newline();
  1203. theme_property_line[theme_item.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1204. class_desc->push_indent(1);
  1205. // Theme item header.
  1206. _push_code_font();
  1207. _add_bulletpoint();
  1208. // Theme item object type.
  1209. _add_type(theme_item.type);
  1210. // Theme item name.
  1211. class_desc->push_color(theme_cache.headline_color);
  1212. class_desc->add_text(" ");
  1213. class_desc->add_text(theme_item.name);
  1214. class_desc->pop(); // color
  1215. // Theme item default value.
  1216. if (!theme_item.default_value.is_empty()) {
  1217. class_desc->push_color(theme_cache.symbol_color);
  1218. class_desc->add_text(" [" + TTR("default:") + " ");
  1219. class_desc->pop(); // color
  1220. class_desc->push_color(theme_cache.value_color);
  1221. class_desc->add_text(_fix_constant(theme_item.default_value));
  1222. class_desc->pop(); // color
  1223. class_desc->push_color(theme_cache.symbol_color);
  1224. class_desc->add_text("]");
  1225. class_desc->pop(); // color
  1226. }
  1227. _pop_code_font();
  1228. // Theme item description.
  1229. class_desc->push_indent(1);
  1230. _push_normal_font();
  1231. class_desc->push_color(theme_cache.comment_color);
  1232. bool has_prev_text = false;
  1233. if (theme_item.is_deprecated) {
  1234. has_prev_text = true;
  1235. DEPRECATED_DOC_MSG(HANDLE_DOC(theme_item.deprecated_message), TTR("This theme property may be changed or removed in future versions."));
  1236. }
  1237. if (theme_item.is_experimental) {
  1238. if (has_prev_text) {
  1239. class_desc->add_newline();
  1240. class_desc->add_newline();
  1241. }
  1242. has_prev_text = true;
  1243. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(theme_item.experimental_message), TTR("This theme property may be changed or removed in future versions."));
  1244. }
  1245. const String descr = HANDLE_DOC(theme_item.description);
  1246. if (!descr.is_empty()) {
  1247. if (has_prev_text) {
  1248. class_desc->add_newline();
  1249. class_desc->add_newline();
  1250. }
  1251. has_prev_text = true;
  1252. _add_text(descr);
  1253. } else if (!has_prev_text) {
  1254. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  1255. class_desc->add_text(" ");
  1256. class_desc->push_color(theme_cache.comment_color);
  1257. if (cd.is_script_doc) {
  1258. class_desc->add_text(TTR("There is currently no description for this theme property."));
  1259. } else {
  1260. 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));
  1261. }
  1262. class_desc->pop(); // color
  1263. }
  1264. class_desc->pop(); // color
  1265. _pop_normal_font();
  1266. class_desc->pop(); // indent
  1267. class_desc->pop(); // indent
  1268. }
  1269. }
  1270. // Signals
  1271. if (!cd.signals.is_empty()) {
  1272. if (sort_methods) {
  1273. cd.signals.sort();
  1274. }
  1275. class_desc->add_newline();
  1276. class_desc->add_newline();
  1277. section_line.push_back(Pair<String, int>(TTR("Signals"), class_desc->get_paragraph_count() - 2));
  1278. _push_title_font();
  1279. class_desc->add_text(TTR("Signals"));
  1280. _pop_title_font();
  1281. for (const DocData::MethodDoc &signal : cd.signals) {
  1282. class_desc->add_newline();
  1283. class_desc->add_newline();
  1284. signal_line[signal.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1285. class_desc->push_indent(1);
  1286. // Signal header.
  1287. _push_code_font();
  1288. _add_bulletpoint();
  1289. class_desc->push_color(theme_cache.headline_color);
  1290. class_desc->add_text(signal.name);
  1291. class_desc->pop(); // color
  1292. class_desc->push_color(theme_cache.symbol_color);
  1293. class_desc->add_text("(");
  1294. class_desc->pop(); // color
  1295. for (int j = 0; j < signal.arguments.size(); j++) {
  1296. const DocData::ArgumentDoc &argument = signal.arguments[j];
  1297. class_desc->push_color(theme_cache.text_color);
  1298. if (j > 0) {
  1299. class_desc->add_text(", ");
  1300. }
  1301. class_desc->add_text(argument.name);
  1302. class_desc->add_text(colon_nbsp);
  1303. _add_type(argument.type, argument.enumeration, argument.is_bitfield);
  1304. // Signals currently do not support default argument values, neither the core nor GDScript.
  1305. // This code is just for completeness.
  1306. if (!argument.default_value.is_empty()) {
  1307. class_desc->push_color(theme_cache.symbol_color);
  1308. class_desc->add_text(nbsp_equal_nbsp);
  1309. class_desc->pop(); // color
  1310. class_desc->push_color(theme_cache.value_color);
  1311. class_desc->add_text(_fix_constant(argument.default_value));
  1312. class_desc->pop(); // color
  1313. }
  1314. class_desc->pop(); // color
  1315. }
  1316. class_desc->push_color(theme_cache.symbol_color);
  1317. class_desc->add_text(")");
  1318. class_desc->pop(); // color
  1319. _pop_code_font();
  1320. class_desc->add_newline();
  1321. // Signal description.
  1322. class_desc->push_indent(1);
  1323. _push_normal_font();
  1324. class_desc->push_color(theme_cache.comment_color);
  1325. const String descr = HANDLE_DOC(signal.description);
  1326. const bool is_multiline = descr.find_char('\n') > 0;
  1327. bool has_prev_text = false;
  1328. if (signal.is_deprecated) {
  1329. has_prev_text = true;
  1330. DEPRECATED_DOC_MSG(HANDLE_DOC(signal.deprecated_message), TTR("This signal may be changed or removed in future versions."));
  1331. }
  1332. if (signal.is_experimental) {
  1333. if (has_prev_text) {
  1334. class_desc->add_newline();
  1335. if (is_multiline) {
  1336. class_desc->add_newline();
  1337. }
  1338. }
  1339. has_prev_text = true;
  1340. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(signal.experimental_message), TTR("This signal may be changed or removed in future versions."));
  1341. }
  1342. if (!descr.is_empty()) {
  1343. if (has_prev_text) {
  1344. class_desc->add_newline();
  1345. if (is_multiline) {
  1346. class_desc->add_newline();
  1347. }
  1348. }
  1349. has_prev_text = true;
  1350. _add_text(descr);
  1351. } else if (!has_prev_text) {
  1352. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  1353. class_desc->add_text(" ");
  1354. class_desc->push_color(theme_cache.comment_color);
  1355. if (cd.is_script_doc) {
  1356. class_desc->add_text(TTR("There is currently no description for this signal."));
  1357. } else {
  1358. 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));
  1359. }
  1360. class_desc->pop(); // color
  1361. }
  1362. class_desc->pop(); // color
  1363. _pop_normal_font();
  1364. class_desc->pop(); // indent
  1365. class_desc->pop(); // indent
  1366. }
  1367. }
  1368. // Constants and enums
  1369. if (!cd.constants.is_empty()) {
  1370. HashMap<String, Vector<DocData::ConstantDoc>> enums;
  1371. Vector<DocData::ConstantDoc> constants;
  1372. for (const DocData::ConstantDoc &constant : cd.constants) {
  1373. if (!constant.enumeration.is_empty()) {
  1374. if (!enums.has(constant.enumeration)) {
  1375. enums[constant.enumeration] = Vector<DocData::ConstantDoc>();
  1376. }
  1377. enums[constant.enumeration].push_back(constant);
  1378. } else {
  1379. // Ignore undocumented private.
  1380. const bool is_documented = constant.is_deprecated || constant.is_experimental || !constant.description.strip_edges().is_empty();
  1381. if (!is_documented && constant.name.begins_with("_")) {
  1382. continue;
  1383. }
  1384. constants.push_back(constant);
  1385. }
  1386. }
  1387. // Enums
  1388. bool has_enums = enums.size() && !cd.is_script_doc;
  1389. if (enums.size() && !has_enums) {
  1390. for (KeyValue<String, DocData::EnumDoc> &E : cd.enums) {
  1391. const bool is_documented = E.value.is_deprecated || E.value.is_experimental || !E.value.description.strip_edges().is_empty();
  1392. if (!is_documented && E.key.begins_with("_")) {
  1393. continue;
  1394. }
  1395. has_enums = true;
  1396. break;
  1397. }
  1398. }
  1399. if (has_enums) {
  1400. class_desc->add_newline();
  1401. class_desc->add_newline();
  1402. section_line.push_back(Pair<String, int>(TTR("Enumerations"), class_desc->get_paragraph_count() - 2));
  1403. _push_title_font();
  1404. class_desc->add_text(TTR("Enumerations"));
  1405. _pop_title_font();
  1406. for (KeyValue<String, Vector<DocData::ConstantDoc>> &E : enums) {
  1407. String key = E.key;
  1408. if ((key.get_slice_count(".") > 1) && (key.get_slice(".", 0) == edited_class)) {
  1409. key = key.get_slice(".", 1);
  1410. }
  1411. if (cd.enums.has(key)) {
  1412. const bool is_documented = cd.enums[key].is_deprecated || cd.enums[key].is_experimental || !cd.enums[key].description.strip_edges().is_empty();
  1413. if (!is_documented && cd.is_script_doc && E.key.begins_with("_")) {
  1414. continue;
  1415. }
  1416. }
  1417. class_desc->add_newline();
  1418. class_desc->add_newline();
  1419. // Enum header.
  1420. _push_code_font();
  1421. enum_line[E.key] = class_desc->get_paragraph_count() - 2;
  1422. class_desc->push_color(theme_cache.title_color);
  1423. if (E.value.size() && E.value[0].is_bitfield) {
  1424. class_desc->add_text("flags ");
  1425. } else {
  1426. class_desc->add_text("enum ");
  1427. }
  1428. class_desc->pop(); // color
  1429. class_desc->push_color(theme_cache.headline_color);
  1430. class_desc->add_text(key);
  1431. class_desc->pop(); // color
  1432. class_desc->push_color(theme_cache.symbol_color);
  1433. class_desc->add_text(":");
  1434. class_desc->pop(); // color
  1435. _pop_code_font();
  1436. // Enum description.
  1437. if (key != "@unnamed_enums" && cd.enums.has(key)) {
  1438. const String descr = HANDLE_DOC(cd.enums[key].description);
  1439. const bool is_multiline = descr.find_char('\n') > 0;
  1440. if (cd.enums[key].is_deprecated || cd.enums[key].is_experimental || !descr.is_empty()) {
  1441. class_desc->add_newline();
  1442. class_desc->push_indent(1);
  1443. _push_normal_font();
  1444. class_desc->push_color(theme_cache.text_color);
  1445. bool has_prev_text = false;
  1446. if (cd.enums[key].is_deprecated) {
  1447. has_prev_text = true;
  1448. DEPRECATED_DOC_MSG(HANDLE_DOC(cd.enums[key].deprecated_message), TTR("This enumeration may be changed or removed in future versions."));
  1449. }
  1450. if (cd.enums[key].is_experimental) {
  1451. if (has_prev_text) {
  1452. class_desc->add_newline();
  1453. if (is_multiline) {
  1454. class_desc->add_newline();
  1455. }
  1456. }
  1457. has_prev_text = true;
  1458. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(cd.enums[key].experimental_message), TTR("This enumeration may be changed or removed in future versions."));
  1459. }
  1460. if (!descr.is_empty()) {
  1461. if (has_prev_text) {
  1462. class_desc->add_newline();
  1463. if (is_multiline) {
  1464. class_desc->add_newline();
  1465. }
  1466. }
  1467. has_prev_text = true;
  1468. _add_text(descr);
  1469. }
  1470. class_desc->pop(); // color
  1471. _pop_normal_font();
  1472. class_desc->pop(); // indent
  1473. }
  1474. }
  1475. HashMap<String, int> enum_values;
  1476. const int enum_start_line = enum_line[E.key];
  1477. bool prev_is_multiline = true; // Use a large margin for the first item.
  1478. for (const DocData::ConstantDoc &enum_value : E.value) {
  1479. const String descr = HANDLE_DOC(enum_value.description);
  1480. const bool is_multiline = descr.find_char('\n') > 0;
  1481. class_desc->add_newline();
  1482. if (prev_is_multiline || is_multiline) {
  1483. class_desc->add_newline();
  1484. }
  1485. prev_is_multiline = is_multiline;
  1486. if (cd.name == "@GlobalScope") {
  1487. enum_values[enum_value.name] = enum_start_line;
  1488. }
  1489. // Add the enum constant line to the constant_line map so we can locate it as a constant.
  1490. constant_line[enum_value.name] = class_desc->get_paragraph_count() - 2;
  1491. class_desc->push_indent(1);
  1492. // Enum value header.
  1493. _push_code_font();
  1494. _add_bulletpoint();
  1495. class_desc->push_color(theme_cache.headline_color);
  1496. class_desc->add_text(enum_value.name);
  1497. class_desc->pop(); // color
  1498. class_desc->push_color(theme_cache.symbol_color);
  1499. class_desc->add_text(nbsp_equal_nbsp);
  1500. class_desc->pop(); // color
  1501. class_desc->push_color(theme_cache.value_color);
  1502. class_desc->add_text(_fix_constant(enum_value.value));
  1503. class_desc->pop(); // color
  1504. _pop_code_font();
  1505. // Enum value description.
  1506. if (enum_value.is_deprecated || enum_value.is_experimental || !descr.is_empty()) {
  1507. class_desc->add_newline();
  1508. class_desc->push_indent(1);
  1509. _push_normal_font();
  1510. class_desc->push_color(theme_cache.comment_color);
  1511. bool has_prev_text = false;
  1512. if (enum_value.is_deprecated) {
  1513. has_prev_text = true;
  1514. DEPRECATED_DOC_MSG(HANDLE_DOC(enum_value.deprecated_message), TTR("This constant may be changed or removed in future versions."));
  1515. }
  1516. if (enum_value.is_experimental) {
  1517. if (has_prev_text) {
  1518. class_desc->add_newline();
  1519. if (is_multiline) {
  1520. class_desc->add_newline();
  1521. }
  1522. }
  1523. has_prev_text = true;
  1524. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(enum_value.experimental_message), TTR("This constant may be changed or removed in future versions."));
  1525. }
  1526. if (!descr.is_empty()) {
  1527. if (has_prev_text) {
  1528. class_desc->add_newline();
  1529. if (is_multiline) {
  1530. class_desc->add_newline();
  1531. }
  1532. }
  1533. has_prev_text = true;
  1534. _add_text(descr);
  1535. }
  1536. class_desc->pop(); // color
  1537. _pop_normal_font();
  1538. class_desc->pop(); // indent
  1539. }
  1540. class_desc->pop(); // indent
  1541. }
  1542. if (cd.name == "@GlobalScope") {
  1543. enum_values_line[E.key] = enum_values;
  1544. }
  1545. }
  1546. }
  1547. // Constants
  1548. if (!constants.is_empty()) {
  1549. class_desc->add_newline();
  1550. class_desc->add_newline();
  1551. section_line.push_back(Pair<String, int>(TTR("Constants"), class_desc->get_paragraph_count() - 2));
  1552. _push_title_font();
  1553. class_desc->add_text(TTR("Constants"));
  1554. _pop_title_font();
  1555. bool prev_is_multiline = true; // Use a large margin for the first item.
  1556. for (const DocData::ConstantDoc &constant : constants) {
  1557. const String descr = HANDLE_DOC(constant.description);
  1558. const bool is_multiline = descr.find_char('\n') > 0;
  1559. class_desc->add_newline();
  1560. if (prev_is_multiline || is_multiline) {
  1561. class_desc->add_newline();
  1562. }
  1563. prev_is_multiline = is_multiline;
  1564. constant_line[constant.name] = class_desc->get_paragraph_count() - 2;
  1565. class_desc->push_indent(1);
  1566. // Constant header.
  1567. _push_code_font();
  1568. if (constant.value.begins_with("Color(") && constant.value.ends_with(")")) {
  1569. String stripped = constant.value.replace(" ", "").replace("Color(", "").replace(")", "");
  1570. PackedFloat64Array color = stripped.split_floats(",");
  1571. if (color.size() >= 3) {
  1572. class_desc->push_color(Color(color[0], color[1], color[2]));
  1573. _add_bulletpoint();
  1574. class_desc->pop(); // color
  1575. }
  1576. } else {
  1577. _add_bulletpoint();
  1578. }
  1579. class_desc->push_color(theme_cache.headline_color);
  1580. class_desc->add_text(constant.name);
  1581. class_desc->pop(); // color
  1582. class_desc->push_color(theme_cache.symbol_color);
  1583. class_desc->add_text(nbsp_equal_nbsp);
  1584. class_desc->pop(); // color
  1585. class_desc->push_color(theme_cache.value_color);
  1586. class_desc->add_text(_fix_constant(constant.value));
  1587. class_desc->pop(); // color
  1588. _pop_code_font();
  1589. // Constant description.
  1590. if (constant.is_deprecated || constant.is_experimental || !descr.is_empty()) {
  1591. class_desc->add_newline();
  1592. class_desc->push_indent(1);
  1593. _push_normal_font();
  1594. class_desc->push_color(theme_cache.comment_color);
  1595. bool has_prev_text = false;
  1596. if (constant.is_deprecated) {
  1597. has_prev_text = true;
  1598. DEPRECATED_DOC_MSG(HANDLE_DOC(constant.deprecated_message), TTR("This constant may be changed or removed in future versions."));
  1599. }
  1600. if (constant.is_experimental) {
  1601. if (has_prev_text) {
  1602. class_desc->add_newline();
  1603. if (is_multiline) {
  1604. class_desc->add_newline();
  1605. }
  1606. }
  1607. has_prev_text = true;
  1608. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(constant.experimental_message), TTR("This constant may be changed or removed in future versions."));
  1609. }
  1610. if (!descr.is_empty()) {
  1611. if (has_prev_text) {
  1612. class_desc->add_newline();
  1613. if (is_multiline) {
  1614. class_desc->add_newline();
  1615. }
  1616. }
  1617. has_prev_text = true;
  1618. _add_text(descr);
  1619. }
  1620. class_desc->pop(); // color
  1621. _pop_normal_font();
  1622. class_desc->pop(); // indent
  1623. }
  1624. class_desc->pop(); // indent
  1625. }
  1626. }
  1627. }
  1628. // Annotations
  1629. if (!cd.annotations.is_empty()) {
  1630. if (sort_methods) {
  1631. cd.annotations.sort();
  1632. }
  1633. class_desc->add_newline();
  1634. class_desc->add_newline();
  1635. section_line.push_back(Pair<String, int>(TTR("Annotations"), class_desc->get_paragraph_count() - 2));
  1636. _push_title_font();
  1637. class_desc->add_text(TTR("Annotations"));
  1638. _pop_title_font();
  1639. for (const DocData::MethodDoc &annotation : cd.annotations) {
  1640. class_desc->add_newline();
  1641. class_desc->add_newline();
  1642. annotation_line[annotation.name] = class_desc->get_paragraph_count() - 2; // Gets overridden if description.
  1643. class_desc->push_indent(1);
  1644. // Annotation header.
  1645. _push_code_font();
  1646. _add_bulletpoint();
  1647. class_desc->push_color(theme_cache.headline_color);
  1648. class_desc->add_text(annotation.name);
  1649. class_desc->pop(); // color
  1650. if (!annotation.arguments.is_empty()) {
  1651. class_desc->push_color(theme_cache.symbol_color);
  1652. class_desc->add_text("(");
  1653. class_desc->pop(); // color
  1654. for (int j = 0; j < annotation.arguments.size(); j++) {
  1655. const DocData::ArgumentDoc &argument = annotation.arguments[j];
  1656. class_desc->push_color(theme_cache.text_color);
  1657. if (j > 0) {
  1658. class_desc->add_text(", ");
  1659. }
  1660. class_desc->add_text(argument.name);
  1661. class_desc->add_text(colon_nbsp);
  1662. _add_type(argument.type, argument.enumeration, argument.is_bitfield);
  1663. if (!argument.default_value.is_empty()) {
  1664. class_desc->push_color(theme_cache.symbol_color);
  1665. class_desc->add_text(nbsp_equal_nbsp);
  1666. class_desc->pop(); // color
  1667. class_desc->push_color(theme_cache.value_color);
  1668. class_desc->add_text(_fix_constant(argument.default_value));
  1669. class_desc->pop(); // color
  1670. }
  1671. class_desc->pop(); // color
  1672. }
  1673. if (annotation.qualifiers.contains("vararg")) {
  1674. if (!annotation.arguments.is_empty()) {
  1675. class_desc->push_color(theme_cache.text_color);
  1676. class_desc->add_text(", ");
  1677. class_desc->pop(); // color
  1678. }
  1679. class_desc->push_color(theme_cache.symbol_color);
  1680. class_desc->add_text("...");
  1681. class_desc->pop(); // color
  1682. }
  1683. class_desc->push_color(theme_cache.symbol_color);
  1684. class_desc->add_text(")");
  1685. class_desc->pop(); // color
  1686. }
  1687. if (!annotation.qualifiers.is_empty()) {
  1688. class_desc->push_color(theme_cache.qualifier_color);
  1689. _add_qualifiers_to_rt(annotation.qualifiers, class_desc);
  1690. class_desc->pop(); // color
  1691. }
  1692. _pop_code_font();
  1693. class_desc->add_newline();
  1694. // Annotation description.
  1695. class_desc->push_indent(1);
  1696. _push_normal_font();
  1697. class_desc->push_color(theme_cache.comment_color);
  1698. const String descr = HANDLE_DOC(annotation.description);
  1699. if (!descr.is_empty()) {
  1700. _add_text(descr);
  1701. } else {
  1702. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  1703. class_desc->add_text(" ");
  1704. class_desc->push_color(theme_cache.comment_color);
  1705. if (cd.is_script_doc) {
  1706. class_desc->add_text(TTR("There is currently no description for this annotation."));
  1707. } else {
  1708. 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));
  1709. }
  1710. class_desc->pop(); // color
  1711. }
  1712. class_desc->pop(); // color
  1713. _pop_normal_font();
  1714. class_desc->pop(); // indent
  1715. class_desc->pop(); // indent
  1716. }
  1717. }
  1718. // Property descriptions
  1719. if (has_property_descriptions) {
  1720. class_desc->add_newline();
  1721. class_desc->add_newline();
  1722. class_desc->add_newline();
  1723. section_line.push_back(Pair<String, int>(TTR("Property Descriptions"), class_desc->get_paragraph_count() - 2));
  1724. _push_title_font();
  1725. class_desc->add_text(TTR("Property Descriptions"));
  1726. _pop_title_font();
  1727. for (const DocData::PropertyDoc &prop : cd.properties) {
  1728. if (prop.overridden) {
  1729. continue;
  1730. }
  1731. // Ignore undocumented private.
  1732. const bool is_documented = prop.is_deprecated || prop.is_experimental || !prop.description.strip_edges().is_empty();
  1733. if (!is_documented && prop.name.begins_with("_")) {
  1734. continue;
  1735. }
  1736. class_desc->add_newline();
  1737. class_desc->add_newline();
  1738. class_desc->add_newline();
  1739. property_line[prop.name] = class_desc->get_paragraph_count() - 2;
  1740. class_desc->push_table(2);
  1741. class_desc->set_table_column_expand(1, true);
  1742. class_desc->push_cell();
  1743. _push_code_font();
  1744. _add_bulletpoint();
  1745. _add_type(prop.type, prop.enumeration, prop.is_bitfield);
  1746. _pop_code_font();
  1747. class_desc->pop(); // cell
  1748. class_desc->push_cell();
  1749. _push_code_font();
  1750. class_desc->push_color(theme_cache.headline_color);
  1751. class_desc->add_text(prop.name);
  1752. class_desc->pop(); // color
  1753. if (!prop.default_value.is_empty()) {
  1754. class_desc->push_color(theme_cache.symbol_color);
  1755. class_desc->add_text(" [" + TTR("default:") + " ");
  1756. class_desc->pop(); // color
  1757. class_desc->push_color(theme_cache.value_color);
  1758. class_desc->add_text(_fix_constant(prop.default_value));
  1759. class_desc->pop(); // color
  1760. class_desc->push_color(theme_cache.symbol_color);
  1761. class_desc->add_text("]");
  1762. class_desc->pop(); // color
  1763. }
  1764. if (cd.is_script_doc && (!prop.setter.is_empty() || !prop.getter.is_empty())) {
  1765. class_desc->push_color(theme_cache.symbol_color);
  1766. class_desc->add_text(" [" + TTR("property:") + " ");
  1767. class_desc->pop(); // color
  1768. if (!prop.setter.is_empty()) {
  1769. class_desc->push_color(theme_cache.value_color);
  1770. class_desc->add_text("setter");
  1771. class_desc->pop(); // color
  1772. }
  1773. if (!prop.getter.is_empty()) {
  1774. if (!prop.setter.is_empty()) {
  1775. class_desc->push_color(theme_cache.symbol_color);
  1776. class_desc->add_text(", ");
  1777. class_desc->pop(); // color
  1778. }
  1779. class_desc->push_color(theme_cache.value_color);
  1780. class_desc->add_text("getter");
  1781. class_desc->pop(); // color
  1782. }
  1783. class_desc->push_color(theme_cache.symbol_color);
  1784. class_desc->add_text("]");
  1785. class_desc->pop(); // color
  1786. }
  1787. _pop_code_font();
  1788. class_desc->pop(); // cell
  1789. // Script doc doesn't have setter, getter.
  1790. if (!cd.is_script_doc) {
  1791. HashMap<String, DocData::MethodDoc> method_map;
  1792. for (int j = 0; j < methods.size(); j++) {
  1793. method_map[methods[j].name] = methods[j];
  1794. }
  1795. if (!prop.setter.is_empty()) {
  1796. class_desc->push_cell();
  1797. class_desc->pop(); // cell
  1798. class_desc->push_cell();
  1799. _push_code_font();
  1800. class_desc->push_color(theme_cache.text_color);
  1801. if (method_map[prop.setter].arguments.size() > 1) {
  1802. // Setters with additional arguments are exposed in the method list, so we link them here for quick access.
  1803. class_desc->push_meta("@method " + prop.setter);
  1804. class_desc->add_text(prop.setter + TTR("(value)"));
  1805. class_desc->pop(); // meta
  1806. } else {
  1807. class_desc->add_text(prop.setter + TTR("(value)"));
  1808. }
  1809. class_desc->pop(); // color
  1810. class_desc->push_color(theme_cache.comment_color);
  1811. class_desc->add_text(" setter");
  1812. class_desc->pop(); // color
  1813. _pop_code_font();
  1814. class_desc->pop(); // cell
  1815. method_line[prop.setter] = property_line[prop.name];
  1816. }
  1817. if (!prop.getter.is_empty()) {
  1818. class_desc->push_cell();
  1819. class_desc->pop(); // cell
  1820. class_desc->push_cell();
  1821. _push_code_font();
  1822. class_desc->push_color(theme_cache.text_color);
  1823. if (!method_map[prop.getter].arguments.is_empty()) {
  1824. // Getters with additional arguments are exposed in the method list, so we link them here for quick access.
  1825. class_desc->push_meta("@method " + prop.getter);
  1826. class_desc->add_text(prop.getter + "()");
  1827. class_desc->pop(); // meta
  1828. } else {
  1829. class_desc->add_text(prop.getter + "()");
  1830. }
  1831. class_desc->pop(); // color
  1832. class_desc->push_color(theme_cache.comment_color);
  1833. class_desc->add_text(" getter");
  1834. class_desc->pop(); // color
  1835. _pop_code_font();
  1836. class_desc->pop(); // cell
  1837. method_line[prop.getter] = property_line[prop.name];
  1838. }
  1839. }
  1840. class_desc->pop(); // table
  1841. class_desc->add_newline();
  1842. class_desc->add_newline();
  1843. class_desc->push_indent(1);
  1844. _push_normal_font();
  1845. class_desc->push_color(theme_cache.text_color);
  1846. bool has_prev_text = false;
  1847. if (prop.is_deprecated) {
  1848. has_prev_text = true;
  1849. DEPRECATED_DOC_MSG(HANDLE_DOC(prop.deprecated_message), TTR("This property may be changed or removed in future versions."));
  1850. }
  1851. if (prop.is_experimental) {
  1852. if (has_prev_text) {
  1853. class_desc->add_newline();
  1854. class_desc->add_newline();
  1855. }
  1856. has_prev_text = true;
  1857. EXPERIMENTAL_DOC_MSG(HANDLE_DOC(prop.experimental_message), TTR("This property may be changed or removed in future versions."));
  1858. }
  1859. const String descr = HANDLE_DOC(prop.description);
  1860. if (!descr.is_empty()) {
  1861. if (has_prev_text) {
  1862. class_desc->add_newline();
  1863. class_desc->add_newline();
  1864. }
  1865. has_prev_text = true;
  1866. _add_text(descr);
  1867. } else if (!has_prev_text) {
  1868. class_desc->add_image(get_editor_theme_icon(SNAME("Error")));
  1869. class_desc->add_text(" ");
  1870. class_desc->push_color(theme_cache.comment_color);
  1871. if (cd.is_script_doc) {
  1872. class_desc->add_text(TTR("There is currently no description for this property."));
  1873. } else {
  1874. 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));
  1875. }
  1876. class_desc->pop(); // color
  1877. }
  1878. // Add copy note to built-in properties returning `Packed*Array`.
  1879. if (!cd.is_script_doc && packed_array_types.has(prop.type)) {
  1880. class_desc->add_newline();
  1881. class_desc->add_newline();
  1882. _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));
  1883. }
  1884. class_desc->pop(); // color
  1885. _pop_normal_font();
  1886. class_desc->pop(); // indent
  1887. }
  1888. }
  1889. // Constructor descriptions
  1890. if (!cd.constructors.is_empty()) {
  1891. _update_method_descriptions(cd, METHOD_TYPE_CONSTRUCTOR, cd.constructors);
  1892. }
  1893. // Method descriptions
  1894. if (!methods.is_empty()) {
  1895. _update_method_descriptions(cd, METHOD_TYPE_METHOD, methods);
  1896. }
  1897. // Operator descriptions
  1898. if (!cd.operators.is_empty()) {
  1899. _update_method_descriptions(cd, METHOD_TYPE_OPERATOR, cd.operators);
  1900. }
  1901. // Allow the document to be scrolled slightly below the end.
  1902. class_desc->add_newline();
  1903. class_desc->add_newline();
  1904. // Free the scroll.
  1905. scroll_locked = false;
  1906. #undef HANDLE_DOC
  1907. }
  1908. void EditorHelp::_request_help(const String &p_string) {
  1909. Error err = _goto_desc(p_string);
  1910. if (err == OK) {
  1911. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  1912. }
  1913. }
  1914. void EditorHelp::_help_callback(const String &p_topic) {
  1915. String what = p_topic.get_slice(":", 0);
  1916. String clss = p_topic.get_slice(":", 1);
  1917. String name;
  1918. if (p_topic.get_slice_count(":") == 3) {
  1919. name = p_topic.get_slice(":", 2);
  1920. }
  1921. _request_help(clss); // First go to class.
  1922. int line = 0;
  1923. if (what == "class_desc") {
  1924. line = description_line;
  1925. } else if (what == "class_signal") {
  1926. if (signal_line.has(name)) {
  1927. line = signal_line[name];
  1928. }
  1929. } else if (what == "class_method" || what == "class_method_desc") {
  1930. if (method_line.has(name)) {
  1931. line = method_line[name];
  1932. }
  1933. } else if (what == "class_property") {
  1934. if (property_line.has(name)) {
  1935. line = property_line[name];
  1936. }
  1937. } else if (what == "class_enum") {
  1938. if (enum_line.has(name)) {
  1939. line = enum_line[name];
  1940. }
  1941. } else if (what == "class_theme_item") {
  1942. if (theme_property_line.has(name)) {
  1943. line = theme_property_line[name];
  1944. }
  1945. } else if (what == "class_constant") {
  1946. if (constant_line.has(name)) {
  1947. line = constant_line[name];
  1948. }
  1949. } else if (what == "class_annotation") {
  1950. if (annotation_line.has(name)) {
  1951. line = annotation_line[name];
  1952. }
  1953. } else if (what == "class_global") { // Deprecated.
  1954. if (constant_line.has(name)) {
  1955. line = constant_line[name];
  1956. } else if (method_line.has(name)) {
  1957. line = method_line[name];
  1958. } else {
  1959. HashMap<String, HashMap<String, int>>::Iterator iter = enum_values_line.begin();
  1960. while (true) {
  1961. if (iter->value.has(name)) {
  1962. line = iter->value[name];
  1963. break;
  1964. } else if (iter == enum_values_line.last()) {
  1965. break;
  1966. } else {
  1967. ++iter;
  1968. }
  1969. }
  1970. }
  1971. }
  1972. if (class_desc->is_finished()) {
  1973. class_desc->scroll_to_paragraph(line);
  1974. } else {
  1975. scroll_to = line;
  1976. }
  1977. }
  1978. static void _add_text_to_rt(const String &p_bbcode, RichTextLabel *p_rt, const Control *p_owner_node, const String &p_class) {
  1979. const DocTools *doc = EditorHelp::get_doc_data();
  1980. bool is_native = false;
  1981. {
  1982. const HashMap<String, DocData::ClassDoc>::ConstIterator E = doc->class_list.find(p_class);
  1983. if (E && !E->value.is_script_doc) {
  1984. is_native = true;
  1985. }
  1986. }
  1987. const bool using_tab_indent = int(EDITOR_GET("text_editor/behavior/indent/type")) == 0;
  1988. const Ref<Font> doc_font = p_owner_node->get_theme_font(SNAME("doc"), EditorStringName(EditorFonts));
  1989. const Ref<Font> doc_bold_font = p_owner_node->get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
  1990. const Ref<Font> doc_italic_font = p_owner_node->get_theme_font(SNAME("doc_italic"), EditorStringName(EditorFonts));
  1991. const Ref<Font> doc_code_font = p_owner_node->get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
  1992. const Ref<Font> doc_kbd_font = p_owner_node->get_theme_font(SNAME("doc_keyboard"), EditorStringName(EditorFonts));
  1993. const int doc_font_size = p_owner_node->get_theme_font_size(SNAME("doc_size"), EditorStringName(EditorFonts));
  1994. const int doc_code_font_size = p_owner_node->get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
  1995. const int doc_kbd_font_size = p_owner_node->get_theme_font_size(SNAME("doc_keyboard_size"), EditorStringName(EditorFonts));
  1996. const Color type_color = p_owner_node->get_theme_color(SNAME("type_color"), SNAME("EditorHelp"));
  1997. const Color code_color = p_owner_node->get_theme_color(SNAME("code_color"), SNAME("EditorHelp"));
  1998. const Color kbd_color = p_owner_node->get_theme_color(SNAME("kbd_color"), SNAME("EditorHelp"));
  1999. const Color code_dark_color = Color(code_color, 0.8);
  2000. const Color link_color = p_owner_node->get_theme_color(SNAME("link_color"), SNAME("EditorHelp"));
  2001. const Color link_method_color = p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  2002. const Color link_property_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)), 0.25);
  2003. const Color link_annotation_color = link_color.lerp(p_owner_node->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)), 0.5);
  2004. const Color code_bg_color = p_owner_node->get_theme_color(SNAME("code_bg_color"), SNAME("EditorHelp"));
  2005. const Color kbd_bg_color = p_owner_node->get_theme_color(SNAME("kbd_bg_color"), SNAME("EditorHelp"));
  2006. const Color param_bg_color = p_owner_node->get_theme_color(SNAME("param_bg_color"), SNAME("EditorHelp"));
  2007. String bbcode = p_bbcode.dedent().replace("\t", "").replace("\r", "").strip_edges();
  2008. // Select the correct code examples.
  2009. switch ((int)EDITOR_GET("text_editor/help/class_reference_examples")) {
  2010. case 0: // GDScript
  2011. bbcode = bbcode.replace("[gdscript", "[codeblock lang=gdscript"); // Tag can have extra arguments.
  2012. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  2013. for (int pos = bbcode.find("[csharp"); pos != -1; pos = bbcode.find("[csharp")) {
  2014. int end_pos = bbcode.find("[/csharp]");
  2015. if (end_pos == -1) {
  2016. WARN_PRINT("Unclosed [csharp] block or parse fail in code (search for tag errors)");
  2017. break;
  2018. }
  2019. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 9); // 9 is length of "[/csharp]".
  2020. while (bbcode[pos] == '\n') {
  2021. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  2022. }
  2023. }
  2024. break;
  2025. case 1: // C#
  2026. bbcode = bbcode.replace("[csharp", "[codeblock lang=csharp"); // Tag can have extra arguments.
  2027. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  2028. for (int pos = bbcode.find("[gdscript"); pos != -1; pos = bbcode.find("[gdscript")) {
  2029. int end_pos = bbcode.find("[/gdscript]");
  2030. if (end_pos == -1) {
  2031. WARN_PRINT("Unclosed [gdscript] block or parse fail in code (search for tag errors)");
  2032. break;
  2033. }
  2034. bbcode = bbcode.left(pos) + bbcode.substr(end_pos + 11); // 11 is length of "[/gdscript]".
  2035. while (bbcode[pos] == '\n') {
  2036. bbcode = bbcode.left(pos) + bbcode.substr(pos + 1);
  2037. }
  2038. }
  2039. break;
  2040. case 2: // GDScript and C#
  2041. bbcode = bbcode.replace("[csharp", "[b]C#:[/b]\n[codeblock lang=csharp"); // Tag can have extra arguments.
  2042. bbcode = bbcode.replace("[gdscript", "[b]GDScript:[/b]\n[codeblock lang=gdscript"); // Tag can have extra arguments.
  2043. bbcode = bbcode.replace("[/csharp]", "[/codeblock]");
  2044. bbcode = bbcode.replace("[/gdscript]", "[/codeblock]");
  2045. break;
  2046. }
  2047. // Remove codeblocks (they would be printed otherwise).
  2048. bbcode = bbcode.replace("[codeblocks]\n", "");
  2049. bbcode = bbcode.replace("\n[/codeblocks]", "");
  2050. bbcode = bbcode.replace("[codeblocks]", "");
  2051. bbcode = bbcode.replace("[/codeblocks]", "");
  2052. // Remove `\n` here because `\n` is replaced by `\n\n` later.
  2053. // Will be compensated when parsing `[/codeblock]`.
  2054. bbcode = bbcode.replace("[/codeblock]\n", "[/codeblock]");
  2055. List<String> tag_stack;
  2056. int pos = 0;
  2057. while (pos < bbcode.length()) {
  2058. int brk_pos = bbcode.find_char('[', pos);
  2059. if (brk_pos < 0) {
  2060. brk_pos = bbcode.length();
  2061. }
  2062. if (brk_pos > pos) {
  2063. p_rt->add_text(bbcode.substr(pos, brk_pos - pos).replace("\n", "\n\n"));
  2064. }
  2065. if (brk_pos == bbcode.length()) {
  2066. break; // Nothing else to add.
  2067. }
  2068. int brk_end = bbcode.find_char(']', brk_pos + 1);
  2069. if (brk_end == -1) {
  2070. p_rt->add_text(bbcode.substr(brk_pos, bbcode.length() - brk_pos).replace("\n", "\n\n"));
  2071. break;
  2072. }
  2073. const String tag = bbcode.substr(brk_pos + 1, brk_end - brk_pos - 1);
  2074. if (tag.begins_with("/")) {
  2075. bool tag_ok = tag_stack.size() && tag_stack.front()->get() == tag.substr(1);
  2076. if (!tag_ok) {
  2077. p_rt->add_text("[");
  2078. pos = brk_pos + 1;
  2079. continue;
  2080. }
  2081. tag_stack.pop_front();
  2082. pos = brk_end + 1;
  2083. if (tag == "/img") {
  2084. // Nothing to do.
  2085. } else if (tag == "/url") {
  2086. p_rt->pop(); // meta
  2087. p_rt->pop(); // color
  2088. p_rt->add_text(nbsp);
  2089. p_rt->add_image(p_owner_node->get_editor_theme_icon(SNAME("ExternalLink")), 0, doc_font_size, link_color);
  2090. } else {
  2091. p_rt->pop();
  2092. }
  2093. } 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 ")) {
  2094. const int tag_end = tag.find_char(' ');
  2095. const String link_tag = tag.left(tag_end);
  2096. const String link_target = tag.substr(tag_end + 1).lstrip(" ");
  2097. Color target_color = link_color;
  2098. RichTextLabel::MetaUnderline underline_mode = RichTextLabel::META_UNDERLINE_ON_HOVER;
  2099. if (link_tag == "method" || link_tag == "constructor" || link_tag == "operator") {
  2100. target_color = link_method_color;
  2101. } else if (link_tag == "member" || link_tag == "signal" || link_tag == "theme_item") {
  2102. target_color = link_property_color;
  2103. } else if (link_tag == "annotation") {
  2104. target_color = link_annotation_color;
  2105. } else {
  2106. // Better visibility for constants, enums, etc.
  2107. underline_mode = RichTextLabel::META_UNDERLINE_ALWAYS;
  2108. }
  2109. // Use monospace font to make clickable references
  2110. // easier to distinguish from inline code and other text.
  2111. p_rt->push_font(doc_code_font);
  2112. p_rt->push_font_size(doc_code_font_size);
  2113. p_rt->push_color(target_color);
  2114. p_rt->push_meta("@" + link_tag + " " + link_target, underline_mode);
  2115. if (link_tag == "member" &&
  2116. ((!link_target.contains_char('.') && (p_class == "ProjectSettings" || p_class == "EditorSettings")) ||
  2117. link_target.begins_with("ProjectSettings.") || link_target.begins_with("EditorSettings."))) {
  2118. // Special formatting for both ProjectSettings and EditorSettings.
  2119. String prefix;
  2120. if (link_target.begins_with("EditorSettings.")) {
  2121. prefix = "(" + TTR("Editor") + ") ";
  2122. }
  2123. const String setting_name = link_target.trim_prefix("ProjectSettings.").trim_prefix("EditorSettings.");
  2124. PackedStringArray setting_sections;
  2125. for (const String &section : setting_name.split("/", false)) {
  2126. setting_sections.append(EditorPropertyNameProcessor::get_singleton()->process_name(section, EditorPropertyNameProcessor::get_settings_style()));
  2127. }
  2128. p_rt->push_bold();
  2129. p_rt->add_text(prefix + String(" > ").join(setting_sections));
  2130. p_rt->pop(); // bold
  2131. } else {
  2132. p_rt->add_text(link_target + (link_tag == "method" ? "()" : ""));
  2133. }
  2134. p_rt->pop(); // meta
  2135. p_rt->pop(); // color
  2136. p_rt->pop(); // font_size
  2137. p_rt->pop(); // font
  2138. pos = brk_end + 1;
  2139. } else if (tag.begins_with("param ")) {
  2140. const int tag_end = tag.find_char(' ');
  2141. const String param_name = tag.substr(tag_end + 1).lstrip(" ");
  2142. // Use monospace font with translucent background color to make code easier to distinguish from other text.
  2143. p_rt->push_font(doc_code_font);
  2144. p_rt->push_font_size(doc_code_font_size);
  2145. p_rt->push_bgcolor(param_bg_color);
  2146. p_rt->push_color(code_color);
  2147. p_rt->add_text(param_name);
  2148. p_rt->pop(); // color
  2149. p_rt->pop(); // bgcolor
  2150. p_rt->pop(); // font_size
  2151. p_rt->pop(); // font
  2152. pos = brk_end + 1;
  2153. } else if (tag == p_class) {
  2154. // Use a bold font when class reference tags are in their own page.
  2155. p_rt->push_font(doc_bold_font);
  2156. p_rt->add_text(tag);
  2157. p_rt->pop(); // font
  2158. pos = brk_end + 1;
  2159. } else if (doc->class_list.has(tag)) {
  2160. // Use a monospace font for class reference tags such as [Node2D] or [SceneTree].
  2161. p_rt->push_font(doc_code_font);
  2162. p_rt->push_font_size(doc_code_font_size);
  2163. p_rt->push_color(type_color);
  2164. p_rt->push_meta("#" + tag, RichTextLabel::META_UNDERLINE_ON_HOVER);
  2165. p_rt->add_text(tag);
  2166. p_rt->pop(); // meta
  2167. p_rt->pop(); // color
  2168. p_rt->pop(); // font_size
  2169. p_rt->pop(); // font
  2170. pos = brk_end + 1;
  2171. } else if (tag == "b") {
  2172. // Use bold font.
  2173. p_rt->push_font(doc_bold_font);
  2174. pos = brk_end + 1;
  2175. tag_stack.push_front(tag);
  2176. } else if (tag == "i") {
  2177. // Use italics font.
  2178. p_rt->push_font(doc_italic_font);
  2179. pos = brk_end + 1;
  2180. tag_stack.push_front(tag);
  2181. } else if (tag == "code" || tag.begins_with("code ")) {
  2182. int end_pos = bbcode.find("[/code]", brk_end + 1);
  2183. if (end_pos < 0) {
  2184. end_pos = bbcode.length();
  2185. }
  2186. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  2187. p_rt->push_font(doc_code_font);
  2188. p_rt->push_font_size(doc_code_font_size);
  2189. p_rt->push_bgcolor(code_bg_color);
  2190. p_rt->push_color(code_color.lerp(p_owner_node->get_theme_color(SNAME("error_color"), EditorStringName(Editor)), 0.6));
  2191. p_rt->add_text(bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)));
  2192. p_rt->pop(); // color
  2193. p_rt->pop(); // bgcolor
  2194. p_rt->pop(); // font_size
  2195. p_rt->pop(); // font
  2196. pos = end_pos + 7; // `len("[/code]")`.
  2197. } else if (tag == "codeblock" || tag.begins_with("codeblock ")) {
  2198. int end_pos = bbcode.find("[/codeblock]", brk_end + 1);
  2199. if (end_pos < 0) {
  2200. end_pos = bbcode.length();
  2201. }
  2202. const String codeblock_text = bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)).strip_edges();
  2203. String codeblock_copy_text = codeblock_text;
  2204. if (using_tab_indent) {
  2205. // Replace the code block's space indentation with tabs.
  2206. StringBuilder builder;
  2207. PackedStringArray text_lines = codeblock_copy_text.split("\n");
  2208. for (const String &line : text_lines) {
  2209. const String stripped_line = line.dedent();
  2210. const int space_count = line.length() - stripped_line.length();
  2211. if (builder.num_strings_appended() > 0) {
  2212. builder.append("\n");
  2213. }
  2214. if (space_count > 0) {
  2215. builder.append(String("\t").repeat(MAX(space_count / 4, 1)) + stripped_line);
  2216. } else {
  2217. builder.append(line);
  2218. }
  2219. }
  2220. codeblock_copy_text = builder.as_string();
  2221. }
  2222. String lang;
  2223. const PackedStringArray args = tag.trim_prefix("codeblock").split(" ", false);
  2224. for (int i = args.size() - 1; i >= 0; i--) {
  2225. if (args[i].begins_with("lang=")) {
  2226. lang = args[i].trim_prefix("lang=");
  2227. break;
  2228. }
  2229. }
  2230. // Use monospace font with darkened background color to make code easier to distinguish from other text.
  2231. // Use a single-column table with cell row background color instead of `[bgcolor]`.
  2232. // This makes the background color highlight cover the entire block, rather than individual lines.
  2233. p_rt->push_font(doc_code_font);
  2234. p_rt->push_font_size(doc_code_font_size);
  2235. p_rt->push_table(2);
  2236. p_rt->push_cell();
  2237. p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
  2238. p_rt->set_cell_padding(Rect2(10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE, 10 * EDSCALE));
  2239. p_rt->push_color(code_dark_color);
  2240. bool codeblock_printed = false;
  2241. #ifdef MODULE_GDSCRIPT_ENABLED
  2242. if (!codeblock_printed && (lang.is_empty() || lang == "gdscript")) {
  2243. EditorHelpHighlighter::get_singleton()->highlight(p_rt, EditorHelpHighlighter::LANGUAGE_GDSCRIPT, codeblock_text, is_native);
  2244. codeblock_printed = true;
  2245. }
  2246. #endif
  2247. #ifdef MODULE_MONO_ENABLED
  2248. if (!codeblock_printed && lang == "csharp") {
  2249. EditorHelpHighlighter::get_singleton()->highlight(p_rt, EditorHelpHighlighter::LANGUAGE_CSHARP, codeblock_text, is_native);
  2250. codeblock_printed = true;
  2251. }
  2252. #endif
  2253. if (!codeblock_printed) {
  2254. p_rt->add_text(codeblock_text);
  2255. codeblock_printed = true;
  2256. }
  2257. p_rt->pop(); // color
  2258. p_rt->pop(); // cell
  2259. // Copy codeblock button.
  2260. p_rt->push_cell();
  2261. p_rt->set_cell_row_background_color(code_bg_color, Color(code_bg_color, 0.99));
  2262. p_rt->set_cell_padding(Rect2(0, 10 * EDSCALE, 0, 10 * EDSCALE));
  2263. p_rt->set_cell_size_override(Vector2(1, 1), Vector2(10, 10) * EDSCALE);
  2264. p_rt->push_meta("^" + codeblock_copy_text, RichTextLabel::META_UNDERLINE_ON_HOVER);
  2265. 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."));
  2266. p_rt->pop(); // meta
  2267. p_rt->pop(); // cell
  2268. p_rt->pop(); // table
  2269. p_rt->pop(); // font_size
  2270. p_rt->pop(); // font
  2271. pos = end_pos + 12; // `len("[/codeblock]")`.
  2272. // Compensate for `\n` removed before the loop.
  2273. if (pos < bbcode.length()) {
  2274. p_rt->add_newline();
  2275. }
  2276. } else if (tag == "kbd") {
  2277. int end_pos = bbcode.find("[/kbd]", brk_end + 1);
  2278. if (end_pos < 0) {
  2279. end_pos = bbcode.length();
  2280. }
  2281. // Use keyboard font with custom color and background color.
  2282. p_rt->push_font(doc_kbd_font);
  2283. p_rt->push_font_size(doc_kbd_font_size);
  2284. p_rt->push_bgcolor(kbd_bg_color);
  2285. p_rt->push_color(kbd_color);
  2286. p_rt->add_text(bbcode.substr(brk_end + 1, end_pos - (brk_end + 1)));
  2287. p_rt->pop(); // color
  2288. p_rt->pop(); // bgcolor
  2289. p_rt->pop(); // font_size
  2290. p_rt->pop(); // font
  2291. pos = end_pos + 6; // `len("[/kbd]")`.
  2292. } else if (tag == "center") {
  2293. // Align to center.
  2294. p_rt->push_paragraph(HORIZONTAL_ALIGNMENT_CENTER, Control::TEXT_DIRECTION_AUTO, "");
  2295. pos = brk_end + 1;
  2296. tag_stack.push_front(tag);
  2297. } else if (tag == "br") {
  2298. // Force a line break.
  2299. p_rt->add_newline();
  2300. pos = brk_end + 1;
  2301. } else if (tag == "u") {
  2302. // Use underline.
  2303. p_rt->push_underline();
  2304. pos = brk_end + 1;
  2305. tag_stack.push_front(tag);
  2306. } else if (tag == "s") {
  2307. // Use strikethrough.
  2308. p_rt->push_strikethrough();
  2309. pos = brk_end + 1;
  2310. tag_stack.push_front(tag);
  2311. } else if (tag == "lb") {
  2312. p_rt->add_text("[");
  2313. pos = brk_end + 1;
  2314. } else if (tag == "rb") {
  2315. p_rt->add_text("]");
  2316. pos = brk_end + 1;
  2317. } else if (tag == "url" || tag.begins_with("url=")) {
  2318. String url;
  2319. if (tag.begins_with("url=")) {
  2320. url = tag.substr(4);
  2321. } else {
  2322. int end = bbcode.find_char('[', brk_end);
  2323. if (end == -1) {
  2324. end = bbcode.length();
  2325. }
  2326. url = bbcode.substr(brk_end + 1, end - brk_end - 1);
  2327. }
  2328. p_rt->push_color(link_color);
  2329. p_rt->push_meta(url, RichTextLabel::META_UNDERLINE_ON_HOVER, url + "\n\n" + TTR("Click to open in browser."));
  2330. pos = brk_end + 1;
  2331. tag_stack.push_front("url");
  2332. } else if (tag.begins_with("img")) {
  2333. int width = 0;
  2334. int height = 0;
  2335. bool size_in_percent = false;
  2336. if (tag.length() > 4) {
  2337. Vector<String> subtags = tag.substr(4).split(" ");
  2338. HashMap<String, String> bbcode_options;
  2339. for (int i = 0; i < subtags.size(); i++) {
  2340. const String &expr = subtags[i];
  2341. int value_pos = expr.find_char('=');
  2342. if (value_pos > -1) {
  2343. bbcode_options[expr.left(value_pos)] = expr.substr(value_pos + 1).unquote();
  2344. }
  2345. }
  2346. HashMap<String, String>::Iterator width_option = bbcode_options.find("width");
  2347. if (width_option) {
  2348. width = width_option->value.to_int();
  2349. if (width_option->value.ends_with("%")) {
  2350. size_in_percent = true;
  2351. }
  2352. }
  2353. HashMap<String, String>::Iterator height_option = bbcode_options.find("height");
  2354. if (height_option) {
  2355. height = height_option->value.to_int();
  2356. if (height_option->value.ends_with("%")) {
  2357. size_in_percent = true;
  2358. }
  2359. }
  2360. }
  2361. int end = bbcode.find_char('[', brk_end);
  2362. if (end == -1) {
  2363. end = bbcode.length();
  2364. }
  2365. String image_path = bbcode.substr(brk_end + 1, end - brk_end - 1);
  2366. 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);
  2367. pos = end;
  2368. tag_stack.push_front("img");
  2369. } else if (tag.begins_with("color=")) {
  2370. String col = tag.substr(6);
  2371. Color color = Color::from_string(col, Color());
  2372. p_rt->push_color(color);
  2373. pos = brk_end + 1;
  2374. tag_stack.push_front("color");
  2375. } else if (tag.begins_with("font=")) {
  2376. String font_path = tag.substr(5);
  2377. Ref<Font> font = ResourceLoader::load(font_path, "Font");
  2378. if (font.is_valid()) {
  2379. p_rt->push_font(font);
  2380. } else {
  2381. p_rt->push_font(doc_font);
  2382. }
  2383. pos = brk_end + 1;
  2384. tag_stack.push_front("font");
  2385. } else {
  2386. p_rt->add_text("["); // Ignore.
  2387. pos = brk_pos + 1;
  2388. }
  2389. }
  2390. // Close unclosed tags.
  2391. for (const String &tag : tag_stack) {
  2392. if (tag != "img") {
  2393. p_rt->pop();
  2394. }
  2395. }
  2396. }
  2397. void EditorHelp::_add_text(const String &p_bbcode) {
  2398. _add_text_to_rt(p_bbcode, class_desc, this, edited_class);
  2399. }
  2400. void EditorHelp::_wait_for_thread() {
  2401. if (worker_thread.is_started()) {
  2402. worker_thread.wait_to_finish();
  2403. }
  2404. }
  2405. void EditorHelp::_compute_doc_version_hash() {
  2406. uint32_t version_hash = Engine::get_singleton()->get_version_info().hash();
  2407. 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);
  2408. }
  2409. String EditorHelp::get_cache_full_path() {
  2410. return EditorPaths::get_singleton()->get_cache_dir().path_join(vformat("editor_doc_cache-%d.%d.res", VERSION_MAJOR, VERSION_MINOR));
  2411. }
  2412. void EditorHelp::load_xml_buffer(const uint8_t *p_buffer, int p_size) {
  2413. if (!ext_doc) {
  2414. ext_doc = memnew(DocTools);
  2415. }
  2416. ext_doc->load_xml(p_buffer, p_size);
  2417. if (doc) {
  2418. doc->load_xml(p_buffer, p_size);
  2419. }
  2420. }
  2421. void EditorHelp::remove_class(const String &p_class) {
  2422. if (ext_doc && ext_doc->has_doc(p_class)) {
  2423. ext_doc->remove_doc(p_class);
  2424. }
  2425. if (doc && doc->has_doc(p_class)) {
  2426. doc->remove_doc(p_class);
  2427. }
  2428. }
  2429. void EditorHelp::_load_doc_thread(void *p_udata) {
  2430. Ref<Resource> cache_res = ResourceLoader::load(get_cache_full_path());
  2431. if (cache_res.is_valid() && cache_res->get_meta("version_hash", "") == doc_version_hash) {
  2432. Array classes = cache_res->get_meta("classes", Array());
  2433. for (int i = 0; i < classes.size(); i++) {
  2434. doc->add_doc(DocData::ClassDoc::from_dict(classes[i]));
  2435. }
  2436. // Extensions' docs are not cached. Generate them now (on the main thread).
  2437. callable_mp_static(&EditorHelp::_gen_extensions_docs).call_deferred();
  2438. } else {
  2439. // We have to go back to the main thread to start from scratch, bypassing any possibly existing cache.
  2440. callable_mp_static(&EditorHelp::generate_doc).call_deferred(false);
  2441. }
  2442. OS::get_singleton()->benchmark_end_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
  2443. }
  2444. void EditorHelp::_gen_doc_thread(void *p_udata) {
  2445. DocTools compdoc;
  2446. compdoc.load_compressed(_doc_data_compressed, _doc_data_compressed_size, _doc_data_uncompressed_size);
  2447. doc->merge_from(compdoc); // Ensure all is up to date.
  2448. Ref<Resource> cache_res;
  2449. cache_res.instantiate();
  2450. cache_res->set_meta("version_hash", doc_version_hash);
  2451. Array classes;
  2452. for (const KeyValue<String, DocData::ClassDoc> &E : doc->class_list) {
  2453. if (ClassDB::class_exists(E.value.name)) {
  2454. ClassDB::APIType api = ClassDB::get_api_type(E.value.name);
  2455. if (api == ClassDB::API_EXTENSION || api == ClassDB::API_EDITOR_EXTENSION) {
  2456. continue;
  2457. }
  2458. }
  2459. classes.push_back(DocData::ClassDoc::to_dict(E.value));
  2460. }
  2461. cache_res->set_meta("classes", classes);
  2462. Error err = ResourceSaver::save(cache_res, get_cache_full_path(), ResourceSaver::FLAG_COMPRESS);
  2463. if (err) {
  2464. ERR_PRINT("Cannot save editor help cache (" + get_cache_full_path() + ").");
  2465. }
  2466. OS::get_singleton()->benchmark_end_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
  2467. }
  2468. void EditorHelp::_gen_extensions_docs() {
  2469. doc->generate((DocTools::GENERATE_FLAG_SKIP_BASIC_TYPES | DocTools::GENERATE_FLAG_EXTENSION_CLASSES_ONLY));
  2470. // Append extra doc data, as it gets overridden by the generation step.
  2471. if (ext_doc) {
  2472. doc->merge_from(*ext_doc);
  2473. }
  2474. }
  2475. void EditorHelp::generate_doc(bool p_use_cache) {
  2476. doc_generation_count++;
  2477. OS::get_singleton()->benchmark_begin_measure("EditorHelp", vformat("Generate Documentation (Run %d)", doc_generation_count));
  2478. // In case not the first attempt.
  2479. _wait_for_thread();
  2480. if (!doc) {
  2481. doc = memnew(DocTools);
  2482. }
  2483. if (doc_version_hash.is_empty()) {
  2484. _compute_doc_version_hash();
  2485. }
  2486. if (p_use_cache && FileAccess::exists(get_cache_full_path())) {
  2487. worker_thread.start(_load_doc_thread, nullptr);
  2488. } else {
  2489. print_verbose("Regenerating editor help cache");
  2490. doc->generate();
  2491. worker_thread.start(_gen_doc_thread, nullptr);
  2492. }
  2493. }
  2494. void EditorHelp::_toggle_scripts_pressed() {
  2495. ScriptEditor::get_singleton()->toggle_scripts_panel();
  2496. update_toggle_scripts_button();
  2497. }
  2498. void EditorHelp::_notification(int p_what) {
  2499. switch (p_what) {
  2500. case NOTIFICATION_POSTINITIALIZE: {
  2501. // Requires theme to be up to date.
  2502. _class_desc_resized(false);
  2503. } break;
  2504. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  2505. bool need_update = false;
  2506. if (EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/help")) {
  2507. need_update = true;
  2508. }
  2509. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  2510. if (!need_update && EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor/theme/highlighting")) {
  2511. need_update = true;
  2512. }
  2513. #endif
  2514. if (!need_update) {
  2515. break;
  2516. }
  2517. [[fallthrough]];
  2518. }
  2519. case NOTIFICATION_READY: {
  2520. _wait_for_thread();
  2521. _update_doc();
  2522. } break;
  2523. case NOTIFICATION_THEME_CHANGED: {
  2524. if (is_inside_tree()) {
  2525. _class_desc_resized(true);
  2526. }
  2527. update_toggle_scripts_button();
  2528. } break;
  2529. case NOTIFICATION_VISIBILITY_CHANGED: {
  2530. update_toggle_scripts_button();
  2531. } break;
  2532. }
  2533. }
  2534. void EditorHelp::go_to_help(const String &p_help) {
  2535. _wait_for_thread();
  2536. _help_callback(p_help);
  2537. }
  2538. void EditorHelp::go_to_class(const String &p_class) {
  2539. _wait_for_thread();
  2540. _goto_desc(p_class);
  2541. }
  2542. void EditorHelp::update_doc() {
  2543. _wait_for_thread();
  2544. ERR_FAIL_COND(!doc->class_list.has(edited_class));
  2545. ERR_FAIL_COND(!doc->class_list[edited_class].is_script_doc);
  2546. _update_doc();
  2547. }
  2548. void EditorHelp::cleanup_doc() {
  2549. _wait_for_thread();
  2550. memdelete(doc);
  2551. doc = nullptr;
  2552. }
  2553. Vector<Pair<String, int>> EditorHelp::get_sections() {
  2554. _wait_for_thread();
  2555. Vector<Pair<String, int>> sections;
  2556. for (int i = 0; i < section_line.size(); i++) {
  2557. sections.push_back(Pair<String, int>(section_line[i].first, i));
  2558. }
  2559. return sections;
  2560. }
  2561. void EditorHelp::scroll_to_section(int p_section_index) {
  2562. _wait_for_thread();
  2563. int line = section_line[p_section_index].second;
  2564. if (class_desc->is_finished()) {
  2565. class_desc->scroll_to_paragraph(line);
  2566. } else {
  2567. scroll_to = line;
  2568. }
  2569. }
  2570. void EditorHelp::popup_search() {
  2571. _wait_for_thread();
  2572. find_bar->popup_search();
  2573. }
  2574. String EditorHelp::get_class() {
  2575. return edited_class;
  2576. }
  2577. void EditorHelp::search_again(bool p_search_previous) {
  2578. _search(p_search_previous);
  2579. }
  2580. int EditorHelp::get_scroll() const {
  2581. return class_desc->get_v_scroll_bar()->get_value();
  2582. }
  2583. void EditorHelp::set_scroll(int p_scroll) {
  2584. class_desc->get_v_scroll_bar()->set_value(p_scroll);
  2585. }
  2586. void EditorHelp::update_toggle_scripts_button() {
  2587. if (is_layout_rtl()) {
  2588. toggle_scripts_button->set_button_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Forward") : SNAME("Back")));
  2589. } else {
  2590. toggle_scripts_button->set_button_icon(get_editor_theme_icon(ScriptEditor::get_singleton()->is_scripts_panel_toggled() ? SNAME("Back") : SNAME("Forward")));
  2591. }
  2592. toggle_scripts_button->set_tooltip_text(vformat("%s (%s)", TTR("Toggle Scripts Panel"), ED_GET_SHORTCUT("script_editor/toggle_scripts_panel")->get_as_text()));
  2593. }
  2594. void EditorHelp::_bind_methods() {
  2595. ClassDB::bind_method("_class_list_select", &EditorHelp::_class_list_select);
  2596. ClassDB::bind_method("_request_help", &EditorHelp::_request_help);
  2597. ClassDB::bind_method("_search", &EditorHelp::_search);
  2598. ClassDB::bind_method("_help_callback", &EditorHelp::_help_callback);
  2599. ADD_SIGNAL(MethodInfo("go_to_help"));
  2600. ADD_SIGNAL(MethodInfo("request_save_history"));
  2601. }
  2602. void EditorHelp::init_gdext_pointers() {
  2603. GDExtensionEditorHelp::editor_help_load_xml_buffer = &EditorHelp::load_xml_buffer;
  2604. GDExtensionEditorHelp::editor_help_remove_class = &EditorHelp::remove_class;
  2605. }
  2606. EditorHelp::EditorHelp() {
  2607. set_custom_minimum_size(Size2(150 * EDSCALE, 0));
  2608. class_desc = memnew(RichTextLabel);
  2609. class_desc->set_tab_size(8);
  2610. add_child(class_desc);
  2611. class_desc->set_threaded(true);
  2612. class_desc->set_v_size_flags(SIZE_EXPAND_FILL);
  2613. class_desc->connect(SceneStringName(finished), callable_mp(this, &EditorHelp::_class_desc_finished));
  2614. class_desc->connect("meta_clicked", callable_mp(this, &EditorHelp::_class_desc_select));
  2615. class_desc->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelp::_class_desc_input));
  2616. class_desc->connect(SceneStringName(resized), callable_mp(this, &EditorHelp::_class_desc_resized).bind(false));
  2617. // Added second so it opens at the bottom so it won't offset the entire widget.
  2618. find_bar = memnew(FindBar);
  2619. add_child(find_bar);
  2620. find_bar->hide();
  2621. find_bar->set_rich_text_label(class_desc);
  2622. status_bar = memnew(HBoxContainer);
  2623. add_child(status_bar);
  2624. status_bar->set_h_size_flags(SIZE_EXPAND_FILL);
  2625. status_bar->set_custom_minimum_size(Size2(0, 24 * EDSCALE));
  2626. toggle_scripts_button = memnew(Button);
  2627. toggle_scripts_button->set_flat(true);
  2628. toggle_scripts_button->connect(SceneStringName(pressed), callable_mp(this, &EditorHelp::_toggle_scripts_pressed));
  2629. status_bar->add_child(toggle_scripts_button);
  2630. class_desc->set_selection_enabled(true);
  2631. class_desc->set_context_menu_enabled(true);
  2632. class_desc->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
  2633. class_desc->hide();
  2634. }
  2635. EditorHelp::~EditorHelp() {
  2636. }
  2637. DocTools *EditorHelp::get_doc_data() {
  2638. _wait_for_thread();
  2639. return doc;
  2640. }
  2641. /// EditorHelpBit ///
  2642. #define HANDLE_DOC(m_string) ((is_native ? DTR(m_string) : (m_string)).strip_edges())
  2643. EditorHelpBit::HelpData EditorHelpBit::_get_class_help_data(const StringName &p_class_name) {
  2644. if (doc_class_cache.has(p_class_name)) {
  2645. return doc_class_cache[p_class_name];
  2646. }
  2647. HelpData result;
  2648. const HashMap<String, DocData::ClassDoc>::ConstIterator E = EditorHelp::get_doc_data()->class_list.find(p_class_name);
  2649. if (E) {
  2650. // Non-native class shouldn't be cached, nor translated.
  2651. const bool is_native = !E->value.is_script_doc;
  2652. const String brief_description = HANDLE_DOC(E->value.brief_description);
  2653. const String long_description = HANDLE_DOC(E->value.description);
  2654. if (!brief_description.is_empty()) {
  2655. result.description += "[b]" + brief_description + "[/b]";
  2656. }
  2657. if (!long_description.is_empty()) {
  2658. if (!result.description.is_empty()) {
  2659. result.description += '\n';
  2660. }
  2661. result.description += long_description;
  2662. }
  2663. if (E->value.is_deprecated) {
  2664. if (E->value.deprecated_message.is_empty()) {
  2665. result.deprecated_message = TTR("This class may be changed or removed in future versions.");
  2666. } else {
  2667. result.deprecated_message = HANDLE_DOC(E->value.deprecated_message);
  2668. }
  2669. }
  2670. if (E->value.is_experimental) {
  2671. if (E->value.experimental_message.is_empty()) {
  2672. result.experimental_message = TTR("This class may be changed or removed in future versions.");
  2673. } else {
  2674. result.experimental_message = HANDLE_DOC(E->value.experimental_message);
  2675. }
  2676. }
  2677. if (is_native) {
  2678. doc_class_cache[p_class_name] = result;
  2679. }
  2680. }
  2681. return result;
  2682. }
  2683. EditorHelpBit::HelpData EditorHelpBit::_get_enum_help_data(const StringName &p_class_name, const StringName &p_enum_name) {
  2684. if (doc_enum_cache.has(p_class_name) && doc_enum_cache[p_class_name].has(p_enum_name)) {
  2685. return doc_enum_cache[p_class_name][p_enum_name];
  2686. }
  2687. HelpData result;
  2688. const DocTools *dd = EditorHelp::get_doc_data();
  2689. const HashMap<String, DocData::ClassDoc>::ConstIterator E = dd->class_list.find(p_class_name);
  2690. if (E) {
  2691. // Non-native enums shouldn't be cached, nor translated.
  2692. const bool is_native = !E->value.is_script_doc;
  2693. for (const KeyValue<String, DocData::EnumDoc> &kv : E->value.enums) {
  2694. const StringName enum_name = kv.key;
  2695. const DocData::EnumDoc &enum_doc = kv.value;
  2696. HelpData current;
  2697. current.description = HANDLE_DOC(enum_doc.description);
  2698. if (enum_doc.is_deprecated) {
  2699. if (enum_doc.deprecated_message.is_empty()) {
  2700. current.deprecated_message = TTR("This enumeration may be changed or removed in future versions.");
  2701. } else {
  2702. current.deprecated_message = HANDLE_DOC(enum_doc.deprecated_message);
  2703. }
  2704. }
  2705. if (enum_doc.is_experimental) {
  2706. if (enum_doc.experimental_message.is_empty()) {
  2707. current.experimental_message = TTR("This enumeration may be changed or removed in future versions.");
  2708. } else {
  2709. current.experimental_message = HANDLE_DOC(enum_doc.experimental_message);
  2710. }
  2711. }
  2712. if (enum_name == p_enum_name) {
  2713. result = current;
  2714. if (!is_native) {
  2715. break;
  2716. }
  2717. }
  2718. if (is_native) {
  2719. doc_enum_cache[p_class_name][enum_name] = current;
  2720. }
  2721. }
  2722. }
  2723. return result;
  2724. }
  2725. EditorHelpBit::HelpData EditorHelpBit::_get_constant_help_data(const StringName &p_class_name, const StringName &p_constant_name) {
  2726. if (doc_constant_cache.has(p_class_name) && doc_constant_cache[p_class_name].has(p_constant_name)) {
  2727. return doc_constant_cache[p_class_name][p_constant_name];
  2728. }
  2729. HelpData result;
  2730. const DocTools *dd = EditorHelp::get_doc_data();
  2731. const HashMap<String, DocData::ClassDoc>::ConstIterator E = dd->class_list.find(p_class_name);
  2732. if (E) {
  2733. // Non-native constants shouldn't be cached, nor translated.
  2734. const bool is_native = !E->value.is_script_doc;
  2735. for (const DocData::ConstantDoc &constant : E->value.constants) {
  2736. HelpData current;
  2737. current.description = HANDLE_DOC(constant.description);
  2738. if (constant.is_deprecated) {
  2739. if (constant.deprecated_message.is_empty()) {
  2740. current.deprecated_message = TTR("This constant may be changed or removed in future versions.");
  2741. } else {
  2742. current.deprecated_message = HANDLE_DOC(constant.deprecated_message);
  2743. }
  2744. }
  2745. if (constant.is_experimental) {
  2746. if (constant.experimental_message.is_empty()) {
  2747. current.experimental_message = TTR("This constant may be changed or removed in future versions.");
  2748. } else {
  2749. current.experimental_message = HANDLE_DOC(constant.experimental_message);
  2750. }
  2751. }
  2752. current.doc_type = { constant.type, constant.enumeration, constant.is_bitfield };
  2753. if (constant.is_value_valid) {
  2754. current.value = constant.value;
  2755. }
  2756. if (constant.name == p_constant_name) {
  2757. result = current;
  2758. if (!is_native) {
  2759. break;
  2760. }
  2761. }
  2762. if (is_native) {
  2763. doc_constant_cache[p_class_name][constant.name] = current;
  2764. }
  2765. }
  2766. }
  2767. return result;
  2768. }
  2769. EditorHelpBit::HelpData EditorHelpBit::_get_property_help_data(const StringName &p_class_name, const StringName &p_property_name) {
  2770. if (doc_property_cache.has(p_class_name) && doc_property_cache[p_class_name].has(p_property_name)) {
  2771. return doc_property_cache[p_class_name][p_property_name];
  2772. }
  2773. HelpData result;
  2774. const DocTools *dd = EditorHelp::get_doc_data();
  2775. const HashMap<String, DocData::ClassDoc>::ConstIterator E = dd->class_list.find(p_class_name);
  2776. if (E) {
  2777. // Non-native properties shouldn't be cached, nor translated.
  2778. const bool is_native = !E->value.is_script_doc;
  2779. for (const DocData::PropertyDoc &property : E->value.properties) {
  2780. HelpData current;
  2781. current.description = HANDLE_DOC(property.description);
  2782. if (property.is_deprecated) {
  2783. if (property.deprecated_message.is_empty()) {
  2784. current.deprecated_message = TTR("This property may be changed or removed in future versions.");
  2785. } else {
  2786. current.deprecated_message = HANDLE_DOC(property.deprecated_message);
  2787. }
  2788. }
  2789. if (property.is_experimental) {
  2790. if (property.experimental_message.is_empty()) {
  2791. current.experimental_message = TTR("This property may be changed or removed in future versions.");
  2792. } else {
  2793. current.experimental_message = HANDLE_DOC(property.experimental_message);
  2794. }
  2795. }
  2796. current.doc_type = { property.type, property.enumeration, property.is_bitfield };
  2797. current.value = property.default_value;
  2798. String enum_class_name;
  2799. String enum_name;
  2800. if (CoreConstants::is_global_enum(property.enumeration)) {
  2801. enum_class_name = "@GlobalScope";
  2802. enum_name = property.enumeration;
  2803. } else {
  2804. const int dot_pos = property.enumeration.rfind_char('.');
  2805. if (dot_pos >= 0) {
  2806. enum_class_name = property.enumeration.left(dot_pos);
  2807. enum_name = property.enumeration.substr(dot_pos + 1);
  2808. }
  2809. }
  2810. if (!enum_class_name.is_empty() && !enum_name.is_empty()) {
  2811. // Classes can use enums from other classes, so check from which it came.
  2812. const HashMap<String, DocData::ClassDoc>::ConstIterator enum_class = dd->class_list.find(enum_class_name);
  2813. if (enum_class) {
  2814. const String enum_prefix = EditorPropertyNameProcessor::get_singleton()->process_name(enum_name, EditorPropertyNameProcessor::STYLE_CAPITALIZED) + " ";
  2815. for (DocData::ConstantDoc constant : enum_class->value.constants) {
  2816. // Don't display `_MAX` enum value descriptions, as these are never exposed in the inspector.
  2817. if (constant.enumeration == enum_name && !constant.name.ends_with("_MAX")) {
  2818. // Prettify the enum value display, so that "<ENUM_NAME>_<ITEM>" becomes "Item".
  2819. const String item_name = EditorPropertyNameProcessor::get_singleton()->process_name(constant.name, EditorPropertyNameProcessor::STYLE_CAPITALIZED).trim_prefix(enum_prefix);
  2820. String item_descr = HANDLE_DOC(constant.description);
  2821. if (item_descr.is_empty()) {
  2822. item_descr = "[color=<EditorHelpBitCommentColor>][i]" + TTR("No description available.") + "[/i][/color]";
  2823. }
  2824. current.description += vformat("\n[b]%s:[/b] %s", item_name, item_descr);
  2825. }
  2826. }
  2827. current.description = current.description.lstrip("\n");
  2828. }
  2829. }
  2830. if (property.name == p_property_name) {
  2831. result = current;
  2832. if (!is_native) {
  2833. break;
  2834. }
  2835. }
  2836. if (is_native) {
  2837. doc_property_cache[p_class_name][property.name] = current;
  2838. }
  2839. }
  2840. }
  2841. return result;
  2842. }
  2843. EditorHelpBit::HelpData EditorHelpBit::_get_theme_item_help_data(const StringName &p_class_name, const StringName &p_theme_item_name) {
  2844. if (doc_theme_item_cache.has(p_class_name) && doc_theme_item_cache[p_class_name].has(p_theme_item_name)) {
  2845. return doc_theme_item_cache[p_class_name][p_theme_item_name];
  2846. }
  2847. HelpData result;
  2848. bool found = false;
  2849. const DocTools *dd = EditorHelp::get_doc_data();
  2850. HashMap<String, DocData::ClassDoc>::ConstIterator E = dd->class_list.find(p_class_name);
  2851. while (E) {
  2852. // Non-native theme items shouldn't be cached, nor translated.
  2853. const bool is_native = !E->value.is_script_doc;
  2854. for (const DocData::ThemeItemDoc &theme_item : E->value.theme_properties) {
  2855. HelpData current;
  2856. current.description = HANDLE_DOC(theme_item.description);
  2857. if (theme_item.is_deprecated) {
  2858. if (theme_item.deprecated_message.is_empty()) {
  2859. current.deprecated_message = TTR("This theme property may be changed or removed in future versions.");
  2860. } else {
  2861. current.deprecated_message = HANDLE_DOC(theme_item.deprecated_message);
  2862. }
  2863. }
  2864. if (theme_item.is_experimental) {
  2865. if (theme_item.experimental_message.is_empty()) {
  2866. current.experimental_message = TTR("This theme property may be changed or removed in future versions.");
  2867. } else {
  2868. current.experimental_message = HANDLE_DOC(theme_item.experimental_message);
  2869. }
  2870. }
  2871. current.doc_type = { theme_item.type, String(), false };
  2872. current.value = theme_item.default_value;
  2873. if (theme_item.name == p_theme_item_name) {
  2874. result = current;
  2875. found = true;
  2876. if (!is_native) {
  2877. break;
  2878. }
  2879. }
  2880. if (is_native) {
  2881. doc_theme_item_cache[p_class_name][theme_item.name] = current;
  2882. }
  2883. }
  2884. if (found || E->value.inherits.is_empty()) {
  2885. break;
  2886. }
  2887. // Check for inherited theme items.
  2888. E = dd->class_list.find(E->value.inherits);
  2889. }
  2890. return result;
  2891. }
  2892. EditorHelpBit::HelpData EditorHelpBit::_get_method_help_data(const StringName &p_class_name, const StringName &p_method_name) {
  2893. if (doc_method_cache.has(p_class_name) && doc_method_cache[p_class_name].has(p_method_name)) {
  2894. return doc_method_cache[p_class_name][p_method_name];
  2895. }
  2896. HelpData result;
  2897. const HashMap<String, DocData::ClassDoc>::ConstIterator E = EditorHelp::get_doc_data()->class_list.find(p_class_name);
  2898. if (E) {
  2899. // Non-native methods shouldn't be cached, nor translated.
  2900. const bool is_native = !E->value.is_script_doc;
  2901. for (const DocData::MethodDoc &method : E->value.methods) {
  2902. HelpData current;
  2903. current.description = HANDLE_DOC(method.description);
  2904. if (method.is_deprecated) {
  2905. if (method.deprecated_message.is_empty()) {
  2906. current.deprecated_message = TTR("This method may be changed or removed in future versions.");
  2907. } else {
  2908. current.deprecated_message = HANDLE_DOC(method.deprecated_message);
  2909. }
  2910. }
  2911. if (method.is_experimental) {
  2912. if (method.experimental_message.is_empty()) {
  2913. current.experimental_message = TTR("This method may be changed or removed in future versions.");
  2914. } else {
  2915. current.experimental_message = HANDLE_DOC(method.experimental_message);
  2916. }
  2917. }
  2918. current.doc_type = { method.return_type, method.return_enum, method.return_is_bitfield };
  2919. for (const DocData::ArgumentDoc &argument : method.arguments) {
  2920. const DocType argument_type = { argument.type, argument.enumeration, argument.is_bitfield };
  2921. current.arguments.push_back({ argument.name, argument_type, argument.default_value });
  2922. }
  2923. current.qualifiers = method.qualifiers;
  2924. if (method.name == p_method_name) {
  2925. result = current;
  2926. if (!is_native) {
  2927. break;
  2928. }
  2929. }
  2930. if (is_native) {
  2931. doc_method_cache[p_class_name][method.name] = current;
  2932. }
  2933. }
  2934. }
  2935. return result;
  2936. }
  2937. EditorHelpBit::HelpData EditorHelpBit::_get_signal_help_data(const StringName &p_class_name, const StringName &p_signal_name) {
  2938. if (doc_signal_cache.has(p_class_name) && doc_signal_cache[p_class_name].has(p_signal_name)) {
  2939. return doc_signal_cache[p_class_name][p_signal_name];
  2940. }
  2941. HelpData result;
  2942. const HashMap<String, DocData::ClassDoc>::ConstIterator E = EditorHelp::get_doc_data()->class_list.find(p_class_name);
  2943. if (E) {
  2944. // Non-native signals shouldn't be cached, nor translated.
  2945. const bool is_native = !E->value.is_script_doc;
  2946. for (const DocData::MethodDoc &signal : E->value.signals) {
  2947. HelpData current;
  2948. current.description = HANDLE_DOC(signal.description);
  2949. if (signal.is_deprecated) {
  2950. if (signal.deprecated_message.is_empty()) {
  2951. current.deprecated_message = TTR("This signal may be changed or removed in future versions.");
  2952. } else {
  2953. current.deprecated_message = HANDLE_DOC(signal.deprecated_message);
  2954. }
  2955. }
  2956. if (signal.is_experimental) {
  2957. if (signal.experimental_message.is_empty()) {
  2958. current.experimental_message = TTR("This signal may be changed or removed in future versions.");
  2959. } else {
  2960. current.experimental_message = HANDLE_DOC(signal.experimental_message);
  2961. }
  2962. }
  2963. for (const DocData::ArgumentDoc &argument : signal.arguments) {
  2964. const DocType argument_type = { argument.type, argument.enumeration, argument.is_bitfield };
  2965. current.arguments.push_back({ argument.name, argument_type, argument.default_value });
  2966. }
  2967. current.qualifiers = signal.qualifiers;
  2968. if (signal.name == p_signal_name) {
  2969. result = current;
  2970. if (!is_native) {
  2971. break;
  2972. }
  2973. }
  2974. if (is_native) {
  2975. doc_signal_cache[p_class_name][signal.name] = current;
  2976. }
  2977. }
  2978. }
  2979. return result;
  2980. }
  2981. EditorHelpBit::HelpData EditorHelpBit::_get_annotation_help_data(const StringName &p_class_name, const StringName &p_annotation_name) {
  2982. if (doc_annotation_cache.has(p_class_name) && doc_annotation_cache[p_class_name].has(p_annotation_name)) {
  2983. return doc_annotation_cache[p_class_name][p_annotation_name];
  2984. }
  2985. HelpData result;
  2986. const HashMap<String, DocData::ClassDoc>::ConstIterator E = EditorHelp::get_doc_data()->class_list.find(p_class_name);
  2987. if (E) {
  2988. // Non-native annotations shouldn't be cached, nor translated.
  2989. const bool is_native = !E->value.is_script_doc;
  2990. for (const DocData::MethodDoc &annotation : E->value.annotations) {
  2991. HelpData current;
  2992. current.description = HANDLE_DOC(annotation.description);
  2993. if (annotation.is_deprecated) {
  2994. if (annotation.deprecated_message.is_empty()) {
  2995. current.deprecated_message = TTR("This annotation may be changed or removed in future versions.");
  2996. } else {
  2997. current.deprecated_message = HANDLE_DOC(annotation.deprecated_message);
  2998. }
  2999. }
  3000. if (annotation.is_experimental) {
  3001. if (annotation.experimental_message.is_empty()) {
  3002. current.experimental_message = TTR("This annotation may be changed or removed in future versions.");
  3003. } else {
  3004. current.experimental_message = HANDLE_DOC(annotation.experimental_message);
  3005. }
  3006. }
  3007. for (const DocData::ArgumentDoc &argument : annotation.arguments) {
  3008. const DocType argument_type = { argument.type, argument.enumeration, argument.is_bitfield };
  3009. current.arguments.push_back({ argument.name, argument_type, argument.default_value });
  3010. }
  3011. current.qualifiers = annotation.qualifiers;
  3012. if (annotation.name == p_annotation_name) {
  3013. result = current;
  3014. if (!is_native) {
  3015. break;
  3016. }
  3017. }
  3018. if (is_native) {
  3019. doc_annotation_cache[p_class_name][annotation.name] = current;
  3020. }
  3021. }
  3022. }
  3023. return result;
  3024. }
  3025. #undef HANDLE_DOC
  3026. void EditorHelpBit::_add_type_to_title(const DocType &p_doc_type) {
  3027. _add_type_to_rt(p_doc_type.type, p_doc_type.enumeration, p_doc_type.is_bitfield, title, this, symbol_class_name);
  3028. }
  3029. void EditorHelpBit::_update_labels() {
  3030. const Ref<Font> doc_bold_font = get_theme_font(SNAME("doc_bold"), EditorStringName(EditorFonts));
  3031. if (!symbol_type.is_empty() || !symbol_name.is_empty()) {
  3032. title->clear();
  3033. title->push_font(doc_bold_font);
  3034. if (!symbol_type.is_empty()) {
  3035. title->push_color(get_theme_color(SNAME("title_color"), SNAME("EditorHelp")));
  3036. title->add_text(symbol_type);
  3037. title->pop(); // color
  3038. }
  3039. if (!symbol_type.is_empty() && !symbol_name.is_empty()) {
  3040. title->add_text(" ");
  3041. }
  3042. if (!symbol_name.is_empty()) {
  3043. if (!symbol_doc_link.is_empty()) {
  3044. title->push_meta(symbol_doc_link, RichTextLabel::META_UNDERLINE_ON_HOVER);
  3045. }
  3046. if (use_class_prefix && !symbol_class_name.is_empty() && symbol_hint != SYMBOL_HINT_INHERITANCE) {
  3047. title->add_text(symbol_class_name + ".");
  3048. }
  3049. title->add_text(symbol_name);
  3050. if (!symbol_doc_link.is_empty()) {
  3051. title->pop(); // meta
  3052. }
  3053. }
  3054. title->pop(); // font
  3055. const Color symbol_color = get_theme_color(SNAME("symbol_color"), SNAME("EditorHelp"));
  3056. const Color value_color = get_theme_color(SNAME("value_color"), SNAME("EditorHelp"));
  3057. const Color qualifier_color = get_theme_color(SNAME("qualifier_color"), SNAME("EditorHelp"));
  3058. const Ref<Font> doc_source = get_theme_font(SNAME("doc_source"), EditorStringName(EditorFonts));
  3059. const int doc_source_size = get_theme_font_size(SNAME("doc_source_size"), EditorStringName(EditorFonts));
  3060. switch (symbol_hint) {
  3061. case SYMBOL_HINT_NONE: {
  3062. // Nothing to do.
  3063. } break;
  3064. case SYMBOL_HINT_INHERITANCE: {
  3065. const HashMap<String, DocData::ClassDoc> &class_list = EditorHelp::get_doc_data()->class_list;
  3066. const DocData::ClassDoc *class_doc = class_list.getptr(symbol_class_name);
  3067. String inherits = class_doc ? class_doc->inherits : String();
  3068. if (!inherits.is_empty()) {
  3069. title->push_font(doc_source);
  3070. title->push_font_size(doc_source_size * 0.9);
  3071. while (!inherits.is_empty()) {
  3072. title->push_color(symbol_color);
  3073. title->add_text(" <" + nbsp);
  3074. title->pop(); // color
  3075. _add_type_to_title({ inherits, String(), false });
  3076. const DocData::ClassDoc *base_class_doc = class_list.getptr(inherits);
  3077. inherits = base_class_doc ? base_class_doc->inherits : String();
  3078. }
  3079. title->pop(); // font_size
  3080. title->pop(); // font
  3081. }
  3082. } break;
  3083. case SYMBOL_HINT_ASSIGNABLE: {
  3084. const bool has_type = !help_data.doc_type.type.is_empty();
  3085. const bool has_value = !help_data.value.is_empty();
  3086. if (has_type || has_value) {
  3087. title->push_font(doc_source);
  3088. title->push_font_size(doc_source_size * 0.9);
  3089. if (has_type) {
  3090. title->push_color(symbol_color);
  3091. title->add_text(colon_nbsp);
  3092. title->pop(); // color
  3093. _add_type_to_title(help_data.doc_type);
  3094. }
  3095. if (has_value) {
  3096. title->push_color(symbol_color);
  3097. title->add_text(nbsp_equal_nbsp);
  3098. title->pop(); // color
  3099. title->push_color(value_color);
  3100. title->add_text(_fix_constant(help_data.value));
  3101. title->pop(); // color
  3102. }
  3103. title->pop(); // font_size
  3104. title->pop(); // font
  3105. }
  3106. } break;
  3107. case SYMBOL_HINT_SIGNATURE: {
  3108. title->push_font(doc_source);
  3109. title->push_font_size(doc_source_size * 0.9);
  3110. title->push_color(symbol_color);
  3111. title->add_text("(");
  3112. title->pop(); // color
  3113. for (int i = 0; i < help_data.arguments.size(); i++) {
  3114. const ArgumentData &argument = help_data.arguments[i];
  3115. if (i > 0) {
  3116. title->add_text(", ");
  3117. }
  3118. title->add_text(argument.name);
  3119. title->push_color(symbol_color);
  3120. title->add_text(colon_nbsp);
  3121. title->pop(); // color
  3122. _add_type_to_title(argument.doc_type);
  3123. if (!argument.default_value.is_empty()) {
  3124. title->push_color(symbol_color);
  3125. title->add_text(nbsp_equal_nbsp);
  3126. title->pop(); // color
  3127. title->push_color(value_color);
  3128. title->add_text(_fix_constant(argument.default_value));
  3129. title->pop(); // color
  3130. }
  3131. }
  3132. if (help_data.qualifiers.contains("vararg")) {
  3133. if (!help_data.arguments.is_empty()) {
  3134. title->add_text(", ");
  3135. }
  3136. title->push_color(symbol_color);
  3137. title->add_text("...");
  3138. title->pop(); // color
  3139. }
  3140. title->push_color(symbol_color);
  3141. title->add_text(")");
  3142. title->pop(); // color
  3143. if (!help_data.doc_type.type.is_empty()) {
  3144. title->push_color(symbol_color);
  3145. title->add_text(" ->" + nbsp);
  3146. title->pop(); // color
  3147. _add_type_to_title(help_data.doc_type);
  3148. }
  3149. if (!help_data.qualifiers.is_empty()) {
  3150. title->push_color(qualifier_color);
  3151. _add_qualifiers_to_rt(help_data.qualifiers, title);
  3152. title->pop(); // color
  3153. }
  3154. title->pop(); // font_size
  3155. title->pop(); // font
  3156. } break;
  3157. }
  3158. title->show();
  3159. } else {
  3160. title->hide();
  3161. }
  3162. content->clear();
  3163. bool has_prev_text = false;
  3164. if (!help_data.deprecated_message.is_empty()) {
  3165. has_prev_text = true;
  3166. Ref<Texture2D> error_icon = get_editor_theme_icon(SNAME("StatusError"));
  3167. content->add_image(error_icon, error_icon->get_width(), error_icon->get_height());
  3168. content->add_text(nbsp);
  3169. content->push_color(get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  3170. content->push_font(doc_bold_font);
  3171. content->add_text(TTR("Deprecated:"));
  3172. content->pop(); // font
  3173. content->pop(); // color
  3174. content->add_text(" ");
  3175. _add_text_to_rt(help_data.deprecated_message, content, this, symbol_class_name);
  3176. }
  3177. if (!help_data.experimental_message.is_empty()) {
  3178. if (has_prev_text) {
  3179. content->add_newline();
  3180. content->add_newline();
  3181. }
  3182. has_prev_text = true;
  3183. Ref<Texture2D> warning_icon = get_editor_theme_icon(SNAME("NodeWarning"));
  3184. content->add_image(warning_icon, warning_icon->get_width(), warning_icon->get_height());
  3185. content->add_text(nbsp);
  3186. content->push_color(get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  3187. content->push_font(doc_bold_font);
  3188. content->add_text(TTR("Experimental:"));
  3189. content->pop(); // font
  3190. content->pop(); // color
  3191. content->add_text(" ");
  3192. _add_text_to_rt(help_data.experimental_message, content, this, symbol_class_name);
  3193. }
  3194. if (!help_data.description.is_empty()) {
  3195. if (has_prev_text) {
  3196. content->add_newline();
  3197. content->add_newline();
  3198. }
  3199. has_prev_text = true;
  3200. const Color comment_color = get_theme_color(SNAME("comment_color"), SNAME("EditorHelp"));
  3201. _add_text_to_rt(help_data.description.replace("<EditorHelpBitCommentColor>", comment_color.to_html()), content, this, symbol_class_name);
  3202. }
  3203. if (!help_data.resource_path.is_empty()) {
  3204. if (has_prev_text) {
  3205. content->add_newline();
  3206. content->add_newline();
  3207. }
  3208. has_prev_text = true;
  3209. const String ext = help_data.resource_path.get_extension();
  3210. const bool is_dir = ext.is_empty();
  3211. const bool is_valid = is_dir || EditorFileSystem::get_singleton()->get_valid_extensions().has(ext);
  3212. if (!is_dir && is_valid) {
  3213. content->push_meta("open-res:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
  3214. content->add_image(get_editor_theme_icon(SNAME("Load")));
  3215. content->add_text(nbsp + TTR("Open"));
  3216. content->pop(); // meta
  3217. content->add_newline();
  3218. }
  3219. if (is_valid) {
  3220. content->push_meta("show:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
  3221. content->add_image(get_editor_theme_icon(SNAME("Filesystem")));
  3222. content->add_text(nbsp + TTR("Show in FileSystem"));
  3223. content->pop(); // meta
  3224. } else {
  3225. content->push_meta("open-file:" + help_data.resource_path, RichTextLabel::META_UNDERLINE_ON_HOVER);
  3226. content->add_image(get_editor_theme_icon(SNAME("Filesystem")));
  3227. content->add_text(nbsp + TTR("Open in File Manager"));
  3228. content->pop(); // meta
  3229. }
  3230. }
  3231. if (is_inside_tree()) {
  3232. update_content_height();
  3233. }
  3234. }
  3235. void EditorHelpBit::_go_to_help(const String &p_what) {
  3236. EditorNode::get_singleton()->get_editor_main_screen()->select(EditorMainScreen::EDITOR_SCRIPT);
  3237. ScriptEditor::get_singleton()->goto_help(p_what);
  3238. emit_signal(SNAME("request_hide"));
  3239. }
  3240. void EditorHelpBit::_meta_clicked(const String &p_select) {
  3241. if (p_select.begins_with("$")) { // Enum.
  3242. const String link = p_select.substr(1);
  3243. String enum_class_name;
  3244. String enum_name;
  3245. if (CoreConstants::is_global_enum(link)) {
  3246. enum_class_name = "@GlobalScope";
  3247. enum_name = link;
  3248. } else {
  3249. const int dot_pos = link.rfind_char('.');
  3250. if (dot_pos >= 0) {
  3251. enum_class_name = link.left(dot_pos);
  3252. enum_name = link.substr(dot_pos + 1);
  3253. } else {
  3254. enum_class_name = symbol_class_name;
  3255. enum_name = link;
  3256. }
  3257. }
  3258. _go_to_help("class_enum:" + enum_class_name + ":" + enum_name);
  3259. } else if (p_select.begins_with("#")) { // Class.
  3260. _go_to_help("class_name:" + p_select.substr(1));
  3261. } else if (p_select.begins_with("@")) { // Member.
  3262. const int tag_end = p_select.find_char(' ');
  3263. const String tag = p_select.substr(1, tag_end - 1);
  3264. const String link = p_select.substr(tag_end + 1).lstrip(" ");
  3265. String topic;
  3266. if (tag == "method") {
  3267. topic = "class_method";
  3268. } else if (tag == "constructor") {
  3269. topic = "class_method";
  3270. } else if (tag == "operator") {
  3271. topic = "class_method";
  3272. } else if (tag == "member") {
  3273. topic = "class_property";
  3274. } else if (tag == "enum") {
  3275. topic = "class_enum";
  3276. } else if (tag == "signal") {
  3277. topic = "class_signal";
  3278. } else if (tag == "constant") {
  3279. topic = "class_constant";
  3280. } else if (tag == "annotation") {
  3281. topic = "class_annotation";
  3282. } else if (tag == "theme_item") {
  3283. topic = "class_theme_item";
  3284. } else {
  3285. return;
  3286. }
  3287. if (topic == "class_enum") {
  3288. const String enum_link = link.trim_prefix("@GlobalScope.");
  3289. if (CoreConstants::is_global_enum(enum_link)) {
  3290. _go_to_help(topic + ":@GlobalScope:" + enum_link);
  3291. return;
  3292. }
  3293. } else if (topic == "class_constant") {
  3294. if (CoreConstants::is_global_constant(link)) {
  3295. _go_to_help(topic + ":@GlobalScope:" + link);
  3296. return;
  3297. }
  3298. }
  3299. if (link.contains_char('.')) {
  3300. const int class_end = link.rfind_char('.');
  3301. _go_to_help(topic + ":" + link.left(class_end) + ":" + link.substr(class_end + 1));
  3302. } else {
  3303. _go_to_help(topic + ":" + symbol_class_name + ":" + link);
  3304. }
  3305. } else if (p_select.begins_with("open-file:")) {
  3306. String path = ProjectSettings::get_singleton()->globalize_path(p_select.trim_prefix("open-file:"));
  3307. OS::get_singleton()->shell_show_in_file_manager(path, true);
  3308. } else if (p_select.begins_with("open-res:")) {
  3309. if (help_data.doc_type.type == "PackedScene") {
  3310. EditorNode::get_singleton()->load_scene(p_select.trim_prefix("open-res:"));
  3311. } else {
  3312. EditorNode::get_singleton()->load_resource(p_select.trim_prefix("open-res:"));
  3313. }
  3314. } else if (p_select.begins_with("show:")) {
  3315. FileSystemDock::get_singleton()->navigate_to_path(p_select.trim_prefix("show:"));
  3316. } else if (p_select.begins_with("http:") || p_select.begins_with("https:")) {
  3317. OS::get_singleton()->shell_open(p_select);
  3318. } else if (p_select.begins_with("^")) { // Copy button.
  3319. DisplayServer::get_singleton()->clipboard_set(p_select.substr(1));
  3320. }
  3321. }
  3322. void EditorHelpBit::_bind_methods() {
  3323. ADD_SIGNAL(MethodInfo("request_hide"));
  3324. }
  3325. void EditorHelpBit::_notification(int p_what) {
  3326. switch (p_what) {
  3327. case NOTIFICATION_THEME_CHANGED:
  3328. _update_labels();
  3329. break;
  3330. }
  3331. }
  3332. void EditorHelpBit::parse_symbol(const String &p_symbol, const String &p_prologue) {
  3333. const PackedStringArray slices = p_symbol.split("|", true, 3);
  3334. ERR_FAIL_COND_MSG(slices.size() < 3, R"(Invalid doc id: The expected format is "item_type|class_name|item_name[|item_data]".)");
  3335. const String &item_type = slices[0];
  3336. const String &class_name = slices[1];
  3337. const String &item_name = slices[2];
  3338. Dictionary item_data;
  3339. if (slices.size() > 3) {
  3340. item_data = JSON::parse_string(slices[3]);
  3341. }
  3342. symbol_doc_link = String();
  3343. symbol_class_name = class_name;
  3344. symbol_type = String();
  3345. symbol_name = item_name;
  3346. symbol_hint = SYMBOL_HINT_NONE;
  3347. help_data = HelpData();
  3348. if (item_type == "class") {
  3349. symbol_doc_link = vformat("#%s", class_name);
  3350. symbol_type = TTR("Class");
  3351. symbol_name = class_name;
  3352. symbol_hint = SYMBOL_HINT_INHERITANCE;
  3353. help_data = _get_class_help_data(class_name);
  3354. } else if (item_type == "enum") {
  3355. symbol_doc_link = vformat("$%s.%s", class_name, item_name);
  3356. symbol_type = TTR("Enumeration");
  3357. help_data = _get_enum_help_data(class_name, item_name);
  3358. } else if (item_type == "constant") {
  3359. symbol_doc_link = vformat("@constant %s.%s", class_name, item_name);
  3360. symbol_type = TTR("Constant");
  3361. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3362. help_data = _get_constant_help_data(class_name, item_name);
  3363. } else if (item_type == "property") {
  3364. if (item_name.begins_with("metadata/")) {
  3365. symbol_type = TTR("Metadata");
  3366. symbol_name = item_name.trim_prefix("metadata/");
  3367. } else if (class_name == "ProjectSettings" || class_name == "EditorSettings") {
  3368. symbol_doc_link = vformat("@member %s.%s", class_name, item_name);
  3369. symbol_type = TTR("Setting");
  3370. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3371. } else {
  3372. symbol_doc_link = vformat("@member %s.%s", class_name, item_name);
  3373. symbol_type = TTR("Property");
  3374. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3375. }
  3376. help_data = _get_property_help_data(class_name, item_name);
  3377. } else if (item_type == "internal_property") {
  3378. symbol_type = TTR("Internal Property");
  3379. help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This property can only be set in the Inspector.") + "[/i][/color]";
  3380. } else if (item_type == "theme_item") {
  3381. symbol_doc_link = vformat("@theme_item %s.%s", class_name, item_name);
  3382. symbol_type = TTR("Theme Property");
  3383. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3384. help_data = _get_theme_item_help_data(class_name, item_name);
  3385. } else if (item_type == "method") {
  3386. symbol_doc_link = vformat("@method %s.%s", class_name, item_name);
  3387. symbol_type = TTR("Method");
  3388. symbol_hint = SYMBOL_HINT_SIGNATURE;
  3389. help_data = _get_method_help_data(class_name, item_name);
  3390. } else if (item_type == "signal") {
  3391. symbol_doc_link = vformat("@signal %s.%s", class_name, item_name);
  3392. symbol_type = TTR("Signal");
  3393. symbol_hint = SYMBOL_HINT_SIGNATURE;
  3394. help_data = _get_signal_help_data(class_name, item_name);
  3395. } else if (item_type == "annotation") {
  3396. symbol_doc_link = vformat("@annotation %s.%s", class_name, item_name);
  3397. symbol_type = TTR("Annotation");
  3398. symbol_hint = SYMBOL_HINT_SIGNATURE;
  3399. help_data = _get_annotation_help_data(class_name, item_name);
  3400. } else if (item_type == "local_constant" || item_type == "local_variable") {
  3401. symbol_type = (item_type == "local_constant") ? TTR("Local Constant") : TTR("Local Variable");
  3402. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3403. help_data.description = item_data.get("description", "").operator String().strip_edges();
  3404. if (item_data.get("is_deprecated", false)) {
  3405. const String deprecated_message = item_data.get("deprecated_message", "").operator String().strip_edges();
  3406. if (deprecated_message.is_empty()) {
  3407. if (item_type == "local_constant") {
  3408. help_data.deprecated_message = TTR("This constant may be changed or removed in future versions.");
  3409. } else {
  3410. help_data.deprecated_message = TTR("This variable may be changed or removed in future versions.");
  3411. }
  3412. } else {
  3413. help_data.deprecated_message = deprecated_message;
  3414. }
  3415. }
  3416. if (item_data.get("is_experimental", false)) {
  3417. const String experimental_message = item_data.get("experimental_message", "").operator String().strip_edges();
  3418. if (experimental_message.is_empty()) {
  3419. if (item_type == "local_constant") {
  3420. help_data.experimental_message = TTR("This constant may be changed or removed in future versions.");
  3421. } else {
  3422. help_data.experimental_message = TTR("This variable may be changed or removed in future versions.");
  3423. }
  3424. } else {
  3425. help_data.experimental_message = experimental_message;
  3426. }
  3427. }
  3428. help_data.doc_type.type = item_data.get("doc_type", "");
  3429. help_data.doc_type.enumeration = item_data.get("enumeration", "");
  3430. help_data.doc_type.is_bitfield = item_data.get("is_bitfield", false);
  3431. help_data.value = item_data.get("value", "");
  3432. } else if (item_type == "resource") {
  3433. String path = item_name.simplify_path();
  3434. const bool is_uid = path.begins_with("uid://");
  3435. if (is_uid) {
  3436. if (ResourceUID::get_singleton()->has_id(ResourceUID::get_singleton()->text_to_id(path))) {
  3437. path = ResourceUID::uid_to_path(path);
  3438. } else {
  3439. path = "";
  3440. }
  3441. }
  3442. help_data.resource_path = path;
  3443. Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
  3444. if (da->file_exists(path)) {
  3445. help_data.doc_type.type = ResourceLoader::get_resource_type(path);
  3446. if (help_data.doc_type.type.is_empty()) {
  3447. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  3448. symbol_type = textfile_ext.has(path.get_extension()) ? TTR("TextFile") : TTR("File");
  3449. } else {
  3450. symbol_type = TTR("Resource");
  3451. symbol_hint = SYMBOL_HINT_ASSIGNABLE;
  3452. if (is_uid) {
  3453. help_data.description = vformat("%s: [color=<EditorHelpBitCommentColor>]%s[/color]", TTR("Path"), path);
  3454. }
  3455. }
  3456. symbol_name = path.get_file();
  3457. } else if (!is_uid && da->dir_exists(path)) {
  3458. symbol_type = TTR("Directory");
  3459. symbol_name = path;
  3460. } else {
  3461. help_data.resource_path = "";
  3462. symbol_name = "";
  3463. if (is_uid) {
  3464. symbol_type = TTR("Invalid UID");
  3465. help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This UID does not point to any valid Resource.") + "[/i][/color]";
  3466. } else {
  3467. symbol_type = TTR("Invalid path");
  3468. help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("This path does not exist.") + "[/i][/color]";
  3469. }
  3470. }
  3471. } else {
  3472. ERR_FAIL_MSG("Invalid doc id: Unknown item type " + item_type.quote() + ".");
  3473. }
  3474. // Do not add links for custom or undocumented symbols.
  3475. if (symbol_class_name.is_empty() || (help_data.description.is_empty() && help_data.deprecated_message.is_empty() && help_data.experimental_message.is_empty())) {
  3476. symbol_doc_link = String();
  3477. }
  3478. if (!p_prologue.is_empty()) {
  3479. if (help_data.description.is_empty()) {
  3480. help_data.description = p_prologue;
  3481. } else {
  3482. help_data.description = p_prologue + "\n" + help_data.description;
  3483. }
  3484. }
  3485. if (help_data.description.is_empty() && item_type != "resource") {
  3486. help_data.description = "[color=<EditorHelpBitCommentColor>][i]" + TTR("No description available.") + "[/i][/color]";
  3487. }
  3488. if (is_inside_tree()) {
  3489. _update_labels();
  3490. }
  3491. }
  3492. void EditorHelpBit::set_custom_text(const String &p_type, const String &p_name, const String &p_description) {
  3493. symbol_doc_link = String();
  3494. symbol_class_name = String();
  3495. symbol_type = p_type;
  3496. symbol_name = p_name;
  3497. symbol_hint = SYMBOL_HINT_NONE;
  3498. help_data = HelpData();
  3499. help_data.description = p_description;
  3500. if (is_inside_tree()) {
  3501. _update_labels();
  3502. }
  3503. }
  3504. void EditorHelpBit::set_content_height_limits(float p_min, float p_max) {
  3505. ERR_FAIL_COND(p_min > p_max);
  3506. content_min_height = p_min;
  3507. content_max_height = p_max;
  3508. if (is_inside_tree()) {
  3509. update_content_height();
  3510. }
  3511. }
  3512. void EditorHelpBit::update_content_height() {
  3513. float content_height = content->get_content_height();
  3514. const Ref<StyleBox> style = content->get_theme_stylebox(CoreStringName(normal));
  3515. if (style.is_valid()) {
  3516. content_height += style->get_content_margin(SIDE_TOP) + style->get_content_margin(SIDE_BOTTOM);
  3517. }
  3518. content->set_custom_minimum_size(Size2(content->get_custom_minimum_size().x, CLAMP(content_height, content_min_height, content_max_height)));
  3519. }
  3520. EditorHelpBit::EditorHelpBit(const String &p_symbol, const String &p_prologue, bool p_use_class_prefix, bool p_allow_selection) {
  3521. add_theme_constant_override("separation", 0);
  3522. title = memnew(RichTextLabel);
  3523. title->set_theme_type_variation("EditorHelpBitTitle");
  3524. title->set_custom_minimum_size(Size2(640 * EDSCALE, 0)); // GH-93031. Set the minimum width even if `fit_content` is true.
  3525. title->set_fit_content(true);
  3526. title->set_selection_enabled(p_allow_selection);
  3527. title->set_context_menu_enabled(p_allow_selection);
  3528. title->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
  3529. title->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  3530. title->hide();
  3531. add_child(title);
  3532. content_min_height = 48 * EDSCALE;
  3533. content_max_height = 360 * EDSCALE;
  3534. content = memnew(RichTextLabel);
  3535. content->set_theme_type_variation("EditorHelpBitContent");
  3536. content->set_custom_minimum_size(Size2(640 * EDSCALE, content_min_height));
  3537. content->set_selection_enabled(p_allow_selection);
  3538. content->set_context_menu_enabled(p_allow_selection);
  3539. content->set_selection_modifier(callable_mp_static(_replace_nbsp_with_space));
  3540. content->connect("meta_clicked", callable_mp(this, &EditorHelpBit::_meta_clicked));
  3541. add_child(content);
  3542. use_class_prefix = p_use_class_prefix;
  3543. if (!p_symbol.is_empty()) {
  3544. parse_symbol(p_symbol, p_prologue);
  3545. } else if (!p_prologue.is_empty()) {
  3546. set_custom_text(String(), String(), p_prologue);
  3547. }
  3548. }
  3549. /// EditorHelpBitTooltip ///
  3550. bool EditorHelpBitTooltip::_is_tooltip_visible = false;
  3551. Control *EditorHelpBitTooltip::_make_invisible_control() {
  3552. Control *control = memnew(Control);
  3553. control->set_visible(false);
  3554. return control;
  3555. }
  3556. void EditorHelpBitTooltip::_start_timer() {
  3557. if (timer->is_inside_tree() && timer->is_stopped()) {
  3558. timer->start();
  3559. }
  3560. }
  3561. void EditorHelpBitTooltip::_target_gui_input(const Ref<InputEvent> &p_event) {
  3562. // Only scrolling is not checked in `NOTIFICATION_INTERNAL_PROCESS`.
  3563. const Ref<InputEventMouseButton> mb = p_event;
  3564. if (mb.is_valid()) {
  3565. switch (mb->get_button_index()) {
  3566. case MouseButton::WHEEL_UP:
  3567. case MouseButton::WHEEL_DOWN:
  3568. case MouseButton::WHEEL_LEFT:
  3569. case MouseButton::WHEEL_RIGHT:
  3570. queue_free();
  3571. break;
  3572. default:
  3573. break;
  3574. }
  3575. }
  3576. }
  3577. void EditorHelpBitTooltip::_notification(int p_what) {
  3578. switch (p_what) {
  3579. case NOTIFICATION_ENTER_TREE:
  3580. _is_tooltip_visible = true;
  3581. _enter_tree_time = OS::get_singleton()->get_ticks_msec();
  3582. break;
  3583. case NOTIFICATION_EXIT_TREE:
  3584. _is_tooltip_visible = false;
  3585. break;
  3586. case NOTIFICATION_WM_MOUSE_ENTER:
  3587. _is_mouse_inside_tooltip = true;
  3588. timer->stop();
  3589. break;
  3590. case NOTIFICATION_WM_MOUSE_EXIT:
  3591. _is_mouse_inside_tooltip = false;
  3592. _start_timer();
  3593. break;
  3594. case NOTIFICATION_INTERNAL_PROCESS:
  3595. // A workaround to hide the tooltip since the window does not receive keyboard events
  3596. // with `FLAG_POPUP` and `FLAG_NO_FOCUS` flags, so we can't use `_input_from_window()`.
  3597. if (is_inside_tree()) {
  3598. if (Input::get_singleton()->is_action_just_pressed(SNAME("ui_cancel"), true)) {
  3599. queue_free();
  3600. get_parent_viewport()->set_input_as_handled();
  3601. } else if (Input::get_singleton()->is_anything_pressed_except_mouse()) {
  3602. queue_free();
  3603. } else if (!Input::get_singleton()->get_mouse_button_mask().is_empty()) {
  3604. if (!_is_mouse_inside_tooltip) {
  3605. queue_free();
  3606. }
  3607. } else if (!Input::get_singleton()->get_last_mouse_velocity().is_zero_approx()) {
  3608. if (!_is_mouse_inside_tooltip && OS::get_singleton()->get_ticks_msec() - _enter_tree_time > 250) {
  3609. _start_timer();
  3610. }
  3611. }
  3612. }
  3613. break;
  3614. }
  3615. }
  3616. Control *EditorHelpBitTooltip::show_tooltip(Control *p_target, const String &p_symbol, const String &p_prologue, bool p_use_class_prefix) {
  3617. ERR_FAIL_NULL_V(p_target, _make_invisible_control());
  3618. // Show the custom tooltip only if it is not already visible.
  3619. // The viewport will retrigger `make_custom_tooltip()` every few seconds
  3620. // because the return control is not visible even if the custom tooltip is displayed.
  3621. if (_is_tooltip_visible || Input::get_singleton()->is_anything_pressed()) {
  3622. return _make_invisible_control();
  3623. }
  3624. EditorHelpBit *help_bit = memnew(EditorHelpBit(p_symbol, p_prologue, p_use_class_prefix, false));
  3625. EditorHelpBitTooltip *tooltip = memnew(EditorHelpBitTooltip(p_target));
  3626. help_bit->connect("request_hide", callable_mp(static_cast<Node *>(tooltip), &Node::queue_free));
  3627. tooltip->add_child(help_bit);
  3628. p_target->add_child(tooltip);
  3629. help_bit->update_content_height();
  3630. tooltip->popup_under_cursor();
  3631. return _make_invisible_control();
  3632. }
  3633. // Copy-paste from `Viewport::_gui_show_tooltip()`.
  3634. void EditorHelpBitTooltip::popup_under_cursor() {
  3635. Point2 mouse_pos = get_mouse_position();
  3636. Point2 tooltip_offset = GLOBAL_GET("display/mouse_cursor/tooltip_position_offset");
  3637. Rect2 r(mouse_pos + tooltip_offset, get_contents_minimum_size());
  3638. r.size = r.size.min(get_max_size());
  3639. Window *window = get_parent_visible_window();
  3640. Rect2i vr;
  3641. if (is_embedded()) {
  3642. vr = get_embedder()->get_visible_rect();
  3643. } else {
  3644. vr = window->get_usable_parent_rect();
  3645. }
  3646. if (r.size.x + r.position.x > vr.size.x + vr.position.x) {
  3647. // Place it in the opposite direction. If it fails, just hug the border.
  3648. r.position.x = mouse_pos.x - r.size.x - tooltip_offset.x;
  3649. if (r.position.x < vr.position.x) {
  3650. r.position.x = vr.position.x + vr.size.x - r.size.x;
  3651. }
  3652. } else if (r.position.x < vr.position.x) {
  3653. r.position.x = vr.position.x;
  3654. }
  3655. if (r.size.y + r.position.y > vr.size.y + vr.position.y) {
  3656. // Same as above.
  3657. r.position.y = mouse_pos.y - r.size.y - tooltip_offset.y;
  3658. if (r.position.y < vr.position.y) {
  3659. r.position.y = vr.position.y + vr.size.y - r.size.y;
  3660. }
  3661. } else if (r.position.y < vr.position.y) {
  3662. r.position.y = vr.position.y;
  3663. }
  3664. // When `FLAG_POPUP` is false, it prevents the editor from losing focus when displaying the tooltip.
  3665. // This way, clicks and double-clicks are still available outside the tooltip.
  3666. set_flag(Window::FLAG_POPUP, false);
  3667. set_flag(Window::FLAG_NO_FOCUS, true);
  3668. popup(r);
  3669. }
  3670. EditorHelpBitTooltip::EditorHelpBitTooltip(Control *p_target) {
  3671. ERR_FAIL_NULL(p_target);
  3672. set_theme_type_variation("TooltipPanel");
  3673. timer = memnew(Timer);
  3674. timer->set_wait_time(0.25);
  3675. timer->connect("timeout", callable_mp(static_cast<Node *>(this), &Node::queue_free));
  3676. add_child(timer);
  3677. p_target->connect(SceneStringName(mouse_exited), callable_mp(this, &EditorHelpBitTooltip::_start_timer));
  3678. p_target->connect(SceneStringName(gui_input), callable_mp(this, &EditorHelpBitTooltip::_target_gui_input));
  3679. set_process_internal(true);
  3680. }
  3681. #if defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  3682. /// EditorHelpHighlighter ///
  3683. EditorHelpHighlighter *EditorHelpHighlighter::singleton = nullptr;
  3684. void EditorHelpHighlighter::create_singleton() {
  3685. ERR_FAIL_COND(singleton != nullptr);
  3686. singleton = memnew(EditorHelpHighlighter);
  3687. }
  3688. void EditorHelpHighlighter::free_singleton() {
  3689. ERR_FAIL_NULL(singleton);
  3690. memdelete(singleton);
  3691. singleton = nullptr;
  3692. }
  3693. EditorHelpHighlighter *EditorHelpHighlighter::get_singleton() {
  3694. return singleton;
  3695. }
  3696. EditorHelpHighlighter::HighlightData EditorHelpHighlighter::_get_highlight_data(Language p_language, const String &p_source, bool p_use_cache) {
  3697. switch (p_language) {
  3698. case LANGUAGE_GDSCRIPT:
  3699. #ifndef MODULE_GDSCRIPT_ENABLED
  3700. ERR_FAIL_V_MSG(HighlightData(), "GDScript module is disabled.");
  3701. #endif
  3702. break;
  3703. case LANGUAGE_CSHARP:
  3704. #ifndef MODULE_MONO_ENABLED
  3705. ERR_FAIL_V_MSG(HighlightData(), "Mono module is disabled.");
  3706. #endif
  3707. break;
  3708. default:
  3709. ERR_FAIL_V_MSG(HighlightData(), "Invalid parameter \"p_language\".");
  3710. }
  3711. if (p_use_cache) {
  3712. const HashMap<String, HighlightData>::ConstIterator E = highlight_data_caches[p_language].find(p_source);
  3713. if (E) {
  3714. return E->value;
  3715. }
  3716. }
  3717. text_edits[p_language]->set_text(p_source);
  3718. if (scripts[p_language].is_valid()) { // See GH-89610.
  3719. scripts[p_language]->set_source_code(p_source);
  3720. }
  3721. highlighters[p_language]->_update_cache();
  3722. HighlightData result;
  3723. int source_offset = 0;
  3724. int result_index = 0;
  3725. for (int i = 0; i < text_edits[p_language]->get_line_count(); i++) {
  3726. const Dictionary dict = highlighters[p_language]->_get_line_syntax_highlighting_impl(i);
  3727. result.resize(result.size() + dict.size());
  3728. const Variant *key = nullptr;
  3729. int prev_column = -1;
  3730. while ((key = dict.next(key)) != nullptr) {
  3731. const int column = *key;
  3732. ERR_FAIL_COND_V(column <= prev_column, HighlightData());
  3733. prev_column = column;
  3734. const Color color = dict[*key].operator Dictionary().get("color", Color());
  3735. result.write[result_index] = { source_offset + column, color };
  3736. result_index++;
  3737. }
  3738. source_offset += text_edits[p_language]->get_line(i).length() + 1; // Plus newline.
  3739. }
  3740. if (p_use_cache) {
  3741. highlight_data_caches[p_language][p_source] = result;
  3742. }
  3743. return result;
  3744. }
  3745. void EditorHelpHighlighter::highlight(RichTextLabel *p_rich_text_label, Language p_language, const String &p_source, bool p_use_cache) {
  3746. ERR_FAIL_NULL(p_rich_text_label);
  3747. const HighlightData highlight_data = _get_highlight_data(p_language, p_source, p_use_cache);
  3748. if (!highlight_data.is_empty()) {
  3749. for (int i = 1; i < highlight_data.size(); i++) {
  3750. const Pair<int, Color> &prev = highlight_data[i - 1];
  3751. const Pair<int, Color> &curr = highlight_data[i];
  3752. p_rich_text_label->push_color(prev.second);
  3753. p_rich_text_label->add_text(p_source.substr(prev.first, curr.first - prev.first));
  3754. p_rich_text_label->pop(); // color
  3755. }
  3756. const Pair<int, Color> &last = highlight_data[highlight_data.size() - 1];
  3757. p_rich_text_label->push_color(last.second);
  3758. p_rich_text_label->add_text(p_source.substr(last.first));
  3759. p_rich_text_label->pop(); // color
  3760. }
  3761. }
  3762. void EditorHelpHighlighter::reset_cache() {
  3763. const Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");
  3764. #ifdef MODULE_GDSCRIPT_ENABLED
  3765. highlight_data_caches[LANGUAGE_GDSCRIPT].clear();
  3766. text_edits[LANGUAGE_GDSCRIPT]->add_theme_color_override(SceneStringName(font_color), text_color);
  3767. #endif
  3768. #ifdef MODULE_MONO_ENABLED
  3769. highlight_data_caches[LANGUAGE_CSHARP].clear();
  3770. text_edits[LANGUAGE_CSHARP]->add_theme_color_override(SceneStringName(font_color), text_color);
  3771. #endif
  3772. }
  3773. EditorHelpHighlighter::EditorHelpHighlighter() {
  3774. const Color text_color = EDITOR_GET("text_editor/theme/highlighting/text_color");
  3775. #ifdef MODULE_GDSCRIPT_ENABLED
  3776. TextEdit *gdscript_text_edit = memnew(TextEdit);
  3777. gdscript_text_edit->add_theme_color_override(SceneStringName(font_color), text_color);
  3778. Ref<GDScript> gdscript;
  3779. gdscript.instantiate();
  3780. Ref<GDScriptSyntaxHighlighter> gdscript_highlighter;
  3781. gdscript_highlighter.instantiate();
  3782. gdscript_highlighter->set_text_edit(gdscript_text_edit);
  3783. gdscript_highlighter->_set_edited_resource(gdscript);
  3784. text_edits[LANGUAGE_GDSCRIPT] = gdscript_text_edit;
  3785. scripts[LANGUAGE_GDSCRIPT] = gdscript;
  3786. highlighters[LANGUAGE_GDSCRIPT] = gdscript_highlighter;
  3787. #endif
  3788. #ifdef MODULE_MONO_ENABLED
  3789. TextEdit *csharp_text_edit = memnew(TextEdit);
  3790. csharp_text_edit->add_theme_color_override(SceneStringName(font_color), text_color);
  3791. // See GH-89610.
  3792. //Ref<CSharpScript> csharp;
  3793. //csharp.instantiate();
  3794. Ref<EditorStandardSyntaxHighlighter> csharp_highlighter;
  3795. csharp_highlighter.instantiate();
  3796. csharp_highlighter->set_text_edit(csharp_text_edit);
  3797. //csharp_highlighter->_set_edited_resource(csharp);
  3798. csharp_highlighter->_set_script_language(CSharpLanguage::get_singleton());
  3799. text_edits[LANGUAGE_CSHARP] = csharp_text_edit;
  3800. //scripts[LANGUAGE_CSHARP] = csharp;
  3801. highlighters[LANGUAGE_CSHARP] = csharp_highlighter;
  3802. #endif
  3803. }
  3804. EditorHelpHighlighter::~EditorHelpHighlighter() {
  3805. #ifdef MODULE_GDSCRIPT_ENABLED
  3806. memdelete(text_edits[LANGUAGE_GDSCRIPT]);
  3807. #endif
  3808. #ifdef MODULE_MONO_ENABLED
  3809. memdelete(text_edits[LANGUAGE_CSHARP]);
  3810. #endif
  3811. }
  3812. #endif // defined(MODULE_GDSCRIPT_ENABLED) || defined(MODULE_MONO_ENABLED)
  3813. /// FindBar ///
  3814. FindBar::FindBar() {
  3815. search_text = memnew(LineEdit);
  3816. add_child(search_text);
  3817. search_text->set_keep_editing_on_text_submit(true);
  3818. search_text->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
  3819. search_text->set_h_size_flags(SIZE_EXPAND_FILL);
  3820. search_text->connect(SceneStringName(text_changed), callable_mp(this, &FindBar::_search_text_changed));
  3821. search_text->connect(SceneStringName(text_submitted), callable_mp(this, &FindBar::_search_text_submitted));
  3822. matches_label = memnew(Label);
  3823. add_child(matches_label);
  3824. matches_label->hide();
  3825. find_prev = memnew(Button);
  3826. find_prev->set_flat(true);
  3827. add_child(find_prev);
  3828. find_prev->set_focus_mode(FOCUS_NONE);
  3829. find_prev->connect(SceneStringName(pressed), callable_mp(this, &FindBar::search_prev));
  3830. find_next = memnew(Button);
  3831. find_next->set_flat(true);
  3832. add_child(find_next);
  3833. find_next->set_focus_mode(FOCUS_NONE);
  3834. find_next->connect(SceneStringName(pressed), callable_mp(this, &FindBar::search_next));
  3835. Control *space = memnew(Control);
  3836. add_child(space);
  3837. space->set_custom_minimum_size(Size2(4, 0) * EDSCALE);
  3838. hide_button = memnew(TextureButton);
  3839. add_child(hide_button);
  3840. hide_button->set_focus_mode(FOCUS_NONE);
  3841. hide_button->set_ignore_texture_size(true);
  3842. hide_button->set_stretch_mode(TextureButton::STRETCH_KEEP_CENTERED);
  3843. hide_button->connect(SceneStringName(pressed), callable_mp(this, &FindBar::_hide_bar));
  3844. }
  3845. void FindBar::popup_search() {
  3846. show();
  3847. bool grabbed_focus = false;
  3848. if (!search_text->has_focus()) {
  3849. search_text->grab_focus();
  3850. grabbed_focus = true;
  3851. }
  3852. if (!search_text->get_text().is_empty()) {
  3853. search_text->select_all();
  3854. search_text->set_caret_column(search_text->get_text().length());
  3855. if (grabbed_focus) {
  3856. _search();
  3857. }
  3858. }
  3859. }
  3860. void FindBar::_notification(int p_what) {
  3861. switch (p_what) {
  3862. case NOTIFICATION_THEME_CHANGED: {
  3863. find_prev->set_button_icon(get_editor_theme_icon(SNAME("MoveUp")));
  3864. find_next->set_button_icon(get_editor_theme_icon(SNAME("MoveDown")));
  3865. hide_button->set_texture_normal(get_editor_theme_icon(SNAME("Close")));
  3866. hide_button->set_texture_hover(get_editor_theme_icon(SNAME("Close")));
  3867. hide_button->set_texture_pressed(get_editor_theme_icon(SNAME("Close")));
  3868. hide_button->set_custom_minimum_size(hide_button->get_texture_normal()->get_size());
  3869. 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)));
  3870. } break;
  3871. case NOTIFICATION_VISIBILITY_CHANGED: {
  3872. set_process_input(is_visible_in_tree());
  3873. } break;
  3874. }
  3875. }
  3876. void FindBar::set_rich_text_label(RichTextLabel *p_rich_text_label) {
  3877. rich_text_label = p_rich_text_label;
  3878. }
  3879. bool FindBar::search_next() {
  3880. return _search();
  3881. }
  3882. bool FindBar::search_prev() {
  3883. return _search(true);
  3884. }
  3885. bool FindBar::_search(bool p_search_previous) {
  3886. String stext = search_text->get_text();
  3887. bool keep = prev_search == stext;
  3888. bool ret = rich_text_label->search(stext, keep, p_search_previous);
  3889. prev_search = stext;
  3890. if (ret) {
  3891. _update_results_count();
  3892. } else {
  3893. results_count = 0;
  3894. }
  3895. _update_matches_label();
  3896. return ret;
  3897. }
  3898. void FindBar::_update_results_count() {
  3899. results_count = 0;
  3900. String searched = search_text->get_text();
  3901. if (searched.is_empty()) {
  3902. return;
  3903. }
  3904. String full_text = rich_text_label->get_parsed_text();
  3905. int from_pos = 0;
  3906. while (true) {
  3907. int pos = full_text.findn(searched, from_pos);
  3908. if (pos == -1) {
  3909. break;
  3910. }
  3911. results_count++;
  3912. from_pos = pos + searched.length();
  3913. }
  3914. }
  3915. void FindBar::_update_matches_label() {
  3916. if (search_text->get_text().is_empty() || results_count == -1) {
  3917. matches_label->hide();
  3918. } else {
  3919. matches_label->show();
  3920. 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)));
  3921. matches_label->set_text(vformat(results_count == 1 ? TTR("%d match.") : TTR("%d matches."), results_count));
  3922. }
  3923. }
  3924. void FindBar::_hide_bar() {
  3925. if (search_text->has_focus()) {
  3926. rich_text_label->grab_focus();
  3927. }
  3928. hide();
  3929. }
  3930. // Implemented in input(..) as the LineEdit consumes the Escape pressed key.
  3931. void FindBar::input(const Ref<InputEvent> &p_event) {
  3932. ERR_FAIL_COND(p_event.is_null());
  3933. Ref<InputEventKey> k = p_event;
  3934. if (k.is_valid() && k->is_action_pressed(SNAME("ui_cancel"), false, true)) {
  3935. Control *focus_owner = get_viewport()->gui_get_focus_owner();
  3936. if (rich_text_label->has_focus() || (focus_owner && is_ancestor_of(focus_owner))) {
  3937. _hide_bar();
  3938. accept_event();
  3939. }
  3940. }
  3941. }
  3942. void FindBar::_search_text_changed(const String &p_text) {
  3943. search_next();
  3944. }
  3945. void FindBar::_search_text_submitted(const String &p_text) {
  3946. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  3947. search_prev();
  3948. } else {
  3949. search_next();
  3950. }
  3951. }