script_text_editor.cpp 110 KB

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