script_editor_plugin.cpp 144 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407
  1. /**************************************************************************/
  2. /* script_editor_plugin.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 "script_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/json.h"
  35. #include "core/io/resource_loader.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/os/os.h"
  38. #include "core/version.h"
  39. #include "editor/debugger/editor_debugger_node.h"
  40. #include "editor/debugger/script_editor_debugger.h"
  41. #include "editor/editor_command_palette.h"
  42. #include "editor/editor_help_search.h"
  43. #include "editor/editor_interface.h"
  44. #include "editor/editor_node.h"
  45. #include "editor/editor_paths.h"
  46. #include "editor/editor_script.h"
  47. #include "editor/editor_settings.h"
  48. #include "editor/editor_string_names.h"
  49. #include "editor/filesystem_dock.h"
  50. #include "editor/find_in_files.h"
  51. #include "editor/gui/editor_bottom_panel.h"
  52. #include "editor/gui/editor_file_dialog.h"
  53. #include "editor/gui/editor_run_bar.h"
  54. #include "editor/gui/editor_toaster.h"
  55. #include "editor/inspector_dock.h"
  56. #include "editor/node_dock.h"
  57. #include "editor/plugins/shader_editor_plugin.h"
  58. #include "editor/plugins/text_shader_editor.h"
  59. #include "editor/themes/editor_scale.h"
  60. #include "editor/window_wrapper.h"
  61. #include "scene/main/node.h"
  62. #include "scene/main/window.h"
  63. #include "scene/scene_string_names.h"
  64. #include "script_text_editor.h"
  65. #include "servers/display_server.h"
  66. #include "text_editor.h"
  67. /*** SYNTAX HIGHLIGHTER ****/
  68. String EditorSyntaxHighlighter::_get_name() const {
  69. String ret = "Unnamed";
  70. GDVIRTUAL_CALL(_get_name, ret);
  71. return ret;
  72. }
  73. PackedStringArray EditorSyntaxHighlighter::_get_supported_languages() const {
  74. PackedStringArray ret;
  75. GDVIRTUAL_CALL(_get_supported_languages, ret);
  76. return ret;
  77. }
  78. Ref<EditorSyntaxHighlighter> EditorSyntaxHighlighter::_create() const {
  79. Ref<EditorSyntaxHighlighter> syntax_highlighter;
  80. syntax_highlighter.instantiate();
  81. if (get_script_instance()) {
  82. syntax_highlighter->set_script(get_script_instance()->get_script());
  83. }
  84. return syntax_highlighter;
  85. }
  86. void EditorSyntaxHighlighter::_bind_methods() {
  87. ClassDB::bind_method(D_METHOD("_get_edited_resource"), &EditorSyntaxHighlighter::_get_edited_resource);
  88. GDVIRTUAL_BIND(_get_name)
  89. GDVIRTUAL_BIND(_get_supported_languages)
  90. }
  91. ////
  92. void EditorStandardSyntaxHighlighter::_update_cache() {
  93. highlighter->set_text_edit(text_edit);
  94. highlighter->clear_keyword_colors();
  95. highlighter->clear_member_keyword_colors();
  96. highlighter->clear_color_regions();
  97. highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
  98. highlighter->set_function_color(EDITOR_GET("text_editor/theme/highlighting/function_color"));
  99. highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
  100. highlighter->set_member_variable_color(EDITOR_GET("text_editor/theme/highlighting/member_variable_color"));
  101. /* Engine types. */
  102. const Color type_color = EDITOR_GET("text_editor/theme/highlighting/engine_type_color");
  103. List<StringName> types;
  104. ClassDB::get_class_list(&types);
  105. for (const StringName &E : types) {
  106. highlighter->add_keyword_color(E, type_color);
  107. }
  108. /* User types. */
  109. const Color usertype_color = EDITOR_GET("text_editor/theme/highlighting/user_type_color");
  110. List<StringName> global_classes;
  111. ScriptServer::get_global_class_list(&global_classes);
  112. for (const StringName &E : global_classes) {
  113. highlighter->add_keyword_color(E, usertype_color);
  114. }
  115. /* Autoloads. */
  116. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  117. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  118. const ProjectSettings::AutoloadInfo &info = E.value;
  119. if (info.is_singleton) {
  120. highlighter->add_keyword_color(info.name, usertype_color);
  121. }
  122. }
  123. const Ref<Script> scr = _get_edited_resource();
  124. if (scr.is_valid()) {
  125. /* Core types. */
  126. const Color basetype_color = EDITOR_GET("text_editor/theme/highlighting/base_type_color");
  127. List<String> core_types;
  128. scr->get_language()->get_core_type_words(&core_types);
  129. for (const String &E : core_types) {
  130. highlighter->add_keyword_color(E, basetype_color);
  131. }
  132. /* Reserved words. */
  133. const Color keyword_color = EDITOR_GET("text_editor/theme/highlighting/keyword_color");
  134. const Color control_flow_keyword_color = EDITOR_GET("text_editor/theme/highlighting/control_flow_keyword_color");
  135. List<String> keywords;
  136. scr->get_language()->get_reserved_words(&keywords);
  137. for (const String &E : keywords) {
  138. if (scr->get_language()->is_control_flow_keyword(E)) {
  139. highlighter->add_keyword_color(E, control_flow_keyword_color);
  140. } else {
  141. highlighter->add_keyword_color(E, keyword_color);
  142. }
  143. }
  144. /* Member types. */
  145. const Color member_variable_color = EDITOR_GET("text_editor/theme/highlighting/member_variable_color");
  146. StringName instance_base = scr->get_instance_base_type();
  147. if (instance_base != StringName()) {
  148. List<PropertyInfo> plist;
  149. ClassDB::get_property_list(instance_base, &plist);
  150. for (const PropertyInfo &E : plist) {
  151. String prop_name = E.name;
  152. if (E.usage & PROPERTY_USAGE_CATEGORY || E.usage & PROPERTY_USAGE_GROUP || E.usage & PROPERTY_USAGE_SUBGROUP) {
  153. continue;
  154. }
  155. if (prop_name.contains("/")) {
  156. continue;
  157. }
  158. highlighter->add_member_keyword_color(prop_name, member_variable_color);
  159. }
  160. List<String> clist;
  161. ClassDB::get_integer_constant_list(instance_base, &clist);
  162. for (const String &E : clist) {
  163. highlighter->add_member_keyword_color(E, member_variable_color);
  164. }
  165. }
  166. /* Comments */
  167. const Color comment_color = EDITOR_GET("text_editor/theme/highlighting/comment_color");
  168. List<String> comments;
  169. scr->get_language()->get_comment_delimiters(&comments);
  170. for (const String &comment : comments) {
  171. String beg = comment.get_slice(" ", 0);
  172. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  173. highlighter->add_color_region(beg, end, comment_color, end.is_empty());
  174. }
  175. /* Doc comments */
  176. const Color doc_comment_color = EDITOR_GET("text_editor/theme/highlighting/doc_comment_color");
  177. List<String> doc_comments;
  178. scr->get_language()->get_doc_comment_delimiters(&doc_comments);
  179. for (const String &doc_comment : doc_comments) {
  180. String beg = doc_comment.get_slice(" ", 0);
  181. String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slice(" ", 1) : String();
  182. highlighter->add_color_region(beg, end, doc_comment_color, end.is_empty());
  183. }
  184. /* Strings */
  185. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  186. List<String> strings;
  187. scr->get_language()->get_string_delimiters(&strings);
  188. for (const String &string : strings) {
  189. String beg = string.get_slice(" ", 0);
  190. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  191. highlighter->add_color_region(beg, end, string_color, end.is_empty());
  192. }
  193. }
  194. }
  195. Ref<EditorSyntaxHighlighter> EditorStandardSyntaxHighlighter::_create() const {
  196. Ref<EditorStandardSyntaxHighlighter> syntax_highlighter;
  197. syntax_highlighter.instantiate();
  198. return syntax_highlighter;
  199. }
  200. ////
  201. Ref<EditorSyntaxHighlighter> EditorPlainTextSyntaxHighlighter::_create() const {
  202. Ref<EditorPlainTextSyntaxHighlighter> syntax_highlighter;
  203. syntax_highlighter.instantiate();
  204. return syntax_highlighter;
  205. }
  206. ////
  207. void EditorJSONSyntaxHighlighter::_update_cache() {
  208. highlighter->set_text_edit(text_edit);
  209. highlighter->clear_keyword_colors();
  210. highlighter->clear_member_keyword_colors();
  211. highlighter->clear_color_regions();
  212. highlighter->set_symbol_color(EDITOR_GET("text_editor/theme/highlighting/symbol_color"));
  213. highlighter->set_number_color(EDITOR_GET("text_editor/theme/highlighting/number_color"));
  214. const Color string_color = EDITOR_GET("text_editor/theme/highlighting/string_color");
  215. highlighter->add_color_region("\"", "\"", string_color);
  216. }
  217. Ref<EditorSyntaxHighlighter> EditorJSONSyntaxHighlighter::_create() const {
  218. Ref<EditorJSONSyntaxHighlighter> syntax_highlighter;
  219. syntax_highlighter.instantiate();
  220. return syntax_highlighter;
  221. }
  222. ////////////////////////////////////////////////////////////////////////////////
  223. /*** SCRIPT EDITOR ****/
  224. void ScriptEditorBase::_bind_methods() {
  225. ClassDB::bind_method(D_METHOD("get_base_editor"), &ScriptEditorBase::get_base_editor);
  226. ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptEditorBase::add_syntax_highlighter);
  227. ADD_SIGNAL(MethodInfo("name_changed"));
  228. ADD_SIGNAL(MethodInfo("edited_script_changed"));
  229. ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
  230. ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
  231. ADD_SIGNAL(MethodInfo("request_save_history"));
  232. ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
  233. ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
  234. ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
  235. ADD_SIGNAL(MethodInfo("go_to_method", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::STRING, "method")));
  236. }
  237. class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache {
  238. struct Cache {
  239. uint64_t time_loaded = 0;
  240. Ref<Resource> cache;
  241. };
  242. HashMap<String, Cache> cached;
  243. public:
  244. uint64_t max_time_cache = 5 * 60 * 1000; //minutes, five
  245. uint32_t max_cache_size = 128;
  246. void cleanup() {
  247. List<String> to_clean;
  248. HashMap<String, Cache>::Iterator I = cached.begin();
  249. while (I) {
  250. if ((OS::get_singleton()->get_ticks_msec() - I->value.time_loaded) > max_time_cache) {
  251. to_clean.push_back(I->key);
  252. }
  253. ++I;
  254. }
  255. while (to_clean.front()) {
  256. cached.erase(to_clean.front()->get());
  257. to_clean.pop_front();
  258. }
  259. }
  260. virtual Ref<Resource> get_cached_resource(const String &p_path) {
  261. HashMap<String, Cache>::Iterator E = cached.find(p_path);
  262. if (!E) {
  263. Cache c;
  264. c.cache = ResourceLoader::load(p_path);
  265. E = cached.insert(p_path, c);
  266. }
  267. E->value.time_loaded = OS::get_singleton()->get_ticks_msec();
  268. if (cached.size() > max_cache_size) {
  269. uint64_t older;
  270. HashMap<String, Cache>::Iterator O = cached.begin();
  271. older = O->value.time_loaded;
  272. HashMap<String, Cache>::Iterator I = O;
  273. while (I) {
  274. if (I->value.time_loaded < older) {
  275. older = I->value.time_loaded;
  276. O = I;
  277. }
  278. ++I;
  279. }
  280. if (O != E) { //should never happen..
  281. cached.remove(O);
  282. }
  283. }
  284. return E->value.cache;
  285. }
  286. virtual ~EditorScriptCodeCompletionCache() {}
  287. };
  288. void ScriptEditorQuickOpen::popup_dialog(const Vector<String> &p_functions, bool p_dontclear) {
  289. popup_centered_ratio(0.6);
  290. if (p_dontclear) {
  291. search_box->select_all();
  292. } else {
  293. search_box->clear();
  294. }
  295. search_box->grab_focus();
  296. functions = p_functions;
  297. _update_search();
  298. }
  299. void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) {
  300. _update_search();
  301. }
  302. void ScriptEditorQuickOpen::_sbox_input(const Ref<InputEvent> &p_ie) {
  303. Ref<InputEventKey> k = p_ie;
  304. if (k.is_valid() && (k->get_keycode() == Key::UP || k->get_keycode() == Key::DOWN || k->get_keycode() == Key::PAGEUP || k->get_keycode() == Key::PAGEDOWN)) {
  305. search_options->gui_input(k);
  306. search_box->accept_event();
  307. }
  308. }
  309. void ScriptEditorQuickOpen::_update_search() {
  310. search_options->clear();
  311. TreeItem *root = search_options->create_item();
  312. for (int i = 0; i < functions.size(); i++) {
  313. String file = functions[i];
  314. if ((search_box->get_text().is_empty() || file.findn(search_box->get_text()) != -1)) {
  315. TreeItem *ti = search_options->create_item(root);
  316. ti->set_text(0, file);
  317. if (root->get_first_child() == ti) {
  318. ti->select(0);
  319. }
  320. }
  321. }
  322. get_ok_button()->set_disabled(root->get_first_child() == nullptr);
  323. }
  324. void ScriptEditorQuickOpen::_confirmed() {
  325. TreeItem *ti = search_options->get_selected();
  326. if (!ti) {
  327. return;
  328. }
  329. int line = ti->get_text(0).get_slice(":", 1).to_int();
  330. emit_signal(SNAME("goto_line"), line - 1);
  331. hide();
  332. }
  333. void ScriptEditorQuickOpen::_notification(int p_what) {
  334. switch (p_what) {
  335. case NOTIFICATION_ENTER_TREE: {
  336. connect("confirmed", callable_mp(this, &ScriptEditorQuickOpen::_confirmed));
  337. search_box->set_clear_button_enabled(true);
  338. [[fallthrough]];
  339. }
  340. case NOTIFICATION_VISIBILITY_CHANGED: {
  341. search_box->set_right_icon(search_options->get_editor_theme_icon(SNAME("Search")));
  342. } break;
  343. case NOTIFICATION_EXIT_TREE: {
  344. disconnect("confirmed", callable_mp(this, &ScriptEditorQuickOpen::_confirmed));
  345. } break;
  346. }
  347. }
  348. void ScriptEditorQuickOpen::_bind_methods() {
  349. ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line")));
  350. }
  351. ScriptEditorQuickOpen::ScriptEditorQuickOpen() {
  352. VBoxContainer *vbc = memnew(VBoxContainer);
  353. add_child(vbc);
  354. search_box = memnew(LineEdit);
  355. vbc->add_margin_child(TTR("Search:"), search_box);
  356. search_box->connect("text_changed", callable_mp(this, &ScriptEditorQuickOpen::_text_changed));
  357. search_box->connect("gui_input", callable_mp(this, &ScriptEditorQuickOpen::_sbox_input));
  358. search_options = memnew(Tree);
  359. vbc->add_margin_child(TTR("Matches:"), search_options, true);
  360. set_ok_button_text(TTR("Open"));
  361. get_ok_button()->set_disabled(true);
  362. register_text_enter(search_box);
  363. set_hide_on_ok(false);
  364. search_options->connect("item_activated", callable_mp(this, &ScriptEditorQuickOpen::_confirmed));
  365. search_options->set_hide_root(true);
  366. search_options->set_hide_folding(true);
  367. search_options->add_theme_constant_override("draw_guides", 1);
  368. }
  369. /////////////////////////////////
  370. ScriptEditor *ScriptEditor::script_editor = nullptr;
  371. /*** SCRIPT EDITOR ******/
  372. String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) {
  373. String val = EditorDebuggerNode::get_singleton()->get_var_value(p_text);
  374. const int display_limit = 300;
  375. if (!val.is_empty()) {
  376. if (val.size() > display_limit) {
  377. val = val.left(display_limit) + " [...] truncated!";
  378. }
  379. return p_text + ": " + val;
  380. } else {
  381. return String();
  382. }
  383. }
  384. void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) {
  385. if (external_editor_active) {
  386. return;
  387. }
  388. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  389. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  390. if (!se) {
  391. continue;
  392. }
  393. se->set_debugger_active(p_breaked);
  394. }
  395. }
  396. void ScriptEditor::_script_created(Ref<Script> p_script) {
  397. EditorNode::get_singleton()->push_item(p_script.operator->());
  398. }
  399. void ScriptEditor::_goto_script_line2(int p_line) {
  400. ScriptEditorBase *current = _get_current_editor();
  401. if (current) {
  402. current->goto_line(p_line);
  403. }
  404. }
  405. void ScriptEditor::_goto_script_line(Ref<RefCounted> p_script, int p_line) {
  406. Ref<Script> scr = Object::cast_to<Script>(*p_script);
  407. if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) {
  408. if (edit(p_script, p_line, 0)) {
  409. EditorNode::get_singleton()->push_item(p_script.ptr());
  410. ScriptEditorBase *current = _get_current_editor();
  411. if (ScriptTextEditor *script_text_editor = Object::cast_to<ScriptTextEditor>(current)) {
  412. script_text_editor->goto_line_centered(p_line);
  413. } else if (current) {
  414. current->goto_line(p_line, true);
  415. }
  416. _save_history();
  417. }
  418. }
  419. }
  420. void ScriptEditor::_set_execution(Ref<RefCounted> p_script, int p_line) {
  421. Ref<Script> scr = Object::cast_to<Script>(*p_script);
  422. if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) {
  423. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  424. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  425. if (!se) {
  426. continue;
  427. }
  428. if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) {
  429. se->set_executing_line(p_line);
  430. }
  431. }
  432. }
  433. }
  434. void ScriptEditor::_clear_execution(Ref<RefCounted> p_script) {
  435. Ref<Script> scr = Object::cast_to<Script>(*p_script);
  436. if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) {
  437. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  438. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  439. if (!se) {
  440. continue;
  441. }
  442. if ((scr != nullptr && se->get_edited_resource() == p_script) || se->get_edited_resource()->get_path() == scr->get_path()) {
  443. se->clear_executing_line();
  444. }
  445. }
  446. }
  447. }
  448. void ScriptEditor::_set_breakpoint(Ref<RefCounted> p_script, int p_line, bool p_enabled) {
  449. Ref<Script> scr = Object::cast_to<Script>(*p_script);
  450. if (scr.is_valid() && (scr->has_source_code() || scr->get_path().is_resource_file())) {
  451. // Update if open.
  452. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  453. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  454. if (se && se->get_edited_resource()->get_path() == scr->get_path()) {
  455. se->set_breakpoint(p_line, p_enabled);
  456. return;
  457. }
  458. }
  459. // Handle closed.
  460. Dictionary state = script_editor_cache->get_value(scr->get_path(), "state");
  461. Array breakpoints;
  462. if (state.has("breakpoints")) {
  463. breakpoints = state["breakpoints"];
  464. }
  465. if (breakpoints.has(p_line)) {
  466. if (!p_enabled) {
  467. breakpoints.erase(p_line);
  468. }
  469. } else if (p_enabled) {
  470. breakpoints.push_back(p_line);
  471. }
  472. state["breakpoints"] = breakpoints;
  473. script_editor_cache->set_value(scr->get_path(), "state", state);
  474. EditorDebuggerNode::get_singleton()->set_breakpoint(scr->get_path(), p_line + 1, p_enabled);
  475. }
  476. }
  477. void ScriptEditor::_clear_breakpoints() {
  478. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  479. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  480. if (se) {
  481. se->clear_breakpoints();
  482. }
  483. }
  484. // Clear from closed scripts.
  485. List<String> cached_editors;
  486. script_editor_cache->get_sections(&cached_editors);
  487. for (const String &E : cached_editors) {
  488. Array breakpoints = _get_cached_breakpoints_for_script(E);
  489. for (int i = 0; i < breakpoints.size(); i++) {
  490. EditorDebuggerNode::get_singleton()->set_breakpoint(E, (int)breakpoints[i] + 1, false);
  491. }
  492. if (breakpoints.size() > 0) {
  493. Dictionary state = script_editor_cache->get_value(E, "state");
  494. state["breakpoints"] = Array();
  495. script_editor_cache->set_value(E, "state", state);
  496. }
  497. }
  498. }
  499. Array ScriptEditor::_get_cached_breakpoints_for_script(const String &p_path) const {
  500. if (!ResourceLoader::exists(p_path, "Script") || p_path.begins_with("local://") || !script_editor_cache->has_section_key(p_path, "state")) {
  501. return Array();
  502. }
  503. Dictionary state = script_editor_cache->get_value(p_path, "state");
  504. if (!state.has("breakpoints")) {
  505. return Array();
  506. }
  507. return state["breakpoints"];
  508. }
  509. ScriptEditorBase *ScriptEditor::_get_current_editor() const {
  510. int selected = tab_container->get_current_tab();
  511. if (selected < 0 || selected >= tab_container->get_tab_count()) {
  512. return nullptr;
  513. }
  514. return Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected));
  515. }
  516. void ScriptEditor::_update_history_arrows() {
  517. script_back->set_disabled(history_pos <= 0);
  518. script_forward->set_disabled(history_pos >= history.size() - 1);
  519. }
  520. void ScriptEditor::_save_history() {
  521. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  522. Node *n = tab_container->get_current_tab_control();
  523. if (Object::cast_to<ScriptEditorBase>(n)) {
  524. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state();
  525. }
  526. if (Object::cast_to<EditorHelp>(n)) {
  527. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  528. }
  529. }
  530. history.resize(history_pos + 1);
  531. ScriptHistory sh;
  532. sh.control = tab_container->get_current_tab_control();
  533. sh.state = Variant();
  534. history.push_back(sh);
  535. history_pos++;
  536. _update_history_arrows();
  537. }
  538. void ScriptEditor::_go_to_tab(int p_idx) {
  539. ScriptEditorBase *current = _get_current_editor();
  540. if (current) {
  541. if (current->is_unsaved()) {
  542. current->apply_code();
  543. }
  544. }
  545. Control *c = tab_container->get_tab_control(p_idx);
  546. if (!c) {
  547. return;
  548. }
  549. if (history_pos >= 0 && history_pos < history.size() && history[history_pos].control == tab_container->get_current_tab_control()) {
  550. Node *n = tab_container->get_current_tab_control();
  551. if (Object::cast_to<ScriptEditorBase>(n)) {
  552. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state();
  553. }
  554. if (Object::cast_to<EditorHelp>(n)) {
  555. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  556. }
  557. }
  558. history.resize(history_pos + 1);
  559. ScriptHistory sh;
  560. sh.control = c;
  561. sh.state = Variant();
  562. history.push_back(sh);
  563. history_pos++;
  564. tab_container->set_current_tab(p_idx);
  565. c = tab_container->get_current_tab_control();
  566. if (Object::cast_to<ScriptEditorBase>(c)) {
  567. script_name_label->set_text(Object::cast_to<ScriptEditorBase>(c)->get_name());
  568. script_icon->set_texture(Object::cast_to<ScriptEditorBase>(c)->get_theme_icon());
  569. if (is_visible_in_tree()) {
  570. Object::cast_to<ScriptEditorBase>(c)->ensure_focus();
  571. }
  572. Ref<Script> scr = Object::cast_to<ScriptEditorBase>(c)->get_edited_resource();
  573. if (scr != nullptr) {
  574. notify_script_changed(scr);
  575. }
  576. Object::cast_to<ScriptEditorBase>(c)->validate();
  577. }
  578. if (Object::cast_to<EditorHelp>(c)) {
  579. script_name_label->set_text(Object::cast_to<EditorHelp>(c)->get_class());
  580. script_icon->set_texture(get_editor_theme_icon(SNAME("Help")));
  581. if (is_visible_in_tree()) {
  582. Object::cast_to<EditorHelp>(c)->set_focused();
  583. }
  584. }
  585. c->set_meta("__editor_pass", ++edit_pass);
  586. _update_history_arrows();
  587. _update_script_colors();
  588. _update_members_overview();
  589. _update_help_overview();
  590. _update_selected_editor_menu();
  591. _update_members_overview_visibility();
  592. _update_help_overview_visibility();
  593. }
  594. void ScriptEditor::_add_recent_script(String p_path) {
  595. if (p_path.is_empty()) {
  596. return;
  597. }
  598. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  599. if (rc.find(p_path) != -1) {
  600. rc.erase(p_path);
  601. }
  602. rc.push_front(p_path);
  603. if (rc.size() > 10) {
  604. rc.resize(10);
  605. }
  606. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  607. _update_recent_scripts();
  608. }
  609. void ScriptEditor::_update_recent_scripts() {
  610. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  611. recent_scripts->clear();
  612. String path;
  613. for (int i = 0; i < rc.size(); i++) {
  614. path = rc[i];
  615. recent_scripts->add_item(path.replace("res://", ""));
  616. }
  617. recent_scripts->add_separator();
  618. recent_scripts->add_shortcut(ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Files")));
  619. recent_scripts->set_item_disabled(recent_scripts->get_item_id(recent_scripts->get_item_count() - 1), rc.is_empty());
  620. recent_scripts->reset_size();
  621. }
  622. void ScriptEditor::_open_recent_script(int p_idx) {
  623. // clear button
  624. if (p_idx == recent_scripts->get_item_count() - 1) {
  625. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", Array());
  626. callable_mp(this, &ScriptEditor::_update_recent_scripts).call_deferred();
  627. return;
  628. }
  629. Array rc = EditorSettings::get_singleton()->get_project_metadata("recent_files", "scripts", Array());
  630. ERR_FAIL_INDEX(p_idx, rc.size());
  631. String path = rc[p_idx];
  632. // if its not on disk its a help file or deleted
  633. if (FileAccess::exists(path)) {
  634. List<String> extensions;
  635. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  636. ResourceLoader::get_recognized_extensions_for_type("JSON", &extensions);
  637. if (extensions.find(path.get_extension())) {
  638. Ref<Resource> scr = ResourceLoader::load(path);
  639. if (scr.is_valid()) {
  640. edit(scr, true);
  641. return;
  642. }
  643. }
  644. Error err;
  645. Ref<TextFile> text_file = _load_text_file(path, &err);
  646. if (text_file.is_valid()) {
  647. edit(text_file, true);
  648. return;
  649. }
  650. // if it's a path then it's most likely a deleted file not help
  651. } else if (path.contains("::")) {
  652. // built-in script
  653. String res_path = path.get_slice("::", 0);
  654. if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
  655. if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
  656. EditorNode::get_singleton()->load_scene(res_path);
  657. }
  658. } else {
  659. EditorNode::get_singleton()->load_resource(res_path);
  660. }
  661. Ref<Script> scr = ResourceLoader::load(path);
  662. if (scr.is_valid()) {
  663. edit(scr, true);
  664. return;
  665. }
  666. } else if (!path.is_resource_file()) {
  667. _help_class_open(path);
  668. return;
  669. }
  670. rc.remove_at(p_idx);
  671. EditorSettings::get_singleton()->set_project_metadata("recent_files", "scripts", rc);
  672. _update_recent_scripts();
  673. _show_error_dialog(path);
  674. }
  675. void ScriptEditor::_show_error_dialog(String p_path) {
  676. error_dialog->set_text(vformat(TTR("Can't open '%s'. The file could have been moved or deleted."), p_path));
  677. error_dialog->popup_centered();
  678. }
  679. void ScriptEditor::_close_tab(int p_idx, bool p_save, bool p_history_back) {
  680. int selected = p_idx;
  681. if (selected < 0 || selected >= tab_container->get_tab_count()) {
  682. return;
  683. }
  684. Node *tselected = tab_container->get_tab_control(selected);
  685. ScriptEditorBase *current = Object::cast_to<ScriptEditorBase>(tselected);
  686. if (current) {
  687. Ref<Resource> file = current->get_edited_resource();
  688. if (p_save && file.is_valid()) {
  689. // Do not try to save internal scripts, but prompt to save in-memory
  690. // scripts which are not saved to disk yet (have empty path).
  691. if (!file->is_built_in()) {
  692. save_current_script();
  693. }
  694. }
  695. if (file.is_valid()) {
  696. if (!file->get_path().is_empty()) {
  697. // Only saved scripts can be restored.
  698. previous_scripts.push_back(file->get_path());
  699. }
  700. Ref<Script> scr = file;
  701. if (scr.is_valid()) {
  702. notify_script_close(scr);
  703. }
  704. }
  705. }
  706. // roll back to previous tab
  707. if (p_history_back) {
  708. _history_back();
  709. }
  710. //remove from history
  711. history.resize(history_pos + 1);
  712. for (int i = 0; i < history.size(); i++) {
  713. if (history[i].control == tselected) {
  714. history.remove_at(i);
  715. i--;
  716. history_pos--;
  717. }
  718. }
  719. if (history_pos >= history.size()) {
  720. history_pos = history.size() - 1;
  721. }
  722. int idx = tab_container->get_current_tab();
  723. if (current) {
  724. current->clear_edit_menu();
  725. _save_editor_state(current);
  726. }
  727. memdelete(tselected);
  728. if (idx >= tab_container->get_tab_count()) {
  729. idx = tab_container->get_tab_count() - 1;
  730. }
  731. if (idx >= 0) {
  732. if (history_pos >= 0) {
  733. idx = tab_container->get_tab_idx_from_control(history[history_pos].control);
  734. }
  735. _go_to_tab(idx);
  736. } else {
  737. _update_selected_editor_menu();
  738. }
  739. if (script_close_queue.is_empty()) {
  740. _update_history_arrows();
  741. _update_script_names();
  742. _update_members_overview_visibility();
  743. _update_help_overview_visibility();
  744. _save_layout();
  745. _update_find_replace_bar();
  746. }
  747. }
  748. void ScriptEditor::_close_current_tab(bool p_save) {
  749. _close_tab(tab_container->get_current_tab(), p_save);
  750. }
  751. void ScriptEditor::_close_discard_current_tab(const String &p_str) {
  752. Ref<Script> scr = _get_current_script();
  753. if (scr.is_valid()) {
  754. scr->reload_from_file();
  755. }
  756. _close_tab(tab_container->get_current_tab(), false);
  757. erase_tab_confirm->hide();
  758. }
  759. void ScriptEditor::_close_docs_tab() {
  760. int child_count = tab_container->get_tab_count();
  761. for (int i = child_count - 1; i >= 0; i--) {
  762. EditorHelp *se = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  763. if (se) {
  764. _close_tab(i, true, false);
  765. }
  766. }
  767. }
  768. void ScriptEditor::_copy_script_path() {
  769. ScriptEditorBase *se = _get_current_editor();
  770. if (se) {
  771. Ref<Resource> scr = se->get_edited_resource();
  772. DisplayServer::get_singleton()->clipboard_set(scr->get_path());
  773. }
  774. }
  775. void ScriptEditor::_close_other_tabs() {
  776. int current_idx = tab_container->get_current_tab();
  777. for (int i = tab_container->get_tab_count() - 1; i >= 0; i--) {
  778. if (i != current_idx) {
  779. script_close_queue.push_back(i);
  780. }
  781. }
  782. _queue_close_tabs();
  783. }
  784. void ScriptEditor::_close_all_tabs() {
  785. for (int i = tab_container->get_tab_count() - 1; i >= 0; i--) {
  786. script_close_queue.push_back(i);
  787. }
  788. _queue_close_tabs();
  789. }
  790. void ScriptEditor::_queue_close_tabs() {
  791. while (!script_close_queue.is_empty()) {
  792. int idx = script_close_queue.front()->get();
  793. script_close_queue.pop_front();
  794. tab_container->set_current_tab(idx);
  795. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(idx));
  796. if (se) {
  797. // Maybe there are unsaved changes.
  798. if (se->is_unsaved()) {
  799. _ask_close_current_unsaved_tab(se);
  800. erase_tab_confirm->connect(SceneStringNames::get_singleton()->visibility_changed, callable_mp(this, &ScriptEditor::_queue_close_tabs), CONNECT_ONE_SHOT);
  801. break;
  802. }
  803. }
  804. _close_current_tab(false);
  805. }
  806. _update_find_replace_bar();
  807. }
  808. void ScriptEditor::_ask_close_current_unsaved_tab(ScriptEditorBase *current) {
  809. erase_tab_confirm->set_text(TTR("Close and save changes?") + "\n\"" + current->get_name() + "\"");
  810. erase_tab_confirm->popup_centered();
  811. }
  812. void ScriptEditor::_resave_scripts(const String &p_str) {
  813. apply_scripts();
  814. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  815. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  816. if (!se) {
  817. continue;
  818. }
  819. Ref<Resource> scr = se->get_edited_resource();
  820. if (scr->is_built_in()) {
  821. continue; //internal script, who cares
  822. }
  823. if (trim_trailing_whitespace_on_save) {
  824. se->trim_trailing_whitespace();
  825. }
  826. se->insert_final_newline();
  827. if (convert_indent_on_save) {
  828. se->convert_indent();
  829. }
  830. Ref<TextFile> text_file = scr;
  831. if (text_file != nullptr) {
  832. se->apply_code();
  833. _save_text_file(text_file, text_file->get_path());
  834. break;
  835. } else {
  836. EditorNode::get_singleton()->save_resource(scr);
  837. }
  838. se->tag_saved_version();
  839. }
  840. disk_changed->hide();
  841. }
  842. void ScriptEditor::_res_saved_callback(const Ref<Resource> &p_res) {
  843. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  844. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  845. if (!se) {
  846. continue;
  847. }
  848. Ref<Resource> scr = se->get_edited_resource();
  849. if (scr == p_res) {
  850. se->tag_saved_version();
  851. }
  852. }
  853. _update_script_names();
  854. Ref<Script> scr = p_res;
  855. if (scr.is_valid()) {
  856. trigger_live_script_reload(scr->get_path());
  857. }
  858. }
  859. void ScriptEditor::_scene_saved_callback(const String &p_path) {
  860. // If scene was saved, mark all built-in scripts from that scene as saved.
  861. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  862. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  863. if (!se) {
  864. continue;
  865. }
  866. Ref<Resource> edited_res = se->get_edited_resource();
  867. if (!edited_res->is_built_in()) {
  868. continue; // External script, who cares.
  869. }
  870. if (edited_res->get_path().get_slice("::", 0) == p_path) {
  871. se->tag_saved_version();
  872. }
  873. Ref<Script> scr = edited_res;
  874. if (scr.is_valid() && scr->is_tool()) {
  875. scr->reload(true);
  876. }
  877. }
  878. }
  879. void ScriptEditor::trigger_live_script_reload(const String &p_script_path) {
  880. if (!script_paths_to_reload.has(p_script_path)) {
  881. script_paths_to_reload.append(p_script_path);
  882. }
  883. if (!pending_auto_reload && auto_reload_running_scripts) {
  884. callable_mp(this, &ScriptEditor::_live_auto_reload_running_scripts).call_deferred();
  885. pending_auto_reload = true;
  886. }
  887. }
  888. void ScriptEditor::trigger_live_script_reload_all() {
  889. if (!pending_auto_reload && auto_reload_running_scripts) {
  890. call_deferred(SNAME("_live_auto_reload_running_scripts"));
  891. pending_auto_reload = true;
  892. reload_all_scripts = true;
  893. }
  894. }
  895. void ScriptEditor::_live_auto_reload_running_scripts() {
  896. pending_auto_reload = false;
  897. if (reload_all_scripts) {
  898. EditorDebuggerNode::get_singleton()->reload_all_scripts();
  899. } else {
  900. EditorDebuggerNode::get_singleton()->reload_scripts(script_paths_to_reload);
  901. }
  902. reload_all_scripts = false;
  903. script_paths_to_reload.clear();
  904. }
  905. bool ScriptEditor::_test_script_times_on_disk(Ref<Resource> p_for_script) {
  906. disk_changed_list->clear();
  907. TreeItem *r = disk_changed_list->create_item();
  908. disk_changed_list->set_hide_root(true);
  909. bool need_ask = false;
  910. bool need_reload = false;
  911. bool use_autoreload = bool(EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change"));
  912. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  913. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  914. if (se) {
  915. Ref<Resource> edited_res = se->get_edited_resource();
  916. if (p_for_script.is_valid() && edited_res.is_valid() && p_for_script != edited_res) {
  917. continue;
  918. }
  919. if (edited_res->is_built_in()) {
  920. continue; //internal script, who cares
  921. }
  922. uint64_t last_date = edited_res->get_last_modified_time();
  923. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  924. if (last_date != date) {
  925. TreeItem *ti = disk_changed_list->create_item(r);
  926. ti->set_text(0, edited_res->get_path().get_file());
  927. if (!use_autoreload || se->is_unsaved()) {
  928. need_ask = true;
  929. }
  930. need_reload = true;
  931. }
  932. }
  933. }
  934. if (need_reload) {
  935. if (!need_ask) {
  936. script_editor->reload_scripts();
  937. need_reload = false;
  938. } else {
  939. callable_mp((Window *)disk_changed, &Window::popup_centered_ratio).call_deferred(0.3);
  940. }
  941. }
  942. return need_reload;
  943. }
  944. void ScriptEditor::_file_dialog_action(String p_file) {
  945. switch (file_dialog_option) {
  946. case FILE_NEW_TEXTFILE: {
  947. Error err;
  948. {
  949. Ref<FileAccess> file = FileAccess::open(p_file, FileAccess::WRITE, &err);
  950. if (err) {
  951. EditorNode::get_singleton()->show_warning(TTR("Error writing TextFile:") + "\n" + p_file, TTR("Error!"));
  952. break;
  953. }
  954. }
  955. if (EditorFileSystem::get_singleton()) {
  956. if (textfile_extensions.has(p_file.get_extension())) {
  957. EditorFileSystem::get_singleton()->update_file(p_file);
  958. }
  959. }
  960. if (!open_textfile_after_create) {
  961. return;
  962. }
  963. [[fallthrough]];
  964. }
  965. case FILE_OPEN: {
  966. open_file(p_file);
  967. file_dialog_option = -1;
  968. } break;
  969. case FILE_SAVE_AS: {
  970. ScriptEditorBase *current = _get_current_editor();
  971. if (current) {
  972. Ref<Resource> resource = current->get_edited_resource();
  973. String path = ProjectSettings::get_singleton()->localize_path(p_file);
  974. Error err = _save_text_file(resource, path);
  975. if (err != OK) {
  976. EditorNode::get_singleton()->show_accept(TTR("Error saving file!"), TTR("OK"));
  977. return;
  978. }
  979. resource->set_path(path);
  980. _update_script_names();
  981. }
  982. } break;
  983. case THEME_SAVE_AS: {
  984. if (!EditorSettings::get_singleton()->save_text_editor_theme_as(p_file)) {
  985. EditorNode::get_singleton()->show_warning(TTR("Error while saving theme."), TTR("Error Saving"));
  986. }
  987. } break;
  988. case THEME_IMPORT: {
  989. if (!EditorSettings::get_singleton()->import_text_editor_theme(p_file)) {
  990. EditorNode::get_singleton()->show_warning(TTR("Error importing theme."), TTR("Error Importing"));
  991. }
  992. } break;
  993. }
  994. file_dialog_option = -1;
  995. }
  996. Ref<Script> ScriptEditor::_get_current_script() {
  997. ScriptEditorBase *current = _get_current_editor();
  998. if (current) {
  999. Ref<Script> scr = current->get_edited_resource();
  1000. return scr != nullptr ? scr : nullptr;
  1001. } else {
  1002. return nullptr;
  1003. }
  1004. }
  1005. TypedArray<Script> ScriptEditor::_get_open_scripts() const {
  1006. TypedArray<Script> ret;
  1007. Vector<Ref<Script>> scripts = get_open_scripts();
  1008. int scrits_amount = scripts.size();
  1009. for (int idx_script = 0; idx_script < scrits_amount; idx_script++) {
  1010. ret.push_back(scripts[idx_script]);
  1011. }
  1012. return ret;
  1013. }
  1014. bool ScriptEditor::toggle_scripts_panel() {
  1015. list_split->set_visible(!list_split->is_visible());
  1016. EditorSettings::get_singleton()->set_project_metadata("scripts_panel", "show_scripts_panel", list_split->is_visible());
  1017. return list_split->is_visible();
  1018. }
  1019. bool ScriptEditor::is_scripts_panel_toggled() {
  1020. return list_split->is_visible();
  1021. }
  1022. void ScriptEditor::_menu_option(int p_option) {
  1023. ScriptEditorBase *current = _get_current_editor();
  1024. switch (p_option) {
  1025. case FILE_NEW: {
  1026. script_create_dialog->config("Node", "new_script", false, false);
  1027. script_create_dialog->popup_centered();
  1028. } break;
  1029. case FILE_NEW_TEXTFILE: {
  1030. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1031. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1032. file_dialog_option = FILE_NEW_TEXTFILE;
  1033. file_dialog->clear_filters();
  1034. for (const String &E : textfile_extensions) {
  1035. file_dialog->add_filter("*." + E, E.to_upper());
  1036. }
  1037. file_dialog->popup_file_dialog();
  1038. file_dialog->set_title(TTR("New Text File..."));
  1039. open_textfile_after_create = true;
  1040. } break;
  1041. case FILE_OPEN: {
  1042. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1043. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1044. file_dialog_option = FILE_OPEN;
  1045. List<String> extensions;
  1046. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  1047. file_dialog->clear_filters();
  1048. for (int i = 0; i < extensions.size(); i++) {
  1049. file_dialog->add_filter("*." + extensions[i], extensions[i].to_upper());
  1050. }
  1051. for (const String &E : textfile_extensions) {
  1052. file_dialog->add_filter("*." + E, E.to_upper());
  1053. }
  1054. file_dialog->popup_file_dialog();
  1055. file_dialog->set_title(TTR("Open File"));
  1056. return;
  1057. } break;
  1058. case FILE_REOPEN_CLOSED: {
  1059. if (previous_scripts.is_empty()) {
  1060. return;
  1061. }
  1062. String path = previous_scripts.back()->get();
  1063. previous_scripts.pop_back();
  1064. List<String> extensions;
  1065. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  1066. ResourceLoader::get_recognized_extensions_for_type("JSON", &extensions);
  1067. bool built_in = !path.is_resource_file();
  1068. if (extensions.find(path.get_extension()) || built_in) {
  1069. if (built_in) {
  1070. String res_path = path.get_slice("::", 0);
  1071. if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
  1072. if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
  1073. EditorNode::get_singleton()->load_scene(res_path);
  1074. }
  1075. } else {
  1076. EditorNode::get_singleton()->load_resource(res_path);
  1077. }
  1078. }
  1079. Ref<Resource> scr = ResourceLoader::load(path);
  1080. if (!scr.is_valid()) {
  1081. EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!"));
  1082. file_dialog_option = -1;
  1083. return;
  1084. }
  1085. edit(scr);
  1086. file_dialog_option = -1;
  1087. } else {
  1088. Error error;
  1089. Ref<TextFile> text_file = _load_text_file(path, &error);
  1090. if (error != OK) {
  1091. EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!"));
  1092. }
  1093. if (text_file.is_valid()) {
  1094. edit(text_file);
  1095. file_dialog_option = -1;
  1096. }
  1097. }
  1098. } break;
  1099. case FILE_SAVE_ALL: {
  1100. if (_test_script_times_on_disk()) {
  1101. return;
  1102. }
  1103. save_all_scripts();
  1104. } break;
  1105. case SEARCH_IN_FILES: {
  1106. _on_find_in_files_requested("");
  1107. } break;
  1108. case REPLACE_IN_FILES: {
  1109. _on_replace_in_files_requested("");
  1110. } break;
  1111. case SEARCH_HELP: {
  1112. help_search_dialog->popup_dialog();
  1113. } break;
  1114. case SEARCH_WEBSITE: {
  1115. OS::get_singleton()->shell_open(VERSION_DOCS_URL "/");
  1116. } break;
  1117. case WINDOW_NEXT: {
  1118. _history_forward();
  1119. } break;
  1120. case WINDOW_PREV: {
  1121. _history_back();
  1122. } break;
  1123. case WINDOW_SORT: {
  1124. _sort_list_on_update = true;
  1125. _update_script_names();
  1126. } break;
  1127. case TOGGLE_SCRIPTS_PANEL: {
  1128. toggle_scripts_panel();
  1129. if (current) {
  1130. current->update_toggle_scripts_button();
  1131. } else {
  1132. Control *tab = tab_container->get_current_tab_control();
  1133. EditorHelp *editor_help = Object::cast_to<EditorHelp>(tab);
  1134. if (editor_help) {
  1135. editor_help->update_toggle_scripts_button();
  1136. }
  1137. }
  1138. }
  1139. }
  1140. if (current) {
  1141. switch (p_option) {
  1142. case FILE_SAVE: {
  1143. save_current_script();
  1144. } break;
  1145. case FILE_SAVE_AS: {
  1146. if (trim_trailing_whitespace_on_save) {
  1147. current->trim_trailing_whitespace();
  1148. }
  1149. current->insert_final_newline();
  1150. if (convert_indent_on_save) {
  1151. current->convert_indent();
  1152. }
  1153. Ref<Resource> resource = current->get_edited_resource();
  1154. Ref<TextFile> text_file = resource;
  1155. Ref<Script> scr = resource;
  1156. if (text_file != nullptr) {
  1157. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1158. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1159. file_dialog_option = FILE_SAVE_AS;
  1160. List<String> extensions;
  1161. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  1162. file_dialog->clear_filters();
  1163. file_dialog->set_current_dir(text_file->get_path().get_base_dir());
  1164. file_dialog->set_current_file(text_file->get_path().get_file());
  1165. file_dialog->popup_file_dialog();
  1166. file_dialog->set_title(TTR("Save File As..."));
  1167. break;
  1168. }
  1169. if (scr.is_valid()) {
  1170. clear_docs_from_script(scr);
  1171. }
  1172. EditorNode::get_singleton()->push_item(resource.ptr());
  1173. EditorNode::get_singleton()->save_resource_as(resource);
  1174. if (scr.is_valid()) {
  1175. update_docs_from_script(scr);
  1176. }
  1177. } break;
  1178. case FILE_TOOL_RELOAD_SOFT: {
  1179. Ref<Script> scr = current->get_edited_resource();
  1180. if (scr == nullptr || scr.is_null()) {
  1181. EditorNode::get_singleton()->show_warning(TTR("Can't obtain the script for reloading."));
  1182. break;
  1183. }
  1184. if (!scr->is_tool()) {
  1185. EditorNode::get_singleton()->show_warning(TTR("Reload only takes effect on tool scripts."));
  1186. return;
  1187. }
  1188. scr->reload(true);
  1189. } break;
  1190. case FILE_RUN: {
  1191. Ref<Script> scr = current->get_edited_resource();
  1192. if (scr == nullptr || scr.is_null()) {
  1193. EditorToaster::get_singleton()->popup_str(TTR("Cannot run the edited file because it's not a script."), EditorToaster::SEVERITY_WARNING);
  1194. break;
  1195. }
  1196. current->apply_code();
  1197. Error err = scr->reload(false); // Always hard reload the script before running.
  1198. if (err != OK || !scr->is_valid()) {
  1199. EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it contains errors, check the output log."), EditorToaster::SEVERITY_WARNING);
  1200. return;
  1201. }
  1202. // Perform additional checks on the script to evaluate if it's runnable.
  1203. bool is_runnable = true;
  1204. if (!ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorScript")) {
  1205. is_runnable = false;
  1206. EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it doesn't extend EditorScript."), EditorToaster::SEVERITY_WARNING);
  1207. }
  1208. if (!scr->is_tool()) {
  1209. is_runnable = false;
  1210. if (scr->get_class() == "GDScript") {
  1211. EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script (add the @tool annotation at the top)."), EditorToaster::SEVERITY_WARNING);
  1212. } else {
  1213. EditorToaster::get_singleton()->popup_str(TTR("Cannot run the script because it's not a tool script."), EditorToaster::SEVERITY_WARNING);
  1214. }
  1215. }
  1216. if (!is_runnable) {
  1217. return;
  1218. }
  1219. Ref<EditorScript> es = memnew(EditorScript);
  1220. es->set_script(scr);
  1221. es->run();
  1222. } break;
  1223. case FILE_CLOSE: {
  1224. if (current->is_unsaved()) {
  1225. _ask_close_current_unsaved_tab(current);
  1226. } else {
  1227. _close_current_tab(false);
  1228. }
  1229. } break;
  1230. case FILE_COPY_PATH: {
  1231. _copy_script_path();
  1232. } break;
  1233. case SHOW_IN_FILE_SYSTEM: {
  1234. const Ref<Resource> scr = current->get_edited_resource();
  1235. String path = scr->get_path();
  1236. if (!path.is_empty()) {
  1237. if (scr->is_built_in()) {
  1238. path = path.get_slice("::", 0); // Show the scene instead.
  1239. }
  1240. FileSystemDock::get_singleton()->navigate_to_path(path);
  1241. }
  1242. } break;
  1243. case CLOSE_DOCS: {
  1244. _close_docs_tab();
  1245. } break;
  1246. case CLOSE_OTHER_TABS: {
  1247. _close_other_tabs();
  1248. } break;
  1249. case CLOSE_ALL: {
  1250. _close_all_tabs();
  1251. } break;
  1252. case WINDOW_MOVE_UP: {
  1253. if (tab_container->get_current_tab() > 0) {
  1254. tab_container->move_child(current, tab_container->get_current_tab() - 1);
  1255. tab_container->set_current_tab(tab_container->get_current_tab());
  1256. _update_script_names();
  1257. }
  1258. } break;
  1259. case WINDOW_MOVE_DOWN: {
  1260. if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) {
  1261. tab_container->move_child(current, tab_container->get_current_tab() + 1);
  1262. tab_container->set_current_tab(tab_container->get_current_tab());
  1263. _update_script_names();
  1264. }
  1265. } break;
  1266. default: {
  1267. if (p_option >= WINDOW_SELECT_BASE) {
  1268. _go_to_tab(p_option - WINDOW_SELECT_BASE);
  1269. _update_script_names();
  1270. }
  1271. }
  1272. }
  1273. } else {
  1274. EditorHelp *help = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  1275. if (help) {
  1276. switch (p_option) {
  1277. case HELP_SEARCH_FIND: {
  1278. help->popup_search();
  1279. } break;
  1280. case HELP_SEARCH_FIND_NEXT: {
  1281. help->search_again();
  1282. } break;
  1283. case HELP_SEARCH_FIND_PREVIOUS: {
  1284. help->search_again(true);
  1285. } break;
  1286. case FILE_CLOSE: {
  1287. _close_current_tab();
  1288. } break;
  1289. case CLOSE_DOCS: {
  1290. _close_docs_tab();
  1291. } break;
  1292. case CLOSE_OTHER_TABS: {
  1293. _close_other_tabs();
  1294. } break;
  1295. case CLOSE_ALL: {
  1296. _close_all_tabs();
  1297. } break;
  1298. case WINDOW_MOVE_UP: {
  1299. if (tab_container->get_current_tab() > 0) {
  1300. tab_container->move_child(help, tab_container->get_current_tab() - 1);
  1301. tab_container->set_current_tab(tab_container->get_current_tab());
  1302. _update_script_names();
  1303. }
  1304. } break;
  1305. case WINDOW_MOVE_DOWN: {
  1306. if (tab_container->get_current_tab() < tab_container->get_tab_count() - 1) {
  1307. tab_container->move_child(help, tab_container->get_current_tab() + 1);
  1308. tab_container->set_current_tab(tab_container->get_current_tab());
  1309. _update_script_names();
  1310. }
  1311. } break;
  1312. }
  1313. }
  1314. }
  1315. }
  1316. void ScriptEditor::_theme_option(int p_option) {
  1317. switch (p_option) {
  1318. case THEME_IMPORT: {
  1319. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
  1320. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1321. file_dialog_option = THEME_IMPORT;
  1322. file_dialog->clear_filters();
  1323. file_dialog->add_filter("*.tet");
  1324. file_dialog->popup_file_dialog();
  1325. file_dialog->set_title(TTR("Import Theme"));
  1326. } break;
  1327. case THEME_RELOAD: {
  1328. EditorSettings::get_singleton()->load_text_editor_theme();
  1329. } break;
  1330. case THEME_SAVE: {
  1331. if (EditorSettings::get_singleton()->is_default_text_editor_theme()) {
  1332. ScriptEditor::_show_save_theme_as_dialog();
  1333. } else if (!EditorSettings::get_singleton()->save_text_editor_theme()) {
  1334. EditorNode::get_singleton()->show_warning(TTR("Error while saving theme"), TTR("Error saving"));
  1335. }
  1336. } break;
  1337. case THEME_SAVE_AS: {
  1338. ScriptEditor::_show_save_theme_as_dialog();
  1339. } break;
  1340. }
  1341. }
  1342. void ScriptEditor::_show_save_theme_as_dialog() {
  1343. file_dialog->set_file_mode(EditorFileDialog::FILE_MODE_SAVE_FILE);
  1344. file_dialog->set_access(EditorFileDialog::ACCESS_FILESYSTEM);
  1345. file_dialog_option = THEME_SAVE_AS;
  1346. file_dialog->clear_filters();
  1347. file_dialog->add_filter("*.tet");
  1348. file_dialog->set_current_path(EditorPaths::get_singleton()->get_text_editor_themes_dir().path_join(EDITOR_GET("text_editor/theme/color_theme")));
  1349. file_dialog->popup_file_dialog();
  1350. file_dialog->set_title(TTR("Save Theme As..."));
  1351. }
  1352. bool ScriptEditor::_has_docs_tab() const {
  1353. const int child_count = tab_container->get_tab_count();
  1354. for (int i = 0; i < child_count; i++) {
  1355. if (Object::cast_to<EditorHelp>(tab_container->get_tab_control(i))) {
  1356. return true;
  1357. }
  1358. }
  1359. return false;
  1360. }
  1361. bool ScriptEditor::_has_script_tab() const {
  1362. const int child_count = tab_container->get_tab_count();
  1363. for (int i = 0; i < child_count; i++) {
  1364. if (Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i))) {
  1365. return true;
  1366. }
  1367. }
  1368. return false;
  1369. }
  1370. void ScriptEditor::_prepare_file_menu() {
  1371. PopupMenu *menu = file_menu->get_popup();
  1372. const bool current_is_doc = _get_current_editor() == nullptr;
  1373. menu->set_item_disabled(menu->get_item_index(FILE_REOPEN_CLOSED), previous_scripts.is_empty());
  1374. menu->set_item_disabled(menu->get_item_index(FILE_SAVE), current_is_doc);
  1375. menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), current_is_doc);
  1376. menu->set_item_disabled(menu->get_item_index(FILE_SAVE_ALL), !_has_script_tab());
  1377. menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), current_is_doc);
  1378. menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), current_is_doc);
  1379. menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), current_is_doc);
  1380. menu->set_item_disabled(menu->get_item_index(WINDOW_PREV), history_pos <= 0);
  1381. menu->set_item_disabled(menu->get_item_index(WINDOW_NEXT), history_pos >= history.size() - 1);
  1382. menu->set_item_disabled(menu->get_item_index(FILE_CLOSE), tab_container->get_tab_count() < 1);
  1383. menu->set_item_disabled(menu->get_item_index(CLOSE_ALL), tab_container->get_tab_count() < 1);
  1384. menu->set_item_disabled(menu->get_item_index(CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
  1385. menu->set_item_disabled(menu->get_item_index(CLOSE_DOCS), !_has_docs_tab());
  1386. menu->set_item_disabled(menu->get_item_index(FILE_RUN), current_is_doc);
  1387. }
  1388. void ScriptEditor::_file_menu_closed() {
  1389. PopupMenu *menu = file_menu->get_popup();
  1390. menu->set_item_disabled(menu->get_item_index(FILE_REOPEN_CLOSED), false);
  1391. menu->set_item_disabled(menu->get_item_index(FILE_SAVE), false);
  1392. menu->set_item_disabled(menu->get_item_index(FILE_SAVE_AS), false);
  1393. menu->set_item_disabled(menu->get_item_index(FILE_SAVE_ALL), false);
  1394. menu->set_item_disabled(menu->get_item_index(FILE_TOOL_RELOAD_SOFT), false);
  1395. menu->set_item_disabled(menu->get_item_index(FILE_COPY_PATH), false);
  1396. menu->set_item_disabled(menu->get_item_index(SHOW_IN_FILE_SYSTEM), false);
  1397. menu->set_item_disabled(menu->get_item_index(WINDOW_PREV), false);
  1398. menu->set_item_disabled(menu->get_item_index(WINDOW_NEXT), false);
  1399. menu->set_item_disabled(menu->get_item_index(FILE_CLOSE), false);
  1400. menu->set_item_disabled(menu->get_item_index(CLOSE_ALL), false);
  1401. menu->set_item_disabled(menu->get_item_index(CLOSE_OTHER_TABS), false);
  1402. menu->set_item_disabled(menu->get_item_index(CLOSE_DOCS), false);
  1403. menu->set_item_disabled(menu->get_item_index(FILE_RUN), false);
  1404. }
  1405. void ScriptEditor::_tab_changed(int p_which) {
  1406. ensure_select_current();
  1407. }
  1408. void ScriptEditor::_notification(int p_what) {
  1409. switch (p_what) {
  1410. case NOTIFICATION_ENTER_TREE: {
  1411. EditorRunBar::get_singleton()->connect("stop_pressed", callable_mp(this, &ScriptEditor::_editor_stop));
  1412. _editor_settings_changed();
  1413. [[fallthrough]];
  1414. }
  1415. case NOTIFICATION_TRANSLATION_CHANGED:
  1416. case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
  1417. case NOTIFICATION_THEME_CHANGED: {
  1418. tab_container->add_theme_style_override("panel", get_theme_stylebox(SNAME("ScriptEditor"), EditorStringName(EditorStyles)));
  1419. help_search->set_icon(get_editor_theme_icon(SNAME("HelpSearch")));
  1420. site_search->set_icon(get_editor_theme_icon(SNAME("ExternalLink")));
  1421. if (is_layout_rtl()) {
  1422. script_forward->set_icon(get_editor_theme_icon(SNAME("Back")));
  1423. script_back->set_icon(get_editor_theme_icon(SNAME("Forward")));
  1424. } else {
  1425. script_forward->set_icon(get_editor_theme_icon(SNAME("Forward")));
  1426. script_back->set_icon(get_editor_theme_icon(SNAME("Back")));
  1427. }
  1428. members_overview_alphabeta_sort_button->set_icon(get_editor_theme_icon(SNAME("Sort")));
  1429. filter_scripts->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1430. filter_methods->set_right_icon(get_editor_theme_icon(SNAME("Search")));
  1431. filename->add_theme_style_override("normal", get_theme_stylebox(SNAME("normal"), SNAME("LineEdit")));
  1432. recent_scripts->reset_size();
  1433. if (is_inside_tree()) {
  1434. _update_script_colors();
  1435. _update_script_names();
  1436. }
  1437. } break;
  1438. case NOTIFICATION_READY: {
  1439. // Can't set own styles in NOTIFICATION_THEME_CHANGED, so for now this will do.
  1440. add_theme_style_override("panel", get_theme_stylebox(SNAME("ScriptEditorPanel"), SNAME("EditorStyles")));
  1441. get_tree()->connect("tree_changed", callable_mp(this, &ScriptEditor::_tree_changed));
  1442. InspectorDock::get_singleton()->connect("request_help", callable_mp(this, &ScriptEditor::_help_class_open));
  1443. EditorNode::get_singleton()->connect("request_help_search", callable_mp(this, &ScriptEditor::_help_search));
  1444. EditorNode::get_singleton()->connect("scene_closed", callable_mp(this, &ScriptEditor::_close_builtin_scripts_from_scene));
  1445. EditorNode::get_singleton()->connect("script_add_function_request", callable_mp(this, &ScriptEditor::_add_callback));
  1446. EditorNode::get_singleton()->connect("resource_saved", callable_mp(this, &ScriptEditor::_res_saved_callback));
  1447. EditorNode::get_singleton()->connect("scene_saved", callable_mp(this, &ScriptEditor::_scene_saved_callback));
  1448. FileSystemDock::get_singleton()->connect("files_moved", callable_mp(this, &ScriptEditor::_files_moved));
  1449. FileSystemDock::get_singleton()->connect("file_removed", callable_mp(this, &ScriptEditor::_file_removed));
  1450. script_list->connect("item_selected", callable_mp(this, &ScriptEditor::_script_selected));
  1451. members_overview->connect("item_selected", callable_mp(this, &ScriptEditor::_members_overview_selected));
  1452. help_overview->connect("item_selected", callable_mp(this, &ScriptEditor::_help_overview_selected));
  1453. script_split->connect("dragged", callable_mp(this, &ScriptEditor::_split_dragged));
  1454. list_split->connect("dragged", callable_mp(this, &ScriptEditor::_split_dragged));
  1455. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &ScriptEditor::_editor_settings_changed));
  1456. EditorFileSystem::get_singleton()->connect("filesystem_changed", callable_mp(this, &ScriptEditor::_filesystem_changed));
  1457. } break;
  1458. case NOTIFICATION_EXIT_TREE: {
  1459. EditorRunBar::get_singleton()->disconnect("stop_pressed", callable_mp(this, &ScriptEditor::_editor_stop));
  1460. } break;
  1461. case NOTIFICATION_APPLICATION_FOCUS_IN: {
  1462. _test_script_times_on_disk();
  1463. _update_modified_scripts_for_external_editor();
  1464. } break;
  1465. case CanvasItem::NOTIFICATION_VISIBILITY_CHANGED: {
  1466. if (is_visible()) {
  1467. find_in_files_button->show();
  1468. } else {
  1469. if (find_in_files->is_visible_in_tree()) {
  1470. EditorNode::get_bottom_panel()->hide_bottom_panel();
  1471. }
  1472. find_in_files_button->hide();
  1473. }
  1474. } break;
  1475. }
  1476. }
  1477. bool ScriptEditor::can_take_away_focus() const {
  1478. ScriptEditorBase *current = _get_current_editor();
  1479. if (current) {
  1480. return current->can_lose_focus_on_node_selection();
  1481. } else {
  1482. return true;
  1483. }
  1484. }
  1485. void ScriptEditor::_close_builtin_scripts_from_scene(const String &p_scene) {
  1486. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  1487. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  1488. if (se) {
  1489. Ref<Script> scr = se->get_edited_resource();
  1490. if (scr == nullptr || !scr.is_valid()) {
  1491. continue;
  1492. }
  1493. if (scr->is_built_in() && scr->get_path().begins_with(p_scene)) { // Is an internal script and belongs to scene being closed.
  1494. _close_tab(i, false);
  1495. i--;
  1496. }
  1497. }
  1498. }
  1499. }
  1500. void ScriptEditor::edited_scene_changed() {
  1501. _update_modified_scripts_for_external_editor();
  1502. }
  1503. void ScriptEditor::notify_script_close(const Ref<Script> &p_script) {
  1504. emit_signal(SNAME("script_close"), p_script);
  1505. }
  1506. void ScriptEditor::notify_script_changed(const Ref<Script> &p_script) {
  1507. emit_signal(SNAME("editor_script_changed"), p_script);
  1508. }
  1509. void ScriptEditor::get_breakpoints(List<String> *p_breakpoints) {
  1510. HashSet<String> loaded_scripts;
  1511. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  1512. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  1513. if (!se) {
  1514. continue;
  1515. }
  1516. Ref<Script> scr = se->get_edited_resource();
  1517. if (scr == nullptr) {
  1518. continue;
  1519. }
  1520. String base = scr->get_path();
  1521. loaded_scripts.insert(base);
  1522. if (base.begins_with("local://") || base.is_empty()) {
  1523. continue;
  1524. }
  1525. PackedInt32Array bpoints = se->get_breakpoints();
  1526. for (int j = 0; j < bpoints.size(); j++) {
  1527. p_breakpoints->push_back(base + ":" + itos((int)bpoints[j] + 1));
  1528. }
  1529. }
  1530. // Load breakpoints that are in closed scripts.
  1531. List<String> cached_editors;
  1532. script_editor_cache->get_sections(&cached_editors);
  1533. for (const String &E : cached_editors) {
  1534. if (loaded_scripts.has(E)) {
  1535. continue;
  1536. }
  1537. Array breakpoints = _get_cached_breakpoints_for_script(E);
  1538. for (int i = 0; i < breakpoints.size(); i++) {
  1539. p_breakpoints->push_back(E + ":" + itos((int)breakpoints[i] + 1));
  1540. }
  1541. }
  1542. }
  1543. void ScriptEditor::_members_overview_selected(int p_idx) {
  1544. ScriptEditorBase *se = _get_current_editor();
  1545. if (!se) {
  1546. return;
  1547. }
  1548. // Go to the member's line and reset the cursor column. We can't change scroll_position
  1549. // directly until we have gone to the line first, since code might be folded.
  1550. se->goto_line(members_overview->get_item_metadata(p_idx));
  1551. Dictionary state = se->get_edit_state();
  1552. state["column"] = 0;
  1553. state["scroll_position"] = members_overview->get_item_metadata(p_idx);
  1554. se->set_edit_state(state);
  1555. }
  1556. void ScriptEditor::_help_overview_selected(int p_idx) {
  1557. Node *current = tab_container->get_tab_control(tab_container->get_current_tab());
  1558. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1559. if (!se) {
  1560. return;
  1561. }
  1562. se->scroll_to_section(help_overview->get_item_metadata(p_idx));
  1563. }
  1564. void ScriptEditor::_script_selected(int p_idx) {
  1565. grab_focus_block = !Input::get_singleton()->is_mouse_button_pressed(MouseButton::LEFT); //amazing hack, simply amazing
  1566. _go_to_tab(script_list->get_item_metadata(p_idx));
  1567. grab_focus_block = false;
  1568. }
  1569. void ScriptEditor::ensure_select_current() {
  1570. if (tab_container->get_tab_count() && tab_container->get_current_tab() >= 0) {
  1571. ScriptEditorBase *se = _get_current_editor();
  1572. if (se) {
  1573. se->enable_editor(this);
  1574. if (!grab_focus_block && is_visible_in_tree()) {
  1575. se->ensure_focus();
  1576. }
  1577. }
  1578. }
  1579. _update_find_replace_bar();
  1580. _update_selected_editor_menu();
  1581. }
  1582. bool ScriptEditor::is_editor_floating() {
  1583. return is_floating;
  1584. }
  1585. void ScriptEditor::_find_scripts(Node *p_base, Node *p_current, HashSet<Ref<Script>> &used) {
  1586. if (p_current != p_base && p_current->get_owner() != p_base) {
  1587. return;
  1588. }
  1589. if (p_current->get_script_instance()) {
  1590. Ref<Script> scr = p_current->get_script();
  1591. if (scr.is_valid()) {
  1592. used.insert(scr);
  1593. }
  1594. }
  1595. for (int i = 0; i < p_current->get_child_count(); i++) {
  1596. _find_scripts(p_base, p_current->get_child(i), used);
  1597. }
  1598. }
  1599. struct _ScriptEditorItemData {
  1600. String name;
  1601. String sort_key;
  1602. Ref<Texture2D> icon;
  1603. bool tool = false;
  1604. int index = 0;
  1605. String tooltip;
  1606. bool used = false;
  1607. int category = 0;
  1608. Node *ref = nullptr;
  1609. bool operator<(const _ScriptEditorItemData &id) const {
  1610. if (category == id.category) {
  1611. if (sort_key == id.sort_key) {
  1612. return index < id.index;
  1613. } else {
  1614. return sort_key.naturalnocasecmp_to(id.sort_key) < 0;
  1615. }
  1616. } else {
  1617. return category < id.category;
  1618. }
  1619. }
  1620. };
  1621. void ScriptEditor::_update_members_overview_visibility() {
  1622. ScriptEditorBase *se = _get_current_editor();
  1623. if (!se) {
  1624. members_overview_alphabeta_sort_button->set_visible(false);
  1625. members_overview->set_visible(false);
  1626. overview_vbox->set_visible(false);
  1627. return;
  1628. }
  1629. if (members_overview_enabled && se->show_members_overview()) {
  1630. members_overview_alphabeta_sort_button->set_visible(true);
  1631. filter_methods->set_visible(true);
  1632. members_overview->set_visible(true);
  1633. overview_vbox->set_visible(true);
  1634. } else {
  1635. members_overview_alphabeta_sort_button->set_visible(false);
  1636. filter_methods->set_visible(false);
  1637. members_overview->set_visible(false);
  1638. overview_vbox->set_visible(false);
  1639. }
  1640. }
  1641. void ScriptEditor::_toggle_members_overview_alpha_sort(bool p_alphabetic_sort) {
  1642. EditorSettings::get_singleton()->set("text_editor/script_list/sort_members_outline_alphabetically", p_alphabetic_sort);
  1643. _update_members_overview();
  1644. }
  1645. void ScriptEditor::_update_members_overview() {
  1646. members_overview->clear();
  1647. ScriptEditorBase *se = _get_current_editor();
  1648. if (!se) {
  1649. return;
  1650. }
  1651. Vector<String> functions = se->get_functions();
  1652. if (EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically")) {
  1653. functions.sort();
  1654. }
  1655. for (int i = 0; i < functions.size(); i++) {
  1656. String filter = filter_methods->get_text();
  1657. String name = functions[i].get_slice(":", 0);
  1658. if (filter.is_empty() || filter.is_subsequence_ofn(name)) {
  1659. members_overview->add_item(name);
  1660. members_overview->set_item_metadata(-1, functions[i].get_slice(":", 1).to_int() - 1);
  1661. }
  1662. }
  1663. String path = se->get_edited_resource()->get_path();
  1664. bool built_in = !path.is_resource_file();
  1665. String name = built_in ? path.get_file() : se->get_name();
  1666. filename->set_text(name);
  1667. }
  1668. void ScriptEditor::_update_help_overview_visibility() {
  1669. int selected = tab_container->get_current_tab();
  1670. if (selected < 0 || selected >= tab_container->get_tab_count()) {
  1671. help_overview->set_visible(false);
  1672. return;
  1673. }
  1674. Node *current = tab_container->get_tab_control(tab_container->get_current_tab());
  1675. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1676. if (!se) {
  1677. help_overview->set_visible(false);
  1678. return;
  1679. }
  1680. if (help_overview_enabled) {
  1681. members_overview_alphabeta_sort_button->set_visible(false);
  1682. filter_methods->set_visible(false);
  1683. help_overview->set_visible(true);
  1684. overview_vbox->set_visible(true);
  1685. filename->set_text(se->get_name());
  1686. } else {
  1687. help_overview->set_visible(false);
  1688. overview_vbox->set_visible(false);
  1689. }
  1690. }
  1691. void ScriptEditor::_update_help_overview() {
  1692. help_overview->clear();
  1693. int selected = tab_container->get_current_tab();
  1694. if (selected < 0 || selected >= tab_container->get_tab_count()) {
  1695. return;
  1696. }
  1697. Node *current = tab_container->get_tab_control(tab_container->get_current_tab());
  1698. EditorHelp *se = Object::cast_to<EditorHelp>(current);
  1699. if (!se) {
  1700. return;
  1701. }
  1702. Vector<Pair<String, int>> sections = se->get_sections();
  1703. for (int i = 0; i < sections.size(); i++) {
  1704. help_overview->add_item(sections[i].first);
  1705. help_overview->set_item_metadata(i, sections[i].second);
  1706. }
  1707. }
  1708. void ScriptEditor::_update_script_colors() {
  1709. bool script_temperature_enabled = EDITOR_GET("text_editor/script_list/script_temperature_enabled");
  1710. int hist_size = EDITOR_GET("text_editor/script_list/script_temperature_history_size");
  1711. Color hot_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1712. hot_color.set_s(hot_color.get_s() * 0.9);
  1713. Color cold_color = get_theme_color(SNAME("font_color"), EditorStringName(Editor));
  1714. for (int i = 0; i < script_list->get_item_count(); i++) {
  1715. int c = script_list->get_item_metadata(i);
  1716. Node *n = tab_container->get_tab_control(c);
  1717. if (!n) {
  1718. continue;
  1719. }
  1720. script_list->set_item_custom_bg_color(i, Color(0, 0, 0, 0));
  1721. if (script_temperature_enabled) {
  1722. int pass = n->get_meta("__editor_pass", -1);
  1723. if (pass < 0) {
  1724. continue;
  1725. }
  1726. int h = edit_pass - pass;
  1727. if (h > hist_size) {
  1728. continue;
  1729. }
  1730. int non_zero_hist_size = (hist_size == 0) ? 1 : hist_size;
  1731. float v = Math::ease((edit_pass - pass) / float(non_zero_hist_size), 0.4);
  1732. script_list->set_item_custom_fg_color(i, hot_color.lerp(cold_color, v));
  1733. }
  1734. }
  1735. }
  1736. void ScriptEditor::_update_script_names() {
  1737. if (restoring_layout) {
  1738. return;
  1739. }
  1740. HashSet<Ref<Script>> used;
  1741. Node *edited = EditorNode::get_singleton()->get_edited_scene();
  1742. if (edited) {
  1743. _find_scripts(edited, edited, used);
  1744. }
  1745. script_list->clear();
  1746. bool split_script_help = EDITOR_GET("text_editor/script_list/group_help_pages");
  1747. ScriptSortBy sort_by = (ScriptSortBy)(int)EDITOR_GET("text_editor/script_list/sort_scripts_by");
  1748. ScriptListName display_as = (ScriptListName)(int)EDITOR_GET("text_editor/script_list/list_script_names_as");
  1749. Vector<_ScriptEditorItemData> sedata;
  1750. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  1751. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  1752. if (se) {
  1753. Ref<Texture2D> icon = se->get_theme_icon();
  1754. String path = se->get_edited_resource()->get_path();
  1755. bool saved = !path.is_empty();
  1756. if (saved) {
  1757. // The script might be deleted, moved, or renamed, so make sure
  1758. // to update original path to previously edited resource.
  1759. se->set_meta("_edit_res_path", path);
  1760. }
  1761. String name = se->get_name();
  1762. Ref<Script> scr = se->get_edited_resource();
  1763. _ScriptEditorItemData sd;
  1764. sd.icon = icon;
  1765. sd.name = name;
  1766. sd.tooltip = saved ? path : TTR("Unsaved file.");
  1767. sd.index = i;
  1768. sd.used = used.has(se->get_edited_resource());
  1769. sd.category = 0;
  1770. sd.ref = se;
  1771. if (scr.is_valid()) {
  1772. sd.tool = scr->is_tool();
  1773. }
  1774. switch (sort_by) {
  1775. case SORT_BY_NAME: {
  1776. sd.sort_key = name.to_lower();
  1777. } break;
  1778. case SORT_BY_PATH: {
  1779. sd.sort_key = path;
  1780. } break;
  1781. case SORT_BY_NONE: {
  1782. sd.sort_key = "";
  1783. } break;
  1784. }
  1785. switch (display_as) {
  1786. case DISPLAY_NAME: {
  1787. sd.name = name;
  1788. } break;
  1789. case DISPLAY_DIR_AND_NAME: {
  1790. if (!path.get_base_dir().get_file().is_empty()) {
  1791. sd.name = path.get_base_dir().get_file().path_join(name);
  1792. } else {
  1793. sd.name = name;
  1794. }
  1795. } break;
  1796. case DISPLAY_FULL_PATH: {
  1797. sd.name = path;
  1798. } break;
  1799. }
  1800. if (!saved) {
  1801. sd.name = se->get_name();
  1802. }
  1803. sedata.push_back(sd);
  1804. }
  1805. Vector<String> disambiguated_script_names;
  1806. Vector<String> full_script_paths;
  1807. for (int j = 0; j < sedata.size(); j++) {
  1808. String name = sedata[j].name.replace("(*)", "");
  1809. ScriptListName script_display = (ScriptListName)(int)EDITOR_GET("text_editor/script_list/list_script_names_as");
  1810. switch (script_display) {
  1811. case DISPLAY_NAME: {
  1812. name = name.get_file();
  1813. } break;
  1814. case DISPLAY_DIR_AND_NAME: {
  1815. name = name.get_base_dir().get_file().path_join(name.get_file());
  1816. } break;
  1817. default:
  1818. break;
  1819. }
  1820. disambiguated_script_names.append(name);
  1821. full_script_paths.append(sedata[j].tooltip);
  1822. }
  1823. EditorNode::disambiguate_filenames(full_script_paths, disambiguated_script_names);
  1824. for (int j = 0; j < sedata.size(); j++) {
  1825. if (sedata[j].name.ends_with("(*)")) {
  1826. sedata.write[j].name = disambiguated_script_names[j] + "(*)";
  1827. } else {
  1828. sedata.write[j].name = disambiguated_script_names[j];
  1829. }
  1830. }
  1831. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  1832. if (eh) {
  1833. String name = eh->get_class();
  1834. Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Help"));
  1835. String tooltip = vformat(TTR("%s Class Reference"), name);
  1836. _ScriptEditorItemData sd;
  1837. sd.icon = icon;
  1838. sd.name = name;
  1839. sd.sort_key = name.to_lower();
  1840. sd.tooltip = tooltip;
  1841. sd.index = i;
  1842. sd.used = false;
  1843. sd.category = split_script_help ? 1 : 0;
  1844. sd.ref = eh;
  1845. sedata.push_back(sd);
  1846. }
  1847. }
  1848. if (_sort_list_on_update && !sedata.is_empty()) {
  1849. sedata.sort();
  1850. // change actual order of tab_container so that the order can be rearranged by user
  1851. int cur_tab = tab_container->get_current_tab();
  1852. int prev_tab = tab_container->get_previous_tab();
  1853. int new_cur_tab = -1;
  1854. int new_prev_tab = -1;
  1855. for (int i = 0; i < sedata.size(); i++) {
  1856. tab_container->move_child(sedata[i].ref, i);
  1857. if (new_prev_tab == -1 && sedata[i].index == prev_tab) {
  1858. new_prev_tab = i;
  1859. }
  1860. if (new_cur_tab == -1 && sedata[i].index == cur_tab) {
  1861. new_cur_tab = i;
  1862. }
  1863. // Update index of sd entries for sorted order
  1864. _ScriptEditorItemData sd = sedata[i];
  1865. sd.index = i;
  1866. sedata.set(i, sd);
  1867. }
  1868. _go_to_tab(new_prev_tab);
  1869. _go_to_tab(new_cur_tab);
  1870. _sort_list_on_update = false;
  1871. }
  1872. Vector<_ScriptEditorItemData> sedata_filtered;
  1873. for (int i = 0; i < sedata.size(); i++) {
  1874. String filter = filter_scripts->get_text();
  1875. if (filter.is_empty() || filter.is_subsequence_ofn(sedata[i].name)) {
  1876. sedata_filtered.push_back(sedata[i]);
  1877. }
  1878. }
  1879. Color tool_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  1880. tool_color.set_s(tool_color.get_s() * 1.5);
  1881. for (int i = 0; i < sedata_filtered.size(); i++) {
  1882. script_list->add_item(sedata_filtered[i].name, sedata_filtered[i].icon);
  1883. if (sedata_filtered[i].tool) {
  1884. script_list->set_item_icon_modulate(-1, tool_color);
  1885. }
  1886. int index = script_list->get_item_count() - 1;
  1887. script_list->set_item_tooltip(index, sedata_filtered[i].tooltip);
  1888. script_list->set_item_metadata(index, sedata_filtered[i].index); /* Saving as metadata the script's index in the tab container and not the filtered one */
  1889. if (sedata_filtered[i].used) {
  1890. script_list->set_item_custom_bg_color(index, Color(88 / 255.0, 88 / 255.0, 60 / 255.0));
  1891. }
  1892. if (tab_container->get_current_tab() == sedata_filtered[i].index) {
  1893. script_list->select(index);
  1894. script_name_label->set_text(sedata_filtered[i].name);
  1895. script_icon->set_texture(sedata_filtered[i].icon);
  1896. ScriptEditorBase *se = _get_current_editor();
  1897. if (se) {
  1898. se->enable_editor(this);
  1899. _update_selected_editor_menu();
  1900. }
  1901. }
  1902. }
  1903. if (!waiting_update_names) {
  1904. _update_members_overview();
  1905. _update_help_overview();
  1906. } else {
  1907. waiting_update_names = false;
  1908. }
  1909. _update_members_overview_visibility();
  1910. _update_help_overview_visibility();
  1911. _update_script_colors();
  1912. }
  1913. Ref<TextFile> ScriptEditor::_load_text_file(const String &p_path, Error *r_error) const {
  1914. if (r_error) {
  1915. *r_error = ERR_FILE_CANT_OPEN;
  1916. }
  1917. String local_path = ProjectSettings::get_singleton()->localize_path(p_path);
  1918. String path = ResourceLoader::path_remap(local_path);
  1919. TextFile *text_file = memnew(TextFile);
  1920. Ref<TextFile> text_res(text_file);
  1921. Error err = text_file->load_text(path);
  1922. ERR_FAIL_COND_V_MSG(err != OK, Ref<Resource>(), "Cannot load text file '" + path + "'.");
  1923. text_file->set_file_path(local_path);
  1924. text_file->set_path(local_path, true);
  1925. if (ResourceLoader::get_timestamp_on_load()) {
  1926. text_file->set_last_modified_time(FileAccess::get_modified_time(path));
  1927. }
  1928. if (r_error) {
  1929. *r_error = OK;
  1930. }
  1931. return text_res;
  1932. }
  1933. Error ScriptEditor::_save_text_file(Ref<TextFile> p_text_file, const String &p_path) {
  1934. Ref<TextFile> sqscr = p_text_file;
  1935. ERR_FAIL_COND_V(sqscr.is_null(), ERR_INVALID_PARAMETER);
  1936. String source = sqscr->get_text();
  1937. Error err;
  1938. {
  1939. Ref<FileAccess> file = FileAccess::open(p_path, FileAccess::WRITE, &err);
  1940. ERR_FAIL_COND_V_MSG(err, err, "Cannot save text file '" + p_path + "'.");
  1941. file->store_string(source);
  1942. if (file->get_error() != OK && file->get_error() != ERR_FILE_EOF) {
  1943. return ERR_CANT_CREATE;
  1944. }
  1945. }
  1946. if (ResourceSaver::get_timestamp_on_save()) {
  1947. p_text_file->set_last_modified_time(FileAccess::get_modified_time(p_path));
  1948. }
  1949. EditorFileSystem::get_singleton()->update_file(p_path);
  1950. _res_saved_callback(sqscr);
  1951. return OK;
  1952. }
  1953. bool ScriptEditor::edit(const Ref<Resource> &p_resource, int p_line, int p_col, bool p_grab_focus) {
  1954. if (p_resource.is_null()) {
  1955. return false;
  1956. }
  1957. Ref<Script> scr = p_resource;
  1958. // Don't open dominant script if using an external editor.
  1959. bool use_external_editor =
  1960. external_editor_active ||
  1961. (scr.is_valid() && scr->get_language()->overrides_external_editor());
  1962. use_external_editor = use_external_editor && !(scr.is_valid() && scr->is_built_in()); // Ignore external editor for built-in scripts.
  1963. const bool open_dominant = EDITOR_GET("text_editor/behavior/files/open_dominant_script_on_scene_change");
  1964. const bool should_open = (open_dominant && !use_external_editor) || !EditorNode::get_singleton()->is_changing_scene();
  1965. if (scr.is_valid() && scr->get_language()->overrides_external_editor()) {
  1966. if (should_open) {
  1967. Error err = scr->get_language()->open_in_external_editor(scr, p_line >= 0 ? p_line : 0, p_col);
  1968. if (err != OK) {
  1969. ERR_PRINT("Couldn't open script in the overridden external text editor");
  1970. }
  1971. }
  1972. return false;
  1973. }
  1974. if (use_external_editor &&
  1975. (EditorDebuggerNode::get_singleton()->get_dump_stack_script() != p_resource || EditorDebuggerNode::get_singleton()->get_debug_with_external_editor()) &&
  1976. p_resource->get_path().is_resource_file()) {
  1977. String path = EDITOR_GET("text_editor/external/exec_path");
  1978. String flags = EDITOR_GET("text_editor/external/exec_flags");
  1979. List<String> args;
  1980. bool has_file_flag = false;
  1981. String script_path = ProjectSettings::get_singleton()->globalize_path(p_resource->get_path());
  1982. if (flags.size()) {
  1983. String project_path = ProjectSettings::get_singleton()->get_resource_path();
  1984. flags = flags.replacen("{line}", itos(p_line > 0 ? p_line : 0));
  1985. flags = flags.replacen("{col}", itos(p_col));
  1986. flags = flags.strip_edges().replace("\\\\", "\\");
  1987. int from = 0;
  1988. int num_chars = 0;
  1989. bool inside_quotes = false;
  1990. for (int i = 0; i < flags.size(); i++) {
  1991. if (flags[i] == '"' && (!i || flags[i - 1] != '\\')) {
  1992. if (!inside_quotes) {
  1993. from++;
  1994. }
  1995. inside_quotes = !inside_quotes;
  1996. } else if (flags[i] == '\0' || (!inside_quotes && flags[i] == ' ')) {
  1997. String arg = flags.substr(from, num_chars);
  1998. if (arg.contains("{file}")) {
  1999. has_file_flag = true;
  2000. }
  2001. // do path replacement here, else there will be issues with spaces and quotes
  2002. arg = arg.replacen("{project}", project_path);
  2003. arg = arg.replacen("{file}", script_path);
  2004. args.push_back(arg);
  2005. from = i + 1;
  2006. num_chars = 0;
  2007. } else {
  2008. num_chars++;
  2009. }
  2010. }
  2011. }
  2012. // Default to passing script path if no {file} flag is specified.
  2013. if (!has_file_flag) {
  2014. args.push_back(script_path);
  2015. }
  2016. if (!path.is_empty()) {
  2017. Error err = OS::get_singleton()->create_process(path, args);
  2018. if (err == OK) {
  2019. return false;
  2020. }
  2021. }
  2022. ERR_PRINT("Couldn't open external text editor, falling back to the internal editor. Review your `text_editor/external/` editor settings.");
  2023. }
  2024. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2025. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2026. if (!se) {
  2027. continue;
  2028. }
  2029. if ((scr != nullptr && se->get_edited_resource() == p_resource) || se->get_edited_resource()->get_path() == p_resource->get_path()) {
  2030. if (should_open) {
  2031. se->enable_editor(this);
  2032. if (tab_container->get_current_tab() != i) {
  2033. _go_to_tab(i);
  2034. }
  2035. if (is_visible_in_tree()) {
  2036. se->ensure_focus();
  2037. }
  2038. if (p_line > 0) {
  2039. se->goto_line(p_line);
  2040. }
  2041. }
  2042. _update_script_names();
  2043. script_list->ensure_current_is_visible();
  2044. return true;
  2045. }
  2046. }
  2047. // doesn't have it, make a new one
  2048. ScriptEditorBase *se = nullptr;
  2049. for (int i = script_editor_func_count - 1; i >= 0; i--) {
  2050. se = script_editor_funcs[i](p_resource);
  2051. if (se) {
  2052. break;
  2053. }
  2054. }
  2055. ERR_FAIL_NULL_V(se, false);
  2056. se->set_edited_resource(p_resource);
  2057. // Syntax highlighting.
  2058. bool highlighter_set = false;
  2059. for (int i = 0; i < syntax_highlighters.size(); i++) {
  2060. Ref<EditorSyntaxHighlighter> highlighter = syntax_highlighters[i]->_create();
  2061. if (highlighter.is_null()) {
  2062. continue;
  2063. }
  2064. se->add_syntax_highlighter(highlighter);
  2065. if (highlighter_set) {
  2066. continue;
  2067. }
  2068. PackedStringArray languages = highlighter->_get_supported_languages();
  2069. // If script try language, else use extension.
  2070. if (scr != nullptr) {
  2071. if (languages.has(scr->get_language()->get_name())) {
  2072. se->set_syntax_highlighter(highlighter);
  2073. highlighter_set = true;
  2074. }
  2075. continue;
  2076. }
  2077. if (languages.has(p_resource->get_path().get_extension())) {
  2078. se->set_syntax_highlighter(highlighter);
  2079. highlighter_set = true;
  2080. }
  2081. }
  2082. tab_container->add_child(se);
  2083. if (p_grab_focus) {
  2084. se->enable_editor(this);
  2085. }
  2086. // If we delete a script within the filesystem, the original resource path
  2087. // is lost, so keep it as metadata to figure out the exact tab to delete.
  2088. se->set_meta("_edit_res_path", p_resource->get_path());
  2089. se->set_tooltip_request_func(callable_mp(this, &ScriptEditor::_get_debug_tooltip));
  2090. if (se->get_edit_menu()) {
  2091. se->get_edit_menu()->hide();
  2092. menu_hb->add_child(se->get_edit_menu());
  2093. menu_hb->move_child(se->get_edit_menu(), 1);
  2094. }
  2095. if (p_grab_focus) {
  2096. _go_to_tab(tab_container->get_tab_count() - 1);
  2097. _add_recent_script(p_resource->get_path());
  2098. }
  2099. if (script_editor_cache->has_section(p_resource->get_path())) {
  2100. se->set_edit_state(script_editor_cache->get_value(p_resource->get_path(), "state"));
  2101. }
  2102. _sort_list_on_update = true;
  2103. _update_script_names();
  2104. _save_layout();
  2105. se->connect("name_changed", callable_mp(this, &ScriptEditor::_update_script_names));
  2106. se->connect("edited_script_changed", callable_mp(this, &ScriptEditor::_script_changed));
  2107. se->connect("request_help", callable_mp(this, &ScriptEditor::_help_search));
  2108. se->connect("request_open_script_at_line", callable_mp(this, &ScriptEditor::_goto_script_line));
  2109. se->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
  2110. se->connect("request_save_history", callable_mp(this, &ScriptEditor::_save_history));
  2111. se->connect("search_in_files_requested", callable_mp(this, &ScriptEditor::_on_find_in_files_requested));
  2112. se->connect("replace_in_files_requested", callable_mp(this, &ScriptEditor::_on_replace_in_files_requested));
  2113. se->connect("go_to_method", callable_mp(this, &ScriptEditor::script_goto_method));
  2114. //test for modification, maybe the script was not edited but was loaded
  2115. _test_script_times_on_disk(p_resource);
  2116. _update_modified_scripts_for_external_editor(p_resource);
  2117. if (p_line >= 0) {
  2118. se->goto_line(p_line);
  2119. }
  2120. notify_script_changed(p_resource);
  2121. return true;
  2122. }
  2123. PackedStringArray ScriptEditor::get_unsaved_scripts() const {
  2124. PackedStringArray unsaved_list;
  2125. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2126. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2127. if (se && se->is_unsaved()) {
  2128. unsaved_list.append(se->get_name());
  2129. }
  2130. }
  2131. return unsaved_list;
  2132. }
  2133. void ScriptEditor::save_current_script() {
  2134. ScriptEditorBase *current = _get_current_editor();
  2135. if (!current || _test_script_times_on_disk()) {
  2136. return;
  2137. }
  2138. if (trim_trailing_whitespace_on_save) {
  2139. current->trim_trailing_whitespace();
  2140. }
  2141. current->insert_final_newline();
  2142. if (convert_indent_on_save) {
  2143. current->convert_indent();
  2144. }
  2145. Ref<Resource> resource = current->get_edited_resource();
  2146. Ref<TextFile> text_file = resource;
  2147. Ref<Script> scr = resource;
  2148. if (text_file != nullptr) {
  2149. current->apply_code();
  2150. _save_text_file(text_file, text_file->get_path());
  2151. return;
  2152. }
  2153. if (scr.is_valid()) {
  2154. clear_docs_from_script(scr);
  2155. }
  2156. EditorNode::get_singleton()->save_resource(resource);
  2157. if (scr.is_valid()) {
  2158. update_docs_from_script(scr);
  2159. }
  2160. }
  2161. void ScriptEditor::save_all_scripts() {
  2162. HashSet<String> scenes_to_save;
  2163. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2164. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2165. if (!se) {
  2166. continue;
  2167. }
  2168. if (convert_indent_on_save) {
  2169. se->convert_indent();
  2170. }
  2171. if (trim_trailing_whitespace_on_save) {
  2172. se->trim_trailing_whitespace();
  2173. }
  2174. se->insert_final_newline();
  2175. if (!se->is_unsaved()) {
  2176. continue;
  2177. }
  2178. Ref<Resource> edited_res = se->get_edited_resource();
  2179. if (edited_res.is_valid()) {
  2180. se->apply_code();
  2181. }
  2182. if (!edited_res->is_built_in()) {
  2183. Ref<TextFile> text_file = edited_res;
  2184. Ref<Script> scr = edited_res;
  2185. if (text_file != nullptr) {
  2186. _save_text_file(text_file, text_file->get_path());
  2187. continue;
  2188. }
  2189. if (scr.is_valid()) {
  2190. clear_docs_from_script(scr);
  2191. }
  2192. EditorNode::get_singleton()->save_resource(edited_res); //external script, save it
  2193. if (scr.is_valid()) {
  2194. update_docs_from_script(scr);
  2195. }
  2196. } else {
  2197. // For built-in scripts, save their scenes instead.
  2198. const String scene_path = edited_res->get_path().get_slice("::", 0);
  2199. if (!scene_path.is_empty() && !scenes_to_save.has(scene_path)) {
  2200. scenes_to_save.insert(scene_path);
  2201. }
  2202. }
  2203. }
  2204. if (!scenes_to_save.is_empty()) {
  2205. EditorNode::get_singleton()->save_scene_list(scenes_to_save);
  2206. }
  2207. _update_script_names();
  2208. }
  2209. void ScriptEditor::apply_scripts() const {
  2210. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2211. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2212. if (!se) {
  2213. continue;
  2214. }
  2215. se->apply_code();
  2216. }
  2217. }
  2218. void ScriptEditor::reload_scripts(bool p_refresh_only) {
  2219. if (external_editor_active) {
  2220. return;
  2221. }
  2222. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2223. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2224. if (!se) {
  2225. continue;
  2226. }
  2227. Ref<Resource> edited_res = se->get_edited_resource();
  2228. if (edited_res->is_built_in()) {
  2229. continue; //internal script, who cares
  2230. }
  2231. if (!p_refresh_only) {
  2232. uint64_t last_date = edited_res->get_last_modified_time();
  2233. uint64_t date = FileAccess::get_modified_time(edited_res->get_path());
  2234. if (last_date == date) {
  2235. continue;
  2236. }
  2237. Ref<Script> scr = edited_res;
  2238. if (scr.is_valid()) {
  2239. Ref<Script> rel_scr = ResourceLoader::load(scr->get_path(), scr->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  2240. ERR_CONTINUE(!rel_scr.is_valid());
  2241. scr->set_source_code(rel_scr->get_source_code());
  2242. scr->set_last_modified_time(rel_scr->get_last_modified_time());
  2243. scr->reload(true);
  2244. }
  2245. Ref<JSON> json = edited_res;
  2246. if (json != nullptr) {
  2247. Ref<JSON> rel_json = ResourceLoader::load(json->get_path(), json->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  2248. ERR_CONTINUE(!rel_json.is_valid());
  2249. json->parse(rel_json->get_parsed_text(), true);
  2250. json->set_last_modified_time(rel_json->get_last_modified_time());
  2251. }
  2252. Ref<TextFile> text_file = edited_res;
  2253. if (text_file.is_valid()) {
  2254. text_file->reload_from_file();
  2255. }
  2256. }
  2257. se->reload_text();
  2258. }
  2259. disk_changed->hide();
  2260. _update_script_names();
  2261. }
  2262. void ScriptEditor::open_script_create_dialog(const String &p_base_name, const String &p_base_path) {
  2263. _menu_option(FILE_NEW);
  2264. script_create_dialog->config(p_base_name, p_base_path);
  2265. }
  2266. void ScriptEditor::open_text_file_create_dialog(const String &p_base_path, const String &p_base_name) {
  2267. _menu_option(FILE_NEW_TEXTFILE);
  2268. file_dialog->set_current_dir(p_base_path);
  2269. file_dialog->set_current_file(p_base_name);
  2270. open_textfile_after_create = false;
  2271. }
  2272. Ref<Resource> ScriptEditor::open_file(const String &p_file) {
  2273. List<String> extensions;
  2274. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  2275. ResourceLoader::get_recognized_extensions_for_type("JSON", &extensions);
  2276. if (extensions.find(p_file.get_extension())) {
  2277. Ref<Resource> scr = ResourceLoader::load(p_file);
  2278. if (!scr.is_valid()) {
  2279. EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
  2280. return Ref<Resource>();
  2281. }
  2282. edit(scr);
  2283. return scr;
  2284. }
  2285. Error error;
  2286. Ref<TextFile> text_file = _load_text_file(p_file, &error);
  2287. if (error != OK) {
  2288. EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + p_file, TTR("Error!"));
  2289. return Ref<Resource>();
  2290. }
  2291. if (text_file.is_valid()) {
  2292. edit(text_file);
  2293. return text_file;
  2294. }
  2295. return Ref<Resource>();
  2296. }
  2297. void ScriptEditor::_editor_stop() {
  2298. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2299. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2300. if (!se) {
  2301. continue;
  2302. }
  2303. se->set_debugger_active(false);
  2304. }
  2305. }
  2306. void ScriptEditor::_add_callback(Object *p_obj, const String &p_function, const PackedStringArray &p_args) {
  2307. ERR_FAIL_NULL(p_obj);
  2308. Ref<Script> scr = p_obj->get_script();
  2309. ERR_FAIL_COND(!scr.is_valid());
  2310. if (!scr->get_language()->can_make_function()) {
  2311. return;
  2312. }
  2313. EditorNode::get_singleton()->push_item(scr.ptr());
  2314. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2315. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2316. if (!se) {
  2317. continue;
  2318. }
  2319. if (se->get_edited_resource() != scr) {
  2320. continue;
  2321. }
  2322. se->add_callback(p_function, p_args);
  2323. _go_to_tab(i);
  2324. script_list->select(script_list->find_metadata(i));
  2325. // Save the current script so the changes can be picked up by an external editor.
  2326. if (!scr.ptr()->is_built_in()) { // But only if it's not built-in script.
  2327. save_current_script();
  2328. }
  2329. break;
  2330. }
  2331. }
  2332. void ScriptEditor::_save_editor_state(ScriptEditorBase *p_editor) {
  2333. if (restoring_layout) {
  2334. return;
  2335. }
  2336. const String &path = p_editor->get_edited_resource()->get_path();
  2337. if (!path.is_resource_file()) {
  2338. return;
  2339. }
  2340. script_editor_cache->set_value(path, "state", p_editor->get_edit_state());
  2341. // This is saved later when we save the editor layout.
  2342. }
  2343. void ScriptEditor::_save_layout() {
  2344. if (restoring_layout) {
  2345. return;
  2346. }
  2347. EditorNode::get_singleton()->save_editor_layout_delayed();
  2348. }
  2349. void ScriptEditor::_editor_settings_changed() {
  2350. if (!EditorSettings::get_singleton()->check_changed_settings_in_group("text_editor") &&
  2351. !EditorSettings::get_singleton()->check_changed_settings_in_group("docks/filesystem")) {
  2352. return;
  2353. }
  2354. textfile_extensions.clear();
  2355. const Vector<String> textfile_ext = ((String)(EDITOR_GET("docks/filesystem/textfile_extensions"))).split(",", false);
  2356. for (const String &E : textfile_ext) {
  2357. textfile_extensions.insert(E);
  2358. }
  2359. trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save");
  2360. convert_indent_on_save = EDITOR_GET("text_editor/behavior/files/convert_indent_on_save");
  2361. members_overview_enabled = EDITOR_GET("text_editor/script_list/show_members_overview");
  2362. help_overview_enabled = EDITOR_GET("text_editor/help/show_help_index");
  2363. external_editor_active = EDITOR_GET("text_editor/external/use_external_editor");
  2364. _update_members_overview_visibility();
  2365. _update_help_overview_visibility();
  2366. _update_autosave_timer();
  2367. if (current_theme.is_empty()) {
  2368. current_theme = EDITOR_GET("text_editor/theme/color_theme");
  2369. } else if (current_theme != String(EDITOR_GET("text_editor/theme/color_theme"))) {
  2370. current_theme = EDITOR_GET("text_editor/theme/color_theme");
  2371. EditorSettings::get_singleton()->load_text_editor_theme();
  2372. }
  2373. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2374. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2375. if (!se) {
  2376. continue;
  2377. }
  2378. se->update_settings();
  2379. }
  2380. _update_script_colors();
  2381. _update_script_names();
  2382. ScriptServer::set_reload_scripts_on_save(EDITOR_GET("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save"));
  2383. }
  2384. void ScriptEditor::_filesystem_changed() {
  2385. _update_script_names();
  2386. }
  2387. void ScriptEditor::_files_moved(const String &p_old_file, const String &p_new_file) {
  2388. if (!script_editor_cache->has_section(p_old_file)) {
  2389. return;
  2390. }
  2391. Variant state = script_editor_cache->get_value(p_old_file, "state");
  2392. script_editor_cache->erase_section(p_old_file);
  2393. script_editor_cache->set_value(p_new_file, "state", state);
  2394. // If Script, update breakpoints with debugger.
  2395. Array breakpoints = _get_cached_breakpoints_for_script(p_new_file);
  2396. for (int i = 0; i < breakpoints.size(); i++) {
  2397. int line = (int)breakpoints[i] + 1;
  2398. EditorDebuggerNode::get_singleton()->set_breakpoint(p_old_file, line, false);
  2399. if (!p_new_file.begins_with("local://") && ResourceLoader::exists(p_new_file, "Script")) {
  2400. EditorDebuggerNode::get_singleton()->set_breakpoint(p_new_file, line, true);
  2401. }
  2402. }
  2403. // This is saved later when we save the editor layout.
  2404. }
  2405. void ScriptEditor::_file_removed(const String &p_removed_file) {
  2406. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2407. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2408. if (!se) {
  2409. continue;
  2410. }
  2411. if (se->get_meta("_edit_res_path") == p_removed_file) {
  2412. // The script is deleted with no undo, so just close the tab.
  2413. _close_tab(i, false, false);
  2414. }
  2415. }
  2416. // Check closed.
  2417. if (script_editor_cache->has_section(p_removed_file)) {
  2418. Array breakpoints = _get_cached_breakpoints_for_script(p_removed_file);
  2419. for (int i = 0; i < breakpoints.size(); i++) {
  2420. EditorDebuggerNode::get_singleton()->set_breakpoint(p_removed_file, (int)breakpoints[i] + 1, false);
  2421. }
  2422. script_editor_cache->erase_section(p_removed_file);
  2423. }
  2424. }
  2425. void ScriptEditor::_update_find_replace_bar() {
  2426. ScriptEditorBase *se = _get_current_editor();
  2427. if (se) {
  2428. se->set_find_replace_bar(find_replace_bar);
  2429. } else {
  2430. find_replace_bar->set_text_edit(nullptr);
  2431. find_replace_bar->hide();
  2432. }
  2433. }
  2434. void ScriptEditor::_autosave_scripts() {
  2435. save_all_scripts();
  2436. }
  2437. void ScriptEditor::_update_autosave_timer() {
  2438. if (!autosave_timer->is_inside_tree()) {
  2439. return;
  2440. }
  2441. float autosave_time = EDITOR_GET("text_editor/behavior/files/autosave_interval_secs");
  2442. if (autosave_time > 0) {
  2443. autosave_timer->set_wait_time(autosave_time);
  2444. autosave_timer->start();
  2445. } else {
  2446. autosave_timer->stop();
  2447. }
  2448. }
  2449. void ScriptEditor::_tree_changed() {
  2450. if (waiting_update_names) {
  2451. return;
  2452. }
  2453. waiting_update_names = true;
  2454. callable_mp(this, &ScriptEditor::_update_script_names).call_deferred();
  2455. }
  2456. void ScriptEditor::_split_dragged(float) {
  2457. _save_layout();
  2458. }
  2459. Variant ScriptEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  2460. if (tab_container->get_tab_count() == 0) {
  2461. return Variant();
  2462. }
  2463. Node *cur_node = tab_container->get_tab_control(tab_container->get_current_tab());
  2464. HBoxContainer *drag_preview = memnew(HBoxContainer);
  2465. String preview_name = "";
  2466. Ref<Texture2D> preview_icon;
  2467. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(cur_node);
  2468. if (se) {
  2469. preview_name = se->get_name();
  2470. preview_icon = se->get_theme_icon();
  2471. }
  2472. EditorHelp *eh = Object::cast_to<EditorHelp>(cur_node);
  2473. if (eh) {
  2474. preview_name = eh->get_class();
  2475. preview_icon = get_editor_theme_icon(SNAME("Help"));
  2476. }
  2477. if (!preview_icon.is_null()) {
  2478. TextureRect *tf = memnew(TextureRect);
  2479. tf->set_texture(preview_icon);
  2480. tf->set_stretch_mode(TextureRect::STRETCH_KEEP_CENTERED);
  2481. drag_preview->add_child(tf);
  2482. }
  2483. Label *label = memnew(Label(preview_name));
  2484. drag_preview->add_child(label);
  2485. set_drag_preview(drag_preview);
  2486. Dictionary drag_data;
  2487. drag_data["type"] = "script_list_element"; // using a custom type because node caused problems when dragging to scene tree
  2488. drag_data["script_list_element"] = cur_node;
  2489. return drag_data;
  2490. }
  2491. bool ScriptEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2492. Dictionary d = p_data;
  2493. if (!d.has("type")) {
  2494. return false;
  2495. }
  2496. if (String(d["type"]) == "script_list_element") {
  2497. Node *node = Object::cast_to<Node>(d["script_list_element"]);
  2498. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  2499. if (se) {
  2500. return true;
  2501. }
  2502. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  2503. if (eh) {
  2504. return true;
  2505. }
  2506. }
  2507. if (String(d["type"]) == "nodes") {
  2508. Array nodes = d["nodes"];
  2509. if (nodes.size() == 0) {
  2510. return false;
  2511. }
  2512. Node *node = get_node((nodes[0]));
  2513. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  2514. if (se) {
  2515. return true;
  2516. }
  2517. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  2518. if (eh) {
  2519. return true;
  2520. }
  2521. }
  2522. if (String(d["type"]) == "files") {
  2523. Vector<String> files = d["files"];
  2524. if (files.size() == 0) {
  2525. return false; //weird
  2526. }
  2527. for (int i = 0; i < files.size(); i++) {
  2528. const String &file = files[i];
  2529. if (file.is_empty() || !FileAccess::exists(file)) {
  2530. continue;
  2531. }
  2532. if (ResourceLoader::exists(file, "Script") || ResourceLoader::exists(file, "JSON")) {
  2533. Ref<Resource> scr = ResourceLoader::load(file);
  2534. if (scr.is_valid()) {
  2535. return true;
  2536. }
  2537. }
  2538. if (textfile_extensions.has(file.get_extension())) {
  2539. Error err;
  2540. Ref<TextFile> text_file = _load_text_file(file, &err);
  2541. if (text_file.is_valid() && err == OK) {
  2542. return true;
  2543. }
  2544. }
  2545. }
  2546. return false;
  2547. }
  2548. return false;
  2549. }
  2550. void ScriptEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2551. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  2552. return;
  2553. }
  2554. Dictionary d = p_data;
  2555. if (!d.has("type")) {
  2556. return;
  2557. }
  2558. if (String(d["type"]) == "script_list_element") {
  2559. Node *node = Object::cast_to<Node>(d["script_list_element"]);
  2560. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  2561. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  2562. if (se || eh) {
  2563. int new_index = 0;
  2564. if (script_list->get_item_count() > 0) {
  2565. new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  2566. }
  2567. tab_container->move_child(node, new_index);
  2568. tab_container->set_current_tab(new_index);
  2569. _update_script_names();
  2570. }
  2571. }
  2572. if (String(d["type"]) == "nodes") {
  2573. Array nodes = d["nodes"];
  2574. if (nodes.size() == 0) {
  2575. return;
  2576. }
  2577. Node *node = get_node(nodes[0]);
  2578. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(node);
  2579. EditorHelp *eh = Object::cast_to<EditorHelp>(node);
  2580. if (se || eh) {
  2581. int new_index = 0;
  2582. if (script_list->get_item_count() > 0) {
  2583. new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  2584. }
  2585. tab_container->move_child(node, new_index);
  2586. tab_container->set_current_tab(new_index);
  2587. _update_script_names();
  2588. }
  2589. }
  2590. if (String(d["type"]) == "files") {
  2591. Vector<String> files = d["files"];
  2592. int new_index = 0;
  2593. if (script_list->get_item_count() > 0) {
  2594. new_index = script_list->get_item_metadata(script_list->get_item_at_position(p_point));
  2595. }
  2596. int num_tabs_before = tab_container->get_tab_count();
  2597. for (int i = 0; i < files.size(); i++) {
  2598. const String &file = files[i];
  2599. if (file.is_empty() || !FileAccess::exists(file)) {
  2600. continue;
  2601. }
  2602. if (!ResourceLoader::exists(file, "Script") && !ResourceLoader::exists(file, "JSON") && !textfile_extensions.has(file.get_extension())) {
  2603. continue;
  2604. }
  2605. Ref<Resource> res = open_file(file);
  2606. if (res.is_valid()) {
  2607. const int num_tabs = tab_container->get_tab_count();
  2608. if (num_tabs > num_tabs_before) {
  2609. tab_container->move_child(tab_container->get_tab_control(tab_container->get_tab_count() - 1), new_index);
  2610. num_tabs_before = num_tabs;
  2611. } else if (num_tabs > 0) { /* Maybe script was already open */
  2612. tab_container->move_child(tab_container->get_tab_control(tab_container->get_current_tab()), new_index);
  2613. }
  2614. }
  2615. }
  2616. if (tab_container->get_tab_count() > 0) {
  2617. tab_container->set_current_tab(new_index);
  2618. }
  2619. _update_script_names();
  2620. }
  2621. }
  2622. void ScriptEditor::input(const Ref<InputEvent> &p_event) {
  2623. // This is implemented in `input()` rather than `unhandled_input()` to allow
  2624. // the shortcut to be used regardless of the click location.
  2625. // This feature can be disabled to avoid interfering with other uses of the additional
  2626. // mouse buttons, such as push-to-talk in a VoIP program.
  2627. if (EDITOR_GET("interface/editor/mouse_extra_buttons_navigate_history")) {
  2628. const Ref<InputEventMouseButton> mb = p_event;
  2629. // Navigate the script history using additional mouse buttons present on some mice.
  2630. // This must be hardcoded as the editor shortcuts dialog doesn't allow assigning
  2631. // more than one shortcut per action.
  2632. if (mb.is_valid() && mb->is_pressed() && is_visible_in_tree()) {
  2633. if (mb->get_button_index() == MouseButton::MB_XBUTTON1) {
  2634. _history_back();
  2635. }
  2636. if (mb->get_button_index() == MouseButton::MB_XBUTTON2) {
  2637. _history_forward();
  2638. }
  2639. }
  2640. }
  2641. }
  2642. void ScriptEditor::shortcut_input(const Ref<InputEvent> &p_event) {
  2643. ERR_FAIL_COND(p_event.is_null());
  2644. if (!is_visible_in_tree() || !p_event->is_pressed() || p_event->is_echo()) {
  2645. return;
  2646. }
  2647. if (ED_IS_SHORTCUT("script_editor/next_script", p_event)) {
  2648. if (script_list->get_item_count() > 1) {
  2649. int next_tab = script_list->get_current() + 1;
  2650. next_tab %= script_list->get_item_count();
  2651. _go_to_tab(script_list->get_item_metadata(next_tab));
  2652. _update_script_names();
  2653. }
  2654. accept_event();
  2655. }
  2656. if (ED_IS_SHORTCUT("script_editor/prev_script", p_event)) {
  2657. if (script_list->get_item_count() > 1) {
  2658. int next_tab = script_list->get_current() - 1;
  2659. next_tab = next_tab >= 0 ? next_tab : script_list->get_item_count() - 1;
  2660. _go_to_tab(script_list->get_item_metadata(next_tab));
  2661. _update_script_names();
  2662. }
  2663. accept_event();
  2664. }
  2665. if (ED_IS_SHORTCUT("script_editor/window_move_up", p_event)) {
  2666. _menu_option(WINDOW_MOVE_UP);
  2667. accept_event();
  2668. }
  2669. if (ED_IS_SHORTCUT("script_editor/window_move_down", p_event)) {
  2670. _menu_option(WINDOW_MOVE_DOWN);
  2671. accept_event();
  2672. }
  2673. }
  2674. void ScriptEditor::_script_list_clicked(int p_item, Vector2 p_local_mouse_pos, MouseButton p_mouse_button_index) {
  2675. if (p_mouse_button_index == MouseButton::MIDDLE) {
  2676. script_list->select(p_item);
  2677. _script_selected(p_item);
  2678. _menu_option(FILE_CLOSE);
  2679. }
  2680. if (p_mouse_button_index == MouseButton::RIGHT) {
  2681. _make_script_list_context_menu();
  2682. }
  2683. }
  2684. void ScriptEditor::_make_script_list_context_menu() {
  2685. context_menu->clear();
  2686. int selected = tab_container->get_current_tab();
  2687. if (selected < 0 || selected >= tab_container->get_tab_count()) {
  2688. return;
  2689. }
  2690. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(selected));
  2691. if (se) {
  2692. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save"), FILE_SAVE);
  2693. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/save_as"), FILE_SAVE_AS);
  2694. }
  2695. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_file"), FILE_CLOSE);
  2696. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_all"), CLOSE_ALL);
  2697. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_other_tabs"), CLOSE_OTHER_TABS);
  2698. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/close_docs"), CLOSE_DOCS);
  2699. context_menu->add_separator();
  2700. if (se) {
  2701. Ref<Script> scr = se->get_edited_resource();
  2702. if (scr != nullptr) {
  2703. if (!scr.is_null() && scr->is_tool()) {
  2704. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/reload_script_soft"), FILE_TOOL_RELOAD_SOFT);
  2705. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/run_file"), FILE_RUN);
  2706. context_menu->add_separator();
  2707. }
  2708. }
  2709. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/copy_path"), FILE_COPY_PATH);
  2710. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/show_in_file_system"), SHOW_IN_FILE_SYSTEM);
  2711. context_menu->add_separator();
  2712. }
  2713. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_up"), WINDOW_MOVE_UP);
  2714. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_move_down"), WINDOW_MOVE_DOWN);
  2715. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/window_sort"), WINDOW_SORT);
  2716. context_menu->add_shortcut(ED_GET_SHORTCUT("script_editor/toggle_scripts_panel"), TOGGLE_SCRIPTS_PANEL);
  2717. context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_ALL), tab_container->get_tab_count() <= 0);
  2718. context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_OTHER_TABS), tab_container->get_tab_count() <= 1);
  2719. context_menu->set_item_disabled(context_menu->get_item_index(CLOSE_DOCS), !_has_docs_tab());
  2720. context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_MOVE_UP), tab_container->get_current_tab() <= 0);
  2721. context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_MOVE_DOWN), tab_container->get_current_tab() >= tab_container->get_tab_count() - 1);
  2722. context_menu->set_item_disabled(context_menu->get_item_index(WINDOW_SORT), tab_container->get_tab_count() <= 1);
  2723. context_menu->set_position(get_screen_position() + get_local_mouse_position());
  2724. context_menu->reset_size();
  2725. context_menu->popup();
  2726. }
  2727. void ScriptEditor::set_window_layout(Ref<ConfigFile> p_layout) {
  2728. if (!bool(EDITOR_GET("text_editor/behavior/files/restore_scripts_on_load"))) {
  2729. return;
  2730. }
  2731. if (!p_layout->has_section_key("ScriptEditor", "open_scripts") && !p_layout->has_section_key("ScriptEditor", "open_help")) {
  2732. return;
  2733. }
  2734. Array scripts = p_layout->get_value("ScriptEditor", "open_scripts");
  2735. Array helps;
  2736. if (p_layout->has_section_key("ScriptEditor", "open_help")) {
  2737. helps = p_layout->get_value("ScriptEditor", "open_help");
  2738. }
  2739. restoring_layout = true;
  2740. HashSet<String> loaded_scripts;
  2741. List<String> extensions;
  2742. ResourceLoader::get_recognized_extensions_for_type("Script", &extensions);
  2743. ResourceLoader::get_recognized_extensions_for_type("JSON", &extensions);
  2744. for (int i = 0; i < scripts.size(); i++) {
  2745. String path = scripts[i];
  2746. Dictionary script_info = scripts[i];
  2747. if (!script_info.is_empty()) {
  2748. path = script_info["path"];
  2749. }
  2750. if (!FileAccess::exists(path)) {
  2751. if (script_editor_cache->has_section(path)) {
  2752. script_editor_cache->erase_section(path);
  2753. }
  2754. continue;
  2755. }
  2756. loaded_scripts.insert(path);
  2757. if (extensions.find(path.get_extension())) {
  2758. Ref<Resource> scr = ResourceLoader::load(path);
  2759. if (!scr.is_valid()) {
  2760. continue;
  2761. }
  2762. if (!edit(scr, false)) {
  2763. continue;
  2764. }
  2765. } else {
  2766. Error error;
  2767. Ref<TextFile> text_file = _load_text_file(path, &error);
  2768. if (error != OK || !text_file.is_valid()) {
  2769. continue;
  2770. }
  2771. if (!edit(text_file, false)) {
  2772. continue;
  2773. }
  2774. }
  2775. if (!script_info.is_empty()) {
  2776. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(tab_container->get_tab_count() - 1));
  2777. if (se) {
  2778. se->set_edit_state(script_info["state"]);
  2779. }
  2780. }
  2781. }
  2782. for (int i = 0; i < helps.size(); i++) {
  2783. String path = helps[i];
  2784. if (path.is_empty()) { // invalid, skip
  2785. continue;
  2786. }
  2787. _help_class_open(path);
  2788. }
  2789. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2790. tab_container->get_tab_control(i)->set_meta("__editor_pass", Variant());
  2791. }
  2792. if (p_layout->has_section_key("ScriptEditor", "script_split_offset")) {
  2793. script_split->set_split_offset(p_layout->get_value("ScriptEditor", "script_split_offset"));
  2794. }
  2795. if (p_layout->has_section_key("ScriptEditor", "list_split_offset")) {
  2796. list_split->set_split_offset(p_layout->get_value("ScriptEditor", "list_split_offset"));
  2797. }
  2798. // Remove any deleted editors that have been removed between launches.
  2799. // and if a Script, register breakpoints with the debugger.
  2800. List<String> cached_editors;
  2801. script_editor_cache->get_sections(&cached_editors);
  2802. for (const String &E : cached_editors) {
  2803. if (loaded_scripts.has(E)) {
  2804. continue;
  2805. }
  2806. if (!FileAccess::exists(E)) {
  2807. script_editor_cache->erase_section(E);
  2808. continue;
  2809. }
  2810. Array breakpoints = _get_cached_breakpoints_for_script(E);
  2811. for (int i = 0; i < breakpoints.size(); i++) {
  2812. EditorDebuggerNode::get_singleton()->set_breakpoint(E, (int)breakpoints[i] + 1, true);
  2813. }
  2814. }
  2815. restoring_layout = false;
  2816. _update_script_names();
  2817. if (p_layout->has_section_key("ScriptEditor", "selected_script")) {
  2818. String selected_script = p_layout->get_value("ScriptEditor", "selected_script");
  2819. // If the selected script is not in the list of open scripts, select nothing.
  2820. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2821. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2822. if (se && se->get_edited_resource()->get_path() == selected_script) {
  2823. _go_to_tab(i);
  2824. break;
  2825. }
  2826. }
  2827. }
  2828. }
  2829. void ScriptEditor::get_window_layout(Ref<ConfigFile> p_layout) {
  2830. Array scripts;
  2831. Array helps;
  2832. String selected_script;
  2833. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2834. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2835. if (se) {
  2836. String path = se->get_edited_resource()->get_path();
  2837. if (!path.is_resource_file()) {
  2838. continue;
  2839. }
  2840. if (tab_container->get_current_tab_control() == tab_container->get_tab_control(i)) {
  2841. selected_script = path;
  2842. }
  2843. _save_editor_state(se);
  2844. scripts.push_back(path);
  2845. }
  2846. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  2847. if (eh) {
  2848. helps.push_back(eh->get_class());
  2849. }
  2850. }
  2851. p_layout->set_value("ScriptEditor", "open_scripts", scripts);
  2852. p_layout->set_value("ScriptEditor", "selected_script", selected_script);
  2853. p_layout->set_value("ScriptEditor", "open_help", helps);
  2854. p_layout->set_value("ScriptEditor", "script_split_offset", script_split->get_split_offset());
  2855. p_layout->set_value("ScriptEditor", "list_split_offset", list_split->get_split_offset());
  2856. // Save the cache.
  2857. script_editor_cache->save(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg"));
  2858. }
  2859. void ScriptEditor::_help_class_open(const String &p_class) {
  2860. if (p_class.is_empty()) {
  2861. return;
  2862. }
  2863. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2864. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  2865. if (eh && eh->get_class() == p_class) {
  2866. _go_to_tab(i);
  2867. _update_script_names();
  2868. return;
  2869. }
  2870. }
  2871. EditorHelp *eh = memnew(EditorHelp);
  2872. eh->set_name(p_class);
  2873. tab_container->add_child(eh);
  2874. _go_to_tab(tab_container->get_tab_count() - 1);
  2875. eh->go_to_class(p_class);
  2876. eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
  2877. _add_recent_script(p_class);
  2878. _sort_list_on_update = true;
  2879. _update_script_names();
  2880. _save_layout();
  2881. }
  2882. void ScriptEditor::_help_class_goto(const String &p_desc) {
  2883. String cname = p_desc.get_slice(":", 1);
  2884. if (_help_tab_goto(cname, p_desc)) {
  2885. return;
  2886. }
  2887. EditorHelp *eh = memnew(EditorHelp);
  2888. eh->set_name(cname);
  2889. tab_container->add_child(eh);
  2890. eh->go_to_help(p_desc);
  2891. eh->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
  2892. _add_recent_script(eh->get_class());
  2893. _sort_list_on_update = true;
  2894. _update_script_names();
  2895. _save_layout();
  2896. callable_mp(this, &ScriptEditor::_help_tab_goto).call_deferred(cname, p_desc);
  2897. }
  2898. bool ScriptEditor::_help_tab_goto(const String &p_name, const String &p_desc) {
  2899. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2900. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  2901. if (eh && eh->get_class() == p_name) {
  2902. _go_to_tab(i);
  2903. eh->go_to_help(p_desc);
  2904. _update_script_names();
  2905. return true;
  2906. }
  2907. }
  2908. return false;
  2909. }
  2910. void ScriptEditor::update_doc(const String &p_name) {
  2911. ERR_FAIL_COND(!EditorHelp::get_doc_data()->has_doc(p_name));
  2912. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2913. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_tab_control(i));
  2914. if (eh && eh->get_class() == p_name) {
  2915. eh->update_doc();
  2916. return;
  2917. }
  2918. }
  2919. }
  2920. void ScriptEditor::clear_docs_from_script(const Ref<Script> &p_script) {
  2921. ERR_FAIL_COND(p_script.is_null());
  2922. Vector<DocData::ClassDoc> documentations = p_script->get_documentation();
  2923. for (int j = 0; j < documentations.size(); j++) {
  2924. const DocData::ClassDoc &doc = documentations.get(j);
  2925. if (EditorHelp::get_doc_data()->has_doc(doc.name)) {
  2926. EditorHelp::get_doc_data()->remove_doc(doc.name);
  2927. }
  2928. }
  2929. }
  2930. void ScriptEditor::update_docs_from_script(const Ref<Script> &p_script) {
  2931. ERR_FAIL_COND(p_script.is_null());
  2932. Vector<DocData::ClassDoc> documentations = p_script->get_documentation();
  2933. for (int j = 0; j < documentations.size(); j++) {
  2934. const DocData::ClassDoc &doc = documentations.get(j);
  2935. EditorHelp::get_doc_data()->add_doc(doc);
  2936. update_doc(doc.name);
  2937. }
  2938. }
  2939. void ScriptEditor::_update_selected_editor_menu() {
  2940. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  2941. bool current = tab_container->get_current_tab() == i;
  2942. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  2943. if (se && se->get_edit_menu()) {
  2944. if (current) {
  2945. se->get_edit_menu()->show();
  2946. } else {
  2947. se->get_edit_menu()->hide();
  2948. }
  2949. }
  2950. }
  2951. EditorHelp *eh = Object::cast_to<EditorHelp>(tab_container->get_current_tab_control());
  2952. script_search_menu->get_popup()->clear();
  2953. if (eh) {
  2954. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find", TTR("Find..."), KeyModifierMask::CMD_OR_CTRL | Key::F), HELP_SEARCH_FIND);
  2955. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_next", TTR("Find Next"), Key::F3), HELP_SEARCH_FIND_NEXT);
  2956. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_previous", TTR("Find Previous"), KeyModifierMask::SHIFT | Key::F3), HELP_SEARCH_FIND_PREVIOUS);
  2957. script_search_menu->get_popup()->add_separator();
  2958. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files", TTR("Find in Files"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F), SEARCH_IN_FILES);
  2959. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace_in_files", TTR("Replace in Files"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R), REPLACE_IN_FILES);
  2960. script_search_menu->show();
  2961. } else {
  2962. if (tab_container->get_tab_count() == 0) {
  2963. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/find_in_files", TTR("Find in Files"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F), SEARCH_IN_FILES);
  2964. script_search_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/replace_in_files", TTR("Replace in Files"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R), REPLACE_IN_FILES);
  2965. script_search_menu->show();
  2966. } else {
  2967. script_search_menu->hide();
  2968. }
  2969. }
  2970. }
  2971. void ScriptEditor::_update_history_pos(int p_new_pos) {
  2972. Node *n = tab_container->get_current_tab_control();
  2973. if (Object::cast_to<ScriptEditorBase>(n)) {
  2974. history.write[history_pos].state = Object::cast_to<ScriptEditorBase>(n)->get_navigation_state();
  2975. }
  2976. if (Object::cast_to<EditorHelp>(n)) {
  2977. history.write[history_pos].state = Object::cast_to<EditorHelp>(n)->get_scroll();
  2978. }
  2979. history_pos = p_new_pos;
  2980. tab_container->set_current_tab(tab_container->get_tab_idx_from_control(history[history_pos].control));
  2981. n = history[history_pos].control;
  2982. ScriptEditorBase *seb = Object::cast_to<ScriptEditorBase>(n);
  2983. if (seb) {
  2984. seb->set_edit_state(history[history_pos].state);
  2985. seb->ensure_focus();
  2986. Ref<Script> scr = seb->get_edited_resource();
  2987. if (scr != nullptr) {
  2988. notify_script_changed(scr);
  2989. }
  2990. }
  2991. if (Object::cast_to<EditorHelp>(n)) {
  2992. Object::cast_to<EditorHelp>(n)->set_scroll(history[history_pos].state);
  2993. Object::cast_to<EditorHelp>(n)->set_focused();
  2994. }
  2995. n->set_meta("__editor_pass", ++edit_pass);
  2996. _update_script_names();
  2997. _update_history_arrows();
  2998. _update_selected_editor_menu();
  2999. }
  3000. void ScriptEditor::_history_forward() {
  3001. if (history_pos < history.size() - 1) {
  3002. _update_history_pos(history_pos + 1);
  3003. }
  3004. }
  3005. void ScriptEditor::_history_back() {
  3006. if (history_pos > 0) {
  3007. _update_history_pos(history_pos - 1);
  3008. }
  3009. }
  3010. Vector<Ref<Script>> ScriptEditor::get_open_scripts() const {
  3011. Vector<Ref<Script>> out_scripts = Vector<Ref<Script>>();
  3012. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  3013. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  3014. if (!se) {
  3015. continue;
  3016. }
  3017. Ref<Script> scr = se->get_edited_resource();
  3018. if (scr != nullptr) {
  3019. out_scripts.push_back(scr);
  3020. }
  3021. }
  3022. return out_scripts;
  3023. }
  3024. TypedArray<ScriptEditorBase> ScriptEditor::_get_open_script_editors() const {
  3025. TypedArray<ScriptEditorBase> script_editors;
  3026. for (int i = 0; i < tab_container->get_tab_count(); i++) {
  3027. ScriptEditorBase *se = Object::cast_to<ScriptEditorBase>(tab_container->get_tab_control(i));
  3028. if (!se) {
  3029. continue;
  3030. }
  3031. script_editors.push_back(se);
  3032. }
  3033. return script_editors;
  3034. }
  3035. void ScriptEditor::set_scene_root_script(Ref<Script> p_script) {
  3036. // Don't open dominant script if using an external editor.
  3037. bool use_external_editor =
  3038. external_editor_active ||
  3039. (p_script.is_valid() && p_script->get_language()->overrides_external_editor());
  3040. use_external_editor = use_external_editor && !(p_script.is_valid() && p_script->is_built_in()); // Ignore external editor for built-in scripts.
  3041. const bool open_dominant = EDITOR_GET("text_editor/behavior/files/open_dominant_script_on_scene_change");
  3042. if (open_dominant && !use_external_editor && p_script.is_valid()) {
  3043. edit(p_script);
  3044. }
  3045. }
  3046. bool ScriptEditor::script_goto_method(Ref<Script> p_script, const String &p_method) {
  3047. int line = p_script->get_member_line(p_method);
  3048. if (line == -1) {
  3049. return false;
  3050. }
  3051. return edit(p_script, line, 0);
  3052. }
  3053. void ScriptEditor::set_live_auto_reload_running_scripts(bool p_enabled) {
  3054. auto_reload_running_scripts = p_enabled;
  3055. }
  3056. void ScriptEditor::_help_search(String p_text) {
  3057. help_search_dialog->popup_dialog(p_text);
  3058. }
  3059. void ScriptEditor::_open_script_request(const String &p_path) {
  3060. Ref<Script> scr = ResourceLoader::load(p_path);
  3061. if (scr.is_valid()) {
  3062. script_editor->edit(scr, false);
  3063. return;
  3064. }
  3065. Ref<JSON> json = ResourceLoader::load(p_path);
  3066. if (json.is_valid()) {
  3067. script_editor->edit(json, false);
  3068. return;
  3069. }
  3070. Error err;
  3071. Ref<TextFile> text_file = script_editor->_load_text_file(p_path, &err);
  3072. if (text_file.is_valid()) {
  3073. script_editor->edit(text_file, false);
  3074. return;
  3075. }
  3076. }
  3077. void ScriptEditor::register_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) {
  3078. ERR_FAIL_COND(p_syntax_highlighter.is_null());
  3079. if (!syntax_highlighters.has(p_syntax_highlighter)) {
  3080. syntax_highlighters.push_back(p_syntax_highlighter);
  3081. }
  3082. }
  3083. void ScriptEditor::unregister_syntax_highlighter(const Ref<EditorSyntaxHighlighter> &p_syntax_highlighter) {
  3084. ERR_FAIL_COND(p_syntax_highlighter.is_null());
  3085. syntax_highlighters.erase(p_syntax_highlighter);
  3086. }
  3087. int ScriptEditor::script_editor_func_count = 0;
  3088. CreateScriptEditorFunc ScriptEditor::script_editor_funcs[ScriptEditor::SCRIPT_EDITOR_FUNC_MAX];
  3089. void ScriptEditor::register_create_script_editor_function(CreateScriptEditorFunc p_func) {
  3090. ERR_FAIL_COND(script_editor_func_count == SCRIPT_EDITOR_FUNC_MAX);
  3091. script_editor_funcs[script_editor_func_count++] = p_func;
  3092. }
  3093. void ScriptEditor::_script_changed() {
  3094. NodeDock::get_singleton()->update_lists();
  3095. }
  3096. void ScriptEditor::_on_find_in_files_requested(String text) {
  3097. find_in_files_dialog->set_find_in_files_mode(FindInFilesDialog::SEARCH_MODE);
  3098. find_in_files_dialog->set_search_text(text);
  3099. find_in_files_dialog->popup_centered();
  3100. }
  3101. void ScriptEditor::_on_replace_in_files_requested(String text) {
  3102. find_in_files_dialog->set_find_in_files_mode(FindInFilesDialog::REPLACE_MODE);
  3103. find_in_files_dialog->set_search_text(text);
  3104. find_in_files_dialog->set_replace_text("");
  3105. find_in_files_dialog->popup_centered();
  3106. }
  3107. void ScriptEditor::_on_find_in_files_result_selected(String fpath, int line_number, int begin, int end) {
  3108. if (ResourceLoader::exists(fpath)) {
  3109. Ref<Resource> res = ResourceLoader::load(fpath);
  3110. if (fpath.get_extension() == "gdshader") {
  3111. ShaderEditorPlugin *shader_editor = Object::cast_to<ShaderEditorPlugin>(EditorNode::get_editor_data().get_editor_by_name("Shader"));
  3112. shader_editor->edit(res.ptr());
  3113. shader_editor->make_visible(true);
  3114. shader_editor->get_shader_editor(res)->goto_line_selection(line_number - 1, begin, end);
  3115. return;
  3116. } else if (fpath.get_extension() == "tscn") {
  3117. Ref<FileAccess> f = FileAccess::open(fpath, FileAccess::READ);
  3118. bool is_script_found = false;
  3119. // Starting from top of the tscn file.
  3120. int scr_start_line = 1;
  3121. String scr_header = "[sub_resource type=\"GDScript\" id=\"";
  3122. String scr_id = "";
  3123. String line = "";
  3124. int l = 0;
  3125. while (!f->eof_reached()) {
  3126. line = f->get_line();
  3127. l++;
  3128. if (!line.begins_with(scr_header)) {
  3129. continue;
  3130. }
  3131. // Found the end of the script.
  3132. scr_id = line.get_slice(scr_header, 1);
  3133. scr_id = scr_id.get_slice("\"", 0);
  3134. scr_start_line = l + 1;
  3135. int scr_line_count = 0;
  3136. do {
  3137. line = f->get_line();
  3138. l++;
  3139. String strline = line.strip_edges();
  3140. if (strline.ends_with("\"") && !strline.ends_with("\\\"")) {
  3141. // Found the end of script.
  3142. break;
  3143. }
  3144. scr_line_count++;
  3145. } while (!f->eof_reached());
  3146. if (line_number > scr_start_line + scr_line_count) {
  3147. // Find in another built-in GDScript.
  3148. continue;
  3149. }
  3150. // Real line number of the built-in script.
  3151. line_number = line_number - scr_start_line;
  3152. is_script_found = true;
  3153. break;
  3154. }
  3155. EditorNode::get_singleton()->load_scene(fpath);
  3156. if (is_script_found && !scr_id.is_empty()) {
  3157. Ref<Script> scr = ResourceLoader::load(fpath + "::" + scr_id, "Script");
  3158. if (scr.is_valid()) {
  3159. edit(scr);
  3160. ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor());
  3161. if (ste) {
  3162. ste->goto_line_selection(line_number, begin, end);
  3163. }
  3164. }
  3165. }
  3166. return;
  3167. } else {
  3168. Ref<Script> scr = res;
  3169. Ref<JSON> json = res;
  3170. if (scr.is_valid() || json.is_valid()) {
  3171. edit(scr);
  3172. ScriptTextEditor *ste = Object::cast_to<ScriptTextEditor>(_get_current_editor());
  3173. if (ste) {
  3174. ste->goto_line_selection(line_number - 1, begin, end);
  3175. }
  3176. return;
  3177. }
  3178. }
  3179. }
  3180. // If the file is not a valid resource/script, load it as a text file.
  3181. Error err;
  3182. Ref<TextFile> text_file = _load_text_file(fpath, &err);
  3183. if (text_file.is_valid()) {
  3184. edit(text_file);
  3185. TextEditor *te = Object::cast_to<TextEditor>(_get_current_editor());
  3186. if (te) {
  3187. te->goto_line_selection(line_number - 1, begin, end);
  3188. }
  3189. }
  3190. }
  3191. void ScriptEditor::_start_find_in_files(bool with_replace) {
  3192. FindInFiles *f = find_in_files->get_finder();
  3193. f->set_search_text(find_in_files_dialog->get_search_text());
  3194. f->set_match_case(find_in_files_dialog->is_match_case());
  3195. f->set_whole_words(find_in_files_dialog->is_whole_words());
  3196. f->set_folder(find_in_files_dialog->get_folder());
  3197. f->set_filter(find_in_files_dialog->get_filter());
  3198. find_in_files->set_with_replace(with_replace);
  3199. find_in_files->set_replace_text(find_in_files_dialog->get_replace_text());
  3200. find_in_files->start_search();
  3201. EditorNode::get_bottom_panel()->make_item_visible(find_in_files);
  3202. }
  3203. void ScriptEditor::_on_find_in_files_modified_files(PackedStringArray paths) {
  3204. _test_script_times_on_disk();
  3205. _update_modified_scripts_for_external_editor();
  3206. }
  3207. void ScriptEditor::_window_changed(bool p_visible) {
  3208. make_floating->set_visible(!p_visible);
  3209. is_floating = p_visible;
  3210. }
  3211. void ScriptEditor::_filter_scripts_text_changed(const String &p_newtext) {
  3212. _update_script_names();
  3213. }
  3214. void ScriptEditor::_filter_methods_text_changed(const String &p_newtext) {
  3215. _update_members_overview();
  3216. }
  3217. void ScriptEditor::_bind_methods() {
  3218. ClassDB::bind_method("_help_tab_goto", &ScriptEditor::_help_tab_goto);
  3219. ClassDB::bind_method("get_current_editor", &ScriptEditor::_get_current_editor);
  3220. ClassDB::bind_method("get_open_script_editors", &ScriptEditor::_get_open_script_editors);
  3221. ClassDB::bind_method(D_METHOD("register_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::register_syntax_highlighter);
  3222. ClassDB::bind_method(D_METHOD("unregister_syntax_highlighter", "syntax_highlighter"), &ScriptEditor::unregister_syntax_highlighter);
  3223. ClassDB::bind_method(D_METHOD("goto_line", "line_number"), &ScriptEditor::_goto_script_line2);
  3224. ClassDB::bind_method(D_METHOD("get_current_script"), &ScriptEditor::_get_current_script);
  3225. ClassDB::bind_method(D_METHOD("get_open_scripts"), &ScriptEditor::_get_open_scripts);
  3226. ClassDB::bind_method(D_METHOD("open_script_create_dialog", "base_name", "base_path"), &ScriptEditor::open_script_create_dialog);
  3227. ADD_SIGNAL(MethodInfo("editor_script_changed", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  3228. ADD_SIGNAL(MethodInfo("script_close", PropertyInfo(Variant::OBJECT, "script", PROPERTY_HINT_RESOURCE_TYPE, "Script")));
  3229. }
  3230. ScriptEditor::ScriptEditor(WindowWrapper *p_wrapper) {
  3231. window_wrapper = p_wrapper;
  3232. current_theme = "";
  3233. script_editor_cache.instantiate();
  3234. script_editor_cache->load(EditorPaths::get_singleton()->get_project_settings_dir().path_join("script_editor_cache.cfg"));
  3235. completion_cache = memnew(EditorScriptCodeCompletionCache);
  3236. restoring_layout = false;
  3237. waiting_update_names = false;
  3238. pending_auto_reload = false;
  3239. auto_reload_running_scripts = true;
  3240. external_editor_active = false;
  3241. members_overview_enabled = EDITOR_GET("text_editor/script_list/show_members_overview");
  3242. help_overview_enabled = EDITOR_GET("text_editor/help/show_help_index");
  3243. VBoxContainer *main_container = memnew(VBoxContainer);
  3244. add_child(main_container);
  3245. menu_hb = memnew(HBoxContainer);
  3246. main_container->add_child(menu_hb);
  3247. script_split = memnew(HSplitContainer);
  3248. main_container->add_child(script_split);
  3249. script_split->set_v_size_flags(SIZE_EXPAND_FILL);
  3250. list_split = memnew(VSplitContainer);
  3251. script_split->add_child(list_split);
  3252. list_split->set_v_size_flags(SIZE_EXPAND_FILL);
  3253. scripts_vbox = memnew(VBoxContainer);
  3254. scripts_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  3255. list_split->add_child(scripts_vbox);
  3256. filter_scripts = memnew(LineEdit);
  3257. filter_scripts->set_placeholder(TTR("Filter Scripts"));
  3258. filter_scripts->set_clear_button_enabled(true);
  3259. filter_scripts->connect("text_changed", callable_mp(this, &ScriptEditor::_filter_scripts_text_changed));
  3260. scripts_vbox->add_child(filter_scripts);
  3261. script_list = memnew(ItemList);
  3262. script_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  3263. scripts_vbox->add_child(script_list);
  3264. script_list->set_custom_minimum_size(Size2(150, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  3265. script_list->set_v_size_flags(SIZE_EXPAND_FILL);
  3266. script_split->set_split_offset(70 * EDSCALE);
  3267. _sort_list_on_update = true;
  3268. script_list->connect("item_clicked", callable_mp(this, &ScriptEditor::_script_list_clicked), CONNECT_DEFERRED);
  3269. script_list->set_allow_rmb_select(true);
  3270. SET_DRAG_FORWARDING_GCD(script_list, ScriptEditor);
  3271. context_menu = memnew(PopupMenu);
  3272. add_child(context_menu);
  3273. context_menu->connect("id_pressed", callable_mp(this, &ScriptEditor::_menu_option));
  3274. overview_vbox = memnew(VBoxContainer);
  3275. overview_vbox->set_custom_minimum_size(Size2(0, 90));
  3276. overview_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
  3277. list_split->add_child(overview_vbox);
  3278. list_split->set_visible(EditorSettings::get_singleton()->get_project_metadata("scripts_panel", "show_scripts_panel", true));
  3279. buttons_hbox = memnew(HBoxContainer);
  3280. overview_vbox->add_child(buttons_hbox);
  3281. filename = memnew(Label);
  3282. filename->set_clip_text(true);
  3283. filename->set_h_size_flags(SIZE_EXPAND_FILL);
  3284. filename->add_theme_style_override("normal", EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SNAME("normal"), SNAME("LineEdit")));
  3285. buttons_hbox->add_child(filename);
  3286. members_overview_alphabeta_sort_button = memnew(Button);
  3287. members_overview_alphabeta_sort_button->set_flat(true);
  3288. members_overview_alphabeta_sort_button->set_tooltip_text(TTR("Toggle alphabetical sorting of the method list."));
  3289. members_overview_alphabeta_sort_button->set_toggle_mode(true);
  3290. members_overview_alphabeta_sort_button->set_pressed(EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically"));
  3291. members_overview_alphabeta_sort_button->connect("toggled", callable_mp(this, &ScriptEditor::_toggle_members_overview_alpha_sort));
  3292. buttons_hbox->add_child(members_overview_alphabeta_sort_button);
  3293. filter_methods = memnew(LineEdit);
  3294. filter_methods->set_placeholder(TTR("Filter Methods"));
  3295. filter_methods->set_clear_button_enabled(true);
  3296. filter_methods->connect("text_changed", callable_mp(this, &ScriptEditor::_filter_methods_text_changed));
  3297. overview_vbox->add_child(filter_methods);
  3298. members_overview = memnew(ItemList);
  3299. members_overview->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  3300. overview_vbox->add_child(members_overview);
  3301. members_overview->set_allow_reselect(true);
  3302. members_overview->set_custom_minimum_size(Size2(0, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  3303. members_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  3304. members_overview->set_allow_rmb_select(true);
  3305. help_overview = memnew(ItemList);
  3306. help_overview->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  3307. overview_vbox->add_child(help_overview);
  3308. help_overview->set_allow_reselect(true);
  3309. help_overview->set_custom_minimum_size(Size2(0, 60) * EDSCALE); //need to give a bit of limit to avoid it from disappearing
  3310. help_overview->set_v_size_flags(SIZE_EXPAND_FILL);
  3311. VBoxContainer *code_editor_container = memnew(VBoxContainer);
  3312. script_split->add_child(code_editor_container);
  3313. tab_container = memnew(TabContainer);
  3314. tab_container->set_tabs_visible(false);
  3315. tab_container->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  3316. code_editor_container->add_child(tab_container);
  3317. tab_container->set_h_size_flags(SIZE_EXPAND_FILL);
  3318. tab_container->set_v_size_flags(SIZE_EXPAND_FILL);
  3319. find_replace_bar = memnew(FindReplaceBar);
  3320. code_editor_container->add_child(find_replace_bar);
  3321. find_replace_bar->hide();
  3322. ED_SHORTCUT("script_editor/window_sort", TTR("Sort"));
  3323. ED_SHORTCUT("script_editor/window_move_up", TTR("Move Up"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::UP);
  3324. ED_SHORTCUT("script_editor/window_move_down", TTR("Move Down"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::DOWN);
  3325. // FIXME: These should be `Key::GREATER` and `Key::LESS` but those don't work.
  3326. ED_SHORTCUT("script_editor/next_script", TTR("Next Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::PERIOD);
  3327. ED_SHORTCUT("script_editor/prev_script", TTR("Previous Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::COMMA);
  3328. set_process_input(true);
  3329. set_process_shortcut_input(true);
  3330. file_menu = memnew(MenuButton);
  3331. file_menu->set_text(TTR("File"));
  3332. file_menu->set_switch_on_hover(true);
  3333. file_menu->set_shortcut_context(this);
  3334. menu_hb->add_child(file_menu);
  3335. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new", TTR("New Script..."), KeyModifierMask::CMD_OR_CTRL | Key::N), FILE_NEW);
  3336. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/new_textfile", TTR("New Text File..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::N), FILE_NEW_TEXTFILE);
  3337. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/open", TTR("Open...")), FILE_OPEN);
  3338. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reopen_closed_script", TTR("Reopen Closed Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::T), FILE_REOPEN_CLOSED);
  3339. file_menu->get_popup()->add_submenu_item(TTR("Open Recent"), "RecentScripts", FILE_OPEN_RECENT);
  3340. recent_scripts = memnew(PopupMenu);
  3341. recent_scripts->set_name("RecentScripts");
  3342. file_menu->get_popup()->add_child(recent_scripts);
  3343. recent_scripts->connect("id_pressed", callable_mp(this, &ScriptEditor::_open_recent_script));
  3344. _update_recent_scripts();
  3345. file_menu->get_popup()->add_separator();
  3346. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save", TTR("Save"), KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::S), FILE_SAVE);
  3347. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_as", TTR("Save As...")), FILE_SAVE_AS);
  3348. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/save_all", TTR("Save All"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::S), FILE_SAVE_ALL);
  3349. ED_SHORTCUT_OVERRIDE("script_editor/save_all", "macos", KeyModifierMask::META | KeyModifierMask::CTRL | Key::S);
  3350. file_menu->get_popup()->add_separator();
  3351. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/reload_script_soft", TTR("Soft Reload Tool Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::R), FILE_TOOL_RELOAD_SOFT);
  3352. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/copy_path", TTR("Copy Script Path")), FILE_COPY_PATH);
  3353. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/show_in_file_system", TTR("Show in FileSystem")), SHOW_IN_FILE_SYSTEM);
  3354. file_menu->get_popup()->add_separator();
  3355. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_previous", TTR("History Previous"), KeyModifierMask::ALT | Key::LEFT), WINDOW_PREV);
  3356. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/history_next", TTR("History Next"), KeyModifierMask::ALT | Key::RIGHT), WINDOW_NEXT);
  3357. file_menu->get_popup()->add_separator();
  3358. file_menu->get_popup()->add_submenu_item(TTR("Theme"), "Theme", FILE_THEME);
  3359. theme_submenu = memnew(PopupMenu);
  3360. theme_submenu->set_name("Theme");
  3361. file_menu->get_popup()->add_child(theme_submenu);
  3362. theme_submenu->connect("id_pressed", callable_mp(this, &ScriptEditor::_theme_option));
  3363. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/import_theme", TTR("Import Theme...")), THEME_IMPORT);
  3364. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/reload_theme", TTR("Reload Theme")), THEME_RELOAD);
  3365. theme_submenu->add_separator();
  3366. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme", TTR("Save Theme")), THEME_SAVE);
  3367. theme_submenu->add_shortcut(ED_SHORTCUT("script_editor/save_theme_as", TTR("Save Theme As...")), THEME_SAVE_AS);
  3368. file_menu->get_popup()->add_separator();
  3369. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_file", TTR("Close"), KeyModifierMask::CMD_OR_CTRL | Key::W), FILE_CLOSE);
  3370. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_all", TTR("Close All")), CLOSE_ALL);
  3371. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_other_tabs", TTR("Close Other Tabs")), CLOSE_OTHER_TABS);
  3372. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/close_docs", TTR("Close Docs")), CLOSE_DOCS);
  3373. file_menu->get_popup()->add_separator();
  3374. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/run_file", TTR("Run"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::X), FILE_RUN);
  3375. file_menu->get_popup()->add_separator();
  3376. file_menu->get_popup()->add_shortcut(ED_SHORTCUT("script_editor/toggle_scripts_panel", TTR("Toggle Scripts Panel"), KeyModifierMask::CMD_OR_CTRL | Key::BACKSLASH), TOGGLE_SCRIPTS_PANEL);
  3377. file_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptEditor::_menu_option));
  3378. file_menu->get_popup()->connect("about_to_popup", callable_mp(this, &ScriptEditor::_prepare_file_menu));
  3379. file_menu->get_popup()->connect("popup_hide", callable_mp(this, &ScriptEditor::_file_menu_closed));
  3380. script_search_menu = memnew(MenuButton);
  3381. script_search_menu->set_text(TTR("Search"));
  3382. script_search_menu->set_switch_on_hover(true);
  3383. script_search_menu->set_shortcut_context(this);
  3384. script_search_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptEditor::_menu_option));
  3385. menu_hb->add_child(script_search_menu);
  3386. MenuButton *debug_menu_btn = memnew(MenuButton);
  3387. menu_hb->add_child(debug_menu_btn);
  3388. debug_menu_btn->hide(); // Handled by EditorDebuggerNode below.
  3389. EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
  3390. debugger->set_script_debug_button(debug_menu_btn);
  3391. debugger->connect("goto_script_line", callable_mp(this, &ScriptEditor::_goto_script_line));
  3392. debugger->connect("set_execution", callable_mp(this, &ScriptEditor::_set_execution));
  3393. debugger->connect("clear_execution", callable_mp(this, &ScriptEditor::_clear_execution));
  3394. debugger->connect("breaked", callable_mp(this, &ScriptEditor::_breaked));
  3395. debugger->connect("breakpoint_set_in_tree", callable_mp(this, &ScriptEditor::_set_breakpoint));
  3396. debugger->connect("breakpoints_cleared_in_tree", callable_mp(this, &ScriptEditor::_clear_breakpoints));
  3397. menu_hb->add_spacer();
  3398. script_icon = memnew(TextureRect);
  3399. menu_hb->add_child(script_icon);
  3400. script_name_label = memnew(Label);
  3401. menu_hb->add_child(script_name_label);
  3402. script_icon->hide();
  3403. script_name_label->hide();
  3404. menu_hb->add_spacer();
  3405. site_search = memnew(Button);
  3406. site_search->set_flat(true);
  3407. site_search->set_text(TTR("Online Docs"));
  3408. site_search->connect("pressed", callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_WEBSITE));
  3409. menu_hb->add_child(site_search);
  3410. site_search->set_tooltip_text(TTR("Open Godot online documentation."));
  3411. help_search = memnew(Button);
  3412. help_search->set_flat(true);
  3413. help_search->set_text(TTR("Search Help"));
  3414. help_search->connect("pressed", callable_mp(this, &ScriptEditor::_menu_option).bind(SEARCH_HELP));
  3415. menu_hb->add_child(help_search);
  3416. help_search->set_tooltip_text(TTR("Search the reference documentation."));
  3417. menu_hb->add_child(memnew(VSeparator));
  3418. script_back = memnew(Button);
  3419. script_back->set_flat(true);
  3420. script_back->connect("pressed", callable_mp(this, &ScriptEditor::_history_back));
  3421. menu_hb->add_child(script_back);
  3422. script_back->set_disabled(true);
  3423. script_back->set_tooltip_text(TTR("Go to previous edited document."));
  3424. script_forward = memnew(Button);
  3425. script_forward->set_flat(true);
  3426. script_forward->connect("pressed", callable_mp(this, &ScriptEditor::_history_forward));
  3427. menu_hb->add_child(script_forward);
  3428. script_forward->set_disabled(true);
  3429. script_forward->set_tooltip_text(TTR("Go to next edited document."));
  3430. menu_hb->add_child(memnew(VSeparator));
  3431. make_floating = memnew(ScreenSelect);
  3432. make_floating->set_flat(true);
  3433. make_floating->connect("request_open_in_screen", callable_mp(window_wrapper, &WindowWrapper::enable_window_on_screen).bind(true));
  3434. if (!make_floating->is_disabled()) {
  3435. // Override default ScreenSelect tooltip if multi-window support is available.
  3436. make_floating->set_tooltip_text(TTR("Make the script editor floating."));
  3437. }
  3438. menu_hb->add_child(make_floating);
  3439. p_wrapper->connect("window_visibility_changed", callable_mp(this, &ScriptEditor::_window_changed));
  3440. tab_container->connect("tab_changed", callable_mp(this, &ScriptEditor::_tab_changed));
  3441. erase_tab_confirm = memnew(ConfirmationDialog);
  3442. erase_tab_confirm->set_ok_button_text(TTR("Save"));
  3443. erase_tab_confirm->add_button(TTR("Discard"), DisplayServer::get_singleton()->get_swap_cancel_ok(), "discard");
  3444. erase_tab_confirm->connect("confirmed", callable_mp(this, &ScriptEditor::_close_current_tab).bind(true));
  3445. erase_tab_confirm->connect("custom_action", callable_mp(this, &ScriptEditor::_close_discard_current_tab));
  3446. add_child(erase_tab_confirm);
  3447. script_create_dialog = memnew(ScriptCreateDialog);
  3448. script_create_dialog->set_title(TTR("Create Script"));
  3449. add_child(script_create_dialog);
  3450. script_create_dialog->connect("script_created", callable_mp(this, &ScriptEditor::_script_created));
  3451. file_dialog_option = -1;
  3452. file_dialog = memnew(EditorFileDialog);
  3453. add_child(file_dialog);
  3454. file_dialog->connect("file_selected", callable_mp(this, &ScriptEditor::_file_dialog_action));
  3455. error_dialog = memnew(AcceptDialog);
  3456. add_child(error_dialog);
  3457. disk_changed = memnew(ConfirmationDialog);
  3458. {
  3459. VBoxContainer *vbc = memnew(VBoxContainer);
  3460. disk_changed->add_child(vbc);
  3461. Label *dl = memnew(Label);
  3462. dl->set_text(TTR("The following files are newer on disk.\nWhat action should be taken?:"));
  3463. vbc->add_child(dl);
  3464. disk_changed_list = memnew(Tree);
  3465. vbc->add_child(disk_changed_list);
  3466. disk_changed_list->set_v_size_flags(SIZE_EXPAND_FILL);
  3467. disk_changed->connect("confirmed", callable_mp(this, &ScriptEditor::reload_scripts).bind(false));
  3468. disk_changed->set_ok_button_text(TTR("Reload"));
  3469. disk_changed->add_button(TTR("Resave"), !DisplayServer::get_singleton()->get_swap_cancel_ok(), "resave");
  3470. disk_changed->connect("custom_action", callable_mp(this, &ScriptEditor::_resave_scripts));
  3471. }
  3472. add_child(disk_changed);
  3473. script_editor = this;
  3474. autosave_timer = memnew(Timer);
  3475. autosave_timer->set_one_shot(false);
  3476. autosave_timer->connect(SceneStringNames::get_singleton()->tree_entered, callable_mp(this, &ScriptEditor::_update_autosave_timer));
  3477. autosave_timer->connect("timeout", callable_mp(this, &ScriptEditor::_autosave_scripts));
  3478. add_child(autosave_timer);
  3479. grab_focus_block = false;
  3480. help_search_dialog = memnew(EditorHelpSearch);
  3481. add_child(help_search_dialog);
  3482. help_search_dialog->connect("go_to_help", callable_mp(this, &ScriptEditor::_help_class_goto));
  3483. find_in_files_dialog = memnew(FindInFilesDialog);
  3484. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_FIND_REQUESTED, callable_mp(this, &ScriptEditor::_start_find_in_files).bind(false));
  3485. find_in_files_dialog->connect(FindInFilesDialog::SIGNAL_REPLACE_REQUESTED, callable_mp(this, &ScriptEditor::_start_find_in_files).bind(true));
  3486. add_child(find_in_files_dialog);
  3487. find_in_files = memnew(FindInFilesPanel);
  3488. find_in_files_button = EditorNode::get_bottom_panel()->add_item(TTR("Search Results"), find_in_files);
  3489. find_in_files->set_custom_minimum_size(Size2(0, 200) * EDSCALE);
  3490. find_in_files->connect(FindInFilesPanel::SIGNAL_RESULT_SELECTED, callable_mp(this, &ScriptEditor::_on_find_in_files_result_selected));
  3491. find_in_files->connect(FindInFilesPanel::SIGNAL_FILES_MODIFIED, callable_mp(this, &ScriptEditor::_on_find_in_files_modified_files));
  3492. find_in_files->hide();
  3493. find_in_files_button->hide();
  3494. history_pos = -1;
  3495. edit_pass = 0;
  3496. trim_trailing_whitespace_on_save = EDITOR_GET("text_editor/behavior/files/trim_trailing_whitespace_on_save");
  3497. convert_indent_on_save = EDITOR_GET("text_editor/behavior/files/convert_indent_on_save");
  3498. ScriptServer::edit_request_func = _open_script_request;
  3499. Ref<EditorJSONSyntaxHighlighter> json_syntax_highlighter;
  3500. json_syntax_highlighter.instantiate();
  3501. register_syntax_highlighter(json_syntax_highlighter);
  3502. }
  3503. ScriptEditor::~ScriptEditor() {
  3504. memdelete(completion_cache);
  3505. }
  3506. void ScriptEditorPlugin::_focus_another_editor() {
  3507. if (window_wrapper->get_window_enabled()) {
  3508. ERR_FAIL_COND(last_editor.is_empty());
  3509. EditorInterface::get_singleton()->set_main_screen_editor(last_editor);
  3510. }
  3511. }
  3512. void ScriptEditorPlugin::_save_last_editor(String p_editor) {
  3513. if (p_editor != get_name()) {
  3514. last_editor = p_editor;
  3515. }
  3516. }
  3517. void ScriptEditorPlugin::_window_visibility_changed(bool p_visible) {
  3518. _focus_another_editor();
  3519. if (p_visible) {
  3520. script_editor->add_theme_style_override("panel", script_editor->get_theme_stylebox("ScriptEditorPanelFloating", EditorStringName(EditorStyles)));
  3521. } else {
  3522. script_editor->add_theme_style_override("panel", script_editor->get_theme_stylebox("ScriptEditorPanel", EditorStringName(EditorStyles)));
  3523. }
  3524. }
  3525. void ScriptEditorPlugin::_notification(int p_what) {
  3526. switch (p_what) {
  3527. case NOTIFICATION_ENTER_TREE: {
  3528. connect("main_screen_changed", callable_mp(this, &ScriptEditorPlugin::_save_last_editor));
  3529. } break;
  3530. case NOTIFICATION_EXIT_TREE: {
  3531. disconnect("main_screen_changed", callable_mp(this, &ScriptEditorPlugin::_save_last_editor));
  3532. } break;
  3533. }
  3534. }
  3535. void ScriptEditorPlugin::edit(Object *p_object) {
  3536. if (Object::cast_to<Script>(p_object)) {
  3537. Script *p_script = Object::cast_to<Script>(p_object);
  3538. String res_path = p_script->get_path().get_slice("::", 0);
  3539. if (p_script->is_built_in() && !res_path.is_empty()) {
  3540. if (ResourceLoader::get_resource_type(res_path) == "PackedScene") {
  3541. if (!EditorNode::get_singleton()->is_scene_open(res_path)) {
  3542. EditorNode::get_singleton()->load_scene(res_path);
  3543. }
  3544. } else {
  3545. EditorNode::get_singleton()->load_resource(res_path);
  3546. }
  3547. }
  3548. script_editor->edit(p_script);
  3549. } else if (Object::cast_to<JSON>(p_object)) {
  3550. script_editor->edit(Object::cast_to<JSON>(p_object));
  3551. } else if (Object::cast_to<TextFile>(p_object)) {
  3552. script_editor->edit(Object::cast_to<TextFile>(p_object));
  3553. }
  3554. }
  3555. bool ScriptEditorPlugin::handles(Object *p_object) const {
  3556. if (Object::cast_to<TextFile>(p_object)) {
  3557. return true;
  3558. }
  3559. if (Object::cast_to<Script>(p_object)) {
  3560. return true;
  3561. }
  3562. if (Object::cast_to<JSON>(p_object)) {
  3563. return true;
  3564. }
  3565. return p_object->is_class("Script");
  3566. }
  3567. void ScriptEditorPlugin::make_visible(bool p_visible) {
  3568. if (p_visible) {
  3569. window_wrapper->show();
  3570. script_editor->set_process(true);
  3571. script_editor->ensure_select_current();
  3572. } else {
  3573. window_wrapper->hide();
  3574. if (!window_wrapper->get_window_enabled()) {
  3575. script_editor->set_process(false);
  3576. }
  3577. }
  3578. }
  3579. void ScriptEditorPlugin::selected_notify() {
  3580. script_editor->ensure_select_current();
  3581. _focus_another_editor();
  3582. }
  3583. String ScriptEditorPlugin::get_unsaved_status(const String &p_for_scene) const {
  3584. const PackedStringArray unsaved_scripts = script_editor->get_unsaved_scripts();
  3585. if (unsaved_scripts.is_empty()) {
  3586. return String();
  3587. }
  3588. PackedStringArray message;
  3589. if (!p_for_scene.is_empty()) {
  3590. PackedStringArray unsaved_built_in_scripts;
  3591. const String scene_file = p_for_scene.get_file();
  3592. for (const String &E : unsaved_scripts) {
  3593. if (!E.is_resource_file() && E.contains(scene_file)) {
  3594. unsaved_built_in_scripts.append(E);
  3595. }
  3596. }
  3597. if (unsaved_built_in_scripts.is_empty()) {
  3598. return String();
  3599. } else {
  3600. message.resize(unsaved_built_in_scripts.size() + 1);
  3601. message.write[0] = TTR("There are unsaved changes in the following built-in script(s):");
  3602. int i = 1;
  3603. for (const String &E : unsaved_built_in_scripts) {
  3604. message.write[i] = E.trim_suffix("(*)");
  3605. i++;
  3606. }
  3607. return String("\n").join(message);
  3608. }
  3609. }
  3610. message.resize(unsaved_scripts.size() + 1);
  3611. message.write[0] = TTR("Save changes to the following script(s) before quitting?");
  3612. int i = 1;
  3613. for (const String &E : unsaved_scripts) {
  3614. message.write[i] = E.trim_suffix("(*)");
  3615. i++;
  3616. }
  3617. return String("\n").join(message);
  3618. }
  3619. void ScriptEditorPlugin::save_external_data() {
  3620. if (!EditorNode::get_singleton()->is_exiting()) {
  3621. script_editor->save_all_scripts();
  3622. }
  3623. }
  3624. void ScriptEditorPlugin::apply_changes() {
  3625. script_editor->apply_scripts();
  3626. }
  3627. void ScriptEditorPlugin::set_window_layout(Ref<ConfigFile> p_layout) {
  3628. script_editor->set_window_layout(p_layout);
  3629. if (EDITOR_GET("interface/multi_window/restore_windows_on_load") && window_wrapper->is_window_available() && p_layout->has_section_key("ScriptEditor", "window_rect")) {
  3630. window_wrapper->restore_window_from_saved_position(
  3631. p_layout->get_value("ScriptEditor", "window_rect", Rect2i()),
  3632. p_layout->get_value("ScriptEditor", "window_screen", -1),
  3633. p_layout->get_value("ScriptEditor", "window_screen_rect", Rect2i()));
  3634. } else {
  3635. window_wrapper->set_window_enabled(false);
  3636. }
  3637. }
  3638. void ScriptEditorPlugin::get_window_layout(Ref<ConfigFile> p_layout) {
  3639. script_editor->get_window_layout(p_layout);
  3640. if (window_wrapper->get_window_enabled()) {
  3641. p_layout->set_value("ScriptEditor", "window_rect", window_wrapper->get_window_rect());
  3642. int screen = window_wrapper->get_window_screen();
  3643. p_layout->set_value("ScriptEditor", "window_screen", screen);
  3644. p_layout->set_value("ScriptEditor", "window_screen_rect", DisplayServer::get_singleton()->screen_get_usable_rect(screen));
  3645. } else {
  3646. if (p_layout->has_section_key("ScriptEditor", "window_rect")) {
  3647. p_layout->erase_section_key("ScriptEditor", "window_rect");
  3648. }
  3649. if (p_layout->has_section_key("ScriptEditor", "window_screen")) {
  3650. p_layout->erase_section_key("ScriptEditor", "window_screen");
  3651. }
  3652. if (p_layout->has_section_key("ScriptEditor", "window_screen_rect")) {
  3653. p_layout->erase_section_key("ScriptEditor", "window_screen_rect");
  3654. }
  3655. }
  3656. }
  3657. void ScriptEditorPlugin::get_breakpoints(List<String> *p_breakpoints) {
  3658. script_editor->get_breakpoints(p_breakpoints);
  3659. }
  3660. void ScriptEditorPlugin::edited_scene_changed() {
  3661. script_editor->edited_scene_changed();
  3662. }
  3663. ScriptEditorPlugin::ScriptEditorPlugin() {
  3664. window_wrapper = memnew(WindowWrapper);
  3665. window_wrapper->set_window_title(vformat(TTR("%s - Godot Engine"), TTR("Script Editor")));
  3666. window_wrapper->set_margins_enabled(true);
  3667. script_editor = memnew(ScriptEditor(window_wrapper));
  3668. Ref<Shortcut> make_floating_shortcut = ED_SHORTCUT_AND_COMMAND("script_editor/make_floating", TTR("Make Floating"));
  3669. window_wrapper->set_wrapped_control(script_editor, make_floating_shortcut);
  3670. EditorNode::get_singleton()->get_main_screen_control()->add_child(window_wrapper);
  3671. window_wrapper->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  3672. window_wrapper->hide();
  3673. window_wrapper->connect("window_visibility_changed", callable_mp(this, &ScriptEditorPlugin::_window_visibility_changed));
  3674. EDITOR_GET("text_editor/behavior/files/auto_reload_scripts_on_external_change");
  3675. ScriptServer::set_reload_scripts_on_save(EDITOR_DEF("text_editor/behavior/files/auto_reload_and_parse_scripts_on_save", true));
  3676. EDITOR_DEF("text_editor/behavior/files/open_dominant_script_on_scene_change", true);
  3677. EDITOR_DEF("text_editor/external/use_external_editor", false);
  3678. EDITOR_DEF("text_editor/external/exec_path", "");
  3679. EDITOR_DEF("text_editor/script_list/script_temperature_enabled", true);
  3680. EDITOR_DEF("text_editor/script_list/script_temperature_history_size", 15);
  3681. EDITOR_DEF("text_editor/script_list/group_help_pages", true);
  3682. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/script_list/sort_scripts_by", PROPERTY_HINT_ENUM, "Name,Path,None"));
  3683. EDITOR_DEF("text_editor/script_list/sort_scripts_by", 0);
  3684. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "text_editor/script_list/list_script_names_as", PROPERTY_HINT_ENUM, "Name,Parent Directory And Name,Full Path"));
  3685. EDITOR_DEF("text_editor/script_list/list_script_names_as", 0);
  3686. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_path", PROPERTY_HINT_GLOBAL_FILE));
  3687. EDITOR_DEF("text_editor/external/exec_flags", "{file}");
  3688. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::STRING, "text_editor/external/exec_flags", PROPERTY_HINT_PLACEHOLDER_TEXT, "Call flags with placeholders: {project}, {file}, {col}, {line}."));
  3689. ED_SHORTCUT("script_editor/reopen_closed_script", TTR("Reopen Closed Script"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::T);
  3690. ED_SHORTCUT("script_editor/clear_recent", TTR("Clear Recent Scripts"));
  3691. }
  3692. ScriptEditorPlugin::~ScriptEditorPlugin() {
  3693. }