script_editor_plugin.cpp 143 KB

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