script_text_editor.cpp 101 KB

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