editor_help.cpp 161 KB

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