script_text_editor.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114
  1. /*************************************************************************/
  2. /* script_text_editor.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "script_text_editor.h"
  31. #include "core/math/expression.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_scale.h"
  36. #include "editor/editor_settings.h"
  37. #include "editor/script_editor_debugger.h"
  38. void ConnectionInfoDialog::ok_pressed() {
  39. }
  40. void ConnectionInfoDialog::popup_connections(String p_method, Vector<Node *> p_nodes) {
  41. method->set_text(p_method);
  42. tree->clear();
  43. TreeItem *root = tree->create_item();
  44. for (int i = 0; i < p_nodes.size(); i++) {
  45. List<Connection> all_connections;
  46. p_nodes[i]->get_signals_connected_to_this(&all_connections);
  47. for (List<Connection>::Element *E = all_connections.front(); E; E = E->next()) {
  48. Connection connection = E->get();
  49. if (connection.method != p_method) {
  50. continue;
  51. }
  52. TreeItem *node_item = tree->create_item(root);
  53. node_item->set_text(0, Object::cast_to<Node>(connection.source)->get_name());
  54. node_item->set_icon(0, EditorNode::get_singleton()->get_object_icon(connection.source, "Node"));
  55. node_item->set_selectable(0, false);
  56. node_item->set_editable(0, false);
  57. node_item->set_text(1, connection.signal);
  58. node_item->set_icon(1, get_parent_control()->get_icon("Slot", "EditorIcons"));
  59. node_item->set_selectable(1, false);
  60. node_item->set_editable(1, false);
  61. node_item->set_text(2, Object::cast_to<Node>(connection.target)->get_name());
  62. node_item->set_icon(2, EditorNode::get_singleton()->get_object_icon(connection.target, "Node"));
  63. node_item->set_selectable(2, false);
  64. node_item->set_editable(2, false);
  65. }
  66. }
  67. popup_centered(Size2(600, 300) * EDSCALE);
  68. }
  69. ConnectionInfoDialog::ConnectionInfoDialog() {
  70. set_title(TTR("Connections to method:"));
  71. VBoxContainer *vbc = memnew(VBoxContainer);
  72. vbc->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_BEGIN, 8 * EDSCALE);
  73. vbc->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 8 * EDSCALE);
  74. vbc->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -8 * EDSCALE);
  75. vbc->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -8 * EDSCALE);
  76. add_child(vbc);
  77. method = memnew(Label);
  78. method->set_align(Label::ALIGN_CENTER);
  79. vbc->add_child(method);
  80. tree = memnew(Tree);
  81. tree->set_columns(3);
  82. tree->set_hide_root(true);
  83. tree->set_column_titles_visible(true);
  84. tree->set_column_title(0, TTR("Source"));
  85. tree->set_column_title(1, TTR("Signal"));
  86. tree->set_column_title(2, TTR("Target"));
  87. vbc->add_child(tree);
  88. tree->set_v_size_flags(SIZE_EXPAND_FILL);
  89. tree->set_allow_rmb_select(true);
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////
  92. Vector<String> ScriptTextEditor::get_functions() {
  93. String errortxt;
  94. int line = -1, col;
  95. TextEdit *te = code_editor->get_text_edit();
  96. String text = te->get_text();
  97. List<String> fnc;
  98. if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  99. //if valid rewrite functions to latest
  100. functions.clear();
  101. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  102. functions.push_back(E->get());
  103. }
  104. }
  105. return functions;
  106. }
  107. void ScriptTextEditor::apply_code() {
  108. if (script.is_null()) {
  109. return;
  110. }
  111. script->set_source_code(code_editor->get_text_edit()->get_text());
  112. script->update_exports();
  113. _update_member_keywords();
  114. }
  115. RES ScriptTextEditor::get_edited_resource() const {
  116. return script;
  117. }
  118. void ScriptTextEditor::set_edited_resource(const RES &p_res) {
  119. ERR_FAIL_COND(script.is_valid());
  120. ERR_FAIL_COND(p_res.is_null());
  121. script = p_res;
  122. code_editor->get_text_edit()->set_text(script->get_source_code());
  123. code_editor->get_text_edit()->clear_undo_history();
  124. code_editor->get_text_edit()->tag_saved_version();
  125. emit_signal("name_changed");
  126. code_editor->update_line_and_column();
  127. }
  128. void ScriptTextEditor::enable_editor() {
  129. if (editor_enabled) {
  130. return;
  131. }
  132. editor_enabled = true;
  133. _enable_code_editor();
  134. _set_theme_for_script();
  135. _validate_script();
  136. }
  137. void ScriptTextEditor::_update_member_keywords() {
  138. member_keywords.clear();
  139. code_editor->get_text_edit()->clear_member_keywords();
  140. Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  141. StringName instance_base = script->get_instance_base_type();
  142. if (instance_base == StringName()) {
  143. return;
  144. }
  145. List<PropertyInfo> plist;
  146. ClassDB::get_property_list(instance_base, &plist);
  147. for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
  148. String name = E->get().name;
  149. if (E->get().usage & PROPERTY_USAGE_CATEGORY || E->get().usage & PROPERTY_USAGE_GROUP) {
  150. continue;
  151. }
  152. if (name.find("/") != -1) {
  153. continue;
  154. }
  155. code_editor->get_text_edit()->add_member_keyword(name, member_variable_color);
  156. }
  157. List<String> clist;
  158. ClassDB::get_integer_constant_list(instance_base, &clist);
  159. for (List<String>::Element *E = clist.front(); E; E = E->next()) {
  160. code_editor->get_text_edit()->add_member_keyword(E->get(), member_variable_color);
  161. }
  162. }
  163. void ScriptTextEditor::_load_theme_settings() {
  164. TextEdit *text_edit = code_editor->get_text_edit();
  165. text_edit->clear_colors();
  166. Color background_color = EDITOR_GET("text_editor/highlighting/background_color");
  167. Color completion_background_color = EDITOR_GET("text_editor/highlighting/completion_background_color");
  168. Color completion_selected_color = EDITOR_GET("text_editor/highlighting/completion_selected_color");
  169. Color completion_existing_color = EDITOR_GET("text_editor/highlighting/completion_existing_color");
  170. Color completion_scroll_color = EDITOR_GET("text_editor/highlighting/completion_scroll_color");
  171. Color completion_font_color = EDITOR_GET("text_editor/highlighting/completion_font_color");
  172. Color text_color = EDITOR_GET("text_editor/highlighting/text_color");
  173. Color line_number_color = EDITOR_GET("text_editor/highlighting/line_number_color");
  174. Color safe_line_number_color = EDITOR_GET("text_editor/highlighting/safe_line_number_color");
  175. Color caret_color = EDITOR_GET("text_editor/highlighting/caret_color");
  176. Color caret_background_color = EDITOR_GET("text_editor/highlighting/caret_background_color");
  177. Color text_selected_color = EDITOR_GET("text_editor/highlighting/text_selected_color");
  178. Color selection_color = EDITOR_GET("text_editor/highlighting/selection_color");
  179. Color brace_mismatch_color = EDITOR_GET("text_editor/highlighting/brace_mismatch_color");
  180. Color current_line_color = EDITOR_GET("text_editor/highlighting/current_line_color");
  181. Color line_length_guideline_color = EDITOR_GET("text_editor/highlighting/line_length_guideline_color");
  182. Color word_highlighted_color = EDITOR_GET("text_editor/highlighting/word_highlighted_color");
  183. Color number_color = EDITOR_GET("text_editor/highlighting/number_color");
  184. Color function_color = EDITOR_GET("text_editor/highlighting/function_color");
  185. Color member_variable_color = EDITOR_GET("text_editor/highlighting/member_variable_color");
  186. Color mark_color = EDITOR_GET("text_editor/highlighting/mark_color");
  187. Color bookmark_color = EDITOR_GET("text_editor/highlighting/bookmark_color");
  188. Color breakpoint_color = EDITOR_GET("text_editor/highlighting/breakpoint_color");
  189. Color executing_line_color = EDITOR_GET("text_editor/highlighting/executing_line_color");
  190. Color code_folding_color = EDITOR_GET("text_editor/highlighting/code_folding_color");
  191. Color search_result_color = EDITOR_GET("text_editor/highlighting/search_result_color");
  192. Color search_result_border_color = EDITOR_GET("text_editor/highlighting/search_result_border_color");
  193. Color symbol_color = EDITOR_GET("text_editor/highlighting/symbol_color");
  194. Color keyword_color = EDITOR_GET("text_editor/highlighting/keyword_color");
  195. Color control_flow_keyword_color = EDITOR_GET("text_editor/highlighting/control_flow_keyword_color");
  196. Color basetype_color = EDITOR_GET("text_editor/highlighting/base_type_color");
  197. Color type_color = EDITOR_GET("text_editor/highlighting/engine_type_color");
  198. Color usertype_color = EDITOR_GET("text_editor/highlighting/user_type_color");
  199. Color comment_color = EDITOR_GET("text_editor/highlighting/comment_color");
  200. Color string_color = EDITOR_GET("text_editor/highlighting/string_color");
  201. text_edit->add_color_override("background_color", background_color);
  202. text_edit->add_color_override("completion_background_color", completion_background_color);
  203. text_edit->add_color_override("completion_selected_color", completion_selected_color);
  204. text_edit->add_color_override("completion_existing_color", completion_existing_color);
  205. text_edit->add_color_override("completion_scroll_color", completion_scroll_color);
  206. text_edit->add_color_override("completion_font_color", completion_font_color);
  207. text_edit->add_color_override("font_color", text_color);
  208. text_edit->add_color_override("line_number_color", line_number_color);
  209. text_edit->add_color_override("safe_line_number_color", safe_line_number_color);
  210. text_edit->add_color_override("caret_color", caret_color);
  211. text_edit->add_color_override("caret_background_color", caret_background_color);
  212. text_edit->add_color_override("font_color_selected", text_selected_color);
  213. text_edit->add_color_override("selection_color", selection_color);
  214. text_edit->add_color_override("brace_mismatch_color", brace_mismatch_color);
  215. text_edit->add_color_override("current_line_color", current_line_color);
  216. text_edit->add_color_override("line_length_guideline_color", line_length_guideline_color);
  217. text_edit->add_color_override("word_highlighted_color", word_highlighted_color);
  218. text_edit->add_color_override("number_color", number_color);
  219. text_edit->add_color_override("function_color", function_color);
  220. text_edit->add_color_override("member_variable_color", member_variable_color);
  221. text_edit->add_color_override("bookmark_color", bookmark_color);
  222. text_edit->add_color_override("breakpoint_color", breakpoint_color);
  223. text_edit->add_color_override("executing_line_color", executing_line_color);
  224. text_edit->add_color_override("mark_color", mark_color);
  225. text_edit->add_color_override("code_folding_color", code_folding_color);
  226. text_edit->add_color_override("search_result_color", search_result_color);
  227. text_edit->add_color_override("search_result_border_color", search_result_border_color);
  228. text_edit->add_color_override("symbol_color", symbol_color);
  229. text_edit->add_constant_override("line_spacing", EDITOR_GET("text_editor/theme/line_spacing"));
  230. colors_cache.symbol_color = symbol_color;
  231. colors_cache.keyword_color = keyword_color;
  232. colors_cache.control_flow_keyword_color = control_flow_keyword_color;
  233. colors_cache.basetype_color = basetype_color;
  234. colors_cache.type_color = type_color;
  235. colors_cache.usertype_color = usertype_color;
  236. colors_cache.comment_color = comment_color;
  237. colors_cache.string_color = string_color;
  238. theme_loaded = true;
  239. if (!script.is_null()) {
  240. _set_theme_for_script();
  241. }
  242. }
  243. void ScriptTextEditor::_set_theme_for_script() {
  244. if (!theme_loaded) {
  245. return;
  246. }
  247. TextEdit *text_edit = code_editor->get_text_edit();
  248. List<String> keywords;
  249. script->get_language()->get_reserved_words(&keywords);
  250. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  251. if (script->get_language()->is_control_flow_keyword(E->get())) {
  252. // Use a different color for control flow keywords to make them easier to distinguish.
  253. text_edit->add_keyword_color(E->get(), colors_cache.control_flow_keyword_color);
  254. } else {
  255. text_edit->add_keyword_color(E->get(), colors_cache.keyword_color);
  256. }
  257. }
  258. //colorize core types
  259. const Color basetype_color = colors_cache.basetype_color;
  260. text_edit->add_keyword_color("String", basetype_color);
  261. text_edit->add_keyword_color("Vector2", basetype_color);
  262. text_edit->add_keyword_color("Rect2", basetype_color);
  263. text_edit->add_keyword_color("Transform2D", basetype_color);
  264. text_edit->add_keyword_color("Vector3", basetype_color);
  265. text_edit->add_keyword_color("AABB", basetype_color);
  266. text_edit->add_keyword_color("Basis", basetype_color);
  267. text_edit->add_keyword_color("Plane", basetype_color);
  268. text_edit->add_keyword_color("Transform", basetype_color);
  269. text_edit->add_keyword_color("Quat", basetype_color);
  270. text_edit->add_keyword_color("Color", basetype_color);
  271. text_edit->add_keyword_color("Object", basetype_color);
  272. text_edit->add_keyword_color("NodePath", basetype_color);
  273. text_edit->add_keyword_color("RID", basetype_color);
  274. text_edit->add_keyword_color("Dictionary", basetype_color);
  275. text_edit->add_keyword_color("Array", basetype_color);
  276. text_edit->add_keyword_color("PoolByteArray", basetype_color);
  277. text_edit->add_keyword_color("PoolIntArray", basetype_color);
  278. text_edit->add_keyword_color("PoolRealArray", basetype_color);
  279. text_edit->add_keyword_color("PoolStringArray", basetype_color);
  280. text_edit->add_keyword_color("PoolVector2Array", basetype_color);
  281. text_edit->add_keyword_color("PoolVector3Array", basetype_color);
  282. text_edit->add_keyword_color("PoolColorArray", basetype_color);
  283. //colorize engine types
  284. List<StringName> types;
  285. ClassDB::get_class_list(&types);
  286. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  287. String n = E->get();
  288. if (n.begins_with("_")) {
  289. n = n.substr(1, n.length());
  290. }
  291. text_edit->add_keyword_color(n, colors_cache.type_color);
  292. }
  293. _update_member_keywords();
  294. //colorize user types
  295. List<StringName> global_classes;
  296. ScriptServer::get_global_class_list(&global_classes);
  297. for (List<StringName>::Element *E = global_classes.front(); E; E = E->next()) {
  298. text_edit->add_keyword_color(E->get(), colors_cache.usertype_color);
  299. }
  300. //colorize singleton autoloads (as types, just as engine singletons are)
  301. List<PropertyInfo> props;
  302. ProjectSettings::get_singleton()->get_property_list(&props);
  303. for (List<PropertyInfo>::Element *E = props.front(); E; E = E->next()) {
  304. String s = E->get().name;
  305. if (!s.begins_with("autoload/")) {
  306. continue;
  307. }
  308. String path = ProjectSettings::get_singleton()->get(s);
  309. if (path.begins_with("*")) {
  310. text_edit->add_keyword_color(s.get_slice("/", 1), colors_cache.usertype_color);
  311. }
  312. }
  313. //colorize comments
  314. List<String> comments;
  315. script->get_language()->get_comment_delimiters(&comments);
  316. for (List<String>::Element *E = comments.front(); E; E = E->next()) {
  317. String comment = E->get();
  318. String beg = comment.get_slice(" ", 0);
  319. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  320. text_edit->add_color_region(beg, end, colors_cache.comment_color, end == "");
  321. }
  322. //colorize strings
  323. List<String> strings;
  324. script->get_language()->get_string_delimiters(&strings);
  325. for (List<String>::Element *E = strings.front(); E; E = E->next()) {
  326. String string = E->get();
  327. String beg = string.get_slice(" ", 0);
  328. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  329. text_edit->add_color_region(beg, end, colors_cache.string_color, end == "");
  330. }
  331. }
  332. void ScriptTextEditor::_show_warnings_panel(bool p_show) {
  333. warnings_panel->set_visible(p_show);
  334. }
  335. void ScriptTextEditor::_warning_clicked(Variant p_line) {
  336. if (p_line.get_type() == Variant::INT) {
  337. goto_line_centered(p_line.operator int64_t());
  338. } else if (p_line.get_type() == Variant::DICTIONARY) {
  339. Dictionary meta = p_line.operator Dictionary();
  340. code_editor->get_text_edit()->insert_at("# warning-ignore:" + meta["code"].operator String(), meta["line"].operator int64_t() - 1);
  341. _validate_script();
  342. }
  343. }
  344. void ScriptTextEditor::reload_text() {
  345. ERR_FAIL_COND(script.is_null());
  346. TextEdit *te = code_editor->get_text_edit();
  347. int column = te->cursor_get_column();
  348. int row = te->cursor_get_line();
  349. int h = te->get_h_scroll();
  350. int v = te->get_v_scroll();
  351. te->set_text(script->get_source_code());
  352. te->cursor_set_line(row);
  353. te->cursor_set_column(column);
  354. te->set_h_scroll(h);
  355. te->set_v_scroll(v);
  356. te->tag_saved_version();
  357. code_editor->update_line_and_column();
  358. }
  359. void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  360. String code = code_editor->get_text_edit()->get_text();
  361. int pos = script->get_language()->find_function(p_function, code);
  362. if (pos == -1) {
  363. //does not exist
  364. code_editor->get_text_edit()->deselect();
  365. pos = code_editor->get_text_edit()->get_line_count() + 2;
  366. String func = script->get_language()->make_function("", p_function, p_args);
  367. //code=code+func;
  368. code_editor->get_text_edit()->cursor_set_line(pos + 1);
  369. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  370. code_editor->get_text_edit()->insert_text_at_cursor("\n\n" + func);
  371. }
  372. code_editor->get_text_edit()->cursor_set_line(pos);
  373. code_editor->get_text_edit()->cursor_set_column(1);
  374. }
  375. bool ScriptTextEditor::show_members_overview() {
  376. return true;
  377. }
  378. void ScriptTextEditor::update_settings() {
  379. code_editor->update_editor_settings();
  380. }
  381. bool ScriptTextEditor::is_unsaved() {
  382. return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
  383. }
  384. Variant ScriptTextEditor::get_edit_state() {
  385. return code_editor->get_edit_state();
  386. }
  387. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  388. code_editor->set_edit_state(p_state);
  389. Dictionary state = p_state;
  390. if (state.has("syntax_highlighter")) {
  391. int idx = highlighter_menu->get_item_idx_from_text(state["syntax_highlighter"]);
  392. if (idx >= 0) {
  393. _change_syntax_highlighter(idx);
  394. }
  395. }
  396. if (editor_enabled) {
  397. ensure_focus();
  398. }
  399. }
  400. void ScriptTextEditor::_convert_case(CodeTextEditor::CaseStyle p_case) {
  401. code_editor->convert_case(p_case);
  402. }
  403. void ScriptTextEditor::trim_trailing_whitespace() {
  404. code_editor->trim_trailing_whitespace();
  405. }
  406. void ScriptTextEditor::insert_final_newline() {
  407. code_editor->insert_final_newline();
  408. }
  409. void ScriptTextEditor::convert_indent_to_spaces() {
  410. code_editor->convert_indent_to_spaces();
  411. }
  412. void ScriptTextEditor::convert_indent_to_tabs() {
  413. code_editor->convert_indent_to_tabs();
  414. }
  415. void ScriptTextEditor::tag_saved_version() {
  416. code_editor->get_text_edit()->tag_saved_version();
  417. }
  418. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  419. code_editor->goto_line(p_line);
  420. }
  421. void ScriptTextEditor::goto_line_selection(int p_line, int p_begin, int p_end) {
  422. code_editor->goto_line_selection(p_line, p_begin, p_end);
  423. }
  424. void ScriptTextEditor::goto_line_centered(int p_line) {
  425. code_editor->goto_line_centered(p_line);
  426. }
  427. void ScriptTextEditor::set_executing_line(int p_line) {
  428. code_editor->set_executing_line(p_line);
  429. }
  430. void ScriptTextEditor::clear_executing_line() {
  431. code_editor->clear_executing_line();
  432. }
  433. void ScriptTextEditor::ensure_focus() {
  434. code_editor->get_text_edit()->grab_focus();
  435. }
  436. String ScriptTextEditor::get_name() {
  437. String name;
  438. name = script->get_path().get_file();
  439. if (name.empty()) {
  440. // This appears for newly created built-in scripts before saving the scene.
  441. name = TTR("[unsaved]");
  442. } else if (script->get_path().find("local://") == -1 || script->get_path().find("::") == -1) {
  443. const String &script_name = script->get_name();
  444. if (script_name != "") {
  445. // If the built-in script has a custom resource name defined,
  446. // display the built-in script name as follows: `ResourceName (scene_file.tscn)`
  447. name = vformat("%s (%s)", script_name, name.get_slice("::", 0));
  448. }
  449. }
  450. if (is_unsaved()) {
  451. name += "(*)";
  452. }
  453. return name;
  454. }
  455. Ref<Texture> ScriptTextEditor::get_icon() {
  456. if (get_parent_control() && get_parent_control()->has_icon(script->get_class(), "EditorIcons")) {
  457. return get_parent_control()->get_icon(script->get_class(), "EditorIcons");
  458. }
  459. return Ref<Texture>();
  460. }
  461. void ScriptTextEditor::_validate_script() {
  462. String errortxt;
  463. int line = -1, col;
  464. TextEdit *te = code_editor->get_text_edit();
  465. String text = te->get_text();
  466. List<String> fnc;
  467. Set<int> safe_lines;
  468. List<ScriptLanguage::Warning> warnings;
  469. if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc, &warnings, &safe_lines)) {
  470. String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
  471. code_editor->set_error(error_text);
  472. code_editor->set_error_pos(line - 1, col - 1);
  473. script_is_valid = false;
  474. } else {
  475. code_editor->set_error("");
  476. line = -1;
  477. if (!script->is_tool()) {
  478. script->set_source_code(text);
  479. script->update_exports();
  480. _update_member_keywords();
  481. }
  482. functions.clear();
  483. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  484. functions.push_back(E->get());
  485. }
  486. script_is_valid = true;
  487. }
  488. _update_connected_methods();
  489. int warning_nb = warnings.size();
  490. warnings_panel->clear();
  491. // Add missing connections.
  492. if (GLOBAL_GET("debug/gdscript/warnings/enable").booleanize()) {
  493. Node *base = get_tree()->get_edited_scene_root();
  494. if (base && missing_connections.size() > 0) {
  495. warnings_panel->push_table(1);
  496. for (List<Connection>::Element *E = missing_connections.front(); E; E = E->next()) {
  497. Connection connection = E->get();
  498. String base_path = base->get_name();
  499. String source_path = base == connection.source ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.source));
  500. String target_path = base == connection.target ? base_path : base_path + "/" + base->get_path_to(Object::cast_to<Node>(connection.target));
  501. warnings_panel->push_cell();
  502. warnings_panel->push_color(warnings_panel->get_color("warning_color", "Editor"));
  503. warnings_panel->add_text(vformat(TTR("Missing connected method '%s' for signal '%s' from node '%s' to node '%s'."), connection.method, connection.signal, source_path, target_path));
  504. warnings_panel->pop(); // Color.
  505. warnings_panel->pop(); // Cell.
  506. }
  507. warnings_panel->pop(); // Table.
  508. warning_nb += missing_connections.size();
  509. }
  510. }
  511. code_editor->set_warning_nb(warning_nb);
  512. // Add script warnings.
  513. warnings_panel->push_table(3);
  514. for (List<ScriptLanguage::Warning>::Element *E = warnings.front(); E; E = E->next()) {
  515. ScriptLanguage::Warning w = E->get();
  516. Dictionary ignore_meta;
  517. ignore_meta["line"] = w.line;
  518. ignore_meta["code"] = w.string_code.to_lower();
  519. warnings_panel->push_cell();
  520. warnings_panel->push_meta(ignore_meta);
  521. warnings_panel->push_color(
  522. warnings_panel->get_color("accent_color", "Editor").linear_interpolate(warnings_panel->get_color("mono_color", "Editor"), 0.5));
  523. warnings_panel->add_text(TTR("[Ignore]"));
  524. warnings_panel->pop(); // Color.
  525. warnings_panel->pop(); // Meta ignore.
  526. warnings_panel->pop(); // Cell.
  527. warnings_panel->push_cell();
  528. warnings_panel->push_meta(w.line - 1);
  529. warnings_panel->push_color(warnings_panel->get_color("warning_color", "Editor"));
  530. warnings_panel->add_text(TTR("Line") + " " + itos(w.line));
  531. warnings_panel->add_text(" (" + w.string_code + "):");
  532. warnings_panel->pop(); // Color.
  533. warnings_panel->pop(); // Meta goto.
  534. warnings_panel->pop(); // Cell.
  535. warnings_panel->push_cell();
  536. warnings_panel->add_text(w.message);
  537. warnings_panel->pop(); // Cell.
  538. }
  539. warnings_panel->pop(); // Table.
  540. line--;
  541. bool highlight_safe = EDITOR_DEF("text_editor/highlighting/highlight_type_safe_lines", true);
  542. bool last_is_safe = false;
  543. for (int i = 0; i < te->get_line_count(); i++) {
  544. te->set_line_as_marked(i, line == i);
  545. if (highlight_safe) {
  546. if (safe_lines.has(i + 1)) {
  547. te->set_line_as_safe(i, true);
  548. last_is_safe = true;
  549. } else if (last_is_safe && (te->is_line_comment(i) || te->get_line(i).strip_edges().empty())) {
  550. te->set_line_as_safe(i, true);
  551. } else {
  552. te->set_line_as_safe(i, false);
  553. last_is_safe = false;
  554. }
  555. } else {
  556. te->set_line_as_safe(i, false);
  557. }
  558. }
  559. emit_signal("name_changed");
  560. emit_signal("edited_script_changed");
  561. }
  562. void ScriptTextEditor::_update_bookmark_list() {
  563. bookmarks_menu->clear();
  564. bookmarks_menu->set_size(Size2(1, 1));
  565. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  566. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
  567. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
  568. bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
  569. Array bookmark_list = code_editor->get_text_edit()->get_bookmarks_array();
  570. if (bookmark_list.size() == 0) {
  571. return;
  572. }
  573. bookmarks_menu->add_separator();
  574. for (int i = 0; i < bookmark_list.size(); i++) {
  575. // Strip edges to remove spaces or tabs.
  576. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  577. String line = code_editor->get_text_edit()->get_line(bookmark_list[i]).replace("\t", " ").strip_edges();
  578. // Limit the size of the line if too big.
  579. if (line.length() > 50) {
  580. line = line.substr(0, 50);
  581. }
  582. bookmarks_menu->add_item(String::num((int)bookmark_list[i] + 1) + " - `" + line + "`");
  583. bookmarks_menu->set_item_metadata(bookmarks_menu->get_item_count() - 1, bookmark_list[i]);
  584. }
  585. }
  586. void ScriptTextEditor::_bookmark_item_pressed(int p_idx) {
  587. if (p_idx < 4) { // Any item before the separator.
  588. _edit_option(bookmarks_menu->get_item_id(p_idx));
  589. } else {
  590. code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
  591. }
  592. }
  593. static Vector<Node *> _find_all_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  594. Vector<Node *> nodes;
  595. if (p_current->get_owner() != p_base && p_base != p_current) {
  596. return nodes;
  597. }
  598. Ref<Script> c = p_current->get_script();
  599. if (c == p_script) {
  600. nodes.push_back(p_current);
  601. }
  602. for (int i = 0; i < p_current->get_child_count(); i++) {
  603. Vector<Node *> found = _find_all_node_for_script(p_base, p_current->get_child(i), p_script);
  604. nodes.append_array(found);
  605. }
  606. return nodes;
  607. }
  608. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  609. if (p_current->get_owner() != p_base && p_base != p_current) {
  610. return nullptr;
  611. }
  612. Ref<Script> c = p_current->get_script();
  613. if (c == p_script) {
  614. return p_current;
  615. }
  616. for (int i = 0; i < p_current->get_child_count(); i++) {
  617. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  618. if (found) {
  619. return found;
  620. }
  621. }
  622. return nullptr;
  623. }
  624. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script>> &r_scripts) {
  625. if (p_current->get_owner() != p_base && p_base != p_current) {
  626. return;
  627. }
  628. Ref<Script> c = p_current->get_script();
  629. if (c.is_valid()) {
  630. r_scripts.insert(c);
  631. }
  632. for (int i = 0; i < p_current->get_child_count(); i++) {
  633. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  634. }
  635. }
  636. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  637. if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) {
  638. return;
  639. }
  640. ERR_FAIL_COND(!get_tree());
  641. Set<Ref<Script>> scripts;
  642. Node *base = get_tree()->get_edited_scene_root();
  643. if (base) {
  644. _find_changed_scripts_for_external_editor(base, base, scripts);
  645. }
  646. for (Set<Ref<Script>>::Element *E = scripts.front(); E; E = E->next()) {
  647. Ref<Script> script = E->get();
  648. if (p_for_script.is_valid() && p_for_script != script) {
  649. continue;
  650. }
  651. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  652. continue; //internal script, who cares, though weird
  653. }
  654. uint64_t last_date = script->get_last_modified_time();
  655. uint64_t date = FileAccess::get_modified_time(script->get_path());
  656. if (last_date != date) {
  657. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  658. ERR_CONTINUE(!rel_script.is_valid());
  659. script->set_source_code(rel_script->get_source_code());
  660. script->set_last_modified_time(rel_script->get_last_modified_time());
  661. script->update_exports();
  662. _trigger_live_script_reload();
  663. }
  664. }
  665. }
  666. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptCodeCompletionOption> *r_options, bool &r_force) {
  667. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  668. ste->_code_complete_script(p_code, r_options, r_force);
  669. }
  670. void ScriptTextEditor::_code_complete_script(const String &p_code, List<ScriptCodeCompletionOption> *r_options, bool &r_force) {
  671. if (color_panel->is_visible_in_tree()) {
  672. return;
  673. }
  674. Node *base = get_tree()->get_edited_scene_root();
  675. if (base) {
  676. base = _find_node_for_script(base, base, script);
  677. }
  678. String hint;
  679. Error err = script->get_language()->complete_code(p_code, script->get_path(), base, r_options, r_force, hint);
  680. if (err == OK) {
  681. code_editor->get_text_edit()->set_code_hint(hint);
  682. }
  683. }
  684. void ScriptTextEditor::_update_breakpoint_list() {
  685. breakpoints_menu->clear();
  686. breakpoints_menu->set_size(Size2(1, 1));
  687. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  688. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  689. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  690. breakpoints_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  691. Array breakpoint_list = code_editor->get_text_edit()->get_breakpoints_array();
  692. if (breakpoint_list.size() == 0) {
  693. return;
  694. }
  695. breakpoints_menu->add_separator();
  696. for (int i = 0; i < breakpoint_list.size(); i++) {
  697. // Strip edges to remove spaces or tabs.
  698. // Also replace any tabs by spaces, since we can't print tabs in the menu.
  699. String line = code_editor->get_text_edit()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
  700. // Limit the size of the line if too big.
  701. if (line.length() > 50) {
  702. line = line.substr(0, 50);
  703. }
  704. breakpoints_menu->add_item(String::num((int)breakpoint_list[i] + 1) + " - `" + line + "`");
  705. breakpoints_menu->set_item_metadata(breakpoints_menu->get_item_count() - 1, breakpoint_list[i]);
  706. }
  707. }
  708. void ScriptTextEditor::_breakpoint_item_pressed(int p_idx) {
  709. if (p_idx < 4) { // Any item before the separator.
  710. _edit_option(breakpoints_menu->get_item_id(p_idx));
  711. } else {
  712. code_editor->goto_line(breakpoints_menu->get_item_metadata(p_idx));
  713. code_editor->get_text_edit()->call_deferred("center_viewport_to_cursor"); //Need to be deferred, because goto uses call_deferred().
  714. }
  715. }
  716. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  717. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), p_row + 1, code_editor->get_text_edit()->is_line_set_as_breakpoint(p_row));
  718. }
  719. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  720. Node *base = get_tree()->get_edited_scene_root();
  721. if (base) {
  722. base = _find_node_for_script(base, base, script);
  723. }
  724. ScriptLanguage::LookupResult result;
  725. if (ScriptServer::is_global_class(p_symbol)) {
  726. EditorNode::get_singleton()->load_resource(ScriptServer::get_global_class_path(p_symbol));
  727. } else if (p_symbol.is_resource_file()) {
  728. List<String> scene_extensions;
  729. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  730. if (scene_extensions.find(p_symbol.get_extension())) {
  731. EditorNode::get_singleton()->load_scene(p_symbol);
  732. } else {
  733. EditorNode::get_singleton()->load_resource(p_symbol);
  734. }
  735. } else if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path(), base, result) == OK) {
  736. _goto_line(p_row);
  737. result.class_name = result.class_name.trim_prefix("_");
  738. switch (result.type) {
  739. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  740. if (result.script.is_valid()) {
  741. emit_signal("request_open_script_at_line", result.script, result.location - 1);
  742. } else {
  743. emit_signal("request_save_history");
  744. goto_line_centered(result.location - 1);
  745. }
  746. } break;
  747. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  748. emit_signal("go_to_help", "class_name:" + result.class_name);
  749. } break;
  750. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  751. StringName cname = result.class_name;
  752. bool success;
  753. while (true) {
  754. ClassDB::get_integer_constant(cname, result.class_member, &success);
  755. if (success) {
  756. result.class_name = cname;
  757. cname = ClassDB::get_parent_class(cname);
  758. } else {
  759. break;
  760. }
  761. }
  762. emit_signal("go_to_help", "class_constant:" + result.class_name + ":" + result.class_member);
  763. } break;
  764. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  765. emit_signal("go_to_help", "class_property:" + result.class_name + ":" + result.class_member);
  766. } break;
  767. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  768. StringName cname = result.class_name;
  769. while (true) {
  770. if (ClassDB::has_method(cname, result.class_member)) {
  771. result.class_name = cname;
  772. cname = ClassDB::get_parent_class(cname);
  773. } else {
  774. break;
  775. }
  776. }
  777. emit_signal("go_to_help", "class_method:" + result.class_name + ":" + result.class_member);
  778. } break;
  779. case ScriptLanguage::LookupResult::RESULT_CLASS_ENUM: {
  780. StringName cname = result.class_name;
  781. StringName success;
  782. while (true) {
  783. success = ClassDB::get_integer_constant_enum(cname, result.class_member, true);
  784. if (success != StringName()) {
  785. result.class_name = cname;
  786. cname = ClassDB::get_parent_class(cname);
  787. } else {
  788. break;
  789. }
  790. }
  791. emit_signal("go_to_help", "class_enum:" + result.class_name + ":" + result.class_member);
  792. } break;
  793. case ScriptLanguage::LookupResult::RESULT_CLASS_TBD_GLOBALSCOPE: {
  794. emit_signal("go_to_help", "class_global:" + result.class_name + ":" + result.class_member);
  795. } break;
  796. }
  797. } else if (ProjectSettings::get_singleton()->has_setting("autoload/" + p_symbol)) {
  798. //check for Autoload scenes
  799. String path = ProjectSettings::get_singleton()->get("autoload/" + p_symbol);
  800. if (path.begins_with("*")) {
  801. path = path.substr(1, path.length());
  802. EditorNode::get_singleton()->load_scene(path);
  803. }
  804. } else if (p_symbol.is_rel_path()) {
  805. // Every symbol other than absolute path is relative path so keep this condition at last.
  806. String path = _get_absolute_path(p_symbol);
  807. if (FileAccess::exists(path)) {
  808. List<String> scene_extensions;
  809. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  810. if (scene_extensions.find(path.get_extension())) {
  811. EditorNode::get_singleton()->load_scene(path);
  812. } else {
  813. EditorNode::get_singleton()->load_resource(path);
  814. }
  815. }
  816. }
  817. }
  818. String ScriptTextEditor::_get_absolute_path(const String &rel_path) {
  819. String base_path = script->get_path().get_base_dir();
  820. String path = base_path.plus_file(rel_path);
  821. return path.replace("///", "//").simplify_path();
  822. }
  823. void ScriptTextEditor::update_toggle_scripts_button() {
  824. if (code_editor != nullptr) {
  825. code_editor->update_toggle_scripts_button();
  826. }
  827. }
  828. void ScriptTextEditor::_update_connected_methods() {
  829. TextEdit *text_edit = code_editor->get_text_edit();
  830. text_edit->clear_info_icons();
  831. missing_connections.clear();
  832. if (!script_is_valid) {
  833. return;
  834. }
  835. Node *base = get_tree()->get_edited_scene_root();
  836. if (!base) {
  837. return;
  838. }
  839. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  840. Set<StringName> methods_found;
  841. for (int i = 0; i < nodes.size(); i++) {
  842. List<Connection> connections;
  843. nodes[i]->get_signals_connected_to_this(&connections);
  844. for (List<Connection>::Element *E = connections.front(); E; E = E->next()) {
  845. Connection connection = E->get();
  846. if (!(connection.flags & CONNECT_PERSIST)) {
  847. continue;
  848. }
  849. // As deleted nodes are still accessible via the undo/redo system, check if they're still on the tree.
  850. Node *source = Object::cast_to<Node>(connection.source);
  851. if (source && !source->is_inside_tree()) {
  852. continue;
  853. }
  854. if (methods_found.has(connection.method)) {
  855. continue;
  856. }
  857. if (!ClassDB::has_method(script->get_instance_base_type(), connection.method)) {
  858. int line = -1;
  859. for (int j = 0; j < functions.size(); j++) {
  860. String name = functions[j].get_slice(":", 0);
  861. if (name == connection.method) {
  862. line = functions[j].get_slice(":", 1).to_int();
  863. text_edit->set_line_info_icon(line - 1, get_parent_control()->get_icon("Slot", "EditorIcons"), connection.method);
  864. methods_found.insert(connection.method);
  865. break;
  866. }
  867. }
  868. if (line >= 0) {
  869. continue;
  870. }
  871. // There is a chance that the method is inherited from another script.
  872. bool found_inherited_function = false;
  873. Ref<Script> inherited_script = script->get_base_script();
  874. while (!inherited_script.is_null()) {
  875. if (inherited_script->has_method(connection.method)) {
  876. found_inherited_function = true;
  877. break;
  878. }
  879. inherited_script = inherited_script->get_base_script();
  880. }
  881. if (!found_inherited_function) {
  882. missing_connections.push_back(connection);
  883. }
  884. }
  885. }
  886. }
  887. }
  888. void ScriptTextEditor::_lookup_connections(int p_row, String p_method) {
  889. Node *base = get_tree()->get_edited_scene_root();
  890. if (!base) {
  891. return;
  892. }
  893. Vector<Node *> nodes = _find_all_node_for_script(base, base, script);
  894. connection_info_dialog->popup_connections(p_method, nodes);
  895. }
  896. void ScriptTextEditor::_edit_option(int p_op) {
  897. TextEdit *tx = code_editor->get_text_edit();
  898. switch (p_op) {
  899. case EDIT_UNDO: {
  900. tx->undo();
  901. tx->call_deferred("grab_focus");
  902. } break;
  903. case EDIT_REDO: {
  904. tx->redo();
  905. tx->call_deferred("grab_focus");
  906. } break;
  907. case EDIT_CUT: {
  908. tx->cut();
  909. tx->call_deferred("grab_focus");
  910. } break;
  911. case EDIT_COPY: {
  912. tx->copy();
  913. tx->call_deferred("grab_focus");
  914. } break;
  915. case EDIT_PASTE: {
  916. tx->paste();
  917. tx->call_deferred("grab_focus");
  918. } break;
  919. case EDIT_SELECT_ALL: {
  920. tx->select_all();
  921. tx->call_deferred("grab_focus");
  922. } break;
  923. case EDIT_MOVE_LINE_UP: {
  924. code_editor->move_lines_up();
  925. } break;
  926. case EDIT_MOVE_LINE_DOWN: {
  927. code_editor->move_lines_down();
  928. } break;
  929. case EDIT_INDENT_LEFT: {
  930. Ref<Script> scr = script;
  931. if (scr.is_null()) {
  932. return;
  933. }
  934. tx->indent_left();
  935. } break;
  936. case EDIT_INDENT_RIGHT: {
  937. Ref<Script> scr = script;
  938. if (scr.is_null()) {
  939. return;
  940. }
  941. tx->indent_right();
  942. } break;
  943. case EDIT_DELETE_LINE: {
  944. code_editor->delete_lines();
  945. } break;
  946. case EDIT_DUPLICATE_SELECTION: {
  947. code_editor->duplicate_selection();
  948. } break;
  949. case EDIT_TOGGLE_FOLD_LINE: {
  950. tx->toggle_fold_line(tx->cursor_get_line());
  951. tx->update();
  952. } break;
  953. case EDIT_FOLD_ALL_LINES: {
  954. tx->fold_all_lines();
  955. tx->update();
  956. } break;
  957. case EDIT_UNFOLD_ALL_LINES: {
  958. tx->unhide_all_lines();
  959. tx->update();
  960. } break;
  961. case EDIT_TOGGLE_COMMENT: {
  962. _edit_option_toggle_inline_comment();
  963. } break;
  964. case EDIT_COMPLETE: {
  965. tx->query_code_comple();
  966. } break;
  967. case EDIT_AUTO_INDENT: {
  968. String text = tx->get_text();
  969. Ref<Script> scr = script;
  970. if (scr.is_null()) {
  971. return;
  972. }
  973. tx->begin_complex_operation();
  974. int begin, end;
  975. if (tx->is_selection_active()) {
  976. begin = tx->get_selection_from_line();
  977. end = tx->get_selection_to_line();
  978. // ignore if the cursor is not past the first column
  979. if (tx->get_selection_to_column() == 0) {
  980. end--;
  981. }
  982. } else {
  983. begin = 0;
  984. end = tx->get_line_count() - 1;
  985. }
  986. scr->get_language()->auto_indent_code(text, begin, end);
  987. Vector<String> lines = text.split("\n");
  988. for (int i = begin; i <= end; ++i) {
  989. tx->set_line(i, lines[i]);
  990. }
  991. tx->end_complex_operation();
  992. } break;
  993. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  994. trim_trailing_whitespace();
  995. } break;
  996. case EDIT_CONVERT_INDENT_TO_SPACES: {
  997. convert_indent_to_spaces();
  998. } break;
  999. case EDIT_CONVERT_INDENT_TO_TABS: {
  1000. convert_indent_to_tabs();
  1001. } break;
  1002. case EDIT_PICK_COLOR: {
  1003. color_panel->popup();
  1004. } break;
  1005. case EDIT_TO_UPPERCASE: {
  1006. _convert_case(CodeTextEditor::UPPER);
  1007. } break;
  1008. case EDIT_TO_LOWERCASE: {
  1009. _convert_case(CodeTextEditor::LOWER);
  1010. } break;
  1011. case EDIT_CAPITALIZE: {
  1012. _convert_case(CodeTextEditor::CAPITALIZE);
  1013. } break;
  1014. case EDIT_EVALUATE: {
  1015. Expression expression;
  1016. Vector<String> lines = code_editor->get_text_edit()->get_selection_text().split("\n");
  1017. PoolStringArray results;
  1018. for (int i = 0; i < lines.size(); i++) {
  1019. String line = lines[i];
  1020. String whitespace = line.substr(0, line.size() - line.strip_edges(true, false).size()); //extract the whitespace at the beginning
  1021. if (expression.parse(line) == OK) {
  1022. Variant result = expression.execute(Array(), Variant(), false);
  1023. if (expression.get_error_text() == "") {
  1024. results.append(whitespace + result.get_construct_string());
  1025. } else {
  1026. results.append(line);
  1027. }
  1028. } else {
  1029. results.append(line);
  1030. }
  1031. }
  1032. code_editor->get_text_edit()->begin_complex_operation(); //prevents creating a two-step undo
  1033. code_editor->get_text_edit()->insert_text_at_cursor(results.join("\n"));
  1034. code_editor->get_text_edit()->end_complex_operation();
  1035. } break;
  1036. case SEARCH_FIND: {
  1037. code_editor->get_find_replace_bar()->popup_search();
  1038. } break;
  1039. case SEARCH_FIND_NEXT: {
  1040. code_editor->get_find_replace_bar()->search_next();
  1041. } break;
  1042. case SEARCH_FIND_PREV: {
  1043. code_editor->get_find_replace_bar()->search_prev();
  1044. } break;
  1045. case SEARCH_REPLACE: {
  1046. code_editor->get_find_replace_bar()->popup_replace();
  1047. } break;
  1048. case SEARCH_IN_FILES: {
  1049. String selected_text = code_editor->get_text_edit()->get_selection_text();
  1050. // Yep, because it doesn't make sense to instance this dialog for every single script open...
  1051. // So this will be delegated to the ScriptEditor.
  1052. emit_signal("search_in_files_requested", selected_text);
  1053. } break;
  1054. case REPLACE_IN_FILES: {
  1055. String selected_text = code_editor->get_text_edit()->get_selection_text();
  1056. emit_signal("replace_in_files_requested", selected_text);
  1057. } break;
  1058. case SEARCH_LOCATE_FUNCTION: {
  1059. quick_open->popup_dialog(get_functions());
  1060. quick_open->set_title(TTR("Go to Function"));
  1061. } break;
  1062. case SEARCH_GOTO_LINE: {
  1063. goto_line_dialog->popup_find_line(tx);
  1064. } break;
  1065. case BOOKMARK_TOGGLE: {
  1066. code_editor->toggle_bookmark();
  1067. } break;
  1068. case BOOKMARK_GOTO_NEXT: {
  1069. code_editor->goto_next_bookmark();
  1070. } break;
  1071. case BOOKMARK_GOTO_PREV: {
  1072. code_editor->goto_prev_bookmark();
  1073. } break;
  1074. case BOOKMARK_REMOVE_ALL: {
  1075. code_editor->remove_all_bookmarks();
  1076. } break;
  1077. case DEBUG_TOGGLE_BREAKPOINT: {
  1078. int line = tx->cursor_get_line();
  1079. bool dobreak = !tx->is_line_set_as_breakpoint(line);
  1080. tx->set_line_as_breakpoint(line, dobreak);
  1081. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1082. } break;
  1083. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  1084. List<int> bpoints;
  1085. tx->get_breakpoints(&bpoints);
  1086. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1087. int line = E->get();
  1088. bool dobreak = !tx->is_line_set_as_breakpoint(line);
  1089. tx->set_line_as_breakpoint(line, dobreak);
  1090. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(script->get_path(), line + 1, dobreak);
  1091. }
  1092. } break;
  1093. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  1094. List<int> bpoints;
  1095. tx->get_breakpoints(&bpoints);
  1096. if (bpoints.size() <= 0) {
  1097. return;
  1098. }
  1099. int line = tx->cursor_get_line();
  1100. // wrap around
  1101. if (line >= bpoints[bpoints.size() - 1]) {
  1102. tx->unfold_line(bpoints[0]);
  1103. tx->cursor_set_line(bpoints[0]);
  1104. tx->center_viewport_to_cursor();
  1105. } else {
  1106. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  1107. int bline = E->get();
  1108. if (bline > line) {
  1109. tx->unfold_line(bline);
  1110. tx->cursor_set_line(bline);
  1111. tx->center_viewport_to_cursor();
  1112. return;
  1113. }
  1114. }
  1115. }
  1116. } break;
  1117. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1118. List<int> bpoints;
  1119. tx->get_breakpoints(&bpoints);
  1120. if (bpoints.size() <= 0) {
  1121. return;
  1122. }
  1123. int line = tx->cursor_get_line();
  1124. // wrap around
  1125. if (line <= bpoints[0]) {
  1126. tx->unfold_line(bpoints[bpoints.size() - 1]);
  1127. tx->cursor_set_line(bpoints[bpoints.size() - 1]);
  1128. tx->center_viewport_to_cursor();
  1129. } else {
  1130. for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
  1131. int bline = E->get();
  1132. if (bline < line) {
  1133. tx->unfold_line(bline);
  1134. tx->cursor_set_line(bline);
  1135. tx->center_viewport_to_cursor();
  1136. return;
  1137. }
  1138. }
  1139. }
  1140. } break;
  1141. case HELP_CONTEXTUAL: {
  1142. String text = tx->get_selection_text();
  1143. if (text == "") {
  1144. text = tx->get_word_under_cursor();
  1145. }
  1146. if (text != "") {
  1147. emit_signal("request_help", text);
  1148. }
  1149. } break;
  1150. case LOOKUP_SYMBOL: {
  1151. String text = tx->get_word_under_cursor();
  1152. if (text == "") {
  1153. text = tx->get_selection_text();
  1154. }
  1155. if (text != "") {
  1156. _lookup_symbol(text, tx->cursor_get_line(), tx->cursor_get_column());
  1157. }
  1158. } break;
  1159. }
  1160. }
  1161. void ScriptTextEditor::_edit_option_toggle_inline_comment() {
  1162. if (script.is_null()) {
  1163. return;
  1164. }
  1165. String delimiter = "#";
  1166. List<String> comment_delimiters;
  1167. script->get_language()->get_comment_delimiters(&comment_delimiters);
  1168. for (List<String>::Element *E = comment_delimiters.front(); E; E = E->next()) {
  1169. String script_delimiter = E->get();
  1170. if (script_delimiter.find(" ") == -1) {
  1171. delimiter = script_delimiter;
  1172. break;
  1173. }
  1174. }
  1175. code_editor->toggle_inline_comment(delimiter);
  1176. }
  1177. void ScriptTextEditor::add_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  1178. highlighters[p_highlighter->get_name()] = p_highlighter;
  1179. highlighter_menu->add_radio_check_item(p_highlighter->get_name());
  1180. }
  1181. void ScriptTextEditor::set_syntax_highlighter(SyntaxHighlighter *p_highlighter) {
  1182. TextEdit *te = code_editor->get_text_edit();
  1183. te->_set_syntax_highlighting(p_highlighter);
  1184. if (p_highlighter != nullptr) {
  1185. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(p_highlighter->get_name()), true);
  1186. } else {
  1187. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(TTR("Standard")), true);
  1188. }
  1189. }
  1190. void ScriptTextEditor::_change_syntax_highlighter(int p_idx) {
  1191. Map<String, SyntaxHighlighter *>::Element *el = highlighters.front();
  1192. while (el != nullptr) {
  1193. highlighter_menu->set_item_checked(highlighter_menu->get_item_idx_from_text(el->key()), false);
  1194. el = el->next();
  1195. }
  1196. // highlighter_menu->set_item_checked(p_idx, true);
  1197. set_syntax_highlighter(highlighters[highlighter_menu->get_item_text(p_idx)]);
  1198. }
  1199. void ScriptTextEditor::_bind_methods() {
  1200. ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
  1201. ClassDB::bind_method("_update_bookmark_list", &ScriptTextEditor::_update_bookmark_list);
  1202. ClassDB::bind_method("_bookmark_item_pressed", &ScriptTextEditor::_bookmark_item_pressed);
  1203. ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
  1204. ClassDB::bind_method("_update_breakpoint_list", &ScriptTextEditor::_update_breakpoint_list);
  1205. ClassDB::bind_method("_breakpoint_item_pressed", &ScriptTextEditor::_breakpoint_item_pressed);
  1206. ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
  1207. ClassDB::bind_method("_lookup_connections", &ScriptTextEditor::_lookup_connections);
  1208. ClassDB::bind_method("_update_connected_methods", &ScriptTextEditor::_update_connected_methods);
  1209. ClassDB::bind_method("_change_syntax_highlighter", &ScriptTextEditor::_change_syntax_highlighter);
  1210. ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
  1211. ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
  1212. ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
  1213. ClassDB::bind_method("_text_edit_gui_input", &ScriptTextEditor::_text_edit_gui_input);
  1214. ClassDB::bind_method("_show_warnings_panel", &ScriptTextEditor::_show_warnings_panel);
  1215. ClassDB::bind_method("_warning_clicked", &ScriptTextEditor::_warning_clicked);
  1216. ClassDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  1217. ClassDB::bind_method("_prepare_edit_menu", &ScriptTextEditor::_prepare_edit_menu);
  1218. ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
  1219. ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
  1220. ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
  1221. }
  1222. Control *ScriptTextEditor::get_edit_menu() {
  1223. return edit_hb;
  1224. }
  1225. void ScriptTextEditor::clear_edit_menu() {
  1226. memdelete(edit_hb);
  1227. }
  1228. void ScriptTextEditor::reload(bool p_soft) {
  1229. TextEdit *te = code_editor->get_text_edit();
  1230. Ref<Script> scr = script;
  1231. if (scr.is_null()) {
  1232. return;
  1233. }
  1234. scr->set_source_code(te->get_text());
  1235. bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
  1236. scr->get_language()->reload_tool_script(scr, soft);
  1237. }
  1238. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  1239. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  1240. }
  1241. void ScriptTextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1242. code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
  1243. }
  1244. void ScriptTextEditor::set_debugger_active(bool p_active) {
  1245. }
  1246. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1247. return Variant();
  1248. }
  1249. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1250. Dictionary d = p_data;
  1251. if (d.has("type") &&
  1252. (String(d["type"]) == "resource" ||
  1253. String(d["type"]) == "files" ||
  1254. String(d["type"]) == "nodes" ||
  1255. String(d["type"]) == "obj_property" ||
  1256. String(d["type"]) == "files_and_dirs")) {
  1257. return true;
  1258. }
  1259. return false;
  1260. }
  1261. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1262. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene) {
  1263. return nullptr;
  1264. }
  1265. Ref<Script> scr = p_current_node->get_script();
  1266. if (scr.is_valid() && scr == script) {
  1267. return p_current_node;
  1268. }
  1269. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1270. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1271. if (n) {
  1272. return n;
  1273. }
  1274. }
  1275. return nullptr;
  1276. }
  1277. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1278. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  1279. Dictionary d = p_data;
  1280. TextEdit *te = code_editor->get_text_edit();
  1281. int row, col;
  1282. te->_get_mouse_pos(p_point, row, col);
  1283. if (d.has("type") && String(d["type"]) == "resource") {
  1284. Ref<Resource> res = d["resource"];
  1285. if (!res.is_valid()) {
  1286. return;
  1287. }
  1288. if (res->get_path().is_resource_file()) {
  1289. EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
  1290. return;
  1291. }
  1292. te->cursor_set_line(row);
  1293. te->cursor_set_column(col);
  1294. te->insert_text_at_cursor(res->get_path());
  1295. }
  1296. if (d.has("type") && (String(d["type"]) == "files" || String(d["type"]) == "files_and_dirs")) {
  1297. Array files = d["files"];
  1298. String text_to_drop;
  1299. bool preload = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  1300. for (int i = 0; i < files.size(); i++) {
  1301. if (i > 0) {
  1302. text_to_drop += ", ";
  1303. }
  1304. if (preload) {
  1305. text_to_drop += "preload(" + String(files[i]).c_escape().quote(quote_style) + ")";
  1306. } else {
  1307. text_to_drop += String(files[i]).c_escape().quote(quote_style);
  1308. }
  1309. }
  1310. te->cursor_set_line(row);
  1311. te->cursor_set_column(col);
  1312. te->insert_text_at_cursor(text_to_drop);
  1313. }
  1314. if (d.has("type") && String(d["type"]) == "nodes") {
  1315. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1316. if (!sn) {
  1317. EditorNode::get_singleton()->show_warning(vformat(TTR("Can't drop nodes because script '%s' is not used in this scene."), get_name()));
  1318. return;
  1319. }
  1320. Array nodes = d["nodes"];
  1321. String text_to_drop;
  1322. if (Input::get_singleton()->is_key_pressed(KEY_CONTROL)) {
  1323. bool use_type = EDITOR_GET("text_editor/completion/add_type_hints");
  1324. for (int i = 0; i < nodes.size(); i++) {
  1325. NodePath np = nodes[i];
  1326. Node *node = get_node(np);
  1327. if (!node) {
  1328. continue;
  1329. }
  1330. String path;
  1331. if (node->is_unique_name_in_owner()) {
  1332. path = "%" + node->get_name();
  1333. } else {
  1334. path = sn->get_path_to(node);
  1335. }
  1336. Vector<String> segments = path.split("/");
  1337. for (int j = 0; j < segments.size(); j++) {
  1338. if (!segments[j].is_valid_identifier()) {
  1339. path = path.c_escape().quote(quote_style);
  1340. break;
  1341. }
  1342. }
  1343. String variable_name = String(node->get_name()).camelcase_to_underscore(true).validate_identifier();
  1344. if (use_type) {
  1345. text_to_drop += vformat("onready var %s: %s = $%s\n", variable_name, node->get_class_name(), path);
  1346. } else {
  1347. text_to_drop += vformat("onready var %s = $%s\n", variable_name, path);
  1348. }
  1349. }
  1350. } else {
  1351. for (int i = 0; i < nodes.size(); i++) {
  1352. if (i > 0) {
  1353. text_to_drop += ", ";
  1354. }
  1355. NodePath np = nodes[i];
  1356. Node *node = get_node(np);
  1357. if (!node) {
  1358. continue;
  1359. }
  1360. String path;
  1361. if (node->is_unique_name_in_owner()) {
  1362. path = "%" + node->get_name();
  1363. } else {
  1364. path = sn->get_path_to(node);
  1365. }
  1366. Vector<String> segments = path.split("/");
  1367. for (int j = 0; j < segments.size(); j++) {
  1368. if (!segments[j].is_valid_identifier()) {
  1369. path = path.c_escape().quote(quote_style);
  1370. break;
  1371. }
  1372. }
  1373. text_to_drop += "$" + path;
  1374. }
  1375. }
  1376. te->cursor_set_line(row);
  1377. te->cursor_set_column(col);
  1378. te->insert_text_at_cursor(text_to_drop);
  1379. }
  1380. if (d.has("type") && String(d["type"]) == "obj_property") {
  1381. const String text_to_drop = String(d["property"]).c_escape().quote(quote_style);
  1382. te->cursor_set_line(row);
  1383. te->cursor_set_column(col);
  1384. te->insert_text_at_cursor(text_to_drop);
  1385. }
  1386. }
  1387. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  1388. Ref<InputEventMouseButton> mb = ev;
  1389. Ref<InputEventKey> k = ev;
  1390. Point2 local_pos;
  1391. bool create_menu = false;
  1392. TextEdit *tx = code_editor->get_text_edit();
  1393. if (mb.is_valid() && mb->get_button_index() == BUTTON_RIGHT && mb->is_pressed()) {
  1394. local_pos = mb->get_global_position() - tx->get_global_position();
  1395. create_menu = true;
  1396. } else if (k.is_valid() && k->get_scancode() == KEY_MENU) {
  1397. local_pos = tx->_get_cursor_pixel_pos();
  1398. create_menu = true;
  1399. }
  1400. if (create_menu) {
  1401. int col, row;
  1402. tx->_get_mouse_pos(local_pos, row, col);
  1403. tx->set_right_click_moves_caret(EditorSettings::get_singleton()->get("text_editor/cursor/right_click_moves_caret"));
  1404. if (tx->is_right_click_moving_caret()) {
  1405. if (tx->is_selection_active()) {
  1406. int from_line = tx->get_selection_from_line();
  1407. int to_line = tx->get_selection_to_line();
  1408. int from_column = tx->get_selection_from_column();
  1409. int to_column = tx->get_selection_to_column();
  1410. if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
  1411. // Right click is outside the selected text
  1412. tx->deselect();
  1413. }
  1414. }
  1415. if (!tx->is_selection_active()) {
  1416. tx->cursor_set_line(row, true, false);
  1417. tx->cursor_set_column(col);
  1418. }
  1419. }
  1420. String word_at_pos = tx->get_word_at_pos(local_pos);
  1421. if (word_at_pos == "") {
  1422. word_at_pos = tx->get_word_under_cursor();
  1423. }
  1424. if (word_at_pos == "") {
  1425. word_at_pos = tx->get_selection_text();
  1426. }
  1427. bool has_color = (word_at_pos == "Color");
  1428. bool foldable = tx->can_fold(row) || tx->is_folded(row);
  1429. bool open_docs = false;
  1430. bool goto_definition = false;
  1431. if (word_at_pos.is_resource_file()) {
  1432. open_docs = true;
  1433. } else {
  1434. Node *base = get_tree()->get_edited_scene_root();
  1435. if (base) {
  1436. base = _find_node_for_script(base, base, script);
  1437. }
  1438. ScriptLanguage::LookupResult result;
  1439. if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), word_at_pos, script->get_path(), base, result) == OK) {
  1440. open_docs = true;
  1441. }
  1442. }
  1443. if (has_color) {
  1444. String line = tx->get_line(row);
  1445. color_position.x = row;
  1446. color_position.y = col;
  1447. int begin = 0;
  1448. int end = 0;
  1449. bool valid = false;
  1450. for (int i = col; i < line.length(); i++) {
  1451. if (line[i] == '(') {
  1452. begin = i;
  1453. continue;
  1454. } else if (line[i] == ')') {
  1455. end = i + 1;
  1456. valid = true;
  1457. break;
  1458. }
  1459. }
  1460. if (valid) {
  1461. color_args = line.substr(begin, end - begin);
  1462. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  1463. Vector<float> color = stripped.split_floats(",");
  1464. if (color.size() > 2) {
  1465. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1466. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1467. }
  1468. color_panel->set_position(get_global_transform().xform(local_pos));
  1469. } else {
  1470. has_color = false;
  1471. }
  1472. }
  1473. _make_context_menu(tx->is_selection_active(), has_color, foldable, open_docs, goto_definition, local_pos);
  1474. }
  1475. }
  1476. void ScriptTextEditor::_color_changed(const Color &p_color) {
  1477. String new_args;
  1478. if (p_color.a == 1.0f) {
  1479. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
  1480. } else {
  1481. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
  1482. }
  1483. String line = code_editor->get_text_edit()->get_line(color_position.x);
  1484. int color_args_pos = line.find(color_args, color_position.y);
  1485. String line_with_replaced_args = line;
  1486. line_with_replaced_args.erase(color_args_pos, color_args.length());
  1487. line_with_replaced_args = line_with_replaced_args.insert(color_args_pos, new_args);
  1488. color_args = new_args;
  1489. code_editor->get_text_edit()->begin_complex_operation();
  1490. code_editor->get_text_edit()->set_line(color_position.x, line_with_replaced_args);
  1491. code_editor->get_text_edit()->end_complex_operation();
  1492. code_editor->get_text_edit()->update();
  1493. }
  1494. void ScriptTextEditor::_prepare_edit_menu() {
  1495. const TextEdit *tx = code_editor->get_text_edit();
  1496. PopupMenu *popup = edit_menu->get_popup();
  1497. popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
  1498. popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
  1499. }
  1500. 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) {
  1501. context_menu->clear();
  1502. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1503. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1504. context_menu->add_separator();
  1505. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1506. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1507. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1508. context_menu->add_separator();
  1509. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1510. context_menu->add_separator();
  1511. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1512. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1513. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1514. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
  1515. if (p_selection) {
  1516. context_menu->add_separator();
  1517. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  1518. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  1519. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  1520. }
  1521. if (p_foldable) {
  1522. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  1523. }
  1524. if (p_color || p_open_docs || p_goto_definition) {
  1525. context_menu->add_separator();
  1526. if (p_open_docs) {
  1527. context_menu->add_item(TTR("Lookup Symbol"), LOOKUP_SYMBOL);
  1528. }
  1529. if (p_color) {
  1530. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  1531. }
  1532. }
  1533. const TextEdit *tx = code_editor->get_text_edit();
  1534. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
  1535. context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
  1536. context_menu->set_position(get_global_transform().xform(p_pos));
  1537. context_menu->set_size(Vector2(1, 1));
  1538. context_menu->popup();
  1539. }
  1540. void ScriptTextEditor::_enable_code_editor() {
  1541. ERR_FAIL_COND(code_editor->get_parent());
  1542. VSplitContainer *editor_box = memnew(VSplitContainer);
  1543. add_child(editor_box);
  1544. editor_box->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1545. editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
  1546. editor_box->add_child(code_editor);
  1547. code_editor->connect("show_warnings_panel", this, "_show_warnings_panel");
  1548. code_editor->connect("validate_script", this, "_validate_script");
  1549. code_editor->connect("load_theme_settings", this, "_load_theme_settings");
  1550. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  1551. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  1552. code_editor->get_text_edit()->connect("info_clicked", this, "_lookup_connections");
  1553. code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  1554. code_editor->show_toggle_scripts_button();
  1555. editor_box->add_child(warnings_panel);
  1556. warnings_panel->add_font_override(
  1557. "normal_font", EditorNode::get_singleton()->get_gui_base()->get_font("main", "EditorFonts"));
  1558. warnings_panel->connect("meta_clicked", this, "_warning_clicked");
  1559. add_child(context_menu);
  1560. context_menu->connect("id_pressed", this, "_edit_option");
  1561. context_menu->set_hide_on_window_lose_focus(true);
  1562. add_child(color_panel);
  1563. color_picker = memnew(ColorPicker);
  1564. color_picker->set_deferred_mode(true);
  1565. color_picker->connect("color_changed", this, "_color_changed");
  1566. color_panel->add_child(color_picker);
  1567. // get default color picker mode from editor settings
  1568. int default_color_mode = EDITOR_GET("interface/inspector/default_color_picker_mode");
  1569. if (default_color_mode == 1) {
  1570. color_picker->set_hsv_mode(true);
  1571. } else if (default_color_mode == 2) {
  1572. color_picker->set_raw_mode(true);
  1573. }
  1574. quick_open = memnew(ScriptEditorQuickOpen);
  1575. quick_open->connect("goto_line", this, "_goto_line");
  1576. add_child(quick_open);
  1577. goto_line_dialog = memnew(GotoLineDialog);
  1578. add_child(goto_line_dialog);
  1579. add_child(connection_info_dialog);
  1580. edit_hb->add_child(search_menu);
  1581. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1582. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1583. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1584. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1585. search_menu->get_popup()->add_separator();
  1586. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_in_files"), SEARCH_IN_FILES);
  1587. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
  1588. search_menu->get_popup()->add_separator();
  1589. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1590. search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1591. edit_hb->add_child(edit_menu);
  1592. edit_menu->connect("about_to_show", this, "_prepare_edit_menu");
  1593. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1594. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1595. edit_menu->get_popup()->add_separator();
  1596. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1597. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1598. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1599. edit_menu->get_popup()->add_separator();
  1600. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1601. edit_menu->get_popup()->add_separator();
  1602. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1603. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1604. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1605. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1606. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  1607. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1608. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
  1609. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  1610. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  1611. edit_menu->get_popup()->add_separator();
  1612. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
  1613. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1614. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/evaluate_selection"), EDIT_EVALUATE);
  1615. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1616. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  1617. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  1618. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1619. edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1620. edit_menu->get_popup()->add_separator();
  1621. edit_menu->get_popup()->add_child(convert_case);
  1622. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  1623. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase"), KEY_MASK_SHIFT | KEY_F4), EDIT_TO_UPPERCASE);
  1624. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase"), KEY_MASK_SHIFT | KEY_F5), EDIT_TO_LOWERCASE);
  1625. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F6), EDIT_CAPITALIZE);
  1626. convert_case->connect("id_pressed", this, "_edit_option");
  1627. edit_menu->get_popup()->add_child(highlighter_menu);
  1628. edit_menu->get_popup()->add_submenu_item(TTR("Syntax Highlighter"), "highlighter_menu");
  1629. highlighter_menu->connect("id_pressed", this, "_change_syntax_highlighter");
  1630. _load_theme_settings();
  1631. edit_hb->add_child(goto_menu);
  1632. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1633. goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1634. goto_menu->get_popup()->add_separator();
  1635. goto_menu->get_popup()->add_child(bookmarks_menu);
  1636. goto_menu->get_popup()->add_submenu_item(TTR("Bookmarks"), "Bookmarks");
  1637. _update_bookmark_list();
  1638. bookmarks_menu->connect("about_to_show", this, "_update_bookmark_list");
  1639. bookmarks_menu->connect("index_pressed", this, "_bookmark_item_pressed");
  1640. goto_menu->get_popup()->add_child(breakpoints_menu);
  1641. goto_menu->get_popup()->add_submenu_item(TTR("Breakpoints"), "Breakpoints");
  1642. _update_breakpoint_list();
  1643. breakpoints_menu->connect("about_to_show", this, "_update_breakpoint_list");
  1644. breakpoints_menu->connect("index_pressed", this, "_breakpoint_item_pressed");
  1645. goto_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1646. }
  1647. ScriptTextEditor::ScriptTextEditor() {
  1648. theme_loaded = false;
  1649. script_is_valid = false;
  1650. editor_enabled = false;
  1651. code_editor = memnew(CodeTextEditor);
  1652. code_editor->add_constant_override("separation", 2);
  1653. code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1654. code_editor->set_code_complete_func(_code_complete_scripts, this);
  1655. code_editor->set_v_size_flags(SIZE_EXPAND_FILL);
  1656. warnings_panel = memnew(RichTextLabel);
  1657. warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
  1658. warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
  1659. warnings_panel->set_meta_underline(true);
  1660. warnings_panel->set_selection_enabled(true);
  1661. warnings_panel->set_focus_mode(FOCUS_CLICK);
  1662. warnings_panel->hide();
  1663. update_settings();
  1664. code_editor->get_text_edit()->set_callhint_settings(
  1665. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1666. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  1667. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  1668. code_editor->get_text_edit()->set_context_menu_enabled(false);
  1669. context_menu = memnew(PopupMenu);
  1670. color_panel = memnew(PopupPanel);
  1671. color_picker = nullptr;
  1672. edit_hb = memnew(HBoxContainer);
  1673. edit_menu = memnew(MenuButton);
  1674. edit_menu->set_text(TTR("Edit"));
  1675. edit_menu->set_switch_on_hover(true);
  1676. edit_menu->get_popup()->set_hide_on_window_lose_focus(true);
  1677. convert_case = memnew(PopupMenu);
  1678. convert_case->set_name("convert_case");
  1679. highlighters[TTR("Standard")] = NULL;
  1680. highlighter_menu = memnew(PopupMenu);
  1681. highlighter_menu->set_name("highlighter_menu");
  1682. highlighter_menu->add_radio_check_item(TTR("Standard"));
  1683. search_menu = memnew(MenuButton);
  1684. search_menu->set_text(TTR("Search"));
  1685. search_menu->set_switch_on_hover(true);
  1686. search_menu->get_popup()->set_hide_on_window_lose_focus(true);
  1687. goto_menu = memnew(MenuButton);
  1688. goto_menu->set_text(TTR("Go To"));
  1689. goto_menu->set_switch_on_hover(true);
  1690. bookmarks_menu = memnew(PopupMenu);
  1691. bookmarks_menu->set_name("Bookmarks");
  1692. breakpoints_menu = memnew(PopupMenu);
  1693. breakpoints_menu->set_name("Breakpoints");
  1694. quick_open = nullptr;
  1695. goto_line_dialog = nullptr;
  1696. connection_info_dialog = memnew(ConnectionInfoDialog);
  1697. code_editor->get_text_edit()->set_drag_forwarding(this);
  1698. }
  1699. ScriptTextEditor::~ScriptTextEditor() {
  1700. for (const Map<String, SyntaxHighlighter *>::Element *E = highlighters.front(); E; E = E->next()) {
  1701. if (E->get() != NULL) {
  1702. memdelete(E->get());
  1703. }
  1704. }
  1705. highlighters.clear();
  1706. if (!editor_enabled) {
  1707. memdelete(code_editor);
  1708. memdelete(warnings_panel);
  1709. memdelete(context_menu);
  1710. memdelete(color_panel);
  1711. memdelete(edit_hb);
  1712. memdelete(edit_menu);
  1713. memdelete(convert_case);
  1714. memdelete(highlighter_menu);
  1715. memdelete(search_menu);
  1716. memdelete(goto_menu);
  1717. memdelete(bookmarks_menu);
  1718. memdelete(breakpoints_menu);
  1719. memdelete(connection_info_dialog);
  1720. }
  1721. }
  1722. static ScriptEditorBase *create_editor(const RES &p_resource) {
  1723. if (Object::cast_to<Script>(*p_resource)) {
  1724. return memnew(ScriptTextEditor);
  1725. }
  1726. return nullptr;
  1727. }
  1728. void ScriptTextEditor::register_editor() {
  1729. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z);
  1730. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y);
  1731. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  1732. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  1733. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  1734. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
  1735. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
  1736. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
  1737. ED_SHORTCUT("script_text_editor/delete_line", TTR("Delete Line"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_K);
  1738. // Leave these at zero, same can be accomplished with tab/shift-tab, including selection.
  1739. // The next/previous in history shortcut in this case makes a lot more sense.
  1740. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
  1741. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
  1742. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K);
  1743. ED_SHORTCUT("script_text_editor/toggle_fold_line", TTR("Fold/Unfold Line"), KEY_MASK_ALT | KEY_F);
  1744. ED_SHORTCUT("script_text_editor/fold_all_lines", TTR("Fold All Lines"), 0);
  1745. ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), 0);
  1746. #ifdef OSX_ENABLED
  1747. ED_SHORTCUT("script_text_editor/duplicate_selection", TTR("Duplicate Selection"), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_C);
  1748. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE);
  1749. #else
  1750. ED_SHORTCUT("script_text_editor/duplicate_selection", TTR("Duplicate Selection"), KEY_MASK_CMD | KEY_D);
  1751. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
  1752. #endif
  1753. ED_SHORTCUT("script_text_editor/evaluate_selection", TTR("Evaluate Selection"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_E);
  1754. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CMD | KEY_MASK_ALT | KEY_T);
  1755. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent to Spaces"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_Y);
  1756. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent to Tabs"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_I);
  1757. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
  1758. ED_SHORTCUT("script_text_editor/find", TTR("Find..."), KEY_MASK_CMD | KEY_F);
  1759. #ifdef OSX_ENABLED
  1760. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_MASK_CMD | KEY_G);
  1761. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_G);
  1762. ED_SHORTCUT("script_text_editor/replace", TTR("Replace..."), KEY_MASK_ALT | KEY_MASK_CMD | KEY_F);
  1763. #else
  1764. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1765. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);
  1766. ED_SHORTCUT("script_text_editor/replace", TTR("Replace..."), KEY_MASK_CMD | KEY_R);
  1767. #endif
  1768. ED_SHORTCUT("script_text_editor/find_in_files", TTR("Find in Files..."), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F);
  1769. ED_SHORTCUT("script_text_editor/replace_in_files", TTR("Replace in Files..."), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_R);
  1770. #ifdef OSX_ENABLED
  1771. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_ALT | KEY_MASK_SHIFT | KEY_SPACE);
  1772. #else
  1773. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_ALT | KEY_F1);
  1774. #endif
  1775. ED_SHORTCUT("script_text_editor/toggle_bookmark", TTR("Toggle Bookmark"), KEY_MASK_CMD | KEY_MASK_ALT | KEY_B);
  1776. ED_SHORTCUT("script_text_editor/goto_next_bookmark", TTR("Go to Next Bookmark"), KEY_MASK_CMD | KEY_B);
  1777. ED_SHORTCUT("script_text_editor/goto_previous_bookmark", TTR("Go to Previous Bookmark"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B);
  1778. ED_SHORTCUT("script_text_editor/remove_all_bookmarks", TTR("Remove All Bookmarks"), 0);
  1779. #ifdef OSX_ENABLED
  1780. ED_SHORTCUT("script_text_editor/goto_function", TTR("Go to Function..."), KEY_MASK_CTRL | KEY_MASK_CMD | KEY_J);
  1781. #else
  1782. ED_SHORTCUT("script_text_editor/goto_function", TTR("Go to Function..."), KEY_MASK_ALT | KEY_MASK_CMD | KEY_F);
  1783. #endif
  1784. ED_SHORTCUT("script_text_editor/goto_line", TTR("Go to Line..."), KEY_MASK_CMD | KEY_L);
  1785. #ifdef OSX_ENABLED
  1786. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_B);
  1787. #else
  1788. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1789. #endif
  1790. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CMD | KEY_MASK_SHIFT | KEY_F9);
  1791. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Go to Next Breakpoint"), KEY_MASK_CMD | KEY_PERIOD);
  1792. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Go to Previous Breakpoint"), KEY_MASK_CMD | KEY_COMMA);
  1793. ScriptEditor::register_create_script_editor_function(create_editor);
  1794. }
  1795. void ScriptTextEditor::validate() {
  1796. this->code_editor->validate_script();
  1797. }