script_text_editor.cpp 81 KB

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