script_text_editor.cpp 110 KB

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