script_editor_plugin.cpp 118 KB

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