script_text_editor.cpp 70 KB

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