editor_help.cpp 150 KB

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