script_text_editor.cpp 98 KB

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