script_text_editor.cpp 112 KB

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