script_text_editor.cpp 91 KB

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