script_text_editor.cpp 110 KB

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