script_text_editor.cpp 93 KB

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