script_editor_plugin.cpp 115 KB

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