script_text_editor.cpp 100 KB

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