script_editor_plugin.cpp 151 KB

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