script_text_editor.cpp 111 KB

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