script_editor_plugin.cpp 116 KB

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