script_text_editor.cpp 100 KB

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