script_text_editor.cpp 79 KB

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