script_text_editor.cpp 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719
  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_slicec(' ', 0);
  198. String end = string.get_slice_count(" ") > 1 ? string.get_slicec(' ', 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_slicec(' ', 0);
  211. String end = comment.get_slice_count(" ") > 1 ? comment.get_slicec(' ', 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_slicec(' ', 0);
  221. String end = doc_comment.get_slice_count(" ") > 1 ? doc_comment.get_slicec(' ', 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 (!EDITOR_GET("text_editor/behavior/documentation/enable_tooltips").booleanize()) {
  956. return;
  957. }
  958. if (p_symbol.begins_with("res://") || p_symbol.begins_with("uid://")) {
  959. EditorHelpBitTooltip::show_tooltip(code_editor->get_text_editor(), "resource||" + p_symbol);
  960. return;
  961. }
  962. Node *base = get_tree()->get_edited_scene_root();
  963. if (base) {
  964. base = _find_node_for_script(base, base, script);
  965. }
  966. ScriptLanguage::LookupResult result;
  967. String doc_symbol;
  968. const String code_text = code_editor->get_text_editor()->get_text_with_cursor_char(p_row, p_column);
  969. const Error lc_error = script->get_language()->lookup_code(code_text, p_symbol, script->get_path(), base, result);
  970. if (lc_error == OK) {
  971. switch (result.type) {
  972. case ScriptLanguage::LOOKUP_RESULT_CLASS: {
  973. doc_symbol = "class|" + result.class_name + "|";
  974. } break;
  975. case ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT: {
  976. StringName cname = result.class_name;
  977. while (ClassDB::class_exists(cname)) {
  978. if (ClassDB::has_integer_constant(cname, result.class_member, true)) {
  979. result.class_name = cname;
  980. break;
  981. }
  982. cname = ClassDB::get_parent_class(cname);
  983. }
  984. doc_symbol = "constant|" + result.class_name + "|" + result.class_member;
  985. } break;
  986. case ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY: {
  987. StringName cname = result.class_name;
  988. while (ClassDB::class_exists(cname)) {
  989. if (ClassDB::has_property(cname, result.class_member, true)) {
  990. result.class_name = cname;
  991. break;
  992. }
  993. cname = ClassDB::get_parent_class(cname);
  994. }
  995. doc_symbol = "property|" + result.class_name + "|" + result.class_member;
  996. } break;
  997. case ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD: {
  998. StringName cname = result.class_name;
  999. while (ClassDB::class_exists(cname)) {
  1000. if (ClassDB::has_method(cname, result.class_member, true)) {
  1001. result.class_name = cname;
  1002. break;
  1003. }
  1004. cname = ClassDB::get_parent_class(cname);
  1005. }
  1006. doc_symbol = "method|" + result.class_name + "|" + result.class_member;
  1007. } break;
  1008. case ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL: {
  1009. StringName cname = result.class_name;
  1010. while (ClassDB::class_exists(cname)) {
  1011. if (ClassDB::has_signal(cname, result.class_member, true)) {
  1012. result.class_name = cname;
  1013. break;
  1014. }
  1015. cname = ClassDB::get_parent_class(cname);
  1016. }
  1017. doc_symbol = "signal|" + result.class_name + "|" + result.class_member;
  1018. } break;
  1019. case ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM: {
  1020. StringName cname = result.class_name;
  1021. while (ClassDB::class_exists(cname)) {
  1022. if (ClassDB::has_enum(cname, result.class_member, true)) {
  1023. result.class_name = cname;
  1024. break;
  1025. }
  1026. cname = ClassDB::get_parent_class(cname);
  1027. }
  1028. doc_symbol = "enum|" + result.class_name + "|" + result.class_member;
  1029. } break;
  1030. case ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION: {
  1031. doc_symbol = "annotation|" + result.class_name + "|" + result.class_member;
  1032. } break;
  1033. case ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT:
  1034. case ScriptLanguage::LOOKUP_RESULT_LOCAL_VARIABLE: {
  1035. const String item_type = (result.type == ScriptLanguage::LOOKUP_RESULT_LOCAL_CONSTANT) ? "local_constant" : "local_variable";
  1036. Dictionary item_data;
  1037. item_data["description"] = result.description;
  1038. item_data["is_deprecated"] = result.is_deprecated;
  1039. item_data["deprecated_message"] = result.deprecated_message;
  1040. item_data["is_experimental"] = result.is_experimental;
  1041. item_data["experimental_message"] = result.experimental_message;
  1042. item_data["doc_type"] = result.doc_type;
  1043. item_data["enumeration"] = result.enumeration;
  1044. item_data["is_bitfield"] = result.is_bitfield;
  1045. item_data["value"] = result.value;
  1046. doc_symbol = item_type + "||" + p_symbol + "|" + JSON::stringify(item_data);
  1047. } break;
  1048. case ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION:
  1049. case ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE: // Deprecated.
  1050. case ScriptLanguage::LOOKUP_RESULT_MAX: {
  1051. // Nothing to do.
  1052. } break;
  1053. }
  1054. }
  1055. // NOTE: See also `ScriptEditor::_get_debug_tooltip()` for documentation tooltips disabled.
  1056. String debug_value = EditorDebuggerNode::get_singleton()->get_var_value(p_symbol);
  1057. if (!debug_value.is_empty()) {
  1058. constexpr int DISPLAY_LIMIT = 1024;
  1059. if (debug_value.size() > DISPLAY_LIMIT) {
  1060. debug_value = debug_value.left(DISPLAY_LIMIT) + "... " + TTR("(truncated)");
  1061. }
  1062. debug_value = TTR("Current value: ") + debug_value.replace("[", "[lb]");
  1063. }
  1064. if (!doc_symbol.is_empty() || !debug_value.is_empty()) {
  1065. EditorHelpBitTooltip::show_tooltip(code_editor->get_text_editor(), doc_symbol, debug_value, true);
  1066. }
  1067. }
  1068. String ScriptTextEditor::_get_absolute_path(const String &rel_path) {
  1069. String base_path = script->get_path().get_base_dir();
  1070. String path = base_path.path_join(rel_path);
  1071. return path.replace("///", "//").simplify_path();
  1072. }
  1073. void ScriptTextEditor::update_toggle_scripts_button() {
  1074. code_editor->update_toggle_scripts_button();
  1075. }
  1076. void ScriptTextEditor::_update_connected_methods() {
  1077. CodeEdit *text_edit = code_editor->get_text_editor();
  1078. text_edit->set_gutter_width(connection_gutter, text_edit->get_line_height());
  1079. for (int i = 0; i < text_edit->get_line_count(); i++) {
  1080. text_edit->set_line_gutter_metadata(i, connection_gutter, Dictionary());
  1081. text_edit->set_line_gutter_icon(i, connection_gutter, nullptr);
  1082. text_edit->set_line_gutter_clickable(i, connection_gutter, false);
  1083. }
  1084. missing_connections.clear();
  1085. if (!script_is_valid) {
  1086. return;
  1087. }
  1088. Node *base = get_tree()->get_edited_scene_root();
  1089. if (!base) {
  1090. return;
  1091. }
  1092. // Add connection icons to methods.
  1093. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  1094. HashSet<StringName> methods_found;
  1095. for (int i = 0; i < nodes.size(); i++) {
  1096. List<Connection> signal_connections;
  1097. nodes[i]->get_signals_connected_to_this(&signal_connections);
  1098. for (const Connection &connection : signal_connections) {
  1099. if (!(connection.flags & CONNECT_PERSIST)) {
  1100. continue;
  1101. }
  1102. // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree.
  1103. Node *source = Object::cast_to<Node>(connection.signal.get_object());
  1104. if (source && !source->is_inside_tree()) {
  1105. continue;
  1106. }
  1107. const StringName method = connection.callable.get_method();
  1108. if (methods_found.has(method)) {
  1109. continue;
  1110. }
  1111. if (!ClassDB::has_method(script->get_instance_base_type(), method)) {
  1112. int line = -1;
  1113. for (int j = 0; j < functions.size(); j++) {
  1114. String name = functions[j].get_slicec(':', 0);
  1115. if (name == method) {
  1116. Dictionary line_meta;
  1117. line_meta["type"] = "connection";
  1118. line_meta["method"] = method;
  1119. line = functions[j].get_slicec(':', 1).to_int() - 1;
  1120. text_edit->set_line_gutter_metadata(line, connection_gutter, line_meta);
  1121. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("Slot")));
  1122. text_edit->set_line_gutter_clickable(line, connection_gutter, true);
  1123. methods_found.insert(method);
  1124. break;
  1125. }
  1126. }
  1127. if (line >= 0) {
  1128. continue;
  1129. }
  1130. // There is a chance that the method is inherited from another script.
  1131. bool found_inherited_function = false;
  1132. Ref<Script> inherited_script = script->get_base_script();
  1133. while (inherited_script.is_valid()) {
  1134. if (inherited_script->has_method(method)) {
  1135. found_inherited_function = true;
  1136. break;
  1137. }
  1138. inherited_script = inherited_script->get_base_script();
  1139. }
  1140. if (!found_inherited_function) {
  1141. missing_connections.push_back(connection);
  1142. }
  1143. }
  1144. }
  1145. }
  1146. // Add override icons to methods.
  1147. methods_found.clear();
  1148. for (int i = 0; i < functions.size(); i++) {
  1149. String raw_name = functions[i].get_slicec(':', 0);
  1150. StringName name = StringName(raw_name);
  1151. if (methods_found.has(name)) {
  1152. continue;
  1153. }
  1154. // Account for inner classes by stripping the class names from the method,
  1155. // starting from the right since our inner class might be inside of another inner class.
  1156. int pos = raw_name.rfind_char('.');
  1157. if (pos != -1) {
  1158. name = raw_name.substr(pos + 1);
  1159. }
  1160. String found_base_class;
  1161. StringName base_class = script->get_instance_base_type();
  1162. Ref<Script> inherited_script = script->get_base_script();
  1163. while (inherited_script.is_valid()) {
  1164. if (inherited_script->has_method(name)) {
  1165. found_base_class = "script:" + inherited_script->get_path();
  1166. break;
  1167. }
  1168. base_class = inherited_script->get_instance_base_type();
  1169. inherited_script = inherited_script->get_base_script();
  1170. }
  1171. if (found_base_class.is_empty()) {
  1172. while (base_class) {
  1173. List<MethodInfo> methods;
  1174. ClassDB::get_method_list(base_class, &methods, true);
  1175. for (const MethodInfo &mi : methods) {
  1176. if (mi.name == name) {
  1177. found_base_class = "builtin:" + base_class;
  1178. break;
  1179. }
  1180. }
  1181. ClassDB::ClassInfo *base_class_ptr = ClassDB::classes.getptr(base_class)->inherits_ptr;
  1182. if (base_class_ptr == nullptr) {
  1183. break;
  1184. }
  1185. base_class = base_class_ptr->name;
  1186. }
  1187. }
  1188. if (!found_base_class.is_empty()) {
  1189. int line = functions[i].get_slicec(':', 1).to_int() - 1;
  1190. Dictionary line_meta = text_edit->get_line_gutter_metadata(line, connection_gutter);
  1191. if (line_meta.is_empty()) {
  1192. // Add override icon to gutter.
  1193. line_meta["type"] = "inherits";
  1194. line_meta["method"] = name;
  1195. line_meta["base_class"] = found_base_class;
  1196. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverride")));
  1197. text_edit->set_line_gutter_clickable(line, connection_gutter, true);
  1198. } else {
  1199. // If method is also connected to signal, then merge icons and keep the click behavior of the slot.
  1200. text_edit->set_line_gutter_icon(line, connection_gutter, get_parent_control()->get_editor_theme_icon(SNAME("MethodOverrideAndSlot")));
  1201. }
  1202. methods_found.insert(StringName(raw_name));
  1203. }
  1204. }
  1205. }
  1206. void ScriptTextEditor::_update_gutter_indexes() {
  1207. for (int i = 0; i < code_editor->get_text_editor()->get_gutter_count(); i++) {
  1208. if (code_editor->get_text_editor()->get_gutter_name(i) == "connection_gutter") {
  1209. connection_gutter = i;
  1210. continue;
  1211. }
  1212. if (code_editor->get_text_editor()->get_gutter_name(i) == "line_numbers") {
  1213. line_number_gutter = i;
  1214. continue;
  1215. }
  1216. }
  1217. }
  1218. void ScriptTextEditor::_gutter_clicked(int p_line, int p_gutter) {
  1219. if (p_gutter != connection_gutter) {
  1220. return;
  1221. }
  1222. Dictionary meta = code_editor->get_text_editor()->get_line_gutter_metadata(p_line, p_gutter);
  1223. String type = meta.get("type", "");
  1224. if (type.is_empty()) {
  1225. return;
  1226. }
  1227. // All types currently need a method name.
  1228. String method = meta.get("method", "");
  1229. if (method.is_empty()) {
  1230. return;
  1231. }
  1232. if (type == "connection") {
  1233. Node *base = get_tree()->get_edited_scene_root();
  1234. if (!base) {
  1235. return;
  1236. }
  1237. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  1238. connection_info_dialog->popup_connections(method, nodes);
  1239. } else if (type == "inherits") {
  1240. String base_class_raw = meta["base_class"];
  1241. PackedStringArray base_class_split = base_class_raw.split(":", true, 1);
  1242. if (base_class_split[0] == "script") {
  1243. // Go to function declaration.
  1244. Ref<Script> base_script = ResourceLoader::load(base_class_split[1]);
  1245. ERR_FAIL_COND(base_script.is_null());
  1246. emit_signal(SNAME("go_to_method"), base_script, method);
  1247. } else if (base_class_split[0] == "builtin") {
  1248. // Open method documentation.
  1249. emit_signal(SNAME("go_to_help"), "class_method:" + base_class_split[1] + ":" + method);
  1250. }
  1251. }
  1252. }
  1253. void ScriptTextEditor::_edit_option(int p_op) {
  1254. CodeEdit *tx = code_editor->get_text_editor();
  1255. tx->apply_ime();
  1256. switch (p_op) {
  1257. case EDIT_UNDO: {
  1258. tx->undo();
  1259. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1260. } break;
  1261. case EDIT_REDO: {
  1262. tx->redo();
  1263. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1264. } break;
  1265. case EDIT_CUT: {
  1266. tx->cut();
  1267. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1268. } break;
  1269. case EDIT_COPY: {
  1270. tx->copy();
  1271. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1272. } break;
  1273. case EDIT_PASTE: {
  1274. tx->paste();
  1275. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1276. } break;
  1277. case EDIT_SELECT_ALL: {
  1278. tx->select_all();
  1279. callable_mp((Control *)tx, &Control::grab_focus).call_deferred();
  1280. } break;
  1281. case EDIT_MOVE_LINE_UP: {
  1282. code_editor->get_text_editor()->move_lines_up();
  1283. } break;
  1284. case EDIT_MOVE_LINE_DOWN: {
  1285. code_editor->get_text_editor()->move_lines_down();
  1286. } break;
  1287. case EDIT_INDENT: {
  1288. Ref<Script> scr = script;
  1289. if (scr.is_null()) {
  1290. return;
  1291. }
  1292. tx->indent_lines();
  1293. } break;
  1294. case EDIT_UNINDENT: {
  1295. Ref<Script> scr = script;
  1296. if (scr.is_null()) {
  1297. return;
  1298. }
  1299. tx->unindent_lines();
  1300. } break;
  1301. case EDIT_DELETE_LINE: {
  1302. code_editor->get_text_editor()->delete_lines();
  1303. } break;
  1304. case EDIT_DUPLICATE_SELECTION: {
  1305. code_editor->get_text_editor()->duplicate_selection();
  1306. } break;
  1307. case EDIT_DUPLICATE_LINES: {
  1308. code_editor->get_text_editor()->duplicate_lines();
  1309. } break;
  1310. case EDIT_TOGGLE_FOLD_LINE: {
  1311. tx->toggle_foldable_lines_at_carets();
  1312. } break;
  1313. case EDIT_FOLD_ALL_LINES: {
  1314. tx->fold_all_lines();
  1315. tx->queue_redraw();
  1316. } break;
  1317. case EDIT_UNFOLD_ALL_LINES: {
  1318. tx->unfold_all_lines();
  1319. tx->queue_redraw();
  1320. } break;
  1321. case EDIT_CREATE_CODE_REGION: {
  1322. tx->create_code_region();
  1323. } break;
  1324. case EDIT_TOGGLE_COMMENT: {
  1325. _edit_option_toggle_inline_comment();
  1326. } break;
  1327. case EDIT_COMPLETE: {
  1328. tx->request_code_completion(true);
  1329. } break;
  1330. case EDIT_AUTO_INDENT: {
  1331. String text = tx->get_text();
  1332. Ref<Script> scr = script;
  1333. if (scr.is_null()) {
  1334. return;
  1335. }
  1336. tx->begin_complex_operation();
  1337. tx->begin_multicaret_edit();
  1338. int begin = tx->get_line_count() - 1, end = 0;
  1339. if (tx->has_selection()) {
  1340. // Auto indent all lines that have a caret or selection on it.
  1341. Vector<Point2i> line_ranges = tx->get_line_ranges_from_carets();
  1342. for (Point2i line_range : line_ranges) {
  1343. scr->get_language()->auto_indent_code(text, line_range.x, line_range.y);
  1344. if (line_range.x < begin) {
  1345. begin = line_range.x;
  1346. }
  1347. if (line_range.y > end) {
  1348. end = line_range.y;
  1349. }
  1350. }
  1351. } else {
  1352. // Auto indent entire text.
  1353. begin = 0;
  1354. end = tx->get_line_count() - 1;
  1355. scr->get_language()->auto_indent_code(text, begin, end);
  1356. }
  1357. // Apply auto indented code.
  1358. Vector<String> lines = text.split("\n");
  1359. for (int i = begin; i <= end; ++i) {
  1360. tx->set_line(i, lines[i]);
  1361. }
  1362. tx->end_multicaret_edit();
  1363. tx->end_complex_operation();
  1364. } break;
  1365. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  1366. trim_trailing_whitespace();
  1367. } break;
  1368. case EDIT_TRIM_FINAL_NEWLINES: {
  1369. trim_final_newlines();
  1370. } break;
  1371. case EDIT_CONVERT_INDENT_TO_SPACES: {
  1372. code_editor->set_indent_using_spaces(true);
  1373. convert_indent();
  1374. } break;
  1375. case EDIT_CONVERT_INDENT_TO_TABS: {
  1376. code_editor->set_indent_using_spaces(false);
  1377. convert_indent();
  1378. } break;
  1379. case EDIT_PICK_COLOR: {
  1380. color_panel->popup();
  1381. } break;
  1382. case EDIT_TO_UPPERCASE: {
  1383. _convert_case(CodeTextEditor::UPPER);
  1384. } break;
  1385. case EDIT_TO_LOWERCASE: {
  1386. _convert_case(CodeTextEditor::LOWER);
  1387. } break;
  1388. case EDIT_CAPITALIZE: {
  1389. _convert_case(CodeTextEditor::CAPITALIZE);
  1390. } break;
  1391. case EDIT_EVALUATE: {
  1392. Expression expression;
  1393. tx->begin_complex_operation();
  1394. for (int caret_idx = 0; caret_idx < tx->get_caret_count(); caret_idx++) {
  1395. Vector<String> lines = tx->get_selected_text(caret_idx).split("\n");
  1396. PackedStringArray results;
  1397. for (int i = 0; i < lines.size(); i++) {
  1398. const String &line = lines[i];
  1399. String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); // Extract the whitespace at the beginning.
  1400. if (expression.parse(line) == OK) {
  1401. Variant result = expression.execute(Array(), Variant(), false, true);
  1402. if (expression.get_error_text().is_empty()) {
  1403. results.push_back(whitespace + result.get_construct_string());
  1404. } else {
  1405. results.push_back(line);
  1406. }
  1407. } else {
  1408. results.push_back(line);
  1409. }
  1410. }
  1411. tx->insert_text_at_caret(String("\n").join(results), caret_idx);
  1412. }
  1413. tx->end_complex_operation();
  1414. } break;
  1415. case EDIT_TOGGLE_WORD_WRAP: {
  1416. TextEdit::LineWrappingMode wrap = code_editor->get_text_editor()->get_line_wrapping_mode();
  1417. code_editor->get_text_editor()->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
  1418. } break;
  1419. case SEARCH_FIND: {
  1420. code_editor->get_find_replace_bar()->popup_search();
  1421. } break;
  1422. case SEARCH_FIND_NEXT: {
  1423. code_editor->get_find_replace_bar()->search_next();
  1424. } break;
  1425. case SEARCH_FIND_PREV: {
  1426. code_editor->get_find_replace_bar()->search_prev();
  1427. } break;
  1428. case SEARCH_REPLACE: {
  1429. code_editor->get_find_replace_bar()->popup_replace();
  1430. } break;
  1431. case SEARCH_IN_FILES: {
  1432. String selected_text = tx->get_selected_text();
  1433. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  1434. // So this will be delegated to the ScriptEditor.
  1435. emit_signal(SNAME("search_in_files_requested"), selected_text);
  1436. } break;
  1437. case REPLACE_IN_FILES: {
  1438. String selected_text = tx->get_selected_text();
  1439. emit_signal(SNAME("replace_in_files_requested"), selected_text);
  1440. } break;
  1441. case SEARCH_LOCATE_FUNCTION: {
  1442. quick_open->popup_dialog(get_functions());
  1443. quick_open->set_title(TTR("Go to Function"));
  1444. } break;
  1445. case SEARCH_GOTO_LINE: {
  1446. goto_line_popup->popup_find_line(code_editor);
  1447. } break;
  1448. case BOOKMARK_TOGGLE: {
  1449. code_editor->toggle_bookmark();
  1450. } break;
  1451. case BOOKMARK_GOTO_NEXT: {
  1452. code_editor->goto_next_bookmark();
  1453. } break;
  1454. case BOOKMARK_GOTO_PREV: {
  1455. code_editor->goto_prev_bookmark();
  1456. } break;
  1457. case BOOKMARK_REMOVE_ALL: {
  1458. code_editor->remove_all_bookmarks();
  1459. } break;
  1460. case DEBUG_TOGGLE_BREAKPOINT: {
  1461. Vector<int> sorted_carets = tx->get_sorted_carets();
  1462. int last_line = -1;
  1463. for (const int &c : sorted_carets) {
  1464. int from = tx->get_selection_from_line(c);
  1465. from += from == last_line ? 1 : 0;
  1466. int to = tx->get_selection_to_line(c);
  1467. if (to < from) {
  1468. continue;
  1469. }
  1470. // Check first if there's any lines with breakpoints in the selection.
  1471. bool selection_has_breakpoints = false;
  1472. for (int line = from; line <= to; line++) {
  1473. if (tx->is_line_breakpointed(line)) {
  1474. selection_has_breakpoints = true;
  1475. break;
  1476. }
  1477. }
  1478. // Set breakpoint on caret or remove all bookmarks from the selection.
  1479. if (!selection_has_breakpoints) {
  1480. if (tx->get_caret_line(c) != last_line) {
  1481. tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
  1482. }
  1483. } else {
  1484. for (int line = from; line <= to; line++) {
  1485. tx->set_line_as_breakpoint(line, false);
  1486. }
  1487. }
  1488. last_line = to;
  1489. }
  1490. } break;
  1491. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1492. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1493. for (int i = 0; i < bpoints.size(); i++) {
  1494. int line = bpoints[i];
  1495. bool dobreak = !tx->is_line_breakpointed(line);
  1496. tx->set_line_as_breakpoint(line, dobreak);
  1497. EditorDebuggerNode::get_singleton()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1498. }
  1499. } break;
  1500. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1501. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1502. if (bpoints.size() <= 0) {
  1503. return;
  1504. }
  1505. int current_line = tx->get_caret_line();
  1506. int bpoint_idx = 0;
  1507. if (current_line < (int)bpoints[bpoints.size() - 1]) {
  1508. while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
  1509. bpoint_idx++;
  1510. }
  1511. }
  1512. code_editor->goto_line_centered(bpoints[bpoint_idx]);
  1513. } break;
  1514. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1515. PackedInt32Array bpoints = tx->get_breakpointed_lines();
  1516. if (bpoints.size() <= 0) {
  1517. return;
  1518. }
  1519. int current_line = tx->get_caret_line();
  1520. int bpoint_idx = bpoints.size() - 1;
  1521. if (current_line > (int)bpoints[0]) {
  1522. while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
  1523. bpoint_idx--;
  1524. }
  1525. }
  1526. code_editor->goto_line_centered(bpoints[bpoint_idx]);
  1527. } break;
  1528. case HELP_CONTEXTUAL: {
  1529. String text = tx->get_selected_text(0);
  1530. if (text.is_empty()) {
  1531. text = tx->get_word_under_caret(0);
  1532. }
  1533. if (!text.is_empty()) {
  1534. emit_signal(SNAME("request_help"), text);
  1535. }
  1536. } break;
  1537. case LOOKUP_SYMBOL: {
  1538. String text = tx->get_word_under_caret(0);
  1539. if (text.is_empty()) {
  1540. text = tx->get_selected_text(0);
  1541. }
  1542. if (!text.is_empty()) {
  1543. _lookup_symbol(text, tx->get_caret_line(0), tx->get_caret_column(0));
  1544. }
  1545. } break;
  1546. case EDIT_EMOJI_AND_SYMBOL: {
  1547. code_editor->get_text_editor()->show_emoji_and_symbol_picker();
  1548. } break;
  1549. default: {
  1550. if (p_op >= EditorContextMenuPlugin::BASE_ID) {
  1551. EditorContextMenuPluginManager::get_singleton()->activate_custom_option(EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, p_op, tx);
  1552. }
  1553. }
  1554. }
  1555. }
  1556. void ScriptTextEditor::_edit_option_toggle_inline_comment() {
  1557. if (script.is_null()) {
  1558. return;
  1559. }
  1560. String delimiter = "#";
  1561. List<String> comment_delimiters;
  1562. script->get_language()->get_comment_delimiters(&comment_delimiters);
  1563. for (const String &script_delimiter : comment_delimiters) {
  1564. if (!script_delimiter.contains_char(' ')) {
  1565. delimiter = script_delimiter;
  1566. break;
  1567. }
  1568. }
  1569. code_editor->toggle_inline_comment(delimiter);
  1570. }
  1571. void ScriptTextEditor::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1572. ERR_FAIL_COND(p_highlighter.is_null());
  1573. highlighters[p_highlighter->_get_name()] = p_highlighter;
  1574. highlighter_menu->add_radio_check_item(p_highlighter->_get_name());
  1575. }
  1576. void ScriptTextEditor::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
  1577. ERR_FAIL_COND(p_highlighter.is_null());
  1578. HashMap<String, Ref<EditorSyntaxHighlighter>>::Iterator el = highlighters.begin();
  1579. while (el) {
  1580. int highlighter_index = highlighter_menu->get_item_idx_from_text(el->key);
  1581. highlighter_menu->set_item_checked(highlighter_index, el->value == p_highlighter);
  1582. ++el;
  1583. }
  1584. CodeEdit *te = code_editor->get_text_editor();
  1585. p_highlighter->_set_edited_resource(script);
  1586. te->set_syntax_highlighter(p_highlighter);
  1587. }
  1588. void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
  1589. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  1590. }
  1591. void ScriptTextEditor::_notification(int p_what) {
  1592. switch (p_what) {
  1593. case NOTIFICATION_THEME_CHANGED:
  1594. if (!editor_enabled) {
  1595. break;
  1596. }
  1597. if (is_visible_in_tree()) {
  1598. _update_errors();
  1599. _update_warnings();
  1600. }
  1601. [[fallthrough]];
  1602. case NOTIFICATION_ENTER_TREE: {
  1603. code_editor->get_text_editor()->set_gutter_width(connection_gutter, code_editor->get_text_editor()->get_line_height());
  1604. } break;
  1605. }
  1606. }
  1607. Control *ScriptTextEditor::get_edit_menu() {
  1608. return edit_hb;
  1609. }
  1610. void ScriptTextEditor::clear_edit_menu() {
  1611. if (editor_enabled) {
  1612. memdelete(edit_hb);
  1613. }
  1614. }
  1615. void ScriptTextEditor::set_find_replace_bar(FindReplaceBar *p_bar) {
  1616. code_editor->set_find_replace_bar(p_bar);
  1617. }
  1618. void ScriptTextEditor::reload(bool p_soft) {
  1619. CodeEdit *te = code_editor->get_text_editor();
  1620. Ref<Script> scr = script;
  1621. if (scr.is_null()) {
  1622. return;
  1623. }
  1624. scr->set_source_code(te->get_text());
  1625. bool soft = p_soft || ClassDB::is_parent_class(scr->get_instance_base_type(), "EditorPlugin"); // Always soft-reload editor plugins.
  1626. scr->get_language()->reload_tool_script(scr, soft);
  1627. }
  1628. PackedInt32Array ScriptTextEditor::get_breakpoints() {
  1629. return code_editor->get_text_editor()->get_breakpointed_lines();
  1630. }
  1631. void ScriptTextEditor::set_breakpoint(int p_line, bool p_enabled) {
  1632. code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
  1633. }
  1634. void ScriptTextEditor::clear_breakpoints() {
  1635. code_editor->get_text_editor()->clear_breakpointed_lines();
  1636. }
  1637. void ScriptTextEditor::set_tooltip_request_func(const Callable &p_toolip_callback) {
  1638. Variant args[1] = { this };
  1639. const Variant *argp[] = { &args[0] };
  1640. code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
  1641. }
  1642. void ScriptTextEditor::set_debugger_active(bool p_active) {
  1643. }
  1644. Control *ScriptTextEditor::get_base_editor() const {
  1645. return code_editor->get_text_editor();
  1646. }
  1647. CodeTextEditor *ScriptTextEditor::get_code_editor() const {
  1648. return code_editor;
  1649. }
  1650. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1651. return Variant();
  1652. }
  1653. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1654. Dictionary d = p_data;
  1655. if (d.has("type") &&
  1656. (String(d["type"]) == "resource" ||
  1657. String(d["type"]) == "files" ||
  1658. String(d["type"]) == "nodes" ||
  1659. String(d["type"]) == "obj_property" ||
  1660. String(d["type"]) == "files_and_dirs")) {
  1661. return true;
  1662. }
  1663. return false;
  1664. }
  1665. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1666. // Check scripts only for the nodes belonging to the edited scene.
  1667. if (p_current_node == p_edited_scene || p_current_node->get_owner() == p_edited_scene) {
  1668. Ref<Script> scr = p_current_node->get_script();
  1669. if (scr.is_valid() && scr == script) {
  1670. return p_current_node;
  1671. }
  1672. }
  1673. // Traverse all children, even the ones not owned by the edited scene as they
  1674. // can still have child nodes added within the edited scene and thus owned by
  1675. // it (e.g. nodes added to subscene's root or to its editable children).
  1676. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1677. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1678. if (n) {
  1679. return n;
  1680. }
  1681. }
  1682. return nullptr;
  1683. }
  1684. static String _quote_drop_data(const String &str) {
  1685. // This function prepares a string for being "dropped" into the script editor.
  1686. // The string can be a resource path, node path or property name.
  1687. const bool using_single_quotes = EDITOR_GET("text_editor/completion/use_single_quotes");
  1688. String escaped = str.c_escape();
  1689. // If string is double quoted, there is no need to escape single quotes.
  1690. // We can revert the extra escaping added in c_escape().
  1691. if (!using_single_quotes) {
  1692. escaped = escaped.replace("\\'", "\'");
  1693. }
  1694. return escaped.quote(using_single_quotes ? "'" : "\"");
  1695. }
  1696. static String _get_dropped_resource_line(const Ref<Resource> &p_resource, bool p_create_field, bool p_allow_uid) {
  1697. String path = p_resource->get_path();
  1698. if (p_allow_uid) {
  1699. ResourceUID::ID id = ResourceLoader::get_resource_uid(path);
  1700. if (id != ResourceUID::INVALID_ID) {
  1701. path = ResourceUID::get_singleton()->id_to_text(id);
  1702. }
  1703. }
  1704. const bool is_script = ClassDB::is_parent_class(p_resource->get_class(), "Script");
  1705. if (!p_create_field) {
  1706. return vformat("preload(%s)", _quote_drop_data(path));
  1707. }
  1708. String variable_name = p_resource->get_name();
  1709. if (variable_name.is_empty()) {
  1710. variable_name = p_resource->get_path().get_file().get_basename();
  1711. }
  1712. if (is_script) {
  1713. variable_name = variable_name.to_pascal_case().validate_unicode_identifier();
  1714. } else {
  1715. variable_name = variable_name.to_snake_case().to_upper().validate_unicode_identifier();
  1716. }
  1717. return vformat("const %s = preload(%s)", variable_name, _quote_drop_data(path));
  1718. }
  1719. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1720. Dictionary d = p_data;
  1721. CodeEdit *te = code_editor->get_text_editor();
  1722. Point2i pos = te->get_line_column_at_pos(p_point);
  1723. int drop_at_line = pos.y;
  1724. int drop_at_column = pos.x;
  1725. int selection_index = te->get_selection_at_line_column(drop_at_line, drop_at_column);
  1726. bool line_will_be_empty = false;
  1727. if (selection_index >= 0) {
  1728. // Dropped on a selection, it will be replaced.
  1729. drop_at_line = te->get_selection_from_line(selection_index);
  1730. drop_at_column = te->get_selection_from_column(selection_index);
  1731. 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();
  1732. }
  1733. String text_to_drop;
  1734. const bool drop_modifier_pressed = Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
  1735. const bool allow_uid = Input::get_singleton()->is_key_pressed(Key::SHIFT) != bool(EDITOR_GET("text_editor/behavior/files/drop_preload_resources_as_uid"));
  1736. const String &line = te->get_line(drop_at_line);
  1737. const bool is_empty_line = line_will_be_empty || line.is_empty() || te->get_first_non_whitespace_column(drop_at_line) == line.length();
  1738. const String type = d.get("type", "");
  1739. if (type == "resource") {
  1740. Ref<Resource> resource = d["resource"];
  1741. if (resource.is_null()) {
  1742. return;
  1743. }
  1744. const String &path = resource->get_path();
  1745. if (path.is_empty() || path.ends_with("::")) {
  1746. 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.");
  1747. EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR);
  1748. return;
  1749. }
  1750. if (drop_modifier_pressed) {
  1751. if (resource->is_built_in()) {
  1752. String warning = TTR("Preloading internal resources is not supported.");
  1753. EditorToaster::get_singleton()->popup_str(warning, EditorToaster::SEVERITY_ERROR);
  1754. } else {
  1755. text_to_drop = _get_dropped_resource_line(resource, is_empty_line, allow_uid);
  1756. }
  1757. } else {
  1758. text_to_drop = _quote_drop_data(path);
  1759. }
  1760. }
  1761. if (type == "files" || type == "files_and_dirs") {
  1762. const PackedStringArray files = d["files"];
  1763. PackedStringArray parts;
  1764. for (const String &path : files) {
  1765. if (drop_modifier_pressed && ResourceLoader::exists(path)) {
  1766. Ref<Resource> resource = ResourceLoader::load(path);
  1767. if (resource.is_null()) {
  1768. // Resource exists, but failed to load. We need only path and name, so we can use a dummy Resource instead.
  1769. resource.instantiate();
  1770. resource->set_path_cache(path);
  1771. }
  1772. parts.append(_get_dropped_resource_line(resource, is_empty_line, allow_uid));
  1773. } else {
  1774. parts.append(_quote_drop_data(path));
  1775. }
  1776. }
  1777. text_to_drop = String(is_empty_line ? "\n" : ", ").join(parts);
  1778. }
  1779. if (type == "nodes") {
  1780. Node *scene_root = get_tree()->get_edited_scene_root();
  1781. if (!scene_root) {
  1782. EditorNode::get_singleton()->show_warning(TTR("Can't drop nodes without an open scene."));
  1783. return;
  1784. }
  1785. if (!ClassDB::is_parent_class(script->get_instance_base_type(), "Node")) {
  1786. EditorToaster::get_singleton()->popup_str(vformat(TTR("Can't drop nodes because script '%s' does not inherit Node."), get_name()), EditorToaster::SEVERITY_WARNING);
  1787. return;
  1788. }
  1789. Node *sn = _find_script_node(scene_root, scene_root, script);
  1790. if (!sn) {
  1791. sn = scene_root;
  1792. }
  1793. Array nodes = d["nodes"];
  1794. if (drop_modifier_pressed) {
  1795. const bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
  1796. for (int i = 0; i < nodes.size(); i++) {
  1797. NodePath np = nodes[i];
  1798. Node *node = get_node(np);
  1799. if (!node) {
  1800. continue;
  1801. }
  1802. bool is_unique = false;
  1803. String path;
  1804. if (node->is_unique_name_in_owner()) {
  1805. path = node->get_name();
  1806. is_unique = true;
  1807. } else {
  1808. path = sn->get_path_to(node);
  1809. }
  1810. for (const String &segment : path.split("/")) {
  1811. if (!segment.is_valid_unicode_identifier()) {
  1812. path = _quote_drop_data(path);
  1813. break;
  1814. }
  1815. }
  1816. String variable_name = String(node->get_name()).to_snake_case().validate_unicode_identifier();
  1817. if (use_type) {
  1818. StringName class_name = node->get_class_name();
  1819. Ref<Script> node_script = node->get_script();
  1820. if (node_script.is_valid()) {
  1821. StringName global_node_script_name = node_script->get_global_name();
  1822. if (global_node_script_name != StringName()) {
  1823. class_name = global_node_script_name;
  1824. }
  1825. }
  1826. text_to_drop += vformat("@onready var %s: %s = %c%s\n", variable_name, class_name, is_unique ? '%' : '$', path);
  1827. } else {
  1828. text_to_drop += vformat("@onready var %s = %c%s\n", variable_name, is_unique ? '%' : '$', path);
  1829. }
  1830. }
  1831. } else {
  1832. for (int i = 0; i < nodes.size(); i++) {
  1833. if (i > 0) {
  1834. text_to_drop += ", ";
  1835. }
  1836. NodePath np = nodes[i];
  1837. Node *node = get_node(np);
  1838. if (!node) {
  1839. continue;
  1840. }
  1841. bool is_unique = false;
  1842. String path;
  1843. if (node->is_unique_name_in_owner()) {
  1844. path = node->get_name();
  1845. is_unique = true;
  1846. } else {
  1847. path = sn->get_path_to(node);
  1848. }
  1849. for (const String &segment : path.split("/")) {
  1850. if (!segment.is_valid_ascii_identifier()) {
  1851. path = _quote_drop_data(path);
  1852. break;
  1853. }
  1854. }
  1855. text_to_drop += (is_unique ? "%" : "$") + path;
  1856. }
  1857. }
  1858. }
  1859. if (type == "obj_property") {
  1860. bool add_literal = EDITOR_GET("text_editor/completion/add_node_path_literals");
  1861. text_to_drop = add_literal ? "^" : "";
  1862. // It is unclear whether properties may contain single or double quotes.
  1863. // Assume here that double-quotes may not exist. We are escaping single-quotes if necessary.
  1864. text_to_drop += _quote_drop_data(String(d["property"]));
  1865. }
  1866. if (text_to_drop.is_empty()) {
  1867. return;
  1868. }
  1869. // Remove drag caret before any actions so it is not included in undo.
  1870. te->remove_drag_caret();
  1871. te->begin_complex_operation();
  1872. if (selection_index >= 0) {
  1873. te->delete_selection(selection_index);
  1874. }
  1875. te->remove_secondary_carets();
  1876. te->deselect();
  1877. te->set_caret_line(drop_at_line);
  1878. te->set_caret_column(drop_at_column);
  1879. te->insert_text_at_caret(text_to_drop);
  1880. te->end_complex_operation();
  1881. te->grab_focus();
  1882. }
  1883. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  1884. Ref<InputEventMouseButton> mb = ev;
  1885. Ref<InputEventKey> k = ev;
  1886. Point2 local_pos;
  1887. bool create_menu = false;
  1888. CodeEdit *tx = code_editor->get_text_editor();
  1889. if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
  1890. local_pos = mb->get_global_position() - tx->get_global_position();
  1891. create_menu = true;
  1892. } else if (k.is_valid() && k->is_action("ui_menu", true)) {
  1893. tx->adjust_viewport_to_caret(0);
  1894. local_pos = tx->get_caret_draw_pos(0);
  1895. create_menu = true;
  1896. }
  1897. if (create_menu) {
  1898. tx->apply_ime();
  1899. Point2i pos = tx->get_line_column_at_pos(local_pos);
  1900. int mouse_line = pos.y;
  1901. int mouse_column = pos.x;
  1902. tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
  1903. int selection_clicked = -1;
  1904. if (tx->is_move_caret_on_right_click_enabled()) {
  1905. selection_clicked = tx->get_selection_at_line_column(mouse_line, mouse_column, true);
  1906. if (selection_clicked < 0) {
  1907. tx->deselect();
  1908. tx->remove_secondary_carets();
  1909. selection_clicked = 0;
  1910. tx->set_caret_line(mouse_line, false, false, -1);
  1911. tx->set_caret_column(mouse_column);
  1912. }
  1913. }
  1914. String word_at_pos = tx->get_word_at_pos(local_pos);
  1915. if (word_at_pos.is_empty()) {
  1916. word_at_pos = tx->get_word_under_caret(selection_clicked);
  1917. }
  1918. if (word_at_pos.is_empty()) {
  1919. word_at_pos = tx->get_selected_text(selection_clicked);
  1920. }
  1921. bool has_color = (word_at_pos == "Color");
  1922. bool foldable = tx->can_fold_line(mouse_line) || tx->is_line_folded(mouse_line);
  1923. bool open_docs = false;
  1924. bool goto_definition = false;
  1925. if (ScriptServer::is_global_class(word_at_pos) || word_at_pos.is_resource_file()) {
  1926. open_docs = true;
  1927. } else {
  1928. Node *base = get_tree()->get_edited_scene_root();
  1929. if (base) {
  1930. base = _find_node_for_script(base, base, script);
  1931. }
  1932. ScriptLanguage::LookupResult result;
  1933. if (script->get_language()->lookup_code(tx->get_text_for_symbol_lookup(), word_at_pos, script->get_path(), base, result) == OK) {
  1934. open_docs = true;
  1935. }
  1936. }
  1937. if (has_color) {
  1938. String line = tx->get_line(mouse_line);
  1939. color_position.x = mouse_line;
  1940. color_position.y = mouse_column;
  1941. int begin = -1;
  1942. int end = -1;
  1943. enum EXPRESSION_PATTERNS {
  1944. NOT_PARSED,
  1945. RGBA_PARAMETER, // Color(float,float,float) or Color(float,float,float,float)
  1946. COLOR_NAME, // Color.COLOR_NAME
  1947. } expression_pattern = NOT_PARSED;
  1948. for (int i = mouse_column; i < line.length(); i++) {
  1949. if (line[i] == '(') {
  1950. if (expression_pattern == NOT_PARSED) {
  1951. begin = i;
  1952. expression_pattern = RGBA_PARAMETER;
  1953. } else {
  1954. // Method call or '(' appearing twice.
  1955. expression_pattern = NOT_PARSED;
  1956. break;
  1957. }
  1958. } else if (expression_pattern == RGBA_PARAMETER && line[i] == ')' && end < 0) {
  1959. end = i + 1;
  1960. break;
  1961. } else if (expression_pattern == NOT_PARSED && line[i] == '.') {
  1962. begin = i;
  1963. expression_pattern = COLOR_NAME;
  1964. } else if (expression_pattern == COLOR_NAME && end < 0 && (line[i] == ' ' || line[i] == '\t')) {
  1965. // Including '.' and spaces.
  1966. continue;
  1967. } else if (expression_pattern == COLOR_NAME && !(line[i] == '_' || ('A' <= line[i] && line[i] <= 'Z'))) {
  1968. end = i;
  1969. break;
  1970. }
  1971. }
  1972. switch (expression_pattern) {
  1973. case RGBA_PARAMETER: {
  1974. color_args = line.substr(begin, end - begin);
  1975. String stripped = color_args.remove_chars(" \t()");
  1976. PackedFloat64Array color = stripped.split_floats(",");
  1977. if (color.size() > 2) {
  1978. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1979. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1980. }
  1981. } break;
  1982. case COLOR_NAME: {
  1983. if (end < 0) {
  1984. end = line.length();
  1985. }
  1986. color_args = line.substr(begin, end - begin);
  1987. const String color_name = color_args.remove_chars(" \t.");
  1988. const int color_index = Color::find_named_color(color_name);
  1989. if (0 <= color_index) {
  1990. const Color color_constant = Color::get_named_color(color_index);
  1991. color_picker->set_pick_color(color_constant);
  1992. } else {
  1993. has_color = false;
  1994. }
  1995. } break;
  1996. default:
  1997. has_color = false;
  1998. break;
  1999. }
  2000. if (has_color) {
  2001. color_panel->set_position(get_screen_position() + local_pos);
  2002. }
  2003. }
  2004. _make_context_menu(tx->has_selection(), has_color, foldable, open_docs, goto_definition, local_pos);
  2005. }
  2006. }
  2007. void ScriptTextEditor::_color_changed(const Color &p_color) {
  2008. String new_args;
  2009. const int decimals = 3;
  2010. if (p_color.a == 1.0f) {
  2011. new_args = String("(" + String::num(p_color.r, decimals) + ", " + String::num(p_color.g, decimals) + ", " + String::num(p_color.b, decimals) + ")");
  2012. } else {
  2013. 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) + ")");
  2014. }
  2015. String line = code_editor->get_text_editor()->get_line(color_position.x);
  2016. String line_with_replaced_args = line.replace(color_args, new_args);
  2017. color_args = new_args;
  2018. code_editor->get_text_editor()->begin_complex_operation();
  2019. code_editor->get_text_editor()->set_line(color_position.x, line_with_replaced_args);
  2020. code_editor->get_text_editor()->end_complex_operation();
  2021. }
  2022. void ScriptTextEditor::_prepare_edit_menu() {
  2023. const CodeEdit *tx = code_editor->get_text_editor();
  2024. PopupMenu *popup = edit_menu->get_popup();
  2025. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  2026. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  2027. }
  2028. 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) {
  2029. context_menu->clear();
  2030. if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
  2031. context_menu->add_item(TTR("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
  2032. context_menu->add_separator();
  2033. }
  2034. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  2035. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  2036. context_menu->add_separator();
  2037. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  2038. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  2039. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  2040. context_menu->add_separator();
  2041. context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  2042. context_menu->add_separator();
  2043. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  2044. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  2045. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  2046. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  2047. if (p_selection) {
  2048. context_menu->add_separator();
  2049. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  2050. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  2051. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  2052. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
  2053. }
  2054. if (p_foldable) {
  2055. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  2056. }
  2057. if (p_color || p_open_docs || p_goto_definition) {
  2058. context_menu->add_separator();
  2059. if (p_open_docs) {
  2060. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL);
  2061. }
  2062. if (p_color) {
  2063. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  2064. }
  2065. }
  2066. const PackedStringArray paths = { code_editor->get_text_editor()->get_path() };
  2067. EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(context_menu, EditorContextMenuPlugin::CONTEXT_SLOT_SCRIPT_EDITOR_CODE, paths);
  2068. const CodeEdit *tx = code_editor->get_text_editor();
  2069. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  2070. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  2071. context_menu->set_position(get_screen_position() + p_pos);
  2072. context_menu->reset_size();
  2073. context_menu->popup();
  2074. }
  2075. void ScriptTextEditor::_enable_code_editor() {
  2076. ERR_FAIL_COND(code_editor->get_parent());
  2077. VSplitContainer *editor_box = memnew(VSplitContainer);
  2078. add_child(editor_box);
  2079. editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2080. editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
  2081. editor_box->add_child(code_editor);
  2082. code_editor->connect("show_errors_panel", callable_mp(this, &ScriptTextEditor::_show_errors_panel));
  2083. code_editor->connect("show_warnings_panel", callable_mp(this, &ScriptTextEditor::_show_warnings_panel));
  2084. code_editor->connect("validate_script", callable_mp(this, &ScriptTextEditor::_validate_script));
  2085. code_editor->connect("load_theme_settings", callable_mp(this, &ScriptTextEditor::_load_theme_settings));
  2086. code_editor->get_text_editor()->connect("symbol_lookup", callable_mp(this, &ScriptTextEditor::_lookup_symbol));
  2087. code_editor->get_text_editor()->connect("symbol_hovered", callable_mp(this, &ScriptTextEditor::_show_symbol_tooltip));
  2088. code_editor->get_text_editor()->connect("symbol_validate", callable_mp(this, &ScriptTextEditor::_validate_symbol));
  2089. code_editor->get_text_editor()->connect("gutter_added", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  2090. code_editor->get_text_editor()->connect("gutter_removed", callable_mp(this, &ScriptTextEditor::_update_gutter_indexes));
  2091. code_editor->get_text_editor()->connect("gutter_clicked", callable_mp(this, &ScriptTextEditor::_gutter_clicked));
  2092. code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &ScriptTextEditor::_text_edit_gui_input));
  2093. code_editor->show_toggle_scripts_button();
  2094. _update_gutter_indexes();
  2095. editor_box->add_child(warnings_panel);
  2096. warnings_panel->add_theme_font_override(
  2097. "normal_font", EditorNode::get_singleton()->get_editor_theme()->get_font(SNAME("main"), EditorStringName(EditorFonts)));
  2098. warnings_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. warnings_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_warning_clicked));
  2101. editor_box->add_child(errors_panel);
  2102. errors_panel->add_theme_font_override(
  2103. "normal_font", EditorNode::get_singleton()->get_editor_theme()->get_font(SNAME("main"), EditorStringName(EditorFonts)));
  2104. errors_panel->add_theme_font_size_override(
  2105. "normal_font_size", EditorNode::get_singleton()->get_editor_theme()->get_font_size(SNAME("main_size"), EditorStringName(EditorFonts)));
  2106. errors_panel->connect("meta_clicked", callable_mp(this, &ScriptTextEditor::_error_clicked));
  2107. add_child(context_menu);
  2108. context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2109. add_child(color_panel);
  2110. color_picker = memnew(ColorPicker);
  2111. color_picker->set_deferred_mode(true);
  2112. color_picker->connect("color_changed", callable_mp(this, &ScriptTextEditor::_color_changed));
  2113. color_panel->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(color_picker));
  2114. color_panel->add_child(color_picker);
  2115. quick_open = memnew(ScriptEditorQuickOpen);
  2116. quick_open->connect("goto_line", callable_mp(this, &ScriptTextEditor::_goto_line));
  2117. add_child(quick_open);
  2118. goto_line_popup = memnew(GotoLinePopup);
  2119. add_child(goto_line_popup);
  2120. add_child(connection_info_dialog);
  2121. edit_hb->add_child(edit_menu);
  2122. edit_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_prepare_edit_menu));
  2123. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
  2124. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
  2125. edit_menu->get_popup()->add_separator();
  2126. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
  2127. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
  2128. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
  2129. edit_menu->get_popup()->add_separator();
  2130. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
  2131. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  2132. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
  2133. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  2134. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
  2135. edit_menu->get_popup()->add_separator();
  2136. {
  2137. PopupMenu *sub_menu = memnew(PopupMenu);
  2138. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  2139. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  2140. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
  2141. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
  2142. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  2143. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  2144. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2145. edit_menu->get_popup()->add_submenu_node_item(TTR("Line"), sub_menu);
  2146. }
  2147. {
  2148. PopupMenu *sub_menu = memnew(PopupMenu);
  2149. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  2150. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  2151. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  2152. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/create_code_region"), EDIT_CREATE_CODE_REGION);
  2153. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2154. edit_menu->get_popup()->add_submenu_node_item(TTR("Folding"), sub_menu);
  2155. }
  2156. edit_menu->get_popup()->add_separator();
  2157. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
  2158. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  2159. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
  2160. {
  2161. PopupMenu *sub_menu = memnew(PopupMenu);
  2162. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  2163. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  2164. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  2165. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2166. edit_menu->get_popup()->add_submenu_node_item(TTR("Indentation"), sub_menu);
  2167. }
  2168. edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2169. edit_menu->get_popup()->add_separator();
  2170. {
  2171. PopupMenu *sub_menu = memnew(PopupMenu);
  2172. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  2173. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  2174. sub_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
  2175. sub_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2176. edit_menu->get_popup()->add_submenu_node_item(TTR("Convert Case"), sub_menu);
  2177. }
  2178. edit_menu->get_popup()->add_submenu_node_item(TTR("Syntax Highlighter"), highlighter_menu);
  2179. highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_change_syntax_highlighter));
  2180. edit_hb->add_child(search_menu);
  2181. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  2182. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  2183. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  2184. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  2185. search_menu->get_popup()->add_separator();
  2186. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  2187. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  2188. search_menu->get_popup()->add_separator();
  2189. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  2190. search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2191. _load_theme_settings();
  2192. edit_hb->add_child(goto_menu);
  2193. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  2194. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  2195. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_symbol"), LOOKUP_SYMBOL);
  2196. goto_menu->get_popup()->add_separator();
  2197. goto_menu->get_popup()->add_submenu_node_item(TTR("Bookmarks"), bookmarks_menu);
  2198. _update_bookmark_list();
  2199. bookmarks_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_bookmark_list));
  2200. bookmarks_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_bookmark_item_pressed));
  2201. goto_menu->get_popup()->add_submenu_node_item(TTR("Breakpoints"), breakpoints_menu);
  2202. _update_breakpoint_list();
  2203. breakpoints_menu->connect("about_to_popup", callable_mp(this, &ScriptTextEditor::_update_breakpoint_list));
  2204. breakpoints_menu->connect("index_pressed", callable_mp(this, &ScriptTextEditor::_breakpoint_item_pressed));
  2205. goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &ScriptTextEditor::_edit_option));
  2206. }
  2207. ScriptTextEditor::ScriptTextEditor() {
  2208. code_editor = memnew(CodeTextEditor);
  2209. code_editor->set_toggle_list_control(ScriptEditor::get_singleton()->get_left_list_split());
  2210. code_editor->add_theme_constant_override("separation", 2);
  2211. code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  2212. code_editor->set_code_complete_func(_code_complete_scripts, this);
  2213. code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  2214. code_editor->get_text_editor()->set_draw_breakpoints_gutter(true);
  2215. code_editor->get_text_editor()->set_draw_executing_lines_gutter(true);
  2216. code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &ScriptTextEditor::_breakpoint_toggled));
  2217. code_editor->get_text_editor()->connect("caret_changed", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
  2218. code_editor->connect("navigation_preview_ended", callable_mp(this, &ScriptTextEditor::_on_caret_moved));
  2219. connection_gutter = 1;
  2220. code_editor->get_text_editor()->add_gutter(connection_gutter);
  2221. code_editor->get_text_editor()->set_gutter_name(connection_gutter, "connection_gutter");
  2222. code_editor->get_text_editor()->set_gutter_draw(connection_gutter, false);
  2223. code_editor->get_text_editor()->set_gutter_overwritable(connection_gutter, true);
  2224. code_editor->get_text_editor()->set_gutter_type(connection_gutter, TextEdit::GUTTER_TYPE_ICON);
  2225. warnings_panel = memnew(RichTextLabel);
  2226. warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  2227. warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2228. warnings_panel->set_meta_underline(true);
  2229. warnings_panel->set_selection_enabled(true);
  2230. warnings_panel->set_context_menu_enabled(true);
  2231. warnings_panel->set_focus_mode(FOCUS_CLICK);
  2232. warnings_panel->hide();
  2233. errors_panel = memnew(RichTextLabel);
  2234. errors_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  2235. errors_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  2236. errors_panel->set_meta_underline(true);
  2237. errors_panel->set_selection_enabled(true);
  2238. errors_panel->set_context_menu_enabled(true);
  2239. errors_panel->set_focus_mode(FOCUS_CLICK);
  2240. errors_panel->hide();
  2241. update_settings();
  2242. code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
  2243. code_editor->get_text_editor()->set_symbol_tooltip_on_hover_enabled(true);
  2244. code_editor->get_text_editor()->set_context_menu_enabled(false);
  2245. context_menu = memnew(PopupMenu);
  2246. color_panel = memnew(PopupPanel);
  2247. edit_hb = memnew(HBoxContainer);
  2248. edit_menu = memnew(MenuButton);
  2249. edit_menu->set_text(TTR("Edit"));
  2250. edit_menu->set_switch_on_hover(true);
  2251. edit_menu->set_shortcut_context(this);
  2252. highlighter_menu = memnew(PopupMenu);
  2253. Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
  2254. plain_highlighter.instantiate();
  2255. add_syntax_highlighter(plain_highlighter);
  2256. Ref<EditorStandardSyntaxHighlighter> highlighter;
  2257. highlighter.instantiate();
  2258. add_syntax_highlighter(highlighter);
  2259. set_syntax_highlighter(highlighter);
  2260. search_menu = memnew(MenuButton);
  2261. search_menu->set_text(TTR("Search"));
  2262. search_menu->set_switch_on_hover(true);
  2263. search_menu->set_shortcut_context(this);
  2264. goto_menu = memnew(MenuButton);
  2265. goto_menu->set_text(TTR("Go To"));
  2266. goto_menu->set_switch_on_hover(true);
  2267. goto_menu->set_shortcut_context(this);
  2268. bookmarks_menu = memnew(PopupMenu);
  2269. breakpoints_menu = memnew(PopupMenu);
  2270. connection_info_dialog = memnew(ConnectionInfoDialog);
  2271. SET_DRAG_FORWARDING_GCD(code_editor->get_text_editor(), ScriptTextEditor);
  2272. }
  2273. ScriptTextEditor::~ScriptTextEditor() {
  2274. highlighters.clear();
  2275. if (!editor_enabled) {
  2276. memdelete(code_editor);
  2277. memdelete(warnings_panel);
  2278. memdelete(errors_panel);
  2279. memdelete(context_menu);
  2280. memdelete(color_panel);
  2281. memdelete(edit_hb);
  2282. memdelete(edit_menu);
  2283. memdelete(highlighter_menu);
  2284. memdelete(search_menu);
  2285. memdelete(goto_menu);
  2286. memdelete(bookmarks_menu);
  2287. memdelete(breakpoints_menu);
  2288. memdelete(connection_info_dialog);
  2289. }
  2290. }
  2291. static ScriptEditorBase *create_editor(const Ref<Resource> &p_resource) {
  2292. if (Object::cast_to<Script>(*p_resource)) {
  2293. return memnew(ScriptTextEditor);
  2294. }
  2295. return nullptr;
  2296. }
  2297. void ScriptTextEditor::register_editor() {
  2298. ED_SHORTCUT("script_text_editor/move_up", TTRC("Move Up"), KeyModifierMask::ALT | Key::UP);
  2299. ED_SHORTCUT("script_text_editor/move_down", TTRC("Move Down"), KeyModifierMask::ALT | Key::DOWN);
  2300. ED_SHORTCUT("script_text_editor/delete_line", TTRC("Delete Line"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::K);
  2301. // Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
  2302. // The next/previous in history shortcut in this case makes a lot more sense.
  2303. ED_SHORTCUT("script_text_editor/indent", TTRC("Indent"), Key::NONE);
  2304. ED_SHORTCUT("script_text_editor/unindent", TTRC("Unindent"), KeyModifierMask::SHIFT | Key::TAB);
  2305. 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) });
  2306. ED_SHORTCUT("script_text_editor/toggle_fold_line", TTRC("Fold/Unfold Line"), KeyModifierMask::ALT | Key::F);
  2307. ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_fold_line", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::F);
  2308. ED_SHORTCUT("script_text_editor/fold_all_lines", TTRC("Fold All Lines"), Key::NONE);
  2309. ED_SHORTCUT("script_text_editor/create_code_region", TTRC("Create Code Region"), KeyModifierMask::ALT | Key::R);
  2310. ED_SHORTCUT("script_text_editor/unfold_all_lines", TTRC("Unfold All Lines"), Key::NONE);
  2311. ED_SHORTCUT("script_text_editor/duplicate_selection", TTRC("Duplicate Selection"), KeyModifierMask::SHIFT | KeyModifierMask::CTRL | Key::D);
  2312. ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_selection", "macos", KeyModifierMask::SHIFT | KeyModifierMask::META | Key::C);
  2313. ED_SHORTCUT("script_text_editor/duplicate_lines", TTRC("Duplicate Lines"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::DOWN);
  2314. ED_SHORTCUT_OVERRIDE("script_text_editor/duplicate_lines", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::DOWN);
  2315. ED_SHORTCUT("script_text_editor/evaluate_selection", TTRC("Evaluate Selection"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::E);
  2316. ED_SHORTCUT("script_text_editor/toggle_word_wrap", TTRC("Toggle Word Wrap"), KeyModifierMask::ALT | Key::Z);
  2317. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTRC("Trim Trailing Whitespace"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::T);
  2318. ED_SHORTCUT("script_text_editor/trim_final_newlines", TTRC("Trim Final Newlines"), Key::NONE);
  2319. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTRC("Convert Indent to Spaces"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::Y);
  2320. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTRC("Convert Indent to Tabs"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::I);
  2321. ED_SHORTCUT("script_text_editor/auto_indent", TTRC("Auto Indent"), KeyModifierMask::CMD_OR_CTRL | Key::I);
  2322. ED_SHORTCUT_AND_COMMAND("script_text_editor/find", TTRC("Find..."), KeyModifierMask::CMD_OR_CTRL | Key::F);
  2323. ED_SHORTCUT("script_text_editor/find_next", TTRC("Find Next"), Key::F3);
  2324. ED_SHORTCUT_OVERRIDE("script_text_editor/find_next", "macos", KeyModifierMask::META | Key::G);
  2325. ED_SHORTCUT("script_text_editor/find_previous", TTRC("Find Previous"), KeyModifierMask::SHIFT | Key::F3);
  2326. ED_SHORTCUT_OVERRIDE("script_text_editor/find_previous", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::G);
  2327. ED_SHORTCUT_AND_COMMAND("script_text_editor/replace", TTRC("Replace..."), KeyModifierMask::CTRL | Key::R);
  2328. ED_SHORTCUT_OVERRIDE("script_text_editor/replace", "macos", KeyModifierMask::ALT | KeyModifierMask::META | Key::F);
  2329. ED_SHORTCUT("script_text_editor/find_in_files", TTRC("Find in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F);
  2330. ED_SHORTCUT("script_text_editor/replace_in_files", TTRC("Replace in Files..."), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::R);
  2331. ED_SHORTCUT("script_text_editor/contextual_help", TTRC("Contextual Help"), KeyModifierMask::ALT | Key::F1);
  2332. ED_SHORTCUT_OVERRIDE("script_text_editor/contextual_help", "macos", KeyModifierMask::ALT | KeyModifierMask::SHIFT | Key::SPACE);
  2333. ED_SHORTCUT("script_text_editor/toggle_bookmark", TTRC("Toggle Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::ALT | Key::B);
  2334. ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTRC("Go to Next Bookmark"), KeyModifierMask::CMD_OR_CTRL | Key::B);
  2335. ED_SHORTCUT_OVERRIDE("script_text_editor/goto_next_bookmark", "macos", KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::B);
  2336. ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTRC("Go to Previous Bookmark"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::B);
  2337. ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTRC("Remove All Bookmarks"), Key::NONE);
  2338. ED_SHORTCUT("script_text_editor/goto_function", TTRC("Go to Function..."), KeyModifierMask::ALT | KeyModifierMask::CTRL | Key::F);
  2339. ED_SHORTCUT_OVERRIDE("script_text_editor/goto_function", "macos", KeyModifierMask::CTRL | KeyModifierMask::META | Key::J);
  2340. ED_SHORTCUT("script_text_editor/goto_line", TTRC("Go to Line..."), KeyModifierMask::CMD_OR_CTRL | Key::L);
  2341. ED_SHORTCUT("script_text_editor/goto_symbol", TTRC("Lookup Symbol"));
  2342. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTRC("Toggle Breakpoint"), Key::F9);
  2343. ED_SHORTCUT_OVERRIDE("script_text_editor/toggle_breakpoint", "macos", KeyModifierMask::META | KeyModifierMask::SHIFT | Key::B);
  2344. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTRC("Remove All Breakpoints"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::F9);
  2345. // Using Control for these shortcuts even on macOS because Command+Comma is taken for opening Editor Settings.
  2346. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTRC("Go to Next Breakpoint"), KeyModifierMask::CTRL | Key::PERIOD);
  2347. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTRC("Go to Previous Breakpoint"), KeyModifierMask::CTRL | Key::COMMA);
  2348. ScriptEditor::register_create_script_editor_function(create_editor);
  2349. }
  2350. void ScriptTextEditor::validate() {
  2351. code_editor->validate_script();
  2352. }