script_editor_plugin.cpp 132 KB

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