script_text_editor.cpp 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705
  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-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "editor/editor_node.h"
  32. #include "editor/editor_settings.h"
  33. #include "editor/script_editor_debugger.h"
  34. #include "os/keyboard.h"
  35. Vector<String> ScriptTextEditor::get_functions() {
  36. String errortxt;
  37. int line = -1, col;
  38. TextEdit *te = code_editor->get_text_edit();
  39. String text = te->get_text();
  40. List<String> fnc;
  41. if (script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  42. //if valid rewrite functions to latest
  43. functions.clear();
  44. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  45. functions.push_back(E->get());
  46. }
  47. }
  48. return functions;
  49. }
  50. void ScriptTextEditor::apply_code() {
  51. if (script.is_null())
  52. return;
  53. //print_line("applying code");
  54. script->set_source_code(code_editor->get_text_edit()->get_text());
  55. script->update_exports();
  56. }
  57. Ref<Script> ScriptTextEditor::get_edited_script() const {
  58. return script;
  59. }
  60. void ScriptTextEditor::_load_theme_settings() {
  61. TextEdit *text_edit = code_editor->get_text_edit();
  62. text_edit->clear_colors();
  63. Color background_color = EDITOR_DEF("text_editor/highlighting/background_color", Color(0, 0, 0, 0));
  64. Color completion_background_color = EDITOR_DEF("text_editor/highlighting/completion_background_color", Color(0, 0, 0, 0));
  65. Color completion_selected_color = EDITOR_DEF("text_editor/highlighting/completion_selected_color", Color::html("434244"));
  66. Color completion_existing_color = EDITOR_DEF("text_editor/highlighting/completion_existing_color", Color::html("21dfdfdf"));
  67. Color completion_scroll_color = EDITOR_DEF("text_editor/highlighting/completion_scroll_color", Color::html("ffffff"));
  68. Color completion_font_color = EDITOR_DEF("text_editor/highlighting/completion_font_color", Color::html("aaaaaa"));
  69. Color text_color = EDITOR_DEF("text_editor/highlighting/text_color", Color(0, 0, 0));
  70. Color line_number_color = EDITOR_DEF("text_editor/highlighting/line_number_color", Color(0, 0, 0));
  71. Color caret_color = EDITOR_DEF("text_editor/highlighting/caret_color", Color(0, 0, 0));
  72. Color caret_background_color = EDITOR_DEF("text_editor/highlighting/caret_background_color", Color(0, 0, 0));
  73. Color text_selected_color = EDITOR_DEF("text_editor/highlighting/text_selected_color", Color(1, 1, 1));
  74. Color selection_color = EDITOR_DEF("text_editor/highlighting/selection_color", Color(0.2, 0.2, 1));
  75. Color brace_mismatch_color = EDITOR_DEF("text_editor/highlighting/brace_mismatch_color", Color(1, 0.2, 0.2));
  76. Color current_line_color = EDITOR_DEF("text_editor/highlighting/current_line_color", Color(0.3, 0.5, 0.8, 0.15));
  77. Color line_length_guideline_color = EDITOR_DEF("text_editor/highlighting/line_length_guideline_color", Color(0, 0, 0));
  78. Color word_highlighted_color = EDITOR_DEF("text_editor/highlighting/word_highlighted_color", Color(0.8, 0.9, 0.9, 0.15));
  79. Color number_color = EDITOR_DEF("text_editor/highlighting/number_color", Color(0.9, 0.6, 0.0, 2));
  80. Color function_color = EDITOR_DEF("text_editor/highlighting/function_color", Color(0.4, 0.6, 0.8));
  81. Color member_variable_color = EDITOR_DEF("text_editor/highlighting/member_variable_color", Color(0.9, 0.3, 0.3));
  82. Color mark_color = EDITOR_DEF("text_editor/highlighting/mark_color", Color(1.0, 0.4, 0.4, 0.4));
  83. Color breakpoint_color = EDITOR_DEF("text_editor/highlighting/breakpoint_color", Color(0.8, 0.8, 0.4, 0.2));
  84. Color code_folding_color = EDITOR_DEF("text_editor/highlighting/code_folding_color", Color(0.8, 0.8, 0.8, 0.8));
  85. Color search_result_color = EDITOR_DEF("text_editor/highlighting/search_result_color", Color(0.05, 0.25, 0.05, 1));
  86. Color search_result_border_color = EDITOR_DEF("text_editor/highlighting/search_result_border_color", Color(0.1, 0.45, 0.1, 1));
  87. Color symbol_color = EDITOR_DEF("text_editor/highlighting/symbol_color", Color::hex(0x005291ff));
  88. Color keyword_color = EDITOR_DEF("text_editor/highlighting/keyword_color", Color(0.5, 0.0, 0.2));
  89. Color basetype_color = EDITOR_DEF("text_editor/highlighting/base_type_color", Color(0.3, 0.3, 0.0));
  90. Color type_color = EDITOR_DEF("text_editor/highlighting/engine_type_color", Color(0.0, 0.2, 0.4));
  91. Color comment_color = EDITOR_DEF("text_editor/highlighting/comment_color", Color::hex(0x797e7eff));
  92. Color string_color = EDITOR_DEF("text_editor/highlighting/string_color", Color::hex(0x6b6f00ff));
  93. // Adapt
  94. if (EditorSettings::get_singleton()->get("text_editor/theme/color_theme") == "Adaptive") {
  95. Ref<Theme> tm = EditorNode::get_singleton()->get_theme_base()->get_theme();
  96. symbol_color = tm->get_color("text_editor/theme/symbol_color", "Editor");
  97. keyword_color = tm->get_color("text_editor/theme/keyword_color", "Editor");
  98. basetype_color = tm->get_color("text_editor/theme/basetype_color", "Editor");
  99. type_color = tm->get_color("text_editor/theme/type_color", "Editor");
  100. comment_color = tm->get_color("text_editor/theme/comment_color", "Editor");
  101. string_color = tm->get_color("text_editor/theme/string_color", "Editor");
  102. background_color = tm->get_color("text_editor/theme/background_color", "Editor");
  103. completion_background_color = tm->get_color("text_editor/theme/completion_background_color", "Editor");
  104. completion_selected_color = tm->get_color("text_editor/theme/completion_selected_color", "Editor");
  105. completion_existing_color = tm->get_color("text_editor/theme/completion_existing_color", "Editor");
  106. completion_scroll_color = tm->get_color("text_editor/theme/completion_scroll_color", "Editor");
  107. completion_font_color = tm->get_color("text_editor/theme/completion_font_color", "Editor");
  108. text_color = tm->get_color("text_editor/theme/text_color", "Editor");
  109. line_number_color = tm->get_color("text_editor/theme/line_number_color", "Editor");
  110. caret_color = tm->get_color("text_editor/theme/caret_color", "Editor");
  111. caret_background_color = tm->get_color("text_editor/theme/caret_background_color", "Editor");
  112. text_selected_color = tm->get_color("text_editor/theme/text_selected_color", "Editor");
  113. selection_color = tm->get_color("text_editor/theme/selection_color", "Editor");
  114. brace_mismatch_color = tm->get_color("text_editor/theme/brace_mismatch_color", "Editor");
  115. current_line_color = tm->get_color("text_editor/theme/current_line_color", "Editor");
  116. line_length_guideline_color = tm->get_color("text_editor/theme/line_length_guideline_color", "Editor");
  117. word_highlighted_color = tm->get_color("text_editor/theme/word_highlighted_color", "Editor");
  118. number_color = tm->get_color("text_editor/theme/number_color", "Editor");
  119. function_color = tm->get_color("text_editor/theme/function_color", "Editor");
  120. member_variable_color = tm->get_color("text_editor/theme/member_variable_color", "Editor");
  121. mark_color = tm->get_color("text_editor/theme/mark_color", "Editor");
  122. breakpoint_color = tm->get_color("text_editor/theme/breakpoint_color", "Editor");
  123. code_folding_color = tm->get_color("text_editor/theme/code_folding_color", "Editor");
  124. search_result_color = tm->get_color("text_editor/theme/search_result_color", "Editor");
  125. search_result_border_color = tm->get_color("text_editor/theme/search_result_border_color", "Editor");
  126. }
  127. text_edit->add_color_override("background_color", background_color);
  128. text_edit->add_color_override("completion_background_color", completion_background_color);
  129. text_edit->add_color_override("completion_selected_color", completion_selected_color);
  130. text_edit->add_color_override("completion_existing_color", completion_existing_color);
  131. text_edit->add_color_override("completion_scroll_color", completion_scroll_color);
  132. text_edit->add_color_override("completion_font_color", completion_font_color);
  133. text_edit->add_color_override("font_color", text_color);
  134. text_edit->add_color_override("line_number_color", line_number_color);
  135. text_edit->add_color_override("caret_color", caret_color);
  136. text_edit->add_color_override("caret_background_color", caret_background_color);
  137. text_edit->add_color_override("font_selected_color", text_selected_color);
  138. text_edit->add_color_override("selection_color", selection_color);
  139. text_edit->add_color_override("brace_mismatch_color", brace_mismatch_color);
  140. text_edit->add_color_override("current_line_color", current_line_color);
  141. text_edit->add_color_override("line_length_guideline_color", line_length_guideline_color);
  142. text_edit->add_color_override("word_highlighted_color", word_highlighted_color);
  143. text_edit->add_color_override("number_color", number_color);
  144. text_edit->add_color_override("function_color", function_color);
  145. text_edit->add_color_override("member_variable_color", member_variable_color);
  146. text_edit->add_color_override("breakpoint_color", breakpoint_color);
  147. text_edit->add_color_override("mark_color", mark_color);
  148. text_edit->add_color_override("code_folding_color", code_folding_color);
  149. text_edit->add_color_override("search_result_color", search_result_color);
  150. text_edit->add_color_override("search_result_border_color", search_result_border_color);
  151. text_edit->add_color_override("symbol_color", symbol_color);
  152. text_edit->add_constant_override("line_spacing", EDITOR_DEF("text_editor/theme/line_spacing", 4));
  153. colors_cache.symbol_color = symbol_color;
  154. colors_cache.keyword_color = keyword_color;
  155. colors_cache.basetype_color = basetype_color;
  156. colors_cache.type_color = type_color;
  157. colors_cache.comment_color = comment_color;
  158. colors_cache.string_color = string_color;
  159. theme_loaded = true;
  160. if (!script.is_null())
  161. _set_theme_for_script();
  162. }
  163. void ScriptTextEditor::_set_theme_for_script() {
  164. if (!theme_loaded)
  165. return;
  166. TextEdit *text_edit = code_editor->get_text_edit();
  167. List<String> keywords;
  168. script->get_language()->get_reserved_words(&keywords);
  169. for (List<String>::Element *E = keywords.front(); E; E = E->next()) {
  170. text_edit->add_keyword_color(E->get(), colors_cache.keyword_color);
  171. }
  172. //colorize core types
  173. const Color basetype_color = colors_cache.basetype_color;
  174. text_edit->add_keyword_color("String", basetype_color);
  175. text_edit->add_keyword_color("Vector2", basetype_color);
  176. text_edit->add_keyword_color("Rect2", basetype_color);
  177. text_edit->add_keyword_color("Transform2D", basetype_color);
  178. text_edit->add_keyword_color("Vector3", basetype_color);
  179. text_edit->add_keyword_color("AABB", basetype_color);
  180. text_edit->add_keyword_color("Basis", basetype_color);
  181. text_edit->add_keyword_color("Plane", basetype_color);
  182. text_edit->add_keyword_color("Transform", basetype_color);
  183. text_edit->add_keyword_color("Quat", basetype_color);
  184. text_edit->add_keyword_color("Color", basetype_color);
  185. text_edit->add_keyword_color("Object", basetype_color);
  186. text_edit->add_keyword_color("NodePath", basetype_color);
  187. text_edit->add_keyword_color("RID", basetype_color);
  188. text_edit->add_keyword_color("Dictionary", basetype_color);
  189. text_edit->add_keyword_color("Array", basetype_color);
  190. text_edit->add_keyword_color("PoolByteArray", basetype_color);
  191. text_edit->add_keyword_color("PoolIntArray", basetype_color);
  192. text_edit->add_keyword_color("PoolRealArray", basetype_color);
  193. text_edit->add_keyword_color("PoolStringArray", basetype_color);
  194. text_edit->add_keyword_color("PoolVector2Array", basetype_color);
  195. text_edit->add_keyword_color("PoolVector3Array", basetype_color);
  196. text_edit->add_keyword_color("PoolColorArray", basetype_color);
  197. //colorize engine types
  198. List<StringName> types;
  199. ClassDB::get_class_list(&types);
  200. for (List<StringName>::Element *E = types.front(); E; E = E->next()) {
  201. String n = E->get();
  202. if (n.begins_with("_"))
  203. n = n.substr(1, n.length());
  204. text_edit->add_keyword_color(n, colors_cache.type_color);
  205. }
  206. //colorize comments
  207. List<String> comments;
  208. script->get_language()->get_comment_delimiters(&comments);
  209. for (List<String>::Element *E = comments.front(); E; E = E->next()) {
  210. String comment = E->get();
  211. String beg = comment.get_slice(" ", 0);
  212. String end = comment.get_slice_count(" ") > 1 ? comment.get_slice(" ", 1) : String();
  213. text_edit->add_color_region(beg, end, colors_cache.comment_color, end == "");
  214. }
  215. //colorize strings
  216. List<String> strings;
  217. script->get_language()->get_string_delimiters(&strings);
  218. for (List<String>::Element *E = strings.front(); E; E = E->next()) {
  219. String string = E->get();
  220. String beg = string.get_slice(" ", 0);
  221. String end = string.get_slice_count(" ") > 1 ? string.get_slice(" ", 1) : String();
  222. text_edit->add_color_region(beg, end, colors_cache.string_color, end == "");
  223. }
  224. }
  225. void ScriptTextEditor::reload_text() {
  226. ERR_FAIL_COND(script.is_null());
  227. TextEdit *te = code_editor->get_text_edit();
  228. int column = te->cursor_get_column();
  229. int row = te->cursor_get_line();
  230. int h = te->get_h_scroll();
  231. int v = te->get_v_scroll();
  232. te->set_text(script->get_source_code());
  233. te->clear_undo_history();
  234. te->cursor_set_line(row);
  235. te->cursor_set_column(column);
  236. te->set_h_scroll(h);
  237. te->set_v_scroll(v);
  238. te->tag_saved_version();
  239. code_editor->update_line_and_column();
  240. }
  241. void ScriptTextEditor::_notification(int p_what) {
  242. switch (p_what) {
  243. case NOTIFICATION_READY:
  244. _load_theme_settings();
  245. break;
  246. }
  247. }
  248. void ScriptTextEditor::add_callback(const String &p_function, PoolStringArray p_args) {
  249. String code = code_editor->get_text_edit()->get_text();
  250. int pos = script->get_language()->find_function(p_function, code);
  251. if (pos == -1) {
  252. //does not exist
  253. code_editor->get_text_edit()->deselect();
  254. pos = code_editor->get_text_edit()->get_line_count() + 2;
  255. String func = script->get_language()->make_function("", p_function, p_args);
  256. //code=code+func;
  257. code_editor->get_text_edit()->cursor_set_line(pos + 1);
  258. code_editor->get_text_edit()->cursor_set_column(1000000); //none shall be that big
  259. code_editor->get_text_edit()->insert_text_at_cursor("\n\n" + func);
  260. }
  261. code_editor->get_text_edit()->cursor_set_line(pos);
  262. code_editor->get_text_edit()->cursor_set_column(1);
  263. }
  264. bool ScriptTextEditor::show_members_overview() {
  265. return true;
  266. }
  267. void ScriptTextEditor::update_settings() {
  268. code_editor->update_editor_settings();
  269. }
  270. bool ScriptTextEditor::is_unsaved() {
  271. return code_editor->get_text_edit()->get_version() != code_editor->get_text_edit()->get_saved_version();
  272. }
  273. Variant ScriptTextEditor::get_edit_state() {
  274. Dictionary state;
  275. state["scroll_position"] = code_editor->get_text_edit()->get_v_scroll();
  276. state["column"] = code_editor->get_text_edit()->cursor_get_column();
  277. state["row"] = code_editor->get_text_edit()->cursor_get_line();
  278. return state;
  279. }
  280. void ScriptTextEditor::_convert_case(CaseStyle p_case) {
  281. TextEdit *te = code_editor->get_text_edit();
  282. Ref<Script> scr = get_edited_script();
  283. if (scr.is_null()) {
  284. return;
  285. }
  286. if (te->is_selection_active()) {
  287. te->begin_complex_operation();
  288. int begin = te->get_selection_from_line();
  289. int end = te->get_selection_to_line();
  290. int begin_col = te->get_selection_from_column();
  291. int end_col = te->get_selection_to_column();
  292. for (int i = begin; i <= end; i++) {
  293. String new_line = te->get_line(i);
  294. switch (p_case) {
  295. case UPPER: {
  296. new_line = new_line.to_upper();
  297. } break;
  298. case LOWER: {
  299. new_line = new_line.to_lower();
  300. } break;
  301. case CAPITALIZE: {
  302. new_line = new_line.capitalize();
  303. } break;
  304. }
  305. if (i == begin) {
  306. new_line = te->get_line(i).left(begin_col) + new_line.right(begin_col);
  307. }
  308. if (i == end) {
  309. new_line = new_line.left(end_col) + te->get_line(i).right(end_col);
  310. }
  311. te->set_line(i, new_line);
  312. }
  313. te->end_complex_operation();
  314. }
  315. }
  316. void ScriptTextEditor::trim_trailing_whitespace() {
  317. TextEdit *tx = code_editor->get_text_edit();
  318. bool trimed_whitespace = false;
  319. for (int i = 0; i < tx->get_line_count(); i++) {
  320. String line = tx->get_line(i);
  321. if (line.ends_with(" ") || line.ends_with("\t")) {
  322. if (!trimed_whitespace) {
  323. tx->begin_complex_operation();
  324. trimed_whitespace = true;
  325. }
  326. int end = 0;
  327. for (int j = line.length() - 1; j > -1; j--) {
  328. if (line[j] != ' ' && line[j] != '\t') {
  329. end = j + 1;
  330. break;
  331. }
  332. }
  333. tx->set_line(i, line.substr(0, end));
  334. }
  335. }
  336. if (trimed_whitespace) {
  337. tx->end_complex_operation();
  338. tx->update();
  339. }
  340. }
  341. void ScriptTextEditor::convert_indent_to_spaces() {
  342. TextEdit *tx = code_editor->get_text_edit();
  343. Ref<Script> scr = get_edited_script();
  344. if (scr.is_null()) {
  345. return;
  346. }
  347. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  348. String indent = "";
  349. for (int i = 0; i < indent_size; i++) {
  350. indent += " ";
  351. }
  352. int cursor_line = tx->cursor_get_line();
  353. int cursor_column = tx->cursor_get_column();
  354. bool changed_indentation = false;
  355. for (int i = 0; i < tx->get_line_count(); i++) {
  356. String line = tx->get_line(i);
  357. if (line.length() <= 0) {
  358. continue;
  359. }
  360. int j = 0;
  361. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  362. if (line[j] == '\t') {
  363. if (!changed_indentation) {
  364. tx->begin_complex_operation();
  365. changed_indentation = true;
  366. }
  367. if (cursor_line == i && cursor_column > j) {
  368. cursor_column += indent_size - 1;
  369. }
  370. line = line.left(j) + indent + line.right(j + 1);
  371. }
  372. j++;
  373. }
  374. if (changed_indentation) {
  375. tx->set_line(i, line);
  376. }
  377. }
  378. if (changed_indentation) {
  379. tx->cursor_set_column(cursor_column);
  380. tx->end_complex_operation();
  381. tx->update();
  382. }
  383. }
  384. void ScriptTextEditor::convert_indent_to_tabs() {
  385. TextEdit *tx = code_editor->get_text_edit();
  386. Ref<Script> scr = get_edited_script();
  387. if (scr.is_null()) {
  388. return;
  389. }
  390. int indent_size = EditorSettings::get_singleton()->get("text_editor/indent/size");
  391. indent_size -= 1;
  392. int cursor_line = tx->cursor_get_line();
  393. int cursor_column = tx->cursor_get_column();
  394. bool changed_indentation = false;
  395. for (int i = 0; i < tx->get_line_count(); i++) {
  396. String line = tx->get_line(i);
  397. if (line.length() <= 0) {
  398. continue;
  399. }
  400. int j = 0;
  401. int space_count = -1;
  402. while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
  403. if (line[j] != '\t') {
  404. space_count++;
  405. if (space_count == indent_size) {
  406. if (!changed_indentation) {
  407. tx->begin_complex_operation();
  408. changed_indentation = true;
  409. }
  410. if (cursor_line == i && cursor_column > j) {
  411. cursor_column -= indent_size;
  412. }
  413. line = line.left(j - indent_size) + "\t" + line.right(j + 1);
  414. j = 0;
  415. space_count = -1;
  416. }
  417. } else {
  418. space_count = -1;
  419. }
  420. j++;
  421. }
  422. if (changed_indentation) {
  423. tx->set_line(i, line);
  424. }
  425. }
  426. if (changed_indentation) {
  427. tx->cursor_set_column(cursor_column);
  428. tx->end_complex_operation();
  429. tx->update();
  430. }
  431. }
  432. void ScriptTextEditor::tag_saved_version() {
  433. code_editor->get_text_edit()->tag_saved_version();
  434. }
  435. void ScriptTextEditor::goto_line(int p_line, bool p_with_error) {
  436. TextEdit *tx = code_editor->get_text_edit();
  437. tx->unfold_line(p_line);
  438. tx->call_deferred("cursor_set_line", p_line);
  439. }
  440. void ScriptTextEditor::ensure_focus() {
  441. code_editor->get_text_edit()->grab_focus();
  442. }
  443. void ScriptTextEditor::set_edit_state(const Variant &p_state) {
  444. Dictionary state = p_state;
  445. code_editor->get_text_edit()->cursor_set_column(state["column"]);
  446. code_editor->get_text_edit()->cursor_set_line(state["row"]);
  447. code_editor->get_text_edit()->set_v_scroll(state["scroll_position"]);
  448. code_editor->get_text_edit()->grab_focus();
  449. //int scroll_pos;
  450. //int cursor_column;
  451. //int cursor_row;
  452. }
  453. String ScriptTextEditor::get_name() {
  454. String name;
  455. if (script->get_path().find("local://") == -1 && script->get_path().find("::") == -1) {
  456. name = script->get_path().get_file();
  457. if (is_unsaved()) {
  458. name += "(*)";
  459. }
  460. } else if (script->get_name() != "")
  461. name = script->get_name();
  462. else
  463. name = script->get_class() + "(" + itos(script->get_instance_id()) + ")";
  464. return name;
  465. }
  466. Ref<Texture> ScriptTextEditor::get_icon() {
  467. if (get_parent_control() && get_parent_control()->has_icon(script->get_class(), "EditorIcons")) {
  468. return get_parent_control()->get_icon(script->get_class(), "EditorIcons");
  469. }
  470. return Ref<Texture>();
  471. }
  472. void ScriptTextEditor::set_edited_script(const Ref<Script> &p_script) {
  473. ERR_FAIL_COND(!script.is_null());
  474. script = p_script;
  475. code_editor->get_text_edit()->set_text(script->get_source_code());
  476. code_editor->get_text_edit()->clear_undo_history();
  477. code_editor->get_text_edit()->tag_saved_version();
  478. emit_signal("name_changed");
  479. code_editor->update_line_and_column();
  480. _set_theme_for_script();
  481. }
  482. void ScriptTextEditor::_validate_script() {
  483. String errortxt;
  484. int line = -1, col;
  485. TextEdit *te = code_editor->get_text_edit();
  486. String text = te->get_text();
  487. List<String> fnc;
  488. if (!script->get_language()->validate(text, line, col, errortxt, script->get_path(), &fnc)) {
  489. String error_text = "error(" + itos(line) + "," + itos(col) + "): " + errortxt;
  490. code_editor->set_error(error_text);
  491. } else {
  492. code_editor->set_error("");
  493. line = -1;
  494. if (!script->is_tool()) {
  495. script->set_source_code(text);
  496. script->update_exports();
  497. //script->reload(); //will update all the variables in property editors
  498. }
  499. functions.clear();
  500. for (List<String>::Element *E = fnc.front(); E; E = E->next()) {
  501. functions.push_back(E->get());
  502. }
  503. }
  504. line--;
  505. for (int i = 0; i < te->get_line_count(); i++) {
  506. te->set_line_as_marked(i, line == i);
  507. }
  508. emit_signal("name_changed");
  509. emit_signal("edited_script_changed");
  510. }
  511. static Node *_find_node_for_script(Node *p_base, Node *p_current, const Ref<Script> &p_script) {
  512. if (p_current->get_owner() != p_base && p_base != p_current)
  513. return NULL;
  514. Ref<Script> c = p_current->get_script();
  515. if (c == p_script)
  516. return p_current;
  517. for (int i = 0; i < p_current->get_child_count(); i++) {
  518. Node *found = _find_node_for_script(p_base, p_current->get_child(i), p_script);
  519. if (found)
  520. return found;
  521. }
  522. return NULL;
  523. }
  524. static void _find_changed_scripts_for_external_editor(Node *p_base, Node *p_current, Set<Ref<Script> > &r_scripts) {
  525. if (p_current->get_owner() != p_base && p_base != p_current)
  526. return;
  527. Ref<Script> c = p_current->get_script();
  528. if (c.is_valid())
  529. r_scripts.insert(c);
  530. for (int i = 0; i < p_current->get_child_count(); i++) {
  531. _find_changed_scripts_for_external_editor(p_base, p_current->get_child(i), r_scripts);
  532. }
  533. }
  534. void ScriptEditor::_update_modified_scripts_for_external_editor(Ref<Script> p_for_script) {
  535. if (!bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor")))
  536. return;
  537. ERR_FAIL_COND(!get_tree());
  538. Set<Ref<Script> > scripts;
  539. Node *base = get_tree()->get_edited_scene_root();
  540. if (base) {
  541. _find_changed_scripts_for_external_editor(base, base, scripts);
  542. }
  543. for (Set<Ref<Script> >::Element *E = scripts.front(); E; E = E->next()) {
  544. Ref<Script> script = E->get();
  545. if (p_for_script.is_valid() && p_for_script != script)
  546. continue;
  547. if (script->get_path() == "" || script->get_path().find("local://") != -1 || script->get_path().find("::") != -1) {
  548. continue; //internal script, who cares, though weird
  549. }
  550. uint64_t last_date = script->get_last_modified_time();
  551. uint64_t date = FileAccess::get_modified_time(script->get_path());
  552. if (last_date != date) {
  553. Ref<Script> rel_script = ResourceLoader::load(script->get_path(), script->get_class(), true);
  554. ERR_CONTINUE(!rel_script.is_valid());
  555. script->set_source_code(rel_script->get_source_code());
  556. script->set_last_modified_time(rel_script->get_last_modified_time());
  557. script->update_exports();
  558. }
  559. }
  560. }
  561. void ScriptTextEditor::_code_complete_scripts(void *p_ud, const String &p_code, List<String> *r_options, bool &r_force) {
  562. ScriptTextEditor *ste = (ScriptTextEditor *)p_ud;
  563. ste->_code_complete_script(p_code, r_options, r_force);
  564. }
  565. void ScriptTextEditor::_code_complete_script(const String &p_code, List<String> *r_options, bool &r_force) {
  566. if (color_panel->is_visible_in_tree()) return;
  567. Node *base = get_tree()->get_edited_scene_root();
  568. if (base) {
  569. base = _find_node_for_script(base, base, script);
  570. }
  571. String hint;
  572. Error err = script->get_language()->complete_code(p_code, script->get_path().get_base_dir(), base, r_options, r_force, hint);
  573. if (err == OK && hint != "") {
  574. code_editor->get_text_edit()->set_code_hint(hint);
  575. }
  576. }
  577. void ScriptTextEditor::_breakpoint_toggled(int p_row) {
  578. 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));
  579. }
  580. void ScriptTextEditor::_lookup_symbol(const String &p_symbol, int p_row, int p_column) {
  581. Node *base = get_tree()->get_edited_scene_root();
  582. if (base) {
  583. base = _find_node_for_script(base, base, script);
  584. }
  585. ScriptLanguage::LookupResult result;
  586. if (p_symbol.is_resource_file()) {
  587. List<String> scene_extensions;
  588. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  589. if (scene_extensions.find(p_symbol.get_extension())) {
  590. EditorNode::get_singleton()->load_scene(p_symbol);
  591. } else {
  592. EditorNode::get_singleton()->load_resource(p_symbol);
  593. }
  594. } else if (script->get_language()->lookup_code(code_editor->get_text_edit()->get_text_for_lookup_completion(), p_symbol, script->get_path().get_base_dir(), base, result) == OK) {
  595. _goto_line(p_row);
  596. switch (result.type) {
  597. case ScriptLanguage::LookupResult::RESULT_SCRIPT_LOCATION: {
  598. if (result.script.is_valid()) {
  599. emit_signal("request_open_script_at_line", result.script, result.location - 1);
  600. } else {
  601. emit_signal("request_save_history");
  602. _goto_line(result.location - 1);
  603. }
  604. } break;
  605. case ScriptLanguage::LookupResult::RESULT_CLASS: {
  606. emit_signal("go_to_help", "class_name:" + result.class_name);
  607. } break;
  608. case ScriptLanguage::LookupResult::RESULT_CLASS_CONSTANT: {
  609. StringName cname = result.class_name;
  610. bool success;
  611. while (true) {
  612. ClassDB::get_integer_constant(cname, result.class_member, &success);
  613. if (success) {
  614. result.class_name = cname;
  615. cname = ClassDB::get_parent_class(cname);
  616. } else {
  617. break;
  618. }
  619. }
  620. emit_signal("go_to_help", "class_constant:" + result.class_name + ":" + result.class_member);
  621. } break;
  622. case ScriptLanguage::LookupResult::RESULT_CLASS_PROPERTY: {
  623. emit_signal("go_to_help", "class_property:" + result.class_name + ":" + result.class_member);
  624. } break;
  625. case ScriptLanguage::LookupResult::RESULT_CLASS_METHOD: {
  626. StringName cname = result.class_name;
  627. while (true) {
  628. if (ClassDB::has_method(cname, result.class_member)) {
  629. result.class_name = cname;
  630. cname = ClassDB::get_parent_class(cname);
  631. } else {
  632. break;
  633. }
  634. }
  635. emit_signal("go_to_help", "class_method:" + result.class_name + ":" + result.class_member);
  636. } break;
  637. }
  638. }
  639. }
  640. void ScriptTextEditor::_edit_option(int p_op) {
  641. TextEdit *tx = code_editor->get_text_edit();
  642. switch (p_op) {
  643. case EDIT_UNDO: {
  644. tx->undo();
  645. tx->call_deferred("grab_focus");
  646. } break;
  647. case EDIT_REDO: {
  648. tx->redo();
  649. tx->call_deferred("grab_focus");
  650. } break;
  651. case EDIT_CUT: {
  652. tx->cut();
  653. tx->call_deferred("grab_focus");
  654. } break;
  655. case EDIT_COPY: {
  656. tx->copy();
  657. tx->call_deferred("grab_focus");
  658. } break;
  659. case EDIT_PASTE: {
  660. tx->paste();
  661. tx->call_deferred("grab_focus");
  662. } break;
  663. case EDIT_SELECT_ALL: {
  664. tx->select_all();
  665. tx->call_deferred("grab_focus");
  666. } break;
  667. case EDIT_MOVE_LINE_UP: {
  668. Ref<Script> scr = script;
  669. if (scr.is_null())
  670. return;
  671. tx->begin_complex_operation();
  672. if (tx->is_selection_active()) {
  673. int from_line = tx->get_selection_from_line();
  674. int from_col = tx->get_selection_from_column();
  675. int to_line = tx->get_selection_to_line();
  676. int to_column = tx->get_selection_to_column();
  677. for (int i = from_line; i <= to_line; i++) {
  678. int line_id = i;
  679. int next_id = i - 1;
  680. if (line_id == 0 || next_id < 0)
  681. return;
  682. tx->unfold_line(line_id);
  683. tx->unfold_line(next_id);
  684. tx->swap_lines(line_id, next_id);
  685. tx->cursor_set_line(next_id);
  686. }
  687. int from_line_up = from_line > 0 ? from_line - 1 : from_line;
  688. int to_line_up = to_line > 0 ? to_line - 1 : to_line;
  689. tx->select(from_line_up, from_col, to_line_up, to_column);
  690. } else {
  691. int line_id = tx->cursor_get_line();
  692. int next_id = line_id - 1;
  693. if (line_id == 0 || next_id < 0)
  694. return;
  695. tx->unfold_line(line_id);
  696. tx->unfold_line(next_id);
  697. tx->swap_lines(line_id, next_id);
  698. tx->cursor_set_line(next_id);
  699. }
  700. tx->end_complex_operation();
  701. tx->update();
  702. } break;
  703. case EDIT_MOVE_LINE_DOWN: {
  704. Ref<Script> scr = get_edited_script();
  705. if (scr.is_null())
  706. return;
  707. tx->begin_complex_operation();
  708. if (tx->is_selection_active()) {
  709. int from_line = tx->get_selection_from_line();
  710. int from_col = tx->get_selection_from_column();
  711. int to_line = tx->get_selection_to_line();
  712. int to_column = tx->get_selection_to_column();
  713. for (int i = to_line; i >= from_line; i--) {
  714. int line_id = i;
  715. int next_id = i + 1;
  716. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  717. return;
  718. tx->unfold_line(line_id);
  719. tx->unfold_line(next_id);
  720. tx->swap_lines(line_id, next_id);
  721. tx->cursor_set_line(next_id);
  722. }
  723. int from_line_down = from_line < tx->get_line_count() ? from_line + 1 : from_line;
  724. int to_line_down = to_line < tx->get_line_count() ? to_line + 1 : to_line;
  725. tx->select(from_line_down, from_col, to_line_down, to_column);
  726. } else {
  727. int line_id = tx->cursor_get_line();
  728. int next_id = line_id + 1;
  729. if (line_id == tx->get_line_count() - 1 || next_id > tx->get_line_count())
  730. return;
  731. tx->unfold_line(line_id);
  732. tx->unfold_line(next_id);
  733. tx->swap_lines(line_id, next_id);
  734. tx->cursor_set_line(next_id);
  735. }
  736. tx->end_complex_operation();
  737. tx->update();
  738. } break;
  739. case EDIT_INDENT_LEFT: {
  740. Ref<Script> scr = get_edited_script();
  741. if (scr.is_null())
  742. return;
  743. tx->begin_complex_operation();
  744. if (tx->is_selection_active()) {
  745. tx->indent_selection_left();
  746. } else {
  747. int begin = tx->cursor_get_line();
  748. String line_text = tx->get_line(begin);
  749. // begins with tab
  750. if (line_text.begins_with("\t")) {
  751. line_text = line_text.substr(1, line_text.length());
  752. tx->set_line(begin, line_text);
  753. }
  754. // begins with 4 spaces
  755. else if (line_text.begins_with(" ")) {
  756. line_text = line_text.substr(4, line_text.length());
  757. tx->set_line(begin, line_text);
  758. }
  759. }
  760. tx->end_complex_operation();
  761. tx->update();
  762. //tx->deselect();
  763. } break;
  764. case EDIT_INDENT_RIGHT: {
  765. Ref<Script> scr = get_edited_script();
  766. if (scr.is_null())
  767. return;
  768. tx->begin_complex_operation();
  769. if (tx->is_selection_active()) {
  770. tx->indent_selection_right();
  771. } else {
  772. int begin = tx->cursor_get_line();
  773. String line_text = tx->get_line(begin);
  774. line_text = '\t' + line_text;
  775. tx->set_line(begin, line_text);
  776. }
  777. tx->end_complex_operation();
  778. tx->update();
  779. //tx->deselect();
  780. } break;
  781. case EDIT_DELETE_LINE: {
  782. Ref<Script> scr = get_edited_script();
  783. if (scr.is_null())
  784. return;
  785. tx->begin_complex_operation();
  786. int line = tx->cursor_get_line();
  787. tx->set_line(tx->cursor_get_line(), "");
  788. tx->backspace_at_cursor();
  789. tx->unfold_line(line);
  790. tx->cursor_set_line(line);
  791. tx->end_complex_operation();
  792. } break;
  793. case EDIT_CLONE_DOWN: {
  794. Ref<Script> scr = get_edited_script();
  795. if (scr.is_null())
  796. return;
  797. int from_line = tx->cursor_get_line();
  798. int to_line = tx->cursor_get_line();
  799. int column = tx->cursor_get_column();
  800. if (tx->is_selection_active()) {
  801. from_line = tx->get_selection_from_line();
  802. to_line = tx->get_selection_to_line();
  803. column = tx->cursor_get_column();
  804. }
  805. int next_line = to_line + 1;
  806. tx->begin_complex_operation();
  807. for (int i = from_line; i <= to_line; i++) {
  808. tx->unfold_line(i);
  809. if (i >= tx->get_line_count() - 1) {
  810. tx->set_line(i, tx->get_line(i) + "\n");
  811. }
  812. String line_clone = tx->get_line(i);
  813. tx->insert_at(line_clone, next_line);
  814. next_line++;
  815. }
  816. tx->cursor_set_column(column);
  817. if (tx->is_selection_active()) {
  818. tx->select(to_line + 1, tx->get_selection_from_column(), next_line - 1, tx->get_selection_to_column());
  819. }
  820. tx->end_complex_operation();
  821. tx->update();
  822. } break;
  823. case EDIT_FOLD_LINE: {
  824. tx->fold_line(tx->cursor_get_line());
  825. tx->update();
  826. } break;
  827. case EDIT_UNFOLD_LINE: {
  828. tx->unfold_line(tx->cursor_get_line());
  829. tx->update();
  830. } break;
  831. case EDIT_FOLD_ALL_LINES: {
  832. tx->fold_all_lines();
  833. tx->update();
  834. } break;
  835. case EDIT_UNFOLD_ALL_LINES: {
  836. tx->unhide_all_lines();
  837. tx->update();
  838. } break;
  839. case EDIT_TOGGLE_COMMENT: {
  840. Ref<Script> scr = get_edited_script();
  841. if (scr.is_null())
  842. return;
  843. String delimiter = "#";
  844. List<String> comment_delimiters;
  845. scr->get_language()->get_comment_delimiters(&comment_delimiters);
  846. for (List<String>::Element *E = comment_delimiters.front(); E; E = E->next()) {
  847. String script_delimiter = E->get();
  848. if (script_delimiter.find(" ") == -1) {
  849. delimiter = script_delimiter;
  850. break;
  851. }
  852. }
  853. tx->begin_complex_operation();
  854. if (tx->is_selection_active()) {
  855. int begin = tx->get_selection_from_line();
  856. int end = tx->get_selection_to_line();
  857. // End of selection ends on the first column of the last line, ignore it.
  858. if (tx->get_selection_to_column() == 0)
  859. end -= 1;
  860. // Check if all lines in the selected block are commented
  861. bool is_commented = true;
  862. for (int i = begin; i <= end; i++) {
  863. if (!tx->get_line(i).begins_with(delimiter)) {
  864. is_commented = false;
  865. break;
  866. }
  867. }
  868. for (int i = begin; i <= end; i++) {
  869. String line_text = tx->get_line(i);
  870. if (line_text.strip_edges().empty()) {
  871. line_text = delimiter;
  872. } else {
  873. if (is_commented) {
  874. line_text = line_text.substr(delimiter.length(), line_text.length());
  875. } else {
  876. line_text = delimiter + line_text;
  877. }
  878. }
  879. tx->set_line(i, line_text);
  880. }
  881. } else {
  882. int begin = tx->cursor_get_line();
  883. String line_text = tx->get_line(begin);
  884. if (line_text.begins_with(delimiter))
  885. line_text = line_text.substr(delimiter.length(), line_text.length());
  886. else
  887. line_text = delimiter + line_text;
  888. tx->set_line(begin, line_text);
  889. }
  890. tx->end_complex_operation();
  891. tx->update();
  892. //tx->deselect();
  893. } break;
  894. case EDIT_COMPLETE: {
  895. tx->query_code_comple();
  896. } break;
  897. case EDIT_AUTO_INDENT: {
  898. String text = tx->get_text();
  899. Ref<Script> scr = get_edited_script();
  900. if (scr.is_null())
  901. return;
  902. tx->begin_complex_operation();
  903. int begin, end;
  904. if (tx->is_selection_active()) {
  905. begin = tx->get_selection_from_line();
  906. end = tx->get_selection_to_line();
  907. // ignore if the cursor is not past the first column
  908. if (tx->get_selection_to_column() == 0) {
  909. end--;
  910. }
  911. } else {
  912. begin = 0;
  913. end = tx->get_line_count() - 1;
  914. }
  915. scr->get_language()->auto_indent_code(text, begin, end);
  916. Vector<String> lines = text.split("\n");
  917. for (int i = begin; i <= end; ++i) {
  918. tx->set_line(i, lines[i]);
  919. }
  920. tx->end_complex_operation();
  921. } break;
  922. case EDIT_TRIM_TRAILING_WHITESAPCE: {
  923. trim_trailing_whitespace();
  924. } break;
  925. case EDIT_CONVERT_INDENT_TO_SPACES: {
  926. convert_indent_to_spaces();
  927. } break;
  928. case EDIT_CONVERT_INDENT_TO_TABS: {
  929. convert_indent_to_tabs();
  930. } break;
  931. case EDIT_PICK_COLOR: {
  932. color_panel->popup();
  933. } break;
  934. case EDIT_TO_UPPERCASE: {
  935. _convert_case(UPPER);
  936. } break;
  937. case EDIT_TO_LOWERCASE: {
  938. _convert_case(LOWER);
  939. } break;
  940. case EDIT_CAPITALIZE: {
  941. _convert_case(CAPITALIZE);
  942. } break;
  943. case SEARCH_FIND: {
  944. code_editor->get_find_replace_bar()->popup_search();
  945. } break;
  946. case SEARCH_FIND_NEXT: {
  947. code_editor->get_find_replace_bar()->search_next();
  948. } break;
  949. case SEARCH_FIND_PREV: {
  950. code_editor->get_find_replace_bar()->search_prev();
  951. } break;
  952. case SEARCH_REPLACE: {
  953. code_editor->get_find_replace_bar()->popup_replace();
  954. } break;
  955. case SEARCH_LOCATE_FUNCTION: {
  956. quick_open->popup(get_functions());
  957. } break;
  958. case SEARCH_GOTO_LINE: {
  959. goto_line_dialog->popup_find_line(tx);
  960. } break;
  961. case DEBUG_TOGGLE_BREAKPOINT: {
  962. int line = tx->cursor_get_line();
  963. bool dobreak = !tx->is_line_set_as_breakpoint(line);
  964. tx->set_line_as_breakpoint(line, dobreak);
  965. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  966. } break;
  967. case DEBUG_REMOVE_ALL_BREAKPOINTS: {
  968. List<int> bpoints;
  969. tx->get_breakpoints(&bpoints);
  970. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  971. int line = E->get();
  972. bool dobreak = !tx->is_line_set_as_breakpoint(line);
  973. tx->set_line_as_breakpoint(line, dobreak);
  974. ScriptEditor::get_singleton()->get_debugger()->set_breakpoint(get_edited_script()->get_path(), line + 1, dobreak);
  975. }
  976. }
  977. case DEBUG_GOTO_NEXT_BREAKPOINT: {
  978. List<int> bpoints;
  979. tx->get_breakpoints(&bpoints);
  980. if (bpoints.size() <= 0) {
  981. return;
  982. }
  983. int line = tx->cursor_get_line();
  984. // wrap around
  985. if (line >= bpoints[bpoints.size() - 1]) {
  986. tx->unfold_line(bpoints[0]);
  987. tx->cursor_set_line(bpoints[0]);
  988. } else {
  989. for (List<int>::Element *E = bpoints.front(); E; E = E->next()) {
  990. int bline = E->get();
  991. if (bline > line) {
  992. tx->unfold_line(bline);
  993. tx->cursor_set_line(bline);
  994. return;
  995. }
  996. }
  997. }
  998. } break;
  999. case DEBUG_GOTO_PREV_BREAKPOINT: {
  1000. List<int> bpoints;
  1001. tx->get_breakpoints(&bpoints);
  1002. if (bpoints.size() <= 0) {
  1003. return;
  1004. }
  1005. int line = tx->cursor_get_line();
  1006. // wrap around
  1007. if (line <= bpoints[0]) {
  1008. tx->unfold_line(bpoints[bpoints.size() - 1]);
  1009. tx->cursor_set_line(bpoints[bpoints.size() - 1]);
  1010. } else {
  1011. for (List<int>::Element *E = bpoints.back(); E; E = E->prev()) {
  1012. int bline = E->get();
  1013. if (bline < line) {
  1014. tx->unfold_line(bline);
  1015. tx->cursor_set_line(bline);
  1016. return;
  1017. }
  1018. }
  1019. }
  1020. } break;
  1021. case HELP_CONTEXTUAL: {
  1022. String text = tx->get_selection_text();
  1023. if (text == "")
  1024. text = tx->get_word_under_cursor();
  1025. if (text != "") {
  1026. emit_signal("request_help_search", text);
  1027. }
  1028. } break;
  1029. }
  1030. }
  1031. void ScriptTextEditor::_bind_methods() {
  1032. ClassDB::bind_method("_validate_script", &ScriptTextEditor::_validate_script);
  1033. ClassDB::bind_method("_load_theme_settings", &ScriptTextEditor::_load_theme_settings);
  1034. ClassDB::bind_method("_breakpoint_toggled", &ScriptTextEditor::_breakpoint_toggled);
  1035. ClassDB::bind_method("_edit_option", &ScriptTextEditor::_edit_option);
  1036. ClassDB::bind_method("_goto_line", &ScriptTextEditor::_goto_line);
  1037. ClassDB::bind_method("_lookup_symbol", &ScriptTextEditor::_lookup_symbol);
  1038. ClassDB::bind_method("_text_edit_gui_input", &ScriptTextEditor::_text_edit_gui_input);
  1039. ClassDB::bind_method("_color_changed", &ScriptTextEditor::_color_changed);
  1040. ClassDB::bind_method("get_drag_data_fw", &ScriptTextEditor::get_drag_data_fw);
  1041. ClassDB::bind_method("can_drop_data_fw", &ScriptTextEditor::can_drop_data_fw);
  1042. ClassDB::bind_method("drop_data_fw", &ScriptTextEditor::drop_data_fw);
  1043. }
  1044. Control *ScriptTextEditor::get_edit_menu() {
  1045. return edit_hb;
  1046. }
  1047. void ScriptTextEditor::clear_edit_menu() {
  1048. memdelete(edit_hb);
  1049. }
  1050. void ScriptTextEditor::reload(bool p_soft) {
  1051. TextEdit *te = code_editor->get_text_edit();
  1052. Ref<Script> scr = get_edited_script();
  1053. if (scr.is_null())
  1054. return;
  1055. scr->set_source_code(te->get_text());
  1056. bool soft = p_soft || scr->get_instance_base_type() == "EditorPlugin"; //always soft-reload editor plugins
  1057. scr->get_language()->reload_tool_script(scr, soft);
  1058. }
  1059. void ScriptTextEditor::get_breakpoints(List<int> *p_breakpoints) {
  1060. code_editor->get_text_edit()->get_breakpoints(p_breakpoints);
  1061. }
  1062. void ScriptTextEditor::set_tooltip_request_func(String p_method, Object *p_obj) {
  1063. code_editor->get_text_edit()->set_tooltip_request_func(p_obj, p_method, this);
  1064. }
  1065. void ScriptTextEditor::set_debugger_active(bool p_active) {
  1066. }
  1067. Variant ScriptTextEditor::get_drag_data_fw(const Point2 &p_point, Control *p_from) {
  1068. return Variant();
  1069. }
  1070. bool ScriptTextEditor::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  1071. Dictionary d = p_data;
  1072. if (d.has("type") &&
  1073. (
  1074. String(d["type"]) == "resource" ||
  1075. String(d["type"]) == "files" ||
  1076. String(d["type"]) == "nodes")) {
  1077. return true;
  1078. }
  1079. return false;
  1080. }
  1081. #ifdef TOOLS_ENABLED
  1082. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1083. if (p_edited_scene != p_current_node && p_current_node->get_owner() != p_edited_scene)
  1084. return NULL;
  1085. Ref<Script> scr = p_current_node->get_script();
  1086. if (scr.is_valid() && scr == script)
  1087. return p_current_node;
  1088. for (int i = 0; i < p_current_node->get_child_count(); i++) {
  1089. Node *n = _find_script_node(p_edited_scene, p_current_node->get_child(i), script);
  1090. if (n)
  1091. return n;
  1092. }
  1093. return NULL;
  1094. }
  1095. #else
  1096. static Node *_find_script_node(Node *p_edited_scene, Node *p_current_node, const Ref<Script> &script) {
  1097. return NULL;
  1098. }
  1099. #endif
  1100. void ScriptTextEditor::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  1101. Dictionary d = p_data;
  1102. if (d.has("type") && String(d["type"]) == "resource") {
  1103. Ref<Resource> res = d["resource"];
  1104. if (!res.is_valid()) {
  1105. return;
  1106. }
  1107. if (res->get_path().is_resource_file()) {
  1108. EditorNode::get_singleton()->show_warning(TTR("Only resources from filesystem can be dropped."));
  1109. return;
  1110. }
  1111. code_editor->get_text_edit()->insert_text_at_cursor(res->get_path());
  1112. }
  1113. if (d.has("type") && String(d["type"]) == "files") {
  1114. Array files = d["files"];
  1115. String text_to_drop;
  1116. for (int i = 0; i < files.size(); i++) {
  1117. if (i > 0)
  1118. text_to_drop += ",";
  1119. text_to_drop += "\"" + String(files[i]).c_escape() + "\"";
  1120. }
  1121. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  1122. }
  1123. if (d.has("type") && String(d["type"]) == "nodes") {
  1124. Node *sn = _find_script_node(get_tree()->get_edited_scene_root(), get_tree()->get_edited_scene_root(), script);
  1125. if (!sn) {
  1126. EditorNode::get_singleton()->show_warning("Can't drop nodes because script '" + get_name() + "' is not used in this scene.");
  1127. return;
  1128. }
  1129. Array nodes = d["nodes"];
  1130. String text_to_drop;
  1131. for (int i = 0; i < nodes.size(); i++) {
  1132. if (i > 0)
  1133. text_to_drop += ",";
  1134. NodePath np = nodes[i];
  1135. Node *node = get_node(np);
  1136. if (!node) {
  1137. continue;
  1138. }
  1139. String path = sn->get_path_to(node);
  1140. text_to_drop += "\"" + path.c_escape() + "\"";
  1141. }
  1142. code_editor->get_text_edit()->insert_text_at_cursor(text_to_drop);
  1143. }
  1144. }
  1145. void ScriptTextEditor::_text_edit_gui_input(const Ref<InputEvent> &ev) {
  1146. Ref<InputEventMouseButton> mb = ev;
  1147. if (mb.is_valid()) {
  1148. if (mb->get_button_index() == BUTTON_RIGHT && !mb->is_pressed()) {
  1149. int col, row;
  1150. TextEdit *tx = code_editor->get_text_edit();
  1151. tx->_get_mouse_pos(mb->get_global_position() - tx->get_global_position(), row, col);
  1152. Vector2 mpos = mb->get_global_position() - tx->get_global_position();
  1153. bool have_selection = (tx->get_selection_text().length() > 0);
  1154. bool have_color = (tx->get_word_at_pos(mpos) == "Color");
  1155. int fold_state = 0;
  1156. bool can_fold = tx->can_fold(row);
  1157. bool is_folded = tx->is_folded(row);
  1158. if (have_color) {
  1159. String line = tx->get_line(row);
  1160. color_line = row;
  1161. int begin = 0;
  1162. int end = 0;
  1163. bool valid = false;
  1164. for (int i = col; i < line.length(); i++) {
  1165. if (line[i] == '(') {
  1166. begin = i;
  1167. continue;
  1168. } else if (line[i] == ')') {
  1169. end = i + 1;
  1170. valid = true;
  1171. break;
  1172. }
  1173. }
  1174. if (valid) {
  1175. color_args = line.substr(begin, end - begin);
  1176. String stripped = color_args.replace(" ", "").replace("(", "").replace(")", "");
  1177. Vector<float> color = stripped.split_floats(",");
  1178. if (color.size() > 2) {
  1179. float alpha = color.size() > 3 ? color[3] : 1.0f;
  1180. color_picker->set_pick_color(Color(color[0], color[1], color[2], alpha));
  1181. }
  1182. color_panel->set_position(get_global_transform().xform(get_local_mouse_position()));
  1183. } else {
  1184. have_color = false;
  1185. }
  1186. }
  1187. _make_context_menu(have_selection, have_color, can_fold, is_folded);
  1188. }
  1189. }
  1190. }
  1191. void ScriptTextEditor::_color_changed(const Color &p_color) {
  1192. String new_args;
  1193. if (p_color.a == 1.0f) {
  1194. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ")");
  1195. } else {
  1196. new_args = String("(" + rtos(p_color.r) + ", " + rtos(p_color.g) + ", " + rtos(p_color.b) + ", " + rtos(p_color.a) + ")");
  1197. }
  1198. String line = code_editor->get_text_edit()->get_line(color_line);
  1199. String new_line = line.replace(color_args, new_args);
  1200. color_args = new_args;
  1201. code_editor->get_text_edit()->set_line(color_line, new_line);
  1202. }
  1203. void ScriptTextEditor::_make_context_menu(bool p_selection, bool p_color, bool p_can_fold, bool p_is_folded) {
  1204. context_menu->clear();
  1205. if (p_selection) {
  1206. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1207. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1208. }
  1209. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1210. context_menu->add_separator();
  1211. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1212. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1213. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1214. if (p_selection) {
  1215. context_menu->add_separator();
  1216. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
  1217. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
  1218. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1219. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1220. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1221. }
  1222. if (p_can_fold) {
  1223. // can fold
  1224. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_line"), EDIT_FOLD_LINE);
  1225. } else if (p_is_folded) {
  1226. // can unfold
  1227. context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_line"), EDIT_UNFOLD_LINE);
  1228. }
  1229. if (p_color) {
  1230. context_menu->add_separator();
  1231. context_menu->add_item(TTR("Pick Color"), EDIT_PICK_COLOR);
  1232. }
  1233. context_menu->set_position(get_global_transform().xform(get_local_mouse_position()));
  1234. context_menu->set_size(Vector2(1, 1));
  1235. context_menu->popup();
  1236. }
  1237. ScriptTextEditor::ScriptTextEditor() {
  1238. theme_loaded = false;
  1239. code_editor = memnew(CodeTextEditor);
  1240. add_child(code_editor);
  1241. code_editor->add_constant_override("separation", 0);
  1242. code_editor->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  1243. code_editor->connect("validate_script", this, "_validate_script");
  1244. code_editor->connect("load_theme_settings", this, "_load_theme_settings");
  1245. code_editor->set_code_complete_func(_code_complete_scripts, this);
  1246. code_editor->get_text_edit()->connect("breakpoint_toggled", this, "_breakpoint_toggled");
  1247. code_editor->get_text_edit()->connect("symbol_lookup", this, "_lookup_symbol");
  1248. update_settings();
  1249. code_editor->get_text_edit()->set_callhint_settings(
  1250. EditorSettings::get_singleton()->get("text_editor/completion/put_callhint_tooltip_below_current_line"),
  1251. EditorSettings::get_singleton()->get("text_editor/completion/callhint_tooltip_offset"));
  1252. code_editor->get_text_edit()->set_select_identifiers_on_hover(true);
  1253. code_editor->get_text_edit()->set_context_menu_enabled(false);
  1254. code_editor->get_text_edit()->connect("gui_input", this, "_text_edit_gui_input");
  1255. context_menu = memnew(PopupMenu);
  1256. add_child(context_menu);
  1257. context_menu->connect("id_pressed", this, "_edit_option");
  1258. color_panel = memnew(PopupPanel);
  1259. add_child(color_panel);
  1260. color_picker = memnew(ColorPicker);
  1261. color_panel->add_child(color_picker);
  1262. color_picker->connect("color_changed", this, "_color_changed");
  1263. edit_hb = memnew(HBoxContainer);
  1264. edit_menu = memnew(MenuButton);
  1265. edit_menu->set_text(TTR("Edit"));
  1266. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/undo"), EDIT_UNDO);
  1267. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/redo"), EDIT_REDO);
  1268. edit_menu->get_popup()->add_separator();
  1269. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/cut"), EDIT_CUT);
  1270. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/copy"), EDIT_COPY);
  1271. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/paste"), EDIT_PASTE);
  1272. edit_menu->get_popup()->add_separator();
  1273. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/select_all"), EDIT_SELECT_ALL);
  1274. edit_menu->get_popup()->add_separator();
  1275. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
  1276. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
  1277. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_left"), EDIT_INDENT_LEFT);
  1278. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent_right"), EDIT_INDENT_RIGHT);
  1279. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
  1280. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
  1281. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/clone_down"), EDIT_CLONE_DOWN);
  1282. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_line"), EDIT_FOLD_LINE);
  1283. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
  1284. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_line"), EDIT_UNFOLD_LINE);
  1285. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
  1286. edit_menu->get_popup()->add_separator();
  1287. #ifdef OSX_ENABLED
  1288. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1289. #else
  1290. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/complete_symbol"), EDIT_COMPLETE);
  1291. #endif
  1292. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
  1293. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
  1294. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
  1295. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/auto_indent"), EDIT_AUTO_INDENT);
  1296. edit_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1297. edit_menu->get_popup()->add_separator();
  1298. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_breakpoint"), DEBUG_TOGGLE_BREAKPOINT);
  1299. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_breakpoints"), DEBUG_REMOVE_ALL_BREAKPOINTS);
  1300. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_breakpoint"), DEBUG_GOTO_NEXT_BREAKPOINT);
  1301. edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_breakpoint"), DEBUG_GOTO_PREV_BREAKPOINT);
  1302. edit_menu->get_popup()->add_separator();
  1303. PopupMenu *convert_case = memnew(PopupMenu);
  1304. convert_case->set_name("convert_case");
  1305. edit_menu->get_popup()->add_child(convert_case);
  1306. edit_menu->get_popup()->add_submenu_item(TTR("Convert Case"), "convert_case");
  1307. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Uppercase")), EDIT_TO_UPPERCASE);
  1308. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Lowercase")), EDIT_TO_LOWERCASE);
  1309. convert_case->add_shortcut(ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize")), EDIT_CAPITALIZE);
  1310. convert_case->connect("id_pressed", this, "_edit_option");
  1311. search_menu = memnew(MenuButton);
  1312. edit_hb->add_child(search_menu);
  1313. search_menu->set_text(TTR("Search"));
  1314. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
  1315. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
  1316. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
  1317. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
  1318. search_menu->get_popup()->add_separator();
  1319. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_function"), SEARCH_LOCATE_FUNCTION);
  1320. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
  1321. search_menu->get_popup()->add_separator();
  1322. search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/contextual_help"), HELP_CONTEXTUAL);
  1323. search_menu->get_popup()->connect("id_pressed", this, "_edit_option");
  1324. edit_hb->add_child(edit_menu);
  1325. quick_open = memnew(ScriptEditorQuickOpen);
  1326. add_child(quick_open);
  1327. quick_open->connect("goto_line", this, "_goto_line");
  1328. goto_line_dialog = memnew(GotoLineDialog);
  1329. add_child(goto_line_dialog);
  1330. code_editor->get_text_edit()->set_drag_forwarding(this);
  1331. }
  1332. static ScriptEditorBase *create_editor(const Ref<Script> &p_script) {
  1333. return memnew(ScriptTextEditor);
  1334. }
  1335. void ScriptTextEditor::register_editor() {
  1336. ED_SHORTCUT("script_text_editor/undo", TTR("Undo"), KEY_MASK_CMD | KEY_Z);
  1337. ED_SHORTCUT("script_text_editor/redo", TTR("Redo"), KEY_MASK_CMD | KEY_Y);
  1338. ED_SHORTCUT("script_text_editor/cut", TTR("Cut"), KEY_MASK_CMD | KEY_X);
  1339. ED_SHORTCUT("script_text_editor/copy", TTR("Copy"), KEY_MASK_CMD | KEY_C);
  1340. ED_SHORTCUT("script_text_editor/paste", TTR("Paste"), KEY_MASK_CMD | KEY_V);
  1341. ED_SHORTCUT("script_text_editor/select_all", TTR("Select All"), KEY_MASK_CMD | KEY_A);
  1342. ED_SHORTCUT("script_text_editor/move_up", TTR("Move Up"), KEY_MASK_ALT | KEY_UP);
  1343. ED_SHORTCUT("script_text_editor/move_down", TTR("Move Down"), KEY_MASK_ALT | KEY_DOWN);
  1344. ED_SHORTCUT("script_text_editor/delete_line", TTR("Delete Line"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_K);
  1345. //leave these at zero, same can be accomplished with tab/shift-tab, including selection
  1346. //the next/previous in history shortcut in this case makes a lot more sene.
  1347. ED_SHORTCUT("script_text_editor/indent_left", TTR("Indent Left"), 0);
  1348. ED_SHORTCUT("script_text_editor/indent_right", TTR("Indent Right"), 0);
  1349. ED_SHORTCUT("script_text_editor/toggle_comment", TTR("Toggle Comment"), KEY_MASK_CMD | KEY_K);
  1350. ED_SHORTCUT("script_text_editor/clone_down", TTR("Clone Down"), KEY_MASK_CMD | KEY_B);
  1351. ED_SHORTCUT("script_text_editor/fold_line", TTR("Fold Line"), KEY_MASK_ALT | KEY_LEFT);
  1352. ED_SHORTCUT("script_text_editor/unfold_line", TTR("Unfold Line"), KEY_MASK_ALT | KEY_RIGHT);
  1353. ED_SHORTCUT("script_text_editor/fold_all_lines", TTR("Fold All Lines"), 0);
  1354. ED_SHORTCUT("script_text_editor/unfold_all_lines", TTR("Unfold All Lines"), 0);
  1355. #ifdef OSX_ENABLED
  1356. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CTRL | KEY_SPACE);
  1357. #else
  1358. ED_SHORTCUT("script_text_editor/complete_symbol", TTR("Complete Symbol"), KEY_MASK_CMD | KEY_SPACE);
  1359. #endif
  1360. ED_SHORTCUT("script_text_editor/trim_trailing_whitespace", TTR("Trim Trailing Whitespace"), KEY_MASK_CTRL | KEY_MASK_ALT | KEY_T);
  1361. ED_SHORTCUT("script_text_editor/convert_indent_to_spaces", TTR("Convert Indent To Spaces"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_Y);
  1362. ED_SHORTCUT("script_text_editor/convert_indent_to_tabs", TTR("Convert Indent To Tabs"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_X);
  1363. ED_SHORTCUT("script_text_editor/auto_indent", TTR("Auto Indent"), KEY_MASK_CMD | KEY_I);
  1364. ED_SHORTCUT("script_text_editor/toggle_breakpoint", TTR("Toggle Breakpoint"), KEY_F9);
  1365. ED_SHORTCUT("script_text_editor/remove_all_breakpoints", TTR("Remove All Breakpoints"), KEY_MASK_CTRL | KEY_MASK_SHIFT | KEY_F9);
  1366. ED_SHORTCUT("script_text_editor/goto_next_breakpoint", TTR("Goto Next Breakpoint"), KEY_MASK_CTRL | KEY_PERIOD);
  1367. ED_SHORTCUT("script_text_editor/goto_previous_breakpoint", TTR("Goto Previous Breakpoint"), KEY_MASK_CTRL | KEY_COMMA);
  1368. ED_SHORTCUT("script_text_editor/convert_to_uppercase", TTR("Convert To Uppercase"), KEY_MASK_SHIFT | KEY_F4);
  1369. ED_SHORTCUT("script_text_editor/convert_to_lowercase", TTR("Convert To Lowercase"), KEY_MASK_SHIFT | KEY_F3);
  1370. ED_SHORTCUT("script_text_editor/capitalize", TTR("Capitalize"), KEY_MASK_SHIFT | KEY_F2);
  1371. ED_SHORTCUT("script_text_editor/find", TTR("Find.."), KEY_MASK_CMD | KEY_F);
  1372. ED_SHORTCUT("script_text_editor/find_next", TTR("Find Next"), KEY_F3);
  1373. ED_SHORTCUT("script_text_editor/find_previous", TTR("Find Previous"), KEY_MASK_SHIFT | KEY_F3);
  1374. ED_SHORTCUT("script_text_editor/replace", TTR("Replace.."), KEY_MASK_CMD | KEY_R);
  1375. ED_SHORTCUT("script_text_editor/goto_function", TTR("Goto Function.."), KEY_MASK_SHIFT | KEY_MASK_CMD | KEY_F);
  1376. ED_SHORTCUT("script_text_editor/goto_line", TTR("Goto Line.."), KEY_MASK_CMD | KEY_L);
  1377. ED_SHORTCUT("script_text_editor/contextual_help", TTR("Contextual Help"), KEY_MASK_SHIFT | KEY_F1);
  1378. ScriptEditor::register_create_script_editor_function(create_editor);
  1379. }