script_editor_plugin.cpp 144 KB

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