editor_help.cpp 165 KB

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