script_editor_plugin.cpp 147 KB

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