editor_help.cpp 160 KB

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