script_text_editor.cpp 91 KB

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