script_editor_plugin.cpp 121 KB

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