script_text_editor.cpp 86 KB

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