2
0

script_text_editor.cpp 93 KB

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