script_text_editor.cpp 100 KB

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