script_text_editor.cpp 91 KB

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