script_text_editor.cpp 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133
  1. /*************************************************************************/
  2. /* script_text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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_scale.h"
  38. #include "editor/editor_settings.h"
  39. void ConnectionInfoDialog::ok_pressed() {
  40. }
  41. void ConnectionInfoDialog::popup_connections(String p_method, Vector<Node *> p_nodes) {
  42. method->set_text(p_method);
  43. tree->clear();
  44. TreeItem *root = tree->create_item();
  45. for (int i = 0; i < p_nodes.size(); i++) {
  46. List<Connection> all_connections;
  47. p_nodes[i]->get_signals_connected_to_this(&all_connections);
  48. for (const Connection &connection : all_connections) {
  49. if (connection.callable.get_method() != p_method) {
  50. continue;
  51. }
  52. TreeItem *node_item = tree->create_item(root);
  53. node_item->set_text(0, Object::cast_to<Node>(connection.signal.get_object())->get_name());
  54. node_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(connection.signal.get_object(), "Node"));
  55. node_item->set_selectable(0, false);
  56. node_item->set_editable(0, false);
  57. node_item->set_text(1, connection.signal.get_name());
  58. Control *p = Object::cast_to<Control>(get_parent());
  59. node_item->set_icon(1, p->get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  60. node_item->set_selectable(1, false);
  61. node_item->set_editable(1, false);
  62. node_item->set_text(2, Object::cast_to<Node>(connection.callable.get_object())->get_name());
  63. node_item->set_icon(2, EditorNode::get_singleton()->get_object_icon(connection.callable.get_object(), "Node"));
  64. node_item->set_selectable(2, false);
  65. node_item->set_editable(2, false);
  66. }
  67. }
  68. popup_centered(Size2(600, 300) * EDSCALE);
  69. }
  70. ConnectionInfoDialog::ConnectionInfoDialog() {
  71. set_title(TTR("Connections to method:"));
  72. VBoxContainer *vbc = memnew(VBoxContainer);
  73. vbc->set_anchor_and_offset(SIDE_LEFT, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  74. vbc->set_anchor_and_offset(SIDE_TOP, Control::ANCHOR_BEGIN, 8 * EDSCALE);
  75. vbc->set_anchor_and_offset(SIDE_RIGHT, Control::ANCHOR_END, -8 * EDSCALE);
  76. vbc->set_anchor_and_offset(SIDE_BOTTOM, Control::ANCHOR_END, -8 * EDSCALE);
  77. add_child(vbc);
  78. method = memnew(Label);
  79. method->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  80. vbc->add_child(method);
  81. tree = memnew(Tree);
  82. tree->set_columns(3);
  83. tree->set_hide_root(true);
  84. tree->set_column_titles_visible(true);
  85. tree->set_column_title(0, TTR("Source"));
  86. tree->set_column_title(1, TTR("Signal"));
  87. tree->set_column_title(2, TTR("Target"));
  88. vbc->add_child(tree);
  89. tree->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  90. tree->set_allow_rmb_select(true);
  91. }
  92. ////////////////////////////////////////////////////////////////////////////////
  93. Vector<String> ScriptTextEditor::get_functions() {
  94. CodeEdit *te = code_editor->get_text_editor();
  95. String text = te->get_text();
  96. List<String> fnc;
  97. if (script->get_language()->validate(text, script->get_path(), &fnc)) {
  98. //if valid rewrite functions to latest
  99. functions.clear();
  100. for (const String &E : fnc) {
  101. functions.push_back(E);
  102. }
  103. }
  104. return functions;
  105. }
  106. void ScriptTextEditor::apply_code() {
  107. if (script.is_null()) {
  108. return;
  109. }
  110. script->set_source_code(code_editor->get_text_editor()->get_text());
  111. script->update_exports();
  112. code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
  113. }
  114. Ref<Resource> ScriptTextEditor::get_edited_resource() const {
  115. return script;
  116. }
  117. void ScriptTextEditor::set_edited_resource(const Ref<Resource> &p_res) {
  118. ERR_FAIL_COND(script.is_valid());
  119. ERR_FAIL_COND(p_res.is_null());
  120. script = p_res;
  121. code_editor->get_text_editor()->set_text(script->get_source_code());
  122. code_editor->get_text_editor()->clear_undo_history();
  123. code_editor->get_text_editor()->tag_saved_version();
  124. emit_signal(SNAME("name_changed"));
  125. code_editor->update_line_and_column();
  126. }
  127. void ScriptTextEditor::enable_editor() {
  128. if (editor_enabled) {
  129. return;
  130. }
  131. editor_enabled = true;
  132. _enable_code_editor();
  133. _validate_script();
  134. }
  135. void ScriptTextEditor::_load_theme_settings() {
  136. CodeEdit *text_edit = code_editor->get_text_editor();
  137. Color updated_marked_line_color = EDITOR_GET("text_editor/theme/highlighting/mark_color");
  138. Color updated_safe_line_number_color = EDITOR_GET("text_editor/theme/highlighting/safe_line_number_color");
  139. bool safe_line_number_color_updated = updated_safe_line_number_color != safe_line_number_color;
  140. bool marked_line_color_updated = updated_marked_line_color != marked_line_color;
  141. if (safe_line_number_color_updated || marked_line_color_updated) {
  142. safe_line_number_color = updated_safe_line_number_color;
  143. for (int i = 0; i < text_edit->get_line_count(); i++) {
  144. if (marked_line_color_updated && text_edit->get_line_background_color(i) == marked_line_color) {
  145. text_edit->set_line_background_color(i, updated_marked_line_color);
  146. }
  147. if (safe_line_number_color_updated && text_edit->get_line_gutter_item_color(i, line_number_gutter) != default_line_number_color) {
  148. text_edit->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  149. }
  150. }
  151. marked_line_color = updated_marked_line_color;
  152. }
  153. theme_loaded = true;
  154. if (!script.is_null()) {
  155. _set_theme_for_script();
  156. }
  157. }
  158. void ScriptTextEditor::_set_theme_for_script() {
  159. if (!theme_loaded) {
  160. return;
  161. }
  162. CodeEdit *text_edit = code_editor->get_text_editor();
  163. text_edit->get_syntax_highlighter()->update_cache();
  164. List<String> strings;
  165. script->get_language()->get_string_delimiters(&strings);
  166. text_edit->clear_string_delimiters();
  167. for (const String &string : strings) {
  168. String beg = string.get_slice(" ", 0);
  169. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  170. if (!text_edit->has_string_delimiter(beg)) {
  171. text_edit->add_string_delimiter(beg, end, end.is_empty());
  172. }
  173. if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) {
  174. text_edit->add_auto_brace_completion_pair(beg, end);
  175. }
  176. }
  177. List<String> comments;
  178. script->get_language()->get_comment_delimiters(&comments);
  179. text_edit->clear_comment_delimiters();
  180. for (const String &comment : comments) {
  181. String beg = comment.get_slice(" ", 0);
  182. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  183. text_edit->add_comment_delimiter(beg, end, end.is_empty());
  184. if (!end.is_empty() && !text_edit->has_auto_brace_completion_open_key(beg)) {
  185. text_edit->add_auto_brace_completion_pair(beg, end);
  186. }
  187. }
  188. }
  189. void ScriptTextEditor::_show_errors_panel(bool p_show) {
  190. errors_panel->set_visible(p_show);
  191. }
  192. void ScriptTextEditor::_show_warnings_panel(bool p_show) {
  193. warnings_panel->set_visible(p_show);
  194. }
  195. void ScriptTextEditor::_warning_clicked(Variant p_line) {
  196. if (p_line.get_type() == Variant::INT) {
  197. goto_line_centered(p_line.operator int64_t());
  198. } else if (p_line.get_type() == Variant::DICTIONARY) {
  199. Dictionary meta = p_line.operator Dictionary();
  200. const int line = meta["line"].operator int64_t() - 1;
  201. CodeEdit *text_editor = code_editor->get_text_editor();
  202. String prev_line = line > 0 ? text_editor->get_line(line - 1) : "";
  203. if (prev_line.contains("@warning_ignore")) {
  204. const int closing_bracket_idx = prev_line.find(")");
  205. const String text_to_insert = ", " + meta["code"].operator String();
  206. prev_line = prev_line.insert(closing_bracket_idx, text_to_insert);
  207. text_editor->set_line(line - 1, prev_line);
  208. } else {
  209. const int indent = text_editor->get_indent_level(line) / text_editor->get_indent_size();
  210. String annotation_indent;
  211. if (!text_editor->is_indent_using_spaces()) {
  212. annotation_indent = String("\t").repeat(indent);
  213. } else {
  214. annotation_indent = String(" ").repeat(text_editor->get_indent_size() * indent);
  215. }
  216. text_editor->insert_line_at(line, annotation_indent + "@warning_ignore(" + meta["code"].operator String() + ")");
  217. }
  218. _validate_script();
  219. }
  220. }
  221. void ScriptTextEditor::_error_clicked(Variant p_line) {
  222. if (p_line.get_type() == Variant::INT) {
  223. code_editor->get_text_editor()->set_caret_line(p_line.operator int64_t());
  224. }
  225. }
  226. void ScriptTextEditor::reload_text() {
  227. ERR_FAIL_COND(script.is_null());
  228. CodeEdit *te = code_editor->get_text_editor();
  229. int column = te->get_caret_column();
  230. int row = te->get_caret_line();
  231. int h = te->get_h_scroll();
  232. int v = te->get_v_scroll();
  233. te->set_text(script->get_source_code());
  234. te->set_caret_line(row);
  235. te->set_caret_column(column);
  236. te->set_h_scroll(h);
  237. te->set_v_scroll(v);
  238. te->tag_saved_version();
  239. code_editor->update_line_and_column();
  240. }
  241. void ScriptTextEditor::add_callback(const String &p_function, PackedStringArray p_args) {
  242. String code = code_editor->get_text_editor()->get_text();
  243. int pos = script->get_language()->find_function(p_function, code);
  244. if (pos == -1) {
  245. //does not exist
  246. code_editor->get_text_editor()->deselect();
  247. pos = code_editor->get_text_editor()->get_line_count() + 2;
  248. String func = script->get_language()->make_function("", p_function, p_args);
  249. //code=code+func;
  250. code_editor->get_text_editor()->set_caret_line(pos + 1);
  251. code_editor->get_text_editor()->set_caret_column(1000000); //none shall be that big
  252. code_editor->get_text_editor()->insert_text_at_caret("\n\n" + func);
  253. }
  254. code_editor->get_text_editor()->set_caret_line(pos);
  255. code_editor->get_text_editor()->set_caret_column(1);
  256. }
  257. bool ScriptTextEditor::show_members_overview() {
  258. return true;
  259. }
  260. void ScriptTextEditor::update_settings() {
  261. code_editor->get_text_editor()->set_gutter_draw(connection_gutter, EditorSettings::get_singleton()->get("text_editor/appearance/gutters/show_info_gutter"));
  262. code_editor->update_editor_settings();
  263. }
  264. bool ScriptTextEditor::is_unsaved() {
  265. const bool unsaved =
  266. code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() ||
  267. script->get_path().is_empty(); // In memory.
  268. return unsaved;
  269. }
  270. Variant ScriptTextEditor::get_edit_state() {
  271. return code_editor->get_edit_state();
  272. }
  273. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  274. code_editor->set_edit_state(p_state);
  275. Dictionary state = p_state;
  276. if (state.has("syntax_highlighter")) {
  277. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  278. if (idx >= 0) {
  279. _change_syntax_highlighter(idx);
  280. }
  281. }
  282. if (editor_enabled) {
  283. #ifndef ANDROID_ENABLED
  284. ensure_focus();
  285. #endif
  286. }
  287. }
  288. void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  289. code_editor->convert_case(p_case);
  290. }
  291. void ScriptTextEditor::trim_trailing_whitespace() {
  292. code_editor->trim_trailing_whitespace();
  293. }
  294. void ScriptTextEditor::insert_final_newline() {
  295. code_editor->insert_final_newline();
  296. }
  297. void ScriptTextEditor::convert_indent_to_spaces() {
  298. code_editor->convert_indent_to_spaces();
  299. }
  300. void ScriptTextEditor::convert_indent_to_tabs() {
  301. code_editor->convert_indent_to_tabs();
  302. }
  303. void ScriptTextEditor::tag_saved_version() {
  304. code_editor->get_text_editor()->tag_saved_version();
  305. }
  306. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  307. code_editor->goto_line(p_line);
  308. }
  309. void ScriptTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  310. code_editor->goto_line_selection(p_line, p_begin, p_end);
  311. }
  312. void ScriptTextEditor::goto_line_centered(int p_line) {
  313. code_editor->goto_line_centered(p_line);
  314. }
  315. void ScriptTextEditor::set_executing_line(int p_line) {
  316. code_editor->set_executing_line(p_line);
  317. }
  318. void ScriptTextEditor::clear_executing_line() {
  319. code_editor->clear_executing_line();
  320. }
  321. void ScriptTextEditor::ensure_focus() {
  322. code_editor->get_text_editor()->grab_focus();
  323. }
  324. String ScriptTextEditor::get_name() {
  325. String name;
  326. name = script->get_path().get_file();
  327. if (name.is_empty()) {
  328. // This appears for newly created built-in scripts before saving the scene.
  329. name = TTR("[unsaved]");
  330. } else if (script->is_built_in()) {
  331. const String &script_name = script->get_name();
  332. if (!script_name.is_empty()) {
  333. // If the built-in script has a custom resource name defined,
  334. // display the built-in script name as follows: `ResourceName (scene_file.tscn)`
  335. name = vformat("%s (%s)", script_name, name.get_slice("::", 0));
  336. }
  337. }
  338. if (is_unsaved()) {
  339. name += "(*)";
  340. }
  341. return name;
  342. }
  343. Ref<Texture2D> ScriptTextEditor::get_theme_icon() {
  344. if (get_parent_control()) {
  345. String icon_name = script->get_class();
  346. if (script->is_built_in()) {
  347. icon_name += "Internal";
  348. }
  349. if (get_parent_control()->has_theme_icon(icon_name, SNAME("EditorIcons"))) {
  350. return get_parent_control()->get_theme_icon(icon_name, SNAME("EditorIcons"));
  351. } else if (get_parent_control()->has_theme_icon(script->get_class(), SNAME("EditorIcons"))) {
  352. return get_parent_control()->get_theme_icon(script->get_class(), SNAME("EditorIcons"));
  353. }
  354. }
  355. return Ref<Texture2D>();
  356. }
  357. void ScriptTextEditor::_validate_script() {
  358. CodeEdit *te = code_editor->get_text_editor();
  359. String text = te->get_text();
  360. List<String> fnc;
  361. warnings.clear();
  362. errors.clear();
  363. safe_lines.clear();
  364. if (!script->get_language()->validate(text, script->get_path(), &fnc, &errors, &warnings, &safe_lines)) {
  365. // TRANSLATORS: Script error pointing to a line and column number.
  366. String error_text = vformat(TTR("Error at (%d, %d):"), errors[0].line, errors[0].column) + " " + errors[0].message;
  367. code_editor->set_error(error_text);
  368. code_editor->set_error_pos(errors[0].line - 1, errors[0].column - 1);
  369. script_is_valid = false;
  370. } else {
  371. code_editor->set_error("");
  372. if (!script->is_tool()) {
  373. script->set_source_code(text);
  374. script->update_exports();
  375. te->get_syntax_highlighter()->update_cache();
  376. }
  377. functions.clear();
  378. for (const String &E : fnc) {
  379. functions.push_back(E);
  380. }
  381. script_is_valid = true;
  382. }
  383. _update_connected_methods();
  384. _update_warnings();
  385. _update_errors();
  386. emit_signal(SNAME("name_changed"));
  387. emit_signal(SNAME("edited_script_changed"));
  388. }
  389. void ScriptTextEditor::_update_warnings() {
  390. int warning_nb = warnings.size();
  391. warnings_panel->clear();
  392. bool has_connections_table = false;
  393. // Add missing connections.
  394. if (GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
  395. Node *base = get_tree()->get_edited_scene_root();
  396. if (base && missing_connections.size() > 0) {
  397. has_connections_table = true;
  398. warnings_panel->push_table(1);
  399. for (const Connection &connection : missing_connections) {
  400. String base_path = base->get_name();
  401. String source_path = base == connection.signal.get_object() ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.signal.get_object()));
  402. String target_path = base == connection.callable.get_object() ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.callable.get_object()));
  403. warnings_panel->push_cell();
  404. warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  405. 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));
  406. warnings_panel->pop(); // Color.
  407. warnings_panel->pop(); // Cell.
  408. }
  409. warnings_panel->pop(); // Table.
  410. warning_nb += missing_connections.size();
  411. }
  412. }
  413. code_editor->set_warning_count(warning_nb);
  414. if (has_connections_table) {
  415. warnings_panel->add_newline();
  416. }
  417. // Add script warnings.
  418. warnings_panel->push_table(3);
  419. for (const ScriptLanguage::Warning &w : warnings) {
  420. Dictionary ignore_meta;
  421. ignore_meta["line"] = w.start_line;
  422. ignore_meta["code"] = w.string_code.to_lower();
  423. warnings_panel->push_cell();
  424. warnings_panel->push_meta(ignore_meta);
  425. warnings_panel->push_color(
  426. warnings_panel->get_theme_color(SNAME("accent_color"), SNAME("Editor")).lerp(warnings_panel->get_theme_color(SNAME("mono_color"), SNAME("Editor")), 0.5f));
  427. warnings_panel->add_text(TTR("[Ignore]"));
  428. warnings_panel->pop(); // Color.
  429. warnings_panel->pop(); // Meta ignore.
  430. warnings_panel->pop(); // Cell.
  431. warnings_panel->push_cell();
  432. warnings_panel->push_meta(w.start_line - 1);
  433. warnings_panel->push_color(warnings_panel->get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  434. warnings_panel->add_text(TTR("Line") + " " + itos(w.start_line));
  435. warnings_panel->add_text(" (" + w.string_code + "):");
  436. warnings_panel->pop(); // Color.
  437. warnings_panel->pop(); // Meta goto.
  438. warnings_panel->pop(); // Cell.
  439. warnings_panel->push_cell();
  440. warnings_panel->add_text(w.message);
  441. warnings_panel->pop(); // Cell.
  442. }
  443. warnings_panel->pop(); // Table.
  444. }
  445. void ScriptTextEditor::_update_errors() {
  446. code_editor->set_error_count(errors.size());
  447. errors_panel->clear();
  448. errors_panel->push_table(2);
  449. for (const ScriptLanguage::ScriptError &err : errors) {
  450. errors_panel->push_cell();
  451. errors_panel->push_meta(err.line - 1);
  452. errors_panel->push_color(warnings_panel->get_theme_color(SNAME("error_color"), SNAME("Editor")));
  453. errors_panel->add_text(TTR("Line") + " " + itos(err.line) + ":");
  454. errors_panel->pop(); // Color.
  455. errors_panel->pop(); // Meta goto.
  456. errors_panel->pop(); // Cell.
  457. errors_panel->push_cell();
  458. errors_panel->add_text(err.message);
  459. errors_panel->pop(); // Cell.
  460. }
  461. errors_panel->pop(); // Table
  462. CodeEdit *te = code_editor->get_text_editor();
  463. bool highlight_safe = EDITOR_GET("text_editor/appearance/gutters/highlight_type_safe_lines");
  464. bool last_is_safe = false;
  465. for (int i = 0; i < te->get_line_count(); i++) {
  466. if (errors.is_empty()) {
  467. te->set_line_background_color(i, Color(0, 0, 0, 0));
  468. } else {
  469. for (const ScriptLanguage::ScriptError &E : errors) {
  470. bool error_line = i == E.line - 1;
  471. te->set_line_background_color(i, error_line ? marked_line_color : Color(0, 0, 0, 0));
  472. if (error_line) {
  473. break;
  474. }
  475. }
  476. }
  477. if (highlight_safe) {
  478. if (safe_lines.has(i + 1)) {
  479. te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  480. last_is_safe = true;
  481. } else if (last_is_safe && (te->is_in_comment(i) != -1 || te->get_line(i).strip_edges().is_empty())) {
  482. te->set_line_gutter_item_color(i, line_number_gutter, safe_line_number_color);
  483. } else {
  484. te->set_line_gutter_item_color(i, line_number_gutter, default_line_number_color);
  485. last_is_safe = false;
  486. }
  487. } else {
  488. te->set_line_gutter_item_color(i, 1, default_line_number_color);
  489. }
  490. }
  491. }
  492. void ScriptTextEditor::_update_bookmark_list() {
  493. bookmarks_menu->clear();
  494. bookmarks_menu->reset_size();
  495. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  496. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  497. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  498. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  499. PackedInt32Array bookmark_list = code_editor->get_text_editor()->get_bookmarked_lines();
  500. if (bookmark_list.size() == 0) {
  501. return;
  502. }
  503. bookmarks_menu->add_separator();
  504. for (int i = 0; i < bookmark_list.size(); i++) {
  505. // Strip edges to remove spaces or tabs.
  506. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  507. String line = code_editor->get_text_editor()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges();
  508. // Limit the size of the line if too big.
  509. if (line.length() > 50) {
  510. line = line.substr(0, 50);
  511. }
  512. bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - `" + line + "`");
  513. bookmarks_menu->set_item_metadata(-1, bookmark_list[i]);
  514. }
  515. }
  516. void ScriptTextEditor::_bookmark_item_pressed(int p_idx) {
  517. if (p_idx < 4) { // Any item before the separator.
  518. _edit_option(bookmarks_menu->get_item_id(p_idx));
  519. } else {
  520. code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
  521. }
  522. }
  523. static Vector<Node *> _find_all_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  524. Vector<Node *> nodes;
  525. if (p_current->get_owner() != p_base && p_base != p_current) {
  526. return nodes;
  527. }
  528. Ref<Script> c = p_current->get_script();
  529. if (c == p_script) {
  530. nodes.push_back(p_current);
  531. }
  532. for (int i = 0; i < p_current->get_child_count(); i++) {
  533. Vector<Node *> found = _find_all_node_for_script(p_base, p_current->get_child(i), p_script);
  534. nodes.append_array(found);
  535. }
  536. return nodes;
  537. }
  538. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  539. if (p_current->get_owner() != p_base && p_base != p_current) {
  540. return nullptr;
  541. }
  542. Ref<Script> c = p_current->get_script();
  543. if (c == p_script) {
  544. return p_current;
  545. }
  546. for (int i = 0; i < p_current->get_child_count(); i++) {
  547. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  548. if (found) {
  549. return found;
  550. }
  551. }
  552. return nullptr;
  553. }
  554. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, HashSet<Ref<Script>> &r_scripts) {
  555. if (p_current->get_owner() != p_base && p_base != p_current) {
  556. return;
  557. }
  558. Ref<Script> c = p_current->get_script();
  559. if (c.is_valid()) {
  560. r_scripts.insert(c);
  561. }
  562. for (int i = 0; i < p_current->get_child_count(); i++) {
  563. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  564. }
  565. }
  566. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  567. if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  568. return;
  569. }
  570. ERR_FAIL_COND(!get_tree());
  571. HashSet<Ref<Script>> scripts;
  572. Node *base = get_tree()->get_edited_scene_root();
  573. if (base) {
  574. _find_changed_scripts_for_external_editor(base, base, scripts);
  575. }
  576. for (const Ref<Script> &E : scripts) {
  577. Ref<Script> script = E;
  578. if (p_for_script.is_valid() && p_for_script != script) {
  579. continue;
  580. }
  581. if (script->is_built_in()) {
  582. continue; //internal script, who cares, though weird
  583. }
  584. uint64_t last_date = script->get_last_modified_time();
  585. uint64_t date = FileAccess::get_modified_time(script->get_path());
  586. if (last_date != date) {
  587. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), ResourceFormatLoader::CACHE_MODE_IGNORE);
  588. ERR_CONTINUE(!rel_script.is_valid());
  589. script->set_source_code(rel_script->get_source_code());
  590. script->set_last_modified_time(rel_script->get_last_modified_time());
  591. script->update_exports();
  592. _trigger_live_script_reload();
  593. }
  594. }
  595. }
  596. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
  597. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  598. ste->_code_complete_script(p_code, r_options, r_force);
  599. }
  600. void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
  601. if (color_panel->is_visible()) {
  602. return;
  603. }
  604. Node *base = get_tree()->get_edited_scene_root();
  605. if (base) {
  606. base = _find_node_for_script(base, base, script);
  607. }
  608. String hint;
  609. Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint);
  610. r_options->sort_custom_inplace<CodeCompletionOptionCompare>();
  611. if (err == OK) {
  612. code_editor->get_text_editor()->set_code_hint(hint);
  613. }
  614. }
  615. void ScriptTextEditor::_update_breakpoint_list() {
  616. breakpoints_menu->clear();
  617. breakpoints_menu->reset_size();
  618. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  619. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  620. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  621. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  622. PackedInt32Array breakpoint_list = code_editor->get_text_editor()->get_breakpointed_lines();
  623. if (breakpoint_list.size() == 0) {
  624. return;
  625. }
  626. breakpoints_menu->add_separator();
  627. for (int i = 0; i < breakpoint_list.size(); i++) {
  628. // Strip edges to remove spaces or tabs.
  629. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  630. String line = code_editor->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
  631. // Limit the size of the line if too big.
  632. if (line.length() > 50) {
  633. line = line.substr(0, 50);
  634. }
  635. breakpoints_menu->add_item(String::num((int)breakpoint_list[i] + 1) + " - `" + line + "`");
  636. breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]);
  637. }
  638. }
  639. void ScriptTextEditor::_breakpoint_item_pressed(int p_idx) {
  640. if (p_idx < 4) { // Any item before the separator.
  641. _edit_option(breakpoints_menu->get_item_id(p_idx));
  642. } else {
  643. code_editor->goto_line(breakpoints_menu->get_item_metadata(p_idx));
  644. code_editor->get_text_editor()->call_deferred(SNAME("center_viewport_to_caret")); //Need to be deferred, because goto uses call_deferred().
  645. }
  646. }
  647. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  648. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_editor()->is_line_breakpointed(p_row));
  649. }
  650. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  651. Node *base = get_tree()->get_edited_scene_root();
  652. if (base) {
  653. base = _find_node_for_script(base, base, script);
  654. }
  655. ScriptLanguage::LookupResult result;
  656. if (ScriptServer::is_global_class(p_symbol)) {
  657. EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol));
  658. } else if (p_symbol.is_resource_file()) {
  659. List<String> scene_extensions;
  660. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  661. if (scene_extensions.find(p_symbol.get_extension())) {
  662. EditorNode::get_singleton()->load_scene(p_symbol);
  663. } else {
  664. EditorNode::get_singleton()->load_resource(p_symbol);
  665. }
  666. } else if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK) {
  667. _goto_line(p_row);
  668. switch (result.type) {
  669. case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION: {
  670. if (result.script.is_valid()) {
  671. emit_signal(SNAME("request_open_script_at_line"), result.script, result.location - 1);
  672. } else {
  673. emit_signal(SNAME("request_save_history"));
  674. goto_line_centered(result.location - 1);
  675. }
  676. } break;
  677. case ScriptLanguage::LOOKUP_RESULT_CLASS: {
  678. emit_signal(SNAME("go_to_help"), "class_name:" + result.class_name);
  679. } break;
  680. case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: {
  681. StringName cname = result.class_name;
  682. bool success;
  683. while (true) {
  684. ClassDB::get_integer_constant(cname, result.class_member, &success);
  685. if (success) {
  686. result.class_name = cname;
  687. cname = ClassDB::get_parent_class(cname);
  688. } else {
  689. break;
  690. }
  691. }
  692. emit_signal(SNAME("go_to_help"), "class_constant:" + result.class_name + ":" + result.class_member);
  693. } break;
  694. case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: {
  695. emit_signal(SNAME("go_to_help"), "class_property:" + result.class_name + ":" + result.class_member);
  696. } break;
  697. case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: {
  698. StringName cname = result.class_name;
  699. while (true) {
  700. if (ClassDB::has_method(cname, result.class_member)) {
  701. result.class_name = cname;
  702. cname = ClassDB::get_parent_class(cname);
  703. } else {
  704. break;
  705. }
  706. }
  707. emit_signal(SNAME("go_to_help"), "class_method:" + result.class_name + ":" + result.class_member);
  708. } break;
  709. case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: {
  710. StringName cname = result.class_name;
  711. while (true) {
  712. if (ClassDB::has_signal(cname, result.class_member)) {
  713. result.class_name = cname;
  714. cname = ClassDB::get_parent_class(cname);
  715. } else {
  716. break;
  717. }
  718. }
  719. emit_signal(SNAME("go_to_help"), "class_signal:" + result.class_name + ":" + result.class_member);
  720. } break;
  721. case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: {
  722. StringName cname = result.class_name;
  723. StringName success;
  724. while (true) {
  725. success = ClassDB::get_integer_constant_enum(cname, result.class_member, true);
  726. if (success != StringName()) {
  727. result.class_name = cname;
  728. cname = ClassDB::get_parent_class(cname);
  729. } else {
  730. break;
  731. }
  732. }
  733. emit_signal(SNAME("go_to_help"), "class_enum:" + result.class_name + ":" + result.class_member);
  734. } break;
  735. case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: {
  736. emit_signal(SNAME("go_to_help"), "class_annotation:" + result.class_name + ":" + result.class_member);
  737. } break;
  738. case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: {
  739. emit_signal(SNAME("go_to_help"), "class_global:" + result.class_name + ":" + result.class_member);
  740. } break;
  741. default: {
  742. }
  743. }
  744. } else if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  745. // Check for Autoload scenes.
  746. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  747. if (info.is_singleton) {
  748. EditorNode::get_singleton()->load_scene(info.path);
  749. }
  750. } else if (p_symbol.is_relative_path()) {
  751. // Every symbol other than absolute path is relative path so keep this condition at last.
  752. String path = _get_absolute_path(p_symbol);
  753. if (FileAccess::exists(path)) {
  754. List<String> scene_extensions;
  755. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  756. if (scene_extensions.find(path.get_extension())) {
  757. EditorNode::get_singleton()->load_scene(path);
  758. } else {
  759. EditorNode::get_singleton()->load_resource(path);
  760. }
  761. }
  762. }
  763. }
  764. void ScriptTextEditor::_validate_symbol(const String &p_symbol) {
  765. CodeEdit *text_edit = code_editor->get_text_editor();
  766. Node *base = get_tree()->get_edited_scene_root();
  767. if (base) {
  768. base = _find_node_for_script(base, base, script);
  769. }
  770. ScriptLanguage::LookupResult result;
  771. if (ScriptServer::is_global_class(p_symbol) || p_symbol.is_resource_file() || script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), p_symbol, script->get_path(), base, result) == OK || (ProjectSettings::get_singleton()->has_autoload(p_symbol) && ProjectSettings::get_singleton()->get_autoload(p_symbol).is_singleton)) {
  772. text_edit->set_symbol_lookup_word_as_valid(true);
  773. } else if (p_symbol.is_relative_path()) {
  774. String path = _get_absolute_path(p_symbol);
  775. if (FileAccess::exists(path)) {
  776. text_edit->set_symbol_lookup_word_as_valid(true);
  777. } else {
  778. text_edit->set_symbol_lookup_word_as_valid(false);
  779. }
  780. } else {
  781. text_edit->set_symbol_lookup_word_as_valid(false);
  782. }
  783. }
  784. String ScriptTextEditor::_get_absolute_path(const String &rel_path) {
  785. String base_path = script->get_path().get_base_dir();
  786. String path = base_path.path_join(rel_path);
  787. return path.replace("///", "//").simplify_path();
  788. }
  789. void ScriptTextEditor::update_toggle_scripts_button() {
  790. code_editor->update_toggle_scripts_button();
  791. }
  792. void ScriptTextEditor::_update_connected_methods() {
  793. CodeEdit *text_edit = code_editor->get_text_editor();
  794. text_edit->set_gutter_width(connection_gutter, text_edit->get_line_height());
  795. for (int i = 0; i < text_edit->get_line_count(); i++) {
  796. if (text_edit->get_line_gutter_metadata(i, connection_gutter) == "") {
  797. continue;
  798. }
  799. text_edit->set_line_gutter_metadata(i, connection_gutter, "");
  800. text_edit->set_line_gutter_icon(i, connection_gutter, nullptr);
  801. text_edit->set_line_gutter_clickable(i, connection_gutter, false);
  802. }
  803. missing_connections.clear();
  804. if (!script_is_valid) {
  805. return;
  806. }
  807. Node *base = get_tree()->get_edited_scene_root();
  808. if (!base) {
  809. return;
  810. }
  811. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  812. HashSet<StringName> methods_found;
  813. for (int i = 0; i < nodes.size(); i++) {
  814. List<Connection> connections;
  815. nodes[i]->get_signals_connected_to_this(&connections);
  816. for (const Connection &connection : connections) {
  817. if (!(connection.flags & CONNECT_PERSIST)) {
  818. continue;
  819. }
  820. // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree.
  821. Node *source = Object::cast_to<Node>(connection.signal.get_object());
  822. if (source && !source->is_inside_tree()) {
  823. continue;
  824. }
  825. const StringName method = connection.callable.get_method();
  826. if (methods_found.has(method)) {
  827. continue;
  828. }
  829. if (!ClassDB::has_method(script->get_instance_base_type(), method)) {
  830. int line = -1;
  831. for (int j = 0; j < functions.size(); j++) {
  832. String name = functions[j].get_slice(":", 0);
  833. if (name == method) {
  834. line = functions[j].get_slice(":", 1).to_int() - 1;
  835. text_edit->set_line_gutter_metadata(line, connection_gutter, method);
  836. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_theme_icon(SNAME("Slot"), SNAME("EditorIcons")));
  837. text_edit->set_line_gutter_clickable(line, connection_gutter, true);
  838. methods_found.insert(method);
  839. break;
  840. }
  841. }
  842. if (line >= 0) {
  843. continue;
  844. }
  845. // There is a chance that the method is inherited from another script.
  846. bool found_inherited_function = false;
  847. Ref<Script> inherited_script = script->get_base_script();
  848. while (!inherited_script.is_null()) {
  849. if (inherited_script->has_method(method)) {
  850. found_inherited_function = true;
  851. break;
  852. }
  853. inherited_script = inherited_script->get_base_script();
  854. }
  855. if (!found_inherited_function) {
  856. missing_connections.push_back(connection);
  857. }
  858. }
  859. }
  860. }
  861. }
  862. void ScriptTextEditor::_update_gutter_indexes() {
  863. for (int i = 0; i < code_editor->get_text_editor()->get_gutter_count(); i++) {
  864. if (code_editor->get_text_editor()->get_gutter_name(i) == "connection_gutter") {
  865. connection_gutter = i;
  866. continue;
  867. }
  868. if (code_editor->get_text_editor()->get_gutter_name(i) == "line_numbers") {
  869. line_number_gutter = i;
  870. continue;
  871. }
  872. }
  873. }
  874. void ScriptTextEditor::_gutter_clicked(int p_line, int p_gutter) {
  875. if (p_gutter != connection_gutter) {
  876. return;
  877. }
  878. String method = code_editor->get_text_editor()->get_line_gutter_metadata(p_line, p_gutter);
  879. if (method.is_empty()) {
  880. return;
  881. }
  882. Node *base = get_tree()->get_edited_scene_root();
  883. if (!base) {
  884. return;
  885. }
  886. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  887. connection_info_dialog->popup_connections(method, nodes);
  888. }
  889. void ScriptTextEditor::_edit_option(int p_op) {
  890. CodeEdit *tx = code_editor->get_text_editor();
  891. switch (p_op) {
  892. case EDIT_UNDO: {
  893. tx->undo();
  894. tx->call_deferred(SNAME("grab_focus"));
  895. } break;
  896. case EDIT_REDO: {
  897. tx->redo();
  898. tx->call_deferred(SNAME("grab_focus"));
  899. } break;
  900. case EDIT_CUT: {
  901. tx->cut();
  902. tx->call_deferred(SNAME("grab_focus"));
  903. } break;
  904. case EDIT_COPY: {
  905. tx->copy();
  906. tx->call_deferred(SNAME("grab_focus"));
  907. } break;
  908. case EDIT_PASTE: {
  909. tx->paste();
  910. tx->call_deferred(SNAME("grab_focus"));
  911. } break;
  912. case EDIT_SELECT_ALL: {
  913. tx->select_all();
  914. tx->call_deferred(SNAME("grab_focus"));
  915. } break;
  916. case EDIT_MOVE_LINE_UP: {
  917. code_editor->move_lines_up();
  918. } break;
  919. case EDIT_MOVE_LINE_DOWN: {
  920. code_editor->move_lines_down();
  921. } break;
  922. case EDIT_INDENT_LEFT: {
  923. Ref<Script> scr = script;
  924. if (scr.is_null()) {
  925. return;
  926. }
  927. tx->unindent_lines();
  928. } break;
  929. case EDIT_INDENT_RIGHT: {
  930. Ref<Script> scr = script;
  931. if (scr.is_null()) {
  932. return;
  933. }
  934. tx->indent_lines();
  935. } break;
  936. case EDIT_DELETE_LINE: {
  937. code_editor->delete_lines();
  938. } break;
  939. case EDIT_DUPLICATE_SELECTION: {
  940. code_editor->duplicate_selection();
  941. } break;
  942. case EDIT_TOGGLE_FOLD_LINE: {
  943. tx->toggle_foldable_line(tx->get_caret_line());
  944. tx->queue_redraw();
  945. } break;
  946. case EDIT_FOLD_ALL_LINES: {
  947. tx->fold_all_lines();
  948. tx->queue_redraw();
  949. } break;
  950. case EDIT_UNFOLD_ALL_LINES: {
  951. tx->unfold_all_lines();
  952. tx->queue_redraw();
  953. } break;
  954. case EDIT_TOGGLE_COMMENT: {
  955. _edit_option_toggle_inline_comment();
  956. } break;
  957. case EDIT_COMPLETE: {
  958. tx->request_code_completion(true);
  959. } break;
  960. case EDIT_AUTO_INDENT: {
  961. String text = tx->get_text();
  962. Ref<Script> scr = script;
  963. if (scr.is_null()) {
  964. return;
  965. }
  966. tx->begin_complex_operation();
  967. int begin, end;
  968. if (tx->has_selection()) {
  969. begin = tx->get_selection_from_line();
  970. end = tx->get_selection_to_line();
  971. // ignore if the cursor is not past the first column
  972. if (tx->get_selection_to_column() == 0) {
  973. end--;
  974. }
  975. } else {
  976. begin = 0;
  977. end = tx->get_line_count() - 1;
  978. }
  979. scr->get_language()->auto_indent_code(text, begin, end);
  980. Vector<String> lines = text.split("\n");
  981. for (int i = begin; i <= end; ++i) {
  982. tx->set_line(i, lines[i]);
  983. }
  984. tx->end_complex_operation();
  985. } break;
  986. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  987. trim_trailing_whitespace();
  988. } break;
  989. case EDIT_CONVERT_INDENT_TO_SPACES: {
  990. convert_indent_to_spaces();
  991. } break;
  992. case EDIT_CONVERT_INDENT_TO_TABS: {
  993. convert_indent_to_tabs();
  994. } break;
  995. case EDIT_PICK_COLOR: {
  996. color_panel->popup();
  997. } break;
  998. case EDIT_TO_UPPERCASE: {
  999. _convert_case(CodeTextEditor::UPPER);
  1000. } break;
  1001. case EDIT_TO_LOWERCASE: {
  1002. _convert_case(CodeTextEditor::LOWER);
  1003. } break;
  1004. case EDIT_CAPITALIZE: {
  1005. _convert_case(CodeTextEditor::CAPITALIZE);
  1006. } break;
  1007. case EDIT_EVALUATE: {
  1008. Expression expression;
  1009. Vector<String> lines = code_editor->get_text_editor()->get_selected_text().split("\n");
  1010. PackedStringArray results;
  1011. for (int i = 0; i < lines.size(); i++) {
  1012. String line = lines[i];
  1013. String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); //extract the whitespace at the beginning
  1014. if (expression.parse(line) == OK) {
  1015. Variant result = expression.execute(Array(), Variant(), false, true);
  1016. if (expression.get_error_text().is_empty()) {
  1017. results.push_back(whitespace + result.get_construct_string());
  1018. } else {
  1019. results.push_back(line);
  1020. }
  1021. } else {
  1022. results.push_back(line);
  1023. }
  1024. }
  1025. code_editor->get_text_editor()->begin_complex_operation(); //prevents creating a two-step undo
  1026. code_editor->get_text_editor()->insert_text_at_caret(String("\n").join(results));
  1027. code_editor->get_text_editor()->end_complex_operation();
  1028. } break;
  1029. case SEARCH_FIND: {
  1030. code_editor->get_find_replace_bar()->popup_search();
  1031. } break;
  1032. case SEARCH_FIND_NEXT: {
  1033. code_editor->get_find_replace_bar()->search_next();
  1034. } break;
  1035. case SEARCH_FIND_PREV: {
  1036. code_editor->get_find_replace_bar()->search_prev();
  1037. } break;
  1038. case SEARCH_REPLACE: {
  1039. code_editor->get_find_replace_bar()->popup_replace();
  1040. } break;
  1041. case SEARCH_IN_FILES: {
  1042. String selected_text = code_editor->get_text_editor()->get_selected_text();
  1043. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  1044. // So this will be delegated to the ScriptEditor.
  1045. emit_signal(SNAME("search_in_files_requested"), selected_text);
  1046. } break;
  1047. case REPLACE_IN_FILES: {
  1048. String selected_text = code_editor->get_text_editor()->get_selected_text();
  1049. emit_signal(SNAME("replace_in_files_requested"), selected_text);
  1050. } break;
  1051. case SEARCH_LOCATE_FUNCTION: {
  1052. quick_open->popup_dialog(get_functions());
  1053. quick_open->set_title(TTR("Go to Function"));
  1054. } break;
  1055. case SEARCH_GOTO_LINE: {
  1056. goto_line_dialog->popup_find_line(tx);
  1057. } break;
  1058. case BOOKMARK_TOGGLE: {
  1059. code_editor->toggle_bookmark();
  1060. } break;
  1061. case BOOKMARK_GOTO_NEXT: {
  1062. code_editor->goto_next_bookmark();
  1063. } break;
  1064. case BOOKMARK_GOTO_PREV: {
  1065. code_editor->goto_prev_bookmark();
  1066. } break;
  1067. case BOOKMARK_REMOVE_ALL: {
  1068. code_editor->remove_all_bookmarks();
  1069. } break;
  1070. case DEBUG_TOGGLE_BREAKPOINT: {
  1071. int line = tx->get_caret_line();
  1072. bool dobreak = !tx->is_line_breakpointed(line);
  1073. tx->set_line_as_breakpoint(line, dobreak);
  1074. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1075. } break;
  1076. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1077. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1078. for (int i = 0; i < bpoints.size(); i++) {
  1079. int line = bpoints[i];
  1080. bool dobreak = !tx->is_line_breakpointed(line);
  1081. tx->set_line_as_breakpoint(line, dobreak);
  1082. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1083. }
  1084. } break;
  1085. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1086. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1087. if (bpoints.size() <= 0) {
  1088. return;
  1089. }
  1090. int line = tx->get_caret_line();
  1091. // wrap around
  1092. if (line >= (int)bpoints[bpoints.size() - 1]) {
  1093. tx->unfold_line(bpoints[0]);
  1094. tx->set_caret_line(bpoints[0]);
  1095. tx->center_viewport_to_caret();
  1096. } else {
  1097. for (int i = 0; i < bpoints.size(); i++) {
  1098. int bline = bpoints[i];
  1099. if (bline > line) {
  1100. tx->unfold_line(bline);
  1101. tx->set_caret_line(bline);
  1102. tx->center_viewport_to_caret();
  1103. return;
  1104. }
  1105. }
  1106. }
  1107. } break;
  1108. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1109. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1110. if (bpoints.size() <= 0) {
  1111. return;
  1112. }
  1113. int line = tx->get_caret_line();
  1114. // wrap around
  1115. if (line <= (int)bpoints[0]) {
  1116. tx->unfold_line(bpoints[bpoints.size() - 1]);
  1117. tx->set_caret_line(bpoints[bpoints.size() - 1]);
  1118. tx->center_viewport_to_caret();
  1119. } else {
  1120. for (int i = bpoints.size() - 1; i >= 0; i--) {
  1121. int bline = bpoints[i];
  1122. if (bline < line) {
  1123. tx->unfold_line(bline);
  1124. tx->set_caret_line(bline);
  1125. tx->center_viewport_to_caret();
  1126. return;
  1127. }
  1128. }
  1129. }
  1130. } break;
  1131. case HELP_CONTEXTUAL: {
  1132. String text = tx->get_selected_text();
  1133. if (text.is_empty()) {
  1134. text = tx->get_word_under_caret();
  1135. }
  1136. if (!text.is_empty()) {
  1137. emit_signal(SNAME("request_help"), text);
  1138. }
  1139. } break;
  1140. case LOOKUP_SYMBOL: {
  1141. String text = tx->get_word_under_caret();
  1142. if (text.is_empty()) {
  1143. text = tx->get_selected_text();
  1144. }
  1145. if (!text.is_empty()) {
  1146. _lookup_symbol(text, tx->get_caret_line(), tx->get_caret_column());
  1147. }
  1148. } break;
  1149. }
  1150. }
  1151. void ScriptTextEditor::_edit_option_toggle_inline_comment() {
  1152. if (script.is_null()) {
  1153. return;
  1154. }
  1155. String delimiter = "#";
  1156. List<String> comment_delimiters;
  1157. script->get_language()->get_comment_delimiters(&comment_delimiters);
  1158. for (const String &script_delimiter : comment_delimiters) {
  1159. if (!script_delimiter.contains(" ")) {
  1160. delimiter = script_delimiter;
  1161. break;
  1162. }
  1163. }
  1164. code_editor->toggle_inline_comment(delimiter);
  1165. }
  1166. void ScriptTextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1167. ERR_FAIL_COND(p_highlighter.is_null());
  1168. highlighters[p_highlighter->_get_name()] = p_highlighter;
  1169. highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
  1170. }
  1171. void ScriptTextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1172. ERR_FAIL_COND(p_highlighter.is_null());
  1173. HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin();
  1174. while (el) {
  1175. int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key);
  1176. highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter);
  1177. ++el;
  1178. }
  1179. CodeEdit *te = code_editor->get_text_editor();
  1180. p_highlighter->_set_edited_resource(script);
  1181. te->set_syntax_highlighter(p_highlighter);
  1182. }
  1183. void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
  1184. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  1185. }
  1186. void ScriptTextEditor::_notification(int p_what) {
  1187. switch (p_what) {
  1188. case NOTIFICATION_THEME_CHANGED:
  1189. if (!editor_enabled) {
  1190. break;
  1191. }
  1192. if (is_visible_in_tree()) {
  1193. _update_warnings();
  1194. _update_errors();
  1195. }
  1196. [[fallthrough]];
  1197. case NOTIFICATION_ENTER_TREE: {
  1198. code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_line_height());
  1199. } break;
  1200. }
  1201. }
  1202. void ScriptTextEditor::_bind_methods() {
  1203. ClassDB::bind_method("_update_connected_methods", &ScriptTextEditor::_update_connected_methods);
  1204. ClassDB::bind_method("_get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
  1205. ClassDB::bind_method("_can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
  1206. ClassDB::bind_method("_drop_data_fw", &ScriptTextEditor::drop_data_fw);
  1207. }
  1208. Control *ScriptTextEditor::get_edit_menu() {
  1209. return edit_hb;
  1210. }
  1211. void ScriptTextEditor::clear_edit_menu() {
  1212. if (editor_enabled) {
  1213. memdelete(edit_hb);
  1214. }
  1215. }
  1216. void ScriptTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  1217. code_editor->set_find_replace_bar(p_bar);
  1218. }
  1219. void ScriptTextEditor::reload(bool p_soft) {
  1220. CodeEdit *te = code_editor->get_text_editor();
  1221. Ref<Script> scr = script;
  1222. if (scr.is_null()) {
  1223. return;
  1224. }
  1225. scr->set_source_code(te->get_text());
  1226. bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; // Always soft-reload editor plugins.
  1227. scr->get_language()->reload_tool_script(scr, soft);
  1228. }
  1229. PackedInt32Array ScriptTextEditor::get_breakpoints() {
  1230. return code_editor->get_text_editor()->get_breakpointed_lines();
  1231. }
  1232. void ScriptTextEditor::set_breakpoint(int p_line, bool p_enabled) {
  1233. code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
  1234. }
  1235. void ScriptTextEditor::clear_breakpoints() {
  1236. code_editor->get_text_editor()->clear_breakpointed_lines();
  1237. }
  1238. void ScriptTextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
  1239. Variant args[1] = { this };
  1240. const Variant *argp[] = { &args[0] };
  1241. code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
  1242. }
  1243. void ScriptTextEditor::set_debugger_active(bool p_active) {
  1244. }
  1245. Control *ScriptTextEditor::get_base_editor() const {
  1246. return code_editor->get_text_editor();
  1247. }
  1248. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1249. return Variant();
  1250. }
  1251. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1252. Dictionary d = p_data;
  1253. if (d.has("type") &&
  1254. (String(d["type"]) == "resource" ||
  1255. String(d["type"]) == "files" ||
  1256. String(d["type"]) == "nodes" ||
  1257. String(d["type"]) == "obj_property" ||
  1258. String(d["type"]) == "files_and_dirs")) {
  1259. return true;
  1260. }
  1261. return false;
  1262. }
  1263. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1264. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  1265. return nullptr;
  1266. }
  1267. Ref<Script> scr = p_current_node->get_script();
  1268. if (scr.is_valid() && scr == script) {
  1269. return p_current_node;
  1270. }
  1271. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1272. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1273. if (n) {
  1274. return n;
  1275. }
  1276. }
  1277. return nullptr;
  1278. }
  1279. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1280. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  1281. Dictionary d = p_data;
  1282. CodeEdit *te = code_editor->get_text_editor();
  1283. Point2i pos = te->get_line_column_at_pos(p_point);
  1284. int row = pos.y;
  1285. int col = pos.x;
  1286. if (d.has("type") && String(d["type"]) == "resource") {
  1287. Ref<Resource> res = d["resource"];
  1288. if (!res.is_valid()) {
  1289. return;
  1290. }
  1291. if (res->get_path().is_resource_file()) {
  1292. EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
  1293. return;
  1294. }
  1295. te->set_caret_line(row);
  1296. te->set_caret_column(col);
  1297. te->insert_text_at_caret(res->get_path());
  1298. te->grab_focus();
  1299. }
  1300. if (d.has("type") && (String(d["type"]) == "files" || String(d["type"]) == "files_and_dirs")) {
  1301. Array files = d["files"];
  1302. String text_to_drop;
  1303. bool preload = Input::get_singleton()->is_key_pressed(Key::CTRL);
  1304. for (int i = 0; i < files.size(); i++) {
  1305. if (i > 0) {
  1306. text_to_drop += ", ";
  1307. }
  1308. if (preload) {
  1309. text_to_drop += "preload(" + String(files[i]).c_escape().quote(quote_style) + ")";
  1310. } else {
  1311. text_to_drop += String(files[i]).c_escape().quote(quote_style);
  1312. }
  1313. }
  1314. te->set_caret_line(row);
  1315. te->set_caret_column(col);
  1316. te->insert_text_at_caret(text_to_drop);
  1317. te->grab_focus();
  1318. }
  1319. if (d.has("type") && String(d["type"]) == "nodes") {
  1320. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1321. if (!sn) {
  1322. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
  1323. return;
  1324. }
  1325. Array nodes = d["nodes"];
  1326. String text_to_drop;
  1327. if (Input::get_singleton()->is_key_pressed(Key::CTRL)) {
  1328. bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
  1329. for (int i = 0; i < nodes.size(); i++) {
  1330. NodePath np = nodes[i];
  1331. Node *node = get_node(np);
  1332. if (!node) {
  1333. continue;
  1334. }
  1335. bool is_unique = false;
  1336. String path;
  1337. if (node->is_unique_name_in_owner()) {
  1338. path = node->get_name();
  1339. is_unique = true;
  1340. } else {
  1341. path = sn->get_path_to(node);
  1342. }
  1343. for (const String &segment : path.split("/")) {
  1344. if (!segment.is_valid_identifier()) {
  1345. path = path.c_escape().quote(quote_style);
  1346. break;
  1347. }
  1348. }
  1349. String variable_name = String(node->get_name()).to_snake_case().validate_identifier();
  1350. if (use_type) {
  1351. text_to_drop += vformat("@onready var %s: %s = %s%s\n", variable_name, node->get_class_name(), is_unique ? "%" : "$", path);
  1352. } else {
  1353. text_to_drop += vformat("@onready var %s = %s%s\n", variable_name, is_unique ? "%" : "$", path);
  1354. }
  1355. }
  1356. } else {
  1357. for (int i = 0; i < nodes.size(); i++) {
  1358. if (i > 0) {
  1359. text_to_drop += ", ";
  1360. }
  1361. NodePath np = nodes[i];
  1362. Node *node = get_node(np);
  1363. if (!node) {
  1364. continue;
  1365. }
  1366. bool is_unique = false;
  1367. String path;
  1368. if (node->is_unique_name_in_owner()) {
  1369. path = node->get_name();
  1370. is_unique = true;
  1371. } else {
  1372. path = sn->get_path_to(node);
  1373. }
  1374. for (const String &segment : path.split("/")) {
  1375. if (!segment.is_valid_identifier()) {
  1376. path = path.c_escape().quote(quote_style);
  1377. break;
  1378. }
  1379. }
  1380. text_to_drop += (is_unique ? "%" : "$") + path;
  1381. }
  1382. }
  1383. te->set_caret_line(row);
  1384. te->set_caret_column(col);
  1385. te->insert_text_at_caret(text_to_drop);
  1386. te->grab_focus();
  1387. }
  1388. if (d.has("type") && String(d["type"]) == "obj_property") {
  1389. const String text_to_drop = String(d["property"]).c_escape().quote(quote_style);
  1390. te->set_caret_line(row);
  1391. te->set_caret_column(col);
  1392. te->insert_text_at_caret(text_to_drop);
  1393. te->grab_focus();
  1394. }
  1395. }
  1396. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  1397. Ref<InputEventMouseButton> mb = ev;
  1398. Ref<InputEventKey> k = ev;
  1399. Point2 local_pos;
  1400. bool create_menu = false;
  1401. CodeEdit *tx = code_editor->get_text_editor();
  1402. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1403. local_pos = mb->get_global_position() - tx->get_global_position();
  1404. create_menu = true;
  1405. } else if (k.is_valid() && k->is_action("ui_menu", true)) {
  1406. tx->adjust_viewport_to_caret();
  1407. local_pos = tx->get_caret_draw_pos();
  1408. create_menu = true;
  1409. }
  1410. if (create_menu) {
  1411. Point2i pos = tx->get_line_column_at_pos(local_pos);
  1412. int row = pos.y;
  1413. int col = pos.x;
  1414. tx->set_move_caret_on_right_click_enabled(EditorSettings::get_singleton()->get("text_editor/behavior/navigation/move_caret_on_right_click"));
  1415. if (tx->is_move_caret_on_right_click_enabled()) {
  1416. if (tx->has_selection()) {
  1417. int from_line = tx->get_selection_from_line();
  1418. int to_line = tx->get_selection_to_line();
  1419. int from_column = tx->get_selection_from_column();
  1420. int to_column = tx->get_selection_to_column();
  1421. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1422. // Right click is outside the selected text
  1423. tx->deselect();
  1424. }
  1425. }
  1426. if (!tx->has_selection()) {
  1427. tx->set_caret_line(row, false, false);
  1428. tx->set_caret_column(col);
  1429. }
  1430. }
  1431. String word_at_pos = tx->get_word_at_pos(local_pos);
  1432. if (word_at_pos.is_empty()) {
  1433. word_at_pos = tx->get_word_under_caret();
  1434. }
  1435. if (word_at_pos.is_empty()) {
  1436. word_at_pos = tx->get_selected_text();
  1437. }
  1438. bool has_color = (word_at_pos == "Color");
  1439. bool foldable = tx->can_fold_line(row) || tx->is_line_folded(row);
  1440. bool open_docs = false;
  1441. bool goto_definition = false;
  1442. if (word_at_pos.is_resource_file()) {
  1443. open_docs = true;
  1444. } else {
  1445. Node *base = get_tree()->get_edited_scene_root();
  1446. if (base) {
  1447. base = _find_node_for_script(base, base, script);
  1448. }
  1449. ScriptLanguage::LookupResult result;
  1450. if (script->get_language()->lookup_code(code_editor->get_text_editor()->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) {
  1451. open_docs = true;
  1452. }
  1453. }
  1454. if (has_color) {
  1455. String line = tx->get_line(row);
  1456. color_position.x = row;
  1457. color_position.y = col;
  1458. int begin = 0;
  1459. int end = 0;
  1460. bool valid = false;
  1461. for (int i = col; i < line.length(); i++) {
  1462. if (line[i] == '(') {
  1463. begin = i;
  1464. continue;
  1465. } else if (line[i] == ')') {
  1466. end = i + 1;
  1467. valid = true;
  1468. break;
  1469. }
  1470. }
  1471. if (valid) {
  1472. color_args = line.substr(begin, end - begin);
  1473. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  1474. Vector<float> color = stripped.split_floats(",");
  1475. if (color.size() > 2) {
  1476. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1477. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1478. }
  1479. color_panel->set_position(get_screen_position() + local_pos);
  1480. } else {
  1481. has_color = false;
  1482. }
  1483. }
  1484. _make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);
  1485. }
  1486. }
  1487. void ScriptTextEditor::_color_changed(const Color &p_color) {
  1488. String new_args;
  1489. if (p_color.a == 1.0f) {
  1490. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
  1491. } else {
  1492. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
  1493. }
  1494. String line = code_editor->get_text_editor()->get_line(color_position.x);
  1495. String line_with_replaced_args = line.replace(color_args, new_args);
  1496. color_args = new_args;
  1497. code_editor->get_text_editor()->begin_complex_operation();
  1498. code_editor->get_text_editor()->set_line(color_position.x, line_with_replaced_args);
  1499. code_editor->get_text_editor()->end_complex_operation();
  1500. code_editor->get_text_editor()->queue_redraw();
  1501. }
  1502. void ScriptTextEditor::_prepare_edit_menu() {
  1503. const CodeEdit *tx = code_editor->get_text_editor();
  1504. PopupMenu *popup = edit_menu->get_popup();
  1505. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  1506. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  1507. }
  1508. 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) {
  1509. context_menu->clear();
  1510. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  1511. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  1512. context_menu->add_separator();
  1513. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  1514. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  1515. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  1516. context_menu->add_separator();
  1517. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  1518. context_menu->add_separator();
  1519. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1520. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1521. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1522. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  1523. if (p_selection) {
  1524. context_menu->add_separator();
  1525. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  1526. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  1527. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  1528. }
  1529. if (p_foldable) {
  1530. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  1531. }
  1532. if (p_color || p_open_docs || p_goto_definition) {
  1533. context_menu->add_separator();
  1534. if (p_open_docs) {
  1535. context_menu->add_item(TTR("Lookup Symbol"), LOOKUP_SYMBOL);
  1536. }
  1537. if (p_color) {
  1538. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  1539. }
  1540. }
  1541. const CodeEdit *tx = code_editor->get_text_editor();
  1542. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  1543. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  1544. context_menu->set_position(get_screen_position() + p_pos);
  1545. context_menu->reset_size();
  1546. context_menu->popup();
  1547. }
  1548. void ScriptTextEditor::_enable_code_editor() {
  1549. ERR_FAIL_COND(code_editor->get_parent());
  1550. VSplitContainer *editor_box = memnew(VSplitContainer);
  1551. add_child(editor_box);
  1552. editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1553. editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1554. editor_box->add_child(code_editor);
  1555. code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel));
  1556. code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel));
  1557. code_editor->connect("validate_script", callable_mp(this, &ScriptTextEditor::_validate_script));
  1558. code_editor->connect("load_theme_settings", callable_mp(this, &ScriptTextEditor::_load_theme_settings));
  1559. code_editor->get_text_editor()->connect("symbol_lookup", callable_mp(this, &ScriptTextEditor::_lookup_symbol));
  1560. code_editor->get_text_editor()->connect("symbol_validate", callable_mp(this, &ScriptTextEditor::_validate_symbol));
  1561. code_editor->get_text_editor()->connect("gutter_added", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  1562. code_editor->get_text_editor()->connect("gutter_removed", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  1563. code_editor->get_text_editor()->connect("gutter_clicked", callable_mp(this, &ScriptTextEditor::_gutter_clicked));
  1564. code_editor->get_text_editor()->connect("gui_input", callable_mp(this, &ScriptTextEditor::_text_edit_gui_input));
  1565. code_editor->show_toggle_scripts_button();
  1566. _update_gutter_indexes();
  1567. editor_box->add_child(warnings_panel);
  1568. warnings_panel->add_theme_font_override(
  1569. "normal_font", EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("main"), SNAME("EditorFonts")));
  1570. warnings_panel->add_theme_font_size_override(
  1571. "normal_font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")));
  1572. warnings_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_warning_clicked));
  1573. editor_box->add_child(errors_panel);
  1574. errors_panel->add_theme_font_override(
  1575. "normal_font", EditorNode::get_singleton()->get_gui_base()->get_theme_font(SNAME("main"), SNAME("EditorFonts")));
  1576. errors_panel->add_theme_font_size_override(
  1577. "normal_font_size", EditorNode::get_singleton()->get_gui_base()->get_theme_font_size(SNAME("main_size"), SNAME("EditorFonts")));
  1578. errors_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_error_clicked));
  1579. add_child(context_menu);
  1580. context_menu->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
  1581. add_child(color_panel);
  1582. color_picker = memnew(ColorPicker);
  1583. color_picker->set_deferred_mode(true);
  1584. color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_color_changed));
  1585. color_panel->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(color_picker));
  1586. color_panel->add_child(color_picker);
  1587. quick_open = memnew(ScriptEditorQuickOpen);
  1588. quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line));
  1589. add_child(quick_open);
  1590. goto_line_dialog = memnew(GotoLineDialog);
  1591. add_child(goto_line_dialog);
  1592. add_child(connection_info_dialog);
  1593. edit_hb->add_child(search_menu);
  1594. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1595. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1596. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1597. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1598. search_menu->get_popup()->add_separator();
  1599. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  1600. search_menu->get_popup()->add_separator();
  1601. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1602. search_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
  1603. edit_hb->add_child(edit_menu);
  1604. edit_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_prepare_edit_menu));
  1605. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  1606. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  1607. edit_menu->get_popup()->add_separator();
  1608. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  1609. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  1610. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  1611. edit_menu->get_popup()->add_separator();
  1612. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  1613. edit_menu->get_popup()->add_separator();
  1614. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1615. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1616. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1617. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1618. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  1619. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1620. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  1621. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  1622. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  1623. edit_menu->get_popup()->add_separator();
  1624. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  1625. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
  1626. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  1627. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1628. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  1629. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  1630. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1631. edit_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
  1632. edit_menu->get_popup()->add_separator();
  1633. edit_menu->get_popup()->add_child(convert_case);
  1634. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  1635. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase"), KeyModifierMask::SHIFT | Key::F4), EDIT_TO_UPPERCASE);
  1636. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase"), KeyModifierMask::SHIFT | Key::F5), EDIT_TO_LOWERCASE);
  1637. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KeyModifierMask::SHIFT | Key::F6), EDIT_CAPITALIZE);
  1638. convert_case->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
  1639. edit_menu->get_popup()->add_child(highlighter_menu);
  1640. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
  1641. highlighter_menu->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_change_syntax_highlighter));
  1642. _load_theme_settings();
  1643. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  1644. edit_hb->add_child(goto_menu);
  1645. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1646. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1647. goto_menu->get_popup()->add_separator();
  1648. goto_menu->get_popup()->add_child(bookmarks_menu);
  1649. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
  1650. _update_bookmark_list();
  1651. bookmarks_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_bookmark_list));
  1652. bookmarks_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_bookmark_item_pressed));
  1653. goto_menu->get_popup()->add_child(breakpoints_menu);
  1654. goto_menu->get_popup()->add_submenu_item(TTR("Breakpoints"), "Breakpoints");
  1655. _update_breakpoint_list();
  1656. breakpoints_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_breakpoint_list));
  1657. breakpoints_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_breakpoint_item_pressed));
  1658. goto_menu->get_popup()->connect("id_pressed", callable_mp(this, &ScriptTextEditor::_edit_option));
  1659. }
  1660. ScriptTextEditor::ScriptTextEditor() {
  1661. code_editor = memnew(CodeTextEditor);
  1662. code_editor->add_theme_constant_override("separation", 2);
  1663. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  1664. code_editor->set_code_complete_func(_code_complete_scripts, this);
  1665. code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1666. code_editor->get_text_editor()->set_draw_breakpoints_gutter(true);
  1667. code_editor->get_text_editor()->set_draw_executing_lines_gutter(true);
  1668. code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled));
  1669. connection_gutter = 1;
  1670. code_editor->get_text_editor()->add_gutter(connection_gutter);
  1671. code_editor->get_text_editor()->set_gutter_name(connection_gutter, "connection_gutter");
  1672. code_editor->get_text_editor()->set_gutter_draw(connection_gutter, false);
  1673. code_editor->get_text_editor()->set_gutter_overwritable(connection_gutter, true);
  1674. code_editor->get_text_editor()->set_gutter_type(connection_gutter, TextEdit::GUTTER_TYPE_ICON);
  1675. warnings_panel = memnew(RichTextLabel);
  1676. warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  1677. warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1678. warnings_panel->set_meta_underline(true);
  1679. warnings_panel->set_selection_enabled(true);
  1680. warnings_panel->set_focus_mode(FOCUS_CLICK);
  1681. warnings_panel->hide();
  1682. errors_panel = memnew(RichTextLabel);
  1683. errors_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  1684. errors_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1685. errors_panel->set_meta_underline(true);
  1686. errors_panel->set_selection_enabled(true);
  1687. errors_panel->set_focus_mode(FOCUS_CLICK);
  1688. errors_panel->hide();
  1689. update_settings();
  1690. code_editor->get_text_editor()->set_code_hint_draw_below(EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"));
  1691. code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
  1692. code_editor->get_text_editor()->set_context_menu_enabled(false);
  1693. context_menu = memnew(PopupMenu);
  1694. color_panel = memnew(PopupPanel);
  1695. edit_hb = memnew(HBoxContainer);
  1696. edit_menu = memnew(MenuButton);
  1697. edit_menu->set_text(TTR("Edit"));
  1698. edit_menu->set_switch_on_hover(true);
  1699. edit_menu->set_shortcut_context(this);
  1700. convert_case = memnew(PopupMenu);
  1701. convert_case->set_name("convert_case");
  1702. highlighter_menu = memnew(PopupMenu);
  1703. highlighter_menu->set_name("highlighter_menu");
  1704. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  1705. plain_highlighter.instantiate();
  1706. add_syntax_highlighter(plain_highlighter);
  1707. Ref<EditorStandardSyntaxHighlighter> highlighter;
  1708. highlighter.instantiate();
  1709. add_syntax_highlighter(highlighter);
  1710. set_syntax_highlighter(highlighter);
  1711. search_menu = memnew(MenuButton);
  1712. search_menu->set_text(TTR("Search"));
  1713. search_menu->set_switch_on_hover(true);
  1714. search_menu->set_shortcut_context(this);
  1715. goto_menu = memnew(MenuButton);
  1716. goto_menu->set_text(TTR("Go To"));
  1717. goto_menu->set_switch_on_hover(true);
  1718. goto_menu->set_shortcut_context(this);
  1719. bookmarks_menu = memnew(PopupMenu);
  1720. bookmarks_menu->set_name("Bookmarks");
  1721. breakpoints_menu = memnew(PopupMenu);
  1722. breakpoints_menu->set_name("Breakpoints");
  1723. connection_info_dialog = memnew(ConnectionInfoDialog);
  1724. code_editor->get_text_editor()->set_drag_forwarding(this);
  1725. }
  1726. ScriptTextEditor::~ScriptTextEditor() {
  1727. highlighters.clear();
  1728. if (!editor_enabled) {
  1729. memdelete(code_editor);
  1730. memdelete(warnings_panel);
  1731. memdelete(errors_panel);
  1732. memdelete(context_menu);
  1733. memdelete(color_panel);
  1734. memdelete(edit_hb);
  1735. memdelete(edit_menu);
  1736. memdelete(convert_case);
  1737. memdelete(highlighter_menu);
  1738. memdelete(search_menu);
  1739. memdelete(goto_menu);
  1740. memdelete(bookmarks_menu);
  1741. memdelete(breakpoints_menu);
  1742. memdelete(connection_info_dialog);
  1743. }
  1744. }
  1745. static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) {
  1746. if (Object::cast_to<Script>(*p_resource)) {
  1747. return memnew(ScriptTextEditor);
  1748. }
  1749. return nullptr;
  1750. }
  1751. void ScriptTextEditor::register_editor() {
  1752. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KeyModifierMask::ALT | Key::UP);
  1753. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KeyModifierMask::ALT | Key::DOWN);
  1754. ED_SHORTCUT("script_text_editor/delete_line", TTR("Delete Line"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K);
  1755. // Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
  1756. // The next/previous in history shortcut in this case makes a lot more sense.
  1757. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), Key::NONE);
  1758. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), Key::NONE);
  1759. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KeyModifierMask::CMD_OR_CTRL | Key::K);
  1760. ED_SHORTCUT("script_text_editor/toggle_fold_line", TTR("Fold/Unfold Line"), KeyModifierMask::ALT | Key::F);
  1761. ED_SHORTCUT("script_text_editor/fold_all_lines", TTR("Fold All Lines"), Key::NONE);
  1762. ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), Key::NONE);
  1763. ED_SHORTCUT("script_text_editor/duplicate_selection", TTR("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D);
  1764. ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C);
  1765. ED_SHORTCUT("script_text_editor/evaluate_selection", TTR("Evaluate Selection"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::E);
  1766. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T);
  1767. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent to Spaces"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::Y);
  1768. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent to Tabs"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::I);
  1769. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KeyModifierMask::CMD_OR_CTRL | Key::I);
  1770. ED_SHORTCUT_AND_COMMAND("script_text_editor/find", TTR("Find..."), KeyModifierMask::CMD_OR_CTRL | Key::F);
  1771. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), Key::F3);
  1772. ED_SHORTCUT_OVERRIDE("script_text_editor/find_next", "macos", KeyModifierMask::META | Key::G);
  1773. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KeyModifierMask::SHIFT | Key::F3);
  1774. ED_SHORTCUT_OVERRIDE("script_text_editor/find_previous", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::G);
  1775. ED_SHORTCUT_AND_COMMAND("script_text_editor/replace", TTR("Replace..."), KeyModifierMask::CTRL | Key::R);
  1776. ED_SHORTCUT_OVERRIDE("script_text_editor/replace", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::F);
  1777. ED_SHORTCUT("script_text_editor/find_in_files", TTR("Find in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F);
  1778. ED_SHORTCUT("script_text_editor/replace_in_files", TTR("Replace in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R);
  1779. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KeyModifierMask::ALT | Key::F1);
  1780. ED_SHORTCUT_OVERRIDE("script_text_editor/contextual_help", "macos", KeyModifierMask::ALT | KeyModifierMask::SHIFT | Key::SPACE);
  1781. ED_SHORTCUT("script_text_editor/toggle_bookmark", TTR("Toggle Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::B);
  1782. ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTR("Go to Next Bookmark"), KeyModifierMask::CMD_OR_CTRL | Key::B);
  1783. ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTR("Go to Previous Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::B);
  1784. ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTR("Remove All Bookmarks"), Key::NONE);
  1785. ED_SHORTCUT("script_text_editor/goto_function", TTR("Go to Function..."), KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::F);
  1786. ED_SHORTCUT_OVERRIDE("script_text_editor/goto_function", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::J);
  1787. ED_SHORTCUT("script_text_editor/goto_line", TTR("Go to Line..."), KeyModifierMask::CMD_OR_CTRL | Key::L);
  1788. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), Key::F9);
  1789. ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_breakpoint", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
  1790. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F9);
  1791. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Go to Next Breakpoint"), KeyModifierMask::CMD_OR_CTRL | Key::PERIOD);
  1792. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Go to Previous Breakpoint"), KeyModifierMask::CMD_OR_CTRL | Key::COMMA);
  1793. ScriptEditor::register_create_script_editor_function(create_editor);
  1794. }
  1795. void ScriptTextEditor::validate() {
  1796. this->code_editor->validate_script();
  1797. }