script_editor_plugin.cpp 145 KB

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