script_text_editor.cpp 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946
  1. /**************************************************************************/
  2. /* script_text_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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_text_editor.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/io/json.h"
  33. #include "core/math/expression.h"
  34. #include "core/os/keyboard.h"
  35. #include "editor/debugger/editor_debugger_node.h"
  36. #include "editor/editor_command_palette.h"
  37. #include "editor/editor_help.h"
  38. #include "editor/editor_node.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/editor_string_names.h"
  41. #include "editor/gui/editor_toaster.h"
  42. #include "editor/plugins/editor_context_menu_plugin.h"
  43. #include "editor/themes/editor_scale.h"
  44. #include "scene/gui/menu_button.h"
  45. #include "scene/gui/rich_text_label.h"
  46. #include "scene/gui/slider.h"
  47. #include "scene/gui/split_container.h"
  48. void ConnectionInfoDialog::ok_pressed() {
  49. }
  50. void ConnectionInfoDialog::popup_connections(const String &p_method, const Vector<Node *> &p_nodes) {
  51. method->set_text(p_method);
  52. tree->clear();
  53. TreeItem *root = tree->create_item();
  54. for (int i = 0; i < p_nodes.size(); i++) {
  55. List<Connection> all_connections;
  56. p_nodes[i]->get_signals_connected_to_this(&all_connections);
  57. for (const Connection &connection : all_connections) {
  58. if (connection.callable.get_method() != p_method) {
  59. continue;
  60. }
  61. TreeItem *node_item = tree->create_item(root);
  62. node_item->set_text(0, Object::cast_to<Node>(connection.signal.get_object())->get_name());
  63. node_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(connection.signal.get_object(), "Node"));
  64. node_item->set_selectable(0, false);
  65. node_item->set_editable(0, false);
  66. node_item->set_text(1, connection.signal.get_name());
  67. Control *p = Object::cast_to<Control>(get_parent());
  68. node_item->set_icon(1, p->get_editor_theme_icon(SNAME("Slot")));
  69. node_item->set_selectable(1, false);
  70. node_item->set_editable(1, false);
  71. node_item->set_text(2, Object::cast_to<Node>(connection.callable.get_object())->get_name());
  72. node_item->set_icon(2, EditorNode::get_singleton()->get_object_icon(connection.callable.get_object(), "Node"));
  73. node_item->set_selectable(2, false);
  74. node_item->set_editable(2, false);
  75. }
  76. }
  77. popup_centered(Size2(600, 300) * EDSCALE);
  78. }
  79. ConnectionInfoDialog::ConnectionInfoDialog() {
  80. set_title(TTR("Connections to method:"));
  81. VBoxContainer *vbc = memnew(VBoxContainer);
  82. vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  83. vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  84. vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  85. vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
  86. add_child(vbc);
  87. method = memnew(Label);
  88. method->set_focus_mode(Control::FOCUS_ACCESSIBILITY);
  89. method->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  90. vbc->add_child(method);
  91. tree = memnew(Tree);
  92. tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
  93. tree->set_columns(3);
  94. tree->set_hide_root(true);
  95. tree->set_column_titles_visible(true);
  96. tree->set_column_title(0, TTR("Source"));
  97. tree->set_column_title(1, TTR("Signal"));
  98. tree->set_column_title(2, TTR("Target"));
  99. vbc->add_child(tree);
  100. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  101. tree->set_allow_rmb_select(true);
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////
  104. Vector<String> ScriptTextEditor::get_functions() {
  105. CodeEdit *te = code_editor->get_text_editor();
  106. String text = te->get_text();
  107. List<String> fnc;
  108. if (script->get_language()->validate(text, script->get_path(), &fnc)) {
  109. //if valid rewrite functions to latest
  110. functions.clear();
  111. for (const String &E : fnc) {
  112. functions.push_back(E);
  113. }
  114. }
  115. return functions;
  116. }
  117. void ScriptTextEditor::apply_code() {
  118. if (script.is_null()) {
  119. return;
  120. }
  121. script->set_source_code(code_editor->get_text_editor()->get_text());
  122. script->update_exports();
  123. code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
  124. }
  125. Ref<Resource> ScriptTextEditor::get_edited_resource() const {
  126. return script;
  127. }
  128. void ScriptTextEditor::set_edited_resource(const Ref<Resource> &p_res) {
  129. ERR_FAIL_COND(script.is_valid());
  130. ERR_FAIL_COND(p_res.is_null());
  131. script = p_res;
  132. code_editor->get_text_editor()->set_text(script->get_source_code());
  133. code_editor->get_text_editor()->clear_undo_history();
  134. code_editor->get_text_editor()->tag_saved_version();
  135. emit_signal(SNAME("name_changed"));
  136. code_editor->update_line_and_column();
  137. }
  138. void ScriptTextEditor::enable_editor(Control *p_shortcut_context) {
  139. if (editor_enabled) {
  140. return;
  141. }
  142. editor_enabled = true;
  143. _enable_code_editor();
  144. _validate_script();
  145. if (p_shortcut_context) {
  146. for (int i = 0; i < edit_hb->get_child_count(); ++i) {
  147. Control *c = cast_to<Control>(edit_hb->get_child(i));
  148. if (c) {
  149. c->set_shortcut_context(p_shortcut_context);
  150. }
  151. }
  152. }
  153. }
  154. void ScriptTextEditor::_load_theme_settings() {
  155. CodeEdit *text_edit = code_editor->get_text_editor();
  156. Color updated_warning_line_color = EDITOR_GET("text_editor/theme/highlighting/warning_color");
  157. Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");
  158. Color updated_safe_line_number_color = EDITOR_GET("text_editor/theme/highlighting/safe_line_number_color");
  159. Color updated_folded_code_region_color = EDITOR_GET("text_editor/theme/highlighting/folded_code_region_color");
  160. bool warning_line_color_updated = updated_warning_line_color != warning_line_color;
  161. bool marked_line_color_updated = updated_marked_line_color != marked_line_color;
  162. bool safe_line_number_color_updated = updated_safe_line_number_color != safe_line_number_color;
  163. bool folded_code_region_color_updated = updated_folded_code_region_color != folded_code_region_color;
  164. if (safe_line_number_color_updated || warning_line_color_updated || marked_line_color_updated || folded_code_region_color_updated) {
  165. safe_line_number_color = updated_safe_line_number_color;
  166. for (int i = 0; i < text_edit->get_line_count(); i++) {
  167. if (warning_line_color_updated && text_edit->get_line_background_color(i) == warning_line_color) {
  168. text_edit->set_line_background_color(i, updated_warning_line_color);
  169. }
  170. if (marked_line_color_updated && text_edit->get_line_background_color(i) == marked_line_color) {
  171. text_edit->set_line_background_color(i, updated_marked_line_color);
  172. }
  173. if (safe_line_number_color_updated && text_edit->get_line_gutter_item_color(i, line_number_gutter) != default_line_number_color) {
  174. text_edit->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  175. }
  176. if (folded_code_region_color_updated && text_edit->get_line_background_color(i) == folded_code_region_color) {
  177. text_edit->set_line_background_color(i, updated_folded_code_region_color);
  178. }
  179. }
  180. warning_line_color = updated_warning_line_color;
  181. marked_line_color = updated_marked_line_color;
  182. folded_code_region_color = updated_folded_code_region_color;
  183. }
  184. theme_loaded = true;
  185. if (script.is_valid()) {
  186. _set_theme_for_script();
  187. }
  188. }
  189. void ScriptTextEditor::_set_theme_for_script() {
  190. if (!theme_loaded) {
  191. return;
  192. }
  193. CodeEdit *text_edit = code_editor->get_text_editor();
  194. text_edit->get_syntax_highlighter()->update_cache();
  195. Vector<String> strings = script->get_language()->get_string_delimiters();
  196. text_edit->clear_string_delimiters();
  197. for (const String &string : strings) {
  198. String beg = string.get_slicec(' ', 0);
  199. String end = string.get_slice_count(" ") > 1 ? string.get_slicec(' ', 1) : String();
  200. if (!text_edit->has_string_delimiter(beg)) {
  201. text_edit->add_string_delimiter(beg, end, end.is_empty());
  202. }
  203. if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) {
  204. text_edit->add_auto_brace_completion_pair(beg, end);
  205. }
  206. }
  207. text_edit->clear_comment_delimiters();
  208. for (const String &comment : script->get_language()->get_comment_delimiters()) {
  209. String beg = comment.get_slicec(' ', 0);
  210. String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 1) : String();
  211. text_edit->add_comment_delimiter(beg, end, end.is_empty());
  212. if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) {
  213. text_edit->add_auto_brace_completion_pair(beg, end);
  214. }
  215. }
  216. for (const String &doc_comment : script->get_language()->get_doc_comment_delimiters()) {
  217. String beg = doc_comment.get_slicec(' ', 0);
  218. String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 1) : String();
  219. text_edit->add_comment_delimiter(beg, end, end.is_empty());
  220. if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) {
  221. text_edit->add_auto_brace_completion_pair(beg, end);
  222. }
  223. }
  224. }
  225. void ScriptTextEditor::_show_errors_panel(bool p_show) {
  226. errors_panel->set_visible(p_show);
  227. }
  228. void ScriptTextEditor::_show_warnings_panel(bool p_show) {
  229. warnings_panel->set_visible(p_show);
  230. }
  231. void ScriptTextEditor::_warning_clicked(const Variant &p_line) {
  232. if (p_line.get_type() == Variant::INT) {
  233. goto_line_centered(p_line.operator int64_t());
  234. } else if (p_line.get_type() == Variant::DICTIONARY) {
  235. Dictionary meta = p_line.operator Dictionary();
  236. const int line = meta["line"].operator int64_t() - 1;
  237. const String code = meta["code"].operator String();
  238. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  239. CodeEdit *text_editor = code_editor->get_text_editor();
  240. String prev_line = line > 0 ? text_editor->get_line(line - 1) : "";
  241. if (prev_line.contains("@warning_ignore")) {
  242. const int closing_bracket_idx = prev_line.find_char(')');
  243. const String text_to_insert = ", " + code.quote(quote_style);
  244. text_editor->insert_text(text_to_insert, line - 1, closing_bracket_idx);
  245. } else {
  246. const int indent = text_editor->get_indent_level(line) / text_editor->get_indent_size();
  247. String annotation_indent;
  248. if (!text_editor->is_indent_using_spaces()) {
  249. annotation_indent = String("\t").repeat(indent);
  250. } else {
  251. annotation_indent = String(" ").repeat(text_editor->get_indent_size() * indent);
  252. }
  253. text_editor->insert_line_at(line, annotation_indent + "@warning_ignore(" + code.quote(quote_style) + ")");
  254. }
  255. _validate_script();
  256. }
  257. }
  258. void ScriptTextEditor::_error_clicked(const Variant &p_line) {
  259. if (p_line.get_type() == Variant::INT) {
  260. goto_line_centered(p_line.operator int64_t());
  261. } else if (p_line.get_type() == Variant::DICTIONARY) {
  262. Dictionary meta = p_line.operator Dictionary();
  263. const String path = meta["path"].operator String();
  264. const int line = meta["line"].operator int64_t();
  265. const int column = meta["column"].operator int64_t();
  266. if (path.is_empty()) {
  267. goto_line_centered(line, column);
  268. } else {
  269. Ref<Resource> scr = ResourceLoader::load(path);
  270. if (scr.is_null()) {
  271. EditorNode::get_singleton()->show_warning(TTR("Could not load file at:") + "\n\n" + path, TTR("Error!"));
  272. } else {
  273. int corrected_column = column;
  274. const String line_text = code_editor->get_text_editor()->get_line(line);
  275. const int indent_size = code_editor->get_text_editor()->get_indent_size();
  276. if (indent_size > 1) {
  277. const int tab_count = line_text.length() - line_text.lstrip("\t").length();
  278. corrected_column -= tab_count * (indent_size - 1);
  279. }
  280. ScriptEditor::get_singleton()->edit(scr, line, corrected_column);
  281. }
  282. }
  283. }
  284. }
  285. void ScriptTextEditor::reload_text() {
  286. ERR_FAIL_COND(script.is_null());
  287. CodeEdit *te = code_editor->get_text_editor();
  288. int column = te->get_caret_column();
  289. int row = te->get_caret_line();
  290. int h = te->get_h_scroll();
  291. int v = te->get_v_scroll();
  292. te->set_text(script->get_source_code());
  293. te->set_caret_line(row);
  294. te->set_caret_column(column);
  295. te->set_h_scroll(h);
  296. te->set_v_scroll(v);
  297. te->tag_saved_version();
  298. code_editor->update_line_and_column();
  299. if (editor_enabled) {
  300. _validate_script();
  301. }
  302. }
  303. void ScriptTextEditor::add_callback(const String &p_function, const PackedStringArray &p_args) {
  304. ScriptLanguage *language = script->get_language();
  305. if (!language->can_make_function()) {
  306. return;
  307. }
  308. code_editor->get_text_editor()->begin_complex_operation();
  309. code_editor->get_text_editor()->remove_secondary_carets();
  310. code_editor->get_text_editor()->deselect();
  311. String code = code_editor->get_text_editor()->get_text();
  312. int pos = language->find_function(p_function, code);
  313. if (pos == -1) {
  314. // Function does not exist, create it at the end of the file.
  315. int last_line = code_editor->get_text_editor()->get_line_count() - 1;
  316. String func = language->make_function("", p_function, p_args);
  317. code_editor->get_text_editor()->insert_text("\n\n" + func, last_line, code_editor->get_text_editor()->get_line(last_line).length());
  318. pos = last_line + 3;
  319. }
  320. // Put caret on the line after the function, after the indent.
  321. int indent_column = 1;
  322. if (EDITOR_GET("text_editor/behavior/indent/type")) {
  323. indent_column = EDITOR_GET("text_editor/behavior/indent/size");
  324. }
  325. code_editor->get_text_editor()->set_caret_line(pos, true, true, -1);
  326. code_editor->get_text_editor()->set_caret_column(indent_column);
  327. code_editor->get_text_editor()->end_complex_operation();
  328. }
  329. bool ScriptTextEditor::show_members_overview() {
  330. return true;
  331. }
  332. bool ScriptTextEditor::_is_valid_color_info(const Dictionary &p_info) {
  333. if (p_info.get_valid("color").get_type() != Variant::COLOR) {
  334. return false;
  335. }
  336. if (!p_info.get_valid("color_end").is_num() || !p_info.get_valid("color_mode").is_num()) {
  337. return false;
  338. }
  339. return true;
  340. }
  341. Array ScriptTextEditor::_inline_object_parse(const String &p_text, int p_line) {
  342. Array result;
  343. int i_end_previous = 0;
  344. int i_start = p_text.find("Color");
  345. while (i_start != -1) {
  346. // Ignore words that just have "Color" in them.
  347. if (i_start == 0 || !("_" + p_text.substr(i_start - 1, 1)).is_valid_ascii_identifier()) {
  348. int i_par_start = p_text.find_char('(', i_start + 5);
  349. if (i_par_start != -1) {
  350. int i_par_end = p_text.find_char(')', i_start + 5);
  351. if (i_par_end != -1) {
  352. Dictionary color_info;
  353. color_info["line"] = p_line;
  354. color_info["column"] = i_start;
  355. color_info["width_ratio"] = 1.0;
  356. color_info["color_end"] = i_par_end;
  357. String fn_name = p_text.substr(i_start + 5, i_par_start - i_start - 5);
  358. String s_params = p_text.substr(i_par_start + 1, i_par_end - i_par_start - 1);
  359. bool has_added_color = false;
  360. if (fn_name.is_empty()) {
  361. String stripped = s_params.strip_edges(true, true);
  362. // String constructor.
  363. if (stripped.length() > 0 && (stripped[0] == '\"')) {
  364. String color_string = stripped.substr(1, stripped.length() - 2);
  365. color_info["color"] = Color(color_string);
  366. color_info["color_mode"] = MODE_STRING;
  367. has_added_color = true;
  368. }
  369. // Hex constructor.
  370. else if (stripped.length() == 10 && stripped.substr(0, 2) == "0x") {
  371. color_info["color"] = Color(stripped.substr(2, stripped.length() - 2));
  372. color_info["color_mode"] = MODE_HEX;
  373. has_added_color = true;
  374. }
  375. // Empty Color() constructor.
  376. else if (stripped.is_empty()) {
  377. color_info["color"] = Color();
  378. color_info["color_mode"] = MODE_RGB;
  379. has_added_color = true;
  380. }
  381. }
  382. // Float & int parameters.
  383. if (!has_added_color && s_params.size() > 0) {
  384. PackedFloat64Array params = s_params.split_floats(",", false);
  385. if (params.size() == 3) {
  386. params.resize(4);
  387. params.set(3, 1.0);
  388. }
  389. if (params.size() == 4) {
  390. has_added_color = true;
  391. if (fn_name == ".from_ok_hsl") {
  392. color_info["color"] = Color::from_ok_hsl(params[0], params[1], params[2], params[3]);
  393. color_info["color_mode"] = MODE_OKHSL;
  394. } else if (fn_name == ".from_hsv") {
  395. color_info["color"] = Color::from_hsv(params[0], params[1], params[2], params[3]);
  396. color_info["color_mode"] = MODE_HSV;
  397. } else if (fn_name == ".from_rgba8") {
  398. color_info["color"] = Color::from_rgba8(int(params[0]), int(params[1]), int(params[2]), int(params[3]));
  399. color_info["color_mode"] = MODE_RGB8;
  400. } else if (fn_name.is_empty()) {
  401. color_info["color"] = Color(params[0], params[1], params[2], params[3]);
  402. color_info["color_mode"] = MODE_RGB;
  403. } else {
  404. has_added_color = false;
  405. }
  406. }
  407. }
  408. if (has_added_color) {
  409. result.push_back(color_info);
  410. i_end_previous = i_par_end + 1;
  411. }
  412. }
  413. }
  414. }
  415. i_end_previous = MAX(i_end_previous, i_start);
  416. i_start = p_text.find("Color", i_start + 1);
  417. }
  418. return result;
  419. }
  420. void ScriptTextEditor::_inline_object_draw(const Dictionary &p_info, const Rect2 &p_rect) {
  421. if (_is_valid_color_info(p_info)) {
  422. Rect2 col_rect = p_rect.grow(-4);
  423. if (color_alpha_texture.is_null()) {
  424. color_alpha_texture = inline_color_picker->get_theme_icon("sample_bg", "ColorPicker");
  425. }
  426. code_editor->get_text_editor()->draw_texture_rect(color_alpha_texture, col_rect, false);
  427. code_editor->get_text_editor()->draw_rect(col_rect, Color(p_info["color"]));
  428. code_editor->get_text_editor()->draw_rect(col_rect, Color(1, 1, 1), false, 1);
  429. }
  430. }
  431. void ScriptTextEditor::_inline_object_handle_click(const Dictionary &p_info, const Rect2 &p_rect) {
  432. if (_is_valid_color_info(p_info)) {
  433. inline_color_picker->set_pick_color(p_info["color"]);
  434. inline_color_line = p_info["line"];
  435. inline_color_start = p_info["column"];
  436. inline_color_end = p_info["color_end"];
  437. // Reset tooltip hover timer.
  438. code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(false);
  439. code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true);
  440. _update_color_constructor_options();
  441. inline_color_options->select(p_info["color_mode"]);
  442. EditorNode::get_singleton()->setup_color_picker(inline_color_picker);
  443. // Move popup above the line if it's too low.
  444. float_t view_h = get_viewport_rect().size.y;
  445. float_t pop_h = inline_color_popup->get_contents_minimum_size().y;
  446. float_t pop_y = p_rect.get_end().y;
  447. float_t pop_x = p_rect.position.x;
  448. if (pop_y + pop_h > view_h) {
  449. pop_y = p_rect.position.y - pop_h;
  450. }
  451. // Move popup to the right if it's too high.
  452. if (pop_y < 0) {
  453. pop_x = p_rect.get_end().x;
  454. }
  455. inline_color_popup->popup(Rect2(pop_x, pop_y, 0, 0));
  456. }
  457. }
  458. String ScriptTextEditor::_picker_color_stringify(const Color &p_color, COLOR_MODE p_mode) {
  459. String result;
  460. String fname;
  461. Vector<String> str_params;
  462. switch (p_mode) {
  463. case ScriptTextEditor::MODE_STRING: {
  464. str_params.push_back("\"" + p_color.to_html() + "\"");
  465. } break;
  466. case ScriptTextEditor::MODE_HEX: {
  467. str_params.push_back("0x" + p_color.to_html());
  468. } break;
  469. case ScriptTextEditor::MODE_RGB: {
  470. str_params = {
  471. String::num(p_color.r, 3),
  472. String::num(p_color.g, 3),
  473. String::num(p_color.b, 3),
  474. String::num(p_color.a, 3)
  475. };
  476. } break;
  477. case ScriptTextEditor::MODE_HSV: {
  478. str_params = {
  479. String::num(p_color.get_h(), 3),
  480. String::num(p_color.get_s(), 3),
  481. String::num(p_color.get_v(), 3),
  482. String::num(p_color.a, 3)
  483. };
  484. fname = ".from_hsv";
  485. } break;
  486. case ScriptTextEditor::MODE_OKHSL: {
  487. str_params = {
  488. String::num(p_color.get_ok_hsl_h(), 3),
  489. String::num(p_color.get_ok_hsl_s(), 3),
  490. String::num(p_color.get_ok_hsl_l(), 3),
  491. String::num(p_color.a, 3)
  492. };
  493. fname = ".from_ok_hsl";
  494. } break;
  495. case ScriptTextEditor::MODE_RGB8: {
  496. str_params = {
  497. itos(p_color.get_r8()),
  498. itos(p_color.get_g8()),
  499. itos(p_color.get_b8()),
  500. itos(p_color.get_a8())
  501. };
  502. fname = ".from_rgba8";
  503. } break;
  504. default: {
  505. } break;
  506. }
  507. result = "Color" + fname + "(" + String(", ").join(str_params) + ")";
  508. return result;
  509. }
  510. void ScriptTextEditor::_picker_color_changed(const Color &p_color) {
  511. _update_color_constructor_options();
  512. _update_color_text();
  513. }
  514. void ScriptTextEditor::_update_color_constructor_options() {
  515. int item_count = inline_color_options->get_item_count();
  516. // Update or add each constructor as an option.
  517. for (int i = 0; i < MODE_MAX; i++) {
  518. String option_text = _picker_color_stringify(inline_color_picker->get_pick_color(), (COLOR_MODE)i);
  519. if (i >= item_count) {
  520. inline_color_options->add_item(option_text);
  521. } else {
  522. inline_color_options->set_item_text(i, option_text);
  523. }
  524. }
  525. }
  526. void ScriptTextEditor::_update_background_color() {
  527. // Clear background lines.
  528. CodeEdit *te = code_editor->get_text_editor();
  529. for (int i = 0; i < te->get_line_count(); i++) {
  530. bool is_folded_code_region = te->is_line_code_region_start(i) && te->is_line_folded(i);
  531. te->set_line_background_color(i, is_folded_code_region ? folded_code_region_color : Color(0, 0, 0, 0));
  532. }
  533. // Set the warning background.
  534. if (warning_line_color.a != 0.0) {
  535. for (const ScriptLanguage::Warning &warning : warnings) {
  536. int folder_line_header = te->get_folded_line_header(warning.end_line - 2);
  537. bool is_folded = folder_line_header != (warning.end_line - 2);
  538. if (is_folded) {
  539. te->set_line_background_color(folder_line_header, warning_line_color);
  540. } else if (warning.end_line - warning.start_line > 0 && warning.end_line - warning.start_line < 20) {
  541. // If the warning spans below 20 lines (arbitrary), set the background color for all lines.
  542. // (W.end_line - W.start_line > 0) ensures that we set the background for single line warnings.
  543. for (int i = warning.start_line - 1; i < warning.end_line - 1; i++) {
  544. te->set_line_background_color(i, warning_line_color);
  545. }
  546. } else {
  547. // Otherwise, just set the background color for the start line of the warning.
  548. te->set_line_background_color(warning.start_line - 1, warning_line_color);
  549. }
  550. }
  551. }
  552. // Set the error background.
  553. if (marked_line_color.a != 0.0) {
  554. for (const ScriptLanguage::ScriptError &error : errors) {
  555. int folder_line_header = te->get_folded_line_header(error.line - 1);
  556. bool is_folded_code = folder_line_header != (error.line - 1);
  557. if (is_folded_code) {
  558. te->set_line_background_color(folder_line_header, marked_line_color);
  559. } else {
  560. te->set_line_background_color(error.line - 1, marked_line_color);
  561. }
  562. }
  563. }
  564. }
  565. void ScriptTextEditor::_update_color_text() {
  566. if (inline_color_line < 0) {
  567. return;
  568. }
  569. String result = inline_color_options->get_item_text(inline_color_options->get_selected_id());
  570. code_editor->get_text_editor()->begin_complex_operation();
  571. code_editor->get_text_editor()->remove_text(inline_color_line, inline_color_start, inline_color_line, inline_color_end + 1);
  572. inline_color_end = inline_color_start + result.size() - 2;
  573. code_editor->get_text_editor()->insert_text(result, inline_color_line, inline_color_start);
  574. code_editor->get_text_editor()->end_complex_operation();
  575. }
  576. void ScriptTextEditor::update_settings() {
  577. code_editor->get_text_editor()->set_gutter_draw(connection_gutter, EDITOR_GET("text_editor/appearance/gutters/show_info_gutter"));
  578. if (EDITOR_GET("text_editor/appearance/enable_inline_color_picker")) {
  579. code_editor->get_text_editor()->set_inline_object_handlers(
  580. callable_mp(this, &ScriptTextEditor::_inline_object_parse),
  581. callable_mp(this, &ScriptTextEditor::_inline_object_draw),
  582. callable_mp(this, &ScriptTextEditor::_inline_object_handle_click));
  583. } else {
  584. code_editor->get_text_editor()->set_inline_object_handlers(Callable(), Callable(), Callable());
  585. }
  586. code_editor->update_editor_settings();
  587. }
  588. bool ScriptTextEditor::is_unsaved() {
  589. const bool unsaved =
  590. code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() ||
  591. script->get_path().is_empty(); // In memory.
  592. return unsaved;
  593. }
  594. Variant ScriptTextEditor::get_edit_state() {
  595. return code_editor->get_edit_state();
  596. }
  597. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  598. code_editor->set_edit_state(p_state);
  599. Dictionary state = p_state;
  600. if (state.has("syntax_highlighter")) {
  601. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  602. if (idx >= 0) {
  603. _change_syntax_highlighter(idx);
  604. }
  605. }
  606. if (editor_enabled) {
  607. #ifndef ANDROID_ENABLED
  608. ensure_focus();
  609. #endif
  610. }
  611. }
  612. Variant ScriptTextEditor::get_navigation_state() {
  613. return code_editor->get_navigation_state();
  614. }
  615. Variant ScriptTextEditor::get_previous_state() {
  616. return code_editor->get_previous_state();
  617. }
  618. void ScriptTextEditor::store_previous_state() {
  619. return code_editor->store_previous_state();
  620. }
  621. void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  622. code_editor->convert_case(p_case);
  623. }
  624. void ScriptTextEditor::trim_trailing_whitespace() {
  625. code_editor->trim_trailing_whitespace();
  626. }
  627. void ScriptTextEditor::trim_final_newlines() {
  628. code_editor->trim_final_newlines();
  629. }
  630. void ScriptTextEditor::insert_final_newline() {
  631. code_editor->insert_final_newline();
  632. }
  633. void ScriptTextEditor::convert_indent() {
  634. code_editor->get_text_editor()->convert_indent();
  635. }
  636. void ScriptTextEditor::tag_saved_version() {
  637. code_editor->get_text_editor()->tag_saved_version();
  638. edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);
  639. }
  640. void ScriptTextEditor::goto_line(int p_line, int p_column) {
  641. code_editor->goto_line(p_line, p_column);
  642. }
  643. void ScriptTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  644. code_editor->goto_line_selection(p_line, p_begin, p_end);
  645. }
  646. void ScriptTextEditor::goto_line_centered(int p_line, int p_column) {
  647. code_editor->goto_line_centered(p_line, p_column);
  648. }
  649. void ScriptTextEditor::set_executing_line(int p_line) {
  650. code_editor->set_executing_line(p_line);
  651. }
  652. void ScriptTextEditor::clear_executing_line() {
  653. code_editor->clear_executing_line();
  654. }
  655. void ScriptTextEditor::ensure_focus() {
  656. code_editor->get_text_editor()->grab_focus();
  657. }
  658. String ScriptTextEditor::get_name() {
  659. String name;
  660. name = script->get_path().get_file();
  661. if (name.is_empty()) {
  662. // This appears for newly created built-in scripts before saving the scene.
  663. name = TTR("[unsaved]");
  664. } else if (script->is_built_in()) {
  665. const String &script_name = script->get_name();
  666. if (!script_name.is_empty()) {
  667. // If the built-in script has a custom resource name defined,
  668. // display the built-in script name as follows: `ResourceName (scene_file.tscn)`
  669. name = vformat("%s (%s)", script_name, name.get_slice("::", 0));
  670. }
  671. }
  672. if (is_unsaved()) {
  673. name += "(*)";
  674. }
  675. return name;
  676. }
  677. Ref<Texture2D> ScriptTextEditor::get_theme_icon() {
  678. if (get_parent_control()) {
  679. String icon_name = script->get_class();
  680. if (script->is_built_in()) {
  681. icon_name += "Internal";
  682. }
  683. if (get_parent_control()->has_theme_icon(icon_name, EditorStringName(EditorIcons))) {
  684. return get_parent_control()->get_editor_theme_icon(icon_name);
  685. } else if (get_parent_control()->has_theme_icon(script->get_class(), EditorStringName(EditorIcons))) {
  686. return get_parent_control()->get_editor_theme_icon(script->get_class());
  687. }
  688. }
  689. return Ref<Texture2D>();
  690. }
  691. void ScriptTextEditor::_validate_script() {
  692. CodeEdit *te = code_editor->get_text_editor();
  693. String text = te->get_text();
  694. List<String> fnc;
  695. warnings.clear();
  696. errors.clear();
  697. depended_errors.clear();
  698. safe_lines.clear();
  699. if (!script->get_language()->validate(text, script->get_path(), &fnc, &errors, &warnings, &safe_lines)) {
  700. List<ScriptLanguage::ScriptError>::Element *E = errors.front();
  701. while (E) {
  702. List<ScriptLanguage::ScriptError>::Element *next_E = E->next();
  703. if ((E->get().path.is_empty() && !script->get_path().is_empty()) || E->get().path != script->get_path()) {
  704. depended_errors[E->get().path].push_back(E->get());
  705. E->erase();
  706. }
  707. E = next_E;
  708. }
  709. if (errors.size() > 0) {
  710. // TRANSLATORS: Script error pointing to a line and column number.
  711. String error_text = vformat(TTR("Error at (%d, %d):"), errors.front()->get().line, errors.front()->get().column) + " " + errors.front()->get().message;
  712. code_editor->set_error(error_text);
  713. code_editor->set_error_pos(errors.front()->get().line - 1, errors.front()->get().column - 1);
  714. }
  715. script_is_valid = false;
  716. } else {
  717. code_editor->set_error("");
  718. if (!script->is_tool()) {
  719. script->set_source_code(text);
  720. script->update_exports();
  721. te->get_syntax_highlighter()->update_cache();
  722. }
  723. functions.clear();
  724. for (const String &E : fnc) {
  725. functions.push_back(E);
  726. }
  727. script_is_valid = true;
  728. }
  729. _update_connected_methods();
  730. _update_warnings();
  731. _update_errors();
  732. _update_background_color();
  733. emit_signal(SNAME("name_changed"));
  734. emit_signal(SNAME("edited_script_changed"));
  735. }
  736. void ScriptTextEditor::_update_warnings() {
  737. int warning_nb = warnings.size();
  738. warnings_panel->clear();
  739. bool has_connections_table = false;
  740. // Add missing connections.
  741. if (GLOBAL_GET("debug/gdscript/warnings/enable")) {
  742. Node *base = get_tree()->get_edited_scene_root();
  743. if (base && missing_connections.size() > 0) {
  744. has_connections_table = true;
  745. warnings_panel->push_table(1);
  746. for (const Connection &connection : missing_connections) {
  747. String base_path = base->get_name();
  748. String source_path = base == connection.signal.get_object() ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.signal.get_object()));
  749. String target_path = base == connection.callable.get_object() ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.callable.get_object()));
  750. warnings_panel->push_cell();
  751. warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  752. warnings_panel->add_text(vformat(TTR("Missing connected method '%s' for signal '%s' from node '%s' to node '%s'."), connection.callable.get_method(), connection.signal.get_name(), source_path, target_path));
  753. warnings_panel->pop(); // Color.
  754. warnings_panel->pop(); // Cell.
  755. }
  756. warnings_panel->pop(); // Table.
  757. warning_nb += missing_connections.size();
  758. }
  759. }
  760. code_editor->set_warning_count(warning_nb);
  761. if (has_connections_table) {
  762. warnings_panel->add_newline();
  763. }
  764. // Add script warnings.
  765. warnings_panel->push_table(3);
  766. for (const ScriptLanguage::Warning &w : warnings) {
  767. Dictionary ignore_meta;
  768. ignore_meta["line"] = w.start_line;
  769. ignore_meta["code"] = w.string_code.to_lower();
  770. warnings_panel->push_cell();
  771. warnings_panel->push_meta(ignore_meta);
  772. warnings_panel->push_color(
  773. warnings_panel->get_theme_color(SNAME("accent_color"), EditorStringName(Editor)).lerp(warnings_panel->get_theme_color(SNAME("mono_color"), EditorStringName(Editor)), 0.5f));
  774. warnings_panel->add_text(TTR("[Ignore]"));
  775. warnings_panel->pop(); // Color.
  776. warnings_panel->pop(); // Meta ignore.
  777. warnings_panel->pop(); // Cell.
  778. warnings_panel->push_cell();
  779. warnings_panel->push_meta(w.start_line - 1);
  780. warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  781. warnings_panel->add_text(vformat(TTR("Line %d (%s):"), w.start_line, w.string_code));
  782. warnings_panel->pop(); // Color.
  783. warnings_panel->pop(); // Meta goto.
  784. warnings_panel->pop(); // Cell.
  785. warnings_panel->push_cell();
  786. warnings_panel->add_text(w.message);
  787. warnings_panel->add_newline();
  788. warnings_panel->pop(); // Cell.
  789. }
  790. warnings_panel->pop(); // Table.
  791. }
  792. void ScriptTextEditor::_update_errors() {
  793. code_editor->set_error_count(errors.size());
  794. errors_panel->clear();
  795. errors_panel->push_table(2);
  796. for (const ScriptLanguage::ScriptError &err : errors) {
  797. Dictionary click_meta;
  798. click_meta["line"] = err.line;
  799. click_meta["column"] = err.column;
  800. errors_panel->push_cell();
  801. errors_panel->push_meta(err.line - 1);
  802. errors_panel->push_color(warnings_panel->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  803. errors_panel->add_text(vformat(TTR("Line %d:"), err.line));
  804. errors_panel->pop(); // Color.
  805. errors_panel->pop(); // Meta goto.
  806. errors_panel->pop(); // Cell.
  807. errors_panel->push_cell();
  808. errors_panel->add_text(err.message);
  809. errors_panel->add_newline();
  810. errors_panel->pop(); // Cell.
  811. }
  812. errors_panel->pop(); // Table
  813. for (const KeyValue<String, List<ScriptLanguage::ScriptError>> &KV : depended_errors) {
  814. Dictionary click_meta;
  815. click_meta["path"] = KV.key;
  816. click_meta["line"] = 1;
  817. errors_panel->add_newline();
  818. errors_panel->add_newline();
  819. errors_panel->push_meta(click_meta);
  820. errors_panel->add_text(vformat(R"(%s:)", KV.key));
  821. errors_panel->pop(); // Meta goto.
  822. errors_panel->add_newline();
  823. errors_panel->push_indent(1);
  824. errors_panel->push_table(2);
  825. String filename = KV.key.get_file();
  826. for (const ScriptLanguage::ScriptError &err : KV.value) {
  827. click_meta["line"] = err.line;
  828. click_meta["column"] = err.column;
  829. errors_panel->push_cell();
  830. errors_panel->push_meta(click_meta);
  831. errors_panel->push_color(errors_panel->get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  832. errors_panel->add_text(vformat(TTR("Line %d:"), err.line));
  833. errors_panel->pop(); // Color.
  834. errors_panel->pop(); // Meta goto.
  835. errors_panel->pop(); // Cell.
  836. errors_panel->push_cell();
  837. errors_panel->add_text(err.message);
  838. errors_panel->pop(); // Cell.
  839. }
  840. errors_panel->pop(); // Table
  841. errors_panel->pop(); // Indent.
  842. }
  843. bool highlight_safe = EDITOR_GET("text_editor/appearance/gutters/highlight_type_safe_lines");
  844. bool last_is_safe = false;
  845. CodeEdit *te = code_editor->get_text_editor();
  846. for (int i = 0; i < te->get_line_count(); i++) {
  847. if (highlight_safe) {
  848. if (safe_lines.has(i + 1)) {
  849. te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  850. last_is_safe = true;
  851. } else if (last_is_safe && (te->is_in_comment(i) != -1 || te->get_line(i).strip_edges().is_empty())) {
  852. te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  853. } else {
  854. te->set_line_gutter_item_color(i, line_number_gutter, default_line_number_color);
  855. last_is_safe = false;
  856. }
  857. } else {
  858. te->set_line_gutter_item_color(i, 1, default_line_number_color);
  859. }
  860. }
  861. }
  862. void ScriptTextEditor::_update_bookmark_list() {
  863. bookmarks_menu->clear();
  864. bookmarks_menu->reset_size();
  865. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  866. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  867. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  868. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  869. PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
  870. if (bookmark_list.is_empty()) {
  871. return;
  872. }
  873. bookmarks_menu->add_separator();
  874. for (int i = 0; i < bookmark_list.size(); i++) {
  875. // Strip edges to remove spaces or tabs.
  876. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  877. String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges();
  878. // Limit the size of the line if too big.
  879. if (line.length() > 50) {
  880. line = line.substr(0, 50);
  881. }
  882. bookmarks_menu->add_item(String::num_int64(bookmark_list[i] + 1) + " - `" + line + "`");
  883. bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
  884. }
  885. }
  886. void ScriptTextEditor::_bookmark_item_pressed(int p_idx) {
  887. if (p_idx < 4) { // Any item before the separator.
  888. _edit_option(bookmarks_menu->get_item_id(p_idx));
  889. } else {
  890. code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
  891. }
  892. }
  893. static Vector<Node *> _find_all_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  894. Vector<Node *> nodes;
  895. if (p_current->get_owner() != p_base && p_base != p_current) {
  896. return nodes;
  897. }
  898. Ref<Script> c = p_current->get_script();
  899. if (c == p_script) {
  900. nodes.push_back(p_current);
  901. }
  902. for (int i = 0; i < p_current->get_child_count(); i++) {
  903. Vector<Node *> found = _find_all_node_for_script(p_base, p_current->get_child(i), p_script);
  904. nodes.append_array(found);
  905. }
  906. return nodes;
  907. }
  908. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  909. if (p_current->get_owner() != p_base && p_base != p_current) {
  910. return nullptr;
  911. }
  912. Ref<Script> c = p_current->get_script();
  913. if (c == p_script) {
  914. return p_current;
  915. }
  916. for (int i = 0; i < p_current->get_child_count(); i++) {
  917. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  918. if (found) {
  919. return found;
  920. }
  921. }
  922. return nullptr;
  923. }
  924. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, HashSet<Ref<Script>> &r_scripts) {
  925. if (p_current->get_owner() != p_base && p_base != p_current) {
  926. return;
  927. }
  928. Ref<Script> c = p_current->get_script();
  929. if (c.is_valid()) {
  930. r_scripts.insert(c);
  931. }
  932. for (int i = 0; i < p_current->get_child_count(); i++) {
  933. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  934. }
  935. }
  936. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  937. bool use_external_editor = bool(EDITOR_GET("text_editor/external/use_external_editor"));
  938. ERR_FAIL_NULL(get_tree());
  939. HashSet<Ref<Script>> scripts;
  940. Node *base = get_tree()->get_edited_scene_root();
  941. if (base) {
  942. _find_changed_scripts_for_external_editor(base, base, scripts);
  943. }
  944. for (const Ref<Script> &E : scripts) {
  945. Ref<Script> scr = E;
  946. if (!use_external_editor && !scr->get_language()->overrides_external_editor()) {
  947. continue; // We're not using an external editor for this script.
  948. }
  949. if (p_for_script.is_valid() && p_for_script != scr) {
  950. continue;
  951. }
  952. if (scr->is_built_in()) {
  953. continue; //internal script, who cares, though weird
  954. }
  955. uint64_t last_date = scr->get_last_modified_time();
  956. uint64_t date = FileAccess::get_modified_time(scr->get_path());
  957. if (last_date != date) {
  958. Ref<Script> rel_scr = ResourceLoader::load(scr->get_path(), scr->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  959. ERR_CONTINUE(rel_scr.is_null());
  960. scr->set_source_code(rel_scr->get_source_code());
  961. scr->set_last_modified_time(rel_scr->get_last_modified_time());
  962. scr->update_exports();
  963. trigger_live_script_reload(scr->get_path());
  964. }
  965. }
  966. }
  967. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
  968. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  969. ste->_code_complete_script(p_code, r_options, r_force);
  970. }
  971. void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
  972. if (color_panel->is_visible()) {
  973. return;
  974. }
  975. Node *base = get_tree()->get_edited_scene_root();
  976. if (base) {
  977. base = _find_node_for_script(base, base, script);
  978. }
  979. String hint;
  980. Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint);
  981. if (err == OK) {
  982. code_editor->get_text_editor()->set_code_hint(hint);
  983. }
  984. }
  985. void ScriptTextEditor::_update_breakpoint_list() {
  986. breakpoints_menu->clear();
  987. breakpoints_menu->reset_size();
  988. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  989. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  990. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  991. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  992. PackedInt32Array breakpoint_list = code_editor->get_text_editor()->get_breakpointed_lines();
  993. if (breakpoint_list.is_empty()) {
  994. return;
  995. }
  996. breakpoints_menu->add_separator();
  997. for (int i = 0; i < breakpoint_list.size(); i++) {
  998. // Strip edges to remove spaces or tabs.
  999. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  1000. String line = code_editor->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
  1001. // Limit the size of the line if too big.
  1002. if (line.length() > 50) {
  1003. line = line.substr(0, 50);
  1004. }
  1005. breakpoints_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`");
  1006. breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]);
  1007. }
  1008. }
  1009. void ScriptTextEditor::_breakpoint_item_pressed(int p_idx) {
  1010. if (p_idx < 4) { // Any item before the separator.
  1011. _edit_option(breakpoints_menu->get_item_id(p_idx));
  1012. } else {
  1013. code_editor->goto_line_centered(breakpoints_menu->get_item_metadata(p_idx));
  1014. }
  1015. }
  1016. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  1017. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_editor()->is_line_breakpointed(p_row));
  1018. }
  1019. void ScriptTextEditor::_on_caret_moved() {
  1020. if (code_editor->is_previewing_navigation_change()) {
  1021. return;
  1022. }
  1023. int current_line = code_editor->get_text_editor()->get_caret_line();
  1024. if (Math::abs(current_line - previous_line) >= 10) {
  1025. Dictionary nav_state = get_navigation_state();
  1026. nav_state["row"] = previous_line;
  1027. nav_state["scroll_position"] = -1;
  1028. emit_signal(SNAME("request_save_previous_state"), nav_state);
  1029. store_previous_state();
  1030. }
  1031. previous_line = current_line;
  1032. }
  1033. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  1034. Node *base = get_tree()->get_edited_scene_root();
  1035. if (base) {
  1036. base = _find_node_for_script(base, base, script);
  1037. }
  1038. ScriptLanguage::LookupResult result;
  1039. String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column);
  1040. Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result);
  1041. if (ScriptServer::is_global_class(p_symbol)) {
  1042. EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol));
  1043. } else if (p_symbol.is_resource_file() || p_symbol.begins_with("uid://")) {
  1044. EditorNode::get_singleton()->load_scene_or_resource(p_symbol);
  1045. } else if (lc_error == OK) {
  1046. _goto_line(p_row);
  1047. if (!result.class_name.is_empty() && EditorHelp::get_doc_data()->class_list.has(result.class_name) && !EditorHelp::get_doc_data()->class_list[result.class_name].is_script_doc) {
  1048. switch (result.type) {
  1049. case ScriptLanguage::LOOKUP_RESULT_CLASS: {
  1050. emit_signal(SNAME("go_to_help"), "class_name:" + result.class_name);
  1051. } break;
  1052. case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: {
  1053. StringName cname = result.class_name;
  1054. while (ClassDB::class_exists(cname)) {
  1055. if (ClassDB::has_integer_constant(cname, result.class_member, true)) {
  1056. result.class_name = cname;
  1057. break;
  1058. }
  1059. cname = ClassDB::get_parent_class(cname);
  1060. }
  1061. emit_signal(SNAME("go_to_help"), "class_constant:" + result.class_name + ":" + result.class_member);
  1062. } break;
  1063. case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: {
  1064. StringName cname = result.class_name;
  1065. while (ClassDB::class_exists(cname)) {
  1066. if (ClassDB::has_property(cname, result.class_member, true)) {
  1067. result.class_name = cname;
  1068. break;
  1069. }
  1070. cname = ClassDB::get_parent_class(cname);
  1071. }
  1072. emit_signal(SNAME("go_to_help"), "class_property:" + result.class_name + ":" + result.class_member);
  1073. } break;
  1074. case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: {
  1075. StringName cname = result.class_name;
  1076. while (ClassDB::class_exists(cname)) {
  1077. if (ClassDB::has_method(cname, result.class_member, true)) {
  1078. result.class_name = cname;
  1079. break;
  1080. }
  1081. cname = ClassDB::get_parent_class(cname);
  1082. }
  1083. emit_signal(SNAME("go_to_help"), "class_method:" + result.class_name + ":" + result.class_member);
  1084. } break;
  1085. case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: {
  1086. StringName cname = result.class_name;
  1087. while (ClassDB::class_exists(cname)) {
  1088. if (ClassDB::has_signal(cname, result.class_member, true)) {
  1089. result.class_name = cname;
  1090. break;
  1091. }
  1092. cname = ClassDB::get_parent_class(cname);
  1093. }
  1094. emit_signal(SNAME("go_to_help"), "class_signal:" + result.class_name + ":" + result.class_member);
  1095. } break;
  1096. case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: {
  1097. StringName cname = result.class_name;
  1098. while (ClassDB::class_exists(cname)) {
  1099. if (ClassDB::has_enum(cname, result.class_member, true)) {
  1100. result.class_name = cname;
  1101. break;
  1102. }
  1103. cname = ClassDB::get_parent_class(cname);
  1104. }
  1105. emit_signal(SNAME("go_to_help"), "class_enum:" + result.class_name + ":" + result.class_member);
  1106. } break;
  1107. case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: {
  1108. emit_signal(SNAME("go_to_help"), "class_annotation:" + result.class_name + ":" + result.class_member);
  1109. } break;
  1110. case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: { // Deprecated.
  1111. emit_signal(SNAME("go_to_help"), "class_global:" + result.class_name + ":" + result.class_member);
  1112. } break;
  1113. case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION:
  1114. case ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT:
  1115. case ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE:
  1116. case ScriptLanguage::LOOKUP_RESULT_MAX: {
  1117. // Nothing to do.
  1118. } break;
  1119. }
  1120. } else if (result.location >= 0) {
  1121. if (result.script.is_valid()) {
  1122. emit_signal(SNAME("request_open_script_at_line"), result.script, result.location - 1);
  1123. } else {
  1124. emit_signal(SNAME("request_save_history"));
  1125. goto_line_centered(result.location - 1);
  1126. }
  1127. }
  1128. } else if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  1129. // Check for Autoload scenes.
  1130. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  1131. if (info.is_singleton) {
  1132. EditorNode::get_singleton()->load_scene(info.path);
  1133. }
  1134. } else if (p_symbol.is_relative_path()) {
  1135. // Every symbol other than absolute path is relative path so keep this condition at last.
  1136. String path = _get_absolute_path(p_symbol);
  1137. if (FileAccess::exists(path)) {
  1138. EditorNode::get_singleton()->load_scene_or_resource(path);
  1139. }
  1140. }
  1141. }
  1142. void ScriptTextEditor::_validate_symbol(const String &p_symbol) {
  1143. CodeEdit *text_edit = code_editor->get_text_editor();
  1144. Node *base = get_tree()->get_edited_scene_root();
  1145. if (base) {
  1146. base = _find_node_for_script(base, base, script);
  1147. }
  1148. ScriptLanguage::LookupResult result;
  1149. String lc_text = code_editor->get_text_editor()->get_text_for_symbol_lookup();
  1150. Error lc_error = script->get_language()->lookup_code(lc_text, p_symbol, script->get_path(), base, result);
  1151. bool is_singleton = ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton;
  1152. if (lc_error == OK || is_singleton || ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || p_symbol.begins_with("uid://")) {
  1153. text_edit->set_symbol_lookup_word_as_valid(true);
  1154. } else if (p_symbol.is_relative_path()) {
  1155. String path = _get_absolute_path(p_symbol);
  1156. if (FileAccess::exists(path)) {
  1157. text_edit->set_symbol_lookup_word_as_valid(true);
  1158. } else {
  1159. text_edit->set_symbol_lookup_word_as_valid(false);
  1160. }
  1161. } else {
  1162. text_edit->set_symbol_lookup_word_as_valid(false);
  1163. }
  1164. }
  1165. void ScriptTextEditor::_show_symbol_tooltip(const String &p_symbol, int p_row, int p_column) {
  1166. if (!EDITOR_GET("text_editor/behavior/documentation/enable_tooltips").booleanize()) {
  1167. return;
  1168. }
  1169. if (p_symbol.begins_with("res://") || p_symbol.begins_with("uid://")) {
  1170. EditorHelpBitTooltip::show_tooltip(code_editor->get_text_editor(), "resource||" + p_symbol);
  1171. return;
  1172. }
  1173. Node *base = get_tree()->get_edited_scene_root();
  1174. if (base) {
  1175. base = _find_node_for_script(base, base, script);
  1176. }
  1177. ScriptLanguage::LookupResult result;
  1178. String doc_symbol;
  1179. const String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column);
  1180. const Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result);
  1181. if (lc_error == OK) {
  1182. switch (result.type) {
  1183. case ScriptLanguage::LOOKUP_RESULT_CLASS: {
  1184. doc_symbol = "class|" + result.class_name + "|";
  1185. } break;
  1186. case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: {
  1187. StringName cname = result.class_name;
  1188. while (ClassDB::class_exists(cname)) {
  1189. if (ClassDB::has_integer_constant(cname, result.class_member, true)) {
  1190. result.class_name = cname;
  1191. break;
  1192. }
  1193. cname = ClassDB::get_parent_class(cname);
  1194. }
  1195. doc_symbol = "constant|" + result.class_name + "|" + result.class_member;
  1196. } break;
  1197. case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: {
  1198. StringName cname = result.class_name;
  1199. while (ClassDB::class_exists(cname)) {
  1200. if (ClassDB::has_property(cname, result.class_member, true)) {
  1201. result.class_name = cname;
  1202. break;
  1203. }
  1204. cname = ClassDB::get_parent_class(cname);
  1205. }
  1206. doc_symbol = "property|" + result.class_name + "|" + result.class_member;
  1207. } break;
  1208. case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: {
  1209. StringName cname = result.class_name;
  1210. while (ClassDB::class_exists(cname)) {
  1211. if (ClassDB::has_method(cname, result.class_member, true)) {
  1212. result.class_name = cname;
  1213. break;
  1214. }
  1215. cname = ClassDB::get_parent_class(cname);
  1216. }
  1217. doc_symbol = "method|" + result.class_name + "|" + result.class_member;
  1218. } break;
  1219. case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: {
  1220. StringName cname = result.class_name;
  1221. while (ClassDB::class_exists(cname)) {
  1222. if (ClassDB::has_signal(cname, result.class_member, true)) {
  1223. result.class_name = cname;
  1224. break;
  1225. }
  1226. cname = ClassDB::get_parent_class(cname);
  1227. }
  1228. doc_symbol = "signal|" + result.class_name + "|" + result.class_member;
  1229. } break;
  1230. case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: {
  1231. StringName cname = result.class_name;
  1232. while (ClassDB::class_exists(cname)) {
  1233. if (ClassDB::has_enum(cname, result.class_member, true)) {
  1234. result.class_name = cname;
  1235. break;
  1236. }
  1237. cname = ClassDB::get_parent_class(cname);
  1238. }
  1239. doc_symbol = "enum|" + result.class_name + "|" + result.class_member;
  1240. } break;
  1241. case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: {
  1242. doc_symbol = "annotation|" + result.class_name + "|" + result.class_member;
  1243. } break;
  1244. case ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT:
  1245. case ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE: {
  1246. const String item_type = (result.type == ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT) ? "local_constant" : "local_variable";
  1247. Dictionary item_data;
  1248. item_data["description"] = result.description;
  1249. item_data["is_deprecated"] = result.is_deprecated;
  1250. item_data["deprecated_message"] = result.deprecated_message;
  1251. item_data["is_experimental"] = result.is_experimental;
  1252. item_data["experimental_message"] = result.experimental_message;
  1253. item_data["doc_type"] = result.doc_type;
  1254. item_data["enumeration"] = result.enumeration;
  1255. item_data["is_bitfield"] = result.is_bitfield;
  1256. item_data["value"] = result.value;
  1257. doc_symbol = item_type + "||" + p_symbol + "|" + JSON::stringify(item_data);
  1258. } break;
  1259. case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION:
  1260. case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: // Deprecated.
  1261. case ScriptLanguage::LOOKUP_RESULT_MAX: {
  1262. // Nothing to do.
  1263. } break;
  1264. }
  1265. }
  1266. // NOTE: See also `ScriptEditor::_get_debug_tooltip()` for documentation tooltips disabled.
  1267. String debug_value = EditorDebuggerNode::get_singleton()->get_var_value(p_symbol);
  1268. if (!debug_value.is_empty()) {
  1269. constexpr int DISPLAY_LIMIT = 1024;
  1270. if (debug_value.size() > DISPLAY_LIMIT) {
  1271. debug_value = debug_value.left(DISPLAY_LIMIT) + "... " + TTR("(truncated)");
  1272. }
  1273. debug_value = TTR("Current value: ") + debug_value.replace("[", "[lb]");
  1274. }
  1275. if (!doc_symbol.is_empty() || !debug_value.is_empty()) {
  1276. EditorHelpBitTooltip::show_tooltip(code_editor->get_text_editor(), doc_symbol, debug_value, true);
  1277. }
  1278. }
  1279. String ScriptTextEditor::_get_absolute_path(const String &rel_path) {
  1280. String base_path = script->get_path().get_base_dir();
  1281. String path = base_path.path_join(rel_path);
  1282. return path.replace("///", "//").simplify_path();
  1283. }
  1284. void ScriptTextEditor::update_toggle_files_button() {
  1285. code_editor->update_toggle_files_button();
  1286. }
  1287. void ScriptTextEditor::_update_connected_methods() {
  1288. CodeEdit *text_edit = code_editor->get_text_editor();
  1289. text_edit->set_gutter_width(connection_gutter, text_edit->get_line_height());
  1290. for (int i = 0; i < text_edit->get_line_count(); i++) {
  1291. text_edit->set_line_gutter_metadata(i, connection_gutter, Dictionary());
  1292. text_edit->set_line_gutter_icon(i, connection_gutter, nullptr);
  1293. text_edit->set_line_gutter_clickable(i, connection_gutter, false);
  1294. }
  1295. missing_connections.clear();
  1296. if (!script_is_valid) {
  1297. return;
  1298. }
  1299. Node *base = get_tree()->get_edited_scene_root();
  1300. if (!base) {
  1301. return;
  1302. }
  1303. // Add connection icons to methods.
  1304. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  1305. HashSet<StringName> methods_found;
  1306. for (int i = 0; i < nodes.size(); i++) {
  1307. List<Connection> signal_connections;
  1308. nodes[i]->get_signals_connected_to_this(&signal_connections);
  1309. for (const Connection &connection : signal_connections) {
  1310. if (!(connection.flags & CONNECT_PERSIST)) {
  1311. continue;
  1312. }
  1313. // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree.
  1314. Node *source = Object::cast_to<Node>(connection.signal.get_object());
  1315. if (source && !source->is_inside_tree()) {
  1316. continue;
  1317. }
  1318. const StringName method = connection.callable.get_method();
  1319. if (methods_found.has(method)) {
  1320. continue;
  1321. }
  1322. if (!ClassDB::has_method(script->get_instance_base_type(), method)) {
  1323. int line = -1;
  1324. for (int j = 0; j < functions.size(); j++) {
  1325. String name = functions[j].get_slicec(':', 0);
  1326. if (name == method) {
  1327. Dictionary line_meta;
  1328. line_meta["type"] = "connection";
  1329. line_meta["method"] = method;
  1330. line = functions[j].get_slicec(':', 1).to_int() - 1;
  1331. text_edit->set_line_gutter_metadata(line, connection_gutter, line_meta);
  1332. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("Slot")));
  1333. text_edit->set_line_gutter_clickable(line, connection_gutter, true);
  1334. methods_found.insert(method);
  1335. break;
  1336. }
  1337. }
  1338. if (line >= 0) {
  1339. continue;
  1340. }
  1341. // There is a chance that the method is inherited from another script.
  1342. bool found_inherited_function = false;
  1343. Ref<Script> inherited_script = script->get_base_script();
  1344. while (inherited_script.is_valid()) {
  1345. if (inherited_script->has_method(method)) {
  1346. found_inherited_function = true;
  1347. break;
  1348. }
  1349. inherited_script = inherited_script->get_base_script();
  1350. }
  1351. if (!found_inherited_function) {
  1352. missing_connections.push_back(connection);
  1353. }
  1354. }
  1355. }
  1356. }
  1357. // Add override icons to methods.
  1358. methods_found.clear();
  1359. for (int i = 0; i < functions.size(); i++) {
  1360. String raw_name = functions[i].get_slicec(':', 0);
  1361. StringName name = StringName(raw_name);
  1362. if (methods_found.has(name)) {
  1363. continue;
  1364. }
  1365. // Account for inner classes by stripping the class names from the method,
  1366. // starting from the right since our inner class might be inside of another inner class.
  1367. int pos = raw_name.rfind_char('.');
  1368. if (pos != -1) {
  1369. name = raw_name.substr(pos + 1);
  1370. }
  1371. String found_base_class;
  1372. StringName base_class = script->get_instance_base_type();
  1373. Ref<Script> inherited_script = script->get_base_script();
  1374. while (inherited_script.is_valid()) {
  1375. if (inherited_script->has_method(name)) {
  1376. found_base_class = "script:" + inherited_script->get_path();
  1377. break;
  1378. }
  1379. base_class = inherited_script->get_instance_base_type();
  1380. inherited_script = inherited_script->get_base_script();
  1381. }
  1382. if (found_base_class.is_empty()) {
  1383. while (base_class) {
  1384. List<MethodInfo> methods;
  1385. ClassDB::get_method_list(base_class, &methods, true);
  1386. for (const MethodInfo &mi : methods) {
  1387. if (mi.name == name) {
  1388. found_base_class = "builtin:" + base_class;
  1389. break;
  1390. }
  1391. }
  1392. ClassDB::ClassInfo *base_class_ptr = ClassDB::classes.getptr(base_class)->inherits_ptr;
  1393. if (base_class_ptr == nullptr) {
  1394. break;
  1395. }
  1396. base_class = base_class_ptr->name;
  1397. }
  1398. }
  1399. if (!found_base_class.is_empty()) {
  1400. int line = functions[i].get_slicec(':', 1).to_int() - 1;
  1401. Dictionary line_meta = text_edit->get_line_gutter_metadata(line, connection_gutter);
  1402. if (line_meta.is_empty()) {
  1403. // Add override icon to gutter.
  1404. line_meta["type"] = "inherits";
  1405. line_meta["method"] = name;
  1406. line_meta["base_class"] = found_base_class;
  1407. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverride")));
  1408. text_edit->set_line_gutter_clickable(line, connection_gutter, true);
  1409. } else {
  1410. // If method is also connected to signal, then merge icons and keep the click behavior of the slot.
  1411. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverrideAndSlot")));
  1412. }
  1413. methods_found.insert(StringName(raw_name));
  1414. }
  1415. }
  1416. }
  1417. void ScriptTextEditor::_update_gutter_indexes() {
  1418. for (int i = 0; i < code_editor->get_text_editor()->get_gutter_count(); i++) {
  1419. if (code_editor->get_text_editor()->get_gutter_name(i) == "connection_gutter") {
  1420. connection_gutter = i;
  1421. continue;
  1422. }
  1423. if (code_editor->get_text_editor()->get_gutter_name(i) == "line_numbers") {
  1424. line_number_gutter = i;
  1425. continue;
  1426. }
  1427. }
  1428. }
  1429. void ScriptTextEditor::_gutter_clicked(int p_line, int p_gutter) {
  1430. if (p_gutter != connection_gutter) {
  1431. return;
  1432. }
  1433. Dictionary meta = code_editor->get_text_editor()->get_line_gutter_metadata(p_line, p_gutter);
  1434. String type = meta.get("type", "");
  1435. if (type.is_empty()) {
  1436. return;
  1437. }
  1438. // All types currently need a method name.
  1439. String method = meta.get("method", "");
  1440. if (method.is_empty()) {
  1441. return;
  1442. }
  1443. if (type == "connection") {
  1444. Node *base = get_tree()->get_edited_scene_root();
  1445. if (!base) {
  1446. return;
  1447. }
  1448. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  1449. connection_info_dialog->popup_connections(method, nodes);
  1450. } else if (type == "inherits") {
  1451. String base_class_raw = meta["base_class"];
  1452. PackedStringArray base_class_split = base_class_raw.split(":", true, 1);
  1453. if (base_class_split[0] == "script") {
  1454. // Go to function declaration.
  1455. Ref<Script> base_script = ResourceLoader::load(base_class_split[1]);
  1456. ERR_FAIL_COND(base_script.is_null());
  1457. emit_signal(SNAME("go_to_method"), base_script, method);
  1458. } else if (base_class_split[0] == "builtin") {
  1459. // Open method documentation.
  1460. emit_signal(SNAME("go_to_help"), "class_method:" + base_class_split[1] + ":" + method);
  1461. }
  1462. }
  1463. }
  1464. void ScriptTextEditor::_edit_option(int p_op) {
  1465. CodeEdit *tx = code_editor->get_text_editor();
  1466. tx->apply_ime();
  1467. switch (p_op) {
  1468. case EDIT_UNDO: {
  1469. tx->undo();
  1470. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1471. } break;
  1472. case EDIT_REDO: {
  1473. tx->redo();
  1474. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1475. } break;
  1476. case EDIT_CUT: {
  1477. tx->cut();
  1478. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1479. } break;
  1480. case EDIT_COPY: {
  1481. tx->copy();
  1482. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1483. } break;
  1484. case EDIT_PASTE: {
  1485. tx->paste();
  1486. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1487. } break;
  1488. case EDIT_SELECT_ALL: {
  1489. tx->select_all();
  1490. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1491. } break;
  1492. case EDIT_MOVE_LINE_UP: {
  1493. code_editor->get_text_editor()->move_lines_up();
  1494. } break;
  1495. case EDIT_MOVE_LINE_DOWN: {
  1496. code_editor->get_text_editor()->move_lines_down();
  1497. } break;
  1498. case EDIT_INDENT: {
  1499. Ref<Script> scr = script;
  1500. if (scr.is_null()) {
  1501. return;
  1502. }
  1503. tx->indent_lines();
  1504. } break;
  1505. case EDIT_UNINDENT: {
  1506. Ref<Script> scr = script;
  1507. if (scr.is_null()) {
  1508. return;
  1509. }
  1510. tx->unindent_lines();
  1511. } break;
  1512. case EDIT_DELETE_LINE: {
  1513. code_editor->get_text_editor()->delete_lines();
  1514. } break;
  1515. case EDIT_DUPLICATE_SELECTION: {
  1516. code_editor->get_text_editor()->duplicate_selection();
  1517. } break;
  1518. case EDIT_DUPLICATE_LINES: {
  1519. code_editor->get_text_editor()->duplicate_lines();
  1520. } break;
  1521. case EDIT_TOGGLE_FOLD_LINE: {
  1522. tx->toggle_foldable_lines_at_carets();
  1523. } break;
  1524. case EDIT_FOLD_ALL_LINES: {
  1525. tx->fold_all_lines();
  1526. } break;
  1527. case EDIT_UNFOLD_ALL_LINES: {
  1528. tx->unfold_all_lines();
  1529. } break;
  1530. case EDIT_CREATE_CODE_REGION: {
  1531. tx->create_code_region();
  1532. } break;
  1533. case EDIT_TOGGLE_COMMENT: {
  1534. _edit_option_toggle_inline_comment();
  1535. } break;
  1536. case EDIT_COMPLETE: {
  1537. tx->request_code_completion(true);
  1538. } break;
  1539. case EDIT_AUTO_INDENT: {
  1540. String text = tx->get_text();
  1541. Ref<Script> scr = script;
  1542. if (scr.is_null()) {
  1543. return;
  1544. }
  1545. tx->begin_complex_operation();
  1546. tx->begin_multicaret_edit();
  1547. int begin = tx->get_line_count() - 1, end = 0;
  1548. if (tx->has_selection()) {
  1549. // Auto indent all lines that have a caret or selection on it.
  1550. Vector<Point2i> line_ranges = tx->get_line_ranges_from_carets();
  1551. for (Point2i line_range : line_ranges) {
  1552. scr->get_language()->auto_indent_code(text, line_range.x, line_range.y);
  1553. if (line_range.x < begin) {
  1554. begin = line_range.x;
  1555. }
  1556. if (line_range.y > end) {
  1557. end = line_range.y;
  1558. }
  1559. }
  1560. } else {
  1561. // Auto indent entire text.
  1562. begin = 0;
  1563. end = tx->get_line_count() - 1;
  1564. scr->get_language()->auto_indent_code(text, begin, end);
  1565. }
  1566. // Apply auto indented code.
  1567. Vector<String> lines = text.split("\n");
  1568. for (int i = begin; i <= end; ++i) {
  1569. tx->set_line(i, lines[i]);
  1570. }
  1571. tx->end_multicaret_edit();
  1572. tx->end_complex_operation();
  1573. } break;
  1574. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1575. trim_trailing_whitespace();
  1576. } break;
  1577. case EDIT_TRIM_FINAL_NEWLINES: {
  1578. trim_final_newlines();
  1579. } break;
  1580. case EDIT_CONVERT_INDENT_TO_SPACES: {
  1581. code_editor->set_indent_using_spaces(true);
  1582. convert_indent();
  1583. } break;
  1584. case EDIT_CONVERT_INDENT_TO_TABS: {
  1585. code_editor->set_indent_using_spaces(false);
  1586. convert_indent();
  1587. } break;
  1588. case EDIT_PICK_COLOR: {
  1589. color_panel->popup();
  1590. } break;
  1591. case EDIT_TO_UPPERCASE: {
  1592. _convert_case(CodeTextEditor::UPPER);
  1593. } break;
  1594. case EDIT_TO_LOWERCASE: {
  1595. _convert_case(CodeTextEditor::LOWER);
  1596. } break;
  1597. case EDIT_CAPITALIZE: {
  1598. _convert_case(CodeTextEditor::CAPITALIZE);
  1599. } break;
  1600. case EDIT_EVALUATE: {
  1601. Expression expression;
  1602. tx->begin_complex_operation();
  1603. for (int caret_idx = 0; caret_idx < tx->get_caret_count(); caret_idx++) {
  1604. Vector<String> lines = tx->get_selected_text(caret_idx).split("\n");
  1605. PackedStringArray results;
  1606. for (int i = 0; i < lines.size(); i++) {
  1607. const String &line = lines[i];
  1608. String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); // Extract the whitespace at the beginning.
  1609. if (expression.parse(line) == OK) {
  1610. Variant result = expression.execute(Array(), Variant(), false, true);
  1611. if (expression.get_error_text().is_empty()) {
  1612. results.push_back(whitespace + result.get_construct_string());
  1613. } else {
  1614. results.push_back(line);
  1615. }
  1616. } else {
  1617. results.push_back(line);
  1618. }
  1619. }
  1620. tx->insert_text_at_caret(String("\n").join(results), caret_idx);
  1621. }
  1622. tx->end_complex_operation();
  1623. } break;
  1624. case EDIT_TOGGLE_WORD_WRAP: {
  1625. TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();
  1626. code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
  1627. } break;
  1628. case SEARCH_FIND: {
  1629. code_editor->get_find_replace_bar()->popup_search();
  1630. } break;
  1631. case SEARCH_FIND_NEXT: {
  1632. code_editor->get_find_replace_bar()->search_next();
  1633. } break;
  1634. case SEARCH_FIND_PREV: {
  1635. code_editor->get_find_replace_bar()->search_prev();
  1636. } break;
  1637. case SEARCH_REPLACE: {
  1638. code_editor->get_find_replace_bar()->popup_replace();
  1639. } break;
  1640. case SEARCH_IN_FILES: {
  1641. String selected_text = tx->get_selected_text();
  1642. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  1643. // So this will be delegated to the ScriptEditor.
  1644. emit_signal(SNAME("search_in_files_requested"), selected_text);
  1645. } break;
  1646. case REPLACE_IN_FILES: {
  1647. String selected_text = tx->get_selected_text();
  1648. emit_signal(SNAME("replace_in_files_requested"), selected_text);
  1649. } break;
  1650. case SEARCH_LOCATE_FUNCTION: {
  1651. quick_open->popup_dialog(get_functions());
  1652. quick_open->set_title(TTR("Go to Function"));
  1653. } break;
  1654. case SEARCH_GOTO_LINE: {
  1655. goto_line_popup->popup_find_line(code_editor);
  1656. } break;
  1657. case BOOKMARK_TOGGLE: {
  1658. code_editor->toggle_bookmark();
  1659. } break;
  1660. case BOOKMARK_GOTO_NEXT: {
  1661. code_editor->goto_next_bookmark();
  1662. } break;
  1663. case BOOKMARK_GOTO_PREV: {
  1664. code_editor->goto_prev_bookmark();
  1665. } break;
  1666. case BOOKMARK_REMOVE_ALL: {
  1667. code_editor->remove_all_bookmarks();
  1668. } break;
  1669. case DEBUG_TOGGLE_BREAKPOINT: {
  1670. Vector<int> sorted_carets = tx->get_sorted_carets();
  1671. int last_line = -1;
  1672. for (const int &c : sorted_carets) {
  1673. int from = tx->get_selection_from_line(c);
  1674. from += from == last_line ? 1 : 0;
  1675. int to = tx->get_selection_to_line(c);
  1676. if (to < from) {
  1677. continue;
  1678. }
  1679. // Check first if there's any lines with breakpoints in the selection.
  1680. bool selection_has_breakpoints = false;
  1681. for (int line = from; line <= to; line++) {
  1682. if (tx->is_line_breakpointed(line)) {
  1683. selection_has_breakpoints = true;
  1684. break;
  1685. }
  1686. }
  1687. // Set breakpoint on caret or remove all bookmarks from the selection.
  1688. if (!selection_has_breakpoints) {
  1689. if (tx->get_caret_line(c) != last_line) {
  1690. tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
  1691. }
  1692. } else {
  1693. for (int line = from; line <= to; line++) {
  1694. tx->set_line_as_breakpoint(line, false);
  1695. }
  1696. }
  1697. last_line = to;
  1698. }
  1699. } break;
  1700. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1701. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1702. for (int i = 0; i < bpoints.size(); i++) {
  1703. int line = bpoints[i];
  1704. bool dobreak = !tx->is_line_breakpointed(line);
  1705. tx->set_line_as_breakpoint(line, dobreak);
  1706. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1707. }
  1708. } break;
  1709. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1710. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1711. if (bpoints.is_empty()) {
  1712. return;
  1713. }
  1714. int current_line = tx->get_caret_line();
  1715. int bpoint_idx = 0;
  1716. if (current_line < (int)bpoints[bpoints.size() - 1]) {
  1717. while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
  1718. bpoint_idx++;
  1719. }
  1720. }
  1721. code_editor->goto_line_centered(bpoints[bpoint_idx]);
  1722. } break;
  1723. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1724. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1725. if (bpoints.is_empty()) {
  1726. return;
  1727. }
  1728. int current_line = tx->get_caret_line();
  1729. int bpoint_idx = bpoints.size() - 1;
  1730. if (current_line > (int)bpoints[0]) {
  1731. while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
  1732. bpoint_idx--;
  1733. }
  1734. }
  1735. code_editor->goto_line_centered(bpoints[bpoint_idx]);
  1736. } break;
  1737. case HELP_CONTEXTUAL: {
  1738. String text = tx->get_selected_text(0);
  1739. if (text.is_empty()) {
  1740. text = tx->get_word_under_caret(0);
  1741. }
  1742. if (!text.is_empty()) {
  1743. emit_signal(SNAME("request_help"), text);
  1744. }
  1745. } break;
  1746. case LOOKUP_SYMBOL: {
  1747. String text = tx->get_word_under_caret(0);
  1748. if (text.is_empty()) {
  1749. text = tx->get_selected_text(0);
  1750. }
  1751. if (!text.is_empty()) {
  1752. _lookup_symbol(text, tx->get_caret_line(0), tx->get_caret_column(0));
  1753. }
  1754. } break;
  1755. case EDIT_EMOJI_AND_SYMBOL: {
  1756. code_editor->get_text_editor()->show_emoji_and_symbol_picker();
  1757. } break;
  1758. default: {
  1759. if (p_op >= EditorContextMenuPlugin::BASE_ID) {
  1760. EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, p_op, tx);
  1761. }
  1762. }
  1763. }
  1764. }
  1765. void ScriptTextEditor::_edit_option_toggle_inline_comment() {
  1766. if (script.is_null()) {
  1767. return;
  1768. }
  1769. String delimiter = "#";
  1770. for (const String &script_delimiter : script->get_language()->get_comment_delimiters()) {
  1771. if (!script_delimiter.contains_char(' ')) {
  1772. delimiter = script_delimiter;
  1773. break;
  1774. }
  1775. }
  1776. code_editor->toggle_inline_comment(delimiter);
  1777. }
  1778. void ScriptTextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1779. ERR_FAIL_COND(p_highlighter.is_null());
  1780. highlighters[p_highlighter->_get_name()] = p_highlighter;
  1781. highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
  1782. }
  1783. void ScriptTextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1784. ERR_FAIL_COND(p_highlighter.is_null());
  1785. HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin();
  1786. while (el) {
  1787. int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key);
  1788. highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter);
  1789. ++el;
  1790. }
  1791. CodeEdit *te = code_editor->get_text_editor();
  1792. p_highlighter->_set_edited_resource(script);
  1793. te->set_syntax_highlighter(p_highlighter);
  1794. }
  1795. void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
  1796. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  1797. }
  1798. void ScriptTextEditor::_notification(int p_what) {
  1799. switch (p_what) {
  1800. case NOTIFICATION_THEME_CHANGED:
  1801. if (!editor_enabled) {
  1802. break;
  1803. }
  1804. if (is_visible_in_tree()) {
  1805. _update_warnings();
  1806. _update_errors();
  1807. _update_background_color();
  1808. }
  1809. [[fallthrough]];
  1810. case NOTIFICATION_ENTER_TREE: {
  1811. code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_line_height());
  1812. Ref<Font> code_font = get_theme_font("font", "CodeEdit");
  1813. inline_color_options->add_theme_font_override("font", code_font);
  1814. inline_color_options->get_popup()->add_theme_font_override("font", code_font);
  1815. } break;
  1816. }
  1817. }
  1818. Control *ScriptTextEditor::get_edit_menu() {
  1819. return edit_hb;
  1820. }
  1821. void ScriptTextEditor::clear_edit_menu() {
  1822. if (editor_enabled) {
  1823. memdelete(edit_hb);
  1824. }
  1825. }
  1826. void ScriptTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  1827. code_editor->set_find_replace_bar(p_bar);
  1828. }
  1829. void ScriptTextEditor::reload(bool p_soft) {
  1830. CodeEdit *te = code_editor->get_text_editor();
  1831. Ref<Script> scr = script;
  1832. if (scr.is_null()) {
  1833. return;
  1834. }
  1835. scr->set_source_code(te->get_text());
  1836. bool soft = p_soft || ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin"); // Always soft-reload editor plugins.
  1837. scr->get_language()->reload_tool_script(scr, soft);
  1838. }
  1839. PackedInt32Array ScriptTextEditor::get_breakpoints() {
  1840. return code_editor->get_text_editor()->get_breakpointed_lines();
  1841. }
  1842. void ScriptTextEditor::set_breakpoint(int p_line, bool p_enabled) {
  1843. code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
  1844. }
  1845. void ScriptTextEditor::clear_breakpoints() {
  1846. code_editor->get_text_editor()->clear_breakpointed_lines();
  1847. }
  1848. void ScriptTextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
  1849. Variant args[1] = { this };
  1850. const Variant *argp[] = { &args[0] };
  1851. code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
  1852. }
  1853. void ScriptTextEditor::set_debugger_active(bool p_active) {
  1854. }
  1855. Control *ScriptTextEditor::get_base_editor() const {
  1856. return code_editor->get_text_editor();
  1857. }
  1858. CodeTextEditor *ScriptTextEditor::get_code_editor() const {
  1859. return code_editor;
  1860. }
  1861. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1862. return Variant();
  1863. }
  1864. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1865. Dictionary d = p_data;
  1866. if (d.has("type") &&
  1867. (String(d["type"]) == "resource" ||
  1868. String(d["type"]) == "files" ||
  1869. String(d["type"]) == "nodes" ||
  1870. String(d["type"]) == "obj_property" ||
  1871. String(d["type"]) == "files_and_dirs")) {
  1872. return true;
  1873. }
  1874. return false;
  1875. }
  1876. static Node *_find_script_node(Node *p_current_node, const Ref<Script> &script) {
  1877. if (p_current_node->get_script() == script) {
  1878. return p_current_node;
  1879. }
  1880. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1881. Node *n = _find_script_node(p_current_node->get_child(i), script);
  1882. if (n) {
  1883. return n;
  1884. }
  1885. }
  1886. return nullptr;
  1887. }
  1888. static String _quote_drop_data(const String &str) {
  1889. // This function prepares a string for being "dropped" into the script editor.
  1890. // The string can be a resource path, node path or property name.
  1891. const bool using_single_quotes = EDITOR_GET("text_editor/completion/use_single_quotes");
  1892. String escaped = str.c_escape();
  1893. // If string is double quoted, there is no need to escape single quotes.
  1894. // We can revert the extra escaping added in c_escape().
  1895. if (!using_single_quotes) {
  1896. escaped = escaped.replace("\\'", "\'");
  1897. }
  1898. return escaped.quote(using_single_quotes ? "'" : "\"");
  1899. }
  1900. static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p_create_field, bool p_allow_uid) {
  1901. String path = p_resource->get_path();
  1902. if (p_allow_uid) {
  1903. ResourceUID::ID id = ResourceLoader::get_resource_uid(path);
  1904. if (id != ResourceUID::INVALID_ID) {
  1905. path = ResourceUID::get_singleton()->id_to_text(id);
  1906. }
  1907. }
  1908. const bool is_script = ClassDB::is_parent_class(p_resource->get_class(), "Script");
  1909. if (!p_create_field) {
  1910. return vformat("preload(%s)", _quote_drop_data(path));
  1911. }
  1912. String variable_name = p_resource->get_name();
  1913. if (variable_name.is_empty()) {
  1914. variable_name = p_resource->get_path().get_file().get_basename();
  1915. }
  1916. if (is_script) {
  1917. variable_name = variable_name.to_pascal_case().validate_unicode_identifier();
  1918. } else {
  1919. variable_name = variable_name.to_snake_case().to_upper().validate_unicode_identifier();
  1920. }
  1921. return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path));
  1922. }
  1923. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1924. Dictionary d = p_data;
  1925. CodeEdit *te = code_editor->get_text_editor();
  1926. Point2i pos = (p_point == Vector2(Math::INF, Math::INF)) ? Point2i(te->get_caret_line(0), te->get_caret_column(0)) : te->get_line_column_at_pos(p_point);
  1927. int drop_at_line = pos.y;
  1928. int drop_at_column = pos.x;
  1929. int selection_index = te->get_selection_at_line_column(drop_at_line, drop_at_column);
  1930. bool line_will_be_empty = false;
  1931. if (selection_index >= 0) {
  1932. // Dropped on a selection, it will be replaced.
  1933. drop_at_line = te->get_selection_from_line(selection_index);
  1934. drop_at_column = te->get_selection_from_column(selection_index);
  1935. line_will_be_empty = drop_at_column <= te->get_first_non_whitespace_column(drop_at_line) && te->get_selection_to_column(selection_index) == te->get_line(te->get_selection_to_line(selection_index)).length();
  1936. }
  1937. String text_to_drop;
  1938. const bool drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
  1939. const bool allow_uid = Input::get_singleton()->is_key_pressed(Key::SHIFT) != bool(EDITOR_GET("text_editor/behavior/files/drop_preload_resources_as_uid"));
  1940. const String &line = te->get_line(drop_at_line);
  1941. const bool is_empty_line = line_will_be_empty || line.is_empty() || te->get_first_non_whitespace_column(drop_at_line) == line.length();
  1942. const String type = d.get("type", "");
  1943. if (type == "resource") {
  1944. Ref<Resource> resource = d["resource"];
  1945. if (resource.is_null()) {
  1946. return;
  1947. }
  1948. const String &path = resource->get_path();
  1949. if (path.is_empty() || path.ends_with("::")) {
  1950. String warning = TTR("The resource does not have a valid path because it has not been saved.\nPlease save the scene or resource that contains this resource and try again.");
  1951. EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR);
  1952. return;
  1953. }
  1954. if (drop_modifier_pressed) {
  1955. if (resource->is_built_in()) {
  1956. String warning = TTR("Preloading internal resources is not supported.");
  1957. EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR);
  1958. } else {
  1959. text_to_drop = _get_dropped_resource_line(resource, is_empty_line, allow_uid);
  1960. }
  1961. } else {
  1962. text_to_drop = _quote_drop_data(path);
  1963. }
  1964. }
  1965. if (type == "files" || type == "files_and_dirs") {
  1966. const PackedStringArray files = d["files"];
  1967. PackedStringArray parts;
  1968. for (const String &path : files) {
  1969. if (drop_modifier_pressed && ResourceLoader::exists(path)) {
  1970. Ref<Resource> resource = ResourceLoader::load(path);
  1971. if (resource.is_null()) {
  1972. // Resource exists, but failed to load. We need only path and name, so we can use a dummy Resource instead.
  1973. resource.instantiate();
  1974. resource->set_path_cache(path);
  1975. }
  1976. parts.append(_get_dropped_resource_line(resource, is_empty_line, allow_uid));
  1977. } else {
  1978. parts.append(_quote_drop_data(path));
  1979. }
  1980. }
  1981. text_to_drop = String(is_empty_line ? "\n" : ", ").join(parts);
  1982. }
  1983. if (type == "nodes") {
  1984. Node *scene_root = get_tree()->get_edited_scene_root();
  1985. if (!scene_root) {
  1986. EditorNode::get_singleton()->show_warning(TTR("Can't drop nodes without an open scene."));
  1987. return;
  1988. }
  1989. if (!ClassDB::is_parent_class(script->get_instance_base_type(), "Node")) {
  1990. EditorToaster::get_singleton()->popup_str(vformat(TTR("Can't drop nodes because script '%s' does not inherit Node."), get_name()), EditorToaster::SEVERITY_WARNING);
  1991. return;
  1992. }
  1993. Node *sn = _find_script_node(scene_root, script);
  1994. if (!sn) {
  1995. sn = scene_root;
  1996. }
  1997. Array nodes = d["nodes"];
  1998. if (drop_modifier_pressed) {
  1999. const bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
  2000. for (int i = 0; i < nodes.size(); i++) {
  2001. NodePath np = nodes[i];
  2002. Node *node = get_node(np);
  2003. if (!node) {
  2004. continue;
  2005. }
  2006. bool is_unique = node->is_unique_name_in_owner() && (node->get_owner() == sn || node->get_owner() == sn->get_owner());
  2007. String path = is_unique ? String(node->get_name()) : String(sn->get_path_to(node));
  2008. for (const String &segment : path.split("/")) {
  2009. if (!segment.is_valid_unicode_identifier()) {
  2010. path = _quote_drop_data(path);
  2011. break;
  2012. }
  2013. }
  2014. String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier();
  2015. if (use_type) {
  2016. StringName class_name = node->get_class_name();
  2017. Ref<Script> node_script = node->get_script();
  2018. if (node_script.is_valid()) {
  2019. StringName global_node_script_name = node_script->get_global_name();
  2020. if (global_node_script_name != StringName()) {
  2021. class_name = global_node_script_name;
  2022. }
  2023. }
  2024. text_to_drop += vformat("@onready var %s: %s = %c%s\n", variable_name, class_name, is_unique ? '%' : '$', path);
  2025. } else {
  2026. text_to_drop += vformat("@onready var %s = %c%s\n", variable_name, is_unique ? '%' : '$', path);
  2027. }
  2028. }
  2029. } else {
  2030. for (int i = 0; i < nodes.size(); i++) {
  2031. if (i > 0) {
  2032. text_to_drop += ", ";
  2033. }
  2034. NodePath np = nodes[i];
  2035. Node *node = get_node(np);
  2036. if (!node) {
  2037. continue;
  2038. }
  2039. bool is_unique = node->is_unique_name_in_owner() && (node->get_owner() == sn || node->get_owner() == sn->get_owner());
  2040. String path = is_unique ? String(node->get_name()) : String(sn->get_path_to(node));
  2041. for (const String &segment : path.split("/")) {
  2042. if (!segment.is_valid_ascii_identifier()) {
  2043. path = _quote_drop_data(path);
  2044. break;
  2045. }
  2046. }
  2047. text_to_drop += (is_unique ? "%" : "$") + path;
  2048. }
  2049. }
  2050. }
  2051. if (type == "obj_property") {
  2052. bool add_literal = EDITOR_GET("text_editor/completion/add_node_path_literals");
  2053. text_to_drop = add_literal ? "^" : "";
  2054. // It is unclear whether properties may contain single or double quotes.
  2055. // Assume here that double-quotes may not exist. We are escaping single-quotes if necessary.
  2056. text_to_drop += _quote_drop_data(String(d["property"]));
  2057. }
  2058. if (text_to_drop.is_empty()) {
  2059. return;
  2060. }
  2061. // Remove drag caret before any actions so it is not included in undo.
  2062. te->remove_drag_caret();
  2063. te->begin_complex_operation();
  2064. if (selection_index >= 0) {
  2065. te->delete_selection(selection_index);
  2066. }
  2067. te->remove_secondary_carets();
  2068. te->deselect();
  2069. te->set_caret_line(drop_at_line);
  2070. te->set_caret_column(drop_at_column);
  2071. te->insert_text_at_caret(text_to_drop);
  2072. te->end_complex_operation();
  2073. te->grab_focus();
  2074. }
  2075. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  2076. Ref<InputEventMouseButton> mb = ev;
  2077. Ref<InputEventKey> k = ev;
  2078. Point2 local_pos;
  2079. bool create_menu = false;
  2080. CodeEdit *tx = code_editor->get_text_editor();
  2081. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  2082. local_pos = mb->get_global_position() - tx->get_global_position();
  2083. create_menu = true;
  2084. } else if (k.is_valid() && k->is_action("ui_menu", true)) {
  2085. tx->adjust_viewport_to_caret(0);
  2086. local_pos = tx->get_caret_draw_pos(0);
  2087. create_menu = true;
  2088. }
  2089. if (create_menu) {
  2090. tx->apply_ime();
  2091. Point2i pos = tx->get_line_column_at_pos(local_pos);
  2092. int mouse_line = pos.y;
  2093. int mouse_column = pos.x;
  2094. tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
  2095. int selection_clicked = -1;
  2096. if (tx->is_move_caret_on_right_click_enabled()) {
  2097. selection_clicked = tx->get_selection_at_line_column(mouse_line, mouse_column, true);
  2098. if (selection_clicked < 0) {
  2099. tx->deselect();
  2100. tx->remove_secondary_carets();
  2101. selection_clicked = 0;
  2102. tx->set_caret_line(mouse_line, false, false, -1);
  2103. tx->set_caret_column(mouse_column);
  2104. }
  2105. }
  2106. String word_at_pos = tx->get_word_at_pos(local_pos);
  2107. if (word_at_pos.is_empty()) {
  2108. word_at_pos = tx->get_word_under_caret(selection_clicked);
  2109. }
  2110. if (word_at_pos.is_empty()) {
  2111. word_at_pos = tx->get_selected_text(selection_clicked);
  2112. }
  2113. bool has_color = (word_at_pos == "Color");
  2114. bool foldable = tx->can_fold_line(mouse_line) || tx->is_line_folded(mouse_line);
  2115. bool open_docs = false;
  2116. bool goto_definition = false;
  2117. if (ScriptServer::is_global_class(word_at_pos) || word_at_pos.is_resource_file()) {
  2118. open_docs = true;
  2119. } else {
  2120. Node *base = get_tree()->get_edited_scene_root();
  2121. if (base) {
  2122. base = _find_node_for_script(base, base, script);
  2123. }
  2124. ScriptLanguage::LookupResult result;
  2125. if (script->get_language()->lookup_code(tx->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) {
  2126. open_docs = true;
  2127. }
  2128. }
  2129. if (has_color) {
  2130. String line = tx->get_line(mouse_line);
  2131. color_position.x = mouse_line;
  2132. color_position.y = mouse_column;
  2133. int begin = -1;
  2134. int end = -1;
  2135. enum EXPRESSION_PATTERNS {
  2136. NOT_PARSED,
  2137. RGBA_PARAMETER, // Color(float,float,float) or Color(float,float,float,float)
  2138. COLOR_NAME, // Color.COLOR_NAME
  2139. } expression_pattern = NOT_PARSED;
  2140. for (int i = mouse_column; i < line.length(); i++) {
  2141. if (line[i] == '(') {
  2142. if (expression_pattern == NOT_PARSED) {
  2143. begin = i;
  2144. expression_pattern = RGBA_PARAMETER;
  2145. } else {
  2146. // Method call or '(' appearing twice.
  2147. expression_pattern = NOT_PARSED;
  2148. break;
  2149. }
  2150. } else if (expression_pattern == RGBA_PARAMETER && line[i] == ')' && end < 0) {
  2151. end = i + 1;
  2152. break;
  2153. } else if (expression_pattern == NOT_PARSED && line[i] == '.') {
  2154. begin = i;
  2155. expression_pattern = COLOR_NAME;
  2156. } else if (expression_pattern == COLOR_NAME && end < 0 && (line[i] == ' ' || line[i] == '\t')) {
  2157. // Including '.' and spaces.
  2158. continue;
  2159. } else if (expression_pattern == COLOR_NAME && !(line[i] == '_' || ('A' <= line[i] && line[i] <= 'Z'))) {
  2160. end = i;
  2161. break;
  2162. }
  2163. }
  2164. switch (expression_pattern) {
  2165. case RGBA_PARAMETER: {
  2166. color_args = line.substr(begin, end - begin);
  2167. String stripped = color_args.remove_chars(" \t()");
  2168. PackedFloat64Array color = stripped.split_floats(",");
  2169. if (color.size() > 2) {
  2170. float alpha = color.size() > 3 ? color[3] : 1.0f;
  2171. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  2172. }
  2173. } break;
  2174. case COLOR_NAME: {
  2175. if (end < 0) {
  2176. end = line.length();
  2177. }
  2178. color_args = line.substr(begin, end - begin);
  2179. const String color_name = color_args.remove_chars(" \t.");
  2180. const int color_index = Color::find_named_color(color_name);
  2181. if (0 <= color_index) {
  2182. const Color color_constant = Color::get_named_color(color_index);
  2183. color_picker->set_pick_color(color_constant);
  2184. } else {
  2185. has_color = false;
  2186. }
  2187. } break;
  2188. default:
  2189. has_color = false;
  2190. break;
  2191. }
  2192. if (has_color) {
  2193. color_panel->set_position(get_screen_position() + local_pos);
  2194. }
  2195. }
  2196. _make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);
  2197. }
  2198. }
  2199. void ScriptTextEditor::_color_changed(const Color &p_color) {
  2200. String new_args;
  2201. const int decimals = 3;
  2202. if (p_color.a == 1.0f) {
  2203. new_args = String("(" + String::num(p_color.r, decimals) + ", " + String::num(p_color.g, decimals) + ", " + String::num(p_color.b, decimals) + ")");
  2204. } else {
  2205. new_args = String("(" + String::num(p_color.r, decimals) + ", " + String::num(p_color.g, decimals) + ", " + String::num(p_color.b, decimals) + ", " + String::num(p_color.a, decimals) + ")");
  2206. }
  2207. String line = code_editor->get_text_editor()->get_line(color_position.x);
  2208. String line_with_replaced_args = line.replace(color_args, new_args);
  2209. color_args = new_args;
  2210. code_editor->get_text_editor()->begin_complex_operation();
  2211. code_editor->get_text_editor()->set_line(color_position.x, line_with_replaced_args);
  2212. code_editor->get_text_editor()->end_complex_operation();
  2213. }
  2214. void ScriptTextEditor::_prepare_edit_menu() {
  2215. const CodeEdit *tx = code_editor->get_text_editor();
  2216. PopupMenu *popup = edit_menu->get_popup();
  2217. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  2218. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  2219. }
  2220. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p_foldable, bool p_open_docs, bool p_goto_definition, Vector2 p_pos) {
  2221. context_menu->clear();
  2222. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2223. context_menu->add_item(TTR("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
  2224. context_menu->add_separator();
  2225. }
  2226. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  2227. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  2228. context_menu->add_separator();
  2229. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  2230. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  2231. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  2232. context_menu->add_separator();
  2233. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  2234. context_menu->add_separator();
  2235. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  2236. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  2237. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  2238. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  2239. if (p_selection) {
  2240. context_menu->add_separator();
  2241. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  2242. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  2243. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  2244. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
  2245. }
  2246. if (p_foldable) {
  2247. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  2248. }
  2249. if (p_color || p_open_docs || p_goto_definition) {
  2250. context_menu->add_separator();
  2251. if (p_open_docs) {
  2252. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL);
  2253. }
  2254. if (p_color) {
  2255. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  2256. }
  2257. }
  2258. const PackedStringArray paths = { code_editor->get_text_editor()->get_path() };
  2259. EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, paths);
  2260. const CodeEdit *tx = code_editor->get_text_editor();
  2261. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  2262. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  2263. context_menu->set_position(get_screen_position() + p_pos);
  2264. context_menu->reset_size();
  2265. context_menu->popup();
  2266. }
  2267. void ScriptTextEditor::_enable_code_editor() {
  2268. ERR_FAIL_COND(code_editor->get_parent());
  2269. VSplitContainer *editor_box = memnew(VSplitContainer);
  2270. add_child(editor_box);
  2271. editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2272. editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2273. editor_box->add_child(code_editor);
  2274. code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel));
  2275. code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel));
  2276. code_editor->connect("validate_script", callable_mp(this, &ScriptTextEditor::_validate_script));
  2277. code_editor->connect("load_theme_settings", callable_mp(this, &ScriptTextEditor::_load_theme_settings));
  2278. code_editor->get_text_editor()->connect("symbol_lookup", callable_mp(this, &ScriptTextEditor::_lookup_symbol));
  2279. code_editor->get_text_editor()->connect("symbol_hovered", callable_mp(this, &ScriptTextEditor::_show_symbol_tooltip));
  2280. code_editor->get_text_editor()->connect("symbol_validate", callable_mp(this, &ScriptTextEditor::_validate_symbol));
  2281. code_editor->get_text_editor()->connect("gutter_added", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  2282. code_editor->get_text_editor()->connect("gutter_removed", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  2283. code_editor->get_text_editor()->connect("gutter_clicked", callable_mp(this, &ScriptTextEditor::_gutter_clicked));
  2284. code_editor->get_text_editor()->connect("_fold_line_updated", callable_mp(this, &ScriptTextEditor::_update_background_color));
  2285. code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &ScriptTextEditor::_text_edit_gui_input));
  2286. code_editor->show_toggle_files_button();
  2287. _update_gutter_indexes();
  2288. editor_box->add_child(warnings_panel);
  2289. warnings_panel->add_theme_font_override(
  2290. "normal_font", EditorNode::get_singleton()->get_editor_theme()->get_font(SNAME("main"), EditorStringName(EditorFonts)));
  2291. warnings_panel->add_theme_font_size_override(
  2292. "normal_font_size", EditorNode::get_singleton()->get_editor_theme()->get_font_size(SNAME("main_size"), EditorStringName(EditorFonts)));
  2293. warnings_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_warning_clicked));
  2294. editor_box->add_child(errors_panel);
  2295. errors_panel->add_theme_font_override(
  2296. "normal_font", EditorNode::get_singleton()->get_editor_theme()->get_font(SNAME("main"), EditorStringName(EditorFonts)));
  2297. errors_panel->add_theme_font_size_override(
  2298. "normal_font_size", EditorNode::get_singleton()->get_editor_theme()->get_font_size(SNAME("main_size"), EditorStringName(EditorFonts)));
  2299. errors_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_error_clicked));
  2300. add_child(context_menu);
  2301. context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2302. add_child(color_panel);
  2303. color_picker = memnew(ColorPicker);
  2304. color_picker->set_deferred_mode(true);
  2305. color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_color_changed));
  2306. color_panel->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(color_picker));
  2307. color_panel->add_child(color_picker);
  2308. quick_open = memnew(ScriptEditorQuickOpen);
  2309. quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line));
  2310. add_child(quick_open);
  2311. goto_line_popup = memnew(GotoLinePopup);
  2312. add_child(goto_line_popup);
  2313. add_child(connection_info_dialog);
  2314. edit_hb->add_child(edit_menu);
  2315. edit_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_prepare_edit_menu));
  2316. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  2317. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  2318. edit_menu->get_popup()->add_separator();
  2319. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  2320. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  2321. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  2322. edit_menu->get_popup()->add_separator();
  2323. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  2324. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  2325. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
  2326. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  2327. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
  2328. edit_menu->get_popup()->add_separator();
  2329. {
  2330. PopupMenu *sub_menu = memnew(PopupMenu);
  2331. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  2332. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  2333. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  2334. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  2335. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  2336. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  2337. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2338. edit_menu->get_popup()->add_submenu_node_item(TTR("Line"), sub_menu);
  2339. }
  2340. {
  2341. PopupMenu *sub_menu = memnew(PopupMenu);
  2342. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  2343. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  2344. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  2345. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
  2346. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2347. edit_menu->get_popup()->add_submenu_node_item(TTR("Folding"), sub_menu);
  2348. }
  2349. edit_menu->get_popup()->add_separator();
  2350. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
  2351. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  2352. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
  2353. {
  2354. PopupMenu *sub_menu = memnew(PopupMenu);
  2355. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  2356. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  2357. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  2358. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2359. edit_menu->get_popup()->add_submenu_node_item(TTR("Indentation"), sub_menu);
  2360. }
  2361. edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2362. edit_menu->get_popup()->add_separator();
  2363. {
  2364. PopupMenu *sub_menu = memnew(PopupMenu);
  2365. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  2366. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  2367. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
  2368. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2369. edit_menu->get_popup()->add_submenu_node_item(TTR("Convert Case"), sub_menu);
  2370. }
  2371. edit_menu->get_popup()->add_submenu_node_item(TTR("Syntax Highlighter"), highlighter_menu);
  2372. highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_change_syntax_highlighter));
  2373. edit_hb->add_child(search_menu);
  2374. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  2375. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  2376. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  2377. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  2378. search_menu->get_popup()->add_separator();
  2379. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);
  2380. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  2381. search_menu->get_popup()->add_separator();
  2382. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  2383. search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2384. _load_theme_settings();
  2385. edit_hb->add_child(goto_menu);
  2386. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  2387. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  2388. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL);
  2389. goto_menu->get_popup()->add_separator();
  2390. goto_menu->get_popup()->add_submenu_node_item(TTR("Bookmarks"), bookmarks_menu);
  2391. _update_bookmark_list();
  2392. bookmarks_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_bookmark_list));
  2393. bookmarks_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_bookmark_item_pressed));
  2394. goto_menu->get_popup()->add_submenu_node_item(TTR("Breakpoints"), breakpoints_menu);
  2395. _update_breakpoint_list();
  2396. breakpoints_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_breakpoint_list));
  2397. breakpoints_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_breakpoint_item_pressed));
  2398. goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2399. }
  2400. ScriptTextEditor::ScriptTextEditor() {
  2401. code_editor = memnew(CodeTextEditor);
  2402. code_editor->set_toggle_list_control(ScriptEditor::get_singleton()->get_left_list_split());
  2403. code_editor->add_theme_constant_override("separation", 2);
  2404. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2405. code_editor->set_code_complete_func(_code_complete_scripts, this);
  2406. code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  2407. code_editor->get_text_editor()->set_draw_breakpoints_gutter(true);
  2408. code_editor->get_text_editor()->set_draw_executing_lines_gutter(true);
  2409. code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled));
  2410. code_editor->get_text_editor()->connect("caret_changed", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
  2411. code_editor->connect("navigation_preview_ended", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
  2412. connection_gutter = 1;
  2413. code_editor->get_text_editor()->add_gutter(connection_gutter);
  2414. code_editor->get_text_editor()->set_gutter_name(connection_gutter, "connection_gutter");
  2415. code_editor->get_text_editor()->set_gutter_draw(connection_gutter, false);
  2416. code_editor->get_text_editor()->set_gutter_overwritable(connection_gutter, true);
  2417. code_editor->get_text_editor()->set_gutter_type(connection_gutter, TextEdit::GUTTER_TYPE_ICON);
  2418. warnings_panel = memnew(RichTextLabel);
  2419. warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  2420. warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2421. warnings_panel->set_meta_underline(true);
  2422. warnings_panel->set_selection_enabled(true);
  2423. warnings_panel->set_context_menu_enabled(true);
  2424. warnings_panel->set_focus_mode(FOCUS_CLICK);
  2425. warnings_panel->hide();
  2426. errors_panel = memnew(RichTextLabel);
  2427. errors_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  2428. errors_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2429. errors_panel->set_meta_underline(true);
  2430. errors_panel->set_selection_enabled(true);
  2431. errors_panel->set_context_menu_enabled(true);
  2432. errors_panel->set_focus_mode(FOCUS_CLICK);
  2433. errors_panel->hide();
  2434. update_settings();
  2435. code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
  2436. code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true);
  2437. code_editor->get_text_editor()->set_context_menu_enabled(false);
  2438. context_menu = memnew(PopupMenu);
  2439. color_panel = memnew(PopupPanel);
  2440. edit_hb = memnew(HBoxContainer);
  2441. edit_menu = memnew(MenuButton);
  2442. edit_menu->set_text(TTR("Edit"));
  2443. edit_menu->set_switch_on_hover(true);
  2444. edit_menu->set_shortcut_context(this);
  2445. highlighter_menu = memnew(PopupMenu);
  2446. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  2447. plain_highlighter.instantiate();
  2448. add_syntax_highlighter(plain_highlighter);
  2449. Ref<EditorStandardSyntaxHighlighter> highlighter;
  2450. highlighter.instantiate();
  2451. add_syntax_highlighter(highlighter);
  2452. set_syntax_highlighter(highlighter);
  2453. search_menu = memnew(MenuButton);
  2454. search_menu->set_text(TTR("Search"));
  2455. search_menu->set_switch_on_hover(true);
  2456. search_menu->set_shortcut_context(this);
  2457. goto_menu = memnew(MenuButton);
  2458. goto_menu->set_text(TTR("Go To"));
  2459. goto_menu->set_switch_on_hover(true);
  2460. goto_menu->set_shortcut_context(this);
  2461. bookmarks_menu = memnew(PopupMenu);
  2462. breakpoints_menu = memnew(PopupMenu);
  2463. inline_color_popup = memnew(PopupPanel);
  2464. add_child(inline_color_popup);
  2465. inline_color_picker = memnew(ColorPicker);
  2466. inline_color_picker->set_mouse_filter(MOUSE_FILTER_STOP);
  2467. inline_color_picker->set_deferred_mode(true);
  2468. inline_color_picker->set_hex_visible(false);
  2469. inline_color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_picker_color_changed));
  2470. inline_color_popup->add_child(inline_color_picker);
  2471. inline_color_options = memnew(OptionButton);
  2472. inline_color_options->set_h_size_flags(SIZE_FILL);
  2473. inline_color_options->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_ELLIPSIS);
  2474. inline_color_options->set_fit_to_longest_item(false);
  2475. inline_color_options->connect("item_selected", callable_mp(this, &ScriptTextEditor::_update_color_text).unbind(1));
  2476. inline_color_picker->get_slider(ColorPicker::SLIDER_COUNT)->get_parent()->add_sibling(inline_color_options);
  2477. connection_info_dialog = memnew(ConnectionInfoDialog);
  2478. SET_DRAG_FORWARDING_GCD(code_editor->get_text_editor(), ScriptTextEditor);
  2479. }
  2480. ScriptTextEditor::~ScriptTextEditor() {
  2481. highlighters.clear();
  2482. if (!editor_enabled) {
  2483. memdelete(code_editor);
  2484. memdelete(warnings_panel);
  2485. memdelete(errors_panel);
  2486. memdelete(context_menu);
  2487. memdelete(color_panel);
  2488. memdelete(edit_hb);
  2489. memdelete(edit_menu);
  2490. memdelete(highlighter_menu);
  2491. memdelete(search_menu);
  2492. memdelete(goto_menu);
  2493. memdelete(bookmarks_menu);
  2494. memdelete(breakpoints_menu);
  2495. memdelete(connection_info_dialog);
  2496. }
  2497. }
  2498. static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) {
  2499. if (Object::cast_to<Script>(*p_resource)) {
  2500. return memnew(ScriptTextEditor);
  2501. }
  2502. return nullptr;
  2503. }
  2504. void ScriptTextEditor::register_editor() {
  2505. ED_SHORTCUT("script_text_editor/move_up", TTRC("Move Up"), KeyModifierMask::ALT | Key::UP);
  2506. ED_SHORTCUT("script_text_editor/move_down", TTRC("Move Down"), KeyModifierMask::ALT | Key::DOWN);
  2507. ED_SHORTCUT("script_text_editor/delete_line", TTRC("Delete Line"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K);
  2508. // Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
  2509. // The next/previous in history shortcut in this case makes a lot more sense.
  2510. ED_SHORTCUT("script_text_editor/indent", TTRC("Indent"), Key::NONE);
  2511. ED_SHORTCUT("script_text_editor/unindent", TTRC("Unindent"), KeyModifierMask::SHIFT | Key::TAB);
  2512. ED_SHORTCUT_ARRAY("script_text_editor/toggle_comment", TTRC("Toggle Comment"), { int32_t(KeyModifierMask::CMD_OR_CTRL | Key::K), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::SLASH), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::KP_DIVIDE), int32_t(KeyModifierMask::CMD_OR_CTRL | Key::NUMBERSIGN) });
  2513. ED_SHORTCUT("script_text_editor/toggle_fold_line", TTRC("Fold/Unfold Line"), KeyModifierMask::ALT | Key::F);
  2514. ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_fold_line", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::F);
  2515. ED_SHORTCUT("script_text_editor/fold_all_lines", TTRC("Fold All Lines"), Key::NONE);
  2516. ED_SHORTCUT("script_text_editor/create_code_region", TTRC("Create Code Region"), KeyModifierMask::ALT | Key::R);
  2517. ED_SHORTCUT("script_text_editor/unfold_all_lines", TTRC("Unfold All Lines"), Key::NONE);
  2518. ED_SHORTCUT("script_text_editor/duplicate_selection", TTRC("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D);
  2519. ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C);
  2520. ED_SHORTCUT("script_text_editor/duplicate_lines", TTRC("Duplicate Lines"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::DOWN);
  2521. ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_lines", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::DOWN);
  2522. ED_SHORTCUT("script_text_editor/evaluate_selection", TTRC("Evaluate Selection"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::E);
  2523. ED_SHORTCUT("script_text_editor/toggle_word_wrap", TTRC("Toggle Word Wrap"), KeyModifierMask::ALT | Key::Z);
  2524. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTRC("Trim Trailing Whitespace"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T);
  2525. ED_SHORTCUT("script_text_editor/trim_final_newlines", TTRC("Trim Final Newlines"), Key::NONE);
  2526. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTRC("Convert Indent to Spaces"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::Y);
  2527. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTRC("Convert Indent to Tabs"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::I);
  2528. ED_SHORTCUT("script_text_editor/auto_indent", TTRC("Auto Indent"), KeyModifierMask::CMD_OR_CTRL | Key::I);
  2529. ED_SHORTCUT_AND_COMMAND("script_text_editor/find", TTRC("Find..."), KeyModifierMask::CMD_OR_CTRL | Key::F);
  2530. ED_SHORTCUT("script_text_editor/find_next", TTRC("Find Next"), Key::F3);
  2531. ED_SHORTCUT_OVERRIDE("script_text_editor/find_next", "macos", KeyModifierMask::META | Key::G);
  2532. ED_SHORTCUT("script_text_editor/find_previous", TTRC("Find Previous"), KeyModifierMask::SHIFT | Key::F3);
  2533. ED_SHORTCUT_OVERRIDE("script_text_editor/find_previous", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::G);
  2534. ED_SHORTCUT_AND_COMMAND("script_text_editor/replace", TTRC("Replace..."), KeyModifierMask::CTRL | Key::R);
  2535. ED_SHORTCUT_OVERRIDE("script_text_editor/replace", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::F);
  2536. ED_SHORTCUT("script_text_editor/replace_in_files", TTRC("Replace in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R);
  2537. ED_SHORTCUT("script_text_editor/contextual_help", TTRC("Contextual Help"), KeyModifierMask::ALT | Key::F1);
  2538. ED_SHORTCUT_OVERRIDE("script_text_editor/contextual_help", "macos", KeyModifierMask::ALT | KeyModifierMask::SHIFT | Key::SPACE);
  2539. ED_SHORTCUT("script_text_editor/toggle_bookmark", TTRC("Toggle Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::B);
  2540. ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTRC("Go to Next Bookmark"), KeyModifierMask::CMD_OR_CTRL | Key::B);
  2541. ED_SHORTCUT_OVERRIDE("script_text_editor/goto_next_bookmark", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::B);
  2542. ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTRC("Go to Previous Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::B);
  2543. ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTRC("Remove All Bookmarks"), Key::NONE);
  2544. ED_SHORTCUT("script_text_editor/goto_function", TTRC("Go to Function..."), KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::F);
  2545. ED_SHORTCUT_OVERRIDE("script_text_editor/goto_function", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::J);
  2546. ED_SHORTCUT("script_text_editor/goto_line", TTRC("Go to Line..."), KeyModifierMask::CMD_OR_CTRL | Key::L);
  2547. ED_SHORTCUT("script_text_editor/goto_symbol", TTRC("Lookup Symbol"));
  2548. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTRC("Toggle Breakpoint"), Key::F9);
  2549. ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_breakpoint", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
  2550. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTRC("Remove All Breakpoints"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F9);
  2551. // Using Control for these shortcuts even on macOS because Command+Comma is taken for opening Editor Settings.
  2552. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTRC("Go to Next Breakpoint"), KeyModifierMask::CTRL | Key::PERIOD);
  2553. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTRC("Go to Previous Breakpoint"), KeyModifierMask::CTRL | Key::COMMA);
  2554. ScriptEditor::register_create_script_editor_function(create_editor);
  2555. }
  2556. void ScriptTextEditor::validate() {
  2557. code_editor->validate_script();
  2558. }