editor_help.cpp 136 KB

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