script_text_editor.cpp 99 KB

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