editor_help.cpp 165 KB

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